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"
29 //===----------------------------------------------------------------------===//
30 // DIEAbbrevData Implementation
31 //===----------------------------------------------------------------------===//
33 /// Profile - Used to gather unique data for the abbreviation folding set.
35 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
36 ID.AddInteger(Attribute);
40 //===----------------------------------------------------------------------===//
41 // DIEAbbrev Implementation
42 //===----------------------------------------------------------------------===//
44 /// Profile - Used to gather unique data for the abbreviation folding set.
46 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
48 ID.AddInteger(ChildrenFlag);
50 // For each attribute description.
51 for (unsigned i = 0, N = Data.size(); i < N; ++i)
55 /// Emit - Print the abbreviation using the specified asm printer.
57 void DIEAbbrev::Emit(AsmPrinter *AP) const {
58 // Emit its Dwarf tag type.
59 AP->EmitULEB128(Tag, dwarf::TagString(Tag));
61 // Emit whether it has children DIEs.
62 AP->EmitULEB128(ChildrenFlag, dwarf::ChildrenString(ChildrenFlag));
64 // For each attribute description.
65 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
66 const DIEAbbrevData &AttrData = Data[i];
68 // Emit attribute type.
69 AP->EmitULEB128(AttrData.getAttribute(),
70 dwarf::AttributeString(AttrData.getAttribute()));
73 AP->EmitULEB128(AttrData.getForm(),
74 dwarf::FormEncodingString(AttrData.getForm()));
77 // Mark end of abbreviation.
78 AP->EmitULEB128(0, "EOM(1)");
79 AP->EmitULEB128(0, "EOM(2)");
83 void DIEAbbrev::print(raw_ostream &O) {
85 << format("0x%lx", (long)(intptr_t)this)
87 << dwarf::TagString(Tag)
89 << dwarf::ChildrenString(ChildrenFlag)
92 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
94 << dwarf::AttributeString(Data[i].getAttribute())
96 << dwarf::FormEncodingString(Data[i].getForm())
100 void DIEAbbrev::dump() { print(dbgs()); }
103 //===----------------------------------------------------------------------===//
104 // DIE Implementation
105 //===----------------------------------------------------------------------===//
108 for (unsigned i = 0, N = Children.size(); i < N; ++i)
112 /// Climb up the parent chain to get the compile unit DIE to which this DIE
114 DIE *DIE::getCompileUnit() {
117 if (p->getTag() == dwarf::DW_TAG_compile_unit)
121 llvm_unreachable("We should not have orphaned DIEs.");
125 void DIE::print(raw_ostream &O, unsigned IndentCount) const {
126 const std::string Indent(IndentCount, ' ');
127 bool isBlock = Abbrev.getTag() == 0;
132 << format("0x%lx", (long)(intptr_t)this)
133 << ", Offset: " << Offset
134 << ", Size: " << Size << "\n";
137 << dwarf::TagString(Abbrev.getTag())
139 << dwarf::ChildrenString(Abbrev.getChildrenFlag()) << "\n";
141 O << "Size: " << Size << "\n";
144 const SmallVectorImpl<DIEAbbrevData> &Data = Abbrev.getData();
147 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
151 O << dwarf::AttributeString(Data[i].getAttribute());
153 O << "Blk[" << i << "]";
156 << dwarf::FormEncodingString(Data[i].getForm())
163 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
164 Children[j]->print(O, IndentCount+4);
167 if (!isBlock) O << "\n";
175 void DIEValue::anchor() { }
178 void DIEValue::dump() const {
183 //===----------------------------------------------------------------------===//
184 // DIEInteger Implementation
185 //===----------------------------------------------------------------------===//
187 /// EmitValue - Emit integer of appropriate size.
189 void DIEInteger::EmitValue(AsmPrinter *Asm, unsigned Form) const {
192 case dwarf::DW_FORM_flag_present:
193 // Emit something to keep the lines and comments in sync.
194 // FIXME: Is there a better way to do this?
195 if (Asm->OutStreamer.hasRawTextSupport())
196 Asm->OutStreamer.EmitRawText(StringRef(""));
198 case dwarf::DW_FORM_flag: // Fall thru
199 case dwarf::DW_FORM_ref1: // Fall thru
200 case dwarf::DW_FORM_data1: Size = 1; break;
201 case dwarf::DW_FORM_ref2: // Fall thru
202 case dwarf::DW_FORM_data2: Size = 2; break;
203 case dwarf::DW_FORM_sec_offset: // Fall thru
204 case dwarf::DW_FORM_ref4: // Fall thru
205 case dwarf::DW_FORM_data4: Size = 4; break;
206 case dwarf::DW_FORM_ref8: // Fall thru
207 case dwarf::DW_FORM_data8: Size = 8; break;
208 case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
209 case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
210 case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
211 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
212 case dwarf::DW_FORM_addr:
213 Size = Asm->getDataLayout().getPointerSize(); break;
214 default: llvm_unreachable("DIE Value form not supported yet");
216 Asm->OutStreamer.EmitIntValue(Integer, Size);
219 /// SizeOf - Determine size of integer value in bytes.
221 unsigned DIEInteger::SizeOf(AsmPrinter *AP, unsigned Form) const {
223 case dwarf::DW_FORM_flag_present: return 0;
224 case dwarf::DW_FORM_flag: // Fall thru
225 case dwarf::DW_FORM_ref1: // Fall thru
226 case dwarf::DW_FORM_data1: return sizeof(int8_t);
227 case dwarf::DW_FORM_ref2: // Fall thru
228 case dwarf::DW_FORM_data2: return sizeof(int16_t);
229 case dwarf::DW_FORM_sec_offset: // Fall thru
230 case dwarf::DW_FORM_ref4: // Fall thru
231 case dwarf::DW_FORM_data4: return sizeof(int32_t);
232 case dwarf::DW_FORM_ref8: // Fall thru
233 case dwarf::DW_FORM_data8: return sizeof(int64_t);
234 case dwarf::DW_FORM_GNU_str_index: return MCAsmInfo::getULEB128Size(Integer);
235 case dwarf::DW_FORM_GNU_addr_index: return MCAsmInfo::getULEB128Size(Integer);
236 case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
237 case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
238 case dwarf::DW_FORM_addr: return AP->getDataLayout().getPointerSize();
239 default: llvm_unreachable("DIE Value form not supported yet");
244 void DIEInteger::print(raw_ostream &O) const {
245 O << "Int: " << (int64_t)Integer << " 0x";
246 O.write_hex(Integer);
250 //===----------------------------------------------------------------------===//
251 // DIEExpr Implementation
252 //===----------------------------------------------------------------------===//
254 /// EmitValue - Emit expression value.
256 void DIEExpr::EmitValue(AsmPrinter *AP, unsigned Form) const {
257 AP->OutStreamer.EmitValue(Expr, SizeOf(AP, Form));
260 /// SizeOf - Determine size of expression value in bytes.
262 unsigned DIEExpr::SizeOf(AsmPrinter *AP, unsigned Form) const {
263 if (Form == dwarf::DW_FORM_data4) return 4;
264 if (Form == dwarf::DW_FORM_sec_offset) return 4;
265 if (Form == dwarf::DW_FORM_strp) return 4;
266 return AP->getDataLayout().getPointerSize();
270 void DIEExpr::print(raw_ostream &O) const {
276 //===----------------------------------------------------------------------===//
277 // DIELabel Implementation
278 //===----------------------------------------------------------------------===//
280 /// EmitValue - Emit label value.
282 void DIELabel::EmitValue(AsmPrinter *AP, unsigned Form) const {
283 AP->EmitLabelReference(Label, SizeOf(AP, Form));
286 /// SizeOf - Determine size of label value in bytes.
288 unsigned DIELabel::SizeOf(AsmPrinter *AP, unsigned Form) const {
289 if (Form == dwarf::DW_FORM_data4) return 4;
290 if (Form == dwarf::DW_FORM_sec_offset) return 4;
291 if (Form == dwarf::DW_FORM_strp) return 4;
292 return AP->getDataLayout().getPointerSize();
296 void DIELabel::print(raw_ostream &O) const {
297 O << "Lbl: " << Label->getName();
301 //===----------------------------------------------------------------------===//
302 // DIEDelta Implementation
303 //===----------------------------------------------------------------------===//
305 /// EmitValue - Emit delta value.
307 void DIEDelta::EmitValue(AsmPrinter *AP, unsigned Form) const {
308 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
311 /// SizeOf - Determine size of delta value in bytes.
313 unsigned DIEDelta::SizeOf(AsmPrinter *AP, unsigned Form) const {
314 if (Form == dwarf::DW_FORM_data4) return 4;
315 if (Form == dwarf::DW_FORM_strp) return 4;
316 return AP->getDataLayout().getPointerSize();
320 void DIEDelta::print(raw_ostream &O) const {
321 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
325 //===----------------------------------------------------------------------===//
326 // DIEEntry Implementation
327 //===----------------------------------------------------------------------===//
329 /// EmitValue - Emit debug information entry offset.
331 void DIEEntry::EmitValue(AsmPrinter *AP, unsigned Form) const {
332 AP->EmitInt32(Entry->getOffset());
335 unsigned DIEEntry::getRefAddrSize(AsmPrinter *AP) {
336 // DWARF4: References that use the attribute form DW_FORM_ref_addr are
337 // specified to be four bytes in the DWARF 32-bit format and eight bytes
338 // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
339 // references have the same size as an address on the target system.
340 if (AP->getDwarfDebug()->getDwarfVersion() == 2)
341 return AP->getDataLayout().getPointerSize();
342 return sizeof(int32_t);
346 void DIEEntry::print(raw_ostream &O) const {
347 O << format("Die: 0x%lx", (long)(intptr_t)Entry);
351 //===----------------------------------------------------------------------===//
352 // DIEBlock Implementation
353 //===----------------------------------------------------------------------===//
355 /// ComputeSize - calculate the size of the block.
357 unsigned DIEBlock::ComputeSize(AsmPrinter *AP) {
359 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
360 for (unsigned i = 0, N = Values.size(); i < N; ++i)
361 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
367 /// EmitValue - Emit block data.
369 void DIEBlock::EmitValue(AsmPrinter *Asm, unsigned Form) const {
371 default: llvm_unreachable("Improper form for block");
372 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
373 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
374 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
375 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
378 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
379 for (unsigned i = 0, N = Values.size(); i < N; ++i)
380 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
383 /// SizeOf - Determine size of block data in bytes.
385 unsigned DIEBlock::SizeOf(AsmPrinter *AP, unsigned Form) const {
387 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
388 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
389 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
390 case dwarf::DW_FORM_block: return Size + MCAsmInfo::getULEB128Size(Size);
391 default: llvm_unreachable("Improper form for block");
396 void DIEBlock::print(raw_ostream &O) const {