1bf215546Sopenharmony_ciISASPEC - XML Based ISA Specification 2bf215546Sopenharmony_ci===================================== 3bf215546Sopenharmony_ci 4bf215546Sopenharmony_ciisaspec provides a mechanism to describe an instruction set in xml, and 5bf215546Sopenharmony_cigenerate a disassembler and assembler (eventually). The intention is 6bf215546Sopenharmony_cito describe the instruction set more formally than hand-coded assembler 7bf215546Sopenharmony_ciand disassembler, and better decouple the shader compiler from the 8bf215546Sopenharmony_ciunderlying instruction encoding to simplify dealing with instruction 9bf215546Sopenharmony_ciencoding differences between generations of GPU. 10bf215546Sopenharmony_ci 11bf215546Sopenharmony_ciBenefits of a formal ISA description, compared to hand-coded assemblers 12bf215546Sopenharmony_ciand disassemblers, include easier detection of new bit combinations that 13bf215546Sopenharmony_ciwere not seen before in previous generations due to more rigorous 14bf215546Sopenharmony_cidescription of bits that are expect to be '0' or '1' or 'x' (dontcare) 15bf215546Sopenharmony_ciand verification that different encodings don't have conflicting bits 16bf215546Sopenharmony_ci(ie. that the specification cannot result in more than one valid 17bf215546Sopenharmony_ciinterpretation of any bit pattern). 18bf215546Sopenharmony_ci 19bf215546Sopenharmony_ciThe isaspec tool and xml schema are intended to be generic (not specific 20bf215546Sopenharmony_cito ir3), although there are currently a couple limitations due to short- 21bf215546Sopenharmony_cicuts taken to get things up and running (which are mostly not inherent to 22bf215546Sopenharmony_cithe xml schema, and should not be too difficult to remove from the py and 23bf215546Sopenharmony_cidecode/disasm utility): 24bf215546Sopenharmony_ci 25bf215546Sopenharmony_ci* Maximum "field" size is 64b 26bf215546Sopenharmony_ci* Fixed instruction size 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ciOften times, especially when new functionality is added in later gens 29bf215546Sopenharmony_ciwhile retaining (or at least mostly retaining) backwards compatibility 30bf215546Sopenharmony_ciwith encodings used in earlier generations, the actual encoding can be 31bf215546Sopenharmony_cirather messy to describe. To support this, isaspec provides many flexible 32bf215546Sopenharmony_cimechanism, such as conditional overrides and derived fields. This not 33bf215546Sopenharmony_cionly allows for describing an irregular instruction encoding, but also 34bf215546Sopenharmony_ciallows matching an existing disasm syntax (which might not have been 35bf215546Sopenharmony_cidesign around the idea of disassembly based on a formal ISA description). 36bf215546Sopenharmony_ci 37bf215546Sopenharmony_ciBitsets 38bf215546Sopenharmony_ci------- 39bf215546Sopenharmony_ci 40bf215546Sopenharmony_ciThe fundamental concept of matching a bit-pattern to an instruction 41bf215546Sopenharmony_cidecoding/encoding is the concept of a hierarchial tree of bitsets. 42bf215546Sopenharmony_ciThis is intended to match how the hw decodes instructions, where certain 43bf215546Sopenharmony_cibits describe the instruction (and sub-encoding, and so on), and other 44bf215546Sopenharmony_cibits describe various operands to the instruction. 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_ciBitsets can also be used recursively as the type of a field described 47bf215546Sopenharmony_ciin another bitset. 48bf215546Sopenharmony_ci 49bf215546Sopenharmony_ciThe leaves of the tree of instruction bitsets represent every possible 50bf215546Sopenharmony_ciinstruction. Deciding which instruction a bitpattern is amounts to: 51bf215546Sopenharmony_ci 52bf215546Sopenharmony_ci.. code-block:: c 53bf215546Sopenharmony_ci 54bf215546Sopenharmony_ci m = (val & bitsets[n]->mask) & ~bitsets[n]->dontcare; 55bf215546Sopenharmony_ci 56bf215546Sopenharmony_ci if (m == bitsets[n]->match) { 57bf215546Sopenharmony_ci /* we've found the instruction description */ 58bf215546Sopenharmony_ci } 59bf215546Sopenharmony_ci 60bf215546Sopenharmony_ciFor example, the starting point to decode an ir3 instruction is a 64b 61bf215546Sopenharmony_cibitset: 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_ci.. code-block:: xml 64bf215546Sopenharmony_ci 65bf215546Sopenharmony_ci <bitset name="#instruction" size="64"> 66bf215546Sopenharmony_ci <doc> 67bf215546Sopenharmony_ci Encoding of an ir3 instruction. All instructions are 64b. 68bf215546Sopenharmony_ci </doc> 69bf215546Sopenharmony_ci </bitset> 70bf215546Sopenharmony_ci 71bf215546Sopenharmony_ciIn the first level of instruction encoding hierarchy, the high three bits 72bf215546Sopenharmony_cigroup things into instruction "categories": 73bf215546Sopenharmony_ci 74bf215546Sopenharmony_ci.. code-block:: xml 75bf215546Sopenharmony_ci 76bf215546Sopenharmony_ci <bitset name="#instruction-cat2" extends="#instruction"> 77bf215546Sopenharmony_ci <field name="DST" low="32" high="39" type="#reg-gpr"/> 78bf215546Sopenharmony_ci <field name="REPEAT" low="40" high="41" type="#rptN"/> 79bf215546Sopenharmony_ci <field name="SAT" pos="42" type="bool" display="(sat)"/> 80bf215546Sopenharmony_ci <field name="SS" pos="44" type="bool" display="(ss)"/> 81bf215546Sopenharmony_ci <field name="UL" pos="45" type="bool" display="(ul)"/> 82bf215546Sopenharmony_ci <field name="DST_CONV" pos="46" type="bool"> 83bf215546Sopenharmony_ci <doc> 84bf215546Sopenharmony_ci Destination register is opposite precision as source, ie. 85bf215546Sopenharmony_ci if {FULL} is true then destination is half precision, and 86bf215546Sopenharmony_ci visa versa. 87bf215546Sopenharmony_ci </doc> 88bf215546Sopenharmony_ci </field> 89bf215546Sopenharmony_ci <derived name="DST_HALF" expr="#dest-half" type="bool" display="h"/> 90bf215546Sopenharmony_ci <field name="EI" pos="47" type="bool" display="(ei)"/> 91bf215546Sopenharmony_ci <field name="FULL" pos="52" type="bool"> 92bf215546Sopenharmony_ci <doc>Full precision source registers</doc> 93bf215546Sopenharmony_ci </field> 94bf215546Sopenharmony_ci <field name="JP" pos="59" type="bool" display="(jp)"/> 95bf215546Sopenharmony_ci <field name="SY" pos="60" type="bool" display="(sy)"/> 96bf215546Sopenharmony_ci <pattern low="61" high="63">010</pattern> <!-- cat2 --> 97bf215546Sopenharmony_ci <!-- 98bf215546Sopenharmony_ci NOTE, both SRC1_R and SRC2_R are defined at this level because 99bf215546Sopenharmony_ci SRC2_R is still a valid bit for (nopN) (REPEAT==0) for cat2 100bf215546Sopenharmony_ci instructions with only a single src 101bf215546Sopenharmony_ci --> 102bf215546Sopenharmony_ci <field name="SRC1_R" pos="43" type="bool" display="(r)"/> 103bf215546Sopenharmony_ci <field name="SRC2_R" pos="51" type="bool" display="(r)"/> 104bf215546Sopenharmony_ci <derived name="ZERO" expr="#zero" type="bool" display=""/> 105bf215546Sopenharmony_ci </bitset> 106bf215546Sopenharmony_ci 107bf215546Sopenharmony_ciThe ``<pattern>`` elements are the part(s) that determine which leaf-node 108bf215546Sopenharmony_cibitset matches against a given bit pattern. The leaf node's match/mask/ 109bf215546Sopenharmony_cidontcare bitmasks are a combination of those defined at the leaf node and 110bf215546Sopenharmony_cirecursively each parent bitclass. 111bf215546Sopenharmony_ci 112bf215546Sopenharmony_ciFor example, cat2 instructions (ALU instructions with up to two src 113bf215546Sopenharmony_ciregisters) can have either one or two source registers: 114bf215546Sopenharmony_ci 115bf215546Sopenharmony_ci.. code-block:: xml 116bf215546Sopenharmony_ci 117bf215546Sopenharmony_ci <bitset name="#instruction-cat2-1src" extends="#instruction-cat2"> 118bf215546Sopenharmony_ci <override expr="#cat2-cat3-nop-encoding"> 119bf215546Sopenharmony_ci <display> 120bf215546Sopenharmony_ci {SY}{SS}{JP}{SAT}(nop{NOP}) {UL}{NAME} {EI}{DST_HALF}{DST}, {SRC1} 121bf215546Sopenharmony_ci </display> 122bf215546Sopenharmony_ci <derived name="NOP" expr="#cat2-cat3-nop-value" type="uint"/> 123bf215546Sopenharmony_ci <field name="SRC1" low="0" high="15" type="#multisrc"> 124bf215546Sopenharmony_ci <param name="ZERO" as="SRC_R"/> 125bf215546Sopenharmony_ci <param name="FULL"/> 126bf215546Sopenharmony_ci </field> 127bf215546Sopenharmony_ci </override> 128bf215546Sopenharmony_ci <display> 129bf215546Sopenharmony_ci {SY}{SS}{JP}{SAT}{REPEAT}{UL}{NAME} {EI}{DST_HALF}{DST}, {SRC1} 130bf215546Sopenharmony_ci </display> 131bf215546Sopenharmony_ci <pattern low="16" high="31">xxxxxxxxxxxxxxxx</pattern> 132bf215546Sopenharmony_ci <pattern low="48" high="50">xxx</pattern> <!-- COND --> 133bf215546Sopenharmony_ci <field name="SRC1" low="0" high="15" type="#multisrc"> 134bf215546Sopenharmony_ci <param name="SRC1_R" as="SRC_R"/> 135bf215546Sopenharmony_ci <param name="FULL"/> 136bf215546Sopenharmony_ci </field> 137bf215546Sopenharmony_ci </bitset> 138bf215546Sopenharmony_ci 139bf215546Sopenharmony_ci <bitset name="absneg.f" extends="#instruction-cat2-1src"> 140bf215546Sopenharmony_ci <pattern low="53" high="58">000110</pattern> 141bf215546Sopenharmony_ci </bitset> 142bf215546Sopenharmony_ci 143bf215546Sopenharmony_ciIn this example, ``absneg.f`` is a concrete cat2 instruction (leaf node of 144bf215546Sopenharmony_cithe bitset inheritance tree) which has a single src register. At the 145bf215546Sopenharmony_ci``#instruction-cat2-1src`` level, bits that are used for the 2nd src arg 146bf215546Sopenharmony_ciand condition code (for cat2 instructions which use a condition code) are 147bf215546Sopenharmony_cidefined as 'x' (dontcare), which matches our understanding of the hardware 148bf215546Sopenharmony_ci(but also lets the disassembler flag cases where '1' bits show up in places 149bf215546Sopenharmony_ciwe don't expect, which may signal a new instruction (sub)encoding). 150bf215546Sopenharmony_ci 151bf215546Sopenharmony_ciYou'll notice that ``SRC1`` refers back to a different bitset hierarchy 152bf215546Sopenharmony_cithat describes various different src register encoding (used for cat2 and 153bf215546Sopenharmony_cicat4 instructions), ie. GPR vs CONST vs relative GPR/CONST. For fields 154bf215546Sopenharmony_ciwhich have bitset types, parameters can be "passed" in via ``<param>`` 155bf215546Sopenharmony_cielements, which can be referred to by the display template string, and/or 156bf215546Sopenharmony_ciexpressions. For example, this helps to deal with cases where other fields 157bf215546Sopenharmony_cioutside of that bitset control the encoding/decoding, such as in the 158bf215546Sopenharmony_ci``#multisrc`` example: 159bf215546Sopenharmony_ci 160bf215546Sopenharmony_ci.. code-block:: xml 161bf215546Sopenharmony_ci 162bf215546Sopenharmony_ci <bitset name="#multisrc" size="16"> 163bf215546Sopenharmony_ci <doc> 164bf215546Sopenharmony_ci Encoding for instruction source which can be GPR/CONST/IMMED 165bf215546Sopenharmony_ci or relative GPR/CONST. 166bf215546Sopenharmony_ci </doc> 167bf215546Sopenharmony_ci </bitset> 168bf215546Sopenharmony_ci 169bf215546Sopenharmony_ci ... 170bf215546Sopenharmony_ci 171bf215546Sopenharmony_ci <bitset name="#multisrc-gpr" extends="#multisrc"> 172bf215546Sopenharmony_ci <display> 173bf215546Sopenharmony_ci {ABSNEG}{SRC_R}{HALF}{SRC} 174bf215546Sopenharmony_ci </display> 175bf215546Sopenharmony_ci <derived name="HALF" expr="#multisrc-half" type="bool" display="h"/> 176bf215546Sopenharmony_ci <field name="SRC" low="0" high="7" type="#reg-gpr"/> 177bf215546Sopenharmony_ci <pattern low="8" high="13">000000</pattern> 178bf215546Sopenharmony_ci <field name="ABSNEG" low="14" high="15" type="#absneg"/> 179bf215546Sopenharmony_ci </bitset> 180bf215546Sopenharmony_ci 181bf215546Sopenharmony_ciAt some level in the bitset inheritance hiearchy, there is expected to be a 182bf215546Sopenharmony_ci``<display>`` element specifying a template string used during bitset 183bf215546Sopenharmony_cidecoding. The display template consists of references to fields (which may 184bf215546Sopenharmony_cibe derived fields) specified as ``{FIELDNAME}`` and other characters 185bf215546Sopenharmony_ciwhich are just echoed through to the resulting decoded bitset. 186bf215546Sopenharmony_ci 187bf215546Sopenharmony_ciIt is possible to define a line column alignment value per field to influence 188bf215546Sopenharmony_cithe visual output. It needs to be pecified as ``{FIELDNAME:align=xx}``. 189bf215546Sopenharmony_ci 190bf215546Sopenharmony_ciThe ``<override>`` element will be described in the next section, but it 191bf215546Sopenharmony_ciprovides for both different decoded instruction syntax/mnemonics (when 192bf215546Sopenharmony_cisimply providing a different display template string) as well as instruction 193bf215546Sopenharmony_ciencoding where different ranges of bits have a different meaning based on 194bf215546Sopenharmony_cisome other bitfield (or combination of bitfields). In this example it is 195bf215546Sopenharmony_ciused to cover the cases where ``SRCn_R`` has a different meaning and a 196bf215546Sopenharmony_cidifferent disassembly syntax depending on whether ``REPEAT`` equals zero. 197bf215546Sopenharmony_ci 198bf215546Sopenharmony_ciOverrides 199bf215546Sopenharmony_ci--------- 200bf215546Sopenharmony_ci 201bf215546Sopenharmony_ciIn many cases, a bitset is not convenient for describing the expected 202bf215546Sopenharmony_cidisasm syntax, and/or interpretation of some range of bits differs based 203bf215546Sopenharmony_cion some other field or combination of fields. These *could* be modeled 204bf215546Sopenharmony_cias different derived bitsets, at the expense of a combinatorical explosion 205bf215546Sopenharmony_ciof the size of the bitset inheritance tree. For example, *every* cat2 206bf215546Sopenharmony_ci(and cat3) instruction has both a ``(nopN)`` interpretation in addtion to 207bf215546Sopenharmony_cithe ``(rptN`)`` interpretation. 208bf215546Sopenharmony_ci 209bf215546Sopenharmony_ciAn ``<override>`` in a bitset allows to redefine the display string, and/or 210bf215546Sopenharmony_cifield definitions from the default case. If the override's expr(ession) 211bf215546Sopenharmony_cievaluates to non-zero, ``<display>``, ``<field>``, and ``<derived>`` 212bf215546Sopenharmony_cielements take precedence over what is defined in the toplevel of the 213bf215546Sopenharmony_cibitset (ie. the default case). 214bf215546Sopenharmony_ci 215bf215546Sopenharmony_ciExpressions 216bf215546Sopenharmony_ci----------- 217bf215546Sopenharmony_ci 218bf215546Sopenharmony_ciBoth ``<override>`` and ``<derived>`` fields make use of ``<expr>`` elements, 219bf215546Sopenharmony_cieither defined inline, or defined and named at the top level and referred to 220bf215546Sopenharmony_ciby name in multiple other places. An expression is a simple 'C' expression 221bf215546Sopenharmony_ciwhich can reference fields (including other derived fields) with the same 222bf215546Sopenharmony_ci``{FIELDNAME}`` syntax as display template strings. For example: 223bf215546Sopenharmony_ci 224bf215546Sopenharmony_ci.. code-block:: xml 225bf215546Sopenharmony_ci 226bf215546Sopenharmony_ci <expr name="#cat2-cat3-nop-encoding"> 227bf215546Sopenharmony_ci (({SRC1_R} != 0) || ({SRC2_R} != 0)) && ({REPEAT} == 0) 228bf215546Sopenharmony_ci </expr> 229bf215546Sopenharmony_ci 230bf215546Sopenharmony_ciIn the case of ``<override>`` elements, the override applies if the expression 231bf215546Sopenharmony_cievaluates to non-zero. In the case of ``<derived>`` fields, the expression 232bf215546Sopenharmony_cievaluates to the value of the derived field. 233bf215546Sopenharmony_ci 234bf215546Sopenharmony_ciEncoding 235bf215546Sopenharmony_ci-------- 236bf215546Sopenharmony_ci 237bf215546Sopenharmony_ciTo facilitate instruction encoding, ``<encode>`` elements can be provided 238bf215546Sopenharmony_cito teach the generated instruction packing code how to map from data structures 239bf215546Sopenharmony_cirepresenting the IR to fields. For example: 240bf215546Sopenharmony_ci 241bf215546Sopenharmony_ci.. code-block:: xml 242bf215546Sopenharmony_ci 243bf215546Sopenharmony_ci <bitset name="#instruction" size="64"> 244bf215546Sopenharmony_ci <doc> 245bf215546Sopenharmony_ci Encoding of an ir3 instruction. All instructions are 64b. 246bf215546Sopenharmony_ci </doc> 247bf215546Sopenharmony_ci <gen min="300"/> 248bf215546Sopenharmony_ci <encode type="struct ir3_instruction *" case-prefix="OPC_"> 249bf215546Sopenharmony_ci <!-- 250bf215546Sopenharmony_ci Define mapping from encode src to individual fields, 251bf215546Sopenharmony_ci which are common across all instruction categories 252bf215546Sopenharmony_ci at the root instruction level 253bf215546Sopenharmony_ci 254bf215546Sopenharmony_ci Not all of these apply to all instructions, but we 255bf215546Sopenharmony_ci can define mappings here for anything that is used 256bf215546Sopenharmony_ci in more than one instruction category. For things 257bf215546Sopenharmony_ci that are specific to a single instruction category, 258bf215546Sopenharmony_ci mappings should be defined at that level instead. 259bf215546Sopenharmony_ci --> 260bf215546Sopenharmony_ci <map name="DST">src->regs[0]</map> 261bf215546Sopenharmony_ci <map name="SRC1">src->regs[1]</map> 262bf215546Sopenharmony_ci <map name="SRC2">src->regs[2]</map> 263bf215546Sopenharmony_ci <map name="SRC3">src->regs[3]</map> 264bf215546Sopenharmony_ci <map name="REPEAT">src->repeat</map> 265bf215546Sopenharmony_ci <map name="SS">!!(src->flags & IR3_INSTR_SS)</map> 266bf215546Sopenharmony_ci <map name="JP">!!(src->flags & IR3_INSTR_JP)</map> 267bf215546Sopenharmony_ci <map name="SY">!!(src->flags & IR3_INSTR_SY)</map> 268bf215546Sopenharmony_ci <map name="UL">!!(src->flags & IR3_INSTR_UL)</map> 269bf215546Sopenharmony_ci <map name="EQ">0</map> <!-- We don't use this (yet) --> 270bf215546Sopenharmony_ci <map name="SAT">!!(src->flags & IR3_INSTR_SAT)</map> 271bf215546Sopenharmony_ci </encode> 272bf215546Sopenharmony_ci </bitset> 273bf215546Sopenharmony_ci 274bf215546Sopenharmony_ciThe ``type`` attribute specifies that the input to encoding an instruction 275bf215546Sopenharmony_ciis a ``struct ir3_instruction *``. In the case of bitset hierarchies with 276bf215546Sopenharmony_cimultiple possible leaf nodes, a ``case-prefix`` attribute should be supplied 277bf215546Sopenharmony_cialong with a function that maps the bitset encode source to an enum value 278bf215546Sopenharmony_ciwith the specified prefix prepended to uppercase'd leaf node name. Ie. in 279bf215546Sopenharmony_cithis case, "add.f" becomes ``OPC_ADD_F``. 280bf215546Sopenharmony_ci 281bf215546Sopenharmony_ciIndividual ``<map>`` elements teach the encoder how to map from the encode 282bf215546Sopenharmony_cisource to fields in the encoded instruction. 283