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 "llvm/ADT/Twine.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/Support/Allocator.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(AsmPrinter *AP) const {
57 // Emit its Dwarf tag type.
58 // FIXME: Doing work even in non-asm-verbose runs.
59 AP->EmitULEB128(Tag, dwarf::TagString(Tag));
61 // Emit whether it has children DIEs.
62 // FIXME: Doing work even in non-asm-verbose runs.
63 AP->EmitULEB128(ChildrenFlag, 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 // FIXME: Doing work even in non-asm-verbose runs.
71 AP->EmitULEB128(AttrData.getAttribute(),
72 dwarf::AttributeString(AttrData.getAttribute()));
75 // FIXME: Doing work even in non-asm-verbose runs.
76 AP->EmitULEB128(AttrData.getForm(),
77 dwarf::FormEncodingString(AttrData.getForm()));
80 // Mark end of abbreviation.
81 AP->EmitULEB128(0, "EOM(1)");
82 AP->EmitULEB128(0, "EOM(2)");
86 void DIEAbbrev::print(raw_ostream &O) {
88 << format("0x%lx", (long)(intptr_t)this)
90 << dwarf::TagString(Tag)
92 << dwarf::ChildrenString(ChildrenFlag)
95 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
97 << dwarf::AttributeString(Data[i].getAttribute())
99 << dwarf::FormEncodingString(Data[i].getForm())
103 void DIEAbbrev::dump() { print(dbgs()); }
106 //===----------------------------------------------------------------------===//
107 // DIE Implementation
108 //===----------------------------------------------------------------------===//
111 for (unsigned i = 0, N = Children.size(); i < N; ++i)
116 void DIE::print(raw_ostream &O, unsigned IncIndent) {
117 IndentCount += IncIndent;
118 const std::string Indent(IndentCount, ' ');
119 bool isBlock = Abbrev.getTag() == 0;
124 << format("0x%lx", (long)(intptr_t)this)
125 << ", Offset: " << Offset
126 << ", Size: " << Size << "\n";
129 << dwarf::TagString(Abbrev.getTag())
131 << dwarf::ChildrenString(Abbrev.getChildrenFlag()) << "\n";
133 O << "Size: " << Size << "\n";
136 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
139 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
143 O << dwarf::AttributeString(Data[i].getAttribute());
145 O << "Blk[" << i << "]";
148 << dwarf::FormEncodingString(Data[i].getForm())
155 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
156 Children[j]->print(O, 4);
159 if (!isBlock) O << "\n";
160 IndentCount -= IncIndent;
168 void DIEValue::anchor() { }
171 void DIEValue::dump() {
176 //===----------------------------------------------------------------------===//
177 // DIEInteger Implementation
178 //===----------------------------------------------------------------------===//
180 /// EmitValue - Emit integer of appropriate size.
182 void DIEInteger::EmitValue(AsmPrinter *Asm, unsigned Form) const {
185 case dwarf::DW_FORM_flag_present:
186 // Emit something to keep the lines and comments in sync.
187 // FIXME: Is there a better way to do this?
188 if (Asm->OutStreamer.hasRawTextSupport())
189 Asm->OutStreamer.EmitRawText(StringRef(""));
191 case dwarf::DW_FORM_flag: // Fall thru
192 case dwarf::DW_FORM_ref1: // Fall thru
193 case dwarf::DW_FORM_data1: Size = 1; break;
194 case dwarf::DW_FORM_ref2: // Fall thru
195 case dwarf::DW_FORM_data2: Size = 2; break;
196 case dwarf::DW_FORM_sec_offset: // Fall thru
197 case dwarf::DW_FORM_ref4: // Fall thru
198 case dwarf::DW_FORM_data4: Size = 4; break;
199 case dwarf::DW_FORM_ref8: // Fall thru
200 case dwarf::DW_FORM_data8: Size = 8; break;
201 case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
202 case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
203 case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
204 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
205 case dwarf::DW_FORM_addr:
206 Size = Asm->getDataLayout().getPointerSize(); break;
207 default: llvm_unreachable("DIE Value form not supported yet");
209 Asm->OutStreamer.EmitIntValue(Integer, Size);
212 /// SizeOf - Determine size of integer value in bytes.
214 unsigned DIEInteger::SizeOf(AsmPrinter *AP, unsigned Form) const {
216 case dwarf::DW_FORM_flag_present: return 0;
217 case dwarf::DW_FORM_flag: // Fall thru
218 case dwarf::DW_FORM_ref1: // Fall thru
219 case dwarf::DW_FORM_data1: return sizeof(int8_t);
220 case dwarf::DW_FORM_ref2: // Fall thru
221 case dwarf::DW_FORM_data2: return sizeof(int16_t);
222 case dwarf::DW_FORM_sec_offset: // Fall thru
223 case dwarf::DW_FORM_ref4: // Fall thru
224 case dwarf::DW_FORM_data4: return sizeof(int32_t);
225 case dwarf::DW_FORM_ref8: // Fall thru
226 case dwarf::DW_FORM_data8: return sizeof(int64_t);
227 case dwarf::DW_FORM_GNU_str_index: return MCAsmInfo::getULEB128Size(Integer);
228 case dwarf::DW_FORM_GNU_addr_index: return MCAsmInfo::getULEB128Size(Integer);
229 case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
230 case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
231 case dwarf::DW_FORM_addr: return AP->getDataLayout().getPointerSize();
232 default: llvm_unreachable("DIE Value form not supported yet");
237 void DIEInteger::print(raw_ostream &O) {
238 O << "Int: " << (int64_t)Integer << " 0x";
239 O.write_hex(Integer);
243 //===----------------------------------------------------------------------===//
244 // DIELabel Implementation
245 //===----------------------------------------------------------------------===//
247 /// EmitValue - Emit label value.
249 void DIELabel::EmitValue(AsmPrinter *AP, unsigned Form) const {
250 AP->OutStreamer.EmitSymbolValue(Label, SizeOf(AP, Form));
253 /// SizeOf - Determine size of label value in bytes.
255 unsigned DIELabel::SizeOf(AsmPrinter *AP, unsigned Form) const {
256 if (Form == dwarf::DW_FORM_data4) return 4;
257 if (Form == dwarf::DW_FORM_strp) return 4;
258 return AP->getDataLayout().getPointerSize();
262 void DIELabel::print(raw_ostream &O) {
263 O << "Lbl: " << Label->getName();
267 //===----------------------------------------------------------------------===//
268 // DIEDelta Implementation
269 //===----------------------------------------------------------------------===//
271 /// EmitValue - Emit delta value.
273 void DIEDelta::EmitValue(AsmPrinter *AP, unsigned Form) const {
274 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
277 /// SizeOf - Determine size of delta value in bytes.
279 unsigned DIEDelta::SizeOf(AsmPrinter *AP, unsigned Form) const {
280 if (Form == dwarf::DW_FORM_data4) return 4;
281 if (Form == dwarf::DW_FORM_strp) return 4;
282 return AP->getDataLayout().getPointerSize();
286 void DIEDelta::print(raw_ostream &O) {
287 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
291 //===----------------------------------------------------------------------===//
292 // DIEEntry Implementation
293 //===----------------------------------------------------------------------===//
295 /// EmitValue - Emit debug information entry offset.
297 void DIEEntry::EmitValue(AsmPrinter *AP, unsigned Form) const {
298 AP->EmitInt32(Entry->getOffset());
302 void DIEEntry::print(raw_ostream &O) {
303 O << format("Die: 0x%lx", (long)(intptr_t)Entry);
307 //===----------------------------------------------------------------------===//
308 // DIEBlock Implementation
309 //===----------------------------------------------------------------------===//
311 /// ComputeSize - calculate the size of the block.
313 unsigned DIEBlock::ComputeSize(AsmPrinter *AP) {
315 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
316 for (unsigned i = 0, N = Values.size(); i < N; ++i)
317 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
323 /// EmitValue - Emit block data.
325 void DIEBlock::EmitValue(AsmPrinter *Asm, unsigned Form) const {
327 default: llvm_unreachable("Improper form for block");
328 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
329 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
330 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
331 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
334 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
335 for (unsigned i = 0, N = Values.size(); i < N; ++i)
336 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
339 /// SizeOf - Determine size of block data in bytes.
341 unsigned DIEBlock::SizeOf(AsmPrinter *AP, unsigned Form) const {
343 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
344 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
345 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
346 case dwarf::DW_FORM_block: return Size + MCAsmInfo::getULEB128Size(Size);
347 default: llvm_unreachable("Improper form for block");
352 void DIEBlock::print(raw_ostream &O) {