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/Debug.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/Format.h"
25 #include "llvm/Support/FormattedStream.h"
28 //===----------------------------------------------------------------------===//
29 // DIEAbbrevData Implementation
30 //===----------------------------------------------------------------------===//
32 /// Profile - Used to gather unique data for the abbreviation folding set.
34 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
35 ID.AddInteger(Attribute);
39 //===----------------------------------------------------------------------===//
40 // DIEAbbrev Implementation
41 //===----------------------------------------------------------------------===//
43 /// Profile - Used to gather unique data for the abbreviation folding set.
45 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
47 ID.AddInteger(ChildrenFlag);
49 // For each attribute description.
50 for (unsigned i = 0, N = Data.size(); i < N; ++i)
54 /// Emit - Print the abbreviation using the specified asm printer.
56 void DIEAbbrev::Emit(const AsmPrinter *Asm) const {
57 // Emit its Dwarf tag type.
58 Asm->EmitULEB128Bytes(Tag);
59 Asm->EOL(dwarf::TagString(Tag));
61 // Emit whether it has children DIEs.
62 Asm->EmitULEB128Bytes(ChildrenFlag);
63 Asm->EOL(dwarf::ChildrenString(ChildrenFlag));
65 // For each attribute description.
66 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
67 const DIEAbbrevData &AttrData = Data[i];
69 // Emit attribute type.
70 Asm->EmitULEB128Bytes(AttrData.getAttribute());
71 Asm->EOL(dwarf::AttributeString(AttrData.getAttribute()));
74 Asm->EmitULEB128Bytes(AttrData.getForm());
75 Asm->EOL(dwarf::FormEncodingString(AttrData.getForm()));
78 // Mark end of abbreviation.
79 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(1)");
80 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(2)");
84 void DIEAbbrev::print(raw_ostream &O) {
86 << format("0x%lx", (long)(intptr_t)this)
88 << dwarf::TagString(Tag)
90 << dwarf::ChildrenString(ChildrenFlag)
93 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
95 << dwarf::AttributeString(Data[i].getAttribute())
97 << dwarf::FormEncodingString(Data[i].getForm())
101 void DIEAbbrev::dump() { print(dbgs()); }
104 //===----------------------------------------------------------------------===//
105 // DIE Implementation
106 //===----------------------------------------------------------------------===//
109 for (unsigned i = 0, N = Children.size(); i < N; ++i)
113 /// addSiblingOffset - Add a sibling offset field to the front of the DIE.
115 void DIE::addSiblingOffset() {
116 DIEInteger *DI = new DIEInteger(0);
117 Values.insert(Values.begin(), DI);
118 Abbrev.AddFirstAttribute(dwarf::DW_AT_sibling, dwarf::DW_FORM_ref4);
122 void DIE::print(raw_ostream &O, unsigned IncIndent) {
123 IndentCount += IncIndent;
124 const std::string Indent(IndentCount, ' ');
125 bool isBlock = Abbrev.getTag() == 0;
130 << format("0x%lx", (long)(intptr_t)this)
131 << ", Offset: " << Offset
132 << ", Size: " << Size
136 << dwarf::TagString(Abbrev.getTag())
138 << dwarf::ChildrenString(Abbrev.getChildrenFlag());
140 O << "Size: " << Size;
144 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
147 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
151 O << dwarf::AttributeString(Data[i].getAttribute());
153 O << "Blk[" << i << "]";
156 << dwarf::FormEncodingString(Data[i].getForm())
163 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
164 Children[j]->print(O, 4);
167 if (!isBlock) O << "\n";
168 IndentCount -= IncIndent;
178 void DIEValue::dump() {
183 //===----------------------------------------------------------------------===//
184 // DIEInteger Implementation
185 //===----------------------------------------------------------------------===//
187 /// EmitValue - Emit integer of appropriate size.
189 void DIEInteger::EmitValue(DwarfPrinter *D, unsigned Form) const {
190 const AsmPrinter *Asm = D->getAsm();
193 case dwarf::DW_FORM_flag: // Fall thru
194 case dwarf::DW_FORM_ref1: // Fall thru
195 case dwarf::DW_FORM_data1: Size = 1; break;
196 case dwarf::DW_FORM_ref2: // Fall thru
197 case dwarf::DW_FORM_data2: Size = 2; break;
198 case dwarf::DW_FORM_ref4: // Fall thru
199 case dwarf::DW_FORM_data4: Size = 4; break;
200 case dwarf::DW_FORM_ref8: // Fall thru
201 case dwarf::DW_FORM_data8: Size = 8; break;
202 case dwarf::DW_FORM_udata: Asm->EmitULEB128Bytes(Integer); return;
203 case dwarf::DW_FORM_sdata: D->EmitSLEB128(Integer, ""); return;
204 default: llvm_unreachable("DIE Value form not supported yet");
206 Asm->OutStreamer.EmitIntValue(Integer, Size, 0/*addrspace*/);
209 /// SizeOf - Determine size of integer value in bytes.
211 unsigned DIEInteger::SizeOf(const TargetData *TD, unsigned Form) const {
213 case dwarf::DW_FORM_flag: // Fall thru
214 case dwarf::DW_FORM_ref1: // Fall thru
215 case dwarf::DW_FORM_data1: return sizeof(int8_t);
216 case dwarf::DW_FORM_ref2: // Fall thru
217 case dwarf::DW_FORM_data2: return sizeof(int16_t);
218 case dwarf::DW_FORM_ref4: // Fall thru
219 case dwarf::DW_FORM_data4: return sizeof(int32_t);
220 case dwarf::DW_FORM_ref8: // Fall thru
221 case dwarf::DW_FORM_data8: return sizeof(int64_t);
222 case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
223 case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
224 default: llvm_unreachable("DIE Value form not supported yet"); break;
230 void DIEInteger::print(raw_ostream &O) {
231 O << "Int: " << (int64_t)Integer
232 << format(" 0x%llx", (unsigned long long)Integer);
236 //===----------------------------------------------------------------------===//
237 // DIEString Implementation
238 //===----------------------------------------------------------------------===//
240 /// EmitValue - Emit string value.
242 void DIEString::EmitValue(DwarfPrinter *D, unsigned Form) const {
243 D->getAsm()->EmitString(Str);
247 void DIEString::print(raw_ostream &O) {
248 O << "Str: \"" << Str << "\"";
252 //===----------------------------------------------------------------------===//
253 // DIEDwarfLabel Implementation
254 //===----------------------------------------------------------------------===//
256 /// EmitValue - Emit label value.
258 void DIEDwarfLabel::EmitValue(DwarfPrinter *D, unsigned Form) const {
259 bool IsSmall = Form == dwarf::DW_FORM_data4;
260 D->EmitReference(Label, false, IsSmall);
263 /// SizeOf - Determine size of label value in bytes.
265 unsigned DIEDwarfLabel::SizeOf(const TargetData *TD, unsigned Form) const {
266 if (Form == dwarf::DW_FORM_data4) return 4;
267 return TD->getPointerSize();
271 void DIEDwarfLabel::print(raw_ostream &O) {
277 //===----------------------------------------------------------------------===//
278 // DIEObjectLabel Implementation
279 //===----------------------------------------------------------------------===//
281 /// EmitValue - Emit label value.
283 void DIEObjectLabel::EmitValue(DwarfPrinter *D, unsigned Form) const {
284 bool IsSmall = Form == dwarf::DW_FORM_data4;
285 D->EmitReference(Sym, false, IsSmall);
288 /// SizeOf - Determine size of label value in bytes.
290 unsigned DIEObjectLabel::SizeOf(const TargetData *TD, unsigned Form) const {
291 if (Form == dwarf::DW_FORM_data4) return 4;
292 return TD->getPointerSize();
296 void DIEObjectLabel::print(raw_ostream &O) {
297 O << "Obj: " << Sym->getName();
301 //===----------------------------------------------------------------------===//
302 // DIESectionOffset Implementation
303 //===----------------------------------------------------------------------===//
305 /// EmitValue - Emit delta value.
307 void DIESectionOffset::EmitValue(DwarfPrinter *D, unsigned Form) const {
308 bool IsSmall = Form == dwarf::DW_FORM_data4;
309 D->EmitSectionOffset(Label.getTag(), Section.getTag(),
310 Label.getNumber(), Section.getNumber(),
311 IsSmall, IsEH, UseSet);
314 /// SizeOf - Determine size of delta value in bytes.
316 unsigned DIESectionOffset::SizeOf(const TargetData *TD, unsigned Form) const {
317 if (Form == dwarf::DW_FORM_data4) return 4;
318 return TD->getPointerSize();
322 void DIESectionOffset::print(raw_ostream &O) {
327 O << "-" << IsEH << "-" << UseSet;
331 //===----------------------------------------------------------------------===//
332 // DIEDelta Implementation
333 //===----------------------------------------------------------------------===//
335 /// EmitValue - Emit delta value.
337 void DIEDelta::EmitValue(DwarfPrinter *D, unsigned Form) const {
338 bool IsSmall = Form == dwarf::DW_FORM_data4;
339 D->EmitDifference(LabelHi, LabelLo, IsSmall);
342 /// SizeOf - Determine size of delta value in bytes.
344 unsigned DIEDelta::SizeOf(const TargetData *TD, unsigned Form) const {
345 if (Form == dwarf::DW_FORM_data4) return 4;
346 return TD->getPointerSize();
350 void DIEDelta::print(raw_ostream &O) {
358 //===----------------------------------------------------------------------===//
359 // DIEEntry Implementation
360 //===----------------------------------------------------------------------===//
362 /// EmitValue - Emit debug information entry offset.
364 void DIEEntry::EmitValue(DwarfPrinter *D, unsigned Form) const {
365 D->getAsm()->EmitInt32(Entry->getOffset());
369 void DIEEntry::print(raw_ostream &O) {
370 O << format("Die: 0x%lx", (long)(intptr_t)Entry);
374 //===----------------------------------------------------------------------===//
375 // DIEBlock Implementation
376 //===----------------------------------------------------------------------===//
378 /// ComputeSize - calculate the size of the block.
380 unsigned DIEBlock::ComputeSize(const TargetData *TD) {
382 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
383 for (unsigned i = 0, N = Values.size(); i < N; ++i)
384 Size += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
390 /// EmitValue - Emit block data.
392 void DIEBlock::EmitValue(DwarfPrinter *D, unsigned Form) const {
393 const AsmPrinter *Asm = D->getAsm();
395 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
396 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
397 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
398 case dwarf::DW_FORM_block: Asm->EmitULEB128Bytes(Size); break;
399 default: llvm_unreachable("Improper form for block"); break;
402 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
403 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
405 Values[i]->EmitValue(D, AbbrevData[i].getForm());
409 /// SizeOf - Determine size of block data in bytes.
411 unsigned DIEBlock::SizeOf(const TargetData *TD, unsigned Form) const {
413 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
414 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
415 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
416 case dwarf::DW_FORM_block: return Size + MCAsmInfo::getULEB128Size(Size);
417 default: llvm_unreachable("Improper form for block"); break;
423 void DIEBlock::print(raw_ostream &O) {