1 ## Introduction to TableGen Part 1: Classes, Defs, Basic Types and Let
3 **Note:** The content in this notebook is adapted from [this document](https://llvm.org/docs/TableGen/index.html). Refer to it if you want more details.
5 This tutorial will cover:
9 * `let` in various forms
10 * Class template arguments
14 TableGen is a language used in LLVM to automate the generation of certain types of code. Usually repetitive code that has a common structure. TableGen is used to generate "records" that are then processed by a "backend" into domain specific code.
16 The compiler for TableGen is the binary `llvm-tblgen`. This contains the logic to convert TableGen source into records that can then be passed to a TableGen backend.
18 TableGen allows you to define Classes and Defs (which are instances of classes) but it doesn't encode what to do with that structure. That's what the backend does. The backend converts this structure into something useful, for example C++ code.
20 These backends are included in the `llvm-tblgen` binary and you can choose which one to run using a command line option. If you don't choose a backend you get a dump of the structure, and that is what this notebook will be showing.
22 This tutorial will focus on the language itself only. The only thing you need to know now is that in addition to `llvm-tblgen` you will see other `*-tblgen` like `clang-tblgen`. The difference between them is the backends they include.
24 The default output from `llvm-tblgen` looks like this:
33 ------------- Classes -----------------
34 ------------- Defs -----------------
37 **Note:** `%config` is not a TableGen command but a "magic" command to the Jupyter kernel for this notebook. By default new cells include the content of previously run cells, but for this notebook we mostly want each to be isolated. On occasion we will use the `%noreset` magic to override this.
39 No source means no classes and no defs. Let's add a class.
48 ------------- Classes -----------------
51 ------------- Defs -----------------
54 Followed by a def (definition).
63 ------------- Classes -----------------
66 ------------- Defs -----------------
71 `def` creates an instance of a class. Typically, the main loop of a TableGen backend will look for all defs that are instances of a certain class.
73 For example if I am generating register information I would look for all defs that are instances of `RegisterInfo` in the example below.
78 def X0: RegisterInfo {}
79 def X1: RegisterInfo {}
82 ------------- Classes -----------------
85 ------------- Defs -----------------
86 def X0 { // RegisterInfo
88 def X1 { // RegisterInfo
94 Like many other languages with classes, a class in TableGen can inherit properties of another class.
102 ------------- Classes -----------------
107 ------------- Defs -----------------
110 Inheritance is done by putting the class you want to inherit from after `:`, before the opening `{`.
112 You'll know that `D` inherits from `C` by the `// C` comment on the `class D {` line in the output.
114 Not very interesting though, what are we actually inheriting? The members of the parent class.
124 ------------- Classes -----------------
131 ------------- Defs -----------------
134 Note that `D` now has the `a` member which was defined in the class `C`.
136 You can inherit from multiple classes. In that case the order that that happens in matches the order you write the class names after the `:`.
149 ------------- Classes -----------------
159 ------------- Defs -----------------
162 Class `E` first inherits from class `C`. This gives `E` a member `a` with value `1`. Then it inherits from class `D` which also has a member `a` but with a value of `2`. Meaning the final value of `E`'s `a` is `2`.
164 When a member has the same name this is handled on a "last one in wins" basis. Assuming the types match.
177 <stdin>:7:14: error: New definition of 'a' of type 'int' is incompatible with previous definition of type 'string'
182 When they don't match, we get an error. Luckily for us, we're about to learn all about types.
186 TableGen is statically typed with error checking to prevent you from assigning things with mismatched types.
197 ------------- Classes -----------------
203 ------------- Defs -----------------
206 Here we've created a class C with integer, bit (1 or 0) and string members. See [here](https://llvm.org/docs/TableGen/ProgRef.html#types) for a full list of types.
208 Note that you do not have to give a member a default value, it can be left uninitialised.
217 ------------- Classes -----------------
223 ------------- Defs -----------------
231 When you make an instance of a class using `def`, that instance gets all the members of the class. Their values will be as set in the class, unless otherwise overridden.
233 In the case of `a` it also keeps the undefined value. Any backend using that definition would have to check for that case.
244 <stdin>:10:13: error: Field 'a' of type 'int' is incompatible with value '"abc"' of type 'string'
247 <stdin>:11:1: error: expected ';' after declaration
252 Here we see the type checking in action. Member `a` has type `int` so we cannot assign a `string` to it.
256 If we want to override those member values we can use `let` ([documented here](https://llvm.org/docs/TableGen/ProgRef.html#let-override-fields-in-classes-or-records)). This can be done in a couple of ways. The first is where you mark the scope of the `let` using `in {}`.
258 `let <name>=<value> in {`
260 The code below says that within the `{}` after the `let`, all `a` should have the value 5.
272 ------------- Classes -----------------
276 ------------- Defs -----------------
282 For multiple names, separate them with a comma.
295 ------------- Classes -----------------
300 ------------- Defs -----------------
307 You can also use `let` within a `def`. This means the scope of the `let` is the same as the scope of the `def` (the def's `{...}`).
320 ------------- Classes -----------------
324 ------------- Defs -----------------
333 Note that `Y` has `a` as `9` because the `let` was only applied to `X`.
335 It is an error to try to `let` a name that hasn't been defined or to give it a value of the incorrect type.
347 <stdin>:5:9: error: Field 'a' of type 'int' is incompatible with value '"Hello"' of type 'string'
352 Above, the member `a` was defined but with a type of `int`. We therefore cannot `let` it have a value of type `string`.
364 <stdin>:5:11: error: Value 'b' unknown!
369 Above, class `C` only has one member, `a`. Therefore we get an error trying to override the value of `b` which doesn't exist.
371 If you have multiple let, the outer scope is applied first then on down to the narrowest scope.
383 def Z: Base { let var=7; }
387 ------------- Classes -----------------
391 ------------- Defs -----------------
403 The first `let` is at what we call the "top level". That means the outer most scope in terms of the source code. A bit like a global variable in a C file.
405 This is applied first and changes `var` from `4` to `5` for all classes within that `let` (`4` came from the definition of `Base`).
407 def `X` is within the global `let`, therefore `var` is `5` within `X`.
409 Then we have a `let` inside the global `let`. This one changes `var` from `5` to `6`. The scope of the `let` only contains the def `Y` therefore within `Y`, `var` is `6`.
411 Finally def `Z` is within the global `let`, so `var` starts as `5`. `Z` has an inner `let` that changes `var` to `7`.
413 That example is quite complex just to demonstrate the feature. Let's look at something more practical.
422 // Repeats 30 times for X1...X31
425 // Repeats 30 times for W1...W31
428 ------------- Classes -----------------
432 ------------- Defs -----------------
441 (for anyone curious that's AArch64's register naming)
443 The use case here is that we are describing registers. Some are 32 bits wide and some are 64 bits wide.
445 We start by setting a default value of `size` which is 4 (4x8=32 bits) in the class `Register`. Then using a top level `let` we override that value and set it to 8 for all the 64 bit registers at once. So we don't need to do `size=8` over and over again.
447 ## Classes As Class Members
449 In addition to the built in types, class members can be user defined classes.
459 ------------- Classes -----------------
465 ------------- Defs -----------------
468 Of course that raises the question, how do we construct an instance of `Inner` to use as the value?
470 We simply use a `def` like we have done before.
475 def AnInner: Inner {}
479 def AnOuter: Outer {}
482 ------------- Classes -----------------
488 ------------- Defs -----------------
489 def AnInner { // Inner
491 def AnOuter { // Outer
496 ## Class Template Arguments
498 Class template arguments are used to pass parameters to classes when you `def` them.
502 class C <int a, int b> {
509 ------------- Classes -----------------
510 class C<int C:a = ?, int C:b = ?> {
514 ------------- Defs -----------------
521 This means that to `def` a `C` we must now provide 2 arguments that have type `int` (type checking applies here as it does elsewhere).
523 This is going to look familiar if you have written C++. In C++ it might look like:
525 template<int a, int b>
533 If templates aren't your thing, another way to think of them is as parameters to the constructor of a class.
535 For instance Python code might look like this:
538 def __init__(self, a, b):
548 class C <int a, int b> {
555 <stdin>:5:8: error: value not specified for template argument 'C:b'
558 <stdin>:1:21: note: declared in 'C'
559 class C <int a, int b> {
563 When not enough arguments are provided, you get an error.
565 Below is what happens when one of those arguments is of the wrong type.
569 class C <int a, int b> {
573 def X: C<0, "hello"> {}
576 <stdin>:5:8: error: Value specified for template argument 'C:b' is of type string; expected type int: "hello"
577 def X: C<0, "hello"> {}
581 You can also provide default values for template arguments.
591 ------------- Classes -----------------
592 class C<int C:a = 10> {
595 ------------- Defs -----------------
601 Using class template arguments you can enforce a structure on the user of the classes. In our previous register example I could use this to require the the user pass a value for the size.
603 The code below makes the size argument mandatory but the alias optional.
607 class Register<int _size, string _alias=""> {
609 string alias = _alias;
611 def X0: Register<8> {}
612 def X29: Register<8, "frame pointer"> {}
615 ------------- Classes -----------------
616 class Register<int Register:_size = ?, string Register:_alias = ""> {
617 int size = Register:_size;
618 string alias = Register:_alias;
620 ------------- Defs -----------------
625 def X29 { // Register
627 string alias = "frame pointer";
631 **Note:** You can't reuse the name between the template argument and the class member.
632 Here I have added `_` to the template argument but there's no required style.
634 For `X0` we don't pass an alias so we get the default of `""`, which would mean there is no alias.
636 For `X29` we've passed a value for the alias, which overrides the default value.
638 In C++, the equivalent would be:
640 // Constructor for class Register
641 Register(int size, const char* alias=nullptr) :
646 def __init__(self, size, alias=""):