blob: 5e98618b2438606b74ec793ac927d2cb34f7b7db [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"
Devang Patelcf3a4482009-01-15 23:41:32 +000022#include "llvm/Constants.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000023#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskey44c3b9f2007-01-26 21:22:28 +000024#include "llvm/CodeGen/MachineModuleInfo.h"
Jim Laskey41886992006-04-07 16:34:46 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000026#include "llvm/CodeGen/MachineLocation.h"
Devang Patel08f053f2009-01-05 17:57:47 +000027#include "llvm/Analysis/DebugInfo.h"
Jim Laskey3f09fc22007-02-28 18:38:31 +000028#include "llvm/Support/Debug.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000029#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000030#include "llvm/Support/CommandLine.h"
Jim Laskey65195462006-10-30 13:35:07 +000031#include "llvm/Support/DataTypes.h"
Jim Laskey52060a02006-01-24 00:49:18 +000032#include "llvm/Support/Mangler.h"
Owen Andersoncb371882008-08-21 00:14:44 +000033#include "llvm/Support/raw_ostream.h"
Dan Gohman85496362007-09-24 21:32:18 +000034#include "llvm/System/Path.h"
Jim Laskey563321a2006-09-06 18:34:40 +000035#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000036#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000037#include "llvm/Target/TargetData.h"
Jim Laskey1069fbd2006-04-10 23:09:19 +000038#include "llvm/Target/TargetFrameInfo.h"
Duncan Sands53c3a332007-05-16 12:12:23 +000039#include "llvm/Target/TargetInstrInfo.h"
Jim Laskey1b340dc2007-01-29 20:48:32 +000040#include "llvm/Target/TargetMachine.h"
Jim Laskeyc1c47c32007-01-29 23:40:33 +000041#include "llvm/Target/TargetOptions.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000042#include <ostream>
Jim Laskey65195462006-10-30 13:35:07 +000043#include <string>
Jim Laskeyb2efb852006-01-04 22:28:25 +000044using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000045using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000046
Devang Pateleb3fc282009-01-08 23:40:34 +000047static RegisterPass<DwarfWriter>
48X("dwarfwriter", "DWARF Information Writer");
49char DwarfWriter::ID = 0;
50
Jim Laskey0d086af2006-02-27 12:43:29 +000051namespace llvm {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000052
Jim Laskey65195462006-10-30 13:35:07 +000053//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +000054
55/// Configuration values for initial hash set sizes (log2).
56///
57static const unsigned InitDiesSetSize = 9; // 512
58static const unsigned InitAbbreviationsSetSize = 9; // 512
59static const unsigned InitValuesSetSize = 9; // 512
60
61//===----------------------------------------------------------------------===//
62/// Forward declarations.
63///
64class DIE;
65class DIEValue;
66
67//===----------------------------------------------------------------------===//
Devang Pateld1ca9252009-01-05 23:03:32 +000068/// Utility routines.
69///
70/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
71/// specified value in their initializer somewhere.
72static void
73getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
74 // Scan though value users.
75 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
76 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
77 // If the user is a GlobalVariable then add to result.
78 Result.push_back(GV);
79 } else if (Constant *C = dyn_cast<Constant>(*I)) {
80 // If the user is a constant variable then scan its users
81 getGlobalVariablesUsing(C, Result);
82 }
83 }
84}
85
86/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
87/// named GlobalVariable.
88static void
89getGlobalVariablesUsing(Module &M, const std::string &RootName,
90 std::vector<GlobalVariable*> &Result) {
91 std::vector<const Type*> FieldTypes;
92 FieldTypes.push_back(Type::Int32Ty);
93 FieldTypes.push_back(Type::Int32Ty);
94
95 // Get the GlobalVariable root.
96 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
97 StructType::get(FieldTypes));
98
99 // If present and linkonce then scan for users.
100 if (UseRoot && UseRoot->hasLinkOnceLinkage())
101 getGlobalVariablesUsing(UseRoot, Result);
102}
103
Devang Patelcf3a4482009-01-15 23:41:32 +0000104/// getGlobalVariable - Return either a direct or cast Global value.
105///
106static GlobalVariable *getGlobalVariable(Value *V) {
107 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
108 return GV;
109 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
110 if (CE->getOpcode() == Instruction::BitCast) {
111 return dyn_cast<GlobalVariable>(CE->getOperand(0));
112 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
113 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
114 if (!CE->getOperand(i)->isNullValue())
115 return NULL;
116 }
117 return dyn_cast<GlobalVariable>(CE->getOperand(0));
118 }
119 }
120 return NULL;
121}
122
Devang Pateld1ca9252009-01-05 23:03:32 +0000123//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000124/// DWLabel - Labels are used to track locations in the assembler file.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000125/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
126/// where the tag is a category of label (Ex. location) and number is a value
Reid Spencer181b6c92007-08-05 20:06:04 +0000127/// unique in that category.
Jim Laskey65195462006-10-30 13:35:07 +0000128class DWLabel {
129public:
Jim Laskeyef42a012006-11-02 20:12:39 +0000130 /// Tag - Label category tag. Should always be a staticly declared C string.
131 ///
132 const char *Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000133
Jim Laskeyef42a012006-11-02 20:12:39 +0000134 /// Number - Value to make label unique.
135 ///
136 unsigned Number;
Jim Laskey65195462006-10-30 13:35:07 +0000137
138 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000139
Jim Laskeyef42a012006-11-02 20:12:39 +0000140 void Profile(FoldingSetNodeID &ID) const {
141 ID.AddString(std::string(Tag));
142 ID.AddInteger(Number);
Jim Laskey90c79d72006-03-23 23:02:34 +0000143 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000144
Jim Laskeyef42a012006-11-02 20:12:39 +0000145#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000146 void print(std::ostream *O) const {
147 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000148 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000149 void print(std::ostream &O) const {
Jim Laskeybacd3042007-02-21 22:48:45 +0000150 O << "." << Tag;
Jim Laskeyef42a012006-11-02 20:12:39 +0000151 if (Number) O << Number;
152 }
153#endif
Jim Laskeybd761842006-02-27 17:27:12 +0000154};
155
156//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000157/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
158/// Dwarf abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +0000159class DIEAbbrevData {
160private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000161 /// Attribute - Dwarf attribute code.
162 ///
163 unsigned Attribute;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000164
Jim Laskeyef42a012006-11-02 20:12:39 +0000165 /// Form - Dwarf form code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000166 ///
167 unsigned Form;
168
Jim Laskey0d086af2006-02-27 12:43:29 +0000169public:
170 DIEAbbrevData(unsigned A, unsigned F)
171 : Attribute(A)
172 , Form(F)
173 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000174
Jim Laskeybd761842006-02-27 17:27:12 +0000175 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000176 unsigned getAttribute() const { return Attribute; }
177 unsigned getForm() const { return Form; }
Jim Laskey063e7652006-01-17 17:31:53 +0000178
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000179 /// Profile - Used to gather unique data for the abbreviation folding set.
Jim Laskey0d086af2006-02-27 12:43:29 +0000180 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000181 void Profile(FoldingSetNodeID &ID)const {
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000182 ID.AddInteger(Attribute);
183 ID.AddInteger(Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000184 }
185};
Jim Laskey063e7652006-01-17 17:31:53 +0000186
Jim Laskey0d086af2006-02-27 12:43:29 +0000187//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000188/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
189/// information object.
190class DIEAbbrev : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000191private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000192 /// Tag - Dwarf tag code.
193 ///
194 unsigned Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000195
Jim Laskeyef42a012006-11-02 20:12:39 +0000196 /// Unique number for node.
197 ///
198 unsigned Number;
199
200 /// ChildrenFlag - Dwarf children flag.
201 ///
202 unsigned ChildrenFlag;
203
204 /// Data - Raw data bytes for abbreviation.
205 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000206 SmallVector<DIEAbbrevData, 8> Data;
Jim Laskey063e7652006-01-17 17:31:53 +0000207
Jim Laskey0d086af2006-02-27 12:43:29 +0000208public:
Jim Laskey063e7652006-01-17 17:31:53 +0000209
Jim Laskey0d086af2006-02-27 12:43:29 +0000210 DIEAbbrev(unsigned T, unsigned C)
Jim Laskeyef42a012006-11-02 20:12:39 +0000211 : Tag(T)
Jim Laskey0d086af2006-02-27 12:43:29 +0000212 , ChildrenFlag(C)
213 , Data()
214 {}
215 ~DIEAbbrev() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000216
Jim Laskeybd761842006-02-27 17:27:12 +0000217 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000218 unsigned getTag() const { return Tag; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000219 unsigned getNumber() const { return Number; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000220 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000221 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000222 void setTag(unsigned T) { Tag = T; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000223 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000224 void setNumber(unsigned N) { Number = N; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000225
Jim Laskey0d086af2006-02-27 12:43:29 +0000226 /// AddAttribute - Adds another set of attribute information to the
227 /// abbreviation.
228 void AddAttribute(unsigned Attribute, unsigned Form) {
229 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000230 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000231
Jim Laskeyb8509c52006-03-23 18:07:55 +0000232 /// AddFirstAttribute - Adds a set of attribute information to the front
233 /// of the abbreviation.
234 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
235 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
236 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000237
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000238 /// Profile - Used to gather unique data for the abbreviation folding set.
239 ///
240 void Profile(FoldingSetNodeID &ID) {
241 ID.AddInteger(Tag);
242 ID.AddInteger(ChildrenFlag);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000243
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000244 // For each attribute description.
245 for (unsigned i = 0, N = Data.size(); i < N; ++i)
246 Data[i].Profile(ID);
247 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000248
Jim Laskey0d086af2006-02-27 12:43:29 +0000249 /// Emit - Print the abbreviation using the specified Dwarf writer.
250 ///
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000251 void Emit(const DwarfDebug &DD) const;
252
Jim Laskey0d086af2006-02-27 12:43:29 +0000253#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000254 void print(std::ostream *O) {
255 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000256 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000257 void print(std::ostream &O);
258 void dump();
259#endif
260};
Jim Laskey063e7652006-01-17 17:31:53 +0000261
Jim Laskey0d086af2006-02-27 12:43:29 +0000262//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000263/// DIE - A structured debug information entry. Has an abbreviation which
264/// describes it's organization.
265class DIE : public FoldingSetNode {
266protected:
267 /// Abbrev - Buffer for constructing abbreviation.
268 ///
269 DIEAbbrev Abbrev;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000270
Jim Laskeyef42a012006-11-02 20:12:39 +0000271 /// Offset - Offset in debug info section.
272 ///
273 unsigned Offset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000274
Jim Laskeyef42a012006-11-02 20:12:39 +0000275 /// Size - Size of instance + children.
276 ///
277 unsigned Size;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000278
Jim Laskeyef42a012006-11-02 20:12:39 +0000279 /// Children DIEs.
280 ///
281 std::vector<DIE *> Children;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000282
Jim Laskeyef42a012006-11-02 20:12:39 +0000283 /// Attributes values.
284 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000285 SmallVector<DIEValue*, 32> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000286
Jim Laskeyef42a012006-11-02 20:12:39 +0000287public:
Dan Gohman81975f62007-08-27 14:50:10 +0000288 explicit DIE(unsigned Tag)
Jim Laskeyef42a012006-11-02 20:12:39 +0000289 : Abbrev(Tag, DW_CHILDREN_no)
290 , Offset(0)
291 , Size(0)
292 , Children()
293 , Values()
294 {}
295 virtual ~DIE();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000296
Jim Laskeyef42a012006-11-02 20:12:39 +0000297 // Accessors.
298 DIEAbbrev &getAbbrev() { return Abbrev; }
299 unsigned getAbbrevNumber() const {
300 return Abbrev.getNumber();
301 }
Jim Laskey85f419b2006-11-09 16:32:26 +0000302 unsigned getTag() const { return Abbrev.getTag(); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000303 unsigned getOffset() const { return Offset; }
304 unsigned getSize() const { return Size; }
305 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000306 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000307 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
308 void setOffset(unsigned O) { Offset = O; }
309 void setSize(unsigned S) { Size = S; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000310
Jim Laskeyef42a012006-11-02 20:12:39 +0000311 /// AddValue - Add a value and attributes to a DIE.
312 ///
313 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
314 Abbrev.AddAttribute(Attribute, Form);
315 Values.push_back(Value);
316 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000317
Jim Laskeyef42a012006-11-02 20:12:39 +0000318 /// SiblingOffset - Return the offset of the debug information entry's
319 /// sibling.
320 unsigned SiblingOffset() const { return Offset + Size; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000321
Jim Laskeyef42a012006-11-02 20:12:39 +0000322 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
323 ///
324 void AddSiblingOffset();
325
326 /// AddChild - Add a child to the DIE.
327 ///
328 void AddChild(DIE *Child) {
329 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
330 Children.push_back(Child);
331 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000332
Jim Laskeyef42a012006-11-02 20:12:39 +0000333 /// Detach - Detaches objects connected to it after copying.
334 ///
335 void Detach() {
336 Children.clear();
337 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000338
Jim Laskeyef42a012006-11-02 20:12:39 +0000339 /// Profile - Used to gather unique data for the value folding set.
340 ///
341 void Profile(FoldingSetNodeID &ID) ;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000342
Jim Laskeyef42a012006-11-02 20:12:39 +0000343#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000344 void print(std::ostream *O, unsigned IncIndent = 0) {
345 if (O) print(*O, IncIndent);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000346 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000347 void print(std::ostream &O, unsigned IncIndent = 0);
348 void dump();
349#endif
350};
351
352//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000353/// DIEValue - A debug information entry value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000354///
355class DIEValue : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000356public:
357 enum {
358 isInteger,
359 isString,
360 isLabel,
361 isAsIsLabel,
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000362 isSectionOffset,
Jim Laskey0d086af2006-02-27 12:43:29 +0000363 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000364 isEntry,
365 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000366 };
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000367
Jim Laskeyef42a012006-11-02 20:12:39 +0000368 /// Type - Type of data stored in the value.
369 ///
370 unsigned Type;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000371
Dan Gohman81975f62007-08-27 14:50:10 +0000372 explicit DIEValue(unsigned T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000373 : Type(T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000374 {}
Jim Laskey0d086af2006-02-27 12:43:29 +0000375 virtual ~DIEValue() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000376
Jim Laskeyf6733882006-11-02 21:48:18 +0000377 // Accessors
Jim Laskeyef42a012006-11-02 20:12:39 +0000378 unsigned getType() const { return Type; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000379
Jim Laskey0d086af2006-02-27 12:43:29 +0000380 // Implement isa/cast/dyncast.
381 static bool classof(const DIEValue *) { return true; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000382
Jim Laskey0d086af2006-02-27 12:43:29 +0000383 /// EmitValue - Emit value via the Dwarf writer.
384 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000385 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000386
Jim Laskey0d086af2006-02-27 12:43:29 +0000387 /// SizeOf - Return the size of a value in bytes.
388 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000389 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000390
Jim Laskeyef42a012006-11-02 20:12:39 +0000391 /// Profile - Used to gather unique data for the value folding set.
392 ///
393 virtual void Profile(FoldingSetNodeID &ID) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000394
Jim Laskeyef42a012006-11-02 20:12:39 +0000395#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000396 void print(std::ostream *O) {
397 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000398 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000399 virtual void print(std::ostream &O) = 0;
400 void dump();
401#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000402};
Jim Laskey063e7652006-01-17 17:31:53 +0000403
Jim Laskey0d086af2006-02-27 12:43:29 +0000404//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000405/// DWInteger - An integer value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000406///
Jim Laskey0d086af2006-02-27 12:43:29 +0000407class DIEInteger : public DIEValue {
408private:
409 uint64_t Integer;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000410
Jim Laskey0d086af2006-02-27 12:43:29 +0000411public:
Dan Gohman81975f62007-08-27 14:50:10 +0000412 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000413
Jim Laskey0d086af2006-02-27 12:43:29 +0000414 // Implement isa/cast/dyncast.
415 static bool classof(const DIEInteger *) { return true; }
416 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000417
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000418 /// BestForm - Choose the best form for integer.
419 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000420 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
421 if (IsSigned) {
422 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
423 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
424 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
425 } else {
426 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
427 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
428 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
429 }
430 return DW_FORM_data8;
431 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000432
Jim Laskey0d086af2006-02-27 12:43:29 +0000433 /// EmitValue - Emit integer of appropriate size.
434 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000435 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000436
Jim Laskey0d086af2006-02-27 12:43:29 +0000437 /// SizeOf - Determine size of integer value in bytes.
438 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000439 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000440
Jim Laskeyef42a012006-11-02 20:12:39 +0000441 /// Profile - Used to gather unique data for the value folding set.
442 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000443 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000444 ID.AddInteger(isInteger);
Jim Laskeyef42a012006-11-02 20:12:39 +0000445 ID.AddInteger(Integer);
446 }
Jim Laskey5496f012006-11-09 14:52:14 +0000447 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000448
Jim Laskeyef42a012006-11-02 20:12:39 +0000449#ifndef NDEBUG
450 virtual void print(std::ostream &O) {
451 O << "Int: " << (int64_t)Integer
452 << " 0x" << std::hex << Integer << std::dec;
453 }
454#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000455};
Jim Laskey063e7652006-01-17 17:31:53 +0000456
Jim Laskey0d086af2006-02-27 12:43:29 +0000457//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000458/// DIEString - A string value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000459///
Jim Laskeyef42a012006-11-02 20:12:39 +0000460class DIEString : public DIEValue {
461public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000462 const std::string String;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000463
Dan Gohman81975f62007-08-27 14:50:10 +0000464 explicit DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000465
Jim Laskey0d086af2006-02-27 12:43:29 +0000466 // Implement isa/cast/dyncast.
467 static bool classof(const DIEString *) { return true; }
468 static bool classof(const DIEValue *S) { return S->Type == isString; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000469
Jim Laskey0d086af2006-02-27 12:43:29 +0000470 /// EmitValue - Emit string value.
471 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000472 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000473
Jim Laskey0d086af2006-02-27 12:43:29 +0000474 /// SizeOf - Determine size of string value in bytes.
475 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000476 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000477 return String.size() + sizeof(char); // sizeof('\0');
478 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000479
Jim Laskeyef42a012006-11-02 20:12:39 +0000480 /// Profile - Used to gather unique data for the value folding set.
481 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000482 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000483 ID.AddInteger(isString);
Jim Laskeyef42a012006-11-02 20:12:39 +0000484 ID.AddString(String);
485 }
Jim Laskey5496f012006-11-09 14:52:14 +0000486 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000487
Jim Laskeyef42a012006-11-02 20:12:39 +0000488#ifndef NDEBUG
489 virtual void print(std::ostream &O) {
490 O << "Str: \"" << String << "\"";
491 }
492#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000493};
Jim Laskey063e7652006-01-17 17:31:53 +0000494
Jim Laskey0d086af2006-02-27 12:43:29 +0000495//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000496/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000497//
Jim Laskeyef42a012006-11-02 20:12:39 +0000498class DIEDwarfLabel : public DIEValue {
499public:
500
Jim Laskey0d086af2006-02-27 12:43:29 +0000501 const DWLabel Label;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000502
Dan Gohman81975f62007-08-27 14:50:10 +0000503 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000504
Jim Laskey0d086af2006-02-27 12:43:29 +0000505 // Implement isa/cast/dyncast.
506 static bool classof(const DIEDwarfLabel *) { return true; }
507 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000508
Jim Laskey0d086af2006-02-27 12:43:29 +0000509 /// EmitValue - Emit label value.
510 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000511 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000512
Jim Laskey0d086af2006-02-27 12:43:29 +0000513 /// SizeOf - Determine size of label value in bytes.
514 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000515 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000516
Jim Laskeyef42a012006-11-02 20:12:39 +0000517 /// Profile - Used to gather unique data for the value folding set.
518 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000519 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000520 ID.AddInteger(isLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000521 Label.Profile(ID);
522 }
Jim Laskey5496f012006-11-09 14:52:14 +0000523 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000524
Jim Laskeyef42a012006-11-02 20:12:39 +0000525#ifndef NDEBUG
526 virtual void print(std::ostream &O) {
527 O << "Lbl: ";
528 Label.print(O);
529 }
530#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000531};
Jim Laskey063e7652006-01-17 17:31:53 +0000532
Jim Laskey063e7652006-01-17 17:31:53 +0000533
Jim Laskey0d086af2006-02-27 12:43:29 +0000534//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000535/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000536//
Jim Laskeyef42a012006-11-02 20:12:39 +0000537class DIEObjectLabel : public DIEValue {
538public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000539 const std::string Label;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000540
Dan Gohman81975f62007-08-27 14:50:10 +0000541 explicit DIEObjectLabel(const std::string &L)
542 : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000543
Jim Laskey0d086af2006-02-27 12:43:29 +0000544 // Implement isa/cast/dyncast.
545 static bool classof(const DIEObjectLabel *) { return true; }
546 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000547
Jim Laskey0d086af2006-02-27 12:43:29 +0000548 /// EmitValue - Emit label value.
549 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000550 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000551
Jim Laskey0d086af2006-02-27 12:43:29 +0000552 /// SizeOf - Determine size of label value in bytes.
553 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000554 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000555
Jim Laskeyef42a012006-11-02 20:12:39 +0000556 /// Profile - Used to gather unique data for the value folding set.
557 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000558 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000559 ID.AddInteger(isAsIsLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000560 ID.AddString(Label);
561 }
Jim Laskey5496f012006-11-09 14:52:14 +0000562 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000563
564#ifndef NDEBUG
565 virtual void print(std::ostream &O) {
566 O << "Obj: " << Label;
567 }
568#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000569};
Jim Laskey063e7652006-01-17 17:31:53 +0000570
Jim Laskey0d086af2006-02-27 12:43:29 +0000571//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000572/// DIESectionOffset - A section offset DIE.
573//
574class DIESectionOffset : public DIEValue {
575public:
576 const DWLabel Label;
577 const DWLabel Section;
578 bool IsEH : 1;
579 bool UseSet : 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000580
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000581 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
582 bool isEH = false, bool useSet = true)
583 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
584 IsEH(isEH), UseSet(useSet) {}
585
586 // Implement isa/cast/dyncast.
587 static bool classof(const DIESectionOffset *) { return true; }
588 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000589
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000590 /// EmitValue - Emit section offset.
591 ///
592 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000593
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000594 /// SizeOf - Determine size of section offset value in bytes.
595 ///
596 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000597
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000598 /// Profile - Used to gather unique data for the value folding set.
599 ///
600 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
601 const DWLabel &Section) {
602 ID.AddInteger(isSectionOffset);
603 Label.Profile(ID);
604 Section.Profile(ID);
605 // IsEH and UseSet are specific to the Label/Section that we will emit
606 // the offset for; so Label/Section are enough for uniqueness.
607 }
608 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
609
610#ifndef NDEBUG
611 virtual void print(std::ostream &O) {
612 O << "Off: ";
613 Label.print(O);
614 O << "-";
615 Section.print(O);
616 O << "-" << IsEH << "-" << UseSet;
617 }
618#endif
619};
620
621//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000622/// DIEDelta - A simple label difference DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000623///
Jim Laskeyef42a012006-11-02 20:12:39 +0000624class DIEDelta : public DIEValue {
625public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000626 const DWLabel LabelHi;
627 const DWLabel LabelLo;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000628
Jim Laskey0d086af2006-02-27 12:43:29 +0000629 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
630 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000631
Jim Laskey0d086af2006-02-27 12:43:29 +0000632 // Implement isa/cast/dyncast.
633 static bool classof(const DIEDelta *) { return true; }
634 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000635
Jim Laskey0d086af2006-02-27 12:43:29 +0000636 /// EmitValue - Emit delta value.
637 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000638 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000639
Jim Laskey0d086af2006-02-27 12:43:29 +0000640 /// SizeOf - Determine size of delta value in bytes.
641 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000642 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000643
Jim Laskeyef42a012006-11-02 20:12:39 +0000644 /// Profile - Used to gather unique data for the value folding set.
645 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000646 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
647 const DWLabel &LabelLo) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000648 ID.AddInteger(isDelta);
Jim Laskeyef42a012006-11-02 20:12:39 +0000649 LabelHi.Profile(ID);
650 LabelLo.Profile(ID);
651 }
Jim Laskey5496f012006-11-09 14:52:14 +0000652 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000653
654#ifndef NDEBUG
655 virtual void print(std::ostream &O) {
656 O << "Del: ";
657 LabelHi.print(O);
658 O << "-";
659 LabelLo.print(O);
660 }
661#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000662};
Jim Laskey063e7652006-01-17 17:31:53 +0000663
Jim Laskey0d086af2006-02-27 12:43:29 +0000664//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000665/// DIEntry - A pointer to another debug information entry. An instance of this
666/// class can also be used as a proxy for a debug information entry not yet
667/// defined (ie. types.)
668class DIEntry : public DIEValue {
669public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000670 DIE *Entry;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000671
Dan Gohman81975f62007-08-27 14:50:10 +0000672 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000673
Jim Laskey0d086af2006-02-27 12:43:29 +0000674 // Implement isa/cast/dyncast.
675 static bool classof(const DIEntry *) { return true; }
676 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000677
Jim Laskeyb8509c52006-03-23 18:07:55 +0000678 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000679 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000680 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000681
Jim Laskeyb8509c52006-03-23 18:07:55 +0000682 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000683 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000684 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000685 return sizeof(int32_t);
686 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000687
Jim Laskeyef42a012006-11-02 20:12:39 +0000688 /// Profile - Used to gather unique data for the value folding set.
689 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000690 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
691 ID.AddInteger(isEntry);
692 ID.AddPointer(Entry);
693 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000694 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000695 ID.AddInteger(isEntry);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000696
Jim Laskeyef42a012006-11-02 20:12:39 +0000697 if (Entry) {
698 ID.AddPointer(Entry);
699 } else {
700 ID.AddPointer(this);
701 }
702 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000703
Jim Laskeyef42a012006-11-02 20:12:39 +0000704#ifndef NDEBUG
705 virtual void print(std::ostream &O) {
706 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
707 }
708#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000709};
710
711//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000712/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000713//
Jim Laskeyef42a012006-11-02 20:12:39 +0000714class DIEBlock : public DIEValue, public DIE {
715public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000716 unsigned Size; // Size in bytes excluding size header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000717
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000718 DIEBlock()
719 : DIEValue(isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +0000720 , DIE(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000721 , Size(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000722 {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000723 ~DIEBlock() {
724 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000725
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000726 // Implement isa/cast/dyncast.
727 static bool classof(const DIEBlock *) { return true; }
728 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000729
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000730 /// ComputeSize - calculate the size of the block.
731 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000732 unsigned ComputeSize(DwarfDebug &DD);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000733
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000734 /// BestForm - Choose the best form for data.
735 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000736 unsigned BestForm() const {
737 if ((unsigned char)Size == Size) return DW_FORM_block1;
738 if ((unsigned short)Size == Size) return DW_FORM_block2;
739 if ((unsigned int)Size == Size) return DW_FORM_block4;
740 return DW_FORM_block;
741 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000742
743 /// EmitValue - Emit block data.
744 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000745 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000746
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000747 /// SizeOf - Determine size of block data in bytes.
748 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000749 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000750
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000751
Jim Laskeyef42a012006-11-02 20:12:39 +0000752 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000753 ///
Reid Spencer97821312006-11-02 23:56:21 +0000754 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000755 ID.AddInteger(isBlock);
Jim Laskeyef42a012006-11-02 20:12:39 +0000756 DIE::Profile(ID);
757 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000758
Jim Laskeyef42a012006-11-02 20:12:39 +0000759#ifndef NDEBUG
760 virtual void print(std::ostream &O) {
761 O << "Blk: ";
762 DIE::print(O, 5);
763 }
764#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000765};
766
767//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000768/// CompileUnit - This dwarf writer support class manages information associate
769/// with a source file.
770class CompileUnit {
Jim Laskey0d086af2006-02-27 12:43:29 +0000771private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000772 /// ID - File identifier for source.
773 ///
774 unsigned ID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000775
Jim Laskeyef42a012006-11-02 20:12:39 +0000776 /// Die - Compile unit debug information entry.
777 ///
778 DIE *Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000779
Devang Patelbbdc8202009-01-13 23:54:55 +0000780 /// GVToDieMap - Tracks the mapping of unit level debug informaton
781 /// variables to debug information entries.
Devang Patel48d190f2009-01-05 21:47:57 +0000782 DenseMap<GlobalVariable *, DIE *> GVToDieMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000783
Devang Patelbbdc8202009-01-13 23:54:55 +0000784 /// GVToDIEntryMap - Tracks the mapping of unit level debug informaton
Jim Laskeyef42a012006-11-02 20:12:39 +0000785 /// descriptors to debug information entries using a DIEntry proxy.
Devang Patel48d190f2009-01-05 21:47:57 +0000786 DenseMap<GlobalVariable *, DIEntry *> GVToDIEntryMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000787
788 /// Globals - A map of globally visible named entities for this unit.
789 ///
790 std::map<std::string, DIE *> Globals;
791
792 /// DiesSet - Used to uniquely define dies within the compile unit.
793 ///
794 FoldingSet<DIE> DiesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000795
Jim Laskeyef42a012006-11-02 20:12:39 +0000796 /// Dies - List of all dies in the compile unit.
797 ///
798 std::vector<DIE *> Dies;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000799
Jim Laskey0d086af2006-02-27 12:43:29 +0000800public:
Devang Pateld1ca9252009-01-05 23:03:32 +0000801 CompileUnit(unsigned I, DIE *D)
Devang Patelbbdc8202009-01-13 23:54:55 +0000802 : ID(I), Die(D), GVToDieMap(),
Devang Pateld1ca9252009-01-05 23:03:32 +0000803 GVToDIEntryMap(), Globals(), DiesSet(InitDiesSetSize), Dies()
804 {}
805
Jim Laskeyef42a012006-11-02 20:12:39 +0000806 ~CompileUnit() {
807 delete Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000808
Jim Laskeyef42a012006-11-02 20:12:39 +0000809 for (unsigned i = 0, N = Dies.size(); i < N; ++i)
810 delete Dies[i];
811 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000812
Jim Laskeybd761842006-02-27 17:27:12 +0000813 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000814 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
Devang Patelbbdc8202009-01-13 23:54:55 +0000831 /// specified debug variable.
Devang Patel48d190f2009-01-05 21:47:57 +0000832 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
833 return GVToDieMap[GV];
834 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000835
Jim Laskeyef42a012006-11-02 20:12:39 +0000836 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
Devang Patelbbdc8202009-01-13 23:54:55 +0000837 /// specified debug variable.
Devang Patel48d190f2009-01-05 21:47:57 +0000838 DIEntry *&getDIEntrySlotFor(GlobalVariable *GV) {
839 return GVToDIEntryMap[GV];
840 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000841
Jim Laskeyef42a012006-11-02 20:12:39 +0000842 /// AddDie - Adds or interns the DIE to the compile unit.
843 ///
844 DIE *AddDie(DIE &Buffer) {
845 FoldingSetNodeID ID;
846 Buffer.Profile(ID);
847 void *Where;
848 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000849
Jim Laskeyef42a012006-11-02 20:12:39 +0000850 if (!Die) {
851 Die = new DIE(Buffer);
852 DiesSet.InsertNode(Die, Where);
853 this->Die->AddChild(Die);
854 Buffer.Detach();
855 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000856
Jim Laskeyef42a012006-11-02 20:12:39 +0000857 return Die;
858 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000859};
860
Jim Laskey65195462006-10-30 13:35:07 +0000861//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000862/// Dwarf - Emits general Dwarf directives.
Jim Laskeyef42a012006-11-02 20:12:39 +0000863///
Jim Laskey65195462006-10-30 13:35:07 +0000864class Dwarf {
865
Jim Laskey072200c2007-01-29 18:51:14 +0000866protected:
Jim Laskey65195462006-10-30 13:35:07 +0000867
868 //===--------------------------------------------------------------------===//
Jim Laskey072200c2007-01-29 18:51:14 +0000869 // Core attributes used by the Dwarf writer.
Jim Laskey65195462006-10-30 13:35:07 +0000870 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000871
Jim Laskey65195462006-10-30 13:35:07 +0000872 //
873 /// O - Stream to .s file.
874 ///
Owen Andersoncb371882008-08-21 00:14:44 +0000875 raw_ostream &O;
Jim Laskey65195462006-10-30 13:35:07 +0000876
877 /// Asm - Target of Dwarf emission.
878 ///
879 AsmPrinter *Asm;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000880
Bill Wendlingaa8f8882008-07-01 23:34:48 +0000881 /// TAI - Target asm information.
Jim Laskey65195462006-10-30 13:35:07 +0000882 const TargetAsmInfo *TAI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000883
Jim Laskey65195462006-10-30 13:35:07 +0000884 /// TD - Target data.
885 const TargetData *TD;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000886
Jim Laskey65195462006-10-30 13:35:07 +0000887 /// RI - Register Information.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000888 const TargetRegisterInfo *RI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000889
Jim Laskey65195462006-10-30 13:35:07 +0000890 /// M - Current module.
891 ///
892 Module *M;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000893
Jim Laskey65195462006-10-30 13:35:07 +0000894 /// MF - Current machine function.
895 ///
896 MachineFunction *MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000897
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000898 /// MMI - Collected machine module information.
Jim Laskey65195462006-10-30 13:35:07 +0000899 ///
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000900 MachineModuleInfo *MMI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000901
Jim Laskey65195462006-10-30 13:35:07 +0000902 /// SubprogramCount - The running count of functions being compiled.
903 ///
904 unsigned SubprogramCount;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000905
Chris Lattner9251f132007-09-24 03:35:37 +0000906 /// Flavor - A unique string indicating what dwarf producer this is, used to
907 /// unique labels.
908 const char * const Flavor;
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000909
910 unsigned SetCounter;
Owen Andersoncb371882008-08-21 00:14:44 +0000911 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattner9251f132007-09-24 03:35:37 +0000912 const char *flavor)
Jim Laskey072200c2007-01-29 18:51:14 +0000913 : O(OS)
914 , Asm(A)
915 , TAI(T)
916 , TD(Asm->TM.getTargetData())
917 , RI(Asm->TM.getRegisterInfo())
918 , M(NULL)
919 , MF(NULL)
920 , MMI(NULL)
Jim Laskey072200c2007-01-29 18:51:14 +0000921 , SubprogramCount(0)
Chris Lattner9251f132007-09-24 03:35:37 +0000922 , Flavor(flavor)
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000923 , SetCounter(1)
Jim Laskey072200c2007-01-29 18:51:14 +0000924 {
925 }
926
927public:
928
929 //===--------------------------------------------------------------------===//
930 // Accessors.
931 //
932 AsmPrinter *getAsm() const { return Asm; }
Jim Laskeyb82313f2007-02-01 16:31:34 +0000933 MachineModuleInfo *getMMI() const { return MMI; }
Jim Laskey072200c2007-01-29 18:51:14 +0000934 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohman82482942007-09-27 23:12:31 +0000935 const TargetData *getTargetData() const { return TD; }
Jim Laskey072200c2007-01-29 18:51:14 +0000936
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000937 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
938 const {
939 if (isInSection && TAI->getDwarfSectionOffsetDirective())
940 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohman82482942007-09-27 23:12:31 +0000941 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000942 O << TAI->getData32bitsDirective();
943 else
944 O << TAI->getData64bitsDirective();
945 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000946
Jim Laskeyb82313f2007-02-01 16:31:34 +0000947 /// PrintLabelName - Print label name in form used by Dwarf writer.
948 ///
949 void PrintLabelName(DWLabel Label) const {
950 PrintLabelName(Label.Tag, Label.Number);
951 }
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000952 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000953 O << TAI->getPrivateGlobalPrefix() << Tag;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000954 if (Number) O << Number;
955 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000956
Chris Lattner9251f132007-09-24 03:35:37 +0000957 void PrintLabelName(const char *Tag, unsigned Number,
958 const char *Suffix) const {
959 O << TAI->getPrivateGlobalPrefix() << Tag;
960 if (Number) O << Number;
961 O << Suffix;
962 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000963
Jim Laskeyb82313f2007-02-01 16:31:34 +0000964 /// EmitLabel - Emit location label for internal use by Dwarf.
965 ///
966 void EmitLabel(DWLabel Label) const {
967 EmitLabel(Label.Tag, Label.Number);
968 }
969 void EmitLabel(const char *Tag, unsigned Number) const {
970 PrintLabelName(Tag, Number);
971 O << ":\n";
972 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000973
Jim Laskeyb82313f2007-02-01 16:31:34 +0000974 /// EmitReference - Emit a reference to a label.
975 ///
Dan Gohman06ff4e62007-09-28 15:43:33 +0000976 void EmitReference(DWLabel Label, bool IsPCRelative = false,
977 bool Force32Bit = false) const {
978 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000979 }
980 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman06ff4e62007-09-28 15:43:33 +0000981 bool IsPCRelative = false, bool Force32Bit = false) const {
982 PrintRelDirective(Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000983 PrintLabelName(Tag, Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000984
Jim Laskeyb82313f2007-02-01 16:31:34 +0000985 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
986 }
Dan Gohman06ff4e62007-09-28 15:43:33 +0000987 void EmitReference(const std::string &Name, bool IsPCRelative = false,
988 bool Force32Bit = false) const {
989 PrintRelDirective(Force32Bit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000990
Jim Laskeyb82313f2007-02-01 16:31:34 +0000991 O << Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000992
Jim Laskeyb82313f2007-02-01 16:31:34 +0000993 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
994 }
995
996 /// EmitDifference - Emit the difference between two labels. Some
997 /// assemblers do not behave with absolute expressions with data directives,
998 /// so there is an option (needsSet) to use an intermediary set expression.
999 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +00001000 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001001 EmitDifference(LabelHi.Tag, LabelHi.Number,
1002 LabelLo.Tag, LabelLo.Number,
1003 IsSmall);
1004 }
1005 void EmitDifference(const char *TagHi, unsigned NumberHi,
1006 const char *TagLo, unsigned NumberLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +00001007 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001008 if (TAI->needsSet()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001009 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +00001010 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001011 O << ",";
1012 PrintLabelName(TagHi, NumberHi);
1013 O << "-";
1014 PrintLabelName(TagLo, NumberLo);
1015 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001016
1017 PrintRelDirective(IsSmall);
Chris Lattner9251f132007-09-24 03:35:37 +00001018 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001019 ++SetCounter;
1020 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001021 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001022
Jim Laskeyb82313f2007-02-01 16:31:34 +00001023 PrintLabelName(TagHi, NumberHi);
1024 O << "-";
1025 PrintLabelName(TagLo, NumberLo);
1026 }
1027 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001028
1029 void EmitSectionOffset(const char* Label, const char* Section,
1030 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesend9ffd4c2008-03-26 23:31:39 +00001031 bool IsSmall = false, bool isEH = false,
1032 bool useSet = true) {
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001033 bool printAbsolute = false;
Dale Johannesend9ffd4c2008-03-26 23:31:39 +00001034 if (isEH)
1035 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
1036 else
1037 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
1038
1039 if (TAI->needsSet() && useSet) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001040 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +00001041 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001042 O << ",";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001043 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001044
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001045 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001046 O << "-";
1047 PrintLabelName(Section, SectionNumber);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001048 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001049 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001050
1051 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001052
Chris Lattner9251f132007-09-24 03:35:37 +00001053 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001054 ++SetCounter;
1055 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001056 PrintRelDirective(IsSmall, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001057
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001058 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001059
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001060 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001061 O << "-";
1062 PrintLabelName(Section, SectionNumber);
1063 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001064 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001065 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001066
Jim Laskeyb82313f2007-02-01 16:31:34 +00001067 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1068 /// frame.
1069 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenb97aec62007-11-13 19:13:01 +00001070 const std::vector<MachineMove> &Moves, bool isEH) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001071 int stackGrowth =
1072 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1073 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00001074 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeybacd3042007-02-21 22:48:45 +00001075 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
Jim Laskeyb82313f2007-02-01 16:31:34 +00001076
1077 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001078 const MachineMove &Move = Moves[i];
Jim Laskeyb82313f2007-02-01 16:31:34 +00001079 unsigned LabelID = Move.getLabelID();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001080
Jim Laskeyb82313f2007-02-01 16:31:34 +00001081 if (LabelID) {
1082 LabelID = MMI->MappedLabel(LabelID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001083
Jim Laskeyb82313f2007-02-01 16:31:34 +00001084 // Throw out move if the label is invalid.
1085 if (!LabelID) continue;
1086 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001087
Jim Laskeyb82313f2007-02-01 16:31:34 +00001088 const MachineLocation &Dst = Move.getDestination();
1089 const MachineLocation &Src = Move.getSource();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001090
Jim Laskeyb82313f2007-02-01 16:31:34 +00001091 // Advance row if new location.
1092 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1093 Asm->EmitInt8(DW_CFA_advance_loc4);
1094 Asm->EOL("DW_CFA_advance_loc4");
Jim Laskeybacd3042007-02-21 22:48:45 +00001095 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1096 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001097
Jim Laskeyb82313f2007-02-01 16:31:34 +00001098 BaseLabelID = LabelID;
Jim Laskeybacd3042007-02-21 22:48:45 +00001099 BaseLabel = "label";
Jim Laskeyb82313f2007-02-01 16:31:34 +00001100 IsLocal = true;
1101 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001102
Jim Laskeyb82313f2007-02-01 16:31:34 +00001103 // If advancing cfa.
Dan Gohmand735b802008-10-03 15:45:36 +00001104 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1105 if (!Src.isReg()) {
1106 if (Src.getReg() == MachineLocation::VirtualFP) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001107 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1108 Asm->EOL("DW_CFA_def_cfa_offset");
1109 } else {
1110 Asm->EmitInt8(DW_CFA_def_cfa);
1111 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmand735b802008-10-03 15:45:36 +00001112 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001113 Asm->EOL("Register");
1114 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001115
Jim Laskeyb82313f2007-02-01 16:31:34 +00001116 int Offset = -Src.getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001117
Jim Laskeyb82313f2007-02-01 16:31:34 +00001118 Asm->EmitULEB128Bytes(Offset);
1119 Asm->EOL("Offset");
1120 } else {
1121 assert(0 && "Machine move no supported yet.");
1122 }
Dan Gohmand735b802008-10-03 15:45:36 +00001123 } else if (Src.isReg() &&
1124 Src.getReg() == MachineLocation::VirtualFP) {
1125 if (Dst.isReg()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001126 Asm->EmitInt8(DW_CFA_def_cfa_register);
1127 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmand735b802008-10-03 15:45:36 +00001128 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001129 Asm->EOL("Register");
1130 } else {
1131 assert(0 && "Machine move no supported yet.");
1132 }
1133 } else {
Dan Gohmand735b802008-10-03 15:45:36 +00001134 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001135 int Offset = Dst.getOffset() / stackGrowth;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001136
Jim Laskeyb82313f2007-02-01 16:31:34 +00001137 if (Offset < 0) {
1138 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1139 Asm->EOL("DW_CFA_offset_extended_sf");
1140 Asm->EmitULEB128Bytes(Reg);
1141 Asm->EOL("Reg");
1142 Asm->EmitSLEB128Bytes(Offset);
1143 Asm->EOL("Offset");
1144 } else if (Reg < 64) {
1145 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Chengd4114192008-07-09 21:53:02 +00001146 if (VerboseAsm)
1147 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1148 else
1149 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00001150 Asm->EmitULEB128Bytes(Offset);
1151 Asm->EOL("Offset");
1152 } else {
1153 Asm->EmitInt8(DW_CFA_offset_extended);
1154 Asm->EOL("DW_CFA_offset_extended");
1155 Asm->EmitULEB128Bytes(Reg);
1156 Asm->EOL("Reg");
1157 Asm->EmitULEB128Bytes(Offset);
1158 Asm->EOL("Offset");
1159 }
1160 }
1161 }
1162 }
1163
Jim Laskey072200c2007-01-29 18:51:14 +00001164};
1165
1166//===----------------------------------------------------------------------===//
Devang Patelf3ee5142009-01-12 22:54:42 +00001167/// SrcLineInfo - This class is used to record source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001168///
1169class SrcLineInfo {
1170 unsigned Line; // Source line number.
1171 unsigned Column; // Source column.
1172 unsigned SourceID; // Source ID number.
1173 unsigned LabelID; // Label in code ID number.
1174public:
1175 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
1176 : Line(L), Column(C), SourceID(S), LabelID(I) {}
1177
1178 // Accessors
1179 unsigned getLine() const { return Line; }
1180 unsigned getColumn() const { return Column; }
1181 unsigned getSourceID() const { return SourceID; }
1182 unsigned getLabelID() const { return LabelID; }
1183};
1184
1185
1186//===----------------------------------------------------------------------===//
Devang Patel8526cc02009-01-05 22:35:52 +00001187/// SrcFileInfo - This class is used to track source information.
1188///
1189class SrcFileInfo {
1190 unsigned DirectoryID; // Directory ID number.
1191 std::string Name; // File name (not including directory.)
1192public:
1193 SrcFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
1194
1195 // Accessors
1196 unsigned getDirectoryID() const { return DirectoryID; }
1197 const std::string &getName() const { return Name; }
1198
1199 /// operator== - Used by UniqueVector to locate entry.
1200 ///
Devang Patelbbdc8202009-01-13 23:54:55 +00001201 bool operator==(const SrcFileInfo &SI) const {
Devang Patel8526cc02009-01-05 22:35:52 +00001202 return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
1203 }
1204
1205 /// operator< - Used by UniqueVector to locate entry.
1206 ///
1207 bool operator<(const SrcFileInfo &SI) const {
1208 return getDirectoryID() < SI.getDirectoryID() ||
1209 (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
1210 }
1211};
1212
1213//===----------------------------------------------------------------------===//
Devang Patel7a6e5a32009-01-08 02:33:41 +00001214/// DbgVariable - This class is used to track local variable information.
1215///
1216class DbgVariable {
1217private:
1218 DIVariable *Var; // Variable Descriptor.
1219 unsigned FrameIndex; // Variable frame index.
1220
1221public:
1222 DbgVariable(DIVariable *V, unsigned I) : Var(V), FrameIndex(I) {}
1223
1224 // Accessors.
1225 DIVariable *getVariable() const { return Var; }
1226 unsigned getFrameIndex() const { return FrameIndex; }
1227};
1228
1229//===----------------------------------------------------------------------===//
1230/// DbgScope - This class is used to track scope information.
1231///
1232class DbgScope {
1233private:
1234 DbgScope *Parent; // Parent to this scope.
Devang Patel0e5200f2009-01-15 18:25:17 +00001235 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001236 // Either subprogram or block.
1237 unsigned StartLabelID; // Label ID of the beginning of scope.
1238 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel9795da52009-01-10 02:42:49 +00001239 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
1240 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001241
1242public:
Devang Patel0e5200f2009-01-15 18:25:17 +00001243 DbgScope(DbgScope *P, DIDescriptor D)
Devang Patel7a6e5a32009-01-08 02:33:41 +00001244 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), Scopes(), Variables()
1245 {}
Devang Patel481ff5b2009-01-12 18:48:36 +00001246 ~DbgScope() {
1247 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1248 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1249 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001250
1251 // Accessors.
1252 DbgScope *getParent() const { return Parent; }
Devang Patel0e5200f2009-01-15 18:25:17 +00001253 DIDescriptor getDesc() const { return Desc; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001254 unsigned getStartLabelID() const { return StartLabelID; }
1255 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel9795da52009-01-10 02:42:49 +00001256 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1257 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001258 void setStartLabelID(unsigned S) { StartLabelID = S; }
1259 void setEndLabelID(unsigned E) { EndLabelID = E; }
1260
1261 /// AddScope - Add a scope to the scope.
1262 ///
1263 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1264
1265 /// AddVariable - Add a variable to the scope.
1266 ///
1267 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
1268};
1269
1270//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001271/// DwarfDebug - Emits Dwarf debug directives.
Jim Laskey072200c2007-01-29 18:51:14 +00001272///
1273class DwarfDebug : public Dwarf {
1274
1275private:
Jim Laskey65195462006-10-30 13:35:07 +00001276 //===--------------------------------------------------------------------===//
1277 // Attributes used to construct specific Dwarf sections.
1278 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001279
Jim Laskey65195462006-10-30 13:35:07 +00001280 /// CompileUnits - All the compile units involved in this build. The index
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001281 /// of each entry in this vector corresponds to the sources in MMI.
Jim Laskey65195462006-10-30 13:35:07 +00001282 std::vector<CompileUnit *> CompileUnits;
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001283 DenseMap<Value *, CompileUnit *> DW_CUs;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001284
Jim Laskeyef42a012006-11-02 20:12:39 +00001285 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +00001286 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001287 FoldingSet<DIEAbbrev> AbbreviationsSet;
1288
1289 /// Abbreviations - A list of all the unique abbreviations in use.
1290 ///
1291 std::vector<DIEAbbrev *> Abbreviations;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001292
Jim Laskeyef42a012006-11-02 20:12:39 +00001293 /// ValuesSet - Used to uniquely define values.
1294 ///
Devang Patel8526cc02009-01-05 22:35:52 +00001295 // Directories - Uniquing vector for directories.
1296 UniqueVector<std::string> Directories;
1297
1298 // SourceFiles - Uniquing vector for source files.
1299 UniqueVector<SrcFileInfo> SrcFiles;
1300
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001301 // Lines - List of of source line correspondence.
1302 std::vector<SrcLineInfo> Lines;
1303
Jim Laskeyef42a012006-11-02 20:12:39 +00001304 FoldingSet<DIEValue> ValuesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001305
Jim Laskeyef42a012006-11-02 20:12:39 +00001306 /// Values - A list of all the unique values in use.
1307 ///
1308 std::vector<DIEValue *> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001309
Jim Laskey65195462006-10-30 13:35:07 +00001310 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +00001311 ///
Jim Laskey65195462006-10-30 13:35:07 +00001312 UniqueVector<std::string> StringPool;
1313
Jim Laskey65195462006-10-30 13:35:07 +00001314 /// SectionMap - Provides a unique id per text section.
1315 ///
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001316 UniqueVector<const Section*> SectionMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001317
Jim Laskey65195462006-10-30 13:35:07 +00001318 /// SectionSourceLines - Tracks line numbers per text section.
1319 ///
Devang Patelf3ee5142009-01-12 22:54:42 +00001320 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Jim Laskey65195462006-10-30 13:35:07 +00001321
Jim Laskeybacd3042007-02-21 22:48:45 +00001322 /// didInitial - Flag to indicate if initial emission has been done.
1323 ///
1324 bool didInitial;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001325
Jim Laskeybacd3042007-02-21 22:48:45 +00001326 /// shouldEmit - Flag to indicate if debug information should be emitted.
1327 ///
1328 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +00001329
Devang Patel0e5200f2009-01-15 18:25:17 +00001330 // RootDbgScope - Top level scope for the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001331 //
1332 DbgScope *RootDbgScope;
1333
1334 // DbgScopeMap - Tracks the scopes in the current function.
1335 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
1336
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001337 struct FunctionDebugFrameInfo {
1338 unsigned Number;
1339 std::vector<MachineMove> Moves;
1340
1341 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001342 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001343 };
1344
1345 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001346
Jim Laskey65195462006-10-30 13:35:07 +00001347public:
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001348
Jim Laskeybacd3042007-02-21 22:48:45 +00001349 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1350 ///
1351 bool ShouldEmitDwarf() const { return shouldEmit; }
Jim Laskey65195462006-10-30 13:35:07 +00001352
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001353 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001354 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001355 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1356 // Profile the node so that we can make it unique.
1357 FoldingSetNodeID ID;
1358 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001359
Jim Laskeyef42a012006-11-02 20:12:39 +00001360 // Check the set for priors.
1361 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001362
Jim Laskeyef42a012006-11-02 20:12:39 +00001363 // If it's newly added.
1364 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001365 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +00001366 Abbreviations.push_back(&Abbrev);
1367 // Assign the vector position + 1 as its number.
1368 Abbrev.setNumber(Abbreviations.size());
1369 } else {
1370 // Assign existing abbreviation number.
1371 Abbrev.setNumber(InSet->getNumber());
1372 }
1373 }
1374
Jim Laskey65195462006-10-30 13:35:07 +00001375 /// NewString - Add a string to the constant pool and returns a label.
1376 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001377 DWLabel NewString(const std::string &String) {
1378 unsigned StringID = StringPool.insert(String);
1379 return DWLabel("string", StringID);
1380 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001381
Jim Laskeyef42a012006-11-02 20:12:39 +00001382 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1383 /// entry.
1384 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1385 DIEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001386
Jim Laskeyef42a012006-11-02 20:12:39 +00001387 if (Entry) {
1388 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001389 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001390 void *Where;
1391 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001392
Jim Laskeyf6733882006-11-02 21:48:18 +00001393 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001394
Jim Laskeyef42a012006-11-02 20:12:39 +00001395 Value = new DIEntry(Entry);
1396 ValuesSet.InsertNode(Value, Where);
1397 } else {
1398 Value = new DIEntry(Entry);
1399 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001400
Jim Laskeyef42a012006-11-02 20:12:39 +00001401 Values.push_back(Value);
1402 return Value;
1403 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001404
Jim Laskeyef42a012006-11-02 20:12:39 +00001405 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1406 ///
1407 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1408 Value->Entry = Entry;
1409 // Add to values set if not already there. If it is, we merely have a
1410 // duplicate in the values list (no harm.)
1411 ValuesSet.GetOrInsertNode(Value);
1412 }
1413
1414 /// AddUInt - Add an unsigned integer attribute data and value.
1415 ///
1416 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1417 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1418
1419 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001420 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001421 void *Where;
1422 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1423 if (!Value) {
1424 Value = new DIEInteger(Integer);
1425 ValuesSet.InsertNode(Value, Where);
1426 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001427 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001428
Jim Laskeyef42a012006-11-02 20:12:39 +00001429 Die->AddValue(Attribute, Form, Value);
1430 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001431
Jim Laskeyef42a012006-11-02 20:12:39 +00001432 /// AddSInt - Add an signed integer attribute data and value.
1433 ///
1434 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1435 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1436
1437 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001438 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001439 void *Where;
1440 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1441 if (!Value) {
1442 Value = new DIEInteger(Integer);
1443 ValuesSet.InsertNode(Value, Where);
1444 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001445 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001446
Jim Laskeyef42a012006-11-02 20:12:39 +00001447 Die->AddValue(Attribute, Form, Value);
1448 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001449
Jim Laskeyef42a012006-11-02 20:12:39 +00001450 /// AddString - Add a std::string attribute data and value.
1451 ///
1452 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1453 const std::string &String) {
1454 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001455 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001456 void *Where;
1457 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1458 if (!Value) {
1459 Value = new DIEString(String);
1460 ValuesSet.InsertNode(Value, Where);
1461 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001462 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001463
Jim Laskeyef42a012006-11-02 20:12:39 +00001464 Die->AddValue(Attribute, Form, Value);
1465 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001466
Jim Laskeyef42a012006-11-02 20:12:39 +00001467 /// AddLabel - Add a Dwarf label attribute data and value.
1468 ///
1469 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1470 const DWLabel &Label) {
1471 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001472 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001473 void *Where;
1474 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1475 if (!Value) {
1476 Value = new DIEDwarfLabel(Label);
1477 ValuesSet.InsertNode(Value, Where);
1478 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001479 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001480
Jim Laskeyef42a012006-11-02 20:12:39 +00001481 Die->AddValue(Attribute, Form, Value);
1482 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001483
Jim Laskeyef42a012006-11-02 20:12:39 +00001484 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1485 ///
1486 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1487 const std::string &Label) {
1488 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001489 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001490 void *Where;
1491 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1492 if (!Value) {
1493 Value = new DIEObjectLabel(Label);
1494 ValuesSet.InsertNode(Value, Where);
1495 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001496 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001497
Jim Laskeyef42a012006-11-02 20:12:39 +00001498 Die->AddValue(Attribute, Form, Value);
1499 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001500
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001501 /// AddSectionOffset - Add a section offset label attribute data and value.
1502 ///
1503 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1504 const DWLabel &Label, const DWLabel &Section,
1505 bool isEH = false, bool useSet = true) {
1506 FoldingSetNodeID ID;
1507 DIESectionOffset::Profile(ID, Label, Section);
1508 void *Where;
1509 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1510 if (!Value) {
1511 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1512 ValuesSet.InsertNode(Value, Where);
1513 Values.push_back(Value);
1514 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001515
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001516 Die->AddValue(Attribute, Form, Value);
1517 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001518
Jim Laskeyef42a012006-11-02 20:12:39 +00001519 /// AddDelta - Add a label delta attribute data and value.
1520 ///
1521 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1522 const DWLabel &Hi, const DWLabel &Lo) {
1523 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001524 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001525 void *Where;
1526 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1527 if (!Value) {
1528 Value = new DIEDelta(Hi, Lo);
1529 ValuesSet.InsertNode(Value, Where);
1530 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001531 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001532
Jim Laskeyef42a012006-11-02 20:12:39 +00001533 Die->AddValue(Attribute, Form, Value);
1534 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001535
Jim Laskeyef42a012006-11-02 20:12:39 +00001536 /// AddDIEntry - Add a DIE attribute data and value.
1537 ///
1538 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1539 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1540 }
1541
1542 /// AddBlock - Add block data.
1543 ///
1544 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1545 Block->ComputeSize(*this);
1546 FoldingSetNodeID ID;
1547 Block->Profile(ID);
1548 void *Where;
1549 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1550 if (!Value) {
1551 Value = Block;
1552 ValuesSet.InsertNode(Value, Where);
1553 Values.push_back(Value);
1554 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001555 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001556 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001557 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001558 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001559
Jim Laskeyef42a012006-11-02 20:12:39 +00001560 Die->AddValue(Attribute, Block->BestForm(), Value);
1561 }
1562
Jim Laskey65195462006-10-30 13:35:07 +00001563private:
1564
1565 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001566 /// entry.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001567 void AddSourceLine(DIE *Die, DIVariable *V) {
1568 unsigned FileID = 0;
1569 unsigned Line = V->getLineNumber();
1570 if (V->getVersion() < DIDescriptor::Version7) {
1571 // Version6 or earlier. Use compile unit info to get file id.
1572 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1573 FileID = Unit->getID();
1574 } else {
1575 // Version7 or newer, use filename and directory info from DIVariable
1576 // directly.
1577 unsigned DID = Directories.idFor(V->getDirectory());
1578 FileID = SrcFiles.idFor(SrcFileInfo(DID, V->getFilename()));
1579 }
1580 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1581 AddUInt(Die, DW_AT_decl_line, 0, Line);
1582 }
1583
1584 /// AddSourceLine - Add location information to specified debug information
1585 /// entry.
Devang Patel8526cc02009-01-05 22:35:52 +00001586 void AddSourceLine(DIE *Die, DIGlobal *G) {
1587 unsigned FileID = 0;
1588 unsigned Line = G->getLineNumber();
1589 if (G->getVersion() < DIDescriptor::Version7) {
1590 // Version6 or earlier. Use compile unit info to get file id.
1591 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1592 FileID = Unit->getID();
1593 } else {
1594 // Version7 or newer, use filename and directory info from DIGlobal
1595 // directly.
1596 unsigned DID = Directories.idFor(G->getDirectory());
1597 FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1598 }
1599 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1600 AddUInt(Die, DW_AT_decl_line, 0, Line);
1601 }
1602
1603 void AddSourceLine(DIE *Die, DIType *G) {
1604 unsigned FileID = 0;
1605 unsigned Line = G->getLineNumber();
1606 if (G->getVersion() < DIDescriptor::Version7) {
1607 // Version6 or earlier. Use compile unit info to get file id.
1608 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1609 FileID = Unit->getID();
1610 } else {
1611 // Version7 or newer, use filename and directory info from DIGlobal
1612 // directly.
1613 unsigned DID = Directories.idFor(G->getDirectory());
1614 FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1615 }
1616 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1617 AddUInt(Die, DW_AT_decl_line, 0, Line);
1618 }
1619
Jim Laskey65195462006-10-30 13:35:07 +00001620 /// AddAddress - Add an address attribute to a die based on the location
1621 /// provided.
1622 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001623 const MachineLocation &Location) {
Dan Gohmand735b802008-10-03 15:45:36 +00001624 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001625 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001626
Dan Gohmand735b802008-10-03 15:45:36 +00001627 if (Location.isReg()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001628 if (Reg < 32) {
1629 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1630 } else {
1631 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1632 AddUInt(Block, 0, DW_FORM_udata, Reg);
1633 }
1634 } else {
1635 if (Reg < 32) {
1636 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1637 } else {
1638 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1639 AddUInt(Block, 0, DW_FORM_udata, Reg);
1640 }
1641 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1642 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001643
Jim Laskeyef42a012006-11-02 20:12:39 +00001644 AddBlock(Die, Attribute, 0, Block);
1645 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001646
Jim Laskeyef42a012006-11-02 20:12:39 +00001647 /// AddBasicType - Add a new basic type attribute to the specified entity.
1648 ///
1649 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1650 const std::string &Name,
1651 unsigned Encoding, unsigned Size) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001652
Jim Laskeyef42a012006-11-02 20:12:39 +00001653 DIE Buffer(DW_TAG_base_type);
1654 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1655 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1656 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel9ede3f22009-01-05 17:44:11 +00001657 DIE *BasicTypeDie = Unit->AddDie(Buffer);
1658 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, BasicTypeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001659 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001660
Jim Laskeyef42a012006-11-02 20:12:39 +00001661 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1662 ///
1663 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001664 DIE Buffer(DW_TAG_pointer_type);
Dan Gohman82482942007-09-27 23:12:31 +00001665 AddUInt(&Buffer, DW_AT_byte_size, 0, TD->getPointerSize());
Jim Laskeyef42a012006-11-02 20:12:39 +00001666 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelca03c272009-01-05 17:45:59 +00001667 DIE *PointerTypeDie = Unit->AddDie(Buffer);
1668 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, PointerTypeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001669 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001670
Jim Laskeyef42a012006-11-02 20:12:39 +00001671 /// AddType - Add a new type attribute to the specified entity.
Devang Patel48d190f2009-01-05 21:47:57 +00001672 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
1673 if (Ty.isNull()) {
1674 AddBasicType(Entity, DW_Unit, "", DW_ATE_signed, sizeof(int32_t));
1675 return;
1676 }
1677
1678 // Check for pre-existence.
1679 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1680 // If it exists then use the existing value.
1681 if (Slot) {
1682 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1683 return;
1684 }
1685
1686 // Set up proxy.
1687 Slot = NewDIEntry();
1688
1689 // Construct type.
1690 DIE Buffer(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001691 if (Ty.isBasicType(Ty.getTag()))
1692 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1693 else if (Ty.isDerivedType(Ty.getTag()))
1694 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1695 else {
1696 assert (Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
1697 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1698 }
1699
Devang Patel48d190f2009-01-05 21:47:57 +00001700 // Add debug information entry to entity and unit.
1701 DIE *Die = DW_Unit->AddDie(Buffer);
1702 SetDIEntry(Slot, Die);
1703 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1704 }
1705
Devang Patele5202732009-01-05 19:07:53 +00001706 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1707 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001708 DIBasicType BTy) {
Devang Patel08f053f2009-01-05 17:57:47 +00001709
1710 // Get core information.
Devang Patelf193ff02009-01-15 19:26:23 +00001711 const std::string &Name = BTy.getName();
Devang Patel08f053f2009-01-05 17:57:47 +00001712 Buffer.setTag(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001713 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patel08f053f2009-01-05 17:57:47 +00001714 // Add name if not anonymous or intermediate type.
1715 if (!Name.empty())
1716 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001717 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel08f053f2009-01-05 17:57:47 +00001718 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1719 }
1720
Devang Patele5202732009-01-05 19:07:53 +00001721 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1722 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001723 DIDerivedType DTy) {
Devang Patel08f053f2009-01-05 17:57:47 +00001724
1725 // Get core information.
Devang Patelf193ff02009-01-15 19:26:23 +00001726 const std::string &Name = DTy.getName();
1727 uint64_t Size = DTy.getSizeInBits() >> 3;
1728 unsigned Tag = DTy.getTag();
Devang Patel08f053f2009-01-05 17:57:47 +00001729 // FIXME - Workaround for templates.
1730 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1731
1732 Buffer.setTag(Tag);
1733 // Map to main type, void will not have a type.
Devang Patelf193ff02009-01-15 19:26:23 +00001734 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00001735 AddType(DW_Unit, &Buffer, FromTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001736
1737 // Add name if not anonymous or intermediate type.
1738 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1739
1740 // Add size if non-zero (derived types might be zero-sized.)
1741 if (Size)
1742 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1743
1744 // Add source line info if available and TyDesc is not a forward
1745 // declaration.
Devang Patelf193ff02009-01-15 19:26:23 +00001746 // FIXME - Enable this. if (!DTy.isForwardDecl())
Devang Patel08f053f2009-01-05 17:57:47 +00001747 // FIXME - Enable this. AddSourceLine(&Buffer, *DTy);
1748 }
1749
Devang Patelf4215332009-01-05 19:55:51 +00001750 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1751 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001752 DICompositeType CTy) {
Devang Patelf4215332009-01-05 19:55:51 +00001753
1754 // Get core information.
Devang Patelf193ff02009-01-15 19:26:23 +00001755 const std::string &Name = CTy.getName();
1756 uint64_t Size = CTy.getSizeInBits() >> 3;
1757 unsigned Tag = CTy.getTag();
Devang Patelf4215332009-01-05 19:55:51 +00001758 switch (Tag) {
1759 case DW_TAG_vector_type:
1760 case DW_TAG_array_type:
Devang Patelf193ff02009-01-15 19:26:23 +00001761 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001762 break;
1763 //FIXME - Enable this.
1764 // case DW_TAG_enumeration_type:
Devang Patelf193ff02009-01-15 19:26:23 +00001765 // DIArray Elements = CTy.getTypeArray();
Devang Patelf4215332009-01-05 19:55:51 +00001766 // // Add enumerators to enumeration type.
1767 // for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i)
1768 // ConstructEnumTypeDIE(Buffer, &Elements.getElement(i));
1769 // break;
1770 case DW_TAG_subroutine_type:
1771 {
1772 // Add prototype flag.
1773 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patelf193ff02009-01-15 19:26:23 +00001774 DIArray Elements = CTy.getTypeArray();
Devang Patelf4215332009-01-05 19:55:51 +00001775 // Add return type.
Devang Patel48d190f2009-01-05 21:47:57 +00001776 DIDescriptor RTy = Elements.getElement(0);
Devang Patelf193ff02009-01-15 19:26:23 +00001777 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel48d190f2009-01-05 21:47:57 +00001778
1779 //AddType(DW_Unit, &Buffer, Elements.getElement(0));
Devang Patelf4215332009-01-05 19:55:51 +00001780 // Add arguments.
1781 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1782 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00001783 DIDescriptor Ty = Elements.getElement(i);
Devang Patelf193ff02009-01-15 19:26:23 +00001784 AddType(DW_Unit, &Buffer, DIType(Ty.getGV()));
Devang Patelf4215332009-01-05 19:55:51 +00001785 Buffer.AddChild(Arg);
1786 }
1787 }
1788 break;
1789 case DW_TAG_structure_type:
1790 case DW_TAG_union_type:
1791 {
1792 // Add elements to structure type.
Devang Patelf193ff02009-01-15 19:26:23 +00001793 DIArray Elements = CTy.getTypeArray();
Devang Patel153745c2009-01-16 00:50:53 +00001794
1795 // A forward struct declared type may not have elements available.
1796 if (Elements.isNull())
1797 break;
1798
Devang Patelf4215332009-01-05 19:55:51 +00001799 // Add elements to structure type.
1800 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1801 DIDescriptor Element = Elements.getElement(i);
Devang Patelf193ff02009-01-15 19:26:23 +00001802 if (Element.getTag() == dwarf::DW_TAG_subprogram)
1803 ConstructFieldTypeDIE(DW_Unit, Buffer, DISubprogram(Element.getGV()));
1804 else if (Element.getTag() == dwarf::DW_TAG_variable)
1805 ConstructFieldTypeDIE(DW_Unit, Buffer,
1806 DIGlobalVariable(Element.getGV()));
1807 else {
1808 DIDerivedType DT = DIDerivedType(Element.getGV());
1809 assert (DT.isDerivedType(DT.getTag()) && "Unexpected strcut element");
Devang Patelf4215332009-01-05 19:55:51 +00001810 ConstructFieldTypeDIE(DW_Unit, Buffer, DT);
Devang Patelf193ff02009-01-15 19:26:23 +00001811 }
Devang Patelf4215332009-01-05 19:55:51 +00001812 }
1813 }
1814 break;
1815 default:
1816 break;
1817 }
1818
1819 // Add name if not anonymous or intermediate type.
1820 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1821
1822 // Add size if non-zero (derived types might be zero-sized.)
1823 if (Size)
1824 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1825 else {
1826 // Add zero size even if it is not a forward declaration.
1827 // FIXME - Enable this.
Devang Patelf193ff02009-01-15 19:26:23 +00001828 // if (!CTy.isDefinition())
Devang Patelf4215332009-01-05 19:55:51 +00001829 // AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1830 // else
1831 // AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1832 }
1833
1834 // Add source line info if available and TyDesc is not a forward
1835 // declaration.
1836 // FIXME - Enable this.
Devang Patelf193ff02009-01-15 19:26:23 +00001837 // if (CTy.isForwardDecl())
Devang Patelf4215332009-01-05 19:55:51 +00001838 // AddSourceLine(&Buffer, *CTy);
1839 }
1840
Devang Patel68afdc32009-01-05 18:33:01 +00001841 // ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
Devang Patelf193ff02009-01-15 19:26:23 +00001842 void ConstructSubrangeDIE (DIE &Buffer, DISubrange SR, DIE *IndexTy) {
1843 int64_t L = SR.getLo();
1844 int64_t H = SR.getHi();
Devang Patel68afdc32009-01-05 18:33:01 +00001845 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1846 if (L != H) {
1847 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1848 if (L)
1849 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1850 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
1851 }
1852 Buffer.AddChild(DW_Subrange);
1853 }
1854
1855 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1856 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1857 DICompositeType *CTy) {
1858 Buffer.setTag(DW_TAG_array_type);
1859 if (CTy->getTag() == DW_TAG_vector_type)
1860 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1861
1862 DIArray Elements = CTy->getTypeArray();
1863 // FIXME - Enable this.
Devang Patel48d190f2009-01-05 21:47:57 +00001864 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel68afdc32009-01-05 18:33:01 +00001865
1866 // Construct an anonymous type for index type.
1867 DIE IdxBuffer(DW_TAG_base_type);
1868 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1869 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1870 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1871
1872 // Add subranges to array type.
1873 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patelf4215332009-01-05 19:55:51 +00001874 DIDescriptor Element = Elements.getElement(i);
Devang Patelf193ff02009-01-15 19:26:23 +00001875 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1876 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel68afdc32009-01-05 18:33:01 +00001877 }
1878 }
1879
Devang Patelc69bf2c2009-01-05 18:38:38 +00001880 /// ConstructEnumTypeDIE - Construct enum type DIE from
1881 /// DIEnumerator.
Devang Patelf4215332009-01-05 19:55:51 +00001882 void ConstructEnumTypeDIE(CompileUnit *DW_Unit,
1883 DIE &Buffer, DIEnumerator *ETy) {
Devang Patelc69bf2c2009-01-05 18:38:38 +00001884
1885 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1886 AddString(Enumerator, DW_AT_name, DW_FORM_string, ETy->getName());
1887 int64_t Value = ETy->getEnumValue();
1888 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1889 Buffer.AddChild(Enumerator);
1890 }
Devang Patel68afdc32009-01-05 18:33:01 +00001891
Devang Patel86ae1422009-01-05 18:59:44 +00001892 /// ConstructFieldTypeDIE - Construct variable DIE for a struct field.
1893 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
Devang Patelf193ff02009-01-15 19:26:23 +00001894 DIE &Buffer, DIGlobalVariable V) {
Devang Patel86ae1422009-01-05 18:59:44 +00001895
1896 DIE *VariableDie = new DIE(DW_TAG_variable);
Devang Patelf193ff02009-01-15 19:26:23 +00001897 const std::string &LinkageName = V.getLinkageName();
Devang Patel86ae1422009-01-05 18:59:44 +00001898 if (!LinkageName.empty())
1899 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1900 LinkageName);
1901 // FIXME - Enable this. AddSourceLine(VariableDie, V);
Devang Patelf193ff02009-01-15 19:26:23 +00001902 AddType(DW_Unit, VariableDie, V.getType());
1903 if (!V.isLocalToUnit())
Devang Patel86ae1422009-01-05 18:59:44 +00001904 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
1905 AddUInt(VariableDie, DW_AT_declaration, DW_FORM_flag, 1);
1906 Buffer.AddChild(VariableDie);
1907 }
1908
1909 /// ConstructFieldTypeDIE - Construct subprogram DIE for a struct field.
1910 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
Devang Patelf193ff02009-01-15 19:26:23 +00001911 DIE &Buffer, DISubprogram SP,
Devang Patel86ae1422009-01-05 18:59:44 +00001912 bool IsConstructor = false) {
1913 DIE *Method = new DIE(DW_TAG_subprogram);
Devang Patelf193ff02009-01-15 19:26:23 +00001914 AddString(Method, DW_AT_name, DW_FORM_string, SP.getName());
1915 const std::string &LinkageName = SP.getLinkageName();
Devang Patel86ae1422009-01-05 18:59:44 +00001916 if (!LinkageName.empty())
1917 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1918 // FIXME - Enable this. AddSourceLine(Method, SP);
1919
Devang Patelf193ff02009-01-15 19:26:23 +00001920 DICompositeType MTy = SP.getType();
Devang Patel86ae1422009-01-05 18:59:44 +00001921 DIArray Args = MTy.getTypeArray();
1922
1923 // Add Return Type.
Devang Patelf193ff02009-01-15 19:26:23 +00001924 if (!IsConstructor)
1925 AddType(DW_Unit, Method, DIType(Args.getElement(0).getGV()));
Devang Patel86ae1422009-01-05 18:59:44 +00001926
1927 // Add arguments.
1928 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1929 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patelf193ff02009-01-15 19:26:23 +00001930 AddType(DW_Unit, Method, DIType(Args.getElement(i).getGV()));
Devang Patel86ae1422009-01-05 18:59:44 +00001931 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
1932 Method->AddChild(Arg);
1933 }
1934
Devang Patelf193ff02009-01-15 19:26:23 +00001935 if (!SP.isLocalToUnit())
Devang Patel86ae1422009-01-05 18:59:44 +00001936 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
1937 Buffer.AddChild(Method);
1938 }
1939
Devang Patelf193ff02009-01-15 19:26:23 +00001940 /// ConstructFieldTypeDIE - Construct derived type DIE for a struct field.
Devang Patel86ae1422009-01-05 18:59:44 +00001941 void ConstructFieldTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001942 DIDerivedType DTy) {
1943 unsigned Tag = DTy.getTag();
Devang Patel86ae1422009-01-05 18:59:44 +00001944 DIE *MemberDie = new DIE(Tag);
Devang Patelf193ff02009-01-15 19:26:23 +00001945 if (!DTy.getName().empty())
1946 AddString(MemberDie, DW_AT_name, DW_FORM_string, DTy.getName());
Devang Patel86ae1422009-01-05 18:59:44 +00001947 // FIXME - Enable this. AddSourceLine(MemberDie, DTy);
1948
Devang Patelf193ff02009-01-15 19:26:23 +00001949 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00001950 AddType(DW_Unit, MemberDie, FromTy);
Devang Patel86ae1422009-01-05 18:59:44 +00001951
Devang Patelf193ff02009-01-15 19:26:23 +00001952 uint64_t Size = DTy.getSizeInBits();
1953 uint64_t Offset = DTy.getOffsetInBits();
Devang Patel86ae1422009-01-05 18:59:44 +00001954
1955 // FIXME Handle bitfields
1956
1957 // Add size.
1958 AddUInt(MemberDie, DW_AT_bit_size, 0, Size);
1959 // Add computation for offset.
1960 DIEBlock *Block = new DIEBlock();
1961 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1962 AddUInt(Block, 0, DW_FORM_udata, Offset >> 3);
1963 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1964
1965 // FIXME Handle DW_AT_accessibility.
1966
1967 Buffer.AddChild(MemberDie);
1968 }
1969
Devang Patel8526cc02009-01-05 22:35:52 +00001970 /// FindCompileUnit - Get the compile unit for the given descriptor.
1971 ///
1972 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
1973 CompileUnit *DW_Unit = DW_CUs[Unit.getGV()];
1974 assert(DW_Unit && "Missing compile unit.");
1975 return DW_Unit;
1976 }
1977
Devang Patelbbdc8202009-01-13 23:54:55 +00001978 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001979 ///
1980 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1981 // Get the descriptor.
1982 DIVariable *VD = DV->getVariable();
1983
1984 // Translate tag to proper Dwarf tag. The result variable is dropped for
1985 // now.
1986 unsigned Tag;
1987 switch (VD->getTag()) {
1988 case DW_TAG_return_variable: return NULL;
1989 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1990 case DW_TAG_auto_variable: // fall thru
1991 default: Tag = DW_TAG_variable; break;
1992 }
1993
1994 // Define variable debug information entry.
1995 DIE *VariableDie = new DIE(Tag);
1996 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
1997
1998 // Add source line info if available.
1999 AddSourceLine(VariableDie, VD);
2000
2001 // Add variable type.
2002 AddType(Unit, VariableDie, VD->getType());
2003
2004 // Add variable address.
2005 MachineLocation Location;
2006 Location.set(RI->getFrameRegister(*MF),
2007 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
2008 AddAddress(VariableDie, DW_AT_location, Location);
2009
2010 return VariableDie;
2011 }
2012
Devang Patel7a6e5a32009-01-08 02:33:41 +00002013 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2014 ///
2015 DbgScope *getOrCreateScope(GlobalVariable *V) {
2016 DbgScope *&Slot = DbgScopeMap[V];
2017 if (!Slot) {
2018 // FIXME - breaks down when the context is an inlined function.
2019 DIDescriptor ParentDesc;
Devang Patel0e5200f2009-01-15 18:25:17 +00002020 DIDescriptor Desc(V);
2021 if (Desc.getTag() == dwarf::DW_TAG_lexical_block) {
2022 DIBlock Block(V);
2023 ParentDesc = Block.getContext();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002024 }
2025 DbgScope *Parent = ParentDesc.isNull() ?
Devang Patel0dc969e2009-01-10 02:34:18 +00002026 NULL : getOrCreateScope(ParentDesc.getGV());
Devang Patel0e5200f2009-01-15 18:25:17 +00002027 Slot = new DbgScope(Parent, Desc);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002028 if (Parent) {
2029 Parent->AddScope(Slot);
2030 } else if (RootDbgScope) {
2031 // FIXME - Add inlined function scopes to the root so we can delete
2032 // them later. Long term, handle inlined functions properly.
2033 RootDbgScope->AddScope(Slot);
2034 } else {
2035 // First function is top level function.
2036 RootDbgScope = Slot;
2037 }
2038 }
2039 return Slot;
2040 }
2041
2042 /// ConstructDbgScope - Construct the components of a scope.
2043 ///
2044 void ConstructDbgScope(DbgScope *ParentScope,
2045 unsigned ParentStartID, unsigned ParentEndID,
2046 DIE *ParentDie, CompileUnit *Unit) {
2047 // Add variables to scope.
Devang Patel9795da52009-01-10 02:42:49 +00002048 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002049 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2050 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2051 if (VariableDie) ParentDie->AddChild(VariableDie);
2052 }
2053
2054 // Add nested scopes.
Devang Patel9795da52009-01-10 02:42:49 +00002055 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002056 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2057 // Define the Scope debug information entry.
2058 DbgScope *Scope = Scopes[j];
2059 // FIXME - Ignore inlined functions for the time being.
2060 if (!Scope->getParent()) continue;
2061
Devang Patele9bfb0f2009-01-12 18:41:00 +00002062 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2063 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002064
2065 // Ignore empty scopes.
2066 if (StartID == EndID && StartID != 0) continue;
2067 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
2068
2069 if (StartID == ParentStartID && EndID == ParentEndID) {
2070 // Just add stuff to the parent scope.
2071 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2072 } else {
2073 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2074
2075 // Add the scope bounds.
2076 if (StartID) {
2077 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2078 DWLabel("label", StartID));
2079 } else {
2080 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2081 DWLabel("func_begin", SubprogramCount));
2082 }
2083 if (EndID) {
2084 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2085 DWLabel("label", EndID));
2086 } else {
2087 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2088 DWLabel("func_end", SubprogramCount));
2089 }
2090
2091 // Add the scope contents.
2092 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2093 ParentDie->AddChild(ScopeDie);
2094 }
2095 }
2096 }
2097
2098 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2099 ///
2100 void ConstructRootDbgScope(DbgScope *RootScope) {
2101 // Exit if there is no root scope.
2102 if (!RootScope) return;
Devang Patel0e5200f2009-01-15 18:25:17 +00002103 DIDescriptor Desc = RootScope->getDesc();
2104 if (Desc.isNull())
2105 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002106
2107 // Get the subprogram debug information entry.
Devang Patel0e5200f2009-01-15 18:25:17 +00002108 DISubprogram SPD(Desc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002109
2110 // Get the compile unit context.
Devang Patelf6bac3e2009-01-12 22:58:14 +00002111 CompileUnit *Unit = FindCompileUnit(SPD.getCompileUnit());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002112
2113 // Get the subprogram die.
Devang Patelf6bac3e2009-01-12 22:58:14 +00002114 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002115 assert(SPDie && "Missing subprogram descriptor");
2116
2117 // Add the function bounds.
2118 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2119 DWLabel("func_begin", SubprogramCount));
2120 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2121 DWLabel("func_end", SubprogramCount));
2122 MachineLocation Location(RI->getFrameRegister(*MF));
2123 AddAddress(SPDie, DW_AT_frame_base, Location);
2124
2125 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2126 }
2127
2128 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2129 ///
2130 void ConstructDefaultDbgScope(MachineFunction *MF) {
2131 // Find the correct subprogram descriptor.
2132 std::string SPName = "llvm.dbg.subprograms";
2133 std::vector<GlobalVariable*> Result;
2134 getGlobalVariablesUsing(*M, SPName, Result);
2135 for (std::vector<GlobalVariable *>::iterator I = Result.begin(),
2136 E = Result.end(); I != E; ++I) {
2137
2138 DISubprogram *SPD = new DISubprogram(*I);
2139
2140 if (SPD->getName() == MF->getFunction()->getName()) {
2141 // Get the compile unit context.
2142 CompileUnit *Unit = FindCompileUnit(SPD->getCompileUnit());
2143
2144 // Get the subprogram die.
2145 DIE *SPDie = Unit->getDieMapSlotFor(SPD->getGV());
2146 assert(SPDie && "Missing subprogram descriptor");
2147
2148 // Add the function bounds.
2149 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2150 DWLabel("func_begin", SubprogramCount));
2151 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2152 DWLabel("func_end", SubprogramCount));
2153
2154 MachineLocation Location(RI->getFrameRegister(*MF));
2155 AddAddress(SPDie, DW_AT_frame_base, Location);
2156 return;
2157 }
2158 }
2159#if 0
2160 // FIXME: This is causing an abort because C++ mangled names are compared
2161 // with their unmangled counterparts. See PR2885. Don't do this assert.
2162 assert(0 && "Couldn't find DIE for machine function!");
2163#endif
2164 }
2165
Jim Laskeyef42a012006-11-02 20:12:39 +00002166 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2167 /// tools to recognize the object file contains Dwarf information.
2168 void EmitInitial() {
2169 // Check to see if we already emitted intial headers.
2170 if (didInitial) return;
2171 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002172
Jim Laskeyef42a012006-11-02 20:12:39 +00002173 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002174 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002175 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002176 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002177 }
2178 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2179 EmitLabel("section_info", 0);
2180 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2181 EmitLabel("section_abbrev", 0);
2182 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2183 EmitLabel("section_aranges", 0);
2184 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2185 EmitLabel("section_macinfo", 0);
2186 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2187 EmitLabel("section_line", 0);
2188 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2189 EmitLabel("section_loc", 0);
2190 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2191 EmitLabel("section_pubnames", 0);
2192 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2193 EmitLabel("section_str", 0);
2194 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2195 EmitLabel("section_ranges", 0);
2196
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002197 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002198 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002199 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002200 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002201 }
2202
Jim Laskey65195462006-10-30 13:35:07 +00002203 /// EmitDIE - Recusively Emits a debug information entry.
2204 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002205 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002206 // Get the abbreviation for this DIE.
2207 unsigned AbbrevNumber = Die->getAbbrevNumber();
2208 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002209
Jim Laskeybacd3042007-02-21 22:48:45 +00002210 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002211
2212 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002213 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002214
2215 if (VerboseAsm)
2216 Asm->EOL(std::string("Abbrev [" +
2217 utostr(AbbrevNumber) +
2218 "] 0x" + utohexstr(Die->getOffset()) +
2219 ":0x" + utohexstr(Die->getSize()) + " " +
2220 TagString(Abbrev->getTag())));
2221 else
2222 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002223
Owen Anderson873e1b52008-06-24 21:44:59 +00002224 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2225 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002226
Jim Laskeyef42a012006-11-02 20:12:39 +00002227 // Emit the DIE attribute values.
2228 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2229 unsigned Attr = AbbrevData[i].getAttribute();
2230 unsigned Form = AbbrevData[i].getForm();
2231 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002232
Jim Laskeyef42a012006-11-02 20:12:39 +00002233 switch (Attr) {
2234 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002235 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002236 break;
2237 }
2238 default: {
2239 // Emit an attribute using the defined form.
2240 Values[i]->EmitValue(*this, Form);
2241 break;
2242 }
2243 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002244
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002245 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002246 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002247
Jim Laskeyef42a012006-11-02 20:12:39 +00002248 // Emit the DIE children if any.
2249 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2250 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002251
Jim Laskeyef42a012006-11-02 20:12:39 +00002252 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2253 EmitDIE(Children[j]);
2254 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002255
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002256 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002257 }
2258 }
2259
Jim Laskey65195462006-10-30 13:35:07 +00002260 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2261 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002262 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2263 // Get the children.
2264 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002265
Jim Laskeyef42a012006-11-02 20:12:39 +00002266 // If not last sibling and has children then add sibling offset attribute.
2267 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2268
2269 // Record the abbreviation.
2270 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002271
Jim Laskeyef42a012006-11-02 20:12:39 +00002272 // Get the abbreviation for this DIE.
2273 unsigned AbbrevNumber = Die->getAbbrevNumber();
2274 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2275
2276 // Set DIE offset
2277 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002278
Jim Laskeyef42a012006-11-02 20:12:39 +00002279 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002280 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2281
Owen Anderson873e1b52008-06-24 21:44:59 +00002282 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2283 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002284
2285 // Size the DIE attribute values.
2286 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2287 // Size attribute value.
2288 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2289 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002290
Jim Laskeyef42a012006-11-02 20:12:39 +00002291 // Size the DIE children if any.
2292 if (!Children.empty()) {
2293 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2294 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002295
Jim Laskeyef42a012006-11-02 20:12:39 +00002296 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2297 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2298 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002299
Jim Laskeyef42a012006-11-02 20:12:39 +00002300 // End of children marker.
2301 Offset += sizeof(int8_t);
2302 }
2303
2304 Die->setSize(Offset - Die->getOffset());
2305 return Offset;
2306 }
Jim Laskey65195462006-10-30 13:35:07 +00002307
2308 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2309 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002310 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002311 // Process base compile unit.
Devang Patel72b66352009-01-12 23:05:55 +00002312 for (DenseMap<Value *, CompileUnit *>::iterator CI = DW_CUs.begin(),
2313 CE = DW_CUs.end(); CI != CE; ++CI) {
2314 CompileUnit *Unit = CI->second;
2315 // Compute size of compile unit header
2316 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2317 sizeof(int16_t) + // DWARF version number
2318 sizeof(int32_t) + // Offset Into Abbrev. Section
2319 sizeof(int8_t); // Pointer Size (in bytes)
2320 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2321 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002322 }
2323
Jim Laskey65195462006-10-30 13:35:07 +00002324 /// EmitDebugInfo - Emit the debug info section.
2325 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002326 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002327 // Start debug info section.
2328 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002329
Devang Patel72b66352009-01-12 23:05:55 +00002330 for (DenseMap<Value *, CompileUnit *>::iterator CI = DW_CUs.begin(),
2331 CE = DW_CUs.end(); CI != CE; ++CI) {
2332 CompileUnit *Unit = CI->second;
2333 DIE *Die = Unit->getDie();
2334 // Emit the compile units header.
2335 EmitLabel("info_begin", Unit->getID());
2336 // Emit size of content not including length itself
2337 unsigned ContentSize = Die->getSize() +
2338 sizeof(int16_t) + // DWARF version number
2339 sizeof(int32_t) + // Offset Into Abbrev. Section
2340 sizeof(int8_t) + // Pointer Size (in bytes)
2341 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2342
2343 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2344 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2345 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2346 Asm->EOL("Offset Into Abbrev. Section");
2347 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2348
2349 EmitDIE(Die);
2350 // FIXME - extra padding for gdb bug.
2351 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2352 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2353 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2354 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2355 EmitLabel("info_end", Unit->getID());
2356
2357 Asm->EOL();
2358 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002359 }
2360
Jim Laskey65195462006-10-30 13:35:07 +00002361 /// EmitAbbreviations - Emit the abbreviation section.
2362 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002363 void EmitAbbreviations() const {
2364 // Check to see if it is worth the effort.
2365 if (!Abbreviations.empty()) {
2366 // Start the debug abbrev section.
2367 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002368
Jim Laskeyef42a012006-11-02 20:12:39 +00002369 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002370
Jim Laskeyef42a012006-11-02 20:12:39 +00002371 // For each abbrevation.
2372 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2373 // Get abbreviation data
2374 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002375
Jim Laskeyef42a012006-11-02 20:12:39 +00002376 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002377 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2378 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002379
Jim Laskeyef42a012006-11-02 20:12:39 +00002380 // Emit the abbreviations data.
2381 Abbrev->Emit(*this);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002382
Jim Laskeybacd3042007-02-21 22:48:45 +00002383 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002384 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002385
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002386 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00002387 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002388
Jim Laskeyef42a012006-11-02 20:12:39 +00002389 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002390
Jim Laskeybacd3042007-02-21 22:48:45 +00002391 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002392 }
2393 }
2394
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002395 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2396 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002397 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002398 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2399 // Define last address of section.
2400 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2401 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2402 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2403 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2404
2405 // Mark end of matrix.
2406 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2407 Asm->EmitULEB128Bytes(1); Asm->EOL();
2408 Asm->EmitInt8(1); Asm->EOL();
2409 }
2410
Jim Laskey65195462006-10-30 13:35:07 +00002411 /// EmitDebugLines - Emit source line information.
2412 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002413 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002414 // If the target is using .loc/.file, the assembler will be emitting the
2415 // .debug_line table automatically.
2416 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00002417 return;
2418
Jim Laskeyef42a012006-11-02 20:12:39 +00002419 // Minimum line delta, thus ranging from -10..(255-10).
2420 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2421 // Maximum line delta, thus ranging from -10..(255-10).
2422 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002423
Jim Laskeyef42a012006-11-02 20:12:39 +00002424 // Start the dwarf line section.
2425 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002426
Jim Laskeyef42a012006-11-02 20:12:39 +00002427 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002428
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002429 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002430 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002431 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002432
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002433 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002434
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002435 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002436 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002437 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002438
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002439 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002440
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002441 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002442
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002443 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002444
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002445 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002446
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002447 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002448
Jim Laskeyef42a012006-11-02 20:12:39 +00002449 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002450 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2451 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2452 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2453 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2454 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2455 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2456 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2457 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2458 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00002459
Jim Laskeyef42a012006-11-02 20:12:39 +00002460 // Emit directories.
2461 for (unsigned DirectoryID = 1, NDID = Directories.size();
2462 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002463 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00002464 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002465 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002466
Jim Laskeyef42a012006-11-02 20:12:39 +00002467 // Emit files.
Devang Patel7bb89ed2009-01-13 00:20:51 +00002468 for (unsigned SourceID = 1, NSID = SrcFiles.size();
Jim Laskeyef42a012006-11-02 20:12:39 +00002469 SourceID <= NSID; ++SourceID) {
Devang Patel7bb89ed2009-01-13 00:20:51 +00002470 const SrcFileInfo &SourceFile = SrcFiles[SourceID];
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002471 Asm->EmitString(SourceFile.getName());
2472 Asm->EOL("Source");
2473 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
2474 Asm->EOL("Directory #");
2475 Asm->EmitULEB128Bytes(0);
2476 Asm->EOL("Mod date");
2477 Asm->EmitULEB128Bytes(0);
2478 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002479 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002480 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002481
Jim Laskeyef42a012006-11-02 20:12:39 +00002482 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002483
Jim Laskeyef42a012006-11-02 20:12:39 +00002484 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002485 unsigned SecSrcLinesSize = SectionSourceLines.size();
2486
2487 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002488 // Isolate current sections line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00002489 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00002490
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002491 if (VerboseAsm) {
2492 const Section* S = SectionMap[j + 1];
2493 Asm->EOL(std::string("Section ") + S->getName());
2494 } else
Evan Cheng6547e402008-07-01 23:18:29 +00002495 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002496
2497 // Dwarf assumes we start with first line of first source file.
2498 unsigned Source = 1;
2499 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002500
Jim Laskeyef42a012006-11-02 20:12:39 +00002501 // Construct rows of the address, source, line, column matrix.
2502 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patelf3ee5142009-01-12 22:54:42 +00002503 const SrcLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002504 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00002505 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002506
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002507 unsigned SourceID = LineInfo.getSourceID();
Devang Patel7bb89ed2009-01-13 00:20:51 +00002508 const SrcFileInfo &SourceFile = SrcFiles[SourceID];
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002509 unsigned DirectoryID = SourceFile.getDirectoryID();
Evan Cheng6547e402008-07-01 23:18:29 +00002510 if (VerboseAsm)
2511 Asm->EOL(Directories[DirectoryID]
2512 + SourceFile.getName()
2513 + ":"
2514 + utostr_32(LineInfo.getLine()));
2515 else
2516 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002517
2518 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002519 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00002520 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002521 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00002522 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002523
Jim Laskeyef42a012006-11-02 20:12:39 +00002524 // If change of source, then switch to the new source.
2525 if (Source != LineInfo.getSourceID()) {
2526 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002527 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2528 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00002529 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002530
Jim Laskeyef42a012006-11-02 20:12:39 +00002531 // If change of line.
2532 if (Line != LineInfo.getLine()) {
2533 // Determine offset.
2534 int Offset = LineInfo.getLine() - Line;
2535 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002536
Jim Laskeyef42a012006-11-02 20:12:39 +00002537 // Update line.
2538 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002539
Jim Laskeyef42a012006-11-02 20:12:39 +00002540 // If delta is small enough and in range...
2541 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2542 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002543 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00002544 } else {
2545 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002546 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2547 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2548 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002549 }
2550 } else {
2551 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002552 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002553 }
2554 }
2555
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002556 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002557 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002558
2559 if (SecSrcLinesSize == 0)
2560 // Because we're emitting a debug_line section, we still need a line
2561 // table. The linker and friends expect it to exist. If there's nothing to
2562 // put into it, emit an empty table.
2563 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002564
Jim Laskeyef42a012006-11-02 20:12:39 +00002565 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002566
Jim Laskeybacd3042007-02-21 22:48:45 +00002567 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002568 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002569
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002570 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00002571 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002572 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002573 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00002574 return;
2575
2576 int stackGrowth =
2577 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2578 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002579 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00002580
2581 // Start the dwarf frame section.
2582 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2583
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002584 EmitLabel("debug_frame_common", 0);
2585 EmitDifference("debug_frame_common_end", 0,
2586 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002587 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002588
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002589 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002590 Asm->EmitInt32((int)DW_CIE_ID);
2591 Asm->EOL("CIE Identifier Tag");
2592 Asm->EmitInt8(DW_CIE_VERSION);
2593 Asm->EOL("CIE Version");
2594 Asm->EmitString("");
2595 Asm->EOL("CIE Augmentation");
2596 Asm->EmitULEB128Bytes(1);
2597 Asm->EOL("CIE Code Alignment Factor");
2598 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002599 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002600 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002601 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002602
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002603 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002604 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002605
Dale Johannesenb97aec62007-11-13 19:13:01 +00002606 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002607
Evan Cheng05548eb2008-02-29 19:36:59 +00002608 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002609 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002610
Jim Laskeybacd3042007-02-21 22:48:45 +00002611 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002612 }
2613
Jim Laskey65195462006-10-30 13:35:07 +00002614 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2615 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002616 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002617 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00002618 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002619
Jim Laskeyef42a012006-11-02 20:12:39 +00002620 // Start the dwarf frame section.
2621 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002622
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002623 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2624 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002625 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002626
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002627 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002628
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002629 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2630 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002631 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002632
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002633 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002634 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002635 EmitDifference("func_end", DebugFrameInfo.Number,
2636 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002637 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002638
Dale Johannesenb97aec62007-11-13 19:13:01 +00002639 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves, false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002640
Evan Cheng05548eb2008-02-29 19:36:59 +00002641 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002642 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00002643
Jim Laskeybacd3042007-02-21 22:48:45 +00002644 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002645 }
2646
2647 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002648 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002649 void EmitDebugPubNames() {
2650 // Start the dwarf pubnames section.
2651 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002652
Devang Patel72b66352009-01-12 23:05:55 +00002653 for (DenseMap<Value *, CompileUnit *>::iterator CI = DW_CUs.begin(),
2654 CE = DW_CUs.end(); CI != CE; ++CI) {
2655 CompileUnit *Unit = CI->second;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002656
Devang Patel72b66352009-01-12 23:05:55 +00002657 EmitDifference("pubnames_end", Unit->getID(),
2658 "pubnames_begin", Unit->getID(), true);
2659 Asm->EOL("Length of Public Names Info");
2660
2661 EmitLabel("pubnames_begin", Unit->getID());
2662
2663 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2664
2665 EmitSectionOffset("info_begin", "section_info",
2666 Unit->getID(), 0, true, false);
2667 Asm->EOL("Offset of Compilation Unit Info");
2668
2669 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
2670 Asm->EOL("Compilation Unit Length");
2671
2672 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2673
2674 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2675 GE = Globals.end();
2676 GI != GE; ++GI) {
2677 const std::string &Name = GI->first;
2678 DIE * Entity = GI->second;
2679
2680 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2681 Asm->EmitString(Name); Asm->EOL("External Name");
2682 }
2683
2684 Asm->EmitInt32(0); Asm->EOL("End Mark");
2685 EmitLabel("pubnames_end", Unit->getID());
2686
2687 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002688 }
2689 }
2690
2691 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002692 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002693 void EmitDebugStr() {
2694 // Check to see if it is worth the effort.
2695 if (!StringPool.empty()) {
2696 // Start the dwarf str section.
2697 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002698
Jim Laskeyef42a012006-11-02 20:12:39 +00002699 // For each of strings in the string pool.
2700 for (unsigned StringID = 1, N = StringPool.size();
2701 StringID <= N; ++StringID) {
2702 // Emit a label for reference from debug information entries.
2703 EmitLabel("string", StringID);
2704 // Emit the string itself.
2705 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00002706 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002707 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002708
Jim Laskeybacd3042007-02-21 22:48:45 +00002709 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002710 }
2711 }
2712
2713 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002714 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002715 void EmitDebugLoc() {
2716 // Start the dwarf loc section.
2717 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002718
Jim Laskeybacd3042007-02-21 22:48:45 +00002719 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002720 }
2721
2722 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002723 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002724 void EmitDebugARanges() {
2725 // Start the dwarf aranges section.
2726 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002727
Jim Laskeyef42a012006-11-02 20:12:39 +00002728 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00002729#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002730 CompileUnit *Unit = GetBaseCompileUnit();
2731
Jim Laskey5496f012006-11-09 14:52:14 +00002732 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002733 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002734
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002735 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002736
Jim Laskey5496f012006-11-09 14:52:14 +00002737 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002738 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002739
Dan Gohman82482942007-09-27 23:12:31 +00002740 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002741
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002742 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002743
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002744 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2745 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002746
Jim Laskey5496f012006-11-09 14:52:14 +00002747 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002748 EmitReference("text_begin", 0); Asm->EOL("Address");
2749 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002750
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002751 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2752 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00002753#endif
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002754
Jim Laskeybacd3042007-02-21 22:48:45 +00002755 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002756 }
2757
2758 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002759 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002760 void EmitDebugRanges() {
2761 // Start the dwarf ranges section.
2762 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002763
Jim Laskeybacd3042007-02-21 22:48:45 +00002764 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002765 }
2766
2767 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002768 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002769 void EmitDebugMacInfo() {
2770 // Start the dwarf macinfo section.
2771 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002772
Jim Laskeybacd3042007-02-21 22:48:45 +00002773 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002774 }
2775
Devang Patelc4523242009-01-05 23:11:11 +00002776 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Pateld1ca9252009-01-05 23:03:32 +00002777 void ConstructCompileUnits() {
2778 std::string CUName = "llvm.dbg.compile_units";
2779 std::vector<GlobalVariable*> Result;
2780 getGlobalVariablesUsing(*M, CUName, Result);
2781 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
2782 RE = Result.end(); RI != RE; ++RI) {
2783 DICompileUnit *DIUnit = new DICompileUnit(*RI);
Devang Patel9f8fcfc2009-01-08 17:19:22 +00002784 unsigned ID = RecordSource(DIUnit->getDirectory(),
2785 DIUnit->getFilename());
Devang Pateld1ca9252009-01-05 23:03:32 +00002786
2787 DIE *Die = new DIE(DW_TAG_compile_unit);
2788 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2789 DWLabel("section_line", 0), DWLabel("section_line", 0),
2790 false);
2791 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit->getProducer());
2792 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit->getLanguage());
2793 AddString(Die, DW_AT_name, DW_FORM_string, DIUnit->getFilename());
2794 if (!DIUnit->getDirectory().empty())
2795 AddString(Die, DW_AT_comp_dir, DW_FORM_string, DIUnit->getDirectory());
2796
2797 CompileUnit *Unit = new CompileUnit(ID, Die);
2798 DW_CUs[DIUnit->getGV()] = Unit;
2799 }
2800 }
2801
Devang Patelc4523242009-01-05 23:11:11 +00002802 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
2803 /// visible global variables.
2804 void ConstructGlobalVariableDIEs() {
2805 std::string GVName = "llvm.dbg.global_variables";
2806 std::vector<GlobalVariable*> Result;
2807 getGlobalVariablesUsing(*M, GVName, Result);
2808 for (std::vector<GlobalVariable *>::iterator GVI = Result.begin(),
2809 GVE = Result.end(); GVI != GVE; ++GVI) {
2810 DIGlobalVariable *DI_GV = new DIGlobalVariable(*GVI);
2811 CompileUnit *DW_Unit = FindCompileUnit(DI_GV->getCompileUnit());
2812
2813 // Check for pre-existence.
2814 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV->getGV());
2815 if (Slot) continue;
2816
2817 DIE *VariableDie = new DIE(DW_TAG_variable);
2818 AddString(VariableDie, DW_AT_name, DW_FORM_string, DI_GV->getName());
2819 const std::string &LinkageName = DI_GV->getLinkageName();
2820 if (!LinkageName.empty())
2821 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
2822 LinkageName);
2823 AddType(DW_Unit, VariableDie, DI_GV->getType());
2824
2825 if (!DI_GV->isLocalToUnit())
2826 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
2827
2828 // Add source line info, if available.
2829 AddSourceLine(VariableDie, DI_GV);
2830
2831 // Add address.
2832 DIEBlock *Block = new DIEBlock();
2833 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
2834 AddObjectLabel(Block, 0, DW_FORM_udata,
2835 Asm->getGlobalLinkName(DI_GV->getGV()));
2836 AddBlock(VariableDie, DW_AT_location, 0, Block);
2837
2838 //Add to map.
2839 Slot = VariableDie;
2840
2841 //Add to context owner.
2842 DW_Unit->getDie()->AddChild(VariableDie);
2843
2844 //Expose as global. FIXME - need to check external flag.
2845 DW_Unit->AddGlobal(DI_GV->getName(), VariableDie);
2846 }
2847 }
2848
Devang Patel78eb6ad2009-01-05 23:21:35 +00002849 /// ConstructSubprograms - Create DIEs for each of the externally visible
2850 /// subprograms.
2851 void ConstructSubprograms() {
2852
2853 std::string SPName = "llvm.dbg.subprograms";
2854 std::vector<GlobalVariable*> Result;
2855 getGlobalVariablesUsing(*M, SPName, Result);
2856 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
2857 RE = Result.end(); RI != RE; ++RI) {
2858
2859 DISubprogram *SP = new DISubprogram(*RI);
2860 CompileUnit *Unit = FindCompileUnit(SP->getCompileUnit());
2861
2862 // Check for pre-existence.
2863 DIE *&Slot = Unit->getDieMapSlotFor(SP->getGV());
2864 if (Slot) continue;
2865
2866 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
2867 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, SP->getName());
2868 const std::string &LinkageName = SP->getLinkageName();
2869 if (!LinkageName.empty())
2870 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
2871 LinkageName);
2872 DIType SPTy = SP->getType();
2873 AddType(Unit, SubprogramDie, SPTy);
2874 if (!SP->isLocalToUnit())
2875 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
2876 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
2877
2878 AddSourceLine(SubprogramDie, SP);
2879 //Add to map.
2880 Slot = SubprogramDie;
2881 //Add to context owner.
2882 Unit->getDie()->AddChild(SubprogramDie);
2883 //Expose as global.
2884 Unit->AddGlobal(SP->getName(), SubprogramDie);
2885 }
2886 }
2887
Jim Laskey65195462006-10-30 13:35:07 +00002888public:
Jim Laskeyef42a012006-11-02 20:12:39 +00002889 //===--------------------------------------------------------------------===//
2890 // Main entry points.
2891 //
Owen Andersoncb371882008-08-21 00:14:44 +00002892 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00002893 : Dwarf(OS, A, T, "dbg")
Jim Laskeyef42a012006-11-02 20:12:39 +00002894 , CompileUnits()
2895 , AbbreviationsSet(InitAbbreviationsSetSize)
2896 , Abbreviations()
2897 , ValuesSet(InitValuesSetSize)
2898 , Values()
2899 , StringPool()
Jim Laskeyef42a012006-11-02 20:12:39 +00002900 , SectionMap()
2901 , SectionSourceLines()
Jim Laskeybacd3042007-02-21 22:48:45 +00002902 , didInitial(false)
2903 , shouldEmit(false)
Devang Patel7a6e5a32009-01-08 02:33:41 +00002904 , RootDbgScope(NULL)
Jim Laskeyef42a012006-11-02 20:12:39 +00002905 {
2906 }
Jim Laskey072200c2007-01-29 18:51:14 +00002907 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002908 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
2909 delete CompileUnits[i];
2910 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2911 delete Values[j];
2912 }
2913
Devang Patel5d315982009-01-06 21:07:30 +00002914 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
2915 /// This is inovked by the target AsmPrinter.
Devang Patel3f7833a2009-01-12 23:09:42 +00002916 void SetDebugInfo(MachineModuleInfo *mmi) {
2917
Devang Patel5d315982009-01-06 21:07:30 +00002918 // Create all the compile unit DIEs.
2919 ConstructCompileUnits();
Devang Patel3f7833a2009-01-12 23:09:42 +00002920
2921 if (DW_CUs.empty())
2922 return;
2923
2924 MMI = mmi;
2925 shouldEmit = true;
Devang Patele2051622009-01-13 23:02:17 +00002926 MMI->setDebugInfoAvailability(true);
Devang Patel5d315982009-01-06 21:07:30 +00002927
2928 // Create DIEs for each of the externally visible global variables.
2929 ConstructGlobalVariableDIEs();
2930
2931 // Create DIEs for each of the externally visible subprograms.
2932 ConstructSubprograms();
2933
2934 // Prime section data.
2935 SectionMap.insert(TAI->getTextSection());
2936
2937 // Print out .file directives to specify files for .loc directives. These
2938 // are printed out early so that they precede any .loc directives.
2939 if (TAI->hasDotLocAndDotFile()) {
2940 for (unsigned i = 1, e = SrcFiles.size(); i <= e; ++i) {
2941 sys::Path FullPath(Directories[SrcFiles[i].getDirectoryID()]);
2942 bool AppendOk = FullPath.appendComponent(SrcFiles[i].getName());
2943 assert(AppendOk && "Could not append filename to directory!");
2944 AppendOk = false;
2945 Asm->EmitFile(i, FullPath.toString());
2946 Asm->EOL();
2947 }
2948 }
2949
2950 // Emit initial sections
2951 EmitInitial();
2952 }
2953
Jim Laskey65195462006-10-30 13:35:07 +00002954 /// BeginModule - Emit all Dwarf sections that should come prior to the
2955 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00002956 void BeginModule(Module *M) {
2957 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00002958 }
2959
Jim Laskey65195462006-10-30 13:35:07 +00002960 /// EndModule - Emit all Dwarf sections that should come after the content.
2961 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002962 void EndModule() {
2963 if (!ShouldEmitDwarf()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002964
Jim Laskeyef42a012006-11-02 20:12:39 +00002965 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002966 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002967 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002968 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002969 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002970
Jim Laskeyef42a012006-11-02 20:12:39 +00002971 // End text sections.
2972 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002973 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002974 EmitLabel("section_end", i);
2975 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002976
2977 // Emit common frame information.
2978 EmitCommonDebugFrame();
2979
2980 // Emit function debug frame information
2981 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2982 E = DebugFrames.end(); I != E; ++I)
2983 EmitFunctionDebugFrame(*I);
2984
Jim Laskeyef42a012006-11-02 20:12:39 +00002985 // Compute DIE offsets and sizes.
2986 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002987
Jim Laskeyef42a012006-11-02 20:12:39 +00002988 // Emit all the DIEs into a debug info section
2989 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002990
Jim Laskeyef42a012006-11-02 20:12:39 +00002991 // Corresponding abbreviations into a abbrev section.
2992 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002993
Jim Laskeyef42a012006-11-02 20:12:39 +00002994 // Emit source line correspondence into a debug line section.
2995 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002996
Jim Laskeyef42a012006-11-02 20:12:39 +00002997 // Emit info into a debug pubnames section.
2998 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002999
Jim Laskeyef42a012006-11-02 20:12:39 +00003000 // Emit info into a debug str section.
3001 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003002
Jim Laskeyef42a012006-11-02 20:12:39 +00003003 // Emit info into a debug loc section.
3004 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003005
Jim Laskeyef42a012006-11-02 20:12:39 +00003006 // Emit info into a debug aranges section.
3007 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003008
Jim Laskeyef42a012006-11-02 20:12:39 +00003009 // Emit info into a debug ranges section.
3010 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003011
Jim Laskeyef42a012006-11-02 20:12:39 +00003012 // Emit info into a debug macinfo section.
3013 EmitDebugMacInfo();
3014 }
3015
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003016 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00003017 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00003018 void BeginFunction(MachineFunction *MF) {
3019 this->MF = MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003020
Jim Laskeyef42a012006-11-02 20:12:39 +00003021 if (!ShouldEmitDwarf()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00003022
3023 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003024 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003025
Jim Laskeyef42a012006-11-02 20:12:39 +00003026 // Assumes in correct section after the entry point.
3027 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00003028
3029 // Emit label for the implicitly defined dbg.stoppoint at the start of
3030 // the function.
Devang Patelf3ee5142009-01-12 22:54:42 +00003031 if (!Lines.empty()) {
3032 const SrcLineInfo &LineInfo = Lines[0];
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00003033 Asm->printLabel(LineInfo.getLabelID());
3034 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003035 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003036
Jim Laskey65195462006-10-30 13:35:07 +00003037 /// EndFunction - Gather and emit post-function debug information.
3038 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00003039 void EndFunction(MachineFunction *MF) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003040 if (!ShouldEmitDwarf()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003041
Jim Laskeyb82313f2007-02-01 16:31:34 +00003042 // Define end label for subprogram.
3043 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003044
Jim Laskeyef42a012006-11-02 20:12:39 +00003045 // Get function line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00003046 if (!Lines.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003047 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003048 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00003049 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patelf3ee5142009-01-12 22:54:42 +00003050 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Jim Laskeyef42a012006-11-02 20:12:39 +00003051 // Append the function info to section info.
3052 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patelf3ee5142009-01-12 22:54:42 +00003053 Lines.begin(), Lines.end());
Jim Laskeyef42a012006-11-02 20:12:39 +00003054 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003055
Jim Laskeyef42a012006-11-02 20:12:39 +00003056 // Construct scopes for subprogram.
Devang Patel7bb89ed2009-01-13 00:20:51 +00003057 if (RootDbgScope)
3058 ConstructRootDbgScope(RootDbgScope);
Bill Wendlingd751c642008-09-26 00:28:12 +00003059 else
3060 // FIXME: This is wrong. We are essentially getting past a problem with
3061 // debug information not being able to handle unreachable blocks that have
3062 // debug information in them. In particular, those unreachable blocks that
3063 // have "region end" info in them. That situation results in the "root
3064 // scope" not being created. If that's the case, then emit a "default"
3065 // scope, i.e., one that encompasses the whole function. This isn't
3066 // desirable. And a better way of handling this (and all of the debugging
3067 // information) needs to be explored.
Devang Patel7bb89ed2009-01-13 00:20:51 +00003068 ConstructDefaultDbgScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003069
3070 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3071 MMI->getFrameMoves()));
Devang Patel481ff5b2009-01-12 18:48:36 +00003072
3073 // Clear debug info
3074 if (RootDbgScope) {
3075 delete RootDbgScope;
3076 DbgScopeMap.clear();
3077 RootDbgScope = NULL;
3078 }
3079 Lines.clear();
Jim Laskeyef42a012006-11-02 20:12:39 +00003080 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003081
3082public:
3083
Devang Patelcf3a4482009-01-15 23:41:32 +00003084 /// ValidDebugInfo - Return true if V represents valid debug info value.
3085 bool ValidDebugInfo(Value *V) {
Devang Patel61c6bf32009-01-16 01:49:46 +00003086
3087 if (!shouldEmit)
3088 return false;
3089
Devang Patelcf3a4482009-01-15 23:41:32 +00003090 GlobalVariable *GV = getGlobalVariable(V);
3091 if (!GV)
3092 return false;
3093
3094 if (GV->getLinkage() != GlobalValue::InternalLinkage
3095 && GV->getLinkage() != GlobalValue::LinkOnceLinkage)
3096 return false;
3097
3098 DIDescriptor DI(GV);
3099 // Check current version. Allow Version6 for now.
3100 unsigned Version = DI.getVersion();
3101 if (Version != DIDescriptor::Version7 && Version != DIDescriptor::Version6)
3102 return false;
3103
3104 //FIXME - Check individual descriptors.
3105 return true;
3106 }
3107
Devang Patelccca7fe2009-01-12 19:17:34 +00003108 /// RecordSourceLine - Records location information and associates it with a
3109 /// label. Returns a unique label ID used to generate a label and provide
3110 /// correspondence to the source line list.
3111 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
3112 CompileUnit *Unit = DW_CUs[V];
3113 assert (Unit && "Unable to find CompileUnit");
3114 unsigned ID = MMI->NextLabelID();
3115 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
3116 return ID;
3117 }
3118
3119 /// RecordSourceLine - Records location information and associates it with a
3120 /// label. Returns a unique label ID used to generate a label and provide
3121 /// correspondence to the source line list.
3122 unsigned RecordSourceLine(unsigned Line, unsigned Col, unsigned Src) {
3123 unsigned ID = MMI->NextLabelID();
3124 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
3125 return ID;
3126 }
3127
3128 unsigned getRecordSourceLineCount() {
3129 return Lines.size();
3130 }
3131
3132 /// RecordSource - Register a source file with debug info. Returns an source
3133 /// ID.
3134 unsigned RecordSource(const std::string &Directory,
3135 const std::string &File) {
3136 unsigned DID = Directories.insert(Directory);
3137 return SrcFiles.insert(SrcFileInfo(DID,File));
3138 }
3139
3140 /// RecordRegionStart - Indicate the start of a region.
3141 ///
3142 unsigned RecordRegionStart(GlobalVariable *V) {
3143 DbgScope *Scope = getOrCreateScope(V);
3144 unsigned ID = MMI->NextLabelID();
3145 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
3146 return ID;
3147 }
3148
3149 /// RecordRegionEnd - Indicate the end of a region.
3150 ///
3151 unsigned RecordRegionEnd(GlobalVariable *V) {
3152 DbgScope *Scope = getOrCreateScope(V);
3153 unsigned ID = MMI->NextLabelID();
3154 Scope->setEndLabelID(ID);
3155 return ID;
3156 }
3157
3158 /// RecordVariable - Indicate the declaration of a local variable.
3159 ///
3160 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
Devang Patel0e5200f2009-01-15 18:25:17 +00003161 DIDescriptor Desc(GV);
3162 DbgScope *Scope = NULL;
3163 if (Desc.getTag() == DW_TAG_variable) {
3164 // GV is a global variable.
3165 DIGlobalVariable DG(GV);
3166 Scope = getOrCreateScope(DG.getContext().getGV());
3167 } else {
3168 // or GV is a local variable.
3169 DIVariable DV(GV);
3170 Scope = getOrCreateScope(DV.getContext().getGV());
3171 }
3172 assert (Scope && "Unable to find variable' scope");
Devang Patelccca7fe2009-01-12 19:17:34 +00003173 DIVariable *VD = new DIVariable(GV);
3174 DbgVariable *DV = new DbgVariable(VD, FrameIndex);
3175 Scope->AddVariable(DV);
3176 }
Jim Laskey65195462006-10-30 13:35:07 +00003177};
3178
Jim Laskey072200c2007-01-29 18:51:14 +00003179//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003180/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00003181///
3182class DwarfException : public Dwarf {
3183
Jim Laskeyb82313f2007-02-01 16:31:34 +00003184private:
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003185 struct FunctionEHFrameInfo {
3186 std::string FnName;
3187 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003188 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003189 bool hasCalls;
3190 bool hasLandingPads;
3191 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003192 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00003193
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003194 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3195 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003196 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003197 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003198 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003199 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003200 };
3201
3202 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003203
3204 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3205 /// be emitted.
3206 bool shouldEmitTable;
3207
3208 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3209 /// should be emitted.
3210 bool shouldEmitMoves;
3211
3212 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3213 /// should be emitted.
3214 bool shouldEmitTableModule;
3215
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003216 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003217 /// should be emitted.
3218 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00003219
Jim Laskey3f09fc22007-02-28 18:38:31 +00003220 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3221 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003222 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003223 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003224 int stackGrowth =
3225 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3226 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003227 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003228
Jim Laskeybacd3042007-02-21 22:48:45 +00003229 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003230 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling722f5f12008-12-24 08:05:17 +00003231
3232 if (!TAI->doesRequireNonLocalEHFrameLabel())
3233 O << TAI->getEHGlobalPrefix();
3234 O << "EH_frame" << Index << ":\n";
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003235 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003236
Jim Laskeybacd3042007-02-21 22:48:45 +00003237 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003238 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003239
Jim Laskeybacd3042007-02-21 22:48:45 +00003240 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003241 EmitDifference("eh_frame_common_end", Index,
3242 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003243 Asm->EOL("Length of Common Information Entry");
3244
Jim Laskeybacd3042007-02-21 22:48:45 +00003245 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003246 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003247 Asm->EmitInt32((int)0);
3248 Asm->EOL("CIE Identifier Tag");
3249 Asm->EmitInt8(DW_CIE_VERSION);
3250 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00003251
Jim Laskeybacd3042007-02-21 22:48:45 +00003252 // The personality presence indicates that language specific information
3253 // will show up in the eh frame.
3254 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00003255 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00003256
Jim Laskeybacd3042007-02-21 22:48:45 +00003257 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003258 Asm->EmitULEB128Bytes(1);
3259 Asm->EOL("CIE Code Alignment Factor");
3260 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00003261 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003262 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00003263 Asm->EOL("CIE Return Address Column");
3264
Jim Laskeybacd3042007-02-21 22:48:45 +00003265 // If there is a personality, we need to indicate the functions location.
3266 if (Personality) {
3267 Asm->EmitULEB128Bytes(7);
3268 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00003269
Duncan Sands671fa972008-05-07 19:11:09 +00003270 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003271 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00003272 Asm->EOL("Personality (pcrel sdata4 indirect)");
3273 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003274 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00003275 Asm->EOL("Personality (pcrel sdata4)");
3276 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00003277
Duncan Sands671fa972008-05-07 19:11:09 +00003278 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00003279 O << TAI->getPersonalityPrefix();
3280 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3281 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00003282 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3283 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00003284 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00003285
Duncan Sands671fa972008-05-07 19:11:09 +00003286 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3287 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003288
Bill Wendlingd60de512009-01-05 22:53:45 +00003289 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3290 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003291 } else {
3292 Asm->EmitULEB128Bytes(1);
3293 Asm->EOL("Augmentation Size");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003294
Bill Wendlingd60de512009-01-05 22:53:45 +00003295 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3296 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003297 }
3298
3299 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003300 std::vector<MachineMove> Moves;
3301 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00003302 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003303
Dale Johannesen21d972a2008-04-30 00:43:29 +00003304 // On Darwin the linker honors the alignment of eh_frame, which means it
3305 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3306 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003307 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003308 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003309 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003310
Jim Laskeybacd3042007-02-21 22:48:45 +00003311 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003312 }
Duncan Sands671fa972008-05-07 19:11:09 +00003313
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003314 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003315 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003316 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003317 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3318
Jim Laskeybacd3042007-02-21 22:48:45 +00003319 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3320
3321 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003322 // If the corresponding function is static, this should not be
3323 // externally visible.
Rafael Espindolabb46f522009-01-15 20:18:42 +00003324 if (linkage != Function::InternalLinkage &&
3325 linkage != Function::PrivateLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003326 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3327 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3328 }
3329
Dale Johannesen038129d2008-01-10 02:03:30 +00003330 // If corresponding function is weak definition, this should be too.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003331 if ((linkage == Function::WeakLinkage ||
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003332 linkage == Function::LinkOnceLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00003333 TAI->getWeakDefDirective())
3334 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3335
3336 // If there are no calls then you can't unwind. This may mean we can
3337 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003338 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00003339 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00003340 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00003341 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00003342 !UnwindTablesMandatory &&
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003343 ((linkage != Function::WeakLinkage &&
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003344 linkage != Function::LinkOnceLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00003345 !TAI->getWeakDefDirective() ||
3346 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003347 {
Bill Wendling6e198962007-09-18 01:47:22 +00003348 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003349 // This name has no connection to the function, so it might get
3350 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003351 // dead-stripping unconditionally.
3352 if (const char *UsedDirective = TAI->getUsedDirective())
3353 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00003354 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00003355 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003356
Jim Laskeybacd3042007-02-21 22:48:45 +00003357 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003358 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3359 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003360 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003361
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003362 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003363
Bill Wendling722f5f12008-12-24 08:05:17 +00003364 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3365 PrintRelDirective(true, true);
3366 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3367
3368 if (!TAI->isAbsoluteEHSectionOffsets())
3369 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3370 } else {
3371 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3372 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3373 true, true, false);
3374 }
3375
Jim Laskeybacd3042007-02-21 22:48:45 +00003376 Asm->EOL("FDE CIE offset");
3377
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003378 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003379 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003380 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003381 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003382 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00003383
Jim Laskey3f09fc22007-02-28 18:38:31 +00003384 // If there is a personality and landing pads then point to the language
3385 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003386 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00003387 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00003388 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00003389
3390 if (EHFrameInfo.hasLandingPads)
3391 EmitReference("exception", EHFrameInfo.Number, true, true);
3392 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00003393 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00003394 Asm->EOL("Language Specific Data Area");
3395 } else {
3396 Asm->EmitULEB128Bytes(0);
3397 Asm->EOL("Augmentation size");
3398 }
Duncan Sands671fa972008-05-07 19:11:09 +00003399
Jim Laskeybacd3042007-02-21 22:48:45 +00003400 // Indicate locations of function specific callee saved registers in
3401 // frame.
Dale Johannesenb97aec62007-11-13 19:13:01 +00003402 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003403
Dale Johannesen21d972a2008-04-30 00:43:29 +00003404 // On Darwin the linker honors the alignment of eh_frame, which means it
3405 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3406 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003407 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003408 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003409 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003410
3411 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003412 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003413 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003414 // that depends on unused functions (calling undefined externals) being
3415 // dead-stripped to link correctly. Yes, there really is.
3416 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3417 if (const char *UsedDirective = TAI->getUsedDirective())
3418 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3419 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003420 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003421
Duncan Sands57810cd2007-09-05 11:27:52 +00003422 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003423 ///
3424 /// The general organization of the table is complex, but the basic concepts
3425 /// are easy. First there is a header which describes the location and
3426 /// organization of the three components that follow.
3427 /// 1. The landing pad site information describes the range of code covered
3428 /// by the try. In our case it's an accumulation of the ranges covered
3429 /// by the invokes in the try. There is also a reference to the landing
3430 /// pad that handles the exception once processed. Finally an index into
3431 /// the actions table.
3432 /// 2. The action table, in our case, is composed of pairs of type ids
3433 /// and next action offset. Starting with the action index from the
3434 /// landing pad site, each type Id is checked for a match to the current
3435 /// exception. If it matches then the exception and type id are passed
3436 /// on to the landing pad. Otherwise the next action is looked up. This
3437 /// chain is terminated with a next action of zero. If no type id is
3438 /// found the the frame is unwound and handling continues.
3439 /// 3. Type id table contains references to all the C++ typeinfo for all
3440 /// catches in the function. This tables is reversed indexed base 1.
3441
Duncan Sandsb32edb42007-06-06 15:37:31 +00003442 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3443 static unsigned SharedTypeIds(const LandingPadInfo *L,
3444 const LandingPadInfo *R) {
3445 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003446 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003447 unsigned MinSize = LSize < RSize ? LSize : RSize;
3448 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003449
Duncan Sandsb32edb42007-06-06 15:37:31 +00003450 for (; Count != MinSize; ++Count)
3451 if (LIds[Count] != RIds[Count])
3452 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003453
Duncan Sandsb32edb42007-06-06 15:37:31 +00003454 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003455 }
3456
Duncan Sandsb32edb42007-06-06 15:37:31 +00003457 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00003458 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003459 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003460 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003461 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003462
Duncan Sandsb32edb42007-06-06 15:37:31 +00003463 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00003464 if (LIds[i] != RIds[i])
3465 return LIds[i] < RIds[i];
3466
Duncan Sandsb32edb42007-06-06 15:37:31 +00003467 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003468 }
3469
Duncan Sands53c3a332007-05-16 12:12:23 +00003470 struct KeyInfo {
3471 static inline unsigned getEmptyKey() { return -1U; }
3472 static inline unsigned getTombstoneKey() { return -2U; }
3473 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00003474 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00003475 static bool isPod() { return true; }
3476 };
3477
Duncan Sands57810cd2007-09-05 11:27:52 +00003478 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003479 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003480 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003481 int NextAction;
3482 struct ActionEntry *Previous;
3483 };
3484
Duncan Sands57810cd2007-09-05 11:27:52 +00003485 /// PadRange - Structure holding a try-range and the associated landing pad.
3486 struct PadRange {
3487 // The index of the landing pad.
3488 unsigned PadIndex;
3489 // The index of the begin and end labels in the landing pad's label lists.
3490 unsigned RangeIndex;
3491 };
3492
3493 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3494
3495 /// CallSiteEntry - Structure describing an entry in the call-site table.
3496 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00003497 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003498 unsigned BeginLabel; // zero indicates the start of the function.
3499 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00003500 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003501 unsigned PadLabel; // zero indicates that there is no landing pad.
3502 unsigned Action;
3503 };
3504
Jim Laskeybacd3042007-02-21 22:48:45 +00003505 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00003506 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00003507 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00003508 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3509 if (PadInfos.empty()) return;
3510
3511 // Sort the landing pads in order of their type ids. This is used to fold
3512 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00003513 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00003514 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00003515 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00003516 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003517 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3518
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003519 // Negative type ids index into FilterIds, positive type ids index into
3520 // TypeInfos. The value written for a positive type id is just the type
3521 // id itself. For a negative type id, however, the value written is the
3522 // (negative) byte offset of the corresponding FilterIds entry. The byte
3523 // offset is usually equal to the type id, because the FilterIds entries
3524 // are written using a variable width encoding which outputs one byte per
3525 // entry as long as the value written is not too large, but can differ.
3526 // This kind of complication does not occur for positive type ids because
3527 // type infos are output using a fixed width encoding.
3528 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3529 SmallVector<int, 16> FilterOffsets;
3530 FilterOffsets.reserve(FilterIds.size());
3531 int Offset = -1;
3532 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3533 E = FilterIds.end(); I != E; ++I) {
3534 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003535 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003536 }
3537
Duncan Sands57810cd2007-09-05 11:27:52 +00003538 // Compute the actions table and gather the first action index for each
3539 // landing pad site.
3540 SmallVector<ActionEntry, 32> Actions;
3541 SmallVector<unsigned, 64> FirstActions;
3542 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00003543
Duncan Sandsb32edb42007-06-06 15:37:31 +00003544 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00003545 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003546 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003547 const LandingPadInfo *LP = LandingPads[i];
3548 const std::vector<int> &TypeIds = LP->TypeIds;
3549 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003550 unsigned SizeSiteActions = 0;
3551
Duncan Sandsb32edb42007-06-06 15:37:31 +00003552 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00003553 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00003554 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003555
Duncan Sandsb32edb42007-06-06 15:37:31 +00003556 if (NumShared) {
3557 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3558 assert(Actions.size());
3559 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003560 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3561 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003562 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003563 SizeAction -=
3564 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003565 SizeAction += -PrevAction->NextAction;
3566 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003567 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00003568 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003569
Duncan Sandsb32edb42007-06-06 15:37:31 +00003570 // Compute the actions.
3571 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3572 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003573 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3574 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003575 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003576
3577 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003578 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003579 SizeSiteActions += SizeAction;
3580
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003581 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00003582 Actions.push_back(Action);
3583
3584 PrevAction = &Actions.back();
3585 }
3586
3587 // Record the first action of the landing pad site.
3588 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3589 } // else identical - re-use previous FirstAction
3590
3591 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003592
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00003593 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00003594 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00003595 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003596
Duncan Sands481dc722007-12-19 07:36:31 +00003597 // Compute the call-site table. The entry for an invoke has a try-range
3598 // containing the call, a non-zero landing pad and an appropriate action.
3599 // The entry for an ordinary call has a try-range containing the call and
3600 // zero for the landing pad and the action. Calls marked 'nounwind' have
3601 // no entry and must not be contained in the try-range of any entry - they
3602 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003603 SmallVector<CallSiteEntry, 64> CallSites;
3604
3605 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00003606 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3607 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3608 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00003609 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3610 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00003611 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003612 unsigned BeginLabel = LandingPad->BeginLabels[j];
3613 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3614 PadRange P = { i, j };
3615 PadMap[BeginLabel] = P;
3616 }
3617 }
3618
Duncan Sands481dc722007-12-19 07:36:31 +00003619 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00003620 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00003621
3622 // Whether there is a potentially throwing instruction (currently this means
3623 // an ordinary call) between the end of the previous try-range and now.
3624 bool SawPotentiallyThrowing = false;
3625
3626 // Whether the last callsite entry was for an invoke.
3627 bool PreviousIsInvoke = false;
3628
Duncan Sands481dc722007-12-19 07:36:31 +00003629 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003630 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3631 I != E; ++I) {
3632 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3633 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00003634 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00003635 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00003636 continue;
3637 }
3638
Chris Lattner9e330492007-12-30 20:50:28 +00003639 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00003640 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00003641
Duncan Sands481dc722007-12-19 07:36:31 +00003642 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00003643 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00003644 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003645
Duncan Sands481dc722007-12-19 07:36:31 +00003646 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00003647 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00003648 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00003649 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00003650 continue;
3651
3652 PadRange P = L->second;
3653 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3654
3655 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3656 "Inconsistent landing pad map!");
3657
3658 // If some instruction between the previous try-range and this one may
3659 // throw, create a call-site entry with no landing pad for the region
3660 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00003661 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003662 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3663 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00003664 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003665 }
3666
3667 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00003668 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00003669
Duncan Sands481dc722007-12-19 07:36:31 +00003670 if (LandingPad->LandingPadLabel) {
3671 // This try-range is for an invoke.
3672 CallSiteEntry Site = {BeginLabel, LastLabel,
3673 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00003674
Duncan Sands481dc722007-12-19 07:36:31 +00003675 // Try to merge with the previous call-site.
3676 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00003677 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00003678 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3679 // Extend the range of the previous entry.
3680 Prev.EndLabel = Site.EndLabel;
3681 continue;
3682 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003683 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003684
Duncan Sands481dc722007-12-19 07:36:31 +00003685 // Otherwise, create a new call-site.
3686 CallSites.push_back(Site);
3687 PreviousIsInvoke = true;
3688 } else {
3689 // Create a gap.
3690 PreviousIsInvoke = false;
3691 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003692 }
3693 }
3694 // If some instruction between the previous try-range and the end of the
3695 // function may throw, create a call-site entry with no landing pad for the
3696 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00003697 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003698 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3699 CallSites.push_back(Site);
3700 }
3701
Jim Laskeybacd3042007-02-21 22:48:45 +00003702 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00003703
3704 // Call sites.
3705 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
3706 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
3707 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
3708 unsigned SizeSites = CallSites.size() * (SiteStartSize +
3709 SiteLengthSize +
3710 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00003711 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003712 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00003713
Duncan Sands671fa972008-05-07 19:11:09 +00003714 // Type infos.
3715 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3716 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003717
3718 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003719 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003720 SizeSites + SizeActions + SizeTypes;
3721
3722 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3723 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003724 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003725 TypeOffset;
3726
3727 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003728
Jim Laskeybacd3042007-02-21 22:48:45 +00003729 // Begin the exception table.
3730 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng05548eb2008-02-29 19:36:59 +00003731 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesend65b2642008-10-08 21:50:21 +00003732 O << "GCC_except_table" << SubprogramCount << ":\n";
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003733 for (unsigned i = 0; i != SizeAlign; ++i) {
3734 Asm->EmitInt8(0);
3735 Asm->EOL("Padding");
3736 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003737 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003738
Jim Laskeybacd3042007-02-21 22:48:45 +00003739 // Emit the header.
3740 Asm->EmitInt8(DW_EH_PE_omit);
3741 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3742 Asm->EmitInt8(DW_EH_PE_absptr);
3743 Asm->EOL("TType format (DW_EH_PE_absptr)");
3744 Asm->EmitULEB128Bytes(TypeOffset);
3745 Asm->EOL("TType base offset");
3746 Asm->EmitInt8(DW_EH_PE_udata4);
3747 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3748 Asm->EmitULEB128Bytes(SizeSites);
3749 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00003750
Duncan Sands57810cd2007-09-05 11:27:52 +00003751 // Emit the landing pad site information.
3752 for (unsigned i = 0; i < CallSites.size(); ++i) {
3753 CallSiteEntry &S = CallSites[i];
3754 const char *BeginTag;
3755 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00003756
Duncan Sands57810cd2007-09-05 11:27:52 +00003757 if (!S.BeginLabel) {
3758 BeginTag = "eh_func_begin";
3759 BeginNumber = SubprogramCount;
3760 } else {
3761 BeginTag = "label";
3762 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00003763 }
Duncan Sands53c3a332007-05-16 12:12:23 +00003764
Duncan Sands57810cd2007-09-05 11:27:52 +00003765 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00003766 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003767 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00003768
Duncan Sands57810cd2007-09-05 11:27:52 +00003769 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00003770 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00003771 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003772 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00003773 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003774 }
3775 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00003776
Duncan Sands671fa972008-05-07 19:11:09 +00003777 if (!S.PadLabel)
3778 Asm->EmitInt32(0);
3779 else
Duncan Sands57810cd2007-09-05 11:27:52 +00003780 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00003781 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003782 Asm->EOL("Landing pad");
3783
3784 Asm->EmitULEB128Bytes(S.Action);
3785 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00003786 }
Duncan Sands53c3a332007-05-16 12:12:23 +00003787
Jim Laskeybacd3042007-02-21 22:48:45 +00003788 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003789 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
3790 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00003791
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003792 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003793 Asm->EOL("TypeInfo index");
3794 Asm->EmitSLEB128Bytes(Action.NextAction);
3795 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00003796 }
3797
3798 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00003799 for (unsigned M = TypeInfos.size(); M; --M) {
3800 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00003801
3802 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00003803
3804 if (GV)
3805 O << Asm->getGlobalLinkName(GV);
3806 else
3807 O << "0";
Duncan Sands57810cd2007-09-05 11:27:52 +00003808
Jim Laskeybacd3042007-02-21 22:48:45 +00003809 Asm->EOL("TypeInfo");
3810 }
3811
Duncan Sands73ef58a2007-06-02 16:53:42 +00003812 // Emit the filter typeids.
3813 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
3814 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00003815 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00003816 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00003817 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003818
Evan Cheng05548eb2008-02-29 19:36:59 +00003819 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003820 }
3821
Jim Laskey072200c2007-01-29 18:51:14 +00003822public:
3823 //===--------------------------------------------------------------------===//
3824 // Main entry points.
3825 //
Owen Andersoncb371882008-08-21 00:14:44 +00003826 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00003827 : Dwarf(OS, A, T, "eh")
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003828 , shouldEmitTable(false)
3829 , shouldEmitMoves(false)
3830 , shouldEmitTableModule(false)
3831 , shouldEmitMovesModule(false)
Jim Laskey072200c2007-01-29 18:51:14 +00003832 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003833
Jim Laskey072200c2007-01-29 18:51:14 +00003834 virtual ~DwarfException() {}
3835
3836 /// SetModuleInfo - Set machine module information when it's known that pass
3837 /// manager has created it. Set by the target AsmPrinter.
3838 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00003839 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00003840 }
3841
3842 /// BeginModule - Emit all exception information that should come prior to the
3843 /// content.
3844 void BeginModule(Module *M) {
3845 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00003846 }
3847
3848 /// EndModule - Emit all exception information that should come after the
3849 /// content.
3850 void EndModule() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003851 if (shouldEmitMovesModule || shouldEmitTableModule) {
3852 const std::vector<Function *> Personalities = MMI->getPersonalities();
3853 for (unsigned i =0; i < Personalities.size(); ++i)
3854 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003855
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003856 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
3857 E = EHFrames.end(); I != E; ++I)
3858 EmitEHFrame(*I);
3859 }
Jim Laskey072200c2007-01-29 18:51:14 +00003860 }
3861
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003862 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00003863 /// emitted immediately after the function entry point.
3864 void BeginFunction(MachineFunction *MF) {
3865 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003866 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesene0040622008-04-02 17:04:45 +00003867 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003868
3869 // Map all labels and get rid of any dead landing pads.
3870 MMI->TidyLandingPads();
3871 // If any landing pads survive, we need an EH table.
3872 if (MMI->getLandingPads().size())
3873 shouldEmitTable = true;
3874
3875 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00003876 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003877 shouldEmitMoves = true;
3878
3879 if (shouldEmitMoves || shouldEmitTable)
3880 // Assumes in correct section after the entry point.
3881 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00003882 }
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003883 shouldEmitTableModule |= shouldEmitTable;
3884 shouldEmitMovesModule |= shouldEmitMoves;
Jim Laskey072200c2007-01-29 18:51:14 +00003885 }
3886
3887 /// EndFunction - Gather and emit post-function exception information.
3888 ///
3889 void EndFunction() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003890 if (shouldEmitMoves || shouldEmitTable) {
3891 EmitLabel("eh_func_end", SubprogramCount);
3892 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003893
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003894 // Save EH frame information
3895 EHFrames.
3896 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendling6e198962007-09-18 01:47:22 +00003897 SubprogramCount,
3898 MMI->getPersonalityIndex(),
3899 MF->getFrameInfo()->hasCalls(),
3900 !MMI->getLandingPads().empty(),
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003901 MMI->getFrameMoves(),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003902 MF->getFunction()));
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003903 }
Jim Laskey072200c2007-01-29 18:51:14 +00003904 }
3905};
3906
Jim Laskey0d086af2006-02-27 12:43:29 +00003907} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00003908
3909//===----------------------------------------------------------------------===//
3910
Jim Laskeyd18e2892006-01-20 20:34:06 +00003911/// Emit - Print the abbreviation using the specified Dwarf writer.
3912///
Jim Laskey072200c2007-01-29 18:51:14 +00003913void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00003914 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00003915 DD.getAsm()->EmitULEB128Bytes(Tag);
3916 DD.getAsm()->EOL(TagString(Tag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003917
Jim Laskeyd18e2892006-01-20 20:34:06 +00003918 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00003919 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
3920 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003921
Jim Laskeyd18e2892006-01-20 20:34:06 +00003922 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00003923 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00003924 const DIEAbbrevData &AttrData = Data[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003925
Jim Laskeyd18e2892006-01-20 20:34:06 +00003926 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00003927 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
3928 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003929
Jim Laskeyd18e2892006-01-20 20:34:06 +00003930 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00003931 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
3932 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00003933 }
3934
3935 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00003936 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
3937 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00003938}
3939
3940#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00003941void DIEAbbrev::print(std::ostream &O) {
3942 O << "Abbreviation @"
3943 << std::hex << (intptr_t)this << std::dec
3944 << " "
3945 << TagString(Tag)
3946 << " "
3947 << ChildrenString(ChildrenFlag)
3948 << "\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003949
Jim Laskeya0f3d172006-09-07 22:06:40 +00003950 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3951 O << " "
3952 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00003953 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00003954 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00003955 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00003956 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00003957}
Bill Wendlinge8156192006-12-07 01:30:32 +00003958void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00003959#endif
3960
3961//===----------------------------------------------------------------------===//
3962
Jim Laskeyef42a012006-11-02 20:12:39 +00003963#ifndef NDEBUG
3964void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00003965 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003966}
Jim Laskeyef42a012006-11-02 20:12:39 +00003967#endif
3968
3969//===----------------------------------------------------------------------===//
3970
Jim Laskey063e7652006-01-17 17:31:53 +00003971/// EmitValue - Emit integer of appropriate size.
3972///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003973void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00003974 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00003975 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00003976 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003977 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003978 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003979 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003980 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003981 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003982 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003983 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
3984 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
3985 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003986 default: assert(0 && "DIE Value form not supported yet"); break;
3987 }
3988}
3989
3990/// SizeOf - Determine size of integer value in bytes.
3991///
Jim Laskey072200c2007-01-29 18:51:14 +00003992unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003993 switch (Form) {
3994 case DW_FORM_flag: // Fall thru
3995 case DW_FORM_ref1: // Fall thru
3996 case DW_FORM_data1: return sizeof(int8_t);
3997 case DW_FORM_ref2: // Fall thru
3998 case DW_FORM_data2: return sizeof(int16_t);
3999 case DW_FORM_ref4: // Fall thru
4000 case DW_FORM_data4: return sizeof(int32_t);
4001 case DW_FORM_ref8: // Fall thru
4002 case DW_FORM_data8: return sizeof(int64_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004003 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4004 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00004005 default: assert(0 && "DIE Value form not supported yet"); break;
4006 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004007 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00004008}
4009
Jim Laskey063e7652006-01-17 17:31:53 +00004010//===----------------------------------------------------------------------===//
4011
4012/// EmitValue - Emit string value.
4013///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004014void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004015 DD.getAsm()->EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00004016}
4017
Jim Laskey063e7652006-01-17 17:31:53 +00004018//===----------------------------------------------------------------------===//
4019
4020/// EmitValue - Emit label value.
4021///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004022void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004023 bool IsSmall = Form == DW_FORM_data4;
4024 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004025}
4026
4027/// SizeOf - Determine size of label value in bytes.
4028///
Jim Laskey072200c2007-01-29 18:51:14 +00004029unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004030 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004031 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004032}
Jim Laskeyef42a012006-11-02 20:12:39 +00004033
Jim Laskey063e7652006-01-17 17:31:53 +00004034//===----------------------------------------------------------------------===//
4035
Jim Laskeyd18e2892006-01-20 20:34:06 +00004036/// EmitValue - Emit label value.
4037///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004038void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004039 bool IsSmall = Form == DW_FORM_data4;
4040 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00004041}
4042
4043/// SizeOf - Determine size of label value in bytes.
4044///
Jim Laskey072200c2007-01-29 18:51:14 +00004045unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004046 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004047 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00004048}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004049
Jim Laskeyd18e2892006-01-20 20:34:06 +00004050//===----------------------------------------------------------------------===//
4051
Jim Laskey063e7652006-01-17 17:31:53 +00004052/// EmitValue - Emit delta value.
4053///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004054void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4055 bool IsSmall = Form == DW_FORM_data4;
4056 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4057 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4058}
4059
4060/// SizeOf - Determine size of delta value in bytes.
4061///
4062unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4063 if (Form == DW_FORM_data4) return 4;
4064 return DD.getTargetData()->getPointerSize();
4065}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004066
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004067//===----------------------------------------------------------------------===//
4068
4069/// EmitValue - Emit delta value.
4070///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004071void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004072 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00004073 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004074}
4075
4076/// SizeOf - Determine size of delta value in bytes.
4077///
Jim Laskey072200c2007-01-29 18:51:14 +00004078unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004079 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004080 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004081}
4082
4083//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004084
Jim Laskeyb8509c52006-03-23 18:07:55 +00004085/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004086///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004087void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004088 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00004089}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004090
Jim Laskeyd18e2892006-01-20 20:34:06 +00004091//===----------------------------------------------------------------------===//
4092
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004093/// ComputeSize - calculate the size of the block.
4094///
Jim Laskey072200c2007-01-29 18:51:14 +00004095unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00004096 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00004097 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004098
Jim Laskeyef42a012006-11-02 20:12:39 +00004099 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00004100 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00004101 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004102 }
4103 return Size;
4104}
4105
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004106/// EmitValue - Emit block data.
4107///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004108void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004109 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004110 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4111 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4112 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4113 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004114 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004115 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004116
Owen Anderson873e1b52008-06-24 21:44:59 +00004117 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00004118
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004119 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00004120 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00004121 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004122 }
4123}
4124
4125/// SizeOf - Determine size of block data in bytes.
4126///
Jim Laskey072200c2007-01-29 18:51:14 +00004127unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004128 switch (Form) {
4129 case DW_FORM_block1: return Size + sizeof(int8_t);
4130 case DW_FORM_block2: return Size + sizeof(int16_t);
4131 case DW_FORM_block4: return Size + sizeof(int32_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004132 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004133 default: assert(0 && "Improper form for block"); break;
4134 }
4135 return 0;
4136}
4137
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004138//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004139/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00004140
4141DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00004142 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00004143 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00004144}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004145
Jim Laskeyb8509c52006-03-23 18:07:55 +00004146/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4147///
4148void DIE::AddSiblingOffset() {
4149 DIEInteger *DI = new DIEInteger(0);
4150 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00004151 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00004152}
4153
Jim Laskeyef42a012006-11-02 20:12:39 +00004154/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004155///
Jim Laskeyef42a012006-11-02 20:12:39 +00004156void DIE::Profile(FoldingSetNodeID &ID) {
4157 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004158
Jim Laskeyef42a012006-11-02 20:12:39 +00004159 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4160 ID.AddPointer(Children[i]);
4161
4162 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4163 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00004164}
Jim Laskeyef42a012006-11-02 20:12:39 +00004165
4166#ifndef NDEBUG
4167void DIE::print(std::ostream &O, unsigned IncIndent) {
4168 static unsigned IndentCount = 0;
4169 IndentCount += IncIndent;
4170 const std::string Indent(IndentCount, ' ');
4171 bool isBlock = Abbrev.getTag() == 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004172
Jim Laskeyef42a012006-11-02 20:12:39 +00004173 if (!isBlock) {
4174 O << Indent
4175 << "Die: "
4176 << "0x" << std::hex << (intptr_t)this << std::dec
4177 << ", Offset: " << Offset
4178 << ", Size: " << Size
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004179 << "\n";
4180
Jim Laskeyef42a012006-11-02 20:12:39 +00004181 O << Indent
4182 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00004183 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00004184 << ChildrenString(Abbrev.getChildrenFlag());
4185 } else {
4186 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00004187 }
4188 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00004189
Owen Anderson873e1b52008-06-24 21:44:59 +00004190 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004191
Jim Laskeyef42a012006-11-02 20:12:39 +00004192 IndentCount += 2;
4193 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4194 O << Indent;
Bill Wendling4e974012008-07-22 00:28:47 +00004195
4196 if (!isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +00004197 O << AttributeString(Data[i].getAttribute());
Bill Wendling4e974012008-07-22 00:28:47 +00004198 else
Jim Laskeyef42a012006-11-02 20:12:39 +00004199 O << "Blk[" << i << "]";
Bill Wendling4e974012008-07-22 00:28:47 +00004200
Jim Laskeyef42a012006-11-02 20:12:39 +00004201 O << " "
4202 << FormEncodingString(Data[i].getForm())
4203 << " ";
4204 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00004205 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00004206 }
Jim Laskeyef42a012006-11-02 20:12:39 +00004207 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00004208
Jim Laskeyef42a012006-11-02 20:12:39 +00004209 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4210 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00004211 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004212
Jim Laskeyef42a012006-11-02 20:12:39 +00004213 if (!isBlock) O << "\n";
4214 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00004215}
4216
Jim Laskeyef42a012006-11-02 20:12:39 +00004217void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004218 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00004219}
Jim Laskeybd761842006-02-27 17:27:12 +00004220#endif
Jim Laskey65195462006-10-30 13:35:07 +00004221
4222//===----------------------------------------------------------------------===//
4223/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00004224///
Jim Laskey65195462006-10-30 13:35:07 +00004225
Devang Pateleb3fc282009-01-08 23:40:34 +00004226DwarfWriter::DwarfWriter() : ImmutablePass(&ID), DD(NULL), DE(NULL) {
Jim Laskey65195462006-10-30 13:35:07 +00004227}
4228
4229DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00004230 delete DE;
4231 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00004232}
4233
Jim Laskey65195462006-10-30 13:35:07 +00004234/// BeginModule - Emit all Dwarf sections that should come prior to the
4235/// content.
Devang Pateleb3fc282009-01-08 23:40:34 +00004236void DwarfWriter::BeginModule(Module *M,
4237 MachineModuleInfo *MMI,
4238 raw_ostream &OS, AsmPrinter *A,
4239 const TargetAsmInfo *T) {
4240 DE = new DwarfException(OS, A, T);
4241 DD = new DwarfDebug(OS, A, T);
Jim Laskey072200c2007-01-29 18:51:14 +00004242 DE->BeginModule(M);
4243 DD->BeginModule(M);
Devang Patel7bb89ed2009-01-13 00:20:51 +00004244 DD->SetDebugInfo(MMI);
Devang Pateleb3fc282009-01-08 23:40:34 +00004245 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00004246}
4247
4248/// EndModule - Emit all Dwarf sections that should come after the content.
4249///
4250void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00004251 DE->EndModule();
4252 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00004253}
4254
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004255/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00004256/// emitted immediately after the function entry point.
4257void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00004258 DE->BeginFunction(MF);
4259 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00004260}
4261
4262/// EndFunction - Gather and emit post-function debug information.
4263///
Bill Wendlingd751c642008-09-26 00:28:12 +00004264void DwarfWriter::EndFunction(MachineFunction *MF) {
4265 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004266 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004267
Bill Wendlingc91d0b92008-07-22 00:53:37 +00004268 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00004269 // Clear function debug information.
4270 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00004271}
Devang Patelccca7fe2009-01-12 19:17:34 +00004272
Devang Patelcf3a4482009-01-15 23:41:32 +00004273/// ValidDebugInfo - Return true if V represents valid debug info value.
4274bool DwarfWriter::ValidDebugInfo(Value *V) {
4275 return DD->ValidDebugInfo(V);
4276}
4277
Devang Patelccca7fe2009-01-12 19:17:34 +00004278/// RecordSourceLine - Records location information and associates it with a
4279/// label. Returns a unique label ID used to generate a label and provide
4280/// correspondence to the source line list.
4281unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
4282 unsigned Src) {
4283 return DD->RecordSourceLine(Line, Col, Src);
4284}
4285
4286/// RecordSource - Register a source file with debug info. Returns an source
4287/// ID.
4288unsigned DwarfWriter::RecordSource(const std::string &Dir,
4289 const std::string &File) {
4290 return DD->RecordSource(Dir, File);
4291}
4292
4293/// RecordRegionStart - Indicate the start of a region.
4294unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
4295 return DD->RecordRegionStart(V);
4296}
4297
4298/// RecordRegionEnd - Indicate the end of a region.
4299unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
4300 return DD->RecordRegionEnd(V);
4301}
4302
4303/// getRecordSourceLineCount - Count source lines.
4304unsigned DwarfWriter::getRecordSourceLineCount() {
4305 return DD->getRecordSourceLineCount();
4306}
Devang Patelbb8c5952009-01-13 21:25:00 +00004307
Devang Patelc48c5502009-01-13 21:44:10 +00004308/// RecordVariable - Indicate the declaration of a local variable.
4309///
4310void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
4311 DD->RecordVariable(GV, FrameIndex);
4312}
Devang Patelbbdc8202009-01-13 23:54:55 +00004313