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 "DwarfUnit.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/CodeGen/AsmPrinter.h"
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/Support/Allocator.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/Format.h"
27 #include "llvm/Support/FormattedStream.h"
28 #include "llvm/Support/MD5.h"
31 //===----------------------------------------------------------------------===//
32 // DIEAbbrevData Implementation
33 //===----------------------------------------------------------------------===//
35 /// Profile - Used to gather unique data for the abbreviation folding set.
37 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
38 // Explicitly cast to an integer type for which FoldingSetNodeID has
39 // overloads. Otherwise MSVC 2010 thinks this call is ambiguous.
40 ID.AddInteger(unsigned(Attribute));
41 ID.AddInteger(unsigned(Form));
44 //===----------------------------------------------------------------------===//
45 // DIEAbbrev Implementation
46 //===----------------------------------------------------------------------===//
48 /// Profile - Used to gather unique data for the abbreviation folding set.
50 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
51 ID.AddInteger(unsigned(Tag));
52 ID.AddInteger(ChildrenFlag);
54 // For each attribute description.
55 for (unsigned i = 0, N = Data.size(); i < N; ++i)
59 /// Emit - Print the abbreviation using the specified asm printer.
61 void DIEAbbrev::Emit(AsmPrinter *AP) const {
62 // Emit its Dwarf tag type.
63 AP->EmitULEB128(Tag, dwarf::TagString(Tag));
65 // Emit whether it has children DIEs.
66 AP->EmitULEB128(ChildrenFlag, dwarf::ChildrenString(ChildrenFlag));
68 // For each attribute description.
69 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
70 const DIEAbbrevData &AttrData = Data[i];
72 // Emit attribute type.
73 AP->EmitULEB128(AttrData.getAttribute(),
74 dwarf::AttributeString(AttrData.getAttribute()));
77 AP->EmitULEB128(AttrData.getForm(),
78 dwarf::FormEncodingString(AttrData.getForm()));
81 // Mark end of abbreviation.
82 AP->EmitULEB128(0, "EOM(1)");
83 AP->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 /// Climb up the parent chain to get the unit DIE to which this DIE
118 const DIE *DIE::getUnit() const {
119 const DIE *Cu = getUnitOrNull();
120 assert(Cu && "We should not have orphaned DIEs.");
124 /// Climb up the parent chain to get the unit DIE this DIE belongs
125 /// to. Return NULL if DIE is not added to an owner yet.
126 const DIE *DIE::getUnitOrNull() const {
129 if (p->getTag() == dwarf::DW_TAG_compile_unit ||
130 p->getTag() == dwarf::DW_TAG_type_unit)
137 DIEValue *DIE::findAttribute(uint16_t Attribute) const {
138 const SmallVectorImpl<DIEValue *> &Values = getValues();
139 const DIEAbbrev &Abbrevs = getAbbrev();
141 // Iterate through all the attributes until we find the one we're
142 // looking for, if we can't find it return NULL.
143 for (size_t i = 0; i < Values.size(); ++i)
144 if (Abbrevs.getData()[i].getAttribute() == Attribute)
150 void DIE::print(raw_ostream &O, unsigned IndentCount) const {
151 const std::string Indent(IndentCount, ' ');
152 bool isBlock = Abbrev.getTag() == 0;
157 << format("0x%lx", (long)(intptr_t)this)
158 << ", Offset: " << Offset
159 << ", Size: " << Size << "\n";
162 << dwarf::TagString(Abbrev.getTag())
164 << dwarf::ChildrenString(Abbrev.getChildrenFlag()) << "\n";
166 O << "Size: " << Size << "\n";
169 const SmallVectorImpl<DIEAbbrevData> &Data = Abbrev.getData();
172 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
176 O << dwarf::AttributeString(Data[i].getAttribute());
178 O << "Blk[" << i << "]";
181 << dwarf::FormEncodingString(Data[i].getForm())
188 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
189 Children[j]->print(O, IndentCount+4);
192 if (!isBlock) O << "\n";
200 void DIEValue::anchor() { }
203 void DIEValue::dump() const {
208 //===----------------------------------------------------------------------===//
209 // DIEInteger Implementation
210 //===----------------------------------------------------------------------===//
212 /// EmitValue - Emit integer of appropriate size.
214 void DIEInteger::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
217 case dwarf::DW_FORM_flag_present:
218 // Emit something to keep the lines and comments in sync.
219 // FIXME: Is there a better way to do this?
220 if (Asm->OutStreamer.hasRawTextSupport())
221 Asm->OutStreamer.EmitRawText("");
223 case dwarf::DW_FORM_flag: // Fall thru
224 case dwarf::DW_FORM_ref1: // Fall thru
225 case dwarf::DW_FORM_data1: Size = 1; break;
226 case dwarf::DW_FORM_ref2: // Fall thru
227 case dwarf::DW_FORM_data2: Size = 2; break;
228 case dwarf::DW_FORM_sec_offset: // Fall thru
229 case dwarf::DW_FORM_ref4: // Fall thru
230 case dwarf::DW_FORM_data4: Size = 4; break;
231 case dwarf::DW_FORM_ref8: // Fall thru
232 case dwarf::DW_FORM_ref_sig8: // Fall thru
233 case dwarf::DW_FORM_data8: Size = 8; break;
234 case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
235 case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
236 case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
237 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
238 case dwarf::DW_FORM_addr:
239 Size = Asm->getDataLayout().getPointerSize(); break;
240 default: llvm_unreachable("DIE Value form not supported yet");
242 Asm->OutStreamer.EmitIntValue(Integer, Size);
245 /// SizeOf - Determine size of integer value in bytes.
247 unsigned DIEInteger::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
249 case dwarf::DW_FORM_flag_present: return 0;
250 case dwarf::DW_FORM_flag: // Fall thru
251 case dwarf::DW_FORM_ref1: // Fall thru
252 case dwarf::DW_FORM_data1: return sizeof(int8_t);
253 case dwarf::DW_FORM_ref2: // Fall thru
254 case dwarf::DW_FORM_data2: return sizeof(int16_t);
255 case dwarf::DW_FORM_sec_offset: // Fall thru
256 case dwarf::DW_FORM_ref4: // Fall thru
257 case dwarf::DW_FORM_data4: return sizeof(int32_t);
258 case dwarf::DW_FORM_ref8: // Fall thru
259 case dwarf::DW_FORM_ref_sig8: // Fall thru
260 case dwarf::DW_FORM_data8: return sizeof(int64_t);
261 case dwarf::DW_FORM_GNU_str_index: return MCAsmInfo::getULEB128Size(Integer);
262 case dwarf::DW_FORM_GNU_addr_index: return MCAsmInfo::getULEB128Size(Integer);
263 case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
264 case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
265 case dwarf::DW_FORM_addr: return AP->getDataLayout().getPointerSize();
266 default: llvm_unreachable("DIE Value form not supported yet");
271 void DIEInteger::print(raw_ostream &O) const {
272 O << "Int: " << (int64_t)Integer << " 0x";
273 O.write_hex(Integer);
277 //===----------------------------------------------------------------------===//
278 // DIEExpr Implementation
279 //===----------------------------------------------------------------------===//
281 /// EmitValue - Emit expression value.
283 void DIEExpr::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
284 AP->OutStreamer.EmitValue(Expr, SizeOf(AP, Form));
287 /// SizeOf - Determine size of expression value in bytes.
289 unsigned DIEExpr::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
290 if (Form == dwarf::DW_FORM_data4) return 4;
291 if (Form == dwarf::DW_FORM_sec_offset) return 4;
292 if (Form == dwarf::DW_FORM_strp) return 4;
293 return AP->getDataLayout().getPointerSize();
297 void DIEExpr::print(raw_ostream &O) const {
303 //===----------------------------------------------------------------------===//
304 // DIELabel Implementation
305 //===----------------------------------------------------------------------===//
307 /// EmitValue - Emit label value.
309 void DIELabel::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
310 AP->EmitLabelReference(Label, SizeOf(AP, Form),
311 Form == dwarf::DW_FORM_strp ||
312 Form == dwarf::DW_FORM_sec_offset ||
313 Form == dwarf::DW_FORM_ref_addr);
316 /// SizeOf - Determine size of label value in bytes.
318 unsigned DIELabel::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
319 if (Form == dwarf::DW_FORM_data4) return 4;
320 if (Form == dwarf::DW_FORM_sec_offset) return 4;
321 if (Form == dwarf::DW_FORM_strp) return 4;
322 return AP->getDataLayout().getPointerSize();
326 void DIELabel::print(raw_ostream &O) const {
327 O << "Lbl: " << Label->getName();
331 //===----------------------------------------------------------------------===//
332 // DIEDelta Implementation
333 //===----------------------------------------------------------------------===//
335 /// EmitValue - Emit delta value.
337 void DIEDelta::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
338 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
341 /// SizeOf - Determine size of delta value in bytes.
343 unsigned DIEDelta::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
344 if (Form == dwarf::DW_FORM_data4) return 4;
345 if (Form == dwarf::DW_FORM_sec_offset) return 4;
346 if (Form == dwarf::DW_FORM_strp) return 4;
347 return AP->getDataLayout().getPointerSize();
351 void DIEDelta::print(raw_ostream &O) const {
352 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
356 //===----------------------------------------------------------------------===//
357 // DIEString Implementation
358 //===----------------------------------------------------------------------===//
360 /// EmitValue - Emit string value.
362 void DIEString::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
363 Access->EmitValue(AP, Form);
366 /// SizeOf - Determine size of delta value in bytes.
368 unsigned DIEString::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
369 return Access->SizeOf(AP, Form);
373 void DIEString::print(raw_ostream &O) const {
374 O << "String: " << Str << "\tSymbol: ";
379 //===----------------------------------------------------------------------===//
380 // DIEEntry Implementation
381 //===----------------------------------------------------------------------===//
383 /// EmitValue - Emit debug information entry offset.
385 void DIEEntry::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
386 AP->EmitInt32(Entry->getOffset());
389 unsigned DIEEntry::getRefAddrSize(AsmPrinter *AP) {
390 // DWARF4: References that use the attribute form DW_FORM_ref_addr are
391 // specified to be four bytes in the DWARF 32-bit format and eight bytes
392 // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
393 // references have the same size as an address on the target system.
394 const DwarfDebug *DD = AP->getDwarfDebug();
395 assert(DD && "Expected Dwarf Debug info to be available");
396 if (DD->getDwarfVersion() == 2)
397 return AP->getDataLayout().getPointerSize();
398 return sizeof(int32_t);
402 void DIEEntry::print(raw_ostream &O) const {
403 O << format("Die: 0x%lx", (long)(intptr_t)Entry);
407 //===----------------------------------------------------------------------===//
408 // DIETypeSignature Implementation
409 //===----------------------------------------------------------------------===//
410 void DIETypeSignature::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
411 assert(Form == dwarf::DW_FORM_ref_sig8);
412 Asm->OutStreamer.EmitIntValue(Unit.getTypeSignature(), 8);
416 void DIETypeSignature::print(raw_ostream &O) const {
417 O << format("Type Unit: 0x%lx", Unit.getTypeSignature());
420 void DIETypeSignature::dump() const { print(dbgs()); }
423 //===----------------------------------------------------------------------===//
424 // DIEBlock Implementation
425 //===----------------------------------------------------------------------===//
427 /// ComputeSize - calculate the size of the block.
429 unsigned DIEBlock::ComputeSize(AsmPrinter *AP) {
431 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
432 for (unsigned i = 0, N = Values.size(); i < N; ++i)
433 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
439 /// EmitValue - Emit block data.
441 void DIEBlock::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
443 default: llvm_unreachable("Improper form for block");
444 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
445 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
446 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
447 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
450 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
451 for (unsigned i = 0, N = Values.size(); i < N; ++i)
452 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
455 /// SizeOf - Determine size of block data in bytes.
457 unsigned DIEBlock::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
459 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
460 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
461 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
462 case dwarf::DW_FORM_block: return Size + MCAsmInfo::getULEB128Size(Size);
463 default: llvm_unreachable("Improper form for block");
468 void DIEBlock::print(raw_ostream &O) const {