blob: 1739ec9d2aa4637b2453bf85c9c24965212bae41 [file] [log] [blame]
Jim Laskeye5032892005-12-21 19:48:16 +00001//===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Jim Laskeye5032892005-12-21 19:48:16 +00007//
8//===----------------------------------------------------------------------===//
9//
Jim Laskey072200c2007-01-29 18:51:14 +000010// This file contains support for writing dwarf info into asm files.
Jim Laskeye5032892005-12-21 19:48:16 +000011//
12//===----------------------------------------------------------------------===//
Jim Laskey3ea0e0e2006-01-27 18:32:41 +000013
Jim Laskeyb2efb852006-01-04 22:28:25 +000014#include "llvm/CodeGen/DwarfWriter.h"
Jim Laskey52060a02006-01-24 00:49:18 +000015#include "llvm/Module.h"
Devang Pateld1ca9252009-01-05 23:03:32 +000016#include "llvm/DerivedTypes.h"
Devang Patelcf3a4482009-01-15 23:41:32 +000017#include "llvm/Constants.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000018#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskey44c3b9f2007-01-26 21:22:28 +000019#include "llvm/CodeGen/MachineModuleInfo.h"
Jim Laskey41886992006-04-07 16:34:46 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000021#include "llvm/CodeGen/MachineLocation.h"
Devang Patel08f053f2009-01-05 17:57:47 +000022#include "llvm/Analysis/DebugInfo.h"
Jim Laskey3f09fc22007-02-28 18:38:31 +000023#include "llvm/Support/Debug.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000024#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000025#include "llvm/Support/CommandLine.h"
Jim Laskey65195462006-10-30 13:35:07 +000026#include "llvm/Support/DataTypes.h"
Jim Laskey52060a02006-01-24 00:49:18 +000027#include "llvm/Support/Mangler.h"
Bill Wendling91b8b802009-03-10 20:41:52 +000028#include "llvm/Support/Timer.h"
Owen Andersoncb371882008-08-21 00:14:44 +000029#include "llvm/Support/raw_ostream.h"
Dan Gohman85496362007-09-24 21:32:18 +000030#include "llvm/System/Path.h"
Jim Laskey563321a2006-09-06 18:34:40 +000031#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000032#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000033#include "llvm/Target/TargetData.h"
Jim Laskey1069fbd2006-04-10 23:09:19 +000034#include "llvm/Target/TargetFrameInfo.h"
Duncan Sands53c3a332007-05-16 12:12:23 +000035#include "llvm/Target/TargetInstrInfo.h"
Jim Laskey1b340dc2007-01-29 20:48:32 +000036#include "llvm/Target/TargetMachine.h"
Jim Laskeyc1c47c32007-01-29 23:40:33 +000037#include "llvm/Target/TargetOptions.h"
Evan Chenge3d42322009-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"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000042#include <ostream>
Jim Laskey65195462006-10-30 13:35:07 +000043#include <string>
Jim Laskeyb2efb852006-01-04 22:28:25 +000044using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000045using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000046
Devang Pateleb3fc282009-01-08 23:40:34 +000047static RegisterPass<DwarfWriter>
48X("dwarfwriter", "DWARF Information Writer");
49char DwarfWriter::ID = 0;
50
Bill Wendling68edf5f2009-03-10 22:58:53 +000051static TimerGroup &getDwarfTimerGroup() {
52 static TimerGroup DwarfTimerGroup("Dwarf Exception and Debugging");
53 return DwarfTimerGroup;
Bill Wendling91b8b802009-03-10 20:41:52 +000054}
55
Jim Laskey0d086af2006-02-27 12:43:29 +000056namespace llvm {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000057
Jim Laskey65195462006-10-30 13:35:07 +000058//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +000059
60/// Configuration values for initial hash set sizes (log2).
61///
Bill Wendlingb9dcef22009-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)
Jim Laskeyef42a012006-11-02 20:12:39 +000065
66//===----------------------------------------------------------------------===//
67/// Forward declarations.
68///
69class DIE;
70class DIEValue;
71
72//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000073/// DWLabel - Labels are used to track locations in the assembler file.
Anton Korobeynikovffe31d72008-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 Spencer181b6c92007-08-05 20:06:04 +000076/// unique in that category.
Jim Laskey65195462006-10-30 13:35:07 +000077class DWLabel {
78public:
Jim Laskeyef42a012006-11-02 20:12:39 +000079 /// Tag - Label category tag. Should always be a staticly declared C string.
80 ///
81 const char *Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000082
Jim Laskeyef42a012006-11-02 20:12:39 +000083 /// Number - Value to make label unique.
84 ///
85 unsigned Number;
Jim Laskey65195462006-10-30 13:35:07 +000086
87 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000088
Jim Laskeyef42a012006-11-02 20:12:39 +000089 void Profile(FoldingSetNodeID &ID) const {
Evan Chenge3d42322009-02-25 07:04:34 +000090 ID.AddString(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +000091 ID.AddInteger(Number);
Jim Laskey90c79d72006-03-23 23:02:34 +000092 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000093
Jim Laskeyef42a012006-11-02 20:12:39 +000094#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +000095 void print(std::ostream *O) const {
96 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +000097 }
Jim Laskeyef42a012006-11-02 20:12:39 +000098 void print(std::ostream &O) const {
Jim Laskeybacd3042007-02-21 22:48:45 +000099 O << "." << Tag;
Jim Laskeyef42a012006-11-02 20:12:39 +0000100 if (Number) O << Number;
101 }
102#endif
Jim Laskeybd761842006-02-27 17:27:12 +0000103};
104
105//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000106/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
107/// Dwarf abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +0000108class DIEAbbrevData {
Jim Laskeyef42a012006-11-02 20:12:39 +0000109 /// Attribute - Dwarf attribute code.
110 ///
111 unsigned Attribute;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000112
Jim Laskeyef42a012006-11-02 20:12:39 +0000113 /// Form - Dwarf form code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000114 ///
115 unsigned Form;
Jim Laskey0d086af2006-02-27 12:43:29 +0000116public:
Bill Wendlingdd446322009-03-10 23:57:09 +0000117 DIEAbbrevData(unsigned A, unsigned F) : Attribute(A), Form(F) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000118
Jim Laskeybd761842006-02-27 17:27:12 +0000119 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000120 unsigned getAttribute() const { return Attribute; }
121 unsigned getForm() const { return Form; }
Jim Laskey063e7652006-01-17 17:31:53 +0000122
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000123 /// Profile - Used to gather unique data for the abbreviation folding set.
Jim Laskey0d086af2006-02-27 12:43:29 +0000124 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000125 void Profile(FoldingSetNodeID &ID)const {
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000126 ID.AddInteger(Attribute);
127 ID.AddInteger(Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000128 }
129};
Jim Laskey063e7652006-01-17 17:31:53 +0000130
Jim Laskey0d086af2006-02-27 12:43:29 +0000131//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000132/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
133/// information object.
134class DIEAbbrev : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000135private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000136 /// Tag - Dwarf tag code.
137 ///
138 unsigned Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000139
Jim Laskeyef42a012006-11-02 20:12:39 +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 Anderson873e1b52008-06-24 21:44:59 +0000150 SmallVector<DIEAbbrevData, 8> Data;
Jim Laskey0d086af2006-02-27 12:43:29 +0000151public:
Bill Wendlingdd446322009-03-10 23:57:09 +0000152 DIEAbbrev(unsigned T, unsigned C) : Tag(T), ChildrenFlag(C), Data() {}
153 virtual ~DIEAbbrev() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000154
Jim Laskeybd761842006-02-27 17:27:12 +0000155 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000156 unsigned getTag() const { return Tag; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000157 unsigned getNumber() const { return Number; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000158 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000159 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000160 void setTag(unsigned T) { Tag = T; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000161 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000162 void setNumber(unsigned N) { Number = N; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000163
Jim Laskey0d086af2006-02-27 12:43:29 +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));
Jim Laskey063e7652006-01-17 17:31:53 +0000168 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000169
Jim Laskeyb8509c52006-03-23 18:07:55 +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 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000175
Jim Laskeya9c83fe2006-10-30 15:59:54 +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);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000181
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000182 // For each attribute description.
183 for (unsigned i = 0, N = Data.size(); i < N; ++i)
184 Data[i].Profile(ID);
185 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000186
Jim Laskey0d086af2006-02-27 12:43:29 +0000187 /// Emit - Print the abbreviation using the specified Dwarf writer.
188 ///
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000189 void Emit(const DwarfDebug &DD) const;
190
Jim Laskey0d086af2006-02-27 12:43:29 +0000191#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000192 void print(std::ostream *O) {
193 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000194 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000195 void print(std::ostream &O);
196 void dump();
197#endif
198};
Jim Laskey063e7652006-01-17 17:31:53 +0000199
Jim Laskey0d086af2006-02-27 12:43:29 +0000200//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000201/// 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;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000208
Jim Laskeyef42a012006-11-02 20:12:39 +0000209 /// Offset - Offset in debug info section.
210 ///
211 unsigned Offset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000212
Jim Laskeyef42a012006-11-02 20:12:39 +0000213 /// Size - Size of instance + children.
214 ///
215 unsigned Size;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000216
Jim Laskeyef42a012006-11-02 20:12:39 +0000217 /// Children DIEs.
218 ///
219 std::vector<DIE *> Children;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000220
Jim Laskeyef42a012006-11-02 20:12:39 +0000221 /// Attributes values.
222 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000223 SmallVector<DIEValue*, 32> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000224
Jim Laskeyef42a012006-11-02 20:12:39 +0000225public:
Dan Gohman81975f62007-08-27 14:50:10 +0000226 explicit DIE(unsigned Tag)
Bill Wendlinga9519572009-05-08 20:38:02 +0000227 : Abbrev(Tag, DW_CHILDREN_no), Offset(0), Size(0) {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000228 virtual ~DIE();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000229
Jim Laskeyef42a012006-11-02 20:12:39 +0000230 // Accessors.
231 DIEAbbrev &getAbbrev() { return Abbrev; }
232 unsigned getAbbrevNumber() const {
233 return Abbrev.getNumber();
234 }
Jim Laskey85f419b2006-11-09 16:32:26 +0000235 unsigned getTag() const { return Abbrev.getTag(); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000236 unsigned getOffset() const { return Offset; }
237 unsigned getSize() const { return Size; }
238 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000239 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000240 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
241 void setOffset(unsigned O) { Offset = O; }
242 void setSize(unsigned S) { Size = S; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000243
Jim Laskeyef42a012006-11-02 20:12:39 +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 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000250
Jim Laskeyef42a012006-11-02 20:12:39 +0000251 /// SiblingOffset - Return the offset of the debug information entry's
252 /// sibling.
253 unsigned SiblingOffset() const { return Offset + Size; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000254
Jim Laskeyef42a012006-11-02 20:12:39 +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 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000265
Jim Laskeyef42a012006-11-02 20:12:39 +0000266 /// Detach - Detaches objects connected to it after copying.
267 ///
268 void Detach() {
269 Children.clear();
270 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000271
Jim Laskeyef42a012006-11-02 20:12:39 +0000272 /// Profile - Used to gather unique data for the value folding set.
273 ///
274 void Profile(FoldingSetNodeID &ID) ;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000275
Jim Laskeyef42a012006-11-02 20:12:39 +0000276#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000277 void print(std::ostream *O, unsigned IncIndent = 0) {
278 if (O) print(*O, IncIndent);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000279 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000280 void print(std::ostream &O, unsigned IncIndent = 0);
281 void dump();
282#endif
283};
284
285//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000286/// DIEValue - A debug information entry value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000287///
288class DIEValue : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000289public:
290 enum {
291 isInteger,
292 isString,
293 isLabel,
294 isAsIsLabel,
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000295 isSectionOffset,
Jim Laskey0d086af2006-02-27 12:43:29 +0000296 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000297 isEntry,
298 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000299 };
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000300
Jim Laskeyef42a012006-11-02 20:12:39 +0000301 /// Type - Type of data stored in the value.
302 ///
303 unsigned Type;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000304
Bill Wendlingdd446322009-03-10 23:57:09 +0000305 explicit DIEValue(unsigned T) : Type(T) {}
Jim Laskey0d086af2006-02-27 12:43:29 +0000306 virtual ~DIEValue() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000307
Jim Laskeyf6733882006-11-02 21:48:18 +0000308 // Accessors
Jim Laskeyef42a012006-11-02 20:12:39 +0000309 unsigned getType() const { return Type; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000310
Jim Laskey0d086af2006-02-27 12:43:29 +0000311 // Implement isa/cast/dyncast.
312 static bool classof(const DIEValue *) { return true; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000313
Jim Laskey0d086af2006-02-27 12:43:29 +0000314 /// EmitValue - Emit value via the Dwarf writer.
315 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000316 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000317
Jim Laskey0d086af2006-02-27 12:43:29 +0000318 /// SizeOf - Return the size of a value in bytes.
319 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000320 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000321
Jim Laskeyef42a012006-11-02 20:12:39 +0000322 /// Profile - Used to gather unique data for the value folding set.
323 ///
324 virtual void Profile(FoldingSetNodeID &ID) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000325
Jim Laskeyef42a012006-11-02 20:12:39 +0000326#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000327 void print(std::ostream *O) {
328 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000329 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000330 virtual void print(std::ostream &O) = 0;
331 void dump();
332#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000333};
Jim Laskey063e7652006-01-17 17:31:53 +0000334
Jim Laskey0d086af2006-02-27 12:43:29 +0000335//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000336/// DWInteger - An integer value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000337///
Jim Laskey0d086af2006-02-27 12:43:29 +0000338class DIEInteger : public DIEValue {
339private:
340 uint64_t Integer;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000341
Jim Laskey0d086af2006-02-27 12:43:29 +0000342public:
Dan Gohman81975f62007-08-27 14:50:10 +0000343 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000344
Jim Laskey0d086af2006-02-27 12:43:29 +0000345 // Implement isa/cast/dyncast.
346 static bool classof(const DIEInteger *) { return true; }
347 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000348
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000349 /// BestForm - Choose the best form for integer.
350 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000351 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 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000363
Jim Laskey0d086af2006-02-27 12:43:29 +0000364 /// EmitValue - Emit integer of appropriate size.
365 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000366 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000367
Jim Laskey0d086af2006-02-27 12:43:29 +0000368 /// SizeOf - Determine size of integer value in bytes.
369 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000370 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000371
Jim Laskeyef42a012006-11-02 20:12:39 +0000372 /// Profile - Used to gather unique data for the value folding set.
373 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000374 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000375 ID.AddInteger(isInteger);
Jim Laskeyef42a012006-11-02 20:12:39 +0000376 ID.AddInteger(Integer);
377 }
Jim Laskey5496f012006-11-09 14:52:14 +0000378 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000379
Jim Laskeyef42a012006-11-02 20:12:39 +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
Jim Laskey0d086af2006-02-27 12:43:29 +0000386};
Jim Laskey063e7652006-01-17 17:31:53 +0000387
Jim Laskey0d086af2006-02-27 12:43:29 +0000388//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000389/// DIEString - A string value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000390///
Jim Laskeyef42a012006-11-02 20:12:39 +0000391class DIEString : public DIEValue {
Bill Wendlingdd446322009-03-10 23:57:09 +0000392 const std::string Str;
Jim Laskeyef42a012006-11-02 20:12:39 +0000393public:
Bill Wendlingdd446322009-03-10 23:57:09 +0000394 explicit DIEString(const std::string &S) : DIEValue(isString), Str(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000395
Jim Laskey0d086af2006-02-27 12:43:29 +0000396 // Implement isa/cast/dyncast.
397 static bool classof(const DIEString *) { return true; }
398 static bool classof(const DIEValue *S) { return S->Type == isString; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000399
Jim Laskey0d086af2006-02-27 12:43:29 +0000400 /// EmitValue - Emit string value.
401 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000402 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000403
Jim Laskey0d086af2006-02-27 12:43:29 +0000404 /// SizeOf - Determine size of string value in bytes.
405 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000406 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Bill Wendlingdd446322009-03-10 23:57:09 +0000407 return Str.size() + sizeof(char); // sizeof('\0');
Jim Laskeyef42a012006-11-02 20:12:39 +0000408 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000409
Jim Laskeyef42a012006-11-02 20:12:39 +0000410 /// Profile - Used to gather unique data for the value folding set.
411 ///
Bill Wendlingdd446322009-03-10 23:57:09 +0000412 static void Profile(FoldingSetNodeID &ID, const std::string &Str) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000413 ID.AddInteger(isString);
Bill Wendlingdd446322009-03-10 23:57:09 +0000414 ID.AddString(Str);
Jim Laskeyef42a012006-11-02 20:12:39 +0000415 }
Bill Wendlingdd446322009-03-10 23:57:09 +0000416 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Str); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000417
Jim Laskeyef42a012006-11-02 20:12:39 +0000418#ifndef NDEBUG
419 virtual void print(std::ostream &O) {
Bill Wendlingdd446322009-03-10 23:57:09 +0000420 O << "Str: \"" << Str << "\"";
Jim Laskeyef42a012006-11-02 20:12:39 +0000421 }
422#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000423};
Jim Laskey063e7652006-01-17 17:31:53 +0000424
Jim Laskey0d086af2006-02-27 12:43:29 +0000425//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000426/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000427//
Jim Laskeyef42a012006-11-02 20:12:39 +0000428class DIEDwarfLabel : public DIEValue {
Jim Laskey0d086af2006-02-27 12:43:29 +0000429 const DWLabel Label;
Bill Wendlingdd446322009-03-10 23:57:09 +0000430public:
Dan Gohman81975f62007-08-27 14:50:10 +0000431 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000432
Jim Laskey0d086af2006-02-27 12:43:29 +0000433 // Implement isa/cast/dyncast.
434 static bool classof(const DIEDwarfLabel *) { return true; }
435 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000436
Jim Laskey0d086af2006-02-27 12:43:29 +0000437 /// EmitValue - Emit label value.
438 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000439 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000440
Jim Laskey0d086af2006-02-27 12:43:29 +0000441 /// SizeOf - Determine size of label value in bytes.
442 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000443 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000444
Jim Laskeyef42a012006-11-02 20:12:39 +0000445 /// Profile - Used to gather unique data for the value folding set.
446 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000447 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000448 ID.AddInteger(isLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000449 Label.Profile(ID);
450 }
Jim Laskey5496f012006-11-09 14:52:14 +0000451 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000452
Jim Laskeyef42a012006-11-02 20:12:39 +0000453#ifndef NDEBUG
454 virtual void print(std::ostream &O) {
455 O << "Lbl: ";
456 Label.print(O);
457 }
458#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000459};
Jim Laskey063e7652006-01-17 17:31:53 +0000460
Jim Laskey0d086af2006-02-27 12:43:29 +0000461//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000462/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000463//
Jim Laskeyef42a012006-11-02 20:12:39 +0000464class DIEObjectLabel : public DIEValue {
Jim Laskey0d086af2006-02-27 12:43:29 +0000465 const std::string Label;
Bill Wendlingdd446322009-03-10 23:57:09 +0000466public:
Dan Gohman81975f62007-08-27 14:50:10 +0000467 explicit DIEObjectLabel(const std::string &L)
468 : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000469
Jim Laskey0d086af2006-02-27 12:43:29 +0000470 // Implement isa/cast/dyncast.
471 static bool classof(const DIEObjectLabel *) { return true; }
472 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000473
Jim Laskey0d086af2006-02-27 12:43:29 +0000474 /// EmitValue - Emit label value.
475 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000476 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000477
Jim Laskey0d086af2006-02-27 12:43:29 +0000478 /// SizeOf - Determine size of label value in bytes.
479 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000480 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000481
Jim Laskeyef42a012006-11-02 20:12:39 +0000482 /// Profile - Used to gather unique data for the value folding set.
483 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000484 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000485 ID.AddInteger(isAsIsLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000486 ID.AddString(Label);
487 }
Evan Chenge3d42322009-02-25 07:04:34 +0000488 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label.c_str()); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000489
490#ifndef NDEBUG
491 virtual void print(std::ostream &O) {
492 O << "Obj: " << Label;
493 }
494#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000495};
Jim Laskey063e7652006-01-17 17:31:53 +0000496
Jim Laskey0d086af2006-02-27 12:43:29 +0000497//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000498/// DIESectionOffset - A section offset DIE.
499//
500class DIESectionOffset : public DIEValue {
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000501 const DWLabel Label;
502 const DWLabel Section;
503 bool IsEH : 1;
504 bool UseSet : 1;
Bill Wendlingdd446322009-03-10 23:57:09 +0000505public:
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000506 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
507 bool isEH = false, bool useSet = true)
Bill Wendlingdd446322009-03-10 23:57:09 +0000508 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
509 IsEH(isEH), UseSet(useSet) {}
Argyrios Kyrtzidisf7acf8f2008-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; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000514
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000515 /// EmitValue - Emit section offset.
516 ///
517 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000518
Argyrios Kyrtzidisf7acf8f2008-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;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000522
Argyrios Kyrtzidisf7acf8f2008-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//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000547/// DIEDelta - A simple label difference DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000548///
Jim Laskeyef42a012006-11-02 20:12:39 +0000549class DIEDelta : public DIEValue {
Jim Laskey0d086af2006-02-27 12:43:29 +0000550 const DWLabel LabelHi;
551 const DWLabel LabelLo;
Bill Wendlingdd446322009-03-10 23:57:09 +0000552public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000553 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
Bill Wendlingdd446322009-03-10 23:57:09 +0000554 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000555
Jim Laskey0d086af2006-02-27 12:43:29 +0000556 // Implement isa/cast/dyncast.
557 static bool classof(const DIEDelta *) { return true; }
558 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000559
Jim Laskey0d086af2006-02-27 12:43:29 +0000560 /// EmitValue - Emit delta value.
561 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000562 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000563
Jim Laskey0d086af2006-02-27 12:43:29 +0000564 /// SizeOf - Determine size of delta value in bytes.
565 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000566 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000567
Jim Laskeyef42a012006-11-02 20:12:39 +0000568 /// Profile - Used to gather unique data for the value folding set.
569 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000570 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
571 const DWLabel &LabelLo) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000572 ID.AddInteger(isDelta);
Jim Laskeyef42a012006-11-02 20:12:39 +0000573 LabelHi.Profile(ID);
574 LabelLo.Profile(ID);
575 }
Jim Laskey5496f012006-11-09 14:52:14 +0000576 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000577
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
Jim Laskey0d086af2006-02-27 12:43:29 +0000586};
Jim Laskey063e7652006-01-17 17:31:53 +0000587
Jim Laskey0d086af2006-02-27 12:43:29 +0000588//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000589/// 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 {
Jim Laskey0d086af2006-02-27 12:43:29 +0000593 DIE *Entry;
Bill Wendlingdd446322009-03-10 23:57:09 +0000594public:
Dan Gohman81975f62007-08-27 14:50:10 +0000595 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000596
Bill Wendlinga9519572009-05-08 20:38:02 +0000597 DIE *getEntry() const { return Entry; }
Bill Wendlingdd446322009-03-10 23:57:09 +0000598 void setEntry(DIE *E) { Entry = E; }
599
Jim Laskey0d086af2006-02-27 12:43:29 +0000600 // Implement isa/cast/dyncast.
601 static bool classof(const DIEntry *) { return true; }
602 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000603
Jim Laskeyb8509c52006-03-23 18:07:55 +0000604 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000605 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000606 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000607
Jim Laskeyb8509c52006-03-23 18:07:55 +0000608 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000609 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000610 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000611 return sizeof(int32_t);
612 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000613
Jim Laskeyef42a012006-11-02 20:12:39 +0000614 /// Profile - Used to gather unique data for the value folding set.
615 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000616 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
617 ID.AddInteger(isEntry);
618 ID.AddPointer(Entry);
619 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000620 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000621 ID.AddInteger(isEntry);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000622
Jim Laskeyef42a012006-11-02 20:12:39 +0000623 if (Entry) {
624 ID.AddPointer(Entry);
625 } else {
626 ID.AddPointer(this);
627 }
628 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000629
Jim Laskeyef42a012006-11-02 20:12:39 +0000630#ifndef NDEBUG
631 virtual void print(std::ostream &O) {
632 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
633 }
634#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000635};
636
637//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000638/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000639//
Jim Laskeyef42a012006-11-02 20:12:39 +0000640class DIEBlock : public DIEValue, public DIE {
Bill Wendlingdd446322009-03-10 23:57:09 +0000641 unsigned Size; // Size in bytes excluding size header.
Jim Laskeyef42a012006-11-02 20:12:39 +0000642public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000643 DIEBlock()
Bill Wendlingdd446322009-03-10 23:57:09 +0000644 : DIEValue(isBlock), DIE(0), Size(0) {}
645 virtual ~DIEBlock() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000646
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000647 // Implement isa/cast/dyncast.
648 static bool classof(const DIEBlock *) { return true; }
649 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000650
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000651 /// ComputeSize - calculate the size of the block.
652 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000653 unsigned ComputeSize(DwarfDebug &DD);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000654
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000655 /// BestForm - Choose the best form for data.
656 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000657 unsigned BestForm() const {
658 if ((unsigned char)Size == Size) return DW_FORM_block1;
659 if ((unsigned short)Size == Size) return DW_FORM_block2;
660 if ((unsigned int)Size == Size) return DW_FORM_block4;
661 return DW_FORM_block;
662 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000663
664 /// EmitValue - Emit block data.
665 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000666 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000667
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000668 /// SizeOf - Determine size of block data in bytes.
669 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000670 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000671
Jim Laskeyef42a012006-11-02 20:12:39 +0000672 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000673 ///
Reid Spencer97821312006-11-02 23:56:21 +0000674 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000675 ID.AddInteger(isBlock);
Jim Laskeyef42a012006-11-02 20:12:39 +0000676 DIE::Profile(ID);
677 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000678
Jim Laskeyef42a012006-11-02 20:12:39 +0000679#ifndef NDEBUG
680 virtual void print(std::ostream &O) {
681 O << "Blk: ";
682 DIE::print(O, 5);
683 }
684#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000685};
686
687//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000688/// CompileUnit - This dwarf writer support class manages information associate
689/// with a source file.
690class CompileUnit {
Jim Laskeyef42a012006-11-02 20:12:39 +0000691 /// ID - File identifier for source.
692 ///
693 unsigned ID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000694
Jim Laskeyef42a012006-11-02 20:12:39 +0000695 /// Die - Compile unit debug information entry.
696 ///
697 DIE *Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000698
Devang Patelbbdc8202009-01-13 23:54:55 +0000699 /// GVToDieMap - Tracks the mapping of unit level debug informaton
700 /// variables to debug information entries.
Devang Patelc2997f42009-01-20 00:58:55 +0000701 std::map<GlobalVariable *, DIE *> GVToDieMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000702
Devang Patelbbdc8202009-01-13 23:54:55 +0000703 /// GVToDIEntryMap - Tracks the mapping of unit level debug informaton
Jim Laskeyef42a012006-11-02 20:12:39 +0000704 /// descriptors to debug information entries using a DIEntry proxy.
Devang Patelc2997f42009-01-20 00:58:55 +0000705 std::map<GlobalVariable *, DIEntry *> GVToDIEntryMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000706
707 /// Globals - A map of globally visible named entities for this unit.
708 ///
Bill Wendling972bbac2009-04-09 21:49:15 +0000709 StringMap<DIE*> Globals;
Jim Laskeyef42a012006-11-02 20:12:39 +0000710
711 /// DiesSet - Used to uniquely define dies within the compile unit.
712 ///
713 FoldingSet<DIE> DiesSet;
Jim Laskey0d086af2006-02-27 12:43:29 +0000714public:
Devang Pateld1ca9252009-01-05 23:03:32 +0000715 CompileUnit(unsigned I, DIE *D)
Devang Patelbbdc8202009-01-13 23:54:55 +0000716 : ID(I), Die(D), GVToDieMap(),
Devang Patel7e55b3a2009-01-17 06:51:37 +0000717 GVToDIEntryMap(), Globals(), DiesSet(InitDiesSetSize)
Devang Pateld1ca9252009-01-05 23:03:32 +0000718 {}
719
Jim Laskeyef42a012006-11-02 20:12:39 +0000720 ~CompileUnit() {
721 delete Die;
Jim Laskeyef42a012006-11-02 20:12:39 +0000722 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000723
Jim Laskeybd761842006-02-27 17:27:12 +0000724 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000725 unsigned getID() const { return ID; }
726 DIE* getDie() const { return Die; }
Bill Wendling972bbac2009-04-09 21:49:15 +0000727 StringMap<DIE*> &getGlobals() { return Globals; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000728
729 /// hasContent - Return true if this compile unit has something to write out.
730 ///
731 bool hasContent() const {
732 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000733 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000734
735 /// AddGlobal - Add a new global entity to the compile unit.
736 ///
737 void AddGlobal(const std::string &Name, DIE *Die) {
738 Globals[Name] = Die;
739 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000740
Jim Laskeyef42a012006-11-02 20:12:39 +0000741 /// getDieMapSlotFor - Returns the debug information entry map slot for the
Devang Patelbbdc8202009-01-13 23:54:55 +0000742 /// specified debug variable.
Devang Patel48d190f2009-01-05 21:47:57 +0000743 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
744 return GVToDieMap[GV];
745 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000746
Jim Laskeyef42a012006-11-02 20:12:39 +0000747 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
Devang Patelbbdc8202009-01-13 23:54:55 +0000748 /// specified debug variable.
Devang Patel48d190f2009-01-05 21:47:57 +0000749 DIEntry *&getDIEntrySlotFor(GlobalVariable *GV) {
750 return GVToDIEntryMap[GV];
751 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000752
Jim Laskeyef42a012006-11-02 20:12:39 +0000753 /// AddDie - Adds or interns the DIE to the compile unit.
754 ///
755 DIE *AddDie(DIE &Buffer) {
756 FoldingSetNodeID ID;
757 Buffer.Profile(ID);
758 void *Where;
759 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000760
Jim Laskeyef42a012006-11-02 20:12:39 +0000761 if (!Die) {
762 Die = new DIE(Buffer);
763 DiesSet.InsertNode(Die, Where);
764 this->Die->AddChild(Die);
765 Buffer.Detach();
766 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000767
Jim Laskeyef42a012006-11-02 20:12:39 +0000768 return Die;
769 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000770};
771
Jim Laskey65195462006-10-30 13:35:07 +0000772//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000773/// Dwarf - Emits general Dwarf directives.
Jim Laskeyef42a012006-11-02 20:12:39 +0000774///
Jim Laskey65195462006-10-30 13:35:07 +0000775class Dwarf {
Jim Laskey072200c2007-01-29 18:51:14 +0000776protected:
Jim Laskey65195462006-10-30 13:35:07 +0000777 //===--------------------------------------------------------------------===//
Jim Laskey072200c2007-01-29 18:51:14 +0000778 // Core attributes used by the Dwarf writer.
Jim Laskey65195462006-10-30 13:35:07 +0000779 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000780
Jim Laskey65195462006-10-30 13:35:07 +0000781 //
782 /// O - Stream to .s file.
783 ///
Owen Andersoncb371882008-08-21 00:14:44 +0000784 raw_ostream &O;
Jim Laskey65195462006-10-30 13:35:07 +0000785
786 /// Asm - Target of Dwarf emission.
787 ///
788 AsmPrinter *Asm;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000789
Bill Wendlingaa8f8882008-07-01 23:34:48 +0000790 /// TAI - Target asm information.
Jim Laskey65195462006-10-30 13:35:07 +0000791 const TargetAsmInfo *TAI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000792
Jim Laskey65195462006-10-30 13:35:07 +0000793 /// TD - Target data.
794 const TargetData *TD;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000795
Jim Laskey65195462006-10-30 13:35:07 +0000796 /// RI - Register Information.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000797 const TargetRegisterInfo *RI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000798
Jim Laskey65195462006-10-30 13:35:07 +0000799 /// M - Current module.
800 ///
801 Module *M;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000802
Jim Laskey65195462006-10-30 13:35:07 +0000803 /// MF - Current machine function.
804 ///
805 MachineFunction *MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000806
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000807 /// MMI - Collected machine module information.
Jim Laskey65195462006-10-30 13:35:07 +0000808 ///
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000809 MachineModuleInfo *MMI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000810
Jim Laskey65195462006-10-30 13:35:07 +0000811 /// SubprogramCount - The running count of functions being compiled.
812 ///
813 unsigned SubprogramCount;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000814
Chris Lattner9251f132007-09-24 03:35:37 +0000815 /// Flavor - A unique string indicating what dwarf producer this is, used to
816 /// unique labels.
817 const char * const Flavor;
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000818
819 unsigned SetCounter;
Owen Andersoncb371882008-08-21 00:14:44 +0000820 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattner9251f132007-09-24 03:35:37 +0000821 const char *flavor)
Jim Laskey072200c2007-01-29 18:51:14 +0000822 : O(OS)
823 , Asm(A)
824 , TAI(T)
825 , TD(Asm->TM.getTargetData())
826 , RI(Asm->TM.getRegisterInfo())
827 , M(NULL)
828 , MF(NULL)
829 , MMI(NULL)
Jim Laskey072200c2007-01-29 18:51:14 +0000830 , SubprogramCount(0)
Chris Lattner9251f132007-09-24 03:35:37 +0000831 , Flavor(flavor)
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000832 , SetCounter(1)
Jim Laskey072200c2007-01-29 18:51:14 +0000833 {
834 }
835
836public:
Jim Laskey072200c2007-01-29 18:51:14 +0000837 //===--------------------------------------------------------------------===//
838 // Accessors.
839 //
Bill Wendlinga6f02fb2009-04-10 00:00:25 +0000840 const AsmPrinter *getAsm() const { return Asm; }
Jim Laskeyb82313f2007-02-01 16:31:34 +0000841 MachineModuleInfo *getMMI() const { return MMI; }
Jim Laskey072200c2007-01-29 18:51:14 +0000842 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohman82482942007-09-27 23:12:31 +0000843 const TargetData *getTargetData() const { return TD; }
Jim Laskey072200c2007-01-29 18:51:14 +0000844
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000845 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
846 const {
847 if (isInSection && TAI->getDwarfSectionOffsetDirective())
848 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohman82482942007-09-27 23:12:31 +0000849 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000850 O << TAI->getData32bitsDirective();
851 else
852 O << TAI->getData64bitsDirective();
853 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000854
Jim Laskeyb82313f2007-02-01 16:31:34 +0000855 /// PrintLabelName - Print label name in form used by Dwarf writer.
856 ///
857 void PrintLabelName(DWLabel Label) const {
858 PrintLabelName(Label.Tag, Label.Number);
859 }
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000860 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000861 O << TAI->getPrivateGlobalPrefix() << Tag;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000862 if (Number) O << Number;
863 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000864
Chris Lattner9251f132007-09-24 03:35:37 +0000865 void PrintLabelName(const char *Tag, unsigned Number,
866 const char *Suffix) const {
867 O << TAI->getPrivateGlobalPrefix() << Tag;
868 if (Number) O << Number;
869 O << Suffix;
870 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000871
Jim Laskeyb82313f2007-02-01 16:31:34 +0000872 /// EmitLabel - Emit location label for internal use by Dwarf.
873 ///
874 void EmitLabel(DWLabel Label) const {
875 EmitLabel(Label.Tag, Label.Number);
876 }
877 void EmitLabel(const char *Tag, unsigned Number) const {
878 PrintLabelName(Tag, Number);
879 O << ":\n";
880 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000881
Jim Laskeyb82313f2007-02-01 16:31:34 +0000882 /// EmitReference - Emit a reference to a label.
883 ///
Dan Gohman06ff4e62007-09-28 15:43:33 +0000884 void EmitReference(DWLabel Label, bool IsPCRelative = false,
885 bool Force32Bit = false) const {
886 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000887 }
888 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman06ff4e62007-09-28 15:43:33 +0000889 bool IsPCRelative = false, bool Force32Bit = false) const {
890 PrintRelDirective(Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000891 PrintLabelName(Tag, Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000892
Jim Laskeyb82313f2007-02-01 16:31:34 +0000893 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
894 }
Dan Gohman06ff4e62007-09-28 15:43:33 +0000895 void EmitReference(const std::string &Name, bool IsPCRelative = false,
896 bool Force32Bit = false) const {
897 PrintRelDirective(Force32Bit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000898
Jim Laskeyb82313f2007-02-01 16:31:34 +0000899 O << Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000900
Jim Laskeyb82313f2007-02-01 16:31:34 +0000901 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
902 }
903
904 /// EmitDifference - Emit the difference between two labels. Some
905 /// assemblers do not behave with absolute expressions with data directives,
906 /// so there is an option (needsSet) to use an intermediary set expression.
907 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000908 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000909 EmitDifference(LabelHi.Tag, LabelHi.Number,
910 LabelLo.Tag, LabelLo.Number,
911 IsSmall);
912 }
913 void EmitDifference(const char *TagHi, unsigned NumberHi,
914 const char *TagLo, unsigned NumberLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000915 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000916 if (TAI->needsSet()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000917 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000918 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000919 O << ",";
920 PrintLabelName(TagHi, NumberHi);
921 O << "-";
922 PrintLabelName(TagLo, NumberLo);
923 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000924
925 PrintRelDirective(IsSmall);
Chris Lattner9251f132007-09-24 03:35:37 +0000926 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000927 ++SetCounter;
928 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000929 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000930
Jim Laskeyb82313f2007-02-01 16:31:34 +0000931 PrintLabelName(TagHi, NumberHi);
932 O << "-";
933 PrintLabelName(TagLo, NumberLo);
934 }
935 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000936
937 void EmitSectionOffset(const char* Label, const char* Section,
938 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000939 bool IsSmall = false, bool isEH = false,
940 bool useSet = true) {
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000941 bool printAbsolute = false;
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000942 if (isEH)
943 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
944 else
945 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
946
947 if (TAI->needsSet() && useSet) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000948 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000949 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000950 O << ",";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000951 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000952
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000953 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000954 O << "-";
955 PrintLabelName(Section, SectionNumber);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000956 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000957 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000958
959 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000960
Chris Lattner9251f132007-09-24 03:35:37 +0000961 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000962 ++SetCounter;
963 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000964 PrintRelDirective(IsSmall, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000965
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000966 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000967
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000968 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000969 O << "-";
970 PrintLabelName(Section, SectionNumber);
971 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000972 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000973 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000974
Jim Laskeyb82313f2007-02-01 16:31:34 +0000975 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
976 /// frame.
977 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenb97aec62007-11-13 19:13:01 +0000978 const std::vector<MachineMove> &Moves, bool isEH) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000979 int stackGrowth =
980 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
981 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +0000982 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeybacd3042007-02-21 22:48:45 +0000983 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000984
985 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +0000986 const MachineMove &Move = Moves[i];
Jim Laskeyb82313f2007-02-01 16:31:34 +0000987 unsigned LabelID = Move.getLabelID();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000988
Jim Laskeyb82313f2007-02-01 16:31:34 +0000989 if (LabelID) {
990 LabelID = MMI->MappedLabel(LabelID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000991
Jim Laskeyb82313f2007-02-01 16:31:34 +0000992 // Throw out move if the label is invalid.
993 if (!LabelID) continue;
994 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000995
Jim Laskeyb82313f2007-02-01 16:31:34 +0000996 const MachineLocation &Dst = Move.getDestination();
997 const MachineLocation &Src = Move.getSource();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000998
Jim Laskeyb82313f2007-02-01 16:31:34 +0000999 // Advance row if new location.
1000 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1001 Asm->EmitInt8(DW_CFA_advance_loc4);
1002 Asm->EOL("DW_CFA_advance_loc4");
Jim Laskeybacd3042007-02-21 22:48:45 +00001003 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1004 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001005
Jim Laskeyb82313f2007-02-01 16:31:34 +00001006 BaseLabelID = LabelID;
Jim Laskeybacd3042007-02-21 22:48:45 +00001007 BaseLabel = "label";
Jim Laskeyb82313f2007-02-01 16:31:34 +00001008 IsLocal = true;
1009 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001010
Jim Laskeyb82313f2007-02-01 16:31:34 +00001011 // If advancing cfa.
Dan Gohmand735b802008-10-03 15:45:36 +00001012 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1013 if (!Src.isReg()) {
1014 if (Src.getReg() == MachineLocation::VirtualFP) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001015 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1016 Asm->EOL("DW_CFA_def_cfa_offset");
1017 } else {
1018 Asm->EmitInt8(DW_CFA_def_cfa);
1019 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmand735b802008-10-03 15:45:36 +00001020 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001021 Asm->EOL("Register");
1022 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001023
Jim Laskeyb82313f2007-02-01 16:31:34 +00001024 int Offset = -Src.getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001025
Jim Laskeyb82313f2007-02-01 16:31:34 +00001026 Asm->EmitULEB128Bytes(Offset);
1027 Asm->EOL("Offset");
1028 } else {
1029 assert(0 && "Machine move no supported yet.");
1030 }
Dan Gohmand735b802008-10-03 15:45:36 +00001031 } else if (Src.isReg() &&
1032 Src.getReg() == MachineLocation::VirtualFP) {
1033 if (Dst.isReg()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001034 Asm->EmitInt8(DW_CFA_def_cfa_register);
1035 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmand735b802008-10-03 15:45:36 +00001036 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001037 Asm->EOL("Register");
1038 } else {
1039 assert(0 && "Machine move no supported yet.");
1040 }
1041 } else {
Dan Gohmand735b802008-10-03 15:45:36 +00001042 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001043 int Offset = Dst.getOffset() / stackGrowth;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001044
Jim Laskeyb82313f2007-02-01 16:31:34 +00001045 if (Offset < 0) {
1046 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1047 Asm->EOL("DW_CFA_offset_extended_sf");
1048 Asm->EmitULEB128Bytes(Reg);
1049 Asm->EOL("Reg");
1050 Asm->EmitSLEB128Bytes(Offset);
1051 Asm->EOL("Offset");
1052 } else if (Reg < 64) {
1053 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Cheng42bf74b2009-03-25 01:47:28 +00001054 if (Asm->isVerbose())
Evan Chengd4114192008-07-09 21:53:02 +00001055 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1056 else
1057 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00001058 Asm->EmitULEB128Bytes(Offset);
1059 Asm->EOL("Offset");
1060 } else {
1061 Asm->EmitInt8(DW_CFA_offset_extended);
1062 Asm->EOL("DW_CFA_offset_extended");
1063 Asm->EmitULEB128Bytes(Reg);
1064 Asm->EOL("Reg");
1065 Asm->EmitULEB128Bytes(Offset);
1066 Asm->EOL("Offset");
1067 }
1068 }
1069 }
1070 }
1071
Jim Laskey072200c2007-01-29 18:51:14 +00001072};
1073
1074//===----------------------------------------------------------------------===//
Devang Patelf3ee5142009-01-12 22:54:42 +00001075/// SrcLineInfo - This class is used to record source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001076///
1077class SrcLineInfo {
1078 unsigned Line; // Source line number.
1079 unsigned Column; // Source column.
1080 unsigned SourceID; // Source ID number.
1081 unsigned LabelID; // Label in code ID number.
1082public:
1083 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001084 : Line(L), Column(C), SourceID(S), LabelID(I) {}
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00001085
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001086 // Accessors
1087 unsigned getLine() const { return Line; }
1088 unsigned getColumn() const { return Column; }
1089 unsigned getSourceID() const { return SourceID; }
1090 unsigned getLabelID() const { return LabelID; }
1091};
1092
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001093//===----------------------------------------------------------------------===//
Devang Patel7a6e5a32009-01-08 02:33:41 +00001094/// DbgVariable - This class is used to track local variable information.
1095///
1096class DbgVariable {
Devang Patel99ec3532009-01-16 19:28:14 +00001097 DIVariable Var; // Variable Descriptor.
Dan Gohman9a38e3e2009-05-07 19:46:24 +00001098 unsigned FrameIndex; // Variable frame index.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001099public:
Devang Patel99ec3532009-01-16 19:28:14 +00001100 DbgVariable(DIVariable V, unsigned I) : Var(V), FrameIndex(I) {}
Devang Patel7a6e5a32009-01-08 02:33:41 +00001101
1102 // Accessors.
Devang Patel99ec3532009-01-16 19:28:14 +00001103 DIVariable getVariable() const { return Var; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001104 unsigned getFrameIndex() const { return FrameIndex; }
1105};
1106
1107//===----------------------------------------------------------------------===//
1108/// DbgScope - This class is used to track scope information.
1109///
1110class DbgScope {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001111 DbgScope *Parent; // Parent to this scope.
Devang Patel7103c6a2009-01-16 18:01:58 +00001112 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001113 // Either subprogram or block.
1114 unsigned StartLabelID; // Label ID of the beginning of scope.
1115 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel7103c6a2009-01-16 18:01:58 +00001116 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
Devang Patel9795da52009-01-10 02:42:49 +00001117 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001118public:
Devang Patel0e5200f2009-01-15 18:25:17 +00001119 DbgScope(DbgScope *P, DIDescriptor D)
Bill Wendlinga9519572009-05-08 20:38:02 +00001120 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0)
Devang Patel7a6e5a32009-01-08 02:33:41 +00001121 {}
Devang Patel1be3ecc2009-04-15 00:10:26 +00001122 virtual ~DbgScope() {
Devang Patel481ff5b2009-01-12 18:48:36 +00001123 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1124 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1125 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001126
1127 // Accessors.
Devang Patel7103c6a2009-01-16 18:01:58 +00001128 DbgScope *getParent() const { return Parent; }
1129 DIDescriptor getDesc() const { return Desc; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001130 unsigned getStartLabelID() const { return StartLabelID; }
1131 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel9795da52009-01-10 02:42:49 +00001132 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1133 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001134 void setStartLabelID(unsigned S) { StartLabelID = S; }
1135 void setEndLabelID(unsigned E) { EndLabelID = E; }
1136
1137 /// AddScope - Add a scope to the scope.
1138 ///
1139 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1140
1141 /// AddVariable - Add a variable to the scope.
1142 ///
1143 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
Devang Patel1be3ecc2009-04-15 00:10:26 +00001144
1145 virtual bool isInlinedSubroutine() { return false; }
Devang Patel3e3702d2009-04-21 00:08:56 +00001146 virtual unsigned getLine() { assert ( 0 && "Unexpected scope!"); return 0; }
1147 virtual unsigned getColumn() { assert ( 0 && "Unexpected scope!"); return 0; }
1148 virtual unsigned getFile() { assert ( 0 && "Unexpected scope!"); return 0; }
Bill Wendling3f500d92009-05-06 21:21:34 +00001149
1150#ifndef NDEBUG
1151 void dump() const;
1152#endif
Devang Patel1be3ecc2009-04-15 00:10:26 +00001153};
1154
Bill Wendling3f500d92009-05-06 21:21:34 +00001155#ifndef NDEBUG
1156void DbgScope::dump() const {
1157 static unsigned IndentLevel = 0;
1158 std::string Indent(IndentLevel, ' ');
1159
1160 cerr << Indent; Desc.dump();
1161 cerr << " [" << StartLabelID << ", " << EndLabelID << "]\n";
1162
1163 IndentLevel += 2;
1164
1165 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
1166 if (Scopes[i] != this)
1167 Scopes[i]->dump();
1168
1169 IndentLevel -= 2;
1170}
1171#endif
Devang Patel1be3ecc2009-04-15 00:10:26 +00001172
1173//===----------------------------------------------------------------------===//
1174/// DbgInlinedSubroutineScope - This class is used to track inlined subroutine
1175/// scope information.
1176///
1177class DbgInlinedSubroutineScope : public DbgScope {
1178 unsigned Src;
1179 unsigned Line;
1180 unsigned Col;
1181public:
1182 DbgInlinedSubroutineScope(DbgScope *P, DIDescriptor D,
1183 unsigned S, unsigned L, unsigned C)
1184 : DbgScope(P, D), Src(S), Line(L), Col(C)
1185 {}
1186
1187 unsigned getLine() { return Line; }
1188 unsigned getColumn() { return Col; }
1189 unsigned getFile() { return Src; }
1190 bool isInlinedSubroutine() { return true; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001191};
1192
1193//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001194/// DwarfDebug - Emits Dwarf debug directives.
Jim Laskey072200c2007-01-29 18:51:14 +00001195///
1196class DwarfDebug : public Dwarf {
Jim Laskey65195462006-10-30 13:35:07 +00001197 //===--------------------------------------------------------------------===//
1198 // Attributes used to construct specific Dwarf sections.
1199 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001200
Evan Chenge3d42322009-02-25 07:04:34 +00001201 /// CompileUnitMap - A map of global variables representing compile units to
1202 /// compile units.
1203 DenseMap<Value *, CompileUnit *> CompileUnitMap;
1204
1205 /// CompileUnits - All the compile units in this module.
1206 ///
1207 SmallVector<CompileUnit *, 8> CompileUnits;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001208
Devang Pateldd9db662009-01-30 18:20:31 +00001209 /// MainCU - Some platform prefers one compile unit per .o file. In such
1210 /// cases, all dies are inserted in MainCU.
1211 CompileUnit *MainCU;
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001212
Jim Laskeyef42a012006-11-02 20:12:39 +00001213 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +00001214 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001215 FoldingSet<DIEAbbrev> AbbreviationsSet;
1216
1217 /// Abbreviations - A list of all the unique abbreviations in use.
1218 ///
1219 std::vector<DIEAbbrev *> Abbreviations;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001220
Evan Chenge3d42322009-02-25 07:04:34 +00001221 /// DirectoryIdMap - Directory name to directory id map.
1222 ///
1223 StringMap<unsigned> DirectoryIdMap;
Devang Patel8526cc02009-01-05 22:35:52 +00001224
Evan Chenge3d42322009-02-25 07:04:34 +00001225 /// DirectoryNames - A list of directory names.
1226 SmallVector<std::string, 8> DirectoryNames;
1227
1228 /// SourceFileIdMap - Source file name to source file id map.
1229 ///
1230 StringMap<unsigned> SourceFileIdMap;
1231
1232 /// SourceFileNames - A list of source file names.
1233 SmallVector<std::string, 8> SourceFileNames;
1234
1235 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
1236 /// id mapped to a unique id.
1237 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
1238
1239 /// SourceIds - Reverse map from source id to directory id + file id pair.
1240 ///
1241 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
Devang Patel8526cc02009-01-05 22:35:52 +00001242
Devang Patel07354e52009-01-16 21:07:53 +00001243 /// Lines - List of of source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001244 std::vector<SrcLineInfo> Lines;
1245
Devang Patel07354e52009-01-16 21:07:53 +00001246 /// ValuesSet - Used to uniquely define values.
1247 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001248 FoldingSet<DIEValue> ValuesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001249
Jim Laskeyef42a012006-11-02 20:12:39 +00001250 /// Values - A list of all the unique values in use.
1251 ///
1252 std::vector<DIEValue *> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001253
Jim Laskey65195462006-10-30 13:35:07 +00001254 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +00001255 ///
Jim Laskey65195462006-10-30 13:35:07 +00001256 UniqueVector<std::string> StringPool;
1257
Jim Laskey65195462006-10-30 13:35:07 +00001258 /// SectionMap - Provides a unique id per text section.
1259 ///
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001260 UniqueVector<const Section*> SectionMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001261
Jim Laskey65195462006-10-30 13:35:07 +00001262 /// SectionSourceLines - Tracks line numbers per text section.
1263 ///
Devang Patelf3ee5142009-01-12 22:54:42 +00001264 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Jim Laskey65195462006-10-30 13:35:07 +00001265
Jim Laskeybacd3042007-02-21 22:48:45 +00001266 /// didInitial - Flag to indicate if initial emission has been done.
1267 ///
1268 bool didInitial;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001269
Jim Laskeybacd3042007-02-21 22:48:45 +00001270 /// shouldEmit - Flag to indicate if debug information should be emitted.
1271 ///
1272 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +00001273
Devang Patel7d2f9722009-04-15 20:41:31 +00001274 // FunctionDbgScope - Top level scope for the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001275 //
Devang Patel7d2f9722009-04-15 20:41:31 +00001276 DbgScope *FunctionDbgScope;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001277
Bill Wendling163ba3f2009-03-10 21:23:25 +00001278 /// DbgScopeMap - Tracks the scopes in the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001279 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
Bill Wendling163ba3f2009-03-10 21:23:25 +00001280
Devang Patel1be3ecc2009-04-15 00:10:26 +00001281 /// DbgInlinedScopeMap - Tracks inlined scopes in the current function.
1282 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> > DbgInlinedScopeMap;
1283
Dan Gohman9a38e3e2009-05-07 19:46:24 +00001284 /// InlineInfo - Keep track of inlined functions and their location.
1285 /// This information is used to populate debug_inlined section.
Devang Patel0f7fef32009-04-13 17:02:03 +00001286 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> > InlineInfo;
1287
Devang Patel1be3ecc2009-04-15 00:10:26 +00001288 /// InlinedVariableScopes - Scopes information for the inlined subroutine
1289 /// variables.
1290 DenseMap<const MachineInstr *, DbgScope *> InlinedVariableScopes;
1291
Bill Wendling163ba3f2009-03-10 21:23:25 +00001292 /// DebugTimer - Timer for the Dwarf debug writer.
1293 Timer *DebugTimer;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001294
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001295 struct FunctionDebugFrameInfo {
1296 unsigned Number;
1297 std::vector<MachineMove> Moves;
1298
1299 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001300 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001301 };
1302
1303 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001304
Bill Wendling163ba3f2009-03-10 21:23:25 +00001305private:
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001306 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
Bill Wendlingc8615e42009-03-10 21:47:45 +00001307 /// maps to the source id. Source id starts at 1.
1308 std::pair<unsigned, unsigned>
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001309 getSourceDirectoryAndFileIds(unsigned SId) const {
Bill Wendlingc8615e42009-03-10 21:47:45 +00001310 return SourceIds[SId-1];
1311 }
1312
1313 /// getNumSourceDirectories - Return the number of source directories in the
1314 /// debug info.
1315 unsigned getNumSourceDirectories() const {
1316 return DirectoryNames.size();
1317 }
1318
1319 /// getSourceDirectoryName - Return the name of the directory corresponding
1320 /// to the id.
1321 const std::string &getSourceDirectoryName(unsigned Id) const {
1322 return DirectoryNames[Id - 1];
1323 }
1324
1325 /// getSourceFileName - Return the name of the source file corresponding
1326 /// to the id.
1327 const std::string &getSourceFileName(unsigned Id) const {
1328 return SourceFileNames[Id - 1];
1329 }
1330
1331 /// getNumSourceIds - Return the number of unique source ids.
Bill Wendlingc8615e42009-03-10 21:47:45 +00001332 unsigned getNumSourceIds() const {
1333 return SourceIds.size();
1334 }
1335
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001336 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001337 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001338 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1339 // Profile the node so that we can make it unique.
1340 FoldingSetNodeID ID;
1341 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001342
Jim Laskeyef42a012006-11-02 20:12:39 +00001343 // Check the set for priors.
1344 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001345
Jim Laskeyef42a012006-11-02 20:12:39 +00001346 // If it's newly added.
1347 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001348 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +00001349 Abbreviations.push_back(&Abbrev);
1350 // Assign the vector position + 1 as its number.
1351 Abbrev.setNumber(Abbreviations.size());
1352 } else {
1353 // Assign existing abbreviation number.
1354 Abbrev.setNumber(InSet->getNumber());
1355 }
1356 }
1357
Jim Laskey65195462006-10-30 13:35:07 +00001358 /// NewString - Add a string to the constant pool and returns a label.
1359 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001360 DWLabel NewString(const std::string &String) {
1361 unsigned StringID = StringPool.insert(String);
1362 return DWLabel("string", StringID);
1363 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001364
Jim Laskeyef42a012006-11-02 20:12:39 +00001365 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1366 /// entry.
1367 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1368 DIEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001369
Jim Laskeyef42a012006-11-02 20:12:39 +00001370 if (Entry) {
1371 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001372 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001373 void *Where;
1374 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001375
Jim Laskeyf6733882006-11-02 21:48:18 +00001376 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001377
Jim Laskeyef42a012006-11-02 20:12:39 +00001378 Value = new DIEntry(Entry);
1379 ValuesSet.InsertNode(Value, Where);
1380 } else {
1381 Value = new DIEntry(Entry);
1382 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001383
Jim Laskeyef42a012006-11-02 20:12:39 +00001384 Values.push_back(Value);
1385 return Value;
1386 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001387
Jim Laskeyef42a012006-11-02 20:12:39 +00001388 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1389 ///
1390 void SetDIEntry(DIEntry *Value, DIE *Entry) {
Bill Wendlingdd446322009-03-10 23:57:09 +00001391 Value->setEntry(Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001392 // Add to values set if not already there. If it is, we merely have a
1393 // duplicate in the values list (no harm.)
1394 ValuesSet.GetOrInsertNode(Value);
1395 }
1396
1397 /// AddUInt - Add an unsigned integer attribute data and value.
1398 ///
1399 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1400 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1401
1402 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001403 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001404 void *Where;
1405 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1406 if (!Value) {
1407 Value = new DIEInteger(Integer);
1408 ValuesSet.InsertNode(Value, Where);
1409 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001410 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001411
Jim Laskeyef42a012006-11-02 20:12:39 +00001412 Die->AddValue(Attribute, Form, Value);
1413 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001414
Jim Laskeyef42a012006-11-02 20:12:39 +00001415 /// AddSInt - Add an signed integer attribute data and value.
1416 ///
1417 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1418 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1419
1420 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001421 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001422 void *Where;
1423 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1424 if (!Value) {
1425 Value = new DIEInteger(Integer);
1426 ValuesSet.InsertNode(Value, Where);
1427 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001428 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001429
Jim Laskeyef42a012006-11-02 20:12:39 +00001430 Die->AddValue(Attribute, Form, Value);
1431 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001432
Evan Chenge3d42322009-02-25 07:04:34 +00001433 /// AddString - Add a string attribute data and value.
Jim Laskeyef42a012006-11-02 20:12:39 +00001434 ///
1435 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1436 const std::string &String) {
1437 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001438 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001439 void *Where;
1440 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1441 if (!Value) {
1442 Value = new DIEString(String);
1443 ValuesSet.InsertNode(Value, Where);
1444 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001445 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001446
Jim Laskeyef42a012006-11-02 20:12:39 +00001447 Die->AddValue(Attribute, Form, Value);
1448 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001449
Jim Laskeyef42a012006-11-02 20:12:39 +00001450 /// AddLabel - Add a Dwarf label attribute data and value.
1451 ///
1452 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1453 const DWLabel &Label) {
1454 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001455 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001456 void *Where;
1457 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1458 if (!Value) {
1459 Value = new DIEDwarfLabel(Label);
1460 ValuesSet.InsertNode(Value, Where);
1461 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001462 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001463
Jim Laskeyef42a012006-11-02 20:12:39 +00001464 Die->AddValue(Attribute, Form, Value);
1465 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001466
Jim Laskeyef42a012006-11-02 20:12:39 +00001467 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1468 ///
1469 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1470 const std::string &Label) {
1471 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001472 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001473 void *Where;
1474 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1475 if (!Value) {
1476 Value = new DIEObjectLabel(Label);
1477 ValuesSet.InsertNode(Value, Where);
1478 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001479 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001480
Jim Laskeyef42a012006-11-02 20:12:39 +00001481 Die->AddValue(Attribute, Form, Value);
1482 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001483
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001484 /// AddSectionOffset - Add a section offset label attribute data and value.
1485 ///
1486 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1487 const DWLabel &Label, const DWLabel &Section,
1488 bool isEH = false, bool useSet = true) {
1489 FoldingSetNodeID ID;
1490 DIESectionOffset::Profile(ID, Label, Section);
1491 void *Where;
1492 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1493 if (!Value) {
1494 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1495 ValuesSet.InsertNode(Value, Where);
1496 Values.push_back(Value);
1497 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001498
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001499 Die->AddValue(Attribute, Form, Value);
1500 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001501
Jim Laskeyef42a012006-11-02 20:12:39 +00001502 /// AddDelta - Add a label delta attribute data and value.
1503 ///
1504 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
Devang Patel1be3ecc2009-04-15 00:10:26 +00001505 const DWLabel &Hi, const DWLabel &Lo) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001506 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001507 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001508 void *Where;
1509 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1510 if (!Value) {
1511 Value = new DIEDelta(Hi, Lo);
1512 ValuesSet.InsertNode(Value, Where);
1513 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001514 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001515
Jim Laskeyef42a012006-11-02 20:12:39 +00001516 Die->AddValue(Attribute, Form, Value);
1517 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001518
Jim Laskeyef42a012006-11-02 20:12:39 +00001519 /// AddDIEntry - Add a DIE attribute data and value.
1520 ///
1521 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1522 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1523 }
1524
1525 /// AddBlock - Add block data.
1526 ///
1527 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1528 Block->ComputeSize(*this);
1529 FoldingSetNodeID ID;
1530 Block->Profile(ID);
1531 void *Where;
1532 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1533 if (!Value) {
1534 Value = Block;
1535 ValuesSet.InsertNode(Value, Where);
1536 Values.push_back(Value);
1537 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001538 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001539 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001540 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001541 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001542
Jim Laskeyef42a012006-11-02 20:12:39 +00001543 Die->AddValue(Attribute, Block->BestForm(), Value);
1544 }
1545
Jim Laskey65195462006-10-30 13:35:07 +00001546 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001547 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001548 void AddSourceLine(DIE *Die, const DIVariable *V) {
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001549 // If there is no compile unit specified, don't add a line #.
1550 if (V->getCompileUnit().isNull())
1551 return;
1552
Devang Patel7a6e5a32009-01-08 02:33:41 +00001553 unsigned Line = V->getLineNumber();
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001554 unsigned FileID = FindCompileUnit(V->getCompileUnit()).getID();
1555 assert(FileID && "Invalid file id");
Devang Patel7a6e5a32009-01-08 02:33:41 +00001556 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1557 AddUInt(Die, DW_AT_decl_line, 0, Line);
1558 }
1559
1560 /// AddSourceLine - Add location information to specified debug information
1561 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001562 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001563 // If there is no compile unit specified, don't add a line #.
1564 if (G->getCompileUnit().isNull())
1565 return;
Devang Patel8526cc02009-01-05 22:35:52 +00001566 unsigned Line = G->getLineNumber();
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001567 unsigned FileID = FindCompileUnit(G->getCompileUnit()).getID();
1568 assert(FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001569 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1570 AddUInt(Die, DW_AT_decl_line, 0, Line);
1571 }
1572
Devang Patel99ec3532009-01-16 19:28:14 +00001573 void AddSourceLine(DIE *Die, const DIType *Ty) {
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001574 // If there is no compile unit specified, don't add a line #.
Devang Pateldd9db662009-01-30 18:20:31 +00001575 DICompileUnit CU = Ty->getCompileUnit();
1576 if (CU.isNull())
1577 return;
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001578
1579 unsigned Line = Ty->getLineNumber();
1580 unsigned FileID = FindCompileUnit(CU).getID();
1581 assert(FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001582 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1583 AddUInt(Die, DW_AT_decl_line, 0, Line);
1584 }
1585
Jim Laskey65195462006-10-30 13:35:07 +00001586 /// AddAddress - Add an address attribute to a die based on the location
1587 /// provided.
1588 void AddAddress(DIE *Die, unsigned Attribute,
Devang Patel1be3ecc2009-04-15 00:10:26 +00001589 const MachineLocation &Location) {
Dan Gohmand735b802008-10-03 15:45:36 +00001590 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001591 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001592
Dan Gohmand735b802008-10-03 15:45:36 +00001593 if (Location.isReg()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001594 if (Reg < 32) {
1595 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1596 } else {
1597 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1598 AddUInt(Block, 0, DW_FORM_udata, Reg);
1599 }
1600 } else {
1601 if (Reg < 32) {
1602 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1603 } else {
1604 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1605 AddUInt(Block, 0, DW_FORM_udata, Reg);
1606 }
1607 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1608 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001609
Jim Laskeyef42a012006-11-02 20:12:39 +00001610 AddBlock(Die, Attribute, 0, Block);
1611 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001612
Jim Laskeyef42a012006-11-02 20:12:39 +00001613 /// AddType - Add a new type attribute to the specified entity.
Devang Patel48d190f2009-01-05 21:47:57 +00001614 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel80303aa2009-01-23 19:13:31 +00001615 if (Ty.isNull())
Devang Patel48d190f2009-01-05 21:47:57 +00001616 return;
Devang Patel48d190f2009-01-05 21:47:57 +00001617
1618 // Check for pre-existence.
1619 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1620 // If it exists then use the existing value.
1621 if (Slot) {
1622 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1623 return;
1624 }
1625
1626 // Set up proxy.
1627 Slot = NewDIEntry();
1628
1629 // Construct type.
1630 DIE Buffer(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001631 if (Ty.isBasicType(Ty.getTag()))
1632 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1633 else if (Ty.isDerivedType(Ty.getTag()))
1634 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1635 else {
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001636 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelf193ff02009-01-15 19:26:23 +00001637 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1638 }
1639
Devang Patel7009d242009-01-27 23:22:55 +00001640 // Add debug information entry to entity and appropriate context.
1641 DIE *Die = NULL;
1642 DIDescriptor Context = Ty.getContext();
1643 if (!Context.isNull())
1644 Die = DW_Unit->getDieMapSlotFor(Context.getGV());
1645
1646 if (Die) {
1647 DIE *Child = new DIE(Buffer);
1648 Die->AddChild(Child);
1649 Buffer.Detach();
1650 SetDIEntry(Slot, Child);
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001651 } else {
Devang Patel7009d242009-01-27 23:22:55 +00001652 Die = DW_Unit->AddDie(Buffer);
1653 SetDIEntry(Slot, Die);
1654 }
1655
Devang Patel48d190f2009-01-05 21:47:57 +00001656 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1657 }
1658
Devang Patele5202732009-01-05 19:07:53 +00001659 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1660 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001661 DIBasicType BTy) {
Bill Wendling0582ae92009-03-13 04:39:26 +00001662
Devang Patel08f053f2009-01-05 17:57:47 +00001663 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001664 std::string Name;
1665 BTy.getName(Name);
Devang Patel08f053f2009-01-05 17:57:47 +00001666 Buffer.setTag(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001667 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patel08f053f2009-01-05 17:57:47 +00001668 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001669 if (!Name.empty())
Devang Patel08f053f2009-01-05 17:57:47 +00001670 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001671 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel08f053f2009-01-05 17:57:47 +00001672 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1673 }
1674
Devang Patele5202732009-01-05 19:07:53 +00001675 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1676 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001677 DIDerivedType DTy) {
Bill Wendling0582ae92009-03-13 04:39:26 +00001678
Devang Patel08f053f2009-01-05 17:57:47 +00001679 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001680 std::string Name;
1681 DTy.getName(Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001682 uint64_t Size = DTy.getSizeInBits() >> 3;
1683 unsigned Tag = DTy.getTag();
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001684
Devang Patel08f053f2009-01-05 17:57:47 +00001685 // FIXME - Workaround for templates.
1686 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1687
1688 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001689
Devang Patel08f053f2009-01-05 17:57:47 +00001690 // Map to main type, void will not have a type.
Devang Patelf193ff02009-01-15 19:26:23 +00001691 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00001692 AddType(DW_Unit, &Buffer, FromTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001693
1694 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001695 if (!Name.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00001696 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel08f053f2009-01-05 17:57:47 +00001697
1698 // Add size if non-zero (derived types might be zero-sized.)
1699 if (Size)
1700 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1701
1702 // Add source line info if available and TyDesc is not a forward
1703 // declaration.
Devang Patelad165be2009-01-27 00:45:04 +00001704 if (!DTy.isForwardDecl())
1705 AddSourceLine(&Buffer, &DTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001706 }
1707
Devang Patelf4215332009-01-05 19:55:51 +00001708 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1709 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001710 DICompositeType CTy) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001711 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001712 std::string Name;
1713 CTy.getName(Name);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001714
Devang Patelf193ff02009-01-15 19:26:23 +00001715 uint64_t Size = CTy.getSizeInBits() >> 3;
1716 unsigned Tag = CTy.getTag();
Devang Patel49f38cb2009-01-23 01:19:09 +00001717 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001718
Devang Patelf4215332009-01-05 19:55:51 +00001719 switch (Tag) {
1720 case DW_TAG_vector_type:
1721 case DW_TAG_array_type:
Devang Patelf193ff02009-01-15 19:26:23 +00001722 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001723 break;
Devang Pateleab4a2e2009-01-20 18:35:14 +00001724 case DW_TAG_enumeration_type:
1725 {
1726 DIArray Elements = CTy.getTypeArray();
1727 // Add enumerators to enumeration type.
1728 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1729 DIE *ElemDie = NULL;
1730 DIEnumerator Enum(Elements.getElement(i).getGV());
1731 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
1732 Buffer.AddChild(ElemDie);
1733 }
1734 }
1735 break;
Devang Patelf4215332009-01-05 19:55:51 +00001736 case DW_TAG_subroutine_type:
1737 {
Devang Patelf4215332009-01-05 19:55:51 +00001738 // Add return type.
Bill Wendling3f500d92009-05-06 21:21:34 +00001739 DIArray Elements = CTy.getTypeArray();
Devang Patel48d190f2009-01-05 21:47:57 +00001740 DIDescriptor RTy = Elements.getElement(0);
Devang Patelf193ff02009-01-15 19:26:23 +00001741 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel48d190f2009-01-05 21:47:57 +00001742
Bill Wendling3f500d92009-05-06 21:21:34 +00001743 // Add prototype flag.
1744 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1745
Devang Patelf4215332009-01-05 19:55:51 +00001746 // Add arguments.
1747 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1748 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00001749 DIDescriptor Ty = Elements.getElement(i);
Devang Patel7ab24502009-01-17 06:57:25 +00001750 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patelf4215332009-01-05 19:55:51 +00001751 Buffer.AddChild(Arg);
1752 }
1753 }
1754 break;
1755 case DW_TAG_structure_type:
1756 case DW_TAG_union_type:
Devang Patel86bda412009-03-25 00:28:40 +00001757 case DW_TAG_class_type:
Devang Patelf4215332009-01-05 19:55:51 +00001758 {
1759 // Add elements to structure type.
Devang Patelf193ff02009-01-15 19:26:23 +00001760 DIArray Elements = CTy.getTypeArray();
Devang Patel153745c2009-01-16 00:50:53 +00001761
1762 // A forward struct declared type may not have elements available.
1763 if (Elements.isNull())
1764 break;
1765
Devang Patelf4215332009-01-05 19:55:51 +00001766 // Add elements to structure type.
1767 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1768 DIDescriptor Element = Elements.getElement(i);
Devang Patel5aac3d32009-01-17 08:01:33 +00001769 DIE *ElemDie = NULL;
Devang Patelf193ff02009-01-15 19:26:23 +00001770 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel2d1768c2009-01-17 08:05:14 +00001771 ElemDie = CreateSubprogramDIE(DW_Unit,
1772 DISubprogram(Element.getGV()));
Dan Gohman9a38e3e2009-05-07 19:46:24 +00001773 else if (Element.getTag() == dwarf::DW_TAG_variable) // ???
Devang Patel5aac3d32009-01-17 08:01:33 +00001774 ElemDie = CreateGlobalVariableDIE(DW_Unit,
1775 DIGlobalVariable(Element.getGV()));
Devang Patel2be58932009-01-20 21:02:02 +00001776 else
1777 ElemDie = CreateMemberDIE(DW_Unit,
1778 DIDerivedType(Element.getGV()));
Devang Patel2d1768c2009-01-17 08:05:14 +00001779 Buffer.AddChild(ElemDie);
Devang Patelf4215332009-01-05 19:55:51 +00001780 }
Devang Patel13319ce2009-02-17 22:43:44 +00001781 unsigned RLang = CTy.getRunTimeLang();
1782 if (RLang)
1783 AddUInt(&Buffer, DW_AT_APPLE_runtime_class, DW_FORM_data1, RLang);
Devang Patelf4215332009-01-05 19:55:51 +00001784 }
1785 break;
1786 default:
1787 break;
1788 }
1789
1790 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001791 if (!Name.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00001792 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf4215332009-01-05 19:55:51 +00001793
Devang Patelad165be2009-01-27 00:45:04 +00001794 if (Tag == DW_TAG_enumeration_type || Tag == DW_TAG_structure_type
1795 || Tag == DW_TAG_union_type) {
1796 // Add size if non-zero (derived types might be zero-sized.)
1797 if (Size)
1798 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1799 else {
1800 // Add zero size if it is not a forward declaration.
1801 if (CTy.isForwardDecl())
1802 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1803 else
1804 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1805 }
1806
1807 // Add source line info if available.
1808 if (!CTy.isForwardDecl())
1809 AddSourceLine(&Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001810 }
Devang Patelf4215332009-01-05 19:55:51 +00001811 }
1812
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001813 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1814 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelf193ff02009-01-15 19:26:23 +00001815 int64_t L = SR.getLo();
1816 int64_t H = SR.getHi();
Devang Patel68afdc32009-01-05 18:33:01 +00001817 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1818 if (L != H) {
1819 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1820 if (L)
Devang Patel2d1768c2009-01-17 08:05:14 +00001821 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1822 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel68afdc32009-01-05 18:33:01 +00001823 }
1824 Buffer.AddChild(DW_Subrange);
1825 }
1826
1827 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1828 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1829 DICompositeType *CTy) {
1830 Buffer.setTag(DW_TAG_array_type);
1831 if (CTy->getTag() == DW_TAG_vector_type)
1832 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1833
Devang Patelf9235742009-01-28 21:08:20 +00001834 // Emit derived type.
1835 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel68afdc32009-01-05 18:33:01 +00001836 DIArray Elements = CTy->getTypeArray();
Devang Patel68afdc32009-01-05 18:33:01 +00001837
1838 // Construct an anonymous type for index type.
1839 DIE IdxBuffer(DW_TAG_base_type);
1840 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1841 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1842 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1843
1844 // Add subranges to array type.
1845 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patelf4215332009-01-05 19:55:51 +00001846 DIDescriptor Element = Elements.getElement(i);
Devang Patelf193ff02009-01-15 19:26:23 +00001847 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1848 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel68afdc32009-01-05 18:33:01 +00001849 }
1850 }
1851
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001852 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Pateleab4a2e2009-01-20 18:35:14 +00001853 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patelc69bf2c2009-01-05 18:38:38 +00001854
1855 DIE *Enumerator = new DIE(DW_TAG_enumerator);
Bill Wendling0582ae92009-03-13 04:39:26 +00001856 std::string Name;
1857 ETy->getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001858 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
Devang Patelc69bf2c2009-01-05 18:38:38 +00001859 int64_t Value = ETy->getEnumValue();
1860 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Pateleab4a2e2009-01-20 18:35:14 +00001861 return Enumerator;
Devang Patelc69bf2c2009-01-05 18:38:38 +00001862 }
Devang Patel68afdc32009-01-05 18:33:01 +00001863
Devang Patel5aac3d32009-01-17 08:01:33 +00001864 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001865 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patel5aac3d32009-01-17 08:01:33 +00001866 {
1867 DIE *GVDie = new DIE(DW_TAG_variable);
Bill Wendling0582ae92009-03-13 04:39:26 +00001868 std::string Name;
1869 GV.getDisplayName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001870 AddString(GVDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling0582ae92009-03-13 04:39:26 +00001871 std::string LinkageName;
1872 GV.getLinkageName(LinkageName);
1873 if (!LinkageName.empty())
Devang Patel5aac3d32009-01-17 08:01:33 +00001874 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1875 AddType(DW_Unit, GVDie, GV.getType());
1876 if (!GV.isLocalToUnit())
1877 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
1878 AddSourceLine(GVDie, &GV);
1879 return GVDie;
Devang Patel86ae1422009-01-05 18:59:44 +00001880 }
1881
Devang Patel2be58932009-01-20 21:02:02 +00001882 /// CreateMemberDIE - Create new member DIE.
1883 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
1884 DIE *MemberDie = new DIE(DT.getTag());
Bill Wendling0582ae92009-03-13 04:39:26 +00001885 std::string Name;
1886 DT.getName(Name);
1887 if (!Name.empty())
Devang Patel2be58932009-01-20 21:02:02 +00001888 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1889
1890 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1891
1892 AddSourceLine(MemberDie, &DT);
1893
Devang Patel36375ee2009-02-17 21:23:59 +00001894 uint64_t Size = DT.getSizeInBits();
1895 uint64_t FieldSize = DT.getOriginalTypeSize();
1896
1897 if (Size != FieldSize) {
1898 // Handle bitfield.
1899 AddUInt(MemberDie, DW_AT_byte_size, 0, DT.getOriginalTypeSize() >> 3);
1900 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1901
1902 uint64_t Offset = DT.getOffsetInBits();
1903 uint64_t FieldOffset = Offset;
1904 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1905 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1906 FieldOffset = (HiMark - FieldSize);
1907 Offset -= FieldOffset;
1908 // Maybe we need to work from the other end.
1909 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1910 AddUInt(MemberDie, DW_AT_bit_offset, 0, Offset);
1911 }
Devang Patel2be58932009-01-20 21:02:02 +00001912 DIEBlock *Block = new DIEBlock();
1913 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1914 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1915 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1916
Devang Patel47661592009-01-21 00:08:04 +00001917 if (DT.isProtected())
1918 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1919 else if (DT.isPrivate())
1920 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1921
Devang Patel2be58932009-01-20 21:02:02 +00001922 return MemberDie;
1923 }
1924
Devang Patel5aac3d32009-01-17 08:01:33 +00001925 /// CreateSubprogramDIE - Create new DIE using SP.
1926 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Bill Wendling3f500d92009-05-06 21:21:34 +00001927 const DISubprogram &SP,
Devang Patel2d1768c2009-01-17 08:05:14 +00001928 bool IsConstructor = false) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001929 DIE *SPDie = new DIE(DW_TAG_subprogram);
Bill Wendling3f500d92009-05-06 21:21:34 +00001930
Bill Wendling0582ae92009-03-13 04:39:26 +00001931 std::string Name;
1932 SP.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001933 AddString(SPDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling3f500d92009-05-06 21:21:34 +00001934
Bill Wendling0582ae92009-03-13 04:39:26 +00001935 std::string LinkageName;
1936 SP.getLinkageName(LinkageName);
Bill Wendling3f500d92009-05-06 21:21:34 +00001937
Bill Wendling0582ae92009-03-13 04:39:26 +00001938 if (!LinkageName.empty())
Bill Wendling3f500d92009-05-06 21:21:34 +00001939 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1940
Devang Patel5aac3d32009-01-17 08:01:33 +00001941 AddSourceLine(SPDie, &SP);
Devang Patel86ae1422009-01-05 18:59:44 +00001942
Devang Patel5aac3d32009-01-17 08:01:33 +00001943 DICompositeType SPTy = SP.getType();
1944 DIArray Args = SPTy.getTypeArray();
Bill Wendling3f500d92009-05-06 21:21:34 +00001945
1946 // Add prototyped tag, if C or ObjC.
1947 unsigned Lang = SP.getCompileUnit().getLanguage();
1948 if (Lang == DW_LANG_C99 || Lang == DW_LANG_C89 || Lang == DW_LANG_ObjC)
1949 AddUInt(SPDie, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patel5aac3d32009-01-17 08:01:33 +00001950
Devang Patel86ae1422009-01-05 18:59:44 +00001951 // Add Return Type.
Devang Patel75b27382009-04-08 22:18:45 +00001952 unsigned SPTag = SPTy.getTag();
Devang Patel9ac08d62009-02-27 18:05:21 +00001953 if (!IsConstructor) {
Devang Patel75b27382009-04-08 22:18:45 +00001954 if (Args.isNull() || SPTag != DW_TAG_subroutine_type)
Devang Patel9ac08d62009-02-27 18:05:21 +00001955 AddType(DW_Unit, SPDie, SPTy);
1956 else
1957 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
1958 }
Devang Pateld234e592009-01-30 01:21:46 +00001959
Devang Pateld5863dd2009-02-02 17:51:41 +00001960 if (!SP.isDefinition()) {
1961 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
Bill Wendling3f500d92009-05-06 21:21:34 +00001962 // Add arguments. Do not add arguments for subprogram definition. They
1963 // will be handled through RecordVariable.
Devang Patel75b27382009-04-08 22:18:45 +00001964 if (SPTag == DW_TAG_subroutine_type)
Devang Pateld5863dd2009-02-02 17:51:41 +00001965 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1966 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1967 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
Bill Wendling3f500d92009-05-06 21:21:34 +00001968 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ??
Devang Pateld5863dd2009-02-02 17:51:41 +00001969 SPDie->AddChild(Arg);
1970 }
1971 }
Devang Pateld234e592009-01-30 01:21:46 +00001972
Devang Patelf193ff02009-01-15 19:26:23 +00001973 if (!SP.isLocalToUnit())
Devang Pateld234e592009-01-30 01:21:46 +00001974 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Bill Wendling3f500d92009-05-06 21:21:34 +00001975
Devang Patel1be3ecc2009-04-15 00:10:26 +00001976 // DW_TAG_inlined_subroutine may refer to this DIE.
1977 DIE *&Slot = DW_Unit->getDieMapSlotFor(SP.getGV());
1978 Slot = SPDie;
Devang Patel5aac3d32009-01-17 08:01:33 +00001979 return SPDie;
Devang Patel86ae1422009-01-05 18:59:44 +00001980 }
1981
Devang Patel5aac3d32009-01-17 08:01:33 +00001982 /// FindCompileUnit - Get the compile unit for the given descriptor.
1983 ///
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001984 CompileUnit &FindCompileUnit(DICompileUnit Unit) const {
1985 DenseMap<Value *, CompileUnit *>::const_iterator I =
1986 CompileUnitMap.find(Unit.getGV());
1987 assert(I != CompileUnitMap.end() && "Missing compile unit.");
1988 return *I->second;
Devang Patel8526cc02009-01-05 22:35:52 +00001989 }
1990
Devang Patelbbdc8202009-01-13 23:54:55 +00001991 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001992 ///
1993 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1994 // Get the descriptor.
Devang Patel99ec3532009-01-16 19:28:14 +00001995 const DIVariable &VD = DV->getVariable();
Devang Patel7a6e5a32009-01-08 02:33:41 +00001996
1997 // Translate tag to proper Dwarf tag. The result variable is dropped for
1998 // now.
1999 unsigned Tag;
Devang Patel99ec3532009-01-16 19:28:14 +00002000 switch (VD.getTag()) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00002001 case DW_TAG_return_variable: return NULL;
2002 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2003 case DW_TAG_auto_variable: // fall thru
2004 default: Tag = DW_TAG_variable; break;
2005 }
2006
2007 // Define variable debug information entry.
2008 DIE *VariableDie = new DIE(Tag);
Bill Wendling0582ae92009-03-13 04:39:26 +00002009 std::string Name;
2010 VD.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00002011 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002012
2013 // Add source line info if available.
Devang Patel99ec3532009-01-16 19:28:14 +00002014 AddSourceLine(VariableDie, &VD);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002015
2016 // Add variable type.
Devang Patel99ec3532009-01-16 19:28:14 +00002017 AddType(Unit, VariableDie, VD.getType());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002018
2019 // Add variable address.
2020 MachineLocation Location;
2021 Location.set(RI->getFrameRegister(*MF),
2022 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
2023 AddAddress(VariableDie, DW_AT_location, Location);
2024
2025 return VariableDie;
2026 }
2027
Devang Patel7a6e5a32009-01-08 02:33:41 +00002028 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2029 ///
2030 DbgScope *getOrCreateScope(GlobalVariable *V) {
2031 DbgScope *&Slot = DbgScopeMap[V];
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002032 if (Slot) return Slot;
2033
Devang Patel1be3ecc2009-04-15 00:10:26 +00002034 DbgScope *Parent = NULL;
2035 DIBlock Block(V);
Bill Wendling3f500d92009-05-06 21:21:34 +00002036
Devang Patel1be3ecc2009-04-15 00:10:26 +00002037 if (!Block.isNull()) {
2038 DIDescriptor ParentDesc = Block.getContext();
2039 Parent =
2040 ParentDesc.isNull() ? NULL : getOrCreateScope(ParentDesc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002041 }
Bill Wendling3f500d92009-05-06 21:21:34 +00002042
Devang Patel1be3ecc2009-04-15 00:10:26 +00002043 Slot = new DbgScope(Parent, DIDescriptor(V));
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002044
Devang Patel1be3ecc2009-04-15 00:10:26 +00002045 if (Parent)
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002046 Parent->AddScope(Slot);
Devang Patel1be3ecc2009-04-15 00:10:26 +00002047 else
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002048 // First function is top level function.
Devang Patel7d2f9722009-04-15 20:41:31 +00002049 FunctionDbgScope = Slot;
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002050
Devang Patel7a6e5a32009-01-08 02:33:41 +00002051 return Slot;
2052 }
2053
Dan Gohman9a38e3e2009-05-07 19:46:24 +00002054 /// createInlinedSubroutineScope - Returns the scope associated with the
2055 /// inlined subroutine.
2056 ///
2057 DbgScope *createInlinedSubroutineScope(DISubprogram SP, unsigned Src,
2058 unsigned Line, unsigned Col) {
2059 DbgScope *Scope =
2060 new DbgInlinedSubroutineScope(NULL, SP, Src, Line, Col);
2061
2062 // FIXME - Add inlined function scopes to the root so we can delete them
2063 // later.
2064 assert (FunctionDbgScope && "Function scope info missing!");
2065 FunctionDbgScope->AddScope(Scope);
2066 return Scope;
2067 }
2068
Devang Patel7a6e5a32009-01-08 02:33:41 +00002069 /// ConstructDbgScope - Construct the components of a scope.
2070 ///
2071 void ConstructDbgScope(DbgScope *ParentScope,
2072 unsigned ParentStartID, unsigned ParentEndID,
2073 DIE *ParentDie, CompileUnit *Unit) {
Dan Gohman9a38e3e2009-05-07 19:46:24 +00002074 // Add variables to scope.
2075 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
2076 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2077 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2078 if (VariableDie) ParentDie->AddChild(VariableDie);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002079 }
2080
2081 // Add nested scopes.
Devang Patel9795da52009-01-10 02:42:49 +00002082 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002083 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2084 // Define the Scope debug information entry.
2085 DbgScope *Scope = Scopes[j];
Devang Patel7a6e5a32009-01-08 02:33:41 +00002086
Devang Patele9bfb0f2009-01-12 18:41:00 +00002087 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2088 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002089
Devang Patel1be3ecc2009-04-15 00:10:26 +00002090 // Ignore empty scopes.
2091 // Do not ignore inlined scope even if it does not have any
2092 // variables or scopes.
Devang Patel7a6e5a32009-01-08 02:33:41 +00002093 if (StartID == EndID && StartID != 0) continue;
Devang Patel1be3ecc2009-04-15 00:10:26 +00002094 if (!Scope->isInlinedSubroutine()
Devang Patel0f7fef32009-04-13 17:02:03 +00002095 && Scope->getScopes().empty() && Scope->getVariables().empty())
2096 continue;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002097
2098 if (StartID == ParentStartID && EndID == ParentEndID) {
2099 // Just add stuff to the parent scope.
2100 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2101 } else {
Devang Patel1be3ecc2009-04-15 00:10:26 +00002102 DIE *ScopeDie = NULL;
Dan Gohman9a38e3e2009-05-07 19:46:24 +00002103 if (MainCU && TAI->doesDwarfUsesInlineInfoSection()
2104 && Scope->isInlinedSubroutine()) {
2105 ScopeDie = new DIE(DW_TAG_inlined_subroutine);
2106 DIE *Origin = MainCU->getDieMapSlotFor(Scope->getDesc().getGV());
2107 AddDIEntry(ScopeDie, DW_AT_abstract_origin, DW_FORM_ref4, Origin);
2108 AddUInt(ScopeDie, DW_AT_call_file, 0, Scope->getFile());
2109 AddUInt(ScopeDie, DW_AT_call_line, 0, Scope->getLine());
2110 AddUInt(ScopeDie, DW_AT_call_column, 0, Scope->getColumn());
2111 } else {
Devang Patel1be3ecc2009-04-15 00:10:26 +00002112 ScopeDie = new DIE(DW_TAG_lexical_block);
Dan Gohman9a38e3e2009-05-07 19:46:24 +00002113 }
Bill Wendling7fa81622009-05-01 08:25:13 +00002114
2115 // Add the scope bounds.
2116 if (StartID)
2117 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2118 DWLabel("label", StartID));
2119 else
2120 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2121 DWLabel("func_begin", SubprogramCount));
2122
2123 if (EndID)
2124 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2125 DWLabel("label", EndID));
2126 else
2127 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2128 DWLabel("func_end", SubprogramCount));
2129
2130 // Add the scope contents.
2131 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2132 ParentDie->AddChild(ScopeDie);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002133 }
2134 }
2135 }
2136
Devang Patel7d2f9722009-04-15 20:41:31 +00002137 /// ConstructFunctionDbgScope - Construct the scope for the subprogram.
Devang Patel7a6e5a32009-01-08 02:33:41 +00002138 ///
Devang Patel7d2f9722009-04-15 20:41:31 +00002139 void ConstructFunctionDbgScope(DbgScope *RootScope) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00002140 // Exit if there is no root scope.
2141 if (!RootScope) return;
Devang Patel0e5200f2009-01-15 18:25:17 +00002142 DIDescriptor Desc = RootScope->getDesc();
2143 if (Desc.isNull())
2144 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002145
2146 // Get the subprogram debug information entry.
Devang Patel0e5200f2009-01-15 18:25:17 +00002147 DISubprogram SPD(Desc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002148
2149 // Get the compile unit context.
Devang Pateldd9db662009-01-30 18:20:31 +00002150 CompileUnit *Unit = MainCU;
2151 if (!Unit)
Chris Lattnere3f6cea2009-05-05 04:55:56 +00002152 Unit = &FindCompileUnit(SPD.getCompileUnit());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002153
2154 // Get the subprogram die.
Devang Patelf6bac3e2009-01-12 22:58:14 +00002155 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002156 assert(SPDie && "Missing subprogram descriptor");
2157
2158 // Add the function bounds.
2159 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2160 DWLabel("func_begin", SubprogramCount));
2161 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2162 DWLabel("func_end", SubprogramCount));
2163 MachineLocation Location(RI->getFrameRegister(*MF));
2164 AddAddress(SPDie, DW_AT_frame_base, Location);
2165
2166 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2167 }
2168
2169 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2170 ///
2171 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Chenge3d42322009-02-25 07:04:34 +00002172 const char *FnName = MF->getFunction()->getNameStart();
2173 if (MainCU) {
Bill Wendling972bbac2009-04-09 21:49:15 +00002174 StringMap<DIE*> &Globals = MainCU->getGlobals();
2175 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Chenge3d42322009-02-25 07:04:34 +00002176 if (GI != Globals.end()) {
2177 DIE *SPDie = GI->second;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002178
2179 // Add the function bounds.
2180 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2181 DWLabel("func_begin", SubprogramCount));
2182 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2183 DWLabel("func_end", SubprogramCount));
2184
2185 MachineLocation Location(RI->getFrameRegister(*MF));
2186 AddAddress(SPDie, DW_AT_frame_base, Location);
2187 return;
2188 }
Evan Chenge3d42322009-02-25 07:04:34 +00002189 } else {
2190 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2191 CompileUnit *Unit = CompileUnits[i];
Bill Wendling972bbac2009-04-09 21:49:15 +00002192 StringMap<DIE*> &Globals = Unit->getGlobals();
2193 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Chenge3d42322009-02-25 07:04:34 +00002194 if (GI != Globals.end()) {
2195 DIE *SPDie = GI->second;
2196
2197 // Add the function bounds.
2198 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2199 DWLabel("func_begin", SubprogramCount));
2200 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2201 DWLabel("func_end", SubprogramCount));
2202
2203 MachineLocation Location(RI->getFrameRegister(*MF));
2204 AddAddress(SPDie, DW_AT_frame_base, Location);
2205 return;
2206 }
2207 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00002208 }
Evan Chenge3d42322009-02-25 07:04:34 +00002209
Devang Patel7a6e5a32009-01-08 02:33:41 +00002210#if 0
2211 // FIXME: This is causing an abort because C++ mangled names are compared
2212 // with their unmangled counterparts. See PR2885. Don't do this assert.
2213 assert(0 && "Couldn't find DIE for machine function!");
2214#endif
2215 }
2216
Jim Laskeyef42a012006-11-02 20:12:39 +00002217 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2218 /// tools to recognize the object file contains Dwarf information.
2219 void EmitInitial() {
2220 // Check to see if we already emitted intial headers.
2221 if (didInitial) return;
2222 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002223
Jim Laskeyef42a012006-11-02 20:12:39 +00002224 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002225 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002226 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002227 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002228 }
2229 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2230 EmitLabel("section_info", 0);
2231 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2232 EmitLabel("section_abbrev", 0);
2233 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2234 EmitLabel("section_aranges", 0);
Scott Michel210de722009-01-26 22:32:51 +00002235 if (TAI->doesSupportMacInfoSection()) {
2236 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2237 EmitLabel("section_macinfo", 0);
2238 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002239 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2240 EmitLabel("section_line", 0);
2241 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2242 EmitLabel("section_loc", 0);
2243 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2244 EmitLabel("section_pubnames", 0);
2245 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2246 EmitLabel("section_str", 0);
2247 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2248 EmitLabel("section_ranges", 0);
2249
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002250 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002251 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002252 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002253 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002254 }
2255
Jim Laskey65195462006-10-30 13:35:07 +00002256 /// EmitDIE - Recusively Emits a debug information entry.
2257 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002258 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002259 // Get the abbreviation for this DIE.
2260 unsigned AbbrevNumber = Die->getAbbrevNumber();
2261 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002262
Jim Laskeybacd3042007-02-21 22:48:45 +00002263 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002264
2265 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002266 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002267
Evan Cheng42bf74b2009-03-25 01:47:28 +00002268 if (Asm->isVerbose())
Evan Cheng6547e402008-07-01 23:18:29 +00002269 Asm->EOL(std::string("Abbrev [" +
2270 utostr(AbbrevNumber) +
2271 "] 0x" + utohexstr(Die->getOffset()) +
2272 ":0x" + utohexstr(Die->getSize()) + " " +
2273 TagString(Abbrev->getTag())));
2274 else
2275 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002276
Owen Anderson873e1b52008-06-24 21:44:59 +00002277 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2278 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002279
Jim Laskeyef42a012006-11-02 20:12:39 +00002280 // Emit the DIE attribute values.
2281 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2282 unsigned Attr = AbbrevData[i].getAttribute();
2283 unsigned Form = AbbrevData[i].getForm();
2284 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002285
Jim Laskeyef42a012006-11-02 20:12:39 +00002286 switch (Attr) {
2287 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002288 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002289 break;
2290 }
2291 default: {
2292 // Emit an attribute using the defined form.
2293 Values[i]->EmitValue(*this, Form);
2294 break;
2295 }
2296 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002297
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002298 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002299 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002300
Jim Laskeyef42a012006-11-02 20:12:39 +00002301 // Emit the DIE children if any.
2302 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2303 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002304
Bill Wendlinga9519572009-05-08 20:38:02 +00002305 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Jim Laskeyef42a012006-11-02 20:12:39 +00002306 EmitDIE(Children[j]);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002307
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002308 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002309 }
2310 }
2311
Jim Laskey65195462006-10-30 13:35:07 +00002312 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2313 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002314 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2315 // Get the children.
2316 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002317
Jim Laskeyef42a012006-11-02 20:12:39 +00002318 // If not last sibling and has children then add sibling offset attribute.
2319 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2320
2321 // Record the abbreviation.
2322 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002323
Jim Laskeyef42a012006-11-02 20:12:39 +00002324 // Get the abbreviation for this DIE.
2325 unsigned AbbrevNumber = Die->getAbbrevNumber();
2326 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2327
2328 // Set DIE offset
2329 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002330
Jim Laskeyef42a012006-11-02 20:12:39 +00002331 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002332 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2333
Owen Anderson873e1b52008-06-24 21:44:59 +00002334 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2335 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002336
2337 // Size the DIE attribute values.
2338 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2339 // Size attribute value.
2340 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2341 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002342
Jim Laskeyef42a012006-11-02 20:12:39 +00002343 // Size the DIE children if any.
2344 if (!Children.empty()) {
2345 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2346 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002347
Jim Laskeyef42a012006-11-02 20:12:39 +00002348 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2349 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2350 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002351
Jim Laskeyef42a012006-11-02 20:12:39 +00002352 // End of children marker.
2353 Offset += sizeof(int8_t);
2354 }
2355
2356 Die->setSize(Offset - Die->getOffset());
2357 return Offset;
2358 }
Jim Laskey65195462006-10-30 13:35:07 +00002359
2360 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2361 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002362 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002363 // Process base compile unit.
Devang Pateldd9db662009-01-30 18:20:31 +00002364 if (MainCU) {
2365 // Compute size of compile unit header
2366 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2367 sizeof(int16_t) + // DWARF version number
2368 sizeof(int32_t) + // Offset Into Abbrev. Section
2369 sizeof(int8_t); // Pointer Size (in bytes)
2370 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
2371 return;
2372 }
Evan Chenge3d42322009-02-25 07:04:34 +00002373 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2374 CompileUnit *Unit = CompileUnits[i];
Devang Patel72b66352009-01-12 23:05:55 +00002375 // Compute size of compile unit header
2376 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2377 sizeof(int16_t) + // DWARF version number
2378 sizeof(int32_t) + // Offset Into Abbrev. Section
2379 sizeof(int8_t); // Pointer Size (in bytes)
2380 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2381 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002382 }
2383
Evan Chenge3d42322009-02-25 07:04:34 +00002384 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Jim Laskey65195462006-10-30 13:35:07 +00002385 ///
Evan Chenge3d42322009-02-25 07:04:34 +00002386 void EmitDebugInfoPerCU(CompileUnit *Unit) {
2387 DIE *Die = Unit->getDie();
2388 // Emit the compile units header.
2389 EmitLabel("info_begin", Unit->getID());
2390 // Emit size of content not including length itself
2391 unsigned ContentSize = Die->getSize() +
2392 sizeof(int16_t) + // DWARF version number
2393 sizeof(int32_t) + // Offset Into Abbrev. Section
2394 sizeof(int8_t) + // Pointer Size (in bytes)
2395 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2396
2397 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2398 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2399 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2400 Asm->EOL("Offset Into Abbrev. Section");
2401 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2402
2403 EmitDIE(Die);
2404 // FIXME - extra padding for gdb bug.
2405 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2406 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2407 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2408 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2409 EmitLabel("info_end", Unit->getID());
2410
2411 Asm->EOL();
2412 }
2413
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002414 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002415 // Start debug info section.
2416 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002417
Evan Chenge3d42322009-02-25 07:04:34 +00002418 if (MainCU) {
2419 EmitDebugInfoPerCU(MainCU);
2420 return;
Devang Patel72b66352009-01-12 23:05:55 +00002421 }
Evan Chenge3d42322009-02-25 07:04:34 +00002422
2423 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2424 EmitDebugInfoPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002425 }
2426
Jim Laskey65195462006-10-30 13:35:07 +00002427 /// EmitAbbreviations - Emit the abbreviation section.
2428 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002429 void EmitAbbreviations() const {
2430 // Check to see if it is worth the effort.
2431 if (!Abbreviations.empty()) {
2432 // Start the debug abbrev section.
2433 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002434
Jim Laskeyef42a012006-11-02 20:12:39 +00002435 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002436
Jim Laskeyef42a012006-11-02 20:12:39 +00002437 // For each abbrevation.
2438 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2439 // Get abbreviation data
2440 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002441
Jim Laskeyef42a012006-11-02 20:12:39 +00002442 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002443 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2444 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002445
Jim Laskeyef42a012006-11-02 20:12:39 +00002446 // Emit the abbreviations data.
2447 Abbrev->Emit(*this);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002448
Jim Laskeybacd3042007-02-21 22:48:45 +00002449 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002450 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002451
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002452 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00002453 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002454
Jim Laskeyef42a012006-11-02 20:12:39 +00002455 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002456
Jim Laskeybacd3042007-02-21 22:48:45 +00002457 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002458 }
2459 }
2460
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002461 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2462 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002463 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002464 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2465 // Define last address of section.
2466 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2467 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2468 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2469 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2470
2471 // Mark end of matrix.
2472 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2473 Asm->EmitULEB128Bytes(1); Asm->EOL();
2474 Asm->EmitInt8(1); Asm->EOL();
2475 }
2476
Jim Laskey65195462006-10-30 13:35:07 +00002477 /// EmitDebugLines - Emit source line information.
2478 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002479 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002480 // If the target is using .loc/.file, the assembler will be emitting the
2481 // .debug_line table automatically.
2482 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00002483 return;
2484
Jim Laskeyef42a012006-11-02 20:12:39 +00002485 // Minimum line delta, thus ranging from -10..(255-10).
2486 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2487 // Maximum line delta, thus ranging from -10..(255-10).
2488 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002489
Jim Laskeyef42a012006-11-02 20:12:39 +00002490 // Start the dwarf line section.
2491 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002492
Jim Laskeyef42a012006-11-02 20:12:39 +00002493 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002494
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002495 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002496 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002497 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002498
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002499 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002500
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002501 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002502 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002503 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002504
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002505 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002506
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002507 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002508
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002509 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002510
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002511 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002512
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002513 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002514
Jim Laskeyef42a012006-11-02 20:12:39 +00002515 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002516 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2517 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2518 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2519 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2520 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2521 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2522 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2523 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2524 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00002525
Jim Laskeyef42a012006-11-02 20:12:39 +00002526 // Emit directories.
Evan Chenge3d42322009-02-25 07:04:34 +00002527 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2528 Asm->EmitString(getSourceDirectoryName(DI));
2529 Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00002530 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002531 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002532
Jim Laskeyef42a012006-11-02 20:12:39 +00002533 // Emit files.
Evan Chenge3d42322009-02-25 07:04:34 +00002534 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2535 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002536 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Evan Chenge3d42322009-02-25 07:04:34 +00002537 Asm->EmitString(getSourceFileName(Id.second));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002538 Asm->EOL("Source");
Evan Chenge3d42322009-02-25 07:04:34 +00002539 Asm->EmitULEB128Bytes(Id.first);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002540 Asm->EOL("Directory #");
2541 Asm->EmitULEB128Bytes(0);
2542 Asm->EOL("Mod date");
2543 Asm->EmitULEB128Bytes(0);
2544 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002545 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002546 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002547
Jim Laskeyef42a012006-11-02 20:12:39 +00002548 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002549
Jim Laskeyef42a012006-11-02 20:12:39 +00002550 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002551 unsigned SecSrcLinesSize = SectionSourceLines.size();
2552
2553 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002554 // Isolate current sections line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00002555 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00002556
Evan Cheng42bf74b2009-03-25 01:47:28 +00002557 if (Asm->isVerbose()) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002558 const Section* S = SectionMap[j + 1];
Evan Chenge3d42322009-02-25 07:04:34 +00002559 O << '\t' << TAI->getCommentString() << " Section"
2560 << S->getName() << '\n';
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002561 } else
Evan Cheng6547e402008-07-01 23:18:29 +00002562 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002563
2564 // Dwarf assumes we start with first line of first source file.
2565 unsigned Source = 1;
2566 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002567
Jim Laskeyef42a012006-11-02 20:12:39 +00002568 // Construct rows of the address, source, line, column matrix.
2569 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patelf3ee5142009-01-12 22:54:42 +00002570 const SrcLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002571 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00002572 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002573
Evan Cheng42bf74b2009-03-25 01:47:28 +00002574 if (!Asm->isVerbose())
Evan Cheng6547e402008-07-01 23:18:29 +00002575 Asm->EOL();
Evan Chenge3d42322009-02-25 07:04:34 +00002576 else {
2577 std::pair<unsigned, unsigned> SourceID =
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002578 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Evan Chenge3d42322009-02-25 07:04:34 +00002579 O << '\t' << TAI->getCommentString() << ' '
2580 << getSourceDirectoryName(SourceID.first) << ' '
2581 << getSourceFileName(SourceID.second)
2582 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2583 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002584
2585 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002586 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00002587 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002588 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00002589 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002590
Jim Laskeyef42a012006-11-02 20:12:39 +00002591 // If change of source, then switch to the new source.
2592 if (Source != LineInfo.getSourceID()) {
2593 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002594 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2595 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00002596 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002597
Jim Laskeyef42a012006-11-02 20:12:39 +00002598 // If change of line.
2599 if (Line != LineInfo.getLine()) {
2600 // Determine offset.
2601 int Offset = LineInfo.getLine() - Line;
2602 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002603
Jim Laskeyef42a012006-11-02 20:12:39 +00002604 // Update line.
2605 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002606
Jim Laskeyef42a012006-11-02 20:12:39 +00002607 // If delta is small enough and in range...
2608 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2609 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002610 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00002611 } else {
2612 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002613 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2614 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2615 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002616 }
2617 } else {
2618 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002619 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002620 }
2621 }
2622
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002623 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002624 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002625
2626 if (SecSrcLinesSize == 0)
2627 // Because we're emitting a debug_line section, we still need a line
2628 // table. The linker and friends expect it to exist. If there's nothing to
2629 // put into it, emit an empty table.
2630 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002631
Jim Laskeyef42a012006-11-02 20:12:39 +00002632 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002633
Jim Laskeybacd3042007-02-21 22:48:45 +00002634 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002635 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002636
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002637 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00002638 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002639 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002640 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00002641 return;
2642
2643 int stackGrowth =
2644 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2645 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002646 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00002647
2648 // Start the dwarf frame section.
2649 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2650
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002651 EmitLabel("debug_frame_common", 0);
2652 EmitDifference("debug_frame_common_end", 0,
2653 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002654 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002655
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002656 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002657 Asm->EmitInt32((int)DW_CIE_ID);
2658 Asm->EOL("CIE Identifier Tag");
2659 Asm->EmitInt8(DW_CIE_VERSION);
2660 Asm->EOL("CIE Version");
2661 Asm->EmitString("");
2662 Asm->EOL("CIE Augmentation");
2663 Asm->EmitULEB128Bytes(1);
2664 Asm->EOL("CIE Code Alignment Factor");
2665 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002666 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002667 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002668 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002669
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002670 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002671 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002672
Dale Johannesenb97aec62007-11-13 19:13:01 +00002673 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002674
Evan Cheng05548eb2008-02-29 19:36:59 +00002675 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002676 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002677
Jim Laskeybacd3042007-02-21 22:48:45 +00002678 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002679 }
2680
Jim Laskey65195462006-10-30 13:35:07 +00002681 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2682 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002683 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002684 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00002685 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002686
Jim Laskeyef42a012006-11-02 20:12:39 +00002687 // Start the dwarf frame section.
2688 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002689
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002690 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2691 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002692 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002693
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002694 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002695
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002696 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2697 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002698 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002699
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002700 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002701 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002702 EmitDifference("func_end", DebugFrameInfo.Number,
2703 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002704 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002705
Devang Patel5aac3d32009-01-17 08:01:33 +00002706 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00002707 false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002708
Evan Cheng05548eb2008-02-29 19:36:59 +00002709 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002710 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00002711
Jim Laskeybacd3042007-02-21 22:48:45 +00002712 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002713 }
2714
Evan Chenge3d42322009-02-25 07:04:34 +00002715 void EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2716 EmitDifference("pubnames_end", Unit->getID(),
2717 "pubnames_begin", Unit->getID(), true);
2718 Asm->EOL("Length of Public Names Info");
2719
2720 EmitLabel("pubnames_begin", Unit->getID());
2721
2722 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2723
2724 EmitSectionOffset("info_begin", "section_info",
2725 Unit->getID(), 0, true, false);
2726 Asm->EOL("Offset of Compilation Unit Info");
2727
2728 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2729 true);
2730 Asm->EOL("Compilation Unit Length");
2731
Bill Wendling972bbac2009-04-09 21:49:15 +00002732 StringMap<DIE*> &Globals = Unit->getGlobals();
Bill Wendlingf34be822009-04-09 23:51:31 +00002733 for (StringMap<DIE*>::const_iterator
Bill Wendling972bbac2009-04-09 21:49:15 +00002734 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
Bill Wendlingf34be822009-04-09 23:51:31 +00002735 const char *Name = GI->getKeyData();
Evan Chenge3d42322009-02-25 07:04:34 +00002736 DIE * Entity = GI->second;
2737
2738 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
Bill Wendlingf34be822009-04-09 23:51:31 +00002739 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
Evan Chenge3d42322009-02-25 07:04:34 +00002740 }
2741
2742 Asm->EmitInt32(0); Asm->EOL("End Mark");
2743 EmitLabel("pubnames_end", Unit->getID());
2744
2745 Asm->EOL();
2746 }
2747
Jim Laskeyef42a012006-11-02 20:12:39 +00002748 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002749 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002750 void EmitDebugPubNames() {
2751 // Start the dwarf pubnames section.
2752 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002753
Evan Chenge3d42322009-02-25 07:04:34 +00002754 if (MainCU) {
2755 EmitDebugPubNamesPerCU(MainCU);
2756 return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002757 }
Evan Chenge3d42322009-02-25 07:04:34 +00002758
2759 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2760 EmitDebugPubNamesPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002761 }
2762
2763 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002764 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002765 void EmitDebugStr() {
2766 // Check to see if it is worth the effort.
2767 if (!StringPool.empty()) {
2768 // Start the dwarf str section.
2769 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002770
Jim Laskeyef42a012006-11-02 20:12:39 +00002771 // For each of strings in the string pool.
2772 for (unsigned StringID = 1, N = StringPool.size();
2773 StringID <= N; ++StringID) {
2774 // Emit a label for reference from debug information entries.
2775 EmitLabel("string", StringID);
2776 // Emit the string itself.
2777 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00002778 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002779 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002780
Jim Laskeybacd3042007-02-21 22:48:45 +00002781 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002782 }
2783 }
2784
2785 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002786 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002787 void EmitDebugLoc() {
2788 // Start the dwarf loc section.
2789 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002790
Jim Laskeybacd3042007-02-21 22:48:45 +00002791 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002792 }
2793
2794 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002795 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002796 void EmitDebugARanges() {
2797 // Start the dwarf aranges section.
2798 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002799
Jim Laskeyef42a012006-11-02 20:12:39 +00002800 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00002801#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002802 CompileUnit *Unit = GetBaseCompileUnit();
2803
Jim Laskey5496f012006-11-09 14:52:14 +00002804 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002805 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002806
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002807 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002808
Jim Laskey5496f012006-11-09 14:52:14 +00002809 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002810 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002811
Dan Gohman82482942007-09-27 23:12:31 +00002812 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002813
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002814 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002815
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002816 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2817 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002818
Jim Laskey5496f012006-11-09 14:52:14 +00002819 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002820 EmitReference("text_begin", 0); Asm->EOL("Address");
2821 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002822
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002823 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2824 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00002825#endif
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002826
Jim Laskeybacd3042007-02-21 22:48:45 +00002827 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002828 }
2829
2830 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002831 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002832 void EmitDebugRanges() {
2833 // Start the dwarf ranges section.
2834 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002835
Jim Laskeybacd3042007-02-21 22:48:45 +00002836 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002837 }
2838
2839 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002840 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002841 void EmitDebugMacInfo() {
Scott Michel210de722009-01-26 22:32:51 +00002842 if (TAI->doesSupportMacInfoSection()) {
2843 // Start the dwarf macinfo section.
2844 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002845
Scott Michel210de722009-01-26 22:32:51 +00002846 Asm->EOL();
2847 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002848 }
2849
Devang Patel0f7fef32009-04-13 17:02:03 +00002850 /// EmitDebugInlineInfo - Emit inline info using following format.
2851 /// Section Header:
2852 /// 1. length of section
2853 /// 2. Dwarf version number
2854 /// 3. address size.
2855 ///
2856 /// Entries (one "entry" for each function that was inlined):
2857 ///
2858 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
2859 /// otherwise offset into __debug_str for regular function name.
2860 /// 2. offset into __debug_str section for regular function name.
2861 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
2862 /// instances for the function.
2863 ///
2864 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
2865 /// inlined instance; the die_offset points to the inlined_subroutine die in
2866 /// the __debug_info section, and the low_pc is the starting address for the
2867 /// inlining instance.
2868 void EmitDebugInlineInfo() {
2869 if (!TAI->doesDwarfUsesInlineInfoSection())
2870 return;
2871
2872 if (!MainCU)
2873 return;
2874
2875 Asm->SwitchToDataSection(TAI->getDwarfDebugInlineSection());
2876 Asm->EOL();
2877 EmitDifference("debug_inlined_end", 1,
2878 "debug_inlined_begin", 1, true);
2879 Asm->EOL("Length of Debug Inlined Information Entry");
2880
2881 EmitLabel("debug_inlined_begin", 1);
2882
2883 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
2884 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2885
2886 for (DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
2887 I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2888 GlobalVariable *GV = I->first;
2889 SmallVector<unsigned, 4> &Labels = I->second;
2890 DISubprogram SP(GV);
2891 std::string Name;
2892 std::string LName;
Dan Gohman9a38e3e2009-05-07 19:46:24 +00002893
Devang Patel0f7fef32009-04-13 17:02:03 +00002894 SP.getLinkageName(LName);
2895 SP.getName(Name);
2896
2897 Asm->EmitString(LName.empty() ? Name : LName);
2898 Asm->EOL("MIPS linkage name");
2899
2900 Asm->EmitString(Name); Asm->EOL("Function name");
2901
2902 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2903
2904 for (SmallVector<unsigned, 4>::iterator LI = Labels.begin(),
2905 LE = Labels.end(); LI != LE; ++LI) {
2906 DIE *SP = MainCU->getDieMapSlotFor(GV);
2907 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2908
2909 if (TD->getPointerSize() == sizeof(int32_t))
2910 O << TAI->getData32bitsDirective();
2911 else
2912 O << TAI->getData64bitsDirective();
2913 PrintLabelName("label", *LI); Asm->EOL("low_pc");
2914 }
2915 }
2916
2917 EmitLabel("debug_inlined_end", 1);
2918 Asm->EOL();
2919 }
2920
Bill Wendlingc8615e42009-03-10 21:47:45 +00002921 /// GetOrCreateSourceID - Look up the source id with the given directory and
2922 /// source file names. If none currently exists, create a new id and insert it
2923 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
2924 /// as well.
2925 unsigned GetOrCreateSourceID(const std::string &DirName,
2926 const std::string &FileName) {
2927 unsigned DId;
2928 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
2929 if (DI != DirectoryIdMap.end()) {
2930 DId = DI->getValue();
2931 } else {
2932 DId = DirectoryNames.size() + 1;
2933 DirectoryIdMap[DirName] = DId;
2934 DirectoryNames.push_back(DirName);
2935 }
2936
2937 unsigned FId;
2938 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
2939 if (FI != SourceFileIdMap.end()) {
2940 FId = FI->getValue();
2941 } else {
2942 FId = SourceFileNames.size() + 1;
2943 SourceFileIdMap[FileName] = FId;
2944 SourceFileNames.push_back(FileName);
2945 }
2946
2947 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
2948 SourceIdMap.find(std::make_pair(DId, FId));
2949 if (SI != SourceIdMap.end())
2950 return SI->second;
2951
2952 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
2953 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
2954 SourceIds.push_back(std::make_pair(DId, FId));
2955
2956 return SrcId;
2957 }
2958
Evan Chenge3d42322009-02-25 07:04:34 +00002959 void ConstructCompileUnit(GlobalVariable *GV) {
2960 DICompileUnit DIUnit(GV);
Bill Wendling0582ae92009-03-13 04:39:26 +00002961 std::string Dir, FN, Prod;
2962 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
2963 DIUnit.getFilename(FN));
Evan Chenge3d42322009-02-25 07:04:34 +00002964
2965 DIE *Die = new DIE(DW_TAG_compile_unit);
2966 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2967 DWLabel("section_line", 0), DWLabel("section_line", 0),
2968 false);
Bill Wendling0582ae92009-03-13 04:39:26 +00002969 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer(Prod));
Evan Chenge3d42322009-02-25 07:04:34 +00002970 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002971 AddString(Die, DW_AT_name, DW_FORM_string, FN);
Bill Wendling0582ae92009-03-13 04:39:26 +00002972 if (!Dir.empty())
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002973 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Chenge3d42322009-02-25 07:04:34 +00002974 if (DIUnit.isOptimized())
2975 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendling0582ae92009-03-13 04:39:26 +00002976 std::string Flags;
2977 DIUnit.getFlags(Flags);
2978 if (!Flags.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00002979 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
2980 unsigned RVer = DIUnit.getRunTimeVersion();
2981 if (RVer)
2982 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
2983
2984 CompileUnit *Unit = new CompileUnit(ID, Die);
2985 if (DIUnit.isMain()) {
2986 assert(!MainCU && "Multiple main compile units are found!");
2987 MainCU = Unit;
2988 }
2989 CompileUnitMap[DIUnit.getGV()] = Unit;
2990 CompileUnits.push_back(Unit);
2991 }
2992
Devang Patelc4523242009-01-05 23:11:11 +00002993 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Pateld1ca9252009-01-05 23:03:32 +00002994 void ConstructCompileUnits() {
Evan Chenge3d42322009-02-25 07:04:34 +00002995 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.compile_units");
2996 if (!Root)
2997 return;
2998 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2999 "Malformed compile unit descriptor anchor type");
3000 Constant *RootC = cast<Constant>(*Root->use_begin());
3001 assert(RootC->hasNUsesOrMore(1) &&
3002 "Malformed compile unit descriptor anchor type");
3003 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3004 UI != UE; ++UI)
3005 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3006 UUI != UUE; ++UUI) {
3007 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3008 ConstructCompileUnit(GV);
Devang Pateldd9db662009-01-30 18:20:31 +00003009 }
Evan Chenge3d42322009-02-25 07:04:34 +00003010 }
3011
3012 bool ConstructGlobalVariableDIE(GlobalVariable *GV) {
3013 DIGlobalVariable DI_GV(GV);
3014 CompileUnit *DW_Unit = MainCU;
3015 if (!DW_Unit)
Chris Lattnere3f6cea2009-05-05 04:55:56 +00003016 DW_Unit = &FindCompileUnit(DI_GV.getCompileUnit());
Evan Chenge3d42322009-02-25 07:04:34 +00003017
3018 // Check for pre-existence.
3019 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
3020 if (Slot)
3021 return false;
3022
3023 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
3024
3025 // Add address.
3026 DIEBlock *Block = new DIEBlock();
3027 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Bill Wendling7d16e852009-04-10 00:12:49 +00003028 std::string GLN;
Evan Chenge3d42322009-02-25 07:04:34 +00003029 AddObjectLabel(Block, 0, DW_FORM_udata,
Bill Wendling7d16e852009-04-10 00:12:49 +00003030 Asm->getGlobalLinkName(DI_GV.getGlobal(), GLN));
Evan Chenge3d42322009-02-25 07:04:34 +00003031 AddBlock(VariableDie, DW_AT_location, 0, Block);
3032
3033 // Add to map.
3034 Slot = VariableDie;
Bill Wendling3f500d92009-05-06 21:21:34 +00003035
Evan Chenge3d42322009-02-25 07:04:34 +00003036 // Add to context owner.
3037 DW_Unit->getDie()->AddChild(VariableDie);
Bill Wendling3f500d92009-05-06 21:21:34 +00003038
Evan Chenge3d42322009-02-25 07:04:34 +00003039 // Expose as global. FIXME - need to check external flag.
Bill Wendling0582ae92009-03-13 04:39:26 +00003040 std::string Name;
3041 DW_Unit->AddGlobal(DI_GV.getName(Name), VariableDie);
Evan Chenge3d42322009-02-25 07:04:34 +00003042 return true;
Devang Pateld1ca9252009-01-05 23:03:32 +00003043 }
3044
Devang Patelc4523242009-01-05 23:11:11 +00003045 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patel53cac182009-02-24 00:02:15 +00003046 /// visible global variables. Return true if at least one global DIE is
3047 /// created.
3048 bool ConstructGlobalVariableDIEs() {
Evan Chenge3d42322009-02-25 07:04:34 +00003049 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
3050 if (!Root)
3051 return false;
Devang Patelc4523242009-01-05 23:11:11 +00003052
Evan Chenge3d42322009-02-25 07:04:34 +00003053 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3054 "Malformed global variable descriptor anchor type");
3055 Constant *RootC = cast<Constant>(*Root->use_begin());
3056 assert(RootC->hasNUsesOrMore(1) &&
3057 "Malformed global variable descriptor anchor type");
Devang Patelc4523242009-01-05 23:11:11 +00003058
Evan Chenge3d42322009-02-25 07:04:34 +00003059 bool Result = false;
3060 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3061 UI != UE; ++UI)
3062 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
Bill Wendling3f500d92009-05-06 21:21:34 +00003063 UUI != UUE; ++UUI)
3064 Result |= ConstructGlobalVariableDIE(cast<GlobalVariable>(*UUI));
3065
Evan Chenge3d42322009-02-25 07:04:34 +00003066 return Result;
3067 }
Devang Patelc4523242009-01-05 23:11:11 +00003068
Evan Chenge3d42322009-02-25 07:04:34 +00003069 bool ConstructSubprogram(GlobalVariable *GV) {
3070 DISubprogram SP(GV);
3071 CompileUnit *Unit = MainCU;
3072 if (!Unit)
Chris Lattnere3f6cea2009-05-05 04:55:56 +00003073 Unit = &FindCompileUnit(SP.getCompileUnit());
Devang Patelc4523242009-01-05 23:11:11 +00003074
Evan Chenge3d42322009-02-25 07:04:34 +00003075 // Check for pre-existence.
3076 DIE *&Slot = Unit->getDieMapSlotFor(GV);
3077 if (Slot)
3078 return false;
3079
3080 if (!SP.isDefinition())
3081 // This is a method declaration which will be handled while
3082 // constructing class type.
3083 return false;
3084
3085 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
3086
3087 // Add to map.
3088 Slot = SubprogramDie;
3089 // Add to context owner.
3090 Unit->getDie()->AddChild(SubprogramDie);
3091 // Expose as global.
Bill Wendling0582ae92009-03-13 04:39:26 +00003092 std::string Name;
3093 Unit->AddGlobal(SP.getName(Name), SubprogramDie);
Evan Chenge3d42322009-02-25 07:04:34 +00003094 return true;
Devang Patelc4523242009-01-05 23:11:11 +00003095 }
3096
Devang Patel78eb6ad2009-01-05 23:21:35 +00003097 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patel53cac182009-02-24 00:02:15 +00003098 /// subprograms. Return true if at least one subprogram DIE is created.
3099 bool ConstructSubprograms() {
Evan Chenge3d42322009-02-25 07:04:34 +00003100 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
3101 if (!Root)
3102 return false;
Devang Patel78eb6ad2009-01-05 23:21:35 +00003103
Evan Chenge3d42322009-02-25 07:04:34 +00003104 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3105 "Malformed subprogram descriptor anchor type");
3106 Constant *RootC = cast<Constant>(*Root->use_begin());
3107 assert(RootC->hasNUsesOrMore(1) &&
3108 "Malformed subprogram descriptor anchor type");
Devang Patel78eb6ad2009-01-05 23:21:35 +00003109
Evan Chenge3d42322009-02-25 07:04:34 +00003110 bool Result = false;
3111 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3112 UI != UE; ++UI)
3113 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
Bill Wendling3f500d92009-05-06 21:21:34 +00003114 UUI != UUE; ++UUI)
3115 Result |= ConstructSubprogram(cast<GlobalVariable>(*UUI));
3116
Evan Chenge3d42322009-02-25 07:04:34 +00003117 return Result;
Devang Patel78eb6ad2009-01-05 23:21:35 +00003118 }
3119
Jim Laskey65195462006-10-30 13:35:07 +00003120public:
Jim Laskeyef42a012006-11-02 20:12:39 +00003121 //===--------------------------------------------------------------------===//
3122 // Main entry points.
3123 //
Owen Andersoncb371882008-08-21 00:14:44 +00003124 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00003125 : Dwarf(OS, A, T, "dbg"), MainCU(0),
3126 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
3127 ValuesSet(InitValuesSetSize), Values(), StringPool(), SectionMap(),
3128 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel7d2f9722009-04-15 20:41:31 +00003129 FunctionDbgScope(0), DebugTimer(0) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003130 if (TimePassesIsEnabled)
3131 DebugTimer = new Timer("Dwarf Debug Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00003132 getDwarfTimerGroup());
Jim Laskeyef42a012006-11-02 20:12:39 +00003133 }
Jim Laskey072200c2007-01-29 18:51:14 +00003134 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003135 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3136 delete Values[j];
Bill Wendling163ba3f2009-03-10 21:23:25 +00003137
3138 delete DebugTimer;
Jim Laskeyef42a012006-11-02 20:12:39 +00003139 }
3140
Bill Wendlingc8615e42009-03-10 21:47:45 +00003141 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
3142 /// be emitted.
3143 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
3144
Devang Patel5d315982009-01-06 21:07:30 +00003145 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3146 /// This is inovked by the target AsmPrinter.
Devang Patel3f7833a2009-01-12 23:09:42 +00003147 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003148 if (TimePassesIsEnabled)
3149 DebugTimer->startTimer();
3150
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003151 // Create all the compile unit DIEs.
3152 ConstructCompileUnits();
Devang Patel3f7833a2009-01-12 23:09:42 +00003153
Bill Wendling163ba3f2009-03-10 21:23:25 +00003154 if (CompileUnits.empty()) {
3155 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003156 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003157
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003158 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003159 }
Devang Patel3f7833a2009-01-12 23:09:42 +00003160
Devang Patel53cac182009-02-24 00:02:15 +00003161 // Create DIEs for each of the externally visible global variables.
3162 bool globalDIEs = ConstructGlobalVariableDIEs();
3163
3164 // Create DIEs for each of the externally visible subprograms.
3165 bool subprogramDIEs = ConstructSubprograms();
3166
3167 // If there is not any debug info available for any global variables
3168 // and any subprograms then there is not any debug info to emit.
Bill Wendling163ba3f2009-03-10 21:23:25 +00003169 if (!globalDIEs && !subprogramDIEs) {
3170 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003171 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003172
Devang Patel53cac182009-02-24 00:02:15 +00003173 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003174 }
Devang Patel53cac182009-02-24 00:02:15 +00003175
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003176 MMI = mmi;
3177 shouldEmit = true;
3178 MMI->setDebugInfoAvailability(true);
Devang Patel5d315982009-01-06 21:07:30 +00003179
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003180 // Prime section data.
3181 SectionMap.insert(TAI->getTextSection());
Devang Patel5d315982009-01-06 21:07:30 +00003182
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003183 // Print out .file directives to specify files for .loc directives. These
3184 // are printed out early so that they precede any .loc directives.
3185 if (TAI->hasDotLocAndDotFile()) {
Evan Chenge3d42322009-02-25 07:04:34 +00003186 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
3187 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00003188 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Evan Chenge3d42322009-02-25 07:04:34 +00003189 sys::Path FullPath(getSourceDirectoryName(Id.first));
3190 bool AppendOk =
3191 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003192 assert(AppendOk && "Could not append filename to directory!");
3193 AppendOk = false;
3194 Asm->EmitFile(i, FullPath.toString());
3195 Asm->EOL();
Devang Patel5d315982009-01-06 21:07:30 +00003196 }
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003197 }
Devang Patel5d315982009-01-06 21:07:30 +00003198
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003199 // Emit initial sections
3200 EmitInitial();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003201
3202 if (TimePassesIsEnabled)
3203 DebugTimer->stopTimer();
Devang Patel5d315982009-01-06 21:07:30 +00003204 }
3205
Jim Laskey65195462006-10-30 13:35:07 +00003206 /// BeginModule - Emit all Dwarf sections that should come prior to the
3207 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00003208 void BeginModule(Module *M) {
3209 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00003210 }
3211
Jim Laskey65195462006-10-30 13:35:07 +00003212 /// EndModule - Emit all Dwarf sections that should come after the content.
3213 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003214 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003215 if (!ShouldEmitDwarfDebug())
3216 return;
3217
3218 if (TimePassesIsEnabled)
3219 DebugTimer->startTimer();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003220
Jim Laskeyef42a012006-11-02 20:12:39 +00003221 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003222 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003223 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00003224 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003225 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003226
Jim Laskeyef42a012006-11-02 20:12:39 +00003227 // End text sections.
3228 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003229 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00003230 EmitLabel("section_end", i);
3231 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003232
3233 // Emit common frame information.
3234 EmitCommonDebugFrame();
3235
3236 // Emit function debug frame information
3237 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3238 E = DebugFrames.end(); I != E; ++I)
3239 EmitFunctionDebugFrame(*I);
3240
Jim Laskeyef42a012006-11-02 20:12:39 +00003241 // Compute DIE offsets and sizes.
3242 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003243
Jim Laskeyef42a012006-11-02 20:12:39 +00003244 // Emit all the DIEs into a debug info section
3245 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003246
Jim Laskeyef42a012006-11-02 20:12:39 +00003247 // Corresponding abbreviations into a abbrev section.
3248 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003249
Jim Laskeyef42a012006-11-02 20:12:39 +00003250 // Emit source line correspondence into a debug line section.
3251 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003252
Jim Laskeyef42a012006-11-02 20:12:39 +00003253 // Emit info into a debug pubnames section.
3254 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003255
Jim Laskeyef42a012006-11-02 20:12:39 +00003256 // Emit info into a debug str section.
3257 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003258
Jim Laskeyef42a012006-11-02 20:12:39 +00003259 // Emit info into a debug loc section.
3260 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003261
Jim Laskeyef42a012006-11-02 20:12:39 +00003262 // Emit info into a debug aranges section.
3263 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003264
Jim Laskeyef42a012006-11-02 20:12:39 +00003265 // Emit info into a debug ranges section.
3266 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003267
Jim Laskeyef42a012006-11-02 20:12:39 +00003268 // Emit info into a debug macinfo section.
3269 EmitDebugMacInfo();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003270
Devang Patel0f7fef32009-04-13 17:02:03 +00003271 // Emit inline info.
3272 EmitDebugInlineInfo();
3273
Bill Wendling163ba3f2009-03-10 21:23:25 +00003274 if (TimePassesIsEnabled)
3275 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003276 }
3277
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003278 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00003279 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00003280 void BeginFunction(MachineFunction *MF) {
Bill Wendling1362f972009-03-11 00:03:50 +00003281 this->MF = MF;
3282
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003283 if (!ShouldEmitDwarfDebug()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00003284
Bill Wendling163ba3f2009-03-10 21:23:25 +00003285 if (TimePassesIsEnabled)
3286 DebugTimer->startTimer();
3287
Jim Laskeyef42a012006-11-02 20:12:39 +00003288 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003289 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003290
Jim Laskeyef42a012006-11-02 20:12:39 +00003291 // Assumes in correct section after the entry point.
3292 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00003293
Argyrios Kyrtzidis28f14122009-05-04 19:23:45 +00003294 // Emit label for the implicitly defined dbg.stoppoint at the start of
3295 // the function.
Mike Stumpfe095f32009-05-04 18:40:41 +00003296 DebugLoc FDL = MF->getDefaultDebugLoc();
3297 if (!FDL.isUnknown()) {
3298 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
3299 unsigned LabelID = RecordSourceLine(DLT.Line, DLT.Col,
3300 DICompileUnit(DLT.CompileUnit));
3301 Asm->printLabel(LabelID);
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00003302 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003303
3304 if (TimePassesIsEnabled)
3305 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003306 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003307
Jim Laskey65195462006-10-30 13:35:07 +00003308 /// EndFunction - Gather and emit post-function debug information.
3309 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00003310 void EndFunction(MachineFunction *MF) {
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003311 if (!ShouldEmitDwarfDebug()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003312
Bill Wendling163ba3f2009-03-10 21:23:25 +00003313 if (TimePassesIsEnabled)
3314 DebugTimer->startTimer();
3315
Jim Laskeyb82313f2007-02-01 16:31:34 +00003316 // Define end label for subprogram.
3317 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003318
Jim Laskeyef42a012006-11-02 20:12:39 +00003319 // Get function line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00003320 if (!Lines.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003321 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003322 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00003323 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patelf3ee5142009-01-12 22:54:42 +00003324 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Jim Laskeyef42a012006-11-02 20:12:39 +00003325 // Append the function info to section info.
3326 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patelf3ee5142009-01-12 22:54:42 +00003327 Lines.begin(), Lines.end());
Jim Laskeyef42a012006-11-02 20:12:39 +00003328 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003329
Jim Laskeyef42a012006-11-02 20:12:39 +00003330 // Construct scopes for subprogram.
Devang Patel7d2f9722009-04-15 20:41:31 +00003331 if (FunctionDbgScope)
3332 ConstructFunctionDbgScope(FunctionDbgScope);
Bill Wendlingd751c642008-09-26 00:28:12 +00003333 else
3334 // FIXME: This is wrong. We are essentially getting past a problem with
3335 // debug information not being able to handle unreachable blocks that have
3336 // debug information in them. In particular, those unreachable blocks that
3337 // have "region end" info in them. That situation results in the "root
3338 // scope" not being created. If that's the case, then emit a "default"
3339 // scope, i.e., one that encompasses the whole function. This isn't
3340 // desirable. And a better way of handling this (and all of the debugging
3341 // information) needs to be explored.
Devang Patel7bb89ed2009-01-13 00:20:51 +00003342 ConstructDefaultDbgScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003343
3344 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3345 MMI->getFrameMoves()));
Devang Patel481ff5b2009-01-12 18:48:36 +00003346
3347 // Clear debug info
Devang Patel7d2f9722009-04-15 20:41:31 +00003348 if (FunctionDbgScope) {
3349 delete FunctionDbgScope;
Devang Patel481ff5b2009-01-12 18:48:36 +00003350 DbgScopeMap.clear();
Devang Patel1be3ecc2009-04-15 00:10:26 +00003351 DbgInlinedScopeMap.clear();
3352 InlinedVariableScopes.clear();
Devang Patel7d2f9722009-04-15 20:41:31 +00003353 FunctionDbgScope = NULL;
Devang Patel481ff5b2009-01-12 18:48:36 +00003354 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003355
Bill Wendling163ba3f2009-03-10 21:23:25 +00003356 Lines.clear();
3357
3358 if (TimePassesIsEnabled)
3359 DebugTimer->stopTimer();
3360 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003361
3362 /// RecordSourceLine - Records location information and associates it with a
3363 /// label. Returns a unique label ID used to generate a label and provide
3364 /// correspondence to the source line list.
3365 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003366 if (TimePassesIsEnabled)
3367 DebugTimer->startTimer();
3368
Evan Chenge3d42322009-02-25 07:04:34 +00003369 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003370 assert(Unit && "Unable to find CompileUnit");
Devang Patelccca7fe2009-01-12 19:17:34 +00003371 unsigned ID = MMI->NextLabelID();
3372 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003373
3374 if (TimePassesIsEnabled)
3375 DebugTimer->stopTimer();
3376
Devang Patelccca7fe2009-01-12 19:17:34 +00003377 return ID;
3378 }
3379
3380 /// RecordSourceLine - Records location information and associates it with a
3381 /// label. Returns a unique label ID used to generate a label and provide
3382 /// correspondence to the source line list.
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00003383 unsigned RecordSourceLine(unsigned Line, unsigned Col, DICompileUnit CU) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003384 if (TimePassesIsEnabled)
3385 DebugTimer->startTimer();
3386
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00003387 std::string Dir, Fn;
3388 unsigned Src = GetOrCreateSourceID(CU.getDirectory(Dir),
3389 CU.getFilename(Fn));
Devang Patelccca7fe2009-01-12 19:17:34 +00003390 unsigned ID = MMI->NextLabelID();
3391 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003392
3393 if (TimePassesIsEnabled)
3394 DebugTimer->stopTimer();
3395
Devang Patelccca7fe2009-01-12 19:17:34 +00003396 return ID;
3397 }
3398
Bill Wendlingc8615e42009-03-10 21:47:45 +00003399 /// getRecordSourceLineCount - Return the number of source lines in the debug
3400 /// info.
3401 unsigned getRecordSourceLineCount() const {
Devang Patelccca7fe2009-01-12 19:17:34 +00003402 return Lines.size();
3403 }
3404
Bill Wendlingc8615e42009-03-10 21:47:45 +00003405 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
3406 /// timed. Look up the source id with the given directory and source file
3407 /// names. If none currently exists, create a new id and insert it in the
3408 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
3409 /// well.
Evan Chenge3d42322009-02-25 07:04:34 +00003410 unsigned getOrCreateSourceID(const std::string &DirName,
3411 const std::string &FileName) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003412 if (TimePassesIsEnabled)
3413 DebugTimer->startTimer();
3414
Bill Wendlingc8615e42009-03-10 21:47:45 +00003415 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003416
3417 if (TimePassesIsEnabled)
3418 DebugTimer->stopTimer();
3419
Evan Chenge3d42322009-02-25 07:04:34 +00003420 return SrcId;
Devang Patelccca7fe2009-01-12 19:17:34 +00003421 }
3422
3423 /// RecordRegionStart - Indicate the start of a region.
Devang Patelccca7fe2009-01-12 19:17:34 +00003424 unsigned RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003425 if (TimePassesIsEnabled)
3426 DebugTimer->startTimer();
3427
Devang Patelccca7fe2009-01-12 19:17:34 +00003428 DbgScope *Scope = getOrCreateScope(V);
3429 unsigned ID = MMI->NextLabelID();
3430 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003431
3432 if (TimePassesIsEnabled)
3433 DebugTimer->stopTimer();
3434
Devang Patelccca7fe2009-01-12 19:17:34 +00003435 return ID;
3436 }
3437
3438 /// RecordRegionEnd - Indicate the end of a region.
Dan Gohman9a38e3e2009-05-07 19:46:24 +00003439 unsigned RecordRegionEnd(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003440 if (TimePassesIsEnabled)
3441 DebugTimer->startTimer();
3442
Bill Wendling5b8479c2009-05-07 17:26:14 +00003443 DbgScope *Scope = getOrCreateScope(V);
Dan Gohman9a38e3e2009-05-07 19:46:24 +00003444 unsigned ID = MMI->NextLabelID();
Devang Patelccca7fe2009-01-12 19:17:34 +00003445 Scope->setEndLabelID(ID);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003446
3447 if (TimePassesIsEnabled)
3448 DebugTimer->stopTimer();
3449
Devang Patelccca7fe2009-01-12 19:17:34 +00003450 return ID;
3451 }
3452
3453 /// RecordVariable - Indicate the declaration of a local variable.
Devang Patel1be3ecc2009-04-15 00:10:26 +00003454 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
3455 const MachineInstr *MI) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003456 if (TimePassesIsEnabled)
3457 DebugTimer->startTimer();
3458
Devang Patel0e5200f2009-01-15 18:25:17 +00003459 DIDescriptor Desc(GV);
3460 DbgScope *Scope = NULL;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003461
Devang Patel0e5200f2009-01-15 18:25:17 +00003462 if (Desc.getTag() == DW_TAG_variable) {
3463 // GV is a global variable.
3464 DIGlobalVariable DG(GV);
3465 Scope = getOrCreateScope(DG.getContext().getGV());
3466 } else {
Devang Patel1be3ecc2009-04-15 00:10:26 +00003467 DenseMap<const MachineInstr *, DbgScope *>::iterator
3468 SI = InlinedVariableScopes.find(MI);
Bill Wendling3f500d92009-05-06 21:21:34 +00003469
Devang Patel1be3ecc2009-04-15 00:10:26 +00003470 if (SI != InlinedVariableScopes.end()) {
3471 // or GV is an inlined local variable.
3472 Scope = SI->second;
3473 } else {
Devang Patel0e5200f2009-01-15 18:25:17 +00003474 // or GV is a local variable.
Devang Patel1be3ecc2009-04-15 00:10:26 +00003475 DIVariable DV(GV);
3476 Scope = getOrCreateScope(DV.getContext().getGV());
3477 }
Devang Patel0e5200f2009-01-15 18:25:17 +00003478 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003479
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003480 assert(Scope && "Unable to find variable' scope");
Devang Patel99ec3532009-01-16 19:28:14 +00003481 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelccca7fe2009-01-12 19:17:34 +00003482 Scope->AddVariable(DV);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003483
3484 if (TimePassesIsEnabled)
3485 DebugTimer->stopTimer();
Devang Patelccca7fe2009-01-12 19:17:34 +00003486 }
Devang Patel0f7fef32009-04-13 17:02:03 +00003487
Devang Patel1be3ecc2009-04-15 00:10:26 +00003488 //// RecordInlinedFnStart - Indicate the start of inlined subroutine.
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00003489 unsigned RecordInlinedFnStart(DISubprogram &SP, DICompileUnit CU,
3490 unsigned Line, unsigned Col) {
3491 unsigned LabelID = MMI->NextLabelID();
3492
Devang Patel1be3ecc2009-04-15 00:10:26 +00003493 if (!TAI->doesDwarfUsesInlineInfoSection())
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00003494 return LabelID;
Devang Patel1be3ecc2009-04-15 00:10:26 +00003495
Bill Wendlingd5a63812009-05-01 08:40:06 +00003496 if (TimePassesIsEnabled)
3497 DebugTimer->startTimer();
3498
Dan Gohman9a38e3e2009-05-07 19:46:24 +00003499 std::string Dir, Fn;
3500 unsigned Src = GetOrCreateSourceID(CU.getDirectory(Dir),
3501 CU.getFilename(Fn));
3502 DbgScope *Scope = createInlinedSubroutineScope(SP, Src, Line, Col);
3503 Scope->setStartLabelID(LabelID);
3504 MMI->RecordUsedDbgLabel(LabelID);
Devang Patel1be3ecc2009-04-15 00:10:26 +00003505 GlobalVariable *GV = SP.getGV();
3506
Dan Gohman9a38e3e2009-05-07 19:46:24 +00003507 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3508 SI = DbgInlinedScopeMap.find(GV);
Bill Wendlingef956fc2009-05-01 08:32:14 +00003509
Dan Gohman9a38e3e2009-05-07 19:46:24 +00003510 if (SI == DbgInlinedScopeMap.end())
3511 DbgInlinedScopeMap[GV].push_back(Scope);
3512 else
3513 SI->second.push_back(Scope);
Devang Patel1be3ecc2009-04-15 00:10:26 +00003514
Dan Gohman9a38e3e2009-05-07 19:46:24 +00003515 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
3516 I = InlineInfo.find(GV);
3517 if (I == InlineInfo.end())
3518 InlineInfo[GV].push_back(LabelID);
3519 else
3520 I->second.push_back(LabelID);
Bill Wendlingd5a63812009-05-01 08:40:06 +00003521
3522 if (TimePassesIsEnabled)
3523 DebugTimer->stopTimer();
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00003524
3525 return LabelID;
Devang Patel0f7fef32009-04-13 17:02:03 +00003526 }
Devang Patel1be3ecc2009-04-15 00:10:26 +00003527
3528 /// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
3529 unsigned RecordInlinedFnEnd(DISubprogram &SP) {
3530 if (!TAI->doesDwarfUsesInlineInfoSection())
3531 return 0;
3532
Bill Wendlingd5a63812009-05-01 08:40:06 +00003533 if (TimePassesIsEnabled)
3534 DebugTimer->startTimer();
3535
Devang Patel1be3ecc2009-04-15 00:10:26 +00003536 GlobalVariable *GV = SP.getGV();
3537 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3538 I = DbgInlinedScopeMap.find(GV);
Bill Wendling3f500d92009-05-06 21:21:34 +00003539
Bill Wendlingd5a63812009-05-01 08:40:06 +00003540 if (I == DbgInlinedScopeMap.end()) {
3541 if (TimePassesIsEnabled)
3542 DebugTimer->stopTimer();
3543
Devang Patel1be3ecc2009-04-15 00:10:26 +00003544 return 0;
Bill Wendlingd5a63812009-05-01 08:40:06 +00003545 }
Devang Patel1be3ecc2009-04-15 00:10:26 +00003546
3547 SmallVector<DbgScope *, 2> &Scopes = I->second;
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00003548 assert(!Scopes.empty() && "We should have at least one debug scope!");
Devang Patel1be3ecc2009-04-15 00:10:26 +00003549 DbgScope *Scope = Scopes.back(); Scopes.pop_back();
3550 unsigned ID = MMI->NextLabelID();
Bill Wendling3f500d92009-05-06 21:21:34 +00003551
Devang Patel1be3ecc2009-04-15 00:10:26 +00003552 MMI->RecordUsedDbgLabel(ID);
3553 Scope->setEndLabelID(ID);
Bill Wendlingd5a63812009-05-01 08:40:06 +00003554
3555 if (TimePassesIsEnabled)
3556 DebugTimer->stopTimer();
3557
Devang Patel1be3ecc2009-04-15 00:10:26 +00003558 return ID;
3559 }
3560
3561 /// RecordVariableScope - Record scope for the variable declared by
3562 /// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
3563 /// Record scopes for only inlined subroutine variables. Other
3564 /// variables' scopes are determined during RecordVariable().
3565 void RecordVariableScope(DIVariable &DV, const MachineInstr *DeclareMI) {
Bill Wendlingd5a63812009-05-01 08:40:06 +00003566 if (TimePassesIsEnabled)
3567 DebugTimer->startTimer();
3568
Devang Patel1be3ecc2009-04-15 00:10:26 +00003569 DISubprogram SP(DV.getContext().getGV());
Bill Wendlingd5a63812009-05-01 08:40:06 +00003570
3571 if (SP.isNull()) {
3572 if (TimePassesIsEnabled)
3573 DebugTimer->stopTimer();
3574
Devang Patel1be3ecc2009-04-15 00:10:26 +00003575 return;
Bill Wendlingd5a63812009-05-01 08:40:06 +00003576 }
3577
Devang Patel1be3ecc2009-04-15 00:10:26 +00003578 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3579 I = DbgInlinedScopeMap.find(SP.getGV());
Bill Wendlingd5a63812009-05-01 08:40:06 +00003580 if (I != DbgInlinedScopeMap.end())
3581 InlinedVariableScopes[DeclareMI] = I->second.back();
Devang Patel1be3ecc2009-04-15 00:10:26 +00003582
Bill Wendlingd5a63812009-05-01 08:40:06 +00003583 if (TimePassesIsEnabled)
3584 DebugTimer->stopTimer();
Devang Patel1be3ecc2009-04-15 00:10:26 +00003585 }
Jim Laskey65195462006-10-30 13:35:07 +00003586};
3587
Jim Laskey072200c2007-01-29 18:51:14 +00003588//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003589/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00003590///
3591class DwarfException : public Dwarf {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003592 struct FunctionEHFrameInfo {
3593 std::string FnName;
3594 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003595 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003596 bool hasCalls;
3597 bool hasLandingPads;
3598 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003599 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00003600
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003601 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3602 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003603 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003604 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003605 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003606 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003607 };
3608
3609 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003610
3611 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3612 /// be emitted.
3613 bool shouldEmitTable;
3614
3615 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3616 /// should be emitted.
3617 bool shouldEmitMoves;
3618
3619 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3620 /// should be emitted.
3621 bool shouldEmitTableModule;
3622
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003623 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003624 /// should be emitted.
3625 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00003626
Bill Wendling163ba3f2009-03-10 21:23:25 +00003627 /// ExceptionTimer - Timer for the Dwarf exception writer.
3628 Timer *ExceptionTimer;
3629
Jim Laskey3f09fc22007-02-28 18:38:31 +00003630 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3631 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003632 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003633 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003634 int stackGrowth =
3635 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3636 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003637 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003638
Jim Laskeybacd3042007-02-21 22:48:45 +00003639 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003640 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling722f5f12008-12-24 08:05:17 +00003641
3642 if (!TAI->doesRequireNonLocalEHFrameLabel())
3643 O << TAI->getEHGlobalPrefix();
3644 O << "EH_frame" << Index << ":\n";
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003645 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003646
Jim Laskeybacd3042007-02-21 22:48:45 +00003647 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003648 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003649
Jim Laskeybacd3042007-02-21 22:48:45 +00003650 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003651 EmitDifference("eh_frame_common_end", Index,
3652 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003653 Asm->EOL("Length of Common Information Entry");
3654
Jim Laskeybacd3042007-02-21 22:48:45 +00003655 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003656 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003657 Asm->EmitInt32((int)0);
3658 Asm->EOL("CIE Identifier Tag");
3659 Asm->EmitInt8(DW_CIE_VERSION);
3660 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00003661
Jim Laskeybacd3042007-02-21 22:48:45 +00003662 // The personality presence indicates that language specific information
3663 // will show up in the eh frame.
3664 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00003665 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00003666
Jim Laskeybacd3042007-02-21 22:48:45 +00003667 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003668 Asm->EmitULEB128Bytes(1);
3669 Asm->EOL("CIE Code Alignment Factor");
3670 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00003671 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003672 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00003673 Asm->EOL("CIE Return Address Column");
3674
Jim Laskeybacd3042007-02-21 22:48:45 +00003675 // If there is a personality, we need to indicate the functions location.
3676 if (Personality) {
3677 Asm->EmitULEB128Bytes(7);
3678 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00003679
Duncan Sands671fa972008-05-07 19:11:09 +00003680 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003681 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00003682 Asm->EOL("Personality (pcrel sdata4 indirect)");
3683 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003684 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00003685 Asm->EOL("Personality (pcrel sdata4)");
3686 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00003687
Duncan Sands671fa972008-05-07 19:11:09 +00003688 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00003689 O << TAI->getPersonalityPrefix();
3690 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3691 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00003692 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3693 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00003694 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00003695
Duncan Sands671fa972008-05-07 19:11:09 +00003696 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3697 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003698
Bill Wendlingd60de512009-01-05 22:53:45 +00003699 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3700 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003701 } else {
3702 Asm->EmitULEB128Bytes(1);
3703 Asm->EOL("Augmentation Size");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003704
Bill Wendlingd60de512009-01-05 22:53:45 +00003705 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3706 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003707 }
3708
3709 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003710 std::vector<MachineMove> Moves;
3711 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00003712 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003713
Dale Johannesen21d972a2008-04-30 00:43:29 +00003714 // On Darwin the linker honors the alignment of eh_frame, which means it
3715 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3716 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003717 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003718 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003719 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003720
Jim Laskeybacd3042007-02-21 22:48:45 +00003721 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003722 }
Duncan Sands671fa972008-05-07 19:11:09 +00003723
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003724 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003725 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003726 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003727 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
Chris Lattner266c7bb2009-04-13 05:44:34 +00003728
3729 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
3730 "Should not emit 'available externally' functions at all");
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003731
Jim Laskeybacd3042007-02-21 22:48:45 +00003732 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3733
3734 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003735 // If the corresponding function is static, this should not be
3736 // externally visible.
Rafael Espindolabb46f522009-01-15 20:18:42 +00003737 if (linkage != Function::InternalLinkage &&
Devang Patel2d1768c2009-01-17 08:05:14 +00003738 linkage != Function::PrivateLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003739 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3740 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3741 }
3742
Dale Johannesen038129d2008-01-10 02:03:30 +00003743 // If corresponding function is weak definition, this should be too.
Duncan Sands667d4b82009-03-07 15:45:40 +00003744 if ((linkage == Function::WeakAnyLinkage ||
3745 linkage == Function::WeakODRLinkage ||
3746 linkage == Function::LinkOnceAnyLinkage ||
3747 linkage == Function::LinkOnceODRLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00003748 TAI->getWeakDefDirective())
3749 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3750
3751 // If there are no calls then you can't unwind. This may mean we can
3752 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003753 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00003754 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00003755 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00003756 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00003757 !UnwindTablesMandatory &&
Duncan Sands667d4b82009-03-07 15:45:40 +00003758 ((linkage != Function::WeakAnyLinkage &&
3759 linkage != Function::WeakODRLinkage &&
3760 linkage != Function::LinkOnceAnyLinkage &&
3761 linkage != Function::LinkOnceODRLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00003762 !TAI->getWeakDefDirective() ||
3763 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003764 {
Bill Wendling6e198962007-09-18 01:47:22 +00003765 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003766 // This name has no connection to the function, so it might get
3767 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003768 // dead-stripping unconditionally.
3769 if (const char *UsedDirective = TAI->getUsedDirective())
3770 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00003771 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00003772 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003773
Jim Laskeybacd3042007-02-21 22:48:45 +00003774 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003775 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3776 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003777 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003778
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003779 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003780
Bill Wendling722f5f12008-12-24 08:05:17 +00003781 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3782 PrintRelDirective(true, true);
3783 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3784
3785 if (!TAI->isAbsoluteEHSectionOffsets())
3786 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3787 } else {
3788 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3789 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3790 true, true, false);
3791 }
3792
Jim Laskeybacd3042007-02-21 22:48:45 +00003793 Asm->EOL("FDE CIE offset");
3794
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003795 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003796 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003797 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003798 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003799 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00003800
Jim Laskey3f09fc22007-02-28 18:38:31 +00003801 // If there is a personality and landing pads then point to the language
3802 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003803 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00003804 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00003805 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00003806
3807 if (EHFrameInfo.hasLandingPads)
3808 EmitReference("exception", EHFrameInfo.Number, true, true);
3809 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00003810 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00003811 Asm->EOL("Language Specific Data Area");
3812 } else {
3813 Asm->EmitULEB128Bytes(0);
3814 Asm->EOL("Augmentation size");
3815 }
Duncan Sands671fa972008-05-07 19:11:09 +00003816
Jim Laskeybacd3042007-02-21 22:48:45 +00003817 // Indicate locations of function specific callee saved registers in
3818 // frame.
Devang Patel5aac3d32009-01-17 08:01:33 +00003819 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00003820 true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003821
Dale Johannesen21d972a2008-04-30 00:43:29 +00003822 // On Darwin the linker honors the alignment of eh_frame, which means it
3823 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3824 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003825 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003826 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003827 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003828
3829 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003830 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003831 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003832 // that depends on unused functions (calling undefined externals) being
3833 // dead-stripped to link correctly. Yes, there really is.
3834 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3835 if (const char *UsedDirective = TAI->getUsedDirective())
3836 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3837 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003838 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003839
Duncan Sands57810cd2007-09-05 11:27:52 +00003840 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003841 ///
3842 /// The general organization of the table is complex, but the basic concepts
3843 /// are easy. First there is a header which describes the location and
3844 /// organization of the three components that follow.
3845 /// 1. The landing pad site information describes the range of code covered
3846 /// by the try. In our case it's an accumulation of the ranges covered
3847 /// by the invokes in the try. There is also a reference to the landing
3848 /// pad that handles the exception once processed. Finally an index into
3849 /// the actions table.
3850 /// 2. The action table, in our case, is composed of pairs of type ids
3851 /// and next action offset. Starting with the action index from the
3852 /// landing pad site, each type Id is checked for a match to the current
3853 /// exception. If it matches then the exception and type id are passed
3854 /// on to the landing pad. Otherwise the next action is looked up. This
3855 /// chain is terminated with a next action of zero. If no type id is
3856 /// found the the frame is unwound and handling continues.
3857 /// 3. Type id table contains references to all the C++ typeinfo for all
3858 /// catches in the function. This tables is reversed indexed base 1.
3859
Duncan Sandsb32edb42007-06-06 15:37:31 +00003860 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3861 static unsigned SharedTypeIds(const LandingPadInfo *L,
3862 const LandingPadInfo *R) {
3863 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003864 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003865 unsigned MinSize = LSize < RSize ? LSize : RSize;
3866 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003867
Duncan Sandsb32edb42007-06-06 15:37:31 +00003868 for (; Count != MinSize; ++Count)
3869 if (LIds[Count] != RIds[Count])
3870 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003871
Duncan Sandsb32edb42007-06-06 15:37:31 +00003872 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003873 }
3874
Duncan Sandsb32edb42007-06-06 15:37:31 +00003875 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00003876 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003877 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003878 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003879 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003880
Duncan Sandsb32edb42007-06-06 15:37:31 +00003881 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00003882 if (LIds[i] != RIds[i])
3883 return LIds[i] < RIds[i];
3884
Duncan Sandsb32edb42007-06-06 15:37:31 +00003885 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003886 }
3887
Duncan Sands53c3a332007-05-16 12:12:23 +00003888 struct KeyInfo {
3889 static inline unsigned getEmptyKey() { return -1U; }
3890 static inline unsigned getTombstoneKey() { return -2U; }
3891 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00003892 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00003893 static bool isPod() { return true; }
3894 };
3895
Duncan Sands57810cd2007-09-05 11:27:52 +00003896 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003897 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003898 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003899 int NextAction;
3900 struct ActionEntry *Previous;
3901 };
3902
Duncan Sands57810cd2007-09-05 11:27:52 +00003903 /// PadRange - Structure holding a try-range and the associated landing pad.
3904 struct PadRange {
3905 // The index of the landing pad.
3906 unsigned PadIndex;
3907 // The index of the begin and end labels in the landing pad's label lists.
3908 unsigned RangeIndex;
3909 };
3910
3911 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3912
3913 /// CallSiteEntry - Structure describing an entry in the call-site table.
3914 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00003915 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003916 unsigned BeginLabel; // zero indicates the start of the function.
3917 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00003918 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003919 unsigned PadLabel; // zero indicates that there is no landing pad.
3920 unsigned Action;
3921 };
3922
Jim Laskeybacd3042007-02-21 22:48:45 +00003923 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00003924 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00003925 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00003926 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3927 if (PadInfos.empty()) return;
3928
3929 // Sort the landing pads in order of their type ids. This is used to fold
3930 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00003931 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00003932 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00003933 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00003934 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003935 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3936
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003937 // Negative type ids index into FilterIds, positive type ids index into
3938 // TypeInfos. The value written for a positive type id is just the type
3939 // id itself. For a negative type id, however, the value written is the
3940 // (negative) byte offset of the corresponding FilterIds entry. The byte
3941 // offset is usually equal to the type id, because the FilterIds entries
3942 // are written using a variable width encoding which outputs one byte per
3943 // entry as long as the value written is not too large, but can differ.
3944 // This kind of complication does not occur for positive type ids because
3945 // type infos are output using a fixed width encoding.
3946 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3947 SmallVector<int, 16> FilterOffsets;
3948 FilterOffsets.reserve(FilterIds.size());
3949 int Offset = -1;
3950 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3951 E = FilterIds.end(); I != E; ++I) {
3952 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003953 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003954 }
3955
Duncan Sands57810cd2007-09-05 11:27:52 +00003956 // Compute the actions table and gather the first action index for each
3957 // landing pad site.
3958 SmallVector<ActionEntry, 32> Actions;
3959 SmallVector<unsigned, 64> FirstActions;
3960 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00003961
Duncan Sandsb32edb42007-06-06 15:37:31 +00003962 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00003963 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003964 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003965 const LandingPadInfo *LP = LandingPads[i];
3966 const std::vector<int> &TypeIds = LP->TypeIds;
3967 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003968 unsigned SizeSiteActions = 0;
3969
Duncan Sandsb32edb42007-06-06 15:37:31 +00003970 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00003971 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00003972 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003973
Duncan Sandsb32edb42007-06-06 15:37:31 +00003974 if (NumShared) {
3975 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3976 assert(Actions.size());
3977 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003978 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3979 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003980 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003981 SizeAction -=
3982 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003983 SizeAction += -PrevAction->NextAction;
3984 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003985 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00003986 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003987
Duncan Sandsb32edb42007-06-06 15:37:31 +00003988 // Compute the actions.
3989 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3990 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003991 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3992 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003993 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003994
3995 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003996 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003997 SizeSiteActions += SizeAction;
3998
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003999 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00004000 Actions.push_back(Action);
4001
4002 PrevAction = &Actions.back();
4003 }
4004
4005 // Record the first action of the landing pad site.
4006 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
4007 } // else identical - re-use previous FirstAction
4008
4009 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00004010
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00004011 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00004012 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00004013 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004014
Duncan Sands481dc722007-12-19 07:36:31 +00004015 // Compute the call-site table. The entry for an invoke has a try-range
4016 // containing the call, a non-zero landing pad and an appropriate action.
4017 // The entry for an ordinary call has a try-range containing the call and
4018 // zero for the landing pad and the action. Calls marked 'nounwind' have
4019 // no entry and must not be contained in the try-range of any entry - they
4020 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004021 SmallVector<CallSiteEntry, 64> CallSites;
4022
4023 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00004024 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
4025 // by try-range labels when lowered). Ordinary calls do not, so appropriate
4026 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00004027 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
4028 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00004029 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004030 unsigned BeginLabel = LandingPad->BeginLabels[j];
4031 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
4032 PadRange P = { i, j };
4033 PadMap[BeginLabel] = P;
4034 }
4035 }
4036
Duncan Sands481dc722007-12-19 07:36:31 +00004037 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00004038 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00004039
4040 // Whether there is a potentially throwing instruction (currently this means
4041 // an ordinary call) between the end of the previous try-range and now.
4042 bool SawPotentiallyThrowing = false;
4043
4044 // Whether the last callsite entry was for an invoke.
4045 bool PreviousIsInvoke = false;
4046
Duncan Sands481dc722007-12-19 07:36:31 +00004047 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004048 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
4049 I != E; ++I) {
4050 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
4051 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00004052 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00004053 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00004054 continue;
4055 }
4056
Chris Lattner9e330492007-12-30 20:50:28 +00004057 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00004058 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00004059
Duncan Sands481dc722007-12-19 07:36:31 +00004060 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00004061 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00004062 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004063
Duncan Sands481dc722007-12-19 07:36:31 +00004064 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00004065 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00004066 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00004067 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00004068 continue;
4069
4070 PadRange P = L->second;
4071 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
4072
4073 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
4074 "Inconsistent landing pad map!");
4075
4076 // If some instruction between the previous try-range and this one may
4077 // throw, create a call-site entry with no landing pad for the region
4078 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00004079 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004080 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
4081 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00004082 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004083 }
4084
4085 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00004086 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00004087
Duncan Sands481dc722007-12-19 07:36:31 +00004088 if (LandingPad->LandingPadLabel) {
4089 // This try-range is for an invoke.
4090 CallSiteEntry Site = {BeginLabel, LastLabel,
4091 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00004092
Duncan Sands481dc722007-12-19 07:36:31 +00004093 // Try to merge with the previous call-site.
4094 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00004095 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00004096 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
4097 // Extend the range of the previous entry.
4098 Prev.EndLabel = Site.EndLabel;
4099 continue;
4100 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004101 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004102
Duncan Sands481dc722007-12-19 07:36:31 +00004103 // Otherwise, create a new call-site.
4104 CallSites.push_back(Site);
4105 PreviousIsInvoke = true;
4106 } else {
4107 // Create a gap.
4108 PreviousIsInvoke = false;
4109 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004110 }
4111 }
4112 // If some instruction between the previous try-range and the end of the
4113 // function may throw, create a call-site entry with no landing pad for the
4114 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00004115 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004116 CallSiteEntry Site = {LastLabel, 0, 0, 0};
4117 CallSites.push_back(Site);
4118 }
4119
Jim Laskeybacd3042007-02-21 22:48:45 +00004120 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00004121
4122 // Call sites.
4123 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
4124 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
4125 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
4126 unsigned SizeSites = CallSites.size() * (SiteStartSize +
4127 SiteLengthSize +
4128 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00004129 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004130 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00004131
Duncan Sands671fa972008-05-07 19:11:09 +00004132 // Type infos.
4133 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
4134 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004135
4136 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004137 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004138 SizeSites + SizeActions + SizeTypes;
4139
4140 unsigned TotalSize = sizeof(int8_t) + // LPStart format
4141 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004142 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004143 TypeOffset;
4144
4145 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004146
Jim Laskeybacd3042007-02-21 22:48:45 +00004147 // Begin the exception table.
4148 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng05548eb2008-02-29 19:36:59 +00004149 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesend65b2642008-10-08 21:50:21 +00004150 O << "GCC_except_table" << SubprogramCount << ":\n";
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004151 for (unsigned i = 0; i != SizeAlign; ++i) {
4152 Asm->EmitInt8(0);
4153 Asm->EOL("Padding");
4154 }
Jim Laskeybacd3042007-02-21 22:48:45 +00004155 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004156
Jim Laskeybacd3042007-02-21 22:48:45 +00004157 // Emit the header.
4158 Asm->EmitInt8(DW_EH_PE_omit);
4159 Asm->EOL("LPStart format (DW_EH_PE_omit)");
4160 Asm->EmitInt8(DW_EH_PE_absptr);
4161 Asm->EOL("TType format (DW_EH_PE_absptr)");
4162 Asm->EmitULEB128Bytes(TypeOffset);
4163 Asm->EOL("TType base offset");
4164 Asm->EmitInt8(DW_EH_PE_udata4);
4165 Asm->EOL("Call site format (DW_EH_PE_udata4)");
4166 Asm->EmitULEB128Bytes(SizeSites);
4167 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004168
Duncan Sands57810cd2007-09-05 11:27:52 +00004169 // Emit the landing pad site information.
4170 for (unsigned i = 0; i < CallSites.size(); ++i) {
4171 CallSiteEntry &S = CallSites[i];
4172 const char *BeginTag;
4173 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00004174
Duncan Sands57810cd2007-09-05 11:27:52 +00004175 if (!S.BeginLabel) {
4176 BeginTag = "eh_func_begin";
4177 BeginNumber = SubprogramCount;
4178 } else {
4179 BeginTag = "label";
4180 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00004181 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004182
Duncan Sands57810cd2007-09-05 11:27:52 +00004183 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004184 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004185 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00004186
Duncan Sands57810cd2007-09-05 11:27:52 +00004187 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00004188 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00004189 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004190 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00004191 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004192 }
4193 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004194
Duncan Sands671fa972008-05-07 19:11:09 +00004195 if (!S.PadLabel)
4196 Asm->EmitInt32(0);
4197 else
Duncan Sands57810cd2007-09-05 11:27:52 +00004198 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004199 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004200 Asm->EOL("Landing pad");
4201
4202 Asm->EmitULEB128Bytes(S.Action);
4203 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004204 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004205
Jim Laskeybacd3042007-02-21 22:48:45 +00004206 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004207 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4208 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00004209
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004210 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004211 Asm->EOL("TypeInfo index");
4212 Asm->EmitSLEB128Bytes(Action.NextAction);
4213 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004214 }
4215
4216 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00004217 for (unsigned M = TypeInfos.size(); M; --M) {
4218 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00004219
4220 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00004221
Bill Wendling7d16e852009-04-10 00:12:49 +00004222 if (GV) {
4223 std::string GLN;
4224 O << Asm->getGlobalLinkName(GV, GLN);
4225 } else {
Jim Laskeybacd3042007-02-21 22:48:45 +00004226 O << "0";
Bill Wendling7d16e852009-04-10 00:12:49 +00004227 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004228
Jim Laskeybacd3042007-02-21 22:48:45 +00004229 Asm->EOL("TypeInfo");
4230 }
4231
Duncan Sands73ef58a2007-06-02 16:53:42 +00004232 // Emit the filter typeids.
4233 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4234 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00004235 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00004236 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00004237 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004238
Evan Cheng05548eb2008-02-29 19:36:59 +00004239 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004240 }
4241
Jim Laskey072200c2007-01-29 18:51:14 +00004242public:
4243 //===--------------------------------------------------------------------===//
4244 // Main entry points.
4245 //
Owen Andersoncb371882008-08-21 00:14:44 +00004246 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00004247 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
4248 shouldEmitTableModule(false), shouldEmitMovesModule(false),
4249 ExceptionTimer(0) {
4250 if (TimePassesIsEnabled)
4251 ExceptionTimer = new Timer("Dwarf Exception Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00004252 getDwarfTimerGroup());
Bill Wendling163ba3f2009-03-10 21:23:25 +00004253 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004254
Bill Wendling163ba3f2009-03-10 21:23:25 +00004255 virtual ~DwarfException() {
4256 delete ExceptionTimer;
4257 }
Jim Laskey072200c2007-01-29 18:51:14 +00004258
4259 /// SetModuleInfo - Set machine module information when it's known that pass
4260 /// manager has created it. Set by the target AsmPrinter.
4261 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00004262 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00004263 }
4264
4265 /// BeginModule - Emit all exception information that should come prior to the
4266 /// content.
4267 void BeginModule(Module *M) {
4268 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00004269 }
4270
4271 /// EndModule - Emit all exception information that should come after the
4272 /// content.
4273 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004274 if (TimePassesIsEnabled)
4275 ExceptionTimer->startTimer();
4276
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004277 if (shouldEmitMovesModule || shouldEmitTableModule) {
4278 const std::vector<Function *> Personalities = MMI->getPersonalities();
Evan Chenge3d42322009-02-25 07:04:34 +00004279 for (unsigned i = 0; i < Personalities.size(); ++i)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004280 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00004281
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004282 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4283 E = EHFrames.end(); I != E; ++I)
4284 EmitEHFrame(*I);
4285 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004286
4287 if (TimePassesIsEnabled)
4288 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004289 }
4290
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004291 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00004292 /// emitted immediately after the function entry point.
4293 void BeginFunction(MachineFunction *MF) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004294 if (TimePassesIsEnabled)
4295 ExceptionTimer->startTimer();
4296
Jim Laskey072200c2007-01-29 18:51:14 +00004297 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004298 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004299
Bill Wendling163ba3f2009-03-10 21:23:25 +00004300 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004301 // Map all labels and get rid of any dead landing pads.
4302 MMI->TidyLandingPads();
Bill Wendling163ba3f2009-03-10 21:23:25 +00004303
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004304 // If any landing pads survive, we need an EH table.
4305 if (MMI->getLandingPads().size())
4306 shouldEmitTable = true;
4307
4308 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00004309 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004310 shouldEmitMoves = true;
4311
4312 if (shouldEmitMoves || shouldEmitTable)
4313 // Assumes in correct section after the entry point.
4314 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00004315 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004316
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004317 shouldEmitTableModule |= shouldEmitTable;
4318 shouldEmitMovesModule |= shouldEmitMoves;
Bill Wendling163ba3f2009-03-10 21:23:25 +00004319
4320 if (TimePassesIsEnabled)
4321 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004322 }
4323
4324 /// EndFunction - Gather and emit post-function exception information.
4325 ///
4326 void EndFunction() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004327 if (TimePassesIsEnabled)
4328 ExceptionTimer->startTimer();
4329
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004330 if (shouldEmitMoves || shouldEmitTable) {
4331 EmitLabel("eh_func_end", SubprogramCount);
4332 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00004333
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004334 // Save EH frame information
Bill Wendling7d16e852009-04-10 00:12:49 +00004335 std::string Name;
4336 EHFrames.push_back(
4337 FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF, Name),
4338 SubprogramCount,
4339 MMI->getPersonalityIndex(),
4340 MF->getFrameInfo()->hasCalls(),
4341 !MMI->getLandingPads().empty(),
4342 MMI->getFrameMoves(),
4343 MF->getFunction()));
Bill Wendling163ba3f2009-03-10 21:23:25 +00004344 }
4345
4346 if (TimePassesIsEnabled)
4347 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004348 }
4349};
4350
Jim Laskey0d086af2006-02-27 12:43:29 +00004351} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00004352
4353//===----------------------------------------------------------------------===//
4354
Jim Laskeyd18e2892006-01-20 20:34:06 +00004355/// Emit - Print the abbreviation using the specified Dwarf writer.
4356///
Jim Laskey072200c2007-01-29 18:51:14 +00004357void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004358 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00004359 DD.getAsm()->EmitULEB128Bytes(Tag);
4360 DD.getAsm()->EOL(TagString(Tag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004361
Jim Laskeyd18e2892006-01-20 20:34:06 +00004362 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00004363 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4364 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004365
Jim Laskeyd18e2892006-01-20 20:34:06 +00004366 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00004367 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004368 const DIEAbbrevData &AttrData = Data[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004369
Jim Laskeyd18e2892006-01-20 20:34:06 +00004370 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00004371 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4372 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004373
Jim Laskeyd18e2892006-01-20 20:34:06 +00004374 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00004375 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4376 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00004377 }
4378
4379 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00004380 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4381 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00004382}
4383
4384#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00004385void DIEAbbrev::print(std::ostream &O) {
4386 O << "Abbreviation @"
4387 << std::hex << (intptr_t)this << std::dec
4388 << " "
4389 << TagString(Tag)
4390 << " "
4391 << ChildrenString(ChildrenFlag)
4392 << "\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004393
Jim Laskeya0f3d172006-09-07 22:06:40 +00004394 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4395 O << " "
4396 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004397 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00004398 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004399 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00004400 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00004401}
Bill Wendlinge8156192006-12-07 01:30:32 +00004402void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00004403#endif
4404
4405//===----------------------------------------------------------------------===//
4406
Jim Laskeyef42a012006-11-02 20:12:39 +00004407#ifndef NDEBUG
4408void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004409 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004410}
Jim Laskeyef42a012006-11-02 20:12:39 +00004411#endif
4412
4413//===----------------------------------------------------------------------===//
4414
Jim Laskey063e7652006-01-17 17:31:53 +00004415/// EmitValue - Emit integer of appropriate size.
4416///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004417void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00004418 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00004419 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00004420 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004421 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004422 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004423 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004424 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004425 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004426 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004427 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4428 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4429 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004430 default: assert(0 && "DIE Value form not supported yet"); break;
4431 }
4432}
4433
4434/// SizeOf - Determine size of integer value in bytes.
4435///
Jim Laskey072200c2007-01-29 18:51:14 +00004436unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004437 switch (Form) {
4438 case DW_FORM_flag: // Fall thru
4439 case DW_FORM_ref1: // Fall thru
4440 case DW_FORM_data1: return sizeof(int8_t);
4441 case DW_FORM_ref2: // Fall thru
4442 case DW_FORM_data2: return sizeof(int16_t);
4443 case DW_FORM_ref4: // Fall thru
4444 case DW_FORM_data4: return sizeof(int32_t);
4445 case DW_FORM_ref8: // Fall thru
4446 case DW_FORM_data8: return sizeof(int64_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004447 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4448 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00004449 default: assert(0 && "DIE Value form not supported yet"); break;
4450 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004451 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00004452}
4453
Jim Laskey063e7652006-01-17 17:31:53 +00004454//===----------------------------------------------------------------------===//
4455
4456/// EmitValue - Emit string value.
4457///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004458void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Bill Wendlingdd446322009-03-10 23:57:09 +00004459 DD.getAsm()->EmitString(Str);
Jim Laskey063e7652006-01-17 17:31:53 +00004460}
4461
Jim Laskey063e7652006-01-17 17:31:53 +00004462//===----------------------------------------------------------------------===//
4463
4464/// EmitValue - Emit label value.
4465///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004466void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004467 bool IsSmall = Form == DW_FORM_data4;
4468 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004469}
4470
4471/// SizeOf - Determine size of label value in bytes.
4472///
Jim Laskey072200c2007-01-29 18:51:14 +00004473unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004474 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004475 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004476}
Jim Laskeyef42a012006-11-02 20:12:39 +00004477
Jim Laskey063e7652006-01-17 17:31:53 +00004478//===----------------------------------------------------------------------===//
4479
Jim Laskeyd18e2892006-01-20 20:34:06 +00004480/// EmitValue - Emit label value.
4481///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004482void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004483 bool IsSmall = Form == DW_FORM_data4;
4484 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00004485}
4486
4487/// SizeOf - Determine size of label value in bytes.
4488///
Jim Laskey072200c2007-01-29 18:51:14 +00004489unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004490 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004491 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00004492}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004493
Jim Laskeyd18e2892006-01-20 20:34:06 +00004494//===----------------------------------------------------------------------===//
4495
Jim Laskey063e7652006-01-17 17:31:53 +00004496/// EmitValue - Emit delta value.
4497///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004498void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4499 bool IsSmall = Form == DW_FORM_data4;
4500 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4501 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4502}
4503
4504/// SizeOf - Determine size of delta value in bytes.
4505///
4506unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4507 if (Form == DW_FORM_data4) return 4;
4508 return DD.getTargetData()->getPointerSize();
4509}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004510
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004511//===----------------------------------------------------------------------===//
4512
4513/// EmitValue - Emit delta value.
4514///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004515void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004516 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00004517 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004518}
4519
4520/// SizeOf - Determine size of delta value in bytes.
4521///
Jim Laskey072200c2007-01-29 18:51:14 +00004522unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004523 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004524 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004525}
4526
4527//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004528
Jim Laskeyb8509c52006-03-23 18:07:55 +00004529/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004530///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004531void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004532 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00004533}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004534
Jim Laskeyd18e2892006-01-20 20:34:06 +00004535//===----------------------------------------------------------------------===//
4536
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004537/// ComputeSize - calculate the size of the block.
4538///
Jim Laskey072200c2007-01-29 18:51:14 +00004539unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00004540 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00004541 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004542
Jim Laskeyef42a012006-11-02 20:12:39 +00004543 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00004544 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00004545 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004546 }
4547 return Size;
4548}
4549
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004550/// EmitValue - Emit block data.
4551///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004552void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004553 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004554 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4555 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4556 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4557 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004558 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004559 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004560
Owen Anderson873e1b52008-06-24 21:44:59 +00004561 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00004562
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004563 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00004564 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00004565 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004566 }
4567}
4568
4569/// SizeOf - Determine size of block data in bytes.
4570///
Jim Laskey072200c2007-01-29 18:51:14 +00004571unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004572 switch (Form) {
4573 case DW_FORM_block1: return Size + sizeof(int8_t);
4574 case DW_FORM_block2: return Size + sizeof(int16_t);
4575 case DW_FORM_block4: return Size + sizeof(int32_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004576 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004577 default: assert(0 && "Improper form for block"); break;
4578 }
4579 return 0;
4580}
4581
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004582//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004583/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00004584
4585DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00004586 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00004587 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00004588}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004589
Jim Laskeyb8509c52006-03-23 18:07:55 +00004590/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4591///
4592void DIE::AddSiblingOffset() {
4593 DIEInteger *DI = new DIEInteger(0);
4594 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00004595 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00004596}
4597
Jim Laskeyef42a012006-11-02 20:12:39 +00004598/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004599///
Jim Laskeyef42a012006-11-02 20:12:39 +00004600void DIE::Profile(FoldingSetNodeID &ID) {
4601 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004602
Jim Laskeyef42a012006-11-02 20:12:39 +00004603 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4604 ID.AddPointer(Children[i]);
4605
4606 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4607 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00004608}
Jim Laskeyef42a012006-11-02 20:12:39 +00004609
4610#ifndef NDEBUG
4611void DIE::print(std::ostream &O, unsigned IncIndent) {
4612 static unsigned IndentCount = 0;
4613 IndentCount += IncIndent;
4614 const std::string Indent(IndentCount, ' ');
4615 bool isBlock = Abbrev.getTag() == 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004616
Jim Laskeyef42a012006-11-02 20:12:39 +00004617 if (!isBlock) {
4618 O << Indent
4619 << "Die: "
4620 << "0x" << std::hex << (intptr_t)this << std::dec
4621 << ", Offset: " << Offset
4622 << ", Size: " << Size
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004623 << "\n";
4624
Jim Laskeyef42a012006-11-02 20:12:39 +00004625 O << Indent
4626 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00004627 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00004628 << ChildrenString(Abbrev.getChildrenFlag());
4629 } else {
4630 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00004631 }
4632 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00004633
Owen Anderson873e1b52008-06-24 21:44:59 +00004634 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004635
Jim Laskeyef42a012006-11-02 20:12:39 +00004636 IndentCount += 2;
4637 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4638 O << Indent;
Bill Wendling4e974012008-07-22 00:28:47 +00004639
4640 if (!isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +00004641 O << AttributeString(Data[i].getAttribute());
Bill Wendling4e974012008-07-22 00:28:47 +00004642 else
Jim Laskeyef42a012006-11-02 20:12:39 +00004643 O << "Blk[" << i << "]";
Bill Wendling4e974012008-07-22 00:28:47 +00004644
Jim Laskeyef42a012006-11-02 20:12:39 +00004645 O << " "
4646 << FormEncodingString(Data[i].getForm())
4647 << " ";
4648 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00004649 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00004650 }
Jim Laskeyef42a012006-11-02 20:12:39 +00004651 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00004652
Jim Laskeyef42a012006-11-02 20:12:39 +00004653 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4654 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00004655 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004656
Jim Laskeyef42a012006-11-02 20:12:39 +00004657 if (!isBlock) O << "\n";
4658 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00004659}
4660
Jim Laskeyef42a012006-11-02 20:12:39 +00004661void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004662 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00004663}
Jim Laskeybd761842006-02-27 17:27:12 +00004664#endif
Jim Laskey65195462006-10-30 13:35:07 +00004665
4666//===----------------------------------------------------------------------===//
4667/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00004668///
Jim Laskey65195462006-10-30 13:35:07 +00004669
Bill Wendling91b8b802009-03-10 20:41:52 +00004670DwarfWriter::DwarfWriter()
Bill Wendling163ba3f2009-03-10 21:23:25 +00004671 : ImmutablePass(&ID), DD(0), DE(0) {}
Jim Laskey65195462006-10-30 13:35:07 +00004672
4673DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00004674 delete DE;
4675 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00004676}
4677
Jim Laskey65195462006-10-30 13:35:07 +00004678/// BeginModule - Emit all Dwarf sections that should come prior to the
4679/// content.
Devang Pateleb3fc282009-01-08 23:40:34 +00004680void DwarfWriter::BeginModule(Module *M,
4681 MachineModuleInfo *MMI,
4682 raw_ostream &OS, AsmPrinter *A,
4683 const TargetAsmInfo *T) {
4684 DE = new DwarfException(OS, A, T);
4685 DD = new DwarfDebug(OS, A, T);
Jim Laskey072200c2007-01-29 18:51:14 +00004686 DE->BeginModule(M);
4687 DD->BeginModule(M);
Devang Patel7bb89ed2009-01-13 00:20:51 +00004688 DD->SetDebugInfo(MMI);
Devang Pateleb3fc282009-01-08 23:40:34 +00004689 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00004690}
4691
4692/// EndModule - Emit all Dwarf sections that should come after the content.
4693///
4694void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00004695 DE->EndModule();
4696 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00004697}
4698
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004699/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00004700/// emitted immediately after the function entry point.
4701void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00004702 DE->BeginFunction(MF);
4703 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00004704}
4705
4706/// EndFunction - Gather and emit post-function debug information.
4707///
Bill Wendlingd751c642008-09-26 00:28:12 +00004708void DwarfWriter::EndFunction(MachineFunction *MF) {
4709 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004710 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004711
Bill Wendlingc91d0b92008-07-22 00:53:37 +00004712 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00004713 // Clear function debug information.
4714 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00004715}
Devang Patelccca7fe2009-01-12 19:17:34 +00004716
4717/// RecordSourceLine - Records location information and associates it with a
4718/// label. Returns a unique label ID used to generate a label and provide
4719/// correspondence to the source line list.
4720unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00004721 DICompileUnit CU) {
4722 return DD->RecordSourceLine(Line, Col, CU);
Devang Patelccca7fe2009-01-12 19:17:34 +00004723}
4724
4725/// RecordRegionStart - Indicate the start of a region.
4726unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004727 return DD->RecordRegionStart(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004728}
4729
4730/// RecordRegionEnd - Indicate the end of a region.
Dan Gohman9a38e3e2009-05-07 19:46:24 +00004731unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
4732 return DD->RecordRegionEnd(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004733}
4734
4735/// getRecordSourceLineCount - Count source lines.
4736unsigned DwarfWriter::getRecordSourceLineCount() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004737 return DD->getRecordSourceLineCount();
Devang Patelccca7fe2009-01-12 19:17:34 +00004738}
Devang Patelbb8c5952009-01-13 21:25:00 +00004739
Devang Patelc48c5502009-01-13 21:44:10 +00004740/// RecordVariable - Indicate the declaration of a local variable.
4741///
Devang Patel1be3ecc2009-04-15 00:10:26 +00004742void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
4743 const MachineInstr *MI) {
4744 DD->RecordVariable(GV, FrameIndex, MI);
Devang Patelc48c5502009-01-13 21:44:10 +00004745}
Devang Patelbbdc8202009-01-13 23:54:55 +00004746
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004747/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
4748/// be emitted.
4749bool DwarfWriter::ShouldEmitDwarfDebug() const {
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +00004750 return DD && DD->ShouldEmitDwarfDebug();
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004751}
Devang Patel0f7fef32009-04-13 17:02:03 +00004752
Devang Patel1be3ecc2009-04-15 00:10:26 +00004753//// RecordInlinedFnStart - Global variable GV is inlined at the location marked
Devang Patel0f7fef32009-04-13 17:02:03 +00004754//// by LabelID label.
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00004755unsigned DwarfWriter::RecordInlinedFnStart(DISubprogram SP, DICompileUnit CU,
4756 unsigned Line, unsigned Col) {
4757 return DD->RecordInlinedFnStart(SP, CU, Line, Col);
Devang Patel0f7fef32009-04-13 17:02:03 +00004758}
4759
Devang Patel1be3ecc2009-04-15 00:10:26 +00004760/// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00004761unsigned DwarfWriter::RecordInlinedFnEnd(DISubprogram SP) {
Devang Patel1be3ecc2009-04-15 00:10:26 +00004762 return DD->RecordInlinedFnEnd(SP);
4763}
4764
4765/// RecordVariableScope - Record scope for the variable declared by
4766/// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
4767void DwarfWriter::RecordVariableScope(DIVariable &DV,
4768 const MachineInstr *DeclareMI) {
4769 DD->RecordVariableScope(DV, DeclareMI);
4770}