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 "DwarfPrinter.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/Target/TargetData.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/Format.h"
26 //===----------------------------------------------------------------------===//
27 // DIEAbbrevData Implementation
28 //===----------------------------------------------------------------------===//
30 /// Profile - Used to gather unique data for the abbreviation folding set.
32 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
33 ID.AddInteger(Attribute);
37 //===----------------------------------------------------------------------===//
38 // DIEAbbrev Implementation
39 //===----------------------------------------------------------------------===//
41 /// Profile - Used to gather unique data for the abbreviation folding set.
43 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
45 ID.AddInteger(ChildrenFlag);
47 // For each attribute description.
48 for (unsigned i = 0, N = Data.size(); i < N; ++i)
52 /// Emit - Print the abbreviation using the specified asm printer.
54 void DIEAbbrev::Emit(const AsmPrinter *Asm) const {
55 // Emit its Dwarf tag type.
56 Asm->EmitULEB128Bytes(Tag);
57 Asm->EOL(dwarf::TagString(Tag));
59 // Emit whether it has children DIEs.
60 Asm->EmitULEB128Bytes(ChildrenFlag);
61 Asm->EOL(dwarf::ChildrenString(ChildrenFlag));
63 // For each attribute description.
64 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
65 const DIEAbbrevData &AttrData = Data[i];
67 // Emit attribute type.
68 Asm->EmitULEB128Bytes(AttrData.getAttribute());
69 Asm->EOL(dwarf::AttributeString(AttrData.getAttribute()));
72 Asm->EmitULEB128Bytes(AttrData.getForm());
73 Asm->EOL(dwarf::FormEncodingString(AttrData.getForm()));
76 // Mark end of abbreviation.
77 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(1)");
78 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(2)");
82 void DIEAbbrev::print(raw_ostream &O) {
84 << format("0x%lx", (long)(intptr_t)this)
86 << dwarf::TagString(Tag)
88 << dwarf::ChildrenString(ChildrenFlag)
91 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
93 << dwarf::AttributeString(Data[i].getAttribute())
95 << dwarf::FormEncodingString(Data[i].getForm())
99 void DIEAbbrev::dump() { print(dbgs()); }
102 //===----------------------------------------------------------------------===//
103 // DIE Implementation
104 //===----------------------------------------------------------------------===//
107 for (unsigned i = 0, N = Children.size(); i < N; ++i)
111 /// addSiblingOffset - Add a sibling offset field to the front of the DIE.
113 void DIE::addSiblingOffset() {
114 DIEInteger *DI = new DIEInteger(0);
115 Values.insert(Values.begin(), DI);
116 Abbrev.AddFirstAttribute(dwarf::DW_AT_sibling, dwarf::DW_FORM_ref4);
120 void DIE::print(raw_ostream &O, unsigned IncIndent) {
121 IndentCount += IncIndent;
122 const std::string Indent(IndentCount, ' ');
123 bool isBlock = Abbrev.getTag() == 0;
128 << format("0x%lx", (long)(intptr_t)this)
129 << ", Offset: " << Offset
130 << ", Size: " << Size
134 << dwarf::TagString(Abbrev.getTag())
136 << dwarf::ChildrenString(Abbrev.getChildrenFlag());
138 O << "Size: " << Size;
142 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
145 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
149 O << dwarf::AttributeString(Data[i].getAttribute());
151 O << "Blk[" << i << "]";
154 << dwarf::FormEncodingString(Data[i].getForm())
161 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
162 Children[j]->print(O, 4);
165 if (!isBlock) O << "\n";
166 IndentCount -= IncIndent;
176 void DIEValue::dump() {
181 //===----------------------------------------------------------------------===//
182 // DIEInteger Implementation
183 //===----------------------------------------------------------------------===//
185 /// EmitValue - Emit integer of appropriate size.
187 void DIEInteger::EmitValue(Dwarf *D, unsigned Form) const {
188 const AsmPrinter *Asm = D->getAsm();
190 case dwarf::DW_FORM_flag: // Fall thru
191 case dwarf::DW_FORM_ref1: // Fall thru
192 case dwarf::DW_FORM_data1: Asm->EmitInt8(Integer); break;
193 case dwarf::DW_FORM_ref2: // Fall thru
194 case dwarf::DW_FORM_data2: Asm->EmitInt16(Integer); break;
195 case dwarf::DW_FORM_ref4: // Fall thru
196 case dwarf::DW_FORM_data4: Asm->EmitInt32(Integer); break;
197 case dwarf::DW_FORM_ref8: // Fall thru
198 case dwarf::DW_FORM_data8: Asm->EmitInt64(Integer); break;
199 case dwarf::DW_FORM_udata: Asm->EmitULEB128Bytes(Integer); break;
200 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128Bytes(Integer); break;
201 default: llvm_unreachable("DIE Value form not supported yet");
205 /// SizeOf - Determine size of integer value in bytes.
207 unsigned DIEInteger::SizeOf(const TargetData *TD, unsigned Form) const {
209 case dwarf::DW_FORM_flag: // Fall thru
210 case dwarf::DW_FORM_ref1: // Fall thru
211 case dwarf::DW_FORM_data1: return sizeof(int8_t);
212 case dwarf::DW_FORM_ref2: // Fall thru
213 case dwarf::DW_FORM_data2: return sizeof(int16_t);
214 case dwarf::DW_FORM_ref4: // Fall thru
215 case dwarf::DW_FORM_data4: return sizeof(int32_t);
216 case dwarf::DW_FORM_ref8: // Fall thru
217 case dwarf::DW_FORM_data8: return sizeof(int64_t);
218 case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
219 case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
220 default: llvm_unreachable("DIE Value form not supported yet"); break;
226 void DIEInteger::print(raw_ostream &O) {
227 O << "Int: " << (int64_t)Integer
228 << format(" 0x%llx", (unsigned long long)Integer);
232 //===----------------------------------------------------------------------===//
233 // DIEString Implementation
234 //===----------------------------------------------------------------------===//
236 /// EmitValue - Emit string value.
238 void DIEString::EmitValue(Dwarf *D, unsigned Form) const {
239 D->getAsm()->EmitString(Str);
243 void DIEString::print(raw_ostream &O) {
244 O << "Str: \"" << Str << "\"";
248 //===----------------------------------------------------------------------===//
249 // DIEDwarfLabel Implementation
250 //===----------------------------------------------------------------------===//
252 /// EmitValue - Emit label value.
254 void DIEDwarfLabel::EmitValue(Dwarf *D, unsigned Form) const {
255 bool IsSmall = Form == dwarf::DW_FORM_data4;
256 D->EmitReference(Label, false, IsSmall);
259 /// SizeOf - Determine size of label value in bytes.
261 unsigned DIEDwarfLabel::SizeOf(const TargetData *TD, unsigned Form) const {
262 if (Form == dwarf::DW_FORM_data4) return 4;
263 return TD->getPointerSize();
267 void DIEDwarfLabel::print(raw_ostream &O) {
273 //===----------------------------------------------------------------------===//
274 // DIEObjectLabel Implementation
275 //===----------------------------------------------------------------------===//
277 /// EmitValue - Emit label value.
279 void DIEObjectLabel::EmitValue(Dwarf *D, unsigned Form) const {
280 bool IsSmall = Form == dwarf::DW_FORM_data4;
281 D->EmitReference(Sym, false, IsSmall);
284 /// SizeOf - Determine size of label value in bytes.
286 unsigned DIEObjectLabel::SizeOf(const TargetData *TD, unsigned Form) const {
287 if (Form == dwarf::DW_FORM_data4) return 4;
288 return TD->getPointerSize();
292 void DIEObjectLabel::print(raw_ostream &O) {
293 O << "Obj: " << Sym->getName();
297 //===----------------------------------------------------------------------===//
298 // DIESectionOffset Implementation
299 //===----------------------------------------------------------------------===//
301 /// EmitValue - Emit delta value.
303 void DIESectionOffset::EmitValue(Dwarf *D, unsigned Form) const {
304 bool IsSmall = Form == dwarf::DW_FORM_data4;
305 D->EmitSectionOffset(Label.getTag(), Section.getTag(),
306 Label.getNumber(), Section.getNumber(),
307 IsSmall, IsEH, UseSet);
310 /// SizeOf - Determine size of delta value in bytes.
312 unsigned DIESectionOffset::SizeOf(const TargetData *TD, unsigned Form) const {
313 if (Form == dwarf::DW_FORM_data4) return 4;
314 return TD->getPointerSize();
318 void DIESectionOffset::print(raw_ostream &O) {
323 O << "-" << IsEH << "-" << UseSet;
327 //===----------------------------------------------------------------------===//
328 // DIEDelta Implementation
329 //===----------------------------------------------------------------------===//
331 /// EmitValue - Emit delta value.
333 void DIEDelta::EmitValue(Dwarf *D, unsigned Form) const {
334 bool IsSmall = Form == dwarf::DW_FORM_data4;
335 D->EmitDifference(LabelHi, LabelLo, IsSmall);
338 /// SizeOf - Determine size of delta value in bytes.
340 unsigned DIEDelta::SizeOf(const TargetData *TD, unsigned Form) const {
341 if (Form == dwarf::DW_FORM_data4) return 4;
342 return TD->getPointerSize();
346 void DIEDelta::print(raw_ostream &O) {
354 //===----------------------------------------------------------------------===//
355 // DIEEntry Implementation
356 //===----------------------------------------------------------------------===//
358 /// EmitValue - Emit debug information entry offset.
360 void DIEEntry::EmitValue(Dwarf *D, unsigned Form) const {
361 D->getAsm()->EmitInt32(Entry->getOffset());
365 void DIEEntry::print(raw_ostream &O) {
366 O << format("Die: 0x%lx", (long)(intptr_t)Entry);
370 //===----------------------------------------------------------------------===//
371 // DIEBlock Implementation
372 //===----------------------------------------------------------------------===//
374 /// ComputeSize - calculate the size of the block.
376 unsigned DIEBlock::ComputeSize(const TargetData *TD) {
378 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
379 for (unsigned i = 0, N = Values.size(); i < N; ++i)
380 Size += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
386 /// EmitValue - Emit block data.
388 void DIEBlock::EmitValue(Dwarf *D, unsigned Form) const {
389 const AsmPrinter *Asm = D->getAsm();
391 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
392 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
393 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
394 case dwarf::DW_FORM_block: Asm->EmitULEB128Bytes(Size); break;
395 default: llvm_unreachable("Improper form for block"); break;
398 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
399 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
401 Values[i]->EmitValue(D, AbbrevData[i].getForm());
405 /// SizeOf - Determine size of block data in bytes.
407 unsigned DIEBlock::SizeOf(const TargetData *TD, unsigned Form) const {
409 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
410 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
411 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
412 case dwarf::DW_FORM_block: return Size + MCAsmInfo::getULEB128Size(Size);
413 default: llvm_unreachable("Improper form for block"); break;
419 void DIEBlock::print(raw_ostream &O) {