Skip to content

Commit

Permalink
Merge branch 'feature/swizzle'
Browse files Browse the repository at this point in the history
  • Loading branch information
tyfkda committed Nov 14, 2024
2 parents 0edd94c + fa29065 commit d862f44
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 112 deletions.
216 changes: 108 additions & 108 deletions src/as/arch/riscv64/riscv64_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,118 +7,117 @@
#define SP 2

#define IMM(imm, t, b) (((imm) >> (b)) & ((1 << (t - b + 1)) - 1))
#define SWIZZLE_JAL(ofs) ((IMM(ofs, 20, 20) << 31) | (IMM(ofs, 10, 1) << 21) | (IMM(ofs, 11, 11) << 20) | (IMM(ofs, 19, 12) << 12))

// Instruction formats: 32bit=[7|5|5|3|5|7]
#define RTYPE(funct7, rs2, rs1, funct3, rd, opcode) MAKE_CODE32(inst, code, ((funct7) << 25) | ((rs2) << 20) | ((rs1) << 15) | ((funct3) << 12) | ((rd) << 7) | (opcode))
#define ITYPE(imm, rs1, funct3, rd, opcode) MAKE_CODE32(inst, code, (IMM(imm, 11, 0) << 20) | ((rs1) << 15) | ((funct3) << 12) | ((rd) << 7) | (opcode))
#define STYPE(imm, rs2, rs1, funct3, opcode) MAKE_CODE32(inst, code, (IMM(imm, 11, 5) << 25) | ((rs2) << 20) | ((rs1) << 15) | ((funct3) << 12) | (IMM(imm, 4, 0) << 7) | (opcode))
#define BTYPE(imm, rs2, rs1, funct3, opcode) MAKE_CODE32(inst, code, (IMM(imm, 12, 12) << 31) | (IMM(imm, 10, 5) << 25) | ((rs2) << 20) | ((rs1) << 15) | ((funct3) << 12) | (IMM(imm, 4, 0) << 7) | (opcode))
#define UTYPE(imm, rd, opcode) MAKE_CODE32(inst, code, (IMM(imm, 31, 12) << 12) | ((rd) << 7) | (opcode))
#define JTYPE(imm, rd, opcode) MAKE_CODE32(inst, code, (IMM(imm, 20, 20) << 31) | (IMM(imm, 10, 1) << 20) | (IMM(imm, 11, 11) << 19) | (IMM(imm, 19, 12) << 12) | ((rd) << 7) | (opcode))
#define RTYPE(funct7, rs2, rs1, funct3, rd, opcode) (((funct7) << 25) | ((rs2) << 20) | ((rs1) << 15) | ((funct3) << 12) | ((rd) << 7) | (opcode))
#define ITYPE(imm, rs1, funct3, rd, opcode) ((IMM(imm, 11, 0) << 20) | ((rs1) << 15) | ((funct3) << 12) | ((rd) << 7) | (opcode))
#define STYPE(imm, rs2, rs1, funct3, opcode) ((IMM(imm, 11, 5) << 25) | ((rs2) << 20) | ((rs1) << 15) | ((funct3) << 12) | (IMM(imm, 4, 0) << 7) | (opcode))
#define BTYPE(imm, rs2, rs1, funct3, opcode) ((IMM(imm, 12, 12) << 31) | (IMM(imm, 10, 5) << 25) | ((rs2) << 20) | ((rs1) << 15) | ((funct3) << 12) | (IMM(imm, 4, 0) << 7) | (opcode))
#define UTYPE(imm, rd, opcode) ((IMM(imm, 31, 12) << 12) | ((rd) << 7) | (opcode))
#define JTYPE(imm, rd, opcode) ((IMM(imm, 20, 20) << 31) | (IMM(imm, 10, 1) << 20) | (IMM(imm, 11, 11) << 19) | (IMM(imm, 19, 12) << 12) | ((rd) << 7) | (opcode))

