blob: 2e4f7c0bb3eed2aca24e1749bdcc998369e938a2 [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 Laskeye5032892005-12-21 19:48:16 +000015
Duncan Sands53c3a332007-05-16 12:12:23 +000016#include "llvm/ADT/DenseMap.h"
Jim Laskeya9c83fe2006-10-30 15:59:54 +000017#include "llvm/ADT/FoldingSet.h"
Jim Laskey063e7652006-01-17 17:31:53 +000018#include "llvm/ADT/StringExtras.h"
Jim Laskey65195462006-10-30 13:35:07 +000019#include "llvm/ADT/UniqueVector.h"
Jim Laskey52060a02006-01-24 00:49:18 +000020#include "llvm/Module.h"
Devang Pateld1ca9252009-01-05 23:03:32 +000021#include "llvm/DerivedTypes.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000022#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskey44c3b9f2007-01-26 21:22:28 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Jim Laskey41886992006-04-07 16:34:46 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000025#include "llvm/CodeGen/MachineLocation.h"
Devang Patel08f053f2009-01-05 17:57:47 +000026#include "llvm/Analysis/DebugInfo.h"
Jim Laskey3f09fc22007-02-28 18:38:31 +000027#include "llvm/Support/Debug.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000028#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000029#include "llvm/Support/CommandLine.h"
Jim Laskey65195462006-10-30 13:35:07 +000030#include "llvm/Support/DataTypes.h"
Jim Laskey52060a02006-01-24 00:49:18 +000031#include "llvm/Support/Mangler.h"
Owen Andersoncb371882008-08-21 00:14:44 +000032#include "llvm/Support/raw_ostream.h"
Dan Gohman85496362007-09-24 21:32:18 +000033#include "llvm/System/Path.h"
Jim Laskey563321a2006-09-06 18:34:40 +000034#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000035#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000036#include "llvm/Target/TargetData.h"
Jim Laskey1069fbd2006-04-10 23:09:19 +000037#include "llvm/Target/TargetFrameInfo.h"
Duncan Sands53c3a332007-05-16 12:12:23 +000038#include "llvm/Target/TargetInstrInfo.h"
Jim Laskey1b340dc2007-01-29 20:48:32 +000039#include "llvm/Target/TargetMachine.h"
Jim Laskeyc1c47c32007-01-29 23:40:33 +000040#include "llvm/Target/TargetOptions.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000041#include <ostream>
Jim Laskey65195462006-10-30 13:35:07 +000042#include <string>
Jim Laskeyb2efb852006-01-04 22:28:25 +000043using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000044using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000045
Devang Pateleb3fc282009-01-08 23:40:34 +000046static RegisterPass<DwarfWriter>
47X("dwarfwriter", "DWARF Information Writer");
48char DwarfWriter::ID = 0;
49
Jim Laskey0d086af2006-02-27 12:43:29 +000050namespace llvm {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000051
Jim Laskey65195462006-10-30 13:35:07 +000052//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +000053
54/// Configuration values for initial hash set sizes (log2).
55///
56static const unsigned InitDiesSetSize = 9; // 512
57static const unsigned InitAbbreviationsSetSize = 9; // 512
58static const unsigned InitValuesSetSize = 9; // 512
59
60//===----------------------------------------------------------------------===//
61/// Forward declarations.
62///
63class DIE;
64class DIEValue;
65
66//===----------------------------------------------------------------------===//
Devang Pateld1ca9252009-01-05 23:03:32 +000067/// Utility routines.
68///
69/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
70/// specified value in their initializer somewhere.
71static void
72getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
73 // Scan though value users.
74 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
75 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
76 // If the user is a GlobalVariable then add to result.
77 Result.push_back(GV);
78 } else if (Constant *C = dyn_cast<Constant>(*I)) {
79 // If the user is a constant variable then scan its users
80 getGlobalVariablesUsing(C, Result);
81 }
82 }
83}
84
85/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
86/// named GlobalVariable.
87static void
88getGlobalVariablesUsing(Module &M, const std::string &RootName,
89 std::vector<GlobalVariable*> &Result) {
90 std::vector<const Type*> FieldTypes;
91 FieldTypes.push_back(Type::Int32Ty);
92 FieldTypes.push_back(Type::Int32Ty);
93
94 // Get the GlobalVariable root.
95 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
96 StructType::get(FieldTypes));
97
98 // If present and linkonce then scan for users.
99 if (UseRoot && UseRoot->hasLinkOnceLinkage())
100 getGlobalVariablesUsing(UseRoot, Result);
101}
102
103//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000104/// DWLabel - Labels are used to track locations in the assembler file.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000105/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
106/// where the tag is a category of label (Ex. location) and number is a value
Reid Spencer181b6c92007-08-05 20:06:04 +0000107/// unique in that category.
Jim Laskey65195462006-10-30 13:35:07 +0000108class DWLabel {
109public:
Jim Laskeyef42a012006-11-02 20:12:39 +0000110 /// Tag - Label category tag. Should always be a staticly declared C string.
111 ///
112 const char *Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000113
Jim Laskeyef42a012006-11-02 20:12:39 +0000114 /// Number - Value to make label unique.
115 ///
116 unsigned Number;
Jim Laskey65195462006-10-30 13:35:07 +0000117
118 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000119
Jim Laskeyef42a012006-11-02 20:12:39 +0000120 void Profile(FoldingSetNodeID &ID) const {
121 ID.AddString(std::string(Tag));
122 ID.AddInteger(Number);
Jim Laskey90c79d72006-03-23 23:02:34 +0000123 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000124
Jim Laskeyef42a012006-11-02 20:12:39 +0000125#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000126 void print(std::ostream *O) const {
127 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000128 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000129 void print(std::ostream &O) const {
Jim Laskeybacd3042007-02-21 22:48:45 +0000130 O << "." << Tag;
Jim Laskeyef42a012006-11-02 20:12:39 +0000131 if (Number) O << Number;
132 }
133#endif
Jim Laskeybd761842006-02-27 17:27:12 +0000134};
135
136//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000137/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
138/// Dwarf abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +0000139class DIEAbbrevData {
140private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000141 /// Attribute - Dwarf attribute code.
142 ///
143 unsigned Attribute;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000144
Jim Laskeyef42a012006-11-02 20:12:39 +0000145 /// Form - Dwarf form code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000146 ///
147 unsigned Form;
148
Jim Laskey0d086af2006-02-27 12:43:29 +0000149public:
150 DIEAbbrevData(unsigned A, unsigned F)
151 : Attribute(A)
152 , Form(F)
153 {}
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 getAttribute() const { return Attribute; }
157 unsigned getForm() const { return Form; }
Jim Laskey063e7652006-01-17 17:31:53 +0000158
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000159 /// Profile - Used to gather unique data for the abbreviation folding set.
Jim Laskey0d086af2006-02-27 12:43:29 +0000160 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000161 void Profile(FoldingSetNodeID &ID)const {
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000162 ID.AddInteger(Attribute);
163 ID.AddInteger(Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000164 }
165};
Jim Laskey063e7652006-01-17 17:31:53 +0000166
Jim Laskey0d086af2006-02-27 12:43:29 +0000167//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000168/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
169/// information object.
170class DIEAbbrev : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000171private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000172 /// Tag - Dwarf tag code.
173 ///
174 unsigned Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000175
Jim Laskeyef42a012006-11-02 20:12:39 +0000176 /// Unique number for node.
177 ///
178 unsigned Number;
179
180 /// ChildrenFlag - Dwarf children flag.
181 ///
182 unsigned ChildrenFlag;
183
184 /// Data - Raw data bytes for abbreviation.
185 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000186 SmallVector<DIEAbbrevData, 8> Data;
Jim Laskey063e7652006-01-17 17:31:53 +0000187
Jim Laskey0d086af2006-02-27 12:43:29 +0000188public:
Jim Laskey063e7652006-01-17 17:31:53 +0000189
Jim Laskey0d086af2006-02-27 12:43:29 +0000190 DIEAbbrev(unsigned T, unsigned C)
Jim Laskeyef42a012006-11-02 20:12:39 +0000191 : Tag(T)
Jim Laskey0d086af2006-02-27 12:43:29 +0000192 , ChildrenFlag(C)
193 , Data()
194 {}
195 ~DIEAbbrev() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000196
Jim Laskeybd761842006-02-27 17:27:12 +0000197 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000198 unsigned getTag() const { return Tag; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000199 unsigned getNumber() const { return Number; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000200 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000201 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000202 void setTag(unsigned T) { Tag = T; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000203 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000204 void setNumber(unsigned N) { Number = N; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000205
Jim Laskey0d086af2006-02-27 12:43:29 +0000206 /// AddAttribute - Adds another set of attribute information to the
207 /// abbreviation.
208 void AddAttribute(unsigned Attribute, unsigned Form) {
209 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000210 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000211
Jim Laskeyb8509c52006-03-23 18:07:55 +0000212 /// AddFirstAttribute - Adds a set of attribute information to the front
213 /// of the abbreviation.
214 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
215 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
216 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000217
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000218 /// Profile - Used to gather unique data for the abbreviation folding set.
219 ///
220 void Profile(FoldingSetNodeID &ID) {
221 ID.AddInteger(Tag);
222 ID.AddInteger(ChildrenFlag);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000223
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000224 // For each attribute description.
225 for (unsigned i = 0, N = Data.size(); i < N; ++i)
226 Data[i].Profile(ID);
227 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000228
Jim Laskey0d086af2006-02-27 12:43:29 +0000229 /// Emit - Print the abbreviation using the specified Dwarf writer.
230 ///
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000231 void Emit(const DwarfDebug &DD) const;
232
Jim Laskey0d086af2006-02-27 12:43:29 +0000233#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000234 void print(std::ostream *O) {
235 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000236 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000237 void print(std::ostream &O);
238 void dump();
239#endif
240};
Jim Laskey063e7652006-01-17 17:31:53 +0000241
Jim Laskey0d086af2006-02-27 12:43:29 +0000242//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000243/// DIE - A structured debug information entry. Has an abbreviation which
244/// describes it's organization.
245class DIE : public FoldingSetNode {
246protected:
247 /// Abbrev - Buffer for constructing abbreviation.
248 ///
249 DIEAbbrev Abbrev;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000250
Jim Laskeyef42a012006-11-02 20:12:39 +0000251 /// Offset - Offset in debug info section.
252 ///
253 unsigned Offset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000254
Jim Laskeyef42a012006-11-02 20:12:39 +0000255 /// Size - Size of instance + children.
256 ///
257 unsigned Size;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000258
Jim Laskeyef42a012006-11-02 20:12:39 +0000259 /// Children DIEs.
260 ///
261 std::vector<DIE *> Children;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000262
Jim Laskeyef42a012006-11-02 20:12:39 +0000263 /// Attributes values.
264 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000265 SmallVector<DIEValue*, 32> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000266
Jim Laskeyef42a012006-11-02 20:12:39 +0000267public:
Dan Gohman81975f62007-08-27 14:50:10 +0000268 explicit DIE(unsigned Tag)
Jim Laskeyef42a012006-11-02 20:12:39 +0000269 : Abbrev(Tag, DW_CHILDREN_no)
270 , Offset(0)
271 , Size(0)
272 , Children()
273 , Values()
274 {}
275 virtual ~DIE();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000276
Jim Laskeyef42a012006-11-02 20:12:39 +0000277 // Accessors.
278 DIEAbbrev &getAbbrev() { return Abbrev; }
279 unsigned getAbbrevNumber() const {
280 return Abbrev.getNumber();
281 }
Jim Laskey85f419b2006-11-09 16:32:26 +0000282 unsigned getTag() const { return Abbrev.getTag(); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000283 unsigned getOffset() const { return Offset; }
284 unsigned getSize() const { return Size; }
285 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000286 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000287 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
288 void setOffset(unsigned O) { Offset = O; }
289 void setSize(unsigned S) { Size = S; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000290
Jim Laskeyef42a012006-11-02 20:12:39 +0000291 /// AddValue - Add a value and attributes to a DIE.
292 ///
293 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
294 Abbrev.AddAttribute(Attribute, Form);
295 Values.push_back(Value);
296 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000297
Jim Laskeyef42a012006-11-02 20:12:39 +0000298 /// SiblingOffset - Return the offset of the debug information entry's
299 /// sibling.
300 unsigned SiblingOffset() const { return Offset + Size; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000301
Jim Laskeyef42a012006-11-02 20:12:39 +0000302 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
303 ///
304 void AddSiblingOffset();
305
306 /// AddChild - Add a child to the DIE.
307 ///
308 void AddChild(DIE *Child) {
309 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
310 Children.push_back(Child);
311 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000312
Jim Laskeyef42a012006-11-02 20:12:39 +0000313 /// Detach - Detaches objects connected to it after copying.
314 ///
315 void Detach() {
316 Children.clear();
317 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000318
Jim Laskeyef42a012006-11-02 20:12:39 +0000319 /// Profile - Used to gather unique data for the value folding set.
320 ///
321 void Profile(FoldingSetNodeID &ID) ;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000322
Jim Laskeyef42a012006-11-02 20:12:39 +0000323#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000324 void print(std::ostream *O, unsigned IncIndent = 0) {
325 if (O) print(*O, IncIndent);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000326 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000327 void print(std::ostream &O, unsigned IncIndent = 0);
328 void dump();
329#endif
330};
331
332//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000333/// DIEValue - A debug information entry value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000334///
335class DIEValue : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000336public:
337 enum {
338 isInteger,
339 isString,
340 isLabel,
341 isAsIsLabel,
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000342 isSectionOffset,
Jim Laskey0d086af2006-02-27 12:43:29 +0000343 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000344 isEntry,
345 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000346 };
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000347
Jim Laskeyef42a012006-11-02 20:12:39 +0000348 /// Type - Type of data stored in the value.
349 ///
350 unsigned Type;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000351
Dan Gohman81975f62007-08-27 14:50:10 +0000352 explicit DIEValue(unsigned T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000353 : Type(T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000354 {}
Jim Laskey0d086af2006-02-27 12:43:29 +0000355 virtual ~DIEValue() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000356
Jim Laskeyf6733882006-11-02 21:48:18 +0000357 // Accessors
Jim Laskeyef42a012006-11-02 20:12:39 +0000358 unsigned getType() const { return Type; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000359
Jim Laskey0d086af2006-02-27 12:43:29 +0000360 // Implement isa/cast/dyncast.
361 static bool classof(const DIEValue *) { return true; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000362
Jim Laskey0d086af2006-02-27 12:43:29 +0000363 /// EmitValue - Emit value via the Dwarf writer.
364 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000365 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000366
Jim Laskey0d086af2006-02-27 12:43:29 +0000367 /// SizeOf - Return the size of a value in bytes.
368 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000369 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000370
Jim Laskeyef42a012006-11-02 20:12:39 +0000371 /// Profile - Used to gather unique data for the value folding set.
372 ///
373 virtual void Profile(FoldingSetNodeID &ID) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000374
Jim Laskeyef42a012006-11-02 20:12:39 +0000375#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000376 void print(std::ostream *O) {
377 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000378 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000379 virtual void print(std::ostream &O) = 0;
380 void dump();
381#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000382};
Jim Laskey063e7652006-01-17 17:31:53 +0000383
Jim Laskey0d086af2006-02-27 12:43:29 +0000384//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000385/// DWInteger - An integer value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000386///
Jim Laskey0d086af2006-02-27 12:43:29 +0000387class DIEInteger : public DIEValue {
388private:
389 uint64_t Integer;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000390
Jim Laskey0d086af2006-02-27 12:43:29 +0000391public:
Dan Gohman81975f62007-08-27 14:50:10 +0000392 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000393
Jim Laskey0d086af2006-02-27 12:43:29 +0000394 // Implement isa/cast/dyncast.
395 static bool classof(const DIEInteger *) { return true; }
396 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000397
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000398 /// BestForm - Choose the best form for integer.
399 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000400 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
401 if (IsSigned) {
402 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
403 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
404 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
405 } else {
406 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
407 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
408 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
409 }
410 return DW_FORM_data8;
411 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000412
Jim Laskey0d086af2006-02-27 12:43:29 +0000413 /// EmitValue - Emit integer of appropriate size.
414 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000415 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000416
Jim Laskey0d086af2006-02-27 12:43:29 +0000417 /// SizeOf - Determine size of integer value in bytes.
418 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000419 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000420
Jim Laskeyef42a012006-11-02 20:12:39 +0000421 /// Profile - Used to gather unique data for the value folding set.
422 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000423 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000424 ID.AddInteger(isInteger);
Jim Laskeyef42a012006-11-02 20:12:39 +0000425 ID.AddInteger(Integer);
426 }
Jim Laskey5496f012006-11-09 14:52:14 +0000427 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000428
Jim Laskeyef42a012006-11-02 20:12:39 +0000429#ifndef NDEBUG
430 virtual void print(std::ostream &O) {
431 O << "Int: " << (int64_t)Integer
432 << " 0x" << std::hex << Integer << std::dec;
433 }
434#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000435};
Jim Laskey063e7652006-01-17 17:31:53 +0000436
Jim Laskey0d086af2006-02-27 12:43:29 +0000437//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000438/// DIEString - A string value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000439///
Jim Laskeyef42a012006-11-02 20:12:39 +0000440class DIEString : public DIEValue {
441public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000442 const std::string String;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000443
Dan Gohman81975f62007-08-27 14:50:10 +0000444 explicit DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000445
Jim Laskey0d086af2006-02-27 12:43:29 +0000446 // Implement isa/cast/dyncast.
447 static bool classof(const DIEString *) { return true; }
448 static bool classof(const DIEValue *S) { return S->Type == isString; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000449
Jim Laskey0d086af2006-02-27 12:43:29 +0000450 /// EmitValue - Emit string value.
451 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000452 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000453
Jim Laskey0d086af2006-02-27 12:43:29 +0000454 /// SizeOf - Determine size of string value in bytes.
455 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000456 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000457 return String.size() + sizeof(char); // sizeof('\0');
458 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000459
Jim Laskeyef42a012006-11-02 20:12:39 +0000460 /// Profile - Used to gather unique data for the value folding set.
461 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000462 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000463 ID.AddInteger(isString);
Jim Laskeyef42a012006-11-02 20:12:39 +0000464 ID.AddString(String);
465 }
Jim Laskey5496f012006-11-09 14:52:14 +0000466 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000467
Jim Laskeyef42a012006-11-02 20:12:39 +0000468#ifndef NDEBUG
469 virtual void print(std::ostream &O) {
470 O << "Str: \"" << String << "\"";
471 }
472#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000473};
Jim Laskey063e7652006-01-17 17:31:53 +0000474
Jim Laskey0d086af2006-02-27 12:43:29 +0000475//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000476/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000477//
Jim Laskeyef42a012006-11-02 20:12:39 +0000478class DIEDwarfLabel : public DIEValue {
479public:
480
Jim Laskey0d086af2006-02-27 12:43:29 +0000481 const DWLabel Label;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000482
Dan Gohman81975f62007-08-27 14:50:10 +0000483 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000484
Jim Laskey0d086af2006-02-27 12:43:29 +0000485 // Implement isa/cast/dyncast.
486 static bool classof(const DIEDwarfLabel *) { return true; }
487 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000488
Jim Laskey0d086af2006-02-27 12:43:29 +0000489 /// EmitValue - Emit label value.
490 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000491 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000492
Jim Laskey0d086af2006-02-27 12:43:29 +0000493 /// SizeOf - Determine size of label value in bytes.
494 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000495 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000496
Jim Laskeyef42a012006-11-02 20:12:39 +0000497 /// Profile - Used to gather unique data for the value folding set.
498 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000499 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000500 ID.AddInteger(isLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000501 Label.Profile(ID);
502 }
Jim Laskey5496f012006-11-09 14:52:14 +0000503 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000504
Jim Laskeyef42a012006-11-02 20:12:39 +0000505#ifndef NDEBUG
506 virtual void print(std::ostream &O) {
507 O << "Lbl: ";
508 Label.print(O);
509 }
510#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000511};
Jim Laskey063e7652006-01-17 17:31:53 +0000512
Jim Laskey063e7652006-01-17 17:31:53 +0000513
Jim Laskey0d086af2006-02-27 12:43:29 +0000514//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000515/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000516//
Jim Laskeyef42a012006-11-02 20:12:39 +0000517class DIEObjectLabel : public DIEValue {
518public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000519 const std::string Label;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000520
Dan Gohman81975f62007-08-27 14:50:10 +0000521 explicit DIEObjectLabel(const std::string &L)
522 : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000523
Jim Laskey0d086af2006-02-27 12:43:29 +0000524 // Implement isa/cast/dyncast.
525 static bool classof(const DIEObjectLabel *) { return true; }
526 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000527
Jim Laskey0d086af2006-02-27 12:43:29 +0000528 /// EmitValue - Emit label value.
529 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000530 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000531
Jim Laskey0d086af2006-02-27 12:43:29 +0000532 /// SizeOf - Determine size of label value in bytes.
533 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000534 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000535
Jim Laskeyef42a012006-11-02 20:12:39 +0000536 /// Profile - Used to gather unique data for the value folding set.
537 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000538 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000539 ID.AddInteger(isAsIsLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000540 ID.AddString(Label);
541 }
Jim Laskey5496f012006-11-09 14:52:14 +0000542 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000543
544#ifndef NDEBUG
545 virtual void print(std::ostream &O) {
546 O << "Obj: " << Label;
547 }
548#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000549};
Jim Laskey063e7652006-01-17 17:31:53 +0000550
Jim Laskey0d086af2006-02-27 12:43:29 +0000551//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000552/// DIESectionOffset - A section offset DIE.
553//
554class DIESectionOffset : public DIEValue {
555public:
556 const DWLabel Label;
557 const DWLabel Section;
558 bool IsEH : 1;
559 bool UseSet : 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000560
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000561 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
562 bool isEH = false, bool useSet = true)
563 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
564 IsEH(isEH), UseSet(useSet) {}
565
566 // Implement isa/cast/dyncast.
567 static bool classof(const DIESectionOffset *) { return true; }
568 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000569
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000570 /// EmitValue - Emit section offset.
571 ///
572 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000573
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000574 /// SizeOf - Determine size of section offset value in bytes.
575 ///
576 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000577
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000578 /// Profile - Used to gather unique data for the value folding set.
579 ///
580 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
581 const DWLabel &Section) {
582 ID.AddInteger(isSectionOffset);
583 Label.Profile(ID);
584 Section.Profile(ID);
585 // IsEH and UseSet are specific to the Label/Section that we will emit
586 // the offset for; so Label/Section are enough for uniqueness.
587 }
588 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
589
590#ifndef NDEBUG
591 virtual void print(std::ostream &O) {
592 O << "Off: ";
593 Label.print(O);
594 O << "-";
595 Section.print(O);
596 O << "-" << IsEH << "-" << UseSet;
597 }
598#endif
599};
600
601//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000602/// DIEDelta - A simple label difference DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000603///
Jim Laskeyef42a012006-11-02 20:12:39 +0000604class DIEDelta : public DIEValue {
605public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000606 const DWLabel LabelHi;
607 const DWLabel LabelLo;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000608
Jim Laskey0d086af2006-02-27 12:43:29 +0000609 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
610 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000611
Jim Laskey0d086af2006-02-27 12:43:29 +0000612 // Implement isa/cast/dyncast.
613 static bool classof(const DIEDelta *) { return true; }
614 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000615
Jim Laskey0d086af2006-02-27 12:43:29 +0000616 /// EmitValue - Emit delta value.
617 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000618 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000619
Jim Laskey0d086af2006-02-27 12:43:29 +0000620 /// SizeOf - Determine size of delta value in bytes.
621 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000622 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000623
Jim Laskeyef42a012006-11-02 20:12:39 +0000624 /// Profile - Used to gather unique data for the value folding set.
625 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000626 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
627 const DWLabel &LabelLo) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000628 ID.AddInteger(isDelta);
Jim Laskeyef42a012006-11-02 20:12:39 +0000629 LabelHi.Profile(ID);
630 LabelLo.Profile(ID);
631 }
Jim Laskey5496f012006-11-09 14:52:14 +0000632 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000633
634#ifndef NDEBUG
635 virtual void print(std::ostream &O) {
636 O << "Del: ";
637 LabelHi.print(O);
638 O << "-";
639 LabelLo.print(O);
640 }
641#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000642};
Jim Laskey063e7652006-01-17 17:31:53 +0000643
Jim Laskey0d086af2006-02-27 12:43:29 +0000644//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000645/// DIEntry - A pointer to another debug information entry. An instance of this
646/// class can also be used as a proxy for a debug information entry not yet
647/// defined (ie. types.)
648class DIEntry : public DIEValue {
649public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000650 DIE *Entry;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000651
Dan Gohman81975f62007-08-27 14:50:10 +0000652 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000653
Jim Laskey0d086af2006-02-27 12:43:29 +0000654 // Implement isa/cast/dyncast.
655 static bool classof(const DIEntry *) { return true; }
656 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000657
Jim Laskeyb8509c52006-03-23 18:07:55 +0000658 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000659 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000660 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000661
Jim Laskeyb8509c52006-03-23 18:07:55 +0000662 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000663 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000664 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000665 return sizeof(int32_t);
666 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000667
Jim Laskeyef42a012006-11-02 20:12:39 +0000668 /// Profile - Used to gather unique data for the value folding set.
669 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000670 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
671 ID.AddInteger(isEntry);
672 ID.AddPointer(Entry);
673 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000674 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000675 ID.AddInteger(isEntry);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000676
Jim Laskeyef42a012006-11-02 20:12:39 +0000677 if (Entry) {
678 ID.AddPointer(Entry);
679 } else {
680 ID.AddPointer(this);
681 }
682 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000683
Jim Laskeyef42a012006-11-02 20:12:39 +0000684#ifndef NDEBUG
685 virtual void print(std::ostream &O) {
686 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
687 }
688#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000689};
690
691//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000692/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000693//
Jim Laskeyef42a012006-11-02 20:12:39 +0000694class DIEBlock : public DIEValue, public DIE {
695public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000696 unsigned Size; // Size in bytes excluding size header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000697
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000698 DIEBlock()
699 : DIEValue(isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +0000700 , DIE(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000701 , Size(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000702 {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000703 ~DIEBlock() {
704 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000705
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000706 // Implement isa/cast/dyncast.
707 static bool classof(const DIEBlock *) { return true; }
708 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000709
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000710 /// ComputeSize - calculate the size of the block.
711 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000712 unsigned ComputeSize(DwarfDebug &DD);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000713
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000714 /// BestForm - Choose the best form for data.
715 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000716 unsigned BestForm() const {
717 if ((unsigned char)Size == Size) return DW_FORM_block1;
718 if ((unsigned short)Size == Size) return DW_FORM_block2;
719 if ((unsigned int)Size == Size) return DW_FORM_block4;
720 return DW_FORM_block;
721 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000722
723 /// EmitValue - Emit block data.
724 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000725 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000726
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000727 /// SizeOf - Determine size of block data in bytes.
728 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000729 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000730
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000731
Jim Laskeyef42a012006-11-02 20:12:39 +0000732 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000733 ///
Reid Spencer97821312006-11-02 23:56:21 +0000734 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000735 ID.AddInteger(isBlock);
Jim Laskeyef42a012006-11-02 20:12:39 +0000736 DIE::Profile(ID);
737 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000738
Jim Laskeyef42a012006-11-02 20:12:39 +0000739#ifndef NDEBUG
740 virtual void print(std::ostream &O) {
741 O << "Blk: ";
742 DIE::print(O, 5);
743 }
744#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000745};
746
747//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000748/// CompileUnit - This dwarf writer support class manages information associate
749/// with a source file.
750class CompileUnit {
Jim Laskey0d086af2006-02-27 12:43:29 +0000751private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000752 /// Desc - Compile unit debug descriptor.
753 ///
754 CompileUnitDesc *Desc;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000755
Jim Laskeyef42a012006-11-02 20:12:39 +0000756 /// ID - File identifier for source.
757 ///
758 unsigned ID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000759
Jim Laskeyef42a012006-11-02 20:12:39 +0000760 /// Die - Compile unit debug information entry.
761 ///
762 DIE *Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000763
Jim Laskeyef42a012006-11-02 20:12:39 +0000764 /// DescToDieMap - Tracks the mapping of unit level debug informaton
765 /// descriptors to debug information entries.
766 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
Devang Patel48d190f2009-01-05 21:47:57 +0000767 DenseMap<GlobalVariable *, DIE *> GVToDieMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000768
769 /// DescToDIEntryMap - Tracks the mapping of unit level debug informaton
770 /// descriptors to debug information entries using a DIEntry proxy.
771 std::map<DebugInfoDesc *, DIEntry *> DescToDIEntryMap;
Devang Patel48d190f2009-01-05 21:47:57 +0000772 DenseMap<GlobalVariable *, DIEntry *> GVToDIEntryMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000773
774 /// Globals - A map of globally visible named entities for this unit.
775 ///
776 std::map<std::string, DIE *> Globals;
777
778 /// DiesSet - Used to uniquely define dies within the compile unit.
779 ///
780 FoldingSet<DIE> DiesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000781
Jim Laskeyef42a012006-11-02 20:12:39 +0000782 /// Dies - List of all dies in the compile unit.
783 ///
784 std::vector<DIE *> Dies;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000785
Jim Laskey0d086af2006-02-27 12:43:29 +0000786public:
Devang Pateld1ca9252009-01-05 23:03:32 +0000787 CompileUnit(unsigned I, DIE *D)
788 : ID(I), Die(D), DescToDieMap(), GVToDieMap(), DescToDIEntryMap(),
789 GVToDIEntryMap(), Globals(), DiesSet(InitDiesSetSize), Dies()
790 {}
791
Jim Laskeyef42a012006-11-02 20:12:39 +0000792 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
793 : Desc(CUD)
794 , ID(I)
795 , Die(D)
796 , DescToDieMap()
Devang Patel48d190f2009-01-05 21:47:57 +0000797 , GVToDieMap()
Jim Laskeyef42a012006-11-02 20:12:39 +0000798 , DescToDIEntryMap()
Devang Patel48d190f2009-01-05 21:47:57 +0000799 , GVToDIEntryMap()
Jim Laskeyef42a012006-11-02 20:12:39 +0000800 , Globals()
801 , DiesSet(InitDiesSetSize)
802 , Dies()
803 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000804
Jim Laskeyef42a012006-11-02 20:12:39 +0000805 ~CompileUnit() {
806 delete Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000807
Jim Laskeyef42a012006-11-02 20:12:39 +0000808 for (unsigned i = 0, N = Dies.size(); i < N; ++i)
809 delete Dies[i];
810 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000811
Jim Laskeybd761842006-02-27 17:27:12 +0000812 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000813 CompileUnitDesc *getDesc() const { return Desc; }
814 unsigned getID() const { return ID; }
815 DIE* getDie() const { return Die; }
816 std::map<std::string, DIE *> &getGlobals() { return Globals; }
817
818 /// hasContent - Return true if this compile unit has something to write out.
819 ///
820 bool hasContent() const {
821 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000822 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000823
824 /// AddGlobal - Add a new global entity to the compile unit.
825 ///
826 void AddGlobal(const std::string &Name, DIE *Die) {
827 Globals[Name] = Die;
828 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000829
Jim Laskeyef42a012006-11-02 20:12:39 +0000830 /// getDieMapSlotFor - Returns the debug information entry map slot for the
831 /// specified debug descriptor.
Jim Laskey072200c2007-01-29 18:51:14 +0000832 DIE *&getDieMapSlotFor(DebugInfoDesc *DID) {
833 return DescToDieMap[DID];
Jim Laskeyef42a012006-11-02 20:12:39 +0000834 }
Devang Patel48d190f2009-01-05 21:47:57 +0000835 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
836 return GVToDieMap[GV];
837 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000838
Jim Laskeyef42a012006-11-02 20:12:39 +0000839 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
840 /// specified debug descriptor.
Jim Laskey072200c2007-01-29 18:51:14 +0000841 DIEntry *&getDIEntrySlotFor(DebugInfoDesc *DID) {
842 return DescToDIEntryMap[DID];
Jim Laskeyef42a012006-11-02 20:12:39 +0000843 }
Devang Patel48d190f2009-01-05 21:47:57 +0000844 DIEntry *&getDIEntrySlotFor(GlobalVariable *GV) {
845 return GVToDIEntryMap[GV];
846 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000847
Jim Laskeyef42a012006-11-02 20:12:39 +0000848 /// AddDie - Adds or interns the DIE to the compile unit.
849 ///
850 DIE *AddDie(DIE &Buffer) {
851 FoldingSetNodeID ID;
852 Buffer.Profile(ID);
853 void *Where;
854 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000855
Jim Laskeyef42a012006-11-02 20:12:39 +0000856 if (!Die) {
857 Die = new DIE(Buffer);
858 DiesSet.InsertNode(Die, Where);
859 this->Die->AddChild(Die);
860 Buffer.Detach();
861 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000862
Jim Laskeyef42a012006-11-02 20:12:39 +0000863 return Die;
864 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000865};
866
Jim Laskey65195462006-10-30 13:35:07 +0000867//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000868/// Dwarf - Emits general Dwarf directives.
Jim Laskeyef42a012006-11-02 20:12:39 +0000869///
Jim Laskey65195462006-10-30 13:35:07 +0000870class Dwarf {
871
Jim Laskey072200c2007-01-29 18:51:14 +0000872protected:
Jim Laskey65195462006-10-30 13:35:07 +0000873
874 //===--------------------------------------------------------------------===//
Jim Laskey072200c2007-01-29 18:51:14 +0000875 // Core attributes used by the Dwarf writer.
Jim Laskey65195462006-10-30 13:35:07 +0000876 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000877
Jim Laskey65195462006-10-30 13:35:07 +0000878 //
879 /// O - Stream to .s file.
880 ///
Owen Andersoncb371882008-08-21 00:14:44 +0000881 raw_ostream &O;
Jim Laskey65195462006-10-30 13:35:07 +0000882
883 /// Asm - Target of Dwarf emission.
884 ///
885 AsmPrinter *Asm;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000886
Bill Wendlingaa8f8882008-07-01 23:34:48 +0000887 /// TAI - Target asm information.
Jim Laskey65195462006-10-30 13:35:07 +0000888 const TargetAsmInfo *TAI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000889
Jim Laskey65195462006-10-30 13:35:07 +0000890 /// TD - Target data.
891 const TargetData *TD;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000892
Jim Laskey65195462006-10-30 13:35:07 +0000893 /// RI - Register Information.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000894 const TargetRegisterInfo *RI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000895
Jim Laskey65195462006-10-30 13:35:07 +0000896 /// M - Current module.
897 ///
898 Module *M;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000899
Jim Laskey65195462006-10-30 13:35:07 +0000900 /// MF - Current machine function.
901 ///
902 MachineFunction *MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000903
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000904 /// MMI - Collected machine module information.
Jim Laskey65195462006-10-30 13:35:07 +0000905 ///
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000906 MachineModuleInfo *MMI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000907
Jim Laskey65195462006-10-30 13:35:07 +0000908 /// SubprogramCount - The running count of functions being compiled.
909 ///
910 unsigned SubprogramCount;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000911
Chris Lattner9251f132007-09-24 03:35:37 +0000912 /// Flavor - A unique string indicating what dwarf producer this is, used to
913 /// unique labels.
914 const char * const Flavor;
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000915
916 unsigned SetCounter;
Owen Andersoncb371882008-08-21 00:14:44 +0000917 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattner9251f132007-09-24 03:35:37 +0000918 const char *flavor)
Jim Laskey072200c2007-01-29 18:51:14 +0000919 : O(OS)
920 , Asm(A)
921 , TAI(T)
922 , TD(Asm->TM.getTargetData())
923 , RI(Asm->TM.getRegisterInfo())
924 , M(NULL)
925 , MF(NULL)
926 , MMI(NULL)
Jim Laskey072200c2007-01-29 18:51:14 +0000927 , SubprogramCount(0)
Chris Lattner9251f132007-09-24 03:35:37 +0000928 , Flavor(flavor)
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000929 , SetCounter(1)
Jim Laskey072200c2007-01-29 18:51:14 +0000930 {
931 }
932
933public:
934
935 //===--------------------------------------------------------------------===//
936 // Accessors.
937 //
938 AsmPrinter *getAsm() const { return Asm; }
Jim Laskeyb82313f2007-02-01 16:31:34 +0000939 MachineModuleInfo *getMMI() const { return MMI; }
Jim Laskey072200c2007-01-29 18:51:14 +0000940 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohman82482942007-09-27 23:12:31 +0000941 const TargetData *getTargetData() const { return TD; }
Jim Laskey072200c2007-01-29 18:51:14 +0000942
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000943 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
944 const {
945 if (isInSection && TAI->getDwarfSectionOffsetDirective())
946 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohman82482942007-09-27 23:12:31 +0000947 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000948 O << TAI->getData32bitsDirective();
949 else
950 O << TAI->getData64bitsDirective();
951 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000952
Jim Laskeyb82313f2007-02-01 16:31:34 +0000953 /// PrintLabelName - Print label name in form used by Dwarf writer.
954 ///
955 void PrintLabelName(DWLabel Label) const {
956 PrintLabelName(Label.Tag, Label.Number);
957 }
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000958 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000959 O << TAI->getPrivateGlobalPrefix() << Tag;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000960 if (Number) O << Number;
961 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000962
Chris Lattner9251f132007-09-24 03:35:37 +0000963 void PrintLabelName(const char *Tag, unsigned Number,
964 const char *Suffix) const {
965 O << TAI->getPrivateGlobalPrefix() << Tag;
966 if (Number) O << Number;
967 O << Suffix;
968 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000969
Jim Laskeyb82313f2007-02-01 16:31:34 +0000970 /// EmitLabel - Emit location label for internal use by Dwarf.
971 ///
972 void EmitLabel(DWLabel Label) const {
973 EmitLabel(Label.Tag, Label.Number);
974 }
975 void EmitLabel(const char *Tag, unsigned Number) const {
976 PrintLabelName(Tag, Number);
977 O << ":\n";
978 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000979
Jim Laskeyb82313f2007-02-01 16:31:34 +0000980 /// EmitReference - Emit a reference to a label.
981 ///
Dan Gohman06ff4e62007-09-28 15:43:33 +0000982 void EmitReference(DWLabel Label, bool IsPCRelative = false,
983 bool Force32Bit = false) const {
984 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000985 }
986 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman06ff4e62007-09-28 15:43:33 +0000987 bool IsPCRelative = false, bool Force32Bit = false) const {
988 PrintRelDirective(Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000989 PrintLabelName(Tag, Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000990
Jim Laskeyb82313f2007-02-01 16:31:34 +0000991 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
992 }
Dan Gohman06ff4e62007-09-28 15:43:33 +0000993 void EmitReference(const std::string &Name, bool IsPCRelative = false,
994 bool Force32Bit = false) const {
995 PrintRelDirective(Force32Bit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000996
Jim Laskeyb82313f2007-02-01 16:31:34 +0000997 O << Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000998
Jim Laskeyb82313f2007-02-01 16:31:34 +0000999 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
1000 }
1001
1002 /// EmitDifference - Emit the difference between two labels. Some
1003 /// assemblers do not behave with absolute expressions with data directives,
1004 /// so there is an option (needsSet) to use an intermediary set expression.
1005 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +00001006 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001007 EmitDifference(LabelHi.Tag, LabelHi.Number,
1008 LabelLo.Tag, LabelLo.Number,
1009 IsSmall);
1010 }
1011 void EmitDifference(const char *TagHi, unsigned NumberHi,
1012 const char *TagLo, unsigned NumberLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +00001013 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001014 if (TAI->needsSet()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001015 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +00001016 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001017 O << ",";
1018 PrintLabelName(TagHi, NumberHi);
1019 O << "-";
1020 PrintLabelName(TagLo, NumberLo);
1021 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001022
1023 PrintRelDirective(IsSmall);
Chris Lattner9251f132007-09-24 03:35:37 +00001024 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001025 ++SetCounter;
1026 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001027 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001028
Jim Laskeyb82313f2007-02-01 16:31:34 +00001029 PrintLabelName(TagHi, NumberHi);
1030 O << "-";
1031 PrintLabelName(TagLo, NumberLo);
1032 }
1033 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001034
1035 void EmitSectionOffset(const char* Label, const char* Section,
1036 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesend9ffd4c2008-03-26 23:31:39 +00001037 bool IsSmall = false, bool isEH = false,
1038 bool useSet = true) {
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001039 bool printAbsolute = false;
Dale Johannesend9ffd4c2008-03-26 23:31:39 +00001040 if (isEH)
1041 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
1042 else
1043 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
1044
1045 if (TAI->needsSet() && useSet) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001046 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +00001047 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001048 O << ",";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001049 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001050
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001051 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001052 O << "-";
1053 PrintLabelName(Section, SectionNumber);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001054 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001055 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001056
1057 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001058
Chris Lattner9251f132007-09-24 03:35:37 +00001059 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001060 ++SetCounter;
1061 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001062 PrintRelDirective(IsSmall, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001063
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001064 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001065
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001066 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001067 O << "-";
1068 PrintLabelName(Section, SectionNumber);
1069 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001070 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001071 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001072
Jim Laskeyb82313f2007-02-01 16:31:34 +00001073 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1074 /// frame.
1075 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenb97aec62007-11-13 19:13:01 +00001076 const std::vector<MachineMove> &Moves, bool isEH) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001077 int stackGrowth =
1078 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1079 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00001080 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeybacd3042007-02-21 22:48:45 +00001081 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
Jim Laskeyb82313f2007-02-01 16:31:34 +00001082
1083 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001084 const MachineMove &Move = Moves[i];
Jim Laskeyb82313f2007-02-01 16:31:34 +00001085 unsigned LabelID = Move.getLabelID();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001086
Jim Laskeyb82313f2007-02-01 16:31:34 +00001087 if (LabelID) {
1088 LabelID = MMI->MappedLabel(LabelID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001089
Jim Laskeyb82313f2007-02-01 16:31:34 +00001090 // Throw out move if the label is invalid.
1091 if (!LabelID) continue;
1092 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001093
Jim Laskeyb82313f2007-02-01 16:31:34 +00001094 const MachineLocation &Dst = Move.getDestination();
1095 const MachineLocation &Src = Move.getSource();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001096
Jim Laskeyb82313f2007-02-01 16:31:34 +00001097 // Advance row if new location.
1098 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1099 Asm->EmitInt8(DW_CFA_advance_loc4);
1100 Asm->EOL("DW_CFA_advance_loc4");
Jim Laskeybacd3042007-02-21 22:48:45 +00001101 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1102 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001103
Jim Laskeyb82313f2007-02-01 16:31:34 +00001104 BaseLabelID = LabelID;
Jim Laskeybacd3042007-02-21 22:48:45 +00001105 BaseLabel = "label";
Jim Laskeyb82313f2007-02-01 16:31:34 +00001106 IsLocal = true;
1107 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001108
Jim Laskeyb82313f2007-02-01 16:31:34 +00001109 // If advancing cfa.
Dan Gohmand735b802008-10-03 15:45:36 +00001110 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1111 if (!Src.isReg()) {
1112 if (Src.getReg() == MachineLocation::VirtualFP) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001113 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1114 Asm->EOL("DW_CFA_def_cfa_offset");
1115 } else {
1116 Asm->EmitInt8(DW_CFA_def_cfa);
1117 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmand735b802008-10-03 15:45:36 +00001118 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001119 Asm->EOL("Register");
1120 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001121
Jim Laskeyb82313f2007-02-01 16:31:34 +00001122 int Offset = -Src.getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001123
Jim Laskeyb82313f2007-02-01 16:31:34 +00001124 Asm->EmitULEB128Bytes(Offset);
1125 Asm->EOL("Offset");
1126 } else {
1127 assert(0 && "Machine move no supported yet.");
1128 }
Dan Gohmand735b802008-10-03 15:45:36 +00001129 } else if (Src.isReg() &&
1130 Src.getReg() == MachineLocation::VirtualFP) {
1131 if (Dst.isReg()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001132 Asm->EmitInt8(DW_CFA_def_cfa_register);
1133 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmand735b802008-10-03 15:45:36 +00001134 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001135 Asm->EOL("Register");
1136 } else {
1137 assert(0 && "Machine move no supported yet.");
1138 }
1139 } else {
Dan Gohmand735b802008-10-03 15:45:36 +00001140 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001141 int Offset = Dst.getOffset() / stackGrowth;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001142
Jim Laskeyb82313f2007-02-01 16:31:34 +00001143 if (Offset < 0) {
1144 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1145 Asm->EOL("DW_CFA_offset_extended_sf");
1146 Asm->EmitULEB128Bytes(Reg);
1147 Asm->EOL("Reg");
1148 Asm->EmitSLEB128Bytes(Offset);
1149 Asm->EOL("Offset");
1150 } else if (Reg < 64) {
1151 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Chengd4114192008-07-09 21:53:02 +00001152 if (VerboseAsm)
1153 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1154 else
1155 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00001156 Asm->EmitULEB128Bytes(Offset);
1157 Asm->EOL("Offset");
1158 } else {
1159 Asm->EmitInt8(DW_CFA_offset_extended);
1160 Asm->EOL("DW_CFA_offset_extended");
1161 Asm->EmitULEB128Bytes(Reg);
1162 Asm->EOL("Reg");
1163 Asm->EmitULEB128Bytes(Offset);
1164 Asm->EOL("Offset");
1165 }
1166 }
1167 }
1168 }
1169
Jim Laskey072200c2007-01-29 18:51:14 +00001170};
1171
1172//===----------------------------------------------------------------------===//
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001173/// SourceLineInfo - This class is used to record source line correspondence.
1174///
1175class SrcLineInfo {
1176 unsigned Line; // Source line number.
1177 unsigned Column; // Source column.
1178 unsigned SourceID; // Source ID number.
1179 unsigned LabelID; // Label in code ID number.
1180public:
1181 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
1182 : Line(L), Column(C), SourceID(S), LabelID(I) {}
1183
1184 // Accessors
1185 unsigned getLine() const { return Line; }
1186 unsigned getColumn() const { return Column; }
1187 unsigned getSourceID() const { return SourceID; }
1188 unsigned getLabelID() const { return LabelID; }
1189};
1190
1191
1192//===----------------------------------------------------------------------===//
Devang Patel8526cc02009-01-05 22:35:52 +00001193/// SrcFileInfo - This class is used to track source information.
1194///
1195class SrcFileInfo {
1196 unsigned DirectoryID; // Directory ID number.
1197 std::string Name; // File name (not including directory.)
1198public:
1199 SrcFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
1200
1201 // Accessors
1202 unsigned getDirectoryID() const { return DirectoryID; }
1203 const std::string &getName() const { return Name; }
1204
1205 /// operator== - Used by UniqueVector to locate entry.
1206 ///
1207 bool operator==(const SourceFileInfo &SI) const {
1208 return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
1209 }
1210
1211 /// operator< - Used by UniqueVector to locate entry.
1212 ///
1213 bool operator<(const SrcFileInfo &SI) const {
1214 return getDirectoryID() < SI.getDirectoryID() ||
1215 (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
1216 }
1217};
1218
1219//===----------------------------------------------------------------------===//
Devang Patel7a6e5a32009-01-08 02:33:41 +00001220/// DbgVariable - This class is used to track local variable information.
1221///
1222class DbgVariable {
1223private:
1224 DIVariable *Var; // Variable Descriptor.
1225 unsigned FrameIndex; // Variable frame index.
1226
1227public:
1228 DbgVariable(DIVariable *V, unsigned I) : Var(V), FrameIndex(I) {}
1229
1230 // Accessors.
1231 DIVariable *getVariable() const { return Var; }
1232 unsigned getFrameIndex() const { return FrameIndex; }
1233};
1234
1235//===----------------------------------------------------------------------===//
1236/// DbgScope - This class is used to track scope information.
1237///
1238class DbgScope {
1239private:
1240 DbgScope *Parent; // Parent to this scope.
1241 DIDescriptor *Desc; // Debug info descriptor for scope.
1242 // Either subprogram or block.
1243 unsigned StartLabelID; // Label ID of the beginning of scope.
1244 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel9795da52009-01-10 02:42:49 +00001245 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
1246 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001247
1248public:
1249 DbgScope(DbgScope *P, DIDescriptor *D)
1250 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), Scopes(), Variables()
1251 {}
Devang Patel481ff5b2009-01-12 18:48:36 +00001252 ~DbgScope() {
1253 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1254 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1255 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001256
1257 // Accessors.
1258 DbgScope *getParent() const { return Parent; }
1259 DIDescriptor *getDesc() const { return Desc; }
1260 unsigned getStartLabelID() const { return StartLabelID; }
1261 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel9795da52009-01-10 02:42:49 +00001262 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1263 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001264 void setStartLabelID(unsigned S) { StartLabelID = S; }
1265 void setEndLabelID(unsigned E) { EndLabelID = E; }
1266
1267 /// AddScope - Add a scope to the scope.
1268 ///
1269 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1270
1271 /// AddVariable - Add a variable to the scope.
1272 ///
1273 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
1274};
1275
1276//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001277/// DwarfDebug - Emits Dwarf debug directives.
Jim Laskey072200c2007-01-29 18:51:14 +00001278///
1279class DwarfDebug : public Dwarf {
1280
1281private:
Jim Laskey65195462006-10-30 13:35:07 +00001282 //===--------------------------------------------------------------------===//
1283 // Attributes used to construct specific Dwarf sections.
1284 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001285
Jim Laskey65195462006-10-30 13:35:07 +00001286 /// CompileUnits - All the compile units involved in this build. The index
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001287 /// of each entry in this vector corresponds to the sources in MMI.
Jim Laskey65195462006-10-30 13:35:07 +00001288 std::vector<CompileUnit *> CompileUnits;
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001289 DenseMap<Value *, CompileUnit *> DW_CUs;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001290
Jim Laskeyef42a012006-11-02 20:12:39 +00001291 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +00001292 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001293 FoldingSet<DIEAbbrev> AbbreviationsSet;
1294
1295 /// Abbreviations - A list of all the unique abbreviations in use.
1296 ///
1297 std::vector<DIEAbbrev *> Abbreviations;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001298
Jim Laskeyef42a012006-11-02 20:12:39 +00001299 /// ValuesSet - Used to uniquely define values.
1300 ///
Devang Patel8526cc02009-01-05 22:35:52 +00001301 // Directories - Uniquing vector for directories.
1302 UniqueVector<std::string> Directories;
1303
1304 // SourceFiles - Uniquing vector for source files.
1305 UniqueVector<SrcFileInfo> SrcFiles;
1306
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001307 // Lines - List of of source line correspondence.
1308 std::vector<SrcLineInfo> Lines;
1309
Jim Laskeyef42a012006-11-02 20:12:39 +00001310 FoldingSet<DIEValue> ValuesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001311
Jim Laskeyef42a012006-11-02 20:12:39 +00001312 /// Values - A list of all the unique values in use.
1313 ///
1314 std::vector<DIEValue *> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001315
Jim Laskey65195462006-10-30 13:35:07 +00001316 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +00001317 ///
Jim Laskey65195462006-10-30 13:35:07 +00001318 UniqueVector<std::string> StringPool;
1319
1320 /// UnitMap - Map debug information descriptor to compile unit.
1321 ///
1322 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001323
Jim Laskey65195462006-10-30 13:35:07 +00001324 /// SectionMap - Provides a unique id per text section.
1325 ///
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001326 UniqueVector<const Section*> SectionMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001327
Jim Laskey65195462006-10-30 13:35:07 +00001328 /// SectionSourceLines - Tracks line numbers per text section.
1329 ///
1330 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
1331
Jim Laskeybacd3042007-02-21 22:48:45 +00001332 /// didInitial - Flag to indicate if initial emission has been done.
1333 ///
1334 bool didInitial;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001335
Jim Laskeybacd3042007-02-21 22:48:45 +00001336 /// shouldEmit - Flag to indicate if debug information should be emitted.
1337 ///
1338 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +00001339
Devang Patel7a6e5a32009-01-08 02:33:41 +00001340 // RootScope - Top level scope for the current function.
1341 //
1342 DbgScope *RootDbgScope;
1343
1344 // DbgScopeMap - Tracks the scopes in the current function.
1345 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
1346
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001347 struct FunctionDebugFrameInfo {
1348 unsigned Number;
1349 std::vector<MachineMove> Moves;
1350
1351 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001352 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001353 };
1354
1355 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001356
Jim Laskey65195462006-10-30 13:35:07 +00001357public:
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001358
Jim Laskeybacd3042007-02-21 22:48:45 +00001359 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1360 ///
1361 bool ShouldEmitDwarf() const { return shouldEmit; }
Jim Laskey65195462006-10-30 13:35:07 +00001362
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001363 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001364 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001365 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1366 // Profile the node so that we can make it unique.
1367 FoldingSetNodeID ID;
1368 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001369
Jim Laskeyef42a012006-11-02 20:12:39 +00001370 // Check the set for priors.
1371 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001372
Jim Laskeyef42a012006-11-02 20:12:39 +00001373 // If it's newly added.
1374 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001375 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +00001376 Abbreviations.push_back(&Abbrev);
1377 // Assign the vector position + 1 as its number.
1378 Abbrev.setNumber(Abbreviations.size());
1379 } else {
1380 // Assign existing abbreviation number.
1381 Abbrev.setNumber(InSet->getNumber());
1382 }
1383 }
1384
Jim Laskey65195462006-10-30 13:35:07 +00001385 /// NewString - Add a string to the constant pool and returns a label.
1386 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001387 DWLabel NewString(const std::string &String) {
1388 unsigned StringID = StringPool.insert(String);
1389 return DWLabel("string", StringID);
1390 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001391
Jim Laskeyef42a012006-11-02 20:12:39 +00001392 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1393 /// entry.
1394 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1395 DIEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001396
Jim Laskeyef42a012006-11-02 20:12:39 +00001397 if (Entry) {
1398 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001399 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001400 void *Where;
1401 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001402
Jim Laskeyf6733882006-11-02 21:48:18 +00001403 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001404
Jim Laskeyef42a012006-11-02 20:12:39 +00001405 Value = new DIEntry(Entry);
1406 ValuesSet.InsertNode(Value, Where);
1407 } else {
1408 Value = new DIEntry(Entry);
1409 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001410
Jim Laskeyef42a012006-11-02 20:12:39 +00001411 Values.push_back(Value);
1412 return Value;
1413 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001414
Jim Laskeyef42a012006-11-02 20:12:39 +00001415 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1416 ///
1417 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1418 Value->Entry = Entry;
1419 // Add to values set if not already there. If it is, we merely have a
1420 // duplicate in the values list (no harm.)
1421 ValuesSet.GetOrInsertNode(Value);
1422 }
1423
1424 /// AddUInt - Add an unsigned integer attribute data and value.
1425 ///
1426 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1427 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1428
1429 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001430 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001431 void *Where;
1432 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1433 if (!Value) {
1434 Value = new DIEInteger(Integer);
1435 ValuesSet.InsertNode(Value, Where);
1436 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001437 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001438
Jim Laskeyef42a012006-11-02 20:12:39 +00001439 Die->AddValue(Attribute, Form, Value);
1440 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001441
Jim Laskeyef42a012006-11-02 20:12:39 +00001442 /// AddSInt - Add an signed integer attribute data and value.
1443 ///
1444 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1445 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1446
1447 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001448 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001449 void *Where;
1450 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1451 if (!Value) {
1452 Value = new DIEInteger(Integer);
1453 ValuesSet.InsertNode(Value, Where);
1454 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001455 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001456
Jim Laskeyef42a012006-11-02 20:12:39 +00001457 Die->AddValue(Attribute, Form, Value);
1458 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001459
Jim Laskeyef42a012006-11-02 20:12:39 +00001460 /// AddString - Add a std::string attribute data and value.
1461 ///
1462 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1463 const std::string &String) {
1464 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001465 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001466 void *Where;
1467 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1468 if (!Value) {
1469 Value = new DIEString(String);
1470 ValuesSet.InsertNode(Value, Where);
1471 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001472 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001473
Jim Laskeyef42a012006-11-02 20:12:39 +00001474 Die->AddValue(Attribute, Form, Value);
1475 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001476
Jim Laskeyef42a012006-11-02 20:12:39 +00001477 /// AddLabel - Add a Dwarf label attribute data and value.
1478 ///
1479 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1480 const DWLabel &Label) {
1481 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001482 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001483 void *Where;
1484 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1485 if (!Value) {
1486 Value = new DIEDwarfLabel(Label);
1487 ValuesSet.InsertNode(Value, Where);
1488 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001489 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001490
Jim Laskeyef42a012006-11-02 20:12:39 +00001491 Die->AddValue(Attribute, Form, Value);
1492 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001493
Jim Laskeyef42a012006-11-02 20:12:39 +00001494 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1495 ///
1496 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1497 const std::string &Label) {
1498 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001499 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001500 void *Where;
1501 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1502 if (!Value) {
1503 Value = new DIEObjectLabel(Label);
1504 ValuesSet.InsertNode(Value, Where);
1505 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001506 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001507
Jim Laskeyef42a012006-11-02 20:12:39 +00001508 Die->AddValue(Attribute, Form, Value);
1509 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001510
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001511 /// AddSectionOffset - Add a section offset label attribute data and value.
1512 ///
1513 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1514 const DWLabel &Label, const DWLabel &Section,
1515 bool isEH = false, bool useSet = true) {
1516 FoldingSetNodeID ID;
1517 DIESectionOffset::Profile(ID, Label, Section);
1518 void *Where;
1519 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1520 if (!Value) {
1521 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1522 ValuesSet.InsertNode(Value, Where);
1523 Values.push_back(Value);
1524 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001525
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001526 Die->AddValue(Attribute, Form, Value);
1527 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001528
Jim Laskeyef42a012006-11-02 20:12:39 +00001529 /// AddDelta - Add a label delta attribute data and value.
1530 ///
1531 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1532 const DWLabel &Hi, const DWLabel &Lo) {
1533 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001534 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001535 void *Where;
1536 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1537 if (!Value) {
1538 Value = new DIEDelta(Hi, Lo);
1539 ValuesSet.InsertNode(Value, Where);
1540 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001541 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001542
Jim Laskeyef42a012006-11-02 20:12:39 +00001543 Die->AddValue(Attribute, Form, Value);
1544 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001545
Jim Laskeyef42a012006-11-02 20:12:39 +00001546 /// AddDIEntry - Add a DIE attribute data and value.
1547 ///
1548 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1549 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1550 }
1551
1552 /// AddBlock - Add block data.
1553 ///
1554 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1555 Block->ComputeSize(*this);
1556 FoldingSetNodeID ID;
1557 Block->Profile(ID);
1558 void *Where;
1559 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1560 if (!Value) {
1561 Value = Block;
1562 ValuesSet.InsertNode(Value, Where);
1563 Values.push_back(Value);
1564 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001565 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001566 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001567 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001568 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001569
Jim Laskeyef42a012006-11-02 20:12:39 +00001570 Die->AddValue(Attribute, Block->BestForm(), Value);
1571 }
1572
Jim Laskey65195462006-10-30 13:35:07 +00001573private:
1574
1575 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001576 /// entry.
1577 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1578 if (File && Line) {
1579 CompileUnit *FileUnit = FindCompileUnit(File);
1580 unsigned FileID = FileUnit->getID();
1581 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1582 AddUInt(Die, DW_AT_decl_line, 0, Line);
1583 }
1584 }
Jim Laskey65195462006-10-30 13:35:07 +00001585
Devang Patel8526cc02009-01-05 22:35:52 +00001586 /// AddSourceLine - Add location information to specified debug information
1587 /// entry.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001588 void AddSourceLine(DIE *Die, DIVariable *V) {
1589 unsigned FileID = 0;
1590 unsigned Line = V->getLineNumber();
1591 if (V->getVersion() < DIDescriptor::Version7) {
1592 // Version6 or earlier. Use compile unit info to get file id.
1593 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1594 FileID = Unit->getID();
1595 } else {
1596 // Version7 or newer, use filename and directory info from DIVariable
1597 // directly.
1598 unsigned DID = Directories.idFor(V->getDirectory());
1599 FileID = SrcFiles.idFor(SrcFileInfo(DID, V->getFilename()));
1600 }
1601 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1602 AddUInt(Die, DW_AT_decl_line, 0, Line);
1603 }
1604
1605 /// AddSourceLine - Add location information to specified debug information
1606 /// entry.
Devang Patel8526cc02009-01-05 22:35:52 +00001607 void AddSourceLine(DIE *Die, DIGlobal *G) {
1608 unsigned FileID = 0;
1609 unsigned Line = G->getLineNumber();
1610 if (G->getVersion() < DIDescriptor::Version7) {
1611 // Version6 or earlier. Use compile unit info to get file id.
1612 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1613 FileID = Unit->getID();
1614 } else {
1615 // Version7 or newer, use filename and directory info from DIGlobal
1616 // directly.
1617 unsigned DID = Directories.idFor(G->getDirectory());
1618 FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1619 }
1620 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1621 AddUInt(Die, DW_AT_decl_line, 0, Line);
1622 }
1623
1624 void AddSourceLine(DIE *Die, DIType *G) {
1625 unsigned FileID = 0;
1626 unsigned Line = G->getLineNumber();
1627 if (G->getVersion() < DIDescriptor::Version7) {
1628 // Version6 or earlier. Use compile unit info to get file id.
1629 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1630 FileID = Unit->getID();
1631 } else {
1632 // Version7 or newer, use filename and directory info from DIGlobal
1633 // directly.
1634 unsigned DID = Directories.idFor(G->getDirectory());
1635 FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1636 }
1637 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1638 AddUInt(Die, DW_AT_decl_line, 0, Line);
1639 }
1640
Jim Laskey65195462006-10-30 13:35:07 +00001641 /// AddAddress - Add an address attribute to a die based on the location
1642 /// provided.
1643 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001644 const MachineLocation &Location) {
Dan Gohmand735b802008-10-03 15:45:36 +00001645 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001646 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001647
Dan Gohmand735b802008-10-03 15:45:36 +00001648 if (Location.isReg()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001649 if (Reg < 32) {
1650 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1651 } else {
1652 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1653 AddUInt(Block, 0, DW_FORM_udata, Reg);
1654 }
1655 } else {
1656 if (Reg < 32) {
1657 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1658 } else {
1659 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1660 AddUInt(Block, 0, DW_FORM_udata, Reg);
1661 }
1662 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1663 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001664
Jim Laskeyef42a012006-11-02 20:12:39 +00001665 AddBlock(Die, Attribute, 0, Block);
1666 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001667
Jim Laskeyef42a012006-11-02 20:12:39 +00001668 /// AddBasicType - Add a new basic type attribute to the specified entity.
1669 ///
1670 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1671 const std::string &Name,
1672 unsigned Encoding, unsigned Size) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001673
Jim Laskeyef42a012006-11-02 20:12:39 +00001674 DIE Buffer(DW_TAG_base_type);
1675 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1676 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1677 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel9ede3f22009-01-05 17:44:11 +00001678 DIE *BasicTypeDie = Unit->AddDie(Buffer);
1679 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, BasicTypeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001680 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001681
Jim Laskeyef42a012006-11-02 20:12:39 +00001682 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1683 ///
1684 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001685 DIE Buffer(DW_TAG_pointer_type);
Dan Gohman82482942007-09-27 23:12:31 +00001686 AddUInt(&Buffer, DW_AT_byte_size, 0, TD->getPointerSize());
Jim Laskeyef42a012006-11-02 20:12:39 +00001687 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelca03c272009-01-05 17:45:59 +00001688 DIE *PointerTypeDie = Unit->AddDie(Buffer);
1689 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, PointerTypeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001690 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001691
Jim Laskeyef42a012006-11-02 20:12:39 +00001692 /// AddType - Add a new type attribute to the specified entity.
1693 ///
1694 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1695 if (!TyDesc) {
Jim Laskey2b935d52007-01-26 14:19:17 +00001696 AddBasicType(Entity, Unit, "", DW_ATE_signed, sizeof(int32_t));
Jim Laskeyef42a012006-11-02 20:12:39 +00001697 } else {
1698 // Check for pre-existence.
1699 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001700
Jim Laskeyef42a012006-11-02 20:12:39 +00001701 // If it exists then use the existing value.
1702 if (Slot) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001703 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1704 return;
1705 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001706
Jim Laskeyef42a012006-11-02 20:12:39 +00001707 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1708 // FIXME - Not sure why programs and variables are coming through here.
1709 // Short cut for handling subprogram types (not really a TyDesc.)
1710 AddPointerType(Entity, Unit, SubprogramTy->getName());
1711 } else if (GlobalVariableDesc *GlobalTy =
1712 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1713 // FIXME - Not sure why programs and variables are coming through here.
1714 // Short cut for handling global variable types (not really a TyDesc.)
1715 AddPointerType(Entity, Unit, GlobalTy->getName());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001716 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00001717 // Set up proxy.
1718 Slot = NewDIEntry();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001719
Jim Laskeyef42a012006-11-02 20:12:39 +00001720 // Construct type.
1721 DIE Buffer(DW_TAG_base_type);
1722 ConstructType(Buffer, TyDesc, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001723
Jim Laskeyef42a012006-11-02 20:12:39 +00001724 // Add debug information entry to entity and unit.
1725 DIE *Die = Unit->AddDie(Buffer);
1726 SetDIEntry(Slot, Die);
1727 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1728 }
1729 }
1730 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001731
Devang Patel48d190f2009-01-05 21:47:57 +00001732 /// AddType - Add a new type attribute to the specified entity.
1733 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
1734 if (Ty.isNull()) {
1735 AddBasicType(Entity, DW_Unit, "", DW_ATE_signed, sizeof(int32_t));
1736 return;
1737 }
1738
1739 // Check for pre-existence.
1740 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1741 // If it exists then use the existing value.
1742 if (Slot) {
1743 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1744 return;
1745 }
1746
1747 // Set up proxy.
1748 Slot = NewDIEntry();
1749
1750 // Construct type.
1751 DIE Buffer(DW_TAG_base_type);
1752 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1753 ConstructTypeDIE(DW_Unit, Buffer, BT);
1754 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1755 ConstructTypeDIE(DW_Unit, Buffer, DT);
1756 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1757 ConstructTypeDIE(DW_Unit, Buffer, CT);
1758
1759 // Add debug information entry to entity and unit.
1760 DIE *Die = DW_Unit->AddDie(Buffer);
1761 SetDIEntry(Slot, Die);
1762 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1763 }
1764
Devang Patele5202732009-01-05 19:07:53 +00001765 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1766 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1767 DIBasicType *BTy) {
Devang Patel08f053f2009-01-05 17:57:47 +00001768
1769 // Get core information.
1770 const std::string &Name = BTy->getName();
1771 Buffer.setTag(DW_TAG_base_type);
1772 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy->getEncoding());
1773 // Add name if not anonymous or intermediate type.
1774 if (!Name.empty())
1775 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1776 uint64_t Size = BTy->getSizeInBits() >> 3;
1777 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1778 }
1779
Devang Patele5202732009-01-05 19:07:53 +00001780 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1781 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1782 DIDerivedType *DTy) {
Devang Patel08f053f2009-01-05 17:57:47 +00001783
1784 // Get core information.
1785 const std::string &Name = DTy->getName();
1786 uint64_t Size = DTy->getSizeInBits() >> 3;
1787 unsigned Tag = DTy->getTag();
1788 // FIXME - Workaround for templates.
1789 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1790
1791 Buffer.setTag(Tag);
1792 // Map to main type, void will not have a type.
1793 DIType FromTy = DTy->getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00001794 AddType(DW_Unit, &Buffer, FromTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001795
1796 // Add name if not anonymous or intermediate type.
1797 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1798
1799 // Add size if non-zero (derived types might be zero-sized.)
1800 if (Size)
1801 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1802
1803 // Add source line info if available and TyDesc is not a forward
1804 // declaration.
1805 // FIXME - Enable this. if (!DTy->isForwardDecl())
1806 // FIXME - Enable this. AddSourceLine(&Buffer, *DTy);
1807 }
1808
Devang Patelf4215332009-01-05 19:55:51 +00001809 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1810 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1811 DICompositeType *CTy) {
1812
1813 // Get core information.
1814 const std::string &Name = CTy->getName();
1815 uint64_t Size = CTy->getSizeInBits() >> 3;
1816 unsigned Tag = CTy->getTag();
1817 switch (Tag) {
1818 case DW_TAG_vector_type:
1819 case DW_TAG_array_type:
1820 ConstructArrayTypeDIE(DW_Unit, Buffer, CTy);
1821 break;
1822 //FIXME - Enable this.
1823 // case DW_TAG_enumeration_type:
1824 // DIArray Elements = CTy->getTypeArray();
1825 // // Add enumerators to enumeration type.
1826 // for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i)
1827 // ConstructEnumTypeDIE(Buffer, &Elements.getElement(i));
1828 // break;
1829 case DW_TAG_subroutine_type:
1830 {
1831 // Add prototype flag.
1832 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1833 DIArray Elements = CTy->getTypeArray();
1834 // Add return type.
Devang Patel48d190f2009-01-05 21:47:57 +00001835 DIDescriptor RTy = Elements.getElement(0);
1836 if (DIBasicType *BT = dyn_cast<DIBasicType>(&RTy))
1837 AddType(DW_Unit, &Buffer, *BT);
1838 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&RTy))
1839 AddType(DW_Unit, &Buffer, *DT);
1840 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&RTy))
1841 AddType(DW_Unit, &Buffer, *CT);
1842
1843 //AddType(DW_Unit, &Buffer, Elements.getElement(0));
Devang Patelf4215332009-01-05 19:55:51 +00001844 // Add arguments.
1845 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1846 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00001847 DIDescriptor Ty = Elements.getElement(i);
1848 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1849 AddType(DW_Unit, &Buffer, *BT);
1850 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1851 AddType(DW_Unit, &Buffer, *DT);
1852 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1853 AddType(DW_Unit, &Buffer, *CT);
Devang Patelf4215332009-01-05 19:55:51 +00001854 Buffer.AddChild(Arg);
1855 }
1856 }
1857 break;
1858 case DW_TAG_structure_type:
1859 case DW_TAG_union_type:
1860 {
1861 // Add elements to structure type.
1862 DIArray Elements = CTy->getTypeArray();
1863 // Add elements to structure type.
1864 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1865 DIDescriptor Element = Elements.getElement(i);
1866 if (DISubprogram *SP = dyn_cast<DISubprogram>(&Element))
1867 ConstructFieldTypeDIE(DW_Unit, Buffer, SP);
1868 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Element))
1869 ConstructFieldTypeDIE(DW_Unit, Buffer, DT);
1870 else if (DIGlobalVariable *GV = dyn_cast<DIGlobalVariable>(&Element))
1871 ConstructFieldTypeDIE(DW_Unit, Buffer, GV);
1872 }
1873 }
1874 break;
1875 default:
1876 break;
1877 }
1878
1879 // Add name if not anonymous or intermediate type.
1880 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1881
1882 // Add size if non-zero (derived types might be zero-sized.)
1883 if (Size)
1884 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1885 else {
1886 // Add zero size even if it is not a forward declaration.
1887 // FIXME - Enable this.
1888 // if (!CTy->isDefinition())
1889 // AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1890 // else
1891 // AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1892 }
1893
1894 // Add source line info if available and TyDesc is not a forward
1895 // declaration.
1896 // FIXME - Enable this.
1897 // if (CTy->isForwardDecl())
1898 // AddSourceLine(&Buffer, *CTy);
1899 }
1900
Devang Patel68afdc32009-01-05 18:33:01 +00001901 // ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1902 void ConstructSubrangeDIE (DIE &Buffer, DISubrange *SR, DIE *IndexTy) {
1903 int64_t L = SR->getLo();
1904 int64_t H = SR->getHi();
1905 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1906 if (L != H) {
1907 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1908 if (L)
1909 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1910 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
1911 }
1912 Buffer.AddChild(DW_Subrange);
1913 }
1914
1915 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1916 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1917 DICompositeType *CTy) {
1918 Buffer.setTag(DW_TAG_array_type);
1919 if (CTy->getTag() == DW_TAG_vector_type)
1920 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1921
1922 DIArray Elements = CTy->getTypeArray();
1923 // FIXME - Enable this.
Devang Patel48d190f2009-01-05 21:47:57 +00001924 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel68afdc32009-01-05 18:33:01 +00001925
1926 // Construct an anonymous type for index type.
1927 DIE IdxBuffer(DW_TAG_base_type);
1928 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1929 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1930 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1931
1932 // Add subranges to array type.
1933 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patelf4215332009-01-05 19:55:51 +00001934 DIDescriptor Element = Elements.getElement(i);
1935 if (DISubrange *SR = dyn_cast<DISubrange>(&Element))
1936 ConstructSubrangeDIE(Buffer, SR, IndexTy);
Devang Patel68afdc32009-01-05 18:33:01 +00001937 }
1938 }
1939
Devang Patelc69bf2c2009-01-05 18:38:38 +00001940 /// ConstructEnumTypeDIE - Construct enum type DIE from
1941 /// DIEnumerator.
Devang Patelf4215332009-01-05 19:55:51 +00001942 void ConstructEnumTypeDIE(CompileUnit *DW_Unit,
1943 DIE &Buffer, DIEnumerator *ETy) {
Devang Patelc69bf2c2009-01-05 18:38:38 +00001944
1945 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1946 AddString(Enumerator, DW_AT_name, DW_FORM_string, ETy->getName());
1947 int64_t Value = ETy->getEnumValue();
1948 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1949 Buffer.AddChild(Enumerator);
1950 }
Devang Patel68afdc32009-01-05 18:33:01 +00001951
Devang Patel86ae1422009-01-05 18:59:44 +00001952 /// ConstructFieldTypeDIE - Construct variable DIE for a struct field.
1953 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
1954 DIE &Buffer, DIGlobalVariable *V) {
1955
1956 DIE *VariableDie = new DIE(DW_TAG_variable);
1957 const std::string &LinkageName = V->getLinkageName();
1958 if (!LinkageName.empty())
1959 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1960 LinkageName);
1961 // FIXME - Enable this. AddSourceLine(VariableDie, V);
Devang Patel48d190f2009-01-05 21:47:57 +00001962 AddType(DW_Unit, VariableDie, V->getType());
Devang Patel86ae1422009-01-05 18:59:44 +00001963 if (!V->isLocalToUnit())
1964 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
1965 AddUInt(VariableDie, DW_AT_declaration, DW_FORM_flag, 1);
1966 Buffer.AddChild(VariableDie);
1967 }
1968
1969 /// ConstructFieldTypeDIE - Construct subprogram DIE for a struct field.
1970 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
1971 DIE &Buffer, DISubprogram *SP,
1972 bool IsConstructor = false) {
1973 DIE *Method = new DIE(DW_TAG_subprogram);
1974 AddString(Method, DW_AT_name, DW_FORM_string, SP->getName());
1975 const std::string &LinkageName = SP->getLinkageName();
1976 if (!LinkageName.empty())
1977 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1978 // FIXME - Enable this. AddSourceLine(Method, SP);
1979
1980 DICompositeType MTy = SP->getType();
1981 DIArray Args = MTy.getTypeArray();
1982
1983 // Add Return Type.
Devang Patel48d190f2009-01-05 21:47:57 +00001984 if (!IsConstructor) {
1985 DIDescriptor Ty = Args.getElement(0);
1986 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1987 AddType(DW_Unit, Method, *BT);
1988 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1989 AddType(DW_Unit, Method, *DT);
1990 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1991 AddType(DW_Unit, Method, *CT);
1992 }
Devang Patel86ae1422009-01-05 18:59:44 +00001993
1994 // Add arguments.
1995 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1996 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00001997 DIDescriptor Ty = Args.getElement(i);
1998 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1999 AddType(DW_Unit, Method, *BT);
2000 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
2001 AddType(DW_Unit, Method, *DT);
2002 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
2003 AddType(DW_Unit, Method, *CT);
Devang Patel86ae1422009-01-05 18:59:44 +00002004 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
2005 Method->AddChild(Arg);
2006 }
2007
2008 if (!SP->isLocalToUnit())
2009 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
2010 Buffer.AddChild(Method);
2011 }
2012
2013 /// COnstructFieldTypeDIE - Construct derived type DIE for a struct field.
2014 void ConstructFieldTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
2015 DIDerivedType *DTy) {
2016 unsigned Tag = DTy->getTag();
2017 DIE *MemberDie = new DIE(Tag);
2018 if (!DTy->getName().empty())
2019 AddString(MemberDie, DW_AT_name, DW_FORM_string, DTy->getName());
2020 // FIXME - Enable this. AddSourceLine(MemberDie, DTy);
2021
2022 DIType FromTy = DTy->getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00002023 AddType(DW_Unit, MemberDie, FromTy);
Devang Patel86ae1422009-01-05 18:59:44 +00002024
2025 uint64_t Size = DTy->getSizeInBits();
2026 uint64_t Offset = DTy->getOffsetInBits();
2027
2028 // FIXME Handle bitfields
2029
2030 // Add size.
2031 AddUInt(MemberDie, DW_AT_bit_size, 0, Size);
2032 // Add computation for offset.
2033 DIEBlock *Block = new DIEBlock();
2034 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
2035 AddUInt(Block, 0, DW_FORM_udata, Offset >> 3);
2036 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
2037
2038 // FIXME Handle DW_AT_accessibility.
2039
2040 Buffer.AddChild(MemberDie);
2041 }
2042
Jim Laskeyef42a012006-11-02 20:12:39 +00002043 /// ConstructType - Adds all the required attributes to the type.
2044 ///
2045 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
2046 // Get core information.
2047 const std::string &Name = TyDesc->getName();
2048 uint64_t Size = TyDesc->getSize() >> 3;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002049
Jim Laskeyef42a012006-11-02 20:12:39 +00002050 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
2051 // Fundamental types like int, float, bool
2052 Buffer.setTag(DW_TAG_base_type);
2053 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
2054 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey85f419b2006-11-09 16:32:26 +00002055 // Fetch tag.
2056 unsigned Tag = DerivedTy->getTag();
2057 // FIXME - Workaround for templates.
2058 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002059 // Pointers, typedefs et al.
Jim Laskey85f419b2006-11-09 16:32:26 +00002060 Buffer.setTag(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +00002061 // Map to main type, void will not have a type.
2062 if (TypeDesc *FromTy = DerivedTy->getFromType())
2063 AddType(&Buffer, FromTy, Unit);
2064 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
2065 // Fetch tag.
2066 unsigned Tag = CompTy->getTag();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002067
Jim Laskeyef42a012006-11-02 20:12:39 +00002068 // Set tag accordingly.
2069 if (Tag == DW_TAG_vector_type)
2070 Buffer.setTag(DW_TAG_array_type);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002071 else
Jim Laskeyef42a012006-11-02 20:12:39 +00002072 Buffer.setTag(Tag);
Jim Laskey65195462006-10-30 13:35:07 +00002073
Jim Laskeyef42a012006-11-02 20:12:39 +00002074 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002075
Jim Laskeyef42a012006-11-02 20:12:39 +00002076 switch (Tag) {
2077 case DW_TAG_vector_type:
2078 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
2079 // Fall thru
2080 case DW_TAG_array_type: {
2081 // Add element type.
2082 if (TypeDesc *FromTy = CompTy->getFromType())
2083 AddType(&Buffer, FromTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002084
Jim Laskeyef42a012006-11-02 20:12:39 +00002085 // Don't emit size attribute.
2086 Size = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002087
Jim Laskeyef42a012006-11-02 20:12:39 +00002088 // Construct an anonymous type for index type.
Devang Patel9ede3f22009-01-05 17:44:11 +00002089 DIE Buffer(DW_TAG_base_type);
2090 AddUInt(&Buffer, DW_AT_byte_size, 0, sizeof(int32_t));
2091 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
2092 DIE *IndexTy = Unit->AddDie(Buffer);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002093
Jim Laskeyef42a012006-11-02 20:12:39 +00002094 // Add subranges to array type.
Evan Chengf8480282008-12-09 17:56:30 +00002095 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002096 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
2097 int64_t Lo = SRD->getLo();
2098 int64_t Hi = SRD->getHi();
2099 DIE *Subrange = new DIE(DW_TAG_subrange_type);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002100
Jim Laskeyef42a012006-11-02 20:12:39 +00002101 // If a range is available.
2102 if (Lo != Hi) {
2103 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
2104 // Only add low if non-zero.
2105 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
2106 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
2107 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002108
Jim Laskeyef42a012006-11-02 20:12:39 +00002109 Buffer.AddChild(Subrange);
2110 }
2111 break;
2112 }
2113 case DW_TAG_structure_type:
2114 case DW_TAG_union_type: {
2115 // Add elements to structure type.
Evan Chengf8480282008-12-09 17:56:30 +00002116 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002117 DebugInfoDesc *Element = Elements[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002118
Jim Laskeyef42a012006-11-02 20:12:39 +00002119 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
2120 // Add field or base class.
Jim Laskeyef42a012006-11-02 20:12:39 +00002121 unsigned Tag = MemberDesc->getTag();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002122
Jim Laskeyef42a012006-11-02 20:12:39 +00002123 // Extract the basic information.
2124 const std::string &Name = MemberDesc->getName();
Jim Laskeyef42a012006-11-02 20:12:39 +00002125 uint64_t Size = MemberDesc->getSize();
2126 uint64_t Align = MemberDesc->getAlign();
2127 uint64_t Offset = MemberDesc->getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002128
Jim Laskeyef42a012006-11-02 20:12:39 +00002129 // Construct member debug information entry.
2130 DIE *Member = new DIE(Tag);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002131
Jim Laskeyef42a012006-11-02 20:12:39 +00002132 // Add name if not "".
2133 if (!Name.empty())
2134 AddString(Member, DW_AT_name, DW_FORM_string, Name);
Evan Chengf8480282008-12-09 17:56:30 +00002135
Jim Laskeyef42a012006-11-02 20:12:39 +00002136 // Add location if available.
2137 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002138
Jim Laskeyef42a012006-11-02 20:12:39 +00002139 // Most of the time the field info is the same as the members.
2140 uint64_t FieldSize = Size;
2141 uint64_t FieldAlign = Align;
2142 uint64_t FieldOffset = Offset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002143
Jim Laskeyee5f9272006-12-22 20:03:42 +00002144 // Set the member type.
2145 TypeDesc *FromTy = MemberDesc->getFromType();
2146 AddType(Member, FromTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002147
Jim Laskeyee5f9272006-12-22 20:03:42 +00002148 // Walk up typedefs until a real size is found.
2149 while (FromTy) {
2150 if (FromTy->getTag() != DW_TAG_typedef) {
2151 FieldSize = FromTy->getSize();
Devang Pateld0935c32008-12-23 21:55:38 +00002152 FieldAlign = FromTy->getAlign();
Jim Laskeyee5f9272006-12-22 20:03:42 +00002153 break;
2154 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002155
Dan Gohman275769a2007-07-23 20:24:29 +00002156 FromTy = cast<DerivedTypeDesc>(FromTy)->getFromType();
Jim Laskeyef42a012006-11-02 20:12:39 +00002157 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002158
Jim Laskeyef42a012006-11-02 20:12:39 +00002159 // Unless we have a bit field.
2160 if (Tag == DW_TAG_member && FieldSize != Size) {
2161 // Construct the alignment mask.
2162 uint64_t AlignMask = ~(FieldAlign - 1);
2163 // Determine the high bit + 1 of the declared size.
2164 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
2165 // Work backwards to determine the base offset of the field.
2166 FieldOffset = HiMark - FieldSize;
2167 // Now normalize offset to the field.
2168 Offset -= FieldOffset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002169
Jim Laskeyef42a012006-11-02 20:12:39 +00002170 // Maybe we need to work from the other end.
2171 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002172
Jim Laskeyef42a012006-11-02 20:12:39 +00002173 // Add size and offset.
2174 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
2175 AddUInt(Member, DW_AT_bit_size, 0, Size);
2176 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
2177 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002178
Jim Laskeyef42a012006-11-02 20:12:39 +00002179 // Add computation for offset.
2180 DIEBlock *Block = new DIEBlock();
2181 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
2182 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
2183 AddBlock(Member, DW_AT_data_member_location, 0, Block);
2184
2185 // Add accessibility (public default unless is base class.
2186 if (MemberDesc->isProtected()) {
2187 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
2188 } else if (MemberDesc->isPrivate()) {
2189 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
2190 } else if (Tag == DW_TAG_inheritance) {
2191 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
2192 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002193
Jim Laskeyef42a012006-11-02 20:12:39 +00002194 Buffer.AddChild(Member);
2195 } else if (GlobalVariableDesc *StaticDesc =
2196 dyn_cast<GlobalVariableDesc>(Element)) {
2197 // Add static member.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002198
Jim Laskeyef42a012006-11-02 20:12:39 +00002199 // Construct member debug information entry.
2200 DIE *Static = new DIE(DW_TAG_variable);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002201
Jim Laskeyef42a012006-11-02 20:12:39 +00002202 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00002203 const std::string &Name = StaticDesc->getName();
2204 const std::string &LinkageName = StaticDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00002205 AddString(Static, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00002206 if (!LinkageName.empty()) {
2207 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
2208 LinkageName);
2209 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002210
Jim Laskeyef42a012006-11-02 20:12:39 +00002211 // Add location.
2212 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002213
Jim Laskeyef42a012006-11-02 20:12:39 +00002214 // Add type.
2215 if (TypeDesc *StaticTy = StaticDesc->getType())
2216 AddType(Static, StaticTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002217
Jim Laskeyef42a012006-11-02 20:12:39 +00002218 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00002219 if (!StaticDesc->isStatic())
2220 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002221 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002222
Jim Laskeyef42a012006-11-02 20:12:39 +00002223 Buffer.AddChild(Static);
2224 } else if (SubprogramDesc *MethodDesc =
2225 dyn_cast<SubprogramDesc>(Element)) {
2226 // Add member function.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002227
Jim Laskeyef42a012006-11-02 20:12:39 +00002228 // Construct member debug information entry.
2229 DIE *Method = new DIE(DW_TAG_subprogram);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002230
Jim Laskeyef42a012006-11-02 20:12:39 +00002231 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00002232 const std::string &Name = MethodDesc->getName();
2233 const std::string &LinkageName = MethodDesc->getLinkageName();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002234
2235 AddString(Method, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00002236 bool IsCTor = TyDesc->getName() == Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002237
Jim Laskey2172f962006-11-30 14:35:45 +00002238 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002239 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00002240 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00002241 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002242
Jim Laskeyef42a012006-11-02 20:12:39 +00002243 // Add location.
2244 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002245
Jim Laskeyef42a012006-11-02 20:12:39 +00002246 // Add type.
2247 if (CompositeTypeDesc *MethodTy =
2248 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
2249 // Get argument information.
2250 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002251
Jim Laskeyef42a012006-11-02 20:12:39 +00002252 // If not a ctor.
2253 if (!IsCTor) {
2254 // Add return type.
2255 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
2256 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002257
Jim Laskeyef42a012006-11-02 20:12:39 +00002258 // Add arguments.
Evan Chengf8480282008-12-09 17:56:30 +00002259 for (unsigned i = 1, N = Args.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002260 DIE *Arg = new DIE(DW_TAG_formal_parameter);
2261 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
2262 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
2263 Method->AddChild(Arg);
2264 }
2265 }
2266
2267 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00002268 if (!MethodDesc->isStatic())
2269 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002270 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002271
Jim Laskeyef42a012006-11-02 20:12:39 +00002272 Buffer.AddChild(Method);
2273 }
2274 }
2275 break;
2276 }
2277 case DW_TAG_enumeration_type: {
2278 // Add enumerators to enumeration type.
Evan Chengf8480282008-12-09 17:56:30 +00002279 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002280 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
2281 const std::string &Name = ED->getName();
2282 int64_t Value = ED->getValue();
2283 DIE *Enumerator = new DIE(DW_TAG_enumerator);
2284 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
2285 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
2286 Buffer.AddChild(Enumerator);
2287 }
2288
2289 break;
2290 }
2291 case DW_TAG_subroutine_type: {
2292 // Add prototype flag.
2293 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
2294 // Add return type.
2295 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002296
Jim Laskeyef42a012006-11-02 20:12:39 +00002297 // Add arguments.
Evan Chengf8480282008-12-09 17:56:30 +00002298 for (unsigned i = 1, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002299 DIE *Arg = new DIE(DW_TAG_formal_parameter);
2300 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
2301 Buffer.AddChild(Arg);
2302 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002303
Jim Laskeyef42a012006-11-02 20:12:39 +00002304 break;
2305 }
2306 default: break;
2307 }
2308 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002309
Jim Laskeyef42a012006-11-02 20:12:39 +00002310 // Add name if not anonymous or intermediate type.
2311 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Evan Chengf8480282008-12-09 17:56:30 +00002312
Evan Cheng94ea5be2008-12-10 00:15:44 +00002313 // Add size if non-zero (derived types might be zero-sized.)
2314 if (Size)
2315 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
2316 else if (isa<CompositeTypeDesc>(TyDesc)) {
2317 // If TyDesc is a composite type, then add size even if it's zero unless
2318 // it's a forward declaration.
2319 if (TyDesc->isForwardDecl())
2320 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
2321 else
2322 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
2323 }
2324
2325 // Add source line info if available and TyDesc is not a forward
2326 // declaration.
2327 if (!TyDesc->isForwardDecl())
2328 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
Jim Laskeyef42a012006-11-02 20:12:39 +00002329 }
2330
2331 /// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey65195462006-10-30 13:35:07 +00002332 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002333 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
2334 // Construct debug information entry.
2335 DIE *Die = new DIE(DW_TAG_compile_unit);
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00002336 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2337 DWLabel("section_line", 0), DWLabel("section_line", 0), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002338 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
2339 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
2340 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
Devang Patel83580992008-12-12 21:57:54 +00002341 if (!UnitDesc->getDirectory().empty())
2342 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002343
Jim Laskeyef42a012006-11-02 20:12:39 +00002344 // Construct compile unit.
2345 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002346
Jim Laskeyef42a012006-11-02 20:12:39 +00002347 // Add Unit to compile unit map.
2348 DescToUnitMap[UnitDesc] = Unit;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002349
Jim Laskeyef42a012006-11-02 20:12:39 +00002350 return Unit;
2351 }
2352
Jim Laskey9d4209f2006-11-07 19:33:46 +00002353 /// GetBaseCompileUnit - Get the main compile unit.
2354 ///
2355 CompileUnit *GetBaseCompileUnit() const {
2356 CompileUnit *Unit = CompileUnits[0];
2357 assert(Unit && "Missing compile unit.");
2358 return Unit;
2359 }
2360
Jim Laskey65195462006-10-30 13:35:07 +00002361 /// FindCompileUnit - Get the compile unit for the given descriptor.
2362 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002363 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002364 CompileUnit *Unit = DescToUnitMap[UnitDesc];
Jim Laskeyef42a012006-11-02 20:12:39 +00002365 assert(Unit && "Missing compile unit.");
2366 return Unit;
2367 }
2368
Devang Patel8526cc02009-01-05 22:35:52 +00002369 /// FindCompileUnit - Get the compile unit for the given descriptor.
2370 ///
2371 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
2372 CompileUnit *DW_Unit = DW_CUs[Unit.getGV()];
2373 assert(DW_Unit && "Missing compile unit.");
2374 return DW_Unit;
2375 }
2376
Jim Laskeyef42a012006-11-02 20:12:39 +00002377 /// NewGlobalVariable - Add a new global variable DIE.
Jim Laskey65195462006-10-30 13:35:07 +00002378 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002379 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
2380 // Get the compile unit context.
2381 CompileUnitDesc *UnitDesc =
2382 static_cast<CompileUnitDesc *>(GVD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00002383 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00002384
2385 // Check for pre-existence.
2386 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
2387 if (Slot) return Slot;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002388
Jim Laskeyef42a012006-11-02 20:12:39 +00002389 // Get the global variable itself.
2390 GlobalVariable *GV = GVD->getGlobalVariable();
2391
Jim Laskey2172f962006-11-30 14:35:45 +00002392 const std::string &Name = GVD->getName();
2393 const std::string &FullName = GVD->getFullName();
2394 const std::string &LinkageName = GVD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00002395 // Create the global's variable DIE.
2396 DIE *VariableDie = new DIE(DW_TAG_variable);
2397 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00002398 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002399 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00002400 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00002401 }
Jim Laskey6488a072007-01-08 22:15:18 +00002402 AddType(VariableDie, GVD->getType(), Unit);
2403 if (!GVD->isStatic())
2404 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002405
Jim Laskeyef42a012006-11-02 20:12:39 +00002406 // Add source line info if available.
2407 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002408
Jim Laskeyef42a012006-11-02 20:12:39 +00002409 // Add address.
2410 DIEBlock *Block = new DIEBlock();
2411 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Jim Laskey2172f962006-11-30 14:35:45 +00002412 AddObjectLabel(Block, 0, DW_FORM_udata, Asm->getGlobalLinkName(GV));
2413 AddBlock(VariableDie, DW_AT_location, 0, Block);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002414
Jim Laskeyef42a012006-11-02 20:12:39 +00002415 // Add to map.
2416 Slot = VariableDie;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002417
Jim Laskeyef42a012006-11-02 20:12:39 +00002418 // Add to context owner.
2419 Unit->getDie()->AddChild(VariableDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002420
Jim Laskeyef42a012006-11-02 20:12:39 +00002421 // Expose as global.
2422 // FIXME - need to check external flag.
Jim Laskey2172f962006-11-30 14:35:45 +00002423 Unit->AddGlobal(FullName, VariableDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002424
Jim Laskeyef42a012006-11-02 20:12:39 +00002425 return VariableDie;
2426 }
Jim Laskey65195462006-10-30 13:35:07 +00002427
2428 /// NewSubprogram - Add a new subprogram DIE.
2429 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002430 DIE *NewSubprogram(SubprogramDesc *SPD) {
2431 // Get the compile unit context.
2432 CompileUnitDesc *UnitDesc =
2433 static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00002434 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00002435
2436 // Check for pre-existence.
2437 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
2438 if (Slot) return Slot;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002439
Jim Laskeyef42a012006-11-02 20:12:39 +00002440 // Gather the details (simplify add attribute code.)
Jim Laskey2172f962006-11-30 14:35:45 +00002441 const std::string &Name = SPD->getName();
2442 const std::string &FullName = SPD->getFullName();
2443 const std::string &LinkageName = SPD->getLinkageName();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002444
Jim Laskeyef42a012006-11-02 20:12:39 +00002445 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
2446 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00002447 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002448 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00002449 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00002450 }
2451 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
Jim Laskey6488a072007-01-08 22:15:18 +00002452 if (!SPD->isStatic())
2453 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002454 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002455
Jim Laskeyef42a012006-11-02 20:12:39 +00002456 // Add source line info if available.
2457 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
2458
2459 // Add to map.
2460 Slot = SubprogramDie;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002461
Jim Laskeyef42a012006-11-02 20:12:39 +00002462 // Add to context owner.
2463 Unit->getDie()->AddChild(SubprogramDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002464
Jim Laskeyef42a012006-11-02 20:12:39 +00002465 // Expose as global.
Jim Laskey2172f962006-11-30 14:35:45 +00002466 Unit->AddGlobal(FullName, SubprogramDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002467
Jim Laskeyef42a012006-11-02 20:12:39 +00002468 return SubprogramDie;
2469 }
Jim Laskey65195462006-10-30 13:35:07 +00002470
2471 /// NewScopeVariable - Create a new scope variable.
2472 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002473 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
2474 // Get the descriptor.
2475 VariableDesc *VD = DV->getDesc();
2476
2477 // Translate tag to proper Dwarf tag. The result variable is dropped for
2478 // now.
2479 unsigned Tag;
2480 switch (VD->getTag()) {
2481 case DW_TAG_return_variable: return NULL;
2482 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2483 case DW_TAG_auto_variable: // fall thru
2484 default: Tag = DW_TAG_variable; break;
2485 }
2486
2487 // Define variable debug information entry.
2488 DIE *VariableDie = new DIE(Tag);
2489 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
2490
2491 // Add source line info if available.
2492 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002493
Jim Laskeyef42a012006-11-02 20:12:39 +00002494 // Add variable type.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002495 AddType(VariableDie, VD->getType(), Unit);
2496
Jim Laskeyef42a012006-11-02 20:12:39 +00002497 // Add variable address.
2498 MachineLocation Location;
Evan Cheng72bebb92008-01-31 03:37:28 +00002499 Location.set(RI->getFrameRegister(*MF),
2500 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
Jim Laskeyef42a012006-11-02 20:12:39 +00002501 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00002502
Jim Laskeyef42a012006-11-02 20:12:39 +00002503 return VariableDie;
2504 }
Jim Laskey65195462006-10-30 13:35:07 +00002505
Devang Patel7a6e5a32009-01-08 02:33:41 +00002506 /// NewScopeVariable - Create a new scope variable.
2507 ///
2508 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
2509 // Get the descriptor.
2510 DIVariable *VD = DV->getVariable();
2511
2512 // Translate tag to proper Dwarf tag. The result variable is dropped for
2513 // now.
2514 unsigned Tag;
2515 switch (VD->getTag()) {
2516 case DW_TAG_return_variable: return NULL;
2517 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2518 case DW_TAG_auto_variable: // fall thru
2519 default: Tag = DW_TAG_variable; break;
2520 }
2521
2522 // Define variable debug information entry.
2523 DIE *VariableDie = new DIE(Tag);
2524 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
2525
2526 // Add source line info if available.
2527 AddSourceLine(VariableDie, VD);
2528
2529 // Add variable type.
2530 AddType(Unit, VariableDie, VD->getType());
2531
2532 // Add variable address.
2533 MachineLocation Location;
2534 Location.set(RI->getFrameRegister(*MF),
2535 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
2536 AddAddress(VariableDie, DW_AT_location, Location);
2537
2538 return VariableDie;
2539 }
2540
Devang Patel9f8fcfc2009-01-08 17:19:22 +00002541 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
2542 CompileUnit *Unit = DW_CUs[V];
2543 assert (Unit && "Unable to find CompileUnit");
Devang Patele9bfb0f2009-01-12 18:41:00 +00002544 unsigned ID = MMI->NextLabelID();
Devang Patel9f8fcfc2009-01-08 17:19:22 +00002545 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
2546 return ID;
2547 }
2548
2549 unsigned getRecordSourceLineCount() {
2550 return Lines.size();
2551 }
2552
2553 unsigned RecordSource(const std::string &Directory,
2554 const std::string &File) {
2555 unsigned DID = Directories.insert(Directory);
2556 return SrcFiles.insert(SrcFileInfo(DID,File));
2557 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00002558
Devang Patel2ec78c42009-01-08 02:49:34 +00002559 /// RecordRegionStart - Indicate the start of a region.
2560 ///
2561 unsigned RecordRegionStart(GlobalVariable *V) {
2562 DbgScope *Scope = getOrCreateScope(V);
Devang Patele9bfb0f2009-01-12 18:41:00 +00002563 unsigned ID = MMI->NextLabelID();
Devang Patel2ec78c42009-01-08 02:49:34 +00002564 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
2565 return ID;
2566 }
2567
2568 /// RecordRegionEnd - Indicate the end of a region.
2569 ///
2570 unsigned RecordRegionEnd(GlobalVariable *V) {
2571 DbgScope *Scope = getOrCreateScope(V);
Devang Patele9bfb0f2009-01-12 18:41:00 +00002572 unsigned ID = MMI->NextLabelID();
Devang Patel2ec78c42009-01-08 02:49:34 +00002573 Scope->setEndLabelID(ID);
2574 return ID;
2575 }
2576
2577 /// RecordVariable - Indicate the declaration of a local variable.
2578 ///
2579 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
2580 DbgScope *Scope = getOrCreateScope(GV);
2581 DIVariable *VD = new DIVariable(GV);
2582 DbgVariable *DV = new DbgVariable(VD, FrameIndex);
2583 Scope->AddVariable(DV);
2584 }
2585
Devang Patel7a6e5a32009-01-08 02:33:41 +00002586 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2587 ///
2588 DbgScope *getOrCreateScope(GlobalVariable *V) {
2589 DbgScope *&Slot = DbgScopeMap[V];
2590 if (!Slot) {
2591 // FIXME - breaks down when the context is an inlined function.
2592 DIDescriptor ParentDesc;
Devang Patel0dc969e2009-01-10 02:34:18 +00002593 DIDescriptor *DB = new DIBlock(V);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002594 if (DIBlock *Block = dyn_cast<DIBlock>(DB)) {
2595 ParentDesc = Block->getContext();
2596 }
2597 DbgScope *Parent = ParentDesc.isNull() ?
Devang Patel0dc969e2009-01-10 02:34:18 +00002598 NULL : getOrCreateScope(ParentDesc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002599 Slot = new DbgScope(Parent, DB);
2600 if (Parent) {
2601 Parent->AddScope(Slot);
2602 } else if (RootDbgScope) {
2603 // FIXME - Add inlined function scopes to the root so we can delete
2604 // them later. Long term, handle inlined functions properly.
2605 RootDbgScope->AddScope(Slot);
2606 } else {
2607 // First function is top level function.
2608 RootDbgScope = Slot;
2609 }
2610 }
2611 return Slot;
2612 }
2613
2614 /// ConstructDbgScope - Construct the components of a scope.
2615 ///
2616 void ConstructDbgScope(DbgScope *ParentScope,
2617 unsigned ParentStartID, unsigned ParentEndID,
2618 DIE *ParentDie, CompileUnit *Unit) {
2619 // Add variables to scope.
Devang Patel9795da52009-01-10 02:42:49 +00002620 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002621 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2622 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2623 if (VariableDie) ParentDie->AddChild(VariableDie);
2624 }
2625
2626 // Add nested scopes.
Devang Patel9795da52009-01-10 02:42:49 +00002627 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002628 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2629 // Define the Scope debug information entry.
2630 DbgScope *Scope = Scopes[j];
2631 // FIXME - Ignore inlined functions for the time being.
2632 if (!Scope->getParent()) continue;
2633
Devang Patele9bfb0f2009-01-12 18:41:00 +00002634 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2635 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002636
2637 // Ignore empty scopes.
2638 if (StartID == EndID && StartID != 0) continue;
2639 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
2640
2641 if (StartID == ParentStartID && EndID == ParentEndID) {
2642 // Just add stuff to the parent scope.
2643 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2644 } else {
2645 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2646
2647 // Add the scope bounds.
2648 if (StartID) {
2649 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2650 DWLabel("label", StartID));
2651 } else {
2652 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2653 DWLabel("func_begin", SubprogramCount));
2654 }
2655 if (EndID) {
2656 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2657 DWLabel("label", EndID));
2658 } else {
2659 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2660 DWLabel("func_end", SubprogramCount));
2661 }
2662
2663 // Add the scope contents.
2664 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2665 ParentDie->AddChild(ScopeDie);
2666 }
2667 }
2668 }
2669
2670 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2671 ///
2672 void ConstructRootDbgScope(DbgScope *RootScope) {
2673 // Exit if there is no root scope.
2674 if (!RootScope) return;
2675
2676 // Get the subprogram debug information entry.
2677 DISubprogram *SPD = cast<DISubprogram>(RootScope->getDesc());
2678
2679 // Get the compile unit context.
2680 CompileUnit *Unit = FindCompileUnit(SPD->getCompileUnit());
2681
2682 // Get the subprogram die.
2683 DIE *SPDie = Unit->getDieMapSlotFor(SPD->getGV());
2684 assert(SPDie && "Missing subprogram descriptor");
2685
2686 // Add the function bounds.
2687 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2688 DWLabel("func_begin", SubprogramCount));
2689 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2690 DWLabel("func_end", SubprogramCount));
2691 MachineLocation Location(RI->getFrameRegister(*MF));
2692 AddAddress(SPDie, DW_AT_frame_base, Location);
2693
2694 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2695 }
2696
2697 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2698 ///
2699 void ConstructDefaultDbgScope(MachineFunction *MF) {
2700 // Find the correct subprogram descriptor.
2701 std::string SPName = "llvm.dbg.subprograms";
2702 std::vector<GlobalVariable*> Result;
2703 getGlobalVariablesUsing(*M, SPName, Result);
2704 for (std::vector<GlobalVariable *>::iterator I = Result.begin(),
2705 E = Result.end(); I != E; ++I) {
2706
2707 DISubprogram *SPD = new DISubprogram(*I);
2708
2709 if (SPD->getName() == MF->getFunction()->getName()) {
2710 // Get the compile unit context.
2711 CompileUnit *Unit = FindCompileUnit(SPD->getCompileUnit());
2712
2713 // Get the subprogram die.
2714 DIE *SPDie = Unit->getDieMapSlotFor(SPD->getGV());
2715 assert(SPDie && "Missing subprogram descriptor");
2716
2717 // Add the function bounds.
2718 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2719 DWLabel("func_begin", SubprogramCount));
2720 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2721 DWLabel("func_end", SubprogramCount));
2722
2723 MachineLocation Location(RI->getFrameRegister(*MF));
2724 AddAddress(SPDie, DW_AT_frame_base, Location);
2725 return;
2726 }
2727 }
2728#if 0
2729 // FIXME: This is causing an abort because C++ mangled names are compared
2730 // with their unmangled counterparts. See PR2885. Don't do this assert.
2731 assert(0 && "Couldn't find DIE for machine function!");
2732#endif
2733 }
2734
Jim Laskey65195462006-10-30 13:35:07 +00002735 /// ConstructScope - Construct the components of a scope.
2736 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002737 void ConstructScope(DebugScope *ParentScope,
Jim Laskey36729dd2006-11-29 16:55:57 +00002738 unsigned ParentStartID, unsigned ParentEndID,
2739 DIE *ParentDie, CompileUnit *Unit) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002740 // Add variables to scope.
2741 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
2742 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2743 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
2744 if (VariableDie) ParentDie->AddChild(VariableDie);
2745 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002746
Jim Laskeyef42a012006-11-02 20:12:39 +00002747 // Add nested scopes.
2748 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
2749 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2750 // Define the Scope debug information entry.
2751 DebugScope *Scope = Scopes[j];
2752 // FIXME - Ignore inlined functions for the time being.
2753 if (!Scope->getParent()) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002754
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002755 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2756 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Jim Laskey5496f012006-11-09 14:52:14 +00002757
Jim Laskey9d4209f2006-11-07 19:33:46 +00002758 // Ignore empty scopes.
2759 if (StartID == EndID && StartID != 0) continue;
2760 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002761
Jim Laskey36729dd2006-11-29 16:55:57 +00002762 if (StartID == ParentStartID && EndID == ParentEndID) {
2763 // Just add stuff to the parent scope.
2764 ConstructScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00002765 } else {
Jim Laskey36729dd2006-11-29 16:55:57 +00002766 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002767
Jim Laskey36729dd2006-11-29 16:55:57 +00002768 // Add the scope bounds.
2769 if (StartID) {
2770 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00002771 DWLabel("label", StartID));
Jim Laskey36729dd2006-11-29 16:55:57 +00002772 } else {
2773 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2774 DWLabel("func_begin", SubprogramCount));
2775 }
2776 if (EndID) {
2777 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00002778 DWLabel("label", EndID));
Jim Laskey36729dd2006-11-29 16:55:57 +00002779 } else {
2780 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2781 DWLabel("func_end", SubprogramCount));
2782 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002783
Jim Laskey36729dd2006-11-29 16:55:57 +00002784 // Add the scope contents.
2785 ConstructScope(Scope, StartID, EndID, ScopeDie, Unit);
2786 ParentDie->AddChild(ScopeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00002787 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002788 }
2789 }
Jim Laskey65195462006-10-30 13:35:07 +00002790
2791 /// ConstructRootScope - Construct the scope for the subprogram.
2792 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002793 void ConstructRootScope(DebugScope *RootScope) {
2794 // Exit if there is no root scope.
2795 if (!RootScope) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002796
2797 // Get the subprogram debug information entry.
Jim Laskeyef42a012006-11-02 20:12:39 +00002798 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002799
Jim Laskeyef42a012006-11-02 20:12:39 +00002800 // Get the compile unit context.
Jim Laskey5496f012006-11-09 14:52:14 +00002801 CompileUnit *Unit = GetBaseCompileUnit();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002802
Jim Laskeyef42a012006-11-02 20:12:39 +00002803 // Get the subprogram die.
2804 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2805 assert(SPDie && "Missing subprogram descriptor");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002806
Jim Laskeyef42a012006-11-02 20:12:39 +00002807 // Add the function bounds.
2808 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2809 DWLabel("func_begin", SubprogramCount));
2810 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2811 DWLabel("func_end", SubprogramCount));
2812 MachineLocation Location(RI->getFrameRegister(*MF));
2813 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00002814
Jim Laskey36729dd2006-11-29 16:55:57 +00002815 ConstructScope(RootScope, 0, 0, SPDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00002816 }
Jim Laskey65195462006-10-30 13:35:07 +00002817
Bill Wendlingd751c642008-09-26 00:28:12 +00002818 /// ConstructDefaultScope - Construct a default scope for the subprogram.
2819 ///
2820 void ConstructDefaultScope(MachineFunction *MF) {
2821 // Find the correct subprogram descriptor.
2822 std::vector<SubprogramDesc *> Subprograms;
2823 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
2824
2825 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2826 SubprogramDesc *SPD = Subprograms[i];
2827
2828 if (SPD->getName() == MF->getFunction()->getName()) {
2829 // Get the compile unit context.
2830 CompileUnit *Unit = GetBaseCompileUnit();
2831
2832 // Get the subprogram die.
2833 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2834 assert(SPDie && "Missing subprogram descriptor");
2835
2836 // Add the function bounds.
2837 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2838 DWLabel("func_begin", SubprogramCount));
2839 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2840 DWLabel("func_end", SubprogramCount));
2841
2842 MachineLocation Location(RI->getFrameRegister(*MF));
2843 AddAddress(SPDie, DW_AT_frame_base, Location);
2844 return;
2845 }
2846 }
Bill Wendlinga9f0cc42008-10-17 18:48:57 +00002847#if 0
2848 // FIXME: This is causing an abort because C++ mangled names are compared
2849 // with their unmangled counterparts. See PR2885. Don't do this assert.
Bill Wendlingd751c642008-09-26 00:28:12 +00002850 assert(0 && "Couldn't find DIE for machine function!");
Bill Wendlinga9f0cc42008-10-17 18:48:57 +00002851#endif
Bill Wendlingd751c642008-09-26 00:28:12 +00002852 }
2853
Jim Laskeyef42a012006-11-02 20:12:39 +00002854 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2855 /// tools to recognize the object file contains Dwarf information.
2856 void EmitInitial() {
2857 // Check to see if we already emitted intial headers.
2858 if (didInitial) return;
2859 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002860
Jim Laskeyef42a012006-11-02 20:12:39 +00002861 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002862 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002863 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002864 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002865 }
2866 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2867 EmitLabel("section_info", 0);
2868 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2869 EmitLabel("section_abbrev", 0);
2870 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2871 EmitLabel("section_aranges", 0);
2872 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2873 EmitLabel("section_macinfo", 0);
2874 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2875 EmitLabel("section_line", 0);
2876 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2877 EmitLabel("section_loc", 0);
2878 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2879 EmitLabel("section_pubnames", 0);
2880 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2881 EmitLabel("section_str", 0);
2882 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2883 EmitLabel("section_ranges", 0);
2884
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002885 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002886 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002887 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002888 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002889 }
2890
Jim Laskey65195462006-10-30 13:35:07 +00002891 /// EmitDIE - Recusively Emits a debug information entry.
2892 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002893 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002894 // Get the abbreviation for this DIE.
2895 unsigned AbbrevNumber = Die->getAbbrevNumber();
2896 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002897
Jim Laskeybacd3042007-02-21 22:48:45 +00002898 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002899
2900 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002901 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002902
2903 if (VerboseAsm)
2904 Asm->EOL(std::string("Abbrev [" +
2905 utostr(AbbrevNumber) +
2906 "] 0x" + utohexstr(Die->getOffset()) +
2907 ":0x" + utohexstr(Die->getSize()) + " " +
2908 TagString(Abbrev->getTag())));
2909 else
2910 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002911
Owen Anderson873e1b52008-06-24 21:44:59 +00002912 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2913 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002914
Jim Laskeyef42a012006-11-02 20:12:39 +00002915 // Emit the DIE attribute values.
2916 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2917 unsigned Attr = AbbrevData[i].getAttribute();
2918 unsigned Form = AbbrevData[i].getForm();
2919 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002920
Jim Laskeyef42a012006-11-02 20:12:39 +00002921 switch (Attr) {
2922 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002923 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002924 break;
2925 }
2926 default: {
2927 // Emit an attribute using the defined form.
2928 Values[i]->EmitValue(*this, Form);
2929 break;
2930 }
2931 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002932
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002933 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002934 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002935
Jim Laskeyef42a012006-11-02 20:12:39 +00002936 // Emit the DIE children if any.
2937 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2938 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002939
Jim Laskeyef42a012006-11-02 20:12:39 +00002940 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2941 EmitDIE(Children[j]);
2942 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002943
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002944 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002945 }
2946 }
2947
Jim Laskey65195462006-10-30 13:35:07 +00002948 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2949 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002950 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2951 // Get the children.
2952 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002953
Jim Laskeyef42a012006-11-02 20:12:39 +00002954 // If not last sibling and has children then add sibling offset attribute.
2955 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2956
2957 // Record the abbreviation.
2958 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002959
Jim Laskeyef42a012006-11-02 20:12:39 +00002960 // Get the abbreviation for this DIE.
2961 unsigned AbbrevNumber = Die->getAbbrevNumber();
2962 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2963
2964 // Set DIE offset
2965 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002966
Jim Laskeyef42a012006-11-02 20:12:39 +00002967 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002968 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2969
Owen Anderson873e1b52008-06-24 21:44:59 +00002970 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2971 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002972
2973 // Size the DIE attribute values.
2974 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2975 // Size attribute value.
2976 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2977 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002978
Jim Laskeyef42a012006-11-02 20:12:39 +00002979 // Size the DIE children if any.
2980 if (!Children.empty()) {
2981 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2982 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002983
Jim Laskeyef42a012006-11-02 20:12:39 +00002984 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2985 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2986 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002987
Jim Laskeyef42a012006-11-02 20:12:39 +00002988 // End of children marker.
2989 Offset += sizeof(int8_t);
2990 }
2991
2992 Die->setSize(Offset - Die->getOffset());
2993 return Offset;
2994 }
Jim Laskey65195462006-10-30 13:35:07 +00002995
2996 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2997 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002998 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002999 // Process base compile unit.
3000 CompileUnit *Unit = GetBaseCompileUnit();
3001 // Compute size of compile unit header
3002 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
3003 sizeof(int16_t) + // DWARF version number
3004 sizeof(int32_t) + // Offset Into Abbrev. Section
3005 sizeof(int8_t); // Pointer Size (in bytes)
3006 SizeAndOffsetDie(Unit->getDie(), Offset, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00003007 }
3008
Jim Laskey65195462006-10-30 13:35:07 +00003009 /// EmitDebugInfo - Emit the debug info section.
3010 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003011 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003012 // Start debug info section.
3013 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003014
Jim Laskey5496f012006-11-09 14:52:14 +00003015 CompileUnit *Unit = GetBaseCompileUnit();
3016 DIE *Die = Unit->getDie();
3017 // Emit the compile units header.
3018 EmitLabel("info_begin", Unit->getID());
3019 // Emit size of content not including length itself
3020 unsigned ContentSize = Die->getSize() +
3021 sizeof(int16_t) + // DWARF version number
3022 sizeof(int32_t) + // Offset Into Abbrev. Section
Jim Laskey749b01d2006-11-30 11:09:42 +00003023 sizeof(int8_t) + // Pointer Size (in bytes)
3024 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003025
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003026 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
3027 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00003028 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003029 Asm->EOL("Offset Into Abbrev. Section");
Dan Gohman82482942007-09-27 23:12:31 +00003030 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003031
Jim Laskey5496f012006-11-09 14:52:14 +00003032 EmitDIE(Die);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003033 // FIXME - extra padding for gdb bug.
3034 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
3035 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
3036 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
3037 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
Jim Laskey5496f012006-11-09 14:52:14 +00003038 EmitLabel("info_end", Unit->getID());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003039
Jim Laskeybacd3042007-02-21 22:48:45 +00003040 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003041 }
3042
Jim Laskey65195462006-10-30 13:35:07 +00003043 /// EmitAbbreviations - Emit the abbreviation section.
3044 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003045 void EmitAbbreviations() const {
3046 // Check to see if it is worth the effort.
3047 if (!Abbreviations.empty()) {
3048 // Start the debug abbrev section.
3049 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003050
Jim Laskeyef42a012006-11-02 20:12:39 +00003051 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003052
Jim Laskeyef42a012006-11-02 20:12:39 +00003053 // For each abbrevation.
3054 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
3055 // Get abbreviation data
3056 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003057
Jim Laskeyef42a012006-11-02 20:12:39 +00003058 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003059 Asm->EmitULEB128Bytes(Abbrev->getNumber());
3060 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003061
Jim Laskeyef42a012006-11-02 20:12:39 +00003062 // Emit the abbreviations data.
3063 Abbrev->Emit(*this);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003064
Jim Laskeybacd3042007-02-21 22:48:45 +00003065 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003066 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003067
Jim Laskey7b1b39d2007-02-22 18:22:42 +00003068 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00003069 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00003070
Jim Laskeyef42a012006-11-02 20:12:39 +00003071 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003072
Jim Laskeybacd3042007-02-21 22:48:45 +00003073 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003074 }
3075 }
3076
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003077 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
3078 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003079 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003080 void EmitEndOfLineMatrix(unsigned SectionEnd) {
3081 // Define last address of section.
3082 Asm->EmitInt8(0); Asm->EOL("Extended Op");
3083 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
3084 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
3085 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
3086
3087 // Mark end of matrix.
3088 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
3089 Asm->EmitULEB128Bytes(1); Asm->EOL();
3090 Asm->EmitInt8(1); Asm->EOL();
3091 }
3092
Jim Laskey65195462006-10-30 13:35:07 +00003093 /// EmitDebugLines - Emit source line information.
3094 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003095 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003096 // If the target is using .loc/.file, the assembler will be emitting the
3097 // .debug_line table automatically.
3098 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00003099 return;
3100
Jim Laskeyef42a012006-11-02 20:12:39 +00003101 // Minimum line delta, thus ranging from -10..(255-10).
3102 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
3103 // Maximum line delta, thus ranging from -10..(255-10).
3104 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00003105
Jim Laskeyef42a012006-11-02 20:12:39 +00003106 // Start the dwarf line section.
3107 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003108
Jim Laskeyef42a012006-11-02 20:12:39 +00003109 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003110
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003111 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003112 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00003113 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003114
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003115 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003116
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003117 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003118 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00003119 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003120
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003121 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00003122
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003123 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00003124
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003125 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003126
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003127 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00003128
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003129 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003130
Jim Laskeyef42a012006-11-02 20:12:39 +00003131 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003132 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
3133 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
3134 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
3135 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
3136 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
3137 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
3138 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
3139 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
3140 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00003141
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003142 const UniqueVector<std::string> &Directories = MMI->getDirectories();
Evan Cheng6547e402008-07-01 23:18:29 +00003143 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
Jim Laskeyef42a012006-11-02 20:12:39 +00003144
3145 // Emit directories.
3146 for (unsigned DirectoryID = 1, NDID = Directories.size();
3147 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003148 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00003149 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003150 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003151
Jim Laskeyef42a012006-11-02 20:12:39 +00003152 // Emit files.
3153 for (unsigned SourceID = 1, NSID = SourceFiles.size();
3154 SourceID <= NSID; ++SourceID) {
3155 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003156 Asm->EmitString(SourceFile.getName());
3157 Asm->EOL("Source");
3158 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
3159 Asm->EOL("Directory #");
3160 Asm->EmitULEB128Bytes(0);
3161 Asm->EOL("Mod date");
3162 Asm->EmitULEB128Bytes(0);
3163 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00003164 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003165 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003166
Jim Laskeyef42a012006-11-02 20:12:39 +00003167 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003168
Jim Laskeyef42a012006-11-02 20:12:39 +00003169 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003170 unsigned SecSrcLinesSize = SectionSourceLines.size();
3171
3172 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003173 // Isolate current sections line info.
3174 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00003175
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003176 if (VerboseAsm) {
3177 const Section* S = SectionMap[j + 1];
3178 Asm->EOL(std::string("Section ") + S->getName());
3179 } else
Evan Cheng6547e402008-07-01 23:18:29 +00003180 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003181
3182 // Dwarf assumes we start with first line of first source file.
3183 unsigned Source = 1;
3184 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003185
Jim Laskeyef42a012006-11-02 20:12:39 +00003186 // Construct rows of the address, source, line, column matrix.
3187 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
3188 const SourceLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003189 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00003190 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003191
Jim Laskey1a4a83c2007-01-25 15:45:58 +00003192 unsigned SourceID = LineInfo.getSourceID();
3193 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
3194 unsigned DirectoryID = SourceFile.getDirectoryID();
Evan Cheng6547e402008-07-01 23:18:29 +00003195 if (VerboseAsm)
3196 Asm->EOL(Directories[DirectoryID]
3197 + SourceFile.getName()
3198 + ":"
3199 + utostr_32(LineInfo.getLine()));
3200 else
3201 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003202
3203 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003204 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00003205 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003206 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00003207 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003208
Jim Laskeyef42a012006-11-02 20:12:39 +00003209 // If change of source, then switch to the new source.
3210 if (Source != LineInfo.getSourceID()) {
3211 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003212 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
3213 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00003214 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003215
Jim Laskeyef42a012006-11-02 20:12:39 +00003216 // If change of line.
3217 if (Line != LineInfo.getLine()) {
3218 // Determine offset.
3219 int Offset = LineInfo.getLine() - Line;
3220 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003221
Jim Laskeyef42a012006-11-02 20:12:39 +00003222 // Update line.
3223 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003224
Jim Laskeyef42a012006-11-02 20:12:39 +00003225 // If delta is small enough and in range...
3226 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
3227 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003228 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00003229 } else {
3230 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003231 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
3232 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
3233 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00003234 }
3235 } else {
3236 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003237 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00003238 }
3239 }
3240
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003241 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00003242 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003243
3244 if (SecSrcLinesSize == 0)
3245 // Because we're emitting a debug_line section, we still need a line
3246 // table. The linker and friends expect it to exist. If there's nothing to
3247 // put into it, emit an empty table.
3248 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003249
Jim Laskeyef42a012006-11-02 20:12:39 +00003250 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003251
Jim Laskeybacd3042007-02-21 22:48:45 +00003252 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003253 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003254
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003255 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00003256 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003257 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00003258 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00003259 return;
3260
3261 int stackGrowth =
3262 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3263 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003264 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00003265
3266 // Start the dwarf frame section.
3267 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
3268
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003269 EmitLabel("debug_frame_common", 0);
3270 EmitDifference("debug_frame_common_end", 0,
3271 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003272 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00003273
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003274 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003275 Asm->EmitInt32((int)DW_CIE_ID);
3276 Asm->EOL("CIE Identifier Tag");
3277 Asm->EmitInt8(DW_CIE_VERSION);
3278 Asm->EOL("CIE Version");
3279 Asm->EmitString("");
3280 Asm->EOL("CIE Augmentation");
3281 Asm->EmitULEB128Bytes(1);
3282 Asm->EOL("CIE Code Alignment Factor");
3283 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003284 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003285 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003286 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003287
Jim Laskey5e73d5b2007-01-24 18:45:13 +00003288 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00003289 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003290
Dale Johannesenb97aec62007-11-13 19:13:01 +00003291 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00003292
Evan Cheng05548eb2008-02-29 19:36:59 +00003293 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003294 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003295
Jim Laskeybacd3042007-02-21 22:48:45 +00003296 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003297 }
3298
Jim Laskey65195462006-10-30 13:35:07 +00003299 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
3300 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003301 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00003302 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00003303 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003304
Jim Laskeyef42a012006-11-02 20:12:39 +00003305 // Start the dwarf frame section.
3306 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003307
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003308 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
3309 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003310 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003311
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003312 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003313
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003314 EmitSectionOffset("debug_frame_common", "section_debug_frame",
3315 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003316 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00003317
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003318 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003319 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003320 EmitDifference("func_end", DebugFrameInfo.Number,
3321 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003322 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003323
Dale Johannesenb97aec62007-11-13 19:13:01 +00003324 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves, false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003325
Evan Cheng05548eb2008-02-29 19:36:59 +00003326 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003327 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00003328
Jim Laskeybacd3042007-02-21 22:48:45 +00003329 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003330 }
3331
3332 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00003333 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003334 void EmitDebugPubNames() {
3335 // Start the dwarf pubnames section.
3336 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003337
3338 CompileUnit *Unit = GetBaseCompileUnit();
3339
Jim Laskey5496f012006-11-09 14:52:14 +00003340 EmitDifference("pubnames_end", Unit->getID(),
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003341 "pubnames_begin", Unit->getID(), true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003342 Asm->EOL("Length of Public Names Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003343
Jim Laskey5496f012006-11-09 14:52:14 +00003344 EmitLabel("pubnames_begin", Unit->getID());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003345
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003346 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003347
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00003348 EmitSectionOffset("info_begin", "section_info",
3349 Unit->getID(), 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003350 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00003351
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003352 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003353 Asm->EOL("Compilation Unit Length");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003354
Jim Laskey5496f012006-11-09 14:52:14 +00003355 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003356
Jim Laskey5496f012006-11-09 14:52:14 +00003357 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
3358 GE = Globals.end();
3359 GI != GE; ++GI) {
3360 const std::string &Name = GI->first;
3361 DIE * Entity = GI->second;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003362
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003363 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
3364 Asm->EmitString(Name); Asm->EOL("External Name");
Jim Laskeyef42a012006-11-02 20:12:39 +00003365 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003366
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003367 Asm->EmitInt32(0); Asm->EOL("End Mark");
Jim Laskey5496f012006-11-09 14:52:14 +00003368 EmitLabel("pubnames_end", Unit->getID());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003369
Jim Laskeybacd3042007-02-21 22:48:45 +00003370 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003371 }
3372
3373 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00003374 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003375 void EmitDebugStr() {
3376 // Check to see if it is worth the effort.
3377 if (!StringPool.empty()) {
3378 // Start the dwarf str section.
3379 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003380
Jim Laskeyef42a012006-11-02 20:12:39 +00003381 // For each of strings in the string pool.
3382 for (unsigned StringID = 1, N = StringPool.size();
3383 StringID <= N; ++StringID) {
3384 // Emit a label for reference from debug information entries.
3385 EmitLabel("string", StringID);
3386 // Emit the string itself.
3387 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00003388 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003389 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003390
Jim Laskeybacd3042007-02-21 22:48:45 +00003391 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003392 }
3393 }
3394
3395 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00003396 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003397 void EmitDebugLoc() {
3398 // Start the dwarf loc section.
3399 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003400
Jim Laskeybacd3042007-02-21 22:48:45 +00003401 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003402 }
3403
3404 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00003405 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003406 void EmitDebugARanges() {
3407 // Start the dwarf aranges section.
3408 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003409
Jim Laskeyef42a012006-11-02 20:12:39 +00003410 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00003411#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003412 CompileUnit *Unit = GetBaseCompileUnit();
3413
Jim Laskey5496f012006-11-09 14:52:14 +00003414 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003415 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003416
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003417 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003418
Jim Laskey5496f012006-11-09 14:52:14 +00003419 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003420 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00003421
Dan Gohman82482942007-09-27 23:12:31 +00003422 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00003423
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003424 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00003425
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003426 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
3427 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00003428
Jim Laskey5496f012006-11-09 14:52:14 +00003429 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003430 EmitReference("text_begin", 0); Asm->EOL("Address");
3431 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00003432
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003433 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
3434 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00003435#endif
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003436
Jim Laskeybacd3042007-02-21 22:48:45 +00003437 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003438 }
3439
3440 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00003441 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003442 void EmitDebugRanges() {
3443 // Start the dwarf ranges section.
3444 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003445
Jim Laskeybacd3042007-02-21 22:48:45 +00003446 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003447 }
3448
3449 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00003450 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003451 void EmitDebugMacInfo() {
3452 // Start the dwarf macinfo section.
3453 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003454
Jim Laskeybacd3042007-02-21 22:48:45 +00003455 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003456 }
3457
Devang Patelc4523242009-01-05 23:11:11 +00003458 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Pateld1ca9252009-01-05 23:03:32 +00003459 void ConstructCompileUnits() {
3460 std::string CUName = "llvm.dbg.compile_units";
3461 std::vector<GlobalVariable*> Result;
3462 getGlobalVariablesUsing(*M, CUName, Result);
3463 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
3464 RE = Result.end(); RI != RE; ++RI) {
3465 DICompileUnit *DIUnit = new DICompileUnit(*RI);
Devang Patel9f8fcfc2009-01-08 17:19:22 +00003466 unsigned ID = RecordSource(DIUnit->getDirectory(),
3467 DIUnit->getFilename());
Devang Pateld1ca9252009-01-05 23:03:32 +00003468
3469 DIE *Die = new DIE(DW_TAG_compile_unit);
3470 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
3471 DWLabel("section_line", 0), DWLabel("section_line", 0),
3472 false);
3473 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit->getProducer());
3474 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit->getLanguage());
3475 AddString(Die, DW_AT_name, DW_FORM_string, DIUnit->getFilename());
3476 if (!DIUnit->getDirectory().empty())
3477 AddString(Die, DW_AT_comp_dir, DW_FORM_string, DIUnit->getDirectory());
3478
3479 CompileUnit *Unit = new CompileUnit(ID, Die);
3480 DW_CUs[DIUnit->getGV()] = Unit;
3481 }
3482 }
3483
Jim Laskey65195462006-10-30 13:35:07 +00003484 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
3485 /// header file.
Jim Laskeyef42a012006-11-02 20:12:39 +00003486 void ConstructCompileUnitDIEs() {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003487 const UniqueVector<CompileUnitDesc *> CUW = MMI->getCompileUnits();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003488
Jim Laskeyef42a012006-11-02 20:12:39 +00003489 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003490 unsigned ID = MMI->RecordSource(CUW[i]);
Jim Laskey9d4209f2006-11-07 19:33:46 +00003491 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
Jim Laskeyef42a012006-11-02 20:12:39 +00003492 CompileUnits.push_back(Unit);
3493 }
3494 }
3495
Devang Patelc4523242009-01-05 23:11:11 +00003496 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
3497 /// visible global variables.
3498 void ConstructGlobalVariableDIEs() {
3499 std::string GVName = "llvm.dbg.global_variables";
3500 std::vector<GlobalVariable*> Result;
3501 getGlobalVariablesUsing(*M, GVName, Result);
3502 for (std::vector<GlobalVariable *>::iterator GVI = Result.begin(),
3503 GVE = Result.end(); GVI != GVE; ++GVI) {
3504 DIGlobalVariable *DI_GV = new DIGlobalVariable(*GVI);
3505 CompileUnit *DW_Unit = FindCompileUnit(DI_GV->getCompileUnit());
3506
3507 // Check for pre-existence.
3508 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV->getGV());
3509 if (Slot) continue;
3510
3511 DIE *VariableDie = new DIE(DW_TAG_variable);
3512 AddString(VariableDie, DW_AT_name, DW_FORM_string, DI_GV->getName());
3513 const std::string &LinkageName = DI_GV->getLinkageName();
3514 if (!LinkageName.empty())
3515 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
3516 LinkageName);
3517 AddType(DW_Unit, VariableDie, DI_GV->getType());
3518
3519 if (!DI_GV->isLocalToUnit())
3520 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
3521
3522 // Add source line info, if available.
3523 AddSourceLine(VariableDie, DI_GV);
3524
3525 // Add address.
3526 DIEBlock *Block = new DIEBlock();
3527 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
3528 AddObjectLabel(Block, 0, DW_FORM_udata,
3529 Asm->getGlobalLinkName(DI_GV->getGV()));
3530 AddBlock(VariableDie, DW_AT_location, 0, Block);
3531
3532 //Add to map.
3533 Slot = VariableDie;
3534
3535 //Add to context owner.
3536 DW_Unit->getDie()->AddChild(VariableDie);
3537
3538 //Expose as global. FIXME - need to check external flag.
3539 DW_Unit->AddGlobal(DI_GV->getName(), VariableDie);
3540 }
3541 }
3542
Jim Laskey65195462006-10-30 13:35:07 +00003543 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
3544 /// global variables.
Jim Laskeyef42a012006-11-02 20:12:39 +00003545 void ConstructGlobalDIEs() {
Bill Wendlingc04f4652008-07-03 23:13:02 +00003546 std::vector<GlobalVariableDesc *> GlobalVariables;
3547 MMI->getAnchoredDescriptors<GlobalVariableDesc>(*M, GlobalVariables);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003548
Bill Wendling10fff602008-07-03 22:53:42 +00003549 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
3550 GlobalVariableDesc *GVD = GlobalVariables[i];
3551 NewGlobalVariable(GVD);
3552 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003553 }
Jim Laskey65195462006-10-30 13:35:07 +00003554
Devang Patel78eb6ad2009-01-05 23:21:35 +00003555 /// ConstructSubprograms - Create DIEs for each of the externally visible
3556 /// subprograms.
3557 void ConstructSubprograms() {
3558
3559 std::string SPName = "llvm.dbg.subprograms";
3560 std::vector<GlobalVariable*> Result;
3561 getGlobalVariablesUsing(*M, SPName, Result);
3562 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
3563 RE = Result.end(); RI != RE; ++RI) {
3564
3565 DISubprogram *SP = new DISubprogram(*RI);
3566 CompileUnit *Unit = FindCompileUnit(SP->getCompileUnit());
3567
3568 // Check for pre-existence.
3569 DIE *&Slot = Unit->getDieMapSlotFor(SP->getGV());
3570 if (Slot) continue;
3571
3572 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
3573 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, SP->getName());
3574 const std::string &LinkageName = SP->getLinkageName();
3575 if (!LinkageName.empty())
3576 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
3577 LinkageName);
3578 DIType SPTy = SP->getType();
3579 AddType(Unit, SubprogramDie, SPTy);
3580 if (!SP->isLocalToUnit())
3581 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
3582 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
3583
3584 AddSourceLine(SubprogramDie, SP);
3585 //Add to map.
3586 Slot = SubprogramDie;
3587 //Add to context owner.
3588 Unit->getDie()->AddChild(SubprogramDie);
3589 //Expose as global.
3590 Unit->AddGlobal(SP->getName(), SubprogramDie);
3591 }
3592 }
3593
Jim Laskey65195462006-10-30 13:35:07 +00003594 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
3595 /// subprograms.
Jim Laskeyef42a012006-11-02 20:12:39 +00003596 void ConstructSubprogramDIEs() {
Bill Wendlingc04f4652008-07-03 23:13:02 +00003597 std::vector<SubprogramDesc *> Subprograms;
3598 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003599
Bill Wendling10fff602008-07-03 22:53:42 +00003600 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
3601 SubprogramDesc *SPD = Subprograms[i];
3602 NewSubprogram(SPD);
3603 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003604 }
Jim Laskey65195462006-10-30 13:35:07 +00003605
Jim Laskey65195462006-10-30 13:35:07 +00003606public:
Jim Laskeyef42a012006-11-02 20:12:39 +00003607 //===--------------------------------------------------------------------===//
3608 // Main entry points.
3609 //
Owen Andersoncb371882008-08-21 00:14:44 +00003610 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00003611 : Dwarf(OS, A, T, "dbg")
Jim Laskeyef42a012006-11-02 20:12:39 +00003612 , CompileUnits()
3613 , AbbreviationsSet(InitAbbreviationsSetSize)
3614 , Abbreviations()
3615 , ValuesSet(InitValuesSetSize)
3616 , Values()
3617 , StringPool()
3618 , DescToUnitMap()
3619 , SectionMap()
3620 , SectionSourceLines()
Jim Laskeybacd3042007-02-21 22:48:45 +00003621 , didInitial(false)
3622 , shouldEmit(false)
Devang Patel7a6e5a32009-01-08 02:33:41 +00003623 , RootDbgScope(NULL)
Jim Laskeyef42a012006-11-02 20:12:39 +00003624 {
3625 }
Jim Laskey072200c2007-01-29 18:51:14 +00003626 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003627 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
3628 delete CompileUnits[i];
3629 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3630 delete Values[j];
3631 }
3632
Devang Patel5d315982009-01-06 21:07:30 +00003633 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3634 /// This is inovked by the target AsmPrinter.
3635 void SetDebugInfo() {
3636 // FIXME - Check if the module has debug info or not.
3637 // Create all the compile unit DIEs.
3638 ConstructCompileUnits();
3639
3640 // Create DIEs for each of the externally visible global variables.
3641 ConstructGlobalVariableDIEs();
3642
3643 // Create DIEs for each of the externally visible subprograms.
3644 ConstructSubprograms();
3645
3646 // Prime section data.
3647 SectionMap.insert(TAI->getTextSection());
3648
3649 // Print out .file directives to specify files for .loc directives. These
3650 // are printed out early so that they precede any .loc directives.
3651 if (TAI->hasDotLocAndDotFile()) {
3652 for (unsigned i = 1, e = SrcFiles.size(); i <= e; ++i) {
3653 sys::Path FullPath(Directories[SrcFiles[i].getDirectoryID()]);
3654 bool AppendOk = FullPath.appendComponent(SrcFiles[i].getName());
3655 assert(AppendOk && "Could not append filename to directory!");
3656 AppendOk = false;
3657 Asm->EmitFile(i, FullPath.toString());
3658 Asm->EOL();
3659 }
3660 }
3661
3662 // Emit initial sections
3663 EmitInitial();
3664 }
3665
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003666 /// SetModuleInfo - Set machine module information when it's known that pass
3667 /// manager has created it. Set by the target AsmPrinter.
3668 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003669 // Make sure initial declarations are made.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003670 if (!MMI && mmi->hasDebugInfo()) {
3671 MMI = mmi;
Jim Laskeyef42a012006-11-02 20:12:39 +00003672 shouldEmit = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003673
Jim Laskeyef42a012006-11-02 20:12:39 +00003674 // Create all the compile unit DIEs.
3675 ConstructCompileUnitDIEs();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003676
Jim Laskeyef42a012006-11-02 20:12:39 +00003677 // Create DIEs for each of the externally visible global variables.
3678 ConstructGlobalDIEs();
Jim Laskey65195462006-10-30 13:35:07 +00003679
Jim Laskeyef42a012006-11-02 20:12:39 +00003680 // Create DIEs for each of the externally visible subprograms.
3681 ConstructSubprogramDIEs();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003682
Jim Laskeyef42a012006-11-02 20:12:39 +00003683 // Prime section data.
Jim Laskeyf910a3f2006-11-06 16:23:59 +00003684 SectionMap.insert(TAI->getTextSection());
Dan Gohmand57c3882007-10-01 22:40:20 +00003685
3686 // Print out .file directives to specify files for .loc directives. These
3687 // are printed out early so that they precede any .loc directives.
3688 if (TAI->hasDotLocAndDotFile()) {
3689 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
3690 const UniqueVector<std::string> &Directories = MMI->getDirectories();
3691 for (unsigned i = 1, e = SourceFiles.size(); i <= e; ++i) {
3692 sys::Path FullPath(Directories[SourceFiles[i].getDirectoryID()]);
3693 bool AppendOk = FullPath.appendComponent(SourceFiles[i].getName());
3694 assert(AppendOk && "Could not append filename to directory!");
Devang Pateld0935c32008-12-23 21:55:38 +00003695 AppendOk = false;
Dan Gohmand57c3882007-10-01 22:40:20 +00003696 Asm->EmitFile(i, FullPath.toString());
3697 Asm->EOL();
3698 }
3699 }
3700
3701 // Emit initial sections
3702 EmitInitial();
Jim Laskeyef42a012006-11-02 20:12:39 +00003703 }
3704 }
3705
Jim Laskey65195462006-10-30 13:35:07 +00003706 /// BeginModule - Emit all Dwarf sections that should come prior to the
3707 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00003708 void BeginModule(Module *M) {
3709 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00003710 }
3711
Jim Laskey65195462006-10-30 13:35:07 +00003712 /// EndModule - Emit all Dwarf sections that should come after the content.
3713 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003714 void EndModule() {
3715 if (!ShouldEmitDwarf()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003716
Jim Laskeyef42a012006-11-02 20:12:39 +00003717 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003718 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003719 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00003720 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003721 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003722
Jim Laskeyef42a012006-11-02 20:12:39 +00003723 // End text sections.
3724 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003725 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00003726 EmitLabel("section_end", i);
3727 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003728
3729 // Emit common frame information.
3730 EmitCommonDebugFrame();
3731
3732 // Emit function debug frame information
3733 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3734 E = DebugFrames.end(); I != E; ++I)
3735 EmitFunctionDebugFrame(*I);
3736
Jim Laskeyef42a012006-11-02 20:12:39 +00003737 // Compute DIE offsets and sizes.
3738 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003739
Jim Laskeyef42a012006-11-02 20:12:39 +00003740 // Emit all the DIEs into a debug info section
3741 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003742
Jim Laskeyef42a012006-11-02 20:12:39 +00003743 // Corresponding abbreviations into a abbrev section.
3744 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003745
Jim Laskeyef42a012006-11-02 20:12:39 +00003746 // Emit source line correspondence into a debug line section.
3747 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003748
Jim Laskeyef42a012006-11-02 20:12:39 +00003749 // Emit info into a debug pubnames section.
3750 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003751
Jim Laskeyef42a012006-11-02 20:12:39 +00003752 // Emit info into a debug str section.
3753 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003754
Jim Laskeyef42a012006-11-02 20:12:39 +00003755 // Emit info into a debug loc section.
3756 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003757
Jim Laskeyef42a012006-11-02 20:12:39 +00003758 // Emit info into a debug aranges section.
3759 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003760
Jim Laskeyef42a012006-11-02 20:12:39 +00003761 // Emit info into a debug ranges section.
3762 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003763
Jim Laskeyef42a012006-11-02 20:12:39 +00003764 // Emit info into a debug macinfo section.
3765 EmitDebugMacInfo();
3766 }
3767
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003768 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00003769 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00003770 void BeginFunction(MachineFunction *MF) {
3771 this->MF = MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003772
Jim Laskeyef42a012006-11-02 20:12:39 +00003773 if (!ShouldEmitDwarf()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00003774
3775 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003776 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003777
Jim Laskeyef42a012006-11-02 20:12:39 +00003778 // Assumes in correct section after the entry point.
3779 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00003780
3781 // Emit label for the implicitly defined dbg.stoppoint at the start of
3782 // the function.
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00003783 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
3784 if (!LineInfos.empty()) {
3785 const SourceLineInfo &LineInfo = LineInfos[0];
3786 Asm->printLabel(LineInfo.getLabelID());
3787 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003788 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003789
Jim Laskey65195462006-10-30 13:35:07 +00003790 /// EndFunction - Gather and emit post-function debug information.
3791 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00003792 void EndFunction(MachineFunction *MF) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003793 if (!ShouldEmitDwarf()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003794
Jim Laskeyb82313f2007-02-01 16:31:34 +00003795 // Define end label for subprogram.
3796 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003797
Jim Laskeyef42a012006-11-02 20:12:39 +00003798 // Get function line info.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003799 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
Jim Laskeyef42a012006-11-02 20:12:39 +00003800
3801 if (!LineInfos.empty()) {
3802 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003803 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00003804 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
3805 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
3806 // Append the function info to section info.
3807 SectionLineInfos.insert(SectionLineInfos.end(),
3808 LineInfos.begin(), LineInfos.end());
3809 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003810
Jim Laskeyef42a012006-11-02 20:12:39 +00003811 // Construct scopes for subprogram.
Bill Wendlingd751c642008-09-26 00:28:12 +00003812 if (MMI->getRootScope())
3813 ConstructRootScope(MMI->getRootScope());
3814 else
3815 // FIXME: This is wrong. We are essentially getting past a problem with
3816 // debug information not being able to handle unreachable blocks that have
3817 // debug information in them. In particular, those unreachable blocks that
3818 // have "region end" info in them. That situation results in the "root
3819 // scope" not being created. If that's the case, then emit a "default"
3820 // scope, i.e., one that encompasses the whole function. This isn't
3821 // desirable. And a better way of handling this (and all of the debugging
3822 // information) needs to be explored.
3823 ConstructDefaultScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003824
3825 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3826 MMI->getFrameMoves()));
Devang Patel481ff5b2009-01-12 18:48:36 +00003827
3828 // Clear debug info
3829 if (RootDbgScope) {
3830 delete RootDbgScope;
3831 DbgScopeMap.clear();
3832 RootDbgScope = NULL;
3833 }
3834 Lines.clear();
Jim Laskeyef42a012006-11-02 20:12:39 +00003835 }
Jim Laskey65195462006-10-30 13:35:07 +00003836};
3837
Jim Laskey072200c2007-01-29 18:51:14 +00003838//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003839/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00003840///
3841class DwarfException : public Dwarf {
3842
Jim Laskeyb82313f2007-02-01 16:31:34 +00003843private:
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003844 struct FunctionEHFrameInfo {
3845 std::string FnName;
3846 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003847 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003848 bool hasCalls;
3849 bool hasLandingPads;
3850 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003851 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00003852
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003853 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3854 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003855 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003856 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003857 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003858 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003859 };
3860
3861 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003862
3863 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3864 /// be emitted.
3865 bool shouldEmitTable;
3866
3867 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3868 /// should be emitted.
3869 bool shouldEmitMoves;
3870
3871 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3872 /// should be emitted.
3873 bool shouldEmitTableModule;
3874
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003875 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003876 /// should be emitted.
3877 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00003878
Jim Laskey3f09fc22007-02-28 18:38:31 +00003879 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3880 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003881 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003882 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003883 int stackGrowth =
3884 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3885 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003886 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003887
Jim Laskeybacd3042007-02-21 22:48:45 +00003888 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003889 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling722f5f12008-12-24 08:05:17 +00003890
3891 if (!TAI->doesRequireNonLocalEHFrameLabel())
3892 O << TAI->getEHGlobalPrefix();
3893 O << "EH_frame" << Index << ":\n";
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003894 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003895
Jim Laskeybacd3042007-02-21 22:48:45 +00003896 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003897 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003898
Jim Laskeybacd3042007-02-21 22:48:45 +00003899 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003900 EmitDifference("eh_frame_common_end", Index,
3901 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003902 Asm->EOL("Length of Common Information Entry");
3903
Jim Laskeybacd3042007-02-21 22:48:45 +00003904 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003905 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003906 Asm->EmitInt32((int)0);
3907 Asm->EOL("CIE Identifier Tag");
3908 Asm->EmitInt8(DW_CIE_VERSION);
3909 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00003910
Jim Laskeybacd3042007-02-21 22:48:45 +00003911 // The personality presence indicates that language specific information
3912 // will show up in the eh frame.
3913 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00003914 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00003915
Jim Laskeybacd3042007-02-21 22:48:45 +00003916 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003917 Asm->EmitULEB128Bytes(1);
3918 Asm->EOL("CIE Code Alignment Factor");
3919 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00003920 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003921 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00003922 Asm->EOL("CIE Return Address Column");
3923
Jim Laskeybacd3042007-02-21 22:48:45 +00003924 // If there is a personality, we need to indicate the functions location.
3925 if (Personality) {
3926 Asm->EmitULEB128Bytes(7);
3927 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00003928
Duncan Sands671fa972008-05-07 19:11:09 +00003929 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003930 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00003931 Asm->EOL("Personality (pcrel sdata4 indirect)");
3932 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003933 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00003934 Asm->EOL("Personality (pcrel sdata4)");
3935 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00003936
Duncan Sands671fa972008-05-07 19:11:09 +00003937 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00003938 O << TAI->getPersonalityPrefix();
3939 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3940 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00003941 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3942 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00003943 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00003944
Duncan Sands671fa972008-05-07 19:11:09 +00003945 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3946 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003947
Bill Wendlingd60de512009-01-05 22:53:45 +00003948 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3949 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003950 } else {
3951 Asm->EmitULEB128Bytes(1);
3952 Asm->EOL("Augmentation Size");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003953
Bill Wendlingd60de512009-01-05 22:53:45 +00003954 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3955 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003956 }
3957
3958 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003959 std::vector<MachineMove> Moves;
3960 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00003961 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003962
Dale Johannesen21d972a2008-04-30 00:43:29 +00003963 // On Darwin the linker honors the alignment of eh_frame, which means it
3964 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3965 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003966 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003967 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003968 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003969
Jim Laskeybacd3042007-02-21 22:48:45 +00003970 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003971 }
Duncan Sands671fa972008-05-07 19:11:09 +00003972
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003973 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003974 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003975 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003976 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3977
Jim Laskeybacd3042007-02-21 22:48:45 +00003978 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3979
3980 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003981 // If the corresponding function is static, this should not be
3982 // externally visible.
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003983 if (linkage != Function::InternalLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003984 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3985 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3986 }
3987
Dale Johannesen038129d2008-01-10 02:03:30 +00003988 // If corresponding function is weak definition, this should be too.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003989 if ((linkage == Function::WeakLinkage ||
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003990 linkage == Function::LinkOnceLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00003991 TAI->getWeakDefDirective())
3992 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3993
3994 // If there are no calls then you can't unwind. This may mean we can
3995 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003996 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00003997 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00003998 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00003999 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00004000 !UnwindTablesMandatory &&
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004001 ((linkage != Function::WeakLinkage &&
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004002 linkage != Function::LinkOnceLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00004003 !TAI->getWeakDefDirective() ||
4004 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004005 {
Bill Wendling6e198962007-09-18 01:47:22 +00004006 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004007 // This name has no connection to the function, so it might get
4008 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004009 // dead-stripping unconditionally.
4010 if (const char *UsedDirective = TAI->getUsedDirective())
4011 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00004012 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00004013 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00004014
Jim Laskeybacd3042007-02-21 22:48:45 +00004015 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004016 EmitDifference("eh_frame_end", EHFrameInfo.Number,
4017 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00004018 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004019
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004020 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00004021
Bill Wendling722f5f12008-12-24 08:05:17 +00004022 if (TAI->doesRequireNonLocalEHFrameLabel()) {
4023 PrintRelDirective(true, true);
4024 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
4025
4026 if (!TAI->isAbsoluteEHSectionOffsets())
4027 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
4028 } else {
4029 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
4030 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
4031 true, true, false);
4032 }
4033
Jim Laskeybacd3042007-02-21 22:48:45 +00004034 Asm->EOL("FDE CIE offset");
4035
Bill Wendling5fe1fac2009-01-06 19:13:55 +00004036 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00004037 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004038 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendling5fe1fac2009-01-06 19:13:55 +00004039 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00004040 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00004041
Jim Laskey3f09fc22007-02-28 18:38:31 +00004042 // If there is a personality and landing pads then point to the language
4043 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00004044 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00004045 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00004046 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00004047
4048 if (EHFrameInfo.hasLandingPads)
4049 EmitReference("exception", EHFrameInfo.Number, true, true);
4050 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00004051 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00004052 Asm->EOL("Language Specific Data Area");
4053 } else {
4054 Asm->EmitULEB128Bytes(0);
4055 Asm->EOL("Augmentation size");
4056 }
Duncan Sands671fa972008-05-07 19:11:09 +00004057
Jim Laskeybacd3042007-02-21 22:48:45 +00004058 // Indicate locations of function specific callee saved registers in
4059 // frame.
Dale Johannesenb97aec62007-11-13 19:13:01 +00004060 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004061
Dale Johannesen21d972a2008-04-30 00:43:29 +00004062 // On Darwin the linker honors the alignment of eh_frame, which means it
4063 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
4064 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004065 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00004066 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004067 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004068
4069 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004070 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004071 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004072 // that depends on unused functions (calling undefined externals) being
4073 // dead-stripped to link correctly. Yes, there really is.
4074 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
4075 if (const char *UsedDirective = TAI->getUsedDirective())
4076 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
4077 }
Jim Laskeybacd3042007-02-21 22:48:45 +00004078 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00004079
Duncan Sands57810cd2007-09-05 11:27:52 +00004080 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004081 ///
4082 /// The general organization of the table is complex, but the basic concepts
4083 /// are easy. First there is a header which describes the location and
4084 /// organization of the three components that follow.
4085 /// 1. The landing pad site information describes the range of code covered
4086 /// by the try. In our case it's an accumulation of the ranges covered
4087 /// by the invokes in the try. There is also a reference to the landing
4088 /// pad that handles the exception once processed. Finally an index into
4089 /// the actions table.
4090 /// 2. The action table, in our case, is composed of pairs of type ids
4091 /// and next action offset. Starting with the action index from the
4092 /// landing pad site, each type Id is checked for a match to the current
4093 /// exception. If it matches then the exception and type id are passed
4094 /// on to the landing pad. Otherwise the next action is looked up. This
4095 /// chain is terminated with a next action of zero. If no type id is
4096 /// found the the frame is unwound and handling continues.
4097 /// 3. Type id table contains references to all the C++ typeinfo for all
4098 /// catches in the function. This tables is reversed indexed base 1.
4099
Duncan Sandsb32edb42007-06-06 15:37:31 +00004100 /// SharedTypeIds - How many leading type ids two landing pads have in common.
4101 static unsigned SharedTypeIds(const LandingPadInfo *L,
4102 const LandingPadInfo *R) {
4103 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004104 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00004105 unsigned MinSize = LSize < RSize ? LSize : RSize;
4106 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004107
Duncan Sandsb32edb42007-06-06 15:37:31 +00004108 for (; Count != MinSize; ++Count)
4109 if (LIds[Count] != RIds[Count])
4110 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004111
Duncan Sandsb32edb42007-06-06 15:37:31 +00004112 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004113 }
4114
Duncan Sandsb32edb42007-06-06 15:37:31 +00004115 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00004116 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00004117 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004118 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00004119 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004120
Duncan Sandsb32edb42007-06-06 15:37:31 +00004121 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00004122 if (LIds[i] != RIds[i])
4123 return LIds[i] < RIds[i];
4124
Duncan Sandsb32edb42007-06-06 15:37:31 +00004125 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004126 }
4127
Duncan Sands53c3a332007-05-16 12:12:23 +00004128 struct KeyInfo {
4129 static inline unsigned getEmptyKey() { return -1U; }
4130 static inline unsigned getTombstoneKey() { return -2U; }
4131 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00004132 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00004133 static bool isPod() { return true; }
4134 };
4135
Duncan Sands57810cd2007-09-05 11:27:52 +00004136 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004137 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004138 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004139 int NextAction;
4140 struct ActionEntry *Previous;
4141 };
4142
Duncan Sands57810cd2007-09-05 11:27:52 +00004143 /// PadRange - Structure holding a try-range and the associated landing pad.
4144 struct PadRange {
4145 // The index of the landing pad.
4146 unsigned PadIndex;
4147 // The index of the begin and end labels in the landing pad's label lists.
4148 unsigned RangeIndex;
4149 };
4150
4151 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
4152
4153 /// CallSiteEntry - Structure describing an entry in the call-site table.
4154 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00004155 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00004156 unsigned BeginLabel; // zero indicates the start of the function.
4157 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00004158 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00004159 unsigned PadLabel; // zero indicates that there is no landing pad.
4160 unsigned Action;
4161 };
4162
Jim Laskeybacd3042007-02-21 22:48:45 +00004163 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00004164 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00004165 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00004166 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
4167 if (PadInfos.empty()) return;
4168
4169 // Sort the landing pads in order of their type ids. This is used to fold
4170 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00004171 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00004172 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00004173 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00004174 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00004175 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
4176
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004177 // Negative type ids index into FilterIds, positive type ids index into
4178 // TypeInfos. The value written for a positive type id is just the type
4179 // id itself. For a negative type id, however, the value written is the
4180 // (negative) byte offset of the corresponding FilterIds entry. The byte
4181 // offset is usually equal to the type id, because the FilterIds entries
4182 // are written using a variable width encoding which outputs one byte per
4183 // entry as long as the value written is not too large, but can differ.
4184 // This kind of complication does not occur for positive type ids because
4185 // type infos are output using a fixed width encoding.
4186 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
4187 SmallVector<int, 16> FilterOffsets;
4188 FilterOffsets.reserve(FilterIds.size());
4189 int Offset = -1;
4190 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
4191 E = FilterIds.end(); I != E; ++I) {
4192 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004193 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004194 }
4195
Duncan Sands57810cd2007-09-05 11:27:52 +00004196 // Compute the actions table and gather the first action index for each
4197 // landing pad site.
4198 SmallVector<ActionEntry, 32> Actions;
4199 SmallVector<unsigned, 64> FirstActions;
4200 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00004201
Duncan Sandsb32edb42007-06-06 15:37:31 +00004202 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00004203 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004204 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00004205 const LandingPadInfo *LP = LandingPads[i];
4206 const std::vector<int> &TypeIds = LP->TypeIds;
4207 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004208 unsigned SizeSiteActions = 0;
4209
Duncan Sandsb32edb42007-06-06 15:37:31 +00004210 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00004211 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00004212 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004213
Duncan Sandsb32edb42007-06-06 15:37:31 +00004214 if (NumShared) {
4215 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
4216 assert(Actions.size());
4217 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004218 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
4219 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004220 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004221 SizeAction -=
4222 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004223 SizeAction += -PrevAction->NextAction;
4224 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004225 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00004226 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00004227
Duncan Sandsb32edb42007-06-06 15:37:31 +00004228 // Compute the actions.
4229 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
4230 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004231 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
4232 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004233 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004234
4235 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004236 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004237 SizeSiteActions += SizeAction;
4238
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004239 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00004240 Actions.push_back(Action);
4241
4242 PrevAction = &Actions.back();
4243 }
4244
4245 // Record the first action of the landing pad site.
4246 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
4247 } // else identical - re-use previous FirstAction
4248
4249 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00004250
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00004251 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00004252 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00004253 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004254
Duncan Sands481dc722007-12-19 07:36:31 +00004255 // Compute the call-site table. The entry for an invoke has a try-range
4256 // containing the call, a non-zero landing pad and an appropriate action.
4257 // The entry for an ordinary call has a try-range containing the call and
4258 // zero for the landing pad and the action. Calls marked 'nounwind' have
4259 // no entry and must not be contained in the try-range of any entry - they
4260 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004261 SmallVector<CallSiteEntry, 64> CallSites;
4262
4263 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00004264 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
4265 // by try-range labels when lowered). Ordinary calls do not, so appropriate
4266 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00004267 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
4268 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00004269 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004270 unsigned BeginLabel = LandingPad->BeginLabels[j];
4271 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
4272 PadRange P = { i, j };
4273 PadMap[BeginLabel] = P;
4274 }
4275 }
4276
Duncan Sands481dc722007-12-19 07:36:31 +00004277 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00004278 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00004279
4280 // Whether there is a potentially throwing instruction (currently this means
4281 // an ordinary call) between the end of the previous try-range and now.
4282 bool SawPotentiallyThrowing = false;
4283
4284 // Whether the last callsite entry was for an invoke.
4285 bool PreviousIsInvoke = false;
4286
Duncan Sands481dc722007-12-19 07:36:31 +00004287 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004288 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
4289 I != E; ++I) {
4290 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
4291 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00004292 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00004293 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00004294 continue;
4295 }
4296
Chris Lattner9e330492007-12-30 20:50:28 +00004297 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00004298 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00004299
Duncan Sands481dc722007-12-19 07:36:31 +00004300 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00004301 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00004302 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004303
Duncan Sands481dc722007-12-19 07:36:31 +00004304 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00004305 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00004306 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00004307 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00004308 continue;
4309
4310 PadRange P = L->second;
4311 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
4312
4313 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
4314 "Inconsistent landing pad map!");
4315
4316 // If some instruction between the previous try-range and this one may
4317 // throw, create a call-site entry with no landing pad for the region
4318 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00004319 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004320 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
4321 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00004322 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004323 }
4324
4325 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00004326 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00004327
Duncan Sands481dc722007-12-19 07:36:31 +00004328 if (LandingPad->LandingPadLabel) {
4329 // This try-range is for an invoke.
4330 CallSiteEntry Site = {BeginLabel, LastLabel,
4331 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00004332
Duncan Sands481dc722007-12-19 07:36:31 +00004333 // Try to merge with the previous call-site.
4334 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00004335 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00004336 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
4337 // Extend the range of the previous entry.
4338 Prev.EndLabel = Site.EndLabel;
4339 continue;
4340 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004341 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004342
Duncan Sands481dc722007-12-19 07:36:31 +00004343 // Otherwise, create a new call-site.
4344 CallSites.push_back(Site);
4345 PreviousIsInvoke = true;
4346 } else {
4347 // Create a gap.
4348 PreviousIsInvoke = false;
4349 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004350 }
4351 }
4352 // If some instruction between the previous try-range and the end of the
4353 // function may throw, create a call-site entry with no landing pad for the
4354 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00004355 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004356 CallSiteEntry Site = {LastLabel, 0, 0, 0};
4357 CallSites.push_back(Site);
4358 }
4359
Jim Laskeybacd3042007-02-21 22:48:45 +00004360 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00004361
4362 // Call sites.
4363 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
4364 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
4365 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
4366 unsigned SizeSites = CallSites.size() * (SiteStartSize +
4367 SiteLengthSize +
4368 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00004369 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004370 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00004371
Duncan Sands671fa972008-05-07 19:11:09 +00004372 // Type infos.
4373 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
4374 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004375
4376 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004377 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004378 SizeSites + SizeActions + SizeTypes;
4379
4380 unsigned TotalSize = sizeof(int8_t) + // LPStart format
4381 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004382 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004383 TypeOffset;
4384
4385 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004386
Jim Laskeybacd3042007-02-21 22:48:45 +00004387 // Begin the exception table.
4388 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng05548eb2008-02-29 19:36:59 +00004389 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesend65b2642008-10-08 21:50:21 +00004390 O << "GCC_except_table" << SubprogramCount << ":\n";
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004391 for (unsigned i = 0; i != SizeAlign; ++i) {
4392 Asm->EmitInt8(0);
4393 Asm->EOL("Padding");
4394 }
Jim Laskeybacd3042007-02-21 22:48:45 +00004395 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004396
Jim Laskeybacd3042007-02-21 22:48:45 +00004397 // Emit the header.
4398 Asm->EmitInt8(DW_EH_PE_omit);
4399 Asm->EOL("LPStart format (DW_EH_PE_omit)");
4400 Asm->EmitInt8(DW_EH_PE_absptr);
4401 Asm->EOL("TType format (DW_EH_PE_absptr)");
4402 Asm->EmitULEB128Bytes(TypeOffset);
4403 Asm->EOL("TType base offset");
4404 Asm->EmitInt8(DW_EH_PE_udata4);
4405 Asm->EOL("Call site format (DW_EH_PE_udata4)");
4406 Asm->EmitULEB128Bytes(SizeSites);
4407 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004408
Duncan Sands57810cd2007-09-05 11:27:52 +00004409 // Emit the landing pad site information.
4410 for (unsigned i = 0; i < CallSites.size(); ++i) {
4411 CallSiteEntry &S = CallSites[i];
4412 const char *BeginTag;
4413 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00004414
Duncan Sands57810cd2007-09-05 11:27:52 +00004415 if (!S.BeginLabel) {
4416 BeginTag = "eh_func_begin";
4417 BeginNumber = SubprogramCount;
4418 } else {
4419 BeginTag = "label";
4420 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00004421 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004422
Duncan Sands57810cd2007-09-05 11:27:52 +00004423 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004424 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004425 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00004426
Duncan Sands57810cd2007-09-05 11:27:52 +00004427 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00004428 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00004429 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004430 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00004431 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004432 }
4433 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004434
Duncan Sands671fa972008-05-07 19:11:09 +00004435 if (!S.PadLabel)
4436 Asm->EmitInt32(0);
4437 else
Duncan Sands57810cd2007-09-05 11:27:52 +00004438 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004439 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004440 Asm->EOL("Landing pad");
4441
4442 Asm->EmitULEB128Bytes(S.Action);
4443 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004444 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004445
Jim Laskeybacd3042007-02-21 22:48:45 +00004446 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004447 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4448 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00004449
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004450 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004451 Asm->EOL("TypeInfo index");
4452 Asm->EmitSLEB128Bytes(Action.NextAction);
4453 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004454 }
4455
4456 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00004457 for (unsigned M = TypeInfos.size(); M; --M) {
4458 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00004459
4460 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00004461
4462 if (GV)
4463 O << Asm->getGlobalLinkName(GV);
4464 else
4465 O << "0";
Duncan Sands57810cd2007-09-05 11:27:52 +00004466
Jim Laskeybacd3042007-02-21 22:48:45 +00004467 Asm->EOL("TypeInfo");
4468 }
4469
Duncan Sands73ef58a2007-06-02 16:53:42 +00004470 // Emit the filter typeids.
4471 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4472 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00004473 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00004474 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00004475 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004476
Evan Cheng05548eb2008-02-29 19:36:59 +00004477 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004478 }
4479
Jim Laskey072200c2007-01-29 18:51:14 +00004480public:
4481 //===--------------------------------------------------------------------===//
4482 // Main entry points.
4483 //
Owen Andersoncb371882008-08-21 00:14:44 +00004484 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00004485 : Dwarf(OS, A, T, "eh")
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004486 , shouldEmitTable(false)
4487 , shouldEmitMoves(false)
4488 , shouldEmitTableModule(false)
4489 , shouldEmitMovesModule(false)
Jim Laskey072200c2007-01-29 18:51:14 +00004490 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004491
Jim Laskey072200c2007-01-29 18:51:14 +00004492 virtual ~DwarfException() {}
4493
4494 /// SetModuleInfo - Set machine module information when it's known that pass
4495 /// manager has created it. Set by the target AsmPrinter.
4496 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00004497 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00004498 }
4499
4500 /// BeginModule - Emit all exception information that should come prior to the
4501 /// content.
4502 void BeginModule(Module *M) {
4503 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00004504 }
4505
4506 /// EndModule - Emit all exception information that should come after the
4507 /// content.
4508 void EndModule() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004509 if (shouldEmitMovesModule || shouldEmitTableModule) {
4510 const std::vector<Function *> Personalities = MMI->getPersonalities();
4511 for (unsigned i =0; i < Personalities.size(); ++i)
4512 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00004513
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004514 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4515 E = EHFrames.end(); I != E; ++I)
4516 EmitEHFrame(*I);
4517 }
Jim Laskey072200c2007-01-29 18:51:14 +00004518 }
4519
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004520 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00004521 /// emitted immediately after the function entry point.
4522 void BeginFunction(MachineFunction *MF) {
4523 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004524 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesene0040622008-04-02 17:04:45 +00004525 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004526
4527 // Map all labels and get rid of any dead landing pads.
4528 MMI->TidyLandingPads();
4529 // If any landing pads survive, we need an EH table.
4530 if (MMI->getLandingPads().size())
4531 shouldEmitTable = true;
4532
4533 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00004534 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004535 shouldEmitMoves = true;
4536
4537 if (shouldEmitMoves || shouldEmitTable)
4538 // Assumes in correct section after the entry point.
4539 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00004540 }
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004541 shouldEmitTableModule |= shouldEmitTable;
4542 shouldEmitMovesModule |= shouldEmitMoves;
Jim Laskey072200c2007-01-29 18:51:14 +00004543 }
4544
4545 /// EndFunction - Gather and emit post-function exception information.
4546 ///
4547 void EndFunction() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004548 if (shouldEmitMoves || shouldEmitTable) {
4549 EmitLabel("eh_func_end", SubprogramCount);
4550 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00004551
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004552 // Save EH frame information
4553 EHFrames.
4554 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendling6e198962007-09-18 01:47:22 +00004555 SubprogramCount,
4556 MMI->getPersonalityIndex(),
4557 MF->getFrameInfo()->hasCalls(),
4558 !MMI->getLandingPads().empty(),
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00004559 MMI->getFrameMoves(),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004560 MF->getFunction()));
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004561 }
Jim Laskey072200c2007-01-29 18:51:14 +00004562 }
4563};
4564
Jim Laskey0d086af2006-02-27 12:43:29 +00004565} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00004566
4567//===----------------------------------------------------------------------===//
4568
Jim Laskeyd18e2892006-01-20 20:34:06 +00004569/// Emit - Print the abbreviation using the specified Dwarf writer.
4570///
Jim Laskey072200c2007-01-29 18:51:14 +00004571void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004572 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00004573 DD.getAsm()->EmitULEB128Bytes(Tag);
4574 DD.getAsm()->EOL(TagString(Tag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004575
Jim Laskeyd18e2892006-01-20 20:34:06 +00004576 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00004577 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4578 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004579
Jim Laskeyd18e2892006-01-20 20:34:06 +00004580 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00004581 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004582 const DIEAbbrevData &AttrData = Data[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004583
Jim Laskeyd18e2892006-01-20 20:34:06 +00004584 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00004585 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4586 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004587
Jim Laskeyd18e2892006-01-20 20:34:06 +00004588 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00004589 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4590 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00004591 }
4592
4593 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00004594 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4595 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00004596}
4597
4598#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00004599void DIEAbbrev::print(std::ostream &O) {
4600 O << "Abbreviation @"
4601 << std::hex << (intptr_t)this << std::dec
4602 << " "
4603 << TagString(Tag)
4604 << " "
4605 << ChildrenString(ChildrenFlag)
4606 << "\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004607
Jim Laskeya0f3d172006-09-07 22:06:40 +00004608 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4609 O << " "
4610 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004611 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00004612 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004613 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00004614 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00004615}
Bill Wendlinge8156192006-12-07 01:30:32 +00004616void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00004617#endif
4618
4619//===----------------------------------------------------------------------===//
4620
Jim Laskeyef42a012006-11-02 20:12:39 +00004621#ifndef NDEBUG
4622void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004623 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004624}
Jim Laskeyef42a012006-11-02 20:12:39 +00004625#endif
4626
4627//===----------------------------------------------------------------------===//
4628
Jim Laskey063e7652006-01-17 17:31:53 +00004629/// EmitValue - Emit integer of appropriate size.
4630///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004631void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00004632 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00004633 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00004634 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004635 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004636 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004637 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004638 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004639 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004640 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004641 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4642 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4643 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004644 default: assert(0 && "DIE Value form not supported yet"); break;
4645 }
4646}
4647
4648/// SizeOf - Determine size of integer value in bytes.
4649///
Jim Laskey072200c2007-01-29 18:51:14 +00004650unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004651 switch (Form) {
4652 case DW_FORM_flag: // Fall thru
4653 case DW_FORM_ref1: // Fall thru
4654 case DW_FORM_data1: return sizeof(int8_t);
4655 case DW_FORM_ref2: // Fall thru
4656 case DW_FORM_data2: return sizeof(int16_t);
4657 case DW_FORM_ref4: // Fall thru
4658 case DW_FORM_data4: return sizeof(int32_t);
4659 case DW_FORM_ref8: // Fall thru
4660 case DW_FORM_data8: return sizeof(int64_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004661 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4662 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00004663 default: assert(0 && "DIE Value form not supported yet"); break;
4664 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004665 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00004666}
4667
Jim Laskey063e7652006-01-17 17:31:53 +00004668//===----------------------------------------------------------------------===//
4669
4670/// EmitValue - Emit string value.
4671///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004672void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004673 DD.getAsm()->EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00004674}
4675
Jim Laskey063e7652006-01-17 17:31:53 +00004676//===----------------------------------------------------------------------===//
4677
4678/// EmitValue - Emit label value.
4679///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004680void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004681 bool IsSmall = Form == DW_FORM_data4;
4682 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004683}
4684
4685/// SizeOf - Determine size of label value in bytes.
4686///
Jim Laskey072200c2007-01-29 18:51:14 +00004687unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004688 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004689 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004690}
Jim Laskeyef42a012006-11-02 20:12:39 +00004691
Jim Laskey063e7652006-01-17 17:31:53 +00004692//===----------------------------------------------------------------------===//
4693
Jim Laskeyd18e2892006-01-20 20:34:06 +00004694/// EmitValue - Emit label value.
4695///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004696void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004697 bool IsSmall = Form == DW_FORM_data4;
4698 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00004699}
4700
4701/// SizeOf - Determine size of label value in bytes.
4702///
Jim Laskey072200c2007-01-29 18:51:14 +00004703unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004704 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004705 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00004706}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004707
Jim Laskeyd18e2892006-01-20 20:34:06 +00004708//===----------------------------------------------------------------------===//
4709
Jim Laskey063e7652006-01-17 17:31:53 +00004710/// EmitValue - Emit delta value.
4711///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004712void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4713 bool IsSmall = Form == DW_FORM_data4;
4714 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4715 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4716}
4717
4718/// SizeOf - Determine size of delta value in bytes.
4719///
4720unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4721 if (Form == DW_FORM_data4) return 4;
4722 return DD.getTargetData()->getPointerSize();
4723}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004724
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004725//===----------------------------------------------------------------------===//
4726
4727/// EmitValue - Emit delta value.
4728///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004729void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004730 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00004731 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004732}
4733
4734/// SizeOf - Determine size of delta value in bytes.
4735///
Jim Laskey072200c2007-01-29 18:51:14 +00004736unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004737 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004738 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004739}
4740
4741//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004742
Jim Laskeyb8509c52006-03-23 18:07:55 +00004743/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004744///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004745void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004746 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00004747}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004748
Jim Laskeyd18e2892006-01-20 20:34:06 +00004749//===----------------------------------------------------------------------===//
4750
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004751/// ComputeSize - calculate the size of the block.
4752///
Jim Laskey072200c2007-01-29 18:51:14 +00004753unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00004754 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00004755 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004756
Jim Laskeyef42a012006-11-02 20:12:39 +00004757 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00004758 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00004759 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004760 }
4761 return Size;
4762}
4763
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004764/// EmitValue - Emit block data.
4765///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004766void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004767 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004768 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4769 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4770 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4771 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004772 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004773 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004774
Owen Anderson873e1b52008-06-24 21:44:59 +00004775 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00004776
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004777 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00004778 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00004779 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004780 }
4781}
4782
4783/// SizeOf - Determine size of block data in bytes.
4784///
Jim Laskey072200c2007-01-29 18:51:14 +00004785unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004786 switch (Form) {
4787 case DW_FORM_block1: return Size + sizeof(int8_t);
4788 case DW_FORM_block2: return Size + sizeof(int16_t);
4789 case DW_FORM_block4: return Size + sizeof(int32_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004790 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004791 default: assert(0 && "Improper form for block"); break;
4792 }
4793 return 0;
4794}
4795
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004796//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004797/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00004798
4799DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00004800 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00004801 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00004802}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004803
Jim Laskeyb8509c52006-03-23 18:07:55 +00004804/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4805///
4806void DIE::AddSiblingOffset() {
4807 DIEInteger *DI = new DIEInteger(0);
4808 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00004809 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00004810}
4811
Jim Laskeyef42a012006-11-02 20:12:39 +00004812/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004813///
Jim Laskeyef42a012006-11-02 20:12:39 +00004814void DIE::Profile(FoldingSetNodeID &ID) {
4815 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004816
Jim Laskeyef42a012006-11-02 20:12:39 +00004817 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4818 ID.AddPointer(Children[i]);
4819
4820 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4821 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00004822}
Jim Laskeyef42a012006-11-02 20:12:39 +00004823
4824#ifndef NDEBUG
4825void DIE::print(std::ostream &O, unsigned IncIndent) {
4826 static unsigned IndentCount = 0;
4827 IndentCount += IncIndent;
4828 const std::string Indent(IndentCount, ' ');
4829 bool isBlock = Abbrev.getTag() == 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004830
Jim Laskeyef42a012006-11-02 20:12:39 +00004831 if (!isBlock) {
4832 O << Indent
4833 << "Die: "
4834 << "0x" << std::hex << (intptr_t)this << std::dec
4835 << ", Offset: " << Offset
4836 << ", Size: " << Size
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004837 << "\n";
4838
Jim Laskeyef42a012006-11-02 20:12:39 +00004839 O << Indent
4840 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00004841 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00004842 << ChildrenString(Abbrev.getChildrenFlag());
4843 } else {
4844 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00004845 }
4846 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00004847
Owen Anderson873e1b52008-06-24 21:44:59 +00004848 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004849
Jim Laskeyef42a012006-11-02 20:12:39 +00004850 IndentCount += 2;
4851 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4852 O << Indent;
Bill Wendling4e974012008-07-22 00:28:47 +00004853
4854 if (!isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +00004855 O << AttributeString(Data[i].getAttribute());
Bill Wendling4e974012008-07-22 00:28:47 +00004856 else
Jim Laskeyef42a012006-11-02 20:12:39 +00004857 O << "Blk[" << i << "]";
Bill Wendling4e974012008-07-22 00:28:47 +00004858
Jim Laskeyef42a012006-11-02 20:12:39 +00004859 O << " "
4860 << FormEncodingString(Data[i].getForm())
4861 << " ";
4862 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00004863 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00004864 }
Jim Laskeyef42a012006-11-02 20:12:39 +00004865 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00004866
Jim Laskeyef42a012006-11-02 20:12:39 +00004867 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4868 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00004869 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004870
Jim Laskeyef42a012006-11-02 20:12:39 +00004871 if (!isBlock) O << "\n";
4872 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00004873}
4874
Jim Laskeyef42a012006-11-02 20:12:39 +00004875void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004876 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00004877}
Jim Laskeybd761842006-02-27 17:27:12 +00004878#endif
Jim Laskey65195462006-10-30 13:35:07 +00004879
4880//===----------------------------------------------------------------------===//
4881/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00004882///
Jim Laskey65195462006-10-30 13:35:07 +00004883
Devang Pateleb3fc282009-01-08 23:40:34 +00004884DwarfWriter::DwarfWriter() : ImmutablePass(&ID), DD(NULL), DE(NULL) {
Jim Laskey65195462006-10-30 13:35:07 +00004885}
4886
4887DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00004888 delete DE;
4889 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00004890}
4891
Jim Laskey65195462006-10-30 13:35:07 +00004892/// BeginModule - Emit all Dwarf sections that should come prior to the
4893/// content.
Devang Pateleb3fc282009-01-08 23:40:34 +00004894void DwarfWriter::BeginModule(Module *M,
4895 MachineModuleInfo *MMI,
4896 raw_ostream &OS, AsmPrinter *A,
4897 const TargetAsmInfo *T) {
4898 DE = new DwarfException(OS, A, T);
4899 DD = new DwarfDebug(OS, A, T);
Jim Laskey072200c2007-01-29 18:51:14 +00004900 DE->BeginModule(M);
4901 DD->BeginModule(M);
Devang Pateleb3fc282009-01-08 23:40:34 +00004902 DD->SetModuleInfo(MMI);
4903 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00004904}
4905
4906/// EndModule - Emit all Dwarf sections that should come after the content.
4907///
4908void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00004909 DE->EndModule();
4910 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00004911}
4912
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004913/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00004914/// emitted immediately after the function entry point.
4915void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00004916 DE->BeginFunction(MF);
4917 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00004918}
4919
4920/// EndFunction - Gather and emit post-function debug information.
4921///
Bill Wendlingd751c642008-09-26 00:28:12 +00004922void DwarfWriter::EndFunction(MachineFunction *MF) {
4923 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004924 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004925
Bill Wendlingc91d0b92008-07-22 00:53:37 +00004926 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00004927 // Clear function debug information.
4928 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00004929}