blob: 6b1d56ec1cbda59a9b5f84adab4d2f9671b0ab56 [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"
15
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/FoldingSet.h"
18#include "llvm/ADT/StringExtras.h"
19#include "llvm/ADT/UniqueVector.h"
20#include "llvm/Module.h"
21#include "llvm/Type.h"
22#include "llvm/CodeGen/AsmPrinter.h"
23#include "llvm/CodeGen/MachineModuleInfo.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineLocation.h"
Devang Patelfc187162009-01-05 17:57:47 +000026#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/Support/Debug.h"
28#include "llvm/Support/Dwarf.h"
29#include "llvm/Support/CommandLine.h"
30#include "llvm/Support/DataTypes.h"
31#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000032#include "llvm/Support/raw_ostream.h"
Dan Gohman80bbde72007-09-24 21:32:18 +000033#include "llvm/System/Path.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000035#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/Target/TargetData.h"
37#include "llvm/Target/TargetFrameInfo.h"
38#include "llvm/Target/TargetInstrInfo.h"
39#include "llvm/Target/TargetMachine.h"
40#include "llvm/Target/TargetOptions.h"
41#include <ostream>
42#include <string>
43using namespace llvm;
44using namespace llvm::dwarf;
45
46namespace llvm {
aslc200b112008-08-16 12:57:46 +000047
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048//===----------------------------------------------------------------------===//
49
50/// Configuration values for initial hash set sizes (log2).
51///
52static const unsigned InitDiesSetSize = 9; // 512
53static const unsigned InitAbbreviationsSetSize = 9; // 512
54static const unsigned InitValuesSetSize = 9; // 512
55
56//===----------------------------------------------------------------------===//
57/// Forward declarations.
58///
59class DIE;
60class DIEValue;
61
62//===----------------------------------------------------------------------===//
63/// DWLabel - Labels are used to track locations in the assembler file.
aslc200b112008-08-16 12:57:46 +000064/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
65/// where the tag is a category of label (Ex. location) and number is a value
Reid Spencer37c7cea2007-08-05 20:06:04 +000066/// unique in that category.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067class DWLabel {
68public:
69 /// Tag - Label category tag. Should always be a staticly declared C string.
70 ///
71 const char *Tag;
aslc200b112008-08-16 12:57:46 +000072
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 /// Number - Value to make label unique.
74 ///
75 unsigned Number;
76
77 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
aslc200b112008-08-16 12:57:46 +000078
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 void Profile(FoldingSetNodeID &ID) const {
80 ID.AddString(std::string(Tag));
81 ID.AddInteger(Number);
82 }
aslc200b112008-08-16 12:57:46 +000083
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084#ifndef NDEBUG
85 void print(std::ostream *O) const {
86 if (O) print(*O);
87 }
88 void print(std::ostream &O) const {
89 O << "." << Tag;
90 if (Number) O << Number;
91 }
92#endif
93};
94
95//===----------------------------------------------------------------------===//
96/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
97/// Dwarf abbreviation.
98class DIEAbbrevData {
99private:
100 /// Attribute - Dwarf attribute code.
101 ///
102 unsigned Attribute;
aslc200b112008-08-16 12:57:46 +0000103
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 /// Form - Dwarf form code.
aslc200b112008-08-16 12:57:46 +0000105 ///
106 unsigned Form;
107
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108public:
109 DIEAbbrevData(unsigned A, unsigned F)
110 : Attribute(A)
111 , Form(F)
112 {}
aslc200b112008-08-16 12:57:46 +0000113
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 // Accessors.
115 unsigned getAttribute() const { return Attribute; }
116 unsigned getForm() const { return Form; }
117
118 /// Profile - Used to gather unique data for the abbreviation folding set.
119 ///
120 void Profile(FoldingSetNodeID &ID)const {
121 ID.AddInteger(Attribute);
122 ID.AddInteger(Form);
123 }
124};
125
126//===----------------------------------------------------------------------===//
127/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
128/// information object.
129class DIEAbbrev : public FoldingSetNode {
130private:
131 /// Tag - Dwarf tag code.
132 ///
133 unsigned Tag;
aslc200b112008-08-16 12:57:46 +0000134
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 /// Unique number for node.
136 ///
137 unsigned Number;
138
139 /// ChildrenFlag - Dwarf children flag.
140 ///
141 unsigned ChildrenFlag;
142
143 /// Data - Raw data bytes for abbreviation.
144 ///
Owen Anderson88dd6232008-06-24 21:44:59 +0000145 SmallVector<DIEAbbrevData, 8> Data;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146
147public:
148
149 DIEAbbrev(unsigned T, unsigned C)
150 : Tag(T)
151 , ChildrenFlag(C)
152 , Data()
153 {}
154 ~DIEAbbrev() {}
aslc200b112008-08-16 12:57:46 +0000155
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 // Accessors.
157 unsigned getTag() const { return Tag; }
158 unsigned getNumber() const { return Number; }
159 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson88dd6232008-06-24 21:44:59 +0000160 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 void setTag(unsigned T) { Tag = T; }
162 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
163 void setNumber(unsigned N) { Number = N; }
aslc200b112008-08-16 12:57:46 +0000164
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 /// AddAttribute - Adds another set of attribute information to the
166 /// abbreviation.
167 void AddAttribute(unsigned Attribute, unsigned Form) {
168 Data.push_back(DIEAbbrevData(Attribute, Form));
169 }
aslc200b112008-08-16 12:57:46 +0000170
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 /// AddFirstAttribute - Adds a set of attribute information to the front
172 /// of the abbreviation.
173 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
174 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
175 }
aslc200b112008-08-16 12:57:46 +0000176
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 /// Profile - Used to gather unique data for the abbreviation folding set.
178 ///
179 void Profile(FoldingSetNodeID &ID) {
180 ID.AddInteger(Tag);
181 ID.AddInteger(ChildrenFlag);
aslc200b112008-08-16 12:57:46 +0000182
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 // For each attribute description.
184 for (unsigned i = 0, N = Data.size(); i < N; ++i)
185 Data[i].Profile(ID);
186 }
aslc200b112008-08-16 12:57:46 +0000187
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 /// Emit - Print the abbreviation using the specified Dwarf writer.
189 ///
aslc200b112008-08-16 12:57:46 +0000190 void Emit(const DwarfDebug &DD) const;
191
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192#ifndef NDEBUG
193 void print(std::ostream *O) {
194 if (O) print(*O);
195 }
196 void print(std::ostream &O);
197 void dump();
198#endif
199};
200
201//===----------------------------------------------------------------------===//
202/// DIE - A structured debug information entry. Has an abbreviation which
203/// describes it's organization.
204class DIE : public FoldingSetNode {
205protected:
206 /// Abbrev - Buffer for constructing abbreviation.
207 ///
208 DIEAbbrev Abbrev;
aslc200b112008-08-16 12:57:46 +0000209
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210 /// Offset - Offset in debug info section.
211 ///
212 unsigned Offset;
aslc200b112008-08-16 12:57:46 +0000213
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 /// Size - Size of instance + children.
215 ///
216 unsigned Size;
aslc200b112008-08-16 12:57:46 +0000217
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 /// Children DIEs.
219 ///
220 std::vector<DIE *> Children;
aslc200b112008-08-16 12:57:46 +0000221
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 /// Attributes values.
223 ///
Owen Anderson88dd6232008-06-24 21:44:59 +0000224 SmallVector<DIEValue*, 32> Values;
aslc200b112008-08-16 12:57:46 +0000225
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000227 explicit DIE(unsigned Tag)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 : Abbrev(Tag, DW_CHILDREN_no)
229 , Offset(0)
230 , Size(0)
231 , Children()
232 , Values()
233 {}
234 virtual ~DIE();
aslc200b112008-08-16 12:57:46 +0000235
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 // Accessors.
237 DIEAbbrev &getAbbrev() { return Abbrev; }
238 unsigned getAbbrevNumber() const {
239 return Abbrev.getNumber();
240 }
241 unsigned getTag() const { return Abbrev.getTag(); }
242 unsigned getOffset() const { return Offset; }
243 unsigned getSize() const { return Size; }
244 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson88dd6232008-06-24 21:44:59 +0000245 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
247 void setOffset(unsigned O) { Offset = O; }
248 void setSize(unsigned S) { Size = S; }
aslc200b112008-08-16 12:57:46 +0000249
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 /// AddValue - Add a value and attributes to a DIE.
251 ///
252 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
253 Abbrev.AddAttribute(Attribute, Form);
254 Values.push_back(Value);
255 }
aslc200b112008-08-16 12:57:46 +0000256
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 /// SiblingOffset - Return the offset of the debug information entry's
258 /// sibling.
259 unsigned SiblingOffset() const { return Offset + Size; }
aslc200b112008-08-16 12:57:46 +0000260
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
262 ///
263 void AddSiblingOffset();
264
265 /// AddChild - Add a child to the DIE.
266 ///
267 void AddChild(DIE *Child) {
268 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
269 Children.push_back(Child);
270 }
aslc200b112008-08-16 12:57:46 +0000271
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 /// Detach - Detaches objects connected to it after copying.
273 ///
274 void Detach() {
275 Children.clear();
276 }
aslc200b112008-08-16 12:57:46 +0000277
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278 /// Profile - Used to gather unique data for the value folding set.
279 ///
280 void Profile(FoldingSetNodeID &ID) ;
aslc200b112008-08-16 12:57:46 +0000281
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282#ifndef NDEBUG
283 void print(std::ostream *O, unsigned IncIndent = 0) {
284 if (O) print(*O, IncIndent);
285 }
286 void print(std::ostream &O, unsigned IncIndent = 0);
287 void dump();
288#endif
289};
290
291//===----------------------------------------------------------------------===//
292/// DIEValue - A debug information entry value.
293///
294class DIEValue : public FoldingSetNode {
295public:
296 enum {
297 isInteger,
298 isString,
299 isLabel,
300 isAsIsLabel,
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000301 isSectionOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 isDelta,
303 isEntry,
304 isBlock
305 };
aslc200b112008-08-16 12:57:46 +0000306
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307 /// Type - Type of data stored in the value.
308 ///
309 unsigned Type;
aslc200b112008-08-16 12:57:46 +0000310
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000311 explicit DIEValue(unsigned T)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 : Type(T)
313 {}
314 virtual ~DIEValue() {}
aslc200b112008-08-16 12:57:46 +0000315
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 // Accessors
317 unsigned getType() const { return Type; }
aslc200b112008-08-16 12:57:46 +0000318
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319 // Implement isa/cast/dyncast.
320 static bool classof(const DIEValue *) { return true; }
aslc200b112008-08-16 12:57:46 +0000321
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322 /// EmitValue - Emit value via the Dwarf writer.
323 ///
324 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
aslc200b112008-08-16 12:57:46 +0000325
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326 /// SizeOf - Return the size of a value in bytes.
327 ///
328 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
aslc200b112008-08-16 12:57:46 +0000329
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330 /// Profile - Used to gather unique data for the value folding set.
331 ///
332 virtual void Profile(FoldingSetNodeID &ID) = 0;
aslc200b112008-08-16 12:57:46 +0000333
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334#ifndef NDEBUG
335 void print(std::ostream *O) {
336 if (O) print(*O);
337 }
338 virtual void print(std::ostream &O) = 0;
339 void dump();
340#endif
341};
342
343//===----------------------------------------------------------------------===//
344/// DWInteger - An integer value DIE.
aslc200b112008-08-16 12:57:46 +0000345///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346class DIEInteger : public DIEValue {
347private:
348 uint64_t Integer;
aslc200b112008-08-16 12:57:46 +0000349
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000351 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352
353 // Implement isa/cast/dyncast.
354 static bool classof(const DIEInteger *) { return true; }
355 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
aslc200b112008-08-16 12:57:46 +0000356
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357 /// BestForm - Choose the best form for integer.
358 ///
359 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
360 if (IsSigned) {
361 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
362 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
363 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
364 } else {
365 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
366 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
367 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
368 }
369 return DW_FORM_data8;
370 }
aslc200b112008-08-16 12:57:46 +0000371
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 /// EmitValue - Emit integer of appropriate size.
373 ///
374 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000375
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 /// SizeOf - Determine size of integer value in bytes.
377 ///
378 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000379
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380 /// Profile - Used to gather unique data for the value folding set.
381 ///
382 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
383 ID.AddInteger(isInteger);
384 ID.AddInteger(Integer);
385 }
386 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
aslc200b112008-08-16 12:57:46 +0000387
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000388#ifndef NDEBUG
389 virtual void print(std::ostream &O) {
390 O << "Int: " << (int64_t)Integer
391 << " 0x" << std::hex << Integer << std::dec;
392 }
393#endif
394};
395
396//===----------------------------------------------------------------------===//
397/// DIEString - A string value DIE.
aslc200b112008-08-16 12:57:46 +0000398///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399class DIEString : public DIEValue {
400public:
401 const std::string String;
aslc200b112008-08-16 12:57:46 +0000402
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000403 explicit DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404
405 // Implement isa/cast/dyncast.
406 static bool classof(const DIEString *) { return true; }
407 static bool classof(const DIEValue *S) { return S->Type == isString; }
aslc200b112008-08-16 12:57:46 +0000408
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409 /// EmitValue - Emit string value.
410 ///
411 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000412
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413 /// SizeOf - Determine size of string value in bytes.
414 ///
415 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
416 return String.size() + sizeof(char); // sizeof('\0');
417 }
aslc200b112008-08-16 12:57:46 +0000418
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419 /// Profile - Used to gather unique data for the value folding set.
420 ///
421 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
422 ID.AddInteger(isString);
423 ID.AddString(String);
424 }
425 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
aslc200b112008-08-16 12:57:46 +0000426
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427#ifndef NDEBUG
428 virtual void print(std::ostream &O) {
429 O << "Str: \"" << String << "\"";
430 }
431#endif
432};
433
434//===----------------------------------------------------------------------===//
435/// DIEDwarfLabel - A Dwarf internal label expression DIE.
436//
437class DIEDwarfLabel : public DIEValue {
438public:
439
440 const DWLabel Label;
aslc200b112008-08-16 12:57:46 +0000441
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000442 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443
444 // Implement isa/cast/dyncast.
445 static bool classof(const DIEDwarfLabel *) { return true; }
446 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
aslc200b112008-08-16 12:57:46 +0000447
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448 /// EmitValue - Emit label value.
449 ///
450 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000451
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452 /// SizeOf - Determine size of label value in bytes.
453 ///
454 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000455
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 /// Profile - Used to gather unique data for the value folding set.
457 ///
458 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
459 ID.AddInteger(isLabel);
460 Label.Profile(ID);
461 }
462 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
aslc200b112008-08-16 12:57:46 +0000463
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464#ifndef NDEBUG
465 virtual void print(std::ostream &O) {
466 O << "Lbl: ";
467 Label.print(O);
468 }
469#endif
470};
471
472
473//===----------------------------------------------------------------------===//
474/// DIEObjectLabel - A label to an object in code or data.
475//
476class DIEObjectLabel : public DIEValue {
477public:
478 const std::string Label;
aslc200b112008-08-16 12:57:46 +0000479
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000480 explicit DIEObjectLabel(const std::string &L)
481 : DIEValue(isAsIsLabel), Label(L) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000482
483 // Implement isa/cast/dyncast.
484 static bool classof(const DIEObjectLabel *) { return true; }
485 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
aslc200b112008-08-16 12:57:46 +0000486
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000487 /// EmitValue - Emit label value.
488 ///
489 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000490
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000491 /// SizeOf - Determine size of label value in bytes.
492 ///
493 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000494
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000495 /// Profile - Used to gather unique data for the value folding set.
496 ///
497 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
498 ID.AddInteger(isAsIsLabel);
499 ID.AddString(Label);
500 }
501 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
502
503#ifndef NDEBUG
504 virtual void print(std::ostream &O) {
505 O << "Obj: " << Label;
506 }
507#endif
508};
509
510//===----------------------------------------------------------------------===//
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000511/// DIESectionOffset - A section offset DIE.
512//
513class DIESectionOffset : public DIEValue {
514public:
515 const DWLabel Label;
516 const DWLabel Section;
517 bool IsEH : 1;
518 bool UseSet : 1;
aslc200b112008-08-16 12:57:46 +0000519
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000520 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
521 bool isEH = false, bool useSet = true)
522 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
523 IsEH(isEH), UseSet(useSet) {}
524
525 // Implement isa/cast/dyncast.
526 static bool classof(const DIESectionOffset *) { return true; }
527 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
aslc200b112008-08-16 12:57:46 +0000528
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000529 /// EmitValue - Emit section offset.
530 ///
531 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000532
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000533 /// SizeOf - Determine size of section offset value in bytes.
534 ///
535 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000536
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000537 /// Profile - Used to gather unique data for the value folding set.
538 ///
539 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
540 const DWLabel &Section) {
541 ID.AddInteger(isSectionOffset);
542 Label.Profile(ID);
543 Section.Profile(ID);
544 // IsEH and UseSet are specific to the Label/Section that we will emit
545 // the offset for; so Label/Section are enough for uniqueness.
546 }
547 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
548
549#ifndef NDEBUG
550 virtual void print(std::ostream &O) {
551 O << "Off: ";
552 Label.print(O);
553 O << "-";
554 Section.print(O);
555 O << "-" << IsEH << "-" << UseSet;
556 }
557#endif
558};
559
560//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561/// DIEDelta - A simple label difference DIE.
aslc200b112008-08-16 12:57:46 +0000562///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563class DIEDelta : public DIEValue {
564public:
565 const DWLabel LabelHi;
566 const DWLabel LabelLo;
aslc200b112008-08-16 12:57:46 +0000567
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
569 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
570
571 // Implement isa/cast/dyncast.
572 static bool classof(const DIEDelta *) { return true; }
573 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
aslc200b112008-08-16 12:57:46 +0000574
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000575 /// EmitValue - Emit delta value.
576 ///
577 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000578
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000579 /// SizeOf - Determine size of delta value in bytes.
580 ///
581 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000582
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583 /// Profile - Used to gather unique data for the value folding set.
584 ///
585 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
586 const DWLabel &LabelLo) {
587 ID.AddInteger(isDelta);
588 LabelHi.Profile(ID);
589 LabelLo.Profile(ID);
590 }
591 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
592
593#ifndef NDEBUG
594 virtual void print(std::ostream &O) {
595 O << "Del: ";
596 LabelHi.print(O);
597 O << "-";
598 LabelLo.print(O);
599 }
600#endif
601};
602
603//===----------------------------------------------------------------------===//
604/// DIEntry - A pointer to another debug information entry. An instance of this
605/// class can also be used as a proxy for a debug information entry not yet
606/// defined (ie. types.)
607class DIEntry : public DIEValue {
608public:
609 DIE *Entry;
aslc200b112008-08-16 12:57:46 +0000610
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000611 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
aslc200b112008-08-16 12:57:46 +0000612
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000613 // Implement isa/cast/dyncast.
614 static bool classof(const DIEntry *) { return true; }
615 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
aslc200b112008-08-16 12:57:46 +0000616
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617 /// EmitValue - Emit debug information entry offset.
618 ///
619 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000620
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000621 /// SizeOf - Determine size of debug information entry in bytes.
622 ///
623 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
624 return sizeof(int32_t);
625 }
aslc200b112008-08-16 12:57:46 +0000626
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627 /// Profile - Used to gather unique data for the value folding set.
628 ///
629 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
630 ID.AddInteger(isEntry);
631 ID.AddPointer(Entry);
632 }
633 virtual void Profile(FoldingSetNodeID &ID) {
634 ID.AddInteger(isEntry);
aslc200b112008-08-16 12:57:46 +0000635
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000636 if (Entry) {
637 ID.AddPointer(Entry);
638 } else {
639 ID.AddPointer(this);
640 }
641 }
aslc200b112008-08-16 12:57:46 +0000642
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000643#ifndef NDEBUG
644 virtual void print(std::ostream &O) {
645 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
646 }
647#endif
648};
649
650//===----------------------------------------------------------------------===//
651/// DIEBlock - A block of values. Primarily used for location expressions.
652//
653class DIEBlock : public DIEValue, public DIE {
654public:
655 unsigned Size; // Size in bytes excluding size header.
aslc200b112008-08-16 12:57:46 +0000656
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657 DIEBlock()
658 : DIEValue(isBlock)
659 , DIE(0)
660 , Size(0)
661 {}
662 ~DIEBlock() {
663 }
aslc200b112008-08-16 12:57:46 +0000664
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000665 // Implement isa/cast/dyncast.
666 static bool classof(const DIEBlock *) { return true; }
667 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
aslc200b112008-08-16 12:57:46 +0000668
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000669 /// ComputeSize - calculate the size of the block.
670 ///
671 unsigned ComputeSize(DwarfDebug &DD);
aslc200b112008-08-16 12:57:46 +0000672
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000673 /// BestForm - Choose the best form for data.
674 ///
675 unsigned BestForm() const {
676 if ((unsigned char)Size == Size) return DW_FORM_block1;
677 if ((unsigned short)Size == Size) return DW_FORM_block2;
678 if ((unsigned int)Size == Size) return DW_FORM_block4;
679 return DW_FORM_block;
680 }
681
682 /// EmitValue - Emit block data.
683 ///
684 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000685
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686 /// SizeOf - Determine size of block data in bytes.
687 ///
688 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000689
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000690
691 /// Profile - Used to gather unique data for the value folding set.
692 ///
693 virtual void Profile(FoldingSetNodeID &ID) {
694 ID.AddInteger(isBlock);
695 DIE::Profile(ID);
696 }
aslc200b112008-08-16 12:57:46 +0000697
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698#ifndef NDEBUG
699 virtual void print(std::ostream &O) {
700 O << "Blk: ";
701 DIE::print(O, 5);
702 }
703#endif
704};
705
706//===----------------------------------------------------------------------===//
707/// CompileUnit - This dwarf writer support class manages information associate
708/// with a source file.
709class CompileUnit {
710private:
711 /// Desc - Compile unit debug descriptor.
712 ///
713 CompileUnitDesc *Desc;
aslc200b112008-08-16 12:57:46 +0000714
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000715 /// ID - File identifier for source.
716 ///
717 unsigned ID;
aslc200b112008-08-16 12:57:46 +0000718
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000719 /// Die - Compile unit debug information entry.
720 ///
721 DIE *Die;
aslc200b112008-08-16 12:57:46 +0000722
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723 /// DescToDieMap - Tracks the mapping of unit level debug informaton
724 /// descriptors to debug information entries.
725 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
726
727 /// DescToDIEntryMap - Tracks the mapping of unit level debug informaton
728 /// descriptors to debug information entries using a DIEntry proxy.
729 std::map<DebugInfoDesc *, DIEntry *> DescToDIEntryMap;
730
731 /// Globals - A map of globally visible named entities for this unit.
732 ///
733 std::map<std::string, DIE *> Globals;
734
735 /// DiesSet - Used to uniquely define dies within the compile unit.
736 ///
737 FoldingSet<DIE> DiesSet;
aslc200b112008-08-16 12:57:46 +0000738
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000739 /// Dies - List of all dies in the compile unit.
740 ///
741 std::vector<DIE *> Dies;
aslc200b112008-08-16 12:57:46 +0000742
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000743public:
744 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
745 : Desc(CUD)
746 , ID(I)
747 , Die(D)
748 , DescToDieMap()
749 , DescToDIEntryMap()
750 , Globals()
751 , DiesSet(InitDiesSetSize)
752 , Dies()
753 {}
aslc200b112008-08-16 12:57:46 +0000754
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000755 ~CompileUnit() {
756 delete Die;
aslc200b112008-08-16 12:57:46 +0000757
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000758 for (unsigned i = 0, N = Dies.size(); i < N; ++i)
759 delete Dies[i];
760 }
aslc200b112008-08-16 12:57:46 +0000761
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000762 // Accessors.
763 CompileUnitDesc *getDesc() const { return Desc; }
764 unsigned getID() const { return ID; }
765 DIE* getDie() const { return Die; }
766 std::map<std::string, DIE *> &getGlobals() { return Globals; }
767
768 /// hasContent - Return true if this compile unit has something to write out.
769 ///
770 bool hasContent() const {
771 return !Die->getChildren().empty();
772 }
773
774 /// AddGlobal - Add a new global entity to the compile unit.
775 ///
776 void AddGlobal(const std::string &Name, DIE *Die) {
777 Globals[Name] = Die;
778 }
aslc200b112008-08-16 12:57:46 +0000779
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000780 /// getDieMapSlotFor - Returns the debug information entry map slot for the
781 /// specified debug descriptor.
782 DIE *&getDieMapSlotFor(DebugInfoDesc *DID) {
783 return DescToDieMap[DID];
784 }
aslc200b112008-08-16 12:57:46 +0000785
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000786 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
787 /// specified debug descriptor.
788 DIEntry *&getDIEntrySlotFor(DebugInfoDesc *DID) {
789 return DescToDIEntryMap[DID];
790 }
aslc200b112008-08-16 12:57:46 +0000791
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000792 /// AddDie - Adds or interns the DIE to the compile unit.
793 ///
794 DIE *AddDie(DIE &Buffer) {
795 FoldingSetNodeID ID;
796 Buffer.Profile(ID);
797 void *Where;
798 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
aslc200b112008-08-16 12:57:46 +0000799
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000800 if (!Die) {
801 Die = new DIE(Buffer);
802 DiesSet.InsertNode(Die, Where);
803 this->Die->AddChild(Die);
804 Buffer.Detach();
805 }
aslc200b112008-08-16 12:57:46 +0000806
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000807 return Die;
808 }
809};
810
811//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +0000812/// Dwarf - Emits general Dwarf directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000813///
814class Dwarf {
815
816protected:
817
818 //===--------------------------------------------------------------------===//
819 // Core attributes used by the Dwarf writer.
820 //
aslc200b112008-08-16 12:57:46 +0000821
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000822 //
823 /// O - Stream to .s file.
824 ///
Owen Anderson847b99b2008-08-21 00:14:44 +0000825 raw_ostream &O;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000826
827 /// Asm - Target of Dwarf emission.
828 ///
829 AsmPrinter *Asm;
aslc200b112008-08-16 12:57:46 +0000830
Bill Wendlingac9639d2008-07-01 23:34:48 +0000831 /// TAI - Target asm information.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000832 const TargetAsmInfo *TAI;
aslc200b112008-08-16 12:57:46 +0000833
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000834 /// TD - Target data.
835 const TargetData *TD;
aslc200b112008-08-16 12:57:46 +0000836
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000837 /// RI - Register Information.
Dan Gohman1e57df32008-02-10 18:45:23 +0000838 const TargetRegisterInfo *RI;
aslc200b112008-08-16 12:57:46 +0000839
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000840 /// M - Current module.
841 ///
842 Module *M;
aslc200b112008-08-16 12:57:46 +0000843
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000844 /// MF - Current machine function.
845 ///
846 MachineFunction *MF;
aslc200b112008-08-16 12:57:46 +0000847
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000848 /// MMI - Collected machine module information.
849 ///
850 MachineModuleInfo *MMI;
aslc200b112008-08-16 12:57:46 +0000851
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000852 /// SubprogramCount - The running count of functions being compiled.
853 ///
854 unsigned SubprogramCount;
aslc200b112008-08-16 12:57:46 +0000855
Chris Lattnerb3876c72007-09-24 03:35:37 +0000856 /// Flavor - A unique string indicating what dwarf producer this is, used to
857 /// unique labels.
858 const char * const Flavor;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859
860 unsigned SetCounter;
Owen Anderson847b99b2008-08-21 00:14:44 +0000861 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattnerb3876c72007-09-24 03:35:37 +0000862 const char *flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000863 : O(OS)
864 , Asm(A)
865 , TAI(T)
866 , TD(Asm->TM.getTargetData())
867 , RI(Asm->TM.getRegisterInfo())
868 , M(NULL)
869 , MF(NULL)
870 , MMI(NULL)
871 , SubprogramCount(0)
Chris Lattnerb3876c72007-09-24 03:35:37 +0000872 , Flavor(flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000873 , SetCounter(1)
874 {
875 }
876
877public:
878
879 //===--------------------------------------------------------------------===//
880 // Accessors.
881 //
882 AsmPrinter *getAsm() const { return Asm; }
883 MachineModuleInfo *getMMI() const { return MMI; }
884 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohmancfb72b22007-09-27 23:12:31 +0000885 const TargetData *getTargetData() const { return TD; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000886
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000887 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
888 const {
889 if (isInSection && TAI->getDwarfSectionOffsetDirective())
890 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohmancfb72b22007-09-27 23:12:31 +0000891 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000892 O << TAI->getData32bitsDirective();
893 else
894 O << TAI->getData64bitsDirective();
895 }
aslc200b112008-08-16 12:57:46 +0000896
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000897 /// PrintLabelName - Print label name in form used by Dwarf writer.
898 ///
899 void PrintLabelName(DWLabel Label) const {
900 PrintLabelName(Label.Tag, Label.Number);
901 }
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000902 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000903 O << TAI->getPrivateGlobalPrefix() << Tag;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000904 if (Number) O << Number;
905 }
aslc200b112008-08-16 12:57:46 +0000906
Chris Lattnerb3876c72007-09-24 03:35:37 +0000907 void PrintLabelName(const char *Tag, unsigned Number,
908 const char *Suffix) const {
909 O << TAI->getPrivateGlobalPrefix() << Tag;
910 if (Number) O << Number;
911 O << Suffix;
912 }
aslc200b112008-08-16 12:57:46 +0000913
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000914 /// EmitLabel - Emit location label for internal use by Dwarf.
915 ///
916 void EmitLabel(DWLabel Label) const {
917 EmitLabel(Label.Tag, Label.Number);
918 }
919 void EmitLabel(const char *Tag, unsigned Number) const {
920 PrintLabelName(Tag, Number);
921 O << ":\n";
922 }
aslc200b112008-08-16 12:57:46 +0000923
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000924 /// EmitReference - Emit a reference to a label.
925 ///
Dan Gohman4fd77742007-09-28 15:43:33 +0000926 void EmitReference(DWLabel Label, bool IsPCRelative = false,
927 bool Force32Bit = false) const {
928 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929 }
930 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman4fd77742007-09-28 15:43:33 +0000931 bool IsPCRelative = false, bool Force32Bit = false) const {
932 PrintRelDirective(Force32Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000933 PrintLabelName(Tag, Number);
aslc200b112008-08-16 12:57:46 +0000934
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000935 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
936 }
Dan Gohman4fd77742007-09-28 15:43:33 +0000937 void EmitReference(const std::string &Name, bool IsPCRelative = false,
938 bool Force32Bit = false) const {
939 PrintRelDirective(Force32Bit);
aslc200b112008-08-16 12:57:46 +0000940
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000941 O << Name;
aslc200b112008-08-16 12:57:46 +0000942
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000943 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
944 }
945
946 /// EmitDifference - Emit the difference between two labels. Some
947 /// assemblers do not behave with absolute expressions with data directives,
948 /// so there is an option (needsSet) to use an intermediary set expression.
949 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
950 bool IsSmall = false) {
951 EmitDifference(LabelHi.Tag, LabelHi.Number,
952 LabelLo.Tag, LabelLo.Number,
953 IsSmall);
954 }
955 void EmitDifference(const char *TagHi, unsigned NumberHi,
956 const char *TagLo, unsigned NumberLo,
957 bool IsSmall = false) {
958 if (TAI->needsSet()) {
959 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +0000960 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000961 O << ",";
962 PrintLabelName(TagHi, NumberHi);
963 O << "-";
964 PrintLabelName(TagLo, NumberLo);
965 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000966
967 PrintRelDirective(IsSmall);
Chris Lattnerb3876c72007-09-24 03:35:37 +0000968 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000969 ++SetCounter;
970 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000971 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +0000972
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000973 PrintLabelName(TagHi, NumberHi);
974 O << "-";
975 PrintLabelName(TagLo, NumberLo);
976 }
977 }
978
979 void EmitSectionOffset(const char* Label, const char* Section,
980 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesen0ebb2432008-03-26 23:31:39 +0000981 bool IsSmall = false, bool isEH = false,
982 bool useSet = true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000983 bool printAbsolute = false;
Dale Johannesen0ebb2432008-03-26 23:31:39 +0000984 if (isEH)
985 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
986 else
987 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
988
989 if (TAI->needsSet() && useSet) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000990 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +0000991 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000992 O << ",";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000993 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000994
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000995 if (!printAbsolute) {
996 O << "-";
997 PrintLabelName(Section, SectionNumber);
aslc200b112008-08-16 12:57:46 +0000998 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000999 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001000
1001 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +00001002
Chris Lattnerb3876c72007-09-24 03:35:37 +00001003 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001004 ++SetCounter;
1005 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001006 PrintRelDirective(IsSmall, true);
aslc200b112008-08-16 12:57:46 +00001007
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001008 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001009
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001010 if (!printAbsolute) {
1011 O << "-";
1012 PrintLabelName(Section, SectionNumber);
1013 }
aslc200b112008-08-16 12:57:46 +00001014 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001015 }
aslc200b112008-08-16 12:57:46 +00001016
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001017 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1018 /// frame.
1019 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenf5a11532007-11-13 19:13:01 +00001020 const std::vector<MachineMove> &Moves, bool isEH) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001021 int stackGrowth =
1022 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1023 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00001024 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001025 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
1026
1027 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
1028 const MachineMove &Move = Moves[i];
1029 unsigned LabelID = Move.getLabelID();
aslc200b112008-08-16 12:57:46 +00001030
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001031 if (LabelID) {
1032 LabelID = MMI->MappedLabel(LabelID);
aslc200b112008-08-16 12:57:46 +00001033
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001034 // Throw out move if the label is invalid.
1035 if (!LabelID) continue;
1036 }
aslc200b112008-08-16 12:57:46 +00001037
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001038 const MachineLocation &Dst = Move.getDestination();
1039 const MachineLocation &Src = Move.getSource();
aslc200b112008-08-16 12:57:46 +00001040
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001041 // Advance row if new location.
1042 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1043 Asm->EmitInt8(DW_CFA_advance_loc4);
1044 Asm->EOL("DW_CFA_advance_loc4");
1045 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1046 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00001047
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001048 BaseLabelID = LabelID;
1049 BaseLabel = "label";
1050 IsLocal = true;
1051 }
aslc200b112008-08-16 12:57:46 +00001052
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001053 // If advancing cfa.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001054 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1055 if (!Src.isReg()) {
1056 if (Src.getReg() == MachineLocation::VirtualFP) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001057 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1058 Asm->EOL("DW_CFA_def_cfa_offset");
1059 } else {
1060 Asm->EmitInt8(DW_CFA_def_cfa);
1061 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001062 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001063 Asm->EOL("Register");
1064 }
aslc200b112008-08-16 12:57:46 +00001065
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001066 int Offset = -Src.getOffset();
aslc200b112008-08-16 12:57:46 +00001067
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001068 Asm->EmitULEB128Bytes(Offset);
1069 Asm->EOL("Offset");
1070 } else {
1071 assert(0 && "Machine move no supported yet.");
1072 }
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001073 } else if (Src.isReg() &&
1074 Src.getReg() == MachineLocation::VirtualFP) {
1075 if (Dst.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001076 Asm->EmitInt8(DW_CFA_def_cfa_register);
1077 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001078 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001079 Asm->EOL("Register");
1080 } else {
1081 assert(0 && "Machine move no supported yet.");
1082 }
1083 } else {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001084 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001085 int Offset = Dst.getOffset() / stackGrowth;
aslc200b112008-08-16 12:57:46 +00001086
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001087 if (Offset < 0) {
1088 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1089 Asm->EOL("DW_CFA_offset_extended_sf");
1090 Asm->EmitULEB128Bytes(Reg);
1091 Asm->EOL("Reg");
1092 Asm->EmitSLEB128Bytes(Offset);
1093 Asm->EOL("Offset");
1094 } else if (Reg < 64) {
1095 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Cheng6181e062008-07-09 21:53:02 +00001096 if (VerboseAsm)
1097 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1098 else
1099 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001100 Asm->EmitULEB128Bytes(Offset);
1101 Asm->EOL("Offset");
1102 } else {
1103 Asm->EmitInt8(DW_CFA_offset_extended);
1104 Asm->EOL("DW_CFA_offset_extended");
1105 Asm->EmitULEB128Bytes(Reg);
1106 Asm->EOL("Reg");
1107 Asm->EmitULEB128Bytes(Offset);
1108 Asm->EOL("Offset");
1109 }
1110 }
1111 }
1112 }
1113
1114};
1115
1116//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00001117/// DwarfDebug - Emits Dwarf debug directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001118///
1119class DwarfDebug : public Dwarf {
1120
1121private:
1122 //===--------------------------------------------------------------------===//
1123 // Attributes used to construct specific Dwarf sections.
1124 //
aslc200b112008-08-16 12:57:46 +00001125
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001126 /// CompileUnits - All the compile units involved in this build. The index
1127 /// of each entry in this vector corresponds to the sources in MMI.
1128 std::vector<CompileUnit *> CompileUnits;
aslc200b112008-08-16 12:57:46 +00001129
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001130 /// AbbreviationsSet - Used to uniquely define abbreviations.
1131 ///
1132 FoldingSet<DIEAbbrev> AbbreviationsSet;
1133
1134 /// Abbreviations - A list of all the unique abbreviations in use.
1135 ///
1136 std::vector<DIEAbbrev *> Abbreviations;
aslc200b112008-08-16 12:57:46 +00001137
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001138 /// ValuesSet - Used to uniquely define values.
1139 ///
1140 FoldingSet<DIEValue> ValuesSet;
aslc200b112008-08-16 12:57:46 +00001141
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001142 /// Values - A list of all the unique values in use.
1143 ///
1144 std::vector<DIEValue *> Values;
aslc200b112008-08-16 12:57:46 +00001145
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001146 /// StringPool - A UniqueVector of strings used by indirect references.
1147 ///
1148 UniqueVector<std::string> StringPool;
1149
1150 /// UnitMap - Map debug information descriptor to compile unit.
1151 ///
1152 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
aslc200b112008-08-16 12:57:46 +00001153
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001154 /// SectionMap - Provides a unique id per text section.
1155 ///
Anton Korobeynikov55b94962008-09-24 22:15:21 +00001156 UniqueVector<const Section*> SectionMap;
aslc200b112008-08-16 12:57:46 +00001157
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001158 /// SectionSourceLines - Tracks line numbers per text section.
1159 ///
1160 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
1161
1162 /// didInitial - Flag to indicate if initial emission has been done.
1163 ///
1164 bool didInitial;
aslc200b112008-08-16 12:57:46 +00001165
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001166 /// shouldEmit - Flag to indicate if debug information should be emitted.
1167 ///
1168 bool shouldEmit;
1169
1170 struct FunctionDebugFrameInfo {
1171 unsigned Number;
1172 std::vector<MachineMove> Moves;
1173
1174 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman9ba5d4d2007-08-27 14:50:10 +00001175 Number(Num), Moves(M) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001176 };
1177
1178 std::vector<FunctionDebugFrameInfo> DebugFrames;
aslc200b112008-08-16 12:57:46 +00001179
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001180public:
aslc200b112008-08-16 12:57:46 +00001181
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001182 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1183 ///
1184 bool ShouldEmitDwarf() const { return shouldEmit; }
1185
1186 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
aslc200b112008-08-16 12:57:46 +00001187 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001188 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1189 // Profile the node so that we can make it unique.
1190 FoldingSetNodeID ID;
1191 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00001192
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001193 // Check the set for priors.
1194 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
aslc200b112008-08-16 12:57:46 +00001195
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001196 // If it's newly added.
1197 if (InSet == &Abbrev) {
aslc200b112008-08-16 12:57:46 +00001198 // Add to abbreviation list.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001199 Abbreviations.push_back(&Abbrev);
1200 // Assign the vector position + 1 as its number.
1201 Abbrev.setNumber(Abbreviations.size());
1202 } else {
1203 // Assign existing abbreviation number.
1204 Abbrev.setNumber(InSet->getNumber());
1205 }
1206 }
1207
1208 /// NewString - Add a string to the constant pool and returns a label.
1209 ///
1210 DWLabel NewString(const std::string &String) {
1211 unsigned StringID = StringPool.insert(String);
1212 return DWLabel("string", StringID);
1213 }
aslc200b112008-08-16 12:57:46 +00001214
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001215 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1216 /// entry.
1217 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1218 DIEntry *Value;
aslc200b112008-08-16 12:57:46 +00001219
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001220 if (Entry) {
1221 FoldingSetNodeID ID;
1222 DIEntry::Profile(ID, Entry);
1223 void *Where;
1224 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
aslc200b112008-08-16 12:57:46 +00001225
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001226 if (Value) return Value;
aslc200b112008-08-16 12:57:46 +00001227
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001228 Value = new DIEntry(Entry);
1229 ValuesSet.InsertNode(Value, Where);
1230 } else {
1231 Value = new DIEntry(Entry);
1232 }
aslc200b112008-08-16 12:57:46 +00001233
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001234 Values.push_back(Value);
1235 return Value;
1236 }
aslc200b112008-08-16 12:57:46 +00001237
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001238 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1239 ///
1240 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1241 Value->Entry = Entry;
1242 // Add to values set if not already there. If it is, we merely have a
1243 // duplicate in the values list (no harm.)
1244 ValuesSet.GetOrInsertNode(Value);
1245 }
1246
1247 /// AddUInt - Add an unsigned integer attribute data and value.
1248 ///
1249 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1250 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1251
1252 FoldingSetNodeID ID;
1253 DIEInteger::Profile(ID, Integer);
1254 void *Where;
1255 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1256 if (!Value) {
1257 Value = new DIEInteger(Integer);
1258 ValuesSet.InsertNode(Value, Where);
1259 Values.push_back(Value);
1260 }
aslc200b112008-08-16 12:57:46 +00001261
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001262 Die->AddValue(Attribute, Form, Value);
1263 }
aslc200b112008-08-16 12:57:46 +00001264
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001265 /// AddSInt - Add an signed integer attribute data and value.
1266 ///
1267 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1268 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1269
1270 FoldingSetNodeID ID;
1271 DIEInteger::Profile(ID, (uint64_t)Integer);
1272 void *Where;
1273 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1274 if (!Value) {
1275 Value = new DIEInteger(Integer);
1276 ValuesSet.InsertNode(Value, Where);
1277 Values.push_back(Value);
1278 }
aslc200b112008-08-16 12:57:46 +00001279
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001280 Die->AddValue(Attribute, Form, Value);
1281 }
aslc200b112008-08-16 12:57:46 +00001282
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001283 /// AddString - Add a std::string attribute data and value.
1284 ///
1285 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1286 const std::string &String) {
1287 FoldingSetNodeID ID;
1288 DIEString::Profile(ID, String);
1289 void *Where;
1290 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1291 if (!Value) {
1292 Value = new DIEString(String);
1293 ValuesSet.InsertNode(Value, Where);
1294 Values.push_back(Value);
1295 }
aslc200b112008-08-16 12:57:46 +00001296
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001297 Die->AddValue(Attribute, Form, Value);
1298 }
aslc200b112008-08-16 12:57:46 +00001299
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001300 /// AddLabel - Add a Dwarf label attribute data and value.
1301 ///
1302 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1303 const DWLabel &Label) {
1304 FoldingSetNodeID ID;
1305 DIEDwarfLabel::Profile(ID, Label);
1306 void *Where;
1307 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1308 if (!Value) {
1309 Value = new DIEDwarfLabel(Label);
1310 ValuesSet.InsertNode(Value, Where);
1311 Values.push_back(Value);
1312 }
aslc200b112008-08-16 12:57:46 +00001313
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001314 Die->AddValue(Attribute, Form, Value);
1315 }
aslc200b112008-08-16 12:57:46 +00001316
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001317 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1318 ///
1319 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1320 const std::string &Label) {
1321 FoldingSetNodeID ID;
1322 DIEObjectLabel::Profile(ID, Label);
1323 void *Where;
1324 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1325 if (!Value) {
1326 Value = new DIEObjectLabel(Label);
1327 ValuesSet.InsertNode(Value, Where);
1328 Values.push_back(Value);
1329 }
aslc200b112008-08-16 12:57:46 +00001330
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001331 Die->AddValue(Attribute, Form, Value);
1332 }
aslc200b112008-08-16 12:57:46 +00001333
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001334 /// AddSectionOffset - Add a section offset label attribute data and value.
1335 ///
1336 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1337 const DWLabel &Label, const DWLabel &Section,
1338 bool isEH = false, bool useSet = true) {
1339 FoldingSetNodeID ID;
1340 DIESectionOffset::Profile(ID, Label, Section);
1341 void *Where;
1342 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1343 if (!Value) {
1344 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1345 ValuesSet.InsertNode(Value, Where);
1346 Values.push_back(Value);
1347 }
aslc200b112008-08-16 12:57:46 +00001348
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001349 Die->AddValue(Attribute, Form, Value);
1350 }
aslc200b112008-08-16 12:57:46 +00001351
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001352 /// AddDelta - Add a label delta attribute data and value.
1353 ///
1354 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1355 const DWLabel &Hi, const DWLabel &Lo) {
1356 FoldingSetNodeID ID;
1357 DIEDelta::Profile(ID, Hi, Lo);
1358 void *Where;
1359 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1360 if (!Value) {
1361 Value = new DIEDelta(Hi, Lo);
1362 ValuesSet.InsertNode(Value, Where);
1363 Values.push_back(Value);
1364 }
aslc200b112008-08-16 12:57:46 +00001365
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001366 Die->AddValue(Attribute, Form, Value);
1367 }
aslc200b112008-08-16 12:57:46 +00001368
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001369 /// AddDIEntry - Add a DIE attribute data and value.
1370 ///
1371 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1372 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1373 }
1374
1375 /// AddBlock - Add block data.
1376 ///
1377 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1378 Block->ComputeSize(*this);
1379 FoldingSetNodeID ID;
1380 Block->Profile(ID);
1381 void *Where;
1382 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1383 if (!Value) {
1384 Value = Block;
1385 ValuesSet.InsertNode(Value, Where);
1386 Values.push_back(Value);
1387 } else {
Chris Lattner3de66892007-09-21 18:25:53 +00001388 // Already exists, reuse the previous one.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001389 delete Block;
Chris Lattner3de66892007-09-21 18:25:53 +00001390 Block = cast<DIEBlock>(Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001391 }
aslc200b112008-08-16 12:57:46 +00001392
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001393 Die->AddValue(Attribute, Block->BestForm(), Value);
1394 }
1395
1396private:
1397
1398 /// AddSourceLine - Add location information to specified debug information
1399 /// entry.
1400 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1401 if (File && Line) {
1402 CompileUnit *FileUnit = FindCompileUnit(File);
1403 unsigned FileID = FileUnit->getID();
1404 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1405 AddUInt(Die, DW_AT_decl_line, 0, Line);
1406 }
1407 }
1408
1409 /// AddAddress - Add an address attribute to a die based on the location
1410 /// provided.
1411 void AddAddress(DIE *Die, unsigned Attribute,
1412 const MachineLocation &Location) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001413 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001414 DIEBlock *Block = new DIEBlock();
aslc200b112008-08-16 12:57:46 +00001415
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001416 if (Location.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001417 if (Reg < 32) {
1418 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1419 } else {
1420 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1421 AddUInt(Block, 0, DW_FORM_udata, Reg);
1422 }
1423 } else {
1424 if (Reg < 32) {
1425 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1426 } else {
1427 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1428 AddUInt(Block, 0, DW_FORM_udata, Reg);
1429 }
1430 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1431 }
aslc200b112008-08-16 12:57:46 +00001432
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001433 AddBlock(Die, Attribute, 0, Block);
1434 }
aslc200b112008-08-16 12:57:46 +00001435
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001436 /// AddBasicType - Add a new basic type attribute to the specified entity.
1437 ///
1438 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1439 const std::string &Name,
1440 unsigned Encoding, unsigned Size) {
aslc200b112008-08-16 12:57:46 +00001441
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001442 DIE Buffer(DW_TAG_base_type);
1443 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1444 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1445 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf49e13d2009-01-05 17:44:11 +00001446 DIE *BasicTypeDie = Unit->AddDie(Buffer);
1447 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, BasicTypeDie);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001448 }
aslc200b112008-08-16 12:57:46 +00001449
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001450 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1451 ///
1452 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001453 DIE Buffer(DW_TAG_pointer_type);
Dan Gohmancfb72b22007-09-27 23:12:31 +00001454 AddUInt(&Buffer, DW_AT_byte_size, 0, TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001455 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelbbca50b2009-01-05 17:45:59 +00001456 DIE *PointerTypeDie = Unit->AddDie(Buffer);
1457 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, PointerTypeDie);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001458 }
aslc200b112008-08-16 12:57:46 +00001459
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001460 /// AddType - Add a new type attribute to the specified entity.
1461 ///
1462 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1463 if (!TyDesc) {
1464 AddBasicType(Entity, Unit, "", DW_ATE_signed, sizeof(int32_t));
1465 } else {
1466 // Check for pre-existence.
1467 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
aslc200b112008-08-16 12:57:46 +00001468
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001469 // If it exists then use the existing value.
1470 if (Slot) {
1471 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1472 return;
1473 }
aslc200b112008-08-16 12:57:46 +00001474
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001475 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1476 // FIXME - Not sure why programs and variables are coming through here.
1477 // Short cut for handling subprogram types (not really a TyDesc.)
1478 AddPointerType(Entity, Unit, SubprogramTy->getName());
1479 } else if (GlobalVariableDesc *GlobalTy =
1480 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1481 // FIXME - Not sure why programs and variables are coming through here.
1482 // Short cut for handling global variable types (not really a TyDesc.)
1483 AddPointerType(Entity, Unit, GlobalTy->getName());
aslc200b112008-08-16 12:57:46 +00001484 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001485 // Set up proxy.
1486 Slot = NewDIEntry();
aslc200b112008-08-16 12:57:46 +00001487
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001488 // Construct type.
1489 DIE Buffer(DW_TAG_base_type);
1490 ConstructType(Buffer, TyDesc, Unit);
aslc200b112008-08-16 12:57:46 +00001491
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001492 // Add debug information entry to entity and unit.
1493 DIE *Die = Unit->AddDie(Buffer);
1494 SetDIEntry(Slot, Die);
1495 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1496 }
1497 }
1498 }
aslc200b112008-08-16 12:57:46 +00001499
Devang Patel46d13752009-01-05 19:07:53 +00001500 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1501 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1502 DIBasicType *BTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001503
1504 // Get core information.
1505 const std::string &Name = BTy->getName();
1506 Buffer.setTag(DW_TAG_base_type);
1507 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy->getEncoding());
1508 // Add name if not anonymous or intermediate type.
1509 if (!Name.empty())
1510 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1511 uint64_t Size = BTy->getSizeInBits() >> 3;
1512 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1513 }
1514
Devang Patel46d13752009-01-05 19:07:53 +00001515 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1516 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1517 DIDerivedType *DTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001518
1519 // Get core information.
1520 const std::string &Name = DTy->getName();
1521 uint64_t Size = DTy->getSizeInBits() >> 3;
1522 unsigned Tag = DTy->getTag();
1523 // FIXME - Workaround for templates.
1524 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1525
1526 Buffer.setTag(Tag);
1527 // Map to main type, void will not have a type.
1528 DIType FromTy = DTy->getTypeDerivedFrom();
1529 // FIXME - Enable this. AddType(&Buffer, FromTy, DW_Unit);
1530
1531 // Add name if not anonymous or intermediate type.
1532 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1533
1534 // Add size if non-zero (derived types might be zero-sized.)
1535 if (Size)
1536 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1537
1538 // Add source line info if available and TyDesc is not a forward
1539 // declaration.
1540 // FIXME - Enable this. if (!DTy->isForwardDecl())
1541 // FIXME - Enable this. AddSourceLine(&Buffer, *DTy);
1542 }
1543
Devang Patel30c01372009-01-05 19:55:51 +00001544 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1545 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1546 DICompositeType *CTy) {
1547
1548 // Get core information.
1549 const std::string &Name = CTy->getName();
1550 uint64_t Size = CTy->getSizeInBits() >> 3;
1551 unsigned Tag = CTy->getTag();
1552 switch (Tag) {
1553 case DW_TAG_vector_type:
1554 case DW_TAG_array_type:
1555 ConstructArrayTypeDIE(DW_Unit, Buffer, CTy);
1556 break;
1557 //FIXME - Enable this.
1558 // case DW_TAG_enumeration_type:
1559 // DIArray Elements = CTy->getTypeArray();
1560 // // Add enumerators to enumeration type.
1561 // for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i)
1562 // ConstructEnumTypeDIE(Buffer, &Elements.getElement(i));
1563 // break;
1564 case DW_TAG_subroutine_type:
1565 {
1566 // Add prototype flag.
1567 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1568 DIArray Elements = CTy->getTypeArray();
1569 // Add return type.
1570 // FIXME - Enable this.AddType(&Buffer, Elements.getElement(0), DW_Unit);
1571 // Add arguments.
1572 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1573 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1574 // FIXME - Enable this.AddType(Arg, Elements.getElement(i), DW_Unit);
1575 Buffer.AddChild(Arg);
1576 }
1577 }
1578 break;
1579 case DW_TAG_structure_type:
1580 case DW_TAG_union_type:
1581 {
1582 // Add elements to structure type.
1583 DIArray Elements = CTy->getTypeArray();
1584 // Add elements to structure type.
1585 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1586 DIDescriptor Element = Elements.getElement(i);
1587 if (DISubprogram *SP = dyn_cast<DISubprogram>(&Element))
1588 ConstructFieldTypeDIE(DW_Unit, Buffer, SP);
1589 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Element))
1590 ConstructFieldTypeDIE(DW_Unit, Buffer, DT);
1591 else if (DIGlobalVariable *GV = dyn_cast<DIGlobalVariable>(&Element))
1592 ConstructFieldTypeDIE(DW_Unit, Buffer, GV);
1593 }
1594 }
1595 break;
1596 default:
1597 break;
1598 }
1599
1600 // Add name if not anonymous or intermediate type.
1601 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1602
1603 // Add size if non-zero (derived types might be zero-sized.)
1604 if (Size)
1605 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1606 else {
1607 // Add zero size even if it is not a forward declaration.
1608 // FIXME - Enable this.
1609 // if (!CTy->isDefinition())
1610 // AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1611 // else
1612 // AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1613 }
1614
1615 // Add source line info if available and TyDesc is not a forward
1616 // declaration.
1617 // FIXME - Enable this.
1618 // if (CTy->isForwardDecl())
1619 // AddSourceLine(&Buffer, *CTy);
1620 }
1621
Devang Patel6fb54132009-01-05 18:33:01 +00001622 // ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1623 void ConstructSubrangeDIE (DIE &Buffer, DISubrange *SR, DIE *IndexTy) {
1624 int64_t L = SR->getLo();
1625 int64_t H = SR->getHi();
1626 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1627 if (L != H) {
1628 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1629 if (L)
1630 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1631 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
1632 }
1633 Buffer.AddChild(DW_Subrange);
1634 }
1635
1636 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1637 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1638 DICompositeType *CTy) {
1639 Buffer.setTag(DW_TAG_array_type);
1640 if (CTy->getTag() == DW_TAG_vector_type)
1641 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1642
1643 DIArray Elements = CTy->getTypeArray();
1644 // FIXME - Enable this.
1645 // AddType(&Buffer, CTy->getTypeDerivedFrom(), DW_Unit);
1646
1647 // Construct an anonymous type for index type.
1648 DIE IdxBuffer(DW_TAG_base_type);
1649 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1650 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1651 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1652
1653 // Add subranges to array type.
1654 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel30c01372009-01-05 19:55:51 +00001655 DIDescriptor Element = Elements.getElement(i);
1656 if (DISubrange *SR = dyn_cast<DISubrange>(&Element))
1657 ConstructSubrangeDIE(Buffer, SR, IndexTy);
Devang Patel6fb54132009-01-05 18:33:01 +00001658 }
1659 }
1660
Devang Patela566e812009-01-05 18:38:38 +00001661 /// ConstructEnumTypeDIE - Construct enum type DIE from
1662 /// DIEnumerator.
Devang Patel30c01372009-01-05 19:55:51 +00001663 void ConstructEnumTypeDIE(CompileUnit *DW_Unit,
1664 DIE &Buffer, DIEnumerator *ETy) {
Devang Patela566e812009-01-05 18:38:38 +00001665
1666 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1667 AddString(Enumerator, DW_AT_name, DW_FORM_string, ETy->getName());
1668 int64_t Value = ETy->getEnumValue();
1669 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1670 Buffer.AddChild(Enumerator);
1671 }
Devang Patel6fb54132009-01-05 18:33:01 +00001672
Devang Patel526b01d2009-01-05 18:59:44 +00001673 /// ConstructFieldTypeDIE - Construct variable DIE for a struct field.
1674 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
1675 DIE &Buffer, DIGlobalVariable *V) {
1676
1677 DIE *VariableDie = new DIE(DW_TAG_variable);
1678 const std::string &LinkageName = V->getLinkageName();
1679 if (!LinkageName.empty())
1680 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1681 LinkageName);
1682 // FIXME - Enable this. AddSourceLine(VariableDie, V);
1683 // FIXME - Enable this. AddType(VariableDie, V->getType(), DW_Unit);
1684 if (!V->isLocalToUnit())
1685 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
1686 AddUInt(VariableDie, DW_AT_declaration, DW_FORM_flag, 1);
1687 Buffer.AddChild(VariableDie);
1688 }
1689
1690 /// ConstructFieldTypeDIE - Construct subprogram DIE for a struct field.
1691 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
1692 DIE &Buffer, DISubprogram *SP,
1693 bool IsConstructor = false) {
1694 DIE *Method = new DIE(DW_TAG_subprogram);
1695 AddString(Method, DW_AT_name, DW_FORM_string, SP->getName());
1696 const std::string &LinkageName = SP->getLinkageName();
1697 if (!LinkageName.empty())
1698 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1699 // FIXME - Enable this. AddSourceLine(Method, SP);
1700
1701 DICompositeType MTy = SP->getType();
1702 DIArray Args = MTy.getTypeArray();
1703
1704 // Add Return Type.
1705 // FIXME - Enable this. if (!IsConstructor)
1706 // Fixme - Enable this. AddType(Method, Args.getElement(0), DW_Unit);
1707
1708 // Add arguments.
1709 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1710 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1711 // FIXME - Enable this. AddType(Arg, Args.getElement(i), DW_Unit);
1712 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
1713 Method->AddChild(Arg);
1714 }
1715
1716 if (!SP->isLocalToUnit())
1717 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
1718 Buffer.AddChild(Method);
1719 }
1720
1721 /// COnstructFieldTypeDIE - Construct derived type DIE for a struct field.
1722 void ConstructFieldTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1723 DIDerivedType *DTy) {
1724 unsigned Tag = DTy->getTag();
1725 DIE *MemberDie = new DIE(Tag);
1726 if (!DTy->getName().empty())
1727 AddString(MemberDie, DW_AT_name, DW_FORM_string, DTy->getName());
1728 // FIXME - Enable this. AddSourceLine(MemberDie, DTy);
1729
1730 DIType FromTy = DTy->getTypeDerivedFrom();
1731 // FIXME - Enable this. AddType(MemberDie, FromTy, DW_Unit);
1732
1733 uint64_t Size = DTy->getSizeInBits();
1734 uint64_t Offset = DTy->getOffsetInBits();
1735
1736 // FIXME Handle bitfields
1737
1738 // Add size.
1739 AddUInt(MemberDie, DW_AT_bit_size, 0, Size);
1740 // Add computation for offset.
1741 DIEBlock *Block = new DIEBlock();
1742 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1743 AddUInt(Block, 0, DW_FORM_udata, Offset >> 3);
1744 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1745
1746 // FIXME Handle DW_AT_accessibility.
1747
1748 Buffer.AddChild(MemberDie);
1749 }
1750
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001751 /// ConstructType - Adds all the required attributes to the type.
1752 ///
1753 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
1754 // Get core information.
1755 const std::string &Name = TyDesc->getName();
1756 uint64_t Size = TyDesc->getSize() >> 3;
aslc200b112008-08-16 12:57:46 +00001757
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001758 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1759 // Fundamental types like int, float, bool
1760 Buffer.setTag(DW_TAG_base_type);
1761 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
1762 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
1763 // Fetch tag.
1764 unsigned Tag = DerivedTy->getTag();
1765 // FIXME - Workaround for templates.
1766 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
aslc200b112008-08-16 12:57:46 +00001767 // Pointers, typedefs et al.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001768 Buffer.setTag(Tag);
1769 // Map to main type, void will not have a type.
1770 if (TypeDesc *FromTy = DerivedTy->getFromType())
1771 AddType(&Buffer, FromTy, Unit);
1772 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
1773 // Fetch tag.
1774 unsigned Tag = CompTy->getTag();
aslc200b112008-08-16 12:57:46 +00001775
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001776 // Set tag accordingly.
1777 if (Tag == DW_TAG_vector_type)
1778 Buffer.setTag(DW_TAG_array_type);
aslc200b112008-08-16 12:57:46 +00001779 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001780 Buffer.setTag(Tag);
1781
1782 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
aslc200b112008-08-16 12:57:46 +00001783
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001784 switch (Tag) {
1785 case DW_TAG_vector_type:
1786 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1787 // Fall thru
1788 case DW_TAG_array_type: {
1789 // Add element type.
1790 if (TypeDesc *FromTy = CompTy->getFromType())
1791 AddType(&Buffer, FromTy, Unit);
aslc200b112008-08-16 12:57:46 +00001792
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001793 // Don't emit size attribute.
1794 Size = 0;
aslc200b112008-08-16 12:57:46 +00001795
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001796 // Construct an anonymous type for index type.
Devang Patelf49e13d2009-01-05 17:44:11 +00001797 DIE Buffer(DW_TAG_base_type);
1798 AddUInt(&Buffer, DW_AT_byte_size, 0, sizeof(int32_t));
1799 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1800 DIE *IndexTy = Unit->AddDie(Buffer);
aslc200b112008-08-16 12:57:46 +00001801
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001802 // Add subranges to array type.
Evan Chengc7efea32008-12-09 17:56:30 +00001803 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001804 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1805 int64_t Lo = SRD->getLo();
1806 int64_t Hi = SRD->getHi();
1807 DIE *Subrange = new DIE(DW_TAG_subrange_type);
aslc200b112008-08-16 12:57:46 +00001808
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001809 // If a range is available.
1810 if (Lo != Hi) {
1811 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1812 // Only add low if non-zero.
1813 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
1814 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
1815 }
aslc200b112008-08-16 12:57:46 +00001816
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001817 Buffer.AddChild(Subrange);
1818 }
1819 break;
1820 }
1821 case DW_TAG_structure_type:
1822 case DW_TAG_union_type: {
1823 // Add elements to structure type.
Evan Chengc7efea32008-12-09 17:56:30 +00001824 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001825 DebugInfoDesc *Element = Elements[i];
aslc200b112008-08-16 12:57:46 +00001826
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001827 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
1828 // Add field or base class.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001829 unsigned Tag = MemberDesc->getTag();
aslc200b112008-08-16 12:57:46 +00001830
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001831 // Extract the basic information.
1832 const std::string &Name = MemberDesc->getName();
1833 uint64_t Size = MemberDesc->getSize();
1834 uint64_t Align = MemberDesc->getAlign();
1835 uint64_t Offset = MemberDesc->getOffset();
aslc200b112008-08-16 12:57:46 +00001836
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001837 // Construct member debug information entry.
1838 DIE *Member = new DIE(Tag);
aslc200b112008-08-16 12:57:46 +00001839
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001840 // Add name if not "".
1841 if (!Name.empty())
1842 AddString(Member, DW_AT_name, DW_FORM_string, Name);
Evan Chengc7efea32008-12-09 17:56:30 +00001843
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001844 // Add location if available.
1845 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
aslc200b112008-08-16 12:57:46 +00001846
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001847 // Most of the time the field info is the same as the members.
1848 uint64_t FieldSize = Size;
1849 uint64_t FieldAlign = Align;
1850 uint64_t FieldOffset = Offset;
aslc200b112008-08-16 12:57:46 +00001851
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001852 // Set the member type.
1853 TypeDesc *FromTy = MemberDesc->getFromType();
1854 AddType(Member, FromTy, Unit);
aslc200b112008-08-16 12:57:46 +00001855
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001856 // Walk up typedefs until a real size is found.
1857 while (FromTy) {
1858 if (FromTy->getTag() != DW_TAG_typedef) {
1859 FieldSize = FromTy->getSize();
Devang Patel105a08a2008-12-23 21:55:38 +00001860 FieldAlign = FromTy->getAlign();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001861 break;
1862 }
aslc200b112008-08-16 12:57:46 +00001863
Dan Gohman53491e92007-07-23 20:24:29 +00001864 FromTy = cast<DerivedTypeDesc>(FromTy)->getFromType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001865 }
aslc200b112008-08-16 12:57:46 +00001866
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001867 // Unless we have a bit field.
1868 if (Tag == DW_TAG_member && FieldSize != Size) {
1869 // Construct the alignment mask.
1870 uint64_t AlignMask = ~(FieldAlign - 1);
1871 // Determine the high bit + 1 of the declared size.
1872 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1873 // Work backwards to determine the base offset of the field.
1874 FieldOffset = HiMark - FieldSize;
1875 // Now normalize offset to the field.
1876 Offset -= FieldOffset;
aslc200b112008-08-16 12:57:46 +00001877
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001878 // Maybe we need to work from the other end.
1879 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
aslc200b112008-08-16 12:57:46 +00001880
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001881 // Add size and offset.
1882 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
1883 AddUInt(Member, DW_AT_bit_size, 0, Size);
1884 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
1885 }
aslc200b112008-08-16 12:57:46 +00001886
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001887 // Add computation for offset.
1888 DIEBlock *Block = new DIEBlock();
1889 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1890 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
1891 AddBlock(Member, DW_AT_data_member_location, 0, Block);
1892
1893 // Add accessibility (public default unless is base class.
1894 if (MemberDesc->isProtected()) {
1895 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
1896 } else if (MemberDesc->isPrivate()) {
1897 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
1898 } else if (Tag == DW_TAG_inheritance) {
1899 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
1900 }
aslc200b112008-08-16 12:57:46 +00001901
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001902 Buffer.AddChild(Member);
1903 } else if (GlobalVariableDesc *StaticDesc =
1904 dyn_cast<GlobalVariableDesc>(Element)) {
1905 // Add static member.
aslc200b112008-08-16 12:57:46 +00001906
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001907 // Construct member debug information entry.
1908 DIE *Static = new DIE(DW_TAG_variable);
aslc200b112008-08-16 12:57:46 +00001909
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001910 // Add name and mangled name.
1911 const std::string &Name = StaticDesc->getName();
1912 const std::string &LinkageName = StaticDesc->getLinkageName();
1913 AddString(Static, DW_AT_name, DW_FORM_string, Name);
1914 if (!LinkageName.empty()) {
1915 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
1916 LinkageName);
1917 }
aslc200b112008-08-16 12:57:46 +00001918
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001919 // Add location.
1920 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
aslc200b112008-08-16 12:57:46 +00001921
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001922 // Add type.
1923 if (TypeDesc *StaticTy = StaticDesc->getType())
1924 AddType(Static, StaticTy, Unit);
aslc200b112008-08-16 12:57:46 +00001925
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001926 // Add flags.
1927 if (!StaticDesc->isStatic())
1928 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
1929 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
aslc200b112008-08-16 12:57:46 +00001930
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001931 Buffer.AddChild(Static);
1932 } else if (SubprogramDesc *MethodDesc =
1933 dyn_cast<SubprogramDesc>(Element)) {
1934 // Add member function.
aslc200b112008-08-16 12:57:46 +00001935
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001936 // Construct member debug information entry.
1937 DIE *Method = new DIE(DW_TAG_subprogram);
aslc200b112008-08-16 12:57:46 +00001938
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001939 // Add name and mangled name.
1940 const std::string &Name = MethodDesc->getName();
1941 const std::string &LinkageName = MethodDesc->getLinkageName();
aslc200b112008-08-16 12:57:46 +00001942
1943 AddString(Method, DW_AT_name, DW_FORM_string, Name);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001944 bool IsCTor = TyDesc->getName() == Name;
aslc200b112008-08-16 12:57:46 +00001945
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001946 if (!LinkageName.empty()) {
1947 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
1948 LinkageName);
1949 }
aslc200b112008-08-16 12:57:46 +00001950
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001951 // Add location.
1952 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
aslc200b112008-08-16 12:57:46 +00001953
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001954 // Add type.
1955 if (CompositeTypeDesc *MethodTy =
1956 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1957 // Get argument information.
1958 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
aslc200b112008-08-16 12:57:46 +00001959
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001960 // If not a ctor.
1961 if (!IsCTor) {
1962 // Add return type.
1963 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
1964 }
aslc200b112008-08-16 12:57:46 +00001965
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001966 // Add arguments.
Evan Chengc7efea32008-12-09 17:56:30 +00001967 for (unsigned i = 1, N = Args.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001968 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1969 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
1970 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
1971 Method->AddChild(Arg);
1972 }
1973 }
1974
1975 // Add flags.
1976 if (!MethodDesc->isStatic())
1977 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
1978 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
aslc200b112008-08-16 12:57:46 +00001979
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001980 Buffer.AddChild(Method);
1981 }
1982 }
1983 break;
1984 }
1985 case DW_TAG_enumeration_type: {
1986 // Add enumerators to enumeration type.
Evan Chengc7efea32008-12-09 17:56:30 +00001987 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001988 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1989 const std::string &Name = ED->getName();
1990 int64_t Value = ED->getValue();
1991 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1992 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
1993 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1994 Buffer.AddChild(Enumerator);
1995 }
1996
1997 break;
1998 }
1999 case DW_TAG_subroutine_type: {
2000 // Add prototype flag.
2001 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
2002 // Add return type.
2003 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
aslc200b112008-08-16 12:57:46 +00002004
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002005 // Add arguments.
Evan Chengc7efea32008-12-09 17:56:30 +00002006 for (unsigned i = 1, N = Elements.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002007 DIE *Arg = new DIE(DW_TAG_formal_parameter);
2008 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
2009 Buffer.AddChild(Arg);
2010 }
aslc200b112008-08-16 12:57:46 +00002011
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002012 break;
2013 }
2014 default: break;
2015 }
2016 }
aslc200b112008-08-16 12:57:46 +00002017
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002018 // Add name if not anonymous or intermediate type.
2019 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Evan Chengc7efea32008-12-09 17:56:30 +00002020
Evan Chengb2fc7112008-12-10 00:15:44 +00002021 // Add size if non-zero (derived types might be zero-sized.)
2022 if (Size)
2023 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
2024 else if (isa<CompositeTypeDesc>(TyDesc)) {
2025 // If TyDesc is a composite type, then add size even if it's zero unless
2026 // it's a forward declaration.
2027 if (TyDesc->isForwardDecl())
2028 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
2029 else
2030 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
2031 }
2032
2033 // Add source line info if available and TyDesc is not a forward
2034 // declaration.
2035 if (!TyDesc->isForwardDecl())
2036 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002037 }
2038
2039 /// NewCompileUnit - Create new compile unit and it's debug information entry.
2040 ///
2041 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
2042 // Construct debug information entry.
2043 DIE *Die = new DIE(DW_TAG_compile_unit);
Argiris Kirtzidis03449652008-06-18 19:27:37 +00002044 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2045 DWLabel("section_line", 0), DWLabel("section_line", 0), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002046 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
2047 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
2048 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
Devang Patel6bcf9822008-12-12 21:57:54 +00002049 if (!UnitDesc->getDirectory().empty())
2050 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
aslc200b112008-08-16 12:57:46 +00002051
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002052 // Construct compile unit.
2053 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
aslc200b112008-08-16 12:57:46 +00002054
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002055 // Add Unit to compile unit map.
2056 DescToUnitMap[UnitDesc] = Unit;
aslc200b112008-08-16 12:57:46 +00002057
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002058 return Unit;
2059 }
2060
2061 /// GetBaseCompileUnit - Get the main compile unit.
2062 ///
2063 CompileUnit *GetBaseCompileUnit() const {
2064 CompileUnit *Unit = CompileUnits[0];
2065 assert(Unit && "Missing compile unit.");
2066 return Unit;
2067 }
2068
2069 /// FindCompileUnit - Get the compile unit for the given descriptor.
2070 ///
2071 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
2072 CompileUnit *Unit = DescToUnitMap[UnitDesc];
2073 assert(Unit && "Missing compile unit.");
2074 return Unit;
2075 }
2076
2077 /// NewGlobalVariable - Add a new global variable DIE.
2078 ///
2079 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
2080 // Get the compile unit context.
2081 CompileUnitDesc *UnitDesc =
2082 static_cast<CompileUnitDesc *>(GVD->getContext());
2083 CompileUnit *Unit = GetBaseCompileUnit();
2084
2085 // Check for pre-existence.
2086 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
2087 if (Slot) return Slot;
aslc200b112008-08-16 12:57:46 +00002088
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002089 // Get the global variable itself.
2090 GlobalVariable *GV = GVD->getGlobalVariable();
2091
2092 const std::string &Name = GVD->getName();
2093 const std::string &FullName = GVD->getFullName();
2094 const std::string &LinkageName = GVD->getLinkageName();
2095 // Create the global's variable DIE.
2096 DIE *VariableDie = new DIE(DW_TAG_variable);
2097 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
2098 if (!LinkageName.empty()) {
2099 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
2100 LinkageName);
2101 }
2102 AddType(VariableDie, GVD->getType(), Unit);
2103 if (!GVD->isStatic())
2104 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
aslc200b112008-08-16 12:57:46 +00002105
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002106 // Add source line info if available.
2107 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
aslc200b112008-08-16 12:57:46 +00002108
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002109 // Add address.
2110 DIEBlock *Block = new DIEBlock();
2111 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
2112 AddObjectLabel(Block, 0, DW_FORM_udata, Asm->getGlobalLinkName(GV));
2113 AddBlock(VariableDie, DW_AT_location, 0, Block);
aslc200b112008-08-16 12:57:46 +00002114
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002115 // Add to map.
2116 Slot = VariableDie;
aslc200b112008-08-16 12:57:46 +00002117
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002118 // Add to context owner.
2119 Unit->getDie()->AddChild(VariableDie);
aslc200b112008-08-16 12:57:46 +00002120
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002121 // Expose as global.
2122 // FIXME - need to check external flag.
2123 Unit->AddGlobal(FullName, VariableDie);
aslc200b112008-08-16 12:57:46 +00002124
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002125 return VariableDie;
2126 }
2127
2128 /// NewSubprogram - Add a new subprogram DIE.
2129 ///
2130 DIE *NewSubprogram(SubprogramDesc *SPD) {
2131 // Get the compile unit context.
2132 CompileUnitDesc *UnitDesc =
2133 static_cast<CompileUnitDesc *>(SPD->getContext());
2134 CompileUnit *Unit = GetBaseCompileUnit();
2135
2136 // Check for pre-existence.
2137 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
2138 if (Slot) return Slot;
aslc200b112008-08-16 12:57:46 +00002139
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002140 // Gather the details (simplify add attribute code.)
2141 const std::string &Name = SPD->getName();
2142 const std::string &FullName = SPD->getFullName();
2143 const std::string &LinkageName = SPD->getLinkageName();
aslc200b112008-08-16 12:57:46 +00002144
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002145 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
2146 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
2147 if (!LinkageName.empty()) {
2148 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
2149 LinkageName);
2150 }
2151 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
2152 if (!SPD->isStatic())
2153 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
2154 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
aslc200b112008-08-16 12:57:46 +00002155
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002156 // Add source line info if available.
2157 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
2158
2159 // Add to map.
2160 Slot = SubprogramDie;
aslc200b112008-08-16 12:57:46 +00002161
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002162 // Add to context owner.
2163 Unit->getDie()->AddChild(SubprogramDie);
aslc200b112008-08-16 12:57:46 +00002164
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002165 // Expose as global.
2166 Unit->AddGlobal(FullName, SubprogramDie);
aslc200b112008-08-16 12:57:46 +00002167
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002168 return SubprogramDie;
2169 }
2170
2171 /// NewScopeVariable - Create a new scope variable.
2172 ///
2173 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
2174 // Get the descriptor.
2175 VariableDesc *VD = DV->getDesc();
2176
2177 // Translate tag to proper Dwarf tag. The result variable is dropped for
2178 // now.
2179 unsigned Tag;
2180 switch (VD->getTag()) {
2181 case DW_TAG_return_variable: return NULL;
2182 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2183 case DW_TAG_auto_variable: // fall thru
2184 default: Tag = DW_TAG_variable; break;
2185 }
2186
2187 // Define variable debug information entry.
2188 DIE *VariableDie = new DIE(Tag);
2189 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
2190
2191 // Add source line info if available.
2192 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
aslc200b112008-08-16 12:57:46 +00002193
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002194 // Add variable type.
aslc200b112008-08-16 12:57:46 +00002195 AddType(VariableDie, VD->getType(), Unit);
2196
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002197 // Add variable address.
2198 MachineLocation Location;
Evan Cheng38948832008-01-31 03:37:28 +00002199 Location.set(RI->getFrameRegister(*MF),
2200 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002201 AddAddress(VariableDie, DW_AT_location, Location);
2202
2203 return VariableDie;
2204 }
2205
2206 /// ConstructScope - Construct the components of a scope.
2207 ///
2208 void ConstructScope(DebugScope *ParentScope,
2209 unsigned ParentStartID, unsigned ParentEndID,
2210 DIE *ParentDie, CompileUnit *Unit) {
2211 // Add variables to scope.
2212 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
2213 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2214 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
2215 if (VariableDie) ParentDie->AddChild(VariableDie);
2216 }
aslc200b112008-08-16 12:57:46 +00002217
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002218 // Add nested scopes.
2219 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
2220 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2221 // Define the Scope debug information entry.
2222 DebugScope *Scope = Scopes[j];
2223 // FIXME - Ignore inlined functions for the time being.
2224 if (!Scope->getParent()) continue;
aslc200b112008-08-16 12:57:46 +00002225
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002226 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2227 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
2228
2229 // Ignore empty scopes.
2230 if (StartID == EndID && StartID != 0) continue;
2231 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
aslc200b112008-08-16 12:57:46 +00002232
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002233 if (StartID == ParentStartID && EndID == ParentEndID) {
2234 // Just add stuff to the parent scope.
2235 ConstructScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2236 } else {
2237 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
aslc200b112008-08-16 12:57:46 +00002238
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002239 // Add the scope bounds.
2240 if (StartID) {
2241 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2242 DWLabel("label", StartID));
2243 } else {
2244 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2245 DWLabel("func_begin", SubprogramCount));
2246 }
2247 if (EndID) {
2248 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2249 DWLabel("label", EndID));
2250 } else {
2251 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2252 DWLabel("func_end", SubprogramCount));
2253 }
aslc200b112008-08-16 12:57:46 +00002254
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002255 // Add the scope contents.
2256 ConstructScope(Scope, StartID, EndID, ScopeDie, Unit);
2257 ParentDie->AddChild(ScopeDie);
2258 }
2259 }
2260 }
2261
2262 /// ConstructRootScope - Construct the scope for the subprogram.
2263 ///
2264 void ConstructRootScope(DebugScope *RootScope) {
2265 // Exit if there is no root scope.
2266 if (!RootScope) return;
aslc200b112008-08-16 12:57:46 +00002267
2268 // Get the subprogram debug information entry.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002269 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
aslc200b112008-08-16 12:57:46 +00002270
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002271 // Get the compile unit context.
2272 CompileUnit *Unit = GetBaseCompileUnit();
aslc200b112008-08-16 12:57:46 +00002273
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002274 // Get the subprogram die.
2275 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2276 assert(SPDie && "Missing subprogram descriptor");
aslc200b112008-08-16 12:57:46 +00002277
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002278 // Add the function bounds.
2279 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2280 DWLabel("func_begin", SubprogramCount));
2281 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2282 DWLabel("func_end", SubprogramCount));
2283 MachineLocation Location(RI->getFrameRegister(*MF));
2284 AddAddress(SPDie, DW_AT_frame_base, Location);
2285
2286 ConstructScope(RootScope, 0, 0, SPDie, Unit);
2287 }
2288
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002289 /// ConstructDefaultScope - Construct a default scope for the subprogram.
2290 ///
2291 void ConstructDefaultScope(MachineFunction *MF) {
2292 // Find the correct subprogram descriptor.
2293 std::vector<SubprogramDesc *> Subprograms;
2294 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
2295
2296 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2297 SubprogramDesc *SPD = Subprograms[i];
2298
2299 if (SPD->getName() == MF->getFunction()->getName()) {
2300 // Get the compile unit context.
2301 CompileUnit *Unit = GetBaseCompileUnit();
2302
2303 // Get the subprogram die.
2304 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2305 assert(SPDie && "Missing subprogram descriptor");
2306
2307 // Add the function bounds.
2308 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2309 DWLabel("func_begin", SubprogramCount));
2310 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2311 DWLabel("func_end", SubprogramCount));
2312
2313 MachineLocation Location(RI->getFrameRegister(*MF));
2314 AddAddress(SPDie, DW_AT_frame_base, Location);
2315 return;
2316 }
2317 }
Bill Wendlingae6b98c2008-10-17 18:48:57 +00002318#if 0
2319 // FIXME: This is causing an abort because C++ mangled names are compared
2320 // with their unmangled counterparts. See PR2885. Don't do this assert.
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002321 assert(0 && "Couldn't find DIE for machine function!");
Bill Wendlingae6b98c2008-10-17 18:48:57 +00002322#endif
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002323 }
2324
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002325 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2326 /// tools to recognize the object file contains Dwarf information.
2327 void EmitInitial() {
2328 // Check to see if we already emitted intial headers.
2329 if (didInitial) return;
2330 didInitial = true;
aslc200b112008-08-16 12:57:46 +00002331
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002332 // Dwarf sections base addresses.
2333 if (TAI->doesDwarfRequireFrameSection()) {
2334 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2335 EmitLabel("section_debug_frame", 0);
2336 }
2337 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2338 EmitLabel("section_info", 0);
2339 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2340 EmitLabel("section_abbrev", 0);
2341 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2342 EmitLabel("section_aranges", 0);
2343 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2344 EmitLabel("section_macinfo", 0);
2345 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2346 EmitLabel("section_line", 0);
2347 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2348 EmitLabel("section_loc", 0);
2349 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2350 EmitLabel("section_pubnames", 0);
2351 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2352 EmitLabel("section_str", 0);
2353 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2354 EmitLabel("section_ranges", 0);
2355
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002356 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002357 EmitLabel("text_begin", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00002358 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002359 EmitLabel("data_begin", 0);
2360 }
2361
2362 /// EmitDIE - Recusively Emits a debug information entry.
2363 ///
2364 void EmitDIE(DIE *Die) {
2365 // Get the abbreviation for this DIE.
2366 unsigned AbbrevNumber = Die->getAbbrevNumber();
2367 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
aslc200b112008-08-16 12:57:46 +00002368
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002369 Asm->EOL();
2370
2371 // Emit the code (index) for the abbreviation.
2372 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng0eeed442008-07-01 23:18:29 +00002373
2374 if (VerboseAsm)
2375 Asm->EOL(std::string("Abbrev [" +
2376 utostr(AbbrevNumber) +
2377 "] 0x" + utohexstr(Die->getOffset()) +
2378 ":0x" + utohexstr(Die->getSize()) + " " +
2379 TagString(Abbrev->getTag())));
2380 else
2381 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00002382
Owen Anderson88dd6232008-06-24 21:44:59 +00002383 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2384 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
aslc200b112008-08-16 12:57:46 +00002385
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002386 // Emit the DIE attribute values.
2387 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2388 unsigned Attr = AbbrevData[i].getAttribute();
2389 unsigned Form = AbbrevData[i].getForm();
2390 assert(Form && "Too many attributes for DIE (check abbreviation)");
aslc200b112008-08-16 12:57:46 +00002391
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002392 switch (Attr) {
2393 case DW_AT_sibling: {
2394 Asm->EmitInt32(Die->SiblingOffset());
2395 break;
2396 }
2397 default: {
2398 // Emit an attribute using the defined form.
2399 Values[i]->EmitValue(*this, Form);
2400 break;
2401 }
2402 }
aslc200b112008-08-16 12:57:46 +00002403
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002404 Asm->EOL(AttributeString(Attr));
2405 }
aslc200b112008-08-16 12:57:46 +00002406
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002407 // Emit the DIE children if any.
2408 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2409 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002410
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002411 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2412 EmitDIE(Children[j]);
2413 }
aslc200b112008-08-16 12:57:46 +00002414
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002415 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2416 }
2417 }
2418
2419 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2420 ///
2421 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2422 // Get the children.
2423 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002424
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002425 // If not last sibling and has children then add sibling offset attribute.
2426 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2427
2428 // Record the abbreviation.
2429 AssignAbbrevNumber(Die->getAbbrev());
aslc200b112008-08-16 12:57:46 +00002430
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002431 // Get the abbreviation for this DIE.
2432 unsigned AbbrevNumber = Die->getAbbrevNumber();
2433 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2434
2435 // Set DIE offset
2436 Die->setOffset(Offset);
aslc200b112008-08-16 12:57:46 +00002437
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002438 // Start the size with the size of abbreviation code.
aslc200b112008-08-16 12:57:46 +00002439 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2440
Owen Anderson88dd6232008-06-24 21:44:59 +00002441 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2442 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002443
2444 // Size the DIE attribute values.
2445 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2446 // Size attribute value.
2447 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2448 }
aslc200b112008-08-16 12:57:46 +00002449
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002450 // Size the DIE children if any.
2451 if (!Children.empty()) {
2452 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2453 "Children flag not set");
aslc200b112008-08-16 12:57:46 +00002454
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002455 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2456 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2457 }
aslc200b112008-08-16 12:57:46 +00002458
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002459 // End of children marker.
2460 Offset += sizeof(int8_t);
2461 }
2462
2463 Die->setSize(Offset - Die->getOffset());
2464 return Offset;
2465 }
2466
2467 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2468 ///
2469 void SizeAndOffsets() {
2470 // Process base compile unit.
2471 CompileUnit *Unit = GetBaseCompileUnit();
2472 // Compute size of compile unit header
2473 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2474 sizeof(int16_t) + // DWARF version number
2475 sizeof(int32_t) + // Offset Into Abbrev. Section
2476 sizeof(int8_t); // Pointer Size (in bytes)
2477 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2478 }
2479
2480 /// EmitDebugInfo - Emit the debug info section.
2481 ///
2482 void EmitDebugInfo() {
2483 // Start debug info section.
2484 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
aslc200b112008-08-16 12:57:46 +00002485
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002486 CompileUnit *Unit = GetBaseCompileUnit();
2487 DIE *Die = Unit->getDie();
2488 // Emit the compile units header.
2489 EmitLabel("info_begin", Unit->getID());
2490 // Emit size of content not including length itself
2491 unsigned ContentSize = Die->getSize() +
2492 sizeof(int16_t) + // DWARF version number
2493 sizeof(int32_t) + // Offset Into Abbrev. Section
2494 sizeof(int8_t) + // Pointer Size (in bytes)
2495 sizeof(int32_t); // FIXME - extra pad for gdb bug.
aslc200b112008-08-16 12:57:46 +00002496
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002497 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2498 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2499 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2500 Asm->EOL("Offset Into Abbrev. Section");
Dan Gohmancfb72b22007-09-27 23:12:31 +00002501 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
aslc200b112008-08-16 12:57:46 +00002502
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002503 EmitDIE(Die);
2504 // FIXME - extra padding for gdb bug.
2505 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2506 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2507 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2508 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2509 EmitLabel("info_end", Unit->getID());
aslc200b112008-08-16 12:57:46 +00002510
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002511 Asm->EOL();
2512 }
2513
2514 /// EmitAbbreviations - Emit the abbreviation section.
2515 ///
2516 void EmitAbbreviations() const {
2517 // Check to see if it is worth the effort.
2518 if (!Abbreviations.empty()) {
2519 // Start the debug abbrev section.
2520 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
aslc200b112008-08-16 12:57:46 +00002521
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002522 EmitLabel("abbrev_begin", 0);
aslc200b112008-08-16 12:57:46 +00002523
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002524 // For each abbrevation.
2525 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2526 // Get abbreviation data
2527 const DIEAbbrev *Abbrev = Abbreviations[i];
aslc200b112008-08-16 12:57:46 +00002528
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002529 // Emit the abbrevations code (base 1 index.)
2530 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2531 Asm->EOL("Abbreviation Code");
aslc200b112008-08-16 12:57:46 +00002532
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002533 // Emit the abbreviations data.
2534 Abbrev->Emit(*this);
aslc200b112008-08-16 12:57:46 +00002535
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002536 Asm->EOL();
2537 }
aslc200b112008-08-16 12:57:46 +00002538
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002539 // Mark end of abbreviations.
2540 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2541
2542 EmitLabel("abbrev_end", 0);
aslc200b112008-08-16 12:57:46 +00002543
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002544 Asm->EOL();
2545 }
2546 }
2547
Bill Wendling1983a2a2008-07-20 00:11:19 +00002548 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2549 /// the line matrix.
aslc200b112008-08-16 12:57:46 +00002550 ///
Bill Wendling1983a2a2008-07-20 00:11:19 +00002551 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2552 // Define last address of section.
2553 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2554 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2555 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2556 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2557
2558 // Mark end of matrix.
2559 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2560 Asm->EmitULEB128Bytes(1); Asm->EOL();
2561 Asm->EmitInt8(1); Asm->EOL();
2562 }
2563
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002564 /// EmitDebugLines - Emit source line information.
2565 ///
2566 void EmitDebugLines() {
Bill Wendling1983a2a2008-07-20 00:11:19 +00002567 // If the target is using .loc/.file, the assembler will be emitting the
2568 // .debug_line table automatically.
2569 if (TAI->hasDotLocAndDotFile())
Dan Gohmanc55b34a2007-09-24 21:43:52 +00002570 return;
2571
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002572 // Minimum line delta, thus ranging from -10..(255-10).
2573 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2574 // Maximum line delta, thus ranging from -10..(255-10).
2575 const int MaxLineDelta = 255 + MinLineDelta;
2576
2577 // Start the dwarf line section.
2578 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
aslc200b112008-08-16 12:57:46 +00002579
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002580 // Construct the section header.
aslc200b112008-08-16 12:57:46 +00002581
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002582 EmitDifference("line_end", 0, "line_begin", 0, true);
2583 Asm->EOL("Length of Source Line Info");
2584 EmitLabel("line_begin", 0);
aslc200b112008-08-16 12:57:46 +00002585
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002586 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
aslc200b112008-08-16 12:57:46 +00002587
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002588 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2589 Asm->EOL("Prolog Length");
2590 EmitLabel("line_prolog_begin", 0);
aslc200b112008-08-16 12:57:46 +00002591
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002592 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2593
2594 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2595
2596 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
aslc200b112008-08-16 12:57:46 +00002597
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002598 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2599
2600 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
aslc200b112008-08-16 12:57:46 +00002601
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002602 // Line number standard opcode encodings argument count
2603 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2604 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2605 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2606 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2607 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2608 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2609 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2610 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2611 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2612
2613 const UniqueVector<std::string> &Directories = MMI->getDirectories();
Evan Cheng0eeed442008-07-01 23:18:29 +00002614 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002615
2616 // Emit directories.
2617 for (unsigned DirectoryID = 1, NDID = Directories.size();
2618 DirectoryID <= NDID; ++DirectoryID) {
2619 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
2620 }
2621 Asm->EmitInt8(0); Asm->EOL("End of directories");
aslc200b112008-08-16 12:57:46 +00002622
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002623 // Emit files.
2624 for (unsigned SourceID = 1, NSID = SourceFiles.size();
2625 SourceID <= NSID; ++SourceID) {
2626 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2627 Asm->EmitString(SourceFile.getName());
2628 Asm->EOL("Source");
2629 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
2630 Asm->EOL("Directory #");
2631 Asm->EmitULEB128Bytes(0);
2632 Asm->EOL("Mod date");
2633 Asm->EmitULEB128Bytes(0);
2634 Asm->EOL("File size");
2635 }
2636 Asm->EmitInt8(0); Asm->EOL("End of files");
aslc200b112008-08-16 12:57:46 +00002637
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002638 EmitLabel("line_prolog_end", 0);
aslc200b112008-08-16 12:57:46 +00002639
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002640 // A sequence for each text section.
Bill Wendling1983a2a2008-07-20 00:11:19 +00002641 unsigned SecSrcLinesSize = SectionSourceLines.size();
2642
2643 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002644 // Isolate current sections line info.
2645 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng0eeed442008-07-01 23:18:29 +00002646
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002647 if (VerboseAsm) {
2648 const Section* S = SectionMap[j + 1];
2649 Asm->EOL(std::string("Section ") + S->getName());
2650 } else
Evan Cheng0eeed442008-07-01 23:18:29 +00002651 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002652
2653 // Dwarf assumes we start with first line of first source file.
2654 unsigned Source = 1;
2655 unsigned Line = 1;
aslc200b112008-08-16 12:57:46 +00002656
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002657 // Construct rows of the address, source, line, column matrix.
2658 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2659 const SourceLineInfo &LineInfo = LineInfos[i];
2660 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2661 if (!LabelID) continue;
aslc200b112008-08-16 12:57:46 +00002662
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002663 unsigned SourceID = LineInfo.getSourceID();
2664 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2665 unsigned DirectoryID = SourceFile.getDirectoryID();
Evan Cheng0eeed442008-07-01 23:18:29 +00002666 if (VerboseAsm)
2667 Asm->EOL(Directories[DirectoryID]
2668 + SourceFile.getName()
2669 + ":"
2670 + utostr_32(LineInfo.getLine()));
2671 else
2672 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002673
2674 // Define the line address.
2675 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohmancfb72b22007-09-27 23:12:31 +00002676 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002677 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2678 EmitReference("label", LabelID); Asm->EOL("Location label");
aslc200b112008-08-16 12:57:46 +00002679
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002680 // If change of source, then switch to the new source.
2681 if (Source != LineInfo.getSourceID()) {
2682 Source = LineInfo.getSourceID();
2683 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2684 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2685 }
aslc200b112008-08-16 12:57:46 +00002686
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002687 // If change of line.
2688 if (Line != LineInfo.getLine()) {
2689 // Determine offset.
2690 int Offset = LineInfo.getLine() - Line;
2691 int Delta = Offset - MinLineDelta;
aslc200b112008-08-16 12:57:46 +00002692
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002693 // Update line.
2694 Line = LineInfo.getLine();
aslc200b112008-08-16 12:57:46 +00002695
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002696 // If delta is small enough and in range...
2697 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2698 // ... then use fast opcode.
2699 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2700 } else {
2701 // ... otherwise use long hand.
2702 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2703 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2704 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2705 }
2706 } else {
2707 // Copy the previous row (different address or source)
2708 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2709 }
2710 }
2711
Bill Wendling1983a2a2008-07-20 00:11:19 +00002712 EmitEndOfLineMatrix(j + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002713 }
Bill Wendling1983a2a2008-07-20 00:11:19 +00002714
2715 if (SecSrcLinesSize == 0)
2716 // Because we're emitting a debug_line section, we still need a line
2717 // table. The linker and friends expect it to exist. If there's nothing to
2718 // put into it, emit an empty table.
2719 EmitEndOfLineMatrix(1);
aslc200b112008-08-16 12:57:46 +00002720
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002721 EmitLabel("line_end", 0);
aslc200b112008-08-16 12:57:46 +00002722
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002723 Asm->EOL();
2724 }
aslc200b112008-08-16 12:57:46 +00002725
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002726 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2727 ///
2728 void EmitCommonDebugFrame() {
2729 if (!TAI->doesDwarfRequireFrameSection())
2730 return;
2731
2732 int stackGrowth =
2733 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2734 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00002735 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002736
2737 // Start the dwarf frame section.
2738 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2739
2740 EmitLabel("debug_frame_common", 0);
2741 EmitDifference("debug_frame_common_end", 0,
2742 "debug_frame_common_begin", 0, true);
2743 Asm->EOL("Length of Common Information Entry");
2744
2745 EmitLabel("debug_frame_common_begin", 0);
2746 Asm->EmitInt32((int)DW_CIE_ID);
2747 Asm->EOL("CIE Identifier Tag");
2748 Asm->EmitInt8(DW_CIE_VERSION);
2749 Asm->EOL("CIE Version");
2750 Asm->EmitString("");
2751 Asm->EOL("CIE Augmentation");
2752 Asm->EmitULEB128Bytes(1);
2753 Asm->EOL("CIE Code Alignment Factor");
2754 Asm->EmitSLEB128Bytes(stackGrowth);
aslc200b112008-08-16 12:57:46 +00002755 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00002756 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002757 Asm->EOL("CIE RA Column");
aslc200b112008-08-16 12:57:46 +00002758
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002759 std::vector<MachineMove> Moves;
2760 RI->getInitialFrameState(Moves);
2761
Dale Johannesenf5a11532007-11-13 19:13:01 +00002762 EmitFrameMoves(NULL, 0, Moves, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002763
Evan Cheng7e7d1942008-02-29 19:36:59 +00002764 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002765 EmitLabel("debug_frame_common_end", 0);
aslc200b112008-08-16 12:57:46 +00002766
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002767 Asm->EOL();
2768 }
2769
2770 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2771 /// section.
2772 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
2773 if (!TAI->doesDwarfRequireFrameSection())
2774 return;
aslc200b112008-08-16 12:57:46 +00002775
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002776 // Start the dwarf frame section.
2777 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
aslc200b112008-08-16 12:57:46 +00002778
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002779 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2780 "debug_frame_begin", DebugFrameInfo.Number, true);
2781 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00002782
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002783 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2784
2785 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2786 0, 0, true, false);
2787 Asm->EOL("FDE CIE offset");
2788
2789 EmitReference("func_begin", DebugFrameInfo.Number);
2790 Asm->EOL("FDE initial location");
2791 EmitDifference("func_end", DebugFrameInfo.Number,
2792 "func_begin", DebugFrameInfo.Number);
2793 Asm->EOL("FDE address range");
aslc200b112008-08-16 12:57:46 +00002794
Dale Johannesenf5a11532007-11-13 19:13:01 +00002795 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves, false);
aslc200b112008-08-16 12:57:46 +00002796
Evan Cheng7e7d1942008-02-29 19:36:59 +00002797 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002798 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2799
2800 Asm->EOL();
2801 }
2802
2803 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2804 ///
2805 void EmitDebugPubNames() {
2806 // Start the dwarf pubnames section.
2807 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
aslc200b112008-08-16 12:57:46 +00002808
2809 CompileUnit *Unit = GetBaseCompileUnit();
2810
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002811 EmitDifference("pubnames_end", Unit->getID(),
2812 "pubnames_begin", Unit->getID(), true);
2813 Asm->EOL("Length of Public Names Info");
aslc200b112008-08-16 12:57:46 +00002814
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002815 EmitLabel("pubnames_begin", Unit->getID());
aslc200b112008-08-16 12:57:46 +00002816
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002817 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2818
2819 EmitSectionOffset("info_begin", "section_info",
2820 Unit->getID(), 0, true, false);
2821 Asm->EOL("Offset of Compilation Unit Info");
2822
2823 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
2824 Asm->EOL("Compilation Unit Length");
aslc200b112008-08-16 12:57:46 +00002825
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002826 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
aslc200b112008-08-16 12:57:46 +00002827
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002828 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2829 GE = Globals.end();
2830 GI != GE; ++GI) {
2831 const std::string &Name = GI->first;
2832 DIE * Entity = GI->second;
aslc200b112008-08-16 12:57:46 +00002833
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002834 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2835 Asm->EmitString(Name); Asm->EOL("External Name");
2836 }
aslc200b112008-08-16 12:57:46 +00002837
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002838 Asm->EmitInt32(0); Asm->EOL("End Mark");
2839 EmitLabel("pubnames_end", Unit->getID());
aslc200b112008-08-16 12:57:46 +00002840
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002841 Asm->EOL();
2842 }
2843
2844 /// EmitDebugStr - Emit visible names into a debug str section.
2845 ///
2846 void EmitDebugStr() {
2847 // Check to see if it is worth the effort.
2848 if (!StringPool.empty()) {
2849 // Start the dwarf str section.
2850 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
aslc200b112008-08-16 12:57:46 +00002851
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002852 // For each of strings in the string pool.
2853 for (unsigned StringID = 1, N = StringPool.size();
2854 StringID <= N; ++StringID) {
2855 // Emit a label for reference from debug information entries.
2856 EmitLabel("string", StringID);
2857 // Emit the string itself.
2858 const std::string &String = StringPool[StringID];
2859 Asm->EmitString(String); Asm->EOL();
2860 }
aslc200b112008-08-16 12:57:46 +00002861
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002862 Asm->EOL();
2863 }
2864 }
2865
2866 /// EmitDebugLoc - Emit visible names into a debug loc section.
2867 ///
2868 void EmitDebugLoc() {
2869 // Start the dwarf loc section.
2870 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
aslc200b112008-08-16 12:57:46 +00002871
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002872 Asm->EOL();
2873 }
2874
2875 /// EmitDebugARanges - Emit visible names into a debug aranges section.
2876 ///
2877 void EmitDebugARanges() {
2878 // Start the dwarf aranges section.
2879 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
aslc200b112008-08-16 12:57:46 +00002880
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002881 // FIXME - Mock up
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002882#if 0
aslc200b112008-08-16 12:57:46 +00002883 CompileUnit *Unit = GetBaseCompileUnit();
2884
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002885 // Don't include size of length
2886 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
aslc200b112008-08-16 12:57:46 +00002887
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002888 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
aslc200b112008-08-16 12:57:46 +00002889
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002890 EmitReference("info_begin", Unit->getID());
2891 Asm->EOL("Offset of Compilation Unit Info");
2892
Dan Gohmancfb72b22007-09-27 23:12:31 +00002893 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002894
2895 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2896
2897 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2898 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2899
2900 // Range 1
2901 EmitReference("text_begin", 0); Asm->EOL("Address");
2902 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2903
2904 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2905 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002906#endif
aslc200b112008-08-16 12:57:46 +00002907
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002908 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002909 }
2910
2911 /// EmitDebugRanges - Emit visible names into a debug ranges section.
2912 ///
2913 void EmitDebugRanges() {
2914 // Start the dwarf ranges section.
2915 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
aslc200b112008-08-16 12:57:46 +00002916
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002917 Asm->EOL();
2918 }
2919
2920 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2921 ///
2922 void EmitDebugMacInfo() {
2923 // Start the dwarf macinfo section.
2924 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
aslc200b112008-08-16 12:57:46 +00002925
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002926 Asm->EOL();
2927 }
2928
2929 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2930 /// header file.
2931 void ConstructCompileUnitDIEs() {
2932 const UniqueVector<CompileUnitDesc *> CUW = MMI->getCompileUnits();
aslc200b112008-08-16 12:57:46 +00002933
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002934 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
2935 unsigned ID = MMI->RecordSource(CUW[i]);
2936 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
2937 CompileUnits.push_back(Unit);
2938 }
2939 }
2940
2941 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
2942 /// global variables.
2943 void ConstructGlobalDIEs() {
Bill Wendling75091f92008-07-03 23:13:02 +00002944 std::vector<GlobalVariableDesc *> GlobalVariables;
2945 MMI->getAnchoredDescriptors<GlobalVariableDesc>(*M, GlobalVariables);
aslc200b112008-08-16 12:57:46 +00002946
Bill Wendling4de8de52008-07-03 22:53:42 +00002947 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
2948 GlobalVariableDesc *GVD = GlobalVariables[i];
2949 NewGlobalVariable(GVD);
2950 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002951 }
2952
2953 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2954 /// subprograms.
2955 void ConstructSubprogramDIEs() {
Bill Wendling75091f92008-07-03 23:13:02 +00002956 std::vector<SubprogramDesc *> Subprograms;
2957 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
aslc200b112008-08-16 12:57:46 +00002958
Bill Wendling4de8de52008-07-03 22:53:42 +00002959 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2960 SubprogramDesc *SPD = Subprograms[i];
2961 NewSubprogram(SPD);
2962 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002963 }
2964
2965public:
2966 //===--------------------------------------------------------------------===//
2967 // Main entry points.
2968 //
Owen Anderson847b99b2008-08-21 00:14:44 +00002969 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattnerb3876c72007-09-24 03:35:37 +00002970 : Dwarf(OS, A, T, "dbg")
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002971 , CompileUnits()
2972 , AbbreviationsSet(InitAbbreviationsSetSize)
2973 , Abbreviations()
2974 , ValuesSet(InitValuesSetSize)
2975 , Values()
2976 , StringPool()
2977 , DescToUnitMap()
2978 , SectionMap()
2979 , SectionSourceLines()
2980 , didInitial(false)
2981 , shouldEmit(false)
2982 {
2983 }
2984 virtual ~DwarfDebug() {
2985 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
2986 delete CompileUnits[i];
2987 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2988 delete Values[j];
2989 }
2990
2991 /// SetModuleInfo - Set machine module information when it's known that pass
2992 /// manager has created it. Set by the target AsmPrinter.
2993 void SetModuleInfo(MachineModuleInfo *mmi) {
2994 // Make sure initial declarations are made.
2995 if (!MMI && mmi->hasDebugInfo()) {
2996 MMI = mmi;
2997 shouldEmit = true;
aslc200b112008-08-16 12:57:46 +00002998
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002999 // Create all the compile unit DIEs.
3000 ConstructCompileUnitDIEs();
aslc200b112008-08-16 12:57:46 +00003001
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003002 // Create DIEs for each of the externally visible global variables.
3003 ConstructGlobalDIEs();
3004
3005 // Create DIEs for each of the externally visible subprograms.
3006 ConstructSubprogramDIEs();
aslc200b112008-08-16 12:57:46 +00003007
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003008 // Prime section data.
3009 SectionMap.insert(TAI->getTextSection());
Dan Gohman6d6c2402007-10-01 22:40:20 +00003010
3011 // Print out .file directives to specify files for .loc directives. These
3012 // are printed out early so that they precede any .loc directives.
3013 if (TAI->hasDotLocAndDotFile()) {
3014 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
3015 const UniqueVector<std::string> &Directories = MMI->getDirectories();
3016 for (unsigned i = 1, e = SourceFiles.size(); i <= e; ++i) {
3017 sys::Path FullPath(Directories[SourceFiles[i].getDirectoryID()]);
3018 bool AppendOk = FullPath.appendComponent(SourceFiles[i].getName());
3019 assert(AppendOk && "Could not append filename to directory!");
Devang Patel105a08a2008-12-23 21:55:38 +00003020 AppendOk = false;
Dan Gohman6d6c2402007-10-01 22:40:20 +00003021 Asm->EmitFile(i, FullPath.toString());
3022 Asm->EOL();
3023 }
3024 }
3025
3026 // Emit initial sections
3027 EmitInitial();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003028 }
3029 }
3030
3031 /// BeginModule - Emit all Dwarf sections that should come prior to the
3032 /// content.
3033 void BeginModule(Module *M) {
3034 this->M = M;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003035 }
3036
3037 /// EndModule - Emit all Dwarf sections that should come after the content.
3038 ///
3039 void EndModule() {
3040 if (!ShouldEmitDwarf()) return;
aslc200b112008-08-16 12:57:46 +00003041
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003042 // Standard sections final addresses.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003043 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003044 EmitLabel("text_end", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00003045 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003046 EmitLabel("data_end", 0);
aslc200b112008-08-16 12:57:46 +00003047
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003048 // End text sections.
3049 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003050 Asm->SwitchToSection(SectionMap[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003051 EmitLabel("section_end", i);
3052 }
3053
3054 // Emit common frame information.
3055 EmitCommonDebugFrame();
3056
3057 // Emit function debug frame information
3058 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3059 E = DebugFrames.end(); I != E; ++I)
3060 EmitFunctionDebugFrame(*I);
3061
3062 // Compute DIE offsets and sizes.
3063 SizeAndOffsets();
aslc200b112008-08-16 12:57:46 +00003064
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003065 // Emit all the DIEs into a debug info section
3066 EmitDebugInfo();
aslc200b112008-08-16 12:57:46 +00003067
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003068 // Corresponding abbreviations into a abbrev section.
3069 EmitAbbreviations();
aslc200b112008-08-16 12:57:46 +00003070
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003071 // Emit source line correspondence into a debug line section.
3072 EmitDebugLines();
aslc200b112008-08-16 12:57:46 +00003073
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003074 // Emit info into a debug pubnames section.
3075 EmitDebugPubNames();
aslc200b112008-08-16 12:57:46 +00003076
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003077 // Emit info into a debug str section.
3078 EmitDebugStr();
aslc200b112008-08-16 12:57:46 +00003079
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003080 // Emit info into a debug loc section.
3081 EmitDebugLoc();
aslc200b112008-08-16 12:57:46 +00003082
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003083 // Emit info into a debug aranges section.
3084 EmitDebugARanges();
aslc200b112008-08-16 12:57:46 +00003085
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003086 // Emit info into a debug ranges section.
3087 EmitDebugRanges();
aslc200b112008-08-16 12:57:46 +00003088
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003089 // Emit info into a debug macinfo section.
3090 EmitDebugMacInfo();
3091 }
3092
aslc200b112008-08-16 12:57:46 +00003093 /// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003094 /// emitted immediately after the function entry point.
3095 void BeginFunction(MachineFunction *MF) {
3096 this->MF = MF;
aslc200b112008-08-16 12:57:46 +00003097
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003098 if (!ShouldEmitDwarf()) return;
3099
3100 // Begin accumulating function debug information.
3101 MMI->BeginFunction(MF);
aslc200b112008-08-16 12:57:46 +00003102
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003103 // Assumes in correct section after the entry point.
3104 EmitLabel("func_begin", ++SubprogramCount);
Evan Chenga53c40a2008-02-01 09:10:45 +00003105
3106 // Emit label for the implicitly defined dbg.stoppoint at the start of
3107 // the function.
Andrew Lenharth42f91402008-04-03 17:37:43 +00003108 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
3109 if (!LineInfos.empty()) {
3110 const SourceLineInfo &LineInfo = LineInfos[0];
3111 Asm->printLabel(LineInfo.getLabelID());
3112 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003113 }
aslc200b112008-08-16 12:57:46 +00003114
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003115 /// EndFunction - Gather and emit post-function debug information.
3116 ///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003117 void EndFunction(MachineFunction *MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003118 if (!ShouldEmitDwarf()) return;
aslc200b112008-08-16 12:57:46 +00003119
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003120 // Define end label for subprogram.
3121 EmitLabel("func_end", SubprogramCount);
aslc200b112008-08-16 12:57:46 +00003122
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003123 // Get function line info.
3124 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
3125
3126 if (!LineInfos.empty()) {
3127 // Get section line info.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003128 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003129 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
3130 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
3131 // Append the function info to section info.
3132 SectionLineInfos.insert(SectionLineInfos.end(),
3133 LineInfos.begin(), LineInfos.end());
3134 }
aslc200b112008-08-16 12:57:46 +00003135
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003136 // Construct scopes for subprogram.
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003137 if (MMI->getRootScope())
3138 ConstructRootScope(MMI->getRootScope());
3139 else
3140 // FIXME: This is wrong. We are essentially getting past a problem with
3141 // debug information not being able to handle unreachable blocks that have
3142 // debug information in them. In particular, those unreachable blocks that
3143 // have "region end" info in them. That situation results in the "root
3144 // scope" not being created. If that's the case, then emit a "default"
3145 // scope, i.e., one that encompasses the whole function. This isn't
3146 // desirable. And a better way of handling this (and all of the debugging
3147 // information) needs to be explored.
3148 ConstructDefaultScope(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003149
3150 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3151 MMI->getFrameMoves()));
3152 }
3153};
3154
3155//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00003156/// DwarfException - Emits Dwarf exception handling directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003157///
3158class DwarfException : public Dwarf {
3159
3160private:
3161 struct FunctionEHFrameInfo {
3162 std::string FnName;
3163 unsigned Number;
3164 unsigned PersonalityIndex;
3165 bool hasCalls;
3166 bool hasLandingPads;
3167 std::vector<MachineMove> Moves;
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003168 const Function * function;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003169
3170 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3171 bool hC, bool hL,
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003172 const std::vector<MachineMove> &M,
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003173 const Function *f):
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003174 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003175 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003176 };
3177
3178 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen85535762008-04-02 00:25:04 +00003179
3180 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3181 /// be emitted.
3182 bool shouldEmitTable;
3183
3184 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3185 /// should be emitted.
3186 bool shouldEmitMoves;
3187
3188 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3189 /// should be emitted.
3190 bool shouldEmitTableModule;
3191
aslc200b112008-08-16 12:57:46 +00003192 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen85535762008-04-02 00:25:04 +00003193 /// should be emitted.
3194 bool shouldEmitMovesModule;
Duncan Sands96144f92008-05-07 19:11:09 +00003195
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003196 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3197 ///
3198 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
3199 // Size and sign of stack growth.
3200 int stackGrowth =
3201 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3202 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00003203 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003204
3205 // Begin eh frame section.
3206 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling189bde72008-12-24 08:05:17 +00003207
3208 if (!TAI->doesRequireNonLocalEHFrameLabel())
3209 O << TAI->getEHGlobalPrefix();
3210 O << "EH_frame" << Index << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003211 EmitLabel("section_eh_frame", Index);
3212
3213 // Define base labels.
3214 EmitLabel("eh_frame_common", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003215
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003216 // Define the eh frame length.
3217 EmitDifference("eh_frame_common_end", Index,
3218 "eh_frame_common_begin", Index, true);
3219 Asm->EOL("Length of Common Information Entry");
3220
3221 // EH frame header.
3222 EmitLabel("eh_frame_common_begin", Index);
3223 Asm->EmitInt32((int)0);
3224 Asm->EOL("CIE Identifier Tag");
3225 Asm->EmitInt8(DW_CIE_VERSION);
3226 Asm->EOL("CIE Version");
Duncan Sands96144f92008-05-07 19:11:09 +00003227
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003228 // The personality presence indicates that language specific information
3229 // will show up in the eh frame.
3230 Asm->EmitString(Personality ? "zPLR" : "zR");
3231 Asm->EOL("CIE Augmentation");
Duncan Sands96144f92008-05-07 19:11:09 +00003232
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003233 // Round out reader.
3234 Asm->EmitULEB128Bytes(1);
3235 Asm->EOL("CIE Code Alignment Factor");
3236 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands96144f92008-05-07 19:11:09 +00003237 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00003238 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands96144f92008-05-07 19:11:09 +00003239 Asm->EOL("CIE Return Address Column");
3240
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003241 // If there is a personality, we need to indicate the functions location.
3242 if (Personality) {
3243 Asm->EmitULEB128Bytes(7);
3244 Asm->EOL("Augmentation Size");
Bill Wendling2d369922007-09-11 17:20:55 +00003245
Duncan Sands96144f92008-05-07 19:11:09 +00003246 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendling2d369922007-09-11 17:20:55 +00003247 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands96144f92008-05-07 19:11:09 +00003248 Asm->EOL("Personality (pcrel sdata4 indirect)");
3249 } else {
Bill Wendling2d369922007-09-11 17:20:55 +00003250 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands96144f92008-05-07 19:11:09 +00003251 Asm->EOL("Personality (pcrel sdata4)");
3252 }
Bill Wendling2d369922007-09-11 17:20:55 +00003253
Duncan Sands96144f92008-05-07 19:11:09 +00003254 PrintRelDirective(true);
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003255 O << TAI->getPersonalityPrefix();
3256 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3257 O << TAI->getPersonalitySuffix();
Duncan Sands4cc39532008-05-08 12:33:11 +00003258 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3259 O << "-" << TAI->getPCSymbol();
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003260 Asm->EOL("Personality");
Bill Wendling38cb7c92007-08-25 00:51:55 +00003261
Duncan Sands96144f92008-05-07 19:11:09 +00003262 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3263 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003264
3265 if (TAI->doesFDEEncodingRequireSData4()) {
3266 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3267 Asm->EOL("FDE Encoding (pcrel sdata4)");
3268 } else {
3269 Asm->EmitInt8(DW_EH_PE_pcrel);
3270 Asm->EOL("FDE Encoding (pcrel)");
3271 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003272 } else {
3273 Asm->EmitULEB128Bytes(1);
3274 Asm->EOL("Augmentation Size");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003275
3276 if (TAI->doesFDEEncodingRequireSData4()) {
3277 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3278 Asm->EOL("FDE Encoding (pcrel sdata4)");
3279 } else {
3280 Asm->EmitInt8(DW_EH_PE_pcrel);
3281 Asm->EOL("FDE Encoding (pcrel)");
3282 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003283 }
3284
3285 // Indicate locations of general callee saved registers in frame.
3286 std::vector<MachineMove> Moves;
3287 RI->getInitialFrameState(Moves);
Dale Johannesenf5a11532007-11-13 19:13:01 +00003288 EmitFrameMoves(NULL, 0, Moves, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003289
Dale Johannesen388f20f2008-04-30 00:43:29 +00003290 // On Darwin the linker honors the alignment of eh_frame, which means it
3291 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3292 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003293 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003294 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003295 EmitLabel("eh_frame_common_end", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003296
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003297 Asm->EOL();
3298 }
Duncan Sands96144f92008-05-07 19:11:09 +00003299
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003300 /// EmitEHFrame - Emit function exception frame information.
3301 ///
3302 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003303 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3304
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003305 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3306
3307 // Externally visible entry into the functions eh frame info.
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003308 // If the corresponding function is static, this should not be
3309 // externally visible.
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003310 if (linkage != Function::InternalLinkage) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003311 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3312 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3313 }
3314
Dale Johannesenf09b5992008-01-10 02:03:30 +00003315 // If corresponding function is weak definition, this should be too.
aslc200b112008-08-16 12:57:46 +00003316 if ((linkage == Function::WeakLinkage ||
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003317 linkage == Function::LinkOnceLinkage) &&
Dale Johannesenf09b5992008-01-10 02:03:30 +00003318 TAI->getWeakDefDirective())
3319 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3320
3321 // If there are no calls then you can't unwind. This may mean we can
3322 // omit the EH Frame, but some environments do not handle weak absolute
aslc200b112008-08-16 12:57:46 +00003323 // symbols.
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003324 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesena9b3e482008-04-08 00:10:24 +00003325 // unwind info is to be available for non-EH uses.
Dale Johannesenf09b5992008-01-10 02:03:30 +00003326 if (!EHFrameInfo.hasCalls &&
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003327 !UnwindTablesMandatory &&
aslc200b112008-08-16 12:57:46 +00003328 ((linkage != Function::WeakLinkage &&
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003329 linkage != Function::LinkOnceLinkage) ||
Dale Johannesenf09b5992008-01-10 02:03:30 +00003330 !TAI->getWeakDefDirective() ||
3331 TAI->getSupportsWeakOmittedEHFrame()))
aslc200b112008-08-16 12:57:46 +00003332 {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003333 O << EHFrameInfo.FnName << " = 0\n";
aslc200b112008-08-16 12:57:46 +00003334 // This name has no connection to the function, so it might get
3335 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003336 // dead-stripping unconditionally.
3337 if (const char *UsedDirective = TAI->getUsedDirective())
3338 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003339 } else {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003340 O << EHFrameInfo.FnName << ":\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003341
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003342 // EH frame header.
3343 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3344 "eh_frame_begin", EHFrameInfo.Number, true);
3345 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00003346
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003347 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
3348
Bill Wendling189bde72008-12-24 08:05:17 +00003349 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3350 PrintRelDirective(true, true);
3351 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3352
3353 if (!TAI->isAbsoluteEHSectionOffsets())
3354 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3355 } else {
3356 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3357 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3358 true, true, false);
3359 }
3360
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003361 Asm->EOL("FDE CIE offset");
3362
Bill Wendlingeeef8b22008-12-29 22:12:11 +00003363 EmitReference("eh_func_begin", EHFrameInfo.Number, true,
3364 TAI->doesRequire32BitFDEReference());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003365 Asm->EOL("FDE initial location");
3366 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendlingeeef8b22008-12-29 22:12:11 +00003367 "eh_func_begin", EHFrameInfo.Number,
3368 TAI->doesRequire32BitFDEReference());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003369 Asm->EOL("FDE address range");
Duncan Sands96144f92008-05-07 19:11:09 +00003370
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003371 // If there is a personality and landing pads then point to the language
3372 // specific data area in the exception table.
3373 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands96144f92008-05-07 19:11:09 +00003374 Asm->EmitULEB128Bytes(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003375 Asm->EOL("Augmentation size");
Duncan Sands96144f92008-05-07 19:11:09 +00003376
3377 if (EHFrameInfo.hasLandingPads)
3378 EmitReference("exception", EHFrameInfo.Number, true, true);
3379 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003380 Asm->EmitInt32((int)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003381 Asm->EOL("Language Specific Data Area");
3382 } else {
3383 Asm->EmitULEB128Bytes(0);
3384 Asm->EOL("Augmentation size");
3385 }
Duncan Sands96144f92008-05-07 19:11:09 +00003386
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003387 // Indicate locations of function specific callee saved registers in
3388 // frame.
Dale Johannesenf5a11532007-11-13 19:13:01 +00003389 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves, true);
aslc200b112008-08-16 12:57:46 +00003390
Dale Johannesen388f20f2008-04-30 00:43:29 +00003391 // On Darwin the linker honors the alignment of eh_frame, which means it
3392 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3393 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003394 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003395 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003396 EmitLabel("eh_frame_end", EHFrameInfo.Number);
aslc200b112008-08-16 12:57:46 +00003397
3398 // If the function is marked used, this table should be also. We cannot
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003399 // make the mark unconditional in this case, since retaining the table
aslc200b112008-08-16 12:57:46 +00003400 // also retains the function in this case, and there is code around
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003401 // that depends on unused functions (calling undefined externals) being
3402 // dead-stripped to link correctly. Yes, there really is.
3403 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3404 if (const char *UsedDirective = TAI->getUsedDirective())
3405 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3406 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003407 }
3408
Duncan Sands241a0c92007-09-05 11:27:52 +00003409 /// EmitExceptionTable - Emit landing pads and actions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003410 ///
3411 /// The general organization of the table is complex, but the basic concepts
3412 /// are easy. First there is a header which describes the location and
3413 /// organization of the three components that follow.
3414 /// 1. The landing pad site information describes the range of code covered
3415 /// by the try. In our case it's an accumulation of the ranges covered
3416 /// by the invokes in the try. There is also a reference to the landing
3417 /// pad that handles the exception once processed. Finally an index into
3418 /// the actions table.
3419 /// 2. The action table, in our case, is composed of pairs of type ids
3420 /// and next action offset. Starting with the action index from the
3421 /// landing pad site, each type Id is checked for a match to the current
3422 /// exception. If it matches then the exception and type id are passed
3423 /// on to the landing pad. Otherwise the next action is looked up. This
3424 /// chain is terminated with a next action of zero. If no type id is
3425 /// found the the frame is unwound and handling continues.
3426 /// 3. Type id table contains references to all the C++ typeinfo for all
3427 /// catches in the function. This tables is reversed indexed base 1.
3428
3429 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3430 static unsigned SharedTypeIds(const LandingPadInfo *L,
3431 const LandingPadInfo *R) {
3432 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3433 unsigned LSize = LIds.size(), RSize = RIds.size();
3434 unsigned MinSize = LSize < RSize ? LSize : RSize;
3435 unsigned Count = 0;
3436
3437 for (; Count != MinSize; ++Count)
3438 if (LIds[Count] != RIds[Count])
3439 return Count;
3440
3441 return Count;
3442 }
3443
3444 /// PadLT - Order landing pads lexicographically by type id.
3445 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
3446 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3447 unsigned LSize = LIds.size(), RSize = RIds.size();
3448 unsigned MinSize = LSize < RSize ? LSize : RSize;
3449
3450 for (unsigned i = 0; i != MinSize; ++i)
3451 if (LIds[i] != RIds[i])
3452 return LIds[i] < RIds[i];
3453
3454 return LSize < RSize;
3455 }
3456
3457 struct KeyInfo {
3458 static inline unsigned getEmptyKey() { return -1U; }
3459 static inline unsigned getTombstoneKey() { return -2U; }
3460 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner92eea072007-09-17 18:34:04 +00003461 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003462 static bool isPod() { return true; }
3463 };
3464
Duncan Sands241a0c92007-09-05 11:27:52 +00003465 /// ActionEntry - Structure describing an entry in the actions table.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003466 struct ActionEntry {
3467 int ValueForTypeID; // The value to write - may not be equal to the type id.
3468 int NextAction;
3469 struct ActionEntry *Previous;
3470 };
3471
Duncan Sands241a0c92007-09-05 11:27:52 +00003472 /// PadRange - Structure holding a try-range and the associated landing pad.
3473 struct PadRange {
3474 // The index of the landing pad.
3475 unsigned PadIndex;
3476 // The index of the begin and end labels in the landing pad's label lists.
3477 unsigned RangeIndex;
3478 };
3479
3480 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3481
3482 /// CallSiteEntry - Structure describing an entry in the call-site table.
3483 struct CallSiteEntry {
Duncan Sands4ff179f2007-12-19 07:36:31 +00003484 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003485 unsigned BeginLabel; // zero indicates the start of the function.
3486 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003487 // The landing pad starts at PadLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003488 unsigned PadLabel; // zero indicates that there is no landing pad.
3489 unsigned Action;
3490 };
3491
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003492 void EmitExceptionTable() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003493 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
3494 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
3495 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3496 if (PadInfos.empty()) return;
3497
3498 // Sort the landing pads in order of their type ids. This is used to fold
3499 // duplicate actions.
3500 SmallVector<const LandingPadInfo *, 64> LandingPads;
3501 LandingPads.reserve(PadInfos.size());
3502 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
3503 LandingPads.push_back(&PadInfos[i]);
3504 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3505
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003506 // Negative type ids index into FilterIds, positive type ids index into
3507 // TypeInfos. The value written for a positive type id is just the type
3508 // id itself. For a negative type id, however, the value written is the
3509 // (negative) byte offset of the corresponding FilterIds entry. The byte
3510 // offset is usually equal to the type id, because the FilterIds entries
3511 // are written using a variable width encoding which outputs one byte per
3512 // entry as long as the value written is not too large, but can differ.
3513 // This kind of complication does not occur for positive type ids because
3514 // type infos are output using a fixed width encoding.
3515 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3516 SmallVector<int, 16> FilterOffsets;
3517 FilterOffsets.reserve(FilterIds.size());
3518 int Offset = -1;
3519 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3520 E = FilterIds.end(); I != E; ++I) {
3521 FilterOffsets.push_back(Offset);
aslc200b112008-08-16 12:57:46 +00003522 Offset -= TargetAsmInfo::getULEB128Size(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003523 }
3524
Duncan Sands241a0c92007-09-05 11:27:52 +00003525 // Compute the actions table and gather the first action index for each
3526 // landing pad site.
3527 SmallVector<ActionEntry, 32> Actions;
3528 SmallVector<unsigned, 64> FirstActions;
3529 FirstActions.reserve(LandingPads.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003530
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003531 int FirstAction = 0;
Duncan Sands241a0c92007-09-05 11:27:52 +00003532 unsigned SizeActions = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003533 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3534 const LandingPadInfo *LP = LandingPads[i];
3535 const std::vector<int> &TypeIds = LP->TypeIds;
3536 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
3537 unsigned SizeSiteActions = 0;
3538
3539 if (NumShared < TypeIds.size()) {
3540 unsigned SizeAction = 0;
3541 ActionEntry *PrevAction = 0;
3542
3543 if (NumShared) {
3544 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3545 assert(Actions.size());
3546 PrevAction = &Actions.back();
aslc200b112008-08-16 12:57:46 +00003547 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3548 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003549 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
aslc200b112008-08-16 12:57:46 +00003550 SizeAction -=
3551 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003552 SizeAction += -PrevAction->NextAction;
3553 PrevAction = PrevAction->Previous;
3554 }
3555 }
3556
3557 // Compute the actions.
3558 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3559 int TypeID = TypeIds[I];
3560 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3561 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
aslc200b112008-08-16 12:57:46 +00003562 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003563
3564 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
aslc200b112008-08-16 12:57:46 +00003565 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003566 SizeSiteActions += SizeAction;
3567
3568 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
3569 Actions.push_back(Action);
3570
3571 PrevAction = &Actions.back();
3572 }
3573
3574 // Record the first action of the landing pad site.
3575 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3576 } // else identical - re-use previous FirstAction
3577
3578 FirstActions.push_back(FirstAction);
3579
3580 // Compute this sites contribution to size.
3581 SizeActions += SizeSiteActions;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003582 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003583
Duncan Sands4ff179f2007-12-19 07:36:31 +00003584 // Compute the call-site table. The entry for an invoke has a try-range
3585 // containing the call, a non-zero landing pad and an appropriate action.
3586 // The entry for an ordinary call has a try-range containing the call and
3587 // zero for the landing pad and the action. Calls marked 'nounwind' have
3588 // no entry and must not be contained in the try-range of any entry - they
3589 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands241a0c92007-09-05 11:27:52 +00003590 SmallVector<CallSiteEntry, 64> CallSites;
3591
3592 RangeMapType PadMap;
Duncan Sands4ff179f2007-12-19 07:36:31 +00003593 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3594 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3595 // try-ranges for them need be deduced.
Duncan Sands241a0c92007-09-05 11:27:52 +00003596 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3597 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003598 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003599 unsigned BeginLabel = LandingPad->BeginLabels[j];
3600 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3601 PadRange P = { i, j };
3602 PadMap[BeginLabel] = P;
3603 }
3604 }
3605
Duncan Sands4ff179f2007-12-19 07:36:31 +00003606 // The end label of the previous invoke or nounwind try-range.
Duncan Sands241a0c92007-09-05 11:27:52 +00003607 unsigned LastLabel = 0;
Duncan Sands4ff179f2007-12-19 07:36:31 +00003608
3609 // Whether there is a potentially throwing instruction (currently this means
3610 // an ordinary call) between the end of the previous try-range and now.
3611 bool SawPotentiallyThrowing = false;
3612
3613 // Whether the last callsite entry was for an invoke.
3614 bool PreviousIsInvoke = false;
3615
Duncan Sands4ff179f2007-12-19 07:36:31 +00003616 // Visit all instructions in order of address.
Duncan Sands241a0c92007-09-05 11:27:52 +00003617 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3618 I != E; ++I) {
3619 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3620 MI != E; ++MI) {
Dan Gohmanfa607c92008-07-01 00:05:16 +00003621 if (!MI->isLabel()) {
Chris Lattner5b930372008-01-07 07:27:27 +00003622 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands241a0c92007-09-05 11:27:52 +00003623 continue;
3624 }
3625
Chris Lattnerda4cff12007-12-30 20:50:28 +00003626 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands241a0c92007-09-05 11:27:52 +00003627 assert(BeginLabel && "Invalid label!");
Duncan Sands89372f62007-09-05 14:12:46 +00003628
Duncan Sands4ff179f2007-12-19 07:36:31 +00003629 // End of the previous try-range?
Duncan Sands89372f62007-09-05 14:12:46 +00003630 if (BeginLabel == LastLabel)
Duncan Sands4ff179f2007-12-19 07:36:31 +00003631 SawPotentiallyThrowing = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003632
Duncan Sands4ff179f2007-12-19 07:36:31 +00003633 // Beginning of a new try-range?
Duncan Sands241a0c92007-09-05 11:27:52 +00003634 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands241a0c92007-09-05 11:27:52 +00003635 if (L == PadMap.end())
Duncan Sands4ff179f2007-12-19 07:36:31 +00003636 // Nope, it was just some random label.
Duncan Sands241a0c92007-09-05 11:27:52 +00003637 continue;
3638
3639 PadRange P = L->second;
3640 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3641
3642 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3643 "Inconsistent landing pad map!");
3644
3645 // If some instruction between the previous try-range and this one may
3646 // throw, create a call-site entry with no landing pad for the region
3647 // between the try-ranges.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003648 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003649 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3650 CallSites.push_back(Site);
Duncan Sands4ff179f2007-12-19 07:36:31 +00003651 PreviousIsInvoke = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003652 }
3653
3654 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003655 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands241a0c92007-09-05 11:27:52 +00003656
Duncan Sands4ff179f2007-12-19 07:36:31 +00003657 if (LandingPad->LandingPadLabel) {
3658 // This try-range is for an invoke.
3659 CallSiteEntry Site = {BeginLabel, LastLabel,
3660 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands241a0c92007-09-05 11:27:52 +00003661
Duncan Sands4ff179f2007-12-19 07:36:31 +00003662 // Try to merge with the previous call-site.
3663 if (PreviousIsInvoke) {
Dan Gohman3d436002008-06-21 22:00:54 +00003664 CallSiteEntry &Prev = CallSites.back();
Duncan Sands4ff179f2007-12-19 07:36:31 +00003665 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3666 // Extend the range of the previous entry.
3667 Prev.EndLabel = Site.EndLabel;
3668 continue;
3669 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003670 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003671
Duncan Sands4ff179f2007-12-19 07:36:31 +00003672 // Otherwise, create a new call-site.
3673 CallSites.push_back(Site);
3674 PreviousIsInvoke = true;
3675 } else {
3676 // Create a gap.
3677 PreviousIsInvoke = false;
3678 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003679 }
3680 }
3681 // If some instruction between the previous try-range and the end of the
3682 // function may throw, create a call-site entry with no landing pad for the
3683 // region following the try-range.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003684 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003685 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3686 CallSites.push_back(Site);
3687 }
3688
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003689 // Final tallies.
Duncan Sands96144f92008-05-07 19:11:09 +00003690
3691 // Call sites.
3692 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
3693 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
3694 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
3695 unsigned SizeSites = CallSites.size() * (SiteStartSize +
3696 SiteLengthSize +
3697 LandingPadSize);
Duncan Sands241a0c92007-09-05 11:27:52 +00003698 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
aslc200b112008-08-16 12:57:46 +00003699 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands241a0c92007-09-05 11:27:52 +00003700
Duncan Sands96144f92008-05-07 19:11:09 +00003701 // Type infos.
3702 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3703 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003704
3705 unsigned TypeOffset = sizeof(int8_t) + // Call site format
aslc200b112008-08-16 12:57:46 +00003706 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003707 SizeSites + SizeActions + SizeTypes;
3708
3709 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3710 sizeof(int8_t) + // TType format
aslc200b112008-08-16 12:57:46 +00003711 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003712 TypeOffset;
3713
3714 unsigned SizeAlign = (4 - TotalSize) & 3;
3715
3716 // Begin the exception table.
3717 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng7e7d1942008-02-29 19:36:59 +00003718 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesen841e4982008-10-08 21:50:21 +00003719 O << "GCC_except_table" << SubprogramCount << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003720 for (unsigned i = 0; i != SizeAlign; ++i) {
3721 Asm->EmitInt8(0);
3722 Asm->EOL("Padding");
3723 }
3724 EmitLabel("exception", SubprogramCount);
3725
3726 // Emit the header.
3727 Asm->EmitInt8(DW_EH_PE_omit);
3728 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3729 Asm->EmitInt8(DW_EH_PE_absptr);
3730 Asm->EOL("TType format (DW_EH_PE_absptr)");
3731 Asm->EmitULEB128Bytes(TypeOffset);
3732 Asm->EOL("TType base offset");
3733 Asm->EmitInt8(DW_EH_PE_udata4);
3734 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3735 Asm->EmitULEB128Bytes(SizeSites);
3736 Asm->EOL("Call-site table length");
3737
Duncan Sands241a0c92007-09-05 11:27:52 +00003738 // Emit the landing pad site information.
3739 for (unsigned i = 0; i < CallSites.size(); ++i) {
3740 CallSiteEntry &S = CallSites[i];
3741 const char *BeginTag;
3742 unsigned BeginNumber;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003743
Duncan Sands241a0c92007-09-05 11:27:52 +00003744 if (!S.BeginLabel) {
3745 BeginTag = "eh_func_begin";
3746 BeginNumber = SubprogramCount;
3747 } else {
3748 BeginTag = "label";
3749 BeginNumber = S.BeginLabel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003750 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003751
Duncan Sands241a0c92007-09-05 11:27:52 +00003752 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00003753 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003754 Asm->EOL("Region start");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003755
Duncan Sands241a0c92007-09-05 11:27:52 +00003756 if (!S.EndLabel) {
Dale Johannesen4670be42008-01-15 23:24:56 +00003757 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands96144f92008-05-07 19:11:09 +00003758 true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003759 } else {
Duncan Sands96144f92008-05-07 19:11:09 +00003760 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003761 }
3762 Asm->EOL("Region length");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003763
Duncan Sands96144f92008-05-07 19:11:09 +00003764 if (!S.PadLabel)
3765 Asm->EmitInt32(0);
3766 else
Duncan Sands241a0c92007-09-05 11:27:52 +00003767 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00003768 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003769 Asm->EOL("Landing pad");
3770
3771 Asm->EmitULEB128Bytes(S.Action);
3772 Asm->EOL("Action");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003773 }
3774
3775 // Emit the actions.
3776 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
3777 ActionEntry &Action = Actions[I];
3778
3779 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
3780 Asm->EOL("TypeInfo index");
3781 Asm->EmitSLEB128Bytes(Action.NextAction);
3782 Asm->EOL("Next action");
3783 }
3784
3785 // Emit the type ids.
3786 for (unsigned M = TypeInfos.size(); M; --M) {
3787 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00003788
3789 PrintRelDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003790
3791 if (GV)
3792 O << Asm->getGlobalLinkName(GV);
3793 else
3794 O << "0";
Duncan Sands241a0c92007-09-05 11:27:52 +00003795
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003796 Asm->EOL("TypeInfo");
3797 }
3798
3799 // Emit the filter typeids.
3800 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
3801 unsigned TypeID = FilterIds[j];
3802 Asm->EmitULEB128Bytes(TypeID);
3803 Asm->EOL("Filter TypeInfo index");
3804 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003805
Evan Cheng7e7d1942008-02-29 19:36:59 +00003806 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003807 }
3808
3809public:
3810 //===--------------------------------------------------------------------===//
3811 // Main entry points.
3812 //
Owen Anderson847b99b2008-08-21 00:14:44 +00003813 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattnerb3876c72007-09-24 03:35:37 +00003814 : Dwarf(OS, A, T, "eh")
Dale Johannesen85535762008-04-02 00:25:04 +00003815 , shouldEmitTable(false)
3816 , shouldEmitMoves(false)
3817 , shouldEmitTableModule(false)
3818 , shouldEmitMovesModule(false)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003819 {}
aslc200b112008-08-16 12:57:46 +00003820
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003821 virtual ~DwarfException() {}
3822
3823 /// SetModuleInfo - Set machine module information when it's known that pass
3824 /// manager has created it. Set by the target AsmPrinter.
3825 void SetModuleInfo(MachineModuleInfo *mmi) {
3826 MMI = mmi;
3827 }
3828
3829 /// BeginModule - Emit all exception information that should come prior to the
3830 /// content.
3831 void BeginModule(Module *M) {
3832 this->M = M;
3833 }
3834
3835 /// EndModule - Emit all exception information that should come after the
3836 /// content.
3837 void EndModule() {
Dale Johannesen85535762008-04-02 00:25:04 +00003838 if (shouldEmitMovesModule || shouldEmitTableModule) {
3839 const std::vector<Function *> Personalities = MMI->getPersonalities();
3840 for (unsigned i =0; i < Personalities.size(); ++i)
3841 EmitCommonEHFrame(Personalities[i], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003842
Dale Johannesen85535762008-04-02 00:25:04 +00003843 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
3844 E = EHFrames.end(); I != E; ++I)
3845 EmitEHFrame(*I);
3846 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003847 }
3848
aslc200b112008-08-16 12:57:46 +00003849 /// BeginFunction - Gather pre-function exception information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003850 /// emitted immediately after the function entry point.
3851 void BeginFunction(MachineFunction *MF) {
3852 this->MF = MF;
Dale Johannesen85535762008-04-02 00:25:04 +00003853 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen62f0a6d2008-04-02 17:04:45 +00003854 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen85535762008-04-02 00:25:04 +00003855
3856 // Map all labels and get rid of any dead landing pads.
3857 MMI->TidyLandingPads();
3858 // If any landing pads survive, we need an EH table.
3859 if (MMI->getLandingPads().size())
3860 shouldEmitTable = true;
3861
3862 // See if we need frame move info.
Duncan Sandscbc28b12008-07-04 09:55:48 +00003863 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen85535762008-04-02 00:25:04 +00003864 shouldEmitMoves = true;
3865
3866 if (shouldEmitMoves || shouldEmitTable)
3867 // Assumes in correct section after the entry point.
3868 EmitLabel("eh_func_begin", ++SubprogramCount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003869 }
Dale Johannesen85535762008-04-02 00:25:04 +00003870 shouldEmitTableModule |= shouldEmitTable;
3871 shouldEmitMovesModule |= shouldEmitMoves;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003872 }
3873
3874 /// EndFunction - Gather and emit post-function exception information.
3875 ///
3876 void EndFunction() {
Dale Johannesen85535762008-04-02 00:25:04 +00003877 if (shouldEmitMoves || shouldEmitTable) {
3878 EmitLabel("eh_func_end", SubprogramCount);
3879 EmitExceptionTable();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003880
Dale Johannesen85535762008-04-02 00:25:04 +00003881 // Save EH frame information
3882 EHFrames.
3883 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendlingef9211a2007-09-18 01:47:22 +00003884 SubprogramCount,
3885 MMI->getPersonalityIndex(),
3886 MF->getFrameInfo()->hasCalls(),
3887 !MMI->getLandingPads().empty(),
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003888 MMI->getFrameMoves(),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003889 MF->getFunction()));
Dale Johannesen85535762008-04-02 00:25:04 +00003890 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003891 }
3892};
3893
3894} // End of namespace llvm
3895
3896//===----------------------------------------------------------------------===//
3897
3898/// Emit - Print the abbreviation using the specified Dwarf writer.
3899///
3900void DIEAbbrev::Emit(const DwarfDebug &DD) const {
3901 // Emit its Dwarf tag type.
3902 DD.getAsm()->EmitULEB128Bytes(Tag);
3903 DD.getAsm()->EOL(TagString(Tag));
aslc200b112008-08-16 12:57:46 +00003904
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003905 // Emit whether it has children DIEs.
3906 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
3907 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
aslc200b112008-08-16 12:57:46 +00003908
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003909 // For each attribute description.
3910 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3911 const DIEAbbrevData &AttrData = Data[i];
aslc200b112008-08-16 12:57:46 +00003912
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003913 // Emit attribute type.
3914 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
3915 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
aslc200b112008-08-16 12:57:46 +00003916
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003917 // Emit form type.
3918 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
3919 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
3920 }
3921
3922 // Mark end of abbreviation.
3923 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
3924 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
3925}
3926
3927#ifndef NDEBUG
3928void DIEAbbrev::print(std::ostream &O) {
3929 O << "Abbreviation @"
3930 << std::hex << (intptr_t)this << std::dec
3931 << " "
3932 << TagString(Tag)
3933 << " "
3934 << ChildrenString(ChildrenFlag)
3935 << "\n";
aslc200b112008-08-16 12:57:46 +00003936
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003937 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3938 O << " "
3939 << AttributeString(Data[i].getAttribute())
3940 << " "
3941 << FormEncodingString(Data[i].getForm())
3942 << "\n";
3943 }
3944}
3945void DIEAbbrev::dump() { print(cerr); }
3946#endif
3947
3948//===----------------------------------------------------------------------===//
3949
3950#ifndef NDEBUG
3951void DIEValue::dump() {
3952 print(cerr);
3953}
3954#endif
3955
3956//===----------------------------------------------------------------------===//
3957
3958/// EmitValue - Emit integer of appropriate size.
3959///
3960void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
3961 switch (Form) {
3962 case DW_FORM_flag: // Fall thru
3963 case DW_FORM_ref1: // Fall thru
3964 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
3965 case DW_FORM_ref2: // Fall thru
3966 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
3967 case DW_FORM_ref4: // Fall thru
3968 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
3969 case DW_FORM_ref8: // Fall thru
3970 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
3971 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
3972 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
3973 default: assert(0 && "DIE Value form not supported yet"); break;
3974 }
3975}
3976
3977/// SizeOf - Determine size of integer value in bytes.
3978///
3979unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
3980 switch (Form) {
3981 case DW_FORM_flag: // Fall thru
3982 case DW_FORM_ref1: // Fall thru
3983 case DW_FORM_data1: return sizeof(int8_t);
3984 case DW_FORM_ref2: // Fall thru
3985 case DW_FORM_data2: return sizeof(int16_t);
3986 case DW_FORM_ref4: // Fall thru
3987 case DW_FORM_data4: return sizeof(int32_t);
3988 case DW_FORM_ref8: // Fall thru
3989 case DW_FORM_data8: return sizeof(int64_t);
aslc200b112008-08-16 12:57:46 +00003990 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
3991 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003992 default: assert(0 && "DIE Value form not supported yet"); break;
3993 }
3994 return 0;
3995}
3996
3997//===----------------------------------------------------------------------===//
3998
3999/// EmitValue - Emit string value.
4000///
4001void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
4002 DD.getAsm()->EmitString(String);
4003}
4004
4005//===----------------------------------------------------------------------===//
4006
4007/// EmitValue - Emit label value.
4008///
4009void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004010 bool IsSmall = Form == DW_FORM_data4;
4011 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004012}
4013
4014/// SizeOf - Determine size of label value in bytes.
4015///
4016unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004017 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004018 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004019}
4020
4021//===----------------------------------------------------------------------===//
4022
4023/// EmitValue - Emit label value.
4024///
4025void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004026 bool IsSmall = Form == DW_FORM_data4;
4027 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004028}
4029
4030/// SizeOf - Determine size of label value in bytes.
4031///
4032unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004033 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004034 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004035}
aslc200b112008-08-16 12:57:46 +00004036
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004037//===----------------------------------------------------------------------===//
4038
4039/// EmitValue - Emit delta value.
4040///
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004041void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4042 bool IsSmall = Form == DW_FORM_data4;
4043 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4044 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4045}
4046
4047/// SizeOf - Determine size of delta value in bytes.
4048///
4049unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4050 if (Form == DW_FORM_data4) return 4;
4051 return DD.getTargetData()->getPointerSize();
4052}
aslc200b112008-08-16 12:57:46 +00004053
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004054//===----------------------------------------------------------------------===//
4055
4056/// EmitValue - Emit delta value.
4057///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004058void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
4059 bool IsSmall = Form == DW_FORM_data4;
4060 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
4061}
4062
4063/// SizeOf - Determine size of delta value in bytes.
4064///
4065unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4066 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004067 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004068}
4069
4070//===----------------------------------------------------------------------===//
4071
4072/// EmitValue - Emit debug information entry offset.
4073///
4074void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
4075 DD.getAsm()->EmitInt32(Entry->getOffset());
4076}
aslc200b112008-08-16 12:57:46 +00004077
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004078//===----------------------------------------------------------------------===//
4079
4080/// ComputeSize - calculate the size of the block.
4081///
4082unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
4083 if (!Size) {
Owen Anderson88dd6232008-06-24 21:44:59 +00004084 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004085
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004086 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4087 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
4088 }
4089 }
4090 return Size;
4091}
4092
4093/// EmitValue - Emit block data.
4094///
4095void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
4096 switch (Form) {
4097 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4098 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4099 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4100 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
4101 default: assert(0 && "Improper form for block"); break;
4102 }
aslc200b112008-08-16 12:57:46 +00004103
Owen Anderson88dd6232008-06-24 21:44:59 +00004104 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004105
4106 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4107 DD.getAsm()->EOL();
4108 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
4109 }
4110}
4111
4112/// SizeOf - Determine size of block data in bytes.
4113///
4114unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4115 switch (Form) {
4116 case DW_FORM_block1: return Size + sizeof(int8_t);
4117 case DW_FORM_block2: return Size + sizeof(int16_t);
4118 case DW_FORM_block4: return Size + sizeof(int32_t);
aslc200b112008-08-16 12:57:46 +00004119 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004120 default: assert(0 && "Improper form for block"); break;
4121 }
4122 return 0;
4123}
4124
4125//===----------------------------------------------------------------------===//
4126/// DIE Implementation
4127
4128DIE::~DIE() {
4129 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4130 delete Children[i];
4131}
aslc200b112008-08-16 12:57:46 +00004132
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004133/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4134///
4135void DIE::AddSiblingOffset() {
4136 DIEInteger *DI = new DIEInteger(0);
4137 Values.insert(Values.begin(), DI);
4138 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
4139}
4140
4141/// Profile - Used to gather unique data for the value folding set.
4142///
4143void DIE::Profile(FoldingSetNodeID &ID) {
4144 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00004145
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004146 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4147 ID.AddPointer(Children[i]);
4148
4149 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4150 ID.AddPointer(Values[j]);
4151}
4152
4153#ifndef NDEBUG
4154void DIE::print(std::ostream &O, unsigned IncIndent) {
4155 static unsigned IndentCount = 0;
4156 IndentCount += IncIndent;
4157 const std::string Indent(IndentCount, ' ');
4158 bool isBlock = Abbrev.getTag() == 0;
aslc200b112008-08-16 12:57:46 +00004159
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004160 if (!isBlock) {
4161 O << Indent
4162 << "Die: "
4163 << "0x" << std::hex << (intptr_t)this << std::dec
4164 << ", Offset: " << Offset
4165 << ", Size: " << Size
aslc200b112008-08-16 12:57:46 +00004166 << "\n";
4167
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004168 O << Indent
4169 << TagString(Abbrev.getTag())
4170 << " "
4171 << ChildrenString(Abbrev.getChildrenFlag());
4172 } else {
4173 O << "Size: " << Size;
4174 }
4175 O << "\n";
4176
Owen Anderson88dd6232008-06-24 21:44:59 +00004177 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004178
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004179 IndentCount += 2;
4180 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4181 O << Indent;
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004182
4183 if (!isBlock)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004184 O << AttributeString(Data[i].getAttribute());
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004185 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004186 O << "Blk[" << i << "]";
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004187
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004188 O << " "
4189 << FormEncodingString(Data[i].getForm())
4190 << " ";
4191 Values[i]->print(O);
4192 O << "\n";
4193 }
4194 IndentCount -= 2;
4195
4196 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4197 Children[j]->print(O, 4);
4198 }
aslc200b112008-08-16 12:57:46 +00004199
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004200 if (!isBlock) O << "\n";
4201 IndentCount -= IncIndent;
4202}
4203
4204void DIE::dump() {
4205 print(cerr);
4206}
4207#endif
4208
4209//===----------------------------------------------------------------------===//
4210/// DwarfWriter Implementation
4211///
4212
Owen Anderson847b99b2008-08-21 00:14:44 +00004213DwarfWriter::DwarfWriter(raw_ostream &OS, AsmPrinter *A,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004214 const TargetAsmInfo *T) {
4215 DE = new DwarfException(OS, A, T);
4216 DD = new DwarfDebug(OS, A, T);
4217}
4218
4219DwarfWriter::~DwarfWriter() {
4220 delete DE;
4221 delete DD;
4222}
4223
4224/// SetModuleInfo - Set machine module info when it's known that pass manager
4225/// has created it. Set by the target AsmPrinter.
4226void DwarfWriter::SetModuleInfo(MachineModuleInfo *MMI) {
4227 DD->SetModuleInfo(MMI);
4228 DE->SetModuleInfo(MMI);
4229}
4230
4231/// BeginModule - Emit all Dwarf sections that should come prior to the
4232/// content.
4233void DwarfWriter::BeginModule(Module *M) {
4234 DE->BeginModule(M);
4235 DD->BeginModule(M);
4236}
4237
4238/// EndModule - Emit all Dwarf sections that should come after the content.
4239///
4240void DwarfWriter::EndModule() {
4241 DE->EndModule();
4242 DD->EndModule();
4243}
4244
aslc200b112008-08-16 12:57:46 +00004245/// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004246/// emitted immediately after the function entry point.
4247void DwarfWriter::BeginFunction(MachineFunction *MF) {
4248 DE->BeginFunction(MF);
4249 DD->BeginFunction(MF);
4250}
4251
4252/// EndFunction - Gather and emit post-function debug information.
4253///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00004254void DwarfWriter::EndFunction(MachineFunction *MF) {
4255 DD->EndFunction(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004256 DE->EndFunction();
aslc200b112008-08-16 12:57:46 +00004257
Bill Wendling5b4796a2008-07-22 00:53:37 +00004258 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004259 // Clear function debug information.
4260 MMI->EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004261}