blob: 8ccd38072c6332c6dd65a2bff1d38647701bc0c4 [file] [log] [blame]
Jim Laskeye5032892005-12-21 19:48:16 +00001//===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Jim Laskeye5032892005-12-21 19:48:16 +00007//
8//===----------------------------------------------------------------------===//
9//
Jim Laskey072200c2007-01-29 18:51:14 +000010// This file contains support for writing dwarf info into asm files.
Jim Laskeye5032892005-12-21 19:48:16 +000011//
12//===----------------------------------------------------------------------===//
Jim Laskey3ea0e0e2006-01-27 18:32:41 +000013
Jim Laskeyb2efb852006-01-04 22:28:25 +000014#include "llvm/CodeGen/DwarfWriter.h"
Jim Laskey52060a02006-01-24 00:49:18 +000015#include "llvm/Module.h"
Devang Pateld1ca9252009-01-05 23:03:32 +000016#include "llvm/DerivedTypes.h"
Devang Patelcf3a4482009-01-15 23:41:32 +000017#include "llvm/Constants.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000018#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskey44c3b9f2007-01-26 21:22:28 +000019#include "llvm/CodeGen/MachineModuleInfo.h"
Jim Laskey41886992006-04-07 16:34:46 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000021#include "llvm/CodeGen/MachineLocation.h"
Devang Patel08f053f2009-01-05 17:57:47 +000022#include "llvm/Analysis/DebugInfo.h"
Jim Laskey3f09fc22007-02-28 18:38:31 +000023#include "llvm/Support/Debug.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000024#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000025#include "llvm/Support/CommandLine.h"
Jim Laskey65195462006-10-30 13:35:07 +000026#include "llvm/Support/DataTypes.h"
Jim Laskey52060a02006-01-24 00:49:18 +000027#include "llvm/Support/Mangler.h"
Bill Wendling91b8b802009-03-10 20:41:52 +000028#include "llvm/Support/Timer.h"
Owen Andersoncb371882008-08-21 00:14:44 +000029#include "llvm/Support/raw_ostream.h"
Dan Gohman85496362007-09-24 21:32:18 +000030#include "llvm/System/Path.h"
Jim Laskey563321a2006-09-06 18:34:40 +000031#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000032#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000033#include "llvm/Target/TargetData.h"
Jim Laskey1069fbd2006-04-10 23:09:19 +000034#include "llvm/Target/TargetFrameInfo.h"
Duncan Sands53c3a332007-05-16 12:12:23 +000035#include "llvm/Target/TargetInstrInfo.h"
Jim Laskey1b340dc2007-01-29 20:48:32 +000036#include "llvm/Target/TargetMachine.h"
Jim Laskeyc1c47c32007-01-29 23:40:33 +000037#include "llvm/Target/TargetOptions.h"
Evan Chenge3d42322009-02-25 07:04:34 +000038#include "llvm/ADT/DenseMap.h"
39#include "llvm/ADT/FoldingSet.h"
40#include "llvm/ADT/StringExtras.h"
41#include "llvm/ADT/StringMap.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000042#include <ostream>
Jim Laskey65195462006-10-30 13:35:07 +000043#include <string>
Jim Laskeyb2efb852006-01-04 22:28:25 +000044using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000045using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000046
Devang Pateleb3fc282009-01-08 23:40:34 +000047static RegisterPass<DwarfWriter>
48X("dwarfwriter", "DWARF Information Writer");
49char DwarfWriter::ID = 0;
50
Bill Wendling68edf5f2009-03-10 22:58:53 +000051static TimerGroup &getDwarfTimerGroup() {
52 static TimerGroup DwarfTimerGroup("Dwarf Exception and Debugging");
53 return DwarfTimerGroup;
Bill Wendling91b8b802009-03-10 20:41:52 +000054}
55
Jim Laskey0d086af2006-02-27 12:43:29 +000056namespace llvm {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000057
Jim Laskey65195462006-10-30 13:35:07 +000058//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +000059
60/// Configuration values for initial hash set sizes (log2).
61///
Bill Wendlingb9dcef22009-02-03 21:17:20 +000062static const unsigned InitDiesSetSize = 9; // log2(512)
63static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
64static const unsigned InitValuesSetSize = 9; // log2(512)
Jim Laskeyef42a012006-11-02 20:12:39 +000065
66//===----------------------------------------------------------------------===//
67/// Forward declarations.
68///
69class DIE;
70class DIEValue;
71
72//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000073/// DWLabel - Labels are used to track locations in the assembler file.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000074/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
75/// where the tag is a category of label (Ex. location) and number is a value
Reid Spencer181b6c92007-08-05 20:06:04 +000076/// unique in that category.
Jim Laskey65195462006-10-30 13:35:07 +000077class DWLabel {
78public:
Jim Laskeyef42a012006-11-02 20:12:39 +000079 /// Tag - Label category tag. Should always be a staticly declared C string.
80 ///
81 const char *Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000082
Jim Laskeyef42a012006-11-02 20:12:39 +000083 /// Number - Value to make label unique.
84 ///
85 unsigned Number;
Jim Laskey65195462006-10-30 13:35:07 +000086
87 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000088
Jim Laskeyef42a012006-11-02 20:12:39 +000089 void Profile(FoldingSetNodeID &ID) const {
Evan Chenge3d42322009-02-25 07:04:34 +000090 ID.AddString(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +000091 ID.AddInteger(Number);
Jim Laskey90c79d72006-03-23 23:02:34 +000092 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000093
Jim Laskeyef42a012006-11-02 20:12:39 +000094#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +000095 void print(std::ostream *O) const {
96 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +000097 }
Jim Laskeyef42a012006-11-02 20:12:39 +000098 void print(std::ostream &O) const {
Jim Laskeybacd3042007-02-21 22:48:45 +000099 O << "." << Tag;
Jim Laskeyef42a012006-11-02 20:12:39 +0000100 if (Number) O << Number;
101 }
102#endif
Jim Laskeybd761842006-02-27 17:27:12 +0000103};
104
105//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000106/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
107/// Dwarf abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +0000108class DIEAbbrevData {
Jim Laskeyef42a012006-11-02 20:12:39 +0000109 /// Attribute - Dwarf attribute code.
110 ///
111 unsigned Attribute;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000112
Jim Laskeyef42a012006-11-02 20:12:39 +0000113 /// Form - Dwarf form code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000114 ///
115 unsigned Form;
Jim Laskey0d086af2006-02-27 12:43:29 +0000116public:
Bill Wendlingdd446322009-03-10 23:57:09 +0000117 DIEAbbrevData(unsigned A, unsigned F) : Attribute(A), Form(F) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000118
Jim Laskeybd761842006-02-27 17:27:12 +0000119 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000120 unsigned getAttribute() const { return Attribute; }
121 unsigned getForm() const { return Form; }
Jim Laskey063e7652006-01-17 17:31:53 +0000122
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000123 /// Profile - Used to gather unique data for the abbreviation folding set.
Jim Laskey0d086af2006-02-27 12:43:29 +0000124 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000125 void Profile(FoldingSetNodeID &ID)const {
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000126 ID.AddInteger(Attribute);
127 ID.AddInteger(Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000128 }
129};
Jim Laskey063e7652006-01-17 17:31:53 +0000130
Jim Laskey0d086af2006-02-27 12:43:29 +0000131//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000132/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
133/// information object.
134class DIEAbbrev : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000135private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000136 /// Tag - Dwarf tag code.
137 ///
138 unsigned Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000139
Jim Laskeyef42a012006-11-02 20:12:39 +0000140 /// Unique number for node.
141 ///
142 unsigned Number;
143
144 /// ChildrenFlag - Dwarf children flag.
145 ///
146 unsigned ChildrenFlag;
147
148 /// Data - Raw data bytes for abbreviation.
149 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000150 SmallVector<DIEAbbrevData, 8> Data;
Jim Laskey0d086af2006-02-27 12:43:29 +0000151public:
Bill Wendlingdd446322009-03-10 23:57:09 +0000152 DIEAbbrev(unsigned T, unsigned C) : Tag(T), ChildrenFlag(C), Data() {}
153 virtual ~DIEAbbrev() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000154
Jim Laskeybd761842006-02-27 17:27:12 +0000155 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000156 unsigned getTag() const { return Tag; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000157 unsigned getNumber() const { return Number; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000158 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000159 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000160 void setTag(unsigned T) { Tag = T; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000161 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000162 void setNumber(unsigned N) { Number = N; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000163
Jim Laskey0d086af2006-02-27 12:43:29 +0000164 /// AddAttribute - Adds another set of attribute information to the
165 /// abbreviation.
166 void AddAttribute(unsigned Attribute, unsigned Form) {
167 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000168 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000169
Jim Laskeyb8509c52006-03-23 18:07:55 +0000170 /// AddFirstAttribute - Adds a set of attribute information to the front
171 /// of the abbreviation.
172 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
173 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
174 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000175
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000176 /// Profile - Used to gather unique data for the abbreviation folding set.
177 ///
178 void Profile(FoldingSetNodeID &ID) {
179 ID.AddInteger(Tag);
180 ID.AddInteger(ChildrenFlag);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000181
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000182 // For each attribute description.
183 for (unsigned i = 0, N = Data.size(); i < N; ++i)
184 Data[i].Profile(ID);
185 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000186
Jim Laskey0d086af2006-02-27 12:43:29 +0000187 /// Emit - Print the abbreviation using the specified Dwarf writer.
188 ///
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000189 void Emit(const DwarfDebug &DD) const;
190
Jim Laskey0d086af2006-02-27 12:43:29 +0000191#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000192 void print(std::ostream *O) {
193 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000194 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000195 void print(std::ostream &O);
196 void dump();
197#endif
198};
Jim Laskey063e7652006-01-17 17:31:53 +0000199
Jim Laskey0d086af2006-02-27 12:43:29 +0000200//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000201/// DIE - A structured debug information entry. Has an abbreviation which
202/// describes it's organization.
203class DIE : public FoldingSetNode {
204protected:
205 /// Abbrev - Buffer for constructing abbreviation.
206 ///
207 DIEAbbrev Abbrev;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000208
Jim Laskeyef42a012006-11-02 20:12:39 +0000209 /// Offset - Offset in debug info section.
210 ///
211 unsigned Offset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000212
Jim Laskeyef42a012006-11-02 20:12:39 +0000213 /// Size - Size of instance + children.
214 ///
215 unsigned Size;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000216
Jim Laskeyef42a012006-11-02 20:12:39 +0000217 /// Children DIEs.
218 ///
219 std::vector<DIE *> Children;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000220
Jim Laskeyef42a012006-11-02 20:12:39 +0000221 /// Attributes values.
222 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000223 SmallVector<DIEValue*, 32> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000224
Jim Laskeyef42a012006-11-02 20:12:39 +0000225public:
Dan Gohman81975f62007-08-27 14:50:10 +0000226 explicit DIE(unsigned Tag)
Bill Wendlingdd446322009-03-10 23:57:09 +0000227 : Abbrev(Tag, DW_CHILDREN_no), Offset(0), Size(0), Children(), Values() {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000228 virtual ~DIE();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000229
Jim Laskeyef42a012006-11-02 20:12:39 +0000230 // Accessors.
231 DIEAbbrev &getAbbrev() { return Abbrev; }
232 unsigned getAbbrevNumber() const {
233 return Abbrev.getNumber();
234 }
Jim Laskey85f419b2006-11-09 16:32:26 +0000235 unsigned getTag() const { return Abbrev.getTag(); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000236 unsigned getOffset() const { return Offset; }
237 unsigned getSize() const { return Size; }
238 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000239 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000240 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
241 void setOffset(unsigned O) { Offset = O; }
242 void setSize(unsigned S) { Size = S; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000243
Jim Laskeyef42a012006-11-02 20:12:39 +0000244 /// AddValue - Add a value and attributes to a DIE.
245 ///
246 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
247 Abbrev.AddAttribute(Attribute, Form);
248 Values.push_back(Value);
249 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000250
Jim Laskeyef42a012006-11-02 20:12:39 +0000251 /// SiblingOffset - Return the offset of the debug information entry's
252 /// sibling.
253 unsigned SiblingOffset() const { return Offset + Size; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000254
Jim Laskeyef42a012006-11-02 20:12:39 +0000255 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
256 ///
257 void AddSiblingOffset();
258
259 /// AddChild - Add a child to the DIE.
260 ///
261 void AddChild(DIE *Child) {
262 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
263 Children.push_back(Child);
264 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000265
Jim Laskeyef42a012006-11-02 20:12:39 +0000266 /// Detach - Detaches objects connected to it after copying.
267 ///
268 void Detach() {
269 Children.clear();
270 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000271
Jim Laskeyef42a012006-11-02 20:12:39 +0000272 /// Profile - Used to gather unique data for the value folding set.
273 ///
274 void Profile(FoldingSetNodeID &ID) ;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000275
Jim Laskeyef42a012006-11-02 20:12:39 +0000276#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000277 void print(std::ostream *O, unsigned IncIndent = 0) {
278 if (O) print(*O, IncIndent);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000279 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000280 void print(std::ostream &O, unsigned IncIndent = 0);
281 void dump();
282#endif
283};
284
285//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000286/// DIEValue - A debug information entry value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000287///
288class DIEValue : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000289public:
290 enum {
291 isInteger,
292 isString,
293 isLabel,
294 isAsIsLabel,
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000295 isSectionOffset,
Jim Laskey0d086af2006-02-27 12:43:29 +0000296 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000297 isEntry,
298 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000299 };
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000300
Jim Laskeyef42a012006-11-02 20:12:39 +0000301 /// Type - Type of data stored in the value.
302 ///
303 unsigned Type;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000304
Bill Wendlingdd446322009-03-10 23:57:09 +0000305 explicit DIEValue(unsigned T) : Type(T) {}
Jim Laskey0d086af2006-02-27 12:43:29 +0000306 virtual ~DIEValue() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000307
Jim Laskeyf6733882006-11-02 21:48:18 +0000308 // Accessors
Jim Laskeyef42a012006-11-02 20:12:39 +0000309 unsigned getType() const { return Type; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000310
Jim Laskey0d086af2006-02-27 12:43:29 +0000311 // Implement isa/cast/dyncast.
312 static bool classof(const DIEValue *) { return true; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000313
Jim Laskey0d086af2006-02-27 12:43:29 +0000314 /// EmitValue - Emit value via the Dwarf writer.
315 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000316 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000317
Jim Laskey0d086af2006-02-27 12:43:29 +0000318 /// SizeOf - Return the size of a value in bytes.
319 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000320 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000321
Jim Laskeyef42a012006-11-02 20:12:39 +0000322 /// Profile - Used to gather unique data for the value folding set.
323 ///
324 virtual void Profile(FoldingSetNodeID &ID) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000325
Jim Laskeyef42a012006-11-02 20:12:39 +0000326#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000327 void print(std::ostream *O) {
328 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000329 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000330 virtual void print(std::ostream &O) = 0;
331 void dump();
332#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000333};
Jim Laskey063e7652006-01-17 17:31:53 +0000334
Jim Laskey0d086af2006-02-27 12:43:29 +0000335//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000336/// DWInteger - An integer value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000337///
Jim Laskey0d086af2006-02-27 12:43:29 +0000338class DIEInteger : public DIEValue {
339private:
340 uint64_t Integer;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000341
Jim Laskey0d086af2006-02-27 12:43:29 +0000342public:
Dan Gohman81975f62007-08-27 14:50:10 +0000343 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000344
Jim Laskey0d086af2006-02-27 12:43:29 +0000345 // Implement isa/cast/dyncast.
346 static bool classof(const DIEInteger *) { return true; }
347 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000348
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000349 /// BestForm - Choose the best form for integer.
350 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000351 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
352 if (IsSigned) {
353 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
354 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
355 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
356 } else {
357 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
358 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
359 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
360 }
361 return DW_FORM_data8;
362 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000363
Jim Laskey0d086af2006-02-27 12:43:29 +0000364 /// EmitValue - Emit integer of appropriate size.
365 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000366 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000367
Jim Laskey0d086af2006-02-27 12:43:29 +0000368 /// SizeOf - Determine size of integer value in bytes.
369 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000370 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000371
Jim Laskeyef42a012006-11-02 20:12:39 +0000372 /// Profile - Used to gather unique data for the value folding set.
373 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000374 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000375 ID.AddInteger(isInteger);
Jim Laskeyef42a012006-11-02 20:12:39 +0000376 ID.AddInteger(Integer);
377 }
Jim Laskey5496f012006-11-09 14:52:14 +0000378 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000379
Jim Laskeyef42a012006-11-02 20:12:39 +0000380#ifndef NDEBUG
381 virtual void print(std::ostream &O) {
382 O << "Int: " << (int64_t)Integer
383 << " 0x" << std::hex << Integer << std::dec;
384 }
385#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000386};
Jim Laskey063e7652006-01-17 17:31:53 +0000387
Jim Laskey0d086af2006-02-27 12:43:29 +0000388//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000389/// DIEString - A string value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000390///
Jim Laskeyef42a012006-11-02 20:12:39 +0000391class DIEString : public DIEValue {
Bill Wendlingdd446322009-03-10 23:57:09 +0000392 const std::string Str;
Jim Laskeyef42a012006-11-02 20:12:39 +0000393public:
Bill Wendlingdd446322009-03-10 23:57:09 +0000394 explicit DIEString(const std::string &S) : DIEValue(isString), Str(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000395
Jim Laskey0d086af2006-02-27 12:43:29 +0000396 // Implement isa/cast/dyncast.
397 static bool classof(const DIEString *) { return true; }
398 static bool classof(const DIEValue *S) { return S->Type == isString; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000399
Jim Laskey0d086af2006-02-27 12:43:29 +0000400 /// EmitValue - Emit string value.
401 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000402 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000403
Jim Laskey0d086af2006-02-27 12:43:29 +0000404 /// SizeOf - Determine size of string value in bytes.
405 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000406 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Bill Wendlingdd446322009-03-10 23:57:09 +0000407 return Str.size() + sizeof(char); // sizeof('\0');
Jim Laskeyef42a012006-11-02 20:12:39 +0000408 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000409
Jim Laskeyef42a012006-11-02 20:12:39 +0000410 /// Profile - Used to gather unique data for the value folding set.
411 ///
Bill Wendlingdd446322009-03-10 23:57:09 +0000412 static void Profile(FoldingSetNodeID &ID, const std::string &Str) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000413 ID.AddInteger(isString);
Bill Wendlingdd446322009-03-10 23:57:09 +0000414 ID.AddString(Str);
Jim Laskeyef42a012006-11-02 20:12:39 +0000415 }
Bill Wendlingdd446322009-03-10 23:57:09 +0000416 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Str); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000417
Jim Laskeyef42a012006-11-02 20:12:39 +0000418#ifndef NDEBUG
419 virtual void print(std::ostream &O) {
Bill Wendlingdd446322009-03-10 23:57:09 +0000420 O << "Str: \"" << Str << "\"";
Jim Laskeyef42a012006-11-02 20:12:39 +0000421 }
422#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000423};
Jim Laskey063e7652006-01-17 17:31:53 +0000424
Jim Laskey0d086af2006-02-27 12:43:29 +0000425//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000426/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000427//
Jim Laskeyef42a012006-11-02 20:12:39 +0000428class DIEDwarfLabel : public DIEValue {
Jim Laskey0d086af2006-02-27 12:43:29 +0000429 const DWLabel Label;
Bill Wendlingdd446322009-03-10 23:57:09 +0000430public:
Dan Gohman81975f62007-08-27 14:50:10 +0000431 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000432
Jim Laskey0d086af2006-02-27 12:43:29 +0000433 // Implement isa/cast/dyncast.
434 static bool classof(const DIEDwarfLabel *) { return true; }
435 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000436
Jim Laskey0d086af2006-02-27 12:43:29 +0000437 /// EmitValue - Emit label value.
438 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000439 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000440
Jim Laskey0d086af2006-02-27 12:43:29 +0000441 /// SizeOf - Determine size of label value in bytes.
442 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000443 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000444
Jim Laskeyef42a012006-11-02 20:12:39 +0000445 /// Profile - Used to gather unique data for the value folding set.
446 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000447 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000448 ID.AddInteger(isLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000449 Label.Profile(ID);
450 }
Jim Laskey5496f012006-11-09 14:52:14 +0000451 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000452
Jim Laskeyef42a012006-11-02 20:12:39 +0000453#ifndef NDEBUG
454 virtual void print(std::ostream &O) {
455 O << "Lbl: ";
456 Label.print(O);
457 }
458#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000459};
Jim Laskey063e7652006-01-17 17:31:53 +0000460
Jim Laskey0d086af2006-02-27 12:43:29 +0000461//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000462/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000463//
Jim Laskeyef42a012006-11-02 20:12:39 +0000464class DIEObjectLabel : public DIEValue {
Jim Laskey0d086af2006-02-27 12:43:29 +0000465 const std::string Label;
Bill Wendlingdd446322009-03-10 23:57:09 +0000466public:
Dan Gohman81975f62007-08-27 14:50:10 +0000467 explicit DIEObjectLabel(const std::string &L)
468 : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000469
Jim Laskey0d086af2006-02-27 12:43:29 +0000470 // Implement isa/cast/dyncast.
471 static bool classof(const DIEObjectLabel *) { return true; }
472 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000473
Jim Laskey0d086af2006-02-27 12:43:29 +0000474 /// EmitValue - Emit label value.
475 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000476 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000477
Jim Laskey0d086af2006-02-27 12:43:29 +0000478 /// SizeOf - Determine size of label value in bytes.
479 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000480 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000481
Jim Laskeyef42a012006-11-02 20:12:39 +0000482 /// Profile - Used to gather unique data for the value folding set.
483 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000484 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000485 ID.AddInteger(isAsIsLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000486 ID.AddString(Label);
487 }
Evan Chenge3d42322009-02-25 07:04:34 +0000488 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label.c_str()); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000489
490#ifndef NDEBUG
491 virtual void print(std::ostream &O) {
492 O << "Obj: " << Label;
493 }
494#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000495};
Jim Laskey063e7652006-01-17 17:31:53 +0000496
Jim Laskey0d086af2006-02-27 12:43:29 +0000497//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000498/// DIESectionOffset - A section offset DIE.
499//
500class DIESectionOffset : public DIEValue {
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000501 const DWLabel Label;
502 const DWLabel Section;
503 bool IsEH : 1;
504 bool UseSet : 1;
Bill Wendlingdd446322009-03-10 23:57:09 +0000505public:
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000506 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
507 bool isEH = false, bool useSet = true)
Bill Wendlingdd446322009-03-10 23:57:09 +0000508 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
509 IsEH(isEH), UseSet(useSet) {}
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000510
511 // Implement isa/cast/dyncast.
512 static bool classof(const DIESectionOffset *) { return true; }
513 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000514
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000515 /// EmitValue - Emit section offset.
516 ///
517 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000518
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000519 /// SizeOf - Determine size of section offset value in bytes.
520 ///
521 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000522
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000523 /// Profile - Used to gather unique data for the value folding set.
524 ///
525 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
526 const DWLabel &Section) {
527 ID.AddInteger(isSectionOffset);
528 Label.Profile(ID);
529 Section.Profile(ID);
530 // IsEH and UseSet are specific to the Label/Section that we will emit
531 // the offset for; so Label/Section are enough for uniqueness.
532 }
533 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
534
535#ifndef NDEBUG
536 virtual void print(std::ostream &O) {
537 O << "Off: ";
538 Label.print(O);
539 O << "-";
540 Section.print(O);
541 O << "-" << IsEH << "-" << UseSet;
542 }
543#endif
544};
545
546//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000547/// DIEDelta - A simple label difference DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000548///
Jim Laskeyef42a012006-11-02 20:12:39 +0000549class DIEDelta : public DIEValue {
Jim Laskey0d086af2006-02-27 12:43:29 +0000550 const DWLabel LabelHi;
551 const DWLabel LabelLo;
Bill Wendlingdd446322009-03-10 23:57:09 +0000552public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000553 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
Bill Wendlingdd446322009-03-10 23:57:09 +0000554 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000555
Jim Laskey0d086af2006-02-27 12:43:29 +0000556 // Implement isa/cast/dyncast.
557 static bool classof(const DIEDelta *) { return true; }
558 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000559
Jim Laskey0d086af2006-02-27 12:43:29 +0000560 /// EmitValue - Emit delta value.
561 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000562 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000563
Jim Laskey0d086af2006-02-27 12:43:29 +0000564 /// SizeOf - Determine size of delta value in bytes.
565 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000566 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000567
Jim Laskeyef42a012006-11-02 20:12:39 +0000568 /// Profile - Used to gather unique data for the value folding set.
569 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000570 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
571 const DWLabel &LabelLo) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000572 ID.AddInteger(isDelta);
Jim Laskeyef42a012006-11-02 20:12:39 +0000573 LabelHi.Profile(ID);
574 LabelLo.Profile(ID);
575 }
Jim Laskey5496f012006-11-09 14:52:14 +0000576 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000577
578#ifndef NDEBUG
579 virtual void print(std::ostream &O) {
580 O << "Del: ";
581 LabelHi.print(O);
582 O << "-";
583 LabelLo.print(O);
584 }
585#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000586};
Jim Laskey063e7652006-01-17 17:31:53 +0000587
Jim Laskey0d086af2006-02-27 12:43:29 +0000588//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000589/// DIEntry - A pointer to another debug information entry. An instance of this
590/// class can also be used as a proxy for a debug information entry not yet
591/// defined (ie. types.)
592class DIEntry : public DIEValue {
Jim Laskey0d086af2006-02-27 12:43:29 +0000593 DIE *Entry;
Bill Wendlingdd446322009-03-10 23:57:09 +0000594public:
Dan Gohman81975f62007-08-27 14:50:10 +0000595 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000596
Bill Wendlingdd446322009-03-10 23:57:09 +0000597 void setEntry(DIE *E) { Entry = E; }
598
Jim Laskey0d086af2006-02-27 12:43:29 +0000599 // Implement isa/cast/dyncast.
600 static bool classof(const DIEntry *) { return true; }
601 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000602
Jim Laskeyb8509c52006-03-23 18:07:55 +0000603 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000604 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000605 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000606
Jim Laskeyb8509c52006-03-23 18:07:55 +0000607 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000608 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000609 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000610 return sizeof(int32_t);
611 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000612
Jim Laskeyef42a012006-11-02 20:12:39 +0000613 /// Profile - Used to gather unique data for the value folding set.
614 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000615 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
616 ID.AddInteger(isEntry);
617 ID.AddPointer(Entry);
618 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000619 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000620 ID.AddInteger(isEntry);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000621
Jim Laskeyef42a012006-11-02 20:12:39 +0000622 if (Entry) {
623 ID.AddPointer(Entry);
624 } else {
625 ID.AddPointer(this);
626 }
627 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000628
Jim Laskeyef42a012006-11-02 20:12:39 +0000629#ifndef NDEBUG
630 virtual void print(std::ostream &O) {
631 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
632 }
633#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000634};
635
636//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000637/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000638//
Jim Laskeyef42a012006-11-02 20:12:39 +0000639class DIEBlock : public DIEValue, public DIE {
Bill Wendlingdd446322009-03-10 23:57:09 +0000640 unsigned Size; // Size in bytes excluding size header.
Jim Laskeyef42a012006-11-02 20:12:39 +0000641public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000642 DIEBlock()
Bill Wendlingdd446322009-03-10 23:57:09 +0000643 : DIEValue(isBlock), DIE(0), Size(0) {}
644 virtual ~DIEBlock() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000645
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000646 // Implement isa/cast/dyncast.
647 static bool classof(const DIEBlock *) { return true; }
648 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000649
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000650 /// ComputeSize - calculate the size of the block.
651 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000652 unsigned ComputeSize(DwarfDebug &DD);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000653
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000654 /// BestForm - Choose the best form for data.
655 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000656 unsigned BestForm() const {
657 if ((unsigned char)Size == Size) return DW_FORM_block1;
658 if ((unsigned short)Size == Size) return DW_FORM_block2;
659 if ((unsigned int)Size == Size) return DW_FORM_block4;
660 return DW_FORM_block;
661 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000662
663 /// EmitValue - Emit block data.
664 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000665 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000666
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000667 /// SizeOf - Determine size of block data in bytes.
668 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000669 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000670
Jim Laskeyef42a012006-11-02 20:12:39 +0000671 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000672 ///
Reid Spencer97821312006-11-02 23:56:21 +0000673 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000674 ID.AddInteger(isBlock);
Jim Laskeyef42a012006-11-02 20:12:39 +0000675 DIE::Profile(ID);
676 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000677
Jim Laskeyef42a012006-11-02 20:12:39 +0000678#ifndef NDEBUG
679 virtual void print(std::ostream &O) {
680 O << "Blk: ";
681 DIE::print(O, 5);
682 }
683#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000684};
685
686//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000687/// CompileUnit - This dwarf writer support class manages information associate
688/// with a source file.
689class CompileUnit {
Jim Laskeyef42a012006-11-02 20:12:39 +0000690 /// ID - File identifier for source.
691 ///
692 unsigned ID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000693
Jim Laskeyef42a012006-11-02 20:12:39 +0000694 /// Die - Compile unit debug information entry.
695 ///
696 DIE *Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000697
Devang Patelbbdc8202009-01-13 23:54:55 +0000698 /// GVToDieMap - Tracks the mapping of unit level debug informaton
699 /// variables to debug information entries.
Devang Patelc2997f42009-01-20 00:58:55 +0000700 std::map<GlobalVariable *, DIE *> GVToDieMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000701
Devang Patelbbdc8202009-01-13 23:54:55 +0000702 /// GVToDIEntryMap - Tracks the mapping of unit level debug informaton
Jim Laskeyef42a012006-11-02 20:12:39 +0000703 /// descriptors to debug information entries using a DIEntry proxy.
Devang Patelc2997f42009-01-20 00:58:55 +0000704 std::map<GlobalVariable *, DIEntry *> GVToDIEntryMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000705
706 /// Globals - A map of globally visible named entities for this unit.
707 ///
Bill Wendling972bbac2009-04-09 21:49:15 +0000708 StringMap<DIE*> Globals;
Jim Laskeyef42a012006-11-02 20:12:39 +0000709
710 /// DiesSet - Used to uniquely define dies within the compile unit.
711 ///
712 FoldingSet<DIE> DiesSet;
Jim Laskey0d086af2006-02-27 12:43:29 +0000713public:
Devang Pateld1ca9252009-01-05 23:03:32 +0000714 CompileUnit(unsigned I, DIE *D)
Devang Patelbbdc8202009-01-13 23:54:55 +0000715 : ID(I), Die(D), GVToDieMap(),
Devang Patel7e55b3a2009-01-17 06:51:37 +0000716 GVToDIEntryMap(), Globals(), DiesSet(InitDiesSetSize)
Devang Pateld1ca9252009-01-05 23:03:32 +0000717 {}
718
Jim Laskeyef42a012006-11-02 20:12:39 +0000719 ~CompileUnit() {
720 delete Die;
Jim Laskeyef42a012006-11-02 20:12:39 +0000721 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000722
Jim Laskeybd761842006-02-27 17:27:12 +0000723 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000724 unsigned getID() const { return ID; }
725 DIE* getDie() const { return Die; }
Bill Wendling972bbac2009-04-09 21:49:15 +0000726 StringMap<DIE*> &getGlobals() { return Globals; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000727
728 /// hasContent - Return true if this compile unit has something to write out.
729 ///
730 bool hasContent() const {
731 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000732 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000733
734 /// AddGlobal - Add a new global entity to the compile unit.
735 ///
736 void AddGlobal(const std::string &Name, DIE *Die) {
737 Globals[Name] = Die;
738 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000739
Jim Laskeyef42a012006-11-02 20:12:39 +0000740 /// getDieMapSlotFor - Returns the debug information entry map slot for the
Devang Patelbbdc8202009-01-13 23:54:55 +0000741 /// specified debug variable.
Devang Patel48d190f2009-01-05 21:47:57 +0000742 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
743 return GVToDieMap[GV];
744 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000745
Jim Laskeyef42a012006-11-02 20:12:39 +0000746 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
Devang Patelbbdc8202009-01-13 23:54:55 +0000747 /// specified debug variable.
Devang Patel48d190f2009-01-05 21:47:57 +0000748 DIEntry *&getDIEntrySlotFor(GlobalVariable *GV) {
749 return GVToDIEntryMap[GV];
750 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000751
Jim Laskeyef42a012006-11-02 20:12:39 +0000752 /// AddDie - Adds or interns the DIE to the compile unit.
753 ///
754 DIE *AddDie(DIE &Buffer) {
755 FoldingSetNodeID ID;
756 Buffer.Profile(ID);
757 void *Where;
758 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000759
Jim Laskeyef42a012006-11-02 20:12:39 +0000760 if (!Die) {
761 Die = new DIE(Buffer);
762 DiesSet.InsertNode(Die, Where);
763 this->Die->AddChild(Die);
764 Buffer.Detach();
765 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000766
Jim Laskeyef42a012006-11-02 20:12:39 +0000767 return Die;
768 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000769};
770
Jim Laskey65195462006-10-30 13:35:07 +0000771//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000772/// Dwarf - Emits general Dwarf directives.
Jim Laskeyef42a012006-11-02 20:12:39 +0000773///
Jim Laskey65195462006-10-30 13:35:07 +0000774class Dwarf {
Jim Laskey072200c2007-01-29 18:51:14 +0000775protected:
Jim Laskey65195462006-10-30 13:35:07 +0000776 //===--------------------------------------------------------------------===//
Jim Laskey072200c2007-01-29 18:51:14 +0000777 // Core attributes used by the Dwarf writer.
Jim Laskey65195462006-10-30 13:35:07 +0000778 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000779
Jim Laskey65195462006-10-30 13:35:07 +0000780 //
781 /// O - Stream to .s file.
782 ///
Owen Andersoncb371882008-08-21 00:14:44 +0000783 raw_ostream &O;
Jim Laskey65195462006-10-30 13:35:07 +0000784
785 /// Asm - Target of Dwarf emission.
786 ///
787 AsmPrinter *Asm;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000788
Bill Wendlingaa8f8882008-07-01 23:34:48 +0000789 /// TAI - Target asm information.
Jim Laskey65195462006-10-30 13:35:07 +0000790 const TargetAsmInfo *TAI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000791
Jim Laskey65195462006-10-30 13:35:07 +0000792 /// TD - Target data.
793 const TargetData *TD;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000794
Jim Laskey65195462006-10-30 13:35:07 +0000795 /// RI - Register Information.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000796 const TargetRegisterInfo *RI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000797
Jim Laskey65195462006-10-30 13:35:07 +0000798 /// M - Current module.
799 ///
800 Module *M;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000801
Jim Laskey65195462006-10-30 13:35:07 +0000802 /// MF - Current machine function.
803 ///
804 MachineFunction *MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000805
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000806 /// MMI - Collected machine module information.
Jim Laskey65195462006-10-30 13:35:07 +0000807 ///
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000808 MachineModuleInfo *MMI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000809
Jim Laskey65195462006-10-30 13:35:07 +0000810 /// SubprogramCount - The running count of functions being compiled.
811 ///
812 unsigned SubprogramCount;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000813
Chris Lattner9251f132007-09-24 03:35:37 +0000814 /// Flavor - A unique string indicating what dwarf producer this is, used to
815 /// unique labels.
816 const char * const Flavor;
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000817
818 unsigned SetCounter;
Owen Andersoncb371882008-08-21 00:14:44 +0000819 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattner9251f132007-09-24 03:35:37 +0000820 const char *flavor)
Jim Laskey072200c2007-01-29 18:51:14 +0000821 : O(OS)
822 , Asm(A)
823 , TAI(T)
824 , TD(Asm->TM.getTargetData())
825 , RI(Asm->TM.getRegisterInfo())
826 , M(NULL)
827 , MF(NULL)
828 , MMI(NULL)
Jim Laskey072200c2007-01-29 18:51:14 +0000829 , SubprogramCount(0)
Chris Lattner9251f132007-09-24 03:35:37 +0000830 , Flavor(flavor)
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000831 , SetCounter(1)
Jim Laskey072200c2007-01-29 18:51:14 +0000832 {
833 }
834
835public:
Jim Laskey072200c2007-01-29 18:51:14 +0000836 //===--------------------------------------------------------------------===//
837 // Accessors.
838 //
Bill Wendlinga6f02fb2009-04-10 00:00:25 +0000839 const AsmPrinter *getAsm() const { return Asm; }
Jim Laskeyb82313f2007-02-01 16:31:34 +0000840 MachineModuleInfo *getMMI() const { return MMI; }
Jim Laskey072200c2007-01-29 18:51:14 +0000841 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohman82482942007-09-27 23:12:31 +0000842 const TargetData *getTargetData() const { return TD; }
Jim Laskey072200c2007-01-29 18:51:14 +0000843
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000844 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
845 const {
846 if (isInSection && TAI->getDwarfSectionOffsetDirective())
847 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohman82482942007-09-27 23:12:31 +0000848 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000849 O << TAI->getData32bitsDirective();
850 else
851 O << TAI->getData64bitsDirective();
852 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000853
Jim Laskeyb82313f2007-02-01 16:31:34 +0000854 /// PrintLabelName - Print label name in form used by Dwarf writer.
855 ///
856 void PrintLabelName(DWLabel Label) const {
857 PrintLabelName(Label.Tag, Label.Number);
858 }
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000859 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000860 O << TAI->getPrivateGlobalPrefix() << Tag;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000861 if (Number) O << Number;
862 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000863
Chris Lattner9251f132007-09-24 03:35:37 +0000864 void PrintLabelName(const char *Tag, unsigned Number,
865 const char *Suffix) const {
866 O << TAI->getPrivateGlobalPrefix() << Tag;
867 if (Number) O << Number;
868 O << Suffix;
869 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000870
Jim Laskeyb82313f2007-02-01 16:31:34 +0000871 /// EmitLabel - Emit location label for internal use by Dwarf.
872 ///
873 void EmitLabel(DWLabel Label) const {
874 EmitLabel(Label.Tag, Label.Number);
875 }
876 void EmitLabel(const char *Tag, unsigned Number) const {
877 PrintLabelName(Tag, Number);
878 O << ":\n";
879 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000880
Jim Laskeyb82313f2007-02-01 16:31:34 +0000881 /// EmitReference - Emit a reference to a label.
882 ///
Dan Gohman06ff4e62007-09-28 15:43:33 +0000883 void EmitReference(DWLabel Label, bool IsPCRelative = false,
884 bool Force32Bit = false) const {
885 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000886 }
887 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman06ff4e62007-09-28 15:43:33 +0000888 bool IsPCRelative = false, bool Force32Bit = false) const {
889 PrintRelDirective(Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000890 PrintLabelName(Tag, Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000891
Jim Laskeyb82313f2007-02-01 16:31:34 +0000892 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
893 }
Dan Gohman06ff4e62007-09-28 15:43:33 +0000894 void EmitReference(const std::string &Name, bool IsPCRelative = false,
895 bool Force32Bit = false) const {
896 PrintRelDirective(Force32Bit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000897
Jim Laskeyb82313f2007-02-01 16:31:34 +0000898 O << Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000899
Jim Laskeyb82313f2007-02-01 16:31:34 +0000900 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
901 }
902
903 /// EmitDifference - Emit the difference between two labels. Some
904 /// assemblers do not behave with absolute expressions with data directives,
905 /// so there is an option (needsSet) to use an intermediary set expression.
906 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000907 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000908 EmitDifference(LabelHi.Tag, LabelHi.Number,
909 LabelLo.Tag, LabelLo.Number,
910 IsSmall);
911 }
912 void EmitDifference(const char *TagHi, unsigned NumberHi,
913 const char *TagLo, unsigned NumberLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000914 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000915 if (TAI->needsSet()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000916 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000917 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000918 O << ",";
919 PrintLabelName(TagHi, NumberHi);
920 O << "-";
921 PrintLabelName(TagLo, NumberLo);
922 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000923
924 PrintRelDirective(IsSmall);
Chris Lattner9251f132007-09-24 03:35:37 +0000925 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000926 ++SetCounter;
927 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000928 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000929
Jim Laskeyb82313f2007-02-01 16:31:34 +0000930 PrintLabelName(TagHi, NumberHi);
931 O << "-";
932 PrintLabelName(TagLo, NumberLo);
933 }
934 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000935
936 void EmitSectionOffset(const char* Label, const char* Section,
937 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000938 bool IsSmall = false, bool isEH = false,
939 bool useSet = true) {
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000940 bool printAbsolute = false;
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000941 if (isEH)
942 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
943 else
944 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
945
946 if (TAI->needsSet() && useSet) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000947 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000948 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000949 O << ",";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000950 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000951
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000952 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000953 O << "-";
954 PrintLabelName(Section, SectionNumber);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000955 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000956 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000957
958 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000959
Chris Lattner9251f132007-09-24 03:35:37 +0000960 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000961 ++SetCounter;
962 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000963 PrintRelDirective(IsSmall, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000964
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000965 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000966
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000967 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000968 O << "-";
969 PrintLabelName(Section, SectionNumber);
970 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000971 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000972 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000973
Jim Laskeyb82313f2007-02-01 16:31:34 +0000974 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
975 /// frame.
976 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenb97aec62007-11-13 19:13:01 +0000977 const std::vector<MachineMove> &Moves, bool isEH) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000978 int stackGrowth =
979 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
980 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +0000981 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeybacd3042007-02-21 22:48:45 +0000982 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000983
984 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +0000985 const MachineMove &Move = Moves[i];
Jim Laskeyb82313f2007-02-01 16:31:34 +0000986 unsigned LabelID = Move.getLabelID();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000987
Jim Laskeyb82313f2007-02-01 16:31:34 +0000988 if (LabelID) {
989 LabelID = MMI->MappedLabel(LabelID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000990
Jim Laskeyb82313f2007-02-01 16:31:34 +0000991 // Throw out move if the label is invalid.
992 if (!LabelID) continue;
993 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000994
Jim Laskeyb82313f2007-02-01 16:31:34 +0000995 const MachineLocation &Dst = Move.getDestination();
996 const MachineLocation &Src = Move.getSource();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000997
Jim Laskeyb82313f2007-02-01 16:31:34 +0000998 // Advance row if new location.
999 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1000 Asm->EmitInt8(DW_CFA_advance_loc4);
1001 Asm->EOL("DW_CFA_advance_loc4");
Jim Laskeybacd3042007-02-21 22:48:45 +00001002 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1003 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001004
Jim Laskeyb82313f2007-02-01 16:31:34 +00001005 BaseLabelID = LabelID;
Jim Laskeybacd3042007-02-21 22:48:45 +00001006 BaseLabel = "label";
Jim Laskeyb82313f2007-02-01 16:31:34 +00001007 IsLocal = true;
1008 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001009
Jim Laskeyb82313f2007-02-01 16:31:34 +00001010 // If advancing cfa.
Dan Gohmand735b802008-10-03 15:45:36 +00001011 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1012 if (!Src.isReg()) {
1013 if (Src.getReg() == MachineLocation::VirtualFP) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001014 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1015 Asm->EOL("DW_CFA_def_cfa_offset");
1016 } else {
1017 Asm->EmitInt8(DW_CFA_def_cfa);
1018 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmand735b802008-10-03 15:45:36 +00001019 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001020 Asm->EOL("Register");
1021 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001022
Jim Laskeyb82313f2007-02-01 16:31:34 +00001023 int Offset = -Src.getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001024
Jim Laskeyb82313f2007-02-01 16:31:34 +00001025 Asm->EmitULEB128Bytes(Offset);
1026 Asm->EOL("Offset");
1027 } else {
1028 assert(0 && "Machine move no supported yet.");
1029 }
Dan Gohmand735b802008-10-03 15:45:36 +00001030 } else if (Src.isReg() &&
1031 Src.getReg() == MachineLocation::VirtualFP) {
1032 if (Dst.isReg()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001033 Asm->EmitInt8(DW_CFA_def_cfa_register);
1034 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmand735b802008-10-03 15:45:36 +00001035 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001036 Asm->EOL("Register");
1037 } else {
1038 assert(0 && "Machine move no supported yet.");
1039 }
1040 } else {
Dan Gohmand735b802008-10-03 15:45:36 +00001041 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001042 int Offset = Dst.getOffset() / stackGrowth;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001043
Jim Laskeyb82313f2007-02-01 16:31:34 +00001044 if (Offset < 0) {
1045 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1046 Asm->EOL("DW_CFA_offset_extended_sf");
1047 Asm->EmitULEB128Bytes(Reg);
1048 Asm->EOL("Reg");
1049 Asm->EmitSLEB128Bytes(Offset);
1050 Asm->EOL("Offset");
1051 } else if (Reg < 64) {
1052 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Cheng42bf74b2009-03-25 01:47:28 +00001053 if (Asm->isVerbose())
Evan Chengd4114192008-07-09 21:53:02 +00001054 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1055 else
1056 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00001057 Asm->EmitULEB128Bytes(Offset);
1058 Asm->EOL("Offset");
1059 } else {
1060 Asm->EmitInt8(DW_CFA_offset_extended);
1061 Asm->EOL("DW_CFA_offset_extended");
1062 Asm->EmitULEB128Bytes(Reg);
1063 Asm->EOL("Reg");
1064 Asm->EmitULEB128Bytes(Offset);
1065 Asm->EOL("Offset");
1066 }
1067 }
1068 }
1069 }
1070
Jim Laskey072200c2007-01-29 18:51:14 +00001071};
1072
1073//===----------------------------------------------------------------------===//
Devang Patelf3ee5142009-01-12 22:54:42 +00001074/// SrcLineInfo - This class is used to record source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001075///
1076class SrcLineInfo {
1077 unsigned Line; // Source line number.
1078 unsigned Column; // Source column.
1079 unsigned SourceID; // Source ID number.
1080 unsigned LabelID; // Label in code ID number.
1081public:
1082 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001083 : Line(L), Column(C), SourceID(S), LabelID(I) {}
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00001084
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001085 // Accessors
1086 unsigned getLine() const { return Line; }
1087 unsigned getColumn() const { return Column; }
1088 unsigned getSourceID() const { return SourceID; }
1089 unsigned getLabelID() const { return LabelID; }
1090};
1091
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001092//===----------------------------------------------------------------------===//
Devang Patel7a6e5a32009-01-08 02:33:41 +00001093/// DbgVariable - This class is used to track local variable information.
1094///
1095class DbgVariable {
Devang Patel99ec3532009-01-16 19:28:14 +00001096 DIVariable Var; // Variable Descriptor.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001097 unsigned FrameIndex; // Variable frame index.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001098public:
Devang Patel99ec3532009-01-16 19:28:14 +00001099 DbgVariable(DIVariable V, unsigned I) : Var(V), FrameIndex(I) {}
Devang Patel7a6e5a32009-01-08 02:33:41 +00001100
1101 // Accessors.
Devang Patel99ec3532009-01-16 19:28:14 +00001102 DIVariable getVariable() const { return Var; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001103 unsigned getFrameIndex() const { return FrameIndex; }
1104};
1105
1106//===----------------------------------------------------------------------===//
1107/// DbgScope - This class is used to track scope information.
1108///
1109class DbgScope {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001110 DbgScope *Parent; // Parent to this scope.
Devang Patel7103c6a2009-01-16 18:01:58 +00001111 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001112 // Either subprogram or block.
1113 unsigned StartLabelID; // Label ID of the beginning of scope.
1114 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel7103c6a2009-01-16 18:01:58 +00001115 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
Devang Patel9795da52009-01-10 02:42:49 +00001116 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001117public:
Devang Patel0e5200f2009-01-15 18:25:17 +00001118 DbgScope(DbgScope *P, DIDescriptor D)
Devang Patel7a6e5a32009-01-08 02:33:41 +00001119 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), Scopes(), Variables()
1120 {}
Devang Patel1be3ecc2009-04-15 00:10:26 +00001121 virtual ~DbgScope() {
Devang Patel481ff5b2009-01-12 18:48:36 +00001122 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1123 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1124 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001125
1126 // Accessors.
Devang Patel7103c6a2009-01-16 18:01:58 +00001127 DbgScope *getParent() const { return Parent; }
1128 DIDescriptor getDesc() const { return Desc; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001129 unsigned getStartLabelID() const { return StartLabelID; }
1130 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel9795da52009-01-10 02:42:49 +00001131 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1132 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001133 void setStartLabelID(unsigned S) { StartLabelID = S; }
1134 void setEndLabelID(unsigned E) { EndLabelID = E; }
1135
1136 /// AddScope - Add a scope to the scope.
1137 ///
1138 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1139
1140 /// AddVariable - Add a variable to the scope.
1141 ///
1142 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
Devang Patel1be3ecc2009-04-15 00:10:26 +00001143
1144 virtual bool isInlinedSubroutine() { return false; }
Devang Patel3e3702d2009-04-21 00:08:56 +00001145 virtual unsigned getLine() { assert ( 0 && "Unexpected scope!"); return 0; }
1146 virtual unsigned getColumn() { assert ( 0 && "Unexpected scope!"); return 0; }
1147 virtual unsigned getFile() { assert ( 0 && "Unexpected scope!"); return 0; }
Devang Patel1be3ecc2009-04-15 00:10:26 +00001148};
1149
1150
1151//===----------------------------------------------------------------------===//
1152/// DbgInlinedSubroutineScope - This class is used to track inlined subroutine
1153/// scope information.
1154///
1155class DbgInlinedSubroutineScope : public DbgScope {
1156 unsigned Src;
1157 unsigned Line;
1158 unsigned Col;
1159public:
1160 DbgInlinedSubroutineScope(DbgScope *P, DIDescriptor D,
1161 unsigned S, unsigned L, unsigned C)
1162 : DbgScope(P, D), Src(S), Line(L), Col(C)
1163 {}
1164
1165 unsigned getLine() { return Line; }
1166 unsigned getColumn() { return Col; }
1167 unsigned getFile() { return Src; }
1168 bool isInlinedSubroutine() { return true; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001169};
1170
1171//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001172/// DwarfDebug - Emits Dwarf debug directives.
Jim Laskey072200c2007-01-29 18:51:14 +00001173///
1174class DwarfDebug : public Dwarf {
Jim Laskey65195462006-10-30 13:35:07 +00001175 //===--------------------------------------------------------------------===//
1176 // Attributes used to construct specific Dwarf sections.
1177 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001178
Evan Chenge3d42322009-02-25 07:04:34 +00001179 /// CompileUnitMap - A map of global variables representing compile units to
1180 /// compile units.
1181 DenseMap<Value *, CompileUnit *> CompileUnitMap;
1182
1183 /// CompileUnits - All the compile units in this module.
1184 ///
1185 SmallVector<CompileUnit *, 8> CompileUnits;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001186
Devang Pateldd9db662009-01-30 18:20:31 +00001187 /// MainCU - Some platform prefers one compile unit per .o file. In such
1188 /// cases, all dies are inserted in MainCU.
1189 CompileUnit *MainCU;
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001190
Jim Laskeyef42a012006-11-02 20:12:39 +00001191 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +00001192 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001193 FoldingSet<DIEAbbrev> AbbreviationsSet;
1194
1195 /// Abbreviations - A list of all the unique abbreviations in use.
1196 ///
1197 std::vector<DIEAbbrev *> Abbreviations;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001198
Evan Chenge3d42322009-02-25 07:04:34 +00001199 /// DirectoryIdMap - Directory name to directory id map.
1200 ///
1201 StringMap<unsigned> DirectoryIdMap;
Devang Patel8526cc02009-01-05 22:35:52 +00001202
Evan Chenge3d42322009-02-25 07:04:34 +00001203 /// DirectoryNames - A list of directory names.
1204 SmallVector<std::string, 8> DirectoryNames;
1205
1206 /// SourceFileIdMap - Source file name to source file id map.
1207 ///
1208 StringMap<unsigned> SourceFileIdMap;
1209
1210 /// SourceFileNames - A list of source file names.
1211 SmallVector<std::string, 8> SourceFileNames;
1212
1213 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
1214 /// id mapped to a unique id.
1215 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
1216
1217 /// SourceIds - Reverse map from source id to directory id + file id pair.
1218 ///
1219 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
Devang Patel8526cc02009-01-05 22:35:52 +00001220
Devang Patel07354e52009-01-16 21:07:53 +00001221 /// Lines - List of of source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001222 std::vector<SrcLineInfo> Lines;
1223
Devang Patel07354e52009-01-16 21:07:53 +00001224 /// ValuesSet - Used to uniquely define values.
1225 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001226 FoldingSet<DIEValue> ValuesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001227
Jim Laskeyef42a012006-11-02 20:12:39 +00001228 /// Values - A list of all the unique values in use.
1229 ///
1230 std::vector<DIEValue *> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001231
Jim Laskey65195462006-10-30 13:35:07 +00001232 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +00001233 ///
Jim Laskey65195462006-10-30 13:35:07 +00001234 UniqueVector<std::string> StringPool;
1235
Jim Laskey65195462006-10-30 13:35:07 +00001236 /// SectionMap - Provides a unique id per text section.
1237 ///
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001238 UniqueVector<const Section*> SectionMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001239
Jim Laskey65195462006-10-30 13:35:07 +00001240 /// SectionSourceLines - Tracks line numbers per text section.
1241 ///
Devang Patelf3ee5142009-01-12 22:54:42 +00001242 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Jim Laskey65195462006-10-30 13:35:07 +00001243
Jim Laskeybacd3042007-02-21 22:48:45 +00001244 /// didInitial - Flag to indicate if initial emission has been done.
1245 ///
1246 bool didInitial;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001247
Jim Laskeybacd3042007-02-21 22:48:45 +00001248 /// shouldEmit - Flag to indicate if debug information should be emitted.
1249 ///
1250 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +00001251
Devang Patel7d2f9722009-04-15 20:41:31 +00001252 // FunctionDbgScope - Top level scope for the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001253 //
Devang Patel7d2f9722009-04-15 20:41:31 +00001254 DbgScope *FunctionDbgScope;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001255
Bill Wendling163ba3f2009-03-10 21:23:25 +00001256 /// DbgScopeMap - Tracks the scopes in the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001257 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
Bill Wendling163ba3f2009-03-10 21:23:25 +00001258
Devang Patel1be3ecc2009-04-15 00:10:26 +00001259 /// DbgInlinedScopeMap - Tracks inlined scopes in the current function.
1260 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> > DbgInlinedScopeMap;
1261
Devang Patel0f7fef32009-04-13 17:02:03 +00001262 /// InlineInfo - Keep track of inlined functions and their location.
1263 /// This information is used to populate debug_inlined section.
1264 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> > InlineInfo;
1265
Devang Patel1be3ecc2009-04-15 00:10:26 +00001266 /// InlinedVariableScopes - Scopes information for the inlined subroutine
1267 /// variables.
1268 DenseMap<const MachineInstr *, DbgScope *> InlinedVariableScopes;
1269
Bill Wendling163ba3f2009-03-10 21:23:25 +00001270 /// DebugTimer - Timer for the Dwarf debug writer.
1271 Timer *DebugTimer;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001272
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001273 struct FunctionDebugFrameInfo {
1274 unsigned Number;
1275 std::vector<MachineMove> Moves;
1276
1277 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001278 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001279 };
1280
1281 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001282
Bill Wendling163ba3f2009-03-10 21:23:25 +00001283private:
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001284 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
Bill Wendlingc8615e42009-03-10 21:47:45 +00001285 /// maps to the source id. Source id starts at 1.
1286 std::pair<unsigned, unsigned>
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001287 getSourceDirectoryAndFileIds(unsigned SId) const {
Bill Wendlingc8615e42009-03-10 21:47:45 +00001288 return SourceIds[SId-1];
1289 }
1290
1291 /// getNumSourceDirectories - Return the number of source directories in the
1292 /// debug info.
1293 unsigned getNumSourceDirectories() const {
1294 return DirectoryNames.size();
1295 }
1296
1297 /// getSourceDirectoryName - Return the name of the directory corresponding
1298 /// to the id.
1299 const std::string &getSourceDirectoryName(unsigned Id) const {
1300 return DirectoryNames[Id - 1];
1301 }
1302
1303 /// getSourceFileName - Return the name of the source file corresponding
1304 /// to the id.
1305 const std::string &getSourceFileName(unsigned Id) const {
1306 return SourceFileNames[Id - 1];
1307 }
1308
1309 /// getNumSourceIds - Return the number of unique source ids.
Bill Wendlingc8615e42009-03-10 21:47:45 +00001310 unsigned getNumSourceIds() const {
1311 return SourceIds.size();
1312 }
1313
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001314 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001315 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001316 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1317 // Profile the node so that we can make it unique.
1318 FoldingSetNodeID ID;
1319 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001320
Jim Laskeyef42a012006-11-02 20:12:39 +00001321 // Check the set for priors.
1322 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001323
Jim Laskeyef42a012006-11-02 20:12:39 +00001324 // If it's newly added.
1325 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001326 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +00001327 Abbreviations.push_back(&Abbrev);
1328 // Assign the vector position + 1 as its number.
1329 Abbrev.setNumber(Abbreviations.size());
1330 } else {
1331 // Assign existing abbreviation number.
1332 Abbrev.setNumber(InSet->getNumber());
1333 }
1334 }
1335
Jim Laskey65195462006-10-30 13:35:07 +00001336 /// NewString - Add a string to the constant pool and returns a label.
1337 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001338 DWLabel NewString(const std::string &String) {
1339 unsigned StringID = StringPool.insert(String);
1340 return DWLabel("string", StringID);
1341 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001342
Jim Laskeyef42a012006-11-02 20:12:39 +00001343 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1344 /// entry.
1345 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1346 DIEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001347
Jim Laskeyef42a012006-11-02 20:12:39 +00001348 if (Entry) {
1349 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001350 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001351 void *Where;
1352 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001353
Jim Laskeyf6733882006-11-02 21:48:18 +00001354 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001355
Jim Laskeyef42a012006-11-02 20:12:39 +00001356 Value = new DIEntry(Entry);
1357 ValuesSet.InsertNode(Value, Where);
1358 } else {
1359 Value = new DIEntry(Entry);
1360 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001361
Jim Laskeyef42a012006-11-02 20:12:39 +00001362 Values.push_back(Value);
1363 return Value;
1364 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001365
Jim Laskeyef42a012006-11-02 20:12:39 +00001366 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1367 ///
1368 void SetDIEntry(DIEntry *Value, DIE *Entry) {
Bill Wendlingdd446322009-03-10 23:57:09 +00001369 Value->setEntry(Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001370 // Add to values set if not already there. If it is, we merely have a
1371 // duplicate in the values list (no harm.)
1372 ValuesSet.GetOrInsertNode(Value);
1373 }
1374
1375 /// AddUInt - Add an unsigned integer attribute data and value.
1376 ///
1377 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1378 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1379
1380 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001381 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001382 void *Where;
1383 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1384 if (!Value) {
1385 Value = new DIEInteger(Integer);
1386 ValuesSet.InsertNode(Value, Where);
1387 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001388 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001389
Jim Laskeyef42a012006-11-02 20:12:39 +00001390 Die->AddValue(Attribute, Form, Value);
1391 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001392
Jim Laskeyef42a012006-11-02 20:12:39 +00001393 /// AddSInt - Add an signed integer attribute data and value.
1394 ///
1395 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1396 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1397
1398 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001399 DIEInteger::Profile(ID, (uint64_t)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
Evan Chenge3d42322009-02-25 07:04:34 +00001411 /// AddString - Add a string attribute data and value.
Jim Laskeyef42a012006-11-02 20:12:39 +00001412 ///
1413 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1414 const std::string &String) {
1415 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001416 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001417 void *Where;
1418 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1419 if (!Value) {
1420 Value = new DIEString(String);
1421 ValuesSet.InsertNode(Value, Where);
1422 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001423 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001424
Jim Laskeyef42a012006-11-02 20:12:39 +00001425 Die->AddValue(Attribute, Form, Value);
1426 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001427
Jim Laskeyef42a012006-11-02 20:12:39 +00001428 /// AddLabel - Add a Dwarf label attribute data and value.
1429 ///
1430 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1431 const DWLabel &Label) {
1432 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001433 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001434 void *Where;
1435 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1436 if (!Value) {
1437 Value = new DIEDwarfLabel(Label);
1438 ValuesSet.InsertNode(Value, Where);
1439 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001440 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001441
Jim Laskeyef42a012006-11-02 20:12:39 +00001442 Die->AddValue(Attribute, Form, Value);
1443 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001444
Jim Laskeyef42a012006-11-02 20:12:39 +00001445 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1446 ///
1447 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1448 const std::string &Label) {
1449 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001450 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001451 void *Where;
1452 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1453 if (!Value) {
1454 Value = new DIEObjectLabel(Label);
1455 ValuesSet.InsertNode(Value, Where);
1456 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001457 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001458
Jim Laskeyef42a012006-11-02 20:12:39 +00001459 Die->AddValue(Attribute, Form, Value);
1460 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001461
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001462 /// AddSectionOffset - Add a section offset label attribute data and value.
1463 ///
1464 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1465 const DWLabel &Label, const DWLabel &Section,
1466 bool isEH = false, bool useSet = true) {
1467 FoldingSetNodeID ID;
1468 DIESectionOffset::Profile(ID, Label, Section);
1469 void *Where;
1470 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1471 if (!Value) {
1472 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1473 ValuesSet.InsertNode(Value, Where);
1474 Values.push_back(Value);
1475 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001476
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001477 Die->AddValue(Attribute, Form, Value);
1478 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001479
Jim Laskeyef42a012006-11-02 20:12:39 +00001480 /// AddDelta - Add a label delta attribute data and value.
1481 ///
1482 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
Devang Patel1be3ecc2009-04-15 00:10:26 +00001483 const DWLabel &Hi, const DWLabel &Lo) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001484 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001485 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001486 void *Where;
1487 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1488 if (!Value) {
1489 Value = new DIEDelta(Hi, Lo);
1490 ValuesSet.InsertNode(Value, Where);
1491 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001492 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001493
Jim Laskeyef42a012006-11-02 20:12:39 +00001494 Die->AddValue(Attribute, Form, Value);
1495 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001496
Jim Laskeyef42a012006-11-02 20:12:39 +00001497 /// AddDIEntry - Add a DIE attribute data and value.
1498 ///
1499 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1500 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1501 }
1502
1503 /// AddBlock - Add block data.
1504 ///
1505 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1506 Block->ComputeSize(*this);
1507 FoldingSetNodeID ID;
1508 Block->Profile(ID);
1509 void *Where;
1510 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1511 if (!Value) {
1512 Value = Block;
1513 ValuesSet.InsertNode(Value, Where);
1514 Values.push_back(Value);
1515 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001516 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001517 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001518 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001519 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001520
Jim Laskeyef42a012006-11-02 20:12:39 +00001521 Die->AddValue(Attribute, Block->BestForm(), Value);
1522 }
1523
Jim Laskey65195462006-10-30 13:35:07 +00001524 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001525 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001526 void AddSourceLine(DIE *Die, const DIVariable *V) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001527 unsigned FileID = 0;
1528 unsigned Line = V->getLineNumber();
Devang Pateldd9db662009-01-30 18:20:31 +00001529 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1530 FileID = Unit->getID();
Devang Patel2303df92009-02-10 06:04:08 +00001531 assert (FileID && "Invalid file id");
Devang Patel7a6e5a32009-01-08 02:33:41 +00001532 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1533 AddUInt(Die, DW_AT_decl_line, 0, Line);
1534 }
1535
1536 /// AddSourceLine - Add location information to specified debug information
1537 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001538 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Devang Patel8526cc02009-01-05 22:35:52 +00001539 unsigned FileID = 0;
1540 unsigned Line = G->getLineNumber();
Devang Pateldd9db662009-01-30 18:20:31 +00001541 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1542 FileID = Unit->getID();
Devang Patel2303df92009-02-10 06:04:08 +00001543 assert (FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001544 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1545 AddUInt(Die, DW_AT_decl_line, 0, Line);
1546 }
1547
Devang Patel99ec3532009-01-16 19:28:14 +00001548 void AddSourceLine(DIE *Die, const DIType *Ty) {
Devang Patel8526cc02009-01-05 22:35:52 +00001549 unsigned FileID = 0;
Devang Patel99ec3532009-01-16 19:28:14 +00001550 unsigned Line = Ty->getLineNumber();
Devang Pateldd9db662009-01-30 18:20:31 +00001551 DICompileUnit CU = Ty->getCompileUnit();
1552 if (CU.isNull())
1553 return;
1554 CompileUnit *Unit = FindCompileUnit(CU);
1555 FileID = Unit->getID();
Devang Patel2303df92009-02-10 06:04:08 +00001556 assert (FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001557 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1558 AddUInt(Die, DW_AT_decl_line, 0, Line);
1559 }
1560
Jim Laskey65195462006-10-30 13:35:07 +00001561 /// AddAddress - Add an address attribute to a die based on the location
1562 /// provided.
1563 void AddAddress(DIE *Die, unsigned Attribute,
Devang Patel1be3ecc2009-04-15 00:10:26 +00001564 const MachineLocation &Location) {
Dan Gohmand735b802008-10-03 15:45:36 +00001565 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001566 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001567
Dan Gohmand735b802008-10-03 15:45:36 +00001568 if (Location.isReg()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001569 if (Reg < 32) {
1570 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1571 } else {
1572 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1573 AddUInt(Block, 0, DW_FORM_udata, Reg);
1574 }
1575 } else {
1576 if (Reg < 32) {
1577 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1578 } else {
1579 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1580 AddUInt(Block, 0, DW_FORM_udata, Reg);
1581 }
1582 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1583 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001584
Jim Laskeyef42a012006-11-02 20:12:39 +00001585 AddBlock(Die, Attribute, 0, Block);
1586 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001587
Jim Laskeyef42a012006-11-02 20:12:39 +00001588 /// AddType - Add a new type attribute to the specified entity.
Devang Patel48d190f2009-01-05 21:47:57 +00001589 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel80303aa2009-01-23 19:13:31 +00001590 if (Ty.isNull())
Devang Patel48d190f2009-01-05 21:47:57 +00001591 return;
Devang Patel48d190f2009-01-05 21:47:57 +00001592
1593 // Check for pre-existence.
1594 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1595 // If it exists then use the existing value.
1596 if (Slot) {
1597 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1598 return;
1599 }
1600
1601 // Set up proxy.
1602 Slot = NewDIEntry();
1603
1604 // Construct type.
1605 DIE Buffer(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001606 if (Ty.isBasicType(Ty.getTag()))
1607 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1608 else if (Ty.isDerivedType(Ty.getTag()))
1609 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1610 else {
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001611 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelf193ff02009-01-15 19:26:23 +00001612 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1613 }
1614
Devang Patel7009d242009-01-27 23:22:55 +00001615 // Add debug information entry to entity and appropriate context.
1616 DIE *Die = NULL;
1617 DIDescriptor Context = Ty.getContext();
1618 if (!Context.isNull())
1619 Die = DW_Unit->getDieMapSlotFor(Context.getGV());
1620
1621 if (Die) {
1622 DIE *Child = new DIE(Buffer);
1623 Die->AddChild(Child);
1624 Buffer.Detach();
1625 SetDIEntry(Slot, Child);
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001626 } else {
Devang Patel7009d242009-01-27 23:22:55 +00001627 Die = DW_Unit->AddDie(Buffer);
1628 SetDIEntry(Slot, Die);
1629 }
1630
Devang Patel48d190f2009-01-05 21:47:57 +00001631 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1632 }
1633
Devang Patele5202732009-01-05 19:07:53 +00001634 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1635 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001636 DIBasicType BTy) {
Bill Wendling0582ae92009-03-13 04:39:26 +00001637
Devang Patel08f053f2009-01-05 17:57:47 +00001638 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001639 std::string Name;
1640 BTy.getName(Name);
Devang Patel08f053f2009-01-05 17:57:47 +00001641 Buffer.setTag(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001642 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patel08f053f2009-01-05 17:57:47 +00001643 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001644 if (!Name.empty())
Devang Patel08f053f2009-01-05 17:57:47 +00001645 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001646 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel08f053f2009-01-05 17:57:47 +00001647 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1648 }
1649
Devang Patele5202732009-01-05 19:07:53 +00001650 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1651 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001652 DIDerivedType DTy) {
Bill Wendling0582ae92009-03-13 04:39:26 +00001653
Devang Patel08f053f2009-01-05 17:57:47 +00001654 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001655 std::string Name;
1656 DTy.getName(Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001657 uint64_t Size = DTy.getSizeInBits() >> 3;
1658 unsigned Tag = DTy.getTag();
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001659
Devang Patel08f053f2009-01-05 17:57:47 +00001660 // FIXME - Workaround for templates.
1661 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1662
1663 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001664
Devang Patel08f053f2009-01-05 17:57:47 +00001665 // Map to main type, void will not have a type.
Devang Patelf193ff02009-01-15 19:26:23 +00001666 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00001667 AddType(DW_Unit, &Buffer, FromTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001668
1669 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001670 if (!Name.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00001671 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel08f053f2009-01-05 17:57:47 +00001672
1673 // Add size if non-zero (derived types might be zero-sized.)
1674 if (Size)
1675 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1676
1677 // Add source line info if available and TyDesc is not a forward
1678 // declaration.
Devang Patelad165be2009-01-27 00:45:04 +00001679 if (!DTy.isForwardDecl())
1680 AddSourceLine(&Buffer, &DTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001681 }
1682
Devang Patelf4215332009-01-05 19:55:51 +00001683 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1684 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001685 DICompositeType CTy) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001686 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001687 std::string Name;
1688 CTy.getName(Name);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001689
Devang Patelf193ff02009-01-15 19:26:23 +00001690 uint64_t Size = CTy.getSizeInBits() >> 3;
1691 unsigned Tag = CTy.getTag();
Devang Patel49f38cb2009-01-23 01:19:09 +00001692 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001693
Devang Patelf4215332009-01-05 19:55:51 +00001694 switch (Tag) {
1695 case DW_TAG_vector_type:
1696 case DW_TAG_array_type:
Devang Patelf193ff02009-01-15 19:26:23 +00001697 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001698 break;
Devang Pateleab4a2e2009-01-20 18:35:14 +00001699 case DW_TAG_enumeration_type:
1700 {
1701 DIArray Elements = CTy.getTypeArray();
1702 // Add enumerators to enumeration type.
1703 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1704 DIE *ElemDie = NULL;
1705 DIEnumerator Enum(Elements.getElement(i).getGV());
1706 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
1707 Buffer.AddChild(ElemDie);
1708 }
1709 }
1710 break;
Devang Patelf4215332009-01-05 19:55:51 +00001711 case DW_TAG_subroutine_type:
1712 {
1713 // Add prototype flag.
1714 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patelf193ff02009-01-15 19:26:23 +00001715 DIArray Elements = CTy.getTypeArray();
Devang Patelf4215332009-01-05 19:55:51 +00001716 // Add return type.
Devang Patel48d190f2009-01-05 21:47:57 +00001717 DIDescriptor RTy = Elements.getElement(0);
Devang Patelf193ff02009-01-15 19:26:23 +00001718 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel48d190f2009-01-05 21:47:57 +00001719
Devang Patelf4215332009-01-05 19:55:51 +00001720 // Add arguments.
1721 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1722 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00001723 DIDescriptor Ty = Elements.getElement(i);
Devang Patel7ab24502009-01-17 06:57:25 +00001724 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patelf4215332009-01-05 19:55:51 +00001725 Buffer.AddChild(Arg);
1726 }
1727 }
1728 break;
1729 case DW_TAG_structure_type:
1730 case DW_TAG_union_type:
Devang Patel86bda412009-03-25 00:28:40 +00001731 case DW_TAG_class_type:
Devang Patelf4215332009-01-05 19:55:51 +00001732 {
1733 // Add elements to structure type.
Devang Patelf193ff02009-01-15 19:26:23 +00001734 DIArray Elements = CTy.getTypeArray();
Devang Patel153745c2009-01-16 00:50:53 +00001735
1736 // A forward struct declared type may not have elements available.
1737 if (Elements.isNull())
1738 break;
1739
Devang Patelf4215332009-01-05 19:55:51 +00001740 // Add elements to structure type.
1741 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1742 DIDescriptor Element = Elements.getElement(i);
Devang Patel5aac3d32009-01-17 08:01:33 +00001743 DIE *ElemDie = NULL;
Devang Patelf193ff02009-01-15 19:26:23 +00001744 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel2d1768c2009-01-17 08:05:14 +00001745 ElemDie = CreateSubprogramDIE(DW_Unit,
1746 DISubprogram(Element.getGV()));
Devang Patel5aac3d32009-01-17 08:01:33 +00001747 else if (Element.getTag() == dwarf::DW_TAG_variable) // ???
1748 ElemDie = CreateGlobalVariableDIE(DW_Unit,
1749 DIGlobalVariable(Element.getGV()));
Devang Patel2be58932009-01-20 21:02:02 +00001750 else
1751 ElemDie = CreateMemberDIE(DW_Unit,
1752 DIDerivedType(Element.getGV()));
Devang Patel2d1768c2009-01-17 08:05:14 +00001753 Buffer.AddChild(ElemDie);
Devang Patelf4215332009-01-05 19:55:51 +00001754 }
Devang Patel13319ce2009-02-17 22:43:44 +00001755 unsigned RLang = CTy.getRunTimeLang();
1756 if (RLang)
1757 AddUInt(&Buffer, DW_AT_APPLE_runtime_class, DW_FORM_data1, RLang);
Devang Patelf4215332009-01-05 19:55:51 +00001758 }
1759 break;
1760 default:
1761 break;
1762 }
1763
1764 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001765 if (!Name.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00001766 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf4215332009-01-05 19:55:51 +00001767
Devang Patelad165be2009-01-27 00:45:04 +00001768 if (Tag == DW_TAG_enumeration_type || Tag == DW_TAG_structure_type
1769 || Tag == DW_TAG_union_type) {
1770 // Add size if non-zero (derived types might be zero-sized.)
1771 if (Size)
1772 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1773 else {
1774 // Add zero size if it is not a forward declaration.
1775 if (CTy.isForwardDecl())
1776 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1777 else
1778 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1779 }
1780
1781 // Add source line info if available.
1782 if (!CTy.isForwardDecl())
1783 AddSourceLine(&Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001784 }
Devang Patelf4215332009-01-05 19:55:51 +00001785 }
1786
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001787 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1788 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelf193ff02009-01-15 19:26:23 +00001789 int64_t L = SR.getLo();
1790 int64_t H = SR.getHi();
Devang Patel68afdc32009-01-05 18:33:01 +00001791 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1792 if (L != H) {
1793 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1794 if (L)
Devang Patel2d1768c2009-01-17 08:05:14 +00001795 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1796 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel68afdc32009-01-05 18:33:01 +00001797 }
1798 Buffer.AddChild(DW_Subrange);
1799 }
1800
1801 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1802 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1803 DICompositeType *CTy) {
1804 Buffer.setTag(DW_TAG_array_type);
1805 if (CTy->getTag() == DW_TAG_vector_type)
1806 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1807
Devang Patelf9235742009-01-28 21:08:20 +00001808 // Emit derived type.
1809 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel68afdc32009-01-05 18:33:01 +00001810 DIArray Elements = CTy->getTypeArray();
Devang Patel68afdc32009-01-05 18:33:01 +00001811
1812 // Construct an anonymous type for index type.
1813 DIE IdxBuffer(DW_TAG_base_type);
1814 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1815 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1816 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1817
1818 // Add subranges to array type.
1819 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patelf4215332009-01-05 19:55:51 +00001820 DIDescriptor Element = Elements.getElement(i);
Devang Patelf193ff02009-01-15 19:26:23 +00001821 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1822 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel68afdc32009-01-05 18:33:01 +00001823 }
1824 }
1825
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001826 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Pateleab4a2e2009-01-20 18:35:14 +00001827 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patelc69bf2c2009-01-05 18:38:38 +00001828
1829 DIE *Enumerator = new DIE(DW_TAG_enumerator);
Bill Wendling0582ae92009-03-13 04:39:26 +00001830 std::string Name;
1831 ETy->getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001832 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
Devang Patelc69bf2c2009-01-05 18:38:38 +00001833 int64_t Value = ETy->getEnumValue();
1834 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Pateleab4a2e2009-01-20 18:35:14 +00001835 return Enumerator;
Devang Patelc69bf2c2009-01-05 18:38:38 +00001836 }
Devang Patel68afdc32009-01-05 18:33:01 +00001837
Devang Patel5aac3d32009-01-17 08:01:33 +00001838 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001839 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patel5aac3d32009-01-17 08:01:33 +00001840 {
1841 DIE *GVDie = new DIE(DW_TAG_variable);
Bill Wendling0582ae92009-03-13 04:39:26 +00001842 std::string Name;
1843 GV.getDisplayName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001844 AddString(GVDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling0582ae92009-03-13 04:39:26 +00001845 std::string LinkageName;
1846 GV.getLinkageName(LinkageName);
1847 if (!LinkageName.empty())
Devang Patel5aac3d32009-01-17 08:01:33 +00001848 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1849 AddType(DW_Unit, GVDie, GV.getType());
1850 if (!GV.isLocalToUnit())
1851 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
1852 AddSourceLine(GVDie, &GV);
1853 return GVDie;
Devang Patel86ae1422009-01-05 18:59:44 +00001854 }
1855
Devang Patel2be58932009-01-20 21:02:02 +00001856 /// CreateMemberDIE - Create new member DIE.
1857 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
1858 DIE *MemberDie = new DIE(DT.getTag());
Bill Wendling0582ae92009-03-13 04:39:26 +00001859 std::string Name;
1860 DT.getName(Name);
1861 if (!Name.empty())
Devang Patel2be58932009-01-20 21:02:02 +00001862 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1863
1864 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1865
1866 AddSourceLine(MemberDie, &DT);
1867
Devang Patel36375ee2009-02-17 21:23:59 +00001868 uint64_t Size = DT.getSizeInBits();
1869 uint64_t FieldSize = DT.getOriginalTypeSize();
1870
1871 if (Size != FieldSize) {
1872 // Handle bitfield.
1873 AddUInt(MemberDie, DW_AT_byte_size, 0, DT.getOriginalTypeSize() >> 3);
1874 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1875
1876 uint64_t Offset = DT.getOffsetInBits();
1877 uint64_t FieldOffset = Offset;
1878 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1879 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1880 FieldOffset = (HiMark - FieldSize);
1881 Offset -= FieldOffset;
1882 // Maybe we need to work from the other end.
1883 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1884 AddUInt(MemberDie, DW_AT_bit_offset, 0, Offset);
1885 }
Devang Patel2be58932009-01-20 21:02:02 +00001886 DIEBlock *Block = new DIEBlock();
1887 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1888 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1889 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1890
Devang Patel47661592009-01-21 00:08:04 +00001891 if (DT.isProtected())
1892 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1893 else if (DT.isPrivate())
1894 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1895
Devang Patel2be58932009-01-20 21:02:02 +00001896 return MemberDie;
1897 }
1898
Devang Patel5aac3d32009-01-17 08:01:33 +00001899 /// CreateSubprogramDIE - Create new DIE using SP.
1900 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Devang Patel2d1768c2009-01-17 08:05:14 +00001901 const DISubprogram &SP,
1902 bool IsConstructor = false) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001903 DIE *SPDie = new DIE(DW_TAG_subprogram);
Bill Wendling0582ae92009-03-13 04:39:26 +00001904 std::string Name;
1905 SP.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001906 AddString(SPDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling0582ae92009-03-13 04:39:26 +00001907 std::string LinkageName;
1908 SP.getLinkageName(LinkageName);
1909 if (!LinkageName.empty())
Devang Patel5aac3d32009-01-17 08:01:33 +00001910 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Devang Patel2d1768c2009-01-17 08:05:14 +00001911 LinkageName);
Devang Patel5aac3d32009-01-17 08:01:33 +00001912 AddSourceLine(SPDie, &SP);
Devang Patel86ae1422009-01-05 18:59:44 +00001913
Devang Patel5aac3d32009-01-17 08:01:33 +00001914 DICompositeType SPTy = SP.getType();
1915 DIArray Args = SPTy.getTypeArray();
1916
Devang Patel86ae1422009-01-05 18:59:44 +00001917 // Add Return Type.
Devang Patel75b27382009-04-08 22:18:45 +00001918 unsigned SPTag = SPTy.getTag();
Devang Patel9ac08d62009-02-27 18:05:21 +00001919 if (!IsConstructor) {
Devang Patel75b27382009-04-08 22:18:45 +00001920 if (Args.isNull() || SPTag != DW_TAG_subroutine_type)
Devang Patel9ac08d62009-02-27 18:05:21 +00001921 AddType(DW_Unit, SPDie, SPTy);
1922 else
1923 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
1924 }
Devang Pateld234e592009-01-30 01:21:46 +00001925
Devang Pateld5863dd2009-02-02 17:51:41 +00001926 if (!SP.isDefinition()) {
1927 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
1928 // Add arguments.
1929 // Do not add arguments for subprogram definition. They will be
1930 // handled through RecordVariable.
Devang Patel75b27382009-04-08 22:18:45 +00001931 if (SPTag == DW_TAG_subroutine_type)
Devang Pateld5863dd2009-02-02 17:51:41 +00001932 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1933 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1934 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
1935 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
1936 SPDie->AddChild(Arg);
1937 }
1938 }
Devang Pateld234e592009-01-30 01:21:46 +00001939
Devang Patel002ec142009-02-24 00:52:19 +00001940 unsigned Lang = SP.getCompileUnit().getLanguage();
1941 if (Lang == DW_LANG_C99 || Lang == DW_LANG_C89
1942 || Lang == DW_LANG_ObjC)
1943 AddUInt(SPDie, DW_AT_prototyped, DW_FORM_flag, 1);
1944
Devang Patelf193ff02009-01-15 19:26:23 +00001945 if (!SP.isLocalToUnit())
Devang Pateld234e592009-01-30 01:21:46 +00001946 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Devang Patel1be3ecc2009-04-15 00:10:26 +00001947
1948 // DW_TAG_inlined_subroutine may refer to this DIE.
1949 DIE *&Slot = DW_Unit->getDieMapSlotFor(SP.getGV());
1950 Slot = SPDie;
Devang Patel5aac3d32009-01-17 08:01:33 +00001951 return SPDie;
Devang Patel86ae1422009-01-05 18:59:44 +00001952 }
1953
Devang Patel5aac3d32009-01-17 08:01:33 +00001954 /// FindCompileUnit - Get the compile unit for the given descriptor.
1955 ///
Devang Patel8526cc02009-01-05 22:35:52 +00001956 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
Evan Chenge3d42322009-02-25 07:04:34 +00001957 CompileUnit *DW_Unit = CompileUnitMap[Unit.getGV()];
Devang Patel8526cc02009-01-05 22:35:52 +00001958 assert(DW_Unit && "Missing compile unit.");
1959 return DW_Unit;
1960 }
1961
Devang Patelbbdc8202009-01-13 23:54:55 +00001962 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001963 ///
1964 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1965 // Get the descriptor.
Devang Patel99ec3532009-01-16 19:28:14 +00001966 const DIVariable &VD = DV->getVariable();
Devang Patel7a6e5a32009-01-08 02:33:41 +00001967
1968 // Translate tag to proper Dwarf tag. The result variable is dropped for
1969 // now.
1970 unsigned Tag;
Devang Patel99ec3532009-01-16 19:28:14 +00001971 switch (VD.getTag()) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001972 case DW_TAG_return_variable: return NULL;
1973 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1974 case DW_TAG_auto_variable: // fall thru
1975 default: Tag = DW_TAG_variable; break;
1976 }
1977
1978 // Define variable debug information entry.
1979 DIE *VariableDie = new DIE(Tag);
Bill Wendling0582ae92009-03-13 04:39:26 +00001980 std::string Name;
1981 VD.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001982 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Devang Patel7a6e5a32009-01-08 02:33:41 +00001983
1984 // Add source line info if available.
Devang Patel99ec3532009-01-16 19:28:14 +00001985 AddSourceLine(VariableDie, &VD);
Devang Patel7a6e5a32009-01-08 02:33:41 +00001986
1987 // Add variable type.
Devang Patel99ec3532009-01-16 19:28:14 +00001988 AddType(Unit, VariableDie, VD.getType());
Devang Patel7a6e5a32009-01-08 02:33:41 +00001989
1990 // Add variable address.
1991 MachineLocation Location;
1992 Location.set(RI->getFrameRegister(*MF),
1993 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
1994 AddAddress(VariableDie, DW_AT_location, Location);
1995
1996 return VariableDie;
1997 }
1998
Devang Patel7a6e5a32009-01-08 02:33:41 +00001999 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2000 ///
2001 DbgScope *getOrCreateScope(GlobalVariable *V) {
2002 DbgScope *&Slot = DbgScopeMap[V];
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002003 if (Slot) return Slot;
2004
Devang Patel1be3ecc2009-04-15 00:10:26 +00002005 DbgScope *Parent = NULL;
2006 DIBlock Block(V);
2007 if (!Block.isNull()) {
2008 DIDescriptor ParentDesc = Block.getContext();
2009 Parent =
2010 ParentDesc.isNull() ? NULL : getOrCreateScope(ParentDesc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002011 }
Devang Patel1be3ecc2009-04-15 00:10:26 +00002012 Slot = new DbgScope(Parent, DIDescriptor(V));
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002013
Devang Patel1be3ecc2009-04-15 00:10:26 +00002014 if (Parent)
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002015 Parent->AddScope(Slot);
Devang Patel1be3ecc2009-04-15 00:10:26 +00002016 else
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002017 // First function is top level function.
Devang Patel7d2f9722009-04-15 20:41:31 +00002018 FunctionDbgScope = Slot;
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002019
Devang Patel7a6e5a32009-01-08 02:33:41 +00002020 return Slot;
2021 }
2022
Devang Patel1be3ecc2009-04-15 00:10:26 +00002023 /// createInlinedSubroutineScope - Returns the scope associated with the
2024 /// inlined subroutine.
2025 ///
2026 DbgScope *createInlinedSubroutineScope(DISubprogram SP, unsigned Src,
2027 unsigned Line, unsigned Col) {
2028 DbgScope *Scope =
2029 new DbgInlinedSubroutineScope(NULL, SP, Src, Line, Col);
2030
2031 // FIXME - Add inlined function scopes to the root so we can delete them
2032 // later.
Devang Patel7d2f9722009-04-15 20:41:31 +00002033 assert (FunctionDbgScope && "Function scope info missing!");
2034 FunctionDbgScope->AddScope(Scope);
Devang Patel1be3ecc2009-04-15 00:10:26 +00002035 return Scope;
2036 }
2037
Devang Patel7a6e5a32009-01-08 02:33:41 +00002038 /// ConstructDbgScope - Construct the components of a scope.
2039 ///
2040 void ConstructDbgScope(DbgScope *ParentScope,
2041 unsigned ParentStartID, unsigned ParentEndID,
2042 DIE *ParentDie, CompileUnit *Unit) {
2043 // Add variables to scope.
Devang Patel9795da52009-01-10 02:42:49 +00002044 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002045 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2046 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2047 if (VariableDie) ParentDie->AddChild(VariableDie);
2048 }
2049
2050 // Add nested scopes.
Devang Patel9795da52009-01-10 02:42:49 +00002051 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002052 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2053 // Define the Scope debug information entry.
2054 DbgScope *Scope = Scopes[j];
Devang Patel7a6e5a32009-01-08 02:33:41 +00002055
Devang Patele9bfb0f2009-01-12 18:41:00 +00002056 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2057 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002058
Devang Patel1be3ecc2009-04-15 00:10:26 +00002059 // Ignore empty scopes.
2060 // Do not ignore inlined scope even if it does not have any
2061 // variables or scopes.
Devang Patel7a6e5a32009-01-08 02:33:41 +00002062 if (StartID == EndID && StartID != 0) continue;
Devang Patel1be3ecc2009-04-15 00:10:26 +00002063 if (!Scope->isInlinedSubroutine()
Devang Patel0f7fef32009-04-13 17:02:03 +00002064 && Scope->getScopes().empty() && Scope->getVariables().empty())
2065 continue;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002066
2067 if (StartID == ParentStartID && EndID == ParentEndID) {
2068 // Just add stuff to the parent scope.
2069 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2070 } else {
Devang Patel1be3ecc2009-04-15 00:10:26 +00002071 DIE *ScopeDie = NULL;
Devang Patelc782ad02009-04-15 19:42:57 +00002072 if (MainCU && TAI->doesDwarfUsesInlineInfoSection()
2073 && Scope->isInlinedSubroutine()) {
Devang Patel1be3ecc2009-04-15 00:10:26 +00002074 ScopeDie = new DIE(DW_TAG_inlined_subroutine);
2075 DIE *Origin = MainCU->getDieMapSlotFor(Scope->getDesc().getGV());
2076 AddDIEntry(ScopeDie, DW_AT_abstract_origin, DW_FORM_ref4, Origin);
2077 AddUInt(ScopeDie, DW_AT_call_file, 0, Scope->getFile());
2078 AddUInt(ScopeDie, DW_AT_call_line, 0, Scope->getLine());
2079 AddUInt(ScopeDie, DW_AT_call_column, 0, Scope->getColumn());
Bill Wendling7fa81622009-05-01 08:25:13 +00002080 } else {
Devang Patel1be3ecc2009-04-15 00:10:26 +00002081 ScopeDie = new DIE(DW_TAG_lexical_block);
Bill Wendling7fa81622009-05-01 08:25:13 +00002082 }
2083
2084 // Add the scope bounds.
2085 if (StartID)
2086 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2087 DWLabel("label", StartID));
2088 else
2089 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2090 DWLabel("func_begin", SubprogramCount));
2091
2092 if (EndID)
2093 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2094 DWLabel("label", EndID));
2095 else
2096 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2097 DWLabel("func_end", SubprogramCount));
2098
2099 // Add the scope contents.
2100 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2101 ParentDie->AddChild(ScopeDie);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002102 }
2103 }
2104 }
2105
Devang Patel7d2f9722009-04-15 20:41:31 +00002106 /// ConstructFunctionDbgScope - Construct the scope for the subprogram.
Devang Patel7a6e5a32009-01-08 02:33:41 +00002107 ///
Devang Patel7d2f9722009-04-15 20:41:31 +00002108 void ConstructFunctionDbgScope(DbgScope *RootScope) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00002109 // Exit if there is no root scope.
2110 if (!RootScope) return;
Devang Patel0e5200f2009-01-15 18:25:17 +00002111 DIDescriptor Desc = RootScope->getDesc();
2112 if (Desc.isNull())
2113 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002114
2115 // Get the subprogram debug information entry.
Devang Patel0e5200f2009-01-15 18:25:17 +00002116 DISubprogram SPD(Desc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002117
2118 // Get the compile unit context.
Devang Pateldd9db662009-01-30 18:20:31 +00002119 CompileUnit *Unit = MainCU;
2120 if (!Unit)
2121 Unit = FindCompileUnit(SPD.getCompileUnit());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002122
2123 // Get the subprogram die.
Devang Patelf6bac3e2009-01-12 22:58:14 +00002124 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002125 assert(SPDie && "Missing subprogram descriptor");
2126
2127 // Add the function bounds.
2128 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2129 DWLabel("func_begin", SubprogramCount));
2130 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2131 DWLabel("func_end", SubprogramCount));
2132 MachineLocation Location(RI->getFrameRegister(*MF));
2133 AddAddress(SPDie, DW_AT_frame_base, Location);
2134
2135 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2136 }
2137
2138 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2139 ///
2140 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Chenge3d42322009-02-25 07:04:34 +00002141 const char *FnName = MF->getFunction()->getNameStart();
2142 if (MainCU) {
Bill Wendling972bbac2009-04-09 21:49:15 +00002143 StringMap<DIE*> &Globals = MainCU->getGlobals();
2144 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Chenge3d42322009-02-25 07:04:34 +00002145 if (GI != Globals.end()) {
2146 DIE *SPDie = GI->second;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002147
2148 // Add the function bounds.
2149 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2150 DWLabel("func_begin", SubprogramCount));
2151 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2152 DWLabel("func_end", SubprogramCount));
2153
2154 MachineLocation Location(RI->getFrameRegister(*MF));
2155 AddAddress(SPDie, DW_AT_frame_base, Location);
2156 return;
2157 }
Evan Chenge3d42322009-02-25 07:04:34 +00002158 } else {
2159 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2160 CompileUnit *Unit = CompileUnits[i];
Bill Wendling972bbac2009-04-09 21:49:15 +00002161 StringMap<DIE*> &Globals = Unit->getGlobals();
2162 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Chenge3d42322009-02-25 07:04:34 +00002163 if (GI != Globals.end()) {
2164 DIE *SPDie = GI->second;
2165
2166 // Add the function bounds.
2167 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2168 DWLabel("func_begin", SubprogramCount));
2169 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2170 DWLabel("func_end", SubprogramCount));
2171
2172 MachineLocation Location(RI->getFrameRegister(*MF));
2173 AddAddress(SPDie, DW_AT_frame_base, Location);
2174 return;
2175 }
2176 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00002177 }
Evan Chenge3d42322009-02-25 07:04:34 +00002178
Devang Patel7a6e5a32009-01-08 02:33:41 +00002179#if 0
2180 // FIXME: This is causing an abort because C++ mangled names are compared
2181 // with their unmangled counterparts. See PR2885. Don't do this assert.
2182 assert(0 && "Couldn't find DIE for machine function!");
2183#endif
Evan Chenge3d42322009-02-25 07:04:34 +00002184 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002185 }
2186
Jim Laskeyef42a012006-11-02 20:12:39 +00002187 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2188 /// tools to recognize the object file contains Dwarf information.
2189 void EmitInitial() {
2190 // Check to see if we already emitted intial headers.
2191 if (didInitial) return;
2192 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002193
Jim Laskeyef42a012006-11-02 20:12:39 +00002194 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002195 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002196 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002197 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002198 }
2199 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2200 EmitLabel("section_info", 0);
2201 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2202 EmitLabel("section_abbrev", 0);
2203 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2204 EmitLabel("section_aranges", 0);
Scott Michel210de722009-01-26 22:32:51 +00002205 if (TAI->doesSupportMacInfoSection()) {
2206 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2207 EmitLabel("section_macinfo", 0);
2208 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002209 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2210 EmitLabel("section_line", 0);
2211 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2212 EmitLabel("section_loc", 0);
2213 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2214 EmitLabel("section_pubnames", 0);
2215 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2216 EmitLabel("section_str", 0);
2217 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2218 EmitLabel("section_ranges", 0);
2219
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002220 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002221 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002222 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002223 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002224 }
2225
Jim Laskey65195462006-10-30 13:35:07 +00002226 /// EmitDIE - Recusively Emits a debug information entry.
2227 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002228 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002229 // Get the abbreviation for this DIE.
2230 unsigned AbbrevNumber = Die->getAbbrevNumber();
2231 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002232
Jim Laskeybacd3042007-02-21 22:48:45 +00002233 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002234
2235 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002236 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002237
Evan Cheng42bf74b2009-03-25 01:47:28 +00002238 if (Asm->isVerbose())
Evan Cheng6547e402008-07-01 23:18:29 +00002239 Asm->EOL(std::string("Abbrev [" +
2240 utostr(AbbrevNumber) +
2241 "] 0x" + utohexstr(Die->getOffset()) +
2242 ":0x" + utohexstr(Die->getSize()) + " " +
2243 TagString(Abbrev->getTag())));
2244 else
2245 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002246
Owen Anderson873e1b52008-06-24 21:44:59 +00002247 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2248 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002249
Jim Laskeyef42a012006-11-02 20:12:39 +00002250 // Emit the DIE attribute values.
2251 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2252 unsigned Attr = AbbrevData[i].getAttribute();
2253 unsigned Form = AbbrevData[i].getForm();
2254 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002255
Jim Laskeyef42a012006-11-02 20:12:39 +00002256 switch (Attr) {
2257 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002258 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002259 break;
2260 }
2261 default: {
2262 // Emit an attribute using the defined form.
2263 Values[i]->EmitValue(*this, Form);
2264 break;
2265 }
2266 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002267
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002268 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002269 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002270
Jim Laskeyef42a012006-11-02 20:12:39 +00002271 // Emit the DIE children if any.
2272 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2273 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002274
Jim Laskeyef42a012006-11-02 20:12:39 +00002275 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2276 EmitDIE(Children[j]);
2277 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002278
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002279 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002280 }
2281 }
2282
Jim Laskey65195462006-10-30 13:35:07 +00002283 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2284 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002285 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2286 // Get the children.
2287 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002288
Jim Laskeyef42a012006-11-02 20:12:39 +00002289 // If not last sibling and has children then add sibling offset attribute.
2290 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2291
2292 // Record the abbreviation.
2293 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002294
Jim Laskeyef42a012006-11-02 20:12:39 +00002295 // Get the abbreviation for this DIE.
2296 unsigned AbbrevNumber = Die->getAbbrevNumber();
2297 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2298
2299 // Set DIE offset
2300 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002301
Jim Laskeyef42a012006-11-02 20:12:39 +00002302 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002303 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2304
Owen Anderson873e1b52008-06-24 21:44:59 +00002305 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2306 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002307
2308 // Size the DIE attribute values.
2309 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2310 // Size attribute value.
2311 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2312 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002313
Jim Laskeyef42a012006-11-02 20:12:39 +00002314 // Size the DIE children if any.
2315 if (!Children.empty()) {
2316 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2317 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002318
Jim Laskeyef42a012006-11-02 20:12:39 +00002319 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2320 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2321 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002322
Jim Laskeyef42a012006-11-02 20:12:39 +00002323 // End of children marker.
2324 Offset += sizeof(int8_t);
2325 }
2326
2327 Die->setSize(Offset - Die->getOffset());
2328 return Offset;
2329 }
Jim Laskey65195462006-10-30 13:35:07 +00002330
2331 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2332 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002333 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002334 // Process base compile unit.
Devang Pateldd9db662009-01-30 18:20:31 +00002335 if (MainCU) {
2336 // Compute size of compile unit header
2337 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2338 sizeof(int16_t) + // DWARF version number
2339 sizeof(int32_t) + // Offset Into Abbrev. Section
2340 sizeof(int8_t); // Pointer Size (in bytes)
2341 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
2342 return;
2343 }
Evan Chenge3d42322009-02-25 07:04:34 +00002344 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2345 CompileUnit *Unit = CompileUnits[i];
Devang Patel72b66352009-01-12 23:05:55 +00002346 // Compute size of compile unit header
2347 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2348 sizeof(int16_t) + // DWARF version number
2349 sizeof(int32_t) + // Offset Into Abbrev. Section
2350 sizeof(int8_t); // Pointer Size (in bytes)
2351 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2352 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002353 }
2354
Evan Chenge3d42322009-02-25 07:04:34 +00002355 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Jim Laskey65195462006-10-30 13:35:07 +00002356 ///
Evan Chenge3d42322009-02-25 07:04:34 +00002357 void EmitDebugInfoPerCU(CompileUnit *Unit) {
2358 DIE *Die = Unit->getDie();
2359 // Emit the compile units header.
2360 EmitLabel("info_begin", Unit->getID());
2361 // Emit size of content not including length itself
2362 unsigned ContentSize = Die->getSize() +
2363 sizeof(int16_t) + // DWARF version number
2364 sizeof(int32_t) + // Offset Into Abbrev. Section
2365 sizeof(int8_t) + // Pointer Size (in bytes)
2366 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2367
2368 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2369 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2370 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2371 Asm->EOL("Offset Into Abbrev. Section");
2372 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2373
2374 EmitDIE(Die);
2375 // FIXME - extra padding for gdb bug.
2376 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2377 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2378 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2379 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2380 EmitLabel("info_end", Unit->getID());
2381
2382 Asm->EOL();
2383 }
2384
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002385 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002386 // Start debug info section.
2387 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002388
Evan Chenge3d42322009-02-25 07:04:34 +00002389 if (MainCU) {
2390 EmitDebugInfoPerCU(MainCU);
2391 return;
Devang Patel72b66352009-01-12 23:05:55 +00002392 }
Evan Chenge3d42322009-02-25 07:04:34 +00002393
2394 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2395 EmitDebugInfoPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002396 }
2397
Jim Laskey65195462006-10-30 13:35:07 +00002398 /// EmitAbbreviations - Emit the abbreviation section.
2399 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002400 void EmitAbbreviations() const {
2401 // Check to see if it is worth the effort.
2402 if (!Abbreviations.empty()) {
2403 // Start the debug abbrev section.
2404 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002405
Jim Laskeyef42a012006-11-02 20:12:39 +00002406 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002407
Jim Laskeyef42a012006-11-02 20:12:39 +00002408 // For each abbrevation.
2409 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2410 // Get abbreviation data
2411 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002412
Jim Laskeyef42a012006-11-02 20:12:39 +00002413 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002414 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2415 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002416
Jim Laskeyef42a012006-11-02 20:12:39 +00002417 // Emit the abbreviations data.
2418 Abbrev->Emit(*this);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002419
Jim Laskeybacd3042007-02-21 22:48:45 +00002420 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002421 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002422
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002423 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00002424 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002425
Jim Laskeyef42a012006-11-02 20:12:39 +00002426 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002427
Jim Laskeybacd3042007-02-21 22:48:45 +00002428 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002429 }
2430 }
2431
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002432 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2433 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002434 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002435 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2436 // Define last address of section.
2437 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2438 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2439 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2440 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2441
2442 // Mark end of matrix.
2443 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2444 Asm->EmitULEB128Bytes(1); Asm->EOL();
2445 Asm->EmitInt8(1); Asm->EOL();
2446 }
2447
Jim Laskey65195462006-10-30 13:35:07 +00002448 /// EmitDebugLines - Emit source line information.
2449 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002450 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002451 // If the target is using .loc/.file, the assembler will be emitting the
2452 // .debug_line table automatically.
2453 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00002454 return;
2455
Jim Laskeyef42a012006-11-02 20:12:39 +00002456 // Minimum line delta, thus ranging from -10..(255-10).
2457 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2458 // Maximum line delta, thus ranging from -10..(255-10).
2459 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002460
Jim Laskeyef42a012006-11-02 20:12:39 +00002461 // Start the dwarf line section.
2462 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002463
Jim Laskeyef42a012006-11-02 20:12:39 +00002464 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002465
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002466 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002467 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002468 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002469
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002470 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002471
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002472 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002473 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002474 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002475
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002476 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002477
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002478 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002479
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002480 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002481
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002482 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002483
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002484 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002485
Jim Laskeyef42a012006-11-02 20:12:39 +00002486 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002487 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2488 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2489 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2490 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2491 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2492 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2493 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2494 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2495 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00002496
Jim Laskeyef42a012006-11-02 20:12:39 +00002497 // Emit directories.
Evan Chenge3d42322009-02-25 07:04:34 +00002498 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2499 Asm->EmitString(getSourceDirectoryName(DI));
2500 Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00002501 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002502 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002503
Jim Laskeyef42a012006-11-02 20:12:39 +00002504 // Emit files.
Evan Chenge3d42322009-02-25 07:04:34 +00002505 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2506 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002507 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Evan Chenge3d42322009-02-25 07:04:34 +00002508 Asm->EmitString(getSourceFileName(Id.second));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002509 Asm->EOL("Source");
Evan Chenge3d42322009-02-25 07:04:34 +00002510 Asm->EmitULEB128Bytes(Id.first);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002511 Asm->EOL("Directory #");
2512 Asm->EmitULEB128Bytes(0);
2513 Asm->EOL("Mod date");
2514 Asm->EmitULEB128Bytes(0);
2515 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002516 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002517 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002518
Jim Laskeyef42a012006-11-02 20:12:39 +00002519 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002520
Jim Laskeyef42a012006-11-02 20:12:39 +00002521 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002522 unsigned SecSrcLinesSize = SectionSourceLines.size();
2523
2524 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002525 // Isolate current sections line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00002526 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00002527
Evan Cheng42bf74b2009-03-25 01:47:28 +00002528 if (Asm->isVerbose()) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002529 const Section* S = SectionMap[j + 1];
Evan Chenge3d42322009-02-25 07:04:34 +00002530 O << '\t' << TAI->getCommentString() << " Section"
2531 << S->getName() << '\n';
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002532 } else
Evan Cheng6547e402008-07-01 23:18:29 +00002533 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002534
2535 // Dwarf assumes we start with first line of first source file.
2536 unsigned Source = 1;
2537 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002538
Jim Laskeyef42a012006-11-02 20:12:39 +00002539 // Construct rows of the address, source, line, column matrix.
2540 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patelf3ee5142009-01-12 22:54:42 +00002541 const SrcLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002542 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00002543 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002544
Evan Cheng42bf74b2009-03-25 01:47:28 +00002545 if (!Asm->isVerbose())
Evan Cheng6547e402008-07-01 23:18:29 +00002546 Asm->EOL();
Evan Chenge3d42322009-02-25 07:04:34 +00002547 else {
2548 std::pair<unsigned, unsigned> SourceID =
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002549 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Evan Chenge3d42322009-02-25 07:04:34 +00002550 O << '\t' << TAI->getCommentString() << ' '
2551 << getSourceDirectoryName(SourceID.first) << ' '
2552 << getSourceFileName(SourceID.second)
2553 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2554 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002555
2556 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002557 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00002558 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002559 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00002560 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002561
Jim Laskeyef42a012006-11-02 20:12:39 +00002562 // If change of source, then switch to the new source.
2563 if (Source != LineInfo.getSourceID()) {
2564 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002565 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2566 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00002567 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002568
Jim Laskeyef42a012006-11-02 20:12:39 +00002569 // If change of line.
2570 if (Line != LineInfo.getLine()) {
2571 // Determine offset.
2572 int Offset = LineInfo.getLine() - Line;
2573 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002574
Jim Laskeyef42a012006-11-02 20:12:39 +00002575 // Update line.
2576 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002577
Jim Laskeyef42a012006-11-02 20:12:39 +00002578 // If delta is small enough and in range...
2579 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2580 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002581 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00002582 } else {
2583 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002584 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2585 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2586 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002587 }
2588 } else {
2589 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002590 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002591 }
2592 }
2593
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002594 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002595 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002596
2597 if (SecSrcLinesSize == 0)
2598 // Because we're emitting a debug_line section, we still need a line
2599 // table. The linker and friends expect it to exist. If there's nothing to
2600 // put into it, emit an empty table.
2601 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002602
Jim Laskeyef42a012006-11-02 20:12:39 +00002603 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002604
Jim Laskeybacd3042007-02-21 22:48:45 +00002605 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002606 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002607
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002608 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00002609 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002610 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002611 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00002612 return;
2613
2614 int stackGrowth =
2615 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2616 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002617 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00002618
2619 // Start the dwarf frame section.
2620 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2621
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002622 EmitLabel("debug_frame_common", 0);
2623 EmitDifference("debug_frame_common_end", 0,
2624 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002625 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002626
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002627 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002628 Asm->EmitInt32((int)DW_CIE_ID);
2629 Asm->EOL("CIE Identifier Tag");
2630 Asm->EmitInt8(DW_CIE_VERSION);
2631 Asm->EOL("CIE Version");
2632 Asm->EmitString("");
2633 Asm->EOL("CIE Augmentation");
2634 Asm->EmitULEB128Bytes(1);
2635 Asm->EOL("CIE Code Alignment Factor");
2636 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002637 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002638 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002639 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002640
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002641 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002642 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002643
Dale Johannesenb97aec62007-11-13 19:13:01 +00002644 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002645
Evan Cheng05548eb2008-02-29 19:36:59 +00002646 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002647 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002648
Jim Laskeybacd3042007-02-21 22:48:45 +00002649 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002650 }
2651
Jim Laskey65195462006-10-30 13:35:07 +00002652 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2653 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002654 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002655 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00002656 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002657
Jim Laskeyef42a012006-11-02 20:12:39 +00002658 // Start the dwarf frame section.
2659 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002660
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002661 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2662 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002663 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002664
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002665 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002666
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002667 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2668 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002669 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002670
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002671 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002672 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002673 EmitDifference("func_end", DebugFrameInfo.Number,
2674 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002675 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002676
Devang Patel5aac3d32009-01-17 08:01:33 +00002677 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00002678 false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002679
Evan Cheng05548eb2008-02-29 19:36:59 +00002680 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002681 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00002682
Jim Laskeybacd3042007-02-21 22:48:45 +00002683 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002684 }
2685
Evan Chenge3d42322009-02-25 07:04:34 +00002686 void EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2687 EmitDifference("pubnames_end", Unit->getID(),
2688 "pubnames_begin", Unit->getID(), true);
2689 Asm->EOL("Length of Public Names Info");
2690
2691 EmitLabel("pubnames_begin", Unit->getID());
2692
2693 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2694
2695 EmitSectionOffset("info_begin", "section_info",
2696 Unit->getID(), 0, true, false);
2697 Asm->EOL("Offset of Compilation Unit Info");
2698
2699 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2700 true);
2701 Asm->EOL("Compilation Unit Length");
2702
Bill Wendling972bbac2009-04-09 21:49:15 +00002703 StringMap<DIE*> &Globals = Unit->getGlobals();
Bill Wendlingf34be822009-04-09 23:51:31 +00002704 for (StringMap<DIE*>::const_iterator
Bill Wendling972bbac2009-04-09 21:49:15 +00002705 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
Bill Wendlingf34be822009-04-09 23:51:31 +00002706 const char *Name = GI->getKeyData();
Evan Chenge3d42322009-02-25 07:04:34 +00002707 DIE * Entity = GI->second;
2708
2709 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
Bill Wendlingf34be822009-04-09 23:51:31 +00002710 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
Evan Chenge3d42322009-02-25 07:04:34 +00002711 }
2712
2713 Asm->EmitInt32(0); Asm->EOL("End Mark");
2714 EmitLabel("pubnames_end", Unit->getID());
2715
2716 Asm->EOL();
2717 }
2718
Jim Laskeyef42a012006-11-02 20:12:39 +00002719 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002720 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002721 void EmitDebugPubNames() {
2722 // Start the dwarf pubnames section.
2723 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002724
Evan Chenge3d42322009-02-25 07:04:34 +00002725 if (MainCU) {
2726 EmitDebugPubNamesPerCU(MainCU);
2727 return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002728 }
Evan Chenge3d42322009-02-25 07:04:34 +00002729
2730 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2731 EmitDebugPubNamesPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002732 }
2733
2734 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002735 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002736 void EmitDebugStr() {
2737 // Check to see if it is worth the effort.
2738 if (!StringPool.empty()) {
2739 // Start the dwarf str section.
2740 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002741
Jim Laskeyef42a012006-11-02 20:12:39 +00002742 // For each of strings in the string pool.
2743 for (unsigned StringID = 1, N = StringPool.size();
2744 StringID <= N; ++StringID) {
2745 // Emit a label for reference from debug information entries.
2746 EmitLabel("string", StringID);
2747 // Emit the string itself.
2748 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00002749 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002750 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002751
Jim Laskeybacd3042007-02-21 22:48:45 +00002752 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002753 }
2754 }
2755
2756 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002757 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002758 void EmitDebugLoc() {
2759 // Start the dwarf loc section.
2760 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002761
Jim Laskeybacd3042007-02-21 22:48:45 +00002762 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002763 }
2764
2765 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002766 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002767 void EmitDebugARanges() {
2768 // Start the dwarf aranges section.
2769 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002770
Jim Laskeyef42a012006-11-02 20:12:39 +00002771 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00002772#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002773 CompileUnit *Unit = GetBaseCompileUnit();
2774
Jim Laskey5496f012006-11-09 14:52:14 +00002775 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002776 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002777
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002778 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002779
Jim Laskey5496f012006-11-09 14:52:14 +00002780 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002781 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002782
Dan Gohman82482942007-09-27 23:12:31 +00002783 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002784
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002785 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002786
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002787 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2788 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002789
Jim Laskey5496f012006-11-09 14:52:14 +00002790 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002791 EmitReference("text_begin", 0); Asm->EOL("Address");
2792 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002793
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002794 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2795 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00002796#endif
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002797
Jim Laskeybacd3042007-02-21 22:48:45 +00002798 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002799 }
2800
2801 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002802 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002803 void EmitDebugRanges() {
2804 // Start the dwarf ranges section.
2805 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002806
Jim Laskeybacd3042007-02-21 22:48:45 +00002807 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002808 }
2809
2810 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002811 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002812 void EmitDebugMacInfo() {
Scott Michel210de722009-01-26 22:32:51 +00002813 if (TAI->doesSupportMacInfoSection()) {
2814 // Start the dwarf macinfo section.
2815 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002816
Scott Michel210de722009-01-26 22:32:51 +00002817 Asm->EOL();
2818 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002819 }
2820
Devang Patel0f7fef32009-04-13 17:02:03 +00002821 /// EmitDebugInlineInfo - Emit inline info using following format.
2822 /// Section Header:
2823 /// 1. length of section
2824 /// 2. Dwarf version number
2825 /// 3. address size.
2826 ///
2827 /// Entries (one "entry" for each function that was inlined):
2828 ///
2829 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
2830 /// otherwise offset into __debug_str for regular function name.
2831 /// 2. offset into __debug_str section for regular function name.
2832 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
2833 /// instances for the function.
2834 ///
2835 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
2836 /// inlined instance; the die_offset points to the inlined_subroutine die in
2837 /// the __debug_info section, and the low_pc is the starting address for the
2838 /// inlining instance.
2839 void EmitDebugInlineInfo() {
2840 if (!TAI->doesDwarfUsesInlineInfoSection())
2841 return;
2842
2843 if (!MainCU)
2844 return;
2845
2846 Asm->SwitchToDataSection(TAI->getDwarfDebugInlineSection());
2847 Asm->EOL();
2848 EmitDifference("debug_inlined_end", 1,
2849 "debug_inlined_begin", 1, true);
2850 Asm->EOL("Length of Debug Inlined Information Entry");
2851
2852 EmitLabel("debug_inlined_begin", 1);
2853
2854 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
2855 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2856
2857 for (DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
2858 I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2859 GlobalVariable *GV = I->first;
2860 SmallVector<unsigned, 4> &Labels = I->second;
2861 DISubprogram SP(GV);
2862 std::string Name;
2863 std::string LName;
2864
2865 SP.getLinkageName(LName);
2866 SP.getName(Name);
2867
2868 Asm->EmitString(LName.empty() ? Name : LName);
2869 Asm->EOL("MIPS linkage name");
2870
2871 Asm->EmitString(Name); Asm->EOL("Function name");
2872
2873 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2874
2875 for (SmallVector<unsigned, 4>::iterator LI = Labels.begin(),
2876 LE = Labels.end(); LI != LE; ++LI) {
2877 DIE *SP = MainCU->getDieMapSlotFor(GV);
2878 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2879
2880 if (TD->getPointerSize() == sizeof(int32_t))
2881 O << TAI->getData32bitsDirective();
2882 else
2883 O << TAI->getData64bitsDirective();
2884 PrintLabelName("label", *LI); Asm->EOL("low_pc");
2885 }
2886 }
2887
2888 EmitLabel("debug_inlined_end", 1);
2889 Asm->EOL();
2890 }
2891
Bill Wendlingc8615e42009-03-10 21:47:45 +00002892 /// GetOrCreateSourceID - Look up the source id with the given directory and
2893 /// source file names. If none currently exists, create a new id and insert it
2894 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
2895 /// as well.
2896 unsigned GetOrCreateSourceID(const std::string &DirName,
2897 const std::string &FileName) {
2898 unsigned DId;
2899 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
2900 if (DI != DirectoryIdMap.end()) {
2901 DId = DI->getValue();
2902 } else {
2903 DId = DirectoryNames.size() + 1;
2904 DirectoryIdMap[DirName] = DId;
2905 DirectoryNames.push_back(DirName);
2906 }
2907
2908 unsigned FId;
2909 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
2910 if (FI != SourceFileIdMap.end()) {
2911 FId = FI->getValue();
2912 } else {
2913 FId = SourceFileNames.size() + 1;
2914 SourceFileIdMap[FileName] = FId;
2915 SourceFileNames.push_back(FileName);
2916 }
2917
2918 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
2919 SourceIdMap.find(std::make_pair(DId, FId));
2920 if (SI != SourceIdMap.end())
2921 return SI->second;
2922
2923 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
2924 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
2925 SourceIds.push_back(std::make_pair(DId, FId));
2926
2927 return SrcId;
2928 }
2929
Evan Chenge3d42322009-02-25 07:04:34 +00002930 void ConstructCompileUnit(GlobalVariable *GV) {
2931 DICompileUnit DIUnit(GV);
Bill Wendling0582ae92009-03-13 04:39:26 +00002932 std::string Dir, FN, Prod;
2933 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
2934 DIUnit.getFilename(FN));
Evan Chenge3d42322009-02-25 07:04:34 +00002935
2936 DIE *Die = new DIE(DW_TAG_compile_unit);
2937 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2938 DWLabel("section_line", 0), DWLabel("section_line", 0),
2939 false);
Bill Wendling0582ae92009-03-13 04:39:26 +00002940 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer(Prod));
Evan Chenge3d42322009-02-25 07:04:34 +00002941 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002942 AddString(Die, DW_AT_name, DW_FORM_string, FN);
Bill Wendling0582ae92009-03-13 04:39:26 +00002943 if (!Dir.empty())
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002944 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Chenge3d42322009-02-25 07:04:34 +00002945 if (DIUnit.isOptimized())
2946 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendling0582ae92009-03-13 04:39:26 +00002947 std::string Flags;
2948 DIUnit.getFlags(Flags);
2949 if (!Flags.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00002950 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
2951 unsigned RVer = DIUnit.getRunTimeVersion();
2952 if (RVer)
2953 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
2954
2955 CompileUnit *Unit = new CompileUnit(ID, Die);
2956 if (DIUnit.isMain()) {
2957 assert(!MainCU && "Multiple main compile units are found!");
2958 MainCU = Unit;
2959 }
2960 CompileUnitMap[DIUnit.getGV()] = Unit;
2961 CompileUnits.push_back(Unit);
2962 }
2963
Devang Patelc4523242009-01-05 23:11:11 +00002964 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Pateld1ca9252009-01-05 23:03:32 +00002965 void ConstructCompileUnits() {
Evan Chenge3d42322009-02-25 07:04:34 +00002966 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.compile_units");
2967 if (!Root)
2968 return;
2969 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2970 "Malformed compile unit descriptor anchor type");
2971 Constant *RootC = cast<Constant>(*Root->use_begin());
2972 assert(RootC->hasNUsesOrMore(1) &&
2973 "Malformed compile unit descriptor anchor type");
2974 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2975 UI != UE; ++UI)
2976 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2977 UUI != UUE; ++UUI) {
2978 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2979 ConstructCompileUnit(GV);
Devang Pateldd9db662009-01-30 18:20:31 +00002980 }
Evan Chenge3d42322009-02-25 07:04:34 +00002981 }
2982
2983 bool ConstructGlobalVariableDIE(GlobalVariable *GV) {
2984 DIGlobalVariable DI_GV(GV);
2985 CompileUnit *DW_Unit = MainCU;
2986 if (!DW_Unit)
2987 DW_Unit = FindCompileUnit(DI_GV.getCompileUnit());
2988
2989 // Check for pre-existence.
2990 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
2991 if (Slot)
2992 return false;
2993
2994 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
2995
2996 // Add address.
2997 DIEBlock *Block = new DIEBlock();
2998 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Bill Wendling7d16e852009-04-10 00:12:49 +00002999 std::string GLN;
Evan Chenge3d42322009-02-25 07:04:34 +00003000 AddObjectLabel(Block, 0, DW_FORM_udata,
Bill Wendling7d16e852009-04-10 00:12:49 +00003001 Asm->getGlobalLinkName(DI_GV.getGlobal(), GLN));
Evan Chenge3d42322009-02-25 07:04:34 +00003002 AddBlock(VariableDie, DW_AT_location, 0, Block);
3003
3004 // Add to map.
3005 Slot = VariableDie;
3006 // Add to context owner.
3007 DW_Unit->getDie()->AddChild(VariableDie);
3008 // Expose as global. FIXME - need to check external flag.
Bill Wendling0582ae92009-03-13 04:39:26 +00003009 std::string Name;
3010 DW_Unit->AddGlobal(DI_GV.getName(Name), VariableDie);
Evan Chenge3d42322009-02-25 07:04:34 +00003011 return true;
Devang Pateld1ca9252009-01-05 23:03:32 +00003012 }
3013
Devang Patelc4523242009-01-05 23:11:11 +00003014 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patel53cac182009-02-24 00:02:15 +00003015 /// visible global variables. Return true if at least one global DIE is
3016 /// created.
3017 bool ConstructGlobalVariableDIEs() {
Evan Chenge3d42322009-02-25 07:04:34 +00003018 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
3019 if (!Root)
3020 return false;
Devang Patelc4523242009-01-05 23:11:11 +00003021
Evan Chenge3d42322009-02-25 07:04:34 +00003022 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3023 "Malformed global variable descriptor anchor type");
3024 Constant *RootC = cast<Constant>(*Root->use_begin());
3025 assert(RootC->hasNUsesOrMore(1) &&
3026 "Malformed global variable descriptor anchor type");
Devang Patelc4523242009-01-05 23:11:11 +00003027
Evan Chenge3d42322009-02-25 07:04:34 +00003028 bool Result = false;
3029 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3030 UI != UE; ++UI)
3031 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3032 UUI != UUE; ++UUI) {
3033 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3034 Result |= ConstructGlobalVariableDIE(GV);
3035 }
3036 return Result;
3037 }
Devang Patelc4523242009-01-05 23:11:11 +00003038
Evan Chenge3d42322009-02-25 07:04:34 +00003039 bool ConstructSubprogram(GlobalVariable *GV) {
3040 DISubprogram SP(GV);
3041 CompileUnit *Unit = MainCU;
3042 if (!Unit)
3043 Unit = FindCompileUnit(SP.getCompileUnit());
Devang Patelc4523242009-01-05 23:11:11 +00003044
Evan Chenge3d42322009-02-25 07:04:34 +00003045 // Check for pre-existence.
3046 DIE *&Slot = Unit->getDieMapSlotFor(GV);
3047 if (Slot)
3048 return false;
3049
3050 if (!SP.isDefinition())
3051 // This is a method declaration which will be handled while
3052 // constructing class type.
3053 return false;
3054
3055 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
3056
3057 // Add to map.
3058 Slot = SubprogramDie;
3059 // Add to context owner.
3060 Unit->getDie()->AddChild(SubprogramDie);
3061 // Expose as global.
Bill Wendling0582ae92009-03-13 04:39:26 +00003062 std::string Name;
3063 Unit->AddGlobal(SP.getName(Name), SubprogramDie);
Evan Chenge3d42322009-02-25 07:04:34 +00003064 return true;
Devang Patelc4523242009-01-05 23:11:11 +00003065 }
3066
Devang Patel78eb6ad2009-01-05 23:21:35 +00003067 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patel53cac182009-02-24 00:02:15 +00003068 /// subprograms. Return true if at least one subprogram DIE is created.
3069 bool ConstructSubprograms() {
Evan Chenge3d42322009-02-25 07:04:34 +00003070 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
3071 if (!Root)
3072 return false;
Devang Patel78eb6ad2009-01-05 23:21:35 +00003073
Evan Chenge3d42322009-02-25 07:04:34 +00003074 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3075 "Malformed subprogram descriptor anchor type");
3076 Constant *RootC = cast<Constant>(*Root->use_begin());
3077 assert(RootC->hasNUsesOrMore(1) &&
3078 "Malformed subprogram descriptor anchor type");
Devang Patel78eb6ad2009-01-05 23:21:35 +00003079
Evan Chenge3d42322009-02-25 07:04:34 +00003080 bool Result = false;
3081 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3082 UI != UE; ++UI)
3083 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3084 UUI != UUE; ++UUI) {
3085 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3086 Result |= ConstructSubprogram(GV);
3087 }
3088 return Result;
Devang Patel78eb6ad2009-01-05 23:21:35 +00003089 }
3090
Jim Laskey65195462006-10-30 13:35:07 +00003091public:
Jim Laskeyef42a012006-11-02 20:12:39 +00003092 //===--------------------------------------------------------------------===//
3093 // Main entry points.
3094 //
Owen Andersoncb371882008-08-21 00:14:44 +00003095 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00003096 : Dwarf(OS, A, T, "dbg"), MainCU(0),
3097 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
3098 ValuesSet(InitValuesSetSize), Values(), StringPool(), SectionMap(),
3099 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel7d2f9722009-04-15 20:41:31 +00003100 FunctionDbgScope(0), DebugTimer(0) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003101 if (TimePassesIsEnabled)
3102 DebugTimer = new Timer("Dwarf Debug Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00003103 getDwarfTimerGroup());
Jim Laskeyef42a012006-11-02 20:12:39 +00003104 }
Jim Laskey072200c2007-01-29 18:51:14 +00003105 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003106 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3107 delete Values[j];
Bill Wendling163ba3f2009-03-10 21:23:25 +00003108
3109 delete DebugTimer;
Jim Laskeyef42a012006-11-02 20:12:39 +00003110 }
3111
Bill Wendlingc8615e42009-03-10 21:47:45 +00003112 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
3113 /// be emitted.
3114 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
3115
Devang Patel5d315982009-01-06 21:07:30 +00003116 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3117 /// This is inovked by the target AsmPrinter.
Devang Patel3f7833a2009-01-12 23:09:42 +00003118 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003119 if (TimePassesIsEnabled)
3120 DebugTimer->startTimer();
3121
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003122 // Create all the compile unit DIEs.
3123 ConstructCompileUnits();
Devang Patel3f7833a2009-01-12 23:09:42 +00003124
Bill Wendling163ba3f2009-03-10 21:23:25 +00003125 if (CompileUnits.empty()) {
3126 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003127 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003128
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003129 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003130 }
Devang Patel3f7833a2009-01-12 23:09:42 +00003131
Devang Patel53cac182009-02-24 00:02:15 +00003132 // Create DIEs for each of the externally visible global variables.
3133 bool globalDIEs = ConstructGlobalVariableDIEs();
3134
3135 // Create DIEs for each of the externally visible subprograms.
3136 bool subprogramDIEs = ConstructSubprograms();
3137
3138 // If there is not any debug info available for any global variables
3139 // and any subprograms then there is not any debug info to emit.
Bill Wendling163ba3f2009-03-10 21:23:25 +00003140 if (!globalDIEs && !subprogramDIEs) {
3141 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003142 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003143
Devang Patel53cac182009-02-24 00:02:15 +00003144 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003145 }
Devang Patel53cac182009-02-24 00:02:15 +00003146
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003147 MMI = mmi;
3148 shouldEmit = true;
3149 MMI->setDebugInfoAvailability(true);
Devang Patel5d315982009-01-06 21:07:30 +00003150
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003151 // Prime section data.
3152 SectionMap.insert(TAI->getTextSection());
Devang Patel5d315982009-01-06 21:07:30 +00003153
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003154 // Print out .file directives to specify files for .loc directives. These
3155 // are printed out early so that they precede any .loc directives.
3156 if (TAI->hasDotLocAndDotFile()) {
Evan Chenge3d42322009-02-25 07:04:34 +00003157 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
3158 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00003159 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Evan Chenge3d42322009-02-25 07:04:34 +00003160 sys::Path FullPath(getSourceDirectoryName(Id.first));
3161 bool AppendOk =
3162 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003163 assert(AppendOk && "Could not append filename to directory!");
3164 AppendOk = false;
3165 Asm->EmitFile(i, FullPath.toString());
3166 Asm->EOL();
Devang Patel5d315982009-01-06 21:07:30 +00003167 }
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003168 }
Devang Patel5d315982009-01-06 21:07:30 +00003169
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003170 // Emit initial sections
3171 EmitInitial();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003172
3173 if (TimePassesIsEnabled)
3174 DebugTimer->stopTimer();
Devang Patel5d315982009-01-06 21:07:30 +00003175 }
3176
Jim Laskey65195462006-10-30 13:35:07 +00003177 /// BeginModule - Emit all Dwarf sections that should come prior to the
3178 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00003179 void BeginModule(Module *M) {
3180 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00003181 }
3182
Jim Laskey65195462006-10-30 13:35:07 +00003183 /// EndModule - Emit all Dwarf sections that should come after the content.
3184 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003185 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003186 if (!ShouldEmitDwarfDebug())
3187 return;
3188
3189 if (TimePassesIsEnabled)
3190 DebugTimer->startTimer();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003191
Jim Laskeyef42a012006-11-02 20:12:39 +00003192 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003193 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003194 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00003195 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003196 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003197
Jim Laskeyef42a012006-11-02 20:12:39 +00003198 // End text sections.
3199 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003200 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00003201 EmitLabel("section_end", i);
3202 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003203
3204 // Emit common frame information.
3205 EmitCommonDebugFrame();
3206
3207 // Emit function debug frame information
3208 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3209 E = DebugFrames.end(); I != E; ++I)
3210 EmitFunctionDebugFrame(*I);
3211
Jim Laskeyef42a012006-11-02 20:12:39 +00003212 // Compute DIE offsets and sizes.
3213 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003214
Jim Laskeyef42a012006-11-02 20:12:39 +00003215 // Emit all the DIEs into a debug info section
3216 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003217
Jim Laskeyef42a012006-11-02 20:12:39 +00003218 // Corresponding abbreviations into a abbrev section.
3219 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003220
Jim Laskeyef42a012006-11-02 20:12:39 +00003221 // Emit source line correspondence into a debug line section.
3222 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003223
Jim Laskeyef42a012006-11-02 20:12:39 +00003224 // Emit info into a debug pubnames section.
3225 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003226
Jim Laskeyef42a012006-11-02 20:12:39 +00003227 // Emit info into a debug str section.
3228 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003229
Jim Laskeyef42a012006-11-02 20:12:39 +00003230 // Emit info into a debug loc section.
3231 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003232
Jim Laskeyef42a012006-11-02 20:12:39 +00003233 // Emit info into a debug aranges section.
3234 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003235
Jim Laskeyef42a012006-11-02 20:12:39 +00003236 // Emit info into a debug ranges section.
3237 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003238
Jim Laskeyef42a012006-11-02 20:12:39 +00003239 // Emit info into a debug macinfo section.
3240 EmitDebugMacInfo();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003241
Devang Patel0f7fef32009-04-13 17:02:03 +00003242 // Emit inline info.
3243 EmitDebugInlineInfo();
3244
Bill Wendling163ba3f2009-03-10 21:23:25 +00003245 if (TimePassesIsEnabled)
3246 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003247 }
3248
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003249 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00003250 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00003251 void BeginFunction(MachineFunction *MF) {
Bill Wendling1362f972009-03-11 00:03:50 +00003252 this->MF = MF;
3253
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003254 if (!ShouldEmitDwarfDebug()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00003255
Bill Wendling163ba3f2009-03-10 21:23:25 +00003256 if (TimePassesIsEnabled)
3257 DebugTimer->startTimer();
3258
Jim Laskeyef42a012006-11-02 20:12:39 +00003259 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003260 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003261
Jim Laskeyef42a012006-11-02 20:12:39 +00003262 // Assumes in correct section after the entry point.
3263 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00003264
Argyrios Kyrtzidis28f14122009-05-04 19:23:45 +00003265 // Emit label for the implicitly defined dbg.stoppoint at the start of
3266 // the function.
Mike Stumpfe095f32009-05-04 18:40:41 +00003267 DebugLoc FDL = MF->getDefaultDebugLoc();
3268 if (!FDL.isUnknown()) {
3269 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
3270 unsigned LabelID = RecordSourceLine(DLT.Line, DLT.Col,
3271 DICompileUnit(DLT.CompileUnit));
3272 Asm->printLabel(LabelID);
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00003273 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003274
3275 if (TimePassesIsEnabled)
3276 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003277 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003278
Jim Laskey65195462006-10-30 13:35:07 +00003279 /// EndFunction - Gather and emit post-function debug information.
3280 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00003281 void EndFunction(MachineFunction *MF) {
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003282 if (!ShouldEmitDwarfDebug()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003283
Bill Wendling163ba3f2009-03-10 21:23:25 +00003284 if (TimePassesIsEnabled)
3285 DebugTimer->startTimer();
3286
Jim Laskeyb82313f2007-02-01 16:31:34 +00003287 // Define end label for subprogram.
3288 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003289
Jim Laskeyef42a012006-11-02 20:12:39 +00003290 // Get function line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00003291 if (!Lines.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003292 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003293 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00003294 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patelf3ee5142009-01-12 22:54:42 +00003295 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Jim Laskeyef42a012006-11-02 20:12:39 +00003296 // Append the function info to section info.
3297 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patelf3ee5142009-01-12 22:54:42 +00003298 Lines.begin(), Lines.end());
Jim Laskeyef42a012006-11-02 20:12:39 +00003299 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003300
Jim Laskeyef42a012006-11-02 20:12:39 +00003301 // Construct scopes for subprogram.
Devang Patel7d2f9722009-04-15 20:41:31 +00003302 if (FunctionDbgScope)
3303 ConstructFunctionDbgScope(FunctionDbgScope);
Bill Wendlingd751c642008-09-26 00:28:12 +00003304 else
3305 // FIXME: This is wrong. We are essentially getting past a problem with
3306 // debug information not being able to handle unreachable blocks that have
3307 // debug information in them. In particular, those unreachable blocks that
3308 // have "region end" info in them. That situation results in the "root
3309 // scope" not being created. If that's the case, then emit a "default"
3310 // scope, i.e., one that encompasses the whole function. This isn't
3311 // desirable. And a better way of handling this (and all of the debugging
3312 // information) needs to be explored.
Devang Patel7bb89ed2009-01-13 00:20:51 +00003313 ConstructDefaultDbgScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003314
3315 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3316 MMI->getFrameMoves()));
Devang Patel481ff5b2009-01-12 18:48:36 +00003317
3318 // Clear debug info
Devang Patel7d2f9722009-04-15 20:41:31 +00003319 if (FunctionDbgScope) {
3320 delete FunctionDbgScope;
Devang Patel481ff5b2009-01-12 18:48:36 +00003321 DbgScopeMap.clear();
Devang Patel1be3ecc2009-04-15 00:10:26 +00003322 DbgInlinedScopeMap.clear();
3323 InlinedVariableScopes.clear();
Devang Patel7d2f9722009-04-15 20:41:31 +00003324 FunctionDbgScope = NULL;
Devang Patel481ff5b2009-01-12 18:48:36 +00003325 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003326
Bill Wendling163ba3f2009-03-10 21:23:25 +00003327 Lines.clear();
3328
3329 if (TimePassesIsEnabled)
3330 DebugTimer->stopTimer();
3331 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003332
3333 /// RecordSourceLine - Records location information and associates it with a
3334 /// label. Returns a unique label ID used to generate a label and provide
3335 /// correspondence to the source line list.
3336 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003337 if (TimePassesIsEnabled)
3338 DebugTimer->startTimer();
3339
Evan Chenge3d42322009-02-25 07:04:34 +00003340 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003341 assert(Unit && "Unable to find CompileUnit");
Devang Patelccca7fe2009-01-12 19:17:34 +00003342 unsigned ID = MMI->NextLabelID();
3343 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003344
3345 if (TimePassesIsEnabled)
3346 DebugTimer->stopTimer();
3347
Devang Patelccca7fe2009-01-12 19:17:34 +00003348 return ID;
3349 }
3350
3351 /// RecordSourceLine - Records location information and associates it with a
3352 /// label. Returns a unique label ID used to generate a label and provide
3353 /// correspondence to the source line list.
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00003354 unsigned RecordSourceLine(unsigned Line, unsigned Col, DICompileUnit CU) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003355 if (TimePassesIsEnabled)
3356 DebugTimer->startTimer();
3357
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00003358 std::string Dir, Fn;
3359 unsigned Src = GetOrCreateSourceID(CU.getDirectory(Dir),
3360 CU.getFilename(Fn));
Devang Patelccca7fe2009-01-12 19:17:34 +00003361 unsigned ID = MMI->NextLabelID();
3362 Lines.push_back(SrcLineInfo(Line, Col, Src, 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
Bill Wendlingc8615e42009-03-10 21:47:45 +00003370 /// getRecordSourceLineCount - Return the number of source lines in the debug
3371 /// info.
3372 unsigned getRecordSourceLineCount() const {
Devang Patelccca7fe2009-01-12 19:17:34 +00003373 return Lines.size();
3374 }
3375
Bill Wendlingc8615e42009-03-10 21:47:45 +00003376 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
3377 /// timed. Look up the source id with the given directory and source file
3378 /// names. If none currently exists, create a new id and insert it in the
3379 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
3380 /// well.
Evan Chenge3d42322009-02-25 07:04:34 +00003381 unsigned getOrCreateSourceID(const std::string &DirName,
3382 const std::string &FileName) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003383 if (TimePassesIsEnabled)
3384 DebugTimer->startTimer();
3385
Bill Wendlingc8615e42009-03-10 21:47:45 +00003386 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003387
3388 if (TimePassesIsEnabled)
3389 DebugTimer->stopTimer();
3390
Evan Chenge3d42322009-02-25 07:04:34 +00003391 return SrcId;
Devang Patelccca7fe2009-01-12 19:17:34 +00003392 }
3393
3394 /// RecordRegionStart - Indicate the start of a region.
Devang Patelccca7fe2009-01-12 19:17:34 +00003395 unsigned RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003396 if (TimePassesIsEnabled)
3397 DebugTimer->startTimer();
3398
Devang Patelccca7fe2009-01-12 19:17:34 +00003399 DbgScope *Scope = getOrCreateScope(V);
3400 unsigned ID = MMI->NextLabelID();
3401 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003402
3403 if (TimePassesIsEnabled)
3404 DebugTimer->stopTimer();
3405
Devang Patelccca7fe2009-01-12 19:17:34 +00003406 return ID;
3407 }
3408
3409 /// RecordRegionEnd - Indicate the end of a region.
Devang Patelccca7fe2009-01-12 19:17:34 +00003410 unsigned RecordRegionEnd(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003411 if (TimePassesIsEnabled)
3412 DebugTimer->startTimer();
3413
Devang Patelccca7fe2009-01-12 19:17:34 +00003414 DbgScope *Scope = getOrCreateScope(V);
3415 unsigned ID = MMI->NextLabelID();
3416 Scope->setEndLabelID(ID);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003417
3418 if (TimePassesIsEnabled)
3419 DebugTimer->stopTimer();
3420
Devang Patelccca7fe2009-01-12 19:17:34 +00003421 return ID;
3422 }
3423
3424 /// RecordVariable - Indicate the declaration of a local variable.
Devang Patel1be3ecc2009-04-15 00:10:26 +00003425 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
3426 const MachineInstr *MI) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003427 if (TimePassesIsEnabled)
3428 DebugTimer->startTimer();
3429
Devang Patel0e5200f2009-01-15 18:25:17 +00003430 DIDescriptor Desc(GV);
3431 DbgScope *Scope = NULL;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003432
Devang Patel0e5200f2009-01-15 18:25:17 +00003433 if (Desc.getTag() == DW_TAG_variable) {
3434 // GV is a global variable.
3435 DIGlobalVariable DG(GV);
3436 Scope = getOrCreateScope(DG.getContext().getGV());
3437 } else {
Devang Patel1be3ecc2009-04-15 00:10:26 +00003438 DenseMap<const MachineInstr *, DbgScope *>::iterator
3439 SI = InlinedVariableScopes.find(MI);
3440 if (SI != InlinedVariableScopes.end()) {
3441 // or GV is an inlined local variable.
3442 Scope = SI->second;
3443 } else {
Devang Patel0e5200f2009-01-15 18:25:17 +00003444 // or GV is a local variable.
Devang Patel1be3ecc2009-04-15 00:10:26 +00003445 DIVariable DV(GV);
3446 Scope = getOrCreateScope(DV.getContext().getGV());
3447 }
Devang Patel0e5200f2009-01-15 18:25:17 +00003448 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003449
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003450 assert(Scope && "Unable to find variable' scope");
Devang Patel99ec3532009-01-16 19:28:14 +00003451 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelccca7fe2009-01-12 19:17:34 +00003452 Scope->AddVariable(DV);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003453
3454 if (TimePassesIsEnabled)
3455 DebugTimer->stopTimer();
Devang Patelccca7fe2009-01-12 19:17:34 +00003456 }
Devang Patel0f7fef32009-04-13 17:02:03 +00003457
Devang Patel1be3ecc2009-04-15 00:10:26 +00003458 //// RecordInlinedFnStart - Indicate the start of inlined subroutine.
3459 void RecordInlinedFnStart(Instruction *FSI, DISubprogram &SP, unsigned LabelID,
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00003460 DICompileUnit CU, unsigned Line, unsigned Col) {
Devang Patel1be3ecc2009-04-15 00:10:26 +00003461 if (!TAI->doesDwarfUsesInlineInfoSection())
3462 return;
3463
Bill Wendlingd5a63812009-05-01 08:40:06 +00003464 if (TimePassesIsEnabled)
3465 DebugTimer->startTimer();
3466
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00003467 std::string Dir, Fn;
3468 unsigned Src = GetOrCreateSourceID(CU.getDirectory(Dir),
3469 CU.getFilename(Fn));
Devang Patel1be3ecc2009-04-15 00:10:26 +00003470 DbgScope *Scope = createInlinedSubroutineScope(SP, Src, Line, Col);
3471 Scope->setStartLabelID(LabelID);
Devang Patel0f7fef32009-04-13 17:02:03 +00003472 MMI->RecordUsedDbgLabel(LabelID);
Devang Patel1be3ecc2009-04-15 00:10:26 +00003473 GlobalVariable *GV = SP.getGV();
3474
3475 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3476 SI = DbgInlinedScopeMap.find(GV);
Bill Wendlingef956fc2009-05-01 08:32:14 +00003477
3478 if (SI == DbgInlinedScopeMap.end())
3479 DbgInlinedScopeMap[GV].push_back(Scope);
3480 else
3481 SI->second.push_back(Scope);
Devang Patel1be3ecc2009-04-15 00:10:26 +00003482
Devang Patel0f7fef32009-04-13 17:02:03 +00003483 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
3484 I = InlineInfo.find(GV);
Bill Wendlingff86ef32009-05-01 08:35:12 +00003485 if (I == InlineInfo.end())
3486 InlineInfo[GV].push_back(LabelID);
3487 else
3488 I->second.push_back(LabelID);
Bill Wendlingd5a63812009-05-01 08:40:06 +00003489
3490 if (TimePassesIsEnabled)
3491 DebugTimer->stopTimer();
Devang Patel0f7fef32009-04-13 17:02:03 +00003492 }
Devang Patel1be3ecc2009-04-15 00:10:26 +00003493
3494 /// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
3495 unsigned RecordInlinedFnEnd(DISubprogram &SP) {
3496 if (!TAI->doesDwarfUsesInlineInfoSection())
3497 return 0;
3498
Bill Wendlingd5a63812009-05-01 08:40:06 +00003499 if (TimePassesIsEnabled)
3500 DebugTimer->startTimer();
3501
Devang Patel1be3ecc2009-04-15 00:10:26 +00003502 GlobalVariable *GV = SP.getGV();
3503 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3504 I = DbgInlinedScopeMap.find(GV);
Bill Wendlingd5a63812009-05-01 08:40:06 +00003505 if (I == DbgInlinedScopeMap.end()) {
3506 if (TimePassesIsEnabled)
3507 DebugTimer->stopTimer();
3508
Devang Patel1be3ecc2009-04-15 00:10:26 +00003509 return 0;
Bill Wendlingd5a63812009-05-01 08:40:06 +00003510 }
Devang Patel1be3ecc2009-04-15 00:10:26 +00003511
3512 SmallVector<DbgScope *, 2> &Scopes = I->second;
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00003513 assert(!Scopes.empty() && "We should have at least one debug scope!");
Devang Patel1be3ecc2009-04-15 00:10:26 +00003514 DbgScope *Scope = Scopes.back(); Scopes.pop_back();
3515 unsigned ID = MMI->NextLabelID();
3516 MMI->RecordUsedDbgLabel(ID);
3517 Scope->setEndLabelID(ID);
Bill Wendlingd5a63812009-05-01 08:40:06 +00003518
3519 if (TimePassesIsEnabled)
3520 DebugTimer->stopTimer();
3521
Devang Patel1be3ecc2009-04-15 00:10:26 +00003522 return ID;
3523 }
3524
3525 /// RecordVariableScope - Record scope for the variable declared by
3526 /// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
3527 /// Record scopes for only inlined subroutine variables. Other
3528 /// variables' scopes are determined during RecordVariable().
3529 void RecordVariableScope(DIVariable &DV, const MachineInstr *DeclareMI) {
Bill Wendlingd5a63812009-05-01 08:40:06 +00003530 if (TimePassesIsEnabled)
3531 DebugTimer->startTimer();
3532
Devang Patel1be3ecc2009-04-15 00:10:26 +00003533 DISubprogram SP(DV.getContext().getGV());
Bill Wendlingd5a63812009-05-01 08:40:06 +00003534
3535 if (SP.isNull()) {
3536 if (TimePassesIsEnabled)
3537 DebugTimer->stopTimer();
3538
Devang Patel1be3ecc2009-04-15 00:10:26 +00003539 return;
Bill Wendlingd5a63812009-05-01 08:40:06 +00003540 }
3541
Devang Patel1be3ecc2009-04-15 00:10:26 +00003542 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 2> >::iterator
3543 I = DbgInlinedScopeMap.find(SP.getGV());
Bill Wendlingd5a63812009-05-01 08:40:06 +00003544 if (I != DbgInlinedScopeMap.end())
3545 InlinedVariableScopes[DeclareMI] = I->second.back();
Devang Patel1be3ecc2009-04-15 00:10:26 +00003546
Bill Wendlingd5a63812009-05-01 08:40:06 +00003547 if (TimePassesIsEnabled)
3548 DebugTimer->stopTimer();
Devang Patel1be3ecc2009-04-15 00:10:26 +00003549 }
Jim Laskey65195462006-10-30 13:35:07 +00003550};
3551
Jim Laskey072200c2007-01-29 18:51:14 +00003552//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003553/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00003554///
3555class DwarfException : public Dwarf {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003556 struct FunctionEHFrameInfo {
3557 std::string FnName;
3558 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003559 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003560 bool hasCalls;
3561 bool hasLandingPads;
3562 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003563 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00003564
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003565 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3566 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003567 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003568 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003569 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003570 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003571 };
3572
3573 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003574
3575 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3576 /// be emitted.
3577 bool shouldEmitTable;
3578
3579 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3580 /// should be emitted.
3581 bool shouldEmitMoves;
3582
3583 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3584 /// should be emitted.
3585 bool shouldEmitTableModule;
3586
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003587 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003588 /// should be emitted.
3589 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00003590
Bill Wendling163ba3f2009-03-10 21:23:25 +00003591 /// ExceptionTimer - Timer for the Dwarf exception writer.
3592 Timer *ExceptionTimer;
3593
Jim Laskey3f09fc22007-02-28 18:38:31 +00003594 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3595 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003596 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003597 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003598 int stackGrowth =
3599 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3600 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003601 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003602
Jim Laskeybacd3042007-02-21 22:48:45 +00003603 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003604 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling722f5f12008-12-24 08:05:17 +00003605
3606 if (!TAI->doesRequireNonLocalEHFrameLabel())
3607 O << TAI->getEHGlobalPrefix();
3608 O << "EH_frame" << Index << ":\n";
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003609 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003610
Jim Laskeybacd3042007-02-21 22:48:45 +00003611 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003612 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003613
Jim Laskeybacd3042007-02-21 22:48:45 +00003614 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003615 EmitDifference("eh_frame_common_end", Index,
3616 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003617 Asm->EOL("Length of Common Information Entry");
3618
Jim Laskeybacd3042007-02-21 22:48:45 +00003619 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003620 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003621 Asm->EmitInt32((int)0);
3622 Asm->EOL("CIE Identifier Tag");
3623 Asm->EmitInt8(DW_CIE_VERSION);
3624 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00003625
Jim Laskeybacd3042007-02-21 22:48:45 +00003626 // The personality presence indicates that language specific information
3627 // will show up in the eh frame.
3628 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00003629 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00003630
Jim Laskeybacd3042007-02-21 22:48:45 +00003631 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003632 Asm->EmitULEB128Bytes(1);
3633 Asm->EOL("CIE Code Alignment Factor");
3634 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00003635 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003636 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00003637 Asm->EOL("CIE Return Address Column");
3638
Jim Laskeybacd3042007-02-21 22:48:45 +00003639 // If there is a personality, we need to indicate the functions location.
3640 if (Personality) {
3641 Asm->EmitULEB128Bytes(7);
3642 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00003643
Duncan Sands671fa972008-05-07 19:11:09 +00003644 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003645 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00003646 Asm->EOL("Personality (pcrel sdata4 indirect)");
3647 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003648 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00003649 Asm->EOL("Personality (pcrel sdata4)");
3650 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00003651
Duncan Sands671fa972008-05-07 19:11:09 +00003652 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00003653 O << TAI->getPersonalityPrefix();
3654 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3655 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00003656 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3657 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00003658 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00003659
Duncan Sands671fa972008-05-07 19:11:09 +00003660 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3661 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003662
Bill Wendlingd60de512009-01-05 22:53:45 +00003663 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3664 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003665 } else {
3666 Asm->EmitULEB128Bytes(1);
3667 Asm->EOL("Augmentation Size");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003668
Bill Wendlingd60de512009-01-05 22:53:45 +00003669 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3670 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003671 }
3672
3673 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003674 std::vector<MachineMove> Moves;
3675 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00003676 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003677
Dale Johannesen21d972a2008-04-30 00:43:29 +00003678 // On Darwin the linker honors the alignment of eh_frame, which means it
3679 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3680 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003681 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003682 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003683 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003684
Jim Laskeybacd3042007-02-21 22:48:45 +00003685 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003686 }
Duncan Sands671fa972008-05-07 19:11:09 +00003687
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003688 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003689 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003690 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003691 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
Chris Lattner266c7bb2009-04-13 05:44:34 +00003692
3693 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
3694 "Should not emit 'available externally' functions at all");
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003695
Jim Laskeybacd3042007-02-21 22:48:45 +00003696 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3697
3698 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003699 // If the corresponding function is static, this should not be
3700 // externally visible.
Rafael Espindolabb46f522009-01-15 20:18:42 +00003701 if (linkage != Function::InternalLinkage &&
Devang Patel2d1768c2009-01-17 08:05:14 +00003702 linkage != Function::PrivateLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003703 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3704 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3705 }
3706
Dale Johannesen038129d2008-01-10 02:03:30 +00003707 // If corresponding function is weak definition, this should be too.
Duncan Sands667d4b82009-03-07 15:45:40 +00003708 if ((linkage == Function::WeakAnyLinkage ||
3709 linkage == Function::WeakODRLinkage ||
3710 linkage == Function::LinkOnceAnyLinkage ||
3711 linkage == Function::LinkOnceODRLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00003712 TAI->getWeakDefDirective())
3713 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3714
3715 // If there are no calls then you can't unwind. This may mean we can
3716 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003717 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00003718 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00003719 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00003720 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00003721 !UnwindTablesMandatory &&
Duncan Sands667d4b82009-03-07 15:45:40 +00003722 ((linkage != Function::WeakAnyLinkage &&
3723 linkage != Function::WeakODRLinkage &&
3724 linkage != Function::LinkOnceAnyLinkage &&
3725 linkage != Function::LinkOnceODRLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00003726 !TAI->getWeakDefDirective() ||
3727 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003728 {
Bill Wendling6e198962007-09-18 01:47:22 +00003729 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003730 // This name has no connection to the function, so it might get
3731 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003732 // dead-stripping unconditionally.
3733 if (const char *UsedDirective = TAI->getUsedDirective())
3734 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00003735 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00003736 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003737
Jim Laskeybacd3042007-02-21 22:48:45 +00003738 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003739 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3740 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003741 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003742
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003743 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003744
Bill Wendling722f5f12008-12-24 08:05:17 +00003745 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3746 PrintRelDirective(true, true);
3747 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3748
3749 if (!TAI->isAbsoluteEHSectionOffsets())
3750 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3751 } else {
3752 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3753 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3754 true, true, false);
3755 }
3756
Jim Laskeybacd3042007-02-21 22:48:45 +00003757 Asm->EOL("FDE CIE offset");
3758
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003759 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003760 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003761 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003762 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003763 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00003764
Jim Laskey3f09fc22007-02-28 18:38:31 +00003765 // If there is a personality and landing pads then point to the language
3766 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003767 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00003768 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00003769 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00003770
3771 if (EHFrameInfo.hasLandingPads)
3772 EmitReference("exception", EHFrameInfo.Number, true, true);
3773 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00003774 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00003775 Asm->EOL("Language Specific Data Area");
3776 } else {
3777 Asm->EmitULEB128Bytes(0);
3778 Asm->EOL("Augmentation size");
3779 }
Duncan Sands671fa972008-05-07 19:11:09 +00003780
Jim Laskeybacd3042007-02-21 22:48:45 +00003781 // Indicate locations of function specific callee saved registers in
3782 // frame.
Devang Patel5aac3d32009-01-17 08:01:33 +00003783 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00003784 true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003785
Dale Johannesen21d972a2008-04-30 00:43:29 +00003786 // On Darwin the linker honors the alignment of eh_frame, which means it
3787 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3788 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003789 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003790 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003791 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003792
3793 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003794 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003795 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003796 // that depends on unused functions (calling undefined externals) being
3797 // dead-stripped to link correctly. Yes, there really is.
3798 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3799 if (const char *UsedDirective = TAI->getUsedDirective())
3800 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3801 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003802 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003803
Duncan Sands57810cd2007-09-05 11:27:52 +00003804 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003805 ///
3806 /// The general organization of the table is complex, but the basic concepts
3807 /// are easy. First there is a header which describes the location and
3808 /// organization of the three components that follow.
3809 /// 1. The landing pad site information describes the range of code covered
3810 /// by the try. In our case it's an accumulation of the ranges covered
3811 /// by the invokes in the try. There is also a reference to the landing
3812 /// pad that handles the exception once processed. Finally an index into
3813 /// the actions table.
3814 /// 2. The action table, in our case, is composed of pairs of type ids
3815 /// and next action offset. Starting with the action index from the
3816 /// landing pad site, each type Id is checked for a match to the current
3817 /// exception. If it matches then the exception and type id are passed
3818 /// on to the landing pad. Otherwise the next action is looked up. This
3819 /// chain is terminated with a next action of zero. If no type id is
3820 /// found the the frame is unwound and handling continues.
3821 /// 3. Type id table contains references to all the C++ typeinfo for all
3822 /// catches in the function. This tables is reversed indexed base 1.
3823
Duncan Sandsb32edb42007-06-06 15:37:31 +00003824 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3825 static unsigned SharedTypeIds(const LandingPadInfo *L,
3826 const LandingPadInfo *R) {
3827 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003828 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003829 unsigned MinSize = LSize < RSize ? LSize : RSize;
3830 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003831
Duncan Sandsb32edb42007-06-06 15:37:31 +00003832 for (; Count != MinSize; ++Count)
3833 if (LIds[Count] != RIds[Count])
3834 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003835
Duncan Sandsb32edb42007-06-06 15:37:31 +00003836 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003837 }
3838
Duncan Sandsb32edb42007-06-06 15:37:31 +00003839 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00003840 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003841 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003842 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003843 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003844
Duncan Sandsb32edb42007-06-06 15:37:31 +00003845 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00003846 if (LIds[i] != RIds[i])
3847 return LIds[i] < RIds[i];
3848
Duncan Sandsb32edb42007-06-06 15:37:31 +00003849 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003850 }
3851
Duncan Sands53c3a332007-05-16 12:12:23 +00003852 struct KeyInfo {
3853 static inline unsigned getEmptyKey() { return -1U; }
3854 static inline unsigned getTombstoneKey() { return -2U; }
3855 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00003856 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00003857 static bool isPod() { return true; }
3858 };
3859
Duncan Sands57810cd2007-09-05 11:27:52 +00003860 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003861 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003862 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003863 int NextAction;
3864 struct ActionEntry *Previous;
3865 };
3866
Duncan Sands57810cd2007-09-05 11:27:52 +00003867 /// PadRange - Structure holding a try-range and the associated landing pad.
3868 struct PadRange {
3869 // The index of the landing pad.
3870 unsigned PadIndex;
3871 // The index of the begin and end labels in the landing pad's label lists.
3872 unsigned RangeIndex;
3873 };
3874
3875 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3876
3877 /// CallSiteEntry - Structure describing an entry in the call-site table.
3878 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00003879 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003880 unsigned BeginLabel; // zero indicates the start of the function.
3881 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00003882 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003883 unsigned PadLabel; // zero indicates that there is no landing pad.
3884 unsigned Action;
3885 };
3886
Jim Laskeybacd3042007-02-21 22:48:45 +00003887 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00003888 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00003889 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00003890 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3891 if (PadInfos.empty()) return;
3892
3893 // Sort the landing pads in order of their type ids. This is used to fold
3894 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00003895 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00003896 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00003897 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00003898 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003899 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3900
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003901 // Negative type ids index into FilterIds, positive type ids index into
3902 // TypeInfos. The value written for a positive type id is just the type
3903 // id itself. For a negative type id, however, the value written is the
3904 // (negative) byte offset of the corresponding FilterIds entry. The byte
3905 // offset is usually equal to the type id, because the FilterIds entries
3906 // are written using a variable width encoding which outputs one byte per
3907 // entry as long as the value written is not too large, but can differ.
3908 // This kind of complication does not occur for positive type ids because
3909 // type infos are output using a fixed width encoding.
3910 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3911 SmallVector<int, 16> FilterOffsets;
3912 FilterOffsets.reserve(FilterIds.size());
3913 int Offset = -1;
3914 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3915 E = FilterIds.end(); I != E; ++I) {
3916 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003917 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003918 }
3919
Duncan Sands57810cd2007-09-05 11:27:52 +00003920 // Compute the actions table and gather the first action index for each
3921 // landing pad site.
3922 SmallVector<ActionEntry, 32> Actions;
3923 SmallVector<unsigned, 64> FirstActions;
3924 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00003925
Duncan Sandsb32edb42007-06-06 15:37:31 +00003926 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00003927 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003928 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003929 const LandingPadInfo *LP = LandingPads[i];
3930 const std::vector<int> &TypeIds = LP->TypeIds;
3931 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003932 unsigned SizeSiteActions = 0;
3933
Duncan Sandsb32edb42007-06-06 15:37:31 +00003934 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00003935 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00003936 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003937
Duncan Sandsb32edb42007-06-06 15:37:31 +00003938 if (NumShared) {
3939 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3940 assert(Actions.size());
3941 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003942 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3943 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003944 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003945 SizeAction -=
3946 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003947 SizeAction += -PrevAction->NextAction;
3948 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003949 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00003950 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003951
Duncan Sandsb32edb42007-06-06 15:37:31 +00003952 // Compute the actions.
3953 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3954 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003955 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3956 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003957 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003958
3959 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003960 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003961 SizeSiteActions += SizeAction;
3962
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003963 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00003964 Actions.push_back(Action);
3965
3966 PrevAction = &Actions.back();
3967 }
3968
3969 // Record the first action of the landing pad site.
3970 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3971 } // else identical - re-use previous FirstAction
3972
3973 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003974
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00003975 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00003976 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00003977 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003978
Duncan Sands481dc722007-12-19 07:36:31 +00003979 // Compute the call-site table. The entry for an invoke has a try-range
3980 // containing the call, a non-zero landing pad and an appropriate action.
3981 // The entry for an ordinary call has a try-range containing the call and
3982 // zero for the landing pad and the action. Calls marked 'nounwind' have
3983 // no entry and must not be contained in the try-range of any entry - they
3984 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003985 SmallVector<CallSiteEntry, 64> CallSites;
3986
3987 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00003988 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3989 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3990 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00003991 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3992 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00003993 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003994 unsigned BeginLabel = LandingPad->BeginLabels[j];
3995 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3996 PadRange P = { i, j };
3997 PadMap[BeginLabel] = P;
3998 }
3999 }
4000
Duncan Sands481dc722007-12-19 07:36:31 +00004001 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00004002 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00004003
4004 // Whether there is a potentially throwing instruction (currently this means
4005 // an ordinary call) between the end of the previous try-range and now.
4006 bool SawPotentiallyThrowing = false;
4007
4008 // Whether the last callsite entry was for an invoke.
4009 bool PreviousIsInvoke = false;
4010
Duncan Sands481dc722007-12-19 07:36:31 +00004011 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004012 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
4013 I != E; ++I) {
4014 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
4015 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00004016 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00004017 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00004018 continue;
4019 }
4020
Chris Lattner9e330492007-12-30 20:50:28 +00004021 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00004022 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00004023
Duncan Sands481dc722007-12-19 07:36:31 +00004024 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00004025 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00004026 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004027
Duncan Sands481dc722007-12-19 07:36:31 +00004028 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00004029 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00004030 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00004031 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00004032 continue;
4033
4034 PadRange P = L->second;
4035 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
4036
4037 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
4038 "Inconsistent landing pad map!");
4039
4040 // If some instruction between the previous try-range and this one may
4041 // throw, create a call-site entry with no landing pad for the region
4042 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00004043 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004044 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
4045 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00004046 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004047 }
4048
4049 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00004050 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00004051
Duncan Sands481dc722007-12-19 07:36:31 +00004052 if (LandingPad->LandingPadLabel) {
4053 // This try-range is for an invoke.
4054 CallSiteEntry Site = {BeginLabel, LastLabel,
4055 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00004056
Duncan Sands481dc722007-12-19 07:36:31 +00004057 // Try to merge with the previous call-site.
4058 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00004059 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00004060 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
4061 // Extend the range of the previous entry.
4062 Prev.EndLabel = Site.EndLabel;
4063 continue;
4064 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004065 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004066
Duncan Sands481dc722007-12-19 07:36:31 +00004067 // Otherwise, create a new call-site.
4068 CallSites.push_back(Site);
4069 PreviousIsInvoke = true;
4070 } else {
4071 // Create a gap.
4072 PreviousIsInvoke = false;
4073 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004074 }
4075 }
4076 // If some instruction between the previous try-range and the end of the
4077 // function may throw, create a call-site entry with no landing pad for the
4078 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00004079 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004080 CallSiteEntry Site = {LastLabel, 0, 0, 0};
4081 CallSites.push_back(Site);
4082 }
4083
Jim Laskeybacd3042007-02-21 22:48:45 +00004084 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00004085
4086 // Call sites.
4087 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
4088 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
4089 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
4090 unsigned SizeSites = CallSites.size() * (SiteStartSize +
4091 SiteLengthSize +
4092 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00004093 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004094 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00004095
Duncan Sands671fa972008-05-07 19:11:09 +00004096 // Type infos.
4097 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
4098 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004099
4100 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004101 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004102 SizeSites + SizeActions + SizeTypes;
4103
4104 unsigned TotalSize = sizeof(int8_t) + // LPStart format
4105 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004106 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004107 TypeOffset;
4108
4109 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004110
Jim Laskeybacd3042007-02-21 22:48:45 +00004111 // Begin the exception table.
4112 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng05548eb2008-02-29 19:36:59 +00004113 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesend65b2642008-10-08 21:50:21 +00004114 O << "GCC_except_table" << SubprogramCount << ":\n";
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004115 for (unsigned i = 0; i != SizeAlign; ++i) {
4116 Asm->EmitInt8(0);
4117 Asm->EOL("Padding");
4118 }
Jim Laskeybacd3042007-02-21 22:48:45 +00004119 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004120
Jim Laskeybacd3042007-02-21 22:48:45 +00004121 // Emit the header.
4122 Asm->EmitInt8(DW_EH_PE_omit);
4123 Asm->EOL("LPStart format (DW_EH_PE_omit)");
4124 Asm->EmitInt8(DW_EH_PE_absptr);
4125 Asm->EOL("TType format (DW_EH_PE_absptr)");
4126 Asm->EmitULEB128Bytes(TypeOffset);
4127 Asm->EOL("TType base offset");
4128 Asm->EmitInt8(DW_EH_PE_udata4);
4129 Asm->EOL("Call site format (DW_EH_PE_udata4)");
4130 Asm->EmitULEB128Bytes(SizeSites);
4131 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004132
Duncan Sands57810cd2007-09-05 11:27:52 +00004133 // Emit the landing pad site information.
4134 for (unsigned i = 0; i < CallSites.size(); ++i) {
4135 CallSiteEntry &S = CallSites[i];
4136 const char *BeginTag;
4137 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00004138
Duncan Sands57810cd2007-09-05 11:27:52 +00004139 if (!S.BeginLabel) {
4140 BeginTag = "eh_func_begin";
4141 BeginNumber = SubprogramCount;
4142 } else {
4143 BeginTag = "label";
4144 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00004145 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004146
Duncan Sands57810cd2007-09-05 11:27:52 +00004147 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004148 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004149 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00004150
Duncan Sands57810cd2007-09-05 11:27:52 +00004151 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00004152 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00004153 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004154 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00004155 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004156 }
4157 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004158
Duncan Sands671fa972008-05-07 19:11:09 +00004159 if (!S.PadLabel)
4160 Asm->EmitInt32(0);
4161 else
Duncan Sands57810cd2007-09-05 11:27:52 +00004162 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004163 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004164 Asm->EOL("Landing pad");
4165
4166 Asm->EmitULEB128Bytes(S.Action);
4167 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004168 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004169
Jim Laskeybacd3042007-02-21 22:48:45 +00004170 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004171 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4172 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00004173
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004174 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004175 Asm->EOL("TypeInfo index");
4176 Asm->EmitSLEB128Bytes(Action.NextAction);
4177 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004178 }
4179
4180 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00004181 for (unsigned M = TypeInfos.size(); M; --M) {
4182 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00004183
4184 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00004185
Bill Wendling7d16e852009-04-10 00:12:49 +00004186 if (GV) {
4187 std::string GLN;
4188 O << Asm->getGlobalLinkName(GV, GLN);
4189 } else {
Jim Laskeybacd3042007-02-21 22:48:45 +00004190 O << "0";
Bill Wendling7d16e852009-04-10 00:12:49 +00004191 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004192
Jim Laskeybacd3042007-02-21 22:48:45 +00004193 Asm->EOL("TypeInfo");
4194 }
4195
Duncan Sands73ef58a2007-06-02 16:53:42 +00004196 // Emit the filter typeids.
4197 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4198 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00004199 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00004200 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00004201 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004202
Evan Cheng05548eb2008-02-29 19:36:59 +00004203 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004204 }
4205
Jim Laskey072200c2007-01-29 18:51:14 +00004206public:
4207 //===--------------------------------------------------------------------===//
4208 // Main entry points.
4209 //
Owen Andersoncb371882008-08-21 00:14:44 +00004210 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00004211 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
4212 shouldEmitTableModule(false), shouldEmitMovesModule(false),
4213 ExceptionTimer(0) {
4214 if (TimePassesIsEnabled)
4215 ExceptionTimer = new Timer("Dwarf Exception Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00004216 getDwarfTimerGroup());
Bill Wendling163ba3f2009-03-10 21:23:25 +00004217 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004218
Bill Wendling163ba3f2009-03-10 21:23:25 +00004219 virtual ~DwarfException() {
4220 delete ExceptionTimer;
4221 }
Jim Laskey072200c2007-01-29 18:51:14 +00004222
4223 /// SetModuleInfo - Set machine module information when it's known that pass
4224 /// manager has created it. Set by the target AsmPrinter.
4225 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00004226 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00004227 }
4228
4229 /// BeginModule - Emit all exception information that should come prior to the
4230 /// content.
4231 void BeginModule(Module *M) {
4232 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00004233 }
4234
4235 /// EndModule - Emit all exception information that should come after the
4236 /// content.
4237 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004238 if (TimePassesIsEnabled)
4239 ExceptionTimer->startTimer();
4240
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004241 if (shouldEmitMovesModule || shouldEmitTableModule) {
4242 const std::vector<Function *> Personalities = MMI->getPersonalities();
Evan Chenge3d42322009-02-25 07:04:34 +00004243 for (unsigned i = 0; i < Personalities.size(); ++i)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004244 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00004245
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004246 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4247 E = EHFrames.end(); I != E; ++I)
4248 EmitEHFrame(*I);
4249 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004250
4251 if (TimePassesIsEnabled)
4252 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004253 }
4254
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004255 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00004256 /// emitted immediately after the function entry point.
4257 void BeginFunction(MachineFunction *MF) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004258 if (TimePassesIsEnabled)
4259 ExceptionTimer->startTimer();
4260
Jim Laskey072200c2007-01-29 18:51:14 +00004261 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004262 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004263
Bill Wendling163ba3f2009-03-10 21:23:25 +00004264 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004265 // Map all labels and get rid of any dead landing pads.
4266 MMI->TidyLandingPads();
Bill Wendling163ba3f2009-03-10 21:23:25 +00004267
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004268 // If any landing pads survive, we need an EH table.
4269 if (MMI->getLandingPads().size())
4270 shouldEmitTable = true;
4271
4272 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00004273 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004274 shouldEmitMoves = true;
4275
4276 if (shouldEmitMoves || shouldEmitTable)
4277 // Assumes in correct section after the entry point.
4278 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00004279 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004280
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004281 shouldEmitTableModule |= shouldEmitTable;
4282 shouldEmitMovesModule |= shouldEmitMoves;
Bill Wendling163ba3f2009-03-10 21:23:25 +00004283
4284 if (TimePassesIsEnabled)
4285 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004286 }
4287
4288 /// EndFunction - Gather and emit post-function exception information.
4289 ///
4290 void EndFunction() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004291 if (TimePassesIsEnabled)
4292 ExceptionTimer->startTimer();
4293
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004294 if (shouldEmitMoves || shouldEmitTable) {
4295 EmitLabel("eh_func_end", SubprogramCount);
4296 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00004297
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004298 // Save EH frame information
Bill Wendling7d16e852009-04-10 00:12:49 +00004299 std::string Name;
4300 EHFrames.push_back(
4301 FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF, Name),
4302 SubprogramCount,
4303 MMI->getPersonalityIndex(),
4304 MF->getFrameInfo()->hasCalls(),
4305 !MMI->getLandingPads().empty(),
4306 MMI->getFrameMoves(),
4307 MF->getFunction()));
Bill Wendling163ba3f2009-03-10 21:23:25 +00004308 }
4309
4310 if (TimePassesIsEnabled)
4311 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004312 }
4313};
4314
Jim Laskey0d086af2006-02-27 12:43:29 +00004315} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00004316
4317//===----------------------------------------------------------------------===//
4318
Jim Laskeyd18e2892006-01-20 20:34:06 +00004319/// Emit - Print the abbreviation using the specified Dwarf writer.
4320///
Jim Laskey072200c2007-01-29 18:51:14 +00004321void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004322 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00004323 DD.getAsm()->EmitULEB128Bytes(Tag);
4324 DD.getAsm()->EOL(TagString(Tag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004325
Jim Laskeyd18e2892006-01-20 20:34:06 +00004326 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00004327 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4328 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004329
Jim Laskeyd18e2892006-01-20 20:34:06 +00004330 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00004331 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004332 const DIEAbbrevData &AttrData = Data[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004333
Jim Laskeyd18e2892006-01-20 20:34:06 +00004334 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00004335 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4336 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004337
Jim Laskeyd18e2892006-01-20 20:34:06 +00004338 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00004339 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4340 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00004341 }
4342
4343 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00004344 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4345 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00004346}
4347
4348#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00004349void DIEAbbrev::print(std::ostream &O) {
4350 O << "Abbreviation @"
4351 << std::hex << (intptr_t)this << std::dec
4352 << " "
4353 << TagString(Tag)
4354 << " "
4355 << ChildrenString(ChildrenFlag)
4356 << "\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004357
Jim Laskeya0f3d172006-09-07 22:06:40 +00004358 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4359 O << " "
4360 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004361 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00004362 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004363 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00004364 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00004365}
Bill Wendlinge8156192006-12-07 01:30:32 +00004366void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00004367#endif
4368
4369//===----------------------------------------------------------------------===//
4370
Jim Laskeyef42a012006-11-02 20:12:39 +00004371#ifndef NDEBUG
4372void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004373 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004374}
Jim Laskeyef42a012006-11-02 20:12:39 +00004375#endif
4376
4377//===----------------------------------------------------------------------===//
4378
Jim Laskey063e7652006-01-17 17:31:53 +00004379/// EmitValue - Emit integer of appropriate size.
4380///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004381void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00004382 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00004383 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00004384 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004385 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004386 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004387 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004388 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004389 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004390 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004391 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4392 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4393 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004394 default: assert(0 && "DIE Value form not supported yet"); break;
4395 }
4396}
4397
4398/// SizeOf - Determine size of integer value in bytes.
4399///
Jim Laskey072200c2007-01-29 18:51:14 +00004400unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004401 switch (Form) {
4402 case DW_FORM_flag: // Fall thru
4403 case DW_FORM_ref1: // Fall thru
4404 case DW_FORM_data1: return sizeof(int8_t);
4405 case DW_FORM_ref2: // Fall thru
4406 case DW_FORM_data2: return sizeof(int16_t);
4407 case DW_FORM_ref4: // Fall thru
4408 case DW_FORM_data4: return sizeof(int32_t);
4409 case DW_FORM_ref8: // Fall thru
4410 case DW_FORM_data8: return sizeof(int64_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004411 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4412 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00004413 default: assert(0 && "DIE Value form not supported yet"); break;
4414 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004415 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00004416}
4417
Jim Laskey063e7652006-01-17 17:31:53 +00004418//===----------------------------------------------------------------------===//
4419
4420/// EmitValue - Emit string value.
4421///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004422void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Bill Wendlingdd446322009-03-10 23:57:09 +00004423 DD.getAsm()->EmitString(Str);
Jim Laskey063e7652006-01-17 17:31:53 +00004424}
4425
Jim Laskey063e7652006-01-17 17:31:53 +00004426//===----------------------------------------------------------------------===//
4427
4428/// EmitValue - Emit label value.
4429///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004430void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004431 bool IsSmall = Form == DW_FORM_data4;
4432 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004433}
4434
4435/// SizeOf - Determine size of label value in bytes.
4436///
Jim Laskey072200c2007-01-29 18:51:14 +00004437unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004438 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004439 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004440}
Jim Laskeyef42a012006-11-02 20:12:39 +00004441
Jim Laskey063e7652006-01-17 17:31:53 +00004442//===----------------------------------------------------------------------===//
4443
Jim Laskeyd18e2892006-01-20 20:34:06 +00004444/// EmitValue - Emit label value.
4445///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004446void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004447 bool IsSmall = Form == DW_FORM_data4;
4448 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00004449}
4450
4451/// SizeOf - Determine size of label value in bytes.
4452///
Jim Laskey072200c2007-01-29 18:51:14 +00004453unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004454 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004455 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00004456}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004457
Jim Laskeyd18e2892006-01-20 20:34:06 +00004458//===----------------------------------------------------------------------===//
4459
Jim Laskey063e7652006-01-17 17:31:53 +00004460/// EmitValue - Emit delta value.
4461///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004462void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4463 bool IsSmall = Form == DW_FORM_data4;
4464 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4465 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4466}
4467
4468/// SizeOf - Determine size of delta value in bytes.
4469///
4470unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4471 if (Form == DW_FORM_data4) return 4;
4472 return DD.getTargetData()->getPointerSize();
4473}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004474
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004475//===----------------------------------------------------------------------===//
4476
4477/// EmitValue - Emit delta value.
4478///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004479void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004480 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00004481 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004482}
4483
4484/// SizeOf - Determine size of delta value in bytes.
4485///
Jim Laskey072200c2007-01-29 18:51:14 +00004486unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004487 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004488 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004489}
4490
4491//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004492
Jim Laskeyb8509c52006-03-23 18:07:55 +00004493/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004494///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004495void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004496 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00004497}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004498
Jim Laskeyd18e2892006-01-20 20:34:06 +00004499//===----------------------------------------------------------------------===//
4500
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004501/// ComputeSize - calculate the size of the block.
4502///
Jim Laskey072200c2007-01-29 18:51:14 +00004503unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00004504 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00004505 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004506
Jim Laskeyef42a012006-11-02 20:12:39 +00004507 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00004508 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00004509 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004510 }
4511 return Size;
4512}
4513
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004514/// EmitValue - Emit block data.
4515///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004516void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004517 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004518 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4519 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4520 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4521 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004522 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004523 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004524
Owen Anderson873e1b52008-06-24 21:44:59 +00004525 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00004526
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004527 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00004528 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00004529 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004530 }
4531}
4532
4533/// SizeOf - Determine size of block data in bytes.
4534///
Jim Laskey072200c2007-01-29 18:51:14 +00004535unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004536 switch (Form) {
4537 case DW_FORM_block1: return Size + sizeof(int8_t);
4538 case DW_FORM_block2: return Size + sizeof(int16_t);
4539 case DW_FORM_block4: return Size + sizeof(int32_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004540 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004541 default: assert(0 && "Improper form for block"); break;
4542 }
4543 return 0;
4544}
4545
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004546//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004547/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00004548
4549DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00004550 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00004551 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00004552}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004553
Jim Laskeyb8509c52006-03-23 18:07:55 +00004554/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4555///
4556void DIE::AddSiblingOffset() {
4557 DIEInteger *DI = new DIEInteger(0);
4558 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00004559 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00004560}
4561
Jim Laskeyef42a012006-11-02 20:12:39 +00004562/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004563///
Jim Laskeyef42a012006-11-02 20:12:39 +00004564void DIE::Profile(FoldingSetNodeID &ID) {
4565 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004566
Jim Laskeyef42a012006-11-02 20:12:39 +00004567 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4568 ID.AddPointer(Children[i]);
4569
4570 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4571 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00004572}
Jim Laskeyef42a012006-11-02 20:12:39 +00004573
4574#ifndef NDEBUG
4575void DIE::print(std::ostream &O, unsigned IncIndent) {
4576 static unsigned IndentCount = 0;
4577 IndentCount += IncIndent;
4578 const std::string Indent(IndentCount, ' ');
4579 bool isBlock = Abbrev.getTag() == 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004580
Jim Laskeyef42a012006-11-02 20:12:39 +00004581 if (!isBlock) {
4582 O << Indent
4583 << "Die: "
4584 << "0x" << std::hex << (intptr_t)this << std::dec
4585 << ", Offset: " << Offset
4586 << ", Size: " << Size
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004587 << "\n";
4588
Jim Laskeyef42a012006-11-02 20:12:39 +00004589 O << Indent
4590 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00004591 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00004592 << ChildrenString(Abbrev.getChildrenFlag());
4593 } else {
4594 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00004595 }
4596 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00004597
Owen Anderson873e1b52008-06-24 21:44:59 +00004598 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004599
Jim Laskeyef42a012006-11-02 20:12:39 +00004600 IndentCount += 2;
4601 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4602 O << Indent;
Bill Wendling4e974012008-07-22 00:28:47 +00004603
4604 if (!isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +00004605 O << AttributeString(Data[i].getAttribute());
Bill Wendling4e974012008-07-22 00:28:47 +00004606 else
Jim Laskeyef42a012006-11-02 20:12:39 +00004607 O << "Blk[" << i << "]";
Bill Wendling4e974012008-07-22 00:28:47 +00004608
Jim Laskeyef42a012006-11-02 20:12:39 +00004609 O << " "
4610 << FormEncodingString(Data[i].getForm())
4611 << " ";
4612 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00004613 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00004614 }
Jim Laskeyef42a012006-11-02 20:12:39 +00004615 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00004616
Jim Laskeyef42a012006-11-02 20:12:39 +00004617 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4618 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00004619 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004620
Jim Laskeyef42a012006-11-02 20:12:39 +00004621 if (!isBlock) O << "\n";
4622 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00004623}
4624
Jim Laskeyef42a012006-11-02 20:12:39 +00004625void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004626 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00004627}
Jim Laskeybd761842006-02-27 17:27:12 +00004628#endif
Jim Laskey65195462006-10-30 13:35:07 +00004629
4630//===----------------------------------------------------------------------===//
4631/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00004632///
Jim Laskey65195462006-10-30 13:35:07 +00004633
Bill Wendling91b8b802009-03-10 20:41:52 +00004634DwarfWriter::DwarfWriter()
Bill Wendling163ba3f2009-03-10 21:23:25 +00004635 : ImmutablePass(&ID), DD(0), DE(0) {}
Jim Laskey65195462006-10-30 13:35:07 +00004636
4637DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00004638 delete DE;
4639 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00004640}
4641
Jim Laskey65195462006-10-30 13:35:07 +00004642/// BeginModule - Emit all Dwarf sections that should come prior to the
4643/// content.
Devang Pateleb3fc282009-01-08 23:40:34 +00004644void DwarfWriter::BeginModule(Module *M,
4645 MachineModuleInfo *MMI,
4646 raw_ostream &OS, AsmPrinter *A,
4647 const TargetAsmInfo *T) {
4648 DE = new DwarfException(OS, A, T);
4649 DD = new DwarfDebug(OS, A, T);
Jim Laskey072200c2007-01-29 18:51:14 +00004650 DE->BeginModule(M);
4651 DD->BeginModule(M);
Devang Patel7bb89ed2009-01-13 00:20:51 +00004652 DD->SetDebugInfo(MMI);
Devang Pateleb3fc282009-01-08 23:40:34 +00004653 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00004654}
4655
4656/// EndModule - Emit all Dwarf sections that should come after the content.
4657///
4658void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00004659 DE->EndModule();
4660 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00004661}
4662
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004663/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00004664/// emitted immediately after the function entry point.
4665void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00004666 DE->BeginFunction(MF);
4667 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00004668}
4669
4670/// EndFunction - Gather and emit post-function debug information.
4671///
Bill Wendlingd751c642008-09-26 00:28:12 +00004672void DwarfWriter::EndFunction(MachineFunction *MF) {
4673 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004674 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004675
Bill Wendlingc91d0b92008-07-22 00:53:37 +00004676 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00004677 // Clear function debug information.
4678 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00004679}
Devang Patelccca7fe2009-01-12 19:17:34 +00004680
4681/// RecordSourceLine - Records location information and associates it with a
4682/// label. Returns a unique label ID used to generate a label and provide
4683/// correspondence to the source line list.
4684unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00004685 DICompileUnit CU) {
4686 return DD->RecordSourceLine(Line, Col, CU);
Devang Patelccca7fe2009-01-12 19:17:34 +00004687}
4688
4689/// RecordRegionStart - Indicate the start of a region.
4690unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004691 return DD->RecordRegionStart(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004692}
4693
4694/// RecordRegionEnd - Indicate the end of a region.
4695unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004696 return DD->RecordRegionEnd(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004697}
4698
4699/// getRecordSourceLineCount - Count source lines.
4700unsigned DwarfWriter::getRecordSourceLineCount() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004701 return DD->getRecordSourceLineCount();
Devang Patelccca7fe2009-01-12 19:17:34 +00004702}
Devang Patelbb8c5952009-01-13 21:25:00 +00004703
Devang Patelc48c5502009-01-13 21:44:10 +00004704/// RecordVariable - Indicate the declaration of a local variable.
4705///
Devang Patel1be3ecc2009-04-15 00:10:26 +00004706void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
4707 const MachineInstr *MI) {
4708 DD->RecordVariable(GV, FrameIndex, MI);
Devang Patelc48c5502009-01-13 21:44:10 +00004709}
Devang Patelbbdc8202009-01-13 23:54:55 +00004710
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004711/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
4712/// be emitted.
4713bool DwarfWriter::ShouldEmitDwarfDebug() const {
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +00004714 return DD && DD->ShouldEmitDwarfDebug();
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004715}
Devang Patel0f7fef32009-04-13 17:02:03 +00004716
Devang Patel1be3ecc2009-04-15 00:10:26 +00004717//// RecordInlinedFnStart - Global variable GV is inlined at the location marked
Devang Patel0f7fef32009-04-13 17:02:03 +00004718//// by LabelID label.
Devang Patel1be3ecc2009-04-15 00:10:26 +00004719void DwarfWriter::RecordInlinedFnStart(Instruction *I, DISubprogram &SP,
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00004720 unsigned LabelID, DICompileUnit CU,
Devang Patel1be3ecc2009-04-15 00:10:26 +00004721 unsigned Line, unsigned Col) {
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00004722 DD->RecordInlinedFnStart(I, SP, LabelID, CU, Line, Col);
Devang Patel0f7fef32009-04-13 17:02:03 +00004723}
4724
Devang Patel1be3ecc2009-04-15 00:10:26 +00004725/// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
4726unsigned DwarfWriter::RecordInlinedFnEnd(DISubprogram &SP) {
4727 return DD->RecordInlinedFnEnd(SP);
4728}
4729
4730/// RecordVariableScope - Record scope for the variable declared by
4731/// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
4732void DwarfWriter::RecordVariableScope(DIVariable &DV,
4733 const MachineInstr *DeclareMI) {
4734 DD->RecordVariableScope(DV, DeclareMI);
4735}