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/MCStreamer.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/Target/TargetData.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/Format.h"
27 //===----------------------------------------------------------------------===//
28 // DIEAbbrevData Implementation
29 //===----------------------------------------------------------------------===//
31 /// Profile - Used to gather unique data for the abbreviation folding set.
33 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
34 ID.AddInteger(Attribute);
38 //===----------------------------------------------------------------------===//
39 // DIEAbbrev Implementation
40 //===----------------------------------------------------------------------===//
42 /// Profile - Used to gather unique data for the abbreviation folding set.
44 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
46 ID.AddInteger(ChildrenFlag);
48 // For each attribute description.
49 for (unsigned i = 0, N = Data.size(); i < N; ++i)
53 /// Emit - Print the abbreviation using the specified asm printer.
55 void DIEAbbrev::Emit(const AsmPrinter *Asm) const {
56 // Emit its Dwarf tag type.
57 Asm->EmitULEB128Bytes(Tag);
58 Asm->EOL(dwarf::TagString(Tag));
60 // Emit whether it has children DIEs.
61 Asm->EmitULEB128Bytes(ChildrenFlag);
62 Asm->EOL(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 Asm->EmitULEB128Bytes(AttrData.getAttribute());
70 Asm->EOL(dwarf::AttributeString(AttrData.getAttribute()));
73 Asm->EmitULEB128Bytes(AttrData.getForm());
74 Asm->EOL(dwarf::FormEncodingString(AttrData.getForm()));
77 // Mark end of abbreviation.
78 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(1)");
79 Asm->EmitULEB128Bytes(0); Asm->EOL("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 /// addSiblingOffset - Add a sibling offset field to the front of the DIE.
114 void DIE::addSiblingOffset() {
115 DIEInteger *DI = new DIEInteger(0);
116 Values.insert(Values.begin(), DI);
117 Abbrev.AddFirstAttribute(dwarf::DW_AT_sibling, dwarf::DW_FORM_ref4);
121 void DIE::print(raw_ostream &O, unsigned IncIndent) {
122 IndentCount += IncIndent;
123 const std::string Indent(IndentCount, ' ');
124 bool isBlock = Abbrev.getTag() == 0;
129 << format("0x%lx", (long)(intptr_t)this)
130 << ", Offset: " << Offset
131 << ", Size: " << Size
135 << dwarf::TagString(Abbrev.getTag())
137 << dwarf::ChildrenString(Abbrev.getChildrenFlag());
139 O << "Size: " << Size;
143 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
146 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
150 O << dwarf::AttributeString(Data[i].getAttribute());
152 O << "Blk[" << i << "]";
155 << dwarf::FormEncodingString(Data[i].getForm())
162 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
163 Children[j]->print(O, 4);
166 if (!isBlock) O << "\n";
167 IndentCount -= IncIndent;
177 void DIEValue::dump() {
182 //===----------------------------------------------------------------------===//
183 // DIEInteger Implementation
184 //===----------------------------------------------------------------------===//
186 /// EmitValue - Emit integer of appropriate size.
188 void DIEInteger::EmitValue(Dwarf *D, unsigned Form) const {
189 const AsmPrinter *Asm = D->getAsm();
192 case dwarf::DW_FORM_flag: // Fall thru
193 case dwarf::DW_FORM_ref1: // Fall thru
194 case dwarf::DW_FORM_data1: Size = 1; break;
195 case dwarf::DW_FORM_ref2: // Fall thru
196 case dwarf::DW_FORM_data2: Size = 2; break;
197 case dwarf::DW_FORM_ref4: // Fall thru
198 case dwarf::DW_FORM_data4: Size = 4; break;
199 case dwarf::DW_FORM_ref8: // Fall thru
200 case dwarf::DW_FORM_data8: Size = 8; break;
201 case dwarf::DW_FORM_udata: Asm->EmitULEB128Bytes(Integer); return;
202 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128Bytes(Integer); return;
203 default: llvm_unreachable("DIE Value form not supported yet");
205 Asm->OutStreamer.EmitIntValue(Integer, Size, 0/*addrspace*/);
208 /// SizeOf - Determine size of integer value in bytes.
210 unsigned DIEInteger::SizeOf(const TargetData *TD, unsigned Form) const {
212 case dwarf::DW_FORM_flag: // Fall thru
213 case dwarf::DW_FORM_ref1: // Fall thru
214 case dwarf::DW_FORM_data1: return sizeof(int8_t);
215 case dwarf::DW_FORM_ref2: // Fall thru
216 case dwarf::DW_FORM_data2: return sizeof(int16_t);
217 case dwarf::DW_FORM_ref4: // Fall thru
218 case dwarf::DW_FORM_data4: return sizeof(int32_t);
219 case dwarf::DW_FORM_ref8: // Fall thru
220 case dwarf::DW_FORM_data8: return sizeof(int64_t);
221 case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
222 case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
223 default: llvm_unreachable("DIE Value form not supported yet"); break;
229 void DIEInteger::print(raw_ostream &O) {
230 O << "Int: " << (int64_t)Integer
231 << format(" 0x%llx", (unsigned long long)Integer);
235 //===----------------------------------------------------------------------===//
236 // DIEString Implementation
237 //===----------------------------------------------------------------------===//
239 /// EmitValue - Emit string value.
241 void DIEString::EmitValue(Dwarf *D, unsigned Form) const {
242 D->getAsm()->EmitString(Str);
246 void DIEString::print(raw_ostream &O) {
247 O << "Str: \"" << Str << "\"";
251 //===----------------------------------------------------------------------===//
252 // DIEDwarfLabel Implementation
253 //===----------------------------------------------------------------------===//
255 /// EmitValue - Emit label value.
257 void DIEDwarfLabel::EmitValue(Dwarf *D, unsigned Form) const {
258 bool IsSmall = Form == dwarf::DW_FORM_data4;
259 D->EmitReference(Label, false, IsSmall);
262 /// SizeOf - Determine size of label value in bytes.
264 unsigned DIEDwarfLabel::SizeOf(const TargetData *TD, unsigned Form) const {
265 if (Form == dwarf::DW_FORM_data4) return 4;
266 return TD->getPointerSize();
270 void DIEDwarfLabel::print(raw_ostream &O) {
276 //===----------------------------------------------------------------------===//
277 // DIEObjectLabel Implementation
278 //===----------------------------------------------------------------------===//
280 /// EmitValue - Emit label value.
282 void DIEObjectLabel::EmitValue(Dwarf *D, unsigned Form) const {
283 bool IsSmall = Form == dwarf::DW_FORM_data4;
284 D->EmitReference(Sym, false, IsSmall);
287 /// SizeOf - Determine size of label value in bytes.
289 unsigned DIEObjectLabel::SizeOf(const TargetData *TD, unsigned Form) const {
290 if (Form == dwarf::DW_FORM_data4) return 4;
291 return TD->getPointerSize();
295 void DIEObjectLabel::print(raw_ostream &O) {
296 O << "Obj: " << Sym->getName();
300 //===----------------------------------------------------------------------===//
301 // DIESectionOffset Implementation
302 //===----------------------------------------------------------------------===//
304 /// EmitValue - Emit delta value.
306 void DIESectionOffset::EmitValue(Dwarf *D, unsigned Form) const {
307 bool IsSmall = Form == dwarf::DW_FORM_data4;
308 D->EmitSectionOffset(Label.getTag(), Section.getTag(),
309 Label.getNumber(), Section.getNumber(),
310 IsSmall, IsEH, UseSet);
313 /// SizeOf - Determine size of delta value in bytes.
315 unsigned DIESectionOffset::SizeOf(const TargetData *TD, unsigned Form) const {
316 if (Form == dwarf::DW_FORM_data4) return 4;
317 return TD->getPointerSize();
321 void DIESectionOffset::print(raw_ostream &O) {
326 O << "-" << IsEH << "-" << UseSet;
330 //===----------------------------------------------------------------------===//
331 // DIEDelta Implementation
332 //===----------------------------------------------------------------------===//
334 /// EmitValue - Emit delta value.
336 void DIEDelta::EmitValue(Dwarf *D, unsigned Form) const {
337 bool IsSmall = Form == dwarf::DW_FORM_data4;
338 D->EmitDifference(LabelHi, LabelLo, IsSmall);
341 /// SizeOf - Determine size of delta value in bytes.
343 unsigned DIEDelta::SizeOf(const TargetData *TD, unsigned Form) const {
344 if (Form == dwarf::DW_FORM_data4) return 4;
345 return TD->getPointerSize();
349 void DIEDelta::print(raw_ostream &O) {
357 //===----------------------------------------------------------------------===//
358 // DIEEntry Implementation
359 //===----------------------------------------------------------------------===//
361 /// EmitValue - Emit debug information entry offset.
363 void DIEEntry::EmitValue(Dwarf *D, unsigned Form) const {
364 D->getAsm()->EmitInt32(Entry->getOffset());
368 void DIEEntry::print(raw_ostream &O) {
369 O << format("Die: 0x%lx", (long)(intptr_t)Entry);
373 //===----------------------------------------------------------------------===//
374 // DIEBlock Implementation
375 //===----------------------------------------------------------------------===//
377 /// ComputeSize - calculate the size of the block.
379 unsigned DIEBlock::ComputeSize(const TargetData *TD) {
381 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
382 for (unsigned i = 0, N = Values.size(); i < N; ++i)
383 Size += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
389 /// EmitValue - Emit block data.
391 void DIEBlock::EmitValue(Dwarf *D, unsigned Form) const {
392 const AsmPrinter *Asm = D->getAsm();
394 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
395 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
396 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
397 case dwarf::DW_FORM_block: Asm->EmitULEB128Bytes(Size); break;
398 default: llvm_unreachable("Improper form for block"); break;
401 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
402 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
404 Values[i]->EmitValue(D, AbbrevData[i].getForm());
408 /// SizeOf - Determine size of block data in bytes.
410 unsigned DIEBlock::SizeOf(const TargetData *TD, unsigned Form) const {
412 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
413 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
414 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
415 case dwarf::DW_FORM_block: return Size + MCAsmInfo::getULEB128Size(Size);
416 default: llvm_unreachable("Improper form for block"); break;
422 void DIEBlock::print(raw_ostream &O) {