blob: 88c0714bfa645651d39e70159f8c2a6e10c0a04a [file] [log] [blame]
Jim Laskeye5032892005-12-21 19:48:16 +00001//===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Jim Laskeye5032892005-12-21 19:48:16 +00007//
8//===----------------------------------------------------------------------===//
9//
Jim Laskey072200c2007-01-29 18:51:14 +000010// This file contains support for writing dwarf info into asm files.
Jim Laskeye5032892005-12-21 19:48:16 +000011//
12//===----------------------------------------------------------------------===//
Jim Laskey3ea0e0e2006-01-27 18:32:41 +000013
Jim Laskeyb2efb852006-01-04 22:28:25 +000014#include "llvm/CodeGen/DwarfWriter.h"
Jim Laskey52060a02006-01-24 00:49:18 +000015#include "llvm/Module.h"
Devang Pateld1ca9252009-01-05 23:03:32 +000016#include "llvm/DerivedTypes.h"
Devang Patelcf3a4482009-01-15 23:41:32 +000017#include "llvm/Constants.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000018#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskey44c3b9f2007-01-26 21:22:28 +000019#include "llvm/CodeGen/MachineModuleInfo.h"
Jim Laskey41886992006-04-07 16:34:46 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000021#include "llvm/CodeGen/MachineLocation.h"
Devang Patel08f053f2009-01-05 17:57:47 +000022#include "llvm/Analysis/DebugInfo.h"
Jim Laskey3f09fc22007-02-28 18:38:31 +000023#include "llvm/Support/Debug.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000024#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000025#include "llvm/Support/CommandLine.h"
Jim Laskey65195462006-10-30 13:35:07 +000026#include "llvm/Support/DataTypes.h"
Jim Laskey52060a02006-01-24 00:49:18 +000027#include "llvm/Support/Mangler.h"
Bill Wendling91b8b802009-03-10 20:41:52 +000028#include "llvm/Support/Timer.h"
Owen Andersoncb371882008-08-21 00:14:44 +000029#include "llvm/Support/raw_ostream.h"
Dan Gohman85496362007-09-24 21:32:18 +000030#include "llvm/System/Path.h"
Jim Laskey563321a2006-09-06 18:34:40 +000031#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000032#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000033#include "llvm/Target/TargetData.h"
Jim Laskey1069fbd2006-04-10 23:09:19 +000034#include "llvm/Target/TargetFrameInfo.h"
Duncan Sands53c3a332007-05-16 12:12:23 +000035#include "llvm/Target/TargetInstrInfo.h"
Jim Laskey1b340dc2007-01-29 20:48:32 +000036#include "llvm/Target/TargetMachine.h"
Jim Laskeyc1c47c32007-01-29 23:40:33 +000037#include "llvm/Target/TargetOptions.h"
Evan Chenge3d42322009-02-25 07:04:34 +000038#include "llvm/ADT/DenseMap.h"
39#include "llvm/ADT/FoldingSet.h"
40#include "llvm/ADT/StringExtras.h"
41#include "llvm/ADT/StringMap.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000042#include <ostream>
Jim Laskey65195462006-10-30 13:35:07 +000043#include <string>
Jim Laskeyb2efb852006-01-04 22:28:25 +000044using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000045using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000046
Devang Pateleb3fc282009-01-08 23:40:34 +000047static RegisterPass<DwarfWriter>
48X("dwarfwriter", "DWARF Information Writer");
49char DwarfWriter::ID = 0;
50
Bill Wendling91b8b802009-03-10 20:41:52 +000051static TimerGroup *DwarfTimerGroup = 0;
52static TimerGroup *getDwarfTimerGroup() {
53 if (DwarfTimerGroup) return DwarfTimerGroup;
54 return DwarfTimerGroup = new TimerGroup("Dwarf Exception and Debugging");
55}
56
Jim Laskey0d086af2006-02-27 12:43:29 +000057namespace llvm {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000058
Jim Laskey65195462006-10-30 13:35:07 +000059//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +000060
61/// Configuration values for initial hash set sizes (log2).
62///
Bill Wendlingb9dcef22009-02-03 21:17:20 +000063static const unsigned InitDiesSetSize = 9; // log2(512)
64static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
65static const unsigned InitValuesSetSize = 9; // log2(512)
Jim Laskeyef42a012006-11-02 20:12:39 +000066
67//===----------------------------------------------------------------------===//
68/// Forward declarations.
69///
70class DIE;
71class DIEValue;
72
73//===----------------------------------------------------------------------===//
Devang Pateld1ca9252009-01-05 23:03:32 +000074/// Utility routines.
75///
Devang Patelcf3a4482009-01-15 23:41:32 +000076/// getGlobalVariable - Return either a direct or cast Global value.
77///
78static GlobalVariable *getGlobalVariable(Value *V) {
79 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
80 return GV;
81 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
82 if (CE->getOpcode() == Instruction::BitCast) {
83 return dyn_cast<GlobalVariable>(CE->getOperand(0));
84 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
85 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
86 if (!CE->getOperand(i)->isNullValue())
87 return NULL;
88 }
89 return dyn_cast<GlobalVariable>(CE->getOperand(0));
90 }
91 }
92 return NULL;
93}
94
Devang Pateld1ca9252009-01-05 23:03:32 +000095//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000096/// DWLabel - Labels are used to track locations in the assembler file.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000097/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
98/// where the tag is a category of label (Ex. location) and number is a value
Reid Spencer181b6c92007-08-05 20:06:04 +000099/// unique in that category.
Jim Laskey65195462006-10-30 13:35:07 +0000100class DWLabel {
101public:
Jim Laskeyef42a012006-11-02 20:12:39 +0000102 /// Tag - Label category tag. Should always be a staticly declared C string.
103 ///
104 const char *Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000105
Jim Laskeyef42a012006-11-02 20:12:39 +0000106 /// Number - Value to make label unique.
107 ///
108 unsigned Number;
Jim Laskey65195462006-10-30 13:35:07 +0000109
110 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000111
Jim Laskeyef42a012006-11-02 20:12:39 +0000112 void Profile(FoldingSetNodeID &ID) const {
Evan Chenge3d42322009-02-25 07:04:34 +0000113 ID.AddString(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +0000114 ID.AddInteger(Number);
Jim Laskey90c79d72006-03-23 23:02:34 +0000115 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000116
Jim Laskeyef42a012006-11-02 20:12:39 +0000117#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000118 void print(std::ostream *O) const {
119 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000120 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000121 void print(std::ostream &O) const {
Jim Laskeybacd3042007-02-21 22:48:45 +0000122 O << "." << Tag;
Jim Laskeyef42a012006-11-02 20:12:39 +0000123 if (Number) O << Number;
124 }
125#endif
Jim Laskeybd761842006-02-27 17:27:12 +0000126};
127
128//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000129/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
130/// Dwarf abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +0000131class DIEAbbrevData {
132private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000133 /// Attribute - Dwarf attribute code.
134 ///
135 unsigned Attribute;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000136
Jim Laskeyef42a012006-11-02 20:12:39 +0000137 /// Form - Dwarf form code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000138 ///
139 unsigned Form;
140
Jim Laskey0d086af2006-02-27 12:43:29 +0000141public:
142 DIEAbbrevData(unsigned A, unsigned F)
143 : Attribute(A)
144 , Form(F)
145 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000146
Jim Laskeybd761842006-02-27 17:27:12 +0000147 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000148 unsigned getAttribute() const { return Attribute; }
149 unsigned getForm() const { return Form; }
Jim Laskey063e7652006-01-17 17:31:53 +0000150
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000151 /// Profile - Used to gather unique data for the abbreviation folding set.
Jim Laskey0d086af2006-02-27 12:43:29 +0000152 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000153 void Profile(FoldingSetNodeID &ID)const {
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000154 ID.AddInteger(Attribute);
155 ID.AddInteger(Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000156 }
157};
Jim Laskey063e7652006-01-17 17:31:53 +0000158
Jim Laskey0d086af2006-02-27 12:43:29 +0000159//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000160/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
161/// information object.
162class DIEAbbrev : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000163private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000164 /// Tag - Dwarf tag code.
165 ///
166 unsigned Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000167
Jim Laskeyef42a012006-11-02 20:12:39 +0000168 /// Unique number for node.
169 ///
170 unsigned Number;
171
172 /// ChildrenFlag - Dwarf children flag.
173 ///
174 unsigned ChildrenFlag;
175
176 /// Data - Raw data bytes for abbreviation.
177 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000178 SmallVector<DIEAbbrevData, 8> Data;
Jim Laskey063e7652006-01-17 17:31:53 +0000179
Jim Laskey0d086af2006-02-27 12:43:29 +0000180public:
Jim Laskey063e7652006-01-17 17:31:53 +0000181
Jim Laskey0d086af2006-02-27 12:43:29 +0000182 DIEAbbrev(unsigned T, unsigned C)
Jim Laskeyef42a012006-11-02 20:12:39 +0000183 : Tag(T)
Jim Laskey0d086af2006-02-27 12:43:29 +0000184 , ChildrenFlag(C)
185 , Data()
186 {}
187 ~DIEAbbrev() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000188
Jim Laskeybd761842006-02-27 17:27:12 +0000189 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000190 unsigned getTag() const { return Tag; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000191 unsigned getNumber() const { return Number; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000192 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000193 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000194 void setTag(unsigned T) { Tag = T; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000195 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000196 void setNumber(unsigned N) { Number = N; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000197
Jim Laskey0d086af2006-02-27 12:43:29 +0000198 /// AddAttribute - Adds another set of attribute information to the
199 /// abbreviation.
200 void AddAttribute(unsigned Attribute, unsigned Form) {
201 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000202 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000203
Jim Laskeyb8509c52006-03-23 18:07:55 +0000204 /// AddFirstAttribute - Adds a set of attribute information to the front
205 /// of the abbreviation.
206 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
207 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
208 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000209
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000210 /// Profile - Used to gather unique data for the abbreviation folding set.
211 ///
212 void Profile(FoldingSetNodeID &ID) {
213 ID.AddInteger(Tag);
214 ID.AddInteger(ChildrenFlag);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000215
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000216 // For each attribute description.
217 for (unsigned i = 0, N = Data.size(); i < N; ++i)
218 Data[i].Profile(ID);
219 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000220
Jim Laskey0d086af2006-02-27 12:43:29 +0000221 /// Emit - Print the abbreviation using the specified Dwarf writer.
222 ///
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000223 void Emit(const DwarfDebug &DD) const;
224
Jim Laskey0d086af2006-02-27 12:43:29 +0000225#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000226 void print(std::ostream *O) {
227 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000228 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000229 void print(std::ostream &O);
230 void dump();
231#endif
232};
Jim Laskey063e7652006-01-17 17:31:53 +0000233
Jim Laskey0d086af2006-02-27 12:43:29 +0000234//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000235/// DIE - A structured debug information entry. Has an abbreviation which
236/// describes it's organization.
237class DIE : public FoldingSetNode {
238protected:
239 /// Abbrev - Buffer for constructing abbreviation.
240 ///
241 DIEAbbrev Abbrev;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000242
Jim Laskeyef42a012006-11-02 20:12:39 +0000243 /// Offset - Offset in debug info section.
244 ///
245 unsigned Offset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000246
Jim Laskeyef42a012006-11-02 20:12:39 +0000247 /// Size - Size of instance + children.
248 ///
249 unsigned Size;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000250
Jim Laskeyef42a012006-11-02 20:12:39 +0000251 /// Children DIEs.
252 ///
253 std::vector<DIE *> Children;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000254
Jim Laskeyef42a012006-11-02 20:12:39 +0000255 /// Attributes values.
256 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000257 SmallVector<DIEValue*, 32> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000258
Jim Laskeyef42a012006-11-02 20:12:39 +0000259public:
Dan Gohman81975f62007-08-27 14:50:10 +0000260 explicit DIE(unsigned Tag)
Jim Laskeyef42a012006-11-02 20:12:39 +0000261 : Abbrev(Tag, DW_CHILDREN_no)
262 , Offset(0)
263 , Size(0)
264 , Children()
265 , Values()
266 {}
267 virtual ~DIE();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000268
Jim Laskeyef42a012006-11-02 20:12:39 +0000269 // Accessors.
270 DIEAbbrev &getAbbrev() { return Abbrev; }
271 unsigned getAbbrevNumber() const {
272 return Abbrev.getNumber();
273 }
Jim Laskey85f419b2006-11-09 16:32:26 +0000274 unsigned getTag() const { return Abbrev.getTag(); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000275 unsigned getOffset() const { return Offset; }
276 unsigned getSize() const { return Size; }
277 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000278 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000279 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
280 void setOffset(unsigned O) { Offset = O; }
281 void setSize(unsigned S) { Size = S; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000282
Jim Laskeyef42a012006-11-02 20:12:39 +0000283 /// AddValue - Add a value and attributes to a DIE.
284 ///
285 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
286 Abbrev.AddAttribute(Attribute, Form);
287 Values.push_back(Value);
288 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000289
Jim Laskeyef42a012006-11-02 20:12:39 +0000290 /// SiblingOffset - Return the offset of the debug information entry's
291 /// sibling.
292 unsigned SiblingOffset() const { return Offset + Size; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000293
Jim Laskeyef42a012006-11-02 20:12:39 +0000294 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
295 ///
296 void AddSiblingOffset();
297
298 /// AddChild - Add a child to the DIE.
299 ///
300 void AddChild(DIE *Child) {
301 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
302 Children.push_back(Child);
303 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000304
Jim Laskeyef42a012006-11-02 20:12:39 +0000305 /// Detach - Detaches objects connected to it after copying.
306 ///
307 void Detach() {
308 Children.clear();
309 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000310
Jim Laskeyef42a012006-11-02 20:12:39 +0000311 /// Profile - Used to gather unique data for the value folding set.
312 ///
313 void Profile(FoldingSetNodeID &ID) ;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000314
Jim Laskeyef42a012006-11-02 20:12:39 +0000315#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000316 void print(std::ostream *O, unsigned IncIndent = 0) {
317 if (O) print(*O, IncIndent);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000318 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000319 void print(std::ostream &O, unsigned IncIndent = 0);
320 void dump();
321#endif
322};
323
324//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000325/// DIEValue - A debug information entry value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000326///
327class DIEValue : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000328public:
329 enum {
330 isInteger,
331 isString,
332 isLabel,
333 isAsIsLabel,
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000334 isSectionOffset,
Jim Laskey0d086af2006-02-27 12:43:29 +0000335 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000336 isEntry,
337 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000338 };
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000339
Jim Laskeyef42a012006-11-02 20:12:39 +0000340 /// Type - Type of data stored in the value.
341 ///
342 unsigned Type;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000343
Dan Gohman81975f62007-08-27 14:50:10 +0000344 explicit DIEValue(unsigned T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000345 : Type(T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000346 {}
Jim Laskey0d086af2006-02-27 12:43:29 +0000347 virtual ~DIEValue() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000348
Jim Laskeyf6733882006-11-02 21:48:18 +0000349 // Accessors
Jim Laskeyef42a012006-11-02 20:12:39 +0000350 unsigned getType() const { return Type; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000351
Jim Laskey0d086af2006-02-27 12:43:29 +0000352 // Implement isa/cast/dyncast.
353 static bool classof(const DIEValue *) { return true; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000354
Jim Laskey0d086af2006-02-27 12:43:29 +0000355 /// EmitValue - Emit value via the Dwarf writer.
356 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000357 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000358
Jim Laskey0d086af2006-02-27 12:43:29 +0000359 /// SizeOf - Return the size of a value in bytes.
360 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000361 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000362
Jim Laskeyef42a012006-11-02 20:12:39 +0000363 /// Profile - Used to gather unique data for the value folding set.
364 ///
365 virtual void Profile(FoldingSetNodeID &ID) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000366
Jim Laskeyef42a012006-11-02 20:12:39 +0000367#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000368 void print(std::ostream *O) {
369 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000370 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000371 virtual void print(std::ostream &O) = 0;
372 void dump();
373#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000374};
Jim Laskey063e7652006-01-17 17:31:53 +0000375
Jim Laskey0d086af2006-02-27 12:43:29 +0000376//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000377/// DWInteger - An integer value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000378///
Jim Laskey0d086af2006-02-27 12:43:29 +0000379class DIEInteger : public DIEValue {
380private:
381 uint64_t Integer;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000382
Jim Laskey0d086af2006-02-27 12:43:29 +0000383public:
Dan Gohman81975f62007-08-27 14:50:10 +0000384 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000385
Jim Laskey0d086af2006-02-27 12:43:29 +0000386 // Implement isa/cast/dyncast.
387 static bool classof(const DIEInteger *) { return true; }
388 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000389
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000390 /// BestForm - Choose the best form for integer.
391 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000392 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
393 if (IsSigned) {
394 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
395 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
396 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
397 } else {
398 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
399 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
400 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
401 }
402 return DW_FORM_data8;
403 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000404
Jim Laskey0d086af2006-02-27 12:43:29 +0000405 /// EmitValue - Emit integer of appropriate size.
406 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000407 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000408
Jim Laskey0d086af2006-02-27 12:43:29 +0000409 /// SizeOf - Determine size of integer value in bytes.
410 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000411 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000412
Jim Laskeyef42a012006-11-02 20:12:39 +0000413 /// Profile - Used to gather unique data for the value folding set.
414 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000415 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000416 ID.AddInteger(isInteger);
Jim Laskeyef42a012006-11-02 20:12:39 +0000417 ID.AddInteger(Integer);
418 }
Jim Laskey5496f012006-11-09 14:52:14 +0000419 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000420
Jim Laskeyef42a012006-11-02 20:12:39 +0000421#ifndef NDEBUG
422 virtual void print(std::ostream &O) {
423 O << "Int: " << (int64_t)Integer
424 << " 0x" << std::hex << Integer << std::dec;
425 }
426#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000427};
Jim Laskey063e7652006-01-17 17:31:53 +0000428
Jim Laskey0d086af2006-02-27 12:43:29 +0000429//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000430/// DIEString - A string value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000431///
Jim Laskeyef42a012006-11-02 20:12:39 +0000432class DIEString : public DIEValue {
433public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000434 const std::string String;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000435
Dan Gohman81975f62007-08-27 14:50:10 +0000436 explicit DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000437
Jim Laskey0d086af2006-02-27 12:43:29 +0000438 // Implement isa/cast/dyncast.
439 static bool classof(const DIEString *) { return true; }
440 static bool classof(const DIEValue *S) { return S->Type == isString; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000441
Jim Laskey0d086af2006-02-27 12:43:29 +0000442 /// EmitValue - Emit string value.
443 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000444 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000445
Jim Laskey0d086af2006-02-27 12:43:29 +0000446 /// SizeOf - Determine size of string value in bytes.
447 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000448 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000449 return String.size() + sizeof(char); // sizeof('\0');
450 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000451
Jim Laskeyef42a012006-11-02 20:12:39 +0000452 /// Profile - Used to gather unique data for the value folding set.
453 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000454 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000455 ID.AddInteger(isString);
Jim Laskeyef42a012006-11-02 20:12:39 +0000456 ID.AddString(String);
457 }
Jim Laskey5496f012006-11-09 14:52:14 +0000458 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000459
Jim Laskeyef42a012006-11-02 20:12:39 +0000460#ifndef NDEBUG
461 virtual void print(std::ostream &O) {
462 O << "Str: \"" << String << "\"";
463 }
464#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000465};
Jim Laskey063e7652006-01-17 17:31:53 +0000466
Jim Laskey0d086af2006-02-27 12:43:29 +0000467//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000468/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000469//
Jim Laskeyef42a012006-11-02 20:12:39 +0000470class DIEDwarfLabel : public DIEValue {
471public:
472
Jim Laskey0d086af2006-02-27 12:43:29 +0000473 const DWLabel Label;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000474
Dan Gohman81975f62007-08-27 14:50:10 +0000475 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000476
Jim Laskey0d086af2006-02-27 12:43:29 +0000477 // Implement isa/cast/dyncast.
478 static bool classof(const DIEDwarfLabel *) { return true; }
479 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000480
Jim Laskey0d086af2006-02-27 12:43:29 +0000481 /// EmitValue - Emit label value.
482 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000483 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000484
Jim Laskey0d086af2006-02-27 12:43:29 +0000485 /// SizeOf - Determine size of label value in bytes.
486 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000487 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000488
Jim Laskeyef42a012006-11-02 20:12:39 +0000489 /// Profile - Used to gather unique data for the value folding set.
490 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000491 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000492 ID.AddInteger(isLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000493 Label.Profile(ID);
494 }
Jim Laskey5496f012006-11-09 14:52:14 +0000495 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000496
Jim Laskeyef42a012006-11-02 20:12:39 +0000497#ifndef NDEBUG
498 virtual void print(std::ostream &O) {
499 O << "Lbl: ";
500 Label.print(O);
501 }
502#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000503};
Jim Laskey063e7652006-01-17 17:31:53 +0000504
Jim Laskey063e7652006-01-17 17:31:53 +0000505
Jim Laskey0d086af2006-02-27 12:43:29 +0000506//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000507/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000508//
Jim Laskeyef42a012006-11-02 20:12:39 +0000509class DIEObjectLabel : public DIEValue {
510public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000511 const std::string Label;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000512
Dan Gohman81975f62007-08-27 14:50:10 +0000513 explicit DIEObjectLabel(const std::string &L)
514 : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000515
Jim Laskey0d086af2006-02-27 12:43:29 +0000516 // Implement isa/cast/dyncast.
517 static bool classof(const DIEObjectLabel *) { return true; }
518 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000519
Jim Laskey0d086af2006-02-27 12:43:29 +0000520 /// EmitValue - Emit label value.
521 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000522 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000523
Jim Laskey0d086af2006-02-27 12:43:29 +0000524 /// SizeOf - Determine size of label value in bytes.
525 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000526 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000527
Jim Laskeyef42a012006-11-02 20:12:39 +0000528 /// Profile - Used to gather unique data for the value folding set.
529 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000530 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000531 ID.AddInteger(isAsIsLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000532 ID.AddString(Label);
533 }
Evan Chenge3d42322009-02-25 07:04:34 +0000534 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label.c_str()); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000535
536#ifndef NDEBUG
537 virtual void print(std::ostream &O) {
538 O << "Obj: " << Label;
539 }
540#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000541};
Jim Laskey063e7652006-01-17 17:31:53 +0000542
Jim Laskey0d086af2006-02-27 12:43:29 +0000543//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000544/// DIESectionOffset - A section offset DIE.
545//
546class DIESectionOffset : public DIEValue {
547public:
548 const DWLabel Label;
549 const DWLabel Section;
550 bool IsEH : 1;
551 bool UseSet : 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000552
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000553 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
554 bool isEH = false, bool useSet = true)
555 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
556 IsEH(isEH), UseSet(useSet) {}
557
558 // Implement isa/cast/dyncast.
559 static bool classof(const DIESectionOffset *) { return true; }
560 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000561
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000562 /// EmitValue - Emit section offset.
563 ///
564 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000565
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000566 /// SizeOf - Determine size of section offset value in bytes.
567 ///
568 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000569
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000570 /// Profile - Used to gather unique data for the value folding set.
571 ///
572 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
573 const DWLabel &Section) {
574 ID.AddInteger(isSectionOffset);
575 Label.Profile(ID);
576 Section.Profile(ID);
577 // IsEH and UseSet are specific to the Label/Section that we will emit
578 // the offset for; so Label/Section are enough for uniqueness.
579 }
580 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
581
582#ifndef NDEBUG
583 virtual void print(std::ostream &O) {
584 O << "Off: ";
585 Label.print(O);
586 O << "-";
587 Section.print(O);
588 O << "-" << IsEH << "-" << UseSet;
589 }
590#endif
591};
592
593//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000594/// DIEDelta - A simple label difference DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000595///
Jim Laskeyef42a012006-11-02 20:12:39 +0000596class DIEDelta : public DIEValue {
597public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000598 const DWLabel LabelHi;
599 const DWLabel LabelLo;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000600
Jim Laskey0d086af2006-02-27 12:43:29 +0000601 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
602 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000603
Jim Laskey0d086af2006-02-27 12:43:29 +0000604 // Implement isa/cast/dyncast.
605 static bool classof(const DIEDelta *) { return true; }
606 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000607
Jim Laskey0d086af2006-02-27 12:43:29 +0000608 /// EmitValue - Emit delta value.
609 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000610 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000611
Jim Laskey0d086af2006-02-27 12:43:29 +0000612 /// SizeOf - Determine size of delta value in bytes.
613 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000614 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000615
Jim Laskeyef42a012006-11-02 20:12:39 +0000616 /// Profile - Used to gather unique data for the value folding set.
617 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000618 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
619 const DWLabel &LabelLo) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000620 ID.AddInteger(isDelta);
Jim Laskeyef42a012006-11-02 20:12:39 +0000621 LabelHi.Profile(ID);
622 LabelLo.Profile(ID);
623 }
Jim Laskey5496f012006-11-09 14:52:14 +0000624 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000625
626#ifndef NDEBUG
627 virtual void print(std::ostream &O) {
628 O << "Del: ";
629 LabelHi.print(O);
630 O << "-";
631 LabelLo.print(O);
632 }
633#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000634};
Jim Laskey063e7652006-01-17 17:31:53 +0000635
Jim Laskey0d086af2006-02-27 12:43:29 +0000636//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000637/// DIEntry - A pointer to another debug information entry. An instance of this
638/// class can also be used as a proxy for a debug information entry not yet
639/// defined (ie. types.)
640class DIEntry : public DIEValue {
641public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000642 DIE *Entry;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000643
Dan Gohman81975f62007-08-27 14:50:10 +0000644 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000645
Jim Laskey0d086af2006-02-27 12:43:29 +0000646 // Implement isa/cast/dyncast.
647 static bool classof(const DIEntry *) { return true; }
648 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000649
Jim Laskeyb8509c52006-03-23 18:07:55 +0000650 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000651 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000652 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000653
Jim Laskeyb8509c52006-03-23 18:07:55 +0000654 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000655 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000656 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000657 return sizeof(int32_t);
658 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000659
Jim Laskeyef42a012006-11-02 20:12:39 +0000660 /// Profile - Used to gather unique data for the value folding set.
661 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000662 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
663 ID.AddInteger(isEntry);
664 ID.AddPointer(Entry);
665 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000666 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000667 ID.AddInteger(isEntry);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000668
Jim Laskeyef42a012006-11-02 20:12:39 +0000669 if (Entry) {
670 ID.AddPointer(Entry);
671 } else {
672 ID.AddPointer(this);
673 }
674 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000675
Jim Laskeyef42a012006-11-02 20:12:39 +0000676#ifndef NDEBUG
677 virtual void print(std::ostream &O) {
678 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
679 }
680#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000681};
682
683//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000684/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000685//
Jim Laskeyef42a012006-11-02 20:12:39 +0000686class DIEBlock : public DIEValue, public DIE {
687public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000688 unsigned Size; // Size in bytes excluding size header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000689
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000690 DIEBlock()
691 : DIEValue(isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +0000692 , DIE(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000693 , Size(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000694 {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000695 ~DIEBlock() {
696 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000697
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000698 // Implement isa/cast/dyncast.
699 static bool classof(const DIEBlock *) { return true; }
700 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000701
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000702 /// ComputeSize - calculate the size of the block.
703 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000704 unsigned ComputeSize(DwarfDebug &DD);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000705
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000706 /// BestForm - Choose the best form for data.
707 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000708 unsigned BestForm() const {
709 if ((unsigned char)Size == Size) return DW_FORM_block1;
710 if ((unsigned short)Size == Size) return DW_FORM_block2;
711 if ((unsigned int)Size == Size) return DW_FORM_block4;
712 return DW_FORM_block;
713 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000714
715 /// EmitValue - Emit block data.
716 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000717 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000718
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000719 /// SizeOf - Determine size of block data in bytes.
720 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000721 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000722
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000723
Jim Laskeyef42a012006-11-02 20:12:39 +0000724 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000725 ///
Reid Spencer97821312006-11-02 23:56:21 +0000726 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000727 ID.AddInteger(isBlock);
Jim Laskeyef42a012006-11-02 20:12:39 +0000728 DIE::Profile(ID);
729 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000730
Jim Laskeyef42a012006-11-02 20:12:39 +0000731#ifndef NDEBUG
732 virtual void print(std::ostream &O) {
733 O << "Blk: ";
734 DIE::print(O, 5);
735 }
736#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000737};
738
739//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000740/// CompileUnit - This dwarf writer support class manages information associate
741/// with a source file.
742class CompileUnit {
Jim Laskey0d086af2006-02-27 12:43:29 +0000743private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000744 /// ID - File identifier for source.
745 ///
746 unsigned ID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000747
Jim Laskeyef42a012006-11-02 20:12:39 +0000748 /// Die - Compile unit debug information entry.
749 ///
750 DIE *Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000751
Devang Patelbbdc8202009-01-13 23:54:55 +0000752 /// GVToDieMap - Tracks the mapping of unit level debug informaton
753 /// variables to debug information entries.
Devang Patelc2997f42009-01-20 00:58:55 +0000754 std::map<GlobalVariable *, DIE *> GVToDieMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000755
Devang Patelbbdc8202009-01-13 23:54:55 +0000756 /// GVToDIEntryMap - Tracks the mapping of unit level debug informaton
Jim Laskeyef42a012006-11-02 20:12:39 +0000757 /// descriptors to debug information entries using a DIEntry proxy.
Devang Patelc2997f42009-01-20 00:58:55 +0000758 std::map<GlobalVariable *, DIEntry *> GVToDIEntryMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000759
760 /// Globals - A map of globally visible named entities for this unit.
761 ///
762 std::map<std::string, DIE *> Globals;
763
764 /// DiesSet - Used to uniquely define dies within the compile unit.
765 ///
766 FoldingSet<DIE> DiesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000767
Jim Laskey0d086af2006-02-27 12:43:29 +0000768public:
Devang Pateld1ca9252009-01-05 23:03:32 +0000769 CompileUnit(unsigned I, DIE *D)
Devang Patelbbdc8202009-01-13 23:54:55 +0000770 : ID(I), Die(D), GVToDieMap(),
Devang Patel7e55b3a2009-01-17 06:51:37 +0000771 GVToDIEntryMap(), Globals(), DiesSet(InitDiesSetSize)
Devang Pateld1ca9252009-01-05 23:03:32 +0000772 {}
773
Jim Laskeyef42a012006-11-02 20:12:39 +0000774 ~CompileUnit() {
775 delete Die;
Jim Laskeyef42a012006-11-02 20:12:39 +0000776 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000777
Jim Laskeybd761842006-02-27 17:27:12 +0000778 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000779 unsigned getID() const { return ID; }
780 DIE* getDie() const { return Die; }
781 std::map<std::string, DIE *> &getGlobals() { return Globals; }
782
783 /// hasContent - Return true if this compile unit has something to write out.
784 ///
785 bool hasContent() const {
786 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000787 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000788
789 /// AddGlobal - Add a new global entity to the compile unit.
790 ///
791 void AddGlobal(const std::string &Name, DIE *Die) {
792 Globals[Name] = Die;
793 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000794
Jim Laskeyef42a012006-11-02 20:12:39 +0000795 /// getDieMapSlotFor - Returns the debug information entry map slot for the
Devang Patelbbdc8202009-01-13 23:54:55 +0000796 /// specified debug variable.
Devang Patel48d190f2009-01-05 21:47:57 +0000797 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
798 return GVToDieMap[GV];
799 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000800
Jim Laskeyef42a012006-11-02 20:12:39 +0000801 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
Devang Patelbbdc8202009-01-13 23:54:55 +0000802 /// specified debug variable.
Devang Patel48d190f2009-01-05 21:47:57 +0000803 DIEntry *&getDIEntrySlotFor(GlobalVariable *GV) {
804 return GVToDIEntryMap[GV];
805 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000806
Jim Laskeyef42a012006-11-02 20:12:39 +0000807 /// AddDie - Adds or interns the DIE to the compile unit.
808 ///
809 DIE *AddDie(DIE &Buffer) {
810 FoldingSetNodeID ID;
811 Buffer.Profile(ID);
812 void *Where;
813 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000814
Jim Laskeyef42a012006-11-02 20:12:39 +0000815 if (!Die) {
816 Die = new DIE(Buffer);
817 DiesSet.InsertNode(Die, Where);
818 this->Die->AddChild(Die);
819 Buffer.Detach();
820 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000821
Jim Laskeyef42a012006-11-02 20:12:39 +0000822 return Die;
823 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000824};
825
Jim Laskey65195462006-10-30 13:35:07 +0000826//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000827/// Dwarf - Emits general Dwarf directives.
Jim Laskeyef42a012006-11-02 20:12:39 +0000828///
Jim Laskey65195462006-10-30 13:35:07 +0000829class Dwarf {
Jim Laskey072200c2007-01-29 18:51:14 +0000830protected:
Jim Laskey65195462006-10-30 13:35:07 +0000831 //===--------------------------------------------------------------------===//
Jim Laskey072200c2007-01-29 18:51:14 +0000832 // Core attributes used by the Dwarf writer.
Jim Laskey65195462006-10-30 13:35:07 +0000833 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000834
Jim Laskey65195462006-10-30 13:35:07 +0000835 //
836 /// O - Stream to .s file.
837 ///
Owen Andersoncb371882008-08-21 00:14:44 +0000838 raw_ostream &O;
Jim Laskey65195462006-10-30 13:35:07 +0000839
840 /// Asm - Target of Dwarf emission.
841 ///
842 AsmPrinter *Asm;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000843
Bill Wendlingaa8f8882008-07-01 23:34:48 +0000844 /// TAI - Target asm information.
Jim Laskey65195462006-10-30 13:35:07 +0000845 const TargetAsmInfo *TAI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000846
Jim Laskey65195462006-10-30 13:35:07 +0000847 /// TD - Target data.
848 const TargetData *TD;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000849
Jim Laskey65195462006-10-30 13:35:07 +0000850 /// RI - Register Information.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000851 const TargetRegisterInfo *RI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000852
Jim Laskey65195462006-10-30 13:35:07 +0000853 /// M - Current module.
854 ///
855 Module *M;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000856
Jim Laskey65195462006-10-30 13:35:07 +0000857 /// MF - Current machine function.
858 ///
859 MachineFunction *MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000860
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000861 /// MMI - Collected machine module information.
Jim Laskey65195462006-10-30 13:35:07 +0000862 ///
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000863 MachineModuleInfo *MMI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000864
Jim Laskey65195462006-10-30 13:35:07 +0000865 /// SubprogramCount - The running count of functions being compiled.
866 ///
867 unsigned SubprogramCount;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000868
Chris Lattner9251f132007-09-24 03:35:37 +0000869 /// Flavor - A unique string indicating what dwarf producer this is, used to
870 /// unique labels.
871 const char * const Flavor;
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000872
873 unsigned SetCounter;
Owen Andersoncb371882008-08-21 00:14:44 +0000874 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattner9251f132007-09-24 03:35:37 +0000875 const char *flavor)
Jim Laskey072200c2007-01-29 18:51:14 +0000876 : O(OS)
877 , Asm(A)
878 , TAI(T)
879 , TD(Asm->TM.getTargetData())
880 , RI(Asm->TM.getRegisterInfo())
881 , M(NULL)
882 , MF(NULL)
883 , MMI(NULL)
Jim Laskey072200c2007-01-29 18:51:14 +0000884 , SubprogramCount(0)
Chris Lattner9251f132007-09-24 03:35:37 +0000885 , Flavor(flavor)
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000886 , SetCounter(1)
Jim Laskey072200c2007-01-29 18:51:14 +0000887 {
888 }
889
890public:
Jim Laskey072200c2007-01-29 18:51:14 +0000891 //===--------------------------------------------------------------------===//
892 // Accessors.
893 //
894 AsmPrinter *getAsm() const { return Asm; }
Jim Laskeyb82313f2007-02-01 16:31:34 +0000895 MachineModuleInfo *getMMI() const { return MMI; }
Jim Laskey072200c2007-01-29 18:51:14 +0000896 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohman82482942007-09-27 23:12:31 +0000897 const TargetData *getTargetData() const { return TD; }
Jim Laskey072200c2007-01-29 18:51:14 +0000898
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000899 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
900 const {
901 if (isInSection && TAI->getDwarfSectionOffsetDirective())
902 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohman82482942007-09-27 23:12:31 +0000903 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000904 O << TAI->getData32bitsDirective();
905 else
906 O << TAI->getData64bitsDirective();
907 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000908
Jim Laskeyb82313f2007-02-01 16:31:34 +0000909 /// PrintLabelName - Print label name in form used by Dwarf writer.
910 ///
911 void PrintLabelName(DWLabel Label) const {
912 PrintLabelName(Label.Tag, Label.Number);
913 }
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000914 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000915 O << TAI->getPrivateGlobalPrefix() << Tag;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000916 if (Number) O << Number;
917 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000918
Chris Lattner9251f132007-09-24 03:35:37 +0000919 void PrintLabelName(const char *Tag, unsigned Number,
920 const char *Suffix) const {
921 O << TAI->getPrivateGlobalPrefix() << Tag;
922 if (Number) O << Number;
923 O << Suffix;
924 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000925
Jim Laskeyb82313f2007-02-01 16:31:34 +0000926 /// EmitLabel - Emit location label for internal use by Dwarf.
927 ///
928 void EmitLabel(DWLabel Label) const {
929 EmitLabel(Label.Tag, Label.Number);
930 }
931 void EmitLabel(const char *Tag, unsigned Number) const {
932 PrintLabelName(Tag, Number);
933 O << ":\n";
934 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000935
Jim Laskeyb82313f2007-02-01 16:31:34 +0000936 /// EmitReference - Emit a reference to a label.
937 ///
Dan Gohman06ff4e62007-09-28 15:43:33 +0000938 void EmitReference(DWLabel Label, bool IsPCRelative = false,
939 bool Force32Bit = false) const {
940 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000941 }
942 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman06ff4e62007-09-28 15:43:33 +0000943 bool IsPCRelative = false, bool Force32Bit = false) const {
944 PrintRelDirective(Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000945 PrintLabelName(Tag, Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000946
Jim Laskeyb82313f2007-02-01 16:31:34 +0000947 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
948 }
Dan Gohman06ff4e62007-09-28 15:43:33 +0000949 void EmitReference(const std::string &Name, bool IsPCRelative = false,
950 bool Force32Bit = false) const {
951 PrintRelDirective(Force32Bit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000952
Jim Laskeyb82313f2007-02-01 16:31:34 +0000953 O << Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000954
Jim Laskeyb82313f2007-02-01 16:31:34 +0000955 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
956 }
957
958 /// EmitDifference - Emit the difference between two labels. Some
959 /// assemblers do not behave with absolute expressions with data directives,
960 /// so there is an option (needsSet) to use an intermediary set expression.
961 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000962 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000963 EmitDifference(LabelHi.Tag, LabelHi.Number,
964 LabelLo.Tag, LabelLo.Number,
965 IsSmall);
966 }
967 void EmitDifference(const char *TagHi, unsigned NumberHi,
968 const char *TagLo, unsigned NumberLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000969 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000970 if (TAI->needsSet()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000971 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000972 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000973 O << ",";
974 PrintLabelName(TagHi, NumberHi);
975 O << "-";
976 PrintLabelName(TagLo, NumberLo);
977 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000978
979 PrintRelDirective(IsSmall);
Chris Lattner9251f132007-09-24 03:35:37 +0000980 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000981 ++SetCounter;
982 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000983 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000984
Jim Laskeyb82313f2007-02-01 16:31:34 +0000985 PrintLabelName(TagHi, NumberHi);
986 O << "-";
987 PrintLabelName(TagLo, NumberLo);
988 }
989 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000990
991 void EmitSectionOffset(const char* Label, const char* Section,
992 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000993 bool IsSmall = false, bool isEH = false,
994 bool useSet = true) {
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000995 bool printAbsolute = false;
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000996 if (isEH)
997 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
998 else
999 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
1000
1001 if (TAI->needsSet() && useSet) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001002 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +00001003 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001004 O << ",";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001005 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001006
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001007 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001008 O << "-";
1009 PrintLabelName(Section, SectionNumber);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001010 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001011 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001012
1013 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001014
Chris Lattner9251f132007-09-24 03:35:37 +00001015 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001016 ++SetCounter;
1017 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001018 PrintRelDirective(IsSmall, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001019
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001020 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001021
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001022 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001023 O << "-";
1024 PrintLabelName(Section, SectionNumber);
1025 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001026 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001027 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001028
Jim Laskeyb82313f2007-02-01 16:31:34 +00001029 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1030 /// frame.
1031 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenb97aec62007-11-13 19:13:01 +00001032 const std::vector<MachineMove> &Moves, bool isEH) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001033 int stackGrowth =
1034 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1035 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00001036 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeybacd3042007-02-21 22:48:45 +00001037 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
Jim Laskeyb82313f2007-02-01 16:31:34 +00001038
1039 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001040 const MachineMove &Move = Moves[i];
Jim Laskeyb82313f2007-02-01 16:31:34 +00001041 unsigned LabelID = Move.getLabelID();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001042
Jim Laskeyb82313f2007-02-01 16:31:34 +00001043 if (LabelID) {
1044 LabelID = MMI->MappedLabel(LabelID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001045
Jim Laskeyb82313f2007-02-01 16:31:34 +00001046 // Throw out move if the label is invalid.
1047 if (!LabelID) continue;
1048 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001049
Jim Laskeyb82313f2007-02-01 16:31:34 +00001050 const MachineLocation &Dst = Move.getDestination();
1051 const MachineLocation &Src = Move.getSource();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001052
Jim Laskeyb82313f2007-02-01 16:31:34 +00001053 // Advance row if new location.
1054 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1055 Asm->EmitInt8(DW_CFA_advance_loc4);
1056 Asm->EOL("DW_CFA_advance_loc4");
Jim Laskeybacd3042007-02-21 22:48:45 +00001057 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1058 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001059
Jim Laskeyb82313f2007-02-01 16:31:34 +00001060 BaseLabelID = LabelID;
Jim Laskeybacd3042007-02-21 22:48:45 +00001061 BaseLabel = "label";
Jim Laskeyb82313f2007-02-01 16:31:34 +00001062 IsLocal = true;
1063 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001064
Jim Laskeyb82313f2007-02-01 16:31:34 +00001065 // If advancing cfa.
Dan Gohmand735b802008-10-03 15:45:36 +00001066 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1067 if (!Src.isReg()) {
1068 if (Src.getReg() == MachineLocation::VirtualFP) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001069 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1070 Asm->EOL("DW_CFA_def_cfa_offset");
1071 } else {
1072 Asm->EmitInt8(DW_CFA_def_cfa);
1073 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmand735b802008-10-03 15:45:36 +00001074 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001075 Asm->EOL("Register");
1076 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001077
Jim Laskeyb82313f2007-02-01 16:31:34 +00001078 int Offset = -Src.getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001079
Jim Laskeyb82313f2007-02-01 16:31:34 +00001080 Asm->EmitULEB128Bytes(Offset);
1081 Asm->EOL("Offset");
1082 } else {
1083 assert(0 && "Machine move no supported yet.");
1084 }
Dan Gohmand735b802008-10-03 15:45:36 +00001085 } else if (Src.isReg() &&
1086 Src.getReg() == MachineLocation::VirtualFP) {
1087 if (Dst.isReg()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001088 Asm->EmitInt8(DW_CFA_def_cfa_register);
1089 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmand735b802008-10-03 15:45:36 +00001090 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001091 Asm->EOL("Register");
1092 } else {
1093 assert(0 && "Machine move no supported yet.");
1094 }
1095 } else {
Dan Gohmand735b802008-10-03 15:45:36 +00001096 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001097 int Offset = Dst.getOffset() / stackGrowth;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001098
Jim Laskeyb82313f2007-02-01 16:31:34 +00001099 if (Offset < 0) {
1100 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1101 Asm->EOL("DW_CFA_offset_extended_sf");
1102 Asm->EmitULEB128Bytes(Reg);
1103 Asm->EOL("Reg");
1104 Asm->EmitSLEB128Bytes(Offset);
1105 Asm->EOL("Offset");
1106 } else if (Reg < 64) {
1107 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Chengd4114192008-07-09 21:53:02 +00001108 if (VerboseAsm)
1109 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1110 else
1111 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00001112 Asm->EmitULEB128Bytes(Offset);
1113 Asm->EOL("Offset");
1114 } else {
1115 Asm->EmitInt8(DW_CFA_offset_extended);
1116 Asm->EOL("DW_CFA_offset_extended");
1117 Asm->EmitULEB128Bytes(Reg);
1118 Asm->EOL("Reg");
1119 Asm->EmitULEB128Bytes(Offset);
1120 Asm->EOL("Offset");
1121 }
1122 }
1123 }
1124 }
1125
Jim Laskey072200c2007-01-29 18:51:14 +00001126};
1127
1128//===----------------------------------------------------------------------===//
Devang Patelf3ee5142009-01-12 22:54:42 +00001129/// SrcLineInfo - This class is used to record source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001130///
1131class SrcLineInfo {
1132 unsigned Line; // Source line number.
1133 unsigned Column; // Source column.
1134 unsigned SourceID; // Source ID number.
1135 unsigned LabelID; // Label in code ID number.
1136public:
1137 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001138 : Line(L), Column(C), SourceID(S), LabelID(I) {}
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001139
1140 // Accessors
1141 unsigned getLine() const { return Line; }
1142 unsigned getColumn() const { return Column; }
1143 unsigned getSourceID() const { return SourceID; }
1144 unsigned getLabelID() const { return LabelID; }
1145};
1146
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001147//===----------------------------------------------------------------------===//
Devang Patel7a6e5a32009-01-08 02:33:41 +00001148/// DbgVariable - This class is used to track local variable information.
1149///
1150class DbgVariable {
Devang Patel99ec3532009-01-16 19:28:14 +00001151 DIVariable Var; // Variable Descriptor.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001152 unsigned FrameIndex; // Variable frame index.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001153public:
Devang Patel99ec3532009-01-16 19:28:14 +00001154 DbgVariable(DIVariable V, unsigned I) : Var(V), FrameIndex(I) {}
Devang Patel7a6e5a32009-01-08 02:33:41 +00001155
1156 // Accessors.
Devang Patel99ec3532009-01-16 19:28:14 +00001157 DIVariable getVariable() const { return Var; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001158 unsigned getFrameIndex() const { return FrameIndex; }
1159};
1160
1161//===----------------------------------------------------------------------===//
1162/// DbgScope - This class is used to track scope information.
1163///
1164class DbgScope {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001165 DbgScope *Parent; // Parent to this scope.
Devang Patel7103c6a2009-01-16 18:01:58 +00001166 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001167 // Either subprogram or block.
1168 unsigned StartLabelID; // Label ID of the beginning of scope.
1169 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel7103c6a2009-01-16 18:01:58 +00001170 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
Devang Patel9795da52009-01-10 02:42:49 +00001171 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001172public:
Devang Patel0e5200f2009-01-15 18:25:17 +00001173 DbgScope(DbgScope *P, DIDescriptor D)
Devang Patel7a6e5a32009-01-08 02:33:41 +00001174 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), Scopes(), Variables()
1175 {}
Devang Patel481ff5b2009-01-12 18:48:36 +00001176 ~DbgScope() {
1177 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1178 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1179 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001180
1181 // Accessors.
Devang Patel7103c6a2009-01-16 18:01:58 +00001182 DbgScope *getParent() const { return Parent; }
1183 DIDescriptor getDesc() const { return Desc; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001184 unsigned getStartLabelID() const { return StartLabelID; }
1185 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel9795da52009-01-10 02:42:49 +00001186 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1187 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001188 void setStartLabelID(unsigned S) { StartLabelID = S; }
1189 void setEndLabelID(unsigned E) { EndLabelID = E; }
1190
1191 /// AddScope - Add a scope to the scope.
1192 ///
1193 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1194
1195 /// AddVariable - Add a variable to the scope.
1196 ///
1197 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
1198};
1199
1200//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001201/// DwarfDebug - Emits Dwarf debug directives.
Jim Laskey072200c2007-01-29 18:51:14 +00001202///
1203class DwarfDebug : public Dwarf {
Jim Laskey65195462006-10-30 13:35:07 +00001204 //===--------------------------------------------------------------------===//
1205 // Attributes used to construct specific Dwarf sections.
1206 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001207
Evan Chenge3d42322009-02-25 07:04:34 +00001208 /// CompileUnitMap - A map of global variables representing compile units to
1209 /// compile units.
1210 DenseMap<Value *, CompileUnit *> CompileUnitMap;
1211
1212 /// CompileUnits - All the compile units in this module.
1213 ///
1214 SmallVector<CompileUnit *, 8> CompileUnits;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001215
Devang Pateldd9db662009-01-30 18:20:31 +00001216 /// MainCU - Some platform prefers one compile unit per .o file. In such
1217 /// cases, all dies are inserted in MainCU.
1218 CompileUnit *MainCU;
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001219
Jim Laskeyef42a012006-11-02 20:12:39 +00001220 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +00001221 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001222 FoldingSet<DIEAbbrev> AbbreviationsSet;
1223
1224 /// Abbreviations - A list of all the unique abbreviations in use.
1225 ///
1226 std::vector<DIEAbbrev *> Abbreviations;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001227
Evan Chenge3d42322009-02-25 07:04:34 +00001228 /// DirectoryIdMap - Directory name to directory id map.
1229 ///
1230 StringMap<unsigned> DirectoryIdMap;
Devang Patel8526cc02009-01-05 22:35:52 +00001231
Evan Chenge3d42322009-02-25 07:04:34 +00001232 /// DirectoryNames - A list of directory names.
1233 SmallVector<std::string, 8> DirectoryNames;
1234
1235 /// SourceFileIdMap - Source file name to source file id map.
1236 ///
1237 StringMap<unsigned> SourceFileIdMap;
1238
1239 /// SourceFileNames - A list of source file names.
1240 SmallVector<std::string, 8> SourceFileNames;
1241
1242 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
1243 /// id mapped to a unique id.
1244 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
1245
1246 /// SourceIds - Reverse map from source id to directory id + file id pair.
1247 ///
1248 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
Devang Patel8526cc02009-01-05 22:35:52 +00001249
Devang Patel07354e52009-01-16 21:07:53 +00001250 /// Lines - List of of source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001251 std::vector<SrcLineInfo> Lines;
1252
Devang Patel07354e52009-01-16 21:07:53 +00001253 /// ValuesSet - Used to uniquely define values.
1254 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001255 FoldingSet<DIEValue> ValuesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001256
Jim Laskeyef42a012006-11-02 20:12:39 +00001257 /// Values - A list of all the unique values in use.
1258 ///
1259 std::vector<DIEValue *> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001260
Jim Laskey65195462006-10-30 13:35:07 +00001261 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +00001262 ///
Jim Laskey65195462006-10-30 13:35:07 +00001263 UniqueVector<std::string> StringPool;
1264
Jim Laskey65195462006-10-30 13:35:07 +00001265 /// SectionMap - Provides a unique id per text section.
1266 ///
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001267 UniqueVector<const Section*> SectionMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001268
Jim Laskey65195462006-10-30 13:35:07 +00001269 /// SectionSourceLines - Tracks line numbers per text section.
1270 ///
Devang Patelf3ee5142009-01-12 22:54:42 +00001271 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Jim Laskey65195462006-10-30 13:35:07 +00001272
Jim Laskeybacd3042007-02-21 22:48:45 +00001273 /// didInitial - Flag to indicate if initial emission has been done.
1274 ///
1275 bool didInitial;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001276
Jim Laskeybacd3042007-02-21 22:48:45 +00001277 /// shouldEmit - Flag to indicate if debug information should be emitted.
1278 ///
1279 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +00001280
Devang Patel0e5200f2009-01-15 18:25:17 +00001281 // RootDbgScope - Top level scope for the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001282 //
1283 DbgScope *RootDbgScope;
1284
Bill Wendling163ba3f2009-03-10 21:23:25 +00001285 /// DbgScopeMap - Tracks the scopes in the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001286 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
Bill Wendling163ba3f2009-03-10 21:23:25 +00001287
1288 /// DebugTimer - Timer for the Dwarf debug writer.
1289 Timer *DebugTimer;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001290
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001291 struct FunctionDebugFrameInfo {
1292 unsigned Number;
1293 std::vector<MachineMove> Moves;
1294
1295 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001296 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001297 };
1298
1299 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001300
Bill Wendling163ba3f2009-03-10 21:23:25 +00001301private:
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001302 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
Bill Wendlingc8615e42009-03-10 21:47:45 +00001303 /// maps to the source id. Source id starts at 1.
1304 std::pair<unsigned, unsigned>
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001305 getSourceDirectoryAndFileIds(unsigned SId) const {
Bill Wendlingc8615e42009-03-10 21:47:45 +00001306 return SourceIds[SId-1];
1307 }
1308
1309 /// getNumSourceDirectories - Return the number of source directories in the
1310 /// debug info.
1311 unsigned getNumSourceDirectories() const {
1312 return DirectoryNames.size();
1313 }
1314
1315 /// getSourceDirectoryName - Return the name of the directory corresponding
1316 /// to the id.
1317 const std::string &getSourceDirectoryName(unsigned Id) const {
1318 return DirectoryNames[Id - 1];
1319 }
1320
1321 /// getSourceFileName - Return the name of the source file corresponding
1322 /// to the id.
1323 const std::string &getSourceFileName(unsigned Id) const {
1324 return SourceFileNames[Id - 1];
1325 }
1326
1327 /// getNumSourceIds - Return the number of unique source ids.
Bill Wendlingc8615e42009-03-10 21:47:45 +00001328 unsigned getNumSourceIds() const {
1329 return SourceIds.size();
1330 }
1331
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001332 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001333 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001334 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1335 // Profile the node so that we can make it unique.
1336 FoldingSetNodeID ID;
1337 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001338
Jim Laskeyef42a012006-11-02 20:12:39 +00001339 // Check the set for priors.
1340 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001341
Jim Laskeyef42a012006-11-02 20:12:39 +00001342 // If it's newly added.
1343 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001344 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +00001345 Abbreviations.push_back(&Abbrev);
1346 // Assign the vector position + 1 as its number.
1347 Abbrev.setNumber(Abbreviations.size());
1348 } else {
1349 // Assign existing abbreviation number.
1350 Abbrev.setNumber(InSet->getNumber());
1351 }
1352 }
1353
Jim Laskey65195462006-10-30 13:35:07 +00001354 /// NewString - Add a string to the constant pool and returns a label.
1355 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001356 DWLabel NewString(const std::string &String) {
1357 unsigned StringID = StringPool.insert(String);
1358 return DWLabel("string", StringID);
1359 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001360
Jim Laskeyef42a012006-11-02 20:12:39 +00001361 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1362 /// entry.
1363 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1364 DIEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001365
Jim Laskeyef42a012006-11-02 20:12:39 +00001366 if (Entry) {
1367 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001368 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001369 void *Where;
1370 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001371
Jim Laskeyf6733882006-11-02 21:48:18 +00001372 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001373
Jim Laskeyef42a012006-11-02 20:12:39 +00001374 Value = new DIEntry(Entry);
1375 ValuesSet.InsertNode(Value, Where);
1376 } else {
1377 Value = new DIEntry(Entry);
1378 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001379
Jim Laskeyef42a012006-11-02 20:12:39 +00001380 Values.push_back(Value);
1381 return Value;
1382 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001383
Jim Laskeyef42a012006-11-02 20:12:39 +00001384 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1385 ///
1386 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1387 Value->Entry = Entry;
1388 // Add to values set if not already there. If it is, we merely have a
1389 // duplicate in the values list (no harm.)
1390 ValuesSet.GetOrInsertNode(Value);
1391 }
1392
1393 /// AddUInt - Add an unsigned integer attribute data and value.
1394 ///
1395 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1396 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1397
1398 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001399 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001400 void *Where;
1401 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1402 if (!Value) {
1403 Value = new DIEInteger(Integer);
1404 ValuesSet.InsertNode(Value, Where);
1405 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001406 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001407
Jim Laskeyef42a012006-11-02 20:12:39 +00001408 Die->AddValue(Attribute, Form, Value);
1409 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001410
Jim Laskeyef42a012006-11-02 20:12:39 +00001411 /// AddSInt - Add an signed integer attribute data and value.
1412 ///
1413 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1414 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1415
1416 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001417 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001418 void *Where;
1419 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1420 if (!Value) {
1421 Value = new DIEInteger(Integer);
1422 ValuesSet.InsertNode(Value, Where);
1423 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001424 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001425
Jim Laskeyef42a012006-11-02 20:12:39 +00001426 Die->AddValue(Attribute, Form, Value);
1427 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001428
Evan Chenge3d42322009-02-25 07:04:34 +00001429 /// AddString - Add a string attribute data and value.
Jim Laskeyef42a012006-11-02 20:12:39 +00001430 ///
1431 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1432 const std::string &String) {
1433 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001434 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001435 void *Where;
1436 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1437 if (!Value) {
1438 Value = new DIEString(String);
1439 ValuesSet.InsertNode(Value, Where);
1440 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001441 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001442
Jim Laskeyef42a012006-11-02 20:12:39 +00001443 Die->AddValue(Attribute, Form, Value);
1444 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001445
Jim Laskeyef42a012006-11-02 20:12:39 +00001446 /// AddLabel - Add a Dwarf label attribute data and value.
1447 ///
1448 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1449 const DWLabel &Label) {
1450 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001451 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001452 void *Where;
1453 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1454 if (!Value) {
1455 Value = new DIEDwarfLabel(Label);
1456 ValuesSet.InsertNode(Value, Where);
1457 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001458 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001459
Jim Laskeyef42a012006-11-02 20:12:39 +00001460 Die->AddValue(Attribute, Form, Value);
1461 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001462
Jim Laskeyef42a012006-11-02 20:12:39 +00001463 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1464 ///
1465 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1466 const std::string &Label) {
1467 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001468 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001469 void *Where;
1470 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1471 if (!Value) {
1472 Value = new DIEObjectLabel(Label);
1473 ValuesSet.InsertNode(Value, Where);
1474 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001475 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001476
Jim Laskeyef42a012006-11-02 20:12:39 +00001477 Die->AddValue(Attribute, Form, Value);
1478 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001479
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001480 /// AddSectionOffset - Add a section offset label attribute data and value.
1481 ///
1482 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1483 const DWLabel &Label, const DWLabel &Section,
1484 bool isEH = false, bool useSet = true) {
1485 FoldingSetNodeID ID;
1486 DIESectionOffset::Profile(ID, Label, Section);
1487 void *Where;
1488 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1489 if (!Value) {
1490 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1491 ValuesSet.InsertNode(Value, Where);
1492 Values.push_back(Value);
1493 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001494
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001495 Die->AddValue(Attribute, Form, Value);
1496 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001497
Jim Laskeyef42a012006-11-02 20:12:39 +00001498 /// AddDelta - Add a label delta attribute data and value.
1499 ///
1500 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1501 const DWLabel &Hi, const DWLabel &Lo) {
1502 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001503 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001504 void *Where;
1505 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1506 if (!Value) {
1507 Value = new DIEDelta(Hi, Lo);
1508 ValuesSet.InsertNode(Value, Where);
1509 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001510 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001511
Jim Laskeyef42a012006-11-02 20:12:39 +00001512 Die->AddValue(Attribute, Form, Value);
1513 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001514
Jim Laskeyef42a012006-11-02 20:12:39 +00001515 /// AddDIEntry - Add a DIE attribute data and value.
1516 ///
1517 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1518 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1519 }
1520
1521 /// AddBlock - Add block data.
1522 ///
1523 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1524 Block->ComputeSize(*this);
1525 FoldingSetNodeID ID;
1526 Block->Profile(ID);
1527 void *Where;
1528 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1529 if (!Value) {
1530 Value = Block;
1531 ValuesSet.InsertNode(Value, Where);
1532 Values.push_back(Value);
1533 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001534 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001535 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001536 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001537 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001538
Jim Laskeyef42a012006-11-02 20:12:39 +00001539 Die->AddValue(Attribute, Block->BestForm(), Value);
1540 }
1541
Jim Laskey65195462006-10-30 13:35:07 +00001542 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001543 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001544 void AddSourceLine(DIE *Die, const DIVariable *V) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001545 unsigned FileID = 0;
1546 unsigned Line = V->getLineNumber();
Devang Pateldd9db662009-01-30 18:20:31 +00001547 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1548 FileID = Unit->getID();
Devang Patel2303df92009-02-10 06:04:08 +00001549 assert (FileID && "Invalid file id");
Devang Patel7a6e5a32009-01-08 02:33:41 +00001550 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1551 AddUInt(Die, DW_AT_decl_line, 0, Line);
1552 }
1553
1554 /// AddSourceLine - Add location information to specified debug information
1555 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001556 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Devang Patel8526cc02009-01-05 22:35:52 +00001557 unsigned FileID = 0;
1558 unsigned Line = G->getLineNumber();
Devang Pateldd9db662009-01-30 18:20:31 +00001559 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1560 FileID = Unit->getID();
Devang Patel2303df92009-02-10 06:04:08 +00001561 assert (FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001562 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1563 AddUInt(Die, DW_AT_decl_line, 0, Line);
1564 }
1565
Devang Patel99ec3532009-01-16 19:28:14 +00001566 void AddSourceLine(DIE *Die, const DIType *Ty) {
Devang Patel8526cc02009-01-05 22:35:52 +00001567 unsigned FileID = 0;
Devang Patel99ec3532009-01-16 19:28:14 +00001568 unsigned Line = Ty->getLineNumber();
Devang Pateldd9db662009-01-30 18:20:31 +00001569 DICompileUnit CU = Ty->getCompileUnit();
1570 if (CU.isNull())
1571 return;
1572 CompileUnit *Unit = FindCompileUnit(CU);
1573 FileID = Unit->getID();
Devang Patel2303df92009-02-10 06:04:08 +00001574 assert (FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001575 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1576 AddUInt(Die, DW_AT_decl_line, 0, Line);
1577 }
1578
Jim Laskey65195462006-10-30 13:35:07 +00001579 /// AddAddress - Add an address attribute to a die based on the location
1580 /// provided.
1581 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001582 const MachineLocation &Location) {
Dan Gohmand735b802008-10-03 15:45:36 +00001583 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001584 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001585
Dan Gohmand735b802008-10-03 15:45:36 +00001586 if (Location.isReg()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001587 if (Reg < 32) {
1588 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1589 } else {
1590 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1591 AddUInt(Block, 0, DW_FORM_udata, Reg);
1592 }
1593 } else {
1594 if (Reg < 32) {
1595 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1596 } else {
1597 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1598 AddUInt(Block, 0, DW_FORM_udata, Reg);
1599 }
1600 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1601 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001602
Jim Laskeyef42a012006-11-02 20:12:39 +00001603 AddBlock(Die, Attribute, 0, Block);
1604 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001605
Jim Laskeyef42a012006-11-02 20:12:39 +00001606 /// AddType - Add a new type attribute to the specified entity.
Devang Patel48d190f2009-01-05 21:47:57 +00001607 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel80303aa2009-01-23 19:13:31 +00001608 if (Ty.isNull())
Devang Patel48d190f2009-01-05 21:47:57 +00001609 return;
Devang Patel48d190f2009-01-05 21:47:57 +00001610
1611 // Check for pre-existence.
1612 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1613 // If it exists then use the existing value.
1614 if (Slot) {
1615 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1616 return;
1617 }
1618
1619 // Set up proxy.
1620 Slot = NewDIEntry();
1621
1622 // Construct type.
1623 DIE Buffer(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001624 if (Ty.isBasicType(Ty.getTag()))
1625 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1626 else if (Ty.isDerivedType(Ty.getTag()))
1627 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1628 else {
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001629 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelf193ff02009-01-15 19:26:23 +00001630 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1631 }
1632
Devang Patel7009d242009-01-27 23:22:55 +00001633 // Add debug information entry to entity and appropriate context.
1634 DIE *Die = NULL;
1635 DIDescriptor Context = Ty.getContext();
1636 if (!Context.isNull())
1637 Die = DW_Unit->getDieMapSlotFor(Context.getGV());
1638
1639 if (Die) {
1640 DIE *Child = new DIE(Buffer);
1641 Die->AddChild(Child);
1642 Buffer.Detach();
1643 SetDIEntry(Slot, Child);
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001644 } else {
Devang Patel7009d242009-01-27 23:22:55 +00001645 Die = DW_Unit->AddDie(Buffer);
1646 SetDIEntry(Slot, Die);
1647 }
1648
Devang Patel48d190f2009-01-05 21:47:57 +00001649 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1650 }
1651
Devang Patele5202732009-01-05 19:07:53 +00001652 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1653 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001654 DIBasicType BTy) {
Devang Patel08f053f2009-01-05 17:57:47 +00001655
1656 // Get core information.
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001657 std::string Name;
1658 BTy.getName(Name);
Devang Patel08f053f2009-01-05 17:57:47 +00001659 Buffer.setTag(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001660 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patel08f053f2009-01-05 17:57:47 +00001661 // Add name if not anonymous or intermediate type.
1662 if (!Name.empty())
1663 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001664 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel08f053f2009-01-05 17:57:47 +00001665 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1666 }
1667
Devang Patele5202732009-01-05 19:07:53 +00001668 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1669 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001670 DIDerivedType DTy) {
Devang Patel08f053f2009-01-05 17:57:47 +00001671
1672 // Get core information.
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001673 std::string Name;
1674 DTy.getName(Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001675 uint64_t Size = DTy.getSizeInBits() >> 3;
1676 unsigned Tag = DTy.getTag();
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001677
Devang Patel08f053f2009-01-05 17:57:47 +00001678 // FIXME - Workaround for templates.
1679 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1680
1681 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001682
Devang Patel08f053f2009-01-05 17:57:47 +00001683 // Map to main type, void will not have a type.
Devang Patelf193ff02009-01-15 19:26:23 +00001684 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00001685 AddType(DW_Unit, &Buffer, FromTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001686
1687 // Add name if not anonymous or intermediate type.
Evan Chenge3d42322009-02-25 07:04:34 +00001688 if (!Name.empty())
1689 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel08f053f2009-01-05 17:57:47 +00001690
1691 // Add size if non-zero (derived types might be zero-sized.)
1692 if (Size)
1693 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1694
1695 // Add source line info if available and TyDesc is not a forward
1696 // declaration.
Devang Patelad165be2009-01-27 00:45:04 +00001697 if (!DTy.isForwardDecl())
1698 AddSourceLine(&Buffer, &DTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001699 }
1700
Devang Patelf4215332009-01-05 19:55:51 +00001701 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1702 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001703 DICompositeType CTy) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001704 // Get core information.
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001705 std::string Name;
1706 CTy.getName(Name);
1707
Devang Patelf193ff02009-01-15 19:26:23 +00001708 uint64_t Size = CTy.getSizeInBits() >> 3;
1709 unsigned Tag = CTy.getTag();
Devang Patel49f38cb2009-01-23 01:19:09 +00001710 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001711
Devang Patelf4215332009-01-05 19:55:51 +00001712 switch (Tag) {
1713 case DW_TAG_vector_type:
1714 case DW_TAG_array_type:
Devang Patelf193ff02009-01-15 19:26:23 +00001715 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001716 break;
Devang Pateleab4a2e2009-01-20 18:35:14 +00001717 case DW_TAG_enumeration_type:
1718 {
1719 DIArray Elements = CTy.getTypeArray();
1720 // Add enumerators to enumeration type.
1721 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1722 DIE *ElemDie = NULL;
1723 DIEnumerator Enum(Elements.getElement(i).getGV());
1724 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
1725 Buffer.AddChild(ElemDie);
1726 }
1727 }
1728 break;
Devang Patelf4215332009-01-05 19:55:51 +00001729 case DW_TAG_subroutine_type:
1730 {
1731 // Add prototype flag.
1732 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patelf193ff02009-01-15 19:26:23 +00001733 DIArray Elements = CTy.getTypeArray();
Devang Patelf4215332009-01-05 19:55:51 +00001734 // Add return type.
Devang Patel48d190f2009-01-05 21:47:57 +00001735 DIDescriptor RTy = Elements.getElement(0);
Devang Patelf193ff02009-01-15 19:26:23 +00001736 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel48d190f2009-01-05 21:47:57 +00001737
Devang Patelf4215332009-01-05 19:55:51 +00001738 // Add arguments.
1739 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1740 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00001741 DIDescriptor Ty = Elements.getElement(i);
Devang Patel7ab24502009-01-17 06:57:25 +00001742 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patelf4215332009-01-05 19:55:51 +00001743 Buffer.AddChild(Arg);
1744 }
1745 }
1746 break;
1747 case DW_TAG_structure_type:
1748 case DW_TAG_union_type:
1749 {
1750 // Add elements to structure type.
Devang Patelf193ff02009-01-15 19:26:23 +00001751 DIArray Elements = CTy.getTypeArray();
Devang Patel153745c2009-01-16 00:50:53 +00001752
1753 // A forward struct declared type may not have elements available.
1754 if (Elements.isNull())
1755 break;
1756
Devang Patelf4215332009-01-05 19:55:51 +00001757 // Add elements to structure type.
1758 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1759 DIDescriptor Element = Elements.getElement(i);
Devang Patel5aac3d32009-01-17 08:01:33 +00001760 DIE *ElemDie = NULL;
Devang Patelf193ff02009-01-15 19:26:23 +00001761 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel2d1768c2009-01-17 08:05:14 +00001762 ElemDie = CreateSubprogramDIE(DW_Unit,
1763 DISubprogram(Element.getGV()));
Devang Patel5aac3d32009-01-17 08:01:33 +00001764 else if (Element.getTag() == dwarf::DW_TAG_variable) // ???
1765 ElemDie = CreateGlobalVariableDIE(DW_Unit,
1766 DIGlobalVariable(Element.getGV()));
Devang Patel2be58932009-01-20 21:02:02 +00001767 else
1768 ElemDie = CreateMemberDIE(DW_Unit,
1769 DIDerivedType(Element.getGV()));
Devang Patel2d1768c2009-01-17 08:05:14 +00001770 Buffer.AddChild(ElemDie);
Devang Patelf4215332009-01-05 19:55:51 +00001771 }
Devang Patel13319ce2009-02-17 22:43:44 +00001772 unsigned RLang = CTy.getRunTimeLang();
1773 if (RLang)
1774 AddUInt(&Buffer, DW_AT_APPLE_runtime_class, DW_FORM_data1, RLang);
Devang Patelf4215332009-01-05 19:55:51 +00001775 }
1776 break;
1777 default:
1778 break;
1779 }
1780
1781 // Add name if not anonymous or intermediate type.
Evan Chenge3d42322009-02-25 07:04:34 +00001782 if (!Name.empty())
1783 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf4215332009-01-05 19:55:51 +00001784
Devang Patelad165be2009-01-27 00:45:04 +00001785 if (Tag == DW_TAG_enumeration_type || Tag == DW_TAG_structure_type
1786 || Tag == DW_TAG_union_type) {
1787 // Add size if non-zero (derived types might be zero-sized.)
1788 if (Size)
1789 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1790 else {
1791 // Add zero size if it is not a forward declaration.
1792 if (CTy.isForwardDecl())
1793 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1794 else
1795 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1796 }
1797
1798 // Add source line info if available.
1799 if (!CTy.isForwardDecl())
1800 AddSourceLine(&Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001801 }
Devang Patelf4215332009-01-05 19:55:51 +00001802 }
1803
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001804 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1805 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelf193ff02009-01-15 19:26:23 +00001806 int64_t L = SR.getLo();
1807 int64_t H = SR.getHi();
Devang Patel68afdc32009-01-05 18:33:01 +00001808 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1809 if (L != H) {
1810 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1811 if (L)
Devang Patel2d1768c2009-01-17 08:05:14 +00001812 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1813 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel68afdc32009-01-05 18:33:01 +00001814 }
1815 Buffer.AddChild(DW_Subrange);
1816 }
1817
1818 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1819 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1820 DICompositeType *CTy) {
1821 Buffer.setTag(DW_TAG_array_type);
1822 if (CTy->getTag() == DW_TAG_vector_type)
1823 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1824
Devang Patelf9235742009-01-28 21:08:20 +00001825 // Emit derived type.
1826 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel68afdc32009-01-05 18:33:01 +00001827 DIArray Elements = CTy->getTypeArray();
Devang Patel68afdc32009-01-05 18:33:01 +00001828
1829 // Construct an anonymous type for index type.
1830 DIE IdxBuffer(DW_TAG_base_type);
1831 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1832 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1833 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1834
1835 // Add subranges to array type.
1836 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patelf4215332009-01-05 19:55:51 +00001837 DIDescriptor Element = Elements.getElement(i);
Devang Patelf193ff02009-01-15 19:26:23 +00001838 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1839 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel68afdc32009-01-05 18:33:01 +00001840 }
1841 }
1842
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001843 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Pateleab4a2e2009-01-20 18:35:14 +00001844 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patelc69bf2c2009-01-05 18:38:38 +00001845
1846 DIE *Enumerator = new DIE(DW_TAG_enumerator);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001847 std::string Name;
1848 ETy->getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001849 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
Devang Patelc69bf2c2009-01-05 18:38:38 +00001850 int64_t Value = ETy->getEnumValue();
1851 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Pateleab4a2e2009-01-20 18:35:14 +00001852 return Enumerator;
Devang Patelc69bf2c2009-01-05 18:38:38 +00001853 }
Devang Patel68afdc32009-01-05 18:33:01 +00001854
Devang Patel5aac3d32009-01-17 08:01:33 +00001855 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001856 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patel5aac3d32009-01-17 08:01:33 +00001857 {
1858 DIE *GVDie = new DIE(DW_TAG_variable);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001859 std::string Name;
1860 GV.getDisplayName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001861 AddString(GVDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001862 std::string LinkageName;
1863 GV.getLinkageName(LinkageName);
Devang Patel86ae1422009-01-05 18:59:44 +00001864 if (!LinkageName.empty())
Devang Patel5aac3d32009-01-17 08:01:33 +00001865 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1866 AddType(DW_Unit, GVDie, GV.getType());
1867 if (!GV.isLocalToUnit())
1868 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
1869 AddSourceLine(GVDie, &GV);
1870 return GVDie;
Devang Patel86ae1422009-01-05 18:59:44 +00001871 }
1872
Devang Patel2be58932009-01-20 21:02:02 +00001873 /// CreateMemberDIE - Create new member DIE.
1874 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
1875 DIE *MemberDie = new DIE(DT.getTag());
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001876 std::string Name;
1877 DT.getName(Name);
Devang Patel2be58932009-01-20 21:02:02 +00001878 if (!Name.empty())
1879 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1880
1881 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1882
1883 AddSourceLine(MemberDie, &DT);
1884
Devang Patel36375ee2009-02-17 21:23:59 +00001885 uint64_t Size = DT.getSizeInBits();
1886 uint64_t FieldSize = DT.getOriginalTypeSize();
1887
1888 if (Size != FieldSize) {
1889 // Handle bitfield.
1890 AddUInt(MemberDie, DW_AT_byte_size, 0, DT.getOriginalTypeSize() >> 3);
1891 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1892
1893 uint64_t Offset = DT.getOffsetInBits();
1894 uint64_t FieldOffset = Offset;
1895 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1896 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1897 FieldOffset = (HiMark - FieldSize);
1898 Offset -= FieldOffset;
1899 // Maybe we need to work from the other end.
1900 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1901 AddUInt(MemberDie, DW_AT_bit_offset, 0, Offset);
1902 }
Devang Patel2be58932009-01-20 21:02:02 +00001903 DIEBlock *Block = new DIEBlock();
1904 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1905 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1906 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1907
Devang Patel47661592009-01-21 00:08:04 +00001908 if (DT.isProtected())
1909 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1910 else if (DT.isPrivate())
1911 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1912
Devang Patel2be58932009-01-20 21:02:02 +00001913 return MemberDie;
1914 }
1915
Devang Patel5aac3d32009-01-17 08:01:33 +00001916 /// CreateSubprogramDIE - Create new DIE using SP.
1917 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Devang Patel2d1768c2009-01-17 08:05:14 +00001918 const DISubprogram &SP,
1919 bool IsConstructor = false) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001920 DIE *SPDie = new DIE(DW_TAG_subprogram);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001921 std::string Name;
1922 SP.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001923 AddString(SPDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001924 std::string LinkageName;
1925 SP.getLinkageName(LinkageName);
Devang Patel86ae1422009-01-05 18:59:44 +00001926 if (!LinkageName.empty())
Devang Patel5aac3d32009-01-17 08:01:33 +00001927 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Devang Patel2d1768c2009-01-17 08:05:14 +00001928 LinkageName);
Devang Patel5aac3d32009-01-17 08:01:33 +00001929 AddSourceLine(SPDie, &SP);
Devang Patel86ae1422009-01-05 18:59:44 +00001930
Devang Patel5aac3d32009-01-17 08:01:33 +00001931 DICompositeType SPTy = SP.getType();
1932 DIArray Args = SPTy.getTypeArray();
1933
Devang Patel86ae1422009-01-05 18:59:44 +00001934 // Add Return Type.
Devang Patel9ac08d62009-02-27 18:05:21 +00001935 if (!IsConstructor) {
1936 if (Args.isNull())
1937 AddType(DW_Unit, SPDie, SPTy);
1938 else
1939 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
1940 }
Devang Pateld234e592009-01-30 01:21:46 +00001941
Devang Pateld5863dd2009-02-02 17:51:41 +00001942 if (!SP.isDefinition()) {
1943 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
1944 // Add arguments.
1945 // Do not add arguments for subprogram definition. They will be
1946 // handled through RecordVariable.
1947 if (!Args.isNull())
1948 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1949 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1950 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
1951 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
1952 SPDie->AddChild(Arg);
1953 }
1954 }
Devang Pateld234e592009-01-30 01:21:46 +00001955
Devang Patel002ec142009-02-24 00:52:19 +00001956 unsigned Lang = SP.getCompileUnit().getLanguage();
1957 if (Lang == DW_LANG_C99 || Lang == DW_LANG_C89
1958 || Lang == DW_LANG_ObjC)
1959 AddUInt(SPDie, DW_AT_prototyped, DW_FORM_flag, 1);
1960
Devang Patelf193ff02009-01-15 19:26:23 +00001961 if (!SP.isLocalToUnit())
Devang Pateld234e592009-01-30 01:21:46 +00001962 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Devang Patel5aac3d32009-01-17 08:01:33 +00001963 return SPDie;
Devang Patel86ae1422009-01-05 18:59:44 +00001964 }
1965
Devang Patel5aac3d32009-01-17 08:01:33 +00001966 /// FindCompileUnit - Get the compile unit for the given descriptor.
1967 ///
Devang Patel8526cc02009-01-05 22:35:52 +00001968 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
Evan Chenge3d42322009-02-25 07:04:34 +00001969 CompileUnit *DW_Unit = CompileUnitMap[Unit.getGV()];
Devang Patel8526cc02009-01-05 22:35:52 +00001970 assert(DW_Unit && "Missing compile unit.");
1971 return DW_Unit;
1972 }
1973
Devang Patelbbdc8202009-01-13 23:54:55 +00001974 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001975 ///
1976 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1977 // Get the descriptor.
Devang Patel99ec3532009-01-16 19:28:14 +00001978 const DIVariable &VD = DV->getVariable();
Devang Patel7a6e5a32009-01-08 02:33:41 +00001979
1980 // Translate tag to proper Dwarf tag. The result variable is dropped for
1981 // now.
1982 unsigned Tag;
Devang Patel99ec3532009-01-16 19:28:14 +00001983 switch (VD.getTag()) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001984 case DW_TAG_return_variable: return NULL;
1985 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1986 case DW_TAG_auto_variable: // fall thru
1987 default: Tag = DW_TAG_variable; break;
1988 }
1989
1990 // Define variable debug information entry.
1991 DIE *VariableDie = new DIE(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001992 std::string Name;
1993 VD.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001994 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Devang Patel7a6e5a32009-01-08 02:33:41 +00001995
1996 // Add source line info if available.
Devang Patel99ec3532009-01-16 19:28:14 +00001997 AddSourceLine(VariableDie, &VD);
Devang Patel7a6e5a32009-01-08 02:33:41 +00001998
1999 // Add variable type.
Devang Patel99ec3532009-01-16 19:28:14 +00002000 AddType(Unit, VariableDie, VD.getType());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002001
2002 // Add variable address.
2003 MachineLocation Location;
2004 Location.set(RI->getFrameRegister(*MF),
2005 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
2006 AddAddress(VariableDie, DW_AT_location, Location);
2007
2008 return VariableDie;
2009 }
2010
Devang Patel7a6e5a32009-01-08 02:33:41 +00002011 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2012 ///
2013 DbgScope *getOrCreateScope(GlobalVariable *V) {
2014 DbgScope *&Slot = DbgScopeMap[V];
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002015 if (Slot) return Slot;
2016
2017 // FIXME - breaks down when the context is an inlined function.
2018 DIDescriptor ParentDesc;
2019 DIDescriptor Desc(V);
2020
2021 if (Desc.getTag() == dwarf::DW_TAG_lexical_block) {
2022 DIBlock Block(V);
2023 ParentDesc = Block.getContext();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002024 }
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002025
2026 DbgScope *Parent = ParentDesc.isNull() ?
2027 NULL : getOrCreateScope(ParentDesc.getGV());
2028 Slot = new DbgScope(Parent, Desc);
2029
2030 if (Parent) {
2031 Parent->AddScope(Slot);
2032 } else if (RootDbgScope) {
2033 // FIXME - Add inlined function scopes to the root so we can delete them
2034 // later. Long term, handle inlined functions properly.
2035 RootDbgScope->AddScope(Slot);
2036 } else {
2037 // First function is top level function.
2038 RootDbgScope = Slot;
2039 }
2040
Devang Patel7a6e5a32009-01-08 02:33:41 +00002041 return Slot;
2042 }
2043
2044 /// ConstructDbgScope - Construct the components of a scope.
2045 ///
2046 void ConstructDbgScope(DbgScope *ParentScope,
2047 unsigned ParentStartID, unsigned ParentEndID,
2048 DIE *ParentDie, CompileUnit *Unit) {
2049 // Add variables to scope.
Devang Patel9795da52009-01-10 02:42:49 +00002050 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002051 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2052 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2053 if (VariableDie) ParentDie->AddChild(VariableDie);
2054 }
2055
2056 // Add nested scopes.
Devang Patel9795da52009-01-10 02:42:49 +00002057 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002058 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2059 // Define the Scope debug information entry.
2060 DbgScope *Scope = Scopes[j];
2061 // FIXME - Ignore inlined functions for the time being.
2062 if (!Scope->getParent()) continue;
2063
Devang Patele9bfb0f2009-01-12 18:41:00 +00002064 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2065 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002066
2067 // Ignore empty scopes.
2068 if (StartID == EndID && StartID != 0) continue;
2069 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
2070
2071 if (StartID == ParentStartID && EndID == ParentEndID) {
2072 // Just add stuff to the parent scope.
2073 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2074 } else {
2075 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2076
2077 // Add the scope bounds.
2078 if (StartID) {
2079 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2080 DWLabel("label", StartID));
2081 } else {
2082 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2083 DWLabel("func_begin", SubprogramCount));
2084 }
2085 if (EndID) {
2086 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2087 DWLabel("label", EndID));
2088 } else {
2089 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2090 DWLabel("func_end", SubprogramCount));
2091 }
2092
2093 // Add the scope contents.
2094 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2095 ParentDie->AddChild(ScopeDie);
2096 }
2097 }
2098 }
2099
2100 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2101 ///
2102 void ConstructRootDbgScope(DbgScope *RootScope) {
2103 // Exit if there is no root scope.
2104 if (!RootScope) return;
Devang Patel0e5200f2009-01-15 18:25:17 +00002105 DIDescriptor Desc = RootScope->getDesc();
2106 if (Desc.isNull())
2107 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002108
2109 // Get the subprogram debug information entry.
Devang Patel0e5200f2009-01-15 18:25:17 +00002110 DISubprogram SPD(Desc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002111
2112 // Get the compile unit context.
Devang Pateldd9db662009-01-30 18:20:31 +00002113 CompileUnit *Unit = MainCU;
2114 if (!Unit)
2115 Unit = FindCompileUnit(SPD.getCompileUnit());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002116
2117 // Get the subprogram die.
Devang Patelf6bac3e2009-01-12 22:58:14 +00002118 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002119 assert(SPDie && "Missing subprogram descriptor");
2120
2121 // Add the function bounds.
2122 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2123 DWLabel("func_begin", SubprogramCount));
2124 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2125 DWLabel("func_end", SubprogramCount));
2126 MachineLocation Location(RI->getFrameRegister(*MF));
2127 AddAddress(SPDie, DW_AT_frame_base, Location);
2128
2129 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2130 }
2131
2132 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2133 ///
2134 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Chenge3d42322009-02-25 07:04:34 +00002135 const char *FnName = MF->getFunction()->getNameStart();
2136 if (MainCU) {
2137 std::map<std::string, DIE*> &Globals = MainCU->getGlobals();
2138 std::map<std::string, DIE*>::iterator GI = Globals.find(FnName);
2139 if (GI != Globals.end()) {
2140 DIE *SPDie = GI->second;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002141
2142 // Add the function bounds.
2143 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2144 DWLabel("func_begin", SubprogramCount));
2145 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2146 DWLabel("func_end", SubprogramCount));
2147
2148 MachineLocation Location(RI->getFrameRegister(*MF));
2149 AddAddress(SPDie, DW_AT_frame_base, Location);
2150 return;
2151 }
Evan Chenge3d42322009-02-25 07:04:34 +00002152 } else {
2153 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2154 CompileUnit *Unit = CompileUnits[i];
2155 std::map<std::string, DIE*> &Globals = Unit->getGlobals();
2156 std::map<std::string, DIE*>::iterator GI = Globals.find(FnName);
2157 if (GI != Globals.end()) {
2158 DIE *SPDie = GI->second;
2159
2160 // Add the function bounds.
2161 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2162 DWLabel("func_begin", SubprogramCount));
2163 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2164 DWLabel("func_end", SubprogramCount));
2165
2166 MachineLocation Location(RI->getFrameRegister(*MF));
2167 AddAddress(SPDie, DW_AT_frame_base, Location);
2168 return;
2169 }
2170 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00002171 }
Evan Chenge3d42322009-02-25 07:04:34 +00002172
Devang Patel7a6e5a32009-01-08 02:33:41 +00002173#if 0
2174 // FIXME: This is causing an abort because C++ mangled names are compared
2175 // with their unmangled counterparts. See PR2885. Don't do this assert.
2176 assert(0 && "Couldn't find DIE for machine function!");
2177#endif
Evan Chenge3d42322009-02-25 07:04:34 +00002178 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002179 }
2180
Jim Laskeyef42a012006-11-02 20:12:39 +00002181 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2182 /// tools to recognize the object file contains Dwarf information.
2183 void EmitInitial() {
2184 // Check to see if we already emitted intial headers.
2185 if (didInitial) return;
2186 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002187
Jim Laskeyef42a012006-11-02 20:12:39 +00002188 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002189 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002190 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002191 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002192 }
2193 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2194 EmitLabel("section_info", 0);
2195 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2196 EmitLabel("section_abbrev", 0);
2197 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2198 EmitLabel("section_aranges", 0);
Scott Michel210de722009-01-26 22:32:51 +00002199 if (TAI->doesSupportMacInfoSection()) {
2200 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2201 EmitLabel("section_macinfo", 0);
2202 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002203 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2204 EmitLabel("section_line", 0);
2205 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2206 EmitLabel("section_loc", 0);
2207 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2208 EmitLabel("section_pubnames", 0);
2209 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2210 EmitLabel("section_str", 0);
2211 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2212 EmitLabel("section_ranges", 0);
2213
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002214 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002215 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002216 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002217 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002218 }
2219
Jim Laskey65195462006-10-30 13:35:07 +00002220 /// EmitDIE - Recusively Emits a debug information entry.
2221 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002222 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002223 // Get the abbreviation for this DIE.
2224 unsigned AbbrevNumber = Die->getAbbrevNumber();
2225 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002226
Jim Laskeybacd3042007-02-21 22:48:45 +00002227 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002228
2229 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002230 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002231
2232 if (VerboseAsm)
2233 Asm->EOL(std::string("Abbrev [" +
2234 utostr(AbbrevNumber) +
2235 "] 0x" + utohexstr(Die->getOffset()) +
2236 ":0x" + utohexstr(Die->getSize()) + " " +
2237 TagString(Abbrev->getTag())));
2238 else
2239 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002240
Owen Anderson873e1b52008-06-24 21:44:59 +00002241 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2242 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002243
Jim Laskeyef42a012006-11-02 20:12:39 +00002244 // Emit the DIE attribute values.
2245 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2246 unsigned Attr = AbbrevData[i].getAttribute();
2247 unsigned Form = AbbrevData[i].getForm();
2248 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002249
Jim Laskeyef42a012006-11-02 20:12:39 +00002250 switch (Attr) {
2251 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002252 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002253 break;
2254 }
2255 default: {
2256 // Emit an attribute using the defined form.
2257 Values[i]->EmitValue(*this, Form);
2258 break;
2259 }
2260 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002261
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002262 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002263 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002264
Jim Laskeyef42a012006-11-02 20:12:39 +00002265 // Emit the DIE children if any.
2266 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2267 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002268
Jim Laskeyef42a012006-11-02 20:12:39 +00002269 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2270 EmitDIE(Children[j]);
2271 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002272
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002273 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002274 }
2275 }
2276
Jim Laskey65195462006-10-30 13:35:07 +00002277 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2278 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002279 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2280 // Get the children.
2281 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002282
Jim Laskeyef42a012006-11-02 20:12:39 +00002283 // If not last sibling and has children then add sibling offset attribute.
2284 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2285
2286 // Record the abbreviation.
2287 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002288
Jim Laskeyef42a012006-11-02 20:12:39 +00002289 // Get the abbreviation for this DIE.
2290 unsigned AbbrevNumber = Die->getAbbrevNumber();
2291 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2292
2293 // Set DIE offset
2294 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002295
Jim Laskeyef42a012006-11-02 20:12:39 +00002296 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002297 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2298
Owen Anderson873e1b52008-06-24 21:44:59 +00002299 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2300 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002301
2302 // Size the DIE attribute values.
2303 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2304 // Size attribute value.
2305 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2306 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002307
Jim Laskeyef42a012006-11-02 20:12:39 +00002308 // Size the DIE children if any.
2309 if (!Children.empty()) {
2310 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2311 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002312
Jim Laskeyef42a012006-11-02 20:12:39 +00002313 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2314 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2315 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002316
Jim Laskeyef42a012006-11-02 20:12:39 +00002317 // End of children marker.
2318 Offset += sizeof(int8_t);
2319 }
2320
2321 Die->setSize(Offset - Die->getOffset());
2322 return Offset;
2323 }
Jim Laskey65195462006-10-30 13:35:07 +00002324
2325 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2326 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002327 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002328 // Process base compile unit.
Devang Pateldd9db662009-01-30 18:20:31 +00002329 if (MainCU) {
2330 // Compute size of compile unit header
2331 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2332 sizeof(int16_t) + // DWARF version number
2333 sizeof(int32_t) + // Offset Into Abbrev. Section
2334 sizeof(int8_t); // Pointer Size (in bytes)
2335 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
2336 return;
2337 }
Evan Chenge3d42322009-02-25 07:04:34 +00002338 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2339 CompileUnit *Unit = CompileUnits[i];
Devang Patel72b66352009-01-12 23:05:55 +00002340 // Compute size of compile unit header
2341 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2342 sizeof(int16_t) + // DWARF version number
2343 sizeof(int32_t) + // Offset Into Abbrev. Section
2344 sizeof(int8_t); // Pointer Size (in bytes)
2345 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2346 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002347 }
2348
Evan Chenge3d42322009-02-25 07:04:34 +00002349 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Jim Laskey65195462006-10-30 13:35:07 +00002350 ///
Evan Chenge3d42322009-02-25 07:04:34 +00002351 void EmitDebugInfoPerCU(CompileUnit *Unit) {
2352 DIE *Die = Unit->getDie();
2353 // Emit the compile units header.
2354 EmitLabel("info_begin", Unit->getID());
2355 // Emit size of content not including length itself
2356 unsigned ContentSize = Die->getSize() +
2357 sizeof(int16_t) + // DWARF version number
2358 sizeof(int32_t) + // Offset Into Abbrev. Section
2359 sizeof(int8_t) + // Pointer Size (in bytes)
2360 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2361
2362 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2363 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2364 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2365 Asm->EOL("Offset Into Abbrev. Section");
2366 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2367
2368 EmitDIE(Die);
2369 // FIXME - extra padding for gdb bug.
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 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2374 EmitLabel("info_end", Unit->getID());
2375
2376 Asm->EOL();
2377 }
2378
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002379 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002380 // Start debug info section.
2381 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002382
Evan Chenge3d42322009-02-25 07:04:34 +00002383 if (MainCU) {
2384 EmitDebugInfoPerCU(MainCU);
2385 return;
Devang Patel72b66352009-01-12 23:05:55 +00002386 }
Evan Chenge3d42322009-02-25 07:04:34 +00002387
2388 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2389 EmitDebugInfoPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002390 }
2391
Jim Laskey65195462006-10-30 13:35:07 +00002392 /// EmitAbbreviations - Emit the abbreviation section.
2393 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002394 void EmitAbbreviations() const {
2395 // Check to see if it is worth the effort.
2396 if (!Abbreviations.empty()) {
2397 // Start the debug abbrev section.
2398 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002399
Jim Laskeyef42a012006-11-02 20:12:39 +00002400 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002401
Jim Laskeyef42a012006-11-02 20:12:39 +00002402 // For each abbrevation.
2403 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2404 // Get abbreviation data
2405 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002406
Jim Laskeyef42a012006-11-02 20:12:39 +00002407 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002408 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2409 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002410
Jim Laskeyef42a012006-11-02 20:12:39 +00002411 // Emit the abbreviations data.
2412 Abbrev->Emit(*this);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002413
Jim Laskeybacd3042007-02-21 22:48:45 +00002414 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002415 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002416
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002417 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00002418 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002419
Jim Laskeyef42a012006-11-02 20:12:39 +00002420 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002421
Jim Laskeybacd3042007-02-21 22:48:45 +00002422 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002423 }
2424 }
2425
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002426 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2427 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002428 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002429 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2430 // Define last address of section.
2431 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2432 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2433 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2434 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2435
2436 // Mark end of matrix.
2437 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2438 Asm->EmitULEB128Bytes(1); Asm->EOL();
2439 Asm->EmitInt8(1); Asm->EOL();
2440 }
2441
Jim Laskey65195462006-10-30 13:35:07 +00002442 /// EmitDebugLines - Emit source line information.
2443 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002444 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002445 // If the target is using .loc/.file, the assembler will be emitting the
2446 // .debug_line table automatically.
2447 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00002448 return;
2449
Jim Laskeyef42a012006-11-02 20:12:39 +00002450 // Minimum line delta, thus ranging from -10..(255-10).
2451 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2452 // Maximum line delta, thus ranging from -10..(255-10).
2453 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002454
Jim Laskeyef42a012006-11-02 20:12:39 +00002455 // Start the dwarf line section.
2456 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002457
Jim Laskeyef42a012006-11-02 20:12:39 +00002458 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002459
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002460 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002461 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002462 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002463
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002464 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002465
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002466 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002467 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002468 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002469
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002470 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002471
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002472 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002473
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002474 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002475
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002476 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002477
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002478 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002479
Jim Laskeyef42a012006-11-02 20:12:39 +00002480 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002481 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2482 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2483 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2484 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2485 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2486 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2487 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2488 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2489 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00002490
Jim Laskeyef42a012006-11-02 20:12:39 +00002491 // Emit directories.
Evan Chenge3d42322009-02-25 07:04:34 +00002492 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2493 Asm->EmitString(getSourceDirectoryName(DI));
2494 Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00002495 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002496 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002497
Jim Laskeyef42a012006-11-02 20:12:39 +00002498 // Emit files.
Evan Chenge3d42322009-02-25 07:04:34 +00002499 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2500 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002501 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Evan Chenge3d42322009-02-25 07:04:34 +00002502 Asm->EmitString(getSourceFileName(Id.second));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002503 Asm->EOL("Source");
Evan Chenge3d42322009-02-25 07:04:34 +00002504 Asm->EmitULEB128Bytes(Id.first);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002505 Asm->EOL("Directory #");
2506 Asm->EmitULEB128Bytes(0);
2507 Asm->EOL("Mod date");
2508 Asm->EmitULEB128Bytes(0);
2509 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002510 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002511 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002512
Jim Laskeyef42a012006-11-02 20:12:39 +00002513 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002514
Jim Laskeyef42a012006-11-02 20:12:39 +00002515 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002516 unsigned SecSrcLinesSize = SectionSourceLines.size();
2517
2518 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002519 // Isolate current sections line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00002520 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00002521
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002522 if (VerboseAsm) {
2523 const Section* S = SectionMap[j + 1];
Evan Chenge3d42322009-02-25 07:04:34 +00002524 O << '\t' << TAI->getCommentString() << " Section"
2525 << S->getName() << '\n';
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002526 } else
Evan Cheng6547e402008-07-01 23:18:29 +00002527 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002528
2529 // Dwarf assumes we start with first line of first source file.
2530 unsigned Source = 1;
2531 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002532
Jim Laskeyef42a012006-11-02 20:12:39 +00002533 // Construct rows of the address, source, line, column matrix.
2534 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patelf3ee5142009-01-12 22:54:42 +00002535 const SrcLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002536 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00002537 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002538
Evan Chenge3d42322009-02-25 07:04:34 +00002539 if (!VerboseAsm)
Evan Cheng6547e402008-07-01 23:18:29 +00002540 Asm->EOL();
Evan Chenge3d42322009-02-25 07:04:34 +00002541 else {
2542 std::pair<unsigned, unsigned> SourceID =
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002543 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Evan Chenge3d42322009-02-25 07:04:34 +00002544 O << '\t' << TAI->getCommentString() << ' '
2545 << getSourceDirectoryName(SourceID.first) << ' '
2546 << getSourceFileName(SourceID.second)
2547 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2548 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002549
2550 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002551 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00002552 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002553 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00002554 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002555
Jim Laskeyef42a012006-11-02 20:12:39 +00002556 // If change of source, then switch to the new source.
2557 if (Source != LineInfo.getSourceID()) {
2558 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002559 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2560 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00002561 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002562
Jim Laskeyef42a012006-11-02 20:12:39 +00002563 // If change of line.
2564 if (Line != LineInfo.getLine()) {
2565 // Determine offset.
2566 int Offset = LineInfo.getLine() - Line;
2567 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002568
Jim Laskeyef42a012006-11-02 20:12:39 +00002569 // Update line.
2570 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002571
Jim Laskeyef42a012006-11-02 20:12:39 +00002572 // If delta is small enough and in range...
2573 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2574 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002575 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00002576 } else {
2577 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002578 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2579 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2580 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002581 }
2582 } else {
2583 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002584 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002585 }
2586 }
2587
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002588 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002589 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002590
2591 if (SecSrcLinesSize == 0)
2592 // Because we're emitting a debug_line section, we still need a line
2593 // table. The linker and friends expect it to exist. If there's nothing to
2594 // put into it, emit an empty table.
2595 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002596
Jim Laskeyef42a012006-11-02 20:12:39 +00002597 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002598
Jim Laskeybacd3042007-02-21 22:48:45 +00002599 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002600 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002601
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002602 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00002603 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002604 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002605 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00002606 return;
2607
2608 int stackGrowth =
2609 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2610 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002611 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00002612
2613 // Start the dwarf frame section.
2614 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2615
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002616 EmitLabel("debug_frame_common", 0);
2617 EmitDifference("debug_frame_common_end", 0,
2618 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002619 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002620
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002621 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002622 Asm->EmitInt32((int)DW_CIE_ID);
2623 Asm->EOL("CIE Identifier Tag");
2624 Asm->EmitInt8(DW_CIE_VERSION);
2625 Asm->EOL("CIE Version");
2626 Asm->EmitString("");
2627 Asm->EOL("CIE Augmentation");
2628 Asm->EmitULEB128Bytes(1);
2629 Asm->EOL("CIE Code Alignment Factor");
2630 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002631 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002632 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002633 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002634
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002635 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002636 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002637
Dale Johannesenb97aec62007-11-13 19:13:01 +00002638 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002639
Evan Cheng05548eb2008-02-29 19:36:59 +00002640 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002641 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002642
Jim Laskeybacd3042007-02-21 22:48:45 +00002643 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002644 }
2645
Jim Laskey65195462006-10-30 13:35:07 +00002646 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2647 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002648 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002649 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00002650 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002651
Jim Laskeyef42a012006-11-02 20:12:39 +00002652 // Start the dwarf frame section.
2653 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002654
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002655 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2656 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002657 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002658
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002659 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002660
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002661 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2662 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002663 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002664
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002665 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002666 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002667 EmitDifference("func_end", DebugFrameInfo.Number,
2668 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002669 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002670
Devang Patel5aac3d32009-01-17 08:01:33 +00002671 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00002672 false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002673
Evan Cheng05548eb2008-02-29 19:36:59 +00002674 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002675 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00002676
Jim Laskeybacd3042007-02-21 22:48:45 +00002677 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002678 }
2679
Evan Chenge3d42322009-02-25 07:04:34 +00002680 void EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2681 EmitDifference("pubnames_end", Unit->getID(),
2682 "pubnames_begin", Unit->getID(), true);
2683 Asm->EOL("Length of Public Names Info");
2684
2685 EmitLabel("pubnames_begin", Unit->getID());
2686
2687 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2688
2689 EmitSectionOffset("info_begin", "section_info",
2690 Unit->getID(), 0, true, false);
2691 Asm->EOL("Offset of Compilation Unit Info");
2692
2693 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2694 true);
2695 Asm->EOL("Compilation Unit Length");
2696
2697 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2698 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2699 GE = Globals.end(); GI != GE; ++GI) {
2700 const std::string &Name = GI->first;
2701 DIE * Entity = GI->second;
2702
2703 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2704 Asm->EmitString(Name); Asm->EOL("External Name");
2705 }
2706
2707 Asm->EmitInt32(0); Asm->EOL("End Mark");
2708 EmitLabel("pubnames_end", Unit->getID());
2709
2710 Asm->EOL();
2711 }
2712
Jim Laskeyef42a012006-11-02 20:12:39 +00002713 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002714 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002715 void EmitDebugPubNames() {
2716 // Start the dwarf pubnames section.
2717 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002718
Evan Chenge3d42322009-02-25 07:04:34 +00002719 if (MainCU) {
2720 EmitDebugPubNamesPerCU(MainCU);
2721 return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002722 }
Evan Chenge3d42322009-02-25 07:04:34 +00002723
2724 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2725 EmitDebugPubNamesPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002726 }
2727
2728 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002729 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002730 void EmitDebugStr() {
2731 // Check to see if it is worth the effort.
2732 if (!StringPool.empty()) {
2733 // Start the dwarf str section.
2734 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002735
Jim Laskeyef42a012006-11-02 20:12:39 +00002736 // For each of strings in the string pool.
2737 for (unsigned StringID = 1, N = StringPool.size();
2738 StringID <= N; ++StringID) {
2739 // Emit a label for reference from debug information entries.
2740 EmitLabel("string", StringID);
2741 // Emit the string itself.
2742 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00002743 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002744 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002745
Jim Laskeybacd3042007-02-21 22:48:45 +00002746 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002747 }
2748 }
2749
2750 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002751 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002752 void EmitDebugLoc() {
2753 // Start the dwarf loc section.
2754 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002755
Jim Laskeybacd3042007-02-21 22:48:45 +00002756 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002757 }
2758
2759 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002760 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002761 void EmitDebugARanges() {
2762 // Start the dwarf aranges section.
2763 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002764
Jim Laskeyef42a012006-11-02 20:12:39 +00002765 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00002766#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002767 CompileUnit *Unit = GetBaseCompileUnit();
2768
Jim Laskey5496f012006-11-09 14:52:14 +00002769 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002770 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002771
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002772 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002773
Jim Laskey5496f012006-11-09 14:52:14 +00002774 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002775 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002776
Dan Gohman82482942007-09-27 23:12:31 +00002777 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002778
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002779 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002780
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002781 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2782 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002783
Jim Laskey5496f012006-11-09 14:52:14 +00002784 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002785 EmitReference("text_begin", 0); Asm->EOL("Address");
2786 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002787
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002788 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2789 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00002790#endif
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002791
Jim Laskeybacd3042007-02-21 22:48:45 +00002792 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002793 }
2794
2795 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002796 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002797 void EmitDebugRanges() {
2798 // Start the dwarf ranges section.
2799 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002800
Jim Laskeybacd3042007-02-21 22:48:45 +00002801 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002802 }
2803
2804 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002805 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002806 void EmitDebugMacInfo() {
Scott Michel210de722009-01-26 22:32:51 +00002807 if (TAI->doesSupportMacInfoSection()) {
2808 // Start the dwarf macinfo section.
2809 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002810
Scott Michel210de722009-01-26 22:32:51 +00002811 Asm->EOL();
2812 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002813 }
2814
Bill Wendlingc8615e42009-03-10 21:47:45 +00002815 /// GetOrCreateSourceID - Look up the source id with the given directory and
2816 /// source file names. If none currently exists, create a new id and insert it
2817 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
2818 /// as well.
2819 unsigned GetOrCreateSourceID(const std::string &DirName,
2820 const std::string &FileName) {
2821 unsigned DId;
2822 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
2823 if (DI != DirectoryIdMap.end()) {
2824 DId = DI->getValue();
2825 } else {
2826 DId = DirectoryNames.size() + 1;
2827 DirectoryIdMap[DirName] = DId;
2828 DirectoryNames.push_back(DirName);
2829 }
2830
2831 unsigned FId;
2832 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
2833 if (FI != SourceFileIdMap.end()) {
2834 FId = FI->getValue();
2835 } else {
2836 FId = SourceFileNames.size() + 1;
2837 SourceFileIdMap[FileName] = FId;
2838 SourceFileNames.push_back(FileName);
2839 }
2840
2841 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
2842 SourceIdMap.find(std::make_pair(DId, FId));
2843 if (SI != SourceIdMap.end())
2844 return SI->second;
2845
2846 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
2847 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
2848 SourceIds.push_back(std::make_pair(DId, FId));
2849
2850 return SrcId;
2851 }
2852
Evan Chenge3d42322009-02-25 07:04:34 +00002853 void ConstructCompileUnit(GlobalVariable *GV) {
2854 DICompileUnit DIUnit(GV);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002855 std::string Dir, FN, Prod;
Bill Wendlingc8615e42009-03-10 21:47:45 +00002856 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002857 DIUnit.getFilename(FN));
Evan Chenge3d42322009-02-25 07:04:34 +00002858
2859 DIE *Die = new DIE(DW_TAG_compile_unit);
2860 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2861 DWLabel("section_line", 0), DWLabel("section_line", 0),
2862 false);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002863 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer(Prod));
Evan Chenge3d42322009-02-25 07:04:34 +00002864 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002865 AddString(Die, DW_AT_name, DW_FORM_string, FN);
2866 if (!Dir.empty())
2867 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Chenge3d42322009-02-25 07:04:34 +00002868 if (DIUnit.isOptimized())
2869 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002870 std::string Flags;
2871 DIUnit.getFlags(Flags);
Evan Chenge3d42322009-02-25 07:04:34 +00002872 if (!Flags.empty())
2873 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
2874 unsigned RVer = DIUnit.getRunTimeVersion();
2875 if (RVer)
2876 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
2877
2878 CompileUnit *Unit = new CompileUnit(ID, Die);
2879 if (DIUnit.isMain()) {
2880 assert(!MainCU && "Multiple main compile units are found!");
2881 MainCU = Unit;
2882 }
2883 CompileUnitMap[DIUnit.getGV()] = Unit;
2884 CompileUnits.push_back(Unit);
2885 }
2886
Devang Patelc4523242009-01-05 23:11:11 +00002887 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Pateld1ca9252009-01-05 23:03:32 +00002888 void ConstructCompileUnits() {
Evan Chenge3d42322009-02-25 07:04:34 +00002889 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.compile_units");
2890 if (!Root)
2891 return;
2892 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2893 "Malformed compile unit descriptor anchor type");
2894 Constant *RootC = cast<Constant>(*Root->use_begin());
2895 assert(RootC->hasNUsesOrMore(1) &&
2896 "Malformed compile unit descriptor anchor type");
2897 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2898 UI != UE; ++UI)
2899 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2900 UUI != UUE; ++UUI) {
2901 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2902 ConstructCompileUnit(GV);
Devang Pateldd9db662009-01-30 18:20:31 +00002903 }
Evan Chenge3d42322009-02-25 07:04:34 +00002904 }
2905
2906 bool ConstructGlobalVariableDIE(GlobalVariable *GV) {
2907 DIGlobalVariable DI_GV(GV);
2908 CompileUnit *DW_Unit = MainCU;
2909 if (!DW_Unit)
2910 DW_Unit = FindCompileUnit(DI_GV.getCompileUnit());
2911
2912 // Check for pre-existence.
2913 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
2914 if (Slot)
2915 return false;
2916
2917 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
2918
2919 // Add address.
2920 DIEBlock *Block = new DIEBlock();
2921 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
2922 AddObjectLabel(Block, 0, DW_FORM_udata,
2923 Asm->getGlobalLinkName(DI_GV.getGlobal()));
2924 AddBlock(VariableDie, DW_AT_location, 0, Block);
2925
2926 // Add to map.
2927 Slot = VariableDie;
2928 // Add to context owner.
2929 DW_Unit->getDie()->AddChild(VariableDie);
2930 // Expose as global. FIXME - need to check external flag.
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002931 std::string Name;
2932 DW_Unit->AddGlobal(DI_GV.getName(Name), VariableDie);
Evan Chenge3d42322009-02-25 07:04:34 +00002933 return true;
Devang Pateld1ca9252009-01-05 23:03:32 +00002934 }
2935
Devang Patelc4523242009-01-05 23:11:11 +00002936 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patel53cac182009-02-24 00:02:15 +00002937 /// visible global variables. Return true if at least one global DIE is
2938 /// created.
2939 bool ConstructGlobalVariableDIEs() {
Evan Chenge3d42322009-02-25 07:04:34 +00002940 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
2941 if (!Root)
2942 return false;
Devang Patelc4523242009-01-05 23:11:11 +00002943
Evan Chenge3d42322009-02-25 07:04:34 +00002944 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2945 "Malformed global variable descriptor anchor type");
2946 Constant *RootC = cast<Constant>(*Root->use_begin());
2947 assert(RootC->hasNUsesOrMore(1) &&
2948 "Malformed global variable descriptor anchor type");
Devang Patelc4523242009-01-05 23:11:11 +00002949
Evan Chenge3d42322009-02-25 07:04:34 +00002950 bool Result = false;
2951 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2952 UI != UE; ++UI)
2953 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2954 UUI != UUE; ++UUI) {
2955 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2956 Result |= ConstructGlobalVariableDIE(GV);
2957 }
2958 return Result;
2959 }
Devang Patelc4523242009-01-05 23:11:11 +00002960
Evan Chenge3d42322009-02-25 07:04:34 +00002961 bool ConstructSubprogram(GlobalVariable *GV) {
2962 DISubprogram SP(GV);
2963 CompileUnit *Unit = MainCU;
2964 if (!Unit)
2965 Unit = FindCompileUnit(SP.getCompileUnit());
Devang Patelc4523242009-01-05 23:11:11 +00002966
Evan Chenge3d42322009-02-25 07:04:34 +00002967 // Check for pre-existence.
2968 DIE *&Slot = Unit->getDieMapSlotFor(GV);
2969 if (Slot)
2970 return false;
2971
2972 if (!SP.isDefinition())
2973 // This is a method declaration which will be handled while
2974 // constructing class type.
2975 return false;
2976
2977 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
2978
2979 // Add to map.
2980 Slot = SubprogramDie;
2981 // Add to context owner.
2982 Unit->getDie()->AddChild(SubprogramDie);
2983 // Expose as global.
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002984 std::string Name;
2985 Unit->AddGlobal(SP.getName(Name), SubprogramDie);
Evan Chenge3d42322009-02-25 07:04:34 +00002986 return true;
Devang Patelc4523242009-01-05 23:11:11 +00002987 }
2988
Devang Patel78eb6ad2009-01-05 23:21:35 +00002989 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patel53cac182009-02-24 00:02:15 +00002990 /// subprograms. Return true if at least one subprogram DIE is created.
2991 bool ConstructSubprograms() {
Evan Chenge3d42322009-02-25 07:04:34 +00002992 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
2993 if (!Root)
2994 return false;
Devang Patel78eb6ad2009-01-05 23:21:35 +00002995
Evan Chenge3d42322009-02-25 07:04:34 +00002996 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2997 "Malformed subprogram descriptor anchor type");
2998 Constant *RootC = cast<Constant>(*Root->use_begin());
2999 assert(RootC->hasNUsesOrMore(1) &&
3000 "Malformed subprogram descriptor anchor type");
Devang Patel78eb6ad2009-01-05 23:21:35 +00003001
Evan Chenge3d42322009-02-25 07:04:34 +00003002 bool Result = false;
3003 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3004 UI != UE; ++UI)
3005 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3006 UUI != UUE; ++UUI) {
3007 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3008 Result |= ConstructSubprogram(GV);
3009 }
3010 return Result;
Devang Patel78eb6ad2009-01-05 23:21:35 +00003011 }
3012
Jim Laskey65195462006-10-30 13:35:07 +00003013public:
Jim Laskeyef42a012006-11-02 20:12:39 +00003014 //===--------------------------------------------------------------------===//
3015 // Main entry points.
3016 //
Owen Andersoncb371882008-08-21 00:14:44 +00003017 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00003018 : Dwarf(OS, A, T, "dbg"), MainCU(0),
3019 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
3020 ValuesSet(InitValuesSetSize), Values(), StringPool(), SectionMap(),
3021 SectionSourceLines(), didInitial(false), shouldEmit(false),
3022 RootDbgScope(0), DebugTimer(0) {
3023 if (TimePassesIsEnabled)
3024 DebugTimer = new Timer("Dwarf Debug Writer",
3025 *getDwarfTimerGroup());
Jim Laskeyef42a012006-11-02 20:12:39 +00003026 }
Jim Laskey072200c2007-01-29 18:51:14 +00003027 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003028 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3029 delete Values[j];
Bill Wendling163ba3f2009-03-10 21:23:25 +00003030
3031 delete DebugTimer;
Jim Laskeyef42a012006-11-02 20:12:39 +00003032 }
3033
Bill Wendlingc8615e42009-03-10 21:47:45 +00003034 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
3035 /// be emitted.
3036 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
3037
Devang Patel5d315982009-01-06 21:07:30 +00003038 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3039 /// This is inovked by the target AsmPrinter.
Devang Patel3f7833a2009-01-12 23:09:42 +00003040 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003041 if (TimePassesIsEnabled)
3042 DebugTimer->startTimer();
3043
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003044 // Create all the compile unit DIEs.
3045 ConstructCompileUnits();
Devang Patel3f7833a2009-01-12 23:09:42 +00003046
Bill Wendling163ba3f2009-03-10 21:23:25 +00003047 if (CompileUnits.empty()) {
3048 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003049 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003050
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003051 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003052 }
Devang Patel3f7833a2009-01-12 23:09:42 +00003053
Devang Patel53cac182009-02-24 00:02:15 +00003054 // Create DIEs for each of the externally visible global variables.
3055 bool globalDIEs = ConstructGlobalVariableDIEs();
3056
3057 // Create DIEs for each of the externally visible subprograms.
3058 bool subprogramDIEs = ConstructSubprograms();
3059
3060 // If there is not any debug info available for any global variables
3061 // and any subprograms then there is not any debug info to emit.
Bill Wendling163ba3f2009-03-10 21:23:25 +00003062 if (!globalDIEs && !subprogramDIEs) {
3063 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003064 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003065
Devang Patel53cac182009-02-24 00:02:15 +00003066 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003067 }
Devang Patel53cac182009-02-24 00:02:15 +00003068
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003069 MMI = mmi;
3070 shouldEmit = true;
3071 MMI->setDebugInfoAvailability(true);
Devang Patel5d315982009-01-06 21:07:30 +00003072
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003073 // Prime section data.
3074 SectionMap.insert(TAI->getTextSection());
Devang Patel5d315982009-01-06 21:07:30 +00003075
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003076 // Print out .file directives to specify files for .loc directives. These
3077 // are printed out early so that they precede any .loc directives.
3078 if (TAI->hasDotLocAndDotFile()) {
Evan Chenge3d42322009-02-25 07:04:34 +00003079 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
3080 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00003081 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Evan Chenge3d42322009-02-25 07:04:34 +00003082 sys::Path FullPath(getSourceDirectoryName(Id.first));
3083 bool AppendOk =
3084 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003085 assert(AppendOk && "Could not append filename to directory!");
3086 AppendOk = false;
3087 Asm->EmitFile(i, FullPath.toString());
3088 Asm->EOL();
Devang Patel5d315982009-01-06 21:07:30 +00003089 }
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003090 }
Devang Patel5d315982009-01-06 21:07:30 +00003091
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003092 // Emit initial sections
3093 EmitInitial();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003094
3095 if (TimePassesIsEnabled)
3096 DebugTimer->stopTimer();
Devang Patel5d315982009-01-06 21:07:30 +00003097 }
3098
Jim Laskey65195462006-10-30 13:35:07 +00003099 /// BeginModule - Emit all Dwarf sections that should come prior to the
3100 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00003101 void BeginModule(Module *M) {
3102 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00003103 }
3104
Jim Laskey65195462006-10-30 13:35:07 +00003105 /// EndModule - Emit all Dwarf sections that should come after the content.
3106 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003107 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003108 if (!ShouldEmitDwarfDebug())
3109 return;
3110
3111 if (TimePassesIsEnabled)
3112 DebugTimer->startTimer();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003113
Jim Laskeyef42a012006-11-02 20:12:39 +00003114 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003115 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003116 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00003117 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003118 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003119
Jim Laskeyef42a012006-11-02 20:12:39 +00003120 // End text sections.
3121 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003122 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00003123 EmitLabel("section_end", i);
3124 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003125
3126 // Emit common frame information.
3127 EmitCommonDebugFrame();
3128
3129 // Emit function debug frame information
3130 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3131 E = DebugFrames.end(); I != E; ++I)
3132 EmitFunctionDebugFrame(*I);
3133
Jim Laskeyef42a012006-11-02 20:12:39 +00003134 // Compute DIE offsets and sizes.
3135 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003136
Jim Laskeyef42a012006-11-02 20:12:39 +00003137 // Emit all the DIEs into a debug info section
3138 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003139
Jim Laskeyef42a012006-11-02 20:12:39 +00003140 // Corresponding abbreviations into a abbrev section.
3141 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003142
Jim Laskeyef42a012006-11-02 20:12:39 +00003143 // Emit source line correspondence into a debug line section.
3144 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003145
Jim Laskeyef42a012006-11-02 20:12:39 +00003146 // Emit info into a debug pubnames section.
3147 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003148
Jim Laskeyef42a012006-11-02 20:12:39 +00003149 // Emit info into a debug str section.
3150 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003151
Jim Laskeyef42a012006-11-02 20:12:39 +00003152 // Emit info into a debug loc section.
3153 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003154
Jim Laskeyef42a012006-11-02 20:12:39 +00003155 // Emit info into a debug aranges section.
3156 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003157
Jim Laskeyef42a012006-11-02 20:12:39 +00003158 // Emit info into a debug ranges section.
3159 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003160
Jim Laskeyef42a012006-11-02 20:12:39 +00003161 // Emit info into a debug macinfo section.
3162 EmitDebugMacInfo();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003163
3164 if (TimePassesIsEnabled)
3165 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003166 }
3167
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003168 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00003169 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00003170 void BeginFunction(MachineFunction *MF) {
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003171 if (!ShouldEmitDwarfDebug()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00003172
Bill Wendling163ba3f2009-03-10 21:23:25 +00003173 if (TimePassesIsEnabled)
3174 DebugTimer->startTimer();
3175
3176 this->MF = MF;
3177
Jim Laskeyef42a012006-11-02 20:12:39 +00003178 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003179 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003180
Jim Laskeyef42a012006-11-02 20:12:39 +00003181 // Assumes in correct section after the entry point.
3182 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00003183
3184 // Emit label for the implicitly defined dbg.stoppoint at the start of
3185 // the function.
Devang Patelf3ee5142009-01-12 22:54:42 +00003186 if (!Lines.empty()) {
3187 const SrcLineInfo &LineInfo = Lines[0];
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00003188 Asm->printLabel(LineInfo.getLabelID());
3189 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003190
3191 if (TimePassesIsEnabled)
3192 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003193 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003194
Jim Laskey65195462006-10-30 13:35:07 +00003195 /// EndFunction - Gather and emit post-function debug information.
3196 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00003197 void EndFunction(MachineFunction *MF) {
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003198 if (!ShouldEmitDwarfDebug()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003199
Bill Wendling163ba3f2009-03-10 21:23:25 +00003200 if (TimePassesIsEnabled)
3201 DebugTimer->startTimer();
3202
Jim Laskeyb82313f2007-02-01 16:31:34 +00003203 // Define end label for subprogram.
3204 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003205
Jim Laskeyef42a012006-11-02 20:12:39 +00003206 // Get function line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00003207 if (!Lines.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003208 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003209 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00003210 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patelf3ee5142009-01-12 22:54:42 +00003211 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Jim Laskeyef42a012006-11-02 20:12:39 +00003212 // Append the function info to section info.
3213 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patelf3ee5142009-01-12 22:54:42 +00003214 Lines.begin(), Lines.end());
Jim Laskeyef42a012006-11-02 20:12:39 +00003215 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003216
Jim Laskeyef42a012006-11-02 20:12:39 +00003217 // Construct scopes for subprogram.
Devang Patel7bb89ed2009-01-13 00:20:51 +00003218 if (RootDbgScope)
3219 ConstructRootDbgScope(RootDbgScope);
Bill Wendlingd751c642008-09-26 00:28:12 +00003220 else
3221 // FIXME: This is wrong. We are essentially getting past a problem with
3222 // debug information not being able to handle unreachable blocks that have
3223 // debug information in them. In particular, those unreachable blocks that
3224 // have "region end" info in them. That situation results in the "root
3225 // scope" not being created. If that's the case, then emit a "default"
3226 // scope, i.e., one that encompasses the whole function. This isn't
3227 // desirable. And a better way of handling this (and all of the debugging
3228 // information) needs to be explored.
Devang Patel7bb89ed2009-01-13 00:20:51 +00003229 ConstructDefaultDbgScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003230
3231 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3232 MMI->getFrameMoves()));
Devang Patel481ff5b2009-01-12 18:48:36 +00003233
3234 // Clear debug info
3235 if (RootDbgScope) {
3236 delete RootDbgScope;
3237 DbgScopeMap.clear();
3238 RootDbgScope = NULL;
3239 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003240
Bill Wendling163ba3f2009-03-10 21:23:25 +00003241 Lines.clear();
3242
3243 if (TimePassesIsEnabled)
3244 DebugTimer->stopTimer();
3245 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003246
Devang Patelcf3a4482009-01-15 23:41:32 +00003247 /// ValidDebugInfo - Return true if V represents valid debug info value.
3248 bool ValidDebugInfo(Value *V) {
Devang Patelb79b5352009-01-19 23:21:49 +00003249 if (!V)
3250 return false;
3251
Devang Patel61c6bf32009-01-16 01:49:46 +00003252 if (!shouldEmit)
3253 return false;
3254
Devang Patelcf3a4482009-01-15 23:41:32 +00003255 GlobalVariable *GV = getGlobalVariable(V);
3256 if (!GV)
3257 return false;
Duncan Sands667d4b82009-03-07 15:45:40 +00003258
3259 if (!GV->hasInternalLinkage () && !GV->hasLinkOnceLinkage())
Devang Patelcf3a4482009-01-15 23:41:32 +00003260 return false;
3261
Bill Wendling163ba3f2009-03-10 21:23:25 +00003262 if (TimePassesIsEnabled)
3263 DebugTimer->startTimer();
3264
Devang Patelcf3a4482009-01-15 23:41:32 +00003265 DIDescriptor DI(GV);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003266
Devang Patelcf3a4482009-01-15 23:41:32 +00003267 // Check current version. Allow Version6 for now.
3268 unsigned Version = DI.getVersion();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003269 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6) {
3270 if (TimePassesIsEnabled)
3271 DebugTimer->stopTimer();
3272
Devang Patelcf3a4482009-01-15 23:41:32 +00003273 return false;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003274 }
Devang Patelcf3a4482009-01-15 23:41:32 +00003275
Devang Patelb79b5352009-01-19 23:21:49 +00003276 unsigned Tag = DI.getTag();
3277 switch (Tag) {
3278 case DW_TAG_variable:
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003279 assert(DIVariable(GV).Verify() && "Invalid DebugInfo value");
Devang Patelb79b5352009-01-19 23:21:49 +00003280 break;
3281 case DW_TAG_compile_unit:
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003282 assert(DICompileUnit(GV).Verify() && "Invalid DebugInfo value");
Devang Patelb79b5352009-01-19 23:21:49 +00003283 break;
3284 case DW_TAG_subprogram:
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003285 assert(DISubprogram(GV).Verify() && "Invalid DebugInfo value");
Devang Patelb79b5352009-01-19 23:21:49 +00003286 break;
3287 default:
3288 break;
3289 }
3290
Bill Wendling163ba3f2009-03-10 21:23:25 +00003291 if (TimePassesIsEnabled)
3292 DebugTimer->stopTimer();
3293
Devang Patelcf3a4482009-01-15 23:41:32 +00003294 return true;
3295 }
3296
Devang Patelccca7fe2009-01-12 19:17:34 +00003297 /// RecordSourceLine - Records location information and associates it with a
3298 /// label. Returns a unique label ID used to generate a label and provide
3299 /// correspondence to the source line list.
3300 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003301 if (TimePassesIsEnabled)
3302 DebugTimer->startTimer();
3303
Evan Chenge3d42322009-02-25 07:04:34 +00003304 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003305 assert(Unit && "Unable to find CompileUnit");
Devang Patelccca7fe2009-01-12 19:17:34 +00003306 unsigned ID = MMI->NextLabelID();
3307 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003308
3309 if (TimePassesIsEnabled)
3310 DebugTimer->stopTimer();
3311
Devang Patelccca7fe2009-01-12 19:17:34 +00003312 return ID;
3313 }
3314
3315 /// RecordSourceLine - Records location information and associates it with a
3316 /// label. Returns a unique label ID used to generate a label and provide
3317 /// correspondence to the source line list.
3318 unsigned RecordSourceLine(unsigned Line, unsigned Col, unsigned Src) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003319 if (TimePassesIsEnabled)
3320 DebugTimer->startTimer();
3321
Devang Patelccca7fe2009-01-12 19:17:34 +00003322 unsigned ID = MMI->NextLabelID();
3323 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003324
3325 if (TimePassesIsEnabled)
3326 DebugTimer->stopTimer();
3327
Devang Patelccca7fe2009-01-12 19:17:34 +00003328 return ID;
3329 }
3330
Bill Wendlingc8615e42009-03-10 21:47:45 +00003331 /// getRecordSourceLineCount - Return the number of source lines in the debug
3332 /// info.
3333 unsigned getRecordSourceLineCount() const {
Devang Patelccca7fe2009-01-12 19:17:34 +00003334 return Lines.size();
3335 }
3336
Bill Wendlingc8615e42009-03-10 21:47:45 +00003337 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
3338 /// timed. Look up the source id with the given directory and source file
3339 /// names. If none currently exists, create a new id and insert it in the
3340 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
3341 /// well.
Evan Chenge3d42322009-02-25 07:04:34 +00003342 unsigned getOrCreateSourceID(const std::string &DirName,
3343 const std::string &FileName) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003344 if (TimePassesIsEnabled)
3345 DebugTimer->startTimer();
3346
Bill Wendlingc8615e42009-03-10 21:47:45 +00003347 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003348
3349 if (TimePassesIsEnabled)
3350 DebugTimer->stopTimer();
3351
Evan Chenge3d42322009-02-25 07:04:34 +00003352 return SrcId;
Devang Patelccca7fe2009-01-12 19:17:34 +00003353 }
3354
3355 /// RecordRegionStart - Indicate the start of a region.
Devang Patelccca7fe2009-01-12 19:17:34 +00003356 unsigned RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003357 if (TimePassesIsEnabled)
3358 DebugTimer->startTimer();
3359
Devang Patelccca7fe2009-01-12 19:17:34 +00003360 DbgScope *Scope = getOrCreateScope(V);
3361 unsigned ID = MMI->NextLabelID();
3362 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003363
3364 if (TimePassesIsEnabled)
3365 DebugTimer->stopTimer();
3366
Devang Patelccca7fe2009-01-12 19:17:34 +00003367 return ID;
3368 }
3369
3370 /// RecordRegionEnd - Indicate the end of a region.
Devang Patelccca7fe2009-01-12 19:17:34 +00003371 unsigned RecordRegionEnd(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003372 if (TimePassesIsEnabled)
3373 DebugTimer->startTimer();
3374
Devang Patelccca7fe2009-01-12 19:17:34 +00003375 DbgScope *Scope = getOrCreateScope(V);
3376 unsigned ID = MMI->NextLabelID();
3377 Scope->setEndLabelID(ID);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003378
3379 if (TimePassesIsEnabled)
3380 DebugTimer->stopTimer();
3381
Devang Patelccca7fe2009-01-12 19:17:34 +00003382 return ID;
3383 }
3384
3385 /// RecordVariable - Indicate the declaration of a local variable.
Devang Patelccca7fe2009-01-12 19:17:34 +00003386 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003387 if (TimePassesIsEnabled)
3388 DebugTimer->startTimer();
3389
Devang Patel0e5200f2009-01-15 18:25:17 +00003390 DIDescriptor Desc(GV);
3391 DbgScope *Scope = NULL;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003392
Devang Patel0e5200f2009-01-15 18:25:17 +00003393 if (Desc.getTag() == DW_TAG_variable) {
3394 // GV is a global variable.
3395 DIGlobalVariable DG(GV);
3396 Scope = getOrCreateScope(DG.getContext().getGV());
3397 } else {
3398 // or GV is a local variable.
3399 DIVariable DV(GV);
3400 Scope = getOrCreateScope(DV.getContext().getGV());
3401 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003402
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003403 assert(Scope && "Unable to find variable' scope");
Devang Patel99ec3532009-01-16 19:28:14 +00003404 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelccca7fe2009-01-12 19:17:34 +00003405 Scope->AddVariable(DV);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003406
3407 if (TimePassesIsEnabled)
3408 DebugTimer->stopTimer();
Devang Patelccca7fe2009-01-12 19:17:34 +00003409 }
Jim Laskey65195462006-10-30 13:35:07 +00003410};
3411
Jim Laskey072200c2007-01-29 18:51:14 +00003412//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003413/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00003414///
3415class DwarfException : public Dwarf {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003416 struct FunctionEHFrameInfo {
3417 std::string FnName;
3418 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003419 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003420 bool hasCalls;
3421 bool hasLandingPads;
3422 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003423 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00003424
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003425 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3426 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003427 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003428 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003429 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003430 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003431 };
3432
3433 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003434
3435 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3436 /// be emitted.
3437 bool shouldEmitTable;
3438
3439 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3440 /// should be emitted.
3441 bool shouldEmitMoves;
3442
3443 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3444 /// should be emitted.
3445 bool shouldEmitTableModule;
3446
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003447 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003448 /// should be emitted.
3449 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00003450
Bill Wendling163ba3f2009-03-10 21:23:25 +00003451 /// ExceptionTimer - Timer for the Dwarf exception writer.
3452 Timer *ExceptionTimer;
3453
Jim Laskey3f09fc22007-02-28 18:38:31 +00003454 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3455 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003456 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003457 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003458 int stackGrowth =
3459 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3460 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003461 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003462
Jim Laskeybacd3042007-02-21 22:48:45 +00003463 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003464 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling722f5f12008-12-24 08:05:17 +00003465
3466 if (!TAI->doesRequireNonLocalEHFrameLabel())
3467 O << TAI->getEHGlobalPrefix();
3468 O << "EH_frame" << Index << ":\n";
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003469 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003470
Jim Laskeybacd3042007-02-21 22:48:45 +00003471 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003472 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003473
Jim Laskeybacd3042007-02-21 22:48:45 +00003474 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003475 EmitDifference("eh_frame_common_end", Index,
3476 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003477 Asm->EOL("Length of Common Information Entry");
3478
Jim Laskeybacd3042007-02-21 22:48:45 +00003479 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003480 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003481 Asm->EmitInt32((int)0);
3482 Asm->EOL("CIE Identifier Tag");
3483 Asm->EmitInt8(DW_CIE_VERSION);
3484 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00003485
Jim Laskeybacd3042007-02-21 22:48:45 +00003486 // The personality presence indicates that language specific information
3487 // will show up in the eh frame.
3488 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00003489 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00003490
Jim Laskeybacd3042007-02-21 22:48:45 +00003491 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003492 Asm->EmitULEB128Bytes(1);
3493 Asm->EOL("CIE Code Alignment Factor");
3494 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00003495 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003496 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00003497 Asm->EOL("CIE Return Address Column");
3498
Jim Laskeybacd3042007-02-21 22:48:45 +00003499 // If there is a personality, we need to indicate the functions location.
3500 if (Personality) {
3501 Asm->EmitULEB128Bytes(7);
3502 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00003503
Duncan Sands671fa972008-05-07 19:11:09 +00003504 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003505 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00003506 Asm->EOL("Personality (pcrel sdata4 indirect)");
3507 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003508 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00003509 Asm->EOL("Personality (pcrel sdata4)");
3510 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00003511
Duncan Sands671fa972008-05-07 19:11:09 +00003512 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00003513 O << TAI->getPersonalityPrefix();
3514 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3515 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00003516 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3517 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00003518 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00003519
Duncan Sands671fa972008-05-07 19:11:09 +00003520 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3521 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003522
Bill Wendlingd60de512009-01-05 22:53:45 +00003523 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3524 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003525 } else {
3526 Asm->EmitULEB128Bytes(1);
3527 Asm->EOL("Augmentation Size");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003528
Bill Wendlingd60de512009-01-05 22:53:45 +00003529 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3530 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003531 }
3532
3533 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003534 std::vector<MachineMove> Moves;
3535 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00003536 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003537
Dale Johannesen21d972a2008-04-30 00:43:29 +00003538 // On Darwin the linker honors the alignment of eh_frame, which means it
3539 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3540 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003541 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003542 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003543 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003544
Jim Laskeybacd3042007-02-21 22:48:45 +00003545 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003546 }
Duncan Sands671fa972008-05-07 19:11:09 +00003547
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003548 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003549 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003550 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003551 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3552
Jim Laskeybacd3042007-02-21 22:48:45 +00003553 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3554
3555 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003556 // If the corresponding function is static, this should not be
3557 // externally visible.
Rafael Espindolabb46f522009-01-15 20:18:42 +00003558 if (linkage != Function::InternalLinkage &&
Devang Patel2d1768c2009-01-17 08:05:14 +00003559 linkage != Function::PrivateLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003560 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3561 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3562 }
3563
Dale Johannesen038129d2008-01-10 02:03:30 +00003564 // If corresponding function is weak definition, this should be too.
Duncan Sands667d4b82009-03-07 15:45:40 +00003565 if ((linkage == Function::WeakAnyLinkage ||
3566 linkage == Function::WeakODRLinkage ||
3567 linkage == Function::LinkOnceAnyLinkage ||
3568 linkage == Function::LinkOnceODRLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00003569 TAI->getWeakDefDirective())
3570 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3571
3572 // If there are no calls then you can't unwind. This may mean we can
3573 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003574 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00003575 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00003576 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00003577 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00003578 !UnwindTablesMandatory &&
Duncan Sands667d4b82009-03-07 15:45:40 +00003579 ((linkage != Function::WeakAnyLinkage &&
3580 linkage != Function::WeakODRLinkage &&
3581 linkage != Function::LinkOnceAnyLinkage &&
3582 linkage != Function::LinkOnceODRLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00003583 !TAI->getWeakDefDirective() ||
3584 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003585 {
Bill Wendling6e198962007-09-18 01:47:22 +00003586 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003587 // This name has no connection to the function, so it might get
3588 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003589 // dead-stripping unconditionally.
3590 if (const char *UsedDirective = TAI->getUsedDirective())
3591 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00003592 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00003593 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003594
Jim Laskeybacd3042007-02-21 22:48:45 +00003595 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003596 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3597 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003598 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003599
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003600 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003601
Bill Wendling722f5f12008-12-24 08:05:17 +00003602 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3603 PrintRelDirective(true, true);
3604 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3605
3606 if (!TAI->isAbsoluteEHSectionOffsets())
3607 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3608 } else {
3609 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3610 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3611 true, true, false);
3612 }
3613
Jim Laskeybacd3042007-02-21 22:48:45 +00003614 Asm->EOL("FDE CIE offset");
3615
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003616 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003617 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003618 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003619 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003620 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00003621
Jim Laskey3f09fc22007-02-28 18:38:31 +00003622 // If there is a personality and landing pads then point to the language
3623 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003624 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00003625 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00003626 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00003627
3628 if (EHFrameInfo.hasLandingPads)
3629 EmitReference("exception", EHFrameInfo.Number, true, true);
3630 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00003631 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00003632 Asm->EOL("Language Specific Data Area");
3633 } else {
3634 Asm->EmitULEB128Bytes(0);
3635 Asm->EOL("Augmentation size");
3636 }
Duncan Sands671fa972008-05-07 19:11:09 +00003637
Jim Laskeybacd3042007-02-21 22:48:45 +00003638 // Indicate locations of function specific callee saved registers in
3639 // frame.
Devang Patel5aac3d32009-01-17 08:01:33 +00003640 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00003641 true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003642
Dale Johannesen21d972a2008-04-30 00:43:29 +00003643 // On Darwin the linker honors the alignment of eh_frame, which means it
3644 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3645 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003646 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003647 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003648 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003649
3650 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003651 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003652 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003653 // that depends on unused functions (calling undefined externals) being
3654 // dead-stripped to link correctly. Yes, there really is.
3655 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3656 if (const char *UsedDirective = TAI->getUsedDirective())
3657 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3658 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003659 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003660
Duncan Sands57810cd2007-09-05 11:27:52 +00003661 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003662 ///
3663 /// The general organization of the table is complex, but the basic concepts
3664 /// are easy. First there is a header which describes the location and
3665 /// organization of the three components that follow.
3666 /// 1. The landing pad site information describes the range of code covered
3667 /// by the try. In our case it's an accumulation of the ranges covered
3668 /// by the invokes in the try. There is also a reference to the landing
3669 /// pad that handles the exception once processed. Finally an index into
3670 /// the actions table.
3671 /// 2. The action table, in our case, is composed of pairs of type ids
3672 /// and next action offset. Starting with the action index from the
3673 /// landing pad site, each type Id is checked for a match to the current
3674 /// exception. If it matches then the exception and type id are passed
3675 /// on to the landing pad. Otherwise the next action is looked up. This
3676 /// chain is terminated with a next action of zero. If no type id is
3677 /// found the the frame is unwound and handling continues.
3678 /// 3. Type id table contains references to all the C++ typeinfo for all
3679 /// catches in the function. This tables is reversed indexed base 1.
3680
Duncan Sandsb32edb42007-06-06 15:37:31 +00003681 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3682 static unsigned SharedTypeIds(const LandingPadInfo *L,
3683 const LandingPadInfo *R) {
3684 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003685 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003686 unsigned MinSize = LSize < RSize ? LSize : RSize;
3687 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003688
Duncan Sandsb32edb42007-06-06 15:37:31 +00003689 for (; Count != MinSize; ++Count)
3690 if (LIds[Count] != RIds[Count])
3691 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003692
Duncan Sandsb32edb42007-06-06 15:37:31 +00003693 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003694 }
3695
Duncan Sandsb32edb42007-06-06 15:37:31 +00003696 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00003697 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003698 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003699 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003700 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003701
Duncan Sandsb32edb42007-06-06 15:37:31 +00003702 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00003703 if (LIds[i] != RIds[i])
3704 return LIds[i] < RIds[i];
3705
Duncan Sandsb32edb42007-06-06 15:37:31 +00003706 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003707 }
3708
Duncan Sands53c3a332007-05-16 12:12:23 +00003709 struct KeyInfo {
3710 static inline unsigned getEmptyKey() { return -1U; }
3711 static inline unsigned getTombstoneKey() { return -2U; }
3712 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00003713 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00003714 static bool isPod() { return true; }
3715 };
3716
Duncan Sands57810cd2007-09-05 11:27:52 +00003717 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003718 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003719 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003720 int NextAction;
3721 struct ActionEntry *Previous;
3722 };
3723
Duncan Sands57810cd2007-09-05 11:27:52 +00003724 /// PadRange - Structure holding a try-range and the associated landing pad.
3725 struct PadRange {
3726 // The index of the landing pad.
3727 unsigned PadIndex;
3728 // The index of the begin and end labels in the landing pad's label lists.
3729 unsigned RangeIndex;
3730 };
3731
3732 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3733
3734 /// CallSiteEntry - Structure describing an entry in the call-site table.
3735 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00003736 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003737 unsigned BeginLabel; // zero indicates the start of the function.
3738 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00003739 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003740 unsigned PadLabel; // zero indicates that there is no landing pad.
3741 unsigned Action;
3742 };
3743
Jim Laskeybacd3042007-02-21 22:48:45 +00003744 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00003745 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00003746 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00003747 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3748 if (PadInfos.empty()) return;
3749
3750 // Sort the landing pads in order of their type ids. This is used to fold
3751 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00003752 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00003753 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00003754 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00003755 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003756 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3757
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003758 // Negative type ids index into FilterIds, positive type ids index into
3759 // TypeInfos. The value written for a positive type id is just the type
3760 // id itself. For a negative type id, however, the value written is the
3761 // (negative) byte offset of the corresponding FilterIds entry. The byte
3762 // offset is usually equal to the type id, because the FilterIds entries
3763 // are written using a variable width encoding which outputs one byte per
3764 // entry as long as the value written is not too large, but can differ.
3765 // This kind of complication does not occur for positive type ids because
3766 // type infos are output using a fixed width encoding.
3767 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3768 SmallVector<int, 16> FilterOffsets;
3769 FilterOffsets.reserve(FilterIds.size());
3770 int Offset = -1;
3771 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3772 E = FilterIds.end(); I != E; ++I) {
3773 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003774 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003775 }
3776
Duncan Sands57810cd2007-09-05 11:27:52 +00003777 // Compute the actions table and gather the first action index for each
3778 // landing pad site.
3779 SmallVector<ActionEntry, 32> Actions;
3780 SmallVector<unsigned, 64> FirstActions;
3781 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00003782
Duncan Sandsb32edb42007-06-06 15:37:31 +00003783 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00003784 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003785 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003786 const LandingPadInfo *LP = LandingPads[i];
3787 const std::vector<int> &TypeIds = LP->TypeIds;
3788 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003789 unsigned SizeSiteActions = 0;
3790
Duncan Sandsb32edb42007-06-06 15:37:31 +00003791 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00003792 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00003793 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003794
Duncan Sandsb32edb42007-06-06 15:37:31 +00003795 if (NumShared) {
3796 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3797 assert(Actions.size());
3798 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003799 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3800 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003801 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003802 SizeAction -=
3803 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003804 SizeAction += -PrevAction->NextAction;
3805 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003806 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00003807 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003808
Duncan Sandsb32edb42007-06-06 15:37:31 +00003809 // Compute the actions.
3810 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3811 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003812 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3813 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003814 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003815
3816 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003817 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003818 SizeSiteActions += SizeAction;
3819
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003820 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00003821 Actions.push_back(Action);
3822
3823 PrevAction = &Actions.back();
3824 }
3825
3826 // Record the first action of the landing pad site.
3827 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3828 } // else identical - re-use previous FirstAction
3829
3830 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003831
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00003832 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00003833 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00003834 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003835
Duncan Sands481dc722007-12-19 07:36:31 +00003836 // Compute the call-site table. The entry for an invoke has a try-range
3837 // containing the call, a non-zero landing pad and an appropriate action.
3838 // The entry for an ordinary call has a try-range containing the call and
3839 // zero for the landing pad and the action. Calls marked 'nounwind' have
3840 // no entry and must not be contained in the try-range of any entry - they
3841 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003842 SmallVector<CallSiteEntry, 64> CallSites;
3843
3844 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00003845 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3846 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3847 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00003848 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3849 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00003850 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003851 unsigned BeginLabel = LandingPad->BeginLabels[j];
3852 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3853 PadRange P = { i, j };
3854 PadMap[BeginLabel] = P;
3855 }
3856 }
3857
Duncan Sands481dc722007-12-19 07:36:31 +00003858 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00003859 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00003860
3861 // Whether there is a potentially throwing instruction (currently this means
3862 // an ordinary call) between the end of the previous try-range and now.
3863 bool SawPotentiallyThrowing = false;
3864
3865 // Whether the last callsite entry was for an invoke.
3866 bool PreviousIsInvoke = false;
3867
Duncan Sands481dc722007-12-19 07:36:31 +00003868 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003869 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3870 I != E; ++I) {
3871 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3872 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00003873 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00003874 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00003875 continue;
3876 }
3877
Chris Lattner9e330492007-12-30 20:50:28 +00003878 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00003879 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00003880
Duncan Sands481dc722007-12-19 07:36:31 +00003881 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00003882 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00003883 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003884
Duncan Sands481dc722007-12-19 07:36:31 +00003885 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00003886 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00003887 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00003888 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00003889 continue;
3890
3891 PadRange P = L->second;
3892 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3893
3894 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3895 "Inconsistent landing pad map!");
3896
3897 // If some instruction between the previous try-range and this one may
3898 // throw, create a call-site entry with no landing pad for the region
3899 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00003900 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003901 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3902 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00003903 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003904 }
3905
3906 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00003907 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00003908
Duncan Sands481dc722007-12-19 07:36:31 +00003909 if (LandingPad->LandingPadLabel) {
3910 // This try-range is for an invoke.
3911 CallSiteEntry Site = {BeginLabel, LastLabel,
3912 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00003913
Duncan Sands481dc722007-12-19 07:36:31 +00003914 // Try to merge with the previous call-site.
3915 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00003916 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00003917 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3918 // Extend the range of the previous entry.
3919 Prev.EndLabel = Site.EndLabel;
3920 continue;
3921 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003922 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003923
Duncan Sands481dc722007-12-19 07:36:31 +00003924 // Otherwise, create a new call-site.
3925 CallSites.push_back(Site);
3926 PreviousIsInvoke = true;
3927 } else {
3928 // Create a gap.
3929 PreviousIsInvoke = false;
3930 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003931 }
3932 }
3933 // If some instruction between the previous try-range and the end of the
3934 // function may throw, create a call-site entry with no landing pad for the
3935 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00003936 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003937 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3938 CallSites.push_back(Site);
3939 }
3940
Jim Laskeybacd3042007-02-21 22:48:45 +00003941 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00003942
3943 // Call sites.
3944 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
3945 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
3946 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
3947 unsigned SizeSites = CallSites.size() * (SiteStartSize +
3948 SiteLengthSize +
3949 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00003950 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003951 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00003952
Duncan Sands671fa972008-05-07 19:11:09 +00003953 // Type infos.
3954 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3955 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003956
3957 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003958 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003959 SizeSites + SizeActions + SizeTypes;
3960
3961 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3962 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003963 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003964 TypeOffset;
3965
3966 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003967
Jim Laskeybacd3042007-02-21 22:48:45 +00003968 // Begin the exception table.
3969 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng05548eb2008-02-29 19:36:59 +00003970 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesend65b2642008-10-08 21:50:21 +00003971 O << "GCC_except_table" << SubprogramCount << ":\n";
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003972 for (unsigned i = 0; i != SizeAlign; ++i) {
3973 Asm->EmitInt8(0);
3974 Asm->EOL("Padding");
3975 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003976 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003977
Jim Laskeybacd3042007-02-21 22:48:45 +00003978 // Emit the header.
3979 Asm->EmitInt8(DW_EH_PE_omit);
3980 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3981 Asm->EmitInt8(DW_EH_PE_absptr);
3982 Asm->EOL("TType format (DW_EH_PE_absptr)");
3983 Asm->EmitULEB128Bytes(TypeOffset);
3984 Asm->EOL("TType base offset");
3985 Asm->EmitInt8(DW_EH_PE_udata4);
3986 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3987 Asm->EmitULEB128Bytes(SizeSites);
3988 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00003989
Duncan Sands57810cd2007-09-05 11:27:52 +00003990 // Emit the landing pad site information.
3991 for (unsigned i = 0; i < CallSites.size(); ++i) {
3992 CallSiteEntry &S = CallSites[i];
3993 const char *BeginTag;
3994 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00003995
Duncan Sands57810cd2007-09-05 11:27:52 +00003996 if (!S.BeginLabel) {
3997 BeginTag = "eh_func_begin";
3998 BeginNumber = SubprogramCount;
3999 } else {
4000 BeginTag = "label";
4001 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00004002 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004003
Duncan Sands57810cd2007-09-05 11:27:52 +00004004 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004005 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004006 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00004007
Duncan Sands57810cd2007-09-05 11:27:52 +00004008 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00004009 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00004010 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004011 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00004012 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004013 }
4014 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004015
Duncan Sands671fa972008-05-07 19:11:09 +00004016 if (!S.PadLabel)
4017 Asm->EmitInt32(0);
4018 else
Duncan Sands57810cd2007-09-05 11:27:52 +00004019 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004020 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004021 Asm->EOL("Landing pad");
4022
4023 Asm->EmitULEB128Bytes(S.Action);
4024 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004025 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004026
Jim Laskeybacd3042007-02-21 22:48:45 +00004027 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004028 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4029 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00004030
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004031 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004032 Asm->EOL("TypeInfo index");
4033 Asm->EmitSLEB128Bytes(Action.NextAction);
4034 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004035 }
4036
4037 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00004038 for (unsigned M = TypeInfos.size(); M; --M) {
4039 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00004040
4041 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00004042
4043 if (GV)
4044 O << Asm->getGlobalLinkName(GV);
4045 else
4046 O << "0";
Duncan Sands57810cd2007-09-05 11:27:52 +00004047
Jim Laskeybacd3042007-02-21 22:48:45 +00004048 Asm->EOL("TypeInfo");
4049 }
4050
Duncan Sands73ef58a2007-06-02 16:53:42 +00004051 // Emit the filter typeids.
4052 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4053 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00004054 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00004055 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00004056 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004057
Evan Cheng05548eb2008-02-29 19:36:59 +00004058 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004059 }
4060
Jim Laskey072200c2007-01-29 18:51:14 +00004061public:
4062 //===--------------------------------------------------------------------===//
4063 // Main entry points.
4064 //
Owen Andersoncb371882008-08-21 00:14:44 +00004065 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00004066 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
4067 shouldEmitTableModule(false), shouldEmitMovesModule(false),
4068 ExceptionTimer(0) {
4069 if (TimePassesIsEnabled)
4070 ExceptionTimer = new Timer("Dwarf Exception Writer",
4071 *getDwarfTimerGroup());
4072 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004073
Bill Wendling163ba3f2009-03-10 21:23:25 +00004074 virtual ~DwarfException() {
4075 delete ExceptionTimer;
4076 }
Jim Laskey072200c2007-01-29 18:51:14 +00004077
4078 /// SetModuleInfo - Set machine module information when it's known that pass
4079 /// manager has created it. Set by the target AsmPrinter.
4080 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00004081 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00004082 }
4083
4084 /// BeginModule - Emit all exception information that should come prior to the
4085 /// content.
4086 void BeginModule(Module *M) {
4087 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00004088 }
4089
4090 /// EndModule - Emit all exception information that should come after the
4091 /// content.
4092 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004093 if (TimePassesIsEnabled)
4094 ExceptionTimer->startTimer();
4095
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004096 if (shouldEmitMovesModule || shouldEmitTableModule) {
4097 const std::vector<Function *> Personalities = MMI->getPersonalities();
Evan Chenge3d42322009-02-25 07:04:34 +00004098 for (unsigned i = 0; i < Personalities.size(); ++i)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004099 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00004100
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004101 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4102 E = EHFrames.end(); I != E; ++I)
4103 EmitEHFrame(*I);
4104 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004105
4106 if (TimePassesIsEnabled)
4107 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004108 }
4109
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004110 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00004111 /// emitted immediately after the function entry point.
4112 void BeginFunction(MachineFunction *MF) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004113 if (TimePassesIsEnabled)
4114 ExceptionTimer->startTimer();
4115
Jim Laskey072200c2007-01-29 18:51:14 +00004116 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004117 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004118
Bill Wendling163ba3f2009-03-10 21:23:25 +00004119 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004120 // Map all labels and get rid of any dead landing pads.
4121 MMI->TidyLandingPads();
Bill Wendling163ba3f2009-03-10 21:23:25 +00004122
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004123 // If any landing pads survive, we need an EH table.
4124 if (MMI->getLandingPads().size())
4125 shouldEmitTable = true;
4126
4127 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00004128 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004129 shouldEmitMoves = true;
4130
4131 if (shouldEmitMoves || shouldEmitTable)
4132 // Assumes in correct section after the entry point.
4133 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00004134 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004135
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004136 shouldEmitTableModule |= shouldEmitTable;
4137 shouldEmitMovesModule |= shouldEmitMoves;
Bill Wendling163ba3f2009-03-10 21:23:25 +00004138
4139 if (TimePassesIsEnabled)
4140 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004141 }
4142
4143 /// EndFunction - Gather and emit post-function exception information.
4144 ///
4145 void EndFunction() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004146 if (TimePassesIsEnabled)
4147 ExceptionTimer->startTimer();
4148
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004149 if (shouldEmitMoves || shouldEmitTable) {
4150 EmitLabel("eh_func_end", SubprogramCount);
4151 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00004152
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004153 // Save EH frame information
4154 EHFrames.
4155 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendling163ba3f2009-03-10 21:23:25 +00004156 SubprogramCount,
4157 MMI->getPersonalityIndex(),
4158 MF->getFrameInfo()->hasCalls(),
4159 !MMI->getLandingPads().empty(),
4160 MMI->getFrameMoves(),
4161 MF->getFunction()));
4162 }
4163
4164 if (TimePassesIsEnabled)
4165 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004166 }
4167};
4168
Jim Laskey0d086af2006-02-27 12:43:29 +00004169} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00004170
4171//===----------------------------------------------------------------------===//
4172
Jim Laskeyd18e2892006-01-20 20:34:06 +00004173/// Emit - Print the abbreviation using the specified Dwarf writer.
4174///
Jim Laskey072200c2007-01-29 18:51:14 +00004175void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004176 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00004177 DD.getAsm()->EmitULEB128Bytes(Tag);
4178 DD.getAsm()->EOL(TagString(Tag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004179
Jim Laskeyd18e2892006-01-20 20:34:06 +00004180 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00004181 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4182 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004183
Jim Laskeyd18e2892006-01-20 20:34:06 +00004184 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00004185 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004186 const DIEAbbrevData &AttrData = Data[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004187
Jim Laskeyd18e2892006-01-20 20:34:06 +00004188 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00004189 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4190 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004191
Jim Laskeyd18e2892006-01-20 20:34:06 +00004192 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00004193 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4194 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00004195 }
4196
4197 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00004198 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4199 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00004200}
4201
4202#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00004203void DIEAbbrev::print(std::ostream &O) {
4204 O << "Abbreviation @"
4205 << std::hex << (intptr_t)this << std::dec
4206 << " "
4207 << TagString(Tag)
4208 << " "
4209 << ChildrenString(ChildrenFlag)
4210 << "\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004211
Jim Laskeya0f3d172006-09-07 22:06:40 +00004212 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4213 O << " "
4214 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004215 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00004216 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004217 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00004218 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00004219}
Bill Wendlinge8156192006-12-07 01:30:32 +00004220void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00004221#endif
4222
4223//===----------------------------------------------------------------------===//
4224
Jim Laskeyef42a012006-11-02 20:12:39 +00004225#ifndef NDEBUG
4226void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004227 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004228}
Jim Laskeyef42a012006-11-02 20:12:39 +00004229#endif
4230
4231//===----------------------------------------------------------------------===//
4232
Jim Laskey063e7652006-01-17 17:31:53 +00004233/// EmitValue - Emit integer of appropriate size.
4234///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004235void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00004236 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00004237 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00004238 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004239 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004240 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004241 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004242 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004243 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004244 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004245 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4246 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4247 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004248 default: assert(0 && "DIE Value form not supported yet"); break;
4249 }
4250}
4251
4252/// SizeOf - Determine size of integer value in bytes.
4253///
Jim Laskey072200c2007-01-29 18:51:14 +00004254unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004255 switch (Form) {
4256 case DW_FORM_flag: // Fall thru
4257 case DW_FORM_ref1: // Fall thru
4258 case DW_FORM_data1: return sizeof(int8_t);
4259 case DW_FORM_ref2: // Fall thru
4260 case DW_FORM_data2: return sizeof(int16_t);
4261 case DW_FORM_ref4: // Fall thru
4262 case DW_FORM_data4: return sizeof(int32_t);
4263 case DW_FORM_ref8: // Fall thru
4264 case DW_FORM_data8: return sizeof(int64_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004265 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4266 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00004267 default: assert(0 && "DIE Value form not supported yet"); break;
4268 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004269 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00004270}
4271
Jim Laskey063e7652006-01-17 17:31:53 +00004272//===----------------------------------------------------------------------===//
4273
4274/// EmitValue - Emit string value.
4275///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004276void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004277 DD.getAsm()->EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00004278}
4279
Jim Laskey063e7652006-01-17 17:31:53 +00004280//===----------------------------------------------------------------------===//
4281
4282/// EmitValue - Emit label value.
4283///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004284void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004285 bool IsSmall = Form == DW_FORM_data4;
4286 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004287}
4288
4289/// SizeOf - Determine size of label value in bytes.
4290///
Jim Laskey072200c2007-01-29 18:51:14 +00004291unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004292 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004293 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004294}
Jim Laskeyef42a012006-11-02 20:12:39 +00004295
Jim Laskey063e7652006-01-17 17:31:53 +00004296//===----------------------------------------------------------------------===//
4297
Jim Laskeyd18e2892006-01-20 20:34:06 +00004298/// EmitValue - Emit label value.
4299///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004300void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004301 bool IsSmall = Form == DW_FORM_data4;
4302 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00004303}
4304
4305/// SizeOf - Determine size of label value in bytes.
4306///
Jim Laskey072200c2007-01-29 18:51:14 +00004307unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004308 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004309 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00004310}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004311
Jim Laskeyd18e2892006-01-20 20:34:06 +00004312//===----------------------------------------------------------------------===//
4313
Jim Laskey063e7652006-01-17 17:31:53 +00004314/// EmitValue - Emit delta value.
4315///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004316void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4317 bool IsSmall = Form == DW_FORM_data4;
4318 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4319 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4320}
4321
4322/// SizeOf - Determine size of delta value in bytes.
4323///
4324unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4325 if (Form == DW_FORM_data4) return 4;
4326 return DD.getTargetData()->getPointerSize();
4327}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004328
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004329//===----------------------------------------------------------------------===//
4330
4331/// EmitValue - Emit delta value.
4332///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004333void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004334 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00004335 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004336}
4337
4338/// SizeOf - Determine size of delta value in bytes.
4339///
Jim Laskey072200c2007-01-29 18:51:14 +00004340unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004341 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004342 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004343}
4344
4345//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004346
Jim Laskeyb8509c52006-03-23 18:07:55 +00004347/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004348///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004349void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004350 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00004351}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004352
Jim Laskeyd18e2892006-01-20 20:34:06 +00004353//===----------------------------------------------------------------------===//
4354
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004355/// ComputeSize - calculate the size of the block.
4356///
Jim Laskey072200c2007-01-29 18:51:14 +00004357unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00004358 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00004359 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004360
Jim Laskeyef42a012006-11-02 20:12:39 +00004361 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00004362 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00004363 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004364 }
4365 return Size;
4366}
4367
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004368/// EmitValue - Emit block data.
4369///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004370void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004371 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004372 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4373 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4374 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4375 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004376 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004377 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004378
Owen Anderson873e1b52008-06-24 21:44:59 +00004379 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00004380
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004381 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00004382 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00004383 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004384 }
4385}
4386
4387/// SizeOf - Determine size of block data in bytes.
4388///
Jim Laskey072200c2007-01-29 18:51:14 +00004389unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004390 switch (Form) {
4391 case DW_FORM_block1: return Size + sizeof(int8_t);
4392 case DW_FORM_block2: return Size + sizeof(int16_t);
4393 case DW_FORM_block4: return Size + sizeof(int32_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004394 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004395 default: assert(0 && "Improper form for block"); break;
4396 }
4397 return 0;
4398}
4399
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004400//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004401/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00004402
4403DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00004404 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00004405 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00004406}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004407
Jim Laskeyb8509c52006-03-23 18:07:55 +00004408/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4409///
4410void DIE::AddSiblingOffset() {
4411 DIEInteger *DI = new DIEInteger(0);
4412 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00004413 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00004414}
4415
Jim Laskeyef42a012006-11-02 20:12:39 +00004416/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004417///
Jim Laskeyef42a012006-11-02 20:12:39 +00004418void DIE::Profile(FoldingSetNodeID &ID) {
4419 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004420
Jim Laskeyef42a012006-11-02 20:12:39 +00004421 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4422 ID.AddPointer(Children[i]);
4423
4424 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4425 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00004426}
Jim Laskeyef42a012006-11-02 20:12:39 +00004427
4428#ifndef NDEBUG
4429void DIE::print(std::ostream &O, unsigned IncIndent) {
4430 static unsigned IndentCount = 0;
4431 IndentCount += IncIndent;
4432 const std::string Indent(IndentCount, ' ');
4433 bool isBlock = Abbrev.getTag() == 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004434
Jim Laskeyef42a012006-11-02 20:12:39 +00004435 if (!isBlock) {
4436 O << Indent
4437 << "Die: "
4438 << "0x" << std::hex << (intptr_t)this << std::dec
4439 << ", Offset: " << Offset
4440 << ", Size: " << Size
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004441 << "\n";
4442
Jim Laskeyef42a012006-11-02 20:12:39 +00004443 O << Indent
4444 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00004445 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00004446 << ChildrenString(Abbrev.getChildrenFlag());
4447 } else {
4448 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00004449 }
4450 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00004451
Owen Anderson873e1b52008-06-24 21:44:59 +00004452 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004453
Jim Laskeyef42a012006-11-02 20:12:39 +00004454 IndentCount += 2;
4455 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4456 O << Indent;
Bill Wendling4e974012008-07-22 00:28:47 +00004457
4458 if (!isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +00004459 O << AttributeString(Data[i].getAttribute());
Bill Wendling4e974012008-07-22 00:28:47 +00004460 else
Jim Laskeyef42a012006-11-02 20:12:39 +00004461 O << "Blk[" << i << "]";
Bill Wendling4e974012008-07-22 00:28:47 +00004462
Jim Laskeyef42a012006-11-02 20:12:39 +00004463 O << " "
4464 << FormEncodingString(Data[i].getForm())
4465 << " ";
4466 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00004467 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00004468 }
Jim Laskeyef42a012006-11-02 20:12:39 +00004469 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00004470
Jim Laskeyef42a012006-11-02 20:12:39 +00004471 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4472 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00004473 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004474
Jim Laskeyef42a012006-11-02 20:12:39 +00004475 if (!isBlock) O << "\n";
4476 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00004477}
4478
Jim Laskeyef42a012006-11-02 20:12:39 +00004479void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004480 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00004481}
Jim Laskeybd761842006-02-27 17:27:12 +00004482#endif
Jim Laskey65195462006-10-30 13:35:07 +00004483
4484//===----------------------------------------------------------------------===//
4485/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00004486///
Jim Laskey65195462006-10-30 13:35:07 +00004487
Bill Wendling91b8b802009-03-10 20:41:52 +00004488DwarfWriter::DwarfWriter()
Bill Wendling163ba3f2009-03-10 21:23:25 +00004489 : ImmutablePass(&ID), DD(0), DE(0) {}
Jim Laskey65195462006-10-30 13:35:07 +00004490
4491DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00004492 delete DE;
4493 delete DD;
Bill Wendling91b8b802009-03-10 20:41:52 +00004494 delete DwarfTimerGroup; DwarfTimerGroup = 0;
Jim Laskey65195462006-10-30 13:35:07 +00004495}
4496
Jim Laskey65195462006-10-30 13:35:07 +00004497/// BeginModule - Emit all Dwarf sections that should come prior to the
4498/// content.
Devang Pateleb3fc282009-01-08 23:40:34 +00004499void DwarfWriter::BeginModule(Module *M,
4500 MachineModuleInfo *MMI,
4501 raw_ostream &OS, AsmPrinter *A,
4502 const TargetAsmInfo *T) {
4503 DE = new DwarfException(OS, A, T);
4504 DD = new DwarfDebug(OS, A, T);
Jim Laskey072200c2007-01-29 18:51:14 +00004505 DE->BeginModule(M);
4506 DD->BeginModule(M);
Devang Patel7bb89ed2009-01-13 00:20:51 +00004507 DD->SetDebugInfo(MMI);
Devang Pateleb3fc282009-01-08 23:40:34 +00004508 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00004509}
4510
4511/// EndModule - Emit all Dwarf sections that should come after the content.
4512///
4513void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00004514 DE->EndModule();
4515 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00004516}
4517
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004518/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00004519/// emitted immediately after the function entry point.
4520void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00004521 DE->BeginFunction(MF);
4522 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00004523}
4524
4525/// EndFunction - Gather and emit post-function debug information.
4526///
Bill Wendlingd751c642008-09-26 00:28:12 +00004527void DwarfWriter::EndFunction(MachineFunction *MF) {
4528 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004529 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004530
Bill Wendlingc91d0b92008-07-22 00:53:37 +00004531 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00004532 // Clear function debug information.
4533 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00004534}
Devang Patelccca7fe2009-01-12 19:17:34 +00004535
Devang Patelcf3a4482009-01-15 23:41:32 +00004536/// ValidDebugInfo - Return true if V represents valid debug info value.
4537bool DwarfWriter::ValidDebugInfo(Value *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004538 return DD && DD->ValidDebugInfo(V);
Devang Patelcf3a4482009-01-15 23:41:32 +00004539}
4540
Devang Patelccca7fe2009-01-12 19:17:34 +00004541/// RecordSourceLine - Records location information and associates it with a
4542/// label. Returns a unique label ID used to generate a label and provide
4543/// correspondence to the source line list.
4544unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
4545 unsigned Src) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004546 return DD->RecordSourceLine(Line, Col, Src);
Devang Patelccca7fe2009-01-12 19:17:34 +00004547}
4548
Evan Chenge3d42322009-02-25 07:04:34 +00004549/// getOrCreateSourceID - Look up the source id with the given directory and
4550/// source file names. If none currently exists, create a new id and insert it
4551/// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
4552/// as well.
4553unsigned DwarfWriter::getOrCreateSourceID(const std::string &DirName,
4554 const std::string &FileName) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004555 return DD->getOrCreateSourceID(DirName, FileName);
Devang Patelccca7fe2009-01-12 19:17:34 +00004556}
4557
4558/// RecordRegionStart - Indicate the start of a region.
4559unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004560 return DD->RecordRegionStart(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004561}
4562
4563/// RecordRegionEnd - Indicate the end of a region.
4564unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004565 return DD->RecordRegionEnd(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004566}
4567
4568/// getRecordSourceLineCount - Count source lines.
4569unsigned DwarfWriter::getRecordSourceLineCount() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004570 return DD->getRecordSourceLineCount();
Devang Patelccca7fe2009-01-12 19:17:34 +00004571}
Devang Patelbb8c5952009-01-13 21:25:00 +00004572
Devang Patelc48c5502009-01-13 21:44:10 +00004573/// RecordVariable - Indicate the declaration of a local variable.
4574///
4575void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
4576 DD->RecordVariable(GV, FrameIndex);
4577}
Devang Patelbbdc8202009-01-13 23:54:55 +00004578
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004579/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
4580/// be emitted.
4581bool DwarfWriter::ShouldEmitDwarfDebug() const {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004582 return DD->ShouldEmitDwarfDebug();
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004583}