LLVM API Documentation

DwarfAccelTable.cpp
Go to the documentation of this file.
00001 //=-- llvm/CodeGen/DwarfAccelTable.cpp - Dwarf Accelerator Tables -*- C++ -*-=//
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 // This file contains support for writing dwarf accelerator tables.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "DwarfAccelTable.h"
00015 #include "DIE.h"
00016 #include "DwarfDebug.h"
00017 #include "llvm/ADT/STLExtras.h"
00018 #include "llvm/ADT/Twine.h"
00019 #include "llvm/CodeGen/AsmPrinter.h"
00020 #include "llvm/MC/MCExpr.h"
00021 #include "llvm/MC/MCStreamer.h"
00022 #include "llvm/MC/MCSymbol.h"
00023 #include "llvm/Support/Debug.h"
00024 
00025 using namespace llvm;
00026 
00027 const char *DwarfAccelTable::Atom::AtomTypeString(enum AtomType AT) {
00028   switch (AT) {
00029   case eAtomTypeNULL: return "eAtomTypeNULL";
00030   case eAtomTypeDIEOffset: return "eAtomTypeDIEOffset";
00031   case eAtomTypeCUOffset: return "eAtomTypeCUOffset";
00032   case eAtomTypeTag: return "eAtomTypeTag";
00033   case eAtomTypeNameFlags: return "eAtomTypeNameFlags";
00034   case eAtomTypeTypeFlags: return "eAtomTypeTypeFlags";
00035   }
00036   llvm_unreachable("invalid AtomType!");
00037 }
00038 
00039 // The length of the header data is always going to be 4 + 4 + 4*NumAtoms.
00040 DwarfAccelTable::DwarfAccelTable(ArrayRef<DwarfAccelTable::Atom> atomList) :
00041   Header(8 + (atomList.size() * 4)),
00042   HeaderData(atomList),
00043   Entries(Allocator) { }
00044 
00045 DwarfAccelTable::~DwarfAccelTable() { }
00046 
00047 void DwarfAccelTable::AddName(StringRef Name, DIE* die, char Flags) {
00048   assert(Data.empty() && "Already finalized!");
00049   // If the string is in the list already then add this die to the list
00050   // otherwise add a new one.
00051   DataArray &DIEs = Entries[Name];
00052   DIEs.push_back(new (Allocator) HashDataContents(die, Flags));
00053 }
00054 
00055 void DwarfAccelTable::ComputeBucketCount(void) {
00056   // First get the number of unique hashes.
00057   std::vector<uint32_t> uniques(Data.size());
00058   for (size_t i = 0, e = Data.size(); i < e; ++i)
00059     uniques[i] = Data[i]->HashValue;
00060   array_pod_sort(uniques.begin(), uniques.end());
00061   std::vector<uint32_t>::iterator p =
00062     std::unique(uniques.begin(), uniques.end());
00063   uint32_t num = std::distance(uniques.begin(), p);
00064 
00065   // Then compute the bucket size, minimum of 1 bucket.
00066   if (num > 1024) Header.bucket_count = num/4;
00067   if (num > 16) Header.bucket_count = num/2;
00068   else Header.bucket_count = num > 0 ? num : 1;
00069 
00070   Header.hashes_count = num;
00071 }
00072 
00073 // compareDIEs - comparison predicate that sorts DIEs by their offset.
00074 static bool compareDIEs(const DwarfAccelTable::HashDataContents *A,
00075                         const DwarfAccelTable::HashDataContents *B) {
00076   return A->Die->getOffset() < B->Die->getOffset();
00077 }
00078 
00079 void DwarfAccelTable::FinalizeTable(AsmPrinter *Asm, StringRef Prefix) {
00080   // Create the individual hash data outputs.
00081   for (StringMap<DataArray>::iterator
00082          EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) {
00083 
00084     // Unique the entries.
00085     std::stable_sort(EI->second.begin(), EI->second.end(), compareDIEs);
00086     EI->second.erase(std::unique(EI->second.begin(), EI->second.end()),
00087                        EI->second.end());
00088 
00089     HashData *Entry = new (Allocator) HashData(EI->getKey(), EI->second);
00090     Data.push_back(Entry);
00091   }
00092 
00093   // Figure out how many buckets we need, then compute the bucket
00094   // contents and the final ordering. We'll emit the hashes and offsets
00095   // by doing a walk during the emission phase. We add temporary
00096   // symbols to the data so that we can reference them during the offset
00097   // later, we'll emit them when we emit the data.
00098   ComputeBucketCount();
00099 
00100   // Compute bucket contents and final ordering.
00101   Buckets.resize(Header.bucket_count);
00102   for (size_t i = 0, e = Data.size(); i < e; ++i) {
00103     uint32_t bucket = Data[i]->HashValue % Header.bucket_count;
00104     Buckets[bucket].push_back(Data[i]);
00105     Data[i]->Sym = Asm->GetTempSymbol(Prefix, i);
00106   }
00107 }
00108 
00109 // Emits the header for the table via the AsmPrinter.
00110 void DwarfAccelTable::EmitHeader(AsmPrinter *Asm) {
00111   Asm->OutStreamer.AddComment("Header Magic");
00112   Asm->EmitInt32(Header.magic);
00113   Asm->OutStreamer.AddComment("Header Version");
00114   Asm->EmitInt16(Header.version);
00115   Asm->OutStreamer.AddComment("Header Hash Function");
00116   Asm->EmitInt16(Header.hash_function);
00117   Asm->OutStreamer.AddComment("Header Bucket Count");
00118   Asm->EmitInt32(Header.bucket_count);
00119   Asm->OutStreamer.AddComment("Header Hash Count");
00120   Asm->EmitInt32(Header.hashes_count);
00121   Asm->OutStreamer.AddComment("Header Data Length");
00122   Asm->EmitInt32(Header.header_data_len);
00123   Asm->OutStreamer.AddComment("HeaderData Die Offset Base");
00124   Asm->EmitInt32(HeaderData.die_offset_base);
00125   Asm->OutStreamer.AddComment("HeaderData Atom Count");
00126   Asm->EmitInt32(HeaderData.Atoms.size());
00127   for (size_t i = 0; i < HeaderData.Atoms.size(); i++) {
00128     Atom A = HeaderData.Atoms[i];
00129     Asm->OutStreamer.AddComment(Atom::AtomTypeString(A.type));
00130     Asm->EmitInt16(A.type);
00131     Asm->OutStreamer.AddComment(dwarf::FormEncodingString(A.form));
00132     Asm->EmitInt16(A.form);
00133   }
00134 }
00135 
00136 // Walk through and emit the buckets for the table. Each index is
00137 // an offset into the list of hashes.
00138 void DwarfAccelTable::EmitBuckets(AsmPrinter *Asm) {
00139   unsigned index = 0;
00140   for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
00141     Asm->OutStreamer.AddComment("Bucket " + Twine(i));
00142     if (Buckets[i].size() != 0)
00143       Asm->EmitInt32(index);
00144     else
00145       Asm->EmitInt32(UINT32_MAX);
00146     index += Buckets[i].size();
00147   }
00148 }
00149 
00150 // Walk through the buckets and emit the individual hashes for each
00151 // bucket.
00152 void DwarfAccelTable::EmitHashes(AsmPrinter *Asm) {
00153   for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
00154     for (HashList::const_iterator HI = Buckets[i].begin(),
00155            HE = Buckets[i].end(); HI != HE; ++HI) {
00156       Asm->OutStreamer.AddComment("Hash in Bucket " + Twine(i));
00157       Asm->EmitInt32((*HI)->HashValue);
00158     }
00159   }
00160 }
00161 
00162 // Walk through the buckets and emit the individual offsets for each
00163 // element in each bucket. This is done via a symbol subtraction from the
00164 // beginning of the section. The non-section symbol will be output later
00165 // when we emit the actual data.
00166 void DwarfAccelTable::EmitOffsets(AsmPrinter *Asm, MCSymbol *SecBegin) {
00167   for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
00168     for (HashList::const_iterator HI = Buckets[i].begin(),
00169            HE = Buckets[i].end(); HI != HE; ++HI) {
00170       Asm->OutStreamer.AddComment("Offset in Bucket " + Twine(i));
00171       MCContext &Context = Asm->OutStreamer.getContext();
00172       const MCExpr *Sub =
00173         MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create((*HI)->Sym, Context),
00174                                 MCSymbolRefExpr::Create(SecBegin, Context),
00175                                 Context);
00176       Asm->OutStreamer.EmitValue(Sub, sizeof(uint32_t));
00177     }
00178   }
00179 }
00180 
00181 // Walk through the buckets and emit the full data for each element in
00182 // the bucket. For the string case emit the dies and the various offsets.
00183 // Terminate each HashData bucket with 0.
00184 void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfUnits *D) {
00185   uint64_t PrevHash = UINT64_MAX;
00186   for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
00187     for (HashList::const_iterator HI = Buckets[i].begin(),
00188            HE = Buckets[i].end(); HI != HE; ++HI) {
00189       // Remember to emit the label for our offset.
00190       Asm->OutStreamer.EmitLabel((*HI)->Sym);
00191       Asm->OutStreamer.AddComment((*HI)->Str);
00192       Asm->EmitSectionOffset(D->getStringPoolEntry((*HI)->Str),
00193                              D->getStringPoolSym());
00194       Asm->OutStreamer.AddComment("Num DIEs");
00195       Asm->EmitInt32((*HI)->Data.size());
00196       for (ArrayRef<HashDataContents*>::const_iterator
00197              DI = (*HI)->Data.begin(), DE = (*HI)->Data.end();
00198            DI != DE; ++DI) {
00199         // Emit the DIE offset
00200         Asm->EmitInt32((*DI)->Die->getOffset());
00201         // If we have multiple Atoms emit that info too.
00202         // FIXME: A bit of a hack, we either emit only one atom or all info.
00203         if (HeaderData.Atoms.size() > 1) {
00204           Asm->EmitInt16((*DI)->Die->getTag());
00205           Asm->EmitInt8((*DI)->Flags);
00206         }
00207       }
00208       // Emit a 0 to terminate the data unless we have a hash collision.
00209       if (PrevHash != (*HI)->HashValue)
00210         Asm->EmitInt32(0);
00211       PrevHash = (*HI)->HashValue;
00212     }
00213   }
00214 }
00215 
00216 // Emit the entire data structure to the output file.
00217 void DwarfAccelTable::Emit(AsmPrinter *Asm, MCSymbol *SecBegin,
00218                            DwarfUnits *D) {
00219   // Emit the header.
00220   EmitHeader(Asm);
00221 
00222   // Emit the buckets.
00223   EmitBuckets(Asm);
00224 
00225   // Emit the hashes.
00226   EmitHashes(Asm);
00227 
00228   // Emit the offsets.
00229   EmitOffsets(Asm, SecBegin);
00230 
00231   // Emit the hash data.
00232   EmitData(Asm, D);
00233 }
00234 
00235 #ifndef NDEBUG
00236 void DwarfAccelTable::print(raw_ostream &O) {
00237 
00238   Header.print(O);
00239   HeaderData.print(O);
00240 
00241   O << "Entries: \n";
00242   for (StringMap<DataArray>::const_iterator
00243          EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) {
00244     O << "Name: " << EI->getKeyData() << "\n";
00245     for (DataArray::const_iterator DI = EI->second.begin(),
00246            DE = EI->second.end();
00247          DI != DE; ++DI)
00248       (*DI)->print(O);
00249   }
00250 
00251   O << "Buckets and Hashes: \n";
00252   for (size_t i = 0, e = Buckets.size(); i < e; ++i)
00253     for (HashList::const_iterator HI = Buckets[i].begin(),
00254            HE = Buckets[i].end(); HI != HE; ++HI)
00255       (*HI)->print(O);
00256 
00257   O << "Data: \n";
00258     for (std::vector<HashData*>::const_iterator
00259            DI = Data.begin(), DE = Data.end(); DI != DE; ++DI)
00260       (*DI)->print(O);
00261 
00262 
00263 }
00264 #endif