// 32-bit instructions
#define W_ADD(rd, rs1, rs2) RTYPE(0x00, rs2, rs1, 0x00, rd, 0x33)
#define W_ADDW(rd, rs1, rs2) RTYPE(0x00, rs2, rs1, 0x00, rd, 0x3b)
#define W_ADDI(rd, rs, imm) ITYPE(imm, rs, 0x00, rd, 0x13)
#define W_ADDIW(rd, rs, imm) ITYPE(imm, rs, 0x00, rd, 0x1b)
#define W_SUB(rd, rs1, rs2) RTYPE(0x20, rs2, rs1, 0x00, rd, 0x33)
#define W_SUBW(rd, rs1, rs2) RTYPE(0x20, rs2, rs1, 0x00, rd, 0x3b)
#define W_MUL(rd, rs1, rs2) RTYPE(0x01, rs2, rs1, 0x00, rd, 0x33)
#define W_MULW(rd, rs1, rs2) RTYPE(0x01, rs2, rs1, 0x00, rd, 0x3b)
#define W_DIV(rd, rs1, rs2) RTYPE(0x01, rs2, rs1, 0x04, rd, 0x33)
#define W_DIVU(rd, rs1, rs2) RTYPE(0x01, rs2, rs1, 0x05, rd, 0x33)
#define W_DIVW(rd, rs1, rs2) RTYPE(0x01, rs2, rs1, 0x04, rd, 0x3b)
#define W_DIVUW(rd, rs1, rs2) RTYPE(0x01, rs2, rs1, 0x05, rd, 0x3b)
#define W_REM(rd, rs1, rs2) RTYPE(0x01, rs2, rs1, 0x06, rd, 0x33)
#define W_REMU(rd, rs1, rs2) RTYPE(0x01, rs2, rs1, 0x07, rd, 0x33)
#define W_REMW(rd, rs1, rs2) RTYPE(0x01, rs2, rs1, 0x06, rd, 0x3b)
#define W_REMUW(rd, rs1, rs2) RTYPE(0x01, rs2, rs1, 0x07, rd, 0x3b)
#define W_AND(rd, rs1, rs2) RTYPE(0x00, rs2, rs1, 0x07, rd, 0x33)
#define W_ANDI(rd, rs, imm) ITYPE(imm, rs, 0x07, rd, 0x13)
#define W_OR(rd, rs1, rs2) RTYPE(0x00, rs2, rs1, 0x06, rd, 0x33)
#define W_ORI(rd, rs, imm) ITYPE(imm, rs, 0x06, rd, 0x13)
#define W_XOR(rd, rs1, rs2) RTYPE(0x00, rs2, rs1, 0x04, rd, 0x33)
#define W_XORI(rd, rs, imm) ITYPE(imm, rs, 0x04, rd, 0x13)
#define W_SLL(rd, rs1, rs2) RTYPE(0x00, rs2, rs1, 0x01, rd, 0x33)
#define W_SLLI(rd, rs, imm) ITYPE((imm) & 63, rs, 0x01, rd, 0x13)
#define W_SLLIW(rd, rs, imm) ITYPE((imm) & 31, rs, 0x01, rd, 0x1b)
#define W_SRL(rd, rs1, rs2) RTYPE(0x00, rs2, rs1, 0x05, rd, 0x33)
#define W_SRLI(rd, rs, imm) ITYPE((imm) & 63, rs, 0x05, rd, 0x13)
#define W_SRA(rd, rs1, rs2) RTYPE(0x20, rs2, rs1, 0x05, rd, 0x33)
#define W_SRAI(rd, rs, imm) ITYPE(0x400 | ((imm) & 63), rs, 0x05, rd, 0x13)
#define W_SLT(rd, rs1, rs2) RTYPE(0x00, rs2, rs1, 0x02, rd, 0x33)
#define W_SLTU(rd, rs1, rs2) RTYPE(0x00, rs2, rs1, 0x03, rd, 0x33)
#define W_SLTI(rd, rs, imm) ITYPE(imm, rs, 0x02, rd, 0x13)
#define W_SLTIU(rd, rs, imm) ITYPE(imm, rs, 0x03, rd, 0x13)
#define W_LB(rd, ofs, rs) ITYPE(ofs, rs, 0x00, rd, 0x03)
#define W_LH(rd, ofs, rs) ITYPE(ofs, rs, 0x01, rd, 0x03)
#define W_LW(rd, ofs, rs) ITYPE(ofs, rs, 0x02, rd, 0x03)
#define W_LD(rd, ofs, rs) ITYPE(ofs, rs, 0x03, rd, 0x03)
#define W_LBU(rd, ofs, rs) ITYPE(ofs, rs, 0x04, rd, 0x03)
#define W_LHU(rd, ofs, rs) ITYPE(ofs, rs, 0x05, rd, 0x03)
#define W_LWU(rd, ofs, rs) ITYPE(ofs, rs, 0x06, rd, 0x03)
#define W_SB(rs2, ofs, rs1) STYPE(ofs, rs2, rs1, 0x00, 0x23)
#define W_SH(rs2, ofs, rs1) STYPE(ofs, rs2, rs1, 0x01, 0x23)
#define W_SW(rs2, ofs, rs1) STYPE(ofs, rs2, rs1, 0x02, 0x23)
#define W_SD(rs2, ofs, rs1) STYPE(ofs, rs2, rs1, 0x03, 0x23)
#define W_LUI(rd, imm) UTYPE(imm, rd, 0x37)
#define W_AUIPC(rd, imm) UTYPE(imm, rd, 0x17)
#define W_JAL(rd, imm) UTYPE(SWIZZLE_JAL(imm), rd, 0x6f)
#define W_JALR(rd, rs, imm) ITYPE(imm, rs, 0x00, rd, 0x67)
#define W_BXX(xx, rs1, rs2, ofs) STYPE(ofs, rs2, rs1, xx, 0x63)
#define W_ECALL() UTYPE(0, 0, 0x73)

