Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 1 | //===-- 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 Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 28 | // 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. |
| 31 | class InstFormat<bits<4> val> { |
| 32 | bits<4> Value = val; |
| 33 | } |
| 34 | def InstFormatPseudo : InstFormat<0>; |
| 35 | def InstFormatR : InstFormat<1>; |
Alex Bradbury | 0d6cf90 | 2017-12-07 10:26:05 +0000 | [diff] [blame^] | 36 | def InstFormatR4 : InstFormat<2>; |
| 37 | def InstFormatI : InstFormat<3>; |
| 38 | def InstFormatS : InstFormat<4>; |
| 39 | def InstFormatB : InstFormat<5>; |
| 40 | def InstFormatU : InstFormat<6>; |
| 41 | def InstFormatJ : InstFormat<7>; |
| 42 | def InstFormatOther : InstFormat<8>; |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 43 | |
Alex Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 44 | // 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"). |
| 46 | class RISCVOpcode<bits<7> val> { |
| 47 | bits<7> Value = val; |
| 48 | } |
| 49 | def OPC_LOAD : RISCVOpcode<0b0000011>; |
| 50 | def OPC_LOAD_FP : RISCVOpcode<0b0000111>; |
| 51 | def OPC_MISC_MEM : RISCVOpcode<0b0001111>; |
| 52 | def OPC_OP_IMM : RISCVOpcode<0b0010011>; |
| 53 | def OPC_AUIPC : RISCVOpcode<0b0010111>; |
| 54 | def OPC_OP_IMM_32 : RISCVOpcode<0b0011011>; |
| 55 | def OPC_STORE : RISCVOpcode<0b0100011>; |
| 56 | def OPC_STORE_FP : RISCVOpcode<0b0100111>; |
| 57 | def OPC_AMO : RISCVOpcode<0b0101111>; |
| 58 | def OPC_OP : RISCVOpcode<0b0110011>; |
| 59 | def OPC_LUI : RISCVOpcode<0b0110111>; |
| 60 | def OPC_OP_32 : RISCVOpcode<0b0111011>; |
| 61 | def OPC_MADD : RISCVOpcode<0b1000011>; |
| 62 | def OPC_MSUB : RISCVOpcode<0b1000111>; |
| 63 | def OPC_NMSUB : RISCVOpcode<0b1001011>; |
| 64 | def OPC_NMADD : RISCVOpcode<0b1001111>; |
| 65 | def OPC_OP_FP : RISCVOpcode<0b1010011>; |
| 66 | def OPC_BRANCH : RISCVOpcode<0b1100011>; |
| 67 | def OPC_JALR : RISCVOpcode<0b1100111>; |
| 68 | def OPC_JAL : RISCVOpcode<0b1101111>; |
| 69 | def OPC_SYSTEM : RISCVOpcode<0b1110011>; |
| 70 | |
| 71 | class RVInst<dag outs, dag ins, string opcodestr, string argstr, |
| 72 | list<dag> pattern, InstFormat format> |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 73 | : Instruction { |
| 74 | field bits<32> Inst; |
Alex Bradbury | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 75 | // 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 Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 80 | 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 Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 90 | let AsmString = opcodestr # "\t" # argstr; |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 91 | let Pattern = pattern; |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 92 | |
| 93 | let TSFlags{3-0} = format.Value; |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // Pseudo instructions |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 97 | class Pseudo<dag outs, dag ins, list<dag> pattern> |
Alex Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 98 | : RVInst<outs, ins, "", "", pattern, InstFormatPseudo> { |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 99 | let isPseudo = 1; |
Alex Bradbury | 6be16fb | 2017-02-14 05:17:23 +0000 | [diff] [blame] | 100 | let isCodeGenOnly = 1; |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Alex Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 103 | // 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 | |
| 107 | class 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 Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 110 | 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 Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 119 | let Opcode = opcode.Value; |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Alex Bradbury | 0d6cf90 | 2017-12-07 10:26:05 +0000 | [diff] [blame^] | 122 | class 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 Bradbury | 8c345c5 | 2017-11-09 15:00:03 +0000 | [diff] [blame] | 140 | class 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 Bradbury | 0d6cf90 | 2017-12-07 10:26:05 +0000 | [diff] [blame^] | 158 | class 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 Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 174 | class RVInstI<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins, |
| 175 | string opcodestr, string argstr> |
| 176 | : RVInst<outs, ins, opcodestr, argstr, [], InstFormatI> { |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 177 | 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 Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 185 | let Opcode = opcode.Value; |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Alex Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 188 | class 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 Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 191 | 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 Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 202 | let Opcode = opcode.Value; |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Alex Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 205 | class RVInstS<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins, |
| 206 | string opcodestr, string argstr> |
| 207 | : RVInst<outs, ins, opcodestr, argstr, [], InstFormatS> { |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 208 | 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 Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 217 | let Opcode = opcode.Value; |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Alex Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 220 | class RVInstB<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins, |
| 221 | string opcodestr, string argstr> |
| 222 | : RVInst<outs, ins, opcodestr, argstr, [], InstFormatB> { |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 223 | 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 Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 234 | let Opcode = opcode.Value; |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Alex Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 237 | class RVInstU<RISCVOpcode opcode, dag outs, dag ins, string opcodestr, |
| 238 | string argstr> |
| 239 | : RVInst<outs, ins, opcodestr, argstr, [], InstFormatU> { |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 240 | bits<20> imm20; |
| 241 | bits<5> rd; |
| 242 | |
| 243 | let Inst{31-12} = imm20; |
| 244 | let Inst{11-7} = rd; |
Alex Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 245 | let Opcode = opcode.Value; |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Alex Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 248 | class RVInstJ<RISCVOpcode opcode, dag outs, dag ins, string opcodestr, |
| 249 | string argstr> |
| 250 | : RVInst<outs, ins, opcodestr, argstr, [], InstFormatJ> { |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 251 | 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 Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 259 | let Opcode = opcode.Value; |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 260 | } |