blob: e9fb32c441c29033b2c3216b4cb7659bb3f59575 [file] [log] [blame]
Jim Laskeye5032892005-12-21 19:48:16 +00001//===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Jim Laskeye5032892005-12-21 19:48:16 +00007//
8//===----------------------------------------------------------------------===//
9//
Jim Laskey072200c2007-01-29 18:51:14 +000010// This file contains support for writing dwarf info into asm files.
Jim Laskeye5032892005-12-21 19:48:16 +000011//
12//===----------------------------------------------------------------------===//
Jim Laskey3ea0e0e2006-01-27 18:32:41 +000013
Jim Laskeyb2efb852006-01-04 22:28:25 +000014#include "llvm/CodeGen/DwarfWriter.h"
Jim Laskey52060a02006-01-24 00:49:18 +000015#include "llvm/Module.h"
Devang Pateld1ca9252009-01-05 23:03:32 +000016#include "llvm/DerivedTypes.h"
Devang Patelcf3a4482009-01-15 23:41:32 +000017#include "llvm/Constants.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000018#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskey44c3b9f2007-01-26 21:22:28 +000019#include "llvm/CodeGen/MachineModuleInfo.h"
Jim Laskey41886992006-04-07 16:34:46 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000021#include "llvm/CodeGen/MachineLocation.h"
Devang Patel08f053f2009-01-05 17:57:47 +000022#include "llvm/Analysis/DebugInfo.h"
Jim Laskey3f09fc22007-02-28 18:38:31 +000023#include "llvm/Support/Debug.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000024#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000025#include "llvm/Support/CommandLine.h"
Jim Laskey65195462006-10-30 13:35:07 +000026#include "llvm/Support/DataTypes.h"
Jim Laskey52060a02006-01-24 00:49:18 +000027#include "llvm/Support/Mangler.h"
Bill Wendling91b8b802009-03-10 20:41:52 +000028#include "llvm/Support/Timer.h"
Owen Andersoncb371882008-08-21 00:14:44 +000029#include "llvm/Support/raw_ostream.h"
Dan Gohman85496362007-09-24 21:32:18 +000030#include "llvm/System/Path.h"
Jim Laskey563321a2006-09-06 18:34:40 +000031#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000032#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000033#include "llvm/Target/TargetData.h"
Jim Laskey1069fbd2006-04-10 23:09:19 +000034#include "llvm/Target/TargetFrameInfo.h"
Duncan Sands53c3a332007-05-16 12:12:23 +000035#include "llvm/Target/TargetInstrInfo.h"
Jim Laskey1b340dc2007-01-29 20:48:32 +000036#include "llvm/Target/TargetMachine.h"
Jim Laskeyc1c47c32007-01-29 23:40:33 +000037#include "llvm/Target/TargetOptions.h"
Evan Chenge3d42322009-02-25 07:04:34 +000038#include "llvm/ADT/DenseMap.h"
39#include "llvm/ADT/FoldingSet.h"
40#include "llvm/ADT/StringExtras.h"
41#include "llvm/ADT/StringMap.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000042#include <ostream>
Jim Laskey65195462006-10-30 13:35:07 +000043#include <string>
Jim Laskeyb2efb852006-01-04 22:28:25 +000044using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000045using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000046
Devang Pateleb3fc282009-01-08 23:40:34 +000047static RegisterPass<DwarfWriter>
48X("dwarfwriter", "DWARF Information Writer");
49char DwarfWriter::ID = 0;
50
Bill Wendling68edf5f2009-03-10 22:58:53 +000051static TimerGroup &getDwarfTimerGroup() {
52 static TimerGroup DwarfTimerGroup("Dwarf Exception and Debugging");
53 return DwarfTimerGroup;
Bill Wendling91b8b802009-03-10 20:41:52 +000054}
55
Jim Laskey0d086af2006-02-27 12:43:29 +000056namespace llvm {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000057
Jim Laskey65195462006-10-30 13:35:07 +000058//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +000059
60/// Configuration values for initial hash set sizes (log2).
61///
Bill Wendlingb9dcef22009-02-03 21:17:20 +000062static const unsigned InitDiesSetSize = 9; // log2(512)
63static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
64static const unsigned InitValuesSetSize = 9; // log2(512)
Jim Laskeyef42a012006-11-02 20:12:39 +000065
66//===----------------------------------------------------------------------===//
67/// Forward declarations.
68///
69class DIE;
70class DIEValue;
71
72//===----------------------------------------------------------------------===//
Devang Pateld1ca9252009-01-05 23:03:32 +000073/// Utility routines.
74///
Devang Patelcf3a4482009-01-15 23:41:32 +000075/// getGlobalVariable - Return either a direct or cast Global value.
76///
77static GlobalVariable *getGlobalVariable(Value *V) {
78 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
79 return GV;
80 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
81 if (CE->getOpcode() == Instruction::BitCast) {
82 return dyn_cast<GlobalVariable>(CE->getOperand(0));
83 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
84 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
85 if (!CE->getOperand(i)->isNullValue())
86 return NULL;
87 }
88 return dyn_cast<GlobalVariable>(CE->getOperand(0));
89 }
90 }
91 return NULL;
92}
93
Devang Pateld1ca9252009-01-05 23:03:32 +000094//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000095/// DWLabel - Labels are used to track locations in the assembler file.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000096/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
97/// where the tag is a category of label (Ex. location) and number is a value
Reid Spencer181b6c92007-08-05 20:06:04 +000098/// unique in that category.
Jim Laskey65195462006-10-30 13:35:07 +000099class DWLabel {
100public:
Jim Laskeyef42a012006-11-02 20:12:39 +0000101 /// Tag - Label category tag. Should always be a staticly declared C string.
102 ///
103 const char *Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000104
Jim Laskeyef42a012006-11-02 20:12:39 +0000105 /// Number - Value to make label unique.
106 ///
107 unsigned Number;
Jim Laskey65195462006-10-30 13:35:07 +0000108
109 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000110
Jim Laskeyef42a012006-11-02 20:12:39 +0000111 void Profile(FoldingSetNodeID &ID) const {
Evan Chenge3d42322009-02-25 07:04:34 +0000112 ID.AddString(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +0000113 ID.AddInteger(Number);
Jim Laskey90c79d72006-03-23 23:02:34 +0000114 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000115
Jim Laskeyef42a012006-11-02 20:12:39 +0000116#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000117 void print(std::ostream *O) const {
118 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000119 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000120 void print(std::ostream &O) const {
Jim Laskeybacd3042007-02-21 22:48:45 +0000121 O << "." << Tag;
Jim Laskeyef42a012006-11-02 20:12:39 +0000122 if (Number) O << Number;
123 }
124#endif
Jim Laskeybd761842006-02-27 17:27:12 +0000125};
126
127//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000128/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
129/// Dwarf abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +0000130class DIEAbbrevData {
131private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000132 /// Attribute - Dwarf attribute code.
133 ///
134 unsigned Attribute;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000135
Jim Laskeyef42a012006-11-02 20:12:39 +0000136 /// Form - Dwarf form code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000137 ///
138 unsigned Form;
139
Jim Laskey0d086af2006-02-27 12:43:29 +0000140public:
141 DIEAbbrevData(unsigned A, unsigned F)
142 : Attribute(A)
143 , Form(F)
144 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000145
Jim Laskeybd761842006-02-27 17:27:12 +0000146 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000147 unsigned getAttribute() const { return Attribute; }
148 unsigned getForm() const { return Form; }
Jim Laskey063e7652006-01-17 17:31:53 +0000149
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000150 /// Profile - Used to gather unique data for the abbreviation folding set.
Jim Laskey0d086af2006-02-27 12:43:29 +0000151 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000152 void Profile(FoldingSetNodeID &ID)const {
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000153 ID.AddInteger(Attribute);
154 ID.AddInteger(Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000155 }
156};
Jim Laskey063e7652006-01-17 17:31:53 +0000157
Jim Laskey0d086af2006-02-27 12:43:29 +0000158//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000159/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
160/// information object.
161class DIEAbbrev : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000162private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000163 /// Tag - Dwarf tag code.
164 ///
165 unsigned Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000166
Jim Laskeyef42a012006-11-02 20:12:39 +0000167 /// Unique number for node.
168 ///
169 unsigned Number;
170
171 /// ChildrenFlag - Dwarf children flag.
172 ///
173 unsigned ChildrenFlag;
174
175 /// Data - Raw data bytes for abbreviation.
176 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000177 SmallVector<DIEAbbrevData, 8> Data;
Jim Laskey063e7652006-01-17 17:31:53 +0000178
Jim Laskey0d086af2006-02-27 12:43:29 +0000179public:
Jim Laskey063e7652006-01-17 17:31:53 +0000180
Jim Laskey0d086af2006-02-27 12:43:29 +0000181 DIEAbbrev(unsigned T, unsigned C)
Jim Laskeyef42a012006-11-02 20:12:39 +0000182 : Tag(T)
Jim Laskey0d086af2006-02-27 12:43:29 +0000183 , ChildrenFlag(C)
184 , Data()
185 {}
186 ~DIEAbbrev() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000187
Jim Laskeybd761842006-02-27 17:27:12 +0000188 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000189 unsigned getTag() const { return Tag; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000190 unsigned getNumber() const { return Number; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000191 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000192 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000193 void setTag(unsigned T) { Tag = T; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000194 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000195 void setNumber(unsigned N) { Number = N; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000196
Jim Laskey0d086af2006-02-27 12:43:29 +0000197 /// AddAttribute - Adds another set of attribute information to the
198 /// abbreviation.
199 void AddAttribute(unsigned Attribute, unsigned Form) {
200 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000201 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000202
Jim Laskeyb8509c52006-03-23 18:07:55 +0000203 /// AddFirstAttribute - Adds a set of attribute information to the front
204 /// of the abbreviation.
205 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
206 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
207 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000208
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000209 /// Profile - Used to gather unique data for the abbreviation folding set.
210 ///
211 void Profile(FoldingSetNodeID &ID) {
212 ID.AddInteger(Tag);
213 ID.AddInteger(ChildrenFlag);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000214
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000215 // For each attribute description.
216 for (unsigned i = 0, N = Data.size(); i < N; ++i)
217 Data[i].Profile(ID);
218 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000219
Jim Laskey0d086af2006-02-27 12:43:29 +0000220 /// Emit - Print the abbreviation using the specified Dwarf writer.
221 ///
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000222 void Emit(const DwarfDebug &DD) const;
223
Jim Laskey0d086af2006-02-27 12:43:29 +0000224#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000225 void print(std::ostream *O) {
226 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000227 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000228 void print(std::ostream &O);
229 void dump();
230#endif
231};
Jim Laskey063e7652006-01-17 17:31:53 +0000232
Jim Laskey0d086af2006-02-27 12:43:29 +0000233//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000234/// DIE - A structured debug information entry. Has an abbreviation which
235/// describes it's organization.
236class DIE : public FoldingSetNode {
237protected:
238 /// Abbrev - Buffer for constructing abbreviation.
239 ///
240 DIEAbbrev Abbrev;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000241
Jim Laskeyef42a012006-11-02 20:12:39 +0000242 /// Offset - Offset in debug info section.
243 ///
244 unsigned Offset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000245
Jim Laskeyef42a012006-11-02 20:12:39 +0000246 /// Size - Size of instance + children.
247 ///
248 unsigned Size;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000249
Jim Laskeyef42a012006-11-02 20:12:39 +0000250 /// Children DIEs.
251 ///
252 std::vector<DIE *> Children;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000253
Jim Laskeyef42a012006-11-02 20:12:39 +0000254 /// Attributes values.
255 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000256 SmallVector<DIEValue*, 32> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000257
Jim Laskeyef42a012006-11-02 20:12:39 +0000258public:
Dan Gohman81975f62007-08-27 14:50:10 +0000259 explicit DIE(unsigned Tag)
Jim Laskeyef42a012006-11-02 20:12:39 +0000260 : Abbrev(Tag, DW_CHILDREN_no)
261 , Offset(0)
262 , Size(0)
263 , Children()
264 , Values()
265 {}
266 virtual ~DIE();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000267
Jim Laskeyef42a012006-11-02 20:12:39 +0000268 // Accessors.
269 DIEAbbrev &getAbbrev() { return Abbrev; }
270 unsigned getAbbrevNumber() const {
271 return Abbrev.getNumber();
272 }
Jim Laskey85f419b2006-11-09 16:32:26 +0000273 unsigned getTag() const { return Abbrev.getTag(); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000274 unsigned getOffset() const { return Offset; }
275 unsigned getSize() const { return Size; }
276 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000277 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000278 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
279 void setOffset(unsigned O) { Offset = O; }
280 void setSize(unsigned S) { Size = S; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000281
Jim Laskeyef42a012006-11-02 20:12:39 +0000282 /// AddValue - Add a value and attributes to a DIE.
283 ///
284 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
285 Abbrev.AddAttribute(Attribute, Form);
286 Values.push_back(Value);
287 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000288
Jim Laskeyef42a012006-11-02 20:12:39 +0000289 /// SiblingOffset - Return the offset of the debug information entry's
290 /// sibling.
291 unsigned SiblingOffset() const { return Offset + Size; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000292
Jim Laskeyef42a012006-11-02 20:12:39 +0000293 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
294 ///
295 void AddSiblingOffset();
296
297 /// AddChild - Add a child to the DIE.
298 ///
299 void AddChild(DIE *Child) {
300 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
301 Children.push_back(Child);
302 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000303
Jim Laskeyef42a012006-11-02 20:12:39 +0000304 /// Detach - Detaches objects connected to it after copying.
305 ///
306 void Detach() {
307 Children.clear();
308 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000309
Jim Laskeyef42a012006-11-02 20:12:39 +0000310 /// Profile - Used to gather unique data for the value folding set.
311 ///
312 void Profile(FoldingSetNodeID &ID) ;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000313
Jim Laskeyef42a012006-11-02 20:12:39 +0000314#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000315 void print(std::ostream *O, unsigned IncIndent = 0) {
316 if (O) print(*O, IncIndent);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000317 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000318 void print(std::ostream &O, unsigned IncIndent = 0);
319 void dump();
320#endif
321};
322
323//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000324/// DIEValue - A debug information entry value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000325///
326class DIEValue : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000327public:
328 enum {
329 isInteger,
330 isString,
331 isLabel,
332 isAsIsLabel,
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000333 isSectionOffset,
Jim Laskey0d086af2006-02-27 12:43:29 +0000334 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000335 isEntry,
336 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000337 };
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000338
Jim Laskeyef42a012006-11-02 20:12:39 +0000339 /// Type - Type of data stored in the value.
340 ///
341 unsigned Type;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000342
Dan Gohman81975f62007-08-27 14:50:10 +0000343 explicit DIEValue(unsigned T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000344 : Type(T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000345 {}
Jim Laskey0d086af2006-02-27 12:43:29 +0000346 virtual ~DIEValue() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000347
Jim Laskeyf6733882006-11-02 21:48:18 +0000348 // Accessors
Jim Laskeyef42a012006-11-02 20:12:39 +0000349 unsigned getType() const { return Type; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000350
Jim Laskey0d086af2006-02-27 12:43:29 +0000351 // Implement isa/cast/dyncast.
352 static bool classof(const DIEValue *) { return true; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000353
Jim Laskey0d086af2006-02-27 12:43:29 +0000354 /// EmitValue - Emit value via the Dwarf writer.
355 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000356 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000357
Jim Laskey0d086af2006-02-27 12:43:29 +0000358 /// SizeOf - Return the size of a value in bytes.
359 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000360 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000361
Jim Laskeyef42a012006-11-02 20:12:39 +0000362 /// Profile - Used to gather unique data for the value folding set.
363 ///
364 virtual void Profile(FoldingSetNodeID &ID) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000365
Jim Laskeyef42a012006-11-02 20:12:39 +0000366#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000367 void print(std::ostream *O) {
368 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000369 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000370 virtual void print(std::ostream &O) = 0;
371 void dump();
372#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000373};
Jim Laskey063e7652006-01-17 17:31:53 +0000374
Jim Laskey0d086af2006-02-27 12:43:29 +0000375//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000376/// DWInteger - An integer value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000377///
Jim Laskey0d086af2006-02-27 12:43:29 +0000378class DIEInteger : public DIEValue {
379private:
380 uint64_t Integer;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000381
Jim Laskey0d086af2006-02-27 12:43:29 +0000382public:
Dan Gohman81975f62007-08-27 14:50:10 +0000383 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000384
Jim Laskey0d086af2006-02-27 12:43:29 +0000385 // Implement isa/cast/dyncast.
386 static bool classof(const DIEInteger *) { return true; }
387 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000388
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000389 /// BestForm - Choose the best form for integer.
390 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000391 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
392 if (IsSigned) {
393 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
394 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
395 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
396 } else {
397 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
398 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
399 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
400 }
401 return DW_FORM_data8;
402 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000403
Jim Laskey0d086af2006-02-27 12:43:29 +0000404 /// EmitValue - Emit integer of appropriate size.
405 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000406 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000407
Jim Laskey0d086af2006-02-27 12:43:29 +0000408 /// SizeOf - Determine size of integer value in bytes.
409 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000410 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000411
Jim Laskeyef42a012006-11-02 20:12:39 +0000412 /// Profile - Used to gather unique data for the value folding set.
413 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000414 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000415 ID.AddInteger(isInteger);
Jim Laskeyef42a012006-11-02 20:12:39 +0000416 ID.AddInteger(Integer);
417 }
Jim Laskey5496f012006-11-09 14:52:14 +0000418 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000419
Jim Laskeyef42a012006-11-02 20:12:39 +0000420#ifndef NDEBUG
421 virtual void print(std::ostream &O) {
422 O << "Int: " << (int64_t)Integer
423 << " 0x" << std::hex << Integer << std::dec;
424 }
425#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000426};
Jim Laskey063e7652006-01-17 17:31:53 +0000427
Jim Laskey0d086af2006-02-27 12:43:29 +0000428//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000429/// DIEString - A string value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000430///
Jim Laskeyef42a012006-11-02 20:12:39 +0000431class DIEString : public DIEValue {
432public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000433 const std::string String;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000434
Dan Gohman81975f62007-08-27 14:50:10 +0000435 explicit DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000436
Jim Laskey0d086af2006-02-27 12:43:29 +0000437 // Implement isa/cast/dyncast.
438 static bool classof(const DIEString *) { return true; }
439 static bool classof(const DIEValue *S) { return S->Type == isString; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000440
Jim Laskey0d086af2006-02-27 12:43:29 +0000441 /// EmitValue - Emit string value.
442 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000443 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000444
Jim Laskey0d086af2006-02-27 12:43:29 +0000445 /// SizeOf - Determine size of string value in bytes.
446 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000447 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000448 return String.size() + sizeof(char); // sizeof('\0');
449 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000450
Jim Laskeyef42a012006-11-02 20:12:39 +0000451 /// Profile - Used to gather unique data for the value folding set.
452 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000453 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000454 ID.AddInteger(isString);
Jim Laskeyef42a012006-11-02 20:12:39 +0000455 ID.AddString(String);
456 }
Jim Laskey5496f012006-11-09 14:52:14 +0000457 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000458
Jim Laskeyef42a012006-11-02 20:12:39 +0000459#ifndef NDEBUG
460 virtual void print(std::ostream &O) {
461 O << "Str: \"" << String << "\"";
462 }
463#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000464};
Jim Laskey063e7652006-01-17 17:31:53 +0000465
Jim Laskey0d086af2006-02-27 12:43:29 +0000466//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000467/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000468//
Jim Laskeyef42a012006-11-02 20:12:39 +0000469class DIEDwarfLabel : public DIEValue {
470public:
471
Jim Laskey0d086af2006-02-27 12:43:29 +0000472 const DWLabel Label;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000473
Dan Gohman81975f62007-08-27 14:50:10 +0000474 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000475
Jim Laskey0d086af2006-02-27 12:43:29 +0000476 // Implement isa/cast/dyncast.
477 static bool classof(const DIEDwarfLabel *) { return true; }
478 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000479
Jim Laskey0d086af2006-02-27 12:43:29 +0000480 /// EmitValue - Emit label value.
481 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000482 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000483
Jim Laskey0d086af2006-02-27 12:43:29 +0000484 /// SizeOf - Determine size of label value in bytes.
485 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000486 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000487
Jim Laskeyef42a012006-11-02 20:12:39 +0000488 /// Profile - Used to gather unique data for the value folding set.
489 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000490 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000491 ID.AddInteger(isLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000492 Label.Profile(ID);
493 }
Jim Laskey5496f012006-11-09 14:52:14 +0000494 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000495
Jim Laskeyef42a012006-11-02 20:12:39 +0000496#ifndef NDEBUG
497 virtual void print(std::ostream &O) {
498 O << "Lbl: ";
499 Label.print(O);
500 }
501#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000502};
Jim Laskey063e7652006-01-17 17:31:53 +0000503
Jim Laskey063e7652006-01-17 17:31:53 +0000504
Jim Laskey0d086af2006-02-27 12:43:29 +0000505//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000506/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000507//
Jim Laskeyef42a012006-11-02 20:12:39 +0000508class DIEObjectLabel : public DIEValue {
509public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000510 const std::string Label;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000511
Dan Gohman81975f62007-08-27 14:50:10 +0000512 explicit DIEObjectLabel(const std::string &L)
513 : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000514
Jim Laskey0d086af2006-02-27 12:43:29 +0000515 // Implement isa/cast/dyncast.
516 static bool classof(const DIEObjectLabel *) { return true; }
517 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000518
Jim Laskey0d086af2006-02-27 12:43:29 +0000519 /// EmitValue - Emit label value.
520 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000521 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000522
Jim Laskey0d086af2006-02-27 12:43:29 +0000523 /// SizeOf - Determine size of label value in bytes.
524 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000525 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000526
Jim Laskeyef42a012006-11-02 20:12:39 +0000527 /// Profile - Used to gather unique data for the value folding set.
528 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000529 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000530 ID.AddInteger(isAsIsLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000531 ID.AddString(Label);
532 }
Evan Chenge3d42322009-02-25 07:04:34 +0000533 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label.c_str()); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000534
535#ifndef NDEBUG
536 virtual void print(std::ostream &O) {
537 O << "Obj: " << Label;
538 }
539#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000540};
Jim Laskey063e7652006-01-17 17:31:53 +0000541
Jim Laskey0d086af2006-02-27 12:43:29 +0000542//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000543/// DIESectionOffset - A section offset DIE.
544//
545class DIESectionOffset : public DIEValue {
546public:
547 const DWLabel Label;
548 const DWLabel Section;
549 bool IsEH : 1;
550 bool UseSet : 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000551
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000552 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
553 bool isEH = false, bool useSet = true)
554 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
555 IsEH(isEH), UseSet(useSet) {}
556
557 // Implement isa/cast/dyncast.
558 static bool classof(const DIESectionOffset *) { return true; }
559 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000560
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000561 /// EmitValue - Emit section offset.
562 ///
563 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000564
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000565 /// SizeOf - Determine size of section offset value in bytes.
566 ///
567 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000568
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000569 /// Profile - Used to gather unique data for the value folding set.
570 ///
571 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
572 const DWLabel &Section) {
573 ID.AddInteger(isSectionOffset);
574 Label.Profile(ID);
575 Section.Profile(ID);
576 // IsEH and UseSet are specific to the Label/Section that we will emit
577 // the offset for; so Label/Section are enough for uniqueness.
578 }
579 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
580
581#ifndef NDEBUG
582 virtual void print(std::ostream &O) {
583 O << "Off: ";
584 Label.print(O);
585 O << "-";
586 Section.print(O);
587 O << "-" << IsEH << "-" << UseSet;
588 }
589#endif
590};
591
592//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000593/// DIEDelta - A simple label difference DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000594///
Jim Laskeyef42a012006-11-02 20:12:39 +0000595class DIEDelta : public DIEValue {
596public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000597 const DWLabel LabelHi;
598 const DWLabel LabelLo;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000599
Jim Laskey0d086af2006-02-27 12:43:29 +0000600 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
601 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000602
Jim Laskey0d086af2006-02-27 12:43:29 +0000603 // Implement isa/cast/dyncast.
604 static bool classof(const DIEDelta *) { return true; }
605 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000606
Jim Laskey0d086af2006-02-27 12:43:29 +0000607 /// EmitValue - Emit delta value.
608 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000609 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000610
Jim Laskey0d086af2006-02-27 12:43:29 +0000611 /// SizeOf - Determine size of delta value in bytes.
612 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000613 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000614
Jim Laskeyef42a012006-11-02 20:12:39 +0000615 /// Profile - Used to gather unique data for the value folding set.
616 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000617 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
618 const DWLabel &LabelLo) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000619 ID.AddInteger(isDelta);
Jim Laskeyef42a012006-11-02 20:12:39 +0000620 LabelHi.Profile(ID);
621 LabelLo.Profile(ID);
622 }
Jim Laskey5496f012006-11-09 14:52:14 +0000623 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000624
625#ifndef NDEBUG
626 virtual void print(std::ostream &O) {
627 O << "Del: ";
628 LabelHi.print(O);
629 O << "-";
630 LabelLo.print(O);
631 }
632#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000633};
Jim Laskey063e7652006-01-17 17:31:53 +0000634
Jim Laskey0d086af2006-02-27 12:43:29 +0000635//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000636/// DIEntry - A pointer to another debug information entry. An instance of this
637/// class can also be used as a proxy for a debug information entry not yet
638/// defined (ie. types.)
639class DIEntry : public DIEValue {
640public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000641 DIE *Entry;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000642
Dan Gohman81975f62007-08-27 14:50:10 +0000643 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000644
Jim Laskey0d086af2006-02-27 12:43:29 +0000645 // Implement isa/cast/dyncast.
646 static bool classof(const DIEntry *) { return true; }
647 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000648
Jim Laskeyb8509c52006-03-23 18:07:55 +0000649 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000650 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000651 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000652
Jim Laskeyb8509c52006-03-23 18:07:55 +0000653 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000654 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000655 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000656 return sizeof(int32_t);
657 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000658
Jim Laskeyef42a012006-11-02 20:12:39 +0000659 /// Profile - Used to gather unique data for the value folding set.
660 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000661 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
662 ID.AddInteger(isEntry);
663 ID.AddPointer(Entry);
664 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000665 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000666 ID.AddInteger(isEntry);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000667
Jim Laskeyef42a012006-11-02 20:12:39 +0000668 if (Entry) {
669 ID.AddPointer(Entry);
670 } else {
671 ID.AddPointer(this);
672 }
673 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000674
Jim Laskeyef42a012006-11-02 20:12:39 +0000675#ifndef NDEBUG
676 virtual void print(std::ostream &O) {
677 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
678 }
679#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000680};
681
682//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000683/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000684//
Jim Laskeyef42a012006-11-02 20:12:39 +0000685class DIEBlock : public DIEValue, public DIE {
686public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000687 unsigned Size; // Size in bytes excluding size header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000688
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000689 DIEBlock()
690 : DIEValue(isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +0000691 , DIE(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000692 , Size(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000693 {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000694 ~DIEBlock() {
695 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000696
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000697 // Implement isa/cast/dyncast.
698 static bool classof(const DIEBlock *) { return true; }
699 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000700
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000701 /// ComputeSize - calculate the size of the block.
702 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000703 unsigned ComputeSize(DwarfDebug &DD);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000704
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000705 /// BestForm - Choose the best form for data.
706 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000707 unsigned BestForm() const {
708 if ((unsigned char)Size == Size) return DW_FORM_block1;
709 if ((unsigned short)Size == Size) return DW_FORM_block2;
710 if ((unsigned int)Size == Size) return DW_FORM_block4;
711 return DW_FORM_block;
712 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000713
714 /// EmitValue - Emit block data.
715 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000716 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000717
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000718 /// SizeOf - Determine size of block data in bytes.
719 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000720 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000721
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000722
Jim Laskeyef42a012006-11-02 20:12:39 +0000723 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000724 ///
Reid Spencer97821312006-11-02 23:56:21 +0000725 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000726 ID.AddInteger(isBlock);
Jim Laskeyef42a012006-11-02 20:12:39 +0000727 DIE::Profile(ID);
728 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000729
Jim Laskeyef42a012006-11-02 20:12:39 +0000730#ifndef NDEBUG
731 virtual void print(std::ostream &O) {
732 O << "Blk: ";
733 DIE::print(O, 5);
734 }
735#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000736};
737
738//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000739/// CompileUnit - This dwarf writer support class manages information associate
740/// with a source file.
741class CompileUnit {
Jim Laskey0d086af2006-02-27 12:43:29 +0000742private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000743 /// ID - File identifier for source.
744 ///
745 unsigned ID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000746
Jim Laskeyef42a012006-11-02 20:12:39 +0000747 /// Die - Compile unit debug information entry.
748 ///
749 DIE *Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000750
Devang Patelbbdc8202009-01-13 23:54:55 +0000751 /// GVToDieMap - Tracks the mapping of unit level debug informaton
752 /// variables to debug information entries.
Devang Patelc2997f42009-01-20 00:58:55 +0000753 std::map<GlobalVariable *, DIE *> GVToDieMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000754
Devang Patelbbdc8202009-01-13 23:54:55 +0000755 /// GVToDIEntryMap - Tracks the mapping of unit level debug informaton
Jim Laskeyef42a012006-11-02 20:12:39 +0000756 /// descriptors to debug information entries using a DIEntry proxy.
Devang Patelc2997f42009-01-20 00:58:55 +0000757 std::map<GlobalVariable *, DIEntry *> GVToDIEntryMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000758
759 /// Globals - A map of globally visible named entities for this unit.
760 ///
761 std::map<std::string, DIE *> Globals;
762
763 /// DiesSet - Used to uniquely define dies within the compile unit.
764 ///
765 FoldingSet<DIE> DiesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000766
Jim Laskey0d086af2006-02-27 12:43:29 +0000767public:
Devang Pateld1ca9252009-01-05 23:03:32 +0000768 CompileUnit(unsigned I, DIE *D)
Devang Patelbbdc8202009-01-13 23:54:55 +0000769 : ID(I), Die(D), GVToDieMap(),
Devang Patel7e55b3a2009-01-17 06:51:37 +0000770 GVToDIEntryMap(), Globals(), DiesSet(InitDiesSetSize)
Devang Pateld1ca9252009-01-05 23:03:32 +0000771 {}
772
Jim Laskeyef42a012006-11-02 20:12:39 +0000773 ~CompileUnit() {
774 delete Die;
Jim Laskeyef42a012006-11-02 20:12:39 +0000775 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000776
Jim Laskeybd761842006-02-27 17:27:12 +0000777 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000778 unsigned getID() const { return ID; }
779 DIE* getDie() const { return Die; }
780 std::map<std::string, DIE *> &getGlobals() { return Globals; }
781
782 /// hasContent - Return true if this compile unit has something to write out.
783 ///
784 bool hasContent() const {
785 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000786 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000787
788 /// AddGlobal - Add a new global entity to the compile unit.
789 ///
790 void AddGlobal(const std::string &Name, DIE *Die) {
791 Globals[Name] = Die;
792 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000793
Jim Laskeyef42a012006-11-02 20:12:39 +0000794 /// getDieMapSlotFor - Returns the debug information entry map slot for the
Devang Patelbbdc8202009-01-13 23:54:55 +0000795 /// specified debug variable.
Devang Patel48d190f2009-01-05 21:47:57 +0000796 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
797 return GVToDieMap[GV];
798 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000799
Jim Laskeyef42a012006-11-02 20:12:39 +0000800 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
Devang Patelbbdc8202009-01-13 23:54:55 +0000801 /// specified debug variable.
Devang Patel48d190f2009-01-05 21:47:57 +0000802 DIEntry *&getDIEntrySlotFor(GlobalVariable *GV) {
803 return GVToDIEntryMap[GV];
804 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000805
Jim Laskeyef42a012006-11-02 20:12:39 +0000806 /// AddDie - Adds or interns the DIE to the compile unit.
807 ///
808 DIE *AddDie(DIE &Buffer) {
809 FoldingSetNodeID ID;
810 Buffer.Profile(ID);
811 void *Where;
812 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000813
Jim Laskeyef42a012006-11-02 20:12:39 +0000814 if (!Die) {
815 Die = new DIE(Buffer);
816 DiesSet.InsertNode(Die, Where);
817 this->Die->AddChild(Die);
818 Buffer.Detach();
819 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000820
Jim Laskeyef42a012006-11-02 20:12:39 +0000821 return Die;
822 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000823};
824
Jim Laskey65195462006-10-30 13:35:07 +0000825//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000826/// Dwarf - Emits general Dwarf directives.
Jim Laskeyef42a012006-11-02 20:12:39 +0000827///
Jim Laskey65195462006-10-30 13:35:07 +0000828class Dwarf {
Jim Laskey072200c2007-01-29 18:51:14 +0000829protected:
Jim Laskey65195462006-10-30 13:35:07 +0000830 //===--------------------------------------------------------------------===//
Jim Laskey072200c2007-01-29 18:51:14 +0000831 // Core attributes used by the Dwarf writer.
Jim Laskey65195462006-10-30 13:35:07 +0000832 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000833
Jim Laskey65195462006-10-30 13:35:07 +0000834 //
835 /// O - Stream to .s file.
836 ///
Owen Andersoncb371882008-08-21 00:14:44 +0000837 raw_ostream &O;
Jim Laskey65195462006-10-30 13:35:07 +0000838
839 /// Asm - Target of Dwarf emission.
840 ///
841 AsmPrinter *Asm;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000842
Bill Wendlingaa8f8882008-07-01 23:34:48 +0000843 /// TAI - Target asm information.
Jim Laskey65195462006-10-30 13:35:07 +0000844 const TargetAsmInfo *TAI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000845
Jim Laskey65195462006-10-30 13:35:07 +0000846 /// TD - Target data.
847 const TargetData *TD;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000848
Jim Laskey65195462006-10-30 13:35:07 +0000849 /// RI - Register Information.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000850 const TargetRegisterInfo *RI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000851
Jim Laskey65195462006-10-30 13:35:07 +0000852 /// M - Current module.
853 ///
854 Module *M;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000855
Jim Laskey65195462006-10-30 13:35:07 +0000856 /// MF - Current machine function.
857 ///
858 MachineFunction *MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000859
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000860 /// MMI - Collected machine module information.
Jim Laskey65195462006-10-30 13:35:07 +0000861 ///
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000862 MachineModuleInfo *MMI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000863
Jim Laskey65195462006-10-30 13:35:07 +0000864 /// SubprogramCount - The running count of functions being compiled.
865 ///
866 unsigned SubprogramCount;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000867
Chris Lattner9251f132007-09-24 03:35:37 +0000868 /// Flavor - A unique string indicating what dwarf producer this is, used to
869 /// unique labels.
870 const char * const Flavor;
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000871
872 unsigned SetCounter;
Owen Andersoncb371882008-08-21 00:14:44 +0000873 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattner9251f132007-09-24 03:35:37 +0000874 const char *flavor)
Jim Laskey072200c2007-01-29 18:51:14 +0000875 : O(OS)
876 , Asm(A)
877 , TAI(T)
878 , TD(Asm->TM.getTargetData())
879 , RI(Asm->TM.getRegisterInfo())
880 , M(NULL)
881 , MF(NULL)
882 , MMI(NULL)
Jim Laskey072200c2007-01-29 18:51:14 +0000883 , SubprogramCount(0)
Chris Lattner9251f132007-09-24 03:35:37 +0000884 , Flavor(flavor)
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000885 , SetCounter(1)
Jim Laskey072200c2007-01-29 18:51:14 +0000886 {
887 }
888
889public:
Jim Laskey072200c2007-01-29 18:51:14 +0000890 //===--------------------------------------------------------------------===//
891 // Accessors.
892 //
893 AsmPrinter *getAsm() const { return Asm; }
Jim Laskeyb82313f2007-02-01 16:31:34 +0000894 MachineModuleInfo *getMMI() const { return MMI; }
Jim Laskey072200c2007-01-29 18:51:14 +0000895 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohman82482942007-09-27 23:12:31 +0000896 const TargetData *getTargetData() const { return TD; }
Jim Laskey072200c2007-01-29 18:51:14 +0000897
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000898 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
899 const {
900 if (isInSection && TAI->getDwarfSectionOffsetDirective())
901 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohman82482942007-09-27 23:12:31 +0000902 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000903 O << TAI->getData32bitsDirective();
904 else
905 O << TAI->getData64bitsDirective();
906 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000907
Jim Laskeyb82313f2007-02-01 16:31:34 +0000908 /// PrintLabelName - Print label name in form used by Dwarf writer.
909 ///
910 void PrintLabelName(DWLabel Label) const {
911 PrintLabelName(Label.Tag, Label.Number);
912 }
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000913 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000914 O << TAI->getPrivateGlobalPrefix() << Tag;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000915 if (Number) O << Number;
916 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000917
Chris Lattner9251f132007-09-24 03:35:37 +0000918 void PrintLabelName(const char *Tag, unsigned Number,
919 const char *Suffix) const {
920 O << TAI->getPrivateGlobalPrefix() << Tag;
921 if (Number) O << Number;
922 O << Suffix;
923 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000924
Jim Laskeyb82313f2007-02-01 16:31:34 +0000925 /// EmitLabel - Emit location label for internal use by Dwarf.
926 ///
927 void EmitLabel(DWLabel Label) const {
928 EmitLabel(Label.Tag, Label.Number);
929 }
930 void EmitLabel(const char *Tag, unsigned Number) const {
931 PrintLabelName(Tag, Number);
932 O << ":\n";
933 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000934
Jim Laskeyb82313f2007-02-01 16:31:34 +0000935 /// EmitReference - Emit a reference to a label.
936 ///
Dan Gohman06ff4e62007-09-28 15:43:33 +0000937 void EmitReference(DWLabel Label, bool IsPCRelative = false,
938 bool Force32Bit = false) const {
939 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000940 }
941 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman06ff4e62007-09-28 15:43:33 +0000942 bool IsPCRelative = false, bool Force32Bit = false) const {
943 PrintRelDirective(Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000944 PrintLabelName(Tag, Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000945
Jim Laskeyb82313f2007-02-01 16:31:34 +0000946 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
947 }
Dan Gohman06ff4e62007-09-28 15:43:33 +0000948 void EmitReference(const std::string &Name, bool IsPCRelative = false,
949 bool Force32Bit = false) const {
950 PrintRelDirective(Force32Bit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000951
Jim Laskeyb82313f2007-02-01 16:31:34 +0000952 O << Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000953
Jim Laskeyb82313f2007-02-01 16:31:34 +0000954 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
955 }
956
957 /// EmitDifference - Emit the difference between two labels. Some
958 /// assemblers do not behave with absolute expressions with data directives,
959 /// so there is an option (needsSet) to use an intermediary set expression.
960 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000961 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000962 EmitDifference(LabelHi.Tag, LabelHi.Number,
963 LabelLo.Tag, LabelLo.Number,
964 IsSmall);
965 }
966 void EmitDifference(const char *TagHi, unsigned NumberHi,
967 const char *TagLo, unsigned NumberLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000968 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000969 if (TAI->needsSet()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000970 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000971 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000972 O << ",";
973 PrintLabelName(TagHi, NumberHi);
974 O << "-";
975 PrintLabelName(TagLo, NumberLo);
976 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000977
978 PrintRelDirective(IsSmall);
Chris Lattner9251f132007-09-24 03:35:37 +0000979 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000980 ++SetCounter;
981 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000982 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000983
Jim Laskeyb82313f2007-02-01 16:31:34 +0000984 PrintLabelName(TagHi, NumberHi);
985 O << "-";
986 PrintLabelName(TagLo, NumberLo);
987 }
988 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000989
990 void EmitSectionOffset(const char* Label, const char* Section,
991 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000992 bool IsSmall = false, bool isEH = false,
993 bool useSet = true) {
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000994 bool printAbsolute = false;
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000995 if (isEH)
996 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
997 else
998 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
999
1000 if (TAI->needsSet() && useSet) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001001 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +00001002 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001003 O << ",";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001004 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001005
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001006 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001007 O << "-";
1008 PrintLabelName(Section, SectionNumber);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001009 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001010 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001011
1012 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001013
Chris Lattner9251f132007-09-24 03:35:37 +00001014 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001015 ++SetCounter;
1016 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001017 PrintRelDirective(IsSmall, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001018
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001019 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001020
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001021 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001022 O << "-";
1023 PrintLabelName(Section, SectionNumber);
1024 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001025 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001026 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001027
Jim Laskeyb82313f2007-02-01 16:31:34 +00001028 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1029 /// frame.
1030 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenb97aec62007-11-13 19:13:01 +00001031 const std::vector<MachineMove> &Moves, bool isEH) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001032 int stackGrowth =
1033 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1034 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00001035 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeybacd3042007-02-21 22:48:45 +00001036 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
Jim Laskeyb82313f2007-02-01 16:31:34 +00001037
1038 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001039 const MachineMove &Move = Moves[i];
Jim Laskeyb82313f2007-02-01 16:31:34 +00001040 unsigned LabelID = Move.getLabelID();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001041
Jim Laskeyb82313f2007-02-01 16:31:34 +00001042 if (LabelID) {
1043 LabelID = MMI->MappedLabel(LabelID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001044
Jim Laskeyb82313f2007-02-01 16:31:34 +00001045 // Throw out move if the label is invalid.
1046 if (!LabelID) continue;
1047 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001048
Jim Laskeyb82313f2007-02-01 16:31:34 +00001049 const MachineLocation &Dst = Move.getDestination();
1050 const MachineLocation &Src = Move.getSource();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001051
Jim Laskeyb82313f2007-02-01 16:31:34 +00001052 // Advance row if new location.
1053 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1054 Asm->EmitInt8(DW_CFA_advance_loc4);
1055 Asm->EOL("DW_CFA_advance_loc4");
Jim Laskeybacd3042007-02-21 22:48:45 +00001056 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1057 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001058
Jim Laskeyb82313f2007-02-01 16:31:34 +00001059 BaseLabelID = LabelID;
Jim Laskeybacd3042007-02-21 22:48:45 +00001060 BaseLabel = "label";
Jim Laskeyb82313f2007-02-01 16:31:34 +00001061 IsLocal = true;
1062 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001063
Jim Laskeyb82313f2007-02-01 16:31:34 +00001064 // If advancing cfa.
Dan Gohmand735b802008-10-03 15:45:36 +00001065 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1066 if (!Src.isReg()) {
1067 if (Src.getReg() == MachineLocation::VirtualFP) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001068 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1069 Asm->EOL("DW_CFA_def_cfa_offset");
1070 } else {
1071 Asm->EmitInt8(DW_CFA_def_cfa);
1072 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmand735b802008-10-03 15:45:36 +00001073 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001074 Asm->EOL("Register");
1075 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001076
Jim Laskeyb82313f2007-02-01 16:31:34 +00001077 int Offset = -Src.getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001078
Jim Laskeyb82313f2007-02-01 16:31:34 +00001079 Asm->EmitULEB128Bytes(Offset);
1080 Asm->EOL("Offset");
1081 } else {
1082 assert(0 && "Machine move no supported yet.");
1083 }
Dan Gohmand735b802008-10-03 15:45:36 +00001084 } else if (Src.isReg() &&
1085 Src.getReg() == MachineLocation::VirtualFP) {
1086 if (Dst.isReg()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001087 Asm->EmitInt8(DW_CFA_def_cfa_register);
1088 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmand735b802008-10-03 15:45:36 +00001089 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001090 Asm->EOL("Register");
1091 } else {
1092 assert(0 && "Machine move no supported yet.");
1093 }
1094 } else {
Dan Gohmand735b802008-10-03 15:45:36 +00001095 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001096 int Offset = Dst.getOffset() / stackGrowth;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001097
Jim Laskeyb82313f2007-02-01 16:31:34 +00001098 if (Offset < 0) {
1099 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1100 Asm->EOL("DW_CFA_offset_extended_sf");
1101 Asm->EmitULEB128Bytes(Reg);
1102 Asm->EOL("Reg");
1103 Asm->EmitSLEB128Bytes(Offset);
1104 Asm->EOL("Offset");
1105 } else if (Reg < 64) {
1106 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Chengd4114192008-07-09 21:53:02 +00001107 if (VerboseAsm)
1108 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1109 else
1110 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00001111 Asm->EmitULEB128Bytes(Offset);
1112 Asm->EOL("Offset");
1113 } else {
1114 Asm->EmitInt8(DW_CFA_offset_extended);
1115 Asm->EOL("DW_CFA_offset_extended");
1116 Asm->EmitULEB128Bytes(Reg);
1117 Asm->EOL("Reg");
1118 Asm->EmitULEB128Bytes(Offset);
1119 Asm->EOL("Offset");
1120 }
1121 }
1122 }
1123 }
1124
Jim Laskey072200c2007-01-29 18:51:14 +00001125};
1126
1127//===----------------------------------------------------------------------===//
Devang Patelf3ee5142009-01-12 22:54:42 +00001128/// SrcLineInfo - This class is used to record source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001129///
1130class SrcLineInfo {
1131 unsigned Line; // Source line number.
1132 unsigned Column; // Source column.
1133 unsigned SourceID; // Source ID number.
1134 unsigned LabelID; // Label in code ID number.
1135public:
1136 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001137 : Line(L), Column(C), SourceID(S), LabelID(I) {}
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001138
1139 // Accessors
1140 unsigned getLine() const { return Line; }
1141 unsigned getColumn() const { return Column; }
1142 unsigned getSourceID() const { return SourceID; }
1143 unsigned getLabelID() const { return LabelID; }
1144};
1145
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001146//===----------------------------------------------------------------------===//
Devang Patel7a6e5a32009-01-08 02:33:41 +00001147/// DbgVariable - This class is used to track local variable information.
1148///
1149class DbgVariable {
Devang Patel99ec3532009-01-16 19:28:14 +00001150 DIVariable Var; // Variable Descriptor.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001151 unsigned FrameIndex; // Variable frame index.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001152public:
Devang Patel99ec3532009-01-16 19:28:14 +00001153 DbgVariable(DIVariable V, unsigned I) : Var(V), FrameIndex(I) {}
Devang Patel7a6e5a32009-01-08 02:33:41 +00001154
1155 // Accessors.
Devang Patel99ec3532009-01-16 19:28:14 +00001156 DIVariable getVariable() const { return Var; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001157 unsigned getFrameIndex() const { return FrameIndex; }
1158};
1159
1160//===----------------------------------------------------------------------===//
1161/// DbgScope - This class is used to track scope information.
1162///
1163class DbgScope {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001164 DbgScope *Parent; // Parent to this scope.
Devang Patel7103c6a2009-01-16 18:01:58 +00001165 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001166 // Either subprogram or block.
1167 unsigned StartLabelID; // Label ID of the beginning of scope.
1168 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel7103c6a2009-01-16 18:01:58 +00001169 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
Devang Patel9795da52009-01-10 02:42:49 +00001170 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001171public:
Devang Patel0e5200f2009-01-15 18:25:17 +00001172 DbgScope(DbgScope *P, DIDescriptor D)
Devang Patel7a6e5a32009-01-08 02:33:41 +00001173 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), Scopes(), Variables()
1174 {}
Devang Patel481ff5b2009-01-12 18:48:36 +00001175 ~DbgScope() {
1176 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1177 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1178 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001179
1180 // Accessors.
Devang Patel7103c6a2009-01-16 18:01:58 +00001181 DbgScope *getParent() const { return Parent; }
1182 DIDescriptor getDesc() const { return Desc; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001183 unsigned getStartLabelID() const { return StartLabelID; }
1184 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel9795da52009-01-10 02:42:49 +00001185 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1186 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001187 void setStartLabelID(unsigned S) { StartLabelID = S; }
1188 void setEndLabelID(unsigned E) { EndLabelID = E; }
1189
1190 /// AddScope - Add a scope to the scope.
1191 ///
1192 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1193
1194 /// AddVariable - Add a variable to the scope.
1195 ///
1196 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
1197};
1198
1199//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001200/// DwarfDebug - Emits Dwarf debug directives.
Jim Laskey072200c2007-01-29 18:51:14 +00001201///
1202class DwarfDebug : public Dwarf {
Jim Laskey65195462006-10-30 13:35:07 +00001203 //===--------------------------------------------------------------------===//
1204 // Attributes used to construct specific Dwarf sections.
1205 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001206
Evan Chenge3d42322009-02-25 07:04:34 +00001207 /// CompileUnitMap - A map of global variables representing compile units to
1208 /// compile units.
1209 DenseMap<Value *, CompileUnit *> CompileUnitMap;
1210
1211 /// CompileUnits - All the compile units in this module.
1212 ///
1213 SmallVector<CompileUnit *, 8> CompileUnits;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001214
Devang Pateldd9db662009-01-30 18:20:31 +00001215 /// MainCU - Some platform prefers one compile unit per .o file. In such
1216 /// cases, all dies are inserted in MainCU.
1217 CompileUnit *MainCU;
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001218
Jim Laskeyef42a012006-11-02 20:12:39 +00001219 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +00001220 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001221 FoldingSet<DIEAbbrev> AbbreviationsSet;
1222
1223 /// Abbreviations - A list of all the unique abbreviations in use.
1224 ///
1225 std::vector<DIEAbbrev *> Abbreviations;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001226
Evan Chenge3d42322009-02-25 07:04:34 +00001227 /// DirectoryIdMap - Directory name to directory id map.
1228 ///
1229 StringMap<unsigned> DirectoryIdMap;
Devang Patel8526cc02009-01-05 22:35:52 +00001230
Evan Chenge3d42322009-02-25 07:04:34 +00001231 /// DirectoryNames - A list of directory names.
1232 SmallVector<std::string, 8> DirectoryNames;
1233
1234 /// SourceFileIdMap - Source file name to source file id map.
1235 ///
1236 StringMap<unsigned> SourceFileIdMap;
1237
1238 /// SourceFileNames - A list of source file names.
1239 SmallVector<std::string, 8> SourceFileNames;
1240
1241 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
1242 /// id mapped to a unique id.
1243 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
1244
1245 /// SourceIds - Reverse map from source id to directory id + file id pair.
1246 ///
1247 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
Devang Patel8526cc02009-01-05 22:35:52 +00001248
Devang Patel07354e52009-01-16 21:07:53 +00001249 /// Lines - List of of source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001250 std::vector<SrcLineInfo> Lines;
1251
Devang Patel07354e52009-01-16 21:07:53 +00001252 /// ValuesSet - Used to uniquely define values.
1253 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001254 FoldingSet<DIEValue> ValuesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001255
Jim Laskeyef42a012006-11-02 20:12:39 +00001256 /// Values - A list of all the unique values in use.
1257 ///
1258 std::vector<DIEValue *> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001259
Jim Laskey65195462006-10-30 13:35:07 +00001260 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +00001261 ///
Jim Laskey65195462006-10-30 13:35:07 +00001262 UniqueVector<std::string> StringPool;
1263
Jim Laskey65195462006-10-30 13:35:07 +00001264 /// SectionMap - Provides a unique id per text section.
1265 ///
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001266 UniqueVector<const Section*> SectionMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001267
Jim Laskey65195462006-10-30 13:35:07 +00001268 /// SectionSourceLines - Tracks line numbers per text section.
1269 ///
Devang Patelf3ee5142009-01-12 22:54:42 +00001270 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Jim Laskey65195462006-10-30 13:35:07 +00001271
Jim Laskeybacd3042007-02-21 22:48:45 +00001272 /// didInitial - Flag to indicate if initial emission has been done.
1273 ///
1274 bool didInitial;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001275
Jim Laskeybacd3042007-02-21 22:48:45 +00001276 /// shouldEmit - Flag to indicate if debug information should be emitted.
1277 ///
1278 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +00001279
Devang Patel0e5200f2009-01-15 18:25:17 +00001280 // RootDbgScope - Top level scope for the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001281 //
1282 DbgScope *RootDbgScope;
1283
Bill Wendling163ba3f2009-03-10 21:23:25 +00001284 /// DbgScopeMap - Tracks the scopes in the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001285 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
Bill Wendling163ba3f2009-03-10 21:23:25 +00001286
1287 /// DebugTimer - Timer for the Dwarf debug writer.
1288 Timer *DebugTimer;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001289
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001290 struct FunctionDebugFrameInfo {
1291 unsigned Number;
1292 std::vector<MachineMove> Moves;
1293
1294 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001295 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001296 };
1297
1298 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001299
Bill Wendling163ba3f2009-03-10 21:23:25 +00001300private:
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001301 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
Bill Wendlingc8615e42009-03-10 21:47:45 +00001302 /// maps to the source id. Source id starts at 1.
1303 std::pair<unsigned, unsigned>
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001304 getSourceDirectoryAndFileIds(unsigned SId) const {
Bill Wendlingc8615e42009-03-10 21:47:45 +00001305 return SourceIds[SId-1];
1306 }
1307
1308 /// getNumSourceDirectories - Return the number of source directories in the
1309 /// debug info.
1310 unsigned getNumSourceDirectories() const {
1311 return DirectoryNames.size();
1312 }
1313
1314 /// getSourceDirectoryName - Return the name of the directory corresponding
1315 /// to the id.
1316 const std::string &getSourceDirectoryName(unsigned Id) const {
1317 return DirectoryNames[Id - 1];
1318 }
1319
1320 /// getSourceFileName - Return the name of the source file corresponding
1321 /// to the id.
1322 const std::string &getSourceFileName(unsigned Id) const {
1323 return SourceFileNames[Id - 1];
1324 }
1325
1326 /// getNumSourceIds - Return the number of unique source ids.
Bill Wendlingc8615e42009-03-10 21:47:45 +00001327 unsigned getNumSourceIds() const {
1328 return SourceIds.size();
1329 }
1330
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001331 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001332 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001333 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1334 // Profile the node so that we can make it unique.
1335 FoldingSetNodeID ID;
1336 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001337
Jim Laskeyef42a012006-11-02 20:12:39 +00001338 // Check the set for priors.
1339 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001340
Jim Laskeyef42a012006-11-02 20:12:39 +00001341 // If it's newly added.
1342 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001343 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +00001344 Abbreviations.push_back(&Abbrev);
1345 // Assign the vector position + 1 as its number.
1346 Abbrev.setNumber(Abbreviations.size());
1347 } else {
1348 // Assign existing abbreviation number.
1349 Abbrev.setNumber(InSet->getNumber());
1350 }
1351 }
1352
Jim Laskey65195462006-10-30 13:35:07 +00001353 /// NewString - Add a string to the constant pool and returns a label.
1354 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001355 DWLabel NewString(const std::string &String) {
1356 unsigned StringID = StringPool.insert(String);
1357 return DWLabel("string", StringID);
1358 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001359
Jim Laskeyef42a012006-11-02 20:12:39 +00001360 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1361 /// entry.
1362 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1363 DIEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001364
Jim Laskeyef42a012006-11-02 20:12:39 +00001365 if (Entry) {
1366 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001367 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001368 void *Where;
1369 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001370
Jim Laskeyf6733882006-11-02 21:48:18 +00001371 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001372
Jim Laskeyef42a012006-11-02 20:12:39 +00001373 Value = new DIEntry(Entry);
1374 ValuesSet.InsertNode(Value, Where);
1375 } else {
1376 Value = new DIEntry(Entry);
1377 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001378
Jim Laskeyef42a012006-11-02 20:12:39 +00001379 Values.push_back(Value);
1380 return Value;
1381 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001382
Jim Laskeyef42a012006-11-02 20:12:39 +00001383 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1384 ///
1385 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1386 Value->Entry = Entry;
1387 // Add to values set if not already there. If it is, we merely have a
1388 // duplicate in the values list (no harm.)
1389 ValuesSet.GetOrInsertNode(Value);
1390 }
1391
1392 /// AddUInt - Add an unsigned integer attribute data and value.
1393 ///
1394 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1395 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1396
1397 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001398 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001399 void *Where;
1400 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1401 if (!Value) {
1402 Value = new DIEInteger(Integer);
1403 ValuesSet.InsertNode(Value, Where);
1404 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001405 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001406
Jim Laskeyef42a012006-11-02 20:12:39 +00001407 Die->AddValue(Attribute, Form, Value);
1408 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001409
Jim Laskeyef42a012006-11-02 20:12:39 +00001410 /// AddSInt - Add an signed integer attribute data and value.
1411 ///
1412 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1413 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1414
1415 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001416 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001417 void *Where;
1418 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1419 if (!Value) {
1420 Value = new DIEInteger(Integer);
1421 ValuesSet.InsertNode(Value, Where);
1422 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001423 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001424
Jim Laskeyef42a012006-11-02 20:12:39 +00001425 Die->AddValue(Attribute, Form, Value);
1426 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001427
Evan Chenge3d42322009-02-25 07:04:34 +00001428 /// AddString - Add a string attribute data and value.
Jim Laskeyef42a012006-11-02 20:12:39 +00001429 ///
1430 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1431 const std::string &String) {
1432 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001433 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001434 void *Where;
1435 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1436 if (!Value) {
1437 Value = new DIEString(String);
1438 ValuesSet.InsertNode(Value, Where);
1439 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001440 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001441
Jim Laskeyef42a012006-11-02 20:12:39 +00001442 Die->AddValue(Attribute, Form, Value);
1443 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001444
Jim Laskeyef42a012006-11-02 20:12:39 +00001445 /// AddLabel - Add a Dwarf label attribute data and value.
1446 ///
1447 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1448 const DWLabel &Label) {
1449 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001450 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001451 void *Where;
1452 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1453 if (!Value) {
1454 Value = new DIEDwarfLabel(Label);
1455 ValuesSet.InsertNode(Value, Where);
1456 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001457 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001458
Jim Laskeyef42a012006-11-02 20:12:39 +00001459 Die->AddValue(Attribute, Form, Value);
1460 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001461
Jim Laskeyef42a012006-11-02 20:12:39 +00001462 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1463 ///
1464 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1465 const std::string &Label) {
1466 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001467 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001468 void *Where;
1469 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1470 if (!Value) {
1471 Value = new DIEObjectLabel(Label);
1472 ValuesSet.InsertNode(Value, Where);
1473 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001474 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001475
Jim Laskeyef42a012006-11-02 20:12:39 +00001476 Die->AddValue(Attribute, Form, Value);
1477 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001478
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001479 /// AddSectionOffset - Add a section offset label attribute data and value.
1480 ///
1481 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1482 const DWLabel &Label, const DWLabel &Section,
1483 bool isEH = false, bool useSet = true) {
1484 FoldingSetNodeID ID;
1485 DIESectionOffset::Profile(ID, Label, Section);
1486 void *Where;
1487 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1488 if (!Value) {
1489 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1490 ValuesSet.InsertNode(Value, Where);
1491 Values.push_back(Value);
1492 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001493
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001494 Die->AddValue(Attribute, Form, Value);
1495 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001496
Jim Laskeyef42a012006-11-02 20:12:39 +00001497 /// AddDelta - Add a label delta attribute data and value.
1498 ///
1499 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1500 const DWLabel &Hi, const DWLabel &Lo) {
1501 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001502 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001503 void *Where;
1504 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1505 if (!Value) {
1506 Value = new DIEDelta(Hi, Lo);
1507 ValuesSet.InsertNode(Value, Where);
1508 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001509 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001510
Jim Laskeyef42a012006-11-02 20:12:39 +00001511 Die->AddValue(Attribute, Form, Value);
1512 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001513
Jim Laskeyef42a012006-11-02 20:12:39 +00001514 /// AddDIEntry - Add a DIE attribute data and value.
1515 ///
1516 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1517 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1518 }
1519
1520 /// AddBlock - Add block data.
1521 ///
1522 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1523 Block->ComputeSize(*this);
1524 FoldingSetNodeID ID;
1525 Block->Profile(ID);
1526 void *Where;
1527 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1528 if (!Value) {
1529 Value = Block;
1530 ValuesSet.InsertNode(Value, Where);
1531 Values.push_back(Value);
1532 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001533 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001534 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001535 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001536 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001537
Jim Laskeyef42a012006-11-02 20:12:39 +00001538 Die->AddValue(Attribute, Block->BestForm(), Value);
1539 }
1540
Jim Laskey65195462006-10-30 13:35:07 +00001541 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001542 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001543 void AddSourceLine(DIE *Die, const DIVariable *V) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001544 unsigned FileID = 0;
1545 unsigned Line = V->getLineNumber();
Devang Pateldd9db662009-01-30 18:20:31 +00001546 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1547 FileID = Unit->getID();
Devang Patel2303df92009-02-10 06:04:08 +00001548 assert (FileID && "Invalid file id");
Devang Patel7a6e5a32009-01-08 02:33:41 +00001549 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1550 AddUInt(Die, DW_AT_decl_line, 0, Line);
1551 }
1552
1553 /// AddSourceLine - Add location information to specified debug information
1554 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001555 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Devang Patel8526cc02009-01-05 22:35:52 +00001556 unsigned FileID = 0;
1557 unsigned Line = G->getLineNumber();
Devang Pateldd9db662009-01-30 18:20:31 +00001558 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1559 FileID = Unit->getID();
Devang Patel2303df92009-02-10 06:04:08 +00001560 assert (FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001561 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1562 AddUInt(Die, DW_AT_decl_line, 0, Line);
1563 }
1564
Devang Patel99ec3532009-01-16 19:28:14 +00001565 void AddSourceLine(DIE *Die, const DIType *Ty) {
Devang Patel8526cc02009-01-05 22:35:52 +00001566 unsigned FileID = 0;
Devang Patel99ec3532009-01-16 19:28:14 +00001567 unsigned Line = Ty->getLineNumber();
Devang Pateldd9db662009-01-30 18:20:31 +00001568 DICompileUnit CU = Ty->getCompileUnit();
1569 if (CU.isNull())
1570 return;
1571 CompileUnit *Unit = FindCompileUnit(CU);
1572 FileID = Unit->getID();
Devang Patel2303df92009-02-10 06:04:08 +00001573 assert (FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001574 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1575 AddUInt(Die, DW_AT_decl_line, 0, Line);
1576 }
1577
Jim Laskey65195462006-10-30 13:35:07 +00001578 /// AddAddress - Add an address attribute to a die based on the location
1579 /// provided.
1580 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001581 const MachineLocation &Location) {
Dan Gohmand735b802008-10-03 15:45:36 +00001582 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001583 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001584
Dan Gohmand735b802008-10-03 15:45:36 +00001585 if (Location.isReg()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001586 if (Reg < 32) {
1587 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1588 } else {
1589 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1590 AddUInt(Block, 0, DW_FORM_udata, Reg);
1591 }
1592 } else {
1593 if (Reg < 32) {
1594 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1595 } else {
1596 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1597 AddUInt(Block, 0, DW_FORM_udata, Reg);
1598 }
1599 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1600 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001601
Jim Laskeyef42a012006-11-02 20:12:39 +00001602 AddBlock(Die, Attribute, 0, Block);
1603 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001604
Jim Laskeyef42a012006-11-02 20:12:39 +00001605 /// AddType - Add a new type attribute to the specified entity.
Devang Patel48d190f2009-01-05 21:47:57 +00001606 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel80303aa2009-01-23 19:13:31 +00001607 if (Ty.isNull())
Devang Patel48d190f2009-01-05 21:47:57 +00001608 return;
Devang Patel48d190f2009-01-05 21:47:57 +00001609
1610 // Check for pre-existence.
1611 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1612 // If it exists then use the existing value.
1613 if (Slot) {
1614 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1615 return;
1616 }
1617
1618 // Set up proxy.
1619 Slot = NewDIEntry();
1620
1621 // Construct type.
1622 DIE Buffer(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001623 if (Ty.isBasicType(Ty.getTag()))
1624 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1625 else if (Ty.isDerivedType(Ty.getTag()))
1626 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1627 else {
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001628 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelf193ff02009-01-15 19:26:23 +00001629 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1630 }
1631
Devang Patel7009d242009-01-27 23:22:55 +00001632 // Add debug information entry to entity and appropriate context.
1633 DIE *Die = NULL;
1634 DIDescriptor Context = Ty.getContext();
1635 if (!Context.isNull())
1636 Die = DW_Unit->getDieMapSlotFor(Context.getGV());
1637
1638 if (Die) {
1639 DIE *Child = new DIE(Buffer);
1640 Die->AddChild(Child);
1641 Buffer.Detach();
1642 SetDIEntry(Slot, Child);
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001643 } else {
Devang Patel7009d242009-01-27 23:22:55 +00001644 Die = DW_Unit->AddDie(Buffer);
1645 SetDIEntry(Slot, Die);
1646 }
1647
Devang Patel48d190f2009-01-05 21:47:57 +00001648 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1649 }
1650
Devang Patele5202732009-01-05 19:07:53 +00001651 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1652 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001653 DIBasicType BTy) {
Devang Patel08f053f2009-01-05 17:57:47 +00001654
1655 // Get core information.
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001656 std::string Name;
1657 BTy.getName(Name);
Devang Patel08f053f2009-01-05 17:57:47 +00001658 Buffer.setTag(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001659 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patel08f053f2009-01-05 17:57:47 +00001660 // Add name if not anonymous or intermediate type.
1661 if (!Name.empty())
1662 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001663 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel08f053f2009-01-05 17:57:47 +00001664 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1665 }
1666
Devang Patele5202732009-01-05 19:07:53 +00001667 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1668 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001669 DIDerivedType DTy) {
Devang Patel08f053f2009-01-05 17:57:47 +00001670
1671 // Get core information.
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001672 std::string Name;
1673 DTy.getName(Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001674 uint64_t Size = DTy.getSizeInBits() >> 3;
1675 unsigned Tag = DTy.getTag();
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001676
Devang Patel08f053f2009-01-05 17:57:47 +00001677 // FIXME - Workaround for templates.
1678 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1679
1680 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001681
Devang Patel08f053f2009-01-05 17:57:47 +00001682 // Map to main type, void will not have a type.
Devang Patelf193ff02009-01-15 19:26:23 +00001683 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00001684 AddType(DW_Unit, &Buffer, FromTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001685
1686 // Add name if not anonymous or intermediate type.
Evan Chenge3d42322009-02-25 07:04:34 +00001687 if (!Name.empty())
1688 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel08f053f2009-01-05 17:57:47 +00001689
1690 // Add size if non-zero (derived types might be zero-sized.)
1691 if (Size)
1692 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1693
1694 // Add source line info if available and TyDesc is not a forward
1695 // declaration.
Devang Patelad165be2009-01-27 00:45:04 +00001696 if (!DTy.isForwardDecl())
1697 AddSourceLine(&Buffer, &DTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001698 }
1699
Devang Patelf4215332009-01-05 19:55:51 +00001700 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1701 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001702 DICompositeType CTy) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001703 // Get core information.
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001704 std::string Name;
1705 CTy.getName(Name);
1706
Devang Patelf193ff02009-01-15 19:26:23 +00001707 uint64_t Size = CTy.getSizeInBits() >> 3;
1708 unsigned Tag = CTy.getTag();
Devang Patel49f38cb2009-01-23 01:19:09 +00001709 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001710
Devang Patelf4215332009-01-05 19:55:51 +00001711 switch (Tag) {
1712 case DW_TAG_vector_type:
1713 case DW_TAG_array_type:
Devang Patelf193ff02009-01-15 19:26:23 +00001714 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001715 break;
Devang Pateleab4a2e2009-01-20 18:35:14 +00001716 case DW_TAG_enumeration_type:
1717 {
1718 DIArray Elements = CTy.getTypeArray();
1719 // Add enumerators to enumeration type.
1720 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1721 DIE *ElemDie = NULL;
1722 DIEnumerator Enum(Elements.getElement(i).getGV());
1723 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
1724 Buffer.AddChild(ElemDie);
1725 }
1726 }
1727 break;
Devang Patelf4215332009-01-05 19:55:51 +00001728 case DW_TAG_subroutine_type:
1729 {
1730 // Add prototype flag.
1731 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patelf193ff02009-01-15 19:26:23 +00001732 DIArray Elements = CTy.getTypeArray();
Devang Patelf4215332009-01-05 19:55:51 +00001733 // Add return type.
Devang Patel48d190f2009-01-05 21:47:57 +00001734 DIDescriptor RTy = Elements.getElement(0);
Devang Patelf193ff02009-01-15 19:26:23 +00001735 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel48d190f2009-01-05 21:47:57 +00001736
Devang Patelf4215332009-01-05 19:55:51 +00001737 // Add arguments.
1738 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1739 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00001740 DIDescriptor Ty = Elements.getElement(i);
Devang Patel7ab24502009-01-17 06:57:25 +00001741 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patelf4215332009-01-05 19:55:51 +00001742 Buffer.AddChild(Arg);
1743 }
1744 }
1745 break;
1746 case DW_TAG_structure_type:
1747 case DW_TAG_union_type:
1748 {
1749 // Add elements to structure type.
Devang Patelf193ff02009-01-15 19:26:23 +00001750 DIArray Elements = CTy.getTypeArray();
Devang Patel153745c2009-01-16 00:50:53 +00001751
1752 // A forward struct declared type may not have elements available.
1753 if (Elements.isNull())
1754 break;
1755
Devang Patelf4215332009-01-05 19:55:51 +00001756 // Add elements to structure type.
1757 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1758 DIDescriptor Element = Elements.getElement(i);
Devang Patel5aac3d32009-01-17 08:01:33 +00001759 DIE *ElemDie = NULL;
Devang Patelf193ff02009-01-15 19:26:23 +00001760 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel2d1768c2009-01-17 08:05:14 +00001761 ElemDie = CreateSubprogramDIE(DW_Unit,
1762 DISubprogram(Element.getGV()));
Devang Patel5aac3d32009-01-17 08:01:33 +00001763 else if (Element.getTag() == dwarf::DW_TAG_variable) // ???
1764 ElemDie = CreateGlobalVariableDIE(DW_Unit,
1765 DIGlobalVariable(Element.getGV()));
Devang Patel2be58932009-01-20 21:02:02 +00001766 else
1767 ElemDie = CreateMemberDIE(DW_Unit,
1768 DIDerivedType(Element.getGV()));
Devang Patel2d1768c2009-01-17 08:05:14 +00001769 Buffer.AddChild(ElemDie);
Devang Patelf4215332009-01-05 19:55:51 +00001770 }
Devang Patel13319ce2009-02-17 22:43:44 +00001771 unsigned RLang = CTy.getRunTimeLang();
1772 if (RLang)
1773 AddUInt(&Buffer, DW_AT_APPLE_runtime_class, DW_FORM_data1, RLang);
Devang Patelf4215332009-01-05 19:55:51 +00001774 }
1775 break;
1776 default:
1777 break;
1778 }
1779
1780 // Add name if not anonymous or intermediate type.
Evan Chenge3d42322009-02-25 07:04:34 +00001781 if (!Name.empty())
1782 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf4215332009-01-05 19:55:51 +00001783
Devang Patelad165be2009-01-27 00:45:04 +00001784 if (Tag == DW_TAG_enumeration_type || Tag == DW_TAG_structure_type
1785 || Tag == DW_TAG_union_type) {
1786 // Add size if non-zero (derived types might be zero-sized.)
1787 if (Size)
1788 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1789 else {
1790 // Add zero size if it is not a forward declaration.
1791 if (CTy.isForwardDecl())
1792 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1793 else
1794 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1795 }
1796
1797 // Add source line info if available.
1798 if (!CTy.isForwardDecl())
1799 AddSourceLine(&Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001800 }
Devang Patelf4215332009-01-05 19:55:51 +00001801 }
1802
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001803 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1804 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelf193ff02009-01-15 19:26:23 +00001805 int64_t L = SR.getLo();
1806 int64_t H = SR.getHi();
Devang Patel68afdc32009-01-05 18:33:01 +00001807 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1808 if (L != H) {
1809 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1810 if (L)
Devang Patel2d1768c2009-01-17 08:05:14 +00001811 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1812 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel68afdc32009-01-05 18:33:01 +00001813 }
1814 Buffer.AddChild(DW_Subrange);
1815 }
1816
1817 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1818 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1819 DICompositeType *CTy) {
1820 Buffer.setTag(DW_TAG_array_type);
1821 if (CTy->getTag() == DW_TAG_vector_type)
1822 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1823
Devang Patelf9235742009-01-28 21:08:20 +00001824 // Emit derived type.
1825 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel68afdc32009-01-05 18:33:01 +00001826 DIArray Elements = CTy->getTypeArray();
Devang Patel68afdc32009-01-05 18:33:01 +00001827
1828 // Construct an anonymous type for index type.
1829 DIE IdxBuffer(DW_TAG_base_type);
1830 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1831 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1832 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1833
1834 // Add subranges to array type.
1835 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patelf4215332009-01-05 19:55:51 +00001836 DIDescriptor Element = Elements.getElement(i);
Devang Patelf193ff02009-01-15 19:26:23 +00001837 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1838 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel68afdc32009-01-05 18:33:01 +00001839 }
1840 }
1841
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001842 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Pateleab4a2e2009-01-20 18:35:14 +00001843 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patelc69bf2c2009-01-05 18:38:38 +00001844
1845 DIE *Enumerator = new DIE(DW_TAG_enumerator);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001846 std::string Name;
1847 ETy->getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001848 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
Devang Patelc69bf2c2009-01-05 18:38:38 +00001849 int64_t Value = ETy->getEnumValue();
1850 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Pateleab4a2e2009-01-20 18:35:14 +00001851 return Enumerator;
Devang Patelc69bf2c2009-01-05 18:38:38 +00001852 }
Devang Patel68afdc32009-01-05 18:33:01 +00001853
Devang Patel5aac3d32009-01-17 08:01:33 +00001854 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001855 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patel5aac3d32009-01-17 08:01:33 +00001856 {
1857 DIE *GVDie = new DIE(DW_TAG_variable);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001858 std::string Name;
1859 GV.getDisplayName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001860 AddString(GVDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001861 std::string LinkageName;
1862 GV.getLinkageName(LinkageName);
Devang Patel86ae1422009-01-05 18:59:44 +00001863 if (!LinkageName.empty())
Devang Patel5aac3d32009-01-17 08:01:33 +00001864 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1865 AddType(DW_Unit, GVDie, GV.getType());
1866 if (!GV.isLocalToUnit())
1867 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
1868 AddSourceLine(GVDie, &GV);
1869 return GVDie;
Devang Patel86ae1422009-01-05 18:59:44 +00001870 }
1871
Devang Patel2be58932009-01-20 21:02:02 +00001872 /// CreateMemberDIE - Create new member DIE.
1873 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
1874 DIE *MemberDie = new DIE(DT.getTag());
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001875 std::string Name;
1876 DT.getName(Name);
Devang Patel2be58932009-01-20 21:02:02 +00001877 if (!Name.empty())
1878 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1879
1880 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1881
1882 AddSourceLine(MemberDie, &DT);
1883
Devang Patel36375ee2009-02-17 21:23:59 +00001884 uint64_t Size = DT.getSizeInBits();
1885 uint64_t FieldSize = DT.getOriginalTypeSize();
1886
1887 if (Size != FieldSize) {
1888 // Handle bitfield.
1889 AddUInt(MemberDie, DW_AT_byte_size, 0, DT.getOriginalTypeSize() >> 3);
1890 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1891
1892 uint64_t Offset = DT.getOffsetInBits();
1893 uint64_t FieldOffset = Offset;
1894 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1895 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1896 FieldOffset = (HiMark - FieldSize);
1897 Offset -= FieldOffset;
1898 // Maybe we need to work from the other end.
1899 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1900 AddUInt(MemberDie, DW_AT_bit_offset, 0, Offset);
1901 }
Devang Patel2be58932009-01-20 21:02:02 +00001902 DIEBlock *Block = new DIEBlock();
1903 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1904 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1905 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1906
Devang Patel47661592009-01-21 00:08:04 +00001907 if (DT.isProtected())
1908 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1909 else if (DT.isPrivate())
1910 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1911
Devang Patel2be58932009-01-20 21:02:02 +00001912 return MemberDie;
1913 }
1914
Devang Patel5aac3d32009-01-17 08:01:33 +00001915 /// CreateSubprogramDIE - Create new DIE using SP.
1916 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Devang Patel2d1768c2009-01-17 08:05:14 +00001917 const DISubprogram &SP,
1918 bool IsConstructor = false) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001919 DIE *SPDie = new DIE(DW_TAG_subprogram);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001920 std::string Name;
1921 SP.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001922 AddString(SPDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001923 std::string LinkageName;
1924 SP.getLinkageName(LinkageName);
Devang Patel86ae1422009-01-05 18:59:44 +00001925 if (!LinkageName.empty())
Devang Patel5aac3d32009-01-17 08:01:33 +00001926 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Devang Patel2d1768c2009-01-17 08:05:14 +00001927 LinkageName);
Devang Patel5aac3d32009-01-17 08:01:33 +00001928 AddSourceLine(SPDie, &SP);
Devang Patel86ae1422009-01-05 18:59:44 +00001929
Devang Patel5aac3d32009-01-17 08:01:33 +00001930 DICompositeType SPTy = SP.getType();
1931 DIArray Args = SPTy.getTypeArray();
1932
Devang Patel86ae1422009-01-05 18:59:44 +00001933 // Add Return Type.
Devang Patel9ac08d62009-02-27 18:05:21 +00001934 if (!IsConstructor) {
1935 if (Args.isNull())
1936 AddType(DW_Unit, SPDie, SPTy);
1937 else
1938 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
1939 }
Devang Pateld234e592009-01-30 01:21:46 +00001940
Devang Pateld5863dd2009-02-02 17:51:41 +00001941 if (!SP.isDefinition()) {
1942 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
1943 // Add arguments.
1944 // Do not add arguments for subprogram definition. They will be
1945 // handled through RecordVariable.
1946 if (!Args.isNull())
1947 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1948 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1949 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
1950 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
1951 SPDie->AddChild(Arg);
1952 }
1953 }
Devang Pateld234e592009-01-30 01:21:46 +00001954
Devang Patel002ec142009-02-24 00:52:19 +00001955 unsigned Lang = SP.getCompileUnit().getLanguage();
1956 if (Lang == DW_LANG_C99 || Lang == DW_LANG_C89
1957 || Lang == DW_LANG_ObjC)
1958 AddUInt(SPDie, DW_AT_prototyped, DW_FORM_flag, 1);
1959
Devang Patelf193ff02009-01-15 19:26:23 +00001960 if (!SP.isLocalToUnit())
Devang Pateld234e592009-01-30 01:21:46 +00001961 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Devang Patel5aac3d32009-01-17 08:01:33 +00001962 return SPDie;
Devang Patel86ae1422009-01-05 18:59:44 +00001963 }
1964
Devang Patel5aac3d32009-01-17 08:01:33 +00001965 /// FindCompileUnit - Get the compile unit for the given descriptor.
1966 ///
Devang Patel8526cc02009-01-05 22:35:52 +00001967 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
Evan Chenge3d42322009-02-25 07:04:34 +00001968 CompileUnit *DW_Unit = CompileUnitMap[Unit.getGV()];
Devang Patel8526cc02009-01-05 22:35:52 +00001969 assert(DW_Unit && "Missing compile unit.");
1970 return DW_Unit;
1971 }
1972
Devang Patelbbdc8202009-01-13 23:54:55 +00001973 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001974 ///
1975 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1976 // Get the descriptor.
Devang Patel99ec3532009-01-16 19:28:14 +00001977 const DIVariable &VD = DV->getVariable();
Devang Patel7a6e5a32009-01-08 02:33:41 +00001978
1979 // Translate tag to proper Dwarf tag. The result variable is dropped for
1980 // now.
1981 unsigned Tag;
Devang Patel99ec3532009-01-16 19:28:14 +00001982 switch (VD.getTag()) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001983 case DW_TAG_return_variable: return NULL;
1984 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1985 case DW_TAG_auto_variable: // fall thru
1986 default: Tag = DW_TAG_variable; break;
1987 }
1988
1989 // Define variable debug information entry.
1990 DIE *VariableDie = new DIE(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001991 std::string Name;
1992 VD.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001993 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Devang Patel7a6e5a32009-01-08 02:33:41 +00001994
1995 // Add source line info if available.
Devang Patel99ec3532009-01-16 19:28:14 +00001996 AddSourceLine(VariableDie, &VD);
Devang Patel7a6e5a32009-01-08 02:33:41 +00001997
1998 // Add variable type.
Devang Patel99ec3532009-01-16 19:28:14 +00001999 AddType(Unit, VariableDie, VD.getType());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002000
2001 // Add variable address.
2002 MachineLocation Location;
2003 Location.set(RI->getFrameRegister(*MF),
2004 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
2005 AddAddress(VariableDie, DW_AT_location, Location);
2006
2007 return VariableDie;
2008 }
2009
Devang Patel7a6e5a32009-01-08 02:33:41 +00002010 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2011 ///
2012 DbgScope *getOrCreateScope(GlobalVariable *V) {
2013 DbgScope *&Slot = DbgScopeMap[V];
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002014 if (Slot) return Slot;
2015
2016 // FIXME - breaks down when the context is an inlined function.
2017 DIDescriptor ParentDesc;
2018 DIDescriptor Desc(V);
2019
2020 if (Desc.getTag() == dwarf::DW_TAG_lexical_block) {
2021 DIBlock Block(V);
2022 ParentDesc = Block.getContext();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002023 }
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002024
2025 DbgScope *Parent = ParentDesc.isNull() ?
2026 NULL : getOrCreateScope(ParentDesc.getGV());
2027 Slot = new DbgScope(Parent, Desc);
2028
2029 if (Parent) {
2030 Parent->AddScope(Slot);
2031 } else if (RootDbgScope) {
2032 // FIXME - Add inlined function scopes to the root so we can delete them
2033 // later. Long term, handle inlined functions properly.
2034 RootDbgScope->AddScope(Slot);
2035 } else {
2036 // First function is top level function.
2037 RootDbgScope = Slot;
2038 }
2039
Devang Patel7a6e5a32009-01-08 02:33:41 +00002040 return Slot;
2041 }
2042
2043 /// ConstructDbgScope - Construct the components of a scope.
2044 ///
2045 void ConstructDbgScope(DbgScope *ParentScope,
2046 unsigned ParentStartID, unsigned ParentEndID,
2047 DIE *ParentDie, CompileUnit *Unit) {
2048 // Add variables to scope.
Devang Patel9795da52009-01-10 02:42:49 +00002049 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002050 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2051 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2052 if (VariableDie) ParentDie->AddChild(VariableDie);
2053 }
2054
2055 // Add nested scopes.
Devang Patel9795da52009-01-10 02:42:49 +00002056 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002057 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2058 // Define the Scope debug information entry.
2059 DbgScope *Scope = Scopes[j];
2060 // FIXME - Ignore inlined functions for the time being.
2061 if (!Scope->getParent()) continue;
2062
Devang Patele9bfb0f2009-01-12 18:41:00 +00002063 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2064 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002065
2066 // Ignore empty scopes.
2067 if (StartID == EndID && StartID != 0) continue;
2068 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
2069
2070 if (StartID == ParentStartID && EndID == ParentEndID) {
2071 // Just add stuff to the parent scope.
2072 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2073 } else {
2074 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2075
2076 // Add the scope bounds.
2077 if (StartID) {
2078 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2079 DWLabel("label", StartID));
2080 } else {
2081 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2082 DWLabel("func_begin", SubprogramCount));
2083 }
2084 if (EndID) {
2085 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2086 DWLabel("label", EndID));
2087 } else {
2088 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2089 DWLabel("func_end", SubprogramCount));
2090 }
2091
2092 // Add the scope contents.
2093 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2094 ParentDie->AddChild(ScopeDie);
2095 }
2096 }
2097 }
2098
2099 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2100 ///
2101 void ConstructRootDbgScope(DbgScope *RootScope) {
2102 // Exit if there is no root scope.
2103 if (!RootScope) return;
Devang Patel0e5200f2009-01-15 18:25:17 +00002104 DIDescriptor Desc = RootScope->getDesc();
2105 if (Desc.isNull())
2106 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002107
2108 // Get the subprogram debug information entry.
Devang Patel0e5200f2009-01-15 18:25:17 +00002109 DISubprogram SPD(Desc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002110
2111 // Get the compile unit context.
Devang Pateldd9db662009-01-30 18:20:31 +00002112 CompileUnit *Unit = MainCU;
2113 if (!Unit)
2114 Unit = FindCompileUnit(SPD.getCompileUnit());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002115
2116 // Get the subprogram die.
Devang Patelf6bac3e2009-01-12 22:58:14 +00002117 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002118 assert(SPDie && "Missing subprogram descriptor");
2119
2120 // Add the function bounds.
2121 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2122 DWLabel("func_begin", SubprogramCount));
2123 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2124 DWLabel("func_end", SubprogramCount));
2125 MachineLocation Location(RI->getFrameRegister(*MF));
2126 AddAddress(SPDie, DW_AT_frame_base, Location);
2127
2128 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2129 }
2130
2131 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2132 ///
2133 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Chenge3d42322009-02-25 07:04:34 +00002134 const char *FnName = MF->getFunction()->getNameStart();
2135 if (MainCU) {
2136 std::map<std::string, DIE*> &Globals = MainCU->getGlobals();
2137 std::map<std::string, DIE*>::iterator GI = Globals.find(FnName);
2138 if (GI != Globals.end()) {
2139 DIE *SPDie = GI->second;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002140
2141 // Add the function bounds.
2142 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2143 DWLabel("func_begin", SubprogramCount));
2144 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2145 DWLabel("func_end", SubprogramCount));
2146
2147 MachineLocation Location(RI->getFrameRegister(*MF));
2148 AddAddress(SPDie, DW_AT_frame_base, Location);
2149 return;
2150 }
Evan Chenge3d42322009-02-25 07:04:34 +00002151 } else {
2152 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2153 CompileUnit *Unit = CompileUnits[i];
2154 std::map<std::string, DIE*> &Globals = Unit->getGlobals();
2155 std::map<std::string, DIE*>::iterator GI = Globals.find(FnName);
2156 if (GI != Globals.end()) {
2157 DIE *SPDie = GI->second;
2158
2159 // Add the function bounds.
2160 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2161 DWLabel("func_begin", SubprogramCount));
2162 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2163 DWLabel("func_end", SubprogramCount));
2164
2165 MachineLocation Location(RI->getFrameRegister(*MF));
2166 AddAddress(SPDie, DW_AT_frame_base, Location);
2167 return;
2168 }
2169 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00002170 }
Evan Chenge3d42322009-02-25 07:04:34 +00002171
Devang Patel7a6e5a32009-01-08 02:33:41 +00002172#if 0
2173 // FIXME: This is causing an abort because C++ mangled names are compared
2174 // with their unmangled counterparts. See PR2885. Don't do this assert.
2175 assert(0 && "Couldn't find DIE for machine function!");
2176#endif
Evan Chenge3d42322009-02-25 07:04:34 +00002177 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002178 }
2179
Jim Laskeyef42a012006-11-02 20:12:39 +00002180 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2181 /// tools to recognize the object file contains Dwarf information.
2182 void EmitInitial() {
2183 // Check to see if we already emitted intial headers.
2184 if (didInitial) return;
2185 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002186
Jim Laskeyef42a012006-11-02 20:12:39 +00002187 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002188 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002189 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002190 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002191 }
2192 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2193 EmitLabel("section_info", 0);
2194 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2195 EmitLabel("section_abbrev", 0);
2196 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2197 EmitLabel("section_aranges", 0);
Scott Michel210de722009-01-26 22:32:51 +00002198 if (TAI->doesSupportMacInfoSection()) {
2199 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2200 EmitLabel("section_macinfo", 0);
2201 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002202 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2203 EmitLabel("section_line", 0);
2204 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2205 EmitLabel("section_loc", 0);
2206 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2207 EmitLabel("section_pubnames", 0);
2208 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2209 EmitLabel("section_str", 0);
2210 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2211 EmitLabel("section_ranges", 0);
2212
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002213 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002214 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002215 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002216 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002217 }
2218
Jim Laskey65195462006-10-30 13:35:07 +00002219 /// EmitDIE - Recusively Emits a debug information entry.
2220 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002221 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002222 // Get the abbreviation for this DIE.
2223 unsigned AbbrevNumber = Die->getAbbrevNumber();
2224 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002225
Jim Laskeybacd3042007-02-21 22:48:45 +00002226 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002227
2228 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002229 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002230
2231 if (VerboseAsm)
2232 Asm->EOL(std::string("Abbrev [" +
2233 utostr(AbbrevNumber) +
2234 "] 0x" + utohexstr(Die->getOffset()) +
2235 ":0x" + utohexstr(Die->getSize()) + " " +
2236 TagString(Abbrev->getTag())));
2237 else
2238 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002239
Owen Anderson873e1b52008-06-24 21:44:59 +00002240 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2241 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002242
Jim Laskeyef42a012006-11-02 20:12:39 +00002243 // Emit the DIE attribute values.
2244 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2245 unsigned Attr = AbbrevData[i].getAttribute();
2246 unsigned Form = AbbrevData[i].getForm();
2247 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002248
Jim Laskeyef42a012006-11-02 20:12:39 +00002249 switch (Attr) {
2250 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002251 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002252 break;
2253 }
2254 default: {
2255 // Emit an attribute using the defined form.
2256 Values[i]->EmitValue(*this, Form);
2257 break;
2258 }
2259 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002260
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002261 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002262 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002263
Jim Laskeyef42a012006-11-02 20:12:39 +00002264 // Emit the DIE children if any.
2265 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2266 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002267
Jim Laskeyef42a012006-11-02 20:12:39 +00002268 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2269 EmitDIE(Children[j]);
2270 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002271
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002272 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002273 }
2274 }
2275
Jim Laskey65195462006-10-30 13:35:07 +00002276 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2277 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002278 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2279 // Get the children.
2280 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002281
Jim Laskeyef42a012006-11-02 20:12:39 +00002282 // If not last sibling and has children then add sibling offset attribute.
2283 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2284
2285 // Record the abbreviation.
2286 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002287
Jim Laskeyef42a012006-11-02 20:12:39 +00002288 // Get the abbreviation for this DIE.
2289 unsigned AbbrevNumber = Die->getAbbrevNumber();
2290 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2291
2292 // Set DIE offset
2293 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002294
Jim Laskeyef42a012006-11-02 20:12:39 +00002295 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002296 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2297
Owen Anderson873e1b52008-06-24 21:44:59 +00002298 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2299 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002300
2301 // Size the DIE attribute values.
2302 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2303 // Size attribute value.
2304 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2305 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002306
Jim Laskeyef42a012006-11-02 20:12:39 +00002307 // Size the DIE children if any.
2308 if (!Children.empty()) {
2309 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2310 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002311
Jim Laskeyef42a012006-11-02 20:12:39 +00002312 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2313 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2314 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002315
Jim Laskeyef42a012006-11-02 20:12:39 +00002316 // End of children marker.
2317 Offset += sizeof(int8_t);
2318 }
2319
2320 Die->setSize(Offset - Die->getOffset());
2321 return Offset;
2322 }
Jim Laskey65195462006-10-30 13:35:07 +00002323
2324 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2325 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002326 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002327 // Process base compile unit.
Devang Pateldd9db662009-01-30 18:20:31 +00002328 if (MainCU) {
2329 // Compute size of compile unit header
2330 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2331 sizeof(int16_t) + // DWARF version number
2332 sizeof(int32_t) + // Offset Into Abbrev. Section
2333 sizeof(int8_t); // Pointer Size (in bytes)
2334 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
2335 return;
2336 }
Evan Chenge3d42322009-02-25 07:04:34 +00002337 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2338 CompileUnit *Unit = CompileUnits[i];
Devang Patel72b66352009-01-12 23:05:55 +00002339 // Compute size of compile unit header
2340 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2341 sizeof(int16_t) + // DWARF version number
2342 sizeof(int32_t) + // Offset Into Abbrev. Section
2343 sizeof(int8_t); // Pointer Size (in bytes)
2344 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2345 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002346 }
2347
Evan Chenge3d42322009-02-25 07:04:34 +00002348 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Jim Laskey65195462006-10-30 13:35:07 +00002349 ///
Evan Chenge3d42322009-02-25 07:04:34 +00002350 void EmitDebugInfoPerCU(CompileUnit *Unit) {
2351 DIE *Die = Unit->getDie();
2352 // Emit the compile units header.
2353 EmitLabel("info_begin", Unit->getID());
2354 // Emit size of content not including length itself
2355 unsigned ContentSize = Die->getSize() +
2356 sizeof(int16_t) + // DWARF version number
2357 sizeof(int32_t) + // Offset Into Abbrev. Section
2358 sizeof(int8_t) + // Pointer Size (in bytes)
2359 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2360
2361 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2362 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2363 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2364 Asm->EOL("Offset Into Abbrev. Section");
2365 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2366
2367 EmitDIE(Die);
2368 // FIXME - extra padding for gdb bug.
2369 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2370 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2371 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2372 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2373 EmitLabel("info_end", Unit->getID());
2374
2375 Asm->EOL();
2376 }
2377
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002378 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002379 // Start debug info section.
2380 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002381
Evan Chenge3d42322009-02-25 07:04:34 +00002382 if (MainCU) {
2383 EmitDebugInfoPerCU(MainCU);
2384 return;
Devang Patel72b66352009-01-12 23:05:55 +00002385 }
Evan Chenge3d42322009-02-25 07:04:34 +00002386
2387 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2388 EmitDebugInfoPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002389 }
2390
Jim Laskey65195462006-10-30 13:35:07 +00002391 /// EmitAbbreviations - Emit the abbreviation section.
2392 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002393 void EmitAbbreviations() const {
2394 // Check to see if it is worth the effort.
2395 if (!Abbreviations.empty()) {
2396 // Start the debug abbrev section.
2397 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002398
Jim Laskeyef42a012006-11-02 20:12:39 +00002399 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002400
Jim Laskeyef42a012006-11-02 20:12:39 +00002401 // For each abbrevation.
2402 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2403 // Get abbreviation data
2404 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002405
Jim Laskeyef42a012006-11-02 20:12:39 +00002406 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002407 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2408 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002409
Jim Laskeyef42a012006-11-02 20:12:39 +00002410 // Emit the abbreviations data.
2411 Abbrev->Emit(*this);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002412
Jim Laskeybacd3042007-02-21 22:48:45 +00002413 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002414 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002415
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002416 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00002417 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002418
Jim Laskeyef42a012006-11-02 20:12:39 +00002419 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002420
Jim Laskeybacd3042007-02-21 22:48:45 +00002421 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002422 }
2423 }
2424
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002425 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2426 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002427 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002428 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2429 // Define last address of section.
2430 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2431 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2432 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2433 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2434
2435 // Mark end of matrix.
2436 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2437 Asm->EmitULEB128Bytes(1); Asm->EOL();
2438 Asm->EmitInt8(1); Asm->EOL();
2439 }
2440
Jim Laskey65195462006-10-30 13:35:07 +00002441 /// EmitDebugLines - Emit source line information.
2442 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002443 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002444 // If the target is using .loc/.file, the assembler will be emitting the
2445 // .debug_line table automatically.
2446 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00002447 return;
2448
Jim Laskeyef42a012006-11-02 20:12:39 +00002449 // Minimum line delta, thus ranging from -10..(255-10).
2450 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2451 // Maximum line delta, thus ranging from -10..(255-10).
2452 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002453
Jim Laskeyef42a012006-11-02 20:12:39 +00002454 // Start the dwarf line section.
2455 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002456
Jim Laskeyef42a012006-11-02 20:12:39 +00002457 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002458
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002459 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002460 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002461 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002462
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002463 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002464
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002465 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002466 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002467 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002468
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002469 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002470
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002471 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002472
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002473 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002474
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002475 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002476
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002477 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002478
Jim Laskeyef42a012006-11-02 20:12:39 +00002479 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002480 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2481 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2482 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2483 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2484 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2485 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2486 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2487 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2488 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00002489
Jim Laskeyef42a012006-11-02 20:12:39 +00002490 // Emit directories.
Evan Chenge3d42322009-02-25 07:04:34 +00002491 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2492 Asm->EmitString(getSourceDirectoryName(DI));
2493 Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00002494 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002495 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002496
Jim Laskeyef42a012006-11-02 20:12:39 +00002497 // Emit files.
Evan Chenge3d42322009-02-25 07:04:34 +00002498 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2499 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002500 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Evan Chenge3d42322009-02-25 07:04:34 +00002501 Asm->EmitString(getSourceFileName(Id.second));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002502 Asm->EOL("Source");
Evan Chenge3d42322009-02-25 07:04:34 +00002503 Asm->EmitULEB128Bytes(Id.first);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002504 Asm->EOL("Directory #");
2505 Asm->EmitULEB128Bytes(0);
2506 Asm->EOL("Mod date");
2507 Asm->EmitULEB128Bytes(0);
2508 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002509 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002510 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002511
Jim Laskeyef42a012006-11-02 20:12:39 +00002512 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002513
Jim Laskeyef42a012006-11-02 20:12:39 +00002514 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002515 unsigned SecSrcLinesSize = SectionSourceLines.size();
2516
2517 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002518 // Isolate current sections line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00002519 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00002520
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002521 if (VerboseAsm) {
2522 const Section* S = SectionMap[j + 1];
Evan Chenge3d42322009-02-25 07:04:34 +00002523 O << '\t' << TAI->getCommentString() << " Section"
2524 << S->getName() << '\n';
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002525 } else
Evan Cheng6547e402008-07-01 23:18:29 +00002526 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002527
2528 // Dwarf assumes we start with first line of first source file.
2529 unsigned Source = 1;
2530 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002531
Jim Laskeyef42a012006-11-02 20:12:39 +00002532 // Construct rows of the address, source, line, column matrix.
2533 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patelf3ee5142009-01-12 22:54:42 +00002534 const SrcLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002535 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00002536 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002537
Evan Chenge3d42322009-02-25 07:04:34 +00002538 if (!VerboseAsm)
Evan Cheng6547e402008-07-01 23:18:29 +00002539 Asm->EOL();
Evan Chenge3d42322009-02-25 07:04:34 +00002540 else {
2541 std::pair<unsigned, unsigned> SourceID =
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002542 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Evan Chenge3d42322009-02-25 07:04:34 +00002543 O << '\t' << TAI->getCommentString() << ' '
2544 << getSourceDirectoryName(SourceID.first) << ' '
2545 << getSourceFileName(SourceID.second)
2546 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2547 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002548
2549 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002550 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00002551 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002552 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00002553 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002554
Jim Laskeyef42a012006-11-02 20:12:39 +00002555 // If change of source, then switch to the new source.
2556 if (Source != LineInfo.getSourceID()) {
2557 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002558 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2559 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00002560 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002561
Jim Laskeyef42a012006-11-02 20:12:39 +00002562 // If change of line.
2563 if (Line != LineInfo.getLine()) {
2564 // Determine offset.
2565 int Offset = LineInfo.getLine() - Line;
2566 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002567
Jim Laskeyef42a012006-11-02 20:12:39 +00002568 // Update line.
2569 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002570
Jim Laskeyef42a012006-11-02 20:12:39 +00002571 // If delta is small enough and in range...
2572 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2573 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002574 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00002575 } else {
2576 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002577 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2578 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2579 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002580 }
2581 } else {
2582 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002583 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002584 }
2585 }
2586
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002587 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002588 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002589
2590 if (SecSrcLinesSize == 0)
2591 // Because we're emitting a debug_line section, we still need a line
2592 // table. The linker and friends expect it to exist. If there's nothing to
2593 // put into it, emit an empty table.
2594 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002595
Jim Laskeyef42a012006-11-02 20:12:39 +00002596 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002597
Jim Laskeybacd3042007-02-21 22:48:45 +00002598 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002599 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002600
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002601 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00002602 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002603 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002604 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00002605 return;
2606
2607 int stackGrowth =
2608 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2609 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002610 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00002611
2612 // Start the dwarf frame section.
2613 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2614
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002615 EmitLabel("debug_frame_common", 0);
2616 EmitDifference("debug_frame_common_end", 0,
2617 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002618 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002619
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002620 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002621 Asm->EmitInt32((int)DW_CIE_ID);
2622 Asm->EOL("CIE Identifier Tag");
2623 Asm->EmitInt8(DW_CIE_VERSION);
2624 Asm->EOL("CIE Version");
2625 Asm->EmitString("");
2626 Asm->EOL("CIE Augmentation");
2627 Asm->EmitULEB128Bytes(1);
2628 Asm->EOL("CIE Code Alignment Factor");
2629 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002630 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002631 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002632 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002633
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002634 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002635 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002636
Dale Johannesenb97aec62007-11-13 19:13:01 +00002637 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002638
Evan Cheng05548eb2008-02-29 19:36:59 +00002639 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002640 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002641
Jim Laskeybacd3042007-02-21 22:48:45 +00002642 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002643 }
2644
Jim Laskey65195462006-10-30 13:35:07 +00002645 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2646 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002647 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002648 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00002649 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002650
Jim Laskeyef42a012006-11-02 20:12:39 +00002651 // Start the dwarf frame section.
2652 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002653
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002654 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2655 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002656 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002657
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002658 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002659
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002660 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2661 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002662 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002663
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002664 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002665 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002666 EmitDifference("func_end", DebugFrameInfo.Number,
2667 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002668 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002669
Devang Patel5aac3d32009-01-17 08:01:33 +00002670 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00002671 false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002672
Evan Cheng05548eb2008-02-29 19:36:59 +00002673 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002674 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00002675
Jim Laskeybacd3042007-02-21 22:48:45 +00002676 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002677 }
2678
Evan Chenge3d42322009-02-25 07:04:34 +00002679 void EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2680 EmitDifference("pubnames_end", Unit->getID(),
2681 "pubnames_begin", Unit->getID(), true);
2682 Asm->EOL("Length of Public Names Info");
2683
2684 EmitLabel("pubnames_begin", Unit->getID());
2685
2686 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2687
2688 EmitSectionOffset("info_begin", "section_info",
2689 Unit->getID(), 0, true, false);
2690 Asm->EOL("Offset of Compilation Unit Info");
2691
2692 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2693 true);
2694 Asm->EOL("Compilation Unit Length");
2695
2696 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2697 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2698 GE = Globals.end(); GI != GE; ++GI) {
2699 const std::string &Name = GI->first;
2700 DIE * Entity = GI->second;
2701
2702 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2703 Asm->EmitString(Name); Asm->EOL("External Name");
2704 }
2705
2706 Asm->EmitInt32(0); Asm->EOL("End Mark");
2707 EmitLabel("pubnames_end", Unit->getID());
2708
2709 Asm->EOL();
2710 }
2711
Jim Laskeyef42a012006-11-02 20:12:39 +00002712 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002713 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002714 void EmitDebugPubNames() {
2715 // Start the dwarf pubnames section.
2716 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002717
Evan Chenge3d42322009-02-25 07:04:34 +00002718 if (MainCU) {
2719 EmitDebugPubNamesPerCU(MainCU);
2720 return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002721 }
Evan Chenge3d42322009-02-25 07:04:34 +00002722
2723 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2724 EmitDebugPubNamesPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002725 }
2726
2727 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002728 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002729 void EmitDebugStr() {
2730 // Check to see if it is worth the effort.
2731 if (!StringPool.empty()) {
2732 // Start the dwarf str section.
2733 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002734
Jim Laskeyef42a012006-11-02 20:12:39 +00002735 // For each of strings in the string pool.
2736 for (unsigned StringID = 1, N = StringPool.size();
2737 StringID <= N; ++StringID) {
2738 // Emit a label for reference from debug information entries.
2739 EmitLabel("string", StringID);
2740 // Emit the string itself.
2741 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00002742 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002743 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002744
Jim Laskeybacd3042007-02-21 22:48:45 +00002745 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002746 }
2747 }
2748
2749 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002750 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002751 void EmitDebugLoc() {
2752 // Start the dwarf loc section.
2753 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002754
Jim Laskeybacd3042007-02-21 22:48:45 +00002755 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002756 }
2757
2758 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002759 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002760 void EmitDebugARanges() {
2761 // Start the dwarf aranges section.
2762 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002763
Jim Laskeyef42a012006-11-02 20:12:39 +00002764 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00002765#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002766 CompileUnit *Unit = GetBaseCompileUnit();
2767
Jim Laskey5496f012006-11-09 14:52:14 +00002768 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002769 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002770
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002771 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002772
Jim Laskey5496f012006-11-09 14:52:14 +00002773 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002774 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002775
Dan Gohman82482942007-09-27 23:12:31 +00002776 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002777
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002778 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002779
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002780 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2781 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002782
Jim Laskey5496f012006-11-09 14:52:14 +00002783 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002784 EmitReference("text_begin", 0); Asm->EOL("Address");
2785 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002786
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002787 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2788 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00002789#endif
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002790
Jim Laskeybacd3042007-02-21 22:48:45 +00002791 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002792 }
2793
2794 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002795 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002796 void EmitDebugRanges() {
2797 // Start the dwarf ranges section.
2798 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002799
Jim Laskeybacd3042007-02-21 22:48:45 +00002800 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002801 }
2802
2803 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002804 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002805 void EmitDebugMacInfo() {
Scott Michel210de722009-01-26 22:32:51 +00002806 if (TAI->doesSupportMacInfoSection()) {
2807 // Start the dwarf macinfo section.
2808 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002809
Scott Michel210de722009-01-26 22:32:51 +00002810 Asm->EOL();
2811 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002812 }
2813
Bill Wendlingc8615e42009-03-10 21:47:45 +00002814 /// GetOrCreateSourceID - Look up the source id with the given directory and
2815 /// source file names. If none currently exists, create a new id and insert it
2816 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
2817 /// as well.
2818 unsigned GetOrCreateSourceID(const std::string &DirName,
2819 const std::string &FileName) {
2820 unsigned DId;
2821 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
2822 if (DI != DirectoryIdMap.end()) {
2823 DId = DI->getValue();
2824 } else {
2825 DId = DirectoryNames.size() + 1;
2826 DirectoryIdMap[DirName] = DId;
2827 DirectoryNames.push_back(DirName);
2828 }
2829
2830 unsigned FId;
2831 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
2832 if (FI != SourceFileIdMap.end()) {
2833 FId = FI->getValue();
2834 } else {
2835 FId = SourceFileNames.size() + 1;
2836 SourceFileIdMap[FileName] = FId;
2837 SourceFileNames.push_back(FileName);
2838 }
2839
2840 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
2841 SourceIdMap.find(std::make_pair(DId, FId));
2842 if (SI != SourceIdMap.end())
2843 return SI->second;
2844
2845 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
2846 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
2847 SourceIds.push_back(std::make_pair(DId, FId));
2848
2849 return SrcId;
2850 }
2851
Evan Chenge3d42322009-02-25 07:04:34 +00002852 void ConstructCompileUnit(GlobalVariable *GV) {
2853 DICompileUnit DIUnit(GV);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002854 std::string Dir, FN, Prod;
Bill Wendlingc8615e42009-03-10 21:47:45 +00002855 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002856 DIUnit.getFilename(FN));
Evan Chenge3d42322009-02-25 07:04:34 +00002857
2858 DIE *Die = new DIE(DW_TAG_compile_unit);
2859 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2860 DWLabel("section_line", 0), DWLabel("section_line", 0),
2861 false);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002862 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer(Prod));
Evan Chenge3d42322009-02-25 07:04:34 +00002863 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002864 AddString(Die, DW_AT_name, DW_FORM_string, FN);
2865 if (!Dir.empty())
2866 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Chenge3d42322009-02-25 07:04:34 +00002867 if (DIUnit.isOptimized())
2868 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002869 std::string Flags;
2870 DIUnit.getFlags(Flags);
Evan Chenge3d42322009-02-25 07:04:34 +00002871 if (!Flags.empty())
2872 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
2873 unsigned RVer = DIUnit.getRunTimeVersion();
2874 if (RVer)
2875 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
2876
2877 CompileUnit *Unit = new CompileUnit(ID, Die);
2878 if (DIUnit.isMain()) {
2879 assert(!MainCU && "Multiple main compile units are found!");
2880 MainCU = Unit;
2881 }
2882 CompileUnitMap[DIUnit.getGV()] = Unit;
2883 CompileUnits.push_back(Unit);
2884 }
2885
Devang Patelc4523242009-01-05 23:11:11 +00002886 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Pateld1ca9252009-01-05 23:03:32 +00002887 void ConstructCompileUnits() {
Evan Chenge3d42322009-02-25 07:04:34 +00002888 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.compile_units");
2889 if (!Root)
2890 return;
2891 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2892 "Malformed compile unit descriptor anchor type");
2893 Constant *RootC = cast<Constant>(*Root->use_begin());
2894 assert(RootC->hasNUsesOrMore(1) &&
2895 "Malformed compile unit descriptor anchor type");
2896 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2897 UI != UE; ++UI)
2898 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2899 UUI != UUE; ++UUI) {
2900 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2901 ConstructCompileUnit(GV);
Devang Pateldd9db662009-01-30 18:20:31 +00002902 }
Evan Chenge3d42322009-02-25 07:04:34 +00002903 }
2904
2905 bool ConstructGlobalVariableDIE(GlobalVariable *GV) {
2906 DIGlobalVariable DI_GV(GV);
2907 CompileUnit *DW_Unit = MainCU;
2908 if (!DW_Unit)
2909 DW_Unit = FindCompileUnit(DI_GV.getCompileUnit());
2910
2911 // Check for pre-existence.
2912 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
2913 if (Slot)
2914 return false;
2915
2916 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
2917
2918 // Add address.
2919 DIEBlock *Block = new DIEBlock();
2920 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
2921 AddObjectLabel(Block, 0, DW_FORM_udata,
2922 Asm->getGlobalLinkName(DI_GV.getGlobal()));
2923 AddBlock(VariableDie, DW_AT_location, 0, Block);
2924
2925 // Add to map.
2926 Slot = VariableDie;
2927 // Add to context owner.
2928 DW_Unit->getDie()->AddChild(VariableDie);
2929 // Expose as global. FIXME - need to check external flag.
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002930 std::string Name;
2931 DW_Unit->AddGlobal(DI_GV.getName(Name), VariableDie);
Evan Chenge3d42322009-02-25 07:04:34 +00002932 return true;
Devang Pateld1ca9252009-01-05 23:03:32 +00002933 }
2934
Devang Patelc4523242009-01-05 23:11:11 +00002935 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patel53cac182009-02-24 00:02:15 +00002936 /// visible global variables. Return true if at least one global DIE is
2937 /// created.
2938 bool ConstructGlobalVariableDIEs() {
Evan Chenge3d42322009-02-25 07:04:34 +00002939 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
2940 if (!Root)
2941 return false;
Devang Patelc4523242009-01-05 23:11:11 +00002942
Evan Chenge3d42322009-02-25 07:04:34 +00002943 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2944 "Malformed global variable descriptor anchor type");
2945 Constant *RootC = cast<Constant>(*Root->use_begin());
2946 assert(RootC->hasNUsesOrMore(1) &&
2947 "Malformed global variable descriptor anchor type");
Devang Patelc4523242009-01-05 23:11:11 +00002948
Evan Chenge3d42322009-02-25 07:04:34 +00002949 bool Result = false;
2950 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2951 UI != UE; ++UI)
2952 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2953 UUI != UUE; ++UUI) {
2954 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2955 Result |= ConstructGlobalVariableDIE(GV);
2956 }
2957 return Result;
2958 }
Devang Patelc4523242009-01-05 23:11:11 +00002959
Evan Chenge3d42322009-02-25 07:04:34 +00002960 bool ConstructSubprogram(GlobalVariable *GV) {
2961 DISubprogram SP(GV);
2962 CompileUnit *Unit = MainCU;
2963 if (!Unit)
2964 Unit = FindCompileUnit(SP.getCompileUnit());
Devang Patelc4523242009-01-05 23:11:11 +00002965
Evan Chenge3d42322009-02-25 07:04:34 +00002966 // Check for pre-existence.
2967 DIE *&Slot = Unit->getDieMapSlotFor(GV);
2968 if (Slot)
2969 return false;
2970
2971 if (!SP.isDefinition())
2972 // This is a method declaration which will be handled while
2973 // constructing class type.
2974 return false;
2975
2976 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
2977
2978 // Add to map.
2979 Slot = SubprogramDie;
2980 // Add to context owner.
2981 Unit->getDie()->AddChild(SubprogramDie);
2982 // Expose as global.
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002983 std::string Name;
2984 Unit->AddGlobal(SP.getName(Name), SubprogramDie);
Evan Chenge3d42322009-02-25 07:04:34 +00002985 return true;
Devang Patelc4523242009-01-05 23:11:11 +00002986 }
2987
Devang Patel78eb6ad2009-01-05 23:21:35 +00002988 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patel53cac182009-02-24 00:02:15 +00002989 /// subprograms. Return true if at least one subprogram DIE is created.
2990 bool ConstructSubprograms() {
Evan Chenge3d42322009-02-25 07:04:34 +00002991 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
2992 if (!Root)
2993 return false;
Devang Patel78eb6ad2009-01-05 23:21:35 +00002994
Evan Chenge3d42322009-02-25 07:04:34 +00002995 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2996 "Malformed subprogram descriptor anchor type");
2997 Constant *RootC = cast<Constant>(*Root->use_begin());
2998 assert(RootC->hasNUsesOrMore(1) &&
2999 "Malformed subprogram descriptor anchor type");
Devang Patel78eb6ad2009-01-05 23:21:35 +00003000
Evan Chenge3d42322009-02-25 07:04:34 +00003001 bool Result = false;
3002 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3003 UI != UE; ++UI)
3004 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3005 UUI != UUE; ++UUI) {
3006 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3007 Result |= ConstructSubprogram(GV);
3008 }
3009 return Result;
Devang Patel78eb6ad2009-01-05 23:21:35 +00003010 }
3011
Jim Laskey65195462006-10-30 13:35:07 +00003012public:
Jim Laskeyef42a012006-11-02 20:12:39 +00003013 //===--------------------------------------------------------------------===//
3014 // Main entry points.
3015 //
Owen Andersoncb371882008-08-21 00:14:44 +00003016 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00003017 : Dwarf(OS, A, T, "dbg"), MainCU(0),
3018 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
3019 ValuesSet(InitValuesSetSize), Values(), StringPool(), SectionMap(),
3020 SectionSourceLines(), didInitial(false), shouldEmit(false),
3021 RootDbgScope(0), DebugTimer(0) {
3022 if (TimePassesIsEnabled)
3023 DebugTimer = new Timer("Dwarf Debug Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00003024 getDwarfTimerGroup());
Jim Laskeyef42a012006-11-02 20:12:39 +00003025 }
Jim Laskey072200c2007-01-29 18:51:14 +00003026 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003027 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3028 delete Values[j];
Bill Wendling163ba3f2009-03-10 21:23:25 +00003029
3030 delete DebugTimer;
Jim Laskeyef42a012006-11-02 20:12:39 +00003031 }
3032
Bill Wendlingc8615e42009-03-10 21:47:45 +00003033 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
3034 /// be emitted.
3035 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
3036
Devang Patel5d315982009-01-06 21:07:30 +00003037 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3038 /// This is inovked by the target AsmPrinter.
Devang Patel3f7833a2009-01-12 23:09:42 +00003039 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003040 if (TimePassesIsEnabled)
3041 DebugTimer->startTimer();
3042
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003043 // Create all the compile unit DIEs.
3044 ConstructCompileUnits();
Devang Patel3f7833a2009-01-12 23:09:42 +00003045
Bill Wendling163ba3f2009-03-10 21:23:25 +00003046 if (CompileUnits.empty()) {
3047 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003048 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003049
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003050 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003051 }
Devang Patel3f7833a2009-01-12 23:09:42 +00003052
Devang Patel53cac182009-02-24 00:02:15 +00003053 // Create DIEs for each of the externally visible global variables.
3054 bool globalDIEs = ConstructGlobalVariableDIEs();
3055
3056 // Create DIEs for each of the externally visible subprograms.
3057 bool subprogramDIEs = ConstructSubprograms();
3058
3059 // If there is not any debug info available for any global variables
3060 // and any subprograms then there is not any debug info to emit.
Bill Wendling163ba3f2009-03-10 21:23:25 +00003061 if (!globalDIEs && !subprogramDIEs) {
3062 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003063 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003064
Devang Patel53cac182009-02-24 00:02:15 +00003065 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003066 }
Devang Patel53cac182009-02-24 00:02:15 +00003067
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003068 MMI = mmi;
3069 shouldEmit = true;
3070 MMI->setDebugInfoAvailability(true);
Devang Patel5d315982009-01-06 21:07:30 +00003071
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003072 // Prime section data.
3073 SectionMap.insert(TAI->getTextSection());
Devang Patel5d315982009-01-06 21:07:30 +00003074
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003075 // Print out .file directives to specify files for .loc directives. These
3076 // are printed out early so that they precede any .loc directives.
3077 if (TAI->hasDotLocAndDotFile()) {
Evan Chenge3d42322009-02-25 07:04:34 +00003078 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
3079 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00003080 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Evan Chenge3d42322009-02-25 07:04:34 +00003081 sys::Path FullPath(getSourceDirectoryName(Id.first));
3082 bool AppendOk =
3083 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003084 assert(AppendOk && "Could not append filename to directory!");
3085 AppendOk = false;
3086 Asm->EmitFile(i, FullPath.toString());
3087 Asm->EOL();
Devang Patel5d315982009-01-06 21:07:30 +00003088 }
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003089 }
Devang Patel5d315982009-01-06 21:07:30 +00003090
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003091 // Emit initial sections
3092 EmitInitial();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003093
3094 if (TimePassesIsEnabled)
3095 DebugTimer->stopTimer();
Devang Patel5d315982009-01-06 21:07:30 +00003096 }
3097
Jim Laskey65195462006-10-30 13:35:07 +00003098 /// BeginModule - Emit all Dwarf sections that should come prior to the
3099 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00003100 void BeginModule(Module *M) {
3101 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00003102 }
3103
Jim Laskey65195462006-10-30 13:35:07 +00003104 /// EndModule - Emit all Dwarf sections that should come after the content.
3105 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003106 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003107 if (!ShouldEmitDwarfDebug())
3108 return;
3109
3110 if (TimePassesIsEnabled)
3111 DebugTimer->startTimer();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003112
Jim Laskeyef42a012006-11-02 20:12:39 +00003113 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003114 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003115 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00003116 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003117 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003118
Jim Laskeyef42a012006-11-02 20:12:39 +00003119 // End text sections.
3120 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003121 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00003122 EmitLabel("section_end", i);
3123 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003124
3125 // Emit common frame information.
3126 EmitCommonDebugFrame();
3127
3128 // Emit function debug frame information
3129 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3130 E = DebugFrames.end(); I != E; ++I)
3131 EmitFunctionDebugFrame(*I);
3132
Jim Laskeyef42a012006-11-02 20:12:39 +00003133 // Compute DIE offsets and sizes.
3134 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003135
Jim Laskeyef42a012006-11-02 20:12:39 +00003136 // Emit all the DIEs into a debug info section
3137 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003138
Jim Laskeyef42a012006-11-02 20:12:39 +00003139 // Corresponding abbreviations into a abbrev section.
3140 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003141
Jim Laskeyef42a012006-11-02 20:12:39 +00003142 // Emit source line correspondence into a debug line section.
3143 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003144
Jim Laskeyef42a012006-11-02 20:12:39 +00003145 // Emit info into a debug pubnames section.
3146 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003147
Jim Laskeyef42a012006-11-02 20:12:39 +00003148 // Emit info into a debug str section.
3149 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003150
Jim Laskeyef42a012006-11-02 20:12:39 +00003151 // Emit info into a debug loc section.
3152 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003153
Jim Laskeyef42a012006-11-02 20:12:39 +00003154 // Emit info into a debug aranges section.
3155 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003156
Jim Laskeyef42a012006-11-02 20:12:39 +00003157 // Emit info into a debug ranges section.
3158 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003159
Jim Laskeyef42a012006-11-02 20:12:39 +00003160 // Emit info into a debug macinfo section.
3161 EmitDebugMacInfo();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003162
3163 if (TimePassesIsEnabled)
3164 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003165 }
3166
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003167 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00003168 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00003169 void BeginFunction(MachineFunction *MF) {
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003170 if (!ShouldEmitDwarfDebug()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00003171
Bill Wendling163ba3f2009-03-10 21:23:25 +00003172 if (TimePassesIsEnabled)
3173 DebugTimer->startTimer();
3174
3175 this->MF = MF;
3176
Jim Laskeyef42a012006-11-02 20:12:39 +00003177 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003178 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003179
Jim Laskeyef42a012006-11-02 20:12:39 +00003180 // Assumes in correct section after the entry point.
3181 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00003182
3183 // Emit label for the implicitly defined dbg.stoppoint at the start of
3184 // the function.
Devang Patelf3ee5142009-01-12 22:54:42 +00003185 if (!Lines.empty()) {
3186 const SrcLineInfo &LineInfo = Lines[0];
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00003187 Asm->printLabel(LineInfo.getLabelID());
3188 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003189
3190 if (TimePassesIsEnabled)
3191 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003192 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003193
Jim Laskey65195462006-10-30 13:35:07 +00003194 /// EndFunction - Gather and emit post-function debug information.
3195 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00003196 void EndFunction(MachineFunction *MF) {
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003197 if (!ShouldEmitDwarfDebug()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003198
Bill Wendling163ba3f2009-03-10 21:23:25 +00003199 if (TimePassesIsEnabled)
3200 DebugTimer->startTimer();
3201
Jim Laskeyb82313f2007-02-01 16:31:34 +00003202 // Define end label for subprogram.
3203 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003204
Jim Laskeyef42a012006-11-02 20:12:39 +00003205 // Get function line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00003206 if (!Lines.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003207 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003208 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00003209 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patelf3ee5142009-01-12 22:54:42 +00003210 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Jim Laskeyef42a012006-11-02 20:12:39 +00003211 // Append the function info to section info.
3212 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patelf3ee5142009-01-12 22:54:42 +00003213 Lines.begin(), Lines.end());
Jim Laskeyef42a012006-11-02 20:12:39 +00003214 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003215
Jim Laskeyef42a012006-11-02 20:12:39 +00003216 // Construct scopes for subprogram.
Devang Patel7bb89ed2009-01-13 00:20:51 +00003217 if (RootDbgScope)
3218 ConstructRootDbgScope(RootDbgScope);
Bill Wendlingd751c642008-09-26 00:28:12 +00003219 else
3220 // FIXME: This is wrong. We are essentially getting past a problem with
3221 // debug information not being able to handle unreachable blocks that have
3222 // debug information in them. In particular, those unreachable blocks that
3223 // have "region end" info in them. That situation results in the "root
3224 // scope" not being created. If that's the case, then emit a "default"
3225 // scope, i.e., one that encompasses the whole function. This isn't
3226 // desirable. And a better way of handling this (and all of the debugging
3227 // information) needs to be explored.
Devang Patel7bb89ed2009-01-13 00:20:51 +00003228 ConstructDefaultDbgScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003229
3230 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3231 MMI->getFrameMoves()));
Devang Patel481ff5b2009-01-12 18:48:36 +00003232
3233 // Clear debug info
3234 if (RootDbgScope) {
3235 delete RootDbgScope;
3236 DbgScopeMap.clear();
3237 RootDbgScope = NULL;
3238 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003239
Bill Wendling163ba3f2009-03-10 21:23:25 +00003240 Lines.clear();
3241
3242 if (TimePassesIsEnabled)
3243 DebugTimer->stopTimer();
3244 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003245
Devang Patelcf3a4482009-01-15 23:41:32 +00003246 /// ValidDebugInfo - Return true if V represents valid debug info value.
3247 bool ValidDebugInfo(Value *V) {
Devang Patelb79b5352009-01-19 23:21:49 +00003248 if (!V)
3249 return false;
3250
Devang Patel61c6bf32009-01-16 01:49:46 +00003251 if (!shouldEmit)
3252 return false;
3253
Devang Patelcf3a4482009-01-15 23:41:32 +00003254 GlobalVariable *GV = getGlobalVariable(V);
3255 if (!GV)
3256 return false;
Duncan Sands667d4b82009-03-07 15:45:40 +00003257
3258 if (!GV->hasInternalLinkage () && !GV->hasLinkOnceLinkage())
Devang Patelcf3a4482009-01-15 23:41:32 +00003259 return false;
3260
Bill Wendling163ba3f2009-03-10 21:23:25 +00003261 if (TimePassesIsEnabled)
3262 DebugTimer->startTimer();
3263
Devang Patelcf3a4482009-01-15 23:41:32 +00003264 DIDescriptor DI(GV);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003265
Devang Patelcf3a4482009-01-15 23:41:32 +00003266 // Check current version. Allow Version6 for now.
3267 unsigned Version = DI.getVersion();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003268 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6) {
3269 if (TimePassesIsEnabled)
3270 DebugTimer->stopTimer();
3271
Devang Patelcf3a4482009-01-15 23:41:32 +00003272 return false;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003273 }
Devang Patelcf3a4482009-01-15 23:41:32 +00003274
Devang Patelb79b5352009-01-19 23:21:49 +00003275 unsigned Tag = DI.getTag();
3276 switch (Tag) {
3277 case DW_TAG_variable:
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003278 assert(DIVariable(GV).Verify() && "Invalid DebugInfo value");
Devang Patelb79b5352009-01-19 23:21:49 +00003279 break;
3280 case DW_TAG_compile_unit:
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003281 assert(DICompileUnit(GV).Verify() && "Invalid DebugInfo value");
Devang Patelb79b5352009-01-19 23:21:49 +00003282 break;
3283 case DW_TAG_subprogram:
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003284 assert(DISubprogram(GV).Verify() && "Invalid DebugInfo value");
Devang Patelb79b5352009-01-19 23:21:49 +00003285 break;
3286 default:
3287 break;
3288 }
3289
Bill Wendling163ba3f2009-03-10 21:23:25 +00003290 if (TimePassesIsEnabled)
3291 DebugTimer->stopTimer();
3292
Devang Patelcf3a4482009-01-15 23:41:32 +00003293 return true;
3294 }
3295
Devang Patelccca7fe2009-01-12 19:17:34 +00003296 /// RecordSourceLine - Records location information and associates it with a
3297 /// label. Returns a unique label ID used to generate a label and provide
3298 /// correspondence to the source line list.
3299 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003300 if (TimePassesIsEnabled)
3301 DebugTimer->startTimer();
3302
Evan Chenge3d42322009-02-25 07:04:34 +00003303 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003304 assert(Unit && "Unable to find CompileUnit");
Devang Patelccca7fe2009-01-12 19:17:34 +00003305 unsigned ID = MMI->NextLabelID();
3306 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003307
3308 if (TimePassesIsEnabled)
3309 DebugTimer->stopTimer();
3310
Devang Patelccca7fe2009-01-12 19:17:34 +00003311 return ID;
3312 }
3313
3314 /// RecordSourceLine - Records location information and associates it with a
3315 /// label. Returns a unique label ID used to generate a label and provide
3316 /// correspondence to the source line list.
3317 unsigned RecordSourceLine(unsigned Line, unsigned Col, unsigned Src) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003318 if (TimePassesIsEnabled)
3319 DebugTimer->startTimer();
3320
Devang Patelccca7fe2009-01-12 19:17:34 +00003321 unsigned ID = MMI->NextLabelID();
3322 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003323
3324 if (TimePassesIsEnabled)
3325 DebugTimer->stopTimer();
3326
Devang Patelccca7fe2009-01-12 19:17:34 +00003327 return ID;
3328 }
3329
Bill Wendlingc8615e42009-03-10 21:47:45 +00003330 /// getRecordSourceLineCount - Return the number of source lines in the debug
3331 /// info.
3332 unsigned getRecordSourceLineCount() const {
Devang Patelccca7fe2009-01-12 19:17:34 +00003333 return Lines.size();
3334 }
3335
Bill Wendlingc8615e42009-03-10 21:47:45 +00003336 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
3337 /// timed. Look up the source id with the given directory and source file
3338 /// names. If none currently exists, create a new id and insert it in the
3339 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
3340 /// well.
Evan Chenge3d42322009-02-25 07:04:34 +00003341 unsigned getOrCreateSourceID(const std::string &DirName,
3342 const std::string &FileName) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003343 if (TimePassesIsEnabled)
3344 DebugTimer->startTimer();
3345
Bill Wendlingc8615e42009-03-10 21:47:45 +00003346 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003347
3348 if (TimePassesIsEnabled)
3349 DebugTimer->stopTimer();
3350
Evan Chenge3d42322009-02-25 07:04:34 +00003351 return SrcId;
Devang Patelccca7fe2009-01-12 19:17:34 +00003352 }
3353
3354 /// RecordRegionStart - Indicate the start of a region.
Devang Patelccca7fe2009-01-12 19:17:34 +00003355 unsigned RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003356 if (TimePassesIsEnabled)
3357 DebugTimer->startTimer();
3358
Devang Patelccca7fe2009-01-12 19:17:34 +00003359 DbgScope *Scope = getOrCreateScope(V);
3360 unsigned ID = MMI->NextLabelID();
3361 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003362
3363 if (TimePassesIsEnabled)
3364 DebugTimer->stopTimer();
3365
Devang Patelccca7fe2009-01-12 19:17:34 +00003366 return ID;
3367 }
3368
3369 /// RecordRegionEnd - Indicate the end of a region.
Devang Patelccca7fe2009-01-12 19:17:34 +00003370 unsigned RecordRegionEnd(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003371 if (TimePassesIsEnabled)
3372 DebugTimer->startTimer();
3373
Devang Patelccca7fe2009-01-12 19:17:34 +00003374 DbgScope *Scope = getOrCreateScope(V);
3375 unsigned ID = MMI->NextLabelID();
3376 Scope->setEndLabelID(ID);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003377
3378 if (TimePassesIsEnabled)
3379 DebugTimer->stopTimer();
3380
Devang Patelccca7fe2009-01-12 19:17:34 +00003381 return ID;
3382 }
3383
3384 /// RecordVariable - Indicate the declaration of a local variable.
Devang Patelccca7fe2009-01-12 19:17:34 +00003385 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003386 if (TimePassesIsEnabled)
3387 DebugTimer->startTimer();
3388
Devang Patel0e5200f2009-01-15 18:25:17 +00003389 DIDescriptor Desc(GV);
3390 DbgScope *Scope = NULL;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003391
Devang Patel0e5200f2009-01-15 18:25:17 +00003392 if (Desc.getTag() == DW_TAG_variable) {
3393 // GV is a global variable.
3394 DIGlobalVariable DG(GV);
3395 Scope = getOrCreateScope(DG.getContext().getGV());
3396 } else {
3397 // or GV is a local variable.
3398 DIVariable DV(GV);
3399 Scope = getOrCreateScope(DV.getContext().getGV());
3400 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003401
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003402 assert(Scope && "Unable to find variable' scope");
Devang Patel99ec3532009-01-16 19:28:14 +00003403 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelccca7fe2009-01-12 19:17:34 +00003404 Scope->AddVariable(DV);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003405
3406 if (TimePassesIsEnabled)
3407 DebugTimer->stopTimer();
Devang Patelccca7fe2009-01-12 19:17:34 +00003408 }
Jim Laskey65195462006-10-30 13:35:07 +00003409};
3410
Jim Laskey072200c2007-01-29 18:51:14 +00003411//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003412/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00003413///
3414class DwarfException : public Dwarf {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003415 struct FunctionEHFrameInfo {
3416 std::string FnName;
3417 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003418 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003419 bool hasCalls;
3420 bool hasLandingPads;
3421 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003422 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00003423
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003424 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3425 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003426 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003427 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003428 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003429 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003430 };
3431
3432 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003433
3434 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3435 /// be emitted.
3436 bool shouldEmitTable;
3437
3438 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3439 /// should be emitted.
3440 bool shouldEmitMoves;
3441
3442 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3443 /// should be emitted.
3444 bool shouldEmitTableModule;
3445
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003446 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003447 /// should be emitted.
3448 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00003449
Bill Wendling163ba3f2009-03-10 21:23:25 +00003450 /// ExceptionTimer - Timer for the Dwarf exception writer.
3451 Timer *ExceptionTimer;
3452
Jim Laskey3f09fc22007-02-28 18:38:31 +00003453 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3454 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003455 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003456 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003457 int stackGrowth =
3458 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3459 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003460 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003461
Jim Laskeybacd3042007-02-21 22:48:45 +00003462 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003463 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling722f5f12008-12-24 08:05:17 +00003464
3465 if (!TAI->doesRequireNonLocalEHFrameLabel())
3466 O << TAI->getEHGlobalPrefix();
3467 O << "EH_frame" << Index << ":\n";
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003468 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003469
Jim Laskeybacd3042007-02-21 22:48:45 +00003470 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003471 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003472
Jim Laskeybacd3042007-02-21 22:48:45 +00003473 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003474 EmitDifference("eh_frame_common_end", Index,
3475 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003476 Asm->EOL("Length of Common Information Entry");
3477
Jim Laskeybacd3042007-02-21 22:48:45 +00003478 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003479 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003480 Asm->EmitInt32((int)0);
3481 Asm->EOL("CIE Identifier Tag");
3482 Asm->EmitInt8(DW_CIE_VERSION);
3483 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00003484
Jim Laskeybacd3042007-02-21 22:48:45 +00003485 // The personality presence indicates that language specific information
3486 // will show up in the eh frame.
3487 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00003488 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00003489
Jim Laskeybacd3042007-02-21 22:48:45 +00003490 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003491 Asm->EmitULEB128Bytes(1);
3492 Asm->EOL("CIE Code Alignment Factor");
3493 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00003494 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003495 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00003496 Asm->EOL("CIE Return Address Column");
3497
Jim Laskeybacd3042007-02-21 22:48:45 +00003498 // If there is a personality, we need to indicate the functions location.
3499 if (Personality) {
3500 Asm->EmitULEB128Bytes(7);
3501 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00003502
Duncan Sands671fa972008-05-07 19:11:09 +00003503 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003504 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00003505 Asm->EOL("Personality (pcrel sdata4 indirect)");
3506 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003507 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00003508 Asm->EOL("Personality (pcrel sdata4)");
3509 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00003510
Duncan Sands671fa972008-05-07 19:11:09 +00003511 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00003512 O << TAI->getPersonalityPrefix();
3513 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3514 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00003515 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3516 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00003517 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00003518
Duncan Sands671fa972008-05-07 19:11:09 +00003519 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3520 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003521
Bill Wendlingd60de512009-01-05 22:53:45 +00003522 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3523 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003524 } else {
3525 Asm->EmitULEB128Bytes(1);
3526 Asm->EOL("Augmentation Size");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003527
Bill Wendlingd60de512009-01-05 22:53:45 +00003528 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3529 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003530 }
3531
3532 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003533 std::vector<MachineMove> Moves;
3534 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00003535 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003536
Dale Johannesen21d972a2008-04-30 00:43:29 +00003537 // On Darwin the linker honors the alignment of eh_frame, which means it
3538 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3539 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003540 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003541 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003542 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003543
Jim Laskeybacd3042007-02-21 22:48:45 +00003544 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003545 }
Duncan Sands671fa972008-05-07 19:11:09 +00003546
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003547 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003548 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003549 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003550 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3551
Jim Laskeybacd3042007-02-21 22:48:45 +00003552 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3553
3554 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003555 // If the corresponding function is static, this should not be
3556 // externally visible.
Rafael Espindolabb46f522009-01-15 20:18:42 +00003557 if (linkage != Function::InternalLinkage &&
Devang Patel2d1768c2009-01-17 08:05:14 +00003558 linkage != Function::PrivateLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003559 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3560 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3561 }
3562
Dale Johannesen038129d2008-01-10 02:03:30 +00003563 // If corresponding function is weak definition, this should be too.
Duncan Sands667d4b82009-03-07 15:45:40 +00003564 if ((linkage == Function::WeakAnyLinkage ||
3565 linkage == Function::WeakODRLinkage ||
3566 linkage == Function::LinkOnceAnyLinkage ||
3567 linkage == Function::LinkOnceODRLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00003568 TAI->getWeakDefDirective())
3569 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3570
3571 // If there are no calls then you can't unwind. This may mean we can
3572 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003573 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00003574 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00003575 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00003576 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00003577 !UnwindTablesMandatory &&
Duncan Sands667d4b82009-03-07 15:45:40 +00003578 ((linkage != Function::WeakAnyLinkage &&
3579 linkage != Function::WeakODRLinkage &&
3580 linkage != Function::LinkOnceAnyLinkage &&
3581 linkage != Function::LinkOnceODRLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00003582 !TAI->getWeakDefDirective() ||
3583 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003584 {
Bill Wendling6e198962007-09-18 01:47:22 +00003585 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003586 // This name has no connection to the function, so it might get
3587 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003588 // dead-stripping unconditionally.
3589 if (const char *UsedDirective = TAI->getUsedDirective())
3590 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00003591 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00003592 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003593
Jim Laskeybacd3042007-02-21 22:48:45 +00003594 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003595 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3596 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003597 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003598
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003599 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003600
Bill Wendling722f5f12008-12-24 08:05:17 +00003601 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3602 PrintRelDirective(true, true);
3603 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3604
3605 if (!TAI->isAbsoluteEHSectionOffsets())
3606 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3607 } else {
3608 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3609 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3610 true, true, false);
3611 }
3612
Jim Laskeybacd3042007-02-21 22:48:45 +00003613 Asm->EOL("FDE CIE offset");
3614
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003615 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003616 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003617 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003618 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003619 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00003620
Jim Laskey3f09fc22007-02-28 18:38:31 +00003621 // If there is a personality and landing pads then point to the language
3622 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003623 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00003624 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00003625 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00003626
3627 if (EHFrameInfo.hasLandingPads)
3628 EmitReference("exception", EHFrameInfo.Number, true, true);
3629 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00003630 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00003631 Asm->EOL("Language Specific Data Area");
3632 } else {
3633 Asm->EmitULEB128Bytes(0);
3634 Asm->EOL("Augmentation size");
3635 }
Duncan Sands671fa972008-05-07 19:11:09 +00003636
Jim Laskeybacd3042007-02-21 22:48:45 +00003637 // Indicate locations of function specific callee saved registers in
3638 // frame.
Devang Patel5aac3d32009-01-17 08:01:33 +00003639 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00003640 true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003641
Dale Johannesen21d972a2008-04-30 00:43:29 +00003642 // On Darwin the linker honors the alignment of eh_frame, which means it
3643 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3644 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003645 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003646 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003647 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003648
3649 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003650 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003651 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003652 // that depends on unused functions (calling undefined externals) being
3653 // dead-stripped to link correctly. Yes, there really is.
3654 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3655 if (const char *UsedDirective = TAI->getUsedDirective())
3656 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3657 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003658 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003659
Duncan Sands57810cd2007-09-05 11:27:52 +00003660 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003661 ///
3662 /// The general organization of the table is complex, but the basic concepts
3663 /// are easy. First there is a header which describes the location and
3664 /// organization of the three components that follow.
3665 /// 1. The landing pad site information describes the range of code covered
3666 /// by the try. In our case it's an accumulation of the ranges covered
3667 /// by the invokes in the try. There is also a reference to the landing
3668 /// pad that handles the exception once processed. Finally an index into
3669 /// the actions table.
3670 /// 2. The action table, in our case, is composed of pairs of type ids
3671 /// and next action offset. Starting with the action index from the
3672 /// landing pad site, each type Id is checked for a match to the current
3673 /// exception. If it matches then the exception and type id are passed
3674 /// on to the landing pad. Otherwise the next action is looked up. This
3675 /// chain is terminated with a next action of zero. If no type id is
3676 /// found the the frame is unwound and handling continues.
3677 /// 3. Type id table contains references to all the C++ typeinfo for all
3678 /// catches in the function. This tables is reversed indexed base 1.
3679
Duncan Sandsb32edb42007-06-06 15:37:31 +00003680 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3681 static unsigned SharedTypeIds(const LandingPadInfo *L,
3682 const LandingPadInfo *R) {
3683 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003684 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003685 unsigned MinSize = LSize < RSize ? LSize : RSize;
3686 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003687
Duncan Sandsb32edb42007-06-06 15:37:31 +00003688 for (; Count != MinSize; ++Count)
3689 if (LIds[Count] != RIds[Count])
3690 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003691
Duncan Sandsb32edb42007-06-06 15:37:31 +00003692 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003693 }
3694
Duncan Sandsb32edb42007-06-06 15:37:31 +00003695 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00003696 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003697 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003698 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003699 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003700
Duncan Sandsb32edb42007-06-06 15:37:31 +00003701 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00003702 if (LIds[i] != RIds[i])
3703 return LIds[i] < RIds[i];
3704
Duncan Sandsb32edb42007-06-06 15:37:31 +00003705 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003706 }
3707
Duncan Sands53c3a332007-05-16 12:12:23 +00003708 struct KeyInfo {
3709 static inline unsigned getEmptyKey() { return -1U; }
3710 static inline unsigned getTombstoneKey() { return -2U; }
3711 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00003712 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00003713 static bool isPod() { return true; }
3714 };
3715
Duncan Sands57810cd2007-09-05 11:27:52 +00003716 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003717 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003718 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003719 int NextAction;
3720 struct ActionEntry *Previous;
3721 };
3722
Duncan Sands57810cd2007-09-05 11:27:52 +00003723 /// PadRange - Structure holding a try-range and the associated landing pad.
3724 struct PadRange {
3725 // The index of the landing pad.
3726 unsigned PadIndex;
3727 // The index of the begin and end labels in the landing pad's label lists.
3728 unsigned RangeIndex;
3729 };
3730
3731 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3732
3733 /// CallSiteEntry - Structure describing an entry in the call-site table.
3734 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00003735 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003736 unsigned BeginLabel; // zero indicates the start of the function.
3737 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00003738 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003739 unsigned PadLabel; // zero indicates that there is no landing pad.
3740 unsigned Action;
3741 };
3742
Jim Laskeybacd3042007-02-21 22:48:45 +00003743 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00003744 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00003745 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00003746 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3747 if (PadInfos.empty()) return;
3748
3749 // Sort the landing pads in order of their type ids. This is used to fold
3750 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00003751 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00003752 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00003753 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00003754 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003755 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3756
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003757 // Negative type ids index into FilterIds, positive type ids index into
3758 // TypeInfos. The value written for a positive type id is just the type
3759 // id itself. For a negative type id, however, the value written is the
3760 // (negative) byte offset of the corresponding FilterIds entry. The byte
3761 // offset is usually equal to the type id, because the FilterIds entries
3762 // are written using a variable width encoding which outputs one byte per
3763 // entry as long as the value written is not too large, but can differ.
3764 // This kind of complication does not occur for positive type ids because
3765 // type infos are output using a fixed width encoding.
3766 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3767 SmallVector<int, 16> FilterOffsets;
3768 FilterOffsets.reserve(FilterIds.size());
3769 int Offset = -1;
3770 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3771 E = FilterIds.end(); I != E; ++I) {
3772 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003773 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003774 }
3775
Duncan Sands57810cd2007-09-05 11:27:52 +00003776 // Compute the actions table and gather the first action index for each
3777 // landing pad site.
3778 SmallVector<ActionEntry, 32> Actions;
3779 SmallVector<unsigned, 64> FirstActions;
3780 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00003781
Duncan Sandsb32edb42007-06-06 15:37:31 +00003782 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00003783 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003784 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003785 const LandingPadInfo *LP = LandingPads[i];
3786 const std::vector<int> &TypeIds = LP->TypeIds;
3787 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003788 unsigned SizeSiteActions = 0;
3789
Duncan Sandsb32edb42007-06-06 15:37:31 +00003790 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00003791 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00003792 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003793
Duncan Sandsb32edb42007-06-06 15:37:31 +00003794 if (NumShared) {
3795 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3796 assert(Actions.size());
3797 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003798 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3799 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003800 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003801 SizeAction -=
3802 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003803 SizeAction += -PrevAction->NextAction;
3804 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003805 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00003806 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003807
Duncan Sandsb32edb42007-06-06 15:37:31 +00003808 // Compute the actions.
3809 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3810 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003811 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3812 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003813 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003814
3815 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003816 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003817 SizeSiteActions += SizeAction;
3818
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003819 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00003820 Actions.push_back(Action);
3821
3822 PrevAction = &Actions.back();
3823 }
3824
3825 // Record the first action of the landing pad site.
3826 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3827 } // else identical - re-use previous FirstAction
3828
3829 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003830
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00003831 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00003832 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00003833 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003834
Duncan Sands481dc722007-12-19 07:36:31 +00003835 // Compute the call-site table. The entry for an invoke has a try-range
3836 // containing the call, a non-zero landing pad and an appropriate action.
3837 // The entry for an ordinary call has a try-range containing the call and
3838 // zero for the landing pad and the action. Calls marked 'nounwind' have
3839 // no entry and must not be contained in the try-range of any entry - they
3840 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003841 SmallVector<CallSiteEntry, 64> CallSites;
3842
3843 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00003844 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3845 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3846 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00003847 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3848 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00003849 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003850 unsigned BeginLabel = LandingPad->BeginLabels[j];
3851 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3852 PadRange P = { i, j };
3853 PadMap[BeginLabel] = P;
3854 }
3855 }
3856
Duncan Sands481dc722007-12-19 07:36:31 +00003857 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00003858 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00003859
3860 // Whether there is a potentially throwing instruction (currently this means
3861 // an ordinary call) between the end of the previous try-range and now.
3862 bool SawPotentiallyThrowing = false;
3863
3864 // Whether the last callsite entry was for an invoke.
3865 bool PreviousIsInvoke = false;
3866
Duncan Sands481dc722007-12-19 07:36:31 +00003867 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003868 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3869 I != E; ++I) {
3870 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3871 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00003872 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00003873 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00003874 continue;
3875 }
3876
Chris Lattner9e330492007-12-30 20:50:28 +00003877 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00003878 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00003879
Duncan Sands481dc722007-12-19 07:36:31 +00003880 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00003881 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00003882 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003883
Duncan Sands481dc722007-12-19 07:36:31 +00003884 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00003885 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00003886 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00003887 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00003888 continue;
3889
3890 PadRange P = L->second;
3891 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3892
3893 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3894 "Inconsistent landing pad map!");
3895
3896 // If some instruction between the previous try-range and this one may
3897 // throw, create a call-site entry with no landing pad for the region
3898 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00003899 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003900 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3901 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00003902 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003903 }
3904
3905 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00003906 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00003907
Duncan Sands481dc722007-12-19 07:36:31 +00003908 if (LandingPad->LandingPadLabel) {
3909 // This try-range is for an invoke.
3910 CallSiteEntry Site = {BeginLabel, LastLabel,
3911 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00003912
Duncan Sands481dc722007-12-19 07:36:31 +00003913 // Try to merge with the previous call-site.
3914 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00003915 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00003916 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3917 // Extend the range of the previous entry.
3918 Prev.EndLabel = Site.EndLabel;
3919 continue;
3920 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003921 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003922
Duncan Sands481dc722007-12-19 07:36:31 +00003923 // Otherwise, create a new call-site.
3924 CallSites.push_back(Site);
3925 PreviousIsInvoke = true;
3926 } else {
3927 // Create a gap.
3928 PreviousIsInvoke = false;
3929 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003930 }
3931 }
3932 // If some instruction between the previous try-range and the end of the
3933 // function may throw, create a call-site entry with no landing pad for the
3934 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00003935 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003936 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3937 CallSites.push_back(Site);
3938 }
3939
Jim Laskeybacd3042007-02-21 22:48:45 +00003940 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00003941
3942 // Call sites.
3943 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
3944 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
3945 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
3946 unsigned SizeSites = CallSites.size() * (SiteStartSize +
3947 SiteLengthSize +
3948 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00003949 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003950 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00003951
Duncan Sands671fa972008-05-07 19:11:09 +00003952 // Type infos.
3953 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3954 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003955
3956 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003957 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003958 SizeSites + SizeActions + SizeTypes;
3959
3960 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3961 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003962 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003963 TypeOffset;
3964
3965 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003966
Jim Laskeybacd3042007-02-21 22:48:45 +00003967 // Begin the exception table.
3968 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng05548eb2008-02-29 19:36:59 +00003969 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesend65b2642008-10-08 21:50:21 +00003970 O << "GCC_except_table" << SubprogramCount << ":\n";
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003971 for (unsigned i = 0; i != SizeAlign; ++i) {
3972 Asm->EmitInt8(0);
3973 Asm->EOL("Padding");
3974 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003975 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003976
Jim Laskeybacd3042007-02-21 22:48:45 +00003977 // Emit the header.
3978 Asm->EmitInt8(DW_EH_PE_omit);
3979 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3980 Asm->EmitInt8(DW_EH_PE_absptr);
3981 Asm->EOL("TType format (DW_EH_PE_absptr)");
3982 Asm->EmitULEB128Bytes(TypeOffset);
3983 Asm->EOL("TType base offset");
3984 Asm->EmitInt8(DW_EH_PE_udata4);
3985 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3986 Asm->EmitULEB128Bytes(SizeSites);
3987 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00003988
Duncan Sands57810cd2007-09-05 11:27:52 +00003989 // Emit the landing pad site information.
3990 for (unsigned i = 0; i < CallSites.size(); ++i) {
3991 CallSiteEntry &S = CallSites[i];
3992 const char *BeginTag;
3993 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00003994
Duncan Sands57810cd2007-09-05 11:27:52 +00003995 if (!S.BeginLabel) {
3996 BeginTag = "eh_func_begin";
3997 BeginNumber = SubprogramCount;
3998 } else {
3999 BeginTag = "label";
4000 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00004001 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004002
Duncan Sands57810cd2007-09-05 11:27:52 +00004003 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004004 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004005 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00004006
Duncan Sands57810cd2007-09-05 11:27:52 +00004007 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00004008 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00004009 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004010 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00004011 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004012 }
4013 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004014
Duncan Sands671fa972008-05-07 19:11:09 +00004015 if (!S.PadLabel)
4016 Asm->EmitInt32(0);
4017 else
Duncan Sands57810cd2007-09-05 11:27:52 +00004018 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004019 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004020 Asm->EOL("Landing pad");
4021
4022 Asm->EmitULEB128Bytes(S.Action);
4023 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004024 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004025
Jim Laskeybacd3042007-02-21 22:48:45 +00004026 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004027 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4028 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00004029
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004030 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004031 Asm->EOL("TypeInfo index");
4032 Asm->EmitSLEB128Bytes(Action.NextAction);
4033 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004034 }
4035
4036 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00004037 for (unsigned M = TypeInfos.size(); M; --M) {
4038 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00004039
4040 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00004041
4042 if (GV)
4043 O << Asm->getGlobalLinkName(GV);
4044 else
4045 O << "0";
Duncan Sands57810cd2007-09-05 11:27:52 +00004046
Jim Laskeybacd3042007-02-21 22:48:45 +00004047 Asm->EOL("TypeInfo");
4048 }
4049
Duncan Sands73ef58a2007-06-02 16:53:42 +00004050 // Emit the filter typeids.
4051 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4052 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00004053 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00004054 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00004055 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004056
Evan Cheng05548eb2008-02-29 19:36:59 +00004057 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004058 }
4059
Jim Laskey072200c2007-01-29 18:51:14 +00004060public:
4061 //===--------------------------------------------------------------------===//
4062 // Main entry points.
4063 //
Owen Andersoncb371882008-08-21 00:14:44 +00004064 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00004065 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
4066 shouldEmitTableModule(false), shouldEmitMovesModule(false),
4067 ExceptionTimer(0) {
4068 if (TimePassesIsEnabled)
4069 ExceptionTimer = new Timer("Dwarf Exception Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00004070 getDwarfTimerGroup());
Bill Wendling163ba3f2009-03-10 21:23:25 +00004071 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004072
Bill Wendling163ba3f2009-03-10 21:23:25 +00004073 virtual ~DwarfException() {
4074 delete ExceptionTimer;
4075 }
Jim Laskey072200c2007-01-29 18:51:14 +00004076
4077 /// SetModuleInfo - Set machine module information when it's known that pass
4078 /// manager has created it. Set by the target AsmPrinter.
4079 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00004080 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00004081 }
4082
4083 /// BeginModule - Emit all exception information that should come prior to the
4084 /// content.
4085 void BeginModule(Module *M) {
4086 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00004087 }
4088
4089 /// EndModule - Emit all exception information that should come after the
4090 /// content.
4091 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004092 if (TimePassesIsEnabled)
4093 ExceptionTimer->startTimer();
4094
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004095 if (shouldEmitMovesModule || shouldEmitTableModule) {
4096 const std::vector<Function *> Personalities = MMI->getPersonalities();
Evan Chenge3d42322009-02-25 07:04:34 +00004097 for (unsigned i = 0; i < Personalities.size(); ++i)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004098 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00004099
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004100 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4101 E = EHFrames.end(); I != E; ++I)
4102 EmitEHFrame(*I);
4103 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004104
4105 if (TimePassesIsEnabled)
4106 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004107 }
4108
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004109 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00004110 /// emitted immediately after the function entry point.
4111 void BeginFunction(MachineFunction *MF) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004112 if (TimePassesIsEnabled)
4113 ExceptionTimer->startTimer();
4114
Jim Laskey072200c2007-01-29 18:51:14 +00004115 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004116 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004117
Bill Wendling163ba3f2009-03-10 21:23:25 +00004118 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004119 // Map all labels and get rid of any dead landing pads.
4120 MMI->TidyLandingPads();
Bill Wendling163ba3f2009-03-10 21:23:25 +00004121
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004122 // If any landing pads survive, we need an EH table.
4123 if (MMI->getLandingPads().size())
4124 shouldEmitTable = true;
4125
4126 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00004127 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004128 shouldEmitMoves = true;
4129
4130 if (shouldEmitMoves || shouldEmitTable)
4131 // Assumes in correct section after the entry point.
4132 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00004133 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004134
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004135 shouldEmitTableModule |= shouldEmitTable;
4136 shouldEmitMovesModule |= shouldEmitMoves;
Bill Wendling163ba3f2009-03-10 21:23:25 +00004137
4138 if (TimePassesIsEnabled)
4139 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004140 }
4141
4142 /// EndFunction - Gather and emit post-function exception information.
4143 ///
4144 void EndFunction() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004145 if (TimePassesIsEnabled)
4146 ExceptionTimer->startTimer();
4147
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004148 if (shouldEmitMoves || shouldEmitTable) {
4149 EmitLabel("eh_func_end", SubprogramCount);
4150 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00004151
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004152 // Save EH frame information
4153 EHFrames.
4154 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendling163ba3f2009-03-10 21:23:25 +00004155 SubprogramCount,
4156 MMI->getPersonalityIndex(),
4157 MF->getFrameInfo()->hasCalls(),
4158 !MMI->getLandingPads().empty(),
4159 MMI->getFrameMoves(),
4160 MF->getFunction()));
4161 }
4162
4163 if (TimePassesIsEnabled)
4164 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004165 }
4166};
4167
Jim Laskey0d086af2006-02-27 12:43:29 +00004168} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00004169
4170//===----------------------------------------------------------------------===//
4171
Jim Laskeyd18e2892006-01-20 20:34:06 +00004172/// Emit - Print the abbreviation using the specified Dwarf writer.
4173///
Jim Laskey072200c2007-01-29 18:51:14 +00004174void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004175 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00004176 DD.getAsm()->EmitULEB128Bytes(Tag);
4177 DD.getAsm()->EOL(TagString(Tag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004178
Jim Laskeyd18e2892006-01-20 20:34:06 +00004179 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00004180 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4181 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004182
Jim Laskeyd18e2892006-01-20 20:34:06 +00004183 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00004184 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004185 const DIEAbbrevData &AttrData = Data[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004186
Jim Laskeyd18e2892006-01-20 20:34:06 +00004187 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00004188 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4189 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004190
Jim Laskeyd18e2892006-01-20 20:34:06 +00004191 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00004192 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4193 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00004194 }
4195
4196 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00004197 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4198 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00004199}
4200
4201#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00004202void DIEAbbrev::print(std::ostream &O) {
4203 O << "Abbreviation @"
4204 << std::hex << (intptr_t)this << std::dec
4205 << " "
4206 << TagString(Tag)
4207 << " "
4208 << ChildrenString(ChildrenFlag)
4209 << "\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004210
Jim Laskeya0f3d172006-09-07 22:06:40 +00004211 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4212 O << " "
4213 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004214 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00004215 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004216 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00004217 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00004218}
Bill Wendlinge8156192006-12-07 01:30:32 +00004219void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00004220#endif
4221
4222//===----------------------------------------------------------------------===//
4223
Jim Laskeyef42a012006-11-02 20:12:39 +00004224#ifndef NDEBUG
4225void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004226 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004227}
Jim Laskeyef42a012006-11-02 20:12:39 +00004228#endif
4229
4230//===----------------------------------------------------------------------===//
4231
Jim Laskey063e7652006-01-17 17:31:53 +00004232/// EmitValue - Emit integer of appropriate size.
4233///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004234void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00004235 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00004236 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00004237 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004238 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004239 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004240 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004241 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004242 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004243 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004244 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4245 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4246 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004247 default: assert(0 && "DIE Value form not supported yet"); break;
4248 }
4249}
4250
4251/// SizeOf - Determine size of integer value in bytes.
4252///
Jim Laskey072200c2007-01-29 18:51:14 +00004253unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004254 switch (Form) {
4255 case DW_FORM_flag: // Fall thru
4256 case DW_FORM_ref1: // Fall thru
4257 case DW_FORM_data1: return sizeof(int8_t);
4258 case DW_FORM_ref2: // Fall thru
4259 case DW_FORM_data2: return sizeof(int16_t);
4260 case DW_FORM_ref4: // Fall thru
4261 case DW_FORM_data4: return sizeof(int32_t);
4262 case DW_FORM_ref8: // Fall thru
4263 case DW_FORM_data8: return sizeof(int64_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004264 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4265 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00004266 default: assert(0 && "DIE Value form not supported yet"); break;
4267 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004268 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00004269}
4270
Jim Laskey063e7652006-01-17 17:31:53 +00004271//===----------------------------------------------------------------------===//
4272
4273/// EmitValue - Emit string value.
4274///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004275void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004276 DD.getAsm()->EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00004277}
4278
Jim Laskey063e7652006-01-17 17:31:53 +00004279//===----------------------------------------------------------------------===//
4280
4281/// EmitValue - Emit label value.
4282///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004283void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004284 bool IsSmall = Form == DW_FORM_data4;
4285 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004286}
4287
4288/// SizeOf - Determine size of label value in bytes.
4289///
Jim Laskey072200c2007-01-29 18:51:14 +00004290unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004291 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004292 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004293}
Jim Laskeyef42a012006-11-02 20:12:39 +00004294
Jim Laskey063e7652006-01-17 17:31:53 +00004295//===----------------------------------------------------------------------===//
4296
Jim Laskeyd18e2892006-01-20 20:34:06 +00004297/// EmitValue - Emit label value.
4298///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004299void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004300 bool IsSmall = Form == DW_FORM_data4;
4301 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00004302}
4303
4304/// SizeOf - Determine size of label value in bytes.
4305///
Jim Laskey072200c2007-01-29 18:51:14 +00004306unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004307 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004308 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00004309}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004310
Jim Laskeyd18e2892006-01-20 20:34:06 +00004311//===----------------------------------------------------------------------===//
4312
Jim Laskey063e7652006-01-17 17:31:53 +00004313/// EmitValue - Emit delta value.
4314///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004315void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4316 bool IsSmall = Form == DW_FORM_data4;
4317 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4318 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4319}
4320
4321/// SizeOf - Determine size of delta value in bytes.
4322///
4323unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4324 if (Form == DW_FORM_data4) return 4;
4325 return DD.getTargetData()->getPointerSize();
4326}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004327
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004328//===----------------------------------------------------------------------===//
4329
4330/// EmitValue - Emit delta value.
4331///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004332void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004333 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00004334 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004335}
4336
4337/// SizeOf - Determine size of delta value in bytes.
4338///
Jim Laskey072200c2007-01-29 18:51:14 +00004339unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004340 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004341 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004342}
4343
4344//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004345
Jim Laskeyb8509c52006-03-23 18:07:55 +00004346/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004347///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004348void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004349 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00004350}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004351
Jim Laskeyd18e2892006-01-20 20:34:06 +00004352//===----------------------------------------------------------------------===//
4353
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004354/// ComputeSize - calculate the size of the block.
4355///
Jim Laskey072200c2007-01-29 18:51:14 +00004356unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00004357 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00004358 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004359
Jim Laskeyef42a012006-11-02 20:12:39 +00004360 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00004361 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00004362 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004363 }
4364 return Size;
4365}
4366
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004367/// EmitValue - Emit block data.
4368///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004369void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004370 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004371 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4372 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4373 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4374 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004375 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004376 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004377
Owen Anderson873e1b52008-06-24 21:44:59 +00004378 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00004379
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004380 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00004381 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00004382 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004383 }
4384}
4385
4386/// SizeOf - Determine size of block data in bytes.
4387///
Jim Laskey072200c2007-01-29 18:51:14 +00004388unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004389 switch (Form) {
4390 case DW_FORM_block1: return Size + sizeof(int8_t);
4391 case DW_FORM_block2: return Size + sizeof(int16_t);
4392 case DW_FORM_block4: return Size + sizeof(int32_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004393 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004394 default: assert(0 && "Improper form for block"); break;
4395 }
4396 return 0;
4397}
4398
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004399//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004400/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00004401
4402DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00004403 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00004404 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00004405}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004406
Jim Laskeyb8509c52006-03-23 18:07:55 +00004407/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4408///
4409void DIE::AddSiblingOffset() {
4410 DIEInteger *DI = new DIEInteger(0);
4411 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00004412 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00004413}
4414
Jim Laskeyef42a012006-11-02 20:12:39 +00004415/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004416///
Jim Laskeyef42a012006-11-02 20:12:39 +00004417void DIE::Profile(FoldingSetNodeID &ID) {
4418 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004419
Jim Laskeyef42a012006-11-02 20:12:39 +00004420 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4421 ID.AddPointer(Children[i]);
4422
4423 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4424 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00004425}
Jim Laskeyef42a012006-11-02 20:12:39 +00004426
4427#ifndef NDEBUG
4428void DIE::print(std::ostream &O, unsigned IncIndent) {
4429 static unsigned IndentCount = 0;
4430 IndentCount += IncIndent;
4431 const std::string Indent(IndentCount, ' ');
4432 bool isBlock = Abbrev.getTag() == 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004433
Jim Laskeyef42a012006-11-02 20:12:39 +00004434 if (!isBlock) {
4435 O << Indent
4436 << "Die: "
4437 << "0x" << std::hex << (intptr_t)this << std::dec
4438 << ", Offset: " << Offset
4439 << ", Size: " << Size
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004440 << "\n";
4441
Jim Laskeyef42a012006-11-02 20:12:39 +00004442 O << Indent
4443 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00004444 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00004445 << ChildrenString(Abbrev.getChildrenFlag());
4446 } else {
4447 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00004448 }
4449 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00004450
Owen Anderson873e1b52008-06-24 21:44:59 +00004451 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004452
Jim Laskeyef42a012006-11-02 20:12:39 +00004453 IndentCount += 2;
4454 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4455 O << Indent;
Bill Wendling4e974012008-07-22 00:28:47 +00004456
4457 if (!isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +00004458 O << AttributeString(Data[i].getAttribute());
Bill Wendling4e974012008-07-22 00:28:47 +00004459 else
Jim Laskeyef42a012006-11-02 20:12:39 +00004460 O << "Blk[" << i << "]";
Bill Wendling4e974012008-07-22 00:28:47 +00004461
Jim Laskeyef42a012006-11-02 20:12:39 +00004462 O << " "
4463 << FormEncodingString(Data[i].getForm())
4464 << " ";
4465 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00004466 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00004467 }
Jim Laskeyef42a012006-11-02 20:12:39 +00004468 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00004469
Jim Laskeyef42a012006-11-02 20:12:39 +00004470 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4471 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00004472 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004473
Jim Laskeyef42a012006-11-02 20:12:39 +00004474 if (!isBlock) O << "\n";
4475 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00004476}
4477
Jim Laskeyef42a012006-11-02 20:12:39 +00004478void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004479 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00004480}
Jim Laskeybd761842006-02-27 17:27:12 +00004481#endif
Jim Laskey65195462006-10-30 13:35:07 +00004482
4483//===----------------------------------------------------------------------===//
4484/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00004485///
Jim Laskey65195462006-10-30 13:35:07 +00004486
Bill Wendling91b8b802009-03-10 20:41:52 +00004487DwarfWriter::DwarfWriter()
Bill Wendling163ba3f2009-03-10 21:23:25 +00004488 : ImmutablePass(&ID), DD(0), DE(0) {}
Jim Laskey65195462006-10-30 13:35:07 +00004489
4490DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00004491 delete DE;
4492 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00004493}
4494
Jim Laskey65195462006-10-30 13:35:07 +00004495/// BeginModule - Emit all Dwarf sections that should come prior to the
4496/// content.
Devang Pateleb3fc282009-01-08 23:40:34 +00004497void DwarfWriter::BeginModule(Module *M,
4498 MachineModuleInfo *MMI,
4499 raw_ostream &OS, AsmPrinter *A,
4500 const TargetAsmInfo *T) {
4501 DE = new DwarfException(OS, A, T);
4502 DD = new DwarfDebug(OS, A, T);
Jim Laskey072200c2007-01-29 18:51:14 +00004503 DE->BeginModule(M);
4504 DD->BeginModule(M);
Devang Patel7bb89ed2009-01-13 00:20:51 +00004505 DD->SetDebugInfo(MMI);
Devang Pateleb3fc282009-01-08 23:40:34 +00004506 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00004507}
4508
4509/// EndModule - Emit all Dwarf sections that should come after the content.
4510///
4511void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00004512 DE->EndModule();
4513 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00004514}
4515
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004516/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00004517/// emitted immediately after the function entry point.
4518void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00004519 DE->BeginFunction(MF);
4520 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00004521}
4522
4523/// EndFunction - Gather and emit post-function debug information.
4524///
Bill Wendlingd751c642008-09-26 00:28:12 +00004525void DwarfWriter::EndFunction(MachineFunction *MF) {
4526 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004527 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004528
Bill Wendlingc91d0b92008-07-22 00:53:37 +00004529 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00004530 // Clear function debug information.
4531 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00004532}
Devang Patelccca7fe2009-01-12 19:17:34 +00004533
Devang Patelcf3a4482009-01-15 23:41:32 +00004534/// ValidDebugInfo - Return true if V represents valid debug info value.
4535bool DwarfWriter::ValidDebugInfo(Value *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004536 return DD && DD->ValidDebugInfo(V);
Devang Patelcf3a4482009-01-15 23:41:32 +00004537}
4538
Devang Patelccca7fe2009-01-12 19:17:34 +00004539/// RecordSourceLine - Records location information and associates it with a
4540/// label. Returns a unique label ID used to generate a label and provide
4541/// correspondence to the source line list.
4542unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
4543 unsigned Src) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004544 return DD->RecordSourceLine(Line, Col, Src);
Devang Patelccca7fe2009-01-12 19:17:34 +00004545}
4546
Evan Chenge3d42322009-02-25 07:04:34 +00004547/// getOrCreateSourceID - Look up the source id with the given directory and
4548/// source file names. If none currently exists, create a new id and insert it
4549/// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
4550/// as well.
4551unsigned DwarfWriter::getOrCreateSourceID(const std::string &DirName,
4552 const std::string &FileName) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004553 return DD->getOrCreateSourceID(DirName, FileName);
Devang Patelccca7fe2009-01-12 19:17:34 +00004554}
4555
4556/// RecordRegionStart - Indicate the start of a region.
4557unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004558 return DD->RecordRegionStart(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004559}
4560
4561/// RecordRegionEnd - Indicate the end of a region.
4562unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004563 return DD->RecordRegionEnd(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004564}
4565
4566/// getRecordSourceLineCount - Count source lines.
4567unsigned DwarfWriter::getRecordSourceLineCount() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004568 return DD->getRecordSourceLineCount();
Devang Patelccca7fe2009-01-12 19:17:34 +00004569}
Devang Patelbb8c5952009-01-13 21:25:00 +00004570
Devang Patelc48c5502009-01-13 21:44:10 +00004571/// RecordVariable - Indicate the declaration of a local variable.
4572///
4573void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
4574 DD->RecordVariable(GV, FrameIndex);
4575}
Devang Patelbbdc8202009-01-13 23:54:55 +00004576
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004577/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
4578/// be emitted.
4579bool DwarfWriter::ShouldEmitDwarfDebug() const {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004580 return DD->ShouldEmitDwarfDebug();
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004581}