1 //===-- RISCVInstrFormats.td - RISCV Instruction Formats ---*- tablegen -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 //===----------------------------------------------------------------------===//
11 // These instruction format definitions are structured to match the
12 // description in the RISC-V User-Level ISA specification as closely as
13 // possible. For instance, the specification describes instructions with the
14 // MSB (31st bit) on the left and the LSB (0th bit) on the right. This is
15 // reflected in the order of parameters to each instruction class.
17 // One area of divergence is in the description of immediates. The
18 // specification describes immediate encoding in terms of bit-slicing
19 // operations on the logical value represented. The immediate argument to
20 // these instruction formats instead represents the bit sequence that will be
21 // inserted into the instruction. e.g. although JAL's immediate is logically
22 // a 21-bit value (where the LSB is always zero), we describe it as an imm20
23 // to match how it is encoded.
25 //===----------------------------------------------------------------------===//
27 // Format specifies the encoding used by the instruction. This is used by
28 // RISCVMCCodeEmitter to determine which form of fixup to use. These
29 // definitions must be kept in-sync with RISCVBaseInfo.h.
30 class InstFormat<bits<5> val> {
33 def InstFormatPseudo : InstFormat<0>;
34 def InstFormatR : InstFormat<1>;
35 def InstFormatR4 : InstFormat<2>;
36 def InstFormatI : InstFormat<3>;
37 def InstFormatS : InstFormat<4>;
38 def InstFormatB : InstFormat<5>;
39 def InstFormatU : InstFormat<6>;
40 def InstFormatJ : InstFormat<7>;
41 def InstFormatCR : InstFormat<8>;
42 def InstFormatCI : InstFormat<9>;
43 def InstFormatCSS : InstFormat<10>;
44 def InstFormatCIW : InstFormat<11>;
45 def InstFormatCL : InstFormat<12>;
46 def InstFormatCS : InstFormat<13>;
47 def InstFormatCA : InstFormat<14>;
48 def InstFormatCB : InstFormat<15>;
49 def InstFormatCJ : InstFormat<16>;
50 def InstFormatOther : InstFormat<17>;
52 class RISCVVConstraint<bits<4> val> {
55 def NoConstraint : RISCVVConstraint<0>;
56 def WidenV : RISCVVConstraint<1>;
57 def WidenW : RISCVVConstraint<2>;
58 def WidenCvt : RISCVVConstraint<3>;
59 def Narrow : RISCVVConstraint<4>;
60 def Iota : RISCVVConstraint<5>;
61 def SlideUp : RISCVVConstraint<6>;
62 def Vrgather : RISCVVConstraint<7>;
63 def Vcompress : RISCVVConstraint<8>;
65 // The following opcode names match those given in Table 19.1 in the
66 // RISC-V User-level ISA specification ("RISC-V base opcode map").
67 class RISCVOpcode<bits<7> val> {
70 def OPC_LOAD : RISCVOpcode<0b0000011>;
71 def OPC_LOAD_FP : RISCVOpcode<0b0000111>;
72 def OPC_MISC_MEM : RISCVOpcode<0b0001111>;
73 def OPC_OP_IMM : RISCVOpcode<0b0010011>;
74 def OPC_AUIPC : RISCVOpcode<0b0010111>;
75 def OPC_OP_IMM_32 : RISCVOpcode<0b0011011>;
76 def OPC_STORE : RISCVOpcode<0b0100011>;
77 def OPC_STORE_FP : RISCVOpcode<0b0100111>;
78 def OPC_AMO : RISCVOpcode<0b0101111>;
79 def OPC_OP : RISCVOpcode<0b0110011>;
80 def OPC_LUI : RISCVOpcode<0b0110111>;
81 def OPC_OP_32 : RISCVOpcode<0b0111011>;
82 def OPC_MADD : RISCVOpcode<0b1000011>;
83 def OPC_MSUB : RISCVOpcode<0b1000111>;
84 def OPC_NMSUB : RISCVOpcode<0b1001011>;
85 def OPC_NMADD : RISCVOpcode<0b1001111>;
86 def OPC_OP_FP : RISCVOpcode<0b1010011>;
87 def OPC_OP_V : RISCVOpcode<0b1010111>;
88 def OPC_BRANCH : RISCVOpcode<0b1100011>;
89 def OPC_JALR : RISCVOpcode<0b1100111>;
90 def OPC_JAL : RISCVOpcode<0b1101111>;
91 def OPC_SYSTEM : RISCVOpcode<0b1110011>;
93 class RVInst<dag outs, dag ins, string opcodestr, string argstr,
94 list<dag> pattern, InstFormat format>
97 // SoftFail is a field the disassembler can use to provide a way for
98 // instructions to not match without killing the whole decode process. It is
99 // mainly used for ARM, but Tablegen expects this field to exist or it fails
100 // to build the decode table.
101 field bits<32> SoftFail = 0;
106 let Inst{6-0} = Opcode;
108 let Namespace = "RISCV";
110 dag OutOperandList = outs;
111 dag InOperandList = ins;
112 let AsmString = opcodestr # "\t" # argstr;
113 let Pattern = pattern;
115 let TSFlags{4-0} = format.Value;
118 RISCVVConstraint RVVConstraint = NoConstraint;
119 let TSFlags{8-5} = RVVConstraint.Value;
122 // Pseudo instructions
123 class Pseudo<dag outs, dag ins, list<dag> pattern, string opcodestr = "", string argstr = "">
124 : RVInst<outs, ins, opcodestr, argstr, pattern, InstFormatPseudo>,
127 let isCodeGenOnly = 1;
130 // Pseudo load instructions.
131 class PseudoLoad<string opcodestr, RegisterClass rdty = GPR>
132 : Pseudo<(outs rdty:$rd), (ins bare_symbol:$addr), [], opcodestr, "$rd, $addr"> {
133 let hasSideEffects = 0;
136 let isCodeGenOnly = 0;
137 let isAsmParserOnly = 1;
140 class PseudoFloatLoad<string opcodestr, RegisterClass rdty = GPR>
141 : Pseudo<(outs rdty:$rd, GPR:$tmp), (ins bare_symbol:$addr), [], opcodestr, "$rd, $addr, $tmp"> {
142 let hasSideEffects = 0;
145 let isCodeGenOnly = 0;
146 let isAsmParserOnly = 1;
149 // Pseudo store instructions.
150 class PseudoStore<string opcodestr, RegisterClass rsty = GPR>
151 : Pseudo<(outs rsty:$rs, GPR:$tmp), (ins bare_symbol:$addr), [], opcodestr, "$rs, $addr, $tmp"> {
152 let hasSideEffects = 0;
155 let isCodeGenOnly = 0;
156 let isAsmParserOnly = 1;
159 // Instruction formats are listed in the order they appear in the RISC-V
160 // instruction set manual (R, I, S, B, U, J) with sub-formats (e.g. RVInstR4,
161 // RVInstRAtomic) sorted alphabetically.
163 class RVInstR<bits<7> funct7, bits<3> funct3, RISCVOpcode opcode, dag outs,
164 dag ins, string opcodestr, string argstr>
165 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR> {
170 let Inst{31-25} = funct7;
171 let Inst{24-20} = rs2;
172 let Inst{19-15} = rs1;
173 let Inst{14-12} = funct3;
175 let Opcode = opcode.Value;
178 class RVInstR4<bits<2> funct2, RISCVOpcode opcode, dag outs, dag ins,
179 string opcodestr, string argstr>
180 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR4> {
187 let Inst{31-27} = rs3;
188 let Inst{26-25} = funct2;
189 let Inst{24-20} = rs2;
190 let Inst{19-15} = rs1;
191 let Inst{14-12} = funct3;
193 let Opcode = opcode.Value;
196 class RVInstRAtomic<bits<5> funct5, bit aq, bit rl, bits<3> funct3,
197 RISCVOpcode opcode, dag outs, dag ins, string opcodestr,
199 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR> {
204 let Inst{31-27} = funct5;
207 let Inst{24-20} = rs2;
208 let Inst{19-15} = rs1;
209 let Inst{14-12} = funct3;
211 let Opcode = opcode.Value;
214 class RVInstRFrm<bits<7> funct7, RISCVOpcode opcode, dag outs, dag ins,
215 string opcodestr, string argstr>
216 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR> {
222 let Inst{31-25} = funct7;
223 let Inst{24-20} = rs2;
224 let Inst{19-15} = rs1;
225 let Inst{14-12} = funct3;
227 let Opcode = opcode.Value;
230 class RVInstI<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
231 string opcodestr, string argstr>
232 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatI> {
237 let Inst{31-20} = imm12;
238 let Inst{19-15} = rs1;
239 let Inst{14-12} = funct3;
241 let Opcode = opcode.Value;
244 class RVInstIShift<bit arithshift, bits<3> funct3, RISCVOpcode opcode,
245 dag outs, dag ins, string opcodestr, string argstr>
246 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatI> {
252 let Inst{30} = arithshift;
254 let Inst{25-20} = shamt;
255 let Inst{19-15} = rs1;
256 let Inst{14-12} = funct3;
258 let Opcode = opcode.Value;
261 class RVInstIShiftW<bit arithshift, bits<3> funct3, RISCVOpcode opcode,
262 dag outs, dag ins, string opcodestr, string argstr>
263 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatI> {
269 let Inst{30} = arithshift;
271 let Inst{24-20} = shamt;
272 let Inst{19-15} = rs1;
273 let Inst{14-12} = funct3;
275 let Opcode = opcode.Value;
278 class RVInstS<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
279 string opcodestr, string argstr>
280 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatS> {
285 let Inst{31-25} = imm12{11-5};
286 let Inst{24-20} = rs2;
287 let Inst{19-15} = rs1;
288 let Inst{14-12} = funct3;
289 let Inst{11-7} = imm12{4-0};
290 let Opcode = opcode.Value;
293 class RVInstB<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
294 string opcodestr, string argstr>
295 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatB> {
300 let Inst{31} = imm12{11};
301 let Inst{30-25} = imm12{9-4};
302 let Inst{24-20} = rs2;
303 let Inst{19-15} = rs1;
304 let Inst{14-12} = funct3;
305 let Inst{11-8} = imm12{3-0};
306 let Inst{7} = imm12{10};
307 let Opcode = opcode.Value;
310 class RVInstU<RISCVOpcode opcode, dag outs, dag ins, string opcodestr,
312 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatU> {
316 let Inst{31-12} = imm20;
318 let Opcode = opcode.Value;
321 class RVInstJ<RISCVOpcode opcode, dag outs, dag ins, string opcodestr,
323 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatJ> {
327 let Inst{31} = imm20{19};
328 let Inst{30-21} = imm20{9-0};
329 let Inst{20} = imm20{10};
330 let Inst{19-12} = imm20{18-11};
332 let Opcode = opcode.Value;