2016-07-05 21:23:04 +00:00
|
|
|
//===- SearchableTableEmitter.cpp - Generate efficiently searchable tables -==//
|
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-07-05 21:23:04 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This tablegen backend emits a generic array initialized by specified fields,
|
|
|
|
// together with companion index tables and lookup functions (binary search,
|
|
|
|
// currently).
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-09-03 09:41:09 -04:00
|
|
|
#include "CodeGenIntrinsics.h"
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2018-04-01 17:08:58 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2016-07-05 21:23:04 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/Support/Format.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/TableGen/Error.h"
|
|
|
|
#include "llvm/TableGen/Record.h"
|
|
|
|
#include <algorithm>
|
2018-06-21 13:36:22 +00:00
|
|
|
#include <set>
|
2016-07-05 21:23:04 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2018-06-21 13:36:22 +00:00
|
|
|
|
2016-07-05 21:23:04 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "searchable-table-emitter"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
struct GenericTable;
|
|
|
|
|
|
|
|
int getAsInt(Init *B) {
|
|
|
|
return cast<IntInit>(B->convertInitializerTo(IntRecTy::get()))->getValue();
|
|
|
|
}
|
|
|
|
int getInt(Record *R, StringRef Field) {
|
|
|
|
return getAsInt(R->getValueInit(Field));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct GenericEnum {
|
|
|
|
using Entry = std::pair<StringRef, int64_t>;
|
|
|
|
|
|
|
|
std::string Name;
|
2019-11-09 17:02:51 +00:00
|
|
|
Record *Class = nullptr;
|
2018-06-21 13:36:22 +00:00
|
|
|
std::string PreprocessorGuard;
|
|
|
|
std::vector<std::unique_ptr<Entry>> Entries;
|
|
|
|
DenseMap<Record *, Entry *> EntryMap;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct GenericField {
|
|
|
|
std::string Name;
|
|
|
|
RecTy *RecType = nullptr;
|
2020-11-24 13:09:02 -05:00
|
|
|
bool IsCode = false;
|
2018-06-21 13:36:22 +00:00
|
|
|
bool IsIntrinsic = false;
|
|
|
|
bool IsInstruction = false;
|
|
|
|
GenericEnum *Enum = nullptr;
|
|
|
|
|
2020-01-28 20:23:46 +01:00
|
|
|
GenericField(StringRef Name) : Name(std::string(Name)) {}
|
2018-06-21 13:36:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SearchIndex {
|
|
|
|
std::string Name;
|
2020-09-03 09:41:09 -04:00
|
|
|
SMLoc Loc; // Source location of PrimaryKey or Key field definition.
|
2018-06-21 13:36:22 +00:00
|
|
|
SmallVector<GenericField, 1> Fields;
|
2019-11-09 17:02:51 +00:00
|
|
|
bool EarlyOut = false;
|
2018-06-21 13:36:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct GenericTable {
|
|
|
|
std::string Name;
|
2020-09-03 09:41:09 -04:00
|
|
|
ArrayRef<SMLoc> Locs; // Source locations from the Record instance.
|
2018-06-21 13:36:22 +00:00
|
|
|
std::string PreprocessorGuard;
|
|
|
|
std::string CppTypeName;
|
|
|
|
SmallVector<GenericField, 2> Fields;
|
|
|
|
std::vector<Record *> Entries;
|
|
|
|
|
|
|
|
std::unique_ptr<SearchIndex> PrimaryKey;
|
|
|
|
SmallVector<std::unique_ptr<SearchIndex>, 2> Indices;
|
|
|
|
|
|
|
|
const GenericField *getFieldByName(StringRef Name) const {
|
|
|
|
for (const auto &Field : Fields) {
|
|
|
|
if (Name == Field.Name)
|
|
|
|
return &Field;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-07-05 21:23:04 +00:00
|
|
|
class SearchableTableEmitter {
|
|
|
|
RecordKeeper &Records;
|
2018-04-01 17:08:58 +00:00
|
|
|
DenseMap<Init *, std::unique_ptr<CodeGenIntrinsic>> Intrinsics;
|
2018-06-21 13:36:22 +00:00
|
|
|
std::vector<std::unique_ptr<GenericEnum>> Enums;
|
|
|
|
DenseMap<Record *, GenericEnum *> EnumMap;
|
|
|
|
std::set<std::string> PreprocessorGuards;
|
2016-07-05 21:23:04 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
SearchableTableEmitter(RecordKeeper &R) : Records(R) {}
|
|
|
|
|
|
|
|
void run(raw_ostream &OS);
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef std::pair<Init *, int> SearchTableEntry;
|
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
enum TypeContext {
|
|
|
|
TypeInStaticStruct,
|
|
|
|
TypeInTempStruct,
|
|
|
|
TypeInArgument,
|
|
|
|
};
|
2016-07-05 21:23:04 +00:00
|
|
|
|
2020-09-03 09:41:09 -04:00
|
|
|
std::string primaryRepresentation(SMLoc Loc, const GenericField &Field,
|
|
|
|
Init *I) {
|
2020-11-24 13:09:02 -05:00
|
|
|
if (StringInit *SI = dyn_cast<StringInit>(I)) {
|
|
|
|
if (Field.IsCode || SI->hasCodeFormat())
|
|
|
|
return std::string(SI->getValue());
|
|
|
|
else
|
|
|
|
return SI->getAsString();
|
|
|
|
} else if (BitsInit *BI = dyn_cast<BitsInit>(I))
|
2016-07-05 21:23:04 +00:00
|
|
|
return "0x" + utohexstr(getAsInt(BI));
|
|
|
|
else if (BitInit *BI = dyn_cast<BitInit>(I))
|
|
|
|
return BI->getValue() ? "true" : "false";
|
2018-06-21 13:36:22 +00:00
|
|
|
else if (Field.IsIntrinsic)
|
|
|
|
return "Intrinsic::" + getIntrinsic(I).EnumName;
|
|
|
|
else if (Field.IsInstruction)
|
|
|
|
return I->getAsString();
|
2020-05-29 16:35:57 +02:00
|
|
|
else if (Field.Enum) {
|
|
|
|
auto *Entry = Field.Enum->EntryMap[cast<DefInit>(I)->getDef()];
|
|
|
|
if (!Entry)
|
2020-09-03 09:41:09 -04:00
|
|
|
PrintFatalError(Loc,
|
|
|
|
Twine("Entry for field '") + Field.Name + "' is null");
|
2020-05-29 16:35:57 +02:00
|
|
|
return std::string(Entry->first);
|
|
|
|
}
|
2020-09-03 09:41:09 -04:00
|
|
|
PrintFatalError(Loc, Twine("invalid field type for field '") + Field.Name +
|
|
|
|
"'; expected: bit, bits, string, or code");
|
2016-07-05 21:23:04 +00:00
|
|
|
}
|
|
|
|
|
2018-04-01 17:08:58 +00:00
|
|
|
bool isIntrinsic(Init *I) {
|
|
|
|
if (DefInit *DI = dyn_cast<DefInit>(I))
|
|
|
|
return DI->getDef()->isSubClassOf("Intrinsic");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeGenIntrinsic &getIntrinsic(Init *I) {
|
|
|
|
std::unique_ptr<CodeGenIntrinsic> &Intr = Intrinsics[I];
|
|
|
|
if (!Intr)
|
2020-08-26 10:37:48 +02:00
|
|
|
Intr = std::make_unique<CodeGenIntrinsic>(cast<DefInit>(I)->getDef(),
|
|
|
|
std::vector<Record *>());
|
2018-04-01 17:08:58 +00:00
|
|
|
return *Intr;
|
|
|
|
}
|
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
bool compareBy(Record *LHS, Record *RHS, const SearchIndex &Index);
|
|
|
|
|
2020-09-03 09:41:09 -04:00
|
|
|
std::string searchableFieldType(const GenericTable &Table,
|
|
|
|
const SearchIndex &Index,
|
|
|
|
const GenericField &Field, TypeContext Ctx) {
|
2018-06-21 13:36:22 +00:00
|
|
|
if (isa<StringRecTy>(Field.RecType)) {
|
|
|
|
if (Ctx == TypeInStaticStruct)
|
|
|
|
return "const char *";
|
|
|
|
if (Ctx == TypeInTempStruct)
|
|
|
|
return "std::string";
|
|
|
|
return "StringRef";
|
|
|
|
} else if (BitsRecTy *BI = dyn_cast<BitsRecTy>(Field.RecType)) {
|
2016-07-05 21:23:04 +00:00
|
|
|
unsigned NumBits = BI->getNumBits();
|
|
|
|
if (NumBits <= 8)
|
TableGen: Fix ASAN error
Summary:
As a bonus, this arguably improves the code by making it simpler.
gcc 8 on Ubuntu 18.10 reports the following:
==39667==ERROR: AddressSanitizer: stack-use-after-scope on address 0x7fffffff8ae0 at pc 0x555555dbfc68 bp 0x7fffffff8760 sp 0x7fffffff8750
WRITE of size 8 at 0x7fffffff8ae0 thread T0
#0 0x555555dbfc67 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Alloc_hider::_Alloc_hider(char*, std::allocator<char>&&) /usr/include/c++/8/bits/basic_string.h:149
#1 0x555555dbfc67 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&) /usr/include/c++/8/bits/basic_string.h:542
#2 0x555555dbfc67 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > std::operator+<char, std::char_traits<char>, std::allocator<char> >(char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&) /usr/include/c++/8/bits/basic_string.h:6009
#3 0x555555dbfc67 in searchableFieldType /home/nha/amd/build/san/llvm-src/utils/TableGen/SearchableTableEmitter.cpp:168
(...)
Address 0x7fffffff8ae0 is located in stack of thread T0 at offset 864 in frame
#0 0x555555dbef3f in searchableFieldType /home/nha/amd/build/san/llvm-src/utils/TableGen/SearchableTableEmitter.cpp:148
Reviewers: fhahn, simon_tatham, kparzysz
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D53931
llvm-svn: 345749
2018-10-31 17:46:21 +00:00
|
|
|
return "uint8_t";
|
|
|
|
if (NumBits <= 16)
|
|
|
|
return "uint16_t";
|
|
|
|
if (NumBits <= 32)
|
|
|
|
return "uint32_t";
|
|
|
|
if (NumBits <= 64)
|
|
|
|
return "uint64_t";
|
2020-09-03 09:41:09 -04:00
|
|
|
PrintFatalError(Index.Loc, Twine("In table '") + Table.Name +
|
|
|
|
"' lookup method '" + Index.Name +
|
|
|
|
"', key field '" + Field.Name +
|
|
|
|
"' of type bits is too large");
|
2018-06-21 13:36:22 +00:00
|
|
|
} else if (Field.Enum || Field.IsIntrinsic || Field.IsInstruction)
|
2018-04-01 17:08:58 +00:00
|
|
|
return "unsigned";
|
2020-09-03 09:41:09 -04:00
|
|
|
PrintFatalError(Index.Loc,
|
|
|
|
Twine("In table '") + Table.Name + "' lookup method '" +
|
|
|
|
Index.Name + "', key field '" + Field.Name +
|
|
|
|
"' has invalid type: " + Field.RecType->getAsString());
|
2016-07-05 21:23:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
void emitGenericTable(const GenericTable &Table, raw_ostream &OS);
|
|
|
|
void emitGenericEnum(const GenericEnum &Enum, raw_ostream &OS);
|
|
|
|
void emitLookupDeclaration(const GenericTable &Table,
|
|
|
|
const SearchIndex &Index, raw_ostream &OS);
|
|
|
|
void emitLookupFunction(const GenericTable &Table, const SearchIndex &Index,
|
|
|
|
bool IsPrimary, raw_ostream &OS);
|
|
|
|
void emitIfdef(StringRef Guard, raw_ostream &OS);
|
|
|
|
|
|
|
|
bool parseFieldType(GenericField &Field, Init *II);
|
|
|
|
std::unique_ptr<SearchIndex>
|
2020-09-03 09:41:09 -04:00
|
|
|
parseSearchIndex(GenericTable &Table, const RecordVal *RecVal, StringRef Name,
|
2018-06-21 13:36:22 +00:00
|
|
|
const std::vector<StringRef> &Key, bool EarlyOut);
|
|
|
|
void collectEnumEntries(GenericEnum &Enum, StringRef NameField,
|
|
|
|
StringRef ValueField,
|
|
|
|
const std::vector<Record *> &Items);
|
|
|
|
void collectTableEntries(GenericTable &Table,
|
|
|
|
const std::vector<Record *> &Items);
|
2016-07-05 21:23:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // End anonymous namespace.
|
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
// For search indices that consists of a single field whose numeric value is
|
|
|
|
// known, return that numeric value.
|
|
|
|
static int64_t getNumericKey(const SearchIndex &Index, Record *Rec) {
|
|
|
|
assert(Index.Fields.size() == 1);
|
|
|
|
|
|
|
|
if (Index.Fields[0].Enum) {
|
|
|
|
Record *EnumEntry = Rec->getValueAsDef(Index.Fields[0].Name);
|
|
|
|
return Index.Fields[0].Enum->EntryMap[EnumEntry]->second;
|
2016-07-05 21:23:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
return getInt(Rec, Index.Fields[0].Name);
|
|
|
|
}
|
2016-07-05 21:23:04 +00:00
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
/// Less-than style comparison between \p LHS and \p RHS according to the
|
|
|
|
/// key of \p Index.
|
|
|
|
bool SearchableTableEmitter::compareBy(Record *LHS, Record *RHS,
|
|
|
|
const SearchIndex &Index) {
|
|
|
|
for (const auto &Field : Index.Fields) {
|
|
|
|
Init *LHSI = LHS->getValueInit(Field.Name);
|
|
|
|
Init *RHSI = RHS->getValueInit(Field.Name);
|
|
|
|
|
|
|
|
if (isa<BitsRecTy>(Field.RecType) || isa<IntRecTy>(Field.RecType)) {
|
|
|
|
int64_t LHSi = getAsInt(LHSI);
|
|
|
|
int64_t RHSi = getAsInt(RHSI);
|
|
|
|
if (LHSi < RHSi)
|
|
|
|
return true;
|
|
|
|
if (LHSi > RHSi)
|
|
|
|
return false;
|
|
|
|
} else if (Field.IsIntrinsic) {
|
|
|
|
CodeGenIntrinsic &LHSi = getIntrinsic(LHSI);
|
|
|
|
CodeGenIntrinsic &RHSi = getIntrinsic(RHSI);
|
|
|
|
if (std::tie(LHSi.TargetPrefix, LHSi.Name) <
|
|
|
|
std::tie(RHSi.TargetPrefix, RHSi.Name))
|
|
|
|
return true;
|
|
|
|
if (std::tie(LHSi.TargetPrefix, LHSi.Name) >
|
|
|
|
std::tie(RHSi.TargetPrefix, RHSi.Name))
|
|
|
|
return false;
|
|
|
|
} else if (Field.IsInstruction) {
|
|
|
|
// This does not correctly compare the predefined instructions!
|
|
|
|
Record *LHSr = cast<DefInit>(LHSI)->getDef();
|
|
|
|
Record *RHSr = cast<DefInit>(RHSI)->getDef();
|
|
|
|
|
|
|
|
bool LHSpseudo = LHSr->getValueAsBit("isPseudo");
|
|
|
|
bool RHSpseudo = RHSr->getValueAsBit("isPseudo");
|
|
|
|
if (LHSpseudo && !RHSpseudo)
|
|
|
|
return true;
|
|
|
|
if (!LHSpseudo && RHSpseudo)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int comp = LHSr->getName().compare(RHSr->getName());
|
|
|
|
if (comp < 0)
|
|
|
|
return true;
|
|
|
|
if (comp > 0)
|
|
|
|
return false;
|
|
|
|
} else if (Field.Enum) {
|
|
|
|
auto LHSr = cast<DefInit>(LHSI)->getDef();
|
|
|
|
auto RHSr = cast<DefInit>(RHSI)->getDef();
|
|
|
|
int64_t LHSv = Field.Enum->EntryMap[LHSr]->second;
|
|
|
|
int64_t RHSv = Field.Enum->EntryMap[RHSr]->second;
|
|
|
|
if (LHSv < RHSv)
|
|
|
|
return true;
|
|
|
|
if (LHSv > RHSv)
|
|
|
|
return false;
|
|
|
|
} else {
|
2020-09-03 09:41:09 -04:00
|
|
|
std::string LHSs = primaryRepresentation(Index.Loc, Field, LHSI);
|
|
|
|
std::string RHSs = primaryRepresentation(Index.Loc, Field, RHSI);
|
2018-06-21 13:36:22 +00:00
|
|
|
|
|
|
|
if (isa<StringRecTy>(Field.RecType)) {
|
|
|
|
LHSs = StringRef(LHSs).upper();
|
|
|
|
RHSs = StringRef(RHSs).upper();
|
|
|
|
}
|
|
|
|
|
|
|
|
int comp = LHSs.compare(RHSs);
|
|
|
|
if (comp < 0)
|
|
|
|
return true;
|
|
|
|
if (comp > 0)
|
|
|
|
return false;
|
2016-07-05 21:23:04 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-21 13:36:22 +00:00
|
|
|
return false;
|
2016-07-05 21:23:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
void SearchableTableEmitter::emitIfdef(StringRef Guard, raw_ostream &OS) {
|
|
|
|
OS << "#ifdef " << Guard << "\n";
|
2020-01-28 20:23:46 +01:00
|
|
|
PreprocessorGuards.insert(std::string(Guard));
|
2018-06-21 13:36:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit a generic enum.
|
|
|
|
void SearchableTableEmitter::emitGenericEnum(const GenericEnum &Enum,
|
|
|
|
raw_ostream &OS) {
|
|
|
|
emitIfdef((Twine("GET_") + Enum.PreprocessorGuard + "_DECL").str(), OS);
|
|
|
|
|
|
|
|
OS << "enum " << Enum.Name << " {\n";
|
|
|
|
for (const auto &Entry : Enum.Entries)
|
|
|
|
OS << " " << Entry->first << " = " << Entry->second << ",\n";
|
|
|
|
OS << "};\n";
|
|
|
|
|
|
|
|
OS << "#endif\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchableTableEmitter::emitLookupFunction(const GenericTable &Table,
|
|
|
|
const SearchIndex &Index,
|
|
|
|
bool IsPrimary,
|
|
|
|
raw_ostream &OS) {
|
|
|
|
OS << "\n";
|
|
|
|
emitLookupDeclaration(Table, Index, OS);
|
|
|
|
OS << " {\n";
|
|
|
|
|
|
|
|
std::vector<Record *> IndexRowsStorage;
|
|
|
|
ArrayRef<Record *> IndexRows;
|
|
|
|
StringRef IndexTypeName;
|
|
|
|
StringRef IndexName;
|
|
|
|
|
|
|
|
if (IsPrimary) {
|
|
|
|
IndexTypeName = Table.CppTypeName;
|
|
|
|
IndexName = Table.Name;
|
|
|
|
IndexRows = Table.Entries;
|
2016-07-05 21:23:04 +00:00
|
|
|
} else {
|
2018-06-21 13:36:22 +00:00
|
|
|
OS << " struct IndexType {\n";
|
|
|
|
for (const auto &Field : Index.Fields) {
|
2020-09-03 09:41:09 -04:00
|
|
|
OS << " "
|
|
|
|
<< searchableFieldType(Table, Index, Field, TypeInStaticStruct) << " "
|
2018-06-21 13:36:22 +00:00
|
|
|
<< Field.Name << ";\n";
|
|
|
|
}
|
|
|
|
OS << " unsigned _index;\n";
|
|
|
|
OS << " };\n";
|
|
|
|
|
|
|
|
OS << " static const struct IndexType Index[] = {\n";
|
|
|
|
|
|
|
|
std::vector<std::pair<Record *, unsigned>> Entries;
|
|
|
|
Entries.reserve(Table.Entries.size());
|
|
|
|
for (unsigned i = 0; i < Table.Entries.size(); ++i)
|
|
|
|
Entries.emplace_back(Table.Entries[i], i);
|
|
|
|
|
2021-01-13 19:14:42 -08:00
|
|
|
llvm::stable_sort(Entries, [&](const std::pair<Record *, unsigned> &LHS,
|
|
|
|
const std::pair<Record *, unsigned> &RHS) {
|
|
|
|
return compareBy(LHS.first, RHS.first, Index);
|
|
|
|
});
|
2018-06-21 13:36:22 +00:00
|
|
|
|
|
|
|
IndexRowsStorage.reserve(Entries.size());
|
|
|
|
for (const auto &Entry : Entries) {
|
|
|
|
IndexRowsStorage.push_back(Entry.first);
|
|
|
|
|
|
|
|
OS << " { ";
|
2021-02-08 22:33:51 -08:00
|
|
|
ListSeparator LS;
|
2018-06-21 13:36:22 +00:00
|
|
|
for (const auto &Field : Index.Fields) {
|
2020-09-03 09:41:09 -04:00
|
|
|
std::string Repr = primaryRepresentation(
|
|
|
|
Index.Loc, Field, Entry.first->getValueInit(Field.Name));
|
2018-06-21 13:36:22 +00:00
|
|
|
if (isa<StringRecTy>(Field.RecType))
|
|
|
|
Repr = StringRef(Repr).upper();
|
2021-02-08 22:33:51 -08:00
|
|
|
OS << LS << Repr;
|
2018-06-21 13:36:22 +00:00
|
|
|
}
|
|
|
|
OS << ", " << Entry.second << " },\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << " };\n\n";
|
|
|
|
|
|
|
|
IndexTypeName = "IndexType";
|
|
|
|
IndexName = "Index";
|
|
|
|
IndexRows = IndexRowsStorage;
|
2016-07-05 21:23:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
bool IsContiguous = false;
|
|
|
|
|
|
|
|
if (Index.Fields.size() == 1 &&
|
|
|
|
(Index.Fields[0].Enum || isa<BitsRecTy>(Index.Fields[0].RecType))) {
|
|
|
|
IsContiguous = true;
|
|
|
|
for (unsigned i = 0; i < IndexRows.size(); ++i) {
|
|
|
|
if (getNumericKey(Index, IndexRows[i]) != i) {
|
|
|
|
IsContiguous = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-07-05 21:23:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
if (IsContiguous) {
|
|
|
|
OS << " auto Table = makeArrayRef(" << IndexName << ");\n";
|
|
|
|
OS << " size_t Idx = " << Index.Fields[0].Name << ";\n";
|
|
|
|
OS << " return Idx >= Table.size() ? nullptr : ";
|
|
|
|
if (IsPrimary)
|
|
|
|
OS << "&Table[Idx]";
|
|
|
|
else
|
|
|
|
OS << "&" << Table.Name << "[Table[Idx]._index]";
|
|
|
|
OS << ";\n";
|
|
|
|
OS << "}\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Index.EarlyOut) {
|
|
|
|
const GenericField &Field = Index.Fields[0];
|
2020-09-03 09:41:09 -04:00
|
|
|
std::string FirstRepr = primaryRepresentation(
|
|
|
|
Index.Loc, Field, IndexRows[0]->getValueInit(Field.Name));
|
2018-06-21 13:36:22 +00:00
|
|
|
std::string LastRepr = primaryRepresentation(
|
2020-09-03 09:41:09 -04:00
|
|
|
Index.Loc, Field, IndexRows.back()->getValueInit(Field.Name));
|
2018-06-21 13:36:22 +00:00
|
|
|
OS << " if ((" << Field.Name << " < " << FirstRepr << ") ||\n";
|
|
|
|
OS << " (" << Field.Name << " > " << LastRepr << "))\n";
|
|
|
|
OS << " return nullptr;\n\n";
|
2016-07-05 21:23:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
OS << " struct KeyType {\n";
|
|
|
|
for (const auto &Field : Index.Fields) {
|
2020-09-03 09:41:09 -04:00
|
|
|
OS << " " << searchableFieldType(Table, Index, Field, TypeInTempStruct)
|
|
|
|
<< " " << Field.Name << ";\n";
|
2018-06-21 13:36:22 +00:00
|
|
|
}
|
|
|
|
OS << " };\n";
|
2020-11-06 14:19:59 +00:00
|
|
|
OS << " KeyType Key = {";
|
2021-01-31 10:23:41 -08:00
|
|
|
ListSeparator LS;
|
2018-06-21 13:36:22 +00:00
|
|
|
for (const auto &Field : Index.Fields) {
|
2021-01-31 10:23:41 -08:00
|
|
|
OS << LS << Field.Name;
|
2018-06-21 13:36:22 +00:00
|
|
|
if (isa<StringRecTy>(Field.RecType)) {
|
|
|
|
OS << ".upper()";
|
|
|
|
if (IsPrimary)
|
2020-09-03 09:41:09 -04:00
|
|
|
PrintFatalError(Index.Loc,
|
|
|
|
Twine("In table '") + Table.Name +
|
|
|
|
"', use a secondary lookup method for "
|
|
|
|
"case-insensitive comparison of field '" +
|
|
|
|
Field.Name + "'");
|
2018-06-21 13:36:22 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-06 14:19:59 +00:00
|
|
|
OS << "};\n";
|
2018-06-21 13:36:22 +00:00
|
|
|
|
|
|
|
OS << " auto Table = makeArrayRef(" << IndexName << ");\n";
|
|
|
|
OS << " auto Idx = std::lower_bound(Table.begin(), Table.end(), Key,\n";
|
|
|
|
OS << " [](const " << IndexTypeName << " &LHS, const KeyType &RHS) {\n";
|
|
|
|
|
|
|
|
for (const auto &Field : Index.Fields) {
|
|
|
|
if (isa<StringRecTy>(Field.RecType)) {
|
|
|
|
OS << " int Cmp" << Field.Name << " = StringRef(LHS." << Field.Name
|
|
|
|
<< ").compare(RHS." << Field.Name << ");\n";
|
|
|
|
OS << " if (Cmp" << Field.Name << " < 0) return true;\n";
|
|
|
|
OS << " if (Cmp" << Field.Name << " > 0) return false;\n";
|
2018-08-23 08:02:02 +00:00
|
|
|
} else if (Field.Enum) {
|
|
|
|
// Explicitly cast to unsigned, because the signedness of enums is
|
|
|
|
// compiler-dependent.
|
|
|
|
OS << " if ((unsigned)LHS." << Field.Name << " < (unsigned)RHS."
|
|
|
|
<< Field.Name << ")\n";
|
|
|
|
OS << " return true;\n";
|
|
|
|
OS << " if ((unsigned)LHS." << Field.Name << " > (unsigned)RHS."
|
|
|
|
<< Field.Name << ")\n";
|
|
|
|
OS << " return false;\n";
|
2018-06-21 13:36:22 +00:00
|
|
|
} else {
|
|
|
|
OS << " if (LHS." << Field.Name << " < RHS." << Field.Name << ")\n";
|
|
|
|
OS << " return true;\n";
|
|
|
|
OS << " if (LHS." << Field.Name << " > RHS." << Field.Name << ")\n";
|
|
|
|
OS << " return false;\n";
|
|
|
|
}
|
2016-07-05 21:23:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
OS << " return false;\n";
|
|
|
|
OS << " });\n\n";
|
|
|
|
|
|
|
|
OS << " if (Idx == Table.end()";
|
2016-07-05 21:23:04 +00:00
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
for (const auto &Field : Index.Fields)
|
|
|
|
OS << " ||\n Key." << Field.Name << " != Idx->" << Field.Name;
|
|
|
|
OS << ")\n return nullptr;\n";
|
|
|
|
|
|
|
|
if (IsPrimary)
|
|
|
|
OS << " return &*Idx;\n";
|
|
|
|
else
|
|
|
|
OS << " return &" << Table.Name << "[Idx->_index];\n";
|
|
|
|
|
|
|
|
OS << "}\n";
|
2016-07-05 21:23:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
void SearchableTableEmitter::emitLookupDeclaration(const GenericTable &Table,
|
|
|
|
const SearchIndex &Index,
|
2016-07-05 21:23:04 +00:00
|
|
|
raw_ostream &OS) {
|
2018-06-21 13:36:22 +00:00
|
|
|
OS << "const " << Table.CppTypeName << " *" << Index.Name << "(";
|
|
|
|
|
2021-01-31 10:23:41 -08:00
|
|
|
ListSeparator LS;
|
|
|
|
for (const auto &Field : Index.Fields)
|
|
|
|
OS << LS << searchableFieldType(Table, Index, Field, TypeInArgument) << " "
|
2020-09-03 09:41:09 -04:00
|
|
|
<< Field.Name;
|
2018-06-21 13:36:22 +00:00
|
|
|
OS << ")";
|
2016-07-05 21:23:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
void SearchableTableEmitter::emitGenericTable(const GenericTable &Table,
|
|
|
|
raw_ostream &OS) {
|
|
|
|
emitIfdef((Twine("GET_") + Table.PreprocessorGuard + "_DECL").str(), OS);
|
2016-07-05 21:23:04 +00:00
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
// Emit the declarations for the functions that will perform lookup.
|
|
|
|
if (Table.PrimaryKey) {
|
|
|
|
emitLookupDeclaration(Table, *Table.PrimaryKey, OS);
|
|
|
|
OS << ";\n";
|
|
|
|
}
|
|
|
|
for (const auto &Index : Table.Indices) {
|
|
|
|
emitLookupDeclaration(Table, *Index, OS);
|
|
|
|
OS << ";\n";
|
|
|
|
}
|
2016-07-05 21:23:04 +00:00
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
OS << "#endif\n\n";
|
2016-07-05 21:23:04 +00:00
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
emitIfdef((Twine("GET_") + Table.PreprocessorGuard + "_IMPL").str(), OS);
|
2016-07-05 21:23:04 +00:00
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
// The primary data table contains all the fields defined for this map.
|
2019-08-24 15:02:44 +00:00
|
|
|
OS << "constexpr " << Table.CppTypeName << " " << Table.Name << "[] = {\n";
|
2018-06-21 13:36:22 +00:00
|
|
|
for (unsigned i = 0; i < Table.Entries.size(); ++i) {
|
|
|
|
Record *Entry = Table.Entries[i];
|
|
|
|
OS << " { ";
|
2016-07-05 21:23:04 +00:00
|
|
|
|
2021-02-08 22:33:51 -08:00
|
|
|
ListSeparator LS;
|
|
|
|
for (const auto &Field : Table.Fields)
|
|
|
|
OS << LS
|
|
|
|
<< primaryRepresentation(Table.Locs[0], Field,
|
2020-09-03 09:41:09 -04:00
|
|
|
Entry->getValueInit(Field.Name));
|
2018-06-21 13:36:22 +00:00
|
|
|
|
|
|
|
OS << " }, // " << i << "\n";
|
2016-07-05 21:23:04 +00:00
|
|
|
}
|
2018-06-21 13:36:22 +00:00
|
|
|
OS << " };\n";
|
|
|
|
|
|
|
|
// Indexes are sorted "{ Thing, PrimaryIdx }" arrays, so that a binary
|
|
|
|
// search can be performed by "Thing".
|
|
|
|
if (Table.PrimaryKey)
|
|
|
|
emitLookupFunction(Table, *Table.PrimaryKey, true, OS);
|
|
|
|
for (const auto &Index : Table.Indices)
|
|
|
|
emitLookupFunction(Table, *Index, false, OS);
|
|
|
|
|
|
|
|
OS << "#endif\n\n";
|
|
|
|
}
|
2016-07-05 21:23:04 +00:00
|
|
|
|
2020-11-24 13:09:02 -05:00
|
|
|
bool SearchableTableEmitter::parseFieldType(GenericField &Field, Init *TypeOf) {
|
|
|
|
if (auto Type = dyn_cast<StringInit>(TypeOf)) {
|
|
|
|
if (Type->getValue() == "code") {
|
|
|
|
Field.IsCode = true;
|
2018-06-21 13:36:22 +00:00
|
|
|
return true;
|
2020-11-24 13:09:02 -05:00
|
|
|
} else {
|
|
|
|
if (Record *TypeRec = Records.getDef(Type->getValue())) {
|
|
|
|
if (TypeRec->isSubClassOf("GenericEnum")) {
|
|
|
|
Field.Enum = EnumMap[TypeRec];
|
|
|
|
Field.RecType = RecordRecTy::get(Field.Enum->Class);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2016-07-05 21:23:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-07-05 21:23:04 +00:00
|
|
|
|
2020-09-03 09:41:09 -04:00
|
|
|
std::unique_ptr<SearchIndex> SearchableTableEmitter::parseSearchIndex(
|
|
|
|
GenericTable &Table, const RecordVal *KeyRecVal, StringRef Name,
|
|
|
|
const std::vector<StringRef> &Key, bool EarlyOut) {
|
2019-08-15 15:54:37 +00:00
|
|
|
auto Index = std::make_unique<SearchIndex>();
|
2020-01-28 20:23:46 +01:00
|
|
|
Index->Name = std::string(Name);
|
2020-09-03 09:41:09 -04:00
|
|
|
Index->Loc = KeyRecVal->getLoc();
|
2018-06-21 13:36:22 +00:00
|
|
|
Index->EarlyOut = EarlyOut;
|
|
|
|
|
|
|
|
for (const auto &FieldName : Key) {
|
|
|
|
const GenericField *Field = Table.getFieldByName(FieldName);
|
|
|
|
if (!Field)
|
2020-09-03 09:41:09 -04:00
|
|
|
PrintFatalError(
|
|
|
|
KeyRecVal,
|
|
|
|
Twine("In table '") + Table.Name +
|
|
|
|
"', 'PrimaryKey' or 'Key' refers to nonexistent field '" +
|
|
|
|
FieldName + "'");
|
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
Index->Fields.push_back(*Field);
|
2016-07-05 21:23:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
if (EarlyOut && isa<StringRecTy>(Index->Fields[0].RecType)) {
|
|
|
|
PrintFatalError(
|
2020-09-03 09:41:09 -04:00
|
|
|
KeyRecVal, Twine("In lookup method '") + Name + "', early-out is not " +
|
|
|
|
"supported for a first key field of type string");
|
2018-06-21 13:36:22 +00:00
|
|
|
}
|
2016-07-05 21:23:04 +00:00
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
return Index;
|
|
|
|
}
|
2016-07-05 21:23:04 +00:00
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
void SearchableTableEmitter::collectEnumEntries(
|
|
|
|
GenericEnum &Enum, StringRef NameField, StringRef ValueField,
|
|
|
|
const std::vector<Record *> &Items) {
|
|
|
|
for (auto EntryRec : Items) {
|
|
|
|
StringRef Name;
|
|
|
|
if (NameField.empty())
|
|
|
|
Name = EntryRec->getName();
|
|
|
|
else
|
|
|
|
Name = EntryRec->getValueAsString(NameField);
|
|
|
|
|
|
|
|
int64_t Value = 0;
|
|
|
|
if (!ValueField.empty())
|
|
|
|
Value = getInt(EntryRec, ValueField);
|
|
|
|
|
2019-08-15 15:54:37 +00:00
|
|
|
Enum.Entries.push_back(std::make_unique<GenericEnum::Entry>(Name, Value));
|
2018-06-21 13:36:22 +00:00
|
|
|
Enum.EntryMap.insert(std::make_pair(EntryRec, Enum.Entries.back().get()));
|
|
|
|
}
|
2016-07-05 21:23:04 +00:00
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
if (ValueField.empty()) {
|
2021-01-13 19:14:42 -08:00
|
|
|
llvm::stable_sort(Enum.Entries,
|
|
|
|
[](const std::unique_ptr<GenericEnum::Entry> &LHS,
|
|
|
|
const std::unique_ptr<GenericEnum::Entry> &RHS) {
|
|
|
|
return LHS->first < RHS->first;
|
|
|
|
});
|
2016-07-05 21:23:04 +00:00
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
for (size_t i = 0; i < Enum.Entries.size(); ++i)
|
|
|
|
Enum.Entries[i]->second = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchableTableEmitter::collectTableEntries(
|
|
|
|
GenericTable &Table, const std::vector<Record *> &Items) {
|
2020-05-29 16:35:57 +02:00
|
|
|
if (Items.empty())
|
2020-09-03 09:41:09 -04:00
|
|
|
PrintFatalError(Table.Locs,
|
|
|
|
Twine("Table '") + Table.Name + "' has no entries");
|
2020-05-29 16:35:57 +02:00
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
for (auto EntryRec : Items) {
|
|
|
|
for (auto &Field : Table.Fields) {
|
|
|
|
auto TI = dyn_cast<TypedInit>(EntryRec->getValueInit(Field.Name));
|
2020-02-07 11:13:51 +00:00
|
|
|
if (!TI || !TI->isComplete()) {
|
2020-09-03 09:41:09 -04:00
|
|
|
PrintFatalError(EntryRec, Twine("Record '") + EntryRec->getName() +
|
|
|
|
"' for table '" + Table.Name +
|
|
|
|
"' is missing field '" + Field.Name +
|
|
|
|
"'");
|
2018-06-21 13:36:22 +00:00
|
|
|
}
|
|
|
|
if (!Field.RecType) {
|
|
|
|
Field.RecType = TI->getType();
|
|
|
|
} else {
|
|
|
|
RecTy *Ty = resolveTypes(Field.RecType, TI->getType());
|
|
|
|
if (!Ty)
|
2020-09-03 09:41:09 -04:00
|
|
|
PrintFatalError(EntryRec->getValue(Field.Name),
|
|
|
|
Twine("Field '") + Field.Name + "' of table '" +
|
|
|
|
Table.Name + "' entry has incompatible type: " +
|
|
|
|
TI->getType()->getAsString() + " vs. " +
|
|
|
|
Field.RecType->getAsString());
|
2018-06-21 13:36:22 +00:00
|
|
|
Field.RecType = Ty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 09:41:09 -04:00
|
|
|
Table.Entries.push_back(EntryRec); // Add record to table's record list.
|
2016-07-05 21:23:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
Record *IntrinsicClass = Records.getClass("Intrinsic");
|
|
|
|
Record *InstructionClass = Records.getClass("Instruction");
|
|
|
|
for (auto &Field : Table.Fields) {
|
2020-05-29 16:35:57 +02:00
|
|
|
if (!Field.RecType)
|
|
|
|
PrintFatalError(Twine("Cannot determine type of field '") + Field.Name +
|
|
|
|
"' in table '" + Table.Name + "'. Maybe it is not used?");
|
|
|
|
|
2018-06-21 13:36:22 +00:00
|
|
|
if (auto RecordTy = dyn_cast<RecordRecTy>(Field.RecType)) {
|
|
|
|
if (IntrinsicClass && RecordTy->isSubClassOf(IntrinsicClass))
|
|
|
|
Field.IsIntrinsic = true;
|
|
|
|
else if (InstructionClass && RecordTy->isSubClassOf(InstructionClass))
|
|
|
|
Field.IsInstruction = true;
|
|
|
|
}
|
|
|
|
}
|
2016-07-05 21:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchableTableEmitter::run(raw_ostream &OS) {
|
2018-06-21 13:36:22 +00:00
|
|
|
// Emit tables in a deterministic order to avoid needless rebuilds.
|
|
|
|
SmallVector<std::unique_ptr<GenericTable>, 4> Tables;
|
|
|
|
DenseMap<Record *, GenericTable *> TableMap;
|
|
|
|
|
|
|
|
// Collect all definitions first.
|
|
|
|
for (auto EnumRec : Records.getAllDerivedDefinitions("GenericEnum")) {
|
|
|
|
StringRef NameField;
|
|
|
|
if (!EnumRec->isValueUnset("NameField"))
|
|
|
|
NameField = EnumRec->getValueAsString("NameField");
|
|
|
|
|
|
|
|
StringRef ValueField;
|
|
|
|
if (!EnumRec->isValueUnset("ValueField"))
|
|
|
|
ValueField = EnumRec->getValueAsString("ValueField");
|
|
|
|
|
2019-08-15 15:54:37 +00:00
|
|
|
auto Enum = std::make_unique<GenericEnum>();
|
2020-01-28 20:23:46 +01:00
|
|
|
Enum->Name = std::string(EnumRec->getName());
|
|
|
|
Enum->PreprocessorGuard = std::string(EnumRec->getName());
|
2018-06-21 13:36:22 +00:00
|
|
|
|
|
|
|
StringRef FilterClass = EnumRec->getValueAsString("FilterClass");
|
|
|
|
Enum->Class = Records.getClass(FilterClass);
|
|
|
|
if (!Enum->Class)
|
2020-09-03 09:41:09 -04:00
|
|
|
PrintFatalError(EnumRec->getValue("FilterClass"),
|
|
|
|
Twine("Enum FilterClass '") + FilterClass +
|
|
|
|
"' does not exist");
|
2018-06-21 13:36:22 +00:00
|
|
|
|
|
|
|
collectEnumEntries(*Enum, NameField, ValueField,
|
|
|
|
Records.getAllDerivedDefinitions(FilterClass));
|
|
|
|
EnumMap.insert(std::make_pair(EnumRec, Enum.get()));
|
|
|
|
Enums.emplace_back(std::move(Enum));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto TableRec : Records.getAllDerivedDefinitions("GenericTable")) {
|
2019-08-15 15:54:37 +00:00
|
|
|
auto Table = std::make_unique<GenericTable>();
|
2020-01-28 20:23:46 +01:00
|
|
|
Table->Name = std::string(TableRec->getName());
|
2020-09-03 09:41:09 -04:00
|
|
|
Table->Locs = TableRec->getLoc();
|
2020-01-28 20:23:46 +01:00
|
|
|
Table->PreprocessorGuard = std::string(TableRec->getName());
|
|
|
|
Table->CppTypeName = std::string(TableRec->getValueAsString("CppTypeName"));
|
2018-06-21 13:36:22 +00:00
|
|
|
|
|
|
|
std::vector<StringRef> Fields = TableRec->getValueAsListOfStrings("Fields");
|
|
|
|
for (const auto &FieldName : Fields) {
|
2020-09-03 09:41:09 -04:00
|
|
|
Table->Fields.emplace_back(FieldName); // Construct a GenericField.
|
2018-06-21 13:36:22 +00:00
|
|
|
|
2020-11-24 13:09:02 -05:00
|
|
|
if (auto TypeOfRecordVal = TableRec->getValue(("TypeOf_" + FieldName).str())) {
|
|
|
|
if (!parseFieldType(Table->Fields.back(), TypeOfRecordVal->getValue())) {
|
|
|
|
PrintError(TypeOfRecordVal,
|
|
|
|
Twine("Table '") + Table->Name +
|
|
|
|
"' has invalid 'TypeOf_" + FieldName +
|
|
|
|
"': " + TypeOfRecordVal->getValue()->getAsString());
|
|
|
|
PrintFatalNote("The 'TypeOf_xxx' field must be a string naming a "
|
|
|
|
"GenericEnum record, or \"code\"");
|
2018-06-21 13:36:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 09:41:09 -04:00
|
|
|
StringRef FilterClass = TableRec->getValueAsString("FilterClass");
|
|
|
|
if (!Records.getClass(FilterClass))
|
|
|
|
PrintFatalError(TableRec->getValue("FilterClass"),
|
|
|
|
Twine("Table FilterClass '") +
|
|
|
|
FilterClass + "' does not exist");
|
|
|
|
|
|
|
|
collectTableEntries(*Table, Records.getAllDerivedDefinitions(FilterClass));
|
2018-06-21 13:36:22 +00:00
|
|
|
|
|
|
|
if (!TableRec->isValueUnset("PrimaryKey")) {
|
|
|
|
Table->PrimaryKey =
|
2020-09-03 09:41:09 -04:00
|
|
|
parseSearchIndex(*Table, TableRec->getValue("PrimaryKey"),
|
|
|
|
TableRec->getValueAsString("PrimaryKeyName"),
|
2018-06-21 13:36:22 +00:00
|
|
|
TableRec->getValueAsListOfStrings("PrimaryKey"),
|
|
|
|
TableRec->getValueAsBit("PrimaryKeyEarlyOut"));
|
|
|
|
|
2021-01-13 19:14:42 -08:00
|
|
|
llvm::stable_sort(Table->Entries, [&](Record *LHS, Record *RHS) {
|
|
|
|
return compareBy(LHS, RHS, *Table->PrimaryKey);
|
|
|
|
});
|
2018-06-21 13:36:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TableMap.insert(std::make_pair(TableRec, Table.get()));
|
|
|
|
Tables.emplace_back(std::move(Table));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Record *IndexRec : Records.getAllDerivedDefinitions("SearchIndex")) {
|
|
|
|
Record *TableRec = IndexRec->getValueAsDef("Table");
|
|
|
|
auto It = TableMap.find(TableRec);
|
|
|
|
if (It == TableMap.end())
|
2020-09-03 09:41:09 -04:00
|
|
|
PrintFatalError(IndexRec->getValue("Table"),
|
[tablegen] Add locations to many PrintFatalError() calls
Summary:
While working on the GISel Combiner, I noticed I was producing location-less
error messages fairly often and set about fixing this. In the process, I
noticed quite a few places elsewhere in TableGen that also neglected to include
a relevant location.
This patch adds locations to errors that relate to a specific record (or a
field within it) and also have easy access to the relevant location. This is
particularly useful when multiclasses are involved as many of these errors
refer to the full name of a record and it's difficult to guess which substring
is grep-able.
Unfortunately, tablegen currently only supports Record granularity so it's not
currently possible to point at a specific Init so these sometimes point at the
record that caused the error rather than the precise origin of the error.
Reviewers: bogner, aditya_nandakumar, volkan, aemerson, paquette, nhaehnle
Reviewed By: nhaehnle
Subscribers: jdoerfert, nhaehnle, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, zzheng, edward-jones, rogfer01, MartinMosbeck, brucehoult, the_o, PkmX, jocewei, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D58077
llvm-svn: 353862
2019-02-12 17:36:57 +00:00
|
|
|
Twine("SearchIndex '") + IndexRec->getName() +
|
2020-09-03 09:41:09 -04:00
|
|
|
"' refers to nonexistent table '" +
|
[tablegen] Add locations to many PrintFatalError() calls
Summary:
While working on the GISel Combiner, I noticed I was producing location-less
error messages fairly often and set about fixing this. In the process, I
noticed quite a few places elsewhere in TableGen that also neglected to include
a relevant location.
This patch adds locations to errors that relate to a specific record (or a
field within it) and also have easy access to the relevant location. This is
particularly useful when multiclasses are involved as many of these errors
refer to the full name of a record and it's difficult to guess which substring
is grep-able.
Unfortunately, tablegen currently only supports Record granularity so it's not
currently possible to point at a specific Init so these sometimes point at the
record that caused the error rather than the precise origin of the error.
Reviewers: bogner, aditya_nandakumar, volkan, aemerson, paquette, nhaehnle
Reviewed By: nhaehnle
Subscribers: jdoerfert, nhaehnle, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, zzheng, edward-jones, rogfer01, MartinMosbeck, brucehoult, the_o, PkmX, jocewei, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D58077
llvm-svn: 353862
2019-02-12 17:36:57 +00:00
|
|
|
TableRec->getName());
|
2018-06-21 13:36:22 +00:00
|
|
|
|
|
|
|
GenericTable &Table = *It->second;
|
2020-09-03 09:41:09 -04:00
|
|
|
Table.Indices.push_back(
|
|
|
|
parseSearchIndex(Table, IndexRec->getValue("Key"), IndexRec->getName(),
|
|
|
|
IndexRec->getValueAsListOfStrings("Key"),
|
|
|
|
IndexRec->getValueAsBit("EarlyOut")));
|
2018-06-21 13:36:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Translate legacy tables.
|
2016-07-05 21:23:04 +00:00
|
|
|
Record *SearchableTable = Records.getClass("SearchableTable");
|
|
|
|
for (auto &NameRec : Records.getClasses()) {
|
|
|
|
Record *Class = NameRec.second.get();
|
|
|
|
if (Class->getSuperClasses().size() != 1 ||
|
|
|
|
!Class->isSubClassOf(SearchableTable))
|
|
|
|
continue;
|
2018-06-21 13:36:22 +00:00
|
|
|
|
|
|
|
StringRef TableName = Class->getName();
|
|
|
|
std::vector<Record *> Items = Records.getAllDerivedDefinitions(TableName);
|
|
|
|
if (!Class->isValueUnset("EnumNameField")) {
|
|
|
|
StringRef NameField = Class->getValueAsString("EnumNameField");
|
|
|
|
StringRef ValueField;
|
|
|
|
if (!Class->isValueUnset("EnumValueField"))
|
|
|
|
ValueField = Class->getValueAsString("EnumValueField");
|
|
|
|
|
2019-08-15 15:54:37 +00:00
|
|
|
auto Enum = std::make_unique<GenericEnum>();
|
2018-06-21 13:36:22 +00:00
|
|
|
Enum->Name = (Twine(Class->getName()) + "Values").str();
|
|
|
|
Enum->PreprocessorGuard = Class->getName().upper();
|
|
|
|
Enum->Class = Class;
|
|
|
|
|
|
|
|
collectEnumEntries(*Enum, NameField, ValueField, Items);
|
|
|
|
|
|
|
|
Enums.emplace_back(std::move(Enum));
|
|
|
|
}
|
|
|
|
|
2019-08-15 15:54:37 +00:00
|
|
|
auto Table = std::make_unique<GenericTable>();
|
2018-06-21 13:36:22 +00:00
|
|
|
Table->Name = (Twine(Class->getName()) + "sList").str();
|
2020-09-03 09:41:09 -04:00
|
|
|
Table->Locs = Class->getLoc();
|
2018-06-21 13:36:22 +00:00
|
|
|
Table->PreprocessorGuard = Class->getName().upper();
|
2020-01-28 20:23:46 +01:00
|
|
|
Table->CppTypeName = std::string(Class->getName());
|
2018-06-21 13:36:22 +00:00
|
|
|
|
|
|
|
for (const RecordVal &Field : Class->getValues()) {
|
2020-01-28 20:23:46 +01:00
|
|
|
std::string FieldName = std::string(Field.getName());
|
2018-06-21 13:36:22 +00:00
|
|
|
|
|
|
|
// Skip uninteresting fields: either special to us, or injected
|
|
|
|
// template parameters (if they contain a ':').
|
|
|
|
if (FieldName.find(':') != std::string::npos ||
|
|
|
|
FieldName == "SearchableFields" || FieldName == "EnumNameField" ||
|
|
|
|
FieldName == "EnumValueField")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Table->Fields.emplace_back(FieldName);
|
|
|
|
}
|
|
|
|
|
|
|
|
collectTableEntries(*Table, Items);
|
|
|
|
|
|
|
|
for (const auto &Field :
|
|
|
|
Class->getValueAsListOfStrings("SearchableFields")) {
|
|
|
|
std::string Name =
|
|
|
|
(Twine("lookup") + Table->CppTypeName + "By" + Field).str();
|
2020-09-03 09:41:09 -04:00
|
|
|
Table->Indices.push_back(parseSearchIndex(*Table, Class->getValue(Field),
|
|
|
|
Name, {Field}, false));
|
2018-06-21 13:36:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Tables.emplace_back(std::move(Table));
|
2016-07-05 21:23:04 +00:00
|
|
|
}
|
2018-06-21 13:36:22 +00:00
|
|
|
|
|
|
|
// Emit everything.
|
|
|
|
for (const auto &Enum : Enums)
|
|
|
|
emitGenericEnum(*Enum, OS);
|
|
|
|
|
|
|
|
for (const auto &Table : Tables)
|
|
|
|
emitGenericTable(*Table, OS);
|
|
|
|
|
|
|
|
// Put all #undefs last, to allow multiple sections guarded by the same
|
|
|
|
// define.
|
|
|
|
for (const auto &Guard : PreprocessorGuards)
|
|
|
|
OS << "#undef " << Guard << "\n";
|
2016-07-05 21:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
void EmitSearchableTables(RecordKeeper &RK, raw_ostream &OS) {
|
|
|
|
SearchableTableEmitter(RK).run(OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // End llvm namespace.
|