2011-11-07 09:18:42 +00:00
|
|
|
//=-- llvm/CodeGen/DwarfAccelTable.cpp - Dwarf Accelerator Tables -*- C++ -*-=//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains support for writing dwarf accelerator tables.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-03-26 06:58:25 +00:00
|
|
|
#include "DwarfAccelTable.h"
|
|
|
|
#include "DIE.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "DwarfDebug.h"
|
2012-03-26 14:17:26 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2011-11-07 09:18:42 +00:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCStreamer.h"
|
|
|
|
#include "llvm/MC/MCSymbol.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2012-01-06 04:35:23 +00:00
|
|
|
// The length of the header data is always going to be 4 + 4 + 4*NumAtoms.
|
2013-09-05 16:46:43 +00:00
|
|
|
DwarfAccelTable::DwarfAccelTable(ArrayRef<DwarfAccelTable::Atom> atomList)
|
|
|
|
: Header(8 + (atomList.size() * 4)), HeaderData(atomList),
|
|
|
|
Entries(Allocator) {}
|
2012-01-06 04:35:23 +00:00
|
|
|
|
2014-04-25 22:21:35 +00:00
|
|
|
void DwarfAccelTable::AddName(StringRef Name, MCSymbol *StrSym, const DIE *die,
|
|
|
|
char Flags) {
|
2012-04-13 20:06:17 +00:00
|
|
|
assert(Data.empty() && "Already finalized!");
|
2011-11-07 09:18:42 +00:00
|
|
|
// If the string is in the list already then add this die to the list
|
|
|
|
// otherwise add a new one.
|
2012-01-06 04:35:23 +00:00
|
|
|
DataArray &DIEs = Entries[Name];
|
2014-04-25 22:21:35 +00:00
|
|
|
assert(!DIEs.StrSym || DIEs.StrSym == StrSym);
|
|
|
|
DIEs.StrSym = StrSym;
|
|
|
|
DIEs.Values.push_back(new (Allocator) HashDataContents(die, Flags));
|
2011-11-07 09:18:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DwarfAccelTable::ComputeBucketCount(void) {
|
|
|
|
// First get the number of unique hashes.
|
2012-03-26 14:17:26 +00:00
|
|
|
std::vector<uint32_t> uniques(Data.size());
|
2011-11-08 18:38:40 +00:00
|
|
|
for (size_t i = 0, e = Data.size(); i < e; ++i)
|
2011-11-07 09:18:42 +00:00
|
|
|
uniques[i] = Data[i]->HashValue;
|
2012-03-26 14:17:26 +00:00
|
|
|
array_pod_sort(uniques.begin(), uniques.end());
|
2011-11-07 09:18:42 +00:00
|
|
|
std::vector<uint32_t>::iterator p =
|
2013-09-05 16:46:43 +00:00
|
|
|
std::unique(uniques.begin(), uniques.end());
|
2011-11-07 09:18:42 +00:00
|
|
|
uint32_t num = std::distance(uniques.begin(), p);
|
|
|
|
|
|
|
|
// Then compute the bucket size, minimum of 1 bucket.
|
2013-09-05 16:46:43 +00:00
|
|
|
if (num > 1024)
|
|
|
|
Header.bucket_count = num / 4;
|
|
|
|
if (num > 16)
|
|
|
|
Header.bucket_count = num / 2;
|
|
|
|
else
|
|
|
|
Header.bucket_count = num > 0 ? num : 1;
|
2011-11-07 09:18:42 +00:00
|
|
|
|
|
|
|
Header.hashes_count = num;
|
|
|
|
}
|
|
|
|
|
2012-04-13 20:06:17 +00:00
|
|
|
// compareDIEs - comparison predicate that sorts DIEs by their offset.
|
|
|
|
static bool compareDIEs(const DwarfAccelTable::HashDataContents *A,
|
|
|
|
const DwarfAccelTable::HashDataContents *B) {
|
|
|
|
return A->Die->getOffset() < B->Die->getOffset();
|
2011-11-15 23:37:17 +00:00
|
|
|
}
|
|
|
|
|
2013-05-11 18:24:28 +00:00
|
|
|
void DwarfAccelTable::FinalizeTable(AsmPrinter *Asm, StringRef Prefix) {
|
2011-11-07 09:18:42 +00:00
|
|
|
// Create the individual hash data outputs.
|
2013-09-05 16:46:43 +00:00
|
|
|
for (StringMap<DataArray>::iterator EI = Entries.begin(), EE = Entries.end();
|
|
|
|
EI != EE; ++EI) {
|
2011-11-10 19:25:34 +00:00
|
|
|
|
|
|
|
// Unique the entries.
|
2014-04-25 22:21:35 +00:00
|
|
|
std::stable_sort(EI->second.Values.begin(), EI->second.Values.end(), compareDIEs);
|
|
|
|
EI->second.Values.erase(
|
|
|
|
std::unique(EI->second.Values.begin(), EI->second.Values.end()),
|
|
|
|
EI->second.Values.end());
|
2011-11-10 19:25:34 +00:00
|
|
|
|
2012-04-13 20:06:17 +00:00
|
|
|
HashData *Entry = new (Allocator) HashData(EI->getKey(), EI->second);
|
2011-11-07 09:18:42 +00:00
|
|
|
Data.push_back(Entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out how many buckets we need, then compute the bucket
|
|
|
|
// contents and the final ordering. We'll emit the hashes and offsets
|
|
|
|
// by doing a walk during the emission phase. We add temporary
|
|
|
|
// symbols to the data so that we can reference them during the offset
|
|
|
|
// later, we'll emit them when we emit the data.
|
|
|
|
ComputeBucketCount();
|
|
|
|
|
|
|
|
// Compute bucket contents and final ordering.
|
|
|
|
Buckets.resize(Header.bucket_count);
|
2011-11-08 18:38:40 +00:00
|
|
|
for (size_t i = 0, e = Data.size(); i < e; ++i) {
|
2011-11-07 09:18:42 +00:00
|
|
|
uint32_t bucket = Data[i]->HashValue % Header.bucket_count;
|
|
|
|
Buckets[bucket].push_back(Data[i]);
|
|
|
|
Data[i]->Sym = Asm->GetTempSymbol(Prefix, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emits the header for the table via the AsmPrinter.
|
|
|
|
void DwarfAccelTable::EmitHeader(AsmPrinter *Asm) {
|
|
|
|
Asm->OutStreamer.AddComment("Header Magic");
|
|
|
|
Asm->EmitInt32(Header.magic);
|
|
|
|
Asm->OutStreamer.AddComment("Header Version");
|
|
|
|
Asm->EmitInt16(Header.version);
|
|
|
|
Asm->OutStreamer.AddComment("Header Hash Function");
|
|
|
|
Asm->EmitInt16(Header.hash_function);
|
|
|
|
Asm->OutStreamer.AddComment("Header Bucket Count");
|
|
|
|
Asm->EmitInt32(Header.bucket_count);
|
|
|
|
Asm->OutStreamer.AddComment("Header Hash Count");
|
|
|
|
Asm->EmitInt32(Header.hashes_count);
|
|
|
|
Asm->OutStreamer.AddComment("Header Data Length");
|
|
|
|
Asm->EmitInt32(Header.header_data_len);
|
|
|
|
Asm->OutStreamer.AddComment("HeaderData Die Offset Base");
|
|
|
|
Asm->EmitInt32(HeaderData.die_offset_base);
|
|
|
|
Asm->OutStreamer.AddComment("HeaderData Atom Count");
|
|
|
|
Asm->EmitInt32(HeaderData.Atoms.size());
|
|
|
|
for (size_t i = 0; i < HeaderData.Atoms.size(); i++) {
|
|
|
|
Atom A = HeaderData.Atoms[i];
|
2013-09-05 18:20:16 +00:00
|
|
|
Asm->OutStreamer.AddComment(dwarf::AtomTypeString(A.type));
|
2011-11-07 09:18:42 +00:00
|
|
|
Asm->EmitInt16(A.type);
|
|
|
|
Asm->OutStreamer.AddComment(dwarf::FormEncodingString(A.form));
|
|
|
|
Asm->EmitInt16(A.form);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-08 23:53:45 +00:00
|
|
|
// Walk through and emit the buckets for the table. Each index is
|
|
|
|
// an offset into the list of hashes.
|
2011-11-07 09:18:42 +00:00
|
|
|
void DwarfAccelTable::EmitBuckets(AsmPrinter *Asm) {
|
|
|
|
unsigned index = 0;
|
2011-11-08 18:38:40 +00:00
|
|
|
for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
|
2011-11-07 18:34:47 +00:00
|
|
|
Asm->OutStreamer.AddComment("Bucket " + Twine(i));
|
2011-11-07 09:18:42 +00:00
|
|
|
if (Buckets[i].size() != 0)
|
|
|
|
Asm->EmitInt32(index);
|
|
|
|
else
|
|
|
|
Asm->EmitInt32(UINT32_MAX);
|
|
|
|
index += Buckets[i].size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk through the buckets and emit the individual hashes for each
|
|
|
|
// bucket.
|
|
|
|
void DwarfAccelTable::EmitHashes(AsmPrinter *Asm) {
|
2011-11-08 18:38:40 +00:00
|
|
|
for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
|
2011-11-07 09:18:42 +00:00
|
|
|
for (HashList::const_iterator HI = Buckets[i].begin(),
|
2013-09-05 16:46:43 +00:00
|
|
|
HE = Buckets[i].end();
|
|
|
|
HI != HE; ++HI) {
|
2011-11-07 18:34:47 +00:00
|
|
|
Asm->OutStreamer.AddComment("Hash in Bucket " + Twine(i));
|
2011-11-07 09:18:42 +00:00
|
|
|
Asm->EmitInt32((*HI)->HashValue);
|
2012-12-20 21:58:40 +00:00
|
|
|
}
|
2011-11-07 09:18:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk through the buckets and emit the individual offsets for each
|
|
|
|
// element in each bucket. This is done via a symbol subtraction from the
|
|
|
|
// beginning of the section. The non-section symbol will be output later
|
|
|
|
// when we emit the actual data.
|
|
|
|
void DwarfAccelTable::EmitOffsets(AsmPrinter *Asm, MCSymbol *SecBegin) {
|
2011-11-08 18:38:40 +00:00
|
|
|
for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
|
2011-11-07 09:18:42 +00:00
|
|
|
for (HashList::const_iterator HI = Buckets[i].begin(),
|
2013-09-05 16:46:43 +00:00
|
|
|
HE = Buckets[i].end();
|
|
|
|
HI != HE; ++HI) {
|
2011-11-07 18:34:47 +00:00
|
|
|
Asm->OutStreamer.AddComment("Offset in Bucket " + Twine(i));
|
2011-11-07 09:18:42 +00:00
|
|
|
MCContext &Context = Asm->OutStreamer.getContext();
|
2013-09-05 16:46:43 +00:00
|
|
|
const MCExpr *Sub = MCBinaryExpr::CreateSub(
|
|
|
|
MCSymbolRefExpr::Create((*HI)->Sym, Context),
|
|
|
|
MCSymbolRefExpr::Create(SecBegin, Context), Context);
|
2013-01-09 03:52:05 +00:00
|
|
|
Asm->OutStreamer.EmitValue(Sub, sizeof(uint32_t));
|
2011-11-07 09:18:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk through the buckets and emit the full data for each element in
|
|
|
|
// the bucket. For the string case emit the dies and the various offsets.
|
|
|
|
// Terminate each HashData bucket with 0.
|
2013-12-05 18:06:10 +00:00
|
|
|
void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfFile *D) {
|
2011-11-07 09:18:42 +00:00
|
|
|
uint64_t PrevHash = UINT64_MAX;
|
2011-11-08 18:38:40 +00:00
|
|
|
for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
|
2011-11-07 09:18:42 +00:00
|
|
|
for (HashList::const_iterator HI = Buckets[i].begin(),
|
2013-09-05 16:46:43 +00:00
|
|
|
HE = Buckets[i].end();
|
|
|
|
HI != HE; ++HI) {
|
2011-11-07 09:18:42 +00:00
|
|
|
// Remember to emit the label for our offset.
|
|
|
|
Asm->OutStreamer.EmitLabel((*HI)->Sym);
|
|
|
|
Asm->OutStreamer.AddComment((*HI)->Str);
|
2014-04-25 22:21:35 +00:00
|
|
|
Asm->EmitSectionOffset((*HI)->Data.StrSym,
|
2014-04-25 21:34:35 +00:00
|
|
|
D->getStringPool().getSectionSymbol());
|
2011-11-07 09:18:42 +00:00
|
|
|
Asm->OutStreamer.AddComment("Num DIEs");
|
2014-04-25 22:21:35 +00:00
|
|
|
Asm->EmitInt32((*HI)->Data.Values.size());
|
|
|
|
for (HashDataContents *HD : (*HI)->Data.Values) {
|
2012-01-06 04:35:23 +00:00
|
|
|
// Emit the DIE offset
|
2014-04-25 22:21:35 +00:00
|
|
|
Asm->EmitInt32(HD->Die->getOffset());
|
2012-01-06 04:35:23 +00:00
|
|
|
// If we have multiple Atoms emit that info too.
|
|
|
|
// FIXME: A bit of a hack, we either emit only one atom or all info.
|
|
|
|
if (HeaderData.Atoms.size() > 1) {
|
2014-04-25 22:21:35 +00:00
|
|
|
Asm->EmitInt16(HD->Die->getTag());
|
|
|
|
Asm->EmitInt8(HD->Flags);
|
2012-01-06 04:35:23 +00:00
|
|
|
}
|
2011-11-07 09:18:42 +00:00
|
|
|
}
|
|
|
|
// Emit a 0 to terminate the data unless we have a hash collision.
|
|
|
|
if (PrevHash != (*HI)->HashValue)
|
|
|
|
Asm->EmitInt32(0);
|
|
|
|
PrevHash = (*HI)->HashValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the entire data structure to the output file.
|
2013-12-05 18:06:10 +00:00
|
|
|
void DwarfAccelTable::Emit(AsmPrinter *Asm, MCSymbol *SecBegin, DwarfFile *D) {
|
2011-11-07 09:18:42 +00:00
|
|
|
// Emit the header.
|
|
|
|
EmitHeader(Asm);
|
|
|
|
|
|
|
|
// Emit the buckets.
|
|
|
|
EmitBuckets(Asm);
|
|
|
|
|
|
|
|
// Emit the hashes.
|
|
|
|
EmitHashes(Asm);
|
|
|
|
|
|
|
|
// Emit the offsets.
|
|
|
|
EmitOffsets(Asm, SecBegin);
|
|
|
|
|
|
|
|
// Emit the hash data.
|
|
|
|
EmitData(Asm, D);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
void DwarfAccelTable::print(raw_ostream &O) {
|
|
|
|
|
|
|
|
Header.print(O);
|
|
|
|
HeaderData.print(O);
|
|
|
|
|
|
|
|
O << "Entries: \n";
|
2013-09-05 16:46:43 +00:00
|
|
|
for (StringMap<DataArray>::const_iterator EI = Entries.begin(),
|
|
|
|
EE = Entries.end();
|
|
|
|
EI != EE; ++EI) {
|
2012-01-06 23:03:27 +00:00
|
|
|
O << "Name: " << EI->getKeyData() << "\n";
|
2014-04-25 22:21:35 +00:00
|
|
|
for (HashDataContents *HD : EI->second.Values)
|
|
|
|
HD->print(O);
|
2011-11-07 09:18:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
O << "Buckets and Hashes: \n";
|
2011-11-08 18:38:40 +00:00
|
|
|
for (size_t i = 0, e = Buckets.size(); i < e; ++i)
|
2011-11-07 09:18:42 +00:00
|
|
|
for (HashList::const_iterator HI = Buckets[i].begin(),
|
2013-09-05 16:46:43 +00:00
|
|
|
HE = Buckets[i].end();
|
|
|
|
HI != HE; ++HI)
|
2011-11-07 09:18:42 +00:00
|
|
|
(*HI)->print(O);
|
|
|
|
|
|
|
|
O << "Data: \n";
|
2013-09-05 16:46:43 +00:00
|
|
|
for (std::vector<HashData *>::const_iterator DI = Data.begin(),
|
|
|
|
DE = Data.end();
|
|
|
|
DI != DE; ++DI)
|
|
|
|
(*DI)->print(O);
|
2011-11-07 09:18:42 +00:00
|
|
|
}
|
|
|
|
#endif
|