#define W_FADD_D(rd, rs1, rs2) RTYPE(0x01, rs2, rs1, 0x07, rd, 0x53)
#define W_FSUB_D(rd, rs1, rs2) RTYPE(0x05, rs2, rs1, 0x07, rd, 0x53)
#define W_FMUL_D(rd, rs1, rs2) RTYPE(0x09, rs2, rs1, 0x07, rd, 0x53)
#define W_FDIV_D(rd, rs1, rs2) RTYPE(0x0d, rs2, rs1, 0x07, rd, 0x53)
#define W_FADD_S(rd, rs1, rs2) RTYPE(0x00, rs2, rs1, 0x07, rd, 0x53)
#define W_FSUB_S(rd, rs1, rs2) RTYPE(0x04, rs2, rs1, 0x07, rd, 0x53)
#define W_FMUL_S(rd, rs1, rs2) RTYPE(0x08, rs2, rs1, 0x07, rd, 0x53)
#define W_FDIV_S(rd, rs1, rs2) RTYPE(0x0c, rs2, rs1, 0x07, rd, 0x53)
#define W_FSQRT_D(rd, rs) RTYPE(0x2d, 0, rs, 0x07, rd, 0x53)
#define W_FSQRT_S(rd, rs) RTYPE(0x2c, 0, rs, 0x07, rd, 0x53)
#define W_FEQ_D(rd, rs1, rs2) RTYPE(0x51, rs2, rs1, 0x02, rd, 0x53)
#define W_FLT_D(rd, rs1, rs2) RTYPE(0x51, rs2, rs1, 0x01, rd, 0x53)
#define W_FLE_D(rd, rs1, rs2) RTYPE(0x51, rs2, rs1, 0x00, rd, 0x53)
#define W_FEQ_S(rd, rs1, rs2) RTYPE(0x50, rs2, rs1, 0x02, rd, 0x53)
#define W_FLT_S(rd, rs1, rs2) RTYPE(0x50, rs2, rs1, 0x01, rd, 0x53)
#define W_FLE_S(rd, rs1, rs2) RTYPE(0x50, rs2, rs1, 0x00, rd, 0x53)
#define W_FLD(rd, ofs, rs) ITYPE(ofs, rs, 0x03, rd, 0x07)
#define W_FLW(rd, ofs, rs) ITYPE(ofs, rs, 0x02, rd, 0x07)
#define W_FSD(rs2, ofs, rs1) STYPE(ofs, rs2, rs1, 0x03, 0x27)
#define W_FSW(rs2, ofs, rs1) STYPE(ofs, rs2, rs1, 0x02, 0x27)
#define W_FSGNJ_D(rd, rs1, rs2) RTYPE(0x11, rs2, rs1, 0x00, rd, 0x53)
#define W_FSGNJ_S(rd, rs1, rs2) RTYPE(0x10, rs2, rs1, 0x00, rd, 0x53)
#define W_FSGNJN_D(rd, rs1, rs2) RTYPE(0x11, rs2, rs1, 0x01, rd, 0x53)
#define W_FSGNJN_S(rd, rs1, rs2) RTYPE(0x10, rs2, rs1, 0x01, rd, 0x53)
#define W_FSGNJX_D(rd, rs1, rs2) RTYPE(0x11, rs2, rs1, 0x02, rd, 0x53)
#define W_FSGNJX_S(rd, rs1, rs2) RTYPE(0x10, rs2, rs1, 0x02, rd, 0x53)

