e740a56312e962cdd8555bfd8a592b4260d83f2c
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DIE.cpp
1 //===--- lib/CodeGen/DIE.cpp - DWARF Info Entries -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Data structures for DWARF info entries.
11 // 
12 //===----------------------------------------------------------------------===//
13
14 #include "DIE.h"
15 #include "DwarfPrinter.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/MC/MCAsmInfo.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/Target/TargetData.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/Format.h"
23 using namespace llvm;
24
25 //===----------------------------------------------------------------------===//
26 // DIEAbbrevData Implementation
27 //===----------------------------------------------------------------------===//
28
29 /// Profile - Used to gather unique data for the abbreviation folding set.
30 ///
31 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
32   ID.AddInteger(Attribute);
33   ID.AddInteger(Form);
34 }
35
36 //===----------------------------------------------------------------------===//
37 // DIEAbbrev Implementation
38 //===----------------------------------------------------------------------===//
39
40 /// Profile - Used to gather unique data for the abbreviation folding set.
41 ///
42 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
43   ID.AddInteger(Tag);
44   ID.AddInteger(ChildrenFlag);
45
46   // For each attribute description.
47   for (unsigned i = 0, N = Data.size(); i < N; ++i)
48     Data[i].Profile(ID);
49 }
50
51 /// Emit - Print the abbreviation using the specified asm printer.
52 ///
53 void DIEAbbrev::Emit(const AsmPrinter *Asm) const {
54   // Emit its Dwarf tag type.
55   Asm->EmitULEB128Bytes(Tag);
56   Asm->EOL(dwarf::TagString(Tag));
57
58   // Emit whether it has children DIEs.
59   Asm->EmitULEB128Bytes(ChildrenFlag);
60   Asm->EOL(dwarf::ChildrenString(ChildrenFlag));
61
62   // For each attribute description.
63   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
64     const DIEAbbrevData &AttrData = Data[i];
65
66     // Emit attribute type.
67     Asm->EmitULEB128Bytes(AttrData.getAttribute());
68     Asm->EOL(dwarf::AttributeString(AttrData.getAttribute()));
69
70     // Emit form type.
71     Asm->EmitULEB128Bytes(AttrData.getForm());
72     Asm->EOL(dwarf::FormEncodingString(AttrData.getForm()));
73   }
74
75   // Mark end of abbreviation.
76   Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(1)");
77   Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(2)");
78 }
79
80 #ifndef NDEBUG
81 void DIEAbbrev::print(raw_ostream &O) {
82   O << "Abbreviation @"
83     << format("0x%lx", (long)(intptr_t)this)
84     << "  "
85     << dwarf::TagString(Tag)
86     << " "
87     << dwarf::ChildrenString(ChildrenFlag)
88     << '\n';
89
90   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
91     O << "  "
92       << dwarf::AttributeString(Data[i].getAttribute())
93       << "  "
94       << dwarf::FormEncodingString(Data[i].getForm())
95       << '\n';
96   }
97 }
98 void DIEAbbrev::dump() { print(dbgs()); }
99 #endif
100
101 //===----------------------------------------------------------------------===//
102 // DIE Implementation
103 //===----------------------------------------------------------------------===//
104
105 DIE::~DIE() {
106   for (unsigned i = 0, N = Children.size(); i < N; ++i)
107     delete Children[i];
108 }
109
110 /// addSiblingOffset - Add a sibling offset field to the front of the DIE.
111 ///
112 void DIE::addSiblingOffset() {
113   DIEInteger *DI = new DIEInteger(0);
114   Values.insert(Values.begin(), DI);
115   Abbrev.AddFirstAttribute(dwarf::DW_AT_sibling, dwarf::DW_FORM_ref4);
116 }
117
118 #ifndef NDEBUG
119 void DIE::print(raw_ostream &O, unsigned IncIndent) {
120   IndentCount += IncIndent;
121   const std::string Indent(IndentCount, ' ');
122   bool isBlock = Abbrev.getTag() == 0;
123
124   if (!isBlock) {
125     O << Indent
126       << "Die: "
127       << format("0x%lx", (long)(intptr_t)this)
128       << ", Offset: " << Offset
129       << ", Size: " << Size
130       << "\n";
131
132     O << Indent
133       << dwarf::TagString(Abbrev.getTag())
134       << " "
135       << dwarf::ChildrenString(Abbrev.getChildrenFlag());
136   } else {
137     O << "Size: " << Size;
138   }
139   O << "\n";
140
141   const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
142
143   IndentCount += 2;
144   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
145     O << Indent;
146
147     if (!isBlock)
148       O << dwarf::AttributeString(Data[i].getAttribute());
149     else
150       O << "Blk[" << i << "]";
151
152     O <<  "  "
153       << dwarf::FormEncodingString(Data[i].getForm())
154       << " ";
155     Values[i]->print(O);
156     O << "\n";
157   }
158   IndentCount -= 2;
159
160   for (unsigned j = 0, M = Children.size(); j < M; ++j) {
161     Children[j]->print(O, 4);
162   }
163
164   if (!isBlock) O << "\n";
165   IndentCount -= IncIndent;
166 }
167
168 void DIE::dump() {
169   print(dbgs());
170 }
171 #endif
172
173
174 #ifndef NDEBUG
175 void DIEValue::dump() {
176   print(dbgs());
177 }
178 #endif
179
180 //===----------------------------------------------------------------------===//
181 // DIEInteger Implementation
182 //===----------------------------------------------------------------------===//
183
184 /// EmitValue - Emit integer of appropriate size.
185 ///
186 void DIEInteger::EmitValue(Dwarf *D, unsigned Form) const {
187   const AsmPrinter *Asm = D->getAsm();
188   switch (Form) {
189   case dwarf::DW_FORM_flag:  // Fall thru
190   case dwarf::DW_FORM_ref1:  // Fall thru
191   case dwarf::DW_FORM_data1: Asm->EmitInt8(Integer);         break;
192   case dwarf::DW_FORM_ref2:  // Fall thru
193   case dwarf::DW_FORM_data2: Asm->EmitInt16(Integer);        break;
194   case dwarf::DW_FORM_ref4:  // Fall thru
195   case dwarf::DW_FORM_data4: Asm->EmitInt32(Integer);        break;
196   case dwarf::DW_FORM_ref8:  // Fall thru
197   case dwarf::DW_FORM_data8: Asm->EmitInt64(Integer);        break;
198   case dwarf::DW_FORM_udata: Asm->EmitULEB128Bytes(Integer); break;
199   case dwarf::DW_FORM_sdata: Asm->EmitSLEB128Bytes(Integer); break;
200   default: llvm_unreachable("DIE Value form not supported yet");
201   }
202 }
203
204 /// SizeOf - Determine size of integer value in bytes.
205 ///
206 unsigned DIEInteger::SizeOf(const TargetData *TD, unsigned Form) const {
207   switch (Form) {
208   case dwarf::DW_FORM_flag:  // Fall thru
209   case dwarf::DW_FORM_ref1:  // Fall thru
210   case dwarf::DW_FORM_data1: return sizeof(int8_t);
211   case dwarf::DW_FORM_ref2:  // Fall thru
212   case dwarf::DW_FORM_data2: return sizeof(int16_t);
213   case dwarf::DW_FORM_ref4:  // Fall thru
214   case dwarf::DW_FORM_data4: return sizeof(int32_t);
215   case dwarf::DW_FORM_ref8:  // Fall thru
216   case dwarf::DW_FORM_data8: return sizeof(int64_t);
217   case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
218   case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
219   default: llvm_unreachable("DIE Value form not supported yet"); break;
220   }
221   return 0;
222 }
223
224 #ifndef NDEBUG
225 void DIEInteger::print(raw_ostream &O) {
226   O << "Int: " << (int64_t)Integer
227     << format("  0x%llx", (unsigned long long)Integer);
228 }
229 #endif
230
231 //===----------------------------------------------------------------------===//
232 // DIEString Implementation
233 //===----------------------------------------------------------------------===//
234
235 /// EmitValue - Emit string value.
236 ///
237 void DIEString::EmitValue(Dwarf *D, unsigned Form) const {
238   D->getAsm()->EmitString(Str);
239 }
240
241 #ifndef NDEBUG
242 void DIEString::print(raw_ostream &O) {
243   O << "Str: \"" << Str << "\"";
244 }
245 #endif
246
247 //===----------------------------------------------------------------------===//
248 // DIEDwarfLabel Implementation
249 //===----------------------------------------------------------------------===//
250
251 /// EmitValue - Emit label value.
252 ///
253 void DIEDwarfLabel::EmitValue(Dwarf *D, unsigned Form) const {
254   bool IsSmall = Form == dwarf::DW_FORM_data4;
255   D->EmitReference(Label, false, IsSmall);
256 }
257
258 /// SizeOf - Determine size of label value in bytes.
259 ///
260 unsigned DIEDwarfLabel::SizeOf(const TargetData *TD, unsigned Form) const {
261   if (Form == dwarf::DW_FORM_data4) return 4;
262   return TD->getPointerSize();
263 }
264
265 #ifndef NDEBUG
266 void DIEDwarfLabel::print(raw_ostream &O) {
267   O << "Lbl: ";
268   Label.print(O);
269 }
270 #endif
271
272 //===----------------------------------------------------------------------===//
273 // DIEObjectLabel Implementation
274 //===----------------------------------------------------------------------===//
275
276 /// EmitValue - Emit label value.
277 ///
278 void DIEObjectLabel::EmitValue(Dwarf *D, unsigned Form) const {
279   bool IsSmall = Form == dwarf::DW_FORM_data4;
280   D->EmitReference(Sym, false, IsSmall);
281 }
282
283 /// SizeOf - Determine size of label value in bytes.
284 ///
285 unsigned DIEObjectLabel::SizeOf(const TargetData *TD, unsigned Form) const {
286   if (Form == dwarf::DW_FORM_data4) return 4;
287   return TD->getPointerSize();
288 }
289
290 #ifndef NDEBUG
291 void DIEObjectLabel::print(raw_ostream &O) {
292   O << "Obj: " << Sym->getName();
293 }
294 #endif
295
296 //===----------------------------------------------------------------------===//
297 // DIESectionOffset Implementation
298 //===----------------------------------------------------------------------===//
299
300 /// EmitValue - Emit delta value.
301 ///
302 void DIESectionOffset::EmitValue(Dwarf *D, unsigned Form) const {
303   bool IsSmall = Form == dwarf::DW_FORM_data4;
304   D->EmitSectionOffset(Label.getTag(), Section.getTag(),
305                        Label.getNumber(), Section.getNumber(),
306                        IsSmall, IsEH, UseSet);
307 }
308
309 /// SizeOf - Determine size of delta value in bytes.
310 ///
311 unsigned DIESectionOffset::SizeOf(const TargetData *TD, unsigned Form) const {
312   if (Form == dwarf::DW_FORM_data4) return 4;
313   return TD->getPointerSize();
314 }
315
316 #ifndef NDEBUG
317 void DIESectionOffset::print(raw_ostream &O) {
318   O << "Off: ";
319   Label.print(O);
320   O << "-";
321   Section.print(O);
322   O << "-" << IsEH << "-" << UseSet;
323 }
324 #endif
325
326 //===----------------------------------------------------------------------===//
327 // DIEDelta Implementation
328 //===----------------------------------------------------------------------===//
329
330 /// EmitValue - Emit delta value.
331 ///
332 void DIEDelta::EmitValue(Dwarf *D, unsigned Form) const {
333   bool IsSmall = Form == dwarf::DW_FORM_data4;
334   D->EmitDifference(LabelHi, LabelLo, IsSmall);
335 }
336
337 /// SizeOf - Determine size of delta value in bytes.
338 ///
339 unsigned DIEDelta::SizeOf(const TargetData *TD, unsigned Form) const {
340   if (Form == dwarf::DW_FORM_data4) return 4;
341   return TD->getPointerSize();
342 }
343
344 #ifndef NDEBUG
345 void DIEDelta::print(raw_ostream &O) {
346   O << "Del: ";
347   LabelHi.print(O);
348   O << "-";
349   LabelLo.print(O);
350 }
351 #endif
352
353 //===----------------------------------------------------------------------===//
354 // DIEEntry Implementation
355 //===----------------------------------------------------------------------===//
356
357 /// EmitValue - Emit debug information entry offset.
358 ///
359 void DIEEntry::EmitValue(Dwarf *D, unsigned Form) const {
360   D->getAsm()->EmitInt32(Entry->getOffset());
361 }
362
363 #ifndef NDEBUG
364 void DIEEntry::print(raw_ostream &O) {
365   O << format("Die: 0x%lx", (long)(intptr_t)Entry);
366 }
367 #endif
368
369 //===----------------------------------------------------------------------===//
370 // DIEBlock Implementation
371 //===----------------------------------------------------------------------===//
372
373 /// ComputeSize - calculate the size of the block.
374 ///
375 unsigned DIEBlock::ComputeSize(const TargetData *TD) {
376   if (!Size) {
377     const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
378     for (unsigned i = 0, N = Values.size(); i < N; ++i)
379       Size += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
380   }
381
382   return Size;
383 }
384
385 /// EmitValue - Emit block data.
386 ///
387 void DIEBlock::EmitValue(Dwarf *D, unsigned Form) const {
388   const AsmPrinter *Asm = D->getAsm();
389   switch (Form) {
390   case dwarf::DW_FORM_block1: Asm->EmitInt8(Size);         break;
391   case dwarf::DW_FORM_block2: Asm->EmitInt16(Size);        break;
392   case dwarf::DW_FORM_block4: Asm->EmitInt32(Size);        break;
393   case dwarf::DW_FORM_block:  Asm->EmitULEB128Bytes(Size); break;
394   default: llvm_unreachable("Improper form for block");         break;
395   }
396
397   const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
398   for (unsigned i = 0, N = Values.size(); i < N; ++i) {
399     Asm->EOL();
400     Values[i]->EmitValue(D, AbbrevData[i].getForm());
401   }
402 }
403
404 /// SizeOf - Determine size of block data in bytes.
405 ///
406 unsigned DIEBlock::SizeOf(const TargetData *TD, unsigned Form) const {
407   switch (Form) {
408   case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
409   case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
410   case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
411   case dwarf::DW_FORM_block: return Size + MCAsmInfo::getULEB128Size(Size);
412   default: llvm_unreachable("Improper form for block"); break;
413   }
414   return 0;
415 }
416
417 #ifndef NDEBUG
418 void DIEBlock::print(raw_ostream &O) {
419   O << "Blk: ";
420   DIE::print(O, 5);
421 }
422 #endif