blob: 7190b9269b256683bb0ae91dac2ef55c9df2df05 [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 ///
730 std::map<std::string, DIE *> Globals;
731
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; }
748 std::map<std::string, DIE *> &getGlobals() { return Globals; }
749
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 //
861 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 Chengd4114192008-07-09 21:53:02 +00001075 if (VerboseAsm)
1076 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1077 else
1078 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00001079 Asm->EmitULEB128Bytes(Offset);
1080 Asm->EOL("Offset");
1081 } else {
1082 Asm->EmitInt8(DW_CFA_offset_extended);
1083 Asm->EOL("DW_CFA_offset_extended");
1084 Asm->EmitULEB128Bytes(Reg);
1085 Asm->EOL("Reg");
1086 Asm->EmitULEB128Bytes(Offset);
1087 Asm->EOL("Offset");
1088 }
1089 }
1090 }
1091 }
1092
Jim Laskey072200c2007-01-29 18:51:14 +00001093};
1094
1095//===----------------------------------------------------------------------===//
Devang Patelf3ee5142009-01-12 22:54:42 +00001096/// SrcLineInfo - This class is used to record source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001097///
1098class SrcLineInfo {
1099 unsigned Line; // Source line number.
1100 unsigned Column; // Source column.
1101 unsigned SourceID; // Source ID number.
1102 unsigned LabelID; // Label in code ID number.
1103public:
1104 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001105 : Line(L), Column(C), SourceID(S), LabelID(I) {}
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001106
1107 // Accessors
1108 unsigned getLine() const { return Line; }
1109 unsigned getColumn() const { return Column; }
1110 unsigned getSourceID() const { return SourceID; }
1111 unsigned getLabelID() const { return LabelID; }
1112};
1113
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001114//===----------------------------------------------------------------------===//
Devang Patel7a6e5a32009-01-08 02:33:41 +00001115/// DbgVariable - This class is used to track local variable information.
1116///
1117class DbgVariable {
Devang Patel99ec3532009-01-16 19:28:14 +00001118 DIVariable Var; // Variable Descriptor.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001119 unsigned FrameIndex; // Variable frame index.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001120public:
Devang Patel99ec3532009-01-16 19:28:14 +00001121 DbgVariable(DIVariable V, unsigned I) : Var(V), FrameIndex(I) {}
Devang Patel7a6e5a32009-01-08 02:33:41 +00001122
1123 // Accessors.
Devang Patel99ec3532009-01-16 19:28:14 +00001124 DIVariable getVariable() const { return Var; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001125 unsigned getFrameIndex() const { return FrameIndex; }
1126};
1127
1128//===----------------------------------------------------------------------===//
1129/// DbgScope - This class is used to track scope information.
1130///
1131class DbgScope {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001132 DbgScope *Parent; // Parent to this scope.
Devang Patel7103c6a2009-01-16 18:01:58 +00001133 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001134 // Either subprogram or block.
1135 unsigned StartLabelID; // Label ID of the beginning of scope.
1136 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel7103c6a2009-01-16 18:01:58 +00001137 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
Devang Patel9795da52009-01-10 02:42:49 +00001138 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001139public:
Devang Patel0e5200f2009-01-15 18:25:17 +00001140 DbgScope(DbgScope *P, DIDescriptor D)
Devang Patel7a6e5a32009-01-08 02:33:41 +00001141 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), Scopes(), Variables()
1142 {}
Devang Patel481ff5b2009-01-12 18:48:36 +00001143 ~DbgScope() {
1144 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1145 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1146 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001147
1148 // Accessors.
Devang Patel7103c6a2009-01-16 18:01:58 +00001149 DbgScope *getParent() const { return Parent; }
1150 DIDescriptor getDesc() const { return Desc; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001151 unsigned getStartLabelID() const { return StartLabelID; }
1152 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel9795da52009-01-10 02:42:49 +00001153 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1154 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001155 void setStartLabelID(unsigned S) { StartLabelID = S; }
1156 void setEndLabelID(unsigned E) { EndLabelID = E; }
1157
1158 /// AddScope - Add a scope to the scope.
1159 ///
1160 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1161
1162 /// AddVariable - Add a variable to the scope.
1163 ///
1164 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
1165};
1166
1167//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001168/// DwarfDebug - Emits Dwarf debug directives.
Jim Laskey072200c2007-01-29 18:51:14 +00001169///
1170class DwarfDebug : public Dwarf {
Jim Laskey65195462006-10-30 13:35:07 +00001171 //===--------------------------------------------------------------------===//
1172 // Attributes used to construct specific Dwarf sections.
1173 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001174
Evan Chenge3d42322009-02-25 07:04:34 +00001175 /// CompileUnitMap - A map of global variables representing compile units to
1176 /// compile units.
1177 DenseMap<Value *, CompileUnit *> CompileUnitMap;
1178
1179 /// CompileUnits - All the compile units in this module.
1180 ///
1181 SmallVector<CompileUnit *, 8> CompileUnits;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001182
Devang Pateldd9db662009-01-30 18:20:31 +00001183 /// MainCU - Some platform prefers one compile unit per .o file. In such
1184 /// cases, all dies are inserted in MainCU.
1185 CompileUnit *MainCU;
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001186
Jim Laskeyef42a012006-11-02 20:12:39 +00001187 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +00001188 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001189 FoldingSet<DIEAbbrev> AbbreviationsSet;
1190
1191 /// Abbreviations - A list of all the unique abbreviations in use.
1192 ///
1193 std::vector<DIEAbbrev *> Abbreviations;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001194
Evan Chenge3d42322009-02-25 07:04:34 +00001195 /// DirectoryIdMap - Directory name to directory id map.
1196 ///
1197 StringMap<unsigned> DirectoryIdMap;
Devang Patel8526cc02009-01-05 22:35:52 +00001198
Evan Chenge3d42322009-02-25 07:04:34 +00001199 /// DirectoryNames - A list of directory names.
1200 SmallVector<std::string, 8> DirectoryNames;
1201
1202 /// SourceFileIdMap - Source file name to source file id map.
1203 ///
1204 StringMap<unsigned> SourceFileIdMap;
1205
1206 /// SourceFileNames - A list of source file names.
1207 SmallVector<std::string, 8> SourceFileNames;
1208
1209 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
1210 /// id mapped to a unique id.
1211 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
1212
1213 /// SourceIds - Reverse map from source id to directory id + file id pair.
1214 ///
1215 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
Devang Patel8526cc02009-01-05 22:35:52 +00001216
Devang Patel07354e52009-01-16 21:07:53 +00001217 /// Lines - List of of source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001218 std::vector<SrcLineInfo> Lines;
1219
Devang Patel07354e52009-01-16 21:07:53 +00001220 /// ValuesSet - Used to uniquely define values.
1221 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001222 FoldingSet<DIEValue> ValuesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001223
Jim Laskeyef42a012006-11-02 20:12:39 +00001224 /// Values - A list of all the unique values in use.
1225 ///
1226 std::vector<DIEValue *> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001227
Jim Laskey65195462006-10-30 13:35:07 +00001228 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +00001229 ///
Jim Laskey65195462006-10-30 13:35:07 +00001230 UniqueVector<std::string> StringPool;
1231
Jim Laskey65195462006-10-30 13:35:07 +00001232 /// SectionMap - Provides a unique id per text section.
1233 ///
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001234 UniqueVector<const Section*> SectionMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001235
Jim Laskey65195462006-10-30 13:35:07 +00001236 /// SectionSourceLines - Tracks line numbers per text section.
1237 ///
Devang Patelf3ee5142009-01-12 22:54:42 +00001238 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Jim Laskey65195462006-10-30 13:35:07 +00001239
Jim Laskeybacd3042007-02-21 22:48:45 +00001240 /// didInitial - Flag to indicate if initial emission has been done.
1241 ///
1242 bool didInitial;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001243
Jim Laskeybacd3042007-02-21 22:48:45 +00001244 /// shouldEmit - Flag to indicate if debug information should be emitted.
1245 ///
1246 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +00001247
Devang Patel0e5200f2009-01-15 18:25:17 +00001248 // RootDbgScope - Top level scope for the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001249 //
1250 DbgScope *RootDbgScope;
1251
Bill Wendling163ba3f2009-03-10 21:23:25 +00001252 /// DbgScopeMap - Tracks the scopes in the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001253 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
Bill Wendling163ba3f2009-03-10 21:23:25 +00001254
1255 /// DebugTimer - Timer for the Dwarf debug writer.
1256 Timer *DebugTimer;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001257
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001258 struct FunctionDebugFrameInfo {
1259 unsigned Number;
1260 std::vector<MachineMove> Moves;
1261
1262 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001263 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001264 };
1265
1266 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001267
Bill Wendling163ba3f2009-03-10 21:23:25 +00001268private:
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001269 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
Bill Wendlingc8615e42009-03-10 21:47:45 +00001270 /// maps to the source id. Source id starts at 1.
1271 std::pair<unsigned, unsigned>
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001272 getSourceDirectoryAndFileIds(unsigned SId) const {
Bill Wendlingc8615e42009-03-10 21:47:45 +00001273 return SourceIds[SId-1];
1274 }
1275
1276 /// getNumSourceDirectories - Return the number of source directories in the
1277 /// debug info.
1278 unsigned getNumSourceDirectories() const {
1279 return DirectoryNames.size();
1280 }
1281
1282 /// getSourceDirectoryName - Return the name of the directory corresponding
1283 /// to the id.
1284 const std::string &getSourceDirectoryName(unsigned Id) const {
1285 return DirectoryNames[Id - 1];
1286 }
1287
1288 /// getSourceFileName - Return the name of the source file corresponding
1289 /// to the id.
1290 const std::string &getSourceFileName(unsigned Id) const {
1291 return SourceFileNames[Id - 1];
1292 }
1293
1294 /// getNumSourceIds - Return the number of unique source ids.
Bill Wendlingc8615e42009-03-10 21:47:45 +00001295 unsigned getNumSourceIds() const {
1296 return SourceIds.size();
1297 }
1298
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001299 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001300 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001301 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1302 // Profile the node so that we can make it unique.
1303 FoldingSetNodeID ID;
1304 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001305
Jim Laskeyef42a012006-11-02 20:12:39 +00001306 // Check the set for priors.
1307 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001308
Jim Laskeyef42a012006-11-02 20:12:39 +00001309 // If it's newly added.
1310 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001311 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +00001312 Abbreviations.push_back(&Abbrev);
1313 // Assign the vector position + 1 as its number.
1314 Abbrev.setNumber(Abbreviations.size());
1315 } else {
1316 // Assign existing abbreviation number.
1317 Abbrev.setNumber(InSet->getNumber());
1318 }
1319 }
1320
Jim Laskey65195462006-10-30 13:35:07 +00001321 /// NewString - Add a string to the constant pool and returns a label.
1322 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001323 DWLabel NewString(const std::string &String) {
1324 unsigned StringID = StringPool.insert(String);
1325 return DWLabel("string", StringID);
1326 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001327
Jim Laskeyef42a012006-11-02 20:12:39 +00001328 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1329 /// entry.
1330 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1331 DIEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001332
Jim Laskeyef42a012006-11-02 20:12:39 +00001333 if (Entry) {
1334 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001335 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001336 void *Where;
1337 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001338
Jim Laskeyf6733882006-11-02 21:48:18 +00001339 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001340
Jim Laskeyef42a012006-11-02 20:12:39 +00001341 Value = new DIEntry(Entry);
1342 ValuesSet.InsertNode(Value, Where);
1343 } else {
1344 Value = new DIEntry(Entry);
1345 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001346
Jim Laskeyef42a012006-11-02 20:12:39 +00001347 Values.push_back(Value);
1348 return Value;
1349 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001350
Jim Laskeyef42a012006-11-02 20:12:39 +00001351 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1352 ///
1353 void SetDIEntry(DIEntry *Value, DIE *Entry) {
Bill Wendlingdd446322009-03-10 23:57:09 +00001354 Value->setEntry(Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001355 // Add to values set if not already there. If it is, we merely have a
1356 // duplicate in the values list (no harm.)
1357 ValuesSet.GetOrInsertNode(Value);
1358 }
1359
1360 /// AddUInt - Add an unsigned integer attribute data and value.
1361 ///
1362 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1363 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1364
1365 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001366 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001367 void *Where;
1368 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1369 if (!Value) {
1370 Value = new DIEInteger(Integer);
1371 ValuesSet.InsertNode(Value, Where);
1372 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001373 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001374
Jim Laskeyef42a012006-11-02 20:12:39 +00001375 Die->AddValue(Attribute, Form, Value);
1376 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001377
Jim Laskeyef42a012006-11-02 20:12:39 +00001378 /// AddSInt - Add an signed integer attribute data and value.
1379 ///
1380 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1381 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1382
1383 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001384 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001385 void *Where;
1386 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1387 if (!Value) {
1388 Value = new DIEInteger(Integer);
1389 ValuesSet.InsertNode(Value, Where);
1390 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001391 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001392
Jim Laskeyef42a012006-11-02 20:12:39 +00001393 Die->AddValue(Attribute, Form, Value);
1394 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001395
Evan Chenge3d42322009-02-25 07:04:34 +00001396 /// AddString - Add a string attribute data and value.
Jim Laskeyef42a012006-11-02 20:12:39 +00001397 ///
1398 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1399 const std::string &String) {
1400 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001401 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001402 void *Where;
1403 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1404 if (!Value) {
1405 Value = new DIEString(String);
1406 ValuesSet.InsertNode(Value, Where);
1407 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001408 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001409
Jim Laskeyef42a012006-11-02 20:12:39 +00001410 Die->AddValue(Attribute, Form, Value);
1411 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001412
Jim Laskeyef42a012006-11-02 20:12:39 +00001413 /// AddLabel - Add a Dwarf label attribute data and value.
1414 ///
1415 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1416 const DWLabel &Label) {
1417 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001418 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001419 void *Where;
1420 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1421 if (!Value) {
1422 Value = new DIEDwarfLabel(Label);
1423 ValuesSet.InsertNode(Value, Where);
1424 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001425 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001426
Jim Laskeyef42a012006-11-02 20:12:39 +00001427 Die->AddValue(Attribute, Form, Value);
1428 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001429
Jim Laskeyef42a012006-11-02 20:12:39 +00001430 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1431 ///
1432 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1433 const std::string &Label) {
1434 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001435 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001436 void *Where;
1437 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1438 if (!Value) {
1439 Value = new DIEObjectLabel(Label);
1440 ValuesSet.InsertNode(Value, Where);
1441 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001442 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001443
Jim Laskeyef42a012006-11-02 20:12:39 +00001444 Die->AddValue(Attribute, Form, Value);
1445 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001446
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001447 /// AddSectionOffset - Add a section offset label attribute data and value.
1448 ///
1449 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1450 const DWLabel &Label, const DWLabel &Section,
1451 bool isEH = false, bool useSet = true) {
1452 FoldingSetNodeID ID;
1453 DIESectionOffset::Profile(ID, Label, Section);
1454 void *Where;
1455 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1456 if (!Value) {
1457 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1458 ValuesSet.InsertNode(Value, Where);
1459 Values.push_back(Value);
1460 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001461
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001462 Die->AddValue(Attribute, Form, Value);
1463 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001464
Jim Laskeyef42a012006-11-02 20:12:39 +00001465 /// AddDelta - Add a label delta attribute data and value.
1466 ///
1467 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1468 const DWLabel &Hi, const DWLabel &Lo) {
1469 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001470 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001471 void *Where;
1472 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1473 if (!Value) {
1474 Value = new DIEDelta(Hi, Lo);
1475 ValuesSet.InsertNode(Value, Where);
1476 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001477 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001478
Jim Laskeyef42a012006-11-02 20:12:39 +00001479 Die->AddValue(Attribute, Form, Value);
1480 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001481
Jim Laskeyef42a012006-11-02 20:12:39 +00001482 /// AddDIEntry - Add a DIE attribute data and value.
1483 ///
1484 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1485 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1486 }
1487
1488 /// AddBlock - Add block data.
1489 ///
1490 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1491 Block->ComputeSize(*this);
1492 FoldingSetNodeID ID;
1493 Block->Profile(ID);
1494 void *Where;
1495 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1496 if (!Value) {
1497 Value = Block;
1498 ValuesSet.InsertNode(Value, Where);
1499 Values.push_back(Value);
1500 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001501 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001502 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001503 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001504 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001505
Jim Laskeyef42a012006-11-02 20:12:39 +00001506 Die->AddValue(Attribute, Block->BestForm(), Value);
1507 }
1508
Jim Laskey65195462006-10-30 13:35:07 +00001509 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001510 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001511 void AddSourceLine(DIE *Die, const DIVariable *V) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001512 unsigned FileID = 0;
1513 unsigned Line = V->getLineNumber();
Devang Pateldd9db662009-01-30 18:20:31 +00001514 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1515 FileID = Unit->getID();
Devang Patel2303df92009-02-10 06:04:08 +00001516 assert (FileID && "Invalid file id");
Devang Patel7a6e5a32009-01-08 02:33:41 +00001517 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1518 AddUInt(Die, DW_AT_decl_line, 0, Line);
1519 }
1520
1521 /// AddSourceLine - Add location information to specified debug information
1522 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001523 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Devang Patel8526cc02009-01-05 22:35:52 +00001524 unsigned FileID = 0;
1525 unsigned Line = G->getLineNumber();
Devang Pateldd9db662009-01-30 18:20:31 +00001526 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1527 FileID = Unit->getID();
Devang Patel2303df92009-02-10 06:04:08 +00001528 assert (FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001529 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1530 AddUInt(Die, DW_AT_decl_line, 0, Line);
1531 }
1532
Devang Patel99ec3532009-01-16 19:28:14 +00001533 void AddSourceLine(DIE *Die, const DIType *Ty) {
Devang Patel8526cc02009-01-05 22:35:52 +00001534 unsigned FileID = 0;
Devang Patel99ec3532009-01-16 19:28:14 +00001535 unsigned Line = Ty->getLineNumber();
Devang Pateldd9db662009-01-30 18:20:31 +00001536 DICompileUnit CU = Ty->getCompileUnit();
1537 if (CU.isNull())
1538 return;
1539 CompileUnit *Unit = FindCompileUnit(CU);
1540 FileID = Unit->getID();
Devang Patel2303df92009-02-10 06:04:08 +00001541 assert (FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001542 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1543 AddUInt(Die, DW_AT_decl_line, 0, Line);
1544 }
1545
Jim Laskey65195462006-10-30 13:35:07 +00001546 /// AddAddress - Add an address attribute to a die based on the location
1547 /// provided.
1548 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001549 const MachineLocation &Location) {
Dan Gohmand735b802008-10-03 15:45:36 +00001550 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001551 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001552
Dan Gohmand735b802008-10-03 15:45:36 +00001553 if (Location.isReg()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001554 if (Reg < 32) {
1555 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1556 } else {
1557 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1558 AddUInt(Block, 0, DW_FORM_udata, Reg);
1559 }
1560 } else {
1561 if (Reg < 32) {
1562 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1563 } else {
1564 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1565 AddUInt(Block, 0, DW_FORM_udata, Reg);
1566 }
1567 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1568 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001569
Jim Laskeyef42a012006-11-02 20:12:39 +00001570 AddBlock(Die, Attribute, 0, Block);
1571 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001572
Jim Laskeyef42a012006-11-02 20:12:39 +00001573 /// AddType - Add a new type attribute to the specified entity.
Devang Patel48d190f2009-01-05 21:47:57 +00001574 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel80303aa2009-01-23 19:13:31 +00001575 if (Ty.isNull())
Devang Patel48d190f2009-01-05 21:47:57 +00001576 return;
Devang Patel48d190f2009-01-05 21:47:57 +00001577
1578 // Check for pre-existence.
1579 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1580 // If it exists then use the existing value.
1581 if (Slot) {
1582 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1583 return;
1584 }
1585
1586 // Set up proxy.
1587 Slot = NewDIEntry();
1588
1589 // Construct type.
1590 DIE Buffer(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001591 if (Ty.isBasicType(Ty.getTag()))
1592 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1593 else if (Ty.isDerivedType(Ty.getTag()))
1594 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1595 else {
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001596 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelf193ff02009-01-15 19:26:23 +00001597 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1598 }
1599
Devang Patel7009d242009-01-27 23:22:55 +00001600 // Add debug information entry to entity and appropriate context.
1601 DIE *Die = NULL;
1602 DIDescriptor Context = Ty.getContext();
1603 if (!Context.isNull())
1604 Die = DW_Unit->getDieMapSlotFor(Context.getGV());
1605
1606 if (Die) {
1607 DIE *Child = new DIE(Buffer);
1608 Die->AddChild(Child);
1609 Buffer.Detach();
1610 SetDIEntry(Slot, Child);
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001611 } else {
Devang Patel7009d242009-01-27 23:22:55 +00001612 Die = DW_Unit->AddDie(Buffer);
1613 SetDIEntry(Slot, Die);
1614 }
1615
Devang Patel48d190f2009-01-05 21:47:57 +00001616 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1617 }
1618
Devang Patele5202732009-01-05 19:07:53 +00001619 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1620 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001621 DIBasicType BTy) {
Devang Patel08f053f2009-01-05 17:57:47 +00001622 // Get core information.
Bill Wendlingc7a09ab2009-03-13 04:37:11 +00001623 const char *Name = BTy.getName();
Devang Patel08f053f2009-01-05 17:57:47 +00001624 Buffer.setTag(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001625 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patel08f053f2009-01-05 17:57:47 +00001626 // Add name if not anonymous or intermediate type.
Bill Wendlingc7a09ab2009-03-13 04:37:11 +00001627 if (Name)
Devang Patel08f053f2009-01-05 17:57:47 +00001628 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001629 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel08f053f2009-01-05 17:57:47 +00001630 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1631 }
1632
Devang Patele5202732009-01-05 19:07:53 +00001633 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1634 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001635 DIDerivedType DTy) {
Devang Patel08f053f2009-01-05 17:57:47 +00001636 // Get core information.
Bill Wendlingc7a09ab2009-03-13 04:37:11 +00001637 const char *Name = DTy.getName();
Devang Patelf193ff02009-01-15 19:26:23 +00001638 uint64_t Size = DTy.getSizeInBits() >> 3;
1639 unsigned Tag = DTy.getTag();
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001640
Devang Patel08f053f2009-01-05 17:57:47 +00001641 // FIXME - Workaround for templates.
1642 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1643
1644 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001645
Devang Patel08f053f2009-01-05 17:57:47 +00001646 // Map to main type, void will not have a type.
Devang Patelf193ff02009-01-15 19:26:23 +00001647 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00001648 AddType(DW_Unit, &Buffer, FromTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001649
1650 // Add name if not anonymous or intermediate type.
Bill Wendlingc7a09ab2009-03-13 04:37:11 +00001651 if (Name)
Evan Chenge3d42322009-02-25 07:04:34 +00001652 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel08f053f2009-01-05 17:57:47 +00001653
1654 // Add size if non-zero (derived types might be zero-sized.)
1655 if (Size)
1656 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1657
1658 // Add source line info if available and TyDesc is not a forward
1659 // declaration.
Devang Patelad165be2009-01-27 00:45:04 +00001660 if (!DTy.isForwardDecl())
1661 AddSourceLine(&Buffer, &DTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001662 }
1663
Devang Patelf4215332009-01-05 19:55:51 +00001664 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1665 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001666 DICompositeType CTy) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001667 // Get core information.
Bill Wendlingc7a09ab2009-03-13 04:37:11 +00001668 const char *Name = CTy.getName();
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001669
Devang Patelf193ff02009-01-15 19:26:23 +00001670 uint64_t Size = CTy.getSizeInBits() >> 3;
1671 unsigned Tag = CTy.getTag();
Devang Patel49f38cb2009-01-23 01:19:09 +00001672 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001673
Devang Patelf4215332009-01-05 19:55:51 +00001674 switch (Tag) {
1675 case DW_TAG_vector_type:
1676 case DW_TAG_array_type:
Devang Patelf193ff02009-01-15 19:26:23 +00001677 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001678 break;
Devang Pateleab4a2e2009-01-20 18:35:14 +00001679 case DW_TAG_enumeration_type:
1680 {
1681 DIArray Elements = CTy.getTypeArray();
1682 // Add enumerators to enumeration type.
1683 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1684 DIE *ElemDie = NULL;
1685 DIEnumerator Enum(Elements.getElement(i).getGV());
1686 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
1687 Buffer.AddChild(ElemDie);
1688 }
1689 }
1690 break;
Devang Patelf4215332009-01-05 19:55:51 +00001691 case DW_TAG_subroutine_type:
1692 {
1693 // Add prototype flag.
1694 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patelf193ff02009-01-15 19:26:23 +00001695 DIArray Elements = CTy.getTypeArray();
Devang Patelf4215332009-01-05 19:55:51 +00001696 // Add return type.
Devang Patel48d190f2009-01-05 21:47:57 +00001697 DIDescriptor RTy = Elements.getElement(0);
Devang Patelf193ff02009-01-15 19:26:23 +00001698 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel48d190f2009-01-05 21:47:57 +00001699
Devang Patelf4215332009-01-05 19:55:51 +00001700 // Add arguments.
1701 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1702 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00001703 DIDescriptor Ty = Elements.getElement(i);
Devang Patel7ab24502009-01-17 06:57:25 +00001704 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patelf4215332009-01-05 19:55:51 +00001705 Buffer.AddChild(Arg);
1706 }
1707 }
1708 break;
1709 case DW_TAG_structure_type:
1710 case DW_TAG_union_type:
1711 {
1712 // Add elements to structure type.
Devang Patelf193ff02009-01-15 19:26:23 +00001713 DIArray Elements = CTy.getTypeArray();
Devang Patel153745c2009-01-16 00:50:53 +00001714
1715 // A forward struct declared type may not have elements available.
1716 if (Elements.isNull())
1717 break;
1718
Devang Patelf4215332009-01-05 19:55:51 +00001719 // Add elements to structure type.
1720 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1721 DIDescriptor Element = Elements.getElement(i);
Devang Patel5aac3d32009-01-17 08:01:33 +00001722 DIE *ElemDie = NULL;
Devang Patelf193ff02009-01-15 19:26:23 +00001723 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel2d1768c2009-01-17 08:05:14 +00001724 ElemDie = CreateSubprogramDIE(DW_Unit,
1725 DISubprogram(Element.getGV()));
Devang Patel5aac3d32009-01-17 08:01:33 +00001726 else if (Element.getTag() == dwarf::DW_TAG_variable) // ???
1727 ElemDie = CreateGlobalVariableDIE(DW_Unit,
1728 DIGlobalVariable(Element.getGV()));
Devang Patel2be58932009-01-20 21:02:02 +00001729 else
1730 ElemDie = CreateMemberDIE(DW_Unit,
1731 DIDerivedType(Element.getGV()));
Devang Patel2d1768c2009-01-17 08:05:14 +00001732 Buffer.AddChild(ElemDie);
Devang Patelf4215332009-01-05 19:55:51 +00001733 }
Devang Patel13319ce2009-02-17 22:43:44 +00001734 unsigned RLang = CTy.getRunTimeLang();
1735 if (RLang)
1736 AddUInt(&Buffer, DW_AT_APPLE_runtime_class, DW_FORM_data1, RLang);
Devang Patelf4215332009-01-05 19:55:51 +00001737 }
1738 break;
1739 default:
1740 break;
1741 }
1742
1743 // Add name if not anonymous or intermediate type.
Bill Wendlingc7a09ab2009-03-13 04:37:11 +00001744 if (Name)
Evan Chenge3d42322009-02-25 07:04:34 +00001745 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf4215332009-01-05 19:55:51 +00001746
Devang Patelad165be2009-01-27 00:45:04 +00001747 if (Tag == DW_TAG_enumeration_type || Tag == DW_TAG_structure_type
1748 || Tag == DW_TAG_union_type) {
1749 // Add size if non-zero (derived types might be zero-sized.)
1750 if (Size)
1751 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1752 else {
1753 // Add zero size if it is not a forward declaration.
1754 if (CTy.isForwardDecl())
1755 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1756 else
1757 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1758 }
1759
1760 // Add source line info if available.
1761 if (!CTy.isForwardDecl())
1762 AddSourceLine(&Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001763 }
Devang Patelf4215332009-01-05 19:55:51 +00001764 }
1765
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001766 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1767 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelf193ff02009-01-15 19:26:23 +00001768 int64_t L = SR.getLo();
1769 int64_t H = SR.getHi();
Devang Patel68afdc32009-01-05 18:33:01 +00001770 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1771 if (L != H) {
1772 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1773 if (L)
Devang Patel2d1768c2009-01-17 08:05:14 +00001774 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1775 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel68afdc32009-01-05 18:33:01 +00001776 }
1777 Buffer.AddChild(DW_Subrange);
1778 }
1779
1780 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1781 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1782 DICompositeType *CTy) {
1783 Buffer.setTag(DW_TAG_array_type);
1784 if (CTy->getTag() == DW_TAG_vector_type)
1785 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1786
Devang Patelf9235742009-01-28 21:08:20 +00001787 // Emit derived type.
1788 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel68afdc32009-01-05 18:33:01 +00001789 DIArray Elements = CTy->getTypeArray();
Devang Patel68afdc32009-01-05 18:33:01 +00001790
1791 // Construct an anonymous type for index type.
1792 DIE IdxBuffer(DW_TAG_base_type);
1793 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1794 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1795 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1796
1797 // Add subranges to array type.
1798 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patelf4215332009-01-05 19:55:51 +00001799 DIDescriptor Element = Elements.getElement(i);
Devang Patelf193ff02009-01-15 19:26:23 +00001800 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1801 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel68afdc32009-01-05 18:33:01 +00001802 }
1803 }
1804
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001805 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Pateleab4a2e2009-01-20 18:35:14 +00001806 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patelc69bf2c2009-01-05 18:38:38 +00001807
1808 DIE *Enumerator = new DIE(DW_TAG_enumerator);
Bill Wendlingc7a09ab2009-03-13 04:37:11 +00001809 const char *Name = ETy->getName();
Evan Chenge3d42322009-02-25 07:04:34 +00001810 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
Devang Patelc69bf2c2009-01-05 18:38:38 +00001811 int64_t Value = ETy->getEnumValue();
1812 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Pateleab4a2e2009-01-20 18:35:14 +00001813 return Enumerator;
Devang Patelc69bf2c2009-01-05 18:38:38 +00001814 }
Devang Patel68afdc32009-01-05 18:33:01 +00001815
Devang Patel5aac3d32009-01-17 08:01:33 +00001816 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001817 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patel5aac3d32009-01-17 08:01:33 +00001818 {
1819 DIE *GVDie = new DIE(DW_TAG_variable);
Bill Wendlingc7a09ab2009-03-13 04:37:11 +00001820 const char *Name = GV.getDisplayName();
Evan Chenge3d42322009-02-25 07:04:34 +00001821 AddString(GVDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendlingc7a09ab2009-03-13 04:37:11 +00001822 const char *LinkageName = GV.getLinkageName();
1823 if (LinkageName)
Devang Patel5aac3d32009-01-17 08:01:33 +00001824 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1825 AddType(DW_Unit, GVDie, GV.getType());
1826 if (!GV.isLocalToUnit())
1827 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
1828 AddSourceLine(GVDie, &GV);
1829 return GVDie;
Devang Patel86ae1422009-01-05 18:59:44 +00001830 }
1831
Devang Patel2be58932009-01-20 21:02:02 +00001832 /// CreateMemberDIE - Create new member DIE.
1833 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
1834 DIE *MemberDie = new DIE(DT.getTag());
Bill Wendlingc7a09ab2009-03-13 04:37:11 +00001835 const char *Name = DT.getName();
1836 if (Name)
Devang Patel2be58932009-01-20 21:02:02 +00001837 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1838
1839 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1840
1841 AddSourceLine(MemberDie, &DT);
1842
Devang Patel36375ee2009-02-17 21:23:59 +00001843 uint64_t Size = DT.getSizeInBits();
1844 uint64_t FieldSize = DT.getOriginalTypeSize();
1845
1846 if (Size != FieldSize) {
1847 // Handle bitfield.
1848 AddUInt(MemberDie, DW_AT_byte_size, 0, DT.getOriginalTypeSize() >> 3);
1849 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1850
1851 uint64_t Offset = DT.getOffsetInBits();
1852 uint64_t FieldOffset = Offset;
1853 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1854 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1855 FieldOffset = (HiMark - FieldSize);
1856 Offset -= FieldOffset;
1857 // Maybe we need to work from the other end.
1858 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1859 AddUInt(MemberDie, DW_AT_bit_offset, 0, Offset);
1860 }
Devang Patel2be58932009-01-20 21:02:02 +00001861 DIEBlock *Block = new DIEBlock();
1862 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1863 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1864 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1865
Devang Patel47661592009-01-21 00:08:04 +00001866 if (DT.isProtected())
1867 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1868 else if (DT.isPrivate())
1869 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1870
Devang Patel2be58932009-01-20 21:02:02 +00001871 return MemberDie;
1872 }
1873
Devang Patel5aac3d32009-01-17 08:01:33 +00001874 /// CreateSubprogramDIE - Create new DIE using SP.
1875 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Devang Patel2d1768c2009-01-17 08:05:14 +00001876 const DISubprogram &SP,
1877 bool IsConstructor = false) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001878 DIE *SPDie = new DIE(DW_TAG_subprogram);
Bill Wendlingc7a09ab2009-03-13 04:37:11 +00001879 const char *Name = SP.getName();
Evan Chenge3d42322009-02-25 07:04:34 +00001880 AddString(SPDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendlingc7a09ab2009-03-13 04:37:11 +00001881 const char *LinkageName = SP.getLinkageName();
1882 if (LinkageName)
Devang Patel5aac3d32009-01-17 08:01:33 +00001883 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Devang Patel2d1768c2009-01-17 08:05:14 +00001884 LinkageName);
Devang Patel5aac3d32009-01-17 08:01:33 +00001885 AddSourceLine(SPDie, &SP);
Devang Patel86ae1422009-01-05 18:59:44 +00001886
Devang Patel5aac3d32009-01-17 08:01:33 +00001887 DICompositeType SPTy = SP.getType();
1888 DIArray Args = SPTy.getTypeArray();
1889
Devang Patel86ae1422009-01-05 18:59:44 +00001890 // Add Return Type.
Devang Patel9ac08d62009-02-27 18:05:21 +00001891 if (!IsConstructor) {
1892 if (Args.isNull())
1893 AddType(DW_Unit, SPDie, SPTy);
1894 else
1895 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
1896 }
Devang Pateld234e592009-01-30 01:21:46 +00001897
Devang Pateld5863dd2009-02-02 17:51:41 +00001898 if (!SP.isDefinition()) {
1899 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
1900 // Add arguments.
1901 // Do not add arguments for subprogram definition. They will be
1902 // handled through RecordVariable.
1903 if (!Args.isNull())
1904 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1905 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1906 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
1907 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
1908 SPDie->AddChild(Arg);
1909 }
1910 }
Devang Pateld234e592009-01-30 01:21:46 +00001911
Devang Patel002ec142009-02-24 00:52:19 +00001912 unsigned Lang = SP.getCompileUnit().getLanguage();
1913 if (Lang == DW_LANG_C99 || Lang == DW_LANG_C89
1914 || Lang == DW_LANG_ObjC)
1915 AddUInt(SPDie, DW_AT_prototyped, DW_FORM_flag, 1);
1916
Devang Patelf193ff02009-01-15 19:26:23 +00001917 if (!SP.isLocalToUnit())
Devang Pateld234e592009-01-30 01:21:46 +00001918 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Devang Patel5aac3d32009-01-17 08:01:33 +00001919 return SPDie;
Devang Patel86ae1422009-01-05 18:59:44 +00001920 }
1921
Devang Patel5aac3d32009-01-17 08:01:33 +00001922 /// FindCompileUnit - Get the compile unit for the given descriptor.
1923 ///
Devang Patel8526cc02009-01-05 22:35:52 +00001924 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
Evan Chenge3d42322009-02-25 07:04:34 +00001925 CompileUnit *DW_Unit = CompileUnitMap[Unit.getGV()];
Devang Patel8526cc02009-01-05 22:35:52 +00001926 assert(DW_Unit && "Missing compile unit.");
1927 return DW_Unit;
1928 }
1929
Devang Patelbbdc8202009-01-13 23:54:55 +00001930 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001931 ///
1932 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1933 // Get the descriptor.
Devang Patel99ec3532009-01-16 19:28:14 +00001934 const DIVariable &VD = DV->getVariable();
Devang Patel7a6e5a32009-01-08 02:33:41 +00001935
1936 // Translate tag to proper Dwarf tag. The result variable is dropped for
1937 // now.
1938 unsigned Tag;
Devang Patel99ec3532009-01-16 19:28:14 +00001939 switch (VD.getTag()) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001940 case DW_TAG_return_variable: return NULL;
1941 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1942 case DW_TAG_auto_variable: // fall thru
1943 default: Tag = DW_TAG_variable; break;
1944 }
1945
1946 // Define variable debug information entry.
1947 DIE *VariableDie = new DIE(Tag);
Bill Wendlingc7a09ab2009-03-13 04:37:11 +00001948 const char *Name = VD.getName();
Evan Chenge3d42322009-02-25 07:04:34 +00001949 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Devang Patel7a6e5a32009-01-08 02:33:41 +00001950
1951 // Add source line info if available.
Devang Patel99ec3532009-01-16 19:28:14 +00001952 AddSourceLine(VariableDie, &VD);
Devang Patel7a6e5a32009-01-08 02:33:41 +00001953
1954 // Add variable type.
Devang Patel99ec3532009-01-16 19:28:14 +00001955 AddType(Unit, VariableDie, VD.getType());
Devang Patel7a6e5a32009-01-08 02:33:41 +00001956
1957 // Add variable address.
1958 MachineLocation Location;
1959 Location.set(RI->getFrameRegister(*MF),
1960 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
1961 AddAddress(VariableDie, DW_AT_location, Location);
1962
1963 return VariableDie;
1964 }
1965
Devang Patel7a6e5a32009-01-08 02:33:41 +00001966 /// getOrCreateScope - Returns the scope associated with the given descriptor.
1967 ///
1968 DbgScope *getOrCreateScope(GlobalVariable *V) {
1969 DbgScope *&Slot = DbgScopeMap[V];
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001970 if (Slot) return Slot;
1971
1972 // FIXME - breaks down when the context is an inlined function.
1973 DIDescriptor ParentDesc;
1974 DIDescriptor Desc(V);
1975
1976 if (Desc.getTag() == dwarf::DW_TAG_lexical_block) {
1977 DIBlock Block(V);
1978 ParentDesc = Block.getContext();
Devang Patel7a6e5a32009-01-08 02:33:41 +00001979 }
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001980
1981 DbgScope *Parent = ParentDesc.isNull() ?
1982 NULL : getOrCreateScope(ParentDesc.getGV());
1983 Slot = new DbgScope(Parent, Desc);
1984
1985 if (Parent) {
1986 Parent->AddScope(Slot);
1987 } else if (RootDbgScope) {
1988 // FIXME - Add inlined function scopes to the root so we can delete them
1989 // later. Long term, handle inlined functions properly.
1990 RootDbgScope->AddScope(Slot);
1991 } else {
1992 // First function is top level function.
1993 RootDbgScope = Slot;
1994 }
1995
Devang Patel7a6e5a32009-01-08 02:33:41 +00001996 return Slot;
1997 }
1998
1999 /// ConstructDbgScope - Construct the components of a scope.
2000 ///
2001 void ConstructDbgScope(DbgScope *ParentScope,
2002 unsigned ParentStartID, unsigned ParentEndID,
2003 DIE *ParentDie, CompileUnit *Unit) {
2004 // Add variables to scope.
Devang Patel9795da52009-01-10 02:42:49 +00002005 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002006 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2007 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2008 if (VariableDie) ParentDie->AddChild(VariableDie);
2009 }
2010
2011 // Add nested scopes.
Devang Patel9795da52009-01-10 02:42:49 +00002012 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002013 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2014 // Define the Scope debug information entry.
2015 DbgScope *Scope = Scopes[j];
2016 // FIXME - Ignore inlined functions for the time being.
2017 if (!Scope->getParent()) continue;
2018
Devang Patele9bfb0f2009-01-12 18:41:00 +00002019 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2020 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002021
2022 // Ignore empty scopes.
2023 if (StartID == EndID && StartID != 0) continue;
2024 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
2025
2026 if (StartID == ParentStartID && EndID == ParentEndID) {
2027 // Just add stuff to the parent scope.
2028 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2029 } else {
2030 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2031
2032 // Add the scope bounds.
2033 if (StartID) {
2034 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2035 DWLabel("label", StartID));
2036 } else {
2037 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2038 DWLabel("func_begin", SubprogramCount));
2039 }
2040 if (EndID) {
2041 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2042 DWLabel("label", EndID));
2043 } else {
2044 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2045 DWLabel("func_end", SubprogramCount));
2046 }
2047
2048 // Add the scope contents.
2049 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2050 ParentDie->AddChild(ScopeDie);
2051 }
2052 }
2053 }
2054
2055 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2056 ///
2057 void ConstructRootDbgScope(DbgScope *RootScope) {
2058 // Exit if there is no root scope.
2059 if (!RootScope) return;
Devang Patel0e5200f2009-01-15 18:25:17 +00002060 DIDescriptor Desc = RootScope->getDesc();
2061 if (Desc.isNull())
2062 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002063
2064 // Get the subprogram debug information entry.
Devang Patel0e5200f2009-01-15 18:25:17 +00002065 DISubprogram SPD(Desc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002066
2067 // Get the compile unit context.
Devang Pateldd9db662009-01-30 18:20:31 +00002068 CompileUnit *Unit = MainCU;
2069 if (!Unit)
2070 Unit = FindCompileUnit(SPD.getCompileUnit());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002071
2072 // Get the subprogram die.
Devang Patelf6bac3e2009-01-12 22:58:14 +00002073 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002074 assert(SPDie && "Missing subprogram descriptor");
2075
2076 // Add the function bounds.
2077 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2078 DWLabel("func_begin", SubprogramCount));
2079 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2080 DWLabel("func_end", SubprogramCount));
2081 MachineLocation Location(RI->getFrameRegister(*MF));
2082 AddAddress(SPDie, DW_AT_frame_base, Location);
2083
2084 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2085 }
2086
2087 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2088 ///
2089 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Chenge3d42322009-02-25 07:04:34 +00002090 const char *FnName = MF->getFunction()->getNameStart();
2091 if (MainCU) {
2092 std::map<std::string, DIE*> &Globals = MainCU->getGlobals();
2093 std::map<std::string, DIE*>::iterator GI = Globals.find(FnName);
2094 if (GI != Globals.end()) {
2095 DIE *SPDie = GI->second;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002096
2097 // Add the function bounds.
2098 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2099 DWLabel("func_begin", SubprogramCount));
2100 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2101 DWLabel("func_end", SubprogramCount));
2102
2103 MachineLocation Location(RI->getFrameRegister(*MF));
2104 AddAddress(SPDie, DW_AT_frame_base, Location);
2105 return;
2106 }
Evan Chenge3d42322009-02-25 07:04:34 +00002107 } else {
2108 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2109 CompileUnit *Unit = CompileUnits[i];
2110 std::map<std::string, DIE*> &Globals = Unit->getGlobals();
2111 std::map<std::string, DIE*>::iterator GI = Globals.find(FnName);
2112 if (GI != Globals.end()) {
2113 DIE *SPDie = GI->second;
2114
2115 // Add the function bounds.
2116 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2117 DWLabel("func_begin", SubprogramCount));
2118 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2119 DWLabel("func_end", SubprogramCount));
2120
2121 MachineLocation Location(RI->getFrameRegister(*MF));
2122 AddAddress(SPDie, DW_AT_frame_base, Location);
2123 return;
2124 }
2125 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00002126 }
Evan Chenge3d42322009-02-25 07:04:34 +00002127
Devang Patel7a6e5a32009-01-08 02:33:41 +00002128#if 0
2129 // FIXME: This is causing an abort because C++ mangled names are compared
2130 // with their unmangled counterparts. See PR2885. Don't do this assert.
2131 assert(0 && "Couldn't find DIE for machine function!");
2132#endif
Evan Chenge3d42322009-02-25 07:04:34 +00002133 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002134 }
2135
Jim Laskeyef42a012006-11-02 20:12:39 +00002136 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2137 /// tools to recognize the object file contains Dwarf information.
2138 void EmitInitial() {
2139 // Check to see if we already emitted intial headers.
2140 if (didInitial) return;
2141 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002142
Jim Laskeyef42a012006-11-02 20:12:39 +00002143 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002144 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002145 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002146 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002147 }
2148 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2149 EmitLabel("section_info", 0);
2150 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2151 EmitLabel("section_abbrev", 0);
2152 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2153 EmitLabel("section_aranges", 0);
Scott Michel210de722009-01-26 22:32:51 +00002154 if (TAI->doesSupportMacInfoSection()) {
2155 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2156 EmitLabel("section_macinfo", 0);
2157 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002158 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2159 EmitLabel("section_line", 0);
2160 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2161 EmitLabel("section_loc", 0);
2162 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2163 EmitLabel("section_pubnames", 0);
2164 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2165 EmitLabel("section_str", 0);
2166 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2167 EmitLabel("section_ranges", 0);
2168
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002169 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002170 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002171 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002172 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002173 }
2174
Jim Laskey65195462006-10-30 13:35:07 +00002175 /// EmitDIE - Recusively Emits a debug information entry.
2176 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002177 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002178 // Get the abbreviation for this DIE.
2179 unsigned AbbrevNumber = Die->getAbbrevNumber();
2180 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002181
Jim Laskeybacd3042007-02-21 22:48:45 +00002182 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002183
2184 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002185 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002186
2187 if (VerboseAsm)
2188 Asm->EOL(std::string("Abbrev [" +
2189 utostr(AbbrevNumber) +
2190 "] 0x" + utohexstr(Die->getOffset()) +
2191 ":0x" + utohexstr(Die->getSize()) + " " +
2192 TagString(Abbrev->getTag())));
2193 else
2194 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002195
Owen Anderson873e1b52008-06-24 21:44:59 +00002196 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2197 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002198
Jim Laskeyef42a012006-11-02 20:12:39 +00002199 // Emit the DIE attribute values.
2200 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2201 unsigned Attr = AbbrevData[i].getAttribute();
2202 unsigned Form = AbbrevData[i].getForm();
2203 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002204
Jim Laskeyef42a012006-11-02 20:12:39 +00002205 switch (Attr) {
2206 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002207 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002208 break;
2209 }
2210 default: {
2211 // Emit an attribute using the defined form.
2212 Values[i]->EmitValue(*this, Form);
2213 break;
2214 }
2215 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002216
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002217 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002218 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002219
Jim Laskeyef42a012006-11-02 20:12:39 +00002220 // Emit the DIE children if any.
2221 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2222 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002223
Jim Laskeyef42a012006-11-02 20:12:39 +00002224 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2225 EmitDIE(Children[j]);
2226 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002227
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002228 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002229 }
2230 }
2231
Jim Laskey65195462006-10-30 13:35:07 +00002232 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2233 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002234 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2235 // Get the children.
2236 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002237
Jim Laskeyef42a012006-11-02 20:12:39 +00002238 // If not last sibling and has children then add sibling offset attribute.
2239 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2240
2241 // Record the abbreviation.
2242 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002243
Jim Laskeyef42a012006-11-02 20:12:39 +00002244 // Get the abbreviation for this DIE.
2245 unsigned AbbrevNumber = Die->getAbbrevNumber();
2246 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2247
2248 // Set DIE offset
2249 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002250
Jim Laskeyef42a012006-11-02 20:12:39 +00002251 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002252 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2253
Owen Anderson873e1b52008-06-24 21:44:59 +00002254 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2255 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002256
2257 // Size the DIE attribute values.
2258 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2259 // Size attribute value.
2260 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2261 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002262
Jim Laskeyef42a012006-11-02 20:12:39 +00002263 // Size the DIE children if any.
2264 if (!Children.empty()) {
2265 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2266 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002267
Jim Laskeyef42a012006-11-02 20:12:39 +00002268 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2269 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2270 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002271
Jim Laskeyef42a012006-11-02 20:12:39 +00002272 // End of children marker.
2273 Offset += sizeof(int8_t);
2274 }
2275
2276 Die->setSize(Offset - Die->getOffset());
2277 return Offset;
2278 }
Jim Laskey65195462006-10-30 13:35:07 +00002279
2280 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2281 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002282 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002283 // Process base compile unit.
Devang Pateldd9db662009-01-30 18:20:31 +00002284 if (MainCU) {
2285 // Compute size of compile unit header
2286 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2287 sizeof(int16_t) + // DWARF version number
2288 sizeof(int32_t) + // Offset Into Abbrev. Section
2289 sizeof(int8_t); // Pointer Size (in bytes)
2290 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
2291 return;
2292 }
Evan Chenge3d42322009-02-25 07:04:34 +00002293 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2294 CompileUnit *Unit = CompileUnits[i];
Devang Patel72b66352009-01-12 23:05:55 +00002295 // Compute size of compile unit header
2296 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2297 sizeof(int16_t) + // DWARF version number
2298 sizeof(int32_t) + // Offset Into Abbrev. Section
2299 sizeof(int8_t); // Pointer Size (in bytes)
2300 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2301 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002302 }
2303
Evan Chenge3d42322009-02-25 07:04:34 +00002304 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Jim Laskey65195462006-10-30 13:35:07 +00002305 ///
Evan Chenge3d42322009-02-25 07:04:34 +00002306 void EmitDebugInfoPerCU(CompileUnit *Unit) {
2307 DIE *Die = Unit->getDie();
2308 // Emit the compile units header.
2309 EmitLabel("info_begin", Unit->getID());
2310 // Emit size of content not including length itself
2311 unsigned ContentSize = Die->getSize() +
2312 sizeof(int16_t) + // DWARF version number
2313 sizeof(int32_t) + // Offset Into Abbrev. Section
2314 sizeof(int8_t) + // Pointer Size (in bytes)
2315 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2316
2317 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2318 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2319 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2320 Asm->EOL("Offset Into Abbrev. Section");
2321 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2322
2323 EmitDIE(Die);
2324 // FIXME - extra padding for gdb bug.
2325 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2326 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2327 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2328 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2329 EmitLabel("info_end", Unit->getID());
2330
2331 Asm->EOL();
2332 }
2333
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002334 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002335 // Start debug info section.
2336 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002337
Evan Chenge3d42322009-02-25 07:04:34 +00002338 if (MainCU) {
2339 EmitDebugInfoPerCU(MainCU);
2340 return;
Devang Patel72b66352009-01-12 23:05:55 +00002341 }
Evan Chenge3d42322009-02-25 07:04:34 +00002342
2343 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2344 EmitDebugInfoPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002345 }
2346
Jim Laskey65195462006-10-30 13:35:07 +00002347 /// EmitAbbreviations - Emit the abbreviation section.
2348 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002349 void EmitAbbreviations() const {
2350 // Check to see if it is worth the effort.
2351 if (!Abbreviations.empty()) {
2352 // Start the debug abbrev section.
2353 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002354
Jim Laskeyef42a012006-11-02 20:12:39 +00002355 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002356
Jim Laskeyef42a012006-11-02 20:12:39 +00002357 // For each abbrevation.
2358 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2359 // Get abbreviation data
2360 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002361
Jim Laskeyef42a012006-11-02 20:12:39 +00002362 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002363 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2364 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002365
Jim Laskeyef42a012006-11-02 20:12:39 +00002366 // Emit the abbreviations data.
2367 Abbrev->Emit(*this);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002368
Jim Laskeybacd3042007-02-21 22:48:45 +00002369 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002370 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002371
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002372 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00002373 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002374
Jim Laskeyef42a012006-11-02 20:12:39 +00002375 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002376
Jim Laskeybacd3042007-02-21 22:48:45 +00002377 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002378 }
2379 }
2380
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002381 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2382 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002383 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002384 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2385 // Define last address of section.
2386 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2387 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2388 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2389 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2390
2391 // Mark end of matrix.
2392 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2393 Asm->EmitULEB128Bytes(1); Asm->EOL();
2394 Asm->EmitInt8(1); Asm->EOL();
2395 }
2396
Jim Laskey65195462006-10-30 13:35:07 +00002397 /// EmitDebugLines - Emit source line information.
2398 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002399 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002400 // If the target is using .loc/.file, the assembler will be emitting the
2401 // .debug_line table automatically.
2402 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00002403 return;
2404
Jim Laskeyef42a012006-11-02 20:12:39 +00002405 // Minimum line delta, thus ranging from -10..(255-10).
2406 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2407 // Maximum line delta, thus ranging from -10..(255-10).
2408 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002409
Jim Laskeyef42a012006-11-02 20:12:39 +00002410 // Start the dwarf line section.
2411 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002412
Jim Laskeyef42a012006-11-02 20:12:39 +00002413 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002414
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002415 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002416 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002417 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002418
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002419 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002420
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002421 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002422 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002423 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002424
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002425 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002426
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002427 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002428
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002429 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002430
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002431 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002432
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002433 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002434
Jim Laskeyef42a012006-11-02 20:12:39 +00002435 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002436 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2437 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2438 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2439 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2440 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2441 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2442 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2443 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2444 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00002445
Jim Laskeyef42a012006-11-02 20:12:39 +00002446 // Emit directories.
Evan Chenge3d42322009-02-25 07:04:34 +00002447 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2448 Asm->EmitString(getSourceDirectoryName(DI));
2449 Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00002450 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002451 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002452
Jim Laskeyef42a012006-11-02 20:12:39 +00002453 // Emit files.
Evan Chenge3d42322009-02-25 07:04:34 +00002454 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2455 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002456 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Evan Chenge3d42322009-02-25 07:04:34 +00002457 Asm->EmitString(getSourceFileName(Id.second));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002458 Asm->EOL("Source");
Evan Chenge3d42322009-02-25 07:04:34 +00002459 Asm->EmitULEB128Bytes(Id.first);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002460 Asm->EOL("Directory #");
2461 Asm->EmitULEB128Bytes(0);
2462 Asm->EOL("Mod date");
2463 Asm->EmitULEB128Bytes(0);
2464 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002465 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002466 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002467
Jim Laskeyef42a012006-11-02 20:12:39 +00002468 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002469
Jim Laskeyef42a012006-11-02 20:12:39 +00002470 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002471 unsigned SecSrcLinesSize = SectionSourceLines.size();
2472
2473 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002474 // Isolate current sections line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00002475 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00002476
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002477 if (VerboseAsm) {
2478 const Section* S = SectionMap[j + 1];
Evan Chenge3d42322009-02-25 07:04:34 +00002479 O << '\t' << TAI->getCommentString() << " Section"
2480 << S->getName() << '\n';
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002481 } else
Evan Cheng6547e402008-07-01 23:18:29 +00002482 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002483
2484 // Dwarf assumes we start with first line of first source file.
2485 unsigned Source = 1;
2486 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002487
Jim Laskeyef42a012006-11-02 20:12:39 +00002488 // Construct rows of the address, source, line, column matrix.
2489 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patelf3ee5142009-01-12 22:54:42 +00002490 const SrcLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002491 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00002492 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002493
Evan Chenge3d42322009-02-25 07:04:34 +00002494 if (!VerboseAsm)
Evan Cheng6547e402008-07-01 23:18:29 +00002495 Asm->EOL();
Evan Chenge3d42322009-02-25 07:04:34 +00002496 else {
2497 std::pair<unsigned, unsigned> SourceID =
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002498 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Evan Chenge3d42322009-02-25 07:04:34 +00002499 O << '\t' << TAI->getCommentString() << ' '
2500 << getSourceDirectoryName(SourceID.first) << ' '
2501 << getSourceFileName(SourceID.second)
2502 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2503 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002504
2505 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002506 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00002507 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002508 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00002509 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002510
Jim Laskeyef42a012006-11-02 20:12:39 +00002511 // If change of source, then switch to the new source.
2512 if (Source != LineInfo.getSourceID()) {
2513 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002514 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2515 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00002516 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002517
Jim Laskeyef42a012006-11-02 20:12:39 +00002518 // If change of line.
2519 if (Line != LineInfo.getLine()) {
2520 // Determine offset.
2521 int Offset = LineInfo.getLine() - Line;
2522 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002523
Jim Laskeyef42a012006-11-02 20:12:39 +00002524 // Update line.
2525 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002526
Jim Laskeyef42a012006-11-02 20:12:39 +00002527 // If delta is small enough and in range...
2528 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2529 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002530 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00002531 } else {
2532 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002533 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2534 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2535 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002536 }
2537 } else {
2538 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002539 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002540 }
2541 }
2542
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002543 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002544 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002545
2546 if (SecSrcLinesSize == 0)
2547 // Because we're emitting a debug_line section, we still need a line
2548 // table. The linker and friends expect it to exist. If there's nothing to
2549 // put into it, emit an empty table.
2550 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002551
Jim Laskeyef42a012006-11-02 20:12:39 +00002552 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002553
Jim Laskeybacd3042007-02-21 22:48:45 +00002554 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002555 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002556
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002557 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00002558 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002559 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002560 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00002561 return;
2562
2563 int stackGrowth =
2564 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2565 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002566 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00002567
2568 // Start the dwarf frame section.
2569 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2570
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002571 EmitLabel("debug_frame_common", 0);
2572 EmitDifference("debug_frame_common_end", 0,
2573 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002574 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002575
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002576 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002577 Asm->EmitInt32((int)DW_CIE_ID);
2578 Asm->EOL("CIE Identifier Tag");
2579 Asm->EmitInt8(DW_CIE_VERSION);
2580 Asm->EOL("CIE Version");
2581 Asm->EmitString("");
2582 Asm->EOL("CIE Augmentation");
2583 Asm->EmitULEB128Bytes(1);
2584 Asm->EOL("CIE Code Alignment Factor");
2585 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002586 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002587 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002588 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002589
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002590 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002591 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002592
Dale Johannesenb97aec62007-11-13 19:13:01 +00002593 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002594
Evan Cheng05548eb2008-02-29 19:36:59 +00002595 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002596 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002597
Jim Laskeybacd3042007-02-21 22:48:45 +00002598 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002599 }
2600
Jim Laskey65195462006-10-30 13:35:07 +00002601 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2602 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002603 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002604 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00002605 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002606
Jim Laskeyef42a012006-11-02 20:12:39 +00002607 // Start the dwarf frame section.
2608 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002609
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002610 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2611 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002612 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002613
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002614 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002615
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002616 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2617 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002618 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002619
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002620 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002621 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002622 EmitDifference("func_end", DebugFrameInfo.Number,
2623 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002624 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002625
Devang Patel5aac3d32009-01-17 08:01:33 +00002626 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00002627 false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002628
Evan Cheng05548eb2008-02-29 19:36:59 +00002629 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002630 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00002631
Jim Laskeybacd3042007-02-21 22:48:45 +00002632 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002633 }
2634
Evan Chenge3d42322009-02-25 07:04:34 +00002635 void EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2636 EmitDifference("pubnames_end", Unit->getID(),
2637 "pubnames_begin", Unit->getID(), true);
2638 Asm->EOL("Length of Public Names Info");
2639
2640 EmitLabel("pubnames_begin", Unit->getID());
2641
2642 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2643
2644 EmitSectionOffset("info_begin", "section_info",
2645 Unit->getID(), 0, true, false);
2646 Asm->EOL("Offset of Compilation Unit Info");
2647
2648 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2649 true);
2650 Asm->EOL("Compilation Unit Length");
2651
2652 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2653 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2654 GE = Globals.end(); GI != GE; ++GI) {
2655 const std::string &Name = GI->first;
2656 DIE * Entity = GI->second;
2657
2658 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2659 Asm->EmitString(Name); Asm->EOL("External Name");
2660 }
2661
2662 Asm->EmitInt32(0); Asm->EOL("End Mark");
2663 EmitLabel("pubnames_end", Unit->getID());
2664
2665 Asm->EOL();
2666 }
2667
Jim Laskeyef42a012006-11-02 20:12:39 +00002668 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002669 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002670 void EmitDebugPubNames() {
2671 // Start the dwarf pubnames section.
2672 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002673
Evan Chenge3d42322009-02-25 07:04:34 +00002674 if (MainCU) {
2675 EmitDebugPubNamesPerCU(MainCU);
2676 return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002677 }
Evan Chenge3d42322009-02-25 07:04:34 +00002678
2679 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2680 EmitDebugPubNamesPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002681 }
2682
2683 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002684 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002685 void EmitDebugStr() {
2686 // Check to see if it is worth the effort.
2687 if (!StringPool.empty()) {
2688 // Start the dwarf str section.
2689 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002690
Jim Laskeyef42a012006-11-02 20:12:39 +00002691 // For each of strings in the string pool.
2692 for (unsigned StringID = 1, N = StringPool.size();
2693 StringID <= N; ++StringID) {
2694 // Emit a label for reference from debug information entries.
2695 EmitLabel("string", StringID);
2696 // Emit the string itself.
2697 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00002698 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002699 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002700
Jim Laskeybacd3042007-02-21 22:48:45 +00002701 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002702 }
2703 }
2704
2705 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002706 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002707 void EmitDebugLoc() {
2708 // Start the dwarf loc section.
2709 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002710
Jim Laskeybacd3042007-02-21 22:48:45 +00002711 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002712 }
2713
2714 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002715 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002716 void EmitDebugARanges() {
2717 // Start the dwarf aranges section.
2718 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002719
Jim Laskeyef42a012006-11-02 20:12:39 +00002720 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00002721#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002722 CompileUnit *Unit = GetBaseCompileUnit();
2723
Jim Laskey5496f012006-11-09 14:52:14 +00002724 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002725 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002726
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002727 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002728
Jim Laskey5496f012006-11-09 14:52:14 +00002729 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002730 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002731
Dan Gohman82482942007-09-27 23:12:31 +00002732 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002733
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002734 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002735
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002736 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2737 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002738
Jim Laskey5496f012006-11-09 14:52:14 +00002739 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002740 EmitReference("text_begin", 0); Asm->EOL("Address");
2741 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002742
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002743 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2744 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00002745#endif
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002746
Jim Laskeybacd3042007-02-21 22:48:45 +00002747 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002748 }
2749
2750 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002751 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002752 void EmitDebugRanges() {
2753 // Start the dwarf ranges section.
2754 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002755
Jim Laskeybacd3042007-02-21 22:48:45 +00002756 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002757 }
2758
2759 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002760 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002761 void EmitDebugMacInfo() {
Scott Michel210de722009-01-26 22:32:51 +00002762 if (TAI->doesSupportMacInfoSection()) {
2763 // Start the dwarf macinfo section.
2764 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002765
Scott Michel210de722009-01-26 22:32:51 +00002766 Asm->EOL();
2767 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002768 }
2769
Bill Wendlingc8615e42009-03-10 21:47:45 +00002770 /// GetOrCreateSourceID - Look up the source id with the given directory and
2771 /// source file names. If none currently exists, create a new id and insert it
2772 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
2773 /// as well.
2774 unsigned GetOrCreateSourceID(const std::string &DirName,
2775 const std::string &FileName) {
2776 unsigned DId;
2777 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
2778 if (DI != DirectoryIdMap.end()) {
2779 DId = DI->getValue();
2780 } else {
2781 DId = DirectoryNames.size() + 1;
2782 DirectoryIdMap[DirName] = DId;
2783 DirectoryNames.push_back(DirName);
2784 }
2785
2786 unsigned FId;
2787 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
2788 if (FI != SourceFileIdMap.end()) {
2789 FId = FI->getValue();
2790 } else {
2791 FId = SourceFileNames.size() + 1;
2792 SourceFileIdMap[FileName] = FId;
2793 SourceFileNames.push_back(FileName);
2794 }
2795
2796 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
2797 SourceIdMap.find(std::make_pair(DId, FId));
2798 if (SI != SourceIdMap.end())
2799 return SI->second;
2800
2801 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
2802 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
2803 SourceIds.push_back(std::make_pair(DId, FId));
2804
2805 return SrcId;
2806 }
2807
Evan Chenge3d42322009-02-25 07:04:34 +00002808 void ConstructCompileUnit(GlobalVariable *GV) {
2809 DICompileUnit DIUnit(GV);
Bill Wendlingc7a09ab2009-03-13 04:37:11 +00002810 const char *Dir = DIUnit.getDirectory();
2811 const char *FN = DIUnit.getFilename();
2812 unsigned ID = GetOrCreateSourceID(Dir, FN);
Evan Chenge3d42322009-02-25 07:04:34 +00002813
2814 DIE *Die = new DIE(DW_TAG_compile_unit);
2815 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2816 DWLabel("section_line", 0), DWLabel("section_line", 0),
2817 false);
Bill Wendlingc7a09ab2009-03-13 04:37:11 +00002818 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer());
Evan Chenge3d42322009-02-25 07:04:34 +00002819 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002820 AddString(Die, DW_AT_name, DW_FORM_string, FN);
Bill Wendlingc7a09ab2009-03-13 04:37:11 +00002821 if (Dir)
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002822 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Chenge3d42322009-02-25 07:04:34 +00002823 if (DIUnit.isOptimized())
2824 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendlingc7a09ab2009-03-13 04:37:11 +00002825 const char *Flags = DIUnit.getFlags();
2826 if (Flags)
Evan Chenge3d42322009-02-25 07:04:34 +00002827 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
2828 unsigned RVer = DIUnit.getRunTimeVersion();
2829 if (RVer)
2830 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
2831
2832 CompileUnit *Unit = new CompileUnit(ID, Die);
2833 if (DIUnit.isMain()) {
2834 assert(!MainCU && "Multiple main compile units are found!");
2835 MainCU = Unit;
2836 }
2837 CompileUnitMap[DIUnit.getGV()] = Unit;
2838 CompileUnits.push_back(Unit);
2839 }
2840
Devang Patelc4523242009-01-05 23:11:11 +00002841 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Pateld1ca9252009-01-05 23:03:32 +00002842 void ConstructCompileUnits() {
Evan Chenge3d42322009-02-25 07:04:34 +00002843 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.compile_units");
2844 if (!Root)
2845 return;
2846 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2847 "Malformed compile unit descriptor anchor type");
2848 Constant *RootC = cast<Constant>(*Root->use_begin());
2849 assert(RootC->hasNUsesOrMore(1) &&
2850 "Malformed compile unit descriptor anchor type");
2851 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2852 UI != UE; ++UI)
2853 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2854 UUI != UUE; ++UUI) {
2855 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2856 ConstructCompileUnit(GV);
Devang Pateldd9db662009-01-30 18:20:31 +00002857 }
Evan Chenge3d42322009-02-25 07:04:34 +00002858 }
2859
2860 bool ConstructGlobalVariableDIE(GlobalVariable *GV) {
2861 DIGlobalVariable DI_GV(GV);
2862 CompileUnit *DW_Unit = MainCU;
2863 if (!DW_Unit)
2864 DW_Unit = FindCompileUnit(DI_GV.getCompileUnit());
2865
2866 // Check for pre-existence.
2867 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
2868 if (Slot)
2869 return false;
2870
2871 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
2872
2873 // Add address.
2874 DIEBlock *Block = new DIEBlock();
2875 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
2876 AddObjectLabel(Block, 0, DW_FORM_udata,
2877 Asm->getGlobalLinkName(DI_GV.getGlobal()));
2878 AddBlock(VariableDie, DW_AT_location, 0, Block);
2879
2880 // Add to map.
2881 Slot = VariableDie;
2882 // Add to context owner.
2883 DW_Unit->getDie()->AddChild(VariableDie);
2884 // Expose as global. FIXME - need to check external flag.
Bill Wendlingc7a09ab2009-03-13 04:37:11 +00002885 DW_Unit->AddGlobal(DI_GV.getName(), VariableDie);
Evan Chenge3d42322009-02-25 07:04:34 +00002886 return true;
Devang Pateld1ca9252009-01-05 23:03:32 +00002887 }
2888
Devang Patelc4523242009-01-05 23:11:11 +00002889 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patel53cac182009-02-24 00:02:15 +00002890 /// visible global variables. Return true if at least one global DIE is
2891 /// created.
2892 bool ConstructGlobalVariableDIEs() {
Evan Chenge3d42322009-02-25 07:04:34 +00002893 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
2894 if (!Root)
2895 return false;
Devang Patelc4523242009-01-05 23:11:11 +00002896
Evan Chenge3d42322009-02-25 07:04:34 +00002897 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2898 "Malformed global variable descriptor anchor type");
2899 Constant *RootC = cast<Constant>(*Root->use_begin());
2900 assert(RootC->hasNUsesOrMore(1) &&
2901 "Malformed global variable descriptor anchor type");
Devang Patelc4523242009-01-05 23:11:11 +00002902
Evan Chenge3d42322009-02-25 07:04:34 +00002903 bool Result = false;
2904 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2905 UI != UE; ++UI)
2906 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2907 UUI != UUE; ++UUI) {
2908 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2909 Result |= ConstructGlobalVariableDIE(GV);
2910 }
2911 return Result;
2912 }
Devang Patelc4523242009-01-05 23:11:11 +00002913
Evan Chenge3d42322009-02-25 07:04:34 +00002914 bool ConstructSubprogram(GlobalVariable *GV) {
2915 DISubprogram SP(GV);
2916 CompileUnit *Unit = MainCU;
2917 if (!Unit)
2918 Unit = FindCompileUnit(SP.getCompileUnit());
Devang Patelc4523242009-01-05 23:11:11 +00002919
Evan Chenge3d42322009-02-25 07:04:34 +00002920 // Check for pre-existence.
2921 DIE *&Slot = Unit->getDieMapSlotFor(GV);
2922 if (Slot)
2923 return false;
2924
2925 if (!SP.isDefinition())
2926 // This is a method declaration which will be handled while
2927 // constructing class type.
2928 return false;
2929
2930 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
2931
2932 // Add to map.
2933 Slot = SubprogramDie;
2934 // Add to context owner.
2935 Unit->getDie()->AddChild(SubprogramDie);
2936 // Expose as global.
Bill Wendlingc7a09ab2009-03-13 04:37:11 +00002937 Unit->AddGlobal(SP.getName(), SubprogramDie);
Evan Chenge3d42322009-02-25 07:04:34 +00002938 return true;
Devang Patelc4523242009-01-05 23:11:11 +00002939 }
2940
Devang Patel78eb6ad2009-01-05 23:21:35 +00002941 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patel53cac182009-02-24 00:02:15 +00002942 /// subprograms. Return true if at least one subprogram DIE is created.
2943 bool ConstructSubprograms() {
Evan Chenge3d42322009-02-25 07:04:34 +00002944 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
2945 if (!Root)
2946 return false;
Devang Patel78eb6ad2009-01-05 23:21:35 +00002947
Evan Chenge3d42322009-02-25 07:04:34 +00002948 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2949 "Malformed subprogram descriptor anchor type");
2950 Constant *RootC = cast<Constant>(*Root->use_begin());
2951 assert(RootC->hasNUsesOrMore(1) &&
2952 "Malformed subprogram descriptor anchor type");
Devang Patel78eb6ad2009-01-05 23:21:35 +00002953
Evan Chenge3d42322009-02-25 07:04:34 +00002954 bool Result = false;
2955 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2956 UI != UE; ++UI)
2957 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2958 UUI != UUE; ++UUI) {
2959 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2960 Result |= ConstructSubprogram(GV);
2961 }
2962 return Result;
Devang Patel78eb6ad2009-01-05 23:21:35 +00002963 }
2964
Jim Laskey65195462006-10-30 13:35:07 +00002965public:
Jim Laskeyef42a012006-11-02 20:12:39 +00002966 //===--------------------------------------------------------------------===//
2967 // Main entry points.
2968 //
Owen Andersoncb371882008-08-21 00:14:44 +00002969 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00002970 : Dwarf(OS, A, T, "dbg"), MainCU(0),
2971 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
2972 ValuesSet(InitValuesSetSize), Values(), StringPool(), SectionMap(),
2973 SectionSourceLines(), didInitial(false), shouldEmit(false),
2974 RootDbgScope(0), DebugTimer(0) {
2975 if (TimePassesIsEnabled)
2976 DebugTimer = new Timer("Dwarf Debug Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00002977 getDwarfTimerGroup());
Jim Laskeyef42a012006-11-02 20:12:39 +00002978 }
Jim Laskey072200c2007-01-29 18:51:14 +00002979 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002980 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2981 delete Values[j];
Bill Wendling163ba3f2009-03-10 21:23:25 +00002982
2983 delete DebugTimer;
Jim Laskeyef42a012006-11-02 20:12:39 +00002984 }
2985
Bill Wendlingc8615e42009-03-10 21:47:45 +00002986 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
2987 /// be emitted.
2988 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
2989
Devang Patel5d315982009-01-06 21:07:30 +00002990 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
2991 /// This is inovked by the target AsmPrinter.
Devang Patel3f7833a2009-01-12 23:09:42 +00002992 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00002993 if (TimePassesIsEnabled)
2994 DebugTimer->startTimer();
2995
Bill Wendlingef7e18e2009-02-03 21:38:21 +00002996 // Create all the compile unit DIEs.
2997 ConstructCompileUnits();
Devang Patel3f7833a2009-01-12 23:09:42 +00002998
Bill Wendling163ba3f2009-03-10 21:23:25 +00002999 if (CompileUnits.empty()) {
3000 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003001 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003002
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003003 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003004 }
Devang Patel3f7833a2009-01-12 23:09:42 +00003005
Devang Patel53cac182009-02-24 00:02:15 +00003006 // Create DIEs for each of the externally visible global variables.
3007 bool globalDIEs = ConstructGlobalVariableDIEs();
3008
3009 // Create DIEs for each of the externally visible subprograms.
3010 bool subprogramDIEs = ConstructSubprograms();
3011
3012 // If there is not any debug info available for any global variables
3013 // and any subprograms then there is not any debug info to emit.
Bill Wendling163ba3f2009-03-10 21:23:25 +00003014 if (!globalDIEs && !subprogramDIEs) {
3015 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003016 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003017
Devang Patel53cac182009-02-24 00:02:15 +00003018 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003019 }
Devang Patel53cac182009-02-24 00:02:15 +00003020
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003021 MMI = mmi;
3022 shouldEmit = true;
3023 MMI->setDebugInfoAvailability(true);
Devang Patel5d315982009-01-06 21:07:30 +00003024
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003025 // Prime section data.
3026 SectionMap.insert(TAI->getTextSection());
Devang Patel5d315982009-01-06 21:07:30 +00003027
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003028 // Print out .file directives to specify files for .loc directives. These
3029 // are printed out early so that they precede any .loc directives.
3030 if (TAI->hasDotLocAndDotFile()) {
Evan Chenge3d42322009-02-25 07:04:34 +00003031 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
3032 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00003033 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Evan Chenge3d42322009-02-25 07:04:34 +00003034 sys::Path FullPath(getSourceDirectoryName(Id.first));
3035 bool AppendOk =
3036 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003037 assert(AppendOk && "Could not append filename to directory!");
3038 AppendOk = false;
3039 Asm->EmitFile(i, FullPath.toString());
3040 Asm->EOL();
Devang Patel5d315982009-01-06 21:07:30 +00003041 }
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003042 }
Devang Patel5d315982009-01-06 21:07:30 +00003043
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003044 // Emit initial sections
3045 EmitInitial();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003046
3047 if (TimePassesIsEnabled)
3048 DebugTimer->stopTimer();
Devang Patel5d315982009-01-06 21:07:30 +00003049 }
3050
Jim Laskey65195462006-10-30 13:35:07 +00003051 /// BeginModule - Emit all Dwarf sections that should come prior to the
3052 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00003053 void BeginModule(Module *M) {
3054 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00003055 }
3056
Jim Laskey65195462006-10-30 13:35:07 +00003057 /// EndModule - Emit all Dwarf sections that should come after the content.
3058 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003059 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003060 if (!ShouldEmitDwarfDebug())
3061 return;
3062
3063 if (TimePassesIsEnabled)
3064 DebugTimer->startTimer();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003065
Jim Laskeyef42a012006-11-02 20:12:39 +00003066 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003067 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003068 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00003069 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003070 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003071
Jim Laskeyef42a012006-11-02 20:12:39 +00003072 // End text sections.
3073 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003074 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00003075 EmitLabel("section_end", i);
3076 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003077
3078 // Emit common frame information.
3079 EmitCommonDebugFrame();
3080
3081 // Emit function debug frame information
3082 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3083 E = DebugFrames.end(); I != E; ++I)
3084 EmitFunctionDebugFrame(*I);
3085
Jim Laskeyef42a012006-11-02 20:12:39 +00003086 // Compute DIE offsets and sizes.
3087 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003088
Jim Laskeyef42a012006-11-02 20:12:39 +00003089 // Emit all the DIEs into a debug info section
3090 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003091
Jim Laskeyef42a012006-11-02 20:12:39 +00003092 // Corresponding abbreviations into a abbrev section.
3093 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003094
Jim Laskeyef42a012006-11-02 20:12:39 +00003095 // Emit source line correspondence into a debug line section.
3096 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003097
Jim Laskeyef42a012006-11-02 20:12:39 +00003098 // Emit info into a debug pubnames section.
3099 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003100
Jim Laskeyef42a012006-11-02 20:12:39 +00003101 // Emit info into a debug str section.
3102 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003103
Jim Laskeyef42a012006-11-02 20:12:39 +00003104 // Emit info into a debug loc section.
3105 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003106
Jim Laskeyef42a012006-11-02 20:12:39 +00003107 // Emit info into a debug aranges section.
3108 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003109
Jim Laskeyef42a012006-11-02 20:12:39 +00003110 // Emit info into a debug ranges section.
3111 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003112
Jim Laskeyef42a012006-11-02 20:12:39 +00003113 // Emit info into a debug macinfo section.
3114 EmitDebugMacInfo();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003115
3116 if (TimePassesIsEnabled)
3117 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003118 }
3119
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003120 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00003121 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00003122 void BeginFunction(MachineFunction *MF) {
Bill Wendling1362f972009-03-11 00:03:50 +00003123 this->MF = MF;
3124
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003125 if (!ShouldEmitDwarfDebug()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00003126
Bill Wendling163ba3f2009-03-10 21:23:25 +00003127 if (TimePassesIsEnabled)
3128 DebugTimer->startTimer();
3129
Jim Laskeyef42a012006-11-02 20:12:39 +00003130 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003131 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003132
Jim Laskeyef42a012006-11-02 20:12:39 +00003133 // Assumes in correct section after the entry point.
3134 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00003135
3136 // Emit label for the implicitly defined dbg.stoppoint at the start of
3137 // the function.
Devang Patelf3ee5142009-01-12 22:54:42 +00003138 if (!Lines.empty()) {
3139 const SrcLineInfo &LineInfo = Lines[0];
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00003140 Asm->printLabel(LineInfo.getLabelID());
3141 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003142
3143 if (TimePassesIsEnabled)
3144 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003145 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003146
Jim Laskey65195462006-10-30 13:35:07 +00003147 /// EndFunction - Gather and emit post-function debug information.
3148 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00003149 void EndFunction(MachineFunction *MF) {
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003150 if (!ShouldEmitDwarfDebug()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003151
Bill Wendling163ba3f2009-03-10 21:23:25 +00003152 if (TimePassesIsEnabled)
3153 DebugTimer->startTimer();
3154
Jim Laskeyb82313f2007-02-01 16:31:34 +00003155 // Define end label for subprogram.
3156 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003157
Jim Laskeyef42a012006-11-02 20:12:39 +00003158 // Get function line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00003159 if (!Lines.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003160 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003161 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00003162 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patelf3ee5142009-01-12 22:54:42 +00003163 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Jim Laskeyef42a012006-11-02 20:12:39 +00003164 // Append the function info to section info.
3165 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patelf3ee5142009-01-12 22:54:42 +00003166 Lines.begin(), Lines.end());
Jim Laskeyef42a012006-11-02 20:12:39 +00003167 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003168
Jim Laskeyef42a012006-11-02 20:12:39 +00003169 // Construct scopes for subprogram.
Devang Patel7bb89ed2009-01-13 00:20:51 +00003170 if (RootDbgScope)
3171 ConstructRootDbgScope(RootDbgScope);
Bill Wendlingd751c642008-09-26 00:28:12 +00003172 else
3173 // FIXME: This is wrong. We are essentially getting past a problem with
3174 // debug information not being able to handle unreachable blocks that have
3175 // debug information in them. In particular, those unreachable blocks that
3176 // have "region end" info in them. That situation results in the "root
3177 // scope" not being created. If that's the case, then emit a "default"
3178 // scope, i.e., one that encompasses the whole function. This isn't
3179 // desirable. And a better way of handling this (and all of the debugging
3180 // information) needs to be explored.
Devang Patel7bb89ed2009-01-13 00:20:51 +00003181 ConstructDefaultDbgScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003182
3183 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3184 MMI->getFrameMoves()));
Devang Patel481ff5b2009-01-12 18:48:36 +00003185
3186 // Clear debug info
3187 if (RootDbgScope) {
3188 delete RootDbgScope;
3189 DbgScopeMap.clear();
3190 RootDbgScope = NULL;
3191 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003192
Bill Wendling163ba3f2009-03-10 21:23:25 +00003193 Lines.clear();
3194
3195 if (TimePassesIsEnabled)
3196 DebugTimer->stopTimer();
3197 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003198
Devang Patelcf3a4482009-01-15 23:41:32 +00003199 /// ValidDebugInfo - Return true if V represents valid debug info value.
3200 bool ValidDebugInfo(Value *V) {
Devang Patelb79b5352009-01-19 23:21:49 +00003201 if (!V)
3202 return false;
3203
Devang Patel61c6bf32009-01-16 01:49:46 +00003204 if (!shouldEmit)
3205 return false;
3206
Devang Patelcf3a4482009-01-15 23:41:32 +00003207 GlobalVariable *GV = getGlobalVariable(V);
3208 if (!GV)
3209 return false;
Duncan Sands667d4b82009-03-07 15:45:40 +00003210
3211 if (!GV->hasInternalLinkage () && !GV->hasLinkOnceLinkage())
Devang Patelcf3a4482009-01-15 23:41:32 +00003212 return false;
3213
Bill Wendling163ba3f2009-03-10 21:23:25 +00003214 if (TimePassesIsEnabled)
3215 DebugTimer->startTimer();
3216
Devang Patelcf3a4482009-01-15 23:41:32 +00003217 DIDescriptor DI(GV);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003218
Devang Patelcf3a4482009-01-15 23:41:32 +00003219 // Check current version. Allow Version6 for now.
3220 unsigned Version = DI.getVersion();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003221 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6) {
3222 if (TimePassesIsEnabled)
3223 DebugTimer->stopTimer();
3224
Devang Patelcf3a4482009-01-15 23:41:32 +00003225 return false;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003226 }
Devang Patelcf3a4482009-01-15 23:41:32 +00003227
Devang Patelb79b5352009-01-19 23:21:49 +00003228 unsigned Tag = DI.getTag();
3229 switch (Tag) {
3230 case DW_TAG_variable:
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003231 assert(DIVariable(GV).Verify() && "Invalid DebugInfo value");
Devang Patelb79b5352009-01-19 23:21:49 +00003232 break;
3233 case DW_TAG_compile_unit:
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003234 assert(DICompileUnit(GV).Verify() && "Invalid DebugInfo value");
Devang Patelb79b5352009-01-19 23:21:49 +00003235 break;
3236 case DW_TAG_subprogram:
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003237 assert(DISubprogram(GV).Verify() && "Invalid DebugInfo value");
Devang Patelb79b5352009-01-19 23:21:49 +00003238 break;
3239 default:
3240 break;
3241 }
3242
Bill Wendling163ba3f2009-03-10 21:23:25 +00003243 if (TimePassesIsEnabled)
3244 DebugTimer->stopTimer();
3245
Devang Patelcf3a4482009-01-15 23:41:32 +00003246 return true;
3247 }
3248
Devang Patelccca7fe2009-01-12 19:17:34 +00003249 /// RecordSourceLine - Records location information and associates it with a
3250 /// label. Returns a unique label ID used to generate a label and provide
3251 /// correspondence to the source line list.
3252 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003253 if (TimePassesIsEnabled)
3254 DebugTimer->startTimer();
3255
Evan Chenge3d42322009-02-25 07:04:34 +00003256 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003257 assert(Unit && "Unable to find CompileUnit");
Devang Patelccca7fe2009-01-12 19:17:34 +00003258 unsigned ID = MMI->NextLabelID();
3259 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003260
3261 if (TimePassesIsEnabled)
3262 DebugTimer->stopTimer();
3263
Devang Patelccca7fe2009-01-12 19:17:34 +00003264 return ID;
3265 }
3266
3267 /// RecordSourceLine - Records location information and associates it with a
3268 /// label. Returns a unique label ID used to generate a label and provide
3269 /// correspondence to the source line list.
3270 unsigned RecordSourceLine(unsigned Line, unsigned Col, unsigned Src) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003271 if (TimePassesIsEnabled)
3272 DebugTimer->startTimer();
3273
Devang Patelccca7fe2009-01-12 19:17:34 +00003274 unsigned ID = MMI->NextLabelID();
3275 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003276
3277 if (TimePassesIsEnabled)
3278 DebugTimer->stopTimer();
3279
Devang Patelccca7fe2009-01-12 19:17:34 +00003280 return ID;
3281 }
3282
Bill Wendlingc8615e42009-03-10 21:47:45 +00003283 /// getRecordSourceLineCount - Return the number of source lines in the debug
3284 /// info.
3285 unsigned getRecordSourceLineCount() const {
Devang Patelccca7fe2009-01-12 19:17:34 +00003286 return Lines.size();
3287 }
3288
Bill Wendlingc8615e42009-03-10 21:47:45 +00003289 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
3290 /// timed. Look up the source id with the given directory and source file
3291 /// names. If none currently exists, create a new id and insert it in the
3292 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
3293 /// well.
Evan Chenge3d42322009-02-25 07:04:34 +00003294 unsigned getOrCreateSourceID(const std::string &DirName,
3295 const std::string &FileName) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003296 if (TimePassesIsEnabled)
3297 DebugTimer->startTimer();
3298
Bill Wendlingc8615e42009-03-10 21:47:45 +00003299 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003300
3301 if (TimePassesIsEnabled)
3302 DebugTimer->stopTimer();
3303
Evan Chenge3d42322009-02-25 07:04:34 +00003304 return SrcId;
Devang Patelccca7fe2009-01-12 19:17:34 +00003305 }
3306
3307 /// RecordRegionStart - Indicate the start of a region.
Devang Patelccca7fe2009-01-12 19:17:34 +00003308 unsigned RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003309 if (TimePassesIsEnabled)
3310 DebugTimer->startTimer();
3311
Devang Patelccca7fe2009-01-12 19:17:34 +00003312 DbgScope *Scope = getOrCreateScope(V);
3313 unsigned ID = MMI->NextLabelID();
3314 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003315
3316 if (TimePassesIsEnabled)
3317 DebugTimer->stopTimer();
3318
Devang Patelccca7fe2009-01-12 19:17:34 +00003319 return ID;
3320 }
3321
3322 /// RecordRegionEnd - Indicate the end of a region.
Devang Patelccca7fe2009-01-12 19:17:34 +00003323 unsigned RecordRegionEnd(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003324 if (TimePassesIsEnabled)
3325 DebugTimer->startTimer();
3326
Devang Patelccca7fe2009-01-12 19:17:34 +00003327 DbgScope *Scope = getOrCreateScope(V);
3328 unsigned ID = MMI->NextLabelID();
3329 Scope->setEndLabelID(ID);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003330
3331 if (TimePassesIsEnabled)
3332 DebugTimer->stopTimer();
3333
Devang Patelccca7fe2009-01-12 19:17:34 +00003334 return ID;
3335 }
3336
3337 /// RecordVariable - Indicate the declaration of a local variable.
Devang Patelccca7fe2009-01-12 19:17:34 +00003338 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003339 if (TimePassesIsEnabled)
3340 DebugTimer->startTimer();
3341
Devang Patel0e5200f2009-01-15 18:25:17 +00003342 DIDescriptor Desc(GV);
3343 DbgScope *Scope = NULL;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003344
Devang Patel0e5200f2009-01-15 18:25:17 +00003345 if (Desc.getTag() == DW_TAG_variable) {
3346 // GV is a global variable.
3347 DIGlobalVariable DG(GV);
3348 Scope = getOrCreateScope(DG.getContext().getGV());
3349 } else {
3350 // or GV is a local variable.
3351 DIVariable DV(GV);
3352 Scope = getOrCreateScope(DV.getContext().getGV());
3353 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003354
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003355 assert(Scope && "Unable to find variable' scope");
Devang Patel99ec3532009-01-16 19:28:14 +00003356 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelccca7fe2009-01-12 19:17:34 +00003357 Scope->AddVariable(DV);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003358
3359 if (TimePassesIsEnabled)
3360 DebugTimer->stopTimer();
Devang Patelccca7fe2009-01-12 19:17:34 +00003361 }
Jim Laskey65195462006-10-30 13:35:07 +00003362};
3363
Jim Laskey072200c2007-01-29 18:51:14 +00003364//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003365/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00003366///
3367class DwarfException : public Dwarf {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003368 struct FunctionEHFrameInfo {
3369 std::string FnName;
3370 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003371 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003372 bool hasCalls;
3373 bool hasLandingPads;
3374 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003375 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00003376
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003377 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3378 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003379 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003380 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003381 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003382 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003383 };
3384
3385 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003386
3387 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3388 /// be emitted.
3389 bool shouldEmitTable;
3390
3391 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3392 /// should be emitted.
3393 bool shouldEmitMoves;
3394
3395 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3396 /// should be emitted.
3397 bool shouldEmitTableModule;
3398
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003399 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003400 /// should be emitted.
3401 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00003402
Bill Wendling163ba3f2009-03-10 21:23:25 +00003403 /// ExceptionTimer - Timer for the Dwarf exception writer.
3404 Timer *ExceptionTimer;
3405
Jim Laskey3f09fc22007-02-28 18:38:31 +00003406 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3407 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003408 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003409 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003410 int stackGrowth =
3411 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3412 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003413 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003414
Jim Laskeybacd3042007-02-21 22:48:45 +00003415 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003416 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling722f5f12008-12-24 08:05:17 +00003417
3418 if (!TAI->doesRequireNonLocalEHFrameLabel())
3419 O << TAI->getEHGlobalPrefix();
3420 O << "EH_frame" << Index << ":\n";
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003421 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003422
Jim Laskeybacd3042007-02-21 22:48:45 +00003423 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003424 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003425
Jim Laskeybacd3042007-02-21 22:48:45 +00003426 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003427 EmitDifference("eh_frame_common_end", Index,
3428 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003429 Asm->EOL("Length of Common Information Entry");
3430
Jim Laskeybacd3042007-02-21 22:48:45 +00003431 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003432 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003433 Asm->EmitInt32((int)0);
3434 Asm->EOL("CIE Identifier Tag");
3435 Asm->EmitInt8(DW_CIE_VERSION);
3436 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00003437
Jim Laskeybacd3042007-02-21 22:48:45 +00003438 // The personality presence indicates that language specific information
3439 // will show up in the eh frame.
3440 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00003441 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00003442
Jim Laskeybacd3042007-02-21 22:48:45 +00003443 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003444 Asm->EmitULEB128Bytes(1);
3445 Asm->EOL("CIE Code Alignment Factor");
3446 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00003447 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003448 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00003449 Asm->EOL("CIE Return Address Column");
3450
Jim Laskeybacd3042007-02-21 22:48:45 +00003451 // If there is a personality, we need to indicate the functions location.
3452 if (Personality) {
3453 Asm->EmitULEB128Bytes(7);
3454 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00003455
Duncan Sands671fa972008-05-07 19:11:09 +00003456 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003457 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00003458 Asm->EOL("Personality (pcrel sdata4 indirect)");
3459 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003460 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00003461 Asm->EOL("Personality (pcrel sdata4)");
3462 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00003463
Duncan Sands671fa972008-05-07 19:11:09 +00003464 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00003465 O << TAI->getPersonalityPrefix();
3466 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3467 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00003468 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3469 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00003470 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00003471
Duncan Sands671fa972008-05-07 19:11:09 +00003472 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3473 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003474
Bill Wendlingd60de512009-01-05 22:53:45 +00003475 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3476 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003477 } else {
3478 Asm->EmitULEB128Bytes(1);
3479 Asm->EOL("Augmentation Size");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003480
Bill Wendlingd60de512009-01-05 22:53:45 +00003481 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3482 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003483 }
3484
3485 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003486 std::vector<MachineMove> Moves;
3487 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00003488 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003489
Dale Johannesen21d972a2008-04-30 00:43:29 +00003490 // On Darwin the linker honors the alignment of eh_frame, which means it
3491 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3492 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003493 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003494 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003495 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003496
Jim Laskeybacd3042007-02-21 22:48:45 +00003497 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003498 }
Duncan Sands671fa972008-05-07 19:11:09 +00003499
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003500 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003501 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003502 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003503 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3504
Jim Laskeybacd3042007-02-21 22:48:45 +00003505 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3506
3507 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003508 // If the corresponding function is static, this should not be
3509 // externally visible.
Rafael Espindolabb46f522009-01-15 20:18:42 +00003510 if (linkage != Function::InternalLinkage &&
Devang Patel2d1768c2009-01-17 08:05:14 +00003511 linkage != Function::PrivateLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003512 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3513 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3514 }
3515
Dale Johannesen038129d2008-01-10 02:03:30 +00003516 // If corresponding function is weak definition, this should be too.
Duncan Sands667d4b82009-03-07 15:45:40 +00003517 if ((linkage == Function::WeakAnyLinkage ||
3518 linkage == Function::WeakODRLinkage ||
3519 linkage == Function::LinkOnceAnyLinkage ||
3520 linkage == Function::LinkOnceODRLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00003521 TAI->getWeakDefDirective())
3522 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3523
3524 // If there are no calls then you can't unwind. This may mean we can
3525 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003526 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00003527 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00003528 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00003529 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00003530 !UnwindTablesMandatory &&
Duncan Sands667d4b82009-03-07 15:45:40 +00003531 ((linkage != Function::WeakAnyLinkage &&
3532 linkage != Function::WeakODRLinkage &&
3533 linkage != Function::LinkOnceAnyLinkage &&
3534 linkage != Function::LinkOnceODRLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00003535 !TAI->getWeakDefDirective() ||
3536 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003537 {
Bill Wendling6e198962007-09-18 01:47:22 +00003538 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003539 // This name has no connection to the function, so it might get
3540 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003541 // dead-stripping unconditionally.
3542 if (const char *UsedDirective = TAI->getUsedDirective())
3543 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00003544 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00003545 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003546
Jim Laskeybacd3042007-02-21 22:48:45 +00003547 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003548 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3549 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003550 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003551
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003552 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003553
Bill Wendling722f5f12008-12-24 08:05:17 +00003554 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3555 PrintRelDirective(true, true);
3556 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3557
3558 if (!TAI->isAbsoluteEHSectionOffsets())
3559 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3560 } else {
3561 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3562 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3563 true, true, false);
3564 }
3565
Jim Laskeybacd3042007-02-21 22:48:45 +00003566 Asm->EOL("FDE CIE offset");
3567
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003568 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003569 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003570 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003571 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003572 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00003573
Jim Laskey3f09fc22007-02-28 18:38:31 +00003574 // If there is a personality and landing pads then point to the language
3575 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003576 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00003577 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00003578 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00003579
3580 if (EHFrameInfo.hasLandingPads)
3581 EmitReference("exception", EHFrameInfo.Number, true, true);
3582 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00003583 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00003584 Asm->EOL("Language Specific Data Area");
3585 } else {
3586 Asm->EmitULEB128Bytes(0);
3587 Asm->EOL("Augmentation size");
3588 }
Duncan Sands671fa972008-05-07 19:11:09 +00003589
Jim Laskeybacd3042007-02-21 22:48:45 +00003590 // Indicate locations of function specific callee saved registers in
3591 // frame.
Devang Patel5aac3d32009-01-17 08:01:33 +00003592 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00003593 true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003594
Dale Johannesen21d972a2008-04-30 00:43:29 +00003595 // On Darwin the linker honors the alignment of eh_frame, which means it
3596 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3597 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003598 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003599 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003600 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003601
3602 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003603 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003604 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003605 // that depends on unused functions (calling undefined externals) being
3606 // dead-stripped to link correctly. Yes, there really is.
3607 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3608 if (const char *UsedDirective = TAI->getUsedDirective())
3609 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3610 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003611 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003612
Duncan Sands57810cd2007-09-05 11:27:52 +00003613 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003614 ///
3615 /// The general organization of the table is complex, but the basic concepts
3616 /// are easy. First there is a header which describes the location and
3617 /// organization of the three components that follow.
3618 /// 1. The landing pad site information describes the range of code covered
3619 /// by the try. In our case it's an accumulation of the ranges covered
3620 /// by the invokes in the try. There is also a reference to the landing
3621 /// pad that handles the exception once processed. Finally an index into
3622 /// the actions table.
3623 /// 2. The action table, in our case, is composed of pairs of type ids
3624 /// and next action offset. Starting with the action index from the
3625 /// landing pad site, each type Id is checked for a match to the current
3626 /// exception. If it matches then the exception and type id are passed
3627 /// on to the landing pad. Otherwise the next action is looked up. This
3628 /// chain is terminated with a next action of zero. If no type id is
3629 /// found the the frame is unwound and handling continues.
3630 /// 3. Type id table contains references to all the C++ typeinfo for all
3631 /// catches in the function. This tables is reversed indexed base 1.
3632
Duncan Sandsb32edb42007-06-06 15:37:31 +00003633 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3634 static unsigned SharedTypeIds(const LandingPadInfo *L,
3635 const LandingPadInfo *R) {
3636 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003637 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003638 unsigned MinSize = LSize < RSize ? LSize : RSize;
3639 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003640
Duncan Sandsb32edb42007-06-06 15:37:31 +00003641 for (; Count != MinSize; ++Count)
3642 if (LIds[Count] != RIds[Count])
3643 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003644
Duncan Sandsb32edb42007-06-06 15:37:31 +00003645 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003646 }
3647
Duncan Sandsb32edb42007-06-06 15:37:31 +00003648 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00003649 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003650 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003651 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003652 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003653
Duncan Sandsb32edb42007-06-06 15:37:31 +00003654 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00003655 if (LIds[i] != RIds[i])
3656 return LIds[i] < RIds[i];
3657
Duncan Sandsb32edb42007-06-06 15:37:31 +00003658 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003659 }
3660
Duncan Sands53c3a332007-05-16 12:12:23 +00003661 struct KeyInfo {
3662 static inline unsigned getEmptyKey() { return -1U; }
3663 static inline unsigned getTombstoneKey() { return -2U; }
3664 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00003665 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00003666 static bool isPod() { return true; }
3667 };
3668
Duncan Sands57810cd2007-09-05 11:27:52 +00003669 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003670 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003671 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003672 int NextAction;
3673 struct ActionEntry *Previous;
3674 };
3675
Duncan Sands57810cd2007-09-05 11:27:52 +00003676 /// PadRange - Structure holding a try-range and the associated landing pad.
3677 struct PadRange {
3678 // The index of the landing pad.
3679 unsigned PadIndex;
3680 // The index of the begin and end labels in the landing pad's label lists.
3681 unsigned RangeIndex;
3682 };
3683
3684 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3685
3686 /// CallSiteEntry - Structure describing an entry in the call-site table.
3687 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00003688 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003689 unsigned BeginLabel; // zero indicates the start of the function.
3690 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00003691 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003692 unsigned PadLabel; // zero indicates that there is no landing pad.
3693 unsigned Action;
3694 };
3695
Jim Laskeybacd3042007-02-21 22:48:45 +00003696 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00003697 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00003698 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00003699 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3700 if (PadInfos.empty()) return;
3701
3702 // Sort the landing pads in order of their type ids. This is used to fold
3703 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00003704 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00003705 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00003706 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00003707 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003708 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3709
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003710 // Negative type ids index into FilterIds, positive type ids index into
3711 // TypeInfos. The value written for a positive type id is just the type
3712 // id itself. For a negative type id, however, the value written is the
3713 // (negative) byte offset of the corresponding FilterIds entry. The byte
3714 // offset is usually equal to the type id, because the FilterIds entries
3715 // are written using a variable width encoding which outputs one byte per
3716 // entry as long as the value written is not too large, but can differ.
3717 // This kind of complication does not occur for positive type ids because
3718 // type infos are output using a fixed width encoding.
3719 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3720 SmallVector<int, 16> FilterOffsets;
3721 FilterOffsets.reserve(FilterIds.size());
3722 int Offset = -1;
3723 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3724 E = FilterIds.end(); I != E; ++I) {
3725 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003726 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003727 }
3728
Duncan Sands57810cd2007-09-05 11:27:52 +00003729 // Compute the actions table and gather the first action index for each
3730 // landing pad site.
3731 SmallVector<ActionEntry, 32> Actions;
3732 SmallVector<unsigned, 64> FirstActions;
3733 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00003734
Duncan Sandsb32edb42007-06-06 15:37:31 +00003735 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00003736 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003737 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003738 const LandingPadInfo *LP = LandingPads[i];
3739 const std::vector<int> &TypeIds = LP->TypeIds;
3740 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003741 unsigned SizeSiteActions = 0;
3742
Duncan Sandsb32edb42007-06-06 15:37:31 +00003743 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00003744 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00003745 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003746
Duncan Sandsb32edb42007-06-06 15:37:31 +00003747 if (NumShared) {
3748 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3749 assert(Actions.size());
3750 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003751 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3752 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003753 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003754 SizeAction -=
3755 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003756 SizeAction += -PrevAction->NextAction;
3757 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003758 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00003759 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003760
Duncan Sandsb32edb42007-06-06 15:37:31 +00003761 // Compute the actions.
3762 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3763 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003764 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3765 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003766 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003767
3768 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003769 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003770 SizeSiteActions += SizeAction;
3771
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003772 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00003773 Actions.push_back(Action);
3774
3775 PrevAction = &Actions.back();
3776 }
3777
3778 // Record the first action of the landing pad site.
3779 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3780 } // else identical - re-use previous FirstAction
3781
3782 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003783
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00003784 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00003785 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00003786 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003787
Duncan Sands481dc722007-12-19 07:36:31 +00003788 // Compute the call-site table. The entry for an invoke has a try-range
3789 // containing the call, a non-zero landing pad and an appropriate action.
3790 // The entry for an ordinary call has a try-range containing the call and
3791 // zero for the landing pad and the action. Calls marked 'nounwind' have
3792 // no entry and must not be contained in the try-range of any entry - they
3793 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003794 SmallVector<CallSiteEntry, 64> CallSites;
3795
3796 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00003797 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3798 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3799 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00003800 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3801 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00003802 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003803 unsigned BeginLabel = LandingPad->BeginLabels[j];
3804 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3805 PadRange P = { i, j };
3806 PadMap[BeginLabel] = P;
3807 }
3808 }
3809
Duncan Sands481dc722007-12-19 07:36:31 +00003810 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00003811 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00003812
3813 // Whether there is a potentially throwing instruction (currently this means
3814 // an ordinary call) between the end of the previous try-range and now.
3815 bool SawPotentiallyThrowing = false;
3816
3817 // Whether the last callsite entry was for an invoke.
3818 bool PreviousIsInvoke = false;
3819
Duncan Sands481dc722007-12-19 07:36:31 +00003820 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003821 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3822 I != E; ++I) {
3823 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3824 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00003825 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00003826 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00003827 continue;
3828 }
3829
Chris Lattner9e330492007-12-30 20:50:28 +00003830 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00003831 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00003832
Duncan Sands481dc722007-12-19 07:36:31 +00003833 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00003834 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00003835 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003836
Duncan Sands481dc722007-12-19 07:36:31 +00003837 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00003838 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00003839 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00003840 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00003841 continue;
3842
3843 PadRange P = L->second;
3844 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3845
3846 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3847 "Inconsistent landing pad map!");
3848
3849 // If some instruction between the previous try-range and this one may
3850 // throw, create a call-site entry with no landing pad for the region
3851 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00003852 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003853 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3854 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00003855 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003856 }
3857
3858 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00003859 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00003860
Duncan Sands481dc722007-12-19 07:36:31 +00003861 if (LandingPad->LandingPadLabel) {
3862 // This try-range is for an invoke.
3863 CallSiteEntry Site = {BeginLabel, LastLabel,
3864 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00003865
Duncan Sands481dc722007-12-19 07:36:31 +00003866 // Try to merge with the previous call-site.
3867 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00003868 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00003869 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3870 // Extend the range of the previous entry.
3871 Prev.EndLabel = Site.EndLabel;
3872 continue;
3873 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003874 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003875
Duncan Sands481dc722007-12-19 07:36:31 +00003876 // Otherwise, create a new call-site.
3877 CallSites.push_back(Site);
3878 PreviousIsInvoke = true;
3879 } else {
3880 // Create a gap.
3881 PreviousIsInvoke = false;
3882 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003883 }
3884 }
3885 // If some instruction between the previous try-range and the end of the
3886 // function may throw, create a call-site entry with no landing pad for the
3887 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00003888 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003889 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3890 CallSites.push_back(Site);
3891 }
3892
Jim Laskeybacd3042007-02-21 22:48:45 +00003893 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00003894
3895 // Call sites.
3896 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
3897 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
3898 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
3899 unsigned SizeSites = CallSites.size() * (SiteStartSize +
3900 SiteLengthSize +
3901 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00003902 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003903 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00003904
Duncan Sands671fa972008-05-07 19:11:09 +00003905 // Type infos.
3906 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3907 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003908
3909 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003910 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003911 SizeSites + SizeActions + SizeTypes;
3912
3913 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3914 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003915 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003916 TypeOffset;
3917
3918 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003919
Jim Laskeybacd3042007-02-21 22:48:45 +00003920 // Begin the exception table.
3921 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng05548eb2008-02-29 19:36:59 +00003922 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesend65b2642008-10-08 21:50:21 +00003923 O << "GCC_except_table" << SubprogramCount << ":\n";
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003924 for (unsigned i = 0; i != SizeAlign; ++i) {
3925 Asm->EmitInt8(0);
3926 Asm->EOL("Padding");
3927 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003928 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003929
Jim Laskeybacd3042007-02-21 22:48:45 +00003930 // Emit the header.
3931 Asm->EmitInt8(DW_EH_PE_omit);
3932 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3933 Asm->EmitInt8(DW_EH_PE_absptr);
3934 Asm->EOL("TType format (DW_EH_PE_absptr)");
3935 Asm->EmitULEB128Bytes(TypeOffset);
3936 Asm->EOL("TType base offset");
3937 Asm->EmitInt8(DW_EH_PE_udata4);
3938 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3939 Asm->EmitULEB128Bytes(SizeSites);
3940 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00003941
Duncan Sands57810cd2007-09-05 11:27:52 +00003942 // Emit the landing pad site information.
3943 for (unsigned i = 0; i < CallSites.size(); ++i) {
3944 CallSiteEntry &S = CallSites[i];
3945 const char *BeginTag;
3946 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00003947
Duncan Sands57810cd2007-09-05 11:27:52 +00003948 if (!S.BeginLabel) {
3949 BeginTag = "eh_func_begin";
3950 BeginNumber = SubprogramCount;
3951 } else {
3952 BeginTag = "label";
3953 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00003954 }
Duncan Sands53c3a332007-05-16 12:12:23 +00003955
Duncan Sands57810cd2007-09-05 11:27:52 +00003956 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00003957 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003958 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00003959
Duncan Sands57810cd2007-09-05 11:27:52 +00003960 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00003961 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00003962 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003963 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00003964 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003965 }
3966 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00003967
Duncan Sands671fa972008-05-07 19:11:09 +00003968 if (!S.PadLabel)
3969 Asm->EmitInt32(0);
3970 else
Duncan Sands57810cd2007-09-05 11:27:52 +00003971 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00003972 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003973 Asm->EOL("Landing pad");
3974
3975 Asm->EmitULEB128Bytes(S.Action);
3976 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00003977 }
Duncan Sands53c3a332007-05-16 12:12:23 +00003978
Jim Laskeybacd3042007-02-21 22:48:45 +00003979 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003980 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
3981 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00003982
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003983 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003984 Asm->EOL("TypeInfo index");
3985 Asm->EmitSLEB128Bytes(Action.NextAction);
3986 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00003987 }
3988
3989 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00003990 for (unsigned M = TypeInfos.size(); M; --M) {
3991 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00003992
3993 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00003994
3995 if (GV)
3996 O << Asm->getGlobalLinkName(GV);
3997 else
3998 O << "0";
Duncan Sands57810cd2007-09-05 11:27:52 +00003999
Jim Laskeybacd3042007-02-21 22:48:45 +00004000 Asm->EOL("TypeInfo");
4001 }
4002
Duncan Sands73ef58a2007-06-02 16:53:42 +00004003 // Emit the filter typeids.
4004 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4005 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00004006 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00004007 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00004008 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004009
Evan Cheng05548eb2008-02-29 19:36:59 +00004010 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004011 }
4012
Jim Laskey072200c2007-01-29 18:51:14 +00004013public:
4014 //===--------------------------------------------------------------------===//
4015 // Main entry points.
4016 //
Owen Andersoncb371882008-08-21 00:14:44 +00004017 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00004018 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
4019 shouldEmitTableModule(false), shouldEmitMovesModule(false),
4020 ExceptionTimer(0) {
4021 if (TimePassesIsEnabled)
4022 ExceptionTimer = new Timer("Dwarf Exception Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00004023 getDwarfTimerGroup());
Bill Wendling163ba3f2009-03-10 21:23:25 +00004024 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004025
Bill Wendling163ba3f2009-03-10 21:23:25 +00004026 virtual ~DwarfException() {
4027 delete ExceptionTimer;
4028 }
Jim Laskey072200c2007-01-29 18:51:14 +00004029
4030 /// SetModuleInfo - Set machine module information when it's known that pass
4031 /// manager has created it. Set by the target AsmPrinter.
4032 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00004033 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00004034 }
4035
4036 /// BeginModule - Emit all exception information that should come prior to the
4037 /// content.
4038 void BeginModule(Module *M) {
4039 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00004040 }
4041
4042 /// EndModule - Emit all exception information that should come after the
4043 /// content.
4044 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004045 if (TimePassesIsEnabled)
4046 ExceptionTimer->startTimer();
4047
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004048 if (shouldEmitMovesModule || shouldEmitTableModule) {
4049 const std::vector<Function *> Personalities = MMI->getPersonalities();
Evan Chenge3d42322009-02-25 07:04:34 +00004050 for (unsigned i = 0; i < Personalities.size(); ++i)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004051 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00004052
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004053 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4054 E = EHFrames.end(); I != E; ++I)
4055 EmitEHFrame(*I);
4056 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004057
4058 if (TimePassesIsEnabled)
4059 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004060 }
4061
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004062 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00004063 /// emitted immediately after the function entry point.
4064 void BeginFunction(MachineFunction *MF) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004065 if (TimePassesIsEnabled)
4066 ExceptionTimer->startTimer();
4067
Jim Laskey072200c2007-01-29 18:51:14 +00004068 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004069 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004070
Bill Wendling163ba3f2009-03-10 21:23:25 +00004071 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004072 // Map all labels and get rid of any dead landing pads.
4073 MMI->TidyLandingPads();
Bill Wendling163ba3f2009-03-10 21:23:25 +00004074
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004075 // If any landing pads survive, we need an EH table.
4076 if (MMI->getLandingPads().size())
4077 shouldEmitTable = true;
4078
4079 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00004080 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004081 shouldEmitMoves = true;
4082
4083 if (shouldEmitMoves || shouldEmitTable)
4084 // Assumes in correct section after the entry point.
4085 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00004086 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004087
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004088 shouldEmitTableModule |= shouldEmitTable;
4089 shouldEmitMovesModule |= shouldEmitMoves;
Bill Wendling163ba3f2009-03-10 21:23:25 +00004090
4091 if (TimePassesIsEnabled)
4092 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004093 }
4094
4095 /// EndFunction - Gather and emit post-function exception information.
4096 ///
4097 void EndFunction() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004098 if (TimePassesIsEnabled)
4099 ExceptionTimer->startTimer();
4100
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004101 if (shouldEmitMoves || shouldEmitTable) {
4102 EmitLabel("eh_func_end", SubprogramCount);
4103 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00004104
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004105 // Save EH frame information
4106 EHFrames.
4107 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendling163ba3f2009-03-10 21:23:25 +00004108 SubprogramCount,
4109 MMI->getPersonalityIndex(),
4110 MF->getFrameInfo()->hasCalls(),
4111 !MMI->getLandingPads().empty(),
4112 MMI->getFrameMoves(),
4113 MF->getFunction()));
4114 }
4115
4116 if (TimePassesIsEnabled)
4117 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004118 }
4119};
4120
Jim Laskey0d086af2006-02-27 12:43:29 +00004121} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00004122
4123//===----------------------------------------------------------------------===//
4124
Jim Laskeyd18e2892006-01-20 20:34:06 +00004125/// Emit - Print the abbreviation using the specified Dwarf writer.
4126///
Jim Laskey072200c2007-01-29 18:51:14 +00004127void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004128 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00004129 DD.getAsm()->EmitULEB128Bytes(Tag);
4130 DD.getAsm()->EOL(TagString(Tag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004131
Jim Laskeyd18e2892006-01-20 20:34:06 +00004132 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00004133 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4134 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004135
Jim Laskeyd18e2892006-01-20 20:34:06 +00004136 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00004137 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004138 const DIEAbbrevData &AttrData = Data[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004139
Jim Laskeyd18e2892006-01-20 20:34:06 +00004140 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00004141 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4142 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004143
Jim Laskeyd18e2892006-01-20 20:34:06 +00004144 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00004145 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4146 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00004147 }
4148
4149 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00004150 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4151 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00004152}
4153
4154#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00004155void DIEAbbrev::print(std::ostream &O) {
4156 O << "Abbreviation @"
4157 << std::hex << (intptr_t)this << std::dec
4158 << " "
4159 << TagString(Tag)
4160 << " "
4161 << ChildrenString(ChildrenFlag)
4162 << "\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004163
Jim Laskeya0f3d172006-09-07 22:06:40 +00004164 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4165 O << " "
4166 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004167 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00004168 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004169 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00004170 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00004171}
Bill Wendlinge8156192006-12-07 01:30:32 +00004172void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00004173#endif
4174
4175//===----------------------------------------------------------------------===//
4176
Jim Laskeyef42a012006-11-02 20:12:39 +00004177#ifndef NDEBUG
4178void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004179 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004180}
Jim Laskeyef42a012006-11-02 20:12:39 +00004181#endif
4182
4183//===----------------------------------------------------------------------===//
4184
Jim Laskey063e7652006-01-17 17:31:53 +00004185/// EmitValue - Emit integer of appropriate size.
4186///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004187void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00004188 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00004189 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00004190 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004191 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004192 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004193 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004194 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004195 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004196 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004197 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4198 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4199 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004200 default: assert(0 && "DIE Value form not supported yet"); break;
4201 }
4202}
4203
4204/// SizeOf - Determine size of integer value in bytes.
4205///
Jim Laskey072200c2007-01-29 18:51:14 +00004206unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004207 switch (Form) {
4208 case DW_FORM_flag: // Fall thru
4209 case DW_FORM_ref1: // Fall thru
4210 case DW_FORM_data1: return sizeof(int8_t);
4211 case DW_FORM_ref2: // Fall thru
4212 case DW_FORM_data2: return sizeof(int16_t);
4213 case DW_FORM_ref4: // Fall thru
4214 case DW_FORM_data4: return sizeof(int32_t);
4215 case DW_FORM_ref8: // Fall thru
4216 case DW_FORM_data8: return sizeof(int64_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004217 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4218 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00004219 default: assert(0 && "DIE Value form not supported yet"); break;
4220 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004221 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00004222}
4223
Jim Laskey063e7652006-01-17 17:31:53 +00004224//===----------------------------------------------------------------------===//
4225
4226/// EmitValue - Emit string value.
4227///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004228void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Bill Wendlingdd446322009-03-10 23:57:09 +00004229 DD.getAsm()->EmitString(Str);
Jim Laskey063e7652006-01-17 17:31:53 +00004230}
4231
Jim Laskey063e7652006-01-17 17:31:53 +00004232//===----------------------------------------------------------------------===//
4233
4234/// EmitValue - Emit label value.
4235///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004236void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004237 bool IsSmall = Form == DW_FORM_data4;
4238 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004239}
4240
4241/// SizeOf - Determine size of label value in bytes.
4242///
Jim Laskey072200c2007-01-29 18:51:14 +00004243unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004244 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004245 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004246}
Jim Laskeyef42a012006-11-02 20:12:39 +00004247
Jim Laskey063e7652006-01-17 17:31:53 +00004248//===----------------------------------------------------------------------===//
4249
Jim Laskeyd18e2892006-01-20 20:34:06 +00004250/// EmitValue - Emit label value.
4251///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004252void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004253 bool IsSmall = Form == DW_FORM_data4;
4254 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00004255}
4256
4257/// SizeOf - Determine size of label value in bytes.
4258///
Jim Laskey072200c2007-01-29 18:51:14 +00004259unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004260 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004261 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00004262}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004263
Jim Laskeyd18e2892006-01-20 20:34:06 +00004264//===----------------------------------------------------------------------===//
4265
Jim Laskey063e7652006-01-17 17:31:53 +00004266/// EmitValue - Emit delta value.
4267///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004268void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4269 bool IsSmall = Form == DW_FORM_data4;
4270 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4271 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4272}
4273
4274/// SizeOf - Determine size of delta value in bytes.
4275///
4276unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4277 if (Form == DW_FORM_data4) return 4;
4278 return DD.getTargetData()->getPointerSize();
4279}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004280
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004281//===----------------------------------------------------------------------===//
4282
4283/// EmitValue - Emit delta value.
4284///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004285void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004286 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00004287 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004288}
4289
4290/// SizeOf - Determine size of delta value in bytes.
4291///
Jim Laskey072200c2007-01-29 18:51:14 +00004292unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004293 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004294 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004295}
4296
4297//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004298
Jim Laskeyb8509c52006-03-23 18:07:55 +00004299/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004300///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004301void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004302 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00004303}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004304
Jim Laskeyd18e2892006-01-20 20:34:06 +00004305//===----------------------------------------------------------------------===//
4306
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004307/// ComputeSize - calculate the size of the block.
4308///
Jim Laskey072200c2007-01-29 18:51:14 +00004309unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00004310 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00004311 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004312
Jim Laskeyef42a012006-11-02 20:12:39 +00004313 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00004314 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00004315 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004316 }
4317 return Size;
4318}
4319
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004320/// EmitValue - Emit block data.
4321///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004322void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004323 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004324 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4325 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4326 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4327 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004328 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004329 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004330
Owen Anderson873e1b52008-06-24 21:44:59 +00004331 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00004332
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004333 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00004334 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00004335 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004336 }
4337}
4338
4339/// SizeOf - Determine size of block data in bytes.
4340///
Jim Laskey072200c2007-01-29 18:51:14 +00004341unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004342 switch (Form) {
4343 case DW_FORM_block1: return Size + sizeof(int8_t);
4344 case DW_FORM_block2: return Size + sizeof(int16_t);
4345 case DW_FORM_block4: return Size + sizeof(int32_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004346 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004347 default: assert(0 && "Improper form for block"); break;
4348 }
4349 return 0;
4350}
4351
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004352//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004353/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00004354
4355DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00004356 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00004357 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00004358}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004359
Jim Laskeyb8509c52006-03-23 18:07:55 +00004360/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4361///
4362void DIE::AddSiblingOffset() {
4363 DIEInteger *DI = new DIEInteger(0);
4364 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00004365 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00004366}
4367
Jim Laskeyef42a012006-11-02 20:12:39 +00004368/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004369///
Jim Laskeyef42a012006-11-02 20:12:39 +00004370void DIE::Profile(FoldingSetNodeID &ID) {
4371 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004372
Jim Laskeyef42a012006-11-02 20:12:39 +00004373 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4374 ID.AddPointer(Children[i]);
4375
4376 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4377 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00004378}
Jim Laskeyef42a012006-11-02 20:12:39 +00004379
4380#ifndef NDEBUG
4381void DIE::print(std::ostream &O, unsigned IncIndent) {
4382 static unsigned IndentCount = 0;
4383 IndentCount += IncIndent;
4384 const std::string Indent(IndentCount, ' ');
4385 bool isBlock = Abbrev.getTag() == 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004386
Jim Laskeyef42a012006-11-02 20:12:39 +00004387 if (!isBlock) {
4388 O << Indent
4389 << "Die: "
4390 << "0x" << std::hex << (intptr_t)this << std::dec
4391 << ", Offset: " << Offset
4392 << ", Size: " << Size
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004393 << "\n";
4394
Jim Laskeyef42a012006-11-02 20:12:39 +00004395 O << Indent
4396 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00004397 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00004398 << ChildrenString(Abbrev.getChildrenFlag());
4399 } else {
4400 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00004401 }
4402 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00004403
Owen Anderson873e1b52008-06-24 21:44:59 +00004404 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004405
Jim Laskeyef42a012006-11-02 20:12:39 +00004406 IndentCount += 2;
4407 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4408 O << Indent;
Bill Wendling4e974012008-07-22 00:28:47 +00004409
4410 if (!isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +00004411 O << AttributeString(Data[i].getAttribute());
Bill Wendling4e974012008-07-22 00:28:47 +00004412 else
Jim Laskeyef42a012006-11-02 20:12:39 +00004413 O << "Blk[" << i << "]";
Bill Wendling4e974012008-07-22 00:28:47 +00004414
Jim Laskeyef42a012006-11-02 20:12:39 +00004415 O << " "
4416 << FormEncodingString(Data[i].getForm())
4417 << " ";
4418 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00004419 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00004420 }
Jim Laskeyef42a012006-11-02 20:12:39 +00004421 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00004422
Jim Laskeyef42a012006-11-02 20:12:39 +00004423 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4424 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00004425 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004426
Jim Laskeyef42a012006-11-02 20:12:39 +00004427 if (!isBlock) O << "\n";
4428 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00004429}
4430
Jim Laskeyef42a012006-11-02 20:12:39 +00004431void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004432 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00004433}
Jim Laskeybd761842006-02-27 17:27:12 +00004434#endif
Jim Laskey65195462006-10-30 13:35:07 +00004435
4436//===----------------------------------------------------------------------===//
4437/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00004438///
Jim Laskey65195462006-10-30 13:35:07 +00004439
Bill Wendling91b8b802009-03-10 20:41:52 +00004440DwarfWriter::DwarfWriter()
Bill Wendling163ba3f2009-03-10 21:23:25 +00004441 : ImmutablePass(&ID), DD(0), DE(0) {}
Jim Laskey65195462006-10-30 13:35:07 +00004442
4443DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00004444 delete DE;
4445 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00004446}
4447
Jim Laskey65195462006-10-30 13:35:07 +00004448/// BeginModule - Emit all Dwarf sections that should come prior to the
4449/// content.
Devang Pateleb3fc282009-01-08 23:40:34 +00004450void DwarfWriter::BeginModule(Module *M,
4451 MachineModuleInfo *MMI,
4452 raw_ostream &OS, AsmPrinter *A,
4453 const TargetAsmInfo *T) {
4454 DE = new DwarfException(OS, A, T);
4455 DD = new DwarfDebug(OS, A, T);
Jim Laskey072200c2007-01-29 18:51:14 +00004456 DE->BeginModule(M);
4457 DD->BeginModule(M);
Devang Patel7bb89ed2009-01-13 00:20:51 +00004458 DD->SetDebugInfo(MMI);
Devang Pateleb3fc282009-01-08 23:40:34 +00004459 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00004460}
4461
4462/// EndModule - Emit all Dwarf sections that should come after the content.
4463///
4464void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00004465 DE->EndModule();
4466 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00004467}
4468
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004469/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00004470/// emitted immediately after the function entry point.
4471void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00004472 DE->BeginFunction(MF);
4473 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00004474}
4475
4476/// EndFunction - Gather and emit post-function debug information.
4477///
Bill Wendlingd751c642008-09-26 00:28:12 +00004478void DwarfWriter::EndFunction(MachineFunction *MF) {
4479 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004480 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004481
Bill Wendlingc91d0b92008-07-22 00:53:37 +00004482 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00004483 // Clear function debug information.
4484 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00004485}
Devang Patelccca7fe2009-01-12 19:17:34 +00004486
Devang Patelcf3a4482009-01-15 23:41:32 +00004487/// ValidDebugInfo - Return true if V represents valid debug info value.
4488bool DwarfWriter::ValidDebugInfo(Value *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004489 return DD && DD->ValidDebugInfo(V);
Devang Patelcf3a4482009-01-15 23:41:32 +00004490}
4491
Devang Patelccca7fe2009-01-12 19:17:34 +00004492/// RecordSourceLine - Records location information and associates it with a
4493/// label. Returns a unique label ID used to generate a label and provide
4494/// correspondence to the source line list.
4495unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
4496 unsigned Src) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004497 return DD->RecordSourceLine(Line, Col, Src);
Devang Patelccca7fe2009-01-12 19:17:34 +00004498}
4499
Evan Chenge3d42322009-02-25 07:04:34 +00004500/// getOrCreateSourceID - Look up the source id with the given directory and
4501/// source file names. If none currently exists, create a new id and insert it
4502/// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
4503/// as well.
4504unsigned DwarfWriter::getOrCreateSourceID(const std::string &DirName,
4505 const std::string &FileName) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004506 return DD->getOrCreateSourceID(DirName, FileName);
Devang Patelccca7fe2009-01-12 19:17:34 +00004507}
4508
4509/// RecordRegionStart - Indicate the start of a region.
4510unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004511 return DD->RecordRegionStart(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004512}
4513
4514/// RecordRegionEnd - Indicate the end of a region.
4515unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004516 return DD->RecordRegionEnd(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004517}
4518
4519/// getRecordSourceLineCount - Count source lines.
4520unsigned DwarfWriter::getRecordSourceLineCount() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004521 return DD->getRecordSourceLineCount();
Devang Patelccca7fe2009-01-12 19:17:34 +00004522}
Devang Patelbb8c5952009-01-13 21:25:00 +00004523
Devang Patelc48c5502009-01-13 21:44:10 +00004524/// RecordVariable - Indicate the declaration of a local variable.
4525///
4526void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
4527 DD->RecordVariable(GV, FrameIndex);
4528}
Devang Patelbbdc8202009-01-13 23:54:55 +00004529
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004530/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
4531/// be emitted.
4532bool DwarfWriter::ShouldEmitDwarfDebug() const {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004533 return DD->ShouldEmitDwarfDebug();
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004534}