blob: 1409feb98d53856227df5f766ade37e88a7a22ed [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 Wendlinge688faf2009-05-08 21:03:15 +00001292 /// CompileUnitOffsets - A vector of the offsets of the compile units. This is
1293 /// used when calculating the "origin" of a concrete instance of an inlined
1294 /// function.
1295 DenseMap<CompileUnit *, unsigned> CompileUnitOffsets;
1296
Bill Wendling163ba3f2009-03-10 21:23:25 +00001297 /// DebugTimer - Timer for the Dwarf debug writer.
1298 Timer *DebugTimer;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001299
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001300 struct FunctionDebugFrameInfo {
1301 unsigned Number;
1302 std::vector<MachineMove> Moves;
1303
1304 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001305 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001306 };
1307
1308 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001309
Bill Wendling163ba3f2009-03-10 21:23:25 +00001310private:
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001311 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
Bill Wendlingc8615e42009-03-10 21:47:45 +00001312 /// maps to the source id. Source id starts at 1.
1313 std::pair<unsigned, unsigned>
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001314 getSourceDirectoryAndFileIds(unsigned SId) const {
Bill Wendlingc8615e42009-03-10 21:47:45 +00001315 return SourceIds[SId-1];
1316 }
1317
1318 /// getNumSourceDirectories - Return the number of source directories in the
1319 /// debug info.
1320 unsigned getNumSourceDirectories() const {
1321 return DirectoryNames.size();
1322 }
1323
1324 /// getSourceDirectoryName - Return the name of the directory corresponding
1325 /// to the id.
1326 const std::string &getSourceDirectoryName(unsigned Id) const {
1327 return DirectoryNames[Id - 1];
1328 }
1329
1330 /// getSourceFileName - Return the name of the source file corresponding
1331 /// to the id.
1332 const std::string &getSourceFileName(unsigned Id) const {
1333 return SourceFileNames[Id - 1];
1334 }
1335
1336 /// getNumSourceIds - Return the number of unique source ids.
Bill Wendlingc8615e42009-03-10 21:47:45 +00001337 unsigned getNumSourceIds() const {
1338 return SourceIds.size();
1339 }
1340
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001341 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001342 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001343 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1344 // Profile the node so that we can make it unique.
1345 FoldingSetNodeID ID;
1346 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001347
Jim Laskeyef42a012006-11-02 20:12:39 +00001348 // Check the set for priors.
1349 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001350
Jim Laskeyef42a012006-11-02 20:12:39 +00001351 // If it's newly added.
1352 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001353 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +00001354 Abbreviations.push_back(&Abbrev);
1355 // Assign the vector position + 1 as its number.
1356 Abbrev.setNumber(Abbreviations.size());
1357 } else {
1358 // Assign existing abbreviation number.
1359 Abbrev.setNumber(InSet->getNumber());
1360 }
1361 }
1362
Jim Laskey65195462006-10-30 13:35:07 +00001363 /// NewString - Add a string to the constant pool and returns a label.
1364 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001365 DWLabel NewString(const std::string &String) {
1366 unsigned StringID = StringPool.insert(String);
1367 return DWLabel("string", StringID);
1368 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001369
Jim Laskeyef42a012006-11-02 20:12:39 +00001370 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1371 /// entry.
1372 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1373 DIEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001374
Jim Laskeyef42a012006-11-02 20:12:39 +00001375 if (Entry) {
1376 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001377 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001378 void *Where;
1379 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001380
Jim Laskeyf6733882006-11-02 21:48:18 +00001381 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001382
Jim Laskeyef42a012006-11-02 20:12:39 +00001383 Value = new DIEntry(Entry);
1384 ValuesSet.InsertNode(Value, Where);
1385 } else {
1386 Value = new DIEntry(Entry);
1387 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001388
Jim Laskeyef42a012006-11-02 20:12:39 +00001389 Values.push_back(Value);
1390 return Value;
1391 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001392
Jim Laskeyef42a012006-11-02 20:12:39 +00001393 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1394 ///
1395 void SetDIEntry(DIEntry *Value, DIE *Entry) {
Bill Wendlingdd446322009-03-10 23:57:09 +00001396 Value->setEntry(Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001397 // Add to values set if not already there. If it is, we merely have a
1398 // duplicate in the values list (no harm.)
1399 ValuesSet.GetOrInsertNode(Value);
1400 }
1401
1402 /// AddUInt - Add an unsigned integer attribute data and value.
1403 ///
1404 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1405 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1406
1407 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001408 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001409 void *Where;
1410 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1411 if (!Value) {
1412 Value = new DIEInteger(Integer);
1413 ValuesSet.InsertNode(Value, Where);
1414 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001415 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001416
Jim Laskeyef42a012006-11-02 20:12:39 +00001417 Die->AddValue(Attribute, Form, Value);
1418 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001419
Jim Laskeyef42a012006-11-02 20:12:39 +00001420 /// AddSInt - Add an signed integer attribute data and value.
1421 ///
1422 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1423 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1424
1425 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001426 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001427 void *Where;
1428 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1429 if (!Value) {
1430 Value = new DIEInteger(Integer);
1431 ValuesSet.InsertNode(Value, Where);
1432 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001433 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001434
Jim Laskeyef42a012006-11-02 20:12:39 +00001435 Die->AddValue(Attribute, Form, Value);
1436 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001437
Evan Chenge3d42322009-02-25 07:04:34 +00001438 /// AddString - Add a string attribute data and value.
Jim Laskeyef42a012006-11-02 20:12:39 +00001439 ///
1440 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1441 const std::string &String) {
1442 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001443 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001444 void *Where;
1445 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1446 if (!Value) {
1447 Value = new DIEString(String);
1448 ValuesSet.InsertNode(Value, Where);
1449 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001450 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001451
Jim Laskeyef42a012006-11-02 20:12:39 +00001452 Die->AddValue(Attribute, Form, Value);
1453 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001454
Jim Laskeyef42a012006-11-02 20:12:39 +00001455 /// AddLabel - Add a Dwarf label attribute data and value.
1456 ///
1457 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1458 const DWLabel &Label) {
1459 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001460 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001461 void *Where;
1462 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1463 if (!Value) {
1464 Value = new DIEDwarfLabel(Label);
1465 ValuesSet.InsertNode(Value, Where);
1466 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001467 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001468
Jim Laskeyef42a012006-11-02 20:12:39 +00001469 Die->AddValue(Attribute, Form, Value);
1470 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001471
Jim Laskeyef42a012006-11-02 20:12:39 +00001472 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1473 ///
1474 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1475 const std::string &Label) {
1476 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001477 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001478 void *Where;
1479 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1480 if (!Value) {
1481 Value = new DIEObjectLabel(Label);
1482 ValuesSet.InsertNode(Value, Where);
1483 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001484 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001485
Jim Laskeyef42a012006-11-02 20:12:39 +00001486 Die->AddValue(Attribute, Form, Value);
1487 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001488
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001489 /// AddSectionOffset - Add a section offset label attribute data and value.
1490 ///
1491 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1492 const DWLabel &Label, const DWLabel &Section,
1493 bool isEH = false, bool useSet = true) {
1494 FoldingSetNodeID ID;
1495 DIESectionOffset::Profile(ID, Label, Section);
1496 void *Where;
1497 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1498 if (!Value) {
1499 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1500 ValuesSet.InsertNode(Value, Where);
1501 Values.push_back(Value);
1502 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001503
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001504 Die->AddValue(Attribute, Form, Value);
1505 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001506
Jim Laskeyef42a012006-11-02 20:12:39 +00001507 /// AddDelta - Add a label delta attribute data and value.
1508 ///
1509 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
Devang Patel1be3ecc2009-04-15 00:10:26 +00001510 const DWLabel &Hi, const DWLabel &Lo) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001511 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001512 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001513 void *Where;
1514 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1515 if (!Value) {
1516 Value = new DIEDelta(Hi, Lo);
1517 ValuesSet.InsertNode(Value, Where);
1518 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001519 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001520
Jim Laskeyef42a012006-11-02 20:12:39 +00001521 Die->AddValue(Attribute, Form, Value);
1522 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001523
Jim Laskeyef42a012006-11-02 20:12:39 +00001524 /// AddDIEntry - Add a DIE attribute data and value.
1525 ///
1526 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1527 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1528 }
1529
1530 /// AddBlock - Add block data.
1531 ///
1532 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1533 Block->ComputeSize(*this);
1534 FoldingSetNodeID ID;
1535 Block->Profile(ID);
1536 void *Where;
1537 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1538 if (!Value) {
1539 Value = Block;
1540 ValuesSet.InsertNode(Value, Where);
1541 Values.push_back(Value);
1542 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001543 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001544 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001545 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001546 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001547
Jim Laskeyef42a012006-11-02 20:12:39 +00001548 Die->AddValue(Attribute, Block->BestForm(), Value);
1549 }
1550
Jim Laskey65195462006-10-30 13:35:07 +00001551 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001552 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001553 void AddSourceLine(DIE *Die, const DIVariable *V) {
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001554 // If there is no compile unit specified, don't add a line #.
1555 if (V->getCompileUnit().isNull())
1556 return;
1557
Devang Patel7a6e5a32009-01-08 02:33:41 +00001558 unsigned Line = V->getLineNumber();
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001559 unsigned FileID = FindCompileUnit(V->getCompileUnit()).getID();
1560 assert(FileID && "Invalid file id");
Devang Patel7a6e5a32009-01-08 02:33:41 +00001561 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1562 AddUInt(Die, DW_AT_decl_line, 0, Line);
1563 }
1564
1565 /// AddSourceLine - Add location information to specified debug information
1566 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001567 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001568 // If there is no compile unit specified, don't add a line #.
1569 if (G->getCompileUnit().isNull())
1570 return;
Devang Patel8526cc02009-01-05 22:35:52 +00001571 unsigned Line = G->getLineNumber();
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001572 unsigned FileID = FindCompileUnit(G->getCompileUnit()).getID();
1573 assert(FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001574 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1575 AddUInt(Die, DW_AT_decl_line, 0, Line);
1576 }
1577
Devang Patel99ec3532009-01-16 19:28:14 +00001578 void AddSourceLine(DIE *Die, const DIType *Ty) {
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001579 // If there is no compile unit specified, don't add a line #.
Devang Pateldd9db662009-01-30 18:20:31 +00001580 DICompileUnit CU = Ty->getCompileUnit();
1581 if (CU.isNull())
1582 return;
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001583
1584 unsigned Line = Ty->getLineNumber();
1585 unsigned FileID = FindCompileUnit(CU).getID();
1586 assert(FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001587 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1588 AddUInt(Die, DW_AT_decl_line, 0, Line);
1589 }
1590
Jim Laskey65195462006-10-30 13:35:07 +00001591 /// AddAddress - Add an address attribute to a die based on the location
1592 /// provided.
1593 void AddAddress(DIE *Die, unsigned Attribute,
Devang Patel1be3ecc2009-04-15 00:10:26 +00001594 const MachineLocation &Location) {
Dan Gohmand735b802008-10-03 15:45:36 +00001595 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001596 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001597
Dan Gohmand735b802008-10-03 15:45:36 +00001598 if (Location.isReg()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001599 if (Reg < 32) {
1600 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1601 } else {
1602 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1603 AddUInt(Block, 0, DW_FORM_udata, Reg);
1604 }
1605 } else {
1606 if (Reg < 32) {
1607 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1608 } else {
1609 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1610 AddUInt(Block, 0, DW_FORM_udata, Reg);
1611 }
1612 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1613 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001614
Jim Laskeyef42a012006-11-02 20:12:39 +00001615 AddBlock(Die, Attribute, 0, Block);
1616 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001617
Jim Laskeyef42a012006-11-02 20:12:39 +00001618 /// AddType - Add a new type attribute to the specified entity.
Devang Patel48d190f2009-01-05 21:47:57 +00001619 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel80303aa2009-01-23 19:13:31 +00001620 if (Ty.isNull())
Devang Patel48d190f2009-01-05 21:47:57 +00001621 return;
Devang Patel48d190f2009-01-05 21:47:57 +00001622
1623 // Check for pre-existence.
1624 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1625 // If it exists then use the existing value.
1626 if (Slot) {
1627 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1628 return;
1629 }
1630
1631 // Set up proxy.
1632 Slot = NewDIEntry();
1633
1634 // Construct type.
1635 DIE Buffer(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001636 if (Ty.isBasicType(Ty.getTag()))
1637 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1638 else if (Ty.isDerivedType(Ty.getTag()))
1639 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1640 else {
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001641 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelf193ff02009-01-15 19:26:23 +00001642 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1643 }
1644
Devang Patel7009d242009-01-27 23:22:55 +00001645 // Add debug information entry to entity and appropriate context.
1646 DIE *Die = NULL;
1647 DIDescriptor Context = Ty.getContext();
1648 if (!Context.isNull())
1649 Die = DW_Unit->getDieMapSlotFor(Context.getGV());
1650
1651 if (Die) {
1652 DIE *Child = new DIE(Buffer);
1653 Die->AddChild(Child);
1654 Buffer.Detach();
1655 SetDIEntry(Slot, Child);
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001656 } else {
Devang Patel7009d242009-01-27 23:22:55 +00001657 Die = DW_Unit->AddDie(Buffer);
1658 SetDIEntry(Slot, Die);
1659 }
1660
Devang Patel48d190f2009-01-05 21:47:57 +00001661 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1662 }
1663
Devang Patele5202732009-01-05 19:07:53 +00001664 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1665 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001666 DIBasicType BTy) {
Bill Wendling0582ae92009-03-13 04:39:26 +00001667
Devang Patel08f053f2009-01-05 17:57:47 +00001668 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001669 std::string Name;
1670 BTy.getName(Name);
Devang Patel08f053f2009-01-05 17:57:47 +00001671 Buffer.setTag(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001672 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patel08f053f2009-01-05 17:57:47 +00001673 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001674 if (!Name.empty())
Devang Patel08f053f2009-01-05 17:57:47 +00001675 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001676 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel08f053f2009-01-05 17:57:47 +00001677 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1678 }
1679
Devang Patele5202732009-01-05 19:07:53 +00001680 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1681 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001682 DIDerivedType DTy) {
Bill Wendling0582ae92009-03-13 04:39:26 +00001683
Devang Patel08f053f2009-01-05 17:57:47 +00001684 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001685 std::string Name;
1686 DTy.getName(Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001687 uint64_t Size = DTy.getSizeInBits() >> 3;
1688 unsigned Tag = DTy.getTag();
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001689
Devang Patel08f053f2009-01-05 17:57:47 +00001690 // FIXME - Workaround for templates.
1691 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1692
1693 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001694
Devang Patel08f053f2009-01-05 17:57:47 +00001695 // Map to main type, void will not have a type.
Devang Patelf193ff02009-01-15 19:26:23 +00001696 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00001697 AddType(DW_Unit, &Buffer, FromTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001698
1699 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001700 if (!Name.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00001701 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel08f053f2009-01-05 17:57:47 +00001702
1703 // Add size if non-zero (derived types might be zero-sized.)
1704 if (Size)
1705 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1706
1707 // Add source line info if available and TyDesc is not a forward
1708 // declaration.
Devang Patelad165be2009-01-27 00:45:04 +00001709 if (!DTy.isForwardDecl())
1710 AddSourceLine(&Buffer, &DTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001711 }
1712
Devang Patelf4215332009-01-05 19:55:51 +00001713 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1714 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001715 DICompositeType CTy) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001716 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001717 std::string Name;
1718 CTy.getName(Name);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001719
Devang Patelf193ff02009-01-15 19:26:23 +00001720 uint64_t Size = CTy.getSizeInBits() >> 3;
1721 unsigned Tag = CTy.getTag();
Devang Patel49f38cb2009-01-23 01:19:09 +00001722 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001723
Devang Patelf4215332009-01-05 19:55:51 +00001724 switch (Tag) {
1725 case DW_TAG_vector_type:
1726 case DW_TAG_array_type:
Devang Patelf193ff02009-01-15 19:26:23 +00001727 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001728 break;
Devang Pateleab4a2e2009-01-20 18:35:14 +00001729 case DW_TAG_enumeration_type:
1730 {
1731 DIArray Elements = CTy.getTypeArray();
1732 // Add enumerators to enumeration type.
1733 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1734 DIE *ElemDie = NULL;
1735 DIEnumerator Enum(Elements.getElement(i).getGV());
1736 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
1737 Buffer.AddChild(ElemDie);
1738 }
1739 }
1740 break;
Devang Patelf4215332009-01-05 19:55:51 +00001741 case DW_TAG_subroutine_type:
1742 {
Devang Patelf4215332009-01-05 19:55:51 +00001743 // Add return type.
Bill Wendling3f500d92009-05-06 21:21:34 +00001744 DIArray Elements = CTy.getTypeArray();
Devang Patel48d190f2009-01-05 21:47:57 +00001745 DIDescriptor RTy = Elements.getElement(0);
Devang Patelf193ff02009-01-15 19:26:23 +00001746 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel48d190f2009-01-05 21:47:57 +00001747
Bill Wendling3f500d92009-05-06 21:21:34 +00001748 // Add prototype flag.
1749 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1750
Devang Patelf4215332009-01-05 19:55:51 +00001751 // Add arguments.
1752 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1753 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00001754 DIDescriptor Ty = Elements.getElement(i);
Devang Patel7ab24502009-01-17 06:57:25 +00001755 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patelf4215332009-01-05 19:55:51 +00001756 Buffer.AddChild(Arg);
1757 }
1758 }
1759 break;
1760 case DW_TAG_structure_type:
1761 case DW_TAG_union_type:
Devang Patel86bda412009-03-25 00:28:40 +00001762 case DW_TAG_class_type:
Devang Patelf4215332009-01-05 19:55:51 +00001763 {
1764 // Add elements to structure type.
Devang Patelf193ff02009-01-15 19:26:23 +00001765 DIArray Elements = CTy.getTypeArray();
Devang Patel153745c2009-01-16 00:50:53 +00001766
1767 // A forward struct declared type may not have elements available.
1768 if (Elements.isNull())
1769 break;
1770
Devang Patelf4215332009-01-05 19:55:51 +00001771 // Add elements to structure type.
1772 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1773 DIDescriptor Element = Elements.getElement(i);
Devang Patel5aac3d32009-01-17 08:01:33 +00001774 DIE *ElemDie = NULL;
Devang Patelf193ff02009-01-15 19:26:23 +00001775 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel2d1768c2009-01-17 08:05:14 +00001776 ElemDie = CreateSubprogramDIE(DW_Unit,
1777 DISubprogram(Element.getGV()));
Dan Gohman9a38e3e2009-05-07 19:46:24 +00001778 else if (Element.getTag() == dwarf::DW_TAG_variable) // ???
Devang Patel5aac3d32009-01-17 08:01:33 +00001779 ElemDie = CreateGlobalVariableDIE(DW_Unit,
1780 DIGlobalVariable(Element.getGV()));
Devang Patel2be58932009-01-20 21:02:02 +00001781 else
1782 ElemDie = CreateMemberDIE(DW_Unit,
1783 DIDerivedType(Element.getGV()));
Devang Patel2d1768c2009-01-17 08:05:14 +00001784 Buffer.AddChild(ElemDie);
Devang Patelf4215332009-01-05 19:55:51 +00001785 }
Devang Patel13319ce2009-02-17 22:43:44 +00001786 unsigned RLang = CTy.getRunTimeLang();
1787 if (RLang)
1788 AddUInt(&Buffer, DW_AT_APPLE_runtime_class, DW_FORM_data1, RLang);
Devang Patelf4215332009-01-05 19:55:51 +00001789 }
1790 break;
1791 default:
1792 break;
1793 }
1794
1795 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001796 if (!Name.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00001797 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf4215332009-01-05 19:55:51 +00001798
Devang Patelad165be2009-01-27 00:45:04 +00001799 if (Tag == DW_TAG_enumeration_type || Tag == DW_TAG_structure_type
1800 || Tag == DW_TAG_union_type) {
1801 // Add size if non-zero (derived types might be zero-sized.)
1802 if (Size)
1803 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1804 else {
1805 // Add zero size if it is not a forward declaration.
1806 if (CTy.isForwardDecl())
1807 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1808 else
1809 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1810 }
1811
1812 // Add source line info if available.
1813 if (!CTy.isForwardDecl())
1814 AddSourceLine(&Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001815 }
Devang Patelf4215332009-01-05 19:55:51 +00001816 }
1817
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001818 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1819 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelf193ff02009-01-15 19:26:23 +00001820 int64_t L = SR.getLo();
1821 int64_t H = SR.getHi();
Devang Patel68afdc32009-01-05 18:33:01 +00001822 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1823 if (L != H) {
1824 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1825 if (L)
Devang Patel2d1768c2009-01-17 08:05:14 +00001826 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1827 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel68afdc32009-01-05 18:33:01 +00001828 }
1829 Buffer.AddChild(DW_Subrange);
1830 }
1831
1832 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1833 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1834 DICompositeType *CTy) {
1835 Buffer.setTag(DW_TAG_array_type);
1836 if (CTy->getTag() == DW_TAG_vector_type)
1837 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1838
Devang Patelf9235742009-01-28 21:08:20 +00001839 // Emit derived type.
1840 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel68afdc32009-01-05 18:33:01 +00001841 DIArray Elements = CTy->getTypeArray();
Devang Patel68afdc32009-01-05 18:33:01 +00001842
1843 // Construct an anonymous type for index type.
1844 DIE IdxBuffer(DW_TAG_base_type);
1845 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1846 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1847 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1848
1849 // Add subranges to array type.
1850 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patelf4215332009-01-05 19:55:51 +00001851 DIDescriptor Element = Elements.getElement(i);
Devang Patelf193ff02009-01-15 19:26:23 +00001852 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1853 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel68afdc32009-01-05 18:33:01 +00001854 }
1855 }
1856
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001857 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Pateleab4a2e2009-01-20 18:35:14 +00001858 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patelc69bf2c2009-01-05 18:38:38 +00001859
1860 DIE *Enumerator = new DIE(DW_TAG_enumerator);
Bill Wendling0582ae92009-03-13 04:39:26 +00001861 std::string Name;
1862 ETy->getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001863 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
Devang Patelc69bf2c2009-01-05 18:38:38 +00001864 int64_t Value = ETy->getEnumValue();
1865 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Pateleab4a2e2009-01-20 18:35:14 +00001866 return Enumerator;
Devang Patelc69bf2c2009-01-05 18:38:38 +00001867 }
Devang Patel68afdc32009-01-05 18:33:01 +00001868
Devang Patel5aac3d32009-01-17 08:01:33 +00001869 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001870 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patel5aac3d32009-01-17 08:01:33 +00001871 {
1872 DIE *GVDie = new DIE(DW_TAG_variable);
Bill Wendling0582ae92009-03-13 04:39:26 +00001873 std::string Name;
1874 GV.getDisplayName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001875 AddString(GVDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling0582ae92009-03-13 04:39:26 +00001876 std::string LinkageName;
1877 GV.getLinkageName(LinkageName);
1878 if (!LinkageName.empty())
Devang Patel5aac3d32009-01-17 08:01:33 +00001879 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1880 AddType(DW_Unit, GVDie, GV.getType());
1881 if (!GV.isLocalToUnit())
1882 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
1883 AddSourceLine(GVDie, &GV);
1884 return GVDie;
Devang Patel86ae1422009-01-05 18:59:44 +00001885 }
1886
Devang Patel2be58932009-01-20 21:02:02 +00001887 /// CreateMemberDIE - Create new member DIE.
1888 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
1889 DIE *MemberDie = new DIE(DT.getTag());
Bill Wendling0582ae92009-03-13 04:39:26 +00001890 std::string Name;
1891 DT.getName(Name);
1892 if (!Name.empty())
Devang Patel2be58932009-01-20 21:02:02 +00001893 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1894
1895 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1896
1897 AddSourceLine(MemberDie, &DT);
1898
Devang Patel36375ee2009-02-17 21:23:59 +00001899 uint64_t Size = DT.getSizeInBits();
1900 uint64_t FieldSize = DT.getOriginalTypeSize();
1901
1902 if (Size != FieldSize) {
1903 // Handle bitfield.
1904 AddUInt(MemberDie, DW_AT_byte_size, 0, DT.getOriginalTypeSize() >> 3);
1905 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1906
1907 uint64_t Offset = DT.getOffsetInBits();
1908 uint64_t FieldOffset = Offset;
1909 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1910 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1911 FieldOffset = (HiMark - FieldSize);
1912 Offset -= FieldOffset;
1913 // Maybe we need to work from the other end.
1914 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1915 AddUInt(MemberDie, DW_AT_bit_offset, 0, Offset);
1916 }
Devang Patel2be58932009-01-20 21:02:02 +00001917 DIEBlock *Block = new DIEBlock();
1918 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1919 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1920 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1921
Devang Patel47661592009-01-21 00:08:04 +00001922 if (DT.isProtected())
1923 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1924 else if (DT.isPrivate())
1925 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1926
Devang Patel2be58932009-01-20 21:02:02 +00001927 return MemberDie;
1928 }
1929
Devang Patel5aac3d32009-01-17 08:01:33 +00001930 /// CreateSubprogramDIE - Create new DIE using SP.
1931 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Bill Wendling3f500d92009-05-06 21:21:34 +00001932 const DISubprogram &SP,
Devang Patel2d1768c2009-01-17 08:05:14 +00001933 bool IsConstructor = false) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001934 DIE *SPDie = new DIE(DW_TAG_subprogram);
Bill Wendling3f500d92009-05-06 21:21:34 +00001935
Bill Wendling0582ae92009-03-13 04:39:26 +00001936 std::string Name;
1937 SP.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001938 AddString(SPDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling3f500d92009-05-06 21:21:34 +00001939
Bill Wendling0582ae92009-03-13 04:39:26 +00001940 std::string LinkageName;
1941 SP.getLinkageName(LinkageName);
Bill Wendling3f500d92009-05-06 21:21:34 +00001942
Bill Wendling0582ae92009-03-13 04:39:26 +00001943 if (!LinkageName.empty())
Bill Wendling3f500d92009-05-06 21:21:34 +00001944 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1945
Devang Patel5aac3d32009-01-17 08:01:33 +00001946 AddSourceLine(SPDie, &SP);
Devang Patel86ae1422009-01-05 18:59:44 +00001947
Devang Patel5aac3d32009-01-17 08:01:33 +00001948 DICompositeType SPTy = SP.getType();
1949 DIArray Args = SPTy.getTypeArray();
Bill Wendling3f500d92009-05-06 21:21:34 +00001950
1951 // Add prototyped tag, if C or ObjC.
1952 unsigned Lang = SP.getCompileUnit().getLanguage();
1953 if (Lang == DW_LANG_C99 || Lang == DW_LANG_C89 || Lang == DW_LANG_ObjC)
1954 AddUInt(SPDie, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patel5aac3d32009-01-17 08:01:33 +00001955
Devang Patel86ae1422009-01-05 18:59:44 +00001956 // Add Return Type.
Devang Patel75b27382009-04-08 22:18:45 +00001957 unsigned SPTag = SPTy.getTag();
Devang Patel9ac08d62009-02-27 18:05:21 +00001958 if (!IsConstructor) {
Devang Patel75b27382009-04-08 22:18:45 +00001959 if (Args.isNull() || SPTag != DW_TAG_subroutine_type)
Devang Patel9ac08d62009-02-27 18:05:21 +00001960 AddType(DW_Unit, SPDie, SPTy);
1961 else
1962 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
1963 }
Devang Pateld234e592009-01-30 01:21:46 +00001964
Devang Pateld5863dd2009-02-02 17:51:41 +00001965 if (!SP.isDefinition()) {
1966 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
Bill Wendling3f500d92009-05-06 21:21:34 +00001967 // Add arguments. Do not add arguments for subprogram definition. They
1968 // will be handled through RecordVariable.
Devang Patel75b27382009-04-08 22:18:45 +00001969 if (SPTag == DW_TAG_subroutine_type)
Devang Pateld5863dd2009-02-02 17:51:41 +00001970 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1971 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1972 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
Bill Wendling3f500d92009-05-06 21:21:34 +00001973 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ??
Devang Pateld5863dd2009-02-02 17:51:41 +00001974 SPDie->AddChild(Arg);
1975 }
1976 }
Devang Pateld234e592009-01-30 01:21:46 +00001977
Devang Patelf193ff02009-01-15 19:26:23 +00001978 if (!SP.isLocalToUnit())
Devang Pateld234e592009-01-30 01:21:46 +00001979 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Bill Wendling3f500d92009-05-06 21:21:34 +00001980
Devang Patel1be3ecc2009-04-15 00:10:26 +00001981 // DW_TAG_inlined_subroutine may refer to this DIE.
1982 DIE *&Slot = DW_Unit->getDieMapSlotFor(SP.getGV());
1983 Slot = SPDie;
Devang Patel5aac3d32009-01-17 08:01:33 +00001984 return SPDie;
Devang Patel86ae1422009-01-05 18:59:44 +00001985 }
1986
Devang Patel5aac3d32009-01-17 08:01:33 +00001987 /// FindCompileUnit - Get the compile unit for the given descriptor.
1988 ///
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001989 CompileUnit &FindCompileUnit(DICompileUnit Unit) const {
1990 DenseMap<Value *, CompileUnit *>::const_iterator I =
1991 CompileUnitMap.find(Unit.getGV());
1992 assert(I != CompileUnitMap.end() && "Missing compile unit.");
1993 return *I->second;
Devang Patel8526cc02009-01-05 22:35:52 +00001994 }
1995
Devang Patelbbdc8202009-01-13 23:54:55 +00001996 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001997 ///
1998 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1999 // Get the descriptor.
Devang Patel99ec3532009-01-16 19:28:14 +00002000 const DIVariable &VD = DV->getVariable();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002001
2002 // Translate tag to proper Dwarf tag. The result variable is dropped for
2003 // now.
2004 unsigned Tag;
Devang Patel99ec3532009-01-16 19:28:14 +00002005 switch (VD.getTag()) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00002006 case DW_TAG_return_variable: return NULL;
2007 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2008 case DW_TAG_auto_variable: // fall thru
2009 default: Tag = DW_TAG_variable; break;
2010 }
2011
2012 // Define variable debug information entry.
2013 DIE *VariableDie = new DIE(Tag);
Bill Wendling0582ae92009-03-13 04:39:26 +00002014 std::string Name;
2015 VD.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00002016 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002017
2018 // Add source line info if available.
Devang Patel99ec3532009-01-16 19:28:14 +00002019 AddSourceLine(VariableDie, &VD);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002020
2021 // Add variable type.
Devang Patel99ec3532009-01-16 19:28:14 +00002022 AddType(Unit, VariableDie, VD.getType());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002023
2024 // Add variable address.
2025 MachineLocation Location;
2026 Location.set(RI->getFrameRegister(*MF),
2027 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
2028 AddAddress(VariableDie, DW_AT_location, Location);
2029
2030 return VariableDie;
2031 }
2032
Devang Patel7a6e5a32009-01-08 02:33:41 +00002033 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2034 ///
2035 DbgScope *getOrCreateScope(GlobalVariable *V) {
2036 DbgScope *&Slot = DbgScopeMap[V];
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002037 if (Slot) return Slot;
2038
Devang Patel1be3ecc2009-04-15 00:10:26 +00002039 DbgScope *Parent = NULL;
2040 DIBlock Block(V);
Bill Wendling3f500d92009-05-06 21:21:34 +00002041
Devang Patel1be3ecc2009-04-15 00:10:26 +00002042 if (!Block.isNull()) {
2043 DIDescriptor ParentDesc = Block.getContext();
2044 Parent =
2045 ParentDesc.isNull() ? NULL : getOrCreateScope(ParentDesc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002046 }
Bill Wendling3f500d92009-05-06 21:21:34 +00002047
Devang Patel1be3ecc2009-04-15 00:10:26 +00002048 Slot = new DbgScope(Parent, DIDescriptor(V));
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002049
Devang Patel1be3ecc2009-04-15 00:10:26 +00002050 if (Parent)
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002051 Parent->AddScope(Slot);
Devang Patel1be3ecc2009-04-15 00:10:26 +00002052 else
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002053 // First function is top level function.
Devang Patel7d2f9722009-04-15 20:41:31 +00002054 FunctionDbgScope = Slot;
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002055
Devang Patel7a6e5a32009-01-08 02:33:41 +00002056 return Slot;
2057 }
2058
Dan Gohman9a38e3e2009-05-07 19:46:24 +00002059 /// createInlinedSubroutineScope - Returns the scope associated with the
2060 /// inlined subroutine.
2061 ///
2062 DbgScope *createInlinedSubroutineScope(DISubprogram SP, unsigned Src,
2063 unsigned Line, unsigned Col) {
2064 DbgScope *Scope =
2065 new DbgInlinedSubroutineScope(NULL, SP, Src, Line, Col);
2066
2067 // FIXME - Add inlined function scopes to the root so we can delete them
2068 // later.
2069 assert (FunctionDbgScope && "Function scope info missing!");
2070 FunctionDbgScope->AddScope(Scope);
2071 return Scope;
2072 }
2073
Devang Patel7a6e5a32009-01-08 02:33:41 +00002074 /// ConstructDbgScope - Construct the components of a scope.
2075 ///
2076 void ConstructDbgScope(DbgScope *ParentScope,
2077 unsigned ParentStartID, unsigned ParentEndID,
2078 DIE *ParentDie, CompileUnit *Unit) {
Dan Gohman9a38e3e2009-05-07 19:46:24 +00002079 // Add variables to scope.
2080 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
2081 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2082 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2083 if (VariableDie) ParentDie->AddChild(VariableDie);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002084 }
2085
2086 // Add nested scopes.
Devang Patel9795da52009-01-10 02:42:49 +00002087 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002088 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2089 // Define the Scope debug information entry.
2090 DbgScope *Scope = Scopes[j];
Devang Patel7a6e5a32009-01-08 02:33:41 +00002091
Devang Patele9bfb0f2009-01-12 18:41:00 +00002092 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2093 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002094
Devang Patel1be3ecc2009-04-15 00:10:26 +00002095 // Ignore empty scopes.
2096 // Do not ignore inlined scope even if it does not have any
2097 // variables or scopes.
Devang Patel7a6e5a32009-01-08 02:33:41 +00002098 if (StartID == EndID && StartID != 0) continue;
Devang Patel1be3ecc2009-04-15 00:10:26 +00002099 if (!Scope->isInlinedSubroutine()
Devang Patel0f7fef32009-04-13 17:02:03 +00002100 && Scope->getScopes().empty() && Scope->getVariables().empty())
2101 continue;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002102
2103 if (StartID == ParentStartID && EndID == ParentEndID) {
2104 // Just add stuff to the parent scope.
2105 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2106 } else {
Devang Patel1be3ecc2009-04-15 00:10:26 +00002107 DIE *ScopeDie = NULL;
Dan Gohman9a38e3e2009-05-07 19:46:24 +00002108 if (MainCU && TAI->doesDwarfUsesInlineInfoSection()
2109 && Scope->isInlinedSubroutine()) {
2110 ScopeDie = new DIE(DW_TAG_inlined_subroutine);
2111 DIE *Origin = MainCU->getDieMapSlotFor(Scope->getDesc().getGV());
2112 AddDIEntry(ScopeDie, DW_AT_abstract_origin, DW_FORM_ref4, Origin);
2113 AddUInt(ScopeDie, DW_AT_call_file, 0, Scope->getFile());
2114 AddUInt(ScopeDie, DW_AT_call_line, 0, Scope->getLine());
2115 AddUInt(ScopeDie, DW_AT_call_column, 0, Scope->getColumn());
2116 } else {
Devang Patel1be3ecc2009-04-15 00:10:26 +00002117 ScopeDie = new DIE(DW_TAG_lexical_block);
Dan Gohman9a38e3e2009-05-07 19:46:24 +00002118 }
Bill Wendling7fa81622009-05-01 08:25:13 +00002119
2120 // Add the scope bounds.
2121 if (StartID)
2122 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2123 DWLabel("label", StartID));
2124 else
2125 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2126 DWLabel("func_begin", SubprogramCount));
2127
2128 if (EndID)
2129 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2130 DWLabel("label", EndID));
2131 else
2132 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2133 DWLabel("func_end", SubprogramCount));
2134
2135 // Add the scope contents.
2136 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2137 ParentDie->AddChild(ScopeDie);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002138 }
2139 }
2140 }
2141
Devang Patel7d2f9722009-04-15 20:41:31 +00002142 /// ConstructFunctionDbgScope - Construct the scope for the subprogram.
Devang Patel7a6e5a32009-01-08 02:33:41 +00002143 ///
Devang Patel7d2f9722009-04-15 20:41:31 +00002144 void ConstructFunctionDbgScope(DbgScope *RootScope) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00002145 // Exit if there is no root scope.
2146 if (!RootScope) return;
Devang Patel0e5200f2009-01-15 18:25:17 +00002147 DIDescriptor Desc = RootScope->getDesc();
2148 if (Desc.isNull())
2149 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002150
2151 // Get the subprogram debug information entry.
Devang Patel0e5200f2009-01-15 18:25:17 +00002152 DISubprogram SPD(Desc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002153
2154 // Get the compile unit context.
Devang Pateldd9db662009-01-30 18:20:31 +00002155 CompileUnit *Unit = MainCU;
2156 if (!Unit)
Chris Lattnere3f6cea2009-05-05 04:55:56 +00002157 Unit = &FindCompileUnit(SPD.getCompileUnit());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002158
2159 // Get the subprogram die.
Devang Patelf6bac3e2009-01-12 22:58:14 +00002160 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002161 assert(SPDie && "Missing subprogram descriptor");
2162
2163 // Add the function bounds.
2164 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2165 DWLabel("func_begin", SubprogramCount));
2166 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2167 DWLabel("func_end", SubprogramCount));
2168 MachineLocation Location(RI->getFrameRegister(*MF));
2169 AddAddress(SPDie, DW_AT_frame_base, Location);
2170
2171 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2172 }
2173
2174 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2175 ///
2176 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Chenge3d42322009-02-25 07:04:34 +00002177 const char *FnName = MF->getFunction()->getNameStart();
2178 if (MainCU) {
Bill Wendling972bbac2009-04-09 21:49:15 +00002179 StringMap<DIE*> &Globals = MainCU->getGlobals();
2180 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Chenge3d42322009-02-25 07:04:34 +00002181 if (GI != Globals.end()) {
2182 DIE *SPDie = GI->second;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002183
2184 // Add the function bounds.
2185 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2186 DWLabel("func_begin", SubprogramCount));
2187 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2188 DWLabel("func_end", SubprogramCount));
2189
2190 MachineLocation Location(RI->getFrameRegister(*MF));
2191 AddAddress(SPDie, DW_AT_frame_base, Location);
2192 return;
2193 }
Evan Chenge3d42322009-02-25 07:04:34 +00002194 } else {
2195 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2196 CompileUnit *Unit = CompileUnits[i];
Bill Wendling972bbac2009-04-09 21:49:15 +00002197 StringMap<DIE*> &Globals = Unit->getGlobals();
2198 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Chenge3d42322009-02-25 07:04:34 +00002199 if (GI != Globals.end()) {
2200 DIE *SPDie = GI->second;
2201
2202 // Add the function bounds.
2203 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2204 DWLabel("func_begin", SubprogramCount));
2205 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2206 DWLabel("func_end", SubprogramCount));
2207
2208 MachineLocation Location(RI->getFrameRegister(*MF));
2209 AddAddress(SPDie, DW_AT_frame_base, Location);
2210 return;
2211 }
2212 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00002213 }
Evan Chenge3d42322009-02-25 07:04:34 +00002214
Devang Patel7a6e5a32009-01-08 02:33:41 +00002215#if 0
2216 // FIXME: This is causing an abort because C++ mangled names are compared
2217 // with their unmangled counterparts. See PR2885. Don't do this assert.
2218 assert(0 && "Couldn't find DIE for machine function!");
2219#endif
2220 }
2221
Jim Laskeyef42a012006-11-02 20:12:39 +00002222 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2223 /// tools to recognize the object file contains Dwarf information.
2224 void EmitInitial() {
2225 // Check to see if we already emitted intial headers.
2226 if (didInitial) return;
2227 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002228
Jim Laskeyef42a012006-11-02 20:12:39 +00002229 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002230 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002231 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002232 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002233 }
2234 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2235 EmitLabel("section_info", 0);
2236 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2237 EmitLabel("section_abbrev", 0);
2238 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2239 EmitLabel("section_aranges", 0);
Scott Michel210de722009-01-26 22:32:51 +00002240 if (TAI->doesSupportMacInfoSection()) {
2241 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2242 EmitLabel("section_macinfo", 0);
2243 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002244 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2245 EmitLabel("section_line", 0);
2246 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2247 EmitLabel("section_loc", 0);
2248 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2249 EmitLabel("section_pubnames", 0);
2250 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2251 EmitLabel("section_str", 0);
2252 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2253 EmitLabel("section_ranges", 0);
2254
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002255 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002256 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002257 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002258 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002259 }
2260
Jim Laskey65195462006-10-30 13:35:07 +00002261 /// EmitDIE - Recusively Emits a debug information entry.
2262 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002263 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002264 // Get the abbreviation for this DIE.
2265 unsigned AbbrevNumber = Die->getAbbrevNumber();
2266 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002267
Jim Laskeybacd3042007-02-21 22:48:45 +00002268 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002269
2270 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002271 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002272
Evan Cheng42bf74b2009-03-25 01:47:28 +00002273 if (Asm->isVerbose())
Evan Cheng6547e402008-07-01 23:18:29 +00002274 Asm->EOL(std::string("Abbrev [" +
2275 utostr(AbbrevNumber) +
2276 "] 0x" + utohexstr(Die->getOffset()) +
2277 ":0x" + utohexstr(Die->getSize()) + " " +
2278 TagString(Abbrev->getTag())));
2279 else
2280 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002281
Owen Anderson873e1b52008-06-24 21:44:59 +00002282 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2283 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002284
Jim Laskeyef42a012006-11-02 20:12:39 +00002285 // Emit the DIE attribute values.
2286 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2287 unsigned Attr = AbbrevData[i].getAttribute();
2288 unsigned Form = AbbrevData[i].getForm();
2289 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002290
Jim Laskeyef42a012006-11-02 20:12:39 +00002291 switch (Attr) {
2292 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002293 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002294 break;
2295 }
2296 default: {
2297 // Emit an attribute using the defined form.
2298 Values[i]->EmitValue(*this, Form);
2299 break;
2300 }
2301 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002302
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002303 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002304 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002305
Jim Laskeyef42a012006-11-02 20:12:39 +00002306 // Emit the DIE children if any.
2307 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2308 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002309
Bill Wendlinga9519572009-05-08 20:38:02 +00002310 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Jim Laskeyef42a012006-11-02 20:12:39 +00002311 EmitDIE(Children[j]);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002312
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002313 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002314 }
2315 }
2316
Jim Laskey65195462006-10-30 13:35:07 +00002317 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2318 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002319 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2320 // Get the children.
2321 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002322
Jim Laskeyef42a012006-11-02 20:12:39 +00002323 // If not last sibling and has children then add sibling offset attribute.
2324 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2325
2326 // Record the abbreviation.
2327 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002328
Jim Laskeyef42a012006-11-02 20:12:39 +00002329 // Get the abbreviation for this DIE.
2330 unsigned AbbrevNumber = Die->getAbbrevNumber();
2331 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2332
2333 // Set DIE offset
2334 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002335
Jim Laskeyef42a012006-11-02 20:12:39 +00002336 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002337 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2338
Owen Anderson873e1b52008-06-24 21:44:59 +00002339 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2340 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002341
2342 // Size the DIE attribute values.
2343 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2344 // Size attribute value.
2345 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2346 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002347
Jim Laskeyef42a012006-11-02 20:12:39 +00002348 // Size the DIE children if any.
2349 if (!Children.empty()) {
2350 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2351 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002352
Jim Laskeyef42a012006-11-02 20:12:39 +00002353 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2354 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2355 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002356
Jim Laskeyef42a012006-11-02 20:12:39 +00002357 // End of children marker.
2358 Offset += sizeof(int8_t);
2359 }
2360
2361 Die->setSize(Offset - Die->getOffset());
2362 return Offset;
2363 }
Jim Laskey65195462006-10-30 13:35:07 +00002364
2365 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2366 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002367 void SizeAndOffsets() {
Bill Wendlinge688faf2009-05-08 21:03:15 +00002368 // Compute size of compile unit header.
2369 static unsigned Offset =
2370 sizeof(int32_t) + // Length of Compilation Unit Info
2371 sizeof(int16_t) + // DWARF version number
2372 sizeof(int32_t) + // Offset Into Abbrev. Section
2373 sizeof(int8_t); // Pointer Size (in bytes)
2374
Jim Laskey5496f012006-11-09 14:52:14 +00002375 // Process base compile unit.
Devang Pateldd9db662009-01-30 18:20:31 +00002376 if (MainCU) {
Devang Pateldd9db662009-01-30 18:20:31 +00002377 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
Bill Wendlinge688faf2009-05-08 21:03:15 +00002378 CompileUnitOffsets[MainCU] = 0;
Devang Pateldd9db662009-01-30 18:20:31 +00002379 return;
2380 }
Bill Wendlinge688faf2009-05-08 21:03:15 +00002381
2382 // Process all compile units.
2383 unsigned PrevOffset = 0;
2384
Evan Chenge3d42322009-02-25 07:04:34 +00002385 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2386 CompileUnit *Unit = CompileUnits[i];
Bill Wendlinge688faf2009-05-08 21:03:15 +00002387 CompileUnitOffsets[Unit] = PrevOffset;
2388 PrevOffset += SizeAndOffsetDie(Unit->getDie(), Offset, true)
2389 + sizeof(int32_t); // FIXME - extra pad for gdb bug.
Devang Patel72b66352009-01-12 23:05:55 +00002390 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002391 }
2392
Evan Chenge3d42322009-02-25 07:04:34 +00002393 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Jim Laskey65195462006-10-30 13:35:07 +00002394 ///
Evan Chenge3d42322009-02-25 07:04:34 +00002395 void EmitDebugInfoPerCU(CompileUnit *Unit) {
2396 DIE *Die = Unit->getDie();
2397 // Emit the compile units header.
2398 EmitLabel("info_begin", Unit->getID());
2399 // Emit size of content not including length itself
2400 unsigned ContentSize = Die->getSize() +
2401 sizeof(int16_t) + // DWARF version number
2402 sizeof(int32_t) + // Offset Into Abbrev. Section
2403 sizeof(int8_t) + // Pointer Size (in bytes)
2404 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2405
2406 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2407 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2408 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2409 Asm->EOL("Offset Into Abbrev. Section");
2410 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2411
2412 EmitDIE(Die);
2413 // FIXME - extra padding for gdb bug.
2414 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2415 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2416 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2417 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2418 EmitLabel("info_end", Unit->getID());
2419
2420 Asm->EOL();
2421 }
2422
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002423 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002424 // Start debug info section.
2425 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002426
Evan Chenge3d42322009-02-25 07:04:34 +00002427 if (MainCU) {
2428 EmitDebugInfoPerCU(MainCU);
2429 return;
Devang Patel72b66352009-01-12 23:05:55 +00002430 }
Evan Chenge3d42322009-02-25 07:04:34 +00002431
2432 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2433 EmitDebugInfoPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002434 }
2435
Jim Laskey65195462006-10-30 13:35:07 +00002436 /// EmitAbbreviations - Emit the abbreviation section.
2437 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002438 void EmitAbbreviations() const {
2439 // Check to see if it is worth the effort.
2440 if (!Abbreviations.empty()) {
2441 // Start the debug abbrev section.
2442 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002443
Jim Laskeyef42a012006-11-02 20:12:39 +00002444 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002445
Jim Laskeyef42a012006-11-02 20:12:39 +00002446 // For each abbrevation.
2447 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2448 // Get abbreviation data
2449 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002450
Jim Laskeyef42a012006-11-02 20:12:39 +00002451 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002452 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2453 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002454
Jim Laskeyef42a012006-11-02 20:12:39 +00002455 // Emit the abbreviations data.
2456 Abbrev->Emit(*this);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002457
Jim Laskeybacd3042007-02-21 22:48:45 +00002458 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002459 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002460
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002461 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00002462 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002463
Jim Laskeyef42a012006-11-02 20:12:39 +00002464 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002465
Jim Laskeybacd3042007-02-21 22:48:45 +00002466 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002467 }
2468 }
2469
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002470 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2471 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002472 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002473 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2474 // Define last address of section.
2475 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2476 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2477 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2478 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2479
2480 // Mark end of matrix.
2481 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2482 Asm->EmitULEB128Bytes(1); Asm->EOL();
2483 Asm->EmitInt8(1); Asm->EOL();
2484 }
2485
Jim Laskey65195462006-10-30 13:35:07 +00002486 /// EmitDebugLines - Emit source line information.
2487 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002488 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002489 // If the target is using .loc/.file, the assembler will be emitting the
2490 // .debug_line table automatically.
2491 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00002492 return;
2493
Jim Laskeyef42a012006-11-02 20:12:39 +00002494 // Minimum line delta, thus ranging from -10..(255-10).
2495 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2496 // Maximum line delta, thus ranging from -10..(255-10).
2497 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002498
Jim Laskeyef42a012006-11-02 20:12:39 +00002499 // Start the dwarf line section.
2500 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002501
Jim Laskeyef42a012006-11-02 20:12:39 +00002502 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002503
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002504 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002505 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002506 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002507
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002508 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002509
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002510 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002511 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002512 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002513
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002514 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002515
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002516 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002517
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002518 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002519
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002520 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002521
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002522 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002523
Jim Laskeyef42a012006-11-02 20:12:39 +00002524 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002525 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2526 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2527 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2528 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2529 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2530 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2531 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2532 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2533 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00002534
Jim Laskeyef42a012006-11-02 20:12:39 +00002535 // Emit directories.
Evan Chenge3d42322009-02-25 07:04:34 +00002536 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2537 Asm->EmitString(getSourceDirectoryName(DI));
2538 Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00002539 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002540 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002541
Jim Laskeyef42a012006-11-02 20:12:39 +00002542 // Emit files.
Evan Chenge3d42322009-02-25 07:04:34 +00002543 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2544 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002545 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Evan Chenge3d42322009-02-25 07:04:34 +00002546 Asm->EmitString(getSourceFileName(Id.second));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002547 Asm->EOL("Source");
Evan Chenge3d42322009-02-25 07:04:34 +00002548 Asm->EmitULEB128Bytes(Id.first);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002549 Asm->EOL("Directory #");
2550 Asm->EmitULEB128Bytes(0);
2551 Asm->EOL("Mod date");
2552 Asm->EmitULEB128Bytes(0);
2553 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002554 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002555 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002556
Jim Laskeyef42a012006-11-02 20:12:39 +00002557 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002558
Jim Laskeyef42a012006-11-02 20:12:39 +00002559 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002560 unsigned SecSrcLinesSize = SectionSourceLines.size();
2561
2562 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002563 // Isolate current sections line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00002564 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00002565
Evan Cheng42bf74b2009-03-25 01:47:28 +00002566 if (Asm->isVerbose()) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002567 const Section* S = SectionMap[j + 1];
Evan Chenge3d42322009-02-25 07:04:34 +00002568 O << '\t' << TAI->getCommentString() << " Section"
2569 << S->getName() << '\n';
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002570 } else
Evan Cheng6547e402008-07-01 23:18:29 +00002571 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002572
2573 // Dwarf assumes we start with first line of first source file.
2574 unsigned Source = 1;
2575 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002576
Jim Laskeyef42a012006-11-02 20:12:39 +00002577 // Construct rows of the address, source, line, column matrix.
2578 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patelf3ee5142009-01-12 22:54:42 +00002579 const SrcLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002580 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00002581 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002582
Evan Cheng42bf74b2009-03-25 01:47:28 +00002583 if (!Asm->isVerbose())
Evan Cheng6547e402008-07-01 23:18:29 +00002584 Asm->EOL();
Evan Chenge3d42322009-02-25 07:04:34 +00002585 else {
2586 std::pair<unsigned, unsigned> SourceID =
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002587 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Evan Chenge3d42322009-02-25 07:04:34 +00002588 O << '\t' << TAI->getCommentString() << ' '
2589 << getSourceDirectoryName(SourceID.first) << ' '
2590 << getSourceFileName(SourceID.second)
2591 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2592 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002593
2594 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002595 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00002596 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002597 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00002598 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002599
Jim Laskeyef42a012006-11-02 20:12:39 +00002600 // If change of source, then switch to the new source.
2601 if (Source != LineInfo.getSourceID()) {
2602 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002603 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2604 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00002605 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002606
Jim Laskeyef42a012006-11-02 20:12:39 +00002607 // If change of line.
2608 if (Line != LineInfo.getLine()) {
2609 // Determine offset.
2610 int Offset = LineInfo.getLine() - Line;
2611 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002612
Jim Laskeyef42a012006-11-02 20:12:39 +00002613 // Update line.
2614 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002615
Jim Laskeyef42a012006-11-02 20:12:39 +00002616 // If delta is small enough and in range...
2617 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2618 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002619 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00002620 } else {
2621 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002622 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2623 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2624 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002625 }
2626 } else {
2627 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002628 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002629 }
2630 }
2631
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002632 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002633 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002634
2635 if (SecSrcLinesSize == 0)
2636 // Because we're emitting a debug_line section, we still need a line
2637 // table. The linker and friends expect it to exist. If there's nothing to
2638 // put into it, emit an empty table.
2639 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002640
Jim Laskeyef42a012006-11-02 20:12:39 +00002641 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002642
Jim Laskeybacd3042007-02-21 22:48:45 +00002643 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002644 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002645
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002646 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00002647 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002648 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002649 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00002650 return;
2651
2652 int stackGrowth =
2653 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2654 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002655 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00002656
2657 // Start the dwarf frame section.
2658 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2659
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002660 EmitLabel("debug_frame_common", 0);
2661 EmitDifference("debug_frame_common_end", 0,
2662 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002663 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002664
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002665 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002666 Asm->EmitInt32((int)DW_CIE_ID);
2667 Asm->EOL("CIE Identifier Tag");
2668 Asm->EmitInt8(DW_CIE_VERSION);
2669 Asm->EOL("CIE Version");
2670 Asm->EmitString("");
2671 Asm->EOL("CIE Augmentation");
2672 Asm->EmitULEB128Bytes(1);
2673 Asm->EOL("CIE Code Alignment Factor");
2674 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002675 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002676 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002677 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002678
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002679 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002680 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002681
Dale Johannesenb97aec62007-11-13 19:13:01 +00002682 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002683
Evan Cheng05548eb2008-02-29 19:36:59 +00002684 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002685 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002686
Jim Laskeybacd3042007-02-21 22:48:45 +00002687 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002688 }
2689
Jim Laskey65195462006-10-30 13:35:07 +00002690 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2691 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002692 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002693 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00002694 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002695
Jim Laskeyef42a012006-11-02 20:12:39 +00002696 // Start the dwarf frame section.
2697 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002698
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002699 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2700 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002701 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002702
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002703 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002704
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002705 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2706 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002707 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002708
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002709 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002710 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002711 EmitDifference("func_end", DebugFrameInfo.Number,
2712 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002713 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002714
Devang Patel5aac3d32009-01-17 08:01:33 +00002715 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00002716 false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002717
Evan Cheng05548eb2008-02-29 19:36:59 +00002718 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002719 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00002720
Jim Laskeybacd3042007-02-21 22:48:45 +00002721 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002722 }
2723
Evan Chenge3d42322009-02-25 07:04:34 +00002724 void EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2725 EmitDifference("pubnames_end", Unit->getID(),
2726 "pubnames_begin", Unit->getID(), true);
2727 Asm->EOL("Length of Public Names Info");
2728
2729 EmitLabel("pubnames_begin", Unit->getID());
2730
2731 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2732
2733 EmitSectionOffset("info_begin", "section_info",
2734 Unit->getID(), 0, true, false);
2735 Asm->EOL("Offset of Compilation Unit Info");
2736
2737 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2738 true);
2739 Asm->EOL("Compilation Unit Length");
2740
Bill Wendling972bbac2009-04-09 21:49:15 +00002741 StringMap<DIE*> &Globals = Unit->getGlobals();
Bill Wendlingf34be822009-04-09 23:51:31 +00002742 for (StringMap<DIE*>::const_iterator
Bill Wendling972bbac2009-04-09 21:49:15 +00002743 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
Bill Wendlingf34be822009-04-09 23:51:31 +00002744 const char *Name = GI->getKeyData();
Evan Chenge3d42322009-02-25 07:04:34 +00002745 DIE * Entity = GI->second;
2746
2747 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
Bill Wendlingf34be822009-04-09 23:51:31 +00002748 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
Evan Chenge3d42322009-02-25 07:04:34 +00002749 }
2750
2751 Asm->EmitInt32(0); Asm->EOL("End Mark");
2752 EmitLabel("pubnames_end", Unit->getID());
2753
2754 Asm->EOL();
2755 }
2756
Jim Laskeyef42a012006-11-02 20:12:39 +00002757 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002758 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002759 void EmitDebugPubNames() {
2760 // Start the dwarf pubnames section.
2761 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002762
Evan Chenge3d42322009-02-25 07:04:34 +00002763 if (MainCU) {
2764 EmitDebugPubNamesPerCU(MainCU);
2765 return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002766 }
Evan Chenge3d42322009-02-25 07:04:34 +00002767
2768 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2769 EmitDebugPubNamesPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002770 }
2771
2772 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002773 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002774 void EmitDebugStr() {
2775 // Check to see if it is worth the effort.
2776 if (!StringPool.empty()) {
2777 // Start the dwarf str section.
2778 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002779
Jim Laskeyef42a012006-11-02 20:12:39 +00002780 // For each of strings in the string pool.
2781 for (unsigned StringID = 1, N = StringPool.size();
2782 StringID <= N; ++StringID) {
2783 // Emit a label for reference from debug information entries.
2784 EmitLabel("string", StringID);
2785 // Emit the string itself.
2786 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00002787 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002788 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002789
Jim Laskeybacd3042007-02-21 22:48:45 +00002790 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002791 }
2792 }
2793
2794 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002795 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002796 void EmitDebugLoc() {
2797 // Start the dwarf loc section.
2798 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002799
Jim Laskeybacd3042007-02-21 22:48:45 +00002800 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002801 }
2802
2803 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002804 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002805 void EmitDebugARanges() {
2806 // Start the dwarf aranges section.
2807 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002808
Jim Laskeyef42a012006-11-02 20:12:39 +00002809 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00002810#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002811 CompileUnit *Unit = GetBaseCompileUnit();
2812
Jim Laskey5496f012006-11-09 14:52:14 +00002813 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002814 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002815
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002816 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002817
Jim Laskey5496f012006-11-09 14:52:14 +00002818 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002819 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002820
Dan Gohman82482942007-09-27 23:12:31 +00002821 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002822
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002823 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002824
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002825 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2826 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002827
Jim Laskey5496f012006-11-09 14:52:14 +00002828 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002829 EmitReference("text_begin", 0); Asm->EOL("Address");
2830 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002831
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002832 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2833 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00002834#endif
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 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002840 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002841 void EmitDebugRanges() {
2842 // Start the dwarf ranges section.
2843 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002844
Jim Laskeybacd3042007-02-21 22:48:45 +00002845 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002846 }
2847
2848 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002849 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002850 void EmitDebugMacInfo() {
Scott Michel210de722009-01-26 22:32:51 +00002851 if (TAI->doesSupportMacInfoSection()) {
2852 // Start the dwarf macinfo section.
2853 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002854
Scott Michel210de722009-01-26 22:32:51 +00002855 Asm->EOL();
2856 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002857 }
2858
Devang Patel0f7fef32009-04-13 17:02:03 +00002859 /// EmitDebugInlineInfo - Emit inline info using following format.
2860 /// Section Header:
2861 /// 1. length of section
2862 /// 2. Dwarf version number
2863 /// 3. address size.
2864 ///
2865 /// Entries (one "entry" for each function that was inlined):
2866 ///
2867 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
2868 /// otherwise offset into __debug_str for regular function name.
2869 /// 2. offset into __debug_str section for regular function name.
2870 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
2871 /// instances for the function.
2872 ///
2873 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
2874 /// inlined instance; the die_offset points to the inlined_subroutine die in
2875 /// the __debug_info section, and the low_pc is the starting address for the
2876 /// inlining instance.
2877 void EmitDebugInlineInfo() {
2878 if (!TAI->doesDwarfUsesInlineInfoSection())
2879 return;
2880
2881 if (!MainCU)
2882 return;
2883
2884 Asm->SwitchToDataSection(TAI->getDwarfDebugInlineSection());
2885 Asm->EOL();
2886 EmitDifference("debug_inlined_end", 1,
2887 "debug_inlined_begin", 1, true);
2888 Asm->EOL("Length of Debug Inlined Information Entry");
2889
2890 EmitLabel("debug_inlined_begin", 1);
2891
2892 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
2893 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2894
2895 for (DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
2896 I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2897 GlobalVariable *GV = I->first;
2898 SmallVector<unsigned, 4> &Labels = I->second;
2899 DISubprogram SP(GV);
2900 std::string Name;
2901 std::string LName;
Dan Gohman9a38e3e2009-05-07 19:46:24 +00002902
Devang Patel0f7fef32009-04-13 17:02:03 +00002903 SP.getLinkageName(LName);
2904 SP.getName(Name);
2905
2906 Asm->EmitString(LName.empty() ? Name : LName);
2907 Asm->EOL("MIPS linkage name");
2908
2909 Asm->EmitString(Name); Asm->EOL("Function name");
2910
2911 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2912
2913 for (SmallVector<unsigned, 4>::iterator LI = Labels.begin(),
2914 LE = Labels.end(); LI != LE; ++LI) {
2915 DIE *SP = MainCU->getDieMapSlotFor(GV);
2916 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2917
2918 if (TD->getPointerSize() == sizeof(int32_t))
2919 O << TAI->getData32bitsDirective();
2920 else
2921 O << TAI->getData64bitsDirective();
2922 PrintLabelName("label", *LI); Asm->EOL("low_pc");
2923 }
2924 }
2925
2926 EmitLabel("debug_inlined_end", 1);
2927 Asm->EOL();
2928 }
2929
Bill Wendlingc8615e42009-03-10 21:47:45 +00002930 /// GetOrCreateSourceID - Look up the source id with the given directory and
2931 /// source file names. If none currently exists, create a new id and insert it
2932 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
2933 /// as well.
2934 unsigned GetOrCreateSourceID(const std::string &DirName,
2935 const std::string &FileName) {
2936 unsigned DId;
2937 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
2938 if (DI != DirectoryIdMap.end()) {
2939 DId = DI->getValue();
2940 } else {
2941 DId = DirectoryNames.size() + 1;
2942 DirectoryIdMap[DirName] = DId;
2943 DirectoryNames.push_back(DirName);
2944 }
2945
2946 unsigned FId;
2947 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
2948 if (FI != SourceFileIdMap.end()) {
2949 FId = FI->getValue();
2950 } else {
2951 FId = SourceFileNames.size() + 1;
2952 SourceFileIdMap[FileName] = FId;
2953 SourceFileNames.push_back(FileName);
2954 }
2955
2956 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
2957 SourceIdMap.find(std::make_pair(DId, FId));
2958 if (SI != SourceIdMap.end())
2959 return SI->second;
2960
2961 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
2962 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
2963 SourceIds.push_back(std::make_pair(DId, FId));
2964
2965 return SrcId;
2966 }
2967
Evan Chenge3d42322009-02-25 07:04:34 +00002968 void ConstructCompileUnit(GlobalVariable *GV) {
2969 DICompileUnit DIUnit(GV);
Bill Wendling0582ae92009-03-13 04:39:26 +00002970 std::string Dir, FN, Prod;
2971 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
2972 DIUnit.getFilename(FN));
Evan Chenge3d42322009-02-25 07:04:34 +00002973
2974 DIE *Die = new DIE(DW_TAG_compile_unit);
2975 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2976 DWLabel("section_line", 0), DWLabel("section_line", 0),
2977 false);
Bill Wendling0582ae92009-03-13 04:39:26 +00002978 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer(Prod));
Evan Chenge3d42322009-02-25 07:04:34 +00002979 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002980 AddString(Die, DW_AT_name, DW_FORM_string, FN);
Bill Wendling0582ae92009-03-13 04:39:26 +00002981 if (!Dir.empty())
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002982 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Chenge3d42322009-02-25 07:04:34 +00002983 if (DIUnit.isOptimized())
2984 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendling0582ae92009-03-13 04:39:26 +00002985 std::string Flags;
2986 DIUnit.getFlags(Flags);
2987 if (!Flags.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00002988 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
2989 unsigned RVer = DIUnit.getRunTimeVersion();
2990 if (RVer)
2991 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
2992
2993 CompileUnit *Unit = new CompileUnit(ID, Die);
2994 if (DIUnit.isMain()) {
2995 assert(!MainCU && "Multiple main compile units are found!");
2996 MainCU = Unit;
2997 }
2998 CompileUnitMap[DIUnit.getGV()] = Unit;
2999 CompileUnits.push_back(Unit);
3000 }
3001
Devang Patelc4523242009-01-05 23:11:11 +00003002 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Pateld1ca9252009-01-05 23:03:32 +00003003 void ConstructCompileUnits() {
Evan Chenge3d42322009-02-25 07:04:34 +00003004 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.compile_units");
3005 if (!Root)
3006 return;
3007 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3008 "Malformed compile unit descriptor anchor type");
3009 Constant *RootC = cast<Constant>(*Root->use_begin());
3010 assert(RootC->hasNUsesOrMore(1) &&
3011 "Malformed compile unit descriptor anchor type");
3012 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3013 UI != UE; ++UI)
3014 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3015 UUI != UUE; ++UUI) {
3016 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3017 ConstructCompileUnit(GV);
Devang Pateldd9db662009-01-30 18:20:31 +00003018 }
Evan Chenge3d42322009-02-25 07:04:34 +00003019 }
3020
3021 bool ConstructGlobalVariableDIE(GlobalVariable *GV) {
3022 DIGlobalVariable DI_GV(GV);
3023 CompileUnit *DW_Unit = MainCU;
3024 if (!DW_Unit)
Chris Lattnere3f6cea2009-05-05 04:55:56 +00003025 DW_Unit = &FindCompileUnit(DI_GV.getCompileUnit());
Evan Chenge3d42322009-02-25 07:04:34 +00003026
3027 // Check for pre-existence.
3028 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
3029 if (Slot)
3030 return false;
3031
3032 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
3033
3034 // Add address.
3035 DIEBlock *Block = new DIEBlock();
3036 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Bill Wendling7d16e852009-04-10 00:12:49 +00003037 std::string GLN;
Evan Chenge3d42322009-02-25 07:04:34 +00003038 AddObjectLabel(Block, 0, DW_FORM_udata,
Bill Wendling7d16e852009-04-10 00:12:49 +00003039 Asm->getGlobalLinkName(DI_GV.getGlobal(), GLN));
Evan Chenge3d42322009-02-25 07:04:34 +00003040 AddBlock(VariableDie, DW_AT_location, 0, Block);
3041
3042 // Add to map.
3043 Slot = VariableDie;
Bill Wendling3f500d92009-05-06 21:21:34 +00003044
Evan Chenge3d42322009-02-25 07:04:34 +00003045 // Add to context owner.
3046 DW_Unit->getDie()->AddChild(VariableDie);
Bill Wendling3f500d92009-05-06 21:21:34 +00003047
Evan Chenge3d42322009-02-25 07:04:34 +00003048 // Expose as global. FIXME - need to check external flag.
Bill Wendling0582ae92009-03-13 04:39:26 +00003049 std::string Name;
3050 DW_Unit->AddGlobal(DI_GV.getName(Name), VariableDie);
Evan Chenge3d42322009-02-25 07:04:34 +00003051 return true;
Devang Pateld1ca9252009-01-05 23:03:32 +00003052 }
3053
Devang Patelc4523242009-01-05 23:11:11 +00003054 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patel53cac182009-02-24 00:02:15 +00003055 /// visible global variables. Return true if at least one global DIE is
3056 /// created.
3057 bool ConstructGlobalVariableDIEs() {
Evan Chenge3d42322009-02-25 07:04:34 +00003058 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
3059 if (!Root)
3060 return false;
Devang Patelc4523242009-01-05 23:11:11 +00003061
Evan Chenge3d42322009-02-25 07:04:34 +00003062 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3063 "Malformed global variable descriptor anchor type");
3064 Constant *RootC = cast<Constant>(*Root->use_begin());
3065 assert(RootC->hasNUsesOrMore(1) &&
3066 "Malformed global variable descriptor anchor type");
Devang Patelc4523242009-01-05 23:11:11 +00003067
Evan Chenge3d42322009-02-25 07:04:34 +00003068 bool Result = false;
3069 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3070 UI != UE; ++UI)
3071 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
Bill Wendling3f500d92009-05-06 21:21:34 +00003072 UUI != UUE; ++UUI)
3073 Result |= ConstructGlobalVariableDIE(cast<GlobalVariable>(*UUI));
3074
Evan Chenge3d42322009-02-25 07:04:34 +00003075 return Result;
3076 }
Devang Patelc4523242009-01-05 23:11:11 +00003077
Evan Chenge3d42322009-02-25 07:04:34 +00003078 bool ConstructSubprogram(GlobalVariable *GV) {
3079 DISubprogram SP(GV);
3080 CompileUnit *Unit = MainCU;
3081 if (!Unit)
Chris Lattnere3f6cea2009-05-05 04:55:56 +00003082 Unit = &FindCompileUnit(SP.getCompileUnit());
Devang Patelc4523242009-01-05 23:11:11 +00003083
Evan Chenge3d42322009-02-25 07:04:34 +00003084 // Check for pre-existence.
3085 DIE *&Slot = Unit->getDieMapSlotFor(GV);
3086 if (Slot)
3087 return false;
3088
3089 if (!SP.isDefinition())
3090 // This is a method declaration which will be handled while
3091 // constructing class type.
3092 return false;
3093
3094 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
3095
3096 // Add to map.
3097 Slot = SubprogramDie;
3098 // Add to context owner.
3099 Unit->getDie()->AddChild(SubprogramDie);
3100 // Expose as global.
Bill Wendling0582ae92009-03-13 04:39:26 +00003101 std::string Name;
3102 Unit->AddGlobal(SP.getName(Name), SubprogramDie);
Evan Chenge3d42322009-02-25 07:04:34 +00003103 return true;
Devang Patelc4523242009-01-05 23:11:11 +00003104 }
3105
Devang Patel78eb6ad2009-01-05 23:21:35 +00003106 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patel53cac182009-02-24 00:02:15 +00003107 /// subprograms. Return true if at least one subprogram DIE is created.
3108 bool ConstructSubprograms() {
Evan Chenge3d42322009-02-25 07:04:34 +00003109 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
3110 if (!Root)
3111 return false;
Devang Patel78eb6ad2009-01-05 23:21:35 +00003112
Evan Chenge3d42322009-02-25 07:04:34 +00003113 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3114 "Malformed subprogram descriptor anchor type");
3115 Constant *RootC = cast<Constant>(*Root->use_begin());
3116 assert(RootC->hasNUsesOrMore(1) &&
3117 "Malformed subprogram descriptor anchor type");
Devang Patel78eb6ad2009-01-05 23:21:35 +00003118
Evan Chenge3d42322009-02-25 07:04:34 +00003119 bool Result = false;
3120 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3121 UI != UE; ++UI)
3122 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
Bill Wendling3f500d92009-05-06 21:21:34 +00003123 UUI != UUE; ++UUI)
3124 Result |= ConstructSubprogram(cast<GlobalVariable>(*UUI));
3125
Evan Chenge3d42322009-02-25 07:04:34 +00003126 return Result;
Devang Patel78eb6ad2009-01-05 23:21:35 +00003127 }
3128
Jim Laskey65195462006-10-30 13:35:07 +00003129public:
Jim Laskeyef42a012006-11-02 20:12:39 +00003130 //===--------------------------------------------------------------------===//
3131 // Main entry points.
3132 //
Owen Andersoncb371882008-08-21 00:14:44 +00003133 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00003134 : Dwarf(OS, A, T, "dbg"), MainCU(0),
3135 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
3136 ValuesSet(InitValuesSetSize), Values(), StringPool(), SectionMap(),
3137 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel7d2f9722009-04-15 20:41:31 +00003138 FunctionDbgScope(0), DebugTimer(0) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003139 if (TimePassesIsEnabled)
3140 DebugTimer = new Timer("Dwarf Debug Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00003141 getDwarfTimerGroup());
Jim Laskeyef42a012006-11-02 20:12:39 +00003142 }
Jim Laskey072200c2007-01-29 18:51:14 +00003143 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003144 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3145 delete Values[j];
Bill Wendling163ba3f2009-03-10 21:23:25 +00003146
3147 delete DebugTimer;
Jim Laskeyef42a012006-11-02 20:12:39 +00003148 }
3149
Bill Wendlingc8615e42009-03-10 21:47:45 +00003150 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
3151 /// be emitted.
3152 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
3153
Devang Patel5d315982009-01-06 21:07:30 +00003154 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3155 /// This is inovked by the target AsmPrinter.
Devang Patel3f7833a2009-01-12 23:09:42 +00003156 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003157 if (TimePassesIsEnabled)
3158 DebugTimer->startTimer();
3159
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003160 // Create all the compile unit DIEs.
3161 ConstructCompileUnits();
Devang Patel3f7833a2009-01-12 23:09:42 +00003162
Bill Wendling163ba3f2009-03-10 21:23:25 +00003163 if (CompileUnits.empty()) {
3164 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003165 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003166
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003167 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003168 }
Devang Patel3f7833a2009-01-12 23:09:42 +00003169
Devang Patel53cac182009-02-24 00:02:15 +00003170 // Create DIEs for each of the externally visible global variables.
3171 bool globalDIEs = ConstructGlobalVariableDIEs();
3172
3173 // Create DIEs for each of the externally visible subprograms.
3174 bool subprogramDIEs = ConstructSubprograms();
3175
3176 // If there is not any debug info available for any global variables
3177 // and any subprograms then there is not any debug info to emit.
Bill Wendling163ba3f2009-03-10 21:23:25 +00003178 if (!globalDIEs && !subprogramDIEs) {
3179 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003180 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003181
Devang Patel53cac182009-02-24 00:02:15 +00003182 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003183 }
Devang Patel53cac182009-02-24 00:02:15 +00003184
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003185 MMI = mmi;
3186 shouldEmit = true;
3187 MMI->setDebugInfoAvailability(true);
Devang Patel5d315982009-01-06 21:07:30 +00003188
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003189 // Prime section data.
3190 SectionMap.insert(TAI->getTextSection());
Devang Patel5d315982009-01-06 21:07:30 +00003191
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003192 // Print out .file directives to specify files for .loc directives. These
3193 // are printed out early so that they precede any .loc directives.
3194 if (TAI->hasDotLocAndDotFile()) {
Evan Chenge3d42322009-02-25 07:04:34 +00003195 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
3196 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00003197 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Evan Chenge3d42322009-02-25 07:04:34 +00003198 sys::Path FullPath(getSourceDirectoryName(Id.first));
3199 bool AppendOk =
3200 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003201 assert(AppendOk && "Could not append filename to directory!");
3202 AppendOk = false;
3203 Asm->EmitFile(i, FullPath.toString());
3204 Asm->EOL();
Devang Patel5d315982009-01-06 21:07:30 +00003205 }
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003206 }
Devang Patel5d315982009-01-06 21:07:30 +00003207
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003208 // Emit initial sections
3209 EmitInitial();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003210
3211 if (TimePassesIsEnabled)
3212 DebugTimer->stopTimer();
Devang Patel5d315982009-01-06 21:07:30 +00003213 }
3214
Jim Laskey65195462006-10-30 13:35:07 +00003215 /// BeginModule - Emit all Dwarf sections that should come prior to the
3216 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00003217 void BeginModule(Module *M) {
3218 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00003219 }
3220
Jim Laskey65195462006-10-30 13:35:07 +00003221 /// EndModule - Emit all Dwarf sections that should come after the content.
3222 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003223 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003224 if (!ShouldEmitDwarfDebug())
3225 return;
3226
3227 if (TimePassesIsEnabled)
3228 DebugTimer->startTimer();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003229
Jim Laskeyef42a012006-11-02 20:12:39 +00003230 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003231 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003232 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00003233 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003234 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003235
Jim Laskeyef42a012006-11-02 20:12:39 +00003236 // End text sections.
3237 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003238 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00003239 EmitLabel("section_end", i);
3240 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003241
3242 // Emit common frame information.
3243 EmitCommonDebugFrame();
3244
3245 // Emit function debug frame information
3246 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3247 E = DebugFrames.end(); I != E; ++I)
3248 EmitFunctionDebugFrame(*I);
3249
Jim Laskeyef42a012006-11-02 20:12:39 +00003250 // Compute DIE offsets and sizes.
3251 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003252
Jim Laskeyef42a012006-11-02 20:12:39 +00003253 // Emit all the DIEs into a debug info section
3254 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003255
Jim Laskeyef42a012006-11-02 20:12:39 +00003256 // Corresponding abbreviations into a abbrev section.
3257 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003258
Jim Laskeyef42a012006-11-02 20:12:39 +00003259 // Emit source line correspondence into a debug line section.
3260 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003261
Jim Laskeyef42a012006-11-02 20:12:39 +00003262 // Emit info into a debug pubnames section.
3263 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003264
Jim Laskeyef42a012006-11-02 20:12:39 +00003265 // Emit info into a debug str section.
3266 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003267
Jim Laskeyef42a012006-11-02 20:12:39 +00003268 // Emit info into a debug loc section.
3269 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003270
Jim Laskeyef42a012006-11-02 20:12:39 +00003271 // Emit info into a debug aranges section.
3272 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003273
Jim Laskeyef42a012006-11-02 20:12:39 +00003274 // Emit info into a debug ranges section.
3275 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003276
Jim Laskeyef42a012006-11-02 20:12:39 +00003277 // Emit info into a debug macinfo section.
3278 EmitDebugMacInfo();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003279
Devang Patel0f7fef32009-04-13 17:02:03 +00003280 // Emit inline info.
3281 EmitDebugInlineInfo();
3282
Bill Wendling163ba3f2009-03-10 21:23:25 +00003283 if (TimePassesIsEnabled)
3284 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003285 }
3286
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003287 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00003288 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00003289 void BeginFunction(MachineFunction *MF) {
Bill Wendling1362f972009-03-11 00:03:50 +00003290 this->MF = MF;
3291
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003292 if (!ShouldEmitDwarfDebug()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00003293
Bill Wendling163ba3f2009-03-10 21:23:25 +00003294 if (TimePassesIsEnabled)
3295 DebugTimer->startTimer();
3296
Jim Laskeyef42a012006-11-02 20:12:39 +00003297 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003298 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003299
Jim Laskeyef42a012006-11-02 20:12:39 +00003300 // Assumes in correct section after the entry point.
3301 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00003302
Argyrios Kyrtzidis28f14122009-05-04 19:23:45 +00003303 // Emit label for the implicitly defined dbg.stoppoint at the start of
3304 // the function.
Mike Stumpfe095f32009-05-04 18:40:41 +00003305 DebugLoc FDL = MF->getDefaultDebugLoc();
3306 if (!FDL.isUnknown()) {
3307 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
3308 unsigned LabelID = RecordSourceLine(DLT.Line, DLT.Col,
3309 DICompileUnit(DLT.CompileUnit));
3310 Asm->printLabel(LabelID);
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00003311 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003312
3313 if (TimePassesIsEnabled)
3314 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003315 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003316
Jim Laskey65195462006-10-30 13:35:07 +00003317 /// EndFunction - Gather and emit post-function debug information.
3318 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00003319 void EndFunction(MachineFunction *MF) {
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003320 if (!ShouldEmitDwarfDebug()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003321
Bill Wendling163ba3f2009-03-10 21:23:25 +00003322 if (TimePassesIsEnabled)
3323 DebugTimer->startTimer();
3324
Jim Laskeyb82313f2007-02-01 16:31:34 +00003325 // Define end label for subprogram.
3326 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003327
Jim Laskeyef42a012006-11-02 20:12:39 +00003328 // Get function line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00003329 if (!Lines.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003330 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003331 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00003332 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patelf3ee5142009-01-12 22:54:42 +00003333 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Jim Laskeyef42a012006-11-02 20:12:39 +00003334 // Append the function info to section info.
3335 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patelf3ee5142009-01-12 22:54:42 +00003336 Lines.begin(), Lines.end());
Jim Laskeyef42a012006-11-02 20:12:39 +00003337 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003338
Jim Laskeyef42a012006-11-02 20:12:39 +00003339 // Construct scopes for subprogram.
Devang Patel7d2f9722009-04-15 20:41:31 +00003340 if (FunctionDbgScope)
3341 ConstructFunctionDbgScope(FunctionDbgScope);
Bill Wendlingd751c642008-09-26 00:28:12 +00003342 else
3343 // FIXME: This is wrong. We are essentially getting past a problem with
3344 // debug information not being able to handle unreachable blocks that have
3345 // debug information in them. In particular, those unreachable blocks that
3346 // have "region end" info in them. That situation results in the "root
3347 // scope" not being created. If that's the case, then emit a "default"
3348 // scope, i.e., one that encompasses the whole function. This isn't
3349 // desirable. And a better way of handling this (and all of the debugging
3350 // information) needs to be explored.
Devang Patel7bb89ed2009-01-13 00:20:51 +00003351 ConstructDefaultDbgScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003352
3353 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3354 MMI->getFrameMoves()));
Devang Patel481ff5b2009-01-12 18:48:36 +00003355
3356 // Clear debug info
Devang Patel7d2f9722009-04-15 20:41:31 +00003357 if (FunctionDbgScope) {
3358 delete FunctionDbgScope;
Devang Patel481ff5b2009-01-12 18:48:36 +00003359 DbgScopeMap.clear();
Devang Patel1be3ecc2009-04-15 00:10:26 +00003360 DbgInlinedScopeMap.clear();
3361 InlinedVariableScopes.clear();
Devang Patel7d2f9722009-04-15 20:41:31 +00003362 FunctionDbgScope = NULL;
Devang Patel481ff5b2009-01-12 18:48:36 +00003363 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003364
Bill Wendling163ba3f2009-03-10 21:23:25 +00003365 Lines.clear();
3366
3367 if (TimePassesIsEnabled)
3368 DebugTimer->stopTimer();
3369 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003370
3371 /// RecordSourceLine - Records location information and associates it with a
3372 /// label. Returns a unique label ID used to generate a label and provide
3373 /// correspondence to the source line list.
3374 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003375 if (TimePassesIsEnabled)
3376 DebugTimer->startTimer();
3377
Evan Chenge3d42322009-02-25 07:04:34 +00003378 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003379 assert(Unit && "Unable to find CompileUnit");
Devang Patelccca7fe2009-01-12 19:17:34 +00003380 unsigned ID = MMI->NextLabelID();
3381 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003382
3383 if (TimePassesIsEnabled)
3384 DebugTimer->stopTimer();
3385
Devang Patelccca7fe2009-01-12 19:17:34 +00003386 return ID;
3387 }
3388
3389 /// RecordSourceLine - Records location information and associates it with a
3390 /// label. Returns a unique label ID used to generate a label and provide
3391 /// correspondence to the source line list.
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00003392 unsigned RecordSourceLine(unsigned Line, unsigned Col, DICompileUnit CU) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003393 if (TimePassesIsEnabled)
3394 DebugTimer->startTimer();
3395
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00003396 std::string Dir, Fn;
3397 unsigned Src = GetOrCreateSourceID(CU.getDirectory(Dir),
3398 CU.getFilename(Fn));
Devang Patelccca7fe2009-01-12 19:17:34 +00003399 unsigned ID = MMI->NextLabelID();
3400 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003401
3402 if (TimePassesIsEnabled)
3403 DebugTimer->stopTimer();
3404
Devang Patelccca7fe2009-01-12 19:17:34 +00003405 return ID;
3406 }
3407
Bill Wendlingc8615e42009-03-10 21:47:45 +00003408 /// getRecordSourceLineCount - Return the number of source lines in the debug
3409 /// info.
3410 unsigned getRecordSourceLineCount() const {
Devang Patelccca7fe2009-01-12 19:17:34 +00003411 return Lines.size();
3412 }
3413
Bill Wendlingc8615e42009-03-10 21:47:45 +00003414 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
3415 /// timed. Look up the source id with the given directory and source file
3416 /// names. If none currently exists, create a new id and insert it in the
3417 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
3418 /// well.
Evan Chenge3d42322009-02-25 07:04:34 +00003419 unsigned getOrCreateSourceID(const std::string &DirName,
3420 const std::string &FileName) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003421 if (TimePassesIsEnabled)
3422 DebugTimer->startTimer();
3423
Bill Wendlingc8615e42009-03-10 21:47:45 +00003424 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003425
3426 if (TimePassesIsEnabled)
3427 DebugTimer->stopTimer();
3428
Evan Chenge3d42322009-02-25 07:04:34 +00003429 return SrcId;
Devang Patelccca7fe2009-01-12 19:17:34 +00003430 }
3431
3432 /// RecordRegionStart - Indicate the start of a region.
Devang Patelccca7fe2009-01-12 19:17:34 +00003433 unsigned RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003434 if (TimePassesIsEnabled)
3435 DebugTimer->startTimer();
3436
Devang Patelccca7fe2009-01-12 19:17:34 +00003437 DbgScope *Scope = getOrCreateScope(V);
3438 unsigned ID = MMI->NextLabelID();
3439 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003440
3441 if (TimePassesIsEnabled)
3442 DebugTimer->stopTimer();
3443
Devang Patelccca7fe2009-01-12 19:17:34 +00003444 return ID;
3445 }
3446
3447 /// RecordRegionEnd - Indicate the end of a region.
Dan Gohman9a38e3e2009-05-07 19:46:24 +00003448 unsigned RecordRegionEnd(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003449 if (TimePassesIsEnabled)
3450 DebugTimer->startTimer();
3451
Bill Wendling5b8479c2009-05-07 17:26:14 +00003452 DbgScope *Scope = getOrCreateScope(V);
Dan Gohman9a38e3e2009-05-07 19:46:24 +00003453 unsigned ID = MMI->NextLabelID();
Devang Patelccca7fe2009-01-12 19:17:34 +00003454 Scope->setEndLabelID(ID);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003455
3456 if (TimePassesIsEnabled)
3457 DebugTimer->stopTimer();
3458
Devang Patelccca7fe2009-01-12 19:17:34 +00003459 return ID;
3460 }
3461
3462 /// RecordVariable - Indicate the declaration of a local variable.
Devang Patel1be3ecc2009-04-15 00:10:26 +00003463 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
3464 const MachineInstr *MI) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003465 if (TimePassesIsEnabled)
3466 DebugTimer->startTimer();
3467
Devang Patel0e5200f2009-01-15 18:25:17 +00003468 DIDescriptor Desc(GV);
3469 DbgScope *Scope = NULL;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003470
Devang Patel0e5200f2009-01-15 18:25:17 +00003471 if (Desc.getTag() == DW_TAG_variable) {
3472 // GV is a global variable.
3473 DIGlobalVariable DG(GV);
3474 Scope = getOrCreateScope(DG.getContext().getGV());
3475 } else {
Devang Patel1be3ecc2009-04-15 00:10:26 +00003476 DenseMap<const MachineInstr *, DbgScope *>::iterator
3477 SI = InlinedVariableScopes.find(MI);
Bill Wendling3f500d92009-05-06 21:21:34 +00003478
Devang Patel1be3ecc2009-04-15 00:10:26 +00003479 if (SI != InlinedVariableScopes.end()) {
3480 // or GV is an inlined local variable.
3481 Scope = SI->second;
3482 } else {
Devang Patel0e5200f2009-01-15 18:25:17 +00003483 // or GV is a local variable.
Devang Patel1be3ecc2009-04-15 00:10:26 +00003484 DIVariable DV(GV);
3485 Scope = getOrCreateScope(DV.getContext().getGV());
3486 }
Devang Patel0e5200f2009-01-15 18:25:17 +00003487 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003488
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003489 assert(Scope && "Unable to find variable' scope");
Devang Patel99ec3532009-01-16 19:28:14 +00003490 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelccca7fe2009-01-12 19:17:34 +00003491 Scope->AddVariable(DV);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003492
3493 if (TimePassesIsEnabled)
3494 DebugTimer->stopTimer();
Devang Patelccca7fe2009-01-12 19:17:34 +00003495 }
Devang Patel0f7fef32009-04-13 17:02:03 +00003496
Devang Patel1be3ecc2009-04-15 00:10:26 +00003497 //// RecordInlinedFnStart - Indicate the start of inlined subroutine.
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00003498 unsigned RecordInlinedFnStart(DISubprogram &SP, DICompileUnit CU,
3499 unsigned Line, unsigned Col) {
3500 unsigned LabelID = MMI->NextLabelID();
3501
Devang Patel1be3ecc2009-04-15 00:10:26 +00003502 if (!TAI->doesDwarfUsesInlineInfoSection())
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00003503 return LabelID;
Devang Patel1be3ecc2009-04-15 00:10:26 +00003504
Bill Wendlingd5a63812009-05-01 08:40:06 +00003505 if (TimePassesIsEnabled)
3506 DebugTimer->startTimer();
3507
Dan Gohman9a38e3e2009-05-07 19:46:24 +00003508 std::string Dir, Fn;
3509 unsigned Src = GetOrCreateSourceID(CU.getDirectory(Dir),
3510 CU.getFilename(Fn));
3511 DbgScope *Scope = createInlinedSubroutineScope(SP, Src, Line, Col);
3512 Scope->setStartLabelID(LabelID);
3513 MMI->RecordUsedDbgLabel(LabelID);
Devang Patel1be3ecc2009-04-15 00:10:26 +00003514 GlobalVariable *GV = SP.getGV();
3515
Dan Gohman9a38e3e2009-05-07 19:46:24 +00003516 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3517 SI = DbgInlinedScopeMap.find(GV);
Bill Wendlingef956fc2009-05-01 08:32:14 +00003518
Dan Gohman9a38e3e2009-05-07 19:46:24 +00003519 if (SI == DbgInlinedScopeMap.end())
3520 DbgInlinedScopeMap[GV].push_back(Scope);
3521 else
3522 SI->second.push_back(Scope);
Devang Patel1be3ecc2009-04-15 00:10:26 +00003523
Dan Gohman9a38e3e2009-05-07 19:46:24 +00003524 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
3525 I = InlineInfo.find(GV);
3526 if (I == InlineInfo.end())
3527 InlineInfo[GV].push_back(LabelID);
3528 else
3529 I->second.push_back(LabelID);
Bill Wendlingd5a63812009-05-01 08:40:06 +00003530
3531 if (TimePassesIsEnabled)
3532 DebugTimer->stopTimer();
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00003533
3534 return LabelID;
Devang Patel0f7fef32009-04-13 17:02:03 +00003535 }
Devang Patel1be3ecc2009-04-15 00:10:26 +00003536
3537 /// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
3538 unsigned RecordInlinedFnEnd(DISubprogram &SP) {
3539 if (!TAI->doesDwarfUsesInlineInfoSection())
3540 return 0;
3541
Bill Wendlingd5a63812009-05-01 08:40:06 +00003542 if (TimePassesIsEnabled)
3543 DebugTimer->startTimer();
3544
Devang Patel1be3ecc2009-04-15 00:10:26 +00003545 GlobalVariable *GV = SP.getGV();
3546 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3547 I = DbgInlinedScopeMap.find(GV);
Bill Wendling3f500d92009-05-06 21:21:34 +00003548
Bill Wendlingd5a63812009-05-01 08:40:06 +00003549 if (I == DbgInlinedScopeMap.end()) {
3550 if (TimePassesIsEnabled)
3551 DebugTimer->stopTimer();
3552
Devang Patel1be3ecc2009-04-15 00:10:26 +00003553 return 0;
Bill Wendlingd5a63812009-05-01 08:40:06 +00003554 }
Devang Patel1be3ecc2009-04-15 00:10:26 +00003555
3556 SmallVector<DbgScope *, 2> &Scopes = I->second;
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00003557 assert(!Scopes.empty() && "We should have at least one debug scope!");
Devang Patel1be3ecc2009-04-15 00:10:26 +00003558 DbgScope *Scope = Scopes.back(); Scopes.pop_back();
3559 unsigned ID = MMI->NextLabelID();
Bill Wendling3f500d92009-05-06 21:21:34 +00003560
Devang Patel1be3ecc2009-04-15 00:10:26 +00003561 MMI->RecordUsedDbgLabel(ID);
3562 Scope->setEndLabelID(ID);
Bill Wendlingd5a63812009-05-01 08:40:06 +00003563
3564 if (TimePassesIsEnabled)
3565 DebugTimer->stopTimer();
3566
Devang Patel1be3ecc2009-04-15 00:10:26 +00003567 return ID;
3568 }
3569
3570 /// RecordVariableScope - Record scope for the variable declared by
3571 /// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
3572 /// Record scopes for only inlined subroutine variables. Other
3573 /// variables' scopes are determined during RecordVariable().
3574 void RecordVariableScope(DIVariable &DV, const MachineInstr *DeclareMI) {
Bill Wendlingd5a63812009-05-01 08:40:06 +00003575 if (TimePassesIsEnabled)
3576 DebugTimer->startTimer();
3577
Devang Patel1be3ecc2009-04-15 00:10:26 +00003578 DISubprogram SP(DV.getContext().getGV());
Bill Wendlingd5a63812009-05-01 08:40:06 +00003579
3580 if (SP.isNull()) {
3581 if (TimePassesIsEnabled)
3582 DebugTimer->stopTimer();
3583
Devang Patel1be3ecc2009-04-15 00:10:26 +00003584 return;
Bill Wendlingd5a63812009-05-01 08:40:06 +00003585 }
3586
Devang Patel1be3ecc2009-04-15 00:10:26 +00003587 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3588 I = DbgInlinedScopeMap.find(SP.getGV());
Bill Wendlingd5a63812009-05-01 08:40:06 +00003589 if (I != DbgInlinedScopeMap.end())
3590 InlinedVariableScopes[DeclareMI] = I->second.back();
Devang Patel1be3ecc2009-04-15 00:10:26 +00003591
Bill Wendlingd5a63812009-05-01 08:40:06 +00003592 if (TimePassesIsEnabled)
3593 DebugTimer->stopTimer();
Devang Patel1be3ecc2009-04-15 00:10:26 +00003594 }
Jim Laskey65195462006-10-30 13:35:07 +00003595};
3596
Jim Laskey072200c2007-01-29 18:51:14 +00003597//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003598/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00003599///
3600class DwarfException : public Dwarf {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003601 struct FunctionEHFrameInfo {
3602 std::string FnName;
3603 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003604 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003605 bool hasCalls;
3606 bool hasLandingPads;
3607 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003608 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00003609
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003610 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3611 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003612 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003613 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003614 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003615 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003616 };
3617
3618 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003619
3620 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3621 /// be emitted.
3622 bool shouldEmitTable;
3623
3624 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3625 /// should be emitted.
3626 bool shouldEmitMoves;
3627
3628 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3629 /// should be emitted.
3630 bool shouldEmitTableModule;
3631
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003632 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003633 /// should be emitted.
3634 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00003635
Bill Wendling163ba3f2009-03-10 21:23:25 +00003636 /// ExceptionTimer - Timer for the Dwarf exception writer.
3637 Timer *ExceptionTimer;
3638
Jim Laskey3f09fc22007-02-28 18:38:31 +00003639 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3640 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003641 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003642 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003643 int stackGrowth =
3644 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3645 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003646 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003647
Jim Laskeybacd3042007-02-21 22:48:45 +00003648 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003649 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling722f5f12008-12-24 08:05:17 +00003650
3651 if (!TAI->doesRequireNonLocalEHFrameLabel())
3652 O << TAI->getEHGlobalPrefix();
3653 O << "EH_frame" << Index << ":\n";
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003654 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003655
Jim Laskeybacd3042007-02-21 22:48:45 +00003656 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003657 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003658
Jim Laskeybacd3042007-02-21 22:48:45 +00003659 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003660 EmitDifference("eh_frame_common_end", Index,
3661 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003662 Asm->EOL("Length of Common Information Entry");
3663
Jim Laskeybacd3042007-02-21 22:48:45 +00003664 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003665 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003666 Asm->EmitInt32((int)0);
3667 Asm->EOL("CIE Identifier Tag");
3668 Asm->EmitInt8(DW_CIE_VERSION);
3669 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00003670
Jim Laskeybacd3042007-02-21 22:48:45 +00003671 // The personality presence indicates that language specific information
3672 // will show up in the eh frame.
3673 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00003674 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00003675
Jim Laskeybacd3042007-02-21 22:48:45 +00003676 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003677 Asm->EmitULEB128Bytes(1);
3678 Asm->EOL("CIE Code Alignment Factor");
3679 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00003680 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003681 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00003682 Asm->EOL("CIE Return Address Column");
3683
Jim Laskeybacd3042007-02-21 22:48:45 +00003684 // If there is a personality, we need to indicate the functions location.
3685 if (Personality) {
3686 Asm->EmitULEB128Bytes(7);
3687 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00003688
Duncan Sands671fa972008-05-07 19:11:09 +00003689 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003690 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00003691 Asm->EOL("Personality (pcrel sdata4 indirect)");
3692 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003693 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00003694 Asm->EOL("Personality (pcrel sdata4)");
3695 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00003696
Duncan Sands671fa972008-05-07 19:11:09 +00003697 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00003698 O << TAI->getPersonalityPrefix();
3699 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3700 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00003701 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3702 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00003703 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00003704
Duncan Sands671fa972008-05-07 19:11:09 +00003705 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3706 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003707
Bill Wendlingd60de512009-01-05 22:53:45 +00003708 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3709 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003710 } else {
3711 Asm->EmitULEB128Bytes(1);
3712 Asm->EOL("Augmentation Size");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003713
Bill Wendlingd60de512009-01-05 22:53:45 +00003714 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3715 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003716 }
3717
3718 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003719 std::vector<MachineMove> Moves;
3720 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00003721 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003722
Dale Johannesen21d972a2008-04-30 00:43:29 +00003723 // On Darwin the linker honors the alignment of eh_frame, which means it
3724 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3725 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003726 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003727 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003728 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003729
Jim Laskeybacd3042007-02-21 22:48:45 +00003730 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003731 }
Duncan Sands671fa972008-05-07 19:11:09 +00003732
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003733 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003734 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003735 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003736 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
Chris Lattner266c7bb2009-04-13 05:44:34 +00003737
3738 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
3739 "Should not emit 'available externally' functions at all");
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003740
Jim Laskeybacd3042007-02-21 22:48:45 +00003741 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3742
3743 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003744 // If the corresponding function is static, this should not be
3745 // externally visible.
Rafael Espindolabb46f522009-01-15 20:18:42 +00003746 if (linkage != Function::InternalLinkage &&
Devang Patel2d1768c2009-01-17 08:05:14 +00003747 linkage != Function::PrivateLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003748 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3749 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3750 }
3751
Dale Johannesen038129d2008-01-10 02:03:30 +00003752 // If corresponding function is weak definition, this should be too.
Duncan Sands667d4b82009-03-07 15:45:40 +00003753 if ((linkage == Function::WeakAnyLinkage ||
3754 linkage == Function::WeakODRLinkage ||
3755 linkage == Function::LinkOnceAnyLinkage ||
3756 linkage == Function::LinkOnceODRLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00003757 TAI->getWeakDefDirective())
3758 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3759
3760 // If there are no calls then you can't unwind. This may mean we can
3761 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003762 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00003763 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00003764 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00003765 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00003766 !UnwindTablesMandatory &&
Duncan Sands667d4b82009-03-07 15:45:40 +00003767 ((linkage != Function::WeakAnyLinkage &&
3768 linkage != Function::WeakODRLinkage &&
3769 linkage != Function::LinkOnceAnyLinkage &&
3770 linkage != Function::LinkOnceODRLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00003771 !TAI->getWeakDefDirective() ||
3772 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003773 {
Bill Wendling6e198962007-09-18 01:47:22 +00003774 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003775 // This name has no connection to the function, so it might get
3776 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003777 // dead-stripping unconditionally.
3778 if (const char *UsedDirective = TAI->getUsedDirective())
3779 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00003780 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00003781 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003782
Jim Laskeybacd3042007-02-21 22:48:45 +00003783 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003784 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3785 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003786 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003787
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003788 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003789
Bill Wendling722f5f12008-12-24 08:05:17 +00003790 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3791 PrintRelDirective(true, true);
3792 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3793
3794 if (!TAI->isAbsoluteEHSectionOffsets())
3795 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3796 } else {
3797 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3798 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3799 true, true, false);
3800 }
3801
Jim Laskeybacd3042007-02-21 22:48:45 +00003802 Asm->EOL("FDE CIE offset");
3803
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003804 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003805 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003806 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003807 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003808 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00003809
Jim Laskey3f09fc22007-02-28 18:38:31 +00003810 // If there is a personality and landing pads then point to the language
3811 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003812 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00003813 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00003814 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00003815
3816 if (EHFrameInfo.hasLandingPads)
3817 EmitReference("exception", EHFrameInfo.Number, true, true);
3818 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00003819 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00003820 Asm->EOL("Language Specific Data Area");
3821 } else {
3822 Asm->EmitULEB128Bytes(0);
3823 Asm->EOL("Augmentation size");
3824 }
Duncan Sands671fa972008-05-07 19:11:09 +00003825
Jim Laskeybacd3042007-02-21 22:48:45 +00003826 // Indicate locations of function specific callee saved registers in
3827 // frame.
Devang Patel5aac3d32009-01-17 08:01:33 +00003828 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00003829 true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003830
Dale Johannesen21d972a2008-04-30 00:43:29 +00003831 // On Darwin the linker honors the alignment of eh_frame, which means it
3832 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3833 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003834 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003835 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003836 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003837
3838 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003839 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003840 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003841 // that depends on unused functions (calling undefined externals) being
3842 // dead-stripped to link correctly. Yes, there really is.
3843 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3844 if (const char *UsedDirective = TAI->getUsedDirective())
3845 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3846 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003847 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003848
Duncan Sands57810cd2007-09-05 11:27:52 +00003849 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003850 ///
3851 /// The general organization of the table is complex, but the basic concepts
3852 /// are easy. First there is a header which describes the location and
3853 /// organization of the three components that follow.
3854 /// 1. The landing pad site information describes the range of code covered
3855 /// by the try. In our case it's an accumulation of the ranges covered
3856 /// by the invokes in the try. There is also a reference to the landing
3857 /// pad that handles the exception once processed. Finally an index into
3858 /// the actions table.
3859 /// 2. The action table, in our case, is composed of pairs of type ids
3860 /// and next action offset. Starting with the action index from the
3861 /// landing pad site, each type Id is checked for a match to the current
3862 /// exception. If it matches then the exception and type id are passed
3863 /// on to the landing pad. Otherwise the next action is looked up. This
3864 /// chain is terminated with a next action of zero. If no type id is
3865 /// found the the frame is unwound and handling continues.
3866 /// 3. Type id table contains references to all the C++ typeinfo for all
3867 /// catches in the function. This tables is reversed indexed base 1.
3868
Duncan Sandsb32edb42007-06-06 15:37:31 +00003869 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3870 static unsigned SharedTypeIds(const LandingPadInfo *L,
3871 const LandingPadInfo *R) {
3872 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003873 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003874 unsigned MinSize = LSize < RSize ? LSize : RSize;
3875 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003876
Duncan Sandsb32edb42007-06-06 15:37:31 +00003877 for (; Count != MinSize; ++Count)
3878 if (LIds[Count] != RIds[Count])
3879 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003880
Duncan Sandsb32edb42007-06-06 15:37:31 +00003881 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003882 }
3883
Duncan Sandsb32edb42007-06-06 15:37:31 +00003884 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00003885 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003886 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003887 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003888 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003889
Duncan Sandsb32edb42007-06-06 15:37:31 +00003890 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00003891 if (LIds[i] != RIds[i])
3892 return LIds[i] < RIds[i];
3893
Duncan Sandsb32edb42007-06-06 15:37:31 +00003894 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003895 }
3896
Duncan Sands53c3a332007-05-16 12:12:23 +00003897 struct KeyInfo {
3898 static inline unsigned getEmptyKey() { return -1U; }
3899 static inline unsigned getTombstoneKey() { return -2U; }
3900 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00003901 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00003902 static bool isPod() { return true; }
3903 };
3904
Duncan Sands57810cd2007-09-05 11:27:52 +00003905 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003906 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003907 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003908 int NextAction;
3909 struct ActionEntry *Previous;
3910 };
3911
Duncan Sands57810cd2007-09-05 11:27:52 +00003912 /// PadRange - Structure holding a try-range and the associated landing pad.
3913 struct PadRange {
3914 // The index of the landing pad.
3915 unsigned PadIndex;
3916 // The index of the begin and end labels in the landing pad's label lists.
3917 unsigned RangeIndex;
3918 };
3919
3920 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3921
3922 /// CallSiteEntry - Structure describing an entry in the call-site table.
3923 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00003924 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003925 unsigned BeginLabel; // zero indicates the start of the function.
3926 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00003927 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003928 unsigned PadLabel; // zero indicates that there is no landing pad.
3929 unsigned Action;
3930 };
3931
Jim Laskeybacd3042007-02-21 22:48:45 +00003932 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00003933 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00003934 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00003935 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3936 if (PadInfos.empty()) return;
3937
3938 // Sort the landing pads in order of their type ids. This is used to fold
3939 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00003940 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00003941 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00003942 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00003943 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003944 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3945
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003946 // Negative type ids index into FilterIds, positive type ids index into
3947 // TypeInfos. The value written for a positive type id is just the type
3948 // id itself. For a negative type id, however, the value written is the
3949 // (negative) byte offset of the corresponding FilterIds entry. The byte
3950 // offset is usually equal to the type id, because the FilterIds entries
3951 // are written using a variable width encoding which outputs one byte per
3952 // entry as long as the value written is not too large, but can differ.
3953 // This kind of complication does not occur for positive type ids because
3954 // type infos are output using a fixed width encoding.
3955 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3956 SmallVector<int, 16> FilterOffsets;
3957 FilterOffsets.reserve(FilterIds.size());
3958 int Offset = -1;
3959 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3960 E = FilterIds.end(); I != E; ++I) {
3961 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003962 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003963 }
3964
Duncan Sands57810cd2007-09-05 11:27:52 +00003965 // Compute the actions table and gather the first action index for each
3966 // landing pad site.
3967 SmallVector<ActionEntry, 32> Actions;
3968 SmallVector<unsigned, 64> FirstActions;
3969 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00003970
Duncan Sandsb32edb42007-06-06 15:37:31 +00003971 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00003972 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003973 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003974 const LandingPadInfo *LP = LandingPads[i];
3975 const std::vector<int> &TypeIds = LP->TypeIds;
3976 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003977 unsigned SizeSiteActions = 0;
3978
Duncan Sandsb32edb42007-06-06 15:37:31 +00003979 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00003980 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00003981 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003982
Duncan Sandsb32edb42007-06-06 15:37:31 +00003983 if (NumShared) {
3984 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3985 assert(Actions.size());
3986 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003987 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3988 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003989 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003990 SizeAction -=
3991 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003992 SizeAction += -PrevAction->NextAction;
3993 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003994 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00003995 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003996
Duncan Sandsb32edb42007-06-06 15:37:31 +00003997 // Compute the actions.
3998 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3999 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004000 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
4001 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004002 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004003
4004 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004005 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004006 SizeSiteActions += SizeAction;
4007
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004008 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00004009 Actions.push_back(Action);
4010
4011 PrevAction = &Actions.back();
4012 }
4013
4014 // Record the first action of the landing pad site.
4015 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
4016 } // else identical - re-use previous FirstAction
4017
4018 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00004019
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00004020 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00004021 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00004022 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004023
Duncan Sands481dc722007-12-19 07:36:31 +00004024 // Compute the call-site table. The entry for an invoke has a try-range
4025 // containing the call, a non-zero landing pad and an appropriate action.
4026 // The entry for an ordinary call has a try-range containing the call and
4027 // zero for the landing pad and the action. Calls marked 'nounwind' have
4028 // no entry and must not be contained in the try-range of any entry - they
4029 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004030 SmallVector<CallSiteEntry, 64> CallSites;
4031
4032 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00004033 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
4034 // by try-range labels when lowered). Ordinary calls do not, so appropriate
4035 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00004036 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
4037 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00004038 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004039 unsigned BeginLabel = LandingPad->BeginLabels[j];
4040 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
4041 PadRange P = { i, j };
4042 PadMap[BeginLabel] = P;
4043 }
4044 }
4045
Duncan Sands481dc722007-12-19 07:36:31 +00004046 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00004047 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00004048
4049 // Whether there is a potentially throwing instruction (currently this means
4050 // an ordinary call) between the end of the previous try-range and now.
4051 bool SawPotentiallyThrowing = false;
4052
4053 // Whether the last callsite entry was for an invoke.
4054 bool PreviousIsInvoke = false;
4055
Duncan Sands481dc722007-12-19 07:36:31 +00004056 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004057 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
4058 I != E; ++I) {
4059 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
4060 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00004061 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00004062 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00004063 continue;
4064 }
4065
Chris Lattner9e330492007-12-30 20:50:28 +00004066 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00004067 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00004068
Duncan Sands481dc722007-12-19 07:36:31 +00004069 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00004070 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00004071 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004072
Duncan Sands481dc722007-12-19 07:36:31 +00004073 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00004074 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00004075 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00004076 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00004077 continue;
4078
4079 PadRange P = L->second;
4080 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
4081
4082 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
4083 "Inconsistent landing pad map!");
4084
4085 // If some instruction between the previous try-range and this one may
4086 // throw, create a call-site entry with no landing pad for the region
4087 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00004088 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004089 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
4090 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00004091 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004092 }
4093
4094 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00004095 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00004096
Duncan Sands481dc722007-12-19 07:36:31 +00004097 if (LandingPad->LandingPadLabel) {
4098 // This try-range is for an invoke.
4099 CallSiteEntry Site = {BeginLabel, LastLabel,
4100 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00004101
Duncan Sands481dc722007-12-19 07:36:31 +00004102 // Try to merge with the previous call-site.
4103 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00004104 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00004105 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
4106 // Extend the range of the previous entry.
4107 Prev.EndLabel = Site.EndLabel;
4108 continue;
4109 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004110 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004111
Duncan Sands481dc722007-12-19 07:36:31 +00004112 // Otherwise, create a new call-site.
4113 CallSites.push_back(Site);
4114 PreviousIsInvoke = true;
4115 } else {
4116 // Create a gap.
4117 PreviousIsInvoke = false;
4118 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004119 }
4120 }
4121 // If some instruction between the previous try-range and the end of the
4122 // function may throw, create a call-site entry with no landing pad for the
4123 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00004124 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004125 CallSiteEntry Site = {LastLabel, 0, 0, 0};
4126 CallSites.push_back(Site);
4127 }
4128
Jim Laskeybacd3042007-02-21 22:48:45 +00004129 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00004130
4131 // Call sites.
4132 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
4133 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
4134 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
4135 unsigned SizeSites = CallSites.size() * (SiteStartSize +
4136 SiteLengthSize +
4137 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00004138 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004139 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00004140
Duncan Sands671fa972008-05-07 19:11:09 +00004141 // Type infos.
4142 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
4143 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004144
4145 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004146 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004147 SizeSites + SizeActions + SizeTypes;
4148
4149 unsigned TotalSize = sizeof(int8_t) + // LPStart format
4150 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004151 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004152 TypeOffset;
4153
4154 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004155
Jim Laskeybacd3042007-02-21 22:48:45 +00004156 // Begin the exception table.
4157 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng05548eb2008-02-29 19:36:59 +00004158 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesend65b2642008-10-08 21:50:21 +00004159 O << "GCC_except_table" << SubprogramCount << ":\n";
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004160 for (unsigned i = 0; i != SizeAlign; ++i) {
4161 Asm->EmitInt8(0);
4162 Asm->EOL("Padding");
4163 }
Jim Laskeybacd3042007-02-21 22:48:45 +00004164 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004165
Jim Laskeybacd3042007-02-21 22:48:45 +00004166 // Emit the header.
4167 Asm->EmitInt8(DW_EH_PE_omit);
4168 Asm->EOL("LPStart format (DW_EH_PE_omit)");
4169 Asm->EmitInt8(DW_EH_PE_absptr);
4170 Asm->EOL("TType format (DW_EH_PE_absptr)");
4171 Asm->EmitULEB128Bytes(TypeOffset);
4172 Asm->EOL("TType base offset");
4173 Asm->EmitInt8(DW_EH_PE_udata4);
4174 Asm->EOL("Call site format (DW_EH_PE_udata4)");
4175 Asm->EmitULEB128Bytes(SizeSites);
4176 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004177
Duncan Sands57810cd2007-09-05 11:27:52 +00004178 // Emit the landing pad site information.
4179 for (unsigned i = 0; i < CallSites.size(); ++i) {
4180 CallSiteEntry &S = CallSites[i];
4181 const char *BeginTag;
4182 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00004183
Duncan Sands57810cd2007-09-05 11:27:52 +00004184 if (!S.BeginLabel) {
4185 BeginTag = "eh_func_begin";
4186 BeginNumber = SubprogramCount;
4187 } else {
4188 BeginTag = "label";
4189 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00004190 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004191
Duncan Sands57810cd2007-09-05 11:27:52 +00004192 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004193 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004194 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00004195
Duncan Sands57810cd2007-09-05 11:27:52 +00004196 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00004197 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00004198 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004199 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00004200 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004201 }
4202 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004203
Duncan Sands671fa972008-05-07 19:11:09 +00004204 if (!S.PadLabel)
4205 Asm->EmitInt32(0);
4206 else
Duncan Sands57810cd2007-09-05 11:27:52 +00004207 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004208 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004209 Asm->EOL("Landing pad");
4210
4211 Asm->EmitULEB128Bytes(S.Action);
4212 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004213 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004214
Jim Laskeybacd3042007-02-21 22:48:45 +00004215 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004216 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4217 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00004218
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004219 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004220 Asm->EOL("TypeInfo index");
4221 Asm->EmitSLEB128Bytes(Action.NextAction);
4222 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004223 }
4224
4225 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00004226 for (unsigned M = TypeInfos.size(); M; --M) {
4227 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00004228
4229 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00004230
Bill Wendling7d16e852009-04-10 00:12:49 +00004231 if (GV) {
4232 std::string GLN;
4233 O << Asm->getGlobalLinkName(GV, GLN);
4234 } else {
Jim Laskeybacd3042007-02-21 22:48:45 +00004235 O << "0";
Bill Wendling7d16e852009-04-10 00:12:49 +00004236 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004237
Jim Laskeybacd3042007-02-21 22:48:45 +00004238 Asm->EOL("TypeInfo");
4239 }
4240
Duncan Sands73ef58a2007-06-02 16:53:42 +00004241 // Emit the filter typeids.
4242 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4243 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00004244 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00004245 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00004246 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004247
Evan Cheng05548eb2008-02-29 19:36:59 +00004248 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004249 }
4250
Jim Laskey072200c2007-01-29 18:51:14 +00004251public:
4252 //===--------------------------------------------------------------------===//
4253 // Main entry points.
4254 //
Owen Andersoncb371882008-08-21 00:14:44 +00004255 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00004256 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
4257 shouldEmitTableModule(false), shouldEmitMovesModule(false),
4258 ExceptionTimer(0) {
4259 if (TimePassesIsEnabled)
4260 ExceptionTimer = new Timer("Dwarf Exception Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00004261 getDwarfTimerGroup());
Bill Wendling163ba3f2009-03-10 21:23:25 +00004262 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004263
Bill Wendling163ba3f2009-03-10 21:23:25 +00004264 virtual ~DwarfException() {
4265 delete ExceptionTimer;
4266 }
Jim Laskey072200c2007-01-29 18:51:14 +00004267
4268 /// SetModuleInfo - Set machine module information when it's known that pass
4269 /// manager has created it. Set by the target AsmPrinter.
4270 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00004271 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00004272 }
4273
4274 /// BeginModule - Emit all exception information that should come prior to the
4275 /// content.
4276 void BeginModule(Module *M) {
4277 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00004278 }
4279
4280 /// EndModule - Emit all exception information that should come after the
4281 /// content.
4282 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004283 if (TimePassesIsEnabled)
4284 ExceptionTimer->startTimer();
4285
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004286 if (shouldEmitMovesModule || shouldEmitTableModule) {
4287 const std::vector<Function *> Personalities = MMI->getPersonalities();
Evan Chenge3d42322009-02-25 07:04:34 +00004288 for (unsigned i = 0; i < Personalities.size(); ++i)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004289 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00004290
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004291 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4292 E = EHFrames.end(); I != E; ++I)
4293 EmitEHFrame(*I);
4294 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004295
4296 if (TimePassesIsEnabled)
4297 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004298 }
4299
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004300 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00004301 /// emitted immediately after the function entry point.
4302 void BeginFunction(MachineFunction *MF) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004303 if (TimePassesIsEnabled)
4304 ExceptionTimer->startTimer();
4305
Jim Laskey072200c2007-01-29 18:51:14 +00004306 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004307 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004308
Bill Wendling163ba3f2009-03-10 21:23:25 +00004309 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004310 // Map all labels and get rid of any dead landing pads.
4311 MMI->TidyLandingPads();
Bill Wendling163ba3f2009-03-10 21:23:25 +00004312
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004313 // If any landing pads survive, we need an EH table.
4314 if (MMI->getLandingPads().size())
4315 shouldEmitTable = true;
4316
4317 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00004318 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004319 shouldEmitMoves = true;
4320
4321 if (shouldEmitMoves || shouldEmitTable)
4322 // Assumes in correct section after the entry point.
4323 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00004324 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004325
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004326 shouldEmitTableModule |= shouldEmitTable;
4327 shouldEmitMovesModule |= shouldEmitMoves;
Bill Wendling163ba3f2009-03-10 21:23:25 +00004328
4329 if (TimePassesIsEnabled)
4330 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004331 }
4332
4333 /// EndFunction - Gather and emit post-function exception information.
4334 ///
4335 void EndFunction() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004336 if (TimePassesIsEnabled)
4337 ExceptionTimer->startTimer();
4338
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004339 if (shouldEmitMoves || shouldEmitTable) {
4340 EmitLabel("eh_func_end", SubprogramCount);
4341 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00004342
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004343 // Save EH frame information
Bill Wendling7d16e852009-04-10 00:12:49 +00004344 std::string Name;
4345 EHFrames.push_back(
4346 FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF, Name),
4347 SubprogramCount,
4348 MMI->getPersonalityIndex(),
4349 MF->getFrameInfo()->hasCalls(),
4350 !MMI->getLandingPads().empty(),
4351 MMI->getFrameMoves(),
4352 MF->getFunction()));
Bill Wendling163ba3f2009-03-10 21:23:25 +00004353 }
4354
4355 if (TimePassesIsEnabled)
4356 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004357 }
4358};
4359
Jim Laskey0d086af2006-02-27 12:43:29 +00004360} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00004361
4362//===----------------------------------------------------------------------===//
4363
Jim Laskeyd18e2892006-01-20 20:34:06 +00004364/// Emit - Print the abbreviation using the specified Dwarf writer.
4365///
Jim Laskey072200c2007-01-29 18:51:14 +00004366void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004367 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00004368 DD.getAsm()->EmitULEB128Bytes(Tag);
4369 DD.getAsm()->EOL(TagString(Tag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004370
Jim Laskeyd18e2892006-01-20 20:34:06 +00004371 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00004372 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4373 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004374
Jim Laskeyd18e2892006-01-20 20:34:06 +00004375 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00004376 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004377 const DIEAbbrevData &AttrData = Data[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004378
Jim Laskeyd18e2892006-01-20 20:34:06 +00004379 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00004380 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4381 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004382
Jim Laskeyd18e2892006-01-20 20:34:06 +00004383 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00004384 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4385 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00004386 }
4387
4388 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00004389 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4390 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00004391}
4392
4393#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00004394void DIEAbbrev::print(std::ostream &O) {
4395 O << "Abbreviation @"
4396 << std::hex << (intptr_t)this << std::dec
4397 << " "
4398 << TagString(Tag)
4399 << " "
4400 << ChildrenString(ChildrenFlag)
4401 << "\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004402
Jim Laskeya0f3d172006-09-07 22:06:40 +00004403 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4404 O << " "
4405 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004406 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00004407 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004408 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00004409 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00004410}
Bill Wendlinge8156192006-12-07 01:30:32 +00004411void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00004412#endif
4413
4414//===----------------------------------------------------------------------===//
4415
Jim Laskeyef42a012006-11-02 20:12:39 +00004416#ifndef NDEBUG
4417void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004418 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004419}
Jim Laskeyef42a012006-11-02 20:12:39 +00004420#endif
4421
4422//===----------------------------------------------------------------------===//
4423
Jim Laskey063e7652006-01-17 17:31:53 +00004424/// EmitValue - Emit integer of appropriate size.
4425///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004426void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00004427 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00004428 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00004429 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004430 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004431 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004432 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004433 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004434 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004435 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004436 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4437 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4438 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004439 default: assert(0 && "DIE Value form not supported yet"); break;
4440 }
4441}
4442
4443/// SizeOf - Determine size of integer value in bytes.
4444///
Jim Laskey072200c2007-01-29 18:51:14 +00004445unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004446 switch (Form) {
4447 case DW_FORM_flag: // Fall thru
4448 case DW_FORM_ref1: // Fall thru
4449 case DW_FORM_data1: return sizeof(int8_t);
4450 case DW_FORM_ref2: // Fall thru
4451 case DW_FORM_data2: return sizeof(int16_t);
4452 case DW_FORM_ref4: // Fall thru
4453 case DW_FORM_data4: return sizeof(int32_t);
4454 case DW_FORM_ref8: // Fall thru
4455 case DW_FORM_data8: return sizeof(int64_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004456 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4457 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00004458 default: assert(0 && "DIE Value form not supported yet"); break;
4459 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004460 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00004461}
4462
Jim Laskey063e7652006-01-17 17:31:53 +00004463//===----------------------------------------------------------------------===//
4464
4465/// EmitValue - Emit string value.
4466///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004467void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Bill Wendlingdd446322009-03-10 23:57:09 +00004468 DD.getAsm()->EmitString(Str);
Jim Laskey063e7652006-01-17 17:31:53 +00004469}
4470
Jim Laskey063e7652006-01-17 17:31:53 +00004471//===----------------------------------------------------------------------===//
4472
4473/// EmitValue - Emit label value.
4474///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004475void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004476 bool IsSmall = Form == DW_FORM_data4;
4477 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004478}
4479
4480/// SizeOf - Determine size of label value in bytes.
4481///
Jim Laskey072200c2007-01-29 18:51:14 +00004482unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004483 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004484 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004485}
Jim Laskeyef42a012006-11-02 20:12:39 +00004486
Jim Laskey063e7652006-01-17 17:31:53 +00004487//===----------------------------------------------------------------------===//
4488
Jim Laskeyd18e2892006-01-20 20:34:06 +00004489/// EmitValue - Emit label value.
4490///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004491void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004492 bool IsSmall = Form == DW_FORM_data4;
4493 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00004494}
4495
4496/// SizeOf - Determine size of label value in bytes.
4497///
Jim Laskey072200c2007-01-29 18:51:14 +00004498unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004499 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004500 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00004501}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004502
Jim Laskeyd18e2892006-01-20 20:34:06 +00004503//===----------------------------------------------------------------------===//
4504
Jim Laskey063e7652006-01-17 17:31:53 +00004505/// EmitValue - Emit delta value.
4506///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004507void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4508 bool IsSmall = Form == DW_FORM_data4;
4509 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4510 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4511}
4512
4513/// SizeOf - Determine size of delta value in bytes.
4514///
4515unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4516 if (Form == DW_FORM_data4) return 4;
4517 return DD.getTargetData()->getPointerSize();
4518}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004519
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004520//===----------------------------------------------------------------------===//
4521
4522/// EmitValue - Emit delta value.
4523///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004524void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004525 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00004526 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004527}
4528
4529/// SizeOf - Determine size of delta value in bytes.
4530///
Jim Laskey072200c2007-01-29 18:51:14 +00004531unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004532 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004533 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004534}
4535
4536//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004537
Jim Laskeyb8509c52006-03-23 18:07:55 +00004538/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004539///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004540void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004541 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00004542}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004543
Jim Laskeyd18e2892006-01-20 20:34:06 +00004544//===----------------------------------------------------------------------===//
4545
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004546/// ComputeSize - calculate the size of the block.
4547///
Jim Laskey072200c2007-01-29 18:51:14 +00004548unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00004549 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00004550 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004551
Jim Laskeyef42a012006-11-02 20:12:39 +00004552 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00004553 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00004554 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004555 }
4556 return Size;
4557}
4558
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004559/// EmitValue - Emit block data.
4560///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004561void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004562 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004563 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4564 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4565 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4566 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004567 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004568 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004569
Owen Anderson873e1b52008-06-24 21:44:59 +00004570 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00004571
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004572 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00004573 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00004574 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004575 }
4576}
4577
4578/// SizeOf - Determine size of block data in bytes.
4579///
Jim Laskey072200c2007-01-29 18:51:14 +00004580unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004581 switch (Form) {
4582 case DW_FORM_block1: return Size + sizeof(int8_t);
4583 case DW_FORM_block2: return Size + sizeof(int16_t);
4584 case DW_FORM_block4: return Size + sizeof(int32_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004585 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004586 default: assert(0 && "Improper form for block"); break;
4587 }
4588 return 0;
4589}
4590
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004591//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004592/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00004593
4594DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00004595 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00004596 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00004597}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004598
Jim Laskeyb8509c52006-03-23 18:07:55 +00004599/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4600///
4601void DIE::AddSiblingOffset() {
4602 DIEInteger *DI = new DIEInteger(0);
4603 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00004604 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00004605}
4606
Jim Laskeyef42a012006-11-02 20:12:39 +00004607/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004608///
Jim Laskeyef42a012006-11-02 20:12:39 +00004609void DIE::Profile(FoldingSetNodeID &ID) {
4610 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004611
Jim Laskeyef42a012006-11-02 20:12:39 +00004612 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4613 ID.AddPointer(Children[i]);
4614
4615 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4616 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00004617}
Jim Laskeyef42a012006-11-02 20:12:39 +00004618
4619#ifndef NDEBUG
4620void DIE::print(std::ostream &O, unsigned IncIndent) {
4621 static unsigned IndentCount = 0;
4622 IndentCount += IncIndent;
4623 const std::string Indent(IndentCount, ' ');
4624 bool isBlock = Abbrev.getTag() == 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004625
Jim Laskeyef42a012006-11-02 20:12:39 +00004626 if (!isBlock) {
4627 O << Indent
4628 << "Die: "
4629 << "0x" << std::hex << (intptr_t)this << std::dec
4630 << ", Offset: " << Offset
4631 << ", Size: " << Size
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004632 << "\n";
4633
Jim Laskeyef42a012006-11-02 20:12:39 +00004634 O << Indent
4635 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00004636 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00004637 << ChildrenString(Abbrev.getChildrenFlag());
4638 } else {
4639 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00004640 }
4641 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00004642
Owen Anderson873e1b52008-06-24 21:44:59 +00004643 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004644
Jim Laskeyef42a012006-11-02 20:12:39 +00004645 IndentCount += 2;
4646 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4647 O << Indent;
Bill Wendling4e974012008-07-22 00:28:47 +00004648
4649 if (!isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +00004650 O << AttributeString(Data[i].getAttribute());
Bill Wendling4e974012008-07-22 00:28:47 +00004651 else
Jim Laskeyef42a012006-11-02 20:12:39 +00004652 O << "Blk[" << i << "]";
Bill Wendling4e974012008-07-22 00:28:47 +00004653
Jim Laskeyef42a012006-11-02 20:12:39 +00004654 O << " "
4655 << FormEncodingString(Data[i].getForm())
4656 << " ";
4657 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00004658 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00004659 }
Jim Laskeyef42a012006-11-02 20:12:39 +00004660 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00004661
Jim Laskeyef42a012006-11-02 20:12:39 +00004662 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4663 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00004664 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004665
Jim Laskeyef42a012006-11-02 20:12:39 +00004666 if (!isBlock) O << "\n";
4667 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00004668}
4669
Jim Laskeyef42a012006-11-02 20:12:39 +00004670void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004671 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00004672}
Jim Laskeybd761842006-02-27 17:27:12 +00004673#endif
Jim Laskey65195462006-10-30 13:35:07 +00004674
4675//===----------------------------------------------------------------------===//
4676/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00004677///
Jim Laskey65195462006-10-30 13:35:07 +00004678
Bill Wendling91b8b802009-03-10 20:41:52 +00004679DwarfWriter::DwarfWriter()
Bill Wendling163ba3f2009-03-10 21:23:25 +00004680 : ImmutablePass(&ID), DD(0), DE(0) {}
Jim Laskey65195462006-10-30 13:35:07 +00004681
4682DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00004683 delete DE;
4684 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00004685}
4686
Jim Laskey65195462006-10-30 13:35:07 +00004687/// BeginModule - Emit all Dwarf sections that should come prior to the
4688/// content.
Devang Pateleb3fc282009-01-08 23:40:34 +00004689void DwarfWriter::BeginModule(Module *M,
4690 MachineModuleInfo *MMI,
4691 raw_ostream &OS, AsmPrinter *A,
4692 const TargetAsmInfo *T) {
4693 DE = new DwarfException(OS, A, T);
4694 DD = new DwarfDebug(OS, A, T);
Jim Laskey072200c2007-01-29 18:51:14 +00004695 DE->BeginModule(M);
4696 DD->BeginModule(M);
Devang Patel7bb89ed2009-01-13 00:20:51 +00004697 DD->SetDebugInfo(MMI);
Devang Pateleb3fc282009-01-08 23:40:34 +00004698 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00004699}
4700
4701/// EndModule - Emit all Dwarf sections that should come after the content.
4702///
4703void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00004704 DE->EndModule();
4705 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00004706}
4707
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004708/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00004709/// emitted immediately after the function entry point.
4710void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00004711 DE->BeginFunction(MF);
4712 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00004713}
4714
4715/// EndFunction - Gather and emit post-function debug information.
4716///
Bill Wendlingd751c642008-09-26 00:28:12 +00004717void DwarfWriter::EndFunction(MachineFunction *MF) {
4718 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004719 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004720
Bill Wendlingc91d0b92008-07-22 00:53:37 +00004721 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00004722 // Clear function debug information.
4723 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00004724}
Devang Patelccca7fe2009-01-12 19:17:34 +00004725
4726/// RecordSourceLine - Records location information and associates it with a
4727/// label. Returns a unique label ID used to generate a label and provide
4728/// correspondence to the source line list.
4729unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00004730 DICompileUnit CU) {
4731 return DD->RecordSourceLine(Line, Col, CU);
Devang Patelccca7fe2009-01-12 19:17:34 +00004732}
4733
4734/// RecordRegionStart - Indicate the start of a region.
4735unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004736 return DD->RecordRegionStart(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004737}
4738
4739/// RecordRegionEnd - Indicate the end of a region.
Dan Gohman9a38e3e2009-05-07 19:46:24 +00004740unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
4741 return DD->RecordRegionEnd(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004742}
4743
4744/// getRecordSourceLineCount - Count source lines.
4745unsigned DwarfWriter::getRecordSourceLineCount() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004746 return DD->getRecordSourceLineCount();
Devang Patelccca7fe2009-01-12 19:17:34 +00004747}
Devang Patelbb8c5952009-01-13 21:25:00 +00004748
Devang Patelc48c5502009-01-13 21:44:10 +00004749/// RecordVariable - Indicate the declaration of a local variable.
4750///
Devang Patel1be3ecc2009-04-15 00:10:26 +00004751void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
4752 const MachineInstr *MI) {
4753 DD->RecordVariable(GV, FrameIndex, MI);
Devang Patelc48c5502009-01-13 21:44:10 +00004754}
Devang Patelbbdc8202009-01-13 23:54:55 +00004755
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004756/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
4757/// be emitted.
4758bool DwarfWriter::ShouldEmitDwarfDebug() const {
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +00004759 return DD && DD->ShouldEmitDwarfDebug();
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004760}
Devang Patel0f7fef32009-04-13 17:02:03 +00004761
Devang Patel1be3ecc2009-04-15 00:10:26 +00004762//// RecordInlinedFnStart - Global variable GV is inlined at the location marked
Devang Patel0f7fef32009-04-13 17:02:03 +00004763//// by LabelID label.
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00004764unsigned DwarfWriter::RecordInlinedFnStart(DISubprogram SP, DICompileUnit CU,
4765 unsigned Line, unsigned Col) {
4766 return DD->RecordInlinedFnStart(SP, CU, Line, Col);
Devang Patel0f7fef32009-04-13 17:02:03 +00004767}
4768
Devang Patel1be3ecc2009-04-15 00:10:26 +00004769/// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00004770unsigned DwarfWriter::RecordInlinedFnEnd(DISubprogram SP) {
Devang Patel1be3ecc2009-04-15 00:10:26 +00004771 return DD->RecordInlinedFnEnd(SP);
4772}
4773
4774/// RecordVariableScope - Record scope for the variable declared by
4775/// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
4776void DwarfWriter::RecordVariableScope(DIVariable &DV,
4777 const MachineInstr *DeclareMI) {
4778 DD->RecordVariableScope(DV, DeclareMI);
4779}