#define W_FCVT_D_W(rd, rs) RTYPE(0x69, 0, rs, 0x00, rd, 0x53)
#define W_FCVT_D_WU(rd, rs) RTYPE(0x69, 1, rs, 0x00, rd, 0x53)
#define W_FCVT_D_L(rd, rs) RTYPE(0x69, 2, rs, 0x07, rd, 0x53)
#define W_FCVT_D_LU(rd, rs) RTYPE(0x69, 3, rs, 0x07, rd, 0x53)
#define W_FCVT_S_W(rd, rs) RTYPE(0x68, 0, rs, 0x07, rd, 0x53)
#define W_FCVT_S_WU(rd, rs) RTYPE(0x68, 1, rs, 0x07, rd, 0x53)
#define W_FCVT_S_L(rd, rs) RTYPE(0x68, 2, rs, 0x07, rd, 0x53)
#define W_FCVT_S_LU(rd, rs) RTYPE(0x68, 3, rs, 0x07, rd, 0x53)

#define W_FCVT_W_D(rd, rs, rm) RTYPE(0x61, 0, rs, rm, rd, 0x53)
#define W_FCVT_WU_D(rd, rs, rm) RTYPE(0x61, 1, rs, rm, rd, 0x53)
#define W_FCVT_L_D(rd, rs, rm) RTYPE(0x61, 2, rs, rm, rd, 0x53)
#define W_FCVT_LU_D(rd, rs, rm) RTYPE(0x61, 3, rs, rm, rd, 0x53)
#define W_FCVT_W_S(rd, rs, rm) RTYPE(0x60, 0, rs, rm, rd, 0x53)
#define W_FCVT_WU_S(rd, rs, rm) RTYPE(0x60, 1, rs, rm, rd, 0x53)
#define W_FCVT_L_S(rd, rs, rm) RTYPE(0x60, 2, rs, rm, rd, 0x53)
#define W_FCVT_LU_S(rd, rs, rm) RTYPE(0x60, 3, rs, rm, rd, 0x53)

#define W_FCVT_D_S(rd, rs) RTYPE(0x21, 0, rs, 0x00, rd, 0x53)
#define W_FCVT_S_D(rd, rs) RTYPE(0x20, 1, rs, 0x07, rd, 0x53)

