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 "DwarfDebug.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCStreamer.h"
21 #include "llvm/MC/MCSymbol.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"
27 #include "llvm/Support/MD5.h"
30 //===----------------------------------------------------------------------===//
31 // DIEAbbrevData Implementation
32 //===----------------------------------------------------------------------===//
34 /// Profile - Used to gather unique data for the abbreviation folding set.
36 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
37 // Explicitly cast to an integer type for which FoldingSetNodeID has
38 // overloads. Otherwise MSVC 2010 thinks this call is ambiguous.
39 ID.AddInteger(unsigned(Attribute));
40 ID.AddInteger(unsigned(Form));
43 //===----------------------------------------------------------------------===//
44 // DIEAbbrev Implementation
45 //===----------------------------------------------------------------------===//
47 /// Profile - Used to gather unique data for the abbreviation folding set.
49 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
50 ID.AddInteger(unsigned(Tag));
51 ID.AddInteger(ChildrenFlag);
53 // For each attribute description.
54 for (unsigned i = 0, N = Data.size(); i < N; ++i)
58 /// Emit - Print the abbreviation using the specified asm printer.
60 void DIEAbbrev::Emit(AsmPrinter *AP) const {
61 // Emit its Dwarf tag type.
62 AP->EmitULEB128(Tag, dwarf::TagString(Tag));
64 // Emit whether it has children DIEs.
65 AP->EmitULEB128(ChildrenFlag, dwarf::ChildrenString(ChildrenFlag));
67 // For each attribute description.
68 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
69 const DIEAbbrevData &AttrData = Data[i];
71 // Emit attribute type.
72 AP->EmitULEB128(AttrData.getAttribute(),
73 dwarf::AttributeString(AttrData.getAttribute()));
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)
115 /// Climb up the parent chain to get the compile unit DIE to which this DIE
117 const DIE *DIE::getCompileUnit() const {
118 const DIE *Cu = getCompileUnitOrNull();
119 assert(Cu && "We should not have orphaned DIEs.");
123 /// Climb up the parent chain to get the compile unit DIE this DIE belongs
124 /// to. Return NULL if DIE is not added to an owner yet.
125 const DIE *DIE::getCompileUnitOrNull() const {
128 if (p->getTag() == dwarf::DW_TAG_compile_unit)
135 DIEValue *DIE::findAttribute(uint16_t Attribute) {
136 const SmallVectorImpl<DIEValue *> &Values = getValues();
137 const DIEAbbrev &Abbrevs = getAbbrev();
139 // Iterate through all the attributes until we find the one we're
140 // looking for, if we can't find it return NULL.
141 for (size_t i = 0; i < Values.size(); ++i)
142 if (Abbrevs.getData()[i].getAttribute() == Attribute)
148 void DIE::print(raw_ostream &O, unsigned IndentCount) const {
149 const std::string Indent(IndentCount, ' ');
150 bool isBlock = Abbrev.getTag() == 0;
155 << format("0x%lx", (long)(intptr_t)this)
156 << ", Offset: " << Offset
157 << ", Size: " << Size << "\n";
160 << dwarf::TagString(Abbrev.getTag())
162 << dwarf::ChildrenString(Abbrev.getChildrenFlag()) << "\n";
164 O << "Size: " << Size << "\n";
167 const SmallVectorImpl<DIEAbbrevData> &Data = Abbrev.getData();
170 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
174 O << dwarf::AttributeString(Data[i].getAttribute());
176 O << "Blk[" << i << "]";
179 << dwarf::FormEncodingString(Data[i].getForm())
186 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
187 Children[j]->print(O, IndentCount+4);
190 if (!isBlock) O << "\n";
198 void DIEValue::anchor() { }
201 void DIEValue::dump() const {
206 //===----------------------------------------------------------------------===//
207 // DIEInteger Implementation
208 //===----------------------------------------------------------------------===//
210 /// EmitValue - Emit integer of appropriate size.
212 void DIEInteger::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
215 case dwarf::DW_FORM_flag_present:
216 // Emit something to keep the lines and comments in sync.
217 // FIXME: Is there a better way to do this?
218 if (Asm->OutStreamer.hasRawTextSupport())
219 Asm->OutStreamer.EmitRawText("");
221 case dwarf::DW_FORM_flag: // Fall thru
222 case dwarf::DW_FORM_ref1: // Fall thru
223 case dwarf::DW_FORM_data1: Size = 1; break;
224 case dwarf::DW_FORM_ref2: // Fall thru
225 case dwarf::DW_FORM_data2: Size = 2; break;
226 case dwarf::DW_FORM_sec_offset: // Fall thru
227 case dwarf::DW_FORM_ref4: // Fall thru
228 case dwarf::DW_FORM_data4: Size = 4; break;
229 case dwarf::DW_FORM_ref8: // Fall thru
230 case dwarf::DW_FORM_data8: Size = 8; break;
231 case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
232 case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
233 case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
234 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
235 case dwarf::DW_FORM_addr:
236 Size = Asm->getDataLayout().getPointerSize(); break;
237 default: llvm_unreachable("DIE Value form not supported yet");
239 Asm->OutStreamer.EmitIntValue(Integer, Size);
242 /// SizeOf - Determine size of integer value in bytes.
244 unsigned DIEInteger::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
246 case dwarf::DW_FORM_flag_present: return 0;
247 case dwarf::DW_FORM_flag: // Fall thru
248 case dwarf::DW_FORM_ref1: // Fall thru
249 case dwarf::DW_FORM_data1: return sizeof(int8_t);
250 case dwarf::DW_FORM_ref2: // Fall thru
251 case dwarf::DW_FORM_data2: return sizeof(int16_t);
252 case dwarf::DW_FORM_sec_offset: // Fall thru
253 case dwarf::DW_FORM_ref4: // Fall thru
254 case dwarf::DW_FORM_data4: return sizeof(int32_t);
255 case dwarf::DW_FORM_ref8: // Fall thru
256 case dwarf::DW_FORM_data8: return sizeof(int64_t);
257 case dwarf::DW_FORM_GNU_str_index: return MCAsmInfo::getULEB128Size(Integer);
258 case dwarf::DW_FORM_GNU_addr_index: return MCAsmInfo::getULEB128Size(Integer);
259 case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
260 case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
261 case dwarf::DW_FORM_addr: return AP->getDataLayout().getPointerSize();
262 default: llvm_unreachable("DIE Value form not supported yet");
267 void DIEInteger::print(raw_ostream &O) const {
268 O << "Int: " << (int64_t)Integer << " 0x";
269 O.write_hex(Integer);
273 //===----------------------------------------------------------------------===//
274 // DIEExpr Implementation
275 //===----------------------------------------------------------------------===//
277 /// EmitValue - Emit expression value.
279 void DIEExpr::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
280 AP->OutStreamer.EmitValue(Expr, SizeOf(AP, Form));
283 /// SizeOf - Determine size of expression value in bytes.
285 unsigned DIEExpr::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
286 if (Form == dwarf::DW_FORM_data4) return 4;
287 if (Form == dwarf::DW_FORM_sec_offset) return 4;
288 if (Form == dwarf::DW_FORM_strp) return 4;
289 return AP->getDataLayout().getPointerSize();
293 void DIEExpr::print(raw_ostream &O) const {
299 //===----------------------------------------------------------------------===//
300 // DIELabel Implementation
301 //===----------------------------------------------------------------------===//
303 /// EmitValue - Emit label value.
305 void DIELabel::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
306 AP->EmitLabelReference(Label, SizeOf(AP, Form),
307 Form == dwarf::DW_FORM_strp ||
308 Form == dwarf::DW_FORM_sec_offset ||
309 Form == dwarf::DW_FORM_ref_addr);
312 /// SizeOf - Determine size of label value in bytes.
314 unsigned DIELabel::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
315 if (Form == dwarf::DW_FORM_data4) return 4;
316 if (Form == dwarf::DW_FORM_sec_offset) return 4;
317 if (Form == dwarf::DW_FORM_strp) return 4;
318 return AP->getDataLayout().getPointerSize();
322 void DIELabel::print(raw_ostream &O) const {
323 O << "Lbl: " << Label->getName();
327 //===----------------------------------------------------------------------===//
328 // DIEDelta Implementation
329 //===----------------------------------------------------------------------===//
331 /// EmitValue - Emit delta value.
333 void DIEDelta::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
334 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
337 /// SizeOf - Determine size of delta value in bytes.
339 unsigned DIEDelta::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
340 if (Form == dwarf::DW_FORM_data4) return 4;
341 if (Form == dwarf::DW_FORM_sec_offset) return 4;
342 if (Form == dwarf::DW_FORM_strp) return 4;
343 return AP->getDataLayout().getPointerSize();
347 void DIEDelta::print(raw_ostream &O) const {
348 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
352 //===----------------------------------------------------------------------===//
353 // DIEString Implementation
354 //===----------------------------------------------------------------------===//
356 /// EmitValue - Emit string value.
358 void DIEString::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
359 Access->EmitValue(AP, Form);
362 /// SizeOf - Determine size of delta value in bytes.
364 unsigned DIEString::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
365 return Access->SizeOf(AP, Form);
369 void DIEString::print(raw_ostream &O) const {
370 O << "String: " << Str << "\tSymbol: ";
375 //===----------------------------------------------------------------------===//
376 // DIEEntry Implementation
377 //===----------------------------------------------------------------------===//
379 /// EmitValue - Emit debug information entry offset.
381 void DIEEntry::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
382 AP->EmitInt32(Entry->getOffset());
385 unsigned DIEEntry::getRefAddrSize(AsmPrinter *AP) {
386 // DWARF4: References that use the attribute form DW_FORM_ref_addr are
387 // specified to be four bytes in the DWARF 32-bit format and eight bytes
388 // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
389 // references have the same size as an address on the target system.
390 if (AP->getDwarfDebug()->getDwarfVersion() == 2)
391 return AP->getDataLayout().getPointerSize();
392 return sizeof(int32_t);
396 void DIEEntry::print(raw_ostream &O) const {
397 O << format("Die: 0x%lx", (long)(intptr_t)Entry);
401 //===----------------------------------------------------------------------===//
402 // DIEBlock Implementation
403 //===----------------------------------------------------------------------===//
405 /// ComputeSize - calculate the size of the block.
407 unsigned DIEBlock::ComputeSize(AsmPrinter *AP) {
409 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
410 for (unsigned i = 0, N = Values.size(); i < N; ++i)
411 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
417 /// EmitValue - Emit block data.
419 void DIEBlock::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
421 default: llvm_unreachable("Improper form for block");
422 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
423 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
424 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
425 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
428 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
429 for (unsigned i = 0, N = Values.size(); i < N; ++i)
430 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
433 /// SizeOf - Determine size of block data in bytes.
435 unsigned DIEBlock::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
437 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
438 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
439 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
440 case dwarf::DW_FORM_block: return Size + MCAsmInfo::getULEB128Size(Size);
441 default: llvm_unreachable("Improper form for block");
446 void DIEBlock::print(raw_ostream &O) const {