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/ADT/Twine.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/Target/TargetData.h"
22 #include "llvm/Support/Allocator.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/Format.h"
26 #include "llvm/Support/FormattedStream.h"
29 //===----------------------------------------------------------------------===//
30 // DIEAbbrevData Implementation
31 //===----------------------------------------------------------------------===//
33 /// Profile - Used to gather unique data for the abbreviation folding set.
35 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
36 ID.AddInteger(Attribute);
40 //===----------------------------------------------------------------------===//
41 // DIEAbbrev Implementation
42 //===----------------------------------------------------------------------===//
44 /// Profile - Used to gather unique data for the abbreviation folding set.
46 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
48 ID.AddInteger(ChildrenFlag);
50 // For each attribute description.
51 for (unsigned i = 0, N = Data.size(); i < N; ++i)
55 /// Emit - Print the abbreviation using the specified asm printer.
57 void DIEAbbrev::Emit(const DwarfPrinter *DP) const {
58 // Emit its Dwarf tag type.
59 // FIXME: Doing work even in non-asm-verbose runs.
60 DP->getAsm()->EmitULEB128(Tag, dwarf::TagString(Tag));
62 // Emit whether it has children DIEs.
63 // FIXME: Doing work even in non-asm-verbose runs.
64 DP->getAsm()->EmitULEB128(ChildrenFlag, dwarf::ChildrenString(ChildrenFlag));
66 // For each attribute description.
67 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
68 const DIEAbbrevData &AttrData = Data[i];
70 // Emit attribute type.
71 // FIXME: Doing work even in non-asm-verbose runs.
72 DP->getAsm()->EmitULEB128(AttrData.getAttribute(),
73 dwarf::AttributeString(AttrData.getAttribute()));
76 // FIXME: Doing work even in non-asm-verbose runs.
77 DP->getAsm()->EmitULEB128(AttrData.getForm(),
78 dwarf::FormEncodingString(AttrData.getForm()));
81 // Mark end of abbreviation.
82 DP->getAsm()->EmitULEB128(0, "EOM(1)");
83 DP->getAsm()->EmitULEB128(0, "EOM(2)");
87 void DIEAbbrev::print(raw_ostream &O) {
89 << format("0x%lx", (long)(intptr_t)this)
91 << dwarf::TagString(Tag)
93 << dwarf::ChildrenString(ChildrenFlag)
96 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
98 << dwarf::AttributeString(Data[i].getAttribute())
100 << dwarf::FormEncodingString(Data[i].getForm())
104 void DIEAbbrev::dump() { print(dbgs()); }
107 //===----------------------------------------------------------------------===//
108 // DIE Implementation
109 //===----------------------------------------------------------------------===//
112 for (unsigned i = 0, N = Children.size(); i < N; ++i)
116 /// addSiblingOffset - Add a sibling offset field to the front of the DIE.
118 DIEValue *DIE::addSiblingOffset(BumpPtrAllocator &A) {
119 DIEInteger *DI = new (A) DIEInteger(0);
120 Values.insert(Values.begin(), DI);
121 Abbrev.AddFirstAttribute(dwarf::DW_AT_sibling, dwarf::DW_FORM_ref4);
126 void DIE::print(raw_ostream &O, unsigned IncIndent) {
127 IndentCount += IncIndent;
128 const std::string Indent(IndentCount, ' ');
129 bool isBlock = Abbrev.getTag() == 0;
134 << format("0x%lx", (long)(intptr_t)this)
135 << ", Offset: " << Offset
136 << ", Size: " << Size << "\n";
139 << dwarf::TagString(Abbrev.getTag())
141 << dwarf::ChildrenString(Abbrev.getChildrenFlag()) << "\n";
143 O << "Size: " << Size << "\n";
146 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
149 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
153 O << dwarf::AttributeString(Data[i].getAttribute());
155 O << "Blk[" << i << "]";
158 << dwarf::FormEncodingString(Data[i].getForm())
165 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
166 Children[j]->print(O, 4);
169 if (!isBlock) O << "\n";
170 IndentCount -= IncIndent;
180 void DIEValue::dump() {
185 //===----------------------------------------------------------------------===//
186 // DIEInteger Implementation
187 //===----------------------------------------------------------------------===//
189 /// EmitValue - Emit integer of appropriate size.
191 void DIEInteger::EmitValue(DwarfPrinter *D, unsigned Form) const {
192 const AsmPrinter *Asm = D->getAsm();
195 case dwarf::DW_FORM_flag: // Fall thru
196 case dwarf::DW_FORM_ref1: // Fall thru
197 case dwarf::DW_FORM_data1: Size = 1; break;
198 case dwarf::DW_FORM_ref2: // Fall thru
199 case dwarf::DW_FORM_data2: Size = 2; break;
200 case dwarf::DW_FORM_ref4: // Fall thru
201 case dwarf::DW_FORM_data4: Size = 4; break;
202 case dwarf::DW_FORM_ref8: // Fall thru
203 case dwarf::DW_FORM_data8: Size = 8; break;
204 case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
205 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
206 default: llvm_unreachable("DIE Value form not supported yet");
208 Asm->OutStreamer.EmitIntValue(Integer, Size, 0/*addrspace*/);
211 /// SizeOf - Determine size of integer value in bytes.
213 unsigned DIEInteger::SizeOf(const TargetData *TD, unsigned Form) const {
215 case dwarf::DW_FORM_flag: // Fall thru
216 case dwarf::DW_FORM_ref1: // Fall thru
217 case dwarf::DW_FORM_data1: return sizeof(int8_t);
218 case dwarf::DW_FORM_ref2: // Fall thru
219 case dwarf::DW_FORM_data2: return sizeof(int16_t);
220 case dwarf::DW_FORM_ref4: // Fall thru
221 case dwarf::DW_FORM_data4: return sizeof(int32_t);
222 case dwarf::DW_FORM_ref8: // Fall thru
223 case dwarf::DW_FORM_data8: return sizeof(int64_t);
224 case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
225 case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
226 default: llvm_unreachable("DIE Value form not supported yet"); break;
232 void DIEInteger::print(raw_ostream &O) {
233 O << "Int: " << (int64_t)Integer
234 << format(" 0x%llx", (unsigned long long)Integer);
238 //===----------------------------------------------------------------------===//
239 // DIEString Implementation
240 //===----------------------------------------------------------------------===//
242 /// EmitValue - Emit string value.
244 void DIEString::EmitValue(DwarfPrinter *D, unsigned Form) const {
245 D->getAsm()->OutStreamer.EmitBytes(Str, /*addrspace*/0);
246 // Emit nul terminator.
247 D->getAsm()->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0);
251 void DIEString::print(raw_ostream &O) {
252 O << "Str: \"" << Str << "\"";
256 //===----------------------------------------------------------------------===//
257 // DIELabel Implementation
258 //===----------------------------------------------------------------------===//
260 /// EmitValue - Emit label value.
262 void DIELabel::EmitValue(DwarfPrinter *D, unsigned Form) const {
263 bool IsSmall = Form == dwarf::DW_FORM_data4;
264 unsigned Size = IsSmall ? 4 : D->getTargetData()->getPointerSize();
265 D->getAsm()->OutStreamer.EmitSymbolValue(Label, Size, 0/*AddrSpace*/);
268 /// SizeOf - Determine size of label value in bytes.
270 unsigned DIELabel::SizeOf(const TargetData *TD, unsigned Form) const {
271 if (Form == dwarf::DW_FORM_data4) return 4;
272 return TD->getPointerSize();
276 void DIELabel::print(raw_ostream &O) {
277 O << "Lbl: " << Label->getName();
281 //===----------------------------------------------------------------------===//
282 // DIEDelta Implementation
283 //===----------------------------------------------------------------------===//
285 /// EmitValue - Emit delta value.
287 void DIEDelta::EmitValue(DwarfPrinter *D, unsigned Form) const {
288 bool IsSmall = Form == dwarf::DW_FORM_data4;
289 D->EmitDifference(LabelHi, LabelLo, IsSmall);
292 /// SizeOf - Determine size of delta value in bytes.
294 unsigned DIEDelta::SizeOf(const TargetData *TD, unsigned Form) const {
295 if (Form == dwarf::DW_FORM_data4) return 4;
296 return TD->getPointerSize();
300 void DIEDelta::print(raw_ostream &O) {
301 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
305 //===----------------------------------------------------------------------===//
306 // DIEEntry Implementation
307 //===----------------------------------------------------------------------===//
309 /// EmitValue - Emit debug information entry offset.
311 void DIEEntry::EmitValue(DwarfPrinter *D, unsigned Form) const {
312 D->getAsm()->EmitInt32(Entry->getOffset());
316 void DIEEntry::print(raw_ostream &O) {
317 O << format("Die: 0x%lx", (long)(intptr_t)Entry);
321 //===----------------------------------------------------------------------===//
322 // DIEBlock Implementation
323 //===----------------------------------------------------------------------===//
325 /// ComputeSize - calculate the size of the block.
327 unsigned DIEBlock::ComputeSize(const TargetData *TD) {
329 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
330 for (unsigned i = 0, N = Values.size(); i < N; ++i)
331 Size += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
337 /// EmitValue - Emit block data.
339 void DIEBlock::EmitValue(DwarfPrinter *D, unsigned Form) const {
340 const AsmPrinter *Asm = D->getAsm();
342 default: assert(0 && "Improper form for block"); break;
343 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
344 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
345 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
346 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
349 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
350 for (unsigned i = 0, N = Values.size(); i < N; ++i)
351 Values[i]->EmitValue(D, AbbrevData[i].getForm());
354 /// SizeOf - Determine size of block data in bytes.
356 unsigned DIEBlock::SizeOf(const TargetData *TD, unsigned Form) const {
358 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
359 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
360 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
361 case dwarf::DW_FORM_block: return Size + MCAsmInfo::getULEB128Size(Size);
362 default: llvm_unreachable("Improper form for block"); break;
368 void DIEBlock::print(raw_ostream &O) {