blob: 9ca9e28010a482bf02dbf30862550e3370f2856e [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"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/Dwarf.h"
28#include "llvm/Support/CommandLine.h"
29#include "llvm/Support/DataTypes.h"
30#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000031#include "llvm/Support/raw_ostream.h"
Dan Gohman80bbde72007-09-24 21:32:18 +000032#include "llvm/System/Path.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000034#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/Target/TargetData.h"
36#include "llvm/Target/TargetFrameInfo.h"
37#include "llvm/Target/TargetInstrInfo.h"
38#include "llvm/Target/TargetMachine.h"
39#include "llvm/Target/TargetOptions.h"
40#include <ostream>
41#include <string>
42using namespace llvm;
43using namespace llvm::dwarf;
44
45namespace llvm {
aslc200b112008-08-16 12:57:46 +000046
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047//===----------------------------------------------------------------------===//
48
49/// Configuration values for initial hash set sizes (log2).
50///
51static const unsigned InitDiesSetSize = 9; // 512
52static const unsigned InitAbbreviationsSetSize = 9; // 512
53static const unsigned InitValuesSetSize = 9; // 512
54
55//===----------------------------------------------------------------------===//
56/// Forward declarations.
57///
58class DIE;
59class DIEValue;
60
61//===----------------------------------------------------------------------===//
62/// DWLabel - Labels are used to track locations in the assembler file.
aslc200b112008-08-16 12:57:46 +000063/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
64/// where the tag is a category of label (Ex. location) and number is a value
Reid Spencer37c7cea2007-08-05 20:06:04 +000065/// unique in that category.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066class DWLabel {
67public:
68 /// Tag - Label category tag. Should always be a staticly declared C string.
69 ///
70 const char *Tag;
aslc200b112008-08-16 12:57:46 +000071
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 /// Number - Value to make label unique.
73 ///
74 unsigned Number;
75
76 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
aslc200b112008-08-16 12:57:46 +000077
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 void Profile(FoldingSetNodeID &ID) const {
79 ID.AddString(std::string(Tag));
80 ID.AddInteger(Number);
81 }
aslc200b112008-08-16 12:57:46 +000082
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083#ifndef NDEBUG
84 void print(std::ostream *O) const {
85 if (O) print(*O);
86 }
87 void print(std::ostream &O) const {
88 O << "." << Tag;
89 if (Number) O << Number;
90 }
91#endif
92};
93
94//===----------------------------------------------------------------------===//
95/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
96/// Dwarf abbreviation.
97class DIEAbbrevData {
98private:
99 /// Attribute - Dwarf attribute code.
100 ///
101 unsigned Attribute;
aslc200b112008-08-16 12:57:46 +0000102
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 /// Form - Dwarf form code.
aslc200b112008-08-16 12:57:46 +0000104 ///
105 unsigned Form;
106
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107public:
108 DIEAbbrevData(unsigned A, unsigned F)
109 : Attribute(A)
110 , Form(F)
111 {}
aslc200b112008-08-16 12:57:46 +0000112
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 // Accessors.
114 unsigned getAttribute() const { return Attribute; }
115 unsigned getForm() const { return Form; }
116
117 /// Profile - Used to gather unique data for the abbreviation folding set.
118 ///
119 void Profile(FoldingSetNodeID &ID)const {
120 ID.AddInteger(Attribute);
121 ID.AddInteger(Form);
122 }
123};
124
125//===----------------------------------------------------------------------===//
126/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
127/// information object.
128class DIEAbbrev : public FoldingSetNode {
129private:
130 /// Tag - Dwarf tag code.
131 ///
132 unsigned Tag;
aslc200b112008-08-16 12:57:46 +0000133
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 /// Unique number for node.
135 ///
136 unsigned Number;
137
138 /// ChildrenFlag - Dwarf children flag.
139 ///
140 unsigned ChildrenFlag;
141
142 /// Data - Raw data bytes for abbreviation.
143 ///
Owen Anderson88dd6232008-06-24 21:44:59 +0000144 SmallVector<DIEAbbrevData, 8> Data;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145
146public:
147
148 DIEAbbrev(unsigned T, unsigned C)
149 : Tag(T)
150 , ChildrenFlag(C)
151 , Data()
152 {}
153 ~DIEAbbrev() {}
aslc200b112008-08-16 12:57:46 +0000154
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 // Accessors.
156 unsigned getTag() const { return Tag; }
157 unsigned getNumber() const { return Number; }
158 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson88dd6232008-06-24 21:44:59 +0000159 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 void setTag(unsigned T) { Tag = T; }
161 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
162 void setNumber(unsigned N) { Number = N; }
aslc200b112008-08-16 12:57:46 +0000163
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 /// AddAttribute - Adds another set of attribute information to the
165 /// abbreviation.
166 void AddAttribute(unsigned Attribute, unsigned Form) {
167 Data.push_back(DIEAbbrevData(Attribute, Form));
168 }
aslc200b112008-08-16 12:57:46 +0000169
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 /// AddFirstAttribute - Adds a set of attribute information to the front
171 /// of the abbreviation.
172 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
173 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
174 }
aslc200b112008-08-16 12:57:46 +0000175
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 /// Profile - Used to gather unique data for the abbreviation folding set.
177 ///
178 void Profile(FoldingSetNodeID &ID) {
179 ID.AddInteger(Tag);
180 ID.AddInteger(ChildrenFlag);
aslc200b112008-08-16 12:57:46 +0000181
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 // For each attribute description.
183 for (unsigned i = 0, N = Data.size(); i < N; ++i)
184 Data[i].Profile(ID);
185 }
aslc200b112008-08-16 12:57:46 +0000186
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 /// Emit - Print the abbreviation using the specified Dwarf writer.
188 ///
aslc200b112008-08-16 12:57:46 +0000189 void Emit(const DwarfDebug &DD) const;
190
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191#ifndef NDEBUG
192 void print(std::ostream *O) {
193 if (O) print(*O);
194 }
195 void print(std::ostream &O);
196 void dump();
197#endif
198};
199
200//===----------------------------------------------------------------------===//
201/// DIE - A structured debug information entry. Has an abbreviation which
202/// describes it's organization.
203class DIE : public FoldingSetNode {
204protected:
205 /// Abbrev - Buffer for constructing abbreviation.
206 ///
207 DIEAbbrev Abbrev;
aslc200b112008-08-16 12:57:46 +0000208
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 /// Offset - Offset in debug info section.
210 ///
211 unsigned Offset;
aslc200b112008-08-16 12:57:46 +0000212
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 /// Size - Size of instance + children.
214 ///
215 unsigned Size;
aslc200b112008-08-16 12:57:46 +0000216
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 /// Children DIEs.
218 ///
219 std::vector<DIE *> Children;
aslc200b112008-08-16 12:57:46 +0000220
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 /// Attributes values.
222 ///
Owen Anderson88dd6232008-06-24 21:44:59 +0000223 SmallVector<DIEValue*, 32> Values;
aslc200b112008-08-16 12:57:46 +0000224
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000226 explicit DIE(unsigned Tag)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 : Abbrev(Tag, DW_CHILDREN_no)
228 , Offset(0)
229 , Size(0)
230 , Children()
231 , Values()
232 {}
233 virtual ~DIE();
aslc200b112008-08-16 12:57:46 +0000234
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 // Accessors.
236 DIEAbbrev &getAbbrev() { return Abbrev; }
237 unsigned getAbbrevNumber() const {
238 return Abbrev.getNumber();
239 }
240 unsigned getTag() const { return Abbrev.getTag(); }
241 unsigned getOffset() const { return Offset; }
242 unsigned getSize() const { return Size; }
243 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson88dd6232008-06-24 21:44:59 +0000244 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
246 void setOffset(unsigned O) { Offset = O; }
247 void setSize(unsigned S) { Size = S; }
aslc200b112008-08-16 12:57:46 +0000248
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 /// AddValue - Add a value and attributes to a DIE.
250 ///
251 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
252 Abbrev.AddAttribute(Attribute, Form);
253 Values.push_back(Value);
254 }
aslc200b112008-08-16 12:57:46 +0000255
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 /// SiblingOffset - Return the offset of the debug information entry's
257 /// sibling.
258 unsigned SiblingOffset() const { return Offset + Size; }
aslc200b112008-08-16 12:57:46 +0000259
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
261 ///
262 void AddSiblingOffset();
263
264 /// AddChild - Add a child to the DIE.
265 ///
266 void AddChild(DIE *Child) {
267 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
268 Children.push_back(Child);
269 }
aslc200b112008-08-16 12:57:46 +0000270
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 /// Detach - Detaches objects connected to it after copying.
272 ///
273 void Detach() {
274 Children.clear();
275 }
aslc200b112008-08-16 12:57:46 +0000276
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 /// Profile - Used to gather unique data for the value folding set.
278 ///
279 void Profile(FoldingSetNodeID &ID) ;
aslc200b112008-08-16 12:57:46 +0000280
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281#ifndef NDEBUG
282 void print(std::ostream *O, unsigned IncIndent = 0) {
283 if (O) print(*O, IncIndent);
284 }
285 void print(std::ostream &O, unsigned IncIndent = 0);
286 void dump();
287#endif
288};
289
290//===----------------------------------------------------------------------===//
291/// DIEValue - A debug information entry value.
292///
293class DIEValue : public FoldingSetNode {
294public:
295 enum {
296 isInteger,
297 isString,
298 isLabel,
299 isAsIsLabel,
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000300 isSectionOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 isDelta,
302 isEntry,
303 isBlock
304 };
aslc200b112008-08-16 12:57:46 +0000305
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306 /// Type - Type of data stored in the value.
307 ///
308 unsigned Type;
aslc200b112008-08-16 12:57:46 +0000309
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000310 explicit DIEValue(unsigned T)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311 : Type(T)
312 {}
313 virtual ~DIEValue() {}
aslc200b112008-08-16 12:57:46 +0000314
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 // Accessors
316 unsigned getType() const { return Type; }
aslc200b112008-08-16 12:57:46 +0000317
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 // Implement isa/cast/dyncast.
319 static bool classof(const DIEValue *) { return true; }
aslc200b112008-08-16 12:57:46 +0000320
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321 /// EmitValue - Emit value via the Dwarf writer.
322 ///
323 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
aslc200b112008-08-16 12:57:46 +0000324
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325 /// SizeOf - Return the size of a value in bytes.
326 ///
327 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
aslc200b112008-08-16 12:57:46 +0000328
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329 /// Profile - Used to gather unique data for the value folding set.
330 ///
331 virtual void Profile(FoldingSetNodeID &ID) = 0;
aslc200b112008-08-16 12:57:46 +0000332
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333#ifndef NDEBUG
334 void print(std::ostream *O) {
335 if (O) print(*O);
336 }
337 virtual void print(std::ostream &O) = 0;
338 void dump();
339#endif
340};
341
342//===----------------------------------------------------------------------===//
343/// DWInteger - An integer value DIE.
aslc200b112008-08-16 12:57:46 +0000344///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345class DIEInteger : public DIEValue {
346private:
347 uint64_t Integer;
aslc200b112008-08-16 12:57:46 +0000348
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000350 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351
352 // Implement isa/cast/dyncast.
353 static bool classof(const DIEInteger *) { return true; }
354 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
aslc200b112008-08-16 12:57:46 +0000355
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356 /// BestForm - Choose the best form for integer.
357 ///
358 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
359 if (IsSigned) {
360 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
361 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
362 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
363 } else {
364 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
365 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
366 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
367 }
368 return DW_FORM_data8;
369 }
aslc200b112008-08-16 12:57:46 +0000370
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371 /// EmitValue - Emit integer of appropriate size.
372 ///
373 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000374
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 /// SizeOf - Determine size of integer value in bytes.
376 ///
377 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000378
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379 /// Profile - Used to gather unique data for the value folding set.
380 ///
381 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
382 ID.AddInteger(isInteger);
383 ID.AddInteger(Integer);
384 }
385 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
aslc200b112008-08-16 12:57:46 +0000386
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387#ifndef NDEBUG
388 virtual void print(std::ostream &O) {
389 O << "Int: " << (int64_t)Integer
390 << " 0x" << std::hex << Integer << std::dec;
391 }
392#endif
393};
394
395//===----------------------------------------------------------------------===//
396/// DIEString - A string value DIE.
aslc200b112008-08-16 12:57:46 +0000397///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398class DIEString : public DIEValue {
399public:
400 const std::string String;
aslc200b112008-08-16 12:57:46 +0000401
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000402 explicit DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403
404 // Implement isa/cast/dyncast.
405 static bool classof(const DIEString *) { return true; }
406 static bool classof(const DIEValue *S) { return S->Type == isString; }
aslc200b112008-08-16 12:57:46 +0000407
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000408 /// EmitValue - Emit string value.
409 ///
410 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000411
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412 /// SizeOf - Determine size of string value in bytes.
413 ///
414 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
415 return String.size() + sizeof(char); // sizeof('\0');
416 }
aslc200b112008-08-16 12:57:46 +0000417
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418 /// Profile - Used to gather unique data for the value folding set.
419 ///
420 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
421 ID.AddInteger(isString);
422 ID.AddString(String);
423 }
424 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
aslc200b112008-08-16 12:57:46 +0000425
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426#ifndef NDEBUG
427 virtual void print(std::ostream &O) {
428 O << "Str: \"" << String << "\"";
429 }
430#endif
431};
432
433//===----------------------------------------------------------------------===//
434/// DIEDwarfLabel - A Dwarf internal label expression DIE.
435//
436class DIEDwarfLabel : public DIEValue {
437public:
438
439 const DWLabel Label;
aslc200b112008-08-16 12:57:46 +0000440
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000441 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442
443 // Implement isa/cast/dyncast.
444 static bool classof(const DIEDwarfLabel *) { return true; }
445 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
aslc200b112008-08-16 12:57:46 +0000446
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000447 /// EmitValue - Emit label value.
448 ///
449 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000450
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451 /// SizeOf - Determine size of label value in bytes.
452 ///
453 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000454
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 /// Profile - Used to gather unique data for the value folding set.
456 ///
457 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
458 ID.AddInteger(isLabel);
459 Label.Profile(ID);
460 }
461 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
aslc200b112008-08-16 12:57:46 +0000462
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463#ifndef NDEBUG
464 virtual void print(std::ostream &O) {
465 O << "Lbl: ";
466 Label.print(O);
467 }
468#endif
469};
470
471
472//===----------------------------------------------------------------------===//
473/// DIEObjectLabel - A label to an object in code or data.
474//
475class DIEObjectLabel : public DIEValue {
476public:
477 const std::string Label;
aslc200b112008-08-16 12:57:46 +0000478
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000479 explicit DIEObjectLabel(const std::string &L)
480 : DIEValue(isAsIsLabel), Label(L) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000481
482 // Implement isa/cast/dyncast.
483 static bool classof(const DIEObjectLabel *) { return true; }
484 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
aslc200b112008-08-16 12:57:46 +0000485
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 /// EmitValue - Emit label value.
487 ///
488 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000489
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490 /// SizeOf - Determine size of label value in bytes.
491 ///
492 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000493
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000494 /// Profile - Used to gather unique data for the value folding set.
495 ///
496 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
497 ID.AddInteger(isAsIsLabel);
498 ID.AddString(Label);
499 }
500 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
501
502#ifndef NDEBUG
503 virtual void print(std::ostream &O) {
504 O << "Obj: " << Label;
505 }
506#endif
507};
508
509//===----------------------------------------------------------------------===//
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000510/// DIESectionOffset - A section offset DIE.
511//
512class DIESectionOffset : public DIEValue {
513public:
514 const DWLabel Label;
515 const DWLabel Section;
516 bool IsEH : 1;
517 bool UseSet : 1;
aslc200b112008-08-16 12:57:46 +0000518
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000519 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
520 bool isEH = false, bool useSet = true)
521 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
522 IsEH(isEH), UseSet(useSet) {}
523
524 // Implement isa/cast/dyncast.
525 static bool classof(const DIESectionOffset *) { return true; }
526 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
aslc200b112008-08-16 12:57:46 +0000527
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000528 /// EmitValue - Emit section offset.
529 ///
530 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000531
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000532 /// SizeOf - Determine size of section offset value in bytes.
533 ///
534 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000535
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000536 /// Profile - Used to gather unique data for the value folding set.
537 ///
538 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
539 const DWLabel &Section) {
540 ID.AddInteger(isSectionOffset);
541 Label.Profile(ID);
542 Section.Profile(ID);
543 // IsEH and UseSet are specific to the Label/Section that we will emit
544 // the offset for; so Label/Section are enough for uniqueness.
545 }
546 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
547
548#ifndef NDEBUG
549 virtual void print(std::ostream &O) {
550 O << "Off: ";
551 Label.print(O);
552 O << "-";
553 Section.print(O);
554 O << "-" << IsEH << "-" << UseSet;
555 }
556#endif
557};
558
559//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560/// DIEDelta - A simple label difference DIE.
aslc200b112008-08-16 12:57:46 +0000561///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000562class DIEDelta : public DIEValue {
563public:
564 const DWLabel LabelHi;
565 const DWLabel LabelLo;
aslc200b112008-08-16 12:57:46 +0000566
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000567 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
568 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
569
570 // Implement isa/cast/dyncast.
571 static bool classof(const DIEDelta *) { return true; }
572 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
aslc200b112008-08-16 12:57:46 +0000573
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574 /// EmitValue - Emit delta value.
575 ///
576 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000577
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578 /// SizeOf - Determine size of delta value in bytes.
579 ///
580 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000581
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000582 /// Profile - Used to gather unique data for the value folding set.
583 ///
584 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
585 const DWLabel &LabelLo) {
586 ID.AddInteger(isDelta);
587 LabelHi.Profile(ID);
588 LabelLo.Profile(ID);
589 }
590 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
591
592#ifndef NDEBUG
593 virtual void print(std::ostream &O) {
594 O << "Del: ";
595 LabelHi.print(O);
596 O << "-";
597 LabelLo.print(O);
598 }
599#endif
600};
601
602//===----------------------------------------------------------------------===//
603/// DIEntry - A pointer to another debug information entry. An instance of this
604/// class can also be used as a proxy for a debug information entry not yet
605/// defined (ie. types.)
606class DIEntry : public DIEValue {
607public:
608 DIE *Entry;
aslc200b112008-08-16 12:57:46 +0000609
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000610 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
aslc200b112008-08-16 12:57:46 +0000611
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 // Implement isa/cast/dyncast.
613 static bool classof(const DIEntry *) { return true; }
614 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
aslc200b112008-08-16 12:57:46 +0000615
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616 /// EmitValue - Emit debug information entry offset.
617 ///
618 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000619
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620 /// SizeOf - Determine size of debug information entry in bytes.
621 ///
622 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
623 return sizeof(int32_t);
624 }
aslc200b112008-08-16 12:57:46 +0000625
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000626 /// Profile - Used to gather unique data for the value folding set.
627 ///
628 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
629 ID.AddInteger(isEntry);
630 ID.AddPointer(Entry);
631 }
632 virtual void Profile(FoldingSetNodeID &ID) {
633 ID.AddInteger(isEntry);
aslc200b112008-08-16 12:57:46 +0000634
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635 if (Entry) {
636 ID.AddPointer(Entry);
637 } else {
638 ID.AddPointer(this);
639 }
640 }
aslc200b112008-08-16 12:57:46 +0000641
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642#ifndef NDEBUG
643 virtual void print(std::ostream &O) {
644 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
645 }
646#endif
647};
648
649//===----------------------------------------------------------------------===//
650/// DIEBlock - A block of values. Primarily used for location expressions.
651//
652class DIEBlock : public DIEValue, public DIE {
653public:
654 unsigned Size; // Size in bytes excluding size header.
aslc200b112008-08-16 12:57:46 +0000655
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000656 DIEBlock()
657 : DIEValue(isBlock)
658 , DIE(0)
659 , Size(0)
660 {}
661 ~DIEBlock() {
662 }
aslc200b112008-08-16 12:57:46 +0000663
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664 // Implement isa/cast/dyncast.
665 static bool classof(const DIEBlock *) { return true; }
666 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
aslc200b112008-08-16 12:57:46 +0000667
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000668 /// ComputeSize - calculate the size of the block.
669 ///
670 unsigned ComputeSize(DwarfDebug &DD);
aslc200b112008-08-16 12:57:46 +0000671
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 /// BestForm - Choose the best form for data.
673 ///
674 unsigned BestForm() const {
675 if ((unsigned char)Size == Size) return DW_FORM_block1;
676 if ((unsigned short)Size == Size) return DW_FORM_block2;
677 if ((unsigned int)Size == Size) return DW_FORM_block4;
678 return DW_FORM_block;
679 }
680
681 /// EmitValue - Emit block data.
682 ///
683 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000684
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000685 /// SizeOf - Determine size of block data in bytes.
686 ///
687 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000688
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689
690 /// Profile - Used to gather unique data for the value folding set.
691 ///
692 virtual void Profile(FoldingSetNodeID &ID) {
693 ID.AddInteger(isBlock);
694 DIE::Profile(ID);
695 }
aslc200b112008-08-16 12:57:46 +0000696
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697#ifndef NDEBUG
698 virtual void print(std::ostream &O) {
699 O << "Blk: ";
700 DIE::print(O, 5);
701 }
702#endif
703};
704
705//===----------------------------------------------------------------------===//
706/// CompileUnit - This dwarf writer support class manages information associate
707/// with a source file.
708class CompileUnit {
709private:
710 /// Desc - Compile unit debug descriptor.
711 ///
712 CompileUnitDesc *Desc;
aslc200b112008-08-16 12:57:46 +0000713
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000714 /// ID - File identifier for source.
715 ///
716 unsigned ID;
aslc200b112008-08-16 12:57:46 +0000717
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000718 /// Die - Compile unit debug information entry.
719 ///
720 DIE *Die;
aslc200b112008-08-16 12:57:46 +0000721
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000722 /// DescToDieMap - Tracks the mapping of unit level debug informaton
723 /// descriptors to debug information entries.
724 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
725
726 /// DescToDIEntryMap - Tracks the mapping of unit level debug informaton
727 /// descriptors to debug information entries using a DIEntry proxy.
728 std::map<DebugInfoDesc *, DIEntry *> DescToDIEntryMap;
729
730 /// Globals - A map of globally visible named entities for this unit.
731 ///
732 std::map<std::string, DIE *> Globals;
733
734 /// DiesSet - Used to uniquely define dies within the compile unit.
735 ///
736 FoldingSet<DIE> DiesSet;
aslc200b112008-08-16 12:57:46 +0000737
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000738 /// Dies - List of all dies in the compile unit.
739 ///
740 std::vector<DIE *> Dies;
aslc200b112008-08-16 12:57:46 +0000741
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000742public:
743 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
744 : Desc(CUD)
745 , ID(I)
746 , Die(D)
747 , DescToDieMap()
748 , DescToDIEntryMap()
749 , Globals()
750 , DiesSet(InitDiesSetSize)
751 , Dies()
752 {}
aslc200b112008-08-16 12:57:46 +0000753
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000754 ~CompileUnit() {
755 delete Die;
aslc200b112008-08-16 12:57:46 +0000756
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000757 for (unsigned i = 0, N = Dies.size(); i < N; ++i)
758 delete Dies[i];
759 }
aslc200b112008-08-16 12:57:46 +0000760
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000761 // Accessors.
762 CompileUnitDesc *getDesc() const { return Desc; }
763 unsigned getID() const { return ID; }
764 DIE* getDie() const { return Die; }
765 std::map<std::string, DIE *> &getGlobals() { return Globals; }
766
767 /// hasContent - Return true if this compile unit has something to write out.
768 ///
769 bool hasContent() const {
770 return !Die->getChildren().empty();
771 }
772
773 /// AddGlobal - Add a new global entity to the compile unit.
774 ///
775 void AddGlobal(const std::string &Name, DIE *Die) {
776 Globals[Name] = Die;
777 }
aslc200b112008-08-16 12:57:46 +0000778
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000779 /// getDieMapSlotFor - Returns the debug information entry map slot for the
780 /// specified debug descriptor.
781 DIE *&getDieMapSlotFor(DebugInfoDesc *DID) {
782 return DescToDieMap[DID];
783 }
aslc200b112008-08-16 12:57:46 +0000784
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000785 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
786 /// specified debug descriptor.
787 DIEntry *&getDIEntrySlotFor(DebugInfoDesc *DID) {
788 return DescToDIEntryMap[DID];
789 }
aslc200b112008-08-16 12:57:46 +0000790
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000791 /// AddDie - Adds or interns the DIE to the compile unit.
792 ///
793 DIE *AddDie(DIE &Buffer) {
794 FoldingSetNodeID ID;
795 Buffer.Profile(ID);
796 void *Where;
797 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
aslc200b112008-08-16 12:57:46 +0000798
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000799 if (!Die) {
800 Die = new DIE(Buffer);
801 DiesSet.InsertNode(Die, Where);
802 this->Die->AddChild(Die);
803 Buffer.Detach();
804 }
aslc200b112008-08-16 12:57:46 +0000805
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806 return Die;
807 }
808};
809
810//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +0000811/// Dwarf - Emits general Dwarf directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000812///
813class Dwarf {
814
815protected:
816
817 //===--------------------------------------------------------------------===//
818 // Core attributes used by the Dwarf writer.
819 //
aslc200b112008-08-16 12:57:46 +0000820
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000821 //
822 /// O - Stream to .s file.
823 ///
Owen Anderson847b99b2008-08-21 00:14:44 +0000824 raw_ostream &O;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000825
826 /// Asm - Target of Dwarf emission.
827 ///
828 AsmPrinter *Asm;
aslc200b112008-08-16 12:57:46 +0000829
Bill Wendlingac9639d2008-07-01 23:34:48 +0000830 /// TAI - Target asm information.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000831 const TargetAsmInfo *TAI;
aslc200b112008-08-16 12:57:46 +0000832
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000833 /// TD - Target data.
834 const TargetData *TD;
aslc200b112008-08-16 12:57:46 +0000835
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000836 /// RI - Register Information.
Dan Gohman1e57df32008-02-10 18:45:23 +0000837 const TargetRegisterInfo *RI;
aslc200b112008-08-16 12:57:46 +0000838
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000839 /// M - Current module.
840 ///
841 Module *M;
aslc200b112008-08-16 12:57:46 +0000842
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000843 /// MF - Current machine function.
844 ///
845 MachineFunction *MF;
aslc200b112008-08-16 12:57:46 +0000846
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000847 /// MMI - Collected machine module information.
848 ///
849 MachineModuleInfo *MMI;
aslc200b112008-08-16 12:57:46 +0000850
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000851 /// SubprogramCount - The running count of functions being compiled.
852 ///
853 unsigned SubprogramCount;
aslc200b112008-08-16 12:57:46 +0000854
Chris Lattnerb3876c72007-09-24 03:35:37 +0000855 /// Flavor - A unique string indicating what dwarf producer this is, used to
856 /// unique labels.
857 const char * const Flavor;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000858
859 unsigned SetCounter;
Owen Anderson847b99b2008-08-21 00:14:44 +0000860 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattnerb3876c72007-09-24 03:35:37 +0000861 const char *flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000862 : O(OS)
863 , Asm(A)
864 , TAI(T)
865 , TD(Asm->TM.getTargetData())
866 , RI(Asm->TM.getRegisterInfo())
867 , M(NULL)
868 , MF(NULL)
869 , MMI(NULL)
870 , SubprogramCount(0)
Chris Lattnerb3876c72007-09-24 03:35:37 +0000871 , Flavor(flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000872 , SetCounter(1)
873 {
874 }
875
876public:
877
878 //===--------------------------------------------------------------------===//
879 // Accessors.
880 //
881 AsmPrinter *getAsm() const { return Asm; }
882 MachineModuleInfo *getMMI() const { return MMI; }
883 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohmancfb72b22007-09-27 23:12:31 +0000884 const TargetData *getTargetData() const { return TD; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000885
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000886 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
887 const {
888 if (isInSection && TAI->getDwarfSectionOffsetDirective())
889 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohmancfb72b22007-09-27 23:12:31 +0000890 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000891 O << TAI->getData32bitsDirective();
892 else
893 O << TAI->getData64bitsDirective();
894 }
aslc200b112008-08-16 12:57:46 +0000895
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000896 /// PrintLabelName - Print label name in form used by Dwarf writer.
897 ///
898 void PrintLabelName(DWLabel Label) const {
899 PrintLabelName(Label.Tag, Label.Number);
900 }
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000901 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000902 O << TAI->getPrivateGlobalPrefix() << Tag;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000903 if (Number) O << Number;
904 }
aslc200b112008-08-16 12:57:46 +0000905
Chris Lattnerb3876c72007-09-24 03:35:37 +0000906 void PrintLabelName(const char *Tag, unsigned Number,
907 const char *Suffix) const {
908 O << TAI->getPrivateGlobalPrefix() << Tag;
909 if (Number) O << Number;
910 O << Suffix;
911 }
aslc200b112008-08-16 12:57:46 +0000912
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000913 /// EmitLabel - Emit location label for internal use by Dwarf.
914 ///
915 void EmitLabel(DWLabel Label) const {
916 EmitLabel(Label.Tag, Label.Number);
917 }
918 void EmitLabel(const char *Tag, unsigned Number) const {
919 PrintLabelName(Tag, Number);
920 O << ":\n";
921 }
aslc200b112008-08-16 12:57:46 +0000922
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000923 /// EmitReference - Emit a reference to a label.
924 ///
Dan Gohman4fd77742007-09-28 15:43:33 +0000925 void EmitReference(DWLabel Label, bool IsPCRelative = false,
926 bool Force32Bit = false) const {
927 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000928 }
929 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman4fd77742007-09-28 15:43:33 +0000930 bool IsPCRelative = false, bool Force32Bit = false) const {
931 PrintRelDirective(Force32Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000932 PrintLabelName(Tag, Number);
aslc200b112008-08-16 12:57:46 +0000933
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000934 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
935 }
Dan Gohman4fd77742007-09-28 15:43:33 +0000936 void EmitReference(const std::string &Name, bool IsPCRelative = false,
937 bool Force32Bit = false) const {
938 PrintRelDirective(Force32Bit);
aslc200b112008-08-16 12:57:46 +0000939
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000940 O << Name;
aslc200b112008-08-16 12:57:46 +0000941
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000942 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
943 }
944
945 /// EmitDifference - Emit the difference between two labels. Some
946 /// assemblers do not behave with absolute expressions with data directives,
947 /// so there is an option (needsSet) to use an intermediary set expression.
948 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
949 bool IsSmall = false) {
950 EmitDifference(LabelHi.Tag, LabelHi.Number,
951 LabelLo.Tag, LabelLo.Number,
952 IsSmall);
953 }
954 void EmitDifference(const char *TagHi, unsigned NumberHi,
955 const char *TagLo, unsigned NumberLo,
956 bool IsSmall = false) {
957 if (TAI->needsSet()) {
958 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +0000959 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000960 O << ",";
961 PrintLabelName(TagHi, NumberHi);
962 O << "-";
963 PrintLabelName(TagLo, NumberLo);
964 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000965
966 PrintRelDirective(IsSmall);
Chris Lattnerb3876c72007-09-24 03:35:37 +0000967 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000968 ++SetCounter;
969 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000970 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +0000971
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000972 PrintLabelName(TagHi, NumberHi);
973 O << "-";
974 PrintLabelName(TagLo, NumberLo);
975 }
976 }
977
978 void EmitSectionOffset(const char* Label, const char* Section,
979 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesen0ebb2432008-03-26 23:31:39 +0000980 bool IsSmall = false, bool isEH = false,
981 bool useSet = true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000982 bool printAbsolute = false;
Dale Johannesen0ebb2432008-03-26 23:31:39 +0000983 if (isEH)
984 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
985 else
986 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
987
988 if (TAI->needsSet() && useSet) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000989 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +0000990 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991 O << ",";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000992 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000993
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000994 if (!printAbsolute) {
995 O << "-";
996 PrintLabelName(Section, SectionNumber);
aslc200b112008-08-16 12:57:46 +0000997 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000998 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000999
1000 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +00001001
Chris Lattnerb3876c72007-09-24 03:35:37 +00001002 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001003 ++SetCounter;
1004 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001005 PrintRelDirective(IsSmall, true);
aslc200b112008-08-16 12:57:46 +00001006
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001007 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001008
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001009 if (!printAbsolute) {
1010 O << "-";
1011 PrintLabelName(Section, SectionNumber);
1012 }
aslc200b112008-08-16 12:57:46 +00001013 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001014 }
aslc200b112008-08-16 12:57:46 +00001015
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001016 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1017 /// frame.
1018 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenf5a11532007-11-13 19:13:01 +00001019 const std::vector<MachineMove> &Moves, bool isEH) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001020 int stackGrowth =
1021 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1022 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00001023 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001024 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
1025
1026 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
1027 const MachineMove &Move = Moves[i];
1028 unsigned LabelID = Move.getLabelID();
aslc200b112008-08-16 12:57:46 +00001029
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001030 if (LabelID) {
1031 LabelID = MMI->MappedLabel(LabelID);
aslc200b112008-08-16 12:57:46 +00001032
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001033 // Throw out move if the label is invalid.
1034 if (!LabelID) continue;
1035 }
aslc200b112008-08-16 12:57:46 +00001036
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001037 const MachineLocation &Dst = Move.getDestination();
1038 const MachineLocation &Src = Move.getSource();
aslc200b112008-08-16 12:57:46 +00001039
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001040 // Advance row if new location.
1041 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1042 Asm->EmitInt8(DW_CFA_advance_loc4);
1043 Asm->EOL("DW_CFA_advance_loc4");
1044 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1045 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00001046
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001047 BaseLabelID = LabelID;
1048 BaseLabel = "label";
1049 IsLocal = true;
1050 }
aslc200b112008-08-16 12:57:46 +00001051
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001052 // If advancing cfa.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001053 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1054 if (!Src.isReg()) {
1055 if (Src.getReg() == MachineLocation::VirtualFP) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001056 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1057 Asm->EOL("DW_CFA_def_cfa_offset");
1058 } else {
1059 Asm->EmitInt8(DW_CFA_def_cfa);
1060 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001061 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001062 Asm->EOL("Register");
1063 }
aslc200b112008-08-16 12:57:46 +00001064
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001065 int Offset = -Src.getOffset();
aslc200b112008-08-16 12:57:46 +00001066
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001067 Asm->EmitULEB128Bytes(Offset);
1068 Asm->EOL("Offset");
1069 } else {
1070 assert(0 && "Machine move no supported yet.");
1071 }
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001072 } else if (Src.isReg() &&
1073 Src.getReg() == MachineLocation::VirtualFP) {
1074 if (Dst.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001075 Asm->EmitInt8(DW_CFA_def_cfa_register);
1076 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001077 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001078 Asm->EOL("Register");
1079 } else {
1080 assert(0 && "Machine move no supported yet.");
1081 }
1082 } else {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001083 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001084 int Offset = Dst.getOffset() / stackGrowth;
aslc200b112008-08-16 12:57:46 +00001085
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001086 if (Offset < 0) {
1087 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1088 Asm->EOL("DW_CFA_offset_extended_sf");
1089 Asm->EmitULEB128Bytes(Reg);
1090 Asm->EOL("Reg");
1091 Asm->EmitSLEB128Bytes(Offset);
1092 Asm->EOL("Offset");
1093 } else if (Reg < 64) {
1094 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Cheng6181e062008-07-09 21:53:02 +00001095 if (VerboseAsm)
1096 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1097 else
1098 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001099 Asm->EmitULEB128Bytes(Offset);
1100 Asm->EOL("Offset");
1101 } else {
1102 Asm->EmitInt8(DW_CFA_offset_extended);
1103 Asm->EOL("DW_CFA_offset_extended");
1104 Asm->EmitULEB128Bytes(Reg);
1105 Asm->EOL("Reg");
1106 Asm->EmitULEB128Bytes(Offset);
1107 Asm->EOL("Offset");
1108 }
1109 }
1110 }
1111 }
1112
1113};
1114
1115//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00001116/// DwarfDebug - Emits Dwarf debug directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001117///
1118class DwarfDebug : public Dwarf {
1119
1120private:
1121 //===--------------------------------------------------------------------===//
1122 // Attributes used to construct specific Dwarf sections.
1123 //
aslc200b112008-08-16 12:57:46 +00001124
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001125 /// CompileUnits - All the compile units involved in this build. The index
1126 /// of each entry in this vector corresponds to the sources in MMI.
1127 std::vector<CompileUnit *> CompileUnits;
aslc200b112008-08-16 12:57:46 +00001128
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001129 /// AbbreviationsSet - Used to uniquely define abbreviations.
1130 ///
1131 FoldingSet<DIEAbbrev> AbbreviationsSet;
1132
1133 /// Abbreviations - A list of all the unique abbreviations in use.
1134 ///
1135 std::vector<DIEAbbrev *> Abbreviations;
aslc200b112008-08-16 12:57:46 +00001136
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001137 /// ValuesSet - Used to uniquely define values.
1138 ///
1139 FoldingSet<DIEValue> ValuesSet;
aslc200b112008-08-16 12:57:46 +00001140
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001141 /// Values - A list of all the unique values in use.
1142 ///
1143 std::vector<DIEValue *> Values;
aslc200b112008-08-16 12:57:46 +00001144
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001145 /// StringPool - A UniqueVector of strings used by indirect references.
1146 ///
1147 UniqueVector<std::string> StringPool;
1148
1149 /// UnitMap - Map debug information descriptor to compile unit.
1150 ///
1151 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
aslc200b112008-08-16 12:57:46 +00001152
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001153 /// SectionMap - Provides a unique id per text section.
1154 ///
Anton Korobeynikov55b94962008-09-24 22:15:21 +00001155 UniqueVector<const Section*> SectionMap;
aslc200b112008-08-16 12:57:46 +00001156
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001157 /// SectionSourceLines - Tracks line numbers per text section.
1158 ///
1159 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
1160
1161 /// didInitial - Flag to indicate if initial emission has been done.
1162 ///
1163 bool didInitial;
aslc200b112008-08-16 12:57:46 +00001164
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001165 /// shouldEmit - Flag to indicate if debug information should be emitted.
1166 ///
1167 bool shouldEmit;
1168
1169 struct FunctionDebugFrameInfo {
1170 unsigned Number;
1171 std::vector<MachineMove> Moves;
1172
1173 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman9ba5d4d2007-08-27 14:50:10 +00001174 Number(Num), Moves(M) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001175 };
1176
1177 std::vector<FunctionDebugFrameInfo> DebugFrames;
aslc200b112008-08-16 12:57:46 +00001178
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001179public:
aslc200b112008-08-16 12:57:46 +00001180
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001181 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1182 ///
1183 bool ShouldEmitDwarf() const { return shouldEmit; }
1184
1185 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
aslc200b112008-08-16 12:57:46 +00001186 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001187 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1188 // Profile the node so that we can make it unique.
1189 FoldingSetNodeID ID;
1190 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00001191
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001192 // Check the set for priors.
1193 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
aslc200b112008-08-16 12:57:46 +00001194
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001195 // If it's newly added.
1196 if (InSet == &Abbrev) {
aslc200b112008-08-16 12:57:46 +00001197 // Add to abbreviation list.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001198 Abbreviations.push_back(&Abbrev);
1199 // Assign the vector position + 1 as its number.
1200 Abbrev.setNumber(Abbreviations.size());
1201 } else {
1202 // Assign existing abbreviation number.
1203 Abbrev.setNumber(InSet->getNumber());
1204 }
1205 }
1206
1207 /// NewString - Add a string to the constant pool and returns a label.
1208 ///
1209 DWLabel NewString(const std::string &String) {
1210 unsigned StringID = StringPool.insert(String);
1211 return DWLabel("string", StringID);
1212 }
aslc200b112008-08-16 12:57:46 +00001213
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001214 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1215 /// entry.
1216 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1217 DIEntry *Value;
aslc200b112008-08-16 12:57:46 +00001218
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001219 if (Entry) {
1220 FoldingSetNodeID ID;
1221 DIEntry::Profile(ID, Entry);
1222 void *Where;
1223 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
aslc200b112008-08-16 12:57:46 +00001224
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001225 if (Value) return Value;
aslc200b112008-08-16 12:57:46 +00001226
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001227 Value = new DIEntry(Entry);
1228 ValuesSet.InsertNode(Value, Where);
1229 } else {
1230 Value = new DIEntry(Entry);
1231 }
aslc200b112008-08-16 12:57:46 +00001232
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001233 Values.push_back(Value);
1234 return Value;
1235 }
aslc200b112008-08-16 12:57:46 +00001236
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001237 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1238 ///
1239 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1240 Value->Entry = Entry;
1241 // Add to values set if not already there. If it is, we merely have a
1242 // duplicate in the values list (no harm.)
1243 ValuesSet.GetOrInsertNode(Value);
1244 }
1245
1246 /// AddUInt - Add an unsigned integer attribute data and value.
1247 ///
1248 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1249 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1250
1251 FoldingSetNodeID ID;
1252 DIEInteger::Profile(ID, Integer);
1253 void *Where;
1254 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1255 if (!Value) {
1256 Value = new DIEInteger(Integer);
1257 ValuesSet.InsertNode(Value, Where);
1258 Values.push_back(Value);
1259 }
aslc200b112008-08-16 12:57:46 +00001260
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001261 Die->AddValue(Attribute, Form, Value);
1262 }
aslc200b112008-08-16 12:57:46 +00001263
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001264 /// AddSInt - Add an signed integer attribute data and value.
1265 ///
1266 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1267 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1268
1269 FoldingSetNodeID ID;
1270 DIEInteger::Profile(ID, (uint64_t)Integer);
1271 void *Where;
1272 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1273 if (!Value) {
1274 Value = new DIEInteger(Integer);
1275 ValuesSet.InsertNode(Value, Where);
1276 Values.push_back(Value);
1277 }
aslc200b112008-08-16 12:57:46 +00001278
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001279 Die->AddValue(Attribute, Form, Value);
1280 }
aslc200b112008-08-16 12:57:46 +00001281
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001282 /// AddString - Add a std::string attribute data and value.
1283 ///
1284 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1285 const std::string &String) {
1286 FoldingSetNodeID ID;
1287 DIEString::Profile(ID, String);
1288 void *Where;
1289 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1290 if (!Value) {
1291 Value = new DIEString(String);
1292 ValuesSet.InsertNode(Value, Where);
1293 Values.push_back(Value);
1294 }
aslc200b112008-08-16 12:57:46 +00001295
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001296 Die->AddValue(Attribute, Form, Value);
1297 }
aslc200b112008-08-16 12:57:46 +00001298
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001299 /// AddLabel - Add a Dwarf label attribute data and value.
1300 ///
1301 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1302 const DWLabel &Label) {
1303 FoldingSetNodeID ID;
1304 DIEDwarfLabel::Profile(ID, Label);
1305 void *Where;
1306 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1307 if (!Value) {
1308 Value = new DIEDwarfLabel(Label);
1309 ValuesSet.InsertNode(Value, Where);
1310 Values.push_back(Value);
1311 }
aslc200b112008-08-16 12:57:46 +00001312
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001313 Die->AddValue(Attribute, Form, Value);
1314 }
aslc200b112008-08-16 12:57:46 +00001315
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001316 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1317 ///
1318 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1319 const std::string &Label) {
1320 FoldingSetNodeID ID;
1321 DIEObjectLabel::Profile(ID, Label);
1322 void *Where;
1323 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1324 if (!Value) {
1325 Value = new DIEObjectLabel(Label);
1326 ValuesSet.InsertNode(Value, Where);
1327 Values.push_back(Value);
1328 }
aslc200b112008-08-16 12:57:46 +00001329
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001330 Die->AddValue(Attribute, Form, Value);
1331 }
aslc200b112008-08-16 12:57:46 +00001332
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001333 /// AddSectionOffset - Add a section offset label attribute data and value.
1334 ///
1335 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1336 const DWLabel &Label, const DWLabel &Section,
1337 bool isEH = false, bool useSet = true) {
1338 FoldingSetNodeID ID;
1339 DIESectionOffset::Profile(ID, Label, Section);
1340 void *Where;
1341 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1342 if (!Value) {
1343 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1344 ValuesSet.InsertNode(Value, Where);
1345 Values.push_back(Value);
1346 }
aslc200b112008-08-16 12:57:46 +00001347
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001348 Die->AddValue(Attribute, Form, Value);
1349 }
aslc200b112008-08-16 12:57:46 +00001350
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001351 /// AddDelta - Add a label delta attribute data and value.
1352 ///
1353 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1354 const DWLabel &Hi, const DWLabel &Lo) {
1355 FoldingSetNodeID ID;
1356 DIEDelta::Profile(ID, Hi, Lo);
1357 void *Where;
1358 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1359 if (!Value) {
1360 Value = new DIEDelta(Hi, Lo);
1361 ValuesSet.InsertNode(Value, Where);
1362 Values.push_back(Value);
1363 }
aslc200b112008-08-16 12:57:46 +00001364
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001365 Die->AddValue(Attribute, Form, Value);
1366 }
aslc200b112008-08-16 12:57:46 +00001367
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001368 /// AddDIEntry - Add a DIE attribute data and value.
1369 ///
1370 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1371 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1372 }
1373
1374 /// AddBlock - Add block data.
1375 ///
1376 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1377 Block->ComputeSize(*this);
1378 FoldingSetNodeID ID;
1379 Block->Profile(ID);
1380 void *Where;
1381 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1382 if (!Value) {
1383 Value = Block;
1384 ValuesSet.InsertNode(Value, Where);
1385 Values.push_back(Value);
1386 } else {
Chris Lattner3de66892007-09-21 18:25:53 +00001387 // Already exists, reuse the previous one.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001388 delete Block;
Chris Lattner3de66892007-09-21 18:25:53 +00001389 Block = cast<DIEBlock>(Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001390 }
aslc200b112008-08-16 12:57:46 +00001391
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001392 Die->AddValue(Attribute, Block->BestForm(), Value);
1393 }
1394
1395private:
1396
1397 /// AddSourceLine - Add location information to specified debug information
1398 /// entry.
1399 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1400 if (File && Line) {
1401 CompileUnit *FileUnit = FindCompileUnit(File);
1402 unsigned FileID = FileUnit->getID();
1403 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1404 AddUInt(Die, DW_AT_decl_line, 0, Line);
1405 }
1406 }
1407
1408 /// AddAddress - Add an address attribute to a die based on the location
1409 /// provided.
1410 void AddAddress(DIE *Die, unsigned Attribute,
1411 const MachineLocation &Location) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001412 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001413 DIEBlock *Block = new DIEBlock();
aslc200b112008-08-16 12:57:46 +00001414
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001415 if (Location.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001416 if (Reg < 32) {
1417 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1418 } else {
1419 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1420 AddUInt(Block, 0, DW_FORM_udata, Reg);
1421 }
1422 } else {
1423 if (Reg < 32) {
1424 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1425 } else {
1426 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1427 AddUInt(Block, 0, DW_FORM_udata, Reg);
1428 }
1429 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1430 }
aslc200b112008-08-16 12:57:46 +00001431
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001432 AddBlock(Die, Attribute, 0, Block);
1433 }
aslc200b112008-08-16 12:57:46 +00001434
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001435 /// AddBasicType - Add a new basic type attribute to the specified entity.
1436 ///
1437 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1438 const std::string &Name,
1439 unsigned Encoding, unsigned Size) {
1440 DIE *Die = ConstructBasicType(Unit, Name, Encoding, Size);
1441 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1442 }
aslc200b112008-08-16 12:57:46 +00001443
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001444 /// ConstructBasicType - Construct a new basic type.
1445 ///
1446 DIE *ConstructBasicType(CompileUnit *Unit,
1447 const std::string &Name,
1448 unsigned Encoding, unsigned Size) {
1449 DIE Buffer(DW_TAG_base_type);
1450 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1451 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1452 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1453 return Unit->AddDie(Buffer);
1454 }
aslc200b112008-08-16 12:57:46 +00001455
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001456 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1457 ///
1458 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
1459 DIE *Die = ConstructPointerType(Unit, Name);
1460 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1461 }
aslc200b112008-08-16 12:57:46 +00001462
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001463 /// ConstructPointerType - Construct a new pointer type.
1464 ///
1465 DIE *ConstructPointerType(CompileUnit *Unit, const std::string &Name) {
1466 DIE Buffer(DW_TAG_pointer_type);
Dan Gohmancfb72b22007-09-27 23:12:31 +00001467 AddUInt(&Buffer, DW_AT_byte_size, 0, TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001468 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1469 return Unit->AddDie(Buffer);
1470 }
aslc200b112008-08-16 12:57:46 +00001471
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001472 /// AddType - Add a new type attribute to the specified entity.
1473 ///
1474 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1475 if (!TyDesc) {
1476 AddBasicType(Entity, Unit, "", DW_ATE_signed, sizeof(int32_t));
1477 } else {
1478 // Check for pre-existence.
1479 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
aslc200b112008-08-16 12:57:46 +00001480
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001481 // If it exists then use the existing value.
1482 if (Slot) {
1483 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1484 return;
1485 }
aslc200b112008-08-16 12:57:46 +00001486
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001487 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1488 // FIXME - Not sure why programs and variables are coming through here.
1489 // Short cut for handling subprogram types (not really a TyDesc.)
1490 AddPointerType(Entity, Unit, SubprogramTy->getName());
1491 } else if (GlobalVariableDesc *GlobalTy =
1492 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1493 // FIXME - Not sure why programs and variables are coming through here.
1494 // Short cut for handling global variable types (not really a TyDesc.)
1495 AddPointerType(Entity, Unit, GlobalTy->getName());
aslc200b112008-08-16 12:57:46 +00001496 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001497 // Set up proxy.
1498 Slot = NewDIEntry();
aslc200b112008-08-16 12:57:46 +00001499
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001500 // Construct type.
1501 DIE Buffer(DW_TAG_base_type);
1502 ConstructType(Buffer, TyDesc, Unit);
aslc200b112008-08-16 12:57:46 +00001503
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001504 // Add debug information entry to entity and unit.
1505 DIE *Die = Unit->AddDie(Buffer);
1506 SetDIEntry(Slot, Die);
1507 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1508 }
1509 }
1510 }
aslc200b112008-08-16 12:57:46 +00001511
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001512 /// ConstructType - Adds all the required attributes to the type.
1513 ///
1514 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
1515 // Get core information.
1516 const std::string &Name = TyDesc->getName();
1517 uint64_t Size = TyDesc->getSize() >> 3;
aslc200b112008-08-16 12:57:46 +00001518
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001519 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1520 // Fundamental types like int, float, bool
1521 Buffer.setTag(DW_TAG_base_type);
1522 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
1523 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
1524 // Fetch tag.
1525 unsigned Tag = DerivedTy->getTag();
1526 // FIXME - Workaround for templates.
1527 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
aslc200b112008-08-16 12:57:46 +00001528 // Pointers, typedefs et al.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001529 Buffer.setTag(Tag);
1530 // Map to main type, void will not have a type.
1531 if (TypeDesc *FromTy = DerivedTy->getFromType())
1532 AddType(&Buffer, FromTy, Unit);
1533 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
1534 // Fetch tag.
1535 unsigned Tag = CompTy->getTag();
aslc200b112008-08-16 12:57:46 +00001536
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001537 // Set tag accordingly.
1538 if (Tag == DW_TAG_vector_type)
1539 Buffer.setTag(DW_TAG_array_type);
aslc200b112008-08-16 12:57:46 +00001540 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001541 Buffer.setTag(Tag);
1542
1543 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
aslc200b112008-08-16 12:57:46 +00001544
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001545 switch (Tag) {
1546 case DW_TAG_vector_type:
1547 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1548 // Fall thru
1549 case DW_TAG_array_type: {
1550 // Add element type.
1551 if (TypeDesc *FromTy = CompTy->getFromType())
1552 AddType(&Buffer, FromTy, Unit);
aslc200b112008-08-16 12:57:46 +00001553
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001554 // Don't emit size attribute.
1555 Size = 0;
aslc200b112008-08-16 12:57:46 +00001556
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001557 // Construct an anonymous type for index type.
1558 DIE *IndexTy = ConstructBasicType(Unit, "", DW_ATE_signed,
1559 sizeof(int32_t));
aslc200b112008-08-16 12:57:46 +00001560
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001561 // Add subranges to array type.
Evan Chengc7efea32008-12-09 17:56:30 +00001562 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001563 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1564 int64_t Lo = SRD->getLo();
1565 int64_t Hi = SRD->getHi();
1566 DIE *Subrange = new DIE(DW_TAG_subrange_type);
aslc200b112008-08-16 12:57:46 +00001567
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001568 // If a range is available.
1569 if (Lo != Hi) {
1570 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1571 // Only add low if non-zero.
1572 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
1573 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
1574 }
aslc200b112008-08-16 12:57:46 +00001575
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001576 Buffer.AddChild(Subrange);
1577 }
1578 break;
1579 }
1580 case DW_TAG_structure_type:
1581 case DW_TAG_union_type: {
1582 // Add elements to structure type.
Evan Chengc7efea32008-12-09 17:56:30 +00001583 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001584 DebugInfoDesc *Element = Elements[i];
aslc200b112008-08-16 12:57:46 +00001585
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001586 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
1587 // Add field or base class.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001588 unsigned Tag = MemberDesc->getTag();
aslc200b112008-08-16 12:57:46 +00001589
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001590 // Extract the basic information.
1591 const std::string &Name = MemberDesc->getName();
1592 uint64_t Size = MemberDesc->getSize();
1593 uint64_t Align = MemberDesc->getAlign();
1594 uint64_t Offset = MemberDesc->getOffset();
aslc200b112008-08-16 12:57:46 +00001595
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001596 // Construct member debug information entry.
1597 DIE *Member = new DIE(Tag);
aslc200b112008-08-16 12:57:46 +00001598
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001599 // Add name if not "".
1600 if (!Name.empty())
1601 AddString(Member, DW_AT_name, DW_FORM_string, Name);
Evan Chengc7efea32008-12-09 17:56:30 +00001602
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001603 // Add location if available.
1604 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
aslc200b112008-08-16 12:57:46 +00001605
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001606 // Most of the time the field info is the same as the members.
1607 uint64_t FieldSize = Size;
1608 uint64_t FieldAlign = Align;
1609 uint64_t FieldOffset = Offset;
aslc200b112008-08-16 12:57:46 +00001610
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001611 // Set the member type.
1612 TypeDesc *FromTy = MemberDesc->getFromType();
1613 AddType(Member, FromTy, Unit);
aslc200b112008-08-16 12:57:46 +00001614
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001615 // Walk up typedefs until a real size is found.
1616 while (FromTy) {
1617 if (FromTy->getTag() != DW_TAG_typedef) {
1618 FieldSize = FromTy->getSize();
1619 FieldAlign = FromTy->getSize();
1620 break;
1621 }
aslc200b112008-08-16 12:57:46 +00001622
Dan Gohman53491e92007-07-23 20:24:29 +00001623 FromTy = cast<DerivedTypeDesc>(FromTy)->getFromType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001624 }
aslc200b112008-08-16 12:57:46 +00001625
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001626 // Unless we have a bit field.
1627 if (Tag == DW_TAG_member && FieldSize != Size) {
1628 // Construct the alignment mask.
1629 uint64_t AlignMask = ~(FieldAlign - 1);
1630 // Determine the high bit + 1 of the declared size.
1631 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1632 // Work backwards to determine the base offset of the field.
1633 FieldOffset = HiMark - FieldSize;
1634 // Now normalize offset to the field.
1635 Offset -= FieldOffset;
aslc200b112008-08-16 12:57:46 +00001636
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001637 // Maybe we need to work from the other end.
1638 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
aslc200b112008-08-16 12:57:46 +00001639
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001640 // Add size and offset.
1641 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
1642 AddUInt(Member, DW_AT_bit_size, 0, Size);
1643 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
1644 }
aslc200b112008-08-16 12:57:46 +00001645
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001646 // Add computation for offset.
1647 DIEBlock *Block = new DIEBlock();
1648 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1649 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
1650 AddBlock(Member, DW_AT_data_member_location, 0, Block);
1651
1652 // Add accessibility (public default unless is base class.
1653 if (MemberDesc->isProtected()) {
1654 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
1655 } else if (MemberDesc->isPrivate()) {
1656 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
1657 } else if (Tag == DW_TAG_inheritance) {
1658 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
1659 }
aslc200b112008-08-16 12:57:46 +00001660
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001661 Buffer.AddChild(Member);
1662 } else if (GlobalVariableDesc *StaticDesc =
1663 dyn_cast<GlobalVariableDesc>(Element)) {
1664 // Add static member.
aslc200b112008-08-16 12:57:46 +00001665
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001666 // Construct member debug information entry.
1667 DIE *Static = new DIE(DW_TAG_variable);
aslc200b112008-08-16 12:57:46 +00001668
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001669 // Add name and mangled name.
1670 const std::string &Name = StaticDesc->getName();
1671 const std::string &LinkageName = StaticDesc->getLinkageName();
1672 AddString(Static, DW_AT_name, DW_FORM_string, Name);
1673 if (!LinkageName.empty()) {
1674 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
1675 LinkageName);
1676 }
aslc200b112008-08-16 12:57:46 +00001677
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001678 // Add location.
1679 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
aslc200b112008-08-16 12:57:46 +00001680
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001681 // Add type.
1682 if (TypeDesc *StaticTy = StaticDesc->getType())
1683 AddType(Static, StaticTy, Unit);
aslc200b112008-08-16 12:57:46 +00001684
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001685 // Add flags.
1686 if (!StaticDesc->isStatic())
1687 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
1688 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
aslc200b112008-08-16 12:57:46 +00001689
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001690 Buffer.AddChild(Static);
1691 } else if (SubprogramDesc *MethodDesc =
1692 dyn_cast<SubprogramDesc>(Element)) {
1693 // Add member function.
aslc200b112008-08-16 12:57:46 +00001694
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001695 // Construct member debug information entry.
1696 DIE *Method = new DIE(DW_TAG_subprogram);
aslc200b112008-08-16 12:57:46 +00001697
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001698 // Add name and mangled name.
1699 const std::string &Name = MethodDesc->getName();
1700 const std::string &LinkageName = MethodDesc->getLinkageName();
aslc200b112008-08-16 12:57:46 +00001701
1702 AddString(Method, DW_AT_name, DW_FORM_string, Name);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001703 bool IsCTor = TyDesc->getName() == Name;
aslc200b112008-08-16 12:57:46 +00001704
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001705 if (!LinkageName.empty()) {
1706 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
1707 LinkageName);
1708 }
aslc200b112008-08-16 12:57:46 +00001709
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001710 // Add location.
1711 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
aslc200b112008-08-16 12:57:46 +00001712
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001713 // Add type.
1714 if (CompositeTypeDesc *MethodTy =
1715 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1716 // Get argument information.
1717 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
aslc200b112008-08-16 12:57:46 +00001718
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001719 // If not a ctor.
1720 if (!IsCTor) {
1721 // Add return type.
1722 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
1723 }
aslc200b112008-08-16 12:57:46 +00001724
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001725 // Add arguments.
Evan Chengc7efea32008-12-09 17:56:30 +00001726 for (unsigned i = 1, N = Args.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001727 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1728 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
1729 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
1730 Method->AddChild(Arg);
1731 }
1732 }
1733
1734 // Add flags.
1735 if (!MethodDesc->isStatic())
1736 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
1737 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
aslc200b112008-08-16 12:57:46 +00001738
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001739 Buffer.AddChild(Method);
1740 }
1741 }
1742 break;
1743 }
1744 case DW_TAG_enumeration_type: {
1745 // Add enumerators to enumeration type.
Evan Chengc7efea32008-12-09 17:56:30 +00001746 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001747 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1748 const std::string &Name = ED->getName();
1749 int64_t Value = ED->getValue();
1750 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1751 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
1752 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1753 Buffer.AddChild(Enumerator);
1754 }
1755
1756 break;
1757 }
1758 case DW_TAG_subroutine_type: {
1759 // Add prototype flag.
1760 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1761 // Add return type.
1762 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
aslc200b112008-08-16 12:57:46 +00001763
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001764 // Add arguments.
Evan Chengc7efea32008-12-09 17:56:30 +00001765 for (unsigned i = 1, N = Elements.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001766 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1767 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
1768 Buffer.AddChild(Arg);
1769 }
aslc200b112008-08-16 12:57:46 +00001770
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001771 break;
1772 }
1773 default: break;
1774 }
1775 }
aslc200b112008-08-16 12:57:46 +00001776
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001777 // Add size if non-zero (derived types don't have a size.)
1778 if (Size) AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
Evan Chengc7efea32008-12-09 17:56:30 +00001779
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001780 // Add name if not anonymous or intermediate type.
1781 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Evan Chengc7efea32008-12-09 17:56:30 +00001782
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001783 // Add source line info if available.
1784 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
1785 }
1786
1787 /// NewCompileUnit - Create new compile unit and it's debug information entry.
1788 ///
1789 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
1790 // Construct debug information entry.
1791 DIE *Die = new DIE(DW_TAG_compile_unit);
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001792 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
1793 DWLabel("section_line", 0), DWLabel("section_line", 0), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001794 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1795 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1796 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1797 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
aslc200b112008-08-16 12:57:46 +00001798
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001799 // Construct compile unit.
1800 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
aslc200b112008-08-16 12:57:46 +00001801
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001802 // Add Unit to compile unit map.
1803 DescToUnitMap[UnitDesc] = Unit;
aslc200b112008-08-16 12:57:46 +00001804
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001805 return Unit;
1806 }
1807
1808 /// GetBaseCompileUnit - Get the main compile unit.
1809 ///
1810 CompileUnit *GetBaseCompileUnit() const {
1811 CompileUnit *Unit = CompileUnits[0];
1812 assert(Unit && "Missing compile unit.");
1813 return Unit;
1814 }
1815
1816 /// FindCompileUnit - Get the compile unit for the given descriptor.
1817 ///
1818 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
1819 CompileUnit *Unit = DescToUnitMap[UnitDesc];
1820 assert(Unit && "Missing compile unit.");
1821 return Unit;
1822 }
1823
1824 /// NewGlobalVariable - Add a new global variable DIE.
1825 ///
1826 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
1827 // Get the compile unit context.
1828 CompileUnitDesc *UnitDesc =
1829 static_cast<CompileUnitDesc *>(GVD->getContext());
1830 CompileUnit *Unit = GetBaseCompileUnit();
1831
1832 // Check for pre-existence.
1833 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1834 if (Slot) return Slot;
aslc200b112008-08-16 12:57:46 +00001835
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001836 // Get the global variable itself.
1837 GlobalVariable *GV = GVD->getGlobalVariable();
1838
1839 const std::string &Name = GVD->getName();
1840 const std::string &FullName = GVD->getFullName();
1841 const std::string &LinkageName = GVD->getLinkageName();
1842 // Create the global's variable DIE.
1843 DIE *VariableDie = new DIE(DW_TAG_variable);
1844 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
1845 if (!LinkageName.empty()) {
1846 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1847 LinkageName);
1848 }
1849 AddType(VariableDie, GVD->getType(), Unit);
1850 if (!GVD->isStatic())
1851 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
aslc200b112008-08-16 12:57:46 +00001852
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001853 // Add source line info if available.
1854 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
aslc200b112008-08-16 12:57:46 +00001855
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001856 // Add address.
1857 DIEBlock *Block = new DIEBlock();
1858 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
1859 AddObjectLabel(Block, 0, DW_FORM_udata, Asm->getGlobalLinkName(GV));
1860 AddBlock(VariableDie, DW_AT_location, 0, Block);
aslc200b112008-08-16 12:57:46 +00001861
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001862 // Add to map.
1863 Slot = VariableDie;
aslc200b112008-08-16 12:57:46 +00001864
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001865 // Add to context owner.
1866 Unit->getDie()->AddChild(VariableDie);
aslc200b112008-08-16 12:57:46 +00001867
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001868 // Expose as global.
1869 // FIXME - need to check external flag.
1870 Unit->AddGlobal(FullName, VariableDie);
aslc200b112008-08-16 12:57:46 +00001871
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001872 return VariableDie;
1873 }
1874
1875 /// NewSubprogram - Add a new subprogram DIE.
1876 ///
1877 DIE *NewSubprogram(SubprogramDesc *SPD) {
1878 // Get the compile unit context.
1879 CompileUnitDesc *UnitDesc =
1880 static_cast<CompileUnitDesc *>(SPD->getContext());
1881 CompileUnit *Unit = GetBaseCompileUnit();
1882
1883 // Check for pre-existence.
1884 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1885 if (Slot) return Slot;
aslc200b112008-08-16 12:57:46 +00001886
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001887 // Gather the details (simplify add attribute code.)
1888 const std::string &Name = SPD->getName();
1889 const std::string &FullName = SPD->getFullName();
1890 const std::string &LinkageName = SPD->getLinkageName();
aslc200b112008-08-16 12:57:46 +00001891
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001892 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1893 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
1894 if (!LinkageName.empty()) {
1895 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1896 LinkageName);
1897 }
1898 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
1899 if (!SPD->isStatic())
1900 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
1901 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
aslc200b112008-08-16 12:57:46 +00001902
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001903 // Add source line info if available.
1904 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1905
1906 // Add to map.
1907 Slot = SubprogramDie;
aslc200b112008-08-16 12:57:46 +00001908
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001909 // Add to context owner.
1910 Unit->getDie()->AddChild(SubprogramDie);
aslc200b112008-08-16 12:57:46 +00001911
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001912 // Expose as global.
1913 Unit->AddGlobal(FullName, SubprogramDie);
aslc200b112008-08-16 12:57:46 +00001914
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001915 return SubprogramDie;
1916 }
1917
1918 /// NewScopeVariable - Create a new scope variable.
1919 ///
1920 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1921 // Get the descriptor.
1922 VariableDesc *VD = DV->getDesc();
1923
1924 // Translate tag to proper Dwarf tag. The result variable is dropped for
1925 // now.
1926 unsigned Tag;
1927 switch (VD->getTag()) {
1928 case DW_TAG_return_variable: return NULL;
1929 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1930 case DW_TAG_auto_variable: // fall thru
1931 default: Tag = DW_TAG_variable; break;
1932 }
1933
1934 // Define variable debug information entry.
1935 DIE *VariableDie = new DIE(Tag);
1936 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
1937
1938 // Add source line info if available.
1939 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
aslc200b112008-08-16 12:57:46 +00001940
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001941 // Add variable type.
aslc200b112008-08-16 12:57:46 +00001942 AddType(VariableDie, VD->getType(), Unit);
1943
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001944 // Add variable address.
1945 MachineLocation Location;
Evan Cheng38948832008-01-31 03:37:28 +00001946 Location.set(RI->getFrameRegister(*MF),
1947 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001948 AddAddress(VariableDie, DW_AT_location, Location);
1949
1950 return VariableDie;
1951 }
1952
1953 /// ConstructScope - Construct the components of a scope.
1954 ///
1955 void ConstructScope(DebugScope *ParentScope,
1956 unsigned ParentStartID, unsigned ParentEndID,
1957 DIE *ParentDie, CompileUnit *Unit) {
1958 // Add variables to scope.
1959 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1960 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1961 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1962 if (VariableDie) ParentDie->AddChild(VariableDie);
1963 }
aslc200b112008-08-16 12:57:46 +00001964
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001965 // Add nested scopes.
1966 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1967 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1968 // Define the Scope debug information entry.
1969 DebugScope *Scope = Scopes[j];
1970 // FIXME - Ignore inlined functions for the time being.
1971 if (!Scope->getParent()) continue;
aslc200b112008-08-16 12:57:46 +00001972
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001973 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1974 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1975
1976 // Ignore empty scopes.
1977 if (StartID == EndID && StartID != 0) continue;
1978 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
aslc200b112008-08-16 12:57:46 +00001979
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001980 if (StartID == ParentStartID && EndID == ParentEndID) {
1981 // Just add stuff to the parent scope.
1982 ConstructScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
1983 } else {
1984 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
aslc200b112008-08-16 12:57:46 +00001985
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001986 // Add the scope bounds.
1987 if (StartID) {
1988 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1989 DWLabel("label", StartID));
1990 } else {
1991 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1992 DWLabel("func_begin", SubprogramCount));
1993 }
1994 if (EndID) {
1995 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1996 DWLabel("label", EndID));
1997 } else {
1998 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1999 DWLabel("func_end", SubprogramCount));
2000 }
aslc200b112008-08-16 12:57:46 +00002001
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002002 // Add the scope contents.
2003 ConstructScope(Scope, StartID, EndID, ScopeDie, Unit);
2004 ParentDie->AddChild(ScopeDie);
2005 }
2006 }
2007 }
2008
2009 /// ConstructRootScope - Construct the scope for the subprogram.
2010 ///
2011 void ConstructRootScope(DebugScope *RootScope) {
2012 // Exit if there is no root scope.
2013 if (!RootScope) return;
aslc200b112008-08-16 12:57:46 +00002014
2015 // Get the subprogram debug information entry.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002016 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
aslc200b112008-08-16 12:57:46 +00002017
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002018 // Get the compile unit context.
2019 CompileUnit *Unit = GetBaseCompileUnit();
aslc200b112008-08-16 12:57:46 +00002020
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002021 // Get the subprogram die.
2022 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2023 assert(SPDie && "Missing subprogram descriptor");
aslc200b112008-08-16 12:57:46 +00002024
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002025 // Add the function bounds.
2026 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2027 DWLabel("func_begin", SubprogramCount));
2028 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2029 DWLabel("func_end", SubprogramCount));
2030 MachineLocation Location(RI->getFrameRegister(*MF));
2031 AddAddress(SPDie, DW_AT_frame_base, Location);
2032
2033 ConstructScope(RootScope, 0, 0, SPDie, Unit);
2034 }
2035
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002036 /// ConstructDefaultScope - Construct a default scope for the subprogram.
2037 ///
2038 void ConstructDefaultScope(MachineFunction *MF) {
2039 // Find the correct subprogram descriptor.
2040 std::vector<SubprogramDesc *> Subprograms;
2041 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
2042
2043 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2044 SubprogramDesc *SPD = Subprograms[i];
2045
2046 if (SPD->getName() == MF->getFunction()->getName()) {
2047 // Get the compile unit context.
2048 CompileUnit *Unit = GetBaseCompileUnit();
2049
2050 // Get the subprogram die.
2051 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2052 assert(SPDie && "Missing subprogram descriptor");
2053
2054 // Add the function bounds.
2055 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2056 DWLabel("func_begin", SubprogramCount));
2057 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2058 DWLabel("func_end", SubprogramCount));
2059
2060 MachineLocation Location(RI->getFrameRegister(*MF));
2061 AddAddress(SPDie, DW_AT_frame_base, Location);
2062 return;
2063 }
2064 }
Bill Wendlingae6b98c2008-10-17 18:48:57 +00002065#if 0
2066 // FIXME: This is causing an abort because C++ mangled names are compared
2067 // with their unmangled counterparts. See PR2885. Don't do this assert.
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002068 assert(0 && "Couldn't find DIE for machine function!");
Bill Wendlingae6b98c2008-10-17 18:48:57 +00002069#endif
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002070 }
2071
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002072 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2073 /// tools to recognize the object file contains Dwarf information.
2074 void EmitInitial() {
2075 // Check to see if we already emitted intial headers.
2076 if (didInitial) return;
2077 didInitial = true;
aslc200b112008-08-16 12:57:46 +00002078
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002079 // Dwarf sections base addresses.
2080 if (TAI->doesDwarfRequireFrameSection()) {
2081 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2082 EmitLabel("section_debug_frame", 0);
2083 }
2084 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2085 EmitLabel("section_info", 0);
2086 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2087 EmitLabel("section_abbrev", 0);
2088 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2089 EmitLabel("section_aranges", 0);
2090 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2091 EmitLabel("section_macinfo", 0);
2092 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2093 EmitLabel("section_line", 0);
2094 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2095 EmitLabel("section_loc", 0);
2096 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2097 EmitLabel("section_pubnames", 0);
2098 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2099 EmitLabel("section_str", 0);
2100 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2101 EmitLabel("section_ranges", 0);
2102
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002103 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002104 EmitLabel("text_begin", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00002105 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002106 EmitLabel("data_begin", 0);
2107 }
2108
2109 /// EmitDIE - Recusively Emits a debug information entry.
2110 ///
2111 void EmitDIE(DIE *Die) {
2112 // Get the abbreviation for this DIE.
2113 unsigned AbbrevNumber = Die->getAbbrevNumber();
2114 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
aslc200b112008-08-16 12:57:46 +00002115
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002116 Asm->EOL();
2117
2118 // Emit the code (index) for the abbreviation.
2119 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng0eeed442008-07-01 23:18:29 +00002120
2121 if (VerboseAsm)
2122 Asm->EOL(std::string("Abbrev [" +
2123 utostr(AbbrevNumber) +
2124 "] 0x" + utohexstr(Die->getOffset()) +
2125 ":0x" + utohexstr(Die->getSize()) + " " +
2126 TagString(Abbrev->getTag())));
2127 else
2128 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00002129
Owen Anderson88dd6232008-06-24 21:44:59 +00002130 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2131 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
aslc200b112008-08-16 12:57:46 +00002132
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002133 // Emit the DIE attribute values.
2134 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2135 unsigned Attr = AbbrevData[i].getAttribute();
2136 unsigned Form = AbbrevData[i].getForm();
2137 assert(Form && "Too many attributes for DIE (check abbreviation)");
aslc200b112008-08-16 12:57:46 +00002138
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002139 switch (Attr) {
2140 case DW_AT_sibling: {
2141 Asm->EmitInt32(Die->SiblingOffset());
2142 break;
2143 }
2144 default: {
2145 // Emit an attribute using the defined form.
2146 Values[i]->EmitValue(*this, Form);
2147 break;
2148 }
2149 }
aslc200b112008-08-16 12:57:46 +00002150
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002151 Asm->EOL(AttributeString(Attr));
2152 }
aslc200b112008-08-16 12:57:46 +00002153
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002154 // Emit the DIE children if any.
2155 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2156 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002157
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002158 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2159 EmitDIE(Children[j]);
2160 }
aslc200b112008-08-16 12:57:46 +00002161
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002162 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2163 }
2164 }
2165
2166 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2167 ///
2168 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2169 // Get the children.
2170 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002171
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002172 // If not last sibling and has children then add sibling offset attribute.
2173 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2174
2175 // Record the abbreviation.
2176 AssignAbbrevNumber(Die->getAbbrev());
aslc200b112008-08-16 12:57:46 +00002177
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002178 // Get the abbreviation for this DIE.
2179 unsigned AbbrevNumber = Die->getAbbrevNumber();
2180 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2181
2182 // Set DIE offset
2183 Die->setOffset(Offset);
aslc200b112008-08-16 12:57:46 +00002184
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002185 // Start the size with the size of abbreviation code.
aslc200b112008-08-16 12:57:46 +00002186 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2187
Owen Anderson88dd6232008-06-24 21:44:59 +00002188 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2189 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002190
2191 // Size the DIE attribute values.
2192 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2193 // Size attribute value.
2194 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2195 }
aslc200b112008-08-16 12:57:46 +00002196
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002197 // Size the DIE children if any.
2198 if (!Children.empty()) {
2199 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2200 "Children flag not set");
aslc200b112008-08-16 12:57:46 +00002201
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002202 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2203 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2204 }
aslc200b112008-08-16 12:57:46 +00002205
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002206 // End of children marker.
2207 Offset += sizeof(int8_t);
2208 }
2209
2210 Die->setSize(Offset - Die->getOffset());
2211 return Offset;
2212 }
2213
2214 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2215 ///
2216 void SizeAndOffsets() {
2217 // Process base compile unit.
2218 CompileUnit *Unit = GetBaseCompileUnit();
2219 // Compute size of compile unit header
2220 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2221 sizeof(int16_t) + // DWARF version number
2222 sizeof(int32_t) + // Offset Into Abbrev. Section
2223 sizeof(int8_t); // Pointer Size (in bytes)
2224 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2225 }
2226
2227 /// EmitDebugInfo - Emit the debug info section.
2228 ///
2229 void EmitDebugInfo() {
2230 // Start debug info section.
2231 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
aslc200b112008-08-16 12:57:46 +00002232
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002233 CompileUnit *Unit = GetBaseCompileUnit();
2234 DIE *Die = Unit->getDie();
2235 // Emit the compile units header.
2236 EmitLabel("info_begin", Unit->getID());
2237 // Emit size of content not including length itself
2238 unsigned ContentSize = Die->getSize() +
2239 sizeof(int16_t) + // DWARF version number
2240 sizeof(int32_t) + // Offset Into Abbrev. Section
2241 sizeof(int8_t) + // Pointer Size (in bytes)
2242 sizeof(int32_t); // FIXME - extra pad for gdb bug.
aslc200b112008-08-16 12:57:46 +00002243
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002244 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2245 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2246 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2247 Asm->EOL("Offset Into Abbrev. Section");
Dan Gohmancfb72b22007-09-27 23:12:31 +00002248 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
aslc200b112008-08-16 12:57:46 +00002249
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002250 EmitDIE(Die);
2251 // FIXME - extra padding for gdb bug.
2252 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2253 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2254 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2255 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2256 EmitLabel("info_end", Unit->getID());
aslc200b112008-08-16 12:57:46 +00002257
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002258 Asm->EOL();
2259 }
2260
2261 /// EmitAbbreviations - Emit the abbreviation section.
2262 ///
2263 void EmitAbbreviations() const {
2264 // Check to see if it is worth the effort.
2265 if (!Abbreviations.empty()) {
2266 // Start the debug abbrev section.
2267 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
aslc200b112008-08-16 12:57:46 +00002268
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002269 EmitLabel("abbrev_begin", 0);
aslc200b112008-08-16 12:57:46 +00002270
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002271 // For each abbrevation.
2272 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2273 // Get abbreviation data
2274 const DIEAbbrev *Abbrev = Abbreviations[i];
aslc200b112008-08-16 12:57:46 +00002275
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002276 // Emit the abbrevations code (base 1 index.)
2277 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2278 Asm->EOL("Abbreviation Code");
aslc200b112008-08-16 12:57:46 +00002279
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002280 // Emit the abbreviations data.
2281 Abbrev->Emit(*this);
aslc200b112008-08-16 12:57:46 +00002282
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002283 Asm->EOL();
2284 }
aslc200b112008-08-16 12:57:46 +00002285
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002286 // Mark end of abbreviations.
2287 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2288
2289 EmitLabel("abbrev_end", 0);
aslc200b112008-08-16 12:57:46 +00002290
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002291 Asm->EOL();
2292 }
2293 }
2294
Bill Wendling1983a2a2008-07-20 00:11:19 +00002295 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2296 /// the line matrix.
aslc200b112008-08-16 12:57:46 +00002297 ///
Bill Wendling1983a2a2008-07-20 00:11:19 +00002298 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2299 // Define last address of section.
2300 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2301 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2302 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2303 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2304
2305 // Mark end of matrix.
2306 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2307 Asm->EmitULEB128Bytes(1); Asm->EOL();
2308 Asm->EmitInt8(1); Asm->EOL();
2309 }
2310
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002311 /// EmitDebugLines - Emit source line information.
2312 ///
2313 void EmitDebugLines() {
Bill Wendling1983a2a2008-07-20 00:11:19 +00002314 // If the target is using .loc/.file, the assembler will be emitting the
2315 // .debug_line table automatically.
2316 if (TAI->hasDotLocAndDotFile())
Dan Gohmanc55b34a2007-09-24 21:43:52 +00002317 return;
2318
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002319 // Minimum line delta, thus ranging from -10..(255-10).
2320 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2321 // Maximum line delta, thus ranging from -10..(255-10).
2322 const int MaxLineDelta = 255 + MinLineDelta;
2323
2324 // Start the dwarf line section.
2325 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
aslc200b112008-08-16 12:57:46 +00002326
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002327 // Construct the section header.
aslc200b112008-08-16 12:57:46 +00002328
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002329 EmitDifference("line_end", 0, "line_begin", 0, true);
2330 Asm->EOL("Length of Source Line Info");
2331 EmitLabel("line_begin", 0);
aslc200b112008-08-16 12:57:46 +00002332
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002333 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
aslc200b112008-08-16 12:57:46 +00002334
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002335 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2336 Asm->EOL("Prolog Length");
2337 EmitLabel("line_prolog_begin", 0);
aslc200b112008-08-16 12:57:46 +00002338
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002339 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2340
2341 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2342
2343 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
aslc200b112008-08-16 12:57:46 +00002344
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002345 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2346
2347 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
aslc200b112008-08-16 12:57:46 +00002348
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002349 // Line number standard opcode encodings argument count
2350 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2351 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2352 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2353 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2354 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2355 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2356 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2357 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2358 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2359
2360 const UniqueVector<std::string> &Directories = MMI->getDirectories();
Evan Cheng0eeed442008-07-01 23:18:29 +00002361 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002362
2363 // Emit directories.
2364 for (unsigned DirectoryID = 1, NDID = Directories.size();
2365 DirectoryID <= NDID; ++DirectoryID) {
2366 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
2367 }
2368 Asm->EmitInt8(0); Asm->EOL("End of directories");
aslc200b112008-08-16 12:57:46 +00002369
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002370 // Emit files.
2371 for (unsigned SourceID = 1, NSID = SourceFiles.size();
2372 SourceID <= NSID; ++SourceID) {
2373 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2374 Asm->EmitString(SourceFile.getName());
2375 Asm->EOL("Source");
2376 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
2377 Asm->EOL("Directory #");
2378 Asm->EmitULEB128Bytes(0);
2379 Asm->EOL("Mod date");
2380 Asm->EmitULEB128Bytes(0);
2381 Asm->EOL("File size");
2382 }
2383 Asm->EmitInt8(0); Asm->EOL("End of files");
aslc200b112008-08-16 12:57:46 +00002384
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002385 EmitLabel("line_prolog_end", 0);
aslc200b112008-08-16 12:57:46 +00002386
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002387 // A sequence for each text section.
Bill Wendling1983a2a2008-07-20 00:11:19 +00002388 unsigned SecSrcLinesSize = SectionSourceLines.size();
2389
2390 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002391 // Isolate current sections line info.
2392 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng0eeed442008-07-01 23:18:29 +00002393
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002394 if (VerboseAsm) {
2395 const Section* S = SectionMap[j + 1];
2396 Asm->EOL(std::string("Section ") + S->getName());
2397 } else
Evan Cheng0eeed442008-07-01 23:18:29 +00002398 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002399
2400 // Dwarf assumes we start with first line of first source file.
2401 unsigned Source = 1;
2402 unsigned Line = 1;
aslc200b112008-08-16 12:57:46 +00002403
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002404 // Construct rows of the address, source, line, column matrix.
2405 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2406 const SourceLineInfo &LineInfo = LineInfos[i];
2407 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2408 if (!LabelID) continue;
aslc200b112008-08-16 12:57:46 +00002409
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002410 unsigned SourceID = LineInfo.getSourceID();
2411 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2412 unsigned DirectoryID = SourceFile.getDirectoryID();
Evan Cheng0eeed442008-07-01 23:18:29 +00002413 if (VerboseAsm)
2414 Asm->EOL(Directories[DirectoryID]
2415 + SourceFile.getName()
2416 + ":"
2417 + utostr_32(LineInfo.getLine()));
2418 else
2419 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002420
2421 // Define the line address.
2422 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohmancfb72b22007-09-27 23:12:31 +00002423 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002424 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2425 EmitReference("label", LabelID); Asm->EOL("Location label");
aslc200b112008-08-16 12:57:46 +00002426
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002427 // If change of source, then switch to the new source.
2428 if (Source != LineInfo.getSourceID()) {
2429 Source = LineInfo.getSourceID();
2430 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2431 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2432 }
aslc200b112008-08-16 12:57:46 +00002433
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002434 // If change of line.
2435 if (Line != LineInfo.getLine()) {
2436 // Determine offset.
2437 int Offset = LineInfo.getLine() - Line;
2438 int Delta = Offset - MinLineDelta;
aslc200b112008-08-16 12:57:46 +00002439
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002440 // Update line.
2441 Line = LineInfo.getLine();
aslc200b112008-08-16 12:57:46 +00002442
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002443 // If delta is small enough and in range...
2444 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2445 // ... then use fast opcode.
2446 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2447 } else {
2448 // ... otherwise use long hand.
2449 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2450 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2451 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2452 }
2453 } else {
2454 // Copy the previous row (different address or source)
2455 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2456 }
2457 }
2458
Bill Wendling1983a2a2008-07-20 00:11:19 +00002459 EmitEndOfLineMatrix(j + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002460 }
Bill Wendling1983a2a2008-07-20 00:11:19 +00002461
2462 if (SecSrcLinesSize == 0)
2463 // Because we're emitting a debug_line section, we still need a line
2464 // table. The linker and friends expect it to exist. If there's nothing to
2465 // put into it, emit an empty table.
2466 EmitEndOfLineMatrix(1);
aslc200b112008-08-16 12:57:46 +00002467
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002468 EmitLabel("line_end", 0);
aslc200b112008-08-16 12:57:46 +00002469
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002470 Asm->EOL();
2471 }
aslc200b112008-08-16 12:57:46 +00002472
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002473 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2474 ///
2475 void EmitCommonDebugFrame() {
2476 if (!TAI->doesDwarfRequireFrameSection())
2477 return;
2478
2479 int stackGrowth =
2480 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2481 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00002482 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002483
2484 // Start the dwarf frame section.
2485 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2486
2487 EmitLabel("debug_frame_common", 0);
2488 EmitDifference("debug_frame_common_end", 0,
2489 "debug_frame_common_begin", 0, true);
2490 Asm->EOL("Length of Common Information Entry");
2491
2492 EmitLabel("debug_frame_common_begin", 0);
2493 Asm->EmitInt32((int)DW_CIE_ID);
2494 Asm->EOL("CIE Identifier Tag");
2495 Asm->EmitInt8(DW_CIE_VERSION);
2496 Asm->EOL("CIE Version");
2497 Asm->EmitString("");
2498 Asm->EOL("CIE Augmentation");
2499 Asm->EmitULEB128Bytes(1);
2500 Asm->EOL("CIE Code Alignment Factor");
2501 Asm->EmitSLEB128Bytes(stackGrowth);
aslc200b112008-08-16 12:57:46 +00002502 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00002503 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002504 Asm->EOL("CIE RA Column");
aslc200b112008-08-16 12:57:46 +00002505
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002506 std::vector<MachineMove> Moves;
2507 RI->getInitialFrameState(Moves);
2508
Dale Johannesenf5a11532007-11-13 19:13:01 +00002509 EmitFrameMoves(NULL, 0, Moves, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002510
Evan Cheng7e7d1942008-02-29 19:36:59 +00002511 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002512 EmitLabel("debug_frame_common_end", 0);
aslc200b112008-08-16 12:57:46 +00002513
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002514 Asm->EOL();
2515 }
2516
2517 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2518 /// section.
2519 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
2520 if (!TAI->doesDwarfRequireFrameSection())
2521 return;
aslc200b112008-08-16 12:57:46 +00002522
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002523 // Start the dwarf frame section.
2524 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
aslc200b112008-08-16 12:57:46 +00002525
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002526 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2527 "debug_frame_begin", DebugFrameInfo.Number, true);
2528 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00002529
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002530 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2531
2532 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2533 0, 0, true, false);
2534 Asm->EOL("FDE CIE offset");
2535
2536 EmitReference("func_begin", DebugFrameInfo.Number);
2537 Asm->EOL("FDE initial location");
2538 EmitDifference("func_end", DebugFrameInfo.Number,
2539 "func_begin", DebugFrameInfo.Number);
2540 Asm->EOL("FDE address range");
aslc200b112008-08-16 12:57:46 +00002541
Dale Johannesenf5a11532007-11-13 19:13:01 +00002542 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves, false);
aslc200b112008-08-16 12:57:46 +00002543
Evan Cheng7e7d1942008-02-29 19:36:59 +00002544 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002545 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2546
2547 Asm->EOL();
2548 }
2549
2550 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2551 ///
2552 void EmitDebugPubNames() {
2553 // Start the dwarf pubnames section.
2554 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
aslc200b112008-08-16 12:57:46 +00002555
2556 CompileUnit *Unit = GetBaseCompileUnit();
2557
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002558 EmitDifference("pubnames_end", Unit->getID(),
2559 "pubnames_begin", Unit->getID(), true);
2560 Asm->EOL("Length of Public Names Info");
aslc200b112008-08-16 12:57:46 +00002561
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002562 EmitLabel("pubnames_begin", Unit->getID());
aslc200b112008-08-16 12:57:46 +00002563
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002564 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2565
2566 EmitSectionOffset("info_begin", "section_info",
2567 Unit->getID(), 0, true, false);
2568 Asm->EOL("Offset of Compilation Unit Info");
2569
2570 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
2571 Asm->EOL("Compilation Unit Length");
aslc200b112008-08-16 12:57:46 +00002572
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002573 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
aslc200b112008-08-16 12:57:46 +00002574
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002575 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2576 GE = Globals.end();
2577 GI != GE; ++GI) {
2578 const std::string &Name = GI->first;
2579 DIE * Entity = GI->second;
aslc200b112008-08-16 12:57:46 +00002580
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002581 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2582 Asm->EmitString(Name); Asm->EOL("External Name");
2583 }
aslc200b112008-08-16 12:57:46 +00002584
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002585 Asm->EmitInt32(0); Asm->EOL("End Mark");
2586 EmitLabel("pubnames_end", Unit->getID());
aslc200b112008-08-16 12:57:46 +00002587
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002588 Asm->EOL();
2589 }
2590
2591 /// EmitDebugStr - Emit visible names into a debug str section.
2592 ///
2593 void EmitDebugStr() {
2594 // Check to see if it is worth the effort.
2595 if (!StringPool.empty()) {
2596 // Start the dwarf str section.
2597 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
aslc200b112008-08-16 12:57:46 +00002598
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002599 // For each of strings in the string pool.
2600 for (unsigned StringID = 1, N = StringPool.size();
2601 StringID <= N; ++StringID) {
2602 // Emit a label for reference from debug information entries.
2603 EmitLabel("string", StringID);
2604 // Emit the string itself.
2605 const std::string &String = StringPool[StringID];
2606 Asm->EmitString(String); Asm->EOL();
2607 }
aslc200b112008-08-16 12:57:46 +00002608
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002609 Asm->EOL();
2610 }
2611 }
2612
2613 /// EmitDebugLoc - Emit visible names into a debug loc section.
2614 ///
2615 void EmitDebugLoc() {
2616 // Start the dwarf loc section.
2617 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
aslc200b112008-08-16 12:57:46 +00002618
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002619 Asm->EOL();
2620 }
2621
2622 /// EmitDebugARanges - Emit visible names into a debug aranges section.
2623 ///
2624 void EmitDebugARanges() {
2625 // Start the dwarf aranges section.
2626 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
aslc200b112008-08-16 12:57:46 +00002627
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002628 // FIXME - Mock up
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002629#if 0
aslc200b112008-08-16 12:57:46 +00002630 CompileUnit *Unit = GetBaseCompileUnit();
2631
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002632 // Don't include size of length
2633 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
aslc200b112008-08-16 12:57:46 +00002634
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002635 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
aslc200b112008-08-16 12:57:46 +00002636
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002637 EmitReference("info_begin", Unit->getID());
2638 Asm->EOL("Offset of Compilation Unit Info");
2639
Dan Gohmancfb72b22007-09-27 23:12:31 +00002640 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002641
2642 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2643
2644 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2645 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2646
2647 // Range 1
2648 EmitReference("text_begin", 0); Asm->EOL("Address");
2649 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2650
2651 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2652 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002653#endif
aslc200b112008-08-16 12:57:46 +00002654
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002655 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002656 }
2657
2658 /// EmitDebugRanges - Emit visible names into a debug ranges section.
2659 ///
2660 void EmitDebugRanges() {
2661 // Start the dwarf ranges section.
2662 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
aslc200b112008-08-16 12:57:46 +00002663
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002664 Asm->EOL();
2665 }
2666
2667 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2668 ///
2669 void EmitDebugMacInfo() {
2670 // Start the dwarf macinfo section.
2671 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
aslc200b112008-08-16 12:57:46 +00002672
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002673 Asm->EOL();
2674 }
2675
2676 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2677 /// header file.
2678 void ConstructCompileUnitDIEs() {
2679 const UniqueVector<CompileUnitDesc *> CUW = MMI->getCompileUnits();
aslc200b112008-08-16 12:57:46 +00002680
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002681 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
2682 unsigned ID = MMI->RecordSource(CUW[i]);
2683 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
2684 CompileUnits.push_back(Unit);
2685 }
2686 }
2687
2688 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
2689 /// global variables.
2690 void ConstructGlobalDIEs() {
Bill Wendling75091f92008-07-03 23:13:02 +00002691 std::vector<GlobalVariableDesc *> GlobalVariables;
2692 MMI->getAnchoredDescriptors<GlobalVariableDesc>(*M, GlobalVariables);
aslc200b112008-08-16 12:57:46 +00002693
Bill Wendling4de8de52008-07-03 22:53:42 +00002694 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
2695 GlobalVariableDesc *GVD = GlobalVariables[i];
2696 NewGlobalVariable(GVD);
2697 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002698 }
2699
2700 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2701 /// subprograms.
2702 void ConstructSubprogramDIEs() {
Bill Wendling75091f92008-07-03 23:13:02 +00002703 std::vector<SubprogramDesc *> Subprograms;
2704 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
aslc200b112008-08-16 12:57:46 +00002705
Bill Wendling4de8de52008-07-03 22:53:42 +00002706 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2707 SubprogramDesc *SPD = Subprograms[i];
2708 NewSubprogram(SPD);
2709 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002710 }
2711
2712public:
2713 //===--------------------------------------------------------------------===//
2714 // Main entry points.
2715 //
Owen Anderson847b99b2008-08-21 00:14:44 +00002716 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattnerb3876c72007-09-24 03:35:37 +00002717 : Dwarf(OS, A, T, "dbg")
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002718 , CompileUnits()
2719 , AbbreviationsSet(InitAbbreviationsSetSize)
2720 , Abbreviations()
2721 , ValuesSet(InitValuesSetSize)
2722 , Values()
2723 , StringPool()
2724 , DescToUnitMap()
2725 , SectionMap()
2726 , SectionSourceLines()
2727 , didInitial(false)
2728 , shouldEmit(false)
2729 {
2730 }
2731 virtual ~DwarfDebug() {
2732 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
2733 delete CompileUnits[i];
2734 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2735 delete Values[j];
2736 }
2737
2738 /// SetModuleInfo - Set machine module information when it's known that pass
2739 /// manager has created it. Set by the target AsmPrinter.
2740 void SetModuleInfo(MachineModuleInfo *mmi) {
2741 // Make sure initial declarations are made.
2742 if (!MMI && mmi->hasDebugInfo()) {
2743 MMI = mmi;
2744 shouldEmit = true;
aslc200b112008-08-16 12:57:46 +00002745
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002746 // Create all the compile unit DIEs.
2747 ConstructCompileUnitDIEs();
aslc200b112008-08-16 12:57:46 +00002748
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002749 // Create DIEs for each of the externally visible global variables.
2750 ConstructGlobalDIEs();
2751
2752 // Create DIEs for each of the externally visible subprograms.
2753 ConstructSubprogramDIEs();
aslc200b112008-08-16 12:57:46 +00002754
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002755 // Prime section data.
2756 SectionMap.insert(TAI->getTextSection());
Dan Gohman6d6c2402007-10-01 22:40:20 +00002757
2758 // Print out .file directives to specify files for .loc directives. These
2759 // are printed out early so that they precede any .loc directives.
2760 if (TAI->hasDotLocAndDotFile()) {
2761 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
2762 const UniqueVector<std::string> &Directories = MMI->getDirectories();
2763 for (unsigned i = 1, e = SourceFiles.size(); i <= e; ++i) {
2764 sys::Path FullPath(Directories[SourceFiles[i].getDirectoryID()]);
2765 bool AppendOk = FullPath.appendComponent(SourceFiles[i].getName());
2766 assert(AppendOk && "Could not append filename to directory!");
2767 Asm->EmitFile(i, FullPath.toString());
2768 Asm->EOL();
2769 }
2770 }
2771
2772 // Emit initial sections
2773 EmitInitial();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002774 }
2775 }
2776
2777 /// BeginModule - Emit all Dwarf sections that should come prior to the
2778 /// content.
2779 void BeginModule(Module *M) {
2780 this->M = M;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002781 }
2782
2783 /// EndModule - Emit all Dwarf sections that should come after the content.
2784 ///
2785 void EndModule() {
2786 if (!ShouldEmitDwarf()) return;
aslc200b112008-08-16 12:57:46 +00002787
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002788 // Standard sections final addresses.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002789 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002790 EmitLabel("text_end", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00002791 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002792 EmitLabel("data_end", 0);
aslc200b112008-08-16 12:57:46 +00002793
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002794 // End text sections.
2795 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002796 Asm->SwitchToSection(SectionMap[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002797 EmitLabel("section_end", i);
2798 }
2799
2800 // Emit common frame information.
2801 EmitCommonDebugFrame();
2802
2803 // Emit function debug frame information
2804 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2805 E = DebugFrames.end(); I != E; ++I)
2806 EmitFunctionDebugFrame(*I);
2807
2808 // Compute DIE offsets and sizes.
2809 SizeAndOffsets();
aslc200b112008-08-16 12:57:46 +00002810
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002811 // Emit all the DIEs into a debug info section
2812 EmitDebugInfo();
aslc200b112008-08-16 12:57:46 +00002813
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002814 // Corresponding abbreviations into a abbrev section.
2815 EmitAbbreviations();
aslc200b112008-08-16 12:57:46 +00002816
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002817 // Emit source line correspondence into a debug line section.
2818 EmitDebugLines();
aslc200b112008-08-16 12:57:46 +00002819
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002820 // Emit info into a debug pubnames section.
2821 EmitDebugPubNames();
aslc200b112008-08-16 12:57:46 +00002822
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002823 // Emit info into a debug str section.
2824 EmitDebugStr();
aslc200b112008-08-16 12:57:46 +00002825
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002826 // Emit info into a debug loc section.
2827 EmitDebugLoc();
aslc200b112008-08-16 12:57:46 +00002828
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002829 // Emit info into a debug aranges section.
2830 EmitDebugARanges();
aslc200b112008-08-16 12:57:46 +00002831
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002832 // Emit info into a debug ranges section.
2833 EmitDebugRanges();
aslc200b112008-08-16 12:57:46 +00002834
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002835 // Emit info into a debug macinfo section.
2836 EmitDebugMacInfo();
2837 }
2838
aslc200b112008-08-16 12:57:46 +00002839 /// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002840 /// emitted immediately after the function entry point.
2841 void BeginFunction(MachineFunction *MF) {
2842 this->MF = MF;
aslc200b112008-08-16 12:57:46 +00002843
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002844 if (!ShouldEmitDwarf()) return;
2845
2846 // Begin accumulating function debug information.
2847 MMI->BeginFunction(MF);
aslc200b112008-08-16 12:57:46 +00002848
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002849 // Assumes in correct section after the entry point.
2850 EmitLabel("func_begin", ++SubprogramCount);
Evan Chenga53c40a2008-02-01 09:10:45 +00002851
2852 // Emit label for the implicitly defined dbg.stoppoint at the start of
2853 // the function.
Andrew Lenharth42f91402008-04-03 17:37:43 +00002854 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
2855 if (!LineInfos.empty()) {
2856 const SourceLineInfo &LineInfo = LineInfos[0];
2857 Asm->printLabel(LineInfo.getLabelID());
2858 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002859 }
aslc200b112008-08-16 12:57:46 +00002860
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002861 /// EndFunction - Gather and emit post-function debug information.
2862 ///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002863 void EndFunction(MachineFunction *MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002864 if (!ShouldEmitDwarf()) return;
aslc200b112008-08-16 12:57:46 +00002865
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002866 // Define end label for subprogram.
2867 EmitLabel("func_end", SubprogramCount);
aslc200b112008-08-16 12:57:46 +00002868
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002869 // Get function line info.
2870 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
2871
2872 if (!LineInfos.empty()) {
2873 // Get section line info.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002874 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002875 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2876 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2877 // Append the function info to section info.
2878 SectionLineInfos.insert(SectionLineInfos.end(),
2879 LineInfos.begin(), LineInfos.end());
2880 }
aslc200b112008-08-16 12:57:46 +00002881
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002882 // Construct scopes for subprogram.
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002883 if (MMI->getRootScope())
2884 ConstructRootScope(MMI->getRootScope());
2885 else
2886 // FIXME: This is wrong. We are essentially getting past a problem with
2887 // debug information not being able to handle unreachable blocks that have
2888 // debug information in them. In particular, those unreachable blocks that
2889 // have "region end" info in them. That situation results in the "root
2890 // scope" not being created. If that's the case, then emit a "default"
2891 // scope, i.e., one that encompasses the whole function. This isn't
2892 // desirable. And a better way of handling this (and all of the debugging
2893 // information) needs to be explored.
2894 ConstructDefaultScope(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002895
2896 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2897 MMI->getFrameMoves()));
2898 }
2899};
2900
2901//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00002902/// DwarfException - Emits Dwarf exception handling directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002903///
2904class DwarfException : public Dwarf {
2905
2906private:
2907 struct FunctionEHFrameInfo {
2908 std::string FnName;
2909 unsigned Number;
2910 unsigned PersonalityIndex;
2911 bool hasCalls;
2912 bool hasLandingPads;
2913 std::vector<MachineMove> Moves;
Dale Johannesen3dadeb32008-01-16 19:59:28 +00002914 const Function * function;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002915
2916 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
2917 bool hC, bool hL,
Dale Johannesenfb3ac732007-11-20 23:24:42 +00002918 const std::vector<MachineMove> &M,
Dale Johannesen3dadeb32008-01-16 19:59:28 +00002919 const Function *f):
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002920 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00002921 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002922 };
2923
2924 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen85535762008-04-02 00:25:04 +00002925
2926 /// shouldEmitTable - Per-function flag to indicate if EH tables should
2927 /// be emitted.
2928 bool shouldEmitTable;
2929
2930 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
2931 /// should be emitted.
2932 bool shouldEmitMoves;
2933
2934 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
2935 /// should be emitted.
2936 bool shouldEmitTableModule;
2937
aslc200b112008-08-16 12:57:46 +00002938 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen85535762008-04-02 00:25:04 +00002939 /// should be emitted.
2940 bool shouldEmitMovesModule;
Duncan Sands96144f92008-05-07 19:11:09 +00002941
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002942 /// EmitCommonEHFrame - Emit the common eh unwind frame.
2943 ///
2944 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
2945 // Size and sign of stack growth.
2946 int stackGrowth =
2947 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2948 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00002949 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002950
2951 // Begin eh frame section.
2952 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
2953 O << "EH_frame" << Index << ":\n";
2954 EmitLabel("section_eh_frame", Index);
2955
2956 // Define base labels.
2957 EmitLabel("eh_frame_common", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00002958
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002959 // Define the eh frame length.
2960 EmitDifference("eh_frame_common_end", Index,
2961 "eh_frame_common_begin", Index, true);
2962 Asm->EOL("Length of Common Information Entry");
2963
2964 // EH frame header.
2965 EmitLabel("eh_frame_common_begin", Index);
2966 Asm->EmitInt32((int)0);
2967 Asm->EOL("CIE Identifier Tag");
2968 Asm->EmitInt8(DW_CIE_VERSION);
2969 Asm->EOL("CIE Version");
Duncan Sands96144f92008-05-07 19:11:09 +00002970
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002971 // The personality presence indicates that language specific information
2972 // will show up in the eh frame.
2973 Asm->EmitString(Personality ? "zPLR" : "zR");
2974 Asm->EOL("CIE Augmentation");
Duncan Sands96144f92008-05-07 19:11:09 +00002975
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002976 // Round out reader.
2977 Asm->EmitULEB128Bytes(1);
2978 Asm->EOL("CIE Code Alignment Factor");
2979 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands96144f92008-05-07 19:11:09 +00002980 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00002981 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands96144f92008-05-07 19:11:09 +00002982 Asm->EOL("CIE Return Address Column");
2983
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002984 // If there is a personality, we need to indicate the functions location.
2985 if (Personality) {
2986 Asm->EmitULEB128Bytes(7);
2987 Asm->EOL("Augmentation Size");
Bill Wendling2d369922007-09-11 17:20:55 +00002988
Duncan Sands96144f92008-05-07 19:11:09 +00002989 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendling2d369922007-09-11 17:20:55 +00002990 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands96144f92008-05-07 19:11:09 +00002991 Asm->EOL("Personality (pcrel sdata4 indirect)");
2992 } else {
Bill Wendling2d369922007-09-11 17:20:55 +00002993 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands96144f92008-05-07 19:11:09 +00002994 Asm->EOL("Personality (pcrel sdata4)");
2995 }
Bill Wendling2d369922007-09-11 17:20:55 +00002996
Duncan Sands96144f92008-05-07 19:11:09 +00002997 PrintRelDirective(true);
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00002998 O << TAI->getPersonalityPrefix();
2999 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3000 O << TAI->getPersonalitySuffix();
Duncan Sands4cc39532008-05-08 12:33:11 +00003001 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3002 O << "-" << TAI->getPCSymbol();
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003003 Asm->EOL("Personality");
Bill Wendling38cb7c92007-08-25 00:51:55 +00003004
Duncan Sands96144f92008-05-07 19:11:09 +00003005 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3006 Asm->EOL("LSDA Encoding (pcrel sdata4)");
3007 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3008 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003009 } else {
3010 Asm->EmitULEB128Bytes(1);
3011 Asm->EOL("Augmentation Size");
Duncan Sands96144f92008-05-07 19:11:09 +00003012 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3013 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003014 }
3015
3016 // Indicate locations of general callee saved registers in frame.
3017 std::vector<MachineMove> Moves;
3018 RI->getInitialFrameState(Moves);
Dale Johannesenf5a11532007-11-13 19:13:01 +00003019 EmitFrameMoves(NULL, 0, Moves, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003020
Dale Johannesen388f20f2008-04-30 00:43:29 +00003021 // On Darwin the linker honors the alignment of eh_frame, which means it
3022 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3023 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003024 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003025 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003026 EmitLabel("eh_frame_common_end", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003027
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003028 Asm->EOL();
3029 }
Duncan Sands96144f92008-05-07 19:11:09 +00003030
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003031 /// EmitEHFrame - Emit function exception frame information.
3032 ///
3033 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003034 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3035
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003036 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3037
3038 // Externally visible entry into the functions eh frame info.
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003039 // If the corresponding function is static, this should not be
3040 // externally visible.
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003041 if (linkage != Function::InternalLinkage) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003042 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3043 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3044 }
3045
Dale Johannesenf09b5992008-01-10 02:03:30 +00003046 // If corresponding function is weak definition, this should be too.
aslc200b112008-08-16 12:57:46 +00003047 if ((linkage == Function::WeakLinkage ||
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003048 linkage == Function::LinkOnceLinkage) &&
Dale Johannesenf09b5992008-01-10 02:03:30 +00003049 TAI->getWeakDefDirective())
3050 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3051
3052 // If there are no calls then you can't unwind. This may mean we can
3053 // omit the EH Frame, but some environments do not handle weak absolute
aslc200b112008-08-16 12:57:46 +00003054 // symbols.
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003055 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesena9b3e482008-04-08 00:10:24 +00003056 // unwind info is to be available for non-EH uses.
Dale Johannesenf09b5992008-01-10 02:03:30 +00003057 if (!EHFrameInfo.hasCalls &&
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003058 !UnwindTablesMandatory &&
aslc200b112008-08-16 12:57:46 +00003059 ((linkage != Function::WeakLinkage &&
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003060 linkage != Function::LinkOnceLinkage) ||
Dale Johannesenf09b5992008-01-10 02:03:30 +00003061 !TAI->getWeakDefDirective() ||
3062 TAI->getSupportsWeakOmittedEHFrame()))
aslc200b112008-08-16 12:57:46 +00003063 {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003064 O << EHFrameInfo.FnName << " = 0\n";
aslc200b112008-08-16 12:57:46 +00003065 // This name has no connection to the function, so it might get
3066 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003067 // dead-stripping unconditionally.
3068 if (const char *UsedDirective = TAI->getUsedDirective())
3069 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003070 } else {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003071 O << EHFrameInfo.FnName << ":\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003072
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003073 // EH frame header.
3074 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3075 "eh_frame_begin", EHFrameInfo.Number, true);
3076 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00003077
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003078 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
3079
3080 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3081 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
Dale Johannesen0ebb2432008-03-26 23:31:39 +00003082 true, true, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003083 Asm->EOL("FDE CIE offset");
3084
Duncan Sands96144f92008-05-07 19:11:09 +00003085 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003086 Asm->EOL("FDE initial location");
3087 EmitDifference("eh_func_end", EHFrameInfo.Number,
Duncan Sands96144f92008-05-07 19:11:09 +00003088 "eh_func_begin", EHFrameInfo.Number, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003089 Asm->EOL("FDE address range");
Duncan Sands96144f92008-05-07 19:11:09 +00003090
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003091 // If there is a personality and landing pads then point to the language
3092 // specific data area in the exception table.
3093 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands96144f92008-05-07 19:11:09 +00003094 Asm->EmitULEB128Bytes(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003095 Asm->EOL("Augmentation size");
Duncan Sands96144f92008-05-07 19:11:09 +00003096
3097 if (EHFrameInfo.hasLandingPads)
3098 EmitReference("exception", EHFrameInfo.Number, true, true);
3099 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003100 Asm->EmitInt32((int)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003101 Asm->EOL("Language Specific Data Area");
3102 } else {
3103 Asm->EmitULEB128Bytes(0);
3104 Asm->EOL("Augmentation size");
3105 }
Duncan Sands96144f92008-05-07 19:11:09 +00003106
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003107 // Indicate locations of function specific callee saved registers in
3108 // frame.
Dale Johannesenf5a11532007-11-13 19:13:01 +00003109 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves, true);
aslc200b112008-08-16 12:57:46 +00003110
Dale Johannesen388f20f2008-04-30 00:43:29 +00003111 // On Darwin the linker honors the alignment of eh_frame, which means it
3112 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3113 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003114 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003115 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003116 EmitLabel("eh_frame_end", EHFrameInfo.Number);
aslc200b112008-08-16 12:57:46 +00003117
3118 // If the function is marked used, this table should be also. We cannot
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003119 // make the mark unconditional in this case, since retaining the table
aslc200b112008-08-16 12:57:46 +00003120 // also retains the function in this case, and there is code around
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003121 // that depends on unused functions (calling undefined externals) being
3122 // dead-stripped to link correctly. Yes, there really is.
3123 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3124 if (const char *UsedDirective = TAI->getUsedDirective())
3125 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3126 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003127 }
3128
Duncan Sands241a0c92007-09-05 11:27:52 +00003129 /// EmitExceptionTable - Emit landing pads and actions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003130 ///
3131 /// The general organization of the table is complex, but the basic concepts
3132 /// are easy. First there is a header which describes the location and
3133 /// organization of the three components that follow.
3134 /// 1. The landing pad site information describes the range of code covered
3135 /// by the try. In our case it's an accumulation of the ranges covered
3136 /// by the invokes in the try. There is also a reference to the landing
3137 /// pad that handles the exception once processed. Finally an index into
3138 /// the actions table.
3139 /// 2. The action table, in our case, is composed of pairs of type ids
3140 /// and next action offset. Starting with the action index from the
3141 /// landing pad site, each type Id is checked for a match to the current
3142 /// exception. If it matches then the exception and type id are passed
3143 /// on to the landing pad. Otherwise the next action is looked up. This
3144 /// chain is terminated with a next action of zero. If no type id is
3145 /// found the the frame is unwound and handling continues.
3146 /// 3. Type id table contains references to all the C++ typeinfo for all
3147 /// catches in the function. This tables is reversed indexed base 1.
3148
3149 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3150 static unsigned SharedTypeIds(const LandingPadInfo *L,
3151 const LandingPadInfo *R) {
3152 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3153 unsigned LSize = LIds.size(), RSize = RIds.size();
3154 unsigned MinSize = LSize < RSize ? LSize : RSize;
3155 unsigned Count = 0;
3156
3157 for (; Count != MinSize; ++Count)
3158 if (LIds[Count] != RIds[Count])
3159 return Count;
3160
3161 return Count;
3162 }
3163
3164 /// PadLT - Order landing pads lexicographically by type id.
3165 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
3166 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3167 unsigned LSize = LIds.size(), RSize = RIds.size();
3168 unsigned MinSize = LSize < RSize ? LSize : RSize;
3169
3170 for (unsigned i = 0; i != MinSize; ++i)
3171 if (LIds[i] != RIds[i])
3172 return LIds[i] < RIds[i];
3173
3174 return LSize < RSize;
3175 }
3176
3177 struct KeyInfo {
3178 static inline unsigned getEmptyKey() { return -1U; }
3179 static inline unsigned getTombstoneKey() { return -2U; }
3180 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner92eea072007-09-17 18:34:04 +00003181 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003182 static bool isPod() { return true; }
3183 };
3184
Duncan Sands241a0c92007-09-05 11:27:52 +00003185 /// ActionEntry - Structure describing an entry in the actions table.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003186 struct ActionEntry {
3187 int ValueForTypeID; // The value to write - may not be equal to the type id.
3188 int NextAction;
3189 struct ActionEntry *Previous;
3190 };
3191
Duncan Sands241a0c92007-09-05 11:27:52 +00003192 /// PadRange - Structure holding a try-range and the associated landing pad.
3193 struct PadRange {
3194 // The index of the landing pad.
3195 unsigned PadIndex;
3196 // The index of the begin and end labels in the landing pad's label lists.
3197 unsigned RangeIndex;
3198 };
3199
3200 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3201
3202 /// CallSiteEntry - Structure describing an entry in the call-site table.
3203 struct CallSiteEntry {
Duncan Sands4ff179f2007-12-19 07:36:31 +00003204 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003205 unsigned BeginLabel; // zero indicates the start of the function.
3206 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003207 // The landing pad starts at PadLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003208 unsigned PadLabel; // zero indicates that there is no landing pad.
3209 unsigned Action;
3210 };
3211
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003212 void EmitExceptionTable() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003213 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
3214 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
3215 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3216 if (PadInfos.empty()) return;
3217
3218 // Sort the landing pads in order of their type ids. This is used to fold
3219 // duplicate actions.
3220 SmallVector<const LandingPadInfo *, 64> LandingPads;
3221 LandingPads.reserve(PadInfos.size());
3222 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
3223 LandingPads.push_back(&PadInfos[i]);
3224 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3225
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003226 // Negative type ids index into FilterIds, positive type ids index into
3227 // TypeInfos. The value written for a positive type id is just the type
3228 // id itself. For a negative type id, however, the value written is the
3229 // (negative) byte offset of the corresponding FilterIds entry. The byte
3230 // offset is usually equal to the type id, because the FilterIds entries
3231 // are written using a variable width encoding which outputs one byte per
3232 // entry as long as the value written is not too large, but can differ.
3233 // This kind of complication does not occur for positive type ids because
3234 // type infos are output using a fixed width encoding.
3235 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3236 SmallVector<int, 16> FilterOffsets;
3237 FilterOffsets.reserve(FilterIds.size());
3238 int Offset = -1;
3239 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3240 E = FilterIds.end(); I != E; ++I) {
3241 FilterOffsets.push_back(Offset);
aslc200b112008-08-16 12:57:46 +00003242 Offset -= TargetAsmInfo::getULEB128Size(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003243 }
3244
Duncan Sands241a0c92007-09-05 11:27:52 +00003245 // Compute the actions table and gather the first action index for each
3246 // landing pad site.
3247 SmallVector<ActionEntry, 32> Actions;
3248 SmallVector<unsigned, 64> FirstActions;
3249 FirstActions.reserve(LandingPads.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003250
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003251 int FirstAction = 0;
Duncan Sands241a0c92007-09-05 11:27:52 +00003252 unsigned SizeActions = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003253 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3254 const LandingPadInfo *LP = LandingPads[i];
3255 const std::vector<int> &TypeIds = LP->TypeIds;
3256 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
3257 unsigned SizeSiteActions = 0;
3258
3259 if (NumShared < TypeIds.size()) {
3260 unsigned SizeAction = 0;
3261 ActionEntry *PrevAction = 0;
3262
3263 if (NumShared) {
3264 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3265 assert(Actions.size());
3266 PrevAction = &Actions.back();
aslc200b112008-08-16 12:57:46 +00003267 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3268 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003269 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
aslc200b112008-08-16 12:57:46 +00003270 SizeAction -=
3271 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003272 SizeAction += -PrevAction->NextAction;
3273 PrevAction = PrevAction->Previous;
3274 }
3275 }
3276
3277 // Compute the actions.
3278 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3279 int TypeID = TypeIds[I];
3280 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3281 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
aslc200b112008-08-16 12:57:46 +00003282 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003283
3284 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
aslc200b112008-08-16 12:57:46 +00003285 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003286 SizeSiteActions += SizeAction;
3287
3288 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
3289 Actions.push_back(Action);
3290
3291 PrevAction = &Actions.back();
3292 }
3293
3294 // Record the first action of the landing pad site.
3295 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3296 } // else identical - re-use previous FirstAction
3297
3298 FirstActions.push_back(FirstAction);
3299
3300 // Compute this sites contribution to size.
3301 SizeActions += SizeSiteActions;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003302 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003303
Duncan Sands4ff179f2007-12-19 07:36:31 +00003304 // Compute the call-site table. The entry for an invoke has a try-range
3305 // containing the call, a non-zero landing pad and an appropriate action.
3306 // The entry for an ordinary call has a try-range containing the call and
3307 // zero for the landing pad and the action. Calls marked 'nounwind' have
3308 // no entry and must not be contained in the try-range of any entry - they
3309 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands241a0c92007-09-05 11:27:52 +00003310 SmallVector<CallSiteEntry, 64> CallSites;
3311
3312 RangeMapType PadMap;
Duncan Sands4ff179f2007-12-19 07:36:31 +00003313 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3314 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3315 // try-ranges for them need be deduced.
Duncan Sands241a0c92007-09-05 11:27:52 +00003316 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3317 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003318 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003319 unsigned BeginLabel = LandingPad->BeginLabels[j];
3320 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3321 PadRange P = { i, j };
3322 PadMap[BeginLabel] = P;
3323 }
3324 }
3325
Duncan Sands4ff179f2007-12-19 07:36:31 +00003326 // The end label of the previous invoke or nounwind try-range.
Duncan Sands241a0c92007-09-05 11:27:52 +00003327 unsigned LastLabel = 0;
Duncan Sands4ff179f2007-12-19 07:36:31 +00003328
3329 // Whether there is a potentially throwing instruction (currently this means
3330 // an ordinary call) between the end of the previous try-range and now.
3331 bool SawPotentiallyThrowing = false;
3332
3333 // Whether the last callsite entry was for an invoke.
3334 bool PreviousIsInvoke = false;
3335
Duncan Sands4ff179f2007-12-19 07:36:31 +00003336 // Visit all instructions in order of address.
Duncan Sands241a0c92007-09-05 11:27:52 +00003337 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3338 I != E; ++I) {
3339 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3340 MI != E; ++MI) {
Dan Gohmanfa607c92008-07-01 00:05:16 +00003341 if (!MI->isLabel()) {
Chris Lattner5b930372008-01-07 07:27:27 +00003342 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands241a0c92007-09-05 11:27:52 +00003343 continue;
3344 }
3345
Chris Lattnerda4cff12007-12-30 20:50:28 +00003346 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands241a0c92007-09-05 11:27:52 +00003347 assert(BeginLabel && "Invalid label!");
Duncan Sands89372f62007-09-05 14:12:46 +00003348
Duncan Sands4ff179f2007-12-19 07:36:31 +00003349 // End of the previous try-range?
Duncan Sands89372f62007-09-05 14:12:46 +00003350 if (BeginLabel == LastLabel)
Duncan Sands4ff179f2007-12-19 07:36:31 +00003351 SawPotentiallyThrowing = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003352
Duncan Sands4ff179f2007-12-19 07:36:31 +00003353 // Beginning of a new try-range?
Duncan Sands241a0c92007-09-05 11:27:52 +00003354 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands241a0c92007-09-05 11:27:52 +00003355 if (L == PadMap.end())
Duncan Sands4ff179f2007-12-19 07:36:31 +00003356 // Nope, it was just some random label.
Duncan Sands241a0c92007-09-05 11:27:52 +00003357 continue;
3358
3359 PadRange P = L->second;
3360 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3361
3362 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3363 "Inconsistent landing pad map!");
3364
3365 // If some instruction between the previous try-range and this one may
3366 // throw, create a call-site entry with no landing pad for the region
3367 // between the try-ranges.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003368 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003369 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3370 CallSites.push_back(Site);
Duncan Sands4ff179f2007-12-19 07:36:31 +00003371 PreviousIsInvoke = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003372 }
3373
3374 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003375 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands241a0c92007-09-05 11:27:52 +00003376
Duncan Sands4ff179f2007-12-19 07:36:31 +00003377 if (LandingPad->LandingPadLabel) {
3378 // This try-range is for an invoke.
3379 CallSiteEntry Site = {BeginLabel, LastLabel,
3380 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands241a0c92007-09-05 11:27:52 +00003381
Duncan Sands4ff179f2007-12-19 07:36:31 +00003382 // Try to merge with the previous call-site.
3383 if (PreviousIsInvoke) {
Dan Gohman3d436002008-06-21 22:00:54 +00003384 CallSiteEntry &Prev = CallSites.back();
Duncan Sands4ff179f2007-12-19 07:36:31 +00003385 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3386 // Extend the range of the previous entry.
3387 Prev.EndLabel = Site.EndLabel;
3388 continue;
3389 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003390 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003391
Duncan Sands4ff179f2007-12-19 07:36:31 +00003392 // Otherwise, create a new call-site.
3393 CallSites.push_back(Site);
3394 PreviousIsInvoke = true;
3395 } else {
3396 // Create a gap.
3397 PreviousIsInvoke = false;
3398 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003399 }
3400 }
3401 // If some instruction between the previous try-range and the end of the
3402 // function may throw, create a call-site entry with no landing pad for the
3403 // region following the try-range.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003404 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003405 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3406 CallSites.push_back(Site);
3407 }
3408
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003409 // Final tallies.
Duncan Sands96144f92008-05-07 19:11:09 +00003410
3411 // Call sites.
3412 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
3413 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
3414 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
3415 unsigned SizeSites = CallSites.size() * (SiteStartSize +
3416 SiteLengthSize +
3417 LandingPadSize);
Duncan Sands241a0c92007-09-05 11:27:52 +00003418 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
aslc200b112008-08-16 12:57:46 +00003419 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands241a0c92007-09-05 11:27:52 +00003420
Duncan Sands96144f92008-05-07 19:11:09 +00003421 // Type infos.
3422 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3423 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003424
3425 unsigned TypeOffset = sizeof(int8_t) + // Call site format
aslc200b112008-08-16 12:57:46 +00003426 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003427 SizeSites + SizeActions + SizeTypes;
3428
3429 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3430 sizeof(int8_t) + // TType format
aslc200b112008-08-16 12:57:46 +00003431 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003432 TypeOffset;
3433
3434 unsigned SizeAlign = (4 - TotalSize) & 3;
3435
3436 // Begin the exception table.
3437 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng7e7d1942008-02-29 19:36:59 +00003438 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesen841e4982008-10-08 21:50:21 +00003439 O << "GCC_except_table" << SubprogramCount << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003440 for (unsigned i = 0; i != SizeAlign; ++i) {
3441 Asm->EmitInt8(0);
3442 Asm->EOL("Padding");
3443 }
3444 EmitLabel("exception", SubprogramCount);
3445
3446 // Emit the header.
3447 Asm->EmitInt8(DW_EH_PE_omit);
3448 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3449 Asm->EmitInt8(DW_EH_PE_absptr);
3450 Asm->EOL("TType format (DW_EH_PE_absptr)");
3451 Asm->EmitULEB128Bytes(TypeOffset);
3452 Asm->EOL("TType base offset");
3453 Asm->EmitInt8(DW_EH_PE_udata4);
3454 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3455 Asm->EmitULEB128Bytes(SizeSites);
3456 Asm->EOL("Call-site table length");
3457
Duncan Sands241a0c92007-09-05 11:27:52 +00003458 // Emit the landing pad site information.
3459 for (unsigned i = 0; i < CallSites.size(); ++i) {
3460 CallSiteEntry &S = CallSites[i];
3461 const char *BeginTag;
3462 unsigned BeginNumber;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003463
Duncan Sands241a0c92007-09-05 11:27:52 +00003464 if (!S.BeginLabel) {
3465 BeginTag = "eh_func_begin";
3466 BeginNumber = SubprogramCount;
3467 } else {
3468 BeginTag = "label";
3469 BeginNumber = S.BeginLabel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003470 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003471
Duncan Sands241a0c92007-09-05 11:27:52 +00003472 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00003473 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003474 Asm->EOL("Region start");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003475
Duncan Sands241a0c92007-09-05 11:27:52 +00003476 if (!S.EndLabel) {
Dale Johannesen4670be42008-01-15 23:24:56 +00003477 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands96144f92008-05-07 19:11:09 +00003478 true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003479 } else {
Duncan Sands96144f92008-05-07 19:11:09 +00003480 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003481 }
3482 Asm->EOL("Region length");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003483
Duncan Sands96144f92008-05-07 19:11:09 +00003484 if (!S.PadLabel)
3485 Asm->EmitInt32(0);
3486 else
Duncan Sands241a0c92007-09-05 11:27:52 +00003487 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00003488 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003489 Asm->EOL("Landing pad");
3490
3491 Asm->EmitULEB128Bytes(S.Action);
3492 Asm->EOL("Action");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003493 }
3494
3495 // Emit the actions.
3496 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
3497 ActionEntry &Action = Actions[I];
3498
3499 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
3500 Asm->EOL("TypeInfo index");
3501 Asm->EmitSLEB128Bytes(Action.NextAction);
3502 Asm->EOL("Next action");
3503 }
3504
3505 // Emit the type ids.
3506 for (unsigned M = TypeInfos.size(); M; --M) {
3507 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00003508
3509 PrintRelDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003510
3511 if (GV)
3512 O << Asm->getGlobalLinkName(GV);
3513 else
3514 O << "0";
Duncan Sands241a0c92007-09-05 11:27:52 +00003515
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003516 Asm->EOL("TypeInfo");
3517 }
3518
3519 // Emit the filter typeids.
3520 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
3521 unsigned TypeID = FilterIds[j];
3522 Asm->EmitULEB128Bytes(TypeID);
3523 Asm->EOL("Filter TypeInfo index");
3524 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003525
Evan Cheng7e7d1942008-02-29 19:36:59 +00003526 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003527 }
3528
3529public:
3530 //===--------------------------------------------------------------------===//
3531 // Main entry points.
3532 //
Owen Anderson847b99b2008-08-21 00:14:44 +00003533 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattnerb3876c72007-09-24 03:35:37 +00003534 : Dwarf(OS, A, T, "eh")
Dale Johannesen85535762008-04-02 00:25:04 +00003535 , shouldEmitTable(false)
3536 , shouldEmitMoves(false)
3537 , shouldEmitTableModule(false)
3538 , shouldEmitMovesModule(false)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003539 {}
aslc200b112008-08-16 12:57:46 +00003540
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003541 virtual ~DwarfException() {}
3542
3543 /// SetModuleInfo - Set machine module information when it's known that pass
3544 /// manager has created it. Set by the target AsmPrinter.
3545 void SetModuleInfo(MachineModuleInfo *mmi) {
3546 MMI = mmi;
3547 }
3548
3549 /// BeginModule - Emit all exception information that should come prior to the
3550 /// content.
3551 void BeginModule(Module *M) {
3552 this->M = M;
3553 }
3554
3555 /// EndModule - Emit all exception information that should come after the
3556 /// content.
3557 void EndModule() {
Dale Johannesen85535762008-04-02 00:25:04 +00003558 if (shouldEmitMovesModule || shouldEmitTableModule) {
3559 const std::vector<Function *> Personalities = MMI->getPersonalities();
3560 for (unsigned i =0; i < Personalities.size(); ++i)
3561 EmitCommonEHFrame(Personalities[i], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003562
Dale Johannesen85535762008-04-02 00:25:04 +00003563 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
3564 E = EHFrames.end(); I != E; ++I)
3565 EmitEHFrame(*I);
3566 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003567 }
3568
aslc200b112008-08-16 12:57:46 +00003569 /// BeginFunction - Gather pre-function exception information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003570 /// emitted immediately after the function entry point.
3571 void BeginFunction(MachineFunction *MF) {
3572 this->MF = MF;
Dale Johannesen85535762008-04-02 00:25:04 +00003573 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen62f0a6d2008-04-02 17:04:45 +00003574 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen85535762008-04-02 00:25:04 +00003575
3576 // Map all labels and get rid of any dead landing pads.
3577 MMI->TidyLandingPads();
3578 // If any landing pads survive, we need an EH table.
3579 if (MMI->getLandingPads().size())
3580 shouldEmitTable = true;
3581
3582 // See if we need frame move info.
Duncan Sandscbc28b12008-07-04 09:55:48 +00003583 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen85535762008-04-02 00:25:04 +00003584 shouldEmitMoves = true;
3585
3586 if (shouldEmitMoves || shouldEmitTable)
3587 // Assumes in correct section after the entry point.
3588 EmitLabel("eh_func_begin", ++SubprogramCount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003589 }
Dale Johannesen85535762008-04-02 00:25:04 +00003590 shouldEmitTableModule |= shouldEmitTable;
3591 shouldEmitMovesModule |= shouldEmitMoves;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003592 }
3593
3594 /// EndFunction - Gather and emit post-function exception information.
3595 ///
3596 void EndFunction() {
Dale Johannesen85535762008-04-02 00:25:04 +00003597 if (shouldEmitMoves || shouldEmitTable) {
3598 EmitLabel("eh_func_end", SubprogramCount);
3599 EmitExceptionTable();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003600
Dale Johannesen85535762008-04-02 00:25:04 +00003601 // Save EH frame information
3602 EHFrames.
3603 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendlingef9211a2007-09-18 01:47:22 +00003604 SubprogramCount,
3605 MMI->getPersonalityIndex(),
3606 MF->getFrameInfo()->hasCalls(),
3607 !MMI->getLandingPads().empty(),
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003608 MMI->getFrameMoves(),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003609 MF->getFunction()));
Dale Johannesen85535762008-04-02 00:25:04 +00003610 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003611 }
3612};
3613
3614} // End of namespace llvm
3615
3616//===----------------------------------------------------------------------===//
3617
3618/// Emit - Print the abbreviation using the specified Dwarf writer.
3619///
3620void DIEAbbrev::Emit(const DwarfDebug &DD) const {
3621 // Emit its Dwarf tag type.
3622 DD.getAsm()->EmitULEB128Bytes(Tag);
3623 DD.getAsm()->EOL(TagString(Tag));
aslc200b112008-08-16 12:57:46 +00003624
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003625 // Emit whether it has children DIEs.
3626 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
3627 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
aslc200b112008-08-16 12:57:46 +00003628
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003629 // For each attribute description.
3630 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3631 const DIEAbbrevData &AttrData = Data[i];
aslc200b112008-08-16 12:57:46 +00003632
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003633 // Emit attribute type.
3634 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
3635 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
aslc200b112008-08-16 12:57:46 +00003636
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003637 // Emit form type.
3638 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
3639 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
3640 }
3641
3642 // Mark end of abbreviation.
3643 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
3644 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
3645}
3646
3647#ifndef NDEBUG
3648void DIEAbbrev::print(std::ostream &O) {
3649 O << "Abbreviation @"
3650 << std::hex << (intptr_t)this << std::dec
3651 << " "
3652 << TagString(Tag)
3653 << " "
3654 << ChildrenString(ChildrenFlag)
3655 << "\n";
aslc200b112008-08-16 12:57:46 +00003656
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003657 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3658 O << " "
3659 << AttributeString(Data[i].getAttribute())
3660 << " "
3661 << FormEncodingString(Data[i].getForm())
3662 << "\n";
3663 }
3664}
3665void DIEAbbrev::dump() { print(cerr); }
3666#endif
3667
3668//===----------------------------------------------------------------------===//
3669
3670#ifndef NDEBUG
3671void DIEValue::dump() {
3672 print(cerr);
3673}
3674#endif
3675
3676//===----------------------------------------------------------------------===//
3677
3678/// EmitValue - Emit integer of appropriate size.
3679///
3680void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
3681 switch (Form) {
3682 case DW_FORM_flag: // Fall thru
3683 case DW_FORM_ref1: // Fall thru
3684 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
3685 case DW_FORM_ref2: // Fall thru
3686 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
3687 case DW_FORM_ref4: // Fall thru
3688 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
3689 case DW_FORM_ref8: // Fall thru
3690 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
3691 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
3692 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
3693 default: assert(0 && "DIE Value form not supported yet"); break;
3694 }
3695}
3696
3697/// SizeOf - Determine size of integer value in bytes.
3698///
3699unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
3700 switch (Form) {
3701 case DW_FORM_flag: // Fall thru
3702 case DW_FORM_ref1: // Fall thru
3703 case DW_FORM_data1: return sizeof(int8_t);
3704 case DW_FORM_ref2: // Fall thru
3705 case DW_FORM_data2: return sizeof(int16_t);
3706 case DW_FORM_ref4: // Fall thru
3707 case DW_FORM_data4: return sizeof(int32_t);
3708 case DW_FORM_ref8: // Fall thru
3709 case DW_FORM_data8: return sizeof(int64_t);
aslc200b112008-08-16 12:57:46 +00003710 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
3711 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003712 default: assert(0 && "DIE Value form not supported yet"); break;
3713 }
3714 return 0;
3715}
3716
3717//===----------------------------------------------------------------------===//
3718
3719/// EmitValue - Emit string value.
3720///
3721void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
3722 DD.getAsm()->EmitString(String);
3723}
3724
3725//===----------------------------------------------------------------------===//
3726
3727/// EmitValue - Emit label value.
3728///
3729void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00003730 bool IsSmall = Form == DW_FORM_data4;
3731 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003732}
3733
3734/// SizeOf - Determine size of label value in bytes.
3735///
3736unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00003737 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00003738 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003739}
3740
3741//===----------------------------------------------------------------------===//
3742
3743/// EmitValue - Emit label value.
3744///
3745void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00003746 bool IsSmall = Form == DW_FORM_data4;
3747 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003748}
3749
3750/// SizeOf - Determine size of label value in bytes.
3751///
3752unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00003753 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00003754 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003755}
aslc200b112008-08-16 12:57:46 +00003756
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003757//===----------------------------------------------------------------------===//
3758
3759/// EmitValue - Emit delta value.
3760///
Argiris Kirtzidis03449652008-06-18 19:27:37 +00003761void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
3762 bool IsSmall = Form == DW_FORM_data4;
3763 DD.EmitSectionOffset(Label.Tag, Section.Tag,
3764 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
3765}
3766
3767/// SizeOf - Determine size of delta value in bytes.
3768///
3769unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
3770 if (Form == DW_FORM_data4) return 4;
3771 return DD.getTargetData()->getPointerSize();
3772}
aslc200b112008-08-16 12:57:46 +00003773
Argiris Kirtzidis03449652008-06-18 19:27:37 +00003774//===----------------------------------------------------------------------===//
3775
3776/// EmitValue - Emit delta value.
3777///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003778void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
3779 bool IsSmall = Form == DW_FORM_data4;
3780 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
3781}
3782
3783/// SizeOf - Determine size of delta value in bytes.
3784///
3785unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
3786 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00003787 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003788}
3789
3790//===----------------------------------------------------------------------===//
3791
3792/// EmitValue - Emit debug information entry offset.
3793///
3794void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
3795 DD.getAsm()->EmitInt32(Entry->getOffset());
3796}
aslc200b112008-08-16 12:57:46 +00003797
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003798//===----------------------------------------------------------------------===//
3799
3800/// ComputeSize - calculate the size of the block.
3801///
3802unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
3803 if (!Size) {
Owen Anderson88dd6232008-06-24 21:44:59 +00003804 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00003805
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003806 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
3807 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
3808 }
3809 }
3810 return Size;
3811}
3812
3813/// EmitValue - Emit block data.
3814///
3815void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
3816 switch (Form) {
3817 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
3818 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
3819 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
3820 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
3821 default: assert(0 && "Improper form for block"); break;
3822 }
aslc200b112008-08-16 12:57:46 +00003823
Owen Anderson88dd6232008-06-24 21:44:59 +00003824 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003825
3826 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
3827 DD.getAsm()->EOL();
3828 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
3829 }
3830}
3831
3832/// SizeOf - Determine size of block data in bytes.
3833///
3834unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
3835 switch (Form) {
3836 case DW_FORM_block1: return Size + sizeof(int8_t);
3837 case DW_FORM_block2: return Size + sizeof(int16_t);
3838 case DW_FORM_block4: return Size + sizeof(int32_t);
aslc200b112008-08-16 12:57:46 +00003839 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003840 default: assert(0 && "Improper form for block"); break;
3841 }
3842 return 0;
3843}
3844
3845//===----------------------------------------------------------------------===//
3846/// DIE Implementation
3847
3848DIE::~DIE() {
3849 for (unsigned i = 0, N = Children.size(); i < N; ++i)
3850 delete Children[i];
3851}
aslc200b112008-08-16 12:57:46 +00003852
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003853/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
3854///
3855void DIE::AddSiblingOffset() {
3856 DIEInteger *DI = new DIEInteger(0);
3857 Values.insert(Values.begin(), DI);
3858 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
3859}
3860
3861/// Profile - Used to gather unique data for the value folding set.
3862///
3863void DIE::Profile(FoldingSetNodeID &ID) {
3864 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00003865
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003866 for (unsigned i = 0, N = Children.size(); i < N; ++i)
3867 ID.AddPointer(Children[i]);
3868
3869 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3870 ID.AddPointer(Values[j]);
3871}
3872
3873#ifndef NDEBUG
3874void DIE::print(std::ostream &O, unsigned IncIndent) {
3875 static unsigned IndentCount = 0;
3876 IndentCount += IncIndent;
3877 const std::string Indent(IndentCount, ' ');
3878 bool isBlock = Abbrev.getTag() == 0;
aslc200b112008-08-16 12:57:46 +00003879
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003880 if (!isBlock) {
3881 O << Indent
3882 << "Die: "
3883 << "0x" << std::hex << (intptr_t)this << std::dec
3884 << ", Offset: " << Offset
3885 << ", Size: " << Size
aslc200b112008-08-16 12:57:46 +00003886 << "\n";
3887
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003888 O << Indent
3889 << TagString(Abbrev.getTag())
3890 << " "
3891 << ChildrenString(Abbrev.getChildrenFlag());
3892 } else {
3893 O << "Size: " << Size;
3894 }
3895 O << "\n";
3896
Owen Anderson88dd6232008-06-24 21:44:59 +00003897 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00003898
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003899 IndentCount += 2;
3900 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3901 O << Indent;
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00003902
3903 if (!isBlock)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003904 O << AttributeString(Data[i].getAttribute());
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00003905 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003906 O << "Blk[" << i << "]";
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00003907
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003908 O << " "
3909 << FormEncodingString(Data[i].getForm())
3910 << " ";
3911 Values[i]->print(O);
3912 O << "\n";
3913 }
3914 IndentCount -= 2;
3915
3916 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
3917 Children[j]->print(O, 4);
3918 }
aslc200b112008-08-16 12:57:46 +00003919
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003920 if (!isBlock) O << "\n";
3921 IndentCount -= IncIndent;
3922}
3923
3924void DIE::dump() {
3925 print(cerr);
3926}
3927#endif
3928
3929//===----------------------------------------------------------------------===//
3930/// DwarfWriter Implementation
3931///
3932
Owen Anderson847b99b2008-08-21 00:14:44 +00003933DwarfWriter::DwarfWriter(raw_ostream &OS, AsmPrinter *A,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003934 const TargetAsmInfo *T) {
3935 DE = new DwarfException(OS, A, T);
3936 DD = new DwarfDebug(OS, A, T);
3937}
3938
3939DwarfWriter::~DwarfWriter() {
3940 delete DE;
3941 delete DD;
3942}
3943
3944/// SetModuleInfo - Set machine module info when it's known that pass manager
3945/// has created it. Set by the target AsmPrinter.
3946void DwarfWriter::SetModuleInfo(MachineModuleInfo *MMI) {
3947 DD->SetModuleInfo(MMI);
3948 DE->SetModuleInfo(MMI);
3949}
3950
3951/// BeginModule - Emit all Dwarf sections that should come prior to the
3952/// content.
3953void DwarfWriter::BeginModule(Module *M) {
3954 DE->BeginModule(M);
3955 DD->BeginModule(M);
3956}
3957
3958/// EndModule - Emit all Dwarf sections that should come after the content.
3959///
3960void DwarfWriter::EndModule() {
3961 DE->EndModule();
3962 DD->EndModule();
3963}
3964
aslc200b112008-08-16 12:57:46 +00003965/// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003966/// emitted immediately after the function entry point.
3967void DwarfWriter::BeginFunction(MachineFunction *MF) {
3968 DE->BeginFunction(MF);
3969 DD->BeginFunction(MF);
3970}
3971
3972/// EndFunction - Gather and emit post-function debug information.
3973///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003974void DwarfWriter::EndFunction(MachineFunction *MF) {
3975 DD->EndFunction(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003976 DE->EndFunction();
aslc200b112008-08-16 12:57:46 +00003977
Bill Wendling5b4796a2008-07-22 00:53:37 +00003978 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003979 // Clear function debug information.
3980 MMI->EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003981}