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[] = {
34 // CHECK: const AEntry *lookupATableByValues(uint8_t Val1, uint16_t Val2) {
35 // CHECK: return &*Idx;
38 class AEntry<string str, int val1, int val2> {
45 def : AEntry<"aaa", 0, 0> { let IsNeeded = 0; }
46 def : AEntry<"bar", 5, 3>;
47 def : AEntry<"baz", 2, 6>;
48 def : AEntry<"foo", 4, 4>;
49 def : AEntry<"foobar", 4, 5>;
51 def ATable : GenericTable {
52 let FilterClass = "AEntry";
53 let FilterClassField = "IsNeeded";
54 let Fields = ["Str", "Val1", "Val2"];
56 let PrimaryKey = ["Val1", "Val2"];
57 let PrimaryKeyName = "lookupATableByValues";
61 // CHECK-LABEL: GET_BTable_IMPL
62 // CHECK: constexpr BTypeName BTable[] = {
63 // CHECK: { "BAlice", 0xAC, false, },
64 // CHECK: { "BBob", 0x14, false, Bob == 13 },
65 // CHECK: { "BCharlie", 0x80, true, Charlie == 42 },
66 // CHECK: { "BEve", 0x4C, true, Eve == 108 },
68 // CHECK: const BTypeName *lookupBTableByName(StringRef Name) {
69 // CHECK: return &BTable[Idx->_index];
71 // CHECK: const BTypeName *lookupBTableByNameAndFlag(StringRef Name, bool Flag) {
72 // CHECK: return &BTable[Idx->_index];
75 class BEntry<bits<16> enc, bit flag = 0, code test = [{}]> {
77 bits<16> Encoding = enc;
82 def BAlice : BEntry<0xac>;
83 def BBob : BEntry<0x14, 0, [{Bob == 13}]>;
84 def BCharlie : BEntry<0x80, 1, "Charlie == 42">;
85 def BEve : BEntry<0x4c, 1, [{Eve == }] # 108>;
87 def BValues : GenericEnum {
88 let FilterClass = "BEntry";
89 let NameField = "Name";
90 let ValueField = "Encoding";
93 def BTable : GenericTable {
94 let FilterClass = "BEntry";
95 string CppTypeName = "BTypeName";
96 let Fields = ["Name", "Encoding", "Flag", "Test"];
97 string TypeOf_Test = "code";
100 def lookupBTableByName : SearchIndex {
105 def lookupBTableByNameAndFlag : SearchIndex {
107 let Key = ["Name", "Flag"];
110 // CHECK-LABEL: GET_CTable_DECL
111 // CHECK: const CEntry *lookupCEntryByEncoding(uint16_t Encoding);
112 // CHECK: const CEntry *lookupCEntry(StringRef Name, unsigned Kind);
113 // CHECK-LABEL: GET_CTable_IMPL
114 // CHECK: const CEntry *lookupCEntryByEncoding(uint16_t Encoding) {
115 // CHECK: if ((Encoding < 0xA) ||
116 // CHECK: (Encoding > 0xF))
117 // CHECK: return nullptr;
119 // CHECK: const CEntry *lookupCEntry(StringRef Name, unsigned Kind) {
120 // CHECK: Index[] = {
121 // CHECK: { "ALICE", CBar, 1 },
122 // CHECK: { "ALICE", CFoo, 0 },
123 // CHECK: { "BOB", CBaz, 2 },
131 def CEnum : GenericEnum {
132 let FilterClass = "CEnum";
135 class CEntry<string name, CEnum kind, int enc> {
138 bits<16> Encoding = enc;
141 def : CEntry<"alice", CFoo, 10>;
142 def : CEntry<"alice", CBar, 13>;
143 def : CEntry<"bob", CBaz, 15>;
145 def CTable : GenericTable {
146 let FilterClass = "CEntry";
147 let Fields = ["Name", "Kind", "Encoding"];
149 string TypeOf_Kind = "CEnum";
151 let PrimaryKey = ["Encoding"];
152 let PrimaryKeyName = "lookupCEntryByEncoding";
153 let PrimaryKeyEarlyOut = 1;
156 def lookupCEntry : SearchIndex {
158 let Key = ["Name", "Kind"];
163 class DEntry<string str, int val1> {
168 def DFoo : DEntry<"foo", 1>;
169 // ERROR1: [[@LINE+1]]:5: error: Record 'DBar' for table 'DTable' is missing field 'Val1'
170 def DBar : DEntry<"bar", ?>;
172 def DTable : GenericTable {
173 let FilterClass = "DEntry";
174 let Fields = ["Str", "Val1"];
179 // CHECK-LABEL: GET_EEntryEvenTable_DECL
180 // CHECK: const EEntry *lookupEEntryEvenTableByValue(uint8_t Value);
182 // CHECK-LABEL: GET_EEntryEvenTable_IMPL
183 // CHECK: constexpr EEntry EEntryEvenTable[] = {
191 // CHECK: const EEntry *lookupEEntryEvenTableByValue(uint8_t Value) {
192 // CHECK: return &*Idx;
195 // CHECK-LABEL: GET_EEntryOddTable_DECL
196 // CHECK: const EEntry *lookupEEntryOddTableByValue(uint8_t Value);
198 // CHECK-LABEL: GET_EEntryOddTable_IMPL
199 // CHECK: constexpr EEntry EEntryOddTable[] = {
207 // CHECK: const EEntry *lookupEEntryOddTableByValue(uint8_t Value) {
208 // CHECK: return &*Idx;
211 // We can construct two GenericTables with the same FilterClass, so that they
212 // select from the same overall set of records, but assign them with different
213 // FilterClassField values so that they include different subsets of the records
215 class EEntry<bits<8> value> {
216 bits<8> Value = value;
217 bit IsEven = !eq(!and(value, 1), 0);
218 bit IsOdd = !not(IsEven);
221 foreach i = {1-10} in {
225 def EEntryEvenTable : GenericTable {
226 let FilterClass = "EEntry";
227 let FilterClassField = "IsEven";
228 let Fields = ["Value"];
229 let PrimaryKey = ["Value"];
230 let PrimaryKeyName = "lookupEEntryEvenTableByValue";
233 def EEntryOddTable : GenericTable {
234 let FilterClass = "EEntry";
235 let FilterClassField = "IsOdd";
236 let Fields = ["Value"];
237 let PrimaryKey = ["Value"];
238 let PrimaryKeyName = "lookupEEntryOddTableByValue";