blob: 6e2011ea2806f5384b6623d24adb84a320db3a84 [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"
15
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/FoldingSet.h"
18#include "llvm/ADT/StringExtras.h"
19#include "llvm/ADT/UniqueVector.h"
20#include "llvm/Module.h"
Devang Patelb3907da2009-01-05 23:03:32 +000021#include "llvm/DerivedTypes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/CodeGen/AsmPrinter.h"
23#include "llvm/CodeGen/MachineModuleInfo.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineLocation.h"
Devang Patelfc187162009-01-05 17:57:47 +000026#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/Support/Debug.h"
28#include "llvm/Support/Dwarf.h"
29#include "llvm/Support/CommandLine.h"
30#include "llvm/Support/DataTypes.h"
31#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000032#include "llvm/Support/raw_ostream.h"
Dan Gohman80bbde72007-09-24 21:32:18 +000033#include "llvm/System/Path.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000035#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/Target/TargetData.h"
37#include "llvm/Target/TargetFrameInfo.h"
38#include "llvm/Target/TargetInstrInfo.h"
39#include "llvm/Target/TargetMachine.h"
40#include "llvm/Target/TargetOptions.h"
41#include <ostream>
42#include <string>
43using namespace llvm;
44using namespace llvm::dwarf;
45
46namespace llvm {
aslc200b112008-08-16 12:57:46 +000047
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048//===----------------------------------------------------------------------===//
49
50/// Configuration values for initial hash set sizes (log2).
51///
52static const unsigned InitDiesSetSize = 9; // 512
53static const unsigned InitAbbreviationsSetSize = 9; // 512
54static const unsigned InitValuesSetSize = 9; // 512
55
56//===----------------------------------------------------------------------===//
57/// Forward declarations.
58///
59class DIE;
60class DIEValue;
61
62//===----------------------------------------------------------------------===//
Devang Patelb3907da2009-01-05 23:03:32 +000063/// Utility routines.
64///
65/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
66/// specified value in their initializer somewhere.
67static void
68getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
69 // Scan though value users.
70 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
71 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
72 // If the user is a GlobalVariable then add to result.
73 Result.push_back(GV);
74 } else if (Constant *C = dyn_cast<Constant>(*I)) {
75 // If the user is a constant variable then scan its users
76 getGlobalVariablesUsing(C, Result);
77 }
78 }
79}
80
81/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
82/// named GlobalVariable.
83static void
84getGlobalVariablesUsing(Module &M, const std::string &RootName,
85 std::vector<GlobalVariable*> &Result) {
86 std::vector<const Type*> FieldTypes;
87 FieldTypes.push_back(Type::Int32Ty);
88 FieldTypes.push_back(Type::Int32Ty);
89
90 // Get the GlobalVariable root.
91 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
92 StructType::get(FieldTypes));
93
94 // If present and linkonce then scan for users.
95 if (UseRoot && UseRoot->hasLinkOnceLinkage())
96 getGlobalVariablesUsing(UseRoot, Result);
97}
98
99//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100/// DWLabel - Labels are used to track locations in the assembler file.
aslc200b112008-08-16 12:57:46 +0000101/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
102/// where the tag is a category of label (Ex. location) and number is a value
Reid Spencer37c7cea2007-08-05 20:06:04 +0000103/// unique in that category.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104class DWLabel {
105public:
106 /// Tag - Label category tag. Should always be a staticly declared C string.
107 ///
108 const char *Tag;
aslc200b112008-08-16 12:57:46 +0000109
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 /// Number - Value to make label unique.
111 ///
112 unsigned Number;
113
114 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
aslc200b112008-08-16 12:57:46 +0000115
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 void Profile(FoldingSetNodeID &ID) const {
117 ID.AddString(std::string(Tag));
118 ID.AddInteger(Number);
119 }
aslc200b112008-08-16 12:57:46 +0000120
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121#ifndef NDEBUG
122 void print(std::ostream *O) const {
123 if (O) print(*O);
124 }
125 void print(std::ostream &O) const {
126 O << "." << Tag;
127 if (Number) O << Number;
128 }
129#endif
130};
131
132//===----------------------------------------------------------------------===//
133/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
134/// Dwarf abbreviation.
135class DIEAbbrevData {
136private:
137 /// Attribute - Dwarf attribute code.
138 ///
139 unsigned Attribute;
aslc200b112008-08-16 12:57:46 +0000140
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 /// Form - Dwarf form code.
aslc200b112008-08-16 12:57:46 +0000142 ///
143 unsigned Form;
144
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145public:
146 DIEAbbrevData(unsigned A, unsigned F)
147 : Attribute(A)
148 , Form(F)
149 {}
aslc200b112008-08-16 12:57:46 +0000150
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 // Accessors.
152 unsigned getAttribute() const { return Attribute; }
153 unsigned getForm() const { return Form; }
154
155 /// Profile - Used to gather unique data for the abbreviation folding set.
156 ///
157 void Profile(FoldingSetNodeID &ID)const {
158 ID.AddInteger(Attribute);
159 ID.AddInteger(Form);
160 }
161};
162
163//===----------------------------------------------------------------------===//
164/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
165/// information object.
166class DIEAbbrev : public FoldingSetNode {
167private:
168 /// Tag - Dwarf tag code.
169 ///
170 unsigned Tag;
aslc200b112008-08-16 12:57:46 +0000171
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 /// Unique number for node.
173 ///
174 unsigned Number;
175
176 /// ChildrenFlag - Dwarf children flag.
177 ///
178 unsigned ChildrenFlag;
179
180 /// Data - Raw data bytes for abbreviation.
181 ///
Owen Anderson88dd6232008-06-24 21:44:59 +0000182 SmallVector<DIEAbbrevData, 8> Data;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183
184public:
185
186 DIEAbbrev(unsigned T, unsigned C)
187 : Tag(T)
188 , ChildrenFlag(C)
189 , Data()
190 {}
191 ~DIEAbbrev() {}
aslc200b112008-08-16 12:57:46 +0000192
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 // Accessors.
194 unsigned getTag() const { return Tag; }
195 unsigned getNumber() const { return Number; }
196 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson88dd6232008-06-24 21:44:59 +0000197 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 void setTag(unsigned T) { Tag = T; }
199 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
200 void setNumber(unsigned N) { Number = N; }
aslc200b112008-08-16 12:57:46 +0000201
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 /// AddAttribute - Adds another set of attribute information to the
203 /// abbreviation.
204 void AddAttribute(unsigned Attribute, unsigned Form) {
205 Data.push_back(DIEAbbrevData(Attribute, Form));
206 }
aslc200b112008-08-16 12:57:46 +0000207
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 /// AddFirstAttribute - Adds a set of attribute information to the front
209 /// of the abbreviation.
210 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
211 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
212 }
aslc200b112008-08-16 12:57:46 +0000213
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 /// Profile - Used to gather unique data for the abbreviation folding set.
215 ///
216 void Profile(FoldingSetNodeID &ID) {
217 ID.AddInteger(Tag);
218 ID.AddInteger(ChildrenFlag);
aslc200b112008-08-16 12:57:46 +0000219
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000220 // For each attribute description.
221 for (unsigned i = 0, N = Data.size(); i < N; ++i)
222 Data[i].Profile(ID);
223 }
aslc200b112008-08-16 12:57:46 +0000224
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 /// Emit - Print the abbreviation using the specified Dwarf writer.
226 ///
aslc200b112008-08-16 12:57:46 +0000227 void Emit(const DwarfDebug &DD) const;
228
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229#ifndef NDEBUG
230 void print(std::ostream *O) {
231 if (O) print(*O);
232 }
233 void print(std::ostream &O);
234 void dump();
235#endif
236};
237
238//===----------------------------------------------------------------------===//
239/// DIE - A structured debug information entry. Has an abbreviation which
240/// describes it's organization.
241class DIE : public FoldingSetNode {
242protected:
243 /// Abbrev - Buffer for constructing abbreviation.
244 ///
245 DIEAbbrev Abbrev;
aslc200b112008-08-16 12:57:46 +0000246
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 /// Offset - Offset in debug info section.
248 ///
249 unsigned Offset;
aslc200b112008-08-16 12:57:46 +0000250
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 /// Size - Size of instance + children.
252 ///
253 unsigned Size;
aslc200b112008-08-16 12:57:46 +0000254
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 /// Children DIEs.
256 ///
257 std::vector<DIE *> Children;
aslc200b112008-08-16 12:57:46 +0000258
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 /// Attributes values.
260 ///
Owen Anderson88dd6232008-06-24 21:44:59 +0000261 SmallVector<DIEValue*, 32> Values;
aslc200b112008-08-16 12:57:46 +0000262
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000264 explicit DIE(unsigned Tag)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 : Abbrev(Tag, DW_CHILDREN_no)
266 , Offset(0)
267 , Size(0)
268 , Children()
269 , Values()
270 {}
271 virtual ~DIE();
aslc200b112008-08-16 12:57:46 +0000272
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 // Accessors.
274 DIEAbbrev &getAbbrev() { return Abbrev; }
275 unsigned getAbbrevNumber() const {
276 return Abbrev.getNumber();
277 }
278 unsigned getTag() const { return Abbrev.getTag(); }
279 unsigned getOffset() const { return Offset; }
280 unsigned getSize() const { return Size; }
281 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson88dd6232008-06-24 21:44:59 +0000282 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
284 void setOffset(unsigned O) { Offset = O; }
285 void setSize(unsigned S) { Size = S; }
aslc200b112008-08-16 12:57:46 +0000286
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 /// AddValue - Add a value and attributes to a DIE.
288 ///
289 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
290 Abbrev.AddAttribute(Attribute, Form);
291 Values.push_back(Value);
292 }
aslc200b112008-08-16 12:57:46 +0000293
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 /// SiblingOffset - Return the offset of the debug information entry's
295 /// sibling.
296 unsigned SiblingOffset() const { return Offset + Size; }
aslc200b112008-08-16 12:57:46 +0000297
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
299 ///
300 void AddSiblingOffset();
301
302 /// AddChild - Add a child to the DIE.
303 ///
304 void AddChild(DIE *Child) {
305 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
306 Children.push_back(Child);
307 }
aslc200b112008-08-16 12:57:46 +0000308
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 /// Detach - Detaches objects connected to it after copying.
310 ///
311 void Detach() {
312 Children.clear();
313 }
aslc200b112008-08-16 12:57:46 +0000314
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 /// Profile - Used to gather unique data for the value folding set.
316 ///
317 void Profile(FoldingSetNodeID &ID) ;
aslc200b112008-08-16 12:57:46 +0000318
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319#ifndef NDEBUG
320 void print(std::ostream *O, unsigned IncIndent = 0) {
321 if (O) print(*O, IncIndent);
322 }
323 void print(std::ostream &O, unsigned IncIndent = 0);
324 void dump();
325#endif
326};
327
328//===----------------------------------------------------------------------===//
329/// DIEValue - A debug information entry value.
330///
331class DIEValue : public FoldingSetNode {
332public:
333 enum {
334 isInteger,
335 isString,
336 isLabel,
337 isAsIsLabel,
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000338 isSectionOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 isDelta,
340 isEntry,
341 isBlock
342 };
aslc200b112008-08-16 12:57:46 +0000343
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 /// Type - Type of data stored in the value.
345 ///
346 unsigned Type;
aslc200b112008-08-16 12:57:46 +0000347
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000348 explicit DIEValue(unsigned T)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 : Type(T)
350 {}
351 virtual ~DIEValue() {}
aslc200b112008-08-16 12:57:46 +0000352
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 // Accessors
354 unsigned getType() const { return Type; }
aslc200b112008-08-16 12:57:46 +0000355
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356 // Implement isa/cast/dyncast.
357 static bool classof(const DIEValue *) { return true; }
aslc200b112008-08-16 12:57:46 +0000358
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359 /// EmitValue - Emit value via the Dwarf writer.
360 ///
361 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
aslc200b112008-08-16 12:57:46 +0000362
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363 /// SizeOf - Return the size of a value in bytes.
364 ///
365 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
aslc200b112008-08-16 12:57:46 +0000366
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 /// Profile - Used to gather unique data for the value folding set.
368 ///
369 virtual void Profile(FoldingSetNodeID &ID) = 0;
aslc200b112008-08-16 12:57:46 +0000370
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371#ifndef NDEBUG
372 void print(std::ostream *O) {
373 if (O) print(*O);
374 }
375 virtual void print(std::ostream &O) = 0;
376 void dump();
377#endif
378};
379
380//===----------------------------------------------------------------------===//
381/// DWInteger - An integer value DIE.
aslc200b112008-08-16 12:57:46 +0000382///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383class DIEInteger : public DIEValue {
384private:
385 uint64_t Integer;
aslc200b112008-08-16 12:57:46 +0000386
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000388 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000389
390 // Implement isa/cast/dyncast.
391 static bool classof(const DIEInteger *) { return true; }
392 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
aslc200b112008-08-16 12:57:46 +0000393
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 /// BestForm - Choose the best form for integer.
395 ///
396 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
397 if (IsSigned) {
398 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
399 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
400 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
401 } else {
402 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
403 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
404 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
405 }
406 return DW_FORM_data8;
407 }
aslc200b112008-08-16 12:57:46 +0000408
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409 /// EmitValue - Emit integer of appropriate size.
410 ///
411 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000412
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413 /// SizeOf - Determine size of integer value in bytes.
414 ///
415 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000416
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 /// Profile - Used to gather unique data for the value folding set.
418 ///
419 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
420 ID.AddInteger(isInteger);
421 ID.AddInteger(Integer);
422 }
423 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
aslc200b112008-08-16 12:57:46 +0000424
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425#ifndef NDEBUG
426 virtual void print(std::ostream &O) {
427 O << "Int: " << (int64_t)Integer
428 << " 0x" << std::hex << Integer << std::dec;
429 }
430#endif
431};
432
433//===----------------------------------------------------------------------===//
434/// DIEString - A string value DIE.
aslc200b112008-08-16 12:57:46 +0000435///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436class DIEString : public DIEValue {
437public:
438 const std::string String;
aslc200b112008-08-16 12:57:46 +0000439
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000440 explicit DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441
442 // Implement isa/cast/dyncast.
443 static bool classof(const DIEString *) { return true; }
444 static bool classof(const DIEValue *S) { return S->Type == isString; }
aslc200b112008-08-16 12:57:46 +0000445
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446 /// EmitValue - Emit string value.
447 ///
448 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000449
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 /// SizeOf - Determine size of string value in bytes.
451 ///
452 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
453 return String.size() + sizeof(char); // sizeof('\0');
454 }
aslc200b112008-08-16 12:57:46 +0000455
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 /// Profile - Used to gather unique data for the value folding set.
457 ///
458 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
459 ID.AddInteger(isString);
460 ID.AddString(String);
461 }
462 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
aslc200b112008-08-16 12:57:46 +0000463
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464#ifndef NDEBUG
465 virtual void print(std::ostream &O) {
466 O << "Str: \"" << String << "\"";
467 }
468#endif
469};
470
471//===----------------------------------------------------------------------===//
472/// DIEDwarfLabel - A Dwarf internal label expression DIE.
473//
474class DIEDwarfLabel : public DIEValue {
475public:
476
477 const DWLabel Label;
aslc200b112008-08-16 12:57:46 +0000478
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000479 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480
481 // Implement isa/cast/dyncast.
482 static bool classof(const DIEDwarfLabel *) { return true; }
483 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
aslc200b112008-08-16 12:57:46 +0000484
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 /// EmitValue - Emit label value.
486 ///
487 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000488
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000489 /// SizeOf - Determine size of label value in bytes.
490 ///
491 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000492
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493 /// Profile - Used to gather unique data for the value folding set.
494 ///
495 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
496 ID.AddInteger(isLabel);
497 Label.Profile(ID);
498 }
499 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
aslc200b112008-08-16 12:57:46 +0000500
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501#ifndef NDEBUG
502 virtual void print(std::ostream &O) {
503 O << "Lbl: ";
504 Label.print(O);
505 }
506#endif
507};
508
509
510//===----------------------------------------------------------------------===//
511/// DIEObjectLabel - A label to an object in code or data.
512//
513class DIEObjectLabel : public DIEValue {
514public:
515 const std::string Label;
aslc200b112008-08-16 12:57:46 +0000516
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000517 explicit DIEObjectLabel(const std::string &L)
518 : DIEValue(isAsIsLabel), Label(L) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519
520 // Implement isa/cast/dyncast.
521 static bool classof(const DIEObjectLabel *) { return true; }
522 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
aslc200b112008-08-16 12:57:46 +0000523
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 /// EmitValue - Emit label value.
525 ///
526 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000527
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528 /// SizeOf - Determine size of label value in bytes.
529 ///
530 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000531
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000532 /// Profile - Used to gather unique data for the value folding set.
533 ///
534 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
535 ID.AddInteger(isAsIsLabel);
536 ID.AddString(Label);
537 }
538 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
539
540#ifndef NDEBUG
541 virtual void print(std::ostream &O) {
542 O << "Obj: " << Label;
543 }
544#endif
545};
546
547//===----------------------------------------------------------------------===//
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000548/// DIESectionOffset - A section offset DIE.
549//
550class DIESectionOffset : public DIEValue {
551public:
552 const DWLabel Label;
553 const DWLabel Section;
554 bool IsEH : 1;
555 bool UseSet : 1;
aslc200b112008-08-16 12:57:46 +0000556
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000557 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
558 bool isEH = false, bool useSet = true)
559 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
560 IsEH(isEH), UseSet(useSet) {}
561
562 // Implement isa/cast/dyncast.
563 static bool classof(const DIESectionOffset *) { return true; }
564 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
aslc200b112008-08-16 12:57:46 +0000565
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000566 /// EmitValue - Emit section offset.
567 ///
568 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000569
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000570 /// SizeOf - Determine size of section offset value in bytes.
571 ///
572 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000573
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000574 /// Profile - Used to gather unique data for the value folding set.
575 ///
576 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
577 const DWLabel &Section) {
578 ID.AddInteger(isSectionOffset);
579 Label.Profile(ID);
580 Section.Profile(ID);
581 // IsEH and UseSet are specific to the Label/Section that we will emit
582 // the offset for; so Label/Section are enough for uniqueness.
583 }
584 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
585
586#ifndef NDEBUG
587 virtual void print(std::ostream &O) {
588 O << "Off: ";
589 Label.print(O);
590 O << "-";
591 Section.print(O);
592 O << "-" << IsEH << "-" << UseSet;
593 }
594#endif
595};
596
597//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598/// DIEDelta - A simple label difference DIE.
aslc200b112008-08-16 12:57:46 +0000599///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600class DIEDelta : public DIEValue {
601public:
602 const DWLabel LabelHi;
603 const DWLabel LabelLo;
aslc200b112008-08-16 12:57:46 +0000604
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000605 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
606 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
607
608 // Implement isa/cast/dyncast.
609 static bool classof(const DIEDelta *) { return true; }
610 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
aslc200b112008-08-16 12:57:46 +0000611
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 /// EmitValue - Emit delta value.
613 ///
614 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000615
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616 /// SizeOf - Determine size of delta value in bytes.
617 ///
618 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000619
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620 /// Profile - Used to gather unique data for the value folding set.
621 ///
622 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
623 const DWLabel &LabelLo) {
624 ID.AddInteger(isDelta);
625 LabelHi.Profile(ID);
626 LabelLo.Profile(ID);
627 }
628 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
629
630#ifndef NDEBUG
631 virtual void print(std::ostream &O) {
632 O << "Del: ";
633 LabelHi.print(O);
634 O << "-";
635 LabelLo.print(O);
636 }
637#endif
638};
639
640//===----------------------------------------------------------------------===//
641/// DIEntry - A pointer to another debug information entry. An instance of this
642/// class can also be used as a proxy for a debug information entry not yet
643/// defined (ie. types.)
644class DIEntry : public DIEValue {
645public:
646 DIE *Entry;
aslc200b112008-08-16 12:57:46 +0000647
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000648 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
aslc200b112008-08-16 12:57:46 +0000649
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650 // Implement isa/cast/dyncast.
651 static bool classof(const DIEntry *) { return true; }
652 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
aslc200b112008-08-16 12:57:46 +0000653
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000654 /// EmitValue - Emit debug information entry offset.
655 ///
656 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000657
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658 /// SizeOf - Determine size of debug information entry in bytes.
659 ///
660 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
661 return sizeof(int32_t);
662 }
aslc200b112008-08-16 12:57:46 +0000663
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664 /// Profile - Used to gather unique data for the value folding set.
665 ///
666 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
667 ID.AddInteger(isEntry);
668 ID.AddPointer(Entry);
669 }
670 virtual void Profile(FoldingSetNodeID &ID) {
671 ID.AddInteger(isEntry);
aslc200b112008-08-16 12:57:46 +0000672
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000673 if (Entry) {
674 ID.AddPointer(Entry);
675 } else {
676 ID.AddPointer(this);
677 }
678 }
aslc200b112008-08-16 12:57:46 +0000679
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680#ifndef NDEBUG
681 virtual void print(std::ostream &O) {
682 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
683 }
684#endif
685};
686
687//===----------------------------------------------------------------------===//
688/// DIEBlock - A block of values. Primarily used for location expressions.
689//
690class DIEBlock : public DIEValue, public DIE {
691public:
692 unsigned Size; // Size in bytes excluding size header.
aslc200b112008-08-16 12:57:46 +0000693
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000694 DIEBlock()
695 : DIEValue(isBlock)
696 , DIE(0)
697 , Size(0)
698 {}
699 ~DIEBlock() {
700 }
aslc200b112008-08-16 12:57:46 +0000701
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000702 // Implement isa/cast/dyncast.
703 static bool classof(const DIEBlock *) { return true; }
704 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
aslc200b112008-08-16 12:57:46 +0000705
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706 /// ComputeSize - calculate the size of the block.
707 ///
708 unsigned ComputeSize(DwarfDebug &DD);
aslc200b112008-08-16 12:57:46 +0000709
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000710 /// BestForm - Choose the best form for data.
711 ///
712 unsigned BestForm() const {
713 if ((unsigned char)Size == Size) return DW_FORM_block1;
714 if ((unsigned short)Size == Size) return DW_FORM_block2;
715 if ((unsigned int)Size == Size) return DW_FORM_block4;
716 return DW_FORM_block;
717 }
718
719 /// EmitValue - Emit block data.
720 ///
721 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000722
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723 /// SizeOf - Determine size of block data in bytes.
724 ///
725 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000726
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000727
728 /// Profile - Used to gather unique data for the value folding set.
729 ///
730 virtual void Profile(FoldingSetNodeID &ID) {
731 ID.AddInteger(isBlock);
732 DIE::Profile(ID);
733 }
aslc200b112008-08-16 12:57:46 +0000734
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000735#ifndef NDEBUG
736 virtual void print(std::ostream &O) {
737 O << "Blk: ";
738 DIE::print(O, 5);
739 }
740#endif
741};
742
743//===----------------------------------------------------------------------===//
744/// CompileUnit - This dwarf writer support class manages information associate
745/// with a source file.
746class CompileUnit {
747private:
748 /// Desc - Compile unit debug descriptor.
749 ///
750 CompileUnitDesc *Desc;
aslc200b112008-08-16 12:57:46 +0000751
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752 /// ID - File identifier for source.
753 ///
754 unsigned ID;
aslc200b112008-08-16 12:57:46 +0000755
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000756 /// Die - Compile unit debug information entry.
757 ///
758 DIE *Die;
aslc200b112008-08-16 12:57:46 +0000759
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000760 /// DescToDieMap - Tracks the mapping of unit level debug informaton
761 /// descriptors to debug information entries.
762 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
Devang Patel4a4cbe72009-01-05 21:47:57 +0000763 DenseMap<GlobalVariable *, DIE *> GVToDieMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000764
765 /// DescToDIEntryMap - Tracks the mapping of unit level debug informaton
766 /// descriptors to debug information entries using a DIEntry proxy.
767 std::map<DebugInfoDesc *, DIEntry *> DescToDIEntryMap;
Devang Patel4a4cbe72009-01-05 21:47:57 +0000768 DenseMap<GlobalVariable *, DIEntry *> GVToDIEntryMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000769
770 /// Globals - A map of globally visible named entities for this unit.
771 ///
772 std::map<std::string, DIE *> Globals;
773
774 /// DiesSet - Used to uniquely define dies within the compile unit.
775 ///
776 FoldingSet<DIE> DiesSet;
aslc200b112008-08-16 12:57:46 +0000777
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000778 /// Dies - List of all dies in the compile unit.
779 ///
780 std::vector<DIE *> Dies;
aslc200b112008-08-16 12:57:46 +0000781
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000782public:
Devang Patelb3907da2009-01-05 23:03:32 +0000783 CompileUnit(unsigned I, DIE *D)
784 : ID(I), Die(D), DescToDieMap(), GVToDieMap(), DescToDIEntryMap(),
785 GVToDIEntryMap(), Globals(), DiesSet(InitDiesSetSize), Dies()
786 {}
787
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000788 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
789 : Desc(CUD)
790 , ID(I)
791 , Die(D)
792 , DescToDieMap()
Devang Patel4a4cbe72009-01-05 21:47:57 +0000793 , GVToDieMap()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000794 , DescToDIEntryMap()
Devang Patel4a4cbe72009-01-05 21:47:57 +0000795 , GVToDIEntryMap()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000796 , Globals()
797 , DiesSet(InitDiesSetSize)
798 , Dies()
799 {}
aslc200b112008-08-16 12:57:46 +0000800
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000801 ~CompileUnit() {
802 delete Die;
aslc200b112008-08-16 12:57:46 +0000803
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804 for (unsigned i = 0, N = Dies.size(); i < N; ++i)
805 delete Dies[i];
806 }
aslc200b112008-08-16 12:57:46 +0000807
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000808 // Accessors.
809 CompileUnitDesc *getDesc() const { return Desc; }
810 unsigned getID() const { return ID; }
811 DIE* getDie() const { return Die; }
812 std::map<std::string, DIE *> &getGlobals() { return Globals; }
813
814 /// hasContent - Return true if this compile unit has something to write out.
815 ///
816 bool hasContent() const {
817 return !Die->getChildren().empty();
818 }
819
820 /// AddGlobal - Add a new global entity to the compile unit.
821 ///
822 void AddGlobal(const std::string &Name, DIE *Die) {
823 Globals[Name] = Die;
824 }
aslc200b112008-08-16 12:57:46 +0000825
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000826 /// getDieMapSlotFor - Returns the debug information entry map slot for the
827 /// specified debug descriptor.
828 DIE *&getDieMapSlotFor(DebugInfoDesc *DID) {
829 return DescToDieMap[DID];
830 }
Devang Patel4a4cbe72009-01-05 21:47:57 +0000831 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
832 return GVToDieMap[GV];
833 }
aslc200b112008-08-16 12:57:46 +0000834
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000835 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
836 /// specified debug descriptor.
837 DIEntry *&getDIEntrySlotFor(DebugInfoDesc *DID) {
838 return DescToDIEntryMap[DID];
839 }
Devang Patel4a4cbe72009-01-05 21:47:57 +0000840 DIEntry *&getDIEntrySlotFor(GlobalVariable *GV) {
841 return GVToDIEntryMap[GV];
842 }
aslc200b112008-08-16 12:57:46 +0000843
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000844 /// AddDie - Adds or interns the DIE to the compile unit.
845 ///
846 DIE *AddDie(DIE &Buffer) {
847 FoldingSetNodeID ID;
848 Buffer.Profile(ID);
849 void *Where;
850 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
aslc200b112008-08-16 12:57:46 +0000851
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000852 if (!Die) {
853 Die = new DIE(Buffer);
854 DiesSet.InsertNode(Die, Where);
855 this->Die->AddChild(Die);
856 Buffer.Detach();
857 }
aslc200b112008-08-16 12:57:46 +0000858
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859 return Die;
860 }
861};
862
863//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +0000864/// Dwarf - Emits general Dwarf directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000865///
866class Dwarf {
867
868protected:
869
870 //===--------------------------------------------------------------------===//
871 // Core attributes used by the Dwarf writer.
872 //
aslc200b112008-08-16 12:57:46 +0000873
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000874 //
875 /// O - Stream to .s file.
876 ///
Owen Anderson847b99b2008-08-21 00:14:44 +0000877 raw_ostream &O;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000878
879 /// Asm - Target of Dwarf emission.
880 ///
881 AsmPrinter *Asm;
aslc200b112008-08-16 12:57:46 +0000882
Bill Wendlingac9639d2008-07-01 23:34:48 +0000883 /// TAI - Target asm information.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000884 const TargetAsmInfo *TAI;
aslc200b112008-08-16 12:57:46 +0000885
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000886 /// TD - Target data.
887 const TargetData *TD;
aslc200b112008-08-16 12:57:46 +0000888
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000889 /// RI - Register Information.
Dan Gohman1e57df32008-02-10 18:45:23 +0000890 const TargetRegisterInfo *RI;
aslc200b112008-08-16 12:57:46 +0000891
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000892 /// M - Current module.
893 ///
894 Module *M;
aslc200b112008-08-16 12:57:46 +0000895
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000896 /// MF - Current machine function.
897 ///
898 MachineFunction *MF;
aslc200b112008-08-16 12:57:46 +0000899
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000900 /// MMI - Collected machine module information.
901 ///
902 MachineModuleInfo *MMI;
aslc200b112008-08-16 12:57:46 +0000903
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000904 /// SubprogramCount - The running count of functions being compiled.
905 ///
906 unsigned SubprogramCount;
aslc200b112008-08-16 12:57:46 +0000907
Chris Lattnerb3876c72007-09-24 03:35:37 +0000908 /// Flavor - A unique string indicating what dwarf producer this is, used to
909 /// unique labels.
910 const char * const Flavor;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000911
912 unsigned SetCounter;
Owen Anderson847b99b2008-08-21 00:14:44 +0000913 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattnerb3876c72007-09-24 03:35:37 +0000914 const char *flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000915 : O(OS)
916 , Asm(A)
917 , TAI(T)
918 , TD(Asm->TM.getTargetData())
919 , RI(Asm->TM.getRegisterInfo())
920 , M(NULL)
921 , MF(NULL)
922 , MMI(NULL)
923 , SubprogramCount(0)
Chris Lattnerb3876c72007-09-24 03:35:37 +0000924 , Flavor(flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000925 , SetCounter(1)
926 {
927 }
928
929public:
930
931 //===--------------------------------------------------------------------===//
932 // Accessors.
933 //
934 AsmPrinter *getAsm() const { return Asm; }
935 MachineModuleInfo *getMMI() const { return MMI; }
936 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohmancfb72b22007-09-27 23:12:31 +0000937 const TargetData *getTargetData() const { return TD; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000938
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000939 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
940 const {
941 if (isInSection && TAI->getDwarfSectionOffsetDirective())
942 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohmancfb72b22007-09-27 23:12:31 +0000943 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000944 O << TAI->getData32bitsDirective();
945 else
946 O << TAI->getData64bitsDirective();
947 }
aslc200b112008-08-16 12:57:46 +0000948
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000949 /// PrintLabelName - Print label name in form used by Dwarf writer.
950 ///
951 void PrintLabelName(DWLabel Label) const {
952 PrintLabelName(Label.Tag, Label.Number);
953 }
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000954 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000955 O << TAI->getPrivateGlobalPrefix() << Tag;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000956 if (Number) O << Number;
957 }
aslc200b112008-08-16 12:57:46 +0000958
Chris Lattnerb3876c72007-09-24 03:35:37 +0000959 void PrintLabelName(const char *Tag, unsigned Number,
960 const char *Suffix) const {
961 O << TAI->getPrivateGlobalPrefix() << Tag;
962 if (Number) O << Number;
963 O << Suffix;
964 }
aslc200b112008-08-16 12:57:46 +0000965
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000966 /// EmitLabel - Emit location label for internal use by Dwarf.
967 ///
968 void EmitLabel(DWLabel Label) const {
969 EmitLabel(Label.Tag, Label.Number);
970 }
971 void EmitLabel(const char *Tag, unsigned Number) const {
972 PrintLabelName(Tag, Number);
973 O << ":\n";
974 }
aslc200b112008-08-16 12:57:46 +0000975
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000976 /// EmitReference - Emit a reference to a label.
977 ///
Dan Gohman4fd77742007-09-28 15:43:33 +0000978 void EmitReference(DWLabel Label, bool IsPCRelative = false,
979 bool Force32Bit = false) const {
980 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000981 }
982 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman4fd77742007-09-28 15:43:33 +0000983 bool IsPCRelative = false, bool Force32Bit = false) const {
984 PrintRelDirective(Force32Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000985 PrintLabelName(Tag, Number);
aslc200b112008-08-16 12:57:46 +0000986
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000987 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
988 }
Dan Gohman4fd77742007-09-28 15:43:33 +0000989 void EmitReference(const std::string &Name, bool IsPCRelative = false,
990 bool Force32Bit = false) const {
991 PrintRelDirective(Force32Bit);
aslc200b112008-08-16 12:57:46 +0000992
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000993 O << Name;
aslc200b112008-08-16 12:57:46 +0000994
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000995 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
996 }
997
998 /// EmitDifference - Emit the difference between two labels. Some
999 /// assemblers do not behave with absolute expressions with data directives,
1000 /// so there is an option (needsSet) to use an intermediary set expression.
1001 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
1002 bool IsSmall = false) {
1003 EmitDifference(LabelHi.Tag, LabelHi.Number,
1004 LabelLo.Tag, LabelLo.Number,
1005 IsSmall);
1006 }
1007 void EmitDifference(const char *TagHi, unsigned NumberHi,
1008 const char *TagLo, unsigned NumberLo,
1009 bool IsSmall = false) {
1010 if (TAI->needsSet()) {
1011 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +00001012 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001013 O << ",";
1014 PrintLabelName(TagHi, NumberHi);
1015 O << "-";
1016 PrintLabelName(TagLo, NumberLo);
1017 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001018
1019 PrintRelDirective(IsSmall);
Chris Lattnerb3876c72007-09-24 03:35:37 +00001020 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001021 ++SetCounter;
1022 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001023 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +00001024
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001025 PrintLabelName(TagHi, NumberHi);
1026 O << "-";
1027 PrintLabelName(TagLo, NumberLo);
1028 }
1029 }
1030
1031 void EmitSectionOffset(const char* Label, const char* Section,
1032 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesen0ebb2432008-03-26 23:31:39 +00001033 bool IsSmall = false, bool isEH = false,
1034 bool useSet = true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035 bool printAbsolute = false;
Dale Johannesen0ebb2432008-03-26 23:31:39 +00001036 if (isEH)
1037 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
1038 else
1039 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
1040
1041 if (TAI->needsSet() && useSet) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001042 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +00001043 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001044 O << ",";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001045 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001046
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001047 if (!printAbsolute) {
1048 O << "-";
1049 PrintLabelName(Section, SectionNumber);
aslc200b112008-08-16 12:57:46 +00001050 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001051 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001052
1053 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +00001054
Chris Lattnerb3876c72007-09-24 03:35:37 +00001055 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001056 ++SetCounter;
1057 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001058 PrintRelDirective(IsSmall, true);
aslc200b112008-08-16 12:57:46 +00001059
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001060 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001061
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001062 if (!printAbsolute) {
1063 O << "-";
1064 PrintLabelName(Section, SectionNumber);
1065 }
aslc200b112008-08-16 12:57:46 +00001066 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001067 }
aslc200b112008-08-16 12:57:46 +00001068
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001069 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1070 /// frame.
1071 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenf5a11532007-11-13 19:13:01 +00001072 const std::vector<MachineMove> &Moves, bool isEH) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001073 int stackGrowth =
1074 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1075 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00001076 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001077 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
1078
1079 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
1080 const MachineMove &Move = Moves[i];
1081 unsigned LabelID = Move.getLabelID();
aslc200b112008-08-16 12:57:46 +00001082
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001083 if (LabelID) {
1084 LabelID = MMI->MappedLabel(LabelID);
aslc200b112008-08-16 12:57:46 +00001085
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001086 // Throw out move if the label is invalid.
1087 if (!LabelID) continue;
1088 }
aslc200b112008-08-16 12:57:46 +00001089
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001090 const MachineLocation &Dst = Move.getDestination();
1091 const MachineLocation &Src = Move.getSource();
aslc200b112008-08-16 12:57:46 +00001092
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001093 // Advance row if new location.
1094 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1095 Asm->EmitInt8(DW_CFA_advance_loc4);
1096 Asm->EOL("DW_CFA_advance_loc4");
1097 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1098 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00001099
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001100 BaseLabelID = LabelID;
1101 BaseLabel = "label";
1102 IsLocal = true;
1103 }
aslc200b112008-08-16 12:57:46 +00001104
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001105 // If advancing cfa.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001106 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1107 if (!Src.isReg()) {
1108 if (Src.getReg() == MachineLocation::VirtualFP) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001109 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1110 Asm->EOL("DW_CFA_def_cfa_offset");
1111 } else {
1112 Asm->EmitInt8(DW_CFA_def_cfa);
1113 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001114 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001115 Asm->EOL("Register");
1116 }
aslc200b112008-08-16 12:57:46 +00001117
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001118 int Offset = -Src.getOffset();
aslc200b112008-08-16 12:57:46 +00001119
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001120 Asm->EmitULEB128Bytes(Offset);
1121 Asm->EOL("Offset");
1122 } else {
1123 assert(0 && "Machine move no supported yet.");
1124 }
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001125 } else if (Src.isReg() &&
1126 Src.getReg() == MachineLocation::VirtualFP) {
1127 if (Dst.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001128 Asm->EmitInt8(DW_CFA_def_cfa_register);
1129 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001130 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001131 Asm->EOL("Register");
1132 } else {
1133 assert(0 && "Machine move no supported yet.");
1134 }
1135 } else {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001136 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001137 int Offset = Dst.getOffset() / stackGrowth;
aslc200b112008-08-16 12:57:46 +00001138
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001139 if (Offset < 0) {
1140 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1141 Asm->EOL("DW_CFA_offset_extended_sf");
1142 Asm->EmitULEB128Bytes(Reg);
1143 Asm->EOL("Reg");
1144 Asm->EmitSLEB128Bytes(Offset);
1145 Asm->EOL("Offset");
1146 } else if (Reg < 64) {
1147 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Cheng6181e062008-07-09 21:53:02 +00001148 if (VerboseAsm)
1149 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1150 else
1151 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001152 Asm->EmitULEB128Bytes(Offset);
1153 Asm->EOL("Offset");
1154 } else {
1155 Asm->EmitInt8(DW_CFA_offset_extended);
1156 Asm->EOL("DW_CFA_offset_extended");
1157 Asm->EmitULEB128Bytes(Reg);
1158 Asm->EOL("Reg");
1159 Asm->EmitULEB128Bytes(Offset);
1160 Asm->EOL("Offset");
1161 }
1162 }
1163 }
1164 }
1165
1166};
1167
1168//===----------------------------------------------------------------------===//
Devang Patel5f244e32009-01-05 22:35:52 +00001169/// SrcFileInfo - This class is used to track source information.
1170///
1171class SrcFileInfo {
1172 unsigned DirectoryID; // Directory ID number.
1173 std::string Name; // File name (not including directory.)
1174public:
1175 SrcFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
1176
1177 // Accessors
1178 unsigned getDirectoryID() const { return DirectoryID; }
1179 const std::string &getName() const { return Name; }
1180
1181 /// operator== - Used by UniqueVector to locate entry.
1182 ///
1183 bool operator==(const SourceFileInfo &SI) const {
1184 return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
1185 }
1186
1187 /// operator< - Used by UniqueVector to locate entry.
1188 ///
1189 bool operator<(const SrcFileInfo &SI) const {
1190 return getDirectoryID() < SI.getDirectoryID() ||
1191 (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
1192 }
1193};
1194
1195//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00001196/// DwarfDebug - Emits Dwarf debug directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001197///
1198class DwarfDebug : public Dwarf {
1199
1200private:
1201 //===--------------------------------------------------------------------===//
1202 // Attributes used to construct specific Dwarf sections.
1203 //
aslc200b112008-08-16 12:57:46 +00001204
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001205 /// CompileUnits - All the compile units involved in this build. The index
1206 /// of each entry in this vector corresponds to the sources in MMI.
1207 std::vector<CompileUnit *> CompileUnits;
Devang Patel5f244e32009-01-05 22:35:52 +00001208 DenseMap<GlobalVariable *, CompileUnit *> DW_CUs;
aslc200b112008-08-16 12:57:46 +00001209
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001210 /// AbbreviationsSet - Used to uniquely define abbreviations.
1211 ///
1212 FoldingSet<DIEAbbrev> AbbreviationsSet;
1213
1214 /// Abbreviations - A list of all the unique abbreviations in use.
1215 ///
1216 std::vector<DIEAbbrev *> Abbreviations;
aslc200b112008-08-16 12:57:46 +00001217
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001218 /// ValuesSet - Used to uniquely define values.
1219 ///
Devang Patel5f244e32009-01-05 22:35:52 +00001220 // Directories - Uniquing vector for directories.
1221 UniqueVector<std::string> Directories;
1222
1223 // SourceFiles - Uniquing vector for source files.
1224 UniqueVector<SrcFileInfo> SrcFiles;
1225
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001226 FoldingSet<DIEValue> ValuesSet;
aslc200b112008-08-16 12:57:46 +00001227
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001228 /// Values - A list of all the unique values in use.
1229 ///
1230 std::vector<DIEValue *> Values;
aslc200b112008-08-16 12:57:46 +00001231
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001232 /// StringPool - A UniqueVector of strings used by indirect references.
1233 ///
1234 UniqueVector<std::string> StringPool;
1235
1236 /// UnitMap - Map debug information descriptor to compile unit.
1237 ///
1238 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
aslc200b112008-08-16 12:57:46 +00001239
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001240 /// SectionMap - Provides a unique id per text section.
1241 ///
Anton Korobeynikov55b94962008-09-24 22:15:21 +00001242 UniqueVector<const Section*> SectionMap;
aslc200b112008-08-16 12:57:46 +00001243
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001244 /// SectionSourceLines - Tracks line numbers per text section.
1245 ///
1246 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
1247
1248 /// didInitial - Flag to indicate if initial emission has been done.
1249 ///
1250 bool didInitial;
aslc200b112008-08-16 12:57:46 +00001251
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001252 /// shouldEmit - Flag to indicate if debug information should be emitted.
1253 ///
1254 bool shouldEmit;
1255
1256 struct FunctionDebugFrameInfo {
1257 unsigned Number;
1258 std::vector<MachineMove> Moves;
1259
1260 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman9ba5d4d2007-08-27 14:50:10 +00001261 Number(Num), Moves(M) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001262 };
1263
1264 std::vector<FunctionDebugFrameInfo> DebugFrames;
aslc200b112008-08-16 12:57:46 +00001265
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001266public:
aslc200b112008-08-16 12:57:46 +00001267
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001268 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1269 ///
1270 bool ShouldEmitDwarf() const { return shouldEmit; }
1271
1272 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
aslc200b112008-08-16 12:57:46 +00001273 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001274 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1275 // Profile the node so that we can make it unique.
1276 FoldingSetNodeID ID;
1277 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00001278
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001279 // Check the set for priors.
1280 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
aslc200b112008-08-16 12:57:46 +00001281
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001282 // If it's newly added.
1283 if (InSet == &Abbrev) {
aslc200b112008-08-16 12:57:46 +00001284 // Add to abbreviation list.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001285 Abbreviations.push_back(&Abbrev);
1286 // Assign the vector position + 1 as its number.
1287 Abbrev.setNumber(Abbreviations.size());
1288 } else {
1289 // Assign existing abbreviation number.
1290 Abbrev.setNumber(InSet->getNumber());
1291 }
1292 }
1293
1294 /// NewString - Add a string to the constant pool and returns a label.
1295 ///
1296 DWLabel NewString(const std::string &String) {
1297 unsigned StringID = StringPool.insert(String);
1298 return DWLabel("string", StringID);
1299 }
aslc200b112008-08-16 12:57:46 +00001300
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001301 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1302 /// entry.
1303 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1304 DIEntry *Value;
aslc200b112008-08-16 12:57:46 +00001305
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001306 if (Entry) {
1307 FoldingSetNodeID ID;
1308 DIEntry::Profile(ID, Entry);
1309 void *Where;
1310 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
aslc200b112008-08-16 12:57:46 +00001311
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001312 if (Value) return Value;
aslc200b112008-08-16 12:57:46 +00001313
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001314 Value = new DIEntry(Entry);
1315 ValuesSet.InsertNode(Value, Where);
1316 } else {
1317 Value = new DIEntry(Entry);
1318 }
aslc200b112008-08-16 12:57:46 +00001319
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001320 Values.push_back(Value);
1321 return Value;
1322 }
aslc200b112008-08-16 12:57:46 +00001323
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001324 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1325 ///
1326 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1327 Value->Entry = Entry;
1328 // Add to values set if not already there. If it is, we merely have a
1329 // duplicate in the values list (no harm.)
1330 ValuesSet.GetOrInsertNode(Value);
1331 }
1332
1333 /// AddUInt - Add an unsigned integer attribute data and value.
1334 ///
1335 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1336 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1337
1338 FoldingSetNodeID ID;
1339 DIEInteger::Profile(ID, Integer);
1340 void *Where;
1341 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1342 if (!Value) {
1343 Value = new DIEInteger(Integer);
1344 ValuesSet.InsertNode(Value, Where);
1345 Values.push_back(Value);
1346 }
aslc200b112008-08-16 12:57:46 +00001347
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001348 Die->AddValue(Attribute, Form, Value);
1349 }
aslc200b112008-08-16 12:57:46 +00001350
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001351 /// AddSInt - Add an signed integer attribute data and value.
1352 ///
1353 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1354 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1355
1356 FoldingSetNodeID ID;
1357 DIEInteger::Profile(ID, (uint64_t)Integer);
1358 void *Where;
1359 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1360 if (!Value) {
1361 Value = new DIEInteger(Integer);
1362 ValuesSet.InsertNode(Value, Where);
1363 Values.push_back(Value);
1364 }
aslc200b112008-08-16 12:57:46 +00001365
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001366 Die->AddValue(Attribute, Form, Value);
1367 }
aslc200b112008-08-16 12:57:46 +00001368
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001369 /// AddString - Add a std::string attribute data and value.
1370 ///
1371 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1372 const std::string &String) {
1373 FoldingSetNodeID ID;
1374 DIEString::Profile(ID, String);
1375 void *Where;
1376 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1377 if (!Value) {
1378 Value = new DIEString(String);
1379 ValuesSet.InsertNode(Value, Where);
1380 Values.push_back(Value);
1381 }
aslc200b112008-08-16 12:57:46 +00001382
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001383 Die->AddValue(Attribute, Form, Value);
1384 }
aslc200b112008-08-16 12:57:46 +00001385
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001386 /// AddLabel - Add a Dwarf label attribute data and value.
1387 ///
1388 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1389 const DWLabel &Label) {
1390 FoldingSetNodeID ID;
1391 DIEDwarfLabel::Profile(ID, Label);
1392 void *Where;
1393 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1394 if (!Value) {
1395 Value = new DIEDwarfLabel(Label);
1396 ValuesSet.InsertNode(Value, Where);
1397 Values.push_back(Value);
1398 }
aslc200b112008-08-16 12:57:46 +00001399
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001400 Die->AddValue(Attribute, Form, Value);
1401 }
aslc200b112008-08-16 12:57:46 +00001402
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001403 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1404 ///
1405 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1406 const std::string &Label) {
1407 FoldingSetNodeID ID;
1408 DIEObjectLabel::Profile(ID, Label);
1409 void *Where;
1410 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1411 if (!Value) {
1412 Value = new DIEObjectLabel(Label);
1413 ValuesSet.InsertNode(Value, Where);
1414 Values.push_back(Value);
1415 }
aslc200b112008-08-16 12:57:46 +00001416
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001417 Die->AddValue(Attribute, Form, Value);
1418 }
aslc200b112008-08-16 12:57:46 +00001419
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001420 /// AddSectionOffset - Add a section offset label attribute data and value.
1421 ///
1422 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1423 const DWLabel &Label, const DWLabel &Section,
1424 bool isEH = false, bool useSet = true) {
1425 FoldingSetNodeID ID;
1426 DIESectionOffset::Profile(ID, Label, Section);
1427 void *Where;
1428 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1429 if (!Value) {
1430 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1431 ValuesSet.InsertNode(Value, Where);
1432 Values.push_back(Value);
1433 }
aslc200b112008-08-16 12:57:46 +00001434
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001435 Die->AddValue(Attribute, Form, Value);
1436 }
aslc200b112008-08-16 12:57:46 +00001437
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001438 /// AddDelta - Add a label delta attribute data and value.
1439 ///
1440 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1441 const DWLabel &Hi, const DWLabel &Lo) {
1442 FoldingSetNodeID ID;
1443 DIEDelta::Profile(ID, Hi, Lo);
1444 void *Where;
1445 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1446 if (!Value) {
1447 Value = new DIEDelta(Hi, Lo);
1448 ValuesSet.InsertNode(Value, Where);
1449 Values.push_back(Value);
1450 }
aslc200b112008-08-16 12:57:46 +00001451
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001452 Die->AddValue(Attribute, Form, Value);
1453 }
aslc200b112008-08-16 12:57:46 +00001454
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001455 /// AddDIEntry - Add a DIE attribute data and value.
1456 ///
1457 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1458 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1459 }
1460
1461 /// AddBlock - Add block data.
1462 ///
1463 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1464 Block->ComputeSize(*this);
1465 FoldingSetNodeID ID;
1466 Block->Profile(ID);
1467 void *Where;
1468 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1469 if (!Value) {
1470 Value = Block;
1471 ValuesSet.InsertNode(Value, Where);
1472 Values.push_back(Value);
1473 } else {
Chris Lattner3de66892007-09-21 18:25:53 +00001474 // Already exists, reuse the previous one.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001475 delete Block;
Chris Lattner3de66892007-09-21 18:25:53 +00001476 Block = cast<DIEBlock>(Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001477 }
aslc200b112008-08-16 12:57:46 +00001478
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001479 Die->AddValue(Attribute, Block->BestForm(), Value);
1480 }
1481
1482private:
1483
1484 /// AddSourceLine - Add location information to specified debug information
1485 /// entry.
1486 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1487 if (File && Line) {
1488 CompileUnit *FileUnit = FindCompileUnit(File);
1489 unsigned FileID = FileUnit->getID();
1490 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1491 AddUInt(Die, DW_AT_decl_line, 0, Line);
1492 }
1493 }
1494
Devang Patel5f244e32009-01-05 22:35:52 +00001495 /// AddSourceLine - Add location information to specified debug information
1496 /// entry.
1497 void AddSourceLine(DIE *Die, DIGlobal *G) {
1498 unsigned FileID = 0;
1499 unsigned Line = G->getLineNumber();
1500 if (G->getVersion() < DIDescriptor::Version7) {
1501 // Version6 or earlier. Use compile unit info to get file id.
1502 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1503 FileID = Unit->getID();
1504 } else {
1505 // Version7 or newer, use filename and directory info from DIGlobal
1506 // directly.
1507 unsigned DID = Directories.idFor(G->getDirectory());
1508 FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1509 }
1510 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1511 AddUInt(Die, DW_AT_decl_line, 0, Line);
1512 }
1513
1514 void AddSourceLine(DIE *Die, DIType *G) {
1515 unsigned FileID = 0;
1516 unsigned Line = G->getLineNumber();
1517 if (G->getVersion() < DIDescriptor::Version7) {
1518 // Version6 or earlier. Use compile unit info to get file id.
1519 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1520 FileID = Unit->getID();
1521 } else {
1522 // Version7 or newer, use filename and directory info from DIGlobal
1523 // directly.
1524 unsigned DID = Directories.idFor(G->getDirectory());
1525 FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1526 }
1527 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1528 AddUInt(Die, DW_AT_decl_line, 0, Line);
1529 }
1530
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001531 /// AddAddress - Add an address attribute to a die based on the location
1532 /// provided.
1533 void AddAddress(DIE *Die, unsigned Attribute,
1534 const MachineLocation &Location) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001535 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001536 DIEBlock *Block = new DIEBlock();
aslc200b112008-08-16 12:57:46 +00001537
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001538 if (Location.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001539 if (Reg < 32) {
1540 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1541 } else {
1542 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1543 AddUInt(Block, 0, DW_FORM_udata, Reg);
1544 }
1545 } else {
1546 if (Reg < 32) {
1547 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1548 } else {
1549 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1550 AddUInt(Block, 0, DW_FORM_udata, Reg);
1551 }
1552 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1553 }
aslc200b112008-08-16 12:57:46 +00001554
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001555 AddBlock(Die, Attribute, 0, Block);
1556 }
aslc200b112008-08-16 12:57:46 +00001557
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001558 /// AddBasicType - Add a new basic type attribute to the specified entity.
1559 ///
1560 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1561 const std::string &Name,
1562 unsigned Encoding, unsigned Size) {
aslc200b112008-08-16 12:57:46 +00001563
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001564 DIE Buffer(DW_TAG_base_type);
1565 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1566 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1567 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf49e13d2009-01-05 17:44:11 +00001568 DIE *BasicTypeDie = Unit->AddDie(Buffer);
1569 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, BasicTypeDie);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001570 }
aslc200b112008-08-16 12:57:46 +00001571
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001572 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1573 ///
1574 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001575 DIE Buffer(DW_TAG_pointer_type);
Dan Gohmancfb72b22007-09-27 23:12:31 +00001576 AddUInt(&Buffer, DW_AT_byte_size, 0, TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001577 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelbbca50b2009-01-05 17:45:59 +00001578 DIE *PointerTypeDie = Unit->AddDie(Buffer);
1579 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, PointerTypeDie);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001580 }
aslc200b112008-08-16 12:57:46 +00001581
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001582 /// AddType - Add a new type attribute to the specified entity.
1583 ///
1584 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1585 if (!TyDesc) {
1586 AddBasicType(Entity, Unit, "", DW_ATE_signed, sizeof(int32_t));
1587 } else {
1588 // Check for pre-existence.
1589 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
aslc200b112008-08-16 12:57:46 +00001590
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001591 // If it exists then use the existing value.
1592 if (Slot) {
1593 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1594 return;
1595 }
aslc200b112008-08-16 12:57:46 +00001596
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001597 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1598 // FIXME - Not sure why programs and variables are coming through here.
1599 // Short cut for handling subprogram types (not really a TyDesc.)
1600 AddPointerType(Entity, Unit, SubprogramTy->getName());
1601 } else if (GlobalVariableDesc *GlobalTy =
1602 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1603 // FIXME - Not sure why programs and variables are coming through here.
1604 // Short cut for handling global variable types (not really a TyDesc.)
1605 AddPointerType(Entity, Unit, GlobalTy->getName());
aslc200b112008-08-16 12:57:46 +00001606 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001607 // Set up proxy.
1608 Slot = NewDIEntry();
aslc200b112008-08-16 12:57:46 +00001609
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001610 // Construct type.
1611 DIE Buffer(DW_TAG_base_type);
1612 ConstructType(Buffer, TyDesc, Unit);
aslc200b112008-08-16 12:57:46 +00001613
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001614 // Add debug information entry to entity and unit.
1615 DIE *Die = Unit->AddDie(Buffer);
1616 SetDIEntry(Slot, Die);
1617 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1618 }
1619 }
1620 }
aslc200b112008-08-16 12:57:46 +00001621
Devang Patel4a4cbe72009-01-05 21:47:57 +00001622 /// AddType - Add a new type attribute to the specified entity.
1623 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
1624 if (Ty.isNull()) {
1625 AddBasicType(Entity, DW_Unit, "", DW_ATE_signed, sizeof(int32_t));
1626 return;
1627 }
1628
1629 // Check for pre-existence.
1630 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1631 // If it exists then use the existing value.
1632 if (Slot) {
1633 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1634 return;
1635 }
1636
1637 // Set up proxy.
1638 Slot = NewDIEntry();
1639
1640 // Construct type.
1641 DIE Buffer(DW_TAG_base_type);
1642 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1643 ConstructTypeDIE(DW_Unit, Buffer, BT);
1644 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1645 ConstructTypeDIE(DW_Unit, Buffer, DT);
1646 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1647 ConstructTypeDIE(DW_Unit, Buffer, CT);
1648
1649 // Add debug information entry to entity and unit.
1650 DIE *Die = DW_Unit->AddDie(Buffer);
1651 SetDIEntry(Slot, Die);
1652 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1653 }
1654
Devang Patel46d13752009-01-05 19:07:53 +00001655 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1656 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1657 DIBasicType *BTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001658
1659 // Get core information.
1660 const std::string &Name = BTy->getName();
1661 Buffer.setTag(DW_TAG_base_type);
1662 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy->getEncoding());
1663 // Add name if not anonymous or intermediate type.
1664 if (!Name.empty())
1665 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1666 uint64_t Size = BTy->getSizeInBits() >> 3;
1667 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1668 }
1669
Devang Patel46d13752009-01-05 19:07:53 +00001670 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1671 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1672 DIDerivedType *DTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001673
1674 // Get core information.
1675 const std::string &Name = DTy->getName();
1676 uint64_t Size = DTy->getSizeInBits() >> 3;
1677 unsigned Tag = DTy->getTag();
1678 // FIXME - Workaround for templates.
1679 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1680
1681 Buffer.setTag(Tag);
1682 // Map to main type, void will not have a type.
1683 DIType FromTy = DTy->getTypeDerivedFrom();
Devang Patel4a4cbe72009-01-05 21:47:57 +00001684 AddType(DW_Unit, &Buffer, FromTy);
Devang Patelfc187162009-01-05 17:57:47 +00001685
1686 // Add name if not anonymous or intermediate type.
1687 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1688
1689 // Add size if non-zero (derived types might be zero-sized.)
1690 if (Size)
1691 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1692
1693 // Add source line info if available and TyDesc is not a forward
1694 // declaration.
1695 // FIXME - Enable this. if (!DTy->isForwardDecl())
1696 // FIXME - Enable this. AddSourceLine(&Buffer, *DTy);
1697 }
1698
Devang Patel30c01372009-01-05 19:55:51 +00001699 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1700 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1701 DICompositeType *CTy) {
1702
1703 // Get core information.
1704 const std::string &Name = CTy->getName();
1705 uint64_t Size = CTy->getSizeInBits() >> 3;
1706 unsigned Tag = CTy->getTag();
1707 switch (Tag) {
1708 case DW_TAG_vector_type:
1709 case DW_TAG_array_type:
1710 ConstructArrayTypeDIE(DW_Unit, Buffer, CTy);
1711 break;
1712 //FIXME - Enable this.
1713 // case DW_TAG_enumeration_type:
1714 // DIArray Elements = CTy->getTypeArray();
1715 // // Add enumerators to enumeration type.
1716 // for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i)
1717 // ConstructEnumTypeDIE(Buffer, &Elements.getElement(i));
1718 // break;
1719 case DW_TAG_subroutine_type:
1720 {
1721 // Add prototype flag.
1722 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1723 DIArray Elements = CTy->getTypeArray();
1724 // Add return type.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001725 DIDescriptor RTy = Elements.getElement(0);
1726 if (DIBasicType *BT = dyn_cast<DIBasicType>(&RTy))
1727 AddType(DW_Unit, &Buffer, *BT);
1728 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&RTy))
1729 AddType(DW_Unit, &Buffer, *DT);
1730 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&RTy))
1731 AddType(DW_Unit, &Buffer, *CT);
1732
1733 //AddType(DW_Unit, &Buffer, Elements.getElement(0));
Devang Patel30c01372009-01-05 19:55:51 +00001734 // Add arguments.
1735 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1736 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel4a4cbe72009-01-05 21:47:57 +00001737 DIDescriptor Ty = Elements.getElement(i);
1738 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1739 AddType(DW_Unit, &Buffer, *BT);
1740 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1741 AddType(DW_Unit, &Buffer, *DT);
1742 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1743 AddType(DW_Unit, &Buffer, *CT);
Devang Patel30c01372009-01-05 19:55:51 +00001744 Buffer.AddChild(Arg);
1745 }
1746 }
1747 break;
1748 case DW_TAG_structure_type:
1749 case DW_TAG_union_type:
1750 {
1751 // Add elements to structure type.
1752 DIArray Elements = CTy->getTypeArray();
1753 // Add elements to structure type.
1754 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1755 DIDescriptor Element = Elements.getElement(i);
1756 if (DISubprogram *SP = dyn_cast<DISubprogram>(&Element))
1757 ConstructFieldTypeDIE(DW_Unit, Buffer, SP);
1758 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Element))
1759 ConstructFieldTypeDIE(DW_Unit, Buffer, DT);
1760 else if (DIGlobalVariable *GV = dyn_cast<DIGlobalVariable>(&Element))
1761 ConstructFieldTypeDIE(DW_Unit, Buffer, GV);
1762 }
1763 }
1764 break;
1765 default:
1766 break;
1767 }
1768
1769 // Add name if not anonymous or intermediate type.
1770 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1771
1772 // Add size if non-zero (derived types might be zero-sized.)
1773 if (Size)
1774 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1775 else {
1776 // Add zero size even if it is not a forward declaration.
1777 // FIXME - Enable this.
1778 // if (!CTy->isDefinition())
1779 // AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1780 // else
1781 // AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1782 }
1783
1784 // Add source line info if available and TyDesc is not a forward
1785 // declaration.
1786 // FIXME - Enable this.
1787 // if (CTy->isForwardDecl())
1788 // AddSourceLine(&Buffer, *CTy);
1789 }
1790
Devang Patel6fb54132009-01-05 18:33:01 +00001791 // ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1792 void ConstructSubrangeDIE (DIE &Buffer, DISubrange *SR, DIE *IndexTy) {
1793 int64_t L = SR->getLo();
1794 int64_t H = SR->getHi();
1795 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1796 if (L != H) {
1797 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1798 if (L)
1799 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1800 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
1801 }
1802 Buffer.AddChild(DW_Subrange);
1803 }
1804
1805 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1806 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1807 DICompositeType *CTy) {
1808 Buffer.setTag(DW_TAG_array_type);
1809 if (CTy->getTag() == DW_TAG_vector_type)
1810 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1811
1812 DIArray Elements = CTy->getTypeArray();
1813 // FIXME - Enable this.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001814 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel6fb54132009-01-05 18:33:01 +00001815
1816 // Construct an anonymous type for index type.
1817 DIE IdxBuffer(DW_TAG_base_type);
1818 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1819 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1820 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1821
1822 // Add subranges to array type.
1823 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel30c01372009-01-05 19:55:51 +00001824 DIDescriptor Element = Elements.getElement(i);
1825 if (DISubrange *SR = dyn_cast<DISubrange>(&Element))
1826 ConstructSubrangeDIE(Buffer, SR, IndexTy);
Devang Patel6fb54132009-01-05 18:33:01 +00001827 }
1828 }
1829
Devang Patela566e812009-01-05 18:38:38 +00001830 /// ConstructEnumTypeDIE - Construct enum type DIE from
1831 /// DIEnumerator.
Devang Patel30c01372009-01-05 19:55:51 +00001832 void ConstructEnumTypeDIE(CompileUnit *DW_Unit,
1833 DIE &Buffer, DIEnumerator *ETy) {
Devang Patela566e812009-01-05 18:38:38 +00001834
1835 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1836 AddString(Enumerator, DW_AT_name, DW_FORM_string, ETy->getName());
1837 int64_t Value = ETy->getEnumValue();
1838 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1839 Buffer.AddChild(Enumerator);
1840 }
Devang Patel6fb54132009-01-05 18:33:01 +00001841
Devang Patel526b01d2009-01-05 18:59:44 +00001842 /// ConstructFieldTypeDIE - Construct variable DIE for a struct field.
1843 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
1844 DIE &Buffer, DIGlobalVariable *V) {
1845
1846 DIE *VariableDie = new DIE(DW_TAG_variable);
1847 const std::string &LinkageName = V->getLinkageName();
1848 if (!LinkageName.empty())
1849 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1850 LinkageName);
1851 // FIXME - Enable this. AddSourceLine(VariableDie, V);
Devang Patel4a4cbe72009-01-05 21:47:57 +00001852 AddType(DW_Unit, VariableDie, V->getType());
Devang Patel526b01d2009-01-05 18:59:44 +00001853 if (!V->isLocalToUnit())
1854 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
1855 AddUInt(VariableDie, DW_AT_declaration, DW_FORM_flag, 1);
1856 Buffer.AddChild(VariableDie);
1857 }
1858
1859 /// ConstructFieldTypeDIE - Construct subprogram DIE for a struct field.
1860 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
1861 DIE &Buffer, DISubprogram *SP,
1862 bool IsConstructor = false) {
1863 DIE *Method = new DIE(DW_TAG_subprogram);
1864 AddString(Method, DW_AT_name, DW_FORM_string, SP->getName());
1865 const std::string &LinkageName = SP->getLinkageName();
1866 if (!LinkageName.empty())
1867 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1868 // FIXME - Enable this. AddSourceLine(Method, SP);
1869
1870 DICompositeType MTy = SP->getType();
1871 DIArray Args = MTy.getTypeArray();
1872
1873 // Add Return Type.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001874 if (!IsConstructor) {
1875 DIDescriptor Ty = Args.getElement(0);
1876 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1877 AddType(DW_Unit, Method, *BT);
1878 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1879 AddType(DW_Unit, Method, *DT);
1880 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1881 AddType(DW_Unit, Method, *CT);
1882 }
Devang Patel526b01d2009-01-05 18:59:44 +00001883
1884 // Add arguments.
1885 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1886 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel4a4cbe72009-01-05 21:47:57 +00001887 DIDescriptor Ty = Args.getElement(i);
1888 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1889 AddType(DW_Unit, Method, *BT);
1890 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1891 AddType(DW_Unit, Method, *DT);
1892 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1893 AddType(DW_Unit, Method, *CT);
Devang Patel526b01d2009-01-05 18:59:44 +00001894 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
1895 Method->AddChild(Arg);
1896 }
1897
1898 if (!SP->isLocalToUnit())
1899 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
1900 Buffer.AddChild(Method);
1901 }
1902
1903 /// COnstructFieldTypeDIE - Construct derived type DIE for a struct field.
1904 void ConstructFieldTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1905 DIDerivedType *DTy) {
1906 unsigned Tag = DTy->getTag();
1907 DIE *MemberDie = new DIE(Tag);
1908 if (!DTy->getName().empty())
1909 AddString(MemberDie, DW_AT_name, DW_FORM_string, DTy->getName());
1910 // FIXME - Enable this. AddSourceLine(MemberDie, DTy);
1911
1912 DIType FromTy = DTy->getTypeDerivedFrom();
Devang Patel4a4cbe72009-01-05 21:47:57 +00001913 AddType(DW_Unit, MemberDie, FromTy);
Devang Patel526b01d2009-01-05 18:59:44 +00001914
1915 uint64_t Size = DTy->getSizeInBits();
1916 uint64_t Offset = DTy->getOffsetInBits();
1917
1918 // FIXME Handle bitfields
1919
1920 // Add size.
1921 AddUInt(MemberDie, DW_AT_bit_size, 0, Size);
1922 // Add computation for offset.
1923 DIEBlock *Block = new DIEBlock();
1924 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1925 AddUInt(Block, 0, DW_FORM_udata, Offset >> 3);
1926 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1927
1928 // FIXME Handle DW_AT_accessibility.
1929
1930 Buffer.AddChild(MemberDie);
1931 }
1932
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001933 /// ConstructType - Adds all the required attributes to the type.
1934 ///
1935 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
1936 // Get core information.
1937 const std::string &Name = TyDesc->getName();
1938 uint64_t Size = TyDesc->getSize() >> 3;
aslc200b112008-08-16 12:57:46 +00001939
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001940 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1941 // Fundamental types like int, float, bool
1942 Buffer.setTag(DW_TAG_base_type);
1943 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
1944 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
1945 // Fetch tag.
1946 unsigned Tag = DerivedTy->getTag();
1947 // FIXME - Workaround for templates.
1948 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
aslc200b112008-08-16 12:57:46 +00001949 // Pointers, typedefs et al.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001950 Buffer.setTag(Tag);
1951 // Map to main type, void will not have a type.
1952 if (TypeDesc *FromTy = DerivedTy->getFromType())
1953 AddType(&Buffer, FromTy, Unit);
1954 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
1955 // Fetch tag.
1956 unsigned Tag = CompTy->getTag();
aslc200b112008-08-16 12:57:46 +00001957
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001958 // Set tag accordingly.
1959 if (Tag == DW_TAG_vector_type)
1960 Buffer.setTag(DW_TAG_array_type);
aslc200b112008-08-16 12:57:46 +00001961 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001962 Buffer.setTag(Tag);
1963
1964 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
aslc200b112008-08-16 12:57:46 +00001965
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001966 switch (Tag) {
1967 case DW_TAG_vector_type:
1968 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1969 // Fall thru
1970 case DW_TAG_array_type: {
1971 // Add element type.
1972 if (TypeDesc *FromTy = CompTy->getFromType())
1973 AddType(&Buffer, FromTy, Unit);
aslc200b112008-08-16 12:57:46 +00001974
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001975 // Don't emit size attribute.
1976 Size = 0;
aslc200b112008-08-16 12:57:46 +00001977
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001978 // Construct an anonymous type for index type.
Devang Patelf49e13d2009-01-05 17:44:11 +00001979 DIE Buffer(DW_TAG_base_type);
1980 AddUInt(&Buffer, DW_AT_byte_size, 0, sizeof(int32_t));
1981 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1982 DIE *IndexTy = Unit->AddDie(Buffer);
aslc200b112008-08-16 12:57:46 +00001983
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001984 // Add subranges to array type.
Evan Chengc7efea32008-12-09 17:56:30 +00001985 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001986 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1987 int64_t Lo = SRD->getLo();
1988 int64_t Hi = SRD->getHi();
1989 DIE *Subrange = new DIE(DW_TAG_subrange_type);
aslc200b112008-08-16 12:57:46 +00001990
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001991 // If a range is available.
1992 if (Lo != Hi) {
1993 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1994 // Only add low if non-zero.
1995 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
1996 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
1997 }
aslc200b112008-08-16 12:57:46 +00001998
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001999 Buffer.AddChild(Subrange);
2000 }
2001 break;
2002 }
2003 case DW_TAG_structure_type:
2004 case DW_TAG_union_type: {
2005 // Add elements to structure type.
Evan Chengc7efea32008-12-09 17:56:30 +00002006 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002007 DebugInfoDesc *Element = Elements[i];
aslc200b112008-08-16 12:57:46 +00002008
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002009 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
2010 // Add field or base class.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002011 unsigned Tag = MemberDesc->getTag();
aslc200b112008-08-16 12:57:46 +00002012
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002013 // Extract the basic information.
2014 const std::string &Name = MemberDesc->getName();
2015 uint64_t Size = MemberDesc->getSize();
2016 uint64_t Align = MemberDesc->getAlign();
2017 uint64_t Offset = MemberDesc->getOffset();
aslc200b112008-08-16 12:57:46 +00002018
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002019 // Construct member debug information entry.
2020 DIE *Member = new DIE(Tag);
aslc200b112008-08-16 12:57:46 +00002021
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002022 // Add name if not "".
2023 if (!Name.empty())
2024 AddString(Member, DW_AT_name, DW_FORM_string, Name);
Evan Chengc7efea32008-12-09 17:56:30 +00002025
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002026 // Add location if available.
2027 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
aslc200b112008-08-16 12:57:46 +00002028
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002029 // Most of the time the field info is the same as the members.
2030 uint64_t FieldSize = Size;
2031 uint64_t FieldAlign = Align;
2032 uint64_t FieldOffset = Offset;
aslc200b112008-08-16 12:57:46 +00002033
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002034 // Set the member type.
2035 TypeDesc *FromTy = MemberDesc->getFromType();
2036 AddType(Member, FromTy, Unit);
aslc200b112008-08-16 12:57:46 +00002037
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002038 // Walk up typedefs until a real size is found.
2039 while (FromTy) {
2040 if (FromTy->getTag() != DW_TAG_typedef) {
2041 FieldSize = FromTy->getSize();
Devang Patel105a08a2008-12-23 21:55:38 +00002042 FieldAlign = FromTy->getAlign();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002043 break;
2044 }
aslc200b112008-08-16 12:57:46 +00002045
Dan Gohman53491e92007-07-23 20:24:29 +00002046 FromTy = cast<DerivedTypeDesc>(FromTy)->getFromType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002047 }
aslc200b112008-08-16 12:57:46 +00002048
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002049 // Unless we have a bit field.
2050 if (Tag == DW_TAG_member && FieldSize != Size) {
2051 // Construct the alignment mask.
2052 uint64_t AlignMask = ~(FieldAlign - 1);
2053 // Determine the high bit + 1 of the declared size.
2054 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
2055 // Work backwards to determine the base offset of the field.
2056 FieldOffset = HiMark - FieldSize;
2057 // Now normalize offset to the field.
2058 Offset -= FieldOffset;
aslc200b112008-08-16 12:57:46 +00002059
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002060 // Maybe we need to work from the other end.
2061 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
aslc200b112008-08-16 12:57:46 +00002062
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002063 // Add size and offset.
2064 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
2065 AddUInt(Member, DW_AT_bit_size, 0, Size);
2066 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
2067 }
aslc200b112008-08-16 12:57:46 +00002068
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002069 // Add computation for offset.
2070 DIEBlock *Block = new DIEBlock();
2071 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
2072 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
2073 AddBlock(Member, DW_AT_data_member_location, 0, Block);
2074
2075 // Add accessibility (public default unless is base class.
2076 if (MemberDesc->isProtected()) {
2077 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
2078 } else if (MemberDesc->isPrivate()) {
2079 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
2080 } else if (Tag == DW_TAG_inheritance) {
2081 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
2082 }
aslc200b112008-08-16 12:57:46 +00002083
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002084 Buffer.AddChild(Member);
2085 } else if (GlobalVariableDesc *StaticDesc =
2086 dyn_cast<GlobalVariableDesc>(Element)) {
2087 // Add static member.
aslc200b112008-08-16 12:57:46 +00002088
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002089 // Construct member debug information entry.
2090 DIE *Static = new DIE(DW_TAG_variable);
aslc200b112008-08-16 12:57:46 +00002091
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002092 // Add name and mangled name.
2093 const std::string &Name = StaticDesc->getName();
2094 const std::string &LinkageName = StaticDesc->getLinkageName();
2095 AddString(Static, DW_AT_name, DW_FORM_string, Name);
2096 if (!LinkageName.empty()) {
2097 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
2098 LinkageName);
2099 }
aslc200b112008-08-16 12:57:46 +00002100
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002101 // Add location.
2102 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
aslc200b112008-08-16 12:57:46 +00002103
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002104 // Add type.
2105 if (TypeDesc *StaticTy = StaticDesc->getType())
2106 AddType(Static, StaticTy, Unit);
aslc200b112008-08-16 12:57:46 +00002107
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002108 // Add flags.
2109 if (!StaticDesc->isStatic())
2110 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
2111 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
aslc200b112008-08-16 12:57:46 +00002112
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002113 Buffer.AddChild(Static);
2114 } else if (SubprogramDesc *MethodDesc =
2115 dyn_cast<SubprogramDesc>(Element)) {
2116 // Add member function.
aslc200b112008-08-16 12:57:46 +00002117
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002118 // Construct member debug information entry.
2119 DIE *Method = new DIE(DW_TAG_subprogram);
aslc200b112008-08-16 12:57:46 +00002120
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002121 // Add name and mangled name.
2122 const std::string &Name = MethodDesc->getName();
2123 const std::string &LinkageName = MethodDesc->getLinkageName();
aslc200b112008-08-16 12:57:46 +00002124
2125 AddString(Method, DW_AT_name, DW_FORM_string, Name);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002126 bool IsCTor = TyDesc->getName() == Name;
aslc200b112008-08-16 12:57:46 +00002127
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002128 if (!LinkageName.empty()) {
2129 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
2130 LinkageName);
2131 }
aslc200b112008-08-16 12:57:46 +00002132
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002133 // Add location.
2134 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
aslc200b112008-08-16 12:57:46 +00002135
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002136 // Add type.
2137 if (CompositeTypeDesc *MethodTy =
2138 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
2139 // Get argument information.
2140 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
aslc200b112008-08-16 12:57:46 +00002141
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002142 // If not a ctor.
2143 if (!IsCTor) {
2144 // Add return type.
2145 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
2146 }
aslc200b112008-08-16 12:57:46 +00002147
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002148 // Add arguments.
Evan Chengc7efea32008-12-09 17:56:30 +00002149 for (unsigned i = 1, N = Args.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002150 DIE *Arg = new DIE(DW_TAG_formal_parameter);
2151 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
2152 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
2153 Method->AddChild(Arg);
2154 }
2155 }
2156
2157 // Add flags.
2158 if (!MethodDesc->isStatic())
2159 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
2160 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
aslc200b112008-08-16 12:57:46 +00002161
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002162 Buffer.AddChild(Method);
2163 }
2164 }
2165 break;
2166 }
2167 case DW_TAG_enumeration_type: {
2168 // Add enumerators to enumeration type.
Evan Chengc7efea32008-12-09 17:56:30 +00002169 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002170 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
2171 const std::string &Name = ED->getName();
2172 int64_t Value = ED->getValue();
2173 DIE *Enumerator = new DIE(DW_TAG_enumerator);
2174 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
2175 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
2176 Buffer.AddChild(Enumerator);
2177 }
2178
2179 break;
2180 }
2181 case DW_TAG_subroutine_type: {
2182 // Add prototype flag.
2183 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
2184 // Add return type.
2185 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
aslc200b112008-08-16 12:57:46 +00002186
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002187 // Add arguments.
Evan Chengc7efea32008-12-09 17:56:30 +00002188 for (unsigned i = 1, N = Elements.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002189 DIE *Arg = new DIE(DW_TAG_formal_parameter);
2190 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
2191 Buffer.AddChild(Arg);
2192 }
aslc200b112008-08-16 12:57:46 +00002193
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002194 break;
2195 }
2196 default: break;
2197 }
2198 }
aslc200b112008-08-16 12:57:46 +00002199
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002200 // Add name if not anonymous or intermediate type.
2201 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Evan Chengc7efea32008-12-09 17:56:30 +00002202
Evan Chengb2fc7112008-12-10 00:15:44 +00002203 // Add size if non-zero (derived types might be zero-sized.)
2204 if (Size)
2205 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
2206 else if (isa<CompositeTypeDesc>(TyDesc)) {
2207 // If TyDesc is a composite type, then add size even if it's zero unless
2208 // it's a forward declaration.
2209 if (TyDesc->isForwardDecl())
2210 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
2211 else
2212 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
2213 }
2214
2215 // Add source line info if available and TyDesc is not a forward
2216 // declaration.
2217 if (!TyDesc->isForwardDecl())
2218 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002219 }
2220
2221 /// NewCompileUnit - Create new compile unit and it's debug information entry.
2222 ///
2223 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
2224 // Construct debug information entry.
2225 DIE *Die = new DIE(DW_TAG_compile_unit);
Argiris Kirtzidis03449652008-06-18 19:27:37 +00002226 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2227 DWLabel("section_line", 0), DWLabel("section_line", 0), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002228 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
2229 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
2230 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
Devang Patel6bcf9822008-12-12 21:57:54 +00002231 if (!UnitDesc->getDirectory().empty())
2232 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
aslc200b112008-08-16 12:57:46 +00002233
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002234 // Construct compile unit.
2235 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
aslc200b112008-08-16 12:57:46 +00002236
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002237 // Add Unit to compile unit map.
2238 DescToUnitMap[UnitDesc] = Unit;
aslc200b112008-08-16 12:57:46 +00002239
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002240 return Unit;
2241 }
2242
2243 /// GetBaseCompileUnit - Get the main compile unit.
2244 ///
2245 CompileUnit *GetBaseCompileUnit() const {
2246 CompileUnit *Unit = CompileUnits[0];
2247 assert(Unit && "Missing compile unit.");
2248 return Unit;
2249 }
2250
2251 /// FindCompileUnit - Get the compile unit for the given descriptor.
2252 ///
2253 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
2254 CompileUnit *Unit = DescToUnitMap[UnitDesc];
2255 assert(Unit && "Missing compile unit.");
2256 return Unit;
2257 }
2258
Devang Patel5f244e32009-01-05 22:35:52 +00002259 /// FindCompileUnit - Get the compile unit for the given descriptor.
2260 ///
2261 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
2262 CompileUnit *DW_Unit = DW_CUs[Unit.getGV()];
2263 assert(DW_Unit && "Missing compile unit.");
2264 return DW_Unit;
2265 }
2266
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002267 /// NewGlobalVariable - Add a new global variable DIE.
2268 ///
2269 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
2270 // Get the compile unit context.
2271 CompileUnitDesc *UnitDesc =
2272 static_cast<CompileUnitDesc *>(GVD->getContext());
2273 CompileUnit *Unit = GetBaseCompileUnit();
2274
2275 // Check for pre-existence.
2276 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
2277 if (Slot) return Slot;
aslc200b112008-08-16 12:57:46 +00002278
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002279 // Get the global variable itself.
2280 GlobalVariable *GV = GVD->getGlobalVariable();
2281
2282 const std::string &Name = GVD->getName();
2283 const std::string &FullName = GVD->getFullName();
2284 const std::string &LinkageName = GVD->getLinkageName();
2285 // Create the global's variable DIE.
2286 DIE *VariableDie = new DIE(DW_TAG_variable);
2287 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
2288 if (!LinkageName.empty()) {
2289 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
2290 LinkageName);
2291 }
2292 AddType(VariableDie, GVD->getType(), Unit);
2293 if (!GVD->isStatic())
2294 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
aslc200b112008-08-16 12:57:46 +00002295
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002296 // Add source line info if available.
2297 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
aslc200b112008-08-16 12:57:46 +00002298
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002299 // Add address.
2300 DIEBlock *Block = new DIEBlock();
2301 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
2302 AddObjectLabel(Block, 0, DW_FORM_udata, Asm->getGlobalLinkName(GV));
2303 AddBlock(VariableDie, DW_AT_location, 0, Block);
aslc200b112008-08-16 12:57:46 +00002304
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002305 // Add to map.
2306 Slot = VariableDie;
aslc200b112008-08-16 12:57:46 +00002307
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002308 // Add to context owner.
2309 Unit->getDie()->AddChild(VariableDie);
aslc200b112008-08-16 12:57:46 +00002310
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002311 // Expose as global.
2312 // FIXME - need to check external flag.
2313 Unit->AddGlobal(FullName, VariableDie);
aslc200b112008-08-16 12:57:46 +00002314
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002315 return VariableDie;
2316 }
2317
2318 /// NewSubprogram - Add a new subprogram DIE.
2319 ///
2320 DIE *NewSubprogram(SubprogramDesc *SPD) {
2321 // Get the compile unit context.
2322 CompileUnitDesc *UnitDesc =
2323 static_cast<CompileUnitDesc *>(SPD->getContext());
2324 CompileUnit *Unit = GetBaseCompileUnit();
2325
2326 // Check for pre-existence.
2327 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
2328 if (Slot) return Slot;
aslc200b112008-08-16 12:57:46 +00002329
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002330 // Gather the details (simplify add attribute code.)
2331 const std::string &Name = SPD->getName();
2332 const std::string &FullName = SPD->getFullName();
2333 const std::string &LinkageName = SPD->getLinkageName();
aslc200b112008-08-16 12:57:46 +00002334
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002335 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
2336 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
2337 if (!LinkageName.empty()) {
2338 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
2339 LinkageName);
2340 }
2341 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
2342 if (!SPD->isStatic())
2343 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
2344 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
aslc200b112008-08-16 12:57:46 +00002345
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002346 // Add source line info if available.
2347 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
2348
2349 // Add to map.
2350 Slot = SubprogramDie;
aslc200b112008-08-16 12:57:46 +00002351
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002352 // Add to context owner.
2353 Unit->getDie()->AddChild(SubprogramDie);
aslc200b112008-08-16 12:57:46 +00002354
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002355 // Expose as global.
2356 Unit->AddGlobal(FullName, SubprogramDie);
aslc200b112008-08-16 12:57:46 +00002357
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002358 return SubprogramDie;
2359 }
2360
2361 /// NewScopeVariable - Create a new scope variable.
2362 ///
2363 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
2364 // Get the descriptor.
2365 VariableDesc *VD = DV->getDesc();
2366
2367 // Translate tag to proper Dwarf tag. The result variable is dropped for
2368 // now.
2369 unsigned Tag;
2370 switch (VD->getTag()) {
2371 case DW_TAG_return_variable: return NULL;
2372 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2373 case DW_TAG_auto_variable: // fall thru
2374 default: Tag = DW_TAG_variable; break;
2375 }
2376
2377 // Define variable debug information entry.
2378 DIE *VariableDie = new DIE(Tag);
2379 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
2380
2381 // Add source line info if available.
2382 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
aslc200b112008-08-16 12:57:46 +00002383
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002384 // Add variable type.
aslc200b112008-08-16 12:57:46 +00002385 AddType(VariableDie, VD->getType(), Unit);
2386
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002387 // Add variable address.
2388 MachineLocation Location;
Evan Cheng38948832008-01-31 03:37:28 +00002389 Location.set(RI->getFrameRegister(*MF),
2390 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002391 AddAddress(VariableDie, DW_AT_location, Location);
2392
2393 return VariableDie;
2394 }
2395
2396 /// ConstructScope - Construct the components of a scope.
2397 ///
2398 void ConstructScope(DebugScope *ParentScope,
2399 unsigned ParentStartID, unsigned ParentEndID,
2400 DIE *ParentDie, CompileUnit *Unit) {
2401 // Add variables to scope.
2402 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
2403 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2404 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
2405 if (VariableDie) ParentDie->AddChild(VariableDie);
2406 }
aslc200b112008-08-16 12:57:46 +00002407
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002408 // Add nested scopes.
2409 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
2410 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2411 // Define the Scope debug information entry.
2412 DebugScope *Scope = Scopes[j];
2413 // FIXME - Ignore inlined functions for the time being.
2414 if (!Scope->getParent()) continue;
aslc200b112008-08-16 12:57:46 +00002415
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002416 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2417 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
2418
2419 // Ignore empty scopes.
2420 if (StartID == EndID && StartID != 0) continue;
2421 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
aslc200b112008-08-16 12:57:46 +00002422
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002423 if (StartID == ParentStartID && EndID == ParentEndID) {
2424 // Just add stuff to the parent scope.
2425 ConstructScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2426 } else {
2427 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
aslc200b112008-08-16 12:57:46 +00002428
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002429 // Add the scope bounds.
2430 if (StartID) {
2431 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2432 DWLabel("label", StartID));
2433 } else {
2434 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2435 DWLabel("func_begin", SubprogramCount));
2436 }
2437 if (EndID) {
2438 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2439 DWLabel("label", EndID));
2440 } else {
2441 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2442 DWLabel("func_end", SubprogramCount));
2443 }
aslc200b112008-08-16 12:57:46 +00002444
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002445 // Add the scope contents.
2446 ConstructScope(Scope, StartID, EndID, ScopeDie, Unit);
2447 ParentDie->AddChild(ScopeDie);
2448 }
2449 }
2450 }
2451
2452 /// ConstructRootScope - Construct the scope for the subprogram.
2453 ///
2454 void ConstructRootScope(DebugScope *RootScope) {
2455 // Exit if there is no root scope.
2456 if (!RootScope) return;
aslc200b112008-08-16 12:57:46 +00002457
2458 // Get the subprogram debug information entry.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002459 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
aslc200b112008-08-16 12:57:46 +00002460
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002461 // Get the compile unit context.
2462 CompileUnit *Unit = GetBaseCompileUnit();
aslc200b112008-08-16 12:57:46 +00002463
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002464 // Get the subprogram die.
2465 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2466 assert(SPDie && "Missing subprogram descriptor");
aslc200b112008-08-16 12:57:46 +00002467
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002468 // Add the function bounds.
2469 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2470 DWLabel("func_begin", SubprogramCount));
2471 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2472 DWLabel("func_end", SubprogramCount));
2473 MachineLocation Location(RI->getFrameRegister(*MF));
2474 AddAddress(SPDie, DW_AT_frame_base, Location);
2475
2476 ConstructScope(RootScope, 0, 0, SPDie, Unit);
2477 }
2478
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002479 /// ConstructDefaultScope - Construct a default scope for the subprogram.
2480 ///
2481 void ConstructDefaultScope(MachineFunction *MF) {
2482 // Find the correct subprogram descriptor.
2483 std::vector<SubprogramDesc *> Subprograms;
2484 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
2485
2486 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2487 SubprogramDesc *SPD = Subprograms[i];
2488
2489 if (SPD->getName() == MF->getFunction()->getName()) {
2490 // Get the compile unit context.
2491 CompileUnit *Unit = GetBaseCompileUnit();
2492
2493 // Get the subprogram die.
2494 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2495 assert(SPDie && "Missing subprogram descriptor");
2496
2497 // Add the function bounds.
2498 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2499 DWLabel("func_begin", SubprogramCount));
2500 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2501 DWLabel("func_end", SubprogramCount));
2502
2503 MachineLocation Location(RI->getFrameRegister(*MF));
2504 AddAddress(SPDie, DW_AT_frame_base, Location);
2505 return;
2506 }
2507 }
Bill Wendlingae6b98c2008-10-17 18:48:57 +00002508#if 0
2509 // FIXME: This is causing an abort because C++ mangled names are compared
2510 // with their unmangled counterparts. See PR2885. Don't do this assert.
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002511 assert(0 && "Couldn't find DIE for machine function!");
Bill Wendlingae6b98c2008-10-17 18:48:57 +00002512#endif
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002513 }
2514
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002515 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2516 /// tools to recognize the object file contains Dwarf information.
2517 void EmitInitial() {
2518 // Check to see if we already emitted intial headers.
2519 if (didInitial) return;
2520 didInitial = true;
aslc200b112008-08-16 12:57:46 +00002521
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002522 // Dwarf sections base addresses.
2523 if (TAI->doesDwarfRequireFrameSection()) {
2524 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2525 EmitLabel("section_debug_frame", 0);
2526 }
2527 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2528 EmitLabel("section_info", 0);
2529 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2530 EmitLabel("section_abbrev", 0);
2531 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2532 EmitLabel("section_aranges", 0);
2533 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2534 EmitLabel("section_macinfo", 0);
2535 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2536 EmitLabel("section_line", 0);
2537 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2538 EmitLabel("section_loc", 0);
2539 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2540 EmitLabel("section_pubnames", 0);
2541 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2542 EmitLabel("section_str", 0);
2543 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2544 EmitLabel("section_ranges", 0);
2545
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002546 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002547 EmitLabel("text_begin", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00002548 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002549 EmitLabel("data_begin", 0);
2550 }
2551
2552 /// EmitDIE - Recusively Emits a debug information entry.
2553 ///
2554 void EmitDIE(DIE *Die) {
2555 // Get the abbreviation for this DIE.
2556 unsigned AbbrevNumber = Die->getAbbrevNumber();
2557 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
aslc200b112008-08-16 12:57:46 +00002558
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002559 Asm->EOL();
2560
2561 // Emit the code (index) for the abbreviation.
2562 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng0eeed442008-07-01 23:18:29 +00002563
2564 if (VerboseAsm)
2565 Asm->EOL(std::string("Abbrev [" +
2566 utostr(AbbrevNumber) +
2567 "] 0x" + utohexstr(Die->getOffset()) +
2568 ":0x" + utohexstr(Die->getSize()) + " " +
2569 TagString(Abbrev->getTag())));
2570 else
2571 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00002572
Owen Anderson88dd6232008-06-24 21:44:59 +00002573 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2574 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
aslc200b112008-08-16 12:57:46 +00002575
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002576 // Emit the DIE attribute values.
2577 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2578 unsigned Attr = AbbrevData[i].getAttribute();
2579 unsigned Form = AbbrevData[i].getForm();
2580 assert(Form && "Too many attributes for DIE (check abbreviation)");
aslc200b112008-08-16 12:57:46 +00002581
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002582 switch (Attr) {
2583 case DW_AT_sibling: {
2584 Asm->EmitInt32(Die->SiblingOffset());
2585 break;
2586 }
2587 default: {
2588 // Emit an attribute using the defined form.
2589 Values[i]->EmitValue(*this, Form);
2590 break;
2591 }
2592 }
aslc200b112008-08-16 12:57:46 +00002593
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002594 Asm->EOL(AttributeString(Attr));
2595 }
aslc200b112008-08-16 12:57:46 +00002596
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002597 // Emit the DIE children if any.
2598 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2599 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002600
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002601 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2602 EmitDIE(Children[j]);
2603 }
aslc200b112008-08-16 12:57:46 +00002604
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002605 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2606 }
2607 }
2608
2609 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2610 ///
2611 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2612 // Get the children.
2613 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002614
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002615 // If not last sibling and has children then add sibling offset attribute.
2616 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2617
2618 // Record the abbreviation.
2619 AssignAbbrevNumber(Die->getAbbrev());
aslc200b112008-08-16 12:57:46 +00002620
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002621 // Get the abbreviation for this DIE.
2622 unsigned AbbrevNumber = Die->getAbbrevNumber();
2623 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2624
2625 // Set DIE offset
2626 Die->setOffset(Offset);
aslc200b112008-08-16 12:57:46 +00002627
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002628 // Start the size with the size of abbreviation code.
aslc200b112008-08-16 12:57:46 +00002629 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2630
Owen Anderson88dd6232008-06-24 21:44:59 +00002631 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2632 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002633
2634 // Size the DIE attribute values.
2635 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2636 // Size attribute value.
2637 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2638 }
aslc200b112008-08-16 12:57:46 +00002639
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002640 // Size the DIE children if any.
2641 if (!Children.empty()) {
2642 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2643 "Children flag not set");
aslc200b112008-08-16 12:57:46 +00002644
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002645 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2646 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2647 }
aslc200b112008-08-16 12:57:46 +00002648
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002649 // End of children marker.
2650 Offset += sizeof(int8_t);
2651 }
2652
2653 Die->setSize(Offset - Die->getOffset());
2654 return Offset;
2655 }
2656
2657 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2658 ///
2659 void SizeAndOffsets() {
2660 // Process base compile unit.
2661 CompileUnit *Unit = GetBaseCompileUnit();
2662 // Compute size of compile unit header
2663 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2664 sizeof(int16_t) + // DWARF version number
2665 sizeof(int32_t) + // Offset Into Abbrev. Section
2666 sizeof(int8_t); // Pointer Size (in bytes)
2667 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2668 }
2669
2670 /// EmitDebugInfo - Emit the debug info section.
2671 ///
2672 void EmitDebugInfo() {
2673 // Start debug info section.
2674 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
aslc200b112008-08-16 12:57:46 +00002675
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002676 CompileUnit *Unit = GetBaseCompileUnit();
2677 DIE *Die = Unit->getDie();
2678 // Emit the compile units header.
2679 EmitLabel("info_begin", Unit->getID());
2680 // Emit size of content not including length itself
2681 unsigned ContentSize = Die->getSize() +
2682 sizeof(int16_t) + // DWARF version number
2683 sizeof(int32_t) + // Offset Into Abbrev. Section
2684 sizeof(int8_t) + // Pointer Size (in bytes)
2685 sizeof(int32_t); // FIXME - extra pad for gdb bug.
aslc200b112008-08-16 12:57:46 +00002686
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002687 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2688 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2689 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2690 Asm->EOL("Offset Into Abbrev. Section");
Dan Gohmancfb72b22007-09-27 23:12:31 +00002691 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
aslc200b112008-08-16 12:57:46 +00002692
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002693 EmitDIE(Die);
2694 // FIXME - extra padding for gdb bug.
2695 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2696 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2697 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2698 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2699 EmitLabel("info_end", Unit->getID());
aslc200b112008-08-16 12:57:46 +00002700
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002701 Asm->EOL();
2702 }
2703
2704 /// EmitAbbreviations - Emit the abbreviation section.
2705 ///
2706 void EmitAbbreviations() const {
2707 // Check to see if it is worth the effort.
2708 if (!Abbreviations.empty()) {
2709 // Start the debug abbrev section.
2710 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
aslc200b112008-08-16 12:57:46 +00002711
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002712 EmitLabel("abbrev_begin", 0);
aslc200b112008-08-16 12:57:46 +00002713
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002714 // For each abbrevation.
2715 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2716 // Get abbreviation data
2717 const DIEAbbrev *Abbrev = Abbreviations[i];
aslc200b112008-08-16 12:57:46 +00002718
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002719 // Emit the abbrevations code (base 1 index.)
2720 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2721 Asm->EOL("Abbreviation Code");
aslc200b112008-08-16 12:57:46 +00002722
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002723 // Emit the abbreviations data.
2724 Abbrev->Emit(*this);
aslc200b112008-08-16 12:57:46 +00002725
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002726 Asm->EOL();
2727 }
aslc200b112008-08-16 12:57:46 +00002728
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002729 // Mark end of abbreviations.
2730 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2731
2732 EmitLabel("abbrev_end", 0);
aslc200b112008-08-16 12:57:46 +00002733
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002734 Asm->EOL();
2735 }
2736 }
2737
Bill Wendling1983a2a2008-07-20 00:11:19 +00002738 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2739 /// the line matrix.
aslc200b112008-08-16 12:57:46 +00002740 ///
Bill Wendling1983a2a2008-07-20 00:11:19 +00002741 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2742 // Define last address of section.
2743 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2744 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2745 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2746 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2747
2748 // Mark end of matrix.
2749 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2750 Asm->EmitULEB128Bytes(1); Asm->EOL();
2751 Asm->EmitInt8(1); Asm->EOL();
2752 }
2753
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002754 /// EmitDebugLines - Emit source line information.
2755 ///
2756 void EmitDebugLines() {
Bill Wendling1983a2a2008-07-20 00:11:19 +00002757 // If the target is using .loc/.file, the assembler will be emitting the
2758 // .debug_line table automatically.
2759 if (TAI->hasDotLocAndDotFile())
Dan Gohmanc55b34a2007-09-24 21:43:52 +00002760 return;
2761
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002762 // Minimum line delta, thus ranging from -10..(255-10).
2763 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2764 // Maximum line delta, thus ranging from -10..(255-10).
2765 const int MaxLineDelta = 255 + MinLineDelta;
2766
2767 // Start the dwarf line section.
2768 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
aslc200b112008-08-16 12:57:46 +00002769
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002770 // Construct the section header.
aslc200b112008-08-16 12:57:46 +00002771
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002772 EmitDifference("line_end", 0, "line_begin", 0, true);
2773 Asm->EOL("Length of Source Line Info");
2774 EmitLabel("line_begin", 0);
aslc200b112008-08-16 12:57:46 +00002775
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002776 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
aslc200b112008-08-16 12:57:46 +00002777
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002778 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2779 Asm->EOL("Prolog Length");
2780 EmitLabel("line_prolog_begin", 0);
aslc200b112008-08-16 12:57:46 +00002781
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002782 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2783
2784 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2785
2786 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
aslc200b112008-08-16 12:57:46 +00002787
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002788 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2789
2790 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
aslc200b112008-08-16 12:57:46 +00002791
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002792 // Line number standard opcode encodings argument count
2793 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2794 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2795 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2796 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2797 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2798 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2799 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2800 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2801 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2802
2803 const UniqueVector<std::string> &Directories = MMI->getDirectories();
Evan Cheng0eeed442008-07-01 23:18:29 +00002804 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002805
2806 // Emit directories.
2807 for (unsigned DirectoryID = 1, NDID = Directories.size();
2808 DirectoryID <= NDID; ++DirectoryID) {
2809 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
2810 }
2811 Asm->EmitInt8(0); Asm->EOL("End of directories");
aslc200b112008-08-16 12:57:46 +00002812
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002813 // Emit files.
2814 for (unsigned SourceID = 1, NSID = SourceFiles.size();
2815 SourceID <= NSID; ++SourceID) {
2816 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2817 Asm->EmitString(SourceFile.getName());
2818 Asm->EOL("Source");
2819 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
2820 Asm->EOL("Directory #");
2821 Asm->EmitULEB128Bytes(0);
2822 Asm->EOL("Mod date");
2823 Asm->EmitULEB128Bytes(0);
2824 Asm->EOL("File size");
2825 }
2826 Asm->EmitInt8(0); Asm->EOL("End of files");
aslc200b112008-08-16 12:57:46 +00002827
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002828 EmitLabel("line_prolog_end", 0);
aslc200b112008-08-16 12:57:46 +00002829
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002830 // A sequence for each text section.
Bill Wendling1983a2a2008-07-20 00:11:19 +00002831 unsigned SecSrcLinesSize = SectionSourceLines.size();
2832
2833 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002834 // Isolate current sections line info.
2835 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng0eeed442008-07-01 23:18:29 +00002836
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002837 if (VerboseAsm) {
2838 const Section* S = SectionMap[j + 1];
2839 Asm->EOL(std::string("Section ") + S->getName());
2840 } else
Evan Cheng0eeed442008-07-01 23:18:29 +00002841 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002842
2843 // Dwarf assumes we start with first line of first source file.
2844 unsigned Source = 1;
2845 unsigned Line = 1;
aslc200b112008-08-16 12:57:46 +00002846
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002847 // Construct rows of the address, source, line, column matrix.
2848 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2849 const SourceLineInfo &LineInfo = LineInfos[i];
2850 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2851 if (!LabelID) continue;
aslc200b112008-08-16 12:57:46 +00002852
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002853 unsigned SourceID = LineInfo.getSourceID();
2854 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2855 unsigned DirectoryID = SourceFile.getDirectoryID();
Evan Cheng0eeed442008-07-01 23:18:29 +00002856 if (VerboseAsm)
2857 Asm->EOL(Directories[DirectoryID]
2858 + SourceFile.getName()
2859 + ":"
2860 + utostr_32(LineInfo.getLine()));
2861 else
2862 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002863
2864 // Define the line address.
2865 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohmancfb72b22007-09-27 23:12:31 +00002866 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002867 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2868 EmitReference("label", LabelID); Asm->EOL("Location label");
aslc200b112008-08-16 12:57:46 +00002869
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002870 // If change of source, then switch to the new source.
2871 if (Source != LineInfo.getSourceID()) {
2872 Source = LineInfo.getSourceID();
2873 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2874 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2875 }
aslc200b112008-08-16 12:57:46 +00002876
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002877 // If change of line.
2878 if (Line != LineInfo.getLine()) {
2879 // Determine offset.
2880 int Offset = LineInfo.getLine() - Line;
2881 int Delta = Offset - MinLineDelta;
aslc200b112008-08-16 12:57:46 +00002882
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002883 // Update line.
2884 Line = LineInfo.getLine();
aslc200b112008-08-16 12:57:46 +00002885
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002886 // If delta is small enough and in range...
2887 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2888 // ... then use fast opcode.
2889 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2890 } else {
2891 // ... otherwise use long hand.
2892 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2893 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2894 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2895 }
2896 } else {
2897 // Copy the previous row (different address or source)
2898 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2899 }
2900 }
2901
Bill Wendling1983a2a2008-07-20 00:11:19 +00002902 EmitEndOfLineMatrix(j + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002903 }
Bill Wendling1983a2a2008-07-20 00:11:19 +00002904
2905 if (SecSrcLinesSize == 0)
2906 // Because we're emitting a debug_line section, we still need a line
2907 // table. The linker and friends expect it to exist. If there's nothing to
2908 // put into it, emit an empty table.
2909 EmitEndOfLineMatrix(1);
aslc200b112008-08-16 12:57:46 +00002910
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002911 EmitLabel("line_end", 0);
aslc200b112008-08-16 12:57:46 +00002912
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002913 Asm->EOL();
2914 }
aslc200b112008-08-16 12:57:46 +00002915
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002916 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2917 ///
2918 void EmitCommonDebugFrame() {
2919 if (!TAI->doesDwarfRequireFrameSection())
2920 return;
2921
2922 int stackGrowth =
2923 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2924 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00002925 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002926
2927 // Start the dwarf frame section.
2928 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2929
2930 EmitLabel("debug_frame_common", 0);
2931 EmitDifference("debug_frame_common_end", 0,
2932 "debug_frame_common_begin", 0, true);
2933 Asm->EOL("Length of Common Information Entry");
2934
2935 EmitLabel("debug_frame_common_begin", 0);
2936 Asm->EmitInt32((int)DW_CIE_ID);
2937 Asm->EOL("CIE Identifier Tag");
2938 Asm->EmitInt8(DW_CIE_VERSION);
2939 Asm->EOL("CIE Version");
2940 Asm->EmitString("");
2941 Asm->EOL("CIE Augmentation");
2942 Asm->EmitULEB128Bytes(1);
2943 Asm->EOL("CIE Code Alignment Factor");
2944 Asm->EmitSLEB128Bytes(stackGrowth);
aslc200b112008-08-16 12:57:46 +00002945 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00002946 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002947 Asm->EOL("CIE RA Column");
aslc200b112008-08-16 12:57:46 +00002948
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002949 std::vector<MachineMove> Moves;
2950 RI->getInitialFrameState(Moves);
2951
Dale Johannesenf5a11532007-11-13 19:13:01 +00002952 EmitFrameMoves(NULL, 0, Moves, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002953
Evan Cheng7e7d1942008-02-29 19:36:59 +00002954 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002955 EmitLabel("debug_frame_common_end", 0);
aslc200b112008-08-16 12:57:46 +00002956
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002957 Asm->EOL();
2958 }
2959
2960 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2961 /// section.
2962 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
2963 if (!TAI->doesDwarfRequireFrameSection())
2964 return;
aslc200b112008-08-16 12:57:46 +00002965
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002966 // Start the dwarf frame section.
2967 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
aslc200b112008-08-16 12:57:46 +00002968
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002969 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2970 "debug_frame_begin", DebugFrameInfo.Number, true);
2971 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00002972
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002973 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2974
2975 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2976 0, 0, true, false);
2977 Asm->EOL("FDE CIE offset");
2978
2979 EmitReference("func_begin", DebugFrameInfo.Number);
2980 Asm->EOL("FDE initial location");
2981 EmitDifference("func_end", DebugFrameInfo.Number,
2982 "func_begin", DebugFrameInfo.Number);
2983 Asm->EOL("FDE address range");
aslc200b112008-08-16 12:57:46 +00002984
Dale Johannesenf5a11532007-11-13 19:13:01 +00002985 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves, false);
aslc200b112008-08-16 12:57:46 +00002986
Evan Cheng7e7d1942008-02-29 19:36:59 +00002987 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002988 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2989
2990 Asm->EOL();
2991 }
2992
2993 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2994 ///
2995 void EmitDebugPubNames() {
2996 // Start the dwarf pubnames section.
2997 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
aslc200b112008-08-16 12:57:46 +00002998
2999 CompileUnit *Unit = GetBaseCompileUnit();
3000
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003001 EmitDifference("pubnames_end", Unit->getID(),
3002 "pubnames_begin", Unit->getID(), true);
3003 Asm->EOL("Length of Public Names Info");
aslc200b112008-08-16 12:57:46 +00003004
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003005 EmitLabel("pubnames_begin", Unit->getID());
aslc200b112008-08-16 12:57:46 +00003006
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003007 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
3008
3009 EmitSectionOffset("info_begin", "section_info",
3010 Unit->getID(), 0, true, false);
3011 Asm->EOL("Offset of Compilation Unit Info");
3012
3013 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
3014 Asm->EOL("Compilation Unit Length");
aslc200b112008-08-16 12:57:46 +00003015
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003016 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
aslc200b112008-08-16 12:57:46 +00003017
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003018 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
3019 GE = Globals.end();
3020 GI != GE; ++GI) {
3021 const std::string &Name = GI->first;
3022 DIE * Entity = GI->second;
aslc200b112008-08-16 12:57:46 +00003023
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003024 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
3025 Asm->EmitString(Name); Asm->EOL("External Name");
3026 }
aslc200b112008-08-16 12:57:46 +00003027
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003028 Asm->EmitInt32(0); Asm->EOL("End Mark");
3029 EmitLabel("pubnames_end", Unit->getID());
aslc200b112008-08-16 12:57:46 +00003030
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003031 Asm->EOL();
3032 }
3033
3034 /// EmitDebugStr - Emit visible names into a debug str section.
3035 ///
3036 void EmitDebugStr() {
3037 // Check to see if it is worth the effort.
3038 if (!StringPool.empty()) {
3039 // Start the dwarf str section.
3040 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
aslc200b112008-08-16 12:57:46 +00003041
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003042 // For each of strings in the string pool.
3043 for (unsigned StringID = 1, N = StringPool.size();
3044 StringID <= N; ++StringID) {
3045 // Emit a label for reference from debug information entries.
3046 EmitLabel("string", StringID);
3047 // Emit the string itself.
3048 const std::string &String = StringPool[StringID];
3049 Asm->EmitString(String); Asm->EOL();
3050 }
aslc200b112008-08-16 12:57:46 +00003051
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003052 Asm->EOL();
3053 }
3054 }
3055
3056 /// EmitDebugLoc - Emit visible names into a debug loc section.
3057 ///
3058 void EmitDebugLoc() {
3059 // Start the dwarf loc section.
3060 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
aslc200b112008-08-16 12:57:46 +00003061
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003062 Asm->EOL();
3063 }
3064
3065 /// EmitDebugARanges - Emit visible names into a debug aranges section.
3066 ///
3067 void EmitDebugARanges() {
3068 // Start the dwarf aranges section.
3069 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
aslc200b112008-08-16 12:57:46 +00003070
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003071 // FIXME - Mock up
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003072#if 0
aslc200b112008-08-16 12:57:46 +00003073 CompileUnit *Unit = GetBaseCompileUnit();
3074
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003075 // Don't include size of length
3076 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
aslc200b112008-08-16 12:57:46 +00003077
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003078 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
aslc200b112008-08-16 12:57:46 +00003079
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003080 EmitReference("info_begin", Unit->getID());
3081 Asm->EOL("Offset of Compilation Unit Info");
3082
Dan Gohmancfb72b22007-09-27 23:12:31 +00003083 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003084
3085 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
3086
3087 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
3088 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
3089
3090 // Range 1
3091 EmitReference("text_begin", 0); Asm->EOL("Address");
3092 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
3093
3094 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
3095 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003096#endif
aslc200b112008-08-16 12:57:46 +00003097
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003098 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003099 }
3100
3101 /// EmitDebugRanges - Emit visible names into a debug ranges section.
3102 ///
3103 void EmitDebugRanges() {
3104 // Start the dwarf ranges section.
3105 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
aslc200b112008-08-16 12:57:46 +00003106
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003107 Asm->EOL();
3108 }
3109
3110 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
3111 ///
3112 void EmitDebugMacInfo() {
3113 // Start the dwarf macinfo section.
3114 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
aslc200b112008-08-16 12:57:46 +00003115
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003116 Asm->EOL();
3117 }
3118
Devang Patel289f2362009-01-05 23:11:11 +00003119 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Patelb3907da2009-01-05 23:03:32 +00003120 void ConstructCompileUnits() {
3121 std::string CUName = "llvm.dbg.compile_units";
3122 std::vector<GlobalVariable*> Result;
3123 getGlobalVariablesUsing(*M, CUName, Result);
3124 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
3125 RE = Result.end(); RI != RE; ++RI) {
3126 DICompileUnit *DIUnit = new DICompileUnit(*RI);
3127 unsigned DID = Directories.insert(DIUnit->getDirectory());
3128 unsigned ID = SrcFiles.insert(SrcFileInfo(DID,
3129 DIUnit->getFilename()));
3130
3131 DIE *Die = new DIE(DW_TAG_compile_unit);
3132 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
3133 DWLabel("section_line", 0), DWLabel("section_line", 0),
3134 false);
3135 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit->getProducer());
3136 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit->getLanguage());
3137 AddString(Die, DW_AT_name, DW_FORM_string, DIUnit->getFilename());
3138 if (!DIUnit->getDirectory().empty())
3139 AddString(Die, DW_AT_comp_dir, DW_FORM_string, DIUnit->getDirectory());
3140
3141 CompileUnit *Unit = new CompileUnit(ID, Die);
3142 DW_CUs[DIUnit->getGV()] = Unit;
3143 }
3144 }
3145
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003146 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
3147 /// header file.
3148 void ConstructCompileUnitDIEs() {
3149 const UniqueVector<CompileUnitDesc *> CUW = MMI->getCompileUnits();
aslc200b112008-08-16 12:57:46 +00003150
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003151 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
3152 unsigned ID = MMI->RecordSource(CUW[i]);
3153 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
3154 CompileUnits.push_back(Unit);
3155 }
3156 }
3157
Devang Patel289f2362009-01-05 23:11:11 +00003158 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
3159 /// visible global variables.
3160 void ConstructGlobalVariableDIEs() {
3161 std::string GVName = "llvm.dbg.global_variables";
3162 std::vector<GlobalVariable*> Result;
3163 getGlobalVariablesUsing(*M, GVName, Result);
3164 for (std::vector<GlobalVariable *>::iterator GVI = Result.begin(),
3165 GVE = Result.end(); GVI != GVE; ++GVI) {
3166 DIGlobalVariable *DI_GV = new DIGlobalVariable(*GVI);
3167 CompileUnit *DW_Unit = FindCompileUnit(DI_GV->getCompileUnit());
3168
3169 // Check for pre-existence.
3170 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV->getGV());
3171 if (Slot) continue;
3172
3173 DIE *VariableDie = new DIE(DW_TAG_variable);
3174 AddString(VariableDie, DW_AT_name, DW_FORM_string, DI_GV->getName());
3175 const std::string &LinkageName = DI_GV->getLinkageName();
3176 if (!LinkageName.empty())
3177 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
3178 LinkageName);
3179 AddType(DW_Unit, VariableDie, DI_GV->getType());
3180
3181 if (!DI_GV->isLocalToUnit())
3182 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
3183
3184 // Add source line info, if available.
3185 AddSourceLine(VariableDie, DI_GV);
3186
3187 // Add address.
3188 DIEBlock *Block = new DIEBlock();
3189 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
3190 AddObjectLabel(Block, 0, DW_FORM_udata,
3191 Asm->getGlobalLinkName(DI_GV->getGV()));
3192 AddBlock(VariableDie, DW_AT_location, 0, Block);
3193
3194 //Add to map.
3195 Slot = VariableDie;
3196
3197 //Add to context owner.
3198 DW_Unit->getDie()->AddChild(VariableDie);
3199
3200 //Expose as global. FIXME - need to check external flag.
3201 DW_Unit->AddGlobal(DI_GV->getName(), VariableDie);
3202 }
3203 }
3204
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003205 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
3206 /// global variables.
3207 void ConstructGlobalDIEs() {
Bill Wendling75091f92008-07-03 23:13:02 +00003208 std::vector<GlobalVariableDesc *> GlobalVariables;
3209 MMI->getAnchoredDescriptors<GlobalVariableDesc>(*M, GlobalVariables);
aslc200b112008-08-16 12:57:46 +00003210
Bill Wendling4de8de52008-07-03 22:53:42 +00003211 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
3212 GlobalVariableDesc *GVD = GlobalVariables[i];
3213 NewGlobalVariable(GVD);
3214 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003215 }
3216
Devang Patele6caf012009-01-05 23:21:35 +00003217 /// ConstructSubprograms - Create DIEs for each of the externally visible
3218 /// subprograms.
3219 void ConstructSubprograms() {
3220
3221 std::string SPName = "llvm.dbg.subprograms";
3222 std::vector<GlobalVariable*> Result;
3223 getGlobalVariablesUsing(*M, SPName, Result);
3224 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
3225 RE = Result.end(); RI != RE; ++RI) {
3226
3227 DISubprogram *SP = new DISubprogram(*RI);
3228 CompileUnit *Unit = FindCompileUnit(SP->getCompileUnit());
3229
3230 // Check for pre-existence.
3231 DIE *&Slot = Unit->getDieMapSlotFor(SP->getGV());
3232 if (Slot) continue;
3233
3234 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
3235 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, SP->getName());
3236 const std::string &LinkageName = SP->getLinkageName();
3237 if (!LinkageName.empty())
3238 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
3239 LinkageName);
3240 DIType SPTy = SP->getType();
3241 AddType(Unit, SubprogramDie, SPTy);
3242 if (!SP->isLocalToUnit())
3243 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
3244 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
3245
3246 AddSourceLine(SubprogramDie, SP);
3247 //Add to map.
3248 Slot = SubprogramDie;
3249 //Add to context owner.
3250 Unit->getDie()->AddChild(SubprogramDie);
3251 //Expose as global.
3252 Unit->AddGlobal(SP->getName(), SubprogramDie);
3253 }
3254 }
3255
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003256 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
3257 /// subprograms.
3258 void ConstructSubprogramDIEs() {
Bill Wendling75091f92008-07-03 23:13:02 +00003259 std::vector<SubprogramDesc *> Subprograms;
3260 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
aslc200b112008-08-16 12:57:46 +00003261
Bill Wendling4de8de52008-07-03 22:53:42 +00003262 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
3263 SubprogramDesc *SPD = Subprograms[i];
3264 NewSubprogram(SPD);
3265 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003266 }
3267
3268public:
3269 //===--------------------------------------------------------------------===//
3270 // Main entry points.
3271 //
Owen Anderson847b99b2008-08-21 00:14:44 +00003272 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattnerb3876c72007-09-24 03:35:37 +00003273 : Dwarf(OS, A, T, "dbg")
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003274 , CompileUnits()
3275 , AbbreviationsSet(InitAbbreviationsSetSize)
3276 , Abbreviations()
3277 , ValuesSet(InitValuesSetSize)
3278 , Values()
3279 , StringPool()
3280 , DescToUnitMap()
3281 , SectionMap()
3282 , SectionSourceLines()
3283 , didInitial(false)
3284 , shouldEmit(false)
3285 {
3286 }
3287 virtual ~DwarfDebug() {
3288 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
3289 delete CompileUnits[i];
3290 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3291 delete Values[j];
3292 }
3293
Devang Patel9304b382009-01-06 21:07:30 +00003294 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3295 /// This is inovked by the target AsmPrinter.
3296 void SetDebugInfo() {
3297 // FIXME - Check if the module has debug info or not.
3298 // Create all the compile unit DIEs.
3299 ConstructCompileUnits();
3300
3301 // Create DIEs for each of the externally visible global variables.
3302 ConstructGlobalVariableDIEs();
3303
3304 // Create DIEs for each of the externally visible subprograms.
3305 ConstructSubprograms();
3306
3307 // Prime section data.
3308 SectionMap.insert(TAI->getTextSection());
3309
3310 // Print out .file directives to specify files for .loc directives. These
3311 // are printed out early so that they precede any .loc directives.
3312 if (TAI->hasDotLocAndDotFile()) {
3313 for (unsigned i = 1, e = SrcFiles.size(); i <= e; ++i) {
3314 sys::Path FullPath(Directories[SrcFiles[i].getDirectoryID()]);
3315 bool AppendOk = FullPath.appendComponent(SrcFiles[i].getName());
3316 assert(AppendOk && "Could not append filename to directory!");
3317 AppendOk = false;
3318 Asm->EmitFile(i, FullPath.toString());
3319 Asm->EOL();
3320 }
3321 }
3322
3323 // Emit initial sections
3324 EmitInitial();
3325 }
3326
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003327 /// SetModuleInfo - Set machine module information when it's known that pass
3328 /// manager has created it. Set by the target AsmPrinter.
3329 void SetModuleInfo(MachineModuleInfo *mmi) {
3330 // Make sure initial declarations are made.
3331 if (!MMI && mmi->hasDebugInfo()) {
3332 MMI = mmi;
3333 shouldEmit = true;
aslc200b112008-08-16 12:57:46 +00003334
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003335 // Create all the compile unit DIEs.
3336 ConstructCompileUnitDIEs();
aslc200b112008-08-16 12:57:46 +00003337
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003338 // Create DIEs for each of the externally visible global variables.
3339 ConstructGlobalDIEs();
3340
3341 // Create DIEs for each of the externally visible subprograms.
3342 ConstructSubprogramDIEs();
aslc200b112008-08-16 12:57:46 +00003343
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003344 // Prime section data.
3345 SectionMap.insert(TAI->getTextSection());
Dan Gohman6d6c2402007-10-01 22:40:20 +00003346
3347 // Print out .file directives to specify files for .loc directives. These
3348 // are printed out early so that they precede any .loc directives.
3349 if (TAI->hasDotLocAndDotFile()) {
3350 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
3351 const UniqueVector<std::string> &Directories = MMI->getDirectories();
3352 for (unsigned i = 1, e = SourceFiles.size(); i <= e; ++i) {
3353 sys::Path FullPath(Directories[SourceFiles[i].getDirectoryID()]);
3354 bool AppendOk = FullPath.appendComponent(SourceFiles[i].getName());
3355 assert(AppendOk && "Could not append filename to directory!");
Devang Patel105a08a2008-12-23 21:55:38 +00003356 AppendOk = false;
Dan Gohman6d6c2402007-10-01 22:40:20 +00003357 Asm->EmitFile(i, FullPath.toString());
3358 Asm->EOL();
3359 }
3360 }
3361
3362 // Emit initial sections
3363 EmitInitial();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003364 }
3365 }
3366
3367 /// BeginModule - Emit all Dwarf sections that should come prior to the
3368 /// content.
3369 void BeginModule(Module *M) {
3370 this->M = M;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003371 }
3372
3373 /// EndModule - Emit all Dwarf sections that should come after the content.
3374 ///
3375 void EndModule() {
3376 if (!ShouldEmitDwarf()) return;
aslc200b112008-08-16 12:57:46 +00003377
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003378 // Standard sections final addresses.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003379 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003380 EmitLabel("text_end", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00003381 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003382 EmitLabel("data_end", 0);
aslc200b112008-08-16 12:57:46 +00003383
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003384 // End text sections.
3385 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003386 Asm->SwitchToSection(SectionMap[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003387 EmitLabel("section_end", i);
3388 }
3389
3390 // Emit common frame information.
3391 EmitCommonDebugFrame();
3392
3393 // Emit function debug frame information
3394 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3395 E = DebugFrames.end(); I != E; ++I)
3396 EmitFunctionDebugFrame(*I);
3397
3398 // Compute DIE offsets and sizes.
3399 SizeAndOffsets();
aslc200b112008-08-16 12:57:46 +00003400
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003401 // Emit all the DIEs into a debug info section
3402 EmitDebugInfo();
aslc200b112008-08-16 12:57:46 +00003403
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003404 // Corresponding abbreviations into a abbrev section.
3405 EmitAbbreviations();
aslc200b112008-08-16 12:57:46 +00003406
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003407 // Emit source line correspondence into a debug line section.
3408 EmitDebugLines();
aslc200b112008-08-16 12:57:46 +00003409
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003410 // Emit info into a debug pubnames section.
3411 EmitDebugPubNames();
aslc200b112008-08-16 12:57:46 +00003412
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003413 // Emit info into a debug str section.
3414 EmitDebugStr();
aslc200b112008-08-16 12:57:46 +00003415
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003416 // Emit info into a debug loc section.
3417 EmitDebugLoc();
aslc200b112008-08-16 12:57:46 +00003418
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003419 // Emit info into a debug aranges section.
3420 EmitDebugARanges();
aslc200b112008-08-16 12:57:46 +00003421
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003422 // Emit info into a debug ranges section.
3423 EmitDebugRanges();
aslc200b112008-08-16 12:57:46 +00003424
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003425 // Emit info into a debug macinfo section.
3426 EmitDebugMacInfo();
3427 }
3428
aslc200b112008-08-16 12:57:46 +00003429 /// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003430 /// emitted immediately after the function entry point.
3431 void BeginFunction(MachineFunction *MF) {
3432 this->MF = MF;
aslc200b112008-08-16 12:57:46 +00003433
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003434 if (!ShouldEmitDwarf()) return;
3435
3436 // Begin accumulating function debug information.
3437 MMI->BeginFunction(MF);
aslc200b112008-08-16 12:57:46 +00003438
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003439 // Assumes in correct section after the entry point.
3440 EmitLabel("func_begin", ++SubprogramCount);
Evan Chenga53c40a2008-02-01 09:10:45 +00003441
3442 // Emit label for the implicitly defined dbg.stoppoint at the start of
3443 // the function.
Andrew Lenharth42f91402008-04-03 17:37:43 +00003444 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
3445 if (!LineInfos.empty()) {
3446 const SourceLineInfo &LineInfo = LineInfos[0];
3447 Asm->printLabel(LineInfo.getLabelID());
3448 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003449 }
aslc200b112008-08-16 12:57:46 +00003450
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003451 /// EndFunction - Gather and emit post-function debug information.
3452 ///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003453 void EndFunction(MachineFunction *MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003454 if (!ShouldEmitDwarf()) return;
aslc200b112008-08-16 12:57:46 +00003455
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003456 // Define end label for subprogram.
3457 EmitLabel("func_end", SubprogramCount);
aslc200b112008-08-16 12:57:46 +00003458
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003459 // Get function line info.
3460 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
3461
3462 if (!LineInfos.empty()) {
3463 // Get section line info.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003464 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003465 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
3466 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
3467 // Append the function info to section info.
3468 SectionLineInfos.insert(SectionLineInfos.end(),
3469 LineInfos.begin(), LineInfos.end());
3470 }
aslc200b112008-08-16 12:57:46 +00003471
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003472 // Construct scopes for subprogram.
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003473 if (MMI->getRootScope())
3474 ConstructRootScope(MMI->getRootScope());
3475 else
3476 // FIXME: This is wrong. We are essentially getting past a problem with
3477 // debug information not being able to handle unreachable blocks that have
3478 // debug information in them. In particular, those unreachable blocks that
3479 // have "region end" info in them. That situation results in the "root
3480 // scope" not being created. If that's the case, then emit a "default"
3481 // scope, i.e., one that encompasses the whole function. This isn't
3482 // desirable. And a better way of handling this (and all of the debugging
3483 // information) needs to be explored.
3484 ConstructDefaultScope(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003485
3486 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3487 MMI->getFrameMoves()));
3488 }
3489};
3490
3491//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00003492/// DwarfException - Emits Dwarf exception handling directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003493///
3494class DwarfException : public Dwarf {
3495
3496private:
3497 struct FunctionEHFrameInfo {
3498 std::string FnName;
3499 unsigned Number;
3500 unsigned PersonalityIndex;
3501 bool hasCalls;
3502 bool hasLandingPads;
3503 std::vector<MachineMove> Moves;
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003504 const Function * function;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003505
3506 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3507 bool hC, bool hL,
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003508 const std::vector<MachineMove> &M,
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003509 const Function *f):
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003510 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003511 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003512 };
3513
3514 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen85535762008-04-02 00:25:04 +00003515
3516 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3517 /// be emitted.
3518 bool shouldEmitTable;
3519
3520 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3521 /// should be emitted.
3522 bool shouldEmitMoves;
3523
3524 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3525 /// should be emitted.
3526 bool shouldEmitTableModule;
3527
aslc200b112008-08-16 12:57:46 +00003528 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen85535762008-04-02 00:25:04 +00003529 /// should be emitted.
3530 bool shouldEmitMovesModule;
Duncan Sands96144f92008-05-07 19:11:09 +00003531
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003532 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3533 ///
3534 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
3535 // Size and sign of stack growth.
3536 int stackGrowth =
3537 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3538 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00003539 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003540
3541 // Begin eh frame section.
3542 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling189bde72008-12-24 08:05:17 +00003543
3544 if (!TAI->doesRequireNonLocalEHFrameLabel())
3545 O << TAI->getEHGlobalPrefix();
3546 O << "EH_frame" << Index << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003547 EmitLabel("section_eh_frame", Index);
3548
3549 // Define base labels.
3550 EmitLabel("eh_frame_common", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003551
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003552 // Define the eh frame length.
3553 EmitDifference("eh_frame_common_end", Index,
3554 "eh_frame_common_begin", Index, true);
3555 Asm->EOL("Length of Common Information Entry");
3556
3557 // EH frame header.
3558 EmitLabel("eh_frame_common_begin", Index);
3559 Asm->EmitInt32((int)0);
3560 Asm->EOL("CIE Identifier Tag");
3561 Asm->EmitInt8(DW_CIE_VERSION);
3562 Asm->EOL("CIE Version");
Duncan Sands96144f92008-05-07 19:11:09 +00003563
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003564 // The personality presence indicates that language specific information
3565 // will show up in the eh frame.
3566 Asm->EmitString(Personality ? "zPLR" : "zR");
3567 Asm->EOL("CIE Augmentation");
Duncan Sands96144f92008-05-07 19:11:09 +00003568
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003569 // Round out reader.
3570 Asm->EmitULEB128Bytes(1);
3571 Asm->EOL("CIE Code Alignment Factor");
3572 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands96144f92008-05-07 19:11:09 +00003573 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00003574 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands96144f92008-05-07 19:11:09 +00003575 Asm->EOL("CIE Return Address Column");
3576
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003577 // If there is a personality, we need to indicate the functions location.
3578 if (Personality) {
3579 Asm->EmitULEB128Bytes(7);
3580 Asm->EOL("Augmentation Size");
Bill Wendling2d369922007-09-11 17:20:55 +00003581
Duncan Sands96144f92008-05-07 19:11:09 +00003582 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendling2d369922007-09-11 17:20:55 +00003583 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands96144f92008-05-07 19:11:09 +00003584 Asm->EOL("Personality (pcrel sdata4 indirect)");
3585 } else {
Bill Wendling2d369922007-09-11 17:20:55 +00003586 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands96144f92008-05-07 19:11:09 +00003587 Asm->EOL("Personality (pcrel sdata4)");
3588 }
Bill Wendling2d369922007-09-11 17:20:55 +00003589
Duncan Sands96144f92008-05-07 19:11:09 +00003590 PrintRelDirective(true);
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003591 O << TAI->getPersonalityPrefix();
3592 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3593 O << TAI->getPersonalitySuffix();
Duncan Sands4cc39532008-05-08 12:33:11 +00003594 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3595 O << "-" << TAI->getPCSymbol();
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003596 Asm->EOL("Personality");
Bill Wendling38cb7c92007-08-25 00:51:55 +00003597
Duncan Sands96144f92008-05-07 19:11:09 +00003598 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3599 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003600
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003601 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3602 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003603 } else {
3604 Asm->EmitULEB128Bytes(1);
3605 Asm->EOL("Augmentation Size");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003606
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003607 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3608 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003609 }
3610
3611 // Indicate locations of general callee saved registers in frame.
3612 std::vector<MachineMove> Moves;
3613 RI->getInitialFrameState(Moves);
Dale Johannesenf5a11532007-11-13 19:13:01 +00003614 EmitFrameMoves(NULL, 0, Moves, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003615
Dale Johannesen388f20f2008-04-30 00:43:29 +00003616 // On Darwin the linker honors the alignment of eh_frame, which means it
3617 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3618 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003619 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003620 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003621 EmitLabel("eh_frame_common_end", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003622
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003623 Asm->EOL();
3624 }
Duncan Sands96144f92008-05-07 19:11:09 +00003625
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003626 /// EmitEHFrame - Emit function exception frame information.
3627 ///
3628 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003629 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3630
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003631 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3632
3633 // Externally visible entry into the functions eh frame info.
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003634 // If the corresponding function is static, this should not be
3635 // externally visible.
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003636 if (linkage != Function::InternalLinkage) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003637 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3638 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3639 }
3640
Dale Johannesenf09b5992008-01-10 02:03:30 +00003641 // If corresponding function is weak definition, this should be too.
aslc200b112008-08-16 12:57:46 +00003642 if ((linkage == Function::WeakLinkage ||
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003643 linkage == Function::LinkOnceLinkage) &&
Dale Johannesenf09b5992008-01-10 02:03:30 +00003644 TAI->getWeakDefDirective())
3645 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3646
3647 // If there are no calls then you can't unwind. This may mean we can
3648 // omit the EH Frame, but some environments do not handle weak absolute
aslc200b112008-08-16 12:57:46 +00003649 // symbols.
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003650 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesena9b3e482008-04-08 00:10:24 +00003651 // unwind info is to be available for non-EH uses.
Dale Johannesenf09b5992008-01-10 02:03:30 +00003652 if (!EHFrameInfo.hasCalls &&
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003653 !UnwindTablesMandatory &&
aslc200b112008-08-16 12:57:46 +00003654 ((linkage != Function::WeakLinkage &&
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003655 linkage != Function::LinkOnceLinkage) ||
Dale Johannesenf09b5992008-01-10 02:03:30 +00003656 !TAI->getWeakDefDirective() ||
3657 TAI->getSupportsWeakOmittedEHFrame()))
aslc200b112008-08-16 12:57:46 +00003658 {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003659 O << EHFrameInfo.FnName << " = 0\n";
aslc200b112008-08-16 12:57:46 +00003660 // This name has no connection to the function, so it might get
3661 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003662 // dead-stripping unconditionally.
3663 if (const char *UsedDirective = TAI->getUsedDirective())
3664 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003665 } else {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003666 O << EHFrameInfo.FnName << ":\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003667
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003668 // EH frame header.
3669 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3670 "eh_frame_begin", EHFrameInfo.Number, true);
3671 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00003672
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003673 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
3674
Bill Wendling189bde72008-12-24 08:05:17 +00003675 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3676 PrintRelDirective(true, true);
3677 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3678
3679 if (!TAI->isAbsoluteEHSectionOffsets())
3680 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3681 } else {
3682 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3683 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3684 true, true, false);
3685 }
3686
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003687 Asm->EOL("FDE CIE offset");
3688
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003689 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003690 Asm->EOL("FDE initial location");
3691 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003692 "eh_func_begin", EHFrameInfo.Number, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003693 Asm->EOL("FDE address range");
Duncan Sands96144f92008-05-07 19:11:09 +00003694
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003695 // If there is a personality and landing pads then point to the language
3696 // specific data area in the exception table.
3697 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands96144f92008-05-07 19:11:09 +00003698 Asm->EmitULEB128Bytes(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003699 Asm->EOL("Augmentation size");
Duncan Sands96144f92008-05-07 19:11:09 +00003700
3701 if (EHFrameInfo.hasLandingPads)
3702 EmitReference("exception", EHFrameInfo.Number, true, true);
3703 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003704 Asm->EmitInt32((int)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003705 Asm->EOL("Language Specific Data Area");
3706 } else {
3707 Asm->EmitULEB128Bytes(0);
3708 Asm->EOL("Augmentation size");
3709 }
Duncan Sands96144f92008-05-07 19:11:09 +00003710
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003711 // Indicate locations of function specific callee saved registers in
3712 // frame.
Dale Johannesenf5a11532007-11-13 19:13:01 +00003713 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves, true);
aslc200b112008-08-16 12:57:46 +00003714
Dale Johannesen388f20f2008-04-30 00:43:29 +00003715 // On Darwin the linker honors the alignment of eh_frame, which means it
3716 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3717 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003718 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003719 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003720 EmitLabel("eh_frame_end", EHFrameInfo.Number);
aslc200b112008-08-16 12:57:46 +00003721
3722 // If the function is marked used, this table should be also. We cannot
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003723 // make the mark unconditional in this case, since retaining the table
aslc200b112008-08-16 12:57:46 +00003724 // also retains the function in this case, and there is code around
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003725 // that depends on unused functions (calling undefined externals) being
3726 // dead-stripped to link correctly. Yes, there really is.
3727 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3728 if (const char *UsedDirective = TAI->getUsedDirective())
3729 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3730 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003731 }
3732
Duncan Sands241a0c92007-09-05 11:27:52 +00003733 /// EmitExceptionTable - Emit landing pads and actions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003734 ///
3735 /// The general organization of the table is complex, but the basic concepts
3736 /// are easy. First there is a header which describes the location and
3737 /// organization of the three components that follow.
3738 /// 1. The landing pad site information describes the range of code covered
3739 /// by the try. In our case it's an accumulation of the ranges covered
3740 /// by the invokes in the try. There is also a reference to the landing
3741 /// pad that handles the exception once processed. Finally an index into
3742 /// the actions table.
3743 /// 2. The action table, in our case, is composed of pairs of type ids
3744 /// and next action offset. Starting with the action index from the
3745 /// landing pad site, each type Id is checked for a match to the current
3746 /// exception. If it matches then the exception and type id are passed
3747 /// on to the landing pad. Otherwise the next action is looked up. This
3748 /// chain is terminated with a next action of zero. If no type id is
3749 /// found the the frame is unwound and handling continues.
3750 /// 3. Type id table contains references to all the C++ typeinfo for all
3751 /// catches in the function. This tables is reversed indexed base 1.
3752
3753 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3754 static unsigned SharedTypeIds(const LandingPadInfo *L,
3755 const LandingPadInfo *R) {
3756 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3757 unsigned LSize = LIds.size(), RSize = RIds.size();
3758 unsigned MinSize = LSize < RSize ? LSize : RSize;
3759 unsigned Count = 0;
3760
3761 for (; Count != MinSize; ++Count)
3762 if (LIds[Count] != RIds[Count])
3763 return Count;
3764
3765 return Count;
3766 }
3767
3768 /// PadLT - Order landing pads lexicographically by type id.
3769 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
3770 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3771 unsigned LSize = LIds.size(), RSize = RIds.size();
3772 unsigned MinSize = LSize < RSize ? LSize : RSize;
3773
3774 for (unsigned i = 0; i != MinSize; ++i)
3775 if (LIds[i] != RIds[i])
3776 return LIds[i] < RIds[i];
3777
3778 return LSize < RSize;
3779 }
3780
3781 struct KeyInfo {
3782 static inline unsigned getEmptyKey() { return -1U; }
3783 static inline unsigned getTombstoneKey() { return -2U; }
3784 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner92eea072007-09-17 18:34:04 +00003785 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003786 static bool isPod() { return true; }
3787 };
3788
Duncan Sands241a0c92007-09-05 11:27:52 +00003789 /// ActionEntry - Structure describing an entry in the actions table.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003790 struct ActionEntry {
3791 int ValueForTypeID; // The value to write - may not be equal to the type id.
3792 int NextAction;
3793 struct ActionEntry *Previous;
3794 };
3795
Duncan Sands241a0c92007-09-05 11:27:52 +00003796 /// PadRange - Structure holding a try-range and the associated landing pad.
3797 struct PadRange {
3798 // The index of the landing pad.
3799 unsigned PadIndex;
3800 // The index of the begin and end labels in the landing pad's label lists.
3801 unsigned RangeIndex;
3802 };
3803
3804 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3805
3806 /// CallSiteEntry - Structure describing an entry in the call-site table.
3807 struct CallSiteEntry {
Duncan Sands4ff179f2007-12-19 07:36:31 +00003808 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003809 unsigned BeginLabel; // zero indicates the start of the function.
3810 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003811 // The landing pad starts at PadLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003812 unsigned PadLabel; // zero indicates that there is no landing pad.
3813 unsigned Action;
3814 };
3815
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003816 void EmitExceptionTable() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003817 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
3818 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
3819 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3820 if (PadInfos.empty()) return;
3821
3822 // Sort the landing pads in order of their type ids. This is used to fold
3823 // duplicate actions.
3824 SmallVector<const LandingPadInfo *, 64> LandingPads;
3825 LandingPads.reserve(PadInfos.size());
3826 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
3827 LandingPads.push_back(&PadInfos[i]);
3828 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3829
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003830 // Negative type ids index into FilterIds, positive type ids index into
3831 // TypeInfos. The value written for a positive type id is just the type
3832 // id itself. For a negative type id, however, the value written is the
3833 // (negative) byte offset of the corresponding FilterIds entry. The byte
3834 // offset is usually equal to the type id, because the FilterIds entries
3835 // are written using a variable width encoding which outputs one byte per
3836 // entry as long as the value written is not too large, but can differ.
3837 // This kind of complication does not occur for positive type ids because
3838 // type infos are output using a fixed width encoding.
3839 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3840 SmallVector<int, 16> FilterOffsets;
3841 FilterOffsets.reserve(FilterIds.size());
3842 int Offset = -1;
3843 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3844 E = FilterIds.end(); I != E; ++I) {
3845 FilterOffsets.push_back(Offset);
aslc200b112008-08-16 12:57:46 +00003846 Offset -= TargetAsmInfo::getULEB128Size(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003847 }
3848
Duncan Sands241a0c92007-09-05 11:27:52 +00003849 // Compute the actions table and gather the first action index for each
3850 // landing pad site.
3851 SmallVector<ActionEntry, 32> Actions;
3852 SmallVector<unsigned, 64> FirstActions;
3853 FirstActions.reserve(LandingPads.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003854
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003855 int FirstAction = 0;
Duncan Sands241a0c92007-09-05 11:27:52 +00003856 unsigned SizeActions = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003857 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3858 const LandingPadInfo *LP = LandingPads[i];
3859 const std::vector<int> &TypeIds = LP->TypeIds;
3860 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
3861 unsigned SizeSiteActions = 0;
3862
3863 if (NumShared < TypeIds.size()) {
3864 unsigned SizeAction = 0;
3865 ActionEntry *PrevAction = 0;
3866
3867 if (NumShared) {
3868 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3869 assert(Actions.size());
3870 PrevAction = &Actions.back();
aslc200b112008-08-16 12:57:46 +00003871 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3872 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003873 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
aslc200b112008-08-16 12:57:46 +00003874 SizeAction -=
3875 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003876 SizeAction += -PrevAction->NextAction;
3877 PrevAction = PrevAction->Previous;
3878 }
3879 }
3880
3881 // Compute the actions.
3882 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3883 int TypeID = TypeIds[I];
3884 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3885 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
aslc200b112008-08-16 12:57:46 +00003886 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003887
3888 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
aslc200b112008-08-16 12:57:46 +00003889 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003890 SizeSiteActions += SizeAction;
3891
3892 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
3893 Actions.push_back(Action);
3894
3895 PrevAction = &Actions.back();
3896 }
3897
3898 // Record the first action of the landing pad site.
3899 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3900 } // else identical - re-use previous FirstAction
3901
3902 FirstActions.push_back(FirstAction);
3903
3904 // Compute this sites contribution to size.
3905 SizeActions += SizeSiteActions;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003906 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003907
Duncan Sands4ff179f2007-12-19 07:36:31 +00003908 // Compute the call-site table. The entry for an invoke has a try-range
3909 // containing the call, a non-zero landing pad and an appropriate action.
3910 // The entry for an ordinary call has a try-range containing the call and
3911 // zero for the landing pad and the action. Calls marked 'nounwind' have
3912 // no entry and must not be contained in the try-range of any entry - they
3913 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands241a0c92007-09-05 11:27:52 +00003914 SmallVector<CallSiteEntry, 64> CallSites;
3915
3916 RangeMapType PadMap;
Duncan Sands4ff179f2007-12-19 07:36:31 +00003917 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3918 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3919 // try-ranges for them need be deduced.
Duncan Sands241a0c92007-09-05 11:27:52 +00003920 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3921 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003922 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003923 unsigned BeginLabel = LandingPad->BeginLabels[j];
3924 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3925 PadRange P = { i, j };
3926 PadMap[BeginLabel] = P;
3927 }
3928 }
3929
Duncan Sands4ff179f2007-12-19 07:36:31 +00003930 // The end label of the previous invoke or nounwind try-range.
Duncan Sands241a0c92007-09-05 11:27:52 +00003931 unsigned LastLabel = 0;
Duncan Sands4ff179f2007-12-19 07:36:31 +00003932
3933 // Whether there is a potentially throwing instruction (currently this means
3934 // an ordinary call) between the end of the previous try-range and now.
3935 bool SawPotentiallyThrowing = false;
3936
3937 // Whether the last callsite entry was for an invoke.
3938 bool PreviousIsInvoke = false;
3939
Duncan Sands4ff179f2007-12-19 07:36:31 +00003940 // Visit all instructions in order of address.
Duncan Sands241a0c92007-09-05 11:27:52 +00003941 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3942 I != E; ++I) {
3943 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3944 MI != E; ++MI) {
Dan Gohmanfa607c92008-07-01 00:05:16 +00003945 if (!MI->isLabel()) {
Chris Lattner5b930372008-01-07 07:27:27 +00003946 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands241a0c92007-09-05 11:27:52 +00003947 continue;
3948 }
3949
Chris Lattnerda4cff12007-12-30 20:50:28 +00003950 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands241a0c92007-09-05 11:27:52 +00003951 assert(BeginLabel && "Invalid label!");
Duncan Sands89372f62007-09-05 14:12:46 +00003952
Duncan Sands4ff179f2007-12-19 07:36:31 +00003953 // End of the previous try-range?
Duncan Sands89372f62007-09-05 14:12:46 +00003954 if (BeginLabel == LastLabel)
Duncan Sands4ff179f2007-12-19 07:36:31 +00003955 SawPotentiallyThrowing = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003956
Duncan Sands4ff179f2007-12-19 07:36:31 +00003957 // Beginning of a new try-range?
Duncan Sands241a0c92007-09-05 11:27:52 +00003958 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands241a0c92007-09-05 11:27:52 +00003959 if (L == PadMap.end())
Duncan Sands4ff179f2007-12-19 07:36:31 +00003960 // Nope, it was just some random label.
Duncan Sands241a0c92007-09-05 11:27:52 +00003961 continue;
3962
3963 PadRange P = L->second;
3964 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3965
3966 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3967 "Inconsistent landing pad map!");
3968
3969 // If some instruction between the previous try-range and this one may
3970 // throw, create a call-site entry with no landing pad for the region
3971 // between the try-ranges.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003972 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003973 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3974 CallSites.push_back(Site);
Duncan Sands4ff179f2007-12-19 07:36:31 +00003975 PreviousIsInvoke = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003976 }
3977
3978 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003979 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands241a0c92007-09-05 11:27:52 +00003980
Duncan Sands4ff179f2007-12-19 07:36:31 +00003981 if (LandingPad->LandingPadLabel) {
3982 // This try-range is for an invoke.
3983 CallSiteEntry Site = {BeginLabel, LastLabel,
3984 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands241a0c92007-09-05 11:27:52 +00003985
Duncan Sands4ff179f2007-12-19 07:36:31 +00003986 // Try to merge with the previous call-site.
3987 if (PreviousIsInvoke) {
Dan Gohman3d436002008-06-21 22:00:54 +00003988 CallSiteEntry &Prev = CallSites.back();
Duncan Sands4ff179f2007-12-19 07:36:31 +00003989 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3990 // Extend the range of the previous entry.
3991 Prev.EndLabel = Site.EndLabel;
3992 continue;
3993 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003994 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003995
Duncan Sands4ff179f2007-12-19 07:36:31 +00003996 // Otherwise, create a new call-site.
3997 CallSites.push_back(Site);
3998 PreviousIsInvoke = true;
3999 } else {
4000 // Create a gap.
4001 PreviousIsInvoke = false;
4002 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004003 }
4004 }
4005 // If some instruction between the previous try-range and the end of the
4006 // function may throw, create a call-site entry with no landing pad for the
4007 // region following the try-range.
Duncan Sands4ff179f2007-12-19 07:36:31 +00004008 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00004009 CallSiteEntry Site = {LastLabel, 0, 0, 0};
4010 CallSites.push_back(Site);
4011 }
4012
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004013 // Final tallies.
Duncan Sands96144f92008-05-07 19:11:09 +00004014
4015 // Call sites.
4016 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
4017 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
4018 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
4019 unsigned SizeSites = CallSites.size() * (SiteStartSize +
4020 SiteLengthSize +
4021 LandingPadSize);
Duncan Sands241a0c92007-09-05 11:27:52 +00004022 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
aslc200b112008-08-16 12:57:46 +00004023 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands241a0c92007-09-05 11:27:52 +00004024
Duncan Sands96144f92008-05-07 19:11:09 +00004025 // Type infos.
4026 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
4027 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004028
4029 unsigned TypeOffset = sizeof(int8_t) + // Call site format
aslc200b112008-08-16 12:57:46 +00004030 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004031 SizeSites + SizeActions + SizeTypes;
4032
4033 unsigned TotalSize = sizeof(int8_t) + // LPStart format
4034 sizeof(int8_t) + // TType format
aslc200b112008-08-16 12:57:46 +00004035 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004036 TypeOffset;
4037
4038 unsigned SizeAlign = (4 - TotalSize) & 3;
4039
4040 // Begin the exception table.
4041 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng7e7d1942008-02-29 19:36:59 +00004042 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesen841e4982008-10-08 21:50:21 +00004043 O << "GCC_except_table" << SubprogramCount << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004044 for (unsigned i = 0; i != SizeAlign; ++i) {
4045 Asm->EmitInt8(0);
4046 Asm->EOL("Padding");
4047 }
4048 EmitLabel("exception", SubprogramCount);
4049
4050 // Emit the header.
4051 Asm->EmitInt8(DW_EH_PE_omit);
4052 Asm->EOL("LPStart format (DW_EH_PE_omit)");
4053 Asm->EmitInt8(DW_EH_PE_absptr);
4054 Asm->EOL("TType format (DW_EH_PE_absptr)");
4055 Asm->EmitULEB128Bytes(TypeOffset);
4056 Asm->EOL("TType base offset");
4057 Asm->EmitInt8(DW_EH_PE_udata4);
4058 Asm->EOL("Call site format (DW_EH_PE_udata4)");
4059 Asm->EmitULEB128Bytes(SizeSites);
4060 Asm->EOL("Call-site table length");
4061
Duncan Sands241a0c92007-09-05 11:27:52 +00004062 // Emit the landing pad site information.
4063 for (unsigned i = 0; i < CallSites.size(); ++i) {
4064 CallSiteEntry &S = CallSites[i];
4065 const char *BeginTag;
4066 unsigned BeginNumber;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004067
Duncan Sands241a0c92007-09-05 11:27:52 +00004068 if (!S.BeginLabel) {
4069 BeginTag = "eh_func_begin";
4070 BeginNumber = SubprogramCount;
4071 } else {
4072 BeginTag = "label";
4073 BeginNumber = S.BeginLabel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004074 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004075
Duncan Sands241a0c92007-09-05 11:27:52 +00004076 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00004077 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004078 Asm->EOL("Region start");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004079
Duncan Sands241a0c92007-09-05 11:27:52 +00004080 if (!S.EndLabel) {
Dale Johannesen4670be42008-01-15 23:24:56 +00004081 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands96144f92008-05-07 19:11:09 +00004082 true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004083 } else {
Duncan Sands96144f92008-05-07 19:11:09 +00004084 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004085 }
4086 Asm->EOL("Region length");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004087
Duncan Sands96144f92008-05-07 19:11:09 +00004088 if (!S.PadLabel)
4089 Asm->EmitInt32(0);
4090 else
Duncan Sands241a0c92007-09-05 11:27:52 +00004091 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00004092 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004093 Asm->EOL("Landing pad");
4094
4095 Asm->EmitULEB128Bytes(S.Action);
4096 Asm->EOL("Action");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004097 }
4098
4099 // Emit the actions.
4100 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4101 ActionEntry &Action = Actions[I];
4102
4103 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
4104 Asm->EOL("TypeInfo index");
4105 Asm->EmitSLEB128Bytes(Action.NextAction);
4106 Asm->EOL("Next action");
4107 }
4108
4109 // Emit the type ids.
4110 for (unsigned M = TypeInfos.size(); M; --M) {
4111 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00004112
4113 PrintRelDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004114
4115 if (GV)
4116 O << Asm->getGlobalLinkName(GV);
4117 else
4118 O << "0";
Duncan Sands241a0c92007-09-05 11:27:52 +00004119
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004120 Asm->EOL("TypeInfo");
4121 }
4122
4123 // Emit the filter typeids.
4124 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4125 unsigned TypeID = FilterIds[j];
4126 Asm->EmitULEB128Bytes(TypeID);
4127 Asm->EOL("Filter TypeInfo index");
4128 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004129
Evan Cheng7e7d1942008-02-29 19:36:59 +00004130 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004131 }
4132
4133public:
4134 //===--------------------------------------------------------------------===//
4135 // Main entry points.
4136 //
Owen Anderson847b99b2008-08-21 00:14:44 +00004137 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattnerb3876c72007-09-24 03:35:37 +00004138 : Dwarf(OS, A, T, "eh")
Dale Johannesen85535762008-04-02 00:25:04 +00004139 , shouldEmitTable(false)
4140 , shouldEmitMoves(false)
4141 , shouldEmitTableModule(false)
4142 , shouldEmitMovesModule(false)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004143 {}
aslc200b112008-08-16 12:57:46 +00004144
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004145 virtual ~DwarfException() {}
4146
4147 /// SetModuleInfo - Set machine module information when it's known that pass
4148 /// manager has created it. Set by the target AsmPrinter.
4149 void SetModuleInfo(MachineModuleInfo *mmi) {
4150 MMI = mmi;
4151 }
4152
4153 /// BeginModule - Emit all exception information that should come prior to the
4154 /// content.
4155 void BeginModule(Module *M) {
4156 this->M = M;
4157 }
4158
4159 /// EndModule - Emit all exception information that should come after the
4160 /// content.
4161 void EndModule() {
Dale Johannesen85535762008-04-02 00:25:04 +00004162 if (shouldEmitMovesModule || shouldEmitTableModule) {
4163 const std::vector<Function *> Personalities = MMI->getPersonalities();
4164 for (unsigned i =0; i < Personalities.size(); ++i)
4165 EmitCommonEHFrame(Personalities[i], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004166
Dale Johannesen85535762008-04-02 00:25:04 +00004167 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4168 E = EHFrames.end(); I != E; ++I)
4169 EmitEHFrame(*I);
4170 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004171 }
4172
aslc200b112008-08-16 12:57:46 +00004173 /// BeginFunction - Gather pre-function exception information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004174 /// emitted immediately after the function entry point.
4175 void BeginFunction(MachineFunction *MF) {
4176 this->MF = MF;
Dale Johannesen85535762008-04-02 00:25:04 +00004177 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen62f0a6d2008-04-02 17:04:45 +00004178 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen85535762008-04-02 00:25:04 +00004179
4180 // Map all labels and get rid of any dead landing pads.
4181 MMI->TidyLandingPads();
4182 // If any landing pads survive, we need an EH table.
4183 if (MMI->getLandingPads().size())
4184 shouldEmitTable = true;
4185
4186 // See if we need frame move info.
Duncan Sandscbc28b12008-07-04 09:55:48 +00004187 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen85535762008-04-02 00:25:04 +00004188 shouldEmitMoves = true;
4189
4190 if (shouldEmitMoves || shouldEmitTable)
4191 // Assumes in correct section after the entry point.
4192 EmitLabel("eh_func_begin", ++SubprogramCount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004193 }
Dale Johannesen85535762008-04-02 00:25:04 +00004194 shouldEmitTableModule |= shouldEmitTable;
4195 shouldEmitMovesModule |= shouldEmitMoves;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004196 }
4197
4198 /// EndFunction - Gather and emit post-function exception information.
4199 ///
4200 void EndFunction() {
Dale Johannesen85535762008-04-02 00:25:04 +00004201 if (shouldEmitMoves || shouldEmitTable) {
4202 EmitLabel("eh_func_end", SubprogramCount);
4203 EmitExceptionTable();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004204
Dale Johannesen85535762008-04-02 00:25:04 +00004205 // Save EH frame information
4206 EHFrames.
4207 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendlingef9211a2007-09-18 01:47:22 +00004208 SubprogramCount,
4209 MMI->getPersonalityIndex(),
4210 MF->getFrameInfo()->hasCalls(),
4211 !MMI->getLandingPads().empty(),
Dale Johannesenfb3ac732007-11-20 23:24:42 +00004212 MMI->getFrameMoves(),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00004213 MF->getFunction()));
Dale Johannesen85535762008-04-02 00:25:04 +00004214 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004215 }
4216};
4217
4218} // End of namespace llvm
4219
4220//===----------------------------------------------------------------------===//
4221
4222/// Emit - Print the abbreviation using the specified Dwarf writer.
4223///
4224void DIEAbbrev::Emit(const DwarfDebug &DD) const {
4225 // Emit its Dwarf tag type.
4226 DD.getAsm()->EmitULEB128Bytes(Tag);
4227 DD.getAsm()->EOL(TagString(Tag));
aslc200b112008-08-16 12:57:46 +00004228
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004229 // Emit whether it has children DIEs.
4230 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4231 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
aslc200b112008-08-16 12:57:46 +00004232
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004233 // For each attribute description.
4234 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4235 const DIEAbbrevData &AttrData = Data[i];
aslc200b112008-08-16 12:57:46 +00004236
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004237 // Emit attribute type.
4238 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4239 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
aslc200b112008-08-16 12:57:46 +00004240
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004241 // Emit form type.
4242 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4243 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
4244 }
4245
4246 // Mark end of abbreviation.
4247 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4248 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
4249}
4250
4251#ifndef NDEBUG
4252void DIEAbbrev::print(std::ostream &O) {
4253 O << "Abbreviation @"
4254 << std::hex << (intptr_t)this << std::dec
4255 << " "
4256 << TagString(Tag)
4257 << " "
4258 << ChildrenString(ChildrenFlag)
4259 << "\n";
aslc200b112008-08-16 12:57:46 +00004260
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004261 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4262 O << " "
4263 << AttributeString(Data[i].getAttribute())
4264 << " "
4265 << FormEncodingString(Data[i].getForm())
4266 << "\n";
4267 }
4268}
4269void DIEAbbrev::dump() { print(cerr); }
4270#endif
4271
4272//===----------------------------------------------------------------------===//
4273
4274#ifndef NDEBUG
4275void DIEValue::dump() {
4276 print(cerr);
4277}
4278#endif
4279
4280//===----------------------------------------------------------------------===//
4281
4282/// EmitValue - Emit integer of appropriate size.
4283///
4284void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
4285 switch (Form) {
4286 case DW_FORM_flag: // Fall thru
4287 case DW_FORM_ref1: // Fall thru
4288 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
4289 case DW_FORM_ref2: // Fall thru
4290 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
4291 case DW_FORM_ref4: // Fall thru
4292 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
4293 case DW_FORM_ref8: // Fall thru
4294 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4295 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4296 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
4297 default: assert(0 && "DIE Value form not supported yet"); break;
4298 }
4299}
4300
4301/// SizeOf - Determine size of integer value in bytes.
4302///
4303unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4304 switch (Form) {
4305 case DW_FORM_flag: // Fall thru
4306 case DW_FORM_ref1: // Fall thru
4307 case DW_FORM_data1: return sizeof(int8_t);
4308 case DW_FORM_ref2: // Fall thru
4309 case DW_FORM_data2: return sizeof(int16_t);
4310 case DW_FORM_ref4: // Fall thru
4311 case DW_FORM_data4: return sizeof(int32_t);
4312 case DW_FORM_ref8: // Fall thru
4313 case DW_FORM_data8: return sizeof(int64_t);
aslc200b112008-08-16 12:57:46 +00004314 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4315 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004316 default: assert(0 && "DIE Value form not supported yet"); break;
4317 }
4318 return 0;
4319}
4320
4321//===----------------------------------------------------------------------===//
4322
4323/// EmitValue - Emit string value.
4324///
4325void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
4326 DD.getAsm()->EmitString(String);
4327}
4328
4329//===----------------------------------------------------------------------===//
4330
4331/// EmitValue - Emit label value.
4332///
4333void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004334 bool IsSmall = Form == DW_FORM_data4;
4335 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004336}
4337
4338/// SizeOf - Determine size of label value in bytes.
4339///
4340unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004341 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004342 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004343}
4344
4345//===----------------------------------------------------------------------===//
4346
4347/// EmitValue - Emit label value.
4348///
4349void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004350 bool IsSmall = Form == DW_FORM_data4;
4351 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004352}
4353
4354/// SizeOf - Determine size of label value in bytes.
4355///
4356unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004357 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004358 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004359}
aslc200b112008-08-16 12:57:46 +00004360
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004361//===----------------------------------------------------------------------===//
4362
4363/// EmitValue - Emit delta value.
4364///
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004365void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4366 bool IsSmall = Form == DW_FORM_data4;
4367 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4368 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4369}
4370
4371/// SizeOf - Determine size of delta value in bytes.
4372///
4373unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4374 if (Form == DW_FORM_data4) return 4;
4375 return DD.getTargetData()->getPointerSize();
4376}
aslc200b112008-08-16 12:57:46 +00004377
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004378//===----------------------------------------------------------------------===//
4379
4380/// EmitValue - Emit delta value.
4381///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004382void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
4383 bool IsSmall = Form == DW_FORM_data4;
4384 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
4385}
4386
4387/// SizeOf - Determine size of delta value in bytes.
4388///
4389unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4390 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004391 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004392}
4393
4394//===----------------------------------------------------------------------===//
4395
4396/// EmitValue - Emit debug information entry offset.
4397///
4398void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
4399 DD.getAsm()->EmitInt32(Entry->getOffset());
4400}
aslc200b112008-08-16 12:57:46 +00004401
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004402//===----------------------------------------------------------------------===//
4403
4404/// ComputeSize - calculate the size of the block.
4405///
4406unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
4407 if (!Size) {
Owen Anderson88dd6232008-06-24 21:44:59 +00004408 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004409
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004410 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4411 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
4412 }
4413 }
4414 return Size;
4415}
4416
4417/// EmitValue - Emit block data.
4418///
4419void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
4420 switch (Form) {
4421 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4422 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4423 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4424 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
4425 default: assert(0 && "Improper form for block"); break;
4426 }
aslc200b112008-08-16 12:57:46 +00004427
Owen Anderson88dd6232008-06-24 21:44:59 +00004428 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004429
4430 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4431 DD.getAsm()->EOL();
4432 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
4433 }
4434}
4435
4436/// SizeOf - Determine size of block data in bytes.
4437///
4438unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4439 switch (Form) {
4440 case DW_FORM_block1: return Size + sizeof(int8_t);
4441 case DW_FORM_block2: return Size + sizeof(int16_t);
4442 case DW_FORM_block4: return Size + sizeof(int32_t);
aslc200b112008-08-16 12:57:46 +00004443 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004444 default: assert(0 && "Improper form for block"); break;
4445 }
4446 return 0;
4447}
4448
4449//===----------------------------------------------------------------------===//
4450/// DIE Implementation
4451
4452DIE::~DIE() {
4453 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4454 delete Children[i];
4455}
aslc200b112008-08-16 12:57:46 +00004456
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004457/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4458///
4459void DIE::AddSiblingOffset() {
4460 DIEInteger *DI = new DIEInteger(0);
4461 Values.insert(Values.begin(), DI);
4462 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
4463}
4464
4465/// Profile - Used to gather unique data for the value folding set.
4466///
4467void DIE::Profile(FoldingSetNodeID &ID) {
4468 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00004469
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004470 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4471 ID.AddPointer(Children[i]);
4472
4473 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4474 ID.AddPointer(Values[j]);
4475}
4476
4477#ifndef NDEBUG
4478void DIE::print(std::ostream &O, unsigned IncIndent) {
4479 static unsigned IndentCount = 0;
4480 IndentCount += IncIndent;
4481 const std::string Indent(IndentCount, ' ');
4482 bool isBlock = Abbrev.getTag() == 0;
aslc200b112008-08-16 12:57:46 +00004483
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004484 if (!isBlock) {
4485 O << Indent
4486 << "Die: "
4487 << "0x" << std::hex << (intptr_t)this << std::dec
4488 << ", Offset: " << Offset
4489 << ", Size: " << Size
aslc200b112008-08-16 12:57:46 +00004490 << "\n";
4491
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004492 O << Indent
4493 << TagString(Abbrev.getTag())
4494 << " "
4495 << ChildrenString(Abbrev.getChildrenFlag());
4496 } else {
4497 O << "Size: " << Size;
4498 }
4499 O << "\n";
4500
Owen Anderson88dd6232008-06-24 21:44:59 +00004501 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004502
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004503 IndentCount += 2;
4504 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4505 O << Indent;
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004506
4507 if (!isBlock)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004508 O << AttributeString(Data[i].getAttribute());
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004509 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004510 O << "Blk[" << i << "]";
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004511
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004512 O << " "
4513 << FormEncodingString(Data[i].getForm())
4514 << " ";
4515 Values[i]->print(O);
4516 O << "\n";
4517 }
4518 IndentCount -= 2;
4519
4520 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4521 Children[j]->print(O, 4);
4522 }
aslc200b112008-08-16 12:57:46 +00004523
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004524 if (!isBlock) O << "\n";
4525 IndentCount -= IncIndent;
4526}
4527
4528void DIE::dump() {
4529 print(cerr);
4530}
4531#endif
4532
4533//===----------------------------------------------------------------------===//
4534/// DwarfWriter Implementation
4535///
4536
Owen Anderson847b99b2008-08-21 00:14:44 +00004537DwarfWriter::DwarfWriter(raw_ostream &OS, AsmPrinter *A,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004538 const TargetAsmInfo *T) {
4539 DE = new DwarfException(OS, A, T);
4540 DD = new DwarfDebug(OS, A, T);
4541}
4542
4543DwarfWriter::~DwarfWriter() {
4544 delete DE;
4545 delete DD;
4546}
4547
4548/// SetModuleInfo - Set machine module info when it's known that pass manager
4549/// has created it. Set by the target AsmPrinter.
4550void DwarfWriter::SetModuleInfo(MachineModuleInfo *MMI) {
4551 DD->SetModuleInfo(MMI);
4552 DE->SetModuleInfo(MMI);
4553}
4554
4555/// BeginModule - Emit all Dwarf sections that should come prior to the
4556/// content.
4557void DwarfWriter::BeginModule(Module *M) {
4558 DE->BeginModule(M);
4559 DD->BeginModule(M);
4560}
4561
4562/// EndModule - Emit all Dwarf sections that should come after the content.
4563///
4564void DwarfWriter::EndModule() {
4565 DE->EndModule();
4566 DD->EndModule();
4567}
4568
aslc200b112008-08-16 12:57:46 +00004569/// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004570/// emitted immediately after the function entry point.
4571void DwarfWriter::BeginFunction(MachineFunction *MF) {
4572 DE->BeginFunction(MF);
4573 DD->BeginFunction(MF);
4574}
4575
4576/// EndFunction - Gather and emit post-function debug information.
4577///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00004578void DwarfWriter::EndFunction(MachineFunction *MF) {
4579 DD->EndFunction(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004580 DE->EndFunction();
aslc200b112008-08-16 12:57:46 +00004581
Bill Wendling5b4796a2008-07-22 00:53:37 +00004582 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004583 // Clear function debug information.
4584 MMI->EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004585}