Skip to content

Commit

Permalink
Change instruction size unit to 16 bit on RISCV (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
zherczeg authored Jan 11, 2025
1 parent 8481dde commit 2cdf4ed
Show file tree
Hide file tree
Showing 5 changed files with 280 additions and 168 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ jobs:
- name: Build and test
env:
CROSS_COMPILER: ./riscv32-ilp32d--glibc--bleeding-edge-2024.02-1/bin/riscv32-buildroot-linux-gnu-gcc-13.2.0.br_real
CFLAGS: -march=rv32gv_zba_zbb
CFLAGS: -march=rv32gcv_zba_zbb
EXTRA_LDFLAGS: -static
run: |
make all
Expand Down Expand Up @@ -221,7 +221,7 @@ jobs:
- name: Build and test
env:
CROSS_COMPILER: ./riscv64-lp64d--glibc--stable-2024.02-1/bin/riscv64-buildroot-linux-gnu-gcc-12.3.0.br_real
CFLAGS: -march=rv64gv_zba_zbb
CFLAGS: -march=rv64gcv_zba_zbb
EXTRA_LDFLAGS: -static
run: |
make all
Expand Down
17 changes: 9 additions & 8 deletions sljit_src/sljitLir.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,17 +275,18 @@

# define PATCH_B 0x010
# define PATCH_J 0x020
# define PATCH_J16 0x040

#if (defined SLJIT_CONFIG_RISCV_64 && SLJIT_CONFIG_RISCV_64)
# define PATCH_REL32 0x040
# define PATCH_ABS32 0x080
# define PATCH_ABS44 0x100
# define PATCH_ABS52 0x200
# define PATCH_REL32 0x080
# define PATCH_ABS32 0x100
# define PATCH_ABS44 0x200
# define PATCH_ABS52 0x400
# define JUMP_SIZE_SHIFT 58
# define JUMP_MAX_SIZE ((sljit_uw)6)
# define JUMP_MAX_SIZE ((sljit_uw)12)
#else /* !SLJIT_CONFIG_RISCV_64 */
# define JUMP_SIZE_SHIFT 26
# define JUMP_MAX_SIZE ((sljit_uw)2)
# define JUMP_MAX_SIZE ((sljit_uw)4)
#endif /* SLJIT_CONFIG_RISCV_64 */
#endif /* SLJIT_CONFIG_RISCV */

Expand Down Expand Up @@ -2005,12 +2006,12 @@ static SLJIT_INLINE CHECK_RETURN_TYPE check_sljit_emit_op_custom(struct sljit_co

#if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86)
CHECK_ARGUMENT(size > 0 && size < 16);
#elif (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2)
#elif (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2) || (defined SLJIT_CONFIG_RISCV && SLJIT_CONFIG_RISCV)
CHECK_ARGUMENT((size == 2 && (((sljit_sw)instruction) & 0x1) == 0)
|| (size == 4 && (((sljit_sw)instruction) & 0x3) == 0));
#elif (defined SLJIT_CONFIG_S390X && SLJIT_CONFIG_S390X)
CHECK_ARGUMENT(size == 2 || size == 4 || size == 6);
#else /* !SLJIT_CONFIG_X86 && !SLJIT_CONFIG_ARM_THUMB2 && !SLJIT_CONFIG_S390X */
#else /* !SLJIT_CONFIG_X86 && !SLJIT_CONFIG_ARM_THUMB2 && !SLJIT_CONFIG_RISCV && !SLJIT_CONFIG_S390X */
CHECK_ARGUMENT(size == 4 && (((sljit_sw)instruction) & 0x3) == 0);
#endif /* SLJIT_CONFIG_X86 */

Expand Down
19 changes: 10 additions & 9 deletions sljit_src/sljitNativeRISCV_32.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,21 @@ static SLJIT_INLINE sljit_s32 emit_const(struct sljit_compiler *compiler, sljit_

SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
{
sljit_ins *inst = (sljit_ins*)addr;
sljit_u16 *inst = (sljit_u16*)addr;
SLJIT_UNUSED_ARG(executable_offset);

if ((new_target & 0x800) != 0)
new_target += 0x1000;

SLJIT_UPDATE_WX_FLAGS(inst, inst + 5, 0);
SLJIT_UPDATE_WX_FLAGS(inst, inst + 4, 0);

SLJIT_ASSERT((inst[0] & 0x7f) == LUI);
inst[0] = (inst[0] & 0xfff) | (sljit_ins)((sljit_sw)new_target & ~0xfff);
SLJIT_ASSERT((inst[1] & 0x707f) == ADDI || (inst[1] & 0x707f) == JALR);
inst[1] = (inst[1] & 0xfffff) | IMM_I(new_target);

SLJIT_UPDATE_WX_FLAGS(inst, inst + 5, 1);
inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
SLJIT_CACHE_FLUSH(inst, inst + 5);
inst[0] = (sljit_u16)((inst[0] & 0xfff) | (new_target & ~(sljit_uw)0xfff));
inst[1] = (sljit_u16)(new_target >> 16);
SLJIT_ASSERT((inst[2] & 0x707f) == ADDI || (inst[2] & 0x707f) == JALR);
inst[3] = (sljit_u16)((inst[3] & 0xf) | (new_target << 4));

SLJIT_UPDATE_WX_FLAGS(inst, inst + 4, 1);
inst = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
SLJIT_CACHE_FLUSH(inst, inst + 4);
}
28 changes: 15 additions & 13 deletions sljit_src/sljitNativeRISCV_64.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ static SLJIT_INLINE sljit_s32 emit_const(struct sljit_compiler *compiler, sljit_

SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
{
sljit_ins *inst = (sljit_ins*)addr;
sljit_u16 *inst = (sljit_u16*)addr;
sljit_sw high;
SLJIT_UNUSED_ARG(executable_offset);

Expand All @@ -205,18 +205,20 @@ SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_ta
if ((high & 0x800) != 0)
high += 0x1000;

SLJIT_UPDATE_WX_FLAGS(inst, inst + 5, 0);
SLJIT_UPDATE_WX_FLAGS(inst, inst + 12, 0);

SLJIT_ASSERT((inst[0] & 0x7f) == LUI);
inst[0] = (inst[0] & 0xfff) | (sljit_ins)(high & ~0xfff);
SLJIT_ASSERT((inst[1] & 0x707f) == ADDI);
inst[1] = (inst[1] & 0xfffff) | IMM_I(high);
SLJIT_ASSERT((inst[2] & 0x7f) == LUI);
inst[2] = (inst[2] & 0xfff) | (sljit_ins)((sljit_sw)new_target & ~0xfff);
SLJIT_ASSERT((inst[5] & 0x707f) == ADDI || (inst[5] & 0x707f) == JALR);
inst[5] = (inst[5] & 0xfffff) | IMM_I(new_target);
SLJIT_UPDATE_WX_FLAGS(inst, inst + 5, 1);

inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
SLJIT_CACHE_FLUSH(inst, inst + 5);
inst[0] = (sljit_u16)((inst[0] & 0xfff) | (high & ~(sljit_sw)0xfff));
inst[1] = (sljit_u16)(high >> 16);
SLJIT_ASSERT((inst[2] & 0x707f) == ADDI);
inst[3] = (sljit_u16)((inst[3] & 0xf) | (high << 4));
SLJIT_ASSERT((inst[4] & 0x7f) == LUI);
inst[4] = (sljit_u16)((inst[4] & 0xfff) | (new_target & ~(sljit_uw)0xfff));
inst[5] = (sljit_u16)(new_target >> 16);
SLJIT_ASSERT((inst[10] & 0x707f) == ADDI || (inst[10] & 0x707f) == JALR);
inst[11] = (sljit_u16)((inst[11] & 0xf) | (new_target << 4));
SLJIT_UPDATE_WX_FLAGS(inst, inst + 12, 1);

inst = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
SLJIT_CACHE_FLUSH(inst, inst + 12);
}
Loading

0 comments on commit 2cdf4ed

Please sign in to comment.