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 ID.AddInteger(Attribute);
41 //===----------------------------------------------------------------------===//
42 // DIEAbbrev Implementation
43 //===----------------------------------------------------------------------===//
45 /// Profile - Used to gather unique data for the abbreviation folding set.
47 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
49 ID.AddInteger(ChildrenFlag);
51 // For each attribute description.
52 for (unsigned i = 0, N = Data.size(); i < N; ++i)
56 /// Emit - Print the abbreviation using the specified asm printer.
58 void DIEAbbrev::Emit(AsmPrinter *AP) const {
59 // Emit its Dwarf tag type.
60 AP->EmitULEB128(Tag, dwarf::TagString(Tag));
62 // Emit whether it has children DIEs.
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 AP->EmitULEB128(AttrData.getAttribute(),
71 dwarf::AttributeString(AttrData.getAttribute()));
74 AP->EmitULEB128(AttrData.getForm(),
75 dwarf::FormEncodingString(AttrData.getForm()));
78 // Mark end of abbreviation.
79 AP->EmitULEB128(0, "EOM(1)");
80 AP->EmitULEB128(0, "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 /// Climb up the parent chain to get the compile unit DIE to which this DIE
115 DIE *DIE::getCompileUnit() {
118 if (p->getTag() == dwarf::DW_TAG_compile_unit)
122 llvm_unreachable("We should not have orphaned DIEs.");
126 void DIE::print(raw_ostream &O, unsigned IndentCount) const {
127 const std::string Indent(IndentCount, ' ');
128 bool isBlock = Abbrev.getTag() == 0;
133 << format("0x%lx", (long)(intptr_t)this)
134 << ", Offset: " << Offset
135 << ", Size: " << Size << "\n";
138 << dwarf::TagString(Abbrev.getTag())
140 << dwarf::ChildrenString(Abbrev.getChildrenFlag()) << "\n";
142 O << "Size: " << Size << "\n";
145 const SmallVectorImpl<DIEAbbrevData> &Data = Abbrev.getData();
148 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
152 O << dwarf::AttributeString(Data[i].getAttribute());
154 O << "Blk[" << i << "]";
157 << dwarf::FormEncodingString(Data[i].getForm())
164 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
165 Children[j]->print(O, IndentCount+4);
168 if (!isBlock) O << "\n";
176 void DIEValue::anchor() { }
179 void DIEValue::dump() const {
184 //===----------------------------------------------------------------------===//
185 // DIEInteger Implementation
186 //===----------------------------------------------------------------------===//
188 /// EmitValue - Emit integer of appropriate size.
190 void DIEInteger::EmitValue(AsmPrinter *Asm, unsigned Form) const {
193 case dwarf::DW_FORM_flag_present:
194 // Emit something to keep the lines and comments in sync.
195 // FIXME: Is there a better way to do this?
196 if (Asm->OutStreamer.hasRawTextSupport())
197 Asm->OutStreamer.EmitRawText(StringRef(""));
199 case dwarf::DW_FORM_flag: // Fall thru
200 case dwarf::DW_FORM_ref1: // Fall thru
201 case dwarf::DW_FORM_data1: Size = 1; break;
202 case dwarf::DW_FORM_ref2: // Fall thru
203 case dwarf::DW_FORM_data2: Size = 2; break;
204 case dwarf::DW_FORM_sec_offset: // Fall thru
205 case dwarf::DW_FORM_ref4: // Fall thru
206 case dwarf::DW_FORM_data4: Size = 4; break;
207 case dwarf::DW_FORM_ref8: // Fall thru
208 case dwarf::DW_FORM_data8: Size = 8; break;
209 case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
210 case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
211 case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
212 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
213 case dwarf::DW_FORM_addr:
214 Size = Asm->getDataLayout().getPointerSize(); break;
215 default: llvm_unreachable("DIE Value form not supported yet");
217 Asm->OutStreamer.EmitIntValue(Integer, Size);
220 /// SizeOf - Determine size of integer value in bytes.
222 unsigned DIEInteger::SizeOf(AsmPrinter *AP, unsigned Form) const {
224 case dwarf::DW_FORM_flag_present: return 0;
225 case dwarf::DW_FORM_flag: // Fall thru
226 case dwarf::DW_FORM_ref1: // Fall thru
227 case dwarf::DW_FORM_data1: return sizeof(int8_t);
228 case dwarf::DW_FORM_ref2: // Fall thru
229 case dwarf::DW_FORM_data2: return sizeof(int16_t);
230 case dwarf::DW_FORM_sec_offset: // Fall thru
231 case dwarf::DW_FORM_ref4: // Fall thru
232 case dwarf::DW_FORM_data4: return sizeof(int32_t);
233 case dwarf::DW_FORM_ref8: // Fall thru
234 case dwarf::DW_FORM_data8: return sizeof(int64_t);
235 case dwarf::DW_FORM_GNU_str_index: return MCAsmInfo::getULEB128Size(Integer);
236 case dwarf::DW_FORM_GNU_addr_index: return MCAsmInfo::getULEB128Size(Integer);
237 case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
238 case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
239 case dwarf::DW_FORM_addr: return AP->getDataLayout().getPointerSize();
240 default: llvm_unreachable("DIE Value form not supported yet");
245 void DIEInteger::print(raw_ostream &O) const {
246 O << "Int: " << (int64_t)Integer << " 0x";
247 O.write_hex(Integer);
251 //===----------------------------------------------------------------------===//
252 // DIEExpr Implementation
253 //===----------------------------------------------------------------------===//
255 /// EmitValue - Emit expression value.
257 void DIEExpr::EmitValue(AsmPrinter *AP, unsigned Form) const {
258 AP->OutStreamer.EmitValue(Expr, SizeOf(AP, Form));
261 /// SizeOf - Determine size of expression value in bytes.
263 unsigned DIEExpr::SizeOf(AsmPrinter *AP, unsigned Form) const {
264 if (Form == dwarf::DW_FORM_data4) return 4;
265 if (Form == dwarf::DW_FORM_sec_offset) return 4;
266 if (Form == dwarf::DW_FORM_strp) return 4;
267 return AP->getDataLayout().getPointerSize();
271 void DIEExpr::print(raw_ostream &O) const {
277 //===----------------------------------------------------------------------===//
278 // DIELabel Implementation
279 //===----------------------------------------------------------------------===//
281 /// EmitValue - Emit label value.
283 void DIELabel::EmitValue(AsmPrinter *AP, unsigned Form) const {
284 AP->EmitLabelReference(Label, SizeOf(AP, Form));
287 /// SizeOf - Determine size of label value in bytes.
289 unsigned DIELabel::SizeOf(AsmPrinter *AP, unsigned 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 DIELabel::print(raw_ostream &O) const {
298 O << "Lbl: " << Label->getName();
302 //===----------------------------------------------------------------------===//
303 // DIEDelta Implementation
304 //===----------------------------------------------------------------------===//
306 /// EmitValue - Emit delta value.
308 void DIEDelta::EmitValue(AsmPrinter *AP, unsigned Form) const {
309 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
312 /// SizeOf - Determine size of delta value in bytes.
314 unsigned DIEDelta::SizeOf(AsmPrinter *AP, unsigned Form) const {
315 if (Form == dwarf::DW_FORM_data4) return 4;
316 if (Form == dwarf::DW_FORM_strp) return 4;
317 return AP->getDataLayout().getPointerSize();
321 void DIEDelta::print(raw_ostream &O) const {
322 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
326 //===----------------------------------------------------------------------===//
327 // DIEString Implementation
328 //===----------------------------------------------------------------------===//
330 /// EmitValue - Emit string value.
332 void DIEString::EmitValue(AsmPrinter *AP, unsigned Form) const {
333 Access->EmitValue(AP, Form);
336 /// SizeOf - Determine size of delta value in bytes.
338 unsigned DIEString::SizeOf(AsmPrinter *AP, unsigned Form) const {
339 return Access->SizeOf(AP, Form);
343 void DIEString::print(raw_ostream &O) const {
344 O << "String: " << Str << "\tSymbol: ";
349 //===----------------------------------------------------------------------===//
350 // DIEEntry Implementation
351 //===----------------------------------------------------------------------===//
353 /// EmitValue - Emit debug information entry offset.
355 void DIEEntry::EmitValue(AsmPrinter *AP, unsigned Form) const {
356 AP->EmitInt32(Entry->getOffset());
359 unsigned DIEEntry::getRefAddrSize(AsmPrinter *AP) {
360 // DWARF4: References that use the attribute form DW_FORM_ref_addr are
361 // specified to be four bytes in the DWARF 32-bit format and eight bytes
362 // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
363 // references have the same size as an address on the target system.
364 if (AP->getDwarfDebug()->getDwarfVersion() == 2)
365 return AP->getDataLayout().getPointerSize();
366 return sizeof(int32_t);
370 void DIEEntry::print(raw_ostream &O) const {
371 O << format("Die: 0x%lx", (long)(intptr_t)Entry);
375 //===----------------------------------------------------------------------===//
376 // DIEBlock Implementation
377 //===----------------------------------------------------------------------===//
379 /// ComputeSize - calculate the size of the block.
381 unsigned DIEBlock::ComputeSize(AsmPrinter *AP) {
383 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
384 for (unsigned i = 0, N = Values.size(); i < N; ++i)
385 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
391 /// EmitValue - Emit block data.
393 void DIEBlock::EmitValue(AsmPrinter *Asm, unsigned Form) const {
395 default: llvm_unreachable("Improper form for block");
396 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
397 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
398 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
399 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
402 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
403 for (unsigned i = 0, N = Values.size(); i < N; ++i)
404 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
407 /// SizeOf - Determine size of block data in bytes.
409 unsigned DIEBlock::SizeOf(AsmPrinter *AP, unsigned Form) const {
411 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
412 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
413 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
414 case dwarf::DW_FORM_block: return Size + MCAsmInfo::getULEB128Size(Size);
415 default: llvm_unreachable("Improper form for block");
420 void DIEBlock::print(raw_ostream &O) const {