2011-11-07 17:18:42 +08:00
|
|
|
//==-- llvm/CodeGen/DwarfAccelTable.h - 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef CODEGEN_ASMPRINTER_DWARFACCELTABLE_H__
|
|
|
|
#define CODEGEN_ASMPRINTER_DWARFACCELTABLE_H__
|
|
|
|
|
2012-12-04 15:12:27 +08:00
|
|
|
#include "DIE.h"
|
2012-04-14 04:06:17 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2012-12-04 15:12:27 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2011-11-07 17:18:42 +08:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
|
|
|
#include "llvm/Support/DataTypes.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2012-12-04 15:12:27 +08:00
|
|
|
#include "llvm/Support/Dwarf.h"
|
2011-11-07 17:18:42 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/Format.h"
|
|
|
|
#include "llvm/Support/FormattedStream.h"
|
|
|
|
#include <map>
|
2012-12-04 15:12:27 +08:00
|
|
|
#include <vector>
|
2011-11-07 17:18:42 +08:00
|
|
|
|
2011-11-07 17:24:32 +08:00
|
|
|
// The dwarf accelerator tables are an indirect hash table optimized
|
2011-11-07 17:18:42 +08:00
|
|
|
// for null lookup rather than access to known data. They are output into
|
|
|
|
// an on-disk format that looks like this:
|
|
|
|
//
|
|
|
|
// .-------------.
|
|
|
|
// | HEADER |
|
|
|
|
// |-------------|
|
|
|
|
// | BUCKETS |
|
|
|
|
// |-------------|
|
|
|
|
// | HASHES |
|
|
|
|
// |-------------|
|
|
|
|
// | OFFSETS |
|
|
|
|
// |-------------|
|
|
|
|
// | DATA |
|
|
|
|
// `-------------'
|
|
|
|
//
|
|
|
|
// where the header contains a magic number, version, type of hash function,
|
|
|
|
// the number of buckets, total number of hashes, and room for a special
|
|
|
|
// struct of data and the length of that struct.
|
|
|
|
//
|
|
|
|
// The buckets contain an index (e.g. 6) into the hashes array. The hashes
|
|
|
|
// section contains all of the 32-bit hash values in contiguous memory, and
|
|
|
|
// the offsets contain the offset into the data area for the particular
|
|
|
|
// hash.
|
2012-12-21 05:58:40 +08:00
|
|
|
//
|
2011-11-07 17:18:42 +08:00
|
|
|
// For a lookup example, we could hash a function name and take it modulo the
|
|
|
|
// number of buckets giving us our bucket. From there we take the bucket value
|
|
|
|
// as an index into the hashes table and look at each successive hash as long
|
|
|
|
// as the hash value is still the same modulo result (bucket value) as earlier.
|
|
|
|
// If we have a match we look at that same entry in the offsets table and
|
|
|
|
// grab the offset in the data for our final match.
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
class AsmPrinter;
|
|
|
|
class DIE;
|
2012-12-21 05:58:36 +08:00
|
|
|
class DwarfUnits;
|
2012-12-21 05:58:40 +08:00
|
|
|
|
2011-11-07 17:18:42 +08:00
|
|
|
class DwarfAccelTable {
|
|
|
|
|
|
|
|
enum HashFunctionType {
|
|
|
|
eHashFunctionDJB = 0u
|
|
|
|
};
|
|
|
|
|
2011-11-08 05:49:35 +08:00
|
|
|
static uint32_t HashDJB (StringRef Str) {
|
2011-11-07 17:18:42 +08:00
|
|
|
uint32_t h = 5381;
|
2011-11-08 05:49:35 +08:00
|
|
|
for (unsigned i = 0, e = Str.size(); i != e; ++i)
|
|
|
|
h = ((h << 5) + h) + Str[i];
|
2011-11-07 17:18:42 +08:00
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function to compute the number of buckets needed based on
|
|
|
|
// the number of unique hashes.
|
|
|
|
void ComputeBucketCount (void);
|
2012-12-21 05:58:40 +08:00
|
|
|
|
2011-11-07 17:18:42 +08:00
|
|
|
struct TableHeader {
|
|
|
|
uint32_t magic; // 'HASH' magic value to allow endian detection
|
|
|
|
uint16_t version; // Version number.
|
|
|
|
uint16_t hash_function; // The hash function enumeration that was used.
|
|
|
|
uint32_t bucket_count; // The number of buckets in this hash table.
|
|
|
|
uint32_t hashes_count; // The total number of unique hash values
|
|
|
|
// and hash data offsets in this table.
|
|
|
|
uint32_t header_data_len; // The bytes to skip to get to the hash
|
|
|
|
// indexes (buckets) for correct alignment.
|
|
|
|
// Also written to disk is the implementation specific header data.
|
|
|
|
|
|
|
|
static const uint32_t MagicHash = 0x48415348;
|
2012-12-21 05:58:40 +08:00
|
|
|
|
2011-11-07 17:18:42 +08:00
|
|
|
TableHeader (uint32_t data_len) :
|
|
|
|
magic (MagicHash), version (1), hash_function (eHashFunctionDJB),
|
|
|
|
bucket_count (0), hashes_count (0), header_data_len (data_len)
|
2011-11-09 14:20:49 +08:00
|
|
|
{}
|
2011-11-07 17:18:42 +08:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
void print(raw_ostream &O) {
|
|
|
|
O << "Magic: " << format("0x%x", magic) << "\n"
|
|
|
|
<< "Version: " << version << "\n"
|
|
|
|
<< "Hash Function: " << hash_function << "\n"
|
|
|
|
<< "Bucket Count: " << bucket_count << "\n"
|
|
|
|
<< "Header Data Length: " << header_data_len << "\n";
|
|
|
|
}
|
|
|
|
void dump() { print(dbgs()); }
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
// The HeaderData describes the form of each set of data. In general this
|
|
|
|
// is as a list of atoms (atom_count) where each atom contains a type
|
|
|
|
// (AtomType type) of data, and an encoding form (form). In the case of
|
|
|
|
// data that is referenced via DW_FORM_ref_* the die_offset_base is
|
|
|
|
// used to describe the offset for all forms in the list of atoms.
|
|
|
|
// This also serves as a public interface of sorts.
|
|
|
|
// When written to disk this will have the form:
|
|
|
|
//
|
|
|
|
// uint32_t die_offset_base
|
|
|
|
// uint32_t atom_count
|
2012-12-21 05:58:40 +08:00
|
|
|
// atom_count Atoms
|
2011-11-07 17:18:42 +08:00
|
|
|
enum AtomType {
|
|
|
|
eAtomTypeNULL = 0u,
|
|
|
|
eAtomTypeDIEOffset = 1u, // DIE offset, check form for encoding
|
|
|
|
eAtomTypeCUOffset = 2u, // DIE offset of the compiler unit header that
|
|
|
|
// contains the item in question
|
|
|
|
eAtomTypeTag = 3u, // DW_TAG_xxx value, should be encoded as
|
|
|
|
// DW_FORM_data1 (if no tags exceed 255) or
|
|
|
|
// DW_FORM_data2.
|
|
|
|
eAtomTypeNameFlags = 4u, // Flags from enum NameFlags
|
|
|
|
eAtomTypeTypeFlags = 5u // Flags from enum TypeFlags
|
|
|
|
};
|
|
|
|
|
2012-01-06 12:35:23 +08:00
|
|
|
enum TypeFlags {
|
|
|
|
eTypeFlagClassMask = 0x0000000fu,
|
2012-12-21 05:58:40 +08:00
|
|
|
|
2012-01-06 12:35:23 +08:00
|
|
|
// Always set for C++, only set for ObjC if this is the
|
|
|
|
// @implementation for a class.
|
|
|
|
eTypeFlagClassIsImplementation = ( 1u << 1 )
|
2012-12-21 05:58:40 +08:00
|
|
|
};
|
|
|
|
|
2011-11-07 17:18:42 +08:00
|
|
|
// Make these public so that they can be used as a general interface to
|
|
|
|
// the class.
|
|
|
|
struct Atom {
|
|
|
|
AtomType type; // enum AtomType
|
|
|
|
uint16_t form; // DWARF DW_FORM_ defines
|
|
|
|
|
2011-11-09 14:20:49 +08:00
|
|
|
Atom(AtomType type, uint16_t form) : type(type), form(form) {}
|
2011-11-07 17:18:42 +08:00
|
|
|
static const char * AtomTypeString(enum AtomType);
|
|
|
|
#ifndef NDEBUG
|
|
|
|
void print(raw_ostream &O) {
|
2012-01-06 12:35:23 +08:00
|
|
|
O << "Type: " << AtomTypeString(type) << "\n"
|
2011-11-07 17:18:42 +08:00
|
|
|
<< "Form: " << dwarf::FormEncodingString(form) << "\n";
|
|
|
|
}
|
|
|
|
void dump() {
|
|
|
|
print(dbgs());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct TableHeaderData {
|
|
|
|
uint32_t die_offset_base;
|
2012-04-14 04:06:17 +08:00
|
|
|
SmallVector<Atom, 1> Atoms;
|
|
|
|
|
|
|
|
TableHeaderData(ArrayRef<Atom> AtomList, uint32_t offset = 0)
|
|
|
|
: die_offset_base(offset), Atoms(AtomList.begin(), AtomList.end()) { }
|
2011-11-07 17:18:42 +08:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
void print (raw_ostream &O) {
|
|
|
|
O << "die_offset_base: " << die_offset_base << "\n";
|
|
|
|
for (size_t i = 0; i < Atoms.size(); i++)
|
|
|
|
Atoms[i].print(O);
|
|
|
|
}
|
|
|
|
void dump() {
|
|
|
|
print(dbgs());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2011-11-07 17:24:32 +08:00
|
|
|
// The data itself consists of a str_offset, a count of the DIEs in the
|
2011-11-07 17:18:42 +08:00
|
|
|
// hash and the offsets to the DIEs themselves.
|
|
|
|
// On disk each data section is ended with a 0 KeyType as the end of the
|
|
|
|
// hash chain.
|
|
|
|
// On output this looks like:
|
|
|
|
// uint32_t str_offset
|
|
|
|
// uint32_t hash_data_count
|
|
|
|
// HashData[hash_data_count]
|
2012-01-06 12:35:23 +08:00
|
|
|
public:
|
|
|
|
struct HashDataContents {
|
|
|
|
DIE *Die; // Offsets
|
|
|
|
char Flags; // Specific flags to output
|
|
|
|
|
|
|
|
HashDataContents(DIE *D, char Flags) :
|
|
|
|
Die(D),
|
2012-01-24 06:55:02 +08:00
|
|
|
Flags(Flags) { }
|
2012-01-06 12:35:23 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
void print(raw_ostream &O) const {
|
|
|
|
O << " Offset: " << Die->getOffset() << "\n";
|
|
|
|
O << " Tag: " << dwarf::TagString(Die->getTag()) << "\n";
|
|
|
|
O << " Flags: " << Flags << "\n";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
private:
|
2011-11-07 17:18:42 +08:00
|
|
|
struct HashData {
|
|
|
|
StringRef Str;
|
|
|
|
uint32_t HashValue;
|
|
|
|
MCSymbol *Sym;
|
2012-04-14 04:06:17 +08:00
|
|
|
ArrayRef<HashDataContents*> Data; // offsets
|
|
|
|
HashData(StringRef S, ArrayRef<HashDataContents*> Data)
|
|
|
|
: Str(S), Data(Data) {
|
2011-11-08 05:49:35 +08:00
|
|
|
HashValue = DwarfAccelTable::HashDJB(S);
|
2011-11-07 17:18:42 +08:00
|
|
|
}
|
|
|
|
#ifndef NDEBUG
|
|
|
|
void print(raw_ostream &O) {
|
|
|
|
O << "Name: " << Str << "\n";
|
|
|
|
O << " Hash Value: " << format("0x%x", HashValue) << "\n";
|
|
|
|
O << " Symbol: " ;
|
|
|
|
if (Sym) Sym->print(O);
|
|
|
|
else O << "<none>";
|
|
|
|
O << "\n";
|
2012-01-06 12:35:23 +08:00
|
|
|
for (size_t i = 0; i < Data.size(); i++) {
|
|
|
|
O << " Offset: " << Data[i]->Die->getOffset() << "\n";
|
|
|
|
O << " Tag: " << dwarf::TagString(Data[i]->Die->getTag()) << "\n";
|
|
|
|
O << " Flags: " << Data[i]->Flags << "\n";
|
|
|
|
}
|
2011-11-07 17:18:42 +08:00
|
|
|
}
|
|
|
|
void dump() {
|
|
|
|
print(dbgs());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2012-09-16 01:09:36 +08:00
|
|
|
DwarfAccelTable(const DwarfAccelTable&) LLVM_DELETED_FUNCTION;
|
|
|
|
void operator=(const DwarfAccelTable&) LLVM_DELETED_FUNCTION;
|
2011-11-07 17:18:42 +08:00
|
|
|
|
|
|
|
// Internal Functions
|
|
|
|
void EmitHeader(AsmPrinter *);
|
|
|
|
void EmitBuckets(AsmPrinter *);
|
|
|
|
void EmitHashes(AsmPrinter *);
|
|
|
|
void EmitOffsets(AsmPrinter *, MCSymbol *);
|
2012-12-21 05:58:36 +08:00
|
|
|
void EmitData(AsmPrinter *, DwarfUnits *D);
|
2012-04-14 04:06:17 +08:00
|
|
|
|
|
|
|
// Allocator for HashData and HashDataContents.
|
|
|
|
BumpPtrAllocator Allocator;
|
|
|
|
|
2011-11-07 17:18:42 +08:00
|
|
|
// Output Variables
|
|
|
|
TableHeader Header;
|
|
|
|
TableHeaderData HeaderData;
|
|
|
|
std::vector<HashData*> Data;
|
|
|
|
|
|
|
|
// String Data
|
2012-04-14 04:06:17 +08:00
|
|
|
typedef std::vector<HashDataContents*> DataArray;
|
|
|
|
typedef StringMap<DataArray, BumpPtrAllocator&> StringEntries;
|
2011-11-07 17:18:42 +08:00
|
|
|
StringEntries Entries;
|
|
|
|
|
|
|
|
// Buckets/Hashes/Offsets
|
|
|
|
typedef std::vector<HashData*> HashList;
|
|
|
|
typedef std::vector<HashList> BucketList;
|
|
|
|
BucketList Buckets;
|
|
|
|
HashList Hashes;
|
2012-12-21 05:58:40 +08:00
|
|
|
|
2011-11-07 17:18:42 +08:00
|
|
|
// Public Implementation
|
|
|
|
public:
|
2012-04-14 04:06:17 +08:00
|
|
|
DwarfAccelTable(ArrayRef<DwarfAccelTable::Atom>);
|
2011-11-08 05:49:28 +08:00
|
|
|
~DwarfAccelTable();
|
2012-01-06 12:35:23 +08:00
|
|
|
void AddName(StringRef, DIE*, char = 0);
|
2011-11-07 17:18:42 +08:00
|
|
|
void FinalizeTable(AsmPrinter *, const char *);
|
2012-12-21 05:58:36 +08:00
|
|
|
void Emit(AsmPrinter *, MCSymbol *, DwarfUnits *);
|
2011-11-07 17:18:42 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
void print(raw_ostream &O);
|
|
|
|
void dump() { print(dbgs()); }
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|