1 //===--- lib/CodeGen/DIE.cpp - DWARF Info Entries -------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Data structures for DWARF info entries. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "DIE.h" 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" 25 #include "llvm/Support/FormattedStream.h" 26 using namespace llvm; 27 28 //===----------------------------------------------------------------------===// 29 // DIEAbbrevData Implementation 30 //===----------------------------------------------------------------------===// 31 32 /// Profile - Used to gather unique data for the abbreviation folding set. 33 /// 34 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const { 35 ID.AddInteger(Attribute); 36 ID.AddInteger(Form); 37 } 38 39 //===----------------------------------------------------------------------===// 40 // DIEAbbrev Implementation 41 //===----------------------------------------------------------------------===// 42 43 /// Profile - Used to gather unique data for the abbreviation folding set. 44 /// 45 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const { 46 ID.AddInteger(Tag); 47 ID.AddInteger(ChildrenFlag); 48 49 // For each attribute description. 50 for (unsigned i = 0, N = Data.size(); i < N; ++i) 51 Data[i].Profile(ID); 52 } 53 54 /// Emit - Print the abbreviation using the specified asm printer. 55 /// 56 void DIEAbbrev::Emit(const DwarfPrinter *DP) const { 57 // Emit its Dwarf tag type. 58 // FIXME: Doing work even in non-asm-verbose runs. 59 DP->EmitULEB128(Tag, dwarf::TagString(Tag)); 60 61 // Emit whether it has children DIEs. 62 // FIXME: Doing work even in non-asm-verbose runs. 63 DP->EmitULEB128(ChildrenFlag, dwarf::ChildrenString(ChildrenFlag)); 64 65 // For each attribute description. 66 for (unsigned i = 0, N = Data.size(); i < N; ++i) { 67 const DIEAbbrevData &AttrData = Data[i]; 68 69 // Emit attribute type. 70 // FIXME: Doing work even in non-asm-verbose runs. 71 DP->EmitULEB128(AttrData.getAttribute(), 72 dwarf::AttributeString(AttrData.getAttribute())); 73 74 // Emit form type. 75 // FIXME: Doing work even in non-asm-verbose runs. 76 DP->EmitULEB128(AttrData.getForm(), 77 dwarf::FormEncodingString(AttrData.getForm())); 78 } 79 80 // Mark end of abbreviation. 81 DP->EmitULEB128(0, "EOM(1)"); 82 DP->EmitULEB128(0, "EOM(2)"); 83 } 84 85 #ifndef NDEBUG 86 void DIEAbbrev::print(raw_ostream &O) { 87 O << "Abbreviation @" 88 << format("0x%lx", (long)(intptr_t)this) 89 << " " 90 << dwarf::TagString(Tag) 91 << " " 92 << dwarf::ChildrenString(ChildrenFlag) 93 << '\n'; 94 95 for (unsigned i = 0, N = Data.size(); i < N; ++i) { 96 O << " " 97 << dwarf::AttributeString(Data[i].getAttribute()) 98 << " " 99 << dwarf::FormEncodingString(Data[i].getForm()) 100 << '\n'; 101 } 102 } 103 void DIEAbbrev::dump() { print(dbgs()); } 104 #endif 105 106 //===----------------------------------------------------------------------===// 107 // DIE Implementation 108 //===----------------------------------------------------------------------===// 109 110 DIE::~DIE() { 111 for (unsigned i = 0, N = Children.size(); i < N; ++i) 112 delete Children[i]; 113 } 114 115 /// addSiblingOffset - Add a sibling offset field to the front of the DIE. 116 /// 117 void DIE::addSiblingOffset() { 118 DIEInteger *DI = new DIEInteger(0); 119 Values.insert(Values.begin(), DI); 120 Abbrev.AddFirstAttribute(dwarf::DW_AT_sibling, dwarf::DW_FORM_ref4); 121 } 122 123 #ifndef NDEBUG 124 void DIE::print(raw_ostream &O, unsigned IncIndent) { 125 IndentCount += IncIndent; 126 const std::string Indent(IndentCount, ' '); 127 bool isBlock = Abbrev.getTag() == 0; 128 129 if (!isBlock) { 130 O << Indent 131 << "Die: " 132 << format("0x%lx", (long)(intptr_t)this) 133 << ", Offset: " << Offset 134 << ", Size: " << Size 135 << "\n"; 136 137 O << Indent 138 << dwarf::TagString(Abbrev.getTag()) 139 << " " 140 << dwarf::ChildrenString(Abbrev.getChildrenFlag()); 141 } else { 142 O << "Size: " << Size; 143 } 144 O << "\n"; 145 146 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData(); 147 148 IndentCount += 2; 149 for (unsigned i = 0, N = Data.size(); i < N; ++i) { 150 O << Indent; 151 152 if (!isBlock) 153 O << dwarf::AttributeString(Data[i].getAttribute()); 154 else 155 O << "Blk[" << i << "]"; 156 157 O << " " 158 << dwarf::FormEncodingString(Data[i].getForm()) 159 << " "; 160 Values[i]->print(O); 161 O << "\n"; 162 } 163 IndentCount -= 2; 164 165 for (unsigned j = 0, M = Children.size(); j < M; ++j) { 166 Children[j]->print(O, 4); 167 } 168 169 if (!isBlock) O << "\n"; 170 IndentCount -= IncIndent; 171 } 172 173 void DIE::dump() { 174 print(dbgs()); 175 } 176 #endif 177 178 179 #ifndef NDEBUG 180 void DIEValue::dump() { 181 print(dbgs()); 182 } 183 #endif 184 185 //===----------------------------------------------------------------------===// 186 // DIEInteger Implementation 187 //===----------------------------------------------------------------------===// 188 189 /// EmitValue - Emit integer of appropriate size. 190 /// 191 void DIEInteger::EmitValue(DwarfPrinter *D, unsigned Form) const { 192 const AsmPrinter *Asm = D->getAsm(); 193 unsigned Size = ~0U; 194 switch (Form) { 195 case dwarf::DW_FORM_flag: // Fall thru 196 case dwarf::DW_FORM_ref1: // Fall thru 197 case dwarf::DW_FORM_data1: Size = 1; break; 198 case dwarf::DW_FORM_ref2: // Fall thru 199 case dwarf::DW_FORM_data2: Size = 2; break; 200 case dwarf::DW_FORM_ref4: // Fall thru 201 case dwarf::DW_FORM_data4: Size = 4; break; 202 case dwarf::DW_FORM_ref8: // Fall thru 203 case dwarf::DW_FORM_data8: Size = 8; break; 204 case dwarf::DW_FORM_udata: D->EmitULEB128(Integer); return; 205 case dwarf::DW_FORM_sdata: D->EmitSLEB128(Integer, ""); return; 206 default: llvm_unreachable("DIE Value form not supported yet"); 207 } 208 Asm->OutStreamer.EmitIntValue(Integer, Size, 0/*addrspace*/); 209 } 210 211 /// SizeOf - Determine size of integer value in bytes. 212 /// 213 unsigned DIEInteger::SizeOf(const TargetData *TD, unsigned Form) const { 214 switch (Form) { 215 case dwarf::DW_FORM_flag: // Fall thru 216 case dwarf::DW_FORM_ref1: // Fall thru 217 case dwarf::DW_FORM_data1: return sizeof(int8_t); 218 case dwarf::DW_FORM_ref2: // Fall thru 219 case dwarf::DW_FORM_data2: return sizeof(int16_t); 220 case dwarf::DW_FORM_ref4: // Fall thru 221 case dwarf::DW_FORM_data4: return sizeof(int32_t); 222 case dwarf::DW_FORM_ref8: // Fall thru 223 case dwarf::DW_FORM_data8: return sizeof(int64_t); 224 case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer); 225 case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer); 226 default: llvm_unreachable("DIE Value form not supported yet"); break; 227 } 228 return 0; 229 } 230 231 #ifndef NDEBUG 232 void DIEInteger::print(raw_ostream &O) { 233 O << "Int: " << (int64_t)Integer 234 << format(" 0x%llx", (unsigned long long)Integer); 235 } 236 #endif 237 238 //===----------------------------------------------------------------------===// 239 // DIEString Implementation 240 //===----------------------------------------------------------------------===// 241 242 /// EmitValue - Emit string value. 243 /// 244 void DIEString::EmitValue(DwarfPrinter *D, unsigned Form) const { 245 D->getAsm()->OutStreamer.EmitBytes(Str, /*addrspace*/0); 246 // Emit nul terminator. 247 D->getAsm()->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); 248 } 249 250 #ifndef NDEBUG 251 void DIEString::print(raw_ostream &O) { 252 O << "Str: \"" << Str << "\""; 253 } 254 #endif 255 256 //===----------------------------------------------------------------------===// 257 // DIEDwarfLabel Implementation 258 //===----------------------------------------------------------------------===// 259 260 /// EmitValue - Emit label value. 261 /// 262 void DIEDwarfLabel::EmitValue(DwarfPrinter *D, unsigned Form) const { 263 bool IsSmall = Form == dwarf::DW_FORM_data4; 264 D->EmitReference(Label, false, IsSmall); 265 } 266 267 /// SizeOf - Determine size of label value in bytes. 268 /// 269 unsigned DIEDwarfLabel::SizeOf(const TargetData *TD, unsigned Form) const { 270 if (Form == dwarf::DW_FORM_data4) return 4; 271 return TD->getPointerSize(); 272 } 273 274 #ifndef NDEBUG 275 void DIEDwarfLabel::print(raw_ostream &O) { 276 O << "Lbl: "; 277 Label.print(O); 278 } 279 #endif 280 281 //===----------------------------------------------------------------------===// 282 // DIEObjectLabel Implementation 283 //===----------------------------------------------------------------------===// 284 285 /// EmitValue - Emit label value. 286 /// 287 void DIEObjectLabel::EmitValue(DwarfPrinter *D, unsigned Form) const { 288 bool IsSmall = Form == dwarf::DW_FORM_data4; 289 D->EmitReference(Sym, false, IsSmall); 290 } 291 292 /// SizeOf - Determine size of label value in bytes. 293 /// 294 unsigned DIEObjectLabel::SizeOf(const TargetData *TD, unsigned Form) const { 295 if (Form == dwarf::DW_FORM_data4) return 4; 296 return TD->getPointerSize(); 297 } 298 299 #ifndef NDEBUG 300 void DIEObjectLabel::print(raw_ostream &O) { 301 O << "Obj: " << Sym->getName(); 302 } 303 #endif 304 305 //===----------------------------------------------------------------------===// 306 // DIESectionOffset Implementation 307 //===----------------------------------------------------------------------===// 308 309 /// EmitValue - Emit delta value. 310 /// 311 void DIESectionOffset::EmitValue(DwarfPrinter *D, unsigned Form) const { 312 bool IsSmall = Form == dwarf::DW_FORM_data4; 313 D->EmitSectionOffset(Label.getTag(), Section.getTag(), 314 Label.getNumber(), Section.getNumber(), 315 IsSmall, IsEH, UseSet); 316 D->getAsm()->O << '\n'; // FIXME: Necesssary? 317 } 318 319 /// SizeOf - Determine size of delta value in bytes. 320 /// 321 unsigned DIESectionOffset::SizeOf(const TargetData *TD, unsigned Form) const { 322 if (Form == dwarf::DW_FORM_data4) return 4; 323 return TD->getPointerSize(); 324 } 325 326 #ifndef NDEBUG 327 void DIESectionOffset::print(raw_ostream &O) { 328 O << "Off: "; 329 Label.print(O); 330 O << "-"; 331 Section.print(O); 332 O << "-" << IsEH << "-" << UseSet; 333 } 334 #endif 335 336 //===----------------------------------------------------------------------===// 337 // DIEDelta Implementation 338 //===----------------------------------------------------------------------===// 339 340 /// EmitValue - Emit delta value. 341 /// 342 void DIEDelta::EmitValue(DwarfPrinter *D, unsigned Form) const { 343 bool IsSmall = Form == dwarf::DW_FORM_data4; 344 D->EmitDifference(LabelHi, LabelLo, IsSmall); 345 } 346 347 /// SizeOf - Determine size of delta value in bytes. 348 /// 349 unsigned DIEDelta::SizeOf(const TargetData *TD, unsigned Form) const { 350 if (Form == dwarf::DW_FORM_data4) return 4; 351 return TD->getPointerSize(); 352 } 353 354 #ifndef NDEBUG 355 void DIEDelta::print(raw_ostream &O) { 356 O << "Del: "; 357 LabelHi.print(O); 358 O << "-"; 359 LabelLo.print(O); 360 } 361 #endif 362 363 //===----------------------------------------------------------------------===// 364 // DIEEntry Implementation 365 //===----------------------------------------------------------------------===// 366 367 /// EmitValue - Emit debug information entry offset. 368 /// 369 void DIEEntry::EmitValue(DwarfPrinter *D, unsigned Form) const { 370 D->getAsm()->EmitInt32(Entry->getOffset()); 371 } 372 373 #ifndef NDEBUG 374 void DIEEntry::print(raw_ostream &O) { 375 O << format("Die: 0x%lx", (long)(intptr_t)Entry); 376 } 377 #endif 378 379 //===----------------------------------------------------------------------===// 380 // DIEBlock Implementation 381 //===----------------------------------------------------------------------===// 382 383 /// ComputeSize - calculate the size of the block. 384 /// 385 unsigned DIEBlock::ComputeSize(const TargetData *TD) { 386 if (!Size) { 387 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData(); 388 for (unsigned i = 0, N = Values.size(); i < N; ++i) 389 Size += Values[i]->SizeOf(TD, AbbrevData[i].getForm()); 390 } 391 392 return Size; 393 } 394 395 /// EmitValue - Emit block data. 396 /// 397 void DIEBlock::EmitValue(DwarfPrinter *D, unsigned Form) const { 398 const AsmPrinter *Asm = D->getAsm(); 399 switch (Form) { 400 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break; 401 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break; 402 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break; 403 case dwarf::DW_FORM_block: D->EmitULEB128(Size); break; 404 default: llvm_unreachable("Improper form for block"); break; 405 } 406 407 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData(); 408 for (unsigned i = 0, N = Values.size(); i < N; ++i) { 409 Asm->O << '\n'; 410 Values[i]->EmitValue(D, AbbrevData[i].getForm()); 411 } 412 } 413 414 /// SizeOf - Determine size of block data in bytes. 415 /// 416 unsigned DIEBlock::SizeOf(const TargetData *TD, unsigned Form) const { 417 switch (Form) { 418 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t); 419 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t); 420 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t); 421 case dwarf::DW_FORM_block: return Size + MCAsmInfo::getULEB128Size(Size); 422 default: llvm_unreachable("Improper form for block"); break; 423 } 424 return 0; 425 } 426 427 #ifndef NDEBUG 428 void DIEBlock::print(raw_ostream &O) { 429 O << "Blk: "; 430 DIE::print(O, 5); 431 } 432 #endif 433