LLVM API Documentation
00001 //===--- lib/CodeGen/DIE.cpp - DWARF Info Entries -------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Data structures for DWARF info entries. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "DIE.h" 00015 #include "DwarfPrinter.h" 00016 #include "llvm/CodeGen/AsmPrinter.h" 00017 #include "llvm/Target/TargetAsmInfo.h" 00018 #include "llvm/Target/TargetData.h" 00019 #include "llvm/Support/ErrorHandling.h" 00020 #include <ostream> 00021 using namespace llvm; 00022 00023 //===----------------------------------------------------------------------===// 00024 // DIEAbbrevData Implementation 00025 //===----------------------------------------------------------------------===// 00026 00027 /// Profile - Used to gather unique data for the abbreviation folding set. 00028 /// 00029 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const { 00030 ID.AddInteger(Attribute); 00031 ID.AddInteger(Form); 00032 } 00033 00034 //===----------------------------------------------------------------------===// 00035 // DIEAbbrev Implementation 00036 //===----------------------------------------------------------------------===// 00037 00038 /// Profile - Used to gather unique data for the abbreviation folding set. 00039 /// 00040 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const { 00041 ID.AddInteger(Tag); 00042 ID.AddInteger(ChildrenFlag); 00043 00044 // For each attribute description. 00045 for (unsigned i = 0, N = Data.size(); i < N; ++i) 00046 Data[i].Profile(ID); 00047 } 00048 00049 /// Emit - Print the abbreviation using the specified asm printer. 00050 /// 00051 void DIEAbbrev::Emit(const AsmPrinter *Asm) const { 00052 // Emit its Dwarf tag type. 00053 Asm->EmitULEB128Bytes(Tag); 00054 Asm->EOL(dwarf::TagString(Tag)); 00055 00056 // Emit whether it has children DIEs. 00057 Asm->EmitULEB128Bytes(ChildrenFlag); 00058 Asm->EOL(dwarf::ChildrenString(ChildrenFlag)); 00059 00060 // For each attribute description. 00061 for (unsigned i = 0, N = Data.size(); i < N; ++i) { 00062 const DIEAbbrevData &AttrData = Data[i]; 00063 00064 // Emit attribute type. 00065 Asm->EmitULEB128Bytes(AttrData.getAttribute()); 00066 Asm->EOL(dwarf::AttributeString(AttrData.getAttribute())); 00067 00068 // Emit form type. 00069 Asm->EmitULEB128Bytes(AttrData.getForm()); 00070 Asm->EOL(dwarf::FormEncodingString(AttrData.getForm())); 00071 } 00072 00073 // Mark end of abbreviation. 00074 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(1)"); 00075 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(2)"); 00076 } 00077 00078 #ifndef NDEBUG 00079 void DIEAbbrev::print(std::ostream &O) { 00080 O << "Abbreviation @" 00081 << std::hex << (intptr_t)this << std::dec 00082 << " " 00083 << dwarf::TagString(Tag) 00084 << " " 00085 << dwarf::ChildrenString(ChildrenFlag) 00086 << "\n"; 00087 00088 for (unsigned i = 0, N = Data.size(); i < N; ++i) { 00089 O << " " 00090 << dwarf::AttributeString(Data[i].getAttribute()) 00091 << " " 00092 << dwarf::FormEncodingString(Data[i].getForm()) 00093 << "\n"; 00094 } 00095 } 00096 void DIEAbbrev::dump() { print(cerr); } 00097 #endif 00098 00099 //===----------------------------------------------------------------------===// 00100 // DIE Implementation 00101 //===----------------------------------------------------------------------===// 00102 00103 DIE::~DIE() { 00104 for (unsigned i = 0, N = Children.size(); i < N; ++i) 00105 delete Children[i]; 00106 } 00107 00108 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE. 00109 /// 00110 void DIE::AddSiblingOffset() { 00111 DIEInteger *DI = new DIEInteger(0); 00112 Values.insert(Values.begin(), DI); 00113 Abbrev.AddFirstAttribute(dwarf::DW_AT_sibling, dwarf::DW_FORM_ref4); 00114 } 00115 00116 /// Profile - Used to gather unique data for the value folding set. 00117 /// 00118 void DIE::Profile(FoldingSetNodeID &ID) { 00119 Abbrev.Profile(ID); 00120 00121 for (unsigned i = 0, N = Children.size(); i < N; ++i) 00122 ID.AddPointer(Children[i]); 00123 00124 for (unsigned j = 0, M = Values.size(); j < M; ++j) 00125 ID.AddPointer(Values[j]); 00126 } 00127 00128 #ifndef NDEBUG 00129 void DIE::print(std::ostream &O, unsigned IncIndent) { 00130 IndentCount += IncIndent; 00131 const std::string Indent(IndentCount, ' '); 00132 bool isBlock = Abbrev.getTag() == 0; 00133 00134 if (!isBlock) { 00135 O << Indent 00136 << "Die: " 00137 << "0x" << std::hex << (intptr_t)this << std::dec 00138 << ", Offset: " << Offset 00139 << ", Size: " << Size 00140 << "\n"; 00141 00142 O << Indent 00143 << dwarf::TagString(Abbrev.getTag()) 00144 << " " 00145 << dwarf::ChildrenString(Abbrev.getChildrenFlag()); 00146 } else { 00147 O << "Size: " << Size; 00148 } 00149 O << "\n"; 00150 00151 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData(); 00152 00153 IndentCount += 2; 00154 for (unsigned i = 0, N = Data.size(); i < N; ++i) { 00155 O << Indent; 00156 00157 if (!isBlock) 00158 O << dwarf::AttributeString(Data[i].getAttribute()); 00159 else 00160 O << "Blk[" << i << "]"; 00161 00162 O << " " 00163 << dwarf::FormEncodingString(Data[i].getForm()) 00164 << " "; 00165 Values[i]->print(O); 00166 O << "\n"; 00167 } 00168 IndentCount -= 2; 00169 00170 for (unsigned j = 0, M = Children.size(); j < M; ++j) { 00171 Children[j]->print(O, 4); 00172 } 00173 00174 if (!isBlock) O << "\n"; 00175 IndentCount -= IncIndent; 00176 } 00177 00178 void DIE::dump() { 00179 print(cerr); 00180 } 00181 #endif 00182 00183 00184 #ifndef NDEBUG 00185 void DIEValue::dump() { 00186 print(cerr); 00187 } 00188 #endif 00189 00190 //===----------------------------------------------------------------------===// 00191 // DIEInteger Implementation 00192 //===----------------------------------------------------------------------===// 00193 00194 /// EmitValue - Emit integer of appropriate size. 00195 /// 00196 void DIEInteger::EmitValue(Dwarf *D, unsigned Form) const { 00197 const AsmPrinter *Asm = D->getAsm(); 00198 switch (Form) { 00199 case dwarf::DW_FORM_flag: // Fall thru 00200 case dwarf::DW_FORM_ref1: // Fall thru 00201 case dwarf::DW_FORM_data1: Asm->EmitInt8(Integer); break; 00202 case dwarf::DW_FORM_ref2: // Fall thru 00203 case dwarf::DW_FORM_data2: Asm->EmitInt16(Integer); break; 00204 case dwarf::DW_FORM_ref4: // Fall thru 00205 case dwarf::DW_FORM_data4: Asm->EmitInt32(Integer); break; 00206 case dwarf::DW_FORM_ref8: // Fall thru 00207 case dwarf::DW_FORM_data8: Asm->EmitInt64(Integer); break; 00208 case dwarf::DW_FORM_udata: Asm->EmitULEB128Bytes(Integer); break; 00209 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128Bytes(Integer); break; 00210 default: llvm_unreachable("DIE Value form not supported yet"); 00211 } 00212 } 00213 00214 /// SizeOf - Determine size of integer value in bytes. 00215 /// 00216 unsigned DIEInteger::SizeOf(const TargetData *TD, unsigned Form) const { 00217 switch (Form) { 00218 case dwarf::DW_FORM_flag: // Fall thru 00219 case dwarf::DW_FORM_ref1: // Fall thru 00220 case dwarf::DW_FORM_data1: return sizeof(int8_t); 00221 case dwarf::DW_FORM_ref2: // Fall thru 00222 case dwarf::DW_FORM_data2: return sizeof(int16_t); 00223 case dwarf::DW_FORM_ref4: // Fall thru 00224 case dwarf::DW_FORM_data4: return sizeof(int32_t); 00225 case dwarf::DW_FORM_ref8: // Fall thru 00226 case dwarf::DW_FORM_data8: return sizeof(int64_t); 00227 case dwarf::DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer); 00228 case dwarf::DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer); 00229 default: llvm_unreachable("DIE Value form not supported yet"); break; 00230 } 00231 return 0; 00232 } 00233 00234 /// Profile - Used to gather unique data for the value folding set. 00235 /// 00236 void DIEInteger::Profile(FoldingSetNodeID &ID, unsigned Int) { 00237 ID.AddInteger(isInteger); 00238 ID.AddInteger(Int); 00239 } 00240 void DIEInteger::Profile(FoldingSetNodeID &ID) { 00241 Profile(ID, Integer); 00242 } 00243 00244 #ifndef NDEBUG 00245 void DIEInteger::print(std::ostream &O) { 00246 O << "Int: " << (int64_t)Integer 00247 << " 0x" << std::hex << Integer << std::dec; 00248 } 00249 #endif 00250 00251 //===----------------------------------------------------------------------===// 00252 // DIEString Implementation 00253 //===----------------------------------------------------------------------===// 00254 00255 /// EmitValue - Emit string value. 00256 /// 00257 void DIEString::EmitValue(Dwarf *D, unsigned Form) const { 00258 D->getAsm()->EmitString(Str); 00259 } 00260 00261 /// Profile - Used to gather unique data for the value folding set. 00262 /// 00263 void DIEString::Profile(FoldingSetNodeID &ID, const std::string &Str) { 00264 ID.AddInteger(isString); 00265 ID.AddString(Str); 00266 } 00267 void DIEString::Profile(FoldingSetNodeID &ID) { 00268 Profile(ID, Str); 00269 } 00270 00271 #ifndef NDEBUG 00272 void DIEString::print(std::ostream &O) { 00273 O << "Str: \"" << Str << "\""; 00274 } 00275 #endif 00276 00277 //===----------------------------------------------------------------------===// 00278 // DIEDwarfLabel Implementation 00279 //===----------------------------------------------------------------------===// 00280 00281 /// EmitValue - Emit label value. 00282 /// 00283 void DIEDwarfLabel::EmitValue(Dwarf *D, unsigned Form) const { 00284 bool IsSmall = Form == dwarf::DW_FORM_data4; 00285 D->EmitReference(Label, false, IsSmall); 00286 } 00287 00288 /// SizeOf - Determine size of label value in bytes. 00289 /// 00290 unsigned DIEDwarfLabel::SizeOf(const TargetData *TD, unsigned Form) const { 00291 if (Form == dwarf::DW_FORM_data4) return 4; 00292 return TD->getPointerSize(); 00293 } 00294 00295 /// Profile - Used to gather unique data for the value folding set. 00296 /// 00297 void DIEDwarfLabel::Profile(FoldingSetNodeID &ID, const DWLabel &Label) { 00298 ID.AddInteger(isLabel); 00299 Label.Profile(ID); 00300 } 00301 void DIEDwarfLabel::Profile(FoldingSetNodeID &ID) { 00302 Profile(ID, Label); 00303 } 00304 00305 #ifndef NDEBUG 00306 void DIEDwarfLabel::print(std::ostream &O) { 00307 O << "Lbl: "; 00308 Label.print(O); 00309 } 00310 #endif 00311 00312 //===----------------------------------------------------------------------===// 00313 // DIEObjectLabel Implementation 00314 //===----------------------------------------------------------------------===// 00315 00316 /// EmitValue - Emit label value. 00317 /// 00318 void DIEObjectLabel::EmitValue(Dwarf *D, unsigned Form) const { 00319 bool IsSmall = Form == dwarf::DW_FORM_data4; 00320 D->EmitReference(Label, false, IsSmall); 00321 } 00322 00323 /// SizeOf - Determine size of label value in bytes. 00324 /// 00325 unsigned DIEObjectLabel::SizeOf(const TargetData *TD, unsigned Form) const { 00326 if (Form == dwarf::DW_FORM_data4) return 4; 00327 return TD->getPointerSize(); 00328 } 00329 00330 /// Profile - Used to gather unique data for the value folding set. 00331 /// 00332 void DIEObjectLabel::Profile(FoldingSetNodeID &ID, const std::string &Label) { 00333 ID.AddInteger(isAsIsLabel); 00334 ID.AddString(Label); 00335 } 00336 void DIEObjectLabel::Profile(FoldingSetNodeID &ID) { 00337 Profile(ID, Label.c_str()); 00338 } 00339 00340 #ifndef NDEBUG 00341 void DIEObjectLabel::print(std::ostream &O) { 00342 O << "Obj: " << Label; 00343 } 00344 #endif 00345 00346 //===----------------------------------------------------------------------===// 00347 // DIESectionOffset Implementation 00348 //===----------------------------------------------------------------------===// 00349 00350 /// EmitValue - Emit delta value. 00351 /// 00352 void DIESectionOffset::EmitValue(Dwarf *D, unsigned Form) const { 00353 bool IsSmall = Form == dwarf::DW_FORM_data4; 00354 D->EmitSectionOffset(Label.getTag(), Section.getTag(), 00355 Label.getNumber(), Section.getNumber(), 00356 IsSmall, IsEH, UseSet); 00357 } 00358 00359 /// SizeOf - Determine size of delta value in bytes. 00360 /// 00361 unsigned DIESectionOffset::SizeOf(const TargetData *TD, unsigned Form) const { 00362 if (Form == dwarf::DW_FORM_data4) return 4; 00363 return TD->getPointerSize(); 00364 } 00365 00366 /// Profile - Used to gather unique data for the value folding set. 00367 /// 00368 void DIESectionOffset::Profile(FoldingSetNodeID &ID, const DWLabel &Label, 00369 const DWLabel &Section) { 00370 ID.AddInteger(isSectionOffset); 00371 Label.Profile(ID); 00372 Section.Profile(ID); 00373 // IsEH and UseSet are specific to the Label/Section that we will emit the 00374 // offset for; so Label/Section are enough for uniqueness. 00375 } 00376 void DIESectionOffset::Profile(FoldingSetNodeID &ID) { 00377 Profile(ID, Label, Section); 00378 } 00379 00380 #ifndef NDEBUG 00381 void DIESectionOffset::print(std::ostream &O) { 00382 O << "Off: "; 00383 Label.print(O); 00384 O << "-"; 00385 Section.print(O); 00386 O << "-" << IsEH << "-" << UseSet; 00387 } 00388 #endif 00389 00390 //===----------------------------------------------------------------------===// 00391 // DIEDelta Implementation 00392 //===----------------------------------------------------------------------===// 00393 00394 /// EmitValue - Emit delta value. 00395 /// 00396 void DIEDelta::EmitValue(Dwarf *D, unsigned Form) const { 00397 bool IsSmall = Form == dwarf::DW_FORM_data4; 00398 D->EmitDifference(LabelHi, LabelLo, IsSmall); 00399 } 00400 00401 /// SizeOf - Determine size of delta value in bytes. 00402 /// 00403 unsigned DIEDelta::SizeOf(const TargetData *TD, unsigned Form) const { 00404 if (Form == dwarf::DW_FORM_data4) return 4; 00405 return TD->getPointerSize(); 00406 } 00407 00408 /// Profile - Used to gather unique data for the value folding set. 00409 /// 00410 void DIEDelta::Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi, 00411 const DWLabel &LabelLo) { 00412 ID.AddInteger(isDelta); 00413 LabelHi.Profile(ID); 00414 LabelLo.Profile(ID); 00415 } 00416 void DIEDelta::Profile(FoldingSetNodeID &ID) { 00417 Profile(ID, LabelHi, LabelLo); 00418 } 00419 00420 #ifndef NDEBUG 00421 void DIEDelta::print(std::ostream &O) { 00422 O << "Del: "; 00423 LabelHi.print(O); 00424 O << "-"; 00425 LabelLo.print(O); 00426 } 00427 #endif 00428 00429 //===----------------------------------------------------------------------===// 00430 // DIEEntry Implementation 00431 //===----------------------------------------------------------------------===// 00432 00433 /// EmitValue - Emit debug information entry offset. 00434 /// 00435 void DIEEntry::EmitValue(Dwarf *D, unsigned Form) const { 00436 D->getAsm()->EmitInt32(Entry->getOffset()); 00437 } 00438 00439 /// Profile - Used to gather unique data for the value folding set. 00440 /// 00441 void DIEEntry::Profile(FoldingSetNodeID &ID, DIE *Entry) { 00442 ID.AddInteger(isEntry); 00443 ID.AddPointer(Entry); 00444 } 00445 void DIEEntry::Profile(FoldingSetNodeID &ID) { 00446 ID.AddInteger(isEntry); 00447 00448 if (Entry) 00449 ID.AddPointer(Entry); 00450 else 00451 ID.AddPointer(this); 00452 } 00453 00454 #ifndef NDEBUG 00455 void DIEEntry::print(std::ostream &O) { 00456 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec; 00457 } 00458 #endif 00459 00460 //===----------------------------------------------------------------------===// 00461 // DIEBlock Implementation 00462 //===----------------------------------------------------------------------===// 00463 00464 /// ComputeSize - calculate the size of the block. 00465 /// 00466 unsigned DIEBlock::ComputeSize(const TargetData *TD) { 00467 if (!Size) { 00468 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData(); 00469 for (unsigned i = 0, N = Values.size(); i < N; ++i) 00470 Size += Values[i]->SizeOf(TD, AbbrevData[i].getForm()); 00471 } 00472 00473 return Size; 00474 } 00475 00476 /// EmitValue - Emit block data. 00477 /// 00478 void DIEBlock::EmitValue(Dwarf *D, unsigned Form) const { 00479 const AsmPrinter *Asm = D->getAsm(); 00480 switch (Form) { 00481 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break; 00482 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break; 00483 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break; 00484 case dwarf::DW_FORM_block: Asm->EmitULEB128Bytes(Size); break; 00485 default: llvm_unreachable("Improper form for block"); break; 00486 } 00487 00488 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData(); 00489 for (unsigned i = 0, N = Values.size(); i < N; ++i) { 00490 Asm->EOL(); 00491 Values[i]->EmitValue(D, AbbrevData[i].getForm()); 00492 } 00493 } 00494 00495 /// SizeOf - Determine size of block data in bytes. 00496 /// 00497 unsigned DIEBlock::SizeOf(const TargetData *TD, unsigned Form) const { 00498 switch (Form) { 00499 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t); 00500 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t); 00501 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t); 00502 case dwarf::DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size); 00503 default: llvm_unreachable("Improper form for block"); break; 00504 } 00505 return 0; 00506 } 00507 00508 void DIEBlock::Profile(FoldingSetNodeID &ID) { 00509 ID.AddInteger(isBlock); 00510 DIE::Profile(ID); 00511 } 00512 00513 #ifndef NDEBUG 00514 void DIEBlock::print(std::ostream &O) { 00515 O << "Blk: "; 00516 DIE::print(O, 5); 00517 } 00518 #endif