blob: 61dfa5c7070646be8a10b1e6e9203b8b0398eae3 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- SearchableTable.td ----------------------------------*- tablegen -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the key top-level classes needed to produce a reasonably
Andrew Scullcdfcccc2018-10-05 20:58:37 +010010// generic table that can be binary-searched. Three types of objects can be
11// defined using the classes in this file:
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010012//
Andrew Scullcdfcccc2018-10-05 20:58:37 +010013// 1. (Generic) Enums. By instantiating the GenericEnum class once, an enum with
14// the name of the def is generated. It is guarded by the preprocessor define
15// GET_name_DECL, where name is the name of the def.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010016//
Andrew Scullcdfcccc2018-10-05 20:58:37 +010017// 2. (Generic) Tables and search indices. By instantiating the GenericTable
18// class once, a table with the name of the instantiating def is generated and
19// guarded by the GET_name_IMPL preprocessor guard.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010020//
Andrew Scullcdfcccc2018-10-05 20:58:37 +010021// Both a primary key and additional secondary keys / search indices can also
22// be defined, which result in the generation of lookup functions. Their
23// declarations and definitions are all guarded by GET_name_DECL and
24// GET_name_IMPL, respectively, where name is the name of the underlying table.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010025//
26// See AArch64SystemOperands.td and its generated header for example uses.
27//
28//===----------------------------------------------------------------------===//
29
Andrew Scullcdfcccc2018-10-05 20:58:37 +010030// Define a record derived from this class to generate a generic enum.
31//
32// The name of the record is used as the type name of the C++ enum.
33class GenericEnum {
34 // Name of a TableGen class. The enum will have one entry for each record
35 // that derives from that class.
36 string FilterClass;
37
38 // (Optional) Name of a field that is present in all collected records and
39 // contains the name of enum entries.
40 //
41 // If NameField is not set, the record names will be used instead.
42 string NameField;
43
44 // (Optional) Name of a field that is present in all collected records and
45 // contains the numerical value of enum entries.
46 //
47 // If ValueField is not set, enum values will be assigned automatically,
48 // starting at 0, according to a lexicographical sort of the entry names.
49 string ValueField;
50}
51
52// Define a record derived from this class to generate a generic table. This
53// table can have a searchable primary key, and it can also be referenced by
54// external search indices.
55//
56// The name of the record is used as the name of the global primary array of
57// entries of the table in C++.
58class GenericTable {
59 // Name of a class. The table will have one entry for each record that
60 // derives from that class.
61 string FilterClass;
62
63 // Name of the C++ struct/class type that holds table entries. The
64 // declaration of this type is not generated automatically.
65 string CppTypeName = FilterClass;
66
67 // List of the names of fields of collected records that contain the data for
68 // table entries, in the order that is used for initialization in C++.
69 //
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020070 // TableGen needs to know the type of the fields so that it can format
71 // the initializers correctly. It can infer the type of bit, bits, string,
72 // Intrinsic, and Instruction values.
73 //
74 // For each field of the table named xxx, TableGen will look for a field
75 // named TypeOf_xxx and use that as a more detailed description of the
76 // type of the field. This is required for fields whose type
Andrew Scullcdfcccc2018-10-05 20:58:37 +010077 // cannot be deduced automatically, such as enum fields. For example:
78 //
79 // def MyEnum : GenericEnum {
80 // let FilterClass = "MyEnum";
81 // ...
82 // }
83 //
84 // class MyTableEntry {
85 // MyEnum V;
86 // ...
87 // }
88 //
89 // def MyTable : GenericTable {
90 // let FilterClass = "MyTableEntry";
91 // let Fields = ["V", ...];
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020092 // string TypeOf_V = "MyEnum";
Andrew Scullcdfcccc2018-10-05 20:58:37 +010093 // }
94 //
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020095 // If a string field was initialized with a code literal, TableGen will
96 // emit the code verbatim. However, if a string field was initialized
97 // in some other way, but should be interpreted as code, then a TypeOf_xxx
98 // field is necessary, with a value of "code":
Andrew Scullcdfcccc2018-10-05 20:58:37 +010099 //
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200100 // string TypeOf_Predicate = "code";
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100101 list<string> Fields;
102
103 // (Optional) List of fields that make up the primary key.
104 list<string> PrimaryKey;
105
106 // (Optional) Name of the primary key search function.
107 string PrimaryKeyName;
108
109 // See SearchIndex.EarlyOut
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200110 bit PrimaryKeyEarlyOut = false;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100111}
112
113// Define a record derived from this class to generate an additional search
114// index for a generic table that has been defined earlier.
115//
116// The name of the record will be used as the name of the C++ lookup function.
117class SearchIndex {
118 // Table that this search index refers to.
119 GenericTable Table;
120
121 // List of fields that make up the key.
122 list<string> Key;
123
124 // If true, the lookup function will check the first field of the key against
125 // the minimum and maximum values in the index before entering the binary
126 // search. This is convenient for tables that add extended data for a subset
127 // of a larger enum-based space, e.g. extended data about a subset of
128 // instructions.
129 //
130 // Can only be used when the first field is an integral (non-string) type.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200131 bit EarlyOut = false;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100132}
133
134// Legacy table type with integrated enum.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100135class SearchableTable {
136 list<string> SearchableFields;
137 string EnumNameField = "Name";
138 string EnumValueField;
139}