blob: ebd676a6056ef366917c7e8a01548d07d498f20f [file] [log] [blame]
Alex Bradbury24d9b132016-11-01 23:40:28 +00001//===-- RISCVInstrFormats.td - RISCV Instruction Formats ---*- tablegen -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10//===----------------------------------------------------------------------===//
11//
12// These instruction format definitions are structured to match the
13// description in the RISC-V User-Level ISA specification as closely as
14// possible. For instance, the specification describes instructions with the
15// MSB (31st bit) on the left and the LSB (0th bit) on the right. This is
16// reflected in the order of parameters to each instruction class.
17//
18// One area of divergence is in the description of immediates. The
19// specification describes immediate encoding in terms of bit-slicing
20// operations on the logical value represented. The immediate argument to
21// these instruction formats instead represents the bit sequence that will be
22// inserted into the instruction. e.g. although JAL's immediate is logically
23// a 21-bit value (where the LSB is always zero), we describe it as an imm20
24// to match how it is encoded.
25//
26//===----------------------------------------------------------------------===//
27
Alex Bradbury9d3f1252017-09-28 08:26:24 +000028// Format specifies the encoding used by the instruction. This is used by
29// RISCVMCCodeEmitter to determine which form of fixup to use. These
30// definitions must be kept in-sync with RISCVBaseInfo.h.
Alex Bradbury9f6aec42017-12-07 12:50:32 +000031class InstFormat<bits<5> val> {
32 bits<5> Value = val;
Alex Bradbury9d3f1252017-09-28 08:26:24 +000033}
34def InstFormatPseudo : InstFormat<0>;
35def InstFormatR : InstFormat<1>;
Alex Bradbury0d6cf902017-12-07 10:26:05 +000036def InstFormatR4 : InstFormat<2>;
37def InstFormatI : InstFormat<3>;
38def InstFormatS : InstFormat<4>;
39def InstFormatB : InstFormat<5>;
40def InstFormatU : InstFormat<6>;
41def InstFormatJ : InstFormat<7>;
Alex Bradbury9f6aec42017-12-07 12:50:32 +000042def InstFormatCR : InstFormat<8>;
43def InstFormatCI : InstFormat<9>;
44def InstFormatCSS : InstFormat<10>;
45def InstFormatCIW : InstFormat<11>;
46def InstFormatCL : InstFormat<12>;
47def InstFormatCS : InstFormat<13>;
Alex Bradburyb4a64ce2018-11-16 10:33:23 +000048def InstFormatCA : InstFormat<14>;
49def InstFormatCB : InstFormat<15>;
50def InstFormatCJ : InstFormat<16>;
51def InstFormatOther : InstFormat<17>;
Alex Bradbury9d3f1252017-09-28 08:26:24 +000052
Alex Bradburyb4a64ce2018-11-16 10:33:23 +000053// The following opcode names match those given in Table 19.1 in the
Alex Bradburyee7c7ec2017-10-19 14:29:03 +000054// RISC-V User-level ISA specification ("RISC-V base opcode map").
55class RISCVOpcode<bits<7> val> {
56 bits<7> Value = val;
57}
58def OPC_LOAD : RISCVOpcode<0b0000011>;
59def OPC_LOAD_FP : RISCVOpcode<0b0000111>;
60def OPC_MISC_MEM : RISCVOpcode<0b0001111>;
61def OPC_OP_IMM : RISCVOpcode<0b0010011>;
62def OPC_AUIPC : RISCVOpcode<0b0010111>;
63def OPC_OP_IMM_32 : RISCVOpcode<0b0011011>;
64def OPC_STORE : RISCVOpcode<0b0100011>;
65def OPC_STORE_FP : RISCVOpcode<0b0100111>;
66def OPC_AMO : RISCVOpcode<0b0101111>;
67def OPC_OP : RISCVOpcode<0b0110011>;
68def OPC_LUI : RISCVOpcode<0b0110111>;
69def OPC_OP_32 : RISCVOpcode<0b0111011>;
70def OPC_MADD : RISCVOpcode<0b1000011>;
71def OPC_MSUB : RISCVOpcode<0b1000111>;
72def OPC_NMSUB : RISCVOpcode<0b1001011>;
73def OPC_NMADD : RISCVOpcode<0b1001111>;
74def OPC_OP_FP : RISCVOpcode<0b1010011>;
75def OPC_BRANCH : RISCVOpcode<0b1100011>;
76def OPC_JALR : RISCVOpcode<0b1100111>;
77def OPC_JAL : RISCVOpcode<0b1101111>;
78def OPC_SYSTEM : RISCVOpcode<0b1110011>;
79
80class RVInst<dag outs, dag ins, string opcodestr, string argstr,
81 list<dag> pattern, InstFormat format>
Alex Bradbury24d9b132016-11-01 23:40:28 +000082 : Instruction {
83 field bits<32> Inst;
Alex Bradbury8ab4a962017-09-17 14:36:28 +000084 // SoftFail is a field the disassembler can use to provide a way for
85 // instructions to not match without killing the whole decode process. It is
86 // mainly used for ARM, but Tablegen expects this field to exist or it fails
87 // to build the decode table.
88 field bits<32> SoftFail = 0;
Alex Bradbury24d9b132016-11-01 23:40:28 +000089 let Size = 4;
90
91 bits<7> Opcode = 0;
92
93 let Inst{6-0} = Opcode;
94
95 let Namespace = "RISCV";
96
97 dag OutOperandList = outs;
98 dag InOperandList = ins;
Alex Bradburyee7c7ec2017-10-19 14:29:03 +000099 let AsmString = opcodestr # "\t" # argstr;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000100 let Pattern = pattern;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000101
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000102 let TSFlags{4-0} = format.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000103}
104
105// Pseudo instructions
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000106class Pseudo<dag outs, dag ins, list<dag> pattern, string opcodestr = "", string argstr = "">
107 : RVInst<outs, ins, opcodestr, argstr, pattern, InstFormatPseudo> {
Alex Bradbury24d9b132016-11-01 23:40:28 +0000108 let isPseudo = 1;
Alex Bradbury6be16fb2017-02-14 05:17:23 +0000109 let isCodeGenOnly = 1;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000110}
111
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000112// Instruction formats are listed in the order they appear in the RISC-V
113// instruction set manual (R, I, S, B, U, J) with sub-formats (e.g. RVInstR4,
114// RVInstRAtomic) sorted alphabetically.
115
116class RVInstR<bits<7> funct7, bits<3> funct3, RISCVOpcode opcode, dag outs,
117 dag ins, string opcodestr, string argstr>
118 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR> {
Alex Bradbury24d9b132016-11-01 23:40:28 +0000119 bits<5> rs2;
120 bits<5> rs1;
121 bits<5> rd;
122
123 let Inst{31-25} = funct7;
124 let Inst{24-20} = rs2;
125 let Inst{19-15} = rs1;
126 let Inst{14-12} = funct3;
127 let Inst{11-7} = rd;
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000128 let Opcode = opcode.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000129}
130
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000131class RVInstR4<bits<2> funct2, RISCVOpcode opcode, dag outs, dag ins,
132 string opcodestr, string argstr>
133 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR4> {
134 bits<5> rs3;
135 bits<5> rs2;
136 bits<5> rs1;
137 bits<3> funct3;
138 bits<5> rd;
139
140 let Inst{31-27} = rs3;
141 let Inst{26-25} = funct2;
142 let Inst{24-20} = rs2;
143 let Inst{19-15} = rs1;
144 let Inst{14-12} = funct3;
145 let Inst{11-7} = rd;
146 let Opcode = opcode.Value;
147}
148
Alex Bradbury8c345c52017-11-09 15:00:03 +0000149class RVInstRAtomic<bits<5> funct5, bit aq, bit rl, bits<3> funct3,
150 RISCVOpcode opcode, dag outs, dag ins, string opcodestr,
151 string argstr>
152 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR> {
153 bits<5> rs2;
154 bits<5> rs1;
155 bits<5> rd;
156
157 let Inst{31-27} = funct5;
158 let Inst{26} = aq;
159 let Inst{25} = rl;
160 let Inst{24-20} = rs2;
161 let Inst{19-15} = rs1;
162 let Inst{14-12} = funct3;
163 let Inst{11-7} = rd;
164 let Opcode = opcode.Value;
165}
166
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000167class RVInstRFrm<bits<7> funct7, RISCVOpcode opcode, dag outs, dag ins,
168 string opcodestr, string argstr>
169 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR> {
170 bits<5> rs2;
171 bits<5> rs1;
172 bits<3> funct3;
173 bits<5> rd;
174
175 let Inst{31-25} = funct7;
176 let Inst{24-20} = rs2;
177 let Inst{19-15} = rs1;
178 let Inst{14-12} = funct3;
179 let Inst{11-7} = rd;
180 let Opcode = opcode.Value;
181}
182
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000183class RVInstI<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
184 string opcodestr, string argstr>
185 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatI> {
Alex Bradbury24d9b132016-11-01 23:40:28 +0000186 bits<12> imm12;
187 bits<5> rs1;
188 bits<5> rd;
189
190 let Inst{31-20} = imm12;
191 let Inst{19-15} = rs1;
192 let Inst{14-12} = funct3;
193 let Inst{11-7} = rd;
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000194 let Opcode = opcode.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000195}
196
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000197class RVInstIShift<bit arithshift, bits<3> funct3, RISCVOpcode opcode,
198 dag outs, dag ins, string opcodestr, string argstr>
199 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatI> {
Alex Bradburya6e62482017-12-07 10:53:48 +0000200 bits<6> shamt;
201 bits<5> rs1;
202 bits<5> rd;
203
204 let Inst{31} = 0;
205 let Inst{30} = arithshift;
206 let Inst{29-26} = 0;
207 let Inst{25-20} = shamt;
208 let Inst{19-15} = rs1;
209 let Inst{14-12} = funct3;
210 let Inst{11-7} = rd;
211 let Opcode = opcode.Value;
212}
213
214class RVInstIShiftW<bit arithshift, bits<3> funct3, RISCVOpcode opcode,
215 dag outs, dag ins, string opcodestr, string argstr>
216 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatI> {
Alex Bradbury24d9b132016-11-01 23:40:28 +0000217 bits<5> shamt;
218 bits<5> rs1;
219 bits<5> rd;
220
221 let Inst{31} = 0;
222 let Inst{30} = arithshift;
223 let Inst{29-25} = 0;
224 let Inst{24-20} = shamt;
225 let Inst{19-15} = rs1;
226 let Inst{14-12} = funct3;
227 let Inst{11-7} = rd;
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000228 let Opcode = opcode.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000229}
230
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000231class RVInstS<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
232 string opcodestr, string argstr>
233 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatS> {
Alex Bradbury24d9b132016-11-01 23:40:28 +0000234 bits<12> imm12;
235 bits<5> rs2;
236 bits<5> rs1;
237
238 let Inst{31-25} = imm12{11-5};
239 let Inst{24-20} = rs2;
240 let Inst{19-15} = rs1;
241 let Inst{14-12} = funct3;
242 let Inst{11-7} = imm12{4-0};
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000243 let Opcode = opcode.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000244}
245
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000246class RVInstB<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
247 string opcodestr, string argstr>
248 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatB> {
Alex Bradbury24d9b132016-11-01 23:40:28 +0000249 bits<12> imm12;
250 bits<5> rs2;
251 bits<5> rs1;
252
253 let Inst{31} = imm12{11};
254 let Inst{30-25} = imm12{9-4};
255 let Inst{24-20} = rs2;
256 let Inst{19-15} = rs1;
257 let Inst{14-12} = funct3;
258 let Inst{11-8} = imm12{3-0};
259 let Inst{7} = imm12{10};
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000260 let Opcode = opcode.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000261}
262
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000263class RVInstU<RISCVOpcode opcode, dag outs, dag ins, string opcodestr,
264 string argstr>
265 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatU> {
Alex Bradbury24d9b132016-11-01 23:40:28 +0000266 bits<20> imm20;
267 bits<5> rd;
268
269 let Inst{31-12} = imm20;
270 let Inst{11-7} = rd;
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000271 let Opcode = opcode.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000272}
273
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000274class RVInstJ<RISCVOpcode opcode, dag outs, dag ins, string opcodestr,
275 string argstr>
276 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatJ> {
Alex Bradbury24d9b132016-11-01 23:40:28 +0000277 bits<20> imm20;
278 bits<5> rd;
279
280 let Inst{31} = imm20{19};
281 let Inst{30-21} = imm20{9-0};
282 let Inst{20} = imm20{10};
283 let Inst{19-12} = imm20{18-11};
284 let Inst{11-7} = rd;
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000285 let Opcode = opcode.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000286}