blob: 4120d9f917105721e3490c31557a796218456ff1 [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 ///
730 std::map<std::string, DIE *> Globals;
731
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; }
748 std::map<std::string, DIE *> &getGlobals() { return Globals; }
749
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 //
861 AsmPrinter *getAsm() const { return Asm; }
862 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 Cheng6181e062008-07-09 21:53:02 +00001075 if (VerboseAsm)
1076 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 Patela4162952009-01-12 18:48:36 +00001143 ~DbgScope() {
1144 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); }
1165};
1166
1167//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00001168/// DwarfDebug - Emits Dwarf debug directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001169///
1170class DwarfDebug : public Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001171 //===--------------------------------------------------------------------===//
1172 // Attributes used to construct specific Dwarf sections.
1173 //
aslc200b112008-08-16 12:57:46 +00001174
Evan Cheng3e288912009-02-25 07:04:34 +00001175 /// CompileUnitMap - A map of global variables representing compile units to
1176 /// compile units.
1177 DenseMap<Value *, CompileUnit *> CompileUnitMap;
1178
1179 /// CompileUnits - All the compile units in this module.
1180 ///
1181 SmallVector<CompileUnit *, 8> CompileUnits;
aslc200b112008-08-16 12:57:46 +00001182
Devang Patel2ae1db52009-01-30 18:20:31 +00001183 /// MainCU - Some platform prefers one compile unit per .o file. In such
1184 /// cases, all dies are inserted in MainCU.
1185 CompileUnit *MainCU;
Bill Wendlinge0f3a262009-02-20 20:40:28 +00001186
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001187 /// AbbreviationsSet - Used to uniquely define abbreviations.
1188 ///
1189 FoldingSet<DIEAbbrev> AbbreviationsSet;
1190
1191 /// Abbreviations - A list of all the unique abbreviations in use.
1192 ///
1193 std::vector<DIEAbbrev *> Abbreviations;
aslc200b112008-08-16 12:57:46 +00001194
Evan Cheng3e288912009-02-25 07:04:34 +00001195 /// DirectoryIdMap - Directory name to directory id map.
1196 ///
1197 StringMap<unsigned> DirectoryIdMap;
Devang Patel5f244e32009-01-05 22:35:52 +00001198
Evan Cheng3e288912009-02-25 07:04:34 +00001199 /// DirectoryNames - A list of directory names.
1200 SmallVector<std::string, 8> DirectoryNames;
1201
1202 /// SourceFileIdMap - Source file name to source file id map.
1203 ///
1204 StringMap<unsigned> SourceFileIdMap;
1205
1206 /// SourceFileNames - A list of source file names.
1207 SmallVector<std::string, 8> SourceFileNames;
1208
1209 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
1210 /// id mapped to a unique id.
1211 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
1212
1213 /// SourceIds - Reverse map from source id to directory id + file id pair.
1214 ///
1215 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
Devang Patel5f244e32009-01-05 22:35:52 +00001216
Devang Patel9b829452009-01-16 21:07:53 +00001217 /// Lines - List of of source line correspondence.
Devang Patel7dd15a92009-01-08 17:19:22 +00001218 std::vector<SrcLineInfo> Lines;
1219
Devang Patel9b829452009-01-16 21:07:53 +00001220 /// ValuesSet - Used to uniquely define values.
1221 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001222 FoldingSet<DIEValue> ValuesSet;
aslc200b112008-08-16 12:57:46 +00001223
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001224 /// Values - A list of all the unique values in use.
1225 ///
1226 std::vector<DIEValue *> Values;
aslc200b112008-08-16 12:57:46 +00001227
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001228 /// StringPool - A UniqueVector of strings used by indirect references.
1229 ///
1230 UniqueVector<std::string> StringPool;
1231
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001232 /// SectionMap - Provides a unique id per text section.
1233 ///
Anton Korobeynikov55b94962008-09-24 22:15:21 +00001234 UniqueVector<const Section*> SectionMap;
aslc200b112008-08-16 12:57:46 +00001235
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001236 /// SectionSourceLines - Tracks line numbers per text section.
1237 ///
Devang Patel35a078f2009-01-12 22:54:42 +00001238 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001239
1240 /// didInitial - Flag to indicate if initial emission has been done.
1241 ///
1242 bool didInitial;
aslc200b112008-08-16 12:57:46 +00001243
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001244 /// shouldEmit - Flag to indicate if debug information should be emitted.
1245 ///
1246 bool shouldEmit;
1247
Devang Patel2560d922009-01-15 18:25:17 +00001248 // RootDbgScope - Top level scope for the current function.
Devang Patel4d1709e2009-01-08 02:33:41 +00001249 //
1250 DbgScope *RootDbgScope;
1251
Bill Wendlingd9308a62009-03-10 21:23:25 +00001252 /// DbgScopeMap - Tracks the scopes in the current function.
Devang Patel4d1709e2009-01-08 02:33:41 +00001253 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
Bill Wendlingd9308a62009-03-10 21:23:25 +00001254
1255 /// DebugTimer - Timer for the Dwarf debug writer.
1256 Timer *DebugTimer;
Devang Patel4d1709e2009-01-08 02:33:41 +00001257
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001258 struct FunctionDebugFrameInfo {
1259 unsigned Number;
1260 std::vector<MachineMove> Moves;
1261
1262 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman9ba5d4d2007-08-27 14:50:10 +00001263 Number(Num), Moves(M) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001264 };
1265
1266 std::vector<FunctionDebugFrameInfo> DebugFrames;
aslc200b112008-08-16 12:57:46 +00001267
Bill Wendlingd9308a62009-03-10 21:23:25 +00001268private:
Bill Wendlingdf25fd62009-03-10 21:59:25 +00001269 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
Bill Wendling278a3922009-03-10 21:47:45 +00001270 /// maps to the source id. Source id starts at 1.
1271 std::pair<unsigned, unsigned>
Bill Wendlingdf25fd62009-03-10 21:59:25 +00001272 getSourceDirectoryAndFileIds(unsigned SId) const {
Bill Wendling278a3922009-03-10 21:47:45 +00001273 return SourceIds[SId-1];
1274 }
1275
1276 /// getNumSourceDirectories - Return the number of source directories in the
1277 /// debug info.
1278 unsigned getNumSourceDirectories() const {
1279 return DirectoryNames.size();
1280 }
1281
1282 /// getSourceDirectoryName - Return the name of the directory corresponding
1283 /// to the id.
1284 const std::string &getSourceDirectoryName(unsigned Id) const {
1285 return DirectoryNames[Id - 1];
1286 }
1287
1288 /// getSourceFileName - Return the name of the source file corresponding
1289 /// to the id.
1290 const std::string &getSourceFileName(unsigned Id) const {
1291 return SourceFileNames[Id - 1];
1292 }
1293
1294 /// getNumSourceIds - Return the number of unique source ids.
Bill Wendling278a3922009-03-10 21:47:45 +00001295 unsigned getNumSourceIds() const {
1296 return SourceIds.size();
1297 }
1298
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001299 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
aslc200b112008-08-16 12:57:46 +00001300 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001301 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1302 // Profile the node so that we can make it unique.
1303 FoldingSetNodeID ID;
1304 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00001305
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001306 // Check the set for priors.
1307 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
aslc200b112008-08-16 12:57:46 +00001308
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001309 // If it's newly added.
1310 if (InSet == &Abbrev) {
aslc200b112008-08-16 12:57:46 +00001311 // Add to abbreviation list.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001312 Abbreviations.push_back(&Abbrev);
1313 // Assign the vector position + 1 as its number.
1314 Abbrev.setNumber(Abbreviations.size());
1315 } else {
1316 // Assign existing abbreviation number.
1317 Abbrev.setNumber(InSet->getNumber());
1318 }
1319 }
1320
1321 /// NewString - Add a string to the constant pool and returns a label.
1322 ///
1323 DWLabel NewString(const std::string &String) {
1324 unsigned StringID = StringPool.insert(String);
1325 return DWLabel("string", StringID);
1326 }
aslc200b112008-08-16 12:57:46 +00001327
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001328 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1329 /// entry.
1330 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1331 DIEntry *Value;
aslc200b112008-08-16 12:57:46 +00001332
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001333 if (Entry) {
1334 FoldingSetNodeID ID;
1335 DIEntry::Profile(ID, Entry);
1336 void *Where;
1337 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
aslc200b112008-08-16 12:57:46 +00001338
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001339 if (Value) return Value;
aslc200b112008-08-16 12:57:46 +00001340
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001341 Value = new DIEntry(Entry);
1342 ValuesSet.InsertNode(Value, Where);
1343 } else {
1344 Value = new DIEntry(Entry);
1345 }
aslc200b112008-08-16 12:57:46 +00001346
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001347 Values.push_back(Value);
1348 return Value;
1349 }
aslc200b112008-08-16 12:57:46 +00001350
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001351 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1352 ///
1353 void SetDIEntry(DIEntry *Value, DIE *Entry) {
Bill Wendling15afa002009-03-10 23:57:09 +00001354 Value->setEntry(Entry);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001355 // Add to values set if not already there. If it is, we merely have a
1356 // duplicate in the values list (no harm.)
1357 ValuesSet.GetOrInsertNode(Value);
1358 }
1359
1360 /// AddUInt - Add an unsigned integer attribute data and value.
1361 ///
1362 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1363 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1364
1365 FoldingSetNodeID ID;
1366 DIEInteger::Profile(ID, Integer);
1367 void *Where;
1368 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1369 if (!Value) {
1370 Value = new DIEInteger(Integer);
1371 ValuesSet.InsertNode(Value, Where);
1372 Values.push_back(Value);
1373 }
aslc200b112008-08-16 12:57:46 +00001374
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001375 Die->AddValue(Attribute, Form, Value);
1376 }
aslc200b112008-08-16 12:57:46 +00001377
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001378 /// AddSInt - Add an signed integer attribute data and value.
1379 ///
1380 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1381 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1382
1383 FoldingSetNodeID ID;
1384 DIEInteger::Profile(ID, (uint64_t)Integer);
1385 void *Where;
1386 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1387 if (!Value) {
1388 Value = new DIEInteger(Integer);
1389 ValuesSet.InsertNode(Value, Where);
1390 Values.push_back(Value);
1391 }
aslc200b112008-08-16 12:57:46 +00001392
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001393 Die->AddValue(Attribute, Form, Value);
1394 }
aslc200b112008-08-16 12:57:46 +00001395
Evan Cheng3e288912009-02-25 07:04:34 +00001396 /// AddString - Add a string attribute data and value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001397 ///
1398 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1399 const std::string &String) {
1400 FoldingSetNodeID ID;
1401 DIEString::Profile(ID, String);
1402 void *Where;
1403 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1404 if (!Value) {
1405 Value = new DIEString(String);
1406 ValuesSet.InsertNode(Value, Where);
1407 Values.push_back(Value);
1408 }
aslc200b112008-08-16 12:57:46 +00001409
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001410 Die->AddValue(Attribute, Form, Value);
1411 }
aslc200b112008-08-16 12:57:46 +00001412
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001413 /// AddLabel - Add a Dwarf label attribute data and value.
1414 ///
1415 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1416 const DWLabel &Label) {
1417 FoldingSetNodeID ID;
1418 DIEDwarfLabel::Profile(ID, Label);
1419 void *Where;
1420 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1421 if (!Value) {
1422 Value = new DIEDwarfLabel(Label);
1423 ValuesSet.InsertNode(Value, Where);
1424 Values.push_back(Value);
1425 }
aslc200b112008-08-16 12:57:46 +00001426
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001427 Die->AddValue(Attribute, Form, Value);
1428 }
aslc200b112008-08-16 12:57:46 +00001429
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001430 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1431 ///
1432 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1433 const std::string &Label) {
1434 FoldingSetNodeID ID;
1435 DIEObjectLabel::Profile(ID, Label);
1436 void *Where;
1437 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1438 if (!Value) {
1439 Value = new DIEObjectLabel(Label);
1440 ValuesSet.InsertNode(Value, Where);
1441 Values.push_back(Value);
1442 }
aslc200b112008-08-16 12:57:46 +00001443
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001444 Die->AddValue(Attribute, Form, Value);
1445 }
aslc200b112008-08-16 12:57:46 +00001446
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001447 /// AddSectionOffset - Add a section offset label attribute data and value.
1448 ///
1449 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1450 const DWLabel &Label, const DWLabel &Section,
1451 bool isEH = false, bool useSet = true) {
1452 FoldingSetNodeID ID;
1453 DIESectionOffset::Profile(ID, Label, Section);
1454 void *Where;
1455 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1456 if (!Value) {
1457 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1458 ValuesSet.InsertNode(Value, Where);
1459 Values.push_back(Value);
1460 }
aslc200b112008-08-16 12:57:46 +00001461
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001462 Die->AddValue(Attribute, Form, Value);
1463 }
aslc200b112008-08-16 12:57:46 +00001464
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001465 /// AddDelta - Add a label delta attribute data and value.
1466 ///
1467 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1468 const DWLabel &Hi, const DWLabel &Lo) {
1469 FoldingSetNodeID ID;
1470 DIEDelta::Profile(ID, Hi, Lo);
1471 void *Where;
1472 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1473 if (!Value) {
1474 Value = new DIEDelta(Hi, Lo);
1475 ValuesSet.InsertNode(Value, Where);
1476 Values.push_back(Value);
1477 }
aslc200b112008-08-16 12:57:46 +00001478
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001479 Die->AddValue(Attribute, Form, Value);
1480 }
aslc200b112008-08-16 12:57:46 +00001481
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001482 /// AddDIEntry - Add a DIE attribute data and value.
1483 ///
1484 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1485 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1486 }
1487
1488 /// AddBlock - Add block data.
1489 ///
1490 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1491 Block->ComputeSize(*this);
1492 FoldingSetNodeID ID;
1493 Block->Profile(ID);
1494 void *Where;
1495 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1496 if (!Value) {
1497 Value = Block;
1498 ValuesSet.InsertNode(Value, Where);
1499 Values.push_back(Value);
1500 } else {
Chris Lattner3de66892007-09-21 18:25:53 +00001501 // Already exists, reuse the previous one.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001502 delete Block;
Chris Lattner3de66892007-09-21 18:25:53 +00001503 Block = cast<DIEBlock>(Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001504 }
aslc200b112008-08-16 12:57:46 +00001505
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001506 Die->AddValue(Attribute, Block->BestForm(), Value);
1507 }
1508
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001509 /// AddSourceLine - Add location information to specified debug information
1510 /// entry.
Devang Patel7c8a2772009-01-16 19:28:14 +00001511 void AddSourceLine(DIE *Die, const DIVariable *V) {
Devang Patel4d1709e2009-01-08 02:33:41 +00001512 unsigned FileID = 0;
1513 unsigned Line = V->getLineNumber();
Devang Patel2ae1db52009-01-30 18:20:31 +00001514 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1515 FileID = Unit->getID();
Devang Pateld94b6932009-02-10 06:04:08 +00001516 assert (FileID && "Invalid file id");
Devang Patel4d1709e2009-01-08 02:33:41 +00001517 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1518 AddUInt(Die, DW_AT_decl_line, 0, Line);
1519 }
1520
1521 /// AddSourceLine - Add location information to specified debug information
1522 /// entry.
Devang Patel7c8a2772009-01-16 19:28:14 +00001523 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Devang Patel5f244e32009-01-05 22:35:52 +00001524 unsigned FileID = 0;
1525 unsigned Line = G->getLineNumber();
Devang Patel2ae1db52009-01-30 18:20:31 +00001526 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1527 FileID = Unit->getID();
Devang Pateld94b6932009-02-10 06:04:08 +00001528 assert (FileID && "Invalid file id");
Devang Patel5f244e32009-01-05 22:35:52 +00001529 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1530 AddUInt(Die, DW_AT_decl_line, 0, Line);
1531 }
1532
Devang Patel7c8a2772009-01-16 19:28:14 +00001533 void AddSourceLine(DIE *Die, const DIType *Ty) {
Devang Patel5f244e32009-01-05 22:35:52 +00001534 unsigned FileID = 0;
Devang Patel7c8a2772009-01-16 19:28:14 +00001535 unsigned Line = Ty->getLineNumber();
Devang Patel2ae1db52009-01-30 18:20:31 +00001536 DICompileUnit CU = Ty->getCompileUnit();
1537 if (CU.isNull())
1538 return;
1539 CompileUnit *Unit = FindCompileUnit(CU);
1540 FileID = Unit->getID();
Devang Pateld94b6932009-02-10 06:04:08 +00001541 assert (FileID && "Invalid file id");
Devang Patel5f244e32009-01-05 22:35:52 +00001542 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1543 AddUInt(Die, DW_AT_decl_line, 0, Line);
1544 }
1545
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001546 /// AddAddress - Add an address attribute to a die based on the location
1547 /// provided.
1548 void AddAddress(DIE *Die, unsigned Attribute,
1549 const MachineLocation &Location) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001550 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001551 DIEBlock *Block = new DIEBlock();
aslc200b112008-08-16 12:57:46 +00001552
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001553 if (Location.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001554 if (Reg < 32) {
1555 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1556 } else {
1557 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1558 AddUInt(Block, 0, DW_FORM_udata, Reg);
1559 }
1560 } else {
1561 if (Reg < 32) {
1562 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1563 } else {
1564 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1565 AddUInt(Block, 0, DW_FORM_udata, Reg);
1566 }
1567 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1568 }
aslc200b112008-08-16 12:57:46 +00001569
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001570 AddBlock(Die, Attribute, 0, Block);
1571 }
aslc200b112008-08-16 12:57:46 +00001572
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001573 /// AddType - Add a new type attribute to the specified entity.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001574 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel165ed512009-01-23 19:13:31 +00001575 if (Ty.isNull())
Devang Patel4a4cbe72009-01-05 21:47:57 +00001576 return;
Devang Patel4a4cbe72009-01-05 21:47:57 +00001577
1578 // Check for pre-existence.
1579 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1580 // If it exists then use the existing value.
1581 if (Slot) {
1582 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1583 return;
1584 }
1585
1586 // Set up proxy.
1587 Slot = NewDIEntry();
1588
1589 // Construct type.
1590 DIE Buffer(DW_TAG_base_type);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001591 if (Ty.isBasicType(Ty.getTag()))
1592 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1593 else if (Ty.isDerivedType(Ty.getTag()))
1594 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1595 else {
Bill Wendling824a8bf2009-02-03 21:17:20 +00001596 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelef4bf3b2009-01-15 19:26:23 +00001597 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1598 }
1599
Devang Patelb0cb07c2009-01-27 23:22:55 +00001600 // Add debug information entry to entity and appropriate context.
1601 DIE *Die = NULL;
1602 DIDescriptor Context = Ty.getContext();
1603 if (!Context.isNull())
1604 Die = DW_Unit->getDieMapSlotFor(Context.getGV());
1605
1606 if (Die) {
1607 DIE *Child = new DIE(Buffer);
1608 Die->AddChild(Child);
1609 Buffer.Detach();
1610 SetDIEntry(Slot, Child);
Bill Wendling824a8bf2009-02-03 21:17:20 +00001611 } else {
Devang Patelb0cb07c2009-01-27 23:22:55 +00001612 Die = DW_Unit->AddDie(Buffer);
1613 SetDIEntry(Slot, Die);
1614 }
1615
Devang Patel4a4cbe72009-01-05 21:47:57 +00001616 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1617 }
1618
Devang Patel46d13752009-01-05 19:07:53 +00001619 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1620 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001621 DIBasicType BTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001622
1623 // Get core information.
Bill Wendling1c5842b2009-03-09 05:04:40 +00001624 std::string Name;
1625 BTy.getName(Name);
Devang Patelfc187162009-01-05 17:57:47 +00001626 Buffer.setTag(DW_TAG_base_type);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001627 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patelfc187162009-01-05 17:57:47 +00001628 // Add name if not anonymous or intermediate type.
1629 if (!Name.empty())
1630 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001631 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelfc187162009-01-05 17:57:47 +00001632 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1633 }
1634
Devang Patel46d13752009-01-05 19:07:53 +00001635 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1636 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001637 DIDerivedType DTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001638
1639 // Get core information.
Bill Wendling1c5842b2009-03-09 05:04:40 +00001640 std::string Name;
1641 DTy.getName(Name);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001642 uint64_t Size = DTy.getSizeInBits() >> 3;
1643 unsigned Tag = DTy.getTag();
Bill Wendling1c5842b2009-03-09 05:04:40 +00001644
Devang Patelfc187162009-01-05 17:57:47 +00001645 // FIXME - Workaround for templates.
1646 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1647
1648 Buffer.setTag(Tag);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001649
Devang Patelfc187162009-01-05 17:57:47 +00001650 // Map to main type, void will not have a type.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001651 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel4a4cbe72009-01-05 21:47:57 +00001652 AddType(DW_Unit, &Buffer, FromTy);
Devang Patelfc187162009-01-05 17:57:47 +00001653
1654 // Add name if not anonymous or intermediate type.
Evan Cheng3e288912009-02-25 07:04:34 +00001655 if (!Name.empty())
1656 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelfc187162009-01-05 17:57:47 +00001657
1658 // Add size if non-zero (derived types might be zero-sized.)
1659 if (Size)
1660 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1661
1662 // Add source line info if available and TyDesc is not a forward
1663 // declaration.
Devang Patele34e0882009-01-27 00:45:04 +00001664 if (!DTy.isForwardDecl())
1665 AddSourceLine(&Buffer, &DTy);
Devang Patelfc187162009-01-05 17:57:47 +00001666 }
1667
Devang Patel30c01372009-01-05 19:55:51 +00001668 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1669 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001670 DICompositeType CTy) {
Devang Patelb28de842009-01-17 08:01:33 +00001671 // Get core information.
Bill Wendling1c5842b2009-03-09 05:04:40 +00001672 std::string Name;
1673 CTy.getName(Name);
1674
Devang Patelef4bf3b2009-01-15 19:26:23 +00001675 uint64_t Size = CTy.getSizeInBits() >> 3;
1676 unsigned Tag = CTy.getTag();
Devang Patel8050bd72009-01-23 01:19:09 +00001677 Buffer.setTag(Tag);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001678
Devang Patel30c01372009-01-05 19:55:51 +00001679 switch (Tag) {
1680 case DW_TAG_vector_type:
1681 case DW_TAG_array_type:
Devang Patelef4bf3b2009-01-15 19:26:23 +00001682 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patel30c01372009-01-05 19:55:51 +00001683 break;
Devang Patel3798f492009-01-20 18:35:14 +00001684 case DW_TAG_enumeration_type:
1685 {
1686 DIArray Elements = CTy.getTypeArray();
1687 // Add enumerators to enumeration type.
1688 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1689 DIE *ElemDie = NULL;
1690 DIEnumerator Enum(Elements.getElement(i).getGV());
1691 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
1692 Buffer.AddChild(ElemDie);
1693 }
1694 }
1695 break;
Devang Patel30c01372009-01-05 19:55:51 +00001696 case DW_TAG_subroutine_type:
1697 {
1698 // Add prototype flag.
1699 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001700 DIArray Elements = CTy.getTypeArray();
Devang Patel30c01372009-01-05 19:55:51 +00001701 // Add return type.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001702 DIDescriptor RTy = Elements.getElement(0);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001703 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel4a4cbe72009-01-05 21:47:57 +00001704
Devang Patel30c01372009-01-05 19:55:51 +00001705 // Add arguments.
1706 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1707 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel4a4cbe72009-01-05 21:47:57 +00001708 DIDescriptor Ty = Elements.getElement(i);
Devang Pateld40a7e52009-01-17 06:57:25 +00001709 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patel30c01372009-01-05 19:55:51 +00001710 Buffer.AddChild(Arg);
1711 }
1712 }
1713 break;
1714 case DW_TAG_structure_type:
1715 case DW_TAG_union_type:
1716 {
1717 // Add elements to structure type.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001718 DIArray Elements = CTy.getTypeArray();
Devang Patelcf7acb12009-01-16 00:50:53 +00001719
1720 // A forward struct declared type may not have elements available.
1721 if (Elements.isNull())
1722 break;
1723
Devang Patel30c01372009-01-05 19:55:51 +00001724 // Add elements to structure type.
1725 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1726 DIDescriptor Element = Elements.getElement(i);
Devang Patelb28de842009-01-17 08:01:33 +00001727 DIE *ElemDie = NULL;
Devang Patelef4bf3b2009-01-15 19:26:23 +00001728 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel245446c2009-01-17 08:05:14 +00001729 ElemDie = CreateSubprogramDIE(DW_Unit,
1730 DISubprogram(Element.getGV()));
Devang Patelb28de842009-01-17 08:01:33 +00001731 else if (Element.getTag() == dwarf::DW_TAG_variable) // ???
1732 ElemDie = CreateGlobalVariableDIE(DW_Unit,
1733 DIGlobalVariable(Element.getGV()));
Devang Patel5c643892009-01-20 21:02:02 +00001734 else
1735 ElemDie = CreateMemberDIE(DW_Unit,
1736 DIDerivedType(Element.getGV()));
Devang Patel245446c2009-01-17 08:05:14 +00001737 Buffer.AddChild(ElemDie);
Devang Patel30c01372009-01-05 19:55:51 +00001738 }
Devang Patel74193d72009-02-17 22:43:44 +00001739 unsigned RLang = CTy.getRunTimeLang();
1740 if (RLang)
1741 AddUInt(&Buffer, DW_AT_APPLE_runtime_class, DW_FORM_data1, RLang);
Devang Patel30c01372009-01-05 19:55:51 +00001742 }
1743 break;
1744 default:
1745 break;
1746 }
1747
1748 // Add name if not anonymous or intermediate type.
Evan Cheng3e288912009-02-25 07:04:34 +00001749 if (!Name.empty())
1750 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel30c01372009-01-05 19:55:51 +00001751
Devang Patele34e0882009-01-27 00:45:04 +00001752 if (Tag == DW_TAG_enumeration_type || Tag == DW_TAG_structure_type
1753 || Tag == DW_TAG_union_type) {
1754 // Add size if non-zero (derived types might be zero-sized.)
1755 if (Size)
1756 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1757 else {
1758 // Add zero size if it is not a forward declaration.
1759 if (CTy.isForwardDecl())
1760 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1761 else
1762 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1763 }
1764
1765 // Add source line info if available.
1766 if (!CTy.isForwardDecl())
1767 AddSourceLine(&Buffer, &CTy);
Devang Patel30c01372009-01-05 19:55:51 +00001768 }
Devang Patel30c01372009-01-05 19:55:51 +00001769 }
1770
Bill Wendling824a8bf2009-02-03 21:17:20 +00001771 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1772 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelef4bf3b2009-01-15 19:26:23 +00001773 int64_t L = SR.getLo();
1774 int64_t H = SR.getHi();
Devang Patel6fb54132009-01-05 18:33:01 +00001775 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1776 if (L != H) {
1777 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1778 if (L)
Devang Patel245446c2009-01-17 08:05:14 +00001779 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1780 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel6fb54132009-01-05 18:33:01 +00001781 }
1782 Buffer.AddChild(DW_Subrange);
1783 }
1784
1785 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1786 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1787 DICompositeType *CTy) {
1788 Buffer.setTag(DW_TAG_array_type);
1789 if (CTy->getTag() == DW_TAG_vector_type)
1790 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1791
Devang Patel6ab30e52009-01-28 21:08:20 +00001792 // Emit derived type.
1793 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel6fb54132009-01-05 18:33:01 +00001794 DIArray Elements = CTy->getTypeArray();
Devang Patel6fb54132009-01-05 18:33:01 +00001795
1796 // Construct an anonymous type for index type.
1797 DIE IdxBuffer(DW_TAG_base_type);
1798 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1799 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1800 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1801
1802 // Add subranges to array type.
1803 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel30c01372009-01-05 19:55:51 +00001804 DIDescriptor Element = Elements.getElement(i);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001805 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1806 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel6fb54132009-01-05 18:33:01 +00001807 }
1808 }
1809
Bill Wendling824a8bf2009-02-03 21:17:20 +00001810 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3798f492009-01-20 18:35:14 +00001811 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patela566e812009-01-05 18:38:38 +00001812
1813 DIE *Enumerator = new DIE(DW_TAG_enumerator);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001814 std::string Name;
1815 ETy->getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001816 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
Devang Patela566e812009-01-05 18:38:38 +00001817 int64_t Value = ETy->getEnumValue();
1818 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Patel3798f492009-01-20 18:35:14 +00001819 return Enumerator;
Devang Patela566e812009-01-05 18:38:38 +00001820 }
Devang Patel6fb54132009-01-05 18:33:01 +00001821
Devang Patelb28de842009-01-17 08:01:33 +00001822 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendling824a8bf2009-02-03 21:17:20 +00001823 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patelb28de842009-01-17 08:01:33 +00001824 {
1825 DIE *GVDie = new DIE(DW_TAG_variable);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001826 std::string Name;
1827 GV.getDisplayName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001828 AddString(GVDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001829 std::string LinkageName;
1830 GV.getLinkageName(LinkageName);
Devang Patel526b01d2009-01-05 18:59:44 +00001831 if (!LinkageName.empty())
Devang Patelb28de842009-01-17 08:01:33 +00001832 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1833 AddType(DW_Unit, GVDie, GV.getType());
1834 if (!GV.isLocalToUnit())
1835 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
1836 AddSourceLine(GVDie, &GV);
1837 return GVDie;
Devang Patel526b01d2009-01-05 18:59:44 +00001838 }
1839
Devang Patel5c643892009-01-20 21:02:02 +00001840 /// CreateMemberDIE - Create new member DIE.
1841 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
1842 DIE *MemberDie = new DIE(DT.getTag());
Bill Wendling1c5842b2009-03-09 05:04:40 +00001843 std::string Name;
1844 DT.getName(Name);
Devang Patel5c643892009-01-20 21:02:02 +00001845 if (!Name.empty())
1846 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1847
1848 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1849
1850 AddSourceLine(MemberDie, &DT);
1851
Devang Patelf1f30d42009-02-17 21:23:59 +00001852 uint64_t Size = DT.getSizeInBits();
1853 uint64_t FieldSize = DT.getOriginalTypeSize();
1854
1855 if (Size != FieldSize) {
1856 // Handle bitfield.
1857 AddUInt(MemberDie, DW_AT_byte_size, 0, DT.getOriginalTypeSize() >> 3);
1858 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1859
1860 uint64_t Offset = DT.getOffsetInBits();
1861 uint64_t FieldOffset = Offset;
1862 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1863 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1864 FieldOffset = (HiMark - FieldSize);
1865 Offset -= FieldOffset;
1866 // Maybe we need to work from the other end.
1867 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1868 AddUInt(MemberDie, DW_AT_bit_offset, 0, Offset);
1869 }
Devang Patel5c643892009-01-20 21:02:02 +00001870 DIEBlock *Block = new DIEBlock();
1871 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1872 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1873 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1874
Devang Patel2e7ee192009-01-21 00:08:04 +00001875 if (DT.isProtected())
1876 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1877 else if (DT.isPrivate())
1878 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1879
Devang Patel5c643892009-01-20 21:02:02 +00001880 return MemberDie;
1881 }
1882
Devang Patelb28de842009-01-17 08:01:33 +00001883 /// CreateSubprogramDIE - Create new DIE using SP.
1884 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Devang Patel245446c2009-01-17 08:05:14 +00001885 const DISubprogram &SP,
1886 bool IsConstructor = false) {
Devang Patelb28de842009-01-17 08:01:33 +00001887 DIE *SPDie = new DIE(DW_TAG_subprogram);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001888 std::string Name;
1889 SP.getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001890 AddString(SPDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001891 std::string LinkageName;
1892 SP.getLinkageName(LinkageName);
Devang Patel526b01d2009-01-05 18:59:44 +00001893 if (!LinkageName.empty())
Devang Patelb28de842009-01-17 08:01:33 +00001894 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Devang Patel245446c2009-01-17 08:05:14 +00001895 LinkageName);
Devang Patelb28de842009-01-17 08:01:33 +00001896 AddSourceLine(SPDie, &SP);
Devang Patel526b01d2009-01-05 18:59:44 +00001897
Devang Patelb28de842009-01-17 08:01:33 +00001898 DICompositeType SPTy = SP.getType();
1899 DIArray Args = SPTy.getTypeArray();
1900
Devang Patel526b01d2009-01-05 18:59:44 +00001901 // Add Return Type.
Devang Patel688a19f2009-02-27 18:05:21 +00001902 if (!IsConstructor) {
1903 if (Args.isNull())
1904 AddType(DW_Unit, SPDie, SPTy);
1905 else
1906 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
1907 }
Devang Patel922d1592009-01-30 01:21:46 +00001908
Devang Patelace2cf62009-02-02 17:51:41 +00001909 if (!SP.isDefinition()) {
1910 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
1911 // Add arguments.
1912 // Do not add arguments for subprogram definition. They will be
1913 // handled through RecordVariable.
1914 if (!Args.isNull())
1915 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1916 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1917 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
1918 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
1919 SPDie->AddChild(Arg);
1920 }
1921 }
Devang Patel922d1592009-01-30 01:21:46 +00001922
Devang Patele075e072009-02-24 00:52:19 +00001923 unsigned Lang = SP.getCompileUnit().getLanguage();
1924 if (Lang == DW_LANG_C99 || Lang == DW_LANG_C89
1925 || Lang == DW_LANG_ObjC)
1926 AddUInt(SPDie, DW_AT_prototyped, DW_FORM_flag, 1);
1927
Devang Patelef4bf3b2009-01-15 19:26:23 +00001928 if (!SP.isLocalToUnit())
Devang Patel922d1592009-01-30 01:21:46 +00001929 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Devang Patelb28de842009-01-17 08:01:33 +00001930 return SPDie;
Devang Patel526b01d2009-01-05 18:59:44 +00001931 }
1932
Devang Patelb28de842009-01-17 08:01:33 +00001933 /// FindCompileUnit - Get the compile unit for the given descriptor.
1934 ///
Devang Patel5f244e32009-01-05 22:35:52 +00001935 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
Evan Cheng3e288912009-02-25 07:04:34 +00001936 CompileUnit *DW_Unit = CompileUnitMap[Unit.getGV()];
Devang Patel5f244e32009-01-05 22:35:52 +00001937 assert(DW_Unit && "Missing compile unit.");
1938 return DW_Unit;
1939 }
1940
Devang Patel42f6bed2009-01-13 23:54:55 +00001941 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel4d1709e2009-01-08 02:33:41 +00001942 ///
1943 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1944 // Get the descriptor.
Devang Patel7c8a2772009-01-16 19:28:14 +00001945 const DIVariable &VD = DV->getVariable();
Devang Patel4d1709e2009-01-08 02:33:41 +00001946
1947 // Translate tag to proper Dwarf tag. The result variable is dropped for
1948 // now.
1949 unsigned Tag;
Devang Patel7c8a2772009-01-16 19:28:14 +00001950 switch (VD.getTag()) {
Devang Patel4d1709e2009-01-08 02:33:41 +00001951 case DW_TAG_return_variable: return NULL;
1952 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1953 case DW_TAG_auto_variable: // fall thru
1954 default: Tag = DW_TAG_variable; break;
1955 }
1956
1957 // Define variable debug information entry.
1958 DIE *VariableDie = new DIE(Tag);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001959 std::string Name;
1960 VD.getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001961 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Devang Patel4d1709e2009-01-08 02:33:41 +00001962
1963 // Add source line info if available.
Devang Patel7c8a2772009-01-16 19:28:14 +00001964 AddSourceLine(VariableDie, &VD);
Devang Patel4d1709e2009-01-08 02:33:41 +00001965
1966 // Add variable type.
Devang Patel7c8a2772009-01-16 19:28:14 +00001967 AddType(Unit, VariableDie, VD.getType());
Devang Patel4d1709e2009-01-08 02:33:41 +00001968
1969 // Add variable address.
1970 MachineLocation Location;
1971 Location.set(RI->getFrameRegister(*MF),
1972 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
1973 AddAddress(VariableDie, DW_AT_location, Location);
1974
1975 return VariableDie;
1976 }
1977
Devang Patel4d1709e2009-01-08 02:33:41 +00001978 /// getOrCreateScope - Returns the scope associated with the given descriptor.
1979 ///
1980 DbgScope *getOrCreateScope(GlobalVariable *V) {
1981 DbgScope *&Slot = DbgScopeMap[V];
Bill Wendlinge0f3a262009-02-20 20:40:28 +00001982 if (Slot) return Slot;
1983
1984 // FIXME - breaks down when the context is an inlined function.
1985 DIDescriptor ParentDesc;
1986 DIDescriptor Desc(V);
1987
1988 if (Desc.getTag() == dwarf::DW_TAG_lexical_block) {
1989 DIBlock Block(V);
1990 ParentDesc = Block.getContext();
Devang Patel4d1709e2009-01-08 02:33:41 +00001991 }
Bill Wendlinge0f3a262009-02-20 20:40:28 +00001992
1993 DbgScope *Parent = ParentDesc.isNull() ?
1994 NULL : getOrCreateScope(ParentDesc.getGV());
1995 Slot = new DbgScope(Parent, Desc);
1996
1997 if (Parent) {
1998 Parent->AddScope(Slot);
1999 } else if (RootDbgScope) {
2000 // FIXME - Add inlined function scopes to the root so we can delete them
2001 // later. Long term, handle inlined functions properly.
2002 RootDbgScope->AddScope(Slot);
2003 } else {
2004 // First function is top level function.
2005 RootDbgScope = Slot;
2006 }
2007
Devang Patel4d1709e2009-01-08 02:33:41 +00002008 return Slot;
2009 }
2010
2011 /// ConstructDbgScope - Construct the components of a scope.
2012 ///
2013 void ConstructDbgScope(DbgScope *ParentScope,
2014 unsigned ParentStartID, unsigned ParentEndID,
2015 DIE *ParentDie, CompileUnit *Unit) {
2016 // Add variables to scope.
Devang Patel63c22f42009-01-10 02:42:49 +00002017 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
Devang Patel4d1709e2009-01-08 02:33:41 +00002018 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2019 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2020 if (VariableDie) ParentDie->AddChild(VariableDie);
2021 }
2022
2023 // Add nested scopes.
Devang Patel63c22f42009-01-10 02:42:49 +00002024 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel4d1709e2009-01-08 02:33:41 +00002025 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2026 // Define the Scope debug information entry.
2027 DbgScope *Scope = Scopes[j];
2028 // FIXME - Ignore inlined functions for the time being.
2029 if (!Scope->getParent()) continue;
2030
Devang Patelb9224922009-01-12 18:41:00 +00002031 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2032 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel4d1709e2009-01-08 02:33:41 +00002033
2034 // Ignore empty scopes.
2035 if (StartID == EndID && StartID != 0) continue;
2036 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
2037
2038 if (StartID == ParentStartID && EndID == ParentEndID) {
2039 // Just add stuff to the parent scope.
2040 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2041 } else {
2042 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2043
2044 // Add the scope bounds.
2045 if (StartID) {
2046 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2047 DWLabel("label", StartID));
2048 } else {
2049 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2050 DWLabel("func_begin", SubprogramCount));
2051 }
2052 if (EndID) {
2053 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2054 DWLabel("label", EndID));
2055 } else {
2056 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2057 DWLabel("func_end", SubprogramCount));
2058 }
2059
2060 // Add the scope contents.
2061 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2062 ParentDie->AddChild(ScopeDie);
2063 }
2064 }
2065 }
2066
2067 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2068 ///
2069 void ConstructRootDbgScope(DbgScope *RootScope) {
2070 // Exit if there is no root scope.
2071 if (!RootScope) return;
Devang Patel2560d922009-01-15 18:25:17 +00002072 DIDescriptor Desc = RootScope->getDesc();
2073 if (Desc.isNull())
2074 return;
Devang Patel4d1709e2009-01-08 02:33:41 +00002075
2076 // Get the subprogram debug information entry.
Devang Patel2560d922009-01-15 18:25:17 +00002077 DISubprogram SPD(Desc.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002078
2079 // Get the compile unit context.
Devang Patel2ae1db52009-01-30 18:20:31 +00002080 CompileUnit *Unit = MainCU;
2081 if (!Unit)
2082 Unit = FindCompileUnit(SPD.getCompileUnit());
Devang Patel4d1709e2009-01-08 02:33:41 +00002083
2084 // Get the subprogram die.
Devang Patel57ec9ac2009-01-12 22:58:14 +00002085 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002086 assert(SPDie && "Missing subprogram descriptor");
2087
2088 // Add the function bounds.
2089 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2090 DWLabel("func_begin", SubprogramCount));
2091 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2092 DWLabel("func_end", SubprogramCount));
2093 MachineLocation Location(RI->getFrameRegister(*MF));
2094 AddAddress(SPDie, DW_AT_frame_base, Location);
2095
2096 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2097 }
2098
2099 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2100 ///
2101 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Cheng3e288912009-02-25 07:04:34 +00002102 const char *FnName = MF->getFunction()->getNameStart();
2103 if (MainCU) {
2104 std::map<std::string, DIE*> &Globals = MainCU->getGlobals();
2105 std::map<std::string, DIE*>::iterator GI = Globals.find(FnName);
2106 if (GI != Globals.end()) {
2107 DIE *SPDie = GI->second;
Devang Patel4d1709e2009-01-08 02:33:41 +00002108
2109 // Add the function bounds.
2110 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2111 DWLabel("func_begin", SubprogramCount));
2112 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2113 DWLabel("func_end", SubprogramCount));
2114
2115 MachineLocation Location(RI->getFrameRegister(*MF));
2116 AddAddress(SPDie, DW_AT_frame_base, Location);
2117 return;
2118 }
Evan Cheng3e288912009-02-25 07:04:34 +00002119 } else {
2120 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2121 CompileUnit *Unit = CompileUnits[i];
2122 std::map<std::string, DIE*> &Globals = Unit->getGlobals();
2123 std::map<std::string, DIE*>::iterator GI = Globals.find(FnName);
2124 if (GI != Globals.end()) {
2125 DIE *SPDie = GI->second;
2126
2127 // Add the function bounds.
2128 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2129 DWLabel("func_begin", SubprogramCount));
2130 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2131 DWLabel("func_end", SubprogramCount));
2132
2133 MachineLocation Location(RI->getFrameRegister(*MF));
2134 AddAddress(SPDie, DW_AT_frame_base, Location);
2135 return;
2136 }
2137 }
Devang Patel4d1709e2009-01-08 02:33:41 +00002138 }
Evan Cheng3e288912009-02-25 07:04:34 +00002139
Devang Patel4d1709e2009-01-08 02:33:41 +00002140#if 0
2141 // FIXME: This is causing an abort because C++ mangled names are compared
2142 // with their unmangled counterparts. See PR2885. Don't do this assert.
2143 assert(0 && "Couldn't find DIE for machine function!");
2144#endif
Evan Cheng3e288912009-02-25 07:04:34 +00002145 return;
Devang Patel4d1709e2009-01-08 02:33:41 +00002146 }
2147
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002148 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2149 /// tools to recognize the object file contains Dwarf information.
2150 void EmitInitial() {
2151 // Check to see if we already emitted intial headers.
2152 if (didInitial) return;
2153 didInitial = true;
aslc200b112008-08-16 12:57:46 +00002154
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002155 // Dwarf sections base addresses.
2156 if (TAI->doesDwarfRequireFrameSection()) {
2157 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2158 EmitLabel("section_debug_frame", 0);
2159 }
2160 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2161 EmitLabel("section_info", 0);
2162 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2163 EmitLabel("section_abbrev", 0);
2164 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2165 EmitLabel("section_aranges", 0);
Scott Michel79f01f52009-01-26 22:32:51 +00002166 if (TAI->doesSupportMacInfoSection()) {
2167 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2168 EmitLabel("section_macinfo", 0);
2169 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002170 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2171 EmitLabel("section_line", 0);
2172 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2173 EmitLabel("section_loc", 0);
2174 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2175 EmitLabel("section_pubnames", 0);
2176 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2177 EmitLabel("section_str", 0);
2178 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2179 EmitLabel("section_ranges", 0);
2180
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002181 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002182 EmitLabel("text_begin", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00002183 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002184 EmitLabel("data_begin", 0);
2185 }
2186
2187 /// EmitDIE - Recusively Emits a debug information entry.
2188 ///
2189 void EmitDIE(DIE *Die) {
2190 // Get the abbreviation for this DIE.
2191 unsigned AbbrevNumber = Die->getAbbrevNumber();
2192 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
aslc200b112008-08-16 12:57:46 +00002193
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002194 Asm->EOL();
2195
2196 // Emit the code (index) for the abbreviation.
2197 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng0eeed442008-07-01 23:18:29 +00002198
2199 if (VerboseAsm)
2200 Asm->EOL(std::string("Abbrev [" +
2201 utostr(AbbrevNumber) +
2202 "] 0x" + utohexstr(Die->getOffset()) +
2203 ":0x" + utohexstr(Die->getSize()) + " " +
2204 TagString(Abbrev->getTag())));
2205 else
2206 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00002207
Owen Anderson88dd6232008-06-24 21:44:59 +00002208 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2209 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
aslc200b112008-08-16 12:57:46 +00002210
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002211 // Emit the DIE attribute values.
2212 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2213 unsigned Attr = AbbrevData[i].getAttribute();
2214 unsigned Form = AbbrevData[i].getForm();
2215 assert(Form && "Too many attributes for DIE (check abbreviation)");
aslc200b112008-08-16 12:57:46 +00002216
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002217 switch (Attr) {
2218 case DW_AT_sibling: {
2219 Asm->EmitInt32(Die->SiblingOffset());
2220 break;
2221 }
2222 default: {
2223 // Emit an attribute using the defined form.
2224 Values[i]->EmitValue(*this, Form);
2225 break;
2226 }
2227 }
aslc200b112008-08-16 12:57:46 +00002228
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002229 Asm->EOL(AttributeString(Attr));
2230 }
aslc200b112008-08-16 12:57:46 +00002231
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002232 // Emit the DIE children if any.
2233 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2234 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002235
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002236 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2237 EmitDIE(Children[j]);
2238 }
aslc200b112008-08-16 12:57:46 +00002239
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002240 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2241 }
2242 }
2243
2244 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2245 ///
2246 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2247 // Get the children.
2248 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002249
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002250 // If not last sibling and has children then add sibling offset attribute.
2251 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2252
2253 // Record the abbreviation.
2254 AssignAbbrevNumber(Die->getAbbrev());
aslc200b112008-08-16 12:57:46 +00002255
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002256 // Get the abbreviation for this DIE.
2257 unsigned AbbrevNumber = Die->getAbbrevNumber();
2258 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2259
2260 // Set DIE offset
2261 Die->setOffset(Offset);
aslc200b112008-08-16 12:57:46 +00002262
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002263 // Start the size with the size of abbreviation code.
aslc200b112008-08-16 12:57:46 +00002264 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2265
Owen Anderson88dd6232008-06-24 21:44:59 +00002266 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2267 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002268
2269 // Size the DIE attribute values.
2270 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2271 // Size attribute value.
2272 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2273 }
aslc200b112008-08-16 12:57:46 +00002274
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002275 // Size the DIE children if any.
2276 if (!Children.empty()) {
2277 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2278 "Children flag not set");
aslc200b112008-08-16 12:57:46 +00002279
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002280 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2281 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2282 }
aslc200b112008-08-16 12:57:46 +00002283
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002284 // End of children marker.
2285 Offset += sizeof(int8_t);
2286 }
2287
2288 Die->setSize(Offset - Die->getOffset());
2289 return Offset;
2290 }
2291
2292 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2293 ///
2294 void SizeAndOffsets() {
2295 // Process base compile unit.
Devang Patel2ae1db52009-01-30 18:20:31 +00002296 if (MainCU) {
2297 // Compute size of compile unit header
2298 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2299 sizeof(int16_t) + // DWARF version number
2300 sizeof(int32_t) + // Offset Into Abbrev. Section
2301 sizeof(int8_t); // Pointer Size (in bytes)
2302 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
2303 return;
2304 }
Evan Cheng3e288912009-02-25 07:04:34 +00002305 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2306 CompileUnit *Unit = CompileUnits[i];
Devang Patel6eae2832009-01-12 23:05:55 +00002307 // Compute size of compile unit header
2308 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2309 sizeof(int16_t) + // DWARF version number
2310 sizeof(int32_t) + // Offset Into Abbrev. Section
2311 sizeof(int8_t); // Pointer Size (in bytes)
2312 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2313 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002314 }
2315
Evan Cheng3e288912009-02-25 07:04:34 +00002316 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002317 ///
Evan Cheng3e288912009-02-25 07:04:34 +00002318 void EmitDebugInfoPerCU(CompileUnit *Unit) {
2319 DIE *Die = Unit->getDie();
2320 // Emit the compile units header.
2321 EmitLabel("info_begin", Unit->getID());
2322 // Emit size of content not including length itself
2323 unsigned ContentSize = Die->getSize() +
2324 sizeof(int16_t) + // DWARF version number
2325 sizeof(int32_t) + // Offset Into Abbrev. Section
2326 sizeof(int8_t) + // Pointer Size (in bytes)
2327 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2328
2329 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2330 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2331 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2332 Asm->EOL("Offset Into Abbrev. Section");
2333 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2334
2335 EmitDIE(Die);
2336 // FIXME - extra padding for gdb bug.
2337 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2338 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2339 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2340 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2341 EmitLabel("info_end", Unit->getID());
2342
2343 Asm->EOL();
2344 }
2345
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002346 void EmitDebugInfo() {
2347 // Start debug info section.
2348 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
aslc200b112008-08-16 12:57:46 +00002349
Evan Cheng3e288912009-02-25 07:04:34 +00002350 if (MainCU) {
2351 EmitDebugInfoPerCU(MainCU);
2352 return;
Devang Patel6eae2832009-01-12 23:05:55 +00002353 }
Evan Cheng3e288912009-02-25 07:04:34 +00002354
2355 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2356 EmitDebugInfoPerCU(CompileUnits[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002357 }
2358
2359 /// EmitAbbreviations - Emit the abbreviation section.
2360 ///
2361 void EmitAbbreviations() const {
2362 // Check to see if it is worth the effort.
2363 if (!Abbreviations.empty()) {
2364 // Start the debug abbrev section.
2365 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
aslc200b112008-08-16 12:57:46 +00002366
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002367 EmitLabel("abbrev_begin", 0);
aslc200b112008-08-16 12:57:46 +00002368
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002369 // For each abbrevation.
2370 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2371 // Get abbreviation data
2372 const DIEAbbrev *Abbrev = Abbreviations[i];
aslc200b112008-08-16 12:57:46 +00002373
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002374 // Emit the abbrevations code (base 1 index.)
2375 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2376 Asm->EOL("Abbreviation Code");
aslc200b112008-08-16 12:57:46 +00002377
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002378 // Emit the abbreviations data.
2379 Abbrev->Emit(*this);
aslc200b112008-08-16 12:57:46 +00002380
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002381 Asm->EOL();
2382 }
aslc200b112008-08-16 12:57:46 +00002383
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002384 // Mark end of abbreviations.
2385 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2386
2387 EmitLabel("abbrev_end", 0);
aslc200b112008-08-16 12:57:46 +00002388
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002389 Asm->EOL();
2390 }
2391 }
2392
Bill Wendling1983a2a2008-07-20 00:11:19 +00002393 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2394 /// the line matrix.
aslc200b112008-08-16 12:57:46 +00002395 ///
Bill Wendling1983a2a2008-07-20 00:11:19 +00002396 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2397 // Define last address of section.
2398 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2399 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2400 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2401 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2402
2403 // Mark end of matrix.
2404 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2405 Asm->EmitULEB128Bytes(1); Asm->EOL();
2406 Asm->EmitInt8(1); Asm->EOL();
2407 }
2408
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002409 /// EmitDebugLines - Emit source line information.
2410 ///
2411 void EmitDebugLines() {
Bill Wendling1983a2a2008-07-20 00:11:19 +00002412 // If the target is using .loc/.file, the assembler will be emitting the
2413 // .debug_line table automatically.
2414 if (TAI->hasDotLocAndDotFile())
Dan Gohmanc55b34a2007-09-24 21:43:52 +00002415 return;
2416
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002417 // Minimum line delta, thus ranging from -10..(255-10).
2418 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2419 // Maximum line delta, thus ranging from -10..(255-10).
2420 const int MaxLineDelta = 255 + MinLineDelta;
2421
2422 // Start the dwarf line section.
2423 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
aslc200b112008-08-16 12:57:46 +00002424
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002425 // Construct the section header.
aslc200b112008-08-16 12:57:46 +00002426
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002427 EmitDifference("line_end", 0, "line_begin", 0, true);
2428 Asm->EOL("Length of Source Line Info");
2429 EmitLabel("line_begin", 0);
aslc200b112008-08-16 12:57:46 +00002430
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002431 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
aslc200b112008-08-16 12:57:46 +00002432
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002433 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2434 Asm->EOL("Prolog Length");
2435 EmitLabel("line_prolog_begin", 0);
aslc200b112008-08-16 12:57:46 +00002436
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002437 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2438
2439 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2440
2441 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
aslc200b112008-08-16 12:57:46 +00002442
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002443 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2444
2445 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
aslc200b112008-08-16 12:57:46 +00002446
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002447 // Line number standard opcode encodings argument count
2448 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2449 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2450 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2451 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2452 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2453 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2454 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2455 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2456 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2457
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002458 // Emit directories.
Evan Cheng3e288912009-02-25 07:04:34 +00002459 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2460 Asm->EmitString(getSourceDirectoryName(DI));
2461 Asm->EOL("Directory");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002462 }
2463 Asm->EmitInt8(0); Asm->EOL("End of directories");
aslc200b112008-08-16 12:57:46 +00002464
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002465 // Emit files.
Evan Cheng3e288912009-02-25 07:04:34 +00002466 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2467 // Remember source id starts at 1.
Bill Wendlingdf25fd62009-03-10 21:59:25 +00002468 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Evan Cheng3e288912009-02-25 07:04:34 +00002469 Asm->EmitString(getSourceFileName(Id.second));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002470 Asm->EOL("Source");
Evan Cheng3e288912009-02-25 07:04:34 +00002471 Asm->EmitULEB128Bytes(Id.first);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002472 Asm->EOL("Directory #");
2473 Asm->EmitULEB128Bytes(0);
2474 Asm->EOL("Mod date");
2475 Asm->EmitULEB128Bytes(0);
2476 Asm->EOL("File size");
2477 }
2478 Asm->EmitInt8(0); Asm->EOL("End of files");
aslc200b112008-08-16 12:57:46 +00002479
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002480 EmitLabel("line_prolog_end", 0);
aslc200b112008-08-16 12:57:46 +00002481
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002482 // A sequence for each text section.
Bill Wendling1983a2a2008-07-20 00:11:19 +00002483 unsigned SecSrcLinesSize = SectionSourceLines.size();
2484
2485 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002486 // Isolate current sections line info.
Devang Patel35a078f2009-01-12 22:54:42 +00002487 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng0eeed442008-07-01 23:18:29 +00002488
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002489 if (VerboseAsm) {
2490 const Section* S = SectionMap[j + 1];
Evan Cheng3e288912009-02-25 07:04:34 +00002491 O << '\t' << TAI->getCommentString() << " Section"
2492 << S->getName() << '\n';
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002493 } else
Evan Cheng0eeed442008-07-01 23:18:29 +00002494 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002495
2496 // Dwarf assumes we start with first line of first source file.
2497 unsigned Source = 1;
2498 unsigned Line = 1;
aslc200b112008-08-16 12:57:46 +00002499
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002500 // Construct rows of the address, source, line, column matrix.
2501 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patel35a078f2009-01-12 22:54:42 +00002502 const SrcLineInfo &LineInfo = LineInfos[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002503 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2504 if (!LabelID) continue;
aslc200b112008-08-16 12:57:46 +00002505
Evan Cheng3e288912009-02-25 07:04:34 +00002506 if (!VerboseAsm)
Evan Cheng0eeed442008-07-01 23:18:29 +00002507 Asm->EOL();
Evan Cheng3e288912009-02-25 07:04:34 +00002508 else {
2509 std::pair<unsigned, unsigned> SourceID =
Bill Wendlingdf25fd62009-03-10 21:59:25 +00002510 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Evan Cheng3e288912009-02-25 07:04:34 +00002511 O << '\t' << TAI->getCommentString() << ' '
2512 << getSourceDirectoryName(SourceID.first) << ' '
2513 << getSourceFileName(SourceID.second)
2514 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2515 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002516
2517 // Define the line address.
2518 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohmancfb72b22007-09-27 23:12:31 +00002519 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002520 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2521 EmitReference("label", LabelID); Asm->EOL("Location label");
aslc200b112008-08-16 12:57:46 +00002522
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002523 // If change of source, then switch to the new source.
2524 if (Source != LineInfo.getSourceID()) {
2525 Source = LineInfo.getSourceID();
2526 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2527 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2528 }
aslc200b112008-08-16 12:57:46 +00002529
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002530 // If change of line.
2531 if (Line != LineInfo.getLine()) {
2532 // Determine offset.
2533 int Offset = LineInfo.getLine() - Line;
2534 int Delta = Offset - MinLineDelta;
aslc200b112008-08-16 12:57:46 +00002535
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002536 // Update line.
2537 Line = LineInfo.getLine();
aslc200b112008-08-16 12:57:46 +00002538
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002539 // If delta is small enough and in range...
2540 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2541 // ... then use fast opcode.
2542 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2543 } else {
2544 // ... otherwise use long hand.
2545 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2546 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2547 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2548 }
2549 } else {
2550 // Copy the previous row (different address or source)
2551 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2552 }
2553 }
2554
Bill Wendling1983a2a2008-07-20 00:11:19 +00002555 EmitEndOfLineMatrix(j + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002556 }
Bill Wendling1983a2a2008-07-20 00:11:19 +00002557
2558 if (SecSrcLinesSize == 0)
2559 // Because we're emitting a debug_line section, we still need a line
2560 // table. The linker and friends expect it to exist. If there's nothing to
2561 // put into it, emit an empty table.
2562 EmitEndOfLineMatrix(1);
aslc200b112008-08-16 12:57:46 +00002563
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002564 EmitLabel("line_end", 0);
aslc200b112008-08-16 12:57:46 +00002565
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002566 Asm->EOL();
2567 }
aslc200b112008-08-16 12:57:46 +00002568
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002569 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2570 ///
2571 void EmitCommonDebugFrame() {
2572 if (!TAI->doesDwarfRequireFrameSection())
2573 return;
2574
2575 int stackGrowth =
2576 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2577 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00002578 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002579
2580 // Start the dwarf frame section.
2581 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2582
2583 EmitLabel("debug_frame_common", 0);
2584 EmitDifference("debug_frame_common_end", 0,
2585 "debug_frame_common_begin", 0, true);
2586 Asm->EOL("Length of Common Information Entry");
2587
2588 EmitLabel("debug_frame_common_begin", 0);
2589 Asm->EmitInt32((int)DW_CIE_ID);
2590 Asm->EOL("CIE Identifier Tag");
2591 Asm->EmitInt8(DW_CIE_VERSION);
2592 Asm->EOL("CIE Version");
2593 Asm->EmitString("");
2594 Asm->EOL("CIE Augmentation");
2595 Asm->EmitULEB128Bytes(1);
2596 Asm->EOL("CIE Code Alignment Factor");
2597 Asm->EmitSLEB128Bytes(stackGrowth);
aslc200b112008-08-16 12:57:46 +00002598 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00002599 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002600 Asm->EOL("CIE RA Column");
aslc200b112008-08-16 12:57:46 +00002601
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002602 std::vector<MachineMove> Moves;
2603 RI->getInitialFrameState(Moves);
2604
Dale Johannesenf5a11532007-11-13 19:13:01 +00002605 EmitFrameMoves(NULL, 0, Moves, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002606
Evan Cheng7e7d1942008-02-29 19:36:59 +00002607 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002608 EmitLabel("debug_frame_common_end", 0);
aslc200b112008-08-16 12:57:46 +00002609
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002610 Asm->EOL();
2611 }
2612
2613 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2614 /// section.
2615 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
2616 if (!TAI->doesDwarfRequireFrameSection())
2617 return;
aslc200b112008-08-16 12:57:46 +00002618
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002619 // Start the dwarf frame section.
2620 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
aslc200b112008-08-16 12:57:46 +00002621
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002622 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2623 "debug_frame_begin", DebugFrameInfo.Number, true);
2624 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00002625
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002626 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2627
2628 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2629 0, 0, true, false);
2630 Asm->EOL("FDE CIE offset");
2631
2632 EmitReference("func_begin", DebugFrameInfo.Number);
2633 Asm->EOL("FDE initial location");
2634 EmitDifference("func_end", DebugFrameInfo.Number,
2635 "func_begin", DebugFrameInfo.Number);
2636 Asm->EOL("FDE address range");
aslc200b112008-08-16 12:57:46 +00002637
Devang Patelb28de842009-01-17 08:01:33 +00002638 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00002639 false);
aslc200b112008-08-16 12:57:46 +00002640
Evan Cheng7e7d1942008-02-29 19:36:59 +00002641 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002642 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2643
2644 Asm->EOL();
2645 }
2646
Evan Cheng3e288912009-02-25 07:04:34 +00002647 void EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2648 EmitDifference("pubnames_end", Unit->getID(),
2649 "pubnames_begin", Unit->getID(), true);
2650 Asm->EOL("Length of Public Names Info");
2651
2652 EmitLabel("pubnames_begin", Unit->getID());
2653
2654 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2655
2656 EmitSectionOffset("info_begin", "section_info",
2657 Unit->getID(), 0, true, false);
2658 Asm->EOL("Offset of Compilation Unit Info");
2659
2660 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2661 true);
2662 Asm->EOL("Compilation Unit Length");
2663
2664 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2665 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2666 GE = Globals.end(); GI != GE; ++GI) {
2667 const std::string &Name = GI->first;
2668 DIE * Entity = GI->second;
2669
2670 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2671 Asm->EmitString(Name); Asm->EOL("External Name");
2672 }
2673
2674 Asm->EmitInt32(0); Asm->EOL("End Mark");
2675 EmitLabel("pubnames_end", Unit->getID());
2676
2677 Asm->EOL();
2678 }
2679
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002680 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2681 ///
2682 void EmitDebugPubNames() {
2683 // Start the dwarf pubnames section.
2684 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
aslc200b112008-08-16 12:57:46 +00002685
Evan Cheng3e288912009-02-25 07:04:34 +00002686 if (MainCU) {
2687 EmitDebugPubNamesPerCU(MainCU);
2688 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002689 }
Evan Cheng3e288912009-02-25 07:04:34 +00002690
2691 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2692 EmitDebugPubNamesPerCU(CompileUnits[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002693 }
2694
2695 /// EmitDebugStr - Emit visible names into a debug str section.
2696 ///
2697 void EmitDebugStr() {
2698 // Check to see if it is worth the effort.
2699 if (!StringPool.empty()) {
2700 // Start the dwarf str section.
2701 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
aslc200b112008-08-16 12:57:46 +00002702
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002703 // For each of strings in the string pool.
2704 for (unsigned StringID = 1, N = StringPool.size();
2705 StringID <= N; ++StringID) {
2706 // Emit a label for reference from debug information entries.
2707 EmitLabel("string", StringID);
2708 // Emit the string itself.
2709 const std::string &String = StringPool[StringID];
2710 Asm->EmitString(String); Asm->EOL();
2711 }
aslc200b112008-08-16 12:57:46 +00002712
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002713 Asm->EOL();
2714 }
2715 }
2716
2717 /// EmitDebugLoc - Emit visible names into a debug loc section.
2718 ///
2719 void EmitDebugLoc() {
2720 // Start the dwarf loc section.
2721 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
aslc200b112008-08-16 12:57:46 +00002722
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002723 Asm->EOL();
2724 }
2725
2726 /// EmitDebugARanges - Emit visible names into a debug aranges section.
2727 ///
2728 void EmitDebugARanges() {
2729 // Start the dwarf aranges section.
2730 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
aslc200b112008-08-16 12:57:46 +00002731
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002732 // FIXME - Mock up
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002733#if 0
aslc200b112008-08-16 12:57:46 +00002734 CompileUnit *Unit = GetBaseCompileUnit();
2735
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002736 // Don't include size of length
2737 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
aslc200b112008-08-16 12:57:46 +00002738
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002739 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
aslc200b112008-08-16 12:57:46 +00002740
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002741 EmitReference("info_begin", Unit->getID());
2742 Asm->EOL("Offset of Compilation Unit Info");
2743
Dan Gohmancfb72b22007-09-27 23:12:31 +00002744 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002745
2746 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2747
2748 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2749 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2750
2751 // Range 1
2752 EmitReference("text_begin", 0); Asm->EOL("Address");
2753 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2754
2755 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2756 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002757#endif
aslc200b112008-08-16 12:57:46 +00002758
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002759 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002760 }
2761
2762 /// EmitDebugRanges - Emit visible names into a debug ranges section.
2763 ///
2764 void EmitDebugRanges() {
2765 // Start the dwarf ranges section.
2766 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
aslc200b112008-08-16 12:57:46 +00002767
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002768 Asm->EOL();
2769 }
2770
2771 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2772 ///
2773 void EmitDebugMacInfo() {
Scott Michel79f01f52009-01-26 22:32:51 +00002774 if (TAI->doesSupportMacInfoSection()) {
2775 // Start the dwarf macinfo section.
2776 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
aslc200b112008-08-16 12:57:46 +00002777
Scott Michel79f01f52009-01-26 22:32:51 +00002778 Asm->EOL();
2779 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002780 }
2781
Bill Wendling278a3922009-03-10 21:47:45 +00002782 /// GetOrCreateSourceID - Look up the source id with the given directory and
2783 /// source file names. If none currently exists, create a new id and insert it
2784 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
2785 /// as well.
2786 unsigned GetOrCreateSourceID(const std::string &DirName,
2787 const std::string &FileName) {
2788 unsigned DId;
2789 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
2790 if (DI != DirectoryIdMap.end()) {
2791 DId = DI->getValue();
2792 } else {
2793 DId = DirectoryNames.size() + 1;
2794 DirectoryIdMap[DirName] = DId;
2795 DirectoryNames.push_back(DirName);
2796 }
2797
2798 unsigned FId;
2799 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
2800 if (FI != SourceFileIdMap.end()) {
2801 FId = FI->getValue();
2802 } else {
2803 FId = SourceFileNames.size() + 1;
2804 SourceFileIdMap[FileName] = FId;
2805 SourceFileNames.push_back(FileName);
2806 }
2807
2808 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
2809 SourceIdMap.find(std::make_pair(DId, FId));
2810 if (SI != SourceIdMap.end())
2811 return SI->second;
2812
2813 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
2814 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
2815 SourceIds.push_back(std::make_pair(DId, FId));
2816
2817 return SrcId;
2818 }
2819
Evan Cheng3e288912009-02-25 07:04:34 +00002820 void ConstructCompileUnit(GlobalVariable *GV) {
2821 DICompileUnit DIUnit(GV);
Bill Wendling1c5842b2009-03-09 05:04:40 +00002822 std::string Dir, FN, Prod;
Bill Wendling278a3922009-03-10 21:47:45 +00002823 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
Bill Wendling1c5842b2009-03-09 05:04:40 +00002824 DIUnit.getFilename(FN));
Evan Cheng3e288912009-02-25 07:04:34 +00002825
2826 DIE *Die = new DIE(DW_TAG_compile_unit);
2827 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2828 DWLabel("section_line", 0), DWLabel("section_line", 0),
2829 false);
Bill Wendling1c5842b2009-03-09 05:04:40 +00002830 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer(Prod));
Evan Cheng3e288912009-02-25 07:04:34 +00002831 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendling1c5842b2009-03-09 05:04:40 +00002832 AddString(Die, DW_AT_name, DW_FORM_string, FN);
2833 if (!Dir.empty())
2834 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Cheng3e288912009-02-25 07:04:34 +00002835 if (DIUnit.isOptimized())
2836 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendling1c5842b2009-03-09 05:04:40 +00002837 std::string Flags;
2838 DIUnit.getFlags(Flags);
Evan Cheng3e288912009-02-25 07:04:34 +00002839 if (!Flags.empty())
2840 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
2841 unsigned RVer = DIUnit.getRunTimeVersion();
2842 if (RVer)
2843 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
2844
2845 CompileUnit *Unit = new CompileUnit(ID, Die);
2846 if (DIUnit.isMain()) {
2847 assert(!MainCU && "Multiple main compile units are found!");
2848 MainCU = Unit;
2849 }
2850 CompileUnitMap[DIUnit.getGV()] = Unit;
2851 CompileUnits.push_back(Unit);
2852 }
2853
Devang Patel289f2362009-01-05 23:11:11 +00002854 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Patelb3907da2009-01-05 23:03:32 +00002855 void ConstructCompileUnits() {
Evan Cheng3e288912009-02-25 07:04:34 +00002856 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.compile_units");
2857 if (!Root)
2858 return;
2859 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2860 "Malformed compile unit descriptor anchor type");
2861 Constant *RootC = cast<Constant>(*Root->use_begin());
2862 assert(RootC->hasNUsesOrMore(1) &&
2863 "Malformed compile unit descriptor anchor type");
2864 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2865 UI != UE; ++UI)
2866 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2867 UUI != UUE; ++UUI) {
2868 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2869 ConstructCompileUnit(GV);
Devang Patel2ae1db52009-01-30 18:20:31 +00002870 }
Evan Cheng3e288912009-02-25 07:04:34 +00002871 }
2872
2873 bool ConstructGlobalVariableDIE(GlobalVariable *GV) {
2874 DIGlobalVariable DI_GV(GV);
2875 CompileUnit *DW_Unit = MainCU;
2876 if (!DW_Unit)
2877 DW_Unit = FindCompileUnit(DI_GV.getCompileUnit());
2878
2879 // Check for pre-existence.
2880 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
2881 if (Slot)
2882 return false;
2883
2884 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
2885
2886 // Add address.
2887 DIEBlock *Block = new DIEBlock();
2888 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
2889 AddObjectLabel(Block, 0, DW_FORM_udata,
2890 Asm->getGlobalLinkName(DI_GV.getGlobal()));
2891 AddBlock(VariableDie, DW_AT_location, 0, Block);
2892
2893 // Add to map.
2894 Slot = VariableDie;
2895 // Add to context owner.
2896 DW_Unit->getDie()->AddChild(VariableDie);
2897 // Expose as global. FIXME - need to check external flag.
Bill Wendling1c5842b2009-03-09 05:04:40 +00002898 std::string Name;
2899 DW_Unit->AddGlobal(DI_GV.getName(Name), VariableDie);
Evan Cheng3e288912009-02-25 07:04:34 +00002900 return true;
Devang Patelb3907da2009-01-05 23:03:32 +00002901 }
2902
Devang Patel289f2362009-01-05 23:11:11 +00002903 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patela9169c32009-02-24 00:02:15 +00002904 /// visible global variables. Return true if at least one global DIE is
2905 /// created.
2906 bool ConstructGlobalVariableDIEs() {
Evan Cheng3e288912009-02-25 07:04:34 +00002907 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
2908 if (!Root)
2909 return false;
Devang Patel289f2362009-01-05 23:11:11 +00002910
Evan Cheng3e288912009-02-25 07:04:34 +00002911 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2912 "Malformed global variable descriptor anchor type");
2913 Constant *RootC = cast<Constant>(*Root->use_begin());
2914 assert(RootC->hasNUsesOrMore(1) &&
2915 "Malformed global variable descriptor anchor type");
Devang Patel289f2362009-01-05 23:11:11 +00002916
Evan Cheng3e288912009-02-25 07:04:34 +00002917 bool Result = false;
2918 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2919 UI != UE; ++UI)
2920 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2921 UUI != UUE; ++UUI) {
2922 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2923 Result |= ConstructGlobalVariableDIE(GV);
2924 }
2925 return Result;
2926 }
Devang Patel289f2362009-01-05 23:11:11 +00002927
Evan Cheng3e288912009-02-25 07:04:34 +00002928 bool ConstructSubprogram(GlobalVariable *GV) {
2929 DISubprogram SP(GV);
2930 CompileUnit *Unit = MainCU;
2931 if (!Unit)
2932 Unit = FindCompileUnit(SP.getCompileUnit());
Devang Patel289f2362009-01-05 23:11:11 +00002933
Evan Cheng3e288912009-02-25 07:04:34 +00002934 // Check for pre-existence.
2935 DIE *&Slot = Unit->getDieMapSlotFor(GV);
2936 if (Slot)
2937 return false;
2938
2939 if (!SP.isDefinition())
2940 // This is a method declaration which will be handled while
2941 // constructing class type.
2942 return false;
2943
2944 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
2945
2946 // Add to map.
2947 Slot = SubprogramDie;
2948 // Add to context owner.
2949 Unit->getDie()->AddChild(SubprogramDie);
2950 // Expose as global.
Bill Wendling1c5842b2009-03-09 05:04:40 +00002951 std::string Name;
2952 Unit->AddGlobal(SP.getName(Name), SubprogramDie);
Evan Cheng3e288912009-02-25 07:04:34 +00002953 return true;
Devang Patel289f2362009-01-05 23:11:11 +00002954 }
2955
Devang Patele6caf012009-01-05 23:21:35 +00002956 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patela9169c32009-02-24 00:02:15 +00002957 /// subprograms. Return true if at least one subprogram DIE is created.
2958 bool ConstructSubprograms() {
Evan Cheng3e288912009-02-25 07:04:34 +00002959 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
2960 if (!Root)
2961 return false;
Devang Patele6caf012009-01-05 23:21:35 +00002962
Evan Cheng3e288912009-02-25 07:04:34 +00002963 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2964 "Malformed subprogram descriptor anchor type");
2965 Constant *RootC = cast<Constant>(*Root->use_begin());
2966 assert(RootC->hasNUsesOrMore(1) &&
2967 "Malformed subprogram descriptor anchor type");
Devang Patele6caf012009-01-05 23:21:35 +00002968
Evan Cheng3e288912009-02-25 07:04:34 +00002969 bool Result = false;
2970 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2971 UI != UE; ++UI)
2972 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2973 UUI != UUE; ++UUI) {
2974 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2975 Result |= ConstructSubprogram(GV);
2976 }
2977 return Result;
Devang Patele6caf012009-01-05 23:21:35 +00002978 }
2979
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002980public:
2981 //===--------------------------------------------------------------------===//
2982 // Main entry points.
2983 //
Owen Anderson847b99b2008-08-21 00:14:44 +00002984 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendlingd9308a62009-03-10 21:23:25 +00002985 : Dwarf(OS, A, T, "dbg"), MainCU(0),
2986 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
2987 ValuesSet(InitValuesSetSize), Values(), StringPool(), SectionMap(),
2988 SectionSourceLines(), didInitial(false), shouldEmit(false),
2989 RootDbgScope(0), DebugTimer(0) {
2990 if (TimePassesIsEnabled)
2991 DebugTimer = new Timer("Dwarf Debug Writer",
Bill Wendling148ecc42009-03-10 22:58:53 +00002992 getDwarfTimerGroup());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002993 }
2994 virtual ~DwarfDebug() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002995 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2996 delete Values[j];
Bill Wendlingd9308a62009-03-10 21:23:25 +00002997
2998 delete DebugTimer;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002999 }
3000
Bill Wendling278a3922009-03-10 21:47:45 +00003001 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
3002 /// be emitted.
3003 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
3004
Devang Patel9304b382009-01-06 21:07:30 +00003005 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3006 /// This is inovked by the target AsmPrinter.
Devang Patel91d27b02009-01-12 23:09:42 +00003007 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003008 if (TimePassesIsEnabled)
3009 DebugTimer->startTimer();
3010
Bill Wendling6baa18d2009-02-03 21:38:21 +00003011 // Create all the compile unit DIEs.
3012 ConstructCompileUnits();
Devang Patel91d27b02009-01-12 23:09:42 +00003013
Bill Wendlingd9308a62009-03-10 21:23:25 +00003014 if (CompileUnits.empty()) {
3015 if (TimePassesIsEnabled)
Bill Wendling0be24752009-03-10 22:02:13 +00003016 DebugTimer->stopTimer();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003017
Bill Wendling6baa18d2009-02-03 21:38:21 +00003018 return;
Bill Wendlingd9308a62009-03-10 21:23:25 +00003019 }
Devang Patel91d27b02009-01-12 23:09:42 +00003020
Devang Patela9169c32009-02-24 00:02:15 +00003021 // Create DIEs for each of the externally visible global variables.
3022 bool globalDIEs = ConstructGlobalVariableDIEs();
3023
3024 // Create DIEs for each of the externally visible subprograms.
3025 bool subprogramDIEs = ConstructSubprograms();
3026
3027 // If there is not any debug info available for any global variables
3028 // and any subprograms then there is not any debug info to emit.
Bill Wendlingd9308a62009-03-10 21:23:25 +00003029 if (!globalDIEs && !subprogramDIEs) {
3030 if (TimePassesIsEnabled)
Bill Wendling0be24752009-03-10 22:02:13 +00003031 DebugTimer->stopTimer();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003032
Devang Patela9169c32009-02-24 00:02:15 +00003033 return;
Bill Wendlingd9308a62009-03-10 21:23:25 +00003034 }
Devang Patela9169c32009-02-24 00:02:15 +00003035
Bill Wendling6baa18d2009-02-03 21:38:21 +00003036 MMI = mmi;
3037 shouldEmit = true;
3038 MMI->setDebugInfoAvailability(true);
Devang Patel9304b382009-01-06 21:07:30 +00003039
Bill Wendling6baa18d2009-02-03 21:38:21 +00003040 // Prime section data.
3041 SectionMap.insert(TAI->getTextSection());
Devang Patel9304b382009-01-06 21:07:30 +00003042
Bill Wendling6baa18d2009-02-03 21:38:21 +00003043 // Print out .file directives to specify files for .loc directives. These
3044 // are printed out early so that they precede any .loc directives.
3045 if (TAI->hasDotLocAndDotFile()) {
Evan Cheng3e288912009-02-25 07:04:34 +00003046 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
3047 // Remember source id starts at 1.
Bill Wendlingdf25fd62009-03-10 21:59:25 +00003048 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Evan Cheng3e288912009-02-25 07:04:34 +00003049 sys::Path FullPath(getSourceDirectoryName(Id.first));
3050 bool AppendOk =
3051 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendling6baa18d2009-02-03 21:38:21 +00003052 assert(AppendOk && "Could not append filename to directory!");
3053 AppendOk = false;
3054 Asm->EmitFile(i, FullPath.toString());
3055 Asm->EOL();
Devang Patel9304b382009-01-06 21:07:30 +00003056 }
Bill Wendling6baa18d2009-02-03 21:38:21 +00003057 }
Devang Patel9304b382009-01-06 21:07:30 +00003058
Bill Wendling6baa18d2009-02-03 21:38:21 +00003059 // Emit initial sections
3060 EmitInitial();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003061
3062 if (TimePassesIsEnabled)
3063 DebugTimer->stopTimer();
Devang Patel9304b382009-01-06 21:07:30 +00003064 }
3065
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003066 /// BeginModule - Emit all Dwarf sections that should come prior to the
3067 /// content.
3068 void BeginModule(Module *M) {
3069 this->M = M;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003070 }
3071
3072 /// EndModule - Emit all Dwarf sections that should come after the content.
3073 ///
3074 void EndModule() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003075 if (!ShouldEmitDwarfDebug())
3076 return;
3077
3078 if (TimePassesIsEnabled)
3079 DebugTimer->startTimer();
aslc200b112008-08-16 12:57:46 +00003080
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003081 // Standard sections final addresses.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003082 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003083 EmitLabel("text_end", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00003084 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003085 EmitLabel("data_end", 0);
aslc200b112008-08-16 12:57:46 +00003086
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003087 // End text sections.
3088 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003089 Asm->SwitchToSection(SectionMap[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003090 EmitLabel("section_end", i);
3091 }
3092
3093 // Emit common frame information.
3094 EmitCommonDebugFrame();
3095
3096 // Emit function debug frame information
3097 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3098 E = DebugFrames.end(); I != E; ++I)
3099 EmitFunctionDebugFrame(*I);
3100
3101 // Compute DIE offsets and sizes.
3102 SizeAndOffsets();
aslc200b112008-08-16 12:57:46 +00003103
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003104 // Emit all the DIEs into a debug info section
3105 EmitDebugInfo();
aslc200b112008-08-16 12:57:46 +00003106
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003107 // Corresponding abbreviations into a abbrev section.
3108 EmitAbbreviations();
aslc200b112008-08-16 12:57:46 +00003109
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003110 // Emit source line correspondence into a debug line section.
3111 EmitDebugLines();
aslc200b112008-08-16 12:57:46 +00003112
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003113 // Emit info into a debug pubnames section.
3114 EmitDebugPubNames();
aslc200b112008-08-16 12:57:46 +00003115
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003116 // Emit info into a debug str section.
3117 EmitDebugStr();
aslc200b112008-08-16 12:57:46 +00003118
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003119 // Emit info into a debug loc section.
3120 EmitDebugLoc();
aslc200b112008-08-16 12:57:46 +00003121
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003122 // Emit info into a debug aranges section.
3123 EmitDebugARanges();
aslc200b112008-08-16 12:57:46 +00003124
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003125 // Emit info into a debug ranges section.
3126 EmitDebugRanges();
aslc200b112008-08-16 12:57:46 +00003127
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003128 // Emit info into a debug macinfo section.
3129 EmitDebugMacInfo();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003130
3131 if (TimePassesIsEnabled)
3132 DebugTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003133 }
3134
aslc200b112008-08-16 12:57:46 +00003135 /// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003136 /// emitted immediately after the function entry point.
3137 void BeginFunction(MachineFunction *MF) {
Bill Wendlingc1d211d2009-03-11 00:03:50 +00003138 this->MF = MF;
3139
Bill Wendling50db0792009-02-20 00:44:43 +00003140 if (!ShouldEmitDwarfDebug()) return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003141
Bill Wendlingd9308a62009-03-10 21:23:25 +00003142 if (TimePassesIsEnabled)
3143 DebugTimer->startTimer();
3144
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003145 // Begin accumulating function debug information.
3146 MMI->BeginFunction(MF);
aslc200b112008-08-16 12:57:46 +00003147
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003148 // Assumes in correct section after the entry point.
3149 EmitLabel("func_begin", ++SubprogramCount);
Evan Chenga53c40a2008-02-01 09:10:45 +00003150
3151 // Emit label for the implicitly defined dbg.stoppoint at the start of
3152 // the function.
Devang Patel35a078f2009-01-12 22:54:42 +00003153 if (!Lines.empty()) {
3154 const SrcLineInfo &LineInfo = Lines[0];
Andrew Lenharth42f91402008-04-03 17:37:43 +00003155 Asm->printLabel(LineInfo.getLabelID());
3156 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00003157
3158 if (TimePassesIsEnabled)
3159 DebugTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003160 }
aslc200b112008-08-16 12:57:46 +00003161
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003162 /// EndFunction - Gather and emit post-function debug information.
3163 ///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003164 void EndFunction(MachineFunction *MF) {
Bill Wendling50db0792009-02-20 00:44:43 +00003165 if (!ShouldEmitDwarfDebug()) return;
aslc200b112008-08-16 12:57:46 +00003166
Bill Wendlingd9308a62009-03-10 21:23:25 +00003167 if (TimePassesIsEnabled)
3168 DebugTimer->startTimer();
3169
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003170 // Define end label for subprogram.
3171 EmitLabel("func_end", SubprogramCount);
aslc200b112008-08-16 12:57:46 +00003172
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003173 // Get function line info.
Devang Patel35a078f2009-01-12 22:54:42 +00003174 if (!Lines.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003175 // Get section line info.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003176 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003177 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patel35a078f2009-01-12 22:54:42 +00003178 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003179 // Append the function info to section info.
3180 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patel35a078f2009-01-12 22:54:42 +00003181 Lines.begin(), Lines.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003182 }
aslc200b112008-08-16 12:57:46 +00003183
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003184 // Construct scopes for subprogram.
Devang Patel6ccd57e2009-01-13 00:20:51 +00003185 if (RootDbgScope)
3186 ConstructRootDbgScope(RootDbgScope);
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003187 else
3188 // FIXME: This is wrong. We are essentially getting past a problem with
3189 // debug information not being able to handle unreachable blocks that have
3190 // debug information in them. In particular, those unreachable blocks that
3191 // have "region end" info in them. That situation results in the "root
3192 // scope" not being created. If that's the case, then emit a "default"
3193 // scope, i.e., one that encompasses the whole function. This isn't
3194 // desirable. And a better way of handling this (and all of the debugging
3195 // information) needs to be explored.
Devang Patel6ccd57e2009-01-13 00:20:51 +00003196 ConstructDefaultDbgScope(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003197
3198 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3199 MMI->getFrameMoves()));
Devang Patela4162952009-01-12 18:48:36 +00003200
3201 // Clear debug info
3202 if (RootDbgScope) {
3203 delete RootDbgScope;
3204 DbgScopeMap.clear();
3205 RootDbgScope = NULL;
3206 }
Devang Patelcb59fd42009-01-12 19:17:34 +00003207
Bill Wendlingd9308a62009-03-10 21:23:25 +00003208 Lines.clear();
3209
3210 if (TimePassesIsEnabled)
3211 DebugTimer->stopTimer();
3212 }
Devang Patelcb59fd42009-01-12 19:17:34 +00003213
Devang Patel2da0cc42009-01-15 23:41:32 +00003214 /// ValidDebugInfo - Return true if V represents valid debug info value.
3215 bool ValidDebugInfo(Value *V) {
Devang Patel208098b2009-01-19 23:21:49 +00003216 if (!V)
3217 return false;
3218
Devang Patel4d92dded2009-01-16 01:49:46 +00003219 if (!shouldEmit)
3220 return false;
3221
Devang Patel2da0cc42009-01-15 23:41:32 +00003222 GlobalVariable *GV = getGlobalVariable(V);
3223 if (!GV)
3224 return false;
Duncan Sands19d161f2009-03-07 15:45:40 +00003225
3226 if (!GV->hasInternalLinkage () && !GV->hasLinkOnceLinkage())
Devang Patel2da0cc42009-01-15 23:41:32 +00003227 return false;
3228
Bill Wendlingd9308a62009-03-10 21:23:25 +00003229 if (TimePassesIsEnabled)
3230 DebugTimer->startTimer();
3231
Devang Patel2da0cc42009-01-15 23:41:32 +00003232 DIDescriptor DI(GV);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003233
Devang Patel2da0cc42009-01-15 23:41:32 +00003234 // Check current version. Allow Version6 for now.
3235 unsigned Version = DI.getVersion();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003236 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6) {
3237 if (TimePassesIsEnabled)
3238 DebugTimer->stopTimer();
3239
Devang Patel2da0cc42009-01-15 23:41:32 +00003240 return false;
Bill Wendlingd9308a62009-03-10 21:23:25 +00003241 }
Devang Patel2da0cc42009-01-15 23:41:32 +00003242
Devang Patel208098b2009-01-19 23:21:49 +00003243 unsigned Tag = DI.getTag();
3244 switch (Tag) {
3245 case DW_TAG_variable:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003246 assert(DIVariable(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003247 break;
3248 case DW_TAG_compile_unit:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003249 assert(DICompileUnit(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003250 break;
3251 case DW_TAG_subprogram:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003252 assert(DISubprogram(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003253 break;
3254 default:
3255 break;
3256 }
3257
Bill Wendlingd9308a62009-03-10 21:23:25 +00003258 if (TimePassesIsEnabled)
3259 DebugTimer->stopTimer();
3260
Devang Patel2da0cc42009-01-15 23:41:32 +00003261 return true;
3262 }
3263
Devang Patelcb59fd42009-01-12 19:17:34 +00003264 /// RecordSourceLine - Records location information and associates it with a
3265 /// label. Returns a unique label ID used to generate a label and provide
3266 /// correspondence to the source line list.
3267 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003268 if (TimePassesIsEnabled)
3269 DebugTimer->startTimer();
3270
Evan Cheng3e288912009-02-25 07:04:34 +00003271 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendling6baa18d2009-02-03 21:38:21 +00003272 assert(Unit && "Unable to find CompileUnit");
Devang Patelcb59fd42009-01-12 19:17:34 +00003273 unsigned ID = MMI->NextLabelID();
3274 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
Bill Wendlingd9308a62009-03-10 21:23:25 +00003275
3276 if (TimePassesIsEnabled)
3277 DebugTimer->stopTimer();
3278
Devang Patelcb59fd42009-01-12 19:17:34 +00003279 return ID;
3280 }
3281
3282 /// RecordSourceLine - Records location information and associates it with a
3283 /// label. Returns a unique label ID used to generate a label and provide
3284 /// correspondence to the source line list.
3285 unsigned RecordSourceLine(unsigned Line, unsigned Col, unsigned Src) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003286 if (TimePassesIsEnabled)
3287 DebugTimer->startTimer();
3288
Devang Patelcb59fd42009-01-12 19:17:34 +00003289 unsigned ID = MMI->NextLabelID();
3290 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
Bill Wendlingd9308a62009-03-10 21:23:25 +00003291
3292 if (TimePassesIsEnabled)
3293 DebugTimer->stopTimer();
3294
Devang Patelcb59fd42009-01-12 19:17:34 +00003295 return ID;
3296 }
3297
Bill Wendling278a3922009-03-10 21:47:45 +00003298 /// getRecordSourceLineCount - Return the number of source lines in the debug
3299 /// info.
3300 unsigned getRecordSourceLineCount() const {
Devang Patelcb59fd42009-01-12 19:17:34 +00003301 return Lines.size();
3302 }
3303
Bill Wendling278a3922009-03-10 21:47:45 +00003304 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
3305 /// timed. Look up the source id with the given directory and source file
3306 /// names. If none currently exists, create a new id and insert it in the
3307 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
3308 /// well.
Evan Cheng3e288912009-02-25 07:04:34 +00003309 unsigned getOrCreateSourceID(const std::string &DirName,
3310 const std::string &FileName) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003311 if (TimePassesIsEnabled)
3312 DebugTimer->startTimer();
3313
Bill Wendling278a3922009-03-10 21:47:45 +00003314 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003315
3316 if (TimePassesIsEnabled)
3317 DebugTimer->stopTimer();
3318
Evan Cheng3e288912009-02-25 07:04:34 +00003319 return SrcId;
Devang Patelcb59fd42009-01-12 19:17:34 +00003320 }
3321
3322 /// RecordRegionStart - Indicate the start of a region.
Devang Patelcb59fd42009-01-12 19:17:34 +00003323 unsigned RecordRegionStart(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003324 if (TimePassesIsEnabled)
3325 DebugTimer->startTimer();
3326
Devang Patelcb59fd42009-01-12 19:17:34 +00003327 DbgScope *Scope = getOrCreateScope(V);
3328 unsigned ID = MMI->NextLabelID();
3329 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003330
3331 if (TimePassesIsEnabled)
3332 DebugTimer->stopTimer();
3333
Devang Patelcb59fd42009-01-12 19:17:34 +00003334 return ID;
3335 }
3336
3337 /// RecordRegionEnd - Indicate the end of a region.
Devang Patelcb59fd42009-01-12 19:17:34 +00003338 unsigned RecordRegionEnd(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003339 if (TimePassesIsEnabled)
3340 DebugTimer->startTimer();
3341
Devang Patelcb59fd42009-01-12 19:17:34 +00003342 DbgScope *Scope = getOrCreateScope(V);
3343 unsigned ID = MMI->NextLabelID();
3344 Scope->setEndLabelID(ID);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003345
3346 if (TimePassesIsEnabled)
3347 DebugTimer->stopTimer();
3348
Devang Patelcb59fd42009-01-12 19:17:34 +00003349 return ID;
3350 }
3351
3352 /// RecordVariable - Indicate the declaration of a local variable.
Devang Patelcb59fd42009-01-12 19:17:34 +00003353 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003354 if (TimePassesIsEnabled)
3355 DebugTimer->startTimer();
3356
Devang Patel2560d922009-01-15 18:25:17 +00003357 DIDescriptor Desc(GV);
3358 DbgScope *Scope = NULL;
Bill Wendlingd9308a62009-03-10 21:23:25 +00003359
Devang Patel2560d922009-01-15 18:25:17 +00003360 if (Desc.getTag() == DW_TAG_variable) {
3361 // GV is a global variable.
3362 DIGlobalVariable DG(GV);
3363 Scope = getOrCreateScope(DG.getContext().getGV());
3364 } else {
3365 // or GV is a local variable.
3366 DIVariable DV(GV);
3367 Scope = getOrCreateScope(DV.getContext().getGV());
3368 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00003369
Bill Wendling6baa18d2009-02-03 21:38:21 +00003370 assert(Scope && "Unable to find variable' scope");
Devang Patel7c8a2772009-01-16 19:28:14 +00003371 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelcb59fd42009-01-12 19:17:34 +00003372 Scope->AddVariable(DV);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003373
3374 if (TimePassesIsEnabled)
3375 DebugTimer->stopTimer();
Devang Patelcb59fd42009-01-12 19:17:34 +00003376 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003377};
3378
3379//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00003380/// DwarfException - Emits Dwarf exception handling directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003381///
3382class DwarfException : public Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003383 struct FunctionEHFrameInfo {
3384 std::string FnName;
3385 unsigned Number;
3386 unsigned PersonalityIndex;
3387 bool hasCalls;
3388 bool hasLandingPads;
3389 std::vector<MachineMove> Moves;
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003390 const Function * function;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003391
3392 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3393 bool hC, bool hL,
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003394 const std::vector<MachineMove> &M,
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003395 const Function *f):
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003396 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003397 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003398 };
3399
3400 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen85535762008-04-02 00:25:04 +00003401
3402 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3403 /// be emitted.
3404 bool shouldEmitTable;
3405
3406 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3407 /// should be emitted.
3408 bool shouldEmitMoves;
3409
3410 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3411 /// should be emitted.
3412 bool shouldEmitTableModule;
3413
aslc200b112008-08-16 12:57:46 +00003414 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen85535762008-04-02 00:25:04 +00003415 /// should be emitted.
3416 bool shouldEmitMovesModule;
Duncan Sands96144f92008-05-07 19:11:09 +00003417
Bill Wendlingd9308a62009-03-10 21:23:25 +00003418 /// ExceptionTimer - Timer for the Dwarf exception writer.
3419 Timer *ExceptionTimer;
3420
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003421 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3422 ///
3423 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
3424 // Size and sign of stack growth.
3425 int stackGrowth =
3426 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3427 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00003428 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003429
3430 // Begin eh frame section.
3431 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling189bde72008-12-24 08:05:17 +00003432
3433 if (!TAI->doesRequireNonLocalEHFrameLabel())
3434 O << TAI->getEHGlobalPrefix();
3435 O << "EH_frame" << Index << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003436 EmitLabel("section_eh_frame", Index);
3437
3438 // Define base labels.
3439 EmitLabel("eh_frame_common", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003440
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003441 // Define the eh frame length.
3442 EmitDifference("eh_frame_common_end", Index,
3443 "eh_frame_common_begin", Index, true);
3444 Asm->EOL("Length of Common Information Entry");
3445
3446 // EH frame header.
3447 EmitLabel("eh_frame_common_begin", Index);
3448 Asm->EmitInt32((int)0);
3449 Asm->EOL("CIE Identifier Tag");
3450 Asm->EmitInt8(DW_CIE_VERSION);
3451 Asm->EOL("CIE Version");
Duncan Sands96144f92008-05-07 19:11:09 +00003452
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003453 // The personality presence indicates that language specific information
3454 // will show up in the eh frame.
3455 Asm->EmitString(Personality ? "zPLR" : "zR");
3456 Asm->EOL("CIE Augmentation");
Duncan Sands96144f92008-05-07 19:11:09 +00003457
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003458 // Round out reader.
3459 Asm->EmitULEB128Bytes(1);
3460 Asm->EOL("CIE Code Alignment Factor");
3461 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands96144f92008-05-07 19:11:09 +00003462 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00003463 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands96144f92008-05-07 19:11:09 +00003464 Asm->EOL("CIE Return Address Column");
3465
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003466 // If there is a personality, we need to indicate the functions location.
3467 if (Personality) {
3468 Asm->EmitULEB128Bytes(7);
3469 Asm->EOL("Augmentation Size");
Bill Wendling2d369922007-09-11 17:20:55 +00003470
Duncan Sands96144f92008-05-07 19:11:09 +00003471 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendling2d369922007-09-11 17:20:55 +00003472 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands96144f92008-05-07 19:11:09 +00003473 Asm->EOL("Personality (pcrel sdata4 indirect)");
3474 } else {
Bill Wendling2d369922007-09-11 17:20:55 +00003475 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands96144f92008-05-07 19:11:09 +00003476 Asm->EOL("Personality (pcrel sdata4)");
3477 }
Bill Wendling2d369922007-09-11 17:20:55 +00003478
Duncan Sands96144f92008-05-07 19:11:09 +00003479 PrintRelDirective(true);
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003480 O << TAI->getPersonalityPrefix();
3481 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3482 O << TAI->getPersonalitySuffix();
Duncan Sands4cc39532008-05-08 12:33:11 +00003483 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3484 O << "-" << TAI->getPCSymbol();
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003485 Asm->EOL("Personality");
Bill Wendling38cb7c92007-08-25 00:51:55 +00003486
Duncan Sands96144f92008-05-07 19:11:09 +00003487 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3488 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003489
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003490 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3491 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003492 } else {
3493 Asm->EmitULEB128Bytes(1);
3494 Asm->EOL("Augmentation Size");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003495
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003496 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3497 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003498 }
3499
3500 // Indicate locations of general callee saved registers in frame.
3501 std::vector<MachineMove> Moves;
3502 RI->getInitialFrameState(Moves);
Dale Johannesenf5a11532007-11-13 19:13:01 +00003503 EmitFrameMoves(NULL, 0, Moves, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003504
Dale Johannesen388f20f2008-04-30 00:43:29 +00003505 // On Darwin the linker honors the alignment of eh_frame, which means it
3506 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3507 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003508 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003509 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003510 EmitLabel("eh_frame_common_end", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003511
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003512 Asm->EOL();
3513 }
Duncan Sands96144f92008-05-07 19:11:09 +00003514
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003515 /// EmitEHFrame - Emit function exception frame information.
3516 ///
3517 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003518 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3519
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003520 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3521
3522 // Externally visible entry into the functions eh frame info.
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003523 // If the corresponding function is static, this should not be
3524 // externally visible.
Rafael Espindolaa168fc92009-01-15 20:18:42 +00003525 if (linkage != Function::InternalLinkage &&
Devang Patel245446c2009-01-17 08:05:14 +00003526 linkage != Function::PrivateLinkage) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003527 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3528 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3529 }
3530
Dale Johannesenf09b5992008-01-10 02:03:30 +00003531 // If corresponding function is weak definition, this should be too.
Duncan Sands19d161f2009-03-07 15:45:40 +00003532 if ((linkage == Function::WeakAnyLinkage ||
3533 linkage == Function::WeakODRLinkage ||
3534 linkage == Function::LinkOnceAnyLinkage ||
3535 linkage == Function::LinkOnceODRLinkage) &&
Dale Johannesenf09b5992008-01-10 02:03:30 +00003536 TAI->getWeakDefDirective())
3537 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3538
3539 // If there are no calls then you can't unwind. This may mean we can
3540 // omit the EH Frame, but some environments do not handle weak absolute
aslc200b112008-08-16 12:57:46 +00003541 // symbols.
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003542 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesena9b3e482008-04-08 00:10:24 +00003543 // unwind info is to be available for non-EH uses.
Dale Johannesenf09b5992008-01-10 02:03:30 +00003544 if (!EHFrameInfo.hasCalls &&
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003545 !UnwindTablesMandatory &&
Duncan Sands19d161f2009-03-07 15:45:40 +00003546 ((linkage != Function::WeakAnyLinkage &&
3547 linkage != Function::WeakODRLinkage &&
3548 linkage != Function::LinkOnceAnyLinkage &&
3549 linkage != Function::LinkOnceODRLinkage) ||
Dale Johannesenf09b5992008-01-10 02:03:30 +00003550 !TAI->getWeakDefDirective() ||
3551 TAI->getSupportsWeakOmittedEHFrame()))
aslc200b112008-08-16 12:57:46 +00003552 {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003553 O << EHFrameInfo.FnName << " = 0\n";
aslc200b112008-08-16 12:57:46 +00003554 // This name has no connection to the function, so it might get
3555 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003556 // dead-stripping unconditionally.
3557 if (const char *UsedDirective = TAI->getUsedDirective())
3558 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003559 } else {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003560 O << EHFrameInfo.FnName << ":\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003561
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003562 // EH frame header.
3563 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3564 "eh_frame_begin", EHFrameInfo.Number, true);
3565 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00003566
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003567 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
3568
Bill Wendling189bde72008-12-24 08:05:17 +00003569 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3570 PrintRelDirective(true, true);
3571 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3572
3573 if (!TAI->isAbsoluteEHSectionOffsets())
3574 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3575 } else {
3576 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3577 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3578 true, true, false);
3579 }
3580
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003581 Asm->EOL("FDE CIE offset");
3582
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003583 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003584 Asm->EOL("FDE initial location");
3585 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003586 "eh_func_begin", EHFrameInfo.Number, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003587 Asm->EOL("FDE address range");
Duncan Sands96144f92008-05-07 19:11:09 +00003588
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003589 // If there is a personality and landing pads then point to the language
3590 // specific data area in the exception table.
3591 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands96144f92008-05-07 19:11:09 +00003592 Asm->EmitULEB128Bytes(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003593 Asm->EOL("Augmentation size");
Duncan Sands96144f92008-05-07 19:11:09 +00003594
3595 if (EHFrameInfo.hasLandingPads)
3596 EmitReference("exception", EHFrameInfo.Number, true, true);
3597 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003598 Asm->EmitInt32((int)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003599 Asm->EOL("Language Specific Data Area");
3600 } else {
3601 Asm->EmitULEB128Bytes(0);
3602 Asm->EOL("Augmentation size");
3603 }
Duncan Sands96144f92008-05-07 19:11:09 +00003604
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003605 // Indicate locations of function specific callee saved registers in
3606 // frame.
Devang Patelb28de842009-01-17 08:01:33 +00003607 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00003608 true);
aslc200b112008-08-16 12:57:46 +00003609
Dale Johannesen388f20f2008-04-30 00:43:29 +00003610 // On Darwin the linker honors the alignment of eh_frame, which means it
3611 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3612 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003613 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003614 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003615 EmitLabel("eh_frame_end", EHFrameInfo.Number);
aslc200b112008-08-16 12:57:46 +00003616
3617 // If the function is marked used, this table should be also. We cannot
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003618 // make the mark unconditional in this case, since retaining the table
aslc200b112008-08-16 12:57:46 +00003619 // also retains the function in this case, and there is code around
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003620 // that depends on unused functions (calling undefined externals) being
3621 // dead-stripped to link correctly. Yes, there really is.
3622 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3623 if (const char *UsedDirective = TAI->getUsedDirective())
3624 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3625 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003626 }
3627
Duncan Sands241a0c92007-09-05 11:27:52 +00003628 /// EmitExceptionTable - Emit landing pads and actions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003629 ///
3630 /// The general organization of the table is complex, but the basic concepts
3631 /// are easy. First there is a header which describes the location and
3632 /// organization of the three components that follow.
3633 /// 1. The landing pad site information describes the range of code covered
3634 /// by the try. In our case it's an accumulation of the ranges covered
3635 /// by the invokes in the try. There is also a reference to the landing
3636 /// pad that handles the exception once processed. Finally an index into
3637 /// the actions table.
3638 /// 2. The action table, in our case, is composed of pairs of type ids
3639 /// and next action offset. Starting with the action index from the
3640 /// landing pad site, each type Id is checked for a match to the current
3641 /// exception. If it matches then the exception and type id are passed
3642 /// on to the landing pad. Otherwise the next action is looked up. This
3643 /// chain is terminated with a next action of zero. If no type id is
3644 /// found the the frame is unwound and handling continues.
3645 /// 3. Type id table contains references to all the C++ typeinfo for all
3646 /// catches in the function. This tables is reversed indexed base 1.
3647
3648 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3649 static unsigned SharedTypeIds(const LandingPadInfo *L,
3650 const LandingPadInfo *R) {
3651 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3652 unsigned LSize = LIds.size(), RSize = RIds.size();
3653 unsigned MinSize = LSize < RSize ? LSize : RSize;
3654 unsigned Count = 0;
3655
3656 for (; Count != MinSize; ++Count)
3657 if (LIds[Count] != RIds[Count])
3658 return Count;
3659
3660 return Count;
3661 }
3662
3663 /// PadLT - Order landing pads lexicographically by type id.
3664 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
3665 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3666 unsigned LSize = LIds.size(), RSize = RIds.size();
3667 unsigned MinSize = LSize < RSize ? LSize : RSize;
3668
3669 for (unsigned i = 0; i != MinSize; ++i)
3670 if (LIds[i] != RIds[i])
3671 return LIds[i] < RIds[i];
3672
3673 return LSize < RSize;
3674 }
3675
3676 struct KeyInfo {
3677 static inline unsigned getEmptyKey() { return -1U; }
3678 static inline unsigned getTombstoneKey() { return -2U; }
3679 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner92eea072007-09-17 18:34:04 +00003680 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003681 static bool isPod() { return true; }
3682 };
3683
Duncan Sands241a0c92007-09-05 11:27:52 +00003684 /// ActionEntry - Structure describing an entry in the actions table.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003685 struct ActionEntry {
3686 int ValueForTypeID; // The value to write - may not be equal to the type id.
3687 int NextAction;
3688 struct ActionEntry *Previous;
3689 };
3690
Duncan Sands241a0c92007-09-05 11:27:52 +00003691 /// PadRange - Structure holding a try-range and the associated landing pad.
3692 struct PadRange {
3693 // The index of the landing pad.
3694 unsigned PadIndex;
3695 // The index of the begin and end labels in the landing pad's label lists.
3696 unsigned RangeIndex;
3697 };
3698
3699 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3700
3701 /// CallSiteEntry - Structure describing an entry in the call-site table.
3702 struct CallSiteEntry {
Duncan Sands4ff179f2007-12-19 07:36:31 +00003703 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003704 unsigned BeginLabel; // zero indicates the start of the function.
3705 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003706 // The landing pad starts at PadLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003707 unsigned PadLabel; // zero indicates that there is no landing pad.
3708 unsigned Action;
3709 };
3710
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003711 void EmitExceptionTable() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003712 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
3713 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
3714 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3715 if (PadInfos.empty()) return;
3716
3717 // Sort the landing pads in order of their type ids. This is used to fold
3718 // duplicate actions.
3719 SmallVector<const LandingPadInfo *, 64> LandingPads;
3720 LandingPads.reserve(PadInfos.size());
3721 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
3722 LandingPads.push_back(&PadInfos[i]);
3723 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3724
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003725 // Negative type ids index into FilterIds, positive type ids index into
3726 // TypeInfos. The value written for a positive type id is just the type
3727 // id itself. For a negative type id, however, the value written is the
3728 // (negative) byte offset of the corresponding FilterIds entry. The byte
3729 // offset is usually equal to the type id, because the FilterIds entries
3730 // are written using a variable width encoding which outputs one byte per
3731 // entry as long as the value written is not too large, but can differ.
3732 // This kind of complication does not occur for positive type ids because
3733 // type infos are output using a fixed width encoding.
3734 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3735 SmallVector<int, 16> FilterOffsets;
3736 FilterOffsets.reserve(FilterIds.size());
3737 int Offset = -1;
3738 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3739 E = FilterIds.end(); I != E; ++I) {
3740 FilterOffsets.push_back(Offset);
aslc200b112008-08-16 12:57:46 +00003741 Offset -= TargetAsmInfo::getULEB128Size(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003742 }
3743
Duncan Sands241a0c92007-09-05 11:27:52 +00003744 // Compute the actions table and gather the first action index for each
3745 // landing pad site.
3746 SmallVector<ActionEntry, 32> Actions;
3747 SmallVector<unsigned, 64> FirstActions;
3748 FirstActions.reserve(LandingPads.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003749
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003750 int FirstAction = 0;
Duncan Sands241a0c92007-09-05 11:27:52 +00003751 unsigned SizeActions = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003752 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3753 const LandingPadInfo *LP = LandingPads[i];
3754 const std::vector<int> &TypeIds = LP->TypeIds;
3755 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
3756 unsigned SizeSiteActions = 0;
3757
3758 if (NumShared < TypeIds.size()) {
3759 unsigned SizeAction = 0;
3760 ActionEntry *PrevAction = 0;
3761
3762 if (NumShared) {
3763 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3764 assert(Actions.size());
3765 PrevAction = &Actions.back();
aslc200b112008-08-16 12:57:46 +00003766 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3767 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003768 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
aslc200b112008-08-16 12:57:46 +00003769 SizeAction -=
3770 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003771 SizeAction += -PrevAction->NextAction;
3772 PrevAction = PrevAction->Previous;
3773 }
3774 }
3775
3776 // Compute the actions.
3777 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3778 int TypeID = TypeIds[I];
3779 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3780 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
aslc200b112008-08-16 12:57:46 +00003781 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003782
3783 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
aslc200b112008-08-16 12:57:46 +00003784 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003785 SizeSiteActions += SizeAction;
3786
3787 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
3788 Actions.push_back(Action);
3789
3790 PrevAction = &Actions.back();
3791 }
3792
3793 // Record the first action of the landing pad site.
3794 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3795 } // else identical - re-use previous FirstAction
3796
3797 FirstActions.push_back(FirstAction);
3798
3799 // Compute this sites contribution to size.
3800 SizeActions += SizeSiteActions;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003801 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003802
Duncan Sands4ff179f2007-12-19 07:36:31 +00003803 // Compute the call-site table. The entry for an invoke has a try-range
3804 // containing the call, a non-zero landing pad and an appropriate action.
3805 // The entry for an ordinary call has a try-range containing the call and
3806 // zero for the landing pad and the action. Calls marked 'nounwind' have
3807 // no entry and must not be contained in the try-range of any entry - they
3808 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands241a0c92007-09-05 11:27:52 +00003809 SmallVector<CallSiteEntry, 64> CallSites;
3810
3811 RangeMapType PadMap;
Duncan Sands4ff179f2007-12-19 07:36:31 +00003812 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3813 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3814 // try-ranges for them need be deduced.
Duncan Sands241a0c92007-09-05 11:27:52 +00003815 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3816 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003817 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003818 unsigned BeginLabel = LandingPad->BeginLabels[j];
3819 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3820 PadRange P = { i, j };
3821 PadMap[BeginLabel] = P;
3822 }
3823 }
3824
Duncan Sands4ff179f2007-12-19 07:36:31 +00003825 // The end label of the previous invoke or nounwind try-range.
Duncan Sands241a0c92007-09-05 11:27:52 +00003826 unsigned LastLabel = 0;
Duncan Sands4ff179f2007-12-19 07:36:31 +00003827
3828 // Whether there is a potentially throwing instruction (currently this means
3829 // an ordinary call) between the end of the previous try-range and now.
3830 bool SawPotentiallyThrowing = false;
3831
3832 // Whether the last callsite entry was for an invoke.
3833 bool PreviousIsInvoke = false;
3834
Duncan Sands4ff179f2007-12-19 07:36:31 +00003835 // Visit all instructions in order of address.
Duncan Sands241a0c92007-09-05 11:27:52 +00003836 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3837 I != E; ++I) {
3838 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3839 MI != E; ++MI) {
Dan Gohmanfa607c92008-07-01 00:05:16 +00003840 if (!MI->isLabel()) {
Chris Lattner5b930372008-01-07 07:27:27 +00003841 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands241a0c92007-09-05 11:27:52 +00003842 continue;
3843 }
3844
Chris Lattnerda4cff12007-12-30 20:50:28 +00003845 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands241a0c92007-09-05 11:27:52 +00003846 assert(BeginLabel && "Invalid label!");
Duncan Sands89372f62007-09-05 14:12:46 +00003847
Duncan Sands4ff179f2007-12-19 07:36:31 +00003848 // End of the previous try-range?
Duncan Sands89372f62007-09-05 14:12:46 +00003849 if (BeginLabel == LastLabel)
Duncan Sands4ff179f2007-12-19 07:36:31 +00003850 SawPotentiallyThrowing = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003851
Duncan Sands4ff179f2007-12-19 07:36:31 +00003852 // Beginning of a new try-range?
Duncan Sands241a0c92007-09-05 11:27:52 +00003853 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands241a0c92007-09-05 11:27:52 +00003854 if (L == PadMap.end())
Duncan Sands4ff179f2007-12-19 07:36:31 +00003855 // Nope, it was just some random label.
Duncan Sands241a0c92007-09-05 11:27:52 +00003856 continue;
3857
3858 PadRange P = L->second;
3859 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3860
3861 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3862 "Inconsistent landing pad map!");
3863
3864 // If some instruction between the previous try-range and this one may
3865 // throw, create a call-site entry with no landing pad for the region
3866 // between the try-ranges.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003867 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003868 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3869 CallSites.push_back(Site);
Duncan Sands4ff179f2007-12-19 07:36:31 +00003870 PreviousIsInvoke = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003871 }
3872
3873 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003874 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands241a0c92007-09-05 11:27:52 +00003875
Duncan Sands4ff179f2007-12-19 07:36:31 +00003876 if (LandingPad->LandingPadLabel) {
3877 // This try-range is for an invoke.
3878 CallSiteEntry Site = {BeginLabel, LastLabel,
3879 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands241a0c92007-09-05 11:27:52 +00003880
Duncan Sands4ff179f2007-12-19 07:36:31 +00003881 // Try to merge with the previous call-site.
3882 if (PreviousIsInvoke) {
Dan Gohman3d436002008-06-21 22:00:54 +00003883 CallSiteEntry &Prev = CallSites.back();
Duncan Sands4ff179f2007-12-19 07:36:31 +00003884 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3885 // Extend the range of the previous entry.
3886 Prev.EndLabel = Site.EndLabel;
3887 continue;
3888 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003889 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003890
Duncan Sands4ff179f2007-12-19 07:36:31 +00003891 // Otherwise, create a new call-site.
3892 CallSites.push_back(Site);
3893 PreviousIsInvoke = true;
3894 } else {
3895 // Create a gap.
3896 PreviousIsInvoke = false;
3897 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003898 }
3899 }
3900 // If some instruction between the previous try-range and the end of the
3901 // function may throw, create a call-site entry with no landing pad for the
3902 // region following the try-range.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003903 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003904 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3905 CallSites.push_back(Site);
3906 }
3907
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003908 // Final tallies.
Duncan Sands96144f92008-05-07 19:11:09 +00003909
3910 // Call sites.
3911 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
3912 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
3913 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
3914 unsigned SizeSites = CallSites.size() * (SiteStartSize +
3915 SiteLengthSize +
3916 LandingPadSize);
Duncan Sands241a0c92007-09-05 11:27:52 +00003917 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
aslc200b112008-08-16 12:57:46 +00003918 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands241a0c92007-09-05 11:27:52 +00003919
Duncan Sands96144f92008-05-07 19:11:09 +00003920 // Type infos.
3921 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3922 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003923
3924 unsigned TypeOffset = sizeof(int8_t) + // Call site format
aslc200b112008-08-16 12:57:46 +00003925 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003926 SizeSites + SizeActions + SizeTypes;
3927
3928 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3929 sizeof(int8_t) + // TType format
aslc200b112008-08-16 12:57:46 +00003930 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003931 TypeOffset;
3932
3933 unsigned SizeAlign = (4 - TotalSize) & 3;
3934
3935 // Begin the exception table.
3936 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng7e7d1942008-02-29 19:36:59 +00003937 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesen841e4982008-10-08 21:50:21 +00003938 O << "GCC_except_table" << SubprogramCount << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003939 for (unsigned i = 0; i != SizeAlign; ++i) {
3940 Asm->EmitInt8(0);
3941 Asm->EOL("Padding");
3942 }
3943 EmitLabel("exception", SubprogramCount);
3944
3945 // Emit the header.
3946 Asm->EmitInt8(DW_EH_PE_omit);
3947 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3948 Asm->EmitInt8(DW_EH_PE_absptr);
3949 Asm->EOL("TType format (DW_EH_PE_absptr)");
3950 Asm->EmitULEB128Bytes(TypeOffset);
3951 Asm->EOL("TType base offset");
3952 Asm->EmitInt8(DW_EH_PE_udata4);
3953 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3954 Asm->EmitULEB128Bytes(SizeSites);
3955 Asm->EOL("Call-site table length");
3956
Duncan Sands241a0c92007-09-05 11:27:52 +00003957 // Emit the landing pad site information.
3958 for (unsigned i = 0; i < CallSites.size(); ++i) {
3959 CallSiteEntry &S = CallSites[i];
3960 const char *BeginTag;
3961 unsigned BeginNumber;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003962
Duncan Sands241a0c92007-09-05 11:27:52 +00003963 if (!S.BeginLabel) {
3964 BeginTag = "eh_func_begin";
3965 BeginNumber = SubprogramCount;
3966 } else {
3967 BeginTag = "label";
3968 BeginNumber = S.BeginLabel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003969 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003970
Duncan Sands241a0c92007-09-05 11:27:52 +00003971 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00003972 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003973 Asm->EOL("Region start");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003974
Duncan Sands241a0c92007-09-05 11:27:52 +00003975 if (!S.EndLabel) {
Dale Johannesen4670be42008-01-15 23:24:56 +00003976 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands96144f92008-05-07 19:11:09 +00003977 true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003978 } else {
Duncan Sands96144f92008-05-07 19:11:09 +00003979 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003980 }
3981 Asm->EOL("Region length");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003982
Duncan Sands96144f92008-05-07 19:11:09 +00003983 if (!S.PadLabel)
3984 Asm->EmitInt32(0);
3985 else
Duncan Sands241a0c92007-09-05 11:27:52 +00003986 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00003987 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003988 Asm->EOL("Landing pad");
3989
3990 Asm->EmitULEB128Bytes(S.Action);
3991 Asm->EOL("Action");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003992 }
3993
3994 // Emit the actions.
3995 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
3996 ActionEntry &Action = Actions[I];
3997
3998 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
3999 Asm->EOL("TypeInfo index");
4000 Asm->EmitSLEB128Bytes(Action.NextAction);
4001 Asm->EOL("Next action");
4002 }
4003
4004 // Emit the type ids.
4005 for (unsigned M = TypeInfos.size(); M; --M) {
4006 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00004007
4008 PrintRelDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004009
4010 if (GV)
4011 O << Asm->getGlobalLinkName(GV);
4012 else
4013 O << "0";
Duncan Sands241a0c92007-09-05 11:27:52 +00004014
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004015 Asm->EOL("TypeInfo");
4016 }
4017
4018 // Emit the filter typeids.
4019 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4020 unsigned TypeID = FilterIds[j];
4021 Asm->EmitULEB128Bytes(TypeID);
4022 Asm->EOL("Filter TypeInfo index");
4023 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004024
Evan Cheng7e7d1942008-02-29 19:36:59 +00004025 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004026 }
4027
4028public:
4029 //===--------------------------------------------------------------------===//
4030 // Main entry points.
4031 //
Owen Anderson847b99b2008-08-21 00:14:44 +00004032 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendlingd9308a62009-03-10 21:23:25 +00004033 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
4034 shouldEmitTableModule(false), shouldEmitMovesModule(false),
4035 ExceptionTimer(0) {
4036 if (TimePassesIsEnabled)
4037 ExceptionTimer = new Timer("Dwarf Exception Writer",
Bill Wendling148ecc42009-03-10 22:58:53 +00004038 getDwarfTimerGroup());
Bill Wendlingd9308a62009-03-10 21:23:25 +00004039 }
aslc200b112008-08-16 12:57:46 +00004040
Bill Wendlingd9308a62009-03-10 21:23:25 +00004041 virtual ~DwarfException() {
4042 delete ExceptionTimer;
4043 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004044
4045 /// SetModuleInfo - Set machine module information when it's known that pass
4046 /// manager has created it. Set by the target AsmPrinter.
4047 void SetModuleInfo(MachineModuleInfo *mmi) {
4048 MMI = mmi;
4049 }
4050
4051 /// BeginModule - Emit all exception information that should come prior to the
4052 /// content.
4053 void BeginModule(Module *M) {
4054 this->M = M;
4055 }
4056
4057 /// EndModule - Emit all exception information that should come after the
4058 /// content.
4059 void EndModule() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004060 if (TimePassesIsEnabled)
4061 ExceptionTimer->startTimer();
4062
Dale Johannesen85535762008-04-02 00:25:04 +00004063 if (shouldEmitMovesModule || shouldEmitTableModule) {
4064 const std::vector<Function *> Personalities = MMI->getPersonalities();
Evan Cheng3e288912009-02-25 07:04:34 +00004065 for (unsigned i = 0; i < Personalities.size(); ++i)
Dale Johannesen85535762008-04-02 00:25:04 +00004066 EmitCommonEHFrame(Personalities[i], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004067
Dale Johannesen85535762008-04-02 00:25:04 +00004068 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4069 E = EHFrames.end(); I != E; ++I)
4070 EmitEHFrame(*I);
4071 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00004072
4073 if (TimePassesIsEnabled)
4074 ExceptionTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004075 }
4076
aslc200b112008-08-16 12:57:46 +00004077 /// BeginFunction - Gather pre-function exception information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004078 /// emitted immediately after the function entry point.
4079 void BeginFunction(MachineFunction *MF) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004080 if (TimePassesIsEnabled)
4081 ExceptionTimer->startTimer();
4082
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004083 this->MF = MF;
Dale Johannesen85535762008-04-02 00:25:04 +00004084 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen85535762008-04-02 00:25:04 +00004085
Bill Wendlingd9308a62009-03-10 21:23:25 +00004086 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen85535762008-04-02 00:25:04 +00004087 // Map all labels and get rid of any dead landing pads.
4088 MMI->TidyLandingPads();
Bill Wendlingd9308a62009-03-10 21:23:25 +00004089
Dale Johannesen85535762008-04-02 00:25:04 +00004090 // If any landing pads survive, we need an EH table.
4091 if (MMI->getLandingPads().size())
4092 shouldEmitTable = true;
4093
4094 // See if we need frame move info.
Duncan Sandscbc28b12008-07-04 09:55:48 +00004095 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen85535762008-04-02 00:25:04 +00004096 shouldEmitMoves = true;
4097
4098 if (shouldEmitMoves || shouldEmitTable)
4099 // Assumes in correct section after the entry point.
4100 EmitLabel("eh_func_begin", ++SubprogramCount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004101 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00004102
Dale Johannesen85535762008-04-02 00:25:04 +00004103 shouldEmitTableModule |= shouldEmitTable;
4104 shouldEmitMovesModule |= shouldEmitMoves;
Bill Wendlingd9308a62009-03-10 21:23:25 +00004105
4106 if (TimePassesIsEnabled)
4107 ExceptionTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004108 }
4109
4110 /// EndFunction - Gather and emit post-function exception information.
4111 ///
4112 void EndFunction() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004113 if (TimePassesIsEnabled)
4114 ExceptionTimer->startTimer();
4115
Dale Johannesen85535762008-04-02 00:25:04 +00004116 if (shouldEmitMoves || shouldEmitTable) {
4117 EmitLabel("eh_func_end", SubprogramCount);
4118 EmitExceptionTable();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004119
Dale Johannesen85535762008-04-02 00:25:04 +00004120 // Save EH frame information
4121 EHFrames.
4122 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendlingd9308a62009-03-10 21:23:25 +00004123 SubprogramCount,
4124 MMI->getPersonalityIndex(),
4125 MF->getFrameInfo()->hasCalls(),
4126 !MMI->getLandingPads().empty(),
4127 MMI->getFrameMoves(),
4128 MF->getFunction()));
4129 }
4130
4131 if (TimePassesIsEnabled)
4132 ExceptionTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004133 }
4134};
4135
4136} // End of namespace llvm
4137
4138//===----------------------------------------------------------------------===//
4139
4140/// Emit - Print the abbreviation using the specified Dwarf writer.
4141///
4142void DIEAbbrev::Emit(const DwarfDebug &DD) const {
4143 // Emit its Dwarf tag type.
4144 DD.getAsm()->EmitULEB128Bytes(Tag);
4145 DD.getAsm()->EOL(TagString(Tag));
aslc200b112008-08-16 12:57:46 +00004146
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004147 // Emit whether it has children DIEs.
4148 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4149 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
aslc200b112008-08-16 12:57:46 +00004150
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004151 // For each attribute description.
4152 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4153 const DIEAbbrevData &AttrData = Data[i];
aslc200b112008-08-16 12:57:46 +00004154
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004155 // Emit attribute type.
4156 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4157 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
aslc200b112008-08-16 12:57:46 +00004158
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004159 // Emit form type.
4160 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4161 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
4162 }
4163
4164 // Mark end of abbreviation.
4165 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4166 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
4167}
4168
4169#ifndef NDEBUG
4170void DIEAbbrev::print(std::ostream &O) {
4171 O << "Abbreviation @"
4172 << std::hex << (intptr_t)this << std::dec
4173 << " "
4174 << TagString(Tag)
4175 << " "
4176 << ChildrenString(ChildrenFlag)
4177 << "\n";
aslc200b112008-08-16 12:57:46 +00004178
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004179 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4180 O << " "
4181 << AttributeString(Data[i].getAttribute())
4182 << " "
4183 << FormEncodingString(Data[i].getForm())
4184 << "\n";
4185 }
4186}
4187void DIEAbbrev::dump() { print(cerr); }
4188#endif
4189
4190//===----------------------------------------------------------------------===//
4191
4192#ifndef NDEBUG
4193void DIEValue::dump() {
4194 print(cerr);
4195}
4196#endif
4197
4198//===----------------------------------------------------------------------===//
4199
4200/// EmitValue - Emit integer of appropriate size.
4201///
4202void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
4203 switch (Form) {
4204 case DW_FORM_flag: // Fall thru
4205 case DW_FORM_ref1: // Fall thru
4206 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
4207 case DW_FORM_ref2: // Fall thru
4208 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
4209 case DW_FORM_ref4: // Fall thru
4210 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
4211 case DW_FORM_ref8: // Fall thru
4212 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4213 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4214 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
4215 default: assert(0 && "DIE Value form not supported yet"); break;
4216 }
4217}
4218
4219/// SizeOf - Determine size of integer value in bytes.
4220///
4221unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4222 switch (Form) {
4223 case DW_FORM_flag: // Fall thru
4224 case DW_FORM_ref1: // Fall thru
4225 case DW_FORM_data1: return sizeof(int8_t);
4226 case DW_FORM_ref2: // Fall thru
4227 case DW_FORM_data2: return sizeof(int16_t);
4228 case DW_FORM_ref4: // Fall thru
4229 case DW_FORM_data4: return sizeof(int32_t);
4230 case DW_FORM_ref8: // Fall thru
4231 case DW_FORM_data8: return sizeof(int64_t);
aslc200b112008-08-16 12:57:46 +00004232 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4233 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004234 default: assert(0 && "DIE Value form not supported yet"); break;
4235 }
4236 return 0;
4237}
4238
4239//===----------------------------------------------------------------------===//
4240
4241/// EmitValue - Emit string value.
4242///
4243void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Bill Wendling15afa002009-03-10 23:57:09 +00004244 DD.getAsm()->EmitString(Str);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004245}
4246
4247//===----------------------------------------------------------------------===//
4248
4249/// EmitValue - Emit label value.
4250///
4251void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004252 bool IsSmall = Form == DW_FORM_data4;
4253 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004254}
4255
4256/// SizeOf - Determine size of label value in bytes.
4257///
4258unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004259 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004260 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004261}
4262
4263//===----------------------------------------------------------------------===//
4264
4265/// EmitValue - Emit label value.
4266///
4267void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004268 bool IsSmall = Form == DW_FORM_data4;
4269 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004270}
4271
4272/// SizeOf - Determine size of label value in bytes.
4273///
4274unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004275 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004276 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004277}
aslc200b112008-08-16 12:57:46 +00004278
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004279//===----------------------------------------------------------------------===//
4280
4281/// EmitValue - Emit delta value.
4282///
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004283void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4284 bool IsSmall = Form == DW_FORM_data4;
4285 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4286 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4287}
4288
4289/// SizeOf - Determine size of delta value in bytes.
4290///
4291unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4292 if (Form == DW_FORM_data4) return 4;
4293 return DD.getTargetData()->getPointerSize();
4294}
aslc200b112008-08-16 12:57:46 +00004295
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004296//===----------------------------------------------------------------------===//
4297
4298/// EmitValue - Emit delta value.
4299///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004300void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
4301 bool IsSmall = Form == DW_FORM_data4;
4302 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
4303}
4304
4305/// SizeOf - Determine size of delta value in bytes.
4306///
4307unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4308 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004309 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004310}
4311
4312//===----------------------------------------------------------------------===//
4313
4314/// EmitValue - Emit debug information entry offset.
4315///
4316void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
4317 DD.getAsm()->EmitInt32(Entry->getOffset());
4318}
aslc200b112008-08-16 12:57:46 +00004319
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004320//===----------------------------------------------------------------------===//
4321
4322/// ComputeSize - calculate the size of the block.
4323///
4324unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
4325 if (!Size) {
Owen Anderson88dd6232008-06-24 21:44:59 +00004326 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004327
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004328 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4329 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
4330 }
4331 }
4332 return Size;
4333}
4334
4335/// EmitValue - Emit block data.
4336///
4337void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
4338 switch (Form) {
4339 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4340 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4341 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4342 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
4343 default: assert(0 && "Improper form for block"); break;
4344 }
aslc200b112008-08-16 12:57:46 +00004345
Owen Anderson88dd6232008-06-24 21:44:59 +00004346 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004347
4348 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4349 DD.getAsm()->EOL();
4350 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
4351 }
4352}
4353
4354/// SizeOf - Determine size of block data in bytes.
4355///
4356unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4357 switch (Form) {
4358 case DW_FORM_block1: return Size + sizeof(int8_t);
4359 case DW_FORM_block2: return Size + sizeof(int16_t);
4360 case DW_FORM_block4: return Size + sizeof(int32_t);
aslc200b112008-08-16 12:57:46 +00004361 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004362 default: assert(0 && "Improper form for block"); break;
4363 }
4364 return 0;
4365}
4366
4367//===----------------------------------------------------------------------===//
4368/// DIE Implementation
4369
4370DIE::~DIE() {
4371 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4372 delete Children[i];
4373}
aslc200b112008-08-16 12:57:46 +00004374
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004375/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4376///
4377void DIE::AddSiblingOffset() {
4378 DIEInteger *DI = new DIEInteger(0);
4379 Values.insert(Values.begin(), DI);
4380 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
4381}
4382
4383/// Profile - Used to gather unique data for the value folding set.
4384///
4385void DIE::Profile(FoldingSetNodeID &ID) {
4386 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00004387
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004388 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4389 ID.AddPointer(Children[i]);
4390
4391 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4392 ID.AddPointer(Values[j]);
4393}
4394
4395#ifndef NDEBUG
4396void DIE::print(std::ostream &O, unsigned IncIndent) {
4397 static unsigned IndentCount = 0;
4398 IndentCount += IncIndent;
4399 const std::string Indent(IndentCount, ' ');
4400 bool isBlock = Abbrev.getTag() == 0;
aslc200b112008-08-16 12:57:46 +00004401
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004402 if (!isBlock) {
4403 O << Indent
4404 << "Die: "
4405 << "0x" << std::hex << (intptr_t)this << std::dec
4406 << ", Offset: " << Offset
4407 << ", Size: " << Size
aslc200b112008-08-16 12:57:46 +00004408 << "\n";
4409
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004410 O << Indent
4411 << TagString(Abbrev.getTag())
4412 << " "
4413 << ChildrenString(Abbrev.getChildrenFlag());
4414 } else {
4415 O << "Size: " << Size;
4416 }
4417 O << "\n";
4418
Owen Anderson88dd6232008-06-24 21:44:59 +00004419 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004420
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004421 IndentCount += 2;
4422 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4423 O << Indent;
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004424
4425 if (!isBlock)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004426 O << AttributeString(Data[i].getAttribute());
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004427 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004428 O << "Blk[" << i << "]";
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004429
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004430 O << " "
4431 << FormEncodingString(Data[i].getForm())
4432 << " ";
4433 Values[i]->print(O);
4434 O << "\n";
4435 }
4436 IndentCount -= 2;
4437
4438 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4439 Children[j]->print(O, 4);
4440 }
aslc200b112008-08-16 12:57:46 +00004441
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004442 if (!isBlock) O << "\n";
4443 IndentCount -= IncIndent;
4444}
4445
4446void DIE::dump() {
4447 print(cerr);
4448}
4449#endif
4450
4451//===----------------------------------------------------------------------===//
4452/// DwarfWriter Implementation
4453///
4454
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004455DwarfWriter::DwarfWriter()
Bill Wendlingd9308a62009-03-10 21:23:25 +00004456 : ImmutablePass(&ID), DD(0), DE(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004457
4458DwarfWriter::~DwarfWriter() {
4459 delete DE;
4460 delete DD;
4461}
4462
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004463/// BeginModule - Emit all Dwarf sections that should come prior to the
4464/// content.
Devang Patelaa1e8432009-01-08 23:40:34 +00004465void DwarfWriter::BeginModule(Module *M,
4466 MachineModuleInfo *MMI,
4467 raw_ostream &OS, AsmPrinter *A,
4468 const TargetAsmInfo *T) {
4469 DE = new DwarfException(OS, A, T);
4470 DD = new DwarfDebug(OS, A, T);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004471 DE->BeginModule(M);
4472 DD->BeginModule(M);
Devang Patel6ccd57e2009-01-13 00:20:51 +00004473 DD->SetDebugInfo(MMI);
Devang Patelaa1e8432009-01-08 23:40:34 +00004474 DE->SetModuleInfo(MMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004475}
4476
4477/// EndModule - Emit all Dwarf sections that should come after the content.
4478///
4479void DwarfWriter::EndModule() {
4480 DE->EndModule();
4481 DD->EndModule();
4482}
4483
aslc200b112008-08-16 12:57:46 +00004484/// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004485/// emitted immediately after the function entry point.
4486void DwarfWriter::BeginFunction(MachineFunction *MF) {
4487 DE->BeginFunction(MF);
4488 DD->BeginFunction(MF);
4489}
4490
4491/// EndFunction - Gather and emit post-function debug information.
4492///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00004493void DwarfWriter::EndFunction(MachineFunction *MF) {
4494 DD->EndFunction(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004495 DE->EndFunction();
aslc200b112008-08-16 12:57:46 +00004496
Bill Wendling5b4796a2008-07-22 00:53:37 +00004497 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004498 // Clear function debug information.
4499 MMI->EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004500}
Devang Patelcb59fd42009-01-12 19:17:34 +00004501
Devang Patel2da0cc42009-01-15 23:41:32 +00004502/// ValidDebugInfo - Return true if V represents valid debug info value.
4503bool DwarfWriter::ValidDebugInfo(Value *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004504 return DD && DD->ValidDebugInfo(V);
Devang Patel2da0cc42009-01-15 23:41:32 +00004505}
4506
Devang Patelcb59fd42009-01-12 19:17:34 +00004507/// RecordSourceLine - Records location information and associates it with a
4508/// label. Returns a unique label ID used to generate a label and provide
4509/// correspondence to the source line list.
4510unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
4511 unsigned Src) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004512 return DD->RecordSourceLine(Line, Col, Src);
Devang Patelcb59fd42009-01-12 19:17:34 +00004513}
4514
Evan Cheng3e288912009-02-25 07:04:34 +00004515/// getOrCreateSourceID - Look up the source id with the given directory and
4516/// source file names. If none currently exists, create a new id and insert it
4517/// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
4518/// as well.
4519unsigned DwarfWriter::getOrCreateSourceID(const std::string &DirName,
4520 const std::string &FileName) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004521 return DD->getOrCreateSourceID(DirName, FileName);
Devang Patelcb59fd42009-01-12 19:17:34 +00004522}
4523
4524/// RecordRegionStart - Indicate the start of a region.
4525unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004526 return DD->RecordRegionStart(V);
Devang Patelcb59fd42009-01-12 19:17:34 +00004527}
4528
4529/// RecordRegionEnd - Indicate the end of a region.
4530unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004531 return DD->RecordRegionEnd(V);
Devang Patelcb59fd42009-01-12 19:17:34 +00004532}
4533
4534/// getRecordSourceLineCount - Count source lines.
4535unsigned DwarfWriter::getRecordSourceLineCount() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004536 return DD->getRecordSourceLineCount();
Devang Patelcb59fd42009-01-12 19:17:34 +00004537}
Devang Patel70190872009-01-13 21:25:00 +00004538
Devang Patelfe359e72009-01-13 21:44:10 +00004539/// RecordVariable - Indicate the declaration of a local variable.
4540///
4541void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
4542 DD->RecordVariable(GV, FrameIndex);
4543}
Devang Patel42f6bed2009-01-13 23:54:55 +00004544
Bill Wendling50db0792009-02-20 00:44:43 +00004545/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
4546/// be emitted.
4547bool DwarfWriter::ShouldEmitDwarfDebug() const {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004548 return DD->ShouldEmitDwarfDebug();
Bill Wendling50db0792009-02-20 00:44:43 +00004549}