blob: c1360a94328589004fb9932749a89999b2525b87 [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//===----------------------------------------------------------------------===//
Devang Patel4d1709e2009-01-08 02:33:41 +00001196/// DbgVariable - This class is used to track local variable information.
1197///
1198class DbgVariable {
1199private:
1200 DIVariable *Var; // Variable Descriptor.
1201 unsigned FrameIndex; // Variable frame index.
1202
1203public:
1204 DbgVariable(DIVariable *V, unsigned I) : Var(V), FrameIndex(I) {}
1205
1206 // Accessors.
1207 DIVariable *getVariable() const { return Var; }
1208 unsigned getFrameIndex() const { return FrameIndex; }
1209};
1210
1211//===----------------------------------------------------------------------===//
1212/// DbgScope - This class is used to track scope information.
1213///
1214class DbgScope {
1215private:
1216 DbgScope *Parent; // Parent to this scope.
1217 DIDescriptor *Desc; // Debug info descriptor for scope.
1218 // Either subprogram or block.
1219 unsigned StartLabelID; // Label ID of the beginning of scope.
1220 unsigned EndLabelID; // Label ID of the end of scope.
1221 SmallVector<DbgScope *, 8> Scopes; // Scopes defined in scope.
1222 SmallVector<DbgVariable *, 32> Variables;// Variables declared in scope.
1223
1224public:
1225 DbgScope(DbgScope *P, DIDescriptor *D)
1226 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), Scopes(), Variables()
1227 {}
1228 ~DbgScope();
1229
1230 // Accessors.
1231 DbgScope *getParent() const { return Parent; }
1232 DIDescriptor *getDesc() const { return Desc; }
1233 unsigned getStartLabelID() const { return StartLabelID; }
1234 unsigned getEndLabelID() const { return EndLabelID; }
1235 SmallVector<DbgScope *, 8> &getScopes() { return Scopes; }
1236 SmallVector<DbgVariable *, 32> &getVariables() { return Variables; }
1237 void setStartLabelID(unsigned S) { StartLabelID = S; }
1238 void setEndLabelID(unsigned E) { EndLabelID = E; }
1239
1240 /// AddScope - Add a scope to the scope.
1241 ///
1242 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1243
1244 /// AddVariable - Add a variable to the scope.
1245 ///
1246 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
1247};
1248
1249//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00001250/// DwarfDebug - Emits Dwarf debug directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001251///
1252class DwarfDebug : public Dwarf {
1253
1254private:
1255 //===--------------------------------------------------------------------===//
1256 // Attributes used to construct specific Dwarf sections.
1257 //
aslc200b112008-08-16 12:57:46 +00001258
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001259 /// CompileUnits - All the compile units involved in this build. The index
1260 /// of each entry in this vector corresponds to the sources in MMI.
1261 std::vector<CompileUnit *> CompileUnits;
Devang Patel5f244e32009-01-05 22:35:52 +00001262 DenseMap<GlobalVariable *, CompileUnit *> DW_CUs;
aslc200b112008-08-16 12:57:46 +00001263
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001264 /// AbbreviationsSet - Used to uniquely define abbreviations.
1265 ///
1266 FoldingSet<DIEAbbrev> AbbreviationsSet;
1267
1268 /// Abbreviations - A list of all the unique abbreviations in use.
1269 ///
1270 std::vector<DIEAbbrev *> Abbreviations;
aslc200b112008-08-16 12:57:46 +00001271
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001272 /// ValuesSet - Used to uniquely define values.
1273 ///
Devang Patel5f244e32009-01-05 22:35:52 +00001274 // Directories - Uniquing vector for directories.
1275 UniqueVector<std::string> Directories;
1276
1277 // SourceFiles - Uniquing vector for source files.
1278 UniqueVector<SrcFileInfo> SrcFiles;
1279
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001280 FoldingSet<DIEValue> ValuesSet;
aslc200b112008-08-16 12:57:46 +00001281
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001282 /// Values - A list of all the unique values in use.
1283 ///
1284 std::vector<DIEValue *> Values;
aslc200b112008-08-16 12:57:46 +00001285
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001286 /// StringPool - A UniqueVector of strings used by indirect references.
1287 ///
1288 UniqueVector<std::string> StringPool;
1289
1290 /// UnitMap - Map debug information descriptor to compile unit.
1291 ///
1292 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
aslc200b112008-08-16 12:57:46 +00001293
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001294 /// SectionMap - Provides a unique id per text section.
1295 ///
Anton Korobeynikov55b94962008-09-24 22:15:21 +00001296 UniqueVector<const Section*> SectionMap;
aslc200b112008-08-16 12:57:46 +00001297
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001298 /// SectionSourceLines - Tracks line numbers per text section.
1299 ///
1300 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
1301
1302 /// didInitial - Flag to indicate if initial emission has been done.
1303 ///
1304 bool didInitial;
aslc200b112008-08-16 12:57:46 +00001305
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001306 /// shouldEmit - Flag to indicate if debug information should be emitted.
1307 ///
1308 bool shouldEmit;
1309
Devang Patel4d1709e2009-01-08 02:33:41 +00001310 // RootScope - Top level scope for the current function.
1311 //
1312 DbgScope *RootDbgScope;
1313
1314 // DbgScopeMap - Tracks the scopes in the current function.
1315 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
1316
1317 // DbgLabelIDList - One entry per assigned label. Normally the entry is equal to
1318 // the list index(+1). If the entry is zero then the label has been deleted.
1319 // Any other value indicates the label has been deleted by is mapped to
1320 // another label.
1321 SmallVector<unsigned, 32> DbgLabelIDList;
1322
1323 /// NextLabelID - Return the next unique label id.
1324 ///
1325 unsigned NextLabelID() {
1326 unsigned ID = (unsigned)DbgLabelIDList.size() + 1;
1327 DbgLabelIDList.push_back(ID);
1328 return ID;
1329 }
1330
1331 /// RemapLabel - Indicate that a label has been merged into another.
1332 ///
1333 void RemapLabel(unsigned OldLabelID, unsigned NewLabelID) {
1334 assert(0 < OldLabelID && OldLabelID <= DbgLabelIDList.size() &&
1335 "Old label ID out of range.");
1336 assert(NewLabelID <= DbgLabelIDList.size() &&
1337 "New label ID out of range.");
1338 DbgLabelIDList[OldLabelID - 1] = NewLabelID;
1339 }
1340
1341 /// MappedLabel - Find out the label's final ID. Zero indicates deletion.
1342 /// ID != Mapped ID indicates that the label was folded into another label.
1343 unsigned MappedLabel(unsigned LabelID) const {
1344 assert(LabelID <= DbgLabelIDList.size() && "Debug label ID out of range.");
1345 return LabelID ? DbgLabelIDList[LabelID - 1] : 0;
1346 }
1347
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001348 struct FunctionDebugFrameInfo {
1349 unsigned Number;
1350 std::vector<MachineMove> Moves;
1351
1352 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman9ba5d4d2007-08-27 14:50:10 +00001353 Number(Num), Moves(M) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001354 };
1355
1356 std::vector<FunctionDebugFrameInfo> DebugFrames;
aslc200b112008-08-16 12:57:46 +00001357
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001358public:
aslc200b112008-08-16 12:57:46 +00001359
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001360 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1361 ///
1362 bool ShouldEmitDwarf() const { return shouldEmit; }
1363
1364 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
aslc200b112008-08-16 12:57:46 +00001365 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001366 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1367 // Profile the node so that we can make it unique.
1368 FoldingSetNodeID ID;
1369 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00001370
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001371 // Check the set for priors.
1372 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
aslc200b112008-08-16 12:57:46 +00001373
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001374 // If it's newly added.
1375 if (InSet == &Abbrev) {
aslc200b112008-08-16 12:57:46 +00001376 // Add to abbreviation list.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001377 Abbreviations.push_back(&Abbrev);
1378 // Assign the vector position + 1 as its number.
1379 Abbrev.setNumber(Abbreviations.size());
1380 } else {
1381 // Assign existing abbreviation number.
1382 Abbrev.setNumber(InSet->getNumber());
1383 }
1384 }
1385
1386 /// NewString - Add a string to the constant pool and returns a label.
1387 ///
1388 DWLabel NewString(const std::string &String) {
1389 unsigned StringID = StringPool.insert(String);
1390 return DWLabel("string", StringID);
1391 }
aslc200b112008-08-16 12:57:46 +00001392
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001393 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1394 /// entry.
1395 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1396 DIEntry *Value;
aslc200b112008-08-16 12:57:46 +00001397
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001398 if (Entry) {
1399 FoldingSetNodeID ID;
1400 DIEntry::Profile(ID, Entry);
1401 void *Where;
1402 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
aslc200b112008-08-16 12:57:46 +00001403
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001404 if (Value) return Value;
aslc200b112008-08-16 12:57:46 +00001405
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001406 Value = new DIEntry(Entry);
1407 ValuesSet.InsertNode(Value, Where);
1408 } else {
1409 Value = new DIEntry(Entry);
1410 }
aslc200b112008-08-16 12:57:46 +00001411
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001412 Values.push_back(Value);
1413 return Value;
1414 }
aslc200b112008-08-16 12:57:46 +00001415
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001416 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1417 ///
1418 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1419 Value->Entry = Entry;
1420 // Add to values set if not already there. If it is, we merely have a
1421 // duplicate in the values list (no harm.)
1422 ValuesSet.GetOrInsertNode(Value);
1423 }
1424
1425 /// AddUInt - Add an unsigned integer attribute data and value.
1426 ///
1427 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1428 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1429
1430 FoldingSetNodeID ID;
1431 DIEInteger::Profile(ID, Integer);
1432 void *Where;
1433 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1434 if (!Value) {
1435 Value = new DIEInteger(Integer);
1436 ValuesSet.InsertNode(Value, Where);
1437 Values.push_back(Value);
1438 }
aslc200b112008-08-16 12:57:46 +00001439
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001440 Die->AddValue(Attribute, Form, Value);
1441 }
aslc200b112008-08-16 12:57:46 +00001442
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001443 /// AddSInt - Add an signed integer attribute data and value.
1444 ///
1445 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1446 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1447
1448 FoldingSetNodeID ID;
1449 DIEInteger::Profile(ID, (uint64_t)Integer);
1450 void *Where;
1451 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1452 if (!Value) {
1453 Value = new DIEInteger(Integer);
1454 ValuesSet.InsertNode(Value, Where);
1455 Values.push_back(Value);
1456 }
aslc200b112008-08-16 12:57:46 +00001457
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001458 Die->AddValue(Attribute, Form, Value);
1459 }
aslc200b112008-08-16 12:57:46 +00001460
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001461 /// AddString - Add a std::string attribute data and value.
1462 ///
1463 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1464 const std::string &String) {
1465 FoldingSetNodeID ID;
1466 DIEString::Profile(ID, String);
1467 void *Where;
1468 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1469 if (!Value) {
1470 Value = new DIEString(String);
1471 ValuesSet.InsertNode(Value, Where);
1472 Values.push_back(Value);
1473 }
aslc200b112008-08-16 12:57:46 +00001474
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001475 Die->AddValue(Attribute, Form, Value);
1476 }
aslc200b112008-08-16 12:57:46 +00001477
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001478 /// AddLabel - Add a Dwarf label attribute data and value.
1479 ///
1480 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1481 const DWLabel &Label) {
1482 FoldingSetNodeID ID;
1483 DIEDwarfLabel::Profile(ID, Label);
1484 void *Where;
1485 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1486 if (!Value) {
1487 Value = new DIEDwarfLabel(Label);
1488 ValuesSet.InsertNode(Value, Where);
1489 Values.push_back(Value);
1490 }
aslc200b112008-08-16 12:57:46 +00001491
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001492 Die->AddValue(Attribute, Form, Value);
1493 }
aslc200b112008-08-16 12:57:46 +00001494
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001495 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1496 ///
1497 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1498 const std::string &Label) {
1499 FoldingSetNodeID ID;
1500 DIEObjectLabel::Profile(ID, Label);
1501 void *Where;
1502 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1503 if (!Value) {
1504 Value = new DIEObjectLabel(Label);
1505 ValuesSet.InsertNode(Value, Where);
1506 Values.push_back(Value);
1507 }
aslc200b112008-08-16 12:57:46 +00001508
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001509 Die->AddValue(Attribute, Form, Value);
1510 }
aslc200b112008-08-16 12:57:46 +00001511
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001512 /// AddSectionOffset - Add a section offset label attribute data and value.
1513 ///
1514 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1515 const DWLabel &Label, const DWLabel &Section,
1516 bool isEH = false, bool useSet = true) {
1517 FoldingSetNodeID ID;
1518 DIESectionOffset::Profile(ID, Label, Section);
1519 void *Where;
1520 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1521 if (!Value) {
1522 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1523 ValuesSet.InsertNode(Value, Where);
1524 Values.push_back(Value);
1525 }
aslc200b112008-08-16 12:57:46 +00001526
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001527 Die->AddValue(Attribute, Form, Value);
1528 }
aslc200b112008-08-16 12:57:46 +00001529
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001530 /// AddDelta - Add a label delta attribute data and value.
1531 ///
1532 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1533 const DWLabel &Hi, const DWLabel &Lo) {
1534 FoldingSetNodeID ID;
1535 DIEDelta::Profile(ID, Hi, Lo);
1536 void *Where;
1537 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1538 if (!Value) {
1539 Value = new DIEDelta(Hi, Lo);
1540 ValuesSet.InsertNode(Value, Where);
1541 Values.push_back(Value);
1542 }
aslc200b112008-08-16 12:57:46 +00001543
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001544 Die->AddValue(Attribute, Form, Value);
1545 }
aslc200b112008-08-16 12:57:46 +00001546
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001547 /// AddDIEntry - Add a DIE attribute data and value.
1548 ///
1549 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1550 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1551 }
1552
1553 /// AddBlock - Add block data.
1554 ///
1555 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1556 Block->ComputeSize(*this);
1557 FoldingSetNodeID ID;
1558 Block->Profile(ID);
1559 void *Where;
1560 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1561 if (!Value) {
1562 Value = Block;
1563 ValuesSet.InsertNode(Value, Where);
1564 Values.push_back(Value);
1565 } else {
Chris Lattner3de66892007-09-21 18:25:53 +00001566 // Already exists, reuse the previous one.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001567 delete Block;
Chris Lattner3de66892007-09-21 18:25:53 +00001568 Block = cast<DIEBlock>(Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001569 }
aslc200b112008-08-16 12:57:46 +00001570
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001571 Die->AddValue(Attribute, Block->BestForm(), Value);
1572 }
1573
1574private:
1575
1576 /// AddSourceLine - Add location information to specified debug information
1577 /// entry.
1578 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1579 if (File && Line) {
1580 CompileUnit *FileUnit = FindCompileUnit(File);
1581 unsigned FileID = FileUnit->getID();
1582 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1583 AddUInt(Die, DW_AT_decl_line, 0, Line);
1584 }
1585 }
1586
Devang Patel5f244e32009-01-05 22:35:52 +00001587 /// AddSourceLine - Add location information to specified debug information
1588 /// entry.
Devang Patel4d1709e2009-01-08 02:33:41 +00001589 void AddSourceLine(DIE *Die, DIVariable *V) {
1590 unsigned FileID = 0;
1591 unsigned Line = V->getLineNumber();
1592 if (V->getVersion() < DIDescriptor::Version7) {
1593 // Version6 or earlier. Use compile unit info to get file id.
1594 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1595 FileID = Unit->getID();
1596 } else {
1597 // Version7 or newer, use filename and directory info from DIVariable
1598 // directly.
1599 unsigned DID = Directories.idFor(V->getDirectory());
1600 FileID = SrcFiles.idFor(SrcFileInfo(DID, V->getFilename()));
1601 }
1602 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1603 AddUInt(Die, DW_AT_decl_line, 0, Line);
1604 }
1605
1606 /// AddSourceLine - Add location information to specified debug information
1607 /// entry.
Devang Patel5f244e32009-01-05 22:35:52 +00001608 void AddSourceLine(DIE *Die, DIGlobal *G) {
1609 unsigned FileID = 0;
1610 unsigned Line = G->getLineNumber();
1611 if (G->getVersion() < DIDescriptor::Version7) {
1612 // Version6 or earlier. Use compile unit info to get file id.
1613 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1614 FileID = Unit->getID();
1615 } else {
1616 // Version7 or newer, use filename and directory info from DIGlobal
1617 // directly.
1618 unsigned DID = Directories.idFor(G->getDirectory());
1619 FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1620 }
1621 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1622 AddUInt(Die, DW_AT_decl_line, 0, Line);
1623 }
1624
1625 void AddSourceLine(DIE *Die, DIType *G) {
1626 unsigned FileID = 0;
1627 unsigned Line = G->getLineNumber();
1628 if (G->getVersion() < DIDescriptor::Version7) {
1629 // Version6 or earlier. Use compile unit info to get file id.
1630 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1631 FileID = Unit->getID();
1632 } else {
1633 // Version7 or newer, use filename and directory info from DIGlobal
1634 // directly.
1635 unsigned DID = Directories.idFor(G->getDirectory());
1636 FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1637 }
1638 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1639 AddUInt(Die, DW_AT_decl_line, 0, Line);
1640 }
1641
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001642 /// AddAddress - Add an address attribute to a die based on the location
1643 /// provided.
1644 void AddAddress(DIE *Die, unsigned Attribute,
1645 const MachineLocation &Location) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001646 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001647 DIEBlock *Block = new DIEBlock();
aslc200b112008-08-16 12:57:46 +00001648
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001649 if (Location.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001650 if (Reg < 32) {
1651 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1652 } else {
1653 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1654 AddUInt(Block, 0, DW_FORM_udata, Reg);
1655 }
1656 } else {
1657 if (Reg < 32) {
1658 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1659 } else {
1660 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1661 AddUInt(Block, 0, DW_FORM_udata, Reg);
1662 }
1663 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1664 }
aslc200b112008-08-16 12:57:46 +00001665
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001666 AddBlock(Die, Attribute, 0, Block);
1667 }
aslc200b112008-08-16 12:57:46 +00001668
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001669 /// AddBasicType - Add a new basic type attribute to the specified entity.
1670 ///
1671 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1672 const std::string &Name,
1673 unsigned Encoding, unsigned Size) {
aslc200b112008-08-16 12:57:46 +00001674
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001675 DIE Buffer(DW_TAG_base_type);
1676 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1677 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1678 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf49e13d2009-01-05 17:44:11 +00001679 DIE *BasicTypeDie = Unit->AddDie(Buffer);
1680 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, BasicTypeDie);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001681 }
aslc200b112008-08-16 12:57:46 +00001682
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001683 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1684 ///
1685 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001686 DIE Buffer(DW_TAG_pointer_type);
Dan Gohmancfb72b22007-09-27 23:12:31 +00001687 AddUInt(&Buffer, DW_AT_byte_size, 0, TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001688 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelbbca50b2009-01-05 17:45:59 +00001689 DIE *PointerTypeDie = Unit->AddDie(Buffer);
1690 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, PointerTypeDie);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001691 }
aslc200b112008-08-16 12:57:46 +00001692
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001693 /// AddType - Add a new type attribute to the specified entity.
1694 ///
1695 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1696 if (!TyDesc) {
1697 AddBasicType(Entity, Unit, "", DW_ATE_signed, sizeof(int32_t));
1698 } else {
1699 // Check for pre-existence.
1700 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
aslc200b112008-08-16 12:57:46 +00001701
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001702 // If it exists then use the existing value.
1703 if (Slot) {
1704 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1705 return;
1706 }
aslc200b112008-08-16 12:57:46 +00001707
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001708 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1709 // FIXME - Not sure why programs and variables are coming through here.
1710 // Short cut for handling subprogram types (not really a TyDesc.)
1711 AddPointerType(Entity, Unit, SubprogramTy->getName());
1712 } else if (GlobalVariableDesc *GlobalTy =
1713 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1714 // FIXME - Not sure why programs and variables are coming through here.
1715 // Short cut for handling global variable types (not really a TyDesc.)
1716 AddPointerType(Entity, Unit, GlobalTy->getName());
aslc200b112008-08-16 12:57:46 +00001717 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001718 // Set up proxy.
1719 Slot = NewDIEntry();
aslc200b112008-08-16 12:57:46 +00001720
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001721 // Construct type.
1722 DIE Buffer(DW_TAG_base_type);
1723 ConstructType(Buffer, TyDesc, Unit);
aslc200b112008-08-16 12:57:46 +00001724
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001725 // Add debug information entry to entity and unit.
1726 DIE *Die = Unit->AddDie(Buffer);
1727 SetDIEntry(Slot, Die);
1728 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1729 }
1730 }
1731 }
aslc200b112008-08-16 12:57:46 +00001732
Devang Patel4a4cbe72009-01-05 21:47:57 +00001733 /// AddType - Add a new type attribute to the specified entity.
1734 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
1735 if (Ty.isNull()) {
1736 AddBasicType(Entity, DW_Unit, "", DW_ATE_signed, sizeof(int32_t));
1737 return;
1738 }
1739
1740 // Check for pre-existence.
1741 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1742 // If it exists then use the existing value.
1743 if (Slot) {
1744 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1745 return;
1746 }
1747
1748 // Set up proxy.
1749 Slot = NewDIEntry();
1750
1751 // Construct type.
1752 DIE Buffer(DW_TAG_base_type);
1753 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1754 ConstructTypeDIE(DW_Unit, Buffer, BT);
1755 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1756 ConstructTypeDIE(DW_Unit, Buffer, DT);
1757 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1758 ConstructTypeDIE(DW_Unit, Buffer, CT);
1759
1760 // Add debug information entry to entity and unit.
1761 DIE *Die = DW_Unit->AddDie(Buffer);
1762 SetDIEntry(Slot, Die);
1763 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1764 }
1765
Devang Patel46d13752009-01-05 19:07:53 +00001766 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1767 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1768 DIBasicType *BTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001769
1770 // Get core information.
1771 const std::string &Name = BTy->getName();
1772 Buffer.setTag(DW_TAG_base_type);
1773 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy->getEncoding());
1774 // Add name if not anonymous or intermediate type.
1775 if (!Name.empty())
1776 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1777 uint64_t Size = BTy->getSizeInBits() >> 3;
1778 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1779 }
1780
Devang Patel46d13752009-01-05 19:07:53 +00001781 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1782 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1783 DIDerivedType *DTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001784
1785 // Get core information.
1786 const std::string &Name = DTy->getName();
1787 uint64_t Size = DTy->getSizeInBits() >> 3;
1788 unsigned Tag = DTy->getTag();
1789 // FIXME - Workaround for templates.
1790 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1791
1792 Buffer.setTag(Tag);
1793 // Map to main type, void will not have a type.
1794 DIType FromTy = DTy->getTypeDerivedFrom();
Devang Patel4a4cbe72009-01-05 21:47:57 +00001795 AddType(DW_Unit, &Buffer, FromTy);
Devang Patelfc187162009-01-05 17:57:47 +00001796
1797 // Add name if not anonymous or intermediate type.
1798 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1799
1800 // Add size if non-zero (derived types might be zero-sized.)
1801 if (Size)
1802 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1803
1804 // Add source line info if available and TyDesc is not a forward
1805 // declaration.
1806 // FIXME - Enable this. if (!DTy->isForwardDecl())
1807 // FIXME - Enable this. AddSourceLine(&Buffer, *DTy);
1808 }
1809
Devang Patel30c01372009-01-05 19:55:51 +00001810 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1811 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1812 DICompositeType *CTy) {
1813
1814 // Get core information.
1815 const std::string &Name = CTy->getName();
1816 uint64_t Size = CTy->getSizeInBits() >> 3;
1817 unsigned Tag = CTy->getTag();
1818 switch (Tag) {
1819 case DW_TAG_vector_type:
1820 case DW_TAG_array_type:
1821 ConstructArrayTypeDIE(DW_Unit, Buffer, CTy);
1822 break;
1823 //FIXME - Enable this.
1824 // case DW_TAG_enumeration_type:
1825 // DIArray Elements = CTy->getTypeArray();
1826 // // Add enumerators to enumeration type.
1827 // for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i)
1828 // ConstructEnumTypeDIE(Buffer, &Elements.getElement(i));
1829 // break;
1830 case DW_TAG_subroutine_type:
1831 {
1832 // Add prototype flag.
1833 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1834 DIArray Elements = CTy->getTypeArray();
1835 // Add return type.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001836 DIDescriptor RTy = Elements.getElement(0);
1837 if (DIBasicType *BT = dyn_cast<DIBasicType>(&RTy))
1838 AddType(DW_Unit, &Buffer, *BT);
1839 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&RTy))
1840 AddType(DW_Unit, &Buffer, *DT);
1841 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&RTy))
1842 AddType(DW_Unit, &Buffer, *CT);
1843
1844 //AddType(DW_Unit, &Buffer, Elements.getElement(0));
Devang Patel30c01372009-01-05 19:55:51 +00001845 // Add arguments.
1846 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1847 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel4a4cbe72009-01-05 21:47:57 +00001848 DIDescriptor Ty = Elements.getElement(i);
1849 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1850 AddType(DW_Unit, &Buffer, *BT);
1851 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1852 AddType(DW_Unit, &Buffer, *DT);
1853 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1854 AddType(DW_Unit, &Buffer, *CT);
Devang Patel30c01372009-01-05 19:55:51 +00001855 Buffer.AddChild(Arg);
1856 }
1857 }
1858 break;
1859 case DW_TAG_structure_type:
1860 case DW_TAG_union_type:
1861 {
1862 // Add elements to structure type.
1863 DIArray Elements = CTy->getTypeArray();
1864 // Add elements to structure type.
1865 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1866 DIDescriptor Element = Elements.getElement(i);
1867 if (DISubprogram *SP = dyn_cast<DISubprogram>(&Element))
1868 ConstructFieldTypeDIE(DW_Unit, Buffer, SP);
1869 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Element))
1870 ConstructFieldTypeDIE(DW_Unit, Buffer, DT);
1871 else if (DIGlobalVariable *GV = dyn_cast<DIGlobalVariable>(&Element))
1872 ConstructFieldTypeDIE(DW_Unit, Buffer, GV);
1873 }
1874 }
1875 break;
1876 default:
1877 break;
1878 }
1879
1880 // Add name if not anonymous or intermediate type.
1881 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1882
1883 // Add size if non-zero (derived types might be zero-sized.)
1884 if (Size)
1885 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1886 else {
1887 // Add zero size even if it is not a forward declaration.
1888 // FIXME - Enable this.
1889 // if (!CTy->isDefinition())
1890 // AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1891 // else
1892 // AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1893 }
1894
1895 // Add source line info if available and TyDesc is not a forward
1896 // declaration.
1897 // FIXME - Enable this.
1898 // if (CTy->isForwardDecl())
1899 // AddSourceLine(&Buffer, *CTy);
1900 }
1901
Devang Patel6fb54132009-01-05 18:33:01 +00001902 // ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1903 void ConstructSubrangeDIE (DIE &Buffer, DISubrange *SR, DIE *IndexTy) {
1904 int64_t L = SR->getLo();
1905 int64_t H = SR->getHi();
1906 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1907 if (L != H) {
1908 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1909 if (L)
1910 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1911 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
1912 }
1913 Buffer.AddChild(DW_Subrange);
1914 }
1915
1916 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1917 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1918 DICompositeType *CTy) {
1919 Buffer.setTag(DW_TAG_array_type);
1920 if (CTy->getTag() == DW_TAG_vector_type)
1921 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1922
1923 DIArray Elements = CTy->getTypeArray();
1924 // FIXME - Enable this.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001925 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel6fb54132009-01-05 18:33:01 +00001926
1927 // Construct an anonymous type for index type.
1928 DIE IdxBuffer(DW_TAG_base_type);
1929 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1930 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1931 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1932
1933 // Add subranges to array type.
1934 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel30c01372009-01-05 19:55:51 +00001935 DIDescriptor Element = Elements.getElement(i);
1936 if (DISubrange *SR = dyn_cast<DISubrange>(&Element))
1937 ConstructSubrangeDIE(Buffer, SR, IndexTy);
Devang Patel6fb54132009-01-05 18:33:01 +00001938 }
1939 }
1940
Devang Patela566e812009-01-05 18:38:38 +00001941 /// ConstructEnumTypeDIE - Construct enum type DIE from
1942 /// DIEnumerator.
Devang Patel30c01372009-01-05 19:55:51 +00001943 void ConstructEnumTypeDIE(CompileUnit *DW_Unit,
1944 DIE &Buffer, DIEnumerator *ETy) {
Devang Patela566e812009-01-05 18:38:38 +00001945
1946 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1947 AddString(Enumerator, DW_AT_name, DW_FORM_string, ETy->getName());
1948 int64_t Value = ETy->getEnumValue();
1949 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1950 Buffer.AddChild(Enumerator);
1951 }
Devang Patel6fb54132009-01-05 18:33:01 +00001952
Devang Patel526b01d2009-01-05 18:59:44 +00001953 /// ConstructFieldTypeDIE - Construct variable DIE for a struct field.
1954 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
1955 DIE &Buffer, DIGlobalVariable *V) {
1956
1957 DIE *VariableDie = new DIE(DW_TAG_variable);
1958 const std::string &LinkageName = V->getLinkageName();
1959 if (!LinkageName.empty())
1960 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1961 LinkageName);
1962 // FIXME - Enable this. AddSourceLine(VariableDie, V);
Devang Patel4a4cbe72009-01-05 21:47:57 +00001963 AddType(DW_Unit, VariableDie, V->getType());
Devang Patel526b01d2009-01-05 18:59:44 +00001964 if (!V->isLocalToUnit())
1965 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
1966 AddUInt(VariableDie, DW_AT_declaration, DW_FORM_flag, 1);
1967 Buffer.AddChild(VariableDie);
1968 }
1969
1970 /// ConstructFieldTypeDIE - Construct subprogram DIE for a struct field.
1971 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
1972 DIE &Buffer, DISubprogram *SP,
1973 bool IsConstructor = false) {
1974 DIE *Method = new DIE(DW_TAG_subprogram);
1975 AddString(Method, DW_AT_name, DW_FORM_string, SP->getName());
1976 const std::string &LinkageName = SP->getLinkageName();
1977 if (!LinkageName.empty())
1978 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1979 // FIXME - Enable this. AddSourceLine(Method, SP);
1980
1981 DICompositeType MTy = SP->getType();
1982 DIArray Args = MTy.getTypeArray();
1983
1984 // Add Return Type.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001985 if (!IsConstructor) {
1986 DIDescriptor Ty = Args.getElement(0);
1987 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1988 AddType(DW_Unit, Method, *BT);
1989 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1990 AddType(DW_Unit, Method, *DT);
1991 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1992 AddType(DW_Unit, Method, *CT);
1993 }
Devang Patel526b01d2009-01-05 18:59:44 +00001994
1995 // Add arguments.
1996 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1997 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel4a4cbe72009-01-05 21:47:57 +00001998 DIDescriptor Ty = Args.getElement(i);
1999 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
2000 AddType(DW_Unit, Method, *BT);
2001 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
2002 AddType(DW_Unit, Method, *DT);
2003 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
2004 AddType(DW_Unit, Method, *CT);
Devang Patel526b01d2009-01-05 18:59:44 +00002005 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
2006 Method->AddChild(Arg);
2007 }
2008
2009 if (!SP->isLocalToUnit())
2010 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
2011 Buffer.AddChild(Method);
2012 }
2013
2014 /// COnstructFieldTypeDIE - Construct derived type DIE for a struct field.
2015 void ConstructFieldTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
2016 DIDerivedType *DTy) {
2017 unsigned Tag = DTy->getTag();
2018 DIE *MemberDie = new DIE(Tag);
2019 if (!DTy->getName().empty())
2020 AddString(MemberDie, DW_AT_name, DW_FORM_string, DTy->getName());
2021 // FIXME - Enable this. AddSourceLine(MemberDie, DTy);
2022
2023 DIType FromTy = DTy->getTypeDerivedFrom();
Devang Patel4a4cbe72009-01-05 21:47:57 +00002024 AddType(DW_Unit, MemberDie, FromTy);
Devang Patel526b01d2009-01-05 18:59:44 +00002025
2026 uint64_t Size = DTy->getSizeInBits();
2027 uint64_t Offset = DTy->getOffsetInBits();
2028
2029 // FIXME Handle bitfields
2030
2031 // Add size.
2032 AddUInt(MemberDie, DW_AT_bit_size, 0, Size);
2033 // Add computation for offset.
2034 DIEBlock *Block = new DIEBlock();
2035 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
2036 AddUInt(Block, 0, DW_FORM_udata, Offset >> 3);
2037 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
2038
2039 // FIXME Handle DW_AT_accessibility.
2040
2041 Buffer.AddChild(MemberDie);
2042 }
2043
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002044 /// ConstructType - Adds all the required attributes to the type.
2045 ///
2046 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
2047 // Get core information.
2048 const std::string &Name = TyDesc->getName();
2049 uint64_t Size = TyDesc->getSize() >> 3;
aslc200b112008-08-16 12:57:46 +00002050
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002051 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
2052 // Fundamental types like int, float, bool
2053 Buffer.setTag(DW_TAG_base_type);
2054 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
2055 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
2056 // Fetch tag.
2057 unsigned Tag = DerivedTy->getTag();
2058 // FIXME - Workaround for templates.
2059 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
aslc200b112008-08-16 12:57:46 +00002060 // Pointers, typedefs et al.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002061 Buffer.setTag(Tag);
2062 // Map to main type, void will not have a type.
2063 if (TypeDesc *FromTy = DerivedTy->getFromType())
2064 AddType(&Buffer, FromTy, Unit);
2065 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
2066 // Fetch tag.
2067 unsigned Tag = CompTy->getTag();
aslc200b112008-08-16 12:57:46 +00002068
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002069 // Set tag accordingly.
2070 if (Tag == DW_TAG_vector_type)
2071 Buffer.setTag(DW_TAG_array_type);
aslc200b112008-08-16 12:57:46 +00002072 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002073 Buffer.setTag(Tag);
2074
2075 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
aslc200b112008-08-16 12:57:46 +00002076
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002077 switch (Tag) {
2078 case DW_TAG_vector_type:
2079 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
2080 // Fall thru
2081 case DW_TAG_array_type: {
2082 // Add element type.
2083 if (TypeDesc *FromTy = CompTy->getFromType())
2084 AddType(&Buffer, FromTy, Unit);
aslc200b112008-08-16 12:57:46 +00002085
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002086 // Don't emit size attribute.
2087 Size = 0;
aslc200b112008-08-16 12:57:46 +00002088
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002089 // Construct an anonymous type for index type.
Devang Patelf49e13d2009-01-05 17:44:11 +00002090 DIE Buffer(DW_TAG_base_type);
2091 AddUInt(&Buffer, DW_AT_byte_size, 0, sizeof(int32_t));
2092 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
2093 DIE *IndexTy = Unit->AddDie(Buffer);
aslc200b112008-08-16 12:57:46 +00002094
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002095 // Add subranges to array type.
Evan Chengc7efea32008-12-09 17:56:30 +00002096 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002097 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
2098 int64_t Lo = SRD->getLo();
2099 int64_t Hi = SRD->getHi();
2100 DIE *Subrange = new DIE(DW_TAG_subrange_type);
aslc200b112008-08-16 12:57:46 +00002101
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002102 // If a range is available.
2103 if (Lo != Hi) {
2104 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
2105 // Only add low if non-zero.
2106 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
2107 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
2108 }
aslc200b112008-08-16 12:57:46 +00002109
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002110 Buffer.AddChild(Subrange);
2111 }
2112 break;
2113 }
2114 case DW_TAG_structure_type:
2115 case DW_TAG_union_type: {
2116 // Add elements to structure type.
Evan Chengc7efea32008-12-09 17:56:30 +00002117 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002118 DebugInfoDesc *Element = Elements[i];
aslc200b112008-08-16 12:57:46 +00002119
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002120 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
2121 // Add field or base class.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002122 unsigned Tag = MemberDesc->getTag();
aslc200b112008-08-16 12:57:46 +00002123
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002124 // Extract the basic information.
2125 const std::string &Name = MemberDesc->getName();
2126 uint64_t Size = MemberDesc->getSize();
2127 uint64_t Align = MemberDesc->getAlign();
2128 uint64_t Offset = MemberDesc->getOffset();
aslc200b112008-08-16 12:57:46 +00002129
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002130 // Construct member debug information entry.
2131 DIE *Member = new DIE(Tag);
aslc200b112008-08-16 12:57:46 +00002132
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002133 // Add name if not "".
2134 if (!Name.empty())
2135 AddString(Member, DW_AT_name, DW_FORM_string, Name);
Evan Chengc7efea32008-12-09 17:56:30 +00002136
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002137 // Add location if available.
2138 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
aslc200b112008-08-16 12:57:46 +00002139
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002140 // Most of the time the field info is the same as the members.
2141 uint64_t FieldSize = Size;
2142 uint64_t FieldAlign = Align;
2143 uint64_t FieldOffset = Offset;
aslc200b112008-08-16 12:57:46 +00002144
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002145 // Set the member type.
2146 TypeDesc *FromTy = MemberDesc->getFromType();
2147 AddType(Member, FromTy, Unit);
aslc200b112008-08-16 12:57:46 +00002148
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002149 // Walk up typedefs until a real size is found.
2150 while (FromTy) {
2151 if (FromTy->getTag() != DW_TAG_typedef) {
2152 FieldSize = FromTy->getSize();
Devang Patel105a08a2008-12-23 21:55:38 +00002153 FieldAlign = FromTy->getAlign();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002154 break;
2155 }
aslc200b112008-08-16 12:57:46 +00002156
Dan Gohman53491e92007-07-23 20:24:29 +00002157 FromTy = cast<DerivedTypeDesc>(FromTy)->getFromType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002158 }
aslc200b112008-08-16 12:57:46 +00002159
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002160 // Unless we have a bit field.
2161 if (Tag == DW_TAG_member && FieldSize != Size) {
2162 // Construct the alignment mask.
2163 uint64_t AlignMask = ~(FieldAlign - 1);
2164 // Determine the high bit + 1 of the declared size.
2165 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
2166 // Work backwards to determine the base offset of the field.
2167 FieldOffset = HiMark - FieldSize;
2168 // Now normalize offset to the field.
2169 Offset -= FieldOffset;
aslc200b112008-08-16 12:57:46 +00002170
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002171 // Maybe we need to work from the other end.
2172 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
aslc200b112008-08-16 12:57:46 +00002173
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002174 // Add size and offset.
2175 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
2176 AddUInt(Member, DW_AT_bit_size, 0, Size);
2177 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
2178 }
aslc200b112008-08-16 12:57:46 +00002179
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002180 // Add computation for offset.
2181 DIEBlock *Block = new DIEBlock();
2182 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
2183 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
2184 AddBlock(Member, DW_AT_data_member_location, 0, Block);
2185
2186 // Add accessibility (public default unless is base class.
2187 if (MemberDesc->isProtected()) {
2188 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
2189 } else if (MemberDesc->isPrivate()) {
2190 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
2191 } else if (Tag == DW_TAG_inheritance) {
2192 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
2193 }
aslc200b112008-08-16 12:57:46 +00002194
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002195 Buffer.AddChild(Member);
2196 } else if (GlobalVariableDesc *StaticDesc =
2197 dyn_cast<GlobalVariableDesc>(Element)) {
2198 // Add static member.
aslc200b112008-08-16 12:57:46 +00002199
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002200 // Construct member debug information entry.
2201 DIE *Static = new DIE(DW_TAG_variable);
aslc200b112008-08-16 12:57:46 +00002202
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002203 // Add name and mangled name.
2204 const std::string &Name = StaticDesc->getName();
2205 const std::string &LinkageName = StaticDesc->getLinkageName();
2206 AddString(Static, DW_AT_name, DW_FORM_string, Name);
2207 if (!LinkageName.empty()) {
2208 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
2209 LinkageName);
2210 }
aslc200b112008-08-16 12:57:46 +00002211
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002212 // Add location.
2213 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
aslc200b112008-08-16 12:57:46 +00002214
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002215 // Add type.
2216 if (TypeDesc *StaticTy = StaticDesc->getType())
2217 AddType(Static, StaticTy, Unit);
aslc200b112008-08-16 12:57:46 +00002218
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002219 // Add flags.
2220 if (!StaticDesc->isStatic())
2221 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
2222 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
aslc200b112008-08-16 12:57:46 +00002223
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002224 Buffer.AddChild(Static);
2225 } else if (SubprogramDesc *MethodDesc =
2226 dyn_cast<SubprogramDesc>(Element)) {
2227 // Add member function.
aslc200b112008-08-16 12:57:46 +00002228
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002229 // Construct member debug information entry.
2230 DIE *Method = new DIE(DW_TAG_subprogram);
aslc200b112008-08-16 12:57:46 +00002231
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002232 // Add name and mangled name.
2233 const std::string &Name = MethodDesc->getName();
2234 const std::string &LinkageName = MethodDesc->getLinkageName();
aslc200b112008-08-16 12:57:46 +00002235
2236 AddString(Method, DW_AT_name, DW_FORM_string, Name);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002237 bool IsCTor = TyDesc->getName() == Name;
aslc200b112008-08-16 12:57:46 +00002238
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002239 if (!LinkageName.empty()) {
2240 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
2241 LinkageName);
2242 }
aslc200b112008-08-16 12:57:46 +00002243
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002244 // Add location.
2245 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
aslc200b112008-08-16 12:57:46 +00002246
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002247 // Add type.
2248 if (CompositeTypeDesc *MethodTy =
2249 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
2250 // Get argument information.
2251 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
aslc200b112008-08-16 12:57:46 +00002252
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002253 // If not a ctor.
2254 if (!IsCTor) {
2255 // Add return type.
2256 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
2257 }
aslc200b112008-08-16 12:57:46 +00002258
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002259 // Add arguments.
Evan Chengc7efea32008-12-09 17:56:30 +00002260 for (unsigned i = 1, N = Args.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002261 DIE *Arg = new DIE(DW_TAG_formal_parameter);
2262 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
2263 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
2264 Method->AddChild(Arg);
2265 }
2266 }
2267
2268 // Add flags.
2269 if (!MethodDesc->isStatic())
2270 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
2271 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
aslc200b112008-08-16 12:57:46 +00002272
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002273 Buffer.AddChild(Method);
2274 }
2275 }
2276 break;
2277 }
2278 case DW_TAG_enumeration_type: {
2279 // Add enumerators to enumeration type.
Evan Chengc7efea32008-12-09 17:56:30 +00002280 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002281 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
2282 const std::string &Name = ED->getName();
2283 int64_t Value = ED->getValue();
2284 DIE *Enumerator = new DIE(DW_TAG_enumerator);
2285 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
2286 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
2287 Buffer.AddChild(Enumerator);
2288 }
2289
2290 break;
2291 }
2292 case DW_TAG_subroutine_type: {
2293 // Add prototype flag.
2294 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
2295 // Add return type.
2296 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
aslc200b112008-08-16 12:57:46 +00002297
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002298 // Add arguments.
Evan Chengc7efea32008-12-09 17:56:30 +00002299 for (unsigned i = 1, N = Elements.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002300 DIE *Arg = new DIE(DW_TAG_formal_parameter);
2301 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
2302 Buffer.AddChild(Arg);
2303 }
aslc200b112008-08-16 12:57:46 +00002304
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002305 break;
2306 }
2307 default: break;
2308 }
2309 }
aslc200b112008-08-16 12:57:46 +00002310
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002311 // Add name if not anonymous or intermediate type.
2312 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Evan Chengc7efea32008-12-09 17:56:30 +00002313
Evan Chengb2fc7112008-12-10 00:15:44 +00002314 // Add size if non-zero (derived types might be zero-sized.)
2315 if (Size)
2316 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
2317 else if (isa<CompositeTypeDesc>(TyDesc)) {
2318 // If TyDesc is a composite type, then add size even if it's zero unless
2319 // it's a forward declaration.
2320 if (TyDesc->isForwardDecl())
2321 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
2322 else
2323 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
2324 }
2325
2326 // Add source line info if available and TyDesc is not a forward
2327 // declaration.
2328 if (!TyDesc->isForwardDecl())
2329 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002330 }
2331
2332 /// NewCompileUnit - Create new compile unit and it's debug information entry.
2333 ///
2334 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
2335 // Construct debug information entry.
2336 DIE *Die = new DIE(DW_TAG_compile_unit);
Argiris Kirtzidis03449652008-06-18 19:27:37 +00002337 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2338 DWLabel("section_line", 0), DWLabel("section_line", 0), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002339 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
2340 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
2341 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
Devang Patel6bcf9822008-12-12 21:57:54 +00002342 if (!UnitDesc->getDirectory().empty())
2343 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
aslc200b112008-08-16 12:57:46 +00002344
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002345 // Construct compile unit.
2346 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
aslc200b112008-08-16 12:57:46 +00002347
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002348 // Add Unit to compile unit map.
2349 DescToUnitMap[UnitDesc] = Unit;
aslc200b112008-08-16 12:57:46 +00002350
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002351 return Unit;
2352 }
2353
2354 /// GetBaseCompileUnit - Get the main compile unit.
2355 ///
2356 CompileUnit *GetBaseCompileUnit() const {
2357 CompileUnit *Unit = CompileUnits[0];
2358 assert(Unit && "Missing compile unit.");
2359 return Unit;
2360 }
2361
2362 /// FindCompileUnit - Get the compile unit for the given descriptor.
2363 ///
2364 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
2365 CompileUnit *Unit = DescToUnitMap[UnitDesc];
2366 assert(Unit && "Missing compile unit.");
2367 return Unit;
2368 }
2369
Devang Patel5f244e32009-01-05 22:35:52 +00002370 /// FindCompileUnit - Get the compile unit for the given descriptor.
2371 ///
2372 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
2373 CompileUnit *DW_Unit = DW_CUs[Unit.getGV()];
2374 assert(DW_Unit && "Missing compile unit.");
2375 return DW_Unit;
2376 }
2377
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002378 /// NewGlobalVariable - Add a new global variable DIE.
2379 ///
2380 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
2381 // Get the compile unit context.
2382 CompileUnitDesc *UnitDesc =
2383 static_cast<CompileUnitDesc *>(GVD->getContext());
2384 CompileUnit *Unit = GetBaseCompileUnit();
2385
2386 // Check for pre-existence.
2387 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
2388 if (Slot) return Slot;
aslc200b112008-08-16 12:57:46 +00002389
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002390 // Get the global variable itself.
2391 GlobalVariable *GV = GVD->getGlobalVariable();
2392
2393 const std::string &Name = GVD->getName();
2394 const std::string &FullName = GVD->getFullName();
2395 const std::string &LinkageName = GVD->getLinkageName();
2396 // Create the global's variable DIE.
2397 DIE *VariableDie = new DIE(DW_TAG_variable);
2398 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
2399 if (!LinkageName.empty()) {
2400 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
2401 LinkageName);
2402 }
2403 AddType(VariableDie, GVD->getType(), Unit);
2404 if (!GVD->isStatic())
2405 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
aslc200b112008-08-16 12:57:46 +00002406
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002407 // Add source line info if available.
2408 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
aslc200b112008-08-16 12:57:46 +00002409
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002410 // Add address.
2411 DIEBlock *Block = new DIEBlock();
2412 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
2413 AddObjectLabel(Block, 0, DW_FORM_udata, Asm->getGlobalLinkName(GV));
2414 AddBlock(VariableDie, DW_AT_location, 0, Block);
aslc200b112008-08-16 12:57:46 +00002415
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002416 // Add to map.
2417 Slot = VariableDie;
aslc200b112008-08-16 12:57:46 +00002418
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002419 // Add to context owner.
2420 Unit->getDie()->AddChild(VariableDie);
aslc200b112008-08-16 12:57:46 +00002421
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002422 // Expose as global.
2423 // FIXME - need to check external flag.
2424 Unit->AddGlobal(FullName, VariableDie);
aslc200b112008-08-16 12:57:46 +00002425
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002426 return VariableDie;
2427 }
2428
2429 /// NewSubprogram - Add a new subprogram DIE.
2430 ///
2431 DIE *NewSubprogram(SubprogramDesc *SPD) {
2432 // Get the compile unit context.
2433 CompileUnitDesc *UnitDesc =
2434 static_cast<CompileUnitDesc *>(SPD->getContext());
2435 CompileUnit *Unit = GetBaseCompileUnit();
2436
2437 // Check for pre-existence.
2438 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
2439 if (Slot) return Slot;
aslc200b112008-08-16 12:57:46 +00002440
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002441 // Gather the details (simplify add attribute code.)
2442 const std::string &Name = SPD->getName();
2443 const std::string &FullName = SPD->getFullName();
2444 const std::string &LinkageName = SPD->getLinkageName();
aslc200b112008-08-16 12:57:46 +00002445
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002446 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
2447 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
2448 if (!LinkageName.empty()) {
2449 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
2450 LinkageName);
2451 }
2452 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
2453 if (!SPD->isStatic())
2454 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
2455 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
aslc200b112008-08-16 12:57:46 +00002456
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002457 // Add source line info if available.
2458 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
2459
2460 // Add to map.
2461 Slot = SubprogramDie;
aslc200b112008-08-16 12:57:46 +00002462
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002463 // Add to context owner.
2464 Unit->getDie()->AddChild(SubprogramDie);
aslc200b112008-08-16 12:57:46 +00002465
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002466 // Expose as global.
2467 Unit->AddGlobal(FullName, SubprogramDie);
aslc200b112008-08-16 12:57:46 +00002468
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002469 return SubprogramDie;
2470 }
2471
2472 /// NewScopeVariable - Create a new scope variable.
2473 ///
2474 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
2475 // Get the descriptor.
2476 VariableDesc *VD = DV->getDesc();
2477
2478 // Translate tag to proper Dwarf tag. The result variable is dropped for
2479 // now.
2480 unsigned Tag;
2481 switch (VD->getTag()) {
2482 case DW_TAG_return_variable: return NULL;
2483 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2484 case DW_TAG_auto_variable: // fall thru
2485 default: Tag = DW_TAG_variable; break;
2486 }
2487
2488 // Define variable debug information entry.
2489 DIE *VariableDie = new DIE(Tag);
2490 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
2491
2492 // Add source line info if available.
2493 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
aslc200b112008-08-16 12:57:46 +00002494
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002495 // Add variable type.
aslc200b112008-08-16 12:57:46 +00002496 AddType(VariableDie, VD->getType(), Unit);
2497
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002498 // Add variable address.
2499 MachineLocation Location;
Evan Cheng38948832008-01-31 03:37:28 +00002500 Location.set(RI->getFrameRegister(*MF),
2501 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002502 AddAddress(VariableDie, DW_AT_location, Location);
2503
2504 return VariableDie;
2505 }
2506
Devang Patel4d1709e2009-01-08 02:33:41 +00002507 /// NewScopeVariable - Create a new scope variable.
2508 ///
2509 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
2510 // Get the descriptor.
2511 DIVariable *VD = DV->getVariable();
2512
2513 // Translate tag to proper Dwarf tag. The result variable is dropped for
2514 // now.
2515 unsigned Tag;
2516 switch (VD->getTag()) {
2517 case DW_TAG_return_variable: return NULL;
2518 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2519 case DW_TAG_auto_variable: // fall thru
2520 default: Tag = DW_TAG_variable; break;
2521 }
2522
2523 // Define variable debug information entry.
2524 DIE *VariableDie = new DIE(Tag);
2525 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
2526
2527 // Add source line info if available.
2528 AddSourceLine(VariableDie, VD);
2529
2530 // Add variable type.
2531 AddType(Unit, VariableDie, VD->getType());
2532
2533 // Add variable address.
2534 MachineLocation Location;
2535 Location.set(RI->getFrameRegister(*MF),
2536 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
2537 AddAddress(VariableDie, DW_AT_location, Location);
2538
2539 return VariableDie;
2540 }
2541
2542
Devang Patel100ca322009-01-08 02:49:34 +00002543 /// RecordRegionStart - Indicate the start of a region.
2544 ///
2545 unsigned RecordRegionStart(GlobalVariable *V) {
2546 DbgScope *Scope = getOrCreateScope(V);
2547 unsigned ID = NextLabelID();
2548 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
2549 return ID;
2550 }
2551
2552 /// RecordRegionEnd - Indicate the end of a region.
2553 ///
2554 unsigned RecordRegionEnd(GlobalVariable *V) {
2555 DbgScope *Scope = getOrCreateScope(V);
2556 unsigned ID = NextLabelID();
2557 Scope->setEndLabelID(ID);
2558 return ID;
2559 }
2560
2561 /// RecordVariable - Indicate the declaration of a local variable.
2562 ///
2563 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
2564 DbgScope *Scope = getOrCreateScope(GV);
2565 DIVariable *VD = new DIVariable(GV);
2566 DbgVariable *DV = new DbgVariable(VD, FrameIndex);
2567 Scope->AddVariable(DV);
2568 }
2569
Devang Patel4d1709e2009-01-08 02:33:41 +00002570 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2571 ///
2572 DbgScope *getOrCreateScope(GlobalVariable *V) {
2573 DbgScope *&Slot = DbgScopeMap[V];
2574 if (!Slot) {
2575 // FIXME - breaks down when the context is an inlined function.
2576 DIDescriptor ParentDesc;
2577 DIBlock *DB = new DIBlock(V);
2578 if (DIBlock *Block = dyn_cast<DIBlock>(DB)) {
2579 ParentDesc = Block->getContext();
2580 }
2581 DbgScope *Parent = ParentDesc.isNull() ?
2582 getOrCreateScope(ParentDesc.getGV()) : NULL;
2583 Slot = new DbgScope(Parent, DB);
2584 if (Parent) {
2585 Parent->AddScope(Slot);
2586 } else if (RootDbgScope) {
2587 // FIXME - Add inlined function scopes to the root so we can delete
2588 // them later. Long term, handle inlined functions properly.
2589 RootDbgScope->AddScope(Slot);
2590 } else {
2591 // First function is top level function.
2592 RootDbgScope = Slot;
2593 }
2594 }
2595 return Slot;
2596 }
2597
2598 /// ConstructDbgScope - Construct the components of a scope.
2599 ///
2600 void ConstructDbgScope(DbgScope *ParentScope,
2601 unsigned ParentStartID, unsigned ParentEndID,
2602 DIE *ParentDie, CompileUnit *Unit) {
2603 // Add variables to scope.
2604 SmallVector<DbgVariable *, 32> &Variables = ParentScope->getVariables();
2605 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2606 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2607 if (VariableDie) ParentDie->AddChild(VariableDie);
2608 }
2609
2610 // Add nested scopes.
2611 SmallVector<DbgScope *, 8> &Scopes = ParentScope->getScopes();
2612 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2613 // Define the Scope debug information entry.
2614 DbgScope *Scope = Scopes[j];
2615 // FIXME - Ignore inlined functions for the time being.
2616 if (!Scope->getParent()) continue;
2617
2618 unsigned StartID = MappedLabel(Scope->getStartLabelID());
2619 unsigned EndID = MappedLabel(Scope->getEndLabelID());
2620
2621 // Ignore empty scopes.
2622 if (StartID == EndID && StartID != 0) continue;
2623 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
2624
2625 if (StartID == ParentStartID && EndID == ParentEndID) {
2626 // Just add stuff to the parent scope.
2627 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2628 } else {
2629 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2630
2631 // Add the scope bounds.
2632 if (StartID) {
2633 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2634 DWLabel("label", StartID));
2635 } else {
2636 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2637 DWLabel("func_begin", SubprogramCount));
2638 }
2639 if (EndID) {
2640 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2641 DWLabel("label", EndID));
2642 } else {
2643 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2644 DWLabel("func_end", SubprogramCount));
2645 }
2646
2647 // Add the scope contents.
2648 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2649 ParentDie->AddChild(ScopeDie);
2650 }
2651 }
2652 }
2653
2654 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2655 ///
2656 void ConstructRootDbgScope(DbgScope *RootScope) {
2657 // Exit if there is no root scope.
2658 if (!RootScope) return;
2659
2660 // Get the subprogram debug information entry.
2661 DISubprogram *SPD = cast<DISubprogram>(RootScope->getDesc());
2662
2663 // Get the compile unit context.
2664 CompileUnit *Unit = FindCompileUnit(SPD->getCompileUnit());
2665
2666 // Get the subprogram die.
2667 DIE *SPDie = Unit->getDieMapSlotFor(SPD->getGV());
2668 assert(SPDie && "Missing subprogram descriptor");
2669
2670 // Add the function bounds.
2671 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2672 DWLabel("func_begin", SubprogramCount));
2673 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2674 DWLabel("func_end", SubprogramCount));
2675 MachineLocation Location(RI->getFrameRegister(*MF));
2676 AddAddress(SPDie, DW_AT_frame_base, Location);
2677
2678 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2679 }
2680
2681 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2682 ///
2683 void ConstructDefaultDbgScope(MachineFunction *MF) {
2684 // Find the correct subprogram descriptor.
2685 std::string SPName = "llvm.dbg.subprograms";
2686 std::vector<GlobalVariable*> Result;
2687 getGlobalVariablesUsing(*M, SPName, Result);
2688 for (std::vector<GlobalVariable *>::iterator I = Result.begin(),
2689 E = Result.end(); I != E; ++I) {
2690
2691 DISubprogram *SPD = new DISubprogram(*I);
2692
2693 if (SPD->getName() == MF->getFunction()->getName()) {
2694 // Get the compile unit context.
2695 CompileUnit *Unit = FindCompileUnit(SPD->getCompileUnit());
2696
2697 // Get the subprogram die.
2698 DIE *SPDie = Unit->getDieMapSlotFor(SPD->getGV());
2699 assert(SPDie && "Missing subprogram descriptor");
2700
2701 // Add the function bounds.
2702 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2703 DWLabel("func_begin", SubprogramCount));
2704 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2705 DWLabel("func_end", SubprogramCount));
2706
2707 MachineLocation Location(RI->getFrameRegister(*MF));
2708 AddAddress(SPDie, DW_AT_frame_base, Location);
2709 return;
2710 }
2711 }
2712#if 0
2713 // FIXME: This is causing an abort because C++ mangled names are compared
2714 // with their unmangled counterparts. See PR2885. Don't do this assert.
2715 assert(0 && "Couldn't find DIE for machine function!");
2716#endif
2717 }
2718
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002719 /// ConstructScope - Construct the components of a scope.
2720 ///
2721 void ConstructScope(DebugScope *ParentScope,
2722 unsigned ParentStartID, unsigned ParentEndID,
2723 DIE *ParentDie, CompileUnit *Unit) {
2724 // Add variables to scope.
2725 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
2726 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2727 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
2728 if (VariableDie) ParentDie->AddChild(VariableDie);
2729 }
aslc200b112008-08-16 12:57:46 +00002730
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002731 // Add nested scopes.
2732 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
2733 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2734 // Define the Scope debug information entry.
2735 DebugScope *Scope = Scopes[j];
2736 // FIXME - Ignore inlined functions for the time being.
2737 if (!Scope->getParent()) continue;
aslc200b112008-08-16 12:57:46 +00002738
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002739 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2740 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
2741
2742 // Ignore empty scopes.
2743 if (StartID == EndID && StartID != 0) continue;
2744 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
aslc200b112008-08-16 12:57:46 +00002745
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002746 if (StartID == ParentStartID && EndID == ParentEndID) {
2747 // Just add stuff to the parent scope.
2748 ConstructScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2749 } else {
2750 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
aslc200b112008-08-16 12:57:46 +00002751
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002752 // Add the scope bounds.
2753 if (StartID) {
2754 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2755 DWLabel("label", StartID));
2756 } else {
2757 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2758 DWLabel("func_begin", SubprogramCount));
2759 }
2760 if (EndID) {
2761 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2762 DWLabel("label", EndID));
2763 } else {
2764 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2765 DWLabel("func_end", SubprogramCount));
2766 }
aslc200b112008-08-16 12:57:46 +00002767
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002768 // Add the scope contents.
2769 ConstructScope(Scope, StartID, EndID, ScopeDie, Unit);
2770 ParentDie->AddChild(ScopeDie);
2771 }
2772 }
2773 }
2774
2775 /// ConstructRootScope - Construct the scope for the subprogram.
2776 ///
2777 void ConstructRootScope(DebugScope *RootScope) {
2778 // Exit if there is no root scope.
2779 if (!RootScope) return;
aslc200b112008-08-16 12:57:46 +00002780
2781 // Get the subprogram debug information entry.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002782 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
aslc200b112008-08-16 12:57:46 +00002783
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002784 // Get the compile unit context.
2785 CompileUnit *Unit = GetBaseCompileUnit();
aslc200b112008-08-16 12:57:46 +00002786
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002787 // Get the subprogram die.
2788 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2789 assert(SPDie && "Missing subprogram descriptor");
aslc200b112008-08-16 12:57:46 +00002790
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002791 // Add the function bounds.
2792 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2793 DWLabel("func_begin", SubprogramCount));
2794 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2795 DWLabel("func_end", SubprogramCount));
2796 MachineLocation Location(RI->getFrameRegister(*MF));
2797 AddAddress(SPDie, DW_AT_frame_base, Location);
2798
2799 ConstructScope(RootScope, 0, 0, SPDie, Unit);
2800 }
2801
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002802 /// ConstructDefaultScope - Construct a default scope for the subprogram.
2803 ///
2804 void ConstructDefaultScope(MachineFunction *MF) {
2805 // Find the correct subprogram descriptor.
2806 std::vector<SubprogramDesc *> Subprograms;
2807 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
2808
2809 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2810 SubprogramDesc *SPD = Subprograms[i];
2811
2812 if (SPD->getName() == MF->getFunction()->getName()) {
2813 // Get the compile unit context.
2814 CompileUnit *Unit = GetBaseCompileUnit();
2815
2816 // Get the subprogram die.
2817 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2818 assert(SPDie && "Missing subprogram descriptor");
2819
2820 // Add the function bounds.
2821 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2822 DWLabel("func_begin", SubprogramCount));
2823 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2824 DWLabel("func_end", SubprogramCount));
2825
2826 MachineLocation Location(RI->getFrameRegister(*MF));
2827 AddAddress(SPDie, DW_AT_frame_base, Location);
2828 return;
2829 }
2830 }
Bill Wendlingae6b98c2008-10-17 18:48:57 +00002831#if 0
2832 // FIXME: This is causing an abort because C++ mangled names are compared
2833 // with their unmangled counterparts. See PR2885. Don't do this assert.
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002834 assert(0 && "Couldn't find DIE for machine function!");
Bill Wendlingae6b98c2008-10-17 18:48:57 +00002835#endif
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002836 }
2837
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002838 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2839 /// tools to recognize the object file contains Dwarf information.
2840 void EmitInitial() {
2841 // Check to see if we already emitted intial headers.
2842 if (didInitial) return;
2843 didInitial = true;
aslc200b112008-08-16 12:57:46 +00002844
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002845 // Dwarf sections base addresses.
2846 if (TAI->doesDwarfRequireFrameSection()) {
2847 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2848 EmitLabel("section_debug_frame", 0);
2849 }
2850 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2851 EmitLabel("section_info", 0);
2852 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2853 EmitLabel("section_abbrev", 0);
2854 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2855 EmitLabel("section_aranges", 0);
2856 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2857 EmitLabel("section_macinfo", 0);
2858 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2859 EmitLabel("section_line", 0);
2860 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2861 EmitLabel("section_loc", 0);
2862 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2863 EmitLabel("section_pubnames", 0);
2864 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2865 EmitLabel("section_str", 0);
2866 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2867 EmitLabel("section_ranges", 0);
2868
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002869 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002870 EmitLabel("text_begin", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00002871 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002872 EmitLabel("data_begin", 0);
2873 }
2874
2875 /// EmitDIE - Recusively Emits a debug information entry.
2876 ///
2877 void EmitDIE(DIE *Die) {
2878 // Get the abbreviation for this DIE.
2879 unsigned AbbrevNumber = Die->getAbbrevNumber();
2880 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
aslc200b112008-08-16 12:57:46 +00002881
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002882 Asm->EOL();
2883
2884 // Emit the code (index) for the abbreviation.
2885 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng0eeed442008-07-01 23:18:29 +00002886
2887 if (VerboseAsm)
2888 Asm->EOL(std::string("Abbrev [" +
2889 utostr(AbbrevNumber) +
2890 "] 0x" + utohexstr(Die->getOffset()) +
2891 ":0x" + utohexstr(Die->getSize()) + " " +
2892 TagString(Abbrev->getTag())));
2893 else
2894 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00002895
Owen Anderson88dd6232008-06-24 21:44:59 +00002896 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2897 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
aslc200b112008-08-16 12:57:46 +00002898
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002899 // Emit the DIE attribute values.
2900 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2901 unsigned Attr = AbbrevData[i].getAttribute();
2902 unsigned Form = AbbrevData[i].getForm();
2903 assert(Form && "Too many attributes for DIE (check abbreviation)");
aslc200b112008-08-16 12:57:46 +00002904
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002905 switch (Attr) {
2906 case DW_AT_sibling: {
2907 Asm->EmitInt32(Die->SiblingOffset());
2908 break;
2909 }
2910 default: {
2911 // Emit an attribute using the defined form.
2912 Values[i]->EmitValue(*this, Form);
2913 break;
2914 }
2915 }
aslc200b112008-08-16 12:57:46 +00002916
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002917 Asm->EOL(AttributeString(Attr));
2918 }
aslc200b112008-08-16 12:57:46 +00002919
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002920 // Emit the DIE children if any.
2921 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2922 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002923
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002924 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2925 EmitDIE(Children[j]);
2926 }
aslc200b112008-08-16 12:57:46 +00002927
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002928 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2929 }
2930 }
2931
2932 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2933 ///
2934 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2935 // Get the children.
2936 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002937
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002938 // If not last sibling and has children then add sibling offset attribute.
2939 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2940
2941 // Record the abbreviation.
2942 AssignAbbrevNumber(Die->getAbbrev());
aslc200b112008-08-16 12:57:46 +00002943
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002944 // Get the abbreviation for this DIE.
2945 unsigned AbbrevNumber = Die->getAbbrevNumber();
2946 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2947
2948 // Set DIE offset
2949 Die->setOffset(Offset);
aslc200b112008-08-16 12:57:46 +00002950
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002951 // Start the size with the size of abbreviation code.
aslc200b112008-08-16 12:57:46 +00002952 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2953
Owen Anderson88dd6232008-06-24 21:44:59 +00002954 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2955 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002956
2957 // Size the DIE attribute values.
2958 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2959 // Size attribute value.
2960 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2961 }
aslc200b112008-08-16 12:57:46 +00002962
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002963 // Size the DIE children if any.
2964 if (!Children.empty()) {
2965 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2966 "Children flag not set");
aslc200b112008-08-16 12:57:46 +00002967
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002968 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2969 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2970 }
aslc200b112008-08-16 12:57:46 +00002971
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002972 // End of children marker.
2973 Offset += sizeof(int8_t);
2974 }
2975
2976 Die->setSize(Offset - Die->getOffset());
2977 return Offset;
2978 }
2979
2980 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2981 ///
2982 void SizeAndOffsets() {
2983 // Process base compile unit.
2984 CompileUnit *Unit = GetBaseCompileUnit();
2985 // Compute size of compile unit header
2986 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2987 sizeof(int16_t) + // DWARF version number
2988 sizeof(int32_t) + // Offset Into Abbrev. Section
2989 sizeof(int8_t); // Pointer Size (in bytes)
2990 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2991 }
2992
2993 /// EmitDebugInfo - Emit the debug info section.
2994 ///
2995 void EmitDebugInfo() {
2996 // Start debug info section.
2997 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
aslc200b112008-08-16 12:57:46 +00002998
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002999 CompileUnit *Unit = GetBaseCompileUnit();
3000 DIE *Die = Unit->getDie();
3001 // Emit the compile units header.
3002 EmitLabel("info_begin", Unit->getID());
3003 // Emit size of content not including length itself
3004 unsigned ContentSize = Die->getSize() +
3005 sizeof(int16_t) + // DWARF version number
3006 sizeof(int32_t) + // Offset Into Abbrev. Section
3007 sizeof(int8_t) + // Pointer Size (in bytes)
3008 sizeof(int32_t); // FIXME - extra pad for gdb bug.
aslc200b112008-08-16 12:57:46 +00003009
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003010 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
3011 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
3012 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
3013 Asm->EOL("Offset Into Abbrev. Section");
Dan Gohmancfb72b22007-09-27 23:12:31 +00003014 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
aslc200b112008-08-16 12:57:46 +00003015
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003016 EmitDIE(Die);
3017 // FIXME - extra padding for gdb bug.
3018 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
3019 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
3020 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
3021 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
3022 EmitLabel("info_end", Unit->getID());
aslc200b112008-08-16 12:57:46 +00003023
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003024 Asm->EOL();
3025 }
3026
3027 /// EmitAbbreviations - Emit the abbreviation section.
3028 ///
3029 void EmitAbbreviations() const {
3030 // Check to see if it is worth the effort.
3031 if (!Abbreviations.empty()) {
3032 // Start the debug abbrev section.
3033 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
aslc200b112008-08-16 12:57:46 +00003034
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003035 EmitLabel("abbrev_begin", 0);
aslc200b112008-08-16 12:57:46 +00003036
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003037 // For each abbrevation.
3038 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
3039 // Get abbreviation data
3040 const DIEAbbrev *Abbrev = Abbreviations[i];
aslc200b112008-08-16 12:57:46 +00003041
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003042 // Emit the abbrevations code (base 1 index.)
3043 Asm->EmitULEB128Bytes(Abbrev->getNumber());
3044 Asm->EOL("Abbreviation Code");
aslc200b112008-08-16 12:57:46 +00003045
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003046 // Emit the abbreviations data.
3047 Abbrev->Emit(*this);
aslc200b112008-08-16 12:57:46 +00003048
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003049 Asm->EOL();
3050 }
aslc200b112008-08-16 12:57:46 +00003051
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003052 // Mark end of abbreviations.
3053 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
3054
3055 EmitLabel("abbrev_end", 0);
aslc200b112008-08-16 12:57:46 +00003056
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003057 Asm->EOL();
3058 }
3059 }
3060
Bill Wendling1983a2a2008-07-20 00:11:19 +00003061 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
3062 /// the line matrix.
aslc200b112008-08-16 12:57:46 +00003063 ///
Bill Wendling1983a2a2008-07-20 00:11:19 +00003064 void EmitEndOfLineMatrix(unsigned SectionEnd) {
3065 // Define last address of section.
3066 Asm->EmitInt8(0); Asm->EOL("Extended Op");
3067 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
3068 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
3069 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
3070
3071 // Mark end of matrix.
3072 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
3073 Asm->EmitULEB128Bytes(1); Asm->EOL();
3074 Asm->EmitInt8(1); Asm->EOL();
3075 }
3076
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003077 /// EmitDebugLines - Emit source line information.
3078 ///
3079 void EmitDebugLines() {
Bill Wendling1983a2a2008-07-20 00:11:19 +00003080 // If the target is using .loc/.file, the assembler will be emitting the
3081 // .debug_line table automatically.
3082 if (TAI->hasDotLocAndDotFile())
Dan Gohmanc55b34a2007-09-24 21:43:52 +00003083 return;
3084
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003085 // Minimum line delta, thus ranging from -10..(255-10).
3086 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
3087 // Maximum line delta, thus ranging from -10..(255-10).
3088 const int MaxLineDelta = 255 + MinLineDelta;
3089
3090 // Start the dwarf line section.
3091 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
aslc200b112008-08-16 12:57:46 +00003092
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003093 // Construct the section header.
aslc200b112008-08-16 12:57:46 +00003094
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003095 EmitDifference("line_end", 0, "line_begin", 0, true);
3096 Asm->EOL("Length of Source Line Info");
3097 EmitLabel("line_begin", 0);
aslc200b112008-08-16 12:57:46 +00003098
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003099 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
aslc200b112008-08-16 12:57:46 +00003100
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003101 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
3102 Asm->EOL("Prolog Length");
3103 EmitLabel("line_prolog_begin", 0);
aslc200b112008-08-16 12:57:46 +00003104
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003105 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
3106
3107 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
3108
3109 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
aslc200b112008-08-16 12:57:46 +00003110
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003111 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
3112
3113 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
aslc200b112008-08-16 12:57:46 +00003114
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003115 // Line number standard opcode encodings argument count
3116 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
3117 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
3118 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
3119 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
3120 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
3121 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
3122 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
3123 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
3124 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
3125
3126 const UniqueVector<std::string> &Directories = MMI->getDirectories();
Evan Cheng0eeed442008-07-01 23:18:29 +00003127 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003128
3129 // Emit directories.
3130 for (unsigned DirectoryID = 1, NDID = Directories.size();
3131 DirectoryID <= NDID; ++DirectoryID) {
3132 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
3133 }
3134 Asm->EmitInt8(0); Asm->EOL("End of directories");
aslc200b112008-08-16 12:57:46 +00003135
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003136 // Emit files.
3137 for (unsigned SourceID = 1, NSID = SourceFiles.size();
3138 SourceID <= NSID; ++SourceID) {
3139 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
3140 Asm->EmitString(SourceFile.getName());
3141 Asm->EOL("Source");
3142 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
3143 Asm->EOL("Directory #");
3144 Asm->EmitULEB128Bytes(0);
3145 Asm->EOL("Mod date");
3146 Asm->EmitULEB128Bytes(0);
3147 Asm->EOL("File size");
3148 }
3149 Asm->EmitInt8(0); Asm->EOL("End of files");
aslc200b112008-08-16 12:57:46 +00003150
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003151 EmitLabel("line_prolog_end", 0);
aslc200b112008-08-16 12:57:46 +00003152
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003153 // A sequence for each text section.
Bill Wendling1983a2a2008-07-20 00:11:19 +00003154 unsigned SecSrcLinesSize = SectionSourceLines.size();
3155
3156 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003157 // Isolate current sections line info.
3158 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng0eeed442008-07-01 23:18:29 +00003159
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003160 if (VerboseAsm) {
3161 const Section* S = SectionMap[j + 1];
3162 Asm->EOL(std::string("Section ") + S->getName());
3163 } else
Evan Cheng0eeed442008-07-01 23:18:29 +00003164 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003165
3166 // Dwarf assumes we start with first line of first source file.
3167 unsigned Source = 1;
3168 unsigned Line = 1;
aslc200b112008-08-16 12:57:46 +00003169
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003170 // Construct rows of the address, source, line, column matrix.
3171 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
3172 const SourceLineInfo &LineInfo = LineInfos[i];
3173 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
3174 if (!LabelID) continue;
aslc200b112008-08-16 12:57:46 +00003175
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003176 unsigned SourceID = LineInfo.getSourceID();
3177 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
3178 unsigned DirectoryID = SourceFile.getDirectoryID();
Evan Cheng0eeed442008-07-01 23:18:29 +00003179 if (VerboseAsm)
3180 Asm->EOL(Directories[DirectoryID]
3181 + SourceFile.getName()
3182 + ":"
3183 + utostr_32(LineInfo.getLine()));
3184 else
3185 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003186
3187 // Define the line address.
3188 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohmancfb72b22007-09-27 23:12:31 +00003189 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003190 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
3191 EmitReference("label", LabelID); Asm->EOL("Location label");
aslc200b112008-08-16 12:57:46 +00003192
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003193 // If change of source, then switch to the new source.
3194 if (Source != LineInfo.getSourceID()) {
3195 Source = LineInfo.getSourceID();
3196 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
3197 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
3198 }
aslc200b112008-08-16 12:57:46 +00003199
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003200 // If change of line.
3201 if (Line != LineInfo.getLine()) {
3202 // Determine offset.
3203 int Offset = LineInfo.getLine() - Line;
3204 int Delta = Offset - MinLineDelta;
aslc200b112008-08-16 12:57:46 +00003205
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003206 // Update line.
3207 Line = LineInfo.getLine();
aslc200b112008-08-16 12:57:46 +00003208
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003209 // If delta is small enough and in range...
3210 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
3211 // ... then use fast opcode.
3212 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
3213 } else {
3214 // ... otherwise use long hand.
3215 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
3216 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
3217 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
3218 }
3219 } else {
3220 // Copy the previous row (different address or source)
3221 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
3222 }
3223 }
3224
Bill Wendling1983a2a2008-07-20 00:11:19 +00003225 EmitEndOfLineMatrix(j + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003226 }
Bill Wendling1983a2a2008-07-20 00:11:19 +00003227
3228 if (SecSrcLinesSize == 0)
3229 // Because we're emitting a debug_line section, we still need a line
3230 // table. The linker and friends expect it to exist. If there's nothing to
3231 // put into it, emit an empty table.
3232 EmitEndOfLineMatrix(1);
aslc200b112008-08-16 12:57:46 +00003233
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003234 EmitLabel("line_end", 0);
aslc200b112008-08-16 12:57:46 +00003235
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003236 Asm->EOL();
3237 }
aslc200b112008-08-16 12:57:46 +00003238
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003239 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
3240 ///
3241 void EmitCommonDebugFrame() {
3242 if (!TAI->doesDwarfRequireFrameSection())
3243 return;
3244
3245 int stackGrowth =
3246 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3247 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00003248 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003249
3250 // Start the dwarf frame section.
3251 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
3252
3253 EmitLabel("debug_frame_common", 0);
3254 EmitDifference("debug_frame_common_end", 0,
3255 "debug_frame_common_begin", 0, true);
3256 Asm->EOL("Length of Common Information Entry");
3257
3258 EmitLabel("debug_frame_common_begin", 0);
3259 Asm->EmitInt32((int)DW_CIE_ID);
3260 Asm->EOL("CIE Identifier Tag");
3261 Asm->EmitInt8(DW_CIE_VERSION);
3262 Asm->EOL("CIE Version");
3263 Asm->EmitString("");
3264 Asm->EOL("CIE Augmentation");
3265 Asm->EmitULEB128Bytes(1);
3266 Asm->EOL("CIE Code Alignment Factor");
3267 Asm->EmitSLEB128Bytes(stackGrowth);
aslc200b112008-08-16 12:57:46 +00003268 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00003269 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003270 Asm->EOL("CIE RA Column");
aslc200b112008-08-16 12:57:46 +00003271
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003272 std::vector<MachineMove> Moves;
3273 RI->getInitialFrameState(Moves);
3274
Dale Johannesenf5a11532007-11-13 19:13:01 +00003275 EmitFrameMoves(NULL, 0, Moves, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003276
Evan Cheng7e7d1942008-02-29 19:36:59 +00003277 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003278 EmitLabel("debug_frame_common_end", 0);
aslc200b112008-08-16 12:57:46 +00003279
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003280 Asm->EOL();
3281 }
3282
3283 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
3284 /// section.
3285 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
3286 if (!TAI->doesDwarfRequireFrameSection())
3287 return;
aslc200b112008-08-16 12:57:46 +00003288
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003289 // Start the dwarf frame section.
3290 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
aslc200b112008-08-16 12:57:46 +00003291
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003292 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
3293 "debug_frame_begin", DebugFrameInfo.Number, true);
3294 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00003295
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003296 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
3297
3298 EmitSectionOffset("debug_frame_common", "section_debug_frame",
3299 0, 0, true, false);
3300 Asm->EOL("FDE CIE offset");
3301
3302 EmitReference("func_begin", DebugFrameInfo.Number);
3303 Asm->EOL("FDE initial location");
3304 EmitDifference("func_end", DebugFrameInfo.Number,
3305 "func_begin", DebugFrameInfo.Number);
3306 Asm->EOL("FDE address range");
aslc200b112008-08-16 12:57:46 +00003307
Dale Johannesenf5a11532007-11-13 19:13:01 +00003308 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves, false);
aslc200b112008-08-16 12:57:46 +00003309
Evan Cheng7e7d1942008-02-29 19:36:59 +00003310 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003311 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
3312
3313 Asm->EOL();
3314 }
3315
3316 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
3317 ///
3318 void EmitDebugPubNames() {
3319 // Start the dwarf pubnames section.
3320 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
aslc200b112008-08-16 12:57:46 +00003321
3322 CompileUnit *Unit = GetBaseCompileUnit();
3323
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003324 EmitDifference("pubnames_end", Unit->getID(),
3325 "pubnames_begin", Unit->getID(), true);
3326 Asm->EOL("Length of Public Names Info");
aslc200b112008-08-16 12:57:46 +00003327
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003328 EmitLabel("pubnames_begin", Unit->getID());
aslc200b112008-08-16 12:57:46 +00003329
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003330 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
3331
3332 EmitSectionOffset("info_begin", "section_info",
3333 Unit->getID(), 0, true, false);
3334 Asm->EOL("Offset of Compilation Unit Info");
3335
3336 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
3337 Asm->EOL("Compilation Unit Length");
aslc200b112008-08-16 12:57:46 +00003338
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003339 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
aslc200b112008-08-16 12:57:46 +00003340
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003341 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
3342 GE = Globals.end();
3343 GI != GE; ++GI) {
3344 const std::string &Name = GI->first;
3345 DIE * Entity = GI->second;
aslc200b112008-08-16 12:57:46 +00003346
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003347 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
3348 Asm->EmitString(Name); Asm->EOL("External Name");
3349 }
aslc200b112008-08-16 12:57:46 +00003350
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003351 Asm->EmitInt32(0); Asm->EOL("End Mark");
3352 EmitLabel("pubnames_end", Unit->getID());
aslc200b112008-08-16 12:57:46 +00003353
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003354 Asm->EOL();
3355 }
3356
3357 /// EmitDebugStr - Emit visible names into a debug str section.
3358 ///
3359 void EmitDebugStr() {
3360 // Check to see if it is worth the effort.
3361 if (!StringPool.empty()) {
3362 // Start the dwarf str section.
3363 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
aslc200b112008-08-16 12:57:46 +00003364
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003365 // For each of strings in the string pool.
3366 for (unsigned StringID = 1, N = StringPool.size();
3367 StringID <= N; ++StringID) {
3368 // Emit a label for reference from debug information entries.
3369 EmitLabel("string", StringID);
3370 // Emit the string itself.
3371 const std::string &String = StringPool[StringID];
3372 Asm->EmitString(String); Asm->EOL();
3373 }
aslc200b112008-08-16 12:57:46 +00003374
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003375 Asm->EOL();
3376 }
3377 }
3378
3379 /// EmitDebugLoc - Emit visible names into a debug loc section.
3380 ///
3381 void EmitDebugLoc() {
3382 // Start the dwarf loc section.
3383 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
aslc200b112008-08-16 12:57:46 +00003384
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003385 Asm->EOL();
3386 }
3387
3388 /// EmitDebugARanges - Emit visible names into a debug aranges section.
3389 ///
3390 void EmitDebugARanges() {
3391 // Start the dwarf aranges section.
3392 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
aslc200b112008-08-16 12:57:46 +00003393
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003394 // FIXME - Mock up
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003395#if 0
aslc200b112008-08-16 12:57:46 +00003396 CompileUnit *Unit = GetBaseCompileUnit();
3397
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003398 // Don't include size of length
3399 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
aslc200b112008-08-16 12:57:46 +00003400
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003401 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
aslc200b112008-08-16 12:57:46 +00003402
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003403 EmitReference("info_begin", Unit->getID());
3404 Asm->EOL("Offset of Compilation Unit Info");
3405
Dan Gohmancfb72b22007-09-27 23:12:31 +00003406 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003407
3408 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
3409
3410 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
3411 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
3412
3413 // Range 1
3414 EmitReference("text_begin", 0); Asm->EOL("Address");
3415 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
3416
3417 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
3418 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003419#endif
aslc200b112008-08-16 12:57:46 +00003420
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003421 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003422 }
3423
3424 /// EmitDebugRanges - Emit visible names into a debug ranges section.
3425 ///
3426 void EmitDebugRanges() {
3427 // Start the dwarf ranges section.
3428 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
aslc200b112008-08-16 12:57:46 +00003429
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003430 Asm->EOL();
3431 }
3432
3433 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
3434 ///
3435 void EmitDebugMacInfo() {
3436 // Start the dwarf macinfo section.
3437 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
aslc200b112008-08-16 12:57:46 +00003438
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003439 Asm->EOL();
3440 }
3441
Devang Patel289f2362009-01-05 23:11:11 +00003442 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Patelb3907da2009-01-05 23:03:32 +00003443 void ConstructCompileUnits() {
3444 std::string CUName = "llvm.dbg.compile_units";
3445 std::vector<GlobalVariable*> Result;
3446 getGlobalVariablesUsing(*M, CUName, Result);
3447 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
3448 RE = Result.end(); RI != RE; ++RI) {
3449 DICompileUnit *DIUnit = new DICompileUnit(*RI);
3450 unsigned DID = Directories.insert(DIUnit->getDirectory());
3451 unsigned ID = SrcFiles.insert(SrcFileInfo(DID,
3452 DIUnit->getFilename()));
3453
3454 DIE *Die = new DIE(DW_TAG_compile_unit);
3455 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
3456 DWLabel("section_line", 0), DWLabel("section_line", 0),
3457 false);
3458 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit->getProducer());
3459 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit->getLanguage());
3460 AddString(Die, DW_AT_name, DW_FORM_string, DIUnit->getFilename());
3461 if (!DIUnit->getDirectory().empty())
3462 AddString(Die, DW_AT_comp_dir, DW_FORM_string, DIUnit->getDirectory());
3463
3464 CompileUnit *Unit = new CompileUnit(ID, Die);
3465 DW_CUs[DIUnit->getGV()] = Unit;
3466 }
3467 }
3468
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003469 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
3470 /// header file.
3471 void ConstructCompileUnitDIEs() {
3472 const UniqueVector<CompileUnitDesc *> CUW = MMI->getCompileUnits();
aslc200b112008-08-16 12:57:46 +00003473
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003474 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
3475 unsigned ID = MMI->RecordSource(CUW[i]);
3476 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
3477 CompileUnits.push_back(Unit);
3478 }
3479 }
3480
Devang Patel289f2362009-01-05 23:11:11 +00003481 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
3482 /// visible global variables.
3483 void ConstructGlobalVariableDIEs() {
3484 std::string GVName = "llvm.dbg.global_variables";
3485 std::vector<GlobalVariable*> Result;
3486 getGlobalVariablesUsing(*M, GVName, Result);
3487 for (std::vector<GlobalVariable *>::iterator GVI = Result.begin(),
3488 GVE = Result.end(); GVI != GVE; ++GVI) {
3489 DIGlobalVariable *DI_GV = new DIGlobalVariable(*GVI);
3490 CompileUnit *DW_Unit = FindCompileUnit(DI_GV->getCompileUnit());
3491
3492 // Check for pre-existence.
3493 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV->getGV());
3494 if (Slot) continue;
3495
3496 DIE *VariableDie = new DIE(DW_TAG_variable);
3497 AddString(VariableDie, DW_AT_name, DW_FORM_string, DI_GV->getName());
3498 const std::string &LinkageName = DI_GV->getLinkageName();
3499 if (!LinkageName.empty())
3500 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
3501 LinkageName);
3502 AddType(DW_Unit, VariableDie, DI_GV->getType());
3503
3504 if (!DI_GV->isLocalToUnit())
3505 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
3506
3507 // Add source line info, if available.
3508 AddSourceLine(VariableDie, DI_GV);
3509
3510 // Add address.
3511 DIEBlock *Block = new DIEBlock();
3512 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
3513 AddObjectLabel(Block, 0, DW_FORM_udata,
3514 Asm->getGlobalLinkName(DI_GV->getGV()));
3515 AddBlock(VariableDie, DW_AT_location, 0, Block);
3516
3517 //Add to map.
3518 Slot = VariableDie;
3519
3520 //Add to context owner.
3521 DW_Unit->getDie()->AddChild(VariableDie);
3522
3523 //Expose as global. FIXME - need to check external flag.
3524 DW_Unit->AddGlobal(DI_GV->getName(), VariableDie);
3525 }
3526 }
3527
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003528 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
3529 /// global variables.
3530 void ConstructGlobalDIEs() {
Bill Wendling75091f92008-07-03 23:13:02 +00003531 std::vector<GlobalVariableDesc *> GlobalVariables;
3532 MMI->getAnchoredDescriptors<GlobalVariableDesc>(*M, GlobalVariables);
aslc200b112008-08-16 12:57:46 +00003533
Bill Wendling4de8de52008-07-03 22:53:42 +00003534 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
3535 GlobalVariableDesc *GVD = GlobalVariables[i];
3536 NewGlobalVariable(GVD);
3537 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003538 }
3539
Devang Patele6caf012009-01-05 23:21:35 +00003540 /// ConstructSubprograms - Create DIEs for each of the externally visible
3541 /// subprograms.
3542 void ConstructSubprograms() {
3543
3544 std::string SPName = "llvm.dbg.subprograms";
3545 std::vector<GlobalVariable*> Result;
3546 getGlobalVariablesUsing(*M, SPName, Result);
3547 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
3548 RE = Result.end(); RI != RE; ++RI) {
3549
3550 DISubprogram *SP = new DISubprogram(*RI);
3551 CompileUnit *Unit = FindCompileUnit(SP->getCompileUnit());
3552
3553 // Check for pre-existence.
3554 DIE *&Slot = Unit->getDieMapSlotFor(SP->getGV());
3555 if (Slot) continue;
3556
3557 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
3558 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, SP->getName());
3559 const std::string &LinkageName = SP->getLinkageName();
3560 if (!LinkageName.empty())
3561 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
3562 LinkageName);
3563 DIType SPTy = SP->getType();
3564 AddType(Unit, SubprogramDie, SPTy);
3565 if (!SP->isLocalToUnit())
3566 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
3567 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
3568
3569 AddSourceLine(SubprogramDie, SP);
3570 //Add to map.
3571 Slot = SubprogramDie;
3572 //Add to context owner.
3573 Unit->getDie()->AddChild(SubprogramDie);
3574 //Expose as global.
3575 Unit->AddGlobal(SP->getName(), SubprogramDie);
3576 }
3577 }
3578
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003579 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
3580 /// subprograms.
3581 void ConstructSubprogramDIEs() {
Bill Wendling75091f92008-07-03 23:13:02 +00003582 std::vector<SubprogramDesc *> Subprograms;
3583 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
aslc200b112008-08-16 12:57:46 +00003584
Bill Wendling4de8de52008-07-03 22:53:42 +00003585 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
3586 SubprogramDesc *SPD = Subprograms[i];
3587 NewSubprogram(SPD);
3588 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003589 }
3590
3591public:
3592 //===--------------------------------------------------------------------===//
3593 // Main entry points.
3594 //
Owen Anderson847b99b2008-08-21 00:14:44 +00003595 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattnerb3876c72007-09-24 03:35:37 +00003596 : Dwarf(OS, A, T, "dbg")
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003597 , CompileUnits()
3598 , AbbreviationsSet(InitAbbreviationsSetSize)
3599 , Abbreviations()
3600 , ValuesSet(InitValuesSetSize)
3601 , Values()
3602 , StringPool()
3603 , DescToUnitMap()
3604 , SectionMap()
3605 , SectionSourceLines()
3606 , didInitial(false)
3607 , shouldEmit(false)
Devang Patel4d1709e2009-01-08 02:33:41 +00003608 , RootDbgScope(NULL)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003609 {
3610 }
3611 virtual ~DwarfDebug() {
3612 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
3613 delete CompileUnits[i];
3614 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3615 delete Values[j];
3616 }
3617
Devang Patel9304b382009-01-06 21:07:30 +00003618 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3619 /// This is inovked by the target AsmPrinter.
3620 void SetDebugInfo() {
3621 // FIXME - Check if the module has debug info or not.
3622 // Create all the compile unit DIEs.
3623 ConstructCompileUnits();
3624
3625 // Create DIEs for each of the externally visible global variables.
3626 ConstructGlobalVariableDIEs();
3627
3628 // Create DIEs for each of the externally visible subprograms.
3629 ConstructSubprograms();
3630
3631 // Prime section data.
3632 SectionMap.insert(TAI->getTextSection());
3633
3634 // Print out .file directives to specify files for .loc directives. These
3635 // are printed out early so that they precede any .loc directives.
3636 if (TAI->hasDotLocAndDotFile()) {
3637 for (unsigned i = 1, e = SrcFiles.size(); i <= e; ++i) {
3638 sys::Path FullPath(Directories[SrcFiles[i].getDirectoryID()]);
3639 bool AppendOk = FullPath.appendComponent(SrcFiles[i].getName());
3640 assert(AppendOk && "Could not append filename to directory!");
3641 AppendOk = false;
3642 Asm->EmitFile(i, FullPath.toString());
3643 Asm->EOL();
3644 }
3645 }
3646
3647 // Emit initial sections
3648 EmitInitial();
3649 }
3650
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003651 /// SetModuleInfo - Set machine module information when it's known that pass
3652 /// manager has created it. Set by the target AsmPrinter.
3653 void SetModuleInfo(MachineModuleInfo *mmi) {
3654 // Make sure initial declarations are made.
3655 if (!MMI && mmi->hasDebugInfo()) {
3656 MMI = mmi;
3657 shouldEmit = true;
aslc200b112008-08-16 12:57:46 +00003658
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003659 // Create all the compile unit DIEs.
3660 ConstructCompileUnitDIEs();
aslc200b112008-08-16 12:57:46 +00003661
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003662 // Create DIEs for each of the externally visible global variables.
3663 ConstructGlobalDIEs();
3664
3665 // Create DIEs for each of the externally visible subprograms.
3666 ConstructSubprogramDIEs();
aslc200b112008-08-16 12:57:46 +00003667
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003668 // Prime section data.
3669 SectionMap.insert(TAI->getTextSection());
Dan Gohman6d6c2402007-10-01 22:40:20 +00003670
3671 // Print out .file directives to specify files for .loc directives. These
3672 // are printed out early so that they precede any .loc directives.
3673 if (TAI->hasDotLocAndDotFile()) {
3674 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
3675 const UniqueVector<std::string> &Directories = MMI->getDirectories();
3676 for (unsigned i = 1, e = SourceFiles.size(); i <= e; ++i) {
3677 sys::Path FullPath(Directories[SourceFiles[i].getDirectoryID()]);
3678 bool AppendOk = FullPath.appendComponent(SourceFiles[i].getName());
3679 assert(AppendOk && "Could not append filename to directory!");
Devang Patel105a08a2008-12-23 21:55:38 +00003680 AppendOk = false;
Dan Gohman6d6c2402007-10-01 22:40:20 +00003681 Asm->EmitFile(i, FullPath.toString());
3682 Asm->EOL();
3683 }
3684 }
3685
3686 // Emit initial sections
3687 EmitInitial();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003688 }
3689 }
3690
3691 /// BeginModule - Emit all Dwarf sections that should come prior to the
3692 /// content.
3693 void BeginModule(Module *M) {
3694 this->M = M;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003695 }
3696
3697 /// EndModule - Emit all Dwarf sections that should come after the content.
3698 ///
3699 void EndModule() {
3700 if (!ShouldEmitDwarf()) return;
aslc200b112008-08-16 12:57:46 +00003701
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003702 // Standard sections final addresses.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003703 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003704 EmitLabel("text_end", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00003705 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003706 EmitLabel("data_end", 0);
aslc200b112008-08-16 12:57:46 +00003707
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003708 // End text sections.
3709 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003710 Asm->SwitchToSection(SectionMap[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003711 EmitLabel("section_end", i);
3712 }
3713
3714 // Emit common frame information.
3715 EmitCommonDebugFrame();
3716
3717 // Emit function debug frame information
3718 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3719 E = DebugFrames.end(); I != E; ++I)
3720 EmitFunctionDebugFrame(*I);
3721
3722 // Compute DIE offsets and sizes.
3723 SizeAndOffsets();
aslc200b112008-08-16 12:57:46 +00003724
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003725 // Emit all the DIEs into a debug info section
3726 EmitDebugInfo();
aslc200b112008-08-16 12:57:46 +00003727
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003728 // Corresponding abbreviations into a abbrev section.
3729 EmitAbbreviations();
aslc200b112008-08-16 12:57:46 +00003730
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003731 // Emit source line correspondence into a debug line section.
3732 EmitDebugLines();
aslc200b112008-08-16 12:57:46 +00003733
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003734 // Emit info into a debug pubnames section.
3735 EmitDebugPubNames();
aslc200b112008-08-16 12:57:46 +00003736
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003737 // Emit info into a debug str section.
3738 EmitDebugStr();
aslc200b112008-08-16 12:57:46 +00003739
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003740 // Emit info into a debug loc section.
3741 EmitDebugLoc();
aslc200b112008-08-16 12:57:46 +00003742
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003743 // Emit info into a debug aranges section.
3744 EmitDebugARanges();
aslc200b112008-08-16 12:57:46 +00003745
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003746 // Emit info into a debug ranges section.
3747 EmitDebugRanges();
aslc200b112008-08-16 12:57:46 +00003748
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003749 // Emit info into a debug macinfo section.
3750 EmitDebugMacInfo();
3751 }
3752
aslc200b112008-08-16 12:57:46 +00003753 /// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003754 /// emitted immediately after the function entry point.
3755 void BeginFunction(MachineFunction *MF) {
3756 this->MF = MF;
aslc200b112008-08-16 12:57:46 +00003757
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003758 if (!ShouldEmitDwarf()) return;
3759
3760 // Begin accumulating function debug information.
3761 MMI->BeginFunction(MF);
aslc200b112008-08-16 12:57:46 +00003762
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003763 // Assumes in correct section after the entry point.
3764 EmitLabel("func_begin", ++SubprogramCount);
Evan Chenga53c40a2008-02-01 09:10:45 +00003765
3766 // Emit label for the implicitly defined dbg.stoppoint at the start of
3767 // the function.
Andrew Lenharth42f91402008-04-03 17:37:43 +00003768 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
3769 if (!LineInfos.empty()) {
3770 const SourceLineInfo &LineInfo = LineInfos[0];
3771 Asm->printLabel(LineInfo.getLabelID());
3772 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003773 }
aslc200b112008-08-16 12:57:46 +00003774
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003775 /// EndFunction - Gather and emit post-function debug information.
3776 ///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003777 void EndFunction(MachineFunction *MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003778 if (!ShouldEmitDwarf()) return;
aslc200b112008-08-16 12:57:46 +00003779
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003780 // Define end label for subprogram.
3781 EmitLabel("func_end", SubprogramCount);
aslc200b112008-08-16 12:57:46 +00003782
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003783 // Get function line info.
3784 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
3785
3786 if (!LineInfos.empty()) {
3787 // Get section line info.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003788 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003789 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
3790 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
3791 // Append the function info to section info.
3792 SectionLineInfos.insert(SectionLineInfos.end(),
3793 LineInfos.begin(), LineInfos.end());
3794 }
aslc200b112008-08-16 12:57:46 +00003795
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003796 // Construct scopes for subprogram.
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003797 if (MMI->getRootScope())
3798 ConstructRootScope(MMI->getRootScope());
3799 else
3800 // FIXME: This is wrong. We are essentially getting past a problem with
3801 // debug information not being able to handle unreachable blocks that have
3802 // debug information in them. In particular, those unreachable blocks that
3803 // have "region end" info in them. That situation results in the "root
3804 // scope" not being created. If that's the case, then emit a "default"
3805 // scope, i.e., one that encompasses the whole function. This isn't
3806 // desirable. And a better way of handling this (and all of the debugging
3807 // information) needs to be explored.
3808 ConstructDefaultScope(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003809
3810 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3811 MMI->getFrameMoves()));
3812 }
3813};
3814
3815//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00003816/// DwarfException - Emits Dwarf exception handling directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003817///
3818class DwarfException : public Dwarf {
3819
3820private:
3821 struct FunctionEHFrameInfo {
3822 std::string FnName;
3823 unsigned Number;
3824 unsigned PersonalityIndex;
3825 bool hasCalls;
3826 bool hasLandingPads;
3827 std::vector<MachineMove> Moves;
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003828 const Function * function;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003829
3830 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3831 bool hC, bool hL,
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003832 const std::vector<MachineMove> &M,
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003833 const Function *f):
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003834 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003835 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003836 };
3837
3838 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen85535762008-04-02 00:25:04 +00003839
3840 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3841 /// be emitted.
3842 bool shouldEmitTable;
3843
3844 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3845 /// should be emitted.
3846 bool shouldEmitMoves;
3847
3848 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3849 /// should be emitted.
3850 bool shouldEmitTableModule;
3851
aslc200b112008-08-16 12:57:46 +00003852 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen85535762008-04-02 00:25:04 +00003853 /// should be emitted.
3854 bool shouldEmitMovesModule;
Duncan Sands96144f92008-05-07 19:11:09 +00003855
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003856 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3857 ///
3858 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
3859 // Size and sign of stack growth.
3860 int stackGrowth =
3861 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3862 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00003863 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003864
3865 // Begin eh frame section.
3866 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling189bde72008-12-24 08:05:17 +00003867
3868 if (!TAI->doesRequireNonLocalEHFrameLabel())
3869 O << TAI->getEHGlobalPrefix();
3870 O << "EH_frame" << Index << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003871 EmitLabel("section_eh_frame", Index);
3872
3873 // Define base labels.
3874 EmitLabel("eh_frame_common", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003875
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003876 // Define the eh frame length.
3877 EmitDifference("eh_frame_common_end", Index,
3878 "eh_frame_common_begin", Index, true);
3879 Asm->EOL("Length of Common Information Entry");
3880
3881 // EH frame header.
3882 EmitLabel("eh_frame_common_begin", Index);
3883 Asm->EmitInt32((int)0);
3884 Asm->EOL("CIE Identifier Tag");
3885 Asm->EmitInt8(DW_CIE_VERSION);
3886 Asm->EOL("CIE Version");
Duncan Sands96144f92008-05-07 19:11:09 +00003887
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003888 // The personality presence indicates that language specific information
3889 // will show up in the eh frame.
3890 Asm->EmitString(Personality ? "zPLR" : "zR");
3891 Asm->EOL("CIE Augmentation");
Duncan Sands96144f92008-05-07 19:11:09 +00003892
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003893 // Round out reader.
3894 Asm->EmitULEB128Bytes(1);
3895 Asm->EOL("CIE Code Alignment Factor");
3896 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands96144f92008-05-07 19:11:09 +00003897 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00003898 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands96144f92008-05-07 19:11:09 +00003899 Asm->EOL("CIE Return Address Column");
3900
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003901 // If there is a personality, we need to indicate the functions location.
3902 if (Personality) {
3903 Asm->EmitULEB128Bytes(7);
3904 Asm->EOL("Augmentation Size");
Bill Wendling2d369922007-09-11 17:20:55 +00003905
Duncan Sands96144f92008-05-07 19:11:09 +00003906 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendling2d369922007-09-11 17:20:55 +00003907 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands96144f92008-05-07 19:11:09 +00003908 Asm->EOL("Personality (pcrel sdata4 indirect)");
3909 } else {
Bill Wendling2d369922007-09-11 17:20:55 +00003910 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands96144f92008-05-07 19:11:09 +00003911 Asm->EOL("Personality (pcrel sdata4)");
3912 }
Bill Wendling2d369922007-09-11 17:20:55 +00003913
Duncan Sands96144f92008-05-07 19:11:09 +00003914 PrintRelDirective(true);
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003915 O << TAI->getPersonalityPrefix();
3916 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3917 O << TAI->getPersonalitySuffix();
Duncan Sands4cc39532008-05-08 12:33:11 +00003918 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3919 O << "-" << TAI->getPCSymbol();
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003920 Asm->EOL("Personality");
Bill Wendling38cb7c92007-08-25 00:51:55 +00003921
Duncan Sands96144f92008-05-07 19:11:09 +00003922 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3923 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003924
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003925 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3926 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003927 } else {
3928 Asm->EmitULEB128Bytes(1);
3929 Asm->EOL("Augmentation Size");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003930
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003931 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3932 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003933 }
3934
3935 // Indicate locations of general callee saved registers in frame.
3936 std::vector<MachineMove> Moves;
3937 RI->getInitialFrameState(Moves);
Dale Johannesenf5a11532007-11-13 19:13:01 +00003938 EmitFrameMoves(NULL, 0, Moves, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003939
Dale Johannesen388f20f2008-04-30 00:43:29 +00003940 // On Darwin the linker honors the alignment of eh_frame, which means it
3941 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3942 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003943 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003944 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003945 EmitLabel("eh_frame_common_end", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003946
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003947 Asm->EOL();
3948 }
Duncan Sands96144f92008-05-07 19:11:09 +00003949
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003950 /// EmitEHFrame - Emit function exception frame information.
3951 ///
3952 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003953 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3954
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003955 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3956
3957 // Externally visible entry into the functions eh frame info.
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003958 // If the corresponding function is static, this should not be
3959 // externally visible.
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003960 if (linkage != Function::InternalLinkage) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003961 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3962 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3963 }
3964
Dale Johannesenf09b5992008-01-10 02:03:30 +00003965 // If corresponding function is weak definition, this should be too.
aslc200b112008-08-16 12:57:46 +00003966 if ((linkage == Function::WeakLinkage ||
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003967 linkage == Function::LinkOnceLinkage) &&
Dale Johannesenf09b5992008-01-10 02:03:30 +00003968 TAI->getWeakDefDirective())
3969 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3970
3971 // If there are no calls then you can't unwind. This may mean we can
3972 // omit the EH Frame, but some environments do not handle weak absolute
aslc200b112008-08-16 12:57:46 +00003973 // symbols.
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003974 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesena9b3e482008-04-08 00:10:24 +00003975 // unwind info is to be available for non-EH uses.
Dale Johannesenf09b5992008-01-10 02:03:30 +00003976 if (!EHFrameInfo.hasCalls &&
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003977 !UnwindTablesMandatory &&
aslc200b112008-08-16 12:57:46 +00003978 ((linkage != Function::WeakLinkage &&
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003979 linkage != Function::LinkOnceLinkage) ||
Dale Johannesenf09b5992008-01-10 02:03:30 +00003980 !TAI->getWeakDefDirective() ||
3981 TAI->getSupportsWeakOmittedEHFrame()))
aslc200b112008-08-16 12:57:46 +00003982 {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003983 O << EHFrameInfo.FnName << " = 0\n";
aslc200b112008-08-16 12:57:46 +00003984 // This name has no connection to the function, so it might get
3985 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003986 // dead-stripping unconditionally.
3987 if (const char *UsedDirective = TAI->getUsedDirective())
3988 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003989 } else {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003990 O << EHFrameInfo.FnName << ":\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003991
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003992 // EH frame header.
3993 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3994 "eh_frame_begin", EHFrameInfo.Number, true);
3995 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00003996
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003997 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
3998
Bill Wendling189bde72008-12-24 08:05:17 +00003999 if (TAI->doesRequireNonLocalEHFrameLabel()) {
4000 PrintRelDirective(true, true);
4001 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
4002
4003 if (!TAI->isAbsoluteEHSectionOffsets())
4004 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
4005 } else {
4006 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
4007 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
4008 true, true, false);
4009 }
4010
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004011 Asm->EOL("FDE CIE offset");
4012
Bill Wendlingdd9127d2009-01-06 19:13:55 +00004013 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004014 Asm->EOL("FDE initial location");
4015 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendlingdd9127d2009-01-06 19:13:55 +00004016 "eh_func_begin", EHFrameInfo.Number, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004017 Asm->EOL("FDE address range");
Duncan Sands96144f92008-05-07 19:11:09 +00004018
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004019 // If there is a personality and landing pads then point to the language
4020 // specific data area in the exception table.
4021 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands96144f92008-05-07 19:11:09 +00004022 Asm->EmitULEB128Bytes(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004023 Asm->EOL("Augmentation size");
Duncan Sands96144f92008-05-07 19:11:09 +00004024
4025 if (EHFrameInfo.hasLandingPads)
4026 EmitReference("exception", EHFrameInfo.Number, true, true);
4027 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004028 Asm->EmitInt32((int)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004029 Asm->EOL("Language Specific Data Area");
4030 } else {
4031 Asm->EmitULEB128Bytes(0);
4032 Asm->EOL("Augmentation size");
4033 }
Duncan Sands96144f92008-05-07 19:11:09 +00004034
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004035 // Indicate locations of function specific callee saved registers in
4036 // frame.
Dale Johannesenf5a11532007-11-13 19:13:01 +00004037 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves, true);
aslc200b112008-08-16 12:57:46 +00004038
Dale Johannesen388f20f2008-04-30 00:43:29 +00004039 // On Darwin the linker honors the alignment of eh_frame, which means it
4040 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
4041 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00004042 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00004043 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004044 EmitLabel("eh_frame_end", EHFrameInfo.Number);
aslc200b112008-08-16 12:57:46 +00004045
4046 // If the function is marked used, this table should be also. We cannot
Dale Johannesen3dadeb32008-01-16 19:59:28 +00004047 // make the mark unconditional in this case, since retaining the table
aslc200b112008-08-16 12:57:46 +00004048 // also retains the function in this case, and there is code around
Dale Johannesen3dadeb32008-01-16 19:59:28 +00004049 // that depends on unused functions (calling undefined externals) being
4050 // dead-stripped to link correctly. Yes, there really is.
4051 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
4052 if (const char *UsedDirective = TAI->getUsedDirective())
4053 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
4054 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004055 }
4056
Duncan Sands241a0c92007-09-05 11:27:52 +00004057 /// EmitExceptionTable - Emit landing pads and actions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004058 ///
4059 /// The general organization of the table is complex, but the basic concepts
4060 /// are easy. First there is a header which describes the location and
4061 /// organization of the three components that follow.
4062 /// 1. The landing pad site information describes the range of code covered
4063 /// by the try. In our case it's an accumulation of the ranges covered
4064 /// by the invokes in the try. There is also a reference to the landing
4065 /// pad that handles the exception once processed. Finally an index into
4066 /// the actions table.
4067 /// 2. The action table, in our case, is composed of pairs of type ids
4068 /// and next action offset. Starting with the action index from the
4069 /// landing pad site, each type Id is checked for a match to the current
4070 /// exception. If it matches then the exception and type id are passed
4071 /// on to the landing pad. Otherwise the next action is looked up. This
4072 /// chain is terminated with a next action of zero. If no type id is
4073 /// found the the frame is unwound and handling continues.
4074 /// 3. Type id table contains references to all the C++ typeinfo for all
4075 /// catches in the function. This tables is reversed indexed base 1.
4076
4077 /// SharedTypeIds - How many leading type ids two landing pads have in common.
4078 static unsigned SharedTypeIds(const LandingPadInfo *L,
4079 const LandingPadInfo *R) {
4080 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
4081 unsigned LSize = LIds.size(), RSize = RIds.size();
4082 unsigned MinSize = LSize < RSize ? LSize : RSize;
4083 unsigned Count = 0;
4084
4085 for (; Count != MinSize; ++Count)
4086 if (LIds[Count] != RIds[Count])
4087 return Count;
4088
4089 return Count;
4090 }
4091
4092 /// PadLT - Order landing pads lexicographically by type id.
4093 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
4094 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
4095 unsigned LSize = LIds.size(), RSize = RIds.size();
4096 unsigned MinSize = LSize < RSize ? LSize : RSize;
4097
4098 for (unsigned i = 0; i != MinSize; ++i)
4099 if (LIds[i] != RIds[i])
4100 return LIds[i] < RIds[i];
4101
4102 return LSize < RSize;
4103 }
4104
4105 struct KeyInfo {
4106 static inline unsigned getEmptyKey() { return -1U; }
4107 static inline unsigned getTombstoneKey() { return -2U; }
4108 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner92eea072007-09-17 18:34:04 +00004109 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004110 static bool isPod() { return true; }
4111 };
4112
Duncan Sands241a0c92007-09-05 11:27:52 +00004113 /// ActionEntry - Structure describing an entry in the actions table.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004114 struct ActionEntry {
4115 int ValueForTypeID; // The value to write - may not be equal to the type id.
4116 int NextAction;
4117 struct ActionEntry *Previous;
4118 };
4119
Duncan Sands241a0c92007-09-05 11:27:52 +00004120 /// PadRange - Structure holding a try-range and the associated landing pad.
4121 struct PadRange {
4122 // The index of the landing pad.
4123 unsigned PadIndex;
4124 // The index of the begin and end labels in the landing pad's label lists.
4125 unsigned RangeIndex;
4126 };
4127
4128 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
4129
4130 /// CallSiteEntry - Structure describing an entry in the call-site table.
4131 struct CallSiteEntry {
Duncan Sands4ff179f2007-12-19 07:36:31 +00004132 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00004133 unsigned BeginLabel; // zero indicates the start of the function.
4134 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands4ff179f2007-12-19 07:36:31 +00004135 // The landing pad starts at PadLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00004136 unsigned PadLabel; // zero indicates that there is no landing pad.
4137 unsigned Action;
4138 };
4139
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004140 void EmitExceptionTable() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004141 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
4142 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
4143 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
4144 if (PadInfos.empty()) return;
4145
4146 // Sort the landing pads in order of their type ids. This is used to fold
4147 // duplicate actions.
4148 SmallVector<const LandingPadInfo *, 64> LandingPads;
4149 LandingPads.reserve(PadInfos.size());
4150 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
4151 LandingPads.push_back(&PadInfos[i]);
4152 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
4153
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004154 // Negative type ids index into FilterIds, positive type ids index into
4155 // TypeInfos. The value written for a positive type id is just the type
4156 // id itself. For a negative type id, however, the value written is the
4157 // (negative) byte offset of the corresponding FilterIds entry. The byte
4158 // offset is usually equal to the type id, because the FilterIds entries
4159 // are written using a variable width encoding which outputs one byte per
4160 // entry as long as the value written is not too large, but can differ.
4161 // This kind of complication does not occur for positive type ids because
4162 // type infos are output using a fixed width encoding.
4163 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
4164 SmallVector<int, 16> FilterOffsets;
4165 FilterOffsets.reserve(FilterIds.size());
4166 int Offset = -1;
4167 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
4168 E = FilterIds.end(); I != E; ++I) {
4169 FilterOffsets.push_back(Offset);
aslc200b112008-08-16 12:57:46 +00004170 Offset -= TargetAsmInfo::getULEB128Size(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004171 }
4172
Duncan Sands241a0c92007-09-05 11:27:52 +00004173 // Compute the actions table and gather the first action index for each
4174 // landing pad site.
4175 SmallVector<ActionEntry, 32> Actions;
4176 SmallVector<unsigned, 64> FirstActions;
4177 FirstActions.reserve(LandingPads.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004178
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004179 int FirstAction = 0;
Duncan Sands241a0c92007-09-05 11:27:52 +00004180 unsigned SizeActions = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004181 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
4182 const LandingPadInfo *LP = LandingPads[i];
4183 const std::vector<int> &TypeIds = LP->TypeIds;
4184 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
4185 unsigned SizeSiteActions = 0;
4186
4187 if (NumShared < TypeIds.size()) {
4188 unsigned SizeAction = 0;
4189 ActionEntry *PrevAction = 0;
4190
4191 if (NumShared) {
4192 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
4193 assert(Actions.size());
4194 PrevAction = &Actions.back();
aslc200b112008-08-16 12:57:46 +00004195 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
4196 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004197 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
aslc200b112008-08-16 12:57:46 +00004198 SizeAction -=
4199 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004200 SizeAction += -PrevAction->NextAction;
4201 PrevAction = PrevAction->Previous;
4202 }
4203 }
4204
4205 // Compute the actions.
4206 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
4207 int TypeID = TypeIds[I];
4208 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
4209 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
aslc200b112008-08-16 12:57:46 +00004210 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004211
4212 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
aslc200b112008-08-16 12:57:46 +00004213 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004214 SizeSiteActions += SizeAction;
4215
4216 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
4217 Actions.push_back(Action);
4218
4219 PrevAction = &Actions.back();
4220 }
4221
4222 // Record the first action of the landing pad site.
4223 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
4224 } // else identical - re-use previous FirstAction
4225
4226 FirstActions.push_back(FirstAction);
4227
4228 // Compute this sites contribution to size.
4229 SizeActions += SizeSiteActions;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004230 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004231
Duncan Sands4ff179f2007-12-19 07:36:31 +00004232 // Compute the call-site table. The entry for an invoke has a try-range
4233 // containing the call, a non-zero landing pad and an appropriate action.
4234 // The entry for an ordinary call has a try-range containing the call and
4235 // zero for the landing pad and the action. Calls marked 'nounwind' have
4236 // no entry and must not be contained in the try-range of any entry - they
4237 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands241a0c92007-09-05 11:27:52 +00004238 SmallVector<CallSiteEntry, 64> CallSites;
4239
4240 RangeMapType PadMap;
Duncan Sands4ff179f2007-12-19 07:36:31 +00004241 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
4242 // by try-range labels when lowered). Ordinary calls do not, so appropriate
4243 // try-ranges for them need be deduced.
Duncan Sands241a0c92007-09-05 11:27:52 +00004244 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
4245 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands4ff179f2007-12-19 07:36:31 +00004246 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands241a0c92007-09-05 11:27:52 +00004247 unsigned BeginLabel = LandingPad->BeginLabels[j];
4248 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
4249 PadRange P = { i, j };
4250 PadMap[BeginLabel] = P;
4251 }
4252 }
4253
Duncan Sands4ff179f2007-12-19 07:36:31 +00004254 // The end label of the previous invoke or nounwind try-range.
Duncan Sands241a0c92007-09-05 11:27:52 +00004255 unsigned LastLabel = 0;
Duncan Sands4ff179f2007-12-19 07:36:31 +00004256
4257 // Whether there is a potentially throwing instruction (currently this means
4258 // an ordinary call) between the end of the previous try-range and now.
4259 bool SawPotentiallyThrowing = false;
4260
4261 // Whether the last callsite entry was for an invoke.
4262 bool PreviousIsInvoke = false;
4263
Duncan Sands4ff179f2007-12-19 07:36:31 +00004264 // Visit all instructions in order of address.
Duncan Sands241a0c92007-09-05 11:27:52 +00004265 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
4266 I != E; ++I) {
4267 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
4268 MI != E; ++MI) {
Dan Gohmanfa607c92008-07-01 00:05:16 +00004269 if (!MI->isLabel()) {
Chris Lattner5b930372008-01-07 07:27:27 +00004270 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands241a0c92007-09-05 11:27:52 +00004271 continue;
4272 }
4273
Chris Lattnerda4cff12007-12-30 20:50:28 +00004274 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands241a0c92007-09-05 11:27:52 +00004275 assert(BeginLabel && "Invalid label!");
Duncan Sands89372f62007-09-05 14:12:46 +00004276
Duncan Sands4ff179f2007-12-19 07:36:31 +00004277 // End of the previous try-range?
Duncan Sands89372f62007-09-05 14:12:46 +00004278 if (BeginLabel == LastLabel)
Duncan Sands4ff179f2007-12-19 07:36:31 +00004279 SawPotentiallyThrowing = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00004280
Duncan Sands4ff179f2007-12-19 07:36:31 +00004281 // Beginning of a new try-range?
Duncan Sands241a0c92007-09-05 11:27:52 +00004282 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands241a0c92007-09-05 11:27:52 +00004283 if (L == PadMap.end())
Duncan Sands4ff179f2007-12-19 07:36:31 +00004284 // Nope, it was just some random label.
Duncan Sands241a0c92007-09-05 11:27:52 +00004285 continue;
4286
4287 PadRange P = L->second;
4288 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
4289
4290 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
4291 "Inconsistent landing pad map!");
4292
4293 // If some instruction between the previous try-range and this one may
4294 // throw, create a call-site entry with no landing pad for the region
4295 // between the try-ranges.
Duncan Sands4ff179f2007-12-19 07:36:31 +00004296 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00004297 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
4298 CallSites.push_back(Site);
Duncan Sands4ff179f2007-12-19 07:36:31 +00004299 PreviousIsInvoke = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00004300 }
4301
4302 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands4ff179f2007-12-19 07:36:31 +00004303 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands241a0c92007-09-05 11:27:52 +00004304
Duncan Sands4ff179f2007-12-19 07:36:31 +00004305 if (LandingPad->LandingPadLabel) {
4306 // This try-range is for an invoke.
4307 CallSiteEntry Site = {BeginLabel, LastLabel,
4308 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands241a0c92007-09-05 11:27:52 +00004309
Duncan Sands4ff179f2007-12-19 07:36:31 +00004310 // Try to merge with the previous call-site.
4311 if (PreviousIsInvoke) {
Dan Gohman3d436002008-06-21 22:00:54 +00004312 CallSiteEntry &Prev = CallSites.back();
Duncan Sands4ff179f2007-12-19 07:36:31 +00004313 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
4314 // Extend the range of the previous entry.
4315 Prev.EndLabel = Site.EndLabel;
4316 continue;
4317 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004318 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004319
Duncan Sands4ff179f2007-12-19 07:36:31 +00004320 // Otherwise, create a new call-site.
4321 CallSites.push_back(Site);
4322 PreviousIsInvoke = true;
4323 } else {
4324 // Create a gap.
4325 PreviousIsInvoke = false;
4326 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004327 }
4328 }
4329 // If some instruction between the previous try-range and the end of the
4330 // function may throw, create a call-site entry with no landing pad for the
4331 // region following the try-range.
Duncan Sands4ff179f2007-12-19 07:36:31 +00004332 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00004333 CallSiteEntry Site = {LastLabel, 0, 0, 0};
4334 CallSites.push_back(Site);
4335 }
4336
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004337 // Final tallies.
Duncan Sands96144f92008-05-07 19:11:09 +00004338
4339 // Call sites.
4340 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
4341 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
4342 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
4343 unsigned SizeSites = CallSites.size() * (SiteStartSize +
4344 SiteLengthSize +
4345 LandingPadSize);
Duncan Sands241a0c92007-09-05 11:27:52 +00004346 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
aslc200b112008-08-16 12:57:46 +00004347 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands241a0c92007-09-05 11:27:52 +00004348
Duncan Sands96144f92008-05-07 19:11:09 +00004349 // Type infos.
4350 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
4351 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004352
4353 unsigned TypeOffset = sizeof(int8_t) + // Call site format
aslc200b112008-08-16 12:57:46 +00004354 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004355 SizeSites + SizeActions + SizeTypes;
4356
4357 unsigned TotalSize = sizeof(int8_t) + // LPStart format
4358 sizeof(int8_t) + // TType format
aslc200b112008-08-16 12:57:46 +00004359 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004360 TypeOffset;
4361
4362 unsigned SizeAlign = (4 - TotalSize) & 3;
4363
4364 // Begin the exception table.
4365 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng7e7d1942008-02-29 19:36:59 +00004366 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesen841e4982008-10-08 21:50:21 +00004367 O << "GCC_except_table" << SubprogramCount << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004368 for (unsigned i = 0; i != SizeAlign; ++i) {
4369 Asm->EmitInt8(0);
4370 Asm->EOL("Padding");
4371 }
4372 EmitLabel("exception", SubprogramCount);
4373
4374 // Emit the header.
4375 Asm->EmitInt8(DW_EH_PE_omit);
4376 Asm->EOL("LPStart format (DW_EH_PE_omit)");
4377 Asm->EmitInt8(DW_EH_PE_absptr);
4378 Asm->EOL("TType format (DW_EH_PE_absptr)");
4379 Asm->EmitULEB128Bytes(TypeOffset);
4380 Asm->EOL("TType base offset");
4381 Asm->EmitInt8(DW_EH_PE_udata4);
4382 Asm->EOL("Call site format (DW_EH_PE_udata4)");
4383 Asm->EmitULEB128Bytes(SizeSites);
4384 Asm->EOL("Call-site table length");
4385
Duncan Sands241a0c92007-09-05 11:27:52 +00004386 // Emit the landing pad site information.
4387 for (unsigned i = 0; i < CallSites.size(); ++i) {
4388 CallSiteEntry &S = CallSites[i];
4389 const char *BeginTag;
4390 unsigned BeginNumber;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004391
Duncan Sands241a0c92007-09-05 11:27:52 +00004392 if (!S.BeginLabel) {
4393 BeginTag = "eh_func_begin";
4394 BeginNumber = SubprogramCount;
4395 } else {
4396 BeginTag = "label";
4397 BeginNumber = S.BeginLabel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004398 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004399
Duncan Sands241a0c92007-09-05 11:27:52 +00004400 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00004401 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004402 Asm->EOL("Region start");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004403
Duncan Sands241a0c92007-09-05 11:27:52 +00004404 if (!S.EndLabel) {
Dale Johannesen4670be42008-01-15 23:24:56 +00004405 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands96144f92008-05-07 19:11:09 +00004406 true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004407 } else {
Duncan Sands96144f92008-05-07 19:11:09 +00004408 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004409 }
4410 Asm->EOL("Region length");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004411
Duncan Sands96144f92008-05-07 19:11:09 +00004412 if (!S.PadLabel)
4413 Asm->EmitInt32(0);
4414 else
Duncan Sands241a0c92007-09-05 11:27:52 +00004415 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00004416 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004417 Asm->EOL("Landing pad");
4418
4419 Asm->EmitULEB128Bytes(S.Action);
4420 Asm->EOL("Action");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004421 }
4422
4423 // Emit the actions.
4424 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4425 ActionEntry &Action = Actions[I];
4426
4427 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
4428 Asm->EOL("TypeInfo index");
4429 Asm->EmitSLEB128Bytes(Action.NextAction);
4430 Asm->EOL("Next action");
4431 }
4432
4433 // Emit the type ids.
4434 for (unsigned M = TypeInfos.size(); M; --M) {
4435 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00004436
4437 PrintRelDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004438
4439 if (GV)
4440 O << Asm->getGlobalLinkName(GV);
4441 else
4442 O << "0";
Duncan Sands241a0c92007-09-05 11:27:52 +00004443
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004444 Asm->EOL("TypeInfo");
4445 }
4446
4447 // Emit the filter typeids.
4448 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4449 unsigned TypeID = FilterIds[j];
4450 Asm->EmitULEB128Bytes(TypeID);
4451 Asm->EOL("Filter TypeInfo index");
4452 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004453
Evan Cheng7e7d1942008-02-29 19:36:59 +00004454 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004455 }
4456
4457public:
4458 //===--------------------------------------------------------------------===//
4459 // Main entry points.
4460 //
Owen Anderson847b99b2008-08-21 00:14:44 +00004461 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattnerb3876c72007-09-24 03:35:37 +00004462 : Dwarf(OS, A, T, "eh")
Dale Johannesen85535762008-04-02 00:25:04 +00004463 , shouldEmitTable(false)
4464 , shouldEmitMoves(false)
4465 , shouldEmitTableModule(false)
4466 , shouldEmitMovesModule(false)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004467 {}
aslc200b112008-08-16 12:57:46 +00004468
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004469 virtual ~DwarfException() {}
4470
4471 /// SetModuleInfo - Set machine module information when it's known that pass
4472 /// manager has created it. Set by the target AsmPrinter.
4473 void SetModuleInfo(MachineModuleInfo *mmi) {
4474 MMI = mmi;
4475 }
4476
4477 /// BeginModule - Emit all exception information that should come prior to the
4478 /// content.
4479 void BeginModule(Module *M) {
4480 this->M = M;
4481 }
4482
4483 /// EndModule - Emit all exception information that should come after the
4484 /// content.
4485 void EndModule() {
Dale Johannesen85535762008-04-02 00:25:04 +00004486 if (shouldEmitMovesModule || shouldEmitTableModule) {
4487 const std::vector<Function *> Personalities = MMI->getPersonalities();
4488 for (unsigned i =0; i < Personalities.size(); ++i)
4489 EmitCommonEHFrame(Personalities[i], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004490
Dale Johannesen85535762008-04-02 00:25:04 +00004491 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4492 E = EHFrames.end(); I != E; ++I)
4493 EmitEHFrame(*I);
4494 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004495 }
4496
aslc200b112008-08-16 12:57:46 +00004497 /// BeginFunction - Gather pre-function exception information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004498 /// emitted immediately after the function entry point.
4499 void BeginFunction(MachineFunction *MF) {
4500 this->MF = MF;
Dale Johannesen85535762008-04-02 00:25:04 +00004501 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen62f0a6d2008-04-02 17:04:45 +00004502 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen85535762008-04-02 00:25:04 +00004503
4504 // Map all labels and get rid of any dead landing pads.
4505 MMI->TidyLandingPads();
4506 // If any landing pads survive, we need an EH table.
4507 if (MMI->getLandingPads().size())
4508 shouldEmitTable = true;
4509
4510 // See if we need frame move info.
Duncan Sandscbc28b12008-07-04 09:55:48 +00004511 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen85535762008-04-02 00:25:04 +00004512 shouldEmitMoves = true;
4513
4514 if (shouldEmitMoves || shouldEmitTable)
4515 // Assumes in correct section after the entry point.
4516 EmitLabel("eh_func_begin", ++SubprogramCount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004517 }
Dale Johannesen85535762008-04-02 00:25:04 +00004518 shouldEmitTableModule |= shouldEmitTable;
4519 shouldEmitMovesModule |= shouldEmitMoves;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004520 }
4521
4522 /// EndFunction - Gather and emit post-function exception information.
4523 ///
4524 void EndFunction() {
Dale Johannesen85535762008-04-02 00:25:04 +00004525 if (shouldEmitMoves || shouldEmitTable) {
4526 EmitLabel("eh_func_end", SubprogramCount);
4527 EmitExceptionTable();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004528
Dale Johannesen85535762008-04-02 00:25:04 +00004529 // Save EH frame information
4530 EHFrames.
4531 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendlingef9211a2007-09-18 01:47:22 +00004532 SubprogramCount,
4533 MMI->getPersonalityIndex(),
4534 MF->getFrameInfo()->hasCalls(),
4535 !MMI->getLandingPads().empty(),
Dale Johannesenfb3ac732007-11-20 23:24:42 +00004536 MMI->getFrameMoves(),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00004537 MF->getFunction()));
Dale Johannesen85535762008-04-02 00:25:04 +00004538 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004539 }
4540};
4541
4542} // End of namespace llvm
4543
4544//===----------------------------------------------------------------------===//
4545
4546/// Emit - Print the abbreviation using the specified Dwarf writer.
4547///
4548void DIEAbbrev::Emit(const DwarfDebug &DD) const {
4549 // Emit its Dwarf tag type.
4550 DD.getAsm()->EmitULEB128Bytes(Tag);
4551 DD.getAsm()->EOL(TagString(Tag));
aslc200b112008-08-16 12:57:46 +00004552
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004553 // Emit whether it has children DIEs.
4554 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4555 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
aslc200b112008-08-16 12:57:46 +00004556
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004557 // For each attribute description.
4558 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4559 const DIEAbbrevData &AttrData = Data[i];
aslc200b112008-08-16 12:57:46 +00004560
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004561 // Emit attribute type.
4562 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4563 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
aslc200b112008-08-16 12:57:46 +00004564
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004565 // Emit form type.
4566 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4567 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
4568 }
4569
4570 // Mark end of abbreviation.
4571 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4572 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
4573}
4574
4575#ifndef NDEBUG
4576void DIEAbbrev::print(std::ostream &O) {
4577 O << "Abbreviation @"
4578 << std::hex << (intptr_t)this << std::dec
4579 << " "
4580 << TagString(Tag)
4581 << " "
4582 << ChildrenString(ChildrenFlag)
4583 << "\n";
aslc200b112008-08-16 12:57:46 +00004584
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004585 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4586 O << " "
4587 << AttributeString(Data[i].getAttribute())
4588 << " "
4589 << FormEncodingString(Data[i].getForm())
4590 << "\n";
4591 }
4592}
4593void DIEAbbrev::dump() { print(cerr); }
4594#endif
4595
4596//===----------------------------------------------------------------------===//
4597
4598#ifndef NDEBUG
4599void DIEValue::dump() {
4600 print(cerr);
4601}
4602#endif
4603
4604//===----------------------------------------------------------------------===//
4605
4606/// EmitValue - Emit integer of appropriate size.
4607///
4608void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
4609 switch (Form) {
4610 case DW_FORM_flag: // Fall thru
4611 case DW_FORM_ref1: // Fall thru
4612 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
4613 case DW_FORM_ref2: // Fall thru
4614 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
4615 case DW_FORM_ref4: // Fall thru
4616 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
4617 case DW_FORM_ref8: // Fall thru
4618 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4619 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4620 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
4621 default: assert(0 && "DIE Value form not supported yet"); break;
4622 }
4623}
4624
4625/// SizeOf - Determine size of integer value in bytes.
4626///
4627unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4628 switch (Form) {
4629 case DW_FORM_flag: // Fall thru
4630 case DW_FORM_ref1: // Fall thru
4631 case DW_FORM_data1: return sizeof(int8_t);
4632 case DW_FORM_ref2: // Fall thru
4633 case DW_FORM_data2: return sizeof(int16_t);
4634 case DW_FORM_ref4: // Fall thru
4635 case DW_FORM_data4: return sizeof(int32_t);
4636 case DW_FORM_ref8: // Fall thru
4637 case DW_FORM_data8: return sizeof(int64_t);
aslc200b112008-08-16 12:57:46 +00004638 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4639 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004640 default: assert(0 && "DIE Value form not supported yet"); break;
4641 }
4642 return 0;
4643}
4644
4645//===----------------------------------------------------------------------===//
4646
4647/// EmitValue - Emit string value.
4648///
4649void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
4650 DD.getAsm()->EmitString(String);
4651}
4652
4653//===----------------------------------------------------------------------===//
4654
4655/// EmitValue - Emit label value.
4656///
4657void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004658 bool IsSmall = Form == DW_FORM_data4;
4659 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004660}
4661
4662/// SizeOf - Determine size of label value in bytes.
4663///
4664unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004665 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004666 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004667}
4668
4669//===----------------------------------------------------------------------===//
4670
4671/// EmitValue - Emit label value.
4672///
4673void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004674 bool IsSmall = Form == DW_FORM_data4;
4675 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004676}
4677
4678/// SizeOf - Determine size of label value in bytes.
4679///
4680unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004681 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004682 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004683}
aslc200b112008-08-16 12:57:46 +00004684
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004685//===----------------------------------------------------------------------===//
4686
4687/// EmitValue - Emit delta value.
4688///
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004689void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4690 bool IsSmall = Form == DW_FORM_data4;
4691 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4692 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4693}
4694
4695/// SizeOf - Determine size of delta value in bytes.
4696///
4697unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4698 if (Form == DW_FORM_data4) return 4;
4699 return DD.getTargetData()->getPointerSize();
4700}
aslc200b112008-08-16 12:57:46 +00004701
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004702//===----------------------------------------------------------------------===//
4703
4704/// EmitValue - Emit delta value.
4705///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004706void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
4707 bool IsSmall = Form == DW_FORM_data4;
4708 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
4709}
4710
4711/// SizeOf - Determine size of delta value in bytes.
4712///
4713unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4714 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004715 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004716}
4717
4718//===----------------------------------------------------------------------===//
4719
4720/// EmitValue - Emit debug information entry offset.
4721///
4722void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
4723 DD.getAsm()->EmitInt32(Entry->getOffset());
4724}
aslc200b112008-08-16 12:57:46 +00004725
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004726//===----------------------------------------------------------------------===//
4727
4728/// ComputeSize - calculate the size of the block.
4729///
4730unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
4731 if (!Size) {
Owen Anderson88dd6232008-06-24 21:44:59 +00004732 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004733
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004734 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4735 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
4736 }
4737 }
4738 return Size;
4739}
4740
4741/// EmitValue - Emit block data.
4742///
4743void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
4744 switch (Form) {
4745 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4746 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4747 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4748 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
4749 default: assert(0 && "Improper form for block"); break;
4750 }
aslc200b112008-08-16 12:57:46 +00004751
Owen Anderson88dd6232008-06-24 21:44:59 +00004752 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004753
4754 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4755 DD.getAsm()->EOL();
4756 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
4757 }
4758}
4759
4760/// SizeOf - Determine size of block data in bytes.
4761///
4762unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4763 switch (Form) {
4764 case DW_FORM_block1: return Size + sizeof(int8_t);
4765 case DW_FORM_block2: return Size + sizeof(int16_t);
4766 case DW_FORM_block4: return Size + sizeof(int32_t);
aslc200b112008-08-16 12:57:46 +00004767 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004768 default: assert(0 && "Improper form for block"); break;
4769 }
4770 return 0;
4771}
4772
4773//===----------------------------------------------------------------------===//
4774/// DIE Implementation
4775
4776DIE::~DIE() {
4777 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4778 delete Children[i];
4779}
aslc200b112008-08-16 12:57:46 +00004780
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004781/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4782///
4783void DIE::AddSiblingOffset() {
4784 DIEInteger *DI = new DIEInteger(0);
4785 Values.insert(Values.begin(), DI);
4786 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
4787}
4788
4789/// Profile - Used to gather unique data for the value folding set.
4790///
4791void DIE::Profile(FoldingSetNodeID &ID) {
4792 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00004793
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004794 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4795 ID.AddPointer(Children[i]);
4796
4797 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4798 ID.AddPointer(Values[j]);
4799}
4800
4801#ifndef NDEBUG
4802void DIE::print(std::ostream &O, unsigned IncIndent) {
4803 static unsigned IndentCount = 0;
4804 IndentCount += IncIndent;
4805 const std::string Indent(IndentCount, ' ');
4806 bool isBlock = Abbrev.getTag() == 0;
aslc200b112008-08-16 12:57:46 +00004807
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004808 if (!isBlock) {
4809 O << Indent
4810 << "Die: "
4811 << "0x" << std::hex << (intptr_t)this << std::dec
4812 << ", Offset: " << Offset
4813 << ", Size: " << Size
aslc200b112008-08-16 12:57:46 +00004814 << "\n";
4815
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004816 O << Indent
4817 << TagString(Abbrev.getTag())
4818 << " "
4819 << ChildrenString(Abbrev.getChildrenFlag());
4820 } else {
4821 O << "Size: " << Size;
4822 }
4823 O << "\n";
4824
Owen Anderson88dd6232008-06-24 21:44:59 +00004825 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004826
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004827 IndentCount += 2;
4828 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4829 O << Indent;
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004830
4831 if (!isBlock)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004832 O << AttributeString(Data[i].getAttribute());
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004833 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004834 O << "Blk[" << i << "]";
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004835
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004836 O << " "
4837 << FormEncodingString(Data[i].getForm())
4838 << " ";
4839 Values[i]->print(O);
4840 O << "\n";
4841 }
4842 IndentCount -= 2;
4843
4844 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4845 Children[j]->print(O, 4);
4846 }
aslc200b112008-08-16 12:57:46 +00004847
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004848 if (!isBlock) O << "\n";
4849 IndentCount -= IncIndent;
4850}
4851
4852void DIE::dump() {
4853 print(cerr);
4854}
4855#endif
4856
4857//===----------------------------------------------------------------------===//
4858/// DwarfWriter Implementation
4859///
4860
Owen Anderson847b99b2008-08-21 00:14:44 +00004861DwarfWriter::DwarfWriter(raw_ostream &OS, AsmPrinter *A,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004862 const TargetAsmInfo *T) {
4863 DE = new DwarfException(OS, A, T);
4864 DD = new DwarfDebug(OS, A, T);
4865}
4866
4867DwarfWriter::~DwarfWriter() {
4868 delete DE;
4869 delete DD;
4870}
4871
4872/// SetModuleInfo - Set machine module info when it's known that pass manager
4873/// has created it. Set by the target AsmPrinter.
4874void DwarfWriter::SetModuleInfo(MachineModuleInfo *MMI) {
4875 DD->SetModuleInfo(MMI);
4876 DE->SetModuleInfo(MMI);
4877}
4878
4879/// BeginModule - Emit all Dwarf sections that should come prior to the
4880/// content.
4881void DwarfWriter::BeginModule(Module *M) {
4882 DE->BeginModule(M);
4883 DD->BeginModule(M);
4884}
4885
4886/// EndModule - Emit all Dwarf sections that should come after the content.
4887///
4888void DwarfWriter::EndModule() {
4889 DE->EndModule();
4890 DD->EndModule();
4891}
4892
aslc200b112008-08-16 12:57:46 +00004893/// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004894/// emitted immediately after the function entry point.
4895void DwarfWriter::BeginFunction(MachineFunction *MF) {
4896 DE->BeginFunction(MF);
4897 DD->BeginFunction(MF);
4898}
4899
4900/// EndFunction - Gather and emit post-function debug information.
4901///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00004902void DwarfWriter::EndFunction(MachineFunction *MF) {
4903 DD->EndFunction(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004904 DE->EndFunction();
aslc200b112008-08-16 12:57:46 +00004905
Bill Wendling5b4796a2008-07-22 00:53:37 +00004906 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004907 // Clear function debug information.
4908 MMI->EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004909}