blob: ec4f465ed7141944fe99702d9e82cb2264a423f1 [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
1289 // DbgScopeMap - Tracks the scopes in the current function.
1290 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
1291
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001292 struct FunctionDebugFrameInfo {
1293 unsigned Number;
1294 std::vector<MachineMove> Moves;
1295
1296 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman9ba5d4d2007-08-27 14:50:10 +00001297 Number(Num), Moves(M) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001298 };
1299
1300 std::vector<FunctionDebugFrameInfo> DebugFrames;
aslc200b112008-08-16 12:57:46 +00001301
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001302public:
aslc200b112008-08-16 12:57:46 +00001303
Bill Wendling50db0792009-02-20 00:44:43 +00001304 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
1305 /// be emitted.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001306 ///
Bill Wendling50db0792009-02-20 00:44:43 +00001307 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001308
1309 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
aslc200b112008-08-16 12:57:46 +00001310 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001311 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1312 // Profile the node so that we can make it unique.
1313 FoldingSetNodeID ID;
1314 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00001315
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001316 // Check the set for priors.
1317 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
aslc200b112008-08-16 12:57:46 +00001318
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001319 // If it's newly added.
1320 if (InSet == &Abbrev) {
aslc200b112008-08-16 12:57:46 +00001321 // Add to abbreviation list.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001322 Abbreviations.push_back(&Abbrev);
1323 // Assign the vector position + 1 as its number.
1324 Abbrev.setNumber(Abbreviations.size());
1325 } else {
1326 // Assign existing abbreviation number.
1327 Abbrev.setNumber(InSet->getNumber());
1328 }
1329 }
1330
1331 /// NewString - Add a string to the constant pool and returns a label.
1332 ///
1333 DWLabel NewString(const std::string &String) {
1334 unsigned StringID = StringPool.insert(String);
1335 return DWLabel("string", StringID);
1336 }
aslc200b112008-08-16 12:57:46 +00001337
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001338 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1339 /// entry.
1340 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1341 DIEntry *Value;
aslc200b112008-08-16 12:57:46 +00001342
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001343 if (Entry) {
1344 FoldingSetNodeID ID;
1345 DIEntry::Profile(ID, Entry);
1346 void *Where;
1347 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
aslc200b112008-08-16 12:57:46 +00001348
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001349 if (Value) return Value;
aslc200b112008-08-16 12:57:46 +00001350
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001351 Value = new DIEntry(Entry);
1352 ValuesSet.InsertNode(Value, Where);
1353 } else {
1354 Value = new DIEntry(Entry);
1355 }
aslc200b112008-08-16 12:57:46 +00001356
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001357 Values.push_back(Value);
1358 return Value;
1359 }
aslc200b112008-08-16 12:57:46 +00001360
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001361 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1362 ///
1363 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1364 Value->Entry = Entry;
1365 // Add to values set if not already there. If it is, we merely have a
1366 // duplicate in the values list (no harm.)
1367 ValuesSet.GetOrInsertNode(Value);
1368 }
1369
1370 /// AddUInt - Add an unsigned integer attribute data and value.
1371 ///
1372 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1373 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1374
1375 FoldingSetNodeID ID;
1376 DIEInteger::Profile(ID, Integer);
1377 void *Where;
1378 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1379 if (!Value) {
1380 Value = new DIEInteger(Integer);
1381 ValuesSet.InsertNode(Value, Where);
1382 Values.push_back(Value);
1383 }
aslc200b112008-08-16 12:57:46 +00001384
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001385 Die->AddValue(Attribute, Form, Value);
1386 }
aslc200b112008-08-16 12:57:46 +00001387
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001388 /// AddSInt - Add an signed integer attribute data and value.
1389 ///
1390 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1391 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1392
1393 FoldingSetNodeID ID;
1394 DIEInteger::Profile(ID, (uint64_t)Integer);
1395 void *Where;
1396 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1397 if (!Value) {
1398 Value = new DIEInteger(Integer);
1399 ValuesSet.InsertNode(Value, Where);
1400 Values.push_back(Value);
1401 }
aslc200b112008-08-16 12:57:46 +00001402
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001403 Die->AddValue(Attribute, Form, Value);
1404 }
aslc200b112008-08-16 12:57:46 +00001405
Evan Cheng3e288912009-02-25 07:04:34 +00001406 /// AddString - Add a string attribute data and value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001407 ///
1408 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1409 const std::string &String) {
1410 FoldingSetNodeID ID;
1411 DIEString::Profile(ID, String);
1412 void *Where;
1413 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1414 if (!Value) {
1415 Value = new DIEString(String);
1416 ValuesSet.InsertNode(Value, Where);
1417 Values.push_back(Value);
1418 }
aslc200b112008-08-16 12:57:46 +00001419
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001420 Die->AddValue(Attribute, Form, Value);
1421 }
aslc200b112008-08-16 12:57:46 +00001422
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001423 /// AddLabel - Add a Dwarf label attribute data and value.
1424 ///
1425 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1426 const DWLabel &Label) {
1427 FoldingSetNodeID ID;
1428 DIEDwarfLabel::Profile(ID, Label);
1429 void *Where;
1430 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1431 if (!Value) {
1432 Value = new DIEDwarfLabel(Label);
1433 ValuesSet.InsertNode(Value, Where);
1434 Values.push_back(Value);
1435 }
aslc200b112008-08-16 12:57:46 +00001436
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001437 Die->AddValue(Attribute, Form, Value);
1438 }
aslc200b112008-08-16 12:57:46 +00001439
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001440 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1441 ///
1442 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1443 const std::string &Label) {
1444 FoldingSetNodeID ID;
1445 DIEObjectLabel::Profile(ID, Label);
1446 void *Where;
1447 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1448 if (!Value) {
1449 Value = new DIEObjectLabel(Label);
1450 ValuesSet.InsertNode(Value, Where);
1451 Values.push_back(Value);
1452 }
aslc200b112008-08-16 12:57:46 +00001453
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001454 Die->AddValue(Attribute, Form, Value);
1455 }
aslc200b112008-08-16 12:57:46 +00001456
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001457 /// AddSectionOffset - Add a section offset label attribute data and value.
1458 ///
1459 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1460 const DWLabel &Label, const DWLabel &Section,
1461 bool isEH = false, bool useSet = true) {
1462 FoldingSetNodeID ID;
1463 DIESectionOffset::Profile(ID, Label, Section);
1464 void *Where;
1465 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1466 if (!Value) {
1467 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1468 ValuesSet.InsertNode(Value, Where);
1469 Values.push_back(Value);
1470 }
aslc200b112008-08-16 12:57:46 +00001471
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001472 Die->AddValue(Attribute, Form, Value);
1473 }
aslc200b112008-08-16 12:57:46 +00001474
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001475 /// AddDelta - Add a label delta attribute data and value.
1476 ///
1477 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1478 const DWLabel &Hi, const DWLabel &Lo) {
1479 FoldingSetNodeID ID;
1480 DIEDelta::Profile(ID, Hi, Lo);
1481 void *Where;
1482 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1483 if (!Value) {
1484 Value = new DIEDelta(Hi, Lo);
1485 ValuesSet.InsertNode(Value, Where);
1486 Values.push_back(Value);
1487 }
aslc200b112008-08-16 12:57:46 +00001488
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001489 Die->AddValue(Attribute, Form, Value);
1490 }
aslc200b112008-08-16 12:57:46 +00001491
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001492 /// AddDIEntry - Add a DIE attribute data and value.
1493 ///
1494 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1495 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1496 }
1497
1498 /// AddBlock - Add block data.
1499 ///
1500 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1501 Block->ComputeSize(*this);
1502 FoldingSetNodeID ID;
1503 Block->Profile(ID);
1504 void *Where;
1505 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1506 if (!Value) {
1507 Value = Block;
1508 ValuesSet.InsertNode(Value, Where);
1509 Values.push_back(Value);
1510 } else {
Chris Lattner3de66892007-09-21 18:25:53 +00001511 // Already exists, reuse the previous one.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001512 delete Block;
Chris Lattner3de66892007-09-21 18:25:53 +00001513 Block = cast<DIEBlock>(Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001514 }
aslc200b112008-08-16 12:57:46 +00001515
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001516 Die->AddValue(Attribute, Block->BestForm(), Value);
1517 }
1518
1519private:
1520
1521 /// AddSourceLine - Add location information to specified debug information
1522 /// entry.
Devang Patel7c8a2772009-01-16 19:28:14 +00001523 void AddSourceLine(DIE *Die, const DIVariable *V) {
Devang Patel4d1709e2009-01-08 02:33:41 +00001524 unsigned FileID = 0;
1525 unsigned Line = V->getLineNumber();
Devang Patel2ae1db52009-01-30 18:20:31 +00001526 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1527 FileID = Unit->getID();
Devang Pateld94b6932009-02-10 06:04:08 +00001528 assert (FileID && "Invalid file id");
Devang Patel4d1709e2009-01-08 02:33:41 +00001529 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1530 AddUInt(Die, DW_AT_decl_line, 0, Line);
1531 }
1532
1533 /// AddSourceLine - Add location information to specified debug information
1534 /// entry.
Devang Patel7c8a2772009-01-16 19:28:14 +00001535 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Devang Patel5f244e32009-01-05 22:35:52 +00001536 unsigned FileID = 0;
1537 unsigned Line = G->getLineNumber();
Devang Patel2ae1db52009-01-30 18:20:31 +00001538 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1539 FileID = Unit->getID();
Devang Pateld94b6932009-02-10 06:04:08 +00001540 assert (FileID && "Invalid file id");
Devang Patel5f244e32009-01-05 22:35:52 +00001541 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1542 AddUInt(Die, DW_AT_decl_line, 0, Line);
1543 }
1544
Devang Patel7c8a2772009-01-16 19:28:14 +00001545 void AddSourceLine(DIE *Die, const DIType *Ty) {
Devang Patel5f244e32009-01-05 22:35:52 +00001546 unsigned FileID = 0;
Devang Patel7c8a2772009-01-16 19:28:14 +00001547 unsigned Line = Ty->getLineNumber();
Devang Patel2ae1db52009-01-30 18:20:31 +00001548 DICompileUnit CU = Ty->getCompileUnit();
1549 if (CU.isNull())
1550 return;
1551 CompileUnit *Unit = FindCompileUnit(CU);
1552 FileID = Unit->getID();
Devang Pateld94b6932009-02-10 06:04:08 +00001553 assert (FileID && "Invalid file id");
Devang Patel5f244e32009-01-05 22:35:52 +00001554 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1555 AddUInt(Die, DW_AT_decl_line, 0, Line);
1556 }
1557
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001558 /// AddAddress - Add an address attribute to a die based on the location
1559 /// provided.
1560 void AddAddress(DIE *Die, unsigned Attribute,
1561 const MachineLocation &Location) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001562 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001563 DIEBlock *Block = new DIEBlock();
aslc200b112008-08-16 12:57:46 +00001564
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001565 if (Location.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001566 if (Reg < 32) {
1567 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1568 } else {
1569 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1570 AddUInt(Block, 0, DW_FORM_udata, Reg);
1571 }
1572 } else {
1573 if (Reg < 32) {
1574 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1575 } else {
1576 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1577 AddUInt(Block, 0, DW_FORM_udata, Reg);
1578 }
1579 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1580 }
aslc200b112008-08-16 12:57:46 +00001581
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001582 AddBlock(Die, Attribute, 0, Block);
1583 }
aslc200b112008-08-16 12:57:46 +00001584
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001585 /// AddType - Add a new type attribute to the specified entity.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001586 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel165ed512009-01-23 19:13:31 +00001587 if (Ty.isNull())
Devang Patel4a4cbe72009-01-05 21:47:57 +00001588 return;
Devang Patel4a4cbe72009-01-05 21:47:57 +00001589
1590 // Check for pre-existence.
1591 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1592 // If it exists then use the existing value.
1593 if (Slot) {
1594 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1595 return;
1596 }
1597
1598 // Set up proxy.
1599 Slot = NewDIEntry();
1600
1601 // Construct type.
1602 DIE Buffer(DW_TAG_base_type);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001603 if (Ty.isBasicType(Ty.getTag()))
1604 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1605 else if (Ty.isDerivedType(Ty.getTag()))
1606 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1607 else {
Bill Wendling824a8bf2009-02-03 21:17:20 +00001608 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelef4bf3b2009-01-15 19:26:23 +00001609 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1610 }
1611
Devang Patelb0cb07c2009-01-27 23:22:55 +00001612 // Add debug information entry to entity and appropriate context.
1613 DIE *Die = NULL;
1614 DIDescriptor Context = Ty.getContext();
1615 if (!Context.isNull())
1616 Die = DW_Unit->getDieMapSlotFor(Context.getGV());
1617
1618 if (Die) {
1619 DIE *Child = new DIE(Buffer);
1620 Die->AddChild(Child);
1621 Buffer.Detach();
1622 SetDIEntry(Slot, Child);
Bill Wendling824a8bf2009-02-03 21:17:20 +00001623 } else {
Devang Patelb0cb07c2009-01-27 23:22:55 +00001624 Die = DW_Unit->AddDie(Buffer);
1625 SetDIEntry(Slot, Die);
1626 }
1627
Devang Patel4a4cbe72009-01-05 21:47:57 +00001628 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1629 }
1630
Devang Patel46d13752009-01-05 19:07:53 +00001631 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1632 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001633 DIBasicType BTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001634
1635 // Get core information.
Bill Wendling1c5842b2009-03-09 05:04:40 +00001636 std::string Name;
1637 BTy.getName(Name);
Devang Patelfc187162009-01-05 17:57:47 +00001638 Buffer.setTag(DW_TAG_base_type);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001639 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patelfc187162009-01-05 17:57:47 +00001640 // Add name if not anonymous or intermediate type.
1641 if (!Name.empty())
1642 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001643 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelfc187162009-01-05 17:57:47 +00001644 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1645 }
1646
Devang Patel46d13752009-01-05 19:07:53 +00001647 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1648 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001649 DIDerivedType DTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001650
1651 // Get core information.
Bill Wendling1c5842b2009-03-09 05:04:40 +00001652 std::string Name;
1653 DTy.getName(Name);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001654 uint64_t Size = DTy.getSizeInBits() >> 3;
1655 unsigned Tag = DTy.getTag();
Bill Wendling1c5842b2009-03-09 05:04:40 +00001656
Devang Patelfc187162009-01-05 17:57:47 +00001657 // FIXME - Workaround for templates.
1658 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1659
1660 Buffer.setTag(Tag);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001661
Devang Patelfc187162009-01-05 17:57:47 +00001662 // Map to main type, void will not have a type.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001663 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel4a4cbe72009-01-05 21:47:57 +00001664 AddType(DW_Unit, &Buffer, FromTy);
Devang Patelfc187162009-01-05 17:57:47 +00001665
1666 // Add name if not anonymous or intermediate type.
Evan Cheng3e288912009-02-25 07:04:34 +00001667 if (!Name.empty())
1668 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelfc187162009-01-05 17:57:47 +00001669
1670 // Add size if non-zero (derived types might be zero-sized.)
1671 if (Size)
1672 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1673
1674 // Add source line info if available and TyDesc is not a forward
1675 // declaration.
Devang Patele34e0882009-01-27 00:45:04 +00001676 if (!DTy.isForwardDecl())
1677 AddSourceLine(&Buffer, &DTy);
Devang Patelfc187162009-01-05 17:57:47 +00001678 }
1679
Devang Patel30c01372009-01-05 19:55:51 +00001680 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1681 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001682 DICompositeType CTy) {
Devang Patelb28de842009-01-17 08:01:33 +00001683 // Get core information.
Bill Wendling1c5842b2009-03-09 05:04:40 +00001684 std::string Name;
1685 CTy.getName(Name);
1686
Devang Patelef4bf3b2009-01-15 19:26:23 +00001687 uint64_t Size = CTy.getSizeInBits() >> 3;
1688 unsigned Tag = CTy.getTag();
Devang Patel8050bd72009-01-23 01:19:09 +00001689 Buffer.setTag(Tag);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001690
Devang Patel30c01372009-01-05 19:55:51 +00001691 switch (Tag) {
1692 case DW_TAG_vector_type:
1693 case DW_TAG_array_type:
Devang Patelef4bf3b2009-01-15 19:26:23 +00001694 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patel30c01372009-01-05 19:55:51 +00001695 break;
Devang Patel3798f492009-01-20 18:35:14 +00001696 case DW_TAG_enumeration_type:
1697 {
1698 DIArray Elements = CTy.getTypeArray();
1699 // Add enumerators to enumeration type.
1700 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1701 DIE *ElemDie = NULL;
1702 DIEnumerator Enum(Elements.getElement(i).getGV());
1703 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
1704 Buffer.AddChild(ElemDie);
1705 }
1706 }
1707 break;
Devang Patel30c01372009-01-05 19:55:51 +00001708 case DW_TAG_subroutine_type:
1709 {
1710 // Add prototype flag.
1711 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001712 DIArray Elements = CTy.getTypeArray();
Devang Patel30c01372009-01-05 19:55:51 +00001713 // Add return type.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001714 DIDescriptor RTy = Elements.getElement(0);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001715 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel4a4cbe72009-01-05 21:47:57 +00001716
Devang Patel30c01372009-01-05 19:55:51 +00001717 // Add arguments.
1718 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1719 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel4a4cbe72009-01-05 21:47:57 +00001720 DIDescriptor Ty = Elements.getElement(i);
Devang Pateld40a7e52009-01-17 06:57:25 +00001721 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patel30c01372009-01-05 19:55:51 +00001722 Buffer.AddChild(Arg);
1723 }
1724 }
1725 break;
1726 case DW_TAG_structure_type:
1727 case DW_TAG_union_type:
1728 {
1729 // Add elements to structure type.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001730 DIArray Elements = CTy.getTypeArray();
Devang Patelcf7acb12009-01-16 00:50:53 +00001731
1732 // A forward struct declared type may not have elements available.
1733 if (Elements.isNull())
1734 break;
1735
Devang Patel30c01372009-01-05 19:55:51 +00001736 // Add elements to structure type.
1737 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1738 DIDescriptor Element = Elements.getElement(i);
Devang Patelb28de842009-01-17 08:01:33 +00001739 DIE *ElemDie = NULL;
Devang Patelef4bf3b2009-01-15 19:26:23 +00001740 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel245446c2009-01-17 08:05:14 +00001741 ElemDie = CreateSubprogramDIE(DW_Unit,
1742 DISubprogram(Element.getGV()));
Devang Patelb28de842009-01-17 08:01:33 +00001743 else if (Element.getTag() == dwarf::DW_TAG_variable) // ???
1744 ElemDie = CreateGlobalVariableDIE(DW_Unit,
1745 DIGlobalVariable(Element.getGV()));
Devang Patel5c643892009-01-20 21:02:02 +00001746 else
1747 ElemDie = CreateMemberDIE(DW_Unit,
1748 DIDerivedType(Element.getGV()));
Devang Patel245446c2009-01-17 08:05:14 +00001749 Buffer.AddChild(ElemDie);
Devang Patel30c01372009-01-05 19:55:51 +00001750 }
Devang Patel74193d72009-02-17 22:43:44 +00001751 unsigned RLang = CTy.getRunTimeLang();
1752 if (RLang)
1753 AddUInt(&Buffer, DW_AT_APPLE_runtime_class, DW_FORM_data1, RLang);
Devang Patel30c01372009-01-05 19:55:51 +00001754 }
1755 break;
1756 default:
1757 break;
1758 }
1759
1760 // Add name if not anonymous or intermediate type.
Evan Cheng3e288912009-02-25 07:04:34 +00001761 if (!Name.empty())
1762 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel30c01372009-01-05 19:55:51 +00001763
Devang Patele34e0882009-01-27 00:45:04 +00001764 if (Tag == DW_TAG_enumeration_type || Tag == DW_TAG_structure_type
1765 || Tag == DW_TAG_union_type) {
1766 // Add size if non-zero (derived types might be zero-sized.)
1767 if (Size)
1768 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1769 else {
1770 // Add zero size if it is not a forward declaration.
1771 if (CTy.isForwardDecl())
1772 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1773 else
1774 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1775 }
1776
1777 // Add source line info if available.
1778 if (!CTy.isForwardDecl())
1779 AddSourceLine(&Buffer, &CTy);
Devang Patel30c01372009-01-05 19:55:51 +00001780 }
Devang Patel30c01372009-01-05 19:55:51 +00001781 }
1782
Bill Wendling824a8bf2009-02-03 21:17:20 +00001783 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1784 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelef4bf3b2009-01-15 19:26:23 +00001785 int64_t L = SR.getLo();
1786 int64_t H = SR.getHi();
Devang Patel6fb54132009-01-05 18:33:01 +00001787 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1788 if (L != H) {
1789 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1790 if (L)
Devang Patel245446c2009-01-17 08:05:14 +00001791 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1792 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel6fb54132009-01-05 18:33:01 +00001793 }
1794 Buffer.AddChild(DW_Subrange);
1795 }
1796
1797 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1798 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1799 DICompositeType *CTy) {
1800 Buffer.setTag(DW_TAG_array_type);
1801 if (CTy->getTag() == DW_TAG_vector_type)
1802 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1803
Devang Patel6ab30e52009-01-28 21:08:20 +00001804 // Emit derived type.
1805 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel6fb54132009-01-05 18:33:01 +00001806 DIArray Elements = CTy->getTypeArray();
Devang Patel6fb54132009-01-05 18:33:01 +00001807
1808 // Construct an anonymous type for index type.
1809 DIE IdxBuffer(DW_TAG_base_type);
1810 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1811 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1812 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1813
1814 // Add subranges to array type.
1815 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel30c01372009-01-05 19:55:51 +00001816 DIDescriptor Element = Elements.getElement(i);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001817 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1818 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel6fb54132009-01-05 18:33:01 +00001819 }
1820 }
1821
Bill Wendling824a8bf2009-02-03 21:17:20 +00001822 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3798f492009-01-20 18:35:14 +00001823 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patela566e812009-01-05 18:38:38 +00001824
1825 DIE *Enumerator = new DIE(DW_TAG_enumerator);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001826 std::string Name;
1827 ETy->getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001828 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
Devang Patela566e812009-01-05 18:38:38 +00001829 int64_t Value = ETy->getEnumValue();
1830 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Patel3798f492009-01-20 18:35:14 +00001831 return Enumerator;
Devang Patela566e812009-01-05 18:38:38 +00001832 }
Devang Patel6fb54132009-01-05 18:33:01 +00001833
Devang Patelb28de842009-01-17 08:01:33 +00001834 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendling824a8bf2009-02-03 21:17:20 +00001835 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patelb28de842009-01-17 08:01:33 +00001836 {
1837 DIE *GVDie = new DIE(DW_TAG_variable);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001838 std::string Name;
1839 GV.getDisplayName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001840 AddString(GVDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001841 std::string LinkageName;
1842 GV.getLinkageName(LinkageName);
Devang Patel526b01d2009-01-05 18:59:44 +00001843 if (!LinkageName.empty())
Devang Patelb28de842009-01-17 08:01:33 +00001844 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1845 AddType(DW_Unit, GVDie, GV.getType());
1846 if (!GV.isLocalToUnit())
1847 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
1848 AddSourceLine(GVDie, &GV);
1849 return GVDie;
Devang Patel526b01d2009-01-05 18:59:44 +00001850 }
1851
Devang Patel5c643892009-01-20 21:02:02 +00001852 /// CreateMemberDIE - Create new member DIE.
1853 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
1854 DIE *MemberDie = new DIE(DT.getTag());
Bill Wendling1c5842b2009-03-09 05:04:40 +00001855 std::string Name;
1856 DT.getName(Name);
Devang Patel5c643892009-01-20 21:02:02 +00001857 if (!Name.empty())
1858 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1859
1860 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1861
1862 AddSourceLine(MemberDie, &DT);
1863
Devang Patelf1f30d42009-02-17 21:23:59 +00001864 uint64_t Size = DT.getSizeInBits();
1865 uint64_t FieldSize = DT.getOriginalTypeSize();
1866
1867 if (Size != FieldSize) {
1868 // Handle bitfield.
1869 AddUInt(MemberDie, DW_AT_byte_size, 0, DT.getOriginalTypeSize() >> 3);
1870 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1871
1872 uint64_t Offset = DT.getOffsetInBits();
1873 uint64_t FieldOffset = Offset;
1874 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1875 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1876 FieldOffset = (HiMark - FieldSize);
1877 Offset -= FieldOffset;
1878 // Maybe we need to work from the other end.
1879 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1880 AddUInt(MemberDie, DW_AT_bit_offset, 0, Offset);
1881 }
Devang Patel5c643892009-01-20 21:02:02 +00001882 DIEBlock *Block = new DIEBlock();
1883 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1884 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1885 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1886
Devang Patel2e7ee192009-01-21 00:08:04 +00001887 if (DT.isProtected())
1888 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1889 else if (DT.isPrivate())
1890 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1891
Devang Patel5c643892009-01-20 21:02:02 +00001892 return MemberDie;
1893 }
1894
Devang Patelb28de842009-01-17 08:01:33 +00001895 /// CreateSubprogramDIE - Create new DIE using SP.
1896 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Devang Patel245446c2009-01-17 08:05:14 +00001897 const DISubprogram &SP,
1898 bool IsConstructor = false) {
Devang Patelb28de842009-01-17 08:01:33 +00001899 DIE *SPDie = new DIE(DW_TAG_subprogram);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001900 std::string Name;
1901 SP.getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001902 AddString(SPDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001903 std::string LinkageName;
1904 SP.getLinkageName(LinkageName);
Devang Patel526b01d2009-01-05 18:59:44 +00001905 if (!LinkageName.empty())
Devang Patelb28de842009-01-17 08:01:33 +00001906 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Devang Patel245446c2009-01-17 08:05:14 +00001907 LinkageName);
Devang Patelb28de842009-01-17 08:01:33 +00001908 AddSourceLine(SPDie, &SP);
Devang Patel526b01d2009-01-05 18:59:44 +00001909
Devang Patelb28de842009-01-17 08:01:33 +00001910 DICompositeType SPTy = SP.getType();
1911 DIArray Args = SPTy.getTypeArray();
1912
Devang Patel526b01d2009-01-05 18:59:44 +00001913 // Add Return Type.
Devang Patel688a19f2009-02-27 18:05:21 +00001914 if (!IsConstructor) {
1915 if (Args.isNull())
1916 AddType(DW_Unit, SPDie, SPTy);
1917 else
1918 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
1919 }
Devang Patel922d1592009-01-30 01:21:46 +00001920
Devang Patelace2cf62009-02-02 17:51:41 +00001921 if (!SP.isDefinition()) {
1922 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
1923 // Add arguments.
1924 // Do not add arguments for subprogram definition. They will be
1925 // handled through RecordVariable.
1926 if (!Args.isNull())
1927 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1928 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1929 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
1930 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
1931 SPDie->AddChild(Arg);
1932 }
1933 }
Devang Patel922d1592009-01-30 01:21:46 +00001934
Devang Patele075e072009-02-24 00:52:19 +00001935 unsigned Lang = SP.getCompileUnit().getLanguage();
1936 if (Lang == DW_LANG_C99 || Lang == DW_LANG_C89
1937 || Lang == DW_LANG_ObjC)
1938 AddUInt(SPDie, DW_AT_prototyped, DW_FORM_flag, 1);
1939
Devang Patelef4bf3b2009-01-15 19:26:23 +00001940 if (!SP.isLocalToUnit())
Devang Patel922d1592009-01-30 01:21:46 +00001941 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Devang Patelb28de842009-01-17 08:01:33 +00001942 return SPDie;
Devang Patel526b01d2009-01-05 18:59:44 +00001943 }
1944
Devang Patelb28de842009-01-17 08:01:33 +00001945 /// FindCompileUnit - Get the compile unit for the given descriptor.
1946 ///
Devang Patel5f244e32009-01-05 22:35:52 +00001947 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
Evan Cheng3e288912009-02-25 07:04:34 +00001948 CompileUnit *DW_Unit = CompileUnitMap[Unit.getGV()];
Devang Patel5f244e32009-01-05 22:35:52 +00001949 assert(DW_Unit && "Missing compile unit.");
1950 return DW_Unit;
1951 }
1952
Devang Patel42f6bed2009-01-13 23:54:55 +00001953 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel4d1709e2009-01-08 02:33:41 +00001954 ///
1955 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1956 // Get the descriptor.
Devang Patel7c8a2772009-01-16 19:28:14 +00001957 const DIVariable &VD = DV->getVariable();
Devang Patel4d1709e2009-01-08 02:33:41 +00001958
1959 // Translate tag to proper Dwarf tag. The result variable is dropped for
1960 // now.
1961 unsigned Tag;
Devang Patel7c8a2772009-01-16 19:28:14 +00001962 switch (VD.getTag()) {
Devang Patel4d1709e2009-01-08 02:33:41 +00001963 case DW_TAG_return_variable: return NULL;
1964 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1965 case DW_TAG_auto_variable: // fall thru
1966 default: Tag = DW_TAG_variable; break;
1967 }
1968
1969 // Define variable debug information entry.
1970 DIE *VariableDie = new DIE(Tag);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001971 std::string Name;
1972 VD.getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001973 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Devang Patel4d1709e2009-01-08 02:33:41 +00001974
1975 // Add source line info if available.
Devang Patel7c8a2772009-01-16 19:28:14 +00001976 AddSourceLine(VariableDie, &VD);
Devang Patel4d1709e2009-01-08 02:33:41 +00001977
1978 // Add variable type.
Devang Patel7c8a2772009-01-16 19:28:14 +00001979 AddType(Unit, VariableDie, VD.getType());
Devang Patel4d1709e2009-01-08 02:33:41 +00001980
1981 // Add variable address.
1982 MachineLocation Location;
1983 Location.set(RI->getFrameRegister(*MF),
1984 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
1985 AddAddress(VariableDie, DW_AT_location, Location);
1986
1987 return VariableDie;
1988 }
1989
Devang Patel4d1709e2009-01-08 02:33:41 +00001990 /// getOrCreateScope - Returns the scope associated with the given descriptor.
1991 ///
1992 DbgScope *getOrCreateScope(GlobalVariable *V) {
1993 DbgScope *&Slot = DbgScopeMap[V];
Bill Wendlinge0f3a262009-02-20 20:40:28 +00001994 if (Slot) return Slot;
1995
1996 // FIXME - breaks down when the context is an inlined function.
1997 DIDescriptor ParentDesc;
1998 DIDescriptor Desc(V);
1999
2000 if (Desc.getTag() == dwarf::DW_TAG_lexical_block) {
2001 DIBlock Block(V);
2002 ParentDesc = Block.getContext();
Devang Patel4d1709e2009-01-08 02:33:41 +00002003 }
Bill Wendlinge0f3a262009-02-20 20:40:28 +00002004
2005 DbgScope *Parent = ParentDesc.isNull() ?
2006 NULL : getOrCreateScope(ParentDesc.getGV());
2007 Slot = new DbgScope(Parent, Desc);
2008
2009 if (Parent) {
2010 Parent->AddScope(Slot);
2011 } else if (RootDbgScope) {
2012 // FIXME - Add inlined function scopes to the root so we can delete them
2013 // later. Long term, handle inlined functions properly.
2014 RootDbgScope->AddScope(Slot);
2015 } else {
2016 // First function is top level function.
2017 RootDbgScope = Slot;
2018 }
2019
Devang Patel4d1709e2009-01-08 02:33:41 +00002020 return Slot;
2021 }
2022
2023 /// ConstructDbgScope - Construct the components of a scope.
2024 ///
2025 void ConstructDbgScope(DbgScope *ParentScope,
2026 unsigned ParentStartID, unsigned ParentEndID,
2027 DIE *ParentDie, CompileUnit *Unit) {
2028 // Add variables to scope.
Devang Patel63c22f42009-01-10 02:42:49 +00002029 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
Devang Patel4d1709e2009-01-08 02:33:41 +00002030 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2031 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2032 if (VariableDie) ParentDie->AddChild(VariableDie);
2033 }
2034
2035 // Add nested scopes.
Devang Patel63c22f42009-01-10 02:42:49 +00002036 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel4d1709e2009-01-08 02:33:41 +00002037 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2038 // Define the Scope debug information entry.
2039 DbgScope *Scope = Scopes[j];
2040 // FIXME - Ignore inlined functions for the time being.
2041 if (!Scope->getParent()) continue;
2042
Devang Patelb9224922009-01-12 18:41:00 +00002043 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2044 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel4d1709e2009-01-08 02:33:41 +00002045
2046 // Ignore empty scopes.
2047 if (StartID == EndID && StartID != 0) continue;
2048 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
2049
2050 if (StartID == ParentStartID && EndID == ParentEndID) {
2051 // Just add stuff to the parent scope.
2052 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2053 } else {
2054 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2055
2056 // Add the scope bounds.
2057 if (StartID) {
2058 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2059 DWLabel("label", StartID));
2060 } else {
2061 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2062 DWLabel("func_begin", SubprogramCount));
2063 }
2064 if (EndID) {
2065 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2066 DWLabel("label", EndID));
2067 } else {
2068 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2069 DWLabel("func_end", SubprogramCount));
2070 }
2071
2072 // Add the scope contents.
2073 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2074 ParentDie->AddChild(ScopeDie);
2075 }
2076 }
2077 }
2078
2079 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2080 ///
2081 void ConstructRootDbgScope(DbgScope *RootScope) {
2082 // Exit if there is no root scope.
2083 if (!RootScope) return;
Devang Patel2560d922009-01-15 18:25:17 +00002084 DIDescriptor Desc = RootScope->getDesc();
2085 if (Desc.isNull())
2086 return;
Devang Patel4d1709e2009-01-08 02:33:41 +00002087
2088 // Get the subprogram debug information entry.
Devang Patel2560d922009-01-15 18:25:17 +00002089 DISubprogram SPD(Desc.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002090
2091 // Get the compile unit context.
Devang Patel2ae1db52009-01-30 18:20:31 +00002092 CompileUnit *Unit = MainCU;
2093 if (!Unit)
2094 Unit = FindCompileUnit(SPD.getCompileUnit());
Devang Patel4d1709e2009-01-08 02:33:41 +00002095
2096 // Get the subprogram die.
Devang Patel57ec9ac2009-01-12 22:58:14 +00002097 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002098 assert(SPDie && "Missing subprogram descriptor");
2099
2100 // Add the function bounds.
2101 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2102 DWLabel("func_begin", SubprogramCount));
2103 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2104 DWLabel("func_end", SubprogramCount));
2105 MachineLocation Location(RI->getFrameRegister(*MF));
2106 AddAddress(SPDie, DW_AT_frame_base, Location);
2107
2108 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2109 }
2110
2111 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2112 ///
2113 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Cheng3e288912009-02-25 07:04:34 +00002114 const char *FnName = MF->getFunction()->getNameStart();
2115 if (MainCU) {
2116 std::map<std::string, DIE*> &Globals = MainCU->getGlobals();
2117 std::map<std::string, DIE*>::iterator GI = Globals.find(FnName);
2118 if (GI != Globals.end()) {
2119 DIE *SPDie = GI->second;
Devang Patel4d1709e2009-01-08 02:33:41 +00002120
2121 // Add the function bounds.
2122 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2123 DWLabel("func_begin", SubprogramCount));
2124 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2125 DWLabel("func_end", SubprogramCount));
2126
2127 MachineLocation Location(RI->getFrameRegister(*MF));
2128 AddAddress(SPDie, DW_AT_frame_base, Location);
2129 return;
2130 }
Evan Cheng3e288912009-02-25 07:04:34 +00002131 } else {
2132 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2133 CompileUnit *Unit = CompileUnits[i];
2134 std::map<std::string, DIE*> &Globals = Unit->getGlobals();
2135 std::map<std::string, DIE*>::iterator GI = Globals.find(FnName);
2136 if (GI != Globals.end()) {
2137 DIE *SPDie = GI->second;
2138
2139 // Add the function bounds.
2140 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2141 DWLabel("func_begin", SubprogramCount));
2142 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2143 DWLabel("func_end", SubprogramCount));
2144
2145 MachineLocation Location(RI->getFrameRegister(*MF));
2146 AddAddress(SPDie, DW_AT_frame_base, Location);
2147 return;
2148 }
2149 }
Devang Patel4d1709e2009-01-08 02:33:41 +00002150 }
Evan Cheng3e288912009-02-25 07:04:34 +00002151
Devang Patel4d1709e2009-01-08 02:33:41 +00002152#if 0
2153 // FIXME: This is causing an abort because C++ mangled names are compared
2154 // with their unmangled counterparts. See PR2885. Don't do this assert.
2155 assert(0 && "Couldn't find DIE for machine function!");
2156#endif
Evan Cheng3e288912009-02-25 07:04:34 +00002157 return;
Devang Patel4d1709e2009-01-08 02:33:41 +00002158 }
2159
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002160 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2161 /// tools to recognize the object file contains Dwarf information.
2162 void EmitInitial() {
2163 // Check to see if we already emitted intial headers.
2164 if (didInitial) return;
2165 didInitial = true;
aslc200b112008-08-16 12:57:46 +00002166
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002167 // Dwarf sections base addresses.
2168 if (TAI->doesDwarfRequireFrameSection()) {
2169 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2170 EmitLabel("section_debug_frame", 0);
2171 }
2172 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2173 EmitLabel("section_info", 0);
2174 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2175 EmitLabel("section_abbrev", 0);
2176 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2177 EmitLabel("section_aranges", 0);
Scott Michel79f01f52009-01-26 22:32:51 +00002178 if (TAI->doesSupportMacInfoSection()) {
2179 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2180 EmitLabel("section_macinfo", 0);
2181 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002182 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2183 EmitLabel("section_line", 0);
2184 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2185 EmitLabel("section_loc", 0);
2186 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2187 EmitLabel("section_pubnames", 0);
2188 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2189 EmitLabel("section_str", 0);
2190 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2191 EmitLabel("section_ranges", 0);
2192
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002193 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002194 EmitLabel("text_begin", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00002195 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002196 EmitLabel("data_begin", 0);
2197 }
2198
2199 /// EmitDIE - Recusively Emits a debug information entry.
2200 ///
2201 void EmitDIE(DIE *Die) {
2202 // Get the abbreviation for this DIE.
2203 unsigned AbbrevNumber = Die->getAbbrevNumber();
2204 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
aslc200b112008-08-16 12:57:46 +00002205
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002206 Asm->EOL();
2207
2208 // Emit the code (index) for the abbreviation.
2209 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng0eeed442008-07-01 23:18:29 +00002210
2211 if (VerboseAsm)
2212 Asm->EOL(std::string("Abbrev [" +
2213 utostr(AbbrevNumber) +
2214 "] 0x" + utohexstr(Die->getOffset()) +
2215 ":0x" + utohexstr(Die->getSize()) + " " +
2216 TagString(Abbrev->getTag())));
2217 else
2218 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00002219
Owen Anderson88dd6232008-06-24 21:44:59 +00002220 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2221 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
aslc200b112008-08-16 12:57:46 +00002222
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002223 // Emit the DIE attribute values.
2224 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2225 unsigned Attr = AbbrevData[i].getAttribute();
2226 unsigned Form = AbbrevData[i].getForm();
2227 assert(Form && "Too many attributes for DIE (check abbreviation)");
aslc200b112008-08-16 12:57:46 +00002228
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002229 switch (Attr) {
2230 case DW_AT_sibling: {
2231 Asm->EmitInt32(Die->SiblingOffset());
2232 break;
2233 }
2234 default: {
2235 // Emit an attribute using the defined form.
2236 Values[i]->EmitValue(*this, Form);
2237 break;
2238 }
2239 }
aslc200b112008-08-16 12:57:46 +00002240
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002241 Asm->EOL(AttributeString(Attr));
2242 }
aslc200b112008-08-16 12:57:46 +00002243
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002244 // Emit the DIE children if any.
2245 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2246 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002247
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002248 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2249 EmitDIE(Children[j]);
2250 }
aslc200b112008-08-16 12:57:46 +00002251
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002252 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2253 }
2254 }
2255
2256 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2257 ///
2258 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2259 // Get the children.
2260 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002261
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002262 // If not last sibling and has children then add sibling offset attribute.
2263 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2264
2265 // Record the abbreviation.
2266 AssignAbbrevNumber(Die->getAbbrev());
aslc200b112008-08-16 12:57:46 +00002267
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002268 // Get the abbreviation for this DIE.
2269 unsigned AbbrevNumber = Die->getAbbrevNumber();
2270 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2271
2272 // Set DIE offset
2273 Die->setOffset(Offset);
aslc200b112008-08-16 12:57:46 +00002274
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002275 // Start the size with the size of abbreviation code.
aslc200b112008-08-16 12:57:46 +00002276 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2277
Owen Anderson88dd6232008-06-24 21:44:59 +00002278 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2279 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002280
2281 // Size the DIE attribute values.
2282 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2283 // Size attribute value.
2284 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2285 }
aslc200b112008-08-16 12:57:46 +00002286
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002287 // Size the DIE children if any.
2288 if (!Children.empty()) {
2289 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2290 "Children flag not set");
aslc200b112008-08-16 12:57:46 +00002291
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002292 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2293 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2294 }
aslc200b112008-08-16 12:57:46 +00002295
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002296 // End of children marker.
2297 Offset += sizeof(int8_t);
2298 }
2299
2300 Die->setSize(Offset - Die->getOffset());
2301 return Offset;
2302 }
2303
2304 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2305 ///
2306 void SizeAndOffsets() {
2307 // Process base compile unit.
Devang Patel2ae1db52009-01-30 18:20:31 +00002308 if (MainCU) {
2309 // Compute size of compile unit header
2310 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2311 sizeof(int16_t) + // DWARF version number
2312 sizeof(int32_t) + // Offset Into Abbrev. Section
2313 sizeof(int8_t); // Pointer Size (in bytes)
2314 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
2315 return;
2316 }
Evan Cheng3e288912009-02-25 07:04:34 +00002317 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2318 CompileUnit *Unit = CompileUnits[i];
Devang Patel6eae2832009-01-12 23:05:55 +00002319 // Compute size of compile unit header
2320 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2321 sizeof(int16_t) + // DWARF version number
2322 sizeof(int32_t) + // Offset Into Abbrev. Section
2323 sizeof(int8_t); // Pointer Size (in bytes)
2324 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2325 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002326 }
2327
Evan Cheng3e288912009-02-25 07:04:34 +00002328 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002329 ///
Evan Cheng3e288912009-02-25 07:04:34 +00002330 void EmitDebugInfoPerCU(CompileUnit *Unit) {
2331 DIE *Die = Unit->getDie();
2332 // Emit the compile units header.
2333 EmitLabel("info_begin", Unit->getID());
2334 // Emit size of content not including length itself
2335 unsigned ContentSize = Die->getSize() +
2336 sizeof(int16_t) + // DWARF version number
2337 sizeof(int32_t) + // Offset Into Abbrev. Section
2338 sizeof(int8_t) + // Pointer Size (in bytes)
2339 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2340
2341 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2342 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2343 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2344 Asm->EOL("Offset Into Abbrev. Section");
2345 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2346
2347 EmitDIE(Die);
2348 // FIXME - extra padding for gdb bug.
2349 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2350 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2351 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2352 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2353 EmitLabel("info_end", Unit->getID());
2354
2355 Asm->EOL();
2356 }
2357
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002358 void EmitDebugInfo() {
2359 // Start debug info section.
2360 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
aslc200b112008-08-16 12:57:46 +00002361
Evan Cheng3e288912009-02-25 07:04:34 +00002362 if (MainCU) {
2363 EmitDebugInfoPerCU(MainCU);
2364 return;
Devang Patel6eae2832009-01-12 23:05:55 +00002365 }
Evan Cheng3e288912009-02-25 07:04:34 +00002366
2367 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2368 EmitDebugInfoPerCU(CompileUnits[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002369 }
2370
2371 /// EmitAbbreviations - Emit the abbreviation section.
2372 ///
2373 void EmitAbbreviations() const {
2374 // Check to see if it is worth the effort.
2375 if (!Abbreviations.empty()) {
2376 // Start the debug abbrev section.
2377 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
aslc200b112008-08-16 12:57:46 +00002378
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002379 EmitLabel("abbrev_begin", 0);
aslc200b112008-08-16 12:57:46 +00002380
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002381 // For each abbrevation.
2382 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2383 // Get abbreviation data
2384 const DIEAbbrev *Abbrev = Abbreviations[i];
aslc200b112008-08-16 12:57:46 +00002385
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002386 // Emit the abbrevations code (base 1 index.)
2387 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2388 Asm->EOL("Abbreviation Code");
aslc200b112008-08-16 12:57:46 +00002389
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002390 // Emit the abbreviations data.
2391 Abbrev->Emit(*this);
aslc200b112008-08-16 12:57:46 +00002392
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002393 Asm->EOL();
2394 }
aslc200b112008-08-16 12:57:46 +00002395
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002396 // Mark end of abbreviations.
2397 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2398
2399 EmitLabel("abbrev_end", 0);
aslc200b112008-08-16 12:57:46 +00002400
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002401 Asm->EOL();
2402 }
2403 }
2404
Bill Wendling1983a2a2008-07-20 00:11:19 +00002405 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2406 /// the line matrix.
aslc200b112008-08-16 12:57:46 +00002407 ///
Bill Wendling1983a2a2008-07-20 00:11:19 +00002408 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2409 // Define last address of section.
2410 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2411 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2412 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2413 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2414
2415 // Mark end of matrix.
2416 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2417 Asm->EmitULEB128Bytes(1); Asm->EOL();
2418 Asm->EmitInt8(1); Asm->EOL();
2419 }
2420
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002421 /// EmitDebugLines - Emit source line information.
2422 ///
2423 void EmitDebugLines() {
Bill Wendling1983a2a2008-07-20 00:11:19 +00002424 // If the target is using .loc/.file, the assembler will be emitting the
2425 // .debug_line table automatically.
2426 if (TAI->hasDotLocAndDotFile())
Dan Gohmanc55b34a2007-09-24 21:43:52 +00002427 return;
2428
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002429 // Minimum line delta, thus ranging from -10..(255-10).
2430 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2431 // Maximum line delta, thus ranging from -10..(255-10).
2432 const int MaxLineDelta = 255 + MinLineDelta;
2433
2434 // Start the dwarf line section.
2435 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
aslc200b112008-08-16 12:57:46 +00002436
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002437 // Construct the section header.
aslc200b112008-08-16 12:57:46 +00002438
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002439 EmitDifference("line_end", 0, "line_begin", 0, true);
2440 Asm->EOL("Length of Source Line Info");
2441 EmitLabel("line_begin", 0);
aslc200b112008-08-16 12:57:46 +00002442
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002443 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
aslc200b112008-08-16 12:57:46 +00002444
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002445 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2446 Asm->EOL("Prolog Length");
2447 EmitLabel("line_prolog_begin", 0);
aslc200b112008-08-16 12:57:46 +00002448
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002449 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2450
2451 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2452
2453 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
aslc200b112008-08-16 12:57:46 +00002454
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002455 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2456
2457 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
aslc200b112008-08-16 12:57:46 +00002458
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002459 // Line number standard opcode encodings argument count
2460 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2461 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2462 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2463 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2464 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2465 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2466 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2467 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2468 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2469
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002470 // Emit directories.
Evan Cheng3e288912009-02-25 07:04:34 +00002471 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2472 Asm->EmitString(getSourceDirectoryName(DI));
2473 Asm->EOL("Directory");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002474 }
2475 Asm->EmitInt8(0); Asm->EOL("End of directories");
aslc200b112008-08-16 12:57:46 +00002476
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002477 // Emit files.
Evan Cheng3e288912009-02-25 07:04:34 +00002478 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2479 // Remember source id starts at 1.
2480 std::pair<unsigned, unsigned> Id = getSourceDirsectoryAndFileIds(SI);
2481 Asm->EmitString(getSourceFileName(Id.second));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002482 Asm->EOL("Source");
Evan Cheng3e288912009-02-25 07:04:34 +00002483 Asm->EmitULEB128Bytes(Id.first);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002484 Asm->EOL("Directory #");
2485 Asm->EmitULEB128Bytes(0);
2486 Asm->EOL("Mod date");
2487 Asm->EmitULEB128Bytes(0);
2488 Asm->EOL("File size");
2489 }
2490 Asm->EmitInt8(0); Asm->EOL("End of files");
aslc200b112008-08-16 12:57:46 +00002491
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002492 EmitLabel("line_prolog_end", 0);
aslc200b112008-08-16 12:57:46 +00002493
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002494 // A sequence for each text section.
Bill Wendling1983a2a2008-07-20 00:11:19 +00002495 unsigned SecSrcLinesSize = SectionSourceLines.size();
2496
2497 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002498 // Isolate current sections line info.
Devang Patel35a078f2009-01-12 22:54:42 +00002499 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng0eeed442008-07-01 23:18:29 +00002500
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002501 if (VerboseAsm) {
2502 const Section* S = SectionMap[j + 1];
Evan Cheng3e288912009-02-25 07:04:34 +00002503 O << '\t' << TAI->getCommentString() << " Section"
2504 << S->getName() << '\n';
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002505 } else
Evan Cheng0eeed442008-07-01 23:18:29 +00002506 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002507
2508 // Dwarf assumes we start with first line of first source file.
2509 unsigned Source = 1;
2510 unsigned Line = 1;
aslc200b112008-08-16 12:57:46 +00002511
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002512 // Construct rows of the address, source, line, column matrix.
2513 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patel35a078f2009-01-12 22:54:42 +00002514 const SrcLineInfo &LineInfo = LineInfos[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002515 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2516 if (!LabelID) continue;
aslc200b112008-08-16 12:57:46 +00002517
Evan Cheng3e288912009-02-25 07:04:34 +00002518 if (!VerboseAsm)
Evan Cheng0eeed442008-07-01 23:18:29 +00002519 Asm->EOL();
Evan Cheng3e288912009-02-25 07:04:34 +00002520 else {
2521 std::pair<unsigned, unsigned> SourceID =
2522 getSourceDirsectoryAndFileIds(LineInfo.getSourceID());
2523 O << '\t' << TAI->getCommentString() << ' '
2524 << getSourceDirectoryName(SourceID.first) << ' '
2525 << getSourceFileName(SourceID.second)
2526 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2527 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002528
2529 // Define the line address.
2530 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohmancfb72b22007-09-27 23:12:31 +00002531 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002532 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2533 EmitReference("label", LabelID); Asm->EOL("Location label");
aslc200b112008-08-16 12:57:46 +00002534
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002535 // If change of source, then switch to the new source.
2536 if (Source != LineInfo.getSourceID()) {
2537 Source = LineInfo.getSourceID();
2538 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2539 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2540 }
aslc200b112008-08-16 12:57:46 +00002541
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002542 // If change of line.
2543 if (Line != LineInfo.getLine()) {
2544 // Determine offset.
2545 int Offset = LineInfo.getLine() - Line;
2546 int Delta = Offset - MinLineDelta;
aslc200b112008-08-16 12:57:46 +00002547
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002548 // Update line.
2549 Line = LineInfo.getLine();
aslc200b112008-08-16 12:57:46 +00002550
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002551 // If delta is small enough and in range...
2552 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2553 // ... then use fast opcode.
2554 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2555 } else {
2556 // ... otherwise use long hand.
2557 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2558 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2559 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2560 }
2561 } else {
2562 // Copy the previous row (different address or source)
2563 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2564 }
2565 }
2566
Bill Wendling1983a2a2008-07-20 00:11:19 +00002567 EmitEndOfLineMatrix(j + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002568 }
Bill Wendling1983a2a2008-07-20 00:11:19 +00002569
2570 if (SecSrcLinesSize == 0)
2571 // Because we're emitting a debug_line section, we still need a line
2572 // table. The linker and friends expect it to exist. If there's nothing to
2573 // put into it, emit an empty table.
2574 EmitEndOfLineMatrix(1);
aslc200b112008-08-16 12:57:46 +00002575
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002576 EmitLabel("line_end", 0);
aslc200b112008-08-16 12:57:46 +00002577
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002578 Asm->EOL();
2579 }
aslc200b112008-08-16 12:57:46 +00002580
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002581 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2582 ///
2583 void EmitCommonDebugFrame() {
2584 if (!TAI->doesDwarfRequireFrameSection())
2585 return;
2586
2587 int stackGrowth =
2588 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2589 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00002590 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002591
2592 // Start the dwarf frame section.
2593 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2594
2595 EmitLabel("debug_frame_common", 0);
2596 EmitDifference("debug_frame_common_end", 0,
2597 "debug_frame_common_begin", 0, true);
2598 Asm->EOL("Length of Common Information Entry");
2599
2600 EmitLabel("debug_frame_common_begin", 0);
2601 Asm->EmitInt32((int)DW_CIE_ID);
2602 Asm->EOL("CIE Identifier Tag");
2603 Asm->EmitInt8(DW_CIE_VERSION);
2604 Asm->EOL("CIE Version");
2605 Asm->EmitString("");
2606 Asm->EOL("CIE Augmentation");
2607 Asm->EmitULEB128Bytes(1);
2608 Asm->EOL("CIE Code Alignment Factor");
2609 Asm->EmitSLEB128Bytes(stackGrowth);
aslc200b112008-08-16 12:57:46 +00002610 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00002611 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002612 Asm->EOL("CIE RA Column");
aslc200b112008-08-16 12:57:46 +00002613
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002614 std::vector<MachineMove> Moves;
2615 RI->getInitialFrameState(Moves);
2616
Dale Johannesenf5a11532007-11-13 19:13:01 +00002617 EmitFrameMoves(NULL, 0, Moves, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002618
Evan Cheng7e7d1942008-02-29 19:36:59 +00002619 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002620 EmitLabel("debug_frame_common_end", 0);
aslc200b112008-08-16 12:57:46 +00002621
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002622 Asm->EOL();
2623 }
2624
2625 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2626 /// section.
2627 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
2628 if (!TAI->doesDwarfRequireFrameSection())
2629 return;
aslc200b112008-08-16 12:57:46 +00002630
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002631 // Start the dwarf frame section.
2632 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
aslc200b112008-08-16 12:57:46 +00002633
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002634 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2635 "debug_frame_begin", DebugFrameInfo.Number, true);
2636 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00002637
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002638 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2639
2640 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2641 0, 0, true, false);
2642 Asm->EOL("FDE CIE offset");
2643
2644 EmitReference("func_begin", DebugFrameInfo.Number);
2645 Asm->EOL("FDE initial location");
2646 EmitDifference("func_end", DebugFrameInfo.Number,
2647 "func_begin", DebugFrameInfo.Number);
2648 Asm->EOL("FDE address range");
aslc200b112008-08-16 12:57:46 +00002649
Devang Patelb28de842009-01-17 08:01:33 +00002650 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00002651 false);
aslc200b112008-08-16 12:57:46 +00002652
Evan Cheng7e7d1942008-02-29 19:36:59 +00002653 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002654 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2655
2656 Asm->EOL();
2657 }
2658
Evan Cheng3e288912009-02-25 07:04:34 +00002659 void EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2660 EmitDifference("pubnames_end", Unit->getID(),
2661 "pubnames_begin", Unit->getID(), true);
2662 Asm->EOL("Length of Public Names Info");
2663
2664 EmitLabel("pubnames_begin", Unit->getID());
2665
2666 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2667
2668 EmitSectionOffset("info_begin", "section_info",
2669 Unit->getID(), 0, true, false);
2670 Asm->EOL("Offset of Compilation Unit Info");
2671
2672 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2673 true);
2674 Asm->EOL("Compilation Unit Length");
2675
2676 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2677 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2678 GE = Globals.end(); GI != GE; ++GI) {
2679 const std::string &Name = GI->first;
2680 DIE * Entity = GI->second;
2681
2682 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2683 Asm->EmitString(Name); Asm->EOL("External Name");
2684 }
2685
2686 Asm->EmitInt32(0); Asm->EOL("End Mark");
2687 EmitLabel("pubnames_end", Unit->getID());
2688
2689 Asm->EOL();
2690 }
2691
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002692 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2693 ///
2694 void EmitDebugPubNames() {
2695 // Start the dwarf pubnames section.
2696 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
aslc200b112008-08-16 12:57:46 +00002697
Evan Cheng3e288912009-02-25 07:04:34 +00002698 if (MainCU) {
2699 EmitDebugPubNamesPerCU(MainCU);
2700 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002701 }
Evan Cheng3e288912009-02-25 07:04:34 +00002702
2703 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2704 EmitDebugPubNamesPerCU(CompileUnits[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002705 }
2706
2707 /// EmitDebugStr - Emit visible names into a debug str section.
2708 ///
2709 void EmitDebugStr() {
2710 // Check to see if it is worth the effort.
2711 if (!StringPool.empty()) {
2712 // Start the dwarf str section.
2713 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
aslc200b112008-08-16 12:57:46 +00002714
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002715 // For each of strings in the string pool.
2716 for (unsigned StringID = 1, N = StringPool.size();
2717 StringID <= N; ++StringID) {
2718 // Emit a label for reference from debug information entries.
2719 EmitLabel("string", StringID);
2720 // Emit the string itself.
2721 const std::string &String = StringPool[StringID];
2722 Asm->EmitString(String); Asm->EOL();
2723 }
aslc200b112008-08-16 12:57:46 +00002724
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002725 Asm->EOL();
2726 }
2727 }
2728
2729 /// EmitDebugLoc - Emit visible names into a debug loc section.
2730 ///
2731 void EmitDebugLoc() {
2732 // Start the dwarf loc section.
2733 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
aslc200b112008-08-16 12:57:46 +00002734
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002735 Asm->EOL();
2736 }
2737
2738 /// EmitDebugARanges - Emit visible names into a debug aranges section.
2739 ///
2740 void EmitDebugARanges() {
2741 // Start the dwarf aranges section.
2742 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
aslc200b112008-08-16 12:57:46 +00002743
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002744 // FIXME - Mock up
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002745#if 0
aslc200b112008-08-16 12:57:46 +00002746 CompileUnit *Unit = GetBaseCompileUnit();
2747
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002748 // Don't include size of length
2749 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
aslc200b112008-08-16 12:57:46 +00002750
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002751 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
aslc200b112008-08-16 12:57:46 +00002752
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002753 EmitReference("info_begin", Unit->getID());
2754 Asm->EOL("Offset of Compilation Unit Info");
2755
Dan Gohmancfb72b22007-09-27 23:12:31 +00002756 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002757
2758 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2759
2760 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2761 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2762
2763 // Range 1
2764 EmitReference("text_begin", 0); Asm->EOL("Address");
2765 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2766
2767 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2768 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002769#endif
aslc200b112008-08-16 12:57:46 +00002770
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002771 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002772 }
2773
2774 /// EmitDebugRanges - Emit visible names into a debug ranges section.
2775 ///
2776 void EmitDebugRanges() {
2777 // Start the dwarf ranges section.
2778 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
aslc200b112008-08-16 12:57:46 +00002779
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002780 Asm->EOL();
2781 }
2782
2783 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2784 ///
2785 void EmitDebugMacInfo() {
Scott Michel79f01f52009-01-26 22:32:51 +00002786 if (TAI->doesSupportMacInfoSection()) {
2787 // Start the dwarf macinfo section.
2788 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
aslc200b112008-08-16 12:57:46 +00002789
Scott Michel79f01f52009-01-26 22:32:51 +00002790 Asm->EOL();
2791 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002792 }
2793
Evan Cheng3e288912009-02-25 07:04:34 +00002794 void ConstructCompileUnit(GlobalVariable *GV) {
2795 DICompileUnit DIUnit(GV);
Bill Wendling1c5842b2009-03-09 05:04:40 +00002796 std::string Dir, FN, Prod;
2797 unsigned ID = getOrCreateSourceID(DIUnit.getDirectory(Dir),
2798 DIUnit.getFilename(FN));
Evan Cheng3e288912009-02-25 07:04:34 +00002799
2800 DIE *Die = new DIE(DW_TAG_compile_unit);
2801 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2802 DWLabel("section_line", 0), DWLabel("section_line", 0),
2803 false);
Bill Wendling1c5842b2009-03-09 05:04:40 +00002804 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer(Prod));
Evan Cheng3e288912009-02-25 07:04:34 +00002805 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendling1c5842b2009-03-09 05:04:40 +00002806 AddString(Die, DW_AT_name, DW_FORM_string, FN);
2807 if (!Dir.empty())
2808 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Cheng3e288912009-02-25 07:04:34 +00002809 if (DIUnit.isOptimized())
2810 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendling1c5842b2009-03-09 05:04:40 +00002811 std::string Flags;
2812 DIUnit.getFlags(Flags);
Evan Cheng3e288912009-02-25 07:04:34 +00002813 if (!Flags.empty())
2814 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
2815 unsigned RVer = DIUnit.getRunTimeVersion();
2816 if (RVer)
2817 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
2818
2819 CompileUnit *Unit = new CompileUnit(ID, Die);
2820 if (DIUnit.isMain()) {
2821 assert(!MainCU && "Multiple main compile units are found!");
2822 MainCU = Unit;
2823 }
2824 CompileUnitMap[DIUnit.getGV()] = Unit;
2825 CompileUnits.push_back(Unit);
2826 }
2827
Devang Patel289f2362009-01-05 23:11:11 +00002828 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Patelb3907da2009-01-05 23:03:32 +00002829 void ConstructCompileUnits() {
Evan Cheng3e288912009-02-25 07:04:34 +00002830 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.compile_units");
2831 if (!Root)
2832 return;
2833 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2834 "Malformed compile unit descriptor anchor type");
2835 Constant *RootC = cast<Constant>(*Root->use_begin());
2836 assert(RootC->hasNUsesOrMore(1) &&
2837 "Malformed compile unit descriptor anchor type");
2838 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2839 UI != UE; ++UI)
2840 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2841 UUI != UUE; ++UUI) {
2842 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2843 ConstructCompileUnit(GV);
Devang Patel2ae1db52009-01-30 18:20:31 +00002844 }
Evan Cheng3e288912009-02-25 07:04:34 +00002845 }
2846
2847 bool ConstructGlobalVariableDIE(GlobalVariable *GV) {
2848 DIGlobalVariable DI_GV(GV);
2849 CompileUnit *DW_Unit = MainCU;
2850 if (!DW_Unit)
2851 DW_Unit = FindCompileUnit(DI_GV.getCompileUnit());
2852
2853 // Check for pre-existence.
2854 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
2855 if (Slot)
2856 return false;
2857
2858 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
2859
2860 // Add address.
2861 DIEBlock *Block = new DIEBlock();
2862 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
2863 AddObjectLabel(Block, 0, DW_FORM_udata,
2864 Asm->getGlobalLinkName(DI_GV.getGlobal()));
2865 AddBlock(VariableDie, DW_AT_location, 0, Block);
2866
2867 // Add to map.
2868 Slot = VariableDie;
2869 // Add to context owner.
2870 DW_Unit->getDie()->AddChild(VariableDie);
2871 // Expose as global. FIXME - need to check external flag.
Bill Wendling1c5842b2009-03-09 05:04:40 +00002872 std::string Name;
2873 DW_Unit->AddGlobal(DI_GV.getName(Name), VariableDie);
Evan Cheng3e288912009-02-25 07:04:34 +00002874 return true;
Devang Patelb3907da2009-01-05 23:03:32 +00002875 }
2876
Devang Patel289f2362009-01-05 23:11:11 +00002877 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patela9169c32009-02-24 00:02:15 +00002878 /// visible global variables. Return true if at least one global DIE is
2879 /// created.
2880 bool ConstructGlobalVariableDIEs() {
Evan Cheng3e288912009-02-25 07:04:34 +00002881 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
2882 if (!Root)
2883 return false;
Devang Patel289f2362009-01-05 23:11:11 +00002884
Evan Cheng3e288912009-02-25 07:04:34 +00002885 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2886 "Malformed global variable descriptor anchor type");
2887 Constant *RootC = cast<Constant>(*Root->use_begin());
2888 assert(RootC->hasNUsesOrMore(1) &&
2889 "Malformed global variable descriptor anchor type");
Devang Patel289f2362009-01-05 23:11:11 +00002890
Evan Cheng3e288912009-02-25 07:04:34 +00002891 bool Result = false;
2892 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2893 UI != UE; ++UI)
2894 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2895 UUI != UUE; ++UUI) {
2896 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2897 Result |= ConstructGlobalVariableDIE(GV);
2898 }
2899 return Result;
2900 }
Devang Patel289f2362009-01-05 23:11:11 +00002901
Evan Cheng3e288912009-02-25 07:04:34 +00002902 bool ConstructSubprogram(GlobalVariable *GV) {
2903 DISubprogram SP(GV);
2904 CompileUnit *Unit = MainCU;
2905 if (!Unit)
2906 Unit = FindCompileUnit(SP.getCompileUnit());
Devang Patel289f2362009-01-05 23:11:11 +00002907
Evan Cheng3e288912009-02-25 07:04:34 +00002908 // Check for pre-existence.
2909 DIE *&Slot = Unit->getDieMapSlotFor(GV);
2910 if (Slot)
2911 return false;
2912
2913 if (!SP.isDefinition())
2914 // This is a method declaration which will be handled while
2915 // constructing class type.
2916 return false;
2917
2918 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
2919
2920 // Add to map.
2921 Slot = SubprogramDie;
2922 // Add to context owner.
2923 Unit->getDie()->AddChild(SubprogramDie);
2924 // Expose as global.
Bill Wendling1c5842b2009-03-09 05:04:40 +00002925 std::string Name;
2926 Unit->AddGlobal(SP.getName(Name), SubprogramDie);
Evan Cheng3e288912009-02-25 07:04:34 +00002927 return true;
Devang Patel289f2362009-01-05 23:11:11 +00002928 }
2929
Devang Patele6caf012009-01-05 23:21:35 +00002930 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patela9169c32009-02-24 00:02:15 +00002931 /// subprograms. Return true if at least one subprogram DIE is created.
2932 bool ConstructSubprograms() {
Evan Cheng3e288912009-02-25 07:04:34 +00002933 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
2934 if (!Root)
2935 return false;
Devang Patele6caf012009-01-05 23:21:35 +00002936
Evan Cheng3e288912009-02-25 07:04:34 +00002937 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2938 "Malformed subprogram descriptor anchor type");
2939 Constant *RootC = cast<Constant>(*Root->use_begin());
2940 assert(RootC->hasNUsesOrMore(1) &&
2941 "Malformed subprogram descriptor anchor type");
Devang Patele6caf012009-01-05 23:21:35 +00002942
Evan Cheng3e288912009-02-25 07:04:34 +00002943 bool Result = false;
2944 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2945 UI != UE; ++UI)
2946 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2947 UUI != UUE; ++UUI) {
2948 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2949 Result |= ConstructSubprogram(GV);
2950 }
2951 return Result;
Devang Patele6caf012009-01-05 23:21:35 +00002952 }
2953
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002954public:
2955 //===--------------------------------------------------------------------===//
2956 // Main entry points.
2957 //
Owen Anderson847b99b2008-08-21 00:14:44 +00002958 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattnerb3876c72007-09-24 03:35:37 +00002959 : Dwarf(OS, A, T, "dbg")
Devang Patel2ae1db52009-01-30 18:20:31 +00002960 , MainCU(NULL)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002961 , AbbreviationsSet(InitAbbreviationsSetSize)
2962 , Abbreviations()
2963 , ValuesSet(InitValuesSetSize)
2964 , Values()
2965 , StringPool()
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002966 , SectionMap()
2967 , SectionSourceLines()
2968 , didInitial(false)
2969 , shouldEmit(false)
Devang Patel4d1709e2009-01-08 02:33:41 +00002970 , RootDbgScope(NULL)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002971 {
2972 }
2973 virtual ~DwarfDebug() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002974 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2975 delete Values[j];
2976 }
2977
Devang Patel9304b382009-01-06 21:07:30 +00002978 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
2979 /// This is inovked by the target AsmPrinter.
Devang Patel91d27b02009-01-12 23:09:42 +00002980 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendling6baa18d2009-02-03 21:38:21 +00002981 // Create all the compile unit DIEs.
2982 ConstructCompileUnits();
Devang Patel91d27b02009-01-12 23:09:42 +00002983
Evan Cheng3e288912009-02-25 07:04:34 +00002984 if (CompileUnits.empty())
Bill Wendling6baa18d2009-02-03 21:38:21 +00002985 return;
Devang Patel91d27b02009-01-12 23:09:42 +00002986
Devang Patela9169c32009-02-24 00:02:15 +00002987 // Create DIEs for each of the externally visible global variables.
2988 bool globalDIEs = ConstructGlobalVariableDIEs();
2989
2990 // Create DIEs for each of the externally visible subprograms.
2991 bool subprogramDIEs = ConstructSubprograms();
2992
2993 // If there is not any debug info available for any global variables
2994 // and any subprograms then there is not any debug info to emit.
2995 if (!globalDIEs && !subprogramDIEs)
2996 return;
2997
Bill Wendling6baa18d2009-02-03 21:38:21 +00002998 MMI = mmi;
2999 shouldEmit = true;
3000 MMI->setDebugInfoAvailability(true);
Devang Patel9304b382009-01-06 21:07:30 +00003001
Bill Wendling6baa18d2009-02-03 21:38:21 +00003002 // Prime section data.
3003 SectionMap.insert(TAI->getTextSection());
Devang Patel9304b382009-01-06 21:07:30 +00003004
Bill Wendling6baa18d2009-02-03 21:38:21 +00003005 // Print out .file directives to specify files for .loc directives. These
3006 // are printed out early so that they precede any .loc directives.
3007 if (TAI->hasDotLocAndDotFile()) {
Evan Cheng3e288912009-02-25 07:04:34 +00003008 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
3009 // Remember source id starts at 1.
3010 std::pair<unsigned, unsigned> Id = getSourceDirsectoryAndFileIds(i);
3011 sys::Path FullPath(getSourceDirectoryName(Id.first));
3012 bool AppendOk =
3013 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendling6baa18d2009-02-03 21:38:21 +00003014 assert(AppendOk && "Could not append filename to directory!");
3015 AppendOk = false;
3016 Asm->EmitFile(i, FullPath.toString());
3017 Asm->EOL();
Devang Patel9304b382009-01-06 21:07:30 +00003018 }
Bill Wendling6baa18d2009-02-03 21:38:21 +00003019 }
Devang Patel9304b382009-01-06 21:07:30 +00003020
Bill Wendling6baa18d2009-02-03 21:38:21 +00003021 // Emit initial sections
3022 EmitInitial();
Devang Patel9304b382009-01-06 21:07:30 +00003023 }
3024
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003025 /// BeginModule - Emit all Dwarf sections that should come prior to the
3026 /// content.
3027 void BeginModule(Module *M) {
3028 this->M = M;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003029 }
3030
3031 /// EndModule - Emit all Dwarf sections that should come after the content.
3032 ///
3033 void EndModule() {
Bill Wendling50db0792009-02-20 00:44:43 +00003034 if (!ShouldEmitDwarfDebug()) return;
aslc200b112008-08-16 12:57:46 +00003035
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003036 // Standard sections final addresses.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003037 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003038 EmitLabel("text_end", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00003039 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003040 EmitLabel("data_end", 0);
aslc200b112008-08-16 12:57:46 +00003041
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003042 // End text sections.
3043 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003044 Asm->SwitchToSection(SectionMap[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003045 EmitLabel("section_end", i);
3046 }
3047
3048 // Emit common frame information.
3049 EmitCommonDebugFrame();
3050
3051 // Emit function debug frame information
3052 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3053 E = DebugFrames.end(); I != E; ++I)
3054 EmitFunctionDebugFrame(*I);
3055
3056 // Compute DIE offsets and sizes.
3057 SizeAndOffsets();
aslc200b112008-08-16 12:57:46 +00003058
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003059 // Emit all the DIEs into a debug info section
3060 EmitDebugInfo();
aslc200b112008-08-16 12:57:46 +00003061
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003062 // Corresponding abbreviations into a abbrev section.
3063 EmitAbbreviations();
aslc200b112008-08-16 12:57:46 +00003064
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003065 // Emit source line correspondence into a debug line section.
3066 EmitDebugLines();
aslc200b112008-08-16 12:57:46 +00003067
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003068 // Emit info into a debug pubnames section.
3069 EmitDebugPubNames();
aslc200b112008-08-16 12:57:46 +00003070
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003071 // Emit info into a debug str section.
3072 EmitDebugStr();
aslc200b112008-08-16 12:57:46 +00003073
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003074 // Emit info into a debug loc section.
3075 EmitDebugLoc();
aslc200b112008-08-16 12:57:46 +00003076
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003077 // Emit info into a debug aranges section.
3078 EmitDebugARanges();
aslc200b112008-08-16 12:57:46 +00003079
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003080 // Emit info into a debug ranges section.
3081 EmitDebugRanges();
aslc200b112008-08-16 12:57:46 +00003082
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003083 // Emit info into a debug macinfo section.
3084 EmitDebugMacInfo();
3085 }
3086
aslc200b112008-08-16 12:57:46 +00003087 /// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003088 /// emitted immediately after the function entry point.
3089 void BeginFunction(MachineFunction *MF) {
3090 this->MF = MF;
aslc200b112008-08-16 12:57:46 +00003091
Bill Wendling50db0792009-02-20 00:44:43 +00003092 if (!ShouldEmitDwarfDebug()) return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003093
3094 // Begin accumulating function debug information.
3095 MMI->BeginFunction(MF);
aslc200b112008-08-16 12:57:46 +00003096
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003097 // Assumes in correct section after the entry point.
3098 EmitLabel("func_begin", ++SubprogramCount);
Evan Chenga53c40a2008-02-01 09:10:45 +00003099
3100 // Emit label for the implicitly defined dbg.stoppoint at the start of
3101 // the function.
Devang Patel35a078f2009-01-12 22:54:42 +00003102 if (!Lines.empty()) {
3103 const SrcLineInfo &LineInfo = Lines[0];
Andrew Lenharth42f91402008-04-03 17:37:43 +00003104 Asm->printLabel(LineInfo.getLabelID());
3105 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003106 }
aslc200b112008-08-16 12:57:46 +00003107
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003108 /// EndFunction - Gather and emit post-function debug information.
3109 ///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003110 void EndFunction(MachineFunction *MF) {
Bill Wendling50db0792009-02-20 00:44:43 +00003111 if (!ShouldEmitDwarfDebug()) return;
aslc200b112008-08-16 12:57:46 +00003112
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003113 // Define end label for subprogram.
3114 EmitLabel("func_end", SubprogramCount);
aslc200b112008-08-16 12:57:46 +00003115
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003116 // Get function line info.
Devang Patel35a078f2009-01-12 22:54:42 +00003117 if (!Lines.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003118 // Get section line info.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003119 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003120 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patel35a078f2009-01-12 22:54:42 +00003121 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003122 // Append the function info to section info.
3123 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patel35a078f2009-01-12 22:54:42 +00003124 Lines.begin(), Lines.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003125 }
aslc200b112008-08-16 12:57:46 +00003126
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003127 // Construct scopes for subprogram.
Devang Patel6ccd57e2009-01-13 00:20:51 +00003128 if (RootDbgScope)
3129 ConstructRootDbgScope(RootDbgScope);
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003130 else
3131 // FIXME: This is wrong. We are essentially getting past a problem with
3132 // debug information not being able to handle unreachable blocks that have
3133 // debug information in them. In particular, those unreachable blocks that
3134 // have "region end" info in them. That situation results in the "root
3135 // scope" not being created. If that's the case, then emit a "default"
3136 // scope, i.e., one that encompasses the whole function. This isn't
3137 // desirable. And a better way of handling this (and all of the debugging
3138 // information) needs to be explored.
Devang Patel6ccd57e2009-01-13 00:20:51 +00003139 ConstructDefaultDbgScope(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003140
3141 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3142 MMI->getFrameMoves()));
Devang Patela4162952009-01-12 18:48:36 +00003143
3144 // Clear debug info
3145 if (RootDbgScope) {
3146 delete RootDbgScope;
3147 DbgScopeMap.clear();
3148 RootDbgScope = NULL;
3149 }
3150 Lines.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003151 }
Devang Patelcb59fd42009-01-12 19:17:34 +00003152
3153public:
3154
Devang Patel2da0cc42009-01-15 23:41:32 +00003155 /// ValidDebugInfo - Return true if V represents valid debug info value.
3156 bool ValidDebugInfo(Value *V) {
Devang Patel208098b2009-01-19 23:21:49 +00003157 if (!V)
3158 return false;
3159
Devang Patel4d92dded2009-01-16 01:49:46 +00003160 if (!shouldEmit)
3161 return false;
3162
Devang Patel2da0cc42009-01-15 23:41:32 +00003163 GlobalVariable *GV = getGlobalVariable(V);
3164 if (!GV)
3165 return false;
Duncan Sands19d161f2009-03-07 15:45:40 +00003166
3167 if (!GV->hasInternalLinkage () && !GV->hasLinkOnceLinkage())
Devang Patel2da0cc42009-01-15 23:41:32 +00003168 return false;
3169
3170 DIDescriptor DI(GV);
3171 // Check current version. Allow Version6 for now.
3172 unsigned Version = DI.getVersion();
Devang Patelb49710a2009-01-20 19:22:03 +00003173 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6)
Devang Patel2da0cc42009-01-15 23:41:32 +00003174 return false;
3175
Devang Patel208098b2009-01-19 23:21:49 +00003176 unsigned Tag = DI.getTag();
3177 switch (Tag) {
3178 case DW_TAG_variable:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003179 assert(DIVariable(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003180 break;
3181 case DW_TAG_compile_unit:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003182 assert(DICompileUnit(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003183 break;
3184 case DW_TAG_subprogram:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003185 assert(DISubprogram(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003186 break;
3187 default:
3188 break;
3189 }
3190
Devang Patel2da0cc42009-01-15 23:41:32 +00003191 return true;
3192 }
3193
Devang Patelcb59fd42009-01-12 19:17:34 +00003194 /// RecordSourceLine - Records location information and associates it with a
3195 /// label. Returns a unique label ID used to generate a label and provide
3196 /// correspondence to the source line list.
3197 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
Evan Cheng3e288912009-02-25 07:04:34 +00003198 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendling6baa18d2009-02-03 21:38:21 +00003199 assert(Unit && "Unable to find CompileUnit");
Devang Patelcb59fd42009-01-12 19:17:34 +00003200 unsigned ID = MMI->NextLabelID();
3201 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
3202 return ID;
3203 }
3204
3205 /// RecordSourceLine - Records location information and associates it with a
3206 /// label. Returns a unique label ID used to generate a label and provide
3207 /// correspondence to the source line list.
3208 unsigned RecordSourceLine(unsigned Line, unsigned Col, unsigned Src) {
3209 unsigned ID = MMI->NextLabelID();
3210 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
3211 return ID;
3212 }
3213
3214 unsigned getRecordSourceLineCount() {
3215 return Lines.size();
3216 }
3217
Evan Cheng3e288912009-02-25 07:04:34 +00003218 /// getNumSourceDirectories - Return the number of source directories in the
3219 /// debug info.
3220 unsigned getNumSourceDirectories() const {
3221 return DirectoryNames.size();
3222 }
3223
3224 /// getSourceDirectoryName - Return the name of the directory corresponding
3225 /// to the id.
3226 const std::string &getSourceDirectoryName(unsigned Id) const {
3227 return DirectoryNames[Id - 1];
3228 }
3229
3230 /// getNumSourceFiles - Return the number of source files in the debug info.
3231 ///
3232 unsigned getNumSourceFiles() const {
3233 return SourceFileNames.size();
3234 }
3235
3236 /// getSourceFileName - Return the name of the source file corresponding
3237 /// to the id.
3238 const std::string &getSourceFileName(unsigned Id) const {
3239 return SourceFileNames[Id - 1];
3240 }
3241
3242 /// getNumSourceIds - Return the number of unique source ids.
3243 ///
3244 unsigned getNumSourceIds() const {
3245 return SourceIds.size();
3246 }
3247
3248 /// getSourceDirsectoryAndFileIds - Return the directory and file ids that
3249 /// maps to the source id. Source id starts at 1.
3250 std::pair<unsigned, unsigned>
3251 getSourceDirsectoryAndFileIds(unsigned SId) const {
3252 return SourceIds[SId-1];
3253 }
3254
3255 /// getOrCreateSourceID - Look up the source id with the given directory and
3256 /// source file names. If none currently exists, create a new id and insert it
3257 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
3258 /// as well.
3259 unsigned getOrCreateSourceID(const std::string &DirName,
3260 const std::string &FileName) {
3261 unsigned DId;
3262 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
3263 if (DI != DirectoryIdMap.end())
3264 DId = DI->getValue();
3265 else {
3266 DId = DirectoryNames.size() + 1;
3267 DirectoryIdMap[DirName] = DId;
3268 DirectoryNames.push_back(DirName);
3269 }
3270
3271 unsigned FId;
3272 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
3273 if (FI != SourceFileIdMap.end())
3274 FId = FI->getValue();
3275 else {
3276 FId = SourceFileNames.size() + 1;
3277 SourceFileIdMap[FileName] = FId;
3278 SourceFileNames.push_back(FileName);
3279 }
3280
3281 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
3282 SourceIdMap.find(std::make_pair(DId, FId));
3283 if (SI != SourceIdMap.end())
3284 return SI->second;
3285 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
3286 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
3287 SourceIds.push_back(std::make_pair(DId, FId));
3288 return SrcId;
Devang Patelcb59fd42009-01-12 19:17:34 +00003289 }
3290
3291 /// RecordRegionStart - Indicate the start of a region.
3292 ///
3293 unsigned RecordRegionStart(GlobalVariable *V) {
3294 DbgScope *Scope = getOrCreateScope(V);
3295 unsigned ID = MMI->NextLabelID();
3296 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
3297 return ID;
3298 }
3299
3300 /// RecordRegionEnd - Indicate the end of a region.
3301 ///
3302 unsigned RecordRegionEnd(GlobalVariable *V) {
3303 DbgScope *Scope = getOrCreateScope(V);
3304 unsigned ID = MMI->NextLabelID();
3305 Scope->setEndLabelID(ID);
3306 return ID;
3307 }
3308
3309 /// RecordVariable - Indicate the declaration of a local variable.
3310 ///
3311 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
Devang Patel2560d922009-01-15 18:25:17 +00003312 DIDescriptor Desc(GV);
3313 DbgScope *Scope = NULL;
3314 if (Desc.getTag() == DW_TAG_variable) {
3315 // GV is a global variable.
3316 DIGlobalVariable DG(GV);
3317 Scope = getOrCreateScope(DG.getContext().getGV());
3318 } else {
3319 // or GV is a local variable.
3320 DIVariable DV(GV);
3321 Scope = getOrCreateScope(DV.getContext().getGV());
3322 }
Bill Wendling6baa18d2009-02-03 21:38:21 +00003323 assert(Scope && "Unable to find variable' scope");
Devang Patel7c8a2772009-01-16 19:28:14 +00003324 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelcb59fd42009-01-12 19:17:34 +00003325 Scope->AddVariable(DV);
3326 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003327};
3328
3329//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00003330/// DwarfException - Emits Dwarf exception handling directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003331///
3332class DwarfException : public Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003333 struct FunctionEHFrameInfo {
3334 std::string FnName;
3335 unsigned Number;
3336 unsigned PersonalityIndex;
3337 bool hasCalls;
3338 bool hasLandingPads;
3339 std::vector<MachineMove> Moves;
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003340 const Function * function;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003341
3342 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3343 bool hC, bool hL,
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003344 const std::vector<MachineMove> &M,
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003345 const Function *f):
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003346 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003347 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003348 };
3349
3350 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen85535762008-04-02 00:25:04 +00003351
3352 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3353 /// be emitted.
3354 bool shouldEmitTable;
3355
3356 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3357 /// should be emitted.
3358 bool shouldEmitMoves;
3359
3360 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3361 /// should be emitted.
3362 bool shouldEmitTableModule;
3363
aslc200b112008-08-16 12:57:46 +00003364 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen85535762008-04-02 00:25:04 +00003365 /// should be emitted.
3366 bool shouldEmitMovesModule;
Duncan Sands96144f92008-05-07 19:11:09 +00003367
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003368 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3369 ///
3370 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
3371 // Size and sign of stack growth.
3372 int stackGrowth =
3373 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3374 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00003375 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003376
3377 // Begin eh frame section.
3378 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling189bde72008-12-24 08:05:17 +00003379
3380 if (!TAI->doesRequireNonLocalEHFrameLabel())
3381 O << TAI->getEHGlobalPrefix();
3382 O << "EH_frame" << Index << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003383 EmitLabel("section_eh_frame", Index);
3384
3385 // Define base labels.
3386 EmitLabel("eh_frame_common", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003387
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003388 // Define the eh frame length.
3389 EmitDifference("eh_frame_common_end", Index,
3390 "eh_frame_common_begin", Index, true);
3391 Asm->EOL("Length of Common Information Entry");
3392
3393 // EH frame header.
3394 EmitLabel("eh_frame_common_begin", Index);
3395 Asm->EmitInt32((int)0);
3396 Asm->EOL("CIE Identifier Tag");
3397 Asm->EmitInt8(DW_CIE_VERSION);
3398 Asm->EOL("CIE Version");
Duncan Sands96144f92008-05-07 19:11:09 +00003399
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003400 // The personality presence indicates that language specific information
3401 // will show up in the eh frame.
3402 Asm->EmitString(Personality ? "zPLR" : "zR");
3403 Asm->EOL("CIE Augmentation");
Duncan Sands96144f92008-05-07 19:11:09 +00003404
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003405 // Round out reader.
3406 Asm->EmitULEB128Bytes(1);
3407 Asm->EOL("CIE Code Alignment Factor");
3408 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands96144f92008-05-07 19:11:09 +00003409 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00003410 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands96144f92008-05-07 19:11:09 +00003411 Asm->EOL("CIE Return Address Column");
3412
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003413 // If there is a personality, we need to indicate the functions location.
3414 if (Personality) {
3415 Asm->EmitULEB128Bytes(7);
3416 Asm->EOL("Augmentation Size");
Bill Wendling2d369922007-09-11 17:20:55 +00003417
Duncan Sands96144f92008-05-07 19:11:09 +00003418 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendling2d369922007-09-11 17:20:55 +00003419 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands96144f92008-05-07 19:11:09 +00003420 Asm->EOL("Personality (pcrel sdata4 indirect)");
3421 } else {
Bill Wendling2d369922007-09-11 17:20:55 +00003422 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands96144f92008-05-07 19:11:09 +00003423 Asm->EOL("Personality (pcrel sdata4)");
3424 }
Bill Wendling2d369922007-09-11 17:20:55 +00003425
Duncan Sands96144f92008-05-07 19:11:09 +00003426 PrintRelDirective(true);
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003427 O << TAI->getPersonalityPrefix();
3428 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3429 O << TAI->getPersonalitySuffix();
Duncan Sands4cc39532008-05-08 12:33:11 +00003430 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3431 O << "-" << TAI->getPCSymbol();
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003432 Asm->EOL("Personality");
Bill Wendling38cb7c92007-08-25 00:51:55 +00003433
Duncan Sands96144f92008-05-07 19:11:09 +00003434 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3435 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003436
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003437 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3438 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003439 } else {
3440 Asm->EmitULEB128Bytes(1);
3441 Asm->EOL("Augmentation Size");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003442
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003443 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3444 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003445 }
3446
3447 // Indicate locations of general callee saved registers in frame.
3448 std::vector<MachineMove> Moves;
3449 RI->getInitialFrameState(Moves);
Dale Johannesenf5a11532007-11-13 19:13:01 +00003450 EmitFrameMoves(NULL, 0, Moves, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003451
Dale Johannesen388f20f2008-04-30 00:43:29 +00003452 // On Darwin the linker honors the alignment of eh_frame, which means it
3453 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3454 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003455 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003456 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003457 EmitLabel("eh_frame_common_end", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003458
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003459 Asm->EOL();
3460 }
Duncan Sands96144f92008-05-07 19:11:09 +00003461
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003462 /// EmitEHFrame - Emit function exception frame information.
3463 ///
3464 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003465 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3466
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003467 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3468
3469 // Externally visible entry into the functions eh frame info.
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003470 // If the corresponding function is static, this should not be
3471 // externally visible.
Rafael Espindolaa168fc92009-01-15 20:18:42 +00003472 if (linkage != Function::InternalLinkage &&
Devang Patel245446c2009-01-17 08:05:14 +00003473 linkage != Function::PrivateLinkage) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003474 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3475 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3476 }
3477
Dale Johannesenf09b5992008-01-10 02:03:30 +00003478 // If corresponding function is weak definition, this should be too.
Duncan Sands19d161f2009-03-07 15:45:40 +00003479 if ((linkage == Function::WeakAnyLinkage ||
3480 linkage == Function::WeakODRLinkage ||
3481 linkage == Function::LinkOnceAnyLinkage ||
3482 linkage == Function::LinkOnceODRLinkage) &&
Dale Johannesenf09b5992008-01-10 02:03:30 +00003483 TAI->getWeakDefDirective())
3484 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3485
3486 // If there are no calls then you can't unwind. This may mean we can
3487 // omit the EH Frame, but some environments do not handle weak absolute
aslc200b112008-08-16 12:57:46 +00003488 // symbols.
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003489 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesena9b3e482008-04-08 00:10:24 +00003490 // unwind info is to be available for non-EH uses.
Dale Johannesenf09b5992008-01-10 02:03:30 +00003491 if (!EHFrameInfo.hasCalls &&
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003492 !UnwindTablesMandatory &&
Duncan Sands19d161f2009-03-07 15:45:40 +00003493 ((linkage != Function::WeakAnyLinkage &&
3494 linkage != Function::WeakODRLinkage &&
3495 linkage != Function::LinkOnceAnyLinkage &&
3496 linkage != Function::LinkOnceODRLinkage) ||
Dale Johannesenf09b5992008-01-10 02:03:30 +00003497 !TAI->getWeakDefDirective() ||
3498 TAI->getSupportsWeakOmittedEHFrame()))
aslc200b112008-08-16 12:57:46 +00003499 {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003500 O << EHFrameInfo.FnName << " = 0\n";
aslc200b112008-08-16 12:57:46 +00003501 // This name has no connection to the function, so it might get
3502 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003503 // dead-stripping unconditionally.
3504 if (const char *UsedDirective = TAI->getUsedDirective())
3505 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003506 } else {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003507 O << EHFrameInfo.FnName << ":\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003508
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003509 // EH frame header.
3510 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3511 "eh_frame_begin", EHFrameInfo.Number, true);
3512 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00003513
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003514 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
3515
Bill Wendling189bde72008-12-24 08:05:17 +00003516 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3517 PrintRelDirective(true, true);
3518 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3519
3520 if (!TAI->isAbsoluteEHSectionOffsets())
3521 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3522 } else {
3523 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3524 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3525 true, true, false);
3526 }
3527
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003528 Asm->EOL("FDE CIE offset");
3529
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003530 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003531 Asm->EOL("FDE initial location");
3532 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003533 "eh_func_begin", EHFrameInfo.Number, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003534 Asm->EOL("FDE address range");
Duncan Sands96144f92008-05-07 19:11:09 +00003535
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003536 // If there is a personality and landing pads then point to the language
3537 // specific data area in the exception table.
3538 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands96144f92008-05-07 19:11:09 +00003539 Asm->EmitULEB128Bytes(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003540 Asm->EOL("Augmentation size");
Duncan Sands96144f92008-05-07 19:11:09 +00003541
3542 if (EHFrameInfo.hasLandingPads)
3543 EmitReference("exception", EHFrameInfo.Number, true, true);
3544 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003545 Asm->EmitInt32((int)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003546 Asm->EOL("Language Specific Data Area");
3547 } else {
3548 Asm->EmitULEB128Bytes(0);
3549 Asm->EOL("Augmentation size");
3550 }
Duncan Sands96144f92008-05-07 19:11:09 +00003551
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003552 // Indicate locations of function specific callee saved registers in
3553 // frame.
Devang Patelb28de842009-01-17 08:01:33 +00003554 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00003555 true);
aslc200b112008-08-16 12:57:46 +00003556
Dale Johannesen388f20f2008-04-30 00:43:29 +00003557 // On Darwin the linker honors the alignment of eh_frame, which means it
3558 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3559 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003560 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003561 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003562 EmitLabel("eh_frame_end", EHFrameInfo.Number);
aslc200b112008-08-16 12:57:46 +00003563
3564 // If the function is marked used, this table should be also. We cannot
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003565 // make the mark unconditional in this case, since retaining the table
aslc200b112008-08-16 12:57:46 +00003566 // also retains the function in this case, and there is code around
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003567 // that depends on unused functions (calling undefined externals) being
3568 // dead-stripped to link correctly. Yes, there really is.
3569 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3570 if (const char *UsedDirective = TAI->getUsedDirective())
3571 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3572 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003573 }
3574
Duncan Sands241a0c92007-09-05 11:27:52 +00003575 /// EmitExceptionTable - Emit landing pads and actions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003576 ///
3577 /// The general organization of the table is complex, but the basic concepts
3578 /// are easy. First there is a header which describes the location and
3579 /// organization of the three components that follow.
3580 /// 1. The landing pad site information describes the range of code covered
3581 /// by the try. In our case it's an accumulation of the ranges covered
3582 /// by the invokes in the try. There is also a reference to the landing
3583 /// pad that handles the exception once processed. Finally an index into
3584 /// the actions table.
3585 /// 2. The action table, in our case, is composed of pairs of type ids
3586 /// and next action offset. Starting with the action index from the
3587 /// landing pad site, each type Id is checked for a match to the current
3588 /// exception. If it matches then the exception and type id are passed
3589 /// on to the landing pad. Otherwise the next action is looked up. This
3590 /// chain is terminated with a next action of zero. If no type id is
3591 /// found the the frame is unwound and handling continues.
3592 /// 3. Type id table contains references to all the C++ typeinfo for all
3593 /// catches in the function. This tables is reversed indexed base 1.
3594
3595 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3596 static unsigned SharedTypeIds(const LandingPadInfo *L,
3597 const LandingPadInfo *R) {
3598 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3599 unsigned LSize = LIds.size(), RSize = RIds.size();
3600 unsigned MinSize = LSize < RSize ? LSize : RSize;
3601 unsigned Count = 0;
3602
3603 for (; Count != MinSize; ++Count)
3604 if (LIds[Count] != RIds[Count])
3605 return Count;
3606
3607 return Count;
3608 }
3609
3610 /// PadLT - Order landing pads lexicographically by type id.
3611 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
3612 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3613 unsigned LSize = LIds.size(), RSize = RIds.size();
3614 unsigned MinSize = LSize < RSize ? LSize : RSize;
3615
3616 for (unsigned i = 0; i != MinSize; ++i)
3617 if (LIds[i] != RIds[i])
3618 return LIds[i] < RIds[i];
3619
3620 return LSize < RSize;
3621 }
3622
3623 struct KeyInfo {
3624 static inline unsigned getEmptyKey() { return -1U; }
3625 static inline unsigned getTombstoneKey() { return -2U; }
3626 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner92eea072007-09-17 18:34:04 +00003627 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003628 static bool isPod() { return true; }
3629 };
3630
Duncan Sands241a0c92007-09-05 11:27:52 +00003631 /// ActionEntry - Structure describing an entry in the actions table.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003632 struct ActionEntry {
3633 int ValueForTypeID; // The value to write - may not be equal to the type id.
3634 int NextAction;
3635 struct ActionEntry *Previous;
3636 };
3637
Duncan Sands241a0c92007-09-05 11:27:52 +00003638 /// PadRange - Structure holding a try-range and the associated landing pad.
3639 struct PadRange {
3640 // The index of the landing pad.
3641 unsigned PadIndex;
3642 // The index of the begin and end labels in the landing pad's label lists.
3643 unsigned RangeIndex;
3644 };
3645
3646 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3647
3648 /// CallSiteEntry - Structure describing an entry in the call-site table.
3649 struct CallSiteEntry {
Duncan Sands4ff179f2007-12-19 07:36:31 +00003650 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003651 unsigned BeginLabel; // zero indicates the start of the function.
3652 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003653 // The landing pad starts at PadLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003654 unsigned PadLabel; // zero indicates that there is no landing pad.
3655 unsigned Action;
3656 };
3657
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003658 void EmitExceptionTable() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003659 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
3660 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
3661 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3662 if (PadInfos.empty()) return;
3663
3664 // Sort the landing pads in order of their type ids. This is used to fold
3665 // duplicate actions.
3666 SmallVector<const LandingPadInfo *, 64> LandingPads;
3667 LandingPads.reserve(PadInfos.size());
3668 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
3669 LandingPads.push_back(&PadInfos[i]);
3670 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3671
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003672 // Negative type ids index into FilterIds, positive type ids index into
3673 // TypeInfos. The value written for a positive type id is just the type
3674 // id itself. For a negative type id, however, the value written is the
3675 // (negative) byte offset of the corresponding FilterIds entry. The byte
3676 // offset is usually equal to the type id, because the FilterIds entries
3677 // are written using a variable width encoding which outputs one byte per
3678 // entry as long as the value written is not too large, but can differ.
3679 // This kind of complication does not occur for positive type ids because
3680 // type infos are output using a fixed width encoding.
3681 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3682 SmallVector<int, 16> FilterOffsets;
3683 FilterOffsets.reserve(FilterIds.size());
3684 int Offset = -1;
3685 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3686 E = FilterIds.end(); I != E; ++I) {
3687 FilterOffsets.push_back(Offset);
aslc200b112008-08-16 12:57:46 +00003688 Offset -= TargetAsmInfo::getULEB128Size(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003689 }
3690
Duncan Sands241a0c92007-09-05 11:27:52 +00003691 // Compute the actions table and gather the first action index for each
3692 // landing pad site.
3693 SmallVector<ActionEntry, 32> Actions;
3694 SmallVector<unsigned, 64> FirstActions;
3695 FirstActions.reserve(LandingPads.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003696
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003697 int FirstAction = 0;
Duncan Sands241a0c92007-09-05 11:27:52 +00003698 unsigned SizeActions = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003699 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3700 const LandingPadInfo *LP = LandingPads[i];
3701 const std::vector<int> &TypeIds = LP->TypeIds;
3702 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
3703 unsigned SizeSiteActions = 0;
3704
3705 if (NumShared < TypeIds.size()) {
3706 unsigned SizeAction = 0;
3707 ActionEntry *PrevAction = 0;
3708
3709 if (NumShared) {
3710 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3711 assert(Actions.size());
3712 PrevAction = &Actions.back();
aslc200b112008-08-16 12:57:46 +00003713 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3714 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003715 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
aslc200b112008-08-16 12:57:46 +00003716 SizeAction -=
3717 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003718 SizeAction += -PrevAction->NextAction;
3719 PrevAction = PrevAction->Previous;
3720 }
3721 }
3722
3723 // Compute the actions.
3724 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3725 int TypeID = TypeIds[I];
3726 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3727 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
aslc200b112008-08-16 12:57:46 +00003728 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003729
3730 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
aslc200b112008-08-16 12:57:46 +00003731 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003732 SizeSiteActions += SizeAction;
3733
3734 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
3735 Actions.push_back(Action);
3736
3737 PrevAction = &Actions.back();
3738 }
3739
3740 // Record the first action of the landing pad site.
3741 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3742 } // else identical - re-use previous FirstAction
3743
3744 FirstActions.push_back(FirstAction);
3745
3746 // Compute this sites contribution to size.
3747 SizeActions += SizeSiteActions;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003748 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003749
Duncan Sands4ff179f2007-12-19 07:36:31 +00003750 // Compute the call-site table. The entry for an invoke has a try-range
3751 // containing the call, a non-zero landing pad and an appropriate action.
3752 // The entry for an ordinary call has a try-range containing the call and
3753 // zero for the landing pad and the action. Calls marked 'nounwind' have
3754 // no entry and must not be contained in the try-range of any entry - they
3755 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands241a0c92007-09-05 11:27:52 +00003756 SmallVector<CallSiteEntry, 64> CallSites;
3757
3758 RangeMapType PadMap;
Duncan Sands4ff179f2007-12-19 07:36:31 +00003759 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3760 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3761 // try-ranges for them need be deduced.
Duncan Sands241a0c92007-09-05 11:27:52 +00003762 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3763 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003764 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003765 unsigned BeginLabel = LandingPad->BeginLabels[j];
3766 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3767 PadRange P = { i, j };
3768 PadMap[BeginLabel] = P;
3769 }
3770 }
3771
Duncan Sands4ff179f2007-12-19 07:36:31 +00003772 // The end label of the previous invoke or nounwind try-range.
Duncan Sands241a0c92007-09-05 11:27:52 +00003773 unsigned LastLabel = 0;
Duncan Sands4ff179f2007-12-19 07:36:31 +00003774
3775 // Whether there is a potentially throwing instruction (currently this means
3776 // an ordinary call) between the end of the previous try-range and now.
3777 bool SawPotentiallyThrowing = false;
3778
3779 // Whether the last callsite entry was for an invoke.
3780 bool PreviousIsInvoke = false;
3781
Duncan Sands4ff179f2007-12-19 07:36:31 +00003782 // Visit all instructions in order of address.
Duncan Sands241a0c92007-09-05 11:27:52 +00003783 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3784 I != E; ++I) {
3785 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3786 MI != E; ++MI) {
Dan Gohmanfa607c92008-07-01 00:05:16 +00003787 if (!MI->isLabel()) {
Chris Lattner5b930372008-01-07 07:27:27 +00003788 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands241a0c92007-09-05 11:27:52 +00003789 continue;
3790 }
3791
Chris Lattnerda4cff12007-12-30 20:50:28 +00003792 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands241a0c92007-09-05 11:27:52 +00003793 assert(BeginLabel && "Invalid label!");
Duncan Sands89372f62007-09-05 14:12:46 +00003794
Duncan Sands4ff179f2007-12-19 07:36:31 +00003795 // End of the previous try-range?
Duncan Sands89372f62007-09-05 14:12:46 +00003796 if (BeginLabel == LastLabel)
Duncan Sands4ff179f2007-12-19 07:36:31 +00003797 SawPotentiallyThrowing = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003798
Duncan Sands4ff179f2007-12-19 07:36:31 +00003799 // Beginning of a new try-range?
Duncan Sands241a0c92007-09-05 11:27:52 +00003800 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands241a0c92007-09-05 11:27:52 +00003801 if (L == PadMap.end())
Duncan Sands4ff179f2007-12-19 07:36:31 +00003802 // Nope, it was just some random label.
Duncan Sands241a0c92007-09-05 11:27:52 +00003803 continue;
3804
3805 PadRange P = L->second;
3806 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3807
3808 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3809 "Inconsistent landing pad map!");
3810
3811 // If some instruction between the previous try-range and this one may
3812 // throw, create a call-site entry with no landing pad for the region
3813 // between the try-ranges.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003814 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003815 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3816 CallSites.push_back(Site);
Duncan Sands4ff179f2007-12-19 07:36:31 +00003817 PreviousIsInvoke = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003818 }
3819
3820 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003821 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands241a0c92007-09-05 11:27:52 +00003822
Duncan Sands4ff179f2007-12-19 07:36:31 +00003823 if (LandingPad->LandingPadLabel) {
3824 // This try-range is for an invoke.
3825 CallSiteEntry Site = {BeginLabel, LastLabel,
3826 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands241a0c92007-09-05 11:27:52 +00003827
Duncan Sands4ff179f2007-12-19 07:36:31 +00003828 // Try to merge with the previous call-site.
3829 if (PreviousIsInvoke) {
Dan Gohman3d436002008-06-21 22:00:54 +00003830 CallSiteEntry &Prev = CallSites.back();
Duncan Sands4ff179f2007-12-19 07:36:31 +00003831 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3832 // Extend the range of the previous entry.
3833 Prev.EndLabel = Site.EndLabel;
3834 continue;
3835 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003836 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003837
Duncan Sands4ff179f2007-12-19 07:36:31 +00003838 // Otherwise, create a new call-site.
3839 CallSites.push_back(Site);
3840 PreviousIsInvoke = true;
3841 } else {
3842 // Create a gap.
3843 PreviousIsInvoke = false;
3844 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003845 }
3846 }
3847 // If some instruction between the previous try-range and the end of the
3848 // function may throw, create a call-site entry with no landing pad for the
3849 // region following the try-range.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003850 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003851 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3852 CallSites.push_back(Site);
3853 }
3854
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003855 // Final tallies.
Duncan Sands96144f92008-05-07 19:11:09 +00003856
3857 // Call sites.
3858 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
3859 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
3860 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
3861 unsigned SizeSites = CallSites.size() * (SiteStartSize +
3862 SiteLengthSize +
3863 LandingPadSize);
Duncan Sands241a0c92007-09-05 11:27:52 +00003864 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
aslc200b112008-08-16 12:57:46 +00003865 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands241a0c92007-09-05 11:27:52 +00003866
Duncan Sands96144f92008-05-07 19:11:09 +00003867 // Type infos.
3868 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3869 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003870
3871 unsigned TypeOffset = sizeof(int8_t) + // Call site format
aslc200b112008-08-16 12:57:46 +00003872 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003873 SizeSites + SizeActions + SizeTypes;
3874
3875 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3876 sizeof(int8_t) + // TType format
aslc200b112008-08-16 12:57:46 +00003877 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003878 TypeOffset;
3879
3880 unsigned SizeAlign = (4 - TotalSize) & 3;
3881
3882 // Begin the exception table.
3883 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng7e7d1942008-02-29 19:36:59 +00003884 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesen841e4982008-10-08 21:50:21 +00003885 O << "GCC_except_table" << SubprogramCount << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003886 for (unsigned i = 0; i != SizeAlign; ++i) {
3887 Asm->EmitInt8(0);
3888 Asm->EOL("Padding");
3889 }
3890 EmitLabel("exception", SubprogramCount);
3891
3892 // Emit the header.
3893 Asm->EmitInt8(DW_EH_PE_omit);
3894 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3895 Asm->EmitInt8(DW_EH_PE_absptr);
3896 Asm->EOL("TType format (DW_EH_PE_absptr)");
3897 Asm->EmitULEB128Bytes(TypeOffset);
3898 Asm->EOL("TType base offset");
3899 Asm->EmitInt8(DW_EH_PE_udata4);
3900 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3901 Asm->EmitULEB128Bytes(SizeSites);
3902 Asm->EOL("Call-site table length");
3903
Duncan Sands241a0c92007-09-05 11:27:52 +00003904 // Emit the landing pad site information.
3905 for (unsigned i = 0; i < CallSites.size(); ++i) {
3906 CallSiteEntry &S = CallSites[i];
3907 const char *BeginTag;
3908 unsigned BeginNumber;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003909
Duncan Sands241a0c92007-09-05 11:27:52 +00003910 if (!S.BeginLabel) {
3911 BeginTag = "eh_func_begin";
3912 BeginNumber = SubprogramCount;
3913 } else {
3914 BeginTag = "label";
3915 BeginNumber = S.BeginLabel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003916 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003917
Duncan Sands241a0c92007-09-05 11:27:52 +00003918 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00003919 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003920 Asm->EOL("Region start");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003921
Duncan Sands241a0c92007-09-05 11:27:52 +00003922 if (!S.EndLabel) {
Dale Johannesen4670be42008-01-15 23:24:56 +00003923 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands96144f92008-05-07 19:11:09 +00003924 true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003925 } else {
Duncan Sands96144f92008-05-07 19:11:09 +00003926 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003927 }
3928 Asm->EOL("Region length");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003929
Duncan Sands96144f92008-05-07 19:11:09 +00003930 if (!S.PadLabel)
3931 Asm->EmitInt32(0);
3932 else
Duncan Sands241a0c92007-09-05 11:27:52 +00003933 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00003934 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003935 Asm->EOL("Landing pad");
3936
3937 Asm->EmitULEB128Bytes(S.Action);
3938 Asm->EOL("Action");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003939 }
3940
3941 // Emit the actions.
3942 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
3943 ActionEntry &Action = Actions[I];
3944
3945 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
3946 Asm->EOL("TypeInfo index");
3947 Asm->EmitSLEB128Bytes(Action.NextAction);
3948 Asm->EOL("Next action");
3949 }
3950
3951 // Emit the type ids.
3952 for (unsigned M = TypeInfos.size(); M; --M) {
3953 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00003954
3955 PrintRelDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003956
3957 if (GV)
3958 O << Asm->getGlobalLinkName(GV);
3959 else
3960 O << "0";
Duncan Sands241a0c92007-09-05 11:27:52 +00003961
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003962 Asm->EOL("TypeInfo");
3963 }
3964
3965 // Emit the filter typeids.
3966 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
3967 unsigned TypeID = FilterIds[j];
3968 Asm->EmitULEB128Bytes(TypeID);
3969 Asm->EOL("Filter TypeInfo index");
3970 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003971
Evan Cheng7e7d1942008-02-29 19:36:59 +00003972 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003973 }
3974
3975public:
3976 //===--------------------------------------------------------------------===//
3977 // Main entry points.
3978 //
Owen Anderson847b99b2008-08-21 00:14:44 +00003979 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattnerb3876c72007-09-24 03:35:37 +00003980 : Dwarf(OS, A, T, "eh")
Dale Johannesen85535762008-04-02 00:25:04 +00003981 , shouldEmitTable(false)
3982 , shouldEmitMoves(false)
3983 , shouldEmitTableModule(false)
3984 , shouldEmitMovesModule(false)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003985 {}
aslc200b112008-08-16 12:57:46 +00003986
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003987 virtual ~DwarfException() {}
3988
3989 /// SetModuleInfo - Set machine module information when it's known that pass
3990 /// manager has created it. Set by the target AsmPrinter.
3991 void SetModuleInfo(MachineModuleInfo *mmi) {
3992 MMI = mmi;
3993 }
3994
3995 /// BeginModule - Emit all exception information that should come prior to the
3996 /// content.
3997 void BeginModule(Module *M) {
3998 this->M = M;
3999 }
4000
4001 /// EndModule - Emit all exception information that should come after the
4002 /// content.
4003 void EndModule() {
Dale Johannesen85535762008-04-02 00:25:04 +00004004 if (shouldEmitMovesModule || shouldEmitTableModule) {
4005 const std::vector<Function *> Personalities = MMI->getPersonalities();
Evan Cheng3e288912009-02-25 07:04:34 +00004006 for (unsigned i = 0; i < Personalities.size(); ++i)
Dale Johannesen85535762008-04-02 00:25:04 +00004007 EmitCommonEHFrame(Personalities[i], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004008
Dale Johannesen85535762008-04-02 00:25:04 +00004009 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4010 E = EHFrames.end(); I != E; ++I)
4011 EmitEHFrame(*I);
4012 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004013 }
4014
aslc200b112008-08-16 12:57:46 +00004015 /// BeginFunction - Gather pre-function exception information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004016 /// emitted immediately after the function entry point.
4017 void BeginFunction(MachineFunction *MF) {
4018 this->MF = MF;
Dale Johannesen85535762008-04-02 00:25:04 +00004019 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen62f0a6d2008-04-02 17:04:45 +00004020 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen85535762008-04-02 00:25:04 +00004021
4022 // Map all labels and get rid of any dead landing pads.
4023 MMI->TidyLandingPads();
4024 // If any landing pads survive, we need an EH table.
4025 if (MMI->getLandingPads().size())
4026 shouldEmitTable = true;
4027
4028 // See if we need frame move info.
Duncan Sandscbc28b12008-07-04 09:55:48 +00004029 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen85535762008-04-02 00:25:04 +00004030 shouldEmitMoves = true;
4031
4032 if (shouldEmitMoves || shouldEmitTable)
4033 // Assumes in correct section after the entry point.
4034 EmitLabel("eh_func_begin", ++SubprogramCount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004035 }
Dale Johannesen85535762008-04-02 00:25:04 +00004036 shouldEmitTableModule |= shouldEmitTable;
4037 shouldEmitMovesModule |= shouldEmitMoves;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004038 }
4039
4040 /// EndFunction - Gather and emit post-function exception information.
4041 ///
4042 void EndFunction() {
Dale Johannesen85535762008-04-02 00:25:04 +00004043 if (shouldEmitMoves || shouldEmitTable) {
4044 EmitLabel("eh_func_end", SubprogramCount);
4045 EmitExceptionTable();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004046
Dale Johannesen85535762008-04-02 00:25:04 +00004047 // Save EH frame information
4048 EHFrames.
4049 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendlingef9211a2007-09-18 01:47:22 +00004050 SubprogramCount,
4051 MMI->getPersonalityIndex(),
4052 MF->getFrameInfo()->hasCalls(),
4053 !MMI->getLandingPads().empty(),
Dale Johannesenfb3ac732007-11-20 23:24:42 +00004054 MMI->getFrameMoves(),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00004055 MF->getFunction()));
Dale Johannesen85535762008-04-02 00:25:04 +00004056 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004057 }
4058};
4059
4060} // End of namespace llvm
4061
4062//===----------------------------------------------------------------------===//
4063
4064/// Emit - Print the abbreviation using the specified Dwarf writer.
4065///
4066void DIEAbbrev::Emit(const DwarfDebug &DD) const {
4067 // Emit its Dwarf tag type.
4068 DD.getAsm()->EmitULEB128Bytes(Tag);
4069 DD.getAsm()->EOL(TagString(Tag));
aslc200b112008-08-16 12:57:46 +00004070
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004071 // Emit whether it has children DIEs.
4072 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4073 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
aslc200b112008-08-16 12:57:46 +00004074
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004075 // For each attribute description.
4076 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4077 const DIEAbbrevData &AttrData = Data[i];
aslc200b112008-08-16 12:57:46 +00004078
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004079 // Emit attribute type.
4080 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4081 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
aslc200b112008-08-16 12:57:46 +00004082
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004083 // Emit form type.
4084 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4085 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
4086 }
4087
4088 // Mark end of abbreviation.
4089 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4090 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
4091}
4092
4093#ifndef NDEBUG
4094void DIEAbbrev::print(std::ostream &O) {
4095 O << "Abbreviation @"
4096 << std::hex << (intptr_t)this << std::dec
4097 << " "
4098 << TagString(Tag)
4099 << " "
4100 << ChildrenString(ChildrenFlag)
4101 << "\n";
aslc200b112008-08-16 12:57:46 +00004102
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004103 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4104 O << " "
4105 << AttributeString(Data[i].getAttribute())
4106 << " "
4107 << FormEncodingString(Data[i].getForm())
4108 << "\n";
4109 }
4110}
4111void DIEAbbrev::dump() { print(cerr); }
4112#endif
4113
4114//===----------------------------------------------------------------------===//
4115
4116#ifndef NDEBUG
4117void DIEValue::dump() {
4118 print(cerr);
4119}
4120#endif
4121
4122//===----------------------------------------------------------------------===//
4123
4124/// EmitValue - Emit integer of appropriate size.
4125///
4126void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
4127 switch (Form) {
4128 case DW_FORM_flag: // Fall thru
4129 case DW_FORM_ref1: // Fall thru
4130 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
4131 case DW_FORM_ref2: // Fall thru
4132 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
4133 case DW_FORM_ref4: // Fall thru
4134 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
4135 case DW_FORM_ref8: // Fall thru
4136 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4137 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4138 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
4139 default: assert(0 && "DIE Value form not supported yet"); break;
4140 }
4141}
4142
4143/// SizeOf - Determine size of integer value in bytes.
4144///
4145unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4146 switch (Form) {
4147 case DW_FORM_flag: // Fall thru
4148 case DW_FORM_ref1: // Fall thru
4149 case DW_FORM_data1: return sizeof(int8_t);
4150 case DW_FORM_ref2: // Fall thru
4151 case DW_FORM_data2: return sizeof(int16_t);
4152 case DW_FORM_ref4: // Fall thru
4153 case DW_FORM_data4: return sizeof(int32_t);
4154 case DW_FORM_ref8: // Fall thru
4155 case DW_FORM_data8: return sizeof(int64_t);
aslc200b112008-08-16 12:57:46 +00004156 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4157 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004158 default: assert(0 && "DIE Value form not supported yet"); break;
4159 }
4160 return 0;
4161}
4162
4163//===----------------------------------------------------------------------===//
4164
4165/// EmitValue - Emit string value.
4166///
4167void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
4168 DD.getAsm()->EmitString(String);
4169}
4170
4171//===----------------------------------------------------------------------===//
4172
4173/// EmitValue - Emit label value.
4174///
4175void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004176 bool IsSmall = Form == DW_FORM_data4;
4177 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004178}
4179
4180/// SizeOf - Determine size of label value in bytes.
4181///
4182unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004183 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004184 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004185}
4186
4187//===----------------------------------------------------------------------===//
4188
4189/// EmitValue - Emit label value.
4190///
4191void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004192 bool IsSmall = Form == DW_FORM_data4;
4193 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004194}
4195
4196/// SizeOf - Determine size of label value in bytes.
4197///
4198unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004199 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004200 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004201}
aslc200b112008-08-16 12:57:46 +00004202
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004203//===----------------------------------------------------------------------===//
4204
4205/// EmitValue - Emit delta value.
4206///
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004207void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4208 bool IsSmall = Form == DW_FORM_data4;
4209 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4210 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4211}
4212
4213/// SizeOf - Determine size of delta value in bytes.
4214///
4215unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4216 if (Form == DW_FORM_data4) return 4;
4217 return DD.getTargetData()->getPointerSize();
4218}
aslc200b112008-08-16 12:57:46 +00004219
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004220//===----------------------------------------------------------------------===//
4221
4222/// EmitValue - Emit delta value.
4223///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004224void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
4225 bool IsSmall = Form == DW_FORM_data4;
4226 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
4227}
4228
4229/// SizeOf - Determine size of delta value in bytes.
4230///
4231unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4232 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004233 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004234}
4235
4236//===----------------------------------------------------------------------===//
4237
4238/// EmitValue - Emit debug information entry offset.
4239///
4240void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
4241 DD.getAsm()->EmitInt32(Entry->getOffset());
4242}
aslc200b112008-08-16 12:57:46 +00004243
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004244//===----------------------------------------------------------------------===//
4245
4246/// ComputeSize - calculate the size of the block.
4247///
4248unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
4249 if (!Size) {
Owen Anderson88dd6232008-06-24 21:44:59 +00004250 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004251
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004252 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4253 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
4254 }
4255 }
4256 return Size;
4257}
4258
4259/// EmitValue - Emit block data.
4260///
4261void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
4262 switch (Form) {
4263 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4264 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4265 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4266 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
4267 default: assert(0 && "Improper form for block"); break;
4268 }
aslc200b112008-08-16 12:57:46 +00004269
Owen Anderson88dd6232008-06-24 21:44:59 +00004270 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004271
4272 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4273 DD.getAsm()->EOL();
4274 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
4275 }
4276}
4277
4278/// SizeOf - Determine size of block data in bytes.
4279///
4280unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4281 switch (Form) {
4282 case DW_FORM_block1: return Size + sizeof(int8_t);
4283 case DW_FORM_block2: return Size + sizeof(int16_t);
4284 case DW_FORM_block4: return Size + sizeof(int32_t);
aslc200b112008-08-16 12:57:46 +00004285 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004286 default: assert(0 && "Improper form for block"); break;
4287 }
4288 return 0;
4289}
4290
4291//===----------------------------------------------------------------------===//
4292/// DIE Implementation
4293
4294DIE::~DIE() {
4295 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4296 delete Children[i];
4297}
aslc200b112008-08-16 12:57:46 +00004298
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004299/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4300///
4301void DIE::AddSiblingOffset() {
4302 DIEInteger *DI = new DIEInteger(0);
4303 Values.insert(Values.begin(), DI);
4304 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
4305}
4306
4307/// Profile - Used to gather unique data for the value folding set.
4308///
4309void DIE::Profile(FoldingSetNodeID &ID) {
4310 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00004311
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004312 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4313 ID.AddPointer(Children[i]);
4314
4315 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4316 ID.AddPointer(Values[j]);
4317}
4318
4319#ifndef NDEBUG
4320void DIE::print(std::ostream &O, unsigned IncIndent) {
4321 static unsigned IndentCount = 0;
4322 IndentCount += IncIndent;
4323 const std::string Indent(IndentCount, ' ');
4324 bool isBlock = Abbrev.getTag() == 0;
aslc200b112008-08-16 12:57:46 +00004325
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004326 if (!isBlock) {
4327 O << Indent
4328 << "Die: "
4329 << "0x" << std::hex << (intptr_t)this << std::dec
4330 << ", Offset: " << Offset
4331 << ", Size: " << Size
aslc200b112008-08-16 12:57:46 +00004332 << "\n";
4333
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004334 O << Indent
4335 << TagString(Abbrev.getTag())
4336 << " "
4337 << ChildrenString(Abbrev.getChildrenFlag());
4338 } else {
4339 O << "Size: " << Size;
4340 }
4341 O << "\n";
4342
Owen Anderson88dd6232008-06-24 21:44:59 +00004343 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004344
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004345 IndentCount += 2;
4346 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4347 O << Indent;
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004348
4349 if (!isBlock)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004350 O << AttributeString(Data[i].getAttribute());
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004351 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004352 O << "Blk[" << i << "]";
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004353
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004354 O << " "
4355 << FormEncodingString(Data[i].getForm())
4356 << " ";
4357 Values[i]->print(O);
4358 O << "\n";
4359 }
4360 IndentCount -= 2;
4361
4362 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4363 Children[j]->print(O, 4);
4364 }
aslc200b112008-08-16 12:57:46 +00004365
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004366 if (!isBlock) O << "\n";
4367 IndentCount -= IncIndent;
4368}
4369
4370void DIE::dump() {
4371 print(cerr);
4372}
4373#endif
4374
4375//===----------------------------------------------------------------------===//
4376/// DwarfWriter Implementation
4377///
4378
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004379DwarfWriter::DwarfWriter()
4380 : ImmutablePass(&ID), DD(0), DE(0), DwarfTimer(0) {
4381 if (TimePassesIsEnabled)
4382 DwarfTimer = new Timer("Dwarf Writer", *getDwarfTimerGroup());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004383}
4384
4385DwarfWriter::~DwarfWriter() {
4386 delete DE;
4387 delete DD;
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004388 delete DwarfTimer;
4389 delete DwarfTimerGroup; DwarfTimerGroup = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004390}
4391
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004392/// BeginModule - Emit all Dwarf sections that should come prior to the
4393/// content.
Devang Patelaa1e8432009-01-08 23:40:34 +00004394void DwarfWriter::BeginModule(Module *M,
4395 MachineModuleInfo *MMI,
4396 raw_ostream &OS, AsmPrinter *A,
4397 const TargetAsmInfo *T) {
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004398 if (TimePassesIsEnabled)
4399 DwarfTimer->startTimer();
4400
Devang Patelaa1e8432009-01-08 23:40:34 +00004401 DE = new DwarfException(OS, A, T);
4402 DD = new DwarfDebug(OS, A, T);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004403 DE->BeginModule(M);
4404 DD->BeginModule(M);
Devang Patel6ccd57e2009-01-13 00:20:51 +00004405 DD->SetDebugInfo(MMI);
Devang Patelaa1e8432009-01-08 23:40:34 +00004406 DE->SetModuleInfo(MMI);
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004407
4408 if (TimePassesIsEnabled)
4409 DwarfTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004410}
4411
4412/// EndModule - Emit all Dwarf sections that should come after the content.
4413///
4414void DwarfWriter::EndModule() {
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004415 if (TimePassesIsEnabled)
4416 DwarfTimer->startTimer();
4417
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004418 DE->EndModule();
4419 DD->EndModule();
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004420
4421 if (TimePassesIsEnabled)
4422 DwarfTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004423}
4424
aslc200b112008-08-16 12:57:46 +00004425/// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004426/// emitted immediately after the function entry point.
4427void DwarfWriter::BeginFunction(MachineFunction *MF) {
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004428 if (TimePassesIsEnabled)
4429 DwarfTimer->startTimer();
4430
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004431 DE->BeginFunction(MF);
4432 DD->BeginFunction(MF);
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004433
4434 if (TimePassesIsEnabled)
4435 DwarfTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004436}
4437
4438/// EndFunction - Gather and emit post-function debug information.
4439///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00004440void DwarfWriter::EndFunction(MachineFunction *MF) {
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004441 if (TimePassesIsEnabled)
4442 DwarfTimer->startTimer();
4443
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00004444 DD->EndFunction(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004445 DE->EndFunction();
aslc200b112008-08-16 12:57:46 +00004446
Bill Wendling5b4796a2008-07-22 00:53:37 +00004447 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004448 // Clear function debug information.
4449 MMI->EndFunction();
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004450
4451 if (TimePassesIsEnabled)
4452 DwarfTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004453}
Devang Patelcb59fd42009-01-12 19:17:34 +00004454
Devang Patel2da0cc42009-01-15 23:41:32 +00004455/// ValidDebugInfo - Return true if V represents valid debug info value.
4456bool DwarfWriter::ValidDebugInfo(Value *V) {
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004457 if (TimePassesIsEnabled)
4458 DwarfTimer->startTimer();
4459
4460 bool Res = DD && DD->ValidDebugInfo(V);
4461
4462 if (TimePassesIsEnabled)
4463 DwarfTimer->stopTimer();
4464
4465 return Res;
Devang Patel2da0cc42009-01-15 23:41:32 +00004466}
4467
Devang Patelcb59fd42009-01-12 19:17:34 +00004468/// RecordSourceLine - Records location information and associates it with a
4469/// label. Returns a unique label ID used to generate a label and provide
4470/// correspondence to the source line list.
4471unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
4472 unsigned Src) {
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004473 if (TimePassesIsEnabled)
4474 DwarfTimer->startTimer();
4475
4476 unsigned Res = DD->RecordSourceLine(Line, Col, Src);
4477
4478 if (TimePassesIsEnabled)
4479 DwarfTimer->stopTimer();
4480
4481 return Res;
Devang Patelcb59fd42009-01-12 19:17:34 +00004482}
4483
Evan Cheng3e288912009-02-25 07:04:34 +00004484/// getOrCreateSourceID - Look up the source id with the given directory and
4485/// source file names. If none currently exists, create a new id and insert it
4486/// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
4487/// as well.
4488unsigned DwarfWriter::getOrCreateSourceID(const std::string &DirName,
4489 const std::string &FileName) {
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004490 if (TimePassesIsEnabled)
4491 DwarfTimer->startTimer();
4492
4493 unsigned Res = DD->getOrCreateSourceID(DirName, FileName);
4494
4495 if (TimePassesIsEnabled)
4496 DwarfTimer->stopTimer();
4497
4498 return Res;
Devang Patelcb59fd42009-01-12 19:17:34 +00004499}
4500
4501/// RecordRegionStart - Indicate the start of a region.
4502unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004503 if (TimePassesIsEnabled)
4504 DwarfTimer->startTimer();
4505
4506 unsigned Res = DD->RecordRegionStart(V);
4507
4508 if (TimePassesIsEnabled)
4509 DwarfTimer->stopTimer();
4510
4511 return Res;
Devang Patelcb59fd42009-01-12 19:17:34 +00004512}
4513
4514/// RecordRegionEnd - Indicate the end of a region.
4515unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004516 if (TimePassesIsEnabled)
4517 DwarfTimer->startTimer();
4518
4519 unsigned Res = DD->RecordRegionEnd(V);
4520
4521 if (TimePassesIsEnabled)
4522 DwarfTimer->stopTimer();
4523
4524 return Res;
Devang Patelcb59fd42009-01-12 19:17:34 +00004525}
4526
4527/// getRecordSourceLineCount - Count source lines.
4528unsigned DwarfWriter::getRecordSourceLineCount() {
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004529 if (TimePassesIsEnabled)
4530 DwarfTimer->startTimer();
4531
4532 unsigned Res = DD->getRecordSourceLineCount();
4533
4534 if (TimePassesIsEnabled)
4535 DwarfTimer->stopTimer();
4536
4537 return Res;
Devang Patelcb59fd42009-01-12 19:17:34 +00004538}
Devang Patel70190872009-01-13 21:25:00 +00004539
Devang Patelfe359e72009-01-13 21:44:10 +00004540/// RecordVariable - Indicate the declaration of a local variable.
4541///
4542void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004543 if (TimePassesIsEnabled)
4544 DwarfTimer->startTimer();
4545
Devang Patelfe359e72009-01-13 21:44:10 +00004546 DD->RecordVariable(GV, FrameIndex);
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004547
4548 if (TimePassesIsEnabled)
4549 DwarfTimer->stopTimer();
Devang Patelfe359e72009-01-13 21:44:10 +00004550}
Devang Patel42f6bed2009-01-13 23:54:55 +00004551
Bill Wendling50db0792009-02-20 00:44:43 +00004552/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
4553/// be emitted.
4554bool DwarfWriter::ShouldEmitDwarfDebug() const {
Bill Wendlingcb3661f2009-03-10 20:41:52 +00004555 if (TimePassesIsEnabled)
4556 DwarfTimer->startTimer();
4557
4558 bool Res = DD->ShouldEmitDwarfDebug();
4559
4560 if (TimePassesIsEnabled)
4561 DwarfTimer->stopTimer();
4562
4563 return Res;
Bill Wendling50db0792009-02-20 00:44:43 +00004564}