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/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 unit DIE to which this DIE
117 const DIE *DIE::getUnit() const {
118 const DIE *Cu = getUnitOrNull();
119 assert(Cu && "We should not have orphaned DIEs.");
123 /// Climb up the parent chain to get the unit DIE this DIE belongs
124 /// to. Return NULL if DIE is not added to an owner yet.
125 const DIE *DIE::getUnitOrNull() const {
128 if (p->getTag() == dwarf::DW_TAG_compile_unit ||
129 p->getTag() == dwarf::DW_TAG_type_unit)
136 DIEValue *DIE::findAttribute(uint16_t Attribute) const {
137 const SmallVectorImpl<DIEValue *> &Values = getValues();
138 const DIEAbbrev &Abbrevs = getAbbrev();
140 // Iterate through all the attributes until we find the one we're
141 // looking for, if we can't find it return NULL.
142 for (size_t i = 0; i < Values.size(); ++i)
143 if (Abbrevs.getData()[i].getAttribute() == Attribute)
149 void DIE::print(raw_ostream &O, unsigned IndentCount) const {
150 const std::string Indent(IndentCount, ' ');
151 bool isBlock = Abbrev.getTag() == 0;
156 << format("0x%lx", (long)(intptr_t)this)
157 << ", Offset: " << Offset
158 << ", Size: " << Size << "\n";
161 << dwarf::TagString(Abbrev.getTag())
163 << dwarf::ChildrenString(Abbrev.getChildrenFlag()) << "\n";
165 O << "Size: " << Size << "\n";
168 const SmallVectorImpl<DIEAbbrevData> &Data = Abbrev.getData();
171 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
175 O << dwarf::AttributeString(Data[i].getAttribute());
177 O << "Blk[" << i << "]";
180 << dwarf::FormEncodingString(Data[i].getForm())
187 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
188 Children[j]->print(O, IndentCount+4);
191 if (!isBlock) O << "\n";
199 void DIEValue::anchor() { }
202 void DIEValue::dump() const {
207 //===----------------------------------------------------------------------===//
208 // DIEInteger Implementation
209 //===----------------------------------------------------------------------===//
211 /// EmitValue - Emit integer of appropriate size.
213 void DIEInteger::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
216 case dwarf::DW_FORM_flag_present:
217 // Emit something to keep the lines and comments in sync.
218 // FIXME: Is there a better way to do this?
219 Asm->OutStreamer.AddBlankLine();
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_ref_sig8: // Fall thru
231 case dwarf::DW_FORM_data8: Size = 8; break;
232 case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
233 case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
234 case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
235 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
236 case dwarf::DW_FORM_addr:
237 Size = Asm->getDataLayout().getPointerSize(); break;
238 default: llvm_unreachable("DIE Value form not supported yet");
240 Asm->OutStreamer.EmitIntValue(Integer, Size);
243 /// SizeOf - Determine size of integer value in bytes.
245 unsigned DIEInteger::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
247 case dwarf::DW_FORM_flag_present: return 0;
248 case dwarf::DW_FORM_flag: // Fall thru
249 case dwarf::DW_FORM_ref1: // Fall thru
250 case dwarf::DW_FORM_data1: return sizeof(int8_t);
251 case dwarf::DW_FORM_ref2: // Fall thru
252 case dwarf::DW_FORM_data2: return sizeof(int16_t);
253 case dwarf::DW_FORM_sec_offset: // Fall thru
254 case dwarf::DW_FORM_ref4: // Fall thru
255 case dwarf::DW_FORM_data4: return sizeof(int32_t);
256 case dwarf::DW_FORM_ref8: // Fall thru
257 case dwarf::DW_FORM_ref_sig8: // Fall thru
258 case dwarf::DW_FORM_data8: return sizeof(int64_t);
259 case dwarf::DW_FORM_GNU_str_index: return MCAsmInfo::getULEB128Size(Integer);
260 case dwarf::DW_FORM_GNU_addr_index: return MCAsmInfo::getULEB128Size(Integer);
261 case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
262 case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
263 case dwarf::DW_FORM_addr: return AP->getDataLayout().getPointerSize();
264 default: llvm_unreachable("DIE Value form not supported yet");
269 void DIEInteger::print(raw_ostream &O) const {
270 O << "Int: " << (int64_t)Integer << " 0x";
271 O.write_hex(Integer);
275 //===----------------------------------------------------------------------===//
276 // DIEExpr Implementation
277 //===----------------------------------------------------------------------===//
279 /// EmitValue - Emit expression value.
281 void DIEExpr::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
282 AP->OutStreamer.EmitValue(Expr, SizeOf(AP, Form));
285 /// SizeOf - Determine size of expression value in bytes.
287 unsigned DIEExpr::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
288 if (Form == dwarf::DW_FORM_data4) return 4;
289 if (Form == dwarf::DW_FORM_sec_offset) return 4;
290 if (Form == dwarf::DW_FORM_strp) return 4;
291 return AP->getDataLayout().getPointerSize();
295 void DIEExpr::print(raw_ostream &O) const {
301 //===----------------------------------------------------------------------===//
302 // DIELabel Implementation
303 //===----------------------------------------------------------------------===//
305 /// EmitValue - Emit label value.
307 void DIELabel::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
308 AP->EmitLabelReference(Label, SizeOf(AP, Form),
309 Form == dwarf::DW_FORM_strp ||
310 Form == dwarf::DW_FORM_sec_offset ||
311 Form == dwarf::DW_FORM_ref_addr);
314 /// SizeOf - Determine size of label value in bytes.
316 unsigned DIELabel::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
317 if (Form == dwarf::DW_FORM_data4) return 4;
318 if (Form == dwarf::DW_FORM_sec_offset) return 4;
319 if (Form == dwarf::DW_FORM_strp) return 4;
320 return AP->getDataLayout().getPointerSize();
324 void DIELabel::print(raw_ostream &O) const {
325 O << "Lbl: " << Label->getName();
329 //===----------------------------------------------------------------------===//
330 // DIEDelta Implementation
331 //===----------------------------------------------------------------------===//
333 /// EmitValue - Emit delta value.
335 void DIEDelta::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
336 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
339 /// SizeOf - Determine size of delta value in bytes.
341 unsigned DIEDelta::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
342 if (Form == dwarf::DW_FORM_data4) return 4;
343 if (Form == dwarf::DW_FORM_sec_offset) return 4;
344 if (Form == dwarf::DW_FORM_strp) return 4;
345 return AP->getDataLayout().getPointerSize();
349 void DIEDelta::print(raw_ostream &O) const {
350 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
354 //===----------------------------------------------------------------------===//
355 // DIEString Implementation
356 //===----------------------------------------------------------------------===//
358 /// EmitValue - Emit string value.
360 void DIEString::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
361 Access->EmitValue(AP, Form);
364 /// SizeOf - Determine size of delta value in bytes.
366 unsigned DIEString::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
367 return Access->SizeOf(AP, Form);
371 void DIEString::print(raw_ostream &O) const {
372 O << "String: " << Str << "\tSymbol: ";
377 //===----------------------------------------------------------------------===//
378 // DIEEntry Implementation
379 //===----------------------------------------------------------------------===//
381 /// EmitValue - Emit debug information entry offset.
383 void DIEEntry::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
384 AP->EmitInt32(Entry->getOffset());
387 unsigned DIEEntry::getRefAddrSize(AsmPrinter *AP) {
388 // DWARF4: References that use the attribute form DW_FORM_ref_addr are
389 // specified to be four bytes in the DWARF 32-bit format and eight bytes
390 // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
391 // references have the same size as an address on the target system.
392 const DwarfDebug *DD = AP->getDwarfDebug();
393 assert(DD && "Expected Dwarf Debug info to be available");
394 if (DD->getDwarfVersion() == 2)
395 return AP->getDataLayout().getPointerSize();
396 return sizeof(int32_t);
400 void DIEEntry::print(raw_ostream &O) const {
401 O << format("Die: 0x%lx", (long)(intptr_t)Entry);
405 //===----------------------------------------------------------------------===//
406 // DIETypeSignature Implementation
407 //===----------------------------------------------------------------------===//
408 void DIETypeSignature::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
409 assert(Form == dwarf::DW_FORM_ref_sig8);
410 Asm->OutStreamer.EmitIntValue(Unit.getTypeSignature(), 8);
414 void DIETypeSignature::print(raw_ostream &O) const {
415 O << format("Type Unit: 0x%lx", Unit.getTypeSignature());
418 void DIETypeSignature::dump() const { print(dbgs()); }
421 //===----------------------------------------------------------------------===//
422 // DIELoc Implementation
423 //===----------------------------------------------------------------------===//
425 /// ComputeSize - calculate the size of the location expression.
427 unsigned DIELoc::ComputeSize(AsmPrinter *AP) {
429 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
430 for (unsigned i = 0, N = Values.size(); i < N; ++i)
431 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
437 /// EmitValue - Emit location data.
439 void DIELoc::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
441 default: llvm_unreachable("Improper form for block");
442 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
443 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
444 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
445 case dwarf::DW_FORM_block:
446 case dwarf::DW_FORM_exprloc:
447 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 location data in bytes.
457 unsigned DIELoc::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:
463 case dwarf::DW_FORM_exprloc:
464 return Size + MCAsmInfo::getULEB128Size(Size);
465 default: llvm_unreachable("Improper form for block");
470 void DIELoc::print(raw_ostream &O) const {
476 //===----------------------------------------------------------------------===//
477 // DIEBlock Implementation
478 //===----------------------------------------------------------------------===//
480 /// ComputeSize - calculate the size of the block.
482 unsigned DIEBlock::ComputeSize(AsmPrinter *AP) {
484 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
485 for (unsigned i = 0, N = Values.size(); i < N; ++i)
486 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
492 /// EmitValue - Emit block data.
494 void DIEBlock::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
496 default: llvm_unreachable("Improper form for block");
497 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
498 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
499 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
500 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
503 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
504 for (unsigned i = 0, N = Values.size(); i < N; ++i)
505 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
508 /// SizeOf - Determine size of block data in bytes.
510 unsigned DIEBlock::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
512 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
513 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
514 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
515 case dwarf::DW_FORM_block: return Size + MCAsmInfo::getULEB128Size(Size);
516 default: llvm_unreachable("Improper form for block");
521 void DIEBlock::print(raw_ostream &O) const {