LLVM API Documentation

DIE.cpp
Go to the documentation of this file.
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 "llvm/ADT/Twine.h"
00016 #include "llvm/CodeGen/AsmPrinter.h"
00017 #include "llvm/IR/DataLayout.h"
00018 #include "llvm/MC/MCAsmInfo.h"
00019 #include "llvm/MC/MCStreamer.h"
00020 #include "llvm/MC/MCSymbol.h"
00021 #include "llvm/Support/Allocator.h"
00022 #include "llvm/Support/Debug.h"
00023 #include "llvm/Support/ErrorHandling.h"
00024 #include "llvm/Support/Format.h"
00025 #include "llvm/Support/FormattedStream.h"
00026 using namespace llvm;
00027 
00028 //===----------------------------------------------------------------------===//
00029 // DIEAbbrevData Implementation
00030 //===----------------------------------------------------------------------===//
00031 
00032 /// Profile - Used to gather unique data for the abbreviation folding set.
00033 ///
00034 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
00035   ID.AddInteger(Attribute);
00036   ID.AddInteger(Form);
00037 }
00038 
00039 //===----------------------------------------------------------------------===//
00040 // DIEAbbrev Implementation
00041 //===----------------------------------------------------------------------===//
00042 
00043 /// Profile - Used to gather unique data for the abbreviation folding set.
00044 ///
00045 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
00046   ID.AddInteger(Tag);
00047   ID.AddInteger(ChildrenFlag);
00048 
00049   // For each attribute description.
00050   for (unsigned i = 0, N = Data.size(); i < N; ++i)
00051     Data[i].Profile(ID);
00052 }
00053 
00054 /// Emit - Print the abbreviation using the specified asm printer.
00055 ///
00056 void DIEAbbrev::Emit(AsmPrinter *AP) const {
00057   // Emit its Dwarf tag type.
00058   AP->EmitULEB128(Tag, dwarf::TagString(Tag));
00059 
00060   // Emit whether it has children DIEs.
00061   AP->EmitULEB128(ChildrenFlag, dwarf::ChildrenString(ChildrenFlag));
00062 
00063   // For each attribute description.
00064   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
00065     const DIEAbbrevData &AttrData = Data[i];
00066 
00067     // Emit attribute type.
00068     AP->EmitULEB128(AttrData.getAttribute(),
00069                     dwarf::AttributeString(AttrData.getAttribute()));
00070 
00071     // Emit form type.
00072     AP->EmitULEB128(AttrData.getForm(),
00073                     dwarf::FormEncodingString(AttrData.getForm()));
00074   }
00075 
00076   // Mark end of abbreviation.
00077   AP->EmitULEB128(0, "EOM(1)");
00078   AP->EmitULEB128(0, "EOM(2)");
00079 }
00080 
00081 #ifndef NDEBUG
00082 void DIEAbbrev::print(raw_ostream &O) {
00083   O << "Abbreviation @"
00084     << format("0x%lx", (long)(intptr_t)this)
00085     << "  "
00086     << dwarf::TagString(Tag)
00087     << " "
00088     << dwarf::ChildrenString(ChildrenFlag)
00089     << '\n';
00090 
00091   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
00092     O << "  "
00093       << dwarf::AttributeString(Data[i].getAttribute())
00094       << "  "
00095       << dwarf::FormEncodingString(Data[i].getForm())
00096       << '\n';
00097   }
00098 }
00099 void DIEAbbrev::dump() { print(dbgs()); }
00100 #endif
00101 
00102 //===----------------------------------------------------------------------===//
00103 // DIE Implementation
00104 //===----------------------------------------------------------------------===//
00105 
00106 DIE::~DIE() {
00107   for (unsigned i = 0, N = Children.size(); i < N; ++i)
00108     delete Children[i];
00109 }
00110 
00111 /// Climb up the parent chain to get the compile unit DIE to which this DIE
00112 /// belongs.
00113 DIE *DIE::getCompileUnit() {
00114   DIE *p = this;
00115   while (p) {
00116     if (p->getTag() == dwarf::DW_TAG_compile_unit)
00117       return p;
00118     p = p->getParent();
00119   }
00120   llvm_unreachable("We should not have orphaned DIEs.");
00121 }
00122 
00123 #ifndef NDEBUG
00124 void DIE::print(raw_ostream &O, unsigned IndentCount) const {
00125   const std::string Indent(IndentCount, ' ');
00126   bool isBlock = Abbrev.getTag() == 0;
00127 
00128   if (!isBlock) {
00129     O << Indent
00130       << "Die: "
00131       << format("0x%lx", (long)(intptr_t)this)
00132       << ", Offset: " << Offset
00133       << ", Size: " << Size << "\n";
00134 
00135     O << Indent
00136       << dwarf::TagString(Abbrev.getTag())
00137       << " "
00138       << dwarf::ChildrenString(Abbrev.getChildrenFlag()) << "\n";
00139   } else {
00140     O << "Size: " << Size << "\n";
00141   }
00142 
00143   const SmallVectorImpl<DIEAbbrevData> &Data = Abbrev.getData();
00144 
00145   IndentCount += 2;
00146   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
00147     O << Indent;
00148 
00149     if (!isBlock)
00150       O << dwarf::AttributeString(Data[i].getAttribute());
00151     else
00152       O << "Blk[" << i << "]";
00153 
00154     O <<  "  "
00155       << dwarf::FormEncodingString(Data[i].getForm())
00156       << " ";
00157     Values[i]->print(O);
00158     O << "\n";
00159   }
00160   IndentCount -= 2;
00161 
00162   for (unsigned j = 0, M = Children.size(); j < M; ++j) {
00163     Children[j]->print(O, IndentCount+4);
00164   }
00165 
00166   if (!isBlock) O << "\n";
00167 }
00168 
00169 void DIE::dump() {
00170   print(dbgs());
00171 }
00172 #endif
00173 
00174 void DIEValue::anchor() { }
00175 
00176 #ifndef NDEBUG
00177 void DIEValue::dump() const {
00178   print(dbgs());
00179 }
00180 #endif
00181 
00182 //===----------------------------------------------------------------------===//
00183 // DIEInteger Implementation
00184 //===----------------------------------------------------------------------===//
00185 
00186 /// EmitValue - Emit integer of appropriate size.
00187 ///
00188 void DIEInteger::EmitValue(AsmPrinter *Asm, unsigned Form) const {
00189   unsigned Size = ~0U;
00190   switch (Form) {
00191   case dwarf::DW_FORM_flag_present:
00192     // Emit something to keep the lines and comments in sync.
00193     // FIXME: Is there a better way to do this?
00194     if (Asm->OutStreamer.hasRawTextSupport())
00195       Asm->OutStreamer.EmitRawText(StringRef(""));
00196     return;
00197   case dwarf::DW_FORM_flag:  // Fall thru
00198   case dwarf::DW_FORM_ref1:  // Fall thru
00199   case dwarf::DW_FORM_data1: Size = 1; break;
00200   case dwarf::DW_FORM_ref2:  // Fall thru
00201   case dwarf::DW_FORM_data2: Size = 2; break;
00202   case dwarf::DW_FORM_sec_offset: // Fall thru
00203   case dwarf::DW_FORM_ref4:  // Fall thru
00204   case dwarf::DW_FORM_data4: Size = 4; break;
00205   case dwarf::DW_FORM_ref8:  // Fall thru
00206   case dwarf::DW_FORM_data8: Size = 8; break;
00207   case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
00208   case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
00209   case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
00210   case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
00211   case dwarf::DW_FORM_addr:
00212     Size = Asm->getDataLayout().getPointerSize(); break;
00213   default: llvm_unreachable("DIE Value form not supported yet");
00214   }
00215   Asm->OutStreamer.EmitIntValue(Integer, Size);
00216 }
00217 
00218 /// SizeOf - Determine size of integer value in bytes.
00219 ///
00220 unsigned DIEInteger::SizeOf(AsmPrinter *AP, unsigned Form) const {
00221   switch (Form) {
00222   case dwarf::DW_FORM_flag_present: return 0;
00223   case dwarf::DW_FORM_flag:  // Fall thru
00224   case dwarf::DW_FORM_ref1:  // Fall thru
00225   case dwarf::DW_FORM_data1: return sizeof(int8_t);
00226   case dwarf::DW_FORM_ref2:  // Fall thru
00227   case dwarf::DW_FORM_data2: return sizeof(int16_t);
00228   case dwarf::DW_FORM_sec_offset: // Fall thru
00229   case dwarf::DW_FORM_ref4:  // Fall thru
00230   case dwarf::DW_FORM_data4: return sizeof(int32_t);
00231   case dwarf::DW_FORM_ref8:  // Fall thru
00232   case dwarf::DW_FORM_data8: return sizeof(int64_t);
00233   case dwarf::DW_FORM_GNU_str_index: return MCAsmInfo::getULEB128Size(Integer);
00234   case dwarf::DW_FORM_GNU_addr_index: return MCAsmInfo::getULEB128Size(Integer);
00235   case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
00236   case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
00237   case dwarf::DW_FORM_addr:  return AP->getDataLayout().getPointerSize();
00238   default: llvm_unreachable("DIE Value form not supported yet");
00239   }
00240 }
00241 
00242 #ifndef NDEBUG
00243 void DIEInteger::print(raw_ostream &O) const {
00244   O << "Int: " << (int64_t)Integer << "  0x";
00245   O.write_hex(Integer);
00246 }
00247 #endif
00248 
00249 //===----------------------------------------------------------------------===//
00250 // DIELabel Implementation
00251 //===----------------------------------------------------------------------===//
00252 
00253 /// EmitValue - Emit label value.
00254 ///
00255 void DIELabel::EmitValue(AsmPrinter *AP, unsigned Form) const {
00256   AP->OutStreamer.EmitSymbolValue(Label, SizeOf(AP, Form));
00257 }
00258 
00259 /// SizeOf - Determine size of label value in bytes.
00260 ///
00261 unsigned DIELabel::SizeOf(AsmPrinter *AP, unsigned Form) const {
00262   if (Form == dwarf::DW_FORM_data4) return 4;
00263   if (Form == dwarf::DW_FORM_sec_offset) return 4;
00264   if (Form == dwarf::DW_FORM_strp) return 4;
00265   return AP->getDataLayout().getPointerSize();
00266 }
00267 
00268 #ifndef NDEBUG
00269 void DIELabel::print(raw_ostream &O) const {
00270   O << "Lbl: " << Label->getName();
00271 }
00272 #endif
00273 
00274 //===----------------------------------------------------------------------===//
00275 // DIEDelta Implementation
00276 //===----------------------------------------------------------------------===//
00277 
00278 /// EmitValue - Emit delta value.
00279 ///
00280 void DIEDelta::EmitValue(AsmPrinter *AP, unsigned Form) const {
00281   AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
00282 }
00283 
00284 /// SizeOf - Determine size of delta value in bytes.
00285 ///
00286 unsigned DIEDelta::SizeOf(AsmPrinter *AP, unsigned Form) const {
00287   if (Form == dwarf::DW_FORM_data4) return 4;
00288   if (Form == dwarf::DW_FORM_strp) return 4;
00289   return AP->getDataLayout().getPointerSize();
00290 }
00291 
00292 #ifndef NDEBUG
00293 void DIEDelta::print(raw_ostream &O) const {
00294   O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
00295 }
00296 #endif
00297 
00298 //===----------------------------------------------------------------------===//
00299 // DIEEntry Implementation
00300 //===----------------------------------------------------------------------===//
00301 
00302 /// EmitValue - Emit debug information entry offset.
00303 ///
00304 void DIEEntry::EmitValue(AsmPrinter *AP, unsigned Form) const {
00305   AP->EmitInt32(Entry->getOffset());
00306 }
00307 
00308 #ifndef NDEBUG
00309 void DIEEntry::print(raw_ostream &O) const {
00310   O << format("Die: 0x%lx", (long)(intptr_t)Entry);
00311 }
00312 #endif
00313 
00314 //===----------------------------------------------------------------------===//
00315 // DIEBlock Implementation
00316 //===----------------------------------------------------------------------===//
00317 
00318 /// ComputeSize - calculate the size of the block.
00319 ///
00320 unsigned DIEBlock::ComputeSize(AsmPrinter *AP) {
00321   if (!Size) {
00322     const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
00323     for (unsigned i = 0, N = Values.size(); i < N; ++i)
00324       Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
00325   }
00326 
00327   return Size;
00328 }
00329 
00330 /// EmitValue - Emit block data.
00331 ///
00332 void DIEBlock::EmitValue(AsmPrinter *Asm, unsigned Form) const {
00333   switch (Form) {
00334   default: llvm_unreachable("Improper form for block");
00335   case dwarf::DW_FORM_block1: Asm->EmitInt8(Size);    break;
00336   case dwarf::DW_FORM_block2: Asm->EmitInt16(Size);   break;
00337   case dwarf::DW_FORM_block4: Asm->EmitInt32(Size);   break;
00338   case dwarf::DW_FORM_block:  Asm->EmitULEB128(Size); break;
00339   }
00340 
00341   const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
00342   for (unsigned i = 0, N = Values.size(); i < N; ++i)
00343     Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
00344 }
00345 
00346 /// SizeOf - Determine size of block data in bytes.
00347 ///
00348 unsigned DIEBlock::SizeOf(AsmPrinter *AP, unsigned Form) const {
00349   switch (Form) {
00350   case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
00351   case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
00352   case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
00353   case dwarf::DW_FORM_block:  return Size + MCAsmInfo::getULEB128Size(Size);
00354   default: llvm_unreachable("Improper form for block");
00355   }
00356 }
00357 
00358 #ifndef NDEBUG
00359 void DIEBlock::print(raw_ostream &O) const {
00360   O << "Blk: ";
00361   DIE::print(O, 5);
00362 }
00363 #endif