blob: 48f6cf8762df65ac3e62c58273a45baf05135668 [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>;
36def InstFormatI : InstFormat<2>;
37def InstFormatS : InstFormat<3>;
Alex Bradburyee7c7ec2017-10-19 14:29:03 +000038def InstFormatB : InstFormat<4>;
Alex Bradbury9d3f1252017-09-28 08:26:24 +000039def InstFormatU : InstFormat<5>;
Alex Bradburyee7c7ec2017-10-19 14:29:03 +000040def InstFormatJ : InstFormat<6>;
41def InstFormatOther : InstFormat<7>;
Alex Bradbury9d3f1252017-09-28 08:26:24 +000042
Alex Bradburyee7c7ec2017-10-19 14:29:03 +000043// The following opcode names and match those given in Table 19.1 in the
44// RISC-V User-level ISA specification ("RISC-V base opcode map").
45class RISCVOpcode<bits<7> val> {
46 bits<7> Value = val;
47}
48def OPC_LOAD : RISCVOpcode<0b0000011>;
49def OPC_LOAD_FP : RISCVOpcode<0b0000111>;
50def OPC_MISC_MEM : RISCVOpcode<0b0001111>;
51def OPC_OP_IMM : RISCVOpcode<0b0010011>;
52def OPC_AUIPC : RISCVOpcode<0b0010111>;
53def OPC_OP_IMM_32 : RISCVOpcode<0b0011011>;
54def OPC_STORE : RISCVOpcode<0b0100011>;
55def OPC_STORE_FP : RISCVOpcode<0b0100111>;
56def OPC_AMO : RISCVOpcode<0b0101111>;
57def OPC_OP : RISCVOpcode<0b0110011>;
58def OPC_LUI : RISCVOpcode<0b0110111>;
59def OPC_OP_32 : RISCVOpcode<0b0111011>;
60def OPC_MADD : RISCVOpcode<0b1000011>;
61def OPC_MSUB : RISCVOpcode<0b1000111>;
62def OPC_NMSUB : RISCVOpcode<0b1001011>;
63def OPC_NMADD : RISCVOpcode<0b1001111>;
64def OPC_OP_FP : RISCVOpcode<0b1010011>;
65def OPC_BRANCH : RISCVOpcode<0b1100011>;
66def OPC_JALR : RISCVOpcode<0b1100111>;
67def OPC_JAL : RISCVOpcode<0b1101111>;
68def OPC_SYSTEM : RISCVOpcode<0b1110011>;
69
70class RVInst<dag outs, dag ins, string opcodestr, string argstr,
71 list<dag> pattern, InstFormat format>
Alex Bradbury24d9b132016-11-01 23:40:28 +000072 : Instruction {
73 field bits<32> Inst;
Alex Bradbury8ab4a962017-09-17 14:36:28 +000074 // SoftFail is a field the disassembler can use to provide a way for
75 // instructions to not match without killing the whole decode process. It is
76 // mainly used for ARM, but Tablegen expects this field to exist or it fails
77 // to build the decode table.
78 field bits<32> SoftFail = 0;
Alex Bradbury24d9b132016-11-01 23:40:28 +000079 let Size = 4;
80
81 bits<7> Opcode = 0;
82
83 let Inst{6-0} = Opcode;
84
85 let Namespace = "RISCV";
86
87 dag OutOperandList = outs;
88 dag InOperandList = ins;
Alex Bradburyee7c7ec2017-10-19 14:29:03 +000089 let AsmString = opcodestr # "\t" # argstr;
Alex Bradbury24d9b132016-11-01 23:40:28 +000090 let Pattern = pattern;
Alex Bradbury9d3f1252017-09-28 08:26:24 +000091
92 let TSFlags{3-0} = format.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +000093}
94
95// Pseudo instructions
Alex Bradbury9d3f1252017-09-28 08:26:24 +000096class Pseudo<dag outs, dag ins, list<dag> pattern>
Alex Bradburyee7c7ec2017-10-19 14:29:03 +000097 : RVInst<outs, ins, "", "", pattern, InstFormatPseudo> {
Alex Bradbury24d9b132016-11-01 23:40:28 +000098 let isPseudo = 1;
Alex Bradbury6be16fb2017-02-14 05:17:23 +000099 let isCodeGenOnly = 1;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000100}
101
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000102// Instruction formats are listed in the order they appear in the RISC-V
103// instruction set manual (R, I, S, B, U, J) with sub-formats (e.g. RVInstR4,
104// RVInstRAtomic) sorted alphabetically.
105
106class RVInstR<bits<7> funct7, bits<3> funct3, RISCVOpcode opcode, dag outs,
107 dag ins, string opcodestr, string argstr>
108 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR> {
Alex Bradbury24d9b132016-11-01 23:40:28 +0000109 bits<5> rs2;
110 bits<5> rs1;
111 bits<5> rd;
112
113 let Inst{31-25} = funct7;
114 let Inst{24-20} = rs2;
115 let Inst{19-15} = rs1;
116 let Inst{14-12} = funct3;
117 let Inst{11-7} = rd;
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000118 let Opcode = opcode.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000119}
120
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000121class RVInstI<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
122 string opcodestr, string argstr>
123 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatI> {
Alex Bradbury24d9b132016-11-01 23:40:28 +0000124 bits<12> imm12;
125 bits<5> rs1;
126 bits<5> rd;
127
128 let Inst{31-20} = imm12;
129 let Inst{19-15} = rs1;
130 let Inst{14-12} = funct3;
131 let Inst{11-7} = rd;
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000132 let Opcode = opcode.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000133}
134
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000135class RVInstIShift<bit arithshift, bits<3> funct3, RISCVOpcode opcode,
136 dag outs, dag ins, string opcodestr, string argstr>
137 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatI> {
Alex Bradbury24d9b132016-11-01 23:40:28 +0000138 bits<5> shamt;
139 bits<5> rs1;
140 bits<5> rd;
141
142 let Inst{31} = 0;
143 let Inst{30} = arithshift;
144 let Inst{29-25} = 0;
145 let Inst{24-20} = shamt;
146 let Inst{19-15} = rs1;
147 let Inst{14-12} = funct3;
148 let Inst{11-7} = rd;
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000149 let Opcode = opcode.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000150}
151
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000152class RVInstS<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
153 string opcodestr, string argstr>
154 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatS> {
Alex Bradbury24d9b132016-11-01 23:40:28 +0000155 bits<12> imm12;
156 bits<5> rs2;
157 bits<5> rs1;
158
159 let Inst{31-25} = imm12{11-5};
160 let Inst{24-20} = rs2;
161 let Inst{19-15} = rs1;
162 let Inst{14-12} = funct3;
163 let Inst{11-7} = imm12{4-0};
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000164 let Opcode = opcode.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000165}
166
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000167class RVInstB<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
168 string opcodestr, string argstr>
169 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatB> {
Alex Bradbury24d9b132016-11-01 23:40:28 +0000170 bits<12> imm12;
171 bits<5> rs2;
172 bits<5> rs1;
173
174 let Inst{31} = imm12{11};
175 let Inst{30-25} = imm12{9-4};
176 let Inst{24-20} = rs2;
177 let Inst{19-15} = rs1;
178 let Inst{14-12} = funct3;
179 let Inst{11-8} = imm12{3-0};
180 let Inst{7} = imm12{10};
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000181 let Opcode = opcode.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000182}
183
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000184class RVInstU<RISCVOpcode opcode, dag outs, dag ins, string opcodestr,
185 string argstr>
186 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatU> {
Alex Bradbury24d9b132016-11-01 23:40:28 +0000187 bits<20> imm20;
188 bits<5> rd;
189
190 let Inst{31-12} = imm20;
191 let Inst{11-7} = rd;
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000192 let Opcode = opcode.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000193}
194
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000195class RVInstJ<RISCVOpcode opcode, dag outs, dag ins, string opcodestr,
196 string argstr>
197 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatJ> {
Alex Bradbury24d9b132016-11-01 23:40:28 +0000198 bits<20> imm20;
199 bits<5> rd;
200
201 let Inst{31} = imm20{19};
202 let Inst{30-21} = imm20{9-0};
203 let Inst{20} = imm20{10};
204 let Inst{19-12} = imm20{18-11};
205 let Inst{11-7} = rd;
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000206 let Opcode = opcode.Value;
Alex Bradbury24d9b132016-11-01 23:40:28 +0000207}