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/Target/TargetData.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/Format.h"
24 //===----------------------------------------------------------------------===//
25 // DIEAbbrevData Implementation
26 //===----------------------------------------------------------------------===//
28 /// Profile - Used to gather unique data for the abbreviation folding set.
30 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
31 ID.AddInteger(Attribute);
35 //===----------------------------------------------------------------------===//
36 // DIEAbbrev Implementation
37 //===----------------------------------------------------------------------===//
39 /// Profile - Used to gather unique data for the abbreviation folding set.
41 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
43 ID.AddInteger(ChildrenFlag);
45 // For each attribute description.
46 for (unsigned i = 0, N = Data.size(); i < N; ++i)
50 /// Emit - Print the abbreviation using the specified asm printer.
52 void DIEAbbrev::Emit(const AsmPrinter *Asm) const {
53 // Emit its Dwarf tag type.
54 Asm->EmitULEB128Bytes(Tag);
55 Asm->EOL(dwarf::TagString(Tag));
57 // Emit whether it has children DIEs.
58 Asm->EmitULEB128Bytes(ChildrenFlag);
59 Asm->EOL(dwarf::ChildrenString(ChildrenFlag));
61 // For each attribute description.
62 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
63 const DIEAbbrevData &AttrData = Data[i];
65 // Emit attribute type.
66 Asm->EmitULEB128Bytes(AttrData.getAttribute());
67 Asm->EOL(dwarf::AttributeString(AttrData.getAttribute()));
70 Asm->EmitULEB128Bytes(AttrData.getForm());
71 Asm->EOL(dwarf::FormEncodingString(AttrData.getForm()));
74 // Mark end of abbreviation.
75 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(1)");
76 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(2)");
80 void DIEAbbrev::print(raw_ostream &O) {
82 << format("0x%lx", (long)(intptr_t)this)
84 << dwarf::TagString(Tag)
86 << dwarf::ChildrenString(ChildrenFlag)
89 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
91 << dwarf::AttributeString(Data[i].getAttribute())
93 << dwarf::FormEncodingString(Data[i].getForm())
97 void DIEAbbrev::dump() { print(dbgs()); }
100 //===----------------------------------------------------------------------===//
101 // DIE Implementation
102 //===----------------------------------------------------------------------===//
105 for (unsigned i = 0, N = Children.size(); i < N; ++i)
109 /// addSiblingOffset - Add a sibling offset field to the front of the DIE.
111 void DIE::addSiblingOffset() {
112 DIEInteger *DI = new DIEInteger(0);
113 Values.insert(Values.begin(), DI);
114 Abbrev.AddFirstAttribute(dwarf::DW_AT_sibling, dwarf::DW_FORM_ref4);
118 void DIE::print(raw_ostream &O, unsigned IncIndent) {
119 IndentCount += IncIndent;
120 const std::string Indent(IndentCount, ' ');
121 bool isBlock = Abbrev.getTag() == 0;
126 << format("0x%lx", (long)(intptr_t)this)
127 << ", Offset: " << Offset
128 << ", Size: " << Size
132 << dwarf::TagString(Abbrev.getTag())
134 << dwarf::ChildrenString(Abbrev.getChildrenFlag());
136 O << "Size: " << Size;
140 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
143 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
147 O << dwarf::AttributeString(Data[i].getAttribute());
149 O << "Blk[" << i << "]";
152 << dwarf::FormEncodingString(Data[i].getForm())
159 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
160 Children[j]->print(O, 4);
163 if (!isBlock) O << "\n";
164 IndentCount -= IncIndent;
174 void DIEValue::dump() {
179 //===----------------------------------------------------------------------===//
180 // DIEInteger Implementation
181 //===----------------------------------------------------------------------===//
183 /// EmitValue - Emit integer of appropriate size.
185 void DIEInteger::EmitValue(Dwarf *D, unsigned Form) const {
186 const AsmPrinter *Asm = D->getAsm();
188 case dwarf::DW_FORM_flag: // Fall thru
189 case dwarf::DW_FORM_ref1: // Fall thru
190 case dwarf::DW_FORM_data1: Asm->EmitInt8(Integer); break;
191 case dwarf::DW_FORM_ref2: // Fall thru
192 case dwarf::DW_FORM_data2: Asm->EmitInt16(Integer); break;
193 case dwarf::DW_FORM_ref4: // Fall thru
194 case dwarf::DW_FORM_data4: Asm->EmitInt32(Integer); break;
195 case dwarf::DW_FORM_ref8: // Fall thru
196 case dwarf::DW_FORM_data8: Asm->EmitInt64(Integer); break;
197 case dwarf::DW_FORM_udata: Asm->EmitULEB128Bytes(Integer); break;
198 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128Bytes(Integer); break;
199 default: llvm_unreachable("DIE Value form not supported yet");
203 /// SizeOf - Determine size of integer value in bytes.
205 unsigned DIEInteger::SizeOf(const TargetData *TD, unsigned Form) const {
207 case dwarf::DW_FORM_flag: // Fall thru
208 case dwarf::DW_FORM_ref1: // Fall thru
209 case dwarf::DW_FORM_data1: return sizeof(int8_t);
210 case dwarf::DW_FORM_ref2: // Fall thru
211 case dwarf::DW_FORM_data2: return sizeof(int16_t);
212 case dwarf::DW_FORM_ref4: // Fall thru
213 case dwarf::DW_FORM_data4: return sizeof(int32_t);
214 case dwarf::DW_FORM_ref8: // Fall thru
215 case dwarf::DW_FORM_data8: return sizeof(int64_t);
216 case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
217 case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
218 default: llvm_unreachable("DIE Value form not supported yet"); break;
224 void DIEInteger::print(raw_ostream &O) {
225 O << "Int: " << (int64_t)Integer
226 << format(" 0x%llx", (unsigned long long)Integer);
230 //===----------------------------------------------------------------------===//
231 // DIEString Implementation
232 //===----------------------------------------------------------------------===//
234 /// EmitValue - Emit string value.
236 void DIEString::EmitValue(Dwarf *D, unsigned Form) const {
237 D->getAsm()->EmitString(Str);
241 void DIEString::print(raw_ostream &O) {
242 O << "Str: \"" << Str << "\"";
246 //===----------------------------------------------------------------------===//
247 // DIEDwarfLabel Implementation
248 //===----------------------------------------------------------------------===//
250 /// EmitValue - Emit label value.
252 void DIEDwarfLabel::EmitValue(Dwarf *D, unsigned Form) const {
253 bool IsSmall = Form == dwarf::DW_FORM_data4;
254 D->EmitReference(Label, false, IsSmall);
257 /// SizeOf - Determine size of label value in bytes.
259 unsigned DIEDwarfLabel::SizeOf(const TargetData *TD, unsigned Form) const {
260 if (Form == dwarf::DW_FORM_data4) return 4;
261 return TD->getPointerSize();
265 void DIEDwarfLabel::print(raw_ostream &O) {
271 //===----------------------------------------------------------------------===//
272 // DIEObjectLabel Implementation
273 //===----------------------------------------------------------------------===//
275 /// EmitValue - Emit label value.
277 void DIEObjectLabel::EmitValue(Dwarf *D, unsigned Form) const {
278 bool IsSmall = Form == dwarf::DW_FORM_data4;
279 D->EmitReference(Label, false, IsSmall);
282 /// SizeOf - Determine size of label value in bytes.
284 unsigned DIEObjectLabel::SizeOf(const TargetData *TD, unsigned Form) const {
285 if (Form == dwarf::DW_FORM_data4) return 4;
286 return TD->getPointerSize();
290 void DIEObjectLabel::print(raw_ostream &O) {
291 O << "Obj: " << Label;
295 //===----------------------------------------------------------------------===//
296 // DIESectionOffset Implementation
297 //===----------------------------------------------------------------------===//
299 /// EmitValue - Emit delta value.
301 void DIESectionOffset::EmitValue(Dwarf *D, unsigned Form) const {
302 bool IsSmall = Form == dwarf::DW_FORM_data4;
303 D->EmitSectionOffset(Label.getTag(), Section.getTag(),
304 Label.getNumber(), Section.getNumber(),
305 IsSmall, IsEH, UseSet);
308 /// SizeOf - Determine size of delta value in bytes.
310 unsigned DIESectionOffset::SizeOf(const TargetData *TD, unsigned Form) const {
311 if (Form == dwarf::DW_FORM_data4) return 4;
312 return TD->getPointerSize();
316 void DIESectionOffset::print(raw_ostream &O) {
321 O << "-" << IsEH << "-" << UseSet;
325 //===----------------------------------------------------------------------===//
326 // DIEDelta Implementation
327 //===----------------------------------------------------------------------===//
329 /// EmitValue - Emit delta value.
331 void DIEDelta::EmitValue(Dwarf *D, unsigned Form) const {
332 bool IsSmall = Form == dwarf::DW_FORM_data4;
333 D->EmitDifference(LabelHi, LabelLo, IsSmall);
336 /// SizeOf - Determine size of delta value in bytes.
338 unsigned DIEDelta::SizeOf(const TargetData *TD, unsigned Form) const {
339 if (Form == dwarf::DW_FORM_data4) return 4;
340 return TD->getPointerSize();
344 void DIEDelta::print(raw_ostream &O) {
352 //===----------------------------------------------------------------------===//
353 // DIEEntry Implementation
354 //===----------------------------------------------------------------------===//
356 /// EmitValue - Emit debug information entry offset.
358 void DIEEntry::EmitValue(Dwarf *D, unsigned Form) const {
359 D->getAsm()->EmitInt32(Entry->getOffset());
363 void DIEEntry::print(raw_ostream &O) {
364 O << format("Die: 0x%lx", (long)(intptr_t)Entry);
368 //===----------------------------------------------------------------------===//
369 // DIEBlock Implementation
370 //===----------------------------------------------------------------------===//
372 /// ComputeSize - calculate the size of the block.
374 unsigned DIEBlock::ComputeSize(const TargetData *TD) {
376 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
377 for (unsigned i = 0, N = Values.size(); i < N; ++i)
378 Size += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
384 /// EmitValue - Emit block data.
386 void DIEBlock::EmitValue(Dwarf *D, unsigned Form) const {
387 const AsmPrinter *Asm = D->getAsm();
389 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
390 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
391 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
392 case dwarf::DW_FORM_block: Asm->EmitULEB128Bytes(Size); break;
393 default: llvm_unreachable("Improper form for block"); break;
396 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
397 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
399 Values[i]->EmitValue(D, AbbrevData[i].getForm());
403 /// SizeOf - Determine size of block data in bytes.
405 unsigned DIEBlock::SizeOf(const TargetData *TD, unsigned Form) const {
407 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
408 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
409 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
410 case dwarf::DW_FORM_block: return Size + MCAsmInfo::getULEB128Size(Size);
411 default: llvm_unreachable("Improper form for block"); break;
417 void DIEBlock::print(raw_ostream &O) {