blob: ca36879234004cb2b6cdb7d2e267af80332a1cbc [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.
31class InstFormat<bits<4> val> {
32 bits<4> Value = val;
33}
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>;
42def InstFormatOther : InstFormat<8>;
Alex Bradbury9d3f1252017-09-28 08:26:24 +000043
Alex Bradburyee7c7ec2017-10-19 14:29:03 +000044// The following opcode names and match those given in Table 19.1 in the
45// RISC-V User-level ISA specification ("RISC-V base opcode map").
46class RISCVOpcode<bits<7> val> {
47 bits<7> Value = val;
48}
49def OPC_LOAD : RISCVOpcode<0b0000011>;
50def OPC_LOAD_FP : RISCVOpcode<0b0000111>;
51def OPC_MISC_MEM : RISCVOpcode<0b0001111>;
52def OPC_OP_IMM : RISCVOpcode<0b0010011>;
53def OPC_AUIPC : RISCVOpcode<0b0010111>;
54def OPC_OP_IMM_32 : RISCVOpcode<0b0011011>;
55def OPC_STORE : RISCVOpcode<0b0100011>;
56def OPC_STORE_FP : RISCVOpcode<0b0100111>;
57def OPC_AMO : RISCVOpcode<0b0101111>;
58def OPC_OP : RISCVOpcode<0b0110011>;
59def OPC_LUI : RISCVOpcode<0b0110111>;
60def OPC_OP_32 : RISCVOpcode<0b0111011>;
61def OPC_MADD : RISCVOpcode<0b1000011>;
62def OPC_MSUB : RISCVOpcode<0b1000111>;
63def OPC_NMSUB : RISCVOpcode<0b1001011>;
64def OPC_NMADD : RISCVOpcode<0b1001111>;
65def OPC_OP_FP : RISCVOpcode<0b1010011>;
66def OPC_BRANCH : RISCVOpcode<0b1100011>;
67def OPC_JALR : RISCVOpcode<0b1100111>;
68def OPC_JAL : RISCVOpcode<0b1101111>;
69def OPC_SYSTEM : RISCVOpcode<0b1110011>;
70
71class RVInst<dag outs, dag ins, string opcodestr, string argstr,
72 list<dag> pattern, InstFormat format>
Alex Bradbury24d9b132016-11-01 23:40:28 +000073 : Instruction {
74 field bits<32> Inst;
Alex Bradbury8ab4a962017-09-17 14:36:28 +000075 // SoftFail is a field the disassembler can use to provide a way for
76 // instructions to not match without killing the whole decode process. It is
77 // mainly used for ARM, but Tablegen expects this field to exist or it fails
78 // to build the decode table.
79 field bits<32> SoftFail = 0;
Alex Bradbury24d9b132016-11-01 23:40:28 +000080 let Size = 4;
81
82 bits<7> Opcode = 0;
83
84 let Inst{6-0} = Opcode;
85
86 let Namespace = "RISCV";
87
88 dag OutOperandList = outs;
89 dag InOperandList = ins;
Alex Bradburyee7c7ec2017-10-19 14:29:03 +000090 let AsmString = opcodestr # "\t" # argstr;
Alex Bradbury24d9b132016-11-01 23:40:28 +000091 let Pattern = pattern;
Alex Bradbury9d3f1252017-09-28 08:26:24 +000092
93 let TSFlags{3-0} = format.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +000094}
95
96// Pseudo instructions
Alex Bradbury9d3f1252017-09-28 08:26:24 +000097class Pseudo<dag outs, dag ins, list<dag> pattern>
Alex Bradburyee7c7ec2017-10-19 14:29:03 +000098 : RVInst<outs, ins, "", "", pattern, InstFormatPseudo> {
Alex Bradbury24d9b132016-11-01 23:40:28 +000099 let isPseudo = 1;
Alex Bradbury6be16fb2017-02-14 05:17:23 +0000100 let isCodeGenOnly = 1;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000101}
102
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000103// Instruction formats are listed in the order they appear in the RISC-V
104// instruction set manual (R, I, S, B, U, J) with sub-formats (e.g. RVInstR4,
105// RVInstRAtomic) sorted alphabetically.
106
107class RVInstR<bits<7> funct7, bits<3> funct3, RISCVOpcode opcode, dag outs,
108 dag ins, string opcodestr, string argstr>
109 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR> {
Alex Bradbury24d9b132016-11-01 23:40:28 +0000110 bits<5> rs2;
111 bits<5> rs1;
112 bits<5> rd;
113
114 let Inst{31-25} = funct7;
115 let Inst{24-20} = rs2;
116 let Inst{19-15} = rs1;
117 let Inst{14-12} = funct3;
118 let Inst{11-7} = rd;
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000119 let Opcode = opcode.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000120}
121
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000122class RVInstR4<bits<2> funct2, RISCVOpcode opcode, dag outs, dag ins,
123 string opcodestr, string argstr>
124 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR4> {
125 bits<5> rs3;
126 bits<5> rs2;
127 bits<5> rs1;
128 bits<3> funct3;
129 bits<5> rd;
130
131 let Inst{31-27} = rs3;
132 let Inst{26-25} = funct2;
133 let Inst{24-20} = rs2;
134 let Inst{19-15} = rs1;
135 let Inst{14-12} = funct3;
136 let Inst{11-7} = rd;
137 let Opcode = opcode.Value;
138}
139
Alex Bradbury8c345c52017-11-09 15:00:03 +0000140class RVInstRAtomic<bits<5> funct5, bit aq, bit rl, bits<3> funct3,
141 RISCVOpcode opcode, dag outs, dag ins, string opcodestr,
142 string argstr>
143 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR> {
144 bits<5> rs2;
145 bits<5> rs1;
146 bits<5> rd;
147
148 let Inst{31-27} = funct5;
149 let Inst{26} = aq;
150 let Inst{25} = rl;
151 let Inst{24-20} = rs2;
152 let Inst{19-15} = rs1;
153 let Inst{14-12} = funct3;
154 let Inst{11-7} = rd;
155 let Opcode = opcode.Value;
156}
157
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000158class RVInstRFrm<bits<7> funct7, RISCVOpcode opcode, dag outs, dag ins,
159 string opcodestr, string argstr>
160 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR> {
161 bits<5> rs2;
162 bits<5> rs1;
163 bits<3> funct3;
164 bits<5> rd;
165
166 let Inst{31-25} = funct7;
167 let Inst{24-20} = rs2;
168 let Inst{19-15} = rs1;
169 let Inst{14-12} = funct3;
170 let Inst{11-7} = rd;
171 let Opcode = opcode.Value;
172}
173
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000174class RVInstI<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
175 string opcodestr, string argstr>
176 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatI> {
Alex Bradbury24d9b132016-11-01 23:40:28 +0000177 bits<12> imm12;
178 bits<5> rs1;
179 bits<5> rd;
180
181 let Inst{31-20} = imm12;
182 let Inst{19-15} = rs1;
183 let Inst{14-12} = funct3;
184 let Inst{11-7} = rd;
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000185 let Opcode = opcode.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000186}
187
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000188class RVInstIShift<bit arithshift, bits<3> funct3, RISCVOpcode opcode,
189 dag outs, dag ins, string opcodestr, string argstr>
190 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatI> {
Alex Bradbury24d9b132016-11-01 23:40:28 +0000191 bits<5> shamt;
192 bits<5> rs1;
193 bits<5> rd;
194
195 let Inst{31} = 0;
196 let Inst{30} = arithshift;
197 let Inst{29-25} = 0;
198 let Inst{24-20} = shamt;
199 let Inst{19-15} = rs1;
200 let Inst{14-12} = funct3;
201 let Inst{11-7} = rd;
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000202 let Opcode = opcode.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000203}
204
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000205class RVInstS<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
206 string opcodestr, string argstr>
207 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatS> {
Alex Bradbury24d9b132016-11-01 23:40:28 +0000208 bits<12> imm12;
209 bits<5> rs2;
210 bits<5> rs1;
211
212 let Inst{31-25} = imm12{11-5};
213 let Inst{24-20} = rs2;
214 let Inst{19-15} = rs1;
215 let Inst{14-12} = funct3;
216 let Inst{11-7} = imm12{4-0};
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000217 let Opcode = opcode.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000218}
219
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000220class RVInstB<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
221 string opcodestr, string argstr>
222 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatB> {
Alex Bradbury24d9b132016-11-01 23:40:28 +0000223 bits<12> imm12;
224 bits<5> rs2;
225 bits<5> rs1;
226
227 let Inst{31} = imm12{11};
228 let Inst{30-25} = imm12{9-4};
229 let Inst{24-20} = rs2;
230 let Inst{19-15} = rs1;
231 let Inst{14-12} = funct3;
232 let Inst{11-8} = imm12{3-0};
233 let Inst{7} = imm12{10};
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000234 let Opcode = opcode.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000235}
236
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000237class RVInstU<RISCVOpcode opcode, dag outs, dag ins, string opcodestr,
238 string argstr>
239 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatU> {
Alex Bradbury24d9b132016-11-01 23:40:28 +0000240 bits<20> imm20;
241 bits<5> rd;
242
243 let Inst{31-12} = imm20;
244 let Inst{11-7} = rd;
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000245 let Opcode = opcode.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000246}
247
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000248class RVInstJ<RISCVOpcode opcode, dag outs, dag ins, string opcodestr,
249 string argstr>
250 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatJ> {
Alex Bradbury24d9b132016-11-01 23:40:28 +0000251 bits<20> imm20;
252 bits<5> rd;
253
254 let Inst{31} = imm20{19};
255 let Inst{30-21} = imm20{9-0};
256 let Inst{20} = imm20{10};
257 let Inst{19-12} = imm20{18-11};
258 let Inst{11-7} = rd;
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000259 let Opcode = opcode.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000260}