llvm.org GIT mirror llvm / d80222a docs / TableGen / LangRef.rst

Tree @d80222a (Download .tar.gz)

LangRef.rst @d80222aview markup · raw · history · blame

TableGen Language Reference


This document is extremely rough. If you find something lacking, please fix it, file a documentation bug, or ask about it on llvmdev.


This document is meant to be a normative spec about the TableGen language in and of itself (i.e. how to understand a given construct in terms of how it affects the final set of records represented by the TableGen file). If you are unsure if this document is really what you are looking for, please read the :doc:`introduction to TableGen <index>` first.


The lexical and syntax notation used here is intended to imitate Python's. In particular, for lexical definitions, the productions operate at the character level and there is no implied whitespace between elements. The syntax definitions operate at the token level, so there is implied whitespace between tokens.

Lexical Analysis

TableGen supports BCPL (// ...) and nestable C-style (/* ... */) comments.

The following is a listing of the basic punctuation tokens:

- + [ ] { } ( ) < > : ; .  = ? #

Numeric literals take one of the following forms:

One aspect to note is that the :token:`DecimalInteger` token includes the + or -, as opposed to having + and - be unary operators as most languages do.

TableGen has identifier-like tokens:

Note that unlike most languages, TableGen allows :token:`TokIdentifier` to begin with a number. In case of ambiguity, a token will be interpreted as a numeric literal rather than an identifier.

TableGen also has two string-like literals:

:token:`TokCodeFragment` is essentially a multiline string literal delimited by [{ and }].


The current implementation accepts the following C-like escapes:

\\ \' \" \t \n

TableGen also has the following keywords:

bit   bits      class   code         dag
def   foreach   defm    field        in
int   let       list    multiclass   string

TableGen also has "bang operators" which have a wide variety of meanings:


TableGen has an include mechanism. It does not play a role in the syntax per se, since it is lexically replaced with the contents of the included file.

TableGen's top-level production consists of "objects".


A class declaration creates a record which other records can inherit from. A class can be parametrized by a list of "template arguments", whose values can be used in the class body.

A given class can only be defined once. A class declaration is considered to define the class if any of the following is true:

  1. The :token:`TemplateArgList` is present.
  2. The :token:`Body` in the :token:`ObjectBody` is present and is not empty.
  3. The :token:`BaseClassList` in the :token:`ObjectBody` is present.

You can declare an empty class by giving and empty :token:`TemplateArgList` and an empty :token:`ObjectBody`. This can serve as a restricted form of forward declaration: note that records deriving from the forward-declared class will inherit no fields from it since the record expansion is done when the record is parsed.


The declaration syntax is pretty much what you would expect as a C++ programmer.

It assigns the value to the identifer.


Both string and code correspond to the string type; the difference is purely to indicate programmer intention.

The :token:`ClassID` must identify a class that has been previously declared or defined.


The peculiar last form of :token:`RangePiece` is due to the fact that the "-" is included in the :token:`TokInteger`, hence 1-5 gets lexed as two consecutive :token:`TokInteger`'s, with values 1 and -5, instead of "1", "-", and "5". The :token:`RangeList` can be thought of as specifying "list slice" in some contexts.

:token:`SimpleValue` has a number of forms:

The value will be the variable referenced by the identifier. It can be one of:

  • name of a def, such as the use of Bar in:

    def Bar : SomeClass {
      int X = 5;
    def Foo {
      SomeClass Baz = Bar;
  • value local to a def, such as the use of Bar in:

    def Foo {
      int Bar = 5;
      int Baz = Bar;
  • a template arg of a class, such as the use of Bar in:

    class Foo<int Bar> {
      int Baz = Bar;
  • value local to a multiclass, such as the use of Bar in:

    multiclass Foo {
      int Bar = 5;
      int Baz = Bar;
  • a template arg to a multiclass, such as the use of Bar in:

    multiclass Foo<int Bar> {
      int Baz = Bar;

This represents the numeric value of the integer.

Multiple adjacent string literals are concatenated like in C/C++. The value is the concatenation of the strings.

The value is the string value of the code fragment.

? represents an "unset" initializer.

This represents a sequence of bits, as would be used to initialize a bits<n> field (where n is the number of bits).

This generates a new anonymous record definition (as would be created by an unnamed def inheriting from the given class with the given template arguments) and the value is the value of that record definition.

A list initializer. The optional :token:`Type` can be used to indicate a specific element type, otherwise the element type will be deduced from the given values.

The initial :token:`DagArg` is called the "operator" of the dag.


The version with the :token:`MultiClassID` is only valid in the :token:`BaseClassList` of a defm. The :token:`MultiClassID` should be the name of a multiclass.

It is after parsing the base class list that the "let stack" is applied.

The let form allows overriding the value of an inherited field.


Defines a record whose name is given by the :token:`TokIdentifier`. The fields of the record are inherited from the base classes and defined in the body.

Special handling occurs if this def appears inside a multiclass or a foreach.


Note that in the :token:`BaseClassList`, all of the multiclass's must precede any class's that appear.


The value assigned to the variable in the declaration is iterated over and the object or object list is reevaluated with the variable set at each iterated value.

Top-Level let

This is effectively equivalent to let inside the body of a record except that it applies to multiple records at a time. The bindings are applied at the end of parsing the base classes of a record.