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/CodeGen/AsmPrinter.h"
17 #include "llvm/MC/MCAsmInfo.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/Target/TargetData.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/Format.h"
25 //===----------------------------------------------------------------------===//
26 // DIEAbbrevData Implementation
27 //===----------------------------------------------------------------------===//
29 /// Profile - Used to gather unique data for the abbreviation folding set.
31 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
32 ID.AddInteger(Attribute);
36 //===----------------------------------------------------------------------===//
37 // DIEAbbrev Implementation
38 //===----------------------------------------------------------------------===//
40 /// Profile - Used to gather unique data for the abbreviation folding set.
42 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
44 ID.AddInteger(ChildrenFlag);
46 // For each attribute description.
47 for (unsigned i = 0, N = Data.size(); i < N; ++i)
51 /// Emit - Print the abbreviation using the specified asm printer.
53 void DIEAbbrev::Emit(const AsmPrinter *Asm) const {
54 // Emit its Dwarf tag type.
55 Asm->EmitULEB128Bytes(Tag);
56 Asm->EOL(dwarf::TagString(Tag));
58 // Emit whether it has children DIEs.
59 Asm->EmitULEB128Bytes(ChildrenFlag);
60 Asm->EOL(dwarf::ChildrenString(ChildrenFlag));
62 // For each attribute description.
63 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
64 const DIEAbbrevData &AttrData = Data[i];
66 // Emit attribute type.
67 Asm->EmitULEB128Bytes(AttrData.getAttribute());
68 Asm->EOL(dwarf::AttributeString(AttrData.getAttribute()));
71 Asm->EmitULEB128Bytes(AttrData.getForm());
72 Asm->EOL(dwarf::FormEncodingString(AttrData.getForm()));
75 // Mark end of abbreviation.
76 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(1)");
77 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(2)");
81 void DIEAbbrev::print(raw_ostream &O) {
83 << format("0x%lx", (long)(intptr_t)this)
85 << dwarf::TagString(Tag)
87 << dwarf::ChildrenString(ChildrenFlag)
90 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
92 << dwarf::AttributeString(Data[i].getAttribute())
94 << dwarf::FormEncodingString(Data[i].getForm())
98 void DIEAbbrev::dump() { print(dbgs()); }
101 //===----------------------------------------------------------------------===//
102 // DIE Implementation
103 //===----------------------------------------------------------------------===//
106 for (unsigned i = 0, N = Children.size(); i < N; ++i)
110 /// addSiblingOffset - Add a sibling offset field to the front of the DIE.
112 void DIE::addSiblingOffset() {
113 DIEInteger *DI = new DIEInteger(0);
114 Values.insert(Values.begin(), DI);
115 Abbrev.AddFirstAttribute(dwarf::DW_AT_sibling, dwarf::DW_FORM_ref4);
119 void DIE::print(raw_ostream &O, unsigned IncIndent) {
120 IndentCount += IncIndent;
121 const std::string Indent(IndentCount, ' ');
122 bool isBlock = Abbrev.getTag() == 0;
127 << format("0x%lx", (long)(intptr_t)this)
128 << ", Offset: " << Offset
129 << ", Size: " << Size
133 << dwarf::TagString(Abbrev.getTag())
135 << dwarf::ChildrenString(Abbrev.getChildrenFlag());
137 O << "Size: " << Size;
141 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
144 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
148 O << dwarf::AttributeString(Data[i].getAttribute());
150 O << "Blk[" << i << "]";
153 << dwarf::FormEncodingString(Data[i].getForm())
160 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
161 Children[j]->print(O, 4);
164 if (!isBlock) O << "\n";
165 IndentCount -= IncIndent;
175 void DIEValue::dump() {
180 //===----------------------------------------------------------------------===//
181 // DIEInteger Implementation
182 //===----------------------------------------------------------------------===//
184 /// EmitValue - Emit integer of appropriate size.
186 void DIEInteger::EmitValue(Dwarf *D, unsigned Form) const {
187 const AsmPrinter *Asm = D->getAsm();
189 case dwarf::DW_FORM_flag: // Fall thru
190 case dwarf::DW_FORM_ref1: // Fall thru
191 case dwarf::DW_FORM_data1: Asm->EmitInt8(Integer); break;
192 case dwarf::DW_FORM_ref2: // Fall thru
193 case dwarf::DW_FORM_data2: Asm->EmitInt16(Integer); break;
194 case dwarf::DW_FORM_ref4: // Fall thru
195 case dwarf::DW_FORM_data4: Asm->EmitInt32(Integer); break;
196 case dwarf::DW_FORM_ref8: // Fall thru
197 case dwarf::DW_FORM_data8: Asm->EmitInt64(Integer); break;
198 case dwarf::DW_FORM_udata: Asm->EmitULEB128Bytes(Integer); break;
199 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128Bytes(Integer); break;
200 default: llvm_unreachable("DIE Value form not supported yet");
204 /// SizeOf - Determine size of integer value in bytes.
206 unsigned DIEInteger::SizeOf(const TargetData *TD, unsigned Form) const {
208 case dwarf::DW_FORM_flag: // Fall thru
209 case dwarf::DW_FORM_ref1: // Fall thru
210 case dwarf::DW_FORM_data1: return sizeof(int8_t);
211 case dwarf::DW_FORM_ref2: // Fall thru
212 case dwarf::DW_FORM_data2: return sizeof(int16_t);
213 case dwarf::DW_FORM_ref4: // Fall thru
214 case dwarf::DW_FORM_data4: return sizeof(int32_t);
215 case dwarf::DW_FORM_ref8: // Fall thru
216 case dwarf::DW_FORM_data8: return sizeof(int64_t);
217 case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
218 case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
219 default: llvm_unreachable("DIE Value form not supported yet"); break;
225 void DIEInteger::print(raw_ostream &O) {
226 O << "Int: " << (int64_t)Integer
227 << format(" 0x%llx", (unsigned long long)Integer);
231 //===----------------------------------------------------------------------===//
232 // DIEString Implementation
233 //===----------------------------------------------------------------------===//
235 /// EmitValue - Emit string value.
237 void DIEString::EmitValue(Dwarf *D, unsigned Form) const {
238 D->getAsm()->EmitString(Str);
242 void DIEString::print(raw_ostream &O) {
243 O << "Str: \"" << Str << "\"";
247 //===----------------------------------------------------------------------===//
248 // DIEDwarfLabel Implementation
249 //===----------------------------------------------------------------------===//
251 /// EmitValue - Emit label value.
253 void DIEDwarfLabel::EmitValue(Dwarf *D, unsigned Form) const {
254 bool IsSmall = Form == dwarf::DW_FORM_data4;
255 D->EmitReference(Label, false, IsSmall);
258 /// SizeOf - Determine size of label value in bytes.
260 unsigned DIEDwarfLabel::SizeOf(const TargetData *TD, unsigned Form) const {
261 if (Form == dwarf::DW_FORM_data4) return 4;
262 return TD->getPointerSize();
266 void DIEDwarfLabel::print(raw_ostream &O) {
272 //===----------------------------------------------------------------------===//
273 // DIEObjectLabel Implementation
274 //===----------------------------------------------------------------------===//
276 /// EmitValue - Emit label value.
278 void DIEObjectLabel::EmitValue(Dwarf *D, unsigned Form) const {
279 bool IsSmall = Form == dwarf::DW_FORM_data4;
280 D->EmitReference(Sym, false, IsSmall);
283 /// SizeOf - Determine size of label value in bytes.
285 unsigned DIEObjectLabel::SizeOf(const TargetData *TD, unsigned Form) const {
286 if (Form == dwarf::DW_FORM_data4) return 4;
287 return TD->getPointerSize();
291 void DIEObjectLabel::print(raw_ostream &O) {
292 O << "Obj: " << Sym->getName();
296 //===----------------------------------------------------------------------===//
297 // DIESectionOffset Implementation
298 //===----------------------------------------------------------------------===//
300 /// EmitValue - Emit delta value.
302 void DIESectionOffset::EmitValue(Dwarf *D, unsigned Form) const {
303 bool IsSmall = Form == dwarf::DW_FORM_data4;
304 D->EmitSectionOffset(Label.getTag(), Section.getTag(),
305 Label.getNumber(), Section.getNumber(),
306 IsSmall, IsEH, UseSet);
309 /// SizeOf - Determine size of delta value in bytes.
311 unsigned DIESectionOffset::SizeOf(const TargetData *TD, unsigned Form) const {
312 if (Form == dwarf::DW_FORM_data4) return 4;
313 return TD->getPointerSize();
317 void DIESectionOffset::print(raw_ostream &O) {
322 O << "-" << IsEH << "-" << UseSet;
326 //===----------------------------------------------------------------------===//
327 // DIEDelta Implementation
328 //===----------------------------------------------------------------------===//
330 /// EmitValue - Emit delta value.
332 void DIEDelta::EmitValue(Dwarf *D, unsigned Form) const {
333 bool IsSmall = Form == dwarf::DW_FORM_data4;
334 D->EmitDifference(LabelHi, LabelLo, IsSmall);
337 /// SizeOf - Determine size of delta value in bytes.
339 unsigned DIEDelta::SizeOf(const TargetData *TD, unsigned Form) const {
340 if (Form == dwarf::DW_FORM_data4) return 4;
341 return TD->getPointerSize();
345 void DIEDelta::print(raw_ostream &O) {
353 //===----------------------------------------------------------------------===//
354 // DIEEntry Implementation
355 //===----------------------------------------------------------------------===//
357 /// EmitValue - Emit debug information entry offset.
359 void DIEEntry::EmitValue(Dwarf *D, unsigned Form) const {
360 D->getAsm()->EmitInt32(Entry->getOffset());
364 void DIEEntry::print(raw_ostream &O) {
365 O << format("Die: 0x%lx", (long)(intptr_t)Entry);
369 //===----------------------------------------------------------------------===//
370 // DIEBlock Implementation
371 //===----------------------------------------------------------------------===//
373 /// ComputeSize - calculate the size of the block.
375 unsigned DIEBlock::ComputeSize(const TargetData *TD) {
377 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
378 for (unsigned i = 0, N = Values.size(); i < N; ++i)
379 Size += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
385 /// EmitValue - Emit block data.
387 void DIEBlock::EmitValue(Dwarf *D, unsigned Form) const {
388 const AsmPrinter *Asm = D->getAsm();
390 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
391 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
392 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
393 case dwarf::DW_FORM_block: Asm->EmitULEB128Bytes(Size); break;
394 default: llvm_unreachable("Improper form for block"); break;
397 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
398 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
400 Values[i]->EmitValue(D, AbbrevData[i].getForm());
404 /// SizeOf - Determine size of block data in bytes.
406 unsigned DIEBlock::SizeOf(const TargetData *TD, unsigned Form) const {
408 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
409 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
410 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
411 case dwarf::DW_FORM_block: return Size + MCAsmInfo::getULEB128Size(Size);
412 default: llvm_unreachable("Improper form for block"); break;
418 void DIEBlock::print(raw_ostream &O) {