#define W_FMV_X_D(rd, rs) RTYPE(0x71, 0, rs, 0x00, rd, 0x53)
#define W_FMV_X_W(rd, rs) RTYPE(0x70, 0, rs, 0x00, rd, 0x53)
#define W_ADD(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x00, rs2, rs1, 0x00, rd, 0x33))
#define W_ADDW(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x00, rs2, rs1, 0x00, rd, 0x3b))
#define W_ADDI(rd, rs, imm) MAKE_CODE32(inst, code, ITYPE(imm, rs, 0x00, rd, 0x13))
#define W_ADDIW(rd, rs, imm) MAKE_CODE32(inst, code, ITYPE(imm, rs, 0x00, rd, 0x1b))
#define W_SUB(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x20, rs2, rs1, 0x00, rd, 0x33))
#define W_SUBW(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x20, rs2, rs1, 0x00, rd, 0x3b))
#define W_MUL(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x01, rs2, rs1, 0x00, rd, 0x33))
#define W_MULW(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x01, rs2, rs1, 0x00, rd, 0x3b))
#define W_DIV(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x01, rs2, rs1, 0x04, rd, 0x33))
#define W_DIVU(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x01, rs2, rs1, 0x05, rd, 0x33))
#define W_DIVW(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x01, rs2, rs1, 0x04, rd, 0x3b))
#define W_DIVUW(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x01, rs2, rs1, 0x05, rd, 0x3b))
#define W_REM(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x01, rs2, rs1, 0x06, rd, 0x33))
#define W_REMU(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x01, rs2, rs1, 0x07, rd, 0x33))
#define W_REMW(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x01, rs2, rs1, 0x06, rd, 0x3b))
#define W_REMUW(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x01, rs2, rs1, 0x07, rd, 0x3b))
#define W_AND(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x00, rs2, rs1, 0x07, rd, 0x33))
#define W_ANDI(rd, rs, imm) MAKE_CODE32(inst, code, ITYPE(imm, rs, 0x07, rd, 0x13))
#define W_OR(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x00, rs2, rs1, 0x06, rd, 0x33))
#define W_ORI(rd, rs, imm) MAKE_CODE32(inst, code, ITYPE(imm, rs, 0x06, rd, 0x13))
#define W_XOR(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x00, rs2, rs1, 0x04, rd, 0x33))
#define W_XORI(rd, rs, imm) MAKE_CODE32(inst, code, ITYPE(imm, rs, 0x04, rd, 0x13))
#define W_SLL(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x00, rs2, rs1, 0x01, rd, 0x33))
#define W_SLLI(rd, rs, imm) MAKE_CODE32(inst, code, ITYPE((imm) & 63, rs, 0x01, rd, 0x13))
#define W_SLLIW(rd, rs, imm) MAKE_CODE32(inst, code, ITYPE((imm) & 31, rs, 0x01, rd, 0x1b))
#define W_SRL(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x00, rs2, rs1, 0x05, rd, 0x33))
#define W_SRLI(rd, rs, imm) MAKE_CODE32(inst, code, ITYPE((imm) & 63, rs, 0x05, rd, 0x13))
#define W_SRA(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x20, rs2, rs1, 0x05, rd, 0x33))
#define W_SRAI(rd, rs, imm) MAKE_CODE32(inst, code, ITYPE(0x400 | ((imm) & 63), rs, 0x05, rd, 0x13))
#define W_SLT(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x00, rs2, rs1, 0x02, rd, 0x33))
#define W_SLTU(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x00, rs2, rs1, 0x03, rd, 0x33))
#define W_SLTI(rd, rs, imm) MAKE_CODE32(inst, code, ITYPE(imm, rs, 0x02, rd, 0x13))
#define W_SLTIU(rd, rs, imm) MAKE_CODE32(inst, code, ITYPE(imm, rs, 0x03, rd, 0x13))
#define W_LB(rd, ofs, rs) MAKE_CODE32(inst, code, ITYPE(ofs, rs, 0x00, rd, 0x03))
#define W_LH(rd, ofs, rs) MAKE_CODE32(inst, code, ITYPE(ofs, rs, 0x01, rd, 0x03))
#define W_LW(rd, ofs, rs) MAKE_CODE32(inst, code, ITYPE(ofs, rs, 0x02, rd, 0x03))
#define W_LD(rd, ofs, rs) MAKE_CODE32(inst, code, ITYPE(ofs, rs, 0x03, rd, 0x03))
#define W_LBU(rd, ofs, rs) MAKE_CODE32(inst, code, ITYPE(ofs, rs, 0x04, rd, 0x03))
#define W_LHU(rd, ofs, rs) MAKE_CODE32(inst, code, ITYPE(ofs, rs, 0x05, rd, 0x03))
#define W_LWU(rd, ofs, rs) MAKE_CODE32(inst, code, ITYPE(ofs, rs, 0x06, rd, 0x03))
#define W_SB(rs2, ofs, rs1) MAKE_CODE32(inst, code, STYPE(ofs, rs2, rs1, 0x00, 0x23))
#define W_SH(rs2, ofs, rs1) MAKE_CODE32(inst, code, STYPE(ofs, rs2, rs1, 0x01, 0x23))
#define W_SW(rs2, ofs, rs1) MAKE_CODE32(inst, code, STYPE(ofs, rs2, rs1, 0x02, 0x23))
#define W_SD(rs2, ofs, rs1) MAKE_CODE32(inst, code, STYPE(ofs, rs2, rs1, 0x03, 0x23))
#define W_LUI(rd, imm) MAKE_CODE32(inst, code, UTYPE(imm, rd, 0x37))
#define W_AUIPC(rd, imm) MAKE_CODE32(inst, code, UTYPE(imm, rd, 0x17))
#define W_JAL(rd, imm) MAKE_CODE32(inst, code, UTYPE(SWIZZLE_JAL(imm), rd, 0x6f))
#define W_JALR(rd, rs, imm) MAKE_CODE32(inst, code, ITYPE(imm, rs, 0x00, rd, 0x67))
#define W_BXX(xx, rs1, rs2, ofs) MAKE_CODE32(inst, code, STYPE(0, rs2, rs1, xx, 0x63) | SWIZZLE_BXX(ofs))
#define W_ECALL() MAKE_CODE32(inst, code, UTYPE(0, 0, 0x73))

