1699b0f92Schristos /* riscv.h. RISC-V opcode list for GDB, the GNU debugger. 2*6881a400Schristos Copyright (C) 2011-2022 Free Software Foundation, Inc. 3699b0f92Schristos Contributed by Andrew Waterman 4699b0f92Schristos 5699b0f92Schristos This file is part of GDB, GAS, and the GNU binutils. 6699b0f92Schristos 7699b0f92Schristos GDB, GAS, and the GNU binutils are free software; you can redistribute 8699b0f92Schristos them and/or modify them under the terms of the GNU General Public 9699b0f92Schristos License as published by the Free Software Foundation; either version 10699b0f92Schristos 3, or (at your option) any later version. 11699b0f92Schristos 12699b0f92Schristos GDB, GAS, and the GNU binutils are distributed in the hope that they 13699b0f92Schristos will be useful, but WITHOUT ANY WARRANTY; without even the implied 14699b0f92Schristos warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 15699b0f92Schristos the GNU General Public License for more details. 16699b0f92Schristos 17699b0f92Schristos You should have received a copy of the GNU General Public License 18699b0f92Schristos along with this program; see the file COPYING3. If not, 19699b0f92Schristos see <http://www.gnu.org/licenses/>. */ 20699b0f92Schristos 21699b0f92Schristos #ifndef _RISCV_H_ 22699b0f92Schristos #define _RISCV_H_ 23699b0f92Schristos 24699b0f92Schristos #include "riscv-opc.h" 25699b0f92Schristos #include <stdlib.h> 26699b0f92Schristos #include <stdint.h> 27699b0f92Schristos 28699b0f92Schristos typedef uint64_t insn_t; 29699b0f92Schristos 30699b0f92Schristos static inline unsigned int riscv_insn_length (insn_t insn) 31699b0f92Schristos { 32*6881a400Schristos if ((insn & 0x3) != 0x3) /* RVC instructions. */ 33699b0f92Schristos return 2; 34*6881a400Schristos if ((insn & 0x1f) != 0x1f) /* 32-bit instructions. */ 35699b0f92Schristos return 4; 36*6881a400Schristos if ((insn & 0x3f) == 0x1f) /* 48-bit instructions. */ 37699b0f92Schristos return 6; 38*6881a400Schristos if ((insn & 0x7f) == 0x3f) /* 64-bit instructions. */ 39699b0f92Schristos return 8; 40*6881a400Schristos /* 80- ... 176-bit instructions. */ 41*6881a400Schristos if ((insn & 0x7f) == 0x7f && (insn & 0x7000) != 0x7000) 42*6881a400Schristos return 10 + ((insn >> 11) & 0xe); 43*6881a400Schristos /* Maximum value returned by this function. */ 44*6881a400Schristos #define RISCV_MAX_INSN_LEN 22 45699b0f92Schristos /* Longer instructions not supported at the moment. */ 46699b0f92Schristos return 2; 47699b0f92Schristos } 48699b0f92Schristos 49699b0f92Schristos #define RVC_JUMP_BITS 11 50699b0f92Schristos #define RVC_JUMP_REACH ((1ULL << RVC_JUMP_BITS) * RISCV_JUMP_ALIGN) 51699b0f92Schristos 52699b0f92Schristos #define RVC_BRANCH_BITS 8 53699b0f92Schristos #define RVC_BRANCH_REACH ((1ULL << RVC_BRANCH_BITS) * RISCV_BRANCH_ALIGN) 54699b0f92Schristos 55699b0f92Schristos #define RV_X(x, s, n) (((x) >> (s)) & ((1 << (n)) - 1)) 56699b0f92Schristos #define RV_IMM_SIGN(x) (-(((x) >> 31) & 1)) 57*6881a400Schristos #define RV_X_SIGNED(x, s, n) (RV_X(x, s, n) | ((-(RV_X(x, (s + n - 1), 1))) << (n))) 58699b0f92Schristos 59699b0f92Schristos #define EXTRACT_ITYPE_IMM(x) \ 60699b0f92Schristos (RV_X(x, 20, 12) | (RV_IMM_SIGN(x) << 12)) 61699b0f92Schristos #define EXTRACT_STYPE_IMM(x) \ 62699b0f92Schristos (RV_X(x, 7, 5) | (RV_X(x, 25, 7) << 5) | (RV_IMM_SIGN(x) << 12)) 63*6881a400Schristos #define EXTRACT_BTYPE_IMM(x) \ 64699b0f92Schristos ((RV_X(x, 8, 4) << 1) | (RV_X(x, 25, 6) << 5) | (RV_X(x, 7, 1) << 11) | (RV_IMM_SIGN(x) << 12)) 65699b0f92Schristos #define EXTRACT_UTYPE_IMM(x) \ 66699b0f92Schristos ((RV_X(x, 12, 20) << 12) | (RV_IMM_SIGN(x) << 32)) 67*6881a400Schristos #define EXTRACT_JTYPE_IMM(x) \ 68699b0f92Schristos ((RV_X(x, 21, 10) << 1) | (RV_X(x, 20, 1) << 11) | (RV_X(x, 12, 8) << 12) | (RV_IMM_SIGN(x) << 20)) 69*6881a400Schristos #define EXTRACT_CITYPE_IMM(x) \ 70699b0f92Schristos (RV_X(x, 2, 5) | (-RV_X(x, 12, 1) << 5)) 71*6881a400Schristos #define EXTRACT_CITYPE_LUI_IMM(x) \ 72*6881a400Schristos (EXTRACT_CITYPE_IMM (x) << RISCV_IMM_BITS) 73*6881a400Schristos #define EXTRACT_CITYPE_ADDI16SP_IMM(x) \ 74699b0f92Schristos ((RV_X(x, 6, 1) << 4) | (RV_X(x, 2, 1) << 5) | (RV_X(x, 5, 1) << 6) | (RV_X(x, 3, 2) << 7) | (-RV_X(x, 12, 1) << 9)) 75*6881a400Schristos #define EXTRACT_CITYPE_LWSP_IMM(x) \ 76699b0f92Schristos ((RV_X(x, 4, 3) << 2) | (RV_X(x, 12, 1) << 5) | (RV_X(x, 2, 2) << 6)) 77*6881a400Schristos #define EXTRACT_CITYPE_LDSP_IMM(x) \ 78699b0f92Schristos ((RV_X(x, 5, 2) << 3) | (RV_X(x, 12, 1) << 5) | (RV_X(x, 2, 3) << 6)) 79*6881a400Schristos #define EXTRACT_CSSTYPE_IMM(x) \ 80*6881a400Schristos (RV_X(x, 7, 6) << 0) 81*6881a400Schristos #define EXTRACT_CSSTYPE_SWSP_IMM(x) \ 82699b0f92Schristos ((RV_X(x, 9, 4) << 2) | (RV_X(x, 7, 2) << 6)) 83*6881a400Schristos #define EXTRACT_CSSTYPE_SDSP_IMM(x) \ 84699b0f92Schristos ((RV_X(x, 10, 3) << 3) | (RV_X(x, 7, 3) << 6)) 85*6881a400Schristos #define EXTRACT_CIWTYPE_IMM(x) \ 86*6881a400Schristos (RV_X(x, 5, 8)) 87*6881a400Schristos #define EXTRACT_CIWTYPE_ADDI4SPN_IMM(x) \ 88*6881a400Schristos ((RV_X(x, 6, 1) << 2) | (RV_X(x, 5, 1) << 3) | (RV_X(x, 11, 2) << 4) | (RV_X(x, 7, 4) << 6)) 89*6881a400Schristos #define EXTRACT_CLTYPE_IMM(x) \ 90*6881a400Schristos ((RV_X(x, 5, 2) << 0) | (RV_X(x, 10, 3) << 2)) 91*6881a400Schristos #define EXTRACT_CLTYPE_LW_IMM(x) \ 92*6881a400Schristos ((RV_X(x, 6, 1) << 2) | (RV_X(x, 10, 3) << 3) | (RV_X(x, 5, 1) << 6)) 93*6881a400Schristos #define EXTRACT_CLTYPE_LD_IMM(x) \ 94*6881a400Schristos ((RV_X(x, 10, 3) << 3) | (RV_X(x, 5, 2) << 6)) 95*6881a400Schristos #define EXTRACT_CBTYPE_IMM(x) \ 96699b0f92Schristos ((RV_X(x, 3, 2) << 1) | (RV_X(x, 10, 2) << 3) | (RV_X(x, 2, 1) << 5) | (RV_X(x, 5, 2) << 6) | (-RV_X(x, 12, 1) << 8)) 97*6881a400Schristos #define EXTRACT_CJTYPE_IMM(x) \ 98699b0f92Schristos ((RV_X(x, 3, 3) << 1) | (RV_X(x, 11, 1) << 4) | (RV_X(x, 2, 1) << 5) | (RV_X(x, 7, 1) << 6) | (RV_X(x, 6, 1) << 7) | (RV_X(x, 9, 2) << 8) | (RV_X(x, 8, 1) << 10) | (-RV_X(x, 12, 1) << 11)) 99*6881a400Schristos #define EXTRACT_RVV_VI_IMM(x) \ 100*6881a400Schristos (RV_X(x, 15, 5) | (-RV_X(x, 19, 1) << 5)) 101*6881a400Schristos #define EXTRACT_RVV_VI_UIMM(x) \ 102*6881a400Schristos (RV_X(x, 15, 5)) 103*6881a400Schristos #define EXTRACT_RVV_OFFSET(x) \ 104*6881a400Schristos (RV_X(x, 29, 3)) 105*6881a400Schristos #define EXTRACT_RVV_VB_IMM(x) \ 106*6881a400Schristos (RV_X(x, 20, 10)) 107*6881a400Schristos #define EXTRACT_RVV_VC_IMM(x) \ 108*6881a400Schristos (RV_X(x, 20, 11)) 109699b0f92Schristos 110699b0f92Schristos #define ENCODE_ITYPE_IMM(x) \ 111699b0f92Schristos (RV_X(x, 0, 12) << 20) 112699b0f92Schristos #define ENCODE_STYPE_IMM(x) \ 113699b0f92Schristos ((RV_X(x, 0, 5) << 7) | (RV_X(x, 5, 7) << 25)) 114*6881a400Schristos #define ENCODE_BTYPE_IMM(x) \ 115699b0f92Schristos ((RV_X(x, 1, 4) << 8) | (RV_X(x, 5, 6) << 25) | (RV_X(x, 11, 1) << 7) | (RV_X(x, 12, 1) << 31)) 116699b0f92Schristos #define ENCODE_UTYPE_IMM(x) \ 117699b0f92Schristos (RV_X(x, 12, 20) << 12) 118*6881a400Schristos #define ENCODE_JTYPE_IMM(x) \ 119699b0f92Schristos ((RV_X(x, 1, 10) << 21) | (RV_X(x, 11, 1) << 20) | (RV_X(x, 12, 8) << 12) | (RV_X(x, 20, 1) << 31)) 120*6881a400Schristos #define ENCODE_CITYPE_IMM(x) \ 121699b0f92Schristos ((RV_X(x, 0, 5) << 2) | (RV_X(x, 5, 1) << 12)) 122*6881a400Schristos #define ENCODE_CITYPE_LUI_IMM(x) \ 123*6881a400Schristos ENCODE_CITYPE_IMM ((x) >> RISCV_IMM_BITS) 124*6881a400Schristos #define ENCODE_CITYPE_ADDI16SP_IMM(x) \ 125699b0f92Schristos ((RV_X(x, 4, 1) << 6) | (RV_X(x, 5, 1) << 2) | (RV_X(x, 6, 1) << 5) | (RV_X(x, 7, 2) << 3) | (RV_X(x, 9, 1) << 12)) 126*6881a400Schristos #define ENCODE_CITYPE_LWSP_IMM(x) \ 127699b0f92Schristos ((RV_X(x, 2, 3) << 4) | (RV_X(x, 5, 1) << 12) | (RV_X(x, 6, 2) << 2)) 128*6881a400Schristos #define ENCODE_CITYPE_LDSP_IMM(x) \ 129699b0f92Schristos ((RV_X(x, 3, 2) << 5) | (RV_X(x, 5, 1) << 12) | (RV_X(x, 6, 3) << 2)) 130*6881a400Schristos #define ENCODE_CSSTYPE_IMM(x) \ 131*6881a400Schristos (RV_X(x, 0, 6) << 7) 132*6881a400Schristos #define ENCODE_CSSTYPE_SWSP_IMM(x) \ 133699b0f92Schristos ((RV_X(x, 2, 4) << 9) | (RV_X(x, 6, 2) << 7)) 134*6881a400Schristos #define ENCODE_CSSTYPE_SDSP_IMM(x) \ 135699b0f92Schristos ((RV_X(x, 3, 3) << 10) | (RV_X(x, 6, 3) << 7)) 136*6881a400Schristos #define ENCODE_CIWTYPE_IMM(x) \ 137*6881a400Schristos (RV_X(x, 0, 8) << 5) 138*6881a400Schristos #define ENCODE_CIWTYPE_ADDI4SPN_IMM(x) \ 139*6881a400Schristos ((RV_X(x, 2, 1) << 6) | (RV_X(x, 3, 1) << 5) | (RV_X(x, 4, 2) << 11) | (RV_X(x, 6, 4) << 7)) 140*6881a400Schristos #define ENCODE_CLTYPE_IMM(x) \ 141*6881a400Schristos ((RV_X(x, 0, 2) << 5) | (RV_X(x, 2, 3) << 10)) 142*6881a400Schristos #define ENCODE_CLTYPE_LW_IMM(x) \ 143*6881a400Schristos ((RV_X(x, 2, 1) << 6) | (RV_X(x, 3, 3) << 10) | (RV_X(x, 6, 1) << 5)) 144*6881a400Schristos #define ENCODE_CLTYPE_LD_IMM(x) \ 145*6881a400Schristos ((RV_X(x, 3, 3) << 10) | (RV_X(x, 6, 2) << 5)) 146*6881a400Schristos #define ENCODE_CBTYPE_IMM(x) \ 147699b0f92Schristos ((RV_X(x, 1, 2) << 3) | (RV_X(x, 3, 2) << 10) | (RV_X(x, 5, 1) << 2) | (RV_X(x, 6, 2) << 5) | (RV_X(x, 8, 1) << 12)) 148*6881a400Schristos #define ENCODE_CJTYPE_IMM(x) \ 149699b0f92Schristos ((RV_X(x, 1, 3) << 3) | (RV_X(x, 4, 1) << 11) | (RV_X(x, 5, 1) << 2) | (RV_X(x, 6, 1) << 7) | (RV_X(x, 7, 1) << 6) | (RV_X(x, 8, 2) << 9) | (RV_X(x, 10, 1) << 8) | (RV_X(x, 11, 1) << 12)) 150*6881a400Schristos #define ENCODE_RVV_VB_IMM(x) \ 151*6881a400Schristos (RV_X(x, 0, 10) << 20) 152*6881a400Schristos #define ENCODE_RVV_VC_IMM(x) \ 153*6881a400Schristos (RV_X(x, 0, 11) << 20) 154699b0f92Schristos 155699b0f92Schristos #define VALID_ITYPE_IMM(x) (EXTRACT_ITYPE_IMM(ENCODE_ITYPE_IMM(x)) == (x)) 156699b0f92Schristos #define VALID_STYPE_IMM(x) (EXTRACT_STYPE_IMM(ENCODE_STYPE_IMM(x)) == (x)) 157*6881a400Schristos #define VALID_BTYPE_IMM(x) (EXTRACT_BTYPE_IMM(ENCODE_BTYPE_IMM(x)) == (x)) 158699b0f92Schristos #define VALID_UTYPE_IMM(x) (EXTRACT_UTYPE_IMM(ENCODE_UTYPE_IMM(x)) == (x)) 159*6881a400Schristos #define VALID_JTYPE_IMM(x) (EXTRACT_JTYPE_IMM(ENCODE_JTYPE_IMM(x)) == (x)) 160*6881a400Schristos #define VALID_CITYPE_IMM(x) (EXTRACT_CITYPE_IMM(ENCODE_CITYPE_IMM(x)) == (x)) 161*6881a400Schristos #define VALID_CITYPE_LUI_IMM(x) (ENCODE_CITYPE_LUI_IMM(x) != 0 \ 162*6881a400Schristos && EXTRACT_CITYPE_LUI_IMM(ENCODE_CITYPE_LUI_IMM(x)) == (x)) 163*6881a400Schristos #define VALID_CITYPE_ADDI16SP_IMM(x) (ENCODE_CITYPE_ADDI16SP_IMM(x) != 0 \ 164*6881a400Schristos && EXTRACT_CITYPE_ADDI16SP_IMM(ENCODE_CITYPE_ADDI16SP_IMM(x)) == (x)) 165*6881a400Schristos #define VALID_CITYPE_LWSP_IMM(x) (EXTRACT_CITYPE_LWSP_IMM(ENCODE_CITYPE_LWSP_IMM(x)) == (x)) 166*6881a400Schristos #define VALID_CITYPE_LDSP_IMM(x) (EXTRACT_CITYPE_LDSP_IMM(ENCODE_CITYPE_LDSP_IMM(x)) == (x)) 167*6881a400Schristos #define VALID_CSSTYPE_IMM(x) (EXTRACT_CSSTYPE_IMM(ENCODE_CSSTYPE_IMM(x)) == (x)) 168*6881a400Schristos #define VALID_CSSTYPE_SWSP_IMM(x) (EXTRACT_CSSTYPE_SWSP_IMM(ENCODE_CSSTYPE_SWSP_IMM(x)) == (x)) 169*6881a400Schristos #define VALID_CSSTYPE_SDSP_IMM(x) (EXTRACT_CSSTYPE_SDSP_IMM(ENCODE_CSSTYPE_SDSP_IMM(x)) == (x)) 170*6881a400Schristos #define VALID_CIWTYPE_IMM(x) (EXTRACT_CIWTYPE_IMM(ENCODE_CIWTYPE_IMM(x)) == (x)) 171*6881a400Schristos #define VALID_CIWTYPE_ADDI4SPN_IMM(x) (EXTRACT_CIWTYPE_ADDI4SPN_IMM(ENCODE_CIWTYPE_ADDI4SPN_IMM(x)) == (x)) 172*6881a400Schristos #define VALID_CLTYPE_IMM(x) (EXTRACT_CLTYPE_IMM(ENCODE_CLTYPE_IMM(x)) == (x)) 173*6881a400Schristos #define VALID_CLTYPE_LW_IMM(x) (EXTRACT_CLTYPE_LW_IMM(ENCODE_CLTYPE_LW_IMM(x)) == (x)) 174*6881a400Schristos #define VALID_CLTYPE_LD_IMM(x) (EXTRACT_CLTYPE_LD_IMM(ENCODE_CLTYPE_LD_IMM(x)) == (x)) 175*6881a400Schristos #define VALID_CBTYPE_IMM(x) (EXTRACT_CBTYPE_IMM(ENCODE_CBTYPE_IMM(x)) == (x)) 176*6881a400Schristos #define VALID_CJTYPE_IMM(x) (EXTRACT_CJTYPE_IMM(ENCODE_CJTYPE_IMM(x)) == (x)) 177*6881a400Schristos #define VALID_RVV_VB_IMM(x) (EXTRACT_RVV_VB_IMM(ENCODE_RVV_VB_IMM(x)) == (x)) 178*6881a400Schristos #define VALID_RVV_VC_IMM(x) (EXTRACT_RVV_VC_IMM(ENCODE_RVV_VC_IMM(x)) == (x)) 179699b0f92Schristos 180699b0f92Schristos #define RISCV_RTYPE(insn, rd, rs1, rs2) \ 181699b0f92Schristos ((MATCH_ ## insn) | ((rd) << OP_SH_RD) | ((rs1) << OP_SH_RS1) | ((rs2) << OP_SH_RS2)) 182699b0f92Schristos #define RISCV_ITYPE(insn, rd, rs1, imm) \ 183699b0f92Schristos ((MATCH_ ## insn) | ((rd) << OP_SH_RD) | ((rs1) << OP_SH_RS1) | ENCODE_ITYPE_IMM(imm)) 184699b0f92Schristos #define RISCV_STYPE(insn, rs1, rs2, imm) \ 185699b0f92Schristos ((MATCH_ ## insn) | ((rs1) << OP_SH_RS1) | ((rs2) << OP_SH_RS2) | ENCODE_STYPE_IMM(imm)) 186*6881a400Schristos #define RISCV_BTYPE(insn, rs1, rs2, target) \ 187*6881a400Schristos ((MATCH_ ## insn) | ((rs1) << OP_SH_RS1) | ((rs2) << OP_SH_RS2) | ENCODE_BTYPE_IMM(target)) 188699b0f92Schristos #define RISCV_UTYPE(insn, rd, bigimm) \ 189699b0f92Schristos ((MATCH_ ## insn) | ((rd) << OP_SH_RD) | ENCODE_UTYPE_IMM(bigimm)) 190*6881a400Schristos #define RISCV_JTYPE(insn, rd, target) \ 191*6881a400Schristos ((MATCH_ ## insn) | ((rd) << OP_SH_RD) | ENCODE_JTYPE_IMM(target)) 192699b0f92Schristos 193699b0f92Schristos #define RISCV_NOP RISCV_ITYPE(ADDI, 0, 0, 0) 194699b0f92Schristos #define RVC_NOP MATCH_C_ADDI 195699b0f92Schristos 196699b0f92Schristos #define RISCV_CONST_HIGH_PART(VALUE) \ 197699b0f92Schristos (((VALUE) + (RISCV_IMM_REACH/2)) & ~(RISCV_IMM_REACH-1)) 198699b0f92Schristos #define RISCV_CONST_LOW_PART(VALUE) ((VALUE) - RISCV_CONST_HIGH_PART (VALUE)) 199699b0f92Schristos #define RISCV_PCREL_HIGH_PART(VALUE, PC) RISCV_CONST_HIGH_PART((VALUE) - (PC)) 200699b0f92Schristos #define RISCV_PCREL_LOW_PART(VALUE, PC) RISCV_CONST_LOW_PART((VALUE) - (PC)) 201699b0f92Schristos 202699b0f92Schristos #define RISCV_JUMP_BITS RISCV_BIGIMM_BITS 203699b0f92Schristos #define RISCV_JUMP_ALIGN_BITS 1 204699b0f92Schristos #define RISCV_JUMP_ALIGN (1 << RISCV_JUMP_ALIGN_BITS) 205699b0f92Schristos #define RISCV_JUMP_REACH ((1ULL << RISCV_JUMP_BITS) * RISCV_JUMP_ALIGN) 206699b0f92Schristos 207699b0f92Schristos #define RISCV_IMM_BITS 12 208699b0f92Schristos #define RISCV_BIGIMM_BITS (32 - RISCV_IMM_BITS) 209699b0f92Schristos #define RISCV_IMM_REACH (1LL << RISCV_IMM_BITS) 210699b0f92Schristos #define RISCV_BIGIMM_REACH (1LL << RISCV_BIGIMM_BITS) 211699b0f92Schristos #define RISCV_RVC_IMM_REACH (1LL << 6) 212699b0f92Schristos #define RISCV_BRANCH_BITS RISCV_IMM_BITS 213699b0f92Schristos #define RISCV_BRANCH_ALIGN_BITS RISCV_JUMP_ALIGN_BITS 214699b0f92Schristos #define RISCV_BRANCH_ALIGN (1 << RISCV_BRANCH_ALIGN_BITS) 215699b0f92Schristos #define RISCV_BRANCH_REACH (RISCV_IMM_REACH * RISCV_BRANCH_ALIGN) 216699b0f92Schristos 217699b0f92Schristos /* RV fields. */ 218699b0f92Schristos 219699b0f92Schristos #define OP_MASK_OP 0x7f 220699b0f92Schristos #define OP_SH_OP 0 221699b0f92Schristos #define OP_MASK_RS2 0x1f 222699b0f92Schristos #define OP_SH_RS2 20 223699b0f92Schristos #define OP_MASK_RS1 0x1f 224699b0f92Schristos #define OP_SH_RS1 15 2257d62b00eSchristos #define OP_MASK_RS3 0x1fU 226699b0f92Schristos #define OP_SH_RS3 27 227699b0f92Schristos #define OP_MASK_RD 0x1f 228699b0f92Schristos #define OP_SH_RD 7 229699b0f92Schristos #define OP_MASK_SHAMT 0x3f 230699b0f92Schristos #define OP_SH_SHAMT 20 231699b0f92Schristos #define OP_MASK_SHAMTW 0x1f 232699b0f92Schristos #define OP_SH_SHAMTW 20 233699b0f92Schristos #define OP_MASK_RM 0x7 234699b0f92Schristos #define OP_SH_RM 12 235699b0f92Schristos #define OP_MASK_PRED 0xf 236699b0f92Schristos #define OP_SH_PRED 24 237699b0f92Schristos #define OP_MASK_SUCC 0xf 238699b0f92Schristos #define OP_SH_SUCC 20 239699b0f92Schristos #define OP_MASK_AQ 0x1 240699b0f92Schristos #define OP_SH_AQ 26 241699b0f92Schristos #define OP_MASK_RL 0x1 242699b0f92Schristos #define OP_SH_RL 25 243699b0f92Schristos 2447d62b00eSchristos #define OP_MASK_CSR 0xfffU 245699b0f92Schristos #define OP_SH_CSR 20 246699b0f92Schristos 2477f2ac410Schristos #define OP_MASK_FUNCT3 0x7 2487f2ac410Schristos #define OP_SH_FUNCT3 12 2497d62b00eSchristos #define OP_MASK_FUNCT7 0x7fU 2507f2ac410Schristos #define OP_SH_FUNCT7 25 2517f2ac410Schristos #define OP_MASK_FUNCT2 0x3 2527f2ac410Schristos #define OP_SH_FUNCT2 25 2537f2ac410Schristos 254699b0f92Schristos /* RVC fields. */ 255699b0f92Schristos 2567f2ac410Schristos #define OP_MASK_OP2 0x3 2577f2ac410Schristos #define OP_SH_OP2 0 2587f2ac410Schristos 259699b0f92Schristos #define OP_MASK_CRS2 0x1f 260699b0f92Schristos #define OP_SH_CRS2 2 261699b0f92Schristos #define OP_MASK_CRS1S 0x7 262699b0f92Schristos #define OP_SH_CRS1S 7 263699b0f92Schristos #define OP_MASK_CRS2S 0x7 264699b0f92Schristos #define OP_SH_CRS2S 2 265699b0f92Schristos 2667f2ac410Schristos #define OP_MASK_CFUNCT6 0x3f 2677f2ac410Schristos #define OP_SH_CFUNCT6 10 2687f2ac410Schristos #define OP_MASK_CFUNCT4 0xf 2697f2ac410Schristos #define OP_SH_CFUNCT4 12 2707f2ac410Schristos #define OP_MASK_CFUNCT3 0x7 2717f2ac410Schristos #define OP_SH_CFUNCT3 13 2727f2ac410Schristos #define OP_MASK_CFUNCT2 0x3 2737f2ac410Schristos #define OP_SH_CFUNCT2 5 2747f2ac410Schristos 275*6881a400Schristos /* Scalar crypto fields. */ 276*6881a400Schristos 277*6881a400Schristos #define OP_SH_BS 30 278*6881a400Schristos #define OP_MASK_BS 3 279*6881a400Schristos #define OP_SH_RNUM 20 280*6881a400Schristos #define OP_MASK_RNUM 0xf 281*6881a400Schristos 282*6881a400Schristos /* RVV fields. */ 283*6881a400Schristos 284*6881a400Schristos #define OP_MASK_VD 0x1f 285*6881a400Schristos #define OP_SH_VD 7 286*6881a400Schristos #define OP_MASK_VS1 0x1f 287*6881a400Schristos #define OP_SH_VS1 15 288*6881a400Schristos #define OP_MASK_VS2 0x1f 289*6881a400Schristos #define OP_SH_VS2 20 290*6881a400Schristos #define OP_MASK_VIMM 0x1f 291*6881a400Schristos #define OP_SH_VIMM 15 292*6881a400Schristos #define OP_MASK_VMASK 0x1 293*6881a400Schristos #define OP_SH_VMASK 25 294*6881a400Schristos #define OP_MASK_VFUNCT6 0x3f 295*6881a400Schristos #define OP_SH_VFUNCT6 26 296*6881a400Schristos #define OP_MASK_VLMUL 0x7 297*6881a400Schristos #define OP_SH_VLMUL 0 298*6881a400Schristos #define OP_MASK_VSEW 0x7 299*6881a400Schristos #define OP_SH_VSEW 3 300*6881a400Schristos #define OP_MASK_VTA 0x1 301*6881a400Schristos #define OP_SH_VTA 6 302*6881a400Schristos #define OP_MASK_VMA 0x1 303*6881a400Schristos #define OP_SH_VMA 7 304*6881a400Schristos #define OP_MASK_VWD 0x1 305*6881a400Schristos #define OP_SH_VWD 26 306*6881a400Schristos 307*6881a400Schristos #define NVECR 32 308*6881a400Schristos #define NVECM 1 309*6881a400Schristos 310699b0f92Schristos /* ABI names for selected x-registers. */ 311699b0f92Schristos 312699b0f92Schristos #define X_RA 1 313699b0f92Schristos #define X_SP 2 314699b0f92Schristos #define X_GP 3 315699b0f92Schristos #define X_TP 4 316699b0f92Schristos #define X_T0 5 317699b0f92Schristos #define X_T1 6 318699b0f92Schristos #define X_T2 7 319699b0f92Schristos #define X_T3 28 320699b0f92Schristos 321699b0f92Schristos #define NGPR 32 322699b0f92Schristos #define NFPR 32 323699b0f92Schristos 3247f2ac410Schristos /* These fake label defines are use by both the assembler, and 3257f2ac410Schristos libopcodes. The assembler uses this when it needs to generate a fake 3267f2ac410Schristos label, and libopcodes uses it to hide the fake labels in its output. */ 3277f2ac410Schristos #define RISCV_FAKE_LABEL_NAME ".L0 " 3287f2ac410Schristos #define RISCV_FAKE_LABEL_CHAR ' ' 3297f2ac410Schristos 330699b0f92Schristos /* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in 331699b0f92Schristos VALUE << SHIFT. VALUE is evaluated exactly once. */ 332699b0f92Schristos #define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \ 333699b0f92Schristos (STRUCT) = (((STRUCT) & ~((insn_t)(MASK) << (SHIFT))) \ 334699b0f92Schristos | ((insn_t)((VALUE) & (MASK)) << (SHIFT))) 335699b0f92Schristos 336699b0f92Schristos /* Extract bits MASK << SHIFT from STRUCT and shift them right 337699b0f92Schristos SHIFT places. */ 338699b0f92Schristos #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \ 339699b0f92Schristos (((STRUCT) >> (SHIFT)) & (MASK)) 340699b0f92Schristos 341699b0f92Schristos /* Extract the operand given by FIELD from integer INSN. */ 342699b0f92Schristos #define EXTRACT_OPERAND(FIELD, INSN) \ 343699b0f92Schristos EXTRACT_BITS ((INSN), OP_MASK_##FIELD, OP_SH_##FIELD) 344699b0f92Schristos 345*6881a400Schristos /* Extract an unsigned immediate operand on position s with n bits. */ 346*6881a400Schristos #define EXTRACT_U_IMM(n, s, l) \ 347*6881a400Schristos RV_X (l, s, n) 348*6881a400Schristos 349*6881a400Schristos /* Extract an signed immediate operand on position s with n bits. */ 350*6881a400Schristos #define EXTRACT_S_IMM(n, s, l) \ 351*6881a400Schristos RV_X_SIGNED (l, s, n) 352*6881a400Schristos 353*6881a400Schristos /* Validate that unsigned n-bit immediate is within bounds. */ 354*6881a400Schristos #define VALIDATE_U_IMM(v, n) \ 355*6881a400Schristos ((unsigned long) v < (1UL << n)) 356*6881a400Schristos 357*6881a400Schristos /* Validate that signed n-bit immediate is within bounds. */ 358*6881a400Schristos #define VALIDATE_S_IMM(v, n) \ 359*6881a400Schristos (v < (long) (1UL << (n-1)) && v >= -(offsetT) (1UL << (n-1))) 360*6881a400Schristos 3617f2ac410Schristos /* The maximal number of subset can be required. */ 3627f2ac410Schristos #define MAX_SUBSET_NUM 4 3637f2ac410Schristos 3647d62b00eSchristos /* All RISC-V instructions belong to at least one of these classes. */ 3657d62b00eSchristos enum riscv_insn_class 3667d62b00eSchristos { 3677d62b00eSchristos INSN_CLASS_NONE, 3687d62b00eSchristos 3697d62b00eSchristos INSN_CLASS_I, 3707d62b00eSchristos INSN_CLASS_C, 3717d62b00eSchristos INSN_CLASS_A, 3727d62b00eSchristos INSN_CLASS_M, 3737d62b00eSchristos INSN_CLASS_F, 3747d62b00eSchristos INSN_CLASS_D, 3757d62b00eSchristos INSN_CLASS_Q, 376*6881a400Schristos INSN_CLASS_F_AND_C, 377*6881a400Schristos INSN_CLASS_D_AND_C, 378*6881a400Schristos INSN_CLASS_ZICSR, 379*6881a400Schristos INSN_CLASS_ZIFENCEI, 380*6881a400Schristos INSN_CLASS_ZIHINTPAUSE, 381*6881a400Schristos INSN_CLASS_ZMMUL, 382*6881a400Schristos INSN_CLASS_ZAWRS, 383*6881a400Schristos INSN_CLASS_F_INX, 384*6881a400Schristos INSN_CLASS_D_INX, 385*6881a400Schristos INSN_CLASS_Q_INX, 386*6881a400Schristos INSN_CLASS_ZFH_INX, 387*6881a400Schristos INSN_CLASS_ZFHMIN, 388*6881a400Schristos INSN_CLASS_ZFHMIN_INX, 389*6881a400Schristos INSN_CLASS_ZFHMIN_AND_D_INX, 390*6881a400Schristos INSN_CLASS_ZFHMIN_AND_Q_INX, 391*6881a400Schristos INSN_CLASS_ZBA, 392*6881a400Schristos INSN_CLASS_ZBB, 393*6881a400Schristos INSN_CLASS_ZBC, 394*6881a400Schristos INSN_CLASS_ZBS, 395*6881a400Schristos INSN_CLASS_ZBKB, 396*6881a400Schristos INSN_CLASS_ZBKC, 397*6881a400Schristos INSN_CLASS_ZBKX, 398*6881a400Schristos INSN_CLASS_ZKND, 399*6881a400Schristos INSN_CLASS_ZKNE, 400*6881a400Schristos INSN_CLASS_ZKNH, 401*6881a400Schristos INSN_CLASS_ZKSED, 402*6881a400Schristos INSN_CLASS_ZKSH, 403*6881a400Schristos INSN_CLASS_ZBB_OR_ZBKB, 404*6881a400Schristos INSN_CLASS_ZBC_OR_ZBKC, 405*6881a400Schristos INSN_CLASS_ZKND_OR_ZKNE, 406*6881a400Schristos INSN_CLASS_V, 407*6881a400Schristos INSN_CLASS_ZVEF, 408*6881a400Schristos INSN_CLASS_SVINVAL, 409*6881a400Schristos INSN_CLASS_ZICBOM, 410*6881a400Schristos INSN_CLASS_ZICBOP, 411*6881a400Schristos INSN_CLASS_ZICBOZ, 412*6881a400Schristos INSN_CLASS_H, 413*6881a400Schristos INSN_CLASS_XTHEADBA, 414*6881a400Schristos INSN_CLASS_XTHEADBB, 415*6881a400Schristos INSN_CLASS_XTHEADBS, 416*6881a400Schristos INSN_CLASS_XTHEADCMO, 417*6881a400Schristos INSN_CLASS_XTHEADCONDMOV, 418*6881a400Schristos INSN_CLASS_XTHEADFMEMIDX, 419*6881a400Schristos INSN_CLASS_XTHEADFMV, 420*6881a400Schristos INSN_CLASS_XTHEADINT, 421*6881a400Schristos INSN_CLASS_XTHEADMAC, 422*6881a400Schristos INSN_CLASS_XTHEADMEMIDX, 423*6881a400Schristos INSN_CLASS_XTHEADMEMPAIR, 424*6881a400Schristos INSN_CLASS_XTHEADSYNC, 4257d62b00eSchristos }; 4267d62b00eSchristos 427699b0f92Schristos /* This structure holds information for a particular instruction. */ 428699b0f92Schristos struct riscv_opcode 429699b0f92Schristos { 430699b0f92Schristos /* The name of the instruction. */ 431699b0f92Schristos const char *name; 432*6881a400Schristos 4337f2ac410Schristos /* The requirement of xlen for the instruction, 0 if no requirement. */ 4347f2ac410Schristos unsigned xlen_requirement; 435*6881a400Schristos 4367d62b00eSchristos /* Class to which this instruction belongs. Used to decide whether or 4377d62b00eSchristos not this instruction is legal in the current -march context. */ 4387d62b00eSchristos enum riscv_insn_class insn_class; 439*6881a400Schristos 440699b0f92Schristos /* A string describing the arguments for this instruction. */ 441699b0f92Schristos const char *args; 442*6881a400Schristos 443699b0f92Schristos /* The basic opcode for the instruction. When assembling, this 444699b0f92Schristos opcode is modified by the arguments to produce the actual opcode 445699b0f92Schristos that is used. If pinfo is INSN_MACRO, then this is 0. */ 446699b0f92Schristos insn_t match; 447*6881a400Schristos 448699b0f92Schristos /* If pinfo is not INSN_MACRO, then this is a bit mask for the 449699b0f92Schristos relevant portions of the opcode when disassembling. If the 450699b0f92Schristos actual opcode anded with the match field equals the opcode field, 451699b0f92Schristos then we have found the correct instruction. If pinfo is 452699b0f92Schristos INSN_MACRO, then this field is the macro identifier. */ 453699b0f92Schristos insn_t mask; 454*6881a400Schristos 455699b0f92Schristos /* A function to determine if a word corresponds to this instruction. 456699b0f92Schristos Usually, this computes ((word & mask) == match). */ 457699b0f92Schristos int (*match_func) (const struct riscv_opcode *op, insn_t word); 458*6881a400Schristos 459699b0f92Schristos /* For a macro, this is INSN_MACRO. Otherwise, it is a collection 460699b0f92Schristos of bits describing the instruction, notably any relevant hazard 461699b0f92Schristos information. */ 462699b0f92Schristos unsigned long pinfo; 463699b0f92Schristos }; 464699b0f92Schristos 465699b0f92Schristos /* Instruction is a simple alias (e.g. "mv" for "addi"). */ 466699b0f92Schristos #define INSN_ALIAS 0x00000001 4677f2ac410Schristos 4687f2ac410Schristos /* These are for setting insn_info fields. 4697f2ac410Schristos 4707f2ac410Schristos Nonbranch is the default. Noninsn is used only if there is no match. 4717f2ac410Schristos There are no condjsr or dref2 instructions. So that leaves condbranch, 4727f2ac410Schristos branch, jsr, and dref that we need to handle here, encoded in 3 bits. */ 4737f2ac410Schristos #define INSN_TYPE 0x0000000e 4747f2ac410Schristos 4757f2ac410Schristos /* Instruction is an unconditional branch. */ 4767f2ac410Schristos #define INSN_BRANCH 0x00000002 4777f2ac410Schristos /* Instruction is a conditional branch. */ 4787f2ac410Schristos #define INSN_CONDBRANCH 0x00000004 4797f2ac410Schristos /* Instruction is a jump to subroutine. */ 4807f2ac410Schristos #define INSN_JSR 0x00000006 4817f2ac410Schristos /* Instruction is a data reference. */ 4827f2ac410Schristos #define INSN_DREF 0x00000008 483*6881a400Schristos /* Instruction is allowed when eew >= 64. */ 484*6881a400Schristos #define INSN_V_EEW64 0x10000000 4857f2ac410Schristos 4867f2ac410Schristos /* We have 5 data reference sizes, which we can encode in 3 bits. */ 4877f2ac410Schristos #define INSN_DATA_SIZE 0x00000070 4887f2ac410Schristos #define INSN_DATA_SIZE_SHIFT 4 4897f2ac410Schristos #define INSN_1_BYTE 0x00000010 4907f2ac410Schristos #define INSN_2_BYTE 0x00000020 4917f2ac410Schristos #define INSN_4_BYTE 0x00000030 4927f2ac410Schristos #define INSN_8_BYTE 0x00000040 4937f2ac410Schristos #define INSN_16_BYTE 0x00000050 4947f2ac410Schristos 495699b0f92Schristos /* Instruction is actually a macro. It should be ignored by the 496699b0f92Schristos disassembler, and requires special treatment by the assembler. */ 497699b0f92Schristos #define INSN_MACRO 0xffffffff 498699b0f92Schristos 499*6881a400Schristos /* This is a list of macro expanded instructions. */ 500699b0f92Schristos enum 501699b0f92Schristos { 502699b0f92Schristos M_LA, 503699b0f92Schristos M_LLA, 504699b0f92Schristos M_LA_TLS_GD, 505699b0f92Schristos M_LA_TLS_IE, 506699b0f92Schristos M_LB, 507699b0f92Schristos M_LBU, 508699b0f92Schristos M_LH, 509699b0f92Schristos M_LHU, 510699b0f92Schristos M_LW, 511699b0f92Schristos M_LWU, 512699b0f92Schristos M_LD, 513699b0f92Schristos M_SB, 514699b0f92Schristos M_SH, 515699b0f92Schristos M_SW, 516699b0f92Schristos M_SD, 517699b0f92Schristos M_FLW, 518699b0f92Schristos M_FLD, 519699b0f92Schristos M_FLQ, 520699b0f92Schristos M_FSW, 521699b0f92Schristos M_FSD, 522699b0f92Schristos M_FSQ, 523699b0f92Schristos M_CALL, 524699b0f92Schristos M_J, 525699b0f92Schristos M_LI, 526*6881a400Schristos M_ZEXTH, 527*6881a400Schristos M_ZEXTW, 528*6881a400Schristos M_SEXTB, 529*6881a400Schristos M_SEXTH, 530*6881a400Schristos M_VMSGE, 531*6881a400Schristos M_VMSGEU, 532*6881a400Schristos M_FLH, 533*6881a400Schristos M_FSH, 534699b0f92Schristos M_NUM_MACROS 535699b0f92Schristos }; 536699b0f92Schristos 537*6881a400Schristos /* The mapping symbol states. */ 538*6881a400Schristos enum riscv_seg_mstate 539*6881a400Schristos { 540*6881a400Schristos MAP_NONE = 0, /* Must be zero, for seginfo in new sections. */ 541*6881a400Schristos MAP_DATA, /* Data. */ 542*6881a400Schristos MAP_INSN, /* Instructions. */ 543*6881a400Schristos }; 544699b0f92Schristos 545699b0f92Schristos extern const char * const riscv_gpr_names_numeric[NGPR]; 546699b0f92Schristos extern const char * const riscv_gpr_names_abi[NGPR]; 547699b0f92Schristos extern const char * const riscv_fpr_names_numeric[NFPR]; 548699b0f92Schristos extern const char * const riscv_fpr_names_abi[NFPR]; 549*6881a400Schristos extern const char * const riscv_rm[8]; 550*6881a400Schristos extern const char * const riscv_pred_succ[16]; 551*6881a400Schristos extern const char * const riscv_vecr_names_numeric[NVECR]; 552*6881a400Schristos extern const char * const riscv_vecm_names_numeric[NVECM]; 553*6881a400Schristos extern const char * const riscv_vsew[8]; 554*6881a400Schristos extern const char * const riscv_vlmul[8]; 555*6881a400Schristos extern const char * const riscv_vta[2]; 556*6881a400Schristos extern const char * const riscv_vma[2]; 557699b0f92Schristos 558699b0f92Schristos extern const struct riscv_opcode riscv_opcodes[]; 5597f2ac410Schristos extern const struct riscv_opcode riscv_insn_types[]; 560699b0f92Schristos 561699b0f92Schristos #endif /* _RISCV_H_ */ 562