From 23f54fa655dfc6eb9521aa99cb8ccf706483a39b Mon Sep 17 00:00:00 2001
From: Chris Lattner
+In general, classes are useful for collecting together the commonality between a +group of records, and isolating it in a single places. Also, classes permit the +specification of default values for their subclasses, allowing the subclasses to +override them as they wish. +
+ @@ -456,7 +463,84 @@ because the D class overrode its value.+TableGen permits the definition of parameterized classes as well as normal +concrete classes. Parameterized TableGen classes specify a list of variable +bindings (which may optionally have defaults) that are bound when used. Here is +a simple example:
+ ++class FPFormat<bits<3> val> { + bits<3> Value = val; +} +def NotFP : FPFormat<0>; +def ZeroArgFP : FPFormat<1>; +def OneArgFP : FPFormat<2>; +def OneArgFPRW : FPFormat<3>; +def TwoArgFP : FPFormat<4>; +def SpecialFP : FPFormat<5>; ++ +
+In this case, template arguments are used as a space efficient way to specify a +list of "enumeration values", each with a "Value" field set to the specified +integer.
+ +The more esoteric forms of TableGen expressions are +useful in conjunction with template arguments. As an example:
+ ++class ModRefVal<bits<2> val> { + bits<2> Value = val; +} + +def None : ModRefVal<0>; +def Mod : ModRefVal<1>; +def Ref : ModRefVal<2>; +def ModRef : ModRefVal<3>; + +class Value<ModRefVal MR> { + // decode some information into a more convenient format, while providing + // a nice interface to the user of the "Value" class. + bit isMod = MR.Value{0}; + bit isRef = MR.Value{1}; + + // other stuff... +} + +// Example uses +def bork : Value<Mod>; +def zork : Value<Ref>; +def hork : Value<ModRef>; ++ +
+This is obviously a contrived example, but it shows how template arguments can +be used to decouple the interface provided to the user of the class from the +actual internal data representation expected by the class. In this case, +running tblgen on the example prints the following definitions:
+ ++def bork { // Value + bit isMod = 1; + bit isRef = 0; +} +def hork { // Value + bit isMod = 1; + bit isRef = 1; +} +def zork { // Value + bit isMod = 0; + bit isRef = 1; +} ++ +
+This shows that TableGen was able to dig into the argument and extract a piece +of information that was requested by the designer of the "Value" class. For +more realistic examples, please see existing users of TableGen, such as the X86 +backend.
+- include "foo.td" +include "foo.td"