1 //===--- lib/CodeGen/DIE.cpp - DWARF Info Entries -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Data structures for DWARF info entries.
12 //===----------------------------------------------------------------------===//
15 #include "DwarfPrinter.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/MC/MCAsmInfo.h"
18 #include "llvm/Target/TargetData.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/Format.h"
23 //===----------------------------------------------------------------------===//
24 // DIEAbbrevData Implementation
25 //===----------------------------------------------------------------------===//
27 /// Profile - Used to gather unique data for the abbreviation folding set.
29 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
30 ID.AddInteger(Attribute);
34 //===----------------------------------------------------------------------===//
35 // DIEAbbrev Implementation
36 //===----------------------------------------------------------------------===//
38 /// Profile - Used to gather unique data for the abbreviation folding set.
40 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
42 ID.AddInteger(ChildrenFlag);
44 // For each attribute description.
45 for (unsigned i = 0, N = Data.size(); i < N; ++i)
49 /// Emit - Print the abbreviation using the specified asm printer.
51 void DIEAbbrev::Emit(const AsmPrinter *Asm) const {
52 // Emit its Dwarf tag type.
53 Asm->EmitULEB128Bytes(Tag);
54 Asm->EOL(dwarf::TagString(Tag));
56 // Emit whether it has children DIEs.
57 Asm->EmitULEB128Bytes(ChildrenFlag);
58 Asm->EOL(dwarf::ChildrenString(ChildrenFlag));
60 // For each attribute description.
61 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
62 const DIEAbbrevData &AttrData = Data[i];
64 // Emit attribute type.
65 Asm->EmitULEB128Bytes(AttrData.getAttribute());
66 Asm->EOL(dwarf::AttributeString(AttrData.getAttribute()));
69 Asm->EmitULEB128Bytes(AttrData.getForm());
70 Asm->EOL(dwarf::FormEncodingString(AttrData.getForm()));
73 // Mark end of abbreviation.
74 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(1)");
75 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(2)");
79 void DIEAbbrev::print(raw_ostream &O) {
81 << format("0x%lx", (long)(intptr_t)this)
83 << dwarf::TagString(Tag)
85 << dwarf::ChildrenString(ChildrenFlag)
88 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
90 << dwarf::AttributeString(Data[i].getAttribute())
92 << dwarf::FormEncodingString(Data[i].getForm())
96 void DIEAbbrev::dump() { print(errs()); }
99 //===----------------------------------------------------------------------===//
100 // DIE Implementation
101 //===----------------------------------------------------------------------===//
104 for (unsigned i = 0, N = Children.size(); i < N; ++i)
108 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
110 void DIE::AddSiblingOffset() {
111 DIEInteger *DI = new DIEInteger(0);
112 Values.insert(Values.begin(), DI);
113 Abbrev.AddFirstAttribute(dwarf::DW_AT_sibling, dwarf::DW_FORM_ref4);
117 void DIE::print(raw_ostream &O, unsigned IncIndent) {
118 IndentCount += IncIndent;
119 const std::string Indent(IndentCount, ' ');
120 bool isBlock = Abbrev.getTag() == 0;
125 << format("0x%lx", (long)(intptr_t)this)
126 << ", Offset: " << Offset
127 << ", Size: " << Size
131 << dwarf::TagString(Abbrev.getTag())
133 << dwarf::ChildrenString(Abbrev.getChildrenFlag());
135 O << "Size: " << Size;
139 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
142 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
146 O << dwarf::AttributeString(Data[i].getAttribute());
148 O << "Blk[" << i << "]";
151 << dwarf::FormEncodingString(Data[i].getForm())
158 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
159 Children[j]->print(O, 4);
162 if (!isBlock) O << "\n";
163 IndentCount -= IncIndent;
173 void DIEValue::dump() {
178 //===----------------------------------------------------------------------===//
179 // DIEInteger Implementation
180 //===----------------------------------------------------------------------===//
182 /// EmitValue - Emit integer of appropriate size.
184 void DIEInteger::EmitValue(Dwarf *D, unsigned Form) const {
185 const AsmPrinter *Asm = D->getAsm();
187 case dwarf::DW_FORM_flag: // Fall thru
188 case dwarf::DW_FORM_ref1: // Fall thru
189 case dwarf::DW_FORM_data1: Asm->EmitInt8(Integer); break;
190 case dwarf::DW_FORM_ref2: // Fall thru
191 case dwarf::DW_FORM_data2: Asm->EmitInt16(Integer); break;
192 case dwarf::DW_FORM_ref4: // Fall thru
193 case dwarf::DW_FORM_data4: Asm->EmitInt32(Integer); break;
194 case dwarf::DW_FORM_ref8: // Fall thru
195 case dwarf::DW_FORM_data8: Asm->EmitInt64(Integer); break;
196 case dwarf::DW_FORM_udata: Asm->EmitULEB128Bytes(Integer); break;
197 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128Bytes(Integer); break;
198 default: llvm_unreachable("DIE Value form not supported yet");
202 /// SizeOf - Determine size of integer value in bytes.
204 unsigned DIEInteger::SizeOf(const TargetData *TD, unsigned Form) const {
206 case dwarf::DW_FORM_flag: // Fall thru
207 case dwarf::DW_FORM_ref1: // Fall thru
208 case dwarf::DW_FORM_data1: return sizeof(int8_t);
209 case dwarf::DW_FORM_ref2: // Fall thru
210 case dwarf::DW_FORM_data2: return sizeof(int16_t);
211 case dwarf::DW_FORM_ref4: // Fall thru
212 case dwarf::DW_FORM_data4: return sizeof(int32_t);
213 case dwarf::DW_FORM_ref8: // Fall thru
214 case dwarf::DW_FORM_data8: return sizeof(int64_t);
215 case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
216 case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
217 default: llvm_unreachable("DIE Value form not supported yet"); break;
223 void DIEInteger::print(raw_ostream &O) {
224 O << "Int: " << (int64_t)Integer
225 << format(" 0x%llx", (unsigned long long)Integer);
229 //===----------------------------------------------------------------------===//
230 // DIEString Implementation
231 //===----------------------------------------------------------------------===//
233 /// EmitValue - Emit string value.
235 void DIEString::EmitValue(Dwarf *D, unsigned Form) const {
236 D->getAsm()->EmitString(Str);
240 void DIEString::print(raw_ostream &O) {
241 O << "Str: \"" << Str << "\"";
245 //===----------------------------------------------------------------------===//
246 // DIEDwarfLabel Implementation
247 //===----------------------------------------------------------------------===//
249 /// EmitValue - Emit label value.
251 void DIEDwarfLabel::EmitValue(Dwarf *D, unsigned Form) const {
252 bool IsSmall = Form == dwarf::DW_FORM_data4;
253 D->EmitReference(Label, false, IsSmall);
256 /// SizeOf - Determine size of label value in bytes.
258 unsigned DIEDwarfLabel::SizeOf(const TargetData *TD, unsigned Form) const {
259 if (Form == dwarf::DW_FORM_data4) return 4;
260 return TD->getPointerSize();
264 void DIEDwarfLabel::print(raw_ostream &O) {
270 //===----------------------------------------------------------------------===//
271 // DIEObjectLabel Implementation
272 //===----------------------------------------------------------------------===//
274 /// EmitValue - Emit label value.
276 void DIEObjectLabel::EmitValue(Dwarf *D, unsigned Form) const {
277 bool IsSmall = Form == dwarf::DW_FORM_data4;
278 D->EmitReference(Label, false, IsSmall);
281 /// SizeOf - Determine size of label value in bytes.
283 unsigned DIEObjectLabel::SizeOf(const TargetData *TD, unsigned Form) const {
284 if (Form == dwarf::DW_FORM_data4) return 4;
285 return TD->getPointerSize();
289 void DIEObjectLabel::print(raw_ostream &O) {
290 O << "Obj: " << Label;
294 //===----------------------------------------------------------------------===//
295 // DIESectionOffset Implementation
296 //===----------------------------------------------------------------------===//
298 /// EmitValue - Emit delta value.
300 void DIESectionOffset::EmitValue(Dwarf *D, unsigned Form) const {
301 bool IsSmall = Form == dwarf::DW_FORM_data4;
302 D->EmitSectionOffset(Label.getTag(), Section.getTag(),
303 Label.getNumber(), Section.getNumber(),
304 IsSmall, IsEH, UseSet);
307 /// SizeOf - Determine size of delta value in bytes.
309 unsigned DIESectionOffset::SizeOf(const TargetData *TD, unsigned Form) const {
310 if (Form == dwarf::DW_FORM_data4) return 4;
311 return TD->getPointerSize();
315 void DIESectionOffset::print(raw_ostream &O) {
320 O << "-" << IsEH << "-" << UseSet;
324 //===----------------------------------------------------------------------===//
325 // DIEDelta Implementation
326 //===----------------------------------------------------------------------===//
328 /// EmitValue - Emit delta value.
330 void DIEDelta::EmitValue(Dwarf *D, unsigned Form) const {
331 bool IsSmall = Form == dwarf::DW_FORM_data4;
332 D->EmitDifference(LabelHi, LabelLo, IsSmall);
335 /// SizeOf - Determine size of delta value in bytes.
337 unsigned DIEDelta::SizeOf(const TargetData *TD, unsigned Form) const {
338 if (Form == dwarf::DW_FORM_data4) return 4;
339 return TD->getPointerSize();
343 void DIEDelta::print(raw_ostream &O) {
351 //===----------------------------------------------------------------------===//
352 // DIEEntry Implementation
353 //===----------------------------------------------------------------------===//
355 /// EmitValue - Emit debug information entry offset.
357 void DIEEntry::EmitValue(Dwarf *D, unsigned Form) const {
358 D->getAsm()->EmitInt32(Entry->getOffset());
362 void DIEEntry::print(raw_ostream &O) {
363 O << format("Die: 0x%lx", (long)(intptr_t)Entry);
367 //===----------------------------------------------------------------------===//
368 // DIEBlock Implementation
369 //===----------------------------------------------------------------------===//
371 /// ComputeSize - calculate the size of the block.
373 unsigned DIEBlock::ComputeSize(const TargetData *TD) {
375 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
376 for (unsigned i = 0, N = Values.size(); i < N; ++i)
377 Size += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
383 /// EmitValue - Emit block data.
385 void DIEBlock::EmitValue(Dwarf *D, unsigned Form) const {
386 const AsmPrinter *Asm = D->getAsm();
388 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
389 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
390 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
391 case dwarf::DW_FORM_block: Asm->EmitULEB128Bytes(Size); break;
392 default: llvm_unreachable("Improper form for block"); break;
395 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
396 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
398 Values[i]->EmitValue(D, AbbrevData[i].getForm());
402 /// SizeOf - Determine size of block data in bytes.
404 unsigned DIEBlock::SizeOf(const TargetData *TD, unsigned Form) const {
406 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
407 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
408 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
409 case dwarf::DW_FORM_block: return Size + MCAsmInfo::getULEB128Size(Size);
410 default: llvm_unreachable("Improper form for block"); break;
416 void DIEBlock::print(raw_ostream &O) {