blob: 597f925f51c163ce086fb72634aea0dcd3a4bb7a [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf info into asm files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/DwarfWriter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000015#include "llvm/Module.h"
Devang Patelb3907da2009-01-05 23:03:32 +000016#include "llvm/DerivedTypes.h"
Devang Patel2da0cc42009-01-15 23:41:32 +000017#include "llvm/Constants.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/CodeGen/AsmPrinter.h"
19#include "llvm/CodeGen/MachineModuleInfo.h"
20#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineLocation.h"
Devang Patelfc187162009-01-05 17:57:47 +000022#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/Support/Debug.h"
24#include "llvm/Support/Dwarf.h"
25#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/DataTypes.h"
27#include "llvm/Support/Mangler.h"
Bill Wendlingcb3661f2009-03-10 20:41:52 +000028#include "llvm/Support/Timer.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000029#include "llvm/Support/raw_ostream.h"
Dan Gohman80bbde72007-09-24 21:32:18 +000030#include "llvm/System/Path.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000032#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/Target/TargetData.h"
34#include "llvm/Target/TargetFrameInfo.h"
35#include "llvm/Target/TargetInstrInfo.h"
36#include "llvm/Target/TargetMachine.h"
37#include "llvm/Target/TargetOptions.h"
Evan Cheng3e288912009-02-25 07:04:34 +000038#include "llvm/ADT/DenseMap.h"
39#include "llvm/ADT/FoldingSet.h"
40#include "llvm/ADT/StringExtras.h"
41#include "llvm/ADT/StringMap.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042#include <ostream>
43#include <string>
44using namespace llvm;
45using namespace llvm::dwarf;
46
Devang Patelaa1e8432009-01-08 23:40:34 +000047static RegisterPass<DwarfWriter>
48X("dwarfwriter", "DWARF Information Writer");
49char DwarfWriter::ID = 0;
50
Bill Wendling148ecc42009-03-10 22:58:53 +000051static TimerGroup &getDwarfTimerGroup() {
52 static TimerGroup DwarfTimerGroup("Dwarf Exception and Debugging");
53 return DwarfTimerGroup;
Bill Wendlingcb3661f2009-03-10 20:41:52 +000054}
55
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056namespace llvm {
aslc200b112008-08-16 12:57:46 +000057
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058//===----------------------------------------------------------------------===//
59
60/// Configuration values for initial hash set sizes (log2).
61///
Bill Wendling824a8bf2009-02-03 21:17:20 +000062static const unsigned InitDiesSetSize = 9; // log2(512)
63static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
64static const unsigned InitValuesSetSize = 9; // log2(512)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065
66//===----------------------------------------------------------------------===//
67/// Forward declarations.
68///
69class DIE;
70class DIEValue;
71
72//===----------------------------------------------------------------------===//
Devang Patelb3907da2009-01-05 23:03:32 +000073/// Utility routines.
74///
Devang Patel2da0cc42009-01-15 23:41:32 +000075/// getGlobalVariable - Return either a direct or cast Global value.
76///
77static GlobalVariable *getGlobalVariable(Value *V) {
78 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
79 return GV;
80 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
81 if (CE->getOpcode() == Instruction::BitCast) {
82 return dyn_cast<GlobalVariable>(CE->getOperand(0));
83 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
84 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
85 if (!CE->getOperand(i)->isNullValue())
86 return NULL;
87 }
88 return dyn_cast<GlobalVariable>(CE->getOperand(0));
89 }
90 }
91 return NULL;
92}
93
Devang Patelb3907da2009-01-05 23:03:32 +000094//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095/// DWLabel - Labels are used to track locations in the assembler file.
aslc200b112008-08-16 12:57:46 +000096/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
97/// where the tag is a category of label (Ex. location) and number is a value
Reid Spencer37c7cea2007-08-05 20:06:04 +000098/// unique in that category.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099class DWLabel {
100public:
101 /// Tag - Label category tag. Should always be a staticly declared C string.
102 ///
103 const char *Tag;
aslc200b112008-08-16 12:57:46 +0000104
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 /// Number - Value to make label unique.
106 ///
107 unsigned Number;
108
109 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
aslc200b112008-08-16 12:57:46 +0000110
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 void Profile(FoldingSetNodeID &ID) const {
Evan Cheng3e288912009-02-25 07:04:34 +0000112 ID.AddString(Tag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 ID.AddInteger(Number);
114 }
aslc200b112008-08-16 12:57:46 +0000115
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116#ifndef NDEBUG
117 void print(std::ostream *O) const {
118 if (O) print(*O);
119 }
120 void print(std::ostream &O) const {
121 O << "." << Tag;
122 if (Number) O << Number;
123 }
124#endif
125};
126
127//===----------------------------------------------------------------------===//
128/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
129/// Dwarf abbreviation.
130class DIEAbbrevData {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 /// Attribute - Dwarf attribute code.
132 ///
133 unsigned Attribute;
aslc200b112008-08-16 12:57:46 +0000134
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 /// Form - Dwarf form code.
aslc200b112008-08-16 12:57:46 +0000136 ///
137 unsigned Form;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138public:
Bill Wendling15afa002009-03-10 23:57:09 +0000139 DIEAbbrevData(unsigned A, unsigned F) : Attribute(A), Form(F) {}
aslc200b112008-08-16 12:57:46 +0000140
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 // Accessors.
142 unsigned getAttribute() const { return Attribute; }
143 unsigned getForm() const { return Form; }
144
145 /// Profile - Used to gather unique data for the abbreviation folding set.
146 ///
147 void Profile(FoldingSetNodeID &ID)const {
148 ID.AddInteger(Attribute);
149 ID.AddInteger(Form);
150 }
151};
152
153//===----------------------------------------------------------------------===//
154/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
155/// information object.
156class DIEAbbrev : public FoldingSetNode {
157private:
158 /// Tag - Dwarf tag code.
159 ///
160 unsigned Tag;
aslc200b112008-08-16 12:57:46 +0000161
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 /// Unique number for node.
163 ///
164 unsigned Number;
165
166 /// ChildrenFlag - Dwarf children flag.
167 ///
168 unsigned ChildrenFlag;
169
170 /// Data - Raw data bytes for abbreviation.
171 ///
Owen Anderson88dd6232008-06-24 21:44:59 +0000172 SmallVector<DIEAbbrevData, 8> Data;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173public:
Bill Wendling15afa002009-03-10 23:57:09 +0000174 DIEAbbrev(unsigned T, unsigned C) : Tag(T), ChildrenFlag(C), Data() {}
175 virtual ~DIEAbbrev() {}
aslc200b112008-08-16 12:57:46 +0000176
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 // Accessors.
178 unsigned getTag() const { return Tag; }
179 unsigned getNumber() const { return Number; }
180 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson88dd6232008-06-24 21:44:59 +0000181 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 void setTag(unsigned T) { Tag = T; }
183 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
184 void setNumber(unsigned N) { Number = N; }
aslc200b112008-08-16 12:57:46 +0000185
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 /// AddAttribute - Adds another set of attribute information to the
187 /// abbreviation.
188 void AddAttribute(unsigned Attribute, unsigned Form) {
189 Data.push_back(DIEAbbrevData(Attribute, Form));
190 }
aslc200b112008-08-16 12:57:46 +0000191
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 /// AddFirstAttribute - Adds a set of attribute information to the front
193 /// of the abbreviation.
194 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
195 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
196 }
aslc200b112008-08-16 12:57:46 +0000197
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 /// Profile - Used to gather unique data for the abbreviation folding set.
199 ///
200 void Profile(FoldingSetNodeID &ID) {
201 ID.AddInteger(Tag);
202 ID.AddInteger(ChildrenFlag);
aslc200b112008-08-16 12:57:46 +0000203
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 // For each attribute description.
205 for (unsigned i = 0, N = Data.size(); i < N; ++i)
206 Data[i].Profile(ID);
207 }
aslc200b112008-08-16 12:57:46 +0000208
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 /// Emit - Print the abbreviation using the specified Dwarf writer.
210 ///
aslc200b112008-08-16 12:57:46 +0000211 void Emit(const DwarfDebug &DD) const;
212
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213#ifndef NDEBUG
214 void print(std::ostream *O) {
215 if (O) print(*O);
216 }
217 void print(std::ostream &O);
218 void dump();
219#endif
220};
221
222//===----------------------------------------------------------------------===//
223/// DIE - A structured debug information entry. Has an abbreviation which
224/// describes it's organization.
225class DIE : public FoldingSetNode {
226protected:
227 /// Abbrev - Buffer for constructing abbreviation.
228 ///
229 DIEAbbrev Abbrev;
aslc200b112008-08-16 12:57:46 +0000230
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 /// Offset - Offset in debug info section.
232 ///
233 unsigned Offset;
aslc200b112008-08-16 12:57:46 +0000234
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 /// Size - Size of instance + children.
236 ///
237 unsigned Size;
aslc200b112008-08-16 12:57:46 +0000238
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 /// Children DIEs.
240 ///
241 std::vector<DIE *> Children;
aslc200b112008-08-16 12:57:46 +0000242
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 /// Attributes values.
244 ///
Owen Anderson88dd6232008-06-24 21:44:59 +0000245 SmallVector<DIEValue*, 32> Values;
aslc200b112008-08-16 12:57:46 +0000246
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000248 explicit DIE(unsigned Tag)
Bill Wendling15afa002009-03-10 23:57:09 +0000249 : Abbrev(Tag, DW_CHILDREN_no), Offset(0), Size(0), Children(), Values() {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 virtual ~DIE();
aslc200b112008-08-16 12:57:46 +0000251
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 // Accessors.
253 DIEAbbrev &getAbbrev() { return Abbrev; }
254 unsigned getAbbrevNumber() const {
255 return Abbrev.getNumber();
256 }
257 unsigned getTag() const { return Abbrev.getTag(); }
258 unsigned getOffset() const { return Offset; }
259 unsigned getSize() const { return Size; }
260 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson88dd6232008-06-24 21:44:59 +0000261 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
263 void setOffset(unsigned O) { Offset = O; }
264 void setSize(unsigned S) { Size = S; }
aslc200b112008-08-16 12:57:46 +0000265
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 /// AddValue - Add a value and attributes to a DIE.
267 ///
268 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
269 Abbrev.AddAttribute(Attribute, Form);
270 Values.push_back(Value);
271 }
aslc200b112008-08-16 12:57:46 +0000272
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 /// SiblingOffset - Return the offset of the debug information entry's
274 /// sibling.
275 unsigned SiblingOffset() const { return Offset + Size; }
aslc200b112008-08-16 12:57:46 +0000276
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
278 ///
279 void AddSiblingOffset();
280
281 /// AddChild - Add a child to the DIE.
282 ///
283 void AddChild(DIE *Child) {
284 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
285 Children.push_back(Child);
286 }
aslc200b112008-08-16 12:57:46 +0000287
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 /// Detach - Detaches objects connected to it after copying.
289 ///
290 void Detach() {
291 Children.clear();
292 }
aslc200b112008-08-16 12:57:46 +0000293
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 /// Profile - Used to gather unique data for the value folding set.
295 ///
296 void Profile(FoldingSetNodeID &ID) ;
aslc200b112008-08-16 12:57:46 +0000297
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298#ifndef NDEBUG
299 void print(std::ostream *O, unsigned IncIndent = 0) {
300 if (O) print(*O, IncIndent);
301 }
302 void print(std::ostream &O, unsigned IncIndent = 0);
303 void dump();
304#endif
305};
306
307//===----------------------------------------------------------------------===//
308/// DIEValue - A debug information entry value.
309///
310class DIEValue : public FoldingSetNode {
311public:
312 enum {
313 isInteger,
314 isString,
315 isLabel,
316 isAsIsLabel,
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000317 isSectionOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 isDelta,
319 isEntry,
320 isBlock
321 };
aslc200b112008-08-16 12:57:46 +0000322
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323 /// Type - Type of data stored in the value.
324 ///
325 unsigned Type;
aslc200b112008-08-16 12:57:46 +0000326
Bill Wendling15afa002009-03-10 23:57:09 +0000327 explicit DIEValue(unsigned T) : Type(T) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 virtual ~DIEValue() {}
aslc200b112008-08-16 12:57:46 +0000329
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330 // Accessors
331 unsigned getType() const { return Type; }
aslc200b112008-08-16 12:57:46 +0000332
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 // Implement isa/cast/dyncast.
334 static bool classof(const DIEValue *) { return true; }
aslc200b112008-08-16 12:57:46 +0000335
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336 /// EmitValue - Emit value via the Dwarf writer.
337 ///
338 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
aslc200b112008-08-16 12:57:46 +0000339
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 /// SizeOf - Return the size of a value in bytes.
341 ///
342 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
aslc200b112008-08-16 12:57:46 +0000343
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 /// Profile - Used to gather unique data for the value folding set.
345 ///
346 virtual void Profile(FoldingSetNodeID &ID) = 0;
aslc200b112008-08-16 12:57:46 +0000347
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348#ifndef NDEBUG
349 void print(std::ostream *O) {
350 if (O) print(*O);
351 }
352 virtual void print(std::ostream &O) = 0;
353 void dump();
354#endif
355};
356
357//===----------------------------------------------------------------------===//
358/// DWInteger - An integer value DIE.
aslc200b112008-08-16 12:57:46 +0000359///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360class DIEInteger : public DIEValue {
361private:
362 uint64_t Integer;
aslc200b112008-08-16 12:57:46 +0000363
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000365 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366
367 // Implement isa/cast/dyncast.
368 static bool classof(const DIEInteger *) { return true; }
369 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
aslc200b112008-08-16 12:57:46 +0000370
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371 /// BestForm - Choose the best form for integer.
372 ///
373 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
374 if (IsSigned) {
375 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
376 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
377 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
378 } else {
379 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
380 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
381 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
382 }
383 return DW_FORM_data8;
384 }
aslc200b112008-08-16 12:57:46 +0000385
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386 /// EmitValue - Emit integer of appropriate size.
387 ///
388 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000389
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 /// SizeOf - Determine size of integer value in bytes.
391 ///
392 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000393
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 /// Profile - Used to gather unique data for the value folding set.
395 ///
396 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
397 ID.AddInteger(isInteger);
398 ID.AddInteger(Integer);
399 }
400 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
aslc200b112008-08-16 12:57:46 +0000401
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402#ifndef NDEBUG
403 virtual void print(std::ostream &O) {
404 O << "Int: " << (int64_t)Integer
405 << " 0x" << std::hex << Integer << std::dec;
406 }
407#endif
408};
409
410//===----------------------------------------------------------------------===//
411/// DIEString - A string value DIE.
aslc200b112008-08-16 12:57:46 +0000412///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413class DIEString : public DIEValue {
Bill Wendling15afa002009-03-10 23:57:09 +0000414 const std::string Str;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415public:
Bill Wendling15afa002009-03-10 23:57:09 +0000416 explicit DIEString(const std::string &S) : DIEValue(isString), Str(S) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417
418 // Implement isa/cast/dyncast.
419 static bool classof(const DIEString *) { return true; }
420 static bool classof(const DIEValue *S) { return S->Type == isString; }
aslc200b112008-08-16 12:57:46 +0000421
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 /// EmitValue - Emit string value.
423 ///
424 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000425
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426 /// SizeOf - Determine size of string value in bytes.
427 ///
428 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Bill Wendling15afa002009-03-10 23:57:09 +0000429 return Str.size() + sizeof(char); // sizeof('\0');
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430 }
aslc200b112008-08-16 12:57:46 +0000431
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000432 /// Profile - Used to gather unique data for the value folding set.
433 ///
Bill Wendling15afa002009-03-10 23:57:09 +0000434 static void Profile(FoldingSetNodeID &ID, const std::string &Str) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000435 ID.AddInteger(isString);
Bill Wendling15afa002009-03-10 23:57:09 +0000436 ID.AddString(Str);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437 }
Bill Wendling15afa002009-03-10 23:57:09 +0000438 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Str); }
aslc200b112008-08-16 12:57:46 +0000439
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440#ifndef NDEBUG
441 virtual void print(std::ostream &O) {
Bill Wendling15afa002009-03-10 23:57:09 +0000442 O << "Str: \"" << Str << "\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443 }
444#endif
445};
446
447//===----------------------------------------------------------------------===//
448/// DIEDwarfLabel - A Dwarf internal label expression DIE.
449//
450class DIEDwarfLabel : public DIEValue {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451 const DWLabel Label;
Bill Wendling15afa002009-03-10 23:57:09 +0000452public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000453 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454
455 // Implement isa/cast/dyncast.
456 static bool classof(const DIEDwarfLabel *) { return true; }
457 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
aslc200b112008-08-16 12:57:46 +0000458
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 /// EmitValue - Emit label value.
460 ///
461 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000462
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463 /// SizeOf - Determine size of label value in bytes.
464 ///
465 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000466
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 /// Profile - Used to gather unique data for the value folding set.
468 ///
469 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
470 ID.AddInteger(isLabel);
471 Label.Profile(ID);
472 }
473 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
aslc200b112008-08-16 12:57:46 +0000474
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000475#ifndef NDEBUG
476 virtual void print(std::ostream &O) {
477 O << "Lbl: ";
478 Label.print(O);
479 }
480#endif
481};
482
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483//===----------------------------------------------------------------------===//
484/// DIEObjectLabel - A label to an object in code or data.
485//
486class DIEObjectLabel : public DIEValue {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000487 const std::string Label;
Bill Wendling15afa002009-03-10 23:57:09 +0000488public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000489 explicit DIEObjectLabel(const std::string &L)
490 : DIEValue(isAsIsLabel), Label(L) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000491
492 // Implement isa/cast/dyncast.
493 static bool classof(const DIEObjectLabel *) { return true; }
494 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
aslc200b112008-08-16 12:57:46 +0000495
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000496 /// EmitValue - Emit label value.
497 ///
498 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000499
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 /// SizeOf - Determine size of label value in bytes.
501 ///
502 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000503
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000504 /// Profile - Used to gather unique data for the value folding set.
505 ///
506 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
507 ID.AddInteger(isAsIsLabel);
508 ID.AddString(Label);
509 }
Evan Cheng3e288912009-02-25 07:04:34 +0000510 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label.c_str()); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000511
512#ifndef NDEBUG
513 virtual void print(std::ostream &O) {
514 O << "Obj: " << Label;
515 }
516#endif
517};
518
519//===----------------------------------------------------------------------===//
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000520/// DIESectionOffset - A section offset DIE.
521//
522class DIESectionOffset : public DIEValue {
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000523 const DWLabel Label;
524 const DWLabel Section;
525 bool IsEH : 1;
526 bool UseSet : 1;
Bill Wendling15afa002009-03-10 23:57:09 +0000527public:
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000528 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
529 bool isEH = false, bool useSet = true)
Bill Wendling15afa002009-03-10 23:57:09 +0000530 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
531 IsEH(isEH), UseSet(useSet) {}
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000532
533 // Implement isa/cast/dyncast.
534 static bool classof(const DIESectionOffset *) { return true; }
535 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
aslc200b112008-08-16 12:57:46 +0000536
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000537 /// EmitValue - Emit section offset.
538 ///
539 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000540
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000541 /// SizeOf - Determine size of section offset value in bytes.
542 ///
543 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000544
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000545 /// Profile - Used to gather unique data for the value folding set.
546 ///
547 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
548 const DWLabel &Section) {
549 ID.AddInteger(isSectionOffset);
550 Label.Profile(ID);
551 Section.Profile(ID);
552 // IsEH and UseSet are specific to the Label/Section that we will emit
553 // the offset for; so Label/Section are enough for uniqueness.
554 }
555 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
556
557#ifndef NDEBUG
558 virtual void print(std::ostream &O) {
559 O << "Off: ";
560 Label.print(O);
561 O << "-";
562 Section.print(O);
563 O << "-" << IsEH << "-" << UseSet;
564 }
565#endif
566};
567
568//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000569/// DIEDelta - A simple label difference DIE.
aslc200b112008-08-16 12:57:46 +0000570///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000571class DIEDelta : public DIEValue {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000572 const DWLabel LabelHi;
573 const DWLabel LabelLo;
Bill Wendling15afa002009-03-10 23:57:09 +0000574public:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000575 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
Bill Wendling15afa002009-03-10 23:57:09 +0000576 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000577
578 // Implement isa/cast/dyncast.
579 static bool classof(const DIEDelta *) { return true; }
580 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
aslc200b112008-08-16 12:57:46 +0000581
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000582 /// EmitValue - Emit delta value.
583 ///
584 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000585
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586 /// SizeOf - Determine size of delta value in bytes.
587 ///
588 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000589
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 /// Profile - Used to gather unique data for the value folding set.
591 ///
592 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
593 const DWLabel &LabelLo) {
594 ID.AddInteger(isDelta);
595 LabelHi.Profile(ID);
596 LabelLo.Profile(ID);
597 }
598 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
599
600#ifndef NDEBUG
601 virtual void print(std::ostream &O) {
602 O << "Del: ";
603 LabelHi.print(O);
604 O << "-";
605 LabelLo.print(O);
606 }
607#endif
608};
609
610//===----------------------------------------------------------------------===//
611/// DIEntry - A pointer to another debug information entry. An instance of this
612/// class can also be used as a proxy for a debug information entry not yet
613/// defined (ie. types.)
614class DIEntry : public DIEValue {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000615 DIE *Entry;
Bill Wendling15afa002009-03-10 23:57:09 +0000616public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000617 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
aslc200b112008-08-16 12:57:46 +0000618
Bill Wendling15afa002009-03-10 23:57:09 +0000619 void setEntry(DIE *E) { Entry = E; }
620
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000621 // Implement isa/cast/dyncast.
622 static bool classof(const DIEntry *) { return true; }
623 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
aslc200b112008-08-16 12:57:46 +0000624
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000625 /// EmitValue - Emit debug information entry offset.
626 ///
627 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000628
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629 /// SizeOf - Determine size of debug information entry in bytes.
630 ///
631 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
632 return sizeof(int32_t);
633 }
aslc200b112008-08-16 12:57:46 +0000634
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635 /// Profile - Used to gather unique data for the value folding set.
636 ///
637 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
638 ID.AddInteger(isEntry);
639 ID.AddPointer(Entry);
640 }
641 virtual void Profile(FoldingSetNodeID &ID) {
642 ID.AddInteger(isEntry);
aslc200b112008-08-16 12:57:46 +0000643
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000644 if (Entry) {
645 ID.AddPointer(Entry);
646 } else {
647 ID.AddPointer(this);
648 }
649 }
aslc200b112008-08-16 12:57:46 +0000650
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651#ifndef NDEBUG
652 virtual void print(std::ostream &O) {
653 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
654 }
655#endif
656};
657
658//===----------------------------------------------------------------------===//
659/// DIEBlock - A block of values. Primarily used for location expressions.
660//
661class DIEBlock : public DIEValue, public DIE {
Bill Wendling15afa002009-03-10 23:57:09 +0000662 unsigned Size; // Size in bytes excluding size header.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000663public:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664 DIEBlock()
Bill Wendling15afa002009-03-10 23:57:09 +0000665 : DIEValue(isBlock), DIE(0), Size(0) {}
666 virtual ~DIEBlock() {}
aslc200b112008-08-16 12:57:46 +0000667
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000668 // Implement isa/cast/dyncast.
669 static bool classof(const DIEBlock *) { return true; }
670 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
aslc200b112008-08-16 12:57:46 +0000671
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 /// ComputeSize - calculate the size of the block.
673 ///
674 unsigned ComputeSize(DwarfDebug &DD);
aslc200b112008-08-16 12:57:46 +0000675
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000676 /// BestForm - Choose the best form for data.
677 ///
678 unsigned BestForm() const {
679 if ((unsigned char)Size == Size) return DW_FORM_block1;
680 if ((unsigned short)Size == Size) return DW_FORM_block2;
681 if ((unsigned int)Size == Size) return DW_FORM_block4;
682 return DW_FORM_block;
683 }
684
685 /// EmitValue - Emit block data.
686 ///
687 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000688
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689 /// SizeOf - Determine size of block data in bytes.
690 ///
691 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000692
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693 /// Profile - Used to gather unique data for the value folding set.
694 ///
695 virtual void Profile(FoldingSetNodeID &ID) {
696 ID.AddInteger(isBlock);
697 DIE::Profile(ID);
698 }
aslc200b112008-08-16 12:57:46 +0000699
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700#ifndef NDEBUG
701 virtual void print(std::ostream &O) {
702 O << "Blk: ";
703 DIE::print(O, 5);
704 }
705#endif
706};
707
708//===----------------------------------------------------------------------===//
709/// CompileUnit - This dwarf writer support class manages information associate
710/// with a source file.
711class CompileUnit {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712 /// ID - File identifier for source.
713 ///
714 unsigned ID;
aslc200b112008-08-16 12:57:46 +0000715
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000716 /// Die - Compile unit debug information entry.
717 ///
718 DIE *Die;
aslc200b112008-08-16 12:57:46 +0000719
Devang Patel42f6bed2009-01-13 23:54:55 +0000720 /// GVToDieMap - Tracks the mapping of unit level debug informaton
721 /// variables to debug information entries.
Devang Patel56b1d132009-01-20 00:58:55 +0000722 std::map<GlobalVariable *, DIE *> GVToDieMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723
Devang Patel42f6bed2009-01-13 23:54:55 +0000724 /// GVToDIEntryMap - Tracks the mapping of unit level debug informaton
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000725 /// descriptors to debug information entries using a DIEntry proxy.
Devang Patel56b1d132009-01-20 00:58:55 +0000726 std::map<GlobalVariable *, DIEntry *> GVToDIEntryMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000727
728 /// Globals - A map of globally visible named entities for this unit.
729 ///
Bill Wendlinge06da442009-04-09 21:49:15 +0000730 StringMap<DIE*> Globals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000731
732 /// DiesSet - Used to uniquely define dies within the compile unit.
733 ///
734 FoldingSet<DIE> DiesSet;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000735public:
Devang Patelb3907da2009-01-05 23:03:32 +0000736 CompileUnit(unsigned I, DIE *D)
Devang Patel42f6bed2009-01-13 23:54:55 +0000737 : ID(I), Die(D), GVToDieMap(),
Devang Patel5302e672009-01-17 06:51:37 +0000738 GVToDIEntryMap(), Globals(), DiesSet(InitDiesSetSize)
Devang Patelb3907da2009-01-05 23:03:32 +0000739 {}
740
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000741 ~CompileUnit() {
742 delete Die;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000743 }
aslc200b112008-08-16 12:57:46 +0000744
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000745 // Accessors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000746 unsigned getID() const { return ID; }
747 DIE* getDie() const { return Die; }
Bill Wendlinge06da442009-04-09 21:49:15 +0000748 StringMap<DIE*> &getGlobals() { return Globals; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000749
750 /// hasContent - Return true if this compile unit has something to write out.
751 ///
752 bool hasContent() const {
753 return !Die->getChildren().empty();
754 }
755
756 /// AddGlobal - Add a new global entity to the compile unit.
757 ///
758 void AddGlobal(const std::string &Name, DIE *Die) {
759 Globals[Name] = Die;
760 }
aslc200b112008-08-16 12:57:46 +0000761
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000762 /// getDieMapSlotFor - Returns the debug information entry map slot for the
Devang Patel42f6bed2009-01-13 23:54:55 +0000763 /// specified debug variable.
Devang Patel4a4cbe72009-01-05 21:47:57 +0000764 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
765 return GVToDieMap[GV];
766 }
aslc200b112008-08-16 12:57:46 +0000767
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000768 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
Devang Patel42f6bed2009-01-13 23:54:55 +0000769 /// specified debug variable.
Devang Patel4a4cbe72009-01-05 21:47:57 +0000770 DIEntry *&getDIEntrySlotFor(GlobalVariable *GV) {
771 return GVToDIEntryMap[GV];
772 }
aslc200b112008-08-16 12:57:46 +0000773
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000774 /// AddDie - Adds or interns the DIE to the compile unit.
775 ///
776 DIE *AddDie(DIE &Buffer) {
777 FoldingSetNodeID ID;
778 Buffer.Profile(ID);
779 void *Where;
780 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
aslc200b112008-08-16 12:57:46 +0000781
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000782 if (!Die) {
783 Die = new DIE(Buffer);
784 DiesSet.InsertNode(Die, Where);
785 this->Die->AddChild(Die);
786 Buffer.Detach();
787 }
aslc200b112008-08-16 12:57:46 +0000788
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000789 return Die;
790 }
791};
792
793//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +0000794/// Dwarf - Emits general Dwarf directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000795///
796class Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000797protected:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000798 //===--------------------------------------------------------------------===//
799 // Core attributes used by the Dwarf writer.
800 //
aslc200b112008-08-16 12:57:46 +0000801
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000802 //
803 /// O - Stream to .s file.
804 ///
Owen Anderson847b99b2008-08-21 00:14:44 +0000805 raw_ostream &O;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806
807 /// Asm - Target of Dwarf emission.
808 ///
809 AsmPrinter *Asm;
aslc200b112008-08-16 12:57:46 +0000810
Bill Wendlingac9639d2008-07-01 23:34:48 +0000811 /// TAI - Target asm information.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000812 const TargetAsmInfo *TAI;
aslc200b112008-08-16 12:57:46 +0000813
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000814 /// TD - Target data.
815 const TargetData *TD;
aslc200b112008-08-16 12:57:46 +0000816
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000817 /// RI - Register Information.
Dan Gohman1e57df32008-02-10 18:45:23 +0000818 const TargetRegisterInfo *RI;
aslc200b112008-08-16 12:57:46 +0000819
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000820 /// M - Current module.
821 ///
822 Module *M;
aslc200b112008-08-16 12:57:46 +0000823
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000824 /// MF - Current machine function.
825 ///
826 MachineFunction *MF;
aslc200b112008-08-16 12:57:46 +0000827
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000828 /// MMI - Collected machine module information.
829 ///
830 MachineModuleInfo *MMI;
aslc200b112008-08-16 12:57:46 +0000831
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000832 /// SubprogramCount - The running count of functions being compiled.
833 ///
834 unsigned SubprogramCount;
aslc200b112008-08-16 12:57:46 +0000835
Chris Lattnerb3876c72007-09-24 03:35:37 +0000836 /// Flavor - A unique string indicating what dwarf producer this is, used to
837 /// unique labels.
838 const char * const Flavor;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000839
840 unsigned SetCounter;
Owen Anderson847b99b2008-08-21 00:14:44 +0000841 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattnerb3876c72007-09-24 03:35:37 +0000842 const char *flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000843 : O(OS)
844 , Asm(A)
845 , TAI(T)
846 , TD(Asm->TM.getTargetData())
847 , RI(Asm->TM.getRegisterInfo())
848 , M(NULL)
849 , MF(NULL)
850 , MMI(NULL)
851 , SubprogramCount(0)
Chris Lattnerb3876c72007-09-24 03:35:37 +0000852 , Flavor(flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000853 , SetCounter(1)
854 {
855 }
856
857public:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000858 //===--------------------------------------------------------------------===//
859 // Accessors.
860 //
Bill Wendling4226f4f2009-04-10 00:00:25 +0000861 const AsmPrinter *getAsm() const { return Asm; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000862 MachineModuleInfo *getMMI() const { return MMI; }
863 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohmancfb72b22007-09-27 23:12:31 +0000864 const TargetData *getTargetData() const { return TD; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000865
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000866 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
867 const {
868 if (isInSection && TAI->getDwarfSectionOffsetDirective())
869 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohmancfb72b22007-09-27 23:12:31 +0000870 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000871 O << TAI->getData32bitsDirective();
872 else
873 O << TAI->getData64bitsDirective();
874 }
aslc200b112008-08-16 12:57:46 +0000875
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000876 /// PrintLabelName - Print label name in form used by Dwarf writer.
877 ///
878 void PrintLabelName(DWLabel Label) const {
879 PrintLabelName(Label.Tag, Label.Number);
880 }
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000881 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000882 O << TAI->getPrivateGlobalPrefix() << Tag;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000883 if (Number) O << Number;
884 }
aslc200b112008-08-16 12:57:46 +0000885
Chris Lattnerb3876c72007-09-24 03:35:37 +0000886 void PrintLabelName(const char *Tag, unsigned Number,
887 const char *Suffix) const {
888 O << TAI->getPrivateGlobalPrefix() << Tag;
889 if (Number) O << Number;
890 O << Suffix;
891 }
aslc200b112008-08-16 12:57:46 +0000892
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000893 /// EmitLabel - Emit location label for internal use by Dwarf.
894 ///
895 void EmitLabel(DWLabel Label) const {
896 EmitLabel(Label.Tag, Label.Number);
897 }
898 void EmitLabel(const char *Tag, unsigned Number) const {
899 PrintLabelName(Tag, Number);
900 O << ":\n";
901 }
aslc200b112008-08-16 12:57:46 +0000902
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000903 /// EmitReference - Emit a reference to a label.
904 ///
Dan Gohman4fd77742007-09-28 15:43:33 +0000905 void EmitReference(DWLabel Label, bool IsPCRelative = false,
906 bool Force32Bit = false) const {
907 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000908 }
909 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman4fd77742007-09-28 15:43:33 +0000910 bool IsPCRelative = false, bool Force32Bit = false) const {
911 PrintRelDirective(Force32Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000912 PrintLabelName(Tag, Number);
aslc200b112008-08-16 12:57:46 +0000913
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000914 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
915 }
Dan Gohman4fd77742007-09-28 15:43:33 +0000916 void EmitReference(const std::string &Name, bool IsPCRelative = false,
917 bool Force32Bit = false) const {
918 PrintRelDirective(Force32Bit);
aslc200b112008-08-16 12:57:46 +0000919
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000920 O << Name;
aslc200b112008-08-16 12:57:46 +0000921
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
923 }
924
925 /// EmitDifference - Emit the difference between two labels. Some
926 /// assemblers do not behave with absolute expressions with data directives,
927 /// so there is an option (needsSet) to use an intermediary set expression.
928 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
929 bool IsSmall = false) {
930 EmitDifference(LabelHi.Tag, LabelHi.Number,
931 LabelLo.Tag, LabelLo.Number,
932 IsSmall);
933 }
934 void EmitDifference(const char *TagHi, unsigned NumberHi,
935 const char *TagLo, unsigned NumberLo,
936 bool IsSmall = false) {
937 if (TAI->needsSet()) {
938 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +0000939 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000940 O << ",";
941 PrintLabelName(TagHi, NumberHi);
942 O << "-";
943 PrintLabelName(TagLo, NumberLo);
944 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000945
946 PrintRelDirective(IsSmall);
Chris Lattnerb3876c72007-09-24 03:35:37 +0000947 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000948 ++SetCounter;
949 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000950 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +0000951
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000952 PrintLabelName(TagHi, NumberHi);
953 O << "-";
954 PrintLabelName(TagLo, NumberLo);
955 }
956 }
957
958 void EmitSectionOffset(const char* Label, const char* Section,
959 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesen0ebb2432008-03-26 23:31:39 +0000960 bool IsSmall = false, bool isEH = false,
961 bool useSet = true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000962 bool printAbsolute = false;
Dale Johannesen0ebb2432008-03-26 23:31:39 +0000963 if (isEH)
964 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
965 else
966 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
967
968 if (TAI->needsSet() && useSet) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000969 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +0000970 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000971 O << ",";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000972 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000973
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000974 if (!printAbsolute) {
975 O << "-";
976 PrintLabelName(Section, SectionNumber);
aslc200b112008-08-16 12:57:46 +0000977 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000978 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000979
980 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +0000981
Chris Lattnerb3876c72007-09-24 03:35:37 +0000982 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000983 ++SetCounter;
984 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000985 PrintRelDirective(IsSmall, true);
aslc200b112008-08-16 12:57:46 +0000986
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000987 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000988
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000989 if (!printAbsolute) {
990 O << "-";
991 PrintLabelName(Section, SectionNumber);
992 }
aslc200b112008-08-16 12:57:46 +0000993 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000994 }
aslc200b112008-08-16 12:57:46 +0000995
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000996 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
997 /// frame.
998 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenf5a11532007-11-13 19:13:01 +0000999 const std::vector<MachineMove> &Moves, bool isEH) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001000 int stackGrowth =
1001 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1002 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00001003 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001004 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
1005
1006 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
1007 const MachineMove &Move = Moves[i];
1008 unsigned LabelID = Move.getLabelID();
aslc200b112008-08-16 12:57:46 +00001009
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001010 if (LabelID) {
1011 LabelID = MMI->MappedLabel(LabelID);
aslc200b112008-08-16 12:57:46 +00001012
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001013 // Throw out move if the label is invalid.
1014 if (!LabelID) continue;
1015 }
aslc200b112008-08-16 12:57:46 +00001016
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001017 const MachineLocation &Dst = Move.getDestination();
1018 const MachineLocation &Src = Move.getSource();
aslc200b112008-08-16 12:57:46 +00001019
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001020 // Advance row if new location.
1021 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1022 Asm->EmitInt8(DW_CFA_advance_loc4);
1023 Asm->EOL("DW_CFA_advance_loc4");
1024 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1025 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00001026
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001027 BaseLabelID = LabelID;
1028 BaseLabel = "label";
1029 IsLocal = true;
1030 }
aslc200b112008-08-16 12:57:46 +00001031
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001032 // If advancing cfa.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001033 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1034 if (!Src.isReg()) {
1035 if (Src.getReg() == MachineLocation::VirtualFP) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001036 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1037 Asm->EOL("DW_CFA_def_cfa_offset");
1038 } else {
1039 Asm->EmitInt8(DW_CFA_def_cfa);
1040 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001041 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001042 Asm->EOL("Register");
1043 }
aslc200b112008-08-16 12:57:46 +00001044
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001045 int Offset = -Src.getOffset();
aslc200b112008-08-16 12:57:46 +00001046
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001047 Asm->EmitULEB128Bytes(Offset);
1048 Asm->EOL("Offset");
1049 } else {
1050 assert(0 && "Machine move no supported yet.");
1051 }
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001052 } else if (Src.isReg() &&
1053 Src.getReg() == MachineLocation::VirtualFP) {
1054 if (Dst.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001055 Asm->EmitInt8(DW_CFA_def_cfa_register);
1056 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001057 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001058 Asm->EOL("Register");
1059 } else {
1060 assert(0 && "Machine move no supported yet.");
1061 }
1062 } else {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001063 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001064 int Offset = Dst.getOffset() / stackGrowth;
aslc200b112008-08-16 12:57:46 +00001065
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001066 if (Offset < 0) {
1067 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1068 Asm->EOL("DW_CFA_offset_extended_sf");
1069 Asm->EmitULEB128Bytes(Reg);
1070 Asm->EOL("Reg");
1071 Asm->EmitSLEB128Bytes(Offset);
1072 Asm->EOL("Offset");
1073 } else if (Reg < 64) {
1074 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Cheng42ceb472009-03-25 01:47:28 +00001075 if (Asm->isVerbose())
Evan Cheng6181e062008-07-09 21:53:02 +00001076 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1077 else
1078 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001079 Asm->EmitULEB128Bytes(Offset);
1080 Asm->EOL("Offset");
1081 } else {
1082 Asm->EmitInt8(DW_CFA_offset_extended);
1083 Asm->EOL("DW_CFA_offset_extended");
1084 Asm->EmitULEB128Bytes(Reg);
1085 Asm->EOL("Reg");
1086 Asm->EmitULEB128Bytes(Offset);
1087 Asm->EOL("Offset");
1088 }
1089 }
1090 }
1091 }
1092
1093};
1094
1095//===----------------------------------------------------------------------===//
Devang Patel35a078f2009-01-12 22:54:42 +00001096/// SrcLineInfo - This class is used to record source line correspondence.
Devang Patel7dd15a92009-01-08 17:19:22 +00001097///
1098class SrcLineInfo {
1099 unsigned Line; // Source line number.
1100 unsigned Column; // Source column.
1101 unsigned SourceID; // Source ID number.
1102 unsigned LabelID; // Label in code ID number.
1103public:
1104 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
Bill Wendling824a8bf2009-02-03 21:17:20 +00001105 : Line(L), Column(C), SourceID(S), LabelID(I) {}
Devang Patel7dd15a92009-01-08 17:19:22 +00001106
1107 // Accessors
1108 unsigned getLine() const { return Line; }
1109 unsigned getColumn() const { return Column; }
1110 unsigned getSourceID() const { return SourceID; }
1111 unsigned getLabelID() const { return LabelID; }
1112};
1113
Devang Patel7dd15a92009-01-08 17:19:22 +00001114//===----------------------------------------------------------------------===//
Devang Patel4d1709e2009-01-08 02:33:41 +00001115/// DbgVariable - This class is used to track local variable information.
1116///
1117class DbgVariable {
Devang Patel7c8a2772009-01-16 19:28:14 +00001118 DIVariable Var; // Variable Descriptor.
Devang Patel4d1709e2009-01-08 02:33:41 +00001119 unsigned FrameIndex; // Variable frame index.
Devang Patel4d1709e2009-01-08 02:33:41 +00001120public:
Devang Patel7c8a2772009-01-16 19:28:14 +00001121 DbgVariable(DIVariable V, unsigned I) : Var(V), FrameIndex(I) {}
Devang Patel4d1709e2009-01-08 02:33:41 +00001122
1123 // Accessors.
Devang Patel7c8a2772009-01-16 19:28:14 +00001124 DIVariable getVariable() const { return Var; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001125 unsigned getFrameIndex() const { return FrameIndex; }
1126};
1127
1128//===----------------------------------------------------------------------===//
1129/// DbgScope - This class is used to track scope information.
1130///
1131class DbgScope {
Devang Patel4d1709e2009-01-08 02:33:41 +00001132 DbgScope *Parent; // Parent to this scope.
Devang Patel49a3bd92009-01-16 18:01:58 +00001133 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel4d1709e2009-01-08 02:33:41 +00001134 // Either subprogram or block.
1135 unsigned StartLabelID; // Label ID of the beginning of scope.
1136 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel49a3bd92009-01-16 18:01:58 +00001137 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
Devang Patel63c22f42009-01-10 02:42:49 +00001138 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Devang Patel4d1709e2009-01-08 02:33:41 +00001139public:
Devang Patel2560d922009-01-15 18:25:17 +00001140 DbgScope(DbgScope *P, DIDescriptor D)
Devang Patel4d1709e2009-01-08 02:33:41 +00001141 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), Scopes(), Variables()
1142 {}
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001143 virtual ~DbgScope() {
Devang Patela4162952009-01-12 18:48:36 +00001144 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1145 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1146 }
Devang Patel4d1709e2009-01-08 02:33:41 +00001147
1148 // Accessors.
Devang Patel49a3bd92009-01-16 18:01:58 +00001149 DbgScope *getParent() const { return Parent; }
1150 DIDescriptor getDesc() const { return Desc; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001151 unsigned getStartLabelID() const { return StartLabelID; }
1152 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel63c22f42009-01-10 02:42:49 +00001153 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1154 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001155 void setStartLabelID(unsigned S) { StartLabelID = S; }
1156 void setEndLabelID(unsigned E) { EndLabelID = E; }
1157
1158 /// AddScope - Add a scope to the scope.
1159 ///
1160 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1161
1162 /// AddVariable - Add a variable to the scope.
1163 ///
1164 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001165
1166 virtual bool isInlinedSubroutine() { return false; }
1167 virtual unsigned getLine() { assert ( 0 && "Unexpected scope!"); }
1168 virtual unsigned getColumn() { assert ( 0 && "Unexpected scope!"); }
1169 virtual unsigned getFile() { assert ( 0 && "Unexpected scope!"); }
1170};
1171
1172
1173//===----------------------------------------------------------------------===//
1174/// DbgInlinedSubroutineScope - This class is used to track inlined subroutine
1175/// scope information.
1176///
1177class DbgInlinedSubroutineScope : public DbgScope {
1178 unsigned Src;
1179 unsigned Line;
1180 unsigned Col;
1181public:
1182 DbgInlinedSubroutineScope(DbgScope *P, DIDescriptor D,
1183 unsigned S, unsigned L, unsigned C)
1184 : DbgScope(P, D), Src(S), Line(L), Col(C)
1185 {}
1186
1187 unsigned getLine() { return Line; }
1188 unsigned getColumn() { return Col; }
1189 unsigned getFile() { return Src; }
1190 bool isInlinedSubroutine() { return true; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001191};
1192
1193//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00001194/// DwarfDebug - Emits Dwarf debug directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001195///
1196class DwarfDebug : public Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001197 //===--------------------------------------------------------------------===//
1198 // Attributes used to construct specific Dwarf sections.
1199 //
aslc200b112008-08-16 12:57:46 +00001200
Evan Cheng3e288912009-02-25 07:04:34 +00001201 /// CompileUnitMap - A map of global variables representing compile units to
1202 /// compile units.
1203 DenseMap<Value *, CompileUnit *> CompileUnitMap;
1204
1205 /// CompileUnits - All the compile units in this module.
1206 ///
1207 SmallVector<CompileUnit *, 8> CompileUnits;
aslc200b112008-08-16 12:57:46 +00001208
Devang Patel2ae1db52009-01-30 18:20:31 +00001209 /// MainCU - Some platform prefers one compile unit per .o file. In such
1210 /// cases, all dies are inserted in MainCU.
1211 CompileUnit *MainCU;
Bill Wendlinge0f3a262009-02-20 20:40:28 +00001212
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001213 /// AbbreviationsSet - Used to uniquely define abbreviations.
1214 ///
1215 FoldingSet<DIEAbbrev> AbbreviationsSet;
1216
1217 /// Abbreviations - A list of all the unique abbreviations in use.
1218 ///
1219 std::vector<DIEAbbrev *> Abbreviations;
aslc200b112008-08-16 12:57:46 +00001220
Evan Cheng3e288912009-02-25 07:04:34 +00001221 /// DirectoryIdMap - Directory name to directory id map.
1222 ///
1223 StringMap<unsigned> DirectoryIdMap;
Devang Patel5f244e32009-01-05 22:35:52 +00001224
Evan Cheng3e288912009-02-25 07:04:34 +00001225 /// DirectoryNames - A list of directory names.
1226 SmallVector<std::string, 8> DirectoryNames;
1227
1228 /// SourceFileIdMap - Source file name to source file id map.
1229 ///
1230 StringMap<unsigned> SourceFileIdMap;
1231
1232 /// SourceFileNames - A list of source file names.
1233 SmallVector<std::string, 8> SourceFileNames;
1234
1235 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
1236 /// id mapped to a unique id.
1237 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
1238
1239 /// SourceIds - Reverse map from source id to directory id + file id pair.
1240 ///
1241 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
Devang Patel5f244e32009-01-05 22:35:52 +00001242
Devang Patel9b829452009-01-16 21:07:53 +00001243 /// Lines - List of of source line correspondence.
Devang Patel7dd15a92009-01-08 17:19:22 +00001244 std::vector<SrcLineInfo> Lines;
1245
Devang Patel9b829452009-01-16 21:07:53 +00001246 /// ValuesSet - Used to uniquely define values.
1247 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001248 FoldingSet<DIEValue> ValuesSet;
aslc200b112008-08-16 12:57:46 +00001249
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001250 /// Values - A list of all the unique values in use.
1251 ///
1252 std::vector<DIEValue *> Values;
aslc200b112008-08-16 12:57:46 +00001253
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001254 /// StringPool - A UniqueVector of strings used by indirect references.
1255 ///
1256 UniqueVector<std::string> StringPool;
1257
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001258 /// SectionMap - Provides a unique id per text section.
1259 ///
Anton Korobeynikov55b94962008-09-24 22:15:21 +00001260 UniqueVector<const Section*> SectionMap;
aslc200b112008-08-16 12:57:46 +00001261
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001262 /// SectionSourceLines - Tracks line numbers per text section.
1263 ///
Devang Patel35a078f2009-01-12 22:54:42 +00001264 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001265
1266 /// didInitial - Flag to indicate if initial emission has been done.
1267 ///
1268 bool didInitial;
aslc200b112008-08-16 12:57:46 +00001269
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001270 /// shouldEmit - Flag to indicate if debug information should be emitted.
1271 ///
1272 bool shouldEmit;
1273
Devang Patel2560d922009-01-15 18:25:17 +00001274 // RootDbgScope - Top level scope for the current function.
Devang Patel4d1709e2009-01-08 02:33:41 +00001275 //
1276 DbgScope *RootDbgScope;
1277
Bill Wendlingd9308a62009-03-10 21:23:25 +00001278 /// DbgScopeMap - Tracks the scopes in the current function.
Devang Patel4d1709e2009-01-08 02:33:41 +00001279 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
Bill Wendlingd9308a62009-03-10 21:23:25 +00001280
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001281 /// DbgInlinedScopeMap - Tracks inlined scopes in the current function.
1282 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> > DbgInlinedScopeMap;
1283
Devang Patel88bf96e2009-04-13 17:02:03 +00001284 /// InlineInfo - Keep track of inlined functions and their location.
1285 /// This information is used to populate debug_inlined section.
1286 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> > InlineInfo;
1287
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001288 /// InlinedVariableScopes - Scopes information for the inlined subroutine
1289 /// variables.
1290 DenseMap<const MachineInstr *, DbgScope *> InlinedVariableScopes;
1291
Bill Wendlingd9308a62009-03-10 21:23:25 +00001292 /// DebugTimer - Timer for the Dwarf debug writer.
1293 Timer *DebugTimer;
Devang Patel4d1709e2009-01-08 02:33:41 +00001294
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001295 struct FunctionDebugFrameInfo {
1296 unsigned Number;
1297 std::vector<MachineMove> Moves;
1298
1299 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman9ba5d4d2007-08-27 14:50:10 +00001300 Number(Num), Moves(M) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001301 };
1302
1303 std::vector<FunctionDebugFrameInfo> DebugFrames;
aslc200b112008-08-16 12:57:46 +00001304
Bill Wendlingd9308a62009-03-10 21:23:25 +00001305private:
Bill Wendlingdf25fd62009-03-10 21:59:25 +00001306 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
Bill Wendling278a3922009-03-10 21:47:45 +00001307 /// maps to the source id. Source id starts at 1.
1308 std::pair<unsigned, unsigned>
Bill Wendlingdf25fd62009-03-10 21:59:25 +00001309 getSourceDirectoryAndFileIds(unsigned SId) const {
Bill Wendling278a3922009-03-10 21:47:45 +00001310 return SourceIds[SId-1];
1311 }
1312
1313 /// getNumSourceDirectories - Return the number of source directories in the
1314 /// debug info.
1315 unsigned getNumSourceDirectories() const {
1316 return DirectoryNames.size();
1317 }
1318
1319 /// getSourceDirectoryName - Return the name of the directory corresponding
1320 /// to the id.
1321 const std::string &getSourceDirectoryName(unsigned Id) const {
1322 return DirectoryNames[Id - 1];
1323 }
1324
1325 /// getSourceFileName - Return the name of the source file corresponding
1326 /// to the id.
1327 const std::string &getSourceFileName(unsigned Id) const {
1328 return SourceFileNames[Id - 1];
1329 }
1330
1331 /// getNumSourceIds - Return the number of unique source ids.
Bill Wendling278a3922009-03-10 21:47:45 +00001332 unsigned getNumSourceIds() const {
1333 return SourceIds.size();
1334 }
1335
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001336 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
aslc200b112008-08-16 12:57:46 +00001337 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001338 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1339 // Profile the node so that we can make it unique.
1340 FoldingSetNodeID ID;
1341 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00001342
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001343 // Check the set for priors.
1344 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
aslc200b112008-08-16 12:57:46 +00001345
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001346 // If it's newly added.
1347 if (InSet == &Abbrev) {
aslc200b112008-08-16 12:57:46 +00001348 // Add to abbreviation list.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001349 Abbreviations.push_back(&Abbrev);
1350 // Assign the vector position + 1 as its number.
1351 Abbrev.setNumber(Abbreviations.size());
1352 } else {
1353 // Assign existing abbreviation number.
1354 Abbrev.setNumber(InSet->getNumber());
1355 }
1356 }
1357
1358 /// NewString - Add a string to the constant pool and returns a label.
1359 ///
1360 DWLabel NewString(const std::string &String) {
1361 unsigned StringID = StringPool.insert(String);
1362 return DWLabel("string", StringID);
1363 }
aslc200b112008-08-16 12:57:46 +00001364
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001365 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1366 /// entry.
1367 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1368 DIEntry *Value;
aslc200b112008-08-16 12:57:46 +00001369
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001370 if (Entry) {
1371 FoldingSetNodeID ID;
1372 DIEntry::Profile(ID, Entry);
1373 void *Where;
1374 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
aslc200b112008-08-16 12:57:46 +00001375
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001376 if (Value) return Value;
aslc200b112008-08-16 12:57:46 +00001377
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001378 Value = new DIEntry(Entry);
1379 ValuesSet.InsertNode(Value, Where);
1380 } else {
1381 Value = new DIEntry(Entry);
1382 }
aslc200b112008-08-16 12:57:46 +00001383
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001384 Values.push_back(Value);
1385 return Value;
1386 }
aslc200b112008-08-16 12:57:46 +00001387
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001388 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1389 ///
1390 void SetDIEntry(DIEntry *Value, DIE *Entry) {
Bill Wendling15afa002009-03-10 23:57:09 +00001391 Value->setEntry(Entry);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001392 // Add to values set if not already there. If it is, we merely have a
1393 // duplicate in the values list (no harm.)
1394 ValuesSet.GetOrInsertNode(Value);
1395 }
1396
1397 /// AddUInt - Add an unsigned integer attribute data and value.
1398 ///
1399 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1400 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1401
1402 FoldingSetNodeID ID;
1403 DIEInteger::Profile(ID, Integer);
1404 void *Where;
1405 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1406 if (!Value) {
1407 Value = new DIEInteger(Integer);
1408 ValuesSet.InsertNode(Value, Where);
1409 Values.push_back(Value);
1410 }
aslc200b112008-08-16 12:57:46 +00001411
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001412 Die->AddValue(Attribute, Form, Value);
1413 }
aslc200b112008-08-16 12:57:46 +00001414
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001415 /// AddSInt - Add an signed integer attribute data and value.
1416 ///
1417 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1418 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1419
1420 FoldingSetNodeID ID;
1421 DIEInteger::Profile(ID, (uint64_t)Integer);
1422 void *Where;
1423 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1424 if (!Value) {
1425 Value = new DIEInteger(Integer);
1426 ValuesSet.InsertNode(Value, Where);
1427 Values.push_back(Value);
1428 }
aslc200b112008-08-16 12:57:46 +00001429
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001430 Die->AddValue(Attribute, Form, Value);
1431 }
aslc200b112008-08-16 12:57:46 +00001432
Evan Cheng3e288912009-02-25 07:04:34 +00001433 /// AddString - Add a string attribute data and value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001434 ///
1435 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1436 const std::string &String) {
1437 FoldingSetNodeID ID;
1438 DIEString::Profile(ID, String);
1439 void *Where;
1440 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1441 if (!Value) {
1442 Value = new DIEString(String);
1443 ValuesSet.InsertNode(Value, Where);
1444 Values.push_back(Value);
1445 }
aslc200b112008-08-16 12:57:46 +00001446
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001447 Die->AddValue(Attribute, Form, Value);
1448 }
aslc200b112008-08-16 12:57:46 +00001449
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001450 /// AddLabel - Add a Dwarf label attribute data and value.
1451 ///
1452 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1453 const DWLabel &Label) {
1454 FoldingSetNodeID ID;
1455 DIEDwarfLabel::Profile(ID, Label);
1456 void *Where;
1457 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1458 if (!Value) {
1459 Value = new DIEDwarfLabel(Label);
1460 ValuesSet.InsertNode(Value, Where);
1461 Values.push_back(Value);
1462 }
aslc200b112008-08-16 12:57:46 +00001463
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001464 Die->AddValue(Attribute, Form, Value);
1465 }
aslc200b112008-08-16 12:57:46 +00001466
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001467 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1468 ///
1469 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1470 const std::string &Label) {
1471 FoldingSetNodeID ID;
1472 DIEObjectLabel::Profile(ID, Label);
1473 void *Where;
1474 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1475 if (!Value) {
1476 Value = new DIEObjectLabel(Label);
1477 ValuesSet.InsertNode(Value, Where);
1478 Values.push_back(Value);
1479 }
aslc200b112008-08-16 12:57:46 +00001480
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001481 Die->AddValue(Attribute, Form, Value);
1482 }
aslc200b112008-08-16 12:57:46 +00001483
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001484 /// AddSectionOffset - Add a section offset label attribute data and value.
1485 ///
1486 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1487 const DWLabel &Label, const DWLabel &Section,
1488 bool isEH = false, bool useSet = true) {
1489 FoldingSetNodeID ID;
1490 DIESectionOffset::Profile(ID, Label, Section);
1491 void *Where;
1492 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1493 if (!Value) {
1494 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1495 ValuesSet.InsertNode(Value, Where);
1496 Values.push_back(Value);
1497 }
aslc200b112008-08-16 12:57:46 +00001498
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001499 Die->AddValue(Attribute, Form, Value);
1500 }
aslc200b112008-08-16 12:57:46 +00001501
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001502 /// AddDelta - Add a label delta attribute data and value.
1503 ///
1504 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001505 const DWLabel &Hi, const DWLabel &Lo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001506 FoldingSetNodeID ID;
1507 DIEDelta::Profile(ID, Hi, Lo);
1508 void *Where;
1509 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1510 if (!Value) {
1511 Value = new DIEDelta(Hi, Lo);
1512 ValuesSet.InsertNode(Value, Where);
1513 Values.push_back(Value);
1514 }
aslc200b112008-08-16 12:57:46 +00001515
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001516 Die->AddValue(Attribute, Form, Value);
1517 }
aslc200b112008-08-16 12:57:46 +00001518
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001519 /// AddDIEntry - Add a DIE attribute data and value.
1520 ///
1521 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1522 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1523 }
1524
1525 /// AddBlock - Add block data.
1526 ///
1527 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1528 Block->ComputeSize(*this);
1529 FoldingSetNodeID ID;
1530 Block->Profile(ID);
1531 void *Where;
1532 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1533 if (!Value) {
1534 Value = Block;
1535 ValuesSet.InsertNode(Value, Where);
1536 Values.push_back(Value);
1537 } else {
Chris Lattner3de66892007-09-21 18:25:53 +00001538 // Already exists, reuse the previous one.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001539 delete Block;
Chris Lattner3de66892007-09-21 18:25:53 +00001540 Block = cast<DIEBlock>(Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001541 }
aslc200b112008-08-16 12:57:46 +00001542
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001543 Die->AddValue(Attribute, Block->BestForm(), Value);
1544 }
1545
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001546 /// AddSourceLine - Add location information to specified debug information
1547 /// entry.
Devang Patel7c8a2772009-01-16 19:28:14 +00001548 void AddSourceLine(DIE *Die, const DIVariable *V) {
Devang Patel4d1709e2009-01-08 02:33:41 +00001549 unsigned FileID = 0;
1550 unsigned Line = V->getLineNumber();
Devang Patel2ae1db52009-01-30 18:20:31 +00001551 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1552 FileID = Unit->getID();
Devang Pateld94b6932009-02-10 06:04:08 +00001553 assert (FileID && "Invalid file id");
Devang Patel4d1709e2009-01-08 02:33:41 +00001554 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1555 AddUInt(Die, DW_AT_decl_line, 0, Line);
1556 }
1557
1558 /// AddSourceLine - Add location information to specified debug information
1559 /// entry.
Devang Patel7c8a2772009-01-16 19:28:14 +00001560 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Devang Patel5f244e32009-01-05 22:35:52 +00001561 unsigned FileID = 0;
1562 unsigned Line = G->getLineNumber();
Devang Patel2ae1db52009-01-30 18:20:31 +00001563 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1564 FileID = Unit->getID();
Devang Pateld94b6932009-02-10 06:04:08 +00001565 assert (FileID && "Invalid file id");
Devang Patel5f244e32009-01-05 22:35:52 +00001566 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1567 AddUInt(Die, DW_AT_decl_line, 0, Line);
1568 }
1569
Devang Patel7c8a2772009-01-16 19:28:14 +00001570 void AddSourceLine(DIE *Die, const DIType *Ty) {
Devang Patel5f244e32009-01-05 22:35:52 +00001571 unsigned FileID = 0;
Devang Patel7c8a2772009-01-16 19:28:14 +00001572 unsigned Line = Ty->getLineNumber();
Devang Patel2ae1db52009-01-30 18:20:31 +00001573 DICompileUnit CU = Ty->getCompileUnit();
1574 if (CU.isNull())
1575 return;
1576 CompileUnit *Unit = FindCompileUnit(CU);
1577 FileID = Unit->getID();
Devang Pateld94b6932009-02-10 06:04:08 +00001578 assert (FileID && "Invalid file id");
Devang Patel5f244e32009-01-05 22:35:52 +00001579 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1580 AddUInt(Die, DW_AT_decl_line, 0, Line);
1581 }
1582
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001583 /// AddAddress - Add an address attribute to a die based on the location
1584 /// provided.
1585 void AddAddress(DIE *Die, unsigned Attribute,
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001586 const MachineLocation &Location) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001587 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001588 DIEBlock *Block = new DIEBlock();
aslc200b112008-08-16 12:57:46 +00001589
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001590 if (Location.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001591 if (Reg < 32) {
1592 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1593 } else {
1594 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1595 AddUInt(Block, 0, DW_FORM_udata, Reg);
1596 }
1597 } else {
1598 if (Reg < 32) {
1599 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1600 } else {
1601 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1602 AddUInt(Block, 0, DW_FORM_udata, Reg);
1603 }
1604 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1605 }
aslc200b112008-08-16 12:57:46 +00001606
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001607 AddBlock(Die, Attribute, 0, Block);
1608 }
aslc200b112008-08-16 12:57:46 +00001609
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001610 /// AddType - Add a new type attribute to the specified entity.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001611 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel165ed512009-01-23 19:13:31 +00001612 if (Ty.isNull())
Devang Patel4a4cbe72009-01-05 21:47:57 +00001613 return;
Devang Patel4a4cbe72009-01-05 21:47:57 +00001614
1615 // Check for pre-existence.
1616 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1617 // If it exists then use the existing value.
1618 if (Slot) {
1619 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1620 return;
1621 }
1622
1623 // Set up proxy.
1624 Slot = NewDIEntry();
1625
1626 // Construct type.
1627 DIE Buffer(DW_TAG_base_type);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001628 if (Ty.isBasicType(Ty.getTag()))
1629 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1630 else if (Ty.isDerivedType(Ty.getTag()))
1631 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1632 else {
Bill Wendling824a8bf2009-02-03 21:17:20 +00001633 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelef4bf3b2009-01-15 19:26:23 +00001634 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1635 }
1636
Devang Patelb0cb07c2009-01-27 23:22:55 +00001637 // Add debug information entry to entity and appropriate context.
1638 DIE *Die = NULL;
1639 DIDescriptor Context = Ty.getContext();
1640 if (!Context.isNull())
1641 Die = DW_Unit->getDieMapSlotFor(Context.getGV());
1642
1643 if (Die) {
1644 DIE *Child = new DIE(Buffer);
1645 Die->AddChild(Child);
1646 Buffer.Detach();
1647 SetDIEntry(Slot, Child);
Bill Wendling824a8bf2009-02-03 21:17:20 +00001648 } else {
Devang Patelb0cb07c2009-01-27 23:22:55 +00001649 Die = DW_Unit->AddDie(Buffer);
1650 SetDIEntry(Slot, Die);
1651 }
1652
Devang Patel4a4cbe72009-01-05 21:47:57 +00001653 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1654 }
1655
Devang Patel46d13752009-01-05 19:07:53 +00001656 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1657 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001658 DIBasicType BTy) {
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001659
Devang Patelfc187162009-01-05 17:57:47 +00001660 // Get core information.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001661 std::string Name;
1662 BTy.getName(Name);
Devang Patelfc187162009-01-05 17:57:47 +00001663 Buffer.setTag(DW_TAG_base_type);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001664 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patelfc187162009-01-05 17:57:47 +00001665 // Add name if not anonymous or intermediate type.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001666 if (!Name.empty())
Devang Patelfc187162009-01-05 17:57:47 +00001667 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001668 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelfc187162009-01-05 17:57:47 +00001669 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1670 }
1671
Devang Patel46d13752009-01-05 19:07:53 +00001672 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1673 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001674 DIDerivedType DTy) {
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001675
Devang Patelfc187162009-01-05 17:57:47 +00001676 // Get core information.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001677 std::string Name;
1678 DTy.getName(Name);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001679 uint64_t Size = DTy.getSizeInBits() >> 3;
1680 unsigned Tag = DTy.getTag();
Bill Wendling1c5842b2009-03-09 05:04:40 +00001681
Devang Patelfc187162009-01-05 17:57:47 +00001682 // FIXME - Workaround for templates.
1683 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1684
1685 Buffer.setTag(Tag);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001686
Devang Patelfc187162009-01-05 17:57:47 +00001687 // Map to main type, void will not have a type.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001688 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel4a4cbe72009-01-05 21:47:57 +00001689 AddType(DW_Unit, &Buffer, FromTy);
Devang Patelfc187162009-01-05 17:57:47 +00001690
1691 // Add name if not anonymous or intermediate type.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001692 if (!Name.empty())
Evan Cheng3e288912009-02-25 07:04:34 +00001693 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelfc187162009-01-05 17:57:47 +00001694
1695 // Add size if non-zero (derived types might be zero-sized.)
1696 if (Size)
1697 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1698
1699 // Add source line info if available and TyDesc is not a forward
1700 // declaration.
Devang Patele34e0882009-01-27 00:45:04 +00001701 if (!DTy.isForwardDecl())
1702 AddSourceLine(&Buffer, &DTy);
Devang Patelfc187162009-01-05 17:57:47 +00001703 }
1704
Devang Patel30c01372009-01-05 19:55:51 +00001705 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1706 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001707 DICompositeType CTy) {
Devang Patelb28de842009-01-17 08:01:33 +00001708 // Get core information.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001709 std::string Name;
1710 CTy.getName(Name);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001711
Devang Patelef4bf3b2009-01-15 19:26:23 +00001712 uint64_t Size = CTy.getSizeInBits() >> 3;
1713 unsigned Tag = CTy.getTag();
Devang Patel8050bd72009-01-23 01:19:09 +00001714 Buffer.setTag(Tag);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001715
Devang Patel30c01372009-01-05 19:55:51 +00001716 switch (Tag) {
1717 case DW_TAG_vector_type:
1718 case DW_TAG_array_type:
Devang Patelef4bf3b2009-01-15 19:26:23 +00001719 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patel30c01372009-01-05 19:55:51 +00001720 break;
Devang Patel3798f492009-01-20 18:35:14 +00001721 case DW_TAG_enumeration_type:
1722 {
1723 DIArray Elements = CTy.getTypeArray();
1724 // Add enumerators to enumeration type.
1725 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1726 DIE *ElemDie = NULL;
1727 DIEnumerator Enum(Elements.getElement(i).getGV());
1728 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
1729 Buffer.AddChild(ElemDie);
1730 }
1731 }
1732 break;
Devang Patel30c01372009-01-05 19:55:51 +00001733 case DW_TAG_subroutine_type:
1734 {
1735 // Add prototype flag.
1736 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001737 DIArray Elements = CTy.getTypeArray();
Devang Patel30c01372009-01-05 19:55:51 +00001738 // Add return type.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001739 DIDescriptor RTy = Elements.getElement(0);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001740 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel4a4cbe72009-01-05 21:47:57 +00001741
Devang Patel30c01372009-01-05 19:55:51 +00001742 // Add arguments.
1743 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1744 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel4a4cbe72009-01-05 21:47:57 +00001745 DIDescriptor Ty = Elements.getElement(i);
Devang Pateld40a7e52009-01-17 06:57:25 +00001746 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patel30c01372009-01-05 19:55:51 +00001747 Buffer.AddChild(Arg);
1748 }
1749 }
1750 break;
1751 case DW_TAG_structure_type:
1752 case DW_TAG_union_type:
Devang Patel09353602009-03-25 00:28:40 +00001753 case DW_TAG_class_type:
Devang Patel30c01372009-01-05 19:55:51 +00001754 {
1755 // Add elements to structure type.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001756 DIArray Elements = CTy.getTypeArray();
Devang Patelcf7acb12009-01-16 00:50:53 +00001757
1758 // A forward struct declared type may not have elements available.
1759 if (Elements.isNull())
1760 break;
1761
Devang Patel30c01372009-01-05 19:55:51 +00001762 // Add elements to structure type.
1763 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1764 DIDescriptor Element = Elements.getElement(i);
Devang Patelb28de842009-01-17 08:01:33 +00001765 DIE *ElemDie = NULL;
Devang Patelef4bf3b2009-01-15 19:26:23 +00001766 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel245446c2009-01-17 08:05:14 +00001767 ElemDie = CreateSubprogramDIE(DW_Unit,
1768 DISubprogram(Element.getGV()));
Devang Patelb28de842009-01-17 08:01:33 +00001769 else if (Element.getTag() == dwarf::DW_TAG_variable) // ???
1770 ElemDie = CreateGlobalVariableDIE(DW_Unit,
1771 DIGlobalVariable(Element.getGV()));
Devang Patel5c643892009-01-20 21:02:02 +00001772 else
1773 ElemDie = CreateMemberDIE(DW_Unit,
1774 DIDerivedType(Element.getGV()));
Devang Patel245446c2009-01-17 08:05:14 +00001775 Buffer.AddChild(ElemDie);
Devang Patel30c01372009-01-05 19:55:51 +00001776 }
Devang Patel74193d72009-02-17 22:43:44 +00001777 unsigned RLang = CTy.getRunTimeLang();
1778 if (RLang)
1779 AddUInt(&Buffer, DW_AT_APPLE_runtime_class, DW_FORM_data1, RLang);
Devang Patel30c01372009-01-05 19:55:51 +00001780 }
1781 break;
1782 default:
1783 break;
1784 }
1785
1786 // Add name if not anonymous or intermediate type.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001787 if (!Name.empty())
Evan Cheng3e288912009-02-25 07:04:34 +00001788 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel30c01372009-01-05 19:55:51 +00001789
Devang Patele34e0882009-01-27 00:45:04 +00001790 if (Tag == DW_TAG_enumeration_type || Tag == DW_TAG_structure_type
1791 || Tag == DW_TAG_union_type) {
1792 // Add size if non-zero (derived types might be zero-sized.)
1793 if (Size)
1794 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1795 else {
1796 // Add zero size if it is not a forward declaration.
1797 if (CTy.isForwardDecl())
1798 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1799 else
1800 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1801 }
1802
1803 // Add source line info if available.
1804 if (!CTy.isForwardDecl())
1805 AddSourceLine(&Buffer, &CTy);
Devang Patel30c01372009-01-05 19:55:51 +00001806 }
Devang Patel30c01372009-01-05 19:55:51 +00001807 }
1808
Bill Wendling824a8bf2009-02-03 21:17:20 +00001809 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1810 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelef4bf3b2009-01-15 19:26:23 +00001811 int64_t L = SR.getLo();
1812 int64_t H = SR.getHi();
Devang Patel6fb54132009-01-05 18:33:01 +00001813 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1814 if (L != H) {
1815 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1816 if (L)
Devang Patel245446c2009-01-17 08:05:14 +00001817 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1818 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel6fb54132009-01-05 18:33:01 +00001819 }
1820 Buffer.AddChild(DW_Subrange);
1821 }
1822
1823 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1824 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1825 DICompositeType *CTy) {
1826 Buffer.setTag(DW_TAG_array_type);
1827 if (CTy->getTag() == DW_TAG_vector_type)
1828 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1829
Devang Patel6ab30e52009-01-28 21:08:20 +00001830 // Emit derived type.
1831 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel6fb54132009-01-05 18:33:01 +00001832 DIArray Elements = CTy->getTypeArray();
Devang Patel6fb54132009-01-05 18:33:01 +00001833
1834 // Construct an anonymous type for index type.
1835 DIE IdxBuffer(DW_TAG_base_type);
1836 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1837 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1838 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1839
1840 // Add subranges to array type.
1841 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel30c01372009-01-05 19:55:51 +00001842 DIDescriptor Element = Elements.getElement(i);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001843 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1844 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel6fb54132009-01-05 18:33:01 +00001845 }
1846 }
1847
Bill Wendling824a8bf2009-02-03 21:17:20 +00001848 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3798f492009-01-20 18:35:14 +00001849 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patela566e812009-01-05 18:38:38 +00001850
1851 DIE *Enumerator = new DIE(DW_TAG_enumerator);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001852 std::string Name;
1853 ETy->getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001854 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
Devang Patela566e812009-01-05 18:38:38 +00001855 int64_t Value = ETy->getEnumValue();
1856 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Patel3798f492009-01-20 18:35:14 +00001857 return Enumerator;
Devang Patela566e812009-01-05 18:38:38 +00001858 }
Devang Patel6fb54132009-01-05 18:33:01 +00001859
Devang Patelb28de842009-01-17 08:01:33 +00001860 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendling824a8bf2009-02-03 21:17:20 +00001861 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patelb28de842009-01-17 08:01:33 +00001862 {
1863 DIE *GVDie = new DIE(DW_TAG_variable);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001864 std::string Name;
1865 GV.getDisplayName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001866 AddString(GVDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001867 std::string LinkageName;
1868 GV.getLinkageName(LinkageName);
1869 if (!LinkageName.empty())
Devang Patelb28de842009-01-17 08:01:33 +00001870 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1871 AddType(DW_Unit, GVDie, GV.getType());
1872 if (!GV.isLocalToUnit())
1873 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
1874 AddSourceLine(GVDie, &GV);
1875 return GVDie;
Devang Patel526b01d2009-01-05 18:59:44 +00001876 }
1877
Devang Patel5c643892009-01-20 21:02:02 +00001878 /// CreateMemberDIE - Create new member DIE.
1879 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
1880 DIE *MemberDie = new DIE(DT.getTag());
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001881 std::string Name;
1882 DT.getName(Name);
1883 if (!Name.empty())
Devang Patel5c643892009-01-20 21:02:02 +00001884 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1885
1886 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1887
1888 AddSourceLine(MemberDie, &DT);
1889
Devang Patelf1f30d42009-02-17 21:23:59 +00001890 uint64_t Size = DT.getSizeInBits();
1891 uint64_t FieldSize = DT.getOriginalTypeSize();
1892
1893 if (Size != FieldSize) {
1894 // Handle bitfield.
1895 AddUInt(MemberDie, DW_AT_byte_size, 0, DT.getOriginalTypeSize() >> 3);
1896 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1897
1898 uint64_t Offset = DT.getOffsetInBits();
1899 uint64_t FieldOffset = Offset;
1900 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1901 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1902 FieldOffset = (HiMark - FieldSize);
1903 Offset -= FieldOffset;
1904 // Maybe we need to work from the other end.
1905 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1906 AddUInt(MemberDie, DW_AT_bit_offset, 0, Offset);
1907 }
Devang Patel5c643892009-01-20 21:02:02 +00001908 DIEBlock *Block = new DIEBlock();
1909 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1910 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1911 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1912
Devang Patel2e7ee192009-01-21 00:08:04 +00001913 if (DT.isProtected())
1914 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1915 else if (DT.isPrivate())
1916 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1917
Devang Patel5c643892009-01-20 21:02:02 +00001918 return MemberDie;
1919 }
1920
Devang Patelb28de842009-01-17 08:01:33 +00001921 /// CreateSubprogramDIE - Create new DIE using SP.
1922 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Devang Patel245446c2009-01-17 08:05:14 +00001923 const DISubprogram &SP,
1924 bool IsConstructor = false) {
Devang Patelb28de842009-01-17 08:01:33 +00001925 DIE *SPDie = new DIE(DW_TAG_subprogram);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001926 std::string Name;
1927 SP.getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001928 AddString(SPDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001929 std::string LinkageName;
1930 SP.getLinkageName(LinkageName);
1931 if (!LinkageName.empty())
Devang Patelb28de842009-01-17 08:01:33 +00001932 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Devang Patel245446c2009-01-17 08:05:14 +00001933 LinkageName);
Devang Patelb28de842009-01-17 08:01:33 +00001934 AddSourceLine(SPDie, &SP);
Devang Patel526b01d2009-01-05 18:59:44 +00001935
Devang Patelb28de842009-01-17 08:01:33 +00001936 DICompositeType SPTy = SP.getType();
1937 DIArray Args = SPTy.getTypeArray();
1938
Devang Patel526b01d2009-01-05 18:59:44 +00001939 // Add Return Type.
Devang Patel6e199962009-04-08 22:18:45 +00001940 unsigned SPTag = SPTy.getTag();
Devang Patel688a19f2009-02-27 18:05:21 +00001941 if (!IsConstructor) {
Devang Patel6e199962009-04-08 22:18:45 +00001942 if (Args.isNull() || SPTag != DW_TAG_subroutine_type)
Devang Patel688a19f2009-02-27 18:05:21 +00001943 AddType(DW_Unit, SPDie, SPTy);
1944 else
1945 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
1946 }
Devang Patel922d1592009-01-30 01:21:46 +00001947
Devang Patelace2cf62009-02-02 17:51:41 +00001948 if (!SP.isDefinition()) {
1949 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
1950 // Add arguments.
1951 // Do not add arguments for subprogram definition. They will be
1952 // handled through RecordVariable.
Devang Patel6e199962009-04-08 22:18:45 +00001953 if (SPTag == DW_TAG_subroutine_type)
Devang Patelace2cf62009-02-02 17:51:41 +00001954 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1955 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1956 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
1957 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
1958 SPDie->AddChild(Arg);
1959 }
1960 }
Devang Patel922d1592009-01-30 01:21:46 +00001961
Devang Patele075e072009-02-24 00:52:19 +00001962 unsigned Lang = SP.getCompileUnit().getLanguage();
1963 if (Lang == DW_LANG_C99 || Lang == DW_LANG_C89
1964 || Lang == DW_LANG_ObjC)
1965 AddUInt(SPDie, DW_AT_prototyped, DW_FORM_flag, 1);
1966
Devang Patelef4bf3b2009-01-15 19:26:23 +00001967 if (!SP.isLocalToUnit())
Devang Patel922d1592009-01-30 01:21:46 +00001968 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001969
1970 // DW_TAG_inlined_subroutine may refer to this DIE.
1971 DIE *&Slot = DW_Unit->getDieMapSlotFor(SP.getGV());
1972 Slot = SPDie;
Devang Patelb28de842009-01-17 08:01:33 +00001973 return SPDie;
Devang Patel526b01d2009-01-05 18:59:44 +00001974 }
1975
Devang Patelb28de842009-01-17 08:01:33 +00001976 /// FindCompileUnit - Get the compile unit for the given descriptor.
1977 ///
Devang Patel5f244e32009-01-05 22:35:52 +00001978 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
Evan Cheng3e288912009-02-25 07:04:34 +00001979 CompileUnit *DW_Unit = CompileUnitMap[Unit.getGV()];
Devang Patel5f244e32009-01-05 22:35:52 +00001980 assert(DW_Unit && "Missing compile unit.");
1981 return DW_Unit;
1982 }
1983
Devang Patel42f6bed2009-01-13 23:54:55 +00001984 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel4d1709e2009-01-08 02:33:41 +00001985 ///
1986 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1987 // Get the descriptor.
Devang Patel7c8a2772009-01-16 19:28:14 +00001988 const DIVariable &VD = DV->getVariable();
Devang Patel4d1709e2009-01-08 02:33:41 +00001989
1990 // Translate tag to proper Dwarf tag. The result variable is dropped for
1991 // now.
1992 unsigned Tag;
Devang Patel7c8a2772009-01-16 19:28:14 +00001993 switch (VD.getTag()) {
Devang Patel4d1709e2009-01-08 02:33:41 +00001994 case DW_TAG_return_variable: return NULL;
1995 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1996 case DW_TAG_auto_variable: // fall thru
1997 default: Tag = DW_TAG_variable; break;
1998 }
1999
2000 // Define variable debug information entry.
2001 DIE *VariableDie = new DIE(Tag);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00002002 std::string Name;
2003 VD.getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00002004 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Devang Patel4d1709e2009-01-08 02:33:41 +00002005
2006 // Add source line info if available.
Devang Patel7c8a2772009-01-16 19:28:14 +00002007 AddSourceLine(VariableDie, &VD);
Devang Patel4d1709e2009-01-08 02:33:41 +00002008
2009 // Add variable type.
Devang Patel7c8a2772009-01-16 19:28:14 +00002010 AddType(Unit, VariableDie, VD.getType());
Devang Patel4d1709e2009-01-08 02:33:41 +00002011
2012 // Add variable address.
2013 MachineLocation Location;
2014 Location.set(RI->getFrameRegister(*MF),
2015 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
2016 AddAddress(VariableDie, DW_AT_location, Location);
2017
2018 return VariableDie;
2019 }
2020
Devang Patel4d1709e2009-01-08 02:33:41 +00002021 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2022 ///
2023 DbgScope *getOrCreateScope(GlobalVariable *V) {
2024 DbgScope *&Slot = DbgScopeMap[V];
Bill Wendlinge0f3a262009-02-20 20:40:28 +00002025 if (Slot) return Slot;
2026
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002027 DbgScope *Parent = NULL;
2028 DIBlock Block(V);
2029 if (!Block.isNull()) {
2030 DIDescriptor ParentDesc = Block.getContext();
2031 Parent =
2032 ParentDesc.isNull() ? NULL : getOrCreateScope(ParentDesc.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002033 }
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002034 Slot = new DbgScope(Parent, DIDescriptor(V));
Bill Wendlinge0f3a262009-02-20 20:40:28 +00002035
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002036 if (Parent)
Bill Wendlinge0f3a262009-02-20 20:40:28 +00002037 Parent->AddScope(Slot);
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002038 else
Bill Wendlinge0f3a262009-02-20 20:40:28 +00002039 // First function is top level function.
2040 RootDbgScope = Slot;
Bill Wendlinge0f3a262009-02-20 20:40:28 +00002041
Devang Patel4d1709e2009-01-08 02:33:41 +00002042 return Slot;
2043 }
2044
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002045 /// createInlinedSubroutineScope - Returns the scope associated with the
2046 /// inlined subroutine.
2047 ///
2048 DbgScope *createInlinedSubroutineScope(DISubprogram SP, unsigned Src,
2049 unsigned Line, unsigned Col) {
2050 DbgScope *Scope =
2051 new DbgInlinedSubroutineScope(NULL, SP, Src, Line, Col);
2052
2053 // FIXME - Add inlined function scopes to the root so we can delete them
2054 // later.
2055 assert (RootDbgScope && "Function scope info missing!");
2056 RootDbgScope->AddScope(Scope);
2057 return Scope;
2058 }
2059
Devang Patel4d1709e2009-01-08 02:33:41 +00002060 /// ConstructDbgScope - Construct the components of a scope.
2061 ///
2062 void ConstructDbgScope(DbgScope *ParentScope,
2063 unsigned ParentStartID, unsigned ParentEndID,
2064 DIE *ParentDie, CompileUnit *Unit) {
2065 // Add variables to scope.
Devang Patel63c22f42009-01-10 02:42:49 +00002066 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
Devang Patel4d1709e2009-01-08 02:33:41 +00002067 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2068 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2069 if (VariableDie) ParentDie->AddChild(VariableDie);
2070 }
2071
2072 // Add nested scopes.
Devang Patel63c22f42009-01-10 02:42:49 +00002073 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel4d1709e2009-01-08 02:33:41 +00002074 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2075 // Define the Scope debug information entry.
2076 DbgScope *Scope = Scopes[j];
Devang Patel4d1709e2009-01-08 02:33:41 +00002077
Devang Patelb9224922009-01-12 18:41:00 +00002078 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2079 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel4d1709e2009-01-08 02:33:41 +00002080
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002081 // Ignore empty scopes.
2082 // Do not ignore inlined scope even if it does not have any
2083 // variables or scopes.
Devang Patel4d1709e2009-01-08 02:33:41 +00002084 if (StartID == EndID && StartID != 0) continue;
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002085 if (!Scope->isInlinedSubroutine()
Devang Patel88bf96e2009-04-13 17:02:03 +00002086 && Scope->getScopes().empty() && Scope->getVariables().empty())
2087 continue;
Devang Patel4d1709e2009-01-08 02:33:41 +00002088
2089 if (StartID == ParentStartID && EndID == ParentEndID) {
2090 // Just add stuff to the parent scope.
2091 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2092 } else {
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002093 DIE *ScopeDie = NULL;
2094 if (MainCU && TAI->doesDwarfUsesInlineInfoSection()) {
2095 ScopeDie = new DIE(DW_TAG_inlined_subroutine);
2096 DIE *Origin = MainCU->getDieMapSlotFor(Scope->getDesc().getGV());
2097 AddDIEntry(ScopeDie, DW_AT_abstract_origin, DW_FORM_ref4, Origin);
2098 AddUInt(ScopeDie, DW_AT_call_file, 0, Scope->getFile());
2099 AddUInt(ScopeDie, DW_AT_call_line, 0, Scope->getLine());
2100 AddUInt(ScopeDie, DW_AT_call_column, 0, Scope->getColumn());
Devang Patel4d1709e2009-01-08 02:33:41 +00002101 }
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002102 else
2103 ScopeDie = new DIE(DW_TAG_lexical_block);
2104
2105 // Add the scope bounds.
2106 if (StartID) {
2107 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2108 DWLabel("label", StartID));
2109 } else {
2110 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2111 DWLabel("func_begin", SubprogramCount));
2112 }
2113 if (EndID) {
2114 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2115 DWLabel("label", EndID));
2116 } else {
2117 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2118 DWLabel("func_end", SubprogramCount));
2119 }
2120
2121 // Add the scope contents.
2122 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2123 ParentDie->AddChild(ScopeDie);
Devang Patel4d1709e2009-01-08 02:33:41 +00002124 }
2125 }
2126 }
2127
2128 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2129 ///
2130 void ConstructRootDbgScope(DbgScope *RootScope) {
2131 // Exit if there is no root scope.
2132 if (!RootScope) return;
Devang Patel2560d922009-01-15 18:25:17 +00002133 DIDescriptor Desc = RootScope->getDesc();
2134 if (Desc.isNull())
2135 return;
Devang Patel4d1709e2009-01-08 02:33:41 +00002136
2137 // Get the subprogram debug information entry.
Devang Patel2560d922009-01-15 18:25:17 +00002138 DISubprogram SPD(Desc.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002139
2140 // Get the compile unit context.
Devang Patel2ae1db52009-01-30 18:20:31 +00002141 CompileUnit *Unit = MainCU;
2142 if (!Unit)
2143 Unit = FindCompileUnit(SPD.getCompileUnit());
Devang Patel4d1709e2009-01-08 02:33:41 +00002144
2145 // Get the subprogram die.
Devang Patel57ec9ac2009-01-12 22:58:14 +00002146 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002147 assert(SPDie && "Missing subprogram descriptor");
2148
2149 // Add the function bounds.
2150 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2151 DWLabel("func_begin", SubprogramCount));
2152 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2153 DWLabel("func_end", SubprogramCount));
2154 MachineLocation Location(RI->getFrameRegister(*MF));
2155 AddAddress(SPDie, DW_AT_frame_base, Location);
2156
2157 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2158 }
2159
2160 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2161 ///
2162 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Cheng3e288912009-02-25 07:04:34 +00002163 const char *FnName = MF->getFunction()->getNameStart();
2164 if (MainCU) {
Bill Wendlinge06da442009-04-09 21:49:15 +00002165 StringMap<DIE*> &Globals = MainCU->getGlobals();
2166 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Cheng3e288912009-02-25 07:04:34 +00002167 if (GI != Globals.end()) {
2168 DIE *SPDie = GI->second;
Devang Patel4d1709e2009-01-08 02:33:41 +00002169
2170 // Add the function bounds.
2171 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2172 DWLabel("func_begin", SubprogramCount));
2173 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2174 DWLabel("func_end", SubprogramCount));
2175
2176 MachineLocation Location(RI->getFrameRegister(*MF));
2177 AddAddress(SPDie, DW_AT_frame_base, Location);
2178 return;
2179 }
Evan Cheng3e288912009-02-25 07:04:34 +00002180 } else {
2181 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2182 CompileUnit *Unit = CompileUnits[i];
Bill Wendlinge06da442009-04-09 21:49:15 +00002183 StringMap<DIE*> &Globals = Unit->getGlobals();
2184 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Cheng3e288912009-02-25 07:04:34 +00002185 if (GI != Globals.end()) {
2186 DIE *SPDie = GI->second;
2187
2188 // Add the function bounds.
2189 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2190 DWLabel("func_begin", SubprogramCount));
2191 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2192 DWLabel("func_end", SubprogramCount));
2193
2194 MachineLocation Location(RI->getFrameRegister(*MF));
2195 AddAddress(SPDie, DW_AT_frame_base, Location);
2196 return;
2197 }
2198 }
Devang Patel4d1709e2009-01-08 02:33:41 +00002199 }
Evan Cheng3e288912009-02-25 07:04:34 +00002200
Devang Patel4d1709e2009-01-08 02:33:41 +00002201#if 0
2202 // FIXME: This is causing an abort because C++ mangled names are compared
2203 // with their unmangled counterparts. See PR2885. Don't do this assert.
2204 assert(0 && "Couldn't find DIE for machine function!");
2205#endif
Evan Cheng3e288912009-02-25 07:04:34 +00002206 return;
Devang Patel4d1709e2009-01-08 02:33:41 +00002207 }
2208
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002209 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2210 /// tools to recognize the object file contains Dwarf information.
2211 void EmitInitial() {
2212 // Check to see if we already emitted intial headers.
2213 if (didInitial) return;
2214 didInitial = true;
aslc200b112008-08-16 12:57:46 +00002215
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002216 // Dwarf sections base addresses.
2217 if (TAI->doesDwarfRequireFrameSection()) {
2218 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2219 EmitLabel("section_debug_frame", 0);
2220 }
2221 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2222 EmitLabel("section_info", 0);
2223 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2224 EmitLabel("section_abbrev", 0);
2225 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2226 EmitLabel("section_aranges", 0);
Scott Michel79f01f52009-01-26 22:32:51 +00002227 if (TAI->doesSupportMacInfoSection()) {
2228 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2229 EmitLabel("section_macinfo", 0);
2230 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002231 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2232 EmitLabel("section_line", 0);
2233 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2234 EmitLabel("section_loc", 0);
2235 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2236 EmitLabel("section_pubnames", 0);
2237 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2238 EmitLabel("section_str", 0);
2239 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2240 EmitLabel("section_ranges", 0);
2241
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002242 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002243 EmitLabel("text_begin", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00002244 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002245 EmitLabel("data_begin", 0);
2246 }
2247
2248 /// EmitDIE - Recusively Emits a debug information entry.
2249 ///
2250 void EmitDIE(DIE *Die) {
2251 // Get the abbreviation for this DIE.
2252 unsigned AbbrevNumber = Die->getAbbrevNumber();
2253 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
aslc200b112008-08-16 12:57:46 +00002254
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002255 Asm->EOL();
2256
2257 // Emit the code (index) for the abbreviation.
2258 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng0eeed442008-07-01 23:18:29 +00002259
Evan Cheng42ceb472009-03-25 01:47:28 +00002260 if (Asm->isVerbose())
Evan Cheng0eeed442008-07-01 23:18:29 +00002261 Asm->EOL(std::string("Abbrev [" +
2262 utostr(AbbrevNumber) +
2263 "] 0x" + utohexstr(Die->getOffset()) +
2264 ":0x" + utohexstr(Die->getSize()) + " " +
2265 TagString(Abbrev->getTag())));
2266 else
2267 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00002268
Owen Anderson88dd6232008-06-24 21:44:59 +00002269 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2270 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
aslc200b112008-08-16 12:57:46 +00002271
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002272 // Emit the DIE attribute values.
2273 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2274 unsigned Attr = AbbrevData[i].getAttribute();
2275 unsigned Form = AbbrevData[i].getForm();
2276 assert(Form && "Too many attributes for DIE (check abbreviation)");
aslc200b112008-08-16 12:57:46 +00002277
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002278 switch (Attr) {
2279 case DW_AT_sibling: {
2280 Asm->EmitInt32(Die->SiblingOffset());
2281 break;
2282 }
2283 default: {
2284 // Emit an attribute using the defined form.
2285 Values[i]->EmitValue(*this, Form);
2286 break;
2287 }
2288 }
aslc200b112008-08-16 12:57:46 +00002289
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002290 Asm->EOL(AttributeString(Attr));
2291 }
aslc200b112008-08-16 12:57:46 +00002292
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002293 // Emit the DIE children if any.
2294 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2295 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002296
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002297 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2298 EmitDIE(Children[j]);
2299 }
aslc200b112008-08-16 12:57:46 +00002300
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002301 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2302 }
2303 }
2304
2305 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2306 ///
2307 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2308 // Get the children.
2309 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002310
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002311 // If not last sibling and has children then add sibling offset attribute.
2312 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2313
2314 // Record the abbreviation.
2315 AssignAbbrevNumber(Die->getAbbrev());
aslc200b112008-08-16 12:57:46 +00002316
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002317 // Get the abbreviation for this DIE.
2318 unsigned AbbrevNumber = Die->getAbbrevNumber();
2319 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2320
2321 // Set DIE offset
2322 Die->setOffset(Offset);
aslc200b112008-08-16 12:57:46 +00002323
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002324 // Start the size with the size of abbreviation code.
aslc200b112008-08-16 12:57:46 +00002325 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2326
Owen Anderson88dd6232008-06-24 21:44:59 +00002327 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2328 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002329
2330 // Size the DIE attribute values.
2331 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2332 // Size attribute value.
2333 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2334 }
aslc200b112008-08-16 12:57:46 +00002335
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002336 // Size the DIE children if any.
2337 if (!Children.empty()) {
2338 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2339 "Children flag not set");
aslc200b112008-08-16 12:57:46 +00002340
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002341 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2342 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2343 }
aslc200b112008-08-16 12:57:46 +00002344
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002345 // End of children marker.
2346 Offset += sizeof(int8_t);
2347 }
2348
2349 Die->setSize(Offset - Die->getOffset());
2350 return Offset;
2351 }
2352
2353 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2354 ///
2355 void SizeAndOffsets() {
2356 // Process base compile unit.
Devang Patel2ae1db52009-01-30 18:20:31 +00002357 if (MainCU) {
2358 // Compute size of compile unit header
2359 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2360 sizeof(int16_t) + // DWARF version number
2361 sizeof(int32_t) + // Offset Into Abbrev. Section
2362 sizeof(int8_t); // Pointer Size (in bytes)
2363 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
2364 return;
2365 }
Evan Cheng3e288912009-02-25 07:04:34 +00002366 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2367 CompileUnit *Unit = CompileUnits[i];
Devang Patel6eae2832009-01-12 23:05:55 +00002368 // Compute size of compile unit header
2369 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2370 sizeof(int16_t) + // DWARF version number
2371 sizeof(int32_t) + // Offset Into Abbrev. Section
2372 sizeof(int8_t); // Pointer Size (in bytes)
2373 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2374 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002375 }
2376
Evan Cheng3e288912009-02-25 07:04:34 +00002377 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002378 ///
Evan Cheng3e288912009-02-25 07:04:34 +00002379 void EmitDebugInfoPerCU(CompileUnit *Unit) {
2380 DIE *Die = Unit->getDie();
2381 // Emit the compile units header.
2382 EmitLabel("info_begin", Unit->getID());
2383 // Emit size of content not including length itself
2384 unsigned ContentSize = Die->getSize() +
2385 sizeof(int16_t) + // DWARF version number
2386 sizeof(int32_t) + // Offset Into Abbrev. Section
2387 sizeof(int8_t) + // Pointer Size (in bytes)
2388 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2389
2390 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2391 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2392 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2393 Asm->EOL("Offset Into Abbrev. Section");
2394 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2395
2396 EmitDIE(Die);
2397 // FIXME - extra padding for gdb bug.
2398 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2399 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2400 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2401 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2402 EmitLabel("info_end", Unit->getID());
2403
2404 Asm->EOL();
2405 }
2406
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002407 void EmitDebugInfo() {
2408 // Start debug info section.
2409 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
aslc200b112008-08-16 12:57:46 +00002410
Evan Cheng3e288912009-02-25 07:04:34 +00002411 if (MainCU) {
2412 EmitDebugInfoPerCU(MainCU);
2413 return;
Devang Patel6eae2832009-01-12 23:05:55 +00002414 }
Evan Cheng3e288912009-02-25 07:04:34 +00002415
2416 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2417 EmitDebugInfoPerCU(CompileUnits[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002418 }
2419
2420 /// EmitAbbreviations - Emit the abbreviation section.
2421 ///
2422 void EmitAbbreviations() const {
2423 // Check to see if it is worth the effort.
2424 if (!Abbreviations.empty()) {
2425 // Start the debug abbrev section.
2426 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
aslc200b112008-08-16 12:57:46 +00002427
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002428 EmitLabel("abbrev_begin", 0);
aslc200b112008-08-16 12:57:46 +00002429
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002430 // For each abbrevation.
2431 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2432 // Get abbreviation data
2433 const DIEAbbrev *Abbrev = Abbreviations[i];
aslc200b112008-08-16 12:57:46 +00002434
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002435 // Emit the abbrevations code (base 1 index.)
2436 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2437 Asm->EOL("Abbreviation Code");
aslc200b112008-08-16 12:57:46 +00002438
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002439 // Emit the abbreviations data.
2440 Abbrev->Emit(*this);
aslc200b112008-08-16 12:57:46 +00002441
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002442 Asm->EOL();
2443 }
aslc200b112008-08-16 12:57:46 +00002444
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002445 // Mark end of abbreviations.
2446 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2447
2448 EmitLabel("abbrev_end", 0);
aslc200b112008-08-16 12:57:46 +00002449
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002450 Asm->EOL();
2451 }
2452 }
2453
Bill Wendling1983a2a2008-07-20 00:11:19 +00002454 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2455 /// the line matrix.
aslc200b112008-08-16 12:57:46 +00002456 ///
Bill Wendling1983a2a2008-07-20 00:11:19 +00002457 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2458 // Define last address of section.
2459 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2460 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2461 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2462 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2463
2464 // Mark end of matrix.
2465 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2466 Asm->EmitULEB128Bytes(1); Asm->EOL();
2467 Asm->EmitInt8(1); Asm->EOL();
2468 }
2469
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002470 /// EmitDebugLines - Emit source line information.
2471 ///
2472 void EmitDebugLines() {
Bill Wendling1983a2a2008-07-20 00:11:19 +00002473 // If the target is using .loc/.file, the assembler will be emitting the
2474 // .debug_line table automatically.
2475 if (TAI->hasDotLocAndDotFile())
Dan Gohmanc55b34a2007-09-24 21:43:52 +00002476 return;
2477
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002478 // Minimum line delta, thus ranging from -10..(255-10).
2479 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2480 // Maximum line delta, thus ranging from -10..(255-10).
2481 const int MaxLineDelta = 255 + MinLineDelta;
2482
2483 // Start the dwarf line section.
2484 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
aslc200b112008-08-16 12:57:46 +00002485
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002486 // Construct the section header.
aslc200b112008-08-16 12:57:46 +00002487
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002488 EmitDifference("line_end", 0, "line_begin", 0, true);
2489 Asm->EOL("Length of Source Line Info");
2490 EmitLabel("line_begin", 0);
aslc200b112008-08-16 12:57:46 +00002491
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002492 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
aslc200b112008-08-16 12:57:46 +00002493
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002494 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2495 Asm->EOL("Prolog Length");
2496 EmitLabel("line_prolog_begin", 0);
aslc200b112008-08-16 12:57:46 +00002497
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002498 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2499
2500 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2501
2502 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
aslc200b112008-08-16 12:57:46 +00002503
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002504 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2505
2506 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
aslc200b112008-08-16 12:57:46 +00002507
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002508 // Line number standard opcode encodings argument count
2509 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2510 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2511 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2512 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2513 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2514 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2515 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2516 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2517 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2518
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002519 // Emit directories.
Evan Cheng3e288912009-02-25 07:04:34 +00002520 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2521 Asm->EmitString(getSourceDirectoryName(DI));
2522 Asm->EOL("Directory");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002523 }
2524 Asm->EmitInt8(0); Asm->EOL("End of directories");
aslc200b112008-08-16 12:57:46 +00002525
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002526 // Emit files.
Evan Cheng3e288912009-02-25 07:04:34 +00002527 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2528 // Remember source id starts at 1.
Bill Wendlingdf25fd62009-03-10 21:59:25 +00002529 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Evan Cheng3e288912009-02-25 07:04:34 +00002530 Asm->EmitString(getSourceFileName(Id.second));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002531 Asm->EOL("Source");
Evan Cheng3e288912009-02-25 07:04:34 +00002532 Asm->EmitULEB128Bytes(Id.first);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002533 Asm->EOL("Directory #");
2534 Asm->EmitULEB128Bytes(0);
2535 Asm->EOL("Mod date");
2536 Asm->EmitULEB128Bytes(0);
2537 Asm->EOL("File size");
2538 }
2539 Asm->EmitInt8(0); Asm->EOL("End of files");
aslc200b112008-08-16 12:57:46 +00002540
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002541 EmitLabel("line_prolog_end", 0);
aslc200b112008-08-16 12:57:46 +00002542
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002543 // A sequence for each text section.
Bill Wendling1983a2a2008-07-20 00:11:19 +00002544 unsigned SecSrcLinesSize = SectionSourceLines.size();
2545
2546 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002547 // Isolate current sections line info.
Devang Patel35a078f2009-01-12 22:54:42 +00002548 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng0eeed442008-07-01 23:18:29 +00002549
Evan Cheng42ceb472009-03-25 01:47:28 +00002550 if (Asm->isVerbose()) {
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002551 const Section* S = SectionMap[j + 1];
Evan Cheng3e288912009-02-25 07:04:34 +00002552 O << '\t' << TAI->getCommentString() << " Section"
2553 << S->getName() << '\n';
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002554 } else
Evan Cheng0eeed442008-07-01 23:18:29 +00002555 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002556
2557 // Dwarf assumes we start with first line of first source file.
2558 unsigned Source = 1;
2559 unsigned Line = 1;
aslc200b112008-08-16 12:57:46 +00002560
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002561 // Construct rows of the address, source, line, column matrix.
2562 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patel35a078f2009-01-12 22:54:42 +00002563 const SrcLineInfo &LineInfo = LineInfos[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002564 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2565 if (!LabelID) continue;
aslc200b112008-08-16 12:57:46 +00002566
Evan Cheng42ceb472009-03-25 01:47:28 +00002567 if (!Asm->isVerbose())
Evan Cheng0eeed442008-07-01 23:18:29 +00002568 Asm->EOL();
Evan Cheng3e288912009-02-25 07:04:34 +00002569 else {
2570 std::pair<unsigned, unsigned> SourceID =
Bill Wendlingdf25fd62009-03-10 21:59:25 +00002571 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Evan Cheng3e288912009-02-25 07:04:34 +00002572 O << '\t' << TAI->getCommentString() << ' '
2573 << getSourceDirectoryName(SourceID.first) << ' '
2574 << getSourceFileName(SourceID.second)
2575 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2576 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002577
2578 // Define the line address.
2579 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohmancfb72b22007-09-27 23:12:31 +00002580 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002581 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2582 EmitReference("label", LabelID); Asm->EOL("Location label");
aslc200b112008-08-16 12:57:46 +00002583
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002584 // If change of source, then switch to the new source.
2585 if (Source != LineInfo.getSourceID()) {
2586 Source = LineInfo.getSourceID();
2587 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2588 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2589 }
aslc200b112008-08-16 12:57:46 +00002590
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002591 // If change of line.
2592 if (Line != LineInfo.getLine()) {
2593 // Determine offset.
2594 int Offset = LineInfo.getLine() - Line;
2595 int Delta = Offset - MinLineDelta;
aslc200b112008-08-16 12:57:46 +00002596
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002597 // Update line.
2598 Line = LineInfo.getLine();
aslc200b112008-08-16 12:57:46 +00002599
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002600 // If delta is small enough and in range...
2601 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2602 // ... then use fast opcode.
2603 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2604 } else {
2605 // ... otherwise use long hand.
2606 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2607 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2608 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2609 }
2610 } else {
2611 // Copy the previous row (different address or source)
2612 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2613 }
2614 }
2615
Bill Wendling1983a2a2008-07-20 00:11:19 +00002616 EmitEndOfLineMatrix(j + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002617 }
Bill Wendling1983a2a2008-07-20 00:11:19 +00002618
2619 if (SecSrcLinesSize == 0)
2620 // Because we're emitting a debug_line section, we still need a line
2621 // table. The linker and friends expect it to exist. If there's nothing to
2622 // put into it, emit an empty table.
2623 EmitEndOfLineMatrix(1);
aslc200b112008-08-16 12:57:46 +00002624
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002625 EmitLabel("line_end", 0);
aslc200b112008-08-16 12:57:46 +00002626
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002627 Asm->EOL();
2628 }
aslc200b112008-08-16 12:57:46 +00002629
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002630 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2631 ///
2632 void EmitCommonDebugFrame() {
2633 if (!TAI->doesDwarfRequireFrameSection())
2634 return;
2635
2636 int stackGrowth =
2637 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2638 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00002639 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002640
2641 // Start the dwarf frame section.
2642 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2643
2644 EmitLabel("debug_frame_common", 0);
2645 EmitDifference("debug_frame_common_end", 0,
2646 "debug_frame_common_begin", 0, true);
2647 Asm->EOL("Length of Common Information Entry");
2648
2649 EmitLabel("debug_frame_common_begin", 0);
2650 Asm->EmitInt32((int)DW_CIE_ID);
2651 Asm->EOL("CIE Identifier Tag");
2652 Asm->EmitInt8(DW_CIE_VERSION);
2653 Asm->EOL("CIE Version");
2654 Asm->EmitString("");
2655 Asm->EOL("CIE Augmentation");
2656 Asm->EmitULEB128Bytes(1);
2657 Asm->EOL("CIE Code Alignment Factor");
2658 Asm->EmitSLEB128Bytes(stackGrowth);
aslc200b112008-08-16 12:57:46 +00002659 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00002660 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002661 Asm->EOL("CIE RA Column");
aslc200b112008-08-16 12:57:46 +00002662
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002663 std::vector<MachineMove> Moves;
2664 RI->getInitialFrameState(Moves);
2665
Dale Johannesenf5a11532007-11-13 19:13:01 +00002666 EmitFrameMoves(NULL, 0, Moves, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002667
Evan Cheng7e7d1942008-02-29 19:36:59 +00002668 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002669 EmitLabel("debug_frame_common_end", 0);
aslc200b112008-08-16 12:57:46 +00002670
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002671 Asm->EOL();
2672 }
2673
2674 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2675 /// section.
2676 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
2677 if (!TAI->doesDwarfRequireFrameSection())
2678 return;
aslc200b112008-08-16 12:57:46 +00002679
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002680 // Start the dwarf frame section.
2681 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
aslc200b112008-08-16 12:57:46 +00002682
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002683 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2684 "debug_frame_begin", DebugFrameInfo.Number, true);
2685 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00002686
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002687 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2688
2689 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2690 0, 0, true, false);
2691 Asm->EOL("FDE CIE offset");
2692
2693 EmitReference("func_begin", DebugFrameInfo.Number);
2694 Asm->EOL("FDE initial location");
2695 EmitDifference("func_end", DebugFrameInfo.Number,
2696 "func_begin", DebugFrameInfo.Number);
2697 Asm->EOL("FDE address range");
aslc200b112008-08-16 12:57:46 +00002698
Devang Patelb28de842009-01-17 08:01:33 +00002699 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00002700 false);
aslc200b112008-08-16 12:57:46 +00002701
Evan Cheng7e7d1942008-02-29 19:36:59 +00002702 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002703 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2704
2705 Asm->EOL();
2706 }
2707
Evan Cheng3e288912009-02-25 07:04:34 +00002708 void EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2709 EmitDifference("pubnames_end", Unit->getID(),
2710 "pubnames_begin", Unit->getID(), true);
2711 Asm->EOL("Length of Public Names Info");
2712
2713 EmitLabel("pubnames_begin", Unit->getID());
2714
2715 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2716
2717 EmitSectionOffset("info_begin", "section_info",
2718 Unit->getID(), 0, true, false);
2719 Asm->EOL("Offset of Compilation Unit Info");
2720
2721 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2722 true);
2723 Asm->EOL("Compilation Unit Length");
2724
Bill Wendlinge06da442009-04-09 21:49:15 +00002725 StringMap<DIE*> &Globals = Unit->getGlobals();
Bill Wendling3f94b412009-04-09 23:51:31 +00002726 for (StringMap<DIE*>::const_iterator
Bill Wendlinge06da442009-04-09 21:49:15 +00002727 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
Bill Wendling3f94b412009-04-09 23:51:31 +00002728 const char *Name = GI->getKeyData();
Evan Cheng3e288912009-02-25 07:04:34 +00002729 DIE * Entity = GI->second;
2730
2731 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
Bill Wendling3f94b412009-04-09 23:51:31 +00002732 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
Evan Cheng3e288912009-02-25 07:04:34 +00002733 }
2734
2735 Asm->EmitInt32(0); Asm->EOL("End Mark");
2736 EmitLabel("pubnames_end", Unit->getID());
2737
2738 Asm->EOL();
2739 }
2740
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002741 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2742 ///
2743 void EmitDebugPubNames() {
2744 // Start the dwarf pubnames section.
2745 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
aslc200b112008-08-16 12:57:46 +00002746
Evan Cheng3e288912009-02-25 07:04:34 +00002747 if (MainCU) {
2748 EmitDebugPubNamesPerCU(MainCU);
2749 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002750 }
Evan Cheng3e288912009-02-25 07:04:34 +00002751
2752 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2753 EmitDebugPubNamesPerCU(CompileUnits[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002754 }
2755
2756 /// EmitDebugStr - Emit visible names into a debug str section.
2757 ///
2758 void EmitDebugStr() {
2759 // Check to see if it is worth the effort.
2760 if (!StringPool.empty()) {
2761 // Start the dwarf str section.
2762 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
aslc200b112008-08-16 12:57:46 +00002763
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002764 // For each of strings in the string pool.
2765 for (unsigned StringID = 1, N = StringPool.size();
2766 StringID <= N; ++StringID) {
2767 // Emit a label for reference from debug information entries.
2768 EmitLabel("string", StringID);
2769 // Emit the string itself.
2770 const std::string &String = StringPool[StringID];
2771 Asm->EmitString(String); Asm->EOL();
2772 }
aslc200b112008-08-16 12:57:46 +00002773
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002774 Asm->EOL();
2775 }
2776 }
2777
2778 /// EmitDebugLoc - Emit visible names into a debug loc section.
2779 ///
2780 void EmitDebugLoc() {
2781 // Start the dwarf loc section.
2782 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
aslc200b112008-08-16 12:57:46 +00002783
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002784 Asm->EOL();
2785 }
2786
2787 /// EmitDebugARanges - Emit visible names into a debug aranges section.
2788 ///
2789 void EmitDebugARanges() {
2790 // Start the dwarf aranges section.
2791 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
aslc200b112008-08-16 12:57:46 +00002792
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002793 // FIXME - Mock up
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002794#if 0
aslc200b112008-08-16 12:57:46 +00002795 CompileUnit *Unit = GetBaseCompileUnit();
2796
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002797 // Don't include size of length
2798 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
aslc200b112008-08-16 12:57:46 +00002799
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002800 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
aslc200b112008-08-16 12:57:46 +00002801
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002802 EmitReference("info_begin", Unit->getID());
2803 Asm->EOL("Offset of Compilation Unit Info");
2804
Dan Gohmancfb72b22007-09-27 23:12:31 +00002805 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002806
2807 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2808
2809 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2810 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2811
2812 // Range 1
2813 EmitReference("text_begin", 0); Asm->EOL("Address");
2814 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2815
2816 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2817 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002818#endif
aslc200b112008-08-16 12:57:46 +00002819
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002820 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002821 }
2822
2823 /// EmitDebugRanges - Emit visible names into a debug ranges section.
2824 ///
2825 void EmitDebugRanges() {
2826 // Start the dwarf ranges section.
2827 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
aslc200b112008-08-16 12:57:46 +00002828
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002829 Asm->EOL();
2830 }
2831
2832 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2833 ///
2834 void EmitDebugMacInfo() {
Scott Michel79f01f52009-01-26 22:32:51 +00002835 if (TAI->doesSupportMacInfoSection()) {
2836 // Start the dwarf macinfo section.
2837 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
aslc200b112008-08-16 12:57:46 +00002838
Scott Michel79f01f52009-01-26 22:32:51 +00002839 Asm->EOL();
2840 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002841 }
2842
Devang Patel88bf96e2009-04-13 17:02:03 +00002843 /// EmitDebugInlineInfo - Emit inline info using following format.
2844 /// Section Header:
2845 /// 1. length of section
2846 /// 2. Dwarf version number
2847 /// 3. address size.
2848 ///
2849 /// Entries (one "entry" for each function that was inlined):
2850 ///
2851 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
2852 /// otherwise offset into __debug_str for regular function name.
2853 /// 2. offset into __debug_str section for regular function name.
2854 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
2855 /// instances for the function.
2856 ///
2857 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
2858 /// inlined instance; the die_offset points to the inlined_subroutine die in
2859 /// the __debug_info section, and the low_pc is the starting address for the
2860 /// inlining instance.
2861 void EmitDebugInlineInfo() {
2862 if (!TAI->doesDwarfUsesInlineInfoSection())
2863 return;
2864
2865 if (!MainCU)
2866 return;
2867
2868 Asm->SwitchToDataSection(TAI->getDwarfDebugInlineSection());
2869 Asm->EOL();
2870 EmitDifference("debug_inlined_end", 1,
2871 "debug_inlined_begin", 1, true);
2872 Asm->EOL("Length of Debug Inlined Information Entry");
2873
2874 EmitLabel("debug_inlined_begin", 1);
2875
2876 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
2877 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2878
2879 for (DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
2880 I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2881 GlobalVariable *GV = I->first;
2882 SmallVector<unsigned, 4> &Labels = I->second;
2883 DISubprogram SP(GV);
2884 std::string Name;
2885 std::string LName;
2886
2887 SP.getLinkageName(LName);
2888 SP.getName(Name);
2889
2890 Asm->EmitString(LName.empty() ? Name : LName);
2891 Asm->EOL("MIPS linkage name");
2892
2893 Asm->EmitString(Name); Asm->EOL("Function name");
2894
2895 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2896
2897 for (SmallVector<unsigned, 4>::iterator LI = Labels.begin(),
2898 LE = Labels.end(); LI != LE; ++LI) {
2899 DIE *SP = MainCU->getDieMapSlotFor(GV);
2900 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2901
2902 if (TD->getPointerSize() == sizeof(int32_t))
2903 O << TAI->getData32bitsDirective();
2904 else
2905 O << TAI->getData64bitsDirective();
2906 PrintLabelName("label", *LI); Asm->EOL("low_pc");
2907 }
2908 }
2909
2910 EmitLabel("debug_inlined_end", 1);
2911 Asm->EOL();
2912 }
2913
Bill Wendling278a3922009-03-10 21:47:45 +00002914 /// GetOrCreateSourceID - Look up the source id with the given directory and
2915 /// source file names. If none currently exists, create a new id and insert it
2916 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
2917 /// as well.
2918 unsigned GetOrCreateSourceID(const std::string &DirName,
2919 const std::string &FileName) {
2920 unsigned DId;
2921 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
2922 if (DI != DirectoryIdMap.end()) {
2923 DId = DI->getValue();
2924 } else {
2925 DId = DirectoryNames.size() + 1;
2926 DirectoryIdMap[DirName] = DId;
2927 DirectoryNames.push_back(DirName);
2928 }
2929
2930 unsigned FId;
2931 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
2932 if (FI != SourceFileIdMap.end()) {
2933 FId = FI->getValue();
2934 } else {
2935 FId = SourceFileNames.size() + 1;
2936 SourceFileIdMap[FileName] = FId;
2937 SourceFileNames.push_back(FileName);
2938 }
2939
2940 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
2941 SourceIdMap.find(std::make_pair(DId, FId));
2942 if (SI != SourceIdMap.end())
2943 return SI->second;
2944
2945 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
2946 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
2947 SourceIds.push_back(std::make_pair(DId, FId));
2948
2949 return SrcId;
2950 }
2951
Evan Cheng3e288912009-02-25 07:04:34 +00002952 void ConstructCompileUnit(GlobalVariable *GV) {
2953 DICompileUnit DIUnit(GV);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00002954 std::string Dir, FN, Prod;
2955 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
2956 DIUnit.getFilename(FN));
Evan Cheng3e288912009-02-25 07:04:34 +00002957
2958 DIE *Die = new DIE(DW_TAG_compile_unit);
2959 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2960 DWLabel("section_line", 0), DWLabel("section_line", 0),
2961 false);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00002962 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer(Prod));
Evan Cheng3e288912009-02-25 07:04:34 +00002963 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendling1c5842b2009-03-09 05:04:40 +00002964 AddString(Die, DW_AT_name, DW_FORM_string, FN);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00002965 if (!Dir.empty())
Bill Wendling1c5842b2009-03-09 05:04:40 +00002966 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Cheng3e288912009-02-25 07:04:34 +00002967 if (DIUnit.isOptimized())
2968 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00002969 std::string Flags;
2970 DIUnit.getFlags(Flags);
2971 if (!Flags.empty())
Evan Cheng3e288912009-02-25 07:04:34 +00002972 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
2973 unsigned RVer = DIUnit.getRunTimeVersion();
2974 if (RVer)
2975 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
2976
2977 CompileUnit *Unit = new CompileUnit(ID, Die);
2978 if (DIUnit.isMain()) {
2979 assert(!MainCU && "Multiple main compile units are found!");
2980 MainCU = Unit;
2981 }
2982 CompileUnitMap[DIUnit.getGV()] = Unit;
2983 CompileUnits.push_back(Unit);
2984 }
2985
Devang Patel289f2362009-01-05 23:11:11 +00002986 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Patelb3907da2009-01-05 23:03:32 +00002987 void ConstructCompileUnits() {
Evan Cheng3e288912009-02-25 07:04:34 +00002988 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.compile_units");
2989 if (!Root)
2990 return;
2991 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2992 "Malformed compile unit descriptor anchor type");
2993 Constant *RootC = cast<Constant>(*Root->use_begin());
2994 assert(RootC->hasNUsesOrMore(1) &&
2995 "Malformed compile unit descriptor anchor type");
2996 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2997 UI != UE; ++UI)
2998 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2999 UUI != UUE; ++UUI) {
3000 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3001 ConstructCompileUnit(GV);
Devang Patel2ae1db52009-01-30 18:20:31 +00003002 }
Evan Cheng3e288912009-02-25 07:04:34 +00003003 }
3004
3005 bool ConstructGlobalVariableDIE(GlobalVariable *GV) {
3006 DIGlobalVariable DI_GV(GV);
3007 CompileUnit *DW_Unit = MainCU;
3008 if (!DW_Unit)
3009 DW_Unit = FindCompileUnit(DI_GV.getCompileUnit());
3010
3011 // Check for pre-existence.
3012 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
3013 if (Slot)
3014 return false;
3015
3016 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
3017
3018 // Add address.
3019 DIEBlock *Block = new DIEBlock();
3020 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Bill Wendling26a8ab92009-04-10 00:12:49 +00003021 std::string GLN;
Evan Cheng3e288912009-02-25 07:04:34 +00003022 AddObjectLabel(Block, 0, DW_FORM_udata,
Bill Wendling26a8ab92009-04-10 00:12:49 +00003023 Asm->getGlobalLinkName(DI_GV.getGlobal(), GLN));
Evan Cheng3e288912009-02-25 07:04:34 +00003024 AddBlock(VariableDie, DW_AT_location, 0, Block);
3025
3026 // Add to map.
3027 Slot = VariableDie;
3028 // Add to context owner.
3029 DW_Unit->getDie()->AddChild(VariableDie);
3030 // Expose as global. FIXME - need to check external flag.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00003031 std::string Name;
3032 DW_Unit->AddGlobal(DI_GV.getName(Name), VariableDie);
Evan Cheng3e288912009-02-25 07:04:34 +00003033 return true;
Devang Patelb3907da2009-01-05 23:03:32 +00003034 }
3035
Devang Patel289f2362009-01-05 23:11:11 +00003036 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patela9169c32009-02-24 00:02:15 +00003037 /// visible global variables. Return true if at least one global DIE is
3038 /// created.
3039 bool ConstructGlobalVariableDIEs() {
Evan Cheng3e288912009-02-25 07:04:34 +00003040 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
3041 if (!Root)
3042 return false;
Devang Patel289f2362009-01-05 23:11:11 +00003043
Evan Cheng3e288912009-02-25 07:04:34 +00003044 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3045 "Malformed global variable descriptor anchor type");
3046 Constant *RootC = cast<Constant>(*Root->use_begin());
3047 assert(RootC->hasNUsesOrMore(1) &&
3048 "Malformed global variable descriptor anchor type");
Devang Patel289f2362009-01-05 23:11:11 +00003049
Evan Cheng3e288912009-02-25 07:04:34 +00003050 bool Result = false;
3051 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3052 UI != UE; ++UI)
3053 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3054 UUI != UUE; ++UUI) {
3055 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3056 Result |= ConstructGlobalVariableDIE(GV);
3057 }
3058 return Result;
3059 }
Devang Patel289f2362009-01-05 23:11:11 +00003060
Evan Cheng3e288912009-02-25 07:04:34 +00003061 bool ConstructSubprogram(GlobalVariable *GV) {
3062 DISubprogram SP(GV);
3063 CompileUnit *Unit = MainCU;
3064 if (!Unit)
3065 Unit = FindCompileUnit(SP.getCompileUnit());
Devang Patel289f2362009-01-05 23:11:11 +00003066
Evan Cheng3e288912009-02-25 07:04:34 +00003067 // Check for pre-existence.
3068 DIE *&Slot = Unit->getDieMapSlotFor(GV);
3069 if (Slot)
3070 return false;
3071
3072 if (!SP.isDefinition())
3073 // This is a method declaration which will be handled while
3074 // constructing class type.
3075 return false;
3076
3077 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
3078
3079 // Add to map.
3080 Slot = SubprogramDie;
3081 // Add to context owner.
3082 Unit->getDie()->AddChild(SubprogramDie);
3083 // Expose as global.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00003084 std::string Name;
3085 Unit->AddGlobal(SP.getName(Name), SubprogramDie);
Evan Cheng3e288912009-02-25 07:04:34 +00003086 return true;
Devang Patel289f2362009-01-05 23:11:11 +00003087 }
3088
Devang Patele6caf012009-01-05 23:21:35 +00003089 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patela9169c32009-02-24 00:02:15 +00003090 /// subprograms. Return true if at least one subprogram DIE is created.
3091 bool ConstructSubprograms() {
Evan Cheng3e288912009-02-25 07:04:34 +00003092 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
3093 if (!Root)
3094 return false;
Devang Patele6caf012009-01-05 23:21:35 +00003095
Evan Cheng3e288912009-02-25 07:04:34 +00003096 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3097 "Malformed subprogram descriptor anchor type");
3098 Constant *RootC = cast<Constant>(*Root->use_begin());
3099 assert(RootC->hasNUsesOrMore(1) &&
3100 "Malformed subprogram descriptor anchor type");
Devang Patele6caf012009-01-05 23:21:35 +00003101
Evan Cheng3e288912009-02-25 07:04:34 +00003102 bool Result = false;
3103 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3104 UI != UE; ++UI)
3105 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3106 UUI != UUE; ++UUI) {
3107 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3108 Result |= ConstructSubprogram(GV);
3109 }
3110 return Result;
Devang Patele6caf012009-01-05 23:21:35 +00003111 }
3112
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003113public:
3114 //===--------------------------------------------------------------------===//
3115 // Main entry points.
3116 //
Owen Anderson847b99b2008-08-21 00:14:44 +00003117 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendlingd9308a62009-03-10 21:23:25 +00003118 : Dwarf(OS, A, T, "dbg"), MainCU(0),
3119 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
3120 ValuesSet(InitValuesSetSize), Values(), StringPool(), SectionMap(),
3121 SectionSourceLines(), didInitial(false), shouldEmit(false),
3122 RootDbgScope(0), DebugTimer(0) {
3123 if (TimePassesIsEnabled)
3124 DebugTimer = new Timer("Dwarf Debug Writer",
Bill Wendling148ecc42009-03-10 22:58:53 +00003125 getDwarfTimerGroup());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003126 }
3127 virtual ~DwarfDebug() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003128 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3129 delete Values[j];
Bill Wendlingd9308a62009-03-10 21:23:25 +00003130
3131 delete DebugTimer;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003132 }
3133
Bill Wendling278a3922009-03-10 21:47:45 +00003134 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
3135 /// be emitted.
3136 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
3137
Devang Patel9304b382009-01-06 21:07:30 +00003138 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3139 /// This is inovked by the target AsmPrinter.
Devang Patel91d27b02009-01-12 23:09:42 +00003140 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003141 if (TimePassesIsEnabled)
3142 DebugTimer->startTimer();
3143
Bill Wendling6baa18d2009-02-03 21:38:21 +00003144 // Create all the compile unit DIEs.
3145 ConstructCompileUnits();
Devang Patel91d27b02009-01-12 23:09:42 +00003146
Bill Wendlingd9308a62009-03-10 21:23:25 +00003147 if (CompileUnits.empty()) {
3148 if (TimePassesIsEnabled)
Bill Wendling0be24752009-03-10 22:02:13 +00003149 DebugTimer->stopTimer();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003150
Bill Wendling6baa18d2009-02-03 21:38:21 +00003151 return;
Bill Wendlingd9308a62009-03-10 21:23:25 +00003152 }
Devang Patel91d27b02009-01-12 23:09:42 +00003153
Devang Patela9169c32009-02-24 00:02:15 +00003154 // Create DIEs for each of the externally visible global variables.
3155 bool globalDIEs = ConstructGlobalVariableDIEs();
3156
3157 // Create DIEs for each of the externally visible subprograms.
3158 bool subprogramDIEs = ConstructSubprograms();
3159
3160 // If there is not any debug info available for any global variables
3161 // and any subprograms then there is not any debug info to emit.
Bill Wendlingd9308a62009-03-10 21:23:25 +00003162 if (!globalDIEs && !subprogramDIEs) {
3163 if (TimePassesIsEnabled)
Bill Wendling0be24752009-03-10 22:02:13 +00003164 DebugTimer->stopTimer();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003165
Devang Patela9169c32009-02-24 00:02:15 +00003166 return;
Bill Wendlingd9308a62009-03-10 21:23:25 +00003167 }
Devang Patela9169c32009-02-24 00:02:15 +00003168
Bill Wendling6baa18d2009-02-03 21:38:21 +00003169 MMI = mmi;
3170 shouldEmit = true;
3171 MMI->setDebugInfoAvailability(true);
Devang Patel9304b382009-01-06 21:07:30 +00003172
Bill Wendling6baa18d2009-02-03 21:38:21 +00003173 // Prime section data.
3174 SectionMap.insert(TAI->getTextSection());
Devang Patel9304b382009-01-06 21:07:30 +00003175
Bill Wendling6baa18d2009-02-03 21:38:21 +00003176 // Print out .file directives to specify files for .loc directives. These
3177 // are printed out early so that they precede any .loc directives.
3178 if (TAI->hasDotLocAndDotFile()) {
Evan Cheng3e288912009-02-25 07:04:34 +00003179 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
3180 // Remember source id starts at 1.
Bill Wendlingdf25fd62009-03-10 21:59:25 +00003181 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Evan Cheng3e288912009-02-25 07:04:34 +00003182 sys::Path FullPath(getSourceDirectoryName(Id.first));
3183 bool AppendOk =
3184 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendling6baa18d2009-02-03 21:38:21 +00003185 assert(AppendOk && "Could not append filename to directory!");
3186 AppendOk = false;
3187 Asm->EmitFile(i, FullPath.toString());
3188 Asm->EOL();
Devang Patel9304b382009-01-06 21:07:30 +00003189 }
Bill Wendling6baa18d2009-02-03 21:38:21 +00003190 }
Devang Patel9304b382009-01-06 21:07:30 +00003191
Bill Wendling6baa18d2009-02-03 21:38:21 +00003192 // Emit initial sections
3193 EmitInitial();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003194
3195 if (TimePassesIsEnabled)
3196 DebugTimer->stopTimer();
Devang Patel9304b382009-01-06 21:07:30 +00003197 }
3198
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003199 /// BeginModule - Emit all Dwarf sections that should come prior to the
3200 /// content.
3201 void BeginModule(Module *M) {
3202 this->M = M;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003203 }
3204
3205 /// EndModule - Emit all Dwarf sections that should come after the content.
3206 ///
3207 void EndModule() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003208 if (!ShouldEmitDwarfDebug())
3209 return;
3210
3211 if (TimePassesIsEnabled)
3212 DebugTimer->startTimer();
aslc200b112008-08-16 12:57:46 +00003213
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003214 // Standard sections final addresses.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003215 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003216 EmitLabel("text_end", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00003217 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003218 EmitLabel("data_end", 0);
aslc200b112008-08-16 12:57:46 +00003219
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003220 // End text sections.
3221 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003222 Asm->SwitchToSection(SectionMap[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003223 EmitLabel("section_end", i);
3224 }
3225
3226 // Emit common frame information.
3227 EmitCommonDebugFrame();
3228
3229 // Emit function debug frame information
3230 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3231 E = DebugFrames.end(); I != E; ++I)
3232 EmitFunctionDebugFrame(*I);
3233
3234 // Compute DIE offsets and sizes.
3235 SizeAndOffsets();
aslc200b112008-08-16 12:57:46 +00003236
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003237 // Emit all the DIEs into a debug info section
3238 EmitDebugInfo();
aslc200b112008-08-16 12:57:46 +00003239
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003240 // Corresponding abbreviations into a abbrev section.
3241 EmitAbbreviations();
aslc200b112008-08-16 12:57:46 +00003242
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003243 // Emit source line correspondence into a debug line section.
3244 EmitDebugLines();
aslc200b112008-08-16 12:57:46 +00003245
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003246 // Emit info into a debug pubnames section.
3247 EmitDebugPubNames();
aslc200b112008-08-16 12:57:46 +00003248
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003249 // Emit info into a debug str section.
3250 EmitDebugStr();
aslc200b112008-08-16 12:57:46 +00003251
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003252 // Emit info into a debug loc section.
3253 EmitDebugLoc();
aslc200b112008-08-16 12:57:46 +00003254
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003255 // Emit info into a debug aranges section.
3256 EmitDebugARanges();
aslc200b112008-08-16 12:57:46 +00003257
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003258 // Emit info into a debug ranges section.
3259 EmitDebugRanges();
aslc200b112008-08-16 12:57:46 +00003260
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003261 // Emit info into a debug macinfo section.
3262 EmitDebugMacInfo();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003263
Devang Patel88bf96e2009-04-13 17:02:03 +00003264 // Emit inline info.
3265 EmitDebugInlineInfo();
3266
Bill Wendlingd9308a62009-03-10 21:23:25 +00003267 if (TimePassesIsEnabled)
3268 DebugTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003269 }
3270
aslc200b112008-08-16 12:57:46 +00003271 /// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003272 /// emitted immediately after the function entry point.
3273 void BeginFunction(MachineFunction *MF) {
Bill Wendlingc1d211d2009-03-11 00:03:50 +00003274 this->MF = MF;
3275
Bill Wendling50db0792009-02-20 00:44:43 +00003276 if (!ShouldEmitDwarfDebug()) return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003277
Bill Wendlingd9308a62009-03-10 21:23:25 +00003278 if (TimePassesIsEnabled)
3279 DebugTimer->startTimer();
3280
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003281 // Begin accumulating function debug information.
3282 MMI->BeginFunction(MF);
aslc200b112008-08-16 12:57:46 +00003283
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003284 // Assumes in correct section after the entry point.
3285 EmitLabel("func_begin", ++SubprogramCount);
Evan Chenga53c40a2008-02-01 09:10:45 +00003286
3287 // Emit label for the implicitly defined dbg.stoppoint at the start of
3288 // the function.
Devang Patel35a078f2009-01-12 22:54:42 +00003289 if (!Lines.empty()) {
3290 const SrcLineInfo &LineInfo = Lines[0];
Andrew Lenharth42f91402008-04-03 17:37:43 +00003291 Asm->printLabel(LineInfo.getLabelID());
3292 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00003293
3294 if (TimePassesIsEnabled)
3295 DebugTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003296 }
aslc200b112008-08-16 12:57:46 +00003297
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003298 /// EndFunction - Gather and emit post-function debug information.
3299 ///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003300 void EndFunction(MachineFunction *MF) {
Bill Wendling50db0792009-02-20 00:44:43 +00003301 if (!ShouldEmitDwarfDebug()) return;
aslc200b112008-08-16 12:57:46 +00003302
Bill Wendlingd9308a62009-03-10 21:23:25 +00003303 if (TimePassesIsEnabled)
3304 DebugTimer->startTimer();
3305
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003306 // Define end label for subprogram.
3307 EmitLabel("func_end", SubprogramCount);
aslc200b112008-08-16 12:57:46 +00003308
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003309 // Get function line info.
Devang Patel35a078f2009-01-12 22:54:42 +00003310 if (!Lines.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003311 // Get section line info.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003312 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003313 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patel35a078f2009-01-12 22:54:42 +00003314 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003315 // Append the function info to section info.
3316 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patel35a078f2009-01-12 22:54:42 +00003317 Lines.begin(), Lines.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003318 }
aslc200b112008-08-16 12:57:46 +00003319
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003320 // Construct scopes for subprogram.
Devang Patel6ccd57e2009-01-13 00:20:51 +00003321 if (RootDbgScope)
3322 ConstructRootDbgScope(RootDbgScope);
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003323 else
3324 // FIXME: This is wrong. We are essentially getting past a problem with
3325 // debug information not being able to handle unreachable blocks that have
3326 // debug information in them. In particular, those unreachable blocks that
3327 // have "region end" info in them. That situation results in the "root
3328 // scope" not being created. If that's the case, then emit a "default"
3329 // scope, i.e., one that encompasses the whole function. This isn't
3330 // desirable. And a better way of handling this (and all of the debugging
3331 // information) needs to be explored.
Devang Patel6ccd57e2009-01-13 00:20:51 +00003332 ConstructDefaultDbgScope(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003333
3334 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3335 MMI->getFrameMoves()));
Devang Patela4162952009-01-12 18:48:36 +00003336
3337 // Clear debug info
3338 if (RootDbgScope) {
3339 delete RootDbgScope;
3340 DbgScopeMap.clear();
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003341 DbgInlinedScopeMap.clear();
3342 InlinedVariableScopes.clear();
Devang Patela4162952009-01-12 18:48:36 +00003343 RootDbgScope = NULL;
3344 }
Devang Patelcb59fd42009-01-12 19:17:34 +00003345
Bill Wendlingd9308a62009-03-10 21:23:25 +00003346 Lines.clear();
3347
3348 if (TimePassesIsEnabled)
3349 DebugTimer->stopTimer();
3350 }
Devang Patelcb59fd42009-01-12 19:17:34 +00003351
Devang Patel2da0cc42009-01-15 23:41:32 +00003352 /// ValidDebugInfo - Return true if V represents valid debug info value.
Devang Patel18992922009-04-13 18:13:16 +00003353 bool ValidDebugInfo(Value *V, bool FastISel) {
Devang Patel208098b2009-01-19 23:21:49 +00003354 if (!V)
3355 return false;
3356
Devang Patel4d92dded2009-01-16 01:49:46 +00003357 if (!shouldEmit)
3358 return false;
3359
Devang Patel2da0cc42009-01-15 23:41:32 +00003360 GlobalVariable *GV = getGlobalVariable(V);
3361 if (!GV)
3362 return false;
Duncan Sands19d161f2009-03-07 15:45:40 +00003363
3364 if (!GV->hasInternalLinkage () && !GV->hasLinkOnceLinkage())
Devang Patel2da0cc42009-01-15 23:41:32 +00003365 return false;
3366
Bill Wendlingd9308a62009-03-10 21:23:25 +00003367 if (TimePassesIsEnabled)
3368 DebugTimer->startTimer();
3369
Devang Patel2da0cc42009-01-15 23:41:32 +00003370 DIDescriptor DI(GV);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003371
Devang Patel2da0cc42009-01-15 23:41:32 +00003372 // Check current version. Allow Version6 for now.
3373 unsigned Version = DI.getVersion();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003374 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6) {
3375 if (TimePassesIsEnabled)
3376 DebugTimer->stopTimer();
3377
Devang Patel2da0cc42009-01-15 23:41:32 +00003378 return false;
Bill Wendlingd9308a62009-03-10 21:23:25 +00003379 }
Devang Patel2da0cc42009-01-15 23:41:32 +00003380
Devang Patel208098b2009-01-19 23:21:49 +00003381 unsigned Tag = DI.getTag();
3382 switch (Tag) {
3383 case DW_TAG_variable:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003384 assert(DIVariable(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003385 break;
3386 case DW_TAG_compile_unit:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003387 assert(DICompileUnit(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003388 break;
3389 case DW_TAG_subprogram:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003390 assert(DISubprogram(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003391 break;
Devang Patel18992922009-04-13 18:13:16 +00003392 case DW_TAG_lexical_block:
3393 /// FIXME. This interfers with the qualitfy of generated code when
3394 /// during optimization.
3395 if (FastISel == false)
3396 return false;
Devang Patel208098b2009-01-19 23:21:49 +00003397 default:
3398 break;
3399 }
3400
Bill Wendlingd9308a62009-03-10 21:23:25 +00003401 if (TimePassesIsEnabled)
3402 DebugTimer->stopTimer();
3403
Devang Patel2da0cc42009-01-15 23:41:32 +00003404 return true;
3405 }
3406
Devang Patelcb59fd42009-01-12 19:17:34 +00003407 /// RecordSourceLine - Records location information and associates it with a
3408 /// label. Returns a unique label ID used to generate a label and provide
3409 /// correspondence to the source line list.
3410 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003411 if (TimePassesIsEnabled)
3412 DebugTimer->startTimer();
3413
Evan Cheng3e288912009-02-25 07:04:34 +00003414 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendling6baa18d2009-02-03 21:38:21 +00003415 assert(Unit && "Unable to find CompileUnit");
Devang Patelcb59fd42009-01-12 19:17:34 +00003416 unsigned ID = MMI->NextLabelID();
3417 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
Bill Wendlingd9308a62009-03-10 21:23:25 +00003418
3419 if (TimePassesIsEnabled)
3420 DebugTimer->stopTimer();
3421
Devang Patelcb59fd42009-01-12 19:17:34 +00003422 return ID;
3423 }
3424
3425 /// RecordSourceLine - Records location information and associates it with a
3426 /// label. Returns a unique label ID used to generate a label and provide
3427 /// correspondence to the source line list.
3428 unsigned RecordSourceLine(unsigned Line, unsigned Col, unsigned Src) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003429 if (TimePassesIsEnabled)
3430 DebugTimer->startTimer();
3431
Devang Patelcb59fd42009-01-12 19:17:34 +00003432 unsigned ID = MMI->NextLabelID();
3433 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
Bill Wendlingd9308a62009-03-10 21:23:25 +00003434
3435 if (TimePassesIsEnabled)
3436 DebugTimer->stopTimer();
3437
Devang Patelcb59fd42009-01-12 19:17:34 +00003438 return ID;
3439 }
3440
Bill Wendling278a3922009-03-10 21:47:45 +00003441 /// getRecordSourceLineCount - Return the number of source lines in the debug
3442 /// info.
3443 unsigned getRecordSourceLineCount() const {
Devang Patelcb59fd42009-01-12 19:17:34 +00003444 return Lines.size();
3445 }
3446
Bill Wendling278a3922009-03-10 21:47:45 +00003447 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
3448 /// timed. Look up the source id with the given directory and source file
3449 /// names. If none currently exists, create a new id and insert it in the
3450 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
3451 /// well.
Evan Cheng3e288912009-02-25 07:04:34 +00003452 unsigned getOrCreateSourceID(const std::string &DirName,
3453 const std::string &FileName) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003454 if (TimePassesIsEnabled)
3455 DebugTimer->startTimer();
3456
Bill Wendling278a3922009-03-10 21:47:45 +00003457 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003458
3459 if (TimePassesIsEnabled)
3460 DebugTimer->stopTimer();
3461
Evan Cheng3e288912009-02-25 07:04:34 +00003462 return SrcId;
Devang Patelcb59fd42009-01-12 19:17:34 +00003463 }
3464
3465 /// RecordRegionStart - Indicate the start of a region.
Devang Patelcb59fd42009-01-12 19:17:34 +00003466 unsigned RecordRegionStart(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003467 if (TimePassesIsEnabled)
3468 DebugTimer->startTimer();
3469
Devang Patelcb59fd42009-01-12 19:17:34 +00003470 DbgScope *Scope = getOrCreateScope(V);
3471 unsigned ID = MMI->NextLabelID();
3472 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003473
3474 if (TimePassesIsEnabled)
3475 DebugTimer->stopTimer();
3476
Devang Patelcb59fd42009-01-12 19:17:34 +00003477 return ID;
3478 }
3479
3480 /// RecordRegionEnd - Indicate the end of a region.
Devang Patelcb59fd42009-01-12 19:17:34 +00003481 unsigned RecordRegionEnd(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003482 if (TimePassesIsEnabled)
3483 DebugTimer->startTimer();
3484
Devang Patelcb59fd42009-01-12 19:17:34 +00003485 DbgScope *Scope = getOrCreateScope(V);
3486 unsigned ID = MMI->NextLabelID();
3487 Scope->setEndLabelID(ID);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003488
3489 if (TimePassesIsEnabled)
3490 DebugTimer->stopTimer();
3491
Devang Patelcb59fd42009-01-12 19:17:34 +00003492 return ID;
3493 }
3494
3495 /// RecordVariable - Indicate the declaration of a local variable.
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003496 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
3497 const MachineInstr *MI) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003498 if (TimePassesIsEnabled)
3499 DebugTimer->startTimer();
3500
Devang Patel2560d922009-01-15 18:25:17 +00003501 DIDescriptor Desc(GV);
3502 DbgScope *Scope = NULL;
Bill Wendlingd9308a62009-03-10 21:23:25 +00003503
Devang Patel2560d922009-01-15 18:25:17 +00003504 if (Desc.getTag() == DW_TAG_variable) {
3505 // GV is a global variable.
3506 DIGlobalVariable DG(GV);
3507 Scope = getOrCreateScope(DG.getContext().getGV());
3508 } else {
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003509 DenseMap<const MachineInstr *, DbgScope *>::iterator
3510 SI = InlinedVariableScopes.find(MI);
3511 if (SI != InlinedVariableScopes.end()) {
3512 // or GV is an inlined local variable.
3513 Scope = SI->second;
3514 } else {
Devang Patel2560d922009-01-15 18:25:17 +00003515 // or GV is a local variable.
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003516 DIVariable DV(GV);
3517 Scope = getOrCreateScope(DV.getContext().getGV());
3518 }
Devang Patel2560d922009-01-15 18:25:17 +00003519 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00003520
Bill Wendling6baa18d2009-02-03 21:38:21 +00003521 assert(Scope && "Unable to find variable' scope");
Devang Patel7c8a2772009-01-16 19:28:14 +00003522 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelcb59fd42009-01-12 19:17:34 +00003523 Scope->AddVariable(DV);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003524
3525 if (TimePassesIsEnabled)
3526 DebugTimer->stopTimer();
Devang Patelcb59fd42009-01-12 19:17:34 +00003527 }
Devang Patel88bf96e2009-04-13 17:02:03 +00003528
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003529 //// RecordInlinedFnStart - Indicate the start of inlined subroutine.
3530 void RecordInlinedFnStart(Instruction *FSI, DISubprogram &SP, unsigned LabelID,
3531 unsigned Src, unsigned Line, unsigned Col) {
3532 if (!TAI->doesDwarfUsesInlineInfoSection())
3533 return;
3534
3535 DbgScope *Scope = createInlinedSubroutineScope(SP, Src, Line, Col);
3536 Scope->setStartLabelID(LabelID);
Devang Patel88bf96e2009-04-13 17:02:03 +00003537 MMI->RecordUsedDbgLabel(LabelID);
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003538 GlobalVariable *GV = SP.getGV();
3539
3540 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3541 SI = DbgInlinedScopeMap.find(GV);
3542 if (SI == DbgInlinedScopeMap.end()) {
3543 SmallVector<DbgScope *, 2> Scopes;
3544 Scopes.push_back(Scope);
3545 DbgInlinedScopeMap[GV] = Scopes;
3546 } else {
3547 SmallVector<DbgScope *, 2> &Scopes = SI->second;
3548 Scopes.push_back(Scope);
3549 }
3550
Devang Patel88bf96e2009-04-13 17:02:03 +00003551 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
3552 I = InlineInfo.find(GV);
3553 if (I == InlineInfo.end()) {
3554 SmallVector<unsigned, 4> Labels;
3555 Labels.push_back(LabelID);
3556 InlineInfo[GV] = Labels;
3557 return;
3558 }
3559
3560 SmallVector<unsigned, 4> &Labels = I->second;
3561 Labels.push_back(LabelID);
3562 }
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003563
3564 /// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
3565 unsigned RecordInlinedFnEnd(DISubprogram &SP) {
3566 if (!TAI->doesDwarfUsesInlineInfoSection())
3567 return 0;
3568
3569 GlobalVariable *GV = SP.getGV();
3570 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3571 I = DbgInlinedScopeMap.find(GV);
3572 if (I == DbgInlinedScopeMap.end())
3573 return 0;
3574
3575 SmallVector<DbgScope *, 2> &Scopes = I->second;
3576 DbgScope *Scope = Scopes.back(); Scopes.pop_back();
3577 unsigned ID = MMI->NextLabelID();
3578 MMI->RecordUsedDbgLabel(ID);
3579 Scope->setEndLabelID(ID);
3580 return ID;
3581 }
3582
3583 /// RecordVariableScope - Record scope for the variable declared by
3584 /// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
3585 /// Record scopes for only inlined subroutine variables. Other
3586 /// variables' scopes are determined during RecordVariable().
3587 void RecordVariableScope(DIVariable &DV, const MachineInstr *DeclareMI) {
3588 DISubprogram SP(DV.getContext().getGV());
3589 if (SP.isNull())
3590 return;
3591 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3592 I = DbgInlinedScopeMap.find(SP.getGV());
3593 if (I == DbgInlinedScopeMap.end())
3594 return;
3595
3596 SmallVector<DbgScope *, 2> &Scopes = I->second;
3597 InlinedVariableScopes[DeclareMI] = Scopes.back();
3598 }
3599
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003600};
3601
3602//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00003603/// DwarfException - Emits Dwarf exception handling directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003604///
3605class DwarfException : public Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003606 struct FunctionEHFrameInfo {
3607 std::string FnName;
3608 unsigned Number;
3609 unsigned PersonalityIndex;
3610 bool hasCalls;
3611 bool hasLandingPads;
3612 std::vector<MachineMove> Moves;
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003613 const Function * function;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003614
3615 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3616 bool hC, bool hL,
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003617 const std::vector<MachineMove> &M,
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003618 const Function *f):
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003619 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003620 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003621 };
3622
3623 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen85535762008-04-02 00:25:04 +00003624
3625 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3626 /// be emitted.
3627 bool shouldEmitTable;
3628
3629 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3630 /// should be emitted.
3631 bool shouldEmitMoves;
3632
3633 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3634 /// should be emitted.
3635 bool shouldEmitTableModule;
3636
aslc200b112008-08-16 12:57:46 +00003637 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen85535762008-04-02 00:25:04 +00003638 /// should be emitted.
3639 bool shouldEmitMovesModule;
Duncan Sands96144f92008-05-07 19:11:09 +00003640
Bill Wendlingd9308a62009-03-10 21:23:25 +00003641 /// ExceptionTimer - Timer for the Dwarf exception writer.
3642 Timer *ExceptionTimer;
3643
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003644 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3645 ///
3646 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
3647 // Size and sign of stack growth.
3648 int stackGrowth =
3649 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3650 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00003651 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003652
3653 // Begin eh frame section.
3654 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling189bde72008-12-24 08:05:17 +00003655
3656 if (!TAI->doesRequireNonLocalEHFrameLabel())
3657 O << TAI->getEHGlobalPrefix();
3658 O << "EH_frame" << Index << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003659 EmitLabel("section_eh_frame", Index);
3660
3661 // Define base labels.
3662 EmitLabel("eh_frame_common", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003663
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003664 // Define the eh frame length.
3665 EmitDifference("eh_frame_common_end", Index,
3666 "eh_frame_common_begin", Index, true);
3667 Asm->EOL("Length of Common Information Entry");
3668
3669 // EH frame header.
3670 EmitLabel("eh_frame_common_begin", Index);
3671 Asm->EmitInt32((int)0);
3672 Asm->EOL("CIE Identifier Tag");
3673 Asm->EmitInt8(DW_CIE_VERSION);
3674 Asm->EOL("CIE Version");
Duncan Sands96144f92008-05-07 19:11:09 +00003675
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003676 // The personality presence indicates that language specific information
3677 // will show up in the eh frame.
3678 Asm->EmitString(Personality ? "zPLR" : "zR");
3679 Asm->EOL("CIE Augmentation");
Duncan Sands96144f92008-05-07 19:11:09 +00003680
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003681 // Round out reader.
3682 Asm->EmitULEB128Bytes(1);
3683 Asm->EOL("CIE Code Alignment Factor");
3684 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands96144f92008-05-07 19:11:09 +00003685 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00003686 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands96144f92008-05-07 19:11:09 +00003687 Asm->EOL("CIE Return Address Column");
3688
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003689 // If there is a personality, we need to indicate the functions location.
3690 if (Personality) {
3691 Asm->EmitULEB128Bytes(7);
3692 Asm->EOL("Augmentation Size");
Bill Wendling2d369922007-09-11 17:20:55 +00003693
Duncan Sands96144f92008-05-07 19:11:09 +00003694 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendling2d369922007-09-11 17:20:55 +00003695 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands96144f92008-05-07 19:11:09 +00003696 Asm->EOL("Personality (pcrel sdata4 indirect)");
3697 } else {
Bill Wendling2d369922007-09-11 17:20:55 +00003698 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands96144f92008-05-07 19:11:09 +00003699 Asm->EOL("Personality (pcrel sdata4)");
3700 }
Bill Wendling2d369922007-09-11 17:20:55 +00003701
Duncan Sands96144f92008-05-07 19:11:09 +00003702 PrintRelDirective(true);
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003703 O << TAI->getPersonalityPrefix();
3704 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3705 O << TAI->getPersonalitySuffix();
Duncan Sands4cc39532008-05-08 12:33:11 +00003706 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3707 O << "-" << TAI->getPCSymbol();
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003708 Asm->EOL("Personality");
Bill Wendling38cb7c92007-08-25 00:51:55 +00003709
Duncan Sands96144f92008-05-07 19:11:09 +00003710 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3711 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003712
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003713 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3714 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003715 } else {
3716 Asm->EmitULEB128Bytes(1);
3717 Asm->EOL("Augmentation Size");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003718
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003719 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3720 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003721 }
3722
3723 // Indicate locations of general callee saved registers in frame.
3724 std::vector<MachineMove> Moves;
3725 RI->getInitialFrameState(Moves);
Dale Johannesenf5a11532007-11-13 19:13:01 +00003726 EmitFrameMoves(NULL, 0, Moves, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003727
Dale Johannesen388f20f2008-04-30 00:43:29 +00003728 // On Darwin the linker honors the alignment of eh_frame, which means it
3729 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3730 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003731 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003732 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003733 EmitLabel("eh_frame_common_end", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003734
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003735 Asm->EOL();
3736 }
Duncan Sands96144f92008-05-07 19:11:09 +00003737
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003738 /// EmitEHFrame - Emit function exception frame information.
3739 ///
3740 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003741 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
Chris Lattner68433442009-04-13 05:44:34 +00003742
3743 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
3744 "Should not emit 'available externally' functions at all");
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003745
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003746 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3747
3748 // Externally visible entry into the functions eh frame info.
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003749 // If the corresponding function is static, this should not be
3750 // externally visible.
Rafael Espindolaa168fc92009-01-15 20:18:42 +00003751 if (linkage != Function::InternalLinkage &&
Devang Patel245446c2009-01-17 08:05:14 +00003752 linkage != Function::PrivateLinkage) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003753 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3754 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3755 }
3756
Dale Johannesenf09b5992008-01-10 02:03:30 +00003757 // If corresponding function is weak definition, this should be too.
Duncan Sands19d161f2009-03-07 15:45:40 +00003758 if ((linkage == Function::WeakAnyLinkage ||
3759 linkage == Function::WeakODRLinkage ||
3760 linkage == Function::LinkOnceAnyLinkage ||
3761 linkage == Function::LinkOnceODRLinkage) &&
Dale Johannesenf09b5992008-01-10 02:03:30 +00003762 TAI->getWeakDefDirective())
3763 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3764
3765 // If there are no calls then you can't unwind. This may mean we can
3766 // omit the EH Frame, but some environments do not handle weak absolute
aslc200b112008-08-16 12:57:46 +00003767 // symbols.
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003768 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesena9b3e482008-04-08 00:10:24 +00003769 // unwind info is to be available for non-EH uses.
Dale Johannesenf09b5992008-01-10 02:03:30 +00003770 if (!EHFrameInfo.hasCalls &&
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003771 !UnwindTablesMandatory &&
Duncan Sands19d161f2009-03-07 15:45:40 +00003772 ((linkage != Function::WeakAnyLinkage &&
3773 linkage != Function::WeakODRLinkage &&
3774 linkage != Function::LinkOnceAnyLinkage &&
3775 linkage != Function::LinkOnceODRLinkage) ||
Dale Johannesenf09b5992008-01-10 02:03:30 +00003776 !TAI->getWeakDefDirective() ||
3777 TAI->getSupportsWeakOmittedEHFrame()))
aslc200b112008-08-16 12:57:46 +00003778 {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003779 O << EHFrameInfo.FnName << " = 0\n";
aslc200b112008-08-16 12:57:46 +00003780 // This name has no connection to the function, so it might get
3781 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003782 // dead-stripping unconditionally.
3783 if (const char *UsedDirective = TAI->getUsedDirective())
3784 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003785 } else {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003786 O << EHFrameInfo.FnName << ":\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003787
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003788 // EH frame header.
3789 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3790 "eh_frame_begin", EHFrameInfo.Number, true);
3791 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00003792
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003793 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
3794
Bill Wendling189bde72008-12-24 08:05:17 +00003795 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3796 PrintRelDirective(true, true);
3797 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3798
3799 if (!TAI->isAbsoluteEHSectionOffsets())
3800 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3801 } else {
3802 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3803 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3804 true, true, false);
3805 }
3806
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003807 Asm->EOL("FDE CIE offset");
3808
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003809 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003810 Asm->EOL("FDE initial location");
3811 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003812 "eh_func_begin", EHFrameInfo.Number, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003813 Asm->EOL("FDE address range");
Duncan Sands96144f92008-05-07 19:11:09 +00003814
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003815 // If there is a personality and landing pads then point to the language
3816 // specific data area in the exception table.
3817 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands96144f92008-05-07 19:11:09 +00003818 Asm->EmitULEB128Bytes(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003819 Asm->EOL("Augmentation size");
Duncan Sands96144f92008-05-07 19:11:09 +00003820
3821 if (EHFrameInfo.hasLandingPads)
3822 EmitReference("exception", EHFrameInfo.Number, true, true);
3823 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003824 Asm->EmitInt32((int)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003825 Asm->EOL("Language Specific Data Area");
3826 } else {
3827 Asm->EmitULEB128Bytes(0);
3828 Asm->EOL("Augmentation size");
3829 }
Duncan Sands96144f92008-05-07 19:11:09 +00003830
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003831 // Indicate locations of function specific callee saved registers in
3832 // frame.
Devang Patelb28de842009-01-17 08:01:33 +00003833 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00003834 true);
aslc200b112008-08-16 12:57:46 +00003835
Dale Johannesen388f20f2008-04-30 00:43:29 +00003836 // On Darwin the linker honors the alignment of eh_frame, which means it
3837 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3838 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003839 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003840 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003841 EmitLabel("eh_frame_end", EHFrameInfo.Number);
aslc200b112008-08-16 12:57:46 +00003842
3843 // If the function is marked used, this table should be also. We cannot
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003844 // make the mark unconditional in this case, since retaining the table
aslc200b112008-08-16 12:57:46 +00003845 // also retains the function in this case, and there is code around
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003846 // that depends on unused functions (calling undefined externals) being
3847 // dead-stripped to link correctly. Yes, there really is.
3848 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3849 if (const char *UsedDirective = TAI->getUsedDirective())
3850 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3851 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003852 }
3853
Duncan Sands241a0c92007-09-05 11:27:52 +00003854 /// EmitExceptionTable - Emit landing pads and actions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003855 ///
3856 /// The general organization of the table is complex, but the basic concepts
3857 /// are easy. First there is a header which describes the location and
3858 /// organization of the three components that follow.
3859 /// 1. The landing pad site information describes the range of code covered
3860 /// by the try. In our case it's an accumulation of the ranges covered
3861 /// by the invokes in the try. There is also a reference to the landing
3862 /// pad that handles the exception once processed. Finally an index into
3863 /// the actions table.
3864 /// 2. The action table, in our case, is composed of pairs of type ids
3865 /// and next action offset. Starting with the action index from the
3866 /// landing pad site, each type Id is checked for a match to the current
3867 /// exception. If it matches then the exception and type id are passed
3868 /// on to the landing pad. Otherwise the next action is looked up. This
3869 /// chain is terminated with a next action of zero. If no type id is
3870 /// found the the frame is unwound and handling continues.
3871 /// 3. Type id table contains references to all the C++ typeinfo for all
3872 /// catches in the function. This tables is reversed indexed base 1.
3873
3874 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3875 static unsigned SharedTypeIds(const LandingPadInfo *L,
3876 const LandingPadInfo *R) {
3877 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3878 unsigned LSize = LIds.size(), RSize = RIds.size();
3879 unsigned MinSize = LSize < RSize ? LSize : RSize;
3880 unsigned Count = 0;
3881
3882 for (; Count != MinSize; ++Count)
3883 if (LIds[Count] != RIds[Count])
3884 return Count;
3885
3886 return Count;
3887 }
3888
3889 /// PadLT - Order landing pads lexicographically by type id.
3890 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
3891 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3892 unsigned LSize = LIds.size(), RSize = RIds.size();
3893 unsigned MinSize = LSize < RSize ? LSize : RSize;
3894
3895 for (unsigned i = 0; i != MinSize; ++i)
3896 if (LIds[i] != RIds[i])
3897 return LIds[i] < RIds[i];
3898
3899 return LSize < RSize;
3900 }
3901
3902 struct KeyInfo {
3903 static inline unsigned getEmptyKey() { return -1U; }
3904 static inline unsigned getTombstoneKey() { return -2U; }
3905 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner92eea072007-09-17 18:34:04 +00003906 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003907 static bool isPod() { return true; }
3908 };
3909
Duncan Sands241a0c92007-09-05 11:27:52 +00003910 /// ActionEntry - Structure describing an entry in the actions table.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003911 struct ActionEntry {
3912 int ValueForTypeID; // The value to write - may not be equal to the type id.
3913 int NextAction;
3914 struct ActionEntry *Previous;
3915 };
3916
Duncan Sands241a0c92007-09-05 11:27:52 +00003917 /// PadRange - Structure holding a try-range and the associated landing pad.
3918 struct PadRange {
3919 // The index of the landing pad.
3920 unsigned PadIndex;
3921 // The index of the begin and end labels in the landing pad's label lists.
3922 unsigned RangeIndex;
3923 };
3924
3925 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3926
3927 /// CallSiteEntry - Structure describing an entry in the call-site table.
3928 struct CallSiteEntry {
Duncan Sands4ff179f2007-12-19 07:36:31 +00003929 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003930 unsigned BeginLabel; // zero indicates the start of the function.
3931 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003932 // The landing pad starts at PadLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003933 unsigned PadLabel; // zero indicates that there is no landing pad.
3934 unsigned Action;
3935 };
3936
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003937 void EmitExceptionTable() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003938 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
3939 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
3940 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3941 if (PadInfos.empty()) return;
3942
3943 // Sort the landing pads in order of their type ids. This is used to fold
3944 // duplicate actions.
3945 SmallVector<const LandingPadInfo *, 64> LandingPads;
3946 LandingPads.reserve(PadInfos.size());
3947 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
3948 LandingPads.push_back(&PadInfos[i]);
3949 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3950
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003951 // Negative type ids index into FilterIds, positive type ids index into
3952 // TypeInfos. The value written for a positive type id is just the type
3953 // id itself. For a negative type id, however, the value written is the
3954 // (negative) byte offset of the corresponding FilterIds entry. The byte
3955 // offset is usually equal to the type id, because the FilterIds entries
3956 // are written using a variable width encoding which outputs one byte per
3957 // entry as long as the value written is not too large, but can differ.
3958 // This kind of complication does not occur for positive type ids because
3959 // type infos are output using a fixed width encoding.
3960 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3961 SmallVector<int, 16> FilterOffsets;
3962 FilterOffsets.reserve(FilterIds.size());
3963 int Offset = -1;
3964 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3965 E = FilterIds.end(); I != E; ++I) {
3966 FilterOffsets.push_back(Offset);
aslc200b112008-08-16 12:57:46 +00003967 Offset -= TargetAsmInfo::getULEB128Size(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003968 }
3969
Duncan Sands241a0c92007-09-05 11:27:52 +00003970 // Compute the actions table and gather the first action index for each
3971 // landing pad site.
3972 SmallVector<ActionEntry, 32> Actions;
3973 SmallVector<unsigned, 64> FirstActions;
3974 FirstActions.reserve(LandingPads.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003975
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003976 int FirstAction = 0;
Duncan Sands241a0c92007-09-05 11:27:52 +00003977 unsigned SizeActions = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003978 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3979 const LandingPadInfo *LP = LandingPads[i];
3980 const std::vector<int> &TypeIds = LP->TypeIds;
3981 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
3982 unsigned SizeSiteActions = 0;
3983
3984 if (NumShared < TypeIds.size()) {
3985 unsigned SizeAction = 0;
3986 ActionEntry *PrevAction = 0;
3987
3988 if (NumShared) {
3989 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3990 assert(Actions.size());
3991 PrevAction = &Actions.back();
aslc200b112008-08-16 12:57:46 +00003992 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3993 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003994 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
aslc200b112008-08-16 12:57:46 +00003995 SizeAction -=
3996 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003997 SizeAction += -PrevAction->NextAction;
3998 PrevAction = PrevAction->Previous;
3999 }
4000 }
4001
4002 // Compute the actions.
4003 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
4004 int TypeID = TypeIds[I];
4005 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
4006 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
aslc200b112008-08-16 12:57:46 +00004007 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004008
4009 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
aslc200b112008-08-16 12:57:46 +00004010 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004011 SizeSiteActions += SizeAction;
4012
4013 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
4014 Actions.push_back(Action);
4015
4016 PrevAction = &Actions.back();
4017 }
4018
4019 // Record the first action of the landing pad site.
4020 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
4021 } // else identical - re-use previous FirstAction
4022
4023 FirstActions.push_back(FirstAction);
4024
4025 // Compute this sites contribution to size.
4026 SizeActions += SizeSiteActions;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004027 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004028
Duncan Sands4ff179f2007-12-19 07:36:31 +00004029 // Compute the call-site table. The entry for an invoke has a try-range
4030 // containing the call, a non-zero landing pad and an appropriate action.
4031 // The entry for an ordinary call has a try-range containing the call and
4032 // zero for the landing pad and the action. Calls marked 'nounwind' have
4033 // no entry and must not be contained in the try-range of any entry - they
4034 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands241a0c92007-09-05 11:27:52 +00004035 SmallVector<CallSiteEntry, 64> CallSites;
4036
4037 RangeMapType PadMap;
Duncan Sands4ff179f2007-12-19 07:36:31 +00004038 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
4039 // by try-range labels when lowered). Ordinary calls do not, so appropriate
4040 // try-ranges for them need be deduced.
Duncan Sands241a0c92007-09-05 11:27:52 +00004041 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
4042 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands4ff179f2007-12-19 07:36:31 +00004043 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands241a0c92007-09-05 11:27:52 +00004044 unsigned BeginLabel = LandingPad->BeginLabels[j];
4045 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
4046 PadRange P = { i, j };
4047 PadMap[BeginLabel] = P;
4048 }
4049 }
4050
Duncan Sands4ff179f2007-12-19 07:36:31 +00004051 // The end label of the previous invoke or nounwind try-range.
Duncan Sands241a0c92007-09-05 11:27:52 +00004052 unsigned LastLabel = 0;
Duncan Sands4ff179f2007-12-19 07:36:31 +00004053
4054 // Whether there is a potentially throwing instruction (currently this means
4055 // an ordinary call) between the end of the previous try-range and now.
4056 bool SawPotentiallyThrowing = false;
4057
4058 // Whether the last callsite entry was for an invoke.
4059 bool PreviousIsInvoke = false;
4060
Duncan Sands4ff179f2007-12-19 07:36:31 +00004061 // Visit all instructions in order of address.
Duncan Sands241a0c92007-09-05 11:27:52 +00004062 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
4063 I != E; ++I) {
4064 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
4065 MI != E; ++MI) {
Dan Gohmanfa607c92008-07-01 00:05:16 +00004066 if (!MI->isLabel()) {
Chris Lattner5b930372008-01-07 07:27:27 +00004067 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands241a0c92007-09-05 11:27:52 +00004068 continue;
4069 }
4070
Chris Lattnerda4cff12007-12-30 20:50:28 +00004071 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands241a0c92007-09-05 11:27:52 +00004072 assert(BeginLabel && "Invalid label!");
Duncan Sands89372f62007-09-05 14:12:46 +00004073
Duncan Sands4ff179f2007-12-19 07:36:31 +00004074 // End of the previous try-range?
Duncan Sands89372f62007-09-05 14:12:46 +00004075 if (BeginLabel == LastLabel)
Duncan Sands4ff179f2007-12-19 07:36:31 +00004076 SawPotentiallyThrowing = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00004077
Duncan Sands4ff179f2007-12-19 07:36:31 +00004078 // Beginning of a new try-range?
Duncan Sands241a0c92007-09-05 11:27:52 +00004079 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands241a0c92007-09-05 11:27:52 +00004080 if (L == PadMap.end())
Duncan Sands4ff179f2007-12-19 07:36:31 +00004081 // Nope, it was just some random label.
Duncan Sands241a0c92007-09-05 11:27:52 +00004082 continue;
4083
4084 PadRange P = L->second;
4085 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
4086
4087 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
4088 "Inconsistent landing pad map!");
4089
4090 // If some instruction between the previous try-range and this one may
4091 // throw, create a call-site entry with no landing pad for the region
4092 // between the try-ranges.
Duncan Sands4ff179f2007-12-19 07:36:31 +00004093 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00004094 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
4095 CallSites.push_back(Site);
Duncan Sands4ff179f2007-12-19 07:36:31 +00004096 PreviousIsInvoke = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00004097 }
4098
4099 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands4ff179f2007-12-19 07:36:31 +00004100 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands241a0c92007-09-05 11:27:52 +00004101
Duncan Sands4ff179f2007-12-19 07:36:31 +00004102 if (LandingPad->LandingPadLabel) {
4103 // This try-range is for an invoke.
4104 CallSiteEntry Site = {BeginLabel, LastLabel,
4105 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands241a0c92007-09-05 11:27:52 +00004106
Duncan Sands4ff179f2007-12-19 07:36:31 +00004107 // Try to merge with the previous call-site.
4108 if (PreviousIsInvoke) {
Dan Gohman3d436002008-06-21 22:00:54 +00004109 CallSiteEntry &Prev = CallSites.back();
Duncan Sands4ff179f2007-12-19 07:36:31 +00004110 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
4111 // Extend the range of the previous entry.
4112 Prev.EndLabel = Site.EndLabel;
4113 continue;
4114 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004115 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004116
Duncan Sands4ff179f2007-12-19 07:36:31 +00004117 // Otherwise, create a new call-site.
4118 CallSites.push_back(Site);
4119 PreviousIsInvoke = true;
4120 } else {
4121 // Create a gap.
4122 PreviousIsInvoke = false;
4123 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004124 }
4125 }
4126 // If some instruction between the previous try-range and the end of the
4127 // function may throw, create a call-site entry with no landing pad for the
4128 // region following the try-range.
Duncan Sands4ff179f2007-12-19 07:36:31 +00004129 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00004130 CallSiteEntry Site = {LastLabel, 0, 0, 0};
4131 CallSites.push_back(Site);
4132 }
4133
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004134 // Final tallies.
Duncan Sands96144f92008-05-07 19:11:09 +00004135
4136 // Call sites.
4137 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
4138 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
4139 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
4140 unsigned SizeSites = CallSites.size() * (SiteStartSize +
4141 SiteLengthSize +
4142 LandingPadSize);
Duncan Sands241a0c92007-09-05 11:27:52 +00004143 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
aslc200b112008-08-16 12:57:46 +00004144 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands241a0c92007-09-05 11:27:52 +00004145
Duncan Sands96144f92008-05-07 19:11:09 +00004146 // Type infos.
4147 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
4148 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004149
4150 unsigned TypeOffset = sizeof(int8_t) + // Call site format
aslc200b112008-08-16 12:57:46 +00004151 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004152 SizeSites + SizeActions + SizeTypes;
4153
4154 unsigned TotalSize = sizeof(int8_t) + // LPStart format
4155 sizeof(int8_t) + // TType format
aslc200b112008-08-16 12:57:46 +00004156 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004157 TypeOffset;
4158
4159 unsigned SizeAlign = (4 - TotalSize) & 3;
4160
4161 // Begin the exception table.
4162 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng7e7d1942008-02-29 19:36:59 +00004163 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesen841e4982008-10-08 21:50:21 +00004164 O << "GCC_except_table" << SubprogramCount << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004165 for (unsigned i = 0; i != SizeAlign; ++i) {
4166 Asm->EmitInt8(0);
4167 Asm->EOL("Padding");
4168 }
4169 EmitLabel("exception", SubprogramCount);
4170
4171 // Emit the header.
4172 Asm->EmitInt8(DW_EH_PE_omit);
4173 Asm->EOL("LPStart format (DW_EH_PE_omit)");
4174 Asm->EmitInt8(DW_EH_PE_absptr);
4175 Asm->EOL("TType format (DW_EH_PE_absptr)");
4176 Asm->EmitULEB128Bytes(TypeOffset);
4177 Asm->EOL("TType base offset");
4178 Asm->EmitInt8(DW_EH_PE_udata4);
4179 Asm->EOL("Call site format (DW_EH_PE_udata4)");
4180 Asm->EmitULEB128Bytes(SizeSites);
4181 Asm->EOL("Call-site table length");
4182
Duncan Sands241a0c92007-09-05 11:27:52 +00004183 // Emit the landing pad site information.
4184 for (unsigned i = 0; i < CallSites.size(); ++i) {
4185 CallSiteEntry &S = CallSites[i];
4186 const char *BeginTag;
4187 unsigned BeginNumber;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004188
Duncan Sands241a0c92007-09-05 11:27:52 +00004189 if (!S.BeginLabel) {
4190 BeginTag = "eh_func_begin";
4191 BeginNumber = SubprogramCount;
4192 } else {
4193 BeginTag = "label";
4194 BeginNumber = S.BeginLabel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004195 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004196
Duncan Sands241a0c92007-09-05 11:27:52 +00004197 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00004198 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004199 Asm->EOL("Region start");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004200
Duncan Sands241a0c92007-09-05 11:27:52 +00004201 if (!S.EndLabel) {
Dale Johannesen4670be42008-01-15 23:24:56 +00004202 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands96144f92008-05-07 19:11:09 +00004203 true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004204 } else {
Duncan Sands96144f92008-05-07 19:11:09 +00004205 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004206 }
4207 Asm->EOL("Region length");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004208
Duncan Sands96144f92008-05-07 19:11:09 +00004209 if (!S.PadLabel)
4210 Asm->EmitInt32(0);
4211 else
Duncan Sands241a0c92007-09-05 11:27:52 +00004212 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00004213 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004214 Asm->EOL("Landing pad");
4215
4216 Asm->EmitULEB128Bytes(S.Action);
4217 Asm->EOL("Action");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004218 }
4219
4220 // Emit the actions.
4221 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4222 ActionEntry &Action = Actions[I];
4223
4224 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
4225 Asm->EOL("TypeInfo index");
4226 Asm->EmitSLEB128Bytes(Action.NextAction);
4227 Asm->EOL("Next action");
4228 }
4229
4230 // Emit the type ids.
4231 for (unsigned M = TypeInfos.size(); M; --M) {
4232 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00004233
4234 PrintRelDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004235
Bill Wendling26a8ab92009-04-10 00:12:49 +00004236 if (GV) {
4237 std::string GLN;
4238 O << Asm->getGlobalLinkName(GV, GLN);
4239 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004240 O << "0";
Bill Wendling26a8ab92009-04-10 00:12:49 +00004241 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004242
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004243 Asm->EOL("TypeInfo");
4244 }
4245
4246 // Emit the filter typeids.
4247 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4248 unsigned TypeID = FilterIds[j];
4249 Asm->EmitULEB128Bytes(TypeID);
4250 Asm->EOL("Filter TypeInfo index");
4251 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004252
Evan Cheng7e7d1942008-02-29 19:36:59 +00004253 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004254 }
4255
4256public:
4257 //===--------------------------------------------------------------------===//
4258 // Main entry points.
4259 //
Owen Anderson847b99b2008-08-21 00:14:44 +00004260 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendlingd9308a62009-03-10 21:23:25 +00004261 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
4262 shouldEmitTableModule(false), shouldEmitMovesModule(false),
4263 ExceptionTimer(0) {
4264 if (TimePassesIsEnabled)
4265 ExceptionTimer = new Timer("Dwarf Exception Writer",
Bill Wendling148ecc42009-03-10 22:58:53 +00004266 getDwarfTimerGroup());
Bill Wendlingd9308a62009-03-10 21:23:25 +00004267 }
aslc200b112008-08-16 12:57:46 +00004268
Bill Wendlingd9308a62009-03-10 21:23:25 +00004269 virtual ~DwarfException() {
4270 delete ExceptionTimer;
4271 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004272
4273 /// SetModuleInfo - Set machine module information when it's known that pass
4274 /// manager has created it. Set by the target AsmPrinter.
4275 void SetModuleInfo(MachineModuleInfo *mmi) {
4276 MMI = mmi;
4277 }
4278
4279 /// BeginModule - Emit all exception information that should come prior to the
4280 /// content.
4281 void BeginModule(Module *M) {
4282 this->M = M;
4283 }
4284
4285 /// EndModule - Emit all exception information that should come after the
4286 /// content.
4287 void EndModule() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004288 if (TimePassesIsEnabled)
4289 ExceptionTimer->startTimer();
4290
Dale Johannesen85535762008-04-02 00:25:04 +00004291 if (shouldEmitMovesModule || shouldEmitTableModule) {
4292 const std::vector<Function *> Personalities = MMI->getPersonalities();
Evan Cheng3e288912009-02-25 07:04:34 +00004293 for (unsigned i = 0; i < Personalities.size(); ++i)
Dale Johannesen85535762008-04-02 00:25:04 +00004294 EmitCommonEHFrame(Personalities[i], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004295
Dale Johannesen85535762008-04-02 00:25:04 +00004296 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4297 E = EHFrames.end(); I != E; ++I)
4298 EmitEHFrame(*I);
4299 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00004300
4301 if (TimePassesIsEnabled)
4302 ExceptionTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004303 }
4304
aslc200b112008-08-16 12:57:46 +00004305 /// BeginFunction - Gather pre-function exception information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004306 /// emitted immediately after the function entry point.
4307 void BeginFunction(MachineFunction *MF) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004308 if (TimePassesIsEnabled)
4309 ExceptionTimer->startTimer();
4310
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004311 this->MF = MF;
Dale Johannesen85535762008-04-02 00:25:04 +00004312 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen85535762008-04-02 00:25:04 +00004313
Bill Wendlingd9308a62009-03-10 21:23:25 +00004314 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen85535762008-04-02 00:25:04 +00004315 // Map all labels and get rid of any dead landing pads.
4316 MMI->TidyLandingPads();
Bill Wendlingd9308a62009-03-10 21:23:25 +00004317
Dale Johannesen85535762008-04-02 00:25:04 +00004318 // If any landing pads survive, we need an EH table.
4319 if (MMI->getLandingPads().size())
4320 shouldEmitTable = true;
4321
4322 // See if we need frame move info.
Duncan Sandscbc28b12008-07-04 09:55:48 +00004323 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen85535762008-04-02 00:25:04 +00004324 shouldEmitMoves = true;
4325
4326 if (shouldEmitMoves || shouldEmitTable)
4327 // Assumes in correct section after the entry point.
4328 EmitLabel("eh_func_begin", ++SubprogramCount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004329 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00004330
Dale Johannesen85535762008-04-02 00:25:04 +00004331 shouldEmitTableModule |= shouldEmitTable;
4332 shouldEmitMovesModule |= shouldEmitMoves;
Bill Wendlingd9308a62009-03-10 21:23:25 +00004333
4334 if (TimePassesIsEnabled)
4335 ExceptionTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004336 }
4337
4338 /// EndFunction - Gather and emit post-function exception information.
4339 ///
4340 void EndFunction() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004341 if (TimePassesIsEnabled)
4342 ExceptionTimer->startTimer();
4343
Dale Johannesen85535762008-04-02 00:25:04 +00004344 if (shouldEmitMoves || shouldEmitTable) {
4345 EmitLabel("eh_func_end", SubprogramCount);
4346 EmitExceptionTable();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004347
Dale Johannesen85535762008-04-02 00:25:04 +00004348 // Save EH frame information
Bill Wendling26a8ab92009-04-10 00:12:49 +00004349 std::string Name;
4350 EHFrames.push_back(
4351 FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF, Name),
4352 SubprogramCount,
4353 MMI->getPersonalityIndex(),
4354 MF->getFrameInfo()->hasCalls(),
4355 !MMI->getLandingPads().empty(),
4356 MMI->getFrameMoves(),
4357 MF->getFunction()));
Bill Wendlingd9308a62009-03-10 21:23:25 +00004358 }
4359
4360 if (TimePassesIsEnabled)
4361 ExceptionTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004362 }
4363};
4364
4365} // End of namespace llvm
4366
4367//===----------------------------------------------------------------------===//
4368
4369/// Emit - Print the abbreviation using the specified Dwarf writer.
4370///
4371void DIEAbbrev::Emit(const DwarfDebug &DD) const {
4372 // Emit its Dwarf tag type.
4373 DD.getAsm()->EmitULEB128Bytes(Tag);
4374 DD.getAsm()->EOL(TagString(Tag));
aslc200b112008-08-16 12:57:46 +00004375
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004376 // Emit whether it has children DIEs.
4377 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4378 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
aslc200b112008-08-16 12:57:46 +00004379
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004380 // For each attribute description.
4381 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4382 const DIEAbbrevData &AttrData = Data[i];
aslc200b112008-08-16 12:57:46 +00004383
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004384 // Emit attribute type.
4385 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4386 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
aslc200b112008-08-16 12:57:46 +00004387
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004388 // Emit form type.
4389 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4390 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
4391 }
4392
4393 // Mark end of abbreviation.
4394 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4395 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
4396}
4397
4398#ifndef NDEBUG
4399void DIEAbbrev::print(std::ostream &O) {
4400 O << "Abbreviation @"
4401 << std::hex << (intptr_t)this << std::dec
4402 << " "
4403 << TagString(Tag)
4404 << " "
4405 << ChildrenString(ChildrenFlag)
4406 << "\n";
aslc200b112008-08-16 12:57:46 +00004407
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004408 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4409 O << " "
4410 << AttributeString(Data[i].getAttribute())
4411 << " "
4412 << FormEncodingString(Data[i].getForm())
4413 << "\n";
4414 }
4415}
4416void DIEAbbrev::dump() { print(cerr); }
4417#endif
4418
4419//===----------------------------------------------------------------------===//
4420
4421#ifndef NDEBUG
4422void DIEValue::dump() {
4423 print(cerr);
4424}
4425#endif
4426
4427//===----------------------------------------------------------------------===//
4428
4429/// EmitValue - Emit integer of appropriate size.
4430///
4431void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
4432 switch (Form) {
4433 case DW_FORM_flag: // Fall thru
4434 case DW_FORM_ref1: // Fall thru
4435 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
4436 case DW_FORM_ref2: // Fall thru
4437 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
4438 case DW_FORM_ref4: // Fall thru
4439 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
4440 case DW_FORM_ref8: // Fall thru
4441 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4442 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4443 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
4444 default: assert(0 && "DIE Value form not supported yet"); break;
4445 }
4446}
4447
4448/// SizeOf - Determine size of integer value in bytes.
4449///
4450unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4451 switch (Form) {
4452 case DW_FORM_flag: // Fall thru
4453 case DW_FORM_ref1: // Fall thru
4454 case DW_FORM_data1: return sizeof(int8_t);
4455 case DW_FORM_ref2: // Fall thru
4456 case DW_FORM_data2: return sizeof(int16_t);
4457 case DW_FORM_ref4: // Fall thru
4458 case DW_FORM_data4: return sizeof(int32_t);
4459 case DW_FORM_ref8: // Fall thru
4460 case DW_FORM_data8: return sizeof(int64_t);
aslc200b112008-08-16 12:57:46 +00004461 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4462 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004463 default: assert(0 && "DIE Value form not supported yet"); break;
4464 }
4465 return 0;
4466}
4467
4468//===----------------------------------------------------------------------===//
4469
4470/// EmitValue - Emit string value.
4471///
4472void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Bill Wendling15afa002009-03-10 23:57:09 +00004473 DD.getAsm()->EmitString(Str);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004474}
4475
4476//===----------------------------------------------------------------------===//
4477
4478/// EmitValue - Emit label value.
4479///
4480void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004481 bool IsSmall = Form == DW_FORM_data4;
4482 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004483}
4484
4485/// SizeOf - Determine size of label value in bytes.
4486///
4487unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004488 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004489 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004490}
4491
4492//===----------------------------------------------------------------------===//
4493
4494/// EmitValue - Emit label value.
4495///
4496void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004497 bool IsSmall = Form == DW_FORM_data4;
4498 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004499}
4500
4501/// SizeOf - Determine size of label value in bytes.
4502///
4503unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004504 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004505 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004506}
aslc200b112008-08-16 12:57:46 +00004507
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004508//===----------------------------------------------------------------------===//
4509
4510/// EmitValue - Emit delta value.
4511///
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004512void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4513 bool IsSmall = Form == DW_FORM_data4;
4514 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4515 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4516}
4517
4518/// SizeOf - Determine size of delta value in bytes.
4519///
4520unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4521 if (Form == DW_FORM_data4) return 4;
4522 return DD.getTargetData()->getPointerSize();
4523}
aslc200b112008-08-16 12:57:46 +00004524
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004525//===----------------------------------------------------------------------===//
4526
4527/// EmitValue - Emit delta value.
4528///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004529void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
4530 bool IsSmall = Form == DW_FORM_data4;
4531 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
4532}
4533
4534/// SizeOf - Determine size of delta value in bytes.
4535///
4536unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4537 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004538 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004539}
4540
4541//===----------------------------------------------------------------------===//
4542
4543/// EmitValue - Emit debug information entry offset.
4544///
4545void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
4546 DD.getAsm()->EmitInt32(Entry->getOffset());
4547}
aslc200b112008-08-16 12:57:46 +00004548
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004549//===----------------------------------------------------------------------===//
4550
4551/// ComputeSize - calculate the size of the block.
4552///
4553unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
4554 if (!Size) {
Owen Anderson88dd6232008-06-24 21:44:59 +00004555 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004556
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004557 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4558 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
4559 }
4560 }
4561 return Size;
4562}
4563
4564/// EmitValue - Emit block data.
4565///
4566void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
4567 switch (Form) {
4568 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4569 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4570 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4571 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
4572 default: assert(0 && "Improper form for block"); break;
4573 }
aslc200b112008-08-16 12:57:46 +00004574
Owen Anderson88dd6232008-06-24 21:44:59 +00004575 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004576
4577 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4578 DD.getAsm()->EOL();
4579 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
4580 }
4581}
4582
4583/// SizeOf - Determine size of block data in bytes.
4584///
4585unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4586 switch (Form) {
4587 case DW_FORM_block1: return Size + sizeof(int8_t);
4588 case DW_FORM_block2: return Size + sizeof(int16_t);
4589 case DW_FORM_block4: return Size + sizeof(int32_t);
aslc200b112008-08-16 12:57:46 +00004590 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004591 default: assert(0 && "Improper form for block"); break;
4592 }
4593 return 0;
4594}
4595
4596//===----------------------------------------------------------------------===//
4597/// DIE Implementation
4598
4599DIE::~DIE() {
4600 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4601 delete Children[i];
4602}
aslc200b112008-08-16 12:57:46 +00004603
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004604/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4605///
4606void DIE::AddSiblingOffset() {
4607 DIEInteger *DI = new DIEInteger(0);
4608 Values.insert(Values.begin(), DI);
4609 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
4610}
4611
4612/// Profile - Used to gather unique data for the value folding set.
4613///
4614void DIE::Profile(FoldingSetNodeID &ID) {
4615 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00004616
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004617 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4618 ID.AddPointer(Children[i]);
4619
4620 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4621 ID.AddPointer(Values[j]);
4622}
4623
4624#ifndef NDEBUG
4625void DIE::print(std::ostream &O, unsigned IncIndent) {
4626 static unsigned IndentCount = 0;
4627 IndentCount += IncIndent;
4628 const std::string Indent(IndentCount, ' ');
4629 bool isBlock = Abbrev.getTag() == 0;
aslc200b112008-08-16 12:57:46 +00004630
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004631 if (!isBlock) {
4632 O << Indent
4633 << "Die: "
4634 << "0x" << std::hex << (intptr_t)this << std::dec
4635 << ", Offset: " << Offset
4636 << ", Size: " << Size
aslc200b112008-08-16 12:57:46 +00004637 << "\n";
4638
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004639 O << Indent
4640 << TagString(Abbrev.getTag())
4641 << " "
4642 << ChildrenString(Abbrev.getChildrenFlag());
4643 } else {
4644 O << "Size: " << Size;
4645 }
4646 O << "\n";
4647
Owen Anderson88dd6232008-06-24 21:44:59 +00004648 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004649
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004650 IndentCount += 2;
4651 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4652 O << Indent;
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004653
4654 if (!isBlock)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004655 O << AttributeString(Data[i].getAttribute());
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004656 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004657 O << "Blk[" << i << "]";
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004658
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004659 O << " "
4660 << FormEncodingString(Data[i].getForm())
4661 << " ";
4662 Values[i]->print(O);
4663 O << "\n";
4664 }
4665 IndentCount -= 2;
4666
4667 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4668 Children[j]->print(O, 4);
4669 }
aslc200b112008-08-16 12:57:46 +00004670
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004671 if (!isBlock) O << "\n";
4672 IndentCount -= IncIndent;
4673}
4674
4675void DIE::dump() {
4676 print(cerr);
4677}
4678#endif
4679
4680//===----------------------------------------------------------------------===//
4681/// DwarfWriter Implementation
4682///
4683
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004684DwarfWriter::DwarfWriter()
Bill Wendlingd9308a62009-03-10 21:23:25 +00004685 : ImmutablePass(&ID), DD(0), DE(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004686
4687DwarfWriter::~DwarfWriter() {
4688 delete DE;
4689 delete DD;
4690}
4691
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004692/// BeginModule - Emit all Dwarf sections that should come prior to the
4693/// content.
Devang Patelaa1e8432009-01-08 23:40:34 +00004694void DwarfWriter::BeginModule(Module *M,
4695 MachineModuleInfo *MMI,
4696 raw_ostream &OS, AsmPrinter *A,
4697 const TargetAsmInfo *T) {
4698 DE = new DwarfException(OS, A, T);
4699 DD = new DwarfDebug(OS, A, T);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004700 DE->BeginModule(M);
4701 DD->BeginModule(M);
Devang Patel6ccd57e2009-01-13 00:20:51 +00004702 DD->SetDebugInfo(MMI);
Devang Patelaa1e8432009-01-08 23:40:34 +00004703 DE->SetModuleInfo(MMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004704}
4705
4706/// EndModule - Emit all Dwarf sections that should come after the content.
4707///
4708void DwarfWriter::EndModule() {
4709 DE->EndModule();
4710 DD->EndModule();
4711}
4712
aslc200b112008-08-16 12:57:46 +00004713/// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004714/// emitted immediately after the function entry point.
4715void DwarfWriter::BeginFunction(MachineFunction *MF) {
4716 DE->BeginFunction(MF);
4717 DD->BeginFunction(MF);
4718}
4719
4720/// EndFunction - Gather and emit post-function debug information.
4721///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00004722void DwarfWriter::EndFunction(MachineFunction *MF) {
4723 DD->EndFunction(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004724 DE->EndFunction();
aslc200b112008-08-16 12:57:46 +00004725
Bill Wendling5b4796a2008-07-22 00:53:37 +00004726 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004727 // Clear function debug information.
4728 MMI->EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004729}
Devang Patelcb59fd42009-01-12 19:17:34 +00004730
Devang Patel2da0cc42009-01-15 23:41:32 +00004731/// ValidDebugInfo - Return true if V represents valid debug info value.
Devang Patel18992922009-04-13 18:13:16 +00004732bool DwarfWriter::ValidDebugInfo(Value *V, bool FastISel) {
4733 return DD && DD->ValidDebugInfo(V, FastISel);
Devang Patel2da0cc42009-01-15 23:41:32 +00004734}
4735
Devang Patelcb59fd42009-01-12 19:17:34 +00004736/// RecordSourceLine - Records location information and associates it with a
4737/// label. Returns a unique label ID used to generate a label and provide
4738/// correspondence to the source line list.
4739unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
4740 unsigned Src) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004741 return DD->RecordSourceLine(Line, Col, Src);
Devang Patelcb59fd42009-01-12 19:17:34 +00004742}
4743
Evan Cheng3e288912009-02-25 07:04:34 +00004744/// getOrCreateSourceID - Look up the source id with the given directory and
4745/// source file names. If none currently exists, create a new id and insert it
4746/// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
4747/// as well.
4748unsigned DwarfWriter::getOrCreateSourceID(const std::string &DirName,
4749 const std::string &FileName) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004750 return DD->getOrCreateSourceID(DirName, FileName);
Devang Patelcb59fd42009-01-12 19:17:34 +00004751}
4752
4753/// RecordRegionStart - Indicate the start of a region.
4754unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004755 return DD->RecordRegionStart(V);
Devang Patelcb59fd42009-01-12 19:17:34 +00004756}
4757
4758/// RecordRegionEnd - Indicate the end of a region.
4759unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004760 return DD->RecordRegionEnd(V);
Devang Patelcb59fd42009-01-12 19:17:34 +00004761}
4762
4763/// getRecordSourceLineCount - Count source lines.
4764unsigned DwarfWriter::getRecordSourceLineCount() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004765 return DD->getRecordSourceLineCount();
Devang Patelcb59fd42009-01-12 19:17:34 +00004766}
Devang Patel70190872009-01-13 21:25:00 +00004767
Devang Patelfe359e72009-01-13 21:44:10 +00004768/// RecordVariable - Indicate the declaration of a local variable.
4769///
Devang Patel8a9a7dc2009-04-15 00:10:26 +00004770void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
4771 const MachineInstr *MI) {
4772 DD->RecordVariable(GV, FrameIndex, MI);
Devang Patelfe359e72009-01-13 21:44:10 +00004773}
Devang Patel42f6bed2009-01-13 23:54:55 +00004774
Bill Wendling50db0792009-02-20 00:44:43 +00004775/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
4776/// be emitted.
4777bool DwarfWriter::ShouldEmitDwarfDebug() const {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004778 return DD->ShouldEmitDwarfDebug();
Bill Wendling50db0792009-02-20 00:44:43 +00004779}
Devang Patel88bf96e2009-04-13 17:02:03 +00004780
Devang Patel8a9a7dc2009-04-15 00:10:26 +00004781//// RecordInlinedFnStart - Global variable GV is inlined at the location marked
Devang Patel88bf96e2009-04-13 17:02:03 +00004782//// by LabelID label.
Devang Patel8a9a7dc2009-04-15 00:10:26 +00004783void DwarfWriter::RecordInlinedFnStart(Instruction *I, DISubprogram &SP,
4784 unsigned LabelID, unsigned Src,
4785 unsigned Line, unsigned Col) {
4786 DD->RecordInlinedFnStart(I, SP, LabelID, Src, Line, Col);
Devang Patel88bf96e2009-04-13 17:02:03 +00004787}
4788
Devang Patel8a9a7dc2009-04-15 00:10:26 +00004789/// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
4790unsigned DwarfWriter::RecordInlinedFnEnd(DISubprogram &SP) {
4791 return DD->RecordInlinedFnEnd(SP);
4792}
4793
4794/// RecordVariableScope - Record scope for the variable declared by
4795/// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
4796void DwarfWriter::RecordVariableScope(DIVariable &DV,
4797 const MachineInstr *DeclareMI) {
4798 DD->RecordVariableScope(DV, DeclareMI);
4799}