blob: 2bc23780573da4da60a30d407eb381187a191804 [file] [log] [blame]
{
"cells": [
{
"cell_type": "markdown",
"id": "b1123484",
"metadata": {},
"source": [
"## Introduction to TableGen Part 1: Classes, Defs, Basic Types and Let"
]
},
{
"cell_type": "markdown",
"id": "15a8d57a",
"metadata": {},
"source": [
"**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.\n",
"\n",
"This tutorial will cover:\n",
"* Classes\n",
"* Defs\n",
"* Basic types\n",
"* `let` in various forms\n",
"* Class template arguments"
]
},
{
"cell_type": "markdown",
"id": "fbfb640e",
"metadata": {},
"source": [
"## What is TableGen?"
]
},
{
"cell_type": "markdown",
"id": "4b7d9c3e",
"metadata": {},
"source": [
"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.\n",
"\n",
"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.\n",
"\n",
"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.\n",
"\n",
"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.\n",
"\n",
"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.\n",
"\n",
"The default output from `llvm-tblgen` looks like this:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "88331eb7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------- Classes -----------------\n",
"------------- Defs -----------------\n"
]
}
],
"source": [
"%config cellreset on\n",
"\n",
"// Empty source file"
]
},
{
"cell_type": "markdown",
"id": "58f4b3e8",
"metadata": {},
"source": [
"**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.\n",
"\n",
"No source means no classes and no defs. Let's add a class."
]
},
{
"cell_type": "markdown",
"id": "da055b37",
"metadata": {},
"source": [
"## Classes"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "249f27bf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------- Classes -----------------\n",
"class C {\n",
"}\n",
"------------- Defs -----------------\n"
]
}
],
"source": [
"class C {}"
]
},
{
"cell_type": "markdown",
"id": "e92f5c84",
"metadata": {},
"source": [
"Followed by a def (definition)."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "2a101610",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------- Classes -----------------\n",
"class C {\n",
"}\n",
"------------- Defs -----------------\n",
"def X {\t// C\n",
"}\n"
]
}
],
"source": [
"%noreset\n",
"\n",
"def X: C;"
]
},
{
"cell_type": "markdown",
"id": "6b1b79a5",
"metadata": {},
"source": [
"`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.\n",
"\n",
"For example if I am generating register information I would look for all defs that are instances of `RegisterInfo` in the example below."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "964e9ba5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------- Classes -----------------\n",
"class RegisterInfo {\n",
"}\n",
"------------- Defs -----------------\n",
"def X0 {\t// RegisterInfo\n",
"}\n",
"def X1 {\t// RegisterInfo\n",
"}\n"
]
}
],
"source": [
"class RegisterInfo {}\n",
"def X0: RegisterInfo {}\n",
"def X1: RegisterInfo {}"
]
},
{
"cell_type": "markdown",
"id": "211bb409",
"metadata": {},
"source": [
"## Inheritance"
]
},
{
"cell_type": "markdown",
"id": "b2ef33b7",
"metadata": {},
"source": [
"Like many other languages with classes, a class in TableGen can inherit properties of another class."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "b3c23664",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------- Classes -----------------\n",
"class C {\n",
"}\n",
"class D {\t// C\n",
"}\n",
"------------- Defs -----------------\n"
]
}
],
"source": [
"class C {}\n",
"class D : C {}"
]
},
{
"cell_type": "markdown",
"id": "c85bfa75",
"metadata": {},
"source": [
"Inheritance is done by putting the class you want to inherit from after `:`, before the opening `{`.\n",
"\n",
"You'll know that `D` inherits from `C` by the `// C` comment on the `class D {` line in the output.\n",
"\n",
"Not very interesting though, what are we actually inheriting? The members of the parent class."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "eb8e210e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------- Classes -----------------\n",
"class C {\n",
" int a = ?;\n",
"}\n",
"class D {\t// C\n",
" int a = ?;\n",
"}\n",
"------------- Defs -----------------\n"
]
}
],
"source": [
"class C {\n",
" int a;\n",
"}\n",
"class D : C {}"
]
},
{
"cell_type": "markdown",
"id": "224caf2d",
"metadata": {},
"source": [
"Note that `D` now has the `a` member which was defined in the class `C`.\n",
"\n",
"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 `:`."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "842e12a7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------- Classes -----------------\n",
"class C {\n",
" int a = 1;\n",
"}\n",
"class D {\n",
" int a = 2;\n",
"}\n",
"class E {\t// C D\n",
" int a = 2;\n",
"}\n",
"------------- Defs -----------------\n"
]
}
],
"source": [
"class C {\n",
" int a = 1;\n",
"}\n",
"class D {\n",
" int a = 2;\n",
"}\n",
"class E : C, D {}"
]
},
{
"cell_type": "markdown",
"id": "de372733",
"metadata": {},
"source": [
"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`.\n",
"\n",
"When a member has the same name this is handled on a \"last one in wins\" basis. Assuming the types match."
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "54782b14",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"<stdin>:7:14: error: New definition of 'a' of type 'int' is incompatible with previous definition of type 'string'\n",
"class E : C, D {}\n",
" ^\n"
]
}
],
"source": [
"class C {\n",
" string a = \"\";\n",
"}\n",
"class D {\n",
" int a = 2;\n",
"}\n",
"class E : C, D {}"
]
},
{
"cell_type": "markdown",
"id": "538264ff",
"metadata": {},
"source": [
"When they don't match, we get an error. Luckily for us, we're about to learn all about types."
]
},
{
"cell_type": "markdown",
"id": "90c1d03f",
"metadata": {},
"source": [
"## Types"
]
},
{
"cell_type": "markdown",
"id": "3a7b4496",
"metadata": {},
"source": [
"TableGen is statically typed with error checking to prevent you from assigning things with mismatched types."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "232a8bdf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------- Classes -----------------\n",
"class C {\n",
" int a = ?;\n",
" bit b = 0;\n",
" string s = \"Hello\";\n",
"}\n",
"------------- Defs -----------------\n"
]
}
],
"source": [
"class C {\n",
" int a;\n",
" bit b = 0;\n",
" string s = \"Hello\";\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "c2c6203b",
"metadata": {},
"source": [
"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.\n",
"\n",
"Note that you do not have to give a member a default value, it can be left uninitialised."
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "9f8149cb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------- Classes -----------------\n",
"class C {\n",
" int a = ?;\n",
" bit b = 0;\n",
" string s = \"Hello\";\n",
"}\n",
"------------- Defs -----------------\n",
"def X {\t// C\n",
" int a = ?;\n",
" bit b = 0;\n",
" string s = \"Hello\";\n",
"}\n"
]
}
],
"source": [
"%noreset\n",
"\n",
"def X: C {}"
]
},
{
"cell_type": "markdown",
"id": "79ead3c0",
"metadata": {},
"source": [
"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.\n",
"\n",
"In the case of `a` it also keeps the undefined value. Any backend using that definition would have to check for that case."
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "447e20b6",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"<stdin>:10:13: error: Field 'a' of type 'int' is incompatible with value '\"abc\"' of type 'string'\n",
" int a = \"abc\"\n",
" ^\n",
"<stdin>:11:1: error: expected ';' after declaration\n",
"}\n",
"^\n"
]
}
],
"source": [
"%noreset\n",
"\n",
"def Y {\n",
" int a = \"abc\"\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "0cbe813f",
"metadata": {},
"source": [
"Here we see the type checking in action. Member `a` has type `int` so we cannot assign a `string` to it."
]
},
{
"cell_type": "markdown",
"id": "0db796fc",
"metadata": {},
"source": [
"## Let"
]
},
{
"cell_type": "markdown",
"id": "d3c41506",
"metadata": {},
"source": [
"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 {}`.\n",
"\n",
"`let <name>=<value> in {`\n",
"\n",
"The code below says that within the `{}` after the `let`, all `a` should have the value 5."
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "296a5d9c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------- Classes -----------------\n",
"class C {\n",
" int a = 9;\n",
"}\n",
"------------- Defs -----------------\n",
"def X {\t// C\n",
" int a = 5;\n",
"}\n"
]
}
],
"source": [
"class C {\n",
" int a = 9;\n",
"}\n",
"let a=5 in {\n",
" def X: C {}\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "1bcceae5",
"metadata": {},
"source": [
"For multiple names, separate them with a comma."
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "3b640bcb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------- Classes -----------------\n",
"class C {\n",
" int a = ?;\n",
" int b = ?;\n",
"}\n",
"------------- Defs -----------------\n",
"def X {\t// C\n",
" int a = 5;\n",
" int b = 6;\n",
"}\n"
]
}
],
"source": [
"class C {\n",
" int a;\n",
" int b;\n",
"}\n",
"let a=5, b=6 in {\n",
" def X: C {}\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "89b5cdd9",
"metadata": {},
"source": [
"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 `{...}`)."
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "826a5449",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------- Classes -----------------\n",
"class C {\n",
" int a = 9;\n",
"}\n",
"------------- Defs -----------------\n",
"def X {\t// C\n",
" int a = 5;\n",
"}\n",
"def Y {\t// C\n",
" int a = 9;\n",
"}\n"
]
}
],
"source": [
"class C {\n",
" int a = 9;\n",
"}\n",
"def X: C {\n",
" let a=5;\n",
"}\n",
"def Y: C {}"
]
},
{
"cell_type": "markdown",
"id": "ad57c118",
"metadata": {},
"source": [
"Note that `Y` has `a` as `9` because the `let` was only applied to `X`.\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "ddc86443",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"<stdin>:5:9: error: Field 'a' of type 'int' is incompatible with value '\"Hello\"' of type 'string'\n",
" let a=\"Hello\";\n",
" ^\n"
]
}
],
"source": [
"class C {\n",
" int a = 9;\n",
"}\n",
"def X: C {\n",
" let a=\"Hello\";\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "4f720f3d",
"metadata": {},
"source": [
"Above, the member `a` was defined but with a type of `int`. We therefore cannot `let` it have a value of type `string`."
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "2748d14c",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"<stdin>:5:11: error: Value 'b' unknown!\n",
" let b=5;\n",
" ^\n"
]
}
],
"source": [
"class C {\n",
" int a = 9;\n",
"}\n",
"def X: C {\n",
" let b=5;\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "a8b89546",
"metadata": {},
"source": [
"Above, class `C` only has one member, `a`. Therefore we get an error trying to override the value of `b` which doesn't exist.\n",
"\n",
"If you have multiple let, the outer scope is applied first then on down to the narrowest scope."
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "92e3bb40",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------- Classes -----------------\n",
"class Base {\n",
" int var = 4;\n",
"}\n",
"------------- Defs -----------------\n",
"def X {\t// Base\n",
" int var = 5;\n",
"}\n",
"def Y {\t// Base\n",
" int var = 6;\n",
"}\n",
"def Z {\t// Base\n",
" int var = 7;\n",
"}\n"
]
}
],
"source": [
"class Base {\n",
" int var=4;\n",
"}\n",
"let var=5 in {\n",
" def X: Base {}\n",
" let var=6 in {\n",
" def Y: Base {}\n",
" }\n",
" def Z: Base { let var=7; }\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "777c9390",
"metadata": {},
"source": [
"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.\n",
"\n",
"This is applied first and changes `var` from `4` to `5` for all classes within that `let` (`4` came from the definition of `Base`).\n",
"\n",
"def `X` is within the global `let`, therefore `var` is `5` within `X`.\n",
"\n",
"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`.\n",
"\n",
"Finally def `Z` is within the global `let`, so `var` starts as `5`. `Z` has an inner `let` that changes `var` to `7`.\n",
"\n",
"That example is quite complex just to demonstrate the feature. Let's look at something more practical."
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "626b2c19",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------- Classes -----------------\n",
"class Register {\n",
" int size = 4;\n",
"}\n",
"------------- Defs -----------------\n",
"def W0 {\t// Register\n",
" int size = 4;\n",
"}\n",
"def X0 {\t// Register\n",
" int size = 8;\n",
"}\n"
]
}
],
"source": [
"class Register {\n",
" int size=4;\n",
"}\n",
"let size=8 in {\n",
" def X0: Register {}\n",
" // Repeats 30 times for X1...X31\n",
"}\n",
"def W0: Register {}\n",
"// Repeats 30 times for W1...W31"
]
},
{
"cell_type": "markdown",
"id": "6b90974b",
"metadata": {},
"source": [
"(for anyone curious that's AArch64's register naming)\n",
"\n",
"The use case here is that we are describing registers. Some are 32 bits wide and some are 64 bits wide.\n",
"\n",
"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."
]
},
{
"cell_type": "markdown",
"id": "a15296c4",
"metadata": {},
"source": [
"## Classes As Class Members"
]
},
{
"cell_type": "markdown",
"id": "b8dffde4",
"metadata": {},
"source": [
"In addition to the built in types, class members can be user defined classes."
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "eaa4b9ec",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------- Classes -----------------\n",
"class Inner {\n",
"}\n",
"class Outer {\n",
" Inner i = ?;\n",
"}\n",
"------------- Defs -----------------\n"
]
}
],
"source": [
"class Inner {}\n",
"class Outer {\n",
" Inner i;\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "1faa1c3c",
"metadata": {},
"source": [
"Of course that raises the question, how do we construct an instance of `Inner` to use as the value?\n",
"\n",
"We simply use a `def` like we have done before."
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "243afa46",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------- Classes -----------------\n",
"class Inner {\n",
"}\n",
"class Outer {\n",
" Inner i = AnInner;\n",
"}\n",
"------------- Defs -----------------\n",
"def AnInner {\t// Inner\n",
"}\n",
"def AnOuter {\t// Outer\n",
" Inner i = AnInner;\n",
"}\n"
]
}
],
"source": [
"class Inner {}\n",
"def AnInner: Inner {}\n",
"class Outer {\n",
" Inner i = AnInner;\n",
"}\n",
"def AnOuter: Outer {}"
]
},
{
"cell_type": "markdown",
"id": "a652b8e7",
"metadata": {},
"source": [
"## Class Template Arguments"
]
},
{
"cell_type": "markdown",
"id": "49ef7b63",
"metadata": {},
"source": [
"Class template arguments are used to pass parameters to classes when you `def` them."
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "888f1d04",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------- Classes -----------------\n",
"class C<int C:a = ?, int C:b = ?> {\n",
" int c = C:a;\n",
" int d = C:b;\n",
"}\n",
"------------- Defs -----------------\n",
"def X {\t// C\n",
" int c = 0;\n",
" int d = 1;\n",
"}\n"
]
}
],
"source": [
"class C <int a, int b> {\n",
" int c = a;\n",
" int d = b;\n",
"}\n",
"def X: C<0, 1> {}"
]
},
{
"cell_type": "markdown",
"id": "071294c5",
"metadata": {},
"source": [
"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).\n",
"\n",
"This is going to look familiar if you have written C++. In C++ it might look like:\n",
"```\n",
"template<int a, int b>\n",
"class C {\n",
" int c = a;\n",
" int d = b;\n",
"};\n",
"C<0, 1> X;\n",
"```\n",
"\n",
"If templates aren't your thing, another way to think of them is as parameters to the constructor of a class. \n",
"\n",
"For instance Python code might look like this:\n",
"```\n",
"class C(object):\n",
" def __init__(self, a, b):\n",
" self.c = a\n",
" self.d = b\n",
"\n",
"print(C(0, 1).c)\n",
"# prints \"0\"\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "eb5cb0a9",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"<stdin>:5:8: error: value not specified for template argument 'C:b'\n",
"def X: C<0> {}\n",
" ^\n",
"<stdin>:1:21: note: declared in 'C'\n",
"class C <int a, int b> {\n",
" ^\n"
]
}
],
"source": [
"class C <int a, int b> {\n",
" int c = a;\n",
" int d = b;\n",
"}\n",
"def X: C<0> {}"
]
},
{
"cell_type": "markdown",
"id": "efac82b8",
"metadata": {},
"source": [
"When not enough arguments are provided, you get an error.\n",
"\n",
"Below is what happens when one of those arguments is of the wrong type."
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "480b263f",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"<stdin>:5:8: error: Value specified for template argument 'C:b' is of type string; expected type int: \"hello\"\n",
"def X: C<0, \"hello\"> {}\n",
" ^\n"
]
}
],
"source": [
"class C <int a, int b> {\n",
" int c = a;\n",
" int d = b;\n",
"}\n",
"def X: C<0, \"hello\"> {}"
]
},
{
"cell_type": "markdown",
"id": "802e6d91",
"metadata": {},
"source": [
"You can also provide default values for template arguments."
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "75dcf42f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------- Classes -----------------\n",
"class C<int C:a = 10> {\n",
" int b = C:a;\n",
"}\n",
"------------- Defs -----------------\n",
"def X {\t// C\n",
" int b = 10;\n",
"}\n"
]
}
],
"source": [
"class C <int a=10> {\n",
" int b = a;\n",
"}\n",
"def X: C<> {}"
]
},
{
"cell_type": "markdown",
"id": "fd25ec74",
"metadata": {},
"source": [
"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.\n",
"\n",
"The code below makes the size argument mandatory but the alias optional."
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "e6d4b0e5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------- Classes -----------------\n",
"class Register<int Register:_size = ?, string Register:_alias = \"\"> {\n",
" int size = Register:_size;\n",
" string alias = Register:_alias;\n",
"}\n",
"------------- Defs -----------------\n",
"def X0 {\t// Register\n",
" int size = 8;\n",
" string alias = \"\";\n",
"}\n",
"def X29 {\t// Register\n",
" int size = 8;\n",
" string alias = \"frame pointer\";\n",
"}\n"
]
}
],
"source": [
"class Register<int _size, string _alias=\"\"> {\n",
" int size = _size;\n",
" string alias = _alias;\n",
"}\n",
"def X0: Register<8> {}\n",
"def X29: Register<8, \"frame pointer\"> {}"
]
},
{
"cell_type": "markdown",
"id": "fa230949",
"metadata": {},
"source": [
"**Note:** You can't reuse the name between the template argument and the class member.\n",
"Here I have added `_` to the template argument but there's no required style.\n",
"\n",
"For `X0` we don't pass an alias so we get the default of `\"\"`, which would mean there is no alias.\n",
"\n",
"For `X29` we've passed a value for the alias, which overrides the default value.\n",
"\n",
"In C++, the equivalent would be:\n",
"```\n",
"// Constructor for class Register\n",
"Register(int size, const char* alias=nullptr) :\n",
"```\n",
"\n",
"Or in Python:\n",
"```\n",
"def __init__(self, size, alias=\"\"):\n",
"```"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "LLVM TableGen",
"language": "tablegen",
"name": "tablegen"
},
"language_info": {
"file_extension": ".td",
"mimetype": "text/x-tablegen",
"name": "tablegen",
"pygments_lexer": "text"
}
},
"nbformat": 4,
"nbformat_minor": 5
}