10b57cec5SDimitry Andric //===- SearchableTableEmitter.cpp - Generate efficiently searchable tables -==// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This tablegen backend emits a generic array initialized by specified fields, 10*0fca6ea1SDimitry Andric // together with companion index tables and lookup functions. The lookup 11*0fca6ea1SDimitry Andric // function generated is either a direct lookup (when a single primary key field 12*0fca6ea1SDimitry Andric // is integral and densely numbered) or a binary search otherwise. 130b57cec5SDimitry Andric // 140b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 150b57cec5SDimitry Andric 16*0fca6ea1SDimitry Andric #include "Basic/CodeGenIntrinsics.h" 17*0fca6ea1SDimitry Andric #include "Common/CodeGenTarget.h" 18e8d8bef9SDimitry Andric #include "llvm/ADT/ArrayRef.h" 190b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 20fcaf7f86SDimitry Andric #include "llvm/ADT/STLExtras.h" 210b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h" 220b57cec5SDimitry Andric #include "llvm/TableGen/Error.h" 230b57cec5SDimitry Andric #include "llvm/TableGen/Record.h" 2406c3fb27SDimitry Andric #include "llvm/TableGen/TableGenBackend.h" 250b57cec5SDimitry Andric #include <algorithm> 260b57cec5SDimitry Andric #include <set> 270b57cec5SDimitry Andric #include <string> 280b57cec5SDimitry Andric #include <vector> 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric using namespace llvm; 310b57cec5SDimitry Andric 320b57cec5SDimitry Andric #define DEBUG_TYPE "searchable-table-emitter" 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric namespace { 350b57cec5SDimitry Andric 365f757f3fSDimitry Andric int64_t getAsInt(Init *B) { 3781ad6265SDimitry Andric return cast<IntInit>( 3881ad6265SDimitry Andric B->convertInitializerTo(IntRecTy::get(B->getRecordKeeper()))) 3981ad6265SDimitry Andric ->getValue(); 400b57cec5SDimitry Andric } 415f757f3fSDimitry Andric int64_t getInt(Record *R, StringRef Field) { 420b57cec5SDimitry Andric return getAsInt(R->getValueInit(Field)); 430b57cec5SDimitry Andric } 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric struct GenericEnum { 460b57cec5SDimitry Andric using Entry = std::pair<StringRef, int64_t>; 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric std::string Name; 49480093f4SDimitry Andric Record *Class = nullptr; 500b57cec5SDimitry Andric std::string PreprocessorGuard; 510b57cec5SDimitry Andric std::vector<std::unique_ptr<Entry>> Entries; 520b57cec5SDimitry Andric DenseMap<Record *, Entry *> EntryMap; 530b57cec5SDimitry Andric }; 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric struct GenericField { 560b57cec5SDimitry Andric std::string Name; 570b57cec5SDimitry Andric RecTy *RecType = nullptr; 58e8d8bef9SDimitry Andric bool IsCode = false; 590b57cec5SDimitry Andric bool IsIntrinsic = false; 600b57cec5SDimitry Andric bool IsInstruction = false; 610b57cec5SDimitry Andric GenericEnum *Enum = nullptr; 620b57cec5SDimitry Andric 635ffd83dbSDimitry Andric GenericField(StringRef Name) : Name(std::string(Name)) {} 640b57cec5SDimitry Andric }; 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric struct SearchIndex { 670b57cec5SDimitry Andric std::string Name; 68e8d8bef9SDimitry Andric SMLoc Loc; // Source location of PrimaryKey or Key field definition. 690b57cec5SDimitry Andric SmallVector<GenericField, 1> Fields; 70480093f4SDimitry Andric bool EarlyOut = false; 71*0fca6ea1SDimitry Andric bool ReturnRange = false; 720b57cec5SDimitry Andric }; 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric struct GenericTable { 750b57cec5SDimitry Andric std::string Name; 76e8d8bef9SDimitry Andric ArrayRef<SMLoc> Locs; // Source locations from the Record instance. 770b57cec5SDimitry Andric std::string PreprocessorGuard; 780b57cec5SDimitry Andric std::string CppTypeName; 790b57cec5SDimitry Andric SmallVector<GenericField, 2> Fields; 800b57cec5SDimitry Andric std::vector<Record *> Entries; 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric std::unique_ptr<SearchIndex> PrimaryKey; 830b57cec5SDimitry Andric SmallVector<std::unique_ptr<SearchIndex>, 2> Indices; 840b57cec5SDimitry Andric 850b57cec5SDimitry Andric const GenericField *getFieldByName(StringRef Name) const { 860b57cec5SDimitry Andric for (const auto &Field : Fields) { 870b57cec5SDimitry Andric if (Name == Field.Name) 880b57cec5SDimitry Andric return &Field; 890b57cec5SDimitry Andric } 900b57cec5SDimitry Andric return nullptr; 910b57cec5SDimitry Andric } 920b57cec5SDimitry Andric }; 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric class SearchableTableEmitter { 950b57cec5SDimitry Andric RecordKeeper &Records; 96*0fca6ea1SDimitry Andric std::unique_ptr<CodeGenTarget> Target; 970b57cec5SDimitry Andric DenseMap<Init *, std::unique_ptr<CodeGenIntrinsic>> Intrinsics; 980b57cec5SDimitry Andric std::vector<std::unique_ptr<GenericEnum>> Enums; 990b57cec5SDimitry Andric DenseMap<Record *, GenericEnum *> EnumMap; 1000b57cec5SDimitry Andric std::set<std::string> PreprocessorGuards; 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric public: 1030b57cec5SDimitry Andric SearchableTableEmitter(RecordKeeper &R) : Records(R) {} 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric void run(raw_ostream &OS); 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric private: 1080b57cec5SDimitry Andric typedef std::pair<Init *, int> SearchTableEntry; 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric enum TypeContext { 1110b57cec5SDimitry Andric TypeInStaticStruct, 1120b57cec5SDimitry Andric TypeInTempStruct, 1130b57cec5SDimitry Andric TypeInArgument, 1140b57cec5SDimitry Andric }; 1150b57cec5SDimitry Andric 116e8d8bef9SDimitry Andric std::string primaryRepresentation(SMLoc Loc, const GenericField &Field, 117e8d8bef9SDimitry Andric Init *I) { 118e8d8bef9SDimitry Andric if (StringInit *SI = dyn_cast<StringInit>(I)) { 119e8d8bef9SDimitry Andric if (Field.IsCode || SI->hasCodeFormat()) 120e8d8bef9SDimitry Andric return std::string(SI->getValue()); 121e8d8bef9SDimitry Andric else 1220b57cec5SDimitry Andric return SI->getAsString(); 123e8d8bef9SDimitry Andric } else if (BitsInit *BI = dyn_cast<BitsInit>(I)) 1240b57cec5SDimitry Andric return "0x" + utohexstr(getAsInt(BI)); 1250b57cec5SDimitry Andric else if (BitInit *BI = dyn_cast<BitInit>(I)) 1260b57cec5SDimitry Andric return BI->getValue() ? "true" : "false"; 1270b57cec5SDimitry Andric else if (Field.IsIntrinsic) 1280b57cec5SDimitry Andric return "Intrinsic::" + getIntrinsic(I).EnumName; 1290b57cec5SDimitry Andric else if (Field.IsInstruction) 1300b57cec5SDimitry Andric return I->getAsString(); 1315ffd83dbSDimitry Andric else if (Field.Enum) { 1325ffd83dbSDimitry Andric auto *Entry = Field.Enum->EntryMap[cast<DefInit>(I)->getDef()]; 1335ffd83dbSDimitry Andric if (!Entry) 134e8d8bef9SDimitry Andric PrintFatalError(Loc, 135e8d8bef9SDimitry Andric Twine("Entry for field '") + Field.Name + "' is null"); 1365ffd83dbSDimitry Andric return std::string(Entry->first); 1375ffd83dbSDimitry Andric } 138e8d8bef9SDimitry Andric PrintFatalError(Loc, Twine("invalid field type for field '") + Field.Name + 139e8d8bef9SDimitry Andric "'; expected: bit, bits, string, or code"); 1400b57cec5SDimitry Andric } 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric bool isIntrinsic(Init *I) { 1430b57cec5SDimitry Andric if (DefInit *DI = dyn_cast<DefInit>(I)) 1440b57cec5SDimitry Andric return DI->getDef()->isSubClassOf("Intrinsic"); 1450b57cec5SDimitry Andric return false; 1460b57cec5SDimitry Andric } 1470b57cec5SDimitry Andric 1480b57cec5SDimitry Andric CodeGenIntrinsic &getIntrinsic(Init *I) { 1490b57cec5SDimitry Andric std::unique_ptr<CodeGenIntrinsic> &Intr = Intrinsics[I]; 1500b57cec5SDimitry Andric if (!Intr) 151e8d8bef9SDimitry Andric Intr = std::make_unique<CodeGenIntrinsic>(cast<DefInit>(I)->getDef(), 152e8d8bef9SDimitry Andric std::vector<Record *>()); 1530b57cec5SDimitry Andric return *Intr; 1540b57cec5SDimitry Andric } 1550b57cec5SDimitry Andric 1560b57cec5SDimitry Andric bool compareBy(Record *LHS, Record *RHS, const SearchIndex &Index); 1570b57cec5SDimitry Andric 158e8d8bef9SDimitry Andric std::string searchableFieldType(const GenericTable &Table, 159e8d8bef9SDimitry Andric const SearchIndex &Index, 160e8d8bef9SDimitry Andric const GenericField &Field, TypeContext Ctx) { 1610b57cec5SDimitry Andric if (isa<StringRecTy>(Field.RecType)) { 1620b57cec5SDimitry Andric if (Ctx == TypeInStaticStruct) 1630b57cec5SDimitry Andric return "const char *"; 1640b57cec5SDimitry Andric if (Ctx == TypeInTempStruct) 1650b57cec5SDimitry Andric return "std::string"; 1660b57cec5SDimitry Andric return "StringRef"; 1670b57cec5SDimitry Andric } else if (BitsRecTy *BI = dyn_cast<BitsRecTy>(Field.RecType)) { 1680b57cec5SDimitry Andric unsigned NumBits = BI->getNumBits(); 1690b57cec5SDimitry Andric if (NumBits <= 8) 1700b57cec5SDimitry Andric return "uint8_t"; 1710b57cec5SDimitry Andric if (NumBits <= 16) 1720b57cec5SDimitry Andric return "uint16_t"; 1730b57cec5SDimitry Andric if (NumBits <= 32) 1740b57cec5SDimitry Andric return "uint32_t"; 1750b57cec5SDimitry Andric if (NumBits <= 64) 1760b57cec5SDimitry Andric return "uint64_t"; 177e8d8bef9SDimitry Andric PrintFatalError(Index.Loc, Twine("In table '") + Table.Name + 178e8d8bef9SDimitry Andric "' lookup method '" + Index.Name + 179e8d8bef9SDimitry Andric "', key field '" + Field.Name + 180e8d8bef9SDimitry Andric "' of type bits is too large"); 18106c3fb27SDimitry Andric } else if (isa<BitRecTy>(Field.RecType)) { 18206c3fb27SDimitry Andric return "bool"; 1830b57cec5SDimitry Andric } else if (Field.Enum || Field.IsIntrinsic || Field.IsInstruction) 1840b57cec5SDimitry Andric return "unsigned"; 185e8d8bef9SDimitry Andric PrintFatalError(Index.Loc, 186e8d8bef9SDimitry Andric Twine("In table '") + Table.Name + "' lookup method '" + 187e8d8bef9SDimitry Andric Index.Name + "', key field '" + Field.Name + 188e8d8bef9SDimitry Andric "' has invalid type: " + Field.RecType->getAsString()); 1890b57cec5SDimitry Andric } 1900b57cec5SDimitry Andric 1910b57cec5SDimitry Andric void emitGenericTable(const GenericTable &Table, raw_ostream &OS); 1920b57cec5SDimitry Andric void emitGenericEnum(const GenericEnum &Enum, raw_ostream &OS); 1930b57cec5SDimitry Andric void emitLookupDeclaration(const GenericTable &Table, 1940b57cec5SDimitry Andric const SearchIndex &Index, raw_ostream &OS); 1950b57cec5SDimitry Andric void emitLookupFunction(const GenericTable &Table, const SearchIndex &Index, 1960b57cec5SDimitry Andric bool IsPrimary, raw_ostream &OS); 1970b57cec5SDimitry Andric void emitIfdef(StringRef Guard, raw_ostream &OS); 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric bool parseFieldType(GenericField &Field, Init *II); 2000b57cec5SDimitry Andric std::unique_ptr<SearchIndex> 201e8d8bef9SDimitry Andric parseSearchIndex(GenericTable &Table, const RecordVal *RecVal, StringRef Name, 202*0fca6ea1SDimitry Andric const std::vector<StringRef> &Key, bool EarlyOut, 203*0fca6ea1SDimitry Andric bool ReturnRange); 2040b57cec5SDimitry Andric void collectEnumEntries(GenericEnum &Enum, StringRef NameField, 2050b57cec5SDimitry Andric StringRef ValueField, 2060b57cec5SDimitry Andric const std::vector<Record *> &Items); 2070b57cec5SDimitry Andric void collectTableEntries(GenericTable &Table, 2080b57cec5SDimitry Andric const std::vector<Record *> &Items); 209*0fca6ea1SDimitry Andric int64_t getNumericKey(const SearchIndex &Index, Record *Rec); 2100b57cec5SDimitry Andric }; 2110b57cec5SDimitry Andric 2120b57cec5SDimitry Andric } // End anonymous namespace. 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric // For search indices that consists of a single field whose numeric value is 2150b57cec5SDimitry Andric // known, return that numeric value. 216*0fca6ea1SDimitry Andric int64_t SearchableTableEmitter::getNumericKey(const SearchIndex &Index, 217*0fca6ea1SDimitry Andric Record *Rec) { 2180b57cec5SDimitry Andric assert(Index.Fields.size() == 1); 2190b57cec5SDimitry Andric 220*0fca6ea1SDimitry Andric // To be consistent with compareBy and primaryRepresentation elsewhere, 221*0fca6ea1SDimitry Andric // we check for IsInstruction before Enum-- these fields are not exclusive. 222*0fca6ea1SDimitry Andric if (Index.Fields[0].IsInstruction) { 223*0fca6ea1SDimitry Andric Record *TheDef = Rec->getValueAsDef(Index.Fields[0].Name); 224*0fca6ea1SDimitry Andric return Target->getInstrIntValue(TheDef); 225*0fca6ea1SDimitry Andric } 2260b57cec5SDimitry Andric if (Index.Fields[0].Enum) { 2270b57cec5SDimitry Andric Record *EnumEntry = Rec->getValueAsDef(Index.Fields[0].Name); 2280b57cec5SDimitry Andric return Index.Fields[0].Enum->EntryMap[EnumEntry]->second; 2290b57cec5SDimitry Andric } 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric return getInt(Rec, Index.Fields[0].Name); 2320b57cec5SDimitry Andric } 2330b57cec5SDimitry Andric 2340b57cec5SDimitry Andric /// Less-than style comparison between \p LHS and \p RHS according to the 2350b57cec5SDimitry Andric /// key of \p Index. 2360b57cec5SDimitry Andric bool SearchableTableEmitter::compareBy(Record *LHS, Record *RHS, 2370b57cec5SDimitry Andric const SearchIndex &Index) { 2380b57cec5SDimitry Andric for (const auto &Field : Index.Fields) { 2390b57cec5SDimitry Andric Init *LHSI = LHS->getValueInit(Field.Name); 2400b57cec5SDimitry Andric Init *RHSI = RHS->getValueInit(Field.Name); 2410b57cec5SDimitry Andric 2420b57cec5SDimitry Andric if (isa<BitsRecTy>(Field.RecType) || isa<IntRecTy>(Field.RecType)) { 2430b57cec5SDimitry Andric int64_t LHSi = getAsInt(LHSI); 2440b57cec5SDimitry Andric int64_t RHSi = getAsInt(RHSI); 2450b57cec5SDimitry Andric if (LHSi < RHSi) 2460b57cec5SDimitry Andric return true; 2470b57cec5SDimitry Andric if (LHSi > RHSi) 2480b57cec5SDimitry Andric return false; 2490b57cec5SDimitry Andric } else if (Field.IsIntrinsic) { 2500b57cec5SDimitry Andric CodeGenIntrinsic &LHSi = getIntrinsic(LHSI); 2510b57cec5SDimitry Andric CodeGenIntrinsic &RHSi = getIntrinsic(RHSI); 2520b57cec5SDimitry Andric if (std::tie(LHSi.TargetPrefix, LHSi.Name) < 2530b57cec5SDimitry Andric std::tie(RHSi.TargetPrefix, RHSi.Name)) 2540b57cec5SDimitry Andric return true; 2550b57cec5SDimitry Andric if (std::tie(LHSi.TargetPrefix, LHSi.Name) > 2560b57cec5SDimitry Andric std::tie(RHSi.TargetPrefix, RHSi.Name)) 2570b57cec5SDimitry Andric return false; 2580b57cec5SDimitry Andric } else if (Field.IsInstruction) { 2590b57cec5SDimitry Andric // This does not correctly compare the predefined instructions! 2600b57cec5SDimitry Andric Record *LHSr = cast<DefInit>(LHSI)->getDef(); 2610b57cec5SDimitry Andric Record *RHSr = cast<DefInit>(RHSI)->getDef(); 2620b57cec5SDimitry Andric 2630b57cec5SDimitry Andric bool LHSpseudo = LHSr->getValueAsBit("isPseudo"); 2640b57cec5SDimitry Andric bool RHSpseudo = RHSr->getValueAsBit("isPseudo"); 2650b57cec5SDimitry Andric if (LHSpseudo && !RHSpseudo) 2660b57cec5SDimitry Andric return true; 2670b57cec5SDimitry Andric if (!LHSpseudo && RHSpseudo) 2680b57cec5SDimitry Andric return false; 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric int comp = LHSr->getName().compare(RHSr->getName()); 2710b57cec5SDimitry Andric if (comp < 0) 2720b57cec5SDimitry Andric return true; 2730b57cec5SDimitry Andric if (comp > 0) 2740b57cec5SDimitry Andric return false; 2750b57cec5SDimitry Andric } else if (Field.Enum) { 2760b57cec5SDimitry Andric auto LHSr = cast<DefInit>(LHSI)->getDef(); 2770b57cec5SDimitry Andric auto RHSr = cast<DefInit>(RHSI)->getDef(); 2780b57cec5SDimitry Andric int64_t LHSv = Field.Enum->EntryMap[LHSr]->second; 2790b57cec5SDimitry Andric int64_t RHSv = Field.Enum->EntryMap[RHSr]->second; 2800b57cec5SDimitry Andric if (LHSv < RHSv) 2810b57cec5SDimitry Andric return true; 2820b57cec5SDimitry Andric if (LHSv > RHSv) 2830b57cec5SDimitry Andric return false; 2840b57cec5SDimitry Andric } else { 285e8d8bef9SDimitry Andric std::string LHSs = primaryRepresentation(Index.Loc, Field, LHSI); 286e8d8bef9SDimitry Andric std::string RHSs = primaryRepresentation(Index.Loc, Field, RHSI); 2870b57cec5SDimitry Andric 2880b57cec5SDimitry Andric if (isa<StringRecTy>(Field.RecType)) { 2890b57cec5SDimitry Andric LHSs = StringRef(LHSs).upper(); 2900b57cec5SDimitry Andric RHSs = StringRef(RHSs).upper(); 2910b57cec5SDimitry Andric } 2920b57cec5SDimitry Andric 2930b57cec5SDimitry Andric int comp = LHSs.compare(RHSs); 2940b57cec5SDimitry Andric if (comp < 0) 2950b57cec5SDimitry Andric return true; 2960b57cec5SDimitry Andric if (comp > 0) 2970b57cec5SDimitry Andric return false; 2980b57cec5SDimitry Andric } 2990b57cec5SDimitry Andric } 3000b57cec5SDimitry Andric return false; 3010b57cec5SDimitry Andric } 3020b57cec5SDimitry Andric 3030b57cec5SDimitry Andric void SearchableTableEmitter::emitIfdef(StringRef Guard, raw_ostream &OS) { 3040b57cec5SDimitry Andric OS << "#ifdef " << Guard << "\n"; 3055ffd83dbSDimitry Andric PreprocessorGuards.insert(std::string(Guard)); 3060b57cec5SDimitry Andric } 3070b57cec5SDimitry Andric 3080b57cec5SDimitry Andric /// Emit a generic enum. 3090b57cec5SDimitry Andric void SearchableTableEmitter::emitGenericEnum(const GenericEnum &Enum, 3100b57cec5SDimitry Andric raw_ostream &OS) { 3110b57cec5SDimitry Andric emitIfdef((Twine("GET_") + Enum.PreprocessorGuard + "_DECL").str(), OS); 3120b57cec5SDimitry Andric 3130b57cec5SDimitry Andric OS << "enum " << Enum.Name << " {\n"; 3140b57cec5SDimitry Andric for (const auto &Entry : Enum.Entries) 3150b57cec5SDimitry Andric OS << " " << Entry->first << " = " << Entry->second << ",\n"; 3160b57cec5SDimitry Andric OS << "};\n"; 3170b57cec5SDimitry Andric 3180b57cec5SDimitry Andric OS << "#endif\n\n"; 3190b57cec5SDimitry Andric } 3200b57cec5SDimitry Andric 3210b57cec5SDimitry Andric void SearchableTableEmitter::emitLookupFunction(const GenericTable &Table, 3220b57cec5SDimitry Andric const SearchIndex &Index, 3230b57cec5SDimitry Andric bool IsPrimary, 3240b57cec5SDimitry Andric raw_ostream &OS) { 3250b57cec5SDimitry Andric OS << "\n"; 3260b57cec5SDimitry Andric emitLookupDeclaration(Table, Index, OS); 3270b57cec5SDimitry Andric OS << " {\n"; 3280b57cec5SDimitry Andric 3290b57cec5SDimitry Andric std::vector<Record *> IndexRowsStorage; 3300b57cec5SDimitry Andric ArrayRef<Record *> IndexRows; 3310b57cec5SDimitry Andric StringRef IndexTypeName; 3320b57cec5SDimitry Andric StringRef IndexName; 3330b57cec5SDimitry Andric 3340b57cec5SDimitry Andric if (IsPrimary) { 3350b57cec5SDimitry Andric IndexTypeName = Table.CppTypeName; 3360b57cec5SDimitry Andric IndexName = Table.Name; 3370b57cec5SDimitry Andric IndexRows = Table.Entries; 3380b57cec5SDimitry Andric } else { 3390b57cec5SDimitry Andric OS << " struct IndexType {\n"; 3400b57cec5SDimitry Andric for (const auto &Field : Index.Fields) { 341e8d8bef9SDimitry Andric OS << " " 342e8d8bef9SDimitry Andric << searchableFieldType(Table, Index, Field, TypeInStaticStruct) << " " 3430b57cec5SDimitry Andric << Field.Name << ";\n"; 3440b57cec5SDimitry Andric } 3450b57cec5SDimitry Andric OS << " unsigned _index;\n"; 3460b57cec5SDimitry Andric OS << " };\n"; 3470b57cec5SDimitry Andric 3480b57cec5SDimitry Andric OS << " static const struct IndexType Index[] = {\n"; 3490b57cec5SDimitry Andric 3500b57cec5SDimitry Andric std::vector<std::pair<Record *, unsigned>> Entries; 3510b57cec5SDimitry Andric Entries.reserve(Table.Entries.size()); 3520b57cec5SDimitry Andric for (unsigned i = 0; i < Table.Entries.size(); ++i) 3530b57cec5SDimitry Andric Entries.emplace_back(Table.Entries[i], i); 3540b57cec5SDimitry Andric 355e8d8bef9SDimitry Andric llvm::stable_sort(Entries, [&](const std::pair<Record *, unsigned> &LHS, 3560b57cec5SDimitry Andric const std::pair<Record *, unsigned> &RHS) { 3570b57cec5SDimitry Andric return compareBy(LHS.first, RHS.first, Index); 3580b57cec5SDimitry Andric }); 3590b57cec5SDimitry Andric 3600b57cec5SDimitry Andric IndexRowsStorage.reserve(Entries.size()); 3610b57cec5SDimitry Andric for (const auto &Entry : Entries) { 3620b57cec5SDimitry Andric IndexRowsStorage.push_back(Entry.first); 3630b57cec5SDimitry Andric 3640b57cec5SDimitry Andric OS << " { "; 365fe6060f1SDimitry Andric ListSeparator LS; 3660b57cec5SDimitry Andric for (const auto &Field : Index.Fields) { 367e8d8bef9SDimitry Andric std::string Repr = primaryRepresentation( 368e8d8bef9SDimitry Andric Index.Loc, Field, Entry.first->getValueInit(Field.Name)); 3690b57cec5SDimitry Andric if (isa<StringRecTy>(Field.RecType)) 3700b57cec5SDimitry Andric Repr = StringRef(Repr).upper(); 371fe6060f1SDimitry Andric OS << LS << Repr; 3720b57cec5SDimitry Andric } 3730b57cec5SDimitry Andric OS << ", " << Entry.second << " },\n"; 3740b57cec5SDimitry Andric } 3750b57cec5SDimitry Andric 3760b57cec5SDimitry Andric OS << " };\n\n"; 3770b57cec5SDimitry Andric 3780b57cec5SDimitry Andric IndexTypeName = "IndexType"; 3790b57cec5SDimitry Andric IndexName = "Index"; 3800b57cec5SDimitry Andric IndexRows = IndexRowsStorage; 3810b57cec5SDimitry Andric } 3820b57cec5SDimitry Andric 3830b57cec5SDimitry Andric bool IsContiguous = false; 3840b57cec5SDimitry Andric 3850b57cec5SDimitry Andric if (Index.Fields.size() == 1 && 386*0fca6ea1SDimitry Andric (Index.Fields[0].Enum || isa<BitsRecTy>(Index.Fields[0].RecType) || 387*0fca6ea1SDimitry Andric Index.Fields[0].IsInstruction)) { 388*0fca6ea1SDimitry Andric int64_t FirstKeyVal = getNumericKey(Index, IndexRows[0]); 3890b57cec5SDimitry Andric IsContiguous = true; 3900b57cec5SDimitry Andric for (unsigned i = 0; i < IndexRows.size(); ++i) { 391*0fca6ea1SDimitry Andric if (getNumericKey(Index, IndexRows[i]) != (FirstKeyVal + i)) { 3920b57cec5SDimitry Andric IsContiguous = false; 3930b57cec5SDimitry Andric break; 3940b57cec5SDimitry Andric } 3950b57cec5SDimitry Andric } 3960b57cec5SDimitry Andric } 3970b57cec5SDimitry Andric 3980b57cec5SDimitry Andric if (IsContiguous) { 399*0fca6ea1SDimitry Andric const GenericField &Field = Index.Fields[0]; 400*0fca6ea1SDimitry Andric std::string FirstRepr = primaryRepresentation( 401*0fca6ea1SDimitry Andric Index.Loc, Field, IndexRows[0]->getValueInit(Field.Name)); 402*0fca6ea1SDimitry Andric std::string LastRepr = primaryRepresentation( 403*0fca6ea1SDimitry Andric Index.Loc, Field, IndexRows.back()->getValueInit(Field.Name)); 404*0fca6ea1SDimitry Andric OS << " if ((" << Field.Name << " < " << FirstRepr << ") ||\n"; 405*0fca6ea1SDimitry Andric OS << " (" << Field.Name << " > " << LastRepr << "))\n"; 406*0fca6ea1SDimitry Andric OS << " return nullptr;\n"; 407bdd1243dSDimitry Andric OS << " auto Table = ArrayRef(" << IndexName << ");\n"; 408*0fca6ea1SDimitry Andric OS << " size_t Idx = " << Index.Fields[0].Name << " - " << FirstRepr 409*0fca6ea1SDimitry Andric << ";\n"; 410*0fca6ea1SDimitry Andric OS << " return "; 4110b57cec5SDimitry Andric if (IsPrimary) 4120b57cec5SDimitry Andric OS << "&Table[Idx]"; 4130b57cec5SDimitry Andric else 4140b57cec5SDimitry Andric OS << "&" << Table.Name << "[Table[Idx]._index]"; 4150b57cec5SDimitry Andric OS << ";\n"; 4160b57cec5SDimitry Andric OS << "}\n"; 4170b57cec5SDimitry Andric return; 4180b57cec5SDimitry Andric } 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andric if (Index.EarlyOut) { 4210b57cec5SDimitry Andric const GenericField &Field = Index.Fields[0]; 422e8d8bef9SDimitry Andric std::string FirstRepr = primaryRepresentation( 423e8d8bef9SDimitry Andric Index.Loc, Field, IndexRows[0]->getValueInit(Field.Name)); 4240b57cec5SDimitry Andric std::string LastRepr = primaryRepresentation( 425e8d8bef9SDimitry Andric Index.Loc, Field, IndexRows.back()->getValueInit(Field.Name)); 4260b57cec5SDimitry Andric OS << " if ((" << Field.Name << " < " << FirstRepr << ") ||\n"; 4270b57cec5SDimitry Andric OS << " (" << Field.Name << " > " << LastRepr << "))\n"; 4280b57cec5SDimitry Andric OS << " return nullptr;\n\n"; 4290b57cec5SDimitry Andric } 4300b57cec5SDimitry Andric 4310b57cec5SDimitry Andric OS << " struct KeyType {\n"; 4320b57cec5SDimitry Andric for (const auto &Field : Index.Fields) { 433e8d8bef9SDimitry Andric OS << " " << searchableFieldType(Table, Index, Field, TypeInTempStruct) 434e8d8bef9SDimitry Andric << " " << Field.Name << ";\n"; 4350b57cec5SDimitry Andric } 4360b57cec5SDimitry Andric OS << " };\n"; 4370b57cec5SDimitry Andric OS << " KeyType Key = {"; 438fe6060f1SDimitry Andric ListSeparator LS; 4390b57cec5SDimitry Andric for (const auto &Field : Index.Fields) { 440fe6060f1SDimitry Andric OS << LS << Field.Name; 4410b57cec5SDimitry Andric if (isa<StringRecTy>(Field.RecType)) { 4420b57cec5SDimitry Andric OS << ".upper()"; 4430b57cec5SDimitry Andric if (IsPrimary) 444e8d8bef9SDimitry Andric PrintFatalError(Index.Loc, 445e8d8bef9SDimitry Andric Twine("In table '") + Table.Name + 446e8d8bef9SDimitry Andric "', use a secondary lookup method for " 447e8d8bef9SDimitry Andric "case-insensitive comparison of field '" + 448e8d8bef9SDimitry Andric Field.Name + "'"); 4490b57cec5SDimitry Andric } 4500b57cec5SDimitry Andric } 4510b57cec5SDimitry Andric OS << "};\n"; 4520b57cec5SDimitry Andric 453*0fca6ea1SDimitry Andric OS << " struct Comp {\n"; 454*0fca6ea1SDimitry Andric OS << " bool operator()(const " << IndexTypeName 455*0fca6ea1SDimitry Andric << " &LHS, const KeyType &RHS) const {\n"; 4560b57cec5SDimitry Andric 457*0fca6ea1SDimitry Andric auto emitComparator = [&]() { 4580b57cec5SDimitry Andric for (const auto &Field : Index.Fields) { 4590b57cec5SDimitry Andric if (isa<StringRecTy>(Field.RecType)) { 4600b57cec5SDimitry Andric OS << " int Cmp" << Field.Name << " = StringRef(LHS." << Field.Name 4610b57cec5SDimitry Andric << ").compare(RHS." << Field.Name << ");\n"; 4620b57cec5SDimitry Andric OS << " if (Cmp" << Field.Name << " < 0) return true;\n"; 4630b57cec5SDimitry Andric OS << " if (Cmp" << Field.Name << " > 0) return false;\n"; 4640b57cec5SDimitry Andric } else if (Field.Enum) { 4650b57cec5SDimitry Andric // Explicitly cast to unsigned, because the signedness of enums is 4660b57cec5SDimitry Andric // compiler-dependent. 4670b57cec5SDimitry Andric OS << " if ((unsigned)LHS." << Field.Name << " < (unsigned)RHS." 4680b57cec5SDimitry Andric << Field.Name << ")\n"; 4690b57cec5SDimitry Andric OS << " return true;\n"; 4700b57cec5SDimitry Andric OS << " if ((unsigned)LHS." << Field.Name << " > (unsigned)RHS." 4710b57cec5SDimitry Andric << Field.Name << ")\n"; 4720b57cec5SDimitry Andric OS << " return false;\n"; 4730b57cec5SDimitry Andric } else { 474*0fca6ea1SDimitry Andric OS << " if (LHS." << Field.Name << " < RHS." << Field.Name 475*0fca6ea1SDimitry Andric << ")\n"; 4760b57cec5SDimitry Andric OS << " return true;\n"; 477*0fca6ea1SDimitry Andric OS << " if (LHS." << Field.Name << " > RHS." << Field.Name 478*0fca6ea1SDimitry Andric << ")\n"; 4790b57cec5SDimitry Andric OS << " return false;\n"; 4800b57cec5SDimitry Andric } 4810b57cec5SDimitry Andric } 4820b57cec5SDimitry Andric OS << " return false;\n"; 483*0fca6ea1SDimitry Andric OS << " }\n"; 484*0fca6ea1SDimitry Andric }; 485*0fca6ea1SDimitry Andric emitComparator(); 486*0fca6ea1SDimitry Andric bool ShouldReturnRange = Index.ReturnRange; 487*0fca6ea1SDimitry Andric if (ShouldReturnRange) { 488*0fca6ea1SDimitry Andric OS << " bool operator()(const KeyType &LHS, const " << IndexTypeName 489*0fca6ea1SDimitry Andric << " &RHS) const {\n"; 490*0fca6ea1SDimitry Andric emitComparator(); 491*0fca6ea1SDimitry Andric } 4920b57cec5SDimitry Andric 493*0fca6ea1SDimitry Andric OS << " };\n"; 494*0fca6ea1SDimitry Andric OS << " auto Table = ArrayRef(" << IndexName << ");\n"; 495*0fca6ea1SDimitry Andric if (ShouldReturnRange) 496*0fca6ea1SDimitry Andric OS << " auto It = std::equal_range(Table.begin(), Table.end(), Key, "; 497*0fca6ea1SDimitry Andric else 498*0fca6ea1SDimitry Andric OS << " auto Idx = std::lower_bound(Table.begin(), Table.end(), Key, "; 499*0fca6ea1SDimitry Andric OS << "Comp());\n"; 500*0fca6ea1SDimitry Andric 501*0fca6ea1SDimitry Andric if (!ShouldReturnRange) { 5020b57cec5SDimitry Andric OS << " if (Idx == Table.end()"; 5030b57cec5SDimitry Andric for (const auto &Field : Index.Fields) 5040b57cec5SDimitry Andric OS << " ||\n Key." << Field.Name << " != Idx->" << Field.Name; 505*0fca6ea1SDimitry Andric } 5060b57cec5SDimitry Andric 507*0fca6ea1SDimitry Andric if (ShouldReturnRange) 508*0fca6ea1SDimitry Andric OS << " return llvm::make_range(It.first, It.second);\n"; 509*0fca6ea1SDimitry Andric else if (IsPrimary) { 510*0fca6ea1SDimitry Andric OS << ")\n return nullptr;\n\n"; 5110b57cec5SDimitry Andric OS << " return &*Idx;\n"; 512*0fca6ea1SDimitry Andric } else { 513*0fca6ea1SDimitry Andric OS << ")\n return nullptr;\n\n"; 5140b57cec5SDimitry Andric OS << " return &" << Table.Name << "[Idx->_index];\n"; 515*0fca6ea1SDimitry Andric } 5160b57cec5SDimitry Andric 5170b57cec5SDimitry Andric OS << "}\n"; 5180b57cec5SDimitry Andric } 5190b57cec5SDimitry Andric 5200b57cec5SDimitry Andric void SearchableTableEmitter::emitLookupDeclaration(const GenericTable &Table, 5210b57cec5SDimitry Andric const SearchIndex &Index, 5220b57cec5SDimitry Andric raw_ostream &OS) { 523*0fca6ea1SDimitry Andric if (Index.ReturnRange) 524*0fca6ea1SDimitry Andric OS << "llvm::iterator_range<const " << Table.CppTypeName << " *> "; 525*0fca6ea1SDimitry Andric else 526*0fca6ea1SDimitry Andric OS << "const " << Table.CppTypeName << " *"; 527*0fca6ea1SDimitry Andric OS << Index.Name << "("; 528fe6060f1SDimitry Andric ListSeparator LS; 529fe6060f1SDimitry Andric for (const auto &Field : Index.Fields) 530fe6060f1SDimitry Andric OS << LS << searchableFieldType(Table, Index, Field, TypeInArgument) << " " 531e8d8bef9SDimitry Andric << Field.Name; 5320b57cec5SDimitry Andric OS << ")"; 5330b57cec5SDimitry Andric } 5340b57cec5SDimitry Andric 5350b57cec5SDimitry Andric void SearchableTableEmitter::emitGenericTable(const GenericTable &Table, 5360b57cec5SDimitry Andric raw_ostream &OS) { 5370b57cec5SDimitry Andric emitIfdef((Twine("GET_") + Table.PreprocessorGuard + "_DECL").str(), OS); 5380b57cec5SDimitry Andric 5390b57cec5SDimitry Andric // Emit the declarations for the functions that will perform lookup. 5400b57cec5SDimitry Andric if (Table.PrimaryKey) { 5410b57cec5SDimitry Andric emitLookupDeclaration(Table, *Table.PrimaryKey, OS); 5420b57cec5SDimitry Andric OS << ";\n"; 5430b57cec5SDimitry Andric } 5440b57cec5SDimitry Andric for (const auto &Index : Table.Indices) { 5450b57cec5SDimitry Andric emitLookupDeclaration(Table, *Index, OS); 5460b57cec5SDimitry Andric OS << ";\n"; 5470b57cec5SDimitry Andric } 5480b57cec5SDimitry Andric 5490b57cec5SDimitry Andric OS << "#endif\n\n"; 5500b57cec5SDimitry Andric 5510b57cec5SDimitry Andric emitIfdef((Twine("GET_") + Table.PreprocessorGuard + "_IMPL").str(), OS); 5520b57cec5SDimitry Andric 5530b57cec5SDimitry Andric // The primary data table contains all the fields defined for this map. 5548bcb0991SDimitry Andric OS << "constexpr " << Table.CppTypeName << " " << Table.Name << "[] = {\n"; 5550b57cec5SDimitry Andric for (unsigned i = 0; i < Table.Entries.size(); ++i) { 5560b57cec5SDimitry Andric Record *Entry = Table.Entries[i]; 5570b57cec5SDimitry Andric OS << " { "; 5580b57cec5SDimitry Andric 559fe6060f1SDimitry Andric ListSeparator LS; 560fe6060f1SDimitry Andric for (const auto &Field : Table.Fields) 561fe6060f1SDimitry Andric OS << LS 562fe6060f1SDimitry Andric << primaryRepresentation(Table.Locs[0], Field, 563e8d8bef9SDimitry Andric Entry->getValueInit(Field.Name)); 5640b57cec5SDimitry Andric 5650b57cec5SDimitry Andric OS << " }, // " << i << "\n"; 5660b57cec5SDimitry Andric } 5670b57cec5SDimitry Andric OS << " };\n"; 5680b57cec5SDimitry Andric 5690b57cec5SDimitry Andric // Indexes are sorted "{ Thing, PrimaryIdx }" arrays, so that a binary 5700b57cec5SDimitry Andric // search can be performed by "Thing". 5710b57cec5SDimitry Andric if (Table.PrimaryKey) 572*0fca6ea1SDimitry Andric emitLookupFunction(Table, *Table.PrimaryKey, /*IsPrimary=*/true, OS); 5730b57cec5SDimitry Andric for (const auto &Index : Table.Indices) 574*0fca6ea1SDimitry Andric emitLookupFunction(Table, *Index, /*IsPrimary=*/false, OS); 5750b57cec5SDimitry Andric 5760b57cec5SDimitry Andric OS << "#endif\n\n"; 5770b57cec5SDimitry Andric } 5780b57cec5SDimitry Andric 579e8d8bef9SDimitry Andric bool SearchableTableEmitter::parseFieldType(GenericField &Field, Init *TypeOf) { 580e8d8bef9SDimitry Andric if (auto Type = dyn_cast<StringInit>(TypeOf)) { 581e8d8bef9SDimitry Andric if (Type->getValue() == "code") { 582e8d8bef9SDimitry Andric Field.IsCode = true; 583e8d8bef9SDimitry Andric return true; 584e8d8bef9SDimitry Andric } else { 585e8d8bef9SDimitry Andric if (Record *TypeRec = Records.getDef(Type->getValue())) { 5860b57cec5SDimitry Andric if (TypeRec->isSubClassOf("GenericEnum")) { 5870b57cec5SDimitry Andric Field.Enum = EnumMap[TypeRec]; 5880b57cec5SDimitry Andric Field.RecType = RecordRecTy::get(Field.Enum->Class); 5890b57cec5SDimitry Andric return true; 5900b57cec5SDimitry Andric } 5910b57cec5SDimitry Andric } 592e8d8bef9SDimitry Andric } 593e8d8bef9SDimitry Andric } 5940b57cec5SDimitry Andric 5950b57cec5SDimitry Andric return false; 5960b57cec5SDimitry Andric } 5970b57cec5SDimitry Andric 598e8d8bef9SDimitry Andric std::unique_ptr<SearchIndex> SearchableTableEmitter::parseSearchIndex( 599e8d8bef9SDimitry Andric GenericTable &Table, const RecordVal *KeyRecVal, StringRef Name, 600*0fca6ea1SDimitry Andric const std::vector<StringRef> &Key, bool EarlyOut, bool ReturnRange) { 6018bcb0991SDimitry Andric auto Index = std::make_unique<SearchIndex>(); 6025ffd83dbSDimitry Andric Index->Name = std::string(Name); 603e8d8bef9SDimitry Andric Index->Loc = KeyRecVal->getLoc(); 6040b57cec5SDimitry Andric Index->EarlyOut = EarlyOut; 605*0fca6ea1SDimitry Andric Index->ReturnRange = ReturnRange; 6060b57cec5SDimitry Andric 6070b57cec5SDimitry Andric for (const auto &FieldName : Key) { 6080b57cec5SDimitry Andric const GenericField *Field = Table.getFieldByName(FieldName); 6090b57cec5SDimitry Andric if (!Field) 610e8d8bef9SDimitry Andric PrintFatalError( 611e8d8bef9SDimitry Andric KeyRecVal, 612e8d8bef9SDimitry Andric Twine("In table '") + Table.Name + 613e8d8bef9SDimitry Andric "', 'PrimaryKey' or 'Key' refers to nonexistent field '" + 614e8d8bef9SDimitry Andric FieldName + "'"); 615e8d8bef9SDimitry Andric 6160b57cec5SDimitry Andric Index->Fields.push_back(*Field); 6170b57cec5SDimitry Andric } 6180b57cec5SDimitry Andric 6190b57cec5SDimitry Andric if (EarlyOut && isa<StringRecTy>(Index->Fields[0].RecType)) { 6200b57cec5SDimitry Andric PrintFatalError( 621e8d8bef9SDimitry Andric KeyRecVal, Twine("In lookup method '") + Name + "', early-out is not " + 622e8d8bef9SDimitry Andric "supported for a first key field of type string"); 6230b57cec5SDimitry Andric } 6240b57cec5SDimitry Andric 6250b57cec5SDimitry Andric return Index; 6260b57cec5SDimitry Andric } 6270b57cec5SDimitry Andric 6280b57cec5SDimitry Andric void SearchableTableEmitter::collectEnumEntries( 6290b57cec5SDimitry Andric GenericEnum &Enum, StringRef NameField, StringRef ValueField, 6300b57cec5SDimitry Andric const std::vector<Record *> &Items) { 631bdd1243dSDimitry Andric for (auto *EntryRec : Items) { 6320b57cec5SDimitry Andric StringRef Name; 6330b57cec5SDimitry Andric if (NameField.empty()) 6340b57cec5SDimitry Andric Name = EntryRec->getName(); 6350b57cec5SDimitry Andric else 6360b57cec5SDimitry Andric Name = EntryRec->getValueAsString(NameField); 6370b57cec5SDimitry Andric 6380b57cec5SDimitry Andric int64_t Value = 0; 6390b57cec5SDimitry Andric if (!ValueField.empty()) 6400b57cec5SDimitry Andric Value = getInt(EntryRec, ValueField); 6410b57cec5SDimitry Andric 6428bcb0991SDimitry Andric Enum.Entries.push_back(std::make_unique<GenericEnum::Entry>(Name, Value)); 643*0fca6ea1SDimitry Andric Enum.EntryMap.insert(std::pair(EntryRec, Enum.Entries.back().get())); 6440b57cec5SDimitry Andric } 6450b57cec5SDimitry Andric 6460b57cec5SDimitry Andric if (ValueField.empty()) { 647e8d8bef9SDimitry Andric llvm::stable_sort(Enum.Entries, 6480b57cec5SDimitry Andric [](const std::unique_ptr<GenericEnum::Entry> &LHS, 6490b57cec5SDimitry Andric const std::unique_ptr<GenericEnum::Entry> &RHS) { 6500b57cec5SDimitry Andric return LHS->first < RHS->first; 6510b57cec5SDimitry Andric }); 6520b57cec5SDimitry Andric 6530b57cec5SDimitry Andric for (size_t i = 0; i < Enum.Entries.size(); ++i) 6540b57cec5SDimitry Andric Enum.Entries[i]->second = i; 6550b57cec5SDimitry Andric } 6560b57cec5SDimitry Andric } 6570b57cec5SDimitry Andric 6580b57cec5SDimitry Andric void SearchableTableEmitter::collectTableEntries( 6590b57cec5SDimitry Andric GenericTable &Table, const std::vector<Record *> &Items) { 6605ffd83dbSDimitry Andric if (Items.empty()) 661e8d8bef9SDimitry Andric PrintFatalError(Table.Locs, 662e8d8bef9SDimitry Andric Twine("Table '") + Table.Name + "' has no entries"); 6635ffd83dbSDimitry Andric 664bdd1243dSDimitry Andric for (auto *EntryRec : Items) { 6650b57cec5SDimitry Andric for (auto &Field : Table.Fields) { 6660b57cec5SDimitry Andric auto TI = dyn_cast<TypedInit>(EntryRec->getValueInit(Field.Name)); 6675ffd83dbSDimitry Andric if (!TI || !TI->isComplete()) { 668e8d8bef9SDimitry Andric PrintFatalError(EntryRec, Twine("Record '") + EntryRec->getName() + 669e8d8bef9SDimitry Andric "' for table '" + Table.Name + 670e8d8bef9SDimitry Andric "' is missing field '" + Field.Name + 671e8d8bef9SDimitry Andric "'"); 6720b57cec5SDimitry Andric } 6730b57cec5SDimitry Andric if (!Field.RecType) { 6740b57cec5SDimitry Andric Field.RecType = TI->getType(); 6750b57cec5SDimitry Andric } else { 6760b57cec5SDimitry Andric RecTy *Ty = resolveTypes(Field.RecType, TI->getType()); 6770b57cec5SDimitry Andric if (!Ty) 678e8d8bef9SDimitry Andric PrintFatalError(EntryRec->getValue(Field.Name), 679e8d8bef9SDimitry Andric Twine("Field '") + Field.Name + "' of table '" + 680e8d8bef9SDimitry Andric Table.Name + "' entry has incompatible type: " + 681e8d8bef9SDimitry Andric TI->getType()->getAsString() + " vs. " + 682e8d8bef9SDimitry Andric Field.RecType->getAsString()); 6830b57cec5SDimitry Andric Field.RecType = Ty; 6840b57cec5SDimitry Andric } 6850b57cec5SDimitry Andric } 6860b57cec5SDimitry Andric 687e8d8bef9SDimitry Andric Table.Entries.push_back(EntryRec); // Add record to table's record list. 6880b57cec5SDimitry Andric } 6890b57cec5SDimitry Andric 6900b57cec5SDimitry Andric Record *IntrinsicClass = Records.getClass("Intrinsic"); 6910b57cec5SDimitry Andric Record *InstructionClass = Records.getClass("Instruction"); 6920b57cec5SDimitry Andric for (auto &Field : Table.Fields) { 6935ffd83dbSDimitry Andric if (!Field.RecType) 6945ffd83dbSDimitry Andric PrintFatalError(Twine("Cannot determine type of field '") + Field.Name + 6955ffd83dbSDimitry Andric "' in table '" + Table.Name + "'. Maybe it is not used?"); 6965ffd83dbSDimitry Andric 6970b57cec5SDimitry Andric if (auto RecordTy = dyn_cast<RecordRecTy>(Field.RecType)) { 6980b57cec5SDimitry Andric if (IntrinsicClass && RecordTy->isSubClassOf(IntrinsicClass)) 6990b57cec5SDimitry Andric Field.IsIntrinsic = true; 7000b57cec5SDimitry Andric else if (InstructionClass && RecordTy->isSubClassOf(InstructionClass)) 7010b57cec5SDimitry Andric Field.IsInstruction = true; 7020b57cec5SDimitry Andric } 7030b57cec5SDimitry Andric } 70404eeddc0SDimitry Andric 70504eeddc0SDimitry Andric SearchIndex Idx; 70604eeddc0SDimitry Andric std::copy(Table.Fields.begin(), Table.Fields.end(), 70704eeddc0SDimitry Andric std::back_inserter(Idx.Fields)); 708fcaf7f86SDimitry Andric llvm::sort(Table.Entries, [&](Record *LHS, Record *RHS) { 709fcaf7f86SDimitry Andric return compareBy(LHS, RHS, Idx); 710fcaf7f86SDimitry Andric }); 7110b57cec5SDimitry Andric } 7120b57cec5SDimitry Andric 7130b57cec5SDimitry Andric void SearchableTableEmitter::run(raw_ostream &OS) { 7140b57cec5SDimitry Andric // Emit tables in a deterministic order to avoid needless rebuilds. 7150b57cec5SDimitry Andric SmallVector<std::unique_ptr<GenericTable>, 4> Tables; 7160b57cec5SDimitry Andric DenseMap<Record *, GenericTable *> TableMap; 717*0fca6ea1SDimitry Andric if (!Records.getAllDerivedDefinitionsIfDefined("Instruction").empty()) 718*0fca6ea1SDimitry Andric Target = std::make_unique<CodeGenTarget>(Records); 7190b57cec5SDimitry Andric 7200b57cec5SDimitry Andric // Collect all definitions first. 721bdd1243dSDimitry Andric for (auto *EnumRec : Records.getAllDerivedDefinitions("GenericEnum")) { 7220b57cec5SDimitry Andric StringRef NameField; 7230b57cec5SDimitry Andric if (!EnumRec->isValueUnset("NameField")) 7240b57cec5SDimitry Andric NameField = EnumRec->getValueAsString("NameField"); 7250b57cec5SDimitry Andric 7260b57cec5SDimitry Andric StringRef ValueField; 7270b57cec5SDimitry Andric if (!EnumRec->isValueUnset("ValueField")) 7280b57cec5SDimitry Andric ValueField = EnumRec->getValueAsString("ValueField"); 7290b57cec5SDimitry Andric 7308bcb0991SDimitry Andric auto Enum = std::make_unique<GenericEnum>(); 7315ffd83dbSDimitry Andric Enum->Name = std::string(EnumRec->getName()); 7325ffd83dbSDimitry Andric Enum->PreprocessorGuard = std::string(EnumRec->getName()); 7330b57cec5SDimitry Andric 7340b57cec5SDimitry Andric StringRef FilterClass = EnumRec->getValueAsString("FilterClass"); 7350b57cec5SDimitry Andric Enum->Class = Records.getClass(FilterClass); 7360b57cec5SDimitry Andric if (!Enum->Class) 737e8d8bef9SDimitry Andric PrintFatalError(EnumRec->getValue("FilterClass"), 738e8d8bef9SDimitry Andric Twine("Enum FilterClass '") + FilterClass + 739e8d8bef9SDimitry Andric "' does not exist"); 7400b57cec5SDimitry Andric 7410b57cec5SDimitry Andric collectEnumEntries(*Enum, NameField, ValueField, 7420b57cec5SDimitry Andric Records.getAllDerivedDefinitions(FilterClass)); 743*0fca6ea1SDimitry Andric EnumMap.insert(std::pair(EnumRec, Enum.get())); 7440b57cec5SDimitry Andric Enums.emplace_back(std::move(Enum)); 7450b57cec5SDimitry Andric } 7460b57cec5SDimitry Andric 747bdd1243dSDimitry Andric for (auto *TableRec : Records.getAllDerivedDefinitions("GenericTable")) { 7488bcb0991SDimitry Andric auto Table = std::make_unique<GenericTable>(); 7495ffd83dbSDimitry Andric Table->Name = std::string(TableRec->getName()); 750e8d8bef9SDimitry Andric Table->Locs = TableRec->getLoc(); 7515ffd83dbSDimitry Andric Table->PreprocessorGuard = std::string(TableRec->getName()); 7525ffd83dbSDimitry Andric Table->CppTypeName = std::string(TableRec->getValueAsString("CppTypeName")); 7530b57cec5SDimitry Andric 7540b57cec5SDimitry Andric std::vector<StringRef> Fields = TableRec->getValueAsListOfStrings("Fields"); 7550b57cec5SDimitry Andric for (const auto &FieldName : Fields) { 756e8d8bef9SDimitry Andric Table->Fields.emplace_back(FieldName); // Construct a GenericField. 7570b57cec5SDimitry Andric 758*0fca6ea1SDimitry Andric if (auto TypeOfRecordVal = 759*0fca6ea1SDimitry Andric TableRec->getValue(("TypeOf_" + FieldName).str())) { 760*0fca6ea1SDimitry Andric if (!parseFieldType(Table->Fields.back(), 761*0fca6ea1SDimitry Andric TypeOfRecordVal->getValue())) { 762e8d8bef9SDimitry Andric PrintError(TypeOfRecordVal, 763*0fca6ea1SDimitry Andric Twine("Table '") + Table->Name + "' has invalid 'TypeOf_" + 764*0fca6ea1SDimitry Andric FieldName + 765e8d8bef9SDimitry Andric "': " + TypeOfRecordVal->getValue()->getAsString()); 766e8d8bef9SDimitry Andric PrintFatalNote("The 'TypeOf_xxx' field must be a string naming a " 767e8d8bef9SDimitry Andric "GenericEnum record, or \"code\""); 7680b57cec5SDimitry Andric } 7690b57cec5SDimitry Andric } 7700b57cec5SDimitry Andric } 7710b57cec5SDimitry Andric 772e8d8bef9SDimitry Andric StringRef FilterClass = TableRec->getValueAsString("FilterClass"); 773e8d8bef9SDimitry Andric if (!Records.getClass(FilterClass)) 774e8d8bef9SDimitry Andric PrintFatalError(TableRec->getValue("FilterClass"), 775*0fca6ea1SDimitry Andric Twine("Table FilterClass '") + FilterClass + 776*0fca6ea1SDimitry Andric "' does not exist"); 777e8d8bef9SDimitry Andric 7785f757f3fSDimitry Andric RecordVal *FilterClassFieldVal = TableRec->getValue("FilterClassField"); 7795f757f3fSDimitry Andric std::vector<Record *> Definitions = 7805f757f3fSDimitry Andric Records.getAllDerivedDefinitions(FilterClass); 7815f757f3fSDimitry Andric if (auto *FilterClassFieldInit = 7825f757f3fSDimitry Andric dyn_cast<StringInit>(FilterClassFieldVal->getValue())) { 7835f757f3fSDimitry Andric StringRef FilterClassField = FilterClassFieldInit->getValue(); 7845f757f3fSDimitry Andric llvm::erase_if(Definitions, [&](const Record *R) { 7855f757f3fSDimitry Andric const RecordVal *Filter = R->getValue(FilterClassField); 7865f757f3fSDimitry Andric if (auto *BitV = dyn_cast<BitInit>(Filter->getValue())) 7875f757f3fSDimitry Andric return !BitV->getValue(); 7885f757f3fSDimitry Andric 7895f757f3fSDimitry Andric PrintFatalError(Filter, Twine("FilterClassField '") + FilterClass + 7905f757f3fSDimitry Andric "' should be a bit value"); 7915f757f3fSDimitry Andric return true; 7925f757f3fSDimitry Andric }); 7935f757f3fSDimitry Andric } 7945f757f3fSDimitry Andric collectTableEntries(*Table, Definitions); 7950b57cec5SDimitry Andric 7960b57cec5SDimitry Andric if (!TableRec->isValueUnset("PrimaryKey")) { 7970b57cec5SDimitry Andric Table->PrimaryKey = 798e8d8bef9SDimitry Andric parseSearchIndex(*Table, TableRec->getValue("PrimaryKey"), 799e8d8bef9SDimitry Andric TableRec->getValueAsString("PrimaryKeyName"), 8000b57cec5SDimitry Andric TableRec->getValueAsListOfStrings("PrimaryKey"), 801*0fca6ea1SDimitry Andric TableRec->getValueAsBit("PrimaryKeyEarlyOut"), 802*0fca6ea1SDimitry Andric TableRec->getValueAsBit("PrimaryKeyReturnRange")); 8030b57cec5SDimitry Andric 804e8d8bef9SDimitry Andric llvm::stable_sort(Table->Entries, [&](Record *LHS, Record *RHS) { 8050b57cec5SDimitry Andric return compareBy(LHS, RHS, *Table->PrimaryKey); 8060b57cec5SDimitry Andric }); 8070b57cec5SDimitry Andric } 8080b57cec5SDimitry Andric 809*0fca6ea1SDimitry Andric TableMap.insert(std::pair(TableRec, Table.get())); 8100b57cec5SDimitry Andric Tables.emplace_back(std::move(Table)); 8110b57cec5SDimitry Andric } 8120b57cec5SDimitry Andric 8130b57cec5SDimitry Andric for (Record *IndexRec : Records.getAllDerivedDefinitions("SearchIndex")) { 8140b57cec5SDimitry Andric Record *TableRec = IndexRec->getValueAsDef("Table"); 8150b57cec5SDimitry Andric auto It = TableMap.find(TableRec); 8160b57cec5SDimitry Andric if (It == TableMap.end()) 817e8d8bef9SDimitry Andric PrintFatalError(IndexRec->getValue("Table"), 8180b57cec5SDimitry Andric Twine("SearchIndex '") + IndexRec->getName() + 819e8d8bef9SDimitry Andric "' refers to nonexistent table '" + 8200b57cec5SDimitry Andric TableRec->getName()); 8210b57cec5SDimitry Andric 8220b57cec5SDimitry Andric GenericTable &Table = *It->second; 823*0fca6ea1SDimitry Andric Table.Indices.push_back(parseSearchIndex( 824*0fca6ea1SDimitry Andric Table, IndexRec->getValue("Key"), IndexRec->getName(), 825e8d8bef9SDimitry Andric IndexRec->getValueAsListOfStrings("Key"), 826*0fca6ea1SDimitry Andric IndexRec->getValueAsBit("EarlyOut"), /*ReturnRange*/ false)); 8270b57cec5SDimitry Andric } 8280b57cec5SDimitry Andric 8290b57cec5SDimitry Andric // Translate legacy tables. 8300b57cec5SDimitry Andric Record *SearchableTable = Records.getClass("SearchableTable"); 8310b57cec5SDimitry Andric for (auto &NameRec : Records.getClasses()) { 8320b57cec5SDimitry Andric Record *Class = NameRec.second.get(); 8330b57cec5SDimitry Andric if (Class->getSuperClasses().size() != 1 || 8340b57cec5SDimitry Andric !Class->isSubClassOf(SearchableTable)) 8350b57cec5SDimitry Andric continue; 8360b57cec5SDimitry Andric 8370b57cec5SDimitry Andric StringRef TableName = Class->getName(); 8380b57cec5SDimitry Andric std::vector<Record *> Items = Records.getAllDerivedDefinitions(TableName); 8390b57cec5SDimitry Andric if (!Class->isValueUnset("EnumNameField")) { 8400b57cec5SDimitry Andric StringRef NameField = Class->getValueAsString("EnumNameField"); 8410b57cec5SDimitry Andric StringRef ValueField; 8420b57cec5SDimitry Andric if (!Class->isValueUnset("EnumValueField")) 8430b57cec5SDimitry Andric ValueField = Class->getValueAsString("EnumValueField"); 8440b57cec5SDimitry Andric 8458bcb0991SDimitry Andric auto Enum = std::make_unique<GenericEnum>(); 8460b57cec5SDimitry Andric Enum->Name = (Twine(Class->getName()) + "Values").str(); 8470b57cec5SDimitry Andric Enum->PreprocessorGuard = Class->getName().upper(); 8480b57cec5SDimitry Andric Enum->Class = Class; 8490b57cec5SDimitry Andric 8500b57cec5SDimitry Andric collectEnumEntries(*Enum, NameField, ValueField, Items); 8510b57cec5SDimitry Andric 8520b57cec5SDimitry Andric Enums.emplace_back(std::move(Enum)); 8530b57cec5SDimitry Andric } 8540b57cec5SDimitry Andric 8558bcb0991SDimitry Andric auto Table = std::make_unique<GenericTable>(); 8560b57cec5SDimitry Andric Table->Name = (Twine(Class->getName()) + "sList").str(); 857e8d8bef9SDimitry Andric Table->Locs = Class->getLoc(); 8580b57cec5SDimitry Andric Table->PreprocessorGuard = Class->getName().upper(); 8595ffd83dbSDimitry Andric Table->CppTypeName = std::string(Class->getName()); 8600b57cec5SDimitry Andric 8610b57cec5SDimitry Andric for (const RecordVal &Field : Class->getValues()) { 8625ffd83dbSDimitry Andric std::string FieldName = std::string(Field.getName()); 8630b57cec5SDimitry Andric 8640b57cec5SDimitry Andric // Skip uninteresting fields: either special to us, or injected 8650b57cec5SDimitry Andric // template parameters (if they contain a ':'). 8660b57cec5SDimitry Andric if (FieldName.find(':') != std::string::npos || 8670b57cec5SDimitry Andric FieldName == "SearchableFields" || FieldName == "EnumNameField" || 8680b57cec5SDimitry Andric FieldName == "EnumValueField") 8690b57cec5SDimitry Andric continue; 8700b57cec5SDimitry Andric 8710b57cec5SDimitry Andric Table->Fields.emplace_back(FieldName); 8720b57cec5SDimitry Andric } 8730b57cec5SDimitry Andric 8740b57cec5SDimitry Andric collectTableEntries(*Table, Items); 8750b57cec5SDimitry Andric 8760b57cec5SDimitry Andric for (const auto &Field : 8770b57cec5SDimitry Andric Class->getValueAsListOfStrings("SearchableFields")) { 8780b57cec5SDimitry Andric std::string Name = 8790b57cec5SDimitry Andric (Twine("lookup") + Table->CppTypeName + "By" + Field).str(); 880*0fca6ea1SDimitry Andric Table->Indices.push_back( 881*0fca6ea1SDimitry Andric parseSearchIndex(*Table, Class->getValue(Field), Name, {Field}, 882*0fca6ea1SDimitry Andric /*EarlyOut*/ false, /*ReturnRange*/ false)); 8830b57cec5SDimitry Andric } 8840b57cec5SDimitry Andric 8850b57cec5SDimitry Andric Tables.emplace_back(std::move(Table)); 8860b57cec5SDimitry Andric } 8870b57cec5SDimitry Andric 8880b57cec5SDimitry Andric // Emit everything. 8890b57cec5SDimitry Andric for (const auto &Enum : Enums) 8900b57cec5SDimitry Andric emitGenericEnum(*Enum, OS); 8910b57cec5SDimitry Andric 8920b57cec5SDimitry Andric for (const auto &Table : Tables) 8930b57cec5SDimitry Andric emitGenericTable(*Table, OS); 8940b57cec5SDimitry Andric 8950b57cec5SDimitry Andric // Put all #undefs last, to allow multiple sections guarded by the same 8960b57cec5SDimitry Andric // define. 8970b57cec5SDimitry Andric for (const auto &Guard : PreprocessorGuards) 8980b57cec5SDimitry Andric OS << "#undef " << Guard << "\n"; 8990b57cec5SDimitry Andric } 9000b57cec5SDimitry Andric 90106c3fb27SDimitry Andric static TableGen::Emitter::OptClass<SearchableTableEmitter> 90206c3fb27SDimitry Andric X("gen-searchable-tables", "Generate generic binary-searchable table"); 903