blob: 75686e10c5dacb6d229346d0e3616b82cffd09ea [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.
Devang Patel4d1709e2009-01-08 02:33:41 +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; }
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001148};
1149
1150
1151//===----------------------------------------------------------------------===//
1152/// DbgInlinedSubroutineScope - This class is used to track inlined subroutine
1153/// scope information.
1154///
1155class DbgInlinedSubroutineScope : public DbgScope {
1156 unsigned Src;
1157 unsigned Line;
1158 unsigned Col;
1159public:
1160 DbgInlinedSubroutineScope(DbgScope *P, DIDescriptor D,
1161 unsigned S, unsigned L, unsigned C)
1162 : DbgScope(P, D), Src(S), Line(L), Col(C)
1163 {}
1164
1165 unsigned getLine() { return Line; }
1166 unsigned getColumn() { return Col; }
1167 unsigned getFile() { return Src; }
1168 bool isInlinedSubroutine() { return true; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001169};
1170
1171//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00001172/// DwarfDebug - Emits Dwarf debug directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001173///
1174class DwarfDebug : public Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001175 //===--------------------------------------------------------------------===//
1176 // Attributes used to construct specific Dwarf sections.
1177 //
aslc200b112008-08-16 12:57:46 +00001178
Evan Cheng3e288912009-02-25 07:04:34 +00001179 /// CompileUnitMap - A map of global variables representing compile units to
1180 /// compile units.
1181 DenseMap<Value *, CompileUnit *> CompileUnitMap;
1182
1183 /// CompileUnits - All the compile units in this module.
1184 ///
1185 SmallVector<CompileUnit *, 8> CompileUnits;
aslc200b112008-08-16 12:57:46 +00001186
Devang Patel2ae1db52009-01-30 18:20:31 +00001187 /// MainCU - Some platform prefers one compile unit per .o file. In such
1188 /// cases, all dies are inserted in MainCU.
1189 CompileUnit *MainCU;
Bill Wendlinge0f3a262009-02-20 20:40:28 +00001190
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001191 /// AbbreviationsSet - Used to uniquely define abbreviations.
1192 ///
1193 FoldingSet<DIEAbbrev> AbbreviationsSet;
1194
1195 /// Abbreviations - A list of all the unique abbreviations in use.
1196 ///
1197 std::vector<DIEAbbrev *> Abbreviations;
aslc200b112008-08-16 12:57:46 +00001198
Evan Cheng3e288912009-02-25 07:04:34 +00001199 /// DirectoryIdMap - Directory name to directory id map.
1200 ///
1201 StringMap<unsigned> DirectoryIdMap;
Devang Patel5f244e32009-01-05 22:35:52 +00001202
Evan Cheng3e288912009-02-25 07:04:34 +00001203 /// DirectoryNames - A list of directory names.
1204 SmallVector<std::string, 8> DirectoryNames;
1205
1206 /// SourceFileIdMap - Source file name to source file id map.
1207 ///
1208 StringMap<unsigned> SourceFileIdMap;
1209
1210 /// SourceFileNames - A list of source file names.
1211 SmallVector<std::string, 8> SourceFileNames;
1212
1213 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
1214 /// id mapped to a unique id.
1215 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
1216
1217 /// SourceIds - Reverse map from source id to directory id + file id pair.
1218 ///
1219 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
Devang Patel5f244e32009-01-05 22:35:52 +00001220
Devang Patel9b829452009-01-16 21:07:53 +00001221 /// Lines - List of of source line correspondence.
Devang Patel7dd15a92009-01-08 17:19:22 +00001222 std::vector<SrcLineInfo> Lines;
1223
Devang Patel9b829452009-01-16 21:07:53 +00001224 /// ValuesSet - Used to uniquely define values.
1225 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001226 FoldingSet<DIEValue> ValuesSet;
aslc200b112008-08-16 12:57:46 +00001227
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001228 /// Values - A list of all the unique values in use.
1229 ///
1230 std::vector<DIEValue *> Values;
aslc200b112008-08-16 12:57:46 +00001231
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001232 /// StringPool - A UniqueVector of strings used by indirect references.
1233 ///
1234 UniqueVector<std::string> StringPool;
1235
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001236 /// SectionMap - Provides a unique id per text section.
1237 ///
Anton Korobeynikov55b94962008-09-24 22:15:21 +00001238 UniqueVector<const Section*> SectionMap;
aslc200b112008-08-16 12:57:46 +00001239
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001240 /// SectionSourceLines - Tracks line numbers per text section.
1241 ///
Devang Patel35a078f2009-01-12 22:54:42 +00001242 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001243
1244 /// didInitial - Flag to indicate if initial emission has been done.
1245 ///
1246 bool didInitial;
aslc200b112008-08-16 12:57:46 +00001247
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001248 /// shouldEmit - Flag to indicate if debug information should be emitted.
1249 ///
1250 bool shouldEmit;
1251
Devang Patel7b60d552009-04-15 20:41:31 +00001252 // FunctionDbgScope - Top level scope for the current function.
Devang Patel4d1709e2009-01-08 02:33:41 +00001253 //
Devang Patel7b60d552009-04-15 20:41:31 +00001254 DbgScope *FunctionDbgScope;
Devang Patel4d1709e2009-01-08 02:33:41 +00001255
Bill Wendlingd9308a62009-03-10 21:23:25 +00001256 /// DbgScopeMap - Tracks the scopes in the current function.
Devang Patel4d1709e2009-01-08 02:33:41 +00001257 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
Bill Wendlingd9308a62009-03-10 21:23:25 +00001258
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001259 /// DbgInlinedScopeMap - Tracks inlined scopes in the current function.
1260 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> > DbgInlinedScopeMap;
1261
Devang Patel88bf96e2009-04-13 17:02:03 +00001262 /// InlineInfo - Keep track of inlined functions and their location.
1263 /// This information is used to populate debug_inlined section.
1264 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> > InlineInfo;
1265
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001266 /// InlinedVariableScopes - Scopes information for the inlined subroutine
1267 /// variables.
1268 DenseMap<const MachineInstr *, DbgScope *> InlinedVariableScopes;
1269
Bill Wendlingd9308a62009-03-10 21:23:25 +00001270 /// DebugTimer - Timer for the Dwarf debug writer.
1271 Timer *DebugTimer;
Devang Patel4d1709e2009-01-08 02:33:41 +00001272
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001273 struct FunctionDebugFrameInfo {
1274 unsigned Number;
1275 std::vector<MachineMove> Moves;
1276
1277 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman9ba5d4d2007-08-27 14:50:10 +00001278 Number(Num), Moves(M) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001279 };
1280
1281 std::vector<FunctionDebugFrameInfo> DebugFrames;
aslc200b112008-08-16 12:57:46 +00001282
Bill Wendlingd9308a62009-03-10 21:23:25 +00001283private:
Bill Wendlingdf25fd62009-03-10 21:59:25 +00001284 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
Bill Wendling278a3922009-03-10 21:47:45 +00001285 /// maps to the source id. Source id starts at 1.
1286 std::pair<unsigned, unsigned>
Bill Wendlingdf25fd62009-03-10 21:59:25 +00001287 getSourceDirectoryAndFileIds(unsigned SId) const {
Bill Wendling278a3922009-03-10 21:47:45 +00001288 return SourceIds[SId-1];
1289 }
1290
1291 /// getNumSourceDirectories - Return the number of source directories in the
1292 /// debug info.
1293 unsigned getNumSourceDirectories() const {
1294 return DirectoryNames.size();
1295 }
1296
1297 /// getSourceDirectoryName - Return the name of the directory corresponding
1298 /// to the id.
1299 const std::string &getSourceDirectoryName(unsigned Id) const {
1300 return DirectoryNames[Id - 1];
1301 }
1302
1303 /// getSourceFileName - Return the name of the source file corresponding
1304 /// to the id.
1305 const std::string &getSourceFileName(unsigned Id) const {
1306 return SourceFileNames[Id - 1];
1307 }
1308
1309 /// getNumSourceIds - Return the number of unique source ids.
Bill Wendling278a3922009-03-10 21:47:45 +00001310 unsigned getNumSourceIds() const {
1311 return SourceIds.size();
1312 }
1313
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001314 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
aslc200b112008-08-16 12:57:46 +00001315 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001316 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1317 // Profile the node so that we can make it unique.
1318 FoldingSetNodeID ID;
1319 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00001320
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001321 // Check the set for priors.
1322 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
aslc200b112008-08-16 12:57:46 +00001323
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001324 // If it's newly added.
1325 if (InSet == &Abbrev) {
aslc200b112008-08-16 12:57:46 +00001326 // Add to abbreviation list.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001327 Abbreviations.push_back(&Abbrev);
1328 // Assign the vector position + 1 as its number.
1329 Abbrev.setNumber(Abbreviations.size());
1330 } else {
1331 // Assign existing abbreviation number.
1332 Abbrev.setNumber(InSet->getNumber());
1333 }
1334 }
1335
1336 /// NewString - Add a string to the constant pool and returns a label.
1337 ///
1338 DWLabel NewString(const std::string &String) {
1339 unsigned StringID = StringPool.insert(String);
1340 return DWLabel("string", StringID);
1341 }
aslc200b112008-08-16 12:57:46 +00001342
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001343 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1344 /// entry.
1345 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1346 DIEntry *Value;
aslc200b112008-08-16 12:57:46 +00001347
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001348 if (Entry) {
1349 FoldingSetNodeID ID;
1350 DIEntry::Profile(ID, Entry);
1351 void *Where;
1352 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
aslc200b112008-08-16 12:57:46 +00001353
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001354 if (Value) return Value;
aslc200b112008-08-16 12:57:46 +00001355
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001356 Value = new DIEntry(Entry);
1357 ValuesSet.InsertNode(Value, Where);
1358 } else {
1359 Value = new DIEntry(Entry);
1360 }
aslc200b112008-08-16 12:57:46 +00001361
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001362 Values.push_back(Value);
1363 return Value;
1364 }
aslc200b112008-08-16 12:57:46 +00001365
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001366 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1367 ///
1368 void SetDIEntry(DIEntry *Value, DIE *Entry) {
Bill Wendling15afa002009-03-10 23:57:09 +00001369 Value->setEntry(Entry);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001370 // Add to values set if not already there. If it is, we merely have a
1371 // duplicate in the values list (no harm.)
1372 ValuesSet.GetOrInsertNode(Value);
1373 }
1374
1375 /// AddUInt - Add an unsigned integer attribute data and value.
1376 ///
1377 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1378 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1379
1380 FoldingSetNodeID ID;
1381 DIEInteger::Profile(ID, Integer);
1382 void *Where;
1383 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1384 if (!Value) {
1385 Value = new DIEInteger(Integer);
1386 ValuesSet.InsertNode(Value, Where);
1387 Values.push_back(Value);
1388 }
aslc200b112008-08-16 12:57:46 +00001389
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001390 Die->AddValue(Attribute, Form, Value);
1391 }
aslc200b112008-08-16 12:57:46 +00001392
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001393 /// AddSInt - Add an signed integer attribute data and value.
1394 ///
1395 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1396 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1397
1398 FoldingSetNodeID ID;
1399 DIEInteger::Profile(ID, (uint64_t)Integer);
1400 void *Where;
1401 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1402 if (!Value) {
1403 Value = new DIEInteger(Integer);
1404 ValuesSet.InsertNode(Value, Where);
1405 Values.push_back(Value);
1406 }
aslc200b112008-08-16 12:57:46 +00001407
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001408 Die->AddValue(Attribute, Form, Value);
1409 }
aslc200b112008-08-16 12:57:46 +00001410
Evan Cheng3e288912009-02-25 07:04:34 +00001411 /// AddString - Add a string attribute data and value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001412 ///
1413 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1414 const std::string &String) {
1415 FoldingSetNodeID ID;
1416 DIEString::Profile(ID, String);
1417 void *Where;
1418 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1419 if (!Value) {
1420 Value = new DIEString(String);
1421 ValuesSet.InsertNode(Value, Where);
1422 Values.push_back(Value);
1423 }
aslc200b112008-08-16 12:57:46 +00001424
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001425 Die->AddValue(Attribute, Form, Value);
1426 }
aslc200b112008-08-16 12:57:46 +00001427
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001428 /// AddLabel - Add a Dwarf label attribute data and value.
1429 ///
1430 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1431 const DWLabel &Label) {
1432 FoldingSetNodeID ID;
1433 DIEDwarfLabel::Profile(ID, Label);
1434 void *Where;
1435 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1436 if (!Value) {
1437 Value = new DIEDwarfLabel(Label);
1438 ValuesSet.InsertNode(Value, Where);
1439 Values.push_back(Value);
1440 }
aslc200b112008-08-16 12:57:46 +00001441
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001442 Die->AddValue(Attribute, Form, Value);
1443 }
aslc200b112008-08-16 12:57:46 +00001444
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001445 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1446 ///
1447 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1448 const std::string &Label) {
1449 FoldingSetNodeID ID;
1450 DIEObjectLabel::Profile(ID, Label);
1451 void *Where;
1452 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1453 if (!Value) {
1454 Value = new DIEObjectLabel(Label);
1455 ValuesSet.InsertNode(Value, Where);
1456 Values.push_back(Value);
1457 }
aslc200b112008-08-16 12:57:46 +00001458
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001459 Die->AddValue(Attribute, Form, Value);
1460 }
aslc200b112008-08-16 12:57:46 +00001461
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001462 /// AddSectionOffset - Add a section offset label attribute data and value.
1463 ///
1464 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1465 const DWLabel &Label, const DWLabel &Section,
1466 bool isEH = false, bool useSet = true) {
1467 FoldingSetNodeID ID;
1468 DIESectionOffset::Profile(ID, Label, Section);
1469 void *Where;
1470 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1471 if (!Value) {
1472 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1473 ValuesSet.InsertNode(Value, Where);
1474 Values.push_back(Value);
1475 }
aslc200b112008-08-16 12:57:46 +00001476
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001477 Die->AddValue(Attribute, Form, Value);
1478 }
aslc200b112008-08-16 12:57:46 +00001479
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001480 /// AddDelta - Add a label delta attribute data and value.
1481 ///
1482 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001483 const DWLabel &Hi, const DWLabel &Lo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001484 FoldingSetNodeID ID;
1485 DIEDelta::Profile(ID, Hi, Lo);
1486 void *Where;
1487 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1488 if (!Value) {
1489 Value = new DIEDelta(Hi, Lo);
1490 ValuesSet.InsertNode(Value, Where);
1491 Values.push_back(Value);
1492 }
aslc200b112008-08-16 12:57:46 +00001493
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001494 Die->AddValue(Attribute, Form, Value);
1495 }
aslc200b112008-08-16 12:57:46 +00001496
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001497 /// AddDIEntry - Add a DIE attribute data and value.
1498 ///
1499 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1500 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1501 }
1502
1503 /// AddBlock - Add block data.
1504 ///
1505 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1506 Block->ComputeSize(*this);
1507 FoldingSetNodeID ID;
1508 Block->Profile(ID);
1509 void *Where;
1510 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1511 if (!Value) {
1512 Value = Block;
1513 ValuesSet.InsertNode(Value, Where);
1514 Values.push_back(Value);
1515 } else {
Chris Lattner3de66892007-09-21 18:25:53 +00001516 // Already exists, reuse the previous one.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001517 delete Block;
Chris Lattner3de66892007-09-21 18:25:53 +00001518 Block = cast<DIEBlock>(Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001519 }
aslc200b112008-08-16 12:57:46 +00001520
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001521 Die->AddValue(Attribute, Block->BestForm(), Value);
1522 }
1523
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001524 /// AddSourceLine - Add location information to specified debug information
1525 /// entry.
Devang Patel7c8a2772009-01-16 19:28:14 +00001526 void AddSourceLine(DIE *Die, const DIVariable *V) {
Chris Lattner88ab9742009-05-05 04:55:56 +00001527 // If there is no compile unit specified, don't add a line #.
1528 if (V->getCompileUnit().isNull())
1529 return;
1530
Devang Patel4d1709e2009-01-08 02:33:41 +00001531 unsigned Line = V->getLineNumber();
Chris Lattner88ab9742009-05-05 04:55:56 +00001532 unsigned FileID = FindCompileUnit(V->getCompileUnit()).getID();
1533 assert(FileID && "Invalid file id");
Devang Patel4d1709e2009-01-08 02:33:41 +00001534 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1535 AddUInt(Die, DW_AT_decl_line, 0, Line);
1536 }
1537
1538 /// AddSourceLine - Add location information to specified debug information
1539 /// entry.
Devang Patel7c8a2772009-01-16 19:28:14 +00001540 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Chris Lattner88ab9742009-05-05 04:55:56 +00001541 // If there is no compile unit specified, don't add a line #.
1542 if (G->getCompileUnit().isNull())
1543 return;
Devang Patel5f244e32009-01-05 22:35:52 +00001544 unsigned Line = G->getLineNumber();
Chris Lattner88ab9742009-05-05 04:55:56 +00001545 unsigned FileID = FindCompileUnit(G->getCompileUnit()).getID();
1546 assert(FileID && "Invalid file id");
Devang Patel5f244e32009-01-05 22:35:52 +00001547 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1548 AddUInt(Die, DW_AT_decl_line, 0, Line);
1549 }
1550
Devang Patel7c8a2772009-01-16 19:28:14 +00001551 void AddSourceLine(DIE *Die, const DIType *Ty) {
Chris Lattner88ab9742009-05-05 04:55:56 +00001552 // If there is no compile unit specified, don't add a line #.
Devang Patel2ae1db52009-01-30 18:20:31 +00001553 DICompileUnit CU = Ty->getCompileUnit();
1554 if (CU.isNull())
1555 return;
Chris Lattner88ab9742009-05-05 04:55:56 +00001556
1557 unsigned Line = Ty->getLineNumber();
1558 unsigned FileID = FindCompileUnit(CU).getID();
1559 assert(FileID && "Invalid file id");
Devang Patel5f244e32009-01-05 22:35:52 +00001560 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1561 AddUInt(Die, DW_AT_decl_line, 0, Line);
1562 }
1563
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001564 /// AddAddress - Add an address attribute to a die based on the location
1565 /// provided.
1566 void AddAddress(DIE *Die, unsigned Attribute,
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001567 const MachineLocation &Location) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001568 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001569 DIEBlock *Block = new DIEBlock();
aslc200b112008-08-16 12:57:46 +00001570
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001571 if (Location.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001572 if (Reg < 32) {
1573 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1574 } else {
1575 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1576 AddUInt(Block, 0, DW_FORM_udata, Reg);
1577 }
1578 } else {
1579 if (Reg < 32) {
1580 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1581 } else {
1582 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1583 AddUInt(Block, 0, DW_FORM_udata, Reg);
1584 }
1585 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1586 }
aslc200b112008-08-16 12:57:46 +00001587
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001588 AddBlock(Die, Attribute, 0, Block);
1589 }
aslc200b112008-08-16 12:57:46 +00001590
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001591 /// AddType - Add a new type attribute to the specified entity.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001592 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel165ed512009-01-23 19:13:31 +00001593 if (Ty.isNull())
Devang Patel4a4cbe72009-01-05 21:47:57 +00001594 return;
Devang Patel4a4cbe72009-01-05 21:47:57 +00001595
1596 // Check for pre-existence.
1597 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1598 // If it exists then use the existing value.
1599 if (Slot) {
1600 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1601 return;
1602 }
1603
1604 // Set up proxy.
1605 Slot = NewDIEntry();
1606
1607 // Construct type.
1608 DIE Buffer(DW_TAG_base_type);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001609 if (Ty.isBasicType(Ty.getTag()))
1610 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1611 else if (Ty.isDerivedType(Ty.getTag()))
1612 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1613 else {
Bill Wendling824a8bf2009-02-03 21:17:20 +00001614 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelef4bf3b2009-01-15 19:26:23 +00001615 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1616 }
1617
Devang Patelb0cb07c2009-01-27 23:22:55 +00001618 // Add debug information entry to entity and appropriate context.
1619 DIE *Die = NULL;
1620 DIDescriptor Context = Ty.getContext();
1621 if (!Context.isNull())
1622 Die = DW_Unit->getDieMapSlotFor(Context.getGV());
1623
1624 if (Die) {
1625 DIE *Child = new DIE(Buffer);
1626 Die->AddChild(Child);
1627 Buffer.Detach();
1628 SetDIEntry(Slot, Child);
Bill Wendling824a8bf2009-02-03 21:17:20 +00001629 } else {
Devang Patelb0cb07c2009-01-27 23:22:55 +00001630 Die = DW_Unit->AddDie(Buffer);
1631 SetDIEntry(Slot, Die);
1632 }
1633
Devang Patel4a4cbe72009-01-05 21:47:57 +00001634 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1635 }
1636
Devang Patel46d13752009-01-05 19:07:53 +00001637 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1638 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001639 DIBasicType BTy) {
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001640
Devang Patelfc187162009-01-05 17:57:47 +00001641 // Get core information.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001642 std::string Name;
1643 BTy.getName(Name);
Devang Patelfc187162009-01-05 17:57:47 +00001644 Buffer.setTag(DW_TAG_base_type);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001645 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patelfc187162009-01-05 17:57:47 +00001646 // Add name if not anonymous or intermediate type.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001647 if (!Name.empty())
Devang Patelfc187162009-01-05 17:57:47 +00001648 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001649 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelfc187162009-01-05 17:57:47 +00001650 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1651 }
1652
Devang Patel46d13752009-01-05 19:07:53 +00001653 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1654 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001655 DIDerivedType DTy) {
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001656
Devang Patelfc187162009-01-05 17:57:47 +00001657 // Get core information.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001658 std::string Name;
1659 DTy.getName(Name);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001660 uint64_t Size = DTy.getSizeInBits() >> 3;
1661 unsigned Tag = DTy.getTag();
Bill Wendling1c5842b2009-03-09 05:04:40 +00001662
Devang Patelfc187162009-01-05 17:57:47 +00001663 // FIXME - Workaround for templates.
1664 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1665
1666 Buffer.setTag(Tag);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001667
Devang Patelfc187162009-01-05 17:57:47 +00001668 // Map to main type, void will not have a type.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001669 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel4a4cbe72009-01-05 21:47:57 +00001670 AddType(DW_Unit, &Buffer, FromTy);
Devang Patelfc187162009-01-05 17:57:47 +00001671
1672 // Add name if not anonymous or intermediate type.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001673 if (!Name.empty())
Evan Cheng3e288912009-02-25 07:04:34 +00001674 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelfc187162009-01-05 17:57:47 +00001675
1676 // Add size if non-zero (derived types might be zero-sized.)
1677 if (Size)
1678 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1679
1680 // Add source line info if available and TyDesc is not a forward
1681 // declaration.
Devang Patele34e0882009-01-27 00:45:04 +00001682 if (!DTy.isForwardDecl())
1683 AddSourceLine(&Buffer, &DTy);
Devang Patelfc187162009-01-05 17:57:47 +00001684 }
1685
Devang Patel30c01372009-01-05 19:55:51 +00001686 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1687 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001688 DICompositeType CTy) {
Devang Patelb28de842009-01-17 08:01:33 +00001689 // Get core information.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001690 std::string Name;
1691 CTy.getName(Name);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001692
Devang Patelef4bf3b2009-01-15 19:26:23 +00001693 uint64_t Size = CTy.getSizeInBits() >> 3;
1694 unsigned Tag = CTy.getTag();
Devang Patel8050bd72009-01-23 01:19:09 +00001695 Buffer.setTag(Tag);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001696
Devang Patel30c01372009-01-05 19:55:51 +00001697 switch (Tag) {
1698 case DW_TAG_vector_type:
1699 case DW_TAG_array_type:
Devang Patelef4bf3b2009-01-15 19:26:23 +00001700 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patel30c01372009-01-05 19:55:51 +00001701 break;
Devang Patel3798f492009-01-20 18:35:14 +00001702 case DW_TAG_enumeration_type:
1703 {
1704 DIArray Elements = CTy.getTypeArray();
1705 // Add enumerators to enumeration type.
1706 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1707 DIE *ElemDie = NULL;
1708 DIEnumerator Enum(Elements.getElement(i).getGV());
1709 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
1710 Buffer.AddChild(ElemDie);
1711 }
1712 }
1713 break;
Devang Patel30c01372009-01-05 19:55:51 +00001714 case DW_TAG_subroutine_type:
1715 {
1716 // Add prototype flag.
1717 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001718 DIArray Elements = CTy.getTypeArray();
Devang Patel30c01372009-01-05 19:55:51 +00001719 // Add return type.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001720 DIDescriptor RTy = Elements.getElement(0);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001721 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel4a4cbe72009-01-05 21:47:57 +00001722
Devang Patel30c01372009-01-05 19:55:51 +00001723 // Add arguments.
1724 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1725 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel4a4cbe72009-01-05 21:47:57 +00001726 DIDescriptor Ty = Elements.getElement(i);
Devang Pateld40a7e52009-01-17 06:57:25 +00001727 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patel30c01372009-01-05 19:55:51 +00001728 Buffer.AddChild(Arg);
1729 }
1730 }
1731 break;
1732 case DW_TAG_structure_type:
1733 case DW_TAG_union_type:
Devang Patel09353602009-03-25 00:28:40 +00001734 case DW_TAG_class_type:
Devang Patel30c01372009-01-05 19:55:51 +00001735 {
1736 // Add elements to structure type.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001737 DIArray Elements = CTy.getTypeArray();
Devang Patelcf7acb12009-01-16 00:50:53 +00001738
1739 // A forward struct declared type may not have elements available.
1740 if (Elements.isNull())
1741 break;
1742
Devang Patel30c01372009-01-05 19:55:51 +00001743 // Add elements to structure type.
1744 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1745 DIDescriptor Element = Elements.getElement(i);
Devang Patelb28de842009-01-17 08:01:33 +00001746 DIE *ElemDie = NULL;
Devang Patelef4bf3b2009-01-15 19:26:23 +00001747 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel245446c2009-01-17 08:05:14 +00001748 ElemDie = CreateSubprogramDIE(DW_Unit,
1749 DISubprogram(Element.getGV()));
Devang Patelb28de842009-01-17 08:01:33 +00001750 else if (Element.getTag() == dwarf::DW_TAG_variable) // ???
1751 ElemDie = CreateGlobalVariableDIE(DW_Unit,
1752 DIGlobalVariable(Element.getGV()));
Devang Patel5c643892009-01-20 21:02:02 +00001753 else
1754 ElemDie = CreateMemberDIE(DW_Unit,
1755 DIDerivedType(Element.getGV()));
Devang Patel245446c2009-01-17 08:05:14 +00001756 Buffer.AddChild(ElemDie);
Devang Patel30c01372009-01-05 19:55:51 +00001757 }
Devang Patel74193d72009-02-17 22:43:44 +00001758 unsigned RLang = CTy.getRunTimeLang();
1759 if (RLang)
1760 AddUInt(&Buffer, DW_AT_APPLE_runtime_class, DW_FORM_data1, RLang);
Devang Patel30c01372009-01-05 19:55:51 +00001761 }
1762 break;
1763 default:
1764 break;
1765 }
1766
1767 // Add name if not anonymous or intermediate type.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001768 if (!Name.empty())
Evan Cheng3e288912009-02-25 07:04:34 +00001769 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel30c01372009-01-05 19:55:51 +00001770
Devang Patele34e0882009-01-27 00:45:04 +00001771 if (Tag == DW_TAG_enumeration_type || Tag == DW_TAG_structure_type
1772 || Tag == DW_TAG_union_type) {
1773 // Add size if non-zero (derived types might be zero-sized.)
1774 if (Size)
1775 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1776 else {
1777 // Add zero size if it is not a forward declaration.
1778 if (CTy.isForwardDecl())
1779 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1780 else
1781 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1782 }
1783
1784 // Add source line info if available.
1785 if (!CTy.isForwardDecl())
1786 AddSourceLine(&Buffer, &CTy);
Devang Patel30c01372009-01-05 19:55:51 +00001787 }
Devang Patel30c01372009-01-05 19:55:51 +00001788 }
1789
Bill Wendling824a8bf2009-02-03 21:17:20 +00001790 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1791 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelef4bf3b2009-01-15 19:26:23 +00001792 int64_t L = SR.getLo();
1793 int64_t H = SR.getHi();
Devang Patel6fb54132009-01-05 18:33:01 +00001794 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1795 if (L != H) {
1796 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1797 if (L)
Devang Patel245446c2009-01-17 08:05:14 +00001798 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1799 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel6fb54132009-01-05 18:33:01 +00001800 }
1801 Buffer.AddChild(DW_Subrange);
1802 }
1803
1804 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1805 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1806 DICompositeType *CTy) {
1807 Buffer.setTag(DW_TAG_array_type);
1808 if (CTy->getTag() == DW_TAG_vector_type)
1809 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1810
Devang Patel6ab30e52009-01-28 21:08:20 +00001811 // Emit derived type.
1812 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel6fb54132009-01-05 18:33:01 +00001813 DIArray Elements = CTy->getTypeArray();
Devang Patel6fb54132009-01-05 18:33:01 +00001814
1815 // Construct an anonymous type for index type.
1816 DIE IdxBuffer(DW_TAG_base_type);
1817 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1818 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1819 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1820
1821 // Add subranges to array type.
1822 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel30c01372009-01-05 19:55:51 +00001823 DIDescriptor Element = Elements.getElement(i);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001824 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1825 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel6fb54132009-01-05 18:33:01 +00001826 }
1827 }
1828
Bill Wendling824a8bf2009-02-03 21:17:20 +00001829 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3798f492009-01-20 18:35:14 +00001830 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patela566e812009-01-05 18:38:38 +00001831
1832 DIE *Enumerator = new DIE(DW_TAG_enumerator);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001833 std::string Name;
1834 ETy->getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001835 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
Devang Patela566e812009-01-05 18:38:38 +00001836 int64_t Value = ETy->getEnumValue();
1837 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Patel3798f492009-01-20 18:35:14 +00001838 return Enumerator;
Devang Patela566e812009-01-05 18:38:38 +00001839 }
Devang Patel6fb54132009-01-05 18:33:01 +00001840
Devang Patelb28de842009-01-17 08:01:33 +00001841 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendling824a8bf2009-02-03 21:17:20 +00001842 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patelb28de842009-01-17 08:01:33 +00001843 {
1844 DIE *GVDie = new DIE(DW_TAG_variable);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001845 std::string Name;
1846 GV.getDisplayName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001847 AddString(GVDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001848 std::string LinkageName;
1849 GV.getLinkageName(LinkageName);
1850 if (!LinkageName.empty())
Devang Patelb28de842009-01-17 08:01:33 +00001851 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1852 AddType(DW_Unit, GVDie, GV.getType());
1853 if (!GV.isLocalToUnit())
1854 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
1855 AddSourceLine(GVDie, &GV);
1856 return GVDie;
Devang Patel526b01d2009-01-05 18:59:44 +00001857 }
1858
Devang Patel5c643892009-01-20 21:02:02 +00001859 /// CreateMemberDIE - Create new member DIE.
1860 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
1861 DIE *MemberDie = new DIE(DT.getTag());
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001862 std::string Name;
1863 DT.getName(Name);
1864 if (!Name.empty())
Devang Patel5c643892009-01-20 21:02:02 +00001865 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1866
1867 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1868
1869 AddSourceLine(MemberDie, &DT);
1870
Devang Patelf1f30d42009-02-17 21:23:59 +00001871 uint64_t Size = DT.getSizeInBits();
1872 uint64_t FieldSize = DT.getOriginalTypeSize();
1873
1874 if (Size != FieldSize) {
1875 // Handle bitfield.
1876 AddUInt(MemberDie, DW_AT_byte_size, 0, DT.getOriginalTypeSize() >> 3);
1877 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1878
1879 uint64_t Offset = DT.getOffsetInBits();
1880 uint64_t FieldOffset = Offset;
1881 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1882 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1883 FieldOffset = (HiMark - FieldSize);
1884 Offset -= FieldOffset;
1885 // Maybe we need to work from the other end.
1886 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1887 AddUInt(MemberDie, DW_AT_bit_offset, 0, Offset);
1888 }
Devang Patel5c643892009-01-20 21:02:02 +00001889 DIEBlock *Block = new DIEBlock();
1890 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1891 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1892 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1893
Devang Patel2e7ee192009-01-21 00:08:04 +00001894 if (DT.isProtected())
1895 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1896 else if (DT.isPrivate())
1897 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1898
Devang Patel5c643892009-01-20 21:02:02 +00001899 return MemberDie;
1900 }
1901
Devang Patelb28de842009-01-17 08:01:33 +00001902 /// CreateSubprogramDIE - Create new DIE using SP.
1903 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Devang Patel245446c2009-01-17 08:05:14 +00001904 const DISubprogram &SP,
1905 bool IsConstructor = false) {
Devang Patelb28de842009-01-17 08:01:33 +00001906 DIE *SPDie = new DIE(DW_TAG_subprogram);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001907 std::string Name;
1908 SP.getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001909 AddString(SPDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001910 std::string LinkageName;
1911 SP.getLinkageName(LinkageName);
1912 if (!LinkageName.empty())
Devang Patelb28de842009-01-17 08:01:33 +00001913 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Devang Patel245446c2009-01-17 08:05:14 +00001914 LinkageName);
Devang Patelb28de842009-01-17 08:01:33 +00001915 AddSourceLine(SPDie, &SP);
Devang Patel526b01d2009-01-05 18:59:44 +00001916
Devang Patelb28de842009-01-17 08:01:33 +00001917 DICompositeType SPTy = SP.getType();
1918 DIArray Args = SPTy.getTypeArray();
1919
Devang Patel526b01d2009-01-05 18:59:44 +00001920 // Add Return Type.
Devang Patel6e199962009-04-08 22:18:45 +00001921 unsigned SPTag = SPTy.getTag();
Devang Patel688a19f2009-02-27 18:05:21 +00001922 if (!IsConstructor) {
Devang Patel6e199962009-04-08 22:18:45 +00001923 if (Args.isNull() || SPTag != DW_TAG_subroutine_type)
Devang Patel688a19f2009-02-27 18:05:21 +00001924 AddType(DW_Unit, SPDie, SPTy);
1925 else
1926 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
1927 }
Devang Patel922d1592009-01-30 01:21:46 +00001928
Devang Patelace2cf62009-02-02 17:51:41 +00001929 if (!SP.isDefinition()) {
1930 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
1931 // Add arguments.
1932 // Do not add arguments for subprogram definition. They will be
1933 // handled through RecordVariable.
Devang Patel6e199962009-04-08 22:18:45 +00001934 if (SPTag == DW_TAG_subroutine_type)
Devang Patelace2cf62009-02-02 17:51:41 +00001935 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1936 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1937 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
1938 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
1939 SPDie->AddChild(Arg);
1940 }
1941 }
Devang Patel922d1592009-01-30 01:21:46 +00001942
Devang Patele075e072009-02-24 00:52:19 +00001943 unsigned Lang = SP.getCompileUnit().getLanguage();
1944 if (Lang == DW_LANG_C99 || Lang == DW_LANG_C89
1945 || Lang == DW_LANG_ObjC)
1946 AddUInt(SPDie, DW_AT_prototyped, DW_FORM_flag, 1);
1947
Devang Patelef4bf3b2009-01-15 19:26:23 +00001948 if (!SP.isLocalToUnit())
Devang Patel922d1592009-01-30 01:21:46 +00001949 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001950
1951 // DW_TAG_inlined_subroutine may refer to this DIE.
1952 DIE *&Slot = DW_Unit->getDieMapSlotFor(SP.getGV());
1953 Slot = SPDie;
Devang Patelb28de842009-01-17 08:01:33 +00001954 return SPDie;
Devang Patel526b01d2009-01-05 18:59:44 +00001955 }
1956
Devang Patelb28de842009-01-17 08:01:33 +00001957 /// FindCompileUnit - Get the compile unit for the given descriptor.
1958 ///
Chris Lattner88ab9742009-05-05 04:55:56 +00001959 CompileUnit &FindCompileUnit(DICompileUnit Unit) const {
1960 DenseMap<Value *, CompileUnit *>::const_iterator I =
1961 CompileUnitMap.find(Unit.getGV());
1962 assert(I != CompileUnitMap.end() && "Missing compile unit.");
1963 return *I->second;
Devang Patel5f244e32009-01-05 22:35:52 +00001964 }
1965
Devang Patel42f6bed2009-01-13 23:54:55 +00001966 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel4d1709e2009-01-08 02:33:41 +00001967 ///
1968 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1969 // Get the descriptor.
Devang Patel7c8a2772009-01-16 19:28:14 +00001970 const DIVariable &VD = DV->getVariable();
Devang Patel4d1709e2009-01-08 02:33:41 +00001971
1972 // Translate tag to proper Dwarf tag. The result variable is dropped for
1973 // now.
1974 unsigned Tag;
Devang Patel7c8a2772009-01-16 19:28:14 +00001975 switch (VD.getTag()) {
Devang Patel4d1709e2009-01-08 02:33:41 +00001976 case DW_TAG_return_variable: return NULL;
1977 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1978 case DW_TAG_auto_variable: // fall thru
1979 default: Tag = DW_TAG_variable; break;
1980 }
1981
1982 // Define variable debug information entry.
1983 DIE *VariableDie = new DIE(Tag);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001984 std::string Name;
1985 VD.getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001986 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Devang Patel4d1709e2009-01-08 02:33:41 +00001987
1988 // Add source line info if available.
Devang Patel7c8a2772009-01-16 19:28:14 +00001989 AddSourceLine(VariableDie, &VD);
Devang Patel4d1709e2009-01-08 02:33:41 +00001990
1991 // Add variable type.
Devang Patel7c8a2772009-01-16 19:28:14 +00001992 AddType(Unit, VariableDie, VD.getType());
Devang Patel4d1709e2009-01-08 02:33:41 +00001993
1994 // Add variable address.
1995 MachineLocation Location;
1996 Location.set(RI->getFrameRegister(*MF),
1997 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
1998 AddAddress(VariableDie, DW_AT_location, Location);
1999
2000 return VariableDie;
2001 }
2002
Devang Patel4d1709e2009-01-08 02:33:41 +00002003 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2004 ///
2005 DbgScope *getOrCreateScope(GlobalVariable *V) {
2006 DbgScope *&Slot = DbgScopeMap[V];
Bill Wendlinge0f3a262009-02-20 20:40:28 +00002007 if (Slot) return Slot;
2008
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002009 DbgScope *Parent = NULL;
2010 DIBlock Block(V);
2011 if (!Block.isNull()) {
2012 DIDescriptor ParentDesc = Block.getContext();
2013 Parent =
2014 ParentDesc.isNull() ? NULL : getOrCreateScope(ParentDesc.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002015 }
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002016 Slot = new DbgScope(Parent, DIDescriptor(V));
Bill Wendlinge0f3a262009-02-20 20:40:28 +00002017
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002018 if (Parent)
Bill Wendlinge0f3a262009-02-20 20:40:28 +00002019 Parent->AddScope(Slot);
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002020 else
Bill Wendlinge0f3a262009-02-20 20:40:28 +00002021 // First function is top level function.
Devang Patel7b60d552009-04-15 20:41:31 +00002022 FunctionDbgScope = Slot;
Bill Wendlinge0f3a262009-02-20 20:40:28 +00002023
Devang Patel4d1709e2009-01-08 02:33:41 +00002024 return Slot;
2025 }
2026
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002027 /// createInlinedSubroutineScope - Returns the scope associated with the
2028 /// inlined subroutine.
2029 ///
2030 DbgScope *createInlinedSubroutineScope(DISubprogram SP, unsigned Src,
2031 unsigned Line, unsigned Col) {
2032 DbgScope *Scope =
2033 new DbgInlinedSubroutineScope(NULL, SP, Src, Line, Col);
2034
2035 // FIXME - Add inlined function scopes to the root so we can delete them
2036 // later.
Devang Patel7b60d552009-04-15 20:41:31 +00002037 assert (FunctionDbgScope && "Function scope info missing!");
2038 FunctionDbgScope->AddScope(Scope);
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002039 return Scope;
2040 }
2041
Devang Patel4d1709e2009-01-08 02:33:41 +00002042 /// ConstructDbgScope - Construct the components of a scope.
2043 ///
2044 void ConstructDbgScope(DbgScope *ParentScope,
2045 unsigned ParentStartID, unsigned ParentEndID,
2046 DIE *ParentDie, CompileUnit *Unit) {
2047 // Add variables to scope.
Devang Patel63c22f42009-01-10 02:42:49 +00002048 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
Devang Patel4d1709e2009-01-08 02:33:41 +00002049 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2050 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2051 if (VariableDie) ParentDie->AddChild(VariableDie);
2052 }
2053
2054 // Add nested scopes.
Devang Patel63c22f42009-01-10 02:42:49 +00002055 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel4d1709e2009-01-08 02:33:41 +00002056 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2057 // Define the Scope debug information entry.
2058 DbgScope *Scope = Scopes[j];
Devang Patel4d1709e2009-01-08 02:33:41 +00002059
Devang Patelb9224922009-01-12 18:41:00 +00002060 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2061 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel4d1709e2009-01-08 02:33:41 +00002062
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002063 // Ignore empty scopes.
2064 // Do not ignore inlined scope even if it does not have any
2065 // variables or scopes.
Devang Patel4d1709e2009-01-08 02:33:41 +00002066 if (StartID == EndID && StartID != 0) continue;
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002067 if (!Scope->isInlinedSubroutine()
Devang Patel88bf96e2009-04-13 17:02:03 +00002068 && Scope->getScopes().empty() && Scope->getVariables().empty())
2069 continue;
Devang Patel4d1709e2009-01-08 02:33:41 +00002070
2071 if (StartID == ParentStartID && EndID == ParentEndID) {
2072 // Just add stuff to the parent scope.
2073 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2074 } else {
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002075 DIE *ScopeDie = NULL;
Devang Patel88bcc9e2009-04-15 19:42:57 +00002076 if (MainCU && TAI->doesDwarfUsesInlineInfoSection()
2077 && Scope->isInlinedSubroutine()) {
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002078 ScopeDie = new DIE(DW_TAG_inlined_subroutine);
2079 DIE *Origin = MainCU->getDieMapSlotFor(Scope->getDesc().getGV());
2080 AddDIEntry(ScopeDie, DW_AT_abstract_origin, DW_FORM_ref4, Origin);
2081 AddUInt(ScopeDie, DW_AT_call_file, 0, Scope->getFile());
2082 AddUInt(ScopeDie, DW_AT_call_line, 0, Scope->getLine());
2083 AddUInt(ScopeDie, DW_AT_call_column, 0, Scope->getColumn());
Bill Wendling821048d2009-05-01 08:25:13 +00002084 } else {
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002085 ScopeDie = new DIE(DW_TAG_lexical_block);
Bill Wendling821048d2009-05-01 08:25:13 +00002086 }
2087
2088 // Add the scope bounds.
2089 if (StartID)
2090 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2091 DWLabel("label", StartID));
2092 else
2093 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2094 DWLabel("func_begin", SubprogramCount));
2095
2096 if (EndID)
2097 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2098 DWLabel("label", EndID));
2099 else
2100 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2101 DWLabel("func_end", SubprogramCount));
2102
2103 // Add the scope contents.
2104 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2105 ParentDie->AddChild(ScopeDie);
Devang Patel4d1709e2009-01-08 02:33:41 +00002106 }
2107 }
2108 }
2109
Devang Patel7b60d552009-04-15 20:41:31 +00002110 /// ConstructFunctionDbgScope - Construct the scope for the subprogram.
Devang Patel4d1709e2009-01-08 02:33:41 +00002111 ///
Devang Patel7b60d552009-04-15 20:41:31 +00002112 void ConstructFunctionDbgScope(DbgScope *RootScope) {
Devang Patel4d1709e2009-01-08 02:33:41 +00002113 // Exit if there is no root scope.
2114 if (!RootScope) return;
Devang Patel2560d922009-01-15 18:25:17 +00002115 DIDescriptor Desc = RootScope->getDesc();
2116 if (Desc.isNull())
2117 return;
Devang Patel4d1709e2009-01-08 02:33:41 +00002118
2119 // Get the subprogram debug information entry.
Devang Patel2560d922009-01-15 18:25:17 +00002120 DISubprogram SPD(Desc.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002121
2122 // Get the compile unit context.
Devang Patel2ae1db52009-01-30 18:20:31 +00002123 CompileUnit *Unit = MainCU;
2124 if (!Unit)
Chris Lattner88ab9742009-05-05 04:55:56 +00002125 Unit = &FindCompileUnit(SPD.getCompileUnit());
Devang Patel4d1709e2009-01-08 02:33:41 +00002126
2127 // Get the subprogram die.
Devang Patel57ec9ac2009-01-12 22:58:14 +00002128 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002129 assert(SPDie && "Missing subprogram descriptor");
2130
2131 // Add the function bounds.
2132 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2133 DWLabel("func_begin", SubprogramCount));
2134 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2135 DWLabel("func_end", SubprogramCount));
2136 MachineLocation Location(RI->getFrameRegister(*MF));
2137 AddAddress(SPDie, DW_AT_frame_base, Location);
2138
2139 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2140 }
2141
2142 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2143 ///
2144 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Cheng3e288912009-02-25 07:04:34 +00002145 const char *FnName = MF->getFunction()->getNameStart();
2146 if (MainCU) {
Bill Wendlinge06da442009-04-09 21:49:15 +00002147 StringMap<DIE*> &Globals = MainCU->getGlobals();
2148 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Cheng3e288912009-02-25 07:04:34 +00002149 if (GI != Globals.end()) {
2150 DIE *SPDie = GI->second;
Devang Patel4d1709e2009-01-08 02:33:41 +00002151
2152 // Add the function bounds.
2153 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2154 DWLabel("func_begin", SubprogramCount));
2155 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2156 DWLabel("func_end", SubprogramCount));
2157
2158 MachineLocation Location(RI->getFrameRegister(*MF));
2159 AddAddress(SPDie, DW_AT_frame_base, Location);
2160 return;
2161 }
Evan Cheng3e288912009-02-25 07:04:34 +00002162 } else {
2163 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2164 CompileUnit *Unit = CompileUnits[i];
Bill Wendlinge06da442009-04-09 21:49:15 +00002165 StringMap<DIE*> &Globals = Unit->getGlobals();
2166 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Cheng3e288912009-02-25 07:04:34 +00002167 if (GI != Globals.end()) {
2168 DIE *SPDie = GI->second;
2169
2170 // Add the function bounds.
2171 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2172 DWLabel("func_begin", SubprogramCount));
2173 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2174 DWLabel("func_end", SubprogramCount));
2175
2176 MachineLocation Location(RI->getFrameRegister(*MF));
2177 AddAddress(SPDie, DW_AT_frame_base, Location);
2178 return;
2179 }
2180 }
Devang Patel4d1709e2009-01-08 02:33:41 +00002181 }
Evan Cheng3e288912009-02-25 07:04:34 +00002182
Devang Patel4d1709e2009-01-08 02:33:41 +00002183#if 0
2184 // FIXME: This is causing an abort because C++ mangled names are compared
2185 // with their unmangled counterparts. See PR2885. Don't do this assert.
2186 assert(0 && "Couldn't find DIE for machine function!");
2187#endif
Evan Cheng3e288912009-02-25 07:04:34 +00002188 return;
Devang Patel4d1709e2009-01-08 02:33:41 +00002189 }
2190
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002191 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2192 /// tools to recognize the object file contains Dwarf information.
2193 void EmitInitial() {
2194 // Check to see if we already emitted intial headers.
2195 if (didInitial) return;
2196 didInitial = true;
aslc200b112008-08-16 12:57:46 +00002197
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002198 // Dwarf sections base addresses.
2199 if (TAI->doesDwarfRequireFrameSection()) {
2200 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2201 EmitLabel("section_debug_frame", 0);
2202 }
2203 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2204 EmitLabel("section_info", 0);
2205 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2206 EmitLabel("section_abbrev", 0);
2207 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2208 EmitLabel("section_aranges", 0);
Scott Michel79f01f52009-01-26 22:32:51 +00002209 if (TAI->doesSupportMacInfoSection()) {
2210 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2211 EmitLabel("section_macinfo", 0);
2212 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002213 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2214 EmitLabel("section_line", 0);
2215 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2216 EmitLabel("section_loc", 0);
2217 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2218 EmitLabel("section_pubnames", 0);
2219 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2220 EmitLabel("section_str", 0);
2221 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2222 EmitLabel("section_ranges", 0);
2223
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002224 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002225 EmitLabel("text_begin", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00002226 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002227 EmitLabel("data_begin", 0);
2228 }
2229
2230 /// EmitDIE - Recusively Emits a debug information entry.
2231 ///
2232 void EmitDIE(DIE *Die) {
2233 // Get the abbreviation for this DIE.
2234 unsigned AbbrevNumber = Die->getAbbrevNumber();
2235 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
aslc200b112008-08-16 12:57:46 +00002236
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002237 Asm->EOL();
2238
2239 // Emit the code (index) for the abbreviation.
2240 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng0eeed442008-07-01 23:18:29 +00002241
Evan Cheng42ceb472009-03-25 01:47:28 +00002242 if (Asm->isVerbose())
Evan Cheng0eeed442008-07-01 23:18:29 +00002243 Asm->EOL(std::string("Abbrev [" +
2244 utostr(AbbrevNumber) +
2245 "] 0x" + utohexstr(Die->getOffset()) +
2246 ":0x" + utohexstr(Die->getSize()) + " " +
2247 TagString(Abbrev->getTag())));
2248 else
2249 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00002250
Owen Anderson88dd6232008-06-24 21:44:59 +00002251 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2252 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
aslc200b112008-08-16 12:57:46 +00002253
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002254 // Emit the DIE attribute values.
2255 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2256 unsigned Attr = AbbrevData[i].getAttribute();
2257 unsigned Form = AbbrevData[i].getForm();
2258 assert(Form && "Too many attributes for DIE (check abbreviation)");
aslc200b112008-08-16 12:57:46 +00002259
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002260 switch (Attr) {
2261 case DW_AT_sibling: {
2262 Asm->EmitInt32(Die->SiblingOffset());
2263 break;
2264 }
2265 default: {
2266 // Emit an attribute using the defined form.
2267 Values[i]->EmitValue(*this, Form);
2268 break;
2269 }
2270 }
aslc200b112008-08-16 12:57:46 +00002271
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002272 Asm->EOL(AttributeString(Attr));
2273 }
aslc200b112008-08-16 12:57:46 +00002274
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002275 // Emit the DIE children if any.
2276 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2277 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002278
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002279 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2280 EmitDIE(Children[j]);
2281 }
aslc200b112008-08-16 12:57:46 +00002282
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002283 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2284 }
2285 }
2286
2287 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2288 ///
2289 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2290 // Get the children.
2291 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002292
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002293 // If not last sibling and has children then add sibling offset attribute.
2294 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2295
2296 // Record the abbreviation.
2297 AssignAbbrevNumber(Die->getAbbrev());
aslc200b112008-08-16 12:57:46 +00002298
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002299 // Get the abbreviation for this DIE.
2300 unsigned AbbrevNumber = Die->getAbbrevNumber();
2301 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2302
2303 // Set DIE offset
2304 Die->setOffset(Offset);
aslc200b112008-08-16 12:57:46 +00002305
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002306 // Start the size with the size of abbreviation code.
aslc200b112008-08-16 12:57:46 +00002307 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2308
Owen Anderson88dd6232008-06-24 21:44:59 +00002309 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2310 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002311
2312 // Size the DIE attribute values.
2313 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2314 // Size attribute value.
2315 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2316 }
aslc200b112008-08-16 12:57:46 +00002317
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002318 // Size the DIE children if any.
2319 if (!Children.empty()) {
2320 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2321 "Children flag not set");
aslc200b112008-08-16 12:57:46 +00002322
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002323 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2324 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2325 }
aslc200b112008-08-16 12:57:46 +00002326
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002327 // End of children marker.
2328 Offset += sizeof(int8_t);
2329 }
2330
2331 Die->setSize(Offset - Die->getOffset());
2332 return Offset;
2333 }
2334
2335 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2336 ///
2337 void SizeAndOffsets() {
2338 // Process base compile unit.
Devang Patel2ae1db52009-01-30 18:20:31 +00002339 if (MainCU) {
2340 // Compute size of compile unit header
2341 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2342 sizeof(int16_t) + // DWARF version number
2343 sizeof(int32_t) + // Offset Into Abbrev. Section
2344 sizeof(int8_t); // Pointer Size (in bytes)
2345 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
2346 return;
2347 }
Evan Cheng3e288912009-02-25 07:04:34 +00002348 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2349 CompileUnit *Unit = CompileUnits[i];
Devang Patel6eae2832009-01-12 23:05:55 +00002350 // Compute size of compile unit header
2351 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2352 sizeof(int16_t) + // DWARF version number
2353 sizeof(int32_t) + // Offset Into Abbrev. Section
2354 sizeof(int8_t); // Pointer Size (in bytes)
2355 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2356 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002357 }
2358
Evan Cheng3e288912009-02-25 07:04:34 +00002359 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002360 ///
Evan Cheng3e288912009-02-25 07:04:34 +00002361 void EmitDebugInfoPerCU(CompileUnit *Unit) {
2362 DIE *Die = Unit->getDie();
2363 // Emit the compile units header.
2364 EmitLabel("info_begin", Unit->getID());
2365 // Emit size of content not including length itself
2366 unsigned ContentSize = Die->getSize() +
2367 sizeof(int16_t) + // DWARF version number
2368 sizeof(int32_t) + // Offset Into Abbrev. Section
2369 sizeof(int8_t) + // Pointer Size (in bytes)
2370 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2371
2372 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2373 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2374 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2375 Asm->EOL("Offset Into Abbrev. Section");
2376 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2377
2378 EmitDIE(Die);
2379 // FIXME - extra padding for gdb bug.
2380 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2381 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2382 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2383 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2384 EmitLabel("info_end", Unit->getID());
2385
2386 Asm->EOL();
2387 }
2388
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002389 void EmitDebugInfo() {
2390 // Start debug info section.
2391 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
aslc200b112008-08-16 12:57:46 +00002392
Evan Cheng3e288912009-02-25 07:04:34 +00002393 if (MainCU) {
2394 EmitDebugInfoPerCU(MainCU);
2395 return;
Devang Patel6eae2832009-01-12 23:05:55 +00002396 }
Evan Cheng3e288912009-02-25 07:04:34 +00002397
2398 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2399 EmitDebugInfoPerCU(CompileUnits[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002400 }
2401
2402 /// EmitAbbreviations - Emit the abbreviation section.
2403 ///
2404 void EmitAbbreviations() const {
2405 // Check to see if it is worth the effort.
2406 if (!Abbreviations.empty()) {
2407 // Start the debug abbrev section.
2408 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
aslc200b112008-08-16 12:57:46 +00002409
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002410 EmitLabel("abbrev_begin", 0);
aslc200b112008-08-16 12:57:46 +00002411
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002412 // For each abbrevation.
2413 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2414 // Get abbreviation data
2415 const DIEAbbrev *Abbrev = Abbreviations[i];
aslc200b112008-08-16 12:57:46 +00002416
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002417 // Emit the abbrevations code (base 1 index.)
2418 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2419 Asm->EOL("Abbreviation Code");
aslc200b112008-08-16 12:57:46 +00002420
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002421 // Emit the abbreviations data.
2422 Abbrev->Emit(*this);
aslc200b112008-08-16 12:57:46 +00002423
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002424 Asm->EOL();
2425 }
aslc200b112008-08-16 12:57:46 +00002426
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002427 // Mark end of abbreviations.
2428 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2429
2430 EmitLabel("abbrev_end", 0);
aslc200b112008-08-16 12:57:46 +00002431
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002432 Asm->EOL();
2433 }
2434 }
2435
Bill Wendling1983a2a2008-07-20 00:11:19 +00002436 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2437 /// the line matrix.
aslc200b112008-08-16 12:57:46 +00002438 ///
Bill Wendling1983a2a2008-07-20 00:11:19 +00002439 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2440 // Define last address of section.
2441 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2442 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2443 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2444 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2445
2446 // Mark end of matrix.
2447 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2448 Asm->EmitULEB128Bytes(1); Asm->EOL();
2449 Asm->EmitInt8(1); Asm->EOL();
2450 }
2451
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002452 /// EmitDebugLines - Emit source line information.
2453 ///
2454 void EmitDebugLines() {
Bill Wendling1983a2a2008-07-20 00:11:19 +00002455 // If the target is using .loc/.file, the assembler will be emitting the
2456 // .debug_line table automatically.
2457 if (TAI->hasDotLocAndDotFile())
Dan Gohmanc55b34a2007-09-24 21:43:52 +00002458 return;
2459
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002460 // Minimum line delta, thus ranging from -10..(255-10).
2461 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2462 // Maximum line delta, thus ranging from -10..(255-10).
2463 const int MaxLineDelta = 255 + MinLineDelta;
2464
2465 // Start the dwarf line section.
2466 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
aslc200b112008-08-16 12:57:46 +00002467
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002468 // Construct the section header.
aslc200b112008-08-16 12:57:46 +00002469
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002470 EmitDifference("line_end", 0, "line_begin", 0, true);
2471 Asm->EOL("Length of Source Line Info");
2472 EmitLabel("line_begin", 0);
aslc200b112008-08-16 12:57:46 +00002473
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002474 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
aslc200b112008-08-16 12:57:46 +00002475
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002476 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2477 Asm->EOL("Prolog Length");
2478 EmitLabel("line_prolog_begin", 0);
aslc200b112008-08-16 12:57:46 +00002479
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002480 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2481
2482 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2483
2484 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
aslc200b112008-08-16 12:57:46 +00002485
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002486 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2487
2488 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
aslc200b112008-08-16 12:57:46 +00002489
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002490 // Line number standard opcode encodings argument count
2491 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2492 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2493 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2494 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2495 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2496 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2497 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2498 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2499 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2500
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002501 // Emit directories.
Evan Cheng3e288912009-02-25 07:04:34 +00002502 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2503 Asm->EmitString(getSourceDirectoryName(DI));
2504 Asm->EOL("Directory");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002505 }
2506 Asm->EmitInt8(0); Asm->EOL("End of directories");
aslc200b112008-08-16 12:57:46 +00002507
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002508 // Emit files.
Evan Cheng3e288912009-02-25 07:04:34 +00002509 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2510 // Remember source id starts at 1.
Bill Wendlingdf25fd62009-03-10 21:59:25 +00002511 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Evan Cheng3e288912009-02-25 07:04:34 +00002512 Asm->EmitString(getSourceFileName(Id.second));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002513 Asm->EOL("Source");
Evan Cheng3e288912009-02-25 07:04:34 +00002514 Asm->EmitULEB128Bytes(Id.first);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002515 Asm->EOL("Directory #");
2516 Asm->EmitULEB128Bytes(0);
2517 Asm->EOL("Mod date");
2518 Asm->EmitULEB128Bytes(0);
2519 Asm->EOL("File size");
2520 }
2521 Asm->EmitInt8(0); Asm->EOL("End of files");
aslc200b112008-08-16 12:57:46 +00002522
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002523 EmitLabel("line_prolog_end", 0);
aslc200b112008-08-16 12:57:46 +00002524
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002525 // A sequence for each text section.
Bill Wendling1983a2a2008-07-20 00:11:19 +00002526 unsigned SecSrcLinesSize = SectionSourceLines.size();
2527
2528 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002529 // Isolate current sections line info.
Devang Patel35a078f2009-01-12 22:54:42 +00002530 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng0eeed442008-07-01 23:18:29 +00002531
Evan Cheng42ceb472009-03-25 01:47:28 +00002532 if (Asm->isVerbose()) {
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002533 const Section* S = SectionMap[j + 1];
Evan Cheng3e288912009-02-25 07:04:34 +00002534 O << '\t' << TAI->getCommentString() << " Section"
2535 << S->getName() << '\n';
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002536 } else
Evan Cheng0eeed442008-07-01 23:18:29 +00002537 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002538
2539 // Dwarf assumes we start with first line of first source file.
2540 unsigned Source = 1;
2541 unsigned Line = 1;
aslc200b112008-08-16 12:57:46 +00002542
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002543 // Construct rows of the address, source, line, column matrix.
2544 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patel35a078f2009-01-12 22:54:42 +00002545 const SrcLineInfo &LineInfo = LineInfos[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002546 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2547 if (!LabelID) continue;
aslc200b112008-08-16 12:57:46 +00002548
Evan Cheng42ceb472009-03-25 01:47:28 +00002549 if (!Asm->isVerbose())
Evan Cheng0eeed442008-07-01 23:18:29 +00002550 Asm->EOL();
Evan Cheng3e288912009-02-25 07:04:34 +00002551 else {
2552 std::pair<unsigned, unsigned> SourceID =
Bill Wendlingdf25fd62009-03-10 21:59:25 +00002553 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Evan Cheng3e288912009-02-25 07:04:34 +00002554 O << '\t' << TAI->getCommentString() << ' '
2555 << getSourceDirectoryName(SourceID.first) << ' '
2556 << getSourceFileName(SourceID.second)
2557 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2558 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002559
2560 // Define the line address.
2561 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohmancfb72b22007-09-27 23:12:31 +00002562 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002563 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2564 EmitReference("label", LabelID); Asm->EOL("Location label");
aslc200b112008-08-16 12:57:46 +00002565
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002566 // If change of source, then switch to the new source.
2567 if (Source != LineInfo.getSourceID()) {
2568 Source = LineInfo.getSourceID();
2569 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2570 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2571 }
aslc200b112008-08-16 12:57:46 +00002572
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002573 // If change of line.
2574 if (Line != LineInfo.getLine()) {
2575 // Determine offset.
2576 int Offset = LineInfo.getLine() - Line;
2577 int Delta = Offset - MinLineDelta;
aslc200b112008-08-16 12:57:46 +00002578
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002579 // Update line.
2580 Line = LineInfo.getLine();
aslc200b112008-08-16 12:57:46 +00002581
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002582 // If delta is small enough and in range...
2583 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2584 // ... then use fast opcode.
2585 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2586 } else {
2587 // ... otherwise use long hand.
2588 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2589 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2590 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2591 }
2592 } else {
2593 // Copy the previous row (different address or source)
2594 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2595 }
2596 }
2597
Bill Wendling1983a2a2008-07-20 00:11:19 +00002598 EmitEndOfLineMatrix(j + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002599 }
Bill Wendling1983a2a2008-07-20 00:11:19 +00002600
2601 if (SecSrcLinesSize == 0)
2602 // Because we're emitting a debug_line section, we still need a line
2603 // table. The linker and friends expect it to exist. If there's nothing to
2604 // put into it, emit an empty table.
2605 EmitEndOfLineMatrix(1);
aslc200b112008-08-16 12:57:46 +00002606
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002607 EmitLabel("line_end", 0);
aslc200b112008-08-16 12:57:46 +00002608
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002609 Asm->EOL();
2610 }
aslc200b112008-08-16 12:57:46 +00002611
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002612 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2613 ///
2614 void EmitCommonDebugFrame() {
2615 if (!TAI->doesDwarfRequireFrameSection())
2616 return;
2617
2618 int stackGrowth =
2619 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2620 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00002621 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002622
2623 // Start the dwarf frame section.
2624 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2625
2626 EmitLabel("debug_frame_common", 0);
2627 EmitDifference("debug_frame_common_end", 0,
2628 "debug_frame_common_begin", 0, true);
2629 Asm->EOL("Length of Common Information Entry");
2630
2631 EmitLabel("debug_frame_common_begin", 0);
2632 Asm->EmitInt32((int)DW_CIE_ID);
2633 Asm->EOL("CIE Identifier Tag");
2634 Asm->EmitInt8(DW_CIE_VERSION);
2635 Asm->EOL("CIE Version");
2636 Asm->EmitString("");
2637 Asm->EOL("CIE Augmentation");
2638 Asm->EmitULEB128Bytes(1);
2639 Asm->EOL("CIE Code Alignment Factor");
2640 Asm->EmitSLEB128Bytes(stackGrowth);
aslc200b112008-08-16 12:57:46 +00002641 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00002642 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002643 Asm->EOL("CIE RA Column");
aslc200b112008-08-16 12:57:46 +00002644
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002645 std::vector<MachineMove> Moves;
2646 RI->getInitialFrameState(Moves);
2647
Dale Johannesenf5a11532007-11-13 19:13:01 +00002648 EmitFrameMoves(NULL, 0, Moves, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002649
Evan Cheng7e7d1942008-02-29 19:36:59 +00002650 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002651 EmitLabel("debug_frame_common_end", 0);
aslc200b112008-08-16 12:57:46 +00002652
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002653 Asm->EOL();
2654 }
2655
2656 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2657 /// section.
2658 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
2659 if (!TAI->doesDwarfRequireFrameSection())
2660 return;
aslc200b112008-08-16 12:57:46 +00002661
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002662 // Start the dwarf frame section.
2663 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
aslc200b112008-08-16 12:57:46 +00002664
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002665 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2666 "debug_frame_begin", DebugFrameInfo.Number, true);
2667 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00002668
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002669 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2670
2671 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2672 0, 0, true, false);
2673 Asm->EOL("FDE CIE offset");
2674
2675 EmitReference("func_begin", DebugFrameInfo.Number);
2676 Asm->EOL("FDE initial location");
2677 EmitDifference("func_end", DebugFrameInfo.Number,
2678 "func_begin", DebugFrameInfo.Number);
2679 Asm->EOL("FDE address range");
aslc200b112008-08-16 12:57:46 +00002680
Devang Patelb28de842009-01-17 08:01:33 +00002681 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00002682 false);
aslc200b112008-08-16 12:57:46 +00002683
Evan Cheng7e7d1942008-02-29 19:36:59 +00002684 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002685 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2686
2687 Asm->EOL();
2688 }
2689
Evan Cheng3e288912009-02-25 07:04:34 +00002690 void EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2691 EmitDifference("pubnames_end", Unit->getID(),
2692 "pubnames_begin", Unit->getID(), true);
2693 Asm->EOL("Length of Public Names Info");
2694
2695 EmitLabel("pubnames_begin", Unit->getID());
2696
2697 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2698
2699 EmitSectionOffset("info_begin", "section_info",
2700 Unit->getID(), 0, true, false);
2701 Asm->EOL("Offset of Compilation Unit Info");
2702
2703 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2704 true);
2705 Asm->EOL("Compilation Unit Length");
2706
Bill Wendlinge06da442009-04-09 21:49:15 +00002707 StringMap<DIE*> &Globals = Unit->getGlobals();
Bill Wendling3f94b412009-04-09 23:51:31 +00002708 for (StringMap<DIE*>::const_iterator
Bill Wendlinge06da442009-04-09 21:49:15 +00002709 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
Bill Wendling3f94b412009-04-09 23:51:31 +00002710 const char *Name = GI->getKeyData();
Evan Cheng3e288912009-02-25 07:04:34 +00002711 DIE * Entity = GI->second;
2712
2713 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
Bill Wendling3f94b412009-04-09 23:51:31 +00002714 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
Evan Cheng3e288912009-02-25 07:04:34 +00002715 }
2716
2717 Asm->EmitInt32(0); Asm->EOL("End Mark");
2718 EmitLabel("pubnames_end", Unit->getID());
2719
2720 Asm->EOL();
2721 }
2722
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002723 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2724 ///
2725 void EmitDebugPubNames() {
2726 // Start the dwarf pubnames section.
2727 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
aslc200b112008-08-16 12:57:46 +00002728
Evan Cheng3e288912009-02-25 07:04:34 +00002729 if (MainCU) {
2730 EmitDebugPubNamesPerCU(MainCU);
2731 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002732 }
Evan Cheng3e288912009-02-25 07:04:34 +00002733
2734 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2735 EmitDebugPubNamesPerCU(CompileUnits[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002736 }
2737
2738 /// EmitDebugStr - Emit visible names into a debug str section.
2739 ///
2740 void EmitDebugStr() {
2741 // Check to see if it is worth the effort.
2742 if (!StringPool.empty()) {
2743 // Start the dwarf str section.
2744 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
aslc200b112008-08-16 12:57:46 +00002745
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002746 // For each of strings in the string pool.
2747 for (unsigned StringID = 1, N = StringPool.size();
2748 StringID <= N; ++StringID) {
2749 // Emit a label for reference from debug information entries.
2750 EmitLabel("string", StringID);
2751 // Emit the string itself.
2752 const std::string &String = StringPool[StringID];
2753 Asm->EmitString(String); Asm->EOL();
2754 }
aslc200b112008-08-16 12:57:46 +00002755
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002756 Asm->EOL();
2757 }
2758 }
2759
2760 /// EmitDebugLoc - Emit visible names into a debug loc section.
2761 ///
2762 void EmitDebugLoc() {
2763 // Start the dwarf loc section.
2764 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
aslc200b112008-08-16 12:57:46 +00002765
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002766 Asm->EOL();
2767 }
2768
2769 /// EmitDebugARanges - Emit visible names into a debug aranges section.
2770 ///
2771 void EmitDebugARanges() {
2772 // Start the dwarf aranges section.
2773 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
aslc200b112008-08-16 12:57:46 +00002774
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002775 // FIXME - Mock up
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002776#if 0
aslc200b112008-08-16 12:57:46 +00002777 CompileUnit *Unit = GetBaseCompileUnit();
2778
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002779 // Don't include size of length
2780 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
aslc200b112008-08-16 12:57:46 +00002781
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002782 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
aslc200b112008-08-16 12:57:46 +00002783
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002784 EmitReference("info_begin", Unit->getID());
2785 Asm->EOL("Offset of Compilation Unit Info");
2786
Dan Gohmancfb72b22007-09-27 23:12:31 +00002787 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002788
2789 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2790
2791 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2792 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2793
2794 // Range 1
2795 EmitReference("text_begin", 0); Asm->EOL("Address");
2796 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2797
2798 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2799 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002800#endif
aslc200b112008-08-16 12:57:46 +00002801
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002802 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002803 }
2804
2805 /// EmitDebugRanges - Emit visible names into a debug ranges section.
2806 ///
2807 void EmitDebugRanges() {
2808 // Start the dwarf ranges section.
2809 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
aslc200b112008-08-16 12:57:46 +00002810
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002811 Asm->EOL();
2812 }
2813
2814 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2815 ///
2816 void EmitDebugMacInfo() {
Scott Michel79f01f52009-01-26 22:32:51 +00002817 if (TAI->doesSupportMacInfoSection()) {
2818 // Start the dwarf macinfo section.
2819 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
aslc200b112008-08-16 12:57:46 +00002820
Scott Michel79f01f52009-01-26 22:32:51 +00002821 Asm->EOL();
2822 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002823 }
2824
Devang Patel88bf96e2009-04-13 17:02:03 +00002825 /// EmitDebugInlineInfo - Emit inline info using following format.
2826 /// Section Header:
2827 /// 1. length of section
2828 /// 2. Dwarf version number
2829 /// 3. address size.
2830 ///
2831 /// Entries (one "entry" for each function that was inlined):
2832 ///
2833 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
2834 /// otherwise offset into __debug_str for regular function name.
2835 /// 2. offset into __debug_str section for regular function name.
2836 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
2837 /// instances for the function.
2838 ///
2839 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
2840 /// inlined instance; the die_offset points to the inlined_subroutine die in
2841 /// the __debug_info section, and the low_pc is the starting address for the
2842 /// inlining instance.
2843 void EmitDebugInlineInfo() {
2844 if (!TAI->doesDwarfUsesInlineInfoSection())
2845 return;
2846
2847 if (!MainCU)
2848 return;
2849
2850 Asm->SwitchToDataSection(TAI->getDwarfDebugInlineSection());
2851 Asm->EOL();
2852 EmitDifference("debug_inlined_end", 1,
2853 "debug_inlined_begin", 1, true);
2854 Asm->EOL("Length of Debug Inlined Information Entry");
2855
2856 EmitLabel("debug_inlined_begin", 1);
2857
2858 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
2859 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2860
2861 for (DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
2862 I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2863 GlobalVariable *GV = I->first;
2864 SmallVector<unsigned, 4> &Labels = I->second;
2865 DISubprogram SP(GV);
2866 std::string Name;
2867 std::string LName;
2868
2869 SP.getLinkageName(LName);
2870 SP.getName(Name);
2871
2872 Asm->EmitString(LName.empty() ? Name : LName);
2873 Asm->EOL("MIPS linkage name");
2874
2875 Asm->EmitString(Name); Asm->EOL("Function name");
2876
2877 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2878
2879 for (SmallVector<unsigned, 4>::iterator LI = Labels.begin(),
2880 LE = Labels.end(); LI != LE; ++LI) {
2881 DIE *SP = MainCU->getDieMapSlotFor(GV);
2882 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2883
2884 if (TD->getPointerSize() == sizeof(int32_t))
2885 O << TAI->getData32bitsDirective();
2886 else
2887 O << TAI->getData64bitsDirective();
2888 PrintLabelName("label", *LI); Asm->EOL("low_pc");
2889 }
2890 }
2891
2892 EmitLabel("debug_inlined_end", 1);
2893 Asm->EOL();
2894 }
2895
Bill Wendling278a3922009-03-10 21:47:45 +00002896 /// GetOrCreateSourceID - Look up the source id with the given directory and
2897 /// source file names. If none currently exists, create a new id and insert it
2898 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
2899 /// as well.
2900 unsigned GetOrCreateSourceID(const std::string &DirName,
2901 const std::string &FileName) {
2902 unsigned DId;
2903 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
2904 if (DI != DirectoryIdMap.end()) {
2905 DId = DI->getValue();
2906 } else {
2907 DId = DirectoryNames.size() + 1;
2908 DirectoryIdMap[DirName] = DId;
2909 DirectoryNames.push_back(DirName);
2910 }
2911
2912 unsigned FId;
2913 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
2914 if (FI != SourceFileIdMap.end()) {
2915 FId = FI->getValue();
2916 } else {
2917 FId = SourceFileNames.size() + 1;
2918 SourceFileIdMap[FileName] = FId;
2919 SourceFileNames.push_back(FileName);
2920 }
2921
2922 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
2923 SourceIdMap.find(std::make_pair(DId, FId));
2924 if (SI != SourceIdMap.end())
2925 return SI->second;
2926
2927 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
2928 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
2929 SourceIds.push_back(std::make_pair(DId, FId));
2930
2931 return SrcId;
2932 }
2933
Evan Cheng3e288912009-02-25 07:04:34 +00002934 void ConstructCompileUnit(GlobalVariable *GV) {
2935 DICompileUnit DIUnit(GV);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00002936 std::string Dir, FN, Prod;
2937 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
2938 DIUnit.getFilename(FN));
Evan Cheng3e288912009-02-25 07:04:34 +00002939
2940 DIE *Die = new DIE(DW_TAG_compile_unit);
2941 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2942 DWLabel("section_line", 0), DWLabel("section_line", 0),
2943 false);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00002944 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer(Prod));
Evan Cheng3e288912009-02-25 07:04:34 +00002945 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendling1c5842b2009-03-09 05:04:40 +00002946 AddString(Die, DW_AT_name, DW_FORM_string, FN);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00002947 if (!Dir.empty())
Bill Wendling1c5842b2009-03-09 05:04:40 +00002948 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Cheng3e288912009-02-25 07:04:34 +00002949 if (DIUnit.isOptimized())
2950 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00002951 std::string Flags;
2952 DIUnit.getFlags(Flags);
2953 if (!Flags.empty())
Evan Cheng3e288912009-02-25 07:04:34 +00002954 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
2955 unsigned RVer = DIUnit.getRunTimeVersion();
2956 if (RVer)
2957 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
2958
2959 CompileUnit *Unit = new CompileUnit(ID, Die);
2960 if (DIUnit.isMain()) {
2961 assert(!MainCU && "Multiple main compile units are found!");
2962 MainCU = Unit;
2963 }
2964 CompileUnitMap[DIUnit.getGV()] = Unit;
2965 CompileUnits.push_back(Unit);
2966 }
2967
Devang Patel289f2362009-01-05 23:11:11 +00002968 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Patelb3907da2009-01-05 23:03:32 +00002969 void ConstructCompileUnits() {
Evan Cheng3e288912009-02-25 07:04:34 +00002970 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.compile_units");
2971 if (!Root)
2972 return;
2973 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2974 "Malformed compile unit descriptor anchor type");
2975 Constant *RootC = cast<Constant>(*Root->use_begin());
2976 assert(RootC->hasNUsesOrMore(1) &&
2977 "Malformed compile unit descriptor anchor type");
2978 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2979 UI != UE; ++UI)
2980 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2981 UUI != UUE; ++UUI) {
2982 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2983 ConstructCompileUnit(GV);
Devang Patel2ae1db52009-01-30 18:20:31 +00002984 }
Evan Cheng3e288912009-02-25 07:04:34 +00002985 }
2986
2987 bool ConstructGlobalVariableDIE(GlobalVariable *GV) {
2988 DIGlobalVariable DI_GV(GV);
2989 CompileUnit *DW_Unit = MainCU;
2990 if (!DW_Unit)
Chris Lattner88ab9742009-05-05 04:55:56 +00002991 DW_Unit = &FindCompileUnit(DI_GV.getCompileUnit());
Evan Cheng3e288912009-02-25 07:04:34 +00002992
2993 // Check for pre-existence.
2994 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
2995 if (Slot)
2996 return false;
2997
2998 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
2999
3000 // Add address.
3001 DIEBlock *Block = new DIEBlock();
3002 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Bill Wendling26a8ab92009-04-10 00:12:49 +00003003 std::string GLN;
Evan Cheng3e288912009-02-25 07:04:34 +00003004 AddObjectLabel(Block, 0, DW_FORM_udata,
Bill Wendling26a8ab92009-04-10 00:12:49 +00003005 Asm->getGlobalLinkName(DI_GV.getGlobal(), GLN));
Evan Cheng3e288912009-02-25 07:04:34 +00003006 AddBlock(VariableDie, DW_AT_location, 0, Block);
3007
3008 // Add to map.
3009 Slot = VariableDie;
3010 // Add to context owner.
3011 DW_Unit->getDie()->AddChild(VariableDie);
3012 // Expose as global. FIXME - need to check external flag.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00003013 std::string Name;
3014 DW_Unit->AddGlobal(DI_GV.getName(Name), VariableDie);
Evan Cheng3e288912009-02-25 07:04:34 +00003015 return true;
Devang Patelb3907da2009-01-05 23:03:32 +00003016 }
3017
Devang Patel289f2362009-01-05 23:11:11 +00003018 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patela9169c32009-02-24 00:02:15 +00003019 /// visible global variables. Return true if at least one global DIE is
3020 /// created.
3021 bool ConstructGlobalVariableDIEs() {
Evan Cheng3e288912009-02-25 07:04:34 +00003022 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
3023 if (!Root)
3024 return false;
Devang Patel289f2362009-01-05 23:11:11 +00003025
Evan Cheng3e288912009-02-25 07:04:34 +00003026 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3027 "Malformed global variable descriptor anchor type");
3028 Constant *RootC = cast<Constant>(*Root->use_begin());
3029 assert(RootC->hasNUsesOrMore(1) &&
3030 "Malformed global variable descriptor anchor type");
Devang Patel289f2362009-01-05 23:11:11 +00003031
Evan Cheng3e288912009-02-25 07:04:34 +00003032 bool Result = false;
3033 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3034 UI != UE; ++UI)
3035 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3036 UUI != UUE; ++UUI) {
3037 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3038 Result |= ConstructGlobalVariableDIE(GV);
3039 }
3040 return Result;
3041 }
Devang Patel289f2362009-01-05 23:11:11 +00003042
Evan Cheng3e288912009-02-25 07:04:34 +00003043 bool ConstructSubprogram(GlobalVariable *GV) {
3044 DISubprogram SP(GV);
3045 CompileUnit *Unit = MainCU;
3046 if (!Unit)
Chris Lattner88ab9742009-05-05 04:55:56 +00003047 Unit = &FindCompileUnit(SP.getCompileUnit());
Devang Patel289f2362009-01-05 23:11:11 +00003048
Evan Cheng3e288912009-02-25 07:04:34 +00003049 // Check for pre-existence.
3050 DIE *&Slot = Unit->getDieMapSlotFor(GV);
3051 if (Slot)
3052 return false;
3053
3054 if (!SP.isDefinition())
3055 // This is a method declaration which will be handled while
3056 // constructing class type.
3057 return false;
3058
3059 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
3060
3061 // Add to map.
3062 Slot = SubprogramDie;
3063 // Add to context owner.
3064 Unit->getDie()->AddChild(SubprogramDie);
3065 // Expose as global.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00003066 std::string Name;
3067 Unit->AddGlobal(SP.getName(Name), SubprogramDie);
Evan Cheng3e288912009-02-25 07:04:34 +00003068 return true;
Devang Patel289f2362009-01-05 23:11:11 +00003069 }
3070
Devang Patele6caf012009-01-05 23:21:35 +00003071 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patela9169c32009-02-24 00:02:15 +00003072 /// subprograms. Return true if at least one subprogram DIE is created.
3073 bool ConstructSubprograms() {
Evan Cheng3e288912009-02-25 07:04:34 +00003074 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
3075 if (!Root)
3076 return false;
Devang Patele6caf012009-01-05 23:21:35 +00003077
Evan Cheng3e288912009-02-25 07:04:34 +00003078 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3079 "Malformed subprogram descriptor anchor type");
3080 Constant *RootC = cast<Constant>(*Root->use_begin());
3081 assert(RootC->hasNUsesOrMore(1) &&
3082 "Malformed subprogram descriptor anchor type");
Devang Patele6caf012009-01-05 23:21:35 +00003083
Evan Cheng3e288912009-02-25 07:04:34 +00003084 bool Result = false;
3085 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3086 UI != UE; ++UI)
3087 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3088 UUI != UUE; ++UUI) {
3089 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3090 Result |= ConstructSubprogram(GV);
3091 }
3092 return Result;
Devang Patele6caf012009-01-05 23:21:35 +00003093 }
3094
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003095public:
3096 //===--------------------------------------------------------------------===//
3097 // Main entry points.
3098 //
Owen Anderson847b99b2008-08-21 00:14:44 +00003099 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendlingd9308a62009-03-10 21:23:25 +00003100 : Dwarf(OS, A, T, "dbg"), MainCU(0),
3101 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
3102 ValuesSet(InitValuesSetSize), Values(), StringPool(), SectionMap(),
3103 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel7b60d552009-04-15 20:41:31 +00003104 FunctionDbgScope(0), DebugTimer(0) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003105 if (TimePassesIsEnabled)
3106 DebugTimer = new Timer("Dwarf Debug Writer",
Bill Wendling148ecc42009-03-10 22:58:53 +00003107 getDwarfTimerGroup());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003108 }
3109 virtual ~DwarfDebug() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003110 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3111 delete Values[j];
Bill Wendlingd9308a62009-03-10 21:23:25 +00003112
3113 delete DebugTimer;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003114 }
3115
Bill Wendling278a3922009-03-10 21:47:45 +00003116 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
3117 /// be emitted.
3118 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
3119
Devang Patel9304b382009-01-06 21:07:30 +00003120 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3121 /// This is inovked by the target AsmPrinter.
Devang Patel91d27b02009-01-12 23:09:42 +00003122 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003123 if (TimePassesIsEnabled)
3124 DebugTimer->startTimer();
3125
Bill Wendling6baa18d2009-02-03 21:38:21 +00003126 // Create all the compile unit DIEs.
3127 ConstructCompileUnits();
Devang Patel91d27b02009-01-12 23:09:42 +00003128
Bill Wendlingd9308a62009-03-10 21:23:25 +00003129 if (CompileUnits.empty()) {
3130 if (TimePassesIsEnabled)
Bill Wendling0be24752009-03-10 22:02:13 +00003131 DebugTimer->stopTimer();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003132
Bill Wendling6baa18d2009-02-03 21:38:21 +00003133 return;
Bill Wendlingd9308a62009-03-10 21:23:25 +00003134 }
Devang Patel91d27b02009-01-12 23:09:42 +00003135
Devang Patela9169c32009-02-24 00:02:15 +00003136 // Create DIEs for each of the externally visible global variables.
3137 bool globalDIEs = ConstructGlobalVariableDIEs();
3138
3139 // Create DIEs for each of the externally visible subprograms.
3140 bool subprogramDIEs = ConstructSubprograms();
3141
3142 // If there is not any debug info available for any global variables
3143 // and any subprograms then there is not any debug info to emit.
Bill Wendlingd9308a62009-03-10 21:23:25 +00003144 if (!globalDIEs && !subprogramDIEs) {
3145 if (TimePassesIsEnabled)
Bill Wendling0be24752009-03-10 22:02:13 +00003146 DebugTimer->stopTimer();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003147
Devang Patela9169c32009-02-24 00:02:15 +00003148 return;
Bill Wendlingd9308a62009-03-10 21:23:25 +00003149 }
Devang Patela9169c32009-02-24 00:02:15 +00003150
Bill Wendling6baa18d2009-02-03 21:38:21 +00003151 MMI = mmi;
3152 shouldEmit = true;
3153 MMI->setDebugInfoAvailability(true);
Devang Patel9304b382009-01-06 21:07:30 +00003154
Bill Wendling6baa18d2009-02-03 21:38:21 +00003155 // Prime section data.
3156 SectionMap.insert(TAI->getTextSection());
Devang Patel9304b382009-01-06 21:07:30 +00003157
Bill Wendling6baa18d2009-02-03 21:38:21 +00003158 // Print out .file directives to specify files for .loc directives. These
3159 // are printed out early so that they precede any .loc directives.
3160 if (TAI->hasDotLocAndDotFile()) {
Evan Cheng3e288912009-02-25 07:04:34 +00003161 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
3162 // Remember source id starts at 1.
Bill Wendlingdf25fd62009-03-10 21:59:25 +00003163 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Evan Cheng3e288912009-02-25 07:04:34 +00003164 sys::Path FullPath(getSourceDirectoryName(Id.first));
3165 bool AppendOk =
3166 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendling6baa18d2009-02-03 21:38:21 +00003167 assert(AppendOk && "Could not append filename to directory!");
3168 AppendOk = false;
3169 Asm->EmitFile(i, FullPath.toString());
3170 Asm->EOL();
Devang Patel9304b382009-01-06 21:07:30 +00003171 }
Bill Wendling6baa18d2009-02-03 21:38:21 +00003172 }
Devang Patel9304b382009-01-06 21:07:30 +00003173
Bill Wendling6baa18d2009-02-03 21:38:21 +00003174 // Emit initial sections
3175 EmitInitial();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003176
3177 if (TimePassesIsEnabled)
3178 DebugTimer->stopTimer();
Devang Patel9304b382009-01-06 21:07:30 +00003179 }
3180
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003181 /// BeginModule - Emit all Dwarf sections that should come prior to the
3182 /// content.
3183 void BeginModule(Module *M) {
3184 this->M = M;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003185 }
3186
3187 /// EndModule - Emit all Dwarf sections that should come after the content.
3188 ///
3189 void EndModule() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003190 if (!ShouldEmitDwarfDebug())
3191 return;
3192
3193 if (TimePassesIsEnabled)
3194 DebugTimer->startTimer();
aslc200b112008-08-16 12:57:46 +00003195
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003196 // Standard sections final addresses.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003197 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003198 EmitLabel("text_end", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00003199 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003200 EmitLabel("data_end", 0);
aslc200b112008-08-16 12:57:46 +00003201
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003202 // End text sections.
3203 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003204 Asm->SwitchToSection(SectionMap[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003205 EmitLabel("section_end", i);
3206 }
3207
3208 // Emit common frame information.
3209 EmitCommonDebugFrame();
3210
3211 // Emit function debug frame information
3212 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3213 E = DebugFrames.end(); I != E; ++I)
3214 EmitFunctionDebugFrame(*I);
3215
3216 // Compute DIE offsets and sizes.
3217 SizeAndOffsets();
aslc200b112008-08-16 12:57:46 +00003218
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003219 // Emit all the DIEs into a debug info section
3220 EmitDebugInfo();
aslc200b112008-08-16 12:57:46 +00003221
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003222 // Corresponding abbreviations into a abbrev section.
3223 EmitAbbreviations();
aslc200b112008-08-16 12:57:46 +00003224
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003225 // Emit source line correspondence into a debug line section.
3226 EmitDebugLines();
aslc200b112008-08-16 12:57:46 +00003227
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003228 // Emit info into a debug pubnames section.
3229 EmitDebugPubNames();
aslc200b112008-08-16 12:57:46 +00003230
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003231 // Emit info into a debug str section.
3232 EmitDebugStr();
aslc200b112008-08-16 12:57:46 +00003233
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003234 // Emit info into a debug loc section.
3235 EmitDebugLoc();
aslc200b112008-08-16 12:57:46 +00003236
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003237 // Emit info into a debug aranges section.
3238 EmitDebugARanges();
aslc200b112008-08-16 12:57:46 +00003239
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003240 // Emit info into a debug ranges section.
3241 EmitDebugRanges();
aslc200b112008-08-16 12:57:46 +00003242
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003243 // Emit info into a debug macinfo section.
3244 EmitDebugMacInfo();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003245
Devang Patel88bf96e2009-04-13 17:02:03 +00003246 // Emit inline info.
3247 EmitDebugInlineInfo();
3248
Bill Wendlingd9308a62009-03-10 21:23:25 +00003249 if (TimePassesIsEnabled)
3250 DebugTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003251 }
3252
aslc200b112008-08-16 12:57:46 +00003253 /// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003254 /// emitted immediately after the function entry point.
3255 void BeginFunction(MachineFunction *MF) {
Bill Wendlingc1d211d2009-03-11 00:03:50 +00003256 this->MF = MF;
3257
Bill Wendling50db0792009-02-20 00:44:43 +00003258 if (!ShouldEmitDwarfDebug()) return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003259
Bill Wendlingd9308a62009-03-10 21:23:25 +00003260 if (TimePassesIsEnabled)
3261 DebugTimer->startTimer();
3262
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003263 // Begin accumulating function debug information.
3264 MMI->BeginFunction(MF);
aslc200b112008-08-16 12:57:46 +00003265
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003266 // Assumes in correct section after the entry point.
3267 EmitLabel("func_begin", ++SubprogramCount);
Evan Chenga53c40a2008-02-01 09:10:45 +00003268
Argiris Kirtzidis8cac4932009-05-04 19:23:45 +00003269 // Emit label for the implicitly defined dbg.stoppoint at the start of
3270 // the function.
asl7a969d82009-05-04 19:10:38 +00003271 DebugLoc FDL = MF->getDefaultDebugLoc();
3272 if (!FDL.isUnknown()) {
3273 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
3274 unsigned LabelID = RecordSourceLine(DLT.Line, DLT.Col,
3275 DICompileUnit(DLT.CompileUnit));
3276 Asm->printLabel(LabelID);
Argiris Kirtzidis5e3ef112009-05-03 23:27:19 +00003277 }
3278
Bill Wendlingd9308a62009-03-10 21:23:25 +00003279 if (TimePassesIsEnabled)
3280 DebugTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003281 }
aslc200b112008-08-16 12:57:46 +00003282
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003283 /// EndFunction - Gather and emit post-function debug information.
3284 ///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003285 void EndFunction(MachineFunction *MF) {
Bill Wendling50db0792009-02-20 00:44:43 +00003286 if (!ShouldEmitDwarfDebug()) return;
aslc200b112008-08-16 12:57:46 +00003287
Bill Wendlingd9308a62009-03-10 21:23:25 +00003288 if (TimePassesIsEnabled)
3289 DebugTimer->startTimer();
3290
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003291 // Define end label for subprogram.
3292 EmitLabel("func_end", SubprogramCount);
aslc200b112008-08-16 12:57:46 +00003293
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003294 // Get function line info.
Devang Patel35a078f2009-01-12 22:54:42 +00003295 if (!Lines.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003296 // Get section line info.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003297 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003298 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patel35a078f2009-01-12 22:54:42 +00003299 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003300 // Append the function info to section info.
3301 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patel35a078f2009-01-12 22:54:42 +00003302 Lines.begin(), Lines.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003303 }
aslc200b112008-08-16 12:57:46 +00003304
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003305 // Construct scopes for subprogram.
Devang Patel7b60d552009-04-15 20:41:31 +00003306 if (FunctionDbgScope)
3307 ConstructFunctionDbgScope(FunctionDbgScope);
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003308 else
3309 // FIXME: This is wrong. We are essentially getting past a problem with
3310 // debug information not being able to handle unreachable blocks that have
3311 // debug information in them. In particular, those unreachable blocks that
3312 // have "region end" info in them. That situation results in the "root
3313 // scope" not being created. If that's the case, then emit a "default"
3314 // scope, i.e., one that encompasses the whole function. This isn't
3315 // desirable. And a better way of handling this (and all of the debugging
3316 // information) needs to be explored.
Devang Patel6ccd57e2009-01-13 00:20:51 +00003317 ConstructDefaultDbgScope(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003318
3319 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3320 MMI->getFrameMoves()));
Devang Patela4162952009-01-12 18:48:36 +00003321
3322 // Clear debug info
Devang Patel7b60d552009-04-15 20:41:31 +00003323 if (FunctionDbgScope) {
3324 delete FunctionDbgScope;
Devang Patela4162952009-01-12 18:48:36 +00003325 DbgScopeMap.clear();
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003326 DbgInlinedScopeMap.clear();
3327 InlinedVariableScopes.clear();
Devang Patel7b60d552009-04-15 20:41:31 +00003328 FunctionDbgScope = NULL;
Devang Patela4162952009-01-12 18:48:36 +00003329 }
Devang Patelcb59fd42009-01-12 19:17:34 +00003330
Bill Wendlingd9308a62009-03-10 21:23:25 +00003331 Lines.clear();
3332
3333 if (TimePassesIsEnabled)
3334 DebugTimer->stopTimer();
3335 }
Devang Patelcb59fd42009-01-12 19:17:34 +00003336
3337 /// RecordSourceLine - Records location information and associates it with a
3338 /// label. Returns a unique label ID used to generate a label and provide
3339 /// correspondence to the source line list.
3340 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003341 if (TimePassesIsEnabled)
3342 DebugTimer->startTimer();
3343
Evan Cheng3e288912009-02-25 07:04:34 +00003344 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendling6baa18d2009-02-03 21:38:21 +00003345 assert(Unit && "Unable to find CompileUnit");
Devang Patelcb59fd42009-01-12 19:17:34 +00003346 unsigned ID = MMI->NextLabelID();
3347 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
Bill Wendlingd9308a62009-03-10 21:23:25 +00003348
3349 if (TimePassesIsEnabled)
3350 DebugTimer->stopTimer();
3351
Devang Patelcb59fd42009-01-12 19:17:34 +00003352 return ID;
3353 }
3354
3355 /// RecordSourceLine - Records location information and associates it with a
3356 /// label. Returns a unique label ID used to generate a label and provide
3357 /// correspondence to the source line list.
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +00003358 unsigned RecordSourceLine(unsigned Line, unsigned Col, DICompileUnit CU) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003359 if (TimePassesIsEnabled)
3360 DebugTimer->startTimer();
3361
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +00003362 std::string Dir, Fn;
3363 unsigned Src = GetOrCreateSourceID(CU.getDirectory(Dir),
3364 CU.getFilename(Fn));
Devang Patelcb59fd42009-01-12 19:17:34 +00003365 unsigned ID = MMI->NextLabelID();
3366 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
Bill Wendlingd9308a62009-03-10 21:23:25 +00003367
3368 if (TimePassesIsEnabled)
3369 DebugTimer->stopTimer();
3370
Devang Patelcb59fd42009-01-12 19:17:34 +00003371 return ID;
3372 }
3373
Bill Wendling278a3922009-03-10 21:47:45 +00003374 /// getRecordSourceLineCount - Return the number of source lines in the debug
3375 /// info.
3376 unsigned getRecordSourceLineCount() const {
Devang Patelcb59fd42009-01-12 19:17:34 +00003377 return Lines.size();
3378 }
3379
Bill Wendling278a3922009-03-10 21:47:45 +00003380 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
3381 /// timed. Look up the source id with the given directory and source file
3382 /// names. If none currently exists, create a new id and insert it in the
3383 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
3384 /// well.
Evan Cheng3e288912009-02-25 07:04:34 +00003385 unsigned getOrCreateSourceID(const std::string &DirName,
3386 const std::string &FileName) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003387 if (TimePassesIsEnabled)
3388 DebugTimer->startTimer();
3389
Bill Wendling278a3922009-03-10 21:47:45 +00003390 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003391
3392 if (TimePassesIsEnabled)
3393 DebugTimer->stopTimer();
3394
Evan Cheng3e288912009-02-25 07:04:34 +00003395 return SrcId;
Devang Patelcb59fd42009-01-12 19:17:34 +00003396 }
3397
3398 /// RecordRegionStart - Indicate the start of a region.
Devang Patelcb59fd42009-01-12 19:17:34 +00003399 unsigned RecordRegionStart(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003400 if (TimePassesIsEnabled)
3401 DebugTimer->startTimer();
3402
Devang Patelcb59fd42009-01-12 19:17:34 +00003403 DbgScope *Scope = getOrCreateScope(V);
3404 unsigned ID = MMI->NextLabelID();
3405 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003406
3407 if (TimePassesIsEnabled)
3408 DebugTimer->stopTimer();
3409
Devang Patelcb59fd42009-01-12 19:17:34 +00003410 return ID;
3411 }
3412
3413 /// RecordRegionEnd - Indicate the end of a region.
Devang Patelcb59fd42009-01-12 19:17:34 +00003414 unsigned RecordRegionEnd(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003415 if (TimePassesIsEnabled)
3416 DebugTimer->startTimer();
3417
Devang Patelcb59fd42009-01-12 19:17:34 +00003418 DbgScope *Scope = getOrCreateScope(V);
3419 unsigned ID = MMI->NextLabelID();
3420 Scope->setEndLabelID(ID);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003421
3422 if (TimePassesIsEnabled)
3423 DebugTimer->stopTimer();
3424
Devang Patelcb59fd42009-01-12 19:17:34 +00003425 return ID;
3426 }
3427
3428 /// RecordVariable - Indicate the declaration of a local variable.
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003429 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
3430 const MachineInstr *MI) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003431 if (TimePassesIsEnabled)
3432 DebugTimer->startTimer();
3433
Devang Patel2560d922009-01-15 18:25:17 +00003434 DIDescriptor Desc(GV);
3435 DbgScope *Scope = NULL;
Bill Wendlingd9308a62009-03-10 21:23:25 +00003436
Devang Patel2560d922009-01-15 18:25:17 +00003437 if (Desc.getTag() == DW_TAG_variable) {
3438 // GV is a global variable.
3439 DIGlobalVariable DG(GV);
3440 Scope = getOrCreateScope(DG.getContext().getGV());
3441 } else {
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003442 DenseMap<const MachineInstr *, DbgScope *>::iterator
3443 SI = InlinedVariableScopes.find(MI);
3444 if (SI != InlinedVariableScopes.end()) {
3445 // or GV is an inlined local variable.
3446 Scope = SI->second;
3447 } else {
Devang Patel2560d922009-01-15 18:25:17 +00003448 // or GV is a local variable.
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003449 DIVariable DV(GV);
3450 Scope = getOrCreateScope(DV.getContext().getGV());
3451 }
Devang Patel2560d922009-01-15 18:25:17 +00003452 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00003453
Bill Wendling6baa18d2009-02-03 21:38:21 +00003454 assert(Scope && "Unable to find variable' scope");
Devang Patel7c8a2772009-01-16 19:28:14 +00003455 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelcb59fd42009-01-12 19:17:34 +00003456 Scope->AddVariable(DV);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003457
3458 if (TimePassesIsEnabled)
3459 DebugTimer->stopTimer();
Devang Patelcb59fd42009-01-12 19:17:34 +00003460 }
Devang Patel88bf96e2009-04-13 17:02:03 +00003461
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003462 //// RecordInlinedFnStart - Indicate the start of inlined subroutine.
3463 void RecordInlinedFnStart(Instruction *FSI, DISubprogram &SP, unsigned LabelID,
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +00003464 DICompileUnit CU, unsigned Line, unsigned Col) {
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003465 if (!TAI->doesDwarfUsesInlineInfoSection())
3466 return;
3467
Bill Wendlingb183d0c2009-05-01 08:40:06 +00003468 if (TimePassesIsEnabled)
3469 DebugTimer->startTimer();
3470
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +00003471 std::string Dir, Fn;
3472 unsigned Src = GetOrCreateSourceID(CU.getDirectory(Dir),
3473 CU.getFilename(Fn));
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003474 DbgScope *Scope = createInlinedSubroutineScope(SP, Src, Line, Col);
3475 Scope->setStartLabelID(LabelID);
Devang Patel88bf96e2009-04-13 17:02:03 +00003476 MMI->RecordUsedDbgLabel(LabelID);
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003477 GlobalVariable *GV = SP.getGV();
3478
3479 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3480 SI = DbgInlinedScopeMap.find(GV);
Bill Wendling0cfbd9b2009-05-01 08:32:14 +00003481
3482 if (SI == DbgInlinedScopeMap.end())
3483 DbgInlinedScopeMap[GV].push_back(Scope);
3484 else
3485 SI->second.push_back(Scope);
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003486
Devang Patel88bf96e2009-04-13 17:02:03 +00003487 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
3488 I = InlineInfo.find(GV);
Bill Wendling86c053c2009-05-01 08:35:12 +00003489 if (I == InlineInfo.end())
3490 InlineInfo[GV].push_back(LabelID);
3491 else
3492 I->second.push_back(LabelID);
Bill Wendlingb183d0c2009-05-01 08:40:06 +00003493
3494 if (TimePassesIsEnabled)
3495 DebugTimer->stopTimer();
Devang Patel88bf96e2009-04-13 17:02:03 +00003496 }
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003497
3498 /// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
3499 unsigned RecordInlinedFnEnd(DISubprogram &SP) {
3500 if (!TAI->doesDwarfUsesInlineInfoSection())
3501 return 0;
3502
Bill Wendlingb183d0c2009-05-01 08:40:06 +00003503 if (TimePassesIsEnabled)
3504 DebugTimer->startTimer();
3505
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003506 GlobalVariable *GV = SP.getGV();
3507 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3508 I = DbgInlinedScopeMap.find(GV);
Bill Wendlingb183d0c2009-05-01 08:40:06 +00003509 if (I == DbgInlinedScopeMap.end()) {
3510 if (TimePassesIsEnabled)
3511 DebugTimer->stopTimer();
3512
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003513 return 0;
Bill Wendlingb183d0c2009-05-01 08:40:06 +00003514 }
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003515
3516 SmallVector<DbgScope *, 2> &Scopes = I->second;
Bill Wendling58ed5d22009-04-29 00:15:41 +00003517 assert(!Scopes.empty() && "We should have at least one debug scope!");
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003518 DbgScope *Scope = Scopes.back(); Scopes.pop_back();
3519 unsigned ID = MMI->NextLabelID();
3520 MMI->RecordUsedDbgLabel(ID);
3521 Scope->setEndLabelID(ID);
Bill Wendlingb183d0c2009-05-01 08:40:06 +00003522
3523 if (TimePassesIsEnabled)
3524 DebugTimer->stopTimer();
3525
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003526 return ID;
3527 }
3528
3529 /// RecordVariableScope - Record scope for the variable declared by
3530 /// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
3531 /// Record scopes for only inlined subroutine variables. Other
3532 /// variables' scopes are determined during RecordVariable().
3533 void RecordVariableScope(DIVariable &DV, const MachineInstr *DeclareMI) {
Bill Wendlingb183d0c2009-05-01 08:40:06 +00003534 if (TimePassesIsEnabled)
3535 DebugTimer->startTimer();
3536
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003537 DISubprogram SP(DV.getContext().getGV());
Bill Wendlingb183d0c2009-05-01 08:40:06 +00003538
3539 if (SP.isNull()) {
3540 if (TimePassesIsEnabled)
3541 DebugTimer->stopTimer();
3542
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003543 return;
Bill Wendlingb183d0c2009-05-01 08:40:06 +00003544 }
3545
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003546 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3547 I = DbgInlinedScopeMap.find(SP.getGV());
Bill Wendlingb183d0c2009-05-01 08:40:06 +00003548 if (I != DbgInlinedScopeMap.end())
3549 InlinedVariableScopes[DeclareMI] = I->second.back();
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003550
Bill Wendlingb183d0c2009-05-01 08:40:06 +00003551 if (TimePassesIsEnabled)
3552 DebugTimer->stopTimer();
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003553 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003554};
3555
3556//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00003557/// DwarfException - Emits Dwarf exception handling directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003558///
3559class DwarfException : public Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003560 struct FunctionEHFrameInfo {
3561 std::string FnName;
3562 unsigned Number;
3563 unsigned PersonalityIndex;
3564 bool hasCalls;
3565 bool hasLandingPads;
3566 std::vector<MachineMove> Moves;
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003567 const Function * function;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003568
3569 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3570 bool hC, bool hL,
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003571 const std::vector<MachineMove> &M,
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003572 const Function *f):
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003573 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003574 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003575 };
3576
3577 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen85535762008-04-02 00:25:04 +00003578
3579 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3580 /// be emitted.
3581 bool shouldEmitTable;
3582
3583 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3584 /// should be emitted.
3585 bool shouldEmitMoves;
3586
3587 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3588 /// should be emitted.
3589 bool shouldEmitTableModule;
3590
aslc200b112008-08-16 12:57:46 +00003591 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen85535762008-04-02 00:25:04 +00003592 /// should be emitted.
3593 bool shouldEmitMovesModule;
Duncan Sands96144f92008-05-07 19:11:09 +00003594
Bill Wendlingd9308a62009-03-10 21:23:25 +00003595 /// ExceptionTimer - Timer for the Dwarf exception writer.
3596 Timer *ExceptionTimer;
3597
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003598 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3599 ///
3600 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
3601 // Size and sign of stack growth.
3602 int stackGrowth =
3603 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3604 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00003605 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003606
3607 // Begin eh frame section.
3608 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling189bde72008-12-24 08:05:17 +00003609
3610 if (!TAI->doesRequireNonLocalEHFrameLabel())
3611 O << TAI->getEHGlobalPrefix();
3612 O << "EH_frame" << Index << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003613 EmitLabel("section_eh_frame", Index);
3614
3615 // Define base labels.
3616 EmitLabel("eh_frame_common", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003617
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003618 // Define the eh frame length.
3619 EmitDifference("eh_frame_common_end", Index,
3620 "eh_frame_common_begin", Index, true);
3621 Asm->EOL("Length of Common Information Entry");
3622
3623 // EH frame header.
3624 EmitLabel("eh_frame_common_begin", Index);
3625 Asm->EmitInt32((int)0);
3626 Asm->EOL("CIE Identifier Tag");
3627 Asm->EmitInt8(DW_CIE_VERSION);
3628 Asm->EOL("CIE Version");
Duncan Sands96144f92008-05-07 19:11:09 +00003629
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003630 // The personality presence indicates that language specific information
3631 // will show up in the eh frame.
3632 Asm->EmitString(Personality ? "zPLR" : "zR");
3633 Asm->EOL("CIE Augmentation");
Duncan Sands96144f92008-05-07 19:11:09 +00003634
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003635 // Round out reader.
3636 Asm->EmitULEB128Bytes(1);
3637 Asm->EOL("CIE Code Alignment Factor");
3638 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands96144f92008-05-07 19:11:09 +00003639 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00003640 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands96144f92008-05-07 19:11:09 +00003641 Asm->EOL("CIE Return Address Column");
3642
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003643 // If there is a personality, we need to indicate the functions location.
3644 if (Personality) {
3645 Asm->EmitULEB128Bytes(7);
3646 Asm->EOL("Augmentation Size");
Bill Wendling2d369922007-09-11 17:20:55 +00003647
Duncan Sands96144f92008-05-07 19:11:09 +00003648 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendling2d369922007-09-11 17:20:55 +00003649 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands96144f92008-05-07 19:11:09 +00003650 Asm->EOL("Personality (pcrel sdata4 indirect)");
3651 } else {
Bill Wendling2d369922007-09-11 17:20:55 +00003652 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands96144f92008-05-07 19:11:09 +00003653 Asm->EOL("Personality (pcrel sdata4)");
3654 }
Bill Wendling2d369922007-09-11 17:20:55 +00003655
Duncan Sands96144f92008-05-07 19:11:09 +00003656 PrintRelDirective(true);
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003657 O << TAI->getPersonalityPrefix();
3658 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3659 O << TAI->getPersonalitySuffix();
Duncan Sands4cc39532008-05-08 12:33:11 +00003660 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3661 O << "-" << TAI->getPCSymbol();
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003662 Asm->EOL("Personality");
Bill Wendling38cb7c92007-08-25 00:51:55 +00003663
Duncan Sands96144f92008-05-07 19:11:09 +00003664 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3665 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003666
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003667 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3668 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003669 } else {
3670 Asm->EmitULEB128Bytes(1);
3671 Asm->EOL("Augmentation Size");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003672
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003673 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3674 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003675 }
3676
3677 // Indicate locations of general callee saved registers in frame.
3678 std::vector<MachineMove> Moves;
3679 RI->getInitialFrameState(Moves);
Dale Johannesenf5a11532007-11-13 19:13:01 +00003680 EmitFrameMoves(NULL, 0, Moves, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003681
Dale Johannesen388f20f2008-04-30 00:43:29 +00003682 // On Darwin the linker honors the alignment of eh_frame, which means it
3683 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3684 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003685 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003686 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003687 EmitLabel("eh_frame_common_end", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003688
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003689 Asm->EOL();
3690 }
Duncan Sands96144f92008-05-07 19:11:09 +00003691
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003692 /// EmitEHFrame - Emit function exception frame information.
3693 ///
3694 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003695 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
Chris Lattner68433442009-04-13 05:44:34 +00003696
3697 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
3698 "Should not emit 'available externally' functions at all");
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003699
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003700 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3701
3702 // Externally visible entry into the functions eh frame info.
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003703 // If the corresponding function is static, this should not be
3704 // externally visible.
Rafael Espindolaa168fc92009-01-15 20:18:42 +00003705 if (linkage != Function::InternalLinkage &&
Devang Patel245446c2009-01-17 08:05:14 +00003706 linkage != Function::PrivateLinkage) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003707 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3708 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3709 }
3710
Dale Johannesenf09b5992008-01-10 02:03:30 +00003711 // If corresponding function is weak definition, this should be too.
Duncan Sands19d161f2009-03-07 15:45:40 +00003712 if ((linkage == Function::WeakAnyLinkage ||
3713 linkage == Function::WeakODRLinkage ||
3714 linkage == Function::LinkOnceAnyLinkage ||
3715 linkage == Function::LinkOnceODRLinkage) &&
Dale Johannesenf09b5992008-01-10 02:03:30 +00003716 TAI->getWeakDefDirective())
3717 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3718
3719 // If there are no calls then you can't unwind. This may mean we can
3720 // omit the EH Frame, but some environments do not handle weak absolute
aslc200b112008-08-16 12:57:46 +00003721 // symbols.
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003722 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesena9b3e482008-04-08 00:10:24 +00003723 // unwind info is to be available for non-EH uses.
Dale Johannesenf09b5992008-01-10 02:03:30 +00003724 if (!EHFrameInfo.hasCalls &&
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003725 !UnwindTablesMandatory &&
Duncan Sands19d161f2009-03-07 15:45:40 +00003726 ((linkage != Function::WeakAnyLinkage &&
3727 linkage != Function::WeakODRLinkage &&
3728 linkage != Function::LinkOnceAnyLinkage &&
3729 linkage != Function::LinkOnceODRLinkage) ||
Dale Johannesenf09b5992008-01-10 02:03:30 +00003730 !TAI->getWeakDefDirective() ||
3731 TAI->getSupportsWeakOmittedEHFrame()))
aslc200b112008-08-16 12:57:46 +00003732 {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003733 O << EHFrameInfo.FnName << " = 0\n";
aslc200b112008-08-16 12:57:46 +00003734 // This name has no connection to the function, so it might get
3735 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003736 // dead-stripping unconditionally.
3737 if (const char *UsedDirective = TAI->getUsedDirective())
3738 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003739 } else {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003740 O << EHFrameInfo.FnName << ":\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003741
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003742 // EH frame header.
3743 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3744 "eh_frame_begin", EHFrameInfo.Number, true);
3745 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00003746
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003747 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
3748
Bill Wendling189bde72008-12-24 08:05:17 +00003749 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3750 PrintRelDirective(true, true);
3751 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3752
3753 if (!TAI->isAbsoluteEHSectionOffsets())
3754 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3755 } else {
3756 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3757 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3758 true, true, false);
3759 }
3760
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003761 Asm->EOL("FDE CIE offset");
3762
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003763 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003764 Asm->EOL("FDE initial location");
3765 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003766 "eh_func_begin", EHFrameInfo.Number, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003767 Asm->EOL("FDE address range");
Duncan Sands96144f92008-05-07 19:11:09 +00003768
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003769 // If there is a personality and landing pads then point to the language
3770 // specific data area in the exception table.
3771 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands96144f92008-05-07 19:11:09 +00003772 Asm->EmitULEB128Bytes(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003773 Asm->EOL("Augmentation size");
Duncan Sands96144f92008-05-07 19:11:09 +00003774
3775 if (EHFrameInfo.hasLandingPads)
3776 EmitReference("exception", EHFrameInfo.Number, true, true);
3777 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003778 Asm->EmitInt32((int)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003779 Asm->EOL("Language Specific Data Area");
3780 } else {
3781 Asm->EmitULEB128Bytes(0);
3782 Asm->EOL("Augmentation size");
3783 }
Duncan Sands96144f92008-05-07 19:11:09 +00003784
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003785 // Indicate locations of function specific callee saved registers in
3786 // frame.
Devang Patelb28de842009-01-17 08:01:33 +00003787 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00003788 true);
aslc200b112008-08-16 12:57:46 +00003789
Dale Johannesen388f20f2008-04-30 00:43:29 +00003790 // On Darwin the linker honors the alignment of eh_frame, which means it
3791 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3792 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003793 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003794 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003795 EmitLabel("eh_frame_end", EHFrameInfo.Number);
aslc200b112008-08-16 12:57:46 +00003796
3797 // If the function is marked used, this table should be also. We cannot
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003798 // make the mark unconditional in this case, since retaining the table
aslc200b112008-08-16 12:57:46 +00003799 // also retains the function in this case, and there is code around
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003800 // that depends on unused functions (calling undefined externals) being
3801 // dead-stripped to link correctly. Yes, there really is.
3802 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3803 if (const char *UsedDirective = TAI->getUsedDirective())
3804 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3805 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003806 }
3807
Duncan Sands241a0c92007-09-05 11:27:52 +00003808 /// EmitExceptionTable - Emit landing pads and actions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003809 ///
3810 /// The general organization of the table is complex, but the basic concepts
3811 /// are easy. First there is a header which describes the location and
3812 /// organization of the three components that follow.
3813 /// 1. The landing pad site information describes the range of code covered
3814 /// by the try. In our case it's an accumulation of the ranges covered
3815 /// by the invokes in the try. There is also a reference to the landing
3816 /// pad that handles the exception once processed. Finally an index into
3817 /// the actions table.
3818 /// 2. The action table, in our case, is composed of pairs of type ids
3819 /// and next action offset. Starting with the action index from the
3820 /// landing pad site, each type Id is checked for a match to the current
3821 /// exception. If it matches then the exception and type id are passed
3822 /// on to the landing pad. Otherwise the next action is looked up. This
3823 /// chain is terminated with a next action of zero. If no type id is
3824 /// found the the frame is unwound and handling continues.
3825 /// 3. Type id table contains references to all the C++ typeinfo for all
3826 /// catches in the function. This tables is reversed indexed base 1.
3827
3828 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3829 static unsigned SharedTypeIds(const LandingPadInfo *L,
3830 const LandingPadInfo *R) {
3831 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3832 unsigned LSize = LIds.size(), RSize = RIds.size();
3833 unsigned MinSize = LSize < RSize ? LSize : RSize;
3834 unsigned Count = 0;
3835
3836 for (; Count != MinSize; ++Count)
3837 if (LIds[Count] != RIds[Count])
3838 return Count;
3839
3840 return Count;
3841 }
3842
3843 /// PadLT - Order landing pads lexicographically by type id.
3844 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
3845 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3846 unsigned LSize = LIds.size(), RSize = RIds.size();
3847 unsigned MinSize = LSize < RSize ? LSize : RSize;
3848
3849 for (unsigned i = 0; i != MinSize; ++i)
3850 if (LIds[i] != RIds[i])
3851 return LIds[i] < RIds[i];
3852
3853 return LSize < RSize;
3854 }
3855
3856 struct KeyInfo {
3857 static inline unsigned getEmptyKey() { return -1U; }
3858 static inline unsigned getTombstoneKey() { return -2U; }
3859 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner92eea072007-09-17 18:34:04 +00003860 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003861 static bool isPod() { return true; }
3862 };
3863
Duncan Sands241a0c92007-09-05 11:27:52 +00003864 /// ActionEntry - Structure describing an entry in the actions table.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003865 struct ActionEntry {
3866 int ValueForTypeID; // The value to write - may not be equal to the type id.
3867 int NextAction;
3868 struct ActionEntry *Previous;
3869 };
3870
Duncan Sands241a0c92007-09-05 11:27:52 +00003871 /// PadRange - Structure holding a try-range and the associated landing pad.
3872 struct PadRange {
3873 // The index of the landing pad.
3874 unsigned PadIndex;
3875 // The index of the begin and end labels in the landing pad's label lists.
3876 unsigned RangeIndex;
3877 };
3878
3879 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3880
3881 /// CallSiteEntry - Structure describing an entry in the call-site table.
3882 struct CallSiteEntry {
Duncan Sands4ff179f2007-12-19 07:36:31 +00003883 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003884 unsigned BeginLabel; // zero indicates the start of the function.
3885 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003886 // The landing pad starts at PadLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003887 unsigned PadLabel; // zero indicates that there is no landing pad.
3888 unsigned Action;
3889 };
3890
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003891 void EmitExceptionTable() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003892 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
3893 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
3894 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3895 if (PadInfos.empty()) return;
3896
3897 // Sort the landing pads in order of their type ids. This is used to fold
3898 // duplicate actions.
3899 SmallVector<const LandingPadInfo *, 64> LandingPads;
3900 LandingPads.reserve(PadInfos.size());
3901 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
3902 LandingPads.push_back(&PadInfos[i]);
3903 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3904
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003905 // Negative type ids index into FilterIds, positive type ids index into
3906 // TypeInfos. The value written for a positive type id is just the type
3907 // id itself. For a negative type id, however, the value written is the
3908 // (negative) byte offset of the corresponding FilterIds entry. The byte
3909 // offset is usually equal to the type id, because the FilterIds entries
3910 // are written using a variable width encoding which outputs one byte per
3911 // entry as long as the value written is not too large, but can differ.
3912 // This kind of complication does not occur for positive type ids because
3913 // type infos are output using a fixed width encoding.
3914 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3915 SmallVector<int, 16> FilterOffsets;
3916 FilterOffsets.reserve(FilterIds.size());
3917 int Offset = -1;
3918 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3919 E = FilterIds.end(); I != E; ++I) {
3920 FilterOffsets.push_back(Offset);
aslc200b112008-08-16 12:57:46 +00003921 Offset -= TargetAsmInfo::getULEB128Size(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003922 }
3923
Duncan Sands241a0c92007-09-05 11:27:52 +00003924 // Compute the actions table and gather the first action index for each
3925 // landing pad site.
3926 SmallVector<ActionEntry, 32> Actions;
3927 SmallVector<unsigned, 64> FirstActions;
3928 FirstActions.reserve(LandingPads.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003929
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003930 int FirstAction = 0;
Duncan Sands241a0c92007-09-05 11:27:52 +00003931 unsigned SizeActions = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003932 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3933 const LandingPadInfo *LP = LandingPads[i];
3934 const std::vector<int> &TypeIds = LP->TypeIds;
3935 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
3936 unsigned SizeSiteActions = 0;
3937
3938 if (NumShared < TypeIds.size()) {
3939 unsigned SizeAction = 0;
3940 ActionEntry *PrevAction = 0;
3941
3942 if (NumShared) {
3943 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3944 assert(Actions.size());
3945 PrevAction = &Actions.back();
aslc200b112008-08-16 12:57:46 +00003946 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3947 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003948 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
aslc200b112008-08-16 12:57:46 +00003949 SizeAction -=
3950 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003951 SizeAction += -PrevAction->NextAction;
3952 PrevAction = PrevAction->Previous;
3953 }
3954 }
3955
3956 // Compute the actions.
3957 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3958 int TypeID = TypeIds[I];
3959 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3960 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
aslc200b112008-08-16 12:57:46 +00003961 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003962
3963 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
aslc200b112008-08-16 12:57:46 +00003964 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003965 SizeSiteActions += SizeAction;
3966
3967 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
3968 Actions.push_back(Action);
3969
3970 PrevAction = &Actions.back();
3971 }
3972
3973 // Record the first action of the landing pad site.
3974 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3975 } // else identical - re-use previous FirstAction
3976
3977 FirstActions.push_back(FirstAction);
3978
3979 // Compute this sites contribution to size.
3980 SizeActions += SizeSiteActions;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003981 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003982
Duncan Sands4ff179f2007-12-19 07:36:31 +00003983 // Compute the call-site table. The entry for an invoke has a try-range
3984 // containing the call, a non-zero landing pad and an appropriate action.
3985 // The entry for an ordinary call has a try-range containing the call and
3986 // zero for the landing pad and the action. Calls marked 'nounwind' have
3987 // no entry and must not be contained in the try-range of any entry - they
3988 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands241a0c92007-09-05 11:27:52 +00003989 SmallVector<CallSiteEntry, 64> CallSites;
3990
3991 RangeMapType PadMap;
Duncan Sands4ff179f2007-12-19 07:36:31 +00003992 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3993 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3994 // try-ranges for them need be deduced.
Duncan Sands241a0c92007-09-05 11:27:52 +00003995 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3996 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003997 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003998 unsigned BeginLabel = LandingPad->BeginLabels[j];
3999 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
4000 PadRange P = { i, j };
4001 PadMap[BeginLabel] = P;
4002 }
4003 }
4004
Duncan Sands4ff179f2007-12-19 07:36:31 +00004005 // The end label of the previous invoke or nounwind try-range.
Duncan Sands241a0c92007-09-05 11:27:52 +00004006 unsigned LastLabel = 0;
Duncan Sands4ff179f2007-12-19 07:36:31 +00004007
4008 // Whether there is a potentially throwing instruction (currently this means
4009 // an ordinary call) between the end of the previous try-range and now.
4010 bool SawPotentiallyThrowing = false;
4011
4012 // Whether the last callsite entry was for an invoke.
4013 bool PreviousIsInvoke = false;
4014
Duncan Sands4ff179f2007-12-19 07:36:31 +00004015 // Visit all instructions in order of address.
Duncan Sands241a0c92007-09-05 11:27:52 +00004016 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
4017 I != E; ++I) {
4018 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
4019 MI != E; ++MI) {
Dan Gohmanfa607c92008-07-01 00:05:16 +00004020 if (!MI->isLabel()) {
Chris Lattner5b930372008-01-07 07:27:27 +00004021 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands241a0c92007-09-05 11:27:52 +00004022 continue;
4023 }
4024
Chris Lattnerda4cff12007-12-30 20:50:28 +00004025 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands241a0c92007-09-05 11:27:52 +00004026 assert(BeginLabel && "Invalid label!");
Duncan Sands89372f62007-09-05 14:12:46 +00004027
Duncan Sands4ff179f2007-12-19 07:36:31 +00004028 // End of the previous try-range?
Duncan Sands89372f62007-09-05 14:12:46 +00004029 if (BeginLabel == LastLabel)
Duncan Sands4ff179f2007-12-19 07:36:31 +00004030 SawPotentiallyThrowing = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00004031
Duncan Sands4ff179f2007-12-19 07:36:31 +00004032 // Beginning of a new try-range?
Duncan Sands241a0c92007-09-05 11:27:52 +00004033 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands241a0c92007-09-05 11:27:52 +00004034 if (L == PadMap.end())
Duncan Sands4ff179f2007-12-19 07:36:31 +00004035 // Nope, it was just some random label.
Duncan Sands241a0c92007-09-05 11:27:52 +00004036 continue;
4037
4038 PadRange P = L->second;
4039 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
4040
4041 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
4042 "Inconsistent landing pad map!");
4043
4044 // If some instruction between the previous try-range and this one may
4045 // throw, create a call-site entry with no landing pad for the region
4046 // between the try-ranges.
Duncan Sands4ff179f2007-12-19 07:36:31 +00004047 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00004048 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
4049 CallSites.push_back(Site);
Duncan Sands4ff179f2007-12-19 07:36:31 +00004050 PreviousIsInvoke = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00004051 }
4052
4053 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands4ff179f2007-12-19 07:36:31 +00004054 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands241a0c92007-09-05 11:27:52 +00004055
Duncan Sands4ff179f2007-12-19 07:36:31 +00004056 if (LandingPad->LandingPadLabel) {
4057 // This try-range is for an invoke.
4058 CallSiteEntry Site = {BeginLabel, LastLabel,
4059 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands241a0c92007-09-05 11:27:52 +00004060
Duncan Sands4ff179f2007-12-19 07:36:31 +00004061 // Try to merge with the previous call-site.
4062 if (PreviousIsInvoke) {
Dan Gohman3d436002008-06-21 22:00:54 +00004063 CallSiteEntry &Prev = CallSites.back();
Duncan Sands4ff179f2007-12-19 07:36:31 +00004064 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
4065 // Extend the range of the previous entry.
4066 Prev.EndLabel = Site.EndLabel;
4067 continue;
4068 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004069 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004070
Duncan Sands4ff179f2007-12-19 07:36:31 +00004071 // Otherwise, create a new call-site.
4072 CallSites.push_back(Site);
4073 PreviousIsInvoke = true;
4074 } else {
4075 // Create a gap.
4076 PreviousIsInvoke = false;
4077 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004078 }
4079 }
4080 // If some instruction between the previous try-range and the end of the
4081 // function may throw, create a call-site entry with no landing pad for the
4082 // region following the try-range.
Duncan Sands4ff179f2007-12-19 07:36:31 +00004083 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00004084 CallSiteEntry Site = {LastLabel, 0, 0, 0};
4085 CallSites.push_back(Site);
4086 }
4087
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004088 // Final tallies.
Duncan Sands96144f92008-05-07 19:11:09 +00004089
4090 // Call sites.
4091 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
4092 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
4093 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
4094 unsigned SizeSites = CallSites.size() * (SiteStartSize +
4095 SiteLengthSize +
4096 LandingPadSize);
Duncan Sands241a0c92007-09-05 11:27:52 +00004097 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
aslc200b112008-08-16 12:57:46 +00004098 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands241a0c92007-09-05 11:27:52 +00004099
Duncan Sands96144f92008-05-07 19:11:09 +00004100 // Type infos.
4101 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
4102 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004103
4104 unsigned TypeOffset = sizeof(int8_t) + // Call site format
aslc200b112008-08-16 12:57:46 +00004105 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004106 SizeSites + SizeActions + SizeTypes;
4107
4108 unsigned TotalSize = sizeof(int8_t) + // LPStart format
4109 sizeof(int8_t) + // TType format
aslc200b112008-08-16 12:57:46 +00004110 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004111 TypeOffset;
4112
4113 unsigned SizeAlign = (4 - TotalSize) & 3;
4114
4115 // Begin the exception table.
4116 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng7e7d1942008-02-29 19:36:59 +00004117 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesen841e4982008-10-08 21:50:21 +00004118 O << "GCC_except_table" << SubprogramCount << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004119 for (unsigned i = 0; i != SizeAlign; ++i) {
4120 Asm->EmitInt8(0);
4121 Asm->EOL("Padding");
4122 }
4123 EmitLabel("exception", SubprogramCount);
4124
4125 // Emit the header.
4126 Asm->EmitInt8(DW_EH_PE_omit);
4127 Asm->EOL("LPStart format (DW_EH_PE_omit)");
4128 Asm->EmitInt8(DW_EH_PE_absptr);
4129 Asm->EOL("TType format (DW_EH_PE_absptr)");
4130 Asm->EmitULEB128Bytes(TypeOffset);
4131 Asm->EOL("TType base offset");
4132 Asm->EmitInt8(DW_EH_PE_udata4);
4133 Asm->EOL("Call site format (DW_EH_PE_udata4)");
4134 Asm->EmitULEB128Bytes(SizeSites);
4135 Asm->EOL("Call-site table length");
4136
Duncan Sands241a0c92007-09-05 11:27:52 +00004137 // Emit the landing pad site information.
4138 for (unsigned i = 0; i < CallSites.size(); ++i) {
4139 CallSiteEntry &S = CallSites[i];
4140 const char *BeginTag;
4141 unsigned BeginNumber;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004142
Duncan Sands241a0c92007-09-05 11:27:52 +00004143 if (!S.BeginLabel) {
4144 BeginTag = "eh_func_begin";
4145 BeginNumber = SubprogramCount;
4146 } else {
4147 BeginTag = "label";
4148 BeginNumber = S.BeginLabel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004149 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004150
Duncan Sands241a0c92007-09-05 11:27:52 +00004151 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00004152 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004153 Asm->EOL("Region start");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004154
Duncan Sands241a0c92007-09-05 11:27:52 +00004155 if (!S.EndLabel) {
Dale Johannesen4670be42008-01-15 23:24:56 +00004156 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands96144f92008-05-07 19:11:09 +00004157 true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004158 } else {
Duncan Sands96144f92008-05-07 19:11:09 +00004159 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004160 }
4161 Asm->EOL("Region length");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004162
Duncan Sands96144f92008-05-07 19:11:09 +00004163 if (!S.PadLabel)
4164 Asm->EmitInt32(0);
4165 else
Duncan Sands241a0c92007-09-05 11:27:52 +00004166 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00004167 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004168 Asm->EOL("Landing pad");
4169
4170 Asm->EmitULEB128Bytes(S.Action);
4171 Asm->EOL("Action");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004172 }
4173
4174 // Emit the actions.
4175 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4176 ActionEntry &Action = Actions[I];
4177
4178 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
4179 Asm->EOL("TypeInfo index");
4180 Asm->EmitSLEB128Bytes(Action.NextAction);
4181 Asm->EOL("Next action");
4182 }
4183
4184 // Emit the type ids.
4185 for (unsigned M = TypeInfos.size(); M; --M) {
4186 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00004187
4188 PrintRelDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004189
Bill Wendling26a8ab92009-04-10 00:12:49 +00004190 if (GV) {
4191 std::string GLN;
4192 O << Asm->getGlobalLinkName(GV, GLN);
4193 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004194 O << "0";
Bill Wendling26a8ab92009-04-10 00:12:49 +00004195 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004196
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004197 Asm->EOL("TypeInfo");
4198 }
4199
4200 // Emit the filter typeids.
4201 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4202 unsigned TypeID = FilterIds[j];
4203 Asm->EmitULEB128Bytes(TypeID);
4204 Asm->EOL("Filter TypeInfo index");
4205 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004206
Evan Cheng7e7d1942008-02-29 19:36:59 +00004207 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004208 }
4209
4210public:
4211 //===--------------------------------------------------------------------===//
4212 // Main entry points.
4213 //
Owen Anderson847b99b2008-08-21 00:14:44 +00004214 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendlingd9308a62009-03-10 21:23:25 +00004215 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
4216 shouldEmitTableModule(false), shouldEmitMovesModule(false),
4217 ExceptionTimer(0) {
4218 if (TimePassesIsEnabled)
4219 ExceptionTimer = new Timer("Dwarf Exception Writer",
Bill Wendling148ecc42009-03-10 22:58:53 +00004220 getDwarfTimerGroup());
Bill Wendlingd9308a62009-03-10 21:23:25 +00004221 }
aslc200b112008-08-16 12:57:46 +00004222
Bill Wendlingd9308a62009-03-10 21:23:25 +00004223 virtual ~DwarfException() {
4224 delete ExceptionTimer;
4225 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004226
4227 /// SetModuleInfo - Set machine module information when it's known that pass
4228 /// manager has created it. Set by the target AsmPrinter.
4229 void SetModuleInfo(MachineModuleInfo *mmi) {
4230 MMI = mmi;
4231 }
4232
4233 /// BeginModule - Emit all exception information that should come prior to the
4234 /// content.
4235 void BeginModule(Module *M) {
4236 this->M = M;
4237 }
4238
4239 /// EndModule - Emit all exception information that should come after the
4240 /// content.
4241 void EndModule() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004242 if (TimePassesIsEnabled)
4243 ExceptionTimer->startTimer();
4244
Dale Johannesen85535762008-04-02 00:25:04 +00004245 if (shouldEmitMovesModule || shouldEmitTableModule) {
4246 const std::vector<Function *> Personalities = MMI->getPersonalities();
Evan Cheng3e288912009-02-25 07:04:34 +00004247 for (unsigned i = 0; i < Personalities.size(); ++i)
Dale Johannesen85535762008-04-02 00:25:04 +00004248 EmitCommonEHFrame(Personalities[i], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004249
Dale Johannesen85535762008-04-02 00:25:04 +00004250 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4251 E = EHFrames.end(); I != E; ++I)
4252 EmitEHFrame(*I);
4253 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00004254
4255 if (TimePassesIsEnabled)
4256 ExceptionTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004257 }
4258
aslc200b112008-08-16 12:57:46 +00004259 /// BeginFunction - Gather pre-function exception information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004260 /// emitted immediately after the function entry point.
4261 void BeginFunction(MachineFunction *MF) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004262 if (TimePassesIsEnabled)
4263 ExceptionTimer->startTimer();
4264
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004265 this->MF = MF;
Dale Johannesen85535762008-04-02 00:25:04 +00004266 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen85535762008-04-02 00:25:04 +00004267
Bill Wendlingd9308a62009-03-10 21:23:25 +00004268 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen85535762008-04-02 00:25:04 +00004269 // Map all labels and get rid of any dead landing pads.
4270 MMI->TidyLandingPads();
Bill Wendlingd9308a62009-03-10 21:23:25 +00004271
Dale Johannesen85535762008-04-02 00:25:04 +00004272 // If any landing pads survive, we need an EH table.
4273 if (MMI->getLandingPads().size())
4274 shouldEmitTable = true;
4275
4276 // See if we need frame move info.
Duncan Sandscbc28b12008-07-04 09:55:48 +00004277 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen85535762008-04-02 00:25:04 +00004278 shouldEmitMoves = true;
4279
4280 if (shouldEmitMoves || shouldEmitTable)
4281 // Assumes in correct section after the entry point.
4282 EmitLabel("eh_func_begin", ++SubprogramCount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004283 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00004284
Dale Johannesen85535762008-04-02 00:25:04 +00004285 shouldEmitTableModule |= shouldEmitTable;
4286 shouldEmitMovesModule |= shouldEmitMoves;
Bill Wendlingd9308a62009-03-10 21:23:25 +00004287
4288 if (TimePassesIsEnabled)
4289 ExceptionTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004290 }
4291
4292 /// EndFunction - Gather and emit post-function exception information.
4293 ///
4294 void EndFunction() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004295 if (TimePassesIsEnabled)
4296 ExceptionTimer->startTimer();
4297
Dale Johannesen85535762008-04-02 00:25:04 +00004298 if (shouldEmitMoves || shouldEmitTable) {
4299 EmitLabel("eh_func_end", SubprogramCount);
4300 EmitExceptionTable();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004301
Dale Johannesen85535762008-04-02 00:25:04 +00004302 // Save EH frame information
Bill Wendling26a8ab92009-04-10 00:12:49 +00004303 std::string Name;
4304 EHFrames.push_back(
4305 FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF, Name),
4306 SubprogramCount,
4307 MMI->getPersonalityIndex(),
4308 MF->getFrameInfo()->hasCalls(),
4309 !MMI->getLandingPads().empty(),
4310 MMI->getFrameMoves(),
4311 MF->getFunction()));
Bill Wendlingd9308a62009-03-10 21:23:25 +00004312 }
4313
4314 if (TimePassesIsEnabled)
4315 ExceptionTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004316 }
4317};
4318
4319} // End of namespace llvm
4320
4321//===----------------------------------------------------------------------===//
4322
4323/// Emit - Print the abbreviation using the specified Dwarf writer.
4324///
4325void DIEAbbrev::Emit(const DwarfDebug &DD) const {
4326 // Emit its Dwarf tag type.
4327 DD.getAsm()->EmitULEB128Bytes(Tag);
4328 DD.getAsm()->EOL(TagString(Tag));
aslc200b112008-08-16 12:57:46 +00004329
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004330 // Emit whether it has children DIEs.
4331 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4332 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
aslc200b112008-08-16 12:57:46 +00004333
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004334 // For each attribute description.
4335 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4336 const DIEAbbrevData &AttrData = Data[i];
aslc200b112008-08-16 12:57:46 +00004337
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004338 // Emit attribute type.
4339 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4340 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
aslc200b112008-08-16 12:57:46 +00004341
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004342 // Emit form type.
4343 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4344 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
4345 }
4346
4347 // Mark end of abbreviation.
4348 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4349 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
4350}
4351
4352#ifndef NDEBUG
4353void DIEAbbrev::print(std::ostream &O) {
4354 O << "Abbreviation @"
4355 << std::hex << (intptr_t)this << std::dec
4356 << " "
4357 << TagString(Tag)
4358 << " "
4359 << ChildrenString(ChildrenFlag)
4360 << "\n";
aslc200b112008-08-16 12:57:46 +00004361
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004362 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4363 O << " "
4364 << AttributeString(Data[i].getAttribute())
4365 << " "
4366 << FormEncodingString(Data[i].getForm())
4367 << "\n";
4368 }
4369}
4370void DIEAbbrev::dump() { print(cerr); }
4371#endif
4372
4373//===----------------------------------------------------------------------===//
4374
4375#ifndef NDEBUG
4376void DIEValue::dump() {
4377 print(cerr);
4378}
4379#endif
4380
4381//===----------------------------------------------------------------------===//
4382
4383/// EmitValue - Emit integer of appropriate size.
4384///
4385void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
4386 switch (Form) {
4387 case DW_FORM_flag: // Fall thru
4388 case DW_FORM_ref1: // Fall thru
4389 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
4390 case DW_FORM_ref2: // Fall thru
4391 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
4392 case DW_FORM_ref4: // Fall thru
4393 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
4394 case DW_FORM_ref8: // Fall thru
4395 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4396 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4397 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
4398 default: assert(0 && "DIE Value form not supported yet"); break;
4399 }
4400}
4401
4402/// SizeOf - Determine size of integer value in bytes.
4403///
4404unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4405 switch (Form) {
4406 case DW_FORM_flag: // Fall thru
4407 case DW_FORM_ref1: // Fall thru
4408 case DW_FORM_data1: return sizeof(int8_t);
4409 case DW_FORM_ref2: // Fall thru
4410 case DW_FORM_data2: return sizeof(int16_t);
4411 case DW_FORM_ref4: // Fall thru
4412 case DW_FORM_data4: return sizeof(int32_t);
4413 case DW_FORM_ref8: // Fall thru
4414 case DW_FORM_data8: return sizeof(int64_t);
aslc200b112008-08-16 12:57:46 +00004415 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4416 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004417 default: assert(0 && "DIE Value form not supported yet"); break;
4418 }
4419 return 0;
4420}
4421
4422//===----------------------------------------------------------------------===//
4423
4424/// EmitValue - Emit string value.
4425///
4426void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Bill Wendling15afa002009-03-10 23:57:09 +00004427 DD.getAsm()->EmitString(Str);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004428}
4429
4430//===----------------------------------------------------------------------===//
4431
4432/// EmitValue - Emit label value.
4433///
4434void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004435 bool IsSmall = Form == DW_FORM_data4;
4436 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004437}
4438
4439/// SizeOf - Determine size of label value in bytes.
4440///
4441unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004442 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004443 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004444}
4445
4446//===----------------------------------------------------------------------===//
4447
4448/// EmitValue - Emit label value.
4449///
4450void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004451 bool IsSmall = Form == DW_FORM_data4;
4452 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004453}
4454
4455/// SizeOf - Determine size of label value in bytes.
4456///
4457unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004458 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004459 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004460}
aslc200b112008-08-16 12:57:46 +00004461
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004462//===----------------------------------------------------------------------===//
4463
4464/// EmitValue - Emit delta value.
4465///
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004466void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4467 bool IsSmall = Form == DW_FORM_data4;
4468 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4469 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4470}
4471
4472/// SizeOf - Determine size of delta value in bytes.
4473///
4474unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4475 if (Form == DW_FORM_data4) return 4;
4476 return DD.getTargetData()->getPointerSize();
4477}
aslc200b112008-08-16 12:57:46 +00004478
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004479//===----------------------------------------------------------------------===//
4480
4481/// EmitValue - Emit delta value.
4482///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004483void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
4484 bool IsSmall = Form == DW_FORM_data4;
4485 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
4486}
4487
4488/// SizeOf - Determine size of delta value in bytes.
4489///
4490unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4491 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004492 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004493}
4494
4495//===----------------------------------------------------------------------===//
4496
4497/// EmitValue - Emit debug information entry offset.
4498///
4499void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
4500 DD.getAsm()->EmitInt32(Entry->getOffset());
4501}
aslc200b112008-08-16 12:57:46 +00004502
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004503//===----------------------------------------------------------------------===//
4504
4505/// ComputeSize - calculate the size of the block.
4506///
4507unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
4508 if (!Size) {
Owen Anderson88dd6232008-06-24 21:44:59 +00004509 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004510
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004511 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4512 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
4513 }
4514 }
4515 return Size;
4516}
4517
4518/// EmitValue - Emit block data.
4519///
4520void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
4521 switch (Form) {
4522 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4523 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4524 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4525 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
4526 default: assert(0 && "Improper form for block"); break;
4527 }
aslc200b112008-08-16 12:57:46 +00004528
Owen Anderson88dd6232008-06-24 21:44:59 +00004529 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004530
4531 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4532 DD.getAsm()->EOL();
4533 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
4534 }
4535}
4536
4537/// SizeOf - Determine size of block data in bytes.
4538///
4539unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4540 switch (Form) {
4541 case DW_FORM_block1: return Size + sizeof(int8_t);
4542 case DW_FORM_block2: return Size + sizeof(int16_t);
4543 case DW_FORM_block4: return Size + sizeof(int32_t);
aslc200b112008-08-16 12:57:46 +00004544 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004545 default: assert(0 && "Improper form for block"); break;
4546 }
4547 return 0;
4548}
4549
4550//===----------------------------------------------------------------------===//
4551/// DIE Implementation
4552
4553DIE::~DIE() {
4554 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4555 delete Children[i];
4556}
aslc200b112008-08-16 12:57:46 +00004557
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004558/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4559///
4560void DIE::AddSiblingOffset() {
4561 DIEInteger *DI = new DIEInteger(0);
4562 Values.insert(Values.begin(), DI);
4563 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
4564}
4565
4566/// Profile - Used to gather unique data for the value folding set.
4567///
4568void DIE::Profile(FoldingSetNodeID &ID) {
4569 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00004570
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004571 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4572 ID.AddPointer(Children[i]);
4573
4574 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4575 ID.AddPointer(Values[j]);
4576}
4577
4578#ifndef NDEBUG
4579void DIE::print(std::ostream &O, unsigned IncIndent) {
4580 static unsigned IndentCount = 0;
4581 IndentCount += IncIndent;
4582 const std::string Indent(IndentCount, ' ');
4583 bool isBlock = Abbrev.getTag() == 0;
aslc200b112008-08-16 12:57:46 +00004584
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004585 if (!isBlock) {
4586 O << Indent
4587 << "Die: "
4588 << "0x" << std::hex << (intptr_t)this << std::dec
4589 << ", Offset: " << Offset
4590 << ", Size: " << Size
aslc200b112008-08-16 12:57:46 +00004591 << "\n";
4592
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004593 O << Indent
4594 << TagString(Abbrev.getTag())
4595 << " "
4596 << ChildrenString(Abbrev.getChildrenFlag());
4597 } else {
4598 O << "Size: " << Size;
4599 }
4600 O << "\n";
4601
Owen Anderson88dd6232008-06-24 21:44:59 +00004602 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004603
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004604 IndentCount += 2;
4605 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4606 O << Indent;
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004607
4608 if (!isBlock)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004609 O << AttributeString(Data[i].getAttribute());
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004610 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004611 O << "Blk[" << i << "]";
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004612
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004613 O << " "
4614 << FormEncodingString(Data[i].getForm())
4615 << " ";
4616 Values[i]->print(O);
4617 O << "\n";
4618 }
4619 IndentCount -= 2;
4620
4621 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4622 Children[j]->print(O, 4);
4623 }
aslc200b112008-08-16 12:57:46 +00004624
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004625 if (!isBlock) O << "\n";
4626 IndentCount -= IncIndent;
4627}
4628
4629void DIE::dump() {
4630 print(cerr);
4631}
4632#endif
4633
4634//===----------------------------------------------------------------------===//
4635/// DwarfWriter Implementation
4636///
4637
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004638DwarfWriter::DwarfWriter()
Bill Wendlingd9308a62009-03-10 21:23:25 +00004639 : ImmutablePass(&ID), DD(0), DE(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004640
4641DwarfWriter::~DwarfWriter() {
4642 delete DE;
4643 delete DD;
4644}
4645
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004646/// BeginModule - Emit all Dwarf sections that should come prior to the
4647/// content.
Devang Patelaa1e8432009-01-08 23:40:34 +00004648void DwarfWriter::BeginModule(Module *M,
4649 MachineModuleInfo *MMI,
4650 raw_ostream &OS, AsmPrinter *A,
4651 const TargetAsmInfo *T) {
4652 DE = new DwarfException(OS, A, T);
4653 DD = new DwarfDebug(OS, A, T);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004654 DE->BeginModule(M);
4655 DD->BeginModule(M);
Devang Patel6ccd57e2009-01-13 00:20:51 +00004656 DD->SetDebugInfo(MMI);
Devang Patelaa1e8432009-01-08 23:40:34 +00004657 DE->SetModuleInfo(MMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004658}
4659
4660/// EndModule - Emit all Dwarf sections that should come after the content.
4661///
4662void DwarfWriter::EndModule() {
4663 DE->EndModule();
4664 DD->EndModule();
4665}
4666
aslc200b112008-08-16 12:57:46 +00004667/// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004668/// emitted immediately after the function entry point.
4669void DwarfWriter::BeginFunction(MachineFunction *MF) {
4670 DE->BeginFunction(MF);
4671 DD->BeginFunction(MF);
4672}
4673
4674/// EndFunction - Gather and emit post-function debug information.
4675///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00004676void DwarfWriter::EndFunction(MachineFunction *MF) {
4677 DD->EndFunction(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004678 DE->EndFunction();
aslc200b112008-08-16 12:57:46 +00004679
Bill Wendling5b4796a2008-07-22 00:53:37 +00004680 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004681 // Clear function debug information.
4682 MMI->EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004683}
Devang Patelcb59fd42009-01-12 19:17:34 +00004684
4685/// RecordSourceLine - Records location information and associates it with a
4686/// label. Returns a unique label ID used to generate a label and provide
4687/// correspondence to the source line list.
4688unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +00004689 DICompileUnit CU) {
4690 return DD->RecordSourceLine(Line, Col, CU);
Devang Patelcb59fd42009-01-12 19:17:34 +00004691}
4692
4693/// RecordRegionStart - Indicate the start of a region.
4694unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004695 return DD->RecordRegionStart(V);
Devang Patelcb59fd42009-01-12 19:17:34 +00004696}
4697
4698/// RecordRegionEnd - Indicate the end of a region.
4699unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004700 return DD->RecordRegionEnd(V);
Devang Patelcb59fd42009-01-12 19:17:34 +00004701}
4702
4703/// getRecordSourceLineCount - Count source lines.
4704unsigned DwarfWriter::getRecordSourceLineCount() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004705 return DD->getRecordSourceLineCount();
Devang Patelcb59fd42009-01-12 19:17:34 +00004706}
Devang Patel70190872009-01-13 21:25:00 +00004707
Devang Patelfe359e72009-01-13 21:44:10 +00004708/// RecordVariable - Indicate the declaration of a local variable.
4709///
Devang Patel8a9a7dc2009-04-15 00:10:26 +00004710void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
4711 const MachineInstr *MI) {
4712 DD->RecordVariable(GV, FrameIndex, MI);
Devang Patelfe359e72009-01-13 21:44:10 +00004713}
Devang Patel42f6bed2009-01-13 23:54:55 +00004714
Bill Wendling50db0792009-02-20 00:44:43 +00004715/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
4716/// be emitted.
4717bool DwarfWriter::ShouldEmitDwarfDebug() const {
Argiris Kirtzidis25657342009-05-03 08:50:41 +00004718 return DD && DD->ShouldEmitDwarfDebug();
Bill Wendling50db0792009-02-20 00:44:43 +00004719}
Devang Patel88bf96e2009-04-13 17:02:03 +00004720
Devang Patel8a9a7dc2009-04-15 00:10:26 +00004721//// RecordInlinedFnStart - Global variable GV is inlined at the location marked
Devang Patel88bf96e2009-04-13 17:02:03 +00004722//// by LabelID label.
Devang Patel8a9a7dc2009-04-15 00:10:26 +00004723void DwarfWriter::RecordInlinedFnStart(Instruction *I, DISubprogram &SP,
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +00004724 unsigned LabelID, DICompileUnit CU,
Devang Patel8a9a7dc2009-04-15 00:10:26 +00004725 unsigned Line, unsigned Col) {
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +00004726 DD->RecordInlinedFnStart(I, SP, LabelID, CU, Line, Col);
Devang Patel88bf96e2009-04-13 17:02:03 +00004727}
4728
Devang Patel8a9a7dc2009-04-15 00:10:26 +00004729/// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
4730unsigned DwarfWriter::RecordInlinedFnEnd(DISubprogram &SP) {
4731 return DD->RecordInlinedFnEnd(SP);
4732}
4733
4734/// RecordVariableScope - Record scope for the variable declared by
4735/// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
4736void DwarfWriter::RecordVariableScope(DIVariable &DV,
4737 const MachineInstr *DeclareMI) {
4738 DD->RecordVariableScope(DV, DeclareMI);
4739}