1 // RUN: llvm-tblgen -gen-searchable-tables -I %p/../../include %s | FileCheck %s
2 // RUN: not llvm-tblgen -gen-searchable-tables -I %p/../../include -DERROR1 %s 2>&1 | FileCheck --check-prefix=ERROR1 %s
5 include "llvm/TableGen/SearchableTable.td"
7 // CHECK-LABEL: GET_BValues_DECL
8 // CHECK: enum BValues {
9 // CHECK: BAlice = 172,
11 // CHECK: BCharlie = 128,
15 // CHECK-LABEL: GET_CEnum_DECL
16 // CHECK: enum CEnum {
22 // CHECK-LABEL: GET_ATable_DECL
23 // CHECK: const AEntry *lookupATableByValues(uint8_t Val1, uint16_t Val2);
25 // CHECK-LABEL: GET_ATable_IMPL
26 // CHECK: constexpr AEntry ATable[] = {
28 // CHECK: { "baz", 0x2, 0x6, 0xFFFFFFFF00000000 },
29 // CHECK: { "foo", 0x4, 0x4, 0x100000000 },
30 // CHECK: { "foobar", 0x4, 0x5, 0x100000000 },
31 // CHECK: { "bar", 0x5, 0x3, 0x100000000 },
34 // CHECK: const AEntry *lookupATableByValues(uint8_t Val1, uint16_t Val2) {
35 // CHECK: return &*Idx;
38 class AEntry<string str, int val1, int val2, bits<64> val3> {
46 def : AEntry<"aaa", 0, 0, 0> { let IsNeeded = 0; }
47 def : AEntry<"bar", 5, 3, 0x100000000>;
48 def : AEntry<"baz", 2, 6, 0xFFFFFFFF00000000>;
49 def : AEntry<"foo", 4, 4, 0b0000000000000000000000000000000100000000000000000000000000000000>;
50 def : AEntry<"foobar", 4, 5, 4294967296>;
52 def ATable : GenericTable {
53 let FilterClass = "AEntry";
54 let FilterClassField = "IsNeeded";
55 let Fields = ["Str", "Val1", "Val2", "Val3"];
57 let PrimaryKey = ["Val1", "Val2"];
58 let PrimaryKeyName = "lookupATableByValues";
62 // CHECK-LABEL: GET_BTable_IMPL
63 // CHECK: constexpr BTypeName BTable[] = {
64 // CHECK: { "BAlice", 0xAC, false, },
65 // CHECK: { "BBob", 0x14, false, Bob == 13 },
66 // CHECK: { "BCharlie", 0x80, true, Charlie == 42 },
67 // CHECK: { "BEve", 0x4C, true, Eve == 108 },
69 // CHECK: const BTypeName *lookupBTableByName(StringRef Name) {
70 // CHECK: return &BTable[Idx->_index];
72 // CHECK: const BTypeName *lookupBTableByNameAndFlag(StringRef Name, bool Flag) {
73 // CHECK: return &BTable[Idx->_index];
76 class BEntry<bits<16> enc, bit flag = 0, code test = [{}]> {
78 bits<16> Encoding = enc;
83 def BAlice : BEntry<0xac>;
84 def BBob : BEntry<0x14, 0, [{Bob == 13}]>;
85 def BCharlie : BEntry<0x80, 1, "Charlie == 42">;
86 def BEve : BEntry<0x4c, 1, [{Eve == }] # 108>;
88 def BValues : GenericEnum {
89 let FilterClass = "BEntry";
90 let NameField = "Name";
91 let ValueField = "Encoding";
94 def BTable : GenericTable {
95 let FilterClass = "BEntry";
96 string CppTypeName = "BTypeName";
97 let Fields = ["Name", "Encoding", "Flag", "Test"];
98 string TypeOf_Test = "code";
101 def lookupBTableByName : SearchIndex {
106 def lookupBTableByNameAndFlag : SearchIndex {
108 let Key = ["Name", "Flag"];
111 // CHECK-LABEL: GET_CTable_DECL
112 // CHECK: const CEntry *lookupCEntryByEncoding(uint16_t Encoding);
113 // CHECK: const CEntry *lookupCEntry(StringRef Name, unsigned Kind);
114 // CHECK-LABEL: GET_CTable_IMPL
115 // CHECK: const CEntry *lookupCEntryByEncoding(uint16_t Encoding) {
116 // CHECK: if ((Encoding < 0xA) ||
117 // CHECK: (Encoding > 0xF))
118 // CHECK: return nullptr;
120 // CHECK: const CEntry *lookupCEntry(StringRef Name, unsigned Kind) {
121 // CHECK: Index[] = {
122 // CHECK: { "ALICE", CBar, 1 },
123 // CHECK: { "ALICE", CFoo, 0 },
124 // CHECK: { "BOB", CBaz, 2 },
132 def CEnum : GenericEnum {
133 let FilterClass = "CEnum";
136 class CEntry<string name, CEnum kind, int enc> {
139 bits<16> Encoding = enc;
142 def : CEntry<"alice", CFoo, 10>;
143 def : CEntry<"alice", CBar, 13>;
144 def : CEntry<"bob", CBaz, 15>;
146 def CTable : GenericTable {
147 let FilterClass = "CEntry";
148 let Fields = ["Name", "Kind", "Encoding"];
150 string TypeOf_Kind = "CEnum";
152 let PrimaryKey = ["Encoding"];
153 let PrimaryKeyName = "lookupCEntryByEncoding";
154 let PrimaryKeyEarlyOut = 1;
157 def lookupCEntry : SearchIndex {
159 let Key = ["Name", "Kind"];
164 class DEntry<string str, int val1> {
169 def DFoo : DEntry<"foo", 1>;
170 // ERROR1: [[@LINE+1]]:5: error: Record 'DBar' for table 'DTable' is missing field 'Val1'
171 def DBar : DEntry<"bar", ?>;
173 def DTable : GenericTable {
174 let FilterClass = "DEntry";
175 let Fields = ["Str", "Val1"];
180 // CHECK-LABEL: GET_EEntryEvenTable_DECL
181 // CHECK: const EEntry *lookupEEntryEvenTableByValue(uint8_t Value);
183 // CHECK-LABEL: GET_EEntryEvenTable_IMPL
184 // CHECK: constexpr EEntry EEntryEvenTable[] = {
192 // CHECK: const EEntry *lookupEEntryEvenTableByValue(uint8_t Value) {
193 // CHECK: return &*Idx;
196 // CHECK-LABEL: GET_EEntryOddTable_DECL
197 // CHECK: const EEntry *lookupEEntryOddTableByValue(uint8_t Value);
199 // CHECK-LABEL: GET_EEntryOddTable_IMPL
200 // CHECK: constexpr EEntry EEntryOddTable[] = {
208 // CHECK: const EEntry *lookupEEntryOddTableByValue(uint8_t Value) {
209 // CHECK: return &*Idx;
212 // We can construct two GenericTables with the same FilterClass, so that they
213 // select from the same overall set of records, but assign them with different
214 // FilterClassField values so that they include different subsets of the records
216 class EEntry<bits<8> value> {
217 bits<8> Value = value;
218 bit IsEven = !eq(!and(value, 1), 0);
219 bit IsOdd = !not(IsEven);
222 foreach i = {1-10} in {
226 def EEntryEvenTable : GenericTable {
227 let FilterClass = "EEntry";
228 let FilterClassField = "IsEven";
229 let Fields = ["Value"];
230 let PrimaryKey = ["Value"];
231 let PrimaryKeyName = "lookupEEntryEvenTableByValue";
234 def EEntryOddTable : GenericTable {
235 let FilterClass = "EEntry";
236 let FilterClassField = "IsOdd";
237 let Fields = ["Value"];
238 let PrimaryKey = ["Value"];
239 let PrimaryKeyName = "lookupEEntryOddTableByValue";