1e1a4e71a1fdfab6f7aaaec44aade56411b3555a
[platform/upstream/llvm.git] / llvm / docs / TableGen / BackEnds.rst
1 =================
2 TableGen BackEnds
3 =================
4
5 .. contents::
6    :local:
7
8 Introduction
9 ============
10
11 TableGen backends are at the core of TableGen's functionality. The source
12 files provide the classes and records that are parsed and end up as a
13 collection of record instances, but it's up to the backend to interpret and
14 print the records in a way that is meaningful to the user (normally a C++
15 include file or a textual list of warnings, options, and error messages).
16
17 TableGen is used by both LLVM, Clang, and MLIR with very different goals.
18 LLVM uses it as a way to automate the generation of massive amounts of
19 information regarding instructions, schedules, cores, and architecture
20 features. Some backends generate output that is consumed by more than one
21 source file, so they need to be created in a way that makes it is easy for
22 preprocessor tricks to be used. Some backends can also print C++ code
23 structures, so that they can be directly included as-is.
24
25 Clang, on the other hand, uses it mainly for diagnostic messages (errors,
26 warnings, tips) and attributes, so more on the textual end of the scale.
27
28 MLIR uses TableGen to define operations, operation dialects, and operation
29 traits.
30
31 See the :doc:`TableGen Programmer's Reference <./ProgRef>` for an in-depth
32 description of TableGen, and the :doc:`TableGen Backend Developer's Guide
33 <./BackGuide>` for a guide to writing a new backend.
34
35 LLVM BackEnds
36 =============
37
38 .. warning::
39    This portion is incomplete. Each section below needs three subsections:
40    description of its purpose with a list of users, output generated from
41    generic input, and finally why it needed a new backend (in case there's
42    something similar).
43
44 Overall, each backend will take the same TableGen file type and transform into
45 similar output for different targets/uses. There is an implicit contract between
46 the TableGen files, the back-ends and their users.
47
48 For instance, a global contract is that each back-end produces macro-guarded
49 sections. Based on whether the file is included by a header or a source file,
50 or even in which context of each file the include is being used, you have
51 todefine a macro just before including it, to get the right output:
52
53 .. code-block:: c++
54
55   #define GET_REGINFO_TARGET_DESC
56   #include "ARMGenRegisterInfo.inc"
57
58 And just part of the generated file would be included. This is useful if
59 you need the same information in multiple formats (instantiation, initialization,
60 getter/setter functions, etc) from the same source TableGen file without having
61 to re-compile the TableGen file multiple times.
62
63 Sometimes, multiple macros might be defined before the same include file to
64 output multiple blocks:
65
66 .. code-block:: c++
67
68   #define GET_REGISTER_MATCHER
69   #define GET_SUBTARGET_FEATURE_NAME
70   #define GET_MATCHER_IMPLEMENTATION
71   #include "ARMGenAsmMatcher.inc"
72
73 The macros will be undef'd automatically as they're used, in the include file.
74
75 On all LLVM back-ends, the ``llvm-tblgen`` binary will be executed on the root
76 TableGen file ``<Target>.td``, which should include all others. This guarantees
77 that all information needed is accessible, and that no duplication is needed
78 in the TableGen files.
79
80 CodeEmitter
81 -----------
82
83 **Purpose**: CodeEmitterGen uses the descriptions of instructions and their fields to
84 construct an automated code emitter: a function that, given a MachineInstr,
85 returns the (currently, 32-bit unsigned) value of the instruction.
86
87 **Output**: C++ code, implementing the target's CodeEmitter
88 class by overriding the virtual functions as ``<Target>CodeEmitter::function()``.
89
90 **Usage**: Used to include directly at the end of ``<Target>MCCodeEmitter.cpp``.
91
92 RegisterInfo
93 ------------
94
95 **Purpose**: This tablegen backend is responsible for emitting a description of a target
96 register file for a code generator.  It uses instances of the Register,
97 RegisterAliases, and RegisterClass classes to gather this information.
98
99 **Output**: C++ code with enums and structures representing the register mappings,
100 properties, masks, etc.
101
102 **Usage**: Both on ``<Target>BaseRegisterInfo`` and ``<Target>MCTargetDesc`` (headers
103 and source files) with macros defining in which they are for declaration vs.
104 initialization issues.
105
106 InstrInfo
107 ---------
108
109 **Purpose**: This tablegen backend is responsible for emitting a description of the target
110 instruction set for the code generator. (what are the differences from CodeEmitter?)
111
112 **Output**: C++ code with enums and structures representing the instruction mappings,
113 properties, masks, etc.
114
115 **Usage**: Both on ``<Target>BaseInstrInfo`` and ``<Target>MCTargetDesc`` (headers
116 and source files) with macros defining in which they are for declaration vs.
117 initialization issues.
118
119 AsmWriter
120 ---------
121
122 **Purpose**: Emits an assembly printer for the current target.
123
124 **Output**: Implementation of ``<Target>InstPrinter::printInstruction()``, among
125 other things.
126
127 **Usage**: Included directly into ``InstPrinter/<Target>InstPrinter.cpp``.
128
129 AsmMatcher
130 ----------
131
132 **Purpose**: Emits a target specifier matcher for
133 converting parsed assembly operands in the MCInst structures. It also
134 emits a matcher for custom operand parsing. Extensive documentation is
135 written on the ``AsmMatcherEmitter.cpp`` file.
136
137 **Output**: Assembler parsers' matcher functions, declarations, etc.
138
139 **Usage**: Used in back-ends' ``AsmParser/<Target>AsmParser.cpp`` for
140 building the AsmParser class.
141
142 Disassembler
143 ------------
144
145 **Purpose**: Contains disassembler table emitters for various
146 architectures. Extensive documentation is written on the
147 ``DisassemblerEmitter.cpp`` file.
148
149 **Output**: Decoding tables, static decoding functions, etc.
150
151 **Usage**: Directly included in ``Disassembler/<Target>Disassembler.cpp``
152 to cater for all default decodings, after all hand-made ones.
153
154 PseudoLowering
155 --------------
156
157 **Purpose**: Generate pseudo instruction lowering.
158
159 **Output**: Implements ``<Target>AsmPrinter::emitPseudoExpansionLowering()``.
160
161 **Usage**: Included directly into ``<Target>AsmPrinter.cpp``.
162
163 CallingConv
164 -----------
165
166 **Purpose**: Responsible for emitting descriptions of the calling
167 conventions supported by this target.
168
169 **Output**: Implement static functions to deal with calling conventions
170 chained by matching styles, returning false on no match.
171
172 **Usage**: Used in ISelLowering and FastIsel as function pointers to
173 implementation returned by a CC selection function.
174
175 DAGISel
176 -------
177
178 **Purpose**: Generate a DAG instruction selector.
179
180 **Output**: Creates huge functions for automating DAG selection.
181
182 **Usage**: Included in ``<Target>ISelDAGToDAG.cpp`` inside the target's
183 implementation of ``SelectionDAGISel``.
184
185 DFAPacketizer
186 -------------
187
188 **Purpose**: This class parses the Schedule.td file and produces an API that
189 can be used to reason about whether an instruction can be added to a packet
190 on a VLIW architecture. The class internally generates a deterministic finite
191 automaton (DFA) that models all possible mappings of machine instructions
192 to functional units as instructions are added to a packet.
193
194 **Output**: Scheduling tables for GPU back-ends (Hexagon, AMD).
195
196 **Usage**: Included directly on ``<Target>InstrInfo.cpp``.
197
198 FastISel
199 --------
200
201 **Purpose**: This tablegen backend emits code for use by the "fast"
202 instruction selection algorithm. See the comments at the top of
203 lib/CodeGen/SelectionDAG/FastISel.cpp for background. This file
204 scans through the target's tablegen instruction-info files
205 and extracts instructions with obvious-looking patterns, and it emits
206 code to look up these instructions by type and operator.
207
208 **Output**: Generates ``Predicate`` and ``FastEmit`` methods.
209
210 **Usage**: Implements private methods of the targets' implementation
211 of ``FastISel`` class.
212
213 Subtarget
214 ---------
215
216 **Purpose**: Generate subtarget enumerations.
217
218 **Output**: Enums, globals, local tables for sub-target information.
219
220 **Usage**: Populates ``<Target>Subtarget`` and
221 ``MCTargetDesc/<Target>MCTargetDesc`` files (both headers and source).
222
223 Intrinsic
224 ---------
225
226 **Purpose**: Generate (target) intrinsic information.
227
228 OptParserDefs
229 -------------
230
231 **Purpose**: Print enum values for a class.
232
233 SearchableTables
234 ----------------
235
236 **Purpose**: Generate custom searchable tables.
237
238 **Output**: Enums, global tables, and lookup helper functions.
239
240 **Usage**: This backend allows generating free-form, target-specific tables
241 from TableGen records. The ARM and AArch64 targets use this backend to generate
242 tables of system registers; the AMDGPU target uses it to generate meta-data
243 about complex image and memory buffer instructions.
244
245 See `SearchableTables Reference`_ for a detailed description.
246
247 CTags
248 -----
249
250 **Purpose**: This tablegen backend emits an index of definitions in ctags(1)
251 format. A helper script, utils/TableGen/tdtags, provides an easier-to-use
252 interface; run 'tdtags -H' for documentation.
253
254 X86EVEX2VEX
255 -----------
256
257 **Purpose**: This X86 specific tablegen backend emits tables that map EVEX
258 encoded instructions to their VEX encoded identical instruction.
259
260 Clang BackEnds
261 ==============
262
263 ClangAttrClasses
264 ----------------
265
266 **Purpose**: Creates Attrs.inc, which contains semantic attribute class
267 declarations for any attribute in ``Attr.td`` that has not set ``ASTNode = 0``.
268 This file is included as part of ``Attr.h``.
269
270 ClangAttrParserStringSwitches
271 -----------------------------
272
273 **Purpose**: Creates AttrParserStringSwitches.inc, which contains
274 StringSwitch::Case statements for parser-related string switches. Each switch
275 is given its own macro (such as ``CLANG_ATTR_ARG_CONTEXT_LIST``, or
276 ``CLANG_ATTR_IDENTIFIER_ARG_LIST``), which is expected to be defined before
277 including AttrParserStringSwitches.inc, and undefined after.
278
279 ClangAttrImpl
280 -------------
281
282 **Purpose**: Creates AttrImpl.inc, which contains semantic attribute class
283 definitions for any attribute in ``Attr.td`` that has not set ``ASTNode = 0``.
284 This file is included as part of ``AttrImpl.cpp``.
285
286 ClangAttrList
287 -------------
288
289 **Purpose**: Creates AttrList.inc, which is used when a list of semantic
290 attribute identifiers is required. For instance, ``AttrKinds.h`` includes this
291 file to generate the list of ``attr::Kind`` enumeration values. This list is
292 separated out into multiple categories: attributes, inheritable attributes, and
293 inheritable parameter attributes. This categorization happens automatically
294 based on information in ``Attr.td`` and is used to implement the ``classof``
295 functionality required for ``dyn_cast`` and similar APIs.
296
297 ClangAttrPCHRead
298 ----------------
299
300 **Purpose**: Creates AttrPCHRead.inc, which is used to deserialize attributes
301 in the ``ASTReader::ReadAttributes`` function.
302
303 ClangAttrPCHWrite
304 -----------------
305
306 **Purpose**: Creates AttrPCHWrite.inc, which is used to serialize attributes in
307 the ``ASTWriter::WriteAttributes`` function.
308
309 ClangAttrSpellings
310 ---------------------
311
312 **Purpose**: Creates AttrSpellings.inc, which is used to implement the
313 ``__has_attribute`` feature test macro.
314
315 ClangAttrSpellingListIndex
316 --------------------------
317
318 **Purpose**: Creates AttrSpellingListIndex.inc, which is used to map parsed
319 attribute spellings (including which syntax or scope was used) to an attribute
320 spelling list index. These spelling list index values are internal
321 implementation details exposed via
322 ``AttributeList::getAttributeSpellingListIndex``.
323
324 ClangAttrVisitor
325 -------------------
326
327 **Purpose**: Creates AttrVisitor.inc, which is used when implementing 
328 recursive AST visitors.
329
330 ClangAttrTemplateInstantiate
331 ----------------------------
332
333 **Purpose**: Creates AttrTemplateInstantiate.inc, which implements the
334 ``instantiateTemplateAttribute`` function, used when instantiating a template
335 that requires an attribute to be cloned.
336
337 ClangAttrParsedAttrList
338 -----------------------
339
340 **Purpose**: Creates AttrParsedAttrList.inc, which is used to generate the
341 ``AttributeList::Kind`` parsed attribute enumeration.
342
343 ClangAttrParsedAttrImpl
344 -----------------------
345
346 **Purpose**: Creates AttrParsedAttrImpl.inc, which is used by
347 ``AttributeList.cpp`` to implement several functions on the ``AttributeList``
348 class. This functionality is implemented via the ``AttrInfoMap ParsedAttrInfo``
349 array, which contains one element per parsed attribute object.
350
351 ClangAttrParsedAttrKinds
352 ------------------------
353
354 **Purpose**: Creates AttrParsedAttrKinds.inc, which is used to implement the
355 ``AttributeList::getKind`` function, mapping a string (and syntax) to a parsed
356 attribute ``AttributeList::Kind`` enumeration.
357
358 ClangAttrDump
359 -------------
360
361 **Purpose**: Creates AttrDump.inc, which dumps information about an attribute.
362 It is used to implement ``ASTDumper::dumpAttr``.
363
364 ClangDiagsDefs
365 --------------
366
367 Generate Clang diagnostics definitions.
368
369 ClangDiagGroups
370 ---------------
371
372 Generate Clang diagnostic groups.
373
374 ClangDiagsIndexName
375 -------------------
376
377 Generate Clang diagnostic name index.
378
379 ClangCommentNodes
380 -----------------
381
382 Generate Clang AST comment nodes.
383
384 ClangDeclNodes
385 --------------
386
387 Generate Clang AST declaration nodes.
388
389 ClangStmtNodes
390 --------------
391
392 Generate Clang AST statement nodes.
393
394 ClangSACheckers
395 ---------------
396
397 Generate Clang Static Analyzer checkers.
398
399 ClangCommentHTMLTags
400 --------------------
401
402 Generate efficient matchers for HTML tag names that are used in documentation comments.
403
404 ClangCommentHTMLTagsProperties
405 ------------------------------
406
407 Generate efficient matchers for HTML tag properties.
408
409 ClangCommentHTMLNamedCharacterReferences
410 ----------------------------------------
411
412 Generate function to translate named character references to UTF-8 sequences.
413
414 ClangCommentCommandInfo
415 -----------------------
416
417 Generate command properties for commands that are used in documentation comments.
418
419 ClangCommentCommandList
420 -----------------------
421
422 Generate list of commands that are used in documentation comments.
423
424 ArmNeon
425 -------
426
427 Generate arm_neon.h for clang.
428
429 ArmNeonSema
430 -----------
431
432 Generate ARM NEON sema support for clang.
433
434 ArmNeonTest
435 -----------
436
437 Generate ARM NEON tests for clang.
438
439 AttrDocs
440 --------
441
442 **Purpose**: Creates ``AttributeReference.rst`` from ``AttrDocs.td``, and is
443 used for documenting user-facing attributes.
444
445 General BackEnds
446 ================
447
448 Print Records
449 -------------
450
451 The TableGen command option ``--print-records`` invokes a simple backend
452 that prints all the classes and records defined in the source files. This is
453 the default backend option. See the :doc:`TableGen Backend Developer's Guide
454 <./BackGuide>` for more information.
455
456 Print Detailed Records
457 ----------------------
458
459 The TableGen command option ``--print-detailed-records`` invokes a backend
460 that prints all the global variables, classes, and records defined in the
461 source files, with more detail than the default record printer. See the
462 :doc:`TableGen Backend Developer's Guide <./BackGuide>` for more
463 information.
464
465 JSON Reference
466 --------------
467
468 **Purpose**: Output all the values in every ``def``, as a JSON data
469 structure that can be easily parsed by a variety of languages. Useful
470 for writing custom backends without having to modify TableGen itself,
471 or for performing auxiliary analysis on the same TableGen data passed
472 to a built-in backend.
473
474 **Output**:
475
476 The root of the output file is a JSON object (i.e. dictionary),
477 containing the following fixed keys:
478
479 * ``!tablegen_json_version``: a numeric version field that will
480   increase if an incompatible change is ever made to the structure of
481   this data. The format described here corresponds to version 1.
482
483 * ``!instanceof``: a dictionary whose keys are the class names defined
484   in the TableGen input. For each key, the corresponding value is an
485   array of strings giving the names of ``def`` records that derive
486   from that class. So ``root["!instanceof"]["Instruction"]``, for
487   example, would list the names of all the records deriving from the
488   class ``Instruction``.
489
490 For each ``def`` record, the root object also has a key for the record
491 name. The corresponding value is a subsidiary object containing the
492 following fixed keys:
493
494 * ``!superclasses``: an array of strings giving the names of all the
495   classes that this record derives from.
496
497 * ``!fields``: an array of strings giving the names of all the variables
498   in this record that were defined with the ``field`` keyword.
499
500 * ``!name``: a string giving the name of the record. This is always
501   identical to the key in the JSON root object corresponding to this
502   record's dictionary. (If the record is anonymous, the name is
503   arbitrary.)
504
505 * ``!anonymous``: a boolean indicating whether the record's name was
506   specified by the TableGen input (if it is ``false``), or invented by
507   TableGen itself (if ``true``).
508
509 For each variable defined in a record, the ``def`` object for that
510 record also has a key for the variable name. The corresponding value
511 is a translation into JSON of the variable's value, using the
512 conventions described below.
513
514 Some TableGen data types are translated directly into the
515 corresponding JSON type:
516
517 * A completely undefined value (e.g. for a variable declared without
518   initializer in some superclass of this record, and never initialized
519   by the record itself or any other superclass) is emitted as the JSON
520   ``null`` value.
521
522 * ``int`` and ``bit`` values are emitted as numbers. Note that
523   TableGen ``int`` values are capable of holding integers too large to
524   be exactly representable in IEEE double precision. The integer
525   literal in the JSON output will show the full exact integer value.
526   So if you need to retrieve large integers with full precision, you
527   should use a JSON reader capable of translating such literals back
528   into 64-bit integers without losing precision, such as Python's
529   standard ``json`` module.
530
531 * ``string`` and ``code`` values are emitted as JSON strings.
532
533 * ``list<T>`` values, for any element type ``T``, are emitted as JSON
534   arrays. Each element of the array is represented in turn using these
535   same conventions.
536
537 * ``bits`` values are also emitted as arrays. A ``bits`` array is
538   ordered from least-significant bit to most-significant. So the
539   element with index ``i`` corresponds to the bit described as
540   ``x{i}`` in TableGen source. However, note that this means that
541   scripting languages are likely to *display* the array in the
542   opposite order from the way it appears in the TableGen source or in
543   the diagnostic ``-print-records`` output.
544
545 All other TableGen value types are emitted as a JSON object,
546 containing two standard fields: ``kind`` is a discriminator describing
547 which kind of value the object represents, and ``printable`` is a
548 string giving the same representation of the value that would appear
549 in ``-print-records``.
550
551 * A reference to a ``def`` object has ``kind=="def"``, and has an
552   extra field ``def`` giving the name of the object referred to.
553
554 * A reference to another variable in the same record has
555   ``kind=="var"``, and has an extra field ``var`` giving the name of
556   the variable referred to.
557
558 * A reference to a specific bit of a ``bits``-typed variable in the
559   same record has ``kind=="varbit"``, and has two extra fields:
560   ``var`` gives the name of the variable referred to, and ``index``
561   gives the index of the bit.
562
563 * A value of type ``dag`` has ``kind=="dag"``, and has two extra
564   fields. ``operator`` gives the initial value after the opening
565   parenthesis of the dag initializer; ``args`` is an array giving the
566   following arguments. The elements of ``args`` are arrays of length
567   2, giving the value of each argument followed by its colon-suffixed
568   name (if any). For example, in the JSON representation of the dag
569   value ``(Op 22, "hello":$foo)`` (assuming that ``Op`` is the name of
570   a record defined elsewhere with a ``def`` statement):
571
572   * ``operator`` will be an object in which ``kind=="def"`` and
573     ``def=="Op"``
574
575   * ``args`` will be the array ``[[22, null], ["hello", "foo"]]``.
576
577 * If any other kind of value or complicated expression appears in the
578   output, it will have ``kind=="complex"``, and no additional fields.
579   These values are not expected to be needed by backends. The standard
580   ``printable`` field can be used to extract a representation of them
581   in TableGen source syntax if necessary.
582
583 SearchableTables Reference
584 --------------------------
585
586 A TableGen include file, ``SearchableTable.td``, provides classes for
587 generating C++ searchable tables. These tables are described in the
588 following sections. To generate the C++ code, run ``llvm-tblgen`` with the
589 ``--gen-searchable-tables`` option, which invokes the backend that generates
590 the tables from the records you provide.
591
592 Each of the data structures generated for searchable tables is guarded by an
593 ``#ifdef``. This allows you to include the generated ``.inc`` file and select only
594 certain data structures for inclusion. The examples below show the macro
595 names used in these guards.
596
597 Generic Enumerated Types
598 ~~~~~~~~~~~~~~~~~~~~~~~~
599
600 The ``GenericEnum`` class makes it easy to define a C++ enumerated type and
601 the enumerated *elements* of that type. To define the type, define a record
602 whose parent class is ``GenericEnum`` and whose name is the desired enum
603 type. This class provides three fields, which you can set in the record
604 using the ``let`` statement.
605
606 * ``string FilterClass``. The enum type will have one element for each record
607   that derives from this class. These records are collected to assemble the
608   complete set of elements.
609
610 * ``string NameField``. The name of a field *in the collected records* that specifies
611   the name of the element. If a record has no such field, the record's
612   name will be used.
613
614 * ``string ValueField``. The name of a field *in the collected records* that
615   specifies the numerical value of the element. If a record has no such
616   field, it will be assigned an integer value. Values are assigned in
617   alphabetical order starting with 0.
618
619 Here is an example where the values of the elements are specified
620 explicitly, as a template argument to the ``BEntry`` class. The resulting
621 C++ code is shown.
622
623 .. code-block:: text
624
625   def BValues : GenericEnum {
626     let FilterClass = "BEntry";
627     let NameField = "Name";
628     let ValueField = "Encoding";
629   }
630
631   class BEntry<bits<16> enc> {
632     string Name = NAME;
633     bits<16> Encoding = enc;
634   }
635
636   def BFoo   : BEntry<0xac>;
637   def BBar   : BEntry<0x14>;
638   def BZoo   : BEntry<0x80>;
639   def BSnork : BEntry<0x4c>;
640
641 .. code-block:: text
642
643   #ifdef GET_BValues_DECL
644   enum BValues {
645     BBar = 20,
646     BFoo = 172,
647     BSnork = 76,
648     BZoo = 128,
649   };
650   #endif
651
652 In the following example, the values of the elements are assigned
653 automatically. Note that values are assigned from 0, in alphabetical order
654 by element name.
655
656 .. code-block:: text
657
658   def CEnum : GenericEnum {
659     let FilterClass = "CEnum";
660   }
661
662   class CEnum;
663
664   def CFoo : CEnum;
665   def CBar : CEnum;
666   def CBaz : CEnum;
667
668 .. code-block:: text
669
670   #ifdef GET_CEnum_DECL
671   enum CEnum {
672     CBar = 0,
673     CBaz = 1,
674     CFoo = 2,
675   };
676   #endif
677
678
679 Generic Tables
680 ~~~~~~~~~~~~~~
681
682 The ``GenericTable`` class is used to define a searchable generic table.
683 TableGen produces C++ code to define the table entries and also produces
684 the declaration and definition of a function to search the table based on a
685 primary key. To define the table, define a record whose parent class is
686 ``GenericTable`` and whose name is the name of the global table of entries.
687 This class provides six fields.
688
689 * ``string FilterClass``. The table will have one entry for each record
690   that derives from this class.
691
692 * ``string CppTypeName``. The name of the C++ struct/class type of the
693   table that holds the entries. If unspecified, the ``FilterClass`` name is
694   used.
695
696 * ``list<string> Fields``. A list of the names of the fields *in the
697   collected records* that contain the data for the table entries. The order of
698   this list determines the order of the values in the C++ initializers. See
699   below for information about the types of these fields.
700
701 * ``list<string> PrimaryKey``. The list of fields that make up the
702   primary key.
703
704 * ``string PrimaryKeyName``. The name of the generated C++ function
705   that performs a lookup on the primary key.
706
707 * ``bit PrimaryKeyEarlyOut``. See the third example below.
708
709 TableGen attempts to deduce the type of each of the table fields so that it
710 can format the C++ initializers in the emitted table. It can deduce ``bit``,
711 ``bits<n>``, ``string``, ``Intrinsic``, and ``Instruction``.  These can be
712 used in the primary key. Any other field types must be specified
713 explicitly; this is done as shown in the second example below. Such fields
714 cannot be used in the primary key.
715
716 One special case of the field type has to do with code. Arbitrary code is
717 represented by a string, but has to be emitted as a C++ initializer without
718 quotes. If the code field was defined using a code literal (``[{...}]``),
719 then TableGen will know to emit it without quotes. However, if it was
720 defined using a string literal or complex string expression, then TableGen
721 will not know. In this case, you can force TableGen to treat the field as
722 code by including the following line in the ``GenericTable`` record, where
723 *xxx* is the code field name.
724
725 .. code-block:: text
726
727   string TypeOf_xxx = "code";
728
729 Here is an example where TableGen can deduce the field types. Note that the
730 table entry records are anonymous; the names of entry records are
731 irrelevant.
732
733 .. code-block:: text
734
735   def ATable : GenericTable {
736     let FilterClass = "AEntry";
737     let Fields = ["Str", "Val1", "Val2"];
738     let PrimaryKey = ["Val1", "Val2"];
739     let PrimaryKeyName = "lookupATableByValues";
740   }
741
742   class AEntry<string str, int val1, int val2> {
743     string Str = str;
744     bits<8> Val1 = val1;
745     bits<10> Val2 = val2;
746   }
747
748   def : AEntry<"Bob",   5, 3>;
749   def : AEntry<"Carol", 2, 6>;
750   def : AEntry<"Ted",   4, 4>;
751   def : AEntry<"Alice", 4, 5>;
752   def : AEntry<"Costa", 2, 1>;
753
754 Here is the generated C++ code. The declaration of ``lookupATableByValues``
755 is guarded by ``GET_ATable_DECL``, while the definitions are guarded by
756 ``GET_ATable_IMPL``.
757
758 .. code-block:: text
759
760   #ifdef GET_ATable_DECL
761   const AEntry *lookupATableByValues(uint8_t Val1, uint16_t Val2);
762   #endif
763
764   #ifdef GET_ATable_IMPL
765   constexpr AEntry ATable[] = {
766     { "Costa", 0x2, 0x1 }, // 0
767     { "Carol", 0x2, 0x6 }, // 1
768     { "Ted", 0x4, 0x4 }, // 2
769     { "Alice", 0x4, 0x5 }, // 3
770     { "Bob", 0x5, 0x3 }, // 4
771   };
772
773   const AEntry *lookupATableByValues(uint8_t Val1, uint16_t Val2) {
774     struct KeyType {
775       uint8_t Val1;
776       uint16_t Val2;
777     };
778     KeyType Key = { Val1, Val2 };
779     auto Table = makeArrayRef(ATable);
780     auto Idx = std::lower_bound(Table.begin(), Table.end(), Key,
781       [](const AEntry &LHS, const KeyType &RHS) {
782         if (LHS.Val1 < RHS.Val1)
783           return true;
784         if (LHS.Val1 > RHS.Val1)
785           return false;
786         if (LHS.Val2 < RHS.Val2)
787           return true;
788         if (LHS.Val2 > RHS.Val2)
789           return false;
790         return false;
791       });
792   
793     if (Idx == Table.end() ||
794         Key.Val1 != Idx->Val1 ||
795         Key.Val2 != Idx->Val2)
796       return nullptr;
797     return &*Idx;
798   }
799   #endif
800
801 The table entries in ``ATable`` are sorted in order by ``Val1``, and within
802 each of those values, by ``Val2``. This allows a binary search of the table,
803 which is performed in the lookup function by ``std::lower_bound``. The
804 lookup function returns a reference to the found table entry, or the null
805 pointer if no entry is found.
806
807 This example includes a field whose type TableGen cannot deduce. The ``Kind``
808 field uses the enumerated type ``CEnum`` defined above. To inform TableGen
809 of the type, the record derived from ``GenericTable`` must include a string field
810 named ``TypeOf_``\ *field*, where *field* is the name of the field whose type
811 is required.
812
813 .. code-block:: text
814
815   def CTable : GenericTable {
816     let FilterClass = "CEntry";
817     let Fields = ["Name", "Kind", "Encoding"];
818     string TypeOf_Kind = "CEnum";
819     let PrimaryKey = ["Encoding"];
820     let PrimaryKeyName = "lookupCEntryByEncoding";
821   }
822
823   class CEntry<string name, CEnum kind, int enc> {
824     string Name = name;
825     CEnum Kind = kind;
826     bits<16> Encoding = enc;
827   }
828
829   def : CEntry<"Apple", CFoo, 10>;
830   def : CEntry<"Pear",  CBaz, 15>;
831   def : CEntry<"Apple", CBar, 13>;
832
833 Here is the generated C++ code.
834
835 .. code-block:: text
836
837   #ifdef GET_CTable_DECL
838   const CEntry *lookupCEntryByEncoding(uint16_t Encoding);
839   #endif
840
841   #ifdef GET_CTable_IMPL
842   constexpr CEntry CTable[] = {
843     { "Apple", CFoo, 0xA }, // 0
844     { "Apple", CBar, 0xD }, // 1
845     { "Pear", CBaz, 0xF }, // 2
846   };
847
848   const CEntry *lookupCEntryByEncoding(uint16_t Encoding) {
849     struct KeyType {
850       uint16_t Encoding;
851     };
852     KeyType Key = { Encoding };
853     auto Table = makeArrayRef(CTable);
854     auto Idx = std::lower_bound(Table.begin(), Table.end(), Key,
855       [](const CEntry &LHS, const KeyType &RHS) {
856         if (LHS.Encoding < RHS.Encoding)
857           return true;
858         if (LHS.Encoding > RHS.Encoding)
859           return false;
860         return false;
861       });
862
863     if (Idx == Table.end() ||
864         Key.Encoding != Idx->Encoding)
865       return nullptr;
866     return &*Idx;
867   }
868
869 The ``PrimaryKeyEarlyOut`` field, when set to 1, modifies the lookup
870 function so that it tests the first field of the primary key to determine
871 whether it is within the range of the collected records' primary keys. If
872 not, the function returns the null pointer without performing the binary
873 search. This is useful for tables that provide data for only some of the
874 elements of a larger enum-based space. The first field of the primary key
875 must be an integral type; it cannot be a string.
876
877 Adding ``let PrimaryKeyEarlyOut = 1`` to the ``ATable`` above:
878
879 .. code-block:: text
880
881   def ATable : GenericTable {
882     let FilterClass = "AEntry";
883     let Fields = ["Str", "Val1", "Val2"];
884     let PrimaryKey = ["Val1", "Val2"];
885     let PrimaryKeyName = "lookupATableByValues";
886     let PrimaryKeyEarlyOut = 1;
887   }
888
889 causes the lookup function to change as follows:
890
891 .. code-block:: text
892
893   const AEntry *lookupATableByValues(uint8_t Val1, uint16_t Val2) {
894     if ((Val1 < 0x2) ||
895         (Val1 > 0x5))
896       return nullptr;
897
898     struct KeyType {
899     ...
900
901 Search Indexes
902 ~~~~~~~~~~~~~~
903
904 The ``SearchIndex`` class is used to define additional lookup functions for
905 generic tables. To define an additional function, define a record whose parent
906 class is ``SearchIndex`` and whose name is the name of the desired lookup
907 function. This class provides three fields.
908
909 * ``GenericTable Table``. The name of the table that is to receive another
910   lookup function.
911
912 * ``list<string> Key``. The list of fields that make up the secondary key.
913
914 * ``bit EarlyOut``. See the third example in `Generic Tables`_.
915
916 Here is an example of a secondary key added to the ``CTable`` above. The
917 generated function looks up entries based on the ``Name`` and ``Kind`` fields.
918
919 .. code-block:: text
920
921   def lookupCEntry : SearchIndex {
922     let Table = CTable;
923     let Key = ["Name", "Kind"];
924   }
925
926 This use of ``SearchIndex`` generates the following additional C++ code.
927
928 .. code-block:: text
929
930   const CEntry *lookupCEntry(StringRef Name, unsigned Kind);
931
932   ...
933
934   const CEntry *lookupCEntryByName(StringRef Name, unsigned Kind) {
935     struct IndexType {
936       const char * Name;
937       unsigned Kind;
938       unsigned _index;
939     };
940     static const struct IndexType Index[] = {
941       { "APPLE", CBar, 1 },
942       { "APPLE", CFoo, 0 },
943       { "PEAR", CBaz, 2 },
944     };
945
946     struct KeyType {
947       std::string Name;
948       unsigned Kind;
949     };
950     KeyType Key = { Name.upper(), Kind };
951     auto Table = makeArrayRef(Index);
952     auto Idx = std::lower_bound(Table.begin(), Table.end(), Key,
953       [](const IndexType &LHS, const KeyType &RHS) {
954         int CmpName = StringRef(LHS.Name).compare(RHS.Name);
955         if (CmpName < 0) return true;
956         if (CmpName > 0) return false;
957         if ((unsigned)LHS.Kind < (unsigned)RHS.Kind)
958           return true;
959         if ((unsigned)LHS.Kind > (unsigned)RHS.Kind)
960           return false;
961         return false;
962       });
963
964     if (Idx == Table.end() ||
965         Key.Name != Idx->Name ||
966         Key.Kind != Idx->Kind)
967       return nullptr;
968     return &CTable[Idx->_index];
969   }
970