#define W_FADD_D(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x01, rs2, rs1, 0x07, rd, 0x53))
#define W_FSUB_D(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x05, rs2, rs1, 0x07, rd, 0x53))
#define W_FMUL_D(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x09, rs2, rs1, 0x07, rd, 0x53))
#define W_FDIV_D(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x0d, rs2, rs1, 0x07, rd, 0x53))
#define W_FADD_S(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x00, rs2, rs1, 0x07, rd, 0x53))
#define W_FSUB_S(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x04, rs2, rs1, 0x07, rd, 0x53))
#define W_FMUL_S(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x08, rs2, rs1, 0x07, rd, 0x53))
#define W_FDIV_S(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x0c, rs2, rs1, 0x07, rd, 0x53))
#define W_FSQRT_D(rd, rs) MAKE_CODE32(inst, code, RTYPE(0x2d, 0, rs, 0x07, rd, 0x53))
#define W_FSQRT_S(rd, rs) MAKE_CODE32(inst, code, RTYPE(0x2c, 0, rs, 0x07, rd, 0x53))
#define W_FEQ_D(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x51, rs2, rs1, 0x02, rd, 0x53))
#define W_FLT_D(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x51, rs2, rs1, 0x01, rd, 0x53))
#define W_FLE_D(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x51, rs2, rs1, 0x00, rd, 0x53))
#define W_FEQ_S(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x50, rs2, rs1, 0x02, rd, 0x53))
#define W_FLT_S(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x50, rs2, rs1, 0x01, rd, 0x53))
#define W_FLE_S(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x50, rs2, rs1, 0x00, rd, 0x53))
#define W_FLD(rd, ofs, rs) MAKE_CODE32(inst, code, ITYPE(ofs, rs, 0x03, rd, 0x07))
#define W_FLW(rd, ofs, rs) MAKE_CODE32(inst, code, ITYPE(ofs, rs, 0x02, rd, 0x07))
#define W_FSD(rs2, ofs, rs1) MAKE_CODE32(inst, code, STYPE(ofs, rs2, rs1, 0x03, 0x27))
#define W_FSW(rs2, ofs, rs1) MAKE_CODE32(inst, code, STYPE(ofs, rs2, rs1, 0x02, 0x27))
#define W_FSGNJ_D(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x11, rs2, rs1, 0x00, rd, 0x53))
#define W_FSGNJ_S(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x10, rs2, rs1, 0x00, rd, 0x53))
#define W_FSGNJN_D(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x11, rs2, rs1, 0x01, rd, 0x53))
#define W_FSGNJN_S(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x10, rs2, rs1, 0x01, rd, 0x53))
#define W_FSGNJX_D(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x11, rs2, rs1, 0x02, rd, 0x53))
#define W_FSGNJX_S(rd, rs1, rs2) MAKE_CODE32(inst, code, RTYPE(0x10, rs2, rs1, 0x02, rd, 0x53))

#define W_FCVT_D_W(rd, rs) MAKE_CODE32(inst, code, RTYPE(0x69, 0, rs, 0x00, rd, 0x53))
#define W_FCVT_D_WU(rd, rs) MAKE_CODE32(inst, code, RTYPE(0x69, 1, rs, 0x00, rd, 0x53))
#define W_FCVT_D_L(rd, rs) MAKE_CODE32(inst, code, RTYPE(0x69, 2, rs, 0x07, rd, 0x53))
#define W_FCVT_D_LU(rd, rs) MAKE_CODE32(inst, code, RTYPE(0x69, 3, rs, 0x07, rd, 0x53))
#define W_FCVT_S_W(rd, rs) MAKE_CODE32(inst, code, RTYPE(0x68, 0, rs, 0x07, rd, 0x53))
#define W_FCVT_S_WU(rd, rs) MAKE_CODE32(inst, code, RTYPE(0x68, 1, rs, 0x07, rd, 0x53))
#define W_FCVT_S_L(rd, rs) MAKE_CODE32(inst, code, RTYPE(0x68, 2, rs, 0x07, rd, 0x53))
#define W_FCVT_S_LU(rd, rs) MAKE_CODE32(inst, code, RTYPE(0x68, 3, rs, 0x07, rd, 0x53))

