blob: c4191e523ce75be9f3c1d22d76288e6f8be5039e [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//===----------------------------------------------------------------------===//
Devang Pateld1ca9252009-01-05 23:03:32 +000073/// Utility routines.
74///
Devang Patelcf3a4482009-01-15 23:41:32 +000075/// getGlobalVariable - Return either a direct or cast Global value.
76///
77static GlobalVariable *getGlobalVariable(Value *V) {
78 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
79 return GV;
80 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
81 if (CE->getOpcode() == Instruction::BitCast) {
82 return dyn_cast<GlobalVariable>(CE->getOperand(0));
83 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
84 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
85 if (!CE->getOperand(i)->isNullValue())
86 return NULL;
87 }
88 return dyn_cast<GlobalVariable>(CE->getOperand(0));
89 }
90 }
91 return NULL;
92}
93
Devang Pateld1ca9252009-01-05 23:03:32 +000094//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000095/// DWLabel - Labels are used to track locations in the assembler file.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000096/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
97/// where the tag is a category of label (Ex. location) and number is a value
Reid Spencer181b6c92007-08-05 20:06:04 +000098/// unique in that category.
Jim Laskey65195462006-10-30 13:35:07 +000099class DWLabel {
100public:
Jim Laskeyef42a012006-11-02 20:12:39 +0000101 /// Tag - Label category tag. Should always be a staticly declared C string.
102 ///
103 const char *Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000104
Jim Laskeyef42a012006-11-02 20:12:39 +0000105 /// Number - Value to make label unique.
106 ///
107 unsigned Number;
Jim Laskey65195462006-10-30 13:35:07 +0000108
109 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000110
Jim Laskeyef42a012006-11-02 20:12:39 +0000111 void Profile(FoldingSetNodeID &ID) const {
Evan Chenge3d42322009-02-25 07:04:34 +0000112 ID.AddString(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +0000113 ID.AddInteger(Number);
Jim Laskey90c79d72006-03-23 23:02:34 +0000114 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000115
Jim Laskeyef42a012006-11-02 20:12:39 +0000116#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000117 void print(std::ostream *O) const {
118 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000119 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000120 void print(std::ostream &O) const {
Jim Laskeybacd3042007-02-21 22:48:45 +0000121 O << "." << Tag;
Jim Laskeyef42a012006-11-02 20:12:39 +0000122 if (Number) O << Number;
123 }
124#endif
Jim Laskeybd761842006-02-27 17:27:12 +0000125};
126
127//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000128/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
129/// Dwarf abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +0000130class DIEAbbrevData {
Jim Laskeyef42a012006-11-02 20:12:39 +0000131 /// Attribute - Dwarf attribute code.
132 ///
133 unsigned Attribute;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000134
Jim Laskeyef42a012006-11-02 20:12:39 +0000135 /// Form - Dwarf form code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000136 ///
137 unsigned Form;
Jim Laskey0d086af2006-02-27 12:43:29 +0000138public:
Bill Wendlingdd446322009-03-10 23:57:09 +0000139 DIEAbbrevData(unsigned A, unsigned F) : Attribute(A), Form(F) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000140
Jim Laskeybd761842006-02-27 17:27:12 +0000141 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000142 unsigned getAttribute() const { return Attribute; }
143 unsigned getForm() const { return Form; }
Jim Laskey063e7652006-01-17 17:31:53 +0000144
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000145 /// Profile - Used to gather unique data for the abbreviation folding set.
Jim Laskey0d086af2006-02-27 12:43:29 +0000146 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000147 void Profile(FoldingSetNodeID &ID)const {
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000148 ID.AddInteger(Attribute);
149 ID.AddInteger(Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000150 }
151};
Jim Laskey063e7652006-01-17 17:31:53 +0000152
Jim Laskey0d086af2006-02-27 12:43:29 +0000153//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000154/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
155/// information object.
156class DIEAbbrev : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000157private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000158 /// Tag - Dwarf tag code.
159 ///
160 unsigned Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000161
Jim Laskeyef42a012006-11-02 20:12:39 +0000162 /// Unique number for node.
163 ///
164 unsigned Number;
165
166 /// ChildrenFlag - Dwarf children flag.
167 ///
168 unsigned ChildrenFlag;
169
170 /// Data - Raw data bytes for abbreviation.
171 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000172 SmallVector<DIEAbbrevData, 8> Data;
Jim Laskey0d086af2006-02-27 12:43:29 +0000173public:
Bill Wendlingdd446322009-03-10 23:57:09 +0000174 DIEAbbrev(unsigned T, unsigned C) : Tag(T), ChildrenFlag(C), Data() {}
175 virtual ~DIEAbbrev() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000176
Jim Laskeybd761842006-02-27 17:27:12 +0000177 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000178 unsigned getTag() const { return Tag; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000179 unsigned getNumber() const { return Number; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000180 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000181 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000182 void setTag(unsigned T) { Tag = T; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000183 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000184 void setNumber(unsigned N) { Number = N; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000185
Jim Laskey0d086af2006-02-27 12:43:29 +0000186 /// AddAttribute - Adds another set of attribute information to the
187 /// abbreviation.
188 void AddAttribute(unsigned Attribute, unsigned Form) {
189 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000190 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000191
Jim Laskeyb8509c52006-03-23 18:07:55 +0000192 /// AddFirstAttribute - Adds a set of attribute information to the front
193 /// of the abbreviation.
194 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
195 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
196 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000197
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000198 /// Profile - Used to gather unique data for the abbreviation folding set.
199 ///
200 void Profile(FoldingSetNodeID &ID) {
201 ID.AddInteger(Tag);
202 ID.AddInteger(ChildrenFlag);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000203
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000204 // For each attribute description.
205 for (unsigned i = 0, N = Data.size(); i < N; ++i)
206 Data[i].Profile(ID);
207 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000208
Jim Laskey0d086af2006-02-27 12:43:29 +0000209 /// Emit - Print the abbreviation using the specified Dwarf writer.
210 ///
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000211 void Emit(const DwarfDebug &DD) const;
212
Jim Laskey0d086af2006-02-27 12:43:29 +0000213#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000214 void print(std::ostream *O) {
215 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000216 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000217 void print(std::ostream &O);
218 void dump();
219#endif
220};
Jim Laskey063e7652006-01-17 17:31:53 +0000221
Jim Laskey0d086af2006-02-27 12:43:29 +0000222//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000223/// DIE - A structured debug information entry. Has an abbreviation which
224/// describes it's organization.
225class DIE : public FoldingSetNode {
226protected:
227 /// Abbrev - Buffer for constructing abbreviation.
228 ///
229 DIEAbbrev Abbrev;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000230
Jim Laskeyef42a012006-11-02 20:12:39 +0000231 /// Offset - Offset in debug info section.
232 ///
233 unsigned Offset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000234
Jim Laskeyef42a012006-11-02 20:12:39 +0000235 /// Size - Size of instance + children.
236 ///
237 unsigned Size;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000238
Jim Laskeyef42a012006-11-02 20:12:39 +0000239 /// Children DIEs.
240 ///
241 std::vector<DIE *> Children;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000242
Jim Laskeyef42a012006-11-02 20:12:39 +0000243 /// Attributes values.
244 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000245 SmallVector<DIEValue*, 32> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000246
Jim Laskeyef42a012006-11-02 20:12:39 +0000247public:
Dan Gohman81975f62007-08-27 14:50:10 +0000248 explicit DIE(unsigned Tag)
Bill Wendlingdd446322009-03-10 23:57:09 +0000249 : Abbrev(Tag, DW_CHILDREN_no), Offset(0), Size(0), Children(), Values() {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000250 virtual ~DIE();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000251
Jim Laskeyef42a012006-11-02 20:12:39 +0000252 // Accessors.
253 DIEAbbrev &getAbbrev() { return Abbrev; }
254 unsigned getAbbrevNumber() const {
255 return Abbrev.getNumber();
256 }
Jim Laskey85f419b2006-11-09 16:32:26 +0000257 unsigned getTag() const { return Abbrev.getTag(); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000258 unsigned getOffset() const { return Offset; }
259 unsigned getSize() const { return Size; }
260 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000261 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000262 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
263 void setOffset(unsigned O) { Offset = O; }
264 void setSize(unsigned S) { Size = S; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000265
Jim Laskeyef42a012006-11-02 20:12:39 +0000266 /// AddValue - Add a value and attributes to a DIE.
267 ///
268 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
269 Abbrev.AddAttribute(Attribute, Form);
270 Values.push_back(Value);
271 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000272
Jim Laskeyef42a012006-11-02 20:12:39 +0000273 /// SiblingOffset - Return the offset of the debug information entry's
274 /// sibling.
275 unsigned SiblingOffset() const { return Offset + Size; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000276
Jim Laskeyef42a012006-11-02 20:12:39 +0000277 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
278 ///
279 void AddSiblingOffset();
280
281 /// AddChild - Add a child to the DIE.
282 ///
283 void AddChild(DIE *Child) {
284 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
285 Children.push_back(Child);
286 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000287
Jim Laskeyef42a012006-11-02 20:12:39 +0000288 /// Detach - Detaches objects connected to it after copying.
289 ///
290 void Detach() {
291 Children.clear();
292 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000293
Jim Laskeyef42a012006-11-02 20:12:39 +0000294 /// Profile - Used to gather unique data for the value folding set.
295 ///
296 void Profile(FoldingSetNodeID &ID) ;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000297
Jim Laskeyef42a012006-11-02 20:12:39 +0000298#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000299 void print(std::ostream *O, unsigned IncIndent = 0) {
300 if (O) print(*O, IncIndent);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000301 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000302 void print(std::ostream &O, unsigned IncIndent = 0);
303 void dump();
304#endif
305};
306
307//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000308/// DIEValue - A debug information entry value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000309///
310class DIEValue : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000311public:
312 enum {
313 isInteger,
314 isString,
315 isLabel,
316 isAsIsLabel,
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000317 isSectionOffset,
Jim Laskey0d086af2006-02-27 12:43:29 +0000318 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000319 isEntry,
320 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000321 };
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000322
Jim Laskeyef42a012006-11-02 20:12:39 +0000323 /// Type - Type of data stored in the value.
324 ///
325 unsigned Type;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000326
Bill Wendlingdd446322009-03-10 23:57:09 +0000327 explicit DIEValue(unsigned T) : Type(T) {}
Jim Laskey0d086af2006-02-27 12:43:29 +0000328 virtual ~DIEValue() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000329
Jim Laskeyf6733882006-11-02 21:48:18 +0000330 // Accessors
Jim Laskeyef42a012006-11-02 20:12:39 +0000331 unsigned getType() const { return Type; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000332
Jim Laskey0d086af2006-02-27 12:43:29 +0000333 // Implement isa/cast/dyncast.
334 static bool classof(const DIEValue *) { return true; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000335
Jim Laskey0d086af2006-02-27 12:43:29 +0000336 /// EmitValue - Emit value via the Dwarf writer.
337 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000338 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000339
Jim Laskey0d086af2006-02-27 12:43:29 +0000340 /// SizeOf - Return the size of a value in bytes.
341 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000342 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000343
Jim Laskeyef42a012006-11-02 20:12:39 +0000344 /// Profile - Used to gather unique data for the value folding set.
345 ///
346 virtual void Profile(FoldingSetNodeID &ID) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000347
Jim Laskeyef42a012006-11-02 20:12:39 +0000348#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000349 void print(std::ostream *O) {
350 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000351 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000352 virtual void print(std::ostream &O) = 0;
353 void dump();
354#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000355};
Jim Laskey063e7652006-01-17 17:31:53 +0000356
Jim Laskey0d086af2006-02-27 12:43:29 +0000357//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000358/// DWInteger - An integer value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000359///
Jim Laskey0d086af2006-02-27 12:43:29 +0000360class DIEInteger : public DIEValue {
361private:
362 uint64_t Integer;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000363
Jim Laskey0d086af2006-02-27 12:43:29 +0000364public:
Dan Gohman81975f62007-08-27 14:50:10 +0000365 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000366
Jim Laskey0d086af2006-02-27 12:43:29 +0000367 // Implement isa/cast/dyncast.
368 static bool classof(const DIEInteger *) { return true; }
369 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000370
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000371 /// BestForm - Choose the best form for integer.
372 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000373 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
374 if (IsSigned) {
375 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
376 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
377 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
378 } else {
379 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
380 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
381 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
382 }
383 return DW_FORM_data8;
384 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000385
Jim Laskey0d086af2006-02-27 12:43:29 +0000386 /// EmitValue - Emit integer of appropriate size.
387 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000388 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000389
Jim Laskey0d086af2006-02-27 12:43:29 +0000390 /// SizeOf - Determine size of integer value in bytes.
391 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000392 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000393
Jim Laskeyef42a012006-11-02 20:12:39 +0000394 /// Profile - Used to gather unique data for the value folding set.
395 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000396 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000397 ID.AddInteger(isInteger);
Jim Laskeyef42a012006-11-02 20:12:39 +0000398 ID.AddInteger(Integer);
399 }
Jim Laskey5496f012006-11-09 14:52:14 +0000400 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000401
Jim Laskeyef42a012006-11-02 20:12:39 +0000402#ifndef NDEBUG
403 virtual void print(std::ostream &O) {
404 O << "Int: " << (int64_t)Integer
405 << " 0x" << std::hex << Integer << std::dec;
406 }
407#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000408};
Jim Laskey063e7652006-01-17 17:31:53 +0000409
Jim Laskey0d086af2006-02-27 12:43:29 +0000410//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000411/// DIEString - A string value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000412///
Jim Laskeyef42a012006-11-02 20:12:39 +0000413class DIEString : public DIEValue {
Bill Wendlingdd446322009-03-10 23:57:09 +0000414 const std::string Str;
Jim Laskeyef42a012006-11-02 20:12:39 +0000415public:
Bill Wendlingdd446322009-03-10 23:57:09 +0000416 explicit DIEString(const std::string &S) : DIEValue(isString), Str(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000417
Jim Laskey0d086af2006-02-27 12:43:29 +0000418 // Implement isa/cast/dyncast.
419 static bool classof(const DIEString *) { return true; }
420 static bool classof(const DIEValue *S) { return S->Type == isString; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000421
Jim Laskey0d086af2006-02-27 12:43:29 +0000422 /// EmitValue - Emit string value.
423 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000424 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000425
Jim Laskey0d086af2006-02-27 12:43:29 +0000426 /// SizeOf - Determine size of string value in bytes.
427 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000428 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Bill Wendlingdd446322009-03-10 23:57:09 +0000429 return Str.size() + sizeof(char); // sizeof('\0');
Jim Laskeyef42a012006-11-02 20:12:39 +0000430 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000431
Jim Laskeyef42a012006-11-02 20:12:39 +0000432 /// Profile - Used to gather unique data for the value folding set.
433 ///
Bill Wendlingdd446322009-03-10 23:57:09 +0000434 static void Profile(FoldingSetNodeID &ID, const std::string &Str) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000435 ID.AddInteger(isString);
Bill Wendlingdd446322009-03-10 23:57:09 +0000436 ID.AddString(Str);
Jim Laskeyef42a012006-11-02 20:12:39 +0000437 }
Bill Wendlingdd446322009-03-10 23:57:09 +0000438 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Str); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000439
Jim Laskeyef42a012006-11-02 20:12:39 +0000440#ifndef NDEBUG
441 virtual void print(std::ostream &O) {
Bill Wendlingdd446322009-03-10 23:57:09 +0000442 O << "Str: \"" << Str << "\"";
Jim Laskeyef42a012006-11-02 20:12:39 +0000443 }
444#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000445};
Jim Laskey063e7652006-01-17 17:31:53 +0000446
Jim Laskey0d086af2006-02-27 12:43:29 +0000447//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000448/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000449//
Jim Laskeyef42a012006-11-02 20:12:39 +0000450class DIEDwarfLabel : public DIEValue {
Jim Laskey0d086af2006-02-27 12:43:29 +0000451 const DWLabel Label;
Bill Wendlingdd446322009-03-10 23:57:09 +0000452public:
Dan Gohman81975f62007-08-27 14:50:10 +0000453 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000454
Jim Laskey0d086af2006-02-27 12:43:29 +0000455 // Implement isa/cast/dyncast.
456 static bool classof(const DIEDwarfLabel *) { return true; }
457 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000458
Jim Laskey0d086af2006-02-27 12:43:29 +0000459 /// EmitValue - Emit label value.
460 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000461 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000462
Jim Laskey0d086af2006-02-27 12:43:29 +0000463 /// SizeOf - Determine size of label value in bytes.
464 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000465 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000466
Jim Laskeyef42a012006-11-02 20:12:39 +0000467 /// Profile - Used to gather unique data for the value folding set.
468 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000469 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000470 ID.AddInteger(isLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000471 Label.Profile(ID);
472 }
Jim Laskey5496f012006-11-09 14:52:14 +0000473 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000474
Jim Laskeyef42a012006-11-02 20:12:39 +0000475#ifndef NDEBUG
476 virtual void print(std::ostream &O) {
477 O << "Lbl: ";
478 Label.print(O);
479 }
480#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000481};
Jim Laskey063e7652006-01-17 17:31:53 +0000482
Jim Laskey0d086af2006-02-27 12:43:29 +0000483//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000484/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000485//
Jim Laskeyef42a012006-11-02 20:12:39 +0000486class DIEObjectLabel : public DIEValue {
Jim Laskey0d086af2006-02-27 12:43:29 +0000487 const std::string Label;
Bill Wendlingdd446322009-03-10 23:57:09 +0000488public:
Dan Gohman81975f62007-08-27 14:50:10 +0000489 explicit DIEObjectLabel(const std::string &L)
490 : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000491
Jim Laskey0d086af2006-02-27 12:43:29 +0000492 // Implement isa/cast/dyncast.
493 static bool classof(const DIEObjectLabel *) { return true; }
494 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000495
Jim Laskey0d086af2006-02-27 12:43:29 +0000496 /// EmitValue - Emit label value.
497 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000498 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000499
Jim Laskey0d086af2006-02-27 12:43:29 +0000500 /// SizeOf - Determine size of label value in bytes.
501 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000502 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000503
Jim Laskeyef42a012006-11-02 20:12:39 +0000504 /// Profile - Used to gather unique data for the value folding set.
505 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000506 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000507 ID.AddInteger(isAsIsLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000508 ID.AddString(Label);
509 }
Evan Chenge3d42322009-02-25 07:04:34 +0000510 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label.c_str()); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000511
512#ifndef NDEBUG
513 virtual void print(std::ostream &O) {
514 O << "Obj: " << Label;
515 }
516#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000517};
Jim Laskey063e7652006-01-17 17:31:53 +0000518
Jim Laskey0d086af2006-02-27 12:43:29 +0000519//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000520/// DIESectionOffset - A section offset DIE.
521//
522class DIESectionOffset : public DIEValue {
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000523 const DWLabel Label;
524 const DWLabel Section;
525 bool IsEH : 1;
526 bool UseSet : 1;
Bill Wendlingdd446322009-03-10 23:57:09 +0000527public:
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000528 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
529 bool isEH = false, bool useSet = true)
Bill Wendlingdd446322009-03-10 23:57:09 +0000530 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
531 IsEH(isEH), UseSet(useSet) {}
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000532
533 // Implement isa/cast/dyncast.
534 static bool classof(const DIESectionOffset *) { return true; }
535 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000536
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000537 /// EmitValue - Emit section offset.
538 ///
539 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000540
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000541 /// SizeOf - Determine size of section offset value in bytes.
542 ///
543 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000544
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000545 /// Profile - Used to gather unique data for the value folding set.
546 ///
547 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
548 const DWLabel &Section) {
549 ID.AddInteger(isSectionOffset);
550 Label.Profile(ID);
551 Section.Profile(ID);
552 // IsEH and UseSet are specific to the Label/Section that we will emit
553 // the offset for; so Label/Section are enough for uniqueness.
554 }
555 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
556
557#ifndef NDEBUG
558 virtual void print(std::ostream &O) {
559 O << "Off: ";
560 Label.print(O);
561 O << "-";
562 Section.print(O);
563 O << "-" << IsEH << "-" << UseSet;
564 }
565#endif
566};
567
568//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000569/// DIEDelta - A simple label difference DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000570///
Jim Laskeyef42a012006-11-02 20:12:39 +0000571class DIEDelta : public DIEValue {
Jim Laskey0d086af2006-02-27 12:43:29 +0000572 const DWLabel LabelHi;
573 const DWLabel LabelLo;
Bill Wendlingdd446322009-03-10 23:57:09 +0000574public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000575 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
Bill Wendlingdd446322009-03-10 23:57:09 +0000576 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000577
Jim Laskey0d086af2006-02-27 12:43:29 +0000578 // Implement isa/cast/dyncast.
579 static bool classof(const DIEDelta *) { return true; }
580 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000581
Jim Laskey0d086af2006-02-27 12:43:29 +0000582 /// EmitValue - Emit delta value.
583 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000584 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000585
Jim Laskey0d086af2006-02-27 12:43:29 +0000586 /// SizeOf - Determine size of delta value in bytes.
587 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000588 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000589
Jim Laskeyef42a012006-11-02 20:12:39 +0000590 /// Profile - Used to gather unique data for the value folding set.
591 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000592 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
593 const DWLabel &LabelLo) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000594 ID.AddInteger(isDelta);
Jim Laskeyef42a012006-11-02 20:12:39 +0000595 LabelHi.Profile(ID);
596 LabelLo.Profile(ID);
597 }
Jim Laskey5496f012006-11-09 14:52:14 +0000598 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000599
600#ifndef NDEBUG
601 virtual void print(std::ostream &O) {
602 O << "Del: ";
603 LabelHi.print(O);
604 O << "-";
605 LabelLo.print(O);
606 }
607#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000608};
Jim Laskey063e7652006-01-17 17:31:53 +0000609
Jim Laskey0d086af2006-02-27 12:43:29 +0000610//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000611/// DIEntry - A pointer to another debug information entry. An instance of this
612/// class can also be used as a proxy for a debug information entry not yet
613/// defined (ie. types.)
614class DIEntry : public DIEValue {
Jim Laskey0d086af2006-02-27 12:43:29 +0000615 DIE *Entry;
Bill Wendlingdd446322009-03-10 23:57:09 +0000616public:
Dan Gohman81975f62007-08-27 14:50:10 +0000617 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000618
Bill Wendlingdd446322009-03-10 23:57:09 +0000619 void setEntry(DIE *E) { Entry = E; }
620
Jim Laskey0d086af2006-02-27 12:43:29 +0000621 // Implement isa/cast/dyncast.
622 static bool classof(const DIEntry *) { return true; }
623 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000624
Jim Laskeyb8509c52006-03-23 18:07:55 +0000625 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000626 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000627 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000628
Jim Laskeyb8509c52006-03-23 18:07:55 +0000629 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000630 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000631 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000632 return sizeof(int32_t);
633 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000634
Jim Laskeyef42a012006-11-02 20:12:39 +0000635 /// Profile - Used to gather unique data for the value folding set.
636 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000637 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
638 ID.AddInteger(isEntry);
639 ID.AddPointer(Entry);
640 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000641 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000642 ID.AddInteger(isEntry);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000643
Jim Laskeyef42a012006-11-02 20:12:39 +0000644 if (Entry) {
645 ID.AddPointer(Entry);
646 } else {
647 ID.AddPointer(this);
648 }
649 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000650
Jim Laskeyef42a012006-11-02 20:12:39 +0000651#ifndef NDEBUG
652 virtual void print(std::ostream &O) {
653 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
654 }
655#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000656};
657
658//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000659/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000660//
Jim Laskeyef42a012006-11-02 20:12:39 +0000661class DIEBlock : public DIEValue, public DIE {
Bill Wendlingdd446322009-03-10 23:57:09 +0000662 unsigned Size; // Size in bytes excluding size header.
Jim Laskeyef42a012006-11-02 20:12:39 +0000663public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000664 DIEBlock()
Bill Wendlingdd446322009-03-10 23:57:09 +0000665 : DIEValue(isBlock), DIE(0), Size(0) {}
666 virtual ~DIEBlock() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000667
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000668 // Implement isa/cast/dyncast.
669 static bool classof(const DIEBlock *) { return true; }
670 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000671
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000672 /// ComputeSize - calculate the size of the block.
673 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000674 unsigned ComputeSize(DwarfDebug &DD);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000675
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000676 /// BestForm - Choose the best form for data.
677 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000678 unsigned BestForm() const {
679 if ((unsigned char)Size == Size) return DW_FORM_block1;
680 if ((unsigned short)Size == Size) return DW_FORM_block2;
681 if ((unsigned int)Size == Size) return DW_FORM_block4;
682 return DW_FORM_block;
683 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000684
685 /// EmitValue - Emit block data.
686 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000687 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000688
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000689 /// SizeOf - Determine size of block data in bytes.
690 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000691 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000692
Jim Laskeyef42a012006-11-02 20:12:39 +0000693 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000694 ///
Reid Spencer97821312006-11-02 23:56:21 +0000695 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000696 ID.AddInteger(isBlock);
Jim Laskeyef42a012006-11-02 20:12:39 +0000697 DIE::Profile(ID);
698 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000699
Jim Laskeyef42a012006-11-02 20:12:39 +0000700#ifndef NDEBUG
701 virtual void print(std::ostream &O) {
702 O << "Blk: ";
703 DIE::print(O, 5);
704 }
705#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000706};
707
708//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000709/// CompileUnit - This dwarf writer support class manages information associate
710/// with a source file.
711class CompileUnit {
Jim Laskeyef42a012006-11-02 20:12:39 +0000712 /// ID - File identifier for source.
713 ///
714 unsigned ID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000715
Jim Laskeyef42a012006-11-02 20:12:39 +0000716 /// Die - Compile unit debug information entry.
717 ///
718 DIE *Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000719
Devang Patelbbdc8202009-01-13 23:54:55 +0000720 /// GVToDieMap - Tracks the mapping of unit level debug informaton
721 /// variables to debug information entries.
Devang Patelc2997f42009-01-20 00:58:55 +0000722 std::map<GlobalVariable *, DIE *> GVToDieMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000723
Devang Patelbbdc8202009-01-13 23:54:55 +0000724 /// GVToDIEntryMap - Tracks the mapping of unit level debug informaton
Jim Laskeyef42a012006-11-02 20:12:39 +0000725 /// descriptors to debug information entries using a DIEntry proxy.
Devang Patelc2997f42009-01-20 00:58:55 +0000726 std::map<GlobalVariable *, DIEntry *> GVToDIEntryMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000727
728 /// Globals - A map of globally visible named entities for this unit.
729 ///
Bill Wendling972bbac2009-04-09 21:49:15 +0000730 StringMap<DIE*> Globals;
Jim Laskeyef42a012006-11-02 20:12:39 +0000731
732 /// DiesSet - Used to uniquely define dies within the compile unit.
733 ///
734 FoldingSet<DIE> DiesSet;
Jim Laskey0d086af2006-02-27 12:43:29 +0000735public:
Devang Pateld1ca9252009-01-05 23:03:32 +0000736 CompileUnit(unsigned I, DIE *D)
Devang Patelbbdc8202009-01-13 23:54:55 +0000737 : ID(I), Die(D), GVToDieMap(),
Devang Patel7e55b3a2009-01-17 06:51:37 +0000738 GVToDIEntryMap(), Globals(), DiesSet(InitDiesSetSize)
Devang Pateld1ca9252009-01-05 23:03:32 +0000739 {}
740
Jim Laskeyef42a012006-11-02 20:12:39 +0000741 ~CompileUnit() {
742 delete Die;
Jim Laskeyef42a012006-11-02 20:12:39 +0000743 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000744
Jim Laskeybd761842006-02-27 17:27:12 +0000745 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000746 unsigned getID() const { return ID; }
747 DIE* getDie() const { return Die; }
Bill Wendling972bbac2009-04-09 21:49:15 +0000748 StringMap<DIE*> &getGlobals() { return Globals; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000749
750 /// hasContent - Return true if this compile unit has something to write out.
751 ///
752 bool hasContent() const {
753 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000754 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000755
756 /// AddGlobal - Add a new global entity to the compile unit.
757 ///
758 void AddGlobal(const std::string &Name, DIE *Die) {
759 Globals[Name] = Die;
760 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000761
Jim Laskeyef42a012006-11-02 20:12:39 +0000762 /// getDieMapSlotFor - Returns the debug information entry map slot for the
Devang Patelbbdc8202009-01-13 23:54:55 +0000763 /// specified debug variable.
Devang Patel48d190f2009-01-05 21:47:57 +0000764 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
765 return GVToDieMap[GV];
766 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000767
Jim Laskeyef42a012006-11-02 20:12:39 +0000768 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
Devang Patelbbdc8202009-01-13 23:54:55 +0000769 /// specified debug variable.
Devang Patel48d190f2009-01-05 21:47:57 +0000770 DIEntry *&getDIEntrySlotFor(GlobalVariable *GV) {
771 return GVToDIEntryMap[GV];
772 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000773
Jim Laskeyef42a012006-11-02 20:12:39 +0000774 /// AddDie - Adds or interns the DIE to the compile unit.
775 ///
776 DIE *AddDie(DIE &Buffer) {
777 FoldingSetNodeID ID;
778 Buffer.Profile(ID);
779 void *Where;
780 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000781
Jim Laskeyef42a012006-11-02 20:12:39 +0000782 if (!Die) {
783 Die = new DIE(Buffer);
784 DiesSet.InsertNode(Die, Where);
785 this->Die->AddChild(Die);
786 Buffer.Detach();
787 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000788
Jim Laskeyef42a012006-11-02 20:12:39 +0000789 return Die;
790 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000791};
792
Jim Laskey65195462006-10-30 13:35:07 +0000793//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000794/// Dwarf - Emits general Dwarf directives.
Jim Laskeyef42a012006-11-02 20:12:39 +0000795///
Jim Laskey65195462006-10-30 13:35:07 +0000796class Dwarf {
Jim Laskey072200c2007-01-29 18:51:14 +0000797protected:
Jim Laskey65195462006-10-30 13:35:07 +0000798 //===--------------------------------------------------------------------===//
Jim Laskey072200c2007-01-29 18:51:14 +0000799 // Core attributes used by the Dwarf writer.
Jim Laskey65195462006-10-30 13:35:07 +0000800 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000801
Jim Laskey65195462006-10-30 13:35:07 +0000802 //
803 /// O - Stream to .s file.
804 ///
Owen Andersoncb371882008-08-21 00:14:44 +0000805 raw_ostream &O;
Jim Laskey65195462006-10-30 13:35:07 +0000806
807 /// Asm - Target of Dwarf emission.
808 ///
809 AsmPrinter *Asm;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000810
Bill Wendlingaa8f8882008-07-01 23:34:48 +0000811 /// TAI - Target asm information.
Jim Laskey65195462006-10-30 13:35:07 +0000812 const TargetAsmInfo *TAI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000813
Jim Laskey65195462006-10-30 13:35:07 +0000814 /// TD - Target data.
815 const TargetData *TD;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000816
Jim Laskey65195462006-10-30 13:35:07 +0000817 /// RI - Register Information.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000818 const TargetRegisterInfo *RI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000819
Jim Laskey65195462006-10-30 13:35:07 +0000820 /// M - Current module.
821 ///
822 Module *M;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000823
Jim Laskey65195462006-10-30 13:35:07 +0000824 /// MF - Current machine function.
825 ///
826 MachineFunction *MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000827
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000828 /// MMI - Collected machine module information.
Jim Laskey65195462006-10-30 13:35:07 +0000829 ///
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000830 MachineModuleInfo *MMI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000831
Jim Laskey65195462006-10-30 13:35:07 +0000832 /// SubprogramCount - The running count of functions being compiled.
833 ///
834 unsigned SubprogramCount;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000835
Chris Lattner9251f132007-09-24 03:35:37 +0000836 /// Flavor - A unique string indicating what dwarf producer this is, used to
837 /// unique labels.
838 const char * const Flavor;
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000839
840 unsigned SetCounter;
Owen Andersoncb371882008-08-21 00:14:44 +0000841 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattner9251f132007-09-24 03:35:37 +0000842 const char *flavor)
Jim Laskey072200c2007-01-29 18:51:14 +0000843 : O(OS)
844 , Asm(A)
845 , TAI(T)
846 , TD(Asm->TM.getTargetData())
847 , RI(Asm->TM.getRegisterInfo())
848 , M(NULL)
849 , MF(NULL)
850 , MMI(NULL)
Jim Laskey072200c2007-01-29 18:51:14 +0000851 , SubprogramCount(0)
Chris Lattner9251f132007-09-24 03:35:37 +0000852 , Flavor(flavor)
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000853 , SetCounter(1)
Jim Laskey072200c2007-01-29 18:51:14 +0000854 {
855 }
856
857public:
Jim Laskey072200c2007-01-29 18:51:14 +0000858 //===--------------------------------------------------------------------===//
859 // Accessors.
860 //
Bill Wendlinga6f02fb2009-04-10 00:00:25 +0000861 const AsmPrinter *getAsm() const { return Asm; }
Jim Laskeyb82313f2007-02-01 16:31:34 +0000862 MachineModuleInfo *getMMI() const { return MMI; }
Jim Laskey072200c2007-01-29 18:51:14 +0000863 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohman82482942007-09-27 23:12:31 +0000864 const TargetData *getTargetData() const { return TD; }
Jim Laskey072200c2007-01-29 18:51:14 +0000865
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000866 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
867 const {
868 if (isInSection && TAI->getDwarfSectionOffsetDirective())
869 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohman82482942007-09-27 23:12:31 +0000870 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000871 O << TAI->getData32bitsDirective();
872 else
873 O << TAI->getData64bitsDirective();
874 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000875
Jim Laskeyb82313f2007-02-01 16:31:34 +0000876 /// PrintLabelName - Print label name in form used by Dwarf writer.
877 ///
878 void PrintLabelName(DWLabel Label) const {
879 PrintLabelName(Label.Tag, Label.Number);
880 }
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000881 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000882 O << TAI->getPrivateGlobalPrefix() << Tag;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000883 if (Number) O << Number;
884 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000885
Chris Lattner9251f132007-09-24 03:35:37 +0000886 void PrintLabelName(const char *Tag, unsigned Number,
887 const char *Suffix) const {
888 O << TAI->getPrivateGlobalPrefix() << Tag;
889 if (Number) O << Number;
890 O << Suffix;
891 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000892
Jim Laskeyb82313f2007-02-01 16:31:34 +0000893 /// EmitLabel - Emit location label for internal use by Dwarf.
894 ///
895 void EmitLabel(DWLabel Label) const {
896 EmitLabel(Label.Tag, Label.Number);
897 }
898 void EmitLabel(const char *Tag, unsigned Number) const {
899 PrintLabelName(Tag, Number);
900 O << ":\n";
901 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000902
Jim Laskeyb82313f2007-02-01 16:31:34 +0000903 /// EmitReference - Emit a reference to a label.
904 ///
Dan Gohman06ff4e62007-09-28 15:43:33 +0000905 void EmitReference(DWLabel Label, bool IsPCRelative = false,
906 bool Force32Bit = false) const {
907 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000908 }
909 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman06ff4e62007-09-28 15:43:33 +0000910 bool IsPCRelative = false, bool Force32Bit = false) const {
911 PrintRelDirective(Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000912 PrintLabelName(Tag, Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000913
Jim Laskeyb82313f2007-02-01 16:31:34 +0000914 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
915 }
Dan Gohman06ff4e62007-09-28 15:43:33 +0000916 void EmitReference(const std::string &Name, bool IsPCRelative = false,
917 bool Force32Bit = false) const {
918 PrintRelDirective(Force32Bit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000919
Jim Laskeyb82313f2007-02-01 16:31:34 +0000920 O << Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000921
Jim Laskeyb82313f2007-02-01 16:31:34 +0000922 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
923 }
924
925 /// EmitDifference - Emit the difference between two labels. Some
926 /// assemblers do not behave with absolute expressions with data directives,
927 /// so there is an option (needsSet) to use an intermediary set expression.
928 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000929 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000930 EmitDifference(LabelHi.Tag, LabelHi.Number,
931 LabelLo.Tag, LabelLo.Number,
932 IsSmall);
933 }
934 void EmitDifference(const char *TagHi, unsigned NumberHi,
935 const char *TagLo, unsigned NumberLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000936 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000937 if (TAI->needsSet()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000938 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000939 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000940 O << ",";
941 PrintLabelName(TagHi, NumberHi);
942 O << "-";
943 PrintLabelName(TagLo, NumberLo);
944 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000945
946 PrintRelDirective(IsSmall);
Chris Lattner9251f132007-09-24 03:35:37 +0000947 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000948 ++SetCounter;
949 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000950 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000951
Jim Laskeyb82313f2007-02-01 16:31:34 +0000952 PrintLabelName(TagHi, NumberHi);
953 O << "-";
954 PrintLabelName(TagLo, NumberLo);
955 }
956 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000957
958 void EmitSectionOffset(const char* Label, const char* Section,
959 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000960 bool IsSmall = false, bool isEH = false,
961 bool useSet = true) {
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000962 bool printAbsolute = false;
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000963 if (isEH)
964 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
965 else
966 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
967
968 if (TAI->needsSet() && useSet) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000969 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000970 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000971 O << ",";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000972 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000973
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000974 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000975 O << "-";
976 PrintLabelName(Section, SectionNumber);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000977 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000978 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000979
980 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000981
Chris Lattner9251f132007-09-24 03:35:37 +0000982 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000983 ++SetCounter;
984 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000985 PrintRelDirective(IsSmall, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000986
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000987 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000988
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000989 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000990 O << "-";
991 PrintLabelName(Section, SectionNumber);
992 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000993 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000994 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000995
Jim Laskeyb82313f2007-02-01 16:31:34 +0000996 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
997 /// frame.
998 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenb97aec62007-11-13 19:13:01 +0000999 const std::vector<MachineMove> &Moves, bool isEH) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001000 int stackGrowth =
1001 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1002 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00001003 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeybacd3042007-02-21 22:48:45 +00001004 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
Jim Laskeyb82313f2007-02-01 16:31:34 +00001005
1006 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001007 const MachineMove &Move = Moves[i];
Jim Laskeyb82313f2007-02-01 16:31:34 +00001008 unsigned LabelID = Move.getLabelID();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001009
Jim Laskeyb82313f2007-02-01 16:31:34 +00001010 if (LabelID) {
1011 LabelID = MMI->MappedLabel(LabelID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001012
Jim Laskeyb82313f2007-02-01 16:31:34 +00001013 // Throw out move if the label is invalid.
1014 if (!LabelID) continue;
1015 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001016
Jim Laskeyb82313f2007-02-01 16:31:34 +00001017 const MachineLocation &Dst = Move.getDestination();
1018 const MachineLocation &Src = Move.getSource();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001019
Jim Laskeyb82313f2007-02-01 16:31:34 +00001020 // Advance row if new location.
1021 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1022 Asm->EmitInt8(DW_CFA_advance_loc4);
1023 Asm->EOL("DW_CFA_advance_loc4");
Jim Laskeybacd3042007-02-21 22:48:45 +00001024 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1025 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001026
Jim Laskeyb82313f2007-02-01 16:31:34 +00001027 BaseLabelID = LabelID;
Jim Laskeybacd3042007-02-21 22:48:45 +00001028 BaseLabel = "label";
Jim Laskeyb82313f2007-02-01 16:31:34 +00001029 IsLocal = true;
1030 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001031
Jim Laskeyb82313f2007-02-01 16:31:34 +00001032 // If advancing cfa.
Dan Gohmand735b802008-10-03 15:45:36 +00001033 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1034 if (!Src.isReg()) {
1035 if (Src.getReg() == MachineLocation::VirtualFP) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001036 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1037 Asm->EOL("DW_CFA_def_cfa_offset");
1038 } else {
1039 Asm->EmitInt8(DW_CFA_def_cfa);
1040 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmand735b802008-10-03 15:45:36 +00001041 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001042 Asm->EOL("Register");
1043 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001044
Jim Laskeyb82313f2007-02-01 16:31:34 +00001045 int Offset = -Src.getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001046
Jim Laskeyb82313f2007-02-01 16:31:34 +00001047 Asm->EmitULEB128Bytes(Offset);
1048 Asm->EOL("Offset");
1049 } else {
1050 assert(0 && "Machine move no supported yet.");
1051 }
Dan Gohmand735b802008-10-03 15:45:36 +00001052 } else if (Src.isReg() &&
1053 Src.getReg() == MachineLocation::VirtualFP) {
1054 if (Dst.isReg()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001055 Asm->EmitInt8(DW_CFA_def_cfa_register);
1056 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmand735b802008-10-03 15:45:36 +00001057 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001058 Asm->EOL("Register");
1059 } else {
1060 assert(0 && "Machine move no supported yet.");
1061 }
1062 } else {
Dan Gohmand735b802008-10-03 15:45:36 +00001063 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001064 int Offset = Dst.getOffset() / stackGrowth;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001065
Jim Laskeyb82313f2007-02-01 16:31:34 +00001066 if (Offset < 0) {
1067 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1068 Asm->EOL("DW_CFA_offset_extended_sf");
1069 Asm->EmitULEB128Bytes(Reg);
1070 Asm->EOL("Reg");
1071 Asm->EmitSLEB128Bytes(Offset);
1072 Asm->EOL("Offset");
1073 } else if (Reg < 64) {
1074 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Cheng42bf74b2009-03-25 01:47:28 +00001075 if (Asm->isVerbose())
Evan Chengd4114192008-07-09 21:53:02 +00001076 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1077 else
1078 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00001079 Asm->EmitULEB128Bytes(Offset);
1080 Asm->EOL("Offset");
1081 } else {
1082 Asm->EmitInt8(DW_CFA_offset_extended);
1083 Asm->EOL("DW_CFA_offset_extended");
1084 Asm->EmitULEB128Bytes(Reg);
1085 Asm->EOL("Reg");
1086 Asm->EmitULEB128Bytes(Offset);
1087 Asm->EOL("Offset");
1088 }
1089 }
1090 }
1091 }
1092
Jim Laskey072200c2007-01-29 18:51:14 +00001093};
1094
1095//===----------------------------------------------------------------------===//
Devang Patelf3ee5142009-01-12 22:54:42 +00001096/// SrcLineInfo - This class is used to record source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001097///
1098class SrcLineInfo {
1099 unsigned Line; // Source line number.
1100 unsigned Column; // Source column.
1101 unsigned SourceID; // Source ID number.
1102 unsigned LabelID; // Label in code ID number.
1103public:
1104 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001105 : Line(L), Column(C), SourceID(S), LabelID(I) {}
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001106
1107 // Accessors
1108 unsigned getLine() const { return Line; }
1109 unsigned getColumn() const { return Column; }
1110 unsigned getSourceID() const { return SourceID; }
1111 unsigned getLabelID() const { return LabelID; }
1112};
1113
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001114//===----------------------------------------------------------------------===//
Devang Patel7a6e5a32009-01-08 02:33:41 +00001115/// DbgVariable - This class is used to track local variable information.
1116///
1117class DbgVariable {
Devang Patel99ec3532009-01-16 19:28:14 +00001118 DIVariable Var; // Variable Descriptor.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001119 unsigned FrameIndex; // Variable frame index.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001120public:
Devang Patel99ec3532009-01-16 19:28:14 +00001121 DbgVariable(DIVariable V, unsigned I) : Var(V), FrameIndex(I) {}
Devang Patel7a6e5a32009-01-08 02:33:41 +00001122
1123 // Accessors.
Devang Patel99ec3532009-01-16 19:28:14 +00001124 DIVariable getVariable() const { return Var; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001125 unsigned getFrameIndex() const { return FrameIndex; }
1126};
1127
1128//===----------------------------------------------------------------------===//
1129/// DbgScope - This class is used to track scope information.
1130///
1131class DbgScope {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001132 DbgScope *Parent; // Parent to this scope.
Devang Patel7103c6a2009-01-16 18:01:58 +00001133 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001134 // Either subprogram or block.
1135 unsigned StartLabelID; // Label ID of the beginning of scope.
1136 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel7103c6a2009-01-16 18:01:58 +00001137 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
Devang Patel9795da52009-01-10 02:42:49 +00001138 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001139public:
Devang Patel0e5200f2009-01-15 18:25:17 +00001140 DbgScope(DbgScope *P, DIDescriptor D)
Devang Patel7a6e5a32009-01-08 02:33:41 +00001141 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), Scopes(), Variables()
1142 {}
Devang Patel481ff5b2009-01-12 18:48:36 +00001143 ~DbgScope() {
1144 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1145 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1146 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001147
1148 // Accessors.
Devang Patel7103c6a2009-01-16 18:01:58 +00001149 DbgScope *getParent() const { return Parent; }
1150 DIDescriptor getDesc() const { return Desc; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001151 unsigned getStartLabelID() const { return StartLabelID; }
1152 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel9795da52009-01-10 02:42:49 +00001153 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1154 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001155 void setStartLabelID(unsigned S) { StartLabelID = S; }
1156 void setEndLabelID(unsigned E) { EndLabelID = E; }
1157
1158 /// AddScope - Add a scope to the scope.
1159 ///
1160 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1161
1162 /// AddVariable - Add a variable to the scope.
1163 ///
1164 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
1165};
1166
1167//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001168/// DwarfDebug - Emits Dwarf debug directives.
Jim Laskey072200c2007-01-29 18:51:14 +00001169///
1170class DwarfDebug : public Dwarf {
Jim Laskey65195462006-10-30 13:35:07 +00001171 //===--------------------------------------------------------------------===//
1172 // Attributes used to construct specific Dwarf sections.
1173 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001174
Evan Chenge3d42322009-02-25 07:04:34 +00001175 /// CompileUnitMap - A map of global variables representing compile units to
1176 /// compile units.
1177 DenseMap<Value *, CompileUnit *> CompileUnitMap;
1178
1179 /// CompileUnits - All the compile units in this module.
1180 ///
1181 SmallVector<CompileUnit *, 8> CompileUnits;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001182
Devang Pateldd9db662009-01-30 18:20:31 +00001183 /// MainCU - Some platform prefers one compile unit per .o file. In such
1184 /// cases, all dies are inserted in MainCU.
1185 CompileUnit *MainCU;
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001186
Jim Laskeyef42a012006-11-02 20:12:39 +00001187 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +00001188 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001189 FoldingSet<DIEAbbrev> AbbreviationsSet;
1190
1191 /// Abbreviations - A list of all the unique abbreviations in use.
1192 ///
1193 std::vector<DIEAbbrev *> Abbreviations;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001194
Evan Chenge3d42322009-02-25 07:04:34 +00001195 /// DirectoryIdMap - Directory name to directory id map.
1196 ///
1197 StringMap<unsigned> DirectoryIdMap;
Devang Patel8526cc02009-01-05 22:35:52 +00001198
Evan Chenge3d42322009-02-25 07:04:34 +00001199 /// DirectoryNames - A list of directory names.
1200 SmallVector<std::string, 8> DirectoryNames;
1201
1202 /// SourceFileIdMap - Source file name to source file id map.
1203 ///
1204 StringMap<unsigned> SourceFileIdMap;
1205
1206 /// SourceFileNames - A list of source file names.
1207 SmallVector<std::string, 8> SourceFileNames;
1208
1209 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
1210 /// id mapped to a unique id.
1211 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
1212
1213 /// SourceIds - Reverse map from source id to directory id + file id pair.
1214 ///
1215 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
Devang Patel8526cc02009-01-05 22:35:52 +00001216
Devang Patel07354e52009-01-16 21:07:53 +00001217 /// Lines - List of of source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001218 std::vector<SrcLineInfo> Lines;
1219
Devang Patel07354e52009-01-16 21:07:53 +00001220 /// ValuesSet - Used to uniquely define values.
1221 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001222 FoldingSet<DIEValue> ValuesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001223
Jim Laskeyef42a012006-11-02 20:12:39 +00001224 /// Values - A list of all the unique values in use.
1225 ///
1226 std::vector<DIEValue *> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001227
Jim Laskey65195462006-10-30 13:35:07 +00001228 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +00001229 ///
Jim Laskey65195462006-10-30 13:35:07 +00001230 UniqueVector<std::string> StringPool;
1231
Jim Laskey65195462006-10-30 13:35:07 +00001232 /// SectionMap - Provides a unique id per text section.
1233 ///
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001234 UniqueVector<const Section*> SectionMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001235
Jim Laskey65195462006-10-30 13:35:07 +00001236 /// SectionSourceLines - Tracks line numbers per text section.
1237 ///
Devang Patelf3ee5142009-01-12 22:54:42 +00001238 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Jim Laskey65195462006-10-30 13:35:07 +00001239
Jim Laskeybacd3042007-02-21 22:48:45 +00001240 /// didInitial - Flag to indicate if initial emission has been done.
1241 ///
1242 bool didInitial;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001243
Jim Laskeybacd3042007-02-21 22:48:45 +00001244 /// shouldEmit - Flag to indicate if debug information should be emitted.
1245 ///
1246 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +00001247
Devang Patel0e5200f2009-01-15 18:25:17 +00001248 // RootDbgScope - Top level scope for the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001249 //
1250 DbgScope *RootDbgScope;
1251
Bill Wendling163ba3f2009-03-10 21:23:25 +00001252 /// DbgScopeMap - Tracks the scopes in the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001253 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
Bill Wendling163ba3f2009-03-10 21:23:25 +00001254
Devang Patel0f7fef32009-04-13 17:02:03 +00001255 /// InlineInfo - Keep track of inlined functions and their location.
1256 /// This information is used to populate debug_inlined section.
1257 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> > InlineInfo;
1258
Bill Wendling163ba3f2009-03-10 21:23:25 +00001259 /// DebugTimer - Timer for the Dwarf debug writer.
1260 Timer *DebugTimer;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001261
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001262 struct FunctionDebugFrameInfo {
1263 unsigned Number;
1264 std::vector<MachineMove> Moves;
1265
1266 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001267 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001268 };
1269
1270 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001271
Bill Wendling163ba3f2009-03-10 21:23:25 +00001272private:
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001273 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
Bill Wendlingc8615e42009-03-10 21:47:45 +00001274 /// maps to the source id. Source id starts at 1.
1275 std::pair<unsigned, unsigned>
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001276 getSourceDirectoryAndFileIds(unsigned SId) const {
Bill Wendlingc8615e42009-03-10 21:47:45 +00001277 return SourceIds[SId-1];
1278 }
1279
1280 /// getNumSourceDirectories - Return the number of source directories in the
1281 /// debug info.
1282 unsigned getNumSourceDirectories() const {
1283 return DirectoryNames.size();
1284 }
1285
1286 /// getSourceDirectoryName - Return the name of the directory corresponding
1287 /// to the id.
1288 const std::string &getSourceDirectoryName(unsigned Id) const {
1289 return DirectoryNames[Id - 1];
1290 }
1291
1292 /// getSourceFileName - Return the name of the source file corresponding
1293 /// to the id.
1294 const std::string &getSourceFileName(unsigned Id) const {
1295 return SourceFileNames[Id - 1];
1296 }
1297
1298 /// getNumSourceIds - Return the number of unique source ids.
Bill Wendlingc8615e42009-03-10 21:47:45 +00001299 unsigned getNumSourceIds() const {
1300 return SourceIds.size();
1301 }
1302
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001303 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001304 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001305 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1306 // Profile the node so that we can make it unique.
1307 FoldingSetNodeID ID;
1308 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001309
Jim Laskeyef42a012006-11-02 20:12:39 +00001310 // Check the set for priors.
1311 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001312
Jim Laskeyef42a012006-11-02 20:12:39 +00001313 // If it's newly added.
1314 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001315 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +00001316 Abbreviations.push_back(&Abbrev);
1317 // Assign the vector position + 1 as its number.
1318 Abbrev.setNumber(Abbreviations.size());
1319 } else {
1320 // Assign existing abbreviation number.
1321 Abbrev.setNumber(InSet->getNumber());
1322 }
1323 }
1324
Jim Laskey65195462006-10-30 13:35:07 +00001325 /// NewString - Add a string to the constant pool and returns a label.
1326 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001327 DWLabel NewString(const std::string &String) {
1328 unsigned StringID = StringPool.insert(String);
1329 return DWLabel("string", StringID);
1330 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001331
Jim Laskeyef42a012006-11-02 20:12:39 +00001332 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1333 /// entry.
1334 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1335 DIEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001336
Jim Laskeyef42a012006-11-02 20:12:39 +00001337 if (Entry) {
1338 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001339 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001340 void *Where;
1341 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001342
Jim Laskeyf6733882006-11-02 21:48:18 +00001343 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001344
Jim Laskeyef42a012006-11-02 20:12:39 +00001345 Value = new DIEntry(Entry);
1346 ValuesSet.InsertNode(Value, Where);
1347 } else {
1348 Value = new DIEntry(Entry);
1349 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001350
Jim Laskeyef42a012006-11-02 20:12:39 +00001351 Values.push_back(Value);
1352 return Value;
1353 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001354
Jim Laskeyef42a012006-11-02 20:12:39 +00001355 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1356 ///
1357 void SetDIEntry(DIEntry *Value, DIE *Entry) {
Bill Wendlingdd446322009-03-10 23:57:09 +00001358 Value->setEntry(Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001359 // Add to values set if not already there. If it is, we merely have a
1360 // duplicate in the values list (no harm.)
1361 ValuesSet.GetOrInsertNode(Value);
1362 }
1363
1364 /// AddUInt - Add an unsigned integer attribute data and value.
1365 ///
1366 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1367 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1368
1369 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001370 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001371 void *Where;
1372 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1373 if (!Value) {
1374 Value = new DIEInteger(Integer);
1375 ValuesSet.InsertNode(Value, Where);
1376 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001377 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001378
Jim Laskeyef42a012006-11-02 20:12:39 +00001379 Die->AddValue(Attribute, Form, Value);
1380 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001381
Jim Laskeyef42a012006-11-02 20:12:39 +00001382 /// AddSInt - Add an signed integer attribute data and value.
1383 ///
1384 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1385 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1386
1387 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001388 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001389 void *Where;
1390 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1391 if (!Value) {
1392 Value = new DIEInteger(Integer);
1393 ValuesSet.InsertNode(Value, Where);
1394 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001395 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001396
Jim Laskeyef42a012006-11-02 20:12:39 +00001397 Die->AddValue(Attribute, Form, Value);
1398 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001399
Evan Chenge3d42322009-02-25 07:04:34 +00001400 /// AddString - Add a string attribute data and value.
Jim Laskeyef42a012006-11-02 20:12:39 +00001401 ///
1402 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1403 const std::string &String) {
1404 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001405 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001406 void *Where;
1407 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1408 if (!Value) {
1409 Value = new DIEString(String);
1410 ValuesSet.InsertNode(Value, Where);
1411 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001412 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001413
Jim Laskeyef42a012006-11-02 20:12:39 +00001414 Die->AddValue(Attribute, Form, Value);
1415 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001416
Jim Laskeyef42a012006-11-02 20:12:39 +00001417 /// AddLabel - Add a Dwarf label attribute data and value.
1418 ///
1419 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1420 const DWLabel &Label) {
1421 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001422 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001423 void *Where;
1424 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1425 if (!Value) {
1426 Value = new DIEDwarfLabel(Label);
1427 ValuesSet.InsertNode(Value, Where);
1428 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001429 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001430
Jim Laskeyef42a012006-11-02 20:12:39 +00001431 Die->AddValue(Attribute, Form, Value);
1432 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001433
Jim Laskeyef42a012006-11-02 20:12:39 +00001434 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1435 ///
1436 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1437 const std::string &Label) {
1438 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001439 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001440 void *Where;
1441 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1442 if (!Value) {
1443 Value = new DIEObjectLabel(Label);
1444 ValuesSet.InsertNode(Value, Where);
1445 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001446 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001447
Jim Laskeyef42a012006-11-02 20:12:39 +00001448 Die->AddValue(Attribute, Form, Value);
1449 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001450
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001451 /// AddSectionOffset - Add a section offset label attribute data and value.
1452 ///
1453 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1454 const DWLabel &Label, const DWLabel &Section,
1455 bool isEH = false, bool useSet = true) {
1456 FoldingSetNodeID ID;
1457 DIESectionOffset::Profile(ID, Label, Section);
1458 void *Where;
1459 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1460 if (!Value) {
1461 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1462 ValuesSet.InsertNode(Value, Where);
1463 Values.push_back(Value);
1464 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001465
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001466 Die->AddValue(Attribute, Form, Value);
1467 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001468
Jim Laskeyef42a012006-11-02 20:12:39 +00001469 /// AddDelta - Add a label delta attribute data and value.
1470 ///
1471 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1472 const DWLabel &Hi, const DWLabel &Lo) {
1473 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001474 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001475 void *Where;
1476 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1477 if (!Value) {
1478 Value = new DIEDelta(Hi, Lo);
1479 ValuesSet.InsertNode(Value, Where);
1480 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001481 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001482
Jim Laskeyef42a012006-11-02 20:12:39 +00001483 Die->AddValue(Attribute, Form, Value);
1484 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001485
Jim Laskeyef42a012006-11-02 20:12:39 +00001486 /// AddDIEntry - Add a DIE attribute data and value.
1487 ///
1488 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1489 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1490 }
1491
1492 /// AddBlock - Add block data.
1493 ///
1494 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1495 Block->ComputeSize(*this);
1496 FoldingSetNodeID ID;
1497 Block->Profile(ID);
1498 void *Where;
1499 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1500 if (!Value) {
1501 Value = Block;
1502 ValuesSet.InsertNode(Value, Where);
1503 Values.push_back(Value);
1504 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001505 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001506 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001507 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001508 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001509
Jim Laskeyef42a012006-11-02 20:12:39 +00001510 Die->AddValue(Attribute, Block->BestForm(), Value);
1511 }
1512
Jim Laskey65195462006-10-30 13:35:07 +00001513 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001514 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001515 void AddSourceLine(DIE *Die, const DIVariable *V) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001516 unsigned FileID = 0;
1517 unsigned Line = V->getLineNumber();
Devang Pateldd9db662009-01-30 18:20:31 +00001518 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1519 FileID = Unit->getID();
Devang Patel2303df92009-02-10 06:04:08 +00001520 assert (FileID && "Invalid file id");
Devang Patel7a6e5a32009-01-08 02:33:41 +00001521 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1522 AddUInt(Die, DW_AT_decl_line, 0, Line);
1523 }
1524
1525 /// AddSourceLine - Add location information to specified debug information
1526 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001527 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Devang Patel8526cc02009-01-05 22:35:52 +00001528 unsigned FileID = 0;
1529 unsigned Line = G->getLineNumber();
Devang Pateldd9db662009-01-30 18:20:31 +00001530 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1531 FileID = Unit->getID();
Devang Patel2303df92009-02-10 06:04:08 +00001532 assert (FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001533 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1534 AddUInt(Die, DW_AT_decl_line, 0, Line);
1535 }
1536
Devang Patel99ec3532009-01-16 19:28:14 +00001537 void AddSourceLine(DIE *Die, const DIType *Ty) {
Devang Patel8526cc02009-01-05 22:35:52 +00001538 unsigned FileID = 0;
Devang Patel99ec3532009-01-16 19:28:14 +00001539 unsigned Line = Ty->getLineNumber();
Devang Pateldd9db662009-01-30 18:20:31 +00001540 DICompileUnit CU = Ty->getCompileUnit();
1541 if (CU.isNull())
1542 return;
1543 CompileUnit *Unit = FindCompileUnit(CU);
1544 FileID = Unit->getID();
Devang Patel2303df92009-02-10 06:04:08 +00001545 assert (FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001546 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1547 AddUInt(Die, DW_AT_decl_line, 0, Line);
1548 }
1549
Jim Laskey65195462006-10-30 13:35:07 +00001550 /// AddAddress - Add an address attribute to a die based on the location
1551 /// provided.
1552 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001553 const MachineLocation &Location) {
Dan Gohmand735b802008-10-03 15:45:36 +00001554 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001555 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001556
Dan Gohmand735b802008-10-03 15:45:36 +00001557 if (Location.isReg()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001558 if (Reg < 32) {
1559 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1560 } else {
1561 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1562 AddUInt(Block, 0, DW_FORM_udata, Reg);
1563 }
1564 } else {
1565 if (Reg < 32) {
1566 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1567 } else {
1568 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1569 AddUInt(Block, 0, DW_FORM_udata, Reg);
1570 }
1571 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1572 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001573
Jim Laskeyef42a012006-11-02 20:12:39 +00001574 AddBlock(Die, Attribute, 0, Block);
1575 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001576
Jim Laskeyef42a012006-11-02 20:12:39 +00001577 /// AddType - Add a new type attribute to the specified entity.
Devang Patel48d190f2009-01-05 21:47:57 +00001578 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel80303aa2009-01-23 19:13:31 +00001579 if (Ty.isNull())
Devang Patel48d190f2009-01-05 21:47:57 +00001580 return;
Devang Patel48d190f2009-01-05 21:47:57 +00001581
1582 // Check for pre-existence.
1583 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1584 // If it exists then use the existing value.
1585 if (Slot) {
1586 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1587 return;
1588 }
1589
1590 // Set up proxy.
1591 Slot = NewDIEntry();
1592
1593 // Construct type.
1594 DIE Buffer(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001595 if (Ty.isBasicType(Ty.getTag()))
1596 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1597 else if (Ty.isDerivedType(Ty.getTag()))
1598 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1599 else {
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001600 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelf193ff02009-01-15 19:26:23 +00001601 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1602 }
1603
Devang Patel7009d242009-01-27 23:22:55 +00001604 // Add debug information entry to entity and appropriate context.
1605 DIE *Die = NULL;
1606 DIDescriptor Context = Ty.getContext();
1607 if (!Context.isNull())
1608 Die = DW_Unit->getDieMapSlotFor(Context.getGV());
1609
1610 if (Die) {
1611 DIE *Child = new DIE(Buffer);
1612 Die->AddChild(Child);
1613 Buffer.Detach();
1614 SetDIEntry(Slot, Child);
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001615 } else {
Devang Patel7009d242009-01-27 23:22:55 +00001616 Die = DW_Unit->AddDie(Buffer);
1617 SetDIEntry(Slot, Die);
1618 }
1619
Devang Patel48d190f2009-01-05 21:47:57 +00001620 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1621 }
1622
Devang Patele5202732009-01-05 19:07:53 +00001623 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1624 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001625 DIBasicType BTy) {
Bill Wendling0582ae92009-03-13 04:39:26 +00001626
Devang Patel08f053f2009-01-05 17:57:47 +00001627 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001628 std::string Name;
1629 BTy.getName(Name);
Devang Patel08f053f2009-01-05 17:57:47 +00001630 Buffer.setTag(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001631 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patel08f053f2009-01-05 17:57:47 +00001632 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001633 if (!Name.empty())
Devang Patel08f053f2009-01-05 17:57:47 +00001634 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001635 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel08f053f2009-01-05 17:57:47 +00001636 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1637 }
1638
Devang Patele5202732009-01-05 19:07:53 +00001639 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1640 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001641 DIDerivedType DTy) {
Bill Wendling0582ae92009-03-13 04:39:26 +00001642
Devang Patel08f053f2009-01-05 17:57:47 +00001643 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001644 std::string Name;
1645 DTy.getName(Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001646 uint64_t Size = DTy.getSizeInBits() >> 3;
1647 unsigned Tag = DTy.getTag();
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001648
Devang Patel08f053f2009-01-05 17:57:47 +00001649 // FIXME - Workaround for templates.
1650 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1651
1652 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001653
Devang Patel08f053f2009-01-05 17:57:47 +00001654 // Map to main type, void will not have a type.
Devang Patelf193ff02009-01-15 19:26:23 +00001655 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00001656 AddType(DW_Unit, &Buffer, FromTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001657
1658 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001659 if (!Name.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00001660 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel08f053f2009-01-05 17:57:47 +00001661
1662 // Add size if non-zero (derived types might be zero-sized.)
1663 if (Size)
1664 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1665
1666 // Add source line info if available and TyDesc is not a forward
1667 // declaration.
Devang Patelad165be2009-01-27 00:45:04 +00001668 if (!DTy.isForwardDecl())
1669 AddSourceLine(&Buffer, &DTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001670 }
1671
Devang Patelf4215332009-01-05 19:55:51 +00001672 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1673 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001674 DICompositeType CTy) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001675 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001676 std::string Name;
1677 CTy.getName(Name);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001678
Devang Patelf193ff02009-01-15 19:26:23 +00001679 uint64_t Size = CTy.getSizeInBits() >> 3;
1680 unsigned Tag = CTy.getTag();
Devang Patel49f38cb2009-01-23 01:19:09 +00001681 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001682
Devang Patelf4215332009-01-05 19:55:51 +00001683 switch (Tag) {
1684 case DW_TAG_vector_type:
1685 case DW_TAG_array_type:
Devang Patelf193ff02009-01-15 19:26:23 +00001686 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001687 break;
Devang Pateleab4a2e2009-01-20 18:35:14 +00001688 case DW_TAG_enumeration_type:
1689 {
1690 DIArray Elements = CTy.getTypeArray();
1691 // Add enumerators to enumeration type.
1692 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1693 DIE *ElemDie = NULL;
1694 DIEnumerator Enum(Elements.getElement(i).getGV());
1695 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
1696 Buffer.AddChild(ElemDie);
1697 }
1698 }
1699 break;
Devang Patelf4215332009-01-05 19:55:51 +00001700 case DW_TAG_subroutine_type:
1701 {
1702 // Add prototype flag.
1703 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patelf193ff02009-01-15 19:26:23 +00001704 DIArray Elements = CTy.getTypeArray();
Devang Patelf4215332009-01-05 19:55:51 +00001705 // Add return type.
Devang Patel48d190f2009-01-05 21:47:57 +00001706 DIDescriptor RTy = Elements.getElement(0);
Devang Patelf193ff02009-01-15 19:26:23 +00001707 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel48d190f2009-01-05 21:47:57 +00001708
Devang Patelf4215332009-01-05 19:55:51 +00001709 // Add arguments.
1710 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1711 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00001712 DIDescriptor Ty = Elements.getElement(i);
Devang Patel7ab24502009-01-17 06:57:25 +00001713 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patelf4215332009-01-05 19:55:51 +00001714 Buffer.AddChild(Arg);
1715 }
1716 }
1717 break;
1718 case DW_TAG_structure_type:
1719 case DW_TAG_union_type:
Devang Patel86bda412009-03-25 00:28:40 +00001720 case DW_TAG_class_type:
Devang Patelf4215332009-01-05 19:55:51 +00001721 {
1722 // Add elements to structure type.
Devang Patelf193ff02009-01-15 19:26:23 +00001723 DIArray Elements = CTy.getTypeArray();
Devang Patel153745c2009-01-16 00:50:53 +00001724
1725 // A forward struct declared type may not have elements available.
1726 if (Elements.isNull())
1727 break;
1728
Devang Patelf4215332009-01-05 19:55:51 +00001729 // Add elements to structure type.
1730 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1731 DIDescriptor Element = Elements.getElement(i);
Devang Patel5aac3d32009-01-17 08:01:33 +00001732 DIE *ElemDie = NULL;
Devang Patelf193ff02009-01-15 19:26:23 +00001733 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel2d1768c2009-01-17 08:05:14 +00001734 ElemDie = CreateSubprogramDIE(DW_Unit,
1735 DISubprogram(Element.getGV()));
Devang Patel5aac3d32009-01-17 08:01:33 +00001736 else if (Element.getTag() == dwarf::DW_TAG_variable) // ???
1737 ElemDie = CreateGlobalVariableDIE(DW_Unit,
1738 DIGlobalVariable(Element.getGV()));
Devang Patel2be58932009-01-20 21:02:02 +00001739 else
1740 ElemDie = CreateMemberDIE(DW_Unit,
1741 DIDerivedType(Element.getGV()));
Devang Patel2d1768c2009-01-17 08:05:14 +00001742 Buffer.AddChild(ElemDie);
Devang Patelf4215332009-01-05 19:55:51 +00001743 }
Devang Patel13319ce2009-02-17 22:43:44 +00001744 unsigned RLang = CTy.getRunTimeLang();
1745 if (RLang)
1746 AddUInt(&Buffer, DW_AT_APPLE_runtime_class, DW_FORM_data1, RLang);
Devang Patelf4215332009-01-05 19:55:51 +00001747 }
1748 break;
1749 default:
1750 break;
1751 }
1752
1753 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001754 if (!Name.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00001755 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf4215332009-01-05 19:55:51 +00001756
Devang Patelad165be2009-01-27 00:45:04 +00001757 if (Tag == DW_TAG_enumeration_type || Tag == DW_TAG_structure_type
1758 || Tag == DW_TAG_union_type) {
1759 // Add size if non-zero (derived types might be zero-sized.)
1760 if (Size)
1761 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1762 else {
1763 // Add zero size if it is not a forward declaration.
1764 if (CTy.isForwardDecl())
1765 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1766 else
1767 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1768 }
1769
1770 // Add source line info if available.
1771 if (!CTy.isForwardDecl())
1772 AddSourceLine(&Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001773 }
Devang Patelf4215332009-01-05 19:55:51 +00001774 }
1775
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001776 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1777 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelf193ff02009-01-15 19:26:23 +00001778 int64_t L = SR.getLo();
1779 int64_t H = SR.getHi();
Devang Patel68afdc32009-01-05 18:33:01 +00001780 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1781 if (L != H) {
1782 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1783 if (L)
Devang Patel2d1768c2009-01-17 08:05:14 +00001784 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1785 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel68afdc32009-01-05 18:33:01 +00001786 }
1787 Buffer.AddChild(DW_Subrange);
1788 }
1789
1790 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1791 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1792 DICompositeType *CTy) {
1793 Buffer.setTag(DW_TAG_array_type);
1794 if (CTy->getTag() == DW_TAG_vector_type)
1795 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1796
Devang Patelf9235742009-01-28 21:08:20 +00001797 // Emit derived type.
1798 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel68afdc32009-01-05 18:33:01 +00001799 DIArray Elements = CTy->getTypeArray();
Devang Patel68afdc32009-01-05 18:33:01 +00001800
1801 // Construct an anonymous type for index type.
1802 DIE IdxBuffer(DW_TAG_base_type);
1803 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1804 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1805 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1806
1807 // Add subranges to array type.
1808 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patelf4215332009-01-05 19:55:51 +00001809 DIDescriptor Element = Elements.getElement(i);
Devang Patelf193ff02009-01-15 19:26:23 +00001810 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1811 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel68afdc32009-01-05 18:33:01 +00001812 }
1813 }
1814
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001815 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Pateleab4a2e2009-01-20 18:35:14 +00001816 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patelc69bf2c2009-01-05 18:38:38 +00001817
1818 DIE *Enumerator = new DIE(DW_TAG_enumerator);
Bill Wendling0582ae92009-03-13 04:39:26 +00001819 std::string Name;
1820 ETy->getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001821 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
Devang Patelc69bf2c2009-01-05 18:38:38 +00001822 int64_t Value = ETy->getEnumValue();
1823 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Pateleab4a2e2009-01-20 18:35:14 +00001824 return Enumerator;
Devang Patelc69bf2c2009-01-05 18:38:38 +00001825 }
Devang Patel68afdc32009-01-05 18:33:01 +00001826
Devang Patel5aac3d32009-01-17 08:01:33 +00001827 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001828 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patel5aac3d32009-01-17 08:01:33 +00001829 {
1830 DIE *GVDie = new DIE(DW_TAG_variable);
Bill Wendling0582ae92009-03-13 04:39:26 +00001831 std::string Name;
1832 GV.getDisplayName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001833 AddString(GVDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling0582ae92009-03-13 04:39:26 +00001834 std::string LinkageName;
1835 GV.getLinkageName(LinkageName);
1836 if (!LinkageName.empty())
Devang Patel5aac3d32009-01-17 08:01:33 +00001837 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1838 AddType(DW_Unit, GVDie, GV.getType());
1839 if (!GV.isLocalToUnit())
1840 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
1841 AddSourceLine(GVDie, &GV);
1842 return GVDie;
Devang Patel86ae1422009-01-05 18:59:44 +00001843 }
1844
Devang Patel2be58932009-01-20 21:02:02 +00001845 /// CreateMemberDIE - Create new member DIE.
1846 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
1847 DIE *MemberDie = new DIE(DT.getTag());
Bill Wendling0582ae92009-03-13 04:39:26 +00001848 std::string Name;
1849 DT.getName(Name);
1850 if (!Name.empty())
Devang Patel2be58932009-01-20 21:02:02 +00001851 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1852
1853 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1854
1855 AddSourceLine(MemberDie, &DT);
1856
Devang Patel36375ee2009-02-17 21:23:59 +00001857 uint64_t Size = DT.getSizeInBits();
1858 uint64_t FieldSize = DT.getOriginalTypeSize();
1859
1860 if (Size != FieldSize) {
1861 // Handle bitfield.
1862 AddUInt(MemberDie, DW_AT_byte_size, 0, DT.getOriginalTypeSize() >> 3);
1863 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1864
1865 uint64_t Offset = DT.getOffsetInBits();
1866 uint64_t FieldOffset = Offset;
1867 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1868 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1869 FieldOffset = (HiMark - FieldSize);
1870 Offset -= FieldOffset;
1871 // Maybe we need to work from the other end.
1872 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1873 AddUInt(MemberDie, DW_AT_bit_offset, 0, Offset);
1874 }
Devang Patel2be58932009-01-20 21:02:02 +00001875 DIEBlock *Block = new DIEBlock();
1876 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1877 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1878 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1879
Devang Patel47661592009-01-21 00:08:04 +00001880 if (DT.isProtected())
1881 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1882 else if (DT.isPrivate())
1883 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1884
Devang Patel2be58932009-01-20 21:02:02 +00001885 return MemberDie;
1886 }
1887
Devang Patel5aac3d32009-01-17 08:01:33 +00001888 /// CreateSubprogramDIE - Create new DIE using SP.
1889 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Devang Patel2d1768c2009-01-17 08:05:14 +00001890 const DISubprogram &SP,
1891 bool IsConstructor = false) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001892 DIE *SPDie = new DIE(DW_TAG_subprogram);
Bill Wendling0582ae92009-03-13 04:39:26 +00001893 std::string Name;
1894 SP.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001895 AddString(SPDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling0582ae92009-03-13 04:39:26 +00001896 std::string LinkageName;
1897 SP.getLinkageName(LinkageName);
1898 if (!LinkageName.empty())
Devang Patel5aac3d32009-01-17 08:01:33 +00001899 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Devang Patel2d1768c2009-01-17 08:05:14 +00001900 LinkageName);
Devang Patel5aac3d32009-01-17 08:01:33 +00001901 AddSourceLine(SPDie, &SP);
Devang Patel86ae1422009-01-05 18:59:44 +00001902
Devang Patel5aac3d32009-01-17 08:01:33 +00001903 DICompositeType SPTy = SP.getType();
1904 DIArray Args = SPTy.getTypeArray();
1905
Devang Patel86ae1422009-01-05 18:59:44 +00001906 // Add Return Type.
Devang Patel75b27382009-04-08 22:18:45 +00001907 unsigned SPTag = SPTy.getTag();
Devang Patel9ac08d62009-02-27 18:05:21 +00001908 if (!IsConstructor) {
Devang Patel75b27382009-04-08 22:18:45 +00001909 if (Args.isNull() || SPTag != DW_TAG_subroutine_type)
Devang Patel9ac08d62009-02-27 18:05:21 +00001910 AddType(DW_Unit, SPDie, SPTy);
1911 else
1912 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
1913 }
Devang Pateld234e592009-01-30 01:21:46 +00001914
Devang Pateld5863dd2009-02-02 17:51:41 +00001915 if (!SP.isDefinition()) {
1916 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
1917 // Add arguments.
1918 // Do not add arguments for subprogram definition. They will be
1919 // handled through RecordVariable.
Devang Patel75b27382009-04-08 22:18:45 +00001920 if (SPTag == DW_TAG_subroutine_type)
Devang Pateld5863dd2009-02-02 17:51:41 +00001921 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1922 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1923 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
1924 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
1925 SPDie->AddChild(Arg);
1926 }
1927 }
Devang Pateld234e592009-01-30 01:21:46 +00001928
Devang Patel002ec142009-02-24 00:52:19 +00001929 unsigned Lang = SP.getCompileUnit().getLanguage();
1930 if (Lang == DW_LANG_C99 || Lang == DW_LANG_C89
1931 || Lang == DW_LANG_ObjC)
1932 AddUInt(SPDie, DW_AT_prototyped, DW_FORM_flag, 1);
1933
Devang Patelf193ff02009-01-15 19:26:23 +00001934 if (!SP.isLocalToUnit())
Devang Pateld234e592009-01-30 01:21:46 +00001935 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Devang Patel5aac3d32009-01-17 08:01:33 +00001936 return SPDie;
Devang Patel86ae1422009-01-05 18:59:44 +00001937 }
1938
Devang Patel5aac3d32009-01-17 08:01:33 +00001939 /// FindCompileUnit - Get the compile unit for the given descriptor.
1940 ///
Devang Patel8526cc02009-01-05 22:35:52 +00001941 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
Evan Chenge3d42322009-02-25 07:04:34 +00001942 CompileUnit *DW_Unit = CompileUnitMap[Unit.getGV()];
Devang Patel8526cc02009-01-05 22:35:52 +00001943 assert(DW_Unit && "Missing compile unit.");
1944 return DW_Unit;
1945 }
1946
Devang Patelbbdc8202009-01-13 23:54:55 +00001947 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001948 ///
1949 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1950 // Get the descriptor.
Devang Patel99ec3532009-01-16 19:28:14 +00001951 const DIVariable &VD = DV->getVariable();
Devang Patel7a6e5a32009-01-08 02:33:41 +00001952
1953 // Translate tag to proper Dwarf tag. The result variable is dropped for
1954 // now.
1955 unsigned Tag;
Devang Patel99ec3532009-01-16 19:28:14 +00001956 switch (VD.getTag()) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001957 case DW_TAG_return_variable: return NULL;
1958 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1959 case DW_TAG_auto_variable: // fall thru
1960 default: Tag = DW_TAG_variable; break;
1961 }
1962
1963 // Define variable debug information entry.
1964 DIE *VariableDie = new DIE(Tag);
Bill Wendling0582ae92009-03-13 04:39:26 +00001965 std::string Name;
1966 VD.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001967 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Devang Patel7a6e5a32009-01-08 02:33:41 +00001968
1969 // Add source line info if available.
Devang Patel99ec3532009-01-16 19:28:14 +00001970 AddSourceLine(VariableDie, &VD);
Devang Patel7a6e5a32009-01-08 02:33:41 +00001971
1972 // Add variable type.
Devang Patel99ec3532009-01-16 19:28:14 +00001973 AddType(Unit, VariableDie, VD.getType());
Devang Patel7a6e5a32009-01-08 02:33:41 +00001974
1975 // Add variable address.
1976 MachineLocation Location;
1977 Location.set(RI->getFrameRegister(*MF),
1978 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
1979 AddAddress(VariableDie, DW_AT_location, Location);
1980
1981 return VariableDie;
1982 }
1983
Devang Patel7a6e5a32009-01-08 02:33:41 +00001984 /// getOrCreateScope - Returns the scope associated with the given descriptor.
1985 ///
1986 DbgScope *getOrCreateScope(GlobalVariable *V) {
1987 DbgScope *&Slot = DbgScopeMap[V];
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001988 if (Slot) return Slot;
1989
1990 // FIXME - breaks down when the context is an inlined function.
1991 DIDescriptor ParentDesc;
1992 DIDescriptor Desc(V);
1993
1994 if (Desc.getTag() == dwarf::DW_TAG_lexical_block) {
1995 DIBlock Block(V);
1996 ParentDesc = Block.getContext();
Devang Patel7a6e5a32009-01-08 02:33:41 +00001997 }
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001998
1999 DbgScope *Parent = ParentDesc.isNull() ?
2000 NULL : getOrCreateScope(ParentDesc.getGV());
2001 Slot = new DbgScope(Parent, Desc);
2002
2003 if (Parent) {
2004 Parent->AddScope(Slot);
2005 } else if (RootDbgScope) {
2006 // FIXME - Add inlined function scopes to the root so we can delete them
2007 // later. Long term, handle inlined functions properly.
2008 RootDbgScope->AddScope(Slot);
2009 } else {
2010 // First function is top level function.
2011 RootDbgScope = Slot;
2012 }
2013
Devang Patel7a6e5a32009-01-08 02:33:41 +00002014 return Slot;
2015 }
2016
2017 /// ConstructDbgScope - Construct the components of a scope.
2018 ///
2019 void ConstructDbgScope(DbgScope *ParentScope,
2020 unsigned ParentStartID, unsigned ParentEndID,
2021 DIE *ParentDie, CompileUnit *Unit) {
2022 // Add variables to scope.
Devang Patel9795da52009-01-10 02:42:49 +00002023 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002024 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2025 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2026 if (VariableDie) ParentDie->AddChild(VariableDie);
2027 }
2028
2029 // Add nested scopes.
Devang Patel9795da52009-01-10 02:42:49 +00002030 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002031 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2032 // Define the Scope debug information entry.
2033 DbgScope *Scope = Scopes[j];
Devang Patel7a6e5a32009-01-08 02:33:41 +00002034
Devang Patele9bfb0f2009-01-12 18:41:00 +00002035 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2036 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002037
2038 // Ignore empty scopes.
2039 if (StartID == EndID && StartID != 0) continue;
Devang Patel0f7fef32009-04-13 17:02:03 +00002040
2041 // Do not ignore inlined scope even if it is empty. Inlined scope
2042 // does not have any parent.
2043 if (Scope->getParent()
2044 && Scope->getScopes().empty() && Scope->getVariables().empty())
2045 continue;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002046
2047 if (StartID == ParentStartID && EndID == ParentEndID) {
2048 // Just add stuff to the parent scope.
2049 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2050 } else {
2051 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2052
2053 // Add the scope bounds.
2054 if (StartID) {
2055 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2056 DWLabel("label", StartID));
2057 } else {
2058 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2059 DWLabel("func_begin", SubprogramCount));
2060 }
2061 if (EndID) {
2062 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2063 DWLabel("label", EndID));
2064 } else {
2065 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2066 DWLabel("func_end", SubprogramCount));
2067 }
2068
2069 // Add the scope contents.
2070 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2071 ParentDie->AddChild(ScopeDie);
2072 }
2073 }
2074 }
2075
2076 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2077 ///
2078 void ConstructRootDbgScope(DbgScope *RootScope) {
2079 // Exit if there is no root scope.
2080 if (!RootScope) return;
Devang Patel0e5200f2009-01-15 18:25:17 +00002081 DIDescriptor Desc = RootScope->getDesc();
2082 if (Desc.isNull())
2083 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002084
2085 // Get the subprogram debug information entry.
Devang Patel0e5200f2009-01-15 18:25:17 +00002086 DISubprogram SPD(Desc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002087
2088 // Get the compile unit context.
Devang Pateldd9db662009-01-30 18:20:31 +00002089 CompileUnit *Unit = MainCU;
2090 if (!Unit)
2091 Unit = FindCompileUnit(SPD.getCompileUnit());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002092
2093 // Get the subprogram die.
Devang Patelf6bac3e2009-01-12 22:58:14 +00002094 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002095 assert(SPDie && "Missing subprogram descriptor");
2096
2097 // Add the function bounds.
2098 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2099 DWLabel("func_begin", SubprogramCount));
2100 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2101 DWLabel("func_end", SubprogramCount));
2102 MachineLocation Location(RI->getFrameRegister(*MF));
2103 AddAddress(SPDie, DW_AT_frame_base, Location);
2104
2105 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2106 }
2107
2108 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2109 ///
2110 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Chenge3d42322009-02-25 07:04:34 +00002111 const char *FnName = MF->getFunction()->getNameStart();
2112 if (MainCU) {
Bill Wendling972bbac2009-04-09 21:49:15 +00002113 StringMap<DIE*> &Globals = MainCU->getGlobals();
2114 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Chenge3d42322009-02-25 07:04:34 +00002115 if (GI != Globals.end()) {
2116 DIE *SPDie = GI->second;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002117
2118 // Add the function bounds.
2119 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2120 DWLabel("func_begin", SubprogramCount));
2121 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2122 DWLabel("func_end", SubprogramCount));
2123
2124 MachineLocation Location(RI->getFrameRegister(*MF));
2125 AddAddress(SPDie, DW_AT_frame_base, Location);
2126 return;
2127 }
Evan Chenge3d42322009-02-25 07:04:34 +00002128 } else {
2129 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2130 CompileUnit *Unit = CompileUnits[i];
Bill Wendling972bbac2009-04-09 21:49:15 +00002131 StringMap<DIE*> &Globals = Unit->getGlobals();
2132 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Chenge3d42322009-02-25 07:04:34 +00002133 if (GI != Globals.end()) {
2134 DIE *SPDie = GI->second;
2135
2136 // Add the function bounds.
2137 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2138 DWLabel("func_begin", SubprogramCount));
2139 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2140 DWLabel("func_end", SubprogramCount));
2141
2142 MachineLocation Location(RI->getFrameRegister(*MF));
2143 AddAddress(SPDie, DW_AT_frame_base, Location);
2144 return;
2145 }
2146 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00002147 }
Evan Chenge3d42322009-02-25 07:04:34 +00002148
Devang Patel7a6e5a32009-01-08 02:33:41 +00002149#if 0
2150 // FIXME: This is causing an abort because C++ mangled names are compared
2151 // with their unmangled counterparts. See PR2885. Don't do this assert.
2152 assert(0 && "Couldn't find DIE for machine function!");
2153#endif
Evan Chenge3d42322009-02-25 07:04:34 +00002154 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002155 }
2156
Jim Laskeyef42a012006-11-02 20:12:39 +00002157 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2158 /// tools to recognize the object file contains Dwarf information.
2159 void EmitInitial() {
2160 // Check to see if we already emitted intial headers.
2161 if (didInitial) return;
2162 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002163
Jim Laskeyef42a012006-11-02 20:12:39 +00002164 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002165 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002166 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002167 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002168 }
2169 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2170 EmitLabel("section_info", 0);
2171 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2172 EmitLabel("section_abbrev", 0);
2173 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2174 EmitLabel("section_aranges", 0);
Scott Michel210de722009-01-26 22:32:51 +00002175 if (TAI->doesSupportMacInfoSection()) {
2176 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2177 EmitLabel("section_macinfo", 0);
2178 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002179 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2180 EmitLabel("section_line", 0);
2181 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2182 EmitLabel("section_loc", 0);
2183 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2184 EmitLabel("section_pubnames", 0);
2185 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2186 EmitLabel("section_str", 0);
2187 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2188 EmitLabel("section_ranges", 0);
2189
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002190 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002191 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002192 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002193 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002194 }
2195
Jim Laskey65195462006-10-30 13:35:07 +00002196 /// EmitDIE - Recusively Emits a debug information entry.
2197 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002198 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002199 // Get the abbreviation for this DIE.
2200 unsigned AbbrevNumber = Die->getAbbrevNumber();
2201 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002202
Jim Laskeybacd3042007-02-21 22:48:45 +00002203 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002204
2205 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002206 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002207
Evan Cheng42bf74b2009-03-25 01:47:28 +00002208 if (Asm->isVerbose())
Evan Cheng6547e402008-07-01 23:18:29 +00002209 Asm->EOL(std::string("Abbrev [" +
2210 utostr(AbbrevNumber) +
2211 "] 0x" + utohexstr(Die->getOffset()) +
2212 ":0x" + utohexstr(Die->getSize()) + " " +
2213 TagString(Abbrev->getTag())));
2214 else
2215 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002216
Owen Anderson873e1b52008-06-24 21:44:59 +00002217 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2218 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002219
Jim Laskeyef42a012006-11-02 20:12:39 +00002220 // Emit the DIE attribute values.
2221 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2222 unsigned Attr = AbbrevData[i].getAttribute();
2223 unsigned Form = AbbrevData[i].getForm();
2224 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002225
Jim Laskeyef42a012006-11-02 20:12:39 +00002226 switch (Attr) {
2227 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002228 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002229 break;
2230 }
2231 default: {
2232 // Emit an attribute using the defined form.
2233 Values[i]->EmitValue(*this, Form);
2234 break;
2235 }
2236 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002237
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002238 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002239 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002240
Jim Laskeyef42a012006-11-02 20:12:39 +00002241 // Emit the DIE children if any.
2242 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2243 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002244
Jim Laskeyef42a012006-11-02 20:12:39 +00002245 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2246 EmitDIE(Children[j]);
2247 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002248
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002249 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002250 }
2251 }
2252
Jim Laskey65195462006-10-30 13:35:07 +00002253 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2254 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002255 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2256 // Get the children.
2257 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002258
Jim Laskeyef42a012006-11-02 20:12:39 +00002259 // If not last sibling and has children then add sibling offset attribute.
2260 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2261
2262 // Record the abbreviation.
2263 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002264
Jim Laskeyef42a012006-11-02 20:12:39 +00002265 // Get the abbreviation for this DIE.
2266 unsigned AbbrevNumber = Die->getAbbrevNumber();
2267 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2268
2269 // Set DIE offset
2270 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002271
Jim Laskeyef42a012006-11-02 20:12:39 +00002272 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002273 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2274
Owen Anderson873e1b52008-06-24 21:44:59 +00002275 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2276 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002277
2278 // Size the DIE attribute values.
2279 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2280 // Size attribute value.
2281 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2282 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002283
Jim Laskeyef42a012006-11-02 20:12:39 +00002284 // Size the DIE children if any.
2285 if (!Children.empty()) {
2286 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2287 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002288
Jim Laskeyef42a012006-11-02 20:12:39 +00002289 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2290 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2291 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002292
Jim Laskeyef42a012006-11-02 20:12:39 +00002293 // End of children marker.
2294 Offset += sizeof(int8_t);
2295 }
2296
2297 Die->setSize(Offset - Die->getOffset());
2298 return Offset;
2299 }
Jim Laskey65195462006-10-30 13:35:07 +00002300
2301 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2302 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002303 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002304 // Process base compile unit.
Devang Pateldd9db662009-01-30 18:20:31 +00002305 if (MainCU) {
2306 // Compute size of compile unit header
2307 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2308 sizeof(int16_t) + // DWARF version number
2309 sizeof(int32_t) + // Offset Into Abbrev. Section
2310 sizeof(int8_t); // Pointer Size (in bytes)
2311 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
2312 return;
2313 }
Evan Chenge3d42322009-02-25 07:04:34 +00002314 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2315 CompileUnit *Unit = CompileUnits[i];
Devang Patel72b66352009-01-12 23:05:55 +00002316 // Compute size of compile unit header
2317 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2318 sizeof(int16_t) + // DWARF version number
2319 sizeof(int32_t) + // Offset Into Abbrev. Section
2320 sizeof(int8_t); // Pointer Size (in bytes)
2321 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2322 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002323 }
2324
Evan Chenge3d42322009-02-25 07:04:34 +00002325 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Jim Laskey65195462006-10-30 13:35:07 +00002326 ///
Evan Chenge3d42322009-02-25 07:04:34 +00002327 void EmitDebugInfoPerCU(CompileUnit *Unit) {
2328 DIE *Die = Unit->getDie();
2329 // Emit the compile units header.
2330 EmitLabel("info_begin", Unit->getID());
2331 // Emit size of content not including length itself
2332 unsigned ContentSize = Die->getSize() +
2333 sizeof(int16_t) + // DWARF version number
2334 sizeof(int32_t) + // Offset Into Abbrev. Section
2335 sizeof(int8_t) + // Pointer Size (in bytes)
2336 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2337
2338 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2339 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2340 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2341 Asm->EOL("Offset Into Abbrev. Section");
2342 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2343
2344 EmitDIE(Die);
2345 // FIXME - extra padding for gdb bug.
2346 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2347 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2348 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2349 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2350 EmitLabel("info_end", Unit->getID());
2351
2352 Asm->EOL();
2353 }
2354
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002355 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002356 // Start debug info section.
2357 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002358
Evan Chenge3d42322009-02-25 07:04:34 +00002359 if (MainCU) {
2360 EmitDebugInfoPerCU(MainCU);
2361 return;
Devang Patel72b66352009-01-12 23:05:55 +00002362 }
Evan Chenge3d42322009-02-25 07:04:34 +00002363
2364 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2365 EmitDebugInfoPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002366 }
2367
Jim Laskey65195462006-10-30 13:35:07 +00002368 /// EmitAbbreviations - Emit the abbreviation section.
2369 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002370 void EmitAbbreviations() const {
2371 // Check to see if it is worth the effort.
2372 if (!Abbreviations.empty()) {
2373 // Start the debug abbrev section.
2374 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002375
Jim Laskeyef42a012006-11-02 20:12:39 +00002376 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002377
Jim Laskeyef42a012006-11-02 20:12:39 +00002378 // For each abbrevation.
2379 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2380 // Get abbreviation data
2381 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002382
Jim Laskeyef42a012006-11-02 20:12:39 +00002383 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002384 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2385 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002386
Jim Laskeyef42a012006-11-02 20:12:39 +00002387 // Emit the abbreviations data.
2388 Abbrev->Emit(*this);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002389
Jim Laskeybacd3042007-02-21 22:48:45 +00002390 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002391 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002392
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002393 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00002394 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002395
Jim Laskeyef42a012006-11-02 20:12:39 +00002396 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002397
Jim Laskeybacd3042007-02-21 22:48:45 +00002398 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002399 }
2400 }
2401
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002402 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2403 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002404 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002405 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2406 // Define last address of section.
2407 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2408 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2409 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2410 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2411
2412 // Mark end of matrix.
2413 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2414 Asm->EmitULEB128Bytes(1); Asm->EOL();
2415 Asm->EmitInt8(1); Asm->EOL();
2416 }
2417
Jim Laskey65195462006-10-30 13:35:07 +00002418 /// EmitDebugLines - Emit source line information.
2419 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002420 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002421 // If the target is using .loc/.file, the assembler will be emitting the
2422 // .debug_line table automatically.
2423 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00002424 return;
2425
Jim Laskeyef42a012006-11-02 20:12:39 +00002426 // Minimum line delta, thus ranging from -10..(255-10).
2427 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2428 // Maximum line delta, thus ranging from -10..(255-10).
2429 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002430
Jim Laskeyef42a012006-11-02 20:12:39 +00002431 // Start the dwarf line section.
2432 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002433
Jim Laskeyef42a012006-11-02 20:12:39 +00002434 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002435
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002436 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002437 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002438 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002439
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002440 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002441
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002442 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002443 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002444 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002445
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002446 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002447
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002448 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002449
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002450 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002451
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002452 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002453
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002454 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002455
Jim Laskeyef42a012006-11-02 20:12:39 +00002456 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002457 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2458 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2459 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2460 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2461 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2462 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2463 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2464 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2465 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00002466
Jim Laskeyef42a012006-11-02 20:12:39 +00002467 // Emit directories.
Evan Chenge3d42322009-02-25 07:04:34 +00002468 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2469 Asm->EmitString(getSourceDirectoryName(DI));
2470 Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00002471 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002472 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002473
Jim Laskeyef42a012006-11-02 20:12:39 +00002474 // Emit files.
Evan Chenge3d42322009-02-25 07:04:34 +00002475 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2476 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002477 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Evan Chenge3d42322009-02-25 07:04:34 +00002478 Asm->EmitString(getSourceFileName(Id.second));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002479 Asm->EOL("Source");
Evan Chenge3d42322009-02-25 07:04:34 +00002480 Asm->EmitULEB128Bytes(Id.first);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002481 Asm->EOL("Directory #");
2482 Asm->EmitULEB128Bytes(0);
2483 Asm->EOL("Mod date");
2484 Asm->EmitULEB128Bytes(0);
2485 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002486 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002487 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002488
Jim Laskeyef42a012006-11-02 20:12:39 +00002489 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002490
Jim Laskeyef42a012006-11-02 20:12:39 +00002491 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002492 unsigned SecSrcLinesSize = SectionSourceLines.size();
2493
2494 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002495 // Isolate current sections line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00002496 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00002497
Evan Cheng42bf74b2009-03-25 01:47:28 +00002498 if (Asm->isVerbose()) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002499 const Section* S = SectionMap[j + 1];
Evan Chenge3d42322009-02-25 07:04:34 +00002500 O << '\t' << TAI->getCommentString() << " Section"
2501 << S->getName() << '\n';
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002502 } else
Evan Cheng6547e402008-07-01 23:18:29 +00002503 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002504
2505 // Dwarf assumes we start with first line of first source file.
2506 unsigned Source = 1;
2507 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002508
Jim Laskeyef42a012006-11-02 20:12:39 +00002509 // Construct rows of the address, source, line, column matrix.
2510 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patelf3ee5142009-01-12 22:54:42 +00002511 const SrcLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002512 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00002513 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002514
Evan Cheng42bf74b2009-03-25 01:47:28 +00002515 if (!Asm->isVerbose())
Evan Cheng6547e402008-07-01 23:18:29 +00002516 Asm->EOL();
Evan Chenge3d42322009-02-25 07:04:34 +00002517 else {
2518 std::pair<unsigned, unsigned> SourceID =
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002519 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Evan Chenge3d42322009-02-25 07:04:34 +00002520 O << '\t' << TAI->getCommentString() << ' '
2521 << getSourceDirectoryName(SourceID.first) << ' '
2522 << getSourceFileName(SourceID.second)
2523 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2524 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002525
2526 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002527 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00002528 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002529 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00002530 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002531
Jim Laskeyef42a012006-11-02 20:12:39 +00002532 // If change of source, then switch to the new source.
2533 if (Source != LineInfo.getSourceID()) {
2534 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002535 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2536 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00002537 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002538
Jim Laskeyef42a012006-11-02 20:12:39 +00002539 // If change of line.
2540 if (Line != LineInfo.getLine()) {
2541 // Determine offset.
2542 int Offset = LineInfo.getLine() - Line;
2543 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002544
Jim Laskeyef42a012006-11-02 20:12:39 +00002545 // Update line.
2546 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002547
Jim Laskeyef42a012006-11-02 20:12:39 +00002548 // If delta is small enough and in range...
2549 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2550 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002551 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00002552 } else {
2553 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002554 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2555 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2556 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002557 }
2558 } else {
2559 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002560 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002561 }
2562 }
2563
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002564 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002565 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002566
2567 if (SecSrcLinesSize == 0)
2568 // Because we're emitting a debug_line section, we still need a line
2569 // table. The linker and friends expect it to exist. If there's nothing to
2570 // put into it, emit an empty table.
2571 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002572
Jim Laskeyef42a012006-11-02 20:12:39 +00002573 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002574
Jim Laskeybacd3042007-02-21 22:48:45 +00002575 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002576 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002577
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002578 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00002579 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002580 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002581 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00002582 return;
2583
2584 int stackGrowth =
2585 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2586 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002587 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00002588
2589 // Start the dwarf frame section.
2590 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2591
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002592 EmitLabel("debug_frame_common", 0);
2593 EmitDifference("debug_frame_common_end", 0,
2594 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002595 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002596
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002597 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002598 Asm->EmitInt32((int)DW_CIE_ID);
2599 Asm->EOL("CIE Identifier Tag");
2600 Asm->EmitInt8(DW_CIE_VERSION);
2601 Asm->EOL("CIE Version");
2602 Asm->EmitString("");
2603 Asm->EOL("CIE Augmentation");
2604 Asm->EmitULEB128Bytes(1);
2605 Asm->EOL("CIE Code Alignment Factor");
2606 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002607 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002608 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002609 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002610
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002611 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002612 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002613
Dale Johannesenb97aec62007-11-13 19:13:01 +00002614 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002615
Evan Cheng05548eb2008-02-29 19:36:59 +00002616 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002617 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002618
Jim Laskeybacd3042007-02-21 22:48:45 +00002619 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002620 }
2621
Jim Laskey65195462006-10-30 13:35:07 +00002622 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2623 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002624 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002625 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00002626 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002627
Jim Laskeyef42a012006-11-02 20:12:39 +00002628 // Start the dwarf frame section.
2629 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002630
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002631 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2632 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002633 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002634
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002635 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002636
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002637 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2638 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002639 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002640
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002641 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002642 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002643 EmitDifference("func_end", DebugFrameInfo.Number,
2644 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002645 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002646
Devang Patel5aac3d32009-01-17 08:01:33 +00002647 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00002648 false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002649
Evan Cheng05548eb2008-02-29 19:36:59 +00002650 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002651 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00002652
Jim Laskeybacd3042007-02-21 22:48:45 +00002653 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002654 }
2655
Evan Chenge3d42322009-02-25 07:04:34 +00002656 void EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2657 EmitDifference("pubnames_end", Unit->getID(),
2658 "pubnames_begin", Unit->getID(), true);
2659 Asm->EOL("Length of Public Names Info");
2660
2661 EmitLabel("pubnames_begin", Unit->getID());
2662
2663 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2664
2665 EmitSectionOffset("info_begin", "section_info",
2666 Unit->getID(), 0, true, false);
2667 Asm->EOL("Offset of Compilation Unit Info");
2668
2669 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2670 true);
2671 Asm->EOL("Compilation Unit Length");
2672
Bill Wendling972bbac2009-04-09 21:49:15 +00002673 StringMap<DIE*> &Globals = Unit->getGlobals();
Bill Wendlingf34be822009-04-09 23:51:31 +00002674 for (StringMap<DIE*>::const_iterator
Bill Wendling972bbac2009-04-09 21:49:15 +00002675 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
Bill Wendlingf34be822009-04-09 23:51:31 +00002676 const char *Name = GI->getKeyData();
Evan Chenge3d42322009-02-25 07:04:34 +00002677 DIE * Entity = GI->second;
2678
2679 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
Bill Wendlingf34be822009-04-09 23:51:31 +00002680 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
Evan Chenge3d42322009-02-25 07:04:34 +00002681 }
2682
2683 Asm->EmitInt32(0); Asm->EOL("End Mark");
2684 EmitLabel("pubnames_end", Unit->getID());
2685
2686 Asm->EOL();
2687 }
2688
Jim Laskeyef42a012006-11-02 20:12:39 +00002689 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002690 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002691 void EmitDebugPubNames() {
2692 // Start the dwarf pubnames section.
2693 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002694
Evan Chenge3d42322009-02-25 07:04:34 +00002695 if (MainCU) {
2696 EmitDebugPubNamesPerCU(MainCU);
2697 return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002698 }
Evan Chenge3d42322009-02-25 07:04:34 +00002699
2700 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2701 EmitDebugPubNamesPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002702 }
2703
2704 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002705 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002706 void EmitDebugStr() {
2707 // Check to see if it is worth the effort.
2708 if (!StringPool.empty()) {
2709 // Start the dwarf str section.
2710 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002711
Jim Laskeyef42a012006-11-02 20:12:39 +00002712 // For each of strings in the string pool.
2713 for (unsigned StringID = 1, N = StringPool.size();
2714 StringID <= N; ++StringID) {
2715 // Emit a label for reference from debug information entries.
2716 EmitLabel("string", StringID);
2717 // Emit the string itself.
2718 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00002719 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002720 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002721
Jim Laskeybacd3042007-02-21 22:48:45 +00002722 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002723 }
2724 }
2725
2726 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002727 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002728 void EmitDebugLoc() {
2729 // Start the dwarf loc section.
2730 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002731
Jim Laskeybacd3042007-02-21 22:48:45 +00002732 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002733 }
2734
2735 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002736 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002737 void EmitDebugARanges() {
2738 // Start the dwarf aranges section.
2739 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002740
Jim Laskeyef42a012006-11-02 20:12:39 +00002741 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00002742#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002743 CompileUnit *Unit = GetBaseCompileUnit();
2744
Jim Laskey5496f012006-11-09 14:52:14 +00002745 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002746 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002747
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002748 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002749
Jim Laskey5496f012006-11-09 14:52:14 +00002750 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002751 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002752
Dan Gohman82482942007-09-27 23:12:31 +00002753 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002754
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002755 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002756
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002757 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2758 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002759
Jim Laskey5496f012006-11-09 14:52:14 +00002760 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002761 EmitReference("text_begin", 0); Asm->EOL("Address");
2762 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002763
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002764 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2765 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00002766#endif
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002767
Jim Laskeybacd3042007-02-21 22:48:45 +00002768 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002769 }
2770
2771 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002772 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002773 void EmitDebugRanges() {
2774 // Start the dwarf ranges section.
2775 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002776
Jim Laskeybacd3042007-02-21 22:48:45 +00002777 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002778 }
2779
2780 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002781 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002782 void EmitDebugMacInfo() {
Scott Michel210de722009-01-26 22:32:51 +00002783 if (TAI->doesSupportMacInfoSection()) {
2784 // Start the dwarf macinfo section.
2785 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002786
Scott Michel210de722009-01-26 22:32:51 +00002787 Asm->EOL();
2788 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002789 }
2790
Devang Patel0f7fef32009-04-13 17:02:03 +00002791 /// EmitDebugInlineInfo - Emit inline info using following format.
2792 /// Section Header:
2793 /// 1. length of section
2794 /// 2. Dwarf version number
2795 /// 3. address size.
2796 ///
2797 /// Entries (one "entry" for each function that was inlined):
2798 ///
2799 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
2800 /// otherwise offset into __debug_str for regular function name.
2801 /// 2. offset into __debug_str section for regular function name.
2802 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
2803 /// instances for the function.
2804 ///
2805 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
2806 /// inlined instance; the die_offset points to the inlined_subroutine die in
2807 /// the __debug_info section, and the low_pc is the starting address for the
2808 /// inlining instance.
2809 void EmitDebugInlineInfo() {
2810 if (!TAI->doesDwarfUsesInlineInfoSection())
2811 return;
2812
2813 if (!MainCU)
2814 return;
2815
2816 Asm->SwitchToDataSection(TAI->getDwarfDebugInlineSection());
2817 Asm->EOL();
2818 EmitDifference("debug_inlined_end", 1,
2819 "debug_inlined_begin", 1, true);
2820 Asm->EOL("Length of Debug Inlined Information Entry");
2821
2822 EmitLabel("debug_inlined_begin", 1);
2823
2824 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
2825 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2826
2827 for (DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
2828 I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2829 GlobalVariable *GV = I->first;
2830 SmallVector<unsigned, 4> &Labels = I->second;
2831 DISubprogram SP(GV);
2832 std::string Name;
2833 std::string LName;
2834
2835 SP.getLinkageName(LName);
2836 SP.getName(Name);
2837
2838 Asm->EmitString(LName.empty() ? Name : LName);
2839 Asm->EOL("MIPS linkage name");
2840
2841 Asm->EmitString(Name); Asm->EOL("Function name");
2842
2843 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2844
2845 for (SmallVector<unsigned, 4>::iterator LI = Labels.begin(),
2846 LE = Labels.end(); LI != LE; ++LI) {
2847 DIE *SP = MainCU->getDieMapSlotFor(GV);
2848 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2849
2850 if (TD->getPointerSize() == sizeof(int32_t))
2851 O << TAI->getData32bitsDirective();
2852 else
2853 O << TAI->getData64bitsDirective();
2854 PrintLabelName("label", *LI); Asm->EOL("low_pc");
2855 }
2856 }
2857
2858 EmitLabel("debug_inlined_end", 1);
2859 Asm->EOL();
2860 }
2861
Bill Wendlingc8615e42009-03-10 21:47:45 +00002862 /// GetOrCreateSourceID - Look up the source id with the given directory and
2863 /// source file names. If none currently exists, create a new id and insert it
2864 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
2865 /// as well.
2866 unsigned GetOrCreateSourceID(const std::string &DirName,
2867 const std::string &FileName) {
2868 unsigned DId;
2869 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
2870 if (DI != DirectoryIdMap.end()) {
2871 DId = DI->getValue();
2872 } else {
2873 DId = DirectoryNames.size() + 1;
2874 DirectoryIdMap[DirName] = DId;
2875 DirectoryNames.push_back(DirName);
2876 }
2877
2878 unsigned FId;
2879 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
2880 if (FI != SourceFileIdMap.end()) {
2881 FId = FI->getValue();
2882 } else {
2883 FId = SourceFileNames.size() + 1;
2884 SourceFileIdMap[FileName] = FId;
2885 SourceFileNames.push_back(FileName);
2886 }
2887
2888 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
2889 SourceIdMap.find(std::make_pair(DId, FId));
2890 if (SI != SourceIdMap.end())
2891 return SI->second;
2892
2893 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
2894 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
2895 SourceIds.push_back(std::make_pair(DId, FId));
2896
2897 return SrcId;
2898 }
2899
Evan Chenge3d42322009-02-25 07:04:34 +00002900 void ConstructCompileUnit(GlobalVariable *GV) {
2901 DICompileUnit DIUnit(GV);
Bill Wendling0582ae92009-03-13 04:39:26 +00002902 std::string Dir, FN, Prod;
2903 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
2904 DIUnit.getFilename(FN));
Evan Chenge3d42322009-02-25 07:04:34 +00002905
2906 DIE *Die = new DIE(DW_TAG_compile_unit);
2907 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2908 DWLabel("section_line", 0), DWLabel("section_line", 0),
2909 false);
Bill Wendling0582ae92009-03-13 04:39:26 +00002910 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer(Prod));
Evan Chenge3d42322009-02-25 07:04:34 +00002911 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002912 AddString(Die, DW_AT_name, DW_FORM_string, FN);
Bill Wendling0582ae92009-03-13 04:39:26 +00002913 if (!Dir.empty())
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002914 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Chenge3d42322009-02-25 07:04:34 +00002915 if (DIUnit.isOptimized())
2916 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendling0582ae92009-03-13 04:39:26 +00002917 std::string Flags;
2918 DIUnit.getFlags(Flags);
2919 if (!Flags.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00002920 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
2921 unsigned RVer = DIUnit.getRunTimeVersion();
2922 if (RVer)
2923 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
2924
2925 CompileUnit *Unit = new CompileUnit(ID, Die);
2926 if (DIUnit.isMain()) {
2927 assert(!MainCU && "Multiple main compile units are found!");
2928 MainCU = Unit;
2929 }
2930 CompileUnitMap[DIUnit.getGV()] = Unit;
2931 CompileUnits.push_back(Unit);
2932 }
2933
Devang Patelc4523242009-01-05 23:11:11 +00002934 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Pateld1ca9252009-01-05 23:03:32 +00002935 void ConstructCompileUnits() {
Evan Chenge3d42322009-02-25 07:04:34 +00002936 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.compile_units");
2937 if (!Root)
2938 return;
2939 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2940 "Malformed compile unit descriptor anchor type");
2941 Constant *RootC = cast<Constant>(*Root->use_begin());
2942 assert(RootC->hasNUsesOrMore(1) &&
2943 "Malformed compile unit descriptor anchor type");
2944 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2945 UI != UE; ++UI)
2946 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2947 UUI != UUE; ++UUI) {
2948 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2949 ConstructCompileUnit(GV);
Devang Pateldd9db662009-01-30 18:20:31 +00002950 }
Evan Chenge3d42322009-02-25 07:04:34 +00002951 }
2952
2953 bool ConstructGlobalVariableDIE(GlobalVariable *GV) {
2954 DIGlobalVariable DI_GV(GV);
2955 CompileUnit *DW_Unit = MainCU;
2956 if (!DW_Unit)
2957 DW_Unit = FindCompileUnit(DI_GV.getCompileUnit());
2958
2959 // Check for pre-existence.
2960 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
2961 if (Slot)
2962 return false;
2963
2964 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
2965
2966 // Add address.
2967 DIEBlock *Block = new DIEBlock();
2968 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Bill Wendling7d16e852009-04-10 00:12:49 +00002969 std::string GLN;
Evan Chenge3d42322009-02-25 07:04:34 +00002970 AddObjectLabel(Block, 0, DW_FORM_udata,
Bill Wendling7d16e852009-04-10 00:12:49 +00002971 Asm->getGlobalLinkName(DI_GV.getGlobal(), GLN));
Evan Chenge3d42322009-02-25 07:04:34 +00002972 AddBlock(VariableDie, DW_AT_location, 0, Block);
2973
2974 // Add to map.
2975 Slot = VariableDie;
2976 // Add to context owner.
2977 DW_Unit->getDie()->AddChild(VariableDie);
2978 // Expose as global. FIXME - need to check external flag.
Bill Wendling0582ae92009-03-13 04:39:26 +00002979 std::string Name;
2980 DW_Unit->AddGlobal(DI_GV.getName(Name), VariableDie);
Evan Chenge3d42322009-02-25 07:04:34 +00002981 return true;
Devang Pateld1ca9252009-01-05 23:03:32 +00002982 }
2983
Devang Patelc4523242009-01-05 23:11:11 +00002984 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patel53cac182009-02-24 00:02:15 +00002985 /// visible global variables. Return true if at least one global DIE is
2986 /// created.
2987 bool ConstructGlobalVariableDIEs() {
Evan Chenge3d42322009-02-25 07:04:34 +00002988 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
2989 if (!Root)
2990 return false;
Devang Patelc4523242009-01-05 23:11:11 +00002991
Evan Chenge3d42322009-02-25 07:04:34 +00002992 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2993 "Malformed global variable descriptor anchor type");
2994 Constant *RootC = cast<Constant>(*Root->use_begin());
2995 assert(RootC->hasNUsesOrMore(1) &&
2996 "Malformed global variable descriptor anchor type");
Devang Patelc4523242009-01-05 23:11:11 +00002997
Evan Chenge3d42322009-02-25 07:04:34 +00002998 bool Result = false;
2999 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3000 UI != UE; ++UI)
3001 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3002 UUI != UUE; ++UUI) {
3003 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3004 Result |= ConstructGlobalVariableDIE(GV);
3005 }
3006 return Result;
3007 }
Devang Patelc4523242009-01-05 23:11:11 +00003008
Evan Chenge3d42322009-02-25 07:04:34 +00003009 bool ConstructSubprogram(GlobalVariable *GV) {
3010 DISubprogram SP(GV);
3011 CompileUnit *Unit = MainCU;
3012 if (!Unit)
3013 Unit = FindCompileUnit(SP.getCompileUnit());
Devang Patelc4523242009-01-05 23:11:11 +00003014
Evan Chenge3d42322009-02-25 07:04:34 +00003015 // Check for pre-existence.
3016 DIE *&Slot = Unit->getDieMapSlotFor(GV);
3017 if (Slot)
3018 return false;
3019
3020 if (!SP.isDefinition())
3021 // This is a method declaration which will be handled while
3022 // constructing class type.
3023 return false;
3024
3025 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
3026
3027 // Add to map.
3028 Slot = SubprogramDie;
3029 // Add to context owner.
3030 Unit->getDie()->AddChild(SubprogramDie);
3031 // Expose as global.
Bill Wendling0582ae92009-03-13 04:39:26 +00003032 std::string Name;
3033 Unit->AddGlobal(SP.getName(Name), SubprogramDie);
Evan Chenge3d42322009-02-25 07:04:34 +00003034 return true;
Devang Patelc4523242009-01-05 23:11:11 +00003035 }
3036
Devang Patel78eb6ad2009-01-05 23:21:35 +00003037 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patel53cac182009-02-24 00:02:15 +00003038 /// subprograms. Return true if at least one subprogram DIE is created.
3039 bool ConstructSubprograms() {
Evan Chenge3d42322009-02-25 07:04:34 +00003040 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
3041 if (!Root)
3042 return false;
Devang Patel78eb6ad2009-01-05 23:21:35 +00003043
Evan Chenge3d42322009-02-25 07:04:34 +00003044 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3045 "Malformed subprogram descriptor anchor type");
3046 Constant *RootC = cast<Constant>(*Root->use_begin());
3047 assert(RootC->hasNUsesOrMore(1) &&
3048 "Malformed subprogram descriptor anchor type");
Devang Patel78eb6ad2009-01-05 23:21:35 +00003049
Evan Chenge3d42322009-02-25 07:04:34 +00003050 bool Result = false;
3051 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3052 UI != UE; ++UI)
3053 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3054 UUI != UUE; ++UUI) {
3055 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3056 Result |= ConstructSubprogram(GV);
3057 }
3058 return Result;
Devang Patel78eb6ad2009-01-05 23:21:35 +00003059 }
3060
Jim Laskey65195462006-10-30 13:35:07 +00003061public:
Jim Laskeyef42a012006-11-02 20:12:39 +00003062 //===--------------------------------------------------------------------===//
3063 // Main entry points.
3064 //
Owen Andersoncb371882008-08-21 00:14:44 +00003065 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00003066 : Dwarf(OS, A, T, "dbg"), MainCU(0),
3067 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
3068 ValuesSet(InitValuesSetSize), Values(), StringPool(), SectionMap(),
3069 SectionSourceLines(), didInitial(false), shouldEmit(false),
3070 RootDbgScope(0), DebugTimer(0) {
3071 if (TimePassesIsEnabled)
3072 DebugTimer = new Timer("Dwarf Debug Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00003073 getDwarfTimerGroup());
Jim Laskeyef42a012006-11-02 20:12:39 +00003074 }
Jim Laskey072200c2007-01-29 18:51:14 +00003075 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003076 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3077 delete Values[j];
Bill Wendling163ba3f2009-03-10 21:23:25 +00003078
3079 delete DebugTimer;
Jim Laskeyef42a012006-11-02 20:12:39 +00003080 }
3081
Bill Wendlingc8615e42009-03-10 21:47:45 +00003082 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
3083 /// be emitted.
3084 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
3085
Devang Patel5d315982009-01-06 21:07:30 +00003086 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3087 /// This is inovked by the target AsmPrinter.
Devang Patel3f7833a2009-01-12 23:09:42 +00003088 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003089 if (TimePassesIsEnabled)
3090 DebugTimer->startTimer();
3091
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003092 // Create all the compile unit DIEs.
3093 ConstructCompileUnits();
Devang Patel3f7833a2009-01-12 23:09:42 +00003094
Bill Wendling163ba3f2009-03-10 21:23:25 +00003095 if (CompileUnits.empty()) {
3096 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003097 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003098
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003099 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003100 }
Devang Patel3f7833a2009-01-12 23:09:42 +00003101
Devang Patel53cac182009-02-24 00:02:15 +00003102 // Create DIEs for each of the externally visible global variables.
3103 bool globalDIEs = ConstructGlobalVariableDIEs();
3104
3105 // Create DIEs for each of the externally visible subprograms.
3106 bool subprogramDIEs = ConstructSubprograms();
3107
3108 // If there is not any debug info available for any global variables
3109 // and any subprograms then there is not any debug info to emit.
Bill Wendling163ba3f2009-03-10 21:23:25 +00003110 if (!globalDIEs && !subprogramDIEs) {
3111 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003112 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003113
Devang Patel53cac182009-02-24 00:02:15 +00003114 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003115 }
Devang Patel53cac182009-02-24 00:02:15 +00003116
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003117 MMI = mmi;
3118 shouldEmit = true;
3119 MMI->setDebugInfoAvailability(true);
Devang Patel5d315982009-01-06 21:07:30 +00003120
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003121 // Prime section data.
3122 SectionMap.insert(TAI->getTextSection());
Devang Patel5d315982009-01-06 21:07:30 +00003123
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003124 // Print out .file directives to specify files for .loc directives. These
3125 // are printed out early so that they precede any .loc directives.
3126 if (TAI->hasDotLocAndDotFile()) {
Evan Chenge3d42322009-02-25 07:04:34 +00003127 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
3128 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00003129 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Evan Chenge3d42322009-02-25 07:04:34 +00003130 sys::Path FullPath(getSourceDirectoryName(Id.first));
3131 bool AppendOk =
3132 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003133 assert(AppendOk && "Could not append filename to directory!");
3134 AppendOk = false;
3135 Asm->EmitFile(i, FullPath.toString());
3136 Asm->EOL();
Devang Patel5d315982009-01-06 21:07:30 +00003137 }
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003138 }
Devang Patel5d315982009-01-06 21:07:30 +00003139
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003140 // Emit initial sections
3141 EmitInitial();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003142
3143 if (TimePassesIsEnabled)
3144 DebugTimer->stopTimer();
Devang Patel5d315982009-01-06 21:07:30 +00003145 }
3146
Jim Laskey65195462006-10-30 13:35:07 +00003147 /// BeginModule - Emit all Dwarf sections that should come prior to the
3148 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00003149 void BeginModule(Module *M) {
3150 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00003151 }
3152
Jim Laskey65195462006-10-30 13:35:07 +00003153 /// EndModule - Emit all Dwarf sections that should come after the content.
3154 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003155 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003156 if (!ShouldEmitDwarfDebug())
3157 return;
3158
3159 if (TimePassesIsEnabled)
3160 DebugTimer->startTimer();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003161
Jim Laskeyef42a012006-11-02 20:12:39 +00003162 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003163 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003164 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00003165 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003166 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003167
Jim Laskeyef42a012006-11-02 20:12:39 +00003168 // End text sections.
3169 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003170 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00003171 EmitLabel("section_end", i);
3172 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003173
3174 // Emit common frame information.
3175 EmitCommonDebugFrame();
3176
3177 // Emit function debug frame information
3178 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3179 E = DebugFrames.end(); I != E; ++I)
3180 EmitFunctionDebugFrame(*I);
3181
Jim Laskeyef42a012006-11-02 20:12:39 +00003182 // Compute DIE offsets and sizes.
3183 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003184
Jim Laskeyef42a012006-11-02 20:12:39 +00003185 // Emit all the DIEs into a debug info section
3186 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003187
Jim Laskeyef42a012006-11-02 20:12:39 +00003188 // Corresponding abbreviations into a abbrev section.
3189 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003190
Jim Laskeyef42a012006-11-02 20:12:39 +00003191 // Emit source line correspondence into a debug line section.
3192 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003193
Jim Laskeyef42a012006-11-02 20:12:39 +00003194 // Emit info into a debug pubnames section.
3195 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003196
Jim Laskeyef42a012006-11-02 20:12:39 +00003197 // Emit info into a debug str section.
3198 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003199
Jim Laskeyef42a012006-11-02 20:12:39 +00003200 // Emit info into a debug loc section.
3201 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003202
Jim Laskeyef42a012006-11-02 20:12:39 +00003203 // Emit info into a debug aranges section.
3204 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003205
Jim Laskeyef42a012006-11-02 20:12:39 +00003206 // Emit info into a debug ranges section.
3207 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003208
Jim Laskeyef42a012006-11-02 20:12:39 +00003209 // Emit info into a debug macinfo section.
3210 EmitDebugMacInfo();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003211
Devang Patel0f7fef32009-04-13 17:02:03 +00003212 // Emit inline info.
3213 EmitDebugInlineInfo();
3214
Bill Wendling163ba3f2009-03-10 21:23:25 +00003215 if (TimePassesIsEnabled)
3216 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003217 }
3218
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003219 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00003220 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00003221 void BeginFunction(MachineFunction *MF) {
Bill Wendling1362f972009-03-11 00:03:50 +00003222 this->MF = MF;
3223
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003224 if (!ShouldEmitDwarfDebug()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00003225
Bill Wendling163ba3f2009-03-10 21:23:25 +00003226 if (TimePassesIsEnabled)
3227 DebugTimer->startTimer();
3228
Jim Laskeyef42a012006-11-02 20:12:39 +00003229 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003230 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003231
Jim Laskeyef42a012006-11-02 20:12:39 +00003232 // Assumes in correct section after the entry point.
3233 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00003234
3235 // Emit label for the implicitly defined dbg.stoppoint at the start of
3236 // the function.
Devang Patelf3ee5142009-01-12 22:54:42 +00003237 if (!Lines.empty()) {
3238 const SrcLineInfo &LineInfo = Lines[0];
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00003239 Asm->printLabel(LineInfo.getLabelID());
3240 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003241
3242 if (TimePassesIsEnabled)
3243 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003244 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003245
Jim Laskey65195462006-10-30 13:35:07 +00003246 /// EndFunction - Gather and emit post-function debug information.
3247 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00003248 void EndFunction(MachineFunction *MF) {
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003249 if (!ShouldEmitDwarfDebug()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003250
Bill Wendling163ba3f2009-03-10 21:23:25 +00003251 if (TimePassesIsEnabled)
3252 DebugTimer->startTimer();
3253
Jim Laskeyb82313f2007-02-01 16:31:34 +00003254 // Define end label for subprogram.
3255 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003256
Jim Laskeyef42a012006-11-02 20:12:39 +00003257 // Get function line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00003258 if (!Lines.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003259 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003260 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00003261 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patelf3ee5142009-01-12 22:54:42 +00003262 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Jim Laskeyef42a012006-11-02 20:12:39 +00003263 // Append the function info to section info.
3264 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patelf3ee5142009-01-12 22:54:42 +00003265 Lines.begin(), Lines.end());
Jim Laskeyef42a012006-11-02 20:12:39 +00003266 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003267
Jim Laskeyef42a012006-11-02 20:12:39 +00003268 // Construct scopes for subprogram.
Devang Patel7bb89ed2009-01-13 00:20:51 +00003269 if (RootDbgScope)
3270 ConstructRootDbgScope(RootDbgScope);
Bill Wendlingd751c642008-09-26 00:28:12 +00003271 else
3272 // FIXME: This is wrong. We are essentially getting past a problem with
3273 // debug information not being able to handle unreachable blocks that have
3274 // debug information in them. In particular, those unreachable blocks that
3275 // have "region end" info in them. That situation results in the "root
3276 // scope" not being created. If that's the case, then emit a "default"
3277 // scope, i.e., one that encompasses the whole function. This isn't
3278 // desirable. And a better way of handling this (and all of the debugging
3279 // information) needs to be explored.
Devang Patel7bb89ed2009-01-13 00:20:51 +00003280 ConstructDefaultDbgScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003281
3282 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3283 MMI->getFrameMoves()));
Devang Patel481ff5b2009-01-12 18:48:36 +00003284
3285 // Clear debug info
3286 if (RootDbgScope) {
3287 delete RootDbgScope;
3288 DbgScopeMap.clear();
3289 RootDbgScope = NULL;
3290 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003291
Bill Wendling163ba3f2009-03-10 21:23:25 +00003292 Lines.clear();
3293
3294 if (TimePassesIsEnabled)
3295 DebugTimer->stopTimer();
3296 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003297
Devang Patelcf3a4482009-01-15 23:41:32 +00003298 /// ValidDebugInfo - Return true if V represents valid debug info value.
Devang Patel48c7fa22009-04-13 18:13:16 +00003299 bool ValidDebugInfo(Value *V, bool FastISel) {
Devang Patelb79b5352009-01-19 23:21:49 +00003300 if (!V)
3301 return false;
3302
Devang Patel61c6bf32009-01-16 01:49:46 +00003303 if (!shouldEmit)
3304 return false;
3305
Devang Patelcf3a4482009-01-15 23:41:32 +00003306 GlobalVariable *GV = getGlobalVariable(V);
3307 if (!GV)
3308 return false;
Duncan Sands667d4b82009-03-07 15:45:40 +00003309
3310 if (!GV->hasInternalLinkage () && !GV->hasLinkOnceLinkage())
Devang Patelcf3a4482009-01-15 23:41:32 +00003311 return false;
3312
Bill Wendling163ba3f2009-03-10 21:23:25 +00003313 if (TimePassesIsEnabled)
3314 DebugTimer->startTimer();
3315
Devang Patelcf3a4482009-01-15 23:41:32 +00003316 DIDescriptor DI(GV);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003317
Devang Patelcf3a4482009-01-15 23:41:32 +00003318 // Check current version. Allow Version6 for now.
3319 unsigned Version = DI.getVersion();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003320 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6) {
3321 if (TimePassesIsEnabled)
3322 DebugTimer->stopTimer();
3323
Devang Patelcf3a4482009-01-15 23:41:32 +00003324 return false;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003325 }
Devang Patelcf3a4482009-01-15 23:41:32 +00003326
Devang Patelb79b5352009-01-19 23:21:49 +00003327 unsigned Tag = DI.getTag();
3328 switch (Tag) {
3329 case DW_TAG_variable:
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003330 assert(DIVariable(GV).Verify() && "Invalid DebugInfo value");
Devang Patelb79b5352009-01-19 23:21:49 +00003331 break;
3332 case DW_TAG_compile_unit:
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003333 assert(DICompileUnit(GV).Verify() && "Invalid DebugInfo value");
Devang Patelb79b5352009-01-19 23:21:49 +00003334 break;
3335 case DW_TAG_subprogram:
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003336 assert(DISubprogram(GV).Verify() && "Invalid DebugInfo value");
Devang Patelb79b5352009-01-19 23:21:49 +00003337 break;
Devang Patel48c7fa22009-04-13 18:13:16 +00003338 case DW_TAG_lexical_block:
3339 /// FIXME. This interfers with the qualitfy of generated code when
3340 /// during optimization.
3341 if (FastISel == false)
3342 return false;
Devang Patelb79b5352009-01-19 23:21:49 +00003343 default:
3344 break;
3345 }
3346
Bill Wendling163ba3f2009-03-10 21:23:25 +00003347 if (TimePassesIsEnabled)
3348 DebugTimer->stopTimer();
3349
Devang Patelcf3a4482009-01-15 23:41:32 +00003350 return true;
3351 }
3352
Devang Patelccca7fe2009-01-12 19:17:34 +00003353 /// RecordSourceLine - Records location information and associates it with a
3354 /// label. Returns a unique label ID used to generate a label and provide
3355 /// correspondence to the source line list.
3356 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003357 if (TimePassesIsEnabled)
3358 DebugTimer->startTimer();
3359
Evan Chenge3d42322009-02-25 07:04:34 +00003360 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003361 assert(Unit && "Unable to find CompileUnit");
Devang Patelccca7fe2009-01-12 19:17:34 +00003362 unsigned ID = MMI->NextLabelID();
3363 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003364
3365 if (TimePassesIsEnabled)
3366 DebugTimer->stopTimer();
3367
Devang Patelccca7fe2009-01-12 19:17:34 +00003368 return ID;
3369 }
3370
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(unsigned Line, unsigned Col, unsigned Src) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003375 if (TimePassesIsEnabled)
3376 DebugTimer->startTimer();
3377
Devang Patelccca7fe2009-01-12 19:17:34 +00003378 unsigned ID = MMI->NextLabelID();
3379 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003380
3381 if (TimePassesIsEnabled)
3382 DebugTimer->stopTimer();
3383
Devang Patelccca7fe2009-01-12 19:17:34 +00003384 return ID;
3385 }
3386
Bill Wendlingc8615e42009-03-10 21:47:45 +00003387 /// getRecordSourceLineCount - Return the number of source lines in the debug
3388 /// info.
3389 unsigned getRecordSourceLineCount() const {
Devang Patelccca7fe2009-01-12 19:17:34 +00003390 return Lines.size();
3391 }
3392
Bill Wendlingc8615e42009-03-10 21:47:45 +00003393 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
3394 /// timed. Look up the source id with the given directory and source file
3395 /// names. If none currently exists, create a new id and insert it in the
3396 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
3397 /// well.
Evan Chenge3d42322009-02-25 07:04:34 +00003398 unsigned getOrCreateSourceID(const std::string &DirName,
3399 const std::string &FileName) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003400 if (TimePassesIsEnabled)
3401 DebugTimer->startTimer();
3402
Bill Wendlingc8615e42009-03-10 21:47:45 +00003403 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003404
3405 if (TimePassesIsEnabled)
3406 DebugTimer->stopTimer();
3407
Evan Chenge3d42322009-02-25 07:04:34 +00003408 return SrcId;
Devang Patelccca7fe2009-01-12 19:17:34 +00003409 }
3410
3411 /// RecordRegionStart - Indicate the start of a region.
Devang Patelccca7fe2009-01-12 19:17:34 +00003412 unsigned RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003413 if (TimePassesIsEnabled)
3414 DebugTimer->startTimer();
3415
Devang Patelccca7fe2009-01-12 19:17:34 +00003416 DbgScope *Scope = getOrCreateScope(V);
3417 unsigned ID = MMI->NextLabelID();
3418 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003419
3420 if (TimePassesIsEnabled)
3421 DebugTimer->stopTimer();
3422
Devang Patelccca7fe2009-01-12 19:17:34 +00003423 return ID;
3424 }
3425
Devang Patel0f7fef32009-04-13 17:02:03 +00003426 /// RecordRegionStart - Indicate the start of a region.
3427 unsigned RecordRegionStart(GlobalVariable *V, unsigned ID) {
3428 if (TimePassesIsEnabled)
3429 DebugTimer->startTimer();
3430
3431 DbgScope *Scope = getOrCreateScope(V);
3432 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
3433
3434 if (TimePassesIsEnabled)
3435 DebugTimer->stopTimer();
3436
3437 return ID;
3438 }
3439
Devang Patelccca7fe2009-01-12 19:17:34 +00003440 /// RecordRegionEnd - Indicate the end of a region.
Devang Patelccca7fe2009-01-12 19:17:34 +00003441 unsigned RecordRegionEnd(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003442 if (TimePassesIsEnabled)
3443 DebugTimer->startTimer();
3444
Devang Patelccca7fe2009-01-12 19:17:34 +00003445 DbgScope *Scope = getOrCreateScope(V);
3446 unsigned ID = MMI->NextLabelID();
3447 Scope->setEndLabelID(ID);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003448
3449 if (TimePassesIsEnabled)
3450 DebugTimer->stopTimer();
3451
Devang Patelccca7fe2009-01-12 19:17:34 +00003452 return ID;
3453 }
3454
3455 /// RecordVariable - Indicate the declaration of a local variable.
Devang Patelccca7fe2009-01-12 19:17:34 +00003456 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003457 if (TimePassesIsEnabled)
3458 DebugTimer->startTimer();
3459
Devang Patel0e5200f2009-01-15 18:25:17 +00003460 DIDescriptor Desc(GV);
3461 DbgScope *Scope = NULL;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003462
Devang Patel0e5200f2009-01-15 18:25:17 +00003463 if (Desc.getTag() == DW_TAG_variable) {
3464 // GV is a global variable.
3465 DIGlobalVariable DG(GV);
3466 Scope = getOrCreateScope(DG.getContext().getGV());
3467 } else {
3468 // or GV is a local variable.
3469 DIVariable DV(GV);
3470 Scope = getOrCreateScope(DV.getContext().getGV());
3471 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003472
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003473 assert(Scope && "Unable to find variable' scope");
Devang Patel99ec3532009-01-16 19:28:14 +00003474 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelccca7fe2009-01-12 19:17:34 +00003475 Scope->AddVariable(DV);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003476
3477 if (TimePassesIsEnabled)
3478 DebugTimer->stopTimer();
Devang Patelccca7fe2009-01-12 19:17:34 +00003479 }
Devang Patel0f7fef32009-04-13 17:02:03 +00003480
3481 //// RecordInlineInfo - Global variable GV is inlined at the location marked
3482 //// by LabelID label.
3483 void RecordInlineInfo(GlobalVariable *GV, unsigned LabelID) {
3484 MMI->RecordUsedDbgLabel(LabelID);
3485 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
3486 I = InlineInfo.find(GV);
3487 if (I == InlineInfo.end()) {
3488 SmallVector<unsigned, 4> Labels;
3489 Labels.push_back(LabelID);
3490 InlineInfo[GV] = Labels;
3491 return;
3492 }
3493
3494 SmallVector<unsigned, 4> &Labels = I->second;
3495 Labels.push_back(LabelID);
3496 }
Jim Laskey65195462006-10-30 13:35:07 +00003497};
3498
Jim Laskey072200c2007-01-29 18:51:14 +00003499//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003500/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00003501///
3502class DwarfException : public Dwarf {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003503 struct FunctionEHFrameInfo {
3504 std::string FnName;
3505 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003506 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003507 bool hasCalls;
3508 bool hasLandingPads;
3509 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003510 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00003511
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003512 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3513 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003514 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003515 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003516 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003517 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003518 };
3519
3520 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003521
3522 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3523 /// be emitted.
3524 bool shouldEmitTable;
3525
3526 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3527 /// should be emitted.
3528 bool shouldEmitMoves;
3529
3530 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3531 /// should be emitted.
3532 bool shouldEmitTableModule;
3533
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003534 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003535 /// should be emitted.
3536 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00003537
Bill Wendling163ba3f2009-03-10 21:23:25 +00003538 /// ExceptionTimer - Timer for the Dwarf exception writer.
3539 Timer *ExceptionTimer;
3540
Jim Laskey3f09fc22007-02-28 18:38:31 +00003541 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3542 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003543 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003544 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003545 int stackGrowth =
3546 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3547 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003548 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003549
Jim Laskeybacd3042007-02-21 22:48:45 +00003550 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003551 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling722f5f12008-12-24 08:05:17 +00003552
3553 if (!TAI->doesRequireNonLocalEHFrameLabel())
3554 O << TAI->getEHGlobalPrefix();
3555 O << "EH_frame" << Index << ":\n";
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003556 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003557
Jim Laskeybacd3042007-02-21 22:48:45 +00003558 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003559 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003560
Jim Laskeybacd3042007-02-21 22:48:45 +00003561 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003562 EmitDifference("eh_frame_common_end", Index,
3563 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003564 Asm->EOL("Length of Common Information Entry");
3565
Jim Laskeybacd3042007-02-21 22:48:45 +00003566 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003567 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003568 Asm->EmitInt32((int)0);
3569 Asm->EOL("CIE Identifier Tag");
3570 Asm->EmitInt8(DW_CIE_VERSION);
3571 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00003572
Jim Laskeybacd3042007-02-21 22:48:45 +00003573 // The personality presence indicates that language specific information
3574 // will show up in the eh frame.
3575 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00003576 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00003577
Jim Laskeybacd3042007-02-21 22:48:45 +00003578 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003579 Asm->EmitULEB128Bytes(1);
3580 Asm->EOL("CIE Code Alignment Factor");
3581 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00003582 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003583 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00003584 Asm->EOL("CIE Return Address Column");
3585
Jim Laskeybacd3042007-02-21 22:48:45 +00003586 // If there is a personality, we need to indicate the functions location.
3587 if (Personality) {
3588 Asm->EmitULEB128Bytes(7);
3589 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00003590
Duncan Sands671fa972008-05-07 19:11:09 +00003591 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003592 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00003593 Asm->EOL("Personality (pcrel sdata4 indirect)");
3594 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003595 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00003596 Asm->EOL("Personality (pcrel sdata4)");
3597 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00003598
Duncan Sands671fa972008-05-07 19:11:09 +00003599 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00003600 O << TAI->getPersonalityPrefix();
3601 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3602 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00003603 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3604 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00003605 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00003606
Duncan Sands671fa972008-05-07 19:11:09 +00003607 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3608 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003609
Bill Wendlingd60de512009-01-05 22:53:45 +00003610 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3611 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003612 } else {
3613 Asm->EmitULEB128Bytes(1);
3614 Asm->EOL("Augmentation Size");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003615
Bill Wendlingd60de512009-01-05 22:53:45 +00003616 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3617 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003618 }
3619
3620 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003621 std::vector<MachineMove> Moves;
3622 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00003623 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003624
Dale Johannesen21d972a2008-04-30 00:43:29 +00003625 // On Darwin the linker honors the alignment of eh_frame, which means it
3626 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3627 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003628 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003629 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003630 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003631
Jim Laskeybacd3042007-02-21 22:48:45 +00003632 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003633 }
Duncan Sands671fa972008-05-07 19:11:09 +00003634
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003635 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003636 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003637 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003638 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
Chris Lattner266c7bb2009-04-13 05:44:34 +00003639
3640 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
3641 "Should not emit 'available externally' functions at all");
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003642
Jim Laskeybacd3042007-02-21 22:48:45 +00003643 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3644
3645 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003646 // If the corresponding function is static, this should not be
3647 // externally visible.
Rafael Espindolabb46f522009-01-15 20:18:42 +00003648 if (linkage != Function::InternalLinkage &&
Devang Patel2d1768c2009-01-17 08:05:14 +00003649 linkage != Function::PrivateLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003650 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3651 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3652 }
3653
Dale Johannesen038129d2008-01-10 02:03:30 +00003654 // If corresponding function is weak definition, this should be too.
Duncan Sands667d4b82009-03-07 15:45:40 +00003655 if ((linkage == Function::WeakAnyLinkage ||
3656 linkage == Function::WeakODRLinkage ||
3657 linkage == Function::LinkOnceAnyLinkage ||
3658 linkage == Function::LinkOnceODRLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00003659 TAI->getWeakDefDirective())
3660 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3661
3662 // If there are no calls then you can't unwind. This may mean we can
3663 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003664 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00003665 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00003666 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00003667 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00003668 !UnwindTablesMandatory &&
Duncan Sands667d4b82009-03-07 15:45:40 +00003669 ((linkage != Function::WeakAnyLinkage &&
3670 linkage != Function::WeakODRLinkage &&
3671 linkage != Function::LinkOnceAnyLinkage &&
3672 linkage != Function::LinkOnceODRLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00003673 !TAI->getWeakDefDirective() ||
3674 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003675 {
Bill Wendling6e198962007-09-18 01:47:22 +00003676 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003677 // This name has no connection to the function, so it might get
3678 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003679 // dead-stripping unconditionally.
3680 if (const char *UsedDirective = TAI->getUsedDirective())
3681 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00003682 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00003683 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003684
Jim Laskeybacd3042007-02-21 22:48:45 +00003685 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003686 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3687 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003688 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003689
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003690 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003691
Bill Wendling722f5f12008-12-24 08:05:17 +00003692 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3693 PrintRelDirective(true, true);
3694 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3695
3696 if (!TAI->isAbsoluteEHSectionOffsets())
3697 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3698 } else {
3699 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3700 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3701 true, true, false);
3702 }
3703
Jim Laskeybacd3042007-02-21 22:48:45 +00003704 Asm->EOL("FDE CIE offset");
3705
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003706 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003707 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003708 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003709 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003710 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00003711
Jim Laskey3f09fc22007-02-28 18:38:31 +00003712 // If there is a personality and landing pads then point to the language
3713 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003714 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00003715 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00003716 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00003717
3718 if (EHFrameInfo.hasLandingPads)
3719 EmitReference("exception", EHFrameInfo.Number, true, true);
3720 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00003721 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00003722 Asm->EOL("Language Specific Data Area");
3723 } else {
3724 Asm->EmitULEB128Bytes(0);
3725 Asm->EOL("Augmentation size");
3726 }
Duncan Sands671fa972008-05-07 19:11:09 +00003727
Jim Laskeybacd3042007-02-21 22:48:45 +00003728 // Indicate locations of function specific callee saved registers in
3729 // frame.
Devang Patel5aac3d32009-01-17 08:01:33 +00003730 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00003731 true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003732
Dale Johannesen21d972a2008-04-30 00:43:29 +00003733 // On Darwin the linker honors the alignment of eh_frame, which means it
3734 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3735 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003736 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003737 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003738 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003739
3740 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003741 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003742 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003743 // that depends on unused functions (calling undefined externals) being
3744 // dead-stripped to link correctly. Yes, there really is.
3745 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3746 if (const char *UsedDirective = TAI->getUsedDirective())
3747 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3748 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003749 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003750
Duncan Sands57810cd2007-09-05 11:27:52 +00003751 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003752 ///
3753 /// The general organization of the table is complex, but the basic concepts
3754 /// are easy. First there is a header which describes the location and
3755 /// organization of the three components that follow.
3756 /// 1. The landing pad site information describes the range of code covered
3757 /// by the try. In our case it's an accumulation of the ranges covered
3758 /// by the invokes in the try. There is also a reference to the landing
3759 /// pad that handles the exception once processed. Finally an index into
3760 /// the actions table.
3761 /// 2. The action table, in our case, is composed of pairs of type ids
3762 /// and next action offset. Starting with the action index from the
3763 /// landing pad site, each type Id is checked for a match to the current
3764 /// exception. If it matches then the exception and type id are passed
3765 /// on to the landing pad. Otherwise the next action is looked up. This
3766 /// chain is terminated with a next action of zero. If no type id is
3767 /// found the the frame is unwound and handling continues.
3768 /// 3. Type id table contains references to all the C++ typeinfo for all
3769 /// catches in the function. This tables is reversed indexed base 1.
3770
Duncan Sandsb32edb42007-06-06 15:37:31 +00003771 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3772 static unsigned SharedTypeIds(const LandingPadInfo *L,
3773 const LandingPadInfo *R) {
3774 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003775 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003776 unsigned MinSize = LSize < RSize ? LSize : RSize;
3777 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003778
Duncan Sandsb32edb42007-06-06 15:37:31 +00003779 for (; Count != MinSize; ++Count)
3780 if (LIds[Count] != RIds[Count])
3781 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003782
Duncan Sandsb32edb42007-06-06 15:37:31 +00003783 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003784 }
3785
Duncan Sandsb32edb42007-06-06 15:37:31 +00003786 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00003787 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003788 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003789 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003790 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003791
Duncan Sandsb32edb42007-06-06 15:37:31 +00003792 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00003793 if (LIds[i] != RIds[i])
3794 return LIds[i] < RIds[i];
3795
Duncan Sandsb32edb42007-06-06 15:37:31 +00003796 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003797 }
3798
Duncan Sands53c3a332007-05-16 12:12:23 +00003799 struct KeyInfo {
3800 static inline unsigned getEmptyKey() { return -1U; }
3801 static inline unsigned getTombstoneKey() { return -2U; }
3802 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00003803 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00003804 static bool isPod() { return true; }
3805 };
3806
Duncan Sands57810cd2007-09-05 11:27:52 +00003807 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003808 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003809 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003810 int NextAction;
3811 struct ActionEntry *Previous;
3812 };
3813
Duncan Sands57810cd2007-09-05 11:27:52 +00003814 /// PadRange - Structure holding a try-range and the associated landing pad.
3815 struct PadRange {
3816 // The index of the landing pad.
3817 unsigned PadIndex;
3818 // The index of the begin and end labels in the landing pad's label lists.
3819 unsigned RangeIndex;
3820 };
3821
3822 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3823
3824 /// CallSiteEntry - Structure describing an entry in the call-site table.
3825 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00003826 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003827 unsigned BeginLabel; // zero indicates the start of the function.
3828 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00003829 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003830 unsigned PadLabel; // zero indicates that there is no landing pad.
3831 unsigned Action;
3832 };
3833
Jim Laskeybacd3042007-02-21 22:48:45 +00003834 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00003835 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00003836 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00003837 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3838 if (PadInfos.empty()) return;
3839
3840 // Sort the landing pads in order of their type ids. This is used to fold
3841 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00003842 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00003843 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00003844 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00003845 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003846 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3847
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003848 // Negative type ids index into FilterIds, positive type ids index into
3849 // TypeInfos. The value written for a positive type id is just the type
3850 // id itself. For a negative type id, however, the value written is the
3851 // (negative) byte offset of the corresponding FilterIds entry. The byte
3852 // offset is usually equal to the type id, because the FilterIds entries
3853 // are written using a variable width encoding which outputs one byte per
3854 // entry as long as the value written is not too large, but can differ.
3855 // This kind of complication does not occur for positive type ids because
3856 // type infos are output using a fixed width encoding.
3857 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3858 SmallVector<int, 16> FilterOffsets;
3859 FilterOffsets.reserve(FilterIds.size());
3860 int Offset = -1;
3861 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3862 E = FilterIds.end(); I != E; ++I) {
3863 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003864 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003865 }
3866
Duncan Sands57810cd2007-09-05 11:27:52 +00003867 // Compute the actions table and gather the first action index for each
3868 // landing pad site.
3869 SmallVector<ActionEntry, 32> Actions;
3870 SmallVector<unsigned, 64> FirstActions;
3871 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00003872
Duncan Sandsb32edb42007-06-06 15:37:31 +00003873 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00003874 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003875 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003876 const LandingPadInfo *LP = LandingPads[i];
3877 const std::vector<int> &TypeIds = LP->TypeIds;
3878 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003879 unsigned SizeSiteActions = 0;
3880
Duncan Sandsb32edb42007-06-06 15:37:31 +00003881 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00003882 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00003883 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003884
Duncan Sandsb32edb42007-06-06 15:37:31 +00003885 if (NumShared) {
3886 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3887 assert(Actions.size());
3888 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003889 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3890 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003891 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003892 SizeAction -=
3893 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003894 SizeAction += -PrevAction->NextAction;
3895 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003896 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00003897 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003898
Duncan Sandsb32edb42007-06-06 15:37:31 +00003899 // Compute the actions.
3900 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3901 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003902 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3903 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003904 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003905
3906 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003907 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003908 SizeSiteActions += SizeAction;
3909
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003910 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00003911 Actions.push_back(Action);
3912
3913 PrevAction = &Actions.back();
3914 }
3915
3916 // Record the first action of the landing pad site.
3917 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3918 } // else identical - re-use previous FirstAction
3919
3920 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003921
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00003922 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00003923 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00003924 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003925
Duncan Sands481dc722007-12-19 07:36:31 +00003926 // Compute the call-site table. The entry for an invoke has a try-range
3927 // containing the call, a non-zero landing pad and an appropriate action.
3928 // The entry for an ordinary call has a try-range containing the call and
3929 // zero for the landing pad and the action. Calls marked 'nounwind' have
3930 // no entry and must not be contained in the try-range of any entry - they
3931 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003932 SmallVector<CallSiteEntry, 64> CallSites;
3933
3934 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00003935 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3936 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3937 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00003938 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3939 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00003940 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003941 unsigned BeginLabel = LandingPad->BeginLabels[j];
3942 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3943 PadRange P = { i, j };
3944 PadMap[BeginLabel] = P;
3945 }
3946 }
3947
Duncan Sands481dc722007-12-19 07:36:31 +00003948 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00003949 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00003950
3951 // Whether there is a potentially throwing instruction (currently this means
3952 // an ordinary call) between the end of the previous try-range and now.
3953 bool SawPotentiallyThrowing = false;
3954
3955 // Whether the last callsite entry was for an invoke.
3956 bool PreviousIsInvoke = false;
3957
Duncan Sands481dc722007-12-19 07:36:31 +00003958 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003959 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3960 I != E; ++I) {
3961 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3962 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00003963 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00003964 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00003965 continue;
3966 }
3967
Chris Lattner9e330492007-12-30 20:50:28 +00003968 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00003969 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00003970
Duncan Sands481dc722007-12-19 07:36:31 +00003971 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00003972 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00003973 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003974
Duncan Sands481dc722007-12-19 07:36:31 +00003975 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00003976 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00003977 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00003978 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00003979 continue;
3980
3981 PadRange P = L->second;
3982 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3983
3984 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3985 "Inconsistent landing pad map!");
3986
3987 // If some instruction between the previous try-range and this one may
3988 // throw, create a call-site entry with no landing pad for the region
3989 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00003990 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003991 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3992 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00003993 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003994 }
3995
3996 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00003997 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00003998
Duncan Sands481dc722007-12-19 07:36:31 +00003999 if (LandingPad->LandingPadLabel) {
4000 // This try-range is for an invoke.
4001 CallSiteEntry Site = {BeginLabel, LastLabel,
4002 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00004003
Duncan Sands481dc722007-12-19 07:36:31 +00004004 // Try to merge with the previous call-site.
4005 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00004006 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00004007 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
4008 // Extend the range of the previous entry.
4009 Prev.EndLabel = Site.EndLabel;
4010 continue;
4011 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004012 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004013
Duncan Sands481dc722007-12-19 07:36:31 +00004014 // Otherwise, create a new call-site.
4015 CallSites.push_back(Site);
4016 PreviousIsInvoke = true;
4017 } else {
4018 // Create a gap.
4019 PreviousIsInvoke = false;
4020 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004021 }
4022 }
4023 // If some instruction between the previous try-range and the end of the
4024 // function may throw, create a call-site entry with no landing pad for the
4025 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00004026 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004027 CallSiteEntry Site = {LastLabel, 0, 0, 0};
4028 CallSites.push_back(Site);
4029 }
4030
Jim Laskeybacd3042007-02-21 22:48:45 +00004031 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00004032
4033 // Call sites.
4034 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
4035 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
4036 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
4037 unsigned SizeSites = CallSites.size() * (SiteStartSize +
4038 SiteLengthSize +
4039 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00004040 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004041 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00004042
Duncan Sands671fa972008-05-07 19:11:09 +00004043 // Type infos.
4044 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
4045 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004046
4047 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004048 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004049 SizeSites + SizeActions + SizeTypes;
4050
4051 unsigned TotalSize = sizeof(int8_t) + // LPStart format
4052 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004053 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004054 TypeOffset;
4055
4056 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004057
Jim Laskeybacd3042007-02-21 22:48:45 +00004058 // Begin the exception table.
4059 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng05548eb2008-02-29 19:36:59 +00004060 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesend65b2642008-10-08 21:50:21 +00004061 O << "GCC_except_table" << SubprogramCount << ":\n";
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004062 for (unsigned i = 0; i != SizeAlign; ++i) {
4063 Asm->EmitInt8(0);
4064 Asm->EOL("Padding");
4065 }
Jim Laskeybacd3042007-02-21 22:48:45 +00004066 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004067
Jim Laskeybacd3042007-02-21 22:48:45 +00004068 // Emit the header.
4069 Asm->EmitInt8(DW_EH_PE_omit);
4070 Asm->EOL("LPStart format (DW_EH_PE_omit)");
4071 Asm->EmitInt8(DW_EH_PE_absptr);
4072 Asm->EOL("TType format (DW_EH_PE_absptr)");
4073 Asm->EmitULEB128Bytes(TypeOffset);
4074 Asm->EOL("TType base offset");
4075 Asm->EmitInt8(DW_EH_PE_udata4);
4076 Asm->EOL("Call site format (DW_EH_PE_udata4)");
4077 Asm->EmitULEB128Bytes(SizeSites);
4078 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004079
Duncan Sands57810cd2007-09-05 11:27:52 +00004080 // Emit the landing pad site information.
4081 for (unsigned i = 0; i < CallSites.size(); ++i) {
4082 CallSiteEntry &S = CallSites[i];
4083 const char *BeginTag;
4084 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00004085
Duncan Sands57810cd2007-09-05 11:27:52 +00004086 if (!S.BeginLabel) {
4087 BeginTag = "eh_func_begin";
4088 BeginNumber = SubprogramCount;
4089 } else {
4090 BeginTag = "label";
4091 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00004092 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004093
Duncan Sands57810cd2007-09-05 11:27:52 +00004094 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004095 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004096 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00004097
Duncan Sands57810cd2007-09-05 11:27:52 +00004098 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00004099 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00004100 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004101 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00004102 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004103 }
4104 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004105
Duncan Sands671fa972008-05-07 19:11:09 +00004106 if (!S.PadLabel)
4107 Asm->EmitInt32(0);
4108 else
Duncan Sands57810cd2007-09-05 11:27:52 +00004109 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004110 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004111 Asm->EOL("Landing pad");
4112
4113 Asm->EmitULEB128Bytes(S.Action);
4114 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004115 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004116
Jim Laskeybacd3042007-02-21 22:48:45 +00004117 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004118 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4119 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00004120
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004121 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004122 Asm->EOL("TypeInfo index");
4123 Asm->EmitSLEB128Bytes(Action.NextAction);
4124 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004125 }
4126
4127 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00004128 for (unsigned M = TypeInfos.size(); M; --M) {
4129 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00004130
4131 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00004132
Bill Wendling7d16e852009-04-10 00:12:49 +00004133 if (GV) {
4134 std::string GLN;
4135 O << Asm->getGlobalLinkName(GV, GLN);
4136 } else {
Jim Laskeybacd3042007-02-21 22:48:45 +00004137 O << "0";
Bill Wendling7d16e852009-04-10 00:12:49 +00004138 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004139
Jim Laskeybacd3042007-02-21 22:48:45 +00004140 Asm->EOL("TypeInfo");
4141 }
4142
Duncan Sands73ef58a2007-06-02 16:53:42 +00004143 // Emit the filter typeids.
4144 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4145 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00004146 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00004147 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00004148 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004149
Evan Cheng05548eb2008-02-29 19:36:59 +00004150 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004151 }
4152
Jim Laskey072200c2007-01-29 18:51:14 +00004153public:
4154 //===--------------------------------------------------------------------===//
4155 // Main entry points.
4156 //
Owen Andersoncb371882008-08-21 00:14:44 +00004157 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00004158 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
4159 shouldEmitTableModule(false), shouldEmitMovesModule(false),
4160 ExceptionTimer(0) {
4161 if (TimePassesIsEnabled)
4162 ExceptionTimer = new Timer("Dwarf Exception Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00004163 getDwarfTimerGroup());
Bill Wendling163ba3f2009-03-10 21:23:25 +00004164 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004165
Bill Wendling163ba3f2009-03-10 21:23:25 +00004166 virtual ~DwarfException() {
4167 delete ExceptionTimer;
4168 }
Jim Laskey072200c2007-01-29 18:51:14 +00004169
4170 /// SetModuleInfo - Set machine module information when it's known that pass
4171 /// manager has created it. Set by the target AsmPrinter.
4172 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00004173 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00004174 }
4175
4176 /// BeginModule - Emit all exception information that should come prior to the
4177 /// content.
4178 void BeginModule(Module *M) {
4179 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00004180 }
4181
4182 /// EndModule - Emit all exception information that should come after the
4183 /// content.
4184 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004185 if (TimePassesIsEnabled)
4186 ExceptionTimer->startTimer();
4187
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004188 if (shouldEmitMovesModule || shouldEmitTableModule) {
4189 const std::vector<Function *> Personalities = MMI->getPersonalities();
Evan Chenge3d42322009-02-25 07:04:34 +00004190 for (unsigned i = 0; i < Personalities.size(); ++i)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004191 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00004192
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004193 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4194 E = EHFrames.end(); I != E; ++I)
4195 EmitEHFrame(*I);
4196 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004197
4198 if (TimePassesIsEnabled)
4199 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004200 }
4201
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004202 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00004203 /// emitted immediately after the function entry point.
4204 void BeginFunction(MachineFunction *MF) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004205 if (TimePassesIsEnabled)
4206 ExceptionTimer->startTimer();
4207
Jim Laskey072200c2007-01-29 18:51:14 +00004208 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004209 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004210
Bill Wendling163ba3f2009-03-10 21:23:25 +00004211 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004212 // Map all labels and get rid of any dead landing pads.
4213 MMI->TidyLandingPads();
Bill Wendling163ba3f2009-03-10 21:23:25 +00004214
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004215 // If any landing pads survive, we need an EH table.
4216 if (MMI->getLandingPads().size())
4217 shouldEmitTable = true;
4218
4219 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00004220 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004221 shouldEmitMoves = true;
4222
4223 if (shouldEmitMoves || shouldEmitTable)
4224 // Assumes in correct section after the entry point.
4225 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00004226 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004227
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004228 shouldEmitTableModule |= shouldEmitTable;
4229 shouldEmitMovesModule |= shouldEmitMoves;
Bill Wendling163ba3f2009-03-10 21:23:25 +00004230
4231 if (TimePassesIsEnabled)
4232 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004233 }
4234
4235 /// EndFunction - Gather and emit post-function exception information.
4236 ///
4237 void EndFunction() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004238 if (TimePassesIsEnabled)
4239 ExceptionTimer->startTimer();
4240
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004241 if (shouldEmitMoves || shouldEmitTable) {
4242 EmitLabel("eh_func_end", SubprogramCount);
4243 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00004244
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004245 // Save EH frame information
Bill Wendling7d16e852009-04-10 00:12:49 +00004246 std::string Name;
4247 EHFrames.push_back(
4248 FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF, Name),
4249 SubprogramCount,
4250 MMI->getPersonalityIndex(),
4251 MF->getFrameInfo()->hasCalls(),
4252 !MMI->getLandingPads().empty(),
4253 MMI->getFrameMoves(),
4254 MF->getFunction()));
Bill Wendling163ba3f2009-03-10 21:23:25 +00004255 }
4256
4257 if (TimePassesIsEnabled)
4258 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004259 }
4260};
4261
Jim Laskey0d086af2006-02-27 12:43:29 +00004262} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00004263
4264//===----------------------------------------------------------------------===//
4265
Jim Laskeyd18e2892006-01-20 20:34:06 +00004266/// Emit - Print the abbreviation using the specified Dwarf writer.
4267///
Jim Laskey072200c2007-01-29 18:51:14 +00004268void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004269 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00004270 DD.getAsm()->EmitULEB128Bytes(Tag);
4271 DD.getAsm()->EOL(TagString(Tag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004272
Jim Laskeyd18e2892006-01-20 20:34:06 +00004273 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00004274 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4275 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004276
Jim Laskeyd18e2892006-01-20 20:34:06 +00004277 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00004278 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004279 const DIEAbbrevData &AttrData = Data[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004280
Jim Laskeyd18e2892006-01-20 20:34:06 +00004281 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00004282 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4283 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004284
Jim Laskeyd18e2892006-01-20 20:34:06 +00004285 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00004286 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4287 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00004288 }
4289
4290 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00004291 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4292 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00004293}
4294
4295#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00004296void DIEAbbrev::print(std::ostream &O) {
4297 O << "Abbreviation @"
4298 << std::hex << (intptr_t)this << std::dec
4299 << " "
4300 << TagString(Tag)
4301 << " "
4302 << ChildrenString(ChildrenFlag)
4303 << "\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004304
Jim Laskeya0f3d172006-09-07 22:06:40 +00004305 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4306 O << " "
4307 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004308 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00004309 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004310 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00004311 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00004312}
Bill Wendlinge8156192006-12-07 01:30:32 +00004313void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00004314#endif
4315
4316//===----------------------------------------------------------------------===//
4317
Jim Laskeyef42a012006-11-02 20:12:39 +00004318#ifndef NDEBUG
4319void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004320 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004321}
Jim Laskeyef42a012006-11-02 20:12:39 +00004322#endif
4323
4324//===----------------------------------------------------------------------===//
4325
Jim Laskey063e7652006-01-17 17:31:53 +00004326/// EmitValue - Emit integer of appropriate size.
4327///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004328void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00004329 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00004330 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00004331 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004332 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004333 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004334 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004335 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004336 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004337 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004338 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4339 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4340 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004341 default: assert(0 && "DIE Value form not supported yet"); break;
4342 }
4343}
4344
4345/// SizeOf - Determine size of integer value in bytes.
4346///
Jim Laskey072200c2007-01-29 18:51:14 +00004347unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004348 switch (Form) {
4349 case DW_FORM_flag: // Fall thru
4350 case DW_FORM_ref1: // Fall thru
4351 case DW_FORM_data1: return sizeof(int8_t);
4352 case DW_FORM_ref2: // Fall thru
4353 case DW_FORM_data2: return sizeof(int16_t);
4354 case DW_FORM_ref4: // Fall thru
4355 case DW_FORM_data4: return sizeof(int32_t);
4356 case DW_FORM_ref8: // Fall thru
4357 case DW_FORM_data8: return sizeof(int64_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004358 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4359 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00004360 default: assert(0 && "DIE Value form not supported yet"); break;
4361 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004362 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00004363}
4364
Jim Laskey063e7652006-01-17 17:31:53 +00004365//===----------------------------------------------------------------------===//
4366
4367/// EmitValue - Emit string value.
4368///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004369void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Bill Wendlingdd446322009-03-10 23:57:09 +00004370 DD.getAsm()->EmitString(Str);
Jim Laskey063e7652006-01-17 17:31:53 +00004371}
4372
Jim Laskey063e7652006-01-17 17:31:53 +00004373//===----------------------------------------------------------------------===//
4374
4375/// EmitValue - Emit label value.
4376///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004377void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004378 bool IsSmall = Form == DW_FORM_data4;
4379 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004380}
4381
4382/// SizeOf - Determine size of label value in bytes.
4383///
Jim Laskey072200c2007-01-29 18:51:14 +00004384unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004385 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004386 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004387}
Jim Laskeyef42a012006-11-02 20:12:39 +00004388
Jim Laskey063e7652006-01-17 17:31:53 +00004389//===----------------------------------------------------------------------===//
4390
Jim Laskeyd18e2892006-01-20 20:34:06 +00004391/// EmitValue - Emit label value.
4392///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004393void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004394 bool IsSmall = Form == DW_FORM_data4;
4395 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00004396}
4397
4398/// SizeOf - Determine size of label value in bytes.
4399///
Jim Laskey072200c2007-01-29 18:51:14 +00004400unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004401 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004402 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00004403}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004404
Jim Laskeyd18e2892006-01-20 20:34:06 +00004405//===----------------------------------------------------------------------===//
4406
Jim Laskey063e7652006-01-17 17:31:53 +00004407/// EmitValue - Emit delta value.
4408///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004409void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4410 bool IsSmall = Form == DW_FORM_data4;
4411 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4412 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4413}
4414
4415/// SizeOf - Determine size of delta value in bytes.
4416///
4417unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4418 if (Form == DW_FORM_data4) return 4;
4419 return DD.getTargetData()->getPointerSize();
4420}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004421
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004422//===----------------------------------------------------------------------===//
4423
4424/// EmitValue - Emit delta value.
4425///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004426void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004427 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00004428 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004429}
4430
4431/// SizeOf - Determine size of delta value in bytes.
4432///
Jim Laskey072200c2007-01-29 18:51:14 +00004433unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004434 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004435 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004436}
4437
4438//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004439
Jim Laskeyb8509c52006-03-23 18:07:55 +00004440/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004441///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004442void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004443 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00004444}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004445
Jim Laskeyd18e2892006-01-20 20:34:06 +00004446//===----------------------------------------------------------------------===//
4447
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004448/// ComputeSize - calculate the size of the block.
4449///
Jim Laskey072200c2007-01-29 18:51:14 +00004450unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00004451 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00004452 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004453
Jim Laskeyef42a012006-11-02 20:12:39 +00004454 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00004455 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00004456 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004457 }
4458 return Size;
4459}
4460
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004461/// EmitValue - Emit block data.
4462///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004463void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004464 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004465 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4466 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4467 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4468 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004469 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004470 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004471
Owen Anderson873e1b52008-06-24 21:44:59 +00004472 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00004473
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004474 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00004475 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00004476 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004477 }
4478}
4479
4480/// SizeOf - Determine size of block data in bytes.
4481///
Jim Laskey072200c2007-01-29 18:51:14 +00004482unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004483 switch (Form) {
4484 case DW_FORM_block1: return Size + sizeof(int8_t);
4485 case DW_FORM_block2: return Size + sizeof(int16_t);
4486 case DW_FORM_block4: return Size + sizeof(int32_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004487 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004488 default: assert(0 && "Improper form for block"); break;
4489 }
4490 return 0;
4491}
4492
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004493//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004494/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00004495
4496DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00004497 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00004498 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00004499}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004500
Jim Laskeyb8509c52006-03-23 18:07:55 +00004501/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4502///
4503void DIE::AddSiblingOffset() {
4504 DIEInteger *DI = new DIEInteger(0);
4505 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00004506 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00004507}
4508
Jim Laskeyef42a012006-11-02 20:12:39 +00004509/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004510///
Jim Laskeyef42a012006-11-02 20:12:39 +00004511void DIE::Profile(FoldingSetNodeID &ID) {
4512 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004513
Jim Laskeyef42a012006-11-02 20:12:39 +00004514 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4515 ID.AddPointer(Children[i]);
4516
4517 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4518 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00004519}
Jim Laskeyef42a012006-11-02 20:12:39 +00004520
4521#ifndef NDEBUG
4522void DIE::print(std::ostream &O, unsigned IncIndent) {
4523 static unsigned IndentCount = 0;
4524 IndentCount += IncIndent;
4525 const std::string Indent(IndentCount, ' ');
4526 bool isBlock = Abbrev.getTag() == 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004527
Jim Laskeyef42a012006-11-02 20:12:39 +00004528 if (!isBlock) {
4529 O << Indent
4530 << "Die: "
4531 << "0x" << std::hex << (intptr_t)this << std::dec
4532 << ", Offset: " << Offset
4533 << ", Size: " << Size
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004534 << "\n";
4535
Jim Laskeyef42a012006-11-02 20:12:39 +00004536 O << Indent
4537 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00004538 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00004539 << ChildrenString(Abbrev.getChildrenFlag());
4540 } else {
4541 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00004542 }
4543 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00004544
Owen Anderson873e1b52008-06-24 21:44:59 +00004545 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004546
Jim Laskeyef42a012006-11-02 20:12:39 +00004547 IndentCount += 2;
4548 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4549 O << Indent;
Bill Wendling4e974012008-07-22 00:28:47 +00004550
4551 if (!isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +00004552 O << AttributeString(Data[i].getAttribute());
Bill Wendling4e974012008-07-22 00:28:47 +00004553 else
Jim Laskeyef42a012006-11-02 20:12:39 +00004554 O << "Blk[" << i << "]";
Bill Wendling4e974012008-07-22 00:28:47 +00004555
Jim Laskeyef42a012006-11-02 20:12:39 +00004556 O << " "
4557 << FormEncodingString(Data[i].getForm())
4558 << " ";
4559 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00004560 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00004561 }
Jim Laskeyef42a012006-11-02 20:12:39 +00004562 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00004563
Jim Laskeyef42a012006-11-02 20:12:39 +00004564 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4565 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00004566 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004567
Jim Laskeyef42a012006-11-02 20:12:39 +00004568 if (!isBlock) O << "\n";
4569 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00004570}
4571
Jim Laskeyef42a012006-11-02 20:12:39 +00004572void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004573 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00004574}
Jim Laskeybd761842006-02-27 17:27:12 +00004575#endif
Jim Laskey65195462006-10-30 13:35:07 +00004576
4577//===----------------------------------------------------------------------===//
4578/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00004579///
Jim Laskey65195462006-10-30 13:35:07 +00004580
Bill Wendling91b8b802009-03-10 20:41:52 +00004581DwarfWriter::DwarfWriter()
Bill Wendling163ba3f2009-03-10 21:23:25 +00004582 : ImmutablePass(&ID), DD(0), DE(0) {}
Jim Laskey65195462006-10-30 13:35:07 +00004583
4584DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00004585 delete DE;
4586 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00004587}
4588
Jim Laskey65195462006-10-30 13:35:07 +00004589/// BeginModule - Emit all Dwarf sections that should come prior to the
4590/// content.
Devang Pateleb3fc282009-01-08 23:40:34 +00004591void DwarfWriter::BeginModule(Module *M,
4592 MachineModuleInfo *MMI,
4593 raw_ostream &OS, AsmPrinter *A,
4594 const TargetAsmInfo *T) {
4595 DE = new DwarfException(OS, A, T);
4596 DD = new DwarfDebug(OS, A, T);
Jim Laskey072200c2007-01-29 18:51:14 +00004597 DE->BeginModule(M);
4598 DD->BeginModule(M);
Devang Patel7bb89ed2009-01-13 00:20:51 +00004599 DD->SetDebugInfo(MMI);
Devang Pateleb3fc282009-01-08 23:40:34 +00004600 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00004601}
4602
4603/// EndModule - Emit all Dwarf sections that should come after the content.
4604///
4605void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00004606 DE->EndModule();
4607 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00004608}
4609
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004610/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00004611/// emitted immediately after the function entry point.
4612void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00004613 DE->BeginFunction(MF);
4614 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00004615}
4616
4617/// EndFunction - Gather and emit post-function debug information.
4618///
Bill Wendlingd751c642008-09-26 00:28:12 +00004619void DwarfWriter::EndFunction(MachineFunction *MF) {
4620 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004621 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004622
Bill Wendlingc91d0b92008-07-22 00:53:37 +00004623 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00004624 // Clear function debug information.
4625 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00004626}
Devang Patelccca7fe2009-01-12 19:17:34 +00004627
Devang Patelcf3a4482009-01-15 23:41:32 +00004628/// ValidDebugInfo - Return true if V represents valid debug info value.
Devang Patel48c7fa22009-04-13 18:13:16 +00004629bool DwarfWriter::ValidDebugInfo(Value *V, bool FastISel) {
4630 return DD && DD->ValidDebugInfo(V, FastISel);
Devang Patelcf3a4482009-01-15 23:41:32 +00004631}
4632
Devang Patelccca7fe2009-01-12 19:17:34 +00004633/// RecordSourceLine - Records location information and associates it with a
4634/// label. Returns a unique label ID used to generate a label and provide
4635/// correspondence to the source line list.
4636unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
4637 unsigned Src) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004638 return DD->RecordSourceLine(Line, Col, Src);
Devang Patelccca7fe2009-01-12 19:17:34 +00004639}
4640
Evan Chenge3d42322009-02-25 07:04:34 +00004641/// getOrCreateSourceID - Look up the source id with the given directory and
4642/// source file names. If none currently exists, create a new id and insert it
4643/// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
4644/// as well.
4645unsigned DwarfWriter::getOrCreateSourceID(const std::string &DirName,
4646 const std::string &FileName) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004647 return DD->getOrCreateSourceID(DirName, FileName);
Devang Patelccca7fe2009-01-12 19:17:34 +00004648}
4649
4650/// RecordRegionStart - Indicate the start of a region.
4651unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004652 return DD->RecordRegionStart(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004653}
4654
Devang Patel0f7fef32009-04-13 17:02:03 +00004655/// RecordRegionStart - Indicate the start of a region.
4656unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V, unsigned ID) {
4657 return DD->RecordRegionStart(V, ID);
4658}
4659
Devang Patelccca7fe2009-01-12 19:17:34 +00004660/// RecordRegionEnd - Indicate the end of a region.
4661unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004662 return DD->RecordRegionEnd(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004663}
4664
4665/// getRecordSourceLineCount - Count source lines.
4666unsigned DwarfWriter::getRecordSourceLineCount() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004667 return DD->getRecordSourceLineCount();
Devang Patelccca7fe2009-01-12 19:17:34 +00004668}
Devang Patelbb8c5952009-01-13 21:25:00 +00004669
Devang Patelc48c5502009-01-13 21:44:10 +00004670/// RecordVariable - Indicate the declaration of a local variable.
4671///
4672void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
4673 DD->RecordVariable(GV, FrameIndex);
4674}
Devang Patelbbdc8202009-01-13 23:54:55 +00004675
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004676/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
4677/// be emitted.
4678bool DwarfWriter::ShouldEmitDwarfDebug() const {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004679 return DD->ShouldEmitDwarfDebug();
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004680}
Devang Patel0f7fef32009-04-13 17:02:03 +00004681
4682//// RecordInlineInfo - Global variable GV is inlined at the location marked
4683//// by LabelID label.
4684void DwarfWriter::RecordInlineInfo(GlobalVariable *GV, unsigned LabelID) {
4685 DD->RecordInlineInfo(GV, LabelID);
4686}
4687