blob: eb2e448abc58de3e2baf8a1a1f8003d414ca9cfc [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf info into asm files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/DwarfWriter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000015#include "llvm/Module.h"
Devang Patelb3907da2009-01-05 23:03:32 +000016#include "llvm/DerivedTypes.h"
Devang Patel2da0cc42009-01-15 23:41:32 +000017#include "llvm/Constants.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/CodeGen/AsmPrinter.h"
19#include "llvm/CodeGen/MachineModuleInfo.h"
20#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineLocation.h"
Devang Patelfc187162009-01-05 17:57:47 +000022#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/Support/Debug.h"
24#include "llvm/Support/Dwarf.h"
25#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/DataTypes.h"
27#include "llvm/Support/Mangler.h"
Bill Wendlingcb3661f2009-03-10 20:41:52 +000028#include "llvm/Support/Timer.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000029#include "llvm/Support/raw_ostream.h"
Dan Gohman80bbde72007-09-24 21:32:18 +000030#include "llvm/System/Path.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000032#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/Target/TargetData.h"
34#include "llvm/Target/TargetFrameInfo.h"
35#include "llvm/Target/TargetInstrInfo.h"
36#include "llvm/Target/TargetMachine.h"
37#include "llvm/Target/TargetOptions.h"
Evan Cheng3e288912009-02-25 07:04:34 +000038#include "llvm/ADT/DenseMap.h"
39#include "llvm/ADT/FoldingSet.h"
40#include "llvm/ADT/StringExtras.h"
41#include "llvm/ADT/StringMap.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042#include <ostream>
43#include <string>
44using namespace llvm;
45using namespace llvm::dwarf;
46
Devang Patelaa1e8432009-01-08 23:40:34 +000047static RegisterPass<DwarfWriter>
48X("dwarfwriter", "DWARF Information Writer");
49char DwarfWriter::ID = 0;
50
Bill Wendlingcb3661f2009-03-10 20:41:52 +000051namespace {
52
53static TimerGroup *DwarfTimerGroup = 0;
54static TimerGroup *getDwarfTimerGroup() {
55 if (DwarfTimerGroup) return DwarfTimerGroup;
56 return DwarfTimerGroup = new TimerGroup("Dwarf Exception and Debugging");
57}
58
59} // end anonymous namespace
60
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061namespace llvm {
aslc200b112008-08-16 12:57:46 +000062
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063//===----------------------------------------------------------------------===//
64
65/// Configuration values for initial hash set sizes (log2).
66///
Bill Wendling824a8bf2009-02-03 21:17:20 +000067static const unsigned InitDiesSetSize = 9; // log2(512)
68static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
69static const unsigned InitValuesSetSize = 9; // log2(512)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070
71//===----------------------------------------------------------------------===//
72/// Forward declarations.
73///
74class DIE;
75class DIEValue;
76
77//===----------------------------------------------------------------------===//
Devang Patelb3907da2009-01-05 23:03:32 +000078/// Utility routines.
79///
Devang Patel2da0cc42009-01-15 23:41:32 +000080/// getGlobalVariable - Return either a direct or cast Global value.
81///
82static GlobalVariable *getGlobalVariable(Value *V) {
83 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
84 return GV;
85 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
86 if (CE->getOpcode() == Instruction::BitCast) {
87 return dyn_cast<GlobalVariable>(CE->getOperand(0));
88 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
89 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
90 if (!CE->getOperand(i)->isNullValue())
91 return NULL;
92 }
93 return dyn_cast<GlobalVariable>(CE->getOperand(0));
94 }
95 }
96 return NULL;
97}
98
Devang Patelb3907da2009-01-05 23:03:32 +000099//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100/// DWLabel - Labels are used to track locations in the assembler file.
aslc200b112008-08-16 12:57:46 +0000101/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
102/// where the tag is a category of label (Ex. location) and number is a value
Reid Spencer37c7cea2007-08-05 20:06:04 +0000103/// unique in that category.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104class DWLabel {
105public:
106 /// Tag - Label category tag. Should always be a staticly declared C string.
107 ///
108 const char *Tag;
aslc200b112008-08-16 12:57:46 +0000109
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 /// Number - Value to make label unique.
111 ///
112 unsigned Number;
113
114 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
aslc200b112008-08-16 12:57:46 +0000115
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 void Profile(FoldingSetNodeID &ID) const {
Evan Cheng3e288912009-02-25 07:04:34 +0000117 ID.AddString(Tag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 ID.AddInteger(Number);
119 }
aslc200b112008-08-16 12:57:46 +0000120
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121#ifndef NDEBUG
122 void print(std::ostream *O) const {
123 if (O) print(*O);
124 }
125 void print(std::ostream &O) const {
126 O << "." << Tag;
127 if (Number) O << Number;
128 }
129#endif
130};
131
132//===----------------------------------------------------------------------===//
133/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
134/// Dwarf abbreviation.
135class DIEAbbrevData {
136private:
137 /// Attribute - Dwarf attribute code.
138 ///
139 unsigned Attribute;
aslc200b112008-08-16 12:57:46 +0000140
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 /// Form - Dwarf form code.
aslc200b112008-08-16 12:57:46 +0000142 ///
143 unsigned Form;
144
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145public:
146 DIEAbbrevData(unsigned A, unsigned F)
147 : Attribute(A)
148 , Form(F)
149 {}
aslc200b112008-08-16 12:57:46 +0000150
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 // Accessors.
152 unsigned getAttribute() const { return Attribute; }
153 unsigned getForm() const { return Form; }
154
155 /// Profile - Used to gather unique data for the abbreviation folding set.
156 ///
157 void Profile(FoldingSetNodeID &ID)const {
158 ID.AddInteger(Attribute);
159 ID.AddInteger(Form);
160 }
161};
162
163//===----------------------------------------------------------------------===//
164/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
165/// information object.
166class DIEAbbrev : public FoldingSetNode {
167private:
168 /// Tag - Dwarf tag code.
169 ///
170 unsigned Tag;
aslc200b112008-08-16 12:57:46 +0000171
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 /// Unique number for node.
173 ///
174 unsigned Number;
175
176 /// ChildrenFlag - Dwarf children flag.
177 ///
178 unsigned ChildrenFlag;
179
180 /// Data - Raw data bytes for abbreviation.
181 ///
Owen Anderson88dd6232008-06-24 21:44:59 +0000182 SmallVector<DIEAbbrevData, 8> Data;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183
184public:
185
186 DIEAbbrev(unsigned T, unsigned C)
187 : Tag(T)
188 , ChildrenFlag(C)
189 , Data()
190 {}
191 ~DIEAbbrev() {}
aslc200b112008-08-16 12:57:46 +0000192
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 // Accessors.
194 unsigned getTag() const { return Tag; }
195 unsigned getNumber() const { return Number; }
196 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson88dd6232008-06-24 21:44:59 +0000197 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 void setTag(unsigned T) { Tag = T; }
199 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
200 void setNumber(unsigned N) { Number = N; }
aslc200b112008-08-16 12:57:46 +0000201
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 /// AddAttribute - Adds another set of attribute information to the
203 /// abbreviation.
204 void AddAttribute(unsigned Attribute, unsigned Form) {
205 Data.push_back(DIEAbbrevData(Attribute, Form));
206 }
aslc200b112008-08-16 12:57:46 +0000207
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 /// AddFirstAttribute - Adds a set of attribute information to the front
209 /// of the abbreviation.
210 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
211 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
212 }
aslc200b112008-08-16 12:57:46 +0000213
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 /// Profile - Used to gather unique data for the abbreviation folding set.
215 ///
216 void Profile(FoldingSetNodeID &ID) {
217 ID.AddInteger(Tag);
218 ID.AddInteger(ChildrenFlag);
aslc200b112008-08-16 12:57:46 +0000219
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000220 // For each attribute description.
221 for (unsigned i = 0, N = Data.size(); i < N; ++i)
222 Data[i].Profile(ID);
223 }
aslc200b112008-08-16 12:57:46 +0000224
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 /// Emit - Print the abbreviation using the specified Dwarf writer.
226 ///
aslc200b112008-08-16 12:57:46 +0000227 void Emit(const DwarfDebug &DD) const;
228
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229#ifndef NDEBUG
230 void print(std::ostream *O) {
231 if (O) print(*O);
232 }
233 void print(std::ostream &O);
234 void dump();
235#endif
236};
237
238//===----------------------------------------------------------------------===//
239/// DIE - A structured debug information entry. Has an abbreviation which
240/// describes it's organization.
241class DIE : public FoldingSetNode {
242protected:
243 /// Abbrev - Buffer for constructing abbreviation.
244 ///
245 DIEAbbrev Abbrev;
aslc200b112008-08-16 12:57:46 +0000246
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 /// Offset - Offset in debug info section.
248 ///
249 unsigned Offset;
aslc200b112008-08-16 12:57:46 +0000250
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 /// Size - Size of instance + children.
252 ///
253 unsigned Size;
aslc200b112008-08-16 12:57:46 +0000254
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 /// Children DIEs.
256 ///
257 std::vector<DIE *> Children;
aslc200b112008-08-16 12:57:46 +0000258
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 /// Attributes values.
260 ///
Owen Anderson88dd6232008-06-24 21:44:59 +0000261 SmallVector<DIEValue*, 32> Values;
aslc200b112008-08-16 12:57:46 +0000262
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000264 explicit DIE(unsigned Tag)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 : Abbrev(Tag, DW_CHILDREN_no)
266 , Offset(0)
267 , Size(0)
268 , Children()
269 , Values()
270 {}
271 virtual ~DIE();
aslc200b112008-08-16 12:57:46 +0000272
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 // Accessors.
274 DIEAbbrev &getAbbrev() { return Abbrev; }
275 unsigned getAbbrevNumber() const {
276 return Abbrev.getNumber();
277 }
278 unsigned getTag() const { return Abbrev.getTag(); }
279 unsigned getOffset() const { return Offset; }
280 unsigned getSize() const { return Size; }
281 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson88dd6232008-06-24 21:44:59 +0000282 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
284 void setOffset(unsigned O) { Offset = O; }
285 void setSize(unsigned S) { Size = S; }
aslc200b112008-08-16 12:57:46 +0000286
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 /// AddValue - Add a value and attributes to a DIE.
288 ///
289 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
290 Abbrev.AddAttribute(Attribute, Form);
291 Values.push_back(Value);
292 }
aslc200b112008-08-16 12:57:46 +0000293
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 /// SiblingOffset - Return the offset of the debug information entry's
295 /// sibling.
296 unsigned SiblingOffset() const { return Offset + Size; }
aslc200b112008-08-16 12:57:46 +0000297
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
299 ///
300 void AddSiblingOffset();
301
302 /// AddChild - Add a child to the DIE.
303 ///
304 void AddChild(DIE *Child) {
305 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
306 Children.push_back(Child);
307 }
aslc200b112008-08-16 12:57:46 +0000308
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 /// Detach - Detaches objects connected to it after copying.
310 ///
311 void Detach() {
312 Children.clear();
313 }
aslc200b112008-08-16 12:57:46 +0000314
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 /// Profile - Used to gather unique data for the value folding set.
316 ///
317 void Profile(FoldingSetNodeID &ID) ;
aslc200b112008-08-16 12:57:46 +0000318
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319#ifndef NDEBUG
320 void print(std::ostream *O, unsigned IncIndent = 0) {
321 if (O) print(*O, IncIndent);
322 }
323 void print(std::ostream &O, unsigned IncIndent = 0);
324 void dump();
325#endif
326};
327
328//===----------------------------------------------------------------------===//
329/// DIEValue - A debug information entry value.
330///
331class DIEValue : public FoldingSetNode {
332public:
333 enum {
334 isInteger,
335 isString,
336 isLabel,
337 isAsIsLabel,
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000338 isSectionOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 isDelta,
340 isEntry,
341 isBlock
342 };
aslc200b112008-08-16 12:57:46 +0000343
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 /// Type - Type of data stored in the value.
345 ///
346 unsigned Type;
aslc200b112008-08-16 12:57:46 +0000347
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000348 explicit DIEValue(unsigned T)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 : Type(T)
350 {}
351 virtual ~DIEValue() {}
aslc200b112008-08-16 12:57:46 +0000352
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 // Accessors
354 unsigned getType() const { return Type; }
aslc200b112008-08-16 12:57:46 +0000355
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356 // Implement isa/cast/dyncast.
357 static bool classof(const DIEValue *) { return true; }
aslc200b112008-08-16 12:57:46 +0000358
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359 /// EmitValue - Emit value via the Dwarf writer.
360 ///
361 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
aslc200b112008-08-16 12:57:46 +0000362
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363 /// SizeOf - Return the size of a value in bytes.
364 ///
365 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
aslc200b112008-08-16 12:57:46 +0000366
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 /// Profile - Used to gather unique data for the value folding set.
368 ///
369 virtual void Profile(FoldingSetNodeID &ID) = 0;
aslc200b112008-08-16 12:57:46 +0000370
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371#ifndef NDEBUG
372 void print(std::ostream *O) {
373 if (O) print(*O);
374 }
375 virtual void print(std::ostream &O) = 0;
376 void dump();
377#endif
378};
379
380//===----------------------------------------------------------------------===//
381/// DWInteger - An integer value DIE.
aslc200b112008-08-16 12:57:46 +0000382///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383class DIEInteger : public DIEValue {
384private:
385 uint64_t Integer;
aslc200b112008-08-16 12:57:46 +0000386
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000388 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000389
390 // Implement isa/cast/dyncast.
391 static bool classof(const DIEInteger *) { return true; }
392 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
aslc200b112008-08-16 12:57:46 +0000393
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 /// BestForm - Choose the best form for integer.
395 ///
396 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
397 if (IsSigned) {
398 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
399 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
400 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
401 } else {
402 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
403 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
404 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
405 }
406 return DW_FORM_data8;
407 }
aslc200b112008-08-16 12:57:46 +0000408
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409 /// EmitValue - Emit integer of appropriate size.
410 ///
411 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000412
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413 /// SizeOf - Determine size of integer value in bytes.
414 ///
415 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000416
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 /// Profile - Used to gather unique data for the value folding set.
418 ///
419 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
420 ID.AddInteger(isInteger);
421 ID.AddInteger(Integer);
422 }
423 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
aslc200b112008-08-16 12:57:46 +0000424
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425#ifndef NDEBUG
426 virtual void print(std::ostream &O) {
427 O << "Int: " << (int64_t)Integer
428 << " 0x" << std::hex << Integer << std::dec;
429 }
430#endif
431};
432
433//===----------------------------------------------------------------------===//
434/// DIEString - A string value DIE.
aslc200b112008-08-16 12:57:46 +0000435///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436class DIEString : public DIEValue {
437public:
438 const std::string String;
aslc200b112008-08-16 12:57:46 +0000439
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000440 explicit DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441
442 // Implement isa/cast/dyncast.
443 static bool classof(const DIEString *) { return true; }
444 static bool classof(const DIEValue *S) { return S->Type == isString; }
aslc200b112008-08-16 12:57:46 +0000445
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446 /// EmitValue - Emit string value.
447 ///
448 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000449
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 /// SizeOf - Determine size of string value in bytes.
451 ///
452 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
453 return String.size() + sizeof(char); // sizeof('\0');
454 }
aslc200b112008-08-16 12:57:46 +0000455
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 /// Profile - Used to gather unique data for the value folding set.
457 ///
458 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
459 ID.AddInteger(isString);
460 ID.AddString(String);
461 }
462 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
aslc200b112008-08-16 12:57:46 +0000463
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464#ifndef NDEBUG
465 virtual void print(std::ostream &O) {
466 O << "Str: \"" << String << "\"";
467 }
468#endif
469};
470
471//===----------------------------------------------------------------------===//
472/// DIEDwarfLabel - A Dwarf internal label expression DIE.
473//
474class DIEDwarfLabel : public DIEValue {
475public:
476
477 const DWLabel Label;
aslc200b112008-08-16 12:57:46 +0000478
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000479 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480
481 // Implement isa/cast/dyncast.
482 static bool classof(const DIEDwarfLabel *) { return true; }
483 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
aslc200b112008-08-16 12:57:46 +0000484
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 /// EmitValue - Emit label value.
486 ///
487 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000488
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000489 /// SizeOf - Determine size of label value in bytes.
490 ///
491 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000492
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493 /// Profile - Used to gather unique data for the value folding set.
494 ///
495 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
496 ID.AddInteger(isLabel);
497 Label.Profile(ID);
498 }
499 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
aslc200b112008-08-16 12:57:46 +0000500
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501#ifndef NDEBUG
502 virtual void print(std::ostream &O) {
503 O << "Lbl: ";
504 Label.print(O);
505 }
506#endif
507};
508
509
510//===----------------------------------------------------------------------===//
511/// DIEObjectLabel - A label to an object in code or data.
512//
513class DIEObjectLabel : public DIEValue {
514public:
515 const std::string Label;
aslc200b112008-08-16 12:57:46 +0000516
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000517 explicit DIEObjectLabel(const std::string &L)
518 : DIEValue(isAsIsLabel), Label(L) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519
520 // Implement isa/cast/dyncast.
521 static bool classof(const DIEObjectLabel *) { return true; }
522 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
aslc200b112008-08-16 12:57:46 +0000523
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 /// EmitValue - Emit label value.
525 ///
526 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000527
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528 /// SizeOf - Determine size of label value in bytes.
529 ///
530 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000531
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000532 /// Profile - Used to gather unique data for the value folding set.
533 ///
534 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
535 ID.AddInteger(isAsIsLabel);
536 ID.AddString(Label);
537 }
Evan Cheng3e288912009-02-25 07:04:34 +0000538 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label.c_str()); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539
540#ifndef NDEBUG
541 virtual void print(std::ostream &O) {
542 O << "Obj: " << Label;
543 }
544#endif
545};
546
547//===----------------------------------------------------------------------===//
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000548/// DIESectionOffset - A section offset DIE.
549//
550class DIESectionOffset : public DIEValue {
551public:
552 const DWLabel Label;
553 const DWLabel Section;
554 bool IsEH : 1;
555 bool UseSet : 1;
aslc200b112008-08-16 12:57:46 +0000556
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000557 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
558 bool isEH = false, bool useSet = true)
559 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
560 IsEH(isEH), UseSet(useSet) {}
561
562 // Implement isa/cast/dyncast.
563 static bool classof(const DIESectionOffset *) { return true; }
564 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
aslc200b112008-08-16 12:57:46 +0000565
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000566 /// EmitValue - Emit section offset.
567 ///
568 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000569
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000570 /// SizeOf - Determine size of section offset value in bytes.
571 ///
572 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000573
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000574 /// Profile - Used to gather unique data for the value folding set.
575 ///
576 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
577 const DWLabel &Section) {
578 ID.AddInteger(isSectionOffset);
579 Label.Profile(ID);
580 Section.Profile(ID);
581 // IsEH and UseSet are specific to the Label/Section that we will emit
582 // the offset for; so Label/Section are enough for uniqueness.
583 }
584 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
585
586#ifndef NDEBUG
587 virtual void print(std::ostream &O) {
588 O << "Off: ";
589 Label.print(O);
590 O << "-";
591 Section.print(O);
592 O << "-" << IsEH << "-" << UseSet;
593 }
594#endif
595};
596
597//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598/// DIEDelta - A simple label difference DIE.
aslc200b112008-08-16 12:57:46 +0000599///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600class DIEDelta : public DIEValue {
601public:
602 const DWLabel LabelHi;
603 const DWLabel LabelLo;
aslc200b112008-08-16 12:57:46 +0000604
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000605 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
606 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
607
608 // Implement isa/cast/dyncast.
609 static bool classof(const DIEDelta *) { return true; }
610 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
aslc200b112008-08-16 12:57:46 +0000611
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 /// EmitValue - Emit delta value.
613 ///
614 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000615
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616 /// SizeOf - Determine size of delta value in bytes.
617 ///
618 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000619
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620 /// Profile - Used to gather unique data for the value folding set.
621 ///
622 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
623 const DWLabel &LabelLo) {
624 ID.AddInteger(isDelta);
625 LabelHi.Profile(ID);
626 LabelLo.Profile(ID);
627 }
628 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
629
630#ifndef NDEBUG
631 virtual void print(std::ostream &O) {
632 O << "Del: ";
633 LabelHi.print(O);
634 O << "-";
635 LabelLo.print(O);
636 }
637#endif
638};
639
640//===----------------------------------------------------------------------===//
641/// DIEntry - A pointer to another debug information entry. An instance of this
642/// class can also be used as a proxy for a debug information entry not yet
643/// defined (ie. types.)
644class DIEntry : public DIEValue {
645public:
646 DIE *Entry;
aslc200b112008-08-16 12:57:46 +0000647
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000648 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
aslc200b112008-08-16 12:57:46 +0000649
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650 // Implement isa/cast/dyncast.
651 static bool classof(const DIEntry *) { return true; }
652 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
aslc200b112008-08-16 12:57:46 +0000653
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000654 /// EmitValue - Emit debug information entry offset.
655 ///
656 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000657
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658 /// SizeOf - Determine size of debug information entry in bytes.
659 ///
660 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
661 return sizeof(int32_t);
662 }
aslc200b112008-08-16 12:57:46 +0000663
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664 /// Profile - Used to gather unique data for the value folding set.
665 ///
666 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
667 ID.AddInteger(isEntry);
668 ID.AddPointer(Entry);
669 }
670 virtual void Profile(FoldingSetNodeID &ID) {
671 ID.AddInteger(isEntry);
aslc200b112008-08-16 12:57:46 +0000672
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000673 if (Entry) {
674 ID.AddPointer(Entry);
675 } else {
676 ID.AddPointer(this);
677 }
678 }
aslc200b112008-08-16 12:57:46 +0000679
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680#ifndef NDEBUG
681 virtual void print(std::ostream &O) {
682 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
683 }
684#endif
685};
686
687//===----------------------------------------------------------------------===//
688/// DIEBlock - A block of values. Primarily used for location expressions.
689//
690class DIEBlock : public DIEValue, public DIE {
691public:
692 unsigned Size; // Size in bytes excluding size header.
aslc200b112008-08-16 12:57:46 +0000693
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000694 DIEBlock()
695 : DIEValue(isBlock)
696 , DIE(0)
697 , Size(0)
698 {}
699 ~DIEBlock() {
700 }
aslc200b112008-08-16 12:57:46 +0000701
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000702 // Implement isa/cast/dyncast.
703 static bool classof(const DIEBlock *) { return true; }
704 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
aslc200b112008-08-16 12:57:46 +0000705
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706 /// ComputeSize - calculate the size of the block.
707 ///
708 unsigned ComputeSize(DwarfDebug &DD);
aslc200b112008-08-16 12:57:46 +0000709
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000710 /// BestForm - Choose the best form for data.
711 ///
712 unsigned BestForm() const {
713 if ((unsigned char)Size == Size) return DW_FORM_block1;
714 if ((unsigned short)Size == Size) return DW_FORM_block2;
715 if ((unsigned int)Size == Size) return DW_FORM_block4;
716 return DW_FORM_block;
717 }
718
719 /// EmitValue - Emit block data.
720 ///
721 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000722
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723 /// SizeOf - Determine size of block data in bytes.
724 ///
725 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000726
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000727
728 /// Profile - Used to gather unique data for the value folding set.
729 ///
730 virtual void Profile(FoldingSetNodeID &ID) {
731 ID.AddInteger(isBlock);
732 DIE::Profile(ID);
733 }
aslc200b112008-08-16 12:57:46 +0000734
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000735#ifndef NDEBUG
736 virtual void print(std::ostream &O) {
737 O << "Blk: ";
738 DIE::print(O, 5);
739 }
740#endif
741};
742
743//===----------------------------------------------------------------------===//
744/// CompileUnit - This dwarf writer support class manages information associate
745/// with a source file.
746class CompileUnit {
747private:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000748 /// ID - File identifier for source.
749 ///
750 unsigned ID;
aslc200b112008-08-16 12:57:46 +0000751
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752 /// Die - Compile unit debug information entry.
753 ///
754 DIE *Die;
aslc200b112008-08-16 12:57:46 +0000755
Devang Patel42f6bed2009-01-13 23:54:55 +0000756 /// GVToDieMap - Tracks the mapping of unit level debug informaton
757 /// variables to debug information entries.
Devang Patel56b1d132009-01-20 00:58:55 +0000758 std::map<GlobalVariable *, DIE *> GVToDieMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000759
Devang Patel42f6bed2009-01-13 23:54:55 +0000760 /// GVToDIEntryMap - Tracks the mapping of unit level debug informaton
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000761 /// descriptors to debug information entries using a DIEntry proxy.
Devang Patel56b1d132009-01-20 00:58:55 +0000762 std::map<GlobalVariable *, DIEntry *> GVToDIEntryMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000763
764 /// Globals - A map of globally visible named entities for this unit.
765 ///
766 std::map<std::string, DIE *> Globals;
767
768 /// DiesSet - Used to uniquely define dies within the compile unit.
769 ///
770 FoldingSet<DIE> DiesSet;
aslc200b112008-08-16 12:57:46 +0000771
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000772public:
Devang Patelb3907da2009-01-05 23:03:32 +0000773 CompileUnit(unsigned I, DIE *D)
Devang Patel42f6bed2009-01-13 23:54:55 +0000774 : ID(I), Die(D), GVToDieMap(),
Devang Patel5302e672009-01-17 06:51:37 +0000775 GVToDIEntryMap(), Globals(), DiesSet(InitDiesSetSize)
Devang Patelb3907da2009-01-05 23:03:32 +0000776 {}
777
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000778 ~CompileUnit() {
779 delete Die;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000780 }
aslc200b112008-08-16 12:57:46 +0000781
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000782 // Accessors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000783 unsigned getID() const { return ID; }
784 DIE* getDie() const { return Die; }
785 std::map<std::string, DIE *> &getGlobals() { return Globals; }
786
787 /// hasContent - Return true if this compile unit has something to write out.
788 ///
789 bool hasContent() const {
790 return !Die->getChildren().empty();
791 }
792
793 /// AddGlobal - Add a new global entity to the compile unit.
794 ///
795 void AddGlobal(const std::string &Name, DIE *Die) {
796 Globals[Name] = Die;
797 }
aslc200b112008-08-16 12:57:46 +0000798
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000799 /// getDieMapSlotFor - Returns the debug information entry map slot for the
Devang Patel42f6bed2009-01-13 23:54:55 +0000800 /// specified debug variable.
Devang Patel4a4cbe72009-01-05 21:47:57 +0000801 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
802 return GVToDieMap[GV];
803 }
aslc200b112008-08-16 12:57:46 +0000804
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000805 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
Devang Patel42f6bed2009-01-13 23:54:55 +0000806 /// specified debug variable.
Devang Patel4a4cbe72009-01-05 21:47:57 +0000807 DIEntry *&getDIEntrySlotFor(GlobalVariable *GV) {
808 return GVToDIEntryMap[GV];
809 }
aslc200b112008-08-16 12:57:46 +0000810
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000811 /// AddDie - Adds or interns the DIE to the compile unit.
812 ///
813 DIE *AddDie(DIE &Buffer) {
814 FoldingSetNodeID ID;
815 Buffer.Profile(ID);
816 void *Where;
817 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
aslc200b112008-08-16 12:57:46 +0000818
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000819 if (!Die) {
820 Die = new DIE(Buffer);
821 DiesSet.InsertNode(Die, Where);
822 this->Die->AddChild(Die);
823 Buffer.Detach();
824 }
aslc200b112008-08-16 12:57:46 +0000825
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000826 return Die;
827 }
828};
829
830//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +0000831/// Dwarf - Emits general Dwarf directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000832///
833class Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000834protected:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000835 //===--------------------------------------------------------------------===//
836 // Core attributes used by the Dwarf writer.
837 //
aslc200b112008-08-16 12:57:46 +0000838
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000839 //
840 /// O - Stream to .s file.
841 ///
Owen Anderson847b99b2008-08-21 00:14:44 +0000842 raw_ostream &O;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000843
844 /// Asm - Target of Dwarf emission.
845 ///
846 AsmPrinter *Asm;
aslc200b112008-08-16 12:57:46 +0000847
Bill Wendlingac9639d2008-07-01 23:34:48 +0000848 /// TAI - Target asm information.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000849 const TargetAsmInfo *TAI;
aslc200b112008-08-16 12:57:46 +0000850
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000851 /// TD - Target data.
852 const TargetData *TD;
aslc200b112008-08-16 12:57:46 +0000853
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000854 /// RI - Register Information.
Dan Gohman1e57df32008-02-10 18:45:23 +0000855 const TargetRegisterInfo *RI;
aslc200b112008-08-16 12:57:46 +0000856
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000857 /// M - Current module.
858 ///
859 Module *M;
aslc200b112008-08-16 12:57:46 +0000860
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000861 /// MF - Current machine function.
862 ///
863 MachineFunction *MF;
aslc200b112008-08-16 12:57:46 +0000864
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000865 /// MMI - Collected machine module information.
866 ///
867 MachineModuleInfo *MMI;
aslc200b112008-08-16 12:57:46 +0000868
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000869 /// SubprogramCount - The running count of functions being compiled.
870 ///
871 unsigned SubprogramCount;
aslc200b112008-08-16 12:57:46 +0000872
Chris Lattnerb3876c72007-09-24 03:35:37 +0000873 /// Flavor - A unique string indicating what dwarf producer this is, used to
874 /// unique labels.
875 const char * const Flavor;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000876
877 unsigned SetCounter;
Owen Anderson847b99b2008-08-21 00:14:44 +0000878 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattnerb3876c72007-09-24 03:35:37 +0000879 const char *flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000880 : O(OS)
881 , Asm(A)
882 , TAI(T)
883 , TD(Asm->TM.getTargetData())
884 , RI(Asm->TM.getRegisterInfo())
885 , M(NULL)
886 , MF(NULL)
887 , MMI(NULL)
888 , SubprogramCount(0)
Chris Lattnerb3876c72007-09-24 03:35:37 +0000889 , Flavor(flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000890 , SetCounter(1)
891 {
892 }
893
894public:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000895 //===--------------------------------------------------------------------===//
896 // Accessors.
897 //
898 AsmPrinter *getAsm() const { return Asm; }
899 MachineModuleInfo *getMMI() const { return MMI; }
900 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohmancfb72b22007-09-27 23:12:31 +0000901 const TargetData *getTargetData() const { return TD; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000902
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000903 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
904 const {
905 if (isInSection && TAI->getDwarfSectionOffsetDirective())
906 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohmancfb72b22007-09-27 23:12:31 +0000907 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000908 O << TAI->getData32bitsDirective();
909 else
910 O << TAI->getData64bitsDirective();
911 }
aslc200b112008-08-16 12:57:46 +0000912
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000913 /// PrintLabelName - Print label name in form used by Dwarf writer.
914 ///
915 void PrintLabelName(DWLabel Label) const {
916 PrintLabelName(Label.Tag, Label.Number);
917 }
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000918 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000919 O << TAI->getPrivateGlobalPrefix() << Tag;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000920 if (Number) O << Number;
921 }
aslc200b112008-08-16 12:57:46 +0000922
Chris Lattnerb3876c72007-09-24 03:35:37 +0000923 void PrintLabelName(const char *Tag, unsigned Number,
924 const char *Suffix) const {
925 O << TAI->getPrivateGlobalPrefix() << Tag;
926 if (Number) O << Number;
927 O << Suffix;
928 }
aslc200b112008-08-16 12:57:46 +0000929
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000930 /// EmitLabel - Emit location label for internal use by Dwarf.
931 ///
932 void EmitLabel(DWLabel Label) const {
933 EmitLabel(Label.Tag, Label.Number);
934 }
935 void EmitLabel(const char *Tag, unsigned Number) const {
936 PrintLabelName(Tag, Number);
937 O << ":\n";
938 }
aslc200b112008-08-16 12:57:46 +0000939
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000940 /// EmitReference - Emit a reference to a label.
941 ///
Dan Gohman4fd77742007-09-28 15:43:33 +0000942 void EmitReference(DWLabel Label, bool IsPCRelative = false,
943 bool Force32Bit = false) const {
944 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000945 }
946 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman4fd77742007-09-28 15:43:33 +0000947 bool IsPCRelative = false, bool Force32Bit = false) const {
948 PrintRelDirective(Force32Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000949 PrintLabelName(Tag, Number);
aslc200b112008-08-16 12:57:46 +0000950
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000951 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
952 }
Dan Gohman4fd77742007-09-28 15:43:33 +0000953 void EmitReference(const std::string &Name, bool IsPCRelative = false,
954 bool Force32Bit = false) const {
955 PrintRelDirective(Force32Bit);
aslc200b112008-08-16 12:57:46 +0000956
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000957 O << Name;
aslc200b112008-08-16 12:57:46 +0000958
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000959 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
960 }
961
962 /// EmitDifference - Emit the difference between two labels. Some
963 /// assemblers do not behave with absolute expressions with data directives,
964 /// so there is an option (needsSet) to use an intermediary set expression.
965 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
966 bool IsSmall = false) {
967 EmitDifference(LabelHi.Tag, LabelHi.Number,
968 LabelLo.Tag, LabelLo.Number,
969 IsSmall);
970 }
971 void EmitDifference(const char *TagHi, unsigned NumberHi,
972 const char *TagLo, unsigned NumberLo,
973 bool IsSmall = false) {
974 if (TAI->needsSet()) {
975 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +0000976 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000977 O << ",";
978 PrintLabelName(TagHi, NumberHi);
979 O << "-";
980 PrintLabelName(TagLo, NumberLo);
981 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000982
983 PrintRelDirective(IsSmall);
Chris Lattnerb3876c72007-09-24 03:35:37 +0000984 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000985 ++SetCounter;
986 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000987 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +0000988
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000989 PrintLabelName(TagHi, NumberHi);
990 O << "-";
991 PrintLabelName(TagLo, NumberLo);
992 }
993 }
994
995 void EmitSectionOffset(const char* Label, const char* Section,
996 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesen0ebb2432008-03-26 23:31:39 +0000997 bool IsSmall = false, bool isEH = false,
998 bool useSet = true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000999 bool printAbsolute = false;
Dale Johannesen0ebb2432008-03-26 23:31:39 +00001000 if (isEH)
1001 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
1002 else
1003 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
1004
1005 if (TAI->needsSet() && useSet) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001006 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +00001007 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001008 O << ",";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001009 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001010
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001011 if (!printAbsolute) {
1012 O << "-";
1013 PrintLabelName(Section, SectionNumber);
aslc200b112008-08-16 12:57:46 +00001014 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001015 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001016
1017 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +00001018
Chris Lattnerb3876c72007-09-24 03:35:37 +00001019 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001020 ++SetCounter;
1021 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001022 PrintRelDirective(IsSmall, true);
aslc200b112008-08-16 12:57:46 +00001023
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001024 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001025
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001026 if (!printAbsolute) {
1027 O << "-";
1028 PrintLabelName(Section, SectionNumber);
1029 }
aslc200b112008-08-16 12:57:46 +00001030 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001031 }
aslc200b112008-08-16 12:57:46 +00001032
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001033 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1034 /// frame.
1035 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenf5a11532007-11-13 19:13:01 +00001036 const std::vector<MachineMove> &Moves, bool isEH) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001037 int stackGrowth =
1038 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1039 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00001040 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001041 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
1042
1043 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
1044 const MachineMove &Move = Moves[i];
1045 unsigned LabelID = Move.getLabelID();
aslc200b112008-08-16 12:57:46 +00001046
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001047 if (LabelID) {
1048 LabelID = MMI->MappedLabel(LabelID);
aslc200b112008-08-16 12:57:46 +00001049
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001050 // Throw out move if the label is invalid.
1051 if (!LabelID) continue;
1052 }
aslc200b112008-08-16 12:57:46 +00001053
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001054 const MachineLocation &Dst = Move.getDestination();
1055 const MachineLocation &Src = Move.getSource();
aslc200b112008-08-16 12:57:46 +00001056
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001057 // Advance row if new location.
1058 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1059 Asm->EmitInt8(DW_CFA_advance_loc4);
1060 Asm->EOL("DW_CFA_advance_loc4");
1061 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1062 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00001063
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001064 BaseLabelID = LabelID;
1065 BaseLabel = "label";
1066 IsLocal = true;
1067 }
aslc200b112008-08-16 12:57:46 +00001068
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001069 // If advancing cfa.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001070 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1071 if (!Src.isReg()) {
1072 if (Src.getReg() == MachineLocation::VirtualFP) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001073 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1074 Asm->EOL("DW_CFA_def_cfa_offset");
1075 } else {
1076 Asm->EmitInt8(DW_CFA_def_cfa);
1077 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001078 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001079 Asm->EOL("Register");
1080 }
aslc200b112008-08-16 12:57:46 +00001081
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001082 int Offset = -Src.getOffset();
aslc200b112008-08-16 12:57:46 +00001083
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001084 Asm->EmitULEB128Bytes(Offset);
1085 Asm->EOL("Offset");
1086 } else {
1087 assert(0 && "Machine move no supported yet.");
1088 }
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001089 } else if (Src.isReg() &&
1090 Src.getReg() == MachineLocation::VirtualFP) {
1091 if (Dst.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001092 Asm->EmitInt8(DW_CFA_def_cfa_register);
1093 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001094 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001095 Asm->EOL("Register");
1096 } else {
1097 assert(0 && "Machine move no supported yet.");
1098 }
1099 } else {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001100 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001101 int Offset = Dst.getOffset() / stackGrowth;
aslc200b112008-08-16 12:57:46 +00001102
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001103 if (Offset < 0) {
1104 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1105 Asm->EOL("DW_CFA_offset_extended_sf");
1106 Asm->EmitULEB128Bytes(Reg);
1107 Asm->EOL("Reg");
1108 Asm->EmitSLEB128Bytes(Offset);
1109 Asm->EOL("Offset");
1110 } else if (Reg < 64) {
1111 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Cheng6181e062008-07-09 21:53:02 +00001112 if (VerboseAsm)
1113 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1114 else
1115 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001116 Asm->EmitULEB128Bytes(Offset);
1117 Asm->EOL("Offset");
1118 } else {
1119 Asm->EmitInt8(DW_CFA_offset_extended);
1120 Asm->EOL("DW_CFA_offset_extended");
1121 Asm->EmitULEB128Bytes(Reg);
1122 Asm->EOL("Reg");
1123 Asm->EmitULEB128Bytes(Offset);
1124 Asm->EOL("Offset");
1125 }
1126 }
1127 }
1128 }
1129
1130};
1131
1132//===----------------------------------------------------------------------===//
Devang Patel35a078f2009-01-12 22:54:42 +00001133/// SrcLineInfo - This class is used to record source line correspondence.
Devang Patel7dd15a92009-01-08 17:19:22 +00001134///
1135class SrcLineInfo {
1136 unsigned Line; // Source line number.
1137 unsigned Column; // Source column.
1138 unsigned SourceID; // Source ID number.
1139 unsigned LabelID; // Label in code ID number.
1140public:
1141 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
Bill Wendling824a8bf2009-02-03 21:17:20 +00001142 : Line(L), Column(C), SourceID(S), LabelID(I) {}
Devang Patel7dd15a92009-01-08 17:19:22 +00001143
1144 // Accessors
1145 unsigned getLine() const { return Line; }
1146 unsigned getColumn() const { return Column; }
1147 unsigned getSourceID() const { return SourceID; }
1148 unsigned getLabelID() const { return LabelID; }
1149};
1150
Devang Patel7dd15a92009-01-08 17:19:22 +00001151//===----------------------------------------------------------------------===//
Devang Patel4d1709e2009-01-08 02:33:41 +00001152/// DbgVariable - This class is used to track local variable information.
1153///
1154class DbgVariable {
Devang Patel7c8a2772009-01-16 19:28:14 +00001155 DIVariable Var; // Variable Descriptor.
Devang Patel4d1709e2009-01-08 02:33:41 +00001156 unsigned FrameIndex; // Variable frame index.
Devang Patel4d1709e2009-01-08 02:33:41 +00001157public:
Devang Patel7c8a2772009-01-16 19:28:14 +00001158 DbgVariable(DIVariable V, unsigned I) : Var(V), FrameIndex(I) {}
Devang Patel4d1709e2009-01-08 02:33:41 +00001159
1160 // Accessors.
Devang Patel7c8a2772009-01-16 19:28:14 +00001161 DIVariable getVariable() const { return Var; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001162 unsigned getFrameIndex() const { return FrameIndex; }
1163};
1164
1165//===----------------------------------------------------------------------===//
1166/// DbgScope - This class is used to track scope information.
1167///
1168class DbgScope {
Devang Patel4d1709e2009-01-08 02:33:41 +00001169 DbgScope *Parent; // Parent to this scope.
Devang Patel49a3bd92009-01-16 18:01:58 +00001170 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel4d1709e2009-01-08 02:33:41 +00001171 // Either subprogram or block.
1172 unsigned StartLabelID; // Label ID of the beginning of scope.
1173 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel49a3bd92009-01-16 18:01:58 +00001174 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
Devang Patel63c22f42009-01-10 02:42:49 +00001175 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Devang Patel4d1709e2009-01-08 02:33:41 +00001176public:
Devang Patel2560d922009-01-15 18:25:17 +00001177 DbgScope(DbgScope *P, DIDescriptor D)
Devang Patel4d1709e2009-01-08 02:33:41 +00001178 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), Scopes(), Variables()
1179 {}
Devang Patela4162952009-01-12 18:48:36 +00001180 ~DbgScope() {
1181 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1182 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1183 }
Devang Patel4d1709e2009-01-08 02:33:41 +00001184
1185 // Accessors.
Devang Patel49a3bd92009-01-16 18:01:58 +00001186 DbgScope *getParent() const { return Parent; }
1187 DIDescriptor getDesc() const { return Desc; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001188 unsigned getStartLabelID() const { return StartLabelID; }
1189 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel63c22f42009-01-10 02:42:49 +00001190 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1191 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001192 void setStartLabelID(unsigned S) { StartLabelID = S; }
1193 void setEndLabelID(unsigned E) { EndLabelID = E; }
1194
1195 /// AddScope - Add a scope to the scope.
1196 ///
1197 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1198
1199 /// AddVariable - Add a variable to the scope.
1200 ///
1201 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
1202};
1203
1204//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00001205/// DwarfDebug - Emits Dwarf debug directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001206///
1207class DwarfDebug : public Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001208 //===--------------------------------------------------------------------===//
1209 // Attributes used to construct specific Dwarf sections.
1210 //
aslc200b112008-08-16 12:57:46 +00001211
Evan Cheng3e288912009-02-25 07:04:34 +00001212 /// CompileUnitMap - A map of global variables representing compile units to
1213 /// compile units.
1214 DenseMap<Value *, CompileUnit *> CompileUnitMap;
1215
1216 /// CompileUnits - All the compile units in this module.
1217 ///
1218 SmallVector<CompileUnit *, 8> CompileUnits;
aslc200b112008-08-16 12:57:46 +00001219
Devang Patel2ae1db52009-01-30 18:20:31 +00001220 /// MainCU - Some platform prefers one compile unit per .o file. In such
1221 /// cases, all dies are inserted in MainCU.
1222 CompileUnit *MainCU;
Bill Wendlinge0f3a262009-02-20 20:40:28 +00001223
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001224 /// AbbreviationsSet - Used to uniquely define abbreviations.
1225 ///
1226 FoldingSet<DIEAbbrev> AbbreviationsSet;
1227
1228 /// Abbreviations - A list of all the unique abbreviations in use.
1229 ///
1230 std::vector<DIEAbbrev *> Abbreviations;
aslc200b112008-08-16 12:57:46 +00001231
Evan Cheng3e288912009-02-25 07:04:34 +00001232 /// DirectoryIdMap - Directory name to directory id map.
1233 ///
1234 StringMap<unsigned> DirectoryIdMap;
Devang Patel5f244e32009-01-05 22:35:52 +00001235
Evan Cheng3e288912009-02-25 07:04:34 +00001236 /// DirectoryNames - A list of directory names.
1237 SmallVector<std::string, 8> DirectoryNames;
1238
1239 /// SourceFileIdMap - Source file name to source file id map.
1240 ///
1241 StringMap<unsigned> SourceFileIdMap;
1242
1243 /// SourceFileNames - A list of source file names.
1244 SmallVector<std::string, 8> SourceFileNames;
1245
1246 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
1247 /// id mapped to a unique id.
1248 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
1249
1250 /// SourceIds - Reverse map from source id to directory id + file id pair.
1251 ///
1252 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
Devang Patel5f244e32009-01-05 22:35:52 +00001253
Devang Patel9b829452009-01-16 21:07:53 +00001254 /// Lines - List of of source line correspondence.
Devang Patel7dd15a92009-01-08 17:19:22 +00001255 std::vector<SrcLineInfo> Lines;
1256
Devang Patel9b829452009-01-16 21:07:53 +00001257 /// ValuesSet - Used to uniquely define values.
1258 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001259 FoldingSet<DIEValue> ValuesSet;
aslc200b112008-08-16 12:57:46 +00001260
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001261 /// Values - A list of all the unique values in use.
1262 ///
1263 std::vector<DIEValue *> Values;
aslc200b112008-08-16 12:57:46 +00001264
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001265 /// StringPool - A UniqueVector of strings used by indirect references.
1266 ///
1267 UniqueVector<std::string> StringPool;
1268
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001269 /// SectionMap - Provides a unique id per text section.
1270 ///
Anton Korobeynikov55b94962008-09-24 22:15:21 +00001271 UniqueVector<const Section*> SectionMap;
aslc200b112008-08-16 12:57:46 +00001272
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001273 /// SectionSourceLines - Tracks line numbers per text section.
1274 ///
Devang Patel35a078f2009-01-12 22:54:42 +00001275 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001276
1277 /// didInitial - Flag to indicate if initial emission has been done.
1278 ///
1279 bool didInitial;
aslc200b112008-08-16 12:57:46 +00001280
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001281 /// shouldEmit - Flag to indicate if debug information should be emitted.
1282 ///
1283 bool shouldEmit;
1284
Devang Patel2560d922009-01-15 18:25:17 +00001285 // RootDbgScope - Top level scope for the current function.
Devang Patel4d1709e2009-01-08 02:33:41 +00001286 //
1287 DbgScope *RootDbgScope;
1288
Bill Wendlingd9308a62009-03-10 21:23:25 +00001289 /// DbgScopeMap - Tracks the scopes in the current function.
Devang Patel4d1709e2009-01-08 02:33:41 +00001290 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
Bill Wendlingd9308a62009-03-10 21:23:25 +00001291
1292 /// DebugTimer - Timer for the Dwarf debug writer.
1293 Timer *DebugTimer;
Devang Patel4d1709e2009-01-08 02:33:41 +00001294
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001295 struct FunctionDebugFrameInfo {
1296 unsigned Number;
1297 std::vector<MachineMove> Moves;
1298
1299 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman9ba5d4d2007-08-27 14:50:10 +00001300 Number(Num), Moves(M) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001301 };
1302
1303 std::vector<FunctionDebugFrameInfo> DebugFrames;
aslc200b112008-08-16 12:57:46 +00001304
Bill Wendlingd9308a62009-03-10 21:23:25 +00001305private:
Bill Wendling278a3922009-03-10 21:47:45 +00001306 /// getSourceDirsectoryAndFileIds - Return the directory and file ids that
1307 /// maps to the source id. Source id starts at 1.
1308 std::pair<unsigned, unsigned>
1309 getSourceDirsectoryAndFileIds(unsigned SId) const {
1310 return SourceIds[SId-1];
1311 }
1312
1313 /// getNumSourceDirectories - Return the number of source directories in the
1314 /// debug info.
1315 unsigned getNumSourceDirectories() const {
1316 return DirectoryNames.size();
1317 }
1318
1319 /// getSourceDirectoryName - Return the name of the directory corresponding
1320 /// to the id.
1321 const std::string &getSourceDirectoryName(unsigned Id) const {
1322 return DirectoryNames[Id - 1];
1323 }
1324
1325 /// getSourceFileName - Return the name of the source file corresponding
1326 /// to the id.
1327 const std::string &getSourceFileName(unsigned Id) const {
1328 return SourceFileNames[Id - 1];
1329 }
1330
1331 /// getNumSourceIds - Return the number of unique source ids.
1332 ///
1333 unsigned getNumSourceIds() const {
1334 return SourceIds.size();
1335 }
1336
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001337 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
aslc200b112008-08-16 12:57:46 +00001338 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001339 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1340 // Profile the node so that we can make it unique.
1341 FoldingSetNodeID ID;
1342 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00001343
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001344 // Check the set for priors.
1345 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
aslc200b112008-08-16 12:57:46 +00001346
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001347 // If it's newly added.
1348 if (InSet == &Abbrev) {
aslc200b112008-08-16 12:57:46 +00001349 // Add to abbreviation list.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001350 Abbreviations.push_back(&Abbrev);
1351 // Assign the vector position + 1 as its number.
1352 Abbrev.setNumber(Abbreviations.size());
1353 } else {
1354 // Assign existing abbreviation number.
1355 Abbrev.setNumber(InSet->getNumber());
1356 }
1357 }
1358
1359 /// NewString - Add a string to the constant pool and returns a label.
1360 ///
1361 DWLabel NewString(const std::string &String) {
1362 unsigned StringID = StringPool.insert(String);
1363 return DWLabel("string", StringID);
1364 }
aslc200b112008-08-16 12:57:46 +00001365
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001366 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1367 /// entry.
1368 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1369 DIEntry *Value;
aslc200b112008-08-16 12:57:46 +00001370
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001371 if (Entry) {
1372 FoldingSetNodeID ID;
1373 DIEntry::Profile(ID, Entry);
1374 void *Where;
1375 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
aslc200b112008-08-16 12:57:46 +00001376
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001377 if (Value) return Value;
aslc200b112008-08-16 12:57:46 +00001378
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001379 Value = new DIEntry(Entry);
1380 ValuesSet.InsertNode(Value, Where);
1381 } else {
1382 Value = new DIEntry(Entry);
1383 }
aslc200b112008-08-16 12:57:46 +00001384
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001385 Values.push_back(Value);
1386 return Value;
1387 }
aslc200b112008-08-16 12:57:46 +00001388
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001389 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1390 ///
1391 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1392 Value->Entry = Entry;
1393 // Add to values set if not already there. If it is, we merely have a
1394 // duplicate in the values list (no harm.)
1395 ValuesSet.GetOrInsertNode(Value);
1396 }
1397
1398 /// AddUInt - Add an unsigned integer attribute data and value.
1399 ///
1400 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1401 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1402
1403 FoldingSetNodeID ID;
1404 DIEInteger::Profile(ID, Integer);
1405 void *Where;
1406 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1407 if (!Value) {
1408 Value = new DIEInteger(Integer);
1409 ValuesSet.InsertNode(Value, Where);
1410 Values.push_back(Value);
1411 }
aslc200b112008-08-16 12:57:46 +00001412
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001413 Die->AddValue(Attribute, Form, Value);
1414 }
aslc200b112008-08-16 12:57:46 +00001415
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001416 /// AddSInt - Add an signed integer attribute data and value.
1417 ///
1418 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1419 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1420
1421 FoldingSetNodeID ID;
1422 DIEInteger::Profile(ID, (uint64_t)Integer);
1423 void *Where;
1424 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1425 if (!Value) {
1426 Value = new DIEInteger(Integer);
1427 ValuesSet.InsertNode(Value, Where);
1428 Values.push_back(Value);
1429 }
aslc200b112008-08-16 12:57:46 +00001430
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001431 Die->AddValue(Attribute, Form, Value);
1432 }
aslc200b112008-08-16 12:57:46 +00001433
Evan Cheng3e288912009-02-25 07:04:34 +00001434 /// AddString - Add a string attribute data and value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001435 ///
1436 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1437 const std::string &String) {
1438 FoldingSetNodeID ID;
1439 DIEString::Profile(ID, String);
1440 void *Where;
1441 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1442 if (!Value) {
1443 Value = new DIEString(String);
1444 ValuesSet.InsertNode(Value, Where);
1445 Values.push_back(Value);
1446 }
aslc200b112008-08-16 12:57:46 +00001447
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001448 Die->AddValue(Attribute, Form, Value);
1449 }
aslc200b112008-08-16 12:57:46 +00001450
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001451 /// AddLabel - Add a Dwarf label attribute data and value.
1452 ///
1453 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1454 const DWLabel &Label) {
1455 FoldingSetNodeID ID;
1456 DIEDwarfLabel::Profile(ID, Label);
1457 void *Where;
1458 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1459 if (!Value) {
1460 Value = new DIEDwarfLabel(Label);
1461 ValuesSet.InsertNode(Value, Where);
1462 Values.push_back(Value);
1463 }
aslc200b112008-08-16 12:57:46 +00001464
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001465 Die->AddValue(Attribute, Form, Value);
1466 }
aslc200b112008-08-16 12:57:46 +00001467
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001468 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1469 ///
1470 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1471 const std::string &Label) {
1472 FoldingSetNodeID ID;
1473 DIEObjectLabel::Profile(ID, Label);
1474 void *Where;
1475 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1476 if (!Value) {
1477 Value = new DIEObjectLabel(Label);
1478 ValuesSet.InsertNode(Value, Where);
1479 Values.push_back(Value);
1480 }
aslc200b112008-08-16 12:57:46 +00001481
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001482 Die->AddValue(Attribute, Form, Value);
1483 }
aslc200b112008-08-16 12:57:46 +00001484
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001485 /// AddSectionOffset - Add a section offset label attribute data and value.
1486 ///
1487 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1488 const DWLabel &Label, const DWLabel &Section,
1489 bool isEH = false, bool useSet = true) {
1490 FoldingSetNodeID ID;
1491 DIESectionOffset::Profile(ID, Label, Section);
1492 void *Where;
1493 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1494 if (!Value) {
1495 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1496 ValuesSet.InsertNode(Value, Where);
1497 Values.push_back(Value);
1498 }
aslc200b112008-08-16 12:57:46 +00001499
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001500 Die->AddValue(Attribute, Form, Value);
1501 }
aslc200b112008-08-16 12:57:46 +00001502
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001503 /// AddDelta - Add a label delta attribute data and value.
1504 ///
1505 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1506 const DWLabel &Hi, const DWLabel &Lo) {
1507 FoldingSetNodeID ID;
1508 DIEDelta::Profile(ID, Hi, Lo);
1509 void *Where;
1510 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1511 if (!Value) {
1512 Value = new DIEDelta(Hi, Lo);
1513 ValuesSet.InsertNode(Value, Where);
1514 Values.push_back(Value);
1515 }
aslc200b112008-08-16 12:57:46 +00001516
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001517 Die->AddValue(Attribute, Form, Value);
1518 }
aslc200b112008-08-16 12:57:46 +00001519
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001520 /// AddDIEntry - Add a DIE attribute data and value.
1521 ///
1522 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1523 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1524 }
1525
1526 /// AddBlock - Add block data.
1527 ///
1528 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1529 Block->ComputeSize(*this);
1530 FoldingSetNodeID ID;
1531 Block->Profile(ID);
1532 void *Where;
1533 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1534 if (!Value) {
1535 Value = Block;
1536 ValuesSet.InsertNode(Value, Where);
1537 Values.push_back(Value);
1538 } else {
Chris Lattner3de66892007-09-21 18:25:53 +00001539 // Already exists, reuse the previous one.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001540 delete Block;
Chris Lattner3de66892007-09-21 18:25:53 +00001541 Block = cast<DIEBlock>(Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001542 }
aslc200b112008-08-16 12:57:46 +00001543
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001544 Die->AddValue(Attribute, Block->BestForm(), Value);
1545 }
1546
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001547 /// AddSourceLine - Add location information to specified debug information
1548 /// entry.
Devang Patel7c8a2772009-01-16 19:28:14 +00001549 void AddSourceLine(DIE *Die, const DIVariable *V) {
Devang Patel4d1709e2009-01-08 02:33:41 +00001550 unsigned FileID = 0;
1551 unsigned Line = V->getLineNumber();
Devang Patel2ae1db52009-01-30 18:20:31 +00001552 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1553 FileID = Unit->getID();
Devang Pateld94b6932009-02-10 06:04:08 +00001554 assert (FileID && "Invalid file id");
Devang Patel4d1709e2009-01-08 02:33:41 +00001555 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1556 AddUInt(Die, DW_AT_decl_line, 0, Line);
1557 }
1558
1559 /// AddSourceLine - Add location information to specified debug information
1560 /// entry.
Devang Patel7c8a2772009-01-16 19:28:14 +00001561 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Devang Patel5f244e32009-01-05 22:35:52 +00001562 unsigned FileID = 0;
1563 unsigned Line = G->getLineNumber();
Devang Patel2ae1db52009-01-30 18:20:31 +00001564 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1565 FileID = Unit->getID();
Devang Pateld94b6932009-02-10 06:04:08 +00001566 assert (FileID && "Invalid file id");
Devang Patel5f244e32009-01-05 22:35:52 +00001567 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1568 AddUInt(Die, DW_AT_decl_line, 0, Line);
1569 }
1570
Devang Patel7c8a2772009-01-16 19:28:14 +00001571 void AddSourceLine(DIE *Die, const DIType *Ty) {
Devang Patel5f244e32009-01-05 22:35:52 +00001572 unsigned FileID = 0;
Devang Patel7c8a2772009-01-16 19:28:14 +00001573 unsigned Line = Ty->getLineNumber();
Devang Patel2ae1db52009-01-30 18:20:31 +00001574 DICompileUnit CU = Ty->getCompileUnit();
1575 if (CU.isNull())
1576 return;
1577 CompileUnit *Unit = FindCompileUnit(CU);
1578 FileID = Unit->getID();
Devang Pateld94b6932009-02-10 06:04:08 +00001579 assert (FileID && "Invalid file id");
Devang Patel5f244e32009-01-05 22:35:52 +00001580 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1581 AddUInt(Die, DW_AT_decl_line, 0, Line);
1582 }
1583
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001584 /// AddAddress - Add an address attribute to a die based on the location
1585 /// provided.
1586 void AddAddress(DIE *Die, unsigned Attribute,
1587 const MachineLocation &Location) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001588 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001589 DIEBlock *Block = new DIEBlock();
aslc200b112008-08-16 12:57:46 +00001590
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001591 if (Location.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001592 if (Reg < 32) {
1593 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1594 } else {
1595 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1596 AddUInt(Block, 0, DW_FORM_udata, Reg);
1597 }
1598 } else {
1599 if (Reg < 32) {
1600 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1601 } else {
1602 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1603 AddUInt(Block, 0, DW_FORM_udata, Reg);
1604 }
1605 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1606 }
aslc200b112008-08-16 12:57:46 +00001607
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001608 AddBlock(Die, Attribute, 0, Block);
1609 }
aslc200b112008-08-16 12:57:46 +00001610
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001611 /// AddType - Add a new type attribute to the specified entity.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001612 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel165ed512009-01-23 19:13:31 +00001613 if (Ty.isNull())
Devang Patel4a4cbe72009-01-05 21:47:57 +00001614 return;
Devang Patel4a4cbe72009-01-05 21:47:57 +00001615
1616 // Check for pre-existence.
1617 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1618 // If it exists then use the existing value.
1619 if (Slot) {
1620 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1621 return;
1622 }
1623
1624 // Set up proxy.
1625 Slot = NewDIEntry();
1626
1627 // Construct type.
1628 DIE Buffer(DW_TAG_base_type);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001629 if (Ty.isBasicType(Ty.getTag()))
1630 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1631 else if (Ty.isDerivedType(Ty.getTag()))
1632 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1633 else {
Bill Wendling824a8bf2009-02-03 21:17:20 +00001634 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelef4bf3b2009-01-15 19:26:23 +00001635 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1636 }
1637
Devang Patelb0cb07c2009-01-27 23:22:55 +00001638 // Add debug information entry to entity and appropriate context.
1639 DIE *Die = NULL;
1640 DIDescriptor Context = Ty.getContext();
1641 if (!Context.isNull())
1642 Die = DW_Unit->getDieMapSlotFor(Context.getGV());
1643
1644 if (Die) {
1645 DIE *Child = new DIE(Buffer);
1646 Die->AddChild(Child);
1647 Buffer.Detach();
1648 SetDIEntry(Slot, Child);
Bill Wendling824a8bf2009-02-03 21:17:20 +00001649 } else {
Devang Patelb0cb07c2009-01-27 23:22:55 +00001650 Die = DW_Unit->AddDie(Buffer);
1651 SetDIEntry(Slot, Die);
1652 }
1653
Devang Patel4a4cbe72009-01-05 21:47:57 +00001654 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1655 }
1656
Devang Patel46d13752009-01-05 19:07:53 +00001657 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1658 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001659 DIBasicType BTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001660
1661 // Get core information.
Bill Wendling1c5842b2009-03-09 05:04:40 +00001662 std::string Name;
1663 BTy.getName(Name);
Devang Patelfc187162009-01-05 17:57:47 +00001664 Buffer.setTag(DW_TAG_base_type);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001665 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patelfc187162009-01-05 17:57:47 +00001666 // Add name if not anonymous or intermediate type.
1667 if (!Name.empty())
1668 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001669 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelfc187162009-01-05 17:57:47 +00001670 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1671 }
1672
Devang Patel46d13752009-01-05 19:07:53 +00001673 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1674 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001675 DIDerivedType DTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001676
1677 // Get core information.
Bill Wendling1c5842b2009-03-09 05:04:40 +00001678 std::string Name;
1679 DTy.getName(Name);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001680 uint64_t Size = DTy.getSizeInBits() >> 3;
1681 unsigned Tag = DTy.getTag();
Bill Wendling1c5842b2009-03-09 05:04:40 +00001682
Devang Patelfc187162009-01-05 17:57:47 +00001683 // FIXME - Workaround for templates.
1684 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1685
1686 Buffer.setTag(Tag);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001687
Devang Patelfc187162009-01-05 17:57:47 +00001688 // Map to main type, void will not have a type.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001689 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel4a4cbe72009-01-05 21:47:57 +00001690 AddType(DW_Unit, &Buffer, FromTy);
Devang Patelfc187162009-01-05 17:57:47 +00001691
1692 // Add name if not anonymous or intermediate type.
Evan Cheng3e288912009-02-25 07:04:34 +00001693 if (!Name.empty())
1694 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelfc187162009-01-05 17:57:47 +00001695
1696 // Add size if non-zero (derived types might be zero-sized.)
1697 if (Size)
1698 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1699
1700 // Add source line info if available and TyDesc is not a forward
1701 // declaration.
Devang Patele34e0882009-01-27 00:45:04 +00001702 if (!DTy.isForwardDecl())
1703 AddSourceLine(&Buffer, &DTy);
Devang Patelfc187162009-01-05 17:57:47 +00001704 }
1705
Devang Patel30c01372009-01-05 19:55:51 +00001706 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1707 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001708 DICompositeType CTy) {
Devang Patelb28de842009-01-17 08:01:33 +00001709 // Get core information.
Bill Wendling1c5842b2009-03-09 05:04:40 +00001710 std::string Name;
1711 CTy.getName(Name);
1712
Devang Patelef4bf3b2009-01-15 19:26:23 +00001713 uint64_t Size = CTy.getSizeInBits() >> 3;
1714 unsigned Tag = CTy.getTag();
Devang Patel8050bd72009-01-23 01:19:09 +00001715 Buffer.setTag(Tag);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001716
Devang Patel30c01372009-01-05 19:55:51 +00001717 switch (Tag) {
1718 case DW_TAG_vector_type:
1719 case DW_TAG_array_type:
Devang Patelef4bf3b2009-01-15 19:26:23 +00001720 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patel30c01372009-01-05 19:55:51 +00001721 break;
Devang Patel3798f492009-01-20 18:35:14 +00001722 case DW_TAG_enumeration_type:
1723 {
1724 DIArray Elements = CTy.getTypeArray();
1725 // Add enumerators to enumeration type.
1726 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1727 DIE *ElemDie = NULL;
1728 DIEnumerator Enum(Elements.getElement(i).getGV());
1729 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
1730 Buffer.AddChild(ElemDie);
1731 }
1732 }
1733 break;
Devang Patel30c01372009-01-05 19:55:51 +00001734 case DW_TAG_subroutine_type:
1735 {
1736 // Add prototype flag.
1737 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001738 DIArray Elements = CTy.getTypeArray();
Devang Patel30c01372009-01-05 19:55:51 +00001739 // Add return type.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001740 DIDescriptor RTy = Elements.getElement(0);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001741 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel4a4cbe72009-01-05 21:47:57 +00001742
Devang Patel30c01372009-01-05 19:55:51 +00001743 // Add arguments.
1744 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1745 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel4a4cbe72009-01-05 21:47:57 +00001746 DIDescriptor Ty = Elements.getElement(i);
Devang Pateld40a7e52009-01-17 06:57:25 +00001747 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patel30c01372009-01-05 19:55:51 +00001748 Buffer.AddChild(Arg);
1749 }
1750 }
1751 break;
1752 case DW_TAG_structure_type:
1753 case DW_TAG_union_type:
1754 {
1755 // Add elements to structure type.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001756 DIArray Elements = CTy.getTypeArray();
Devang Patelcf7acb12009-01-16 00:50:53 +00001757
1758 // A forward struct declared type may not have elements available.
1759 if (Elements.isNull())
1760 break;
1761
Devang Patel30c01372009-01-05 19:55:51 +00001762 // Add elements to structure type.
1763 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1764 DIDescriptor Element = Elements.getElement(i);
Devang Patelb28de842009-01-17 08:01:33 +00001765 DIE *ElemDie = NULL;
Devang Patelef4bf3b2009-01-15 19:26:23 +00001766 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel245446c2009-01-17 08:05:14 +00001767 ElemDie = CreateSubprogramDIE(DW_Unit,
1768 DISubprogram(Element.getGV()));
Devang Patelb28de842009-01-17 08:01:33 +00001769 else if (Element.getTag() == dwarf::DW_TAG_variable) // ???
1770 ElemDie = CreateGlobalVariableDIE(DW_Unit,
1771 DIGlobalVariable(Element.getGV()));
Devang Patel5c643892009-01-20 21:02:02 +00001772 else
1773 ElemDie = CreateMemberDIE(DW_Unit,
1774 DIDerivedType(Element.getGV()));
Devang Patel245446c2009-01-17 08:05:14 +00001775 Buffer.AddChild(ElemDie);
Devang Patel30c01372009-01-05 19:55:51 +00001776 }
Devang Patel74193d72009-02-17 22:43:44 +00001777 unsigned RLang = CTy.getRunTimeLang();
1778 if (RLang)
1779 AddUInt(&Buffer, DW_AT_APPLE_runtime_class, DW_FORM_data1, RLang);
Devang Patel30c01372009-01-05 19:55:51 +00001780 }
1781 break;
1782 default:
1783 break;
1784 }
1785
1786 // Add name if not anonymous or intermediate type.
Evan Cheng3e288912009-02-25 07:04:34 +00001787 if (!Name.empty())
1788 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel30c01372009-01-05 19:55:51 +00001789
Devang Patele34e0882009-01-27 00:45:04 +00001790 if (Tag == DW_TAG_enumeration_type || Tag == DW_TAG_structure_type
1791 || Tag == DW_TAG_union_type) {
1792 // Add size if non-zero (derived types might be zero-sized.)
1793 if (Size)
1794 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1795 else {
1796 // Add zero size if it is not a forward declaration.
1797 if (CTy.isForwardDecl())
1798 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1799 else
1800 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1801 }
1802
1803 // Add source line info if available.
1804 if (!CTy.isForwardDecl())
1805 AddSourceLine(&Buffer, &CTy);
Devang Patel30c01372009-01-05 19:55:51 +00001806 }
Devang Patel30c01372009-01-05 19:55:51 +00001807 }
1808
Bill Wendling824a8bf2009-02-03 21:17:20 +00001809 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1810 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelef4bf3b2009-01-15 19:26:23 +00001811 int64_t L = SR.getLo();
1812 int64_t H = SR.getHi();
Devang Patel6fb54132009-01-05 18:33:01 +00001813 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1814 if (L != H) {
1815 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1816 if (L)
Devang Patel245446c2009-01-17 08:05:14 +00001817 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1818 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel6fb54132009-01-05 18:33:01 +00001819 }
1820 Buffer.AddChild(DW_Subrange);
1821 }
1822
1823 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1824 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1825 DICompositeType *CTy) {
1826 Buffer.setTag(DW_TAG_array_type);
1827 if (CTy->getTag() == DW_TAG_vector_type)
1828 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1829
Devang Patel6ab30e52009-01-28 21:08:20 +00001830 // Emit derived type.
1831 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel6fb54132009-01-05 18:33:01 +00001832 DIArray Elements = CTy->getTypeArray();
Devang Patel6fb54132009-01-05 18:33:01 +00001833
1834 // Construct an anonymous type for index type.
1835 DIE IdxBuffer(DW_TAG_base_type);
1836 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1837 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1838 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1839
1840 // Add subranges to array type.
1841 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel30c01372009-01-05 19:55:51 +00001842 DIDescriptor Element = Elements.getElement(i);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001843 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1844 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel6fb54132009-01-05 18:33:01 +00001845 }
1846 }
1847
Bill Wendling824a8bf2009-02-03 21:17:20 +00001848 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3798f492009-01-20 18:35:14 +00001849 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patela566e812009-01-05 18:38:38 +00001850
1851 DIE *Enumerator = new DIE(DW_TAG_enumerator);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001852 std::string Name;
1853 ETy->getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001854 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
Devang Patela566e812009-01-05 18:38:38 +00001855 int64_t Value = ETy->getEnumValue();
1856 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Patel3798f492009-01-20 18:35:14 +00001857 return Enumerator;
Devang Patela566e812009-01-05 18:38:38 +00001858 }
Devang Patel6fb54132009-01-05 18:33:01 +00001859
Devang Patelb28de842009-01-17 08:01:33 +00001860 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendling824a8bf2009-02-03 21:17:20 +00001861 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patelb28de842009-01-17 08:01:33 +00001862 {
1863 DIE *GVDie = new DIE(DW_TAG_variable);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001864 std::string Name;
1865 GV.getDisplayName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001866 AddString(GVDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001867 std::string LinkageName;
1868 GV.getLinkageName(LinkageName);
Devang Patel526b01d2009-01-05 18:59:44 +00001869 if (!LinkageName.empty())
Devang Patelb28de842009-01-17 08:01:33 +00001870 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1871 AddType(DW_Unit, GVDie, GV.getType());
1872 if (!GV.isLocalToUnit())
1873 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
1874 AddSourceLine(GVDie, &GV);
1875 return GVDie;
Devang Patel526b01d2009-01-05 18:59:44 +00001876 }
1877
Devang Patel5c643892009-01-20 21:02:02 +00001878 /// CreateMemberDIE - Create new member DIE.
1879 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
1880 DIE *MemberDie = new DIE(DT.getTag());
Bill Wendling1c5842b2009-03-09 05:04:40 +00001881 std::string Name;
1882 DT.getName(Name);
Devang Patel5c643892009-01-20 21:02:02 +00001883 if (!Name.empty())
1884 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1885
1886 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1887
1888 AddSourceLine(MemberDie, &DT);
1889
Devang Patelf1f30d42009-02-17 21:23:59 +00001890 uint64_t Size = DT.getSizeInBits();
1891 uint64_t FieldSize = DT.getOriginalTypeSize();
1892
1893 if (Size != FieldSize) {
1894 // Handle bitfield.
1895 AddUInt(MemberDie, DW_AT_byte_size, 0, DT.getOriginalTypeSize() >> 3);
1896 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1897
1898 uint64_t Offset = DT.getOffsetInBits();
1899 uint64_t FieldOffset = Offset;
1900 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1901 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1902 FieldOffset = (HiMark - FieldSize);
1903 Offset -= FieldOffset;
1904 // Maybe we need to work from the other end.
1905 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1906 AddUInt(MemberDie, DW_AT_bit_offset, 0, Offset);
1907 }
Devang Patel5c643892009-01-20 21:02:02 +00001908 DIEBlock *Block = new DIEBlock();
1909 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1910 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1911 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1912
Devang Patel2e7ee192009-01-21 00:08:04 +00001913 if (DT.isProtected())
1914 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1915 else if (DT.isPrivate())
1916 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1917
Devang Patel5c643892009-01-20 21:02:02 +00001918 return MemberDie;
1919 }
1920
Devang Patelb28de842009-01-17 08:01:33 +00001921 /// CreateSubprogramDIE - Create new DIE using SP.
1922 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Devang Patel245446c2009-01-17 08:05:14 +00001923 const DISubprogram &SP,
1924 bool IsConstructor = false) {
Devang Patelb28de842009-01-17 08:01:33 +00001925 DIE *SPDie = new DIE(DW_TAG_subprogram);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001926 std::string Name;
1927 SP.getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001928 AddString(SPDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001929 std::string LinkageName;
1930 SP.getLinkageName(LinkageName);
Devang Patel526b01d2009-01-05 18:59:44 +00001931 if (!LinkageName.empty())
Devang Patelb28de842009-01-17 08:01:33 +00001932 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Devang Patel245446c2009-01-17 08:05:14 +00001933 LinkageName);
Devang Patelb28de842009-01-17 08:01:33 +00001934 AddSourceLine(SPDie, &SP);
Devang Patel526b01d2009-01-05 18:59:44 +00001935
Devang Patelb28de842009-01-17 08:01:33 +00001936 DICompositeType SPTy = SP.getType();
1937 DIArray Args = SPTy.getTypeArray();
1938
Devang Patel526b01d2009-01-05 18:59:44 +00001939 // Add Return Type.
Devang Patel688a19f2009-02-27 18:05:21 +00001940 if (!IsConstructor) {
1941 if (Args.isNull())
1942 AddType(DW_Unit, SPDie, SPTy);
1943 else
1944 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
1945 }
Devang Patel922d1592009-01-30 01:21:46 +00001946
Devang Patelace2cf62009-02-02 17:51:41 +00001947 if (!SP.isDefinition()) {
1948 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
1949 // Add arguments.
1950 // Do not add arguments for subprogram definition. They will be
1951 // handled through RecordVariable.
1952 if (!Args.isNull())
1953 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1954 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1955 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
1956 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
1957 SPDie->AddChild(Arg);
1958 }
1959 }
Devang Patel922d1592009-01-30 01:21:46 +00001960
Devang Patele075e072009-02-24 00:52:19 +00001961 unsigned Lang = SP.getCompileUnit().getLanguage();
1962 if (Lang == DW_LANG_C99 || Lang == DW_LANG_C89
1963 || Lang == DW_LANG_ObjC)
1964 AddUInt(SPDie, DW_AT_prototyped, DW_FORM_flag, 1);
1965
Devang Patelef4bf3b2009-01-15 19:26:23 +00001966 if (!SP.isLocalToUnit())
Devang Patel922d1592009-01-30 01:21:46 +00001967 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Devang Patelb28de842009-01-17 08:01:33 +00001968 return SPDie;
Devang Patel526b01d2009-01-05 18:59:44 +00001969 }
1970
Devang Patelb28de842009-01-17 08:01:33 +00001971 /// FindCompileUnit - Get the compile unit for the given descriptor.
1972 ///
Devang Patel5f244e32009-01-05 22:35:52 +00001973 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
Evan Cheng3e288912009-02-25 07:04:34 +00001974 CompileUnit *DW_Unit = CompileUnitMap[Unit.getGV()];
Devang Patel5f244e32009-01-05 22:35:52 +00001975 assert(DW_Unit && "Missing compile unit.");
1976 return DW_Unit;
1977 }
1978
Devang Patel42f6bed2009-01-13 23:54:55 +00001979 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel4d1709e2009-01-08 02:33:41 +00001980 ///
1981 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1982 // Get the descriptor.
Devang Patel7c8a2772009-01-16 19:28:14 +00001983 const DIVariable &VD = DV->getVariable();
Devang Patel4d1709e2009-01-08 02:33:41 +00001984
1985 // Translate tag to proper Dwarf tag. The result variable is dropped for
1986 // now.
1987 unsigned Tag;
Devang Patel7c8a2772009-01-16 19:28:14 +00001988 switch (VD.getTag()) {
Devang Patel4d1709e2009-01-08 02:33:41 +00001989 case DW_TAG_return_variable: return NULL;
1990 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1991 case DW_TAG_auto_variable: // fall thru
1992 default: Tag = DW_TAG_variable; break;
1993 }
1994
1995 // Define variable debug information entry.
1996 DIE *VariableDie = new DIE(Tag);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001997 std::string Name;
1998 VD.getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001999 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Devang Patel4d1709e2009-01-08 02:33:41 +00002000
2001 // Add source line info if available.
Devang Patel7c8a2772009-01-16 19:28:14 +00002002 AddSourceLine(VariableDie, &VD);
Devang Patel4d1709e2009-01-08 02:33:41 +00002003
2004 // Add variable type.
Devang Patel7c8a2772009-01-16 19:28:14 +00002005 AddType(Unit, VariableDie, VD.getType());
Devang Patel4d1709e2009-01-08 02:33:41 +00002006
2007 // Add variable address.
2008 MachineLocation Location;
2009 Location.set(RI->getFrameRegister(*MF),
2010 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
2011 AddAddress(VariableDie, DW_AT_location, Location);
2012
2013 return VariableDie;
2014 }
2015
Devang Patel4d1709e2009-01-08 02:33:41 +00002016 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2017 ///
2018 DbgScope *getOrCreateScope(GlobalVariable *V) {
2019 DbgScope *&Slot = DbgScopeMap[V];
Bill Wendlinge0f3a262009-02-20 20:40:28 +00002020 if (Slot) return Slot;
2021
2022 // FIXME - breaks down when the context is an inlined function.
2023 DIDescriptor ParentDesc;
2024 DIDescriptor Desc(V);
2025
2026 if (Desc.getTag() == dwarf::DW_TAG_lexical_block) {
2027 DIBlock Block(V);
2028 ParentDesc = Block.getContext();
Devang Patel4d1709e2009-01-08 02:33:41 +00002029 }
Bill Wendlinge0f3a262009-02-20 20:40:28 +00002030
2031 DbgScope *Parent = ParentDesc.isNull() ?
2032 NULL : getOrCreateScope(ParentDesc.getGV());
2033 Slot = new DbgScope(Parent, Desc);
2034
2035 if (Parent) {
2036 Parent->AddScope(Slot);
2037 } else if (RootDbgScope) {
2038 // FIXME - Add inlined function scopes to the root so we can delete them
2039 // later. Long term, handle inlined functions properly.
2040 RootDbgScope->AddScope(Slot);
2041 } else {
2042 // First function is top level function.
2043 RootDbgScope = Slot;
2044 }
2045
Devang Patel4d1709e2009-01-08 02:33:41 +00002046 return Slot;
2047 }
2048
2049 /// ConstructDbgScope - Construct the components of a scope.
2050 ///
2051 void ConstructDbgScope(DbgScope *ParentScope,
2052 unsigned ParentStartID, unsigned ParentEndID,
2053 DIE *ParentDie, CompileUnit *Unit) {
2054 // Add variables to scope.
Devang Patel63c22f42009-01-10 02:42:49 +00002055 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
Devang Patel4d1709e2009-01-08 02:33:41 +00002056 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2057 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2058 if (VariableDie) ParentDie->AddChild(VariableDie);
2059 }
2060
2061 // Add nested scopes.
Devang Patel63c22f42009-01-10 02:42:49 +00002062 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel4d1709e2009-01-08 02:33:41 +00002063 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2064 // Define the Scope debug information entry.
2065 DbgScope *Scope = Scopes[j];
2066 // FIXME - Ignore inlined functions for the time being.
2067 if (!Scope->getParent()) continue;
2068
Devang Patelb9224922009-01-12 18:41:00 +00002069 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2070 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel4d1709e2009-01-08 02:33:41 +00002071
2072 // Ignore empty scopes.
2073 if (StartID == EndID && StartID != 0) continue;
2074 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
2075
2076 if (StartID == ParentStartID && EndID == ParentEndID) {
2077 // Just add stuff to the parent scope.
2078 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2079 } else {
2080 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2081
2082 // Add the scope bounds.
2083 if (StartID) {
2084 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2085 DWLabel("label", StartID));
2086 } else {
2087 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2088 DWLabel("func_begin", SubprogramCount));
2089 }
2090 if (EndID) {
2091 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2092 DWLabel("label", EndID));
2093 } else {
2094 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2095 DWLabel("func_end", SubprogramCount));
2096 }
2097
2098 // Add the scope contents.
2099 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2100 ParentDie->AddChild(ScopeDie);
2101 }
2102 }
2103 }
2104
2105 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2106 ///
2107 void ConstructRootDbgScope(DbgScope *RootScope) {
2108 // Exit if there is no root scope.
2109 if (!RootScope) return;
Devang Patel2560d922009-01-15 18:25:17 +00002110 DIDescriptor Desc = RootScope->getDesc();
2111 if (Desc.isNull())
2112 return;
Devang Patel4d1709e2009-01-08 02:33:41 +00002113
2114 // Get the subprogram debug information entry.
Devang Patel2560d922009-01-15 18:25:17 +00002115 DISubprogram SPD(Desc.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002116
2117 // Get the compile unit context.
Devang Patel2ae1db52009-01-30 18:20:31 +00002118 CompileUnit *Unit = MainCU;
2119 if (!Unit)
2120 Unit = FindCompileUnit(SPD.getCompileUnit());
Devang Patel4d1709e2009-01-08 02:33:41 +00002121
2122 // Get the subprogram die.
Devang Patel57ec9ac2009-01-12 22:58:14 +00002123 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002124 assert(SPDie && "Missing subprogram descriptor");
2125
2126 // Add the function bounds.
2127 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2128 DWLabel("func_begin", SubprogramCount));
2129 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2130 DWLabel("func_end", SubprogramCount));
2131 MachineLocation Location(RI->getFrameRegister(*MF));
2132 AddAddress(SPDie, DW_AT_frame_base, Location);
2133
2134 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2135 }
2136
2137 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2138 ///
2139 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Cheng3e288912009-02-25 07:04:34 +00002140 const char *FnName = MF->getFunction()->getNameStart();
2141 if (MainCU) {
2142 std::map<std::string, DIE*> &Globals = MainCU->getGlobals();
2143 std::map<std::string, DIE*>::iterator GI = Globals.find(FnName);
2144 if (GI != Globals.end()) {
2145 DIE *SPDie = GI->second;
Devang Patel4d1709e2009-01-08 02:33:41 +00002146
2147 // Add the function bounds.
2148 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2149 DWLabel("func_begin", SubprogramCount));
2150 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2151 DWLabel("func_end", SubprogramCount));
2152
2153 MachineLocation Location(RI->getFrameRegister(*MF));
2154 AddAddress(SPDie, DW_AT_frame_base, Location);
2155 return;
2156 }
Evan Cheng3e288912009-02-25 07:04:34 +00002157 } else {
2158 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2159 CompileUnit *Unit = CompileUnits[i];
2160 std::map<std::string, DIE*> &Globals = Unit->getGlobals();
2161 std::map<std::string, DIE*>::iterator GI = Globals.find(FnName);
2162 if (GI != Globals.end()) {
2163 DIE *SPDie = GI->second;
2164
2165 // Add the function bounds.
2166 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2167 DWLabel("func_begin", SubprogramCount));
2168 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2169 DWLabel("func_end", SubprogramCount));
2170
2171 MachineLocation Location(RI->getFrameRegister(*MF));
2172 AddAddress(SPDie, DW_AT_frame_base, Location);
2173 return;
2174 }
2175 }
Devang Patel4d1709e2009-01-08 02:33:41 +00002176 }
Evan Cheng3e288912009-02-25 07:04:34 +00002177
Devang Patel4d1709e2009-01-08 02:33:41 +00002178#if 0
2179 // FIXME: This is causing an abort because C++ mangled names are compared
2180 // with their unmangled counterparts. See PR2885. Don't do this assert.
2181 assert(0 && "Couldn't find DIE for machine function!");
2182#endif
Evan Cheng3e288912009-02-25 07:04:34 +00002183 return;
Devang Patel4d1709e2009-01-08 02:33:41 +00002184 }
2185
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002186 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2187 /// tools to recognize the object file contains Dwarf information.
2188 void EmitInitial() {
2189 // Check to see if we already emitted intial headers.
2190 if (didInitial) return;
2191 didInitial = true;
aslc200b112008-08-16 12:57:46 +00002192
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002193 // Dwarf sections base addresses.
2194 if (TAI->doesDwarfRequireFrameSection()) {
2195 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2196 EmitLabel("section_debug_frame", 0);
2197 }
2198 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2199 EmitLabel("section_info", 0);
2200 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2201 EmitLabel("section_abbrev", 0);
2202 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2203 EmitLabel("section_aranges", 0);
Scott Michel79f01f52009-01-26 22:32:51 +00002204 if (TAI->doesSupportMacInfoSection()) {
2205 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2206 EmitLabel("section_macinfo", 0);
2207 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002208 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2209 EmitLabel("section_line", 0);
2210 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2211 EmitLabel("section_loc", 0);
2212 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2213 EmitLabel("section_pubnames", 0);
2214 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2215 EmitLabel("section_str", 0);
2216 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2217 EmitLabel("section_ranges", 0);
2218
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002219 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002220 EmitLabel("text_begin", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00002221 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002222 EmitLabel("data_begin", 0);
2223 }
2224
2225 /// EmitDIE - Recusively Emits a debug information entry.
2226 ///
2227 void EmitDIE(DIE *Die) {
2228 // Get the abbreviation for this DIE.
2229 unsigned AbbrevNumber = Die->getAbbrevNumber();
2230 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
aslc200b112008-08-16 12:57:46 +00002231
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002232 Asm->EOL();
2233
2234 // Emit the code (index) for the abbreviation.
2235 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng0eeed442008-07-01 23:18:29 +00002236
2237 if (VerboseAsm)
2238 Asm->EOL(std::string("Abbrev [" +
2239 utostr(AbbrevNumber) +
2240 "] 0x" + utohexstr(Die->getOffset()) +
2241 ":0x" + utohexstr(Die->getSize()) + " " +
2242 TagString(Abbrev->getTag())));
2243 else
2244 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00002245
Owen Anderson88dd6232008-06-24 21:44:59 +00002246 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2247 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
aslc200b112008-08-16 12:57:46 +00002248
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002249 // Emit the DIE attribute values.
2250 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2251 unsigned Attr = AbbrevData[i].getAttribute();
2252 unsigned Form = AbbrevData[i].getForm();
2253 assert(Form && "Too many attributes for DIE (check abbreviation)");
aslc200b112008-08-16 12:57:46 +00002254
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002255 switch (Attr) {
2256 case DW_AT_sibling: {
2257 Asm->EmitInt32(Die->SiblingOffset());
2258 break;
2259 }
2260 default: {
2261 // Emit an attribute using the defined form.
2262 Values[i]->EmitValue(*this, Form);
2263 break;
2264 }
2265 }
aslc200b112008-08-16 12:57:46 +00002266
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002267 Asm->EOL(AttributeString(Attr));
2268 }
aslc200b112008-08-16 12:57:46 +00002269
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002270 // Emit the DIE children if any.
2271 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2272 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002273
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002274 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2275 EmitDIE(Children[j]);
2276 }
aslc200b112008-08-16 12:57:46 +00002277
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002278 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2279 }
2280 }
2281
2282 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2283 ///
2284 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2285 // Get the children.
2286 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002287
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002288 // If not last sibling and has children then add sibling offset attribute.
2289 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2290
2291 // Record the abbreviation.
2292 AssignAbbrevNumber(Die->getAbbrev());
aslc200b112008-08-16 12:57:46 +00002293
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002294 // Get the abbreviation for this DIE.
2295 unsigned AbbrevNumber = Die->getAbbrevNumber();
2296 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2297
2298 // Set DIE offset
2299 Die->setOffset(Offset);
aslc200b112008-08-16 12:57:46 +00002300
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002301 // Start the size with the size of abbreviation code.
aslc200b112008-08-16 12:57:46 +00002302 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2303
Owen Anderson88dd6232008-06-24 21:44:59 +00002304 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2305 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002306
2307 // Size the DIE attribute values.
2308 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2309 // Size attribute value.
2310 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2311 }
aslc200b112008-08-16 12:57:46 +00002312
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002313 // Size the DIE children if any.
2314 if (!Children.empty()) {
2315 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2316 "Children flag not set");
aslc200b112008-08-16 12:57:46 +00002317
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002318 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2319 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2320 }
aslc200b112008-08-16 12:57:46 +00002321
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002322 // End of children marker.
2323 Offset += sizeof(int8_t);
2324 }
2325
2326 Die->setSize(Offset - Die->getOffset());
2327 return Offset;
2328 }
2329
2330 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2331 ///
2332 void SizeAndOffsets() {
2333 // Process base compile unit.
Devang Patel2ae1db52009-01-30 18:20:31 +00002334 if (MainCU) {
2335 // Compute size of compile unit header
2336 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2337 sizeof(int16_t) + // DWARF version number
2338 sizeof(int32_t) + // Offset Into Abbrev. Section
2339 sizeof(int8_t); // Pointer Size (in bytes)
2340 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
2341 return;
2342 }
Evan Cheng3e288912009-02-25 07:04:34 +00002343 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2344 CompileUnit *Unit = CompileUnits[i];
Devang Patel6eae2832009-01-12 23:05:55 +00002345 // Compute size of compile unit header
2346 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2347 sizeof(int16_t) + // DWARF version number
2348 sizeof(int32_t) + // Offset Into Abbrev. Section
2349 sizeof(int8_t); // Pointer Size (in bytes)
2350 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2351 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002352 }
2353
Evan Cheng3e288912009-02-25 07:04:34 +00002354 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002355 ///
Evan Cheng3e288912009-02-25 07:04:34 +00002356 void EmitDebugInfoPerCU(CompileUnit *Unit) {
2357 DIE *Die = Unit->getDie();
2358 // Emit the compile units header.
2359 EmitLabel("info_begin", Unit->getID());
2360 // Emit size of content not including length itself
2361 unsigned ContentSize = Die->getSize() +
2362 sizeof(int16_t) + // DWARF version number
2363 sizeof(int32_t) + // Offset Into Abbrev. Section
2364 sizeof(int8_t) + // Pointer Size (in bytes)
2365 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2366
2367 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2368 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2369 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2370 Asm->EOL("Offset Into Abbrev. Section");
2371 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2372
2373 EmitDIE(Die);
2374 // FIXME - extra padding for gdb bug.
2375 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2376 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2377 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2378 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2379 EmitLabel("info_end", Unit->getID());
2380
2381 Asm->EOL();
2382 }
2383
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002384 void EmitDebugInfo() {
2385 // Start debug info section.
2386 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
aslc200b112008-08-16 12:57:46 +00002387
Evan Cheng3e288912009-02-25 07:04:34 +00002388 if (MainCU) {
2389 EmitDebugInfoPerCU(MainCU);
2390 return;
Devang Patel6eae2832009-01-12 23:05:55 +00002391 }
Evan Cheng3e288912009-02-25 07:04:34 +00002392
2393 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2394 EmitDebugInfoPerCU(CompileUnits[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002395 }
2396
2397 /// EmitAbbreviations - Emit the abbreviation section.
2398 ///
2399 void EmitAbbreviations() const {
2400 // Check to see if it is worth the effort.
2401 if (!Abbreviations.empty()) {
2402 // Start the debug abbrev section.
2403 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
aslc200b112008-08-16 12:57:46 +00002404
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002405 EmitLabel("abbrev_begin", 0);
aslc200b112008-08-16 12:57:46 +00002406
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002407 // For each abbrevation.
2408 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2409 // Get abbreviation data
2410 const DIEAbbrev *Abbrev = Abbreviations[i];
aslc200b112008-08-16 12:57:46 +00002411
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002412 // Emit the abbrevations code (base 1 index.)
2413 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2414 Asm->EOL("Abbreviation Code");
aslc200b112008-08-16 12:57:46 +00002415
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002416 // Emit the abbreviations data.
2417 Abbrev->Emit(*this);
aslc200b112008-08-16 12:57:46 +00002418
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002419 Asm->EOL();
2420 }
aslc200b112008-08-16 12:57:46 +00002421
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002422 // Mark end of abbreviations.
2423 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2424
2425 EmitLabel("abbrev_end", 0);
aslc200b112008-08-16 12:57:46 +00002426
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002427 Asm->EOL();
2428 }
2429 }
2430
Bill Wendling1983a2a2008-07-20 00:11:19 +00002431 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2432 /// the line matrix.
aslc200b112008-08-16 12:57:46 +00002433 ///
Bill Wendling1983a2a2008-07-20 00:11:19 +00002434 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2435 // Define last address of section.
2436 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2437 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2438 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2439 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2440
2441 // Mark end of matrix.
2442 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2443 Asm->EmitULEB128Bytes(1); Asm->EOL();
2444 Asm->EmitInt8(1); Asm->EOL();
2445 }
2446
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002447 /// EmitDebugLines - Emit source line information.
2448 ///
2449 void EmitDebugLines() {
Bill Wendling1983a2a2008-07-20 00:11:19 +00002450 // If the target is using .loc/.file, the assembler will be emitting the
2451 // .debug_line table automatically.
2452 if (TAI->hasDotLocAndDotFile())
Dan Gohmanc55b34a2007-09-24 21:43:52 +00002453 return;
2454
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002455 // Minimum line delta, thus ranging from -10..(255-10).
2456 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2457 // Maximum line delta, thus ranging from -10..(255-10).
2458 const int MaxLineDelta = 255 + MinLineDelta;
2459
2460 // Start the dwarf line section.
2461 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
aslc200b112008-08-16 12:57:46 +00002462
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002463 // Construct the section header.
aslc200b112008-08-16 12:57:46 +00002464
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002465 EmitDifference("line_end", 0, "line_begin", 0, true);
2466 Asm->EOL("Length of Source Line Info");
2467 EmitLabel("line_begin", 0);
aslc200b112008-08-16 12:57:46 +00002468
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002469 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
aslc200b112008-08-16 12:57:46 +00002470
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002471 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2472 Asm->EOL("Prolog Length");
2473 EmitLabel("line_prolog_begin", 0);
aslc200b112008-08-16 12:57:46 +00002474
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002475 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2476
2477 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2478
2479 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
aslc200b112008-08-16 12:57:46 +00002480
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002481 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2482
2483 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
aslc200b112008-08-16 12:57:46 +00002484
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002485 // Line number standard opcode encodings argument count
2486 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2487 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2488 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2489 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2490 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2491 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2492 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2493 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2494 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2495
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002496 // Emit directories.
Evan Cheng3e288912009-02-25 07:04:34 +00002497 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2498 Asm->EmitString(getSourceDirectoryName(DI));
2499 Asm->EOL("Directory");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002500 }
2501 Asm->EmitInt8(0); Asm->EOL("End of directories");
aslc200b112008-08-16 12:57:46 +00002502
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002503 // Emit files.
Evan Cheng3e288912009-02-25 07:04:34 +00002504 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2505 // Remember source id starts at 1.
2506 std::pair<unsigned, unsigned> Id = getSourceDirsectoryAndFileIds(SI);
2507 Asm->EmitString(getSourceFileName(Id.second));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002508 Asm->EOL("Source");
Evan Cheng3e288912009-02-25 07:04:34 +00002509 Asm->EmitULEB128Bytes(Id.first);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002510 Asm->EOL("Directory #");
2511 Asm->EmitULEB128Bytes(0);
2512 Asm->EOL("Mod date");
2513 Asm->EmitULEB128Bytes(0);
2514 Asm->EOL("File size");
2515 }
2516 Asm->EmitInt8(0); Asm->EOL("End of files");
aslc200b112008-08-16 12:57:46 +00002517
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002518 EmitLabel("line_prolog_end", 0);
aslc200b112008-08-16 12:57:46 +00002519
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002520 // A sequence for each text section.
Bill Wendling1983a2a2008-07-20 00:11:19 +00002521 unsigned SecSrcLinesSize = SectionSourceLines.size();
2522
2523 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002524 // Isolate current sections line info.
Devang Patel35a078f2009-01-12 22:54:42 +00002525 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng0eeed442008-07-01 23:18:29 +00002526
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002527 if (VerboseAsm) {
2528 const Section* S = SectionMap[j + 1];
Evan Cheng3e288912009-02-25 07:04:34 +00002529 O << '\t' << TAI->getCommentString() << " Section"
2530 << S->getName() << '\n';
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002531 } else
Evan Cheng0eeed442008-07-01 23:18:29 +00002532 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002533
2534 // Dwarf assumes we start with first line of first source file.
2535 unsigned Source = 1;
2536 unsigned Line = 1;
aslc200b112008-08-16 12:57:46 +00002537
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002538 // Construct rows of the address, source, line, column matrix.
2539 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patel35a078f2009-01-12 22:54:42 +00002540 const SrcLineInfo &LineInfo = LineInfos[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002541 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2542 if (!LabelID) continue;
aslc200b112008-08-16 12:57:46 +00002543
Evan Cheng3e288912009-02-25 07:04:34 +00002544 if (!VerboseAsm)
Evan Cheng0eeed442008-07-01 23:18:29 +00002545 Asm->EOL();
Evan Cheng3e288912009-02-25 07:04:34 +00002546 else {
2547 std::pair<unsigned, unsigned> SourceID =
2548 getSourceDirsectoryAndFileIds(LineInfo.getSourceID());
2549 O << '\t' << TAI->getCommentString() << ' '
2550 << getSourceDirectoryName(SourceID.first) << ' '
2551 << getSourceFileName(SourceID.second)
2552 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2553 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002554
2555 // Define the line address.
2556 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohmancfb72b22007-09-27 23:12:31 +00002557 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002558 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2559 EmitReference("label", LabelID); Asm->EOL("Location label");
aslc200b112008-08-16 12:57:46 +00002560
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002561 // If change of source, then switch to the new source.
2562 if (Source != LineInfo.getSourceID()) {
2563 Source = LineInfo.getSourceID();
2564 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2565 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2566 }
aslc200b112008-08-16 12:57:46 +00002567
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002568 // If change of line.
2569 if (Line != LineInfo.getLine()) {
2570 // Determine offset.
2571 int Offset = LineInfo.getLine() - Line;
2572 int Delta = Offset - MinLineDelta;
aslc200b112008-08-16 12:57:46 +00002573
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002574 // Update line.
2575 Line = LineInfo.getLine();
aslc200b112008-08-16 12:57:46 +00002576
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002577 // If delta is small enough and in range...
2578 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2579 // ... then use fast opcode.
2580 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2581 } else {
2582 // ... otherwise use long hand.
2583 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2584 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2585 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2586 }
2587 } else {
2588 // Copy the previous row (different address or source)
2589 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2590 }
2591 }
2592
Bill Wendling1983a2a2008-07-20 00:11:19 +00002593 EmitEndOfLineMatrix(j + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002594 }
Bill Wendling1983a2a2008-07-20 00:11:19 +00002595
2596 if (SecSrcLinesSize == 0)
2597 // Because we're emitting a debug_line section, we still need a line
2598 // table. The linker and friends expect it to exist. If there's nothing to
2599 // put into it, emit an empty table.
2600 EmitEndOfLineMatrix(1);
aslc200b112008-08-16 12:57:46 +00002601
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002602 EmitLabel("line_end", 0);
aslc200b112008-08-16 12:57:46 +00002603
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002604 Asm->EOL();
2605 }
aslc200b112008-08-16 12:57:46 +00002606
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002607 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2608 ///
2609 void EmitCommonDebugFrame() {
2610 if (!TAI->doesDwarfRequireFrameSection())
2611 return;
2612
2613 int stackGrowth =
2614 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2615 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00002616 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002617
2618 // Start the dwarf frame section.
2619 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2620
2621 EmitLabel("debug_frame_common", 0);
2622 EmitDifference("debug_frame_common_end", 0,
2623 "debug_frame_common_begin", 0, true);
2624 Asm->EOL("Length of Common Information Entry");
2625
2626 EmitLabel("debug_frame_common_begin", 0);
2627 Asm->EmitInt32((int)DW_CIE_ID);
2628 Asm->EOL("CIE Identifier Tag");
2629 Asm->EmitInt8(DW_CIE_VERSION);
2630 Asm->EOL("CIE Version");
2631 Asm->EmitString("");
2632 Asm->EOL("CIE Augmentation");
2633 Asm->EmitULEB128Bytes(1);
2634 Asm->EOL("CIE Code Alignment Factor");
2635 Asm->EmitSLEB128Bytes(stackGrowth);
aslc200b112008-08-16 12:57:46 +00002636 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00002637 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002638 Asm->EOL("CIE RA Column");
aslc200b112008-08-16 12:57:46 +00002639
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002640 std::vector<MachineMove> Moves;
2641 RI->getInitialFrameState(Moves);
2642
Dale Johannesenf5a11532007-11-13 19:13:01 +00002643 EmitFrameMoves(NULL, 0, Moves, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002644
Evan Cheng7e7d1942008-02-29 19:36:59 +00002645 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002646 EmitLabel("debug_frame_common_end", 0);
aslc200b112008-08-16 12:57:46 +00002647
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002648 Asm->EOL();
2649 }
2650
2651 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2652 /// section.
2653 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
2654 if (!TAI->doesDwarfRequireFrameSection())
2655 return;
aslc200b112008-08-16 12:57:46 +00002656
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002657 // Start the dwarf frame section.
2658 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
aslc200b112008-08-16 12:57:46 +00002659
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002660 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2661 "debug_frame_begin", DebugFrameInfo.Number, true);
2662 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00002663
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002664 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2665
2666 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2667 0, 0, true, false);
2668 Asm->EOL("FDE CIE offset");
2669
2670 EmitReference("func_begin", DebugFrameInfo.Number);
2671 Asm->EOL("FDE initial location");
2672 EmitDifference("func_end", DebugFrameInfo.Number,
2673 "func_begin", DebugFrameInfo.Number);
2674 Asm->EOL("FDE address range");
aslc200b112008-08-16 12:57:46 +00002675
Devang Patelb28de842009-01-17 08:01:33 +00002676 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00002677 false);
aslc200b112008-08-16 12:57:46 +00002678
Evan Cheng7e7d1942008-02-29 19:36:59 +00002679 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002680 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2681
2682 Asm->EOL();
2683 }
2684
Evan Cheng3e288912009-02-25 07:04:34 +00002685 void EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2686 EmitDifference("pubnames_end", Unit->getID(),
2687 "pubnames_begin", Unit->getID(), true);
2688 Asm->EOL("Length of Public Names Info");
2689
2690 EmitLabel("pubnames_begin", Unit->getID());
2691
2692 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2693
2694 EmitSectionOffset("info_begin", "section_info",
2695 Unit->getID(), 0, true, false);
2696 Asm->EOL("Offset of Compilation Unit Info");
2697
2698 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2699 true);
2700 Asm->EOL("Compilation Unit Length");
2701
2702 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2703 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2704 GE = Globals.end(); GI != GE; ++GI) {
2705 const std::string &Name = GI->first;
2706 DIE * Entity = GI->second;
2707
2708 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2709 Asm->EmitString(Name); Asm->EOL("External Name");
2710 }
2711
2712 Asm->EmitInt32(0); Asm->EOL("End Mark");
2713 EmitLabel("pubnames_end", Unit->getID());
2714
2715 Asm->EOL();
2716 }
2717
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002718 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2719 ///
2720 void EmitDebugPubNames() {
2721 // Start the dwarf pubnames section.
2722 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
aslc200b112008-08-16 12:57:46 +00002723
Evan Cheng3e288912009-02-25 07:04:34 +00002724 if (MainCU) {
2725 EmitDebugPubNamesPerCU(MainCU);
2726 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002727 }
Evan Cheng3e288912009-02-25 07:04:34 +00002728
2729 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2730 EmitDebugPubNamesPerCU(CompileUnits[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002731 }
2732
2733 /// EmitDebugStr - Emit visible names into a debug str section.
2734 ///
2735 void EmitDebugStr() {
2736 // Check to see if it is worth the effort.
2737 if (!StringPool.empty()) {
2738 // Start the dwarf str section.
2739 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
aslc200b112008-08-16 12:57:46 +00002740
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002741 // For each of strings in the string pool.
2742 for (unsigned StringID = 1, N = StringPool.size();
2743 StringID <= N; ++StringID) {
2744 // Emit a label for reference from debug information entries.
2745 EmitLabel("string", StringID);
2746 // Emit the string itself.
2747 const std::string &String = StringPool[StringID];
2748 Asm->EmitString(String); Asm->EOL();
2749 }
aslc200b112008-08-16 12:57:46 +00002750
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002751 Asm->EOL();
2752 }
2753 }
2754
2755 /// EmitDebugLoc - Emit visible names into a debug loc section.
2756 ///
2757 void EmitDebugLoc() {
2758 // Start the dwarf loc section.
2759 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
aslc200b112008-08-16 12:57:46 +00002760
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002761 Asm->EOL();
2762 }
2763
2764 /// EmitDebugARanges - Emit visible names into a debug aranges section.
2765 ///
2766 void EmitDebugARanges() {
2767 // Start the dwarf aranges section.
2768 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
aslc200b112008-08-16 12:57:46 +00002769
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002770 // FIXME - Mock up
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002771#if 0
aslc200b112008-08-16 12:57:46 +00002772 CompileUnit *Unit = GetBaseCompileUnit();
2773
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002774 // Don't include size of length
2775 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
aslc200b112008-08-16 12:57:46 +00002776
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002777 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
aslc200b112008-08-16 12:57:46 +00002778
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002779 EmitReference("info_begin", Unit->getID());
2780 Asm->EOL("Offset of Compilation Unit Info");
2781
Dan Gohmancfb72b22007-09-27 23:12:31 +00002782 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002783
2784 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2785
2786 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2787 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2788
2789 // Range 1
2790 EmitReference("text_begin", 0); Asm->EOL("Address");
2791 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2792
2793 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2794 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002795#endif
aslc200b112008-08-16 12:57:46 +00002796
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002797 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002798 }
2799
2800 /// EmitDebugRanges - Emit visible names into a debug ranges section.
2801 ///
2802 void EmitDebugRanges() {
2803 // Start the dwarf ranges section.
2804 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
aslc200b112008-08-16 12:57:46 +00002805
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002806 Asm->EOL();
2807 }
2808
2809 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2810 ///
2811 void EmitDebugMacInfo() {
Scott Michel79f01f52009-01-26 22:32:51 +00002812 if (TAI->doesSupportMacInfoSection()) {
2813 // Start the dwarf macinfo section.
2814 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
aslc200b112008-08-16 12:57:46 +00002815
Scott Michel79f01f52009-01-26 22:32:51 +00002816 Asm->EOL();
2817 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002818 }
2819
Bill Wendling278a3922009-03-10 21:47:45 +00002820 /// GetOrCreateSourceID - Look up the source id with the given directory and
2821 /// source file names. If none currently exists, create a new id and insert it
2822 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
2823 /// as well.
2824 unsigned GetOrCreateSourceID(const std::string &DirName,
2825 const std::string &FileName) {
2826 unsigned DId;
2827 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
2828 if (DI != DirectoryIdMap.end()) {
2829 DId = DI->getValue();
2830 } else {
2831 DId = DirectoryNames.size() + 1;
2832 DirectoryIdMap[DirName] = DId;
2833 DirectoryNames.push_back(DirName);
2834 }
2835
2836 unsigned FId;
2837 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
2838 if (FI != SourceFileIdMap.end()) {
2839 FId = FI->getValue();
2840 } else {
2841 FId = SourceFileNames.size() + 1;
2842 SourceFileIdMap[FileName] = FId;
2843 SourceFileNames.push_back(FileName);
2844 }
2845
2846 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
2847 SourceIdMap.find(std::make_pair(DId, FId));
2848 if (SI != SourceIdMap.end())
2849 return SI->second;
2850
2851 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
2852 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
2853 SourceIds.push_back(std::make_pair(DId, FId));
2854
2855 return SrcId;
2856 }
2857
Evan Cheng3e288912009-02-25 07:04:34 +00002858 void ConstructCompileUnit(GlobalVariable *GV) {
2859 DICompileUnit DIUnit(GV);
Bill Wendling1c5842b2009-03-09 05:04:40 +00002860 std::string Dir, FN, Prod;
Bill Wendling278a3922009-03-10 21:47:45 +00002861 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
Bill Wendling1c5842b2009-03-09 05:04:40 +00002862 DIUnit.getFilename(FN));
Evan Cheng3e288912009-02-25 07:04:34 +00002863
2864 DIE *Die = new DIE(DW_TAG_compile_unit);
2865 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2866 DWLabel("section_line", 0), DWLabel("section_line", 0),
2867 false);
Bill Wendling1c5842b2009-03-09 05:04:40 +00002868 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer(Prod));
Evan Cheng3e288912009-02-25 07:04:34 +00002869 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendling1c5842b2009-03-09 05:04:40 +00002870 AddString(Die, DW_AT_name, DW_FORM_string, FN);
2871 if (!Dir.empty())
2872 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Cheng3e288912009-02-25 07:04:34 +00002873 if (DIUnit.isOptimized())
2874 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendling1c5842b2009-03-09 05:04:40 +00002875 std::string Flags;
2876 DIUnit.getFlags(Flags);
Evan Cheng3e288912009-02-25 07:04:34 +00002877 if (!Flags.empty())
2878 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
2879 unsigned RVer = DIUnit.getRunTimeVersion();
2880 if (RVer)
2881 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
2882
2883 CompileUnit *Unit = new CompileUnit(ID, Die);
2884 if (DIUnit.isMain()) {
2885 assert(!MainCU && "Multiple main compile units are found!");
2886 MainCU = Unit;
2887 }
2888 CompileUnitMap[DIUnit.getGV()] = Unit;
2889 CompileUnits.push_back(Unit);
2890 }
2891
Devang Patel289f2362009-01-05 23:11:11 +00002892 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Patelb3907da2009-01-05 23:03:32 +00002893 void ConstructCompileUnits() {
Evan Cheng3e288912009-02-25 07:04:34 +00002894 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.compile_units");
2895 if (!Root)
2896 return;
2897 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2898 "Malformed compile unit descriptor anchor type");
2899 Constant *RootC = cast<Constant>(*Root->use_begin());
2900 assert(RootC->hasNUsesOrMore(1) &&
2901 "Malformed compile unit descriptor anchor type");
2902 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2903 UI != UE; ++UI)
2904 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2905 UUI != UUE; ++UUI) {
2906 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2907 ConstructCompileUnit(GV);
Devang Patel2ae1db52009-01-30 18:20:31 +00002908 }
Evan Cheng3e288912009-02-25 07:04:34 +00002909 }
2910
2911 bool ConstructGlobalVariableDIE(GlobalVariable *GV) {
2912 DIGlobalVariable DI_GV(GV);
2913 CompileUnit *DW_Unit = MainCU;
2914 if (!DW_Unit)
2915 DW_Unit = FindCompileUnit(DI_GV.getCompileUnit());
2916
2917 // Check for pre-existence.
2918 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
2919 if (Slot)
2920 return false;
2921
2922 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
2923
2924 // Add address.
2925 DIEBlock *Block = new DIEBlock();
2926 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
2927 AddObjectLabel(Block, 0, DW_FORM_udata,
2928 Asm->getGlobalLinkName(DI_GV.getGlobal()));
2929 AddBlock(VariableDie, DW_AT_location, 0, Block);
2930
2931 // Add to map.
2932 Slot = VariableDie;
2933 // Add to context owner.
2934 DW_Unit->getDie()->AddChild(VariableDie);
2935 // Expose as global. FIXME - need to check external flag.
Bill Wendling1c5842b2009-03-09 05:04:40 +00002936 std::string Name;
2937 DW_Unit->AddGlobal(DI_GV.getName(Name), VariableDie);
Evan Cheng3e288912009-02-25 07:04:34 +00002938 return true;
Devang Patelb3907da2009-01-05 23:03:32 +00002939 }
2940
Devang Patel289f2362009-01-05 23:11:11 +00002941 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patela9169c32009-02-24 00:02:15 +00002942 /// visible global variables. Return true if at least one global DIE is
2943 /// created.
2944 bool ConstructGlobalVariableDIEs() {
Evan Cheng3e288912009-02-25 07:04:34 +00002945 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
2946 if (!Root)
2947 return false;
Devang Patel289f2362009-01-05 23:11:11 +00002948
Evan Cheng3e288912009-02-25 07:04:34 +00002949 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2950 "Malformed global variable descriptor anchor type");
2951 Constant *RootC = cast<Constant>(*Root->use_begin());
2952 assert(RootC->hasNUsesOrMore(1) &&
2953 "Malformed global variable descriptor anchor type");
Devang Patel289f2362009-01-05 23:11:11 +00002954
Evan Cheng3e288912009-02-25 07:04:34 +00002955 bool Result = false;
2956 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2957 UI != UE; ++UI)
2958 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2959 UUI != UUE; ++UUI) {
2960 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2961 Result |= ConstructGlobalVariableDIE(GV);
2962 }
2963 return Result;
2964 }
Devang Patel289f2362009-01-05 23:11:11 +00002965
Evan Cheng3e288912009-02-25 07:04:34 +00002966 bool ConstructSubprogram(GlobalVariable *GV) {
2967 DISubprogram SP(GV);
2968 CompileUnit *Unit = MainCU;
2969 if (!Unit)
2970 Unit = FindCompileUnit(SP.getCompileUnit());
Devang Patel289f2362009-01-05 23:11:11 +00002971
Evan Cheng3e288912009-02-25 07:04:34 +00002972 // Check for pre-existence.
2973 DIE *&Slot = Unit->getDieMapSlotFor(GV);
2974 if (Slot)
2975 return false;
2976
2977 if (!SP.isDefinition())
2978 // This is a method declaration which will be handled while
2979 // constructing class type.
2980 return false;
2981
2982 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
2983
2984 // Add to map.
2985 Slot = SubprogramDie;
2986 // Add to context owner.
2987 Unit->getDie()->AddChild(SubprogramDie);
2988 // Expose as global.
Bill Wendling1c5842b2009-03-09 05:04:40 +00002989 std::string Name;
2990 Unit->AddGlobal(SP.getName(Name), SubprogramDie);
Evan Cheng3e288912009-02-25 07:04:34 +00002991 return true;
Devang Patel289f2362009-01-05 23:11:11 +00002992 }
2993
Devang Patele6caf012009-01-05 23:21:35 +00002994 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patela9169c32009-02-24 00:02:15 +00002995 /// subprograms. Return true if at least one subprogram DIE is created.
2996 bool ConstructSubprograms() {
Evan Cheng3e288912009-02-25 07:04:34 +00002997 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
2998 if (!Root)
2999 return false;
Devang Patele6caf012009-01-05 23:21:35 +00003000
Evan Cheng3e288912009-02-25 07:04:34 +00003001 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3002 "Malformed subprogram descriptor anchor type");
3003 Constant *RootC = cast<Constant>(*Root->use_begin());
3004 assert(RootC->hasNUsesOrMore(1) &&
3005 "Malformed subprogram descriptor anchor type");
Devang Patele6caf012009-01-05 23:21:35 +00003006
Evan Cheng3e288912009-02-25 07:04:34 +00003007 bool Result = false;
3008 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3009 UI != UE; ++UI)
3010 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3011 UUI != UUE; ++UUI) {
3012 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3013 Result |= ConstructSubprogram(GV);
3014 }
3015 return Result;
Devang Patele6caf012009-01-05 23:21:35 +00003016 }
3017
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003018public:
3019 //===--------------------------------------------------------------------===//
3020 // Main entry points.
3021 //
Owen Anderson847b99b2008-08-21 00:14:44 +00003022 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendlingd9308a62009-03-10 21:23:25 +00003023 : Dwarf(OS, A, T, "dbg"), MainCU(0),
3024 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
3025 ValuesSet(InitValuesSetSize), Values(), StringPool(), SectionMap(),
3026 SectionSourceLines(), didInitial(false), shouldEmit(false),
3027 RootDbgScope(0), DebugTimer(0) {
3028 if (TimePassesIsEnabled)
3029 DebugTimer = new Timer("Dwarf Debug Writer",
3030 *getDwarfTimerGroup());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003031 }
3032 virtual ~DwarfDebug() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003033 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3034 delete Values[j];
Bill Wendlingd9308a62009-03-10 21:23:25 +00003035
3036 delete DebugTimer;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003037 }
3038
Bill Wendling278a3922009-03-10 21:47:45 +00003039 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
3040 /// be emitted.
3041 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
3042
Devang Patel9304b382009-01-06 21:07:30 +00003043 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3044 /// This is inovked by the target AsmPrinter.
Devang Patel91d27b02009-01-12 23:09:42 +00003045 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003046 if (TimePassesIsEnabled)
3047 DebugTimer->startTimer();
3048
Bill Wendling6baa18d2009-02-03 21:38:21 +00003049 // Create all the compile unit DIEs.
3050 ConstructCompileUnits();
Devang Patel91d27b02009-01-12 23:09:42 +00003051
Bill Wendlingd9308a62009-03-10 21:23:25 +00003052 if (CompileUnits.empty()) {
3053 if (TimePassesIsEnabled)
3054 DebugTimer->startTimer();
3055
Bill Wendling6baa18d2009-02-03 21:38:21 +00003056 return;
Bill Wendlingd9308a62009-03-10 21:23:25 +00003057 }
Devang Patel91d27b02009-01-12 23:09:42 +00003058
Devang Patela9169c32009-02-24 00:02:15 +00003059 // Create DIEs for each of the externally visible global variables.
3060 bool globalDIEs = ConstructGlobalVariableDIEs();
3061
3062 // Create DIEs for each of the externally visible subprograms.
3063 bool subprogramDIEs = ConstructSubprograms();
3064
3065 // If there is not any debug info available for any global variables
3066 // and any subprograms then there is not any debug info to emit.
Bill Wendlingd9308a62009-03-10 21:23:25 +00003067 if (!globalDIEs && !subprogramDIEs) {
3068 if (TimePassesIsEnabled)
3069 DebugTimer->startTimer();
3070
Devang Patela9169c32009-02-24 00:02:15 +00003071 return;
Bill Wendlingd9308a62009-03-10 21:23:25 +00003072 }
Devang Patela9169c32009-02-24 00:02:15 +00003073
Bill Wendling6baa18d2009-02-03 21:38:21 +00003074 MMI = mmi;
3075 shouldEmit = true;
3076 MMI->setDebugInfoAvailability(true);
Devang Patel9304b382009-01-06 21:07:30 +00003077
Bill Wendling6baa18d2009-02-03 21:38:21 +00003078 // Prime section data.
3079 SectionMap.insert(TAI->getTextSection());
Devang Patel9304b382009-01-06 21:07:30 +00003080
Bill Wendling6baa18d2009-02-03 21:38:21 +00003081 // Print out .file directives to specify files for .loc directives. These
3082 // are printed out early so that they precede any .loc directives.
3083 if (TAI->hasDotLocAndDotFile()) {
Evan Cheng3e288912009-02-25 07:04:34 +00003084 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
3085 // Remember source id starts at 1.
3086 std::pair<unsigned, unsigned> Id = getSourceDirsectoryAndFileIds(i);
3087 sys::Path FullPath(getSourceDirectoryName(Id.first));
3088 bool AppendOk =
3089 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendling6baa18d2009-02-03 21:38:21 +00003090 assert(AppendOk && "Could not append filename to directory!");
3091 AppendOk = false;
3092 Asm->EmitFile(i, FullPath.toString());
3093 Asm->EOL();
Devang Patel9304b382009-01-06 21:07:30 +00003094 }
Bill Wendling6baa18d2009-02-03 21:38:21 +00003095 }
Devang Patel9304b382009-01-06 21:07:30 +00003096
Bill Wendling6baa18d2009-02-03 21:38:21 +00003097 // Emit initial sections
3098 EmitInitial();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003099
3100 if (TimePassesIsEnabled)
3101 DebugTimer->stopTimer();
Devang Patel9304b382009-01-06 21:07:30 +00003102 }
3103
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003104 /// BeginModule - Emit all Dwarf sections that should come prior to the
3105 /// content.
3106 void BeginModule(Module *M) {
3107 this->M = M;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003108 }
3109
3110 /// EndModule - Emit all Dwarf sections that should come after the content.
3111 ///
3112 void EndModule() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003113 if (!ShouldEmitDwarfDebug())
3114 return;
3115
3116 if (TimePassesIsEnabled)
3117 DebugTimer->startTimer();
aslc200b112008-08-16 12:57:46 +00003118
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003119 // Standard sections final addresses.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003120 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003121 EmitLabel("text_end", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00003122 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003123 EmitLabel("data_end", 0);
aslc200b112008-08-16 12:57:46 +00003124
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003125 // End text sections.
3126 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003127 Asm->SwitchToSection(SectionMap[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003128 EmitLabel("section_end", i);
3129 }
3130
3131 // Emit common frame information.
3132 EmitCommonDebugFrame();
3133
3134 // Emit function debug frame information
3135 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3136 E = DebugFrames.end(); I != E; ++I)
3137 EmitFunctionDebugFrame(*I);
3138
3139 // Compute DIE offsets and sizes.
3140 SizeAndOffsets();
aslc200b112008-08-16 12:57:46 +00003141
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003142 // Emit all the DIEs into a debug info section
3143 EmitDebugInfo();
aslc200b112008-08-16 12:57:46 +00003144
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003145 // Corresponding abbreviations into a abbrev section.
3146 EmitAbbreviations();
aslc200b112008-08-16 12:57:46 +00003147
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003148 // Emit source line correspondence into a debug line section.
3149 EmitDebugLines();
aslc200b112008-08-16 12:57:46 +00003150
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003151 // Emit info into a debug pubnames section.
3152 EmitDebugPubNames();
aslc200b112008-08-16 12:57:46 +00003153
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003154 // Emit info into a debug str section.
3155 EmitDebugStr();
aslc200b112008-08-16 12:57:46 +00003156
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003157 // Emit info into a debug loc section.
3158 EmitDebugLoc();
aslc200b112008-08-16 12:57:46 +00003159
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003160 // Emit info into a debug aranges section.
3161 EmitDebugARanges();
aslc200b112008-08-16 12:57:46 +00003162
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003163 // Emit info into a debug ranges section.
3164 EmitDebugRanges();
aslc200b112008-08-16 12:57:46 +00003165
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003166 // Emit info into a debug macinfo section.
3167 EmitDebugMacInfo();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003168
3169 if (TimePassesIsEnabled)
3170 DebugTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003171 }
3172
aslc200b112008-08-16 12:57:46 +00003173 /// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003174 /// emitted immediately after the function entry point.
3175 void BeginFunction(MachineFunction *MF) {
Bill Wendling50db0792009-02-20 00:44:43 +00003176 if (!ShouldEmitDwarfDebug()) return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003177
Bill Wendlingd9308a62009-03-10 21:23:25 +00003178 if (TimePassesIsEnabled)
3179 DebugTimer->startTimer();
3180
3181 this->MF = MF;
3182
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003183 // Begin accumulating function debug information.
3184 MMI->BeginFunction(MF);
aslc200b112008-08-16 12:57:46 +00003185
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003186 // Assumes in correct section after the entry point.
3187 EmitLabel("func_begin", ++SubprogramCount);
Evan Chenga53c40a2008-02-01 09:10:45 +00003188
3189 // Emit label for the implicitly defined dbg.stoppoint at the start of
3190 // the function.
Devang Patel35a078f2009-01-12 22:54:42 +00003191 if (!Lines.empty()) {
3192 const SrcLineInfo &LineInfo = Lines[0];
Andrew Lenharth42f91402008-04-03 17:37:43 +00003193 Asm->printLabel(LineInfo.getLabelID());
3194 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00003195
3196 if (TimePassesIsEnabled)
3197 DebugTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003198 }
aslc200b112008-08-16 12:57:46 +00003199
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003200 /// EndFunction - Gather and emit post-function debug information.
3201 ///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003202 void EndFunction(MachineFunction *MF) {
Bill Wendling50db0792009-02-20 00:44:43 +00003203 if (!ShouldEmitDwarfDebug()) return;
aslc200b112008-08-16 12:57:46 +00003204
Bill Wendlingd9308a62009-03-10 21:23:25 +00003205 if (TimePassesIsEnabled)
3206 DebugTimer->startTimer();
3207
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003208 // Define end label for subprogram.
3209 EmitLabel("func_end", SubprogramCount);
aslc200b112008-08-16 12:57:46 +00003210
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003211 // Get function line info.
Devang Patel35a078f2009-01-12 22:54:42 +00003212 if (!Lines.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003213 // Get section line info.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003214 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003215 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patel35a078f2009-01-12 22:54:42 +00003216 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003217 // Append the function info to section info.
3218 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patel35a078f2009-01-12 22:54:42 +00003219 Lines.begin(), Lines.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003220 }
aslc200b112008-08-16 12:57:46 +00003221
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003222 // Construct scopes for subprogram.
Devang Patel6ccd57e2009-01-13 00:20:51 +00003223 if (RootDbgScope)
3224 ConstructRootDbgScope(RootDbgScope);
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003225 else
3226 // FIXME: This is wrong. We are essentially getting past a problem with
3227 // debug information not being able to handle unreachable blocks that have
3228 // debug information in them. In particular, those unreachable blocks that
3229 // have "region end" info in them. That situation results in the "root
3230 // scope" not being created. If that's the case, then emit a "default"
3231 // scope, i.e., one that encompasses the whole function. This isn't
3232 // desirable. And a better way of handling this (and all of the debugging
3233 // information) needs to be explored.
Devang Patel6ccd57e2009-01-13 00:20:51 +00003234 ConstructDefaultDbgScope(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003235
3236 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3237 MMI->getFrameMoves()));
Devang Patela4162952009-01-12 18:48:36 +00003238
3239 // Clear debug info
3240 if (RootDbgScope) {
3241 delete RootDbgScope;
3242 DbgScopeMap.clear();
3243 RootDbgScope = NULL;
3244 }
Devang Patelcb59fd42009-01-12 19:17:34 +00003245
Bill Wendlingd9308a62009-03-10 21:23:25 +00003246 Lines.clear();
3247
3248 if (TimePassesIsEnabled)
3249 DebugTimer->stopTimer();
3250 }
Devang Patelcb59fd42009-01-12 19:17:34 +00003251
Devang Patel2da0cc42009-01-15 23:41:32 +00003252 /// ValidDebugInfo - Return true if V represents valid debug info value.
3253 bool ValidDebugInfo(Value *V) {
Devang Patel208098b2009-01-19 23:21:49 +00003254 if (!V)
3255 return false;
3256
Devang Patel4d92dded2009-01-16 01:49:46 +00003257 if (!shouldEmit)
3258 return false;
3259
Devang Patel2da0cc42009-01-15 23:41:32 +00003260 GlobalVariable *GV = getGlobalVariable(V);
3261 if (!GV)
3262 return false;
Duncan Sands19d161f2009-03-07 15:45:40 +00003263
3264 if (!GV->hasInternalLinkage () && !GV->hasLinkOnceLinkage())
Devang Patel2da0cc42009-01-15 23:41:32 +00003265 return false;
3266
Bill Wendlingd9308a62009-03-10 21:23:25 +00003267 if (TimePassesIsEnabled)
3268 DebugTimer->startTimer();
3269
Devang Patel2da0cc42009-01-15 23:41:32 +00003270 DIDescriptor DI(GV);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003271
Devang Patel2da0cc42009-01-15 23:41:32 +00003272 // Check current version. Allow Version6 for now.
3273 unsigned Version = DI.getVersion();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003274 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6) {
3275 if (TimePassesIsEnabled)
3276 DebugTimer->stopTimer();
3277
Devang Patel2da0cc42009-01-15 23:41:32 +00003278 return false;
Bill Wendlingd9308a62009-03-10 21:23:25 +00003279 }
Devang Patel2da0cc42009-01-15 23:41:32 +00003280
Devang Patel208098b2009-01-19 23:21:49 +00003281 unsigned Tag = DI.getTag();
3282 switch (Tag) {
3283 case DW_TAG_variable:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003284 assert(DIVariable(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003285 break;
3286 case DW_TAG_compile_unit:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003287 assert(DICompileUnit(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003288 break;
3289 case DW_TAG_subprogram:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003290 assert(DISubprogram(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003291 break;
3292 default:
3293 break;
3294 }
3295
Bill Wendlingd9308a62009-03-10 21:23:25 +00003296 if (TimePassesIsEnabled)
3297 DebugTimer->stopTimer();
3298
Devang Patel2da0cc42009-01-15 23:41:32 +00003299 return true;
3300 }
3301
Devang Patelcb59fd42009-01-12 19:17:34 +00003302 /// RecordSourceLine - Records location information and associates it with a
3303 /// label. Returns a unique label ID used to generate a label and provide
3304 /// correspondence to the source line list.
3305 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003306 if (TimePassesIsEnabled)
3307 DebugTimer->startTimer();
3308
Evan Cheng3e288912009-02-25 07:04:34 +00003309 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendling6baa18d2009-02-03 21:38:21 +00003310 assert(Unit && "Unable to find CompileUnit");
Devang Patelcb59fd42009-01-12 19:17:34 +00003311 unsigned ID = MMI->NextLabelID();
3312 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
Bill Wendlingd9308a62009-03-10 21:23:25 +00003313
3314 if (TimePassesIsEnabled)
3315 DebugTimer->stopTimer();
3316
Devang Patelcb59fd42009-01-12 19:17:34 +00003317 return ID;
3318 }
3319
3320 /// RecordSourceLine - Records location information and associates it with a
3321 /// label. Returns a unique label ID used to generate a label and provide
3322 /// correspondence to the source line list.
3323 unsigned RecordSourceLine(unsigned Line, unsigned Col, unsigned Src) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003324 if (TimePassesIsEnabled)
3325 DebugTimer->startTimer();
3326
Devang Patelcb59fd42009-01-12 19:17:34 +00003327 unsigned ID = MMI->NextLabelID();
3328 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
Bill Wendlingd9308a62009-03-10 21:23:25 +00003329
3330 if (TimePassesIsEnabled)
3331 DebugTimer->stopTimer();
3332
Devang Patelcb59fd42009-01-12 19:17:34 +00003333 return ID;
3334 }
3335
Bill Wendling278a3922009-03-10 21:47:45 +00003336 /// getRecordSourceLineCount - Return the number of source lines in the debug
3337 /// info.
3338 unsigned getRecordSourceLineCount() const {
Devang Patelcb59fd42009-01-12 19:17:34 +00003339 return Lines.size();
3340 }
3341
Evan Cheng3e288912009-02-25 07:04:34 +00003342 /// getNumSourceFiles - Return the number of source files in the debug info.
Evan Cheng3e288912009-02-25 07:04:34 +00003343 unsigned getNumSourceFiles() const {
3344 return SourceFileNames.size();
3345 }
3346
Bill Wendling278a3922009-03-10 21:47:45 +00003347 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
3348 /// timed. Look up the source id with the given directory and source file
3349 /// names. If none currently exists, create a new id and insert it in the
3350 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
3351 /// well.
Evan Cheng3e288912009-02-25 07:04:34 +00003352 unsigned getOrCreateSourceID(const std::string &DirName,
3353 const std::string &FileName) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003354 if (TimePassesIsEnabled)
3355 DebugTimer->startTimer();
3356
Bill Wendling278a3922009-03-10 21:47:45 +00003357 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003358
3359 if (TimePassesIsEnabled)
3360 DebugTimer->stopTimer();
3361
Evan Cheng3e288912009-02-25 07:04:34 +00003362 return SrcId;
Devang Patelcb59fd42009-01-12 19:17:34 +00003363 }
3364
3365 /// RecordRegionStart - Indicate the start of a region.
Devang Patelcb59fd42009-01-12 19:17:34 +00003366 unsigned RecordRegionStart(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003367 if (TimePassesIsEnabled)
3368 DebugTimer->startTimer();
3369
Devang Patelcb59fd42009-01-12 19:17:34 +00003370 DbgScope *Scope = getOrCreateScope(V);
3371 unsigned ID = MMI->NextLabelID();
3372 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003373
3374 if (TimePassesIsEnabled)
3375 DebugTimer->stopTimer();
3376
Devang Patelcb59fd42009-01-12 19:17:34 +00003377 return ID;
3378 }
3379
3380 /// RecordRegionEnd - Indicate the end of a region.
Devang Patelcb59fd42009-01-12 19:17:34 +00003381 unsigned RecordRegionEnd(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003382 if (TimePassesIsEnabled)
3383 DebugTimer->startTimer();
3384
Devang Patelcb59fd42009-01-12 19:17:34 +00003385 DbgScope *Scope = getOrCreateScope(V);
3386 unsigned ID = MMI->NextLabelID();
3387 Scope->setEndLabelID(ID);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003388
3389 if (TimePassesIsEnabled)
3390 DebugTimer->stopTimer();
3391
Devang Patelcb59fd42009-01-12 19:17:34 +00003392 return ID;
3393 }
3394
3395 /// RecordVariable - Indicate the declaration of a local variable.
Devang Patelcb59fd42009-01-12 19:17:34 +00003396 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003397 if (TimePassesIsEnabled)
3398 DebugTimer->startTimer();
3399
Devang Patel2560d922009-01-15 18:25:17 +00003400 DIDescriptor Desc(GV);
3401 DbgScope *Scope = NULL;
Bill Wendlingd9308a62009-03-10 21:23:25 +00003402
Devang Patel2560d922009-01-15 18:25:17 +00003403 if (Desc.getTag() == DW_TAG_variable) {
3404 // GV is a global variable.
3405 DIGlobalVariable DG(GV);
3406 Scope = getOrCreateScope(DG.getContext().getGV());
3407 } else {
3408 // or GV is a local variable.
3409 DIVariable DV(GV);
3410 Scope = getOrCreateScope(DV.getContext().getGV());
3411 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00003412
Bill Wendling6baa18d2009-02-03 21:38:21 +00003413 assert(Scope && "Unable to find variable' scope");
Devang Patel7c8a2772009-01-16 19:28:14 +00003414 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelcb59fd42009-01-12 19:17:34 +00003415 Scope->AddVariable(DV);
Bill Wendlingd9308a62009-03-10 21:23:25 +00003416
3417 if (TimePassesIsEnabled)
3418 DebugTimer->stopTimer();
Devang Patelcb59fd42009-01-12 19:17:34 +00003419 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003420};
3421
3422//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00003423/// DwarfException - Emits Dwarf exception handling directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003424///
3425class DwarfException : public Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003426 struct FunctionEHFrameInfo {
3427 std::string FnName;
3428 unsigned Number;
3429 unsigned PersonalityIndex;
3430 bool hasCalls;
3431 bool hasLandingPads;
3432 std::vector<MachineMove> Moves;
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003433 const Function * function;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003434
3435 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3436 bool hC, bool hL,
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003437 const std::vector<MachineMove> &M,
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003438 const Function *f):
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003439 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003440 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003441 };
3442
3443 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen85535762008-04-02 00:25:04 +00003444
3445 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3446 /// be emitted.
3447 bool shouldEmitTable;
3448
3449 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3450 /// should be emitted.
3451 bool shouldEmitMoves;
3452
3453 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3454 /// should be emitted.
3455 bool shouldEmitTableModule;
3456
aslc200b112008-08-16 12:57:46 +00003457 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen85535762008-04-02 00:25:04 +00003458 /// should be emitted.
3459 bool shouldEmitMovesModule;
Duncan Sands96144f92008-05-07 19:11:09 +00003460
Bill Wendlingd9308a62009-03-10 21:23:25 +00003461 /// ExceptionTimer - Timer for the Dwarf exception writer.
3462 Timer *ExceptionTimer;
3463
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003464 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3465 ///
3466 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
3467 // Size and sign of stack growth.
3468 int stackGrowth =
3469 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3470 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00003471 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003472
3473 // Begin eh frame section.
3474 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling189bde72008-12-24 08:05:17 +00003475
3476 if (!TAI->doesRequireNonLocalEHFrameLabel())
3477 O << TAI->getEHGlobalPrefix();
3478 O << "EH_frame" << Index << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003479 EmitLabel("section_eh_frame", Index);
3480
3481 // Define base labels.
3482 EmitLabel("eh_frame_common", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003483
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003484 // Define the eh frame length.
3485 EmitDifference("eh_frame_common_end", Index,
3486 "eh_frame_common_begin", Index, true);
3487 Asm->EOL("Length of Common Information Entry");
3488
3489 // EH frame header.
3490 EmitLabel("eh_frame_common_begin", Index);
3491 Asm->EmitInt32((int)0);
3492 Asm->EOL("CIE Identifier Tag");
3493 Asm->EmitInt8(DW_CIE_VERSION);
3494 Asm->EOL("CIE Version");
Duncan Sands96144f92008-05-07 19:11:09 +00003495
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003496 // The personality presence indicates that language specific information
3497 // will show up in the eh frame.
3498 Asm->EmitString(Personality ? "zPLR" : "zR");
3499 Asm->EOL("CIE Augmentation");
Duncan Sands96144f92008-05-07 19:11:09 +00003500
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003501 // Round out reader.
3502 Asm->EmitULEB128Bytes(1);
3503 Asm->EOL("CIE Code Alignment Factor");
3504 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands96144f92008-05-07 19:11:09 +00003505 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00003506 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands96144f92008-05-07 19:11:09 +00003507 Asm->EOL("CIE Return Address Column");
3508
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003509 // If there is a personality, we need to indicate the functions location.
3510 if (Personality) {
3511 Asm->EmitULEB128Bytes(7);
3512 Asm->EOL("Augmentation Size");
Bill Wendling2d369922007-09-11 17:20:55 +00003513
Duncan Sands96144f92008-05-07 19:11:09 +00003514 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendling2d369922007-09-11 17:20:55 +00003515 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands96144f92008-05-07 19:11:09 +00003516 Asm->EOL("Personality (pcrel sdata4 indirect)");
3517 } else {
Bill Wendling2d369922007-09-11 17:20:55 +00003518 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands96144f92008-05-07 19:11:09 +00003519 Asm->EOL("Personality (pcrel sdata4)");
3520 }
Bill Wendling2d369922007-09-11 17:20:55 +00003521
Duncan Sands96144f92008-05-07 19:11:09 +00003522 PrintRelDirective(true);
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003523 O << TAI->getPersonalityPrefix();
3524 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3525 O << TAI->getPersonalitySuffix();
Duncan Sands4cc39532008-05-08 12:33:11 +00003526 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3527 O << "-" << TAI->getPCSymbol();
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003528 Asm->EOL("Personality");
Bill Wendling38cb7c92007-08-25 00:51:55 +00003529
Duncan Sands96144f92008-05-07 19:11:09 +00003530 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3531 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003532
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003533 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3534 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003535 } else {
3536 Asm->EmitULEB128Bytes(1);
3537 Asm->EOL("Augmentation Size");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003538
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003539 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3540 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003541 }
3542
3543 // Indicate locations of general callee saved registers in frame.
3544 std::vector<MachineMove> Moves;
3545 RI->getInitialFrameState(Moves);
Dale Johannesenf5a11532007-11-13 19:13:01 +00003546 EmitFrameMoves(NULL, 0, Moves, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003547
Dale Johannesen388f20f2008-04-30 00:43:29 +00003548 // On Darwin the linker honors the alignment of eh_frame, which means it
3549 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3550 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003551 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003552 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003553 EmitLabel("eh_frame_common_end", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003554
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003555 Asm->EOL();
3556 }
Duncan Sands96144f92008-05-07 19:11:09 +00003557
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003558 /// EmitEHFrame - Emit function exception frame information.
3559 ///
3560 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003561 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3562
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003563 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3564
3565 // Externally visible entry into the functions eh frame info.
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003566 // If the corresponding function is static, this should not be
3567 // externally visible.
Rafael Espindolaa168fc92009-01-15 20:18:42 +00003568 if (linkage != Function::InternalLinkage &&
Devang Patel245446c2009-01-17 08:05:14 +00003569 linkage != Function::PrivateLinkage) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003570 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3571 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3572 }
3573
Dale Johannesenf09b5992008-01-10 02:03:30 +00003574 // If corresponding function is weak definition, this should be too.
Duncan Sands19d161f2009-03-07 15:45:40 +00003575 if ((linkage == Function::WeakAnyLinkage ||
3576 linkage == Function::WeakODRLinkage ||
3577 linkage == Function::LinkOnceAnyLinkage ||
3578 linkage == Function::LinkOnceODRLinkage) &&
Dale Johannesenf09b5992008-01-10 02:03:30 +00003579 TAI->getWeakDefDirective())
3580 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3581
3582 // If there are no calls then you can't unwind. This may mean we can
3583 // omit the EH Frame, but some environments do not handle weak absolute
aslc200b112008-08-16 12:57:46 +00003584 // symbols.
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003585 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesena9b3e482008-04-08 00:10:24 +00003586 // unwind info is to be available for non-EH uses.
Dale Johannesenf09b5992008-01-10 02:03:30 +00003587 if (!EHFrameInfo.hasCalls &&
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003588 !UnwindTablesMandatory &&
Duncan Sands19d161f2009-03-07 15:45:40 +00003589 ((linkage != Function::WeakAnyLinkage &&
3590 linkage != Function::WeakODRLinkage &&
3591 linkage != Function::LinkOnceAnyLinkage &&
3592 linkage != Function::LinkOnceODRLinkage) ||
Dale Johannesenf09b5992008-01-10 02:03:30 +00003593 !TAI->getWeakDefDirective() ||
3594 TAI->getSupportsWeakOmittedEHFrame()))
aslc200b112008-08-16 12:57:46 +00003595 {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003596 O << EHFrameInfo.FnName << " = 0\n";
aslc200b112008-08-16 12:57:46 +00003597 // This name has no connection to the function, so it might get
3598 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003599 // dead-stripping unconditionally.
3600 if (const char *UsedDirective = TAI->getUsedDirective())
3601 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003602 } else {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003603 O << EHFrameInfo.FnName << ":\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003604
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003605 // EH frame header.
3606 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3607 "eh_frame_begin", EHFrameInfo.Number, true);
3608 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00003609
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003610 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
3611
Bill Wendling189bde72008-12-24 08:05:17 +00003612 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3613 PrintRelDirective(true, true);
3614 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3615
3616 if (!TAI->isAbsoluteEHSectionOffsets())
3617 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3618 } else {
3619 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3620 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3621 true, true, false);
3622 }
3623
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003624 Asm->EOL("FDE CIE offset");
3625
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003626 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003627 Asm->EOL("FDE initial location");
3628 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003629 "eh_func_begin", EHFrameInfo.Number, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003630 Asm->EOL("FDE address range");
Duncan Sands96144f92008-05-07 19:11:09 +00003631
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003632 // If there is a personality and landing pads then point to the language
3633 // specific data area in the exception table.
3634 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands96144f92008-05-07 19:11:09 +00003635 Asm->EmitULEB128Bytes(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003636 Asm->EOL("Augmentation size");
Duncan Sands96144f92008-05-07 19:11:09 +00003637
3638 if (EHFrameInfo.hasLandingPads)
3639 EmitReference("exception", EHFrameInfo.Number, true, true);
3640 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003641 Asm->EmitInt32((int)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003642 Asm->EOL("Language Specific Data Area");
3643 } else {
3644 Asm->EmitULEB128Bytes(0);
3645 Asm->EOL("Augmentation size");
3646 }
Duncan Sands96144f92008-05-07 19:11:09 +00003647
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003648 // Indicate locations of function specific callee saved registers in
3649 // frame.
Devang Patelb28de842009-01-17 08:01:33 +00003650 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00003651 true);
aslc200b112008-08-16 12:57:46 +00003652
Dale Johannesen388f20f2008-04-30 00:43:29 +00003653 // On Darwin the linker honors the alignment of eh_frame, which means it
3654 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3655 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003656 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003657 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003658 EmitLabel("eh_frame_end", EHFrameInfo.Number);
aslc200b112008-08-16 12:57:46 +00003659
3660 // If the function is marked used, this table should be also. We cannot
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003661 // make the mark unconditional in this case, since retaining the table
aslc200b112008-08-16 12:57:46 +00003662 // also retains the function in this case, and there is code around
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003663 // that depends on unused functions (calling undefined externals) being
3664 // dead-stripped to link correctly. Yes, there really is.
3665 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3666 if (const char *UsedDirective = TAI->getUsedDirective())
3667 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3668 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003669 }
3670
Duncan Sands241a0c92007-09-05 11:27:52 +00003671 /// EmitExceptionTable - Emit landing pads and actions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003672 ///
3673 /// The general organization of the table is complex, but the basic concepts
3674 /// are easy. First there is a header which describes the location and
3675 /// organization of the three components that follow.
3676 /// 1. The landing pad site information describes the range of code covered
3677 /// by the try. In our case it's an accumulation of the ranges covered
3678 /// by the invokes in the try. There is also a reference to the landing
3679 /// pad that handles the exception once processed. Finally an index into
3680 /// the actions table.
3681 /// 2. The action table, in our case, is composed of pairs of type ids
3682 /// and next action offset. Starting with the action index from the
3683 /// landing pad site, each type Id is checked for a match to the current
3684 /// exception. If it matches then the exception and type id are passed
3685 /// on to the landing pad. Otherwise the next action is looked up. This
3686 /// chain is terminated with a next action of zero. If no type id is
3687 /// found the the frame is unwound and handling continues.
3688 /// 3. Type id table contains references to all the C++ typeinfo for all
3689 /// catches in the function. This tables is reversed indexed base 1.
3690
3691 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3692 static unsigned SharedTypeIds(const LandingPadInfo *L,
3693 const LandingPadInfo *R) {
3694 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3695 unsigned LSize = LIds.size(), RSize = RIds.size();
3696 unsigned MinSize = LSize < RSize ? LSize : RSize;
3697 unsigned Count = 0;
3698
3699 for (; Count != MinSize; ++Count)
3700 if (LIds[Count] != RIds[Count])
3701 return Count;
3702
3703 return Count;
3704 }
3705
3706 /// PadLT - Order landing pads lexicographically by type id.
3707 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
3708 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3709 unsigned LSize = LIds.size(), RSize = RIds.size();
3710 unsigned MinSize = LSize < RSize ? LSize : RSize;
3711
3712 for (unsigned i = 0; i != MinSize; ++i)
3713 if (LIds[i] != RIds[i])
3714 return LIds[i] < RIds[i];
3715
3716 return LSize < RSize;
3717 }
3718
3719 struct KeyInfo {
3720 static inline unsigned getEmptyKey() { return -1U; }
3721 static inline unsigned getTombstoneKey() { return -2U; }
3722 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner92eea072007-09-17 18:34:04 +00003723 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003724 static bool isPod() { return true; }
3725 };
3726
Duncan Sands241a0c92007-09-05 11:27:52 +00003727 /// ActionEntry - Structure describing an entry in the actions table.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003728 struct ActionEntry {
3729 int ValueForTypeID; // The value to write - may not be equal to the type id.
3730 int NextAction;
3731 struct ActionEntry *Previous;
3732 };
3733
Duncan Sands241a0c92007-09-05 11:27:52 +00003734 /// PadRange - Structure holding a try-range and the associated landing pad.
3735 struct PadRange {
3736 // The index of the landing pad.
3737 unsigned PadIndex;
3738 // The index of the begin and end labels in the landing pad's label lists.
3739 unsigned RangeIndex;
3740 };
3741
3742 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3743
3744 /// CallSiteEntry - Structure describing an entry in the call-site table.
3745 struct CallSiteEntry {
Duncan Sands4ff179f2007-12-19 07:36:31 +00003746 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003747 unsigned BeginLabel; // zero indicates the start of the function.
3748 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003749 // The landing pad starts at PadLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003750 unsigned PadLabel; // zero indicates that there is no landing pad.
3751 unsigned Action;
3752 };
3753
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003754 void EmitExceptionTable() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003755 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
3756 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
3757 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3758 if (PadInfos.empty()) return;
3759
3760 // Sort the landing pads in order of their type ids. This is used to fold
3761 // duplicate actions.
3762 SmallVector<const LandingPadInfo *, 64> LandingPads;
3763 LandingPads.reserve(PadInfos.size());
3764 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
3765 LandingPads.push_back(&PadInfos[i]);
3766 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3767
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003768 // Negative type ids index into FilterIds, positive type ids index into
3769 // TypeInfos. The value written for a positive type id is just the type
3770 // id itself. For a negative type id, however, the value written is the
3771 // (negative) byte offset of the corresponding FilterIds entry. The byte
3772 // offset is usually equal to the type id, because the FilterIds entries
3773 // are written using a variable width encoding which outputs one byte per
3774 // entry as long as the value written is not too large, but can differ.
3775 // This kind of complication does not occur for positive type ids because
3776 // type infos are output using a fixed width encoding.
3777 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3778 SmallVector<int, 16> FilterOffsets;
3779 FilterOffsets.reserve(FilterIds.size());
3780 int Offset = -1;
3781 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3782 E = FilterIds.end(); I != E; ++I) {
3783 FilterOffsets.push_back(Offset);
aslc200b112008-08-16 12:57:46 +00003784 Offset -= TargetAsmInfo::getULEB128Size(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003785 }
3786
Duncan Sands241a0c92007-09-05 11:27:52 +00003787 // Compute the actions table and gather the first action index for each
3788 // landing pad site.
3789 SmallVector<ActionEntry, 32> Actions;
3790 SmallVector<unsigned, 64> FirstActions;
3791 FirstActions.reserve(LandingPads.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003792
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003793 int FirstAction = 0;
Duncan Sands241a0c92007-09-05 11:27:52 +00003794 unsigned SizeActions = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003795 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3796 const LandingPadInfo *LP = LandingPads[i];
3797 const std::vector<int> &TypeIds = LP->TypeIds;
3798 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
3799 unsigned SizeSiteActions = 0;
3800
3801 if (NumShared < TypeIds.size()) {
3802 unsigned SizeAction = 0;
3803 ActionEntry *PrevAction = 0;
3804
3805 if (NumShared) {
3806 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3807 assert(Actions.size());
3808 PrevAction = &Actions.back();
aslc200b112008-08-16 12:57:46 +00003809 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3810 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003811 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
aslc200b112008-08-16 12:57:46 +00003812 SizeAction -=
3813 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003814 SizeAction += -PrevAction->NextAction;
3815 PrevAction = PrevAction->Previous;
3816 }
3817 }
3818
3819 // Compute the actions.
3820 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3821 int TypeID = TypeIds[I];
3822 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3823 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
aslc200b112008-08-16 12:57:46 +00003824 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003825
3826 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
aslc200b112008-08-16 12:57:46 +00003827 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003828 SizeSiteActions += SizeAction;
3829
3830 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
3831 Actions.push_back(Action);
3832
3833 PrevAction = &Actions.back();
3834 }
3835
3836 // Record the first action of the landing pad site.
3837 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3838 } // else identical - re-use previous FirstAction
3839
3840 FirstActions.push_back(FirstAction);
3841
3842 // Compute this sites contribution to size.
3843 SizeActions += SizeSiteActions;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003844 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003845
Duncan Sands4ff179f2007-12-19 07:36:31 +00003846 // Compute the call-site table. The entry for an invoke has a try-range
3847 // containing the call, a non-zero landing pad and an appropriate action.
3848 // The entry for an ordinary call has a try-range containing the call and
3849 // zero for the landing pad and the action. Calls marked 'nounwind' have
3850 // no entry and must not be contained in the try-range of any entry - they
3851 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands241a0c92007-09-05 11:27:52 +00003852 SmallVector<CallSiteEntry, 64> CallSites;
3853
3854 RangeMapType PadMap;
Duncan Sands4ff179f2007-12-19 07:36:31 +00003855 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3856 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3857 // try-ranges for them need be deduced.
Duncan Sands241a0c92007-09-05 11:27:52 +00003858 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3859 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003860 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003861 unsigned BeginLabel = LandingPad->BeginLabels[j];
3862 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3863 PadRange P = { i, j };
3864 PadMap[BeginLabel] = P;
3865 }
3866 }
3867
Duncan Sands4ff179f2007-12-19 07:36:31 +00003868 // The end label of the previous invoke or nounwind try-range.
Duncan Sands241a0c92007-09-05 11:27:52 +00003869 unsigned LastLabel = 0;
Duncan Sands4ff179f2007-12-19 07:36:31 +00003870
3871 // Whether there is a potentially throwing instruction (currently this means
3872 // an ordinary call) between the end of the previous try-range and now.
3873 bool SawPotentiallyThrowing = false;
3874
3875 // Whether the last callsite entry was for an invoke.
3876 bool PreviousIsInvoke = false;
3877
Duncan Sands4ff179f2007-12-19 07:36:31 +00003878 // Visit all instructions in order of address.
Duncan Sands241a0c92007-09-05 11:27:52 +00003879 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3880 I != E; ++I) {
3881 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3882 MI != E; ++MI) {
Dan Gohmanfa607c92008-07-01 00:05:16 +00003883 if (!MI->isLabel()) {
Chris Lattner5b930372008-01-07 07:27:27 +00003884 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands241a0c92007-09-05 11:27:52 +00003885 continue;
3886 }
3887
Chris Lattnerda4cff12007-12-30 20:50:28 +00003888 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands241a0c92007-09-05 11:27:52 +00003889 assert(BeginLabel && "Invalid label!");
Duncan Sands89372f62007-09-05 14:12:46 +00003890
Duncan Sands4ff179f2007-12-19 07:36:31 +00003891 // End of the previous try-range?
Duncan Sands89372f62007-09-05 14:12:46 +00003892 if (BeginLabel == LastLabel)
Duncan Sands4ff179f2007-12-19 07:36:31 +00003893 SawPotentiallyThrowing = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003894
Duncan Sands4ff179f2007-12-19 07:36:31 +00003895 // Beginning of a new try-range?
Duncan Sands241a0c92007-09-05 11:27:52 +00003896 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands241a0c92007-09-05 11:27:52 +00003897 if (L == PadMap.end())
Duncan Sands4ff179f2007-12-19 07:36:31 +00003898 // Nope, it was just some random label.
Duncan Sands241a0c92007-09-05 11:27:52 +00003899 continue;
3900
3901 PadRange P = L->second;
3902 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3903
3904 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3905 "Inconsistent landing pad map!");
3906
3907 // If some instruction between the previous try-range and this one may
3908 // throw, create a call-site entry with no landing pad for the region
3909 // between the try-ranges.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003910 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003911 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3912 CallSites.push_back(Site);
Duncan Sands4ff179f2007-12-19 07:36:31 +00003913 PreviousIsInvoke = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003914 }
3915
3916 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003917 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands241a0c92007-09-05 11:27:52 +00003918
Duncan Sands4ff179f2007-12-19 07:36:31 +00003919 if (LandingPad->LandingPadLabel) {
3920 // This try-range is for an invoke.
3921 CallSiteEntry Site = {BeginLabel, LastLabel,
3922 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands241a0c92007-09-05 11:27:52 +00003923
Duncan Sands4ff179f2007-12-19 07:36:31 +00003924 // Try to merge with the previous call-site.
3925 if (PreviousIsInvoke) {
Dan Gohman3d436002008-06-21 22:00:54 +00003926 CallSiteEntry &Prev = CallSites.back();
Duncan Sands4ff179f2007-12-19 07:36:31 +00003927 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3928 // Extend the range of the previous entry.
3929 Prev.EndLabel = Site.EndLabel;
3930 continue;
3931 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003932 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003933
Duncan Sands4ff179f2007-12-19 07:36:31 +00003934 // Otherwise, create a new call-site.
3935 CallSites.push_back(Site);
3936 PreviousIsInvoke = true;
3937 } else {
3938 // Create a gap.
3939 PreviousIsInvoke = false;
3940 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003941 }
3942 }
3943 // If some instruction between the previous try-range and the end of the
3944 // function may throw, create a call-site entry with no landing pad for the
3945 // region following the try-range.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003946 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003947 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3948 CallSites.push_back(Site);
3949 }
3950
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003951 // Final tallies.
Duncan Sands96144f92008-05-07 19:11:09 +00003952
3953 // Call sites.
3954 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
3955 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
3956 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
3957 unsigned SizeSites = CallSites.size() * (SiteStartSize +
3958 SiteLengthSize +
3959 LandingPadSize);
Duncan Sands241a0c92007-09-05 11:27:52 +00003960 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
aslc200b112008-08-16 12:57:46 +00003961 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands241a0c92007-09-05 11:27:52 +00003962
Duncan Sands96144f92008-05-07 19:11:09 +00003963 // Type infos.
3964 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3965 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003966
3967 unsigned TypeOffset = sizeof(int8_t) + // Call site format
aslc200b112008-08-16 12:57:46 +00003968 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003969 SizeSites + SizeActions + SizeTypes;
3970
3971 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3972 sizeof(int8_t) + // TType format
aslc200b112008-08-16 12:57:46 +00003973 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003974 TypeOffset;
3975
3976 unsigned SizeAlign = (4 - TotalSize) & 3;
3977
3978 // Begin the exception table.
3979 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng7e7d1942008-02-29 19:36:59 +00003980 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesen841e4982008-10-08 21:50:21 +00003981 O << "GCC_except_table" << SubprogramCount << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003982 for (unsigned i = 0; i != SizeAlign; ++i) {
3983 Asm->EmitInt8(0);
3984 Asm->EOL("Padding");
3985 }
3986 EmitLabel("exception", SubprogramCount);
3987
3988 // Emit the header.
3989 Asm->EmitInt8(DW_EH_PE_omit);
3990 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3991 Asm->EmitInt8(DW_EH_PE_absptr);
3992 Asm->EOL("TType format (DW_EH_PE_absptr)");
3993 Asm->EmitULEB128Bytes(TypeOffset);
3994 Asm->EOL("TType base offset");
3995 Asm->EmitInt8(DW_EH_PE_udata4);
3996 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3997 Asm->EmitULEB128Bytes(SizeSites);
3998 Asm->EOL("Call-site table length");
3999
Duncan Sands241a0c92007-09-05 11:27:52 +00004000 // Emit the landing pad site information.
4001 for (unsigned i = 0; i < CallSites.size(); ++i) {
4002 CallSiteEntry &S = CallSites[i];
4003 const char *BeginTag;
4004 unsigned BeginNumber;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004005
Duncan Sands241a0c92007-09-05 11:27:52 +00004006 if (!S.BeginLabel) {
4007 BeginTag = "eh_func_begin";
4008 BeginNumber = SubprogramCount;
4009 } else {
4010 BeginTag = "label";
4011 BeginNumber = S.BeginLabel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004012 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004013
Duncan Sands241a0c92007-09-05 11:27:52 +00004014 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00004015 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004016 Asm->EOL("Region start");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004017
Duncan Sands241a0c92007-09-05 11:27:52 +00004018 if (!S.EndLabel) {
Dale Johannesen4670be42008-01-15 23:24:56 +00004019 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands96144f92008-05-07 19:11:09 +00004020 true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004021 } else {
Duncan Sands96144f92008-05-07 19:11:09 +00004022 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004023 }
4024 Asm->EOL("Region length");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004025
Duncan Sands96144f92008-05-07 19:11:09 +00004026 if (!S.PadLabel)
4027 Asm->EmitInt32(0);
4028 else
Duncan Sands241a0c92007-09-05 11:27:52 +00004029 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00004030 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004031 Asm->EOL("Landing pad");
4032
4033 Asm->EmitULEB128Bytes(S.Action);
4034 Asm->EOL("Action");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004035 }
4036
4037 // Emit the actions.
4038 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4039 ActionEntry &Action = Actions[I];
4040
4041 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
4042 Asm->EOL("TypeInfo index");
4043 Asm->EmitSLEB128Bytes(Action.NextAction);
4044 Asm->EOL("Next action");
4045 }
4046
4047 // Emit the type ids.
4048 for (unsigned M = TypeInfos.size(); M; --M) {
4049 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00004050
4051 PrintRelDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004052
4053 if (GV)
4054 O << Asm->getGlobalLinkName(GV);
4055 else
4056 O << "0";
Duncan Sands241a0c92007-09-05 11:27:52 +00004057
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004058 Asm->EOL("TypeInfo");
4059 }
4060
4061 // Emit the filter typeids.
4062 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4063 unsigned TypeID = FilterIds[j];
4064 Asm->EmitULEB128Bytes(TypeID);
4065 Asm->EOL("Filter TypeInfo index");
4066 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004067
Evan Cheng7e7d1942008-02-29 19:36:59 +00004068 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004069 }
4070
4071public:
4072 //===--------------------------------------------------------------------===//
4073 // Main entry points.
4074 //
Owen Anderson847b99b2008-08-21 00:14:44 +00004075 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendlingd9308a62009-03-10 21:23:25 +00004076 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
4077 shouldEmitTableModule(false), shouldEmitMovesModule(false),
4078 ExceptionTimer(0) {
4079 if (TimePassesIsEnabled)
4080 ExceptionTimer = new Timer("Dwarf Exception Writer",
4081 *getDwarfTimerGroup());
4082 }
aslc200b112008-08-16 12:57:46 +00004083
Bill Wendlingd9308a62009-03-10 21:23:25 +00004084 virtual ~DwarfException() {
4085 delete ExceptionTimer;
4086 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004087
4088 /// SetModuleInfo - Set machine module information when it's known that pass
4089 /// manager has created it. Set by the target AsmPrinter.
4090 void SetModuleInfo(MachineModuleInfo *mmi) {
4091 MMI = mmi;
4092 }
4093
4094 /// BeginModule - Emit all exception information that should come prior to the
4095 /// content.
4096 void BeginModule(Module *M) {
4097 this->M = M;
4098 }
4099
4100 /// EndModule - Emit all exception information that should come after the
4101 /// content.
4102 void EndModule() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004103 if (TimePassesIsEnabled)
4104 ExceptionTimer->startTimer();
4105
Dale Johannesen85535762008-04-02 00:25:04 +00004106 if (shouldEmitMovesModule || shouldEmitTableModule) {
4107 const std::vector<Function *> Personalities = MMI->getPersonalities();
Evan Cheng3e288912009-02-25 07:04:34 +00004108 for (unsigned i = 0; i < Personalities.size(); ++i)
Dale Johannesen85535762008-04-02 00:25:04 +00004109 EmitCommonEHFrame(Personalities[i], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004110
Dale Johannesen85535762008-04-02 00:25:04 +00004111 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4112 E = EHFrames.end(); I != E; ++I)
4113 EmitEHFrame(*I);
4114 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00004115
4116 if (TimePassesIsEnabled)
4117 ExceptionTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004118 }
4119
aslc200b112008-08-16 12:57:46 +00004120 /// BeginFunction - Gather pre-function exception information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004121 /// emitted immediately after the function entry point.
4122 void BeginFunction(MachineFunction *MF) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004123 if (TimePassesIsEnabled)
4124 ExceptionTimer->startTimer();
4125
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004126 this->MF = MF;
Dale Johannesen85535762008-04-02 00:25:04 +00004127 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen85535762008-04-02 00:25:04 +00004128
Bill Wendlingd9308a62009-03-10 21:23:25 +00004129 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen85535762008-04-02 00:25:04 +00004130 // Map all labels and get rid of any dead landing pads.
4131 MMI->TidyLandingPads();
Bill Wendlingd9308a62009-03-10 21:23:25 +00004132
Dale Johannesen85535762008-04-02 00:25:04 +00004133 // If any landing pads survive, we need an EH table.
4134 if (MMI->getLandingPads().size())
4135 shouldEmitTable = true;
4136
4137 // See if we need frame move info.
Duncan Sandscbc28b12008-07-04 09:55:48 +00004138 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen85535762008-04-02 00:25:04 +00004139 shouldEmitMoves = true;
4140
4141 if (shouldEmitMoves || shouldEmitTable)
4142 // Assumes in correct section after the entry point.
4143 EmitLabel("eh_func_begin", ++SubprogramCount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004144 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00004145
Dale Johannesen85535762008-04-02 00:25:04 +00004146 shouldEmitTableModule |= shouldEmitTable;
4147 shouldEmitMovesModule |= shouldEmitMoves;
Bill Wendlingd9308a62009-03-10 21:23:25 +00004148
4149 if (TimePassesIsEnabled)
4150 ExceptionTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004151 }
4152
4153 /// EndFunction - Gather and emit post-function exception information.
4154 ///
4155 void EndFunction() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004156 if (TimePassesIsEnabled)
4157 ExceptionTimer->startTimer();
4158
Dale Johannesen85535762008-04-02 00:25:04 +00004159 if (shouldEmitMoves || shouldEmitTable) {
4160 EmitLabel("eh_func_end", SubprogramCount);
4161 EmitExceptionTable();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004162
Dale Johannesen85535762008-04-02 00:25:04 +00004163 // Save EH frame information
4164 EHFrames.
4165 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendlingd9308a62009-03-10 21:23:25 +00004166 SubprogramCount,
4167 MMI->getPersonalityIndex(),
4168 MF->getFrameInfo()->hasCalls(),
4169 !MMI->getLandingPads().empty(),
4170 MMI->getFrameMoves(),
4171 MF->getFunction()));
4172 }
4173
4174 if (TimePassesIsEnabled)
4175 ExceptionTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004176 }
4177};
4178
4179} // End of namespace llvm
4180
4181//===----------------------------------------------------------------------===//
4182
4183/// Emit - Print the abbreviation using the specified Dwarf writer.
4184///
4185void DIEAbbrev::Emit(const DwarfDebug &DD) const {
4186 // Emit its Dwarf tag type.
4187 DD.getAsm()->EmitULEB128Bytes(Tag);
4188 DD.getAsm()->EOL(TagString(Tag));
aslc200b112008-08-16 12:57:46 +00004189
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004190 // Emit whether it has children DIEs.
4191 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4192 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
aslc200b112008-08-16 12:57:46 +00004193
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004194 // For each attribute description.
4195 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4196 const DIEAbbrevData &AttrData = Data[i];
aslc200b112008-08-16 12:57:46 +00004197
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004198 // Emit attribute type.
4199 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4200 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
aslc200b112008-08-16 12:57:46 +00004201
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004202 // Emit form type.
4203 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4204 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
4205 }
4206
4207 // Mark end of abbreviation.
4208 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4209 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
4210}
4211
4212#ifndef NDEBUG
4213void DIEAbbrev::print(std::ostream &O) {
4214 O << "Abbreviation @"
4215 << std::hex << (intptr_t)this << std::dec
4216 << " "
4217 << TagString(Tag)
4218 << " "
4219 << ChildrenString(ChildrenFlag)
4220 << "\n";
aslc200b112008-08-16 12:57:46 +00004221
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004222 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4223 O << " "
4224 << AttributeString(Data[i].getAttribute())
4225 << " "
4226 << FormEncodingString(Data[i].getForm())
4227 << "\n";
4228 }
4229}
4230void DIEAbbrev::dump() { print(cerr); }
4231#endif
4232
4233//===----------------------------------------------------------------------===//
4234
4235#ifndef NDEBUG
4236void DIEValue::dump() {
4237 print(cerr);
4238}
4239#endif
4240
4241//===----------------------------------------------------------------------===//
4242
4243/// EmitValue - Emit integer of appropriate size.
4244///
4245void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
4246 switch (Form) {
4247 case DW_FORM_flag: // Fall thru
4248 case DW_FORM_ref1: // Fall thru
4249 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
4250 case DW_FORM_ref2: // Fall thru
4251 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
4252 case DW_FORM_ref4: // Fall thru
4253 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
4254 case DW_FORM_ref8: // Fall thru
4255 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4256 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4257 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
4258 default: assert(0 && "DIE Value form not supported yet"); break;
4259 }
4260}
4261
4262/// SizeOf - Determine size of integer value in bytes.
4263///
4264unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4265 switch (Form) {
4266 case DW_FORM_flag: // Fall thru
4267 case DW_FORM_ref1: // Fall thru
4268 case DW_FORM_data1: return sizeof(int8_t);
4269 case DW_FORM_ref2: // Fall thru
4270 case DW_FORM_data2: return sizeof(int16_t);
4271 case DW_FORM_ref4: // Fall thru
4272 case DW_FORM_data4: return sizeof(int32_t);
4273 case DW_FORM_ref8: // Fall thru
4274 case DW_FORM_data8: return sizeof(int64_t);
aslc200b112008-08-16 12:57:46 +00004275 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4276 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004277 default: assert(0 && "DIE Value form not supported yet"); break;
4278 }
4279 return 0;
4280}
4281
4282//===----------------------------------------------------------------------===//
4283
4284/// EmitValue - Emit string value.
4285///
4286void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
4287 DD.getAsm()->EmitString(String);
4288}
4289
4290//===----------------------------------------------------------------------===//
4291
4292/// EmitValue - Emit label value.
4293///
4294void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004295 bool IsSmall = Form == DW_FORM_data4;
4296 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004297}
4298
4299/// SizeOf - Determine size of label value in bytes.
4300///
4301unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004302 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004303 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004304}
4305
4306//===----------------------------------------------------------------------===//
4307
4308/// EmitValue - Emit label value.
4309///
4310void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004311 bool IsSmall = Form == DW_FORM_data4;
4312 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004313}
4314
4315/// SizeOf - Determine size of label value in bytes.
4316///
4317unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004318 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004319 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004320}
aslc200b112008-08-16 12:57:46 +00004321
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004322//===----------------------------------------------------------------------===//
4323
4324/// EmitValue - Emit delta value.
4325///
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004326void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4327 bool IsSmall = Form == DW_FORM_data4;
4328 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4329 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4330}
4331
4332/// SizeOf - Determine size of delta value in bytes.
4333///
4334unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4335 if (Form == DW_FORM_data4) return 4;
4336 return DD.getTargetData()->getPointerSize();
4337}
aslc200b112008-08-16 12:57:46 +00004338
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004339//===----------------------------------------------------------------------===//
4340
4341/// EmitValue - Emit delta value.
4342///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004343void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
4344 bool IsSmall = Form == DW_FORM_data4;
4345 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
4346}
4347
4348/// SizeOf - Determine size of delta value in bytes.
4349///
4350unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4351 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004352 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004353}
4354
4355//===----------------------------------------------------------------------===//
4356
4357/// EmitValue - Emit debug information entry offset.
4358///
4359void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
4360 DD.getAsm()->EmitInt32(Entry->getOffset());
4361}
aslc200b112008-08-16 12:57:46 +00004362
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004363//===----------------------------------------------------------------------===//
4364
4365/// ComputeSize - calculate the size of the block.
4366///
4367unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
4368 if (!Size) {
Owen Anderson88dd6232008-06-24 21:44:59 +00004369 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004370
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004371 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4372 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
4373 }
4374 }
4375 return Size;
4376}
4377
4378/// EmitValue - Emit block data.
4379///
4380void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
4381 switch (Form) {
4382 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4383 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4384 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4385 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
4386 default: assert(0 && "Improper form for block"); break;
4387 }
aslc200b112008-08-16 12:57:46 +00004388
Owen Anderson88dd6232008-06-24 21:44:59 +00004389 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004390
4391 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4392 DD.getAsm()->EOL();
4393 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
4394 }
4395}
4396
4397/// SizeOf - Determine size of block data in bytes.
4398///
4399unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4400 switch (Form) {
4401 case DW_FORM_block1: return Size + sizeof(int8_t);
4402 case DW_FORM_block2: return Size + sizeof(int16_t);
4403 case DW_FORM_block4: return Size + sizeof(int32_t);
aslc200b112008-08-16 12:57:46 +00004404 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004405 default: assert(0 && "Improper form for block"); break;
4406 }
4407 return 0;
4408}
4409
4410//===----------------------------------------------------------------------===//
4411/// DIE Implementation
4412
4413DIE::~DIE() {
4414 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4415 delete Children[i];
4416}
aslc200b112008-08-16 12:57:46 +00004417
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004418/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4419///
4420void DIE::AddSiblingOffset() {
4421 DIEInteger *DI = new DIEInteger(0);
4422 Values.insert(Values.begin(), DI);
4423 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
4424}
4425
4426/// Profile - Used to gather unique data for the value folding set.
4427///
4428void DIE::Profile(FoldingSetNodeID &ID) {
4429 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00004430
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004431 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4432 ID.AddPointer(Children[i]);
4433
4434 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4435 ID.AddPointer(Values[j]);
4436}
4437
4438#ifndef NDEBUG
4439void DIE::print(std::ostream &O, unsigned IncIndent) {
4440 static unsigned IndentCount = 0;
4441 IndentCount += IncIndent;
4442 const std::string Indent(IndentCount, ' ');
4443 bool isBlock = Abbrev.getTag() == 0;
aslc200b112008-08-16 12:57:46 +00004444
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004445 if (!isBlock) {
4446 O << Indent
4447 << "Die: "
4448 << "0x" << std::hex << (intptr_t)this << std::dec
4449 << ", Offset: " << Offset
4450 << ", Size: " << Size
aslc200b112008-08-16 12:57:46 +00004451 << "\n";
4452
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004453 O << Indent
4454 << TagString(Abbrev.getTag())
4455 << " "
4456 << ChildrenString(Abbrev.getChildrenFlag());
4457 } else {
4458 O << "Size: " << Size;
4459 }
4460 O << "\n";
4461
Owen Anderson88dd6232008-06-24 21:44:59 +00004462 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004463
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004464 IndentCount += 2;
4465 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4466 O << Indent;
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004467
4468 if (!isBlock)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004469 O << AttributeString(Data[i].getAttribute());
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004470 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004471 O << "Blk[" << i << "]";
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004472
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004473 O << " "
4474 << FormEncodingString(Data[i].getForm())
4475 << " ";
4476 Values[i]->print(O);
4477 O << "\n";
4478 }
4479 IndentCount -= 2;
4480
4481 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4482 Children[j]->print(O, 4);
4483 }
aslc200b112008-08-16 12:57:46 +00004484
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004485 if (!isBlock) O << "\n";
4486 IndentCount -= IncIndent;
4487}
4488
4489void DIE::dump() {
4490 print(cerr);
4491}
4492#endif
4493
4494//===----------------------------------------------------------------------===//
4495/// DwarfWriter Implementation
4496///
4497
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004498DwarfWriter::DwarfWriter()
Bill Wendlingd9308a62009-03-10 21:23:25 +00004499 : ImmutablePass(&ID), DD(0), DE(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004500
4501DwarfWriter::~DwarfWriter() {
4502 delete DE;
4503 delete DD;
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004504 delete DwarfTimerGroup; DwarfTimerGroup = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004505}
4506
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004507/// BeginModule - Emit all Dwarf sections that should come prior to the
4508/// content.
Devang Patelaa1e8432009-01-08 23:40:34 +00004509void DwarfWriter::BeginModule(Module *M,
4510 MachineModuleInfo *MMI,
4511 raw_ostream &OS, AsmPrinter *A,
4512 const TargetAsmInfo *T) {
4513 DE = new DwarfException(OS, A, T);
4514 DD = new DwarfDebug(OS, A, T);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004515 DE->BeginModule(M);
4516 DD->BeginModule(M);
Devang Patel6ccd57e2009-01-13 00:20:51 +00004517 DD->SetDebugInfo(MMI);
Devang Patelaa1e8432009-01-08 23:40:34 +00004518 DE->SetModuleInfo(MMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004519}
4520
4521/// EndModule - Emit all Dwarf sections that should come after the content.
4522///
4523void DwarfWriter::EndModule() {
4524 DE->EndModule();
4525 DD->EndModule();
4526}
4527
aslc200b112008-08-16 12:57:46 +00004528/// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004529/// emitted immediately after the function entry point.
4530void DwarfWriter::BeginFunction(MachineFunction *MF) {
4531 DE->BeginFunction(MF);
4532 DD->BeginFunction(MF);
4533}
4534
4535/// EndFunction - Gather and emit post-function debug information.
4536///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00004537void DwarfWriter::EndFunction(MachineFunction *MF) {
4538 DD->EndFunction(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004539 DE->EndFunction();
aslc200b112008-08-16 12:57:46 +00004540
Bill Wendling5b4796a2008-07-22 00:53:37 +00004541 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004542 // Clear function debug information.
4543 MMI->EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004544}
Devang Patelcb59fd42009-01-12 19:17:34 +00004545
Devang Patel2da0cc42009-01-15 23:41:32 +00004546/// ValidDebugInfo - Return true if V represents valid debug info value.
4547bool DwarfWriter::ValidDebugInfo(Value *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004548 return DD && DD->ValidDebugInfo(V);
Devang Patel2da0cc42009-01-15 23:41:32 +00004549}
4550
Devang Patelcb59fd42009-01-12 19:17:34 +00004551/// RecordSourceLine - Records location information and associates it with a
4552/// label. Returns a unique label ID used to generate a label and provide
4553/// correspondence to the source line list.
4554unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
4555 unsigned Src) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004556 return DD->RecordSourceLine(Line, Col, Src);
Devang Patelcb59fd42009-01-12 19:17:34 +00004557}
4558
Evan Cheng3e288912009-02-25 07:04:34 +00004559/// getOrCreateSourceID - Look up the source id with the given directory and
4560/// source file names. If none currently exists, create a new id and insert it
4561/// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
4562/// as well.
4563unsigned DwarfWriter::getOrCreateSourceID(const std::string &DirName,
4564 const std::string &FileName) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004565 return DD->getOrCreateSourceID(DirName, FileName);
Devang Patelcb59fd42009-01-12 19:17:34 +00004566}
4567
4568/// RecordRegionStart - Indicate the start of a region.
4569unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004570 return DD->RecordRegionStart(V);
Devang Patelcb59fd42009-01-12 19:17:34 +00004571}
4572
4573/// RecordRegionEnd - Indicate the end of a region.
4574unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004575 return DD->RecordRegionEnd(V);
Devang Patelcb59fd42009-01-12 19:17:34 +00004576}
4577
4578/// getRecordSourceLineCount - Count source lines.
4579unsigned DwarfWriter::getRecordSourceLineCount() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004580 return DD->getRecordSourceLineCount();
Devang Patelcb59fd42009-01-12 19:17:34 +00004581}
Devang Patel70190872009-01-13 21:25:00 +00004582
Devang Patelfe359e72009-01-13 21:44:10 +00004583/// RecordVariable - Indicate the declaration of a local variable.
4584///
4585void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
4586 DD->RecordVariable(GV, FrameIndex);
4587}
Devang Patel42f6bed2009-01-13 23:54:55 +00004588
Bill Wendling50db0792009-02-20 00:44:43 +00004589/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
4590/// be emitted.
4591bool DwarfWriter::ShouldEmitDwarfDebug() const {
Bill Wendlingd9308a62009-03-10 21:23:25 +00004592 return DD->ShouldEmitDwarfDebug();
Bill Wendling50db0792009-02-20 00:44:43 +00004593}