blob: 847a1c9ba6d96ab92387d7d94dc10760cbabd64c [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 Patel7d2f9722009-04-15 20:41:31 +00001274 // FunctionDbgScope - Top level scope for the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001275 //
Devang Patel7d2f9722009-04-15 20:41:31 +00001276 DbgScope *FunctionDbgScope;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001277
Bill Wendling163ba3f2009-03-10 21:23:25 +00001278 /// DbgScopeMap - Tracks the scopes in the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001279 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
Bill Wendling163ba3f2009-03-10 21:23:25 +00001280
Devang Patel1be3ecc2009-04-15 00:10:26 +00001281 /// DbgInlinedScopeMap - Tracks inlined scopes in the current function.
1282 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> > DbgInlinedScopeMap;
1283
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.
Devang Patel7d2f9722009-04-15 20:41:31 +00002040 FunctionDbgScope = 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.
Devang Patel7d2f9722009-04-15 20:41:31 +00002055 assert (FunctionDbgScope && "Function scope info missing!");
2056 FunctionDbgScope->AddScope(Scope);
Devang Patel1be3ecc2009-04-15 00:10:26 +00002057 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;
Devang Patelc782ad02009-04-15 19:42:57 +00002094 if (MainCU && TAI->doesDwarfUsesInlineInfoSection()
2095 && Scope->isInlinedSubroutine()) {
Devang Patel1be3ecc2009-04-15 00:10:26 +00002096 ScopeDie = new DIE(DW_TAG_inlined_subroutine);
2097 DIE *Origin = MainCU->getDieMapSlotFor(Scope->getDesc().getGV());
2098 AddDIEntry(ScopeDie, DW_AT_abstract_origin, DW_FORM_ref4, Origin);
2099 AddUInt(ScopeDie, DW_AT_call_file, 0, Scope->getFile());
2100 AddUInt(ScopeDie, DW_AT_call_line, 0, Scope->getLine());
2101 AddUInt(ScopeDie, DW_AT_call_column, 0, Scope->getColumn());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002102 }
Devang Patel1be3ecc2009-04-15 00:10:26 +00002103 else
2104 ScopeDie = new DIE(DW_TAG_lexical_block);
2105
2106 // Add the scope bounds.
2107 if (StartID) {
2108 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2109 DWLabel("label", StartID));
2110 } else {
2111 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2112 DWLabel("func_begin", SubprogramCount));
2113 }
2114 if (EndID) {
2115 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2116 DWLabel("label", EndID));
2117 } else {
2118 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2119 DWLabel("func_end", SubprogramCount));
2120 }
2121
2122 // Add the scope contents.
2123 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2124 ParentDie->AddChild(ScopeDie);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002125 }
2126 }
2127 }
2128
Devang Patel7d2f9722009-04-15 20:41:31 +00002129 /// ConstructFunctionDbgScope - Construct the scope for the subprogram.
Devang Patel7a6e5a32009-01-08 02:33:41 +00002130 ///
Devang Patel7d2f9722009-04-15 20:41:31 +00002131 void ConstructFunctionDbgScope(DbgScope *RootScope) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00002132 // Exit if there is no root scope.
2133 if (!RootScope) return;
Devang Patel0e5200f2009-01-15 18:25:17 +00002134 DIDescriptor Desc = RootScope->getDesc();
2135 if (Desc.isNull())
2136 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002137
2138 // Get the subprogram debug information entry.
Devang Patel0e5200f2009-01-15 18:25:17 +00002139 DISubprogram SPD(Desc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002140
2141 // Get the compile unit context.
Devang Pateldd9db662009-01-30 18:20:31 +00002142 CompileUnit *Unit = MainCU;
2143 if (!Unit)
2144 Unit = FindCompileUnit(SPD.getCompileUnit());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002145
2146 // Get the subprogram die.
Devang Patelf6bac3e2009-01-12 22:58:14 +00002147 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002148 assert(SPDie && "Missing subprogram descriptor");
2149
2150 // Add the function bounds.
2151 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2152 DWLabel("func_begin", SubprogramCount));
2153 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2154 DWLabel("func_end", SubprogramCount));
2155 MachineLocation Location(RI->getFrameRegister(*MF));
2156 AddAddress(SPDie, DW_AT_frame_base, Location);
2157
2158 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2159 }
2160
2161 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2162 ///
2163 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Chenge3d42322009-02-25 07:04:34 +00002164 const char *FnName = MF->getFunction()->getNameStart();
2165 if (MainCU) {
Bill Wendling972bbac2009-04-09 21:49:15 +00002166 StringMap<DIE*> &Globals = MainCU->getGlobals();
2167 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Chenge3d42322009-02-25 07:04:34 +00002168 if (GI != Globals.end()) {
2169 DIE *SPDie = GI->second;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002170
2171 // Add the function bounds.
2172 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2173 DWLabel("func_begin", SubprogramCount));
2174 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2175 DWLabel("func_end", SubprogramCount));
2176
2177 MachineLocation Location(RI->getFrameRegister(*MF));
2178 AddAddress(SPDie, DW_AT_frame_base, Location);
2179 return;
2180 }
Evan Chenge3d42322009-02-25 07:04:34 +00002181 } else {
2182 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2183 CompileUnit *Unit = CompileUnits[i];
Bill Wendling972bbac2009-04-09 21:49:15 +00002184 StringMap<DIE*> &Globals = Unit->getGlobals();
2185 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Chenge3d42322009-02-25 07:04:34 +00002186 if (GI != Globals.end()) {
2187 DIE *SPDie = GI->second;
2188
2189 // Add the function bounds.
2190 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2191 DWLabel("func_begin", SubprogramCount));
2192 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2193 DWLabel("func_end", SubprogramCount));
2194
2195 MachineLocation Location(RI->getFrameRegister(*MF));
2196 AddAddress(SPDie, DW_AT_frame_base, Location);
2197 return;
2198 }
2199 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00002200 }
Evan Chenge3d42322009-02-25 07:04:34 +00002201
Devang Patel7a6e5a32009-01-08 02:33:41 +00002202#if 0
2203 // FIXME: This is causing an abort because C++ mangled names are compared
2204 // with their unmangled counterparts. See PR2885. Don't do this assert.
2205 assert(0 && "Couldn't find DIE for machine function!");
2206#endif
Evan Chenge3d42322009-02-25 07:04:34 +00002207 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002208 }
2209
Jim Laskeyef42a012006-11-02 20:12:39 +00002210 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2211 /// tools to recognize the object file contains Dwarf information.
2212 void EmitInitial() {
2213 // Check to see if we already emitted intial headers.
2214 if (didInitial) return;
2215 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002216
Jim Laskeyef42a012006-11-02 20:12:39 +00002217 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002218 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002219 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002220 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002221 }
2222 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2223 EmitLabel("section_info", 0);
2224 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2225 EmitLabel("section_abbrev", 0);
2226 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2227 EmitLabel("section_aranges", 0);
Scott Michel210de722009-01-26 22:32:51 +00002228 if (TAI->doesSupportMacInfoSection()) {
2229 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2230 EmitLabel("section_macinfo", 0);
2231 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002232 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2233 EmitLabel("section_line", 0);
2234 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2235 EmitLabel("section_loc", 0);
2236 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2237 EmitLabel("section_pubnames", 0);
2238 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2239 EmitLabel("section_str", 0);
2240 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2241 EmitLabel("section_ranges", 0);
2242
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002243 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002244 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002245 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002246 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002247 }
2248
Jim Laskey65195462006-10-30 13:35:07 +00002249 /// EmitDIE - Recusively Emits a debug information entry.
2250 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002251 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002252 // Get the abbreviation for this DIE.
2253 unsigned AbbrevNumber = Die->getAbbrevNumber();
2254 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002255
Jim Laskeybacd3042007-02-21 22:48:45 +00002256 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002257
2258 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002259 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002260
Evan Cheng42bf74b2009-03-25 01:47:28 +00002261 if (Asm->isVerbose())
Evan Cheng6547e402008-07-01 23:18:29 +00002262 Asm->EOL(std::string("Abbrev [" +
2263 utostr(AbbrevNumber) +
2264 "] 0x" + utohexstr(Die->getOffset()) +
2265 ":0x" + utohexstr(Die->getSize()) + " " +
2266 TagString(Abbrev->getTag())));
2267 else
2268 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002269
Owen Anderson873e1b52008-06-24 21:44:59 +00002270 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2271 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002272
Jim Laskeyef42a012006-11-02 20:12:39 +00002273 // Emit the DIE attribute values.
2274 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2275 unsigned Attr = AbbrevData[i].getAttribute();
2276 unsigned Form = AbbrevData[i].getForm();
2277 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002278
Jim Laskeyef42a012006-11-02 20:12:39 +00002279 switch (Attr) {
2280 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002281 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002282 break;
2283 }
2284 default: {
2285 // Emit an attribute using the defined form.
2286 Values[i]->EmitValue(*this, Form);
2287 break;
2288 }
2289 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002290
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002291 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002292 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002293
Jim Laskeyef42a012006-11-02 20:12:39 +00002294 // Emit the DIE children if any.
2295 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2296 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002297
Jim Laskeyef42a012006-11-02 20:12:39 +00002298 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2299 EmitDIE(Children[j]);
2300 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002301
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002302 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002303 }
2304 }
2305
Jim Laskey65195462006-10-30 13:35:07 +00002306 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2307 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002308 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2309 // Get the children.
2310 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002311
Jim Laskeyef42a012006-11-02 20:12:39 +00002312 // If not last sibling and has children then add sibling offset attribute.
2313 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2314
2315 // Record the abbreviation.
2316 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002317
Jim Laskeyef42a012006-11-02 20:12:39 +00002318 // Get the abbreviation for this DIE.
2319 unsigned AbbrevNumber = Die->getAbbrevNumber();
2320 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2321
2322 // Set DIE offset
2323 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002324
Jim Laskeyef42a012006-11-02 20:12:39 +00002325 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002326 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2327
Owen Anderson873e1b52008-06-24 21:44:59 +00002328 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2329 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002330
2331 // Size the DIE attribute values.
2332 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2333 // Size attribute value.
2334 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2335 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002336
Jim Laskeyef42a012006-11-02 20:12:39 +00002337 // Size the DIE children if any.
2338 if (!Children.empty()) {
2339 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2340 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002341
Jim Laskeyef42a012006-11-02 20:12:39 +00002342 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2343 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2344 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002345
Jim Laskeyef42a012006-11-02 20:12:39 +00002346 // End of children marker.
2347 Offset += sizeof(int8_t);
2348 }
2349
2350 Die->setSize(Offset - Die->getOffset());
2351 return Offset;
2352 }
Jim Laskey65195462006-10-30 13:35:07 +00002353
2354 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2355 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002356 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002357 // Process base compile unit.
Devang Pateldd9db662009-01-30 18:20:31 +00002358 if (MainCU) {
2359 // Compute size of compile unit header
2360 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2361 sizeof(int16_t) + // DWARF version number
2362 sizeof(int32_t) + // Offset Into Abbrev. Section
2363 sizeof(int8_t); // Pointer Size (in bytes)
2364 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
2365 return;
2366 }
Evan Chenge3d42322009-02-25 07:04:34 +00002367 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2368 CompileUnit *Unit = CompileUnits[i];
Devang Patel72b66352009-01-12 23:05:55 +00002369 // Compute size of compile unit header
2370 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2371 sizeof(int16_t) + // DWARF version number
2372 sizeof(int32_t) + // Offset Into Abbrev. Section
2373 sizeof(int8_t); // Pointer Size (in bytes)
2374 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2375 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002376 }
2377
Evan Chenge3d42322009-02-25 07:04:34 +00002378 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Jim Laskey65195462006-10-30 13:35:07 +00002379 ///
Evan Chenge3d42322009-02-25 07:04:34 +00002380 void EmitDebugInfoPerCU(CompileUnit *Unit) {
2381 DIE *Die = Unit->getDie();
2382 // Emit the compile units header.
2383 EmitLabel("info_begin", Unit->getID());
2384 // Emit size of content not including length itself
2385 unsigned ContentSize = Die->getSize() +
2386 sizeof(int16_t) + // DWARF version number
2387 sizeof(int32_t) + // Offset Into Abbrev. Section
2388 sizeof(int8_t) + // Pointer Size (in bytes)
2389 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2390
2391 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2392 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2393 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2394 Asm->EOL("Offset Into Abbrev. Section");
2395 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2396
2397 EmitDIE(Die);
2398 // FIXME - extra padding for gdb bug.
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 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2403 EmitLabel("info_end", Unit->getID());
2404
2405 Asm->EOL();
2406 }
2407
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002408 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002409 // Start debug info section.
2410 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002411
Evan Chenge3d42322009-02-25 07:04:34 +00002412 if (MainCU) {
2413 EmitDebugInfoPerCU(MainCU);
2414 return;
Devang Patel72b66352009-01-12 23:05:55 +00002415 }
Evan Chenge3d42322009-02-25 07:04:34 +00002416
2417 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2418 EmitDebugInfoPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002419 }
2420
Jim Laskey65195462006-10-30 13:35:07 +00002421 /// EmitAbbreviations - Emit the abbreviation section.
2422 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002423 void EmitAbbreviations() const {
2424 // Check to see if it is worth the effort.
2425 if (!Abbreviations.empty()) {
2426 // Start the debug abbrev section.
2427 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002428
Jim Laskeyef42a012006-11-02 20:12:39 +00002429 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002430
Jim Laskeyef42a012006-11-02 20:12:39 +00002431 // For each abbrevation.
2432 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2433 // Get abbreviation data
2434 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002435
Jim Laskeyef42a012006-11-02 20:12:39 +00002436 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002437 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2438 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002439
Jim Laskeyef42a012006-11-02 20:12:39 +00002440 // Emit the abbreviations data.
2441 Abbrev->Emit(*this);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002442
Jim Laskeybacd3042007-02-21 22:48:45 +00002443 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002444 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002445
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002446 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00002447 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002448
Jim Laskeyef42a012006-11-02 20:12:39 +00002449 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002450
Jim Laskeybacd3042007-02-21 22:48:45 +00002451 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002452 }
2453 }
2454
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002455 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2456 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002457 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002458 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2459 // Define last address of section.
2460 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2461 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2462 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2463 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2464
2465 // Mark end of matrix.
2466 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2467 Asm->EmitULEB128Bytes(1); Asm->EOL();
2468 Asm->EmitInt8(1); Asm->EOL();
2469 }
2470
Jim Laskey65195462006-10-30 13:35:07 +00002471 /// EmitDebugLines - Emit source line information.
2472 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002473 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002474 // If the target is using .loc/.file, the assembler will be emitting the
2475 // .debug_line table automatically.
2476 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00002477 return;
2478
Jim Laskeyef42a012006-11-02 20:12:39 +00002479 // Minimum line delta, thus ranging from -10..(255-10).
2480 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2481 // Maximum line delta, thus ranging from -10..(255-10).
2482 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002483
Jim Laskeyef42a012006-11-02 20:12:39 +00002484 // Start the dwarf line section.
2485 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002486
Jim Laskeyef42a012006-11-02 20:12:39 +00002487 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002488
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002489 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002490 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002491 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002492
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002493 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002494
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002495 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002496 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002497 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002498
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002499 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002500
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002501 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002502
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002503 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002504
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002505 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002506
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002507 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002508
Jim Laskeyef42a012006-11-02 20:12:39 +00002509 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002510 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2511 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2512 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2513 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2514 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2515 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2516 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2517 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2518 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00002519
Jim Laskeyef42a012006-11-02 20:12:39 +00002520 // Emit directories.
Evan Chenge3d42322009-02-25 07:04:34 +00002521 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2522 Asm->EmitString(getSourceDirectoryName(DI));
2523 Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00002524 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002525 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002526
Jim Laskeyef42a012006-11-02 20:12:39 +00002527 // Emit files.
Evan Chenge3d42322009-02-25 07:04:34 +00002528 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2529 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002530 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Evan Chenge3d42322009-02-25 07:04:34 +00002531 Asm->EmitString(getSourceFileName(Id.second));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002532 Asm->EOL("Source");
Evan Chenge3d42322009-02-25 07:04:34 +00002533 Asm->EmitULEB128Bytes(Id.first);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002534 Asm->EOL("Directory #");
2535 Asm->EmitULEB128Bytes(0);
2536 Asm->EOL("Mod date");
2537 Asm->EmitULEB128Bytes(0);
2538 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002539 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002540 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002541
Jim Laskeyef42a012006-11-02 20:12:39 +00002542 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002543
Jim Laskeyef42a012006-11-02 20:12:39 +00002544 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002545 unsigned SecSrcLinesSize = SectionSourceLines.size();
2546
2547 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002548 // Isolate current sections line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00002549 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00002550
Evan Cheng42bf74b2009-03-25 01:47:28 +00002551 if (Asm->isVerbose()) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002552 const Section* S = SectionMap[j + 1];
Evan Chenge3d42322009-02-25 07:04:34 +00002553 O << '\t' << TAI->getCommentString() << " Section"
2554 << S->getName() << '\n';
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002555 } else
Evan Cheng6547e402008-07-01 23:18:29 +00002556 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002557
2558 // Dwarf assumes we start with first line of first source file.
2559 unsigned Source = 1;
2560 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002561
Jim Laskeyef42a012006-11-02 20:12:39 +00002562 // Construct rows of the address, source, line, column matrix.
2563 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patelf3ee5142009-01-12 22:54:42 +00002564 const SrcLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002565 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00002566 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002567
Evan Cheng42bf74b2009-03-25 01:47:28 +00002568 if (!Asm->isVerbose())
Evan Cheng6547e402008-07-01 23:18:29 +00002569 Asm->EOL();
Evan Chenge3d42322009-02-25 07:04:34 +00002570 else {
2571 std::pair<unsigned, unsigned> SourceID =
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002572 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Evan Chenge3d42322009-02-25 07:04:34 +00002573 O << '\t' << TAI->getCommentString() << ' '
2574 << getSourceDirectoryName(SourceID.first) << ' '
2575 << getSourceFileName(SourceID.second)
2576 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2577 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002578
2579 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002580 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00002581 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002582 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00002583 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002584
Jim Laskeyef42a012006-11-02 20:12:39 +00002585 // If change of source, then switch to the new source.
2586 if (Source != LineInfo.getSourceID()) {
2587 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002588 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2589 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00002590 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002591
Jim Laskeyef42a012006-11-02 20:12:39 +00002592 // If change of line.
2593 if (Line != LineInfo.getLine()) {
2594 // Determine offset.
2595 int Offset = LineInfo.getLine() - Line;
2596 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002597
Jim Laskeyef42a012006-11-02 20:12:39 +00002598 // Update line.
2599 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002600
Jim Laskeyef42a012006-11-02 20:12:39 +00002601 // If delta is small enough and in range...
2602 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2603 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002604 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00002605 } else {
2606 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002607 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2608 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2609 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002610 }
2611 } else {
2612 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002613 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002614 }
2615 }
2616
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002617 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002618 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002619
2620 if (SecSrcLinesSize == 0)
2621 // Because we're emitting a debug_line section, we still need a line
2622 // table. The linker and friends expect it to exist. If there's nothing to
2623 // put into it, emit an empty table.
2624 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002625
Jim Laskeyef42a012006-11-02 20:12:39 +00002626 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002627
Jim Laskeybacd3042007-02-21 22:48:45 +00002628 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002629 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002630
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002631 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00002632 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002633 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002634 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00002635 return;
2636
2637 int stackGrowth =
2638 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2639 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002640 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00002641
2642 // Start the dwarf frame section.
2643 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2644
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002645 EmitLabel("debug_frame_common", 0);
2646 EmitDifference("debug_frame_common_end", 0,
2647 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002648 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002649
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002650 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002651 Asm->EmitInt32((int)DW_CIE_ID);
2652 Asm->EOL("CIE Identifier Tag");
2653 Asm->EmitInt8(DW_CIE_VERSION);
2654 Asm->EOL("CIE Version");
2655 Asm->EmitString("");
2656 Asm->EOL("CIE Augmentation");
2657 Asm->EmitULEB128Bytes(1);
2658 Asm->EOL("CIE Code Alignment Factor");
2659 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002660 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002661 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002662 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002663
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002664 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002665 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002666
Dale Johannesenb97aec62007-11-13 19:13:01 +00002667 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002668
Evan Cheng05548eb2008-02-29 19:36:59 +00002669 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002670 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002671
Jim Laskeybacd3042007-02-21 22:48:45 +00002672 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002673 }
2674
Jim Laskey65195462006-10-30 13:35:07 +00002675 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2676 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002677 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002678 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00002679 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002680
Jim Laskeyef42a012006-11-02 20:12:39 +00002681 // Start the dwarf frame section.
2682 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002683
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002684 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2685 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002686 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002687
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002688 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002689
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002690 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2691 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002692 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002693
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002694 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002695 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002696 EmitDifference("func_end", DebugFrameInfo.Number,
2697 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002698 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002699
Devang Patel5aac3d32009-01-17 08:01:33 +00002700 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00002701 false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002702
Evan Cheng05548eb2008-02-29 19:36:59 +00002703 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002704 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00002705
Jim Laskeybacd3042007-02-21 22:48:45 +00002706 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002707 }
2708
Evan Chenge3d42322009-02-25 07:04:34 +00002709 void EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2710 EmitDifference("pubnames_end", Unit->getID(),
2711 "pubnames_begin", Unit->getID(), true);
2712 Asm->EOL("Length of Public Names Info");
2713
2714 EmitLabel("pubnames_begin", Unit->getID());
2715
2716 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2717
2718 EmitSectionOffset("info_begin", "section_info",
2719 Unit->getID(), 0, true, false);
2720 Asm->EOL("Offset of Compilation Unit Info");
2721
2722 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2723 true);
2724 Asm->EOL("Compilation Unit Length");
2725
Bill Wendling972bbac2009-04-09 21:49:15 +00002726 StringMap<DIE*> &Globals = Unit->getGlobals();
Bill Wendlingf34be822009-04-09 23:51:31 +00002727 for (StringMap<DIE*>::const_iterator
Bill Wendling972bbac2009-04-09 21:49:15 +00002728 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
Bill Wendlingf34be822009-04-09 23:51:31 +00002729 const char *Name = GI->getKeyData();
Evan Chenge3d42322009-02-25 07:04:34 +00002730 DIE * Entity = GI->second;
2731
2732 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
Bill Wendlingf34be822009-04-09 23:51:31 +00002733 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
Evan Chenge3d42322009-02-25 07:04:34 +00002734 }
2735
2736 Asm->EmitInt32(0); Asm->EOL("End Mark");
2737 EmitLabel("pubnames_end", Unit->getID());
2738
2739 Asm->EOL();
2740 }
2741
Jim Laskeyef42a012006-11-02 20:12:39 +00002742 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002743 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002744 void EmitDebugPubNames() {
2745 // Start the dwarf pubnames section.
2746 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002747
Evan Chenge3d42322009-02-25 07:04:34 +00002748 if (MainCU) {
2749 EmitDebugPubNamesPerCU(MainCU);
2750 return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002751 }
Evan Chenge3d42322009-02-25 07:04:34 +00002752
2753 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2754 EmitDebugPubNamesPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002755 }
2756
2757 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002758 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002759 void EmitDebugStr() {
2760 // Check to see if it is worth the effort.
2761 if (!StringPool.empty()) {
2762 // Start the dwarf str section.
2763 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002764
Jim Laskeyef42a012006-11-02 20:12:39 +00002765 // For each of strings in the string pool.
2766 for (unsigned StringID = 1, N = StringPool.size();
2767 StringID <= N; ++StringID) {
2768 // Emit a label for reference from debug information entries.
2769 EmitLabel("string", StringID);
2770 // Emit the string itself.
2771 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00002772 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002773 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002774
Jim Laskeybacd3042007-02-21 22:48:45 +00002775 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002776 }
2777 }
2778
2779 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002780 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002781 void EmitDebugLoc() {
2782 // Start the dwarf loc section.
2783 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002784
Jim Laskeybacd3042007-02-21 22:48:45 +00002785 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002786 }
2787
2788 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002789 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002790 void EmitDebugARanges() {
2791 // Start the dwarf aranges section.
2792 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002793
Jim Laskeyef42a012006-11-02 20:12:39 +00002794 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00002795#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002796 CompileUnit *Unit = GetBaseCompileUnit();
2797
Jim Laskey5496f012006-11-09 14:52:14 +00002798 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002799 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002800
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002801 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002802
Jim Laskey5496f012006-11-09 14:52:14 +00002803 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002804 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002805
Dan Gohman82482942007-09-27 23:12:31 +00002806 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002807
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002808 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002809
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002810 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2811 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002812
Jim Laskey5496f012006-11-09 14:52:14 +00002813 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002814 EmitReference("text_begin", 0); Asm->EOL("Address");
2815 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002816
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002817 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2818 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00002819#endif
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002820
Jim Laskeybacd3042007-02-21 22:48:45 +00002821 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002822 }
2823
2824 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002825 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002826 void EmitDebugRanges() {
2827 // Start the dwarf ranges section.
2828 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002829
Jim Laskeybacd3042007-02-21 22:48:45 +00002830 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002831 }
2832
2833 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002834 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002835 void EmitDebugMacInfo() {
Scott Michel210de722009-01-26 22:32:51 +00002836 if (TAI->doesSupportMacInfoSection()) {
2837 // Start the dwarf macinfo section.
2838 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002839
Scott Michel210de722009-01-26 22:32:51 +00002840 Asm->EOL();
2841 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002842 }
2843
Devang Patel0f7fef32009-04-13 17:02:03 +00002844 /// EmitDebugInlineInfo - Emit inline info using following format.
2845 /// Section Header:
2846 /// 1. length of section
2847 /// 2. Dwarf version number
2848 /// 3. address size.
2849 ///
2850 /// Entries (one "entry" for each function that was inlined):
2851 ///
2852 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
2853 /// otherwise offset into __debug_str for regular function name.
2854 /// 2. offset into __debug_str section for regular function name.
2855 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
2856 /// instances for the function.
2857 ///
2858 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
2859 /// inlined instance; the die_offset points to the inlined_subroutine die in
2860 /// the __debug_info section, and the low_pc is the starting address for the
2861 /// inlining instance.
2862 void EmitDebugInlineInfo() {
2863 if (!TAI->doesDwarfUsesInlineInfoSection())
2864 return;
2865
2866 if (!MainCU)
2867 return;
2868
2869 Asm->SwitchToDataSection(TAI->getDwarfDebugInlineSection());
2870 Asm->EOL();
2871 EmitDifference("debug_inlined_end", 1,
2872 "debug_inlined_begin", 1, true);
2873 Asm->EOL("Length of Debug Inlined Information Entry");
2874
2875 EmitLabel("debug_inlined_begin", 1);
2876
2877 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
2878 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2879
2880 for (DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
2881 I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2882 GlobalVariable *GV = I->first;
2883 SmallVector<unsigned, 4> &Labels = I->second;
2884 DISubprogram SP(GV);
2885 std::string Name;
2886 std::string LName;
2887
2888 SP.getLinkageName(LName);
2889 SP.getName(Name);
2890
2891 Asm->EmitString(LName.empty() ? Name : LName);
2892 Asm->EOL("MIPS linkage name");
2893
2894 Asm->EmitString(Name); Asm->EOL("Function name");
2895
2896 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2897
2898 for (SmallVector<unsigned, 4>::iterator LI = Labels.begin(),
2899 LE = Labels.end(); LI != LE; ++LI) {
2900 DIE *SP = MainCU->getDieMapSlotFor(GV);
2901 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2902
2903 if (TD->getPointerSize() == sizeof(int32_t))
2904 O << TAI->getData32bitsDirective();
2905 else
2906 O << TAI->getData64bitsDirective();
2907 PrintLabelName("label", *LI); Asm->EOL("low_pc");
2908 }
2909 }
2910
2911 EmitLabel("debug_inlined_end", 1);
2912 Asm->EOL();
2913 }
2914
Bill Wendlingc8615e42009-03-10 21:47:45 +00002915 /// GetOrCreateSourceID - Look up the source id with the given directory and
2916 /// source file names. If none currently exists, create a new id and insert it
2917 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
2918 /// as well.
2919 unsigned GetOrCreateSourceID(const std::string &DirName,
2920 const std::string &FileName) {
2921 unsigned DId;
2922 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
2923 if (DI != DirectoryIdMap.end()) {
2924 DId = DI->getValue();
2925 } else {
2926 DId = DirectoryNames.size() + 1;
2927 DirectoryIdMap[DirName] = DId;
2928 DirectoryNames.push_back(DirName);
2929 }
2930
2931 unsigned FId;
2932 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
2933 if (FI != SourceFileIdMap.end()) {
2934 FId = FI->getValue();
2935 } else {
2936 FId = SourceFileNames.size() + 1;
2937 SourceFileIdMap[FileName] = FId;
2938 SourceFileNames.push_back(FileName);
2939 }
2940
2941 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
2942 SourceIdMap.find(std::make_pair(DId, FId));
2943 if (SI != SourceIdMap.end())
2944 return SI->second;
2945
2946 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
2947 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
2948 SourceIds.push_back(std::make_pair(DId, FId));
2949
2950 return SrcId;
2951 }
2952
Evan Chenge3d42322009-02-25 07:04:34 +00002953 void ConstructCompileUnit(GlobalVariable *GV) {
2954 DICompileUnit DIUnit(GV);
Bill Wendling0582ae92009-03-13 04:39:26 +00002955 std::string Dir, FN, Prod;
2956 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
2957 DIUnit.getFilename(FN));
Evan Chenge3d42322009-02-25 07:04:34 +00002958
2959 DIE *Die = new DIE(DW_TAG_compile_unit);
2960 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2961 DWLabel("section_line", 0), DWLabel("section_line", 0),
2962 false);
Bill Wendling0582ae92009-03-13 04:39:26 +00002963 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer(Prod));
Evan Chenge3d42322009-02-25 07:04:34 +00002964 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002965 AddString(Die, DW_AT_name, DW_FORM_string, FN);
Bill Wendling0582ae92009-03-13 04:39:26 +00002966 if (!Dir.empty())
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002967 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Chenge3d42322009-02-25 07:04:34 +00002968 if (DIUnit.isOptimized())
2969 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendling0582ae92009-03-13 04:39:26 +00002970 std::string Flags;
2971 DIUnit.getFlags(Flags);
2972 if (!Flags.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00002973 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
2974 unsigned RVer = DIUnit.getRunTimeVersion();
2975 if (RVer)
2976 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
2977
2978 CompileUnit *Unit = new CompileUnit(ID, Die);
2979 if (DIUnit.isMain()) {
2980 assert(!MainCU && "Multiple main compile units are found!");
2981 MainCU = Unit;
2982 }
2983 CompileUnitMap[DIUnit.getGV()] = Unit;
2984 CompileUnits.push_back(Unit);
2985 }
2986
Devang Patelc4523242009-01-05 23:11:11 +00002987 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Pateld1ca9252009-01-05 23:03:32 +00002988 void ConstructCompileUnits() {
Evan Chenge3d42322009-02-25 07:04:34 +00002989 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.compile_units");
2990 if (!Root)
2991 return;
2992 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2993 "Malformed compile unit descriptor anchor type");
2994 Constant *RootC = cast<Constant>(*Root->use_begin());
2995 assert(RootC->hasNUsesOrMore(1) &&
2996 "Malformed compile unit descriptor anchor type");
2997 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2998 UI != UE; ++UI)
2999 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3000 UUI != UUE; ++UUI) {
3001 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3002 ConstructCompileUnit(GV);
Devang Pateldd9db662009-01-30 18:20:31 +00003003 }
Evan Chenge3d42322009-02-25 07:04:34 +00003004 }
3005
3006 bool ConstructGlobalVariableDIE(GlobalVariable *GV) {
3007 DIGlobalVariable DI_GV(GV);
3008 CompileUnit *DW_Unit = MainCU;
3009 if (!DW_Unit)
3010 DW_Unit = FindCompileUnit(DI_GV.getCompileUnit());
3011
3012 // Check for pre-existence.
3013 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
3014 if (Slot)
3015 return false;
3016
3017 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
3018
3019 // Add address.
3020 DIEBlock *Block = new DIEBlock();
3021 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Bill Wendling7d16e852009-04-10 00:12:49 +00003022 std::string GLN;
Evan Chenge3d42322009-02-25 07:04:34 +00003023 AddObjectLabel(Block, 0, DW_FORM_udata,
Bill Wendling7d16e852009-04-10 00:12:49 +00003024 Asm->getGlobalLinkName(DI_GV.getGlobal(), GLN));
Evan Chenge3d42322009-02-25 07:04:34 +00003025 AddBlock(VariableDie, DW_AT_location, 0, Block);
3026
3027 // Add to map.
3028 Slot = VariableDie;
3029 // Add to context owner.
3030 DW_Unit->getDie()->AddChild(VariableDie);
3031 // Expose as global. FIXME - need to check external flag.
Bill Wendling0582ae92009-03-13 04:39:26 +00003032 std::string Name;
3033 DW_Unit->AddGlobal(DI_GV.getName(Name), VariableDie);
Evan Chenge3d42322009-02-25 07:04:34 +00003034 return true;
Devang Pateld1ca9252009-01-05 23:03:32 +00003035 }
3036
Devang Patelc4523242009-01-05 23:11:11 +00003037 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patel53cac182009-02-24 00:02:15 +00003038 /// visible global variables. Return true if at least one global DIE is
3039 /// created.
3040 bool ConstructGlobalVariableDIEs() {
Evan Chenge3d42322009-02-25 07:04:34 +00003041 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
3042 if (!Root)
3043 return false;
Devang Patelc4523242009-01-05 23:11:11 +00003044
Evan Chenge3d42322009-02-25 07:04:34 +00003045 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3046 "Malformed global variable descriptor anchor type");
3047 Constant *RootC = cast<Constant>(*Root->use_begin());
3048 assert(RootC->hasNUsesOrMore(1) &&
3049 "Malformed global variable descriptor anchor type");
Devang Patelc4523242009-01-05 23:11:11 +00003050
Evan Chenge3d42322009-02-25 07:04:34 +00003051 bool Result = false;
3052 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3053 UI != UE; ++UI)
3054 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3055 UUI != UUE; ++UUI) {
3056 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3057 Result |= ConstructGlobalVariableDIE(GV);
3058 }
3059 return Result;
3060 }
Devang Patelc4523242009-01-05 23:11:11 +00003061
Evan Chenge3d42322009-02-25 07:04:34 +00003062 bool ConstructSubprogram(GlobalVariable *GV) {
3063 DISubprogram SP(GV);
3064 CompileUnit *Unit = MainCU;
3065 if (!Unit)
3066 Unit = FindCompileUnit(SP.getCompileUnit());
Devang Patelc4523242009-01-05 23:11:11 +00003067
Evan Chenge3d42322009-02-25 07:04:34 +00003068 // Check for pre-existence.
3069 DIE *&Slot = Unit->getDieMapSlotFor(GV);
3070 if (Slot)
3071 return false;
3072
3073 if (!SP.isDefinition())
3074 // This is a method declaration which will be handled while
3075 // constructing class type.
3076 return false;
3077
3078 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
3079
3080 // Add to map.
3081 Slot = SubprogramDie;
3082 // Add to context owner.
3083 Unit->getDie()->AddChild(SubprogramDie);
3084 // Expose as global.
Bill Wendling0582ae92009-03-13 04:39:26 +00003085 std::string Name;
3086 Unit->AddGlobal(SP.getName(Name), SubprogramDie);
Evan Chenge3d42322009-02-25 07:04:34 +00003087 return true;
Devang Patelc4523242009-01-05 23:11:11 +00003088 }
3089
Devang Patel78eb6ad2009-01-05 23:21:35 +00003090 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patel53cac182009-02-24 00:02:15 +00003091 /// subprograms. Return true if at least one subprogram DIE is created.
3092 bool ConstructSubprograms() {
Evan Chenge3d42322009-02-25 07:04:34 +00003093 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
3094 if (!Root)
3095 return false;
Devang Patel78eb6ad2009-01-05 23:21:35 +00003096
Evan Chenge3d42322009-02-25 07:04:34 +00003097 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3098 "Malformed subprogram descriptor anchor type");
3099 Constant *RootC = cast<Constant>(*Root->use_begin());
3100 assert(RootC->hasNUsesOrMore(1) &&
3101 "Malformed subprogram descriptor anchor type");
Devang Patel78eb6ad2009-01-05 23:21:35 +00003102
Evan Chenge3d42322009-02-25 07:04:34 +00003103 bool Result = false;
3104 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3105 UI != UE; ++UI)
3106 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3107 UUI != UUE; ++UUI) {
3108 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3109 Result |= ConstructSubprogram(GV);
3110 }
3111 return Result;
Devang Patel78eb6ad2009-01-05 23:21:35 +00003112 }
3113
Jim Laskey65195462006-10-30 13:35:07 +00003114public:
Jim Laskeyef42a012006-11-02 20:12:39 +00003115 //===--------------------------------------------------------------------===//
3116 // Main entry points.
3117 //
Owen Andersoncb371882008-08-21 00:14:44 +00003118 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00003119 : Dwarf(OS, A, T, "dbg"), MainCU(0),
3120 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
3121 ValuesSet(InitValuesSetSize), Values(), StringPool(), SectionMap(),
3122 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel7d2f9722009-04-15 20:41:31 +00003123 FunctionDbgScope(0), DebugTimer(0) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003124 if (TimePassesIsEnabled)
3125 DebugTimer = new Timer("Dwarf Debug Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00003126 getDwarfTimerGroup());
Jim Laskeyef42a012006-11-02 20:12:39 +00003127 }
Jim Laskey072200c2007-01-29 18:51:14 +00003128 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003129 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3130 delete Values[j];
Bill Wendling163ba3f2009-03-10 21:23:25 +00003131
3132 delete DebugTimer;
Jim Laskeyef42a012006-11-02 20:12:39 +00003133 }
3134
Bill Wendlingc8615e42009-03-10 21:47:45 +00003135 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
3136 /// be emitted.
3137 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
3138
Devang Patel5d315982009-01-06 21:07:30 +00003139 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3140 /// This is inovked by the target AsmPrinter.
Devang Patel3f7833a2009-01-12 23:09:42 +00003141 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003142 if (TimePassesIsEnabled)
3143 DebugTimer->startTimer();
3144
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003145 // Create all the compile unit DIEs.
3146 ConstructCompileUnits();
Devang Patel3f7833a2009-01-12 23:09:42 +00003147
Bill Wendling163ba3f2009-03-10 21:23:25 +00003148 if (CompileUnits.empty()) {
3149 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003150 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003151
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003152 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003153 }
Devang Patel3f7833a2009-01-12 23:09:42 +00003154
Devang Patel53cac182009-02-24 00:02:15 +00003155 // Create DIEs for each of the externally visible global variables.
3156 bool globalDIEs = ConstructGlobalVariableDIEs();
3157
3158 // Create DIEs for each of the externally visible subprograms.
3159 bool subprogramDIEs = ConstructSubprograms();
3160
3161 // If there is not any debug info available for any global variables
3162 // and any subprograms then there is not any debug info to emit.
Bill Wendling163ba3f2009-03-10 21:23:25 +00003163 if (!globalDIEs && !subprogramDIEs) {
3164 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003165 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003166
Devang Patel53cac182009-02-24 00:02:15 +00003167 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003168 }
Devang Patel53cac182009-02-24 00:02:15 +00003169
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003170 MMI = mmi;
3171 shouldEmit = true;
3172 MMI->setDebugInfoAvailability(true);
Devang Patel5d315982009-01-06 21:07:30 +00003173
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003174 // Prime section data.
3175 SectionMap.insert(TAI->getTextSection());
Devang Patel5d315982009-01-06 21:07:30 +00003176
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003177 // Print out .file directives to specify files for .loc directives. These
3178 // are printed out early so that they precede any .loc directives.
3179 if (TAI->hasDotLocAndDotFile()) {
Evan Chenge3d42322009-02-25 07:04:34 +00003180 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
3181 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00003182 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Evan Chenge3d42322009-02-25 07:04:34 +00003183 sys::Path FullPath(getSourceDirectoryName(Id.first));
3184 bool AppendOk =
3185 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003186 assert(AppendOk && "Could not append filename to directory!");
3187 AppendOk = false;
3188 Asm->EmitFile(i, FullPath.toString());
3189 Asm->EOL();
Devang Patel5d315982009-01-06 21:07:30 +00003190 }
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003191 }
Devang Patel5d315982009-01-06 21:07:30 +00003192
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003193 // Emit initial sections
3194 EmitInitial();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003195
3196 if (TimePassesIsEnabled)
3197 DebugTimer->stopTimer();
Devang Patel5d315982009-01-06 21:07:30 +00003198 }
3199
Jim Laskey65195462006-10-30 13:35:07 +00003200 /// BeginModule - Emit all Dwarf sections that should come prior to the
3201 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00003202 void BeginModule(Module *M) {
3203 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00003204 }
3205
Jim Laskey65195462006-10-30 13:35:07 +00003206 /// EndModule - Emit all Dwarf sections that should come after the content.
3207 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003208 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003209 if (!ShouldEmitDwarfDebug())
3210 return;
3211
3212 if (TimePassesIsEnabled)
3213 DebugTimer->startTimer();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003214
Jim Laskeyef42a012006-11-02 20:12:39 +00003215 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003216 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003217 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00003218 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003219 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003220
Jim Laskeyef42a012006-11-02 20:12:39 +00003221 // End text sections.
3222 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003223 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00003224 EmitLabel("section_end", i);
3225 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003226
3227 // Emit common frame information.
3228 EmitCommonDebugFrame();
3229
3230 // Emit function debug frame information
3231 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3232 E = DebugFrames.end(); I != E; ++I)
3233 EmitFunctionDebugFrame(*I);
3234
Jim Laskeyef42a012006-11-02 20:12:39 +00003235 // Compute DIE offsets and sizes.
3236 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003237
Jim Laskeyef42a012006-11-02 20:12:39 +00003238 // Emit all the DIEs into a debug info section
3239 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003240
Jim Laskeyef42a012006-11-02 20:12:39 +00003241 // Corresponding abbreviations into a abbrev section.
3242 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003243
Jim Laskeyef42a012006-11-02 20:12:39 +00003244 // Emit source line correspondence into a debug line section.
3245 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003246
Jim Laskeyef42a012006-11-02 20:12:39 +00003247 // Emit info into a debug pubnames section.
3248 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003249
Jim Laskeyef42a012006-11-02 20:12:39 +00003250 // Emit info into a debug str section.
3251 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003252
Jim Laskeyef42a012006-11-02 20:12:39 +00003253 // Emit info into a debug loc section.
3254 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003255
Jim Laskeyef42a012006-11-02 20:12:39 +00003256 // Emit info into a debug aranges section.
3257 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003258
Jim Laskeyef42a012006-11-02 20:12:39 +00003259 // Emit info into a debug ranges section.
3260 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003261
Jim Laskeyef42a012006-11-02 20:12:39 +00003262 // Emit info into a debug macinfo section.
3263 EmitDebugMacInfo();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003264
Devang Patel0f7fef32009-04-13 17:02:03 +00003265 // Emit inline info.
3266 EmitDebugInlineInfo();
3267
Bill Wendling163ba3f2009-03-10 21:23:25 +00003268 if (TimePassesIsEnabled)
3269 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003270 }
3271
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003272 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00003273 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00003274 void BeginFunction(MachineFunction *MF) {
Bill Wendling1362f972009-03-11 00:03:50 +00003275 this->MF = MF;
3276
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003277 if (!ShouldEmitDwarfDebug()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00003278
Bill Wendling163ba3f2009-03-10 21:23:25 +00003279 if (TimePassesIsEnabled)
3280 DebugTimer->startTimer();
3281
Jim Laskeyef42a012006-11-02 20:12:39 +00003282 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003283 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003284
Jim Laskeyef42a012006-11-02 20:12:39 +00003285 // Assumes in correct section after the entry point.
3286 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00003287
3288 // Emit label for the implicitly defined dbg.stoppoint at the start of
3289 // the function.
Devang Patelf3ee5142009-01-12 22:54:42 +00003290 if (!Lines.empty()) {
3291 const SrcLineInfo &LineInfo = Lines[0];
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00003292 Asm->printLabel(LineInfo.getLabelID());
3293 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003294
3295 if (TimePassesIsEnabled)
3296 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003297 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003298
Jim Laskey65195462006-10-30 13:35:07 +00003299 /// EndFunction - Gather and emit post-function debug information.
3300 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00003301 void EndFunction(MachineFunction *MF) {
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003302 if (!ShouldEmitDwarfDebug()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003303
Bill Wendling163ba3f2009-03-10 21:23:25 +00003304 if (TimePassesIsEnabled)
3305 DebugTimer->startTimer();
3306
Jim Laskeyb82313f2007-02-01 16:31:34 +00003307 // Define end label for subprogram.
3308 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003309
Jim Laskeyef42a012006-11-02 20:12:39 +00003310 // Get function line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00003311 if (!Lines.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003312 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003313 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00003314 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patelf3ee5142009-01-12 22:54:42 +00003315 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Jim Laskeyef42a012006-11-02 20:12:39 +00003316 // Append the function info to section info.
3317 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patelf3ee5142009-01-12 22:54:42 +00003318 Lines.begin(), Lines.end());
Jim Laskeyef42a012006-11-02 20:12:39 +00003319 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003320
Jim Laskeyef42a012006-11-02 20:12:39 +00003321 // Construct scopes for subprogram.
Devang Patel7d2f9722009-04-15 20:41:31 +00003322 if (FunctionDbgScope)
3323 ConstructFunctionDbgScope(FunctionDbgScope);
Bill Wendlingd751c642008-09-26 00:28:12 +00003324 else
3325 // FIXME: This is wrong. We are essentially getting past a problem with
3326 // debug information not being able to handle unreachable blocks that have
3327 // debug information in them. In particular, those unreachable blocks that
3328 // have "region end" info in them. That situation results in the "root
3329 // scope" not being created. If that's the case, then emit a "default"
3330 // scope, i.e., one that encompasses the whole function. This isn't
3331 // desirable. And a better way of handling this (and all of the debugging
3332 // information) needs to be explored.
Devang Patel7bb89ed2009-01-13 00:20:51 +00003333 ConstructDefaultDbgScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003334
3335 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3336 MMI->getFrameMoves()));
Devang Patel481ff5b2009-01-12 18:48:36 +00003337
3338 // Clear debug info
Devang Patel7d2f9722009-04-15 20:41:31 +00003339 if (FunctionDbgScope) {
3340 delete FunctionDbgScope;
Devang Patel481ff5b2009-01-12 18:48:36 +00003341 DbgScopeMap.clear();
Devang Patel1be3ecc2009-04-15 00:10:26 +00003342 DbgInlinedScopeMap.clear();
3343 InlinedVariableScopes.clear();
Devang Patel7d2f9722009-04-15 20:41:31 +00003344 FunctionDbgScope = NULL;
Devang Patel481ff5b2009-01-12 18:48:36 +00003345 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003346
Bill Wendling163ba3f2009-03-10 21:23:25 +00003347 Lines.clear();
3348
3349 if (TimePassesIsEnabled)
3350 DebugTimer->stopTimer();
3351 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003352
Devang Patelcf3a4482009-01-15 23:41:32 +00003353 /// ValidDebugInfo - Return true if V represents valid debug info value.
Devang Patel48c7fa22009-04-13 18:13:16 +00003354 bool ValidDebugInfo(Value *V, bool FastISel) {
Devang Patelb79b5352009-01-19 23:21:49 +00003355 if (!V)
3356 return false;
3357
Devang Patel61c6bf32009-01-16 01:49:46 +00003358 if (!shouldEmit)
3359 return false;
3360
Devang Patelcf3a4482009-01-15 23:41:32 +00003361 GlobalVariable *GV = getGlobalVariable(V);
3362 if (!GV)
3363 return false;
Duncan Sands667d4b82009-03-07 15:45:40 +00003364
3365 if (!GV->hasInternalLinkage () && !GV->hasLinkOnceLinkage())
Devang Patelcf3a4482009-01-15 23:41:32 +00003366 return false;
3367
Bill Wendling163ba3f2009-03-10 21:23:25 +00003368 if (TimePassesIsEnabled)
3369 DebugTimer->startTimer();
3370
Devang Patelcf3a4482009-01-15 23:41:32 +00003371 DIDescriptor DI(GV);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003372
Devang Patelcf3a4482009-01-15 23:41:32 +00003373 // Check current version. Allow Version6 for now.
3374 unsigned Version = DI.getVersion();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003375 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6) {
3376 if (TimePassesIsEnabled)
3377 DebugTimer->stopTimer();
3378
Devang Patelcf3a4482009-01-15 23:41:32 +00003379 return false;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003380 }
Devang Patelcf3a4482009-01-15 23:41:32 +00003381
Devang Patelb79b5352009-01-19 23:21:49 +00003382 unsigned Tag = DI.getTag();
3383 switch (Tag) {
3384 case DW_TAG_variable:
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003385 assert(DIVariable(GV).Verify() && "Invalid DebugInfo value");
Devang Patelb79b5352009-01-19 23:21:49 +00003386 break;
3387 case DW_TAG_compile_unit:
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003388 assert(DICompileUnit(GV).Verify() && "Invalid DebugInfo value");
Devang Patelb79b5352009-01-19 23:21:49 +00003389 break;
3390 case DW_TAG_subprogram:
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003391 assert(DISubprogram(GV).Verify() && "Invalid DebugInfo value");
Devang Patelb79b5352009-01-19 23:21:49 +00003392 break;
Devang Patel48c7fa22009-04-13 18:13:16 +00003393 case DW_TAG_lexical_block:
3394 /// FIXME. This interfers with the qualitfy of generated code when
3395 /// during optimization.
3396 if (FastISel == false)
3397 return false;
Devang Patelb79b5352009-01-19 23:21:49 +00003398 default:
3399 break;
3400 }
3401
Bill Wendling163ba3f2009-03-10 21:23:25 +00003402 if (TimePassesIsEnabled)
3403 DebugTimer->stopTimer();
3404
Devang Patelcf3a4482009-01-15 23:41:32 +00003405 return true;
3406 }
3407
Devang Patelccca7fe2009-01-12 19:17:34 +00003408 /// RecordSourceLine - Records location information and associates it with a
3409 /// label. Returns a unique label ID used to generate a label and provide
3410 /// correspondence to the source line list.
3411 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003412 if (TimePassesIsEnabled)
3413 DebugTimer->startTimer();
3414
Evan Chenge3d42322009-02-25 07:04:34 +00003415 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003416 assert(Unit && "Unable to find CompileUnit");
Devang Patelccca7fe2009-01-12 19:17:34 +00003417 unsigned ID = MMI->NextLabelID();
3418 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003419
3420 if (TimePassesIsEnabled)
3421 DebugTimer->stopTimer();
3422
Devang Patelccca7fe2009-01-12 19:17:34 +00003423 return ID;
3424 }
3425
3426 /// RecordSourceLine - Records location information and associates it with a
3427 /// label. Returns a unique label ID used to generate a label and provide
3428 /// correspondence to the source line list.
3429 unsigned RecordSourceLine(unsigned Line, unsigned Col, unsigned Src) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003430 if (TimePassesIsEnabled)
3431 DebugTimer->startTimer();
3432
Devang Patelccca7fe2009-01-12 19:17:34 +00003433 unsigned ID = MMI->NextLabelID();
3434 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003435
3436 if (TimePassesIsEnabled)
3437 DebugTimer->stopTimer();
3438
Devang Patelccca7fe2009-01-12 19:17:34 +00003439 return ID;
3440 }
3441
Bill Wendlingc8615e42009-03-10 21:47:45 +00003442 /// getRecordSourceLineCount - Return the number of source lines in the debug
3443 /// info.
3444 unsigned getRecordSourceLineCount() const {
Devang Patelccca7fe2009-01-12 19:17:34 +00003445 return Lines.size();
3446 }
3447
Bill Wendlingc8615e42009-03-10 21:47:45 +00003448 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
3449 /// timed. Look up the source id with the given directory and source file
3450 /// names. If none currently exists, create a new id and insert it in the
3451 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
3452 /// well.
Evan Chenge3d42322009-02-25 07:04:34 +00003453 unsigned getOrCreateSourceID(const std::string &DirName,
3454 const std::string &FileName) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003455 if (TimePassesIsEnabled)
3456 DebugTimer->startTimer();
3457
Bill Wendlingc8615e42009-03-10 21:47:45 +00003458 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003459
3460 if (TimePassesIsEnabled)
3461 DebugTimer->stopTimer();
3462
Evan Chenge3d42322009-02-25 07:04:34 +00003463 return SrcId;
Devang Patelccca7fe2009-01-12 19:17:34 +00003464 }
3465
3466 /// RecordRegionStart - Indicate the start of a region.
Devang Patelccca7fe2009-01-12 19:17:34 +00003467 unsigned RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003468 if (TimePassesIsEnabled)
3469 DebugTimer->startTimer();
3470
Devang Patelccca7fe2009-01-12 19:17:34 +00003471 DbgScope *Scope = getOrCreateScope(V);
3472 unsigned ID = MMI->NextLabelID();
3473 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003474
3475 if (TimePassesIsEnabled)
3476 DebugTimer->stopTimer();
3477
Devang Patelccca7fe2009-01-12 19:17:34 +00003478 return ID;
3479 }
3480
3481 /// RecordRegionEnd - Indicate the end of a region.
Devang Patelccca7fe2009-01-12 19:17:34 +00003482 unsigned RecordRegionEnd(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003483 if (TimePassesIsEnabled)
3484 DebugTimer->startTimer();
3485
Devang Patelccca7fe2009-01-12 19:17:34 +00003486 DbgScope *Scope = getOrCreateScope(V);
3487 unsigned ID = MMI->NextLabelID();
3488 Scope->setEndLabelID(ID);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003489
3490 if (TimePassesIsEnabled)
3491 DebugTimer->stopTimer();
3492
Devang Patelccca7fe2009-01-12 19:17:34 +00003493 return ID;
3494 }
3495
3496 /// RecordVariable - Indicate the declaration of a local variable.
Devang Patel1be3ecc2009-04-15 00:10:26 +00003497 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
3498 const MachineInstr *MI) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003499 if (TimePassesIsEnabled)
3500 DebugTimer->startTimer();
3501
Devang Patel0e5200f2009-01-15 18:25:17 +00003502 DIDescriptor Desc(GV);
3503 DbgScope *Scope = NULL;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003504
Devang Patel0e5200f2009-01-15 18:25:17 +00003505 if (Desc.getTag() == DW_TAG_variable) {
3506 // GV is a global variable.
3507 DIGlobalVariable DG(GV);
3508 Scope = getOrCreateScope(DG.getContext().getGV());
3509 } else {
Devang Patel1be3ecc2009-04-15 00:10:26 +00003510 DenseMap<const MachineInstr *, DbgScope *>::iterator
3511 SI = InlinedVariableScopes.find(MI);
3512 if (SI != InlinedVariableScopes.end()) {
3513 // or GV is an inlined local variable.
3514 Scope = SI->second;
3515 } else {
Devang Patel0e5200f2009-01-15 18:25:17 +00003516 // or GV is a local variable.
Devang Patel1be3ecc2009-04-15 00:10:26 +00003517 DIVariable DV(GV);
3518 Scope = getOrCreateScope(DV.getContext().getGV());
3519 }
Devang Patel0e5200f2009-01-15 18:25:17 +00003520 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003521
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003522 assert(Scope && "Unable to find variable' scope");
Devang Patel99ec3532009-01-16 19:28:14 +00003523 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelccca7fe2009-01-12 19:17:34 +00003524 Scope->AddVariable(DV);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003525
3526 if (TimePassesIsEnabled)
3527 DebugTimer->stopTimer();
Devang Patelccca7fe2009-01-12 19:17:34 +00003528 }
Devang Patel0f7fef32009-04-13 17:02:03 +00003529
Devang Patel1be3ecc2009-04-15 00:10:26 +00003530 //// RecordInlinedFnStart - Indicate the start of inlined subroutine.
3531 void RecordInlinedFnStart(Instruction *FSI, DISubprogram &SP, unsigned LabelID,
3532 unsigned Src, unsigned Line, unsigned Col) {
3533 if (!TAI->doesDwarfUsesInlineInfoSection())
3534 return;
3535
3536 DbgScope *Scope = createInlinedSubroutineScope(SP, Src, Line, Col);
3537 Scope->setStartLabelID(LabelID);
Devang Patel0f7fef32009-04-13 17:02:03 +00003538 MMI->RecordUsedDbgLabel(LabelID);
Devang Patel1be3ecc2009-04-15 00:10:26 +00003539 GlobalVariable *GV = SP.getGV();
3540
3541 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3542 SI = DbgInlinedScopeMap.find(GV);
3543 if (SI == DbgInlinedScopeMap.end()) {
3544 SmallVector<DbgScope *, 2> Scopes;
3545 Scopes.push_back(Scope);
3546 DbgInlinedScopeMap[GV] = Scopes;
3547 } else {
3548 SmallVector<DbgScope *, 2> &Scopes = SI->second;
3549 Scopes.push_back(Scope);
3550 }
3551
Devang Patel0f7fef32009-04-13 17:02:03 +00003552 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
3553 I = InlineInfo.find(GV);
3554 if (I == InlineInfo.end()) {
3555 SmallVector<unsigned, 4> Labels;
3556 Labels.push_back(LabelID);
3557 InlineInfo[GV] = Labels;
3558 return;
3559 }
3560
3561 SmallVector<unsigned, 4> &Labels = I->second;
3562 Labels.push_back(LabelID);
3563 }
Devang Patel1be3ecc2009-04-15 00:10:26 +00003564
3565 /// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
3566 unsigned RecordInlinedFnEnd(DISubprogram &SP) {
3567 if (!TAI->doesDwarfUsesInlineInfoSection())
3568 return 0;
3569
3570 GlobalVariable *GV = SP.getGV();
3571 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3572 I = DbgInlinedScopeMap.find(GV);
3573 if (I == DbgInlinedScopeMap.end())
3574 return 0;
3575
3576 SmallVector<DbgScope *, 2> &Scopes = I->second;
3577 DbgScope *Scope = Scopes.back(); Scopes.pop_back();
3578 unsigned ID = MMI->NextLabelID();
3579 MMI->RecordUsedDbgLabel(ID);
3580 Scope->setEndLabelID(ID);
3581 return ID;
3582 }
3583
3584 /// RecordVariableScope - Record scope for the variable declared by
3585 /// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
3586 /// Record scopes for only inlined subroutine variables. Other
3587 /// variables' scopes are determined during RecordVariable().
3588 void RecordVariableScope(DIVariable &DV, const MachineInstr *DeclareMI) {
3589 DISubprogram SP(DV.getContext().getGV());
3590 if (SP.isNull())
3591 return;
3592 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3593 I = DbgInlinedScopeMap.find(SP.getGV());
3594 if (I == DbgInlinedScopeMap.end())
3595 return;
3596
3597 SmallVector<DbgScope *, 2> &Scopes = I->second;
3598 InlinedVariableScopes[DeclareMI] = Scopes.back();
3599 }
3600
Jim Laskey65195462006-10-30 13:35:07 +00003601};
3602
Jim Laskey072200c2007-01-29 18:51:14 +00003603//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003604/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00003605///
3606class DwarfException : public Dwarf {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003607 struct FunctionEHFrameInfo {
3608 std::string FnName;
3609 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003610 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003611 bool hasCalls;
3612 bool hasLandingPads;
3613 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003614 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00003615
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003616 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3617 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003618 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003619 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003620 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003621 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003622 };
3623
3624 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003625
3626 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3627 /// be emitted.
3628 bool shouldEmitTable;
3629
3630 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3631 /// should be emitted.
3632 bool shouldEmitMoves;
3633
3634 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3635 /// should be emitted.
3636 bool shouldEmitTableModule;
3637
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003638 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003639 /// should be emitted.
3640 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00003641
Bill Wendling163ba3f2009-03-10 21:23:25 +00003642 /// ExceptionTimer - Timer for the Dwarf exception writer.
3643 Timer *ExceptionTimer;
3644
Jim Laskey3f09fc22007-02-28 18:38:31 +00003645 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3646 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003647 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003648 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003649 int stackGrowth =
3650 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3651 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003652 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003653
Jim Laskeybacd3042007-02-21 22:48:45 +00003654 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003655 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling722f5f12008-12-24 08:05:17 +00003656
3657 if (!TAI->doesRequireNonLocalEHFrameLabel())
3658 O << TAI->getEHGlobalPrefix();
3659 O << "EH_frame" << Index << ":\n";
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003660 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003661
Jim Laskeybacd3042007-02-21 22:48:45 +00003662 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003663 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003664
Jim Laskeybacd3042007-02-21 22:48:45 +00003665 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003666 EmitDifference("eh_frame_common_end", Index,
3667 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003668 Asm->EOL("Length of Common Information Entry");
3669
Jim Laskeybacd3042007-02-21 22:48:45 +00003670 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003671 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003672 Asm->EmitInt32((int)0);
3673 Asm->EOL("CIE Identifier Tag");
3674 Asm->EmitInt8(DW_CIE_VERSION);
3675 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00003676
Jim Laskeybacd3042007-02-21 22:48:45 +00003677 // The personality presence indicates that language specific information
3678 // will show up in the eh frame.
3679 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00003680 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00003681
Jim Laskeybacd3042007-02-21 22:48:45 +00003682 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003683 Asm->EmitULEB128Bytes(1);
3684 Asm->EOL("CIE Code Alignment Factor");
3685 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00003686 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003687 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00003688 Asm->EOL("CIE Return Address Column");
3689
Jim Laskeybacd3042007-02-21 22:48:45 +00003690 // If there is a personality, we need to indicate the functions location.
3691 if (Personality) {
3692 Asm->EmitULEB128Bytes(7);
3693 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00003694
Duncan Sands671fa972008-05-07 19:11:09 +00003695 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003696 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00003697 Asm->EOL("Personality (pcrel sdata4 indirect)");
3698 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003699 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00003700 Asm->EOL("Personality (pcrel sdata4)");
3701 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00003702
Duncan Sands671fa972008-05-07 19:11:09 +00003703 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00003704 O << TAI->getPersonalityPrefix();
3705 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3706 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00003707 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3708 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00003709 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00003710
Duncan Sands671fa972008-05-07 19:11:09 +00003711 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3712 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003713
Bill Wendlingd60de512009-01-05 22:53:45 +00003714 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3715 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003716 } else {
3717 Asm->EmitULEB128Bytes(1);
3718 Asm->EOL("Augmentation Size");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003719
Bill Wendlingd60de512009-01-05 22:53:45 +00003720 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3721 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003722 }
3723
3724 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003725 std::vector<MachineMove> Moves;
3726 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00003727 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003728
Dale Johannesen21d972a2008-04-30 00:43:29 +00003729 // On Darwin the linker honors the alignment of eh_frame, which means it
3730 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3731 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003732 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003733 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003734 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003735
Jim Laskeybacd3042007-02-21 22:48:45 +00003736 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003737 }
Duncan Sands671fa972008-05-07 19:11:09 +00003738
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003739 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003740 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003741 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003742 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
Chris Lattner266c7bb2009-04-13 05:44:34 +00003743
3744 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
3745 "Should not emit 'available externally' functions at all");
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003746
Jim Laskeybacd3042007-02-21 22:48:45 +00003747 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3748
3749 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003750 // If the corresponding function is static, this should not be
3751 // externally visible.
Rafael Espindolabb46f522009-01-15 20:18:42 +00003752 if (linkage != Function::InternalLinkage &&
Devang Patel2d1768c2009-01-17 08:05:14 +00003753 linkage != Function::PrivateLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003754 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3755 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3756 }
3757
Dale Johannesen038129d2008-01-10 02:03:30 +00003758 // If corresponding function is weak definition, this should be too.
Duncan Sands667d4b82009-03-07 15:45:40 +00003759 if ((linkage == Function::WeakAnyLinkage ||
3760 linkage == Function::WeakODRLinkage ||
3761 linkage == Function::LinkOnceAnyLinkage ||
3762 linkage == Function::LinkOnceODRLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00003763 TAI->getWeakDefDirective())
3764 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3765
3766 // If there are no calls then you can't unwind. This may mean we can
3767 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003768 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00003769 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00003770 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00003771 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00003772 !UnwindTablesMandatory &&
Duncan Sands667d4b82009-03-07 15:45:40 +00003773 ((linkage != Function::WeakAnyLinkage &&
3774 linkage != Function::WeakODRLinkage &&
3775 linkage != Function::LinkOnceAnyLinkage &&
3776 linkage != Function::LinkOnceODRLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00003777 !TAI->getWeakDefDirective() ||
3778 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003779 {
Bill Wendling6e198962007-09-18 01:47:22 +00003780 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003781 // This name has no connection to the function, so it might get
3782 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003783 // dead-stripping unconditionally.
3784 if (const char *UsedDirective = TAI->getUsedDirective())
3785 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00003786 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00003787 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003788
Jim Laskeybacd3042007-02-21 22:48:45 +00003789 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003790 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3791 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003792 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003793
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003794 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003795
Bill Wendling722f5f12008-12-24 08:05:17 +00003796 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3797 PrintRelDirective(true, true);
3798 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3799
3800 if (!TAI->isAbsoluteEHSectionOffsets())
3801 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3802 } else {
3803 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3804 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3805 true, true, false);
3806 }
3807
Jim Laskeybacd3042007-02-21 22:48:45 +00003808 Asm->EOL("FDE CIE offset");
3809
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003810 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003811 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003812 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003813 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003814 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00003815
Jim Laskey3f09fc22007-02-28 18:38:31 +00003816 // If there is a personality and landing pads then point to the language
3817 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003818 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00003819 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00003820 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00003821
3822 if (EHFrameInfo.hasLandingPads)
3823 EmitReference("exception", EHFrameInfo.Number, true, true);
3824 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00003825 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00003826 Asm->EOL("Language Specific Data Area");
3827 } else {
3828 Asm->EmitULEB128Bytes(0);
3829 Asm->EOL("Augmentation size");
3830 }
Duncan Sands671fa972008-05-07 19:11:09 +00003831
Jim Laskeybacd3042007-02-21 22:48:45 +00003832 // Indicate locations of function specific callee saved registers in
3833 // frame.
Devang Patel5aac3d32009-01-17 08:01:33 +00003834 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00003835 true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003836
Dale Johannesen21d972a2008-04-30 00:43:29 +00003837 // On Darwin the linker honors the alignment of eh_frame, which means it
3838 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3839 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003840 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003841 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003842 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003843
3844 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003845 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003846 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003847 // that depends on unused functions (calling undefined externals) being
3848 // dead-stripped to link correctly. Yes, there really is.
3849 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3850 if (const char *UsedDirective = TAI->getUsedDirective())
3851 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3852 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003853 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003854
Duncan Sands57810cd2007-09-05 11:27:52 +00003855 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003856 ///
3857 /// The general organization of the table is complex, but the basic concepts
3858 /// are easy. First there is a header which describes the location and
3859 /// organization of the three components that follow.
3860 /// 1. The landing pad site information describes the range of code covered
3861 /// by the try. In our case it's an accumulation of the ranges covered
3862 /// by the invokes in the try. There is also a reference to the landing
3863 /// pad that handles the exception once processed. Finally an index into
3864 /// the actions table.
3865 /// 2. The action table, in our case, is composed of pairs of type ids
3866 /// and next action offset. Starting with the action index from the
3867 /// landing pad site, each type Id is checked for a match to the current
3868 /// exception. If it matches then the exception and type id are passed
3869 /// on to the landing pad. Otherwise the next action is looked up. This
3870 /// chain is terminated with a next action of zero. If no type id is
3871 /// found the the frame is unwound and handling continues.
3872 /// 3. Type id table contains references to all the C++ typeinfo for all
3873 /// catches in the function. This tables is reversed indexed base 1.
3874
Duncan Sandsb32edb42007-06-06 15:37:31 +00003875 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3876 static unsigned SharedTypeIds(const LandingPadInfo *L,
3877 const LandingPadInfo *R) {
3878 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003879 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003880 unsigned MinSize = LSize < RSize ? LSize : RSize;
3881 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003882
Duncan Sandsb32edb42007-06-06 15:37:31 +00003883 for (; Count != MinSize; ++Count)
3884 if (LIds[Count] != RIds[Count])
3885 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003886
Duncan Sandsb32edb42007-06-06 15:37:31 +00003887 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003888 }
3889
Duncan Sandsb32edb42007-06-06 15:37:31 +00003890 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00003891 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003892 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003893 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003894 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003895
Duncan Sandsb32edb42007-06-06 15:37:31 +00003896 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00003897 if (LIds[i] != RIds[i])
3898 return LIds[i] < RIds[i];
3899
Duncan Sandsb32edb42007-06-06 15:37:31 +00003900 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003901 }
3902
Duncan Sands53c3a332007-05-16 12:12:23 +00003903 struct KeyInfo {
3904 static inline unsigned getEmptyKey() { return -1U; }
3905 static inline unsigned getTombstoneKey() { return -2U; }
3906 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00003907 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00003908 static bool isPod() { return true; }
3909 };
3910
Duncan Sands57810cd2007-09-05 11:27:52 +00003911 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003912 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003913 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003914 int NextAction;
3915 struct ActionEntry *Previous;
3916 };
3917
Duncan Sands57810cd2007-09-05 11:27:52 +00003918 /// PadRange - Structure holding a try-range and the associated landing pad.
3919 struct PadRange {
3920 // The index of the landing pad.
3921 unsigned PadIndex;
3922 // The index of the begin and end labels in the landing pad's label lists.
3923 unsigned RangeIndex;
3924 };
3925
3926 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3927
3928 /// CallSiteEntry - Structure describing an entry in the call-site table.
3929 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00003930 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003931 unsigned BeginLabel; // zero indicates the start of the function.
3932 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00003933 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003934 unsigned PadLabel; // zero indicates that there is no landing pad.
3935 unsigned Action;
3936 };
3937
Jim Laskeybacd3042007-02-21 22:48:45 +00003938 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00003939 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00003940 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00003941 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3942 if (PadInfos.empty()) return;
3943
3944 // Sort the landing pads in order of their type ids. This is used to fold
3945 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00003946 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00003947 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00003948 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00003949 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003950 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3951
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003952 // Negative type ids index into FilterIds, positive type ids index into
3953 // TypeInfos. The value written for a positive type id is just the type
3954 // id itself. For a negative type id, however, the value written is the
3955 // (negative) byte offset of the corresponding FilterIds entry. The byte
3956 // offset is usually equal to the type id, because the FilterIds entries
3957 // are written using a variable width encoding which outputs one byte per
3958 // entry as long as the value written is not too large, but can differ.
3959 // This kind of complication does not occur for positive type ids because
3960 // type infos are output using a fixed width encoding.
3961 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3962 SmallVector<int, 16> FilterOffsets;
3963 FilterOffsets.reserve(FilterIds.size());
3964 int Offset = -1;
3965 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3966 E = FilterIds.end(); I != E; ++I) {
3967 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003968 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003969 }
3970
Duncan Sands57810cd2007-09-05 11:27:52 +00003971 // Compute the actions table and gather the first action index for each
3972 // landing pad site.
3973 SmallVector<ActionEntry, 32> Actions;
3974 SmallVector<unsigned, 64> FirstActions;
3975 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00003976
Duncan Sandsb32edb42007-06-06 15:37:31 +00003977 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00003978 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003979 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003980 const LandingPadInfo *LP = LandingPads[i];
3981 const std::vector<int> &TypeIds = LP->TypeIds;
3982 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003983 unsigned SizeSiteActions = 0;
3984
Duncan Sandsb32edb42007-06-06 15:37:31 +00003985 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00003986 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00003987 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003988
Duncan Sandsb32edb42007-06-06 15:37:31 +00003989 if (NumShared) {
3990 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3991 assert(Actions.size());
3992 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003993 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3994 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003995 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003996 SizeAction -=
3997 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003998 SizeAction += -PrevAction->NextAction;
3999 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004000 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00004001 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00004002
Duncan Sandsb32edb42007-06-06 15:37:31 +00004003 // Compute the actions.
4004 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
4005 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004006 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
4007 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004008 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004009
4010 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004011 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004012 SizeSiteActions += SizeAction;
4013
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004014 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00004015 Actions.push_back(Action);
4016
4017 PrevAction = &Actions.back();
4018 }
4019
4020 // Record the first action of the landing pad site.
4021 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
4022 } // else identical - re-use previous FirstAction
4023
4024 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00004025
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00004026 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00004027 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00004028 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004029
Duncan Sands481dc722007-12-19 07:36:31 +00004030 // Compute the call-site table. The entry for an invoke has a try-range
4031 // containing the call, a non-zero landing pad and an appropriate action.
4032 // The entry for an ordinary call has a try-range containing the call and
4033 // zero for the landing pad and the action. Calls marked 'nounwind' have
4034 // no entry and must not be contained in the try-range of any entry - they
4035 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004036 SmallVector<CallSiteEntry, 64> CallSites;
4037
4038 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00004039 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
4040 // by try-range labels when lowered). Ordinary calls do not, so appropriate
4041 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00004042 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
4043 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00004044 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004045 unsigned BeginLabel = LandingPad->BeginLabels[j];
4046 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
4047 PadRange P = { i, j };
4048 PadMap[BeginLabel] = P;
4049 }
4050 }
4051
Duncan Sands481dc722007-12-19 07:36:31 +00004052 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00004053 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00004054
4055 // Whether there is a potentially throwing instruction (currently this means
4056 // an ordinary call) between the end of the previous try-range and now.
4057 bool SawPotentiallyThrowing = false;
4058
4059 // Whether the last callsite entry was for an invoke.
4060 bool PreviousIsInvoke = false;
4061
Duncan Sands481dc722007-12-19 07:36:31 +00004062 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004063 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
4064 I != E; ++I) {
4065 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
4066 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00004067 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00004068 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00004069 continue;
4070 }
4071
Chris Lattner9e330492007-12-30 20:50:28 +00004072 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00004073 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00004074
Duncan Sands481dc722007-12-19 07:36:31 +00004075 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00004076 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00004077 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004078
Duncan Sands481dc722007-12-19 07:36:31 +00004079 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00004080 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00004081 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00004082 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00004083 continue;
4084
4085 PadRange P = L->second;
4086 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
4087
4088 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
4089 "Inconsistent landing pad map!");
4090
4091 // If some instruction between the previous try-range and this one may
4092 // throw, create a call-site entry with no landing pad for the region
4093 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00004094 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004095 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
4096 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00004097 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004098 }
4099
4100 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00004101 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00004102
Duncan Sands481dc722007-12-19 07:36:31 +00004103 if (LandingPad->LandingPadLabel) {
4104 // This try-range is for an invoke.
4105 CallSiteEntry Site = {BeginLabel, LastLabel,
4106 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00004107
Duncan Sands481dc722007-12-19 07:36:31 +00004108 // Try to merge with the previous call-site.
4109 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00004110 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00004111 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
4112 // Extend the range of the previous entry.
4113 Prev.EndLabel = Site.EndLabel;
4114 continue;
4115 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004116 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004117
Duncan Sands481dc722007-12-19 07:36:31 +00004118 // Otherwise, create a new call-site.
4119 CallSites.push_back(Site);
4120 PreviousIsInvoke = true;
4121 } else {
4122 // Create a gap.
4123 PreviousIsInvoke = false;
4124 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004125 }
4126 }
4127 // If some instruction between the previous try-range and the end of the
4128 // function may throw, create a call-site entry with no landing pad for the
4129 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00004130 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004131 CallSiteEntry Site = {LastLabel, 0, 0, 0};
4132 CallSites.push_back(Site);
4133 }
4134
Jim Laskeybacd3042007-02-21 22:48:45 +00004135 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00004136
4137 // Call sites.
4138 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
4139 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
4140 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
4141 unsigned SizeSites = CallSites.size() * (SiteStartSize +
4142 SiteLengthSize +
4143 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00004144 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004145 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00004146
Duncan Sands671fa972008-05-07 19:11:09 +00004147 // Type infos.
4148 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
4149 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004150
4151 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004152 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004153 SizeSites + SizeActions + SizeTypes;
4154
4155 unsigned TotalSize = sizeof(int8_t) + // LPStart format
4156 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004157 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004158 TypeOffset;
4159
4160 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004161
Jim Laskeybacd3042007-02-21 22:48:45 +00004162 // Begin the exception table.
4163 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng05548eb2008-02-29 19:36:59 +00004164 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesend65b2642008-10-08 21:50:21 +00004165 O << "GCC_except_table" << SubprogramCount << ":\n";
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004166 for (unsigned i = 0; i != SizeAlign; ++i) {
4167 Asm->EmitInt8(0);
4168 Asm->EOL("Padding");
4169 }
Jim Laskeybacd3042007-02-21 22:48:45 +00004170 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004171
Jim Laskeybacd3042007-02-21 22:48:45 +00004172 // Emit the header.
4173 Asm->EmitInt8(DW_EH_PE_omit);
4174 Asm->EOL("LPStart format (DW_EH_PE_omit)");
4175 Asm->EmitInt8(DW_EH_PE_absptr);
4176 Asm->EOL("TType format (DW_EH_PE_absptr)");
4177 Asm->EmitULEB128Bytes(TypeOffset);
4178 Asm->EOL("TType base offset");
4179 Asm->EmitInt8(DW_EH_PE_udata4);
4180 Asm->EOL("Call site format (DW_EH_PE_udata4)");
4181 Asm->EmitULEB128Bytes(SizeSites);
4182 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004183
Duncan Sands57810cd2007-09-05 11:27:52 +00004184 // Emit the landing pad site information.
4185 for (unsigned i = 0; i < CallSites.size(); ++i) {
4186 CallSiteEntry &S = CallSites[i];
4187 const char *BeginTag;
4188 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00004189
Duncan Sands57810cd2007-09-05 11:27:52 +00004190 if (!S.BeginLabel) {
4191 BeginTag = "eh_func_begin";
4192 BeginNumber = SubprogramCount;
4193 } else {
4194 BeginTag = "label";
4195 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00004196 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004197
Duncan Sands57810cd2007-09-05 11:27:52 +00004198 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004199 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004200 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00004201
Duncan Sands57810cd2007-09-05 11:27:52 +00004202 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00004203 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00004204 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004205 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00004206 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004207 }
4208 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004209
Duncan Sands671fa972008-05-07 19:11:09 +00004210 if (!S.PadLabel)
4211 Asm->EmitInt32(0);
4212 else
Duncan Sands57810cd2007-09-05 11:27:52 +00004213 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004214 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004215 Asm->EOL("Landing pad");
4216
4217 Asm->EmitULEB128Bytes(S.Action);
4218 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004219 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004220
Jim Laskeybacd3042007-02-21 22:48:45 +00004221 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004222 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4223 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00004224
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004225 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004226 Asm->EOL("TypeInfo index");
4227 Asm->EmitSLEB128Bytes(Action.NextAction);
4228 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004229 }
4230
4231 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00004232 for (unsigned M = TypeInfos.size(); M; --M) {
4233 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00004234
4235 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00004236
Bill Wendling7d16e852009-04-10 00:12:49 +00004237 if (GV) {
4238 std::string GLN;
4239 O << Asm->getGlobalLinkName(GV, GLN);
4240 } else {
Jim Laskeybacd3042007-02-21 22:48:45 +00004241 O << "0";
Bill Wendling7d16e852009-04-10 00:12:49 +00004242 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004243
Jim Laskeybacd3042007-02-21 22:48:45 +00004244 Asm->EOL("TypeInfo");
4245 }
4246
Duncan Sands73ef58a2007-06-02 16:53:42 +00004247 // Emit the filter typeids.
4248 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4249 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00004250 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00004251 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00004252 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004253
Evan Cheng05548eb2008-02-29 19:36:59 +00004254 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004255 }
4256
Jim Laskey072200c2007-01-29 18:51:14 +00004257public:
4258 //===--------------------------------------------------------------------===//
4259 // Main entry points.
4260 //
Owen Andersoncb371882008-08-21 00:14:44 +00004261 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00004262 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
4263 shouldEmitTableModule(false), shouldEmitMovesModule(false),
4264 ExceptionTimer(0) {
4265 if (TimePassesIsEnabled)
4266 ExceptionTimer = new Timer("Dwarf Exception Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00004267 getDwarfTimerGroup());
Bill Wendling163ba3f2009-03-10 21:23:25 +00004268 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004269
Bill Wendling163ba3f2009-03-10 21:23:25 +00004270 virtual ~DwarfException() {
4271 delete ExceptionTimer;
4272 }
Jim Laskey072200c2007-01-29 18:51:14 +00004273
4274 /// SetModuleInfo - Set machine module information when it's known that pass
4275 /// manager has created it. Set by the target AsmPrinter.
4276 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00004277 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00004278 }
4279
4280 /// BeginModule - Emit all exception information that should come prior to the
4281 /// content.
4282 void BeginModule(Module *M) {
4283 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00004284 }
4285
4286 /// EndModule - Emit all exception information that should come after the
4287 /// content.
4288 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004289 if (TimePassesIsEnabled)
4290 ExceptionTimer->startTimer();
4291
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004292 if (shouldEmitMovesModule || shouldEmitTableModule) {
4293 const std::vector<Function *> Personalities = MMI->getPersonalities();
Evan Chenge3d42322009-02-25 07:04:34 +00004294 for (unsigned i = 0; i < Personalities.size(); ++i)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004295 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00004296
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004297 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4298 E = EHFrames.end(); I != E; ++I)
4299 EmitEHFrame(*I);
4300 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004301
4302 if (TimePassesIsEnabled)
4303 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004304 }
4305
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004306 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00004307 /// emitted immediately after the function entry point.
4308 void BeginFunction(MachineFunction *MF) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004309 if (TimePassesIsEnabled)
4310 ExceptionTimer->startTimer();
4311
Jim Laskey072200c2007-01-29 18:51:14 +00004312 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004313 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004314
Bill Wendling163ba3f2009-03-10 21:23:25 +00004315 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004316 // Map all labels and get rid of any dead landing pads.
4317 MMI->TidyLandingPads();
Bill Wendling163ba3f2009-03-10 21:23:25 +00004318
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004319 // If any landing pads survive, we need an EH table.
4320 if (MMI->getLandingPads().size())
4321 shouldEmitTable = true;
4322
4323 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00004324 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004325 shouldEmitMoves = true;
4326
4327 if (shouldEmitMoves || shouldEmitTable)
4328 // Assumes in correct section after the entry point.
4329 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00004330 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004331
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004332 shouldEmitTableModule |= shouldEmitTable;
4333 shouldEmitMovesModule |= shouldEmitMoves;
Bill Wendling163ba3f2009-03-10 21:23:25 +00004334
4335 if (TimePassesIsEnabled)
4336 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004337 }
4338
4339 /// EndFunction - Gather and emit post-function exception information.
4340 ///
4341 void EndFunction() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004342 if (TimePassesIsEnabled)
4343 ExceptionTimer->startTimer();
4344
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004345 if (shouldEmitMoves || shouldEmitTable) {
4346 EmitLabel("eh_func_end", SubprogramCount);
4347 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00004348
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004349 // Save EH frame information
Bill Wendling7d16e852009-04-10 00:12:49 +00004350 std::string Name;
4351 EHFrames.push_back(
4352 FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF, Name),
4353 SubprogramCount,
4354 MMI->getPersonalityIndex(),
4355 MF->getFrameInfo()->hasCalls(),
4356 !MMI->getLandingPads().empty(),
4357 MMI->getFrameMoves(),
4358 MF->getFunction()));
Bill Wendling163ba3f2009-03-10 21:23:25 +00004359 }
4360
4361 if (TimePassesIsEnabled)
4362 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004363 }
4364};
4365
Jim Laskey0d086af2006-02-27 12:43:29 +00004366} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00004367
4368//===----------------------------------------------------------------------===//
4369
Jim Laskeyd18e2892006-01-20 20:34:06 +00004370/// Emit - Print the abbreviation using the specified Dwarf writer.
4371///
Jim Laskey072200c2007-01-29 18:51:14 +00004372void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004373 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00004374 DD.getAsm()->EmitULEB128Bytes(Tag);
4375 DD.getAsm()->EOL(TagString(Tag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004376
Jim Laskeyd18e2892006-01-20 20:34:06 +00004377 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00004378 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4379 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004380
Jim Laskeyd18e2892006-01-20 20:34:06 +00004381 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00004382 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004383 const DIEAbbrevData &AttrData = Data[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004384
Jim Laskeyd18e2892006-01-20 20:34:06 +00004385 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00004386 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4387 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004388
Jim Laskeyd18e2892006-01-20 20:34:06 +00004389 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00004390 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4391 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00004392 }
4393
4394 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00004395 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4396 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00004397}
4398
4399#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00004400void DIEAbbrev::print(std::ostream &O) {
4401 O << "Abbreviation @"
4402 << std::hex << (intptr_t)this << std::dec
4403 << " "
4404 << TagString(Tag)
4405 << " "
4406 << ChildrenString(ChildrenFlag)
4407 << "\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004408
Jim Laskeya0f3d172006-09-07 22:06:40 +00004409 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4410 O << " "
4411 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004412 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00004413 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004414 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00004415 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00004416}
Bill Wendlinge8156192006-12-07 01:30:32 +00004417void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00004418#endif
4419
4420//===----------------------------------------------------------------------===//
4421
Jim Laskeyef42a012006-11-02 20:12:39 +00004422#ifndef NDEBUG
4423void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004424 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004425}
Jim Laskeyef42a012006-11-02 20:12:39 +00004426#endif
4427
4428//===----------------------------------------------------------------------===//
4429
Jim Laskey063e7652006-01-17 17:31:53 +00004430/// EmitValue - Emit integer of appropriate size.
4431///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004432void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00004433 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00004434 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00004435 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004436 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004437 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004438 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004439 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004440 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004441 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004442 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4443 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4444 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004445 default: assert(0 && "DIE Value form not supported yet"); break;
4446 }
4447}
4448
4449/// SizeOf - Determine size of integer value in bytes.
4450///
Jim Laskey072200c2007-01-29 18:51:14 +00004451unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004452 switch (Form) {
4453 case DW_FORM_flag: // Fall thru
4454 case DW_FORM_ref1: // Fall thru
4455 case DW_FORM_data1: return sizeof(int8_t);
4456 case DW_FORM_ref2: // Fall thru
4457 case DW_FORM_data2: return sizeof(int16_t);
4458 case DW_FORM_ref4: // Fall thru
4459 case DW_FORM_data4: return sizeof(int32_t);
4460 case DW_FORM_ref8: // Fall thru
4461 case DW_FORM_data8: return sizeof(int64_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004462 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4463 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00004464 default: assert(0 && "DIE Value form not supported yet"); break;
4465 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004466 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00004467}
4468
Jim Laskey063e7652006-01-17 17:31:53 +00004469//===----------------------------------------------------------------------===//
4470
4471/// EmitValue - Emit string value.
4472///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004473void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Bill Wendlingdd446322009-03-10 23:57:09 +00004474 DD.getAsm()->EmitString(Str);
Jim Laskey063e7652006-01-17 17:31:53 +00004475}
4476
Jim Laskey063e7652006-01-17 17:31:53 +00004477//===----------------------------------------------------------------------===//
4478
4479/// EmitValue - Emit label value.
4480///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004481void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004482 bool IsSmall = Form == DW_FORM_data4;
4483 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004484}
4485
4486/// SizeOf - Determine size of label value in bytes.
4487///
Jim Laskey072200c2007-01-29 18:51:14 +00004488unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004489 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004490 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004491}
Jim Laskeyef42a012006-11-02 20:12:39 +00004492
Jim Laskey063e7652006-01-17 17:31:53 +00004493//===----------------------------------------------------------------------===//
4494
Jim Laskeyd18e2892006-01-20 20:34:06 +00004495/// EmitValue - Emit label value.
4496///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004497void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004498 bool IsSmall = Form == DW_FORM_data4;
4499 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00004500}
4501
4502/// SizeOf - Determine size of label value in bytes.
4503///
Jim Laskey072200c2007-01-29 18:51:14 +00004504unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004505 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004506 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00004507}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004508
Jim Laskeyd18e2892006-01-20 20:34:06 +00004509//===----------------------------------------------------------------------===//
4510
Jim Laskey063e7652006-01-17 17:31:53 +00004511/// EmitValue - Emit delta value.
4512///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004513void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4514 bool IsSmall = Form == DW_FORM_data4;
4515 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4516 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4517}
4518
4519/// SizeOf - Determine size of delta value in bytes.
4520///
4521unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4522 if (Form == DW_FORM_data4) return 4;
4523 return DD.getTargetData()->getPointerSize();
4524}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004525
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004526//===----------------------------------------------------------------------===//
4527
4528/// EmitValue - Emit delta value.
4529///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004530void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004531 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00004532 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004533}
4534
4535/// SizeOf - Determine size of delta value in bytes.
4536///
Jim Laskey072200c2007-01-29 18:51:14 +00004537unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004538 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004539 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004540}
4541
4542//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004543
Jim Laskeyb8509c52006-03-23 18:07:55 +00004544/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004545///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004546void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004547 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00004548}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004549
Jim Laskeyd18e2892006-01-20 20:34:06 +00004550//===----------------------------------------------------------------------===//
4551
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004552/// ComputeSize - calculate the size of the block.
4553///
Jim Laskey072200c2007-01-29 18:51:14 +00004554unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00004555 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00004556 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004557
Jim Laskeyef42a012006-11-02 20:12:39 +00004558 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00004559 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00004560 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004561 }
4562 return Size;
4563}
4564
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004565/// EmitValue - Emit block data.
4566///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004567void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004568 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004569 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4570 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4571 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4572 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004573 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004574 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004575
Owen Anderson873e1b52008-06-24 21:44:59 +00004576 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00004577
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004578 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00004579 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00004580 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004581 }
4582}
4583
4584/// SizeOf - Determine size of block data in bytes.
4585///
Jim Laskey072200c2007-01-29 18:51:14 +00004586unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004587 switch (Form) {
4588 case DW_FORM_block1: return Size + sizeof(int8_t);
4589 case DW_FORM_block2: return Size + sizeof(int16_t);
4590 case DW_FORM_block4: return Size + sizeof(int32_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004591 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004592 default: assert(0 && "Improper form for block"); break;
4593 }
4594 return 0;
4595}
4596
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004597//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004598/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00004599
4600DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00004601 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00004602 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00004603}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004604
Jim Laskeyb8509c52006-03-23 18:07:55 +00004605/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4606///
4607void DIE::AddSiblingOffset() {
4608 DIEInteger *DI = new DIEInteger(0);
4609 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00004610 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00004611}
4612
Jim Laskeyef42a012006-11-02 20:12:39 +00004613/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004614///
Jim Laskeyef42a012006-11-02 20:12:39 +00004615void DIE::Profile(FoldingSetNodeID &ID) {
4616 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004617
Jim Laskeyef42a012006-11-02 20:12:39 +00004618 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4619 ID.AddPointer(Children[i]);
4620
4621 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4622 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00004623}
Jim Laskeyef42a012006-11-02 20:12:39 +00004624
4625#ifndef NDEBUG
4626void DIE::print(std::ostream &O, unsigned IncIndent) {
4627 static unsigned IndentCount = 0;
4628 IndentCount += IncIndent;
4629 const std::string Indent(IndentCount, ' ');
4630 bool isBlock = Abbrev.getTag() == 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004631
Jim Laskeyef42a012006-11-02 20:12:39 +00004632 if (!isBlock) {
4633 O << Indent
4634 << "Die: "
4635 << "0x" << std::hex << (intptr_t)this << std::dec
4636 << ", Offset: " << Offset
4637 << ", Size: " << Size
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004638 << "\n";
4639
Jim Laskeyef42a012006-11-02 20:12:39 +00004640 O << Indent
4641 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00004642 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00004643 << ChildrenString(Abbrev.getChildrenFlag());
4644 } else {
4645 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00004646 }
4647 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00004648
Owen Anderson873e1b52008-06-24 21:44:59 +00004649 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004650
Jim Laskeyef42a012006-11-02 20:12:39 +00004651 IndentCount += 2;
4652 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4653 O << Indent;
Bill Wendling4e974012008-07-22 00:28:47 +00004654
4655 if (!isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +00004656 O << AttributeString(Data[i].getAttribute());
Bill Wendling4e974012008-07-22 00:28:47 +00004657 else
Jim Laskeyef42a012006-11-02 20:12:39 +00004658 O << "Blk[" << i << "]";
Bill Wendling4e974012008-07-22 00:28:47 +00004659
Jim Laskeyef42a012006-11-02 20:12:39 +00004660 O << " "
4661 << FormEncodingString(Data[i].getForm())
4662 << " ";
4663 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00004664 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00004665 }
Jim Laskeyef42a012006-11-02 20:12:39 +00004666 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00004667
Jim Laskeyef42a012006-11-02 20:12:39 +00004668 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4669 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00004670 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004671
Jim Laskeyef42a012006-11-02 20:12:39 +00004672 if (!isBlock) O << "\n";
4673 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00004674}
4675
Jim Laskeyef42a012006-11-02 20:12:39 +00004676void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004677 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00004678}
Jim Laskeybd761842006-02-27 17:27:12 +00004679#endif
Jim Laskey65195462006-10-30 13:35:07 +00004680
4681//===----------------------------------------------------------------------===//
4682/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00004683///
Jim Laskey65195462006-10-30 13:35:07 +00004684
Bill Wendling91b8b802009-03-10 20:41:52 +00004685DwarfWriter::DwarfWriter()
Bill Wendling163ba3f2009-03-10 21:23:25 +00004686 : ImmutablePass(&ID), DD(0), DE(0) {}
Jim Laskey65195462006-10-30 13:35:07 +00004687
4688DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00004689 delete DE;
4690 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00004691}
4692
Jim Laskey65195462006-10-30 13:35:07 +00004693/// BeginModule - Emit all Dwarf sections that should come prior to the
4694/// content.
Devang Pateleb3fc282009-01-08 23:40:34 +00004695void DwarfWriter::BeginModule(Module *M,
4696 MachineModuleInfo *MMI,
4697 raw_ostream &OS, AsmPrinter *A,
4698 const TargetAsmInfo *T) {
4699 DE = new DwarfException(OS, A, T);
4700 DD = new DwarfDebug(OS, A, T);
Jim Laskey072200c2007-01-29 18:51:14 +00004701 DE->BeginModule(M);
4702 DD->BeginModule(M);
Devang Patel7bb89ed2009-01-13 00:20:51 +00004703 DD->SetDebugInfo(MMI);
Devang Pateleb3fc282009-01-08 23:40:34 +00004704 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00004705}
4706
4707/// EndModule - Emit all Dwarf sections that should come after the content.
4708///
4709void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00004710 DE->EndModule();
4711 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00004712}
4713
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004714/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00004715/// emitted immediately after the function entry point.
4716void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00004717 DE->BeginFunction(MF);
4718 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00004719}
4720
4721/// EndFunction - Gather and emit post-function debug information.
4722///
Bill Wendlingd751c642008-09-26 00:28:12 +00004723void DwarfWriter::EndFunction(MachineFunction *MF) {
4724 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004725 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004726
Bill Wendlingc91d0b92008-07-22 00:53:37 +00004727 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00004728 // Clear function debug information.
4729 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00004730}
Devang Patelccca7fe2009-01-12 19:17:34 +00004731
Devang Patelcf3a4482009-01-15 23:41:32 +00004732/// ValidDebugInfo - Return true if V represents valid debug info value.
Devang Patel48c7fa22009-04-13 18:13:16 +00004733bool DwarfWriter::ValidDebugInfo(Value *V, bool FastISel) {
4734 return DD && DD->ValidDebugInfo(V, FastISel);
Devang Patelcf3a4482009-01-15 23:41:32 +00004735}
4736
Devang Patelccca7fe2009-01-12 19:17:34 +00004737/// RecordSourceLine - Records location information and associates it with a
4738/// label. Returns a unique label ID used to generate a label and provide
4739/// correspondence to the source line list.
4740unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
4741 unsigned Src) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004742 return DD->RecordSourceLine(Line, Col, Src);
Devang Patelccca7fe2009-01-12 19:17:34 +00004743}
4744
Evan Chenge3d42322009-02-25 07:04:34 +00004745/// getOrCreateSourceID - Look up the source id with the given directory and
4746/// source file names. If none currently exists, create a new id and insert it
4747/// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
4748/// as well.
4749unsigned DwarfWriter::getOrCreateSourceID(const std::string &DirName,
4750 const std::string &FileName) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004751 return DD->getOrCreateSourceID(DirName, FileName);
Devang Patelccca7fe2009-01-12 19:17:34 +00004752}
4753
4754/// RecordRegionStart - Indicate the start of a region.
4755unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004756 return DD->RecordRegionStart(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004757}
4758
4759/// RecordRegionEnd - Indicate the end of a region.
4760unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004761 return DD->RecordRegionEnd(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004762}
4763
4764/// getRecordSourceLineCount - Count source lines.
4765unsigned DwarfWriter::getRecordSourceLineCount() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004766 return DD->getRecordSourceLineCount();
Devang Patelccca7fe2009-01-12 19:17:34 +00004767}
Devang Patelbb8c5952009-01-13 21:25:00 +00004768
Devang Patelc48c5502009-01-13 21:44:10 +00004769/// RecordVariable - Indicate the declaration of a local variable.
4770///
Devang Patel1be3ecc2009-04-15 00:10:26 +00004771void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
4772 const MachineInstr *MI) {
4773 DD->RecordVariable(GV, FrameIndex, MI);
Devang Patelc48c5502009-01-13 21:44:10 +00004774}
Devang Patelbbdc8202009-01-13 23:54:55 +00004775
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004776/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
4777/// be emitted.
4778bool DwarfWriter::ShouldEmitDwarfDebug() const {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004779 return DD->ShouldEmitDwarfDebug();
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004780}
Devang Patel0f7fef32009-04-13 17:02:03 +00004781
Devang Patel1be3ecc2009-04-15 00:10:26 +00004782//// RecordInlinedFnStart - Global variable GV is inlined at the location marked
Devang Patel0f7fef32009-04-13 17:02:03 +00004783//// by LabelID label.
Devang Patel1be3ecc2009-04-15 00:10:26 +00004784void DwarfWriter::RecordInlinedFnStart(Instruction *I, DISubprogram &SP,
4785 unsigned LabelID, unsigned Src,
4786 unsigned Line, unsigned Col) {
4787 DD->RecordInlinedFnStart(I, SP, LabelID, Src, Line, Col);
Devang Patel0f7fef32009-04-13 17:02:03 +00004788}
4789
Devang Patel1be3ecc2009-04-15 00:10:26 +00004790/// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
4791unsigned DwarfWriter::RecordInlinedFnEnd(DISubprogram &SP) {
4792 return DD->RecordInlinedFnEnd(SP);
4793}
4794
4795/// RecordVariableScope - Record scope for the variable declared by
4796/// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
4797void DwarfWriter::RecordVariableScope(DIVariable &DV,
4798 const MachineInstr *DeclareMI) {
4799 DD->RecordVariableScope(DV, DeclareMI);
4800}