blob: c5b24eff452ead566d6c21f5c177b8b126f9b18d [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"
Owen Anderson847b99b2008-08-21 00:14:44 +000028#include "llvm/Support/raw_ostream.h"
Dan Gohman80bbde72007-09-24 21:32:18 +000029#include "llvm/System/Path.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000031#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/Target/TargetData.h"
33#include "llvm/Target/TargetFrameInfo.h"
34#include "llvm/Target/TargetInstrInfo.h"
35#include "llvm/Target/TargetMachine.h"
36#include "llvm/Target/TargetOptions.h"
Evan Cheng3e288912009-02-25 07:04:34 +000037#include "llvm/ADT/DenseMap.h"
38#include "llvm/ADT/FoldingSet.h"
39#include "llvm/ADT/StringExtras.h"
40#include "llvm/ADT/StringMap.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041#include <ostream>
42#include <string>
43using namespace llvm;
44using namespace llvm::dwarf;
45
Devang Patelaa1e8432009-01-08 23:40:34 +000046static RegisterPass<DwarfWriter>
47X("dwarfwriter", "DWARF Information Writer");
48char DwarfWriter::ID = 0;
49
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050namespace llvm {
aslc200b112008-08-16 12:57:46 +000051
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052//===----------------------------------------------------------------------===//
53
54/// Configuration values for initial hash set sizes (log2).
55///
Bill Wendling824a8bf2009-02-03 21:17:20 +000056static const unsigned InitDiesSetSize = 9; // log2(512)
57static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
58static const unsigned InitValuesSetSize = 9; // log2(512)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059
60//===----------------------------------------------------------------------===//
61/// Forward declarations.
62///
63class DIE;
64class DIEValue;
65
66//===----------------------------------------------------------------------===//
Devang Patelb3907da2009-01-05 23:03:32 +000067/// Utility routines.
68///
Devang Patel2da0cc42009-01-15 23:41:32 +000069/// getGlobalVariable - Return either a direct or cast Global value.
70///
71static GlobalVariable *getGlobalVariable(Value *V) {
72 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
73 return GV;
74 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
75 if (CE->getOpcode() == Instruction::BitCast) {
76 return dyn_cast<GlobalVariable>(CE->getOperand(0));
77 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
78 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
79 if (!CE->getOperand(i)->isNullValue())
80 return NULL;
81 }
82 return dyn_cast<GlobalVariable>(CE->getOperand(0));
83 }
84 }
85 return NULL;
86}
87
Devang Patelb3907da2009-01-05 23:03:32 +000088//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089/// DWLabel - Labels are used to track locations in the assembler file.
aslc200b112008-08-16 12:57:46 +000090/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
91/// where the tag is a category of label (Ex. location) and number is a value
Reid Spencer37c7cea2007-08-05 20:06:04 +000092/// unique in that category.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093class DWLabel {
94public:
95 /// Tag - Label category tag. Should always be a staticly declared C string.
96 ///
97 const char *Tag;
aslc200b112008-08-16 12:57:46 +000098
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 /// Number - Value to make label unique.
100 ///
101 unsigned Number;
102
103 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
aslc200b112008-08-16 12:57:46 +0000104
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 void Profile(FoldingSetNodeID &ID) const {
Evan Cheng3e288912009-02-25 07:04:34 +0000106 ID.AddString(Tag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 ID.AddInteger(Number);
108 }
aslc200b112008-08-16 12:57:46 +0000109
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110#ifndef NDEBUG
111 void print(std::ostream *O) const {
112 if (O) print(*O);
113 }
114 void print(std::ostream &O) const {
115 O << "." << Tag;
116 if (Number) O << Number;
117 }
118#endif
119};
120
121//===----------------------------------------------------------------------===//
122/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
123/// Dwarf abbreviation.
124class DIEAbbrevData {
125private:
126 /// Attribute - Dwarf attribute code.
127 ///
128 unsigned Attribute;
aslc200b112008-08-16 12:57:46 +0000129
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 /// Form - Dwarf form code.
aslc200b112008-08-16 12:57:46 +0000131 ///
132 unsigned Form;
133
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134public:
135 DIEAbbrevData(unsigned A, unsigned F)
136 : Attribute(A)
137 , Form(F)
138 {}
aslc200b112008-08-16 12:57:46 +0000139
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 // Accessors.
141 unsigned getAttribute() const { return Attribute; }
142 unsigned getForm() const { return Form; }
143
144 /// Profile - Used to gather unique data for the abbreviation folding set.
145 ///
146 void Profile(FoldingSetNodeID &ID)const {
147 ID.AddInteger(Attribute);
148 ID.AddInteger(Form);
149 }
150};
151
152//===----------------------------------------------------------------------===//
153/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
154/// information object.
155class DIEAbbrev : public FoldingSetNode {
156private:
157 /// Tag - Dwarf tag code.
158 ///
159 unsigned Tag;
aslc200b112008-08-16 12:57:46 +0000160
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 /// Unique number for node.
162 ///
163 unsigned Number;
164
165 /// ChildrenFlag - Dwarf children flag.
166 ///
167 unsigned ChildrenFlag;
168
169 /// Data - Raw data bytes for abbreviation.
170 ///
Owen Anderson88dd6232008-06-24 21:44:59 +0000171 SmallVector<DIEAbbrevData, 8> Data;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172
173public:
174
175 DIEAbbrev(unsigned T, unsigned C)
176 : Tag(T)
177 , ChildrenFlag(C)
178 , Data()
179 {}
180 ~DIEAbbrev() {}
aslc200b112008-08-16 12:57:46 +0000181
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 // Accessors.
183 unsigned getTag() const { return Tag; }
184 unsigned getNumber() const { return Number; }
185 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson88dd6232008-06-24 21:44:59 +0000186 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 void setTag(unsigned T) { Tag = T; }
188 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
189 void setNumber(unsigned N) { Number = N; }
aslc200b112008-08-16 12:57:46 +0000190
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 /// AddAttribute - Adds another set of attribute information to the
192 /// abbreviation.
193 void AddAttribute(unsigned Attribute, unsigned Form) {
194 Data.push_back(DIEAbbrevData(Attribute, Form));
195 }
aslc200b112008-08-16 12:57:46 +0000196
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 /// AddFirstAttribute - Adds a set of attribute information to the front
198 /// of the abbreviation.
199 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
200 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
201 }
aslc200b112008-08-16 12:57:46 +0000202
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 /// Profile - Used to gather unique data for the abbreviation folding set.
204 ///
205 void Profile(FoldingSetNodeID &ID) {
206 ID.AddInteger(Tag);
207 ID.AddInteger(ChildrenFlag);
aslc200b112008-08-16 12:57:46 +0000208
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 // For each attribute description.
210 for (unsigned i = 0, N = Data.size(); i < N; ++i)
211 Data[i].Profile(ID);
212 }
aslc200b112008-08-16 12:57:46 +0000213
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 /// Emit - Print the abbreviation using the specified Dwarf writer.
215 ///
aslc200b112008-08-16 12:57:46 +0000216 void Emit(const DwarfDebug &DD) const;
217
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218#ifndef NDEBUG
219 void print(std::ostream *O) {
220 if (O) print(*O);
221 }
222 void print(std::ostream &O);
223 void dump();
224#endif
225};
226
227//===----------------------------------------------------------------------===//
228/// DIE - A structured debug information entry. Has an abbreviation which
229/// describes it's organization.
230class DIE : public FoldingSetNode {
231protected:
232 /// Abbrev - Buffer for constructing abbreviation.
233 ///
234 DIEAbbrev Abbrev;
aslc200b112008-08-16 12:57:46 +0000235
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 /// Offset - Offset in debug info section.
237 ///
238 unsigned Offset;
aslc200b112008-08-16 12:57:46 +0000239
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 /// Size - Size of instance + children.
241 ///
242 unsigned Size;
aslc200b112008-08-16 12:57:46 +0000243
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 /// Children DIEs.
245 ///
246 std::vector<DIE *> Children;
aslc200b112008-08-16 12:57:46 +0000247
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 /// Attributes values.
249 ///
Owen Anderson88dd6232008-06-24 21:44:59 +0000250 SmallVector<DIEValue*, 32> Values;
aslc200b112008-08-16 12:57:46 +0000251
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000253 explicit DIE(unsigned Tag)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 : Abbrev(Tag, DW_CHILDREN_no)
255 , Offset(0)
256 , Size(0)
257 , Children()
258 , Values()
259 {}
260 virtual ~DIE();
aslc200b112008-08-16 12:57:46 +0000261
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 // Accessors.
263 DIEAbbrev &getAbbrev() { return Abbrev; }
264 unsigned getAbbrevNumber() const {
265 return Abbrev.getNumber();
266 }
267 unsigned getTag() const { return Abbrev.getTag(); }
268 unsigned getOffset() const { return Offset; }
269 unsigned getSize() const { return Size; }
270 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson88dd6232008-06-24 21:44:59 +0000271 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
273 void setOffset(unsigned O) { Offset = O; }
274 void setSize(unsigned S) { Size = S; }
aslc200b112008-08-16 12:57:46 +0000275
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 /// AddValue - Add a value and attributes to a DIE.
277 ///
278 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
279 Abbrev.AddAttribute(Attribute, Form);
280 Values.push_back(Value);
281 }
aslc200b112008-08-16 12:57:46 +0000282
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 /// SiblingOffset - Return the offset of the debug information entry's
284 /// sibling.
285 unsigned SiblingOffset() const { return Offset + Size; }
aslc200b112008-08-16 12:57:46 +0000286
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
288 ///
289 void AddSiblingOffset();
290
291 /// AddChild - Add a child to the DIE.
292 ///
293 void AddChild(DIE *Child) {
294 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
295 Children.push_back(Child);
296 }
aslc200b112008-08-16 12:57:46 +0000297
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 /// Detach - Detaches objects connected to it after copying.
299 ///
300 void Detach() {
301 Children.clear();
302 }
aslc200b112008-08-16 12:57:46 +0000303
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304 /// Profile - Used to gather unique data for the value folding set.
305 ///
306 void Profile(FoldingSetNodeID &ID) ;
aslc200b112008-08-16 12:57:46 +0000307
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308#ifndef NDEBUG
309 void print(std::ostream *O, unsigned IncIndent = 0) {
310 if (O) print(*O, IncIndent);
311 }
312 void print(std::ostream &O, unsigned IncIndent = 0);
313 void dump();
314#endif
315};
316
317//===----------------------------------------------------------------------===//
318/// DIEValue - A debug information entry value.
319///
320class DIEValue : public FoldingSetNode {
321public:
322 enum {
323 isInteger,
324 isString,
325 isLabel,
326 isAsIsLabel,
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000327 isSectionOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 isDelta,
329 isEntry,
330 isBlock
331 };
aslc200b112008-08-16 12:57:46 +0000332
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 /// Type - Type of data stored in the value.
334 ///
335 unsigned Type;
aslc200b112008-08-16 12:57:46 +0000336
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000337 explicit DIEValue(unsigned T)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338 : Type(T)
339 {}
340 virtual ~DIEValue() {}
aslc200b112008-08-16 12:57:46 +0000341
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000342 // Accessors
343 unsigned getType() const { return Type; }
aslc200b112008-08-16 12:57:46 +0000344
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 // Implement isa/cast/dyncast.
346 static bool classof(const DIEValue *) { return true; }
aslc200b112008-08-16 12:57:46 +0000347
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348 /// EmitValue - Emit value via the Dwarf writer.
349 ///
350 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
aslc200b112008-08-16 12:57:46 +0000351
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352 /// SizeOf - Return the size of a value in bytes.
353 ///
354 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
aslc200b112008-08-16 12:57:46 +0000355
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356 /// Profile - Used to gather unique data for the value folding set.
357 ///
358 virtual void Profile(FoldingSetNodeID &ID) = 0;
aslc200b112008-08-16 12:57:46 +0000359
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360#ifndef NDEBUG
361 void print(std::ostream *O) {
362 if (O) print(*O);
363 }
364 virtual void print(std::ostream &O) = 0;
365 void dump();
366#endif
367};
368
369//===----------------------------------------------------------------------===//
370/// DWInteger - An integer value DIE.
aslc200b112008-08-16 12:57:46 +0000371///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372class DIEInteger : public DIEValue {
373private:
374 uint64_t Integer;
aslc200b112008-08-16 12:57:46 +0000375
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000377 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378
379 // Implement isa/cast/dyncast.
380 static bool classof(const DIEInteger *) { return true; }
381 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
aslc200b112008-08-16 12:57:46 +0000382
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 /// BestForm - Choose the best form for integer.
384 ///
385 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
386 if (IsSigned) {
387 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
388 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
389 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
390 } else {
391 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
392 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
393 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
394 }
395 return DW_FORM_data8;
396 }
aslc200b112008-08-16 12:57:46 +0000397
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 /// EmitValue - Emit integer of appropriate size.
399 ///
400 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000401
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 /// SizeOf - Determine size of integer value in bytes.
403 ///
404 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000405
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000406 /// Profile - Used to gather unique data for the value folding set.
407 ///
408 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
409 ID.AddInteger(isInteger);
410 ID.AddInteger(Integer);
411 }
412 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
aslc200b112008-08-16 12:57:46 +0000413
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414#ifndef NDEBUG
415 virtual void print(std::ostream &O) {
416 O << "Int: " << (int64_t)Integer
417 << " 0x" << std::hex << Integer << std::dec;
418 }
419#endif
420};
421
422//===----------------------------------------------------------------------===//
423/// DIEString - A string value DIE.
aslc200b112008-08-16 12:57:46 +0000424///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425class DIEString : public DIEValue {
426public:
427 const std::string String;
aslc200b112008-08-16 12:57:46 +0000428
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000429 explicit DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430
431 // Implement isa/cast/dyncast.
432 static bool classof(const DIEString *) { return true; }
433 static bool classof(const DIEValue *S) { return S->Type == isString; }
aslc200b112008-08-16 12:57:46 +0000434
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000435 /// EmitValue - Emit string value.
436 ///
437 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000438
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000439 /// SizeOf - Determine size of string value in bytes.
440 ///
441 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
442 return String.size() + sizeof(char); // sizeof('\0');
443 }
aslc200b112008-08-16 12:57:46 +0000444
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445 /// Profile - Used to gather unique data for the value folding set.
446 ///
447 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
448 ID.AddInteger(isString);
449 ID.AddString(String);
450 }
451 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
aslc200b112008-08-16 12:57:46 +0000452
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453#ifndef NDEBUG
454 virtual void print(std::ostream &O) {
455 O << "Str: \"" << String << "\"";
456 }
457#endif
458};
459
460//===----------------------------------------------------------------------===//
461/// DIEDwarfLabel - A Dwarf internal label expression DIE.
462//
463class DIEDwarfLabel : public DIEValue {
464public:
465
466 const DWLabel Label;
aslc200b112008-08-16 12:57:46 +0000467
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000468 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000469
470 // Implement isa/cast/dyncast.
471 static bool classof(const DIEDwarfLabel *) { return true; }
472 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
aslc200b112008-08-16 12:57:46 +0000473
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000474 /// EmitValue - Emit label value.
475 ///
476 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000477
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 /// SizeOf - Determine size of label value in bytes.
479 ///
480 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000481
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000482 /// Profile - Used to gather unique data for the value folding set.
483 ///
484 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
485 ID.AddInteger(isLabel);
486 Label.Profile(ID);
487 }
488 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
aslc200b112008-08-16 12:57:46 +0000489
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490#ifndef NDEBUG
491 virtual void print(std::ostream &O) {
492 O << "Lbl: ";
493 Label.print(O);
494 }
495#endif
496};
497
498
499//===----------------------------------------------------------------------===//
500/// DIEObjectLabel - A label to an object in code or data.
501//
502class DIEObjectLabel : public DIEValue {
503public:
504 const std::string Label;
aslc200b112008-08-16 12:57:46 +0000505
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000506 explicit DIEObjectLabel(const std::string &L)
507 : DIEValue(isAsIsLabel), Label(L) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000508
509 // Implement isa/cast/dyncast.
510 static bool classof(const DIEObjectLabel *) { return true; }
511 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
aslc200b112008-08-16 12:57:46 +0000512
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513 /// EmitValue - Emit label value.
514 ///
515 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000516
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 /// SizeOf - Determine size of label value in bytes.
518 ///
519 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000520
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521 /// Profile - Used to gather unique data for the value folding set.
522 ///
523 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
524 ID.AddInteger(isAsIsLabel);
525 ID.AddString(Label);
526 }
Evan Cheng3e288912009-02-25 07:04:34 +0000527 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label.c_str()); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528
529#ifndef NDEBUG
530 virtual void print(std::ostream &O) {
531 O << "Obj: " << Label;
532 }
533#endif
534};
535
536//===----------------------------------------------------------------------===//
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000537/// DIESectionOffset - A section offset DIE.
538//
539class DIESectionOffset : public DIEValue {
540public:
541 const DWLabel Label;
542 const DWLabel Section;
543 bool IsEH : 1;
544 bool UseSet : 1;
aslc200b112008-08-16 12:57:46 +0000545
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000546 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
547 bool isEH = false, bool useSet = true)
548 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
549 IsEH(isEH), UseSet(useSet) {}
550
551 // Implement isa/cast/dyncast.
552 static bool classof(const DIESectionOffset *) { return true; }
553 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
aslc200b112008-08-16 12:57:46 +0000554
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000555 /// EmitValue - Emit section offset.
556 ///
557 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000558
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000559 /// SizeOf - Determine size of section offset value in bytes.
560 ///
561 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000562
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000563 /// Profile - Used to gather unique data for the value folding set.
564 ///
565 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
566 const DWLabel &Section) {
567 ID.AddInteger(isSectionOffset);
568 Label.Profile(ID);
569 Section.Profile(ID);
570 // IsEH and UseSet are specific to the Label/Section that we will emit
571 // the offset for; so Label/Section are enough for uniqueness.
572 }
573 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
574
575#ifndef NDEBUG
576 virtual void print(std::ostream &O) {
577 O << "Off: ";
578 Label.print(O);
579 O << "-";
580 Section.print(O);
581 O << "-" << IsEH << "-" << UseSet;
582 }
583#endif
584};
585
586//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587/// DIEDelta - A simple label difference DIE.
aslc200b112008-08-16 12:57:46 +0000588///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000589class DIEDelta : public DIEValue {
590public:
591 const DWLabel LabelHi;
592 const DWLabel LabelLo;
aslc200b112008-08-16 12:57:46 +0000593
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
595 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
596
597 // Implement isa/cast/dyncast.
598 static bool classof(const DIEDelta *) { return true; }
599 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
aslc200b112008-08-16 12:57:46 +0000600
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000601 /// EmitValue - Emit delta value.
602 ///
603 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000604
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000605 /// SizeOf - Determine size of delta value in bytes.
606 ///
607 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000608
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609 /// Profile - Used to gather unique data for the value folding set.
610 ///
611 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
612 const DWLabel &LabelLo) {
613 ID.AddInteger(isDelta);
614 LabelHi.Profile(ID);
615 LabelLo.Profile(ID);
616 }
617 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
618
619#ifndef NDEBUG
620 virtual void print(std::ostream &O) {
621 O << "Del: ";
622 LabelHi.print(O);
623 O << "-";
624 LabelLo.print(O);
625 }
626#endif
627};
628
629//===----------------------------------------------------------------------===//
630/// DIEntry - A pointer to another debug information entry. An instance of this
631/// class can also be used as a proxy for a debug information entry not yet
632/// defined (ie. types.)
633class DIEntry : public DIEValue {
634public:
635 DIE *Entry;
aslc200b112008-08-16 12:57:46 +0000636
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000637 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
aslc200b112008-08-16 12:57:46 +0000638
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000639 // Implement isa/cast/dyncast.
640 static bool classof(const DIEntry *) { return true; }
641 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
aslc200b112008-08-16 12:57:46 +0000642
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000643 /// EmitValue - Emit debug information entry offset.
644 ///
645 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000646
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000647 /// SizeOf - Determine size of debug information entry in bytes.
648 ///
649 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
650 return sizeof(int32_t);
651 }
aslc200b112008-08-16 12:57:46 +0000652
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000653 /// Profile - Used to gather unique data for the value folding set.
654 ///
655 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
656 ID.AddInteger(isEntry);
657 ID.AddPointer(Entry);
658 }
659 virtual void Profile(FoldingSetNodeID &ID) {
660 ID.AddInteger(isEntry);
aslc200b112008-08-16 12:57:46 +0000661
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000662 if (Entry) {
663 ID.AddPointer(Entry);
664 } else {
665 ID.AddPointer(this);
666 }
667 }
aslc200b112008-08-16 12:57:46 +0000668
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000669#ifndef NDEBUG
670 virtual void print(std::ostream &O) {
671 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
672 }
673#endif
674};
675
676//===----------------------------------------------------------------------===//
677/// DIEBlock - A block of values. Primarily used for location expressions.
678//
679class DIEBlock : public DIEValue, public DIE {
680public:
681 unsigned Size; // Size in bytes excluding size header.
aslc200b112008-08-16 12:57:46 +0000682
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683 DIEBlock()
684 : DIEValue(isBlock)
685 , DIE(0)
686 , Size(0)
687 {}
688 ~DIEBlock() {
689 }
aslc200b112008-08-16 12:57:46 +0000690
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000691 // Implement isa/cast/dyncast.
692 static bool classof(const DIEBlock *) { return true; }
693 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
aslc200b112008-08-16 12:57:46 +0000694
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695 /// ComputeSize - calculate the size of the block.
696 ///
697 unsigned ComputeSize(DwarfDebug &DD);
aslc200b112008-08-16 12:57:46 +0000698
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000699 /// BestForm - Choose the best form for data.
700 ///
701 unsigned BestForm() const {
702 if ((unsigned char)Size == Size) return DW_FORM_block1;
703 if ((unsigned short)Size == Size) return DW_FORM_block2;
704 if ((unsigned int)Size == Size) return DW_FORM_block4;
705 return DW_FORM_block;
706 }
707
708 /// EmitValue - Emit block data.
709 ///
710 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000711
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712 /// SizeOf - Determine size of block data in bytes.
713 ///
714 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000715
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000716
717 /// Profile - Used to gather unique data for the value folding set.
718 ///
719 virtual void Profile(FoldingSetNodeID &ID) {
720 ID.AddInteger(isBlock);
721 DIE::Profile(ID);
722 }
aslc200b112008-08-16 12:57:46 +0000723
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724#ifndef NDEBUG
725 virtual void print(std::ostream &O) {
726 O << "Blk: ";
727 DIE::print(O, 5);
728 }
729#endif
730};
731
732//===----------------------------------------------------------------------===//
733/// CompileUnit - This dwarf writer support class manages information associate
734/// with a source file.
735class CompileUnit {
736private:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000737 /// ID - File identifier for source.
738 ///
739 unsigned ID;
aslc200b112008-08-16 12:57:46 +0000740
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000741 /// Die - Compile unit debug information entry.
742 ///
743 DIE *Die;
aslc200b112008-08-16 12:57:46 +0000744
Devang Patel42f6bed2009-01-13 23:54:55 +0000745 /// GVToDieMap - Tracks the mapping of unit level debug informaton
746 /// variables to debug information entries.
Devang Patel56b1d132009-01-20 00:58:55 +0000747 std::map<GlobalVariable *, DIE *> GVToDieMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000748
Devang Patel42f6bed2009-01-13 23:54:55 +0000749 /// GVToDIEntryMap - Tracks the mapping of unit level debug informaton
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000750 /// descriptors to debug information entries using a DIEntry proxy.
Devang Patel56b1d132009-01-20 00:58:55 +0000751 std::map<GlobalVariable *, DIEntry *> GVToDIEntryMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752
753 /// Globals - A map of globally visible named entities for this unit.
754 ///
755 std::map<std::string, DIE *> Globals;
756
757 /// DiesSet - Used to uniquely define dies within the compile unit.
758 ///
759 FoldingSet<DIE> DiesSet;
aslc200b112008-08-16 12:57:46 +0000760
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000761public:
Devang Patelb3907da2009-01-05 23:03:32 +0000762 CompileUnit(unsigned I, DIE *D)
Devang Patel42f6bed2009-01-13 23:54:55 +0000763 : ID(I), Die(D), GVToDieMap(),
Devang Patel5302e672009-01-17 06:51:37 +0000764 GVToDIEntryMap(), Globals(), DiesSet(InitDiesSetSize)
Devang Patelb3907da2009-01-05 23:03:32 +0000765 {}
766
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000767 ~CompileUnit() {
768 delete Die;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000769 }
aslc200b112008-08-16 12:57:46 +0000770
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000771 // Accessors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000772 unsigned getID() const { return ID; }
773 DIE* getDie() const { return Die; }
774 std::map<std::string, DIE *> &getGlobals() { return Globals; }
775
776 /// hasContent - Return true if this compile unit has something to write out.
777 ///
778 bool hasContent() const {
779 return !Die->getChildren().empty();
780 }
781
782 /// AddGlobal - Add a new global entity to the compile unit.
783 ///
784 void AddGlobal(const std::string &Name, DIE *Die) {
785 Globals[Name] = Die;
786 }
aslc200b112008-08-16 12:57:46 +0000787
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000788 /// getDieMapSlotFor - Returns the debug information entry map slot for the
Devang Patel42f6bed2009-01-13 23:54:55 +0000789 /// specified debug variable.
Devang Patel4a4cbe72009-01-05 21:47:57 +0000790 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
791 return GVToDieMap[GV];
792 }
aslc200b112008-08-16 12:57:46 +0000793
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000794 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
Devang Patel42f6bed2009-01-13 23:54:55 +0000795 /// specified debug variable.
Devang Patel4a4cbe72009-01-05 21:47:57 +0000796 DIEntry *&getDIEntrySlotFor(GlobalVariable *GV) {
797 return GVToDIEntryMap[GV];
798 }
aslc200b112008-08-16 12:57:46 +0000799
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000800 /// AddDie - Adds or interns the DIE to the compile unit.
801 ///
802 DIE *AddDie(DIE &Buffer) {
803 FoldingSetNodeID ID;
804 Buffer.Profile(ID);
805 void *Where;
806 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
aslc200b112008-08-16 12:57:46 +0000807
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000808 if (!Die) {
809 Die = new DIE(Buffer);
810 DiesSet.InsertNode(Die, Where);
811 this->Die->AddChild(Die);
812 Buffer.Detach();
813 }
aslc200b112008-08-16 12:57:46 +0000814
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000815 return Die;
816 }
817};
818
819//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +0000820/// Dwarf - Emits general Dwarf directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000821///
822class Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000823protected:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000824 //===--------------------------------------------------------------------===//
825 // Core attributes used by the Dwarf writer.
826 //
aslc200b112008-08-16 12:57:46 +0000827
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000828 //
829 /// O - Stream to .s file.
830 ///
Owen Anderson847b99b2008-08-21 00:14:44 +0000831 raw_ostream &O;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000832
833 /// Asm - Target of Dwarf emission.
834 ///
835 AsmPrinter *Asm;
aslc200b112008-08-16 12:57:46 +0000836
Bill Wendlingac9639d2008-07-01 23:34:48 +0000837 /// TAI - Target asm information.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000838 const TargetAsmInfo *TAI;
aslc200b112008-08-16 12:57:46 +0000839
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000840 /// TD - Target data.
841 const TargetData *TD;
aslc200b112008-08-16 12:57:46 +0000842
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000843 /// RI - Register Information.
Dan Gohman1e57df32008-02-10 18:45:23 +0000844 const TargetRegisterInfo *RI;
aslc200b112008-08-16 12:57:46 +0000845
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000846 /// M - Current module.
847 ///
848 Module *M;
aslc200b112008-08-16 12:57:46 +0000849
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000850 /// MF - Current machine function.
851 ///
852 MachineFunction *MF;
aslc200b112008-08-16 12:57:46 +0000853
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000854 /// MMI - Collected machine module information.
855 ///
856 MachineModuleInfo *MMI;
aslc200b112008-08-16 12:57:46 +0000857
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000858 /// SubprogramCount - The running count of functions being compiled.
859 ///
860 unsigned SubprogramCount;
aslc200b112008-08-16 12:57:46 +0000861
Chris Lattnerb3876c72007-09-24 03:35:37 +0000862 /// Flavor - A unique string indicating what dwarf producer this is, used to
863 /// unique labels.
864 const char * const Flavor;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000865
866 unsigned SetCounter;
Owen Anderson847b99b2008-08-21 00:14:44 +0000867 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattnerb3876c72007-09-24 03:35:37 +0000868 const char *flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000869 : O(OS)
870 , Asm(A)
871 , TAI(T)
872 , TD(Asm->TM.getTargetData())
873 , RI(Asm->TM.getRegisterInfo())
874 , M(NULL)
875 , MF(NULL)
876 , MMI(NULL)
877 , SubprogramCount(0)
Chris Lattnerb3876c72007-09-24 03:35:37 +0000878 , Flavor(flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000879 , SetCounter(1)
880 {
881 }
882
883public:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000884 //===--------------------------------------------------------------------===//
885 // Accessors.
886 //
887 AsmPrinter *getAsm() const { return Asm; }
888 MachineModuleInfo *getMMI() const { return MMI; }
889 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohmancfb72b22007-09-27 23:12:31 +0000890 const TargetData *getTargetData() const { return TD; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000891
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000892 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
893 const {
894 if (isInSection && TAI->getDwarfSectionOffsetDirective())
895 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohmancfb72b22007-09-27 23:12:31 +0000896 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000897 O << TAI->getData32bitsDirective();
898 else
899 O << TAI->getData64bitsDirective();
900 }
aslc200b112008-08-16 12:57:46 +0000901
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000902 /// PrintLabelName - Print label name in form used by Dwarf writer.
903 ///
904 void PrintLabelName(DWLabel Label) const {
905 PrintLabelName(Label.Tag, Label.Number);
906 }
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000907 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000908 O << TAI->getPrivateGlobalPrefix() << Tag;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000909 if (Number) O << Number;
910 }
aslc200b112008-08-16 12:57:46 +0000911
Chris Lattnerb3876c72007-09-24 03:35:37 +0000912 void PrintLabelName(const char *Tag, unsigned Number,
913 const char *Suffix) const {
914 O << TAI->getPrivateGlobalPrefix() << Tag;
915 if (Number) O << Number;
916 O << Suffix;
917 }
aslc200b112008-08-16 12:57:46 +0000918
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000919 /// EmitLabel - Emit location label for internal use by Dwarf.
920 ///
921 void EmitLabel(DWLabel Label) const {
922 EmitLabel(Label.Tag, Label.Number);
923 }
924 void EmitLabel(const char *Tag, unsigned Number) const {
925 PrintLabelName(Tag, Number);
926 O << ":\n";
927 }
aslc200b112008-08-16 12:57:46 +0000928
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929 /// EmitReference - Emit a reference to a label.
930 ///
Dan Gohman4fd77742007-09-28 15:43:33 +0000931 void EmitReference(DWLabel Label, bool IsPCRelative = false,
932 bool Force32Bit = false) const {
933 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000934 }
935 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman4fd77742007-09-28 15:43:33 +0000936 bool IsPCRelative = false, bool Force32Bit = false) const {
937 PrintRelDirective(Force32Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000938 PrintLabelName(Tag, Number);
aslc200b112008-08-16 12:57:46 +0000939
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000940 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
941 }
Dan Gohman4fd77742007-09-28 15:43:33 +0000942 void EmitReference(const std::string &Name, bool IsPCRelative = false,
943 bool Force32Bit = false) const {
944 PrintRelDirective(Force32Bit);
aslc200b112008-08-16 12:57:46 +0000945
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000946 O << Name;
aslc200b112008-08-16 12:57:46 +0000947
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000948 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
949 }
950
951 /// EmitDifference - Emit the difference between two labels. Some
952 /// assemblers do not behave with absolute expressions with data directives,
953 /// so there is an option (needsSet) to use an intermediary set expression.
954 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
955 bool IsSmall = false) {
956 EmitDifference(LabelHi.Tag, LabelHi.Number,
957 LabelLo.Tag, LabelLo.Number,
958 IsSmall);
959 }
960 void EmitDifference(const char *TagHi, unsigned NumberHi,
961 const char *TagLo, unsigned NumberLo,
962 bool IsSmall = false) {
963 if (TAI->needsSet()) {
964 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +0000965 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000966 O << ",";
967 PrintLabelName(TagHi, NumberHi);
968 O << "-";
969 PrintLabelName(TagLo, NumberLo);
970 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000971
972 PrintRelDirective(IsSmall);
Chris Lattnerb3876c72007-09-24 03:35:37 +0000973 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000974 ++SetCounter;
975 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000976 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +0000977
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000978 PrintLabelName(TagHi, NumberHi);
979 O << "-";
980 PrintLabelName(TagLo, NumberLo);
981 }
982 }
983
984 void EmitSectionOffset(const char* Label, const char* Section,
985 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesen0ebb2432008-03-26 23:31:39 +0000986 bool IsSmall = false, bool isEH = false,
987 bool useSet = true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000988 bool printAbsolute = false;
Dale Johannesen0ebb2432008-03-26 23:31:39 +0000989 if (isEH)
990 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
991 else
992 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
993
994 if (TAI->needsSet() && useSet) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000995 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +0000996 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000997 O << ",";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000998 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000999
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001000 if (!printAbsolute) {
1001 O << "-";
1002 PrintLabelName(Section, SectionNumber);
aslc200b112008-08-16 12:57:46 +00001003 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001004 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001005
1006 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +00001007
Chris Lattnerb3876c72007-09-24 03:35:37 +00001008 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001009 ++SetCounter;
1010 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001011 PrintRelDirective(IsSmall, true);
aslc200b112008-08-16 12:57:46 +00001012
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001013 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001014
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001015 if (!printAbsolute) {
1016 O << "-";
1017 PrintLabelName(Section, SectionNumber);
1018 }
aslc200b112008-08-16 12:57:46 +00001019 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001020 }
aslc200b112008-08-16 12:57:46 +00001021
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001022 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1023 /// frame.
1024 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenf5a11532007-11-13 19:13:01 +00001025 const std::vector<MachineMove> &Moves, bool isEH) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001026 int stackGrowth =
1027 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1028 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00001029 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001030 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
1031
1032 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
1033 const MachineMove &Move = Moves[i];
1034 unsigned LabelID = Move.getLabelID();
aslc200b112008-08-16 12:57:46 +00001035
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001036 if (LabelID) {
1037 LabelID = MMI->MappedLabel(LabelID);
aslc200b112008-08-16 12:57:46 +00001038
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001039 // Throw out move if the label is invalid.
1040 if (!LabelID) continue;
1041 }
aslc200b112008-08-16 12:57:46 +00001042
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001043 const MachineLocation &Dst = Move.getDestination();
1044 const MachineLocation &Src = Move.getSource();
aslc200b112008-08-16 12:57:46 +00001045
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001046 // Advance row if new location.
1047 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1048 Asm->EmitInt8(DW_CFA_advance_loc4);
1049 Asm->EOL("DW_CFA_advance_loc4");
1050 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1051 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00001052
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001053 BaseLabelID = LabelID;
1054 BaseLabel = "label";
1055 IsLocal = true;
1056 }
aslc200b112008-08-16 12:57:46 +00001057
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001058 // If advancing cfa.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001059 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1060 if (!Src.isReg()) {
1061 if (Src.getReg() == MachineLocation::VirtualFP) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001062 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1063 Asm->EOL("DW_CFA_def_cfa_offset");
1064 } else {
1065 Asm->EmitInt8(DW_CFA_def_cfa);
1066 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001067 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001068 Asm->EOL("Register");
1069 }
aslc200b112008-08-16 12:57:46 +00001070
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001071 int Offset = -Src.getOffset();
aslc200b112008-08-16 12:57:46 +00001072
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001073 Asm->EmitULEB128Bytes(Offset);
1074 Asm->EOL("Offset");
1075 } else {
1076 assert(0 && "Machine move no supported yet.");
1077 }
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001078 } else if (Src.isReg() &&
1079 Src.getReg() == MachineLocation::VirtualFP) {
1080 if (Dst.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001081 Asm->EmitInt8(DW_CFA_def_cfa_register);
1082 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001083 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001084 Asm->EOL("Register");
1085 } else {
1086 assert(0 && "Machine move no supported yet.");
1087 }
1088 } else {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001089 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001090 int Offset = Dst.getOffset() / stackGrowth;
aslc200b112008-08-16 12:57:46 +00001091
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001092 if (Offset < 0) {
1093 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1094 Asm->EOL("DW_CFA_offset_extended_sf");
1095 Asm->EmitULEB128Bytes(Reg);
1096 Asm->EOL("Reg");
1097 Asm->EmitSLEB128Bytes(Offset);
1098 Asm->EOL("Offset");
1099 } else if (Reg < 64) {
1100 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Cheng6181e062008-07-09 21:53:02 +00001101 if (VerboseAsm)
1102 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1103 else
1104 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001105 Asm->EmitULEB128Bytes(Offset);
1106 Asm->EOL("Offset");
1107 } else {
1108 Asm->EmitInt8(DW_CFA_offset_extended);
1109 Asm->EOL("DW_CFA_offset_extended");
1110 Asm->EmitULEB128Bytes(Reg);
1111 Asm->EOL("Reg");
1112 Asm->EmitULEB128Bytes(Offset);
1113 Asm->EOL("Offset");
1114 }
1115 }
1116 }
1117 }
1118
1119};
1120
1121//===----------------------------------------------------------------------===//
Devang Patel35a078f2009-01-12 22:54:42 +00001122/// SrcLineInfo - This class is used to record source line correspondence.
Devang Patel7dd15a92009-01-08 17:19:22 +00001123///
1124class SrcLineInfo {
1125 unsigned Line; // Source line number.
1126 unsigned Column; // Source column.
1127 unsigned SourceID; // Source ID number.
1128 unsigned LabelID; // Label in code ID number.
1129public:
1130 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
Bill Wendling824a8bf2009-02-03 21:17:20 +00001131 : Line(L), Column(C), SourceID(S), LabelID(I) {}
Devang Patel7dd15a92009-01-08 17:19:22 +00001132
1133 // Accessors
1134 unsigned getLine() const { return Line; }
1135 unsigned getColumn() const { return Column; }
1136 unsigned getSourceID() const { return SourceID; }
1137 unsigned getLabelID() const { return LabelID; }
1138};
1139
Devang Patel7dd15a92009-01-08 17:19:22 +00001140//===----------------------------------------------------------------------===//
Devang Patel4d1709e2009-01-08 02:33:41 +00001141/// DbgVariable - This class is used to track local variable information.
1142///
1143class DbgVariable {
Devang Patel7c8a2772009-01-16 19:28:14 +00001144 DIVariable Var; // Variable Descriptor.
Devang Patel4d1709e2009-01-08 02:33:41 +00001145 unsigned FrameIndex; // Variable frame index.
Devang Patel4d1709e2009-01-08 02:33:41 +00001146public:
Devang Patel7c8a2772009-01-16 19:28:14 +00001147 DbgVariable(DIVariable V, unsigned I) : Var(V), FrameIndex(I) {}
Devang Patel4d1709e2009-01-08 02:33:41 +00001148
1149 // Accessors.
Devang Patel7c8a2772009-01-16 19:28:14 +00001150 DIVariable getVariable() const { return Var; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001151 unsigned getFrameIndex() const { return FrameIndex; }
1152};
1153
1154//===----------------------------------------------------------------------===//
1155/// DbgScope - This class is used to track scope information.
1156///
1157class DbgScope {
Devang Patel4d1709e2009-01-08 02:33:41 +00001158 DbgScope *Parent; // Parent to this scope.
Devang Patel49a3bd92009-01-16 18:01:58 +00001159 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel4d1709e2009-01-08 02:33:41 +00001160 // Either subprogram or block.
1161 unsigned StartLabelID; // Label ID of the beginning of scope.
1162 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel49a3bd92009-01-16 18:01:58 +00001163 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
Devang Patel63c22f42009-01-10 02:42:49 +00001164 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Devang Patel4d1709e2009-01-08 02:33:41 +00001165public:
Devang Patel2560d922009-01-15 18:25:17 +00001166 DbgScope(DbgScope *P, DIDescriptor D)
Devang Patel4d1709e2009-01-08 02:33:41 +00001167 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), Scopes(), Variables()
1168 {}
Devang Patela4162952009-01-12 18:48:36 +00001169 ~DbgScope() {
1170 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1171 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1172 }
Devang Patel4d1709e2009-01-08 02:33:41 +00001173
1174 // Accessors.
Devang Patel49a3bd92009-01-16 18:01:58 +00001175 DbgScope *getParent() const { return Parent; }
1176 DIDescriptor getDesc() const { return Desc; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001177 unsigned getStartLabelID() const { return StartLabelID; }
1178 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel63c22f42009-01-10 02:42:49 +00001179 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1180 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001181 void setStartLabelID(unsigned S) { StartLabelID = S; }
1182 void setEndLabelID(unsigned E) { EndLabelID = E; }
1183
1184 /// AddScope - Add a scope to the scope.
1185 ///
1186 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1187
1188 /// AddVariable - Add a variable to the scope.
1189 ///
1190 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
1191};
1192
1193//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00001194/// DwarfDebug - Emits Dwarf debug directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001195///
1196class DwarfDebug : public Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001197 //===--------------------------------------------------------------------===//
1198 // Attributes used to construct specific Dwarf sections.
1199 //
aslc200b112008-08-16 12:57:46 +00001200
Evan Cheng3e288912009-02-25 07:04:34 +00001201 /// CompileUnitMap - A map of global variables representing compile units to
1202 /// compile units.
1203 DenseMap<Value *, CompileUnit *> CompileUnitMap;
1204
1205 /// CompileUnits - All the compile units in this module.
1206 ///
1207 SmallVector<CompileUnit *, 8> CompileUnits;
aslc200b112008-08-16 12:57:46 +00001208
Devang Patel2ae1db52009-01-30 18:20:31 +00001209 /// MainCU - Some platform prefers one compile unit per .o file. In such
1210 /// cases, all dies are inserted in MainCU.
1211 CompileUnit *MainCU;
Bill Wendlinge0f3a262009-02-20 20:40:28 +00001212
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001213 /// AbbreviationsSet - Used to uniquely define abbreviations.
1214 ///
1215 FoldingSet<DIEAbbrev> AbbreviationsSet;
1216
1217 /// Abbreviations - A list of all the unique abbreviations in use.
1218 ///
1219 std::vector<DIEAbbrev *> Abbreviations;
aslc200b112008-08-16 12:57:46 +00001220
Evan Cheng3e288912009-02-25 07:04:34 +00001221 /// DirectoryIdMap - Directory name to directory id map.
1222 ///
1223 StringMap<unsigned> DirectoryIdMap;
Devang Patel5f244e32009-01-05 22:35:52 +00001224
Evan Cheng3e288912009-02-25 07:04:34 +00001225 /// DirectoryNames - A list of directory names.
1226 SmallVector<std::string, 8> DirectoryNames;
1227
1228 /// SourceFileIdMap - Source file name to source file id map.
1229 ///
1230 StringMap<unsigned> SourceFileIdMap;
1231
1232 /// SourceFileNames - A list of source file names.
1233 SmallVector<std::string, 8> SourceFileNames;
1234
1235 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
1236 /// id mapped to a unique id.
1237 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
1238
1239 /// SourceIds - Reverse map from source id to directory id + file id pair.
1240 ///
1241 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
Devang Patel5f244e32009-01-05 22:35:52 +00001242
Devang Patel9b829452009-01-16 21:07:53 +00001243 /// Lines - List of of source line correspondence.
Devang Patel7dd15a92009-01-08 17:19:22 +00001244 std::vector<SrcLineInfo> Lines;
1245
Devang Patel9b829452009-01-16 21:07:53 +00001246 /// ValuesSet - Used to uniquely define values.
1247 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001248 FoldingSet<DIEValue> ValuesSet;
aslc200b112008-08-16 12:57:46 +00001249
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001250 /// Values - A list of all the unique values in use.
1251 ///
1252 std::vector<DIEValue *> Values;
aslc200b112008-08-16 12:57:46 +00001253
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001254 /// StringPool - A UniqueVector of strings used by indirect references.
1255 ///
1256 UniqueVector<std::string> StringPool;
1257
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001258 /// SectionMap - Provides a unique id per text section.
1259 ///
Anton Korobeynikov55b94962008-09-24 22:15:21 +00001260 UniqueVector<const Section*> SectionMap;
aslc200b112008-08-16 12:57:46 +00001261
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001262 /// SectionSourceLines - Tracks line numbers per text section.
1263 ///
Devang Patel35a078f2009-01-12 22:54:42 +00001264 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001265
1266 /// didInitial - Flag to indicate if initial emission has been done.
1267 ///
1268 bool didInitial;
aslc200b112008-08-16 12:57:46 +00001269
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001270 /// shouldEmit - Flag to indicate if debug information should be emitted.
1271 ///
1272 bool shouldEmit;
1273
Devang Patel2560d922009-01-15 18:25:17 +00001274 // RootDbgScope - Top level scope for the current function.
Devang Patel4d1709e2009-01-08 02:33:41 +00001275 //
1276 DbgScope *RootDbgScope;
1277
1278 // DbgScopeMap - Tracks the scopes in the current function.
1279 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
1280
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001281 struct FunctionDebugFrameInfo {
1282 unsigned Number;
1283 std::vector<MachineMove> Moves;
1284
1285 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman9ba5d4d2007-08-27 14:50:10 +00001286 Number(Num), Moves(M) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001287 };
1288
1289 std::vector<FunctionDebugFrameInfo> DebugFrames;
aslc200b112008-08-16 12:57:46 +00001290
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001291public:
aslc200b112008-08-16 12:57:46 +00001292
Bill Wendling50db0792009-02-20 00:44:43 +00001293 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
1294 /// be emitted.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001295 ///
Bill Wendling50db0792009-02-20 00:44:43 +00001296 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001297
1298 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
aslc200b112008-08-16 12:57:46 +00001299 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001300 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1301 // Profile the node so that we can make it unique.
1302 FoldingSetNodeID ID;
1303 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00001304
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001305 // Check the set for priors.
1306 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
aslc200b112008-08-16 12:57:46 +00001307
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001308 // If it's newly added.
1309 if (InSet == &Abbrev) {
aslc200b112008-08-16 12:57:46 +00001310 // Add to abbreviation list.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001311 Abbreviations.push_back(&Abbrev);
1312 // Assign the vector position + 1 as its number.
1313 Abbrev.setNumber(Abbreviations.size());
1314 } else {
1315 // Assign existing abbreviation number.
1316 Abbrev.setNumber(InSet->getNumber());
1317 }
1318 }
1319
1320 /// NewString - Add a string to the constant pool and returns a label.
1321 ///
1322 DWLabel NewString(const std::string &String) {
1323 unsigned StringID = StringPool.insert(String);
1324 return DWLabel("string", StringID);
1325 }
aslc200b112008-08-16 12:57:46 +00001326
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001327 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1328 /// entry.
1329 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1330 DIEntry *Value;
aslc200b112008-08-16 12:57:46 +00001331
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001332 if (Entry) {
1333 FoldingSetNodeID ID;
1334 DIEntry::Profile(ID, Entry);
1335 void *Where;
1336 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
aslc200b112008-08-16 12:57:46 +00001337
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001338 if (Value) return Value;
aslc200b112008-08-16 12:57:46 +00001339
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001340 Value = new DIEntry(Entry);
1341 ValuesSet.InsertNode(Value, Where);
1342 } else {
1343 Value = new DIEntry(Entry);
1344 }
aslc200b112008-08-16 12:57:46 +00001345
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001346 Values.push_back(Value);
1347 return Value;
1348 }
aslc200b112008-08-16 12:57:46 +00001349
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001350 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1351 ///
1352 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1353 Value->Entry = Entry;
1354 // Add to values set if not already there. If it is, we merely have a
1355 // duplicate in the values list (no harm.)
1356 ValuesSet.GetOrInsertNode(Value);
1357 }
1358
1359 /// AddUInt - Add an unsigned integer attribute data and value.
1360 ///
1361 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1362 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1363
1364 FoldingSetNodeID ID;
1365 DIEInteger::Profile(ID, Integer);
1366 void *Where;
1367 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1368 if (!Value) {
1369 Value = new DIEInteger(Integer);
1370 ValuesSet.InsertNode(Value, Where);
1371 Values.push_back(Value);
1372 }
aslc200b112008-08-16 12:57:46 +00001373
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001374 Die->AddValue(Attribute, Form, Value);
1375 }
aslc200b112008-08-16 12:57:46 +00001376
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001377 /// AddSInt - Add an signed integer attribute data and value.
1378 ///
1379 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1380 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1381
1382 FoldingSetNodeID ID;
1383 DIEInteger::Profile(ID, (uint64_t)Integer);
1384 void *Where;
1385 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1386 if (!Value) {
1387 Value = new DIEInteger(Integer);
1388 ValuesSet.InsertNode(Value, Where);
1389 Values.push_back(Value);
1390 }
aslc200b112008-08-16 12:57:46 +00001391
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001392 Die->AddValue(Attribute, Form, Value);
1393 }
aslc200b112008-08-16 12:57:46 +00001394
Evan Cheng3e288912009-02-25 07:04:34 +00001395 /// AddString - Add a string attribute data and value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001396 ///
1397 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1398 const std::string &String) {
1399 FoldingSetNodeID ID;
1400 DIEString::Profile(ID, String);
1401 void *Where;
1402 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1403 if (!Value) {
1404 Value = new DIEString(String);
1405 ValuesSet.InsertNode(Value, Where);
1406 Values.push_back(Value);
1407 }
aslc200b112008-08-16 12:57:46 +00001408
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001409 Die->AddValue(Attribute, Form, Value);
1410 }
aslc200b112008-08-16 12:57:46 +00001411
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001412 /// AddLabel - Add a Dwarf label attribute data and value.
1413 ///
1414 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1415 const DWLabel &Label) {
1416 FoldingSetNodeID ID;
1417 DIEDwarfLabel::Profile(ID, Label);
1418 void *Where;
1419 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1420 if (!Value) {
1421 Value = new DIEDwarfLabel(Label);
1422 ValuesSet.InsertNode(Value, Where);
1423 Values.push_back(Value);
1424 }
aslc200b112008-08-16 12:57:46 +00001425
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001426 Die->AddValue(Attribute, Form, Value);
1427 }
aslc200b112008-08-16 12:57:46 +00001428
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001429 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1430 ///
1431 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1432 const std::string &Label) {
1433 FoldingSetNodeID ID;
1434 DIEObjectLabel::Profile(ID, Label);
1435 void *Where;
1436 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1437 if (!Value) {
1438 Value = new DIEObjectLabel(Label);
1439 ValuesSet.InsertNode(Value, Where);
1440 Values.push_back(Value);
1441 }
aslc200b112008-08-16 12:57:46 +00001442
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001443 Die->AddValue(Attribute, Form, Value);
1444 }
aslc200b112008-08-16 12:57:46 +00001445
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001446 /// AddSectionOffset - Add a section offset label attribute data and value.
1447 ///
1448 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1449 const DWLabel &Label, const DWLabel &Section,
1450 bool isEH = false, bool useSet = true) {
1451 FoldingSetNodeID ID;
1452 DIESectionOffset::Profile(ID, Label, Section);
1453 void *Where;
1454 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1455 if (!Value) {
1456 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1457 ValuesSet.InsertNode(Value, Where);
1458 Values.push_back(Value);
1459 }
aslc200b112008-08-16 12:57:46 +00001460
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001461 Die->AddValue(Attribute, Form, Value);
1462 }
aslc200b112008-08-16 12:57:46 +00001463
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001464 /// AddDelta - Add a label delta attribute data and value.
1465 ///
1466 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1467 const DWLabel &Hi, const DWLabel &Lo) {
1468 FoldingSetNodeID ID;
1469 DIEDelta::Profile(ID, Hi, Lo);
1470 void *Where;
1471 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1472 if (!Value) {
1473 Value = new DIEDelta(Hi, Lo);
1474 ValuesSet.InsertNode(Value, Where);
1475 Values.push_back(Value);
1476 }
aslc200b112008-08-16 12:57:46 +00001477
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001478 Die->AddValue(Attribute, Form, Value);
1479 }
aslc200b112008-08-16 12:57:46 +00001480
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001481 /// AddDIEntry - Add a DIE attribute data and value.
1482 ///
1483 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1484 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1485 }
1486
1487 /// AddBlock - Add block data.
1488 ///
1489 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1490 Block->ComputeSize(*this);
1491 FoldingSetNodeID ID;
1492 Block->Profile(ID);
1493 void *Where;
1494 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1495 if (!Value) {
1496 Value = Block;
1497 ValuesSet.InsertNode(Value, Where);
1498 Values.push_back(Value);
1499 } else {
Chris Lattner3de66892007-09-21 18:25:53 +00001500 // Already exists, reuse the previous one.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001501 delete Block;
Chris Lattner3de66892007-09-21 18:25:53 +00001502 Block = cast<DIEBlock>(Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001503 }
aslc200b112008-08-16 12:57:46 +00001504
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001505 Die->AddValue(Attribute, Block->BestForm(), Value);
1506 }
1507
1508private:
1509
1510 /// AddSourceLine - Add location information to specified debug information
1511 /// entry.
Devang Patel7c8a2772009-01-16 19:28:14 +00001512 void AddSourceLine(DIE *Die, const DIVariable *V) {
Devang Patel4d1709e2009-01-08 02:33:41 +00001513 unsigned FileID = 0;
1514 unsigned Line = V->getLineNumber();
Devang Patel2ae1db52009-01-30 18:20:31 +00001515 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1516 FileID = Unit->getID();
Devang Pateld94b6932009-02-10 06:04:08 +00001517 assert (FileID && "Invalid file id");
Devang Patel4d1709e2009-01-08 02:33:41 +00001518 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1519 AddUInt(Die, DW_AT_decl_line, 0, Line);
1520 }
1521
1522 /// AddSourceLine - Add location information to specified debug information
1523 /// entry.
Devang Patel7c8a2772009-01-16 19:28:14 +00001524 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Devang Patel5f244e32009-01-05 22:35:52 +00001525 unsigned FileID = 0;
1526 unsigned Line = G->getLineNumber();
Devang Patel2ae1db52009-01-30 18:20:31 +00001527 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1528 FileID = Unit->getID();
Devang Pateld94b6932009-02-10 06:04:08 +00001529 assert (FileID && "Invalid file id");
Devang Patel5f244e32009-01-05 22:35:52 +00001530 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1531 AddUInt(Die, DW_AT_decl_line, 0, Line);
1532 }
1533
Devang Patel7c8a2772009-01-16 19:28:14 +00001534 void AddSourceLine(DIE *Die, const DIType *Ty) {
Devang Patel5f244e32009-01-05 22:35:52 +00001535 unsigned FileID = 0;
Devang Patel7c8a2772009-01-16 19:28:14 +00001536 unsigned Line = Ty->getLineNumber();
Devang Patel2ae1db52009-01-30 18:20:31 +00001537 DICompileUnit CU = Ty->getCompileUnit();
1538 if (CU.isNull())
1539 return;
1540 CompileUnit *Unit = FindCompileUnit(CU);
1541 FileID = Unit->getID();
Devang Pateld94b6932009-02-10 06:04:08 +00001542 assert (FileID && "Invalid file id");
Devang Patel5f244e32009-01-05 22:35:52 +00001543 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1544 AddUInt(Die, DW_AT_decl_line, 0, Line);
1545 }
1546
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001547 /// AddAddress - Add an address attribute to a die based on the location
1548 /// provided.
1549 void AddAddress(DIE *Die, unsigned Attribute,
1550 const MachineLocation &Location) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001551 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001552 DIEBlock *Block = new DIEBlock();
aslc200b112008-08-16 12:57:46 +00001553
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001554 if (Location.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001555 if (Reg < 32) {
1556 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1557 } else {
1558 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1559 AddUInt(Block, 0, DW_FORM_udata, Reg);
1560 }
1561 } else {
1562 if (Reg < 32) {
1563 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1564 } else {
1565 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1566 AddUInt(Block, 0, DW_FORM_udata, Reg);
1567 }
1568 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1569 }
aslc200b112008-08-16 12:57:46 +00001570
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001571 AddBlock(Die, Attribute, 0, Block);
1572 }
aslc200b112008-08-16 12:57:46 +00001573
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001574 /// AddType - Add a new type attribute to the specified entity.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001575 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel165ed512009-01-23 19:13:31 +00001576 if (Ty.isNull())
Devang Patel4a4cbe72009-01-05 21:47:57 +00001577 return;
Devang Patel4a4cbe72009-01-05 21:47:57 +00001578
1579 // Check for pre-existence.
1580 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1581 // If it exists then use the existing value.
1582 if (Slot) {
1583 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1584 return;
1585 }
1586
1587 // Set up proxy.
1588 Slot = NewDIEntry();
1589
1590 // Construct type.
1591 DIE Buffer(DW_TAG_base_type);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001592 if (Ty.isBasicType(Ty.getTag()))
1593 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1594 else if (Ty.isDerivedType(Ty.getTag()))
1595 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1596 else {
Bill Wendling824a8bf2009-02-03 21:17:20 +00001597 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelef4bf3b2009-01-15 19:26:23 +00001598 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1599 }
1600
Devang Patelb0cb07c2009-01-27 23:22:55 +00001601 // Add debug information entry to entity and appropriate context.
1602 DIE *Die = NULL;
1603 DIDescriptor Context = Ty.getContext();
1604 if (!Context.isNull())
1605 Die = DW_Unit->getDieMapSlotFor(Context.getGV());
1606
1607 if (Die) {
1608 DIE *Child = new DIE(Buffer);
1609 Die->AddChild(Child);
1610 Buffer.Detach();
1611 SetDIEntry(Slot, Child);
Bill Wendling824a8bf2009-02-03 21:17:20 +00001612 } else {
Devang Patelb0cb07c2009-01-27 23:22:55 +00001613 Die = DW_Unit->AddDie(Buffer);
1614 SetDIEntry(Slot, Die);
1615 }
1616
Devang Patel4a4cbe72009-01-05 21:47:57 +00001617 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1618 }
1619
Devang Patel46d13752009-01-05 19:07:53 +00001620 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1621 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001622 DIBasicType BTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001623
1624 // Get core information.
Bill Wendling1c5842b2009-03-09 05:04:40 +00001625 std::string Name;
1626 BTy.getName(Name);
Devang Patelfc187162009-01-05 17:57:47 +00001627 Buffer.setTag(DW_TAG_base_type);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001628 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patelfc187162009-01-05 17:57:47 +00001629 // Add name if not anonymous or intermediate type.
1630 if (!Name.empty())
1631 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001632 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelfc187162009-01-05 17:57:47 +00001633 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1634 }
1635
Devang Patel46d13752009-01-05 19:07:53 +00001636 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1637 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001638 DIDerivedType DTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001639
1640 // Get core information.
Bill Wendling1c5842b2009-03-09 05:04:40 +00001641 std::string Name;
1642 DTy.getName(Name);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001643 uint64_t Size = DTy.getSizeInBits() >> 3;
1644 unsigned Tag = DTy.getTag();
Bill Wendling1c5842b2009-03-09 05:04:40 +00001645
Devang Patelfc187162009-01-05 17:57:47 +00001646 // FIXME - Workaround for templates.
1647 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1648
1649 Buffer.setTag(Tag);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001650
Devang Patelfc187162009-01-05 17:57:47 +00001651 // Map to main type, void will not have a type.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001652 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel4a4cbe72009-01-05 21:47:57 +00001653 AddType(DW_Unit, &Buffer, FromTy);
Devang Patelfc187162009-01-05 17:57:47 +00001654
1655 // Add name if not anonymous or intermediate type.
Evan Cheng3e288912009-02-25 07:04:34 +00001656 if (!Name.empty())
1657 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelfc187162009-01-05 17:57:47 +00001658
1659 // Add size if non-zero (derived types might be zero-sized.)
1660 if (Size)
1661 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1662
1663 // Add source line info if available and TyDesc is not a forward
1664 // declaration.
Devang Patele34e0882009-01-27 00:45:04 +00001665 if (!DTy.isForwardDecl())
1666 AddSourceLine(&Buffer, &DTy);
Devang Patelfc187162009-01-05 17:57:47 +00001667 }
1668
Devang Patel30c01372009-01-05 19:55:51 +00001669 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1670 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001671 DICompositeType CTy) {
Devang Patelb28de842009-01-17 08:01:33 +00001672 // Get core information.
Bill Wendling1c5842b2009-03-09 05:04:40 +00001673 std::string Name;
1674 CTy.getName(Name);
1675
Devang Patelef4bf3b2009-01-15 19:26:23 +00001676 uint64_t Size = CTy.getSizeInBits() >> 3;
1677 unsigned Tag = CTy.getTag();
Devang Patel8050bd72009-01-23 01:19:09 +00001678 Buffer.setTag(Tag);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001679
Devang Patel30c01372009-01-05 19:55:51 +00001680 switch (Tag) {
1681 case DW_TAG_vector_type:
1682 case DW_TAG_array_type:
Devang Patelef4bf3b2009-01-15 19:26:23 +00001683 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patel30c01372009-01-05 19:55:51 +00001684 break;
Devang Patel3798f492009-01-20 18:35:14 +00001685 case DW_TAG_enumeration_type:
1686 {
1687 DIArray Elements = CTy.getTypeArray();
1688 // Add enumerators to enumeration type.
1689 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1690 DIE *ElemDie = NULL;
1691 DIEnumerator Enum(Elements.getElement(i).getGV());
1692 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
1693 Buffer.AddChild(ElemDie);
1694 }
1695 }
1696 break;
Devang Patel30c01372009-01-05 19:55:51 +00001697 case DW_TAG_subroutine_type:
1698 {
1699 // Add prototype flag.
1700 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001701 DIArray Elements = CTy.getTypeArray();
Devang Patel30c01372009-01-05 19:55:51 +00001702 // Add return type.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001703 DIDescriptor RTy = Elements.getElement(0);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001704 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel4a4cbe72009-01-05 21:47:57 +00001705
Devang Patel30c01372009-01-05 19:55:51 +00001706 // Add arguments.
1707 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1708 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel4a4cbe72009-01-05 21:47:57 +00001709 DIDescriptor Ty = Elements.getElement(i);
Devang Pateld40a7e52009-01-17 06:57:25 +00001710 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patel30c01372009-01-05 19:55:51 +00001711 Buffer.AddChild(Arg);
1712 }
1713 }
1714 break;
1715 case DW_TAG_structure_type:
1716 case DW_TAG_union_type:
1717 {
1718 // Add elements to structure type.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001719 DIArray Elements = CTy.getTypeArray();
Devang Patelcf7acb12009-01-16 00:50:53 +00001720
1721 // A forward struct declared type may not have elements available.
1722 if (Elements.isNull())
1723 break;
1724
Devang Patel30c01372009-01-05 19:55:51 +00001725 // Add elements to structure type.
1726 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1727 DIDescriptor Element = Elements.getElement(i);
Devang Patelb28de842009-01-17 08:01:33 +00001728 DIE *ElemDie = NULL;
Devang Patelef4bf3b2009-01-15 19:26:23 +00001729 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel245446c2009-01-17 08:05:14 +00001730 ElemDie = CreateSubprogramDIE(DW_Unit,
1731 DISubprogram(Element.getGV()));
Devang Patelb28de842009-01-17 08:01:33 +00001732 else if (Element.getTag() == dwarf::DW_TAG_variable) // ???
1733 ElemDie = CreateGlobalVariableDIE(DW_Unit,
1734 DIGlobalVariable(Element.getGV()));
Devang Patel5c643892009-01-20 21:02:02 +00001735 else
1736 ElemDie = CreateMemberDIE(DW_Unit,
1737 DIDerivedType(Element.getGV()));
Devang Patel245446c2009-01-17 08:05:14 +00001738 Buffer.AddChild(ElemDie);
Devang Patel30c01372009-01-05 19:55:51 +00001739 }
Devang Patel74193d72009-02-17 22:43:44 +00001740 unsigned RLang = CTy.getRunTimeLang();
1741 if (RLang)
1742 AddUInt(&Buffer, DW_AT_APPLE_runtime_class, DW_FORM_data1, RLang);
Devang Patel30c01372009-01-05 19:55:51 +00001743 }
1744 break;
1745 default:
1746 break;
1747 }
1748
1749 // Add name if not anonymous or intermediate type.
Evan Cheng3e288912009-02-25 07:04:34 +00001750 if (!Name.empty())
1751 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel30c01372009-01-05 19:55:51 +00001752
Devang Patele34e0882009-01-27 00:45:04 +00001753 if (Tag == DW_TAG_enumeration_type || Tag == DW_TAG_structure_type
1754 || Tag == DW_TAG_union_type) {
1755 // Add size if non-zero (derived types might be zero-sized.)
1756 if (Size)
1757 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1758 else {
1759 // Add zero size if it is not a forward declaration.
1760 if (CTy.isForwardDecl())
1761 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1762 else
1763 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1764 }
1765
1766 // Add source line info if available.
1767 if (!CTy.isForwardDecl())
1768 AddSourceLine(&Buffer, &CTy);
Devang Patel30c01372009-01-05 19:55:51 +00001769 }
Devang Patel30c01372009-01-05 19:55:51 +00001770 }
1771
Bill Wendling824a8bf2009-02-03 21:17:20 +00001772 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1773 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelef4bf3b2009-01-15 19:26:23 +00001774 int64_t L = SR.getLo();
1775 int64_t H = SR.getHi();
Devang Patel6fb54132009-01-05 18:33:01 +00001776 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1777 if (L != H) {
1778 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1779 if (L)
Devang Patel245446c2009-01-17 08:05:14 +00001780 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1781 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel6fb54132009-01-05 18:33:01 +00001782 }
1783 Buffer.AddChild(DW_Subrange);
1784 }
1785
1786 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1787 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1788 DICompositeType *CTy) {
1789 Buffer.setTag(DW_TAG_array_type);
1790 if (CTy->getTag() == DW_TAG_vector_type)
1791 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1792
Devang Patel6ab30e52009-01-28 21:08:20 +00001793 // Emit derived type.
1794 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel6fb54132009-01-05 18:33:01 +00001795 DIArray Elements = CTy->getTypeArray();
Devang Patel6fb54132009-01-05 18:33:01 +00001796
1797 // Construct an anonymous type for index type.
1798 DIE IdxBuffer(DW_TAG_base_type);
1799 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1800 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1801 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1802
1803 // Add subranges to array type.
1804 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel30c01372009-01-05 19:55:51 +00001805 DIDescriptor Element = Elements.getElement(i);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001806 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1807 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel6fb54132009-01-05 18:33:01 +00001808 }
1809 }
1810
Bill Wendling824a8bf2009-02-03 21:17:20 +00001811 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3798f492009-01-20 18:35:14 +00001812 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patela566e812009-01-05 18:38:38 +00001813
1814 DIE *Enumerator = new DIE(DW_TAG_enumerator);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001815 std::string Name;
1816 ETy->getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001817 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
Devang Patela566e812009-01-05 18:38:38 +00001818 int64_t Value = ETy->getEnumValue();
1819 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Patel3798f492009-01-20 18:35:14 +00001820 return Enumerator;
Devang Patela566e812009-01-05 18:38:38 +00001821 }
Devang Patel6fb54132009-01-05 18:33:01 +00001822
Devang Patelb28de842009-01-17 08:01:33 +00001823 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendling824a8bf2009-02-03 21:17:20 +00001824 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patelb28de842009-01-17 08:01:33 +00001825 {
1826 DIE *GVDie = new DIE(DW_TAG_variable);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001827 std::string Name;
1828 GV.getDisplayName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001829 AddString(GVDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001830 std::string LinkageName;
1831 GV.getLinkageName(LinkageName);
Devang Patel526b01d2009-01-05 18:59:44 +00001832 if (!LinkageName.empty())
Devang Patelb28de842009-01-17 08:01:33 +00001833 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1834 AddType(DW_Unit, GVDie, GV.getType());
1835 if (!GV.isLocalToUnit())
1836 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
1837 AddSourceLine(GVDie, &GV);
1838 return GVDie;
Devang Patel526b01d2009-01-05 18:59:44 +00001839 }
1840
Devang Patel5c643892009-01-20 21:02:02 +00001841 /// CreateMemberDIE - Create new member DIE.
1842 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
1843 DIE *MemberDie = new DIE(DT.getTag());
Bill Wendling1c5842b2009-03-09 05:04:40 +00001844 std::string Name;
1845 DT.getName(Name);
Devang Patel5c643892009-01-20 21:02:02 +00001846 if (!Name.empty())
1847 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1848
1849 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1850
1851 AddSourceLine(MemberDie, &DT);
1852
Devang Patelf1f30d42009-02-17 21:23:59 +00001853 uint64_t Size = DT.getSizeInBits();
1854 uint64_t FieldSize = DT.getOriginalTypeSize();
1855
1856 if (Size != FieldSize) {
1857 // Handle bitfield.
1858 AddUInt(MemberDie, DW_AT_byte_size, 0, DT.getOriginalTypeSize() >> 3);
1859 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1860
1861 uint64_t Offset = DT.getOffsetInBits();
1862 uint64_t FieldOffset = Offset;
1863 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1864 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1865 FieldOffset = (HiMark - FieldSize);
1866 Offset -= FieldOffset;
1867 // Maybe we need to work from the other end.
1868 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1869 AddUInt(MemberDie, DW_AT_bit_offset, 0, Offset);
1870 }
Devang Patel5c643892009-01-20 21:02:02 +00001871 DIEBlock *Block = new DIEBlock();
1872 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1873 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1874 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1875
Devang Patel2e7ee192009-01-21 00:08:04 +00001876 if (DT.isProtected())
1877 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1878 else if (DT.isPrivate())
1879 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1880
Devang Patel5c643892009-01-20 21:02:02 +00001881 return MemberDie;
1882 }
1883
Devang Patelb28de842009-01-17 08:01:33 +00001884 /// CreateSubprogramDIE - Create new DIE using SP.
1885 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Devang Patel245446c2009-01-17 08:05:14 +00001886 const DISubprogram &SP,
1887 bool IsConstructor = false) {
Devang Patelb28de842009-01-17 08:01:33 +00001888 DIE *SPDie = new DIE(DW_TAG_subprogram);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001889 std::string Name;
1890 SP.getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001891 AddString(SPDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001892 std::string LinkageName;
1893 SP.getLinkageName(LinkageName);
Devang Patel526b01d2009-01-05 18:59:44 +00001894 if (!LinkageName.empty())
Devang Patelb28de842009-01-17 08:01:33 +00001895 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Devang Patel245446c2009-01-17 08:05:14 +00001896 LinkageName);
Devang Patelb28de842009-01-17 08:01:33 +00001897 AddSourceLine(SPDie, &SP);
Devang Patel526b01d2009-01-05 18:59:44 +00001898
Devang Patelb28de842009-01-17 08:01:33 +00001899 DICompositeType SPTy = SP.getType();
1900 DIArray Args = SPTy.getTypeArray();
1901
Devang Patel526b01d2009-01-05 18:59:44 +00001902 // Add Return Type.
Devang Patel688a19f2009-02-27 18:05:21 +00001903 if (!IsConstructor) {
1904 if (Args.isNull())
1905 AddType(DW_Unit, SPDie, SPTy);
1906 else
1907 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
1908 }
Devang Patel922d1592009-01-30 01:21:46 +00001909
Devang Patelace2cf62009-02-02 17:51:41 +00001910 if (!SP.isDefinition()) {
1911 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
1912 // Add arguments.
1913 // Do not add arguments for subprogram definition. They will be
1914 // handled through RecordVariable.
1915 if (!Args.isNull())
1916 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1917 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1918 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
1919 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
1920 SPDie->AddChild(Arg);
1921 }
1922 }
Devang Patel922d1592009-01-30 01:21:46 +00001923
Devang Patele075e072009-02-24 00:52:19 +00001924 unsigned Lang = SP.getCompileUnit().getLanguage();
1925 if (Lang == DW_LANG_C99 || Lang == DW_LANG_C89
1926 || Lang == DW_LANG_ObjC)
1927 AddUInt(SPDie, DW_AT_prototyped, DW_FORM_flag, 1);
1928
Devang Patelef4bf3b2009-01-15 19:26:23 +00001929 if (!SP.isLocalToUnit())
Devang Patel922d1592009-01-30 01:21:46 +00001930 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Devang Patelb28de842009-01-17 08:01:33 +00001931 return SPDie;
Devang Patel526b01d2009-01-05 18:59:44 +00001932 }
1933
Devang Patelb28de842009-01-17 08:01:33 +00001934 /// FindCompileUnit - Get the compile unit for the given descriptor.
1935 ///
Devang Patel5f244e32009-01-05 22:35:52 +00001936 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
Evan Cheng3e288912009-02-25 07:04:34 +00001937 CompileUnit *DW_Unit = CompileUnitMap[Unit.getGV()];
Devang Patel5f244e32009-01-05 22:35:52 +00001938 assert(DW_Unit && "Missing compile unit.");
1939 return DW_Unit;
1940 }
1941
Devang Patel42f6bed2009-01-13 23:54:55 +00001942 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel4d1709e2009-01-08 02:33:41 +00001943 ///
1944 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1945 // Get the descriptor.
Devang Patel7c8a2772009-01-16 19:28:14 +00001946 const DIVariable &VD = DV->getVariable();
Devang Patel4d1709e2009-01-08 02:33:41 +00001947
1948 // Translate tag to proper Dwarf tag. The result variable is dropped for
1949 // now.
1950 unsigned Tag;
Devang Patel7c8a2772009-01-16 19:28:14 +00001951 switch (VD.getTag()) {
Devang Patel4d1709e2009-01-08 02:33:41 +00001952 case DW_TAG_return_variable: return NULL;
1953 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1954 case DW_TAG_auto_variable: // fall thru
1955 default: Tag = DW_TAG_variable; break;
1956 }
1957
1958 // Define variable debug information entry.
1959 DIE *VariableDie = new DIE(Tag);
Bill Wendling1c5842b2009-03-09 05:04:40 +00001960 std::string Name;
1961 VD.getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001962 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Devang Patel4d1709e2009-01-08 02:33:41 +00001963
1964 // Add source line info if available.
Devang Patel7c8a2772009-01-16 19:28:14 +00001965 AddSourceLine(VariableDie, &VD);
Devang Patel4d1709e2009-01-08 02:33:41 +00001966
1967 // Add variable type.
Devang Patel7c8a2772009-01-16 19:28:14 +00001968 AddType(Unit, VariableDie, VD.getType());
Devang Patel4d1709e2009-01-08 02:33:41 +00001969
1970 // Add variable address.
1971 MachineLocation Location;
1972 Location.set(RI->getFrameRegister(*MF),
1973 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
1974 AddAddress(VariableDie, DW_AT_location, Location);
1975
1976 return VariableDie;
1977 }
1978
Devang Patel4d1709e2009-01-08 02:33:41 +00001979 /// getOrCreateScope - Returns the scope associated with the given descriptor.
1980 ///
1981 DbgScope *getOrCreateScope(GlobalVariable *V) {
1982 DbgScope *&Slot = DbgScopeMap[V];
Bill Wendlinge0f3a262009-02-20 20:40:28 +00001983 if (Slot) return Slot;
1984
1985 // FIXME - breaks down when the context is an inlined function.
1986 DIDescriptor ParentDesc;
1987 DIDescriptor Desc(V);
1988
1989 if (Desc.getTag() == dwarf::DW_TAG_lexical_block) {
1990 DIBlock Block(V);
1991 ParentDesc = Block.getContext();
Devang Patel4d1709e2009-01-08 02:33:41 +00001992 }
Bill Wendlinge0f3a262009-02-20 20:40:28 +00001993
1994 DbgScope *Parent = ParentDesc.isNull() ?
1995 NULL : getOrCreateScope(ParentDesc.getGV());
1996 Slot = new DbgScope(Parent, Desc);
1997
1998 if (Parent) {
1999 Parent->AddScope(Slot);
2000 } else if (RootDbgScope) {
2001 // FIXME - Add inlined function scopes to the root so we can delete them
2002 // later. Long term, handle inlined functions properly.
2003 RootDbgScope->AddScope(Slot);
2004 } else {
2005 // First function is top level function.
2006 RootDbgScope = Slot;
2007 }
2008
Devang Patel4d1709e2009-01-08 02:33:41 +00002009 return Slot;
2010 }
2011
2012 /// ConstructDbgScope - Construct the components of a scope.
2013 ///
2014 void ConstructDbgScope(DbgScope *ParentScope,
2015 unsigned ParentStartID, unsigned ParentEndID,
2016 DIE *ParentDie, CompileUnit *Unit) {
2017 // Add variables to scope.
Devang Patel63c22f42009-01-10 02:42:49 +00002018 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
Devang Patel4d1709e2009-01-08 02:33:41 +00002019 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2020 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2021 if (VariableDie) ParentDie->AddChild(VariableDie);
2022 }
2023
2024 // Add nested scopes.
Devang Patel63c22f42009-01-10 02:42:49 +00002025 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel4d1709e2009-01-08 02:33:41 +00002026 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2027 // Define the Scope debug information entry.
2028 DbgScope *Scope = Scopes[j];
2029 // FIXME - Ignore inlined functions for the time being.
2030 if (!Scope->getParent()) continue;
2031
Devang Patelb9224922009-01-12 18:41:00 +00002032 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2033 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel4d1709e2009-01-08 02:33:41 +00002034
2035 // Ignore empty scopes.
2036 if (StartID == EndID && StartID != 0) continue;
2037 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
2038
2039 if (StartID == ParentStartID && EndID == ParentEndID) {
2040 // Just add stuff to the parent scope.
2041 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2042 } else {
2043 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2044
2045 // Add the scope bounds.
2046 if (StartID) {
2047 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2048 DWLabel("label", StartID));
2049 } else {
2050 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2051 DWLabel("func_begin", SubprogramCount));
2052 }
2053 if (EndID) {
2054 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2055 DWLabel("label", EndID));
2056 } else {
2057 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2058 DWLabel("func_end", SubprogramCount));
2059 }
2060
2061 // Add the scope contents.
2062 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2063 ParentDie->AddChild(ScopeDie);
2064 }
2065 }
2066 }
2067
2068 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2069 ///
2070 void ConstructRootDbgScope(DbgScope *RootScope) {
2071 // Exit if there is no root scope.
2072 if (!RootScope) return;
Devang Patel2560d922009-01-15 18:25:17 +00002073 DIDescriptor Desc = RootScope->getDesc();
2074 if (Desc.isNull())
2075 return;
Devang Patel4d1709e2009-01-08 02:33:41 +00002076
2077 // Get the subprogram debug information entry.
Devang Patel2560d922009-01-15 18:25:17 +00002078 DISubprogram SPD(Desc.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002079
2080 // Get the compile unit context.
Devang Patel2ae1db52009-01-30 18:20:31 +00002081 CompileUnit *Unit = MainCU;
2082 if (!Unit)
2083 Unit = FindCompileUnit(SPD.getCompileUnit());
Devang Patel4d1709e2009-01-08 02:33:41 +00002084
2085 // Get the subprogram die.
Devang Patel57ec9ac2009-01-12 22:58:14 +00002086 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002087 assert(SPDie && "Missing subprogram descriptor");
2088
2089 // Add the function bounds.
2090 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2091 DWLabel("func_begin", SubprogramCount));
2092 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2093 DWLabel("func_end", SubprogramCount));
2094 MachineLocation Location(RI->getFrameRegister(*MF));
2095 AddAddress(SPDie, DW_AT_frame_base, Location);
2096
2097 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2098 }
2099
2100 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2101 ///
2102 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Cheng3e288912009-02-25 07:04:34 +00002103 const char *FnName = MF->getFunction()->getNameStart();
2104 if (MainCU) {
2105 std::map<std::string, DIE*> &Globals = MainCU->getGlobals();
2106 std::map<std::string, DIE*>::iterator GI = Globals.find(FnName);
2107 if (GI != Globals.end()) {
2108 DIE *SPDie = GI->second;
Devang Patel4d1709e2009-01-08 02:33:41 +00002109
2110 // Add the function bounds.
2111 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2112 DWLabel("func_begin", SubprogramCount));
2113 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2114 DWLabel("func_end", SubprogramCount));
2115
2116 MachineLocation Location(RI->getFrameRegister(*MF));
2117 AddAddress(SPDie, DW_AT_frame_base, Location);
2118 return;
2119 }
Evan Cheng3e288912009-02-25 07:04:34 +00002120 } else {
2121 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2122 CompileUnit *Unit = CompileUnits[i];
2123 std::map<std::string, DIE*> &Globals = Unit->getGlobals();
2124 std::map<std::string, DIE*>::iterator GI = Globals.find(FnName);
2125 if (GI != Globals.end()) {
2126 DIE *SPDie = GI->second;
2127
2128 // Add the function bounds.
2129 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2130 DWLabel("func_begin", SubprogramCount));
2131 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2132 DWLabel("func_end", SubprogramCount));
2133
2134 MachineLocation Location(RI->getFrameRegister(*MF));
2135 AddAddress(SPDie, DW_AT_frame_base, Location);
2136 return;
2137 }
2138 }
Devang Patel4d1709e2009-01-08 02:33:41 +00002139 }
Evan Cheng3e288912009-02-25 07:04:34 +00002140
Devang Patel4d1709e2009-01-08 02:33:41 +00002141#if 0
2142 // FIXME: This is causing an abort because C++ mangled names are compared
2143 // with their unmangled counterparts. See PR2885. Don't do this assert.
2144 assert(0 && "Couldn't find DIE for machine function!");
2145#endif
Evan Cheng3e288912009-02-25 07:04:34 +00002146 return;
Devang Patel4d1709e2009-01-08 02:33:41 +00002147 }
2148
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002149 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2150 /// tools to recognize the object file contains Dwarf information.
2151 void EmitInitial() {
2152 // Check to see if we already emitted intial headers.
2153 if (didInitial) return;
2154 didInitial = true;
aslc200b112008-08-16 12:57:46 +00002155
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002156 // Dwarf sections base addresses.
2157 if (TAI->doesDwarfRequireFrameSection()) {
2158 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2159 EmitLabel("section_debug_frame", 0);
2160 }
2161 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2162 EmitLabel("section_info", 0);
2163 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2164 EmitLabel("section_abbrev", 0);
2165 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2166 EmitLabel("section_aranges", 0);
Scott Michel79f01f52009-01-26 22:32:51 +00002167 if (TAI->doesSupportMacInfoSection()) {
2168 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2169 EmitLabel("section_macinfo", 0);
2170 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002171 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2172 EmitLabel("section_line", 0);
2173 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2174 EmitLabel("section_loc", 0);
2175 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2176 EmitLabel("section_pubnames", 0);
2177 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2178 EmitLabel("section_str", 0);
2179 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2180 EmitLabel("section_ranges", 0);
2181
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002182 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002183 EmitLabel("text_begin", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00002184 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002185 EmitLabel("data_begin", 0);
2186 }
2187
2188 /// EmitDIE - Recusively Emits a debug information entry.
2189 ///
2190 void EmitDIE(DIE *Die) {
2191 // Get the abbreviation for this DIE.
2192 unsigned AbbrevNumber = Die->getAbbrevNumber();
2193 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
aslc200b112008-08-16 12:57:46 +00002194
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002195 Asm->EOL();
2196
2197 // Emit the code (index) for the abbreviation.
2198 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng0eeed442008-07-01 23:18:29 +00002199
2200 if (VerboseAsm)
2201 Asm->EOL(std::string("Abbrev [" +
2202 utostr(AbbrevNumber) +
2203 "] 0x" + utohexstr(Die->getOffset()) +
2204 ":0x" + utohexstr(Die->getSize()) + " " +
2205 TagString(Abbrev->getTag())));
2206 else
2207 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00002208
Owen Anderson88dd6232008-06-24 21:44:59 +00002209 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2210 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
aslc200b112008-08-16 12:57:46 +00002211
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002212 // Emit the DIE attribute values.
2213 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2214 unsigned Attr = AbbrevData[i].getAttribute();
2215 unsigned Form = AbbrevData[i].getForm();
2216 assert(Form && "Too many attributes for DIE (check abbreviation)");
aslc200b112008-08-16 12:57:46 +00002217
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002218 switch (Attr) {
2219 case DW_AT_sibling: {
2220 Asm->EmitInt32(Die->SiblingOffset());
2221 break;
2222 }
2223 default: {
2224 // Emit an attribute using the defined form.
2225 Values[i]->EmitValue(*this, Form);
2226 break;
2227 }
2228 }
aslc200b112008-08-16 12:57:46 +00002229
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002230 Asm->EOL(AttributeString(Attr));
2231 }
aslc200b112008-08-16 12:57:46 +00002232
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002233 // Emit the DIE children if any.
2234 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2235 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002236
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002237 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2238 EmitDIE(Children[j]);
2239 }
aslc200b112008-08-16 12:57:46 +00002240
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002241 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2242 }
2243 }
2244
2245 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2246 ///
2247 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2248 // Get the children.
2249 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002250
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002251 // If not last sibling and has children then add sibling offset attribute.
2252 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2253
2254 // Record the abbreviation.
2255 AssignAbbrevNumber(Die->getAbbrev());
aslc200b112008-08-16 12:57:46 +00002256
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002257 // Get the abbreviation for this DIE.
2258 unsigned AbbrevNumber = Die->getAbbrevNumber();
2259 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2260
2261 // Set DIE offset
2262 Die->setOffset(Offset);
aslc200b112008-08-16 12:57:46 +00002263
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002264 // Start the size with the size of abbreviation code.
aslc200b112008-08-16 12:57:46 +00002265 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2266
Owen Anderson88dd6232008-06-24 21:44:59 +00002267 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2268 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002269
2270 // Size the DIE attribute values.
2271 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2272 // Size attribute value.
2273 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2274 }
aslc200b112008-08-16 12:57:46 +00002275
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002276 // Size the DIE children if any.
2277 if (!Children.empty()) {
2278 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2279 "Children flag not set");
aslc200b112008-08-16 12:57:46 +00002280
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002281 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2282 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2283 }
aslc200b112008-08-16 12:57:46 +00002284
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002285 // End of children marker.
2286 Offset += sizeof(int8_t);
2287 }
2288
2289 Die->setSize(Offset - Die->getOffset());
2290 return Offset;
2291 }
2292
2293 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2294 ///
2295 void SizeAndOffsets() {
2296 // Process base compile unit.
Devang Patel2ae1db52009-01-30 18:20:31 +00002297 if (MainCU) {
2298 // Compute size of compile unit header
2299 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2300 sizeof(int16_t) + // DWARF version number
2301 sizeof(int32_t) + // Offset Into Abbrev. Section
2302 sizeof(int8_t); // Pointer Size (in bytes)
2303 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
2304 return;
2305 }
Evan Cheng3e288912009-02-25 07:04:34 +00002306 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2307 CompileUnit *Unit = CompileUnits[i];
Devang Patel6eae2832009-01-12 23:05:55 +00002308 // Compute size of compile unit header
2309 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2310 sizeof(int16_t) + // DWARF version number
2311 sizeof(int32_t) + // Offset Into Abbrev. Section
2312 sizeof(int8_t); // Pointer Size (in bytes)
2313 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2314 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002315 }
2316
Evan Cheng3e288912009-02-25 07:04:34 +00002317 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002318 ///
Evan Cheng3e288912009-02-25 07:04:34 +00002319 void EmitDebugInfoPerCU(CompileUnit *Unit) {
2320 DIE *Die = Unit->getDie();
2321 // Emit the compile units header.
2322 EmitLabel("info_begin", Unit->getID());
2323 // Emit size of content not including length itself
2324 unsigned ContentSize = Die->getSize() +
2325 sizeof(int16_t) + // DWARF version number
2326 sizeof(int32_t) + // Offset Into Abbrev. Section
2327 sizeof(int8_t) + // Pointer Size (in bytes)
2328 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2329
2330 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2331 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2332 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2333 Asm->EOL("Offset Into Abbrev. Section");
2334 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2335
2336 EmitDIE(Die);
2337 // FIXME - extra padding for gdb bug.
2338 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2339 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2340 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2341 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2342 EmitLabel("info_end", Unit->getID());
2343
2344 Asm->EOL();
2345 }
2346
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002347 void EmitDebugInfo() {
2348 // Start debug info section.
2349 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
aslc200b112008-08-16 12:57:46 +00002350
Evan Cheng3e288912009-02-25 07:04:34 +00002351 if (MainCU) {
2352 EmitDebugInfoPerCU(MainCU);
2353 return;
Devang Patel6eae2832009-01-12 23:05:55 +00002354 }
Evan Cheng3e288912009-02-25 07:04:34 +00002355
2356 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2357 EmitDebugInfoPerCU(CompileUnits[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002358 }
2359
2360 /// EmitAbbreviations - Emit the abbreviation section.
2361 ///
2362 void EmitAbbreviations() const {
2363 // Check to see if it is worth the effort.
2364 if (!Abbreviations.empty()) {
2365 // Start the debug abbrev section.
2366 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
aslc200b112008-08-16 12:57:46 +00002367
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002368 EmitLabel("abbrev_begin", 0);
aslc200b112008-08-16 12:57:46 +00002369
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002370 // For each abbrevation.
2371 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2372 // Get abbreviation data
2373 const DIEAbbrev *Abbrev = Abbreviations[i];
aslc200b112008-08-16 12:57:46 +00002374
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002375 // Emit the abbrevations code (base 1 index.)
2376 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2377 Asm->EOL("Abbreviation Code");
aslc200b112008-08-16 12:57:46 +00002378
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002379 // Emit the abbreviations data.
2380 Abbrev->Emit(*this);
aslc200b112008-08-16 12:57:46 +00002381
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002382 Asm->EOL();
2383 }
aslc200b112008-08-16 12:57:46 +00002384
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002385 // Mark end of abbreviations.
2386 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2387
2388 EmitLabel("abbrev_end", 0);
aslc200b112008-08-16 12:57:46 +00002389
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002390 Asm->EOL();
2391 }
2392 }
2393
Bill Wendling1983a2a2008-07-20 00:11:19 +00002394 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2395 /// the line matrix.
aslc200b112008-08-16 12:57:46 +00002396 ///
Bill Wendling1983a2a2008-07-20 00:11:19 +00002397 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2398 // Define last address of section.
2399 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2400 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2401 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2402 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2403
2404 // Mark end of matrix.
2405 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2406 Asm->EmitULEB128Bytes(1); Asm->EOL();
2407 Asm->EmitInt8(1); Asm->EOL();
2408 }
2409
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002410 /// EmitDebugLines - Emit source line information.
2411 ///
2412 void EmitDebugLines() {
Bill Wendling1983a2a2008-07-20 00:11:19 +00002413 // If the target is using .loc/.file, the assembler will be emitting the
2414 // .debug_line table automatically.
2415 if (TAI->hasDotLocAndDotFile())
Dan Gohmanc55b34a2007-09-24 21:43:52 +00002416 return;
2417
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002418 // Minimum line delta, thus ranging from -10..(255-10).
2419 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2420 // Maximum line delta, thus ranging from -10..(255-10).
2421 const int MaxLineDelta = 255 + MinLineDelta;
2422
2423 // Start the dwarf line section.
2424 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
aslc200b112008-08-16 12:57:46 +00002425
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002426 // Construct the section header.
aslc200b112008-08-16 12:57:46 +00002427
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002428 EmitDifference("line_end", 0, "line_begin", 0, true);
2429 Asm->EOL("Length of Source Line Info");
2430 EmitLabel("line_begin", 0);
aslc200b112008-08-16 12:57:46 +00002431
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002432 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
aslc200b112008-08-16 12:57:46 +00002433
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002434 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2435 Asm->EOL("Prolog Length");
2436 EmitLabel("line_prolog_begin", 0);
aslc200b112008-08-16 12:57:46 +00002437
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002438 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2439
2440 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2441
2442 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
aslc200b112008-08-16 12:57:46 +00002443
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002444 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2445
2446 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
aslc200b112008-08-16 12:57:46 +00002447
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002448 // Line number standard opcode encodings argument count
2449 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2450 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2451 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2452 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2453 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2454 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2455 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2456 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2457 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2458
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002459 // Emit directories.
Evan Cheng3e288912009-02-25 07:04:34 +00002460 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2461 Asm->EmitString(getSourceDirectoryName(DI));
2462 Asm->EOL("Directory");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002463 }
2464 Asm->EmitInt8(0); Asm->EOL("End of directories");
aslc200b112008-08-16 12:57:46 +00002465
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002466 // Emit files.
Evan Cheng3e288912009-02-25 07:04:34 +00002467 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2468 // Remember source id starts at 1.
2469 std::pair<unsigned, unsigned> Id = getSourceDirsectoryAndFileIds(SI);
2470 Asm->EmitString(getSourceFileName(Id.second));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002471 Asm->EOL("Source");
Evan Cheng3e288912009-02-25 07:04:34 +00002472 Asm->EmitULEB128Bytes(Id.first);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002473 Asm->EOL("Directory #");
2474 Asm->EmitULEB128Bytes(0);
2475 Asm->EOL("Mod date");
2476 Asm->EmitULEB128Bytes(0);
2477 Asm->EOL("File size");
2478 }
2479 Asm->EmitInt8(0); Asm->EOL("End of files");
aslc200b112008-08-16 12:57:46 +00002480
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002481 EmitLabel("line_prolog_end", 0);
aslc200b112008-08-16 12:57:46 +00002482
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002483 // A sequence for each text section.
Bill Wendling1983a2a2008-07-20 00:11:19 +00002484 unsigned SecSrcLinesSize = SectionSourceLines.size();
2485
2486 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002487 // Isolate current sections line info.
Devang Patel35a078f2009-01-12 22:54:42 +00002488 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng0eeed442008-07-01 23:18:29 +00002489
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002490 if (VerboseAsm) {
2491 const Section* S = SectionMap[j + 1];
Evan Cheng3e288912009-02-25 07:04:34 +00002492 O << '\t' << TAI->getCommentString() << " Section"
2493 << S->getName() << '\n';
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002494 } else
Evan Cheng0eeed442008-07-01 23:18:29 +00002495 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002496
2497 // Dwarf assumes we start with first line of first source file.
2498 unsigned Source = 1;
2499 unsigned Line = 1;
aslc200b112008-08-16 12:57:46 +00002500
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002501 // Construct rows of the address, source, line, column matrix.
2502 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patel35a078f2009-01-12 22:54:42 +00002503 const SrcLineInfo &LineInfo = LineInfos[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002504 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2505 if (!LabelID) continue;
aslc200b112008-08-16 12:57:46 +00002506
Evan Cheng3e288912009-02-25 07:04:34 +00002507 if (!VerboseAsm)
Evan Cheng0eeed442008-07-01 23:18:29 +00002508 Asm->EOL();
Evan Cheng3e288912009-02-25 07:04:34 +00002509 else {
2510 std::pair<unsigned, unsigned> SourceID =
2511 getSourceDirsectoryAndFileIds(LineInfo.getSourceID());
2512 O << '\t' << TAI->getCommentString() << ' '
2513 << getSourceDirectoryName(SourceID.first) << ' '
2514 << getSourceFileName(SourceID.second)
2515 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2516 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002517
2518 // Define the line address.
2519 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohmancfb72b22007-09-27 23:12:31 +00002520 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002521 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2522 EmitReference("label", LabelID); Asm->EOL("Location label");
aslc200b112008-08-16 12:57:46 +00002523
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002524 // If change of source, then switch to the new source.
2525 if (Source != LineInfo.getSourceID()) {
2526 Source = LineInfo.getSourceID();
2527 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2528 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2529 }
aslc200b112008-08-16 12:57:46 +00002530
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002531 // If change of line.
2532 if (Line != LineInfo.getLine()) {
2533 // Determine offset.
2534 int Offset = LineInfo.getLine() - Line;
2535 int Delta = Offset - MinLineDelta;
aslc200b112008-08-16 12:57:46 +00002536
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002537 // Update line.
2538 Line = LineInfo.getLine();
aslc200b112008-08-16 12:57:46 +00002539
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002540 // If delta is small enough and in range...
2541 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2542 // ... then use fast opcode.
2543 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2544 } else {
2545 // ... otherwise use long hand.
2546 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2547 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2548 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2549 }
2550 } else {
2551 // Copy the previous row (different address or source)
2552 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2553 }
2554 }
2555
Bill Wendling1983a2a2008-07-20 00:11:19 +00002556 EmitEndOfLineMatrix(j + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002557 }
Bill Wendling1983a2a2008-07-20 00:11:19 +00002558
2559 if (SecSrcLinesSize == 0)
2560 // Because we're emitting a debug_line section, we still need a line
2561 // table. The linker and friends expect it to exist. If there's nothing to
2562 // put into it, emit an empty table.
2563 EmitEndOfLineMatrix(1);
aslc200b112008-08-16 12:57:46 +00002564
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002565 EmitLabel("line_end", 0);
aslc200b112008-08-16 12:57:46 +00002566
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002567 Asm->EOL();
2568 }
aslc200b112008-08-16 12:57:46 +00002569
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002570 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2571 ///
2572 void EmitCommonDebugFrame() {
2573 if (!TAI->doesDwarfRequireFrameSection())
2574 return;
2575
2576 int stackGrowth =
2577 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2578 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00002579 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002580
2581 // Start the dwarf frame section.
2582 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2583
2584 EmitLabel("debug_frame_common", 0);
2585 EmitDifference("debug_frame_common_end", 0,
2586 "debug_frame_common_begin", 0, true);
2587 Asm->EOL("Length of Common Information Entry");
2588
2589 EmitLabel("debug_frame_common_begin", 0);
2590 Asm->EmitInt32((int)DW_CIE_ID);
2591 Asm->EOL("CIE Identifier Tag");
2592 Asm->EmitInt8(DW_CIE_VERSION);
2593 Asm->EOL("CIE Version");
2594 Asm->EmitString("");
2595 Asm->EOL("CIE Augmentation");
2596 Asm->EmitULEB128Bytes(1);
2597 Asm->EOL("CIE Code Alignment Factor");
2598 Asm->EmitSLEB128Bytes(stackGrowth);
aslc200b112008-08-16 12:57:46 +00002599 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00002600 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002601 Asm->EOL("CIE RA Column");
aslc200b112008-08-16 12:57:46 +00002602
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002603 std::vector<MachineMove> Moves;
2604 RI->getInitialFrameState(Moves);
2605
Dale Johannesenf5a11532007-11-13 19:13:01 +00002606 EmitFrameMoves(NULL, 0, Moves, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002607
Evan Cheng7e7d1942008-02-29 19:36:59 +00002608 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002609 EmitLabel("debug_frame_common_end", 0);
aslc200b112008-08-16 12:57:46 +00002610
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002611 Asm->EOL();
2612 }
2613
2614 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2615 /// section.
2616 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
2617 if (!TAI->doesDwarfRequireFrameSection())
2618 return;
aslc200b112008-08-16 12:57:46 +00002619
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002620 // Start the dwarf frame section.
2621 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
aslc200b112008-08-16 12:57:46 +00002622
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002623 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2624 "debug_frame_begin", DebugFrameInfo.Number, true);
2625 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00002626
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002627 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2628
2629 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2630 0, 0, true, false);
2631 Asm->EOL("FDE CIE offset");
2632
2633 EmitReference("func_begin", DebugFrameInfo.Number);
2634 Asm->EOL("FDE initial location");
2635 EmitDifference("func_end", DebugFrameInfo.Number,
2636 "func_begin", DebugFrameInfo.Number);
2637 Asm->EOL("FDE address range");
aslc200b112008-08-16 12:57:46 +00002638
Devang Patelb28de842009-01-17 08:01:33 +00002639 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00002640 false);
aslc200b112008-08-16 12:57:46 +00002641
Evan Cheng7e7d1942008-02-29 19:36:59 +00002642 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002643 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2644
2645 Asm->EOL();
2646 }
2647
Evan Cheng3e288912009-02-25 07:04:34 +00002648 void EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2649 EmitDifference("pubnames_end", Unit->getID(),
2650 "pubnames_begin", Unit->getID(), true);
2651 Asm->EOL("Length of Public Names Info");
2652
2653 EmitLabel("pubnames_begin", Unit->getID());
2654
2655 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2656
2657 EmitSectionOffset("info_begin", "section_info",
2658 Unit->getID(), 0, true, false);
2659 Asm->EOL("Offset of Compilation Unit Info");
2660
2661 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2662 true);
2663 Asm->EOL("Compilation Unit Length");
2664
2665 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2666 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2667 GE = Globals.end(); GI != GE; ++GI) {
2668 const std::string &Name = GI->first;
2669 DIE * Entity = GI->second;
2670
2671 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2672 Asm->EmitString(Name); Asm->EOL("External Name");
2673 }
2674
2675 Asm->EmitInt32(0); Asm->EOL("End Mark");
2676 EmitLabel("pubnames_end", Unit->getID());
2677
2678 Asm->EOL();
2679 }
2680
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002681 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2682 ///
2683 void EmitDebugPubNames() {
2684 // Start the dwarf pubnames section.
2685 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
aslc200b112008-08-16 12:57:46 +00002686
Evan Cheng3e288912009-02-25 07:04:34 +00002687 if (MainCU) {
2688 EmitDebugPubNamesPerCU(MainCU);
2689 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002690 }
Evan Cheng3e288912009-02-25 07:04:34 +00002691
2692 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2693 EmitDebugPubNamesPerCU(CompileUnits[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002694 }
2695
2696 /// EmitDebugStr - Emit visible names into a debug str section.
2697 ///
2698 void EmitDebugStr() {
2699 // Check to see if it is worth the effort.
2700 if (!StringPool.empty()) {
2701 // Start the dwarf str section.
2702 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
aslc200b112008-08-16 12:57:46 +00002703
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002704 // For each of strings in the string pool.
2705 for (unsigned StringID = 1, N = StringPool.size();
2706 StringID <= N; ++StringID) {
2707 // Emit a label for reference from debug information entries.
2708 EmitLabel("string", StringID);
2709 // Emit the string itself.
2710 const std::string &String = StringPool[StringID];
2711 Asm->EmitString(String); Asm->EOL();
2712 }
aslc200b112008-08-16 12:57:46 +00002713
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002714 Asm->EOL();
2715 }
2716 }
2717
2718 /// EmitDebugLoc - Emit visible names into a debug loc section.
2719 ///
2720 void EmitDebugLoc() {
2721 // Start the dwarf loc section.
2722 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
aslc200b112008-08-16 12:57:46 +00002723
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002724 Asm->EOL();
2725 }
2726
2727 /// EmitDebugARanges - Emit visible names into a debug aranges section.
2728 ///
2729 void EmitDebugARanges() {
2730 // Start the dwarf aranges section.
2731 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
aslc200b112008-08-16 12:57:46 +00002732
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002733 // FIXME - Mock up
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002734#if 0
aslc200b112008-08-16 12:57:46 +00002735 CompileUnit *Unit = GetBaseCompileUnit();
2736
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002737 // Don't include size of length
2738 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
aslc200b112008-08-16 12:57:46 +00002739
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002740 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
aslc200b112008-08-16 12:57:46 +00002741
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002742 EmitReference("info_begin", Unit->getID());
2743 Asm->EOL("Offset of Compilation Unit Info");
2744
Dan Gohmancfb72b22007-09-27 23:12:31 +00002745 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002746
2747 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2748
2749 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2750 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2751
2752 // Range 1
2753 EmitReference("text_begin", 0); Asm->EOL("Address");
2754 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2755
2756 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2757 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002758#endif
aslc200b112008-08-16 12:57:46 +00002759
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002760 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002761 }
2762
2763 /// EmitDebugRanges - Emit visible names into a debug ranges section.
2764 ///
2765 void EmitDebugRanges() {
2766 // Start the dwarf ranges section.
2767 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
aslc200b112008-08-16 12:57:46 +00002768
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002769 Asm->EOL();
2770 }
2771
2772 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2773 ///
2774 void EmitDebugMacInfo() {
Scott Michel79f01f52009-01-26 22:32:51 +00002775 if (TAI->doesSupportMacInfoSection()) {
2776 // Start the dwarf macinfo section.
2777 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
aslc200b112008-08-16 12:57:46 +00002778
Scott Michel79f01f52009-01-26 22:32:51 +00002779 Asm->EOL();
2780 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002781 }
2782
Evan Cheng3e288912009-02-25 07:04:34 +00002783 void ConstructCompileUnit(GlobalVariable *GV) {
2784 DICompileUnit DIUnit(GV);
Bill Wendling1c5842b2009-03-09 05:04:40 +00002785 std::string Dir, FN, Prod;
2786 unsigned ID = getOrCreateSourceID(DIUnit.getDirectory(Dir),
2787 DIUnit.getFilename(FN));
Evan Cheng3e288912009-02-25 07:04:34 +00002788
2789 DIE *Die = new DIE(DW_TAG_compile_unit);
2790 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2791 DWLabel("section_line", 0), DWLabel("section_line", 0),
2792 false);
Bill Wendling1c5842b2009-03-09 05:04:40 +00002793 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer(Prod));
Evan Cheng3e288912009-02-25 07:04:34 +00002794 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendling1c5842b2009-03-09 05:04:40 +00002795 AddString(Die, DW_AT_name, DW_FORM_string, FN);
2796 if (!Dir.empty())
2797 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Cheng3e288912009-02-25 07:04:34 +00002798 if (DIUnit.isOptimized())
2799 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendling1c5842b2009-03-09 05:04:40 +00002800 std::string Flags;
2801 DIUnit.getFlags(Flags);
Evan Cheng3e288912009-02-25 07:04:34 +00002802 if (!Flags.empty())
2803 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
2804 unsigned RVer = DIUnit.getRunTimeVersion();
2805 if (RVer)
2806 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
2807
2808 CompileUnit *Unit = new CompileUnit(ID, Die);
2809 if (DIUnit.isMain()) {
2810 assert(!MainCU && "Multiple main compile units are found!");
2811 MainCU = Unit;
2812 }
2813 CompileUnitMap[DIUnit.getGV()] = Unit;
2814 CompileUnits.push_back(Unit);
2815 }
2816
Devang Patel289f2362009-01-05 23:11:11 +00002817 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Patelb3907da2009-01-05 23:03:32 +00002818 void ConstructCompileUnits() {
Evan Cheng3e288912009-02-25 07:04:34 +00002819 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.compile_units");
2820 if (!Root)
2821 return;
2822 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2823 "Malformed compile unit descriptor anchor type");
2824 Constant *RootC = cast<Constant>(*Root->use_begin());
2825 assert(RootC->hasNUsesOrMore(1) &&
2826 "Malformed compile unit descriptor anchor type");
2827 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2828 UI != UE; ++UI)
2829 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2830 UUI != UUE; ++UUI) {
2831 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2832 ConstructCompileUnit(GV);
Devang Patel2ae1db52009-01-30 18:20:31 +00002833 }
Evan Cheng3e288912009-02-25 07:04:34 +00002834 }
2835
2836 bool ConstructGlobalVariableDIE(GlobalVariable *GV) {
2837 DIGlobalVariable DI_GV(GV);
2838 CompileUnit *DW_Unit = MainCU;
2839 if (!DW_Unit)
2840 DW_Unit = FindCompileUnit(DI_GV.getCompileUnit());
2841
2842 // Check for pre-existence.
2843 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
2844 if (Slot)
2845 return false;
2846
2847 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
2848
2849 // Add address.
2850 DIEBlock *Block = new DIEBlock();
2851 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
2852 AddObjectLabel(Block, 0, DW_FORM_udata,
2853 Asm->getGlobalLinkName(DI_GV.getGlobal()));
2854 AddBlock(VariableDie, DW_AT_location, 0, Block);
2855
2856 // Add to map.
2857 Slot = VariableDie;
2858 // Add to context owner.
2859 DW_Unit->getDie()->AddChild(VariableDie);
2860 // Expose as global. FIXME - need to check external flag.
Bill Wendling1c5842b2009-03-09 05:04:40 +00002861 std::string Name;
2862 DW_Unit->AddGlobal(DI_GV.getName(Name), VariableDie);
Evan Cheng3e288912009-02-25 07:04:34 +00002863 return true;
Devang Patelb3907da2009-01-05 23:03:32 +00002864 }
2865
Devang Patel289f2362009-01-05 23:11:11 +00002866 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patela9169c32009-02-24 00:02:15 +00002867 /// visible global variables. Return true if at least one global DIE is
2868 /// created.
2869 bool ConstructGlobalVariableDIEs() {
Evan Cheng3e288912009-02-25 07:04:34 +00002870 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
2871 if (!Root)
2872 return false;
Devang Patel289f2362009-01-05 23:11:11 +00002873
Evan Cheng3e288912009-02-25 07:04:34 +00002874 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2875 "Malformed global variable descriptor anchor type");
2876 Constant *RootC = cast<Constant>(*Root->use_begin());
2877 assert(RootC->hasNUsesOrMore(1) &&
2878 "Malformed global variable descriptor anchor type");
Devang Patel289f2362009-01-05 23:11:11 +00002879
Evan Cheng3e288912009-02-25 07:04:34 +00002880 bool Result = false;
2881 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2882 UI != UE; ++UI)
2883 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2884 UUI != UUE; ++UUI) {
2885 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2886 Result |= ConstructGlobalVariableDIE(GV);
2887 }
2888 return Result;
2889 }
Devang Patel289f2362009-01-05 23:11:11 +00002890
Evan Cheng3e288912009-02-25 07:04:34 +00002891 bool ConstructSubprogram(GlobalVariable *GV) {
2892 DISubprogram SP(GV);
2893 CompileUnit *Unit = MainCU;
2894 if (!Unit)
2895 Unit = FindCompileUnit(SP.getCompileUnit());
Devang Patel289f2362009-01-05 23:11:11 +00002896
Evan Cheng3e288912009-02-25 07:04:34 +00002897 // Check for pre-existence.
2898 DIE *&Slot = Unit->getDieMapSlotFor(GV);
2899 if (Slot)
2900 return false;
2901
2902 if (!SP.isDefinition())
2903 // This is a method declaration which will be handled while
2904 // constructing class type.
2905 return false;
2906
2907 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
2908
2909 // Add to map.
2910 Slot = SubprogramDie;
2911 // Add to context owner.
2912 Unit->getDie()->AddChild(SubprogramDie);
2913 // Expose as global.
Bill Wendling1c5842b2009-03-09 05:04:40 +00002914 std::string Name;
2915 Unit->AddGlobal(SP.getName(Name), SubprogramDie);
Evan Cheng3e288912009-02-25 07:04:34 +00002916 return true;
Devang Patel289f2362009-01-05 23:11:11 +00002917 }
2918
Devang Patele6caf012009-01-05 23:21:35 +00002919 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patela9169c32009-02-24 00:02:15 +00002920 /// subprograms. Return true if at least one subprogram DIE is created.
2921 bool ConstructSubprograms() {
Evan Cheng3e288912009-02-25 07:04:34 +00002922 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
2923 if (!Root)
2924 return false;
Devang Patele6caf012009-01-05 23:21:35 +00002925
Evan Cheng3e288912009-02-25 07:04:34 +00002926 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2927 "Malformed subprogram descriptor anchor type");
2928 Constant *RootC = cast<Constant>(*Root->use_begin());
2929 assert(RootC->hasNUsesOrMore(1) &&
2930 "Malformed subprogram descriptor anchor type");
Devang Patele6caf012009-01-05 23:21:35 +00002931
Evan Cheng3e288912009-02-25 07:04:34 +00002932 bool Result = false;
2933 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2934 UI != UE; ++UI)
2935 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2936 UUI != UUE; ++UUI) {
2937 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2938 Result |= ConstructSubprogram(GV);
2939 }
2940 return Result;
Devang Patele6caf012009-01-05 23:21:35 +00002941 }
2942
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002943public:
2944 //===--------------------------------------------------------------------===//
2945 // Main entry points.
2946 //
Owen Anderson847b99b2008-08-21 00:14:44 +00002947 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattnerb3876c72007-09-24 03:35:37 +00002948 : Dwarf(OS, A, T, "dbg")
Devang Patel2ae1db52009-01-30 18:20:31 +00002949 , MainCU(NULL)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002950 , AbbreviationsSet(InitAbbreviationsSetSize)
2951 , Abbreviations()
2952 , ValuesSet(InitValuesSetSize)
2953 , Values()
2954 , StringPool()
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002955 , SectionMap()
2956 , SectionSourceLines()
2957 , didInitial(false)
2958 , shouldEmit(false)
Devang Patel4d1709e2009-01-08 02:33:41 +00002959 , RootDbgScope(NULL)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002960 {
2961 }
2962 virtual ~DwarfDebug() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002963 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2964 delete Values[j];
2965 }
2966
Devang Patel9304b382009-01-06 21:07:30 +00002967 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
2968 /// This is inovked by the target AsmPrinter.
Devang Patel91d27b02009-01-12 23:09:42 +00002969 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendling6baa18d2009-02-03 21:38:21 +00002970 // Create all the compile unit DIEs.
2971 ConstructCompileUnits();
Devang Patel91d27b02009-01-12 23:09:42 +00002972
Evan Cheng3e288912009-02-25 07:04:34 +00002973 if (CompileUnits.empty())
Bill Wendling6baa18d2009-02-03 21:38:21 +00002974 return;
Devang Patel91d27b02009-01-12 23:09:42 +00002975
Devang Patela9169c32009-02-24 00:02:15 +00002976 // Create DIEs for each of the externally visible global variables.
2977 bool globalDIEs = ConstructGlobalVariableDIEs();
2978
2979 // Create DIEs for each of the externally visible subprograms.
2980 bool subprogramDIEs = ConstructSubprograms();
2981
2982 // If there is not any debug info available for any global variables
2983 // and any subprograms then there is not any debug info to emit.
2984 if (!globalDIEs && !subprogramDIEs)
2985 return;
2986
Bill Wendling6baa18d2009-02-03 21:38:21 +00002987 MMI = mmi;
2988 shouldEmit = true;
2989 MMI->setDebugInfoAvailability(true);
Devang Patel9304b382009-01-06 21:07:30 +00002990
Bill Wendling6baa18d2009-02-03 21:38:21 +00002991 // Prime section data.
2992 SectionMap.insert(TAI->getTextSection());
Devang Patel9304b382009-01-06 21:07:30 +00002993
Bill Wendling6baa18d2009-02-03 21:38:21 +00002994 // Print out .file directives to specify files for .loc directives. These
2995 // are printed out early so that they precede any .loc directives.
2996 if (TAI->hasDotLocAndDotFile()) {
Evan Cheng3e288912009-02-25 07:04:34 +00002997 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
2998 // Remember source id starts at 1.
2999 std::pair<unsigned, unsigned> Id = getSourceDirsectoryAndFileIds(i);
3000 sys::Path FullPath(getSourceDirectoryName(Id.first));
3001 bool AppendOk =
3002 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendling6baa18d2009-02-03 21:38:21 +00003003 assert(AppendOk && "Could not append filename to directory!");
3004 AppendOk = false;
3005 Asm->EmitFile(i, FullPath.toString());
3006 Asm->EOL();
Devang Patel9304b382009-01-06 21:07:30 +00003007 }
Bill Wendling6baa18d2009-02-03 21:38:21 +00003008 }
Devang Patel9304b382009-01-06 21:07:30 +00003009
Bill Wendling6baa18d2009-02-03 21:38:21 +00003010 // Emit initial sections
3011 EmitInitial();
Devang Patel9304b382009-01-06 21:07:30 +00003012 }
3013
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003014 /// BeginModule - Emit all Dwarf sections that should come prior to the
3015 /// content.
3016 void BeginModule(Module *M) {
3017 this->M = M;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003018 }
3019
3020 /// EndModule - Emit all Dwarf sections that should come after the content.
3021 ///
3022 void EndModule() {
Bill Wendling50db0792009-02-20 00:44:43 +00003023 if (!ShouldEmitDwarfDebug()) return;
aslc200b112008-08-16 12:57:46 +00003024
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003025 // Standard sections final addresses.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003026 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003027 EmitLabel("text_end", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00003028 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003029 EmitLabel("data_end", 0);
aslc200b112008-08-16 12:57:46 +00003030
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003031 // End text sections.
3032 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003033 Asm->SwitchToSection(SectionMap[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003034 EmitLabel("section_end", i);
3035 }
3036
3037 // Emit common frame information.
3038 EmitCommonDebugFrame();
3039
3040 // Emit function debug frame information
3041 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3042 E = DebugFrames.end(); I != E; ++I)
3043 EmitFunctionDebugFrame(*I);
3044
3045 // Compute DIE offsets and sizes.
3046 SizeAndOffsets();
aslc200b112008-08-16 12:57:46 +00003047
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003048 // Emit all the DIEs into a debug info section
3049 EmitDebugInfo();
aslc200b112008-08-16 12:57:46 +00003050
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003051 // Corresponding abbreviations into a abbrev section.
3052 EmitAbbreviations();
aslc200b112008-08-16 12:57:46 +00003053
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003054 // Emit source line correspondence into a debug line section.
3055 EmitDebugLines();
aslc200b112008-08-16 12:57:46 +00003056
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003057 // Emit info into a debug pubnames section.
3058 EmitDebugPubNames();
aslc200b112008-08-16 12:57:46 +00003059
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003060 // Emit info into a debug str section.
3061 EmitDebugStr();
aslc200b112008-08-16 12:57:46 +00003062
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003063 // Emit info into a debug loc section.
3064 EmitDebugLoc();
aslc200b112008-08-16 12:57:46 +00003065
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003066 // Emit info into a debug aranges section.
3067 EmitDebugARanges();
aslc200b112008-08-16 12:57:46 +00003068
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003069 // Emit info into a debug ranges section.
3070 EmitDebugRanges();
aslc200b112008-08-16 12:57:46 +00003071
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003072 // Emit info into a debug macinfo section.
3073 EmitDebugMacInfo();
3074 }
3075
aslc200b112008-08-16 12:57:46 +00003076 /// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003077 /// emitted immediately after the function entry point.
3078 void BeginFunction(MachineFunction *MF) {
3079 this->MF = MF;
aslc200b112008-08-16 12:57:46 +00003080
Bill Wendling50db0792009-02-20 00:44:43 +00003081 if (!ShouldEmitDwarfDebug()) return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003082
3083 // Begin accumulating function debug information.
3084 MMI->BeginFunction(MF);
aslc200b112008-08-16 12:57:46 +00003085
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003086 // Assumes in correct section after the entry point.
3087 EmitLabel("func_begin", ++SubprogramCount);
Evan Chenga53c40a2008-02-01 09:10:45 +00003088
3089 // Emit label for the implicitly defined dbg.stoppoint at the start of
3090 // the function.
Devang Patel35a078f2009-01-12 22:54:42 +00003091 if (!Lines.empty()) {
3092 const SrcLineInfo &LineInfo = Lines[0];
Andrew Lenharth42f91402008-04-03 17:37:43 +00003093 Asm->printLabel(LineInfo.getLabelID());
3094 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003095 }
aslc200b112008-08-16 12:57:46 +00003096
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003097 /// EndFunction - Gather and emit post-function debug information.
3098 ///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003099 void EndFunction(MachineFunction *MF) {
Bill Wendling50db0792009-02-20 00:44:43 +00003100 if (!ShouldEmitDwarfDebug()) return;
aslc200b112008-08-16 12:57:46 +00003101
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003102 // Define end label for subprogram.
3103 EmitLabel("func_end", SubprogramCount);
aslc200b112008-08-16 12:57:46 +00003104
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003105 // Get function line info.
Devang Patel35a078f2009-01-12 22:54:42 +00003106 if (!Lines.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003107 // Get section line info.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003108 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003109 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patel35a078f2009-01-12 22:54:42 +00003110 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003111 // Append the function info to section info.
3112 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patel35a078f2009-01-12 22:54:42 +00003113 Lines.begin(), Lines.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003114 }
aslc200b112008-08-16 12:57:46 +00003115
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003116 // Construct scopes for subprogram.
Devang Patel6ccd57e2009-01-13 00:20:51 +00003117 if (RootDbgScope)
3118 ConstructRootDbgScope(RootDbgScope);
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003119 else
3120 // FIXME: This is wrong. We are essentially getting past a problem with
3121 // debug information not being able to handle unreachable blocks that have
3122 // debug information in them. In particular, those unreachable blocks that
3123 // have "region end" info in them. That situation results in the "root
3124 // scope" not being created. If that's the case, then emit a "default"
3125 // scope, i.e., one that encompasses the whole function. This isn't
3126 // desirable. And a better way of handling this (and all of the debugging
3127 // information) needs to be explored.
Devang Patel6ccd57e2009-01-13 00:20:51 +00003128 ConstructDefaultDbgScope(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003129
3130 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3131 MMI->getFrameMoves()));
Devang Patela4162952009-01-12 18:48:36 +00003132
3133 // Clear debug info
3134 if (RootDbgScope) {
3135 delete RootDbgScope;
3136 DbgScopeMap.clear();
3137 RootDbgScope = NULL;
3138 }
3139 Lines.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003140 }
Devang Patelcb59fd42009-01-12 19:17:34 +00003141
3142public:
3143
Devang Patel2da0cc42009-01-15 23:41:32 +00003144 /// ValidDebugInfo - Return true if V represents valid debug info value.
3145 bool ValidDebugInfo(Value *V) {
Devang Patel208098b2009-01-19 23:21:49 +00003146 if (!V)
3147 return false;
3148
Devang Patel4d92dded2009-01-16 01:49:46 +00003149 if (!shouldEmit)
3150 return false;
3151
Devang Patel2da0cc42009-01-15 23:41:32 +00003152 GlobalVariable *GV = getGlobalVariable(V);
3153 if (!GV)
3154 return false;
Duncan Sands19d161f2009-03-07 15:45:40 +00003155
3156 if (!GV->hasInternalLinkage () && !GV->hasLinkOnceLinkage())
Devang Patel2da0cc42009-01-15 23:41:32 +00003157 return false;
3158
3159 DIDescriptor DI(GV);
3160 // Check current version. Allow Version6 for now.
3161 unsigned Version = DI.getVersion();
Devang Patelb49710a2009-01-20 19:22:03 +00003162 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6)
Devang Patel2da0cc42009-01-15 23:41:32 +00003163 return false;
3164
Devang Patel208098b2009-01-19 23:21:49 +00003165 unsigned Tag = DI.getTag();
3166 switch (Tag) {
3167 case DW_TAG_variable:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003168 assert(DIVariable(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003169 break;
3170 case DW_TAG_compile_unit:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003171 assert(DICompileUnit(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003172 break;
3173 case DW_TAG_subprogram:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003174 assert(DISubprogram(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003175 break;
3176 default:
3177 break;
3178 }
3179
Devang Patel2da0cc42009-01-15 23:41:32 +00003180 return true;
3181 }
3182
Devang Patelcb59fd42009-01-12 19:17:34 +00003183 /// RecordSourceLine - Records location information and associates it with a
3184 /// label. Returns a unique label ID used to generate a label and provide
3185 /// correspondence to the source line list.
3186 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
Evan Cheng3e288912009-02-25 07:04:34 +00003187 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendling6baa18d2009-02-03 21:38:21 +00003188 assert(Unit && "Unable to find CompileUnit");
Devang Patelcb59fd42009-01-12 19:17:34 +00003189 unsigned ID = MMI->NextLabelID();
3190 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
3191 return ID;
3192 }
3193
3194 /// 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(unsigned Line, unsigned Col, unsigned Src) {
3198 unsigned ID = MMI->NextLabelID();
3199 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
3200 return ID;
3201 }
3202
3203 unsigned getRecordSourceLineCount() {
3204 return Lines.size();
3205 }
3206
Evan Cheng3e288912009-02-25 07:04:34 +00003207 /// getNumSourceDirectories - Return the number of source directories in the
3208 /// debug info.
3209 unsigned getNumSourceDirectories() const {
3210 return DirectoryNames.size();
3211 }
3212
3213 /// getSourceDirectoryName - Return the name of the directory corresponding
3214 /// to the id.
3215 const std::string &getSourceDirectoryName(unsigned Id) const {
3216 return DirectoryNames[Id - 1];
3217 }
3218
3219 /// getNumSourceFiles - Return the number of source files in the debug info.
3220 ///
3221 unsigned getNumSourceFiles() const {
3222 return SourceFileNames.size();
3223 }
3224
3225 /// getSourceFileName - Return the name of the source file corresponding
3226 /// to the id.
3227 const std::string &getSourceFileName(unsigned Id) const {
3228 return SourceFileNames[Id - 1];
3229 }
3230
3231 /// getNumSourceIds - Return the number of unique source ids.
3232 ///
3233 unsigned getNumSourceIds() const {
3234 return SourceIds.size();
3235 }
3236
3237 /// getSourceDirsectoryAndFileIds - Return the directory and file ids that
3238 /// maps to the source id. Source id starts at 1.
3239 std::pair<unsigned, unsigned>
3240 getSourceDirsectoryAndFileIds(unsigned SId) const {
3241 return SourceIds[SId-1];
3242 }
3243
3244 /// getOrCreateSourceID - Look up the source id with the given directory and
3245 /// source file names. If none currently exists, create a new id and insert it
3246 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
3247 /// as well.
3248 unsigned getOrCreateSourceID(const std::string &DirName,
3249 const std::string &FileName) {
3250 unsigned DId;
3251 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
3252 if (DI != DirectoryIdMap.end())
3253 DId = DI->getValue();
3254 else {
3255 DId = DirectoryNames.size() + 1;
3256 DirectoryIdMap[DirName] = DId;
3257 DirectoryNames.push_back(DirName);
3258 }
3259
3260 unsigned FId;
3261 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
3262 if (FI != SourceFileIdMap.end())
3263 FId = FI->getValue();
3264 else {
3265 FId = SourceFileNames.size() + 1;
3266 SourceFileIdMap[FileName] = FId;
3267 SourceFileNames.push_back(FileName);
3268 }
3269
3270 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
3271 SourceIdMap.find(std::make_pair(DId, FId));
3272 if (SI != SourceIdMap.end())
3273 return SI->second;
3274 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
3275 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
3276 SourceIds.push_back(std::make_pair(DId, FId));
3277 return SrcId;
Devang Patelcb59fd42009-01-12 19:17:34 +00003278 }
3279
3280 /// RecordRegionStart - Indicate the start of a region.
3281 ///
3282 unsigned RecordRegionStart(GlobalVariable *V) {
3283 DbgScope *Scope = getOrCreateScope(V);
3284 unsigned ID = MMI->NextLabelID();
3285 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
3286 return ID;
3287 }
3288
3289 /// RecordRegionEnd - Indicate the end of a region.
3290 ///
3291 unsigned RecordRegionEnd(GlobalVariable *V) {
3292 DbgScope *Scope = getOrCreateScope(V);
3293 unsigned ID = MMI->NextLabelID();
3294 Scope->setEndLabelID(ID);
3295 return ID;
3296 }
3297
3298 /// RecordVariable - Indicate the declaration of a local variable.
3299 ///
3300 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
Devang Patel2560d922009-01-15 18:25:17 +00003301 DIDescriptor Desc(GV);
3302 DbgScope *Scope = NULL;
3303 if (Desc.getTag() == DW_TAG_variable) {
3304 // GV is a global variable.
3305 DIGlobalVariable DG(GV);
3306 Scope = getOrCreateScope(DG.getContext().getGV());
3307 } else {
3308 // or GV is a local variable.
3309 DIVariable DV(GV);
3310 Scope = getOrCreateScope(DV.getContext().getGV());
3311 }
Bill Wendling6baa18d2009-02-03 21:38:21 +00003312 assert(Scope && "Unable to find variable' scope");
Devang Patel7c8a2772009-01-16 19:28:14 +00003313 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelcb59fd42009-01-12 19:17:34 +00003314 Scope->AddVariable(DV);
3315 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003316};
3317
3318//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00003319/// DwarfException - Emits Dwarf exception handling directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003320///
3321class DwarfException : public Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003322 struct FunctionEHFrameInfo {
3323 std::string FnName;
3324 unsigned Number;
3325 unsigned PersonalityIndex;
3326 bool hasCalls;
3327 bool hasLandingPads;
3328 std::vector<MachineMove> Moves;
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003329 const Function * function;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003330
3331 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3332 bool hC, bool hL,
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003333 const std::vector<MachineMove> &M,
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003334 const Function *f):
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003335 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003336 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003337 };
3338
3339 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen85535762008-04-02 00:25:04 +00003340
3341 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3342 /// be emitted.
3343 bool shouldEmitTable;
3344
3345 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3346 /// should be emitted.
3347 bool shouldEmitMoves;
3348
3349 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3350 /// should be emitted.
3351 bool shouldEmitTableModule;
3352
aslc200b112008-08-16 12:57:46 +00003353 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen85535762008-04-02 00:25:04 +00003354 /// should be emitted.
3355 bool shouldEmitMovesModule;
Duncan Sands96144f92008-05-07 19:11:09 +00003356
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003357 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3358 ///
3359 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
3360 // Size and sign of stack growth.
3361 int stackGrowth =
3362 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3363 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00003364 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003365
3366 // Begin eh frame section.
3367 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling189bde72008-12-24 08:05:17 +00003368
3369 if (!TAI->doesRequireNonLocalEHFrameLabel())
3370 O << TAI->getEHGlobalPrefix();
3371 O << "EH_frame" << Index << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003372 EmitLabel("section_eh_frame", Index);
3373
3374 // Define base labels.
3375 EmitLabel("eh_frame_common", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003376
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003377 // Define the eh frame length.
3378 EmitDifference("eh_frame_common_end", Index,
3379 "eh_frame_common_begin", Index, true);
3380 Asm->EOL("Length of Common Information Entry");
3381
3382 // EH frame header.
3383 EmitLabel("eh_frame_common_begin", Index);
3384 Asm->EmitInt32((int)0);
3385 Asm->EOL("CIE Identifier Tag");
3386 Asm->EmitInt8(DW_CIE_VERSION);
3387 Asm->EOL("CIE Version");
Duncan Sands96144f92008-05-07 19:11:09 +00003388
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003389 // The personality presence indicates that language specific information
3390 // will show up in the eh frame.
3391 Asm->EmitString(Personality ? "zPLR" : "zR");
3392 Asm->EOL("CIE Augmentation");
Duncan Sands96144f92008-05-07 19:11:09 +00003393
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003394 // Round out reader.
3395 Asm->EmitULEB128Bytes(1);
3396 Asm->EOL("CIE Code Alignment Factor");
3397 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands96144f92008-05-07 19:11:09 +00003398 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00003399 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands96144f92008-05-07 19:11:09 +00003400 Asm->EOL("CIE Return Address Column");
3401
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003402 // If there is a personality, we need to indicate the functions location.
3403 if (Personality) {
3404 Asm->EmitULEB128Bytes(7);
3405 Asm->EOL("Augmentation Size");
Bill Wendling2d369922007-09-11 17:20:55 +00003406
Duncan Sands96144f92008-05-07 19:11:09 +00003407 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendling2d369922007-09-11 17:20:55 +00003408 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands96144f92008-05-07 19:11:09 +00003409 Asm->EOL("Personality (pcrel sdata4 indirect)");
3410 } else {
Bill Wendling2d369922007-09-11 17:20:55 +00003411 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands96144f92008-05-07 19:11:09 +00003412 Asm->EOL("Personality (pcrel sdata4)");
3413 }
Bill Wendling2d369922007-09-11 17:20:55 +00003414
Duncan Sands96144f92008-05-07 19:11:09 +00003415 PrintRelDirective(true);
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003416 O << TAI->getPersonalityPrefix();
3417 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3418 O << TAI->getPersonalitySuffix();
Duncan Sands4cc39532008-05-08 12:33:11 +00003419 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3420 O << "-" << TAI->getPCSymbol();
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003421 Asm->EOL("Personality");
Bill Wendling38cb7c92007-08-25 00:51:55 +00003422
Duncan Sands96144f92008-05-07 19:11:09 +00003423 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3424 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003425
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003426 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3427 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003428 } else {
3429 Asm->EmitULEB128Bytes(1);
3430 Asm->EOL("Augmentation Size");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003431
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003432 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3433 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003434 }
3435
3436 // Indicate locations of general callee saved registers in frame.
3437 std::vector<MachineMove> Moves;
3438 RI->getInitialFrameState(Moves);
Dale Johannesenf5a11532007-11-13 19:13:01 +00003439 EmitFrameMoves(NULL, 0, Moves, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003440
Dale Johannesen388f20f2008-04-30 00:43:29 +00003441 // On Darwin the linker honors the alignment of eh_frame, which means it
3442 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3443 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003444 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003445 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003446 EmitLabel("eh_frame_common_end", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003447
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003448 Asm->EOL();
3449 }
Duncan Sands96144f92008-05-07 19:11:09 +00003450
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003451 /// EmitEHFrame - Emit function exception frame information.
3452 ///
3453 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003454 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3455
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003456 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3457
3458 // Externally visible entry into the functions eh frame info.
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003459 // If the corresponding function is static, this should not be
3460 // externally visible.
Rafael Espindolaa168fc92009-01-15 20:18:42 +00003461 if (linkage != Function::InternalLinkage &&
Devang Patel245446c2009-01-17 08:05:14 +00003462 linkage != Function::PrivateLinkage) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003463 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3464 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3465 }
3466
Dale Johannesenf09b5992008-01-10 02:03:30 +00003467 // If corresponding function is weak definition, this should be too.
Duncan Sands19d161f2009-03-07 15:45:40 +00003468 if ((linkage == Function::WeakAnyLinkage ||
3469 linkage == Function::WeakODRLinkage ||
3470 linkage == Function::LinkOnceAnyLinkage ||
3471 linkage == Function::LinkOnceODRLinkage) &&
Dale Johannesenf09b5992008-01-10 02:03:30 +00003472 TAI->getWeakDefDirective())
3473 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3474
3475 // If there are no calls then you can't unwind. This may mean we can
3476 // omit the EH Frame, but some environments do not handle weak absolute
aslc200b112008-08-16 12:57:46 +00003477 // symbols.
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003478 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesena9b3e482008-04-08 00:10:24 +00003479 // unwind info is to be available for non-EH uses.
Dale Johannesenf09b5992008-01-10 02:03:30 +00003480 if (!EHFrameInfo.hasCalls &&
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003481 !UnwindTablesMandatory &&
Duncan Sands19d161f2009-03-07 15:45:40 +00003482 ((linkage != Function::WeakAnyLinkage &&
3483 linkage != Function::WeakODRLinkage &&
3484 linkage != Function::LinkOnceAnyLinkage &&
3485 linkage != Function::LinkOnceODRLinkage) ||
Dale Johannesenf09b5992008-01-10 02:03:30 +00003486 !TAI->getWeakDefDirective() ||
3487 TAI->getSupportsWeakOmittedEHFrame()))
aslc200b112008-08-16 12:57:46 +00003488 {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003489 O << EHFrameInfo.FnName << " = 0\n";
aslc200b112008-08-16 12:57:46 +00003490 // This name has no connection to the function, so it might get
3491 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003492 // dead-stripping unconditionally.
3493 if (const char *UsedDirective = TAI->getUsedDirective())
3494 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003495 } else {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003496 O << EHFrameInfo.FnName << ":\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003497
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003498 // EH frame header.
3499 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3500 "eh_frame_begin", EHFrameInfo.Number, true);
3501 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00003502
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003503 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
3504
Bill Wendling189bde72008-12-24 08:05:17 +00003505 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3506 PrintRelDirective(true, true);
3507 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3508
3509 if (!TAI->isAbsoluteEHSectionOffsets())
3510 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3511 } else {
3512 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3513 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3514 true, true, false);
3515 }
3516
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003517 Asm->EOL("FDE CIE offset");
3518
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003519 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003520 Asm->EOL("FDE initial location");
3521 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003522 "eh_func_begin", EHFrameInfo.Number, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003523 Asm->EOL("FDE address range");
Duncan Sands96144f92008-05-07 19:11:09 +00003524
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003525 // If there is a personality and landing pads then point to the language
3526 // specific data area in the exception table.
3527 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands96144f92008-05-07 19:11:09 +00003528 Asm->EmitULEB128Bytes(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003529 Asm->EOL("Augmentation size");
Duncan Sands96144f92008-05-07 19:11:09 +00003530
3531 if (EHFrameInfo.hasLandingPads)
3532 EmitReference("exception", EHFrameInfo.Number, true, true);
3533 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003534 Asm->EmitInt32((int)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003535 Asm->EOL("Language Specific Data Area");
3536 } else {
3537 Asm->EmitULEB128Bytes(0);
3538 Asm->EOL("Augmentation size");
3539 }
Duncan Sands96144f92008-05-07 19:11:09 +00003540
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003541 // Indicate locations of function specific callee saved registers in
3542 // frame.
Devang Patelb28de842009-01-17 08:01:33 +00003543 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00003544 true);
aslc200b112008-08-16 12:57:46 +00003545
Dale Johannesen388f20f2008-04-30 00:43:29 +00003546 // On Darwin the linker honors the alignment of eh_frame, which means it
3547 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3548 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003549 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003550 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003551 EmitLabel("eh_frame_end", EHFrameInfo.Number);
aslc200b112008-08-16 12:57:46 +00003552
3553 // If the function is marked used, this table should be also. We cannot
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003554 // make the mark unconditional in this case, since retaining the table
aslc200b112008-08-16 12:57:46 +00003555 // also retains the function in this case, and there is code around
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003556 // that depends on unused functions (calling undefined externals) being
3557 // dead-stripped to link correctly. Yes, there really is.
3558 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3559 if (const char *UsedDirective = TAI->getUsedDirective())
3560 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3561 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003562 }
3563
Duncan Sands241a0c92007-09-05 11:27:52 +00003564 /// EmitExceptionTable - Emit landing pads and actions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003565 ///
3566 /// The general organization of the table is complex, but the basic concepts
3567 /// are easy. First there is a header which describes the location and
3568 /// organization of the three components that follow.
3569 /// 1. The landing pad site information describes the range of code covered
3570 /// by the try. In our case it's an accumulation of the ranges covered
3571 /// by the invokes in the try. There is also a reference to the landing
3572 /// pad that handles the exception once processed. Finally an index into
3573 /// the actions table.
3574 /// 2. The action table, in our case, is composed of pairs of type ids
3575 /// and next action offset. Starting with the action index from the
3576 /// landing pad site, each type Id is checked for a match to the current
3577 /// exception. If it matches then the exception and type id are passed
3578 /// on to the landing pad. Otherwise the next action is looked up. This
3579 /// chain is terminated with a next action of zero. If no type id is
3580 /// found the the frame is unwound and handling continues.
3581 /// 3. Type id table contains references to all the C++ typeinfo for all
3582 /// catches in the function. This tables is reversed indexed base 1.
3583
3584 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3585 static unsigned SharedTypeIds(const LandingPadInfo *L,
3586 const LandingPadInfo *R) {
3587 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3588 unsigned LSize = LIds.size(), RSize = RIds.size();
3589 unsigned MinSize = LSize < RSize ? LSize : RSize;
3590 unsigned Count = 0;
3591
3592 for (; Count != MinSize; ++Count)
3593 if (LIds[Count] != RIds[Count])
3594 return Count;
3595
3596 return Count;
3597 }
3598
3599 /// PadLT - Order landing pads lexicographically by type id.
3600 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
3601 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3602 unsigned LSize = LIds.size(), RSize = RIds.size();
3603 unsigned MinSize = LSize < RSize ? LSize : RSize;
3604
3605 for (unsigned i = 0; i != MinSize; ++i)
3606 if (LIds[i] != RIds[i])
3607 return LIds[i] < RIds[i];
3608
3609 return LSize < RSize;
3610 }
3611
3612 struct KeyInfo {
3613 static inline unsigned getEmptyKey() { return -1U; }
3614 static inline unsigned getTombstoneKey() { return -2U; }
3615 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner92eea072007-09-17 18:34:04 +00003616 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003617 static bool isPod() { return true; }
3618 };
3619
Duncan Sands241a0c92007-09-05 11:27:52 +00003620 /// ActionEntry - Structure describing an entry in the actions table.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003621 struct ActionEntry {
3622 int ValueForTypeID; // The value to write - may not be equal to the type id.
3623 int NextAction;
3624 struct ActionEntry *Previous;
3625 };
3626
Duncan Sands241a0c92007-09-05 11:27:52 +00003627 /// PadRange - Structure holding a try-range and the associated landing pad.
3628 struct PadRange {
3629 // The index of the landing pad.
3630 unsigned PadIndex;
3631 // The index of the begin and end labels in the landing pad's label lists.
3632 unsigned RangeIndex;
3633 };
3634
3635 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3636
3637 /// CallSiteEntry - Structure describing an entry in the call-site table.
3638 struct CallSiteEntry {
Duncan Sands4ff179f2007-12-19 07:36:31 +00003639 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003640 unsigned BeginLabel; // zero indicates the start of the function.
3641 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003642 // The landing pad starts at PadLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003643 unsigned PadLabel; // zero indicates that there is no landing pad.
3644 unsigned Action;
3645 };
3646
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003647 void EmitExceptionTable() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003648 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
3649 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
3650 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3651 if (PadInfos.empty()) return;
3652
3653 // Sort the landing pads in order of their type ids. This is used to fold
3654 // duplicate actions.
3655 SmallVector<const LandingPadInfo *, 64> LandingPads;
3656 LandingPads.reserve(PadInfos.size());
3657 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
3658 LandingPads.push_back(&PadInfos[i]);
3659 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3660
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003661 // Negative type ids index into FilterIds, positive type ids index into
3662 // TypeInfos. The value written for a positive type id is just the type
3663 // id itself. For a negative type id, however, the value written is the
3664 // (negative) byte offset of the corresponding FilterIds entry. The byte
3665 // offset is usually equal to the type id, because the FilterIds entries
3666 // are written using a variable width encoding which outputs one byte per
3667 // entry as long as the value written is not too large, but can differ.
3668 // This kind of complication does not occur for positive type ids because
3669 // type infos are output using a fixed width encoding.
3670 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3671 SmallVector<int, 16> FilterOffsets;
3672 FilterOffsets.reserve(FilterIds.size());
3673 int Offset = -1;
3674 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3675 E = FilterIds.end(); I != E; ++I) {
3676 FilterOffsets.push_back(Offset);
aslc200b112008-08-16 12:57:46 +00003677 Offset -= TargetAsmInfo::getULEB128Size(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003678 }
3679
Duncan Sands241a0c92007-09-05 11:27:52 +00003680 // Compute the actions table and gather the first action index for each
3681 // landing pad site.
3682 SmallVector<ActionEntry, 32> Actions;
3683 SmallVector<unsigned, 64> FirstActions;
3684 FirstActions.reserve(LandingPads.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003685
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003686 int FirstAction = 0;
Duncan Sands241a0c92007-09-05 11:27:52 +00003687 unsigned SizeActions = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003688 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3689 const LandingPadInfo *LP = LandingPads[i];
3690 const std::vector<int> &TypeIds = LP->TypeIds;
3691 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
3692 unsigned SizeSiteActions = 0;
3693
3694 if (NumShared < TypeIds.size()) {
3695 unsigned SizeAction = 0;
3696 ActionEntry *PrevAction = 0;
3697
3698 if (NumShared) {
3699 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3700 assert(Actions.size());
3701 PrevAction = &Actions.back();
aslc200b112008-08-16 12:57:46 +00003702 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3703 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003704 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
aslc200b112008-08-16 12:57:46 +00003705 SizeAction -=
3706 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003707 SizeAction += -PrevAction->NextAction;
3708 PrevAction = PrevAction->Previous;
3709 }
3710 }
3711
3712 // Compute the actions.
3713 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3714 int TypeID = TypeIds[I];
3715 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3716 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
aslc200b112008-08-16 12:57:46 +00003717 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003718
3719 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
aslc200b112008-08-16 12:57:46 +00003720 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003721 SizeSiteActions += SizeAction;
3722
3723 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
3724 Actions.push_back(Action);
3725
3726 PrevAction = &Actions.back();
3727 }
3728
3729 // Record the first action of the landing pad site.
3730 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3731 } // else identical - re-use previous FirstAction
3732
3733 FirstActions.push_back(FirstAction);
3734
3735 // Compute this sites contribution to size.
3736 SizeActions += SizeSiteActions;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003737 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003738
Duncan Sands4ff179f2007-12-19 07:36:31 +00003739 // Compute the call-site table. The entry for an invoke has a try-range
3740 // containing the call, a non-zero landing pad and an appropriate action.
3741 // The entry for an ordinary call has a try-range containing the call and
3742 // zero for the landing pad and the action. Calls marked 'nounwind' have
3743 // no entry and must not be contained in the try-range of any entry - they
3744 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands241a0c92007-09-05 11:27:52 +00003745 SmallVector<CallSiteEntry, 64> CallSites;
3746
3747 RangeMapType PadMap;
Duncan Sands4ff179f2007-12-19 07:36:31 +00003748 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3749 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3750 // try-ranges for them need be deduced.
Duncan Sands241a0c92007-09-05 11:27:52 +00003751 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3752 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003753 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003754 unsigned BeginLabel = LandingPad->BeginLabels[j];
3755 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3756 PadRange P = { i, j };
3757 PadMap[BeginLabel] = P;
3758 }
3759 }
3760
Duncan Sands4ff179f2007-12-19 07:36:31 +00003761 // The end label of the previous invoke or nounwind try-range.
Duncan Sands241a0c92007-09-05 11:27:52 +00003762 unsigned LastLabel = 0;
Duncan Sands4ff179f2007-12-19 07:36:31 +00003763
3764 // Whether there is a potentially throwing instruction (currently this means
3765 // an ordinary call) between the end of the previous try-range and now.
3766 bool SawPotentiallyThrowing = false;
3767
3768 // Whether the last callsite entry was for an invoke.
3769 bool PreviousIsInvoke = false;
3770
Duncan Sands4ff179f2007-12-19 07:36:31 +00003771 // Visit all instructions in order of address.
Duncan Sands241a0c92007-09-05 11:27:52 +00003772 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3773 I != E; ++I) {
3774 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3775 MI != E; ++MI) {
Dan Gohmanfa607c92008-07-01 00:05:16 +00003776 if (!MI->isLabel()) {
Chris Lattner5b930372008-01-07 07:27:27 +00003777 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands241a0c92007-09-05 11:27:52 +00003778 continue;
3779 }
3780
Chris Lattnerda4cff12007-12-30 20:50:28 +00003781 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands241a0c92007-09-05 11:27:52 +00003782 assert(BeginLabel && "Invalid label!");
Duncan Sands89372f62007-09-05 14:12:46 +00003783
Duncan Sands4ff179f2007-12-19 07:36:31 +00003784 // End of the previous try-range?
Duncan Sands89372f62007-09-05 14:12:46 +00003785 if (BeginLabel == LastLabel)
Duncan Sands4ff179f2007-12-19 07:36:31 +00003786 SawPotentiallyThrowing = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003787
Duncan Sands4ff179f2007-12-19 07:36:31 +00003788 // Beginning of a new try-range?
Duncan Sands241a0c92007-09-05 11:27:52 +00003789 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands241a0c92007-09-05 11:27:52 +00003790 if (L == PadMap.end())
Duncan Sands4ff179f2007-12-19 07:36:31 +00003791 // Nope, it was just some random label.
Duncan Sands241a0c92007-09-05 11:27:52 +00003792 continue;
3793
3794 PadRange P = L->second;
3795 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3796
3797 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3798 "Inconsistent landing pad map!");
3799
3800 // If some instruction between the previous try-range and this one may
3801 // throw, create a call-site entry with no landing pad for the region
3802 // between the try-ranges.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003803 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003804 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3805 CallSites.push_back(Site);
Duncan Sands4ff179f2007-12-19 07:36:31 +00003806 PreviousIsInvoke = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003807 }
3808
3809 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003810 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands241a0c92007-09-05 11:27:52 +00003811
Duncan Sands4ff179f2007-12-19 07:36:31 +00003812 if (LandingPad->LandingPadLabel) {
3813 // This try-range is for an invoke.
3814 CallSiteEntry Site = {BeginLabel, LastLabel,
3815 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands241a0c92007-09-05 11:27:52 +00003816
Duncan Sands4ff179f2007-12-19 07:36:31 +00003817 // Try to merge with the previous call-site.
3818 if (PreviousIsInvoke) {
Dan Gohman3d436002008-06-21 22:00:54 +00003819 CallSiteEntry &Prev = CallSites.back();
Duncan Sands4ff179f2007-12-19 07:36:31 +00003820 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3821 // Extend the range of the previous entry.
3822 Prev.EndLabel = Site.EndLabel;
3823 continue;
3824 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003825 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003826
Duncan Sands4ff179f2007-12-19 07:36:31 +00003827 // Otherwise, create a new call-site.
3828 CallSites.push_back(Site);
3829 PreviousIsInvoke = true;
3830 } else {
3831 // Create a gap.
3832 PreviousIsInvoke = false;
3833 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003834 }
3835 }
3836 // If some instruction between the previous try-range and the end of the
3837 // function may throw, create a call-site entry with no landing pad for the
3838 // region following the try-range.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003839 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003840 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3841 CallSites.push_back(Site);
3842 }
3843
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003844 // Final tallies.
Duncan Sands96144f92008-05-07 19:11:09 +00003845
3846 // Call sites.
3847 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
3848 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
3849 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
3850 unsigned SizeSites = CallSites.size() * (SiteStartSize +
3851 SiteLengthSize +
3852 LandingPadSize);
Duncan Sands241a0c92007-09-05 11:27:52 +00003853 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
aslc200b112008-08-16 12:57:46 +00003854 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands241a0c92007-09-05 11:27:52 +00003855
Duncan Sands96144f92008-05-07 19:11:09 +00003856 // Type infos.
3857 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3858 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003859
3860 unsigned TypeOffset = sizeof(int8_t) + // Call site format
aslc200b112008-08-16 12:57:46 +00003861 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003862 SizeSites + SizeActions + SizeTypes;
3863
3864 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3865 sizeof(int8_t) + // TType format
aslc200b112008-08-16 12:57:46 +00003866 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003867 TypeOffset;
3868
3869 unsigned SizeAlign = (4 - TotalSize) & 3;
3870
3871 // Begin the exception table.
3872 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng7e7d1942008-02-29 19:36:59 +00003873 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesen841e4982008-10-08 21:50:21 +00003874 O << "GCC_except_table" << SubprogramCount << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003875 for (unsigned i = 0; i != SizeAlign; ++i) {
3876 Asm->EmitInt8(0);
3877 Asm->EOL("Padding");
3878 }
3879 EmitLabel("exception", SubprogramCount);
3880
3881 // Emit the header.
3882 Asm->EmitInt8(DW_EH_PE_omit);
3883 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3884 Asm->EmitInt8(DW_EH_PE_absptr);
3885 Asm->EOL("TType format (DW_EH_PE_absptr)");
3886 Asm->EmitULEB128Bytes(TypeOffset);
3887 Asm->EOL("TType base offset");
3888 Asm->EmitInt8(DW_EH_PE_udata4);
3889 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3890 Asm->EmitULEB128Bytes(SizeSites);
3891 Asm->EOL("Call-site table length");
3892
Duncan Sands241a0c92007-09-05 11:27:52 +00003893 // Emit the landing pad site information.
3894 for (unsigned i = 0; i < CallSites.size(); ++i) {
3895 CallSiteEntry &S = CallSites[i];
3896 const char *BeginTag;
3897 unsigned BeginNumber;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003898
Duncan Sands241a0c92007-09-05 11:27:52 +00003899 if (!S.BeginLabel) {
3900 BeginTag = "eh_func_begin";
3901 BeginNumber = SubprogramCount;
3902 } else {
3903 BeginTag = "label";
3904 BeginNumber = S.BeginLabel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003905 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003906
Duncan Sands241a0c92007-09-05 11:27:52 +00003907 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00003908 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003909 Asm->EOL("Region start");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003910
Duncan Sands241a0c92007-09-05 11:27:52 +00003911 if (!S.EndLabel) {
Dale Johannesen4670be42008-01-15 23:24:56 +00003912 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands96144f92008-05-07 19:11:09 +00003913 true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003914 } else {
Duncan Sands96144f92008-05-07 19:11:09 +00003915 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003916 }
3917 Asm->EOL("Region length");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003918
Duncan Sands96144f92008-05-07 19:11:09 +00003919 if (!S.PadLabel)
3920 Asm->EmitInt32(0);
3921 else
Duncan Sands241a0c92007-09-05 11:27:52 +00003922 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00003923 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003924 Asm->EOL("Landing pad");
3925
3926 Asm->EmitULEB128Bytes(S.Action);
3927 Asm->EOL("Action");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003928 }
3929
3930 // Emit the actions.
3931 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
3932 ActionEntry &Action = Actions[I];
3933
3934 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
3935 Asm->EOL("TypeInfo index");
3936 Asm->EmitSLEB128Bytes(Action.NextAction);
3937 Asm->EOL("Next action");
3938 }
3939
3940 // Emit the type ids.
3941 for (unsigned M = TypeInfos.size(); M; --M) {
3942 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00003943
3944 PrintRelDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003945
3946 if (GV)
3947 O << Asm->getGlobalLinkName(GV);
3948 else
3949 O << "0";
Duncan Sands241a0c92007-09-05 11:27:52 +00003950
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003951 Asm->EOL("TypeInfo");
3952 }
3953
3954 // Emit the filter typeids.
3955 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
3956 unsigned TypeID = FilterIds[j];
3957 Asm->EmitULEB128Bytes(TypeID);
3958 Asm->EOL("Filter TypeInfo index");
3959 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003960
Evan Cheng7e7d1942008-02-29 19:36:59 +00003961 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003962 }
3963
3964public:
3965 //===--------------------------------------------------------------------===//
3966 // Main entry points.
3967 //
Owen Anderson847b99b2008-08-21 00:14:44 +00003968 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattnerb3876c72007-09-24 03:35:37 +00003969 : Dwarf(OS, A, T, "eh")
Dale Johannesen85535762008-04-02 00:25:04 +00003970 , shouldEmitTable(false)
3971 , shouldEmitMoves(false)
3972 , shouldEmitTableModule(false)
3973 , shouldEmitMovesModule(false)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003974 {}
aslc200b112008-08-16 12:57:46 +00003975
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003976 virtual ~DwarfException() {}
3977
3978 /// SetModuleInfo - Set machine module information when it's known that pass
3979 /// manager has created it. Set by the target AsmPrinter.
3980 void SetModuleInfo(MachineModuleInfo *mmi) {
3981 MMI = mmi;
3982 }
3983
3984 /// BeginModule - Emit all exception information that should come prior to the
3985 /// content.
3986 void BeginModule(Module *M) {
3987 this->M = M;
3988 }
3989
3990 /// EndModule - Emit all exception information that should come after the
3991 /// content.
3992 void EndModule() {
Dale Johannesen85535762008-04-02 00:25:04 +00003993 if (shouldEmitMovesModule || shouldEmitTableModule) {
3994 const std::vector<Function *> Personalities = MMI->getPersonalities();
Evan Cheng3e288912009-02-25 07:04:34 +00003995 for (unsigned i = 0; i < Personalities.size(); ++i)
Dale Johannesen85535762008-04-02 00:25:04 +00003996 EmitCommonEHFrame(Personalities[i], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003997
Dale Johannesen85535762008-04-02 00:25:04 +00003998 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
3999 E = EHFrames.end(); I != E; ++I)
4000 EmitEHFrame(*I);
4001 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004002 }
4003
aslc200b112008-08-16 12:57:46 +00004004 /// BeginFunction - Gather pre-function exception information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004005 /// emitted immediately after the function entry point.
4006 void BeginFunction(MachineFunction *MF) {
4007 this->MF = MF;
Dale Johannesen85535762008-04-02 00:25:04 +00004008 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen62f0a6d2008-04-02 17:04:45 +00004009 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen85535762008-04-02 00:25:04 +00004010
4011 // Map all labels and get rid of any dead landing pads.
4012 MMI->TidyLandingPads();
4013 // If any landing pads survive, we need an EH table.
4014 if (MMI->getLandingPads().size())
4015 shouldEmitTable = true;
4016
4017 // See if we need frame move info.
Duncan Sandscbc28b12008-07-04 09:55:48 +00004018 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen85535762008-04-02 00:25:04 +00004019 shouldEmitMoves = true;
4020
4021 if (shouldEmitMoves || shouldEmitTable)
4022 // Assumes in correct section after the entry point.
4023 EmitLabel("eh_func_begin", ++SubprogramCount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004024 }
Dale Johannesen85535762008-04-02 00:25:04 +00004025 shouldEmitTableModule |= shouldEmitTable;
4026 shouldEmitMovesModule |= shouldEmitMoves;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004027 }
4028
4029 /// EndFunction - Gather and emit post-function exception information.
4030 ///
4031 void EndFunction() {
Dale Johannesen85535762008-04-02 00:25:04 +00004032 if (shouldEmitMoves || shouldEmitTable) {
4033 EmitLabel("eh_func_end", SubprogramCount);
4034 EmitExceptionTable();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004035
Dale Johannesen85535762008-04-02 00:25:04 +00004036 // Save EH frame information
4037 EHFrames.
4038 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendlingef9211a2007-09-18 01:47:22 +00004039 SubprogramCount,
4040 MMI->getPersonalityIndex(),
4041 MF->getFrameInfo()->hasCalls(),
4042 !MMI->getLandingPads().empty(),
Dale Johannesenfb3ac732007-11-20 23:24:42 +00004043 MMI->getFrameMoves(),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00004044 MF->getFunction()));
Dale Johannesen85535762008-04-02 00:25:04 +00004045 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004046 }
4047};
4048
4049} // End of namespace llvm
4050
4051//===----------------------------------------------------------------------===//
4052
4053/// Emit - Print the abbreviation using the specified Dwarf writer.
4054///
4055void DIEAbbrev::Emit(const DwarfDebug &DD) const {
4056 // Emit its Dwarf tag type.
4057 DD.getAsm()->EmitULEB128Bytes(Tag);
4058 DD.getAsm()->EOL(TagString(Tag));
aslc200b112008-08-16 12:57:46 +00004059
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004060 // Emit whether it has children DIEs.
4061 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4062 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
aslc200b112008-08-16 12:57:46 +00004063
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004064 // For each attribute description.
4065 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4066 const DIEAbbrevData &AttrData = Data[i];
aslc200b112008-08-16 12:57:46 +00004067
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004068 // Emit attribute type.
4069 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4070 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
aslc200b112008-08-16 12:57:46 +00004071
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004072 // Emit form type.
4073 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4074 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
4075 }
4076
4077 // Mark end of abbreviation.
4078 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4079 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
4080}
4081
4082#ifndef NDEBUG
4083void DIEAbbrev::print(std::ostream &O) {
4084 O << "Abbreviation @"
4085 << std::hex << (intptr_t)this << std::dec
4086 << " "
4087 << TagString(Tag)
4088 << " "
4089 << ChildrenString(ChildrenFlag)
4090 << "\n";
aslc200b112008-08-16 12:57:46 +00004091
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004092 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4093 O << " "
4094 << AttributeString(Data[i].getAttribute())
4095 << " "
4096 << FormEncodingString(Data[i].getForm())
4097 << "\n";
4098 }
4099}
4100void DIEAbbrev::dump() { print(cerr); }
4101#endif
4102
4103//===----------------------------------------------------------------------===//
4104
4105#ifndef NDEBUG
4106void DIEValue::dump() {
4107 print(cerr);
4108}
4109#endif
4110
4111//===----------------------------------------------------------------------===//
4112
4113/// EmitValue - Emit integer of appropriate size.
4114///
4115void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
4116 switch (Form) {
4117 case DW_FORM_flag: // Fall thru
4118 case DW_FORM_ref1: // Fall thru
4119 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
4120 case DW_FORM_ref2: // Fall thru
4121 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
4122 case DW_FORM_ref4: // Fall thru
4123 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
4124 case DW_FORM_ref8: // Fall thru
4125 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4126 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4127 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
4128 default: assert(0 && "DIE Value form not supported yet"); break;
4129 }
4130}
4131
4132/// SizeOf - Determine size of integer value in bytes.
4133///
4134unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4135 switch (Form) {
4136 case DW_FORM_flag: // Fall thru
4137 case DW_FORM_ref1: // Fall thru
4138 case DW_FORM_data1: return sizeof(int8_t);
4139 case DW_FORM_ref2: // Fall thru
4140 case DW_FORM_data2: return sizeof(int16_t);
4141 case DW_FORM_ref4: // Fall thru
4142 case DW_FORM_data4: return sizeof(int32_t);
4143 case DW_FORM_ref8: // Fall thru
4144 case DW_FORM_data8: return sizeof(int64_t);
aslc200b112008-08-16 12:57:46 +00004145 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4146 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004147 default: assert(0 && "DIE Value form not supported yet"); break;
4148 }
4149 return 0;
4150}
4151
4152//===----------------------------------------------------------------------===//
4153
4154/// EmitValue - Emit string value.
4155///
4156void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
4157 DD.getAsm()->EmitString(String);
4158}
4159
4160//===----------------------------------------------------------------------===//
4161
4162/// EmitValue - Emit label value.
4163///
4164void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004165 bool IsSmall = Form == DW_FORM_data4;
4166 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004167}
4168
4169/// SizeOf - Determine size of label value in bytes.
4170///
4171unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004172 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004173 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004174}
4175
4176//===----------------------------------------------------------------------===//
4177
4178/// EmitValue - Emit label value.
4179///
4180void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004181 bool IsSmall = Form == DW_FORM_data4;
4182 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004183}
4184
4185/// SizeOf - Determine size of label value in bytes.
4186///
4187unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004188 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004189 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004190}
aslc200b112008-08-16 12:57:46 +00004191
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004192//===----------------------------------------------------------------------===//
4193
4194/// EmitValue - Emit delta value.
4195///
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004196void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4197 bool IsSmall = Form == DW_FORM_data4;
4198 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4199 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4200}
4201
4202/// SizeOf - Determine size of delta value in bytes.
4203///
4204unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4205 if (Form == DW_FORM_data4) return 4;
4206 return DD.getTargetData()->getPointerSize();
4207}
aslc200b112008-08-16 12:57:46 +00004208
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004209//===----------------------------------------------------------------------===//
4210
4211/// EmitValue - Emit delta value.
4212///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004213void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
4214 bool IsSmall = Form == DW_FORM_data4;
4215 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
4216}
4217
4218/// SizeOf - Determine size of delta value in bytes.
4219///
4220unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4221 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004222 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004223}
4224
4225//===----------------------------------------------------------------------===//
4226
4227/// EmitValue - Emit debug information entry offset.
4228///
4229void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
4230 DD.getAsm()->EmitInt32(Entry->getOffset());
4231}
aslc200b112008-08-16 12:57:46 +00004232
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004233//===----------------------------------------------------------------------===//
4234
4235/// ComputeSize - calculate the size of the block.
4236///
4237unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
4238 if (!Size) {
Owen Anderson88dd6232008-06-24 21:44:59 +00004239 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004240
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004241 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4242 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
4243 }
4244 }
4245 return Size;
4246}
4247
4248/// EmitValue - Emit block data.
4249///
4250void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
4251 switch (Form) {
4252 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4253 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4254 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4255 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
4256 default: assert(0 && "Improper form for block"); break;
4257 }
aslc200b112008-08-16 12:57:46 +00004258
Owen Anderson88dd6232008-06-24 21:44:59 +00004259 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004260
4261 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4262 DD.getAsm()->EOL();
4263 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
4264 }
4265}
4266
4267/// SizeOf - Determine size of block data in bytes.
4268///
4269unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4270 switch (Form) {
4271 case DW_FORM_block1: return Size + sizeof(int8_t);
4272 case DW_FORM_block2: return Size + sizeof(int16_t);
4273 case DW_FORM_block4: return Size + sizeof(int32_t);
aslc200b112008-08-16 12:57:46 +00004274 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004275 default: assert(0 && "Improper form for block"); break;
4276 }
4277 return 0;
4278}
4279
4280//===----------------------------------------------------------------------===//
4281/// DIE Implementation
4282
4283DIE::~DIE() {
4284 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4285 delete Children[i];
4286}
aslc200b112008-08-16 12:57:46 +00004287
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004288/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4289///
4290void DIE::AddSiblingOffset() {
4291 DIEInteger *DI = new DIEInteger(0);
4292 Values.insert(Values.begin(), DI);
4293 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
4294}
4295
4296/// Profile - Used to gather unique data for the value folding set.
4297///
4298void DIE::Profile(FoldingSetNodeID &ID) {
4299 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00004300
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004301 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4302 ID.AddPointer(Children[i]);
4303
4304 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4305 ID.AddPointer(Values[j]);
4306}
4307
4308#ifndef NDEBUG
4309void DIE::print(std::ostream &O, unsigned IncIndent) {
4310 static unsigned IndentCount = 0;
4311 IndentCount += IncIndent;
4312 const std::string Indent(IndentCount, ' ');
4313 bool isBlock = Abbrev.getTag() == 0;
aslc200b112008-08-16 12:57:46 +00004314
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004315 if (!isBlock) {
4316 O << Indent
4317 << "Die: "
4318 << "0x" << std::hex << (intptr_t)this << std::dec
4319 << ", Offset: " << Offset
4320 << ", Size: " << Size
aslc200b112008-08-16 12:57:46 +00004321 << "\n";
4322
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004323 O << Indent
4324 << TagString(Abbrev.getTag())
4325 << " "
4326 << ChildrenString(Abbrev.getChildrenFlag());
4327 } else {
4328 O << "Size: " << Size;
4329 }
4330 O << "\n";
4331
Owen Anderson88dd6232008-06-24 21:44:59 +00004332 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004333
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004334 IndentCount += 2;
4335 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4336 O << Indent;
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004337
4338 if (!isBlock)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004339 O << AttributeString(Data[i].getAttribute());
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004340 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004341 O << "Blk[" << i << "]";
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004342
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004343 O << " "
4344 << FormEncodingString(Data[i].getForm())
4345 << " ";
4346 Values[i]->print(O);
4347 O << "\n";
4348 }
4349 IndentCount -= 2;
4350
4351 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4352 Children[j]->print(O, 4);
4353 }
aslc200b112008-08-16 12:57:46 +00004354
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004355 if (!isBlock) O << "\n";
4356 IndentCount -= IncIndent;
4357}
4358
4359void DIE::dump() {
4360 print(cerr);
4361}
4362#endif
4363
4364//===----------------------------------------------------------------------===//
4365/// DwarfWriter Implementation
4366///
4367
Devang Patelaa1e8432009-01-08 23:40:34 +00004368DwarfWriter::DwarfWriter() : ImmutablePass(&ID), DD(NULL), DE(NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004369}
4370
4371DwarfWriter::~DwarfWriter() {
4372 delete DE;
4373 delete DD;
4374}
4375
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004376/// BeginModule - Emit all Dwarf sections that should come prior to the
4377/// content.
Devang Patelaa1e8432009-01-08 23:40:34 +00004378void DwarfWriter::BeginModule(Module *M,
4379 MachineModuleInfo *MMI,
4380 raw_ostream &OS, AsmPrinter *A,
4381 const TargetAsmInfo *T) {
4382 DE = new DwarfException(OS, A, T);
4383 DD = new DwarfDebug(OS, A, T);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004384 DE->BeginModule(M);
4385 DD->BeginModule(M);
Devang Patel6ccd57e2009-01-13 00:20:51 +00004386 DD->SetDebugInfo(MMI);
Devang Patelaa1e8432009-01-08 23:40:34 +00004387 DE->SetModuleInfo(MMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004388}
4389
4390/// EndModule - Emit all Dwarf sections that should come after the content.
4391///
4392void DwarfWriter::EndModule() {
4393 DE->EndModule();
4394 DD->EndModule();
4395}
4396
aslc200b112008-08-16 12:57:46 +00004397/// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004398/// emitted immediately after the function entry point.
4399void DwarfWriter::BeginFunction(MachineFunction *MF) {
4400 DE->BeginFunction(MF);
4401 DD->BeginFunction(MF);
4402}
4403
4404/// EndFunction - Gather and emit post-function debug information.
4405///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00004406void DwarfWriter::EndFunction(MachineFunction *MF) {
4407 DD->EndFunction(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004408 DE->EndFunction();
aslc200b112008-08-16 12:57:46 +00004409
Bill Wendling5b4796a2008-07-22 00:53:37 +00004410 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004411 // Clear function debug information.
4412 MMI->EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004413}
Devang Patelcb59fd42009-01-12 19:17:34 +00004414
Devang Patel2da0cc42009-01-15 23:41:32 +00004415/// ValidDebugInfo - Return true if V represents valid debug info value.
4416bool DwarfWriter::ValidDebugInfo(Value *V) {
Devang Patel943af622009-01-16 02:15:14 +00004417 return DD && DD->ValidDebugInfo(V);
Devang Patel2da0cc42009-01-15 23:41:32 +00004418}
4419
Devang Patelcb59fd42009-01-12 19:17:34 +00004420/// RecordSourceLine - Records location information and associates it with a
4421/// label. Returns a unique label ID used to generate a label and provide
4422/// correspondence to the source line list.
4423unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
4424 unsigned Src) {
4425 return DD->RecordSourceLine(Line, Col, Src);
4426}
4427
Evan Cheng3e288912009-02-25 07:04:34 +00004428/// getOrCreateSourceID - Look up the source id with the given directory and
4429/// source file names. If none currently exists, create a new id and insert it
4430/// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
4431/// as well.
4432unsigned DwarfWriter::getOrCreateSourceID(const std::string &DirName,
4433 const std::string &FileName) {
4434 return DD->getOrCreateSourceID(DirName, FileName);
Devang Patelcb59fd42009-01-12 19:17:34 +00004435}
4436
4437/// RecordRegionStart - Indicate the start of a region.
4438unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
4439 return DD->RecordRegionStart(V);
4440}
4441
4442/// RecordRegionEnd - Indicate the end of a region.
4443unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
4444 return DD->RecordRegionEnd(V);
4445}
4446
4447/// getRecordSourceLineCount - Count source lines.
4448unsigned DwarfWriter::getRecordSourceLineCount() {
4449 return DD->getRecordSourceLineCount();
4450}
Devang Patel70190872009-01-13 21:25:00 +00004451
Devang Patelfe359e72009-01-13 21:44:10 +00004452/// RecordVariable - Indicate the declaration of a local variable.
4453///
4454void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
4455 DD->RecordVariable(GV, FrameIndex);
4456}
Devang Patel42f6bed2009-01-13 23:54:55 +00004457
Bill Wendling50db0792009-02-20 00:44:43 +00004458/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
4459/// be emitted.
4460bool DwarfWriter::ShouldEmitDwarfDebug() const {
4461 return DD->ShouldEmitDwarfDebug();
4462}