#define W_FCVT_W_D(rd, rs, rm) MAKE_CODE32(inst, code, RTYPE(0x61, 0, rs, rm, rd, 0x53))
#define W_FCVT_WU_D(rd, rs, rm) MAKE_CODE32(inst, code, RTYPE(0x61, 1, rs, rm, rd, 0x53))
#define W_FCVT_L_D(rd, rs, rm) MAKE_CODE32(inst, code, RTYPE(0x61, 2, rs, rm, rd, 0x53))
#define W_FCVT_LU_D(rd, rs, rm) MAKE_CODE32(inst, code, RTYPE(0x61, 3, rs, rm, rd, 0x53))
#define W_FCVT_W_S(rd, rs, rm) MAKE_CODE32(inst, code, RTYPE(0x60, 0, rs, rm, rd, 0x53))
#define W_FCVT_WU_S(rd, rs, rm) MAKE_CODE32(inst, code, RTYPE(0x60, 1, rs, rm, rd, 0x53))
#define W_FCVT_L_S(rd, rs, rm) MAKE_CODE32(inst, code, RTYPE(0x60, 2, rs, rm, rd, 0x53))
#define W_FCVT_LU_S(rd, rs, rm) MAKE_CODE32(inst, code, RTYPE(0x60, 3, rs, rm, rd, 0x53))

#define W_FCVT_D_S(rd, rs) MAKE_CODE32(inst, code, RTYPE(0x21, 0, rs, 0x00, rd, 0x53))
#define W_FCVT_S_D(rd, rs) MAKE_CODE32(inst, code, RTYPE(0x20, 1, rs, 0x07, rd, 0x53))

#define W_FMV_X_D(rd, rs) MAKE_CODE32(inst, code, RTYPE(0x71, 0, rs, 0x00, rd, 0x53))
#define W_FMV_X_W(rd, rs) MAKE_CODE32(inst, code, RTYPE(0x70, 0, rs, 0x00, rd, 0x53))

// Compressed instructions
#define C_MV(rd, rs) MAKE_CODE16(inst, code, 0x8002 | ((rd) << 7) | ((rs) << 2))
Expand Down Expand Up @@ -186,6 +185,7 @@
#define SWIZZLE_BXX(offset) \
((IMM(offset, 12, 12) << 31) | (IMM(offset, 10, 5) << 25) | \
(IMM(offset, 4, 1) << 8) | (IMM(offset, 11, 11) << 7))
#define SWIZZLE_JAL(ofs) ((IMM(ofs, 20, 20) << 31) | (IMM(ofs, 10, 1) << 21) | (IMM(ofs, 11, 11) << 20) | (IMM(ofs, 19, 12) << 12))

inline bool is_rvc_reg(int reg) { return reg >= 8 && reg <= 15; } // X8~X15
inline int to_rvc_reg(int reg) { return reg - 8; }
Expand Down
8 changes: 4 additions & 4 deletions src/ld/ld.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ static uint64_t ld_symbol_address(LinkEditor *ld, const Name *name) {
(IMM(offset, 4, 1) << 8) | (IMM(offset, 11, 11) << 7))

#define MAKE_CODE32(x) (x)
#define ITYPE(imm, rs1, funct3, rd, opcode) MAKE_CODE32((IMM(imm, 11, 0) << 20) | ((rs1) << 15) | ((funct3) << 12) | ((rd) << 7) | (opcode))
#define UTYPE(imm, rd, opcode) MAKE_CODE32((IMM(imm, 31, 12) << 12) | ((rd) << 7) | (opcode))
#define W_JAL(rd, imm) UTYPE(SWIZZLE_JAL(imm), rd, 0x6f)
#define W_ADDI(rd, rs, imm) ITYPE(imm, rs, 0x00, rd, 0x13)
#define ITYPE(imm, rs1, funct3, rd, opcode) ((IMM(imm, 11, 0) << 20) | ((rs1) << 15) | ((funct3) << 12) | ((rd) << 7) | (opcode))
#define UTYPE(imm, rd, opcode) ((IMM(imm, 31, 12) << 12) | ((rd) << 7) | (opcode))
#define W_JAL(rd, imm) MAKE_CODE32(UTYPE(SWIZZLE_JAL(imm), rd, 0x6f))
#define W_ADDI(rd, rs, imm) MAKE_CODE32(ITYPE(imm, rs, 0x00, rd, 0x13))
#define P_NOP() W_ADDI(ZERO, ZERO, 0)

static uint64_t calc_rela_sym_address(LinkEditor *ld, ElfObj *elfobj, const Elf64_Rela *rela, const Elf64_Sym *sym, const ElfSectionInfo *strinfo) {
Expand Down

0 comments on commit d862f44

Please sign in to comment.