blob: 597f925f51c163ce086fb72634aea0dcd3a4bb7a [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 Patel1be3ecc2009-04-15 00:10:26 +00001143 virtual ~DbgScope() {
Devang Patel481ff5b2009-01-12 18:48:36 +00001144 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); }
Devang Patel1be3ecc2009-04-15 00:10:26 +00001165
1166 virtual bool isInlinedSubroutine() { return false; }
1167 virtual unsigned getLine() { assert ( 0 && "Unexpected scope!"); }
1168 virtual unsigned getColumn() { assert ( 0 && "Unexpected scope!"); }
1169 virtual unsigned getFile() { assert ( 0 && "Unexpected scope!"); }
1170};
1171
1172
1173//===----------------------------------------------------------------------===//
1174/// DbgInlinedSubroutineScope - This class is used to track inlined subroutine
1175/// scope information.
1176///
1177class DbgInlinedSubroutineScope : public DbgScope {
1178 unsigned Src;
1179 unsigned Line;
1180 unsigned Col;
1181public:
1182 DbgInlinedSubroutineScope(DbgScope *P, DIDescriptor D,
1183 unsigned S, unsigned L, unsigned C)
1184 : DbgScope(P, D), Src(S), Line(L), Col(C)
1185 {}
1186
1187 unsigned getLine() { return Line; }
1188 unsigned getColumn() { return Col; }
1189 unsigned getFile() { return Src; }
1190 bool isInlinedSubroutine() { return true; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001191};
1192
1193//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001194/// DwarfDebug - Emits Dwarf debug directives.
Jim Laskey072200c2007-01-29 18:51:14 +00001195///
1196class DwarfDebug : public Dwarf {
Jim Laskey65195462006-10-30 13:35:07 +00001197 //===--------------------------------------------------------------------===//
1198 // Attributes used to construct specific Dwarf sections.
1199 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001200
Evan Chenge3d42322009-02-25 07:04:34 +00001201 /// CompileUnitMap - A map of global variables representing compile units to
1202 /// compile units.
1203 DenseMap<Value *, CompileUnit *> CompileUnitMap;
1204
1205 /// CompileUnits - All the compile units in this module.
1206 ///
1207 SmallVector<CompileUnit *, 8> CompileUnits;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001208
Devang Pateldd9db662009-01-30 18:20:31 +00001209 /// MainCU - Some platform prefers one compile unit per .o file. In such
1210 /// cases, all dies are inserted in MainCU.
1211 CompileUnit *MainCU;
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001212
Jim Laskeyef42a012006-11-02 20:12:39 +00001213 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +00001214 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001215 FoldingSet<DIEAbbrev> AbbreviationsSet;
1216
1217 /// Abbreviations - A list of all the unique abbreviations in use.
1218 ///
1219 std::vector<DIEAbbrev *> Abbreviations;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001220
Evan Chenge3d42322009-02-25 07:04:34 +00001221 /// DirectoryIdMap - Directory name to directory id map.
1222 ///
1223 StringMap<unsigned> DirectoryIdMap;
Devang Patel8526cc02009-01-05 22:35:52 +00001224
Evan Chenge3d42322009-02-25 07:04:34 +00001225 /// DirectoryNames - A list of directory names.
1226 SmallVector<std::string, 8> DirectoryNames;
1227
1228 /// SourceFileIdMap - Source file name to source file id map.
1229 ///
1230 StringMap<unsigned> SourceFileIdMap;
1231
1232 /// SourceFileNames - A list of source file names.
1233 SmallVector<std::string, 8> SourceFileNames;
1234
1235 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
1236 /// id mapped to a unique id.
1237 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
1238
1239 /// SourceIds - Reverse map from source id to directory id + file id pair.
1240 ///
1241 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
Devang Patel8526cc02009-01-05 22:35:52 +00001242
Devang Patel07354e52009-01-16 21:07:53 +00001243 /// Lines - List of of source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001244 std::vector<SrcLineInfo> Lines;
1245
Devang Patel07354e52009-01-16 21:07:53 +00001246 /// ValuesSet - Used to uniquely define values.
1247 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001248 FoldingSet<DIEValue> ValuesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001249
Jim Laskeyef42a012006-11-02 20:12:39 +00001250 /// Values - A list of all the unique values in use.
1251 ///
1252 std::vector<DIEValue *> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001253
Jim Laskey65195462006-10-30 13:35:07 +00001254 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +00001255 ///
Jim Laskey65195462006-10-30 13:35:07 +00001256 UniqueVector<std::string> StringPool;
1257
Jim Laskey65195462006-10-30 13:35:07 +00001258 /// SectionMap - Provides a unique id per text section.
1259 ///
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001260 UniqueVector<const Section*> SectionMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001261
Jim Laskey65195462006-10-30 13:35:07 +00001262 /// SectionSourceLines - Tracks line numbers per text section.
1263 ///
Devang Patelf3ee5142009-01-12 22:54:42 +00001264 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Jim Laskey65195462006-10-30 13:35:07 +00001265
Jim Laskeybacd3042007-02-21 22:48:45 +00001266 /// didInitial - Flag to indicate if initial emission has been done.
1267 ///
1268 bool didInitial;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001269
Jim Laskeybacd3042007-02-21 22:48:45 +00001270 /// shouldEmit - Flag to indicate if debug information should be emitted.
1271 ///
1272 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +00001273
Devang Patel0e5200f2009-01-15 18:25:17 +00001274 // RootDbgScope - Top level scope for the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001275 //
1276 DbgScope *RootDbgScope;
1277
Bill Wendling163ba3f2009-03-10 21:23:25 +00001278 /// DbgScopeMap - Tracks the scopes in the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001279 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
Bill Wendling163ba3f2009-03-10 21:23:25 +00001280
Devang Patel1be3ecc2009-04-15 00:10:26 +00001281 /// DbgInlinedScopeMap - Tracks inlined scopes in the current function.
1282 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> > DbgInlinedScopeMap;
1283
Devang Patel0f7fef32009-04-13 17:02:03 +00001284 /// InlineInfo - Keep track of inlined functions and their location.
1285 /// This information is used to populate debug_inlined section.
1286 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> > InlineInfo;
1287
Devang Patel1be3ecc2009-04-15 00:10:26 +00001288 /// InlinedVariableScopes - Scopes information for the inlined subroutine
1289 /// variables.
1290 DenseMap<const MachineInstr *, DbgScope *> InlinedVariableScopes;
1291
Bill Wendling163ba3f2009-03-10 21:23:25 +00001292 /// DebugTimer - Timer for the Dwarf debug writer.
1293 Timer *DebugTimer;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001294
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001295 struct FunctionDebugFrameInfo {
1296 unsigned Number;
1297 std::vector<MachineMove> Moves;
1298
1299 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001300 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001301 };
1302
1303 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001304
Bill Wendling163ba3f2009-03-10 21:23:25 +00001305private:
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001306 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
Bill Wendlingc8615e42009-03-10 21:47:45 +00001307 /// maps to the source id. Source id starts at 1.
1308 std::pair<unsigned, unsigned>
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001309 getSourceDirectoryAndFileIds(unsigned SId) const {
Bill Wendlingc8615e42009-03-10 21:47:45 +00001310 return SourceIds[SId-1];
1311 }
1312
1313 /// getNumSourceDirectories - Return the number of source directories in the
1314 /// debug info.
1315 unsigned getNumSourceDirectories() const {
1316 return DirectoryNames.size();
1317 }
1318
1319 /// getSourceDirectoryName - Return the name of the directory corresponding
1320 /// to the id.
1321 const std::string &getSourceDirectoryName(unsigned Id) const {
1322 return DirectoryNames[Id - 1];
1323 }
1324
1325 /// getSourceFileName - Return the name of the source file corresponding
1326 /// to the id.
1327 const std::string &getSourceFileName(unsigned Id) const {
1328 return SourceFileNames[Id - 1];
1329 }
1330
1331 /// getNumSourceIds - Return the number of unique source ids.
Bill Wendlingc8615e42009-03-10 21:47:45 +00001332 unsigned getNumSourceIds() const {
1333 return SourceIds.size();
1334 }
1335
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001336 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001337 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001338 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1339 // Profile the node so that we can make it unique.
1340 FoldingSetNodeID ID;
1341 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001342
Jim Laskeyef42a012006-11-02 20:12:39 +00001343 // Check the set for priors.
1344 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001345
Jim Laskeyef42a012006-11-02 20:12:39 +00001346 // If it's newly added.
1347 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001348 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +00001349 Abbreviations.push_back(&Abbrev);
1350 // Assign the vector position + 1 as its number.
1351 Abbrev.setNumber(Abbreviations.size());
1352 } else {
1353 // Assign existing abbreviation number.
1354 Abbrev.setNumber(InSet->getNumber());
1355 }
1356 }
1357
Jim Laskey65195462006-10-30 13:35:07 +00001358 /// NewString - Add a string to the constant pool and returns a label.
1359 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001360 DWLabel NewString(const std::string &String) {
1361 unsigned StringID = StringPool.insert(String);
1362 return DWLabel("string", StringID);
1363 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001364
Jim Laskeyef42a012006-11-02 20:12:39 +00001365 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1366 /// entry.
1367 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1368 DIEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001369
Jim Laskeyef42a012006-11-02 20:12:39 +00001370 if (Entry) {
1371 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001372 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001373 void *Where;
1374 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001375
Jim Laskeyf6733882006-11-02 21:48:18 +00001376 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001377
Jim Laskeyef42a012006-11-02 20:12:39 +00001378 Value = new DIEntry(Entry);
1379 ValuesSet.InsertNode(Value, Where);
1380 } else {
1381 Value = new DIEntry(Entry);
1382 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001383
Jim Laskeyef42a012006-11-02 20:12:39 +00001384 Values.push_back(Value);
1385 return Value;
1386 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001387
Jim Laskeyef42a012006-11-02 20:12:39 +00001388 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1389 ///
1390 void SetDIEntry(DIEntry *Value, DIE *Entry) {
Bill Wendlingdd446322009-03-10 23:57:09 +00001391 Value->setEntry(Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001392 // Add to values set if not already there. If it is, we merely have a
1393 // duplicate in the values list (no harm.)
1394 ValuesSet.GetOrInsertNode(Value);
1395 }
1396
1397 /// AddUInt - Add an unsigned integer attribute data and value.
1398 ///
1399 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1400 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1401
1402 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001403 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001404 void *Where;
1405 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1406 if (!Value) {
1407 Value = new DIEInteger(Integer);
1408 ValuesSet.InsertNode(Value, Where);
1409 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001410 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001411
Jim Laskeyef42a012006-11-02 20:12:39 +00001412 Die->AddValue(Attribute, Form, Value);
1413 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001414
Jim Laskeyef42a012006-11-02 20:12:39 +00001415 /// AddSInt - Add an signed integer attribute data and value.
1416 ///
1417 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1418 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1419
1420 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001421 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001422 void *Where;
1423 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1424 if (!Value) {
1425 Value = new DIEInteger(Integer);
1426 ValuesSet.InsertNode(Value, Where);
1427 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001428 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001429
Jim Laskeyef42a012006-11-02 20:12:39 +00001430 Die->AddValue(Attribute, Form, Value);
1431 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001432
Evan Chenge3d42322009-02-25 07:04:34 +00001433 /// AddString - Add a string attribute data and value.
Jim Laskeyef42a012006-11-02 20:12:39 +00001434 ///
1435 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1436 const std::string &String) {
1437 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001438 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001439 void *Where;
1440 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1441 if (!Value) {
1442 Value = new DIEString(String);
1443 ValuesSet.InsertNode(Value, Where);
1444 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001445 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001446
Jim Laskeyef42a012006-11-02 20:12:39 +00001447 Die->AddValue(Attribute, Form, Value);
1448 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001449
Jim Laskeyef42a012006-11-02 20:12:39 +00001450 /// AddLabel - Add a Dwarf label attribute data and value.
1451 ///
1452 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1453 const DWLabel &Label) {
1454 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001455 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001456 void *Where;
1457 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1458 if (!Value) {
1459 Value = new DIEDwarfLabel(Label);
1460 ValuesSet.InsertNode(Value, Where);
1461 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001462 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001463
Jim Laskeyef42a012006-11-02 20:12:39 +00001464 Die->AddValue(Attribute, Form, Value);
1465 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001466
Jim Laskeyef42a012006-11-02 20:12:39 +00001467 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1468 ///
1469 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1470 const std::string &Label) {
1471 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001472 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001473 void *Where;
1474 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1475 if (!Value) {
1476 Value = new DIEObjectLabel(Label);
1477 ValuesSet.InsertNode(Value, Where);
1478 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001479 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001480
Jim Laskeyef42a012006-11-02 20:12:39 +00001481 Die->AddValue(Attribute, Form, Value);
1482 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001483
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001484 /// AddSectionOffset - Add a section offset label attribute data and value.
1485 ///
1486 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1487 const DWLabel &Label, const DWLabel &Section,
1488 bool isEH = false, bool useSet = true) {
1489 FoldingSetNodeID ID;
1490 DIESectionOffset::Profile(ID, Label, Section);
1491 void *Where;
1492 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1493 if (!Value) {
1494 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1495 ValuesSet.InsertNode(Value, Where);
1496 Values.push_back(Value);
1497 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001498
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001499 Die->AddValue(Attribute, Form, Value);
1500 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001501
Jim Laskeyef42a012006-11-02 20:12:39 +00001502 /// AddDelta - Add a label delta attribute data and value.
1503 ///
1504 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
Devang Patel1be3ecc2009-04-15 00:10:26 +00001505 const DWLabel &Hi, const DWLabel &Lo) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001506 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001507 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001508 void *Where;
1509 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1510 if (!Value) {
1511 Value = new DIEDelta(Hi, Lo);
1512 ValuesSet.InsertNode(Value, Where);
1513 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001514 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001515
Jim Laskeyef42a012006-11-02 20:12:39 +00001516 Die->AddValue(Attribute, Form, Value);
1517 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001518
Jim Laskeyef42a012006-11-02 20:12:39 +00001519 /// AddDIEntry - Add a DIE attribute data and value.
1520 ///
1521 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1522 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1523 }
1524
1525 /// AddBlock - Add block data.
1526 ///
1527 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1528 Block->ComputeSize(*this);
1529 FoldingSetNodeID ID;
1530 Block->Profile(ID);
1531 void *Where;
1532 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1533 if (!Value) {
1534 Value = Block;
1535 ValuesSet.InsertNode(Value, Where);
1536 Values.push_back(Value);
1537 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001538 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001539 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001540 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001541 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001542
Jim Laskeyef42a012006-11-02 20:12:39 +00001543 Die->AddValue(Attribute, Block->BestForm(), Value);
1544 }
1545
Jim Laskey65195462006-10-30 13:35:07 +00001546 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001547 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001548 void AddSourceLine(DIE *Die, const DIVariable *V) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001549 unsigned FileID = 0;
1550 unsigned Line = V->getLineNumber();
Devang Pateldd9db662009-01-30 18:20:31 +00001551 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1552 FileID = Unit->getID();
Devang Patel2303df92009-02-10 06:04:08 +00001553 assert (FileID && "Invalid file id");
Devang Patel7a6e5a32009-01-08 02:33:41 +00001554 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1555 AddUInt(Die, DW_AT_decl_line, 0, Line);
1556 }
1557
1558 /// AddSourceLine - Add location information to specified debug information
1559 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001560 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Devang Patel8526cc02009-01-05 22:35:52 +00001561 unsigned FileID = 0;
1562 unsigned Line = G->getLineNumber();
Devang Pateldd9db662009-01-30 18:20:31 +00001563 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1564 FileID = Unit->getID();
Devang Patel2303df92009-02-10 06:04:08 +00001565 assert (FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001566 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1567 AddUInt(Die, DW_AT_decl_line, 0, Line);
1568 }
1569
Devang Patel99ec3532009-01-16 19:28:14 +00001570 void AddSourceLine(DIE *Die, const DIType *Ty) {
Devang Patel8526cc02009-01-05 22:35:52 +00001571 unsigned FileID = 0;
Devang Patel99ec3532009-01-16 19:28:14 +00001572 unsigned Line = Ty->getLineNumber();
Devang Pateldd9db662009-01-30 18:20:31 +00001573 DICompileUnit CU = Ty->getCompileUnit();
1574 if (CU.isNull())
1575 return;
1576 CompileUnit *Unit = FindCompileUnit(CU);
1577 FileID = Unit->getID();
Devang Patel2303df92009-02-10 06:04:08 +00001578 assert (FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001579 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1580 AddUInt(Die, DW_AT_decl_line, 0, Line);
1581 }
1582
Jim Laskey65195462006-10-30 13:35:07 +00001583 /// AddAddress - Add an address attribute to a die based on the location
1584 /// provided.
1585 void AddAddress(DIE *Die, unsigned Attribute,
Devang Patel1be3ecc2009-04-15 00:10:26 +00001586 const MachineLocation &Location) {
Dan Gohmand735b802008-10-03 15:45:36 +00001587 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001588 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001589
Dan Gohmand735b802008-10-03 15:45:36 +00001590 if (Location.isReg()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001591 if (Reg < 32) {
1592 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1593 } else {
1594 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1595 AddUInt(Block, 0, DW_FORM_udata, Reg);
1596 }
1597 } else {
1598 if (Reg < 32) {
1599 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1600 } else {
1601 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1602 AddUInt(Block, 0, DW_FORM_udata, Reg);
1603 }
1604 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1605 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001606
Jim Laskeyef42a012006-11-02 20:12:39 +00001607 AddBlock(Die, Attribute, 0, Block);
1608 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001609
Jim Laskeyef42a012006-11-02 20:12:39 +00001610 /// AddType - Add a new type attribute to the specified entity.
Devang Patel48d190f2009-01-05 21:47:57 +00001611 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel80303aa2009-01-23 19:13:31 +00001612 if (Ty.isNull())
Devang Patel48d190f2009-01-05 21:47:57 +00001613 return;
Devang Patel48d190f2009-01-05 21:47:57 +00001614
1615 // Check for pre-existence.
1616 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1617 // If it exists then use the existing value.
1618 if (Slot) {
1619 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1620 return;
1621 }
1622
1623 // Set up proxy.
1624 Slot = NewDIEntry();
1625
1626 // Construct type.
1627 DIE Buffer(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001628 if (Ty.isBasicType(Ty.getTag()))
1629 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1630 else if (Ty.isDerivedType(Ty.getTag()))
1631 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1632 else {
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001633 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelf193ff02009-01-15 19:26:23 +00001634 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1635 }
1636
Devang Patel7009d242009-01-27 23:22:55 +00001637 // Add debug information entry to entity and appropriate context.
1638 DIE *Die = NULL;
1639 DIDescriptor Context = Ty.getContext();
1640 if (!Context.isNull())
1641 Die = DW_Unit->getDieMapSlotFor(Context.getGV());
1642
1643 if (Die) {
1644 DIE *Child = new DIE(Buffer);
1645 Die->AddChild(Child);
1646 Buffer.Detach();
1647 SetDIEntry(Slot, Child);
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001648 } else {
Devang Patel7009d242009-01-27 23:22:55 +00001649 Die = DW_Unit->AddDie(Buffer);
1650 SetDIEntry(Slot, Die);
1651 }
1652
Devang Patel48d190f2009-01-05 21:47:57 +00001653 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1654 }
1655
Devang Patele5202732009-01-05 19:07:53 +00001656 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1657 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001658 DIBasicType BTy) {
Bill Wendling0582ae92009-03-13 04:39:26 +00001659
Devang Patel08f053f2009-01-05 17:57:47 +00001660 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001661 std::string Name;
1662 BTy.getName(Name);
Devang Patel08f053f2009-01-05 17:57:47 +00001663 Buffer.setTag(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001664 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patel08f053f2009-01-05 17:57:47 +00001665 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001666 if (!Name.empty())
Devang Patel08f053f2009-01-05 17:57:47 +00001667 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001668 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel08f053f2009-01-05 17:57:47 +00001669 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1670 }
1671
Devang Patele5202732009-01-05 19:07:53 +00001672 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1673 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001674 DIDerivedType DTy) {
Bill Wendling0582ae92009-03-13 04:39:26 +00001675
Devang Patel08f053f2009-01-05 17:57:47 +00001676 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001677 std::string Name;
1678 DTy.getName(Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001679 uint64_t Size = DTy.getSizeInBits() >> 3;
1680 unsigned Tag = DTy.getTag();
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001681
Devang Patel08f053f2009-01-05 17:57:47 +00001682 // FIXME - Workaround for templates.
1683 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1684
1685 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001686
Devang Patel08f053f2009-01-05 17:57:47 +00001687 // Map to main type, void will not have a type.
Devang Patelf193ff02009-01-15 19:26:23 +00001688 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00001689 AddType(DW_Unit, &Buffer, FromTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001690
1691 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001692 if (!Name.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00001693 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel08f053f2009-01-05 17:57:47 +00001694
1695 // Add size if non-zero (derived types might be zero-sized.)
1696 if (Size)
1697 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1698
1699 // Add source line info if available and TyDesc is not a forward
1700 // declaration.
Devang Patelad165be2009-01-27 00:45:04 +00001701 if (!DTy.isForwardDecl())
1702 AddSourceLine(&Buffer, &DTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001703 }
1704
Devang Patelf4215332009-01-05 19:55:51 +00001705 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1706 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001707 DICompositeType CTy) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001708 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001709 std::string Name;
1710 CTy.getName(Name);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001711
Devang Patelf193ff02009-01-15 19:26:23 +00001712 uint64_t Size = CTy.getSizeInBits() >> 3;
1713 unsigned Tag = CTy.getTag();
Devang Patel49f38cb2009-01-23 01:19:09 +00001714 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001715
Devang Patelf4215332009-01-05 19:55:51 +00001716 switch (Tag) {
1717 case DW_TAG_vector_type:
1718 case DW_TAG_array_type:
Devang Patelf193ff02009-01-15 19:26:23 +00001719 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001720 break;
Devang Pateleab4a2e2009-01-20 18:35:14 +00001721 case DW_TAG_enumeration_type:
1722 {
1723 DIArray Elements = CTy.getTypeArray();
1724 // Add enumerators to enumeration type.
1725 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1726 DIE *ElemDie = NULL;
1727 DIEnumerator Enum(Elements.getElement(i).getGV());
1728 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
1729 Buffer.AddChild(ElemDie);
1730 }
1731 }
1732 break;
Devang Patelf4215332009-01-05 19:55:51 +00001733 case DW_TAG_subroutine_type:
1734 {
1735 // Add prototype flag.
1736 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patelf193ff02009-01-15 19:26:23 +00001737 DIArray Elements = CTy.getTypeArray();
Devang Patelf4215332009-01-05 19:55:51 +00001738 // Add return type.
Devang Patel48d190f2009-01-05 21:47:57 +00001739 DIDescriptor RTy = Elements.getElement(0);
Devang Patelf193ff02009-01-15 19:26:23 +00001740 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel48d190f2009-01-05 21:47:57 +00001741
Devang Patelf4215332009-01-05 19:55:51 +00001742 // Add arguments.
1743 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1744 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00001745 DIDescriptor Ty = Elements.getElement(i);
Devang Patel7ab24502009-01-17 06:57:25 +00001746 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patelf4215332009-01-05 19:55:51 +00001747 Buffer.AddChild(Arg);
1748 }
1749 }
1750 break;
1751 case DW_TAG_structure_type:
1752 case DW_TAG_union_type:
Devang Patel86bda412009-03-25 00:28:40 +00001753 case DW_TAG_class_type:
Devang Patelf4215332009-01-05 19:55:51 +00001754 {
1755 // Add elements to structure type.
Devang Patelf193ff02009-01-15 19:26:23 +00001756 DIArray Elements = CTy.getTypeArray();
Devang Patel153745c2009-01-16 00:50:53 +00001757
1758 // A forward struct declared type may not have elements available.
1759 if (Elements.isNull())
1760 break;
1761
Devang Patelf4215332009-01-05 19:55:51 +00001762 // Add elements to structure type.
1763 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1764 DIDescriptor Element = Elements.getElement(i);
Devang Patel5aac3d32009-01-17 08:01:33 +00001765 DIE *ElemDie = NULL;
Devang Patelf193ff02009-01-15 19:26:23 +00001766 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel2d1768c2009-01-17 08:05:14 +00001767 ElemDie = CreateSubprogramDIE(DW_Unit,
1768 DISubprogram(Element.getGV()));
Devang Patel5aac3d32009-01-17 08:01:33 +00001769 else if (Element.getTag() == dwarf::DW_TAG_variable) // ???
1770 ElemDie = CreateGlobalVariableDIE(DW_Unit,
1771 DIGlobalVariable(Element.getGV()));
Devang Patel2be58932009-01-20 21:02:02 +00001772 else
1773 ElemDie = CreateMemberDIE(DW_Unit,
1774 DIDerivedType(Element.getGV()));
Devang Patel2d1768c2009-01-17 08:05:14 +00001775 Buffer.AddChild(ElemDie);
Devang Patelf4215332009-01-05 19:55:51 +00001776 }
Devang Patel13319ce2009-02-17 22:43:44 +00001777 unsigned RLang = CTy.getRunTimeLang();
1778 if (RLang)
1779 AddUInt(&Buffer, DW_AT_APPLE_runtime_class, DW_FORM_data1, RLang);
Devang Patelf4215332009-01-05 19:55:51 +00001780 }
1781 break;
1782 default:
1783 break;
1784 }
1785
1786 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001787 if (!Name.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00001788 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf4215332009-01-05 19:55:51 +00001789
Devang Patelad165be2009-01-27 00:45:04 +00001790 if (Tag == DW_TAG_enumeration_type || Tag == DW_TAG_structure_type
1791 || Tag == DW_TAG_union_type) {
1792 // Add size if non-zero (derived types might be zero-sized.)
1793 if (Size)
1794 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1795 else {
1796 // Add zero size if it is not a forward declaration.
1797 if (CTy.isForwardDecl())
1798 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1799 else
1800 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1801 }
1802
1803 // Add source line info if available.
1804 if (!CTy.isForwardDecl())
1805 AddSourceLine(&Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001806 }
Devang Patelf4215332009-01-05 19:55:51 +00001807 }
1808
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001809 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1810 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelf193ff02009-01-15 19:26:23 +00001811 int64_t L = SR.getLo();
1812 int64_t H = SR.getHi();
Devang Patel68afdc32009-01-05 18:33:01 +00001813 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1814 if (L != H) {
1815 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1816 if (L)
Devang Patel2d1768c2009-01-17 08:05:14 +00001817 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1818 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel68afdc32009-01-05 18:33:01 +00001819 }
1820 Buffer.AddChild(DW_Subrange);
1821 }
1822
1823 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1824 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1825 DICompositeType *CTy) {
1826 Buffer.setTag(DW_TAG_array_type);
1827 if (CTy->getTag() == DW_TAG_vector_type)
1828 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1829
Devang Patelf9235742009-01-28 21:08:20 +00001830 // Emit derived type.
1831 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel68afdc32009-01-05 18:33:01 +00001832 DIArray Elements = CTy->getTypeArray();
Devang Patel68afdc32009-01-05 18:33:01 +00001833
1834 // Construct an anonymous type for index type.
1835 DIE IdxBuffer(DW_TAG_base_type);
1836 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1837 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1838 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1839
1840 // Add subranges to array type.
1841 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patelf4215332009-01-05 19:55:51 +00001842 DIDescriptor Element = Elements.getElement(i);
Devang Patelf193ff02009-01-15 19:26:23 +00001843 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1844 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel68afdc32009-01-05 18:33:01 +00001845 }
1846 }
1847
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001848 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Pateleab4a2e2009-01-20 18:35:14 +00001849 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patelc69bf2c2009-01-05 18:38:38 +00001850
1851 DIE *Enumerator = new DIE(DW_TAG_enumerator);
Bill Wendling0582ae92009-03-13 04:39:26 +00001852 std::string Name;
1853 ETy->getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001854 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
Devang Patelc69bf2c2009-01-05 18:38:38 +00001855 int64_t Value = ETy->getEnumValue();
1856 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Pateleab4a2e2009-01-20 18:35:14 +00001857 return Enumerator;
Devang Patelc69bf2c2009-01-05 18:38:38 +00001858 }
Devang Patel68afdc32009-01-05 18:33:01 +00001859
Devang Patel5aac3d32009-01-17 08:01:33 +00001860 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001861 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patel5aac3d32009-01-17 08:01:33 +00001862 {
1863 DIE *GVDie = new DIE(DW_TAG_variable);
Bill Wendling0582ae92009-03-13 04:39:26 +00001864 std::string Name;
1865 GV.getDisplayName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001866 AddString(GVDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling0582ae92009-03-13 04:39:26 +00001867 std::string LinkageName;
1868 GV.getLinkageName(LinkageName);
1869 if (!LinkageName.empty())
Devang Patel5aac3d32009-01-17 08:01:33 +00001870 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1871 AddType(DW_Unit, GVDie, GV.getType());
1872 if (!GV.isLocalToUnit())
1873 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
1874 AddSourceLine(GVDie, &GV);
1875 return GVDie;
Devang Patel86ae1422009-01-05 18:59:44 +00001876 }
1877
Devang Patel2be58932009-01-20 21:02:02 +00001878 /// CreateMemberDIE - Create new member DIE.
1879 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
1880 DIE *MemberDie = new DIE(DT.getTag());
Bill Wendling0582ae92009-03-13 04:39:26 +00001881 std::string Name;
1882 DT.getName(Name);
1883 if (!Name.empty())
Devang Patel2be58932009-01-20 21:02:02 +00001884 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1885
1886 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1887
1888 AddSourceLine(MemberDie, &DT);
1889
Devang Patel36375ee2009-02-17 21:23:59 +00001890 uint64_t Size = DT.getSizeInBits();
1891 uint64_t FieldSize = DT.getOriginalTypeSize();
1892
1893 if (Size != FieldSize) {
1894 // Handle bitfield.
1895 AddUInt(MemberDie, DW_AT_byte_size, 0, DT.getOriginalTypeSize() >> 3);
1896 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1897
1898 uint64_t Offset = DT.getOffsetInBits();
1899 uint64_t FieldOffset = Offset;
1900 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1901 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1902 FieldOffset = (HiMark - FieldSize);
1903 Offset -= FieldOffset;
1904 // Maybe we need to work from the other end.
1905 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1906 AddUInt(MemberDie, DW_AT_bit_offset, 0, Offset);
1907 }
Devang Patel2be58932009-01-20 21:02:02 +00001908 DIEBlock *Block = new DIEBlock();
1909 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1910 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1911 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1912
Devang Patel47661592009-01-21 00:08:04 +00001913 if (DT.isProtected())
1914 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1915 else if (DT.isPrivate())
1916 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1917
Devang Patel2be58932009-01-20 21:02:02 +00001918 return MemberDie;
1919 }
1920
Devang Patel5aac3d32009-01-17 08:01:33 +00001921 /// CreateSubprogramDIE - Create new DIE using SP.
1922 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Devang Patel2d1768c2009-01-17 08:05:14 +00001923 const DISubprogram &SP,
1924 bool IsConstructor = false) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001925 DIE *SPDie = new DIE(DW_TAG_subprogram);
Bill Wendling0582ae92009-03-13 04:39:26 +00001926 std::string Name;
1927 SP.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001928 AddString(SPDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling0582ae92009-03-13 04:39:26 +00001929 std::string LinkageName;
1930 SP.getLinkageName(LinkageName);
1931 if (!LinkageName.empty())
Devang Patel5aac3d32009-01-17 08:01:33 +00001932 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Devang Patel2d1768c2009-01-17 08:05:14 +00001933 LinkageName);
Devang Patel5aac3d32009-01-17 08:01:33 +00001934 AddSourceLine(SPDie, &SP);
Devang Patel86ae1422009-01-05 18:59:44 +00001935
Devang Patel5aac3d32009-01-17 08:01:33 +00001936 DICompositeType SPTy = SP.getType();
1937 DIArray Args = SPTy.getTypeArray();
1938
Devang Patel86ae1422009-01-05 18:59:44 +00001939 // Add Return Type.
Devang Patel75b27382009-04-08 22:18:45 +00001940 unsigned SPTag = SPTy.getTag();
Devang Patel9ac08d62009-02-27 18:05:21 +00001941 if (!IsConstructor) {
Devang Patel75b27382009-04-08 22:18:45 +00001942 if (Args.isNull() || SPTag != DW_TAG_subroutine_type)
Devang Patel9ac08d62009-02-27 18:05:21 +00001943 AddType(DW_Unit, SPDie, SPTy);
1944 else
1945 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
1946 }
Devang Pateld234e592009-01-30 01:21:46 +00001947
Devang Pateld5863dd2009-02-02 17:51:41 +00001948 if (!SP.isDefinition()) {
1949 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
1950 // Add arguments.
1951 // Do not add arguments for subprogram definition. They will be
1952 // handled through RecordVariable.
Devang Patel75b27382009-04-08 22:18:45 +00001953 if (SPTag == DW_TAG_subroutine_type)
Devang Pateld5863dd2009-02-02 17:51:41 +00001954 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1955 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1956 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
1957 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
1958 SPDie->AddChild(Arg);
1959 }
1960 }
Devang Pateld234e592009-01-30 01:21:46 +00001961
Devang Patel002ec142009-02-24 00:52:19 +00001962 unsigned Lang = SP.getCompileUnit().getLanguage();
1963 if (Lang == DW_LANG_C99 || Lang == DW_LANG_C89
1964 || Lang == DW_LANG_ObjC)
1965 AddUInt(SPDie, DW_AT_prototyped, DW_FORM_flag, 1);
1966
Devang Patelf193ff02009-01-15 19:26:23 +00001967 if (!SP.isLocalToUnit())
Devang Pateld234e592009-01-30 01:21:46 +00001968 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Devang Patel1be3ecc2009-04-15 00:10:26 +00001969
1970 // DW_TAG_inlined_subroutine may refer to this DIE.
1971 DIE *&Slot = DW_Unit->getDieMapSlotFor(SP.getGV());
1972 Slot = SPDie;
Devang Patel5aac3d32009-01-17 08:01:33 +00001973 return SPDie;
Devang Patel86ae1422009-01-05 18:59:44 +00001974 }
1975
Devang Patel5aac3d32009-01-17 08:01:33 +00001976 /// FindCompileUnit - Get the compile unit for the given descriptor.
1977 ///
Devang Patel8526cc02009-01-05 22:35:52 +00001978 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
Evan Chenge3d42322009-02-25 07:04:34 +00001979 CompileUnit *DW_Unit = CompileUnitMap[Unit.getGV()];
Devang Patel8526cc02009-01-05 22:35:52 +00001980 assert(DW_Unit && "Missing compile unit.");
1981 return DW_Unit;
1982 }
1983
Devang Patelbbdc8202009-01-13 23:54:55 +00001984 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001985 ///
1986 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1987 // Get the descriptor.
Devang Patel99ec3532009-01-16 19:28:14 +00001988 const DIVariable &VD = DV->getVariable();
Devang Patel7a6e5a32009-01-08 02:33:41 +00001989
1990 // Translate tag to proper Dwarf tag. The result variable is dropped for
1991 // now.
1992 unsigned Tag;
Devang Patel99ec3532009-01-16 19:28:14 +00001993 switch (VD.getTag()) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001994 case DW_TAG_return_variable: return NULL;
1995 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1996 case DW_TAG_auto_variable: // fall thru
1997 default: Tag = DW_TAG_variable; break;
1998 }
1999
2000 // Define variable debug information entry.
2001 DIE *VariableDie = new DIE(Tag);
Bill Wendling0582ae92009-03-13 04:39:26 +00002002 std::string Name;
2003 VD.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00002004 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002005
2006 // Add source line info if available.
Devang Patel99ec3532009-01-16 19:28:14 +00002007 AddSourceLine(VariableDie, &VD);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002008
2009 // Add variable type.
Devang Patel99ec3532009-01-16 19:28:14 +00002010 AddType(Unit, VariableDie, VD.getType());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002011
2012 // Add variable address.
2013 MachineLocation Location;
2014 Location.set(RI->getFrameRegister(*MF),
2015 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
2016 AddAddress(VariableDie, DW_AT_location, Location);
2017
2018 return VariableDie;
2019 }
2020
Devang Patel7a6e5a32009-01-08 02:33:41 +00002021 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2022 ///
2023 DbgScope *getOrCreateScope(GlobalVariable *V) {
2024 DbgScope *&Slot = DbgScopeMap[V];
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002025 if (Slot) return Slot;
2026
Devang Patel1be3ecc2009-04-15 00:10:26 +00002027 DbgScope *Parent = NULL;
2028 DIBlock Block(V);
2029 if (!Block.isNull()) {
2030 DIDescriptor ParentDesc = Block.getContext();
2031 Parent =
2032 ParentDesc.isNull() ? NULL : getOrCreateScope(ParentDesc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002033 }
Devang Patel1be3ecc2009-04-15 00:10:26 +00002034 Slot = new DbgScope(Parent, DIDescriptor(V));
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002035
Devang Patel1be3ecc2009-04-15 00:10:26 +00002036 if (Parent)
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002037 Parent->AddScope(Slot);
Devang Patel1be3ecc2009-04-15 00:10:26 +00002038 else
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002039 // First function is top level function.
2040 RootDbgScope = Slot;
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002041
Devang Patel7a6e5a32009-01-08 02:33:41 +00002042 return Slot;
2043 }
2044
Devang Patel1be3ecc2009-04-15 00:10:26 +00002045 /// createInlinedSubroutineScope - Returns the scope associated with the
2046 /// inlined subroutine.
2047 ///
2048 DbgScope *createInlinedSubroutineScope(DISubprogram SP, unsigned Src,
2049 unsigned Line, unsigned Col) {
2050 DbgScope *Scope =
2051 new DbgInlinedSubroutineScope(NULL, SP, Src, Line, Col);
2052
2053 // FIXME - Add inlined function scopes to the root so we can delete them
2054 // later.
2055 assert (RootDbgScope && "Function scope info missing!");
2056 RootDbgScope->AddScope(Scope);
2057 return Scope;
2058 }
2059
Devang Patel7a6e5a32009-01-08 02:33:41 +00002060 /// ConstructDbgScope - Construct the components of a scope.
2061 ///
2062 void ConstructDbgScope(DbgScope *ParentScope,
2063 unsigned ParentStartID, unsigned ParentEndID,
2064 DIE *ParentDie, CompileUnit *Unit) {
2065 // Add variables to scope.
Devang Patel9795da52009-01-10 02:42:49 +00002066 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002067 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2068 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2069 if (VariableDie) ParentDie->AddChild(VariableDie);
2070 }
2071
2072 // Add nested scopes.
Devang Patel9795da52009-01-10 02:42:49 +00002073 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002074 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2075 // Define the Scope debug information entry.
2076 DbgScope *Scope = Scopes[j];
Devang Patel7a6e5a32009-01-08 02:33:41 +00002077
Devang Patele9bfb0f2009-01-12 18:41:00 +00002078 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2079 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002080
Devang Patel1be3ecc2009-04-15 00:10:26 +00002081 // Ignore empty scopes.
2082 // Do not ignore inlined scope even if it does not have any
2083 // variables or scopes.
Devang Patel7a6e5a32009-01-08 02:33:41 +00002084 if (StartID == EndID && StartID != 0) continue;
Devang Patel1be3ecc2009-04-15 00:10:26 +00002085 if (!Scope->isInlinedSubroutine()
Devang Patel0f7fef32009-04-13 17:02:03 +00002086 && Scope->getScopes().empty() && Scope->getVariables().empty())
2087 continue;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002088
2089 if (StartID == ParentStartID && EndID == ParentEndID) {
2090 // Just add stuff to the parent scope.
2091 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2092 } else {
Devang Patel1be3ecc2009-04-15 00:10:26 +00002093 DIE *ScopeDie = NULL;
2094 if (MainCU && TAI->doesDwarfUsesInlineInfoSection()) {
2095 ScopeDie = new DIE(DW_TAG_inlined_subroutine);
2096 DIE *Origin = MainCU->getDieMapSlotFor(Scope->getDesc().getGV());
2097 AddDIEntry(ScopeDie, DW_AT_abstract_origin, DW_FORM_ref4, Origin);
2098 AddUInt(ScopeDie, DW_AT_call_file, 0, Scope->getFile());
2099 AddUInt(ScopeDie, DW_AT_call_line, 0, Scope->getLine());
2100 AddUInt(ScopeDie, DW_AT_call_column, 0, Scope->getColumn());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002101 }
Devang Patel1be3ecc2009-04-15 00:10:26 +00002102 else
2103 ScopeDie = new DIE(DW_TAG_lexical_block);
2104
2105 // Add the scope bounds.
2106 if (StartID) {
2107 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2108 DWLabel("label", StartID));
2109 } else {
2110 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2111 DWLabel("func_begin", SubprogramCount));
2112 }
2113 if (EndID) {
2114 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2115 DWLabel("label", EndID));
2116 } else {
2117 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2118 DWLabel("func_end", SubprogramCount));
2119 }
2120
2121 // Add the scope contents.
2122 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2123 ParentDie->AddChild(ScopeDie);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002124 }
2125 }
2126 }
2127
2128 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2129 ///
2130 void ConstructRootDbgScope(DbgScope *RootScope) {
2131 // Exit if there is no root scope.
2132 if (!RootScope) return;
Devang Patel0e5200f2009-01-15 18:25:17 +00002133 DIDescriptor Desc = RootScope->getDesc();
2134 if (Desc.isNull())
2135 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002136
2137 // Get the subprogram debug information entry.
Devang Patel0e5200f2009-01-15 18:25:17 +00002138 DISubprogram SPD(Desc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002139
2140 // Get the compile unit context.
Devang Pateldd9db662009-01-30 18:20:31 +00002141 CompileUnit *Unit = MainCU;
2142 if (!Unit)
2143 Unit = FindCompileUnit(SPD.getCompileUnit());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002144
2145 // Get the subprogram die.
Devang Patelf6bac3e2009-01-12 22:58:14 +00002146 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002147 assert(SPDie && "Missing subprogram descriptor");
2148
2149 // Add the function bounds.
2150 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2151 DWLabel("func_begin", SubprogramCount));
2152 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2153 DWLabel("func_end", SubprogramCount));
2154 MachineLocation Location(RI->getFrameRegister(*MF));
2155 AddAddress(SPDie, DW_AT_frame_base, Location);
2156
2157 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2158 }
2159
2160 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2161 ///
2162 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Chenge3d42322009-02-25 07:04:34 +00002163 const char *FnName = MF->getFunction()->getNameStart();
2164 if (MainCU) {
Bill Wendling972bbac2009-04-09 21:49:15 +00002165 StringMap<DIE*> &Globals = MainCU->getGlobals();
2166 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Chenge3d42322009-02-25 07:04:34 +00002167 if (GI != Globals.end()) {
2168 DIE *SPDie = GI->second;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002169
2170 // Add the function bounds.
2171 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2172 DWLabel("func_begin", SubprogramCount));
2173 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2174 DWLabel("func_end", SubprogramCount));
2175
2176 MachineLocation Location(RI->getFrameRegister(*MF));
2177 AddAddress(SPDie, DW_AT_frame_base, Location);
2178 return;
2179 }
Evan Chenge3d42322009-02-25 07:04:34 +00002180 } else {
2181 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2182 CompileUnit *Unit = CompileUnits[i];
Bill Wendling972bbac2009-04-09 21:49:15 +00002183 StringMap<DIE*> &Globals = Unit->getGlobals();
2184 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Chenge3d42322009-02-25 07:04:34 +00002185 if (GI != Globals.end()) {
2186 DIE *SPDie = GI->second;
2187
2188 // Add the function bounds.
2189 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2190 DWLabel("func_begin", SubprogramCount));
2191 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2192 DWLabel("func_end", SubprogramCount));
2193
2194 MachineLocation Location(RI->getFrameRegister(*MF));
2195 AddAddress(SPDie, DW_AT_frame_base, Location);
2196 return;
2197 }
2198 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00002199 }
Evan Chenge3d42322009-02-25 07:04:34 +00002200
Devang Patel7a6e5a32009-01-08 02:33:41 +00002201#if 0
2202 // FIXME: This is causing an abort because C++ mangled names are compared
2203 // with their unmangled counterparts. See PR2885. Don't do this assert.
2204 assert(0 && "Couldn't find DIE for machine function!");
2205#endif
Evan Chenge3d42322009-02-25 07:04:34 +00002206 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002207 }
2208
Jim Laskeyef42a012006-11-02 20:12:39 +00002209 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2210 /// tools to recognize the object file contains Dwarf information.
2211 void EmitInitial() {
2212 // Check to see if we already emitted intial headers.
2213 if (didInitial) return;
2214 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002215
Jim Laskeyef42a012006-11-02 20:12:39 +00002216 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002217 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002218 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002219 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002220 }
2221 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2222 EmitLabel("section_info", 0);
2223 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2224 EmitLabel("section_abbrev", 0);
2225 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2226 EmitLabel("section_aranges", 0);
Scott Michel210de722009-01-26 22:32:51 +00002227 if (TAI->doesSupportMacInfoSection()) {
2228 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2229 EmitLabel("section_macinfo", 0);
2230 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002231 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2232 EmitLabel("section_line", 0);
2233 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2234 EmitLabel("section_loc", 0);
2235 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2236 EmitLabel("section_pubnames", 0);
2237 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2238 EmitLabel("section_str", 0);
2239 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2240 EmitLabel("section_ranges", 0);
2241
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002242 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002243 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002244 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002245 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002246 }
2247
Jim Laskey65195462006-10-30 13:35:07 +00002248 /// EmitDIE - Recusively Emits a debug information entry.
2249 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002250 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002251 // Get the abbreviation for this DIE.
2252 unsigned AbbrevNumber = Die->getAbbrevNumber();
2253 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002254
Jim Laskeybacd3042007-02-21 22:48:45 +00002255 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002256
2257 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002258 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002259
Evan Cheng42bf74b2009-03-25 01:47:28 +00002260 if (Asm->isVerbose())
Evan Cheng6547e402008-07-01 23:18:29 +00002261 Asm->EOL(std::string("Abbrev [" +
2262 utostr(AbbrevNumber) +
2263 "] 0x" + utohexstr(Die->getOffset()) +
2264 ":0x" + utohexstr(Die->getSize()) + " " +
2265 TagString(Abbrev->getTag())));
2266 else
2267 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002268
Owen Anderson873e1b52008-06-24 21:44:59 +00002269 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2270 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002271
Jim Laskeyef42a012006-11-02 20:12:39 +00002272 // Emit the DIE attribute values.
2273 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2274 unsigned Attr = AbbrevData[i].getAttribute();
2275 unsigned Form = AbbrevData[i].getForm();
2276 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002277
Jim Laskeyef42a012006-11-02 20:12:39 +00002278 switch (Attr) {
2279 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002280 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002281 break;
2282 }
2283 default: {
2284 // Emit an attribute using the defined form.
2285 Values[i]->EmitValue(*this, Form);
2286 break;
2287 }
2288 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002289
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002290 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002291 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002292
Jim Laskeyef42a012006-11-02 20:12:39 +00002293 // Emit the DIE children if any.
2294 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2295 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002296
Jim Laskeyef42a012006-11-02 20:12:39 +00002297 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2298 EmitDIE(Children[j]);
2299 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002300
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002301 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002302 }
2303 }
2304
Jim Laskey65195462006-10-30 13:35:07 +00002305 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2306 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002307 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2308 // Get the children.
2309 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002310
Jim Laskeyef42a012006-11-02 20:12:39 +00002311 // If not last sibling and has children then add sibling offset attribute.
2312 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2313
2314 // Record the abbreviation.
2315 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002316
Jim Laskeyef42a012006-11-02 20:12:39 +00002317 // Get the abbreviation for this DIE.
2318 unsigned AbbrevNumber = Die->getAbbrevNumber();
2319 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2320
2321 // Set DIE offset
2322 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002323
Jim Laskeyef42a012006-11-02 20:12:39 +00002324 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002325 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2326
Owen Anderson873e1b52008-06-24 21:44:59 +00002327 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2328 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002329
2330 // Size the DIE attribute values.
2331 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2332 // Size attribute value.
2333 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2334 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002335
Jim Laskeyef42a012006-11-02 20:12:39 +00002336 // Size the DIE children if any.
2337 if (!Children.empty()) {
2338 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2339 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002340
Jim Laskeyef42a012006-11-02 20:12:39 +00002341 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2342 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2343 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002344
Jim Laskeyef42a012006-11-02 20:12:39 +00002345 // End of children marker.
2346 Offset += sizeof(int8_t);
2347 }
2348
2349 Die->setSize(Offset - Die->getOffset());
2350 return Offset;
2351 }
Jim Laskey65195462006-10-30 13:35:07 +00002352
2353 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2354 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002355 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002356 // Process base compile unit.
Devang Pateldd9db662009-01-30 18:20:31 +00002357 if (MainCU) {
2358 // Compute size of compile unit header
2359 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2360 sizeof(int16_t) + // DWARF version number
2361 sizeof(int32_t) + // Offset Into Abbrev. Section
2362 sizeof(int8_t); // Pointer Size (in bytes)
2363 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
2364 return;
2365 }
Evan Chenge3d42322009-02-25 07:04:34 +00002366 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2367 CompileUnit *Unit = CompileUnits[i];
Devang Patel72b66352009-01-12 23:05:55 +00002368 // Compute size of compile unit header
2369 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2370 sizeof(int16_t) + // DWARF version number
2371 sizeof(int32_t) + // Offset Into Abbrev. Section
2372 sizeof(int8_t); // Pointer Size (in bytes)
2373 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2374 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002375 }
2376
Evan Chenge3d42322009-02-25 07:04:34 +00002377 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Jim Laskey65195462006-10-30 13:35:07 +00002378 ///
Evan Chenge3d42322009-02-25 07:04:34 +00002379 void EmitDebugInfoPerCU(CompileUnit *Unit) {
2380 DIE *Die = Unit->getDie();
2381 // Emit the compile units header.
2382 EmitLabel("info_begin", Unit->getID());
2383 // Emit size of content not including length itself
2384 unsigned ContentSize = Die->getSize() +
2385 sizeof(int16_t) + // DWARF version number
2386 sizeof(int32_t) + // Offset Into Abbrev. Section
2387 sizeof(int8_t) + // Pointer Size (in bytes)
2388 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2389
2390 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2391 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2392 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2393 Asm->EOL("Offset Into Abbrev. Section");
2394 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2395
2396 EmitDIE(Die);
2397 // FIXME - extra padding for gdb bug.
2398 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2399 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2400 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2401 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2402 EmitLabel("info_end", Unit->getID());
2403
2404 Asm->EOL();
2405 }
2406
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002407 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002408 // Start debug info section.
2409 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002410
Evan Chenge3d42322009-02-25 07:04:34 +00002411 if (MainCU) {
2412 EmitDebugInfoPerCU(MainCU);
2413 return;
Devang Patel72b66352009-01-12 23:05:55 +00002414 }
Evan Chenge3d42322009-02-25 07:04:34 +00002415
2416 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2417 EmitDebugInfoPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002418 }
2419
Jim Laskey65195462006-10-30 13:35:07 +00002420 /// EmitAbbreviations - Emit the abbreviation section.
2421 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002422 void EmitAbbreviations() const {
2423 // Check to see if it is worth the effort.
2424 if (!Abbreviations.empty()) {
2425 // Start the debug abbrev section.
2426 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002427
Jim Laskeyef42a012006-11-02 20:12:39 +00002428 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002429
Jim Laskeyef42a012006-11-02 20:12:39 +00002430 // For each abbrevation.
2431 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2432 // Get abbreviation data
2433 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002434
Jim Laskeyef42a012006-11-02 20:12:39 +00002435 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002436 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2437 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002438
Jim Laskeyef42a012006-11-02 20:12:39 +00002439 // Emit the abbreviations data.
2440 Abbrev->Emit(*this);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002441
Jim Laskeybacd3042007-02-21 22:48:45 +00002442 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002443 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002444
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002445 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00002446 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002447
Jim Laskeyef42a012006-11-02 20:12:39 +00002448 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002449
Jim Laskeybacd3042007-02-21 22:48:45 +00002450 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002451 }
2452 }
2453
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002454 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2455 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002456 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002457 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2458 // Define last address of section.
2459 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2460 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2461 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2462 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2463
2464 // Mark end of matrix.
2465 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2466 Asm->EmitULEB128Bytes(1); Asm->EOL();
2467 Asm->EmitInt8(1); Asm->EOL();
2468 }
2469
Jim Laskey65195462006-10-30 13:35:07 +00002470 /// EmitDebugLines - Emit source line information.
2471 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002472 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002473 // If the target is using .loc/.file, the assembler will be emitting the
2474 // .debug_line table automatically.
2475 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00002476 return;
2477
Jim Laskeyef42a012006-11-02 20:12:39 +00002478 // Minimum line delta, thus ranging from -10..(255-10).
2479 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2480 // Maximum line delta, thus ranging from -10..(255-10).
2481 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002482
Jim Laskeyef42a012006-11-02 20:12:39 +00002483 // Start the dwarf line section.
2484 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002485
Jim Laskeyef42a012006-11-02 20:12:39 +00002486 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002487
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002488 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002489 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002490 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002491
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002492 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002493
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002494 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002495 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002496 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002497
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002498 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002499
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002500 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002501
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002502 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002503
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002504 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002505
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002506 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002507
Jim Laskeyef42a012006-11-02 20:12:39 +00002508 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002509 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2510 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2511 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2512 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2513 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2514 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2515 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2516 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2517 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00002518
Jim Laskeyef42a012006-11-02 20:12:39 +00002519 // Emit directories.
Evan Chenge3d42322009-02-25 07:04:34 +00002520 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2521 Asm->EmitString(getSourceDirectoryName(DI));
2522 Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00002523 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002524 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002525
Jim Laskeyef42a012006-11-02 20:12:39 +00002526 // Emit files.
Evan Chenge3d42322009-02-25 07:04:34 +00002527 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2528 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002529 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Evan Chenge3d42322009-02-25 07:04:34 +00002530 Asm->EmitString(getSourceFileName(Id.second));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002531 Asm->EOL("Source");
Evan Chenge3d42322009-02-25 07:04:34 +00002532 Asm->EmitULEB128Bytes(Id.first);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002533 Asm->EOL("Directory #");
2534 Asm->EmitULEB128Bytes(0);
2535 Asm->EOL("Mod date");
2536 Asm->EmitULEB128Bytes(0);
2537 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002538 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002539 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002540
Jim Laskeyef42a012006-11-02 20:12:39 +00002541 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002542
Jim Laskeyef42a012006-11-02 20:12:39 +00002543 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002544 unsigned SecSrcLinesSize = SectionSourceLines.size();
2545
2546 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002547 // Isolate current sections line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00002548 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00002549
Evan Cheng42bf74b2009-03-25 01:47:28 +00002550 if (Asm->isVerbose()) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002551 const Section* S = SectionMap[j + 1];
Evan Chenge3d42322009-02-25 07:04:34 +00002552 O << '\t' << TAI->getCommentString() << " Section"
2553 << S->getName() << '\n';
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002554 } else
Evan Cheng6547e402008-07-01 23:18:29 +00002555 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002556
2557 // Dwarf assumes we start with first line of first source file.
2558 unsigned Source = 1;
2559 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002560
Jim Laskeyef42a012006-11-02 20:12:39 +00002561 // Construct rows of the address, source, line, column matrix.
2562 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patelf3ee5142009-01-12 22:54:42 +00002563 const SrcLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002564 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00002565 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002566
Evan Cheng42bf74b2009-03-25 01:47:28 +00002567 if (!Asm->isVerbose())
Evan Cheng6547e402008-07-01 23:18:29 +00002568 Asm->EOL();
Evan Chenge3d42322009-02-25 07:04:34 +00002569 else {
2570 std::pair<unsigned, unsigned> SourceID =
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002571 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Evan Chenge3d42322009-02-25 07:04:34 +00002572 O << '\t' << TAI->getCommentString() << ' '
2573 << getSourceDirectoryName(SourceID.first) << ' '
2574 << getSourceFileName(SourceID.second)
2575 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2576 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002577
2578 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002579 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00002580 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002581 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00002582 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002583
Jim Laskeyef42a012006-11-02 20:12:39 +00002584 // If change of source, then switch to the new source.
2585 if (Source != LineInfo.getSourceID()) {
2586 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002587 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2588 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00002589 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002590
Jim Laskeyef42a012006-11-02 20:12:39 +00002591 // If change of line.
2592 if (Line != LineInfo.getLine()) {
2593 // Determine offset.
2594 int Offset = LineInfo.getLine() - Line;
2595 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002596
Jim Laskeyef42a012006-11-02 20:12:39 +00002597 // Update line.
2598 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002599
Jim Laskeyef42a012006-11-02 20:12:39 +00002600 // If delta is small enough and in range...
2601 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2602 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002603 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00002604 } else {
2605 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002606 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2607 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2608 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002609 }
2610 } else {
2611 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002612 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002613 }
2614 }
2615
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002616 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002617 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002618
2619 if (SecSrcLinesSize == 0)
2620 // Because we're emitting a debug_line section, we still need a line
2621 // table. The linker and friends expect it to exist. If there's nothing to
2622 // put into it, emit an empty table.
2623 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002624
Jim Laskeyef42a012006-11-02 20:12:39 +00002625 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002626
Jim Laskeybacd3042007-02-21 22:48:45 +00002627 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002628 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002629
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002630 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00002631 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002632 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002633 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00002634 return;
2635
2636 int stackGrowth =
2637 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2638 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002639 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00002640
2641 // Start the dwarf frame section.
2642 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2643
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002644 EmitLabel("debug_frame_common", 0);
2645 EmitDifference("debug_frame_common_end", 0,
2646 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002647 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002648
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002649 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002650 Asm->EmitInt32((int)DW_CIE_ID);
2651 Asm->EOL("CIE Identifier Tag");
2652 Asm->EmitInt8(DW_CIE_VERSION);
2653 Asm->EOL("CIE Version");
2654 Asm->EmitString("");
2655 Asm->EOL("CIE Augmentation");
2656 Asm->EmitULEB128Bytes(1);
2657 Asm->EOL("CIE Code Alignment Factor");
2658 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002659 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002660 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002661 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002662
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002663 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002664 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002665
Dale Johannesenb97aec62007-11-13 19:13:01 +00002666 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002667
Evan Cheng05548eb2008-02-29 19:36:59 +00002668 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002669 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002670
Jim Laskeybacd3042007-02-21 22:48:45 +00002671 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002672 }
2673
Jim Laskey65195462006-10-30 13:35:07 +00002674 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2675 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002676 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002677 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00002678 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002679
Jim Laskeyef42a012006-11-02 20:12:39 +00002680 // Start the dwarf frame section.
2681 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002682
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002683 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2684 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002685 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002686
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002687 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002688
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002689 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2690 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002691 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002692
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002693 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002694 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002695 EmitDifference("func_end", DebugFrameInfo.Number,
2696 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002697 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002698
Devang Patel5aac3d32009-01-17 08:01:33 +00002699 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00002700 false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002701
Evan Cheng05548eb2008-02-29 19:36:59 +00002702 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002703 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00002704
Jim Laskeybacd3042007-02-21 22:48:45 +00002705 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002706 }
2707
Evan Chenge3d42322009-02-25 07:04:34 +00002708 void EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2709 EmitDifference("pubnames_end", Unit->getID(),
2710 "pubnames_begin", Unit->getID(), true);
2711 Asm->EOL("Length of Public Names Info");
2712
2713 EmitLabel("pubnames_begin", Unit->getID());
2714
2715 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2716
2717 EmitSectionOffset("info_begin", "section_info",
2718 Unit->getID(), 0, true, false);
2719 Asm->EOL("Offset of Compilation Unit Info");
2720
2721 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2722 true);
2723 Asm->EOL("Compilation Unit Length");
2724
Bill Wendling972bbac2009-04-09 21:49:15 +00002725 StringMap<DIE*> &Globals = Unit->getGlobals();
Bill Wendlingf34be822009-04-09 23:51:31 +00002726 for (StringMap<DIE*>::const_iterator
Bill Wendling972bbac2009-04-09 21:49:15 +00002727 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
Bill Wendlingf34be822009-04-09 23:51:31 +00002728 const char *Name = GI->getKeyData();
Evan Chenge3d42322009-02-25 07:04:34 +00002729 DIE * Entity = GI->second;
2730
2731 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
Bill Wendlingf34be822009-04-09 23:51:31 +00002732 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
Evan Chenge3d42322009-02-25 07:04:34 +00002733 }
2734
2735 Asm->EmitInt32(0); Asm->EOL("End Mark");
2736 EmitLabel("pubnames_end", Unit->getID());
2737
2738 Asm->EOL();
2739 }
2740
Jim Laskeyef42a012006-11-02 20:12:39 +00002741 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002742 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002743 void EmitDebugPubNames() {
2744 // Start the dwarf pubnames section.
2745 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002746
Evan Chenge3d42322009-02-25 07:04:34 +00002747 if (MainCU) {
2748 EmitDebugPubNamesPerCU(MainCU);
2749 return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002750 }
Evan Chenge3d42322009-02-25 07:04:34 +00002751
2752 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2753 EmitDebugPubNamesPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002754 }
2755
2756 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002757 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002758 void EmitDebugStr() {
2759 // Check to see if it is worth the effort.
2760 if (!StringPool.empty()) {
2761 // Start the dwarf str section.
2762 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002763
Jim Laskeyef42a012006-11-02 20:12:39 +00002764 // For each of strings in the string pool.
2765 for (unsigned StringID = 1, N = StringPool.size();
2766 StringID <= N; ++StringID) {
2767 // Emit a label for reference from debug information entries.
2768 EmitLabel("string", StringID);
2769 // Emit the string itself.
2770 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00002771 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002772 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002773
Jim Laskeybacd3042007-02-21 22:48:45 +00002774 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002775 }
2776 }
2777
2778 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002779 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002780 void EmitDebugLoc() {
2781 // Start the dwarf loc section.
2782 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002783
Jim Laskeybacd3042007-02-21 22:48:45 +00002784 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002785 }
2786
2787 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002788 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002789 void EmitDebugARanges() {
2790 // Start the dwarf aranges section.
2791 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002792
Jim Laskeyef42a012006-11-02 20:12:39 +00002793 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00002794#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002795 CompileUnit *Unit = GetBaseCompileUnit();
2796
Jim Laskey5496f012006-11-09 14:52:14 +00002797 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002798 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002799
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002800 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002801
Jim Laskey5496f012006-11-09 14:52:14 +00002802 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002803 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002804
Dan Gohman82482942007-09-27 23:12:31 +00002805 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002806
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002807 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002808
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002809 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2810 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002811
Jim Laskey5496f012006-11-09 14:52:14 +00002812 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002813 EmitReference("text_begin", 0); Asm->EOL("Address");
2814 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002815
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002816 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2817 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00002818#endif
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002819
Jim Laskeybacd3042007-02-21 22:48:45 +00002820 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002821 }
2822
2823 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002824 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002825 void EmitDebugRanges() {
2826 // Start the dwarf ranges section.
2827 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002828
Jim Laskeybacd3042007-02-21 22:48:45 +00002829 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002830 }
2831
2832 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002833 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002834 void EmitDebugMacInfo() {
Scott Michel210de722009-01-26 22:32:51 +00002835 if (TAI->doesSupportMacInfoSection()) {
2836 // Start the dwarf macinfo section.
2837 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002838
Scott Michel210de722009-01-26 22:32:51 +00002839 Asm->EOL();
2840 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002841 }
2842
Devang Patel0f7fef32009-04-13 17:02:03 +00002843 /// EmitDebugInlineInfo - Emit inline info using following format.
2844 /// Section Header:
2845 /// 1. length of section
2846 /// 2. Dwarf version number
2847 /// 3. address size.
2848 ///
2849 /// Entries (one "entry" for each function that was inlined):
2850 ///
2851 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
2852 /// otherwise offset into __debug_str for regular function name.
2853 /// 2. offset into __debug_str section for regular function name.
2854 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
2855 /// instances for the function.
2856 ///
2857 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
2858 /// inlined instance; the die_offset points to the inlined_subroutine die in
2859 /// the __debug_info section, and the low_pc is the starting address for the
2860 /// inlining instance.
2861 void EmitDebugInlineInfo() {
2862 if (!TAI->doesDwarfUsesInlineInfoSection())
2863 return;
2864
2865 if (!MainCU)
2866 return;
2867
2868 Asm->SwitchToDataSection(TAI->getDwarfDebugInlineSection());
2869 Asm->EOL();
2870 EmitDifference("debug_inlined_end", 1,
2871 "debug_inlined_begin", 1, true);
2872 Asm->EOL("Length of Debug Inlined Information Entry");
2873
2874 EmitLabel("debug_inlined_begin", 1);
2875
2876 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
2877 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2878
2879 for (DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
2880 I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2881 GlobalVariable *GV = I->first;
2882 SmallVector<unsigned, 4> &Labels = I->second;
2883 DISubprogram SP(GV);
2884 std::string Name;
2885 std::string LName;
2886
2887 SP.getLinkageName(LName);
2888 SP.getName(Name);
2889
2890 Asm->EmitString(LName.empty() ? Name : LName);
2891 Asm->EOL("MIPS linkage name");
2892
2893 Asm->EmitString(Name); Asm->EOL("Function name");
2894
2895 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2896
2897 for (SmallVector<unsigned, 4>::iterator LI = Labels.begin(),
2898 LE = Labels.end(); LI != LE; ++LI) {
2899 DIE *SP = MainCU->getDieMapSlotFor(GV);
2900 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2901
2902 if (TD->getPointerSize() == sizeof(int32_t))
2903 O << TAI->getData32bitsDirective();
2904 else
2905 O << TAI->getData64bitsDirective();
2906 PrintLabelName("label", *LI); Asm->EOL("low_pc");
2907 }
2908 }
2909
2910 EmitLabel("debug_inlined_end", 1);
2911 Asm->EOL();
2912 }
2913
Bill Wendlingc8615e42009-03-10 21:47:45 +00002914 /// GetOrCreateSourceID - Look up the source id with the given directory and
2915 /// source file names. If none currently exists, create a new id and insert it
2916 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
2917 /// as well.
2918 unsigned GetOrCreateSourceID(const std::string &DirName,
2919 const std::string &FileName) {
2920 unsigned DId;
2921 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
2922 if (DI != DirectoryIdMap.end()) {
2923 DId = DI->getValue();
2924 } else {
2925 DId = DirectoryNames.size() + 1;
2926 DirectoryIdMap[DirName] = DId;
2927 DirectoryNames.push_back(DirName);
2928 }
2929
2930 unsigned FId;
2931 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
2932 if (FI != SourceFileIdMap.end()) {
2933 FId = FI->getValue();
2934 } else {
2935 FId = SourceFileNames.size() + 1;
2936 SourceFileIdMap[FileName] = FId;
2937 SourceFileNames.push_back(FileName);
2938 }
2939
2940 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
2941 SourceIdMap.find(std::make_pair(DId, FId));
2942 if (SI != SourceIdMap.end())
2943 return SI->second;
2944
2945 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
2946 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
2947 SourceIds.push_back(std::make_pair(DId, FId));
2948
2949 return SrcId;
2950 }
2951
Evan Chenge3d42322009-02-25 07:04:34 +00002952 void ConstructCompileUnit(GlobalVariable *GV) {
2953 DICompileUnit DIUnit(GV);
Bill Wendling0582ae92009-03-13 04:39:26 +00002954 std::string Dir, FN, Prod;
2955 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
2956 DIUnit.getFilename(FN));
Evan Chenge3d42322009-02-25 07:04:34 +00002957
2958 DIE *Die = new DIE(DW_TAG_compile_unit);
2959 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2960 DWLabel("section_line", 0), DWLabel("section_line", 0),
2961 false);
Bill Wendling0582ae92009-03-13 04:39:26 +00002962 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer(Prod));
Evan Chenge3d42322009-02-25 07:04:34 +00002963 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002964 AddString(Die, DW_AT_name, DW_FORM_string, FN);
Bill Wendling0582ae92009-03-13 04:39:26 +00002965 if (!Dir.empty())
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002966 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Chenge3d42322009-02-25 07:04:34 +00002967 if (DIUnit.isOptimized())
2968 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendling0582ae92009-03-13 04:39:26 +00002969 std::string Flags;
2970 DIUnit.getFlags(Flags);
2971 if (!Flags.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00002972 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
2973 unsigned RVer = DIUnit.getRunTimeVersion();
2974 if (RVer)
2975 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
2976
2977 CompileUnit *Unit = new CompileUnit(ID, Die);
2978 if (DIUnit.isMain()) {
2979 assert(!MainCU && "Multiple main compile units are found!");
2980 MainCU = Unit;
2981 }
2982 CompileUnitMap[DIUnit.getGV()] = Unit;
2983 CompileUnits.push_back(Unit);
2984 }
2985
Devang Patelc4523242009-01-05 23:11:11 +00002986 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Pateld1ca9252009-01-05 23:03:32 +00002987 void ConstructCompileUnits() {
Evan Chenge3d42322009-02-25 07:04:34 +00002988 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.compile_units");
2989 if (!Root)
2990 return;
2991 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2992 "Malformed compile unit descriptor anchor type");
2993 Constant *RootC = cast<Constant>(*Root->use_begin());
2994 assert(RootC->hasNUsesOrMore(1) &&
2995 "Malformed compile unit descriptor anchor type");
2996 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2997 UI != UE; ++UI)
2998 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2999 UUI != UUE; ++UUI) {
3000 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3001 ConstructCompileUnit(GV);
Devang Pateldd9db662009-01-30 18:20:31 +00003002 }
Evan Chenge3d42322009-02-25 07:04:34 +00003003 }
3004
3005 bool ConstructGlobalVariableDIE(GlobalVariable *GV) {
3006 DIGlobalVariable DI_GV(GV);
3007 CompileUnit *DW_Unit = MainCU;
3008 if (!DW_Unit)
3009 DW_Unit = FindCompileUnit(DI_GV.getCompileUnit());
3010
3011 // Check for pre-existence.
3012 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
3013 if (Slot)
3014 return false;
3015
3016 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
3017
3018 // Add address.
3019 DIEBlock *Block = new DIEBlock();
3020 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Bill Wendling7d16e852009-04-10 00:12:49 +00003021 std::string GLN;
Evan Chenge3d42322009-02-25 07:04:34 +00003022 AddObjectLabel(Block, 0, DW_FORM_udata,
Bill Wendling7d16e852009-04-10 00:12:49 +00003023 Asm->getGlobalLinkName(DI_GV.getGlobal(), GLN));
Evan Chenge3d42322009-02-25 07:04:34 +00003024 AddBlock(VariableDie, DW_AT_location, 0, Block);
3025
3026 // Add to map.
3027 Slot = VariableDie;
3028 // Add to context owner.
3029 DW_Unit->getDie()->AddChild(VariableDie);
3030 // Expose as global. FIXME - need to check external flag.
Bill Wendling0582ae92009-03-13 04:39:26 +00003031 std::string Name;
3032 DW_Unit->AddGlobal(DI_GV.getName(Name), VariableDie);
Evan Chenge3d42322009-02-25 07:04:34 +00003033 return true;
Devang Pateld1ca9252009-01-05 23:03:32 +00003034 }
3035
Devang Patelc4523242009-01-05 23:11:11 +00003036 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patel53cac182009-02-24 00:02:15 +00003037 /// visible global variables. Return true if at least one global DIE is
3038 /// created.
3039 bool ConstructGlobalVariableDIEs() {
Evan Chenge3d42322009-02-25 07:04:34 +00003040 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
3041 if (!Root)
3042 return false;
Devang Patelc4523242009-01-05 23:11:11 +00003043
Evan Chenge3d42322009-02-25 07:04:34 +00003044 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3045 "Malformed global variable descriptor anchor type");
3046 Constant *RootC = cast<Constant>(*Root->use_begin());
3047 assert(RootC->hasNUsesOrMore(1) &&
3048 "Malformed global variable descriptor anchor type");
Devang Patelc4523242009-01-05 23:11:11 +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 |= ConstructGlobalVariableDIE(GV);
3057 }
3058 return Result;
3059 }
Devang Patelc4523242009-01-05 23:11:11 +00003060
Evan Chenge3d42322009-02-25 07:04:34 +00003061 bool ConstructSubprogram(GlobalVariable *GV) {
3062 DISubprogram SP(GV);
3063 CompileUnit *Unit = MainCU;
3064 if (!Unit)
3065 Unit = FindCompileUnit(SP.getCompileUnit());
Devang Patelc4523242009-01-05 23:11:11 +00003066
Evan Chenge3d42322009-02-25 07:04:34 +00003067 // Check for pre-existence.
3068 DIE *&Slot = Unit->getDieMapSlotFor(GV);
3069 if (Slot)
3070 return false;
3071
3072 if (!SP.isDefinition())
3073 // This is a method declaration which will be handled while
3074 // constructing class type.
3075 return false;
3076
3077 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
3078
3079 // Add to map.
3080 Slot = SubprogramDie;
3081 // Add to context owner.
3082 Unit->getDie()->AddChild(SubprogramDie);
3083 // Expose as global.
Bill Wendling0582ae92009-03-13 04:39:26 +00003084 std::string Name;
3085 Unit->AddGlobal(SP.getName(Name), SubprogramDie);
Evan Chenge3d42322009-02-25 07:04:34 +00003086 return true;
Devang Patelc4523242009-01-05 23:11:11 +00003087 }
3088
Devang Patel78eb6ad2009-01-05 23:21:35 +00003089 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patel53cac182009-02-24 00:02:15 +00003090 /// subprograms. Return true if at least one subprogram DIE is created.
3091 bool ConstructSubprograms() {
Evan Chenge3d42322009-02-25 07:04:34 +00003092 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
3093 if (!Root)
3094 return false;
Devang Patel78eb6ad2009-01-05 23:21:35 +00003095
Evan Chenge3d42322009-02-25 07:04:34 +00003096 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3097 "Malformed subprogram descriptor anchor type");
3098 Constant *RootC = cast<Constant>(*Root->use_begin());
3099 assert(RootC->hasNUsesOrMore(1) &&
3100 "Malformed subprogram descriptor anchor type");
Devang Patel78eb6ad2009-01-05 23:21:35 +00003101
Evan Chenge3d42322009-02-25 07:04:34 +00003102 bool Result = false;
3103 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3104 UI != UE; ++UI)
3105 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3106 UUI != UUE; ++UUI) {
3107 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3108 Result |= ConstructSubprogram(GV);
3109 }
3110 return Result;
Devang Patel78eb6ad2009-01-05 23:21:35 +00003111 }
3112
Jim Laskey65195462006-10-30 13:35:07 +00003113public:
Jim Laskeyef42a012006-11-02 20:12:39 +00003114 //===--------------------------------------------------------------------===//
3115 // Main entry points.
3116 //
Owen Andersoncb371882008-08-21 00:14:44 +00003117 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00003118 : Dwarf(OS, A, T, "dbg"), MainCU(0),
3119 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
3120 ValuesSet(InitValuesSetSize), Values(), StringPool(), SectionMap(),
3121 SectionSourceLines(), didInitial(false), shouldEmit(false),
3122 RootDbgScope(0), DebugTimer(0) {
3123 if (TimePassesIsEnabled)
3124 DebugTimer = new Timer("Dwarf Debug Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00003125 getDwarfTimerGroup());
Jim Laskeyef42a012006-11-02 20:12:39 +00003126 }
Jim Laskey072200c2007-01-29 18:51:14 +00003127 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003128 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3129 delete Values[j];
Bill Wendling163ba3f2009-03-10 21:23:25 +00003130
3131 delete DebugTimer;
Jim Laskeyef42a012006-11-02 20:12:39 +00003132 }
3133
Bill Wendlingc8615e42009-03-10 21:47:45 +00003134 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
3135 /// be emitted.
3136 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
3137
Devang Patel5d315982009-01-06 21:07:30 +00003138 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3139 /// This is inovked by the target AsmPrinter.
Devang Patel3f7833a2009-01-12 23:09:42 +00003140 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003141 if (TimePassesIsEnabled)
3142 DebugTimer->startTimer();
3143
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003144 // Create all the compile unit DIEs.
3145 ConstructCompileUnits();
Devang Patel3f7833a2009-01-12 23:09:42 +00003146
Bill Wendling163ba3f2009-03-10 21:23:25 +00003147 if (CompileUnits.empty()) {
3148 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003149 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003150
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003151 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003152 }
Devang Patel3f7833a2009-01-12 23:09:42 +00003153
Devang Patel53cac182009-02-24 00:02:15 +00003154 // Create DIEs for each of the externally visible global variables.
3155 bool globalDIEs = ConstructGlobalVariableDIEs();
3156
3157 // Create DIEs for each of the externally visible subprograms.
3158 bool subprogramDIEs = ConstructSubprograms();
3159
3160 // If there is not any debug info available for any global variables
3161 // and any subprograms then there is not any debug info to emit.
Bill Wendling163ba3f2009-03-10 21:23:25 +00003162 if (!globalDIEs && !subprogramDIEs) {
3163 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003164 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003165
Devang Patel53cac182009-02-24 00:02:15 +00003166 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003167 }
Devang Patel53cac182009-02-24 00:02:15 +00003168
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003169 MMI = mmi;
3170 shouldEmit = true;
3171 MMI->setDebugInfoAvailability(true);
Devang Patel5d315982009-01-06 21:07:30 +00003172
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003173 // Prime section data.
3174 SectionMap.insert(TAI->getTextSection());
Devang Patel5d315982009-01-06 21:07:30 +00003175
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003176 // Print out .file directives to specify files for .loc directives. These
3177 // are printed out early so that they precede any .loc directives.
3178 if (TAI->hasDotLocAndDotFile()) {
Evan Chenge3d42322009-02-25 07:04:34 +00003179 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
3180 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00003181 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Evan Chenge3d42322009-02-25 07:04:34 +00003182 sys::Path FullPath(getSourceDirectoryName(Id.first));
3183 bool AppendOk =
3184 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003185 assert(AppendOk && "Could not append filename to directory!");
3186 AppendOk = false;
3187 Asm->EmitFile(i, FullPath.toString());
3188 Asm->EOL();
Devang Patel5d315982009-01-06 21:07:30 +00003189 }
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003190 }
Devang Patel5d315982009-01-06 21:07:30 +00003191
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003192 // Emit initial sections
3193 EmitInitial();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003194
3195 if (TimePassesIsEnabled)
3196 DebugTimer->stopTimer();
Devang Patel5d315982009-01-06 21:07:30 +00003197 }
3198
Jim Laskey65195462006-10-30 13:35:07 +00003199 /// BeginModule - Emit all Dwarf sections that should come prior to the
3200 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00003201 void BeginModule(Module *M) {
3202 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00003203 }
3204
Jim Laskey65195462006-10-30 13:35:07 +00003205 /// EndModule - Emit all Dwarf sections that should come after the content.
3206 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003207 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003208 if (!ShouldEmitDwarfDebug())
3209 return;
3210
3211 if (TimePassesIsEnabled)
3212 DebugTimer->startTimer();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003213
Jim Laskeyef42a012006-11-02 20:12:39 +00003214 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003215 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003216 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00003217 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003218 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003219
Jim Laskeyef42a012006-11-02 20:12:39 +00003220 // End text sections.
3221 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003222 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00003223 EmitLabel("section_end", i);
3224 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003225
3226 // Emit common frame information.
3227 EmitCommonDebugFrame();
3228
3229 // Emit function debug frame information
3230 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3231 E = DebugFrames.end(); I != E; ++I)
3232 EmitFunctionDebugFrame(*I);
3233
Jim Laskeyef42a012006-11-02 20:12:39 +00003234 // Compute DIE offsets and sizes.
3235 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003236
Jim Laskeyef42a012006-11-02 20:12:39 +00003237 // Emit all the DIEs into a debug info section
3238 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003239
Jim Laskeyef42a012006-11-02 20:12:39 +00003240 // Corresponding abbreviations into a abbrev section.
3241 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003242
Jim Laskeyef42a012006-11-02 20:12:39 +00003243 // Emit source line correspondence into a debug line section.
3244 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003245
Jim Laskeyef42a012006-11-02 20:12:39 +00003246 // Emit info into a debug pubnames section.
3247 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003248
Jim Laskeyef42a012006-11-02 20:12:39 +00003249 // Emit info into a debug str section.
3250 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003251
Jim Laskeyef42a012006-11-02 20:12:39 +00003252 // Emit info into a debug loc section.
3253 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003254
Jim Laskeyef42a012006-11-02 20:12:39 +00003255 // Emit info into a debug aranges section.
3256 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003257
Jim Laskeyef42a012006-11-02 20:12:39 +00003258 // Emit info into a debug ranges section.
3259 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003260
Jim Laskeyef42a012006-11-02 20:12:39 +00003261 // Emit info into a debug macinfo section.
3262 EmitDebugMacInfo();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003263
Devang Patel0f7fef32009-04-13 17:02:03 +00003264 // Emit inline info.
3265 EmitDebugInlineInfo();
3266
Bill Wendling163ba3f2009-03-10 21:23:25 +00003267 if (TimePassesIsEnabled)
3268 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003269 }
3270
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003271 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00003272 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00003273 void BeginFunction(MachineFunction *MF) {
Bill Wendling1362f972009-03-11 00:03:50 +00003274 this->MF = MF;
3275
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003276 if (!ShouldEmitDwarfDebug()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00003277
Bill Wendling163ba3f2009-03-10 21:23:25 +00003278 if (TimePassesIsEnabled)
3279 DebugTimer->startTimer();
3280
Jim Laskeyef42a012006-11-02 20:12:39 +00003281 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003282 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003283
Jim Laskeyef42a012006-11-02 20:12:39 +00003284 // Assumes in correct section after the entry point.
3285 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00003286
3287 // Emit label for the implicitly defined dbg.stoppoint at the start of
3288 // the function.
Devang Patelf3ee5142009-01-12 22:54:42 +00003289 if (!Lines.empty()) {
3290 const SrcLineInfo &LineInfo = Lines[0];
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00003291 Asm->printLabel(LineInfo.getLabelID());
3292 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003293
3294 if (TimePassesIsEnabled)
3295 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003296 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003297
Jim Laskey65195462006-10-30 13:35:07 +00003298 /// EndFunction - Gather and emit post-function debug information.
3299 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00003300 void EndFunction(MachineFunction *MF) {
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003301 if (!ShouldEmitDwarfDebug()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003302
Bill Wendling163ba3f2009-03-10 21:23:25 +00003303 if (TimePassesIsEnabled)
3304 DebugTimer->startTimer();
3305
Jim Laskeyb82313f2007-02-01 16:31:34 +00003306 // Define end label for subprogram.
3307 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003308
Jim Laskeyef42a012006-11-02 20:12:39 +00003309 // Get function line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00003310 if (!Lines.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003311 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003312 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00003313 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patelf3ee5142009-01-12 22:54:42 +00003314 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Jim Laskeyef42a012006-11-02 20:12:39 +00003315 // Append the function info to section info.
3316 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patelf3ee5142009-01-12 22:54:42 +00003317 Lines.begin(), Lines.end());
Jim Laskeyef42a012006-11-02 20:12:39 +00003318 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003319
Jim Laskeyef42a012006-11-02 20:12:39 +00003320 // Construct scopes for subprogram.
Devang Patel7bb89ed2009-01-13 00:20:51 +00003321 if (RootDbgScope)
3322 ConstructRootDbgScope(RootDbgScope);
Bill Wendlingd751c642008-09-26 00:28:12 +00003323 else
3324 // FIXME: This is wrong. We are essentially getting past a problem with
3325 // debug information not being able to handle unreachable blocks that have
3326 // debug information in them. In particular, those unreachable blocks that
3327 // have "region end" info in them. That situation results in the "root
3328 // scope" not being created. If that's the case, then emit a "default"
3329 // scope, i.e., one that encompasses the whole function. This isn't
3330 // desirable. And a better way of handling this (and all of the debugging
3331 // information) needs to be explored.
Devang Patel7bb89ed2009-01-13 00:20:51 +00003332 ConstructDefaultDbgScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003333
3334 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3335 MMI->getFrameMoves()));
Devang Patel481ff5b2009-01-12 18:48:36 +00003336
3337 // Clear debug info
3338 if (RootDbgScope) {
3339 delete RootDbgScope;
3340 DbgScopeMap.clear();
Devang Patel1be3ecc2009-04-15 00:10:26 +00003341 DbgInlinedScopeMap.clear();
3342 InlinedVariableScopes.clear();
Devang Patel481ff5b2009-01-12 18:48:36 +00003343 RootDbgScope = NULL;
3344 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003345
Bill Wendling163ba3f2009-03-10 21:23:25 +00003346 Lines.clear();
3347
3348 if (TimePassesIsEnabled)
3349 DebugTimer->stopTimer();
3350 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003351
Devang Patelcf3a4482009-01-15 23:41:32 +00003352 /// ValidDebugInfo - Return true if V represents valid debug info value.
Devang Patel48c7fa22009-04-13 18:13:16 +00003353 bool ValidDebugInfo(Value *V, bool FastISel) {
Devang Patelb79b5352009-01-19 23:21:49 +00003354 if (!V)
3355 return false;
3356
Devang Patel61c6bf32009-01-16 01:49:46 +00003357 if (!shouldEmit)
3358 return false;
3359
Devang Patelcf3a4482009-01-15 23:41:32 +00003360 GlobalVariable *GV = getGlobalVariable(V);
3361 if (!GV)
3362 return false;
Duncan Sands667d4b82009-03-07 15:45:40 +00003363
3364 if (!GV->hasInternalLinkage () && !GV->hasLinkOnceLinkage())
Devang Patelcf3a4482009-01-15 23:41:32 +00003365 return false;
3366
Bill Wendling163ba3f2009-03-10 21:23:25 +00003367 if (TimePassesIsEnabled)
3368 DebugTimer->startTimer();
3369
Devang Patelcf3a4482009-01-15 23:41:32 +00003370 DIDescriptor DI(GV);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003371
Devang Patelcf3a4482009-01-15 23:41:32 +00003372 // Check current version. Allow Version6 for now.
3373 unsigned Version = DI.getVersion();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003374 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6) {
3375 if (TimePassesIsEnabled)
3376 DebugTimer->stopTimer();
3377
Devang Patelcf3a4482009-01-15 23:41:32 +00003378 return false;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003379 }
Devang Patelcf3a4482009-01-15 23:41:32 +00003380
Devang Patelb79b5352009-01-19 23:21:49 +00003381 unsigned Tag = DI.getTag();
3382 switch (Tag) {
3383 case DW_TAG_variable:
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003384 assert(DIVariable(GV).Verify() && "Invalid DebugInfo value");
Devang Patelb79b5352009-01-19 23:21:49 +00003385 break;
3386 case DW_TAG_compile_unit:
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003387 assert(DICompileUnit(GV).Verify() && "Invalid DebugInfo value");
Devang Patelb79b5352009-01-19 23:21:49 +00003388 break;
3389 case DW_TAG_subprogram:
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003390 assert(DISubprogram(GV).Verify() && "Invalid DebugInfo value");
Devang Patelb79b5352009-01-19 23:21:49 +00003391 break;
Devang Patel48c7fa22009-04-13 18:13:16 +00003392 case DW_TAG_lexical_block:
3393 /// FIXME. This interfers with the qualitfy of generated code when
3394 /// during optimization.
3395 if (FastISel == false)
3396 return false;
Devang Patelb79b5352009-01-19 23:21:49 +00003397 default:
3398 break;
3399 }
3400
Bill Wendling163ba3f2009-03-10 21:23:25 +00003401 if (TimePassesIsEnabled)
3402 DebugTimer->stopTimer();
3403
Devang Patelcf3a4482009-01-15 23:41:32 +00003404 return true;
3405 }
3406
Devang Patelccca7fe2009-01-12 19:17:34 +00003407 /// RecordSourceLine - Records location information and associates it with a
3408 /// label. Returns a unique label ID used to generate a label and provide
3409 /// correspondence to the source line list.
3410 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003411 if (TimePassesIsEnabled)
3412 DebugTimer->startTimer();
3413
Evan Chenge3d42322009-02-25 07:04:34 +00003414 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003415 assert(Unit && "Unable to find CompileUnit");
Devang Patelccca7fe2009-01-12 19:17:34 +00003416 unsigned ID = MMI->NextLabelID();
3417 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003418
3419 if (TimePassesIsEnabled)
3420 DebugTimer->stopTimer();
3421
Devang Patelccca7fe2009-01-12 19:17:34 +00003422 return ID;
3423 }
3424
3425 /// RecordSourceLine - Records location information and associates it with a
3426 /// label. Returns a unique label ID used to generate a label and provide
3427 /// correspondence to the source line list.
3428 unsigned RecordSourceLine(unsigned Line, unsigned Col, unsigned Src) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003429 if (TimePassesIsEnabled)
3430 DebugTimer->startTimer();
3431
Devang Patelccca7fe2009-01-12 19:17:34 +00003432 unsigned ID = MMI->NextLabelID();
3433 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003434
3435 if (TimePassesIsEnabled)
3436 DebugTimer->stopTimer();
3437
Devang Patelccca7fe2009-01-12 19:17:34 +00003438 return ID;
3439 }
3440
Bill Wendlingc8615e42009-03-10 21:47:45 +00003441 /// getRecordSourceLineCount - Return the number of source lines in the debug
3442 /// info.
3443 unsigned getRecordSourceLineCount() const {
Devang Patelccca7fe2009-01-12 19:17:34 +00003444 return Lines.size();
3445 }
3446
Bill Wendlingc8615e42009-03-10 21:47:45 +00003447 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
3448 /// timed. Look up the source id with the given directory and source file
3449 /// names. If none currently exists, create a new id and insert it in the
3450 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
3451 /// well.
Evan Chenge3d42322009-02-25 07:04:34 +00003452 unsigned getOrCreateSourceID(const std::string &DirName,
3453 const std::string &FileName) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003454 if (TimePassesIsEnabled)
3455 DebugTimer->startTimer();
3456
Bill Wendlingc8615e42009-03-10 21:47:45 +00003457 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003458
3459 if (TimePassesIsEnabled)
3460 DebugTimer->stopTimer();
3461
Evan Chenge3d42322009-02-25 07:04:34 +00003462 return SrcId;
Devang Patelccca7fe2009-01-12 19:17:34 +00003463 }
3464
3465 /// RecordRegionStart - Indicate the start of a region.
Devang Patelccca7fe2009-01-12 19:17:34 +00003466 unsigned RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003467 if (TimePassesIsEnabled)
3468 DebugTimer->startTimer();
3469
Devang Patelccca7fe2009-01-12 19:17:34 +00003470 DbgScope *Scope = getOrCreateScope(V);
3471 unsigned ID = MMI->NextLabelID();
3472 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003473
3474 if (TimePassesIsEnabled)
3475 DebugTimer->stopTimer();
3476
Devang Patelccca7fe2009-01-12 19:17:34 +00003477 return ID;
3478 }
3479
3480 /// RecordRegionEnd - Indicate the end of a region.
Devang Patelccca7fe2009-01-12 19:17:34 +00003481 unsigned RecordRegionEnd(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003482 if (TimePassesIsEnabled)
3483 DebugTimer->startTimer();
3484
Devang Patelccca7fe2009-01-12 19:17:34 +00003485 DbgScope *Scope = getOrCreateScope(V);
3486 unsigned ID = MMI->NextLabelID();
3487 Scope->setEndLabelID(ID);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003488
3489 if (TimePassesIsEnabled)
3490 DebugTimer->stopTimer();
3491
Devang Patelccca7fe2009-01-12 19:17:34 +00003492 return ID;
3493 }
3494
3495 /// RecordVariable - Indicate the declaration of a local variable.
Devang Patel1be3ecc2009-04-15 00:10:26 +00003496 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
3497 const MachineInstr *MI) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003498 if (TimePassesIsEnabled)
3499 DebugTimer->startTimer();
3500
Devang Patel0e5200f2009-01-15 18:25:17 +00003501 DIDescriptor Desc(GV);
3502 DbgScope *Scope = NULL;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003503
Devang Patel0e5200f2009-01-15 18:25:17 +00003504 if (Desc.getTag() == DW_TAG_variable) {
3505 // GV is a global variable.
3506 DIGlobalVariable DG(GV);
3507 Scope = getOrCreateScope(DG.getContext().getGV());
3508 } else {
Devang Patel1be3ecc2009-04-15 00:10:26 +00003509 DenseMap<const MachineInstr *, DbgScope *>::iterator
3510 SI = InlinedVariableScopes.find(MI);
3511 if (SI != InlinedVariableScopes.end()) {
3512 // or GV is an inlined local variable.
3513 Scope = SI->second;
3514 } else {
Devang Patel0e5200f2009-01-15 18:25:17 +00003515 // or GV is a local variable.
Devang Patel1be3ecc2009-04-15 00:10:26 +00003516 DIVariable DV(GV);
3517 Scope = getOrCreateScope(DV.getContext().getGV());
3518 }
Devang Patel0e5200f2009-01-15 18:25:17 +00003519 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003520
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003521 assert(Scope && "Unable to find variable' scope");
Devang Patel99ec3532009-01-16 19:28:14 +00003522 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelccca7fe2009-01-12 19:17:34 +00003523 Scope->AddVariable(DV);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003524
3525 if (TimePassesIsEnabled)
3526 DebugTimer->stopTimer();
Devang Patelccca7fe2009-01-12 19:17:34 +00003527 }
Devang Patel0f7fef32009-04-13 17:02:03 +00003528
Devang Patel1be3ecc2009-04-15 00:10:26 +00003529 //// RecordInlinedFnStart - Indicate the start of inlined subroutine.
3530 void RecordInlinedFnStart(Instruction *FSI, DISubprogram &SP, unsigned LabelID,
3531 unsigned Src, unsigned Line, unsigned Col) {
3532 if (!TAI->doesDwarfUsesInlineInfoSection())
3533 return;
3534
3535 DbgScope *Scope = createInlinedSubroutineScope(SP, Src, Line, Col);
3536 Scope->setStartLabelID(LabelID);
Devang Patel0f7fef32009-04-13 17:02:03 +00003537 MMI->RecordUsedDbgLabel(LabelID);
Devang Patel1be3ecc2009-04-15 00:10:26 +00003538 GlobalVariable *GV = SP.getGV();
3539
3540 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3541 SI = DbgInlinedScopeMap.find(GV);
3542 if (SI == DbgInlinedScopeMap.end()) {
3543 SmallVector<DbgScope *, 2> Scopes;
3544 Scopes.push_back(Scope);
3545 DbgInlinedScopeMap[GV] = Scopes;
3546 } else {
3547 SmallVector<DbgScope *, 2> &Scopes = SI->second;
3548 Scopes.push_back(Scope);
3549 }
3550
Devang Patel0f7fef32009-04-13 17:02:03 +00003551 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
3552 I = InlineInfo.find(GV);
3553 if (I == InlineInfo.end()) {
3554 SmallVector<unsigned, 4> Labels;
3555 Labels.push_back(LabelID);
3556 InlineInfo[GV] = Labels;
3557 return;
3558 }
3559
3560 SmallVector<unsigned, 4> &Labels = I->second;
3561 Labels.push_back(LabelID);
3562 }
Devang Patel1be3ecc2009-04-15 00:10:26 +00003563
3564 /// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
3565 unsigned RecordInlinedFnEnd(DISubprogram &SP) {
3566 if (!TAI->doesDwarfUsesInlineInfoSection())
3567 return 0;
3568
3569 GlobalVariable *GV = SP.getGV();
3570 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3571 I = DbgInlinedScopeMap.find(GV);
3572 if (I == DbgInlinedScopeMap.end())
3573 return 0;
3574
3575 SmallVector<DbgScope *, 2> &Scopes = I->second;
3576 DbgScope *Scope = Scopes.back(); Scopes.pop_back();
3577 unsigned ID = MMI->NextLabelID();
3578 MMI->RecordUsedDbgLabel(ID);
3579 Scope->setEndLabelID(ID);
3580 return ID;
3581 }
3582
3583 /// RecordVariableScope - Record scope for the variable declared by
3584 /// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
3585 /// Record scopes for only inlined subroutine variables. Other
3586 /// variables' scopes are determined during RecordVariable().
3587 void RecordVariableScope(DIVariable &DV, const MachineInstr *DeclareMI) {
3588 DISubprogram SP(DV.getContext().getGV());
3589 if (SP.isNull())
3590 return;
3591 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3592 I = DbgInlinedScopeMap.find(SP.getGV());
3593 if (I == DbgInlinedScopeMap.end())
3594 return;
3595
3596 SmallVector<DbgScope *, 2> &Scopes = I->second;
3597 InlinedVariableScopes[DeclareMI] = Scopes.back();
3598 }
3599
Jim Laskey65195462006-10-30 13:35:07 +00003600};
3601
Jim Laskey072200c2007-01-29 18:51:14 +00003602//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003603/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00003604///
3605class DwarfException : public Dwarf {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003606 struct FunctionEHFrameInfo {
3607 std::string FnName;
3608 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003609 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003610 bool hasCalls;
3611 bool hasLandingPads;
3612 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003613 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00003614
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003615 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3616 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003617 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003618 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003619 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003620 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003621 };
3622
3623 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003624
3625 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3626 /// be emitted.
3627 bool shouldEmitTable;
3628
3629 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3630 /// should be emitted.
3631 bool shouldEmitMoves;
3632
3633 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3634 /// should be emitted.
3635 bool shouldEmitTableModule;
3636
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003637 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003638 /// should be emitted.
3639 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00003640
Bill Wendling163ba3f2009-03-10 21:23:25 +00003641 /// ExceptionTimer - Timer for the Dwarf exception writer.
3642 Timer *ExceptionTimer;
3643
Jim Laskey3f09fc22007-02-28 18:38:31 +00003644 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3645 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003646 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003647 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003648 int stackGrowth =
3649 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3650 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003651 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003652
Jim Laskeybacd3042007-02-21 22:48:45 +00003653 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003654 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling722f5f12008-12-24 08:05:17 +00003655
3656 if (!TAI->doesRequireNonLocalEHFrameLabel())
3657 O << TAI->getEHGlobalPrefix();
3658 O << "EH_frame" << Index << ":\n";
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003659 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003660
Jim Laskeybacd3042007-02-21 22:48:45 +00003661 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003662 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003663
Jim Laskeybacd3042007-02-21 22:48:45 +00003664 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003665 EmitDifference("eh_frame_common_end", Index,
3666 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003667 Asm->EOL("Length of Common Information Entry");
3668
Jim Laskeybacd3042007-02-21 22:48:45 +00003669 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003670 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003671 Asm->EmitInt32((int)0);
3672 Asm->EOL("CIE Identifier Tag");
3673 Asm->EmitInt8(DW_CIE_VERSION);
3674 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00003675
Jim Laskeybacd3042007-02-21 22:48:45 +00003676 // The personality presence indicates that language specific information
3677 // will show up in the eh frame.
3678 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00003679 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00003680
Jim Laskeybacd3042007-02-21 22:48:45 +00003681 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003682 Asm->EmitULEB128Bytes(1);
3683 Asm->EOL("CIE Code Alignment Factor");
3684 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00003685 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003686 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00003687 Asm->EOL("CIE Return Address Column");
3688
Jim Laskeybacd3042007-02-21 22:48:45 +00003689 // If there is a personality, we need to indicate the functions location.
3690 if (Personality) {
3691 Asm->EmitULEB128Bytes(7);
3692 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00003693
Duncan Sands671fa972008-05-07 19:11:09 +00003694 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003695 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00003696 Asm->EOL("Personality (pcrel sdata4 indirect)");
3697 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003698 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00003699 Asm->EOL("Personality (pcrel sdata4)");
3700 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00003701
Duncan Sands671fa972008-05-07 19:11:09 +00003702 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00003703 O << TAI->getPersonalityPrefix();
3704 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3705 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00003706 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3707 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00003708 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00003709
Duncan Sands671fa972008-05-07 19:11:09 +00003710 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3711 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003712
Bill Wendlingd60de512009-01-05 22:53:45 +00003713 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3714 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003715 } else {
3716 Asm->EmitULEB128Bytes(1);
3717 Asm->EOL("Augmentation Size");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003718
Bill Wendlingd60de512009-01-05 22:53:45 +00003719 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3720 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003721 }
3722
3723 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003724 std::vector<MachineMove> Moves;
3725 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00003726 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003727
Dale Johannesen21d972a2008-04-30 00:43:29 +00003728 // On Darwin the linker honors the alignment of eh_frame, which means it
3729 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3730 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003731 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003732 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003733 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003734
Jim Laskeybacd3042007-02-21 22:48:45 +00003735 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003736 }
Duncan Sands671fa972008-05-07 19:11:09 +00003737
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003738 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003739 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003740 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003741 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
Chris Lattner266c7bb2009-04-13 05:44:34 +00003742
3743 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
3744 "Should not emit 'available externally' functions at all");
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003745
Jim Laskeybacd3042007-02-21 22:48:45 +00003746 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3747
3748 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003749 // If the corresponding function is static, this should not be
3750 // externally visible.
Rafael Espindolabb46f522009-01-15 20:18:42 +00003751 if (linkage != Function::InternalLinkage &&
Devang Patel2d1768c2009-01-17 08:05:14 +00003752 linkage != Function::PrivateLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003753 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3754 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3755 }
3756
Dale Johannesen038129d2008-01-10 02:03:30 +00003757 // If corresponding function is weak definition, this should be too.
Duncan Sands667d4b82009-03-07 15:45:40 +00003758 if ((linkage == Function::WeakAnyLinkage ||
3759 linkage == Function::WeakODRLinkage ||
3760 linkage == Function::LinkOnceAnyLinkage ||
3761 linkage == Function::LinkOnceODRLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00003762 TAI->getWeakDefDirective())
3763 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3764
3765 // If there are no calls then you can't unwind. This may mean we can
3766 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003767 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00003768 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00003769 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00003770 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00003771 !UnwindTablesMandatory &&
Duncan Sands667d4b82009-03-07 15:45:40 +00003772 ((linkage != Function::WeakAnyLinkage &&
3773 linkage != Function::WeakODRLinkage &&
3774 linkage != Function::LinkOnceAnyLinkage &&
3775 linkage != Function::LinkOnceODRLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00003776 !TAI->getWeakDefDirective() ||
3777 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003778 {
Bill Wendling6e198962007-09-18 01:47:22 +00003779 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003780 // This name has no connection to the function, so it might get
3781 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003782 // dead-stripping unconditionally.
3783 if (const char *UsedDirective = TAI->getUsedDirective())
3784 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00003785 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00003786 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003787
Jim Laskeybacd3042007-02-21 22:48:45 +00003788 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003789 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3790 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003791 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003792
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003793 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003794
Bill Wendling722f5f12008-12-24 08:05:17 +00003795 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3796 PrintRelDirective(true, true);
3797 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3798
3799 if (!TAI->isAbsoluteEHSectionOffsets())
3800 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3801 } else {
3802 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3803 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3804 true, true, false);
3805 }
3806
Jim Laskeybacd3042007-02-21 22:48:45 +00003807 Asm->EOL("FDE CIE offset");
3808
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003809 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003810 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003811 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003812 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003813 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00003814
Jim Laskey3f09fc22007-02-28 18:38:31 +00003815 // If there is a personality and landing pads then point to the language
3816 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003817 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00003818 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00003819 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00003820
3821 if (EHFrameInfo.hasLandingPads)
3822 EmitReference("exception", EHFrameInfo.Number, true, true);
3823 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00003824 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00003825 Asm->EOL("Language Specific Data Area");
3826 } else {
3827 Asm->EmitULEB128Bytes(0);
3828 Asm->EOL("Augmentation size");
3829 }
Duncan Sands671fa972008-05-07 19:11:09 +00003830
Jim Laskeybacd3042007-02-21 22:48:45 +00003831 // Indicate locations of function specific callee saved registers in
3832 // frame.
Devang Patel5aac3d32009-01-17 08:01:33 +00003833 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00003834 true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003835
Dale Johannesen21d972a2008-04-30 00:43:29 +00003836 // On Darwin the linker honors the alignment of eh_frame, which means it
3837 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3838 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003839 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003840 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003841 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003842
3843 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003844 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003845 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003846 // that depends on unused functions (calling undefined externals) being
3847 // dead-stripped to link correctly. Yes, there really is.
3848 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3849 if (const char *UsedDirective = TAI->getUsedDirective())
3850 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3851 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003852 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003853
Duncan Sands57810cd2007-09-05 11:27:52 +00003854 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003855 ///
3856 /// The general organization of the table is complex, but the basic concepts
3857 /// are easy. First there is a header which describes the location and
3858 /// organization of the three components that follow.
3859 /// 1. The landing pad site information describes the range of code covered
3860 /// by the try. In our case it's an accumulation of the ranges covered
3861 /// by the invokes in the try. There is also a reference to the landing
3862 /// pad that handles the exception once processed. Finally an index into
3863 /// the actions table.
3864 /// 2. The action table, in our case, is composed of pairs of type ids
3865 /// and next action offset. Starting with the action index from the
3866 /// landing pad site, each type Id is checked for a match to the current
3867 /// exception. If it matches then the exception and type id are passed
3868 /// on to the landing pad. Otherwise the next action is looked up. This
3869 /// chain is terminated with a next action of zero. If no type id is
3870 /// found the the frame is unwound and handling continues.
3871 /// 3. Type id table contains references to all the C++ typeinfo for all
3872 /// catches in the function. This tables is reversed indexed base 1.
3873
Duncan Sandsb32edb42007-06-06 15:37:31 +00003874 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3875 static unsigned SharedTypeIds(const LandingPadInfo *L,
3876 const LandingPadInfo *R) {
3877 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003878 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003879 unsigned MinSize = LSize < RSize ? LSize : RSize;
3880 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003881
Duncan Sandsb32edb42007-06-06 15:37:31 +00003882 for (; Count != MinSize; ++Count)
3883 if (LIds[Count] != RIds[Count])
3884 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003885
Duncan Sandsb32edb42007-06-06 15:37:31 +00003886 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003887 }
3888
Duncan Sandsb32edb42007-06-06 15:37:31 +00003889 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00003890 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003891 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003892 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003893 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003894
Duncan Sandsb32edb42007-06-06 15:37:31 +00003895 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00003896 if (LIds[i] != RIds[i])
3897 return LIds[i] < RIds[i];
3898
Duncan Sandsb32edb42007-06-06 15:37:31 +00003899 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003900 }
3901
Duncan Sands53c3a332007-05-16 12:12:23 +00003902 struct KeyInfo {
3903 static inline unsigned getEmptyKey() { return -1U; }
3904 static inline unsigned getTombstoneKey() { return -2U; }
3905 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00003906 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00003907 static bool isPod() { return true; }
3908 };
3909
Duncan Sands57810cd2007-09-05 11:27:52 +00003910 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003911 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003912 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003913 int NextAction;
3914 struct ActionEntry *Previous;
3915 };
3916
Duncan Sands57810cd2007-09-05 11:27:52 +00003917 /// PadRange - Structure holding a try-range and the associated landing pad.
3918 struct PadRange {
3919 // The index of the landing pad.
3920 unsigned PadIndex;
3921 // The index of the begin and end labels in the landing pad's label lists.
3922 unsigned RangeIndex;
3923 };
3924
3925 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3926
3927 /// CallSiteEntry - Structure describing an entry in the call-site table.
3928 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00003929 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003930 unsigned BeginLabel; // zero indicates the start of the function.
3931 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00003932 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003933 unsigned PadLabel; // zero indicates that there is no landing pad.
3934 unsigned Action;
3935 };
3936
Jim Laskeybacd3042007-02-21 22:48:45 +00003937 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00003938 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00003939 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00003940 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3941 if (PadInfos.empty()) return;
3942
3943 // Sort the landing pads in order of their type ids. This is used to fold
3944 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00003945 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00003946 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00003947 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00003948 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003949 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3950
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003951 // Negative type ids index into FilterIds, positive type ids index into
3952 // TypeInfos. The value written for a positive type id is just the type
3953 // id itself. For a negative type id, however, the value written is the
3954 // (negative) byte offset of the corresponding FilterIds entry. The byte
3955 // offset is usually equal to the type id, because the FilterIds entries
3956 // are written using a variable width encoding which outputs one byte per
3957 // entry as long as the value written is not too large, but can differ.
3958 // This kind of complication does not occur for positive type ids because
3959 // type infos are output using a fixed width encoding.
3960 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3961 SmallVector<int, 16> FilterOffsets;
3962 FilterOffsets.reserve(FilterIds.size());
3963 int Offset = -1;
3964 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3965 E = FilterIds.end(); I != E; ++I) {
3966 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003967 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003968 }
3969
Duncan Sands57810cd2007-09-05 11:27:52 +00003970 // Compute the actions table and gather the first action index for each
3971 // landing pad site.
3972 SmallVector<ActionEntry, 32> Actions;
3973 SmallVector<unsigned, 64> FirstActions;
3974 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00003975
Duncan Sandsb32edb42007-06-06 15:37:31 +00003976 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00003977 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003978 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003979 const LandingPadInfo *LP = LandingPads[i];
3980 const std::vector<int> &TypeIds = LP->TypeIds;
3981 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003982 unsigned SizeSiteActions = 0;
3983
Duncan Sandsb32edb42007-06-06 15:37:31 +00003984 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00003985 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00003986 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003987
Duncan Sandsb32edb42007-06-06 15:37:31 +00003988 if (NumShared) {
3989 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3990 assert(Actions.size());
3991 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003992 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3993 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003994 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003995 SizeAction -=
3996 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003997 SizeAction += -PrevAction->NextAction;
3998 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003999 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00004000 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00004001
Duncan Sandsb32edb42007-06-06 15:37:31 +00004002 // Compute the actions.
4003 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
4004 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004005 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
4006 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004007 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004008
4009 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004010 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004011 SizeSiteActions += SizeAction;
4012
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004013 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00004014 Actions.push_back(Action);
4015
4016 PrevAction = &Actions.back();
4017 }
4018
4019 // Record the first action of the landing pad site.
4020 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
4021 } // else identical - re-use previous FirstAction
4022
4023 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00004024
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00004025 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00004026 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00004027 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004028
Duncan Sands481dc722007-12-19 07:36:31 +00004029 // Compute the call-site table. The entry for an invoke has a try-range
4030 // containing the call, a non-zero landing pad and an appropriate action.
4031 // The entry for an ordinary call has a try-range containing the call and
4032 // zero for the landing pad and the action. Calls marked 'nounwind' have
4033 // no entry and must not be contained in the try-range of any entry - they
4034 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004035 SmallVector<CallSiteEntry, 64> CallSites;
4036
4037 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00004038 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
4039 // by try-range labels when lowered). Ordinary calls do not, so appropriate
4040 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00004041 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
4042 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00004043 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004044 unsigned BeginLabel = LandingPad->BeginLabels[j];
4045 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
4046 PadRange P = { i, j };
4047 PadMap[BeginLabel] = P;
4048 }
4049 }
4050
Duncan Sands481dc722007-12-19 07:36:31 +00004051 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00004052 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00004053
4054 // Whether there is a potentially throwing instruction (currently this means
4055 // an ordinary call) between the end of the previous try-range and now.
4056 bool SawPotentiallyThrowing = false;
4057
4058 // Whether the last callsite entry was for an invoke.
4059 bool PreviousIsInvoke = false;
4060
Duncan Sands481dc722007-12-19 07:36:31 +00004061 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004062 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
4063 I != E; ++I) {
4064 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
4065 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00004066 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00004067 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00004068 continue;
4069 }
4070
Chris Lattner9e330492007-12-30 20:50:28 +00004071 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00004072 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00004073
Duncan Sands481dc722007-12-19 07:36:31 +00004074 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00004075 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00004076 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004077
Duncan Sands481dc722007-12-19 07:36:31 +00004078 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00004079 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00004080 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00004081 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00004082 continue;
4083
4084 PadRange P = L->second;
4085 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
4086
4087 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
4088 "Inconsistent landing pad map!");
4089
4090 // If some instruction between the previous try-range and this one may
4091 // throw, create a call-site entry with no landing pad for the region
4092 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00004093 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004094 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
4095 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00004096 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004097 }
4098
4099 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00004100 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00004101
Duncan Sands481dc722007-12-19 07:36:31 +00004102 if (LandingPad->LandingPadLabel) {
4103 // This try-range is for an invoke.
4104 CallSiteEntry Site = {BeginLabel, LastLabel,
4105 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00004106
Duncan Sands481dc722007-12-19 07:36:31 +00004107 // Try to merge with the previous call-site.
4108 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00004109 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00004110 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
4111 // Extend the range of the previous entry.
4112 Prev.EndLabel = Site.EndLabel;
4113 continue;
4114 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004115 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004116
Duncan Sands481dc722007-12-19 07:36:31 +00004117 // Otherwise, create a new call-site.
4118 CallSites.push_back(Site);
4119 PreviousIsInvoke = true;
4120 } else {
4121 // Create a gap.
4122 PreviousIsInvoke = false;
4123 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004124 }
4125 }
4126 // If some instruction between the previous try-range and the end of the
4127 // function may throw, create a call-site entry with no landing pad for the
4128 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00004129 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004130 CallSiteEntry Site = {LastLabel, 0, 0, 0};
4131 CallSites.push_back(Site);
4132 }
4133
Jim Laskeybacd3042007-02-21 22:48:45 +00004134 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00004135
4136 // Call sites.
4137 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
4138 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
4139 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
4140 unsigned SizeSites = CallSites.size() * (SiteStartSize +
4141 SiteLengthSize +
4142 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00004143 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004144 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00004145
Duncan Sands671fa972008-05-07 19:11:09 +00004146 // Type infos.
4147 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
4148 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004149
4150 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004151 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004152 SizeSites + SizeActions + SizeTypes;
4153
4154 unsigned TotalSize = sizeof(int8_t) + // LPStart format
4155 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004156 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004157 TypeOffset;
4158
4159 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004160
Jim Laskeybacd3042007-02-21 22:48:45 +00004161 // Begin the exception table.
4162 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng05548eb2008-02-29 19:36:59 +00004163 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesend65b2642008-10-08 21:50:21 +00004164 O << "GCC_except_table" << SubprogramCount << ":\n";
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004165 for (unsigned i = 0; i != SizeAlign; ++i) {
4166 Asm->EmitInt8(0);
4167 Asm->EOL("Padding");
4168 }
Jim Laskeybacd3042007-02-21 22:48:45 +00004169 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004170
Jim Laskeybacd3042007-02-21 22:48:45 +00004171 // Emit the header.
4172 Asm->EmitInt8(DW_EH_PE_omit);
4173 Asm->EOL("LPStart format (DW_EH_PE_omit)");
4174 Asm->EmitInt8(DW_EH_PE_absptr);
4175 Asm->EOL("TType format (DW_EH_PE_absptr)");
4176 Asm->EmitULEB128Bytes(TypeOffset);
4177 Asm->EOL("TType base offset");
4178 Asm->EmitInt8(DW_EH_PE_udata4);
4179 Asm->EOL("Call site format (DW_EH_PE_udata4)");
4180 Asm->EmitULEB128Bytes(SizeSites);
4181 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004182
Duncan Sands57810cd2007-09-05 11:27:52 +00004183 // Emit the landing pad site information.
4184 for (unsigned i = 0; i < CallSites.size(); ++i) {
4185 CallSiteEntry &S = CallSites[i];
4186 const char *BeginTag;
4187 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00004188
Duncan Sands57810cd2007-09-05 11:27:52 +00004189 if (!S.BeginLabel) {
4190 BeginTag = "eh_func_begin";
4191 BeginNumber = SubprogramCount;
4192 } else {
4193 BeginTag = "label";
4194 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00004195 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004196
Duncan Sands57810cd2007-09-05 11:27:52 +00004197 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004198 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004199 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00004200
Duncan Sands57810cd2007-09-05 11:27:52 +00004201 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00004202 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00004203 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004204 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00004205 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004206 }
4207 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004208
Duncan Sands671fa972008-05-07 19:11:09 +00004209 if (!S.PadLabel)
4210 Asm->EmitInt32(0);
4211 else
Duncan Sands57810cd2007-09-05 11:27:52 +00004212 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004213 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004214 Asm->EOL("Landing pad");
4215
4216 Asm->EmitULEB128Bytes(S.Action);
4217 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004218 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004219
Jim Laskeybacd3042007-02-21 22:48:45 +00004220 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004221 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4222 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00004223
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004224 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004225 Asm->EOL("TypeInfo index");
4226 Asm->EmitSLEB128Bytes(Action.NextAction);
4227 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004228 }
4229
4230 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00004231 for (unsigned M = TypeInfos.size(); M; --M) {
4232 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00004233
4234 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00004235
Bill Wendling7d16e852009-04-10 00:12:49 +00004236 if (GV) {
4237 std::string GLN;
4238 O << Asm->getGlobalLinkName(GV, GLN);
4239 } else {
Jim Laskeybacd3042007-02-21 22:48:45 +00004240 O << "0";
Bill Wendling7d16e852009-04-10 00:12:49 +00004241 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004242
Jim Laskeybacd3042007-02-21 22:48:45 +00004243 Asm->EOL("TypeInfo");
4244 }
4245
Duncan Sands73ef58a2007-06-02 16:53:42 +00004246 // Emit the filter typeids.
4247 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4248 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00004249 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00004250 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00004251 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004252
Evan Cheng05548eb2008-02-29 19:36:59 +00004253 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004254 }
4255
Jim Laskey072200c2007-01-29 18:51:14 +00004256public:
4257 //===--------------------------------------------------------------------===//
4258 // Main entry points.
4259 //
Owen Andersoncb371882008-08-21 00:14:44 +00004260 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00004261 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
4262 shouldEmitTableModule(false), shouldEmitMovesModule(false),
4263 ExceptionTimer(0) {
4264 if (TimePassesIsEnabled)
4265 ExceptionTimer = new Timer("Dwarf Exception Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00004266 getDwarfTimerGroup());
Bill Wendling163ba3f2009-03-10 21:23:25 +00004267 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004268
Bill Wendling163ba3f2009-03-10 21:23:25 +00004269 virtual ~DwarfException() {
4270 delete ExceptionTimer;
4271 }
Jim Laskey072200c2007-01-29 18:51:14 +00004272
4273 /// SetModuleInfo - Set machine module information when it's known that pass
4274 /// manager has created it. Set by the target AsmPrinter.
4275 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00004276 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00004277 }
4278
4279 /// BeginModule - Emit all exception information that should come prior to the
4280 /// content.
4281 void BeginModule(Module *M) {
4282 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00004283 }
4284
4285 /// EndModule - Emit all exception information that should come after the
4286 /// content.
4287 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004288 if (TimePassesIsEnabled)
4289 ExceptionTimer->startTimer();
4290
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004291 if (shouldEmitMovesModule || shouldEmitTableModule) {
4292 const std::vector<Function *> Personalities = MMI->getPersonalities();
Evan Chenge3d42322009-02-25 07:04:34 +00004293 for (unsigned i = 0; i < Personalities.size(); ++i)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004294 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00004295
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004296 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4297 E = EHFrames.end(); I != E; ++I)
4298 EmitEHFrame(*I);
4299 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004300
4301 if (TimePassesIsEnabled)
4302 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004303 }
4304
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004305 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00004306 /// emitted immediately after the function entry point.
4307 void BeginFunction(MachineFunction *MF) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004308 if (TimePassesIsEnabled)
4309 ExceptionTimer->startTimer();
4310
Jim Laskey072200c2007-01-29 18:51:14 +00004311 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004312 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004313
Bill Wendling163ba3f2009-03-10 21:23:25 +00004314 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004315 // Map all labels and get rid of any dead landing pads.
4316 MMI->TidyLandingPads();
Bill Wendling163ba3f2009-03-10 21:23:25 +00004317
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004318 // If any landing pads survive, we need an EH table.
4319 if (MMI->getLandingPads().size())
4320 shouldEmitTable = true;
4321
4322 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00004323 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004324 shouldEmitMoves = true;
4325
4326 if (shouldEmitMoves || shouldEmitTable)
4327 // Assumes in correct section after the entry point.
4328 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00004329 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004330
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004331 shouldEmitTableModule |= shouldEmitTable;
4332 shouldEmitMovesModule |= shouldEmitMoves;
Bill Wendling163ba3f2009-03-10 21:23:25 +00004333
4334 if (TimePassesIsEnabled)
4335 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004336 }
4337
4338 /// EndFunction - Gather and emit post-function exception information.
4339 ///
4340 void EndFunction() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004341 if (TimePassesIsEnabled)
4342 ExceptionTimer->startTimer();
4343
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004344 if (shouldEmitMoves || shouldEmitTable) {
4345 EmitLabel("eh_func_end", SubprogramCount);
4346 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00004347
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004348 // Save EH frame information
Bill Wendling7d16e852009-04-10 00:12:49 +00004349 std::string Name;
4350 EHFrames.push_back(
4351 FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF, Name),
4352 SubprogramCount,
4353 MMI->getPersonalityIndex(),
4354 MF->getFrameInfo()->hasCalls(),
4355 !MMI->getLandingPads().empty(),
4356 MMI->getFrameMoves(),
4357 MF->getFunction()));
Bill Wendling163ba3f2009-03-10 21:23:25 +00004358 }
4359
4360 if (TimePassesIsEnabled)
4361 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004362 }
4363};
4364
Jim Laskey0d086af2006-02-27 12:43:29 +00004365} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00004366
4367//===----------------------------------------------------------------------===//
4368
Jim Laskeyd18e2892006-01-20 20:34:06 +00004369/// Emit - Print the abbreviation using the specified Dwarf writer.
4370///
Jim Laskey072200c2007-01-29 18:51:14 +00004371void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004372 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00004373 DD.getAsm()->EmitULEB128Bytes(Tag);
4374 DD.getAsm()->EOL(TagString(Tag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004375
Jim Laskeyd18e2892006-01-20 20:34:06 +00004376 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00004377 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4378 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004379
Jim Laskeyd18e2892006-01-20 20:34:06 +00004380 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00004381 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004382 const DIEAbbrevData &AttrData = Data[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004383
Jim Laskeyd18e2892006-01-20 20:34:06 +00004384 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00004385 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4386 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004387
Jim Laskeyd18e2892006-01-20 20:34:06 +00004388 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00004389 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4390 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00004391 }
4392
4393 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00004394 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4395 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00004396}
4397
4398#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00004399void DIEAbbrev::print(std::ostream &O) {
4400 O << "Abbreviation @"
4401 << std::hex << (intptr_t)this << std::dec
4402 << " "
4403 << TagString(Tag)
4404 << " "
4405 << ChildrenString(ChildrenFlag)
4406 << "\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004407
Jim Laskeya0f3d172006-09-07 22:06:40 +00004408 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4409 O << " "
4410 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004411 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00004412 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004413 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00004414 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00004415}
Bill Wendlinge8156192006-12-07 01:30:32 +00004416void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00004417#endif
4418
4419//===----------------------------------------------------------------------===//
4420
Jim Laskeyef42a012006-11-02 20:12:39 +00004421#ifndef NDEBUG
4422void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004423 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004424}
Jim Laskeyef42a012006-11-02 20:12:39 +00004425#endif
4426
4427//===----------------------------------------------------------------------===//
4428
Jim Laskey063e7652006-01-17 17:31:53 +00004429/// EmitValue - Emit integer of appropriate size.
4430///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004431void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00004432 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00004433 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00004434 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004435 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004436 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004437 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004438 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004439 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004440 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004441 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4442 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4443 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004444 default: assert(0 && "DIE Value form not supported yet"); break;
4445 }
4446}
4447
4448/// SizeOf - Determine size of integer value in bytes.
4449///
Jim Laskey072200c2007-01-29 18:51:14 +00004450unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004451 switch (Form) {
4452 case DW_FORM_flag: // Fall thru
4453 case DW_FORM_ref1: // Fall thru
4454 case DW_FORM_data1: return sizeof(int8_t);
4455 case DW_FORM_ref2: // Fall thru
4456 case DW_FORM_data2: return sizeof(int16_t);
4457 case DW_FORM_ref4: // Fall thru
4458 case DW_FORM_data4: return sizeof(int32_t);
4459 case DW_FORM_ref8: // Fall thru
4460 case DW_FORM_data8: return sizeof(int64_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004461 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4462 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00004463 default: assert(0 && "DIE Value form not supported yet"); break;
4464 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004465 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00004466}
4467
Jim Laskey063e7652006-01-17 17:31:53 +00004468//===----------------------------------------------------------------------===//
4469
4470/// EmitValue - Emit string value.
4471///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004472void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Bill Wendlingdd446322009-03-10 23:57:09 +00004473 DD.getAsm()->EmitString(Str);
Jim Laskey063e7652006-01-17 17:31:53 +00004474}
4475
Jim Laskey063e7652006-01-17 17:31:53 +00004476//===----------------------------------------------------------------------===//
4477
4478/// EmitValue - Emit label value.
4479///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004480void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004481 bool IsSmall = Form == DW_FORM_data4;
4482 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004483}
4484
4485/// SizeOf - Determine size of label value in bytes.
4486///
Jim Laskey072200c2007-01-29 18:51:14 +00004487unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004488 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004489 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004490}
Jim Laskeyef42a012006-11-02 20:12:39 +00004491
Jim Laskey063e7652006-01-17 17:31:53 +00004492//===----------------------------------------------------------------------===//
4493
Jim Laskeyd18e2892006-01-20 20:34:06 +00004494/// EmitValue - Emit label value.
4495///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004496void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004497 bool IsSmall = Form == DW_FORM_data4;
4498 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00004499}
4500
4501/// SizeOf - Determine size of label value in bytes.
4502///
Jim Laskey072200c2007-01-29 18:51:14 +00004503unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004504 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004505 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00004506}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004507
Jim Laskeyd18e2892006-01-20 20:34:06 +00004508//===----------------------------------------------------------------------===//
4509
Jim Laskey063e7652006-01-17 17:31:53 +00004510/// EmitValue - Emit delta value.
4511///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004512void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4513 bool IsSmall = Form == DW_FORM_data4;
4514 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4515 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4516}
4517
4518/// SizeOf - Determine size of delta value in bytes.
4519///
4520unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4521 if (Form == DW_FORM_data4) return 4;
4522 return DD.getTargetData()->getPointerSize();
4523}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004524
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004525//===----------------------------------------------------------------------===//
4526
4527/// EmitValue - Emit delta value.
4528///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004529void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004530 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00004531 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004532}
4533
4534/// SizeOf - Determine size of delta value in bytes.
4535///
Jim Laskey072200c2007-01-29 18:51:14 +00004536unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004537 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004538 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004539}
4540
4541//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004542
Jim Laskeyb8509c52006-03-23 18:07:55 +00004543/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004544///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004545void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004546 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00004547}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004548
Jim Laskeyd18e2892006-01-20 20:34:06 +00004549//===----------------------------------------------------------------------===//
4550
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004551/// ComputeSize - calculate the size of the block.
4552///
Jim Laskey072200c2007-01-29 18:51:14 +00004553unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00004554 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00004555 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004556
Jim Laskeyef42a012006-11-02 20:12:39 +00004557 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00004558 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00004559 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004560 }
4561 return Size;
4562}
4563
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004564/// EmitValue - Emit block data.
4565///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004566void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004567 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004568 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4569 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4570 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4571 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004572 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004573 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004574
Owen Anderson873e1b52008-06-24 21:44:59 +00004575 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00004576
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004577 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00004578 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00004579 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004580 }
4581}
4582
4583/// SizeOf - Determine size of block data in bytes.
4584///
Jim Laskey072200c2007-01-29 18:51:14 +00004585unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004586 switch (Form) {
4587 case DW_FORM_block1: return Size + sizeof(int8_t);
4588 case DW_FORM_block2: return Size + sizeof(int16_t);
4589 case DW_FORM_block4: return Size + sizeof(int32_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004590 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004591 default: assert(0 && "Improper form for block"); break;
4592 }
4593 return 0;
4594}
4595
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004596//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004597/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00004598
4599DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00004600 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00004601 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00004602}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004603
Jim Laskeyb8509c52006-03-23 18:07:55 +00004604/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4605///
4606void DIE::AddSiblingOffset() {
4607 DIEInteger *DI = new DIEInteger(0);
4608 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00004609 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00004610}
4611
Jim Laskeyef42a012006-11-02 20:12:39 +00004612/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004613///
Jim Laskeyef42a012006-11-02 20:12:39 +00004614void DIE::Profile(FoldingSetNodeID &ID) {
4615 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004616
Jim Laskeyef42a012006-11-02 20:12:39 +00004617 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4618 ID.AddPointer(Children[i]);
4619
4620 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4621 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00004622}
Jim Laskeyef42a012006-11-02 20:12:39 +00004623
4624#ifndef NDEBUG
4625void DIE::print(std::ostream &O, unsigned IncIndent) {
4626 static unsigned IndentCount = 0;
4627 IndentCount += IncIndent;
4628 const std::string Indent(IndentCount, ' ');
4629 bool isBlock = Abbrev.getTag() == 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004630
Jim Laskeyef42a012006-11-02 20:12:39 +00004631 if (!isBlock) {
4632 O << Indent
4633 << "Die: "
4634 << "0x" << std::hex << (intptr_t)this << std::dec
4635 << ", Offset: " << Offset
4636 << ", Size: " << Size
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004637 << "\n";
4638
Jim Laskeyef42a012006-11-02 20:12:39 +00004639 O << Indent
4640 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00004641 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00004642 << ChildrenString(Abbrev.getChildrenFlag());
4643 } else {
4644 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00004645 }
4646 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00004647
Owen Anderson873e1b52008-06-24 21:44:59 +00004648 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004649
Jim Laskeyef42a012006-11-02 20:12:39 +00004650 IndentCount += 2;
4651 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4652 O << Indent;
Bill Wendling4e974012008-07-22 00:28:47 +00004653
4654 if (!isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +00004655 O << AttributeString(Data[i].getAttribute());
Bill Wendling4e974012008-07-22 00:28:47 +00004656 else
Jim Laskeyef42a012006-11-02 20:12:39 +00004657 O << "Blk[" << i << "]";
Bill Wendling4e974012008-07-22 00:28:47 +00004658
Jim Laskeyef42a012006-11-02 20:12:39 +00004659 O << " "
4660 << FormEncodingString(Data[i].getForm())
4661 << " ";
4662 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00004663 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00004664 }
Jim Laskeyef42a012006-11-02 20:12:39 +00004665 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00004666
Jim Laskeyef42a012006-11-02 20:12:39 +00004667 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4668 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00004669 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004670
Jim Laskeyef42a012006-11-02 20:12:39 +00004671 if (!isBlock) O << "\n";
4672 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00004673}
4674
Jim Laskeyef42a012006-11-02 20:12:39 +00004675void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004676 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00004677}
Jim Laskeybd761842006-02-27 17:27:12 +00004678#endif
Jim Laskey65195462006-10-30 13:35:07 +00004679
4680//===----------------------------------------------------------------------===//
4681/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00004682///
Jim Laskey65195462006-10-30 13:35:07 +00004683
Bill Wendling91b8b802009-03-10 20:41:52 +00004684DwarfWriter::DwarfWriter()
Bill Wendling163ba3f2009-03-10 21:23:25 +00004685 : ImmutablePass(&ID), DD(0), DE(0) {}
Jim Laskey65195462006-10-30 13:35:07 +00004686
4687DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00004688 delete DE;
4689 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00004690}
4691
Jim Laskey65195462006-10-30 13:35:07 +00004692/// BeginModule - Emit all Dwarf sections that should come prior to the
4693/// content.
Devang Pateleb3fc282009-01-08 23:40:34 +00004694void DwarfWriter::BeginModule(Module *M,
4695 MachineModuleInfo *MMI,
4696 raw_ostream &OS, AsmPrinter *A,
4697 const TargetAsmInfo *T) {
4698 DE = new DwarfException(OS, A, T);
4699 DD = new DwarfDebug(OS, A, T);
Jim Laskey072200c2007-01-29 18:51:14 +00004700 DE->BeginModule(M);
4701 DD->BeginModule(M);
Devang Patel7bb89ed2009-01-13 00:20:51 +00004702 DD->SetDebugInfo(MMI);
Devang Pateleb3fc282009-01-08 23:40:34 +00004703 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00004704}
4705
4706/// EndModule - Emit all Dwarf sections that should come after the content.
4707///
4708void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00004709 DE->EndModule();
4710 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00004711}
4712
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004713/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00004714/// emitted immediately after the function entry point.
4715void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00004716 DE->BeginFunction(MF);
4717 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00004718}
4719
4720/// EndFunction - Gather and emit post-function debug information.
4721///
Bill Wendlingd751c642008-09-26 00:28:12 +00004722void DwarfWriter::EndFunction(MachineFunction *MF) {
4723 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004724 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004725
Bill Wendlingc91d0b92008-07-22 00:53:37 +00004726 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00004727 // Clear function debug information.
4728 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00004729}
Devang Patelccca7fe2009-01-12 19:17:34 +00004730
Devang Patelcf3a4482009-01-15 23:41:32 +00004731/// ValidDebugInfo - Return true if V represents valid debug info value.
Devang Patel48c7fa22009-04-13 18:13:16 +00004732bool DwarfWriter::ValidDebugInfo(Value *V, bool FastISel) {
4733 return DD && DD->ValidDebugInfo(V, FastISel);
Devang Patelcf3a4482009-01-15 23:41:32 +00004734}
4735
Devang Patelccca7fe2009-01-12 19:17:34 +00004736/// RecordSourceLine - Records location information and associates it with a
4737/// label. Returns a unique label ID used to generate a label and provide
4738/// correspondence to the source line list.
4739unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
4740 unsigned Src) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004741 return DD->RecordSourceLine(Line, Col, Src);
Devang Patelccca7fe2009-01-12 19:17:34 +00004742}
4743
Evan Chenge3d42322009-02-25 07:04:34 +00004744/// getOrCreateSourceID - Look up the source id with the given directory and
4745/// source file names. If none currently exists, create a new id and insert it
4746/// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
4747/// as well.
4748unsigned DwarfWriter::getOrCreateSourceID(const std::string &DirName,
4749 const std::string &FileName) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004750 return DD->getOrCreateSourceID(DirName, FileName);
Devang Patelccca7fe2009-01-12 19:17:34 +00004751}
4752
4753/// RecordRegionStart - Indicate the start of a region.
4754unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004755 return DD->RecordRegionStart(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004756}
4757
4758/// RecordRegionEnd - Indicate the end of a region.
4759unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004760 return DD->RecordRegionEnd(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004761}
4762
4763/// getRecordSourceLineCount - Count source lines.
4764unsigned DwarfWriter::getRecordSourceLineCount() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004765 return DD->getRecordSourceLineCount();
Devang Patelccca7fe2009-01-12 19:17:34 +00004766}
Devang Patelbb8c5952009-01-13 21:25:00 +00004767
Devang Patelc48c5502009-01-13 21:44:10 +00004768/// RecordVariable - Indicate the declaration of a local variable.
4769///
Devang Patel1be3ecc2009-04-15 00:10:26 +00004770void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
4771 const MachineInstr *MI) {
4772 DD->RecordVariable(GV, FrameIndex, MI);
Devang Patelc48c5502009-01-13 21:44:10 +00004773}
Devang Patelbbdc8202009-01-13 23:54:55 +00004774
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004775/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
4776/// be emitted.
4777bool DwarfWriter::ShouldEmitDwarfDebug() const {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004778 return DD->ShouldEmitDwarfDebug();
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004779}
Devang Patel0f7fef32009-04-13 17:02:03 +00004780
Devang Patel1be3ecc2009-04-15 00:10:26 +00004781//// RecordInlinedFnStart - Global variable GV is inlined at the location marked
Devang Patel0f7fef32009-04-13 17:02:03 +00004782//// by LabelID label.
Devang Patel1be3ecc2009-04-15 00:10:26 +00004783void DwarfWriter::RecordInlinedFnStart(Instruction *I, DISubprogram &SP,
4784 unsigned LabelID, unsigned Src,
4785 unsigned Line, unsigned Col) {
4786 DD->RecordInlinedFnStart(I, SP, LabelID, Src, Line, Col);
Devang Patel0f7fef32009-04-13 17:02:03 +00004787}
4788
Devang Patel1be3ecc2009-04-15 00:10:26 +00004789/// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
4790unsigned DwarfWriter::RecordInlinedFnEnd(DISubprogram &SP) {
4791 return DD->RecordInlinedFnEnd(SP);
4792}
4793
4794/// RecordVariableScope - Record scope for the variable declared by
4795/// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
4796void DwarfWriter::RecordVariableScope(DIVariable &DV,
4797 const MachineInstr *DeclareMI) {
4798 DD->RecordVariableScope(DV, DeclareMI);
4799}