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>; |
| 36 | def InstFormatI : InstFormat<2>; |
| 37 | def InstFormatS : InstFormat<3>; |
Alex Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 38 | def InstFormatB : InstFormat<4>; |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 39 | def InstFormatU : InstFormat<5>; |
Alex Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 40 | def InstFormatJ : InstFormat<6>; |
| 41 | def InstFormatOther : InstFormat<7>; |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 42 | |
Alex Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 43 | // 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"). |
| 45 | class RISCVOpcode<bits<7> val> { |
| 46 | bits<7> Value = val; |
| 47 | } |
| 48 | def OPC_LOAD : RISCVOpcode<0b0000011>; |
| 49 | def OPC_LOAD_FP : RISCVOpcode<0b0000111>; |
| 50 | def OPC_MISC_MEM : RISCVOpcode<0b0001111>; |
| 51 | def OPC_OP_IMM : RISCVOpcode<0b0010011>; |
| 52 | def OPC_AUIPC : RISCVOpcode<0b0010111>; |
| 53 | def OPC_OP_IMM_32 : RISCVOpcode<0b0011011>; |
| 54 | def OPC_STORE : RISCVOpcode<0b0100011>; |
| 55 | def OPC_STORE_FP : RISCVOpcode<0b0100111>; |
| 56 | def OPC_AMO : RISCVOpcode<0b0101111>; |
| 57 | def OPC_OP : RISCVOpcode<0b0110011>; |
| 58 | def OPC_LUI : RISCVOpcode<0b0110111>; |
| 59 | def OPC_OP_32 : RISCVOpcode<0b0111011>; |
| 60 | def OPC_MADD : RISCVOpcode<0b1000011>; |
| 61 | def OPC_MSUB : RISCVOpcode<0b1000111>; |
| 62 | def OPC_NMSUB : RISCVOpcode<0b1001011>; |
| 63 | def OPC_NMADD : RISCVOpcode<0b1001111>; |
| 64 | def OPC_OP_FP : RISCVOpcode<0b1010011>; |
| 65 | def OPC_BRANCH : RISCVOpcode<0b1100011>; |
| 66 | def OPC_JALR : RISCVOpcode<0b1100111>; |
| 67 | def OPC_JAL : RISCVOpcode<0b1101111>; |
| 68 | def OPC_SYSTEM : RISCVOpcode<0b1110011>; |
| 69 | |
| 70 | class RVInst<dag outs, dag ins, string opcodestr, string argstr, |
| 71 | list<dag> pattern, InstFormat format> |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 72 | : Instruction { |
| 73 | field bits<32> Inst; |
Alex Bradbury | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 74 | // 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 Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 79 | 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 Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 89 | let AsmString = opcodestr # "\t" # argstr; |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 90 | let Pattern = pattern; |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 91 | |
| 92 | let TSFlags{3-0} = format.Value; |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // Pseudo instructions |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 96 | class Pseudo<dag outs, dag ins, list<dag> pattern> |
Alex Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 97 | : RVInst<outs, ins, "", "", pattern, InstFormatPseudo> { |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 98 | let isPseudo = 1; |
Alex Bradbury | 6be16fb | 2017-02-14 05:17:23 +0000 | [diff] [blame] | 99 | let isCodeGenOnly = 1; |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Alex Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 102 | // 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 | |
| 106 | class 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 Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 109 | 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 Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 118 | let Opcode = opcode.Value; |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Alex Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 121 | class RVInstI<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins, |
| 122 | string opcodestr, string argstr> |
| 123 | : RVInst<outs, ins, opcodestr, argstr, [], InstFormatI> { |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 124 | 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 Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 132 | let Opcode = opcode.Value; |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Alex Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 135 | class 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 Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 138 | 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 Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 149 | let Opcode = opcode.Value; |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Alex Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 152 | class RVInstS<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins, |
| 153 | string opcodestr, string argstr> |
| 154 | : RVInst<outs, ins, opcodestr, argstr, [], InstFormatS> { |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 155 | 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 Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 164 | let Opcode = opcode.Value; |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Alex Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 167 | class RVInstB<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins, |
| 168 | string opcodestr, string argstr> |
| 169 | : RVInst<outs, ins, opcodestr, argstr, [], InstFormatB> { |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 170 | 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 Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 181 | let Opcode = opcode.Value; |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Alex Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 184 | class RVInstU<RISCVOpcode opcode, dag outs, dag ins, string opcodestr, |
| 185 | string argstr> |
| 186 | : RVInst<outs, ins, opcodestr, argstr, [], InstFormatU> { |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 187 | bits<20> imm20; |
| 188 | bits<5> rd; |
| 189 | |
| 190 | let Inst{31-12} = imm20; |
| 191 | let Inst{11-7} = rd; |
Alex Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 192 | let Opcode = opcode.Value; |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Alex Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 195 | class RVInstJ<RISCVOpcode opcode, dag outs, dag ins, string opcodestr, |
| 196 | string argstr> |
| 197 | : RVInst<outs, ins, opcodestr, argstr, [], InstFormatJ> { |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 198 | 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 Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 206 | let Opcode = opcode.Value; |
Alex Bradbury | 24d9b13 | 2016-11-01 23:40:28 +0000 | [diff] [blame] | 207 | } |