blob: 5d6a70f62231dad2179c2f0fd2378c4242ec0c0e [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; }
Devang Patelda96ca52009-04-21 00:08:56 +00001167 virtual unsigned getLine() { assert ( 0 && "Unexpected scope!"); return 0; }
1168 virtual unsigned getColumn() { assert ( 0 && "Unexpected scope!"); return 0; }
1169 virtual unsigned getFile() { assert ( 0 && "Unexpected scope!"); return 0; }
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001170};
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 Patel7b60d552009-04-15 20:41:31 +00001274 // FunctionDbgScope - Top level scope for the current function.
Devang Patel4d1709e2009-01-08 02:33:41 +00001275 //
Devang Patel7b60d552009-04-15 20:41:31 +00001276 DbgScope *FunctionDbgScope;
Devang Patel4d1709e2009-01-08 02:33:41 +00001277
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.
Devang Patel7b60d552009-04-15 20:41:31 +00002040 FunctionDbgScope = 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.
Devang Patel7b60d552009-04-15 20:41:31 +00002055 assert (FunctionDbgScope && "Function scope info missing!");
2056 FunctionDbgScope->AddScope(Scope);
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002057 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;
Devang Patel88bcc9e2009-04-15 19:42:57 +00002094 if (MainCU && TAI->doesDwarfUsesInlineInfoSection()
2095 && Scope->isInlinedSubroutine()) {
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002096 ScopeDie = new DIE(DW_TAG_inlined_subroutine);
2097 DIE *Origin = MainCU->getDieMapSlotFor(Scope->getDesc().getGV());
2098 AddDIEntry(ScopeDie, DW_AT_abstract_origin, DW_FORM_ref4, Origin);
2099 AddUInt(ScopeDie, DW_AT_call_file, 0, Scope->getFile());
2100 AddUInt(ScopeDie, DW_AT_call_line, 0, Scope->getLine());
2101 AddUInt(ScopeDie, DW_AT_call_column, 0, Scope->getColumn());
Devang Patel4d1709e2009-01-08 02:33:41 +00002102 }
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002103 else
2104 ScopeDie = new DIE(DW_TAG_lexical_block);
2105
2106 // Add the scope bounds.
2107 if (StartID) {
2108 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2109 DWLabel("label", StartID));
2110 } else {
2111 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2112 DWLabel("func_begin", SubprogramCount));
2113 }
2114 if (EndID) {
2115 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2116 DWLabel("label", EndID));
2117 } else {
2118 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2119 DWLabel("func_end", SubprogramCount));
2120 }
2121
2122 // Add the scope contents.
2123 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2124 ParentDie->AddChild(ScopeDie);
Devang Patel4d1709e2009-01-08 02:33:41 +00002125 }
2126 }
2127 }
2128
Devang Patel7b60d552009-04-15 20:41:31 +00002129 /// ConstructFunctionDbgScope - Construct the scope for the subprogram.
Devang Patel4d1709e2009-01-08 02:33:41 +00002130 ///
Devang Patel7b60d552009-04-15 20:41:31 +00002131 void ConstructFunctionDbgScope(DbgScope *RootScope) {
Devang Patel4d1709e2009-01-08 02:33:41 +00002132 // Exit if there is no root scope.
2133 if (!RootScope) return;
Devang Patel2560d922009-01-15 18:25:17 +00002134 DIDescriptor Desc = RootScope->getDesc();
2135 if (Desc.isNull())
2136 return;
Devang Patel4d1709e2009-01-08 02:33:41 +00002137
2138 // Get the subprogram debug information entry.
Devang Patel2560d922009-01-15 18:25:17 +00002139 DISubprogram SPD(Desc.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002140
2141 // Get the compile unit context.
Devang Patel2ae1db52009-01-30 18:20:31 +00002142 CompileUnit *Unit = MainCU;
2143 if (!Unit)
2144 Unit = FindCompileUnit(SPD.getCompileUnit());
Devang Patel4d1709e2009-01-08 02:33:41 +00002145
2146 // Get the subprogram die.
Devang Patel57ec9ac2009-01-12 22:58:14 +00002147 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002148 assert(SPDie && "Missing subprogram descriptor");
2149
2150 // Add the function bounds.
2151 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2152 DWLabel("func_begin", SubprogramCount));
2153 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2154 DWLabel("func_end", SubprogramCount));
2155 MachineLocation Location(RI->getFrameRegister(*MF));
2156 AddAddress(SPDie, DW_AT_frame_base, Location);
2157
2158 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2159 }
2160
2161 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2162 ///
2163 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Cheng3e288912009-02-25 07:04:34 +00002164 const char *FnName = MF->getFunction()->getNameStart();
2165 if (MainCU) {
Bill Wendlinge06da442009-04-09 21:49:15 +00002166 StringMap<DIE*> &Globals = MainCU->getGlobals();
2167 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Cheng3e288912009-02-25 07:04:34 +00002168 if (GI != Globals.end()) {
2169 DIE *SPDie = GI->second;
Devang Patel4d1709e2009-01-08 02:33:41 +00002170
2171 // Add the function bounds.
2172 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2173 DWLabel("func_begin", SubprogramCount));
2174 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2175 DWLabel("func_end", SubprogramCount));
2176
2177 MachineLocation Location(RI->getFrameRegister(*MF));
2178 AddAddress(SPDie, DW_AT_frame_base, Location);
2179 return;
2180 }
Evan Cheng3e288912009-02-25 07:04:34 +00002181 } else {
2182 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2183 CompileUnit *Unit = CompileUnits[i];
Bill Wendlinge06da442009-04-09 21:49:15 +00002184 StringMap<DIE*> &Globals = Unit->getGlobals();
2185 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Cheng3e288912009-02-25 07:04:34 +00002186 if (GI != Globals.end()) {
2187 DIE *SPDie = GI->second;
2188
2189 // Add the function bounds.
2190 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2191 DWLabel("func_begin", SubprogramCount));
2192 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2193 DWLabel("func_end", SubprogramCount));
2194
2195 MachineLocation Location(RI->getFrameRegister(*MF));
2196 AddAddress(SPDie, DW_AT_frame_base, Location);
2197 return;
2198 }
2199 }
Devang Patel4d1709e2009-01-08 02:33:41 +00002200 }
Evan Cheng3e288912009-02-25 07:04:34 +00002201
Devang Patel4d1709e2009-01-08 02:33:41 +00002202#if 0
2203 // FIXME: This is causing an abort because C++ mangled names are compared
2204 // with their unmangled counterparts. See PR2885. Don't do this assert.
2205 assert(0 && "Couldn't find DIE for machine function!");
2206#endif
Evan Cheng3e288912009-02-25 07:04:34 +00002207 return;
Devang Patel4d1709e2009-01-08 02:33:41 +00002208 }
2209
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002210 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2211 /// tools to recognize the object file contains Dwarf information.
2212 void EmitInitial() {
2213 // Check to see if we already emitted intial headers.
2214 if (didInitial) return;
2215 didInitial = true;
aslc200b112008-08-16 12:57:46 +00002216
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002217 // Dwarf sections base addresses.
2218 if (TAI->doesDwarfRequireFrameSection()) {
2219 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2220 EmitLabel("section_debug_frame", 0);
2221 }
2222 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2223 EmitLabel("section_info", 0);
2224 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2225 EmitLabel("section_abbrev", 0);
2226 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2227 EmitLabel("section_aranges", 0);
Scott Michel79f01f52009-01-26 22:32:51 +00002228 if (TAI->doesSupportMacInfoSection()) {
2229 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2230 EmitLabel("section_macinfo", 0);
2231 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002232 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2233 EmitLabel("section_line", 0);
2234 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2235 EmitLabel("section_loc", 0);
2236 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2237 EmitLabel("section_pubnames", 0);
2238 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2239 EmitLabel("section_str", 0);
2240 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2241 EmitLabel("section_ranges", 0);
2242
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002243 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002244 EmitLabel("text_begin", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00002245 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002246 EmitLabel("data_begin", 0);
2247 }
2248
2249 /// EmitDIE - Recusively Emits a debug information entry.
2250 ///
2251 void EmitDIE(DIE *Die) {
2252 // Get the abbreviation for this DIE.
2253 unsigned AbbrevNumber = Die->getAbbrevNumber();
2254 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
aslc200b112008-08-16 12:57:46 +00002255
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002256 Asm->EOL();
2257
2258 // Emit the code (index) for the abbreviation.
2259 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng0eeed442008-07-01 23:18:29 +00002260
Evan Cheng42ceb472009-03-25 01:47:28 +00002261 if (Asm->isVerbose())
Evan Cheng0eeed442008-07-01 23:18:29 +00002262 Asm->EOL(std::string("Abbrev [" +
2263 utostr(AbbrevNumber) +
2264 "] 0x" + utohexstr(Die->getOffset()) +
2265 ":0x" + utohexstr(Die->getSize()) + " " +
2266 TagString(Abbrev->getTag())));
2267 else
2268 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00002269
Owen Anderson88dd6232008-06-24 21:44:59 +00002270 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2271 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
aslc200b112008-08-16 12:57:46 +00002272
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002273 // Emit the DIE attribute values.
2274 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2275 unsigned Attr = AbbrevData[i].getAttribute();
2276 unsigned Form = AbbrevData[i].getForm();
2277 assert(Form && "Too many attributes for DIE (check abbreviation)");
aslc200b112008-08-16 12:57:46 +00002278
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002279 switch (Attr) {
2280 case DW_AT_sibling: {
2281 Asm->EmitInt32(Die->SiblingOffset());
2282 break;
2283 }
2284 default: {
2285 // Emit an attribute using the defined form.
2286 Values[i]->EmitValue(*this, Form);
2287 break;
2288 }
2289 }
aslc200b112008-08-16 12:57:46 +00002290
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002291 Asm->EOL(AttributeString(Attr));
2292 }
aslc200b112008-08-16 12:57:46 +00002293
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002294 // Emit the DIE children if any.
2295 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2296 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002297
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002298 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2299 EmitDIE(Children[j]);
2300 }
aslc200b112008-08-16 12:57:46 +00002301
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002302 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2303 }
2304 }
2305
2306 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2307 ///
2308 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2309 // Get the children.
2310 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002311
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002312 // If not last sibling and has children then add sibling offset attribute.
2313 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2314
2315 // Record the abbreviation.
2316 AssignAbbrevNumber(Die->getAbbrev());
aslc200b112008-08-16 12:57:46 +00002317
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002318 // Get the abbreviation for this DIE.
2319 unsigned AbbrevNumber = Die->getAbbrevNumber();
2320 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2321
2322 // Set DIE offset
2323 Die->setOffset(Offset);
aslc200b112008-08-16 12:57:46 +00002324
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002325 // Start the size with the size of abbreviation code.
aslc200b112008-08-16 12:57:46 +00002326 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2327
Owen Anderson88dd6232008-06-24 21:44:59 +00002328 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2329 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002330
2331 // Size the DIE attribute values.
2332 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2333 // Size attribute value.
2334 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2335 }
aslc200b112008-08-16 12:57:46 +00002336
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002337 // Size the DIE children if any.
2338 if (!Children.empty()) {
2339 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2340 "Children flag not set");
aslc200b112008-08-16 12:57:46 +00002341
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002342 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2343 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2344 }
aslc200b112008-08-16 12:57:46 +00002345
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002346 // End of children marker.
2347 Offset += sizeof(int8_t);
2348 }
2349
2350 Die->setSize(Offset - Die->getOffset());
2351 return Offset;
2352 }
2353
2354 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2355 ///
2356 void SizeAndOffsets() {
2357 // Process base compile unit.
Devang Patel2ae1db52009-01-30 18:20:31 +00002358 if (MainCU) {
2359 // Compute size of compile unit header
2360 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2361 sizeof(int16_t) + // DWARF version number
2362 sizeof(int32_t) + // Offset Into Abbrev. Section
2363 sizeof(int8_t); // Pointer Size (in bytes)
2364 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
2365 return;
2366 }
Evan Cheng3e288912009-02-25 07:04:34 +00002367 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2368 CompileUnit *Unit = CompileUnits[i];
Devang Patel6eae2832009-01-12 23:05:55 +00002369 // Compute size of compile unit header
2370 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2371 sizeof(int16_t) + // DWARF version number
2372 sizeof(int32_t) + // Offset Into Abbrev. Section
2373 sizeof(int8_t); // Pointer Size (in bytes)
2374 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2375 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002376 }
2377
Evan Cheng3e288912009-02-25 07:04:34 +00002378 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002379 ///
Evan Cheng3e288912009-02-25 07:04:34 +00002380 void EmitDebugInfoPerCU(CompileUnit *Unit) {
2381 DIE *Die = Unit->getDie();
2382 // Emit the compile units header.
2383 EmitLabel("info_begin", Unit->getID());
2384 // Emit size of content not including length itself
2385 unsigned ContentSize = Die->getSize() +
2386 sizeof(int16_t) + // DWARF version number
2387 sizeof(int32_t) + // Offset Into Abbrev. Section
2388 sizeof(int8_t) + // Pointer Size (in bytes)
2389 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2390
2391 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2392 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2393 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2394 Asm->EOL("Offset Into Abbrev. Section");
2395 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2396
2397 EmitDIE(Die);
2398 // FIXME - extra padding for gdb bug.
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 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2403 EmitLabel("info_end", Unit->getID());
2404
2405 Asm->EOL();
2406 }
2407
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002408 void EmitDebugInfo() {
2409 // Start debug info section.
2410 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
aslc200b112008-08-16 12:57:46 +00002411
Evan Cheng3e288912009-02-25 07:04:34 +00002412 if (MainCU) {
2413 EmitDebugInfoPerCU(MainCU);
2414 return;
Devang Patel6eae2832009-01-12 23:05:55 +00002415 }
Evan Cheng3e288912009-02-25 07:04:34 +00002416
2417 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2418 EmitDebugInfoPerCU(CompileUnits[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002419 }
2420
2421 /// EmitAbbreviations - Emit the abbreviation section.
2422 ///
2423 void EmitAbbreviations() const {
2424 // Check to see if it is worth the effort.
2425 if (!Abbreviations.empty()) {
2426 // Start the debug abbrev section.
2427 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
aslc200b112008-08-16 12:57:46 +00002428
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002429 EmitLabel("abbrev_begin", 0);
aslc200b112008-08-16 12:57:46 +00002430
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002431 // For each abbrevation.
2432 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2433 // Get abbreviation data
2434 const DIEAbbrev *Abbrev = Abbreviations[i];
aslc200b112008-08-16 12:57:46 +00002435
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002436 // Emit the abbrevations code (base 1 index.)
2437 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2438 Asm->EOL("Abbreviation Code");
aslc200b112008-08-16 12:57:46 +00002439
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002440 // Emit the abbreviations data.
2441 Abbrev->Emit(*this);
aslc200b112008-08-16 12:57:46 +00002442
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002443 Asm->EOL();
2444 }
aslc200b112008-08-16 12:57:46 +00002445
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002446 // Mark end of abbreviations.
2447 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2448
2449 EmitLabel("abbrev_end", 0);
aslc200b112008-08-16 12:57:46 +00002450
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002451 Asm->EOL();
2452 }
2453 }
2454
Bill Wendling1983a2a2008-07-20 00:11:19 +00002455 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2456 /// the line matrix.
aslc200b112008-08-16 12:57:46 +00002457 ///
Bill Wendling1983a2a2008-07-20 00:11:19 +00002458 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2459 // Define last address of section.
2460 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2461 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2462 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2463 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2464
2465 // Mark end of matrix.
2466 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2467 Asm->EmitULEB128Bytes(1); Asm->EOL();
2468 Asm->EmitInt8(1); Asm->EOL();
2469 }
2470
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002471 /// EmitDebugLines - Emit source line information.
2472 ///
2473 void EmitDebugLines() {
Bill Wendling1983a2a2008-07-20 00:11:19 +00002474 // If the target is using .loc/.file, the assembler will be emitting the
2475 // .debug_line table automatically.
2476 if (TAI->hasDotLocAndDotFile())
Dan Gohmanc55b34a2007-09-24 21:43:52 +00002477 return;
2478
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002479 // Minimum line delta, thus ranging from -10..(255-10).
2480 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2481 // Maximum line delta, thus ranging from -10..(255-10).
2482 const int MaxLineDelta = 255 + MinLineDelta;
2483
2484 // Start the dwarf line section.
2485 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
aslc200b112008-08-16 12:57:46 +00002486
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002487 // Construct the section header.
aslc200b112008-08-16 12:57:46 +00002488
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002489 EmitDifference("line_end", 0, "line_begin", 0, true);
2490 Asm->EOL("Length of Source Line Info");
2491 EmitLabel("line_begin", 0);
aslc200b112008-08-16 12:57:46 +00002492
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002493 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
aslc200b112008-08-16 12:57:46 +00002494
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002495 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2496 Asm->EOL("Prolog Length");
2497 EmitLabel("line_prolog_begin", 0);
aslc200b112008-08-16 12:57:46 +00002498
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002499 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2500
2501 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2502
2503 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
aslc200b112008-08-16 12:57:46 +00002504
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002505 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2506
2507 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
aslc200b112008-08-16 12:57:46 +00002508
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002509 // Line number standard opcode encodings argument count
2510 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2511 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2512 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2513 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2514 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2515 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2516 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2517 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2518 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2519
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002520 // Emit directories.
Evan Cheng3e288912009-02-25 07:04:34 +00002521 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2522 Asm->EmitString(getSourceDirectoryName(DI));
2523 Asm->EOL("Directory");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002524 }
2525 Asm->EmitInt8(0); Asm->EOL("End of directories");
aslc200b112008-08-16 12:57:46 +00002526
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002527 // Emit files.
Evan Cheng3e288912009-02-25 07:04:34 +00002528 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2529 // Remember source id starts at 1.
Bill Wendlingdf25fd62009-03-10 21:59:25 +00002530 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Evan Cheng3e288912009-02-25 07:04:34 +00002531 Asm->EmitString(getSourceFileName(Id.second));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002532 Asm->EOL("Source");
Evan Cheng3e288912009-02-25 07:04:34 +00002533 Asm->EmitULEB128Bytes(Id.first);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002534 Asm->EOL("Directory #");
2535 Asm->EmitULEB128Bytes(0);
2536 Asm->EOL("Mod date");
2537 Asm->EmitULEB128Bytes(0);
2538 Asm->EOL("File size");
2539 }
2540 Asm->EmitInt8(0); Asm->EOL("End of files");
aslc200b112008-08-16 12:57:46 +00002541
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002542 EmitLabel("line_prolog_end", 0);
aslc200b112008-08-16 12:57:46 +00002543
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002544 // A sequence for each text section.
Bill Wendling1983a2a2008-07-20 00:11:19 +00002545 unsigned SecSrcLinesSize = SectionSourceLines.size();
2546
2547 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002548 // Isolate current sections line info.
Devang Patel35a078f2009-01-12 22:54:42 +00002549 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng0eeed442008-07-01 23:18:29 +00002550
Evan Cheng42ceb472009-03-25 01:47:28 +00002551 if (Asm->isVerbose()) {
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002552 const Section* S = SectionMap[j + 1];
Evan Cheng3e288912009-02-25 07:04:34 +00002553 O << '\t' << TAI->getCommentString() << " Section"
2554 << S->getName() << '\n';
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002555 } else
Evan Cheng0eeed442008-07-01 23:18:29 +00002556 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002557
2558 // Dwarf assumes we start with first line of first source file.
2559 unsigned Source = 1;
2560 unsigned Line = 1;
aslc200b112008-08-16 12:57:46 +00002561
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002562 // Construct rows of the address, source, line, column matrix.
2563 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patel35a078f2009-01-12 22:54:42 +00002564 const SrcLineInfo &LineInfo = LineInfos[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002565 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2566 if (!LabelID) continue;
aslc200b112008-08-16 12:57:46 +00002567
Evan Cheng42ceb472009-03-25 01:47:28 +00002568 if (!Asm->isVerbose())
Evan Cheng0eeed442008-07-01 23:18:29 +00002569 Asm->EOL();
Evan Cheng3e288912009-02-25 07:04:34 +00002570 else {
2571 std::pair<unsigned, unsigned> SourceID =
Bill Wendlingdf25fd62009-03-10 21:59:25 +00002572 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Evan Cheng3e288912009-02-25 07:04:34 +00002573 O << '\t' << TAI->getCommentString() << ' '
2574 << getSourceDirectoryName(SourceID.first) << ' '
2575 << getSourceFileName(SourceID.second)
2576 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2577 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002578
2579 // Define the line address.
2580 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohmancfb72b22007-09-27 23:12:31 +00002581 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002582 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2583 EmitReference("label", LabelID); Asm->EOL("Location label");
aslc200b112008-08-16 12:57:46 +00002584
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002585 // If change of source, then switch to the new source.
2586 if (Source != LineInfo.getSourceID()) {
2587 Source = LineInfo.getSourceID();
2588 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2589 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2590 }
aslc200b112008-08-16 12:57:46 +00002591
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002592 // If change of line.
2593 if (Line != LineInfo.getLine()) {
2594 // Determine offset.
2595 int Offset = LineInfo.getLine() - Line;
2596 int Delta = Offset - MinLineDelta;
aslc200b112008-08-16 12:57:46 +00002597
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002598 // Update line.
2599 Line = LineInfo.getLine();
aslc200b112008-08-16 12:57:46 +00002600
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002601 // If delta is small enough and in range...
2602 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2603 // ... then use fast opcode.
2604 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2605 } else {
2606 // ... otherwise use long hand.
2607 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2608 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2609 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2610 }
2611 } else {
2612 // Copy the previous row (different address or source)
2613 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2614 }
2615 }
2616
Bill Wendling1983a2a2008-07-20 00:11:19 +00002617 EmitEndOfLineMatrix(j + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002618 }
Bill Wendling1983a2a2008-07-20 00:11:19 +00002619
2620 if (SecSrcLinesSize == 0)
2621 // Because we're emitting a debug_line section, we still need a line
2622 // table. The linker and friends expect it to exist. If there's nothing to
2623 // put into it, emit an empty table.
2624 EmitEndOfLineMatrix(1);
aslc200b112008-08-16 12:57:46 +00002625
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002626 EmitLabel("line_end", 0);
aslc200b112008-08-16 12:57:46 +00002627
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002628 Asm->EOL();
2629 }
aslc200b112008-08-16 12:57:46 +00002630
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002631 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2632 ///
2633 void EmitCommonDebugFrame() {
2634 if (!TAI->doesDwarfRequireFrameSection())
2635 return;
2636
2637 int stackGrowth =
2638 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2639 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00002640 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002641
2642 // Start the dwarf frame section.
2643 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2644
2645 EmitLabel("debug_frame_common", 0);
2646 EmitDifference("debug_frame_common_end", 0,
2647 "debug_frame_common_begin", 0, true);
2648 Asm->EOL("Length of Common Information Entry");
2649
2650 EmitLabel("debug_frame_common_begin", 0);
2651 Asm->EmitInt32((int)DW_CIE_ID);
2652 Asm->EOL("CIE Identifier Tag");
2653 Asm->EmitInt8(DW_CIE_VERSION);
2654 Asm->EOL("CIE Version");
2655 Asm->EmitString("");
2656 Asm->EOL("CIE Augmentation");
2657 Asm->EmitULEB128Bytes(1);
2658 Asm->EOL("CIE Code Alignment Factor");
2659 Asm->EmitSLEB128Bytes(stackGrowth);
aslc200b112008-08-16 12:57:46 +00002660 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00002661 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002662 Asm->EOL("CIE RA Column");
aslc200b112008-08-16 12:57:46 +00002663
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002664 std::vector<MachineMove> Moves;
2665 RI->getInitialFrameState(Moves);
2666
Dale Johannesenf5a11532007-11-13 19:13:01 +00002667 EmitFrameMoves(NULL, 0, Moves, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002668
Evan Cheng7e7d1942008-02-29 19:36:59 +00002669 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002670 EmitLabel("debug_frame_common_end", 0);
aslc200b112008-08-16 12:57:46 +00002671
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002672 Asm->EOL();
2673 }
2674
2675 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2676 /// section.
2677 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
2678 if (!TAI->doesDwarfRequireFrameSection())
2679 return;
aslc200b112008-08-16 12:57:46 +00002680
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002681 // Start the dwarf frame section.
2682 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
aslc200b112008-08-16 12:57:46 +00002683
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002684 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2685 "debug_frame_begin", DebugFrameInfo.Number, true);
2686 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00002687
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002688 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2689
2690 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2691 0, 0, true, false);
2692 Asm->EOL("FDE CIE offset");
2693
2694 EmitReference("func_begin", DebugFrameInfo.Number);
2695 Asm->EOL("FDE initial location");
2696 EmitDifference("func_end", DebugFrameInfo.Number,
2697 "func_begin", DebugFrameInfo.Number);
2698 Asm->EOL("FDE address range");
aslc200b112008-08-16 12:57:46 +00002699
Devang Patelb28de842009-01-17 08:01:33 +00002700 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00002701 false);
aslc200b112008-08-16 12:57:46 +00002702
Evan Cheng7e7d1942008-02-29 19:36:59 +00002703 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002704 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2705
2706 Asm->EOL();
2707 }
2708
Evan Cheng3e288912009-02-25 07:04:34 +00002709 void EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2710 EmitDifference("pubnames_end", Unit->getID(),
2711 "pubnames_begin", Unit->getID(), true);
2712 Asm->EOL("Length of Public Names Info");
2713
2714 EmitLabel("pubnames_begin", Unit->getID());
2715
2716 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2717
2718 EmitSectionOffset("info_begin", "section_info",
2719 Unit->getID(), 0, true, false);
2720 Asm->EOL("Offset of Compilation Unit Info");
2721
2722 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2723 true);
2724 Asm->EOL("Compilation Unit Length");
2725
Bill Wendlinge06da442009-04-09 21:49:15 +00002726 StringMap<DIE*> &Globals = Unit->getGlobals();
Bill Wendling3f94b412009-04-09 23:51:31 +00002727 for (StringMap<DIE*>::const_iterator
Bill Wendlinge06da442009-04-09 21:49:15 +00002728 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
Bill Wendling3f94b412009-04-09 23:51:31 +00002729 const char *Name = GI->getKeyData();
Evan Cheng3e288912009-02-25 07:04:34 +00002730 DIE * Entity = GI->second;
2731
2732 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
Bill Wendling3f94b412009-04-09 23:51:31 +00002733 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
Evan Cheng3e288912009-02-25 07:04:34 +00002734 }
2735
2736 Asm->EmitInt32(0); Asm->EOL("End Mark");
2737 EmitLabel("pubnames_end", Unit->getID());
2738
2739 Asm->EOL();
2740 }
2741
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002742 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2743 ///
2744 void EmitDebugPubNames() {
2745 // Start the dwarf pubnames section.
2746 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
aslc200b112008-08-16 12:57:46 +00002747
Evan Cheng3e288912009-02-25 07:04:34 +00002748 if (MainCU) {
2749 EmitDebugPubNamesPerCU(MainCU);
2750 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002751 }
Evan Cheng3e288912009-02-25 07:04:34 +00002752
2753 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2754 EmitDebugPubNamesPerCU(CompileUnits[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002755 }
2756
2757 /// EmitDebugStr - Emit visible names into a debug str section.
2758 ///
2759 void EmitDebugStr() {
2760 // Check to see if it is worth the effort.
2761 if (!StringPool.empty()) {
2762 // Start the dwarf str section.
2763 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
aslc200b112008-08-16 12:57:46 +00002764
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002765 // For each of strings in the string pool.
2766 for (unsigned StringID = 1, N = StringPool.size();
2767 StringID <= N; ++StringID) {
2768 // Emit a label for reference from debug information entries.
2769 EmitLabel("string", StringID);
2770 // Emit the string itself.
2771 const std::string &String = StringPool[StringID];
2772 Asm->EmitString(String); Asm->EOL();
2773 }
aslc200b112008-08-16 12:57:46 +00002774
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002775 Asm->EOL();
2776 }
2777 }
2778
2779 /// EmitDebugLoc - Emit visible names into a debug loc section.
2780 ///
2781 void EmitDebugLoc() {
2782 // Start the dwarf loc section.
2783 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
aslc200b112008-08-16 12:57:46 +00002784
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002785 Asm->EOL();
2786 }
2787
2788 /// EmitDebugARanges - Emit visible names into a debug aranges section.
2789 ///
2790 void EmitDebugARanges() {
2791 // Start the dwarf aranges section.
2792 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
aslc200b112008-08-16 12:57:46 +00002793
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002794 // FIXME - Mock up
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002795#if 0
aslc200b112008-08-16 12:57:46 +00002796 CompileUnit *Unit = GetBaseCompileUnit();
2797
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002798 // Don't include size of length
2799 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
aslc200b112008-08-16 12:57:46 +00002800
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002801 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
aslc200b112008-08-16 12:57:46 +00002802
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002803 EmitReference("info_begin", Unit->getID());
2804 Asm->EOL("Offset of Compilation Unit Info");
2805
Dan Gohmancfb72b22007-09-27 23:12:31 +00002806 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002807
2808 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2809
2810 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2811 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2812
2813 // Range 1
2814 EmitReference("text_begin", 0); Asm->EOL("Address");
2815 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2816
2817 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2818 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002819#endif
aslc200b112008-08-16 12:57:46 +00002820
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002821 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002822 }
2823
2824 /// EmitDebugRanges - Emit visible names into a debug ranges section.
2825 ///
2826 void EmitDebugRanges() {
2827 // Start the dwarf ranges section.
2828 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
aslc200b112008-08-16 12:57:46 +00002829
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002830 Asm->EOL();
2831 }
2832
2833 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2834 ///
2835 void EmitDebugMacInfo() {
Scott Michel79f01f52009-01-26 22:32:51 +00002836 if (TAI->doesSupportMacInfoSection()) {
2837 // Start the dwarf macinfo section.
2838 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
aslc200b112008-08-16 12:57:46 +00002839
Scott Michel79f01f52009-01-26 22:32:51 +00002840 Asm->EOL();
2841 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002842 }
2843
Devang Patel88bf96e2009-04-13 17:02:03 +00002844 /// EmitDebugInlineInfo - Emit inline info using following format.
2845 /// Section Header:
2846 /// 1. length of section
2847 /// 2. Dwarf version number
2848 /// 3. address size.
2849 ///
2850 /// Entries (one "entry" for each function that was inlined):
2851 ///
2852 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
2853 /// otherwise offset into __debug_str for regular function name.
2854 /// 2. offset into __debug_str section for regular function name.
2855 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
2856 /// instances for the function.
2857 ///
2858 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
2859 /// inlined instance; the die_offset points to the inlined_subroutine die in
2860 /// the __debug_info section, and the low_pc is the starting address for the
2861 /// inlining instance.
2862 void EmitDebugInlineInfo() {
2863 if (!TAI->doesDwarfUsesInlineInfoSection())
2864 return;
2865
2866 if (!MainCU)
2867 return;
2868
2869 Asm->SwitchToDataSection(TAI->getDwarfDebugInlineSection());
2870 Asm->EOL();
2871 EmitDifference("debug_inlined_end", 1,
2872 "debug_inlined_begin", 1, true);
2873 Asm->EOL("Length of Debug Inlined Information Entry");
2874
2875 EmitLabel("debug_inlined_begin", 1);
2876
2877 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
2878 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2879
2880 for (DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
2881 I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2882 GlobalVariable *GV = I->first;
2883 SmallVector<unsigned, 4> &Labels = I->second;
2884 DISubprogram SP(GV);
2885 std::string Name;
2886 std::string LName;
2887
2888 SP.getLinkageName(LName);
2889 SP.getName(Name);
2890
2891 Asm->EmitString(LName.empty() ? Name : LName);
2892 Asm->EOL("MIPS linkage name");
2893
2894 Asm->EmitString(Name); Asm->EOL("Function name");
2895
2896 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2897
2898 for (SmallVector<unsigned, 4>::iterator LI = Labels.begin(),
2899 LE = Labels.end(); LI != LE; ++LI) {
2900 DIE *SP = MainCU->getDieMapSlotFor(GV);
2901 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2902
2903 if (TD->getPointerSize() == sizeof(int32_t))
2904 O << TAI->getData32bitsDirective();
2905 else
2906 O << TAI->getData64bitsDirective();
2907 PrintLabelName("label", *LI); Asm->EOL("low_pc");
2908 }
2909 }
2910
2911 EmitLabel("debug_inlined_end", 1);
2912 Asm->EOL();
2913 }
2914
Bill Wendling278a3922009-03-10 21:47:45 +00002915 /// GetOrCreateSourceID - Look up the source id with the given directory and
2916 /// source file names. If none currently exists, create a new id and insert it
2917 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
2918 /// as well.
2919 unsigned GetOrCreateSourceID(const std::string &DirName,
2920 const std::string &FileName) {
2921 unsigned DId;
2922 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
2923 if (DI != DirectoryIdMap.end()) {
2924 DId = DI->getValue();
2925 } else {
2926 DId = DirectoryNames.size() + 1;
2927 DirectoryIdMap[DirName] = DId;
2928 DirectoryNames.push_back(DirName);
2929 }
2930
2931 unsigned FId;
2932 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
2933 if (FI != SourceFileIdMap.end()) {
2934 FId = FI->getValue();
2935 } else {
2936 FId = SourceFileNames.size() + 1;
2937 SourceFileIdMap[FileName] = FId;
2938 SourceFileNames.push_back(FileName);
2939 }
2940
2941 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
2942 SourceIdMap.find(std::make_pair(DId, FId));
2943 if (SI != SourceIdMap.end())
2944 return SI->second;
2945
2946 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
2947 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
2948 SourceIds.push_back(std::make_pair(DId, FId));
2949
2950 return SrcId;
2951 }
2952
Evan Cheng3e288912009-02-25 07:04:34 +00002953 void ConstructCompileUnit(GlobalVariable *GV) {
2954 DICompileUnit DIUnit(GV);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00002955 std::string Dir, FN, Prod;
2956 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
2957 DIUnit.getFilename(FN));
Evan Cheng3e288912009-02-25 07:04:34 +00002958
2959 DIE *Die = new DIE(DW_TAG_compile_unit);
2960 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2961 DWLabel("section_line", 0), DWLabel("section_line", 0),
2962 false);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00002963 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer(Prod));
Evan Cheng3e288912009-02-25 07:04:34 +00002964 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendling1c5842b2009-03-09 05:04:40 +00002965 AddString(Die, DW_AT_name, DW_FORM_string, FN);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00002966 if (!Dir.empty())
Bill Wendling1c5842b2009-03-09 05:04:40 +00002967 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Cheng3e288912009-02-25 07:04:34 +00002968 if (DIUnit.isOptimized())
2969 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00002970 std::string Flags;
2971 DIUnit.getFlags(Flags);
2972 if (!Flags.empty())
Evan Cheng3e288912009-02-25 07:04:34 +00002973 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
2974 unsigned RVer = DIUnit.getRunTimeVersion();
2975 if (RVer)
2976 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
2977
2978 CompileUnit *Unit = new CompileUnit(ID, Die);
2979 if (DIUnit.isMain()) {
2980 assert(!MainCU && "Multiple main compile units are found!");
2981 MainCU = Unit;
2982 }
2983 CompileUnitMap[DIUnit.getGV()] = Unit;
2984 CompileUnits.push_back(Unit);
2985 }
2986
Devang Patel289f2362009-01-05 23:11:11 +00002987 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Patelb3907da2009-01-05 23:03:32 +00002988 void ConstructCompileUnits() {
Evan Cheng3e288912009-02-25 07:04:34 +00002989 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.compile_units");
2990 if (!Root)
2991 return;
2992 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2993 "Malformed compile unit descriptor anchor type");
2994 Constant *RootC = cast<Constant>(*Root->use_begin());
2995 assert(RootC->hasNUsesOrMore(1) &&
2996 "Malformed compile unit descriptor anchor type");
2997 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2998 UI != UE; ++UI)
2999 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3000 UUI != UUE; ++UUI) {
3001 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3002 ConstructCompileUnit(GV);
Devang Patel2ae1db52009-01-30 18:20:31 +00003003 }
Evan Cheng3e288912009-02-25 07:04:34 +00003004 }
3005
3006 bool ConstructGlobalVariableDIE(GlobalVariable *GV) {
3007 DIGlobalVariable DI_GV(GV);
3008 CompileUnit *DW_Unit = MainCU;
3009 if (!DW_Unit)
3010 DW_Unit = FindCompileUnit(DI_GV.getCompileUnit());
3011
3012 // Check for pre-existence.
3013 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
3014 if (Slot)
3015 return false;
3016
3017 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
3018
3019 // Add address.
3020 DIEBlock *Block = new DIEBlock();
3021 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Bill Wendling26a8ab92009-04-10 00:12:49 +00003022 std::string GLN;
Evan Cheng3e288912009-02-25 07:04:34 +00003023 AddObjectLabel(Block, 0, DW_FORM_udata,
Bill Wendling26a8ab92009-04-10 00:12:49 +00003024 Asm->getGlobalLinkName(DI_GV.getGlobal(), GLN));
Evan Cheng3e288912009-02-25 07:04:34 +00003025 AddBlock(VariableDie, DW_AT_location, 0, Block);
3026
3027 // Add to map.
3028 Slot = VariableDie;
3029 // Add to context owner.
3030 DW_Unit->getDie()->AddChild(VariableDie);
3031 // Expose as global. FIXME - need to check external flag.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00003032 std::string Name;
3033 DW_Unit->AddGlobal(DI_GV.getName(Name), VariableDie);
Evan Cheng3e288912009-02-25 07:04:34 +00003034 return true;
Devang Patelb3907da2009-01-05 23:03:32 +00003035 }
3036
Devang Patel289f2362009-01-05 23:11:11 +00003037 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patela9169c32009-02-24 00:02:15 +00003038 /// visible global variables. Return true if at least one global DIE is
3039 /// created.
3040 bool ConstructGlobalVariableDIEs() {
Evan Cheng3e288912009-02-25 07:04:34 +00003041 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
3042 if (!Root)
3043 return false;
Devang Patel289f2362009-01-05 23:11:11 +00003044
Evan Cheng3e288912009-02-25 07:04:34 +00003045 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3046 "Malformed global variable descriptor anchor type");
3047 Constant *RootC = cast<Constant>(*Root->use_begin());
3048 assert(RootC->hasNUsesOrMore(1) &&
3049 "Malformed global variable descriptor anchor type");
Devang Patel289f2362009-01-05 23:11:11 +00003050
Evan Cheng3e288912009-02-25 07:04:34 +00003051 bool Result = false;
3052 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3053 UI != UE; ++UI)
3054 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3055 UUI != UUE; ++UUI) {
3056 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3057 Result |= ConstructGlobalVariableDIE(GV);
3058 }
3059 return Result;
3060 }
Devang Patel289f2362009-01-05 23:11:11 +00003061
Evan Cheng3e288912009-02-25 07:04:34 +00003062 bool ConstructSubprogram(GlobalVariable *GV) {
3063 DISubprogram SP(GV);
3064 CompileUnit *Unit = MainCU;
3065 if (!Unit)
3066 Unit = FindCompileUnit(SP.getCompileUnit());
Devang Patel289f2362009-01-05 23:11:11 +00003067
Evan Cheng3e288912009-02-25 07:04:34 +00003068 // Check for pre-existence.
3069 DIE *&Slot = Unit->getDieMapSlotFor(GV);
3070 if (Slot)
3071 return false;
3072
3073 if (!SP.isDefinition())
3074 // This is a method declaration which will be handled while
3075 // constructing class type.
3076 return false;
3077
3078 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
3079
3080 // Add to map.
3081 Slot = SubprogramDie;
3082 // Add to context owner.
3083 Unit->getDie()->AddChild(SubprogramDie);
3084 // Expose as global.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00003085 std::string Name;
3086 Unit->AddGlobal(SP.getName(Name), SubprogramDie);
Evan Cheng3e288912009-02-25 07:04:34 +00003087 return true;
Devang Patel289f2362009-01-05 23:11:11 +00003088 }
3089
Devang Patele6caf012009-01-05 23:21:35 +00003090 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patela9169c32009-02-24 00:02:15 +00003091 /// subprograms. Return true if at least one subprogram DIE is created.
3092 bool ConstructSubprograms() {
Evan Cheng3e288912009-02-25 07:04:34 +00003093 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
3094 if (!Root)
3095 return false;
Devang Patele6caf012009-01-05 23:21:35 +00003096
Evan Cheng3e288912009-02-25 07:04:34 +00003097 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3098 "Malformed subprogram descriptor anchor type");
3099 Constant *RootC = cast<Constant>(*Root->use_begin());
3100 assert(RootC->hasNUsesOrMore(1) &&
3101 "Malformed subprogram descriptor anchor type");
Devang Patele6caf012009-01-05 23:21:35 +00003102
Evan Cheng3e288912009-02-25 07:04:34 +00003103 bool Result = false;
3104 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3105 UI != UE; ++UI)
3106 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3107 UUI != UUE; ++UUI) {
3108 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3109 Result |= ConstructSubprogram(GV);
3110 }
3111 return Result;
Devang Patele6caf012009-01-05 23:21:35 +00003112 }
3113
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003114public:
3115 //===--------------------------------------------------------------------===//
3116 // Main entry points.
3117 //
Owen Anderson847b99b2008-08-21 00:14:44 +00003118 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendlingd9308a62009-03-10 21:23:25 +00003119 : Dwarf(OS, A, T, "dbg"), MainCU(0),
3120 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
3121 ValuesSet(InitValuesSetSize), Values(), StringPool(), SectionMap(),
3122 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel7b60d552009-04-15 20:41:31 +00003123 FunctionDbgScope(0), DebugTimer(0) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003124 if (TimePassesIsEnabled)
3125 DebugTimer = new Timer("Dwarf Debug Writer",
Bill Wendling148ecc42009-03-10 22:58:53 +00003126 getDwarfTimerGroup());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003127 }
3128 virtual ~DwarfDebug() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003129 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3130 delete Values[j];
Bill Wendlingd9308a62009-03-10 21:23:25 +00003131
3132 delete DebugTimer;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003133 }
3134
Bill Wendling278a3922009-03-10 21:47:45 +00003135 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
3136 /// be emitted.
3137 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
3138
Devang Patel9304b382009-01-06 21:07:30 +00003139 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3140 /// This is inovked by the target AsmPrinter.
Devang Patel91d27b02009-01-12 23:09:42 +00003141 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003142 if (TimePassesIsEnabled)
3143 DebugTimer->startTimer();
3144
Bill Wendling6baa18d2009-02-03 21:38:21 +00003145 // Create all the compile unit DIEs.
3146 ConstructCompileUnits();
Devang Patel91d27b02009-01-12 23:09:42 +00003147
Bill Wendlingd9308a62009-03-10 21:23:25 +00003148 if (CompileUnits.empty()) {
3149 if (TimePassesIsEnabled)
Bill Wendling0be24752009-03-10 22:02:13 +00003150 DebugTimer->stopTimer();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003151
Bill Wendling6baa18d2009-02-03 21:38:21 +00003152 return;
Bill Wendlingd9308a62009-03-10 21:23:25 +00003153 }
Devang Patel91d27b02009-01-12 23:09:42 +00003154
Devang Patela9169c32009-02-24 00:02:15 +00003155 // Create DIEs for each of the externally visible global variables.
3156 bool globalDIEs = ConstructGlobalVariableDIEs();
3157
3158 // Create DIEs for each of the externally visible subprograms.
3159 bool subprogramDIEs = ConstructSubprograms();
3160
3161 // If there is not any debug info available for any global variables
3162 // and any subprograms then there is not any debug info to emit.
Bill Wendlingd9308a62009-03-10 21:23:25 +00003163 if (!globalDIEs && !subprogramDIEs) {
3164 if (TimePassesIsEnabled)
Bill Wendling0be24752009-03-10 22:02:13 +00003165 DebugTimer->stopTimer();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003166
Devang Patela9169c32009-02-24 00:02:15 +00003167 return;
Bill Wendlingd9308a62009-03-10 21:23:25 +00003168 }
Devang Patela9169c32009-02-24 00:02:15 +00003169
Bill Wendling6baa18d2009-02-03 21:38:21 +00003170 MMI = mmi;
3171 shouldEmit = true;
3172 MMI->setDebugInfoAvailability(true);
Devang Patel9304b382009-01-06 21:07:30 +00003173
Bill Wendling6baa18d2009-02-03 21:38:21 +00003174 // Prime section data.
3175 SectionMap.insert(TAI->getTextSection());
Devang Patel9304b382009-01-06 21:07:30 +00003176
Bill Wendling6baa18d2009-02-03 21:38:21 +00003177 // Print out .file directives to specify files for .loc directives. These
3178 // are printed out early so that they precede any .loc directives.
3179 if (TAI->hasDotLocAndDotFile()) {
Evan Cheng3e288912009-02-25 07:04:34 +00003180 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
3181 // Remember source id starts at 1.
Bill Wendlingdf25fd62009-03-10 21:59:25 +00003182 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Evan Cheng3e288912009-02-25 07:04:34 +00003183 sys::Path FullPath(getSourceDirectoryName(Id.first));
3184 bool AppendOk =
3185 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendling6baa18d2009-02-03 21:38:21 +00003186 assert(AppendOk && "Could not append filename to directory!");
3187 AppendOk = false;
3188 Asm->EmitFile(i, FullPath.toString());
3189 Asm->EOL();
Devang Patel9304b382009-01-06 21:07:30 +00003190 }
Bill Wendling6baa18d2009-02-03 21:38:21 +00003191 }
Devang Patel9304b382009-01-06 21:07:30 +00003192
Bill Wendling6baa18d2009-02-03 21:38:21 +00003193 // Emit initial sections
3194 EmitInitial();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003195
3196 if (TimePassesIsEnabled)
3197 DebugTimer->stopTimer();
Devang Patel9304b382009-01-06 21:07:30 +00003198 }
3199
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003200 /// BeginModule - Emit all Dwarf sections that should come prior to the
3201 /// content.
3202 void BeginModule(Module *M) {
3203 this->M = M;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003204 }
3205
3206 /// EndModule - Emit all Dwarf sections that should come after the content.
3207 ///
3208 void EndModule() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003209 if (!ShouldEmitDwarfDebug())
3210 return;
3211
3212 if (TimePassesIsEnabled)
3213 DebugTimer->startTimer();
aslc200b112008-08-16 12:57:46 +00003214
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003215 // Standard sections final addresses.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003216 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003217 EmitLabel("text_end", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00003218 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003219 EmitLabel("data_end", 0);
aslc200b112008-08-16 12:57:46 +00003220
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003221 // End text sections.
3222 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003223 Asm->SwitchToSection(SectionMap[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003224 EmitLabel("section_end", i);
3225 }
3226
3227 // Emit common frame information.
3228 EmitCommonDebugFrame();
3229
3230 // Emit function debug frame information
3231 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3232 E = DebugFrames.end(); I != E; ++I)
3233 EmitFunctionDebugFrame(*I);
3234
3235 // Compute DIE offsets and sizes.
3236 SizeAndOffsets();
aslc200b112008-08-16 12:57:46 +00003237
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003238 // Emit all the DIEs into a debug info section
3239 EmitDebugInfo();
aslc200b112008-08-16 12:57:46 +00003240
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003241 // Corresponding abbreviations into a abbrev section.
3242 EmitAbbreviations();
aslc200b112008-08-16 12:57:46 +00003243
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003244 // Emit source line correspondence into a debug line section.
3245 EmitDebugLines();
aslc200b112008-08-16 12:57:46 +00003246
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003247 // Emit info into a debug pubnames section.
3248 EmitDebugPubNames();
aslc200b112008-08-16 12:57:46 +00003249
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003250 // Emit info into a debug str section.
3251 EmitDebugStr();
aslc200b112008-08-16 12:57:46 +00003252
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003253 // Emit info into a debug loc section.
3254 EmitDebugLoc();
aslc200b112008-08-16 12:57:46 +00003255
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003256 // Emit info into a debug aranges section.
3257 EmitDebugARanges();
aslc200b112008-08-16 12:57:46 +00003258
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003259 // Emit info into a debug ranges section.
3260 EmitDebugRanges();
aslc200b112008-08-16 12:57:46 +00003261
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003262 // Emit info into a debug macinfo section.
3263 EmitDebugMacInfo();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003264
Devang Patel88bf96e2009-04-13 17:02:03 +00003265 // Emit inline info.
3266 EmitDebugInlineInfo();
3267
Bill Wendlingd9308a62009-03-10 21:23:25 +00003268 if (TimePassesIsEnabled)
3269 DebugTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003270 }
3271
aslc200b112008-08-16 12:57:46 +00003272 /// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003273 /// emitted immediately after the function entry point.
3274 void BeginFunction(MachineFunction *MF) {
Bill Wendlingc1d211d2009-03-11 00:03:50 +00003275 this->MF = MF;
3276
Bill Wendling50db0792009-02-20 00:44:43 +00003277 if (!ShouldEmitDwarfDebug()) return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003278
Bill Wendlingd9308a62009-03-10 21:23:25 +00003279 if (TimePassesIsEnabled)
3280 DebugTimer->startTimer();
3281
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003282 // Begin accumulating function debug information.
3283 MMI->BeginFunction(MF);
aslc200b112008-08-16 12:57:46 +00003284
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003285 // Assumes in correct section after the entry point.
3286 EmitLabel("func_begin", ++SubprogramCount);
Evan Chenga53c40a2008-02-01 09:10:45 +00003287
3288 // Emit label for the implicitly defined dbg.stoppoint at the start of
3289 // the function.
Devang Patel35a078f2009-01-12 22:54:42 +00003290 if (!Lines.empty()) {
3291 const SrcLineInfo &LineInfo = Lines[0];
Andrew Lenharth42f91402008-04-03 17:37:43 +00003292 Asm->printLabel(LineInfo.getLabelID());
3293 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00003294
3295 if (TimePassesIsEnabled)
3296 DebugTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003297 }
aslc200b112008-08-16 12:57:46 +00003298
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003299 /// EndFunction - Gather and emit post-function debug information.
3300 ///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003301 void EndFunction(MachineFunction *MF) {
Bill Wendling50db0792009-02-20 00:44:43 +00003302 if (!ShouldEmitDwarfDebug()) return;
aslc200b112008-08-16 12:57:46 +00003303
Bill Wendlingd9308a62009-03-10 21:23:25 +00003304 if (TimePassesIsEnabled)
3305 DebugTimer->startTimer();
3306
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003307 // Define end label for subprogram.
3308 EmitLabel("func_end", SubprogramCount);
aslc200b112008-08-16 12:57:46 +00003309
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003310 // Get function line info.
Devang Patel35a078f2009-01-12 22:54:42 +00003311 if (!Lines.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003312 // Get section line info.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003313 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003314 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patel35a078f2009-01-12 22:54:42 +00003315 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003316 // Append the function info to section info.
3317 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patel35a078f2009-01-12 22:54:42 +00003318 Lines.begin(), Lines.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003319 }
aslc200b112008-08-16 12:57:46 +00003320
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003321 // Construct scopes for subprogram.
Devang Patel7b60d552009-04-15 20:41:31 +00003322 if (FunctionDbgScope)
3323 ConstructFunctionDbgScope(FunctionDbgScope);
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003324 else
3325 // FIXME: This is wrong. We are essentially getting past a problem with
3326 // debug information not being able to handle unreachable blocks that have
3327 // debug information in them. In particular, those unreachable blocks that
3328 // have "region end" info in them. That situation results in the "root
3329 // scope" not being created. If that's the case, then emit a "default"
3330 // scope, i.e., one that encompasses the whole function. This isn't
3331 // desirable. And a better way of handling this (and all of the debugging
3332 // information) needs to be explored.
Devang Patel6ccd57e2009-01-13 00:20:51 +00003333 ConstructDefaultDbgScope(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003334
3335 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3336 MMI->getFrameMoves()));
Devang Patela4162952009-01-12 18:48:36 +00003337
3338 // Clear debug info
Devang Patel7b60d552009-04-15 20:41:31 +00003339 if (FunctionDbgScope) {
3340 delete FunctionDbgScope;
Devang Patela4162952009-01-12 18:48:36 +00003341 DbgScopeMap.clear();
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003342 DbgInlinedScopeMap.clear();
3343 InlinedVariableScopes.clear();
Devang Patel7b60d552009-04-15 20:41:31 +00003344 FunctionDbgScope = NULL;
Devang Patela4162952009-01-12 18:48:36 +00003345 }
Devang Patelcb59fd42009-01-12 19:17:34 +00003346
Bill Wendlingd9308a62009-03-10 21:23:25 +00003347 Lines.clear();
3348
3349 if (TimePassesIsEnabled)
3350 DebugTimer->stopTimer();
3351 }
Devang Patelcb59fd42009-01-12 19:17:34 +00003352
Devang Patel2da0cc42009-01-15 23:41:32 +00003353 /// ValidDebugInfo - Return true if V represents valid debug info value.
Bill Wendling58ed5d22009-04-29 00:15:41 +00003354 bool ValidDebugInfo(Value *V, unsigned OptLevel) {
Devang Patel208098b2009-01-19 23:21:49 +00003355 if (!V)
3356 return false;
3357
Devang Patel4d92dded2009-01-16 01:49:46 +00003358 if (!shouldEmit)
3359 return false;
3360
Devang Patel2da0cc42009-01-15 23:41:32 +00003361 GlobalVariable *GV = getGlobalVariable(V);
3362 if (!GV)
3363 return false;
Duncan Sands19d161f2009-03-07 15:45:40 +00003364
3365 if (!GV->hasInternalLinkage () && !GV->hasLinkOnceLinkage())
Devang Patel2da0cc42009-01-15 23:41:32 +00003366 return false;
3367
Bill Wendlingd9308a62009-03-10 21:23:25 +00003368 if (TimePassesIsEnabled)
3369 DebugTimer->startTimer();
3370
Devang Patel2da0cc42009-01-15 23:41:32 +00003371 DIDescriptor DI(GV);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003372
Devang Patel2da0cc42009-01-15 23:41:32 +00003373 // Check current version. Allow Version6 for now.
3374 unsigned Version = DI.getVersion();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003375 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6) {
3376 if (TimePassesIsEnabled)
3377 DebugTimer->stopTimer();
3378
Devang Patel2da0cc42009-01-15 23:41:32 +00003379 return false;
Bill Wendlingd9308a62009-03-10 21:23:25 +00003380 }
Devang Patel2da0cc42009-01-15 23:41:32 +00003381
Devang Patel208098b2009-01-19 23:21:49 +00003382 unsigned Tag = DI.getTag();
3383 switch (Tag) {
3384 case DW_TAG_variable:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003385 assert(DIVariable(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003386 break;
3387 case DW_TAG_compile_unit:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003388 assert(DICompileUnit(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003389 break;
3390 case DW_TAG_subprogram:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003391 assert(DISubprogram(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003392 break;
Devang Patel18992922009-04-13 18:13:16 +00003393 case DW_TAG_lexical_block:
3394 /// FIXME. This interfers with the qualitfy of generated code when
3395 /// during optimization.
Bill Wendling58ed5d22009-04-29 00:15:41 +00003396 if (OptLevel != 0)
Devang Patel18992922009-04-13 18:13:16 +00003397 return false;
Devang Patel208098b2009-01-19 23:21:49 +00003398 default:
3399 break;
3400 }
3401
Bill Wendlingd9308a62009-03-10 21:23:25 +00003402 if (TimePassesIsEnabled)
3403 DebugTimer->stopTimer();
3404
Devang Patel2da0cc42009-01-15 23:41:32 +00003405 return true;
3406 }
3407
Devang Patelcb59fd42009-01-12 19:17:34 +00003408 /// RecordSourceLine - Records location information and associates it with a
3409 /// label. Returns a unique label ID used to generate a label and provide
3410 /// correspondence to the source line list.
3411 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003412 if (TimePassesIsEnabled)
3413 DebugTimer->startTimer();
3414
Evan Cheng3e288912009-02-25 07:04:34 +00003415 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendling6baa18d2009-02-03 21:38:21 +00003416 assert(Unit && "Unable to find CompileUnit");
Devang Patelcb59fd42009-01-12 19:17:34 +00003417 unsigned ID = MMI->NextLabelID();
3418 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
Bill Wendlingd9308a62009-03-10 21:23:25 +00003419
3420 if (TimePassesIsEnabled)
3421 DebugTimer->stopTimer();
3422
Devang Patelcb59fd42009-01-12 19:17:34 +00003423 return ID;
3424 }
3425
3426 /// RecordSourceLine - Records location information and associates it with a
3427 /// label. Returns a unique label ID used to generate a label and provide
3428 /// correspondence to the source line list.
3429 unsigned RecordSourceLine(unsigned Line, unsigned Col, unsigned Src) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003430 if (TimePassesIsEnabled)
3431 DebugTimer->startTimer();
3432
Devang Patelcb59fd42009-01-12 19:17:34 +00003433 unsigned ID = MMI->NextLabelID();
3434 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
Bill Wendlingd9308a62009-03-10 21:23:25 +00003435
3436 if (TimePassesIsEnabled)
3437 DebugTimer->stopTimer();
3438
Devang Patelcb59fd42009-01-12 19:17:34 +00003439 return ID;
3440 }
3441
Bill Wendling278a3922009-03-10 21:47:45 +00003442 /// getRecordSourceLineCount - Return the number of source lines in the debug
3443 /// info.
3444 unsigned getRecordSourceLineCount() const {
Devang Patelcb59fd42009-01-12 19:17:34 +00003445 return Lines.size();
3446 }
3447
Bill Wendling278a3922009-03-10 21:47:45 +00003448 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
3449 /// timed. Look up the source id with the given directory and source file
3450 /// names. If none currently exists, create a new id and insert it in the
3451 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
3452 /// well.
Evan Cheng3e288912009-02-25 07:04:34 +00003453 unsigned getOrCreateSourceID(const std::string &DirName,
3454 const std::string &FileName) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003455 if (TimePassesIsEnabled)
3456 DebugTimer->startTimer();
3457
Bill Wendling278a3922009-03-10 21:47:45 +00003458 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003459
3460 if (TimePassesIsEnabled)
3461 DebugTimer->stopTimer();
3462
Evan Cheng3e288912009-02-25 07:04:34 +00003463 return SrcId;
Devang Patelcb59fd42009-01-12 19:17:34 +00003464 }
3465
3466 /// RecordRegionStart - Indicate the start of a region.
Devang Patelcb59fd42009-01-12 19:17:34 +00003467 unsigned RecordRegionStart(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003468 if (TimePassesIsEnabled)
3469 DebugTimer->startTimer();
3470
Devang Patelcb59fd42009-01-12 19:17:34 +00003471 DbgScope *Scope = getOrCreateScope(V);
3472 unsigned ID = MMI->NextLabelID();
3473 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003474
3475 if (TimePassesIsEnabled)
3476 DebugTimer->stopTimer();
3477
Devang Patelcb59fd42009-01-12 19:17:34 +00003478 return ID;
3479 }
3480
3481 /// RecordRegionEnd - Indicate the end of a region.
Devang Patelcb59fd42009-01-12 19:17:34 +00003482 unsigned RecordRegionEnd(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003483 if (TimePassesIsEnabled)
3484 DebugTimer->startTimer();
3485
Devang Patelcb59fd42009-01-12 19:17:34 +00003486 DbgScope *Scope = getOrCreateScope(V);
3487 unsigned ID = MMI->NextLabelID();
3488 Scope->setEndLabelID(ID);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003489
3490 if (TimePassesIsEnabled)
3491 DebugTimer->stopTimer();
3492
Devang Patelcb59fd42009-01-12 19:17:34 +00003493 return ID;
3494 }
3495
3496 /// RecordVariable - Indicate the declaration of a local variable.
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003497 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
3498 const MachineInstr *MI) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003499 if (TimePassesIsEnabled)
3500 DebugTimer->startTimer();
3501
Devang Patel2560d922009-01-15 18:25:17 +00003502 DIDescriptor Desc(GV);
3503 DbgScope *Scope = NULL;
Bill Wendlingd9308a62009-03-10 21:23:25 +00003504
Devang Patel2560d922009-01-15 18:25:17 +00003505 if (Desc.getTag() == DW_TAG_variable) {
3506 // GV is a global variable.
3507 DIGlobalVariable DG(GV);
3508 Scope = getOrCreateScope(DG.getContext().getGV());
3509 } else {
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003510 DenseMap<const MachineInstr *, DbgScope *>::iterator
3511 SI = InlinedVariableScopes.find(MI);
3512 if (SI != InlinedVariableScopes.end()) {
3513 // or GV is an inlined local variable.
3514 Scope = SI->second;
3515 } else {
Devang Patel2560d922009-01-15 18:25:17 +00003516 // or GV is a local variable.
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003517 DIVariable DV(GV);
3518 Scope = getOrCreateScope(DV.getContext().getGV());
3519 }
Devang Patel2560d922009-01-15 18:25:17 +00003520 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00003521
Bill Wendling6baa18d2009-02-03 21:38:21 +00003522 assert(Scope && "Unable to find variable' scope");
Devang Patel7c8a2772009-01-16 19:28:14 +00003523 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelcb59fd42009-01-12 19:17:34 +00003524 Scope->AddVariable(DV);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003525
3526 if (TimePassesIsEnabled)
3527 DebugTimer->stopTimer();
Devang Patelcb59fd42009-01-12 19:17:34 +00003528 }
Devang Patel88bf96e2009-04-13 17:02:03 +00003529
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003530 //// RecordInlinedFnStart - Indicate the start of inlined subroutine.
3531 void RecordInlinedFnStart(Instruction *FSI, DISubprogram &SP, unsigned LabelID,
3532 unsigned Src, unsigned Line, unsigned Col) {
3533 if (!TAI->doesDwarfUsesInlineInfoSection())
3534 return;
3535
3536 DbgScope *Scope = createInlinedSubroutineScope(SP, Src, Line, Col);
3537 Scope->setStartLabelID(LabelID);
Devang Patel88bf96e2009-04-13 17:02:03 +00003538 MMI->RecordUsedDbgLabel(LabelID);
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003539 GlobalVariable *GV = SP.getGV();
3540
3541 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3542 SI = DbgInlinedScopeMap.find(GV);
3543 if (SI == DbgInlinedScopeMap.end()) {
3544 SmallVector<DbgScope *, 2> Scopes;
3545 Scopes.push_back(Scope);
3546 DbgInlinedScopeMap[GV] = Scopes;
3547 } else {
3548 SmallVector<DbgScope *, 2> &Scopes = SI->second;
3549 Scopes.push_back(Scope);
3550 }
3551
Devang Patel88bf96e2009-04-13 17:02:03 +00003552 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
3553 I = InlineInfo.find(GV);
3554 if (I == InlineInfo.end()) {
3555 SmallVector<unsigned, 4> Labels;
3556 Labels.push_back(LabelID);
3557 InlineInfo[GV] = Labels;
3558 return;
3559 }
3560
3561 SmallVector<unsigned, 4> &Labels = I->second;
3562 Labels.push_back(LabelID);
3563 }
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003564
3565 /// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
3566 unsigned RecordInlinedFnEnd(DISubprogram &SP) {
3567 if (!TAI->doesDwarfUsesInlineInfoSection())
3568 return 0;
3569
3570 GlobalVariable *GV = SP.getGV();
3571 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3572 I = DbgInlinedScopeMap.find(GV);
3573 if (I == DbgInlinedScopeMap.end())
3574 return 0;
3575
3576 SmallVector<DbgScope *, 2> &Scopes = I->second;
Bill Wendling58ed5d22009-04-29 00:15:41 +00003577 assert(!Scopes.empty() && "We should have at least one debug scope!");
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003578 DbgScope *Scope = Scopes.back(); Scopes.pop_back();
3579 unsigned ID = MMI->NextLabelID();
3580 MMI->RecordUsedDbgLabel(ID);
3581 Scope->setEndLabelID(ID);
3582 return ID;
3583 }
3584
3585 /// RecordVariableScope - Record scope for the variable declared by
3586 /// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
3587 /// Record scopes for only inlined subroutine variables. Other
3588 /// variables' scopes are determined during RecordVariable().
3589 void RecordVariableScope(DIVariable &DV, const MachineInstr *DeclareMI) {
3590 DISubprogram SP(DV.getContext().getGV());
3591 if (SP.isNull())
3592 return;
3593 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3594 I = DbgInlinedScopeMap.find(SP.getGV());
3595 if (I == DbgInlinedScopeMap.end())
3596 return;
3597
3598 SmallVector<DbgScope *, 2> &Scopes = I->second;
3599 InlinedVariableScopes[DeclareMI] = Scopes.back();
3600 }
3601
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003602};
3603
3604//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00003605/// DwarfException - Emits Dwarf exception handling directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003606///
3607class DwarfException : public Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003608 struct FunctionEHFrameInfo {
3609 std::string FnName;
3610 unsigned Number;
3611 unsigned PersonalityIndex;
3612 bool hasCalls;
3613 bool hasLandingPads;
3614 std::vector<MachineMove> Moves;
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003615 const Function * function;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003616
3617 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3618 bool hC, bool hL,
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003619 const std::vector<MachineMove> &M,
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003620 const Function *f):
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003621 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003622 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003623 };
3624
3625 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen85535762008-04-02 00:25:04 +00003626
3627 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3628 /// be emitted.
3629 bool shouldEmitTable;
3630
3631 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3632 /// should be emitted.
3633 bool shouldEmitMoves;
3634
3635 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3636 /// should be emitted.
3637 bool shouldEmitTableModule;
3638
aslc200b112008-08-16 12:57:46 +00003639 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen85535762008-04-02 00:25:04 +00003640 /// should be emitted.
3641 bool shouldEmitMovesModule;
Duncan Sands96144f92008-05-07 19:11:09 +00003642
Bill Wendlingd9308a62009-03-10 21:23:25 +00003643 /// ExceptionTimer - Timer for the Dwarf exception writer.
3644 Timer *ExceptionTimer;
3645
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003646 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3647 ///
3648 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
3649 // Size and sign of stack growth.
3650 int stackGrowth =
3651 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3652 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00003653 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003654
3655 // Begin eh frame section.
3656 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling189bde72008-12-24 08:05:17 +00003657
3658 if (!TAI->doesRequireNonLocalEHFrameLabel())
3659 O << TAI->getEHGlobalPrefix();
3660 O << "EH_frame" << Index << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003661 EmitLabel("section_eh_frame", Index);
3662
3663 // Define base labels.
3664 EmitLabel("eh_frame_common", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003665
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003666 // Define the eh frame length.
3667 EmitDifference("eh_frame_common_end", Index,
3668 "eh_frame_common_begin", Index, true);
3669 Asm->EOL("Length of Common Information Entry");
3670
3671 // EH frame header.
3672 EmitLabel("eh_frame_common_begin", Index);
3673 Asm->EmitInt32((int)0);
3674 Asm->EOL("CIE Identifier Tag");
3675 Asm->EmitInt8(DW_CIE_VERSION);
3676 Asm->EOL("CIE Version");
Duncan Sands96144f92008-05-07 19:11:09 +00003677
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003678 // The personality presence indicates that language specific information
3679 // will show up in the eh frame.
3680 Asm->EmitString(Personality ? "zPLR" : "zR");
3681 Asm->EOL("CIE Augmentation");
Duncan Sands96144f92008-05-07 19:11:09 +00003682
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003683 // Round out reader.
3684 Asm->EmitULEB128Bytes(1);
3685 Asm->EOL("CIE Code Alignment Factor");
3686 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands96144f92008-05-07 19:11:09 +00003687 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00003688 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands96144f92008-05-07 19:11:09 +00003689 Asm->EOL("CIE Return Address Column");
3690
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003691 // If there is a personality, we need to indicate the functions location.
3692 if (Personality) {
3693 Asm->EmitULEB128Bytes(7);
3694 Asm->EOL("Augmentation Size");
Bill Wendling2d369922007-09-11 17:20:55 +00003695
Duncan Sands96144f92008-05-07 19:11:09 +00003696 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendling2d369922007-09-11 17:20:55 +00003697 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands96144f92008-05-07 19:11:09 +00003698 Asm->EOL("Personality (pcrel sdata4 indirect)");
3699 } else {
Bill Wendling2d369922007-09-11 17:20:55 +00003700 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands96144f92008-05-07 19:11:09 +00003701 Asm->EOL("Personality (pcrel sdata4)");
3702 }
Bill Wendling2d369922007-09-11 17:20:55 +00003703
Duncan Sands96144f92008-05-07 19:11:09 +00003704 PrintRelDirective(true);
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003705 O << TAI->getPersonalityPrefix();
3706 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3707 O << TAI->getPersonalitySuffix();
Duncan Sands4cc39532008-05-08 12:33:11 +00003708 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3709 O << "-" << TAI->getPCSymbol();
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003710 Asm->EOL("Personality");
Bill Wendling38cb7c92007-08-25 00:51:55 +00003711
Duncan Sands96144f92008-05-07 19:11:09 +00003712 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3713 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003714
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003715 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3716 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003717 } else {
3718 Asm->EmitULEB128Bytes(1);
3719 Asm->EOL("Augmentation Size");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003720
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003721 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3722 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003723 }
3724
3725 // Indicate locations of general callee saved registers in frame.
3726 std::vector<MachineMove> Moves;
3727 RI->getInitialFrameState(Moves);
Dale Johannesenf5a11532007-11-13 19:13:01 +00003728 EmitFrameMoves(NULL, 0, Moves, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003729
Dale Johannesen388f20f2008-04-30 00:43:29 +00003730 // On Darwin the linker honors the alignment of eh_frame, which means it
3731 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3732 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003733 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003734 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003735 EmitLabel("eh_frame_common_end", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003736
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003737 Asm->EOL();
3738 }
Duncan Sands96144f92008-05-07 19:11:09 +00003739
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003740 /// EmitEHFrame - Emit function exception frame information.
3741 ///
3742 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003743 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
Chris Lattner68433442009-04-13 05:44:34 +00003744
3745 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
3746 "Should not emit 'available externally' functions at all");
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003747
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003748 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3749
3750 // Externally visible entry into the functions eh frame info.
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003751 // If the corresponding function is static, this should not be
3752 // externally visible.
Rafael Espindolaa168fc92009-01-15 20:18:42 +00003753 if (linkage != Function::InternalLinkage &&
Devang Patel245446c2009-01-17 08:05:14 +00003754 linkage != Function::PrivateLinkage) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003755 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3756 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3757 }
3758
Dale Johannesenf09b5992008-01-10 02:03:30 +00003759 // If corresponding function is weak definition, this should be too.
Duncan Sands19d161f2009-03-07 15:45:40 +00003760 if ((linkage == Function::WeakAnyLinkage ||
3761 linkage == Function::WeakODRLinkage ||
3762 linkage == Function::LinkOnceAnyLinkage ||
3763 linkage == Function::LinkOnceODRLinkage) &&
Dale Johannesenf09b5992008-01-10 02:03:30 +00003764 TAI->getWeakDefDirective())
3765 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3766
3767 // If there are no calls then you can't unwind. This may mean we can
3768 // omit the EH Frame, but some environments do not handle weak absolute
aslc200b112008-08-16 12:57:46 +00003769 // symbols.
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003770 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesena9b3e482008-04-08 00:10:24 +00003771 // unwind info is to be available for non-EH uses.
Dale Johannesenf09b5992008-01-10 02:03:30 +00003772 if (!EHFrameInfo.hasCalls &&
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003773 !UnwindTablesMandatory &&
Duncan Sands19d161f2009-03-07 15:45:40 +00003774 ((linkage != Function::WeakAnyLinkage &&
3775 linkage != Function::WeakODRLinkage &&
3776 linkage != Function::LinkOnceAnyLinkage &&
3777 linkage != Function::LinkOnceODRLinkage) ||
Dale Johannesenf09b5992008-01-10 02:03:30 +00003778 !TAI->getWeakDefDirective() ||
3779 TAI->getSupportsWeakOmittedEHFrame()))
aslc200b112008-08-16 12:57:46 +00003780 {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003781 O << EHFrameInfo.FnName << " = 0\n";
aslc200b112008-08-16 12:57:46 +00003782 // This name has no connection to the function, so it might get
3783 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003784 // dead-stripping unconditionally.
3785 if (const char *UsedDirective = TAI->getUsedDirective())
3786 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003787 } else {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003788 O << EHFrameInfo.FnName << ":\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003789
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003790 // EH frame header.
3791 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3792 "eh_frame_begin", EHFrameInfo.Number, true);
3793 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00003794
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003795 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
3796
Bill Wendling189bde72008-12-24 08:05:17 +00003797 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3798 PrintRelDirective(true, true);
3799 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3800
3801 if (!TAI->isAbsoluteEHSectionOffsets())
3802 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3803 } else {
3804 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3805 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3806 true, true, false);
3807 }
3808
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003809 Asm->EOL("FDE CIE offset");
3810
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003811 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003812 Asm->EOL("FDE initial location");
3813 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003814 "eh_func_begin", EHFrameInfo.Number, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003815 Asm->EOL("FDE address range");
Duncan Sands96144f92008-05-07 19:11:09 +00003816
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003817 // If there is a personality and landing pads then point to the language
3818 // specific data area in the exception table.
3819 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands96144f92008-05-07 19:11:09 +00003820 Asm->EmitULEB128Bytes(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003821 Asm->EOL("Augmentation size");
Duncan Sands96144f92008-05-07 19:11:09 +00003822
3823 if (EHFrameInfo.hasLandingPads)
3824 EmitReference("exception", EHFrameInfo.Number, true, true);
3825 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003826 Asm->EmitInt32((int)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003827 Asm->EOL("Language Specific Data Area");
3828 } else {
3829 Asm->EmitULEB128Bytes(0);
3830 Asm->EOL("Augmentation size");
3831 }
Duncan Sands96144f92008-05-07 19:11:09 +00003832
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003833 // Indicate locations of function specific callee saved registers in
3834 // frame.
Devang Patelb28de842009-01-17 08:01:33 +00003835 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00003836 true);
aslc200b112008-08-16 12:57:46 +00003837
Dale Johannesen388f20f2008-04-30 00:43:29 +00003838 // On Darwin the linker honors the alignment of eh_frame, which means it
3839 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3840 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003841 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003842 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003843 EmitLabel("eh_frame_end", EHFrameInfo.Number);
aslc200b112008-08-16 12:57:46 +00003844
3845 // If the function is marked used, this table should be also. We cannot
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003846 // make the mark unconditional in this case, since retaining the table
aslc200b112008-08-16 12:57:46 +00003847 // also retains the function in this case, and there is code around
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003848 // that depends on unused functions (calling undefined externals) being
3849 // dead-stripped to link correctly. Yes, there really is.
3850 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3851 if (const char *UsedDirective = TAI->getUsedDirective())
3852 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3853 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003854 }
3855
Duncan Sands241a0c92007-09-05 11:27:52 +00003856 /// EmitExceptionTable - Emit landing pads and actions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003857 ///
3858 /// The general organization of the table is complex, but the basic concepts
3859 /// are easy. First there is a header which describes the location and
3860 /// organization of the three components that follow.
3861 /// 1. The landing pad site information describes the range of code covered
3862 /// by the try. In our case it's an accumulation of the ranges covered
3863 /// by the invokes in the try. There is also a reference to the landing
3864 /// pad that handles the exception once processed. Finally an index into
3865 /// the actions table.
3866 /// 2. The action table, in our case, is composed of pairs of type ids
3867 /// and next action offset. Starting with the action index from the
3868 /// landing pad site, each type Id is checked for a match to the current
3869 /// exception. If it matches then the exception and type id are passed
3870 /// on to the landing pad. Otherwise the next action is looked up. This
3871 /// chain is terminated with a next action of zero. If no type id is
3872 /// found the the frame is unwound and handling continues.
3873 /// 3. Type id table contains references to all the C++ typeinfo for all
3874 /// catches in the function. This tables is reversed indexed base 1.
3875
3876 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3877 static unsigned SharedTypeIds(const LandingPadInfo *L,
3878 const LandingPadInfo *R) {
3879 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3880 unsigned LSize = LIds.size(), RSize = RIds.size();
3881 unsigned MinSize = LSize < RSize ? LSize : RSize;
3882 unsigned Count = 0;
3883
3884 for (; Count != MinSize; ++Count)
3885 if (LIds[Count] != RIds[Count])
3886 return Count;
3887
3888 return Count;
3889 }
3890
3891 /// PadLT - Order landing pads lexicographically by type id.
3892 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
3893 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3894 unsigned LSize = LIds.size(), RSize = RIds.size();
3895 unsigned MinSize = LSize < RSize ? LSize : RSize;
3896
3897 for (unsigned i = 0; i != MinSize; ++i)
3898 if (LIds[i] != RIds[i])
3899 return LIds[i] < RIds[i];
3900
3901 return LSize < RSize;
3902 }
3903
3904 struct KeyInfo {
3905 static inline unsigned getEmptyKey() { return -1U; }
3906 static inline unsigned getTombstoneKey() { return -2U; }
3907 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner92eea072007-09-17 18:34:04 +00003908 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003909 static bool isPod() { return true; }
3910 };
3911
Duncan Sands241a0c92007-09-05 11:27:52 +00003912 /// ActionEntry - Structure describing an entry in the actions table.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003913 struct ActionEntry {
3914 int ValueForTypeID; // The value to write - may not be equal to the type id.
3915 int NextAction;
3916 struct ActionEntry *Previous;
3917 };
3918
Duncan Sands241a0c92007-09-05 11:27:52 +00003919 /// PadRange - Structure holding a try-range and the associated landing pad.
3920 struct PadRange {
3921 // The index of the landing pad.
3922 unsigned PadIndex;
3923 // The index of the begin and end labels in the landing pad's label lists.
3924 unsigned RangeIndex;
3925 };
3926
3927 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3928
3929 /// CallSiteEntry - Structure describing an entry in the call-site table.
3930 struct CallSiteEntry {
Duncan Sands4ff179f2007-12-19 07:36:31 +00003931 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003932 unsigned BeginLabel; // zero indicates the start of the function.
3933 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003934 // The landing pad starts at PadLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003935 unsigned PadLabel; // zero indicates that there is no landing pad.
3936 unsigned Action;
3937 };
3938
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003939 void EmitExceptionTable() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003940 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
3941 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
3942 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3943 if (PadInfos.empty()) return;
3944
3945 // Sort the landing pads in order of their type ids. This is used to fold
3946 // duplicate actions.
3947 SmallVector<const LandingPadInfo *, 64> LandingPads;
3948 LandingPads.reserve(PadInfos.size());
3949 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
3950 LandingPads.push_back(&PadInfos[i]);
3951 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3952
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003953 // Negative type ids index into FilterIds, positive type ids index into
3954 // TypeInfos. The value written for a positive type id is just the type
3955 // id itself. For a negative type id, however, the value written is the
3956 // (negative) byte offset of the corresponding FilterIds entry. The byte
3957 // offset is usually equal to the type id, because the FilterIds entries
3958 // are written using a variable width encoding which outputs one byte per
3959 // entry as long as the value written is not too large, but can differ.
3960 // This kind of complication does not occur for positive type ids because
3961 // type infos are output using a fixed width encoding.
3962 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3963 SmallVector<int, 16> FilterOffsets;
3964 FilterOffsets.reserve(FilterIds.size());
3965 int Offset = -1;
3966 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3967 E = FilterIds.end(); I != E; ++I) {
3968 FilterOffsets.push_back(Offset);
aslc200b112008-08-16 12:57:46 +00003969 Offset -= TargetAsmInfo::getULEB128Size(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003970 }
3971
Duncan Sands241a0c92007-09-05 11:27:52 +00003972 // Compute the actions table and gather the first action index for each
3973 // landing pad site.
3974 SmallVector<ActionEntry, 32> Actions;
3975 SmallVector<unsigned, 64> FirstActions;
3976 FirstActions.reserve(LandingPads.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003977
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003978 int FirstAction = 0;
Duncan Sands241a0c92007-09-05 11:27:52 +00003979 unsigned SizeActions = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003980 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3981 const LandingPadInfo *LP = LandingPads[i];
3982 const std::vector<int> &TypeIds = LP->TypeIds;
3983 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
3984 unsigned SizeSiteActions = 0;
3985
3986 if (NumShared < TypeIds.size()) {
3987 unsigned SizeAction = 0;
3988 ActionEntry *PrevAction = 0;
3989
3990 if (NumShared) {
3991 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3992 assert(Actions.size());
3993 PrevAction = &Actions.back();
aslc200b112008-08-16 12:57:46 +00003994 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3995 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003996 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
aslc200b112008-08-16 12:57:46 +00003997 SizeAction -=
3998 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003999 SizeAction += -PrevAction->NextAction;
4000 PrevAction = PrevAction->Previous;
4001 }
4002 }
4003
4004 // Compute the actions.
4005 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
4006 int TypeID = TypeIds[I];
4007 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
4008 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
aslc200b112008-08-16 12:57:46 +00004009 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004010
4011 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
aslc200b112008-08-16 12:57:46 +00004012 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004013 SizeSiteActions += SizeAction;
4014
4015 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
4016 Actions.push_back(Action);
4017
4018 PrevAction = &Actions.back();
4019 }
4020
4021 // Record the first action of the landing pad site.
4022 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
4023 } // else identical - re-use previous FirstAction
4024
4025 FirstActions.push_back(FirstAction);
4026
4027 // Compute this sites contribution to size.
4028 SizeActions += SizeSiteActions;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004029 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004030
Duncan Sands4ff179f2007-12-19 07:36:31 +00004031 // Compute the call-site table. The entry for an invoke has a try-range
4032 // containing the call, a non-zero landing pad and an appropriate action.
4033 // The entry for an ordinary call has a try-range containing the call and
4034 // zero for the landing pad and the action. Calls marked 'nounwind' have
4035 // no entry and must not be contained in the try-range of any entry - they
4036 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands241a0c92007-09-05 11:27:52 +00004037 SmallVector<CallSiteEntry, 64> CallSites;
4038
4039 RangeMapType PadMap;
Duncan Sands4ff179f2007-12-19 07:36:31 +00004040 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
4041 // by try-range labels when lowered). Ordinary calls do not, so appropriate
4042 // try-ranges for them need be deduced.
Duncan Sands241a0c92007-09-05 11:27:52 +00004043 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
4044 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands4ff179f2007-12-19 07:36:31 +00004045 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands241a0c92007-09-05 11:27:52 +00004046 unsigned BeginLabel = LandingPad->BeginLabels[j];
4047 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
4048 PadRange P = { i, j };
4049 PadMap[BeginLabel] = P;
4050 }
4051 }
4052
Duncan Sands4ff179f2007-12-19 07:36:31 +00004053 // The end label of the previous invoke or nounwind try-range.
Duncan Sands241a0c92007-09-05 11:27:52 +00004054 unsigned LastLabel = 0;
Duncan Sands4ff179f2007-12-19 07:36:31 +00004055
4056 // Whether there is a potentially throwing instruction (currently this means
4057 // an ordinary call) between the end of the previous try-range and now.
4058 bool SawPotentiallyThrowing = false;
4059
4060 // Whether the last callsite entry was for an invoke.
4061 bool PreviousIsInvoke = false;
4062
Duncan Sands4ff179f2007-12-19 07:36:31 +00004063 // Visit all instructions in order of address.
Duncan Sands241a0c92007-09-05 11:27:52 +00004064 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
4065 I != E; ++I) {
4066 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
4067 MI != E; ++MI) {
Dan Gohmanfa607c92008-07-01 00:05:16 +00004068 if (!MI->isLabel()) {
Chris Lattner5b930372008-01-07 07:27:27 +00004069 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands241a0c92007-09-05 11:27:52 +00004070 continue;
4071 }
4072
Chris Lattnerda4cff12007-12-30 20:50:28 +00004073 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands241a0c92007-09-05 11:27:52 +00004074 assert(BeginLabel && "Invalid label!");
Duncan Sands89372f62007-09-05 14:12:46 +00004075
Duncan Sands4ff179f2007-12-19 07:36:31 +00004076 // End of the previous try-range?
Duncan Sands89372f62007-09-05 14:12:46 +00004077 if (BeginLabel == LastLabel)
Duncan Sands4ff179f2007-12-19 07:36:31 +00004078 SawPotentiallyThrowing = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00004079
Duncan Sands4ff179f2007-12-19 07:36:31 +00004080 // Beginning of a new try-range?
Duncan Sands241a0c92007-09-05 11:27:52 +00004081 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands241a0c92007-09-05 11:27:52 +00004082 if (L == PadMap.end())
Duncan Sands4ff179f2007-12-19 07:36:31 +00004083 // Nope, it was just some random label.
Duncan Sands241a0c92007-09-05 11:27:52 +00004084 continue;
4085
4086 PadRange P = L->second;
4087 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
4088
4089 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
4090 "Inconsistent landing pad map!");
4091
4092 // If some instruction between the previous try-range and this one may
4093 // throw, create a call-site entry with no landing pad for the region
4094 // between the try-ranges.
Duncan Sands4ff179f2007-12-19 07:36:31 +00004095 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00004096 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
4097 CallSites.push_back(Site);
Duncan Sands4ff179f2007-12-19 07:36:31 +00004098 PreviousIsInvoke = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00004099 }
4100
4101 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands4ff179f2007-12-19 07:36:31 +00004102 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands241a0c92007-09-05 11:27:52 +00004103
Duncan Sands4ff179f2007-12-19 07:36:31 +00004104 if (LandingPad->LandingPadLabel) {
4105 // This try-range is for an invoke.
4106 CallSiteEntry Site = {BeginLabel, LastLabel,
4107 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands241a0c92007-09-05 11:27:52 +00004108
Duncan Sands4ff179f2007-12-19 07:36:31 +00004109 // Try to merge with the previous call-site.
4110 if (PreviousIsInvoke) {
Dan Gohman3d436002008-06-21 22:00:54 +00004111 CallSiteEntry &Prev = CallSites.back();
Duncan Sands4ff179f2007-12-19 07:36:31 +00004112 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
4113 // Extend the range of the previous entry.
4114 Prev.EndLabel = Site.EndLabel;
4115 continue;
4116 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004117 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004118
Duncan Sands4ff179f2007-12-19 07:36:31 +00004119 // Otherwise, create a new call-site.
4120 CallSites.push_back(Site);
4121 PreviousIsInvoke = true;
4122 } else {
4123 // Create a gap.
4124 PreviousIsInvoke = false;
4125 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004126 }
4127 }
4128 // If some instruction between the previous try-range and the end of the
4129 // function may throw, create a call-site entry with no landing pad for the
4130 // region following the try-range.
Duncan Sands4ff179f2007-12-19 07:36:31 +00004131 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00004132 CallSiteEntry Site = {LastLabel, 0, 0, 0};
4133 CallSites.push_back(Site);
4134 }
4135
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004136 // Final tallies.
Duncan Sands96144f92008-05-07 19:11:09 +00004137
4138 // Call sites.
4139 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
4140 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
4141 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
4142 unsigned SizeSites = CallSites.size() * (SiteStartSize +
4143 SiteLengthSize +
4144 LandingPadSize);
Duncan Sands241a0c92007-09-05 11:27:52 +00004145 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
aslc200b112008-08-16 12:57:46 +00004146 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands241a0c92007-09-05 11:27:52 +00004147
Duncan Sands96144f92008-05-07 19:11:09 +00004148 // Type infos.
4149 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
4150 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004151
4152 unsigned TypeOffset = sizeof(int8_t) + // Call site format
aslc200b112008-08-16 12:57:46 +00004153 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004154 SizeSites + SizeActions + SizeTypes;
4155
4156 unsigned TotalSize = sizeof(int8_t) + // LPStart format
4157 sizeof(int8_t) + // TType format
aslc200b112008-08-16 12:57:46 +00004158 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004159 TypeOffset;
4160
4161 unsigned SizeAlign = (4 - TotalSize) & 3;
4162
4163 // Begin the exception table.
4164 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng7e7d1942008-02-29 19:36:59 +00004165 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesen841e4982008-10-08 21:50:21 +00004166 O << "GCC_except_table" << SubprogramCount << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004167 for (unsigned i = 0; i != SizeAlign; ++i) {
4168 Asm->EmitInt8(0);
4169 Asm->EOL("Padding");
4170 }
4171 EmitLabel("exception", SubprogramCount);
4172
4173 // Emit the header.
4174 Asm->EmitInt8(DW_EH_PE_omit);
4175 Asm->EOL("LPStart format (DW_EH_PE_omit)");
4176 Asm->EmitInt8(DW_EH_PE_absptr);
4177 Asm->EOL("TType format (DW_EH_PE_absptr)");
4178 Asm->EmitULEB128Bytes(TypeOffset);
4179 Asm->EOL("TType base offset");
4180 Asm->EmitInt8(DW_EH_PE_udata4);
4181 Asm->EOL("Call site format (DW_EH_PE_udata4)");
4182 Asm->EmitULEB128Bytes(SizeSites);
4183 Asm->EOL("Call-site table length");
4184
Duncan Sands241a0c92007-09-05 11:27:52 +00004185 // Emit the landing pad site information.
4186 for (unsigned i = 0; i < CallSites.size(); ++i) {
4187 CallSiteEntry &S = CallSites[i];
4188 const char *BeginTag;
4189 unsigned BeginNumber;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004190
Duncan Sands241a0c92007-09-05 11:27:52 +00004191 if (!S.BeginLabel) {
4192 BeginTag = "eh_func_begin";
4193 BeginNumber = SubprogramCount;
4194 } else {
4195 BeginTag = "label";
4196 BeginNumber = S.BeginLabel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004197 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004198
Duncan Sands241a0c92007-09-05 11:27:52 +00004199 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00004200 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004201 Asm->EOL("Region start");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004202
Duncan Sands241a0c92007-09-05 11:27:52 +00004203 if (!S.EndLabel) {
Dale Johannesen4670be42008-01-15 23:24:56 +00004204 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands96144f92008-05-07 19:11:09 +00004205 true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004206 } else {
Duncan Sands96144f92008-05-07 19:11:09 +00004207 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004208 }
4209 Asm->EOL("Region length");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004210
Duncan Sands96144f92008-05-07 19:11:09 +00004211 if (!S.PadLabel)
4212 Asm->EmitInt32(0);
4213 else
Duncan Sands241a0c92007-09-05 11:27:52 +00004214 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00004215 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004216 Asm->EOL("Landing pad");
4217
4218 Asm->EmitULEB128Bytes(S.Action);
4219 Asm->EOL("Action");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004220 }
4221
4222 // Emit the actions.
4223 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4224 ActionEntry &Action = Actions[I];
4225
4226 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
4227 Asm->EOL("TypeInfo index");
4228 Asm->EmitSLEB128Bytes(Action.NextAction);
4229 Asm->EOL("Next action");
4230 }
4231
4232 // Emit the type ids.
4233 for (unsigned M = TypeInfos.size(); M; --M) {
4234 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00004235
4236 PrintRelDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004237
Bill Wendling26a8ab92009-04-10 00:12:49 +00004238 if (GV) {
4239 std::string GLN;
4240 O << Asm->getGlobalLinkName(GV, GLN);
4241 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004242 O << "0";
Bill Wendling26a8ab92009-04-10 00:12:49 +00004243 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004244
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004245 Asm->EOL("TypeInfo");
4246 }
4247
4248 // Emit the filter typeids.
4249 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4250 unsigned TypeID = FilterIds[j];
4251 Asm->EmitULEB128Bytes(TypeID);
4252 Asm->EOL("Filter TypeInfo index");
4253 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004254
Evan Cheng7e7d1942008-02-29 19:36:59 +00004255 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004256 }
4257
4258public:
4259 //===--------------------------------------------------------------------===//
4260 // Main entry points.
4261 //
Owen Anderson847b99b2008-08-21 00:14:44 +00004262 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendlingd9308a62009-03-10 21:23:25 +00004263 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
4264 shouldEmitTableModule(false), shouldEmitMovesModule(false),
4265 ExceptionTimer(0) {
4266 if (TimePassesIsEnabled)
4267 ExceptionTimer = new Timer("Dwarf Exception Writer",
Bill Wendling148ecc42009-03-10 22:58:53 +00004268 getDwarfTimerGroup());
Bill Wendlingd9308a62009-03-10 21:23:25 +00004269 }
aslc200b112008-08-16 12:57:46 +00004270
Bill Wendlingd9308a62009-03-10 21:23:25 +00004271 virtual ~DwarfException() {
4272 delete ExceptionTimer;
4273 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004274
4275 /// SetModuleInfo - Set machine module information when it's known that pass
4276 /// manager has created it. Set by the target AsmPrinter.
4277 void SetModuleInfo(MachineModuleInfo *mmi) {
4278 MMI = mmi;
4279 }
4280
4281 /// BeginModule - Emit all exception information that should come prior to the
4282 /// content.
4283 void BeginModule(Module *M) {
4284 this->M = M;
4285 }
4286
4287 /// EndModule - Emit all exception information that should come after the
4288 /// content.
4289 void EndModule() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004290 if (TimePassesIsEnabled)
4291 ExceptionTimer->startTimer();
4292
Dale Johannesen85535762008-04-02 00:25:04 +00004293 if (shouldEmitMovesModule || shouldEmitTableModule) {
4294 const std::vector<Function *> Personalities = MMI->getPersonalities();
Evan Cheng3e288912009-02-25 07:04:34 +00004295 for (unsigned i = 0; i < Personalities.size(); ++i)
Dale Johannesen85535762008-04-02 00:25:04 +00004296 EmitCommonEHFrame(Personalities[i], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004297
Dale Johannesen85535762008-04-02 00:25:04 +00004298 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4299 E = EHFrames.end(); I != E; ++I)
4300 EmitEHFrame(*I);
4301 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00004302
4303 if (TimePassesIsEnabled)
4304 ExceptionTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004305 }
4306
aslc200b112008-08-16 12:57:46 +00004307 /// BeginFunction - Gather pre-function exception information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004308 /// emitted immediately after the function entry point.
4309 void BeginFunction(MachineFunction *MF) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004310 if (TimePassesIsEnabled)
4311 ExceptionTimer->startTimer();
4312
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004313 this->MF = MF;
Dale Johannesen85535762008-04-02 00:25:04 +00004314 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen85535762008-04-02 00:25:04 +00004315
Bill Wendlingd9308a62009-03-10 21:23:25 +00004316 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen85535762008-04-02 00:25:04 +00004317 // Map all labels and get rid of any dead landing pads.
4318 MMI->TidyLandingPads();
Bill Wendlingd9308a62009-03-10 21:23:25 +00004319
Dale Johannesen85535762008-04-02 00:25:04 +00004320 // If any landing pads survive, we need an EH table.
4321 if (MMI->getLandingPads().size())
4322 shouldEmitTable = true;
4323
4324 // See if we need frame move info.
Duncan Sandscbc28b12008-07-04 09:55:48 +00004325 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen85535762008-04-02 00:25:04 +00004326 shouldEmitMoves = true;
4327
4328 if (shouldEmitMoves || shouldEmitTable)
4329 // Assumes in correct section after the entry point.
4330 EmitLabel("eh_func_begin", ++SubprogramCount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004331 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00004332
Dale Johannesen85535762008-04-02 00:25:04 +00004333 shouldEmitTableModule |= shouldEmitTable;
4334 shouldEmitMovesModule |= shouldEmitMoves;
Bill Wendlingd9308a62009-03-10 21:23:25 +00004335
4336 if (TimePassesIsEnabled)
4337 ExceptionTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004338 }
4339
4340 /// EndFunction - Gather and emit post-function exception information.
4341 ///
4342 void EndFunction() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004343 if (TimePassesIsEnabled)
4344 ExceptionTimer->startTimer();
4345
Dale Johannesen85535762008-04-02 00:25:04 +00004346 if (shouldEmitMoves || shouldEmitTable) {
4347 EmitLabel("eh_func_end", SubprogramCount);
4348 EmitExceptionTable();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004349
Dale Johannesen85535762008-04-02 00:25:04 +00004350 // Save EH frame information
Bill Wendling26a8ab92009-04-10 00:12:49 +00004351 std::string Name;
4352 EHFrames.push_back(
4353 FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF, Name),
4354 SubprogramCount,
4355 MMI->getPersonalityIndex(),
4356 MF->getFrameInfo()->hasCalls(),
4357 !MMI->getLandingPads().empty(),
4358 MMI->getFrameMoves(),
4359 MF->getFunction()));
Bill Wendlingd9308a62009-03-10 21:23:25 +00004360 }
4361
4362 if (TimePassesIsEnabled)
4363 ExceptionTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004364 }
4365};
4366
4367} // End of namespace llvm
4368
4369//===----------------------------------------------------------------------===//
4370
4371/// Emit - Print the abbreviation using the specified Dwarf writer.
4372///
4373void DIEAbbrev::Emit(const DwarfDebug &DD) const {
4374 // Emit its Dwarf tag type.
4375 DD.getAsm()->EmitULEB128Bytes(Tag);
4376 DD.getAsm()->EOL(TagString(Tag));
aslc200b112008-08-16 12:57:46 +00004377
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004378 // Emit whether it has children DIEs.
4379 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4380 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
aslc200b112008-08-16 12:57:46 +00004381
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004382 // For each attribute description.
4383 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4384 const DIEAbbrevData &AttrData = Data[i];
aslc200b112008-08-16 12:57:46 +00004385
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004386 // Emit attribute type.
4387 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4388 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
aslc200b112008-08-16 12:57:46 +00004389
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004390 // Emit form type.
4391 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4392 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
4393 }
4394
4395 // Mark end of abbreviation.
4396 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4397 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
4398}
4399
4400#ifndef NDEBUG
4401void DIEAbbrev::print(std::ostream &O) {
4402 O << "Abbreviation @"
4403 << std::hex << (intptr_t)this << std::dec
4404 << " "
4405 << TagString(Tag)
4406 << " "
4407 << ChildrenString(ChildrenFlag)
4408 << "\n";
aslc200b112008-08-16 12:57:46 +00004409
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004410 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4411 O << " "
4412 << AttributeString(Data[i].getAttribute())
4413 << " "
4414 << FormEncodingString(Data[i].getForm())
4415 << "\n";
4416 }
4417}
4418void DIEAbbrev::dump() { print(cerr); }
4419#endif
4420
4421//===----------------------------------------------------------------------===//
4422
4423#ifndef NDEBUG
4424void DIEValue::dump() {
4425 print(cerr);
4426}
4427#endif
4428
4429//===----------------------------------------------------------------------===//
4430
4431/// EmitValue - Emit integer of appropriate size.
4432///
4433void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
4434 switch (Form) {
4435 case DW_FORM_flag: // Fall thru
4436 case DW_FORM_ref1: // Fall thru
4437 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
4438 case DW_FORM_ref2: // Fall thru
4439 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
4440 case DW_FORM_ref4: // Fall thru
4441 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
4442 case DW_FORM_ref8: // Fall thru
4443 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4444 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4445 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
4446 default: assert(0 && "DIE Value form not supported yet"); break;
4447 }
4448}
4449
4450/// SizeOf - Determine size of integer value in bytes.
4451///
4452unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4453 switch (Form) {
4454 case DW_FORM_flag: // Fall thru
4455 case DW_FORM_ref1: // Fall thru
4456 case DW_FORM_data1: return sizeof(int8_t);
4457 case DW_FORM_ref2: // Fall thru
4458 case DW_FORM_data2: return sizeof(int16_t);
4459 case DW_FORM_ref4: // Fall thru
4460 case DW_FORM_data4: return sizeof(int32_t);
4461 case DW_FORM_ref8: // Fall thru
4462 case DW_FORM_data8: return sizeof(int64_t);
aslc200b112008-08-16 12:57:46 +00004463 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4464 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004465 default: assert(0 && "DIE Value form not supported yet"); break;
4466 }
4467 return 0;
4468}
4469
4470//===----------------------------------------------------------------------===//
4471
4472/// EmitValue - Emit string value.
4473///
4474void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Bill Wendling15afa002009-03-10 23:57:09 +00004475 DD.getAsm()->EmitString(Str);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004476}
4477
4478//===----------------------------------------------------------------------===//
4479
4480/// EmitValue - Emit label value.
4481///
4482void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004483 bool IsSmall = Form == DW_FORM_data4;
4484 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004485}
4486
4487/// SizeOf - Determine size of label value in bytes.
4488///
4489unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004490 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004491 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004492}
4493
4494//===----------------------------------------------------------------------===//
4495
4496/// EmitValue - Emit label value.
4497///
4498void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004499 bool IsSmall = Form == DW_FORM_data4;
4500 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004501}
4502
4503/// SizeOf - Determine size of label value in bytes.
4504///
4505unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004506 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004507 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004508}
aslc200b112008-08-16 12:57:46 +00004509
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004510//===----------------------------------------------------------------------===//
4511
4512/// EmitValue - Emit delta value.
4513///
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004514void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4515 bool IsSmall = Form == DW_FORM_data4;
4516 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4517 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4518}
4519
4520/// SizeOf - Determine size of delta value in bytes.
4521///
4522unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4523 if (Form == DW_FORM_data4) return 4;
4524 return DD.getTargetData()->getPointerSize();
4525}
aslc200b112008-08-16 12:57:46 +00004526
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004527//===----------------------------------------------------------------------===//
4528
4529/// EmitValue - Emit delta value.
4530///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004531void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
4532 bool IsSmall = Form == DW_FORM_data4;
4533 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
4534}
4535
4536/// SizeOf - Determine size of delta value in bytes.
4537///
4538unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4539 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004540 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004541}
4542
4543//===----------------------------------------------------------------------===//
4544
4545/// EmitValue - Emit debug information entry offset.
4546///
4547void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
4548 DD.getAsm()->EmitInt32(Entry->getOffset());
4549}
aslc200b112008-08-16 12:57:46 +00004550
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004551//===----------------------------------------------------------------------===//
4552
4553/// ComputeSize - calculate the size of the block.
4554///
4555unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
4556 if (!Size) {
Owen Anderson88dd6232008-06-24 21:44:59 +00004557 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004558
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004559 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4560 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
4561 }
4562 }
4563 return Size;
4564}
4565
4566/// EmitValue - Emit block data.
4567///
4568void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
4569 switch (Form) {
4570 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4571 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4572 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4573 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
4574 default: assert(0 && "Improper form for block"); break;
4575 }
aslc200b112008-08-16 12:57:46 +00004576
Owen Anderson88dd6232008-06-24 21:44:59 +00004577 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004578
4579 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4580 DD.getAsm()->EOL();
4581 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
4582 }
4583}
4584
4585/// SizeOf - Determine size of block data in bytes.
4586///
4587unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4588 switch (Form) {
4589 case DW_FORM_block1: return Size + sizeof(int8_t);
4590 case DW_FORM_block2: return Size + sizeof(int16_t);
4591 case DW_FORM_block4: return Size + sizeof(int32_t);
aslc200b112008-08-16 12:57:46 +00004592 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004593 default: assert(0 && "Improper form for block"); break;
4594 }
4595 return 0;
4596}
4597
4598//===----------------------------------------------------------------------===//
4599/// DIE Implementation
4600
4601DIE::~DIE() {
4602 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4603 delete Children[i];
4604}
aslc200b112008-08-16 12:57:46 +00004605
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004606/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4607///
4608void DIE::AddSiblingOffset() {
4609 DIEInteger *DI = new DIEInteger(0);
4610 Values.insert(Values.begin(), DI);
4611 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
4612}
4613
4614/// Profile - Used to gather unique data for the value folding set.
4615///
4616void DIE::Profile(FoldingSetNodeID &ID) {
4617 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00004618
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004619 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4620 ID.AddPointer(Children[i]);
4621
4622 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4623 ID.AddPointer(Values[j]);
4624}
4625
4626#ifndef NDEBUG
4627void DIE::print(std::ostream &O, unsigned IncIndent) {
4628 static unsigned IndentCount = 0;
4629 IndentCount += IncIndent;
4630 const std::string Indent(IndentCount, ' ');
4631 bool isBlock = Abbrev.getTag() == 0;
aslc200b112008-08-16 12:57:46 +00004632
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004633 if (!isBlock) {
4634 O << Indent
4635 << "Die: "
4636 << "0x" << std::hex << (intptr_t)this << std::dec
4637 << ", Offset: " << Offset
4638 << ", Size: " << Size
aslc200b112008-08-16 12:57:46 +00004639 << "\n";
4640
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004641 O << Indent
4642 << TagString(Abbrev.getTag())
4643 << " "
4644 << ChildrenString(Abbrev.getChildrenFlag());
4645 } else {
4646 O << "Size: " << Size;
4647 }
4648 O << "\n";
4649
Owen Anderson88dd6232008-06-24 21:44:59 +00004650 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004651
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004652 IndentCount += 2;
4653 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4654 O << Indent;
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004655
4656 if (!isBlock)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004657 O << AttributeString(Data[i].getAttribute());
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004658 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004659 O << "Blk[" << i << "]";
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004660
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004661 O << " "
4662 << FormEncodingString(Data[i].getForm())
4663 << " ";
4664 Values[i]->print(O);
4665 O << "\n";
4666 }
4667 IndentCount -= 2;
4668
4669 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4670 Children[j]->print(O, 4);
4671 }
aslc200b112008-08-16 12:57:46 +00004672
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004673 if (!isBlock) O << "\n";
4674 IndentCount -= IncIndent;
4675}
4676
4677void DIE::dump() {
4678 print(cerr);
4679}
4680#endif
4681
4682//===----------------------------------------------------------------------===//
4683/// DwarfWriter Implementation
4684///
4685
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004686DwarfWriter::DwarfWriter()
Bill Wendlingd9308a62009-03-10 21:23:25 +00004687 : ImmutablePass(&ID), DD(0), DE(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004688
4689DwarfWriter::~DwarfWriter() {
4690 delete DE;
4691 delete DD;
4692}
4693
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004694/// BeginModule - Emit all Dwarf sections that should come prior to the
4695/// content.
Devang Patelaa1e8432009-01-08 23:40:34 +00004696void DwarfWriter::BeginModule(Module *M,
4697 MachineModuleInfo *MMI,
4698 raw_ostream &OS, AsmPrinter *A,
4699 const TargetAsmInfo *T) {
4700 DE = new DwarfException(OS, A, T);
4701 DD = new DwarfDebug(OS, A, T);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004702 DE->BeginModule(M);
4703 DD->BeginModule(M);
Devang Patel6ccd57e2009-01-13 00:20:51 +00004704 DD->SetDebugInfo(MMI);
Devang Patelaa1e8432009-01-08 23:40:34 +00004705 DE->SetModuleInfo(MMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004706}
4707
4708/// EndModule - Emit all Dwarf sections that should come after the content.
4709///
4710void DwarfWriter::EndModule() {
4711 DE->EndModule();
4712 DD->EndModule();
4713}
4714
aslc200b112008-08-16 12:57:46 +00004715/// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004716/// emitted immediately after the function entry point.
4717void DwarfWriter::BeginFunction(MachineFunction *MF) {
4718 DE->BeginFunction(MF);
4719 DD->BeginFunction(MF);
4720}
4721
4722/// EndFunction - Gather and emit post-function debug information.
4723///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00004724void DwarfWriter::EndFunction(MachineFunction *MF) {
4725 DD->EndFunction(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004726 DE->EndFunction();
aslc200b112008-08-16 12:57:46 +00004727
Bill Wendling5b4796a2008-07-22 00:53:37 +00004728 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004729 // Clear function debug information.
4730 MMI->EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004731}
Devang Patelcb59fd42009-01-12 19:17:34 +00004732
Devang Patel2da0cc42009-01-15 23:41:32 +00004733/// ValidDebugInfo - Return true if V represents valid debug info value.
Bill Wendling58ed5d22009-04-29 00:15:41 +00004734bool DwarfWriter::ValidDebugInfo(Value *V, unsigned OptLevel) {
4735 return DD && DD->ValidDebugInfo(V, OptLevel);
Devang Patel2da0cc42009-01-15 23:41:32 +00004736}
4737
Devang Patelcb59fd42009-01-12 19:17:34 +00004738/// RecordSourceLine - Records location information and associates it with a
4739/// label. Returns a unique label ID used to generate a label and provide
4740/// correspondence to the source line list.
4741unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
4742 unsigned Src) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004743 return DD->RecordSourceLine(Line, Col, Src);
Devang Patelcb59fd42009-01-12 19:17:34 +00004744}
4745
Evan Cheng3e288912009-02-25 07:04:34 +00004746/// getOrCreateSourceID - Look up the source id with the given directory and
4747/// source file names. If none currently exists, create a new id and insert it
4748/// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
4749/// as well.
4750unsigned DwarfWriter::getOrCreateSourceID(const std::string &DirName,
4751 const std::string &FileName) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004752 return DD->getOrCreateSourceID(DirName, FileName);
Devang Patelcb59fd42009-01-12 19:17:34 +00004753}
4754
4755/// RecordRegionStart - Indicate the start of a region.
4756unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004757 return DD->RecordRegionStart(V);
Devang Patelcb59fd42009-01-12 19:17:34 +00004758}
4759
4760/// RecordRegionEnd - Indicate the end of a region.
4761unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004762 return DD->RecordRegionEnd(V);
Devang Patelcb59fd42009-01-12 19:17:34 +00004763}
4764
4765/// getRecordSourceLineCount - Count source lines.
4766unsigned DwarfWriter::getRecordSourceLineCount() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004767 return DD->getRecordSourceLineCount();
Devang Patelcb59fd42009-01-12 19:17:34 +00004768}
Devang Patel70190872009-01-13 21:25:00 +00004769
Devang Patelfe359e72009-01-13 21:44:10 +00004770/// RecordVariable - Indicate the declaration of a local variable.
4771///
Devang Patel8a9a7dc2009-04-15 00:10:26 +00004772void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
4773 const MachineInstr *MI) {
4774 DD->RecordVariable(GV, FrameIndex, MI);
Devang Patelfe359e72009-01-13 21:44:10 +00004775}
Devang Patel42f6bed2009-01-13 23:54:55 +00004776
Bill Wendling50db0792009-02-20 00:44:43 +00004777/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
4778/// be emitted.
4779bool DwarfWriter::ShouldEmitDwarfDebug() const {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004780 return DD->ShouldEmitDwarfDebug();
Bill Wendling50db0792009-02-20 00:44:43 +00004781}
Devang Patel88bf96e2009-04-13 17:02:03 +00004782
Devang Patel8a9a7dc2009-04-15 00:10:26 +00004783//// RecordInlinedFnStart - Global variable GV is inlined at the location marked
Devang Patel88bf96e2009-04-13 17:02:03 +00004784//// by LabelID label.
Devang Patel8a9a7dc2009-04-15 00:10:26 +00004785void DwarfWriter::RecordInlinedFnStart(Instruction *I, DISubprogram &SP,
4786 unsigned LabelID, unsigned Src,
4787 unsigned Line, unsigned Col) {
4788 DD->RecordInlinedFnStart(I, SP, LabelID, Src, Line, Col);
Devang Patel88bf96e2009-04-13 17:02:03 +00004789}
4790
Devang Patel8a9a7dc2009-04-15 00:10:26 +00004791/// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
4792unsigned DwarfWriter::RecordInlinedFnEnd(DISubprogram &SP) {
4793 return DD->RecordInlinedFnEnd(SP);
4794}
4795
4796/// RecordVariableScope - Record scope for the variable declared by
4797/// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
4798void DwarfWriter::RecordVariableScope(DIVariable &DV,
4799 const MachineInstr *DeclareMI) {
4800 DD->RecordVariableScope(DV, DeclareMI);
4801}