blob: fa230b5a822f9eae3f44f2672635b23a6c89b179 [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 Wendling91b8b802009-03-10 20:41:52 +000051namespace {
52
53static TimerGroup *DwarfTimerGroup = 0;
54static TimerGroup *getDwarfTimerGroup() {
55 if (DwarfTimerGroup) return DwarfTimerGroup;
56 return DwarfTimerGroup = new TimerGroup("Dwarf Exception and Debugging");
57}
58
59} // end anonymous namespace
60
Jim Laskey0d086af2006-02-27 12:43:29 +000061namespace llvm {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000062
Jim Laskey65195462006-10-30 13:35:07 +000063//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +000064
65/// Configuration values for initial hash set sizes (log2).
66///
Bill Wendlingb9dcef22009-02-03 21:17:20 +000067static const unsigned InitDiesSetSize = 9; // log2(512)
68static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
69static const unsigned InitValuesSetSize = 9; // log2(512)
Jim Laskeyef42a012006-11-02 20:12:39 +000070
71//===----------------------------------------------------------------------===//
72/// Forward declarations.
73///
74class DIE;
75class DIEValue;
76
77//===----------------------------------------------------------------------===//
Devang Pateld1ca9252009-01-05 23:03:32 +000078/// Utility routines.
79///
Devang Patelcf3a4482009-01-15 23:41:32 +000080/// getGlobalVariable - Return either a direct or cast Global value.
81///
82static GlobalVariable *getGlobalVariable(Value *V) {
83 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
84 return GV;
85 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
86 if (CE->getOpcode() == Instruction::BitCast) {
87 return dyn_cast<GlobalVariable>(CE->getOperand(0));
88 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
89 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
90 if (!CE->getOperand(i)->isNullValue())
91 return NULL;
92 }
93 return dyn_cast<GlobalVariable>(CE->getOperand(0));
94 }
95 }
96 return NULL;
97}
98
Devang Pateld1ca9252009-01-05 23:03:32 +000099//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000100/// DWLabel - Labels are used to track locations in the assembler file.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000101/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
102/// where the tag is a category of label (Ex. location) and number is a value
Reid Spencer181b6c92007-08-05 20:06:04 +0000103/// unique in that category.
Jim Laskey65195462006-10-30 13:35:07 +0000104class DWLabel {
105public:
Jim Laskeyef42a012006-11-02 20:12:39 +0000106 /// Tag - Label category tag. Should always be a staticly declared C string.
107 ///
108 const char *Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000109
Jim Laskeyef42a012006-11-02 20:12:39 +0000110 /// Number - Value to make label unique.
111 ///
112 unsigned Number;
Jim Laskey65195462006-10-30 13:35:07 +0000113
114 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000115
Jim Laskeyef42a012006-11-02 20:12:39 +0000116 void Profile(FoldingSetNodeID &ID) const {
Evan Chenge3d42322009-02-25 07:04:34 +0000117 ID.AddString(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +0000118 ID.AddInteger(Number);
Jim Laskey90c79d72006-03-23 23:02:34 +0000119 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000120
Jim Laskeyef42a012006-11-02 20:12:39 +0000121#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000122 void print(std::ostream *O) const {
123 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000124 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000125 void print(std::ostream &O) const {
Jim Laskeybacd3042007-02-21 22:48:45 +0000126 O << "." << Tag;
Jim Laskeyef42a012006-11-02 20:12:39 +0000127 if (Number) O << Number;
128 }
129#endif
Jim Laskeybd761842006-02-27 17:27:12 +0000130};
131
132//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000133/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
134/// Dwarf abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +0000135class DIEAbbrevData {
136private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000137 /// Attribute - Dwarf attribute code.
138 ///
139 unsigned Attribute;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000140
Jim Laskeyef42a012006-11-02 20:12:39 +0000141 /// Form - Dwarf form code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000142 ///
143 unsigned Form;
144
Jim Laskey0d086af2006-02-27 12:43:29 +0000145public:
146 DIEAbbrevData(unsigned A, unsigned F)
147 : Attribute(A)
148 , Form(F)
149 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000150
Jim Laskeybd761842006-02-27 17:27:12 +0000151 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000152 unsigned getAttribute() const { return Attribute; }
153 unsigned getForm() const { return Form; }
Jim Laskey063e7652006-01-17 17:31:53 +0000154
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000155 /// Profile - Used to gather unique data for the abbreviation folding set.
Jim Laskey0d086af2006-02-27 12:43:29 +0000156 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000157 void Profile(FoldingSetNodeID &ID)const {
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000158 ID.AddInteger(Attribute);
159 ID.AddInteger(Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000160 }
161};
Jim Laskey063e7652006-01-17 17:31:53 +0000162
Jim Laskey0d086af2006-02-27 12:43:29 +0000163//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000164/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
165/// information object.
166class DIEAbbrev : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000167private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000168 /// Tag - Dwarf tag code.
169 ///
170 unsigned Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000171
Jim Laskeyef42a012006-11-02 20:12:39 +0000172 /// Unique number for node.
173 ///
174 unsigned Number;
175
176 /// ChildrenFlag - Dwarf children flag.
177 ///
178 unsigned ChildrenFlag;
179
180 /// Data - Raw data bytes for abbreviation.
181 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000182 SmallVector<DIEAbbrevData, 8> Data;
Jim Laskey063e7652006-01-17 17:31:53 +0000183
Jim Laskey0d086af2006-02-27 12:43:29 +0000184public:
Jim Laskey063e7652006-01-17 17:31:53 +0000185
Jim Laskey0d086af2006-02-27 12:43:29 +0000186 DIEAbbrev(unsigned T, unsigned C)
Jim Laskeyef42a012006-11-02 20:12:39 +0000187 : Tag(T)
Jim Laskey0d086af2006-02-27 12:43:29 +0000188 , ChildrenFlag(C)
189 , Data()
190 {}
191 ~DIEAbbrev() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000192
Jim Laskeybd761842006-02-27 17:27:12 +0000193 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000194 unsigned getTag() const { return Tag; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000195 unsigned getNumber() const { return Number; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000196 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000197 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000198 void setTag(unsigned T) { Tag = T; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000199 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000200 void setNumber(unsigned N) { Number = N; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000201
Jim Laskey0d086af2006-02-27 12:43:29 +0000202 /// AddAttribute - Adds another set of attribute information to the
203 /// abbreviation.
204 void AddAttribute(unsigned Attribute, unsigned Form) {
205 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000206 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000207
Jim Laskeyb8509c52006-03-23 18:07:55 +0000208 /// AddFirstAttribute - Adds a set of attribute information to the front
209 /// of the abbreviation.
210 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
211 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
212 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000213
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000214 /// Profile - Used to gather unique data for the abbreviation folding set.
215 ///
216 void Profile(FoldingSetNodeID &ID) {
217 ID.AddInteger(Tag);
218 ID.AddInteger(ChildrenFlag);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000219
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000220 // For each attribute description.
221 for (unsigned i = 0, N = Data.size(); i < N; ++i)
222 Data[i].Profile(ID);
223 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000224
Jim Laskey0d086af2006-02-27 12:43:29 +0000225 /// Emit - Print the abbreviation using the specified Dwarf writer.
226 ///
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000227 void Emit(const DwarfDebug &DD) const;
228
Jim Laskey0d086af2006-02-27 12:43:29 +0000229#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000230 void print(std::ostream *O) {
231 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000232 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000233 void print(std::ostream &O);
234 void dump();
235#endif
236};
Jim Laskey063e7652006-01-17 17:31:53 +0000237
Jim Laskey0d086af2006-02-27 12:43:29 +0000238//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000239/// DIE - A structured debug information entry. Has an abbreviation which
240/// describes it's organization.
241class DIE : public FoldingSetNode {
242protected:
243 /// Abbrev - Buffer for constructing abbreviation.
244 ///
245 DIEAbbrev Abbrev;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000246
Jim Laskeyef42a012006-11-02 20:12:39 +0000247 /// Offset - Offset in debug info section.
248 ///
249 unsigned Offset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000250
Jim Laskeyef42a012006-11-02 20:12:39 +0000251 /// Size - Size of instance + children.
252 ///
253 unsigned Size;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000254
Jim Laskeyef42a012006-11-02 20:12:39 +0000255 /// Children DIEs.
256 ///
257 std::vector<DIE *> Children;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000258
Jim Laskeyef42a012006-11-02 20:12:39 +0000259 /// Attributes values.
260 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000261 SmallVector<DIEValue*, 32> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000262
Jim Laskeyef42a012006-11-02 20:12:39 +0000263public:
Dan Gohman81975f62007-08-27 14:50:10 +0000264 explicit DIE(unsigned Tag)
Jim Laskeyef42a012006-11-02 20:12:39 +0000265 : Abbrev(Tag, DW_CHILDREN_no)
266 , Offset(0)
267 , Size(0)
268 , Children()
269 , Values()
270 {}
271 virtual ~DIE();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000272
Jim Laskeyef42a012006-11-02 20:12:39 +0000273 // Accessors.
274 DIEAbbrev &getAbbrev() { return Abbrev; }
275 unsigned getAbbrevNumber() const {
276 return Abbrev.getNumber();
277 }
Jim Laskey85f419b2006-11-09 16:32:26 +0000278 unsigned getTag() const { return Abbrev.getTag(); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000279 unsigned getOffset() const { return Offset; }
280 unsigned getSize() const { return Size; }
281 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000282 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000283 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
284 void setOffset(unsigned O) { Offset = O; }
285 void setSize(unsigned S) { Size = S; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000286
Jim Laskeyef42a012006-11-02 20:12:39 +0000287 /// AddValue - Add a value and attributes to a DIE.
288 ///
289 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
290 Abbrev.AddAttribute(Attribute, Form);
291 Values.push_back(Value);
292 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000293
Jim Laskeyef42a012006-11-02 20:12:39 +0000294 /// SiblingOffset - Return the offset of the debug information entry's
295 /// sibling.
296 unsigned SiblingOffset() const { return Offset + Size; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000297
Jim Laskeyef42a012006-11-02 20:12:39 +0000298 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
299 ///
300 void AddSiblingOffset();
301
302 /// AddChild - Add a child to the DIE.
303 ///
304 void AddChild(DIE *Child) {
305 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
306 Children.push_back(Child);
307 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000308
Jim Laskeyef42a012006-11-02 20:12:39 +0000309 /// Detach - Detaches objects connected to it after copying.
310 ///
311 void Detach() {
312 Children.clear();
313 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000314
Jim Laskeyef42a012006-11-02 20:12:39 +0000315 /// Profile - Used to gather unique data for the value folding set.
316 ///
317 void Profile(FoldingSetNodeID &ID) ;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000318
Jim Laskeyef42a012006-11-02 20:12:39 +0000319#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000320 void print(std::ostream *O, unsigned IncIndent = 0) {
321 if (O) print(*O, IncIndent);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000322 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000323 void print(std::ostream &O, unsigned IncIndent = 0);
324 void dump();
325#endif
326};
327
328//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000329/// DIEValue - A debug information entry value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000330///
331class DIEValue : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000332public:
333 enum {
334 isInteger,
335 isString,
336 isLabel,
337 isAsIsLabel,
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000338 isSectionOffset,
Jim Laskey0d086af2006-02-27 12:43:29 +0000339 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000340 isEntry,
341 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000342 };
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000343
Jim Laskeyef42a012006-11-02 20:12:39 +0000344 /// Type - Type of data stored in the value.
345 ///
346 unsigned Type;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000347
Dan Gohman81975f62007-08-27 14:50:10 +0000348 explicit DIEValue(unsigned T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000349 : Type(T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000350 {}
Jim Laskey0d086af2006-02-27 12:43:29 +0000351 virtual ~DIEValue() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000352
Jim Laskeyf6733882006-11-02 21:48:18 +0000353 // Accessors
Jim Laskeyef42a012006-11-02 20:12:39 +0000354 unsigned getType() const { return Type; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000355
Jim Laskey0d086af2006-02-27 12:43:29 +0000356 // Implement isa/cast/dyncast.
357 static bool classof(const DIEValue *) { return true; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000358
Jim Laskey0d086af2006-02-27 12:43:29 +0000359 /// EmitValue - Emit value via the Dwarf writer.
360 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000361 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000362
Jim Laskey0d086af2006-02-27 12:43:29 +0000363 /// SizeOf - Return the size of a value in bytes.
364 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000365 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000366
Jim Laskeyef42a012006-11-02 20:12:39 +0000367 /// Profile - Used to gather unique data for the value folding set.
368 ///
369 virtual void Profile(FoldingSetNodeID &ID) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000370
Jim Laskeyef42a012006-11-02 20:12:39 +0000371#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000372 void print(std::ostream *O) {
373 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000374 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000375 virtual void print(std::ostream &O) = 0;
376 void dump();
377#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000378};
Jim Laskey063e7652006-01-17 17:31:53 +0000379
Jim Laskey0d086af2006-02-27 12:43:29 +0000380//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000381/// DWInteger - An integer value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000382///
Jim Laskey0d086af2006-02-27 12:43:29 +0000383class DIEInteger : public DIEValue {
384private:
385 uint64_t Integer;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000386
Jim Laskey0d086af2006-02-27 12:43:29 +0000387public:
Dan Gohman81975f62007-08-27 14:50:10 +0000388 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000389
Jim Laskey0d086af2006-02-27 12:43:29 +0000390 // Implement isa/cast/dyncast.
391 static bool classof(const DIEInteger *) { return true; }
392 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000393
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000394 /// BestForm - Choose the best form for integer.
395 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000396 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
397 if (IsSigned) {
398 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
399 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
400 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
401 } else {
402 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
403 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
404 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
405 }
406 return DW_FORM_data8;
407 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000408
Jim Laskey0d086af2006-02-27 12:43:29 +0000409 /// EmitValue - Emit integer of appropriate size.
410 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000411 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000412
Jim Laskey0d086af2006-02-27 12:43:29 +0000413 /// SizeOf - Determine size of integer value in bytes.
414 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000415 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000416
Jim Laskeyef42a012006-11-02 20:12:39 +0000417 /// Profile - Used to gather unique data for the value folding set.
418 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000419 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000420 ID.AddInteger(isInteger);
Jim Laskeyef42a012006-11-02 20:12:39 +0000421 ID.AddInteger(Integer);
422 }
Jim Laskey5496f012006-11-09 14:52:14 +0000423 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000424
Jim Laskeyef42a012006-11-02 20:12:39 +0000425#ifndef NDEBUG
426 virtual void print(std::ostream &O) {
427 O << "Int: " << (int64_t)Integer
428 << " 0x" << std::hex << Integer << std::dec;
429 }
430#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000431};
Jim Laskey063e7652006-01-17 17:31:53 +0000432
Jim Laskey0d086af2006-02-27 12:43:29 +0000433//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000434/// DIEString - A string value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000435///
Jim Laskeyef42a012006-11-02 20:12:39 +0000436class DIEString : public DIEValue {
437public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000438 const std::string String;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000439
Dan Gohman81975f62007-08-27 14:50:10 +0000440 explicit DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000441
Jim Laskey0d086af2006-02-27 12:43:29 +0000442 // Implement isa/cast/dyncast.
443 static bool classof(const DIEString *) { return true; }
444 static bool classof(const DIEValue *S) { return S->Type == isString; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000445
Jim Laskey0d086af2006-02-27 12:43:29 +0000446 /// EmitValue - Emit string value.
447 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000448 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000449
Jim Laskey0d086af2006-02-27 12:43:29 +0000450 /// SizeOf - Determine size of string value in bytes.
451 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000452 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000453 return String.size() + sizeof(char); // sizeof('\0');
454 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000455
Jim Laskeyef42a012006-11-02 20:12:39 +0000456 /// Profile - Used to gather unique data for the value folding set.
457 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000458 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000459 ID.AddInteger(isString);
Jim Laskeyef42a012006-11-02 20:12:39 +0000460 ID.AddString(String);
461 }
Jim Laskey5496f012006-11-09 14:52:14 +0000462 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000463
Jim Laskeyef42a012006-11-02 20:12:39 +0000464#ifndef NDEBUG
465 virtual void print(std::ostream &O) {
466 O << "Str: \"" << String << "\"";
467 }
468#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000469};
Jim Laskey063e7652006-01-17 17:31:53 +0000470
Jim Laskey0d086af2006-02-27 12:43:29 +0000471//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000472/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000473//
Jim Laskeyef42a012006-11-02 20:12:39 +0000474class DIEDwarfLabel : public DIEValue {
475public:
476
Jim Laskey0d086af2006-02-27 12:43:29 +0000477 const DWLabel Label;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000478
Dan Gohman81975f62007-08-27 14:50:10 +0000479 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000480
Jim Laskey0d086af2006-02-27 12:43:29 +0000481 // Implement isa/cast/dyncast.
482 static bool classof(const DIEDwarfLabel *) { return true; }
483 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000484
Jim Laskey0d086af2006-02-27 12:43:29 +0000485 /// EmitValue - Emit label value.
486 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000487 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000488
Jim Laskey0d086af2006-02-27 12:43:29 +0000489 /// SizeOf - Determine size of label value in bytes.
490 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000491 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000492
Jim Laskeyef42a012006-11-02 20:12:39 +0000493 /// Profile - Used to gather unique data for the value folding set.
494 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000495 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000496 ID.AddInteger(isLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000497 Label.Profile(ID);
498 }
Jim Laskey5496f012006-11-09 14:52:14 +0000499 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000500
Jim Laskeyef42a012006-11-02 20:12:39 +0000501#ifndef NDEBUG
502 virtual void print(std::ostream &O) {
503 O << "Lbl: ";
504 Label.print(O);
505 }
506#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000507};
Jim Laskey063e7652006-01-17 17:31:53 +0000508
Jim Laskey063e7652006-01-17 17:31:53 +0000509
Jim Laskey0d086af2006-02-27 12:43:29 +0000510//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000511/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000512//
Jim Laskeyef42a012006-11-02 20:12:39 +0000513class DIEObjectLabel : public DIEValue {
514public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000515 const std::string Label;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000516
Dan Gohman81975f62007-08-27 14:50:10 +0000517 explicit DIEObjectLabel(const std::string &L)
518 : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000519
Jim Laskey0d086af2006-02-27 12:43:29 +0000520 // Implement isa/cast/dyncast.
521 static bool classof(const DIEObjectLabel *) { return true; }
522 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000523
Jim Laskey0d086af2006-02-27 12:43:29 +0000524 /// EmitValue - Emit label value.
525 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000526 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000527
Jim Laskey0d086af2006-02-27 12:43:29 +0000528 /// SizeOf - Determine size of label value in bytes.
529 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000530 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000531
Jim Laskeyef42a012006-11-02 20:12:39 +0000532 /// Profile - Used to gather unique data for the value folding set.
533 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000534 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000535 ID.AddInteger(isAsIsLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000536 ID.AddString(Label);
537 }
Evan Chenge3d42322009-02-25 07:04:34 +0000538 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label.c_str()); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000539
540#ifndef NDEBUG
541 virtual void print(std::ostream &O) {
542 O << "Obj: " << Label;
543 }
544#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000545};
Jim Laskey063e7652006-01-17 17:31:53 +0000546
Jim Laskey0d086af2006-02-27 12:43:29 +0000547//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000548/// DIESectionOffset - A section offset DIE.
549//
550class DIESectionOffset : public DIEValue {
551public:
552 const DWLabel Label;
553 const DWLabel Section;
554 bool IsEH : 1;
555 bool UseSet : 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000556
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000557 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
558 bool isEH = false, bool useSet = true)
559 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
560 IsEH(isEH), UseSet(useSet) {}
561
562 // Implement isa/cast/dyncast.
563 static bool classof(const DIESectionOffset *) { return true; }
564 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000565
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000566 /// EmitValue - Emit section offset.
567 ///
568 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000569
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000570 /// SizeOf - Determine size of section offset value in bytes.
571 ///
572 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000573
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000574 /// Profile - Used to gather unique data for the value folding set.
575 ///
576 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
577 const DWLabel &Section) {
578 ID.AddInteger(isSectionOffset);
579 Label.Profile(ID);
580 Section.Profile(ID);
581 // IsEH and UseSet are specific to the Label/Section that we will emit
582 // the offset for; so Label/Section are enough for uniqueness.
583 }
584 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
585
586#ifndef NDEBUG
587 virtual void print(std::ostream &O) {
588 O << "Off: ";
589 Label.print(O);
590 O << "-";
591 Section.print(O);
592 O << "-" << IsEH << "-" << UseSet;
593 }
594#endif
595};
596
597//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000598/// DIEDelta - A simple label difference DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000599///
Jim Laskeyef42a012006-11-02 20:12:39 +0000600class DIEDelta : public DIEValue {
601public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000602 const DWLabel LabelHi;
603 const DWLabel LabelLo;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000604
Jim Laskey0d086af2006-02-27 12:43:29 +0000605 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
606 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000607
Jim Laskey0d086af2006-02-27 12:43:29 +0000608 // Implement isa/cast/dyncast.
609 static bool classof(const DIEDelta *) { return true; }
610 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000611
Jim Laskey0d086af2006-02-27 12:43:29 +0000612 /// EmitValue - Emit delta value.
613 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000614 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000615
Jim Laskey0d086af2006-02-27 12:43:29 +0000616 /// SizeOf - Determine size of delta value in bytes.
617 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000618 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000619
Jim Laskeyef42a012006-11-02 20:12:39 +0000620 /// Profile - Used to gather unique data for the value folding set.
621 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000622 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
623 const DWLabel &LabelLo) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000624 ID.AddInteger(isDelta);
Jim Laskeyef42a012006-11-02 20:12:39 +0000625 LabelHi.Profile(ID);
626 LabelLo.Profile(ID);
627 }
Jim Laskey5496f012006-11-09 14:52:14 +0000628 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000629
630#ifndef NDEBUG
631 virtual void print(std::ostream &O) {
632 O << "Del: ";
633 LabelHi.print(O);
634 O << "-";
635 LabelLo.print(O);
636 }
637#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000638};
Jim Laskey063e7652006-01-17 17:31:53 +0000639
Jim Laskey0d086af2006-02-27 12:43:29 +0000640//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000641/// DIEntry - A pointer to another debug information entry. An instance of this
642/// class can also be used as a proxy for a debug information entry not yet
643/// defined (ie. types.)
644class DIEntry : public DIEValue {
645public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000646 DIE *Entry;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000647
Dan Gohman81975f62007-08-27 14:50:10 +0000648 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000649
Jim Laskey0d086af2006-02-27 12:43:29 +0000650 // Implement isa/cast/dyncast.
651 static bool classof(const DIEntry *) { return true; }
652 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000653
Jim Laskeyb8509c52006-03-23 18:07:55 +0000654 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000655 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000656 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000657
Jim Laskeyb8509c52006-03-23 18:07:55 +0000658 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000659 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000660 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000661 return sizeof(int32_t);
662 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000663
Jim Laskeyef42a012006-11-02 20:12:39 +0000664 /// Profile - Used to gather unique data for the value folding set.
665 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000666 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
667 ID.AddInteger(isEntry);
668 ID.AddPointer(Entry);
669 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000670 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000671 ID.AddInteger(isEntry);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000672
Jim Laskeyef42a012006-11-02 20:12:39 +0000673 if (Entry) {
674 ID.AddPointer(Entry);
675 } else {
676 ID.AddPointer(this);
677 }
678 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000679
Jim Laskeyef42a012006-11-02 20:12:39 +0000680#ifndef NDEBUG
681 virtual void print(std::ostream &O) {
682 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
683 }
684#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000685};
686
687//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000688/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000689//
Jim Laskeyef42a012006-11-02 20:12:39 +0000690class DIEBlock : public DIEValue, public DIE {
691public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000692 unsigned Size; // Size in bytes excluding size header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000693
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000694 DIEBlock()
695 : DIEValue(isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +0000696 , DIE(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000697 , Size(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000698 {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000699 ~DIEBlock() {
700 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000701
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000702 // Implement isa/cast/dyncast.
703 static bool classof(const DIEBlock *) { return true; }
704 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000705
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000706 /// ComputeSize - calculate the size of the block.
707 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000708 unsigned ComputeSize(DwarfDebug &DD);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000709
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000710 /// BestForm - Choose the best form for data.
711 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000712 unsigned BestForm() const {
713 if ((unsigned char)Size == Size) return DW_FORM_block1;
714 if ((unsigned short)Size == Size) return DW_FORM_block2;
715 if ((unsigned int)Size == Size) return DW_FORM_block4;
716 return DW_FORM_block;
717 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000718
719 /// EmitValue - Emit block data.
720 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000721 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000722
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000723 /// SizeOf - Determine size of block data in bytes.
724 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000725 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000726
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000727
Jim Laskeyef42a012006-11-02 20:12:39 +0000728 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000729 ///
Reid Spencer97821312006-11-02 23:56:21 +0000730 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000731 ID.AddInteger(isBlock);
Jim Laskeyef42a012006-11-02 20:12:39 +0000732 DIE::Profile(ID);
733 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000734
Jim Laskeyef42a012006-11-02 20:12:39 +0000735#ifndef NDEBUG
736 virtual void print(std::ostream &O) {
737 O << "Blk: ";
738 DIE::print(O, 5);
739 }
740#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000741};
742
743//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000744/// CompileUnit - This dwarf writer support class manages information associate
745/// with a source file.
746class CompileUnit {
Jim Laskey0d086af2006-02-27 12:43:29 +0000747private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000748 /// ID - File identifier for source.
749 ///
750 unsigned ID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000751
Jim Laskeyef42a012006-11-02 20:12:39 +0000752 /// Die - Compile unit debug information entry.
753 ///
754 DIE *Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000755
Devang Patelbbdc8202009-01-13 23:54:55 +0000756 /// GVToDieMap - Tracks the mapping of unit level debug informaton
757 /// variables to debug information entries.
Devang Patelc2997f42009-01-20 00:58:55 +0000758 std::map<GlobalVariable *, DIE *> GVToDieMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000759
Devang Patelbbdc8202009-01-13 23:54:55 +0000760 /// GVToDIEntryMap - Tracks the mapping of unit level debug informaton
Jim Laskeyef42a012006-11-02 20:12:39 +0000761 /// descriptors to debug information entries using a DIEntry proxy.
Devang Patelc2997f42009-01-20 00:58:55 +0000762 std::map<GlobalVariable *, DIEntry *> GVToDIEntryMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000763
764 /// Globals - A map of globally visible named entities for this unit.
765 ///
766 std::map<std::string, DIE *> Globals;
767
768 /// DiesSet - Used to uniquely define dies within the compile unit.
769 ///
770 FoldingSet<DIE> DiesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000771
Jim Laskey0d086af2006-02-27 12:43:29 +0000772public:
Devang Pateld1ca9252009-01-05 23:03:32 +0000773 CompileUnit(unsigned I, DIE *D)
Devang Patelbbdc8202009-01-13 23:54:55 +0000774 : ID(I), Die(D), GVToDieMap(),
Devang Patel7e55b3a2009-01-17 06:51:37 +0000775 GVToDIEntryMap(), Globals(), DiesSet(InitDiesSetSize)
Devang Pateld1ca9252009-01-05 23:03:32 +0000776 {}
777
Jim Laskeyef42a012006-11-02 20:12:39 +0000778 ~CompileUnit() {
779 delete Die;
Jim Laskeyef42a012006-11-02 20:12:39 +0000780 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000781
Jim Laskeybd761842006-02-27 17:27:12 +0000782 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000783 unsigned getID() const { return ID; }
784 DIE* getDie() const { return Die; }
785 std::map<std::string, DIE *> &getGlobals() { return Globals; }
786
787 /// hasContent - Return true if this compile unit has something to write out.
788 ///
789 bool hasContent() const {
790 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000791 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000792
793 /// AddGlobal - Add a new global entity to the compile unit.
794 ///
795 void AddGlobal(const std::string &Name, DIE *Die) {
796 Globals[Name] = Die;
797 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000798
Jim Laskeyef42a012006-11-02 20:12:39 +0000799 /// getDieMapSlotFor - Returns the debug information entry map slot for the
Devang Patelbbdc8202009-01-13 23:54:55 +0000800 /// specified debug variable.
Devang Patel48d190f2009-01-05 21:47:57 +0000801 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
802 return GVToDieMap[GV];
803 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000804
Jim Laskeyef42a012006-11-02 20:12:39 +0000805 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
Devang Patelbbdc8202009-01-13 23:54:55 +0000806 /// specified debug variable.
Devang Patel48d190f2009-01-05 21:47:57 +0000807 DIEntry *&getDIEntrySlotFor(GlobalVariable *GV) {
808 return GVToDIEntryMap[GV];
809 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000810
Jim Laskeyef42a012006-11-02 20:12:39 +0000811 /// AddDie - Adds or interns the DIE to the compile unit.
812 ///
813 DIE *AddDie(DIE &Buffer) {
814 FoldingSetNodeID ID;
815 Buffer.Profile(ID);
816 void *Where;
817 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000818
Jim Laskeyef42a012006-11-02 20:12:39 +0000819 if (!Die) {
820 Die = new DIE(Buffer);
821 DiesSet.InsertNode(Die, Where);
822 this->Die->AddChild(Die);
823 Buffer.Detach();
824 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000825
Jim Laskeyef42a012006-11-02 20:12:39 +0000826 return Die;
827 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000828};
829
Jim Laskey65195462006-10-30 13:35:07 +0000830//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000831/// Dwarf - Emits general Dwarf directives.
Jim Laskeyef42a012006-11-02 20:12:39 +0000832///
Jim Laskey65195462006-10-30 13:35:07 +0000833class Dwarf {
Jim Laskey072200c2007-01-29 18:51:14 +0000834protected:
Jim Laskey65195462006-10-30 13:35:07 +0000835 //===--------------------------------------------------------------------===//
Jim Laskey072200c2007-01-29 18:51:14 +0000836 // Core attributes used by the Dwarf writer.
Jim Laskey65195462006-10-30 13:35:07 +0000837 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000838
Jim Laskey65195462006-10-30 13:35:07 +0000839 //
840 /// O - Stream to .s file.
841 ///
Owen Andersoncb371882008-08-21 00:14:44 +0000842 raw_ostream &O;
Jim Laskey65195462006-10-30 13:35:07 +0000843
844 /// Asm - Target of Dwarf emission.
845 ///
846 AsmPrinter *Asm;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000847
Bill Wendlingaa8f8882008-07-01 23:34:48 +0000848 /// TAI - Target asm information.
Jim Laskey65195462006-10-30 13:35:07 +0000849 const TargetAsmInfo *TAI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000850
Jim Laskey65195462006-10-30 13:35:07 +0000851 /// TD - Target data.
852 const TargetData *TD;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000853
Jim Laskey65195462006-10-30 13:35:07 +0000854 /// RI - Register Information.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000855 const TargetRegisterInfo *RI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000856
Jim Laskey65195462006-10-30 13:35:07 +0000857 /// M - Current module.
858 ///
859 Module *M;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000860
Jim Laskey65195462006-10-30 13:35:07 +0000861 /// MF - Current machine function.
862 ///
863 MachineFunction *MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000864
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000865 /// MMI - Collected machine module information.
Jim Laskey65195462006-10-30 13:35:07 +0000866 ///
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000867 MachineModuleInfo *MMI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000868
Jim Laskey65195462006-10-30 13:35:07 +0000869 /// SubprogramCount - The running count of functions being compiled.
870 ///
871 unsigned SubprogramCount;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000872
Chris Lattner9251f132007-09-24 03:35:37 +0000873 /// Flavor - A unique string indicating what dwarf producer this is, used to
874 /// unique labels.
875 const char * const Flavor;
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000876
877 unsigned SetCounter;
Owen Andersoncb371882008-08-21 00:14:44 +0000878 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattner9251f132007-09-24 03:35:37 +0000879 const char *flavor)
Jim Laskey072200c2007-01-29 18:51:14 +0000880 : O(OS)
881 , Asm(A)
882 , TAI(T)
883 , TD(Asm->TM.getTargetData())
884 , RI(Asm->TM.getRegisterInfo())
885 , M(NULL)
886 , MF(NULL)
887 , MMI(NULL)
Jim Laskey072200c2007-01-29 18:51:14 +0000888 , SubprogramCount(0)
Chris Lattner9251f132007-09-24 03:35:37 +0000889 , Flavor(flavor)
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000890 , SetCounter(1)
Jim Laskey072200c2007-01-29 18:51:14 +0000891 {
892 }
893
894public:
Jim Laskey072200c2007-01-29 18:51:14 +0000895 //===--------------------------------------------------------------------===//
896 // Accessors.
897 //
898 AsmPrinter *getAsm() const { return Asm; }
Jim Laskeyb82313f2007-02-01 16:31:34 +0000899 MachineModuleInfo *getMMI() const { return MMI; }
Jim Laskey072200c2007-01-29 18:51:14 +0000900 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohman82482942007-09-27 23:12:31 +0000901 const TargetData *getTargetData() const { return TD; }
Jim Laskey072200c2007-01-29 18:51:14 +0000902
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000903 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
904 const {
905 if (isInSection && TAI->getDwarfSectionOffsetDirective())
906 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohman82482942007-09-27 23:12:31 +0000907 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000908 O << TAI->getData32bitsDirective();
909 else
910 O << TAI->getData64bitsDirective();
911 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000912
Jim Laskeyb82313f2007-02-01 16:31:34 +0000913 /// PrintLabelName - Print label name in form used by Dwarf writer.
914 ///
915 void PrintLabelName(DWLabel Label) const {
916 PrintLabelName(Label.Tag, Label.Number);
917 }
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000918 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000919 O << TAI->getPrivateGlobalPrefix() << Tag;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000920 if (Number) O << Number;
921 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000922
Chris Lattner9251f132007-09-24 03:35:37 +0000923 void PrintLabelName(const char *Tag, unsigned Number,
924 const char *Suffix) const {
925 O << TAI->getPrivateGlobalPrefix() << Tag;
926 if (Number) O << Number;
927 O << Suffix;
928 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000929
Jim Laskeyb82313f2007-02-01 16:31:34 +0000930 /// EmitLabel - Emit location label for internal use by Dwarf.
931 ///
932 void EmitLabel(DWLabel Label) const {
933 EmitLabel(Label.Tag, Label.Number);
934 }
935 void EmitLabel(const char *Tag, unsigned Number) const {
936 PrintLabelName(Tag, Number);
937 O << ":\n";
938 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000939
Jim Laskeyb82313f2007-02-01 16:31:34 +0000940 /// EmitReference - Emit a reference to a label.
941 ///
Dan Gohman06ff4e62007-09-28 15:43:33 +0000942 void EmitReference(DWLabel Label, bool IsPCRelative = false,
943 bool Force32Bit = false) const {
944 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000945 }
946 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman06ff4e62007-09-28 15:43:33 +0000947 bool IsPCRelative = false, bool Force32Bit = false) const {
948 PrintRelDirective(Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000949 PrintLabelName(Tag, Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000950
Jim Laskeyb82313f2007-02-01 16:31:34 +0000951 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
952 }
Dan Gohman06ff4e62007-09-28 15:43:33 +0000953 void EmitReference(const std::string &Name, bool IsPCRelative = false,
954 bool Force32Bit = false) const {
955 PrintRelDirective(Force32Bit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000956
Jim Laskeyb82313f2007-02-01 16:31:34 +0000957 O << Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000958
Jim Laskeyb82313f2007-02-01 16:31:34 +0000959 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
960 }
961
962 /// EmitDifference - Emit the difference between two labels. Some
963 /// assemblers do not behave with absolute expressions with data directives,
964 /// so there is an option (needsSet) to use an intermediary set expression.
965 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000966 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000967 EmitDifference(LabelHi.Tag, LabelHi.Number,
968 LabelLo.Tag, LabelLo.Number,
969 IsSmall);
970 }
971 void EmitDifference(const char *TagHi, unsigned NumberHi,
972 const char *TagLo, unsigned NumberLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000973 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000974 if (TAI->needsSet()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000975 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000976 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000977 O << ",";
978 PrintLabelName(TagHi, NumberHi);
979 O << "-";
980 PrintLabelName(TagLo, NumberLo);
981 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000982
983 PrintRelDirective(IsSmall);
Chris Lattner9251f132007-09-24 03:35:37 +0000984 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000985 ++SetCounter;
986 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000987 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000988
Jim Laskeyb82313f2007-02-01 16:31:34 +0000989 PrintLabelName(TagHi, NumberHi);
990 O << "-";
991 PrintLabelName(TagLo, NumberLo);
992 }
993 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000994
995 void EmitSectionOffset(const char* Label, const char* Section,
996 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000997 bool IsSmall = false, bool isEH = false,
998 bool useSet = true) {
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000999 bool printAbsolute = false;
Dale Johannesend9ffd4c2008-03-26 23:31:39 +00001000 if (isEH)
1001 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
1002 else
1003 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
1004
1005 if (TAI->needsSet() && useSet) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001006 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +00001007 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001008 O << ",";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001009 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001010
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001011 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001012 O << "-";
1013 PrintLabelName(Section, SectionNumber);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001014 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001015 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001016
1017 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001018
Chris Lattner9251f132007-09-24 03:35:37 +00001019 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001020 ++SetCounter;
1021 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001022 PrintRelDirective(IsSmall, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001023
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001024 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001025
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001026 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001027 O << "-";
1028 PrintLabelName(Section, SectionNumber);
1029 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001030 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001031 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001032
Jim Laskeyb82313f2007-02-01 16:31:34 +00001033 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1034 /// frame.
1035 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenb97aec62007-11-13 19:13:01 +00001036 const std::vector<MachineMove> &Moves, bool isEH) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001037 int stackGrowth =
1038 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1039 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00001040 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeybacd3042007-02-21 22:48:45 +00001041 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
Jim Laskeyb82313f2007-02-01 16:31:34 +00001042
1043 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001044 const MachineMove &Move = Moves[i];
Jim Laskeyb82313f2007-02-01 16:31:34 +00001045 unsigned LabelID = Move.getLabelID();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001046
Jim Laskeyb82313f2007-02-01 16:31:34 +00001047 if (LabelID) {
1048 LabelID = MMI->MappedLabel(LabelID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001049
Jim Laskeyb82313f2007-02-01 16:31:34 +00001050 // Throw out move if the label is invalid.
1051 if (!LabelID) continue;
1052 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001053
Jim Laskeyb82313f2007-02-01 16:31:34 +00001054 const MachineLocation &Dst = Move.getDestination();
1055 const MachineLocation &Src = Move.getSource();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001056
Jim Laskeyb82313f2007-02-01 16:31:34 +00001057 // Advance row if new location.
1058 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1059 Asm->EmitInt8(DW_CFA_advance_loc4);
1060 Asm->EOL("DW_CFA_advance_loc4");
Jim Laskeybacd3042007-02-21 22:48:45 +00001061 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1062 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001063
Jim Laskeyb82313f2007-02-01 16:31:34 +00001064 BaseLabelID = LabelID;
Jim Laskeybacd3042007-02-21 22:48:45 +00001065 BaseLabel = "label";
Jim Laskeyb82313f2007-02-01 16:31:34 +00001066 IsLocal = true;
1067 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001068
Jim Laskeyb82313f2007-02-01 16:31:34 +00001069 // If advancing cfa.
Dan Gohmand735b802008-10-03 15:45:36 +00001070 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1071 if (!Src.isReg()) {
1072 if (Src.getReg() == MachineLocation::VirtualFP) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001073 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1074 Asm->EOL("DW_CFA_def_cfa_offset");
1075 } else {
1076 Asm->EmitInt8(DW_CFA_def_cfa);
1077 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmand735b802008-10-03 15:45:36 +00001078 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001079 Asm->EOL("Register");
1080 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001081
Jim Laskeyb82313f2007-02-01 16:31:34 +00001082 int Offset = -Src.getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001083
Jim Laskeyb82313f2007-02-01 16:31:34 +00001084 Asm->EmitULEB128Bytes(Offset);
1085 Asm->EOL("Offset");
1086 } else {
1087 assert(0 && "Machine move no supported yet.");
1088 }
Dan Gohmand735b802008-10-03 15:45:36 +00001089 } else if (Src.isReg() &&
1090 Src.getReg() == MachineLocation::VirtualFP) {
1091 if (Dst.isReg()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001092 Asm->EmitInt8(DW_CFA_def_cfa_register);
1093 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmand735b802008-10-03 15:45:36 +00001094 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001095 Asm->EOL("Register");
1096 } else {
1097 assert(0 && "Machine move no supported yet.");
1098 }
1099 } else {
Dan Gohmand735b802008-10-03 15:45:36 +00001100 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001101 int Offset = Dst.getOffset() / stackGrowth;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001102
Jim Laskeyb82313f2007-02-01 16:31:34 +00001103 if (Offset < 0) {
1104 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1105 Asm->EOL("DW_CFA_offset_extended_sf");
1106 Asm->EmitULEB128Bytes(Reg);
1107 Asm->EOL("Reg");
1108 Asm->EmitSLEB128Bytes(Offset);
1109 Asm->EOL("Offset");
1110 } else if (Reg < 64) {
1111 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Chengd4114192008-07-09 21:53:02 +00001112 if (VerboseAsm)
1113 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1114 else
1115 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00001116 Asm->EmitULEB128Bytes(Offset);
1117 Asm->EOL("Offset");
1118 } else {
1119 Asm->EmitInt8(DW_CFA_offset_extended);
1120 Asm->EOL("DW_CFA_offset_extended");
1121 Asm->EmitULEB128Bytes(Reg);
1122 Asm->EOL("Reg");
1123 Asm->EmitULEB128Bytes(Offset);
1124 Asm->EOL("Offset");
1125 }
1126 }
1127 }
1128 }
1129
Jim Laskey072200c2007-01-29 18:51:14 +00001130};
1131
1132//===----------------------------------------------------------------------===//
Devang Patelf3ee5142009-01-12 22:54:42 +00001133/// SrcLineInfo - This class is used to record source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001134///
1135class SrcLineInfo {
1136 unsigned Line; // Source line number.
1137 unsigned Column; // Source column.
1138 unsigned SourceID; // Source ID number.
1139 unsigned LabelID; // Label in code ID number.
1140public:
1141 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001142 : Line(L), Column(C), SourceID(S), LabelID(I) {}
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001143
1144 // Accessors
1145 unsigned getLine() const { return Line; }
1146 unsigned getColumn() const { return Column; }
1147 unsigned getSourceID() const { return SourceID; }
1148 unsigned getLabelID() const { return LabelID; }
1149};
1150
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001151//===----------------------------------------------------------------------===//
Devang Patel7a6e5a32009-01-08 02:33:41 +00001152/// DbgVariable - This class is used to track local variable information.
1153///
1154class DbgVariable {
Devang Patel99ec3532009-01-16 19:28:14 +00001155 DIVariable Var; // Variable Descriptor.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001156 unsigned FrameIndex; // Variable frame index.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001157public:
Devang Patel99ec3532009-01-16 19:28:14 +00001158 DbgVariable(DIVariable V, unsigned I) : Var(V), FrameIndex(I) {}
Devang Patel7a6e5a32009-01-08 02:33:41 +00001159
1160 // Accessors.
Devang Patel99ec3532009-01-16 19:28:14 +00001161 DIVariable getVariable() const { return Var; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001162 unsigned getFrameIndex() const { return FrameIndex; }
1163};
1164
1165//===----------------------------------------------------------------------===//
1166/// DbgScope - This class is used to track scope information.
1167///
1168class DbgScope {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001169 DbgScope *Parent; // Parent to this scope.
Devang Patel7103c6a2009-01-16 18:01:58 +00001170 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001171 // Either subprogram or block.
1172 unsigned StartLabelID; // Label ID of the beginning of scope.
1173 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel7103c6a2009-01-16 18:01:58 +00001174 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
Devang Patel9795da52009-01-10 02:42:49 +00001175 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001176public:
Devang Patel0e5200f2009-01-15 18:25:17 +00001177 DbgScope(DbgScope *P, DIDescriptor D)
Devang Patel7a6e5a32009-01-08 02:33:41 +00001178 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), Scopes(), Variables()
1179 {}
Devang Patel481ff5b2009-01-12 18:48:36 +00001180 ~DbgScope() {
1181 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1182 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1183 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001184
1185 // Accessors.
Devang Patel7103c6a2009-01-16 18:01:58 +00001186 DbgScope *getParent() const { return Parent; }
1187 DIDescriptor getDesc() const { return Desc; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001188 unsigned getStartLabelID() const { return StartLabelID; }
1189 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel9795da52009-01-10 02:42:49 +00001190 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1191 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001192 void setStartLabelID(unsigned S) { StartLabelID = S; }
1193 void setEndLabelID(unsigned E) { EndLabelID = E; }
1194
1195 /// AddScope - Add a scope to the scope.
1196 ///
1197 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1198
1199 /// AddVariable - Add a variable to the scope.
1200 ///
1201 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
1202};
1203
1204//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001205/// DwarfDebug - Emits Dwarf debug directives.
Jim Laskey072200c2007-01-29 18:51:14 +00001206///
1207class DwarfDebug : public Dwarf {
Jim Laskey65195462006-10-30 13:35:07 +00001208 //===--------------------------------------------------------------------===//
1209 // Attributes used to construct specific Dwarf sections.
1210 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001211
Evan Chenge3d42322009-02-25 07:04:34 +00001212 /// CompileUnitMap - A map of global variables representing compile units to
1213 /// compile units.
1214 DenseMap<Value *, CompileUnit *> CompileUnitMap;
1215
1216 /// CompileUnits - All the compile units in this module.
1217 ///
1218 SmallVector<CompileUnit *, 8> CompileUnits;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001219
Devang Pateldd9db662009-01-30 18:20:31 +00001220 /// MainCU - Some platform prefers one compile unit per .o file. In such
1221 /// cases, all dies are inserted in MainCU.
1222 CompileUnit *MainCU;
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001223
Jim Laskeyef42a012006-11-02 20:12:39 +00001224 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +00001225 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001226 FoldingSet<DIEAbbrev> AbbreviationsSet;
1227
1228 /// Abbreviations - A list of all the unique abbreviations in use.
1229 ///
1230 std::vector<DIEAbbrev *> Abbreviations;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001231
Evan Chenge3d42322009-02-25 07:04:34 +00001232 /// DirectoryIdMap - Directory name to directory id map.
1233 ///
1234 StringMap<unsigned> DirectoryIdMap;
Devang Patel8526cc02009-01-05 22:35:52 +00001235
Evan Chenge3d42322009-02-25 07:04:34 +00001236 /// DirectoryNames - A list of directory names.
1237 SmallVector<std::string, 8> DirectoryNames;
1238
1239 /// SourceFileIdMap - Source file name to source file id map.
1240 ///
1241 StringMap<unsigned> SourceFileIdMap;
1242
1243 /// SourceFileNames - A list of source file names.
1244 SmallVector<std::string, 8> SourceFileNames;
1245
1246 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
1247 /// id mapped to a unique id.
1248 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
1249
1250 /// SourceIds - Reverse map from source id to directory id + file id pair.
1251 ///
1252 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
Devang Patel8526cc02009-01-05 22:35:52 +00001253
Devang Patel07354e52009-01-16 21:07:53 +00001254 /// Lines - List of of source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001255 std::vector<SrcLineInfo> Lines;
1256
Devang Patel07354e52009-01-16 21:07:53 +00001257 /// ValuesSet - Used to uniquely define values.
1258 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001259 FoldingSet<DIEValue> ValuesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001260
Jim Laskeyef42a012006-11-02 20:12:39 +00001261 /// Values - A list of all the unique values in use.
1262 ///
1263 std::vector<DIEValue *> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001264
Jim Laskey65195462006-10-30 13:35:07 +00001265 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +00001266 ///
Jim Laskey65195462006-10-30 13:35:07 +00001267 UniqueVector<std::string> StringPool;
1268
Jim Laskey65195462006-10-30 13:35:07 +00001269 /// SectionMap - Provides a unique id per text section.
1270 ///
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001271 UniqueVector<const Section*> SectionMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001272
Jim Laskey65195462006-10-30 13:35:07 +00001273 /// SectionSourceLines - Tracks line numbers per text section.
1274 ///
Devang Patelf3ee5142009-01-12 22:54:42 +00001275 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Jim Laskey65195462006-10-30 13:35:07 +00001276
Jim Laskeybacd3042007-02-21 22:48:45 +00001277 /// didInitial - Flag to indicate if initial emission has been done.
1278 ///
1279 bool didInitial;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001280
Jim Laskeybacd3042007-02-21 22:48:45 +00001281 /// shouldEmit - Flag to indicate if debug information should be emitted.
1282 ///
1283 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +00001284
Devang Patel0e5200f2009-01-15 18:25:17 +00001285 // RootDbgScope - Top level scope for the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001286 //
1287 DbgScope *RootDbgScope;
1288
Bill Wendling163ba3f2009-03-10 21:23:25 +00001289 /// DbgScopeMap - Tracks the scopes in the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001290 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
Bill Wendling163ba3f2009-03-10 21:23:25 +00001291
1292 /// DebugTimer - Timer for the Dwarf debug writer.
1293 Timer *DebugTimer;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001294
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001295 struct FunctionDebugFrameInfo {
1296 unsigned Number;
1297 std::vector<MachineMove> Moves;
1298
1299 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001300 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001301 };
1302
1303 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001304
Bill Wendling163ba3f2009-03-10 21:23:25 +00001305private:
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001306 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
Bill Wendlingc8615e42009-03-10 21:47:45 +00001307 /// maps to the source id. Source id starts at 1.
1308 std::pair<unsigned, unsigned>
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001309 getSourceDirectoryAndFileIds(unsigned SId) const {
Bill Wendlingc8615e42009-03-10 21:47:45 +00001310 return SourceIds[SId-1];
1311 }
1312
1313 /// getNumSourceDirectories - Return the number of source directories in the
1314 /// debug info.
1315 unsigned getNumSourceDirectories() const {
1316 return DirectoryNames.size();
1317 }
1318
1319 /// getSourceDirectoryName - Return the name of the directory corresponding
1320 /// to the id.
1321 const std::string &getSourceDirectoryName(unsigned Id) const {
1322 return DirectoryNames[Id - 1];
1323 }
1324
1325 /// getSourceFileName - Return the name of the source file corresponding
1326 /// to the id.
1327 const std::string &getSourceFileName(unsigned Id) const {
1328 return SourceFileNames[Id - 1];
1329 }
1330
1331 /// getNumSourceIds - Return the number of unique source ids.
Bill Wendlingc8615e42009-03-10 21:47:45 +00001332 unsigned getNumSourceIds() const {
1333 return SourceIds.size();
1334 }
1335
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001336 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001337 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001338 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1339 // Profile the node so that we can make it unique.
1340 FoldingSetNodeID ID;
1341 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001342
Jim Laskeyef42a012006-11-02 20:12:39 +00001343 // Check the set for priors.
1344 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001345
Jim Laskeyef42a012006-11-02 20:12:39 +00001346 // If it's newly added.
1347 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001348 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +00001349 Abbreviations.push_back(&Abbrev);
1350 // Assign the vector position + 1 as its number.
1351 Abbrev.setNumber(Abbreviations.size());
1352 } else {
1353 // Assign existing abbreviation number.
1354 Abbrev.setNumber(InSet->getNumber());
1355 }
1356 }
1357
Jim Laskey65195462006-10-30 13:35:07 +00001358 /// NewString - Add a string to the constant pool and returns a label.
1359 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001360 DWLabel NewString(const std::string &String) {
1361 unsigned StringID = StringPool.insert(String);
1362 return DWLabel("string", StringID);
1363 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001364
Jim Laskeyef42a012006-11-02 20:12:39 +00001365 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1366 /// entry.
1367 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1368 DIEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001369
Jim Laskeyef42a012006-11-02 20:12:39 +00001370 if (Entry) {
1371 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001372 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001373 void *Where;
1374 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001375
Jim Laskeyf6733882006-11-02 21:48:18 +00001376 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001377
Jim Laskeyef42a012006-11-02 20:12:39 +00001378 Value = new DIEntry(Entry);
1379 ValuesSet.InsertNode(Value, Where);
1380 } else {
1381 Value = new DIEntry(Entry);
1382 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001383
Jim Laskeyef42a012006-11-02 20:12:39 +00001384 Values.push_back(Value);
1385 return Value;
1386 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001387
Jim Laskeyef42a012006-11-02 20:12:39 +00001388 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1389 ///
1390 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1391 Value->Entry = Entry;
1392 // Add to values set if not already there. If it is, we merely have a
1393 // duplicate in the values list (no harm.)
1394 ValuesSet.GetOrInsertNode(Value);
1395 }
1396
1397 /// AddUInt - Add an unsigned integer attribute data and value.
1398 ///
1399 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1400 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1401
1402 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001403 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001404 void *Where;
1405 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1406 if (!Value) {
1407 Value = new DIEInteger(Integer);
1408 ValuesSet.InsertNode(Value, Where);
1409 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001410 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001411
Jim Laskeyef42a012006-11-02 20:12:39 +00001412 Die->AddValue(Attribute, Form, Value);
1413 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001414
Jim Laskeyef42a012006-11-02 20:12:39 +00001415 /// AddSInt - Add an signed integer attribute data and value.
1416 ///
1417 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1418 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1419
1420 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001421 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001422 void *Where;
1423 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1424 if (!Value) {
1425 Value = new DIEInteger(Integer);
1426 ValuesSet.InsertNode(Value, Where);
1427 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001428 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001429
Jim Laskeyef42a012006-11-02 20:12:39 +00001430 Die->AddValue(Attribute, Form, Value);
1431 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001432
Evan Chenge3d42322009-02-25 07:04:34 +00001433 /// AddString - Add a string attribute data and value.
Jim Laskeyef42a012006-11-02 20:12:39 +00001434 ///
1435 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1436 const std::string &String) {
1437 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001438 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001439 void *Where;
1440 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1441 if (!Value) {
1442 Value = new DIEString(String);
1443 ValuesSet.InsertNode(Value, Where);
1444 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001445 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001446
Jim Laskeyef42a012006-11-02 20:12:39 +00001447 Die->AddValue(Attribute, Form, Value);
1448 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001449
Jim Laskeyef42a012006-11-02 20:12:39 +00001450 /// AddLabel - Add a Dwarf label attribute data and value.
1451 ///
1452 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1453 const DWLabel &Label) {
1454 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001455 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001456 void *Where;
1457 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1458 if (!Value) {
1459 Value = new DIEDwarfLabel(Label);
1460 ValuesSet.InsertNode(Value, Where);
1461 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001462 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001463
Jim Laskeyef42a012006-11-02 20:12:39 +00001464 Die->AddValue(Attribute, Form, Value);
1465 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001466
Jim Laskeyef42a012006-11-02 20:12:39 +00001467 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1468 ///
1469 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1470 const std::string &Label) {
1471 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001472 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001473 void *Where;
1474 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1475 if (!Value) {
1476 Value = new DIEObjectLabel(Label);
1477 ValuesSet.InsertNode(Value, Where);
1478 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001479 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001480
Jim Laskeyef42a012006-11-02 20:12:39 +00001481 Die->AddValue(Attribute, Form, Value);
1482 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001483
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001484 /// AddSectionOffset - Add a section offset label attribute data and value.
1485 ///
1486 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1487 const DWLabel &Label, const DWLabel &Section,
1488 bool isEH = false, bool useSet = true) {
1489 FoldingSetNodeID ID;
1490 DIESectionOffset::Profile(ID, Label, Section);
1491 void *Where;
1492 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1493 if (!Value) {
1494 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1495 ValuesSet.InsertNode(Value, Where);
1496 Values.push_back(Value);
1497 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001498
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001499 Die->AddValue(Attribute, Form, Value);
1500 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001501
Jim Laskeyef42a012006-11-02 20:12:39 +00001502 /// AddDelta - Add a label delta attribute data and value.
1503 ///
1504 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1505 const DWLabel &Hi, const DWLabel &Lo) {
1506 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001507 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001508 void *Where;
1509 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1510 if (!Value) {
1511 Value = new DIEDelta(Hi, Lo);
1512 ValuesSet.InsertNode(Value, Where);
1513 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001514 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001515
Jim Laskeyef42a012006-11-02 20:12:39 +00001516 Die->AddValue(Attribute, Form, Value);
1517 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001518
Jim Laskeyef42a012006-11-02 20:12:39 +00001519 /// AddDIEntry - Add a DIE attribute data and value.
1520 ///
1521 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1522 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1523 }
1524
1525 /// AddBlock - Add block data.
1526 ///
1527 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1528 Block->ComputeSize(*this);
1529 FoldingSetNodeID ID;
1530 Block->Profile(ID);
1531 void *Where;
1532 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1533 if (!Value) {
1534 Value = Block;
1535 ValuesSet.InsertNode(Value, Where);
1536 Values.push_back(Value);
1537 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001538 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001539 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001540 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001541 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001542
Jim Laskeyef42a012006-11-02 20:12:39 +00001543 Die->AddValue(Attribute, Block->BestForm(), Value);
1544 }
1545
Jim Laskey65195462006-10-30 13:35:07 +00001546 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001547 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001548 void AddSourceLine(DIE *Die, const DIVariable *V) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001549 unsigned FileID = 0;
1550 unsigned Line = V->getLineNumber();
Devang Pateldd9db662009-01-30 18:20:31 +00001551 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1552 FileID = Unit->getID();
Devang Patel2303df92009-02-10 06:04:08 +00001553 assert (FileID && "Invalid file id");
Devang Patel7a6e5a32009-01-08 02:33:41 +00001554 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1555 AddUInt(Die, DW_AT_decl_line, 0, Line);
1556 }
1557
1558 /// AddSourceLine - Add location information to specified debug information
1559 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001560 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Devang Patel8526cc02009-01-05 22:35:52 +00001561 unsigned FileID = 0;
1562 unsigned Line = G->getLineNumber();
Devang Pateldd9db662009-01-30 18:20:31 +00001563 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1564 FileID = Unit->getID();
Devang Patel2303df92009-02-10 06:04:08 +00001565 assert (FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001566 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1567 AddUInt(Die, DW_AT_decl_line, 0, Line);
1568 }
1569
Devang Patel99ec3532009-01-16 19:28:14 +00001570 void AddSourceLine(DIE *Die, const DIType *Ty) {
Devang Patel8526cc02009-01-05 22:35:52 +00001571 unsigned FileID = 0;
Devang Patel99ec3532009-01-16 19:28:14 +00001572 unsigned Line = Ty->getLineNumber();
Devang Pateldd9db662009-01-30 18:20:31 +00001573 DICompileUnit CU = Ty->getCompileUnit();
1574 if (CU.isNull())
1575 return;
1576 CompileUnit *Unit = FindCompileUnit(CU);
1577 FileID = Unit->getID();
Devang Patel2303df92009-02-10 06:04:08 +00001578 assert (FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001579 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1580 AddUInt(Die, DW_AT_decl_line, 0, Line);
1581 }
1582
Jim Laskey65195462006-10-30 13:35:07 +00001583 /// AddAddress - Add an address attribute to a die based on the location
1584 /// provided.
1585 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001586 const MachineLocation &Location) {
Dan Gohmand735b802008-10-03 15:45:36 +00001587 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001588 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001589
Dan Gohmand735b802008-10-03 15:45:36 +00001590 if (Location.isReg()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001591 if (Reg < 32) {
1592 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1593 } else {
1594 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1595 AddUInt(Block, 0, DW_FORM_udata, Reg);
1596 }
1597 } else {
1598 if (Reg < 32) {
1599 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1600 } else {
1601 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1602 AddUInt(Block, 0, DW_FORM_udata, Reg);
1603 }
1604 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1605 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001606
Jim Laskeyef42a012006-11-02 20:12:39 +00001607 AddBlock(Die, Attribute, 0, Block);
1608 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001609
Jim Laskeyef42a012006-11-02 20:12:39 +00001610 /// AddType - Add a new type attribute to the specified entity.
Devang Patel48d190f2009-01-05 21:47:57 +00001611 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel80303aa2009-01-23 19:13:31 +00001612 if (Ty.isNull())
Devang Patel48d190f2009-01-05 21:47:57 +00001613 return;
Devang Patel48d190f2009-01-05 21:47:57 +00001614
1615 // Check for pre-existence.
1616 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1617 // If it exists then use the existing value.
1618 if (Slot) {
1619 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1620 return;
1621 }
1622
1623 // Set up proxy.
1624 Slot = NewDIEntry();
1625
1626 // Construct type.
1627 DIE Buffer(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001628 if (Ty.isBasicType(Ty.getTag()))
1629 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1630 else if (Ty.isDerivedType(Ty.getTag()))
1631 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1632 else {
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001633 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelf193ff02009-01-15 19:26:23 +00001634 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1635 }
1636
Devang Patel7009d242009-01-27 23:22:55 +00001637 // Add debug information entry to entity and appropriate context.
1638 DIE *Die = NULL;
1639 DIDescriptor Context = Ty.getContext();
1640 if (!Context.isNull())
1641 Die = DW_Unit->getDieMapSlotFor(Context.getGV());
1642
1643 if (Die) {
1644 DIE *Child = new DIE(Buffer);
1645 Die->AddChild(Child);
1646 Buffer.Detach();
1647 SetDIEntry(Slot, Child);
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001648 } else {
Devang Patel7009d242009-01-27 23:22:55 +00001649 Die = DW_Unit->AddDie(Buffer);
1650 SetDIEntry(Slot, Die);
1651 }
1652
Devang Patel48d190f2009-01-05 21:47:57 +00001653 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1654 }
1655
Devang Patele5202732009-01-05 19:07:53 +00001656 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1657 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001658 DIBasicType BTy) {
Devang Patel08f053f2009-01-05 17:57:47 +00001659
1660 // Get core information.
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001661 std::string Name;
1662 BTy.getName(Name);
Devang Patel08f053f2009-01-05 17:57:47 +00001663 Buffer.setTag(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001664 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patel08f053f2009-01-05 17:57:47 +00001665 // Add name if not anonymous or intermediate type.
1666 if (!Name.empty())
1667 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001668 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel08f053f2009-01-05 17:57:47 +00001669 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1670 }
1671
Devang Patele5202732009-01-05 19:07:53 +00001672 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1673 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001674 DIDerivedType DTy) {
Devang Patel08f053f2009-01-05 17:57:47 +00001675
1676 // Get core information.
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001677 std::string Name;
1678 DTy.getName(Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001679 uint64_t Size = DTy.getSizeInBits() >> 3;
1680 unsigned Tag = DTy.getTag();
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001681
Devang Patel08f053f2009-01-05 17:57:47 +00001682 // FIXME - Workaround for templates.
1683 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1684
1685 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001686
Devang Patel08f053f2009-01-05 17:57:47 +00001687 // Map to main type, void will not have a type.
Devang Patelf193ff02009-01-15 19:26:23 +00001688 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00001689 AddType(DW_Unit, &Buffer, FromTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001690
1691 // Add name if not anonymous or intermediate type.
Evan Chenge3d42322009-02-25 07:04:34 +00001692 if (!Name.empty())
1693 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel08f053f2009-01-05 17:57:47 +00001694
1695 // Add size if non-zero (derived types might be zero-sized.)
1696 if (Size)
1697 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1698
1699 // Add source line info if available and TyDesc is not a forward
1700 // declaration.
Devang Patelad165be2009-01-27 00:45:04 +00001701 if (!DTy.isForwardDecl())
1702 AddSourceLine(&Buffer, &DTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001703 }
1704
Devang Patelf4215332009-01-05 19:55:51 +00001705 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1706 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001707 DICompositeType CTy) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001708 // Get core information.
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001709 std::string Name;
1710 CTy.getName(Name);
1711
Devang Patelf193ff02009-01-15 19:26:23 +00001712 uint64_t Size = CTy.getSizeInBits() >> 3;
1713 unsigned Tag = CTy.getTag();
Devang Patel49f38cb2009-01-23 01:19:09 +00001714 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001715
Devang Patelf4215332009-01-05 19:55:51 +00001716 switch (Tag) {
1717 case DW_TAG_vector_type:
1718 case DW_TAG_array_type:
Devang Patelf193ff02009-01-15 19:26:23 +00001719 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001720 break;
Devang Pateleab4a2e2009-01-20 18:35:14 +00001721 case DW_TAG_enumeration_type:
1722 {
1723 DIArray Elements = CTy.getTypeArray();
1724 // Add enumerators to enumeration type.
1725 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1726 DIE *ElemDie = NULL;
1727 DIEnumerator Enum(Elements.getElement(i).getGV());
1728 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
1729 Buffer.AddChild(ElemDie);
1730 }
1731 }
1732 break;
Devang Patelf4215332009-01-05 19:55:51 +00001733 case DW_TAG_subroutine_type:
1734 {
1735 // Add prototype flag.
1736 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patelf193ff02009-01-15 19:26:23 +00001737 DIArray Elements = CTy.getTypeArray();
Devang Patelf4215332009-01-05 19:55:51 +00001738 // Add return type.
Devang Patel48d190f2009-01-05 21:47:57 +00001739 DIDescriptor RTy = Elements.getElement(0);
Devang Patelf193ff02009-01-15 19:26:23 +00001740 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel48d190f2009-01-05 21:47:57 +00001741
Devang Patelf4215332009-01-05 19:55:51 +00001742 // Add arguments.
1743 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1744 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00001745 DIDescriptor Ty = Elements.getElement(i);
Devang Patel7ab24502009-01-17 06:57:25 +00001746 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patelf4215332009-01-05 19:55:51 +00001747 Buffer.AddChild(Arg);
1748 }
1749 }
1750 break;
1751 case DW_TAG_structure_type:
1752 case DW_TAG_union_type:
1753 {
1754 // Add elements to structure type.
Devang Patelf193ff02009-01-15 19:26:23 +00001755 DIArray Elements = CTy.getTypeArray();
Devang Patel153745c2009-01-16 00:50:53 +00001756
1757 // A forward struct declared type may not have elements available.
1758 if (Elements.isNull())
1759 break;
1760
Devang Patelf4215332009-01-05 19:55:51 +00001761 // Add elements to structure type.
1762 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1763 DIDescriptor Element = Elements.getElement(i);
Devang Patel5aac3d32009-01-17 08:01:33 +00001764 DIE *ElemDie = NULL;
Devang Patelf193ff02009-01-15 19:26:23 +00001765 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel2d1768c2009-01-17 08:05:14 +00001766 ElemDie = CreateSubprogramDIE(DW_Unit,
1767 DISubprogram(Element.getGV()));
Devang Patel5aac3d32009-01-17 08:01:33 +00001768 else if (Element.getTag() == dwarf::DW_TAG_variable) // ???
1769 ElemDie = CreateGlobalVariableDIE(DW_Unit,
1770 DIGlobalVariable(Element.getGV()));
Devang Patel2be58932009-01-20 21:02:02 +00001771 else
1772 ElemDie = CreateMemberDIE(DW_Unit,
1773 DIDerivedType(Element.getGV()));
Devang Patel2d1768c2009-01-17 08:05:14 +00001774 Buffer.AddChild(ElemDie);
Devang Patelf4215332009-01-05 19:55:51 +00001775 }
Devang Patel13319ce2009-02-17 22:43:44 +00001776 unsigned RLang = CTy.getRunTimeLang();
1777 if (RLang)
1778 AddUInt(&Buffer, DW_AT_APPLE_runtime_class, DW_FORM_data1, RLang);
Devang Patelf4215332009-01-05 19:55:51 +00001779 }
1780 break;
1781 default:
1782 break;
1783 }
1784
1785 // Add name if not anonymous or intermediate type.
Evan Chenge3d42322009-02-25 07:04:34 +00001786 if (!Name.empty())
1787 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf4215332009-01-05 19:55:51 +00001788
Devang Patelad165be2009-01-27 00:45:04 +00001789 if (Tag == DW_TAG_enumeration_type || Tag == DW_TAG_structure_type
1790 || Tag == DW_TAG_union_type) {
1791 // Add size if non-zero (derived types might be zero-sized.)
1792 if (Size)
1793 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1794 else {
1795 // Add zero size if it is not a forward declaration.
1796 if (CTy.isForwardDecl())
1797 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1798 else
1799 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1800 }
1801
1802 // Add source line info if available.
1803 if (!CTy.isForwardDecl())
1804 AddSourceLine(&Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001805 }
Devang Patelf4215332009-01-05 19:55:51 +00001806 }
1807
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001808 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1809 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelf193ff02009-01-15 19:26:23 +00001810 int64_t L = SR.getLo();
1811 int64_t H = SR.getHi();
Devang Patel68afdc32009-01-05 18:33:01 +00001812 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1813 if (L != H) {
1814 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1815 if (L)
Devang Patel2d1768c2009-01-17 08:05:14 +00001816 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1817 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel68afdc32009-01-05 18:33:01 +00001818 }
1819 Buffer.AddChild(DW_Subrange);
1820 }
1821
1822 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1823 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1824 DICompositeType *CTy) {
1825 Buffer.setTag(DW_TAG_array_type);
1826 if (CTy->getTag() == DW_TAG_vector_type)
1827 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1828
Devang Patelf9235742009-01-28 21:08:20 +00001829 // Emit derived type.
1830 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel68afdc32009-01-05 18:33:01 +00001831 DIArray Elements = CTy->getTypeArray();
Devang Patel68afdc32009-01-05 18:33:01 +00001832
1833 // Construct an anonymous type for index type.
1834 DIE IdxBuffer(DW_TAG_base_type);
1835 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1836 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1837 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1838
1839 // Add subranges to array type.
1840 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patelf4215332009-01-05 19:55:51 +00001841 DIDescriptor Element = Elements.getElement(i);
Devang Patelf193ff02009-01-15 19:26:23 +00001842 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1843 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel68afdc32009-01-05 18:33:01 +00001844 }
1845 }
1846
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001847 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Pateleab4a2e2009-01-20 18:35:14 +00001848 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patelc69bf2c2009-01-05 18:38:38 +00001849
1850 DIE *Enumerator = new DIE(DW_TAG_enumerator);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001851 std::string Name;
1852 ETy->getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001853 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
Devang Patelc69bf2c2009-01-05 18:38:38 +00001854 int64_t Value = ETy->getEnumValue();
1855 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Pateleab4a2e2009-01-20 18:35:14 +00001856 return Enumerator;
Devang Patelc69bf2c2009-01-05 18:38:38 +00001857 }
Devang Patel68afdc32009-01-05 18:33:01 +00001858
Devang Patel5aac3d32009-01-17 08:01:33 +00001859 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001860 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patel5aac3d32009-01-17 08:01:33 +00001861 {
1862 DIE *GVDie = new DIE(DW_TAG_variable);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001863 std::string Name;
1864 GV.getDisplayName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001865 AddString(GVDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001866 std::string LinkageName;
1867 GV.getLinkageName(LinkageName);
Devang Patel86ae1422009-01-05 18:59:44 +00001868 if (!LinkageName.empty())
Devang Patel5aac3d32009-01-17 08:01:33 +00001869 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1870 AddType(DW_Unit, GVDie, GV.getType());
1871 if (!GV.isLocalToUnit())
1872 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
1873 AddSourceLine(GVDie, &GV);
1874 return GVDie;
Devang Patel86ae1422009-01-05 18:59:44 +00001875 }
1876
Devang Patel2be58932009-01-20 21:02:02 +00001877 /// CreateMemberDIE - Create new member DIE.
1878 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
1879 DIE *MemberDie = new DIE(DT.getTag());
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001880 std::string Name;
1881 DT.getName(Name);
Devang Patel2be58932009-01-20 21:02:02 +00001882 if (!Name.empty())
1883 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1884
1885 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1886
1887 AddSourceLine(MemberDie, &DT);
1888
Devang Patel36375ee2009-02-17 21:23:59 +00001889 uint64_t Size = DT.getSizeInBits();
1890 uint64_t FieldSize = DT.getOriginalTypeSize();
1891
1892 if (Size != FieldSize) {
1893 // Handle bitfield.
1894 AddUInt(MemberDie, DW_AT_byte_size, 0, DT.getOriginalTypeSize() >> 3);
1895 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1896
1897 uint64_t Offset = DT.getOffsetInBits();
1898 uint64_t FieldOffset = Offset;
1899 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1900 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1901 FieldOffset = (HiMark - FieldSize);
1902 Offset -= FieldOffset;
1903 // Maybe we need to work from the other end.
1904 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1905 AddUInt(MemberDie, DW_AT_bit_offset, 0, Offset);
1906 }
Devang Patel2be58932009-01-20 21:02:02 +00001907 DIEBlock *Block = new DIEBlock();
1908 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1909 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1910 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1911
Devang Patel47661592009-01-21 00:08:04 +00001912 if (DT.isProtected())
1913 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1914 else if (DT.isPrivate())
1915 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1916
Devang Patel2be58932009-01-20 21:02:02 +00001917 return MemberDie;
1918 }
1919
Devang Patel5aac3d32009-01-17 08:01:33 +00001920 /// CreateSubprogramDIE - Create new DIE using SP.
1921 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Devang Patel2d1768c2009-01-17 08:05:14 +00001922 const DISubprogram &SP,
1923 bool IsConstructor = false) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001924 DIE *SPDie = new DIE(DW_TAG_subprogram);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001925 std::string Name;
1926 SP.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001927 AddString(SPDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001928 std::string LinkageName;
1929 SP.getLinkageName(LinkageName);
Devang Patel86ae1422009-01-05 18:59:44 +00001930 if (!LinkageName.empty())
Devang Patel5aac3d32009-01-17 08:01:33 +00001931 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Devang Patel2d1768c2009-01-17 08:05:14 +00001932 LinkageName);
Devang Patel5aac3d32009-01-17 08:01:33 +00001933 AddSourceLine(SPDie, &SP);
Devang Patel86ae1422009-01-05 18:59:44 +00001934
Devang Patel5aac3d32009-01-17 08:01:33 +00001935 DICompositeType SPTy = SP.getType();
1936 DIArray Args = SPTy.getTypeArray();
1937
Devang Patel86ae1422009-01-05 18:59:44 +00001938 // Add Return Type.
Devang Patel9ac08d62009-02-27 18:05:21 +00001939 if (!IsConstructor) {
1940 if (Args.isNull())
1941 AddType(DW_Unit, SPDie, SPTy);
1942 else
1943 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
1944 }
Devang Pateld234e592009-01-30 01:21:46 +00001945
Devang Pateld5863dd2009-02-02 17:51:41 +00001946 if (!SP.isDefinition()) {
1947 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
1948 // Add arguments.
1949 // Do not add arguments for subprogram definition. They will be
1950 // handled through RecordVariable.
1951 if (!Args.isNull())
1952 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1953 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1954 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
1955 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
1956 SPDie->AddChild(Arg);
1957 }
1958 }
Devang Pateld234e592009-01-30 01:21:46 +00001959
Devang Patel002ec142009-02-24 00:52:19 +00001960 unsigned Lang = SP.getCompileUnit().getLanguage();
1961 if (Lang == DW_LANG_C99 || Lang == DW_LANG_C89
1962 || Lang == DW_LANG_ObjC)
1963 AddUInt(SPDie, DW_AT_prototyped, DW_FORM_flag, 1);
1964
Devang Patelf193ff02009-01-15 19:26:23 +00001965 if (!SP.isLocalToUnit())
Devang Pateld234e592009-01-30 01:21:46 +00001966 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Devang Patel5aac3d32009-01-17 08:01:33 +00001967 return SPDie;
Devang Patel86ae1422009-01-05 18:59:44 +00001968 }
1969
Devang Patel5aac3d32009-01-17 08:01:33 +00001970 /// FindCompileUnit - Get the compile unit for the given descriptor.
1971 ///
Devang Patel8526cc02009-01-05 22:35:52 +00001972 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
Evan Chenge3d42322009-02-25 07:04:34 +00001973 CompileUnit *DW_Unit = CompileUnitMap[Unit.getGV()];
Devang Patel8526cc02009-01-05 22:35:52 +00001974 assert(DW_Unit && "Missing compile unit.");
1975 return DW_Unit;
1976 }
1977
Devang Patelbbdc8202009-01-13 23:54:55 +00001978 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001979 ///
1980 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1981 // Get the descriptor.
Devang Patel99ec3532009-01-16 19:28:14 +00001982 const DIVariable &VD = DV->getVariable();
Devang Patel7a6e5a32009-01-08 02:33:41 +00001983
1984 // Translate tag to proper Dwarf tag. The result variable is dropped for
1985 // now.
1986 unsigned Tag;
Devang Patel99ec3532009-01-16 19:28:14 +00001987 switch (VD.getTag()) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001988 case DW_TAG_return_variable: return NULL;
1989 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1990 case DW_TAG_auto_variable: // fall thru
1991 default: Tag = DW_TAG_variable; break;
1992 }
1993
1994 // Define variable debug information entry.
1995 DIE *VariableDie = new DIE(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001996 std::string Name;
1997 VD.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001998 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Devang Patel7a6e5a32009-01-08 02:33:41 +00001999
2000 // Add source line info if available.
Devang Patel99ec3532009-01-16 19:28:14 +00002001 AddSourceLine(VariableDie, &VD);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002002
2003 // Add variable type.
Devang Patel99ec3532009-01-16 19:28:14 +00002004 AddType(Unit, VariableDie, VD.getType());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002005
2006 // Add variable address.
2007 MachineLocation Location;
2008 Location.set(RI->getFrameRegister(*MF),
2009 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
2010 AddAddress(VariableDie, DW_AT_location, Location);
2011
2012 return VariableDie;
2013 }
2014
Devang Patel7a6e5a32009-01-08 02:33:41 +00002015 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2016 ///
2017 DbgScope *getOrCreateScope(GlobalVariable *V) {
2018 DbgScope *&Slot = DbgScopeMap[V];
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002019 if (Slot) return Slot;
2020
2021 // FIXME - breaks down when the context is an inlined function.
2022 DIDescriptor ParentDesc;
2023 DIDescriptor Desc(V);
2024
2025 if (Desc.getTag() == dwarf::DW_TAG_lexical_block) {
2026 DIBlock Block(V);
2027 ParentDesc = Block.getContext();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002028 }
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002029
2030 DbgScope *Parent = ParentDesc.isNull() ?
2031 NULL : getOrCreateScope(ParentDesc.getGV());
2032 Slot = new DbgScope(Parent, Desc);
2033
2034 if (Parent) {
2035 Parent->AddScope(Slot);
2036 } else if (RootDbgScope) {
2037 // FIXME - Add inlined function scopes to the root so we can delete them
2038 // later. Long term, handle inlined functions properly.
2039 RootDbgScope->AddScope(Slot);
2040 } else {
2041 // First function is top level function.
2042 RootDbgScope = Slot;
2043 }
2044
Devang Patel7a6e5a32009-01-08 02:33:41 +00002045 return Slot;
2046 }
2047
2048 /// ConstructDbgScope - Construct the components of a scope.
2049 ///
2050 void ConstructDbgScope(DbgScope *ParentScope,
2051 unsigned ParentStartID, unsigned ParentEndID,
2052 DIE *ParentDie, CompileUnit *Unit) {
2053 // Add variables to scope.
Devang Patel9795da52009-01-10 02:42:49 +00002054 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002055 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2056 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2057 if (VariableDie) ParentDie->AddChild(VariableDie);
2058 }
2059
2060 // Add nested scopes.
Devang Patel9795da52009-01-10 02:42:49 +00002061 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002062 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2063 // Define the Scope debug information entry.
2064 DbgScope *Scope = Scopes[j];
2065 // FIXME - Ignore inlined functions for the time being.
2066 if (!Scope->getParent()) continue;
2067
Devang Patele9bfb0f2009-01-12 18:41:00 +00002068 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2069 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002070
2071 // Ignore empty scopes.
2072 if (StartID == EndID && StartID != 0) continue;
2073 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
2074
2075 if (StartID == ParentStartID && EndID == ParentEndID) {
2076 // Just add stuff to the parent scope.
2077 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2078 } else {
2079 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2080
2081 // Add the scope bounds.
2082 if (StartID) {
2083 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2084 DWLabel("label", StartID));
2085 } else {
2086 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2087 DWLabel("func_begin", SubprogramCount));
2088 }
2089 if (EndID) {
2090 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2091 DWLabel("label", EndID));
2092 } else {
2093 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2094 DWLabel("func_end", SubprogramCount));
2095 }
2096
2097 // Add the scope contents.
2098 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2099 ParentDie->AddChild(ScopeDie);
2100 }
2101 }
2102 }
2103
2104 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2105 ///
2106 void ConstructRootDbgScope(DbgScope *RootScope) {
2107 // Exit if there is no root scope.
2108 if (!RootScope) return;
Devang Patel0e5200f2009-01-15 18:25:17 +00002109 DIDescriptor Desc = RootScope->getDesc();
2110 if (Desc.isNull())
2111 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002112
2113 // Get the subprogram debug information entry.
Devang Patel0e5200f2009-01-15 18:25:17 +00002114 DISubprogram SPD(Desc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002115
2116 // Get the compile unit context.
Devang Pateldd9db662009-01-30 18:20:31 +00002117 CompileUnit *Unit = MainCU;
2118 if (!Unit)
2119 Unit = FindCompileUnit(SPD.getCompileUnit());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002120
2121 // Get the subprogram die.
Devang Patelf6bac3e2009-01-12 22:58:14 +00002122 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002123 assert(SPDie && "Missing subprogram descriptor");
2124
2125 // Add the function bounds.
2126 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2127 DWLabel("func_begin", SubprogramCount));
2128 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2129 DWLabel("func_end", SubprogramCount));
2130 MachineLocation Location(RI->getFrameRegister(*MF));
2131 AddAddress(SPDie, DW_AT_frame_base, Location);
2132
2133 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2134 }
2135
2136 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2137 ///
2138 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Chenge3d42322009-02-25 07:04:34 +00002139 const char *FnName = MF->getFunction()->getNameStart();
2140 if (MainCU) {
2141 std::map<std::string, DIE*> &Globals = MainCU->getGlobals();
2142 std::map<std::string, DIE*>::iterator GI = Globals.find(FnName);
2143 if (GI != Globals.end()) {
2144 DIE *SPDie = GI->second;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002145
2146 // Add the function bounds.
2147 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2148 DWLabel("func_begin", SubprogramCount));
2149 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2150 DWLabel("func_end", SubprogramCount));
2151
2152 MachineLocation Location(RI->getFrameRegister(*MF));
2153 AddAddress(SPDie, DW_AT_frame_base, Location);
2154 return;
2155 }
Evan Chenge3d42322009-02-25 07:04:34 +00002156 } else {
2157 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2158 CompileUnit *Unit = CompileUnits[i];
2159 std::map<std::string, DIE*> &Globals = Unit->getGlobals();
2160 std::map<std::string, DIE*>::iterator GI = Globals.find(FnName);
2161 if (GI != Globals.end()) {
2162 DIE *SPDie = GI->second;
2163
2164 // Add the function bounds.
2165 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2166 DWLabel("func_begin", SubprogramCount));
2167 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2168 DWLabel("func_end", SubprogramCount));
2169
2170 MachineLocation Location(RI->getFrameRegister(*MF));
2171 AddAddress(SPDie, DW_AT_frame_base, Location);
2172 return;
2173 }
2174 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00002175 }
Evan Chenge3d42322009-02-25 07:04:34 +00002176
Devang Patel7a6e5a32009-01-08 02:33:41 +00002177#if 0
2178 // FIXME: This is causing an abort because C++ mangled names are compared
2179 // with their unmangled counterparts. See PR2885. Don't do this assert.
2180 assert(0 && "Couldn't find DIE for machine function!");
2181#endif
Evan Chenge3d42322009-02-25 07:04:34 +00002182 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002183 }
2184
Jim Laskeyef42a012006-11-02 20:12:39 +00002185 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2186 /// tools to recognize the object file contains Dwarf information.
2187 void EmitInitial() {
2188 // Check to see if we already emitted intial headers.
2189 if (didInitial) return;
2190 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002191
Jim Laskeyef42a012006-11-02 20:12:39 +00002192 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002193 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002194 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002195 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002196 }
2197 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2198 EmitLabel("section_info", 0);
2199 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2200 EmitLabel("section_abbrev", 0);
2201 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2202 EmitLabel("section_aranges", 0);
Scott Michel210de722009-01-26 22:32:51 +00002203 if (TAI->doesSupportMacInfoSection()) {
2204 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2205 EmitLabel("section_macinfo", 0);
2206 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002207 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2208 EmitLabel("section_line", 0);
2209 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2210 EmitLabel("section_loc", 0);
2211 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2212 EmitLabel("section_pubnames", 0);
2213 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2214 EmitLabel("section_str", 0);
2215 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2216 EmitLabel("section_ranges", 0);
2217
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002218 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002219 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002220 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002221 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002222 }
2223
Jim Laskey65195462006-10-30 13:35:07 +00002224 /// EmitDIE - Recusively Emits a debug information entry.
2225 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002226 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002227 // Get the abbreviation for this DIE.
2228 unsigned AbbrevNumber = Die->getAbbrevNumber();
2229 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002230
Jim Laskeybacd3042007-02-21 22:48:45 +00002231 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002232
2233 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002234 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002235
2236 if (VerboseAsm)
2237 Asm->EOL(std::string("Abbrev [" +
2238 utostr(AbbrevNumber) +
2239 "] 0x" + utohexstr(Die->getOffset()) +
2240 ":0x" + utohexstr(Die->getSize()) + " " +
2241 TagString(Abbrev->getTag())));
2242 else
2243 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002244
Owen Anderson873e1b52008-06-24 21:44:59 +00002245 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2246 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002247
Jim Laskeyef42a012006-11-02 20:12:39 +00002248 // Emit the DIE attribute values.
2249 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2250 unsigned Attr = AbbrevData[i].getAttribute();
2251 unsigned Form = AbbrevData[i].getForm();
2252 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002253
Jim Laskeyef42a012006-11-02 20:12:39 +00002254 switch (Attr) {
2255 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002256 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002257 break;
2258 }
2259 default: {
2260 // Emit an attribute using the defined form.
2261 Values[i]->EmitValue(*this, Form);
2262 break;
2263 }
2264 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002265
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002266 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002267 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002268
Jim Laskeyef42a012006-11-02 20:12:39 +00002269 // Emit the DIE children if any.
2270 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2271 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002272
Jim Laskeyef42a012006-11-02 20:12:39 +00002273 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2274 EmitDIE(Children[j]);
2275 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002276
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002277 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002278 }
2279 }
2280
Jim Laskey65195462006-10-30 13:35:07 +00002281 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2282 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002283 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2284 // Get the children.
2285 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002286
Jim Laskeyef42a012006-11-02 20:12:39 +00002287 // If not last sibling and has children then add sibling offset attribute.
2288 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2289
2290 // Record the abbreviation.
2291 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002292
Jim Laskeyef42a012006-11-02 20:12:39 +00002293 // Get the abbreviation for this DIE.
2294 unsigned AbbrevNumber = Die->getAbbrevNumber();
2295 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2296
2297 // Set DIE offset
2298 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002299
Jim Laskeyef42a012006-11-02 20:12:39 +00002300 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002301 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2302
Owen Anderson873e1b52008-06-24 21:44:59 +00002303 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2304 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002305
2306 // Size the DIE attribute values.
2307 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2308 // Size attribute value.
2309 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2310 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002311
Jim Laskeyef42a012006-11-02 20:12:39 +00002312 // Size the DIE children if any.
2313 if (!Children.empty()) {
2314 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2315 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002316
Jim Laskeyef42a012006-11-02 20:12:39 +00002317 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2318 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2319 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002320
Jim Laskeyef42a012006-11-02 20:12:39 +00002321 // End of children marker.
2322 Offset += sizeof(int8_t);
2323 }
2324
2325 Die->setSize(Offset - Die->getOffset());
2326 return Offset;
2327 }
Jim Laskey65195462006-10-30 13:35:07 +00002328
2329 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2330 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002331 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002332 // Process base compile unit.
Devang Pateldd9db662009-01-30 18:20:31 +00002333 if (MainCU) {
2334 // Compute size of compile unit header
2335 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2336 sizeof(int16_t) + // DWARF version number
2337 sizeof(int32_t) + // Offset Into Abbrev. Section
2338 sizeof(int8_t); // Pointer Size (in bytes)
2339 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
2340 return;
2341 }
Evan Chenge3d42322009-02-25 07:04:34 +00002342 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2343 CompileUnit *Unit = CompileUnits[i];
Devang Patel72b66352009-01-12 23:05:55 +00002344 // Compute size of compile unit header
2345 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2346 sizeof(int16_t) + // DWARF version number
2347 sizeof(int32_t) + // Offset Into Abbrev. Section
2348 sizeof(int8_t); // Pointer Size (in bytes)
2349 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2350 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002351 }
2352
Evan Chenge3d42322009-02-25 07:04:34 +00002353 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Jim Laskey65195462006-10-30 13:35:07 +00002354 ///
Evan Chenge3d42322009-02-25 07:04:34 +00002355 void EmitDebugInfoPerCU(CompileUnit *Unit) {
2356 DIE *Die = Unit->getDie();
2357 // Emit the compile units header.
2358 EmitLabel("info_begin", Unit->getID());
2359 // Emit size of content not including length itself
2360 unsigned ContentSize = Die->getSize() +
2361 sizeof(int16_t) + // DWARF version number
2362 sizeof(int32_t) + // Offset Into Abbrev. Section
2363 sizeof(int8_t) + // Pointer Size (in bytes)
2364 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2365
2366 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2367 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2368 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2369 Asm->EOL("Offset Into Abbrev. Section");
2370 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2371
2372 EmitDIE(Die);
2373 // FIXME - extra padding for gdb bug.
2374 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2375 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2376 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2377 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2378 EmitLabel("info_end", Unit->getID());
2379
2380 Asm->EOL();
2381 }
2382
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002383 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002384 // Start debug info section.
2385 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002386
Evan Chenge3d42322009-02-25 07:04:34 +00002387 if (MainCU) {
2388 EmitDebugInfoPerCU(MainCU);
2389 return;
Devang Patel72b66352009-01-12 23:05:55 +00002390 }
Evan Chenge3d42322009-02-25 07:04:34 +00002391
2392 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2393 EmitDebugInfoPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002394 }
2395
Jim Laskey65195462006-10-30 13:35:07 +00002396 /// EmitAbbreviations - Emit the abbreviation section.
2397 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002398 void EmitAbbreviations() const {
2399 // Check to see if it is worth the effort.
2400 if (!Abbreviations.empty()) {
2401 // Start the debug abbrev section.
2402 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002403
Jim Laskeyef42a012006-11-02 20:12:39 +00002404 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002405
Jim Laskeyef42a012006-11-02 20:12:39 +00002406 // For each abbrevation.
2407 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2408 // Get abbreviation data
2409 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002410
Jim Laskeyef42a012006-11-02 20:12:39 +00002411 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002412 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2413 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002414
Jim Laskeyef42a012006-11-02 20:12:39 +00002415 // Emit the abbreviations data.
2416 Abbrev->Emit(*this);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002417
Jim Laskeybacd3042007-02-21 22:48:45 +00002418 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002419 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002420
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002421 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00002422 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002423
Jim Laskeyef42a012006-11-02 20:12:39 +00002424 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002425
Jim Laskeybacd3042007-02-21 22:48:45 +00002426 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002427 }
2428 }
2429
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002430 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2431 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002432 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002433 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2434 // Define last address of section.
2435 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2436 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2437 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2438 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2439
2440 // Mark end of matrix.
2441 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2442 Asm->EmitULEB128Bytes(1); Asm->EOL();
2443 Asm->EmitInt8(1); Asm->EOL();
2444 }
2445
Jim Laskey65195462006-10-30 13:35:07 +00002446 /// EmitDebugLines - Emit source line information.
2447 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002448 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002449 // If the target is using .loc/.file, the assembler will be emitting the
2450 // .debug_line table automatically.
2451 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00002452 return;
2453
Jim Laskeyef42a012006-11-02 20:12:39 +00002454 // Minimum line delta, thus ranging from -10..(255-10).
2455 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2456 // Maximum line delta, thus ranging from -10..(255-10).
2457 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002458
Jim Laskeyef42a012006-11-02 20:12:39 +00002459 // Start the dwarf line section.
2460 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002461
Jim Laskeyef42a012006-11-02 20:12:39 +00002462 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002463
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002464 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002465 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002466 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002467
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002468 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002469
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002470 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002471 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002472 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002473
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002474 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002475
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002476 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002477
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002478 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002479
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002480 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002481
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002482 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002483
Jim Laskeyef42a012006-11-02 20:12:39 +00002484 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002485 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2486 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2487 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2488 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2489 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2490 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2491 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2492 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2493 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00002494
Jim Laskeyef42a012006-11-02 20:12:39 +00002495 // Emit directories.
Evan Chenge3d42322009-02-25 07:04:34 +00002496 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2497 Asm->EmitString(getSourceDirectoryName(DI));
2498 Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00002499 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002500 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002501
Jim Laskeyef42a012006-11-02 20:12:39 +00002502 // Emit files.
Evan Chenge3d42322009-02-25 07:04:34 +00002503 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2504 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002505 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Evan Chenge3d42322009-02-25 07:04:34 +00002506 Asm->EmitString(getSourceFileName(Id.second));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002507 Asm->EOL("Source");
Evan Chenge3d42322009-02-25 07:04:34 +00002508 Asm->EmitULEB128Bytes(Id.first);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002509 Asm->EOL("Directory #");
2510 Asm->EmitULEB128Bytes(0);
2511 Asm->EOL("Mod date");
2512 Asm->EmitULEB128Bytes(0);
2513 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002514 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002515 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002516
Jim Laskeyef42a012006-11-02 20:12:39 +00002517 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002518
Jim Laskeyef42a012006-11-02 20:12:39 +00002519 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002520 unsigned SecSrcLinesSize = SectionSourceLines.size();
2521
2522 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002523 // Isolate current sections line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00002524 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00002525
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002526 if (VerboseAsm) {
2527 const Section* S = SectionMap[j + 1];
Evan Chenge3d42322009-02-25 07:04:34 +00002528 O << '\t' << TAI->getCommentString() << " Section"
2529 << S->getName() << '\n';
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002530 } else
Evan Cheng6547e402008-07-01 23:18:29 +00002531 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002532
2533 // Dwarf assumes we start with first line of first source file.
2534 unsigned Source = 1;
2535 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002536
Jim Laskeyef42a012006-11-02 20:12:39 +00002537 // Construct rows of the address, source, line, column matrix.
2538 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patelf3ee5142009-01-12 22:54:42 +00002539 const SrcLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002540 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00002541 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002542
Evan Chenge3d42322009-02-25 07:04:34 +00002543 if (!VerboseAsm)
Evan Cheng6547e402008-07-01 23:18:29 +00002544 Asm->EOL();
Evan Chenge3d42322009-02-25 07:04:34 +00002545 else {
2546 std::pair<unsigned, unsigned> SourceID =
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002547 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Evan Chenge3d42322009-02-25 07:04:34 +00002548 O << '\t' << TAI->getCommentString() << ' '
2549 << getSourceDirectoryName(SourceID.first) << ' '
2550 << getSourceFileName(SourceID.second)
2551 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2552 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002553
2554 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002555 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00002556 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002557 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00002558 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002559
Jim Laskeyef42a012006-11-02 20:12:39 +00002560 // If change of source, then switch to the new source.
2561 if (Source != LineInfo.getSourceID()) {
2562 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002563 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2564 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00002565 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002566
Jim Laskeyef42a012006-11-02 20:12:39 +00002567 // If change of line.
2568 if (Line != LineInfo.getLine()) {
2569 // Determine offset.
2570 int Offset = LineInfo.getLine() - Line;
2571 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002572
Jim Laskeyef42a012006-11-02 20:12:39 +00002573 // Update line.
2574 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002575
Jim Laskeyef42a012006-11-02 20:12:39 +00002576 // If delta is small enough and in range...
2577 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2578 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002579 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00002580 } else {
2581 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002582 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2583 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2584 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002585 }
2586 } else {
2587 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002588 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002589 }
2590 }
2591
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002592 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002593 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002594
2595 if (SecSrcLinesSize == 0)
2596 // Because we're emitting a debug_line section, we still need a line
2597 // table. The linker and friends expect it to exist. If there's nothing to
2598 // put into it, emit an empty table.
2599 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002600
Jim Laskeyef42a012006-11-02 20:12:39 +00002601 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002602
Jim Laskeybacd3042007-02-21 22:48:45 +00002603 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002604 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002605
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002606 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00002607 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002608 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002609 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00002610 return;
2611
2612 int stackGrowth =
2613 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2614 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002615 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00002616
2617 // Start the dwarf frame section.
2618 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2619
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002620 EmitLabel("debug_frame_common", 0);
2621 EmitDifference("debug_frame_common_end", 0,
2622 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002623 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002624
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002625 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002626 Asm->EmitInt32((int)DW_CIE_ID);
2627 Asm->EOL("CIE Identifier Tag");
2628 Asm->EmitInt8(DW_CIE_VERSION);
2629 Asm->EOL("CIE Version");
2630 Asm->EmitString("");
2631 Asm->EOL("CIE Augmentation");
2632 Asm->EmitULEB128Bytes(1);
2633 Asm->EOL("CIE Code Alignment Factor");
2634 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002635 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002636 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002637 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002638
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002639 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002640 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002641
Dale Johannesenb97aec62007-11-13 19:13:01 +00002642 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002643
Evan Cheng05548eb2008-02-29 19:36:59 +00002644 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002645 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002646
Jim Laskeybacd3042007-02-21 22:48:45 +00002647 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002648 }
2649
Jim Laskey65195462006-10-30 13:35:07 +00002650 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2651 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002652 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002653 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00002654 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002655
Jim Laskeyef42a012006-11-02 20:12:39 +00002656 // Start the dwarf frame section.
2657 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002658
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002659 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2660 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002661 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002662
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002663 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002664
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002665 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2666 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002667 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002668
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002669 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002670 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002671 EmitDifference("func_end", DebugFrameInfo.Number,
2672 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002673 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002674
Devang Patel5aac3d32009-01-17 08:01:33 +00002675 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00002676 false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002677
Evan Cheng05548eb2008-02-29 19:36:59 +00002678 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002679 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00002680
Jim Laskeybacd3042007-02-21 22:48:45 +00002681 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002682 }
2683
Evan Chenge3d42322009-02-25 07:04:34 +00002684 void EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2685 EmitDifference("pubnames_end", Unit->getID(),
2686 "pubnames_begin", Unit->getID(), true);
2687 Asm->EOL("Length of Public Names Info");
2688
2689 EmitLabel("pubnames_begin", Unit->getID());
2690
2691 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2692
2693 EmitSectionOffset("info_begin", "section_info",
2694 Unit->getID(), 0, true, false);
2695 Asm->EOL("Offset of Compilation Unit Info");
2696
2697 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2698 true);
2699 Asm->EOL("Compilation Unit Length");
2700
2701 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2702 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2703 GE = Globals.end(); GI != GE; ++GI) {
2704 const std::string &Name = GI->first;
2705 DIE * Entity = GI->second;
2706
2707 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2708 Asm->EmitString(Name); Asm->EOL("External Name");
2709 }
2710
2711 Asm->EmitInt32(0); Asm->EOL("End Mark");
2712 EmitLabel("pubnames_end", Unit->getID());
2713
2714 Asm->EOL();
2715 }
2716
Jim Laskeyef42a012006-11-02 20:12:39 +00002717 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002718 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002719 void EmitDebugPubNames() {
2720 // Start the dwarf pubnames section.
2721 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002722
Evan Chenge3d42322009-02-25 07:04:34 +00002723 if (MainCU) {
2724 EmitDebugPubNamesPerCU(MainCU);
2725 return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002726 }
Evan Chenge3d42322009-02-25 07:04:34 +00002727
2728 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2729 EmitDebugPubNamesPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002730 }
2731
2732 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002733 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002734 void EmitDebugStr() {
2735 // Check to see if it is worth the effort.
2736 if (!StringPool.empty()) {
2737 // Start the dwarf str section.
2738 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002739
Jim Laskeyef42a012006-11-02 20:12:39 +00002740 // For each of strings in the string pool.
2741 for (unsigned StringID = 1, N = StringPool.size();
2742 StringID <= N; ++StringID) {
2743 // Emit a label for reference from debug information entries.
2744 EmitLabel("string", StringID);
2745 // Emit the string itself.
2746 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00002747 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002748 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002749
Jim Laskeybacd3042007-02-21 22:48:45 +00002750 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002751 }
2752 }
2753
2754 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002755 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002756 void EmitDebugLoc() {
2757 // Start the dwarf loc section.
2758 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002759
Jim Laskeybacd3042007-02-21 22:48:45 +00002760 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002761 }
2762
2763 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002764 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002765 void EmitDebugARanges() {
2766 // Start the dwarf aranges section.
2767 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002768
Jim Laskeyef42a012006-11-02 20:12:39 +00002769 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00002770#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002771 CompileUnit *Unit = GetBaseCompileUnit();
2772
Jim Laskey5496f012006-11-09 14:52:14 +00002773 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002774 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002775
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002776 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002777
Jim Laskey5496f012006-11-09 14:52:14 +00002778 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002779 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002780
Dan Gohman82482942007-09-27 23:12:31 +00002781 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002782
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002783 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002784
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002785 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2786 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002787
Jim Laskey5496f012006-11-09 14:52:14 +00002788 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002789 EmitReference("text_begin", 0); Asm->EOL("Address");
2790 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002791
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002792 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2793 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00002794#endif
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002795
Jim Laskeybacd3042007-02-21 22:48:45 +00002796 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002797 }
2798
2799 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002800 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002801 void EmitDebugRanges() {
2802 // Start the dwarf ranges section.
2803 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002804
Jim Laskeybacd3042007-02-21 22:48:45 +00002805 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002806 }
2807
2808 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002809 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002810 void EmitDebugMacInfo() {
Scott Michel210de722009-01-26 22:32:51 +00002811 if (TAI->doesSupportMacInfoSection()) {
2812 // Start the dwarf macinfo section.
2813 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002814
Scott Michel210de722009-01-26 22:32:51 +00002815 Asm->EOL();
2816 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002817 }
2818
Bill Wendlingc8615e42009-03-10 21:47:45 +00002819 /// GetOrCreateSourceID - Look up the source id with the given directory and
2820 /// source file names. If none currently exists, create a new id and insert it
2821 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
2822 /// as well.
2823 unsigned GetOrCreateSourceID(const std::string &DirName,
2824 const std::string &FileName) {
2825 unsigned DId;
2826 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
2827 if (DI != DirectoryIdMap.end()) {
2828 DId = DI->getValue();
2829 } else {
2830 DId = DirectoryNames.size() + 1;
2831 DirectoryIdMap[DirName] = DId;
2832 DirectoryNames.push_back(DirName);
2833 }
2834
2835 unsigned FId;
2836 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
2837 if (FI != SourceFileIdMap.end()) {
2838 FId = FI->getValue();
2839 } else {
2840 FId = SourceFileNames.size() + 1;
2841 SourceFileIdMap[FileName] = FId;
2842 SourceFileNames.push_back(FileName);
2843 }
2844
2845 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
2846 SourceIdMap.find(std::make_pair(DId, FId));
2847 if (SI != SourceIdMap.end())
2848 return SI->second;
2849
2850 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
2851 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
2852 SourceIds.push_back(std::make_pair(DId, FId));
2853
2854 return SrcId;
2855 }
2856
Evan Chenge3d42322009-02-25 07:04:34 +00002857 void ConstructCompileUnit(GlobalVariable *GV) {
2858 DICompileUnit DIUnit(GV);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002859 std::string Dir, FN, Prod;
Bill Wendlingc8615e42009-03-10 21:47:45 +00002860 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002861 DIUnit.getFilename(FN));
Evan Chenge3d42322009-02-25 07:04:34 +00002862
2863 DIE *Die = new DIE(DW_TAG_compile_unit);
2864 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2865 DWLabel("section_line", 0), DWLabel("section_line", 0),
2866 false);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002867 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer(Prod));
Evan Chenge3d42322009-02-25 07:04:34 +00002868 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002869 AddString(Die, DW_AT_name, DW_FORM_string, FN);
2870 if (!Dir.empty())
2871 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Chenge3d42322009-02-25 07:04:34 +00002872 if (DIUnit.isOptimized())
2873 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002874 std::string Flags;
2875 DIUnit.getFlags(Flags);
Evan Chenge3d42322009-02-25 07:04:34 +00002876 if (!Flags.empty())
2877 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
2878 unsigned RVer = DIUnit.getRunTimeVersion();
2879 if (RVer)
2880 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
2881
2882 CompileUnit *Unit = new CompileUnit(ID, Die);
2883 if (DIUnit.isMain()) {
2884 assert(!MainCU && "Multiple main compile units are found!");
2885 MainCU = Unit;
2886 }
2887 CompileUnitMap[DIUnit.getGV()] = Unit;
2888 CompileUnits.push_back(Unit);
2889 }
2890
Devang Patelc4523242009-01-05 23:11:11 +00002891 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Pateld1ca9252009-01-05 23:03:32 +00002892 void ConstructCompileUnits() {
Evan Chenge3d42322009-02-25 07:04:34 +00002893 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.compile_units");
2894 if (!Root)
2895 return;
2896 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2897 "Malformed compile unit descriptor anchor type");
2898 Constant *RootC = cast<Constant>(*Root->use_begin());
2899 assert(RootC->hasNUsesOrMore(1) &&
2900 "Malformed compile unit descriptor anchor type");
2901 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2902 UI != UE; ++UI)
2903 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2904 UUI != UUE; ++UUI) {
2905 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2906 ConstructCompileUnit(GV);
Devang Pateldd9db662009-01-30 18:20:31 +00002907 }
Evan Chenge3d42322009-02-25 07:04:34 +00002908 }
2909
2910 bool ConstructGlobalVariableDIE(GlobalVariable *GV) {
2911 DIGlobalVariable DI_GV(GV);
2912 CompileUnit *DW_Unit = MainCU;
2913 if (!DW_Unit)
2914 DW_Unit = FindCompileUnit(DI_GV.getCompileUnit());
2915
2916 // Check for pre-existence.
2917 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
2918 if (Slot)
2919 return false;
2920
2921 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
2922
2923 // Add address.
2924 DIEBlock *Block = new DIEBlock();
2925 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
2926 AddObjectLabel(Block, 0, DW_FORM_udata,
2927 Asm->getGlobalLinkName(DI_GV.getGlobal()));
2928 AddBlock(VariableDie, DW_AT_location, 0, Block);
2929
2930 // Add to map.
2931 Slot = VariableDie;
2932 // Add to context owner.
2933 DW_Unit->getDie()->AddChild(VariableDie);
2934 // Expose as global. FIXME - need to check external flag.
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002935 std::string Name;
2936 DW_Unit->AddGlobal(DI_GV.getName(Name), VariableDie);
Evan Chenge3d42322009-02-25 07:04:34 +00002937 return true;
Devang Pateld1ca9252009-01-05 23:03:32 +00002938 }
2939
Devang Patelc4523242009-01-05 23:11:11 +00002940 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patel53cac182009-02-24 00:02:15 +00002941 /// visible global variables. Return true if at least one global DIE is
2942 /// created.
2943 bool ConstructGlobalVariableDIEs() {
Evan Chenge3d42322009-02-25 07:04:34 +00002944 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
2945 if (!Root)
2946 return false;
Devang Patelc4523242009-01-05 23:11:11 +00002947
Evan Chenge3d42322009-02-25 07:04:34 +00002948 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2949 "Malformed global variable descriptor anchor type");
2950 Constant *RootC = cast<Constant>(*Root->use_begin());
2951 assert(RootC->hasNUsesOrMore(1) &&
2952 "Malformed global variable descriptor anchor type");
Devang Patelc4523242009-01-05 23:11:11 +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 |= ConstructGlobalVariableDIE(GV);
2961 }
2962 return Result;
2963 }
Devang Patelc4523242009-01-05 23:11:11 +00002964
Evan Chenge3d42322009-02-25 07:04:34 +00002965 bool ConstructSubprogram(GlobalVariable *GV) {
2966 DISubprogram SP(GV);
2967 CompileUnit *Unit = MainCU;
2968 if (!Unit)
2969 Unit = FindCompileUnit(SP.getCompileUnit());
Devang Patelc4523242009-01-05 23:11:11 +00002970
Evan Chenge3d42322009-02-25 07:04:34 +00002971 // Check for pre-existence.
2972 DIE *&Slot = Unit->getDieMapSlotFor(GV);
2973 if (Slot)
2974 return false;
2975
2976 if (!SP.isDefinition())
2977 // This is a method declaration which will be handled while
2978 // constructing class type.
2979 return false;
2980
2981 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
2982
2983 // Add to map.
2984 Slot = SubprogramDie;
2985 // Add to context owner.
2986 Unit->getDie()->AddChild(SubprogramDie);
2987 // Expose as global.
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002988 std::string Name;
2989 Unit->AddGlobal(SP.getName(Name), SubprogramDie);
Evan Chenge3d42322009-02-25 07:04:34 +00002990 return true;
Devang Patelc4523242009-01-05 23:11:11 +00002991 }
2992
Devang Patel78eb6ad2009-01-05 23:21:35 +00002993 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patel53cac182009-02-24 00:02:15 +00002994 /// subprograms. Return true if at least one subprogram DIE is created.
2995 bool ConstructSubprograms() {
Evan Chenge3d42322009-02-25 07:04:34 +00002996 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
2997 if (!Root)
2998 return false;
Devang Patel78eb6ad2009-01-05 23:21:35 +00002999
Evan Chenge3d42322009-02-25 07:04:34 +00003000 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3001 "Malformed subprogram descriptor anchor type");
3002 Constant *RootC = cast<Constant>(*Root->use_begin());
3003 assert(RootC->hasNUsesOrMore(1) &&
3004 "Malformed subprogram descriptor anchor type");
Devang Patel78eb6ad2009-01-05 23:21:35 +00003005
Evan Chenge3d42322009-02-25 07:04:34 +00003006 bool Result = false;
3007 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3008 UI != UE; ++UI)
3009 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3010 UUI != UUE; ++UUI) {
3011 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3012 Result |= ConstructSubprogram(GV);
3013 }
3014 return Result;
Devang Patel78eb6ad2009-01-05 23:21:35 +00003015 }
3016
Jim Laskey65195462006-10-30 13:35:07 +00003017public:
Jim Laskeyef42a012006-11-02 20:12:39 +00003018 //===--------------------------------------------------------------------===//
3019 // Main entry points.
3020 //
Owen Andersoncb371882008-08-21 00:14:44 +00003021 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00003022 : Dwarf(OS, A, T, "dbg"), MainCU(0),
3023 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
3024 ValuesSet(InitValuesSetSize), Values(), StringPool(), SectionMap(),
3025 SectionSourceLines(), didInitial(false), shouldEmit(false),
3026 RootDbgScope(0), DebugTimer(0) {
3027 if (TimePassesIsEnabled)
3028 DebugTimer = new Timer("Dwarf Debug Writer",
3029 *getDwarfTimerGroup());
Jim Laskeyef42a012006-11-02 20:12:39 +00003030 }
Jim Laskey072200c2007-01-29 18:51:14 +00003031 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003032 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3033 delete Values[j];
Bill Wendling163ba3f2009-03-10 21:23:25 +00003034
3035 delete DebugTimer;
Jim Laskeyef42a012006-11-02 20:12:39 +00003036 }
3037
Bill Wendlingc8615e42009-03-10 21:47:45 +00003038 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
3039 /// be emitted.
3040 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
3041
Devang Patel5d315982009-01-06 21:07:30 +00003042 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3043 /// This is inovked by the target AsmPrinter.
Devang Patel3f7833a2009-01-12 23:09:42 +00003044 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003045 if (TimePassesIsEnabled)
3046 DebugTimer->startTimer();
3047
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003048 // Create all the compile unit DIEs.
3049 ConstructCompileUnits();
Devang Patel3f7833a2009-01-12 23:09:42 +00003050
Bill Wendling163ba3f2009-03-10 21:23:25 +00003051 if (CompileUnits.empty()) {
3052 if (TimePassesIsEnabled)
3053 DebugTimer->startTimer();
3054
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003055 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003056 }
Devang Patel3f7833a2009-01-12 23:09:42 +00003057
Devang Patel53cac182009-02-24 00:02:15 +00003058 // Create DIEs for each of the externally visible global variables.
3059 bool globalDIEs = ConstructGlobalVariableDIEs();
3060
3061 // Create DIEs for each of the externally visible subprograms.
3062 bool subprogramDIEs = ConstructSubprograms();
3063
3064 // If there is not any debug info available for any global variables
3065 // and any subprograms then there is not any debug info to emit.
Bill Wendling163ba3f2009-03-10 21:23:25 +00003066 if (!globalDIEs && !subprogramDIEs) {
3067 if (TimePassesIsEnabled)
3068 DebugTimer->startTimer();
3069
Devang Patel53cac182009-02-24 00:02:15 +00003070 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003071 }
Devang Patel53cac182009-02-24 00:02:15 +00003072
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003073 MMI = mmi;
3074 shouldEmit = true;
3075 MMI->setDebugInfoAvailability(true);
Devang Patel5d315982009-01-06 21:07:30 +00003076
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003077 // Prime section data.
3078 SectionMap.insert(TAI->getTextSection());
Devang Patel5d315982009-01-06 21:07:30 +00003079
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003080 // Print out .file directives to specify files for .loc directives. These
3081 // are printed out early so that they precede any .loc directives.
3082 if (TAI->hasDotLocAndDotFile()) {
Evan Chenge3d42322009-02-25 07:04:34 +00003083 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
3084 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00003085 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Evan Chenge3d42322009-02-25 07:04:34 +00003086 sys::Path FullPath(getSourceDirectoryName(Id.first));
3087 bool AppendOk =
3088 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003089 assert(AppendOk && "Could not append filename to directory!");
3090 AppendOk = false;
3091 Asm->EmitFile(i, FullPath.toString());
3092 Asm->EOL();
Devang Patel5d315982009-01-06 21:07:30 +00003093 }
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003094 }
Devang Patel5d315982009-01-06 21:07:30 +00003095
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003096 // Emit initial sections
3097 EmitInitial();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003098
3099 if (TimePassesIsEnabled)
3100 DebugTimer->stopTimer();
Devang Patel5d315982009-01-06 21:07:30 +00003101 }
3102
Jim Laskey65195462006-10-30 13:35:07 +00003103 /// BeginModule - Emit all Dwarf sections that should come prior to the
3104 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00003105 void BeginModule(Module *M) {
3106 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00003107 }
3108
Jim Laskey65195462006-10-30 13:35:07 +00003109 /// EndModule - Emit all Dwarf sections that should come after the content.
3110 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003111 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003112 if (!ShouldEmitDwarfDebug())
3113 return;
3114
3115 if (TimePassesIsEnabled)
3116 DebugTimer->startTimer();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003117
Jim Laskeyef42a012006-11-02 20:12:39 +00003118 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003119 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003120 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00003121 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003122 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003123
Jim Laskeyef42a012006-11-02 20:12:39 +00003124 // End text sections.
3125 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003126 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00003127 EmitLabel("section_end", i);
3128 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003129
3130 // Emit common frame information.
3131 EmitCommonDebugFrame();
3132
3133 // Emit function debug frame information
3134 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3135 E = DebugFrames.end(); I != E; ++I)
3136 EmitFunctionDebugFrame(*I);
3137
Jim Laskeyef42a012006-11-02 20:12:39 +00003138 // Compute DIE offsets and sizes.
3139 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003140
Jim Laskeyef42a012006-11-02 20:12:39 +00003141 // Emit all the DIEs into a debug info section
3142 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003143
Jim Laskeyef42a012006-11-02 20:12:39 +00003144 // Corresponding abbreviations into a abbrev section.
3145 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003146
Jim Laskeyef42a012006-11-02 20:12:39 +00003147 // Emit source line correspondence into a debug line section.
3148 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003149
Jim Laskeyef42a012006-11-02 20:12:39 +00003150 // Emit info into a debug pubnames section.
3151 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003152
Jim Laskeyef42a012006-11-02 20:12:39 +00003153 // Emit info into a debug str section.
3154 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003155
Jim Laskeyef42a012006-11-02 20:12:39 +00003156 // Emit info into a debug loc section.
3157 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003158
Jim Laskeyef42a012006-11-02 20:12:39 +00003159 // Emit info into a debug aranges section.
3160 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003161
Jim Laskeyef42a012006-11-02 20:12:39 +00003162 // Emit info into a debug ranges section.
3163 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003164
Jim Laskeyef42a012006-11-02 20:12:39 +00003165 // Emit info into a debug macinfo section.
3166 EmitDebugMacInfo();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003167
3168 if (TimePassesIsEnabled)
3169 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003170 }
3171
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003172 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00003173 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00003174 void BeginFunction(MachineFunction *MF) {
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003175 if (!ShouldEmitDwarfDebug()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00003176
Bill Wendling163ba3f2009-03-10 21:23:25 +00003177 if (TimePassesIsEnabled)
3178 DebugTimer->startTimer();
3179
3180 this->MF = MF;
3181
Jim Laskeyef42a012006-11-02 20:12:39 +00003182 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003183 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003184
Jim Laskeyef42a012006-11-02 20:12:39 +00003185 // Assumes in correct section after the entry point.
3186 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00003187
3188 // Emit label for the implicitly defined dbg.stoppoint at the start of
3189 // the function.
Devang Patelf3ee5142009-01-12 22:54:42 +00003190 if (!Lines.empty()) {
3191 const SrcLineInfo &LineInfo = Lines[0];
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00003192 Asm->printLabel(LineInfo.getLabelID());
3193 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003194
3195 if (TimePassesIsEnabled)
3196 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003197 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003198
Jim Laskey65195462006-10-30 13:35:07 +00003199 /// EndFunction - Gather and emit post-function debug information.
3200 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00003201 void EndFunction(MachineFunction *MF) {
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003202 if (!ShouldEmitDwarfDebug()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003203
Bill Wendling163ba3f2009-03-10 21:23:25 +00003204 if (TimePassesIsEnabled)
3205 DebugTimer->startTimer();
3206
Jim Laskeyb82313f2007-02-01 16:31:34 +00003207 // Define end label for subprogram.
3208 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003209
Jim Laskeyef42a012006-11-02 20:12:39 +00003210 // Get function line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00003211 if (!Lines.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003212 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003213 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00003214 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patelf3ee5142009-01-12 22:54:42 +00003215 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Jim Laskeyef42a012006-11-02 20:12:39 +00003216 // Append the function info to section info.
3217 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patelf3ee5142009-01-12 22:54:42 +00003218 Lines.begin(), Lines.end());
Jim Laskeyef42a012006-11-02 20:12:39 +00003219 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003220
Jim Laskeyef42a012006-11-02 20:12:39 +00003221 // Construct scopes for subprogram.
Devang Patel7bb89ed2009-01-13 00:20:51 +00003222 if (RootDbgScope)
3223 ConstructRootDbgScope(RootDbgScope);
Bill Wendlingd751c642008-09-26 00:28:12 +00003224 else
3225 // FIXME: This is wrong. We are essentially getting past a problem with
3226 // debug information not being able to handle unreachable blocks that have
3227 // debug information in them. In particular, those unreachable blocks that
3228 // have "region end" info in them. That situation results in the "root
3229 // scope" not being created. If that's the case, then emit a "default"
3230 // scope, i.e., one that encompasses the whole function. This isn't
3231 // desirable. And a better way of handling this (and all of the debugging
3232 // information) needs to be explored.
Devang Patel7bb89ed2009-01-13 00:20:51 +00003233 ConstructDefaultDbgScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003234
3235 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3236 MMI->getFrameMoves()));
Devang Patel481ff5b2009-01-12 18:48:36 +00003237
3238 // Clear debug info
3239 if (RootDbgScope) {
3240 delete RootDbgScope;
3241 DbgScopeMap.clear();
3242 RootDbgScope = NULL;
3243 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003244
Bill Wendling163ba3f2009-03-10 21:23:25 +00003245 Lines.clear();
3246
3247 if (TimePassesIsEnabled)
3248 DebugTimer->stopTimer();
3249 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003250
Devang Patelcf3a4482009-01-15 23:41:32 +00003251 /// ValidDebugInfo - Return true if V represents valid debug info value.
3252 bool ValidDebugInfo(Value *V) {
Devang Patelb79b5352009-01-19 23:21:49 +00003253 if (!V)
3254 return false;
3255
Devang Patel61c6bf32009-01-16 01:49:46 +00003256 if (!shouldEmit)
3257 return false;
3258
Devang Patelcf3a4482009-01-15 23:41:32 +00003259 GlobalVariable *GV = getGlobalVariable(V);
3260 if (!GV)
3261 return false;
Duncan Sands667d4b82009-03-07 15:45:40 +00003262
3263 if (!GV->hasInternalLinkage () && !GV->hasLinkOnceLinkage())
Devang Patelcf3a4482009-01-15 23:41:32 +00003264 return false;
3265
Bill Wendling163ba3f2009-03-10 21:23:25 +00003266 if (TimePassesIsEnabled)
3267 DebugTimer->startTimer();
3268
Devang Patelcf3a4482009-01-15 23:41:32 +00003269 DIDescriptor DI(GV);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003270
Devang Patelcf3a4482009-01-15 23:41:32 +00003271 // Check current version. Allow Version6 for now.
3272 unsigned Version = DI.getVersion();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003273 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6) {
3274 if (TimePassesIsEnabled)
3275 DebugTimer->stopTimer();
3276
Devang Patelcf3a4482009-01-15 23:41:32 +00003277 return false;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003278 }
Devang Patelcf3a4482009-01-15 23:41:32 +00003279
Devang Patelb79b5352009-01-19 23:21:49 +00003280 unsigned Tag = DI.getTag();
3281 switch (Tag) {
3282 case DW_TAG_variable:
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003283 assert(DIVariable(GV).Verify() && "Invalid DebugInfo value");
Devang Patelb79b5352009-01-19 23:21:49 +00003284 break;
3285 case DW_TAG_compile_unit:
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003286 assert(DICompileUnit(GV).Verify() && "Invalid DebugInfo value");
Devang Patelb79b5352009-01-19 23:21:49 +00003287 break;
3288 case DW_TAG_subprogram:
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003289 assert(DISubprogram(GV).Verify() && "Invalid DebugInfo value");
Devang Patelb79b5352009-01-19 23:21:49 +00003290 break;
3291 default:
3292 break;
3293 }
3294
Bill Wendling163ba3f2009-03-10 21:23:25 +00003295 if (TimePassesIsEnabled)
3296 DebugTimer->stopTimer();
3297
Devang Patelcf3a4482009-01-15 23:41:32 +00003298 return true;
3299 }
3300
Devang Patelccca7fe2009-01-12 19:17:34 +00003301 /// RecordSourceLine - Records location information and associates it with a
3302 /// label. Returns a unique label ID used to generate a label and provide
3303 /// correspondence to the source line list.
3304 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003305 if (TimePassesIsEnabled)
3306 DebugTimer->startTimer();
3307
Evan Chenge3d42322009-02-25 07:04:34 +00003308 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003309 assert(Unit && "Unable to find CompileUnit");
Devang Patelccca7fe2009-01-12 19:17:34 +00003310 unsigned ID = MMI->NextLabelID();
3311 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003312
3313 if (TimePassesIsEnabled)
3314 DebugTimer->stopTimer();
3315
Devang Patelccca7fe2009-01-12 19:17:34 +00003316 return ID;
3317 }
3318
3319 /// RecordSourceLine - Records location information and associates it with a
3320 /// label. Returns a unique label ID used to generate a label and provide
3321 /// correspondence to the source line list.
3322 unsigned RecordSourceLine(unsigned Line, unsigned Col, unsigned Src) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003323 if (TimePassesIsEnabled)
3324 DebugTimer->startTimer();
3325
Devang Patelccca7fe2009-01-12 19:17:34 +00003326 unsigned ID = MMI->NextLabelID();
3327 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003328
3329 if (TimePassesIsEnabled)
3330 DebugTimer->stopTimer();
3331
Devang Patelccca7fe2009-01-12 19:17:34 +00003332 return ID;
3333 }
3334
Bill Wendlingc8615e42009-03-10 21:47:45 +00003335 /// getRecordSourceLineCount - Return the number of source lines in the debug
3336 /// info.
3337 unsigned getRecordSourceLineCount() const {
Devang Patelccca7fe2009-01-12 19:17:34 +00003338 return Lines.size();
3339 }
3340
Bill Wendlingc8615e42009-03-10 21:47:45 +00003341 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
3342 /// timed. Look up the source id with the given directory and source file
3343 /// names. If none currently exists, create a new id and insert it in the
3344 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
3345 /// well.
Evan Chenge3d42322009-02-25 07:04:34 +00003346 unsigned getOrCreateSourceID(const std::string &DirName,
3347 const std::string &FileName) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003348 if (TimePassesIsEnabled)
3349 DebugTimer->startTimer();
3350
Bill Wendlingc8615e42009-03-10 21:47:45 +00003351 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003352
3353 if (TimePassesIsEnabled)
3354 DebugTimer->stopTimer();
3355
Evan Chenge3d42322009-02-25 07:04:34 +00003356 return SrcId;
Devang Patelccca7fe2009-01-12 19:17:34 +00003357 }
3358
3359 /// RecordRegionStart - Indicate the start of a region.
Devang Patelccca7fe2009-01-12 19:17:34 +00003360 unsigned RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003361 if (TimePassesIsEnabled)
3362 DebugTimer->startTimer();
3363
Devang Patelccca7fe2009-01-12 19:17:34 +00003364 DbgScope *Scope = getOrCreateScope(V);
3365 unsigned ID = MMI->NextLabelID();
3366 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003367
3368 if (TimePassesIsEnabled)
3369 DebugTimer->stopTimer();
3370
Devang Patelccca7fe2009-01-12 19:17:34 +00003371 return ID;
3372 }
3373
3374 /// RecordRegionEnd - Indicate the end of a region.
Devang Patelccca7fe2009-01-12 19:17:34 +00003375 unsigned RecordRegionEnd(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003376 if (TimePassesIsEnabled)
3377 DebugTimer->startTimer();
3378
Devang Patelccca7fe2009-01-12 19:17:34 +00003379 DbgScope *Scope = getOrCreateScope(V);
3380 unsigned ID = MMI->NextLabelID();
3381 Scope->setEndLabelID(ID);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003382
3383 if (TimePassesIsEnabled)
3384 DebugTimer->stopTimer();
3385
Devang Patelccca7fe2009-01-12 19:17:34 +00003386 return ID;
3387 }
3388
3389 /// RecordVariable - Indicate the declaration of a local variable.
Devang Patelccca7fe2009-01-12 19:17:34 +00003390 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003391 if (TimePassesIsEnabled)
3392 DebugTimer->startTimer();
3393
Devang Patel0e5200f2009-01-15 18:25:17 +00003394 DIDescriptor Desc(GV);
3395 DbgScope *Scope = NULL;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003396
Devang Patel0e5200f2009-01-15 18:25:17 +00003397 if (Desc.getTag() == DW_TAG_variable) {
3398 // GV is a global variable.
3399 DIGlobalVariable DG(GV);
3400 Scope = getOrCreateScope(DG.getContext().getGV());
3401 } else {
3402 // or GV is a local variable.
3403 DIVariable DV(GV);
3404 Scope = getOrCreateScope(DV.getContext().getGV());
3405 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003406
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003407 assert(Scope && "Unable to find variable' scope");
Devang Patel99ec3532009-01-16 19:28:14 +00003408 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelccca7fe2009-01-12 19:17:34 +00003409 Scope->AddVariable(DV);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003410
3411 if (TimePassesIsEnabled)
3412 DebugTimer->stopTimer();
Devang Patelccca7fe2009-01-12 19:17:34 +00003413 }
Jim Laskey65195462006-10-30 13:35:07 +00003414};
3415
Jim Laskey072200c2007-01-29 18:51:14 +00003416//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003417/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00003418///
3419class DwarfException : public Dwarf {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003420 struct FunctionEHFrameInfo {
3421 std::string FnName;
3422 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003423 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003424 bool hasCalls;
3425 bool hasLandingPads;
3426 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003427 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00003428
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003429 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3430 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003431 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003432 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003433 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003434 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003435 };
3436
3437 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003438
3439 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3440 /// be emitted.
3441 bool shouldEmitTable;
3442
3443 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3444 /// should be emitted.
3445 bool shouldEmitMoves;
3446
3447 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3448 /// should be emitted.
3449 bool shouldEmitTableModule;
3450
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003451 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003452 /// should be emitted.
3453 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00003454
Bill Wendling163ba3f2009-03-10 21:23:25 +00003455 /// ExceptionTimer - Timer for the Dwarf exception writer.
3456 Timer *ExceptionTimer;
3457
Jim Laskey3f09fc22007-02-28 18:38:31 +00003458 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3459 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003460 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003461 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003462 int stackGrowth =
3463 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3464 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003465 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003466
Jim Laskeybacd3042007-02-21 22:48:45 +00003467 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003468 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling722f5f12008-12-24 08:05:17 +00003469
3470 if (!TAI->doesRequireNonLocalEHFrameLabel())
3471 O << TAI->getEHGlobalPrefix();
3472 O << "EH_frame" << Index << ":\n";
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003473 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003474
Jim Laskeybacd3042007-02-21 22:48:45 +00003475 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003476 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003477
Jim Laskeybacd3042007-02-21 22:48:45 +00003478 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003479 EmitDifference("eh_frame_common_end", Index,
3480 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003481 Asm->EOL("Length of Common Information Entry");
3482
Jim Laskeybacd3042007-02-21 22:48:45 +00003483 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003484 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003485 Asm->EmitInt32((int)0);
3486 Asm->EOL("CIE Identifier Tag");
3487 Asm->EmitInt8(DW_CIE_VERSION);
3488 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00003489
Jim Laskeybacd3042007-02-21 22:48:45 +00003490 // The personality presence indicates that language specific information
3491 // will show up in the eh frame.
3492 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00003493 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00003494
Jim Laskeybacd3042007-02-21 22:48:45 +00003495 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003496 Asm->EmitULEB128Bytes(1);
3497 Asm->EOL("CIE Code Alignment Factor");
3498 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00003499 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003500 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00003501 Asm->EOL("CIE Return Address Column");
3502
Jim Laskeybacd3042007-02-21 22:48:45 +00003503 // If there is a personality, we need to indicate the functions location.
3504 if (Personality) {
3505 Asm->EmitULEB128Bytes(7);
3506 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00003507
Duncan Sands671fa972008-05-07 19:11:09 +00003508 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003509 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00003510 Asm->EOL("Personality (pcrel sdata4 indirect)");
3511 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003512 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00003513 Asm->EOL("Personality (pcrel sdata4)");
3514 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00003515
Duncan Sands671fa972008-05-07 19:11:09 +00003516 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00003517 O << TAI->getPersonalityPrefix();
3518 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3519 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00003520 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3521 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00003522 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00003523
Duncan Sands671fa972008-05-07 19:11:09 +00003524 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3525 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003526
Bill Wendlingd60de512009-01-05 22:53:45 +00003527 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3528 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003529 } else {
3530 Asm->EmitULEB128Bytes(1);
3531 Asm->EOL("Augmentation Size");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003532
Bill Wendlingd60de512009-01-05 22:53:45 +00003533 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3534 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003535 }
3536
3537 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003538 std::vector<MachineMove> Moves;
3539 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00003540 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003541
Dale Johannesen21d972a2008-04-30 00:43:29 +00003542 // On Darwin the linker honors the alignment of eh_frame, which means it
3543 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3544 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003545 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003546 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003547 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003548
Jim Laskeybacd3042007-02-21 22:48:45 +00003549 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003550 }
Duncan Sands671fa972008-05-07 19:11:09 +00003551
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003552 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003553 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003554 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003555 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3556
Jim Laskeybacd3042007-02-21 22:48:45 +00003557 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3558
3559 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003560 // If the corresponding function is static, this should not be
3561 // externally visible.
Rafael Espindolabb46f522009-01-15 20:18:42 +00003562 if (linkage != Function::InternalLinkage &&
Devang Patel2d1768c2009-01-17 08:05:14 +00003563 linkage != Function::PrivateLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003564 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3565 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3566 }
3567
Dale Johannesen038129d2008-01-10 02:03:30 +00003568 // If corresponding function is weak definition, this should be too.
Duncan Sands667d4b82009-03-07 15:45:40 +00003569 if ((linkage == Function::WeakAnyLinkage ||
3570 linkage == Function::WeakODRLinkage ||
3571 linkage == Function::LinkOnceAnyLinkage ||
3572 linkage == Function::LinkOnceODRLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00003573 TAI->getWeakDefDirective())
3574 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3575
3576 // If there are no calls then you can't unwind. This may mean we can
3577 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003578 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00003579 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00003580 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00003581 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00003582 !UnwindTablesMandatory &&
Duncan Sands667d4b82009-03-07 15:45:40 +00003583 ((linkage != Function::WeakAnyLinkage &&
3584 linkage != Function::WeakODRLinkage &&
3585 linkage != Function::LinkOnceAnyLinkage &&
3586 linkage != Function::LinkOnceODRLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00003587 !TAI->getWeakDefDirective() ||
3588 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003589 {
Bill Wendling6e198962007-09-18 01:47:22 +00003590 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003591 // This name has no connection to the function, so it might get
3592 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003593 // dead-stripping unconditionally.
3594 if (const char *UsedDirective = TAI->getUsedDirective())
3595 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00003596 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00003597 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003598
Jim Laskeybacd3042007-02-21 22:48:45 +00003599 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003600 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3601 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003602 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003603
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003604 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003605
Bill Wendling722f5f12008-12-24 08:05:17 +00003606 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3607 PrintRelDirective(true, true);
3608 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3609
3610 if (!TAI->isAbsoluteEHSectionOffsets())
3611 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3612 } else {
3613 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3614 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3615 true, true, false);
3616 }
3617
Jim Laskeybacd3042007-02-21 22:48:45 +00003618 Asm->EOL("FDE CIE offset");
3619
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003620 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003621 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003622 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003623 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003624 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00003625
Jim Laskey3f09fc22007-02-28 18:38:31 +00003626 // If there is a personality and landing pads then point to the language
3627 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003628 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00003629 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00003630 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00003631
3632 if (EHFrameInfo.hasLandingPads)
3633 EmitReference("exception", EHFrameInfo.Number, true, true);
3634 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00003635 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00003636 Asm->EOL("Language Specific Data Area");
3637 } else {
3638 Asm->EmitULEB128Bytes(0);
3639 Asm->EOL("Augmentation size");
3640 }
Duncan Sands671fa972008-05-07 19:11:09 +00003641
Jim Laskeybacd3042007-02-21 22:48:45 +00003642 // Indicate locations of function specific callee saved registers in
3643 // frame.
Devang Patel5aac3d32009-01-17 08:01:33 +00003644 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00003645 true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003646
Dale Johannesen21d972a2008-04-30 00:43:29 +00003647 // On Darwin the linker honors the alignment of eh_frame, which means it
3648 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3649 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003650 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003651 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003652 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003653
3654 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003655 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003656 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003657 // that depends on unused functions (calling undefined externals) being
3658 // dead-stripped to link correctly. Yes, there really is.
3659 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3660 if (const char *UsedDirective = TAI->getUsedDirective())
3661 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3662 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003663 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003664
Duncan Sands57810cd2007-09-05 11:27:52 +00003665 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003666 ///
3667 /// The general organization of the table is complex, but the basic concepts
3668 /// are easy. First there is a header which describes the location and
3669 /// organization of the three components that follow.
3670 /// 1. The landing pad site information describes the range of code covered
3671 /// by the try. In our case it's an accumulation of the ranges covered
3672 /// by the invokes in the try. There is also a reference to the landing
3673 /// pad that handles the exception once processed. Finally an index into
3674 /// the actions table.
3675 /// 2. The action table, in our case, is composed of pairs of type ids
3676 /// and next action offset. Starting with the action index from the
3677 /// landing pad site, each type Id is checked for a match to the current
3678 /// exception. If it matches then the exception and type id are passed
3679 /// on to the landing pad. Otherwise the next action is looked up. This
3680 /// chain is terminated with a next action of zero. If no type id is
3681 /// found the the frame is unwound and handling continues.
3682 /// 3. Type id table contains references to all the C++ typeinfo for all
3683 /// catches in the function. This tables is reversed indexed base 1.
3684
Duncan Sandsb32edb42007-06-06 15:37:31 +00003685 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3686 static unsigned SharedTypeIds(const LandingPadInfo *L,
3687 const LandingPadInfo *R) {
3688 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003689 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003690 unsigned MinSize = LSize < RSize ? LSize : RSize;
3691 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003692
Duncan Sandsb32edb42007-06-06 15:37:31 +00003693 for (; Count != MinSize; ++Count)
3694 if (LIds[Count] != RIds[Count])
3695 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003696
Duncan Sandsb32edb42007-06-06 15:37:31 +00003697 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003698 }
3699
Duncan Sandsb32edb42007-06-06 15:37:31 +00003700 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00003701 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003702 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003703 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003704 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003705
Duncan Sandsb32edb42007-06-06 15:37:31 +00003706 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00003707 if (LIds[i] != RIds[i])
3708 return LIds[i] < RIds[i];
3709
Duncan Sandsb32edb42007-06-06 15:37:31 +00003710 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003711 }
3712
Duncan Sands53c3a332007-05-16 12:12:23 +00003713 struct KeyInfo {
3714 static inline unsigned getEmptyKey() { return -1U; }
3715 static inline unsigned getTombstoneKey() { return -2U; }
3716 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00003717 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00003718 static bool isPod() { return true; }
3719 };
3720
Duncan Sands57810cd2007-09-05 11:27:52 +00003721 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003722 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003723 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003724 int NextAction;
3725 struct ActionEntry *Previous;
3726 };
3727
Duncan Sands57810cd2007-09-05 11:27:52 +00003728 /// PadRange - Structure holding a try-range and the associated landing pad.
3729 struct PadRange {
3730 // The index of the landing pad.
3731 unsigned PadIndex;
3732 // The index of the begin and end labels in the landing pad's label lists.
3733 unsigned RangeIndex;
3734 };
3735
3736 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3737
3738 /// CallSiteEntry - Structure describing an entry in the call-site table.
3739 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00003740 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003741 unsigned BeginLabel; // zero indicates the start of the function.
3742 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00003743 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003744 unsigned PadLabel; // zero indicates that there is no landing pad.
3745 unsigned Action;
3746 };
3747
Jim Laskeybacd3042007-02-21 22:48:45 +00003748 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00003749 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00003750 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00003751 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3752 if (PadInfos.empty()) return;
3753
3754 // Sort the landing pads in order of their type ids. This is used to fold
3755 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00003756 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00003757 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00003758 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00003759 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003760 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3761
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003762 // Negative type ids index into FilterIds, positive type ids index into
3763 // TypeInfos. The value written for a positive type id is just the type
3764 // id itself. For a negative type id, however, the value written is the
3765 // (negative) byte offset of the corresponding FilterIds entry. The byte
3766 // offset is usually equal to the type id, because the FilterIds entries
3767 // are written using a variable width encoding which outputs one byte per
3768 // entry as long as the value written is not too large, but can differ.
3769 // This kind of complication does not occur for positive type ids because
3770 // type infos are output using a fixed width encoding.
3771 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3772 SmallVector<int, 16> FilterOffsets;
3773 FilterOffsets.reserve(FilterIds.size());
3774 int Offset = -1;
3775 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3776 E = FilterIds.end(); I != E; ++I) {
3777 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003778 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003779 }
3780
Duncan Sands57810cd2007-09-05 11:27:52 +00003781 // Compute the actions table and gather the first action index for each
3782 // landing pad site.
3783 SmallVector<ActionEntry, 32> Actions;
3784 SmallVector<unsigned, 64> FirstActions;
3785 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00003786
Duncan Sandsb32edb42007-06-06 15:37:31 +00003787 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00003788 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003789 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003790 const LandingPadInfo *LP = LandingPads[i];
3791 const std::vector<int> &TypeIds = LP->TypeIds;
3792 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003793 unsigned SizeSiteActions = 0;
3794
Duncan Sandsb32edb42007-06-06 15:37:31 +00003795 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00003796 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00003797 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003798
Duncan Sandsb32edb42007-06-06 15:37:31 +00003799 if (NumShared) {
3800 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3801 assert(Actions.size());
3802 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003803 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3804 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003805 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003806 SizeAction -=
3807 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003808 SizeAction += -PrevAction->NextAction;
3809 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003810 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00003811 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003812
Duncan Sandsb32edb42007-06-06 15:37:31 +00003813 // Compute the actions.
3814 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3815 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003816 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3817 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003818 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003819
3820 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003821 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003822 SizeSiteActions += SizeAction;
3823
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003824 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00003825 Actions.push_back(Action);
3826
3827 PrevAction = &Actions.back();
3828 }
3829
3830 // Record the first action of the landing pad site.
3831 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3832 } // else identical - re-use previous FirstAction
3833
3834 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003835
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00003836 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00003837 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00003838 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003839
Duncan Sands481dc722007-12-19 07:36:31 +00003840 // Compute the call-site table. The entry for an invoke has a try-range
3841 // containing the call, a non-zero landing pad and an appropriate action.
3842 // The entry for an ordinary call has a try-range containing the call and
3843 // zero for the landing pad and the action. Calls marked 'nounwind' have
3844 // no entry and must not be contained in the try-range of any entry - they
3845 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003846 SmallVector<CallSiteEntry, 64> CallSites;
3847
3848 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00003849 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3850 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3851 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00003852 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3853 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00003854 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003855 unsigned BeginLabel = LandingPad->BeginLabels[j];
3856 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3857 PadRange P = { i, j };
3858 PadMap[BeginLabel] = P;
3859 }
3860 }
3861
Duncan Sands481dc722007-12-19 07:36:31 +00003862 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00003863 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00003864
3865 // Whether there is a potentially throwing instruction (currently this means
3866 // an ordinary call) between the end of the previous try-range and now.
3867 bool SawPotentiallyThrowing = false;
3868
3869 // Whether the last callsite entry was for an invoke.
3870 bool PreviousIsInvoke = false;
3871
Duncan Sands481dc722007-12-19 07:36:31 +00003872 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003873 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3874 I != E; ++I) {
3875 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3876 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00003877 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00003878 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00003879 continue;
3880 }
3881
Chris Lattner9e330492007-12-30 20:50:28 +00003882 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00003883 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00003884
Duncan Sands481dc722007-12-19 07:36:31 +00003885 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00003886 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00003887 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003888
Duncan Sands481dc722007-12-19 07:36:31 +00003889 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00003890 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00003891 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00003892 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00003893 continue;
3894
3895 PadRange P = L->second;
3896 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3897
3898 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3899 "Inconsistent landing pad map!");
3900
3901 // If some instruction between the previous try-range and this one may
3902 // throw, create a call-site entry with no landing pad for the region
3903 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00003904 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003905 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3906 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00003907 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003908 }
3909
3910 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00003911 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00003912
Duncan Sands481dc722007-12-19 07:36:31 +00003913 if (LandingPad->LandingPadLabel) {
3914 // This try-range is for an invoke.
3915 CallSiteEntry Site = {BeginLabel, LastLabel,
3916 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00003917
Duncan Sands481dc722007-12-19 07:36:31 +00003918 // Try to merge with the previous call-site.
3919 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00003920 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00003921 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3922 // Extend the range of the previous entry.
3923 Prev.EndLabel = Site.EndLabel;
3924 continue;
3925 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003926 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003927
Duncan Sands481dc722007-12-19 07:36:31 +00003928 // Otherwise, create a new call-site.
3929 CallSites.push_back(Site);
3930 PreviousIsInvoke = true;
3931 } else {
3932 // Create a gap.
3933 PreviousIsInvoke = false;
3934 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003935 }
3936 }
3937 // If some instruction between the previous try-range and the end of the
3938 // function may throw, create a call-site entry with no landing pad for the
3939 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00003940 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003941 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3942 CallSites.push_back(Site);
3943 }
3944
Jim Laskeybacd3042007-02-21 22:48:45 +00003945 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00003946
3947 // Call sites.
3948 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
3949 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
3950 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
3951 unsigned SizeSites = CallSites.size() * (SiteStartSize +
3952 SiteLengthSize +
3953 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00003954 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003955 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00003956
Duncan Sands671fa972008-05-07 19:11:09 +00003957 // Type infos.
3958 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3959 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003960
3961 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003962 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003963 SizeSites + SizeActions + SizeTypes;
3964
3965 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3966 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003967 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003968 TypeOffset;
3969
3970 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003971
Jim Laskeybacd3042007-02-21 22:48:45 +00003972 // Begin the exception table.
3973 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng05548eb2008-02-29 19:36:59 +00003974 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesend65b2642008-10-08 21:50:21 +00003975 O << "GCC_except_table" << SubprogramCount << ":\n";
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003976 for (unsigned i = 0; i != SizeAlign; ++i) {
3977 Asm->EmitInt8(0);
3978 Asm->EOL("Padding");
3979 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003980 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003981
Jim Laskeybacd3042007-02-21 22:48:45 +00003982 // Emit the header.
3983 Asm->EmitInt8(DW_EH_PE_omit);
3984 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3985 Asm->EmitInt8(DW_EH_PE_absptr);
3986 Asm->EOL("TType format (DW_EH_PE_absptr)");
3987 Asm->EmitULEB128Bytes(TypeOffset);
3988 Asm->EOL("TType base offset");
3989 Asm->EmitInt8(DW_EH_PE_udata4);
3990 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3991 Asm->EmitULEB128Bytes(SizeSites);
3992 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00003993
Duncan Sands57810cd2007-09-05 11:27:52 +00003994 // Emit the landing pad site information.
3995 for (unsigned i = 0; i < CallSites.size(); ++i) {
3996 CallSiteEntry &S = CallSites[i];
3997 const char *BeginTag;
3998 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00003999
Duncan Sands57810cd2007-09-05 11:27:52 +00004000 if (!S.BeginLabel) {
4001 BeginTag = "eh_func_begin";
4002 BeginNumber = SubprogramCount;
4003 } else {
4004 BeginTag = "label";
4005 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00004006 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004007
Duncan Sands57810cd2007-09-05 11:27:52 +00004008 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004009 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004010 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00004011
Duncan Sands57810cd2007-09-05 11:27:52 +00004012 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00004013 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00004014 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004015 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00004016 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004017 }
4018 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004019
Duncan Sands671fa972008-05-07 19:11:09 +00004020 if (!S.PadLabel)
4021 Asm->EmitInt32(0);
4022 else
Duncan Sands57810cd2007-09-05 11:27:52 +00004023 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004024 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004025 Asm->EOL("Landing pad");
4026
4027 Asm->EmitULEB128Bytes(S.Action);
4028 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004029 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004030
Jim Laskeybacd3042007-02-21 22:48:45 +00004031 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004032 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4033 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00004034
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004035 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004036 Asm->EOL("TypeInfo index");
4037 Asm->EmitSLEB128Bytes(Action.NextAction);
4038 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004039 }
4040
4041 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00004042 for (unsigned M = TypeInfos.size(); M; --M) {
4043 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00004044
4045 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00004046
4047 if (GV)
4048 O << Asm->getGlobalLinkName(GV);
4049 else
4050 O << "0";
Duncan Sands57810cd2007-09-05 11:27:52 +00004051
Jim Laskeybacd3042007-02-21 22:48:45 +00004052 Asm->EOL("TypeInfo");
4053 }
4054
Duncan Sands73ef58a2007-06-02 16:53:42 +00004055 // Emit the filter typeids.
4056 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4057 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00004058 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00004059 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00004060 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004061
Evan Cheng05548eb2008-02-29 19:36:59 +00004062 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004063 }
4064
Jim Laskey072200c2007-01-29 18:51:14 +00004065public:
4066 //===--------------------------------------------------------------------===//
4067 // Main entry points.
4068 //
Owen Andersoncb371882008-08-21 00:14:44 +00004069 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00004070 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
4071 shouldEmitTableModule(false), shouldEmitMovesModule(false),
4072 ExceptionTimer(0) {
4073 if (TimePassesIsEnabled)
4074 ExceptionTimer = new Timer("Dwarf Exception Writer",
4075 *getDwarfTimerGroup());
4076 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004077
Bill Wendling163ba3f2009-03-10 21:23:25 +00004078 virtual ~DwarfException() {
4079 delete ExceptionTimer;
4080 }
Jim Laskey072200c2007-01-29 18:51:14 +00004081
4082 /// SetModuleInfo - Set machine module information when it's known that pass
4083 /// manager has created it. Set by the target AsmPrinter.
4084 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00004085 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00004086 }
4087
4088 /// BeginModule - Emit all exception information that should come prior to the
4089 /// content.
4090 void BeginModule(Module *M) {
4091 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00004092 }
4093
4094 /// EndModule - Emit all exception information that should come after the
4095 /// content.
4096 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004097 if (TimePassesIsEnabled)
4098 ExceptionTimer->startTimer();
4099
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004100 if (shouldEmitMovesModule || shouldEmitTableModule) {
4101 const std::vector<Function *> Personalities = MMI->getPersonalities();
Evan Chenge3d42322009-02-25 07:04:34 +00004102 for (unsigned i = 0; i < Personalities.size(); ++i)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004103 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00004104
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004105 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4106 E = EHFrames.end(); I != E; ++I)
4107 EmitEHFrame(*I);
4108 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004109
4110 if (TimePassesIsEnabled)
4111 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004112 }
4113
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004114 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00004115 /// emitted immediately after the function entry point.
4116 void BeginFunction(MachineFunction *MF) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004117 if (TimePassesIsEnabled)
4118 ExceptionTimer->startTimer();
4119
Jim Laskey072200c2007-01-29 18:51:14 +00004120 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004121 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004122
Bill Wendling163ba3f2009-03-10 21:23:25 +00004123 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004124 // Map all labels and get rid of any dead landing pads.
4125 MMI->TidyLandingPads();
Bill Wendling163ba3f2009-03-10 21:23:25 +00004126
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004127 // If any landing pads survive, we need an EH table.
4128 if (MMI->getLandingPads().size())
4129 shouldEmitTable = true;
4130
4131 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00004132 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004133 shouldEmitMoves = true;
4134
4135 if (shouldEmitMoves || shouldEmitTable)
4136 // Assumes in correct section after the entry point.
4137 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00004138 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004139
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004140 shouldEmitTableModule |= shouldEmitTable;
4141 shouldEmitMovesModule |= shouldEmitMoves;
Bill Wendling163ba3f2009-03-10 21:23:25 +00004142
4143 if (TimePassesIsEnabled)
4144 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004145 }
4146
4147 /// EndFunction - Gather and emit post-function exception information.
4148 ///
4149 void EndFunction() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004150 if (TimePassesIsEnabled)
4151 ExceptionTimer->startTimer();
4152
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004153 if (shouldEmitMoves || shouldEmitTable) {
4154 EmitLabel("eh_func_end", SubprogramCount);
4155 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00004156
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004157 // Save EH frame information
4158 EHFrames.
4159 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendling163ba3f2009-03-10 21:23:25 +00004160 SubprogramCount,
4161 MMI->getPersonalityIndex(),
4162 MF->getFrameInfo()->hasCalls(),
4163 !MMI->getLandingPads().empty(),
4164 MMI->getFrameMoves(),
4165 MF->getFunction()));
4166 }
4167
4168 if (TimePassesIsEnabled)
4169 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004170 }
4171};
4172
Jim Laskey0d086af2006-02-27 12:43:29 +00004173} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00004174
4175//===----------------------------------------------------------------------===//
4176
Jim Laskeyd18e2892006-01-20 20:34:06 +00004177/// Emit - Print the abbreviation using the specified Dwarf writer.
4178///
Jim Laskey072200c2007-01-29 18:51:14 +00004179void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004180 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00004181 DD.getAsm()->EmitULEB128Bytes(Tag);
4182 DD.getAsm()->EOL(TagString(Tag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004183
Jim Laskeyd18e2892006-01-20 20:34:06 +00004184 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00004185 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4186 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004187
Jim Laskeyd18e2892006-01-20 20:34:06 +00004188 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00004189 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004190 const DIEAbbrevData &AttrData = Data[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004191
Jim Laskeyd18e2892006-01-20 20:34:06 +00004192 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00004193 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4194 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004195
Jim Laskeyd18e2892006-01-20 20:34:06 +00004196 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00004197 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4198 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00004199 }
4200
4201 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00004202 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4203 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00004204}
4205
4206#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00004207void DIEAbbrev::print(std::ostream &O) {
4208 O << "Abbreviation @"
4209 << std::hex << (intptr_t)this << std::dec
4210 << " "
4211 << TagString(Tag)
4212 << " "
4213 << ChildrenString(ChildrenFlag)
4214 << "\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004215
Jim Laskeya0f3d172006-09-07 22:06:40 +00004216 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4217 O << " "
4218 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004219 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00004220 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004221 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00004222 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00004223}
Bill Wendlinge8156192006-12-07 01:30:32 +00004224void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00004225#endif
4226
4227//===----------------------------------------------------------------------===//
4228
Jim Laskeyef42a012006-11-02 20:12:39 +00004229#ifndef NDEBUG
4230void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004231 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004232}
Jim Laskeyef42a012006-11-02 20:12:39 +00004233#endif
4234
4235//===----------------------------------------------------------------------===//
4236
Jim Laskey063e7652006-01-17 17:31:53 +00004237/// EmitValue - Emit integer of appropriate size.
4238///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004239void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00004240 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00004241 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00004242 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004243 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004244 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004245 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004246 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004247 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004248 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004249 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4250 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4251 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004252 default: assert(0 && "DIE Value form not supported yet"); break;
4253 }
4254}
4255
4256/// SizeOf - Determine size of integer value in bytes.
4257///
Jim Laskey072200c2007-01-29 18:51:14 +00004258unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004259 switch (Form) {
4260 case DW_FORM_flag: // Fall thru
4261 case DW_FORM_ref1: // Fall thru
4262 case DW_FORM_data1: return sizeof(int8_t);
4263 case DW_FORM_ref2: // Fall thru
4264 case DW_FORM_data2: return sizeof(int16_t);
4265 case DW_FORM_ref4: // Fall thru
4266 case DW_FORM_data4: return sizeof(int32_t);
4267 case DW_FORM_ref8: // Fall thru
4268 case DW_FORM_data8: return sizeof(int64_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004269 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4270 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00004271 default: assert(0 && "DIE Value form not supported yet"); break;
4272 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004273 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00004274}
4275
Jim Laskey063e7652006-01-17 17:31:53 +00004276//===----------------------------------------------------------------------===//
4277
4278/// EmitValue - Emit string value.
4279///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004280void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004281 DD.getAsm()->EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00004282}
4283
Jim Laskey063e7652006-01-17 17:31:53 +00004284//===----------------------------------------------------------------------===//
4285
4286/// EmitValue - Emit label value.
4287///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004288void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004289 bool IsSmall = Form == DW_FORM_data4;
4290 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004291}
4292
4293/// SizeOf - Determine size of label value in bytes.
4294///
Jim Laskey072200c2007-01-29 18:51:14 +00004295unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004296 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004297 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004298}
Jim Laskeyef42a012006-11-02 20:12:39 +00004299
Jim Laskey063e7652006-01-17 17:31:53 +00004300//===----------------------------------------------------------------------===//
4301
Jim Laskeyd18e2892006-01-20 20:34:06 +00004302/// EmitValue - Emit label value.
4303///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004304void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004305 bool IsSmall = Form == DW_FORM_data4;
4306 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00004307}
4308
4309/// SizeOf - Determine size of label value in bytes.
4310///
Jim Laskey072200c2007-01-29 18:51:14 +00004311unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004312 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004313 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00004314}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004315
Jim Laskeyd18e2892006-01-20 20:34:06 +00004316//===----------------------------------------------------------------------===//
4317
Jim Laskey063e7652006-01-17 17:31:53 +00004318/// EmitValue - Emit delta value.
4319///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004320void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4321 bool IsSmall = Form == DW_FORM_data4;
4322 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4323 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4324}
4325
4326/// SizeOf - Determine size of delta value in bytes.
4327///
4328unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4329 if (Form == DW_FORM_data4) return 4;
4330 return DD.getTargetData()->getPointerSize();
4331}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004332
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004333//===----------------------------------------------------------------------===//
4334
4335/// EmitValue - Emit delta value.
4336///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004337void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004338 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00004339 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004340}
4341
4342/// SizeOf - Determine size of delta value in bytes.
4343///
Jim Laskey072200c2007-01-29 18:51:14 +00004344unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004345 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004346 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004347}
4348
4349//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004350
Jim Laskeyb8509c52006-03-23 18:07:55 +00004351/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004352///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004353void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004354 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00004355}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004356
Jim Laskeyd18e2892006-01-20 20:34:06 +00004357//===----------------------------------------------------------------------===//
4358
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004359/// ComputeSize - calculate the size of the block.
4360///
Jim Laskey072200c2007-01-29 18:51:14 +00004361unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00004362 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00004363 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004364
Jim Laskeyef42a012006-11-02 20:12:39 +00004365 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00004366 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00004367 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004368 }
4369 return Size;
4370}
4371
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004372/// EmitValue - Emit block data.
4373///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004374void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004375 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004376 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4377 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4378 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4379 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004380 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004381 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004382
Owen Anderson873e1b52008-06-24 21:44:59 +00004383 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00004384
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004385 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00004386 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00004387 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004388 }
4389}
4390
4391/// SizeOf - Determine size of block data in bytes.
4392///
Jim Laskey072200c2007-01-29 18:51:14 +00004393unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004394 switch (Form) {
4395 case DW_FORM_block1: return Size + sizeof(int8_t);
4396 case DW_FORM_block2: return Size + sizeof(int16_t);
4397 case DW_FORM_block4: return Size + sizeof(int32_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004398 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004399 default: assert(0 && "Improper form for block"); break;
4400 }
4401 return 0;
4402}
4403
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004404//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004405/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00004406
4407DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00004408 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00004409 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00004410}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004411
Jim Laskeyb8509c52006-03-23 18:07:55 +00004412/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4413///
4414void DIE::AddSiblingOffset() {
4415 DIEInteger *DI = new DIEInteger(0);
4416 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00004417 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00004418}
4419
Jim Laskeyef42a012006-11-02 20:12:39 +00004420/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004421///
Jim Laskeyef42a012006-11-02 20:12:39 +00004422void DIE::Profile(FoldingSetNodeID &ID) {
4423 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004424
Jim Laskeyef42a012006-11-02 20:12:39 +00004425 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4426 ID.AddPointer(Children[i]);
4427
4428 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4429 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00004430}
Jim Laskeyef42a012006-11-02 20:12:39 +00004431
4432#ifndef NDEBUG
4433void DIE::print(std::ostream &O, unsigned IncIndent) {
4434 static unsigned IndentCount = 0;
4435 IndentCount += IncIndent;
4436 const std::string Indent(IndentCount, ' ');
4437 bool isBlock = Abbrev.getTag() == 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004438
Jim Laskeyef42a012006-11-02 20:12:39 +00004439 if (!isBlock) {
4440 O << Indent
4441 << "Die: "
4442 << "0x" << std::hex << (intptr_t)this << std::dec
4443 << ", Offset: " << Offset
4444 << ", Size: " << Size
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004445 << "\n";
4446
Jim Laskeyef42a012006-11-02 20:12:39 +00004447 O << Indent
4448 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00004449 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00004450 << ChildrenString(Abbrev.getChildrenFlag());
4451 } else {
4452 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00004453 }
4454 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00004455
Owen Anderson873e1b52008-06-24 21:44:59 +00004456 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004457
Jim Laskeyef42a012006-11-02 20:12:39 +00004458 IndentCount += 2;
4459 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4460 O << Indent;
Bill Wendling4e974012008-07-22 00:28:47 +00004461
4462 if (!isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +00004463 O << AttributeString(Data[i].getAttribute());
Bill Wendling4e974012008-07-22 00:28:47 +00004464 else
Jim Laskeyef42a012006-11-02 20:12:39 +00004465 O << "Blk[" << i << "]";
Bill Wendling4e974012008-07-22 00:28:47 +00004466
Jim Laskeyef42a012006-11-02 20:12:39 +00004467 O << " "
4468 << FormEncodingString(Data[i].getForm())
4469 << " ";
4470 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00004471 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00004472 }
Jim Laskeyef42a012006-11-02 20:12:39 +00004473 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00004474
Jim Laskeyef42a012006-11-02 20:12:39 +00004475 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4476 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00004477 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004478
Jim Laskeyef42a012006-11-02 20:12:39 +00004479 if (!isBlock) O << "\n";
4480 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00004481}
4482
Jim Laskeyef42a012006-11-02 20:12:39 +00004483void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004484 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00004485}
Jim Laskeybd761842006-02-27 17:27:12 +00004486#endif
Jim Laskey65195462006-10-30 13:35:07 +00004487
4488//===----------------------------------------------------------------------===//
4489/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00004490///
Jim Laskey65195462006-10-30 13:35:07 +00004491
Bill Wendling91b8b802009-03-10 20:41:52 +00004492DwarfWriter::DwarfWriter()
Bill Wendling163ba3f2009-03-10 21:23:25 +00004493 : ImmutablePass(&ID), DD(0), DE(0) {}
Jim Laskey65195462006-10-30 13:35:07 +00004494
4495DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00004496 delete DE;
4497 delete DD;
Bill Wendling91b8b802009-03-10 20:41:52 +00004498 delete DwarfTimerGroup; DwarfTimerGroup = 0;
Jim Laskey65195462006-10-30 13:35:07 +00004499}
4500
Jim Laskey65195462006-10-30 13:35:07 +00004501/// BeginModule - Emit all Dwarf sections that should come prior to the
4502/// content.
Devang Pateleb3fc282009-01-08 23:40:34 +00004503void DwarfWriter::BeginModule(Module *M,
4504 MachineModuleInfo *MMI,
4505 raw_ostream &OS, AsmPrinter *A,
4506 const TargetAsmInfo *T) {
4507 DE = new DwarfException(OS, A, T);
4508 DD = new DwarfDebug(OS, A, T);
Jim Laskey072200c2007-01-29 18:51:14 +00004509 DE->BeginModule(M);
4510 DD->BeginModule(M);
Devang Patel7bb89ed2009-01-13 00:20:51 +00004511 DD->SetDebugInfo(MMI);
Devang Pateleb3fc282009-01-08 23:40:34 +00004512 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00004513}
4514
4515/// EndModule - Emit all Dwarf sections that should come after the content.
4516///
4517void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00004518 DE->EndModule();
4519 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00004520}
4521
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004522/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00004523/// emitted immediately after the function entry point.
4524void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00004525 DE->BeginFunction(MF);
4526 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00004527}
4528
4529/// EndFunction - Gather and emit post-function debug information.
4530///
Bill Wendlingd751c642008-09-26 00:28:12 +00004531void DwarfWriter::EndFunction(MachineFunction *MF) {
4532 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004533 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004534
Bill Wendlingc91d0b92008-07-22 00:53:37 +00004535 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00004536 // Clear function debug information.
4537 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00004538}
Devang Patelccca7fe2009-01-12 19:17:34 +00004539
Devang Patelcf3a4482009-01-15 23:41:32 +00004540/// ValidDebugInfo - Return true if V represents valid debug info value.
4541bool DwarfWriter::ValidDebugInfo(Value *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004542 return DD && DD->ValidDebugInfo(V);
Devang Patelcf3a4482009-01-15 23:41:32 +00004543}
4544
Devang Patelccca7fe2009-01-12 19:17:34 +00004545/// RecordSourceLine - Records location information and associates it with a
4546/// label. Returns a unique label ID used to generate a label and provide
4547/// correspondence to the source line list.
4548unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
4549 unsigned Src) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004550 return DD->RecordSourceLine(Line, Col, Src);
Devang Patelccca7fe2009-01-12 19:17:34 +00004551}
4552
Evan Chenge3d42322009-02-25 07:04:34 +00004553/// getOrCreateSourceID - Look up the source id with the given directory and
4554/// source file names. If none currently exists, create a new id and insert it
4555/// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
4556/// as well.
4557unsigned DwarfWriter::getOrCreateSourceID(const std::string &DirName,
4558 const std::string &FileName) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004559 return DD->getOrCreateSourceID(DirName, FileName);
Devang Patelccca7fe2009-01-12 19:17:34 +00004560}
4561
4562/// RecordRegionStart - Indicate the start of a region.
4563unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004564 return DD->RecordRegionStart(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004565}
4566
4567/// RecordRegionEnd - Indicate the end of a region.
4568unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004569 return DD->RecordRegionEnd(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004570}
4571
4572/// getRecordSourceLineCount - Count source lines.
4573unsigned DwarfWriter::getRecordSourceLineCount() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004574 return DD->getRecordSourceLineCount();
Devang Patelccca7fe2009-01-12 19:17:34 +00004575}
Devang Patelbb8c5952009-01-13 21:25:00 +00004576
Devang Patelc48c5502009-01-13 21:44:10 +00004577/// RecordVariable - Indicate the declaration of a local variable.
4578///
4579void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
4580 DD->RecordVariable(GV, FrameIndex);
4581}
Devang Patelbbdc8202009-01-13 23:54:55 +00004582
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004583/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
4584/// be emitted.
4585bool DwarfWriter::ShouldEmitDwarfDebug() const {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004586 return DD->ShouldEmitDwarfDebug();
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004587}