blob: 8cb97313e3be7aed225af568fbeecb5984355707 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf info into asm files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/DwarfWriter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000015#include "llvm/Module.h"
Devang Patelb3907da2009-01-05 23:03:32 +000016#include "llvm/DerivedTypes.h"
Devang Patel2da0cc42009-01-15 23:41:32 +000017#include "llvm/Constants.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/CodeGen/AsmPrinter.h"
19#include "llvm/CodeGen/MachineModuleInfo.h"
20#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineLocation.h"
Devang Patelfc187162009-01-05 17:57:47 +000022#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/Support/Debug.h"
24#include "llvm/Support/Dwarf.h"
25#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/DataTypes.h"
27#include "llvm/Support/Mangler.h"
Bill Wendlingcb3661f2009-03-10 20:41:52 +000028#include "llvm/Support/Timer.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000029#include "llvm/Support/raw_ostream.h"
Dan Gohman80bbde72007-09-24 21:32:18 +000030#include "llvm/System/Path.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000032#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/Target/TargetData.h"
34#include "llvm/Target/TargetFrameInfo.h"
35#include "llvm/Target/TargetInstrInfo.h"
36#include "llvm/Target/TargetMachine.h"
37#include "llvm/Target/TargetOptions.h"
Evan Cheng3e288912009-02-25 07:04:34 +000038#include "llvm/ADT/DenseMap.h"
39#include "llvm/ADT/FoldingSet.h"
40#include "llvm/ADT/StringExtras.h"
41#include "llvm/ADT/StringMap.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042#include <ostream>
43#include <string>
44using namespace llvm;
45using namespace llvm::dwarf;
46
Devang Patelaa1e8432009-01-08 23:40:34 +000047static RegisterPass<DwarfWriter>
48X("dwarfwriter", "DWARF Information Writer");
49char DwarfWriter::ID = 0;
50
Bill Wendling148ecc42009-03-10 22:58:53 +000051static TimerGroup &getDwarfTimerGroup() {
52 static TimerGroup DwarfTimerGroup("Dwarf Exception and Debugging");
53 return DwarfTimerGroup;
Bill Wendlingcb3661f2009-03-10 20:41:52 +000054}
55
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056namespace llvm {
aslc200b112008-08-16 12:57:46 +000057
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058//===----------------------------------------------------------------------===//
59
60/// Configuration values for initial hash set sizes (log2).
61///
Bill Wendling824a8bf2009-02-03 21:17:20 +000062static const unsigned InitDiesSetSize = 9; // log2(512)
63static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
64static const unsigned InitValuesSetSize = 9; // log2(512)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065
66//===----------------------------------------------------------------------===//
67/// Forward declarations.
68///
69class DIE;
70class DIEValue;
71
72//===----------------------------------------------------------------------===//
73/// DWLabel - Labels are used to track locations in the assembler file.
aslc200b112008-08-16 12:57:46 +000074/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
75/// where the tag is a category of label (Ex. location) and number is a value
Reid Spencer37c7cea2007-08-05 20:06:04 +000076/// unique in that category.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077class DWLabel {
78public:
79 /// Tag - Label category tag. Should always be a staticly declared C string.
80 ///
81 const char *Tag;
aslc200b112008-08-16 12:57:46 +000082
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 /// Number - Value to make label unique.
84 ///
85 unsigned Number;
86
87 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
aslc200b112008-08-16 12:57:46 +000088
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 void Profile(FoldingSetNodeID &ID) const {
Evan Cheng3e288912009-02-25 07:04:34 +000090 ID.AddString(Tag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 ID.AddInteger(Number);
92 }
aslc200b112008-08-16 12:57:46 +000093
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094#ifndef NDEBUG
95 void print(std::ostream *O) const {
96 if (O) print(*O);
97 }
98 void print(std::ostream &O) const {
99 O << "." << Tag;
100 if (Number) O << Number;
101 }
102#endif
103};
104
105//===----------------------------------------------------------------------===//
106/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
107/// Dwarf abbreviation.
108class DIEAbbrevData {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 /// Attribute - Dwarf attribute code.
110 ///
111 unsigned Attribute;
aslc200b112008-08-16 12:57:46 +0000112
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 /// Form - Dwarf form code.
aslc200b112008-08-16 12:57:46 +0000114 ///
115 unsigned Form;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116public:
Bill Wendling15afa002009-03-10 23:57:09 +0000117 DIEAbbrevData(unsigned A, unsigned F) : Attribute(A), Form(F) {}
aslc200b112008-08-16 12:57:46 +0000118
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 // Accessors.
120 unsigned getAttribute() const { return Attribute; }
121 unsigned getForm() const { return Form; }
122
123 /// Profile - Used to gather unique data for the abbreviation folding set.
124 ///
125 void Profile(FoldingSetNodeID &ID)const {
126 ID.AddInteger(Attribute);
127 ID.AddInteger(Form);
128 }
129};
130
131//===----------------------------------------------------------------------===//
132/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
133/// information object.
134class DIEAbbrev : public FoldingSetNode {
135private:
136 /// Tag - Dwarf tag code.
137 ///
138 unsigned Tag;
aslc200b112008-08-16 12:57:46 +0000139
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 /// Unique number for node.
141 ///
142 unsigned Number;
143
144 /// ChildrenFlag - Dwarf children flag.
145 ///
146 unsigned ChildrenFlag;
147
148 /// Data - Raw data bytes for abbreviation.
149 ///
Owen Anderson88dd6232008-06-24 21:44:59 +0000150 SmallVector<DIEAbbrevData, 8> Data;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151public:
Bill Wendling15afa002009-03-10 23:57:09 +0000152 DIEAbbrev(unsigned T, unsigned C) : Tag(T), ChildrenFlag(C), Data() {}
153 virtual ~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)
Bill Wendling15afa002009-03-10 23:57:09 +0000227 : Abbrev(Tag, DW_CHILDREN_no), Offset(0), Size(0), Children(), Values() {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 virtual ~DIE();
aslc200b112008-08-16 12:57:46 +0000229
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 // Accessors.
231 DIEAbbrev &getAbbrev() { return Abbrev; }
232 unsigned getAbbrevNumber() const {
233 return Abbrev.getNumber();
234 }
235 unsigned getTag() const { return Abbrev.getTag(); }
236 unsigned getOffset() const { return Offset; }
237 unsigned getSize() const { return Size; }
238 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson88dd6232008-06-24 21:44:59 +0000239 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
241 void setOffset(unsigned O) { Offset = O; }
242 void setSize(unsigned S) { Size = S; }
aslc200b112008-08-16 12:57:46 +0000243
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 /// AddValue - Add a value and attributes to a DIE.
245 ///
246 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
247 Abbrev.AddAttribute(Attribute, Form);
248 Values.push_back(Value);
249 }
aslc200b112008-08-16 12:57:46 +0000250
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 /// SiblingOffset - Return the offset of the debug information entry's
252 /// sibling.
253 unsigned SiblingOffset() const { return Offset + Size; }
aslc200b112008-08-16 12:57:46 +0000254
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
256 ///
257 void AddSiblingOffset();
258
259 /// AddChild - Add a child to the DIE.
260 ///
261 void AddChild(DIE *Child) {
262 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
263 Children.push_back(Child);
264 }
aslc200b112008-08-16 12:57:46 +0000265
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 /// Detach - Detaches objects connected to it after copying.
267 ///
268 void Detach() {
269 Children.clear();
270 }
aslc200b112008-08-16 12:57:46 +0000271
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 /// Profile - Used to gather unique data for the value folding set.
273 ///
274 void Profile(FoldingSetNodeID &ID) ;
aslc200b112008-08-16 12:57:46 +0000275
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276#ifndef NDEBUG
277 void print(std::ostream *O, unsigned IncIndent = 0) {
278 if (O) print(*O, IncIndent);
279 }
280 void print(std::ostream &O, unsigned IncIndent = 0);
281 void dump();
282#endif
283};
284
285//===----------------------------------------------------------------------===//
286/// DIEValue - A debug information entry value.
287///
288class DIEValue : public FoldingSetNode {
289public:
290 enum {
291 isInteger,
292 isString,
293 isLabel,
294 isAsIsLabel,
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000295 isSectionOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 isDelta,
297 isEntry,
298 isBlock
299 };
aslc200b112008-08-16 12:57:46 +0000300
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 /// Type - Type of data stored in the value.
302 ///
303 unsigned Type;
aslc200b112008-08-16 12:57:46 +0000304
Bill Wendling15afa002009-03-10 23:57:09 +0000305 explicit DIEValue(unsigned T) : Type(T) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306 virtual ~DIEValue() {}
aslc200b112008-08-16 12:57:46 +0000307
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308 // Accessors
309 unsigned getType() const { return Type; }
aslc200b112008-08-16 12:57:46 +0000310
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311 // Implement isa/cast/dyncast.
312 static bool classof(const DIEValue *) { return true; }
aslc200b112008-08-16 12:57:46 +0000313
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 /// EmitValue - Emit value via the Dwarf writer.
315 ///
316 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
aslc200b112008-08-16 12:57:46 +0000317
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 /// SizeOf - Return the size of a value in bytes.
319 ///
320 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
aslc200b112008-08-16 12:57:46 +0000321
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322 /// Profile - Used to gather unique data for the value folding set.
323 ///
324 virtual void Profile(FoldingSetNodeID &ID) = 0;
aslc200b112008-08-16 12:57:46 +0000325
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326#ifndef NDEBUG
327 void print(std::ostream *O) {
328 if (O) print(*O);
329 }
330 virtual void print(std::ostream &O) = 0;
331 void dump();
332#endif
333};
334
335//===----------------------------------------------------------------------===//
336/// DWInteger - An integer value DIE.
aslc200b112008-08-16 12:57:46 +0000337///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338class DIEInteger : public DIEValue {
339private:
340 uint64_t Integer;
aslc200b112008-08-16 12:57:46 +0000341
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000342public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000343 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344
345 // Implement isa/cast/dyncast.
346 static bool classof(const DIEInteger *) { return true; }
347 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
aslc200b112008-08-16 12:57:46 +0000348
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 /// BestForm - Choose the best form for integer.
350 ///
351 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
352 if (IsSigned) {
353 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
354 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
355 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
356 } else {
357 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
358 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
359 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
360 }
361 return DW_FORM_data8;
362 }
aslc200b112008-08-16 12:57:46 +0000363
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364 /// EmitValue - Emit integer of appropriate size.
365 ///
366 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000367
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368 /// SizeOf - Determine size of integer value in bytes.
369 ///
370 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000371
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 /// Profile - Used to gather unique data for the value folding set.
373 ///
374 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
375 ID.AddInteger(isInteger);
376 ID.AddInteger(Integer);
377 }
378 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
aslc200b112008-08-16 12:57:46 +0000379
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380#ifndef NDEBUG
381 virtual void print(std::ostream &O) {
382 O << "Int: " << (int64_t)Integer
383 << " 0x" << std::hex << Integer << std::dec;
384 }
385#endif
386};
387
388//===----------------------------------------------------------------------===//
389/// DIEString - A string value DIE.
aslc200b112008-08-16 12:57:46 +0000390///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391class DIEString : public DIEValue {
Bill Wendling15afa002009-03-10 23:57:09 +0000392 const std::string Str;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000393public:
Bill Wendling15afa002009-03-10 23:57:09 +0000394 explicit DIEString(const std::string &S) : DIEValue(isString), Str(S) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395
396 // Implement isa/cast/dyncast.
397 static bool classof(const DIEString *) { return true; }
398 static bool classof(const DIEValue *S) { return S->Type == isString; }
aslc200b112008-08-16 12:57:46 +0000399
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400 /// EmitValue - Emit string value.
401 ///
402 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000403
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 /// SizeOf - Determine size of string value in bytes.
405 ///
406 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Bill Wendling15afa002009-03-10 23:57:09 +0000407 return Str.size() + sizeof(char); // sizeof('\0');
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000408 }
aslc200b112008-08-16 12:57:46 +0000409
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000410 /// Profile - Used to gather unique data for the value folding set.
411 ///
Bill Wendling15afa002009-03-10 23:57:09 +0000412 static void Profile(FoldingSetNodeID &ID, const std::string &Str) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413 ID.AddInteger(isString);
Bill Wendling15afa002009-03-10 23:57:09 +0000414 ID.AddString(Str);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415 }
Bill Wendling15afa002009-03-10 23:57:09 +0000416 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Str); }
aslc200b112008-08-16 12:57:46 +0000417
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418#ifndef NDEBUG
419 virtual void print(std::ostream &O) {
Bill Wendling15afa002009-03-10 23:57:09 +0000420 O << "Str: \"" << Str << "\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421 }
422#endif
423};
424
425//===----------------------------------------------------------------------===//
426/// DIEDwarfLabel - A Dwarf internal label expression DIE.
427//
428class DIEDwarfLabel : public DIEValue {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000429 const DWLabel Label;
Bill Wendling15afa002009-03-10 23:57:09 +0000430public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000431 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000432
433 // Implement isa/cast/dyncast.
434 static bool classof(const DIEDwarfLabel *) { return true; }
435 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
aslc200b112008-08-16 12:57:46 +0000436
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437 /// EmitValue - Emit label value.
438 ///
439 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000440
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441 /// SizeOf - Determine size of label value in bytes.
442 ///
443 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000444
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445 /// Profile - Used to gather unique data for the value folding set.
446 ///
447 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
448 ID.AddInteger(isLabel);
449 Label.Profile(ID);
450 }
451 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
aslc200b112008-08-16 12:57:46 +0000452
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453#ifndef NDEBUG
454 virtual void print(std::ostream &O) {
455 O << "Lbl: ";
456 Label.print(O);
457 }
458#endif
459};
460
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461//===----------------------------------------------------------------------===//
462/// DIEObjectLabel - A label to an object in code or data.
463//
464class DIEObjectLabel : public DIEValue {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465 const std::string Label;
Bill Wendling15afa002009-03-10 23:57:09 +0000466public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000467 explicit DIEObjectLabel(const std::string &L)
468 : DIEValue(isAsIsLabel), Label(L) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000469
470 // Implement isa/cast/dyncast.
471 static bool classof(const DIEObjectLabel *) { return true; }
472 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
aslc200b112008-08-16 12:57:46 +0000473
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000474 /// EmitValue - Emit label value.
475 ///
476 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000477
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 /// SizeOf - Determine size of label value in bytes.
479 ///
480 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000481
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000482 /// Profile - Used to gather unique data for the value folding set.
483 ///
484 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
485 ID.AddInteger(isAsIsLabel);
486 ID.AddString(Label);
487 }
Evan Cheng3e288912009-02-25 07:04:34 +0000488 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label.c_str()); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000489
490#ifndef NDEBUG
491 virtual void print(std::ostream &O) {
492 O << "Obj: " << Label;
493 }
494#endif
495};
496
497//===----------------------------------------------------------------------===//
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000498/// DIESectionOffset - A section offset DIE.
499//
500class DIESectionOffset : public DIEValue {
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000501 const DWLabel Label;
502 const DWLabel Section;
503 bool IsEH : 1;
504 bool UseSet : 1;
Bill Wendling15afa002009-03-10 23:57:09 +0000505public:
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000506 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
507 bool isEH = false, bool useSet = true)
Bill Wendling15afa002009-03-10 23:57:09 +0000508 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
509 IsEH(isEH), UseSet(useSet) {}
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000510
511 // Implement isa/cast/dyncast.
512 static bool classof(const DIESectionOffset *) { return true; }
513 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
aslc200b112008-08-16 12:57:46 +0000514
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000515 /// EmitValue - Emit section offset.
516 ///
517 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000518
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000519 /// SizeOf - Determine size of section offset value in bytes.
520 ///
521 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000522
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000523 /// Profile - Used to gather unique data for the value folding set.
524 ///
525 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
526 const DWLabel &Section) {
527 ID.AddInteger(isSectionOffset);
528 Label.Profile(ID);
529 Section.Profile(ID);
530 // IsEH and UseSet are specific to the Label/Section that we will emit
531 // the offset for; so Label/Section are enough for uniqueness.
532 }
533 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
534
535#ifndef NDEBUG
536 virtual void print(std::ostream &O) {
537 O << "Off: ";
538 Label.print(O);
539 O << "-";
540 Section.print(O);
541 O << "-" << IsEH << "-" << UseSet;
542 }
543#endif
544};
545
546//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547/// DIEDelta - A simple label difference DIE.
aslc200b112008-08-16 12:57:46 +0000548///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000549class DIEDelta : public DIEValue {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550 const DWLabel LabelHi;
551 const DWLabel LabelLo;
Bill Wendling15afa002009-03-10 23:57:09 +0000552public:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
Bill Wendling15afa002009-03-10 23:57:09 +0000554 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555
556 // Implement isa/cast/dyncast.
557 static bool classof(const DIEDelta *) { return true; }
558 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
aslc200b112008-08-16 12:57:46 +0000559
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560 /// EmitValue - Emit delta value.
561 ///
562 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000563
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564 /// SizeOf - Determine size of delta value in bytes.
565 ///
566 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000567
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 /// Profile - Used to gather unique data for the value folding set.
569 ///
570 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
571 const DWLabel &LabelLo) {
572 ID.AddInteger(isDelta);
573 LabelHi.Profile(ID);
574 LabelLo.Profile(ID);
575 }
576 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
577
578#ifndef NDEBUG
579 virtual void print(std::ostream &O) {
580 O << "Del: ";
581 LabelHi.print(O);
582 O << "-";
583 LabelLo.print(O);
584 }
585#endif
586};
587
588//===----------------------------------------------------------------------===//
589/// DIEntry - A pointer to another debug information entry. An instance of this
590/// class can also be used as a proxy for a debug information entry not yet
591/// defined (ie. types.)
592class DIEntry : public DIEValue {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000593 DIE *Entry;
Bill Wendling15afa002009-03-10 23:57:09 +0000594public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000595 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
aslc200b112008-08-16 12:57:46 +0000596
Bill Wendling15afa002009-03-10 23:57:09 +0000597 void setEntry(DIE *E) { Entry = E; }
598
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000599 // Implement isa/cast/dyncast.
600 static bool classof(const DIEntry *) { return true; }
601 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
aslc200b112008-08-16 12:57:46 +0000602
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603 /// EmitValue - Emit debug information entry offset.
604 ///
605 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000606
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 /// SizeOf - Determine size of debug information entry in bytes.
608 ///
609 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
610 return sizeof(int32_t);
611 }
aslc200b112008-08-16 12:57:46 +0000612
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000613 /// Profile - Used to gather unique data for the value folding set.
614 ///
615 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
616 ID.AddInteger(isEntry);
617 ID.AddPointer(Entry);
618 }
619 virtual void Profile(FoldingSetNodeID &ID) {
620 ID.AddInteger(isEntry);
aslc200b112008-08-16 12:57:46 +0000621
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000622 if (Entry) {
623 ID.AddPointer(Entry);
624 } else {
625 ID.AddPointer(this);
626 }
627 }
aslc200b112008-08-16 12:57:46 +0000628
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629#ifndef NDEBUG
630 virtual void print(std::ostream &O) {
631 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
632 }
633#endif
634};
635
636//===----------------------------------------------------------------------===//
637/// DIEBlock - A block of values. Primarily used for location expressions.
638//
639class DIEBlock : public DIEValue, public DIE {
Bill Wendling15afa002009-03-10 23:57:09 +0000640 unsigned Size; // Size in bytes excluding size header.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641public:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642 DIEBlock()
Bill Wendling15afa002009-03-10 23:57:09 +0000643 : DIEValue(isBlock), DIE(0), Size(0) {}
644 virtual ~DIEBlock() {}
aslc200b112008-08-16 12:57:46 +0000645
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000646 // Implement isa/cast/dyncast.
647 static bool classof(const DIEBlock *) { return true; }
648 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
aslc200b112008-08-16 12:57:46 +0000649
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650 /// ComputeSize - calculate the size of the block.
651 ///
652 unsigned ComputeSize(DwarfDebug &DD);
aslc200b112008-08-16 12:57:46 +0000653
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000654 /// BestForm - Choose the best form for data.
655 ///
656 unsigned BestForm() const {
657 if ((unsigned char)Size == Size) return DW_FORM_block1;
658 if ((unsigned short)Size == Size) return DW_FORM_block2;
659 if ((unsigned int)Size == Size) return DW_FORM_block4;
660 return DW_FORM_block;
661 }
662
663 /// EmitValue - Emit block data.
664 ///
665 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000666
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000667 /// SizeOf - Determine size of block data in bytes.
668 ///
669 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000670
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000671 /// Profile - Used to gather unique data for the value folding set.
672 ///
673 virtual void Profile(FoldingSetNodeID &ID) {
674 ID.AddInteger(isBlock);
675 DIE::Profile(ID);
676 }
aslc200b112008-08-16 12:57:46 +0000677
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678#ifndef NDEBUG
679 virtual void print(std::ostream &O) {
680 O << "Blk: ";
681 DIE::print(O, 5);
682 }
683#endif
684};
685
686//===----------------------------------------------------------------------===//
687/// CompileUnit - This dwarf writer support class manages information associate
688/// with a source file.
689class CompileUnit {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000690 /// ID - File identifier for source.
691 ///
692 unsigned ID;
aslc200b112008-08-16 12:57:46 +0000693
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000694 /// Die - Compile unit debug information entry.
695 ///
696 DIE *Die;
aslc200b112008-08-16 12:57:46 +0000697
Devang Patel42f6bed2009-01-13 23:54:55 +0000698 /// GVToDieMap - Tracks the mapping of unit level debug informaton
699 /// variables to debug information entries.
Devang Patel56b1d132009-01-20 00:58:55 +0000700 std::map<GlobalVariable *, DIE *> GVToDieMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000701
Devang Patel42f6bed2009-01-13 23:54:55 +0000702 /// GVToDIEntryMap - Tracks the mapping of unit level debug informaton
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703 /// descriptors to debug information entries using a DIEntry proxy.
Devang Patel56b1d132009-01-20 00:58:55 +0000704 std::map<GlobalVariable *, DIEntry *> GVToDIEntryMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705
706 /// Globals - A map of globally visible named entities for this unit.
707 ///
Bill Wendlinge06da442009-04-09 21:49:15 +0000708 StringMap<DIE*> Globals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000709
710 /// DiesSet - Used to uniquely define dies within the compile unit.
711 ///
712 FoldingSet<DIE> DiesSet;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713public:
Devang Patelb3907da2009-01-05 23:03:32 +0000714 CompileUnit(unsigned I, DIE *D)
Devang Patel42f6bed2009-01-13 23:54:55 +0000715 : ID(I), Die(D), GVToDieMap(),
Devang Patel5302e672009-01-17 06:51:37 +0000716 GVToDIEntryMap(), Globals(), DiesSet(InitDiesSetSize)
Devang Patelb3907da2009-01-05 23:03:32 +0000717 {}
718
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000719 ~CompileUnit() {
720 delete Die;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 }
aslc200b112008-08-16 12:57:46 +0000722
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723 // Accessors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724 unsigned getID() const { return ID; }
725 DIE* getDie() const { return Die; }
Bill Wendlinge06da442009-04-09 21:49:15 +0000726 StringMap<DIE*> &getGlobals() { return Globals; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000727
728 /// hasContent - Return true if this compile unit has something to write out.
729 ///
730 bool hasContent() const {
731 return !Die->getChildren().empty();
732 }
733
734 /// AddGlobal - Add a new global entity to the compile unit.
735 ///
736 void AddGlobal(const std::string &Name, DIE *Die) {
737 Globals[Name] = Die;
738 }
aslc200b112008-08-16 12:57:46 +0000739
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000740 /// getDieMapSlotFor - Returns the debug information entry map slot for the
Devang Patel42f6bed2009-01-13 23:54:55 +0000741 /// specified debug variable.
Devang Patel4a4cbe72009-01-05 21:47:57 +0000742 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
743 return GVToDieMap[GV];
744 }
aslc200b112008-08-16 12:57:46 +0000745
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000746 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
Devang Patel42f6bed2009-01-13 23:54:55 +0000747 /// specified debug variable.
Devang Patel4a4cbe72009-01-05 21:47:57 +0000748 DIEntry *&getDIEntrySlotFor(GlobalVariable *GV) {
749 return GVToDIEntryMap[GV];
750 }
aslc200b112008-08-16 12:57:46 +0000751
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752 /// AddDie - Adds or interns the DIE to the compile unit.
753 ///
754 DIE *AddDie(DIE &Buffer) {
755 FoldingSetNodeID ID;
756 Buffer.Profile(ID);
757 void *Where;
758 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
aslc200b112008-08-16 12:57:46 +0000759
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000760 if (!Die) {
761 Die = new DIE(Buffer);
762 DiesSet.InsertNode(Die, Where);
763 this->Die->AddChild(Die);
764 Buffer.Detach();
765 }
aslc200b112008-08-16 12:57:46 +0000766
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000767 return Die;
768 }
769};
770
771//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +0000772/// Dwarf - Emits general Dwarf directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000773///
774class Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000775protected:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000776 //===--------------------------------------------------------------------===//
777 // Core attributes used by the Dwarf writer.
778 //
aslc200b112008-08-16 12:57:46 +0000779
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000780 //
781 /// O - Stream to .s file.
782 ///
Owen Anderson847b99b2008-08-21 00:14:44 +0000783 raw_ostream &O;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000784
785 /// Asm - Target of Dwarf emission.
786 ///
787 AsmPrinter *Asm;
aslc200b112008-08-16 12:57:46 +0000788
Bill Wendlingac9639d2008-07-01 23:34:48 +0000789 /// TAI - Target asm information.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000790 const TargetAsmInfo *TAI;
aslc200b112008-08-16 12:57:46 +0000791
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000792 /// TD - Target data.
793 const TargetData *TD;
aslc200b112008-08-16 12:57:46 +0000794
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000795 /// RI - Register Information.
Dan Gohman1e57df32008-02-10 18:45:23 +0000796 const TargetRegisterInfo *RI;
aslc200b112008-08-16 12:57:46 +0000797
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000798 /// M - Current module.
799 ///
800 Module *M;
aslc200b112008-08-16 12:57:46 +0000801
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000802 /// MF - Current machine function.
803 ///
804 MachineFunction *MF;
aslc200b112008-08-16 12:57:46 +0000805
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806 /// MMI - Collected machine module information.
807 ///
808 MachineModuleInfo *MMI;
aslc200b112008-08-16 12:57:46 +0000809
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000810 /// SubprogramCount - The running count of functions being compiled.
811 ///
812 unsigned SubprogramCount;
aslc200b112008-08-16 12:57:46 +0000813
Chris Lattnerb3876c72007-09-24 03:35:37 +0000814 /// Flavor - A unique string indicating what dwarf producer this is, used to
815 /// unique labels.
816 const char * const Flavor;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000817
818 unsigned SetCounter;
Owen Anderson847b99b2008-08-21 00:14:44 +0000819 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattnerb3876c72007-09-24 03:35:37 +0000820 const char *flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000821 : O(OS)
822 , Asm(A)
823 , TAI(T)
824 , TD(Asm->TM.getTargetData())
825 , RI(Asm->TM.getRegisterInfo())
826 , M(NULL)
827 , MF(NULL)
828 , MMI(NULL)
829 , SubprogramCount(0)
Chris Lattnerb3876c72007-09-24 03:35:37 +0000830 , Flavor(flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000831 , SetCounter(1)
832 {
833 }
834
835public:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000836 //===--------------------------------------------------------------------===//
837 // Accessors.
838 //
Bill Wendling4226f4f2009-04-10 00:00:25 +0000839 const AsmPrinter *getAsm() const { return Asm; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000840 MachineModuleInfo *getMMI() const { return MMI; }
841 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohmancfb72b22007-09-27 23:12:31 +0000842 const TargetData *getTargetData() const { return TD; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000843
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000844 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
845 const {
846 if (isInSection && TAI->getDwarfSectionOffsetDirective())
847 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohmancfb72b22007-09-27 23:12:31 +0000848 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000849 O << TAI->getData32bitsDirective();
850 else
851 O << TAI->getData64bitsDirective();
852 }
aslc200b112008-08-16 12:57:46 +0000853
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000854 /// PrintLabelName - Print label name in form used by Dwarf writer.
855 ///
856 void PrintLabelName(DWLabel Label) const {
857 PrintLabelName(Label.Tag, Label.Number);
858 }
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000859 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000860 O << TAI->getPrivateGlobalPrefix() << Tag;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000861 if (Number) O << Number;
862 }
aslc200b112008-08-16 12:57:46 +0000863
Chris Lattnerb3876c72007-09-24 03:35:37 +0000864 void PrintLabelName(const char *Tag, unsigned Number,
865 const char *Suffix) const {
866 O << TAI->getPrivateGlobalPrefix() << Tag;
867 if (Number) O << Number;
868 O << Suffix;
869 }
aslc200b112008-08-16 12:57:46 +0000870
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000871 /// EmitLabel - Emit location label for internal use by Dwarf.
872 ///
873 void EmitLabel(DWLabel Label) const {
874 EmitLabel(Label.Tag, Label.Number);
875 }
876 void EmitLabel(const char *Tag, unsigned Number) const {
877 PrintLabelName(Tag, Number);
878 O << ":\n";
879 }
aslc200b112008-08-16 12:57:46 +0000880
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000881 /// EmitReference - Emit a reference to a label.
882 ///
Dan Gohman4fd77742007-09-28 15:43:33 +0000883 void EmitReference(DWLabel Label, bool IsPCRelative = false,
884 bool Force32Bit = false) const {
885 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000886 }
887 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman4fd77742007-09-28 15:43:33 +0000888 bool IsPCRelative = false, bool Force32Bit = false) const {
889 PrintRelDirective(Force32Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000890 PrintLabelName(Tag, Number);
aslc200b112008-08-16 12:57:46 +0000891
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000892 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
893 }
Dan Gohman4fd77742007-09-28 15:43:33 +0000894 void EmitReference(const std::string &Name, bool IsPCRelative = false,
895 bool Force32Bit = false) const {
896 PrintRelDirective(Force32Bit);
aslc200b112008-08-16 12:57:46 +0000897
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000898 O << Name;
aslc200b112008-08-16 12:57:46 +0000899
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000900 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
901 }
902
903 /// EmitDifference - Emit the difference between two labels. Some
904 /// assemblers do not behave with absolute expressions with data directives,
905 /// so there is an option (needsSet) to use an intermediary set expression.
906 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
907 bool IsSmall = false) {
908 EmitDifference(LabelHi.Tag, LabelHi.Number,
909 LabelLo.Tag, LabelLo.Number,
910 IsSmall);
911 }
912 void EmitDifference(const char *TagHi, unsigned NumberHi,
913 const char *TagLo, unsigned NumberLo,
914 bool IsSmall = false) {
915 if (TAI->needsSet()) {
916 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +0000917 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000918 O << ",";
919 PrintLabelName(TagHi, NumberHi);
920 O << "-";
921 PrintLabelName(TagLo, NumberLo);
922 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000923
924 PrintRelDirective(IsSmall);
Chris Lattnerb3876c72007-09-24 03:35:37 +0000925 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000926 ++SetCounter;
927 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000928 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +0000929
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000930 PrintLabelName(TagHi, NumberHi);
931 O << "-";
932 PrintLabelName(TagLo, NumberLo);
933 }
934 }
935
936 void EmitSectionOffset(const char* Label, const char* Section,
937 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesen0ebb2432008-03-26 23:31:39 +0000938 bool IsSmall = false, bool isEH = false,
939 bool useSet = true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000940 bool printAbsolute = false;
Dale Johannesen0ebb2432008-03-26 23:31:39 +0000941 if (isEH)
942 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
943 else
944 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
945
946 if (TAI->needsSet() && useSet) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000947 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +0000948 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000949 O << ",";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000950 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000951
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000952 if (!printAbsolute) {
953 O << "-";
954 PrintLabelName(Section, SectionNumber);
aslc200b112008-08-16 12:57:46 +0000955 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000956 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000957
958 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +0000959
Chris Lattnerb3876c72007-09-24 03:35:37 +0000960 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000961 ++SetCounter;
962 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000963 PrintRelDirective(IsSmall, true);
aslc200b112008-08-16 12:57:46 +0000964
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000965 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000966
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000967 if (!printAbsolute) {
968 O << "-";
969 PrintLabelName(Section, SectionNumber);
970 }
aslc200b112008-08-16 12:57:46 +0000971 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000972 }
aslc200b112008-08-16 12:57:46 +0000973
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000974 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
975 /// frame.
976 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenf5a11532007-11-13 19:13:01 +0000977 const std::vector<MachineMove> &Moves, bool isEH) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000978 int stackGrowth =
979 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
980 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +0000981 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000982 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
983
984 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
985 const MachineMove &Move = Moves[i];
986 unsigned LabelID = Move.getLabelID();
aslc200b112008-08-16 12:57:46 +0000987
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000988 if (LabelID) {
989 LabelID = MMI->MappedLabel(LabelID);
aslc200b112008-08-16 12:57:46 +0000990
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991 // Throw out move if the label is invalid.
992 if (!LabelID) continue;
993 }
aslc200b112008-08-16 12:57:46 +0000994
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000995 const MachineLocation &Dst = Move.getDestination();
996 const MachineLocation &Src = Move.getSource();
aslc200b112008-08-16 12:57:46 +0000997
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000998 // Advance row if new location.
999 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1000 Asm->EmitInt8(DW_CFA_advance_loc4);
1001 Asm->EOL("DW_CFA_advance_loc4");
1002 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1003 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00001004
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001005 BaseLabelID = LabelID;
1006 BaseLabel = "label";
1007 IsLocal = true;
1008 }
aslc200b112008-08-16 12:57:46 +00001009
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001010 // If advancing cfa.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001011 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1012 if (!Src.isReg()) {
1013 if (Src.getReg() == MachineLocation::VirtualFP) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001014 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1015 Asm->EOL("DW_CFA_def_cfa_offset");
1016 } else {
1017 Asm->EmitInt8(DW_CFA_def_cfa);
1018 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001019 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001020 Asm->EOL("Register");
1021 }
aslc200b112008-08-16 12:57:46 +00001022
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001023 int Offset = -Src.getOffset();
aslc200b112008-08-16 12:57:46 +00001024
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001025 Asm->EmitULEB128Bytes(Offset);
1026 Asm->EOL("Offset");
1027 } else {
1028 assert(0 && "Machine move no supported yet.");
1029 }
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001030 } else if (Src.isReg() &&
1031 Src.getReg() == MachineLocation::VirtualFP) {
1032 if (Dst.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001033 Asm->EmitInt8(DW_CFA_def_cfa_register);
1034 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001035 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001036 Asm->EOL("Register");
1037 } else {
1038 assert(0 && "Machine move no supported yet.");
1039 }
1040 } else {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001041 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001042 int Offset = Dst.getOffset() / stackGrowth;
aslc200b112008-08-16 12:57:46 +00001043
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001044 if (Offset < 0) {
1045 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1046 Asm->EOL("DW_CFA_offset_extended_sf");
1047 Asm->EmitULEB128Bytes(Reg);
1048 Asm->EOL("Reg");
1049 Asm->EmitSLEB128Bytes(Offset);
1050 Asm->EOL("Offset");
1051 } else if (Reg < 64) {
1052 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Cheng42ceb472009-03-25 01:47:28 +00001053 if (Asm->isVerbose())
Evan Cheng6181e062008-07-09 21:53:02 +00001054 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1055 else
1056 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001057 Asm->EmitULEB128Bytes(Offset);
1058 Asm->EOL("Offset");
1059 } else {
1060 Asm->EmitInt8(DW_CFA_offset_extended);
1061 Asm->EOL("DW_CFA_offset_extended");
1062 Asm->EmitULEB128Bytes(Reg);
1063 Asm->EOL("Reg");
1064 Asm->EmitULEB128Bytes(Offset);
1065 Asm->EOL("Offset");
1066 }
1067 }
1068 }
1069 }
1070
1071};
1072
1073//===----------------------------------------------------------------------===//
Devang Patel35a078f2009-01-12 22:54:42 +00001074/// SrcLineInfo - This class is used to record source line correspondence.
Devang Patel7dd15a92009-01-08 17:19:22 +00001075///
1076class SrcLineInfo {
1077 unsigned Line; // Source line number.
1078 unsigned Column; // Source column.
1079 unsigned SourceID; // Source ID number.
1080 unsigned LabelID; // Label in code ID number.
1081public:
1082 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
Bill Wendling824a8bf2009-02-03 21:17:20 +00001083 : Line(L), Column(C), SourceID(S), LabelID(I) {}
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +00001084
Devang Patel7dd15a92009-01-08 17:19:22 +00001085 // Accessors
1086 unsigned getLine() const { return Line; }
1087 unsigned getColumn() const { return Column; }
1088 unsigned getSourceID() const { return SourceID; }
1089 unsigned getLabelID() const { return LabelID; }
1090};
1091
Devang Patel7dd15a92009-01-08 17:19:22 +00001092//===----------------------------------------------------------------------===//
Devang Patel4d1709e2009-01-08 02:33:41 +00001093/// DbgVariable - This class is used to track local variable information.
1094///
1095class DbgVariable {
Devang Patel7c8a2772009-01-16 19:28:14 +00001096 DIVariable Var; // Variable Descriptor.
Bill Wendlingd32c9722009-05-07 17:26:14 +00001097 unsigned FrameIndex; // Variable frame index.
Devang Patel4d1709e2009-01-08 02:33:41 +00001098public:
Devang Patel7c8a2772009-01-16 19:28:14 +00001099 DbgVariable(DIVariable V, unsigned I) : Var(V), FrameIndex(I) {}
Devang Patel4d1709e2009-01-08 02:33:41 +00001100
1101 // Accessors.
Devang Patel7c8a2772009-01-16 19:28:14 +00001102 DIVariable getVariable() const { return Var; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001103 unsigned getFrameIndex() const { return FrameIndex; }
1104};
1105
1106//===----------------------------------------------------------------------===//
1107/// DbgScope - This class is used to track scope information.
1108///
1109class DbgScope {
Devang Patel4d1709e2009-01-08 02:33:41 +00001110 DbgScope *Parent; // Parent to this scope.
Devang Patel49a3bd92009-01-16 18:01:58 +00001111 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel4d1709e2009-01-08 02:33:41 +00001112 // Either subprogram or block.
1113 unsigned StartLabelID; // Label ID of the beginning of scope.
1114 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel49a3bd92009-01-16 18:01:58 +00001115 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
Devang Patel63c22f42009-01-10 02:42:49 +00001116 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Devang Patel4d1709e2009-01-08 02:33:41 +00001117public:
Devang Patel2560d922009-01-15 18:25:17 +00001118 DbgScope(DbgScope *P, DIDescriptor D)
Devang Patel4d1709e2009-01-08 02:33:41 +00001119 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), Scopes(), Variables()
1120 {}
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001121 virtual ~DbgScope() {
Devang Patela4162952009-01-12 18:48:36 +00001122 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1123 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1124 }
Devang Patel4d1709e2009-01-08 02:33:41 +00001125
1126 // Accessors.
Devang Patel49a3bd92009-01-16 18:01:58 +00001127 DbgScope *getParent() const { return Parent; }
1128 DIDescriptor getDesc() const { return Desc; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001129 unsigned getStartLabelID() const { return StartLabelID; }
1130 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel63c22f42009-01-10 02:42:49 +00001131 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1132 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001133 void setStartLabelID(unsigned S) { StartLabelID = S; }
1134 void setEndLabelID(unsigned E) { EndLabelID = E; }
1135
1136 /// AddScope - Add a scope to the scope.
1137 ///
1138 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1139
1140 /// AddVariable - Add a variable to the scope.
1141 ///
1142 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001143
1144 virtual bool isInlinedSubroutine() { return false; }
Devang Patelda96ca52009-04-21 00:08:56 +00001145 virtual unsigned getLine() { assert ( 0 && "Unexpected scope!"); return 0; }
1146 virtual unsigned getColumn() { assert ( 0 && "Unexpected scope!"); return 0; }
1147 virtual unsigned getFile() { assert ( 0 && "Unexpected scope!"); return 0; }
Bill Wendling74940d12009-05-06 21:21:34 +00001148
1149#ifndef NDEBUG
1150 void dump() const;
1151#endif
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001152};
1153
Bill Wendling74940d12009-05-06 21:21:34 +00001154#ifndef NDEBUG
1155void DbgScope::dump() const {
1156 static unsigned IndentLevel = 0;
1157 std::string Indent(IndentLevel, ' ');
1158
1159 cerr << Indent; Desc.dump();
1160 cerr << " [" << StartLabelID << ", " << EndLabelID << "]\n";
1161
1162 IndentLevel += 2;
1163
1164 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
1165 if (Scopes[i] != this)
1166 Scopes[i]->dump();
1167
1168 IndentLevel -= 2;
1169}
1170#endif
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001171
1172//===----------------------------------------------------------------------===//
1173/// DbgInlinedSubroutineScope - This class is used to track inlined subroutine
1174/// scope information.
1175///
1176class DbgInlinedSubroutineScope : public DbgScope {
1177 unsigned Src;
1178 unsigned Line;
1179 unsigned Col;
1180public:
1181 DbgInlinedSubroutineScope(DbgScope *P, DIDescriptor D,
1182 unsigned S, unsigned L, unsigned C)
1183 : DbgScope(P, D), Src(S), Line(L), Col(C)
1184 {}
1185
1186 unsigned getLine() { return Line; }
1187 unsigned getColumn() { return Col; }
1188 unsigned getFile() { return Src; }
1189 bool isInlinedSubroutine() { return true; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001190};
1191
1192//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00001193/// DwarfDebug - Emits Dwarf debug directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001194///
1195class DwarfDebug : public Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001196 //===--------------------------------------------------------------------===//
1197 // Attributes used to construct specific Dwarf sections.
1198 //
aslc200b112008-08-16 12:57:46 +00001199
Evan Cheng3e288912009-02-25 07:04:34 +00001200 /// CompileUnitMap - A map of global variables representing compile units to
1201 /// compile units.
1202 DenseMap<Value *, CompileUnit *> CompileUnitMap;
1203
1204 /// CompileUnits - All the compile units in this module.
1205 ///
1206 SmallVector<CompileUnit *, 8> CompileUnits;
aslc200b112008-08-16 12:57:46 +00001207
Devang Patel2ae1db52009-01-30 18:20:31 +00001208 /// MainCU - Some platform prefers one compile unit per .o file. In such
1209 /// cases, all dies are inserted in MainCU.
1210 CompileUnit *MainCU;
Bill Wendlinge0f3a262009-02-20 20:40:28 +00001211
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001212 /// AbbreviationsSet - Used to uniquely define abbreviations.
1213 ///
1214 FoldingSet<DIEAbbrev> AbbreviationsSet;
1215
1216 /// Abbreviations - A list of all the unique abbreviations in use.
1217 ///
1218 std::vector<DIEAbbrev *> Abbreviations;
aslc200b112008-08-16 12:57:46 +00001219
Evan Cheng3e288912009-02-25 07:04:34 +00001220 /// DirectoryIdMap - Directory name to directory id map.
1221 ///
1222 StringMap<unsigned> DirectoryIdMap;
Devang Patel5f244e32009-01-05 22:35:52 +00001223
Evan Cheng3e288912009-02-25 07:04:34 +00001224 /// DirectoryNames - A list of directory names.
1225 SmallVector<std::string, 8> DirectoryNames;
1226
1227 /// SourceFileIdMap - Source file name to source file id map.
1228 ///
1229 StringMap<unsigned> SourceFileIdMap;
1230
1231 /// SourceFileNames - A list of source file names.
1232 SmallVector<std::string, 8> SourceFileNames;
1233
1234 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
1235 /// id mapped to a unique id.
1236 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
1237
1238 /// SourceIds - Reverse map from source id to directory id + file id pair.
1239 ///
1240 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
Devang Patel5f244e32009-01-05 22:35:52 +00001241
Devang Patel9b829452009-01-16 21:07:53 +00001242 /// Lines - List of of source line correspondence.
Devang Patel7dd15a92009-01-08 17:19:22 +00001243 std::vector<SrcLineInfo> Lines;
1244
Devang Patel9b829452009-01-16 21:07:53 +00001245 /// ValuesSet - Used to uniquely define values.
1246 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001247 FoldingSet<DIEValue> ValuesSet;
aslc200b112008-08-16 12:57:46 +00001248
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001249 /// Values - A list of all the unique values in use.
1250 ///
1251 std::vector<DIEValue *> Values;
aslc200b112008-08-16 12:57:46 +00001252
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001253 /// StringPool - A UniqueVector of strings used by indirect references.
1254 ///
1255 UniqueVector<std::string> StringPool;
1256
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001257 /// SectionMap - Provides a unique id per text section.
1258 ///
Anton Korobeynikov55b94962008-09-24 22:15:21 +00001259 UniqueVector<const Section*> SectionMap;
aslc200b112008-08-16 12:57:46 +00001260
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001261 /// SectionSourceLines - Tracks line numbers per text section.
1262 ///
Devang Patel35a078f2009-01-12 22:54:42 +00001263 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001264
1265 /// didInitial - Flag to indicate if initial emission has been done.
1266 ///
1267 bool didInitial;
aslc200b112008-08-16 12:57:46 +00001268
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001269 /// shouldEmit - Flag to indicate if debug information should be emitted.
1270 ///
1271 bool shouldEmit;
1272
Devang Patel7b60d552009-04-15 20:41:31 +00001273 // FunctionDbgScope - Top level scope for the current function.
Devang Patel4d1709e2009-01-08 02:33:41 +00001274 //
Devang Patel7b60d552009-04-15 20:41:31 +00001275 DbgScope *FunctionDbgScope;
Devang Patel4d1709e2009-01-08 02:33:41 +00001276
Bill Wendlingd9308a62009-03-10 21:23:25 +00001277 /// DbgScopeMap - Tracks the scopes in the current function.
Devang Patel4d1709e2009-01-08 02:33:41 +00001278 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
Bill Wendlingd9308a62009-03-10 21:23:25 +00001279
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001280 /// DbgInlinedScopeMap - Tracks inlined scopes in the current function.
1281 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> > DbgInlinedScopeMap;
1282
Bill Wendlingd32c9722009-05-07 17:26:14 +00001283 /// InlineInfo - Keep track of inlined functions and their location. This
1284 /// information is used to populate debug_inlined section.
Devang Patel88bf96e2009-04-13 17:02:03 +00001285 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> > InlineInfo;
1286
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001287 /// InlinedVariableScopes - Scopes information for the inlined subroutine
1288 /// variables.
1289 DenseMap<const MachineInstr *, DbgScope *> InlinedVariableScopes;
1290
Bill Wendlingd32c9722009-05-07 17:26:14 +00001291 /// AbstractInstanceRootMap - Map of abstract instance roots of inlined
1292 /// functions. These are subroutine entries that contain a DW_AT_inline
1293 /// attribute.
1294 DenseMap<const GlobalVariable *, DbgScope *> AbstractInstanceRootMap;
1295
1296 /// AbstractInstanceRootList - List of abstract instance roots of inlined
1297 /// functions. These are subroutine entries that contain a DW_AT_inline
1298 /// attribute.
1299 SmallVector<DbgScope *, 32> AbstractInstanceRootList;
1300
1301 /// LexicalScopeToConcreteInstMap - Map a concrete instance's DIE to the
1302 /// lexical scope it's in.
1303 DenseMap<DbgScope *, DIE *> LexicalScopeToConcreteInstMap;
1304
1305 /// LexicalScopeStack - A stack of lexical scopes. The top one is the current
1306 /// scope.
1307 SmallVector<DbgScope *, 16> LexicalScopeStack;
1308
Bill Wendlingd9308a62009-03-10 21:23:25 +00001309 /// DebugTimer - Timer for the Dwarf debug writer.
1310 Timer *DebugTimer;
Devang Patel4d1709e2009-01-08 02:33:41 +00001311
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001312 struct FunctionDebugFrameInfo {
1313 unsigned Number;
1314 std::vector<MachineMove> Moves;
1315
1316 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman9ba5d4d2007-08-27 14:50:10 +00001317 Number(Num), Moves(M) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001318 };
1319
1320 std::vector<FunctionDebugFrameInfo> DebugFrames;
aslc200b112008-08-16 12:57:46 +00001321
Bill Wendlingd9308a62009-03-10 21:23:25 +00001322private:
Bill Wendlingdf25fd62009-03-10 21:59:25 +00001323 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
Bill Wendling278a3922009-03-10 21:47:45 +00001324 /// maps to the source id. Source id starts at 1.
1325 std::pair<unsigned, unsigned>
Bill Wendlingdf25fd62009-03-10 21:59:25 +00001326 getSourceDirectoryAndFileIds(unsigned SId) const {
Bill Wendling278a3922009-03-10 21:47:45 +00001327 return SourceIds[SId-1];
1328 }
1329
1330 /// getNumSourceDirectories - Return the number of source directories in the
1331 /// debug info.
1332 unsigned getNumSourceDirectories() const {
1333 return DirectoryNames.size();
1334 }
1335
1336 /// getSourceDirectoryName - Return the name of the directory corresponding
1337 /// to the id.
1338 const std::string &getSourceDirectoryName(unsigned Id) const {
1339 return DirectoryNames[Id - 1];
1340 }
1341
1342 /// getSourceFileName - Return the name of the source file corresponding
1343 /// to the id.
1344 const std::string &getSourceFileName(unsigned Id) const {
1345 return SourceFileNames[Id - 1];
1346 }
1347
1348 /// getNumSourceIds - Return the number of unique source ids.
Bill Wendling278a3922009-03-10 21:47:45 +00001349 unsigned getNumSourceIds() const {
1350 return SourceIds.size();
1351 }
1352
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001353 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
aslc200b112008-08-16 12:57:46 +00001354 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001355 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1356 // Profile the node so that we can make it unique.
1357 FoldingSetNodeID ID;
1358 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00001359
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001360 // Check the set for priors.
1361 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
aslc200b112008-08-16 12:57:46 +00001362
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001363 // If it's newly added.
1364 if (InSet == &Abbrev) {
aslc200b112008-08-16 12:57:46 +00001365 // Add to abbreviation list.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001366 Abbreviations.push_back(&Abbrev);
1367 // Assign the vector position + 1 as its number.
1368 Abbrev.setNumber(Abbreviations.size());
1369 } else {
1370 // Assign existing abbreviation number.
1371 Abbrev.setNumber(InSet->getNumber());
1372 }
1373 }
1374
1375 /// NewString - Add a string to the constant pool and returns a label.
1376 ///
1377 DWLabel NewString(const std::string &String) {
1378 unsigned StringID = StringPool.insert(String);
1379 return DWLabel("string", StringID);
1380 }
aslc200b112008-08-16 12:57:46 +00001381
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001382 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1383 /// entry.
1384 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1385 DIEntry *Value;
aslc200b112008-08-16 12:57:46 +00001386
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001387 if (Entry) {
1388 FoldingSetNodeID ID;
1389 DIEntry::Profile(ID, Entry);
1390 void *Where;
1391 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
aslc200b112008-08-16 12:57:46 +00001392
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001393 if (Value) return Value;
aslc200b112008-08-16 12:57:46 +00001394
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001395 Value = new DIEntry(Entry);
1396 ValuesSet.InsertNode(Value, Where);
1397 } else {
1398 Value = new DIEntry(Entry);
1399 }
aslc200b112008-08-16 12:57:46 +00001400
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001401 Values.push_back(Value);
1402 return Value;
1403 }
aslc200b112008-08-16 12:57:46 +00001404
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001405 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1406 ///
1407 void SetDIEntry(DIEntry *Value, DIE *Entry) {
Bill Wendling15afa002009-03-10 23:57:09 +00001408 Value->setEntry(Entry);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001409 // Add to values set if not already there. If it is, we merely have a
1410 // duplicate in the values list (no harm.)
1411 ValuesSet.GetOrInsertNode(Value);
1412 }
1413
1414 /// AddUInt - Add an unsigned integer attribute data and value.
1415 ///
1416 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1417 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1418
1419 FoldingSetNodeID ID;
1420 DIEInteger::Profile(ID, Integer);
1421 void *Where;
1422 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1423 if (!Value) {
1424 Value = new DIEInteger(Integer);
1425 ValuesSet.InsertNode(Value, Where);
1426 Values.push_back(Value);
1427 }
aslc200b112008-08-16 12:57:46 +00001428
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001429 Die->AddValue(Attribute, Form, Value);
1430 }
aslc200b112008-08-16 12:57:46 +00001431
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001432 /// AddSInt - Add an signed integer attribute data and value.
1433 ///
1434 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1435 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1436
1437 FoldingSetNodeID ID;
1438 DIEInteger::Profile(ID, (uint64_t)Integer);
1439 void *Where;
1440 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1441 if (!Value) {
1442 Value = new DIEInteger(Integer);
1443 ValuesSet.InsertNode(Value, Where);
1444 Values.push_back(Value);
1445 }
aslc200b112008-08-16 12:57:46 +00001446
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001447 Die->AddValue(Attribute, Form, Value);
1448 }
aslc200b112008-08-16 12:57:46 +00001449
Evan Cheng3e288912009-02-25 07:04:34 +00001450 /// AddString - Add a string attribute data and value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001451 ///
1452 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1453 const std::string &String) {
1454 FoldingSetNodeID ID;
1455 DIEString::Profile(ID, String);
1456 void *Where;
1457 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1458 if (!Value) {
1459 Value = new DIEString(String);
1460 ValuesSet.InsertNode(Value, Where);
1461 Values.push_back(Value);
1462 }
aslc200b112008-08-16 12:57:46 +00001463
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001464 Die->AddValue(Attribute, Form, Value);
1465 }
aslc200b112008-08-16 12:57:46 +00001466
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001467 /// AddLabel - Add a Dwarf label attribute data and value.
1468 ///
1469 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1470 const DWLabel &Label) {
1471 FoldingSetNodeID ID;
1472 DIEDwarfLabel::Profile(ID, Label);
1473 void *Where;
1474 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1475 if (!Value) {
1476 Value = new DIEDwarfLabel(Label);
1477 ValuesSet.InsertNode(Value, Where);
1478 Values.push_back(Value);
1479 }
aslc200b112008-08-16 12:57:46 +00001480
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001481 Die->AddValue(Attribute, Form, Value);
1482 }
aslc200b112008-08-16 12:57:46 +00001483
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001484 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1485 ///
1486 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1487 const std::string &Label) {
1488 FoldingSetNodeID ID;
1489 DIEObjectLabel::Profile(ID, Label);
1490 void *Where;
1491 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1492 if (!Value) {
1493 Value = new DIEObjectLabel(Label);
1494 ValuesSet.InsertNode(Value, Where);
1495 Values.push_back(Value);
1496 }
aslc200b112008-08-16 12:57:46 +00001497
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001498 Die->AddValue(Attribute, Form, Value);
1499 }
aslc200b112008-08-16 12:57:46 +00001500
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001501 /// AddSectionOffset - Add a section offset label attribute data and value.
1502 ///
1503 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1504 const DWLabel &Label, const DWLabel &Section,
1505 bool isEH = false, bool useSet = true) {
1506 FoldingSetNodeID ID;
1507 DIESectionOffset::Profile(ID, Label, Section);
1508 void *Where;
1509 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1510 if (!Value) {
1511 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1512 ValuesSet.InsertNode(Value, Where);
1513 Values.push_back(Value);
1514 }
aslc200b112008-08-16 12:57:46 +00001515
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001516 Die->AddValue(Attribute, Form, Value);
1517 }
aslc200b112008-08-16 12:57:46 +00001518
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001519 /// AddDelta - Add a label delta attribute data and value.
1520 ///
1521 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001522 const DWLabel &Hi, const DWLabel &Lo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001523 FoldingSetNodeID ID;
1524 DIEDelta::Profile(ID, Hi, Lo);
1525 void *Where;
1526 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1527 if (!Value) {
1528 Value = new DIEDelta(Hi, Lo);
1529 ValuesSet.InsertNode(Value, Where);
1530 Values.push_back(Value);
1531 }
aslc200b112008-08-16 12:57:46 +00001532
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001533 Die->AddValue(Attribute, Form, Value);
1534 }
aslc200b112008-08-16 12:57:46 +00001535
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001536 /// AddDIEntry - Add a DIE attribute data and value.
1537 ///
1538 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1539 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1540 }
1541
1542 /// AddBlock - Add block data.
1543 ///
1544 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1545 Block->ComputeSize(*this);
1546 FoldingSetNodeID ID;
1547 Block->Profile(ID);
1548 void *Where;
1549 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1550 if (!Value) {
1551 Value = Block;
1552 ValuesSet.InsertNode(Value, Where);
1553 Values.push_back(Value);
1554 } else {
Chris Lattner3de66892007-09-21 18:25:53 +00001555 // Already exists, reuse the previous one.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001556 delete Block;
Chris Lattner3de66892007-09-21 18:25:53 +00001557 Block = cast<DIEBlock>(Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001558 }
aslc200b112008-08-16 12:57:46 +00001559
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001560 Die->AddValue(Attribute, Block->BestForm(), Value);
1561 }
1562
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001563 /// AddSourceLine - Add location information to specified debug information
1564 /// entry.
Devang Patel7c8a2772009-01-16 19:28:14 +00001565 void AddSourceLine(DIE *Die, const DIVariable *V) {
Chris Lattner88ab9742009-05-05 04:55:56 +00001566 // If there is no compile unit specified, don't add a line #.
1567 if (V->getCompileUnit().isNull())
1568 return;
1569
Devang Patel4d1709e2009-01-08 02:33:41 +00001570 unsigned Line = V->getLineNumber();
Chris Lattner88ab9742009-05-05 04:55:56 +00001571 unsigned FileID = FindCompileUnit(V->getCompileUnit()).getID();
1572 assert(FileID && "Invalid file id");
Devang Patel4d1709e2009-01-08 02:33:41 +00001573 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1574 AddUInt(Die, DW_AT_decl_line, 0, Line);
1575 }
1576
1577 /// AddSourceLine - Add location information to specified debug information
1578 /// entry.
Devang Patel7c8a2772009-01-16 19:28:14 +00001579 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Chris Lattner88ab9742009-05-05 04:55:56 +00001580 // If there is no compile unit specified, don't add a line #.
1581 if (G->getCompileUnit().isNull())
1582 return;
Devang Patel5f244e32009-01-05 22:35:52 +00001583 unsigned Line = G->getLineNumber();
Chris Lattner88ab9742009-05-05 04:55:56 +00001584 unsigned FileID = FindCompileUnit(G->getCompileUnit()).getID();
1585 assert(FileID && "Invalid file id");
Devang Patel5f244e32009-01-05 22:35:52 +00001586 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1587 AddUInt(Die, DW_AT_decl_line, 0, Line);
1588 }
1589
Devang Patel7c8a2772009-01-16 19:28:14 +00001590 void AddSourceLine(DIE *Die, const DIType *Ty) {
Chris Lattner88ab9742009-05-05 04:55:56 +00001591 // If there is no compile unit specified, don't add a line #.
Devang Patel2ae1db52009-01-30 18:20:31 +00001592 DICompileUnit CU = Ty->getCompileUnit();
1593 if (CU.isNull())
1594 return;
Chris Lattner88ab9742009-05-05 04:55:56 +00001595
1596 unsigned Line = Ty->getLineNumber();
1597 unsigned FileID = FindCompileUnit(CU).getID();
1598 assert(FileID && "Invalid file id");
Devang Patel5f244e32009-01-05 22:35:52 +00001599 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1600 AddUInt(Die, DW_AT_decl_line, 0, Line);
1601 }
1602
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001603 /// AddAddress - Add an address attribute to a die based on the location
1604 /// provided.
1605 void AddAddress(DIE *Die, unsigned Attribute,
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001606 const MachineLocation &Location) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001607 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001608 DIEBlock *Block = new DIEBlock();
aslc200b112008-08-16 12:57:46 +00001609
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001610 if (Location.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001611 if (Reg < 32) {
1612 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1613 } else {
1614 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1615 AddUInt(Block, 0, DW_FORM_udata, Reg);
1616 }
1617 } else {
1618 if (Reg < 32) {
1619 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1620 } else {
1621 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1622 AddUInt(Block, 0, DW_FORM_udata, Reg);
1623 }
1624 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1625 }
aslc200b112008-08-16 12:57:46 +00001626
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001627 AddBlock(Die, Attribute, 0, Block);
1628 }
aslc200b112008-08-16 12:57:46 +00001629
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001630 /// AddType - Add a new type attribute to the specified entity.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001631 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel165ed512009-01-23 19:13:31 +00001632 if (Ty.isNull())
Devang Patel4a4cbe72009-01-05 21:47:57 +00001633 return;
Devang Patel4a4cbe72009-01-05 21:47:57 +00001634
1635 // Check for pre-existence.
1636 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1637 // If it exists then use the existing value.
1638 if (Slot) {
1639 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1640 return;
1641 }
1642
1643 // Set up proxy.
1644 Slot = NewDIEntry();
1645
1646 // Construct type.
1647 DIE Buffer(DW_TAG_base_type);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001648 if (Ty.isBasicType(Ty.getTag()))
1649 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1650 else if (Ty.isDerivedType(Ty.getTag()))
1651 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1652 else {
Bill Wendling824a8bf2009-02-03 21:17:20 +00001653 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelef4bf3b2009-01-15 19:26:23 +00001654 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1655 }
1656
Devang Patelb0cb07c2009-01-27 23:22:55 +00001657 // Add debug information entry to entity and appropriate context.
1658 DIE *Die = NULL;
1659 DIDescriptor Context = Ty.getContext();
1660 if (!Context.isNull())
1661 Die = DW_Unit->getDieMapSlotFor(Context.getGV());
1662
1663 if (Die) {
1664 DIE *Child = new DIE(Buffer);
1665 Die->AddChild(Child);
1666 Buffer.Detach();
1667 SetDIEntry(Slot, Child);
Bill Wendling824a8bf2009-02-03 21:17:20 +00001668 } else {
Devang Patelb0cb07c2009-01-27 23:22:55 +00001669 Die = DW_Unit->AddDie(Buffer);
1670 SetDIEntry(Slot, Die);
1671 }
1672
Devang Patel4a4cbe72009-01-05 21:47:57 +00001673 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1674 }
1675
Devang Patel46d13752009-01-05 19:07:53 +00001676 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1677 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001678 DIBasicType BTy) {
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001679
Devang Patelfc187162009-01-05 17:57:47 +00001680 // Get core information.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001681 std::string Name;
1682 BTy.getName(Name);
Devang Patelfc187162009-01-05 17:57:47 +00001683 Buffer.setTag(DW_TAG_base_type);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001684 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patelfc187162009-01-05 17:57:47 +00001685 // Add name if not anonymous or intermediate type.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001686 if (!Name.empty())
Devang Patelfc187162009-01-05 17:57:47 +00001687 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001688 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelfc187162009-01-05 17:57:47 +00001689 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1690 }
1691
Devang Patel46d13752009-01-05 19:07:53 +00001692 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1693 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001694 DIDerivedType DTy) {
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001695
Devang Patelfc187162009-01-05 17:57:47 +00001696 // Get core information.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001697 std::string Name;
1698 DTy.getName(Name);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001699 uint64_t Size = DTy.getSizeInBits() >> 3;
1700 unsigned Tag = DTy.getTag();
Bill Wendling1c5842b2009-03-09 05:04:40 +00001701
Devang Patelfc187162009-01-05 17:57:47 +00001702 // FIXME - Workaround for templates.
1703 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1704
1705 Buffer.setTag(Tag);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001706
Devang Patelfc187162009-01-05 17:57:47 +00001707 // Map to main type, void will not have a type.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001708 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel4a4cbe72009-01-05 21:47:57 +00001709 AddType(DW_Unit, &Buffer, FromTy);
Devang Patelfc187162009-01-05 17:57:47 +00001710
1711 // Add name if not anonymous or intermediate type.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001712 if (!Name.empty())
Evan Cheng3e288912009-02-25 07:04:34 +00001713 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelfc187162009-01-05 17:57:47 +00001714
1715 // Add size if non-zero (derived types might be zero-sized.)
1716 if (Size)
1717 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1718
1719 // Add source line info if available and TyDesc is not a forward
1720 // declaration.
Devang Patele34e0882009-01-27 00:45:04 +00001721 if (!DTy.isForwardDecl())
1722 AddSourceLine(&Buffer, &DTy);
Devang Patelfc187162009-01-05 17:57:47 +00001723 }
1724
Devang Patel30c01372009-01-05 19:55:51 +00001725 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1726 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001727 DICompositeType CTy) {
Devang Patelb28de842009-01-17 08:01:33 +00001728 // Get core information.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001729 std::string Name;
1730 CTy.getName(Name);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001731
Devang Patelef4bf3b2009-01-15 19:26:23 +00001732 uint64_t Size = CTy.getSizeInBits() >> 3;
1733 unsigned Tag = CTy.getTag();
Devang Patel8050bd72009-01-23 01:19:09 +00001734 Buffer.setTag(Tag);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001735
Devang Patel30c01372009-01-05 19:55:51 +00001736 switch (Tag) {
1737 case DW_TAG_vector_type:
1738 case DW_TAG_array_type:
Devang Patelef4bf3b2009-01-15 19:26:23 +00001739 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patel30c01372009-01-05 19:55:51 +00001740 break;
Devang Patel3798f492009-01-20 18:35:14 +00001741 case DW_TAG_enumeration_type:
1742 {
1743 DIArray Elements = CTy.getTypeArray();
1744 // Add enumerators to enumeration type.
1745 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1746 DIE *ElemDie = NULL;
1747 DIEnumerator Enum(Elements.getElement(i).getGV());
1748 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
1749 Buffer.AddChild(ElemDie);
1750 }
1751 }
1752 break;
Devang Patel30c01372009-01-05 19:55:51 +00001753 case DW_TAG_subroutine_type:
1754 {
Devang Patel30c01372009-01-05 19:55:51 +00001755 // Add return type.
Bill Wendling74940d12009-05-06 21:21:34 +00001756 DIArray Elements = CTy.getTypeArray();
Devang Patel4a4cbe72009-01-05 21:47:57 +00001757 DIDescriptor RTy = Elements.getElement(0);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001758 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel4a4cbe72009-01-05 21:47:57 +00001759
Bill Wendling74940d12009-05-06 21:21:34 +00001760 // Add prototype flag.
1761 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1762
Devang Patel30c01372009-01-05 19:55:51 +00001763 // Add arguments.
1764 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1765 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel4a4cbe72009-01-05 21:47:57 +00001766 DIDescriptor Ty = Elements.getElement(i);
Devang Pateld40a7e52009-01-17 06:57:25 +00001767 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patel30c01372009-01-05 19:55:51 +00001768 Buffer.AddChild(Arg);
1769 }
1770 }
1771 break;
1772 case DW_TAG_structure_type:
1773 case DW_TAG_union_type:
Devang Patel09353602009-03-25 00:28:40 +00001774 case DW_TAG_class_type:
Devang Patel30c01372009-01-05 19:55:51 +00001775 {
1776 // Add elements to structure type.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001777 DIArray Elements = CTy.getTypeArray();
Devang Patelcf7acb12009-01-16 00:50:53 +00001778
1779 // A forward struct declared type may not have elements available.
1780 if (Elements.isNull())
1781 break;
1782
Devang Patel30c01372009-01-05 19:55:51 +00001783 // Add elements to structure type.
1784 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1785 DIDescriptor Element = Elements.getElement(i);
Devang Patelb28de842009-01-17 08:01:33 +00001786 DIE *ElemDie = NULL;
Devang Patelef4bf3b2009-01-15 19:26:23 +00001787 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel245446c2009-01-17 08:05:14 +00001788 ElemDie = CreateSubprogramDIE(DW_Unit,
1789 DISubprogram(Element.getGV()));
Bill Wendlingd32c9722009-05-07 17:26:14 +00001790 else if (Element.getTag() == dwarf::DW_TAG_variable) // ??
Devang Patelb28de842009-01-17 08:01:33 +00001791 ElemDie = CreateGlobalVariableDIE(DW_Unit,
1792 DIGlobalVariable(Element.getGV()));
Devang Patel5c643892009-01-20 21:02:02 +00001793 else
1794 ElemDie = CreateMemberDIE(DW_Unit,
1795 DIDerivedType(Element.getGV()));
Devang Patel245446c2009-01-17 08:05:14 +00001796 Buffer.AddChild(ElemDie);
Devang Patel30c01372009-01-05 19:55:51 +00001797 }
Devang Patel74193d72009-02-17 22:43:44 +00001798 unsigned RLang = CTy.getRunTimeLang();
1799 if (RLang)
1800 AddUInt(&Buffer, DW_AT_APPLE_runtime_class, DW_FORM_data1, RLang);
Devang Patel30c01372009-01-05 19:55:51 +00001801 }
1802 break;
1803 default:
1804 break;
1805 }
1806
1807 // Add name if not anonymous or intermediate type.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001808 if (!Name.empty())
Evan Cheng3e288912009-02-25 07:04:34 +00001809 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel30c01372009-01-05 19:55:51 +00001810
Devang Patele34e0882009-01-27 00:45:04 +00001811 if (Tag == DW_TAG_enumeration_type || Tag == DW_TAG_structure_type
1812 || Tag == DW_TAG_union_type) {
1813 // Add size if non-zero (derived types might be zero-sized.)
1814 if (Size)
1815 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1816 else {
1817 // Add zero size if it is not a forward declaration.
1818 if (CTy.isForwardDecl())
1819 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1820 else
1821 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1822 }
1823
1824 // Add source line info if available.
1825 if (!CTy.isForwardDecl())
1826 AddSourceLine(&Buffer, &CTy);
Devang Patel30c01372009-01-05 19:55:51 +00001827 }
Devang Patel30c01372009-01-05 19:55:51 +00001828 }
1829
Bill Wendling824a8bf2009-02-03 21:17:20 +00001830 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1831 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelef4bf3b2009-01-15 19:26:23 +00001832 int64_t L = SR.getLo();
1833 int64_t H = SR.getHi();
Devang Patel6fb54132009-01-05 18:33:01 +00001834 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1835 if (L != H) {
1836 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1837 if (L)
Devang Patel245446c2009-01-17 08:05:14 +00001838 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1839 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel6fb54132009-01-05 18:33:01 +00001840 }
1841 Buffer.AddChild(DW_Subrange);
1842 }
1843
1844 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1845 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1846 DICompositeType *CTy) {
1847 Buffer.setTag(DW_TAG_array_type);
1848 if (CTy->getTag() == DW_TAG_vector_type)
1849 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1850
Devang Patel6ab30e52009-01-28 21:08:20 +00001851 // Emit derived type.
1852 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel6fb54132009-01-05 18:33:01 +00001853 DIArray Elements = CTy->getTypeArray();
Devang Patel6fb54132009-01-05 18:33:01 +00001854
1855 // Construct an anonymous type for index type.
1856 DIE IdxBuffer(DW_TAG_base_type);
1857 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1858 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1859 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1860
1861 // Add subranges to array type.
1862 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel30c01372009-01-05 19:55:51 +00001863 DIDescriptor Element = Elements.getElement(i);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001864 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1865 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel6fb54132009-01-05 18:33:01 +00001866 }
1867 }
1868
Bill Wendling824a8bf2009-02-03 21:17:20 +00001869 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3798f492009-01-20 18:35:14 +00001870 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patela566e812009-01-05 18:38:38 +00001871
1872 DIE *Enumerator = new DIE(DW_TAG_enumerator);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001873 std::string Name;
1874 ETy->getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001875 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
Devang Patela566e812009-01-05 18:38:38 +00001876 int64_t Value = ETy->getEnumValue();
1877 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Patel3798f492009-01-20 18:35:14 +00001878 return Enumerator;
Devang Patela566e812009-01-05 18:38:38 +00001879 }
Devang Patel6fb54132009-01-05 18:33:01 +00001880
Devang Patelb28de842009-01-17 08:01:33 +00001881 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendling824a8bf2009-02-03 21:17:20 +00001882 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patelb28de842009-01-17 08:01:33 +00001883 {
1884 DIE *GVDie = new DIE(DW_TAG_variable);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001885 std::string Name;
1886 GV.getDisplayName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001887 AddString(GVDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001888 std::string LinkageName;
1889 GV.getLinkageName(LinkageName);
1890 if (!LinkageName.empty())
Devang Patelb28de842009-01-17 08:01:33 +00001891 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1892 AddType(DW_Unit, GVDie, GV.getType());
1893 if (!GV.isLocalToUnit())
1894 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
1895 AddSourceLine(GVDie, &GV);
1896 return GVDie;
Devang Patel526b01d2009-01-05 18:59:44 +00001897 }
1898
Devang Patel5c643892009-01-20 21:02:02 +00001899 /// CreateMemberDIE - Create new member DIE.
1900 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
1901 DIE *MemberDie = new DIE(DT.getTag());
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001902 std::string Name;
1903 DT.getName(Name);
1904 if (!Name.empty())
Devang Patel5c643892009-01-20 21:02:02 +00001905 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1906
1907 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1908
1909 AddSourceLine(MemberDie, &DT);
1910
Devang Patelf1f30d42009-02-17 21:23:59 +00001911 uint64_t Size = DT.getSizeInBits();
1912 uint64_t FieldSize = DT.getOriginalTypeSize();
1913
1914 if (Size != FieldSize) {
1915 // Handle bitfield.
1916 AddUInt(MemberDie, DW_AT_byte_size, 0, DT.getOriginalTypeSize() >> 3);
1917 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1918
1919 uint64_t Offset = DT.getOffsetInBits();
1920 uint64_t FieldOffset = Offset;
1921 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1922 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1923 FieldOffset = (HiMark - FieldSize);
1924 Offset -= FieldOffset;
1925 // Maybe we need to work from the other end.
1926 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1927 AddUInt(MemberDie, DW_AT_bit_offset, 0, Offset);
1928 }
Devang Patel5c643892009-01-20 21:02:02 +00001929 DIEBlock *Block = new DIEBlock();
1930 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1931 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1932 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1933
Devang Patel2e7ee192009-01-21 00:08:04 +00001934 if (DT.isProtected())
1935 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1936 else if (DT.isPrivate())
1937 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1938
Devang Patel5c643892009-01-20 21:02:02 +00001939 return MemberDie;
1940 }
1941
Devang Patelb28de842009-01-17 08:01:33 +00001942 /// CreateSubprogramDIE - Create new DIE using SP.
1943 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Bill Wendling74940d12009-05-06 21:21:34 +00001944 const DISubprogram &SP,
Devang Patel245446c2009-01-17 08:05:14 +00001945 bool IsConstructor = false) {
Devang Patelb28de842009-01-17 08:01:33 +00001946 DIE *SPDie = new DIE(DW_TAG_subprogram);
Bill Wendling74940d12009-05-06 21:21:34 +00001947
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001948 std::string Name;
1949 SP.getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001950 AddString(SPDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling74940d12009-05-06 21:21:34 +00001951
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001952 std::string LinkageName;
1953 SP.getLinkageName(LinkageName);
Bill Wendling74940d12009-05-06 21:21:34 +00001954
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001955 if (!LinkageName.empty())
Bill Wendling74940d12009-05-06 21:21:34 +00001956 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1957
Devang Patelb28de842009-01-17 08:01:33 +00001958 AddSourceLine(SPDie, &SP);
Devang Patel526b01d2009-01-05 18:59:44 +00001959
Devang Patelb28de842009-01-17 08:01:33 +00001960 DICompositeType SPTy = SP.getType();
1961 DIArray Args = SPTy.getTypeArray();
Bill Wendling74940d12009-05-06 21:21:34 +00001962
1963 // Add prototyped tag, if C or ObjC.
1964 unsigned Lang = SP.getCompileUnit().getLanguage();
1965 if (Lang == DW_LANG_C99 || Lang == DW_LANG_C89 || Lang == DW_LANG_ObjC)
1966 AddUInt(SPDie, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patelb28de842009-01-17 08:01:33 +00001967
Devang Patel526b01d2009-01-05 18:59:44 +00001968 // Add Return Type.
Devang Patel6e199962009-04-08 22:18:45 +00001969 unsigned SPTag = SPTy.getTag();
Devang Patel688a19f2009-02-27 18:05:21 +00001970 if (!IsConstructor) {
Devang Patel6e199962009-04-08 22:18:45 +00001971 if (Args.isNull() || SPTag != DW_TAG_subroutine_type)
Devang Patel688a19f2009-02-27 18:05:21 +00001972 AddType(DW_Unit, SPDie, SPTy);
1973 else
1974 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
1975 }
Devang Patel922d1592009-01-30 01:21:46 +00001976
Devang Patelace2cf62009-02-02 17:51:41 +00001977 if (!SP.isDefinition()) {
1978 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
Bill Wendlingd32c9722009-05-07 17:26:14 +00001979
Bill Wendling74940d12009-05-06 21:21:34 +00001980 // Add arguments. Do not add arguments for subprogram definition. They
1981 // will be handled through RecordVariable.
Devang Patel6e199962009-04-08 22:18:45 +00001982 if (SPTag == DW_TAG_subroutine_type)
Devang Patelace2cf62009-02-02 17:51:41 +00001983 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1984 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1985 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
Bill Wendling74940d12009-05-06 21:21:34 +00001986 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ??
Devang Patelace2cf62009-02-02 17:51:41 +00001987 SPDie->AddChild(Arg);
1988 }
1989 }
Devang Patel922d1592009-01-30 01:21:46 +00001990
Devang Patelef4bf3b2009-01-15 19:26:23 +00001991 if (!SP.isLocalToUnit())
Devang Patel922d1592009-01-30 01:21:46 +00001992 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Bill Wendling74940d12009-05-06 21:21:34 +00001993
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001994 // DW_TAG_inlined_subroutine may refer to this DIE.
1995 DIE *&Slot = DW_Unit->getDieMapSlotFor(SP.getGV());
1996 Slot = SPDie;
Devang Patelb28de842009-01-17 08:01:33 +00001997 return SPDie;
Devang Patel526b01d2009-01-05 18:59:44 +00001998 }
1999
Devang Patelb28de842009-01-17 08:01:33 +00002000 /// FindCompileUnit - Get the compile unit for the given descriptor.
2001 ///
Chris Lattner88ab9742009-05-05 04:55:56 +00002002 CompileUnit &FindCompileUnit(DICompileUnit Unit) const {
2003 DenseMap<Value *, CompileUnit *>::const_iterator I =
2004 CompileUnitMap.find(Unit.getGV());
2005 assert(I != CompileUnitMap.end() && "Missing compile unit.");
2006 return *I->second;
Devang Patel5f244e32009-01-05 22:35:52 +00002007 }
2008
Devang Patel42f6bed2009-01-13 23:54:55 +00002009 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel4d1709e2009-01-08 02:33:41 +00002010 ///
2011 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
2012 // Get the descriptor.
Devang Patel7c8a2772009-01-16 19:28:14 +00002013 const DIVariable &VD = DV->getVariable();
Devang Patel4d1709e2009-01-08 02:33:41 +00002014
2015 // Translate tag to proper Dwarf tag. The result variable is dropped for
2016 // now.
2017 unsigned Tag;
Devang Patel7c8a2772009-01-16 19:28:14 +00002018 switch (VD.getTag()) {
Devang Patel4d1709e2009-01-08 02:33:41 +00002019 case DW_TAG_return_variable: return NULL;
2020 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2021 case DW_TAG_auto_variable: // fall thru
2022 default: Tag = DW_TAG_variable; break;
2023 }
2024
2025 // Define variable debug information entry.
2026 DIE *VariableDie = new DIE(Tag);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00002027 std::string Name;
2028 VD.getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00002029 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Devang Patel4d1709e2009-01-08 02:33:41 +00002030
2031 // Add source line info if available.
Devang Patel7c8a2772009-01-16 19:28:14 +00002032 AddSourceLine(VariableDie, &VD);
Devang Patel4d1709e2009-01-08 02:33:41 +00002033
2034 // Add variable type.
Devang Patel7c8a2772009-01-16 19:28:14 +00002035 AddType(Unit, VariableDie, VD.getType());
Devang Patel4d1709e2009-01-08 02:33:41 +00002036
2037 // Add variable address.
2038 MachineLocation Location;
2039 Location.set(RI->getFrameRegister(*MF),
2040 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
2041 AddAddress(VariableDie, DW_AT_location, Location);
2042
2043 return VariableDie;
2044 }
2045
Devang Patel4d1709e2009-01-08 02:33:41 +00002046 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2047 ///
2048 DbgScope *getOrCreateScope(GlobalVariable *V) {
2049 DbgScope *&Slot = DbgScopeMap[V];
Bill Wendlinge0f3a262009-02-20 20:40:28 +00002050 if (Slot) return Slot;
2051
Bill Wendlingd32c9722009-05-07 17:26:14 +00002052 // Don't create a new scope if we already created one for an inlined
2053 // function.
2054 DenseMap<const GlobalVariable *, DbgScope *>::iterator
2055 II = AbstractInstanceRootMap.find(V);
2056 if (II != AbstractInstanceRootMap.end())
2057 return LexicalScopeStack.back();
2058
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002059 DbgScope *Parent = NULL;
2060 DIBlock Block(V);
Bill Wendling74940d12009-05-06 21:21:34 +00002061
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002062 if (!Block.isNull()) {
2063 DIDescriptor ParentDesc = Block.getContext();
2064 Parent =
2065 ParentDesc.isNull() ? NULL : getOrCreateScope(ParentDesc.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002066 }
Bill Wendling74940d12009-05-06 21:21:34 +00002067
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002068 Slot = new DbgScope(Parent, DIDescriptor(V));
Bill Wendlinge0f3a262009-02-20 20:40:28 +00002069
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002070 if (Parent)
Bill Wendlinge0f3a262009-02-20 20:40:28 +00002071 Parent->AddScope(Slot);
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002072 else
Bill Wendlinge0f3a262009-02-20 20:40:28 +00002073 // First function is top level function.
Devang Patel7b60d552009-04-15 20:41:31 +00002074 FunctionDbgScope = Slot;
Bill Wendlinge0f3a262009-02-20 20:40:28 +00002075
Devang Patel4d1709e2009-01-08 02:33:41 +00002076 return Slot;
2077 }
2078
2079 /// ConstructDbgScope - Construct the components of a scope.
2080 ///
2081 void ConstructDbgScope(DbgScope *ParentScope,
2082 unsigned ParentStartID, unsigned ParentEndID,
2083 DIE *ParentDie, CompileUnit *Unit) {
Bill Wendlingd32c9722009-05-07 17:26:14 +00002084 if (LexicalScopeToConcreteInstMap.find(ParentScope) ==
2085 LexicalScopeToConcreteInstMap.end()) {
2086 // Add variables to scope.
2087 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
2088 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2089 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2090 if (VariableDie) ParentDie->AddChild(VariableDie);
2091 }
Devang Patel4d1709e2009-01-08 02:33:41 +00002092 }
2093
2094 // Add nested scopes.
Devang Patel63c22f42009-01-10 02:42:49 +00002095 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel4d1709e2009-01-08 02:33:41 +00002096 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2097 // Define the Scope debug information entry.
2098 DbgScope *Scope = Scopes[j];
Devang Patel4d1709e2009-01-08 02:33:41 +00002099
Devang Patelb9224922009-01-12 18:41:00 +00002100 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2101 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel4d1709e2009-01-08 02:33:41 +00002102
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002103 // Ignore empty scopes.
2104 // Do not ignore inlined scope even if it does not have any
2105 // variables or scopes.
Devang Patel4d1709e2009-01-08 02:33:41 +00002106 if (StartID == EndID && StartID != 0) continue;
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002107 if (!Scope->isInlinedSubroutine()
Devang Patel88bf96e2009-04-13 17:02:03 +00002108 && Scope->getScopes().empty() && Scope->getVariables().empty())
2109 continue;
Devang Patel4d1709e2009-01-08 02:33:41 +00002110
2111 if (StartID == ParentStartID && EndID == ParentEndID) {
2112 // Just add stuff to the parent scope.
2113 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2114 } else {
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002115 DIE *ScopeDie = NULL;
Bill Wendlingd32c9722009-05-07 17:26:14 +00002116
2117 DenseMap<DbgScope *, DIE *>::iterator I =
2118 LexicalScopeToConcreteInstMap.find(Scope);
2119
2120 if (I != LexicalScopeToConcreteInstMap.end())
2121 ScopeDie = I->second;
2122 else
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002123 ScopeDie = new DIE(DW_TAG_lexical_block);
Bill Wendling821048d2009-05-01 08:25:13 +00002124
2125 // Add the scope bounds.
2126 if (StartID)
2127 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2128 DWLabel("label", StartID));
2129 else
2130 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2131 DWLabel("func_begin", SubprogramCount));
2132
2133 if (EndID)
2134 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2135 DWLabel("label", EndID));
2136 else
2137 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2138 DWLabel("func_end", SubprogramCount));
2139
2140 // Add the scope contents.
2141 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2142 ParentDie->AddChild(ScopeDie);
Devang Patel4d1709e2009-01-08 02:33:41 +00002143 }
2144 }
2145 }
2146
Devang Patel7b60d552009-04-15 20:41:31 +00002147 /// ConstructFunctionDbgScope - Construct the scope for the subprogram.
Devang Patel4d1709e2009-01-08 02:33:41 +00002148 ///
Devang Patel7b60d552009-04-15 20:41:31 +00002149 void ConstructFunctionDbgScope(DbgScope *RootScope) {
Devang Patel4d1709e2009-01-08 02:33:41 +00002150 // Exit if there is no root scope.
2151 if (!RootScope) return;
Devang Patel2560d922009-01-15 18:25:17 +00002152 DIDescriptor Desc = RootScope->getDesc();
2153 if (Desc.isNull())
2154 return;
Devang Patel4d1709e2009-01-08 02:33:41 +00002155
2156 // Get the subprogram debug information entry.
Devang Patel2560d922009-01-15 18:25:17 +00002157 DISubprogram SPD(Desc.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002158
2159 // Get the compile unit context.
Devang Patel2ae1db52009-01-30 18:20:31 +00002160 CompileUnit *Unit = MainCU;
2161 if (!Unit)
Chris Lattner88ab9742009-05-05 04:55:56 +00002162 Unit = &FindCompileUnit(SPD.getCompileUnit());
Devang Patel4d1709e2009-01-08 02:33:41 +00002163
2164 // Get the subprogram die.
Devang Patel57ec9ac2009-01-12 22:58:14 +00002165 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002166 assert(SPDie && "Missing subprogram descriptor");
2167
2168 // Add the function bounds.
2169 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2170 DWLabel("func_begin", SubprogramCount));
2171 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2172 DWLabel("func_end", SubprogramCount));
2173 MachineLocation Location(RI->getFrameRegister(*MF));
2174 AddAddress(SPDie, DW_AT_frame_base, Location);
2175
2176 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2177 }
2178
Bill Wendlingd32c9722009-05-07 17:26:14 +00002179 void ConstructAbstractDbgScope(DbgScope *AbsScope) {
2180 // Exit if there is no root scope.
2181 if (!AbsScope) return;
2182
2183 DIDescriptor Desc = AbsScope->getDesc();
2184 if (Desc.isNull())
2185 return;
2186
2187 // Get the subprogram debug information entry.
2188 DISubprogram SPD(Desc.getGV());
2189
2190 // Get the compile unit context.
2191 CompileUnit *Unit = MainCU;
2192 if (!Unit)
2193 Unit = &FindCompileUnit(SPD.getCompileUnit());
2194
2195 // Get the subprogram die.
2196 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
2197 assert(SPDie && "Missing subprogram descriptor");
2198
2199 ConstructDbgScope(AbsScope, 0, 0, SPDie, Unit);
2200 }
2201
Devang Patel4d1709e2009-01-08 02:33:41 +00002202 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2203 ///
2204 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Cheng3e288912009-02-25 07:04:34 +00002205 const char *FnName = MF->getFunction()->getNameStart();
2206 if (MainCU) {
Bill Wendlinge06da442009-04-09 21:49:15 +00002207 StringMap<DIE*> &Globals = MainCU->getGlobals();
2208 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Cheng3e288912009-02-25 07:04:34 +00002209 if (GI != Globals.end()) {
2210 DIE *SPDie = GI->second;
Devang Patel4d1709e2009-01-08 02:33:41 +00002211
2212 // Add the function bounds.
2213 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2214 DWLabel("func_begin", SubprogramCount));
2215 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2216 DWLabel("func_end", SubprogramCount));
2217
2218 MachineLocation Location(RI->getFrameRegister(*MF));
2219 AddAddress(SPDie, DW_AT_frame_base, Location);
2220 return;
2221 }
Evan Cheng3e288912009-02-25 07:04:34 +00002222 } else {
2223 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2224 CompileUnit *Unit = CompileUnits[i];
Bill Wendlinge06da442009-04-09 21:49:15 +00002225 StringMap<DIE*> &Globals = Unit->getGlobals();
2226 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Cheng3e288912009-02-25 07:04:34 +00002227 if (GI != Globals.end()) {
2228 DIE *SPDie = GI->second;
2229
2230 // Add the function bounds.
2231 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2232 DWLabel("func_begin", SubprogramCount));
2233 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2234 DWLabel("func_end", SubprogramCount));
2235
2236 MachineLocation Location(RI->getFrameRegister(*MF));
2237 AddAddress(SPDie, DW_AT_frame_base, Location);
2238 return;
2239 }
2240 }
Devang Patel4d1709e2009-01-08 02:33:41 +00002241 }
Evan Cheng3e288912009-02-25 07:04:34 +00002242
Devang Patel4d1709e2009-01-08 02:33:41 +00002243#if 0
2244 // FIXME: This is causing an abort because C++ mangled names are compared
2245 // with their unmangled counterparts. See PR2885. Don't do this assert.
2246 assert(0 && "Couldn't find DIE for machine function!");
2247#endif
Evan Cheng3e288912009-02-25 07:04:34 +00002248 return;
Devang Patel4d1709e2009-01-08 02:33:41 +00002249 }
2250
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002251 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2252 /// tools to recognize the object file contains Dwarf information.
2253 void EmitInitial() {
2254 // Check to see if we already emitted intial headers.
2255 if (didInitial) return;
2256 didInitial = true;
aslc200b112008-08-16 12:57:46 +00002257
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002258 // Dwarf sections base addresses.
2259 if (TAI->doesDwarfRequireFrameSection()) {
2260 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2261 EmitLabel("section_debug_frame", 0);
2262 }
2263 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2264 EmitLabel("section_info", 0);
2265 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2266 EmitLabel("section_abbrev", 0);
2267 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2268 EmitLabel("section_aranges", 0);
Scott Michel79f01f52009-01-26 22:32:51 +00002269 if (TAI->doesSupportMacInfoSection()) {
2270 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2271 EmitLabel("section_macinfo", 0);
2272 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002273 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2274 EmitLabel("section_line", 0);
2275 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2276 EmitLabel("section_loc", 0);
2277 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2278 EmitLabel("section_pubnames", 0);
2279 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2280 EmitLabel("section_str", 0);
2281 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2282 EmitLabel("section_ranges", 0);
2283
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002284 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002285 EmitLabel("text_begin", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00002286 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002287 EmitLabel("data_begin", 0);
2288 }
2289
2290 /// EmitDIE - Recusively Emits a debug information entry.
2291 ///
2292 void EmitDIE(DIE *Die) {
2293 // Get the abbreviation for this DIE.
2294 unsigned AbbrevNumber = Die->getAbbrevNumber();
2295 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
aslc200b112008-08-16 12:57:46 +00002296
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002297 Asm->EOL();
2298
2299 // Emit the code (index) for the abbreviation.
2300 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng0eeed442008-07-01 23:18:29 +00002301
Evan Cheng42ceb472009-03-25 01:47:28 +00002302 if (Asm->isVerbose())
Evan Cheng0eeed442008-07-01 23:18:29 +00002303 Asm->EOL(std::string("Abbrev [" +
2304 utostr(AbbrevNumber) +
2305 "] 0x" + utohexstr(Die->getOffset()) +
2306 ":0x" + utohexstr(Die->getSize()) + " " +
2307 TagString(Abbrev->getTag())));
2308 else
2309 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00002310
Owen Anderson88dd6232008-06-24 21:44:59 +00002311 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2312 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
aslc200b112008-08-16 12:57:46 +00002313
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002314 // Emit the DIE attribute values.
2315 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2316 unsigned Attr = AbbrevData[i].getAttribute();
2317 unsigned Form = AbbrevData[i].getForm();
2318 assert(Form && "Too many attributes for DIE (check abbreviation)");
aslc200b112008-08-16 12:57:46 +00002319
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002320 switch (Attr) {
2321 case DW_AT_sibling: {
2322 Asm->EmitInt32(Die->SiblingOffset());
2323 break;
2324 }
2325 default: {
2326 // Emit an attribute using the defined form.
2327 Values[i]->EmitValue(*this, Form);
2328 break;
2329 }
2330 }
aslc200b112008-08-16 12:57:46 +00002331
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002332 Asm->EOL(AttributeString(Attr));
2333 }
aslc200b112008-08-16 12:57:46 +00002334
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002335 // Emit the DIE children if any.
2336 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2337 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002338
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002339 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2340 EmitDIE(Children[j]);
2341 }
aslc200b112008-08-16 12:57:46 +00002342
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002343 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2344 }
2345 }
2346
2347 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2348 ///
2349 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2350 // Get the children.
2351 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002352
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002353 // If not last sibling and has children then add sibling offset attribute.
2354 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2355
2356 // Record the abbreviation.
2357 AssignAbbrevNumber(Die->getAbbrev());
aslc200b112008-08-16 12:57:46 +00002358
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002359 // Get the abbreviation for this DIE.
2360 unsigned AbbrevNumber = Die->getAbbrevNumber();
2361 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2362
2363 // Set DIE offset
2364 Die->setOffset(Offset);
aslc200b112008-08-16 12:57:46 +00002365
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002366 // Start the size with the size of abbreviation code.
aslc200b112008-08-16 12:57:46 +00002367 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2368
Owen Anderson88dd6232008-06-24 21:44:59 +00002369 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2370 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002371
2372 // Size the DIE attribute values.
2373 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2374 // Size attribute value.
2375 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2376 }
aslc200b112008-08-16 12:57:46 +00002377
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002378 // Size the DIE children if any.
2379 if (!Children.empty()) {
2380 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2381 "Children flag not set");
aslc200b112008-08-16 12:57:46 +00002382
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002383 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2384 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2385 }
aslc200b112008-08-16 12:57:46 +00002386
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002387 // End of children marker.
2388 Offset += sizeof(int8_t);
2389 }
2390
2391 Die->setSize(Offset - Die->getOffset());
2392 return Offset;
2393 }
2394
2395 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2396 ///
2397 void SizeAndOffsets() {
2398 // Process base compile unit.
Devang Patel2ae1db52009-01-30 18:20:31 +00002399 if (MainCU) {
2400 // Compute size of compile unit header
2401 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2402 sizeof(int16_t) + // DWARF version number
2403 sizeof(int32_t) + // Offset Into Abbrev. Section
2404 sizeof(int8_t); // Pointer Size (in bytes)
2405 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
2406 return;
2407 }
Evan Cheng3e288912009-02-25 07:04:34 +00002408 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2409 CompileUnit *Unit = CompileUnits[i];
Devang Patel6eae2832009-01-12 23:05:55 +00002410 // Compute size of compile unit header
2411 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2412 sizeof(int16_t) + // DWARF version number
2413 sizeof(int32_t) + // Offset Into Abbrev. Section
2414 sizeof(int8_t); // Pointer Size (in bytes)
2415 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2416 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002417 }
2418
Evan Cheng3e288912009-02-25 07:04:34 +00002419 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002420 ///
Evan Cheng3e288912009-02-25 07:04:34 +00002421 void EmitDebugInfoPerCU(CompileUnit *Unit) {
2422 DIE *Die = Unit->getDie();
2423 // Emit the compile units header.
2424 EmitLabel("info_begin", Unit->getID());
2425 // Emit size of content not including length itself
2426 unsigned ContentSize = Die->getSize() +
2427 sizeof(int16_t) + // DWARF version number
2428 sizeof(int32_t) + // Offset Into Abbrev. Section
2429 sizeof(int8_t) + // Pointer Size (in bytes)
2430 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2431
2432 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2433 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2434 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2435 Asm->EOL("Offset Into Abbrev. Section");
2436 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2437
2438 EmitDIE(Die);
2439 // FIXME - extra padding for gdb bug.
2440 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2441 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2442 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2443 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2444 EmitLabel("info_end", Unit->getID());
2445
2446 Asm->EOL();
2447 }
2448
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002449 void EmitDebugInfo() {
2450 // Start debug info section.
2451 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
aslc200b112008-08-16 12:57:46 +00002452
Evan Cheng3e288912009-02-25 07:04:34 +00002453 if (MainCU) {
2454 EmitDebugInfoPerCU(MainCU);
2455 return;
Devang Patel6eae2832009-01-12 23:05:55 +00002456 }
Evan Cheng3e288912009-02-25 07:04:34 +00002457
2458 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2459 EmitDebugInfoPerCU(CompileUnits[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002460 }
2461
2462 /// EmitAbbreviations - Emit the abbreviation section.
2463 ///
2464 void EmitAbbreviations() const {
2465 // Check to see if it is worth the effort.
2466 if (!Abbreviations.empty()) {
2467 // Start the debug abbrev section.
2468 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
aslc200b112008-08-16 12:57:46 +00002469
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002470 EmitLabel("abbrev_begin", 0);
aslc200b112008-08-16 12:57:46 +00002471
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002472 // For each abbrevation.
2473 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2474 // Get abbreviation data
2475 const DIEAbbrev *Abbrev = Abbreviations[i];
aslc200b112008-08-16 12:57:46 +00002476
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002477 // Emit the abbrevations code (base 1 index.)
2478 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2479 Asm->EOL("Abbreviation Code");
aslc200b112008-08-16 12:57:46 +00002480
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002481 // Emit the abbreviations data.
2482 Abbrev->Emit(*this);
aslc200b112008-08-16 12:57:46 +00002483
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002484 Asm->EOL();
2485 }
aslc200b112008-08-16 12:57:46 +00002486
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002487 // Mark end of abbreviations.
2488 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2489
2490 EmitLabel("abbrev_end", 0);
aslc200b112008-08-16 12:57:46 +00002491
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002492 Asm->EOL();
2493 }
2494 }
2495
Bill Wendling1983a2a2008-07-20 00:11:19 +00002496 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2497 /// the line matrix.
aslc200b112008-08-16 12:57:46 +00002498 ///
Bill Wendling1983a2a2008-07-20 00:11:19 +00002499 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2500 // Define last address of section.
2501 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2502 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2503 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2504 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2505
2506 // Mark end of matrix.
2507 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2508 Asm->EmitULEB128Bytes(1); Asm->EOL();
2509 Asm->EmitInt8(1); Asm->EOL();
2510 }
2511
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002512 /// EmitDebugLines - Emit source line information.
2513 ///
2514 void EmitDebugLines() {
Bill Wendling1983a2a2008-07-20 00:11:19 +00002515 // If the target is using .loc/.file, the assembler will be emitting the
2516 // .debug_line table automatically.
2517 if (TAI->hasDotLocAndDotFile())
Dan Gohmanc55b34a2007-09-24 21:43:52 +00002518 return;
2519
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002520 // Minimum line delta, thus ranging from -10..(255-10).
2521 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2522 // Maximum line delta, thus ranging from -10..(255-10).
2523 const int MaxLineDelta = 255 + MinLineDelta;
2524
2525 // Start the dwarf line section.
2526 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
aslc200b112008-08-16 12:57:46 +00002527
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002528 // Construct the section header.
aslc200b112008-08-16 12:57:46 +00002529
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002530 EmitDifference("line_end", 0, "line_begin", 0, true);
2531 Asm->EOL("Length of Source Line Info");
2532 EmitLabel("line_begin", 0);
aslc200b112008-08-16 12:57:46 +00002533
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002534 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
aslc200b112008-08-16 12:57:46 +00002535
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002536 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2537 Asm->EOL("Prolog Length");
2538 EmitLabel("line_prolog_begin", 0);
aslc200b112008-08-16 12:57:46 +00002539
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002540 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2541
2542 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2543
2544 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
aslc200b112008-08-16 12:57:46 +00002545
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002546 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2547
2548 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
aslc200b112008-08-16 12:57:46 +00002549
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002550 // Line number standard opcode encodings argument count
2551 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2552 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2553 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2554 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2555 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2556 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2557 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2558 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2559 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2560
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002561 // Emit directories.
Evan Cheng3e288912009-02-25 07:04:34 +00002562 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2563 Asm->EmitString(getSourceDirectoryName(DI));
2564 Asm->EOL("Directory");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002565 }
2566 Asm->EmitInt8(0); Asm->EOL("End of directories");
aslc200b112008-08-16 12:57:46 +00002567
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002568 // Emit files.
Evan Cheng3e288912009-02-25 07:04:34 +00002569 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2570 // Remember source id starts at 1.
Bill Wendlingdf25fd62009-03-10 21:59:25 +00002571 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Evan Cheng3e288912009-02-25 07:04:34 +00002572 Asm->EmitString(getSourceFileName(Id.second));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002573 Asm->EOL("Source");
Evan Cheng3e288912009-02-25 07:04:34 +00002574 Asm->EmitULEB128Bytes(Id.first);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002575 Asm->EOL("Directory #");
2576 Asm->EmitULEB128Bytes(0);
2577 Asm->EOL("Mod date");
2578 Asm->EmitULEB128Bytes(0);
2579 Asm->EOL("File size");
2580 }
2581 Asm->EmitInt8(0); Asm->EOL("End of files");
aslc200b112008-08-16 12:57:46 +00002582
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002583 EmitLabel("line_prolog_end", 0);
aslc200b112008-08-16 12:57:46 +00002584
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002585 // A sequence for each text section.
Bill Wendling1983a2a2008-07-20 00:11:19 +00002586 unsigned SecSrcLinesSize = SectionSourceLines.size();
2587
2588 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002589 // Isolate current sections line info.
Devang Patel35a078f2009-01-12 22:54:42 +00002590 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng0eeed442008-07-01 23:18:29 +00002591
Evan Cheng42ceb472009-03-25 01:47:28 +00002592 if (Asm->isVerbose()) {
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002593 const Section* S = SectionMap[j + 1];
Evan Cheng3e288912009-02-25 07:04:34 +00002594 O << '\t' << TAI->getCommentString() << " Section"
2595 << S->getName() << '\n';
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002596 } else
Evan Cheng0eeed442008-07-01 23:18:29 +00002597 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002598
2599 // Dwarf assumes we start with first line of first source file.
2600 unsigned Source = 1;
2601 unsigned Line = 1;
aslc200b112008-08-16 12:57:46 +00002602
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002603 // Construct rows of the address, source, line, column matrix.
2604 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patel35a078f2009-01-12 22:54:42 +00002605 const SrcLineInfo &LineInfo = LineInfos[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002606 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2607 if (!LabelID) continue;
aslc200b112008-08-16 12:57:46 +00002608
Evan Cheng42ceb472009-03-25 01:47:28 +00002609 if (!Asm->isVerbose())
Evan Cheng0eeed442008-07-01 23:18:29 +00002610 Asm->EOL();
Evan Cheng3e288912009-02-25 07:04:34 +00002611 else {
2612 std::pair<unsigned, unsigned> SourceID =
Bill Wendlingdf25fd62009-03-10 21:59:25 +00002613 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Evan Cheng3e288912009-02-25 07:04:34 +00002614 O << '\t' << TAI->getCommentString() << ' '
2615 << getSourceDirectoryName(SourceID.first) << ' '
2616 << getSourceFileName(SourceID.second)
2617 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2618 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002619
2620 // Define the line address.
2621 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohmancfb72b22007-09-27 23:12:31 +00002622 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002623 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2624 EmitReference("label", LabelID); Asm->EOL("Location label");
aslc200b112008-08-16 12:57:46 +00002625
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002626 // If change of source, then switch to the new source.
2627 if (Source != LineInfo.getSourceID()) {
2628 Source = LineInfo.getSourceID();
2629 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2630 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2631 }
aslc200b112008-08-16 12:57:46 +00002632
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002633 // If change of line.
2634 if (Line != LineInfo.getLine()) {
2635 // Determine offset.
2636 int Offset = LineInfo.getLine() - Line;
2637 int Delta = Offset - MinLineDelta;
aslc200b112008-08-16 12:57:46 +00002638
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002639 // Update line.
2640 Line = LineInfo.getLine();
aslc200b112008-08-16 12:57:46 +00002641
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002642 // If delta is small enough and in range...
2643 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2644 // ... then use fast opcode.
2645 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2646 } else {
2647 // ... otherwise use long hand.
2648 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2649 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2650 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2651 }
2652 } else {
2653 // Copy the previous row (different address or source)
2654 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2655 }
2656 }
2657
Bill Wendling1983a2a2008-07-20 00:11:19 +00002658 EmitEndOfLineMatrix(j + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002659 }
Bill Wendling1983a2a2008-07-20 00:11:19 +00002660
2661 if (SecSrcLinesSize == 0)
2662 // Because we're emitting a debug_line section, we still need a line
2663 // table. The linker and friends expect it to exist. If there's nothing to
2664 // put into it, emit an empty table.
2665 EmitEndOfLineMatrix(1);
aslc200b112008-08-16 12:57:46 +00002666
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002667 EmitLabel("line_end", 0);
aslc200b112008-08-16 12:57:46 +00002668
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002669 Asm->EOL();
2670 }
aslc200b112008-08-16 12:57:46 +00002671
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002672 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2673 ///
2674 void EmitCommonDebugFrame() {
2675 if (!TAI->doesDwarfRequireFrameSection())
2676 return;
2677
2678 int stackGrowth =
2679 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2680 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00002681 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002682
2683 // Start the dwarf frame section.
2684 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2685
2686 EmitLabel("debug_frame_common", 0);
2687 EmitDifference("debug_frame_common_end", 0,
2688 "debug_frame_common_begin", 0, true);
2689 Asm->EOL("Length of Common Information Entry");
2690
2691 EmitLabel("debug_frame_common_begin", 0);
2692 Asm->EmitInt32((int)DW_CIE_ID);
2693 Asm->EOL("CIE Identifier Tag");
2694 Asm->EmitInt8(DW_CIE_VERSION);
2695 Asm->EOL("CIE Version");
2696 Asm->EmitString("");
2697 Asm->EOL("CIE Augmentation");
2698 Asm->EmitULEB128Bytes(1);
2699 Asm->EOL("CIE Code Alignment Factor");
2700 Asm->EmitSLEB128Bytes(stackGrowth);
aslc200b112008-08-16 12:57:46 +00002701 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00002702 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002703 Asm->EOL("CIE RA Column");
aslc200b112008-08-16 12:57:46 +00002704
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002705 std::vector<MachineMove> Moves;
2706 RI->getInitialFrameState(Moves);
2707
Dale Johannesenf5a11532007-11-13 19:13:01 +00002708 EmitFrameMoves(NULL, 0, Moves, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002709
Evan Cheng7e7d1942008-02-29 19:36:59 +00002710 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002711 EmitLabel("debug_frame_common_end", 0);
aslc200b112008-08-16 12:57:46 +00002712
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002713 Asm->EOL();
2714 }
2715
2716 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2717 /// section.
2718 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
2719 if (!TAI->doesDwarfRequireFrameSection())
2720 return;
aslc200b112008-08-16 12:57:46 +00002721
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002722 // Start the dwarf frame section.
2723 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
aslc200b112008-08-16 12:57:46 +00002724
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002725 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2726 "debug_frame_begin", DebugFrameInfo.Number, true);
2727 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00002728
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002729 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2730
2731 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2732 0, 0, true, false);
2733 Asm->EOL("FDE CIE offset");
2734
2735 EmitReference("func_begin", DebugFrameInfo.Number);
2736 Asm->EOL("FDE initial location");
2737 EmitDifference("func_end", DebugFrameInfo.Number,
2738 "func_begin", DebugFrameInfo.Number);
2739 Asm->EOL("FDE address range");
aslc200b112008-08-16 12:57:46 +00002740
Devang Patelb28de842009-01-17 08:01:33 +00002741 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00002742 false);
aslc200b112008-08-16 12:57:46 +00002743
Evan Cheng7e7d1942008-02-29 19:36:59 +00002744 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002745 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2746
2747 Asm->EOL();
2748 }
2749
Evan Cheng3e288912009-02-25 07:04:34 +00002750 void EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2751 EmitDifference("pubnames_end", Unit->getID(),
2752 "pubnames_begin", Unit->getID(), true);
2753 Asm->EOL("Length of Public Names Info");
2754
2755 EmitLabel("pubnames_begin", Unit->getID());
2756
2757 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2758
2759 EmitSectionOffset("info_begin", "section_info",
2760 Unit->getID(), 0, true, false);
2761 Asm->EOL("Offset of Compilation Unit Info");
2762
2763 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2764 true);
2765 Asm->EOL("Compilation Unit Length");
2766
Bill Wendlinge06da442009-04-09 21:49:15 +00002767 StringMap<DIE*> &Globals = Unit->getGlobals();
Bill Wendling3f94b412009-04-09 23:51:31 +00002768 for (StringMap<DIE*>::const_iterator
Bill Wendlinge06da442009-04-09 21:49:15 +00002769 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
Bill Wendling3f94b412009-04-09 23:51:31 +00002770 const char *Name = GI->getKeyData();
Evan Cheng3e288912009-02-25 07:04:34 +00002771 DIE * Entity = GI->second;
2772
2773 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
Bill Wendling3f94b412009-04-09 23:51:31 +00002774 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
Evan Cheng3e288912009-02-25 07:04:34 +00002775 }
2776
2777 Asm->EmitInt32(0); Asm->EOL("End Mark");
2778 EmitLabel("pubnames_end", Unit->getID());
2779
2780 Asm->EOL();
2781 }
2782
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002783 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2784 ///
2785 void EmitDebugPubNames() {
2786 // Start the dwarf pubnames section.
2787 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
aslc200b112008-08-16 12:57:46 +00002788
Evan Cheng3e288912009-02-25 07:04:34 +00002789 if (MainCU) {
2790 EmitDebugPubNamesPerCU(MainCU);
2791 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002792 }
Evan Cheng3e288912009-02-25 07:04:34 +00002793
2794 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2795 EmitDebugPubNamesPerCU(CompileUnits[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002796 }
2797
2798 /// EmitDebugStr - Emit visible names into a debug str section.
2799 ///
2800 void EmitDebugStr() {
2801 // Check to see if it is worth the effort.
2802 if (!StringPool.empty()) {
2803 // Start the dwarf str section.
2804 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
aslc200b112008-08-16 12:57:46 +00002805
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002806 // For each of strings in the string pool.
2807 for (unsigned StringID = 1, N = StringPool.size();
2808 StringID <= N; ++StringID) {
2809 // Emit a label for reference from debug information entries.
2810 EmitLabel("string", StringID);
2811 // Emit the string itself.
2812 const std::string &String = StringPool[StringID];
2813 Asm->EmitString(String); Asm->EOL();
2814 }
aslc200b112008-08-16 12:57:46 +00002815
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002816 Asm->EOL();
2817 }
2818 }
2819
2820 /// EmitDebugLoc - Emit visible names into a debug loc section.
2821 ///
2822 void EmitDebugLoc() {
2823 // Start the dwarf loc section.
2824 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
aslc200b112008-08-16 12:57:46 +00002825
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002826 Asm->EOL();
2827 }
2828
2829 /// EmitDebugARanges - Emit visible names into a debug aranges section.
2830 ///
2831 void EmitDebugARanges() {
2832 // Start the dwarf aranges section.
2833 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
aslc200b112008-08-16 12:57:46 +00002834
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002835 // FIXME - Mock up
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002836#if 0
aslc200b112008-08-16 12:57:46 +00002837 CompileUnit *Unit = GetBaseCompileUnit();
2838
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002839 // Don't include size of length
2840 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
aslc200b112008-08-16 12:57:46 +00002841
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002842 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
aslc200b112008-08-16 12:57:46 +00002843
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002844 EmitReference("info_begin", Unit->getID());
2845 Asm->EOL("Offset of Compilation Unit Info");
2846
Dan Gohmancfb72b22007-09-27 23:12:31 +00002847 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002848
2849 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2850
2851 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2852 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2853
2854 // Range 1
2855 EmitReference("text_begin", 0); Asm->EOL("Address");
2856 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2857
2858 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2859 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002860#endif
aslc200b112008-08-16 12:57:46 +00002861
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002862 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002863 }
2864
2865 /// EmitDebugRanges - Emit visible names into a debug ranges section.
2866 ///
2867 void EmitDebugRanges() {
2868 // Start the dwarf ranges section.
2869 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
aslc200b112008-08-16 12:57:46 +00002870
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002871 Asm->EOL();
2872 }
2873
2874 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2875 ///
2876 void EmitDebugMacInfo() {
Scott Michel79f01f52009-01-26 22:32:51 +00002877 if (TAI->doesSupportMacInfoSection()) {
2878 // Start the dwarf macinfo section.
2879 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
aslc200b112008-08-16 12:57:46 +00002880
Scott Michel79f01f52009-01-26 22:32:51 +00002881 Asm->EOL();
2882 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002883 }
2884
Devang Patel88bf96e2009-04-13 17:02:03 +00002885 /// EmitDebugInlineInfo - Emit inline info using following format.
2886 /// Section Header:
2887 /// 1. length of section
2888 /// 2. Dwarf version number
2889 /// 3. address size.
2890 ///
2891 /// Entries (one "entry" for each function that was inlined):
2892 ///
2893 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
2894 /// otherwise offset into __debug_str for regular function name.
2895 /// 2. offset into __debug_str section for regular function name.
2896 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
2897 /// instances for the function.
2898 ///
2899 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
2900 /// inlined instance; the die_offset points to the inlined_subroutine die in
2901 /// the __debug_info section, and the low_pc is the starting address for the
2902 /// inlining instance.
2903 void EmitDebugInlineInfo() {
2904 if (!TAI->doesDwarfUsesInlineInfoSection())
2905 return;
2906
2907 if (!MainCU)
2908 return;
2909
2910 Asm->SwitchToDataSection(TAI->getDwarfDebugInlineSection());
2911 Asm->EOL();
2912 EmitDifference("debug_inlined_end", 1,
2913 "debug_inlined_begin", 1, true);
2914 Asm->EOL("Length of Debug Inlined Information Entry");
2915
2916 EmitLabel("debug_inlined_begin", 1);
2917
2918 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
2919 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2920
2921 for (DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
2922 I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2923 GlobalVariable *GV = I->first;
2924 SmallVector<unsigned, 4> &Labels = I->second;
2925 DISubprogram SP(GV);
2926 std::string Name;
2927 std::string LName;
Devang Patel88bf96e2009-04-13 17:02:03 +00002928 SP.getLinkageName(LName);
2929 SP.getName(Name);
2930
2931 Asm->EmitString(LName.empty() ? Name : LName);
2932 Asm->EOL("MIPS linkage name");
2933
2934 Asm->EmitString(Name); Asm->EOL("Function name");
2935
2936 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2937
2938 for (SmallVector<unsigned, 4>::iterator LI = Labels.begin(),
2939 LE = Labels.end(); LI != LE; ++LI) {
2940 DIE *SP = MainCU->getDieMapSlotFor(GV);
2941 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2942
2943 if (TD->getPointerSize() == sizeof(int32_t))
2944 O << TAI->getData32bitsDirective();
2945 else
2946 O << TAI->getData64bitsDirective();
2947 PrintLabelName("label", *LI); Asm->EOL("low_pc");
2948 }
2949 }
2950
2951 EmitLabel("debug_inlined_end", 1);
2952 Asm->EOL();
2953 }
2954
Bill Wendling278a3922009-03-10 21:47:45 +00002955 /// GetOrCreateSourceID - Look up the source id with the given directory and
2956 /// source file names. If none currently exists, create a new id and insert it
2957 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
2958 /// as well.
2959 unsigned GetOrCreateSourceID(const std::string &DirName,
2960 const std::string &FileName) {
2961 unsigned DId;
2962 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
2963 if (DI != DirectoryIdMap.end()) {
2964 DId = DI->getValue();
2965 } else {
2966 DId = DirectoryNames.size() + 1;
2967 DirectoryIdMap[DirName] = DId;
2968 DirectoryNames.push_back(DirName);
2969 }
2970
2971 unsigned FId;
2972 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
2973 if (FI != SourceFileIdMap.end()) {
2974 FId = FI->getValue();
2975 } else {
2976 FId = SourceFileNames.size() + 1;
2977 SourceFileIdMap[FileName] = FId;
2978 SourceFileNames.push_back(FileName);
2979 }
2980
2981 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
2982 SourceIdMap.find(std::make_pair(DId, FId));
2983 if (SI != SourceIdMap.end())
2984 return SI->second;
2985
2986 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
2987 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
2988 SourceIds.push_back(std::make_pair(DId, FId));
2989
2990 return SrcId;
2991 }
2992
Evan Cheng3e288912009-02-25 07:04:34 +00002993 void ConstructCompileUnit(GlobalVariable *GV) {
2994 DICompileUnit DIUnit(GV);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00002995 std::string Dir, FN, Prod;
2996 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
2997 DIUnit.getFilename(FN));
Evan Cheng3e288912009-02-25 07:04:34 +00002998
2999 DIE *Die = new DIE(DW_TAG_compile_unit);
3000 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
3001 DWLabel("section_line", 0), DWLabel("section_line", 0),
3002 false);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00003003 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer(Prod));
Evan Cheng3e288912009-02-25 07:04:34 +00003004 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendling1c5842b2009-03-09 05:04:40 +00003005 AddString(Die, DW_AT_name, DW_FORM_string, FN);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00003006 if (!Dir.empty())
Bill Wendling1c5842b2009-03-09 05:04:40 +00003007 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Cheng3e288912009-02-25 07:04:34 +00003008 if (DIUnit.isOptimized())
3009 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00003010 std::string Flags;
3011 DIUnit.getFlags(Flags);
3012 if (!Flags.empty())
Evan Cheng3e288912009-02-25 07:04:34 +00003013 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
3014 unsigned RVer = DIUnit.getRunTimeVersion();
3015 if (RVer)
3016 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
3017
3018 CompileUnit *Unit = new CompileUnit(ID, Die);
3019 if (DIUnit.isMain()) {
3020 assert(!MainCU && "Multiple main compile units are found!");
3021 MainCU = Unit;
3022 }
3023 CompileUnitMap[DIUnit.getGV()] = Unit;
3024 CompileUnits.push_back(Unit);
3025 }
3026
Devang Patel289f2362009-01-05 23:11:11 +00003027 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Patelb3907da2009-01-05 23:03:32 +00003028 void ConstructCompileUnits() {
Evan Cheng3e288912009-02-25 07:04:34 +00003029 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.compile_units");
3030 if (!Root)
3031 return;
3032 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3033 "Malformed compile unit descriptor anchor type");
3034 Constant *RootC = cast<Constant>(*Root->use_begin());
3035 assert(RootC->hasNUsesOrMore(1) &&
3036 "Malformed compile unit descriptor anchor type");
3037 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3038 UI != UE; ++UI)
3039 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3040 UUI != UUE; ++UUI) {
3041 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3042 ConstructCompileUnit(GV);
Devang Patel2ae1db52009-01-30 18:20:31 +00003043 }
Evan Cheng3e288912009-02-25 07:04:34 +00003044 }
3045
3046 bool ConstructGlobalVariableDIE(GlobalVariable *GV) {
3047 DIGlobalVariable DI_GV(GV);
3048 CompileUnit *DW_Unit = MainCU;
3049 if (!DW_Unit)
Chris Lattner88ab9742009-05-05 04:55:56 +00003050 DW_Unit = &FindCompileUnit(DI_GV.getCompileUnit());
Evan Cheng3e288912009-02-25 07:04:34 +00003051
3052 // Check for pre-existence.
3053 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
3054 if (Slot)
3055 return false;
3056
3057 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
3058
3059 // Add address.
3060 DIEBlock *Block = new DIEBlock();
3061 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Bill Wendling26a8ab92009-04-10 00:12:49 +00003062 std::string GLN;
Evan Cheng3e288912009-02-25 07:04:34 +00003063 AddObjectLabel(Block, 0, DW_FORM_udata,
Bill Wendling26a8ab92009-04-10 00:12:49 +00003064 Asm->getGlobalLinkName(DI_GV.getGlobal(), GLN));
Evan Cheng3e288912009-02-25 07:04:34 +00003065 AddBlock(VariableDie, DW_AT_location, 0, Block);
3066
3067 // Add to map.
3068 Slot = VariableDie;
Bill Wendling74940d12009-05-06 21:21:34 +00003069
Evan Cheng3e288912009-02-25 07:04:34 +00003070 // Add to context owner.
3071 DW_Unit->getDie()->AddChild(VariableDie);
Bill Wendling74940d12009-05-06 21:21:34 +00003072
Evan Cheng3e288912009-02-25 07:04:34 +00003073 // Expose as global. FIXME - need to check external flag.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00003074 std::string Name;
3075 DW_Unit->AddGlobal(DI_GV.getName(Name), VariableDie);
Evan Cheng3e288912009-02-25 07:04:34 +00003076 return true;
Devang Patelb3907da2009-01-05 23:03:32 +00003077 }
3078
Devang Patel289f2362009-01-05 23:11:11 +00003079 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patela9169c32009-02-24 00:02:15 +00003080 /// visible global variables. Return true if at least one global DIE is
3081 /// created.
3082 bool ConstructGlobalVariableDIEs() {
Evan Cheng3e288912009-02-25 07:04:34 +00003083 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
3084 if (!Root)
3085 return false;
Devang Patel289f2362009-01-05 23:11:11 +00003086
Evan Cheng3e288912009-02-25 07:04:34 +00003087 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3088 "Malformed global variable descriptor anchor type");
3089 Constant *RootC = cast<Constant>(*Root->use_begin());
3090 assert(RootC->hasNUsesOrMore(1) &&
3091 "Malformed global variable descriptor anchor type");
Devang Patel289f2362009-01-05 23:11:11 +00003092
Evan Cheng3e288912009-02-25 07:04:34 +00003093 bool Result = false;
3094 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3095 UI != UE; ++UI)
3096 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
Bill Wendling74940d12009-05-06 21:21:34 +00003097 UUI != UUE; ++UUI)
3098 Result |= ConstructGlobalVariableDIE(cast<GlobalVariable>(*UUI));
3099
Evan Cheng3e288912009-02-25 07:04:34 +00003100 return Result;
3101 }
Devang Patel289f2362009-01-05 23:11:11 +00003102
Evan Cheng3e288912009-02-25 07:04:34 +00003103 bool ConstructSubprogram(GlobalVariable *GV) {
3104 DISubprogram SP(GV);
3105 CompileUnit *Unit = MainCU;
3106 if (!Unit)
Chris Lattner88ab9742009-05-05 04:55:56 +00003107 Unit = &FindCompileUnit(SP.getCompileUnit());
Devang Patel289f2362009-01-05 23:11:11 +00003108
Evan Cheng3e288912009-02-25 07:04:34 +00003109 // Check for pre-existence.
3110 DIE *&Slot = Unit->getDieMapSlotFor(GV);
3111 if (Slot)
3112 return false;
3113
3114 if (!SP.isDefinition())
3115 // This is a method declaration which will be handled while
3116 // constructing class type.
3117 return false;
3118
3119 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
3120
3121 // Add to map.
3122 Slot = SubprogramDie;
Bill Wendlingd32c9722009-05-07 17:26:14 +00003123
Evan Cheng3e288912009-02-25 07:04:34 +00003124 // Add to context owner.
3125 Unit->getDie()->AddChild(SubprogramDie);
Bill Wendlingd32c9722009-05-07 17:26:14 +00003126
Evan Cheng3e288912009-02-25 07:04:34 +00003127 // Expose as global.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00003128 std::string Name;
3129 Unit->AddGlobal(SP.getName(Name), SubprogramDie);
Evan Cheng3e288912009-02-25 07:04:34 +00003130 return true;
Devang Patel289f2362009-01-05 23:11:11 +00003131 }
3132
Devang Patele6caf012009-01-05 23:21:35 +00003133 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patela9169c32009-02-24 00:02:15 +00003134 /// subprograms. Return true if at least one subprogram DIE is created.
3135 bool ConstructSubprograms() {
Evan Cheng3e288912009-02-25 07:04:34 +00003136 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
3137 if (!Root)
3138 return false;
Devang Patele6caf012009-01-05 23:21:35 +00003139
Evan Cheng3e288912009-02-25 07:04:34 +00003140 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3141 "Malformed subprogram descriptor anchor type");
3142 Constant *RootC = cast<Constant>(*Root->use_begin());
3143 assert(RootC->hasNUsesOrMore(1) &&
3144 "Malformed subprogram descriptor anchor type");
Devang Patele6caf012009-01-05 23:21:35 +00003145
Evan Cheng3e288912009-02-25 07:04:34 +00003146 bool Result = false;
3147 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3148 UI != UE; ++UI)
3149 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
Bill Wendling74940d12009-05-06 21:21:34 +00003150 UUI != UUE; ++UUI)
3151 Result |= ConstructSubprogram(cast<GlobalVariable>(*UUI));
3152
Evan Cheng3e288912009-02-25 07:04:34 +00003153 return Result;
Devang Patele6caf012009-01-05 23:21:35 +00003154 }
3155
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003156public:
3157 //===--------------------------------------------------------------------===//
3158 // Main entry points.
3159 //
Owen Anderson847b99b2008-08-21 00:14:44 +00003160 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendlingd9308a62009-03-10 21:23:25 +00003161 : Dwarf(OS, A, T, "dbg"), MainCU(0),
3162 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
3163 ValuesSet(InitValuesSetSize), Values(), StringPool(), SectionMap(),
3164 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel7b60d552009-04-15 20:41:31 +00003165 FunctionDbgScope(0), DebugTimer(0) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003166 if (TimePassesIsEnabled)
3167 DebugTimer = new Timer("Dwarf Debug Writer",
Bill Wendling148ecc42009-03-10 22:58:53 +00003168 getDwarfTimerGroup());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003169 }
3170 virtual ~DwarfDebug() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003171 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3172 delete Values[j];
Bill Wendlingd9308a62009-03-10 21:23:25 +00003173
Bill Wendlingd32c9722009-05-07 17:26:14 +00003174 for (DenseMap<const GlobalVariable *, DbgScope *>::iterator
3175 I = AbstractInstanceRootMap.begin(),
3176 E = AbstractInstanceRootMap.end(); I != E;++I)
3177 delete I->second;
3178
Bill Wendlingd9308a62009-03-10 21:23:25 +00003179 delete DebugTimer;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003180 }
3181
Bill Wendling278a3922009-03-10 21:47:45 +00003182 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
3183 /// be emitted.
3184 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
3185
Devang Patel9304b382009-01-06 21:07:30 +00003186 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3187 /// This is inovked by the target AsmPrinter.
Devang Patel91d27b02009-01-12 23:09:42 +00003188 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003189 if (TimePassesIsEnabled)
3190 DebugTimer->startTimer();
3191
Bill Wendling6baa18d2009-02-03 21:38:21 +00003192 // Create all the compile unit DIEs.
3193 ConstructCompileUnits();
Devang Patel91d27b02009-01-12 23:09:42 +00003194
Bill Wendlingd9308a62009-03-10 21:23:25 +00003195 if (CompileUnits.empty()) {
3196 if (TimePassesIsEnabled)
Bill Wendling0be24752009-03-10 22:02:13 +00003197 DebugTimer->stopTimer();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003198
Bill Wendling6baa18d2009-02-03 21:38:21 +00003199 return;
Bill Wendlingd9308a62009-03-10 21:23:25 +00003200 }
Devang Patel91d27b02009-01-12 23:09:42 +00003201
Devang Patela9169c32009-02-24 00:02:15 +00003202 // Create DIEs for each of the externally visible global variables.
3203 bool globalDIEs = ConstructGlobalVariableDIEs();
3204
3205 // Create DIEs for each of the externally visible subprograms.
3206 bool subprogramDIEs = ConstructSubprograms();
3207
3208 // If there is not any debug info available for any global variables
3209 // and any subprograms then there is not any debug info to emit.
Bill Wendlingd9308a62009-03-10 21:23:25 +00003210 if (!globalDIEs && !subprogramDIEs) {
3211 if (TimePassesIsEnabled)
Bill Wendling0be24752009-03-10 22:02:13 +00003212 DebugTimer->stopTimer();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003213
Devang Patela9169c32009-02-24 00:02:15 +00003214 return;
Bill Wendlingd9308a62009-03-10 21:23:25 +00003215 }
Devang Patela9169c32009-02-24 00:02:15 +00003216
Bill Wendling6baa18d2009-02-03 21:38:21 +00003217 MMI = mmi;
3218 shouldEmit = true;
3219 MMI->setDebugInfoAvailability(true);
Devang Patel9304b382009-01-06 21:07:30 +00003220
Bill Wendling6baa18d2009-02-03 21:38:21 +00003221 // Prime section data.
3222 SectionMap.insert(TAI->getTextSection());
Devang Patel9304b382009-01-06 21:07:30 +00003223
Bill Wendling6baa18d2009-02-03 21:38:21 +00003224 // Print out .file directives to specify files for .loc directives. These
3225 // are printed out early so that they precede any .loc directives.
3226 if (TAI->hasDotLocAndDotFile()) {
Evan Cheng3e288912009-02-25 07:04:34 +00003227 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
3228 // Remember source id starts at 1.
Bill Wendlingdf25fd62009-03-10 21:59:25 +00003229 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Evan Cheng3e288912009-02-25 07:04:34 +00003230 sys::Path FullPath(getSourceDirectoryName(Id.first));
3231 bool AppendOk =
3232 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendling6baa18d2009-02-03 21:38:21 +00003233 assert(AppendOk && "Could not append filename to directory!");
3234 AppendOk = false;
3235 Asm->EmitFile(i, FullPath.toString());
3236 Asm->EOL();
Devang Patel9304b382009-01-06 21:07:30 +00003237 }
Bill Wendling6baa18d2009-02-03 21:38:21 +00003238 }
Devang Patel9304b382009-01-06 21:07:30 +00003239
Bill Wendling6baa18d2009-02-03 21:38:21 +00003240 // Emit initial sections
3241 EmitInitial();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003242
3243 if (TimePassesIsEnabled)
3244 DebugTimer->stopTimer();
Devang Patel9304b382009-01-06 21:07:30 +00003245 }
3246
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003247 /// BeginModule - Emit all Dwarf sections that should come prior to the
3248 /// content.
3249 void BeginModule(Module *M) {
3250 this->M = M;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003251 }
3252
3253 /// EndModule - Emit all Dwarf sections that should come after the content.
3254 ///
3255 void EndModule() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003256 if (!ShouldEmitDwarfDebug())
3257 return;
3258
3259 if (TimePassesIsEnabled)
3260 DebugTimer->startTimer();
aslc200b112008-08-16 12:57:46 +00003261
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003262 // Standard sections final addresses.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003263 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003264 EmitLabel("text_end", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00003265 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003266 EmitLabel("data_end", 0);
aslc200b112008-08-16 12:57:46 +00003267
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003268 // End text sections.
3269 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003270 Asm->SwitchToSection(SectionMap[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003271 EmitLabel("section_end", i);
3272 }
3273
3274 // Emit common frame information.
3275 EmitCommonDebugFrame();
3276
3277 // Emit function debug frame information
3278 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3279 E = DebugFrames.end(); I != E; ++I)
3280 EmitFunctionDebugFrame(*I);
3281
3282 // Compute DIE offsets and sizes.
3283 SizeAndOffsets();
aslc200b112008-08-16 12:57:46 +00003284
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003285 // Emit all the DIEs into a debug info section
3286 EmitDebugInfo();
aslc200b112008-08-16 12:57:46 +00003287
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003288 // Corresponding abbreviations into a abbrev section.
3289 EmitAbbreviations();
aslc200b112008-08-16 12:57:46 +00003290
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003291 // Emit source line correspondence into a debug line section.
3292 EmitDebugLines();
aslc200b112008-08-16 12:57:46 +00003293
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003294 // Emit info into a debug pubnames section.
3295 EmitDebugPubNames();
aslc200b112008-08-16 12:57:46 +00003296
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003297 // Emit info into a debug str section.
3298 EmitDebugStr();
aslc200b112008-08-16 12:57:46 +00003299
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003300 // Emit info into a debug loc section.
3301 EmitDebugLoc();
aslc200b112008-08-16 12:57:46 +00003302
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003303 // Emit info into a debug aranges section.
3304 EmitDebugARanges();
aslc200b112008-08-16 12:57:46 +00003305
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003306 // Emit info into a debug ranges section.
3307 EmitDebugRanges();
aslc200b112008-08-16 12:57:46 +00003308
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003309 // Emit info into a debug macinfo section.
3310 EmitDebugMacInfo();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003311
Devang Patel88bf96e2009-04-13 17:02:03 +00003312 // Emit inline info.
3313 EmitDebugInlineInfo();
3314
Bill Wendlingd9308a62009-03-10 21:23:25 +00003315 if (TimePassesIsEnabled)
3316 DebugTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003317 }
3318
aslc200b112008-08-16 12:57:46 +00003319 /// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003320 /// emitted immediately after the function entry point.
3321 void BeginFunction(MachineFunction *MF) {
Bill Wendlingc1d211d2009-03-11 00:03:50 +00003322 this->MF = MF;
3323
Bill Wendling50db0792009-02-20 00:44:43 +00003324 if (!ShouldEmitDwarfDebug()) return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003325
Bill Wendlingd9308a62009-03-10 21:23:25 +00003326 if (TimePassesIsEnabled)
3327 DebugTimer->startTimer();
3328
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003329 // Begin accumulating function debug information.
3330 MMI->BeginFunction(MF);
aslc200b112008-08-16 12:57:46 +00003331
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003332 // Assumes in correct section after the entry point.
3333 EmitLabel("func_begin", ++SubprogramCount);
Evan Chenga53c40a2008-02-01 09:10:45 +00003334
Argiris Kirtzidis8cac4932009-05-04 19:23:45 +00003335 // Emit label for the implicitly defined dbg.stoppoint at the start of
3336 // the function.
asl7a969d82009-05-04 19:10:38 +00003337 DebugLoc FDL = MF->getDefaultDebugLoc();
3338 if (!FDL.isUnknown()) {
3339 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
3340 unsigned LabelID = RecordSourceLine(DLT.Line, DLT.Col,
3341 DICompileUnit(DLT.CompileUnit));
3342 Asm->printLabel(LabelID);
Argiris Kirtzidis5e3ef112009-05-03 23:27:19 +00003343 }
3344
Bill Wendlingd9308a62009-03-10 21:23:25 +00003345 if (TimePassesIsEnabled)
3346 DebugTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003347 }
aslc200b112008-08-16 12:57:46 +00003348
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003349 /// EndFunction - Gather and emit post-function debug information.
3350 ///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003351 void EndFunction(MachineFunction *MF) {
Bill Wendling50db0792009-02-20 00:44:43 +00003352 if (!ShouldEmitDwarfDebug()) return;
aslc200b112008-08-16 12:57:46 +00003353
Bill Wendlingd9308a62009-03-10 21:23:25 +00003354 if (TimePassesIsEnabled)
3355 DebugTimer->startTimer();
3356
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003357 // Define end label for subprogram.
3358 EmitLabel("func_end", SubprogramCount);
aslc200b112008-08-16 12:57:46 +00003359
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003360 // Get function line info.
Devang Patel35a078f2009-01-12 22:54:42 +00003361 if (!Lines.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003362 // Get section line info.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003363 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003364 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patel35a078f2009-01-12 22:54:42 +00003365 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003366 // Append the function info to section info.
3367 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patel35a078f2009-01-12 22:54:42 +00003368 Lines.begin(), Lines.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003369 }
aslc200b112008-08-16 12:57:46 +00003370
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003371 // Construct scopes for subprogram.
Devang Patel7b60d552009-04-15 20:41:31 +00003372 if (FunctionDbgScope)
3373 ConstructFunctionDbgScope(FunctionDbgScope);
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003374 else
3375 // FIXME: This is wrong. We are essentially getting past a problem with
3376 // debug information not being able to handle unreachable blocks that have
3377 // debug information in them. In particular, those unreachable blocks that
3378 // have "region end" info in them. That situation results in the "root
3379 // scope" not being created. If that's the case, then emit a "default"
3380 // scope, i.e., one that encompasses the whole function. This isn't
3381 // desirable. And a better way of handling this (and all of the debugging
3382 // information) needs to be explored.
Devang Patel6ccd57e2009-01-13 00:20:51 +00003383 ConstructDefaultDbgScope(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003384
Bill Wendlingd32c9722009-05-07 17:26:14 +00003385 // Construct the DbgScope for abstract instances.
3386 for (SmallVector<DbgScope *, 32>::iterator
3387 I = AbstractInstanceRootList.begin(),
3388 E = AbstractInstanceRootList.end(); I != E; ++I)
3389 ConstructAbstractDbgScope(*I);
3390
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003391 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3392 MMI->getFrameMoves()));
Devang Patela4162952009-01-12 18:48:36 +00003393
3394 // Clear debug info
Devang Patel7b60d552009-04-15 20:41:31 +00003395 if (FunctionDbgScope) {
3396 delete FunctionDbgScope;
Devang Patela4162952009-01-12 18:48:36 +00003397 DbgScopeMap.clear();
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003398 DbgInlinedScopeMap.clear();
3399 InlinedVariableScopes.clear();
Devang Patel7b60d552009-04-15 20:41:31 +00003400 FunctionDbgScope = NULL;
Bill Wendlingd32c9722009-05-07 17:26:14 +00003401 LexicalScopeStack.clear();
3402 AbstractInstanceRootList.clear();
3403 LexicalScopeToConcreteInstMap.clear();
Devang Patela4162952009-01-12 18:48:36 +00003404 }
Devang Patelcb59fd42009-01-12 19:17:34 +00003405
Bill Wendlingd9308a62009-03-10 21:23:25 +00003406 Lines.clear();
3407
3408 if (TimePassesIsEnabled)
3409 DebugTimer->stopTimer();
3410 }
Devang Patelcb59fd42009-01-12 19:17:34 +00003411
3412 /// RecordSourceLine - Records location information and associates it with a
3413 /// label. Returns a unique label ID used to generate a label and provide
3414 /// correspondence to the source line list.
3415 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003416 if (TimePassesIsEnabled)
3417 DebugTimer->startTimer();
3418
Evan Cheng3e288912009-02-25 07:04:34 +00003419 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendling6baa18d2009-02-03 21:38:21 +00003420 assert(Unit && "Unable to find CompileUnit");
Devang Patelcb59fd42009-01-12 19:17:34 +00003421 unsigned ID = MMI->NextLabelID();
3422 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
Bill Wendlingd9308a62009-03-10 21:23:25 +00003423
3424 if (TimePassesIsEnabled)
3425 DebugTimer->stopTimer();
3426
Devang Patelcb59fd42009-01-12 19:17:34 +00003427 return ID;
3428 }
3429
3430 /// RecordSourceLine - Records location information and associates it with a
3431 /// label. Returns a unique label ID used to generate a label and provide
3432 /// correspondence to the source line list.
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +00003433 unsigned RecordSourceLine(unsigned Line, unsigned Col, DICompileUnit CU) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003434 if (TimePassesIsEnabled)
3435 DebugTimer->startTimer();
3436
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +00003437 std::string Dir, Fn;
3438 unsigned Src = GetOrCreateSourceID(CU.getDirectory(Dir),
3439 CU.getFilename(Fn));
Devang Patelcb59fd42009-01-12 19:17:34 +00003440 unsigned ID = MMI->NextLabelID();
3441 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
Bill Wendlingd9308a62009-03-10 21:23:25 +00003442
3443 if (TimePassesIsEnabled)
3444 DebugTimer->stopTimer();
3445
Devang Patelcb59fd42009-01-12 19:17:34 +00003446 return ID;
3447 }
3448
Bill Wendling278a3922009-03-10 21:47:45 +00003449 /// getRecordSourceLineCount - Return the number of source lines in the debug
3450 /// info.
3451 unsigned getRecordSourceLineCount() const {
Devang Patelcb59fd42009-01-12 19:17:34 +00003452 return Lines.size();
3453 }
3454
Bill Wendling278a3922009-03-10 21:47:45 +00003455 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
3456 /// timed. Look up the source id with the given directory and source file
3457 /// names. If none currently exists, create a new id and insert it in the
3458 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
3459 /// well.
Evan Cheng3e288912009-02-25 07:04:34 +00003460 unsigned getOrCreateSourceID(const std::string &DirName,
3461 const std::string &FileName) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003462 if (TimePassesIsEnabled)
3463 DebugTimer->startTimer();
3464
Bill Wendling278a3922009-03-10 21:47:45 +00003465 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003466
3467 if (TimePassesIsEnabled)
3468 DebugTimer->stopTimer();
3469
Evan Cheng3e288912009-02-25 07:04:34 +00003470 return SrcId;
Devang Patelcb59fd42009-01-12 19:17:34 +00003471 }
3472
3473 /// RecordRegionStart - Indicate the start of a region.
Devang Patelcb59fd42009-01-12 19:17:34 +00003474 unsigned RecordRegionStart(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003475 if (TimePassesIsEnabled)
3476 DebugTimer->startTimer();
3477
Devang Patelcb59fd42009-01-12 19:17:34 +00003478 DbgScope *Scope = getOrCreateScope(V);
3479 unsigned ID = MMI->NextLabelID();
3480 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
Bill Wendlingd32c9722009-05-07 17:26:14 +00003481 LexicalScopeStack.push_back(Scope);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003482
3483 if (TimePassesIsEnabled)
3484 DebugTimer->stopTimer();
3485
Devang Patelcb59fd42009-01-12 19:17:34 +00003486 return ID;
3487 }
3488
3489 /// RecordRegionEnd - Indicate the end of a region.
Bill Wendlingd32c9722009-05-07 17:26:14 +00003490 unsigned RecordRegionEnd(GlobalVariable *V, DISubprogram &SP) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003491 if (TimePassesIsEnabled)
3492 DebugTimer->startTimer();
3493
Devang Patelcb59fd42009-01-12 19:17:34 +00003494 unsigned ID = MMI->NextLabelID();
Bill Wendlingd32c9722009-05-07 17:26:14 +00003495 DbgScope *Scope = getOrCreateScope(V);
Devang Patelcb59fd42009-01-12 19:17:34 +00003496 Scope->setEndLabelID(ID);
Bill Wendlingd32c9722009-05-07 17:26:14 +00003497 LexicalScopeStack.pop_back();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003498
3499 if (TimePassesIsEnabled)
3500 DebugTimer->stopTimer();
3501
Devang Patelcb59fd42009-01-12 19:17:34 +00003502 return ID;
3503 }
3504
3505 /// RecordVariable - Indicate the declaration of a local variable.
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003506 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
3507 const MachineInstr *MI) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003508 if (TimePassesIsEnabled)
3509 DebugTimer->startTimer();
3510
Devang Patel2560d922009-01-15 18:25:17 +00003511 DIDescriptor Desc(GV);
3512 DbgScope *Scope = NULL;
Bill Wendlingd9308a62009-03-10 21:23:25 +00003513
Devang Patel2560d922009-01-15 18:25:17 +00003514 if (Desc.getTag() == DW_TAG_variable) {
3515 // GV is a global variable.
3516 DIGlobalVariable DG(GV);
3517 Scope = getOrCreateScope(DG.getContext().getGV());
3518 } else {
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003519 DenseMap<const MachineInstr *, DbgScope *>::iterator
3520 SI = InlinedVariableScopes.find(MI);
Bill Wendling74940d12009-05-06 21:21:34 +00003521
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003522 if (SI != InlinedVariableScopes.end()) {
3523 // or GV is an inlined local variable.
3524 Scope = SI->second;
3525 } else {
Devang Patel2560d922009-01-15 18:25:17 +00003526 // or GV is a local variable.
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003527 DIVariable DV(GV);
3528 Scope = getOrCreateScope(DV.getContext().getGV());
3529 }
Devang Patel2560d922009-01-15 18:25:17 +00003530 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00003531
Bill Wendling6baa18d2009-02-03 21:38:21 +00003532 assert(Scope && "Unable to find variable' scope");
Devang Patel7c8a2772009-01-16 19:28:14 +00003533 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelcb59fd42009-01-12 19:17:34 +00003534 Scope->AddVariable(DV);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003535
3536 if (TimePassesIsEnabled)
3537 DebugTimer->stopTimer();
Devang Patelcb59fd42009-01-12 19:17:34 +00003538 }
Devang Patel88bf96e2009-04-13 17:02:03 +00003539
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003540 //// RecordInlinedFnStart - Indicate the start of inlined subroutine.
Argiris Kirtzidisf4510c02009-05-07 00:16:31 +00003541 unsigned RecordInlinedFnStart(DISubprogram &SP, DICompileUnit CU,
3542 unsigned Line, unsigned Col) {
3543 unsigned LabelID = MMI->NextLabelID();
3544
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003545 if (!TAI->doesDwarfUsesInlineInfoSection())
Argiris Kirtzidisf4510c02009-05-07 00:16:31 +00003546 return LabelID;
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003547
Bill Wendlingb183d0c2009-05-01 08:40:06 +00003548 if (TimePassesIsEnabled)
3549 DebugTimer->startTimer();
3550
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003551 GlobalVariable *GV = SP.getGV();
Bill Wendlingd32c9722009-05-07 17:26:14 +00003552 DenseMap<const GlobalVariable *, DbgScope *>::iterator
3553 II = AbstractInstanceRootMap.find(GV);
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003554
Bill Wendlingd32c9722009-05-07 17:26:14 +00003555 if (II == AbstractInstanceRootMap.end()) {
3556 // Create an abstract instance entry for this inlined function if it
3557 // doesn't already exist.
3558 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(GV));
Bill Wendling0cfbd9b2009-05-01 08:32:14 +00003559
Bill Wendlingd32c9722009-05-07 17:26:14 +00003560 // Get the compile unit context.
3561 CompileUnit *Unit = &FindCompileUnit(SP.getCompileUnit());
3562 DIE *SPDie = Unit->getDieMapSlotFor(GV);
3563 assert(SPDie && "Missing subprogram descriptor!");
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003564
Bill Wendlingd32c9722009-05-07 17:26:14 +00003565 // Mark as being inlined. This makes this subprogram entry an abstract
3566 // instance root.
3567 // FIXME: Our debugger doesn't care about the value of DW_AT_inline, only
3568 // that it's defined. It probably won't change in the future, but this
3569 // could be more elegant.
3570 AddUInt(SPDie, DW_AT_inline, 0, DW_INL_declared_not_inlined);
3571
3572 // Keep track of the scope that's inlined into this function.
3573 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3574 SI = DbgInlinedScopeMap.find(GV);
3575
3576 if (SI == DbgInlinedScopeMap.end())
3577 DbgInlinedScopeMap[GV].push_back(Scope);
3578 else
3579 SI->second.push_back(Scope);
3580
3581 // Track the start label for this inlined function.
3582 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
3583 I = InlineInfo.find(GV);
3584
3585 if (I == InlineInfo.end())
3586 InlineInfo[GV].push_back(LabelID);
3587 else
3588 I->second.push_back(LabelID);
3589
3590 AbstractInstanceRootMap[GV] = Scope;
3591 AbstractInstanceRootList.push_back(Scope);
3592 }
3593
3594 // Create a concrete inlined instance for this inlined function.
3595 DIE *ScopeDie = new DIE(DW_TAG_inlined_subroutine);
3596 CompileUnit *Unit = &FindCompileUnit(SP.getCompileUnit());
3597 DIE *Origin = Unit->getDieMapSlotFor(GV);
3598 AddDIEntry(ScopeDie, DW_AT_abstract_origin, DW_FORM_ref4, Origin);
3599 AddUInt(ScopeDie, DW_AT_call_file, 0, Unit->getID());
3600 AddUInt(ScopeDie, DW_AT_call_line, 0, Line);
3601 AddUInt(ScopeDie, DW_AT_call_column, 0, Col);
3602
3603 LexicalScopeToConcreteInstMap[LexicalScopeStack.back()] = ScopeDie;
Bill Wendlingb183d0c2009-05-01 08:40:06 +00003604
3605 if (TimePassesIsEnabled)
3606 DebugTimer->stopTimer();
Argiris Kirtzidisf4510c02009-05-07 00:16:31 +00003607
3608 return LabelID;
Devang Patel88bf96e2009-04-13 17:02:03 +00003609 }
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003610
3611 /// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
3612 unsigned RecordInlinedFnEnd(DISubprogram &SP) {
Bill Wendlingd32c9722009-05-07 17:26:14 +00003613 // FIXME: This function never seems to be called!!
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003614 if (!TAI->doesDwarfUsesInlineInfoSection())
3615 return 0;
3616
Bill Wendlingb183d0c2009-05-01 08:40:06 +00003617 if (TimePassesIsEnabled)
3618 DebugTimer->startTimer();
3619
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003620 GlobalVariable *GV = SP.getGV();
3621 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3622 I = DbgInlinedScopeMap.find(GV);
Bill Wendling74940d12009-05-06 21:21:34 +00003623
Bill Wendlingb183d0c2009-05-01 08:40:06 +00003624 if (I == DbgInlinedScopeMap.end()) {
3625 if (TimePassesIsEnabled)
3626 DebugTimer->stopTimer();
3627
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003628 return 0;
Bill Wendlingb183d0c2009-05-01 08:40:06 +00003629 }
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003630
3631 SmallVector<DbgScope *, 2> &Scopes = I->second;
Bill Wendling58ed5d22009-04-29 00:15:41 +00003632 assert(!Scopes.empty() && "We should have at least one debug scope!");
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003633 DbgScope *Scope = Scopes.back(); Scopes.pop_back();
3634 unsigned ID = MMI->NextLabelID();
Bill Wendling74940d12009-05-06 21:21:34 +00003635
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003636 MMI->RecordUsedDbgLabel(ID);
3637 Scope->setEndLabelID(ID);
Bill Wendlingb183d0c2009-05-01 08:40:06 +00003638
3639 if (TimePassesIsEnabled)
3640 DebugTimer->stopTimer();
3641
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003642 return ID;
3643 }
3644
3645 /// RecordVariableScope - Record scope for the variable declared by
3646 /// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
3647 /// Record scopes for only inlined subroutine variables. Other
3648 /// variables' scopes are determined during RecordVariable().
3649 void RecordVariableScope(DIVariable &DV, const MachineInstr *DeclareMI) {
Bill Wendlingb183d0c2009-05-01 08:40:06 +00003650 if (TimePassesIsEnabled)
3651 DebugTimer->startTimer();
3652
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003653 DISubprogram SP(DV.getContext().getGV());
Bill Wendlingb183d0c2009-05-01 08:40:06 +00003654
3655 if (SP.isNull()) {
3656 if (TimePassesIsEnabled)
3657 DebugTimer->stopTimer();
3658
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003659 return;
Bill Wendlingb183d0c2009-05-01 08:40:06 +00003660 }
3661
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003662 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3663 I = DbgInlinedScopeMap.find(SP.getGV());
Bill Wendlingb183d0c2009-05-01 08:40:06 +00003664 if (I != DbgInlinedScopeMap.end())
3665 InlinedVariableScopes[DeclareMI] = I->second.back();
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003666
Bill Wendlingb183d0c2009-05-01 08:40:06 +00003667 if (TimePassesIsEnabled)
3668 DebugTimer->stopTimer();
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003669 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003670};
3671
3672//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00003673/// DwarfException - Emits Dwarf exception handling directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003674///
3675class DwarfException : public Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003676 struct FunctionEHFrameInfo {
3677 std::string FnName;
3678 unsigned Number;
3679 unsigned PersonalityIndex;
3680 bool hasCalls;
3681 bool hasLandingPads;
3682 std::vector<MachineMove> Moves;
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003683 const Function * function;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003684
3685 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3686 bool hC, bool hL,
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003687 const std::vector<MachineMove> &M,
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003688 const Function *f):
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003689 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003690 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003691 };
3692
3693 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen85535762008-04-02 00:25:04 +00003694
3695 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3696 /// be emitted.
3697 bool shouldEmitTable;
3698
3699 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3700 /// should be emitted.
3701 bool shouldEmitMoves;
3702
3703 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3704 /// should be emitted.
3705 bool shouldEmitTableModule;
3706
aslc200b112008-08-16 12:57:46 +00003707 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen85535762008-04-02 00:25:04 +00003708 /// should be emitted.
3709 bool shouldEmitMovesModule;
Duncan Sands96144f92008-05-07 19:11:09 +00003710
Bill Wendlingd9308a62009-03-10 21:23:25 +00003711 /// ExceptionTimer - Timer for the Dwarf exception writer.
3712 Timer *ExceptionTimer;
3713
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003714 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3715 ///
3716 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
3717 // Size and sign of stack growth.
3718 int stackGrowth =
3719 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3720 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00003721 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003722
3723 // Begin eh frame section.
3724 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling189bde72008-12-24 08:05:17 +00003725
3726 if (!TAI->doesRequireNonLocalEHFrameLabel())
3727 O << TAI->getEHGlobalPrefix();
3728 O << "EH_frame" << Index << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003729 EmitLabel("section_eh_frame", Index);
3730
3731 // Define base labels.
3732 EmitLabel("eh_frame_common", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003733
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003734 // Define the eh frame length.
3735 EmitDifference("eh_frame_common_end", Index,
3736 "eh_frame_common_begin", Index, true);
3737 Asm->EOL("Length of Common Information Entry");
3738
3739 // EH frame header.
3740 EmitLabel("eh_frame_common_begin", Index);
3741 Asm->EmitInt32((int)0);
3742 Asm->EOL("CIE Identifier Tag");
3743 Asm->EmitInt8(DW_CIE_VERSION);
3744 Asm->EOL("CIE Version");
Duncan Sands96144f92008-05-07 19:11:09 +00003745
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003746 // The personality presence indicates that language specific information
3747 // will show up in the eh frame.
3748 Asm->EmitString(Personality ? "zPLR" : "zR");
3749 Asm->EOL("CIE Augmentation");
Duncan Sands96144f92008-05-07 19:11:09 +00003750
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003751 // Round out reader.
3752 Asm->EmitULEB128Bytes(1);
3753 Asm->EOL("CIE Code Alignment Factor");
3754 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands96144f92008-05-07 19:11:09 +00003755 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00003756 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands96144f92008-05-07 19:11:09 +00003757 Asm->EOL("CIE Return Address Column");
3758
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003759 // If there is a personality, we need to indicate the functions location.
3760 if (Personality) {
3761 Asm->EmitULEB128Bytes(7);
3762 Asm->EOL("Augmentation Size");
Bill Wendling2d369922007-09-11 17:20:55 +00003763
Duncan Sands96144f92008-05-07 19:11:09 +00003764 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendling2d369922007-09-11 17:20:55 +00003765 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands96144f92008-05-07 19:11:09 +00003766 Asm->EOL("Personality (pcrel sdata4 indirect)");
3767 } else {
Bill Wendling2d369922007-09-11 17:20:55 +00003768 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands96144f92008-05-07 19:11:09 +00003769 Asm->EOL("Personality (pcrel sdata4)");
3770 }
Bill Wendling2d369922007-09-11 17:20:55 +00003771
Duncan Sands96144f92008-05-07 19:11:09 +00003772 PrintRelDirective(true);
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003773 O << TAI->getPersonalityPrefix();
3774 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3775 O << TAI->getPersonalitySuffix();
Duncan Sands4cc39532008-05-08 12:33:11 +00003776 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3777 O << "-" << TAI->getPCSymbol();
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003778 Asm->EOL("Personality");
Bill Wendling38cb7c92007-08-25 00:51:55 +00003779
Duncan Sands96144f92008-05-07 19:11:09 +00003780 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3781 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003782
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003783 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3784 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003785 } else {
3786 Asm->EmitULEB128Bytes(1);
3787 Asm->EOL("Augmentation Size");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003788
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003789 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3790 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003791 }
3792
3793 // Indicate locations of general callee saved registers in frame.
3794 std::vector<MachineMove> Moves;
3795 RI->getInitialFrameState(Moves);
Dale Johannesenf5a11532007-11-13 19:13:01 +00003796 EmitFrameMoves(NULL, 0, Moves, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003797
Dale Johannesen388f20f2008-04-30 00:43:29 +00003798 // On Darwin the linker honors the alignment of eh_frame, which means it
3799 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3800 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003801 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003802 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003803 EmitLabel("eh_frame_common_end", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003804
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003805 Asm->EOL();
3806 }
Duncan Sands96144f92008-05-07 19:11:09 +00003807
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003808 /// EmitEHFrame - Emit function exception frame information.
3809 ///
3810 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003811 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
Chris Lattner68433442009-04-13 05:44:34 +00003812
3813 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
3814 "Should not emit 'available externally' functions at all");
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003815
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003816 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3817
3818 // Externally visible entry into the functions eh frame info.
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003819 // If the corresponding function is static, this should not be
3820 // externally visible.
Rafael Espindolaa168fc92009-01-15 20:18:42 +00003821 if (linkage != Function::InternalLinkage &&
Devang Patel245446c2009-01-17 08:05:14 +00003822 linkage != Function::PrivateLinkage) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003823 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3824 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3825 }
3826
Dale Johannesenf09b5992008-01-10 02:03:30 +00003827 // If corresponding function is weak definition, this should be too.
Duncan Sands19d161f2009-03-07 15:45:40 +00003828 if ((linkage == Function::WeakAnyLinkage ||
3829 linkage == Function::WeakODRLinkage ||
3830 linkage == Function::LinkOnceAnyLinkage ||
3831 linkage == Function::LinkOnceODRLinkage) &&
Dale Johannesenf09b5992008-01-10 02:03:30 +00003832 TAI->getWeakDefDirective())
3833 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3834
3835 // If there are no calls then you can't unwind. This may mean we can
3836 // omit the EH Frame, but some environments do not handle weak absolute
aslc200b112008-08-16 12:57:46 +00003837 // symbols.
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003838 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesena9b3e482008-04-08 00:10:24 +00003839 // unwind info is to be available for non-EH uses.
Dale Johannesenf09b5992008-01-10 02:03:30 +00003840 if (!EHFrameInfo.hasCalls &&
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003841 !UnwindTablesMandatory &&
Duncan Sands19d161f2009-03-07 15:45:40 +00003842 ((linkage != Function::WeakAnyLinkage &&
3843 linkage != Function::WeakODRLinkage &&
3844 linkage != Function::LinkOnceAnyLinkage &&
3845 linkage != Function::LinkOnceODRLinkage) ||
Dale Johannesenf09b5992008-01-10 02:03:30 +00003846 !TAI->getWeakDefDirective() ||
3847 TAI->getSupportsWeakOmittedEHFrame()))
aslc200b112008-08-16 12:57:46 +00003848 {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003849 O << EHFrameInfo.FnName << " = 0\n";
aslc200b112008-08-16 12:57:46 +00003850 // This name has no connection to the function, so it might get
3851 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003852 // dead-stripping unconditionally.
3853 if (const char *UsedDirective = TAI->getUsedDirective())
3854 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003855 } else {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003856 O << EHFrameInfo.FnName << ":\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003857
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003858 // EH frame header.
3859 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3860 "eh_frame_begin", EHFrameInfo.Number, true);
3861 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00003862
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003863 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
3864
Bill Wendling189bde72008-12-24 08:05:17 +00003865 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3866 PrintRelDirective(true, true);
3867 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3868
3869 if (!TAI->isAbsoluteEHSectionOffsets())
3870 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3871 } else {
3872 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3873 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3874 true, true, false);
3875 }
3876
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003877 Asm->EOL("FDE CIE offset");
3878
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003879 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003880 Asm->EOL("FDE initial location");
3881 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003882 "eh_func_begin", EHFrameInfo.Number, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003883 Asm->EOL("FDE address range");
Duncan Sands96144f92008-05-07 19:11:09 +00003884
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003885 // If there is a personality and landing pads then point to the language
3886 // specific data area in the exception table.
3887 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands96144f92008-05-07 19:11:09 +00003888 Asm->EmitULEB128Bytes(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003889 Asm->EOL("Augmentation size");
Duncan Sands96144f92008-05-07 19:11:09 +00003890
3891 if (EHFrameInfo.hasLandingPads)
3892 EmitReference("exception", EHFrameInfo.Number, true, true);
3893 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003894 Asm->EmitInt32((int)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003895 Asm->EOL("Language Specific Data Area");
3896 } else {
3897 Asm->EmitULEB128Bytes(0);
3898 Asm->EOL("Augmentation size");
3899 }
Duncan Sands96144f92008-05-07 19:11:09 +00003900
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003901 // Indicate locations of function specific callee saved registers in
3902 // frame.
Devang Patelb28de842009-01-17 08:01:33 +00003903 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00003904 true);
aslc200b112008-08-16 12:57:46 +00003905
Dale Johannesen388f20f2008-04-30 00:43:29 +00003906 // On Darwin the linker honors the alignment of eh_frame, which means it
3907 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3908 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003909 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003910 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003911 EmitLabel("eh_frame_end", EHFrameInfo.Number);
aslc200b112008-08-16 12:57:46 +00003912
3913 // If the function is marked used, this table should be also. We cannot
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003914 // make the mark unconditional in this case, since retaining the table
aslc200b112008-08-16 12:57:46 +00003915 // also retains the function in this case, and there is code around
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003916 // that depends on unused functions (calling undefined externals) being
3917 // dead-stripped to link correctly. Yes, there really is.
3918 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3919 if (const char *UsedDirective = TAI->getUsedDirective())
3920 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3921 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003922 }
3923
Duncan Sands241a0c92007-09-05 11:27:52 +00003924 /// EmitExceptionTable - Emit landing pads and actions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003925 ///
3926 /// The general organization of the table is complex, but the basic concepts
3927 /// are easy. First there is a header which describes the location and
3928 /// organization of the three components that follow.
3929 /// 1. The landing pad site information describes the range of code covered
3930 /// by the try. In our case it's an accumulation of the ranges covered
3931 /// by the invokes in the try. There is also a reference to the landing
3932 /// pad that handles the exception once processed. Finally an index into
3933 /// the actions table.
3934 /// 2. The action table, in our case, is composed of pairs of type ids
3935 /// and next action offset. Starting with the action index from the
3936 /// landing pad site, each type Id is checked for a match to the current
3937 /// exception. If it matches then the exception and type id are passed
3938 /// on to the landing pad. Otherwise the next action is looked up. This
3939 /// chain is terminated with a next action of zero. If no type id is
3940 /// found the the frame is unwound and handling continues.
3941 /// 3. Type id table contains references to all the C++ typeinfo for all
3942 /// catches in the function. This tables is reversed indexed base 1.
3943
3944 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3945 static unsigned SharedTypeIds(const LandingPadInfo *L,
3946 const LandingPadInfo *R) {
3947 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3948 unsigned LSize = LIds.size(), RSize = RIds.size();
3949 unsigned MinSize = LSize < RSize ? LSize : RSize;
3950 unsigned Count = 0;
3951
3952 for (; Count != MinSize; ++Count)
3953 if (LIds[Count] != RIds[Count])
3954 return Count;
3955
3956 return Count;
3957 }
3958
3959 /// PadLT - Order landing pads lexicographically by type id.
3960 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
3961 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3962 unsigned LSize = LIds.size(), RSize = RIds.size();
3963 unsigned MinSize = LSize < RSize ? LSize : RSize;
3964
3965 for (unsigned i = 0; i != MinSize; ++i)
3966 if (LIds[i] != RIds[i])
3967 return LIds[i] < RIds[i];
3968
3969 return LSize < RSize;
3970 }
3971
3972 struct KeyInfo {
3973 static inline unsigned getEmptyKey() { return -1U; }
3974 static inline unsigned getTombstoneKey() { return -2U; }
3975 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner92eea072007-09-17 18:34:04 +00003976 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003977 static bool isPod() { return true; }
3978 };
3979
Duncan Sands241a0c92007-09-05 11:27:52 +00003980 /// ActionEntry - Structure describing an entry in the actions table.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003981 struct ActionEntry {
3982 int ValueForTypeID; // The value to write - may not be equal to the type id.
3983 int NextAction;
3984 struct ActionEntry *Previous;
3985 };
3986
Duncan Sands241a0c92007-09-05 11:27:52 +00003987 /// PadRange - Structure holding a try-range and the associated landing pad.
3988 struct PadRange {
3989 // The index of the landing pad.
3990 unsigned PadIndex;
3991 // The index of the begin and end labels in the landing pad's label lists.
3992 unsigned RangeIndex;
3993 };
3994
3995 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3996
3997 /// CallSiteEntry - Structure describing an entry in the call-site table.
3998 struct CallSiteEntry {
Duncan Sands4ff179f2007-12-19 07:36:31 +00003999 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00004000 unsigned BeginLabel; // zero indicates the start of the function.
4001 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands4ff179f2007-12-19 07:36:31 +00004002 // The landing pad starts at PadLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00004003 unsigned PadLabel; // zero indicates that there is no landing pad.
4004 unsigned Action;
4005 };
4006
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004007 void EmitExceptionTable() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004008 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
4009 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
4010 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
4011 if (PadInfos.empty()) return;
4012
4013 // Sort the landing pads in order of their type ids. This is used to fold
4014 // duplicate actions.
4015 SmallVector<const LandingPadInfo *, 64> LandingPads;
4016 LandingPads.reserve(PadInfos.size());
4017 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
4018 LandingPads.push_back(&PadInfos[i]);
4019 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
4020
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004021 // Negative type ids index into FilterIds, positive type ids index into
4022 // TypeInfos. The value written for a positive type id is just the type
4023 // id itself. For a negative type id, however, the value written is the
4024 // (negative) byte offset of the corresponding FilterIds entry. The byte
4025 // offset is usually equal to the type id, because the FilterIds entries
4026 // are written using a variable width encoding which outputs one byte per
4027 // entry as long as the value written is not too large, but can differ.
4028 // This kind of complication does not occur for positive type ids because
4029 // type infos are output using a fixed width encoding.
4030 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
4031 SmallVector<int, 16> FilterOffsets;
4032 FilterOffsets.reserve(FilterIds.size());
4033 int Offset = -1;
4034 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
4035 E = FilterIds.end(); I != E; ++I) {
4036 FilterOffsets.push_back(Offset);
aslc200b112008-08-16 12:57:46 +00004037 Offset -= TargetAsmInfo::getULEB128Size(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004038 }
4039
Duncan Sands241a0c92007-09-05 11:27:52 +00004040 // Compute the actions table and gather the first action index for each
4041 // landing pad site.
4042 SmallVector<ActionEntry, 32> Actions;
4043 SmallVector<unsigned, 64> FirstActions;
4044 FirstActions.reserve(LandingPads.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004045
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004046 int FirstAction = 0;
Duncan Sands241a0c92007-09-05 11:27:52 +00004047 unsigned SizeActions = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004048 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
4049 const LandingPadInfo *LP = LandingPads[i];
4050 const std::vector<int> &TypeIds = LP->TypeIds;
4051 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
4052 unsigned SizeSiteActions = 0;
4053
4054 if (NumShared < TypeIds.size()) {
4055 unsigned SizeAction = 0;
4056 ActionEntry *PrevAction = 0;
4057
4058 if (NumShared) {
4059 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
4060 assert(Actions.size());
4061 PrevAction = &Actions.back();
aslc200b112008-08-16 12:57:46 +00004062 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
4063 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004064 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
aslc200b112008-08-16 12:57:46 +00004065 SizeAction -=
4066 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004067 SizeAction += -PrevAction->NextAction;
4068 PrevAction = PrevAction->Previous;
4069 }
4070 }
4071
4072 // Compute the actions.
4073 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
4074 int TypeID = TypeIds[I];
4075 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
4076 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
aslc200b112008-08-16 12:57:46 +00004077 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004078
4079 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
aslc200b112008-08-16 12:57:46 +00004080 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004081 SizeSiteActions += SizeAction;
4082
4083 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
4084 Actions.push_back(Action);
4085
4086 PrevAction = &Actions.back();
4087 }
4088
4089 // Record the first action of the landing pad site.
4090 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
4091 } // else identical - re-use previous FirstAction
4092
4093 FirstActions.push_back(FirstAction);
4094
4095 // Compute this sites contribution to size.
4096 SizeActions += SizeSiteActions;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004097 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004098
Duncan Sands4ff179f2007-12-19 07:36:31 +00004099 // Compute the call-site table. The entry for an invoke has a try-range
4100 // containing the call, a non-zero landing pad and an appropriate action.
4101 // The entry for an ordinary call has a try-range containing the call and
4102 // zero for the landing pad and the action. Calls marked 'nounwind' have
4103 // no entry and must not be contained in the try-range of any entry - they
4104 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands241a0c92007-09-05 11:27:52 +00004105 SmallVector<CallSiteEntry, 64> CallSites;
4106
4107 RangeMapType PadMap;
Duncan Sands4ff179f2007-12-19 07:36:31 +00004108 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
4109 // by try-range labels when lowered). Ordinary calls do not, so appropriate
4110 // try-ranges for them need be deduced.
Duncan Sands241a0c92007-09-05 11:27:52 +00004111 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
4112 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands4ff179f2007-12-19 07:36:31 +00004113 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands241a0c92007-09-05 11:27:52 +00004114 unsigned BeginLabel = LandingPad->BeginLabels[j];
4115 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
4116 PadRange P = { i, j };
4117 PadMap[BeginLabel] = P;
4118 }
4119 }
4120
Duncan Sands4ff179f2007-12-19 07:36:31 +00004121 // The end label of the previous invoke or nounwind try-range.
Duncan Sands241a0c92007-09-05 11:27:52 +00004122 unsigned LastLabel = 0;
Duncan Sands4ff179f2007-12-19 07:36:31 +00004123
4124 // Whether there is a potentially throwing instruction (currently this means
4125 // an ordinary call) between the end of the previous try-range and now.
4126 bool SawPotentiallyThrowing = false;
4127
4128 // Whether the last callsite entry was for an invoke.
4129 bool PreviousIsInvoke = false;
4130
Duncan Sands4ff179f2007-12-19 07:36:31 +00004131 // Visit all instructions in order of address.
Duncan Sands241a0c92007-09-05 11:27:52 +00004132 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
4133 I != E; ++I) {
4134 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
4135 MI != E; ++MI) {
Dan Gohmanfa607c92008-07-01 00:05:16 +00004136 if (!MI->isLabel()) {
Chris Lattner5b930372008-01-07 07:27:27 +00004137 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands241a0c92007-09-05 11:27:52 +00004138 continue;
4139 }
4140
Chris Lattnerda4cff12007-12-30 20:50:28 +00004141 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands241a0c92007-09-05 11:27:52 +00004142 assert(BeginLabel && "Invalid label!");
Duncan Sands89372f62007-09-05 14:12:46 +00004143
Duncan Sands4ff179f2007-12-19 07:36:31 +00004144 // End of the previous try-range?
Duncan Sands89372f62007-09-05 14:12:46 +00004145 if (BeginLabel == LastLabel)
Duncan Sands4ff179f2007-12-19 07:36:31 +00004146 SawPotentiallyThrowing = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00004147
Duncan Sands4ff179f2007-12-19 07:36:31 +00004148 // Beginning of a new try-range?
Duncan Sands241a0c92007-09-05 11:27:52 +00004149 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands241a0c92007-09-05 11:27:52 +00004150 if (L == PadMap.end())
Duncan Sands4ff179f2007-12-19 07:36:31 +00004151 // Nope, it was just some random label.
Duncan Sands241a0c92007-09-05 11:27:52 +00004152 continue;
4153
4154 PadRange P = L->second;
4155 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
4156
4157 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
4158 "Inconsistent landing pad map!");
4159
4160 // If some instruction between the previous try-range and this one may
4161 // throw, create a call-site entry with no landing pad for the region
4162 // between the try-ranges.
Duncan Sands4ff179f2007-12-19 07:36:31 +00004163 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00004164 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
4165 CallSites.push_back(Site);
Duncan Sands4ff179f2007-12-19 07:36:31 +00004166 PreviousIsInvoke = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00004167 }
4168
4169 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands4ff179f2007-12-19 07:36:31 +00004170 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands241a0c92007-09-05 11:27:52 +00004171
Duncan Sands4ff179f2007-12-19 07:36:31 +00004172 if (LandingPad->LandingPadLabel) {
4173 // This try-range is for an invoke.
4174 CallSiteEntry Site = {BeginLabel, LastLabel,
4175 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands241a0c92007-09-05 11:27:52 +00004176
Duncan Sands4ff179f2007-12-19 07:36:31 +00004177 // Try to merge with the previous call-site.
4178 if (PreviousIsInvoke) {
Dan Gohman3d436002008-06-21 22:00:54 +00004179 CallSiteEntry &Prev = CallSites.back();
Duncan Sands4ff179f2007-12-19 07:36:31 +00004180 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
4181 // Extend the range of the previous entry.
4182 Prev.EndLabel = Site.EndLabel;
4183 continue;
4184 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004185 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004186
Duncan Sands4ff179f2007-12-19 07:36:31 +00004187 // Otherwise, create a new call-site.
4188 CallSites.push_back(Site);
4189 PreviousIsInvoke = true;
4190 } else {
4191 // Create a gap.
4192 PreviousIsInvoke = false;
4193 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004194 }
4195 }
4196 // If some instruction between the previous try-range and the end of the
4197 // function may throw, create a call-site entry with no landing pad for the
4198 // region following the try-range.
Duncan Sands4ff179f2007-12-19 07:36:31 +00004199 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00004200 CallSiteEntry Site = {LastLabel, 0, 0, 0};
4201 CallSites.push_back(Site);
4202 }
4203
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004204 // Final tallies.
Duncan Sands96144f92008-05-07 19:11:09 +00004205
4206 // Call sites.
4207 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
4208 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
4209 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
4210 unsigned SizeSites = CallSites.size() * (SiteStartSize +
4211 SiteLengthSize +
4212 LandingPadSize);
Duncan Sands241a0c92007-09-05 11:27:52 +00004213 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
aslc200b112008-08-16 12:57:46 +00004214 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands241a0c92007-09-05 11:27:52 +00004215
Duncan Sands96144f92008-05-07 19:11:09 +00004216 // Type infos.
4217 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
4218 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004219
4220 unsigned TypeOffset = sizeof(int8_t) + // Call site format
aslc200b112008-08-16 12:57:46 +00004221 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004222 SizeSites + SizeActions + SizeTypes;
4223
4224 unsigned TotalSize = sizeof(int8_t) + // LPStart format
4225 sizeof(int8_t) + // TType format
aslc200b112008-08-16 12:57:46 +00004226 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004227 TypeOffset;
4228
4229 unsigned SizeAlign = (4 - TotalSize) & 3;
4230
4231 // Begin the exception table.
4232 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng7e7d1942008-02-29 19:36:59 +00004233 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesen841e4982008-10-08 21:50:21 +00004234 O << "GCC_except_table" << SubprogramCount << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004235 for (unsigned i = 0; i != SizeAlign; ++i) {
4236 Asm->EmitInt8(0);
4237 Asm->EOL("Padding");
4238 }
4239 EmitLabel("exception", SubprogramCount);
4240
4241 // Emit the header.
4242 Asm->EmitInt8(DW_EH_PE_omit);
4243 Asm->EOL("LPStart format (DW_EH_PE_omit)");
4244 Asm->EmitInt8(DW_EH_PE_absptr);
4245 Asm->EOL("TType format (DW_EH_PE_absptr)");
4246 Asm->EmitULEB128Bytes(TypeOffset);
4247 Asm->EOL("TType base offset");
4248 Asm->EmitInt8(DW_EH_PE_udata4);
4249 Asm->EOL("Call site format (DW_EH_PE_udata4)");
4250 Asm->EmitULEB128Bytes(SizeSites);
4251 Asm->EOL("Call-site table length");
4252
Duncan Sands241a0c92007-09-05 11:27:52 +00004253 // Emit the landing pad site information.
4254 for (unsigned i = 0; i < CallSites.size(); ++i) {
4255 CallSiteEntry &S = CallSites[i];
4256 const char *BeginTag;
4257 unsigned BeginNumber;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004258
Duncan Sands241a0c92007-09-05 11:27:52 +00004259 if (!S.BeginLabel) {
4260 BeginTag = "eh_func_begin";
4261 BeginNumber = SubprogramCount;
4262 } else {
4263 BeginTag = "label";
4264 BeginNumber = S.BeginLabel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004265 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004266
Duncan Sands241a0c92007-09-05 11:27:52 +00004267 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00004268 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004269 Asm->EOL("Region start");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004270
Duncan Sands241a0c92007-09-05 11:27:52 +00004271 if (!S.EndLabel) {
Dale Johannesen4670be42008-01-15 23:24:56 +00004272 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands96144f92008-05-07 19:11:09 +00004273 true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004274 } else {
Duncan Sands96144f92008-05-07 19:11:09 +00004275 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004276 }
4277 Asm->EOL("Region length");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004278
Duncan Sands96144f92008-05-07 19:11:09 +00004279 if (!S.PadLabel)
4280 Asm->EmitInt32(0);
4281 else
Duncan Sands241a0c92007-09-05 11:27:52 +00004282 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00004283 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004284 Asm->EOL("Landing pad");
4285
4286 Asm->EmitULEB128Bytes(S.Action);
4287 Asm->EOL("Action");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004288 }
4289
4290 // Emit the actions.
4291 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4292 ActionEntry &Action = Actions[I];
4293
4294 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
4295 Asm->EOL("TypeInfo index");
4296 Asm->EmitSLEB128Bytes(Action.NextAction);
4297 Asm->EOL("Next action");
4298 }
4299
4300 // Emit the type ids.
4301 for (unsigned M = TypeInfos.size(); M; --M) {
4302 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00004303
4304 PrintRelDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004305
Bill Wendling26a8ab92009-04-10 00:12:49 +00004306 if (GV) {
4307 std::string GLN;
4308 O << Asm->getGlobalLinkName(GV, GLN);
4309 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004310 O << "0";
Bill Wendling26a8ab92009-04-10 00:12:49 +00004311 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004312
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004313 Asm->EOL("TypeInfo");
4314 }
4315
4316 // Emit the filter typeids.
4317 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4318 unsigned TypeID = FilterIds[j];
4319 Asm->EmitULEB128Bytes(TypeID);
4320 Asm->EOL("Filter TypeInfo index");
4321 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004322
Evan Cheng7e7d1942008-02-29 19:36:59 +00004323 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004324 }
4325
4326public:
4327 //===--------------------------------------------------------------------===//
4328 // Main entry points.
4329 //
Owen Anderson847b99b2008-08-21 00:14:44 +00004330 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendlingd9308a62009-03-10 21:23:25 +00004331 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
4332 shouldEmitTableModule(false), shouldEmitMovesModule(false),
4333 ExceptionTimer(0) {
4334 if (TimePassesIsEnabled)
4335 ExceptionTimer = new Timer("Dwarf Exception Writer",
Bill Wendling148ecc42009-03-10 22:58:53 +00004336 getDwarfTimerGroup());
Bill Wendlingd9308a62009-03-10 21:23:25 +00004337 }
aslc200b112008-08-16 12:57:46 +00004338
Bill Wendlingd9308a62009-03-10 21:23:25 +00004339 virtual ~DwarfException() {
4340 delete ExceptionTimer;
4341 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004342
4343 /// SetModuleInfo - Set machine module information when it's known that pass
4344 /// manager has created it. Set by the target AsmPrinter.
4345 void SetModuleInfo(MachineModuleInfo *mmi) {
4346 MMI = mmi;
4347 }
4348
4349 /// BeginModule - Emit all exception information that should come prior to the
4350 /// content.
4351 void BeginModule(Module *M) {
4352 this->M = M;
4353 }
4354
4355 /// EndModule - Emit all exception information that should come after the
4356 /// content.
4357 void EndModule() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004358 if (TimePassesIsEnabled)
4359 ExceptionTimer->startTimer();
4360
Dale Johannesen85535762008-04-02 00:25:04 +00004361 if (shouldEmitMovesModule || shouldEmitTableModule) {
4362 const std::vector<Function *> Personalities = MMI->getPersonalities();
Evan Cheng3e288912009-02-25 07:04:34 +00004363 for (unsigned i = 0; i < Personalities.size(); ++i)
Dale Johannesen85535762008-04-02 00:25:04 +00004364 EmitCommonEHFrame(Personalities[i], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004365
Dale Johannesen85535762008-04-02 00:25:04 +00004366 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4367 E = EHFrames.end(); I != E; ++I)
4368 EmitEHFrame(*I);
4369 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00004370
4371 if (TimePassesIsEnabled)
4372 ExceptionTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004373 }
4374
aslc200b112008-08-16 12:57:46 +00004375 /// BeginFunction - Gather pre-function exception information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004376 /// emitted immediately after the function entry point.
4377 void BeginFunction(MachineFunction *MF) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004378 if (TimePassesIsEnabled)
4379 ExceptionTimer->startTimer();
4380
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004381 this->MF = MF;
Dale Johannesen85535762008-04-02 00:25:04 +00004382 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen85535762008-04-02 00:25:04 +00004383
Bill Wendlingd9308a62009-03-10 21:23:25 +00004384 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen85535762008-04-02 00:25:04 +00004385 // Map all labels and get rid of any dead landing pads.
4386 MMI->TidyLandingPads();
Bill Wendlingd9308a62009-03-10 21:23:25 +00004387
Dale Johannesen85535762008-04-02 00:25:04 +00004388 // If any landing pads survive, we need an EH table.
4389 if (MMI->getLandingPads().size())
4390 shouldEmitTable = true;
4391
4392 // See if we need frame move info.
Duncan Sandscbc28b12008-07-04 09:55:48 +00004393 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen85535762008-04-02 00:25:04 +00004394 shouldEmitMoves = true;
4395
4396 if (shouldEmitMoves || shouldEmitTable)
4397 // Assumes in correct section after the entry point.
4398 EmitLabel("eh_func_begin", ++SubprogramCount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004399 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00004400
Dale Johannesen85535762008-04-02 00:25:04 +00004401 shouldEmitTableModule |= shouldEmitTable;
4402 shouldEmitMovesModule |= shouldEmitMoves;
Bill Wendlingd9308a62009-03-10 21:23:25 +00004403
4404 if (TimePassesIsEnabled)
4405 ExceptionTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004406 }
4407
4408 /// EndFunction - Gather and emit post-function exception information.
4409 ///
4410 void EndFunction() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004411 if (TimePassesIsEnabled)
4412 ExceptionTimer->startTimer();
4413
Dale Johannesen85535762008-04-02 00:25:04 +00004414 if (shouldEmitMoves || shouldEmitTable) {
4415 EmitLabel("eh_func_end", SubprogramCount);
4416 EmitExceptionTable();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004417
Dale Johannesen85535762008-04-02 00:25:04 +00004418 // Save EH frame information
Bill Wendling26a8ab92009-04-10 00:12:49 +00004419 std::string Name;
4420 EHFrames.push_back(
4421 FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF, Name),
4422 SubprogramCount,
4423 MMI->getPersonalityIndex(),
4424 MF->getFrameInfo()->hasCalls(),
4425 !MMI->getLandingPads().empty(),
4426 MMI->getFrameMoves(),
4427 MF->getFunction()));
Bill Wendlingd9308a62009-03-10 21:23:25 +00004428 }
4429
4430 if (TimePassesIsEnabled)
4431 ExceptionTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004432 }
4433};
4434
4435} // End of namespace llvm
4436
4437//===----------------------------------------------------------------------===//
4438
4439/// Emit - Print the abbreviation using the specified Dwarf writer.
4440///
4441void DIEAbbrev::Emit(const DwarfDebug &DD) const {
4442 // Emit its Dwarf tag type.
4443 DD.getAsm()->EmitULEB128Bytes(Tag);
4444 DD.getAsm()->EOL(TagString(Tag));
aslc200b112008-08-16 12:57:46 +00004445
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004446 // Emit whether it has children DIEs.
4447 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4448 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
aslc200b112008-08-16 12:57:46 +00004449
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004450 // For each attribute description.
4451 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4452 const DIEAbbrevData &AttrData = Data[i];
aslc200b112008-08-16 12:57:46 +00004453
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004454 // Emit attribute type.
4455 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4456 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
aslc200b112008-08-16 12:57:46 +00004457
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004458 // Emit form type.
4459 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4460 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
4461 }
4462
4463 // Mark end of abbreviation.
4464 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4465 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
4466}
4467
4468#ifndef NDEBUG
4469void DIEAbbrev::print(std::ostream &O) {
4470 O << "Abbreviation @"
4471 << std::hex << (intptr_t)this << std::dec
4472 << " "
4473 << TagString(Tag)
4474 << " "
4475 << ChildrenString(ChildrenFlag)
4476 << "\n";
aslc200b112008-08-16 12:57:46 +00004477
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004478 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4479 O << " "
4480 << AttributeString(Data[i].getAttribute())
4481 << " "
4482 << FormEncodingString(Data[i].getForm())
4483 << "\n";
4484 }
4485}
4486void DIEAbbrev::dump() { print(cerr); }
4487#endif
4488
4489//===----------------------------------------------------------------------===//
4490
4491#ifndef NDEBUG
4492void DIEValue::dump() {
4493 print(cerr);
4494}
4495#endif
4496
4497//===----------------------------------------------------------------------===//
4498
4499/// EmitValue - Emit integer of appropriate size.
4500///
4501void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
4502 switch (Form) {
4503 case DW_FORM_flag: // Fall thru
4504 case DW_FORM_ref1: // Fall thru
4505 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
4506 case DW_FORM_ref2: // Fall thru
4507 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
4508 case DW_FORM_ref4: // Fall thru
4509 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
4510 case DW_FORM_ref8: // Fall thru
4511 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4512 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4513 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
4514 default: assert(0 && "DIE Value form not supported yet"); break;
4515 }
4516}
4517
4518/// SizeOf - Determine size of integer value in bytes.
4519///
4520unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4521 switch (Form) {
4522 case DW_FORM_flag: // Fall thru
4523 case DW_FORM_ref1: // Fall thru
4524 case DW_FORM_data1: return sizeof(int8_t);
4525 case DW_FORM_ref2: // Fall thru
4526 case DW_FORM_data2: return sizeof(int16_t);
4527 case DW_FORM_ref4: // Fall thru
4528 case DW_FORM_data4: return sizeof(int32_t);
4529 case DW_FORM_ref8: // Fall thru
4530 case DW_FORM_data8: return sizeof(int64_t);
aslc200b112008-08-16 12:57:46 +00004531 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4532 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004533 default: assert(0 && "DIE Value form not supported yet"); break;
4534 }
4535 return 0;
4536}
4537
4538//===----------------------------------------------------------------------===//
4539
4540/// EmitValue - Emit string value.
4541///
4542void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Bill Wendling15afa002009-03-10 23:57:09 +00004543 DD.getAsm()->EmitString(Str);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004544}
4545
4546//===----------------------------------------------------------------------===//
4547
4548/// EmitValue - Emit label value.
4549///
4550void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004551 bool IsSmall = Form == DW_FORM_data4;
4552 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004553}
4554
4555/// SizeOf - Determine size of label value in bytes.
4556///
4557unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004558 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004559 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004560}
4561
4562//===----------------------------------------------------------------------===//
4563
4564/// EmitValue - Emit label value.
4565///
4566void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004567 bool IsSmall = Form == DW_FORM_data4;
4568 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004569}
4570
4571/// SizeOf - Determine size of label value in bytes.
4572///
4573unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004574 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004575 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004576}
aslc200b112008-08-16 12:57:46 +00004577
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004578//===----------------------------------------------------------------------===//
4579
4580/// EmitValue - Emit delta value.
4581///
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004582void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4583 bool IsSmall = Form == DW_FORM_data4;
4584 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4585 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4586}
4587
4588/// SizeOf - Determine size of delta value in bytes.
4589///
4590unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4591 if (Form == DW_FORM_data4) return 4;
4592 return DD.getTargetData()->getPointerSize();
4593}
aslc200b112008-08-16 12:57:46 +00004594
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004595//===----------------------------------------------------------------------===//
4596
4597/// EmitValue - Emit delta value.
4598///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004599void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
4600 bool IsSmall = Form == DW_FORM_data4;
4601 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
4602}
4603
4604/// SizeOf - Determine size of delta value in bytes.
4605///
4606unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4607 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004608 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004609}
4610
4611//===----------------------------------------------------------------------===//
4612
4613/// EmitValue - Emit debug information entry offset.
4614///
4615void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
4616 DD.getAsm()->EmitInt32(Entry->getOffset());
4617}
aslc200b112008-08-16 12:57:46 +00004618
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004619//===----------------------------------------------------------------------===//
4620
4621/// ComputeSize - calculate the size of the block.
4622///
4623unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
4624 if (!Size) {
Owen Anderson88dd6232008-06-24 21:44:59 +00004625 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004626
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004627 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4628 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
4629 }
4630 }
4631 return Size;
4632}
4633
4634/// EmitValue - Emit block data.
4635///
4636void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
4637 switch (Form) {
4638 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4639 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4640 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4641 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
4642 default: assert(0 && "Improper form for block"); break;
4643 }
aslc200b112008-08-16 12:57:46 +00004644
Owen Anderson88dd6232008-06-24 21:44:59 +00004645 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004646
4647 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4648 DD.getAsm()->EOL();
4649 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
4650 }
4651}
4652
4653/// SizeOf - Determine size of block data in bytes.
4654///
4655unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4656 switch (Form) {
4657 case DW_FORM_block1: return Size + sizeof(int8_t);
4658 case DW_FORM_block2: return Size + sizeof(int16_t);
4659 case DW_FORM_block4: return Size + sizeof(int32_t);
aslc200b112008-08-16 12:57:46 +00004660 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004661 default: assert(0 && "Improper form for block"); break;
4662 }
4663 return 0;
4664}
4665
4666//===----------------------------------------------------------------------===//
4667/// DIE Implementation
4668
4669DIE::~DIE() {
4670 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4671 delete Children[i];
4672}
aslc200b112008-08-16 12:57:46 +00004673
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004674/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4675///
4676void DIE::AddSiblingOffset() {
4677 DIEInteger *DI = new DIEInteger(0);
4678 Values.insert(Values.begin(), DI);
4679 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
4680}
4681
4682/// Profile - Used to gather unique data for the value folding set.
4683///
4684void DIE::Profile(FoldingSetNodeID &ID) {
4685 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00004686
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004687 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4688 ID.AddPointer(Children[i]);
4689
4690 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4691 ID.AddPointer(Values[j]);
4692}
4693
4694#ifndef NDEBUG
4695void DIE::print(std::ostream &O, unsigned IncIndent) {
4696 static unsigned IndentCount = 0;
4697 IndentCount += IncIndent;
4698 const std::string Indent(IndentCount, ' ');
4699 bool isBlock = Abbrev.getTag() == 0;
aslc200b112008-08-16 12:57:46 +00004700
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004701 if (!isBlock) {
4702 O << Indent
4703 << "Die: "
4704 << "0x" << std::hex << (intptr_t)this << std::dec
4705 << ", Offset: " << Offset
4706 << ", Size: " << Size
aslc200b112008-08-16 12:57:46 +00004707 << "\n";
4708
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004709 O << Indent
4710 << TagString(Abbrev.getTag())
4711 << " "
4712 << ChildrenString(Abbrev.getChildrenFlag());
4713 } else {
4714 O << "Size: " << Size;
4715 }
4716 O << "\n";
4717
Owen Anderson88dd6232008-06-24 21:44:59 +00004718 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004719
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004720 IndentCount += 2;
4721 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4722 O << Indent;
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004723
4724 if (!isBlock)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004725 O << AttributeString(Data[i].getAttribute());
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004726 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004727 O << "Blk[" << i << "]";
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004728
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004729 O << " "
4730 << FormEncodingString(Data[i].getForm())
4731 << " ";
4732 Values[i]->print(O);
4733 O << "\n";
4734 }
4735 IndentCount -= 2;
4736
4737 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4738 Children[j]->print(O, 4);
4739 }
aslc200b112008-08-16 12:57:46 +00004740
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004741 if (!isBlock) O << "\n";
4742 IndentCount -= IncIndent;
4743}
4744
4745void DIE::dump() {
4746 print(cerr);
4747}
4748#endif
4749
4750//===----------------------------------------------------------------------===//
4751/// DwarfWriter Implementation
4752///
4753
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004754DwarfWriter::DwarfWriter()
Bill Wendlingd9308a62009-03-10 21:23:25 +00004755 : ImmutablePass(&ID), DD(0), DE(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004756
4757DwarfWriter::~DwarfWriter() {
4758 delete DE;
4759 delete DD;
4760}
4761
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004762/// BeginModule - Emit all Dwarf sections that should come prior to the
4763/// content.
Devang Patelaa1e8432009-01-08 23:40:34 +00004764void DwarfWriter::BeginModule(Module *M,
4765 MachineModuleInfo *MMI,
4766 raw_ostream &OS, AsmPrinter *A,
4767 const TargetAsmInfo *T) {
4768 DE = new DwarfException(OS, A, T);
4769 DD = new DwarfDebug(OS, A, T);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004770 DE->BeginModule(M);
4771 DD->BeginModule(M);
Devang Patel6ccd57e2009-01-13 00:20:51 +00004772 DD->SetDebugInfo(MMI);
Devang Patelaa1e8432009-01-08 23:40:34 +00004773 DE->SetModuleInfo(MMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004774}
4775
4776/// EndModule - Emit all Dwarf sections that should come after the content.
4777///
4778void DwarfWriter::EndModule() {
4779 DE->EndModule();
4780 DD->EndModule();
4781}
4782
aslc200b112008-08-16 12:57:46 +00004783/// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004784/// emitted immediately after the function entry point.
4785void DwarfWriter::BeginFunction(MachineFunction *MF) {
4786 DE->BeginFunction(MF);
4787 DD->BeginFunction(MF);
4788}
4789
4790/// EndFunction - Gather and emit post-function debug information.
4791///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00004792void DwarfWriter::EndFunction(MachineFunction *MF) {
4793 DD->EndFunction(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004794 DE->EndFunction();
aslc200b112008-08-16 12:57:46 +00004795
Bill Wendling5b4796a2008-07-22 00:53:37 +00004796 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004797 // Clear function debug information.
4798 MMI->EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004799}
Devang Patelcb59fd42009-01-12 19:17:34 +00004800
4801/// RecordSourceLine - Records location information and associates it with a
4802/// label. Returns a unique label ID used to generate a label and provide
4803/// correspondence to the source line list.
4804unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +00004805 DICompileUnit CU) {
4806 return DD->RecordSourceLine(Line, Col, CU);
Devang Patelcb59fd42009-01-12 19:17:34 +00004807}
4808
4809/// RecordRegionStart - Indicate the start of a region.
4810unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004811 return DD->RecordRegionStart(V);
Devang Patelcb59fd42009-01-12 19:17:34 +00004812}
4813
4814/// RecordRegionEnd - Indicate the end of a region.
Bill Wendlingd32c9722009-05-07 17:26:14 +00004815unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V, DISubprogram &SP) {
4816 return DD->RecordRegionEnd(V, SP);
Devang Patelcb59fd42009-01-12 19:17:34 +00004817}
4818
4819/// getRecordSourceLineCount - Count source lines.
4820unsigned DwarfWriter::getRecordSourceLineCount() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004821 return DD->getRecordSourceLineCount();
Devang Patelcb59fd42009-01-12 19:17:34 +00004822}
Devang Patel70190872009-01-13 21:25:00 +00004823
Devang Patelfe359e72009-01-13 21:44:10 +00004824/// RecordVariable - Indicate the declaration of a local variable.
4825///
Devang Patel8a9a7dc2009-04-15 00:10:26 +00004826void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
4827 const MachineInstr *MI) {
4828 DD->RecordVariable(GV, FrameIndex, MI);
Devang Patelfe359e72009-01-13 21:44:10 +00004829}
Devang Patel42f6bed2009-01-13 23:54:55 +00004830
Bill Wendling50db0792009-02-20 00:44:43 +00004831/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
4832/// be emitted.
4833bool DwarfWriter::ShouldEmitDwarfDebug() const {
Argiris Kirtzidis25657342009-05-03 08:50:41 +00004834 return DD && DD->ShouldEmitDwarfDebug();
Bill Wendling50db0792009-02-20 00:44:43 +00004835}
Devang Patel88bf96e2009-04-13 17:02:03 +00004836
Devang Patel8a9a7dc2009-04-15 00:10:26 +00004837//// RecordInlinedFnStart - Global variable GV is inlined at the location marked
Devang Patel88bf96e2009-04-13 17:02:03 +00004838//// by LabelID label.
Argiris Kirtzidisf4510c02009-05-07 00:16:31 +00004839unsigned DwarfWriter::RecordInlinedFnStart(DISubprogram SP, DICompileUnit CU,
4840 unsigned Line, unsigned Col) {
4841 return DD->RecordInlinedFnStart(SP, CU, Line, Col);
Devang Patel88bf96e2009-04-13 17:02:03 +00004842}
4843
Devang Patel8a9a7dc2009-04-15 00:10:26 +00004844/// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
Argiris Kirtzidisf4510c02009-05-07 00:16:31 +00004845unsigned DwarfWriter::RecordInlinedFnEnd(DISubprogram SP) {
Devang Patel8a9a7dc2009-04-15 00:10:26 +00004846 return DD->RecordInlinedFnEnd(SP);
4847}
4848
4849/// RecordVariableScope - Record scope for the variable declared by
4850/// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
4851void DwarfWriter::RecordVariableScope(DIVariable &DV,
4852 const MachineInstr *DeclareMI) {
4853 DD->RecordVariableScope(DV, DeclareMI);
4854}