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/Target/TargetAsmInfo.h"
18 #include "llvm/Target/TargetData.h"
22 //===----------------------------------------------------------------------===//
23 // DIEAbbrevData Implementation
24 //===----------------------------------------------------------------------===//
26 /// Profile - Used to gather unique data for the abbreviation folding set.
28 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
29 ID.AddInteger(Attribute);
33 //===----------------------------------------------------------------------===//
34 // DIEAbbrev Implementation
35 //===----------------------------------------------------------------------===//
37 /// Profile - Used to gather unique data for the abbreviation folding set.
39 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
41 ID.AddInteger(ChildrenFlag);
43 // For each attribute description.
44 for (unsigned i = 0, N = Data.size(); i < N; ++i)
48 /// Emit - Print the abbreviation using the specified asm printer.
50 void DIEAbbrev::Emit(const AsmPrinter *Asm) const {
51 // Emit its Dwarf tag type.
52 Asm->EmitULEB128Bytes(Tag);
53 Asm->EOL(dwarf::TagString(Tag));
55 // Emit whether it has children DIEs.
56 Asm->EmitULEB128Bytes(ChildrenFlag);
57 Asm->EOL(dwarf::ChildrenString(ChildrenFlag));
59 // For each attribute description.
60 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
61 const DIEAbbrevData &AttrData = Data[i];
63 // Emit attribute type.
64 Asm->EmitULEB128Bytes(AttrData.getAttribute());
65 Asm->EOL(dwarf::AttributeString(AttrData.getAttribute()));
68 Asm->EmitULEB128Bytes(AttrData.getForm());
69 Asm->EOL(dwarf::FormEncodingString(AttrData.getForm()));
72 // Mark end of abbreviation.
73 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(1)");
74 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(2)");
78 void DIEAbbrev::print(std::ostream &O) {
80 << std::hex << (intptr_t)this << std::dec
82 << dwarf::TagString(Tag)
84 << dwarf::ChildrenString(ChildrenFlag)
87 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
89 << dwarf::AttributeString(Data[i].getAttribute())
91 << dwarf::FormEncodingString(Data[i].getForm())
95 void DIEAbbrev::dump() { print(cerr); }
98 //===----------------------------------------------------------------------===//
100 //===----------------------------------------------------------------------===//
103 for (unsigned i = 0, N = Children.size(); i < N; ++i)
107 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
109 void DIE::AddSiblingOffset() {
110 DIEInteger *DI = new DIEInteger(0);
111 Values.insert(Values.begin(), DI);
112 Abbrev.AddFirstAttribute(dwarf::DW_AT_sibling, dwarf::DW_FORM_ref4);
115 /// Profile - Used to gather unique data for the value folding set.
117 void DIE::Profile(FoldingSetNodeID &ID) {
120 for (unsigned i = 0, N = Children.size(); i < N; ++i)
121 ID.AddPointer(Children[i]);
123 for (unsigned j = 0, M = Values.size(); j < M; ++j)
124 ID.AddPointer(Values[j]);
128 void DIE::print(std::ostream &O, unsigned IncIndent) {
129 static unsigned IndentCount = 0;
130 IndentCount += IncIndent;
131 const std::string Indent(IndentCount, ' ');
132 bool isBlock = Abbrev.getTag() == 0;
137 << "0x" << std::hex << (intptr_t)this << std::dec
138 << ", Offset: " << Offset
139 << ", Size: " << Size
143 << dwarf::TagString(Abbrev.getTag())
145 << dwarf::ChildrenString(Abbrev.getChildrenFlag());
147 O << "Size: " << Size;
151 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
154 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
158 O << dwarf::AttributeString(Data[i].getAttribute());
160 O << "Blk[" << i << "]";
163 << dwarf::FormEncodingString(Data[i].getForm())
170 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
171 Children[j]->print(O, 4);
174 if (!isBlock) O << "\n";
175 IndentCount -= IncIndent;
185 void DIEValue::dump() {
190 //===----------------------------------------------------------------------===//
191 // DIEInteger Implementation
192 //===----------------------------------------------------------------------===//
194 /// EmitValue - Emit integer of appropriate size.
196 void DIEInteger::EmitValue(Dwarf *D, unsigned Form) const {
197 const AsmPrinter *Asm = D->getAsm();
199 case dwarf::DW_FORM_flag: // Fall thru
200 case dwarf::DW_FORM_ref1: // Fall thru
201 case dwarf::DW_FORM_data1: Asm->EmitInt8(Integer); break;
202 case dwarf::DW_FORM_ref2: // Fall thru
203 case dwarf::DW_FORM_data2: Asm->EmitInt16(Integer); break;
204 case dwarf::DW_FORM_ref4: // Fall thru
205 case dwarf::DW_FORM_data4: Asm->EmitInt32(Integer); break;
206 case dwarf::DW_FORM_ref8: // Fall thru
207 case dwarf::DW_FORM_data8: Asm->EmitInt64(Integer); break;
208 case dwarf::DW_FORM_udata: Asm->EmitULEB128Bytes(Integer); break;
209 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128Bytes(Integer); break;
210 default: assert(0 && "DIE Value form not supported yet"); break;
214 /// SizeOf - Determine size of integer value in bytes.
216 unsigned DIEInteger::SizeOf(const TargetData *TD, unsigned Form) const {
218 case dwarf::DW_FORM_flag: // Fall thru
219 case dwarf::DW_FORM_ref1: // Fall thru
220 case dwarf::DW_FORM_data1: return sizeof(int8_t);
221 case dwarf::DW_FORM_ref2: // Fall thru
222 case dwarf::DW_FORM_data2: return sizeof(int16_t);
223 case dwarf::DW_FORM_ref4: // Fall thru
224 case dwarf::DW_FORM_data4: return sizeof(int32_t);
225 case dwarf::DW_FORM_ref8: // Fall thru
226 case dwarf::DW_FORM_data8: return sizeof(int64_t);
227 case dwarf::DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
228 case dwarf::DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
229 default: assert(0 && "DIE Value form not supported yet"); break;
234 /// Profile - Used to gather unique data for the value folding set.
236 void DIEInteger::Profile(FoldingSetNodeID &ID, unsigned Int) {
237 ID.AddInteger(isInteger);
240 void DIEInteger::Profile(FoldingSetNodeID &ID) {
241 Profile(ID, Integer);
245 void DIEInteger::print(std::ostream &O) {
246 O << "Int: " << (int64_t)Integer
247 << " 0x" << std::hex << Integer << std::dec;
251 //===----------------------------------------------------------------------===//
252 // DIEString Implementation
253 //===----------------------------------------------------------------------===//
255 /// EmitValue - Emit string value.
257 void DIEString::EmitValue(Dwarf *D, unsigned Form) const {
258 D->getAsm()->EmitString(Str);
261 /// Profile - Used to gather unique data for the value folding set.
263 void DIEString::Profile(FoldingSetNodeID &ID, const std::string &Str) {
264 ID.AddInteger(isString);
267 void DIEString::Profile(FoldingSetNodeID &ID) {
272 void DIEString::print(std::ostream &O) {
273 O << "Str: \"" << Str << "\"";
277 //===----------------------------------------------------------------------===//
278 // DIEDwarfLabel Implementation
279 //===----------------------------------------------------------------------===//
281 /// EmitValue - Emit label value.
283 void DIEDwarfLabel::EmitValue(Dwarf *D, unsigned Form) const {
284 bool IsSmall = Form == dwarf::DW_FORM_data4;
285 D->EmitReference(Label, false, IsSmall);
288 /// SizeOf - Determine size of label value in bytes.
290 unsigned DIEDwarfLabel::SizeOf(const TargetData *TD, unsigned Form) const {
291 if (Form == dwarf::DW_FORM_data4) return 4;
292 return TD->getPointerSize();
295 /// Profile - Used to gather unique data for the value folding set.
297 void DIEDwarfLabel::Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
298 ID.AddInteger(isLabel);
301 void DIEDwarfLabel::Profile(FoldingSetNodeID &ID) {
306 void DIEDwarfLabel::print(std::ostream &O) {
312 //===----------------------------------------------------------------------===//
313 // DIEObjectLabel Implementation
314 //===----------------------------------------------------------------------===//
316 /// EmitValue - Emit label value.
318 void DIEObjectLabel::EmitValue(Dwarf *D, unsigned Form) const {
319 bool IsSmall = Form == dwarf::DW_FORM_data4;
320 D->EmitReference(Label, false, IsSmall);
323 /// SizeOf - Determine size of label value in bytes.
325 unsigned DIEObjectLabel::SizeOf(const TargetData *TD, unsigned Form) const {
326 if (Form == dwarf::DW_FORM_data4) return 4;
327 return TD->getPointerSize();
330 /// Profile - Used to gather unique data for the value folding set.
332 void DIEObjectLabel::Profile(FoldingSetNodeID &ID, const std::string &Label) {
333 ID.AddInteger(isAsIsLabel);
336 void DIEObjectLabel::Profile(FoldingSetNodeID &ID) {
337 Profile(ID, Label.c_str());
341 void DIEObjectLabel::print(std::ostream &O) {
342 O << "Obj: " << Label;
346 //===----------------------------------------------------------------------===//
347 // DIESectionOffset Implementation
348 //===----------------------------------------------------------------------===//
350 /// EmitValue - Emit delta value.
352 void DIESectionOffset::EmitValue(Dwarf *D, unsigned Form) const {
353 bool IsSmall = Form == dwarf::DW_FORM_data4;
354 D->EmitSectionOffset(Label.getTag(), Section.getTag(),
355 Label.getNumber(), Section.getNumber(),
356 IsSmall, IsEH, UseSet);
359 /// SizeOf - Determine size of delta value in bytes.
361 unsigned DIESectionOffset::SizeOf(const TargetData *TD, unsigned Form) const {
362 if (Form == dwarf::DW_FORM_data4) return 4;
363 return TD->getPointerSize();
366 /// Profile - Used to gather unique data for the value folding set.
368 void DIESectionOffset::Profile(FoldingSetNodeID &ID, const DWLabel &Label,
369 const DWLabel &Section) {
370 ID.AddInteger(isSectionOffset);
373 // IsEH and UseSet are specific to the Label/Section that we will emit the
374 // offset for; so Label/Section are enough for uniqueness.
376 void DIESectionOffset::Profile(FoldingSetNodeID &ID) {
377 Profile(ID, Label, Section);
381 void DIESectionOffset::print(std::ostream &O) {
386 O << "-" << IsEH << "-" << UseSet;
390 //===----------------------------------------------------------------------===//
391 // DIEDelta Implementation
392 //===----------------------------------------------------------------------===//
394 /// EmitValue - Emit delta value.
396 void DIEDelta::EmitValue(Dwarf *D, unsigned Form) const {
397 bool IsSmall = Form == dwarf::DW_FORM_data4;
398 D->EmitDifference(LabelHi, LabelLo, IsSmall);
401 /// SizeOf - Determine size of delta value in bytes.
403 unsigned DIEDelta::SizeOf(const TargetData *TD, unsigned Form) const {
404 if (Form == dwarf::DW_FORM_data4) return 4;
405 return TD->getPointerSize();
408 /// Profile - Used to gather unique data for the value folding set.
410 void DIEDelta::Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
411 const DWLabel &LabelLo) {
412 ID.AddInteger(isDelta);
416 void DIEDelta::Profile(FoldingSetNodeID &ID) {
417 Profile(ID, LabelHi, LabelLo);
421 void DIEDelta::print(std::ostream &O) {
429 //===----------------------------------------------------------------------===//
430 // DIEEntry Implementation
431 //===----------------------------------------------------------------------===//
433 /// EmitValue - Emit debug information entry offset.
435 void DIEEntry::EmitValue(Dwarf *D, unsigned Form) const {
436 D->getAsm()->EmitInt32(Entry->getOffset());
439 /// Profile - Used to gather unique data for the value folding set.
441 void DIEEntry::Profile(FoldingSetNodeID &ID, DIE *Entry) {
442 ID.AddInteger(isEntry);
443 ID.AddPointer(Entry);
445 void DIEEntry::Profile(FoldingSetNodeID &ID) {
446 ID.AddInteger(isEntry);
449 ID.AddPointer(Entry);
455 void DIEEntry::print(std::ostream &O) {
456 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
460 //===----------------------------------------------------------------------===//
461 // DIEBlock Implementation
462 //===----------------------------------------------------------------------===//
464 /// ComputeSize - calculate the size of the block.
466 unsigned DIEBlock::ComputeSize(const TargetData *TD) {
468 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
469 for (unsigned i = 0, N = Values.size(); i < N; ++i)
470 Size += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
476 /// EmitValue - Emit block data.
478 void DIEBlock::EmitValue(Dwarf *D, unsigned Form) const {
479 const AsmPrinter *Asm = D->getAsm();
481 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
482 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
483 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
484 case dwarf::DW_FORM_block: Asm->EmitULEB128Bytes(Size); break;
485 default: assert(0 && "Improper form for block"); break;
488 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
489 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
491 Values[i]->EmitValue(D, AbbrevData[i].getForm());
495 /// SizeOf - Determine size of block data in bytes.
497 unsigned DIEBlock::SizeOf(const TargetData *TD, unsigned Form) const {
499 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
500 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
501 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
502 case dwarf::DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
503 default: assert(0 && "Improper form for block"); break;
508 void DIEBlock::Profile(FoldingSetNodeID &ID) {
509 ID.AddInteger(isBlock);
514 void DIEBlock::print(std::ostream &O) {