Skip to content

Commit

Permalink
Checks for separated vector registers
Browse files Browse the repository at this point in the history
  • Loading branch information
vorosl committed Nov 6, 2024
1 parent f632608 commit a3c03d4
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 7 deletions.
93 changes: 89 additions & 4 deletions sljit_src/sljitLir.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@
/* Mask for sljit_emit_enter. */
#define ENTER_GET_REGS(regs) ((regs) & 0xff)
#define ENTER_GET_FLOAT_REGS(regs) (((regs) >> 8) & 0xff)
#if (defined SLJIT_SEPARATE_VECTOR_REGISTERS && SLJIT_SEPARATE_VECTOR_REGISTERS)
#define ENTER_GET_VECTOR_REGS(regs) (((regs) >> 16) & 0xff)
#endif
#define SLJIT_KEPT_SAVEDS_COUNT(options) ((options) & 0x3)

/* Getters for simd operations, which returns with log2(size). */
Expand Down Expand Up @@ -312,6 +315,12 @@
(((fscratches < SLJIT_NUMBER_OF_SCRATCH_FLOAT_REGISTERS ? 0 : (fscratches - SLJIT_NUMBER_OF_SCRATCH_FLOAT_REGISTERS)) + \
(fsaveds)) * SSIZE_OF(type))

#if (defined SLJIT_SEPARATE_VECTOR_REGISTERS && SLJIT_SEPARATE_VECTOR_REGISTERS)
#define GET_SAVED_VECTOR_REGISTERS_SIZE(vscratches, vsaveds, type) \
(((vscratches < SLJIT_NUMBER_OF_SCRATCH_VECTOR_REGISTERS ? 0 : (vscratches - SLJIT_NUMBER_OF_SCRATCH_VECTOR_REGISTERS)) + \
(vsaveds)) * SSIZE_OF(type))
#endif /* SLJIT_SEPARATE_VECTOR_REGISTERS */

#define ADJUST_LOCAL_OFFSET(p, i) \
if ((p) == (SLJIT_MEM1(SLJIT_SP))) \
(i) += SLJIT_LOCALS_OFFSET;
Expand Down Expand Up @@ -504,6 +513,10 @@ SLJIT_API_FUNC_ATTRIBUTE struct sljit_compiler* sljit_create_compiler(void *allo
compiler->saveds = -1;
compiler->fscratches = -1;
compiler->fsaveds = -1;
#if (defined SLJIT_SEPARATE_VECTOR_REGISTERS && SLJIT_SEPARATE_VECTOR_REGISTERS)
compiler->vscratches = -1;
compiler->vsaveds = -1;
#endif /* SLJIT_SEPARATE_VECTOR_REGISTERS */
compiler->local_size = -1;

#if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
Expand Down Expand Up @@ -767,6 +780,10 @@ static SLJIT_INLINE void set_emit_enter(struct sljit_compiler *compiler,
compiler->saveds = ENTER_GET_REGS(saveds);
compiler->fscratches = ENTER_GET_FLOAT_REGS(scratches);
compiler->fsaveds = ENTER_GET_FLOAT_REGS(saveds);
#if (defined SLJIT_SEPARATE_VECTOR_REGISTERS && SLJIT_SEPARATE_VECTOR_REGISTERS)
compiler->vscratches = ENTER_GET_VECTOR_REGS(scratches);
compiler->vsaveds = ENTER_GET_VECTOR_REGS(saveds);
#endif /* SLJIT_SEPARATE_VECTOR_REGISTERS */
#if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
compiler->last_return = args & SLJIT_ARG_MASK;
compiler->logical_local_size = local_size;
Expand All @@ -785,6 +802,10 @@ static SLJIT_INLINE void set_set_context(struct sljit_compiler *compiler,
compiler->saveds = ENTER_GET_REGS(saveds);
compiler->fscratches = ENTER_GET_FLOAT_REGS(scratches);
compiler->fsaveds = ENTER_GET_FLOAT_REGS(saveds);
#if (defined SLJIT_SEPARATE_VECTOR_REGISTERS && SLJIT_SEPARATE_VECTOR_REGISTERS)
compiler->vscratches = ENTER_GET_VECTOR_REGS(scratches);
compiler->vsaveds = ENTER_GET_VECTOR_REGS(saveds);
#endif
#if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
compiler->last_return = args & SLJIT_ARG_MASK;
compiler->logical_local_size = local_size;
Expand Down Expand Up @@ -982,6 +1003,15 @@ static sljit_s32 function_check_dst(struct sljit_compiler *compiler, sljit_s32 p
function_check_is_freg(compiler, (fr), (is_32))

static sljit_s32 function_check_is_freg(struct sljit_compiler *compiler, sljit_s32 fr, sljit_s32 is_32);
#if (defined SLJIT_SEPARATE_VECTOR_REGISTERS && SLJIT_SEPARATE_VECTOR_REGISTERS)
#define FUNCTION_CHECK_IS_VREG(vr, is_32) \
function_check_is_vreg(compiler, (vr), (is_32))

static sljit_s32 function_check_is_vreg(struct sljit_compiler *compiler, sljit_s32 vr, sljit_s32 is_32);
#else
#define FUNCTION_CHECK_IS_VREG(vr, is_32) \
function_check_is_freg(compiler, (vr), (is_32))
#endif

#define FUNCTION_FCHECK(p, i, is_32) \
CHECK_ARGUMENT(function_fcheck(compiler, (p), (i), (is_32)));
Expand All @@ -1001,6 +1031,7 @@ static sljit_s32 function_fcheck(struct sljit_compiler *compiler, sljit_s32 p, s
#define FUNCTION_CHECK_IS_FREG(fr, is_32) \
function_check_is_freg(compiler, (fr))


static sljit_s32 function_check_is_freg(struct sljit_compiler *compiler, sljit_s32 fr)
{
if (compiler->scratches == -1)
Expand All @@ -1010,6 +1041,23 @@ static sljit_s32 function_check_is_freg(struct sljit_compiler *compiler, sljit_s
|| (fr > (SLJIT_FS0 - compiler->fsaveds) && fr <= SLJIT_FS0)
|| (fr >= SLJIT_TMP_FREGISTER_BASE && fr < (SLJIT_TMP_FREGISTER_BASE + SLJIT_NUMBER_OF_TEMPORARY_FLOAT_REGISTERS));
}
#if (defined SLJIT_SEPARATE_VECTOR_REGISTERS && SLJIT_SEPARATE_VECTOR_REGISTERS)
#define FUNCTION_CHECK_IS_VREG(vr, is_32) \
function_check_is_vreg(compiler, (vr))

static sljit_s32 function_check_is_vreg(struct sljit_compiler *compiler, sljit_s32 vr)
{
if (compiler->scratches == -1)
return 0;

return (vr >= SLJIT_VR0 && vr < (SLJIT_VR0 + compiler->vscratches))
|| (vr > (SLJIT_VS0 - compiler->vsaveds) && vr <= SLJIT_VS0)
|| (vr >= SLJIT_TMP_VREGISTER_BASE && vr < (SLJIT_TMP_VREGISTER_BASE + SLJIT_NUMBER_OF_TEMPORARY_VECTOR_REGISTERS));
}
#else
#define FUNCTION_CHECK_IS_VREG(vr, is_32) \
function_check_is_freg(compiler, (vr))
#endif /* SLJIT_SEPARATE_VECTOR_REGISTERS */

#define FUNCTION_FCHECK(p, i, is_32) \
CHECK_ARGUMENT(function_fcheck(compiler, (p), (i)));
Expand Down Expand Up @@ -1280,7 +1328,11 @@ static SLJIT_INLINE CHECK_RETURN_TYPE check_sljit_emit_enter(struct sljit_compil
sljit_s32 real_saveds = ENTER_GET_REGS(saveds);
sljit_s32 real_fscratches = ENTER_GET_FLOAT_REGS(scratches);
sljit_s32 real_fsaveds = ENTER_GET_FLOAT_REGS(saveds);
#endif
#if (defined SLJIT_SEPARATE_VECTOR_REGISTERS && SLJIT_SEPARATE_VECTOR_REGISTERS)
sljit_s32 real_vscratches = ENTER_GET_VECTOR_REGS(scratches);
sljit_s32 real_vsaveds = ENTER_GET_VECTOR_REGS(saveds);
#endif /* SLJIT_SEPARATE_VECTOR_REGISTERS */
#endif /* SLJIT_ARGUMENT_CHECKS */
SLJIT_UNUSED_ARG(compiler);

#if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
Expand All @@ -1297,13 +1349,18 @@ static SLJIT_INLINE CHECK_RETURN_TYPE check_sljit_emit_enter(struct sljit_compil
CHECK_ARGUMENT(real_fscratches >= 0 && real_fscratches <= SLJIT_NUMBER_OF_FLOAT_REGISTERS);
CHECK_ARGUMENT(real_fsaveds >= 0 && real_fsaveds <= SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS);
CHECK_ARGUMENT(real_fscratches + real_fsaveds <= SLJIT_NUMBER_OF_FLOAT_REGISTERS);
#if (defined SLJIT_SEPARATE_VECTOR_REGISTERS && SLJIT_SEPARATE_VECTOR_REGISTERS)
CHECK_ARGUMENT(real_vscratches >= 0 && real_vscratches <= SLJIT_NUMBER_OF_VECTOR_REGISTERS);
CHECK_ARGUMENT(real_vsaveds >= 0 && real_vsaveds <= SLJIT_NUMBER_OF_SAVED_VECTOR_REGISTERS);
CHECK_ARGUMENT(real_vscratches + real_vsaveds <= SLJIT_NUMBER_OF_VECTOR_REGISTERS);
#endif /* SLJIT_SEPARATE_VECTOR_REGISTERS */
CHECK_ARGUMENT(local_size >= 0 && local_size <= SLJIT_MAX_LOCAL_SIZE);
CHECK_ARGUMENT((arg_types & SLJIT_ARG_FULL_MASK) <= SLJIT_ARG_TYPE_F32);
CHECK_ARGUMENT(function_check_arguments(arg_types, real_scratches,
(options & SLJIT_ENTER_REG_ARG) ? 0 : real_saveds, real_fscratches));

compiler->last_flags = 0;
#endif
#endif /* SLJIT_ARGUMENT_CHECKS */
#if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
if (SLJIT_UNLIKELY(!!compiler->verbose)) {
fprintf(compiler->verbose, " enter ret[%s", call_arg_names[arg_types & SLJIT_ARG_MASK]);
Expand Down Expand Up @@ -1335,8 +1392,14 @@ static SLJIT_INLINE CHECK_RETURN_TYPE check_sljit_emit_enter(struct sljit_compil
}
#endif /* !SLJIT_CONFIG_X86 */

#if (defined SLJIT_SEPARATE_VECTOR_REGISTERS && SLJIT_SEPARATE_VECTOR_REGISTERS)
fprintf(compiler->verbose, " scratches:%d, saveds:%d, fscratches:%d, fsaveds:%d, vscratches:%d, vsaveds:%d, local_size:%d\n",
ENTER_GET_REGS(scratches), ENTER_GET_REGS(saveds), ENTER_GET_FLOAT_REGS(scratches), ENTER_GET_FLOAT_REGS(saveds),
ENTER_GET_VECTOR_REGS(scratches), ENTER_GET_VECTOR_REGS(saveds), local_size);
#else
fprintf(compiler->verbose, " scratches:%d, saveds:%d, fscratches:%d, fsaveds:%d, local_size:%d\n",
ENTER_GET_REGS(scratches), ENTER_GET_REGS(saveds), ENTER_GET_FLOAT_REGS(scratches), ENTER_GET_FLOAT_REGS(saveds), local_size);
#endif
}
#endif
CHECK_RETURN_OK;
Expand All @@ -1351,6 +1414,10 @@ static SLJIT_INLINE CHECK_RETURN_TYPE check_sljit_set_context(struct sljit_compi
sljit_s32 real_saveds = ENTER_GET_REGS(saveds);
sljit_s32 real_fscratches = ENTER_GET_FLOAT_REGS(scratches);
sljit_s32 real_fsaveds = ENTER_GET_FLOAT_REGS(saveds);
#if (defined SLJIT_SEPARATE_VECTOR_REGISTERS && SLJIT_SEPARATE_VECTOR_REGISTERS)
sljit_s32 real_vscratches = ENTER_GET_VECTOR_REGS(scratches);
sljit_s32 real_vsaveds = ENTER_GET_VECTOR_REGS(saveds);
#endif
#endif
SLJIT_UNUSED_ARG(compiler);

Expand All @@ -1368,6 +1435,11 @@ static SLJIT_INLINE CHECK_RETURN_TYPE check_sljit_set_context(struct sljit_compi
CHECK_ARGUMENT(real_fscratches >= 0 && real_fscratches <= SLJIT_NUMBER_OF_FLOAT_REGISTERS);
CHECK_ARGUMENT(real_fsaveds >= 0 && real_fsaveds <= SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS);
CHECK_ARGUMENT(real_fscratches + real_fsaveds <= SLJIT_NUMBER_OF_FLOAT_REGISTERS);
#if (defined SLJIT_SEPARATE_VECTOR_REGISTERS && SLJIT_SEPARATE_VECTOR_REGISTERS)
CHECK_ARGUMENT(real_vscratches >= 0 && real_vscratches <= SLJIT_NUMBER_OF_VECTOR_REGISTERS);
CHECK_ARGUMENT(real_vsaveds >= 0 && real_vsaveds <= SLJIT_NUMBER_OF_SAVED_VECTOR_REGISTERS);
CHECK_ARGUMENT(real_vscratches + real_vsaveds <= SLJIT_NUMBER_OF_VECTOR_REGISTERS);
#endif
CHECK_ARGUMENT(local_size >= 0 && local_size <= SLJIT_MAX_LOCAL_SIZE);
CHECK_ARGUMENT((arg_types & SLJIT_ARG_FULL_MASK) < SLJIT_ARG_TYPE_F64);
CHECK_ARGUMENT(function_check_arguments(arg_types, real_scratches,
Expand Down Expand Up @@ -1406,8 +1478,14 @@ static SLJIT_INLINE CHECK_RETURN_TYPE check_sljit_set_context(struct sljit_compi
}
#endif /* !SLJIT_CONFIG_X86 */

#if (defined SLJIT_SEPARATE_VECTOR_REGISTERS && SLJIT_SEPARATE_VECTOR_REGISTERS)
fprintf(compiler->verbose, " scratches:%d, saveds:%d, fscratches:%d, fsaveds:%d, vscratches:%d, vsaveds:%d, local_size:%d\n",
ENTER_GET_REGS(scratches), ENTER_GET_REGS(saveds), ENTER_GET_FLOAT_REGS(scratches), ENTER_GET_FLOAT_REGS(saveds),
ENTER_GET_VECTOR_REGS(scratches), ENTER_GET_VECTOR_REGS(saveds), local_size);
#else
fprintf(compiler->verbose, " scratches:%d, saveds:%d, fscratches:%d, fsaveds:%d, local_size:%d\n",
ENTER_GET_REGS(scratches), ENTER_GET_REGS(saveds), ENTER_GET_FLOAT_REGS(scratches), ENTER_GET_FLOAT_REGS(saveds), local_size);
#endif
}
#endif
CHECK_RETURN_OK;
Expand Down Expand Up @@ -1878,7 +1956,14 @@ static SLJIT_INLINE CHECK_RETURN_TYPE check_sljit_get_register_index(sljit_s32 t
if (type == SLJIT_GP_REGISTER) {
CHECK_ARGUMENT((reg > 0 && reg <= SLJIT_NUMBER_OF_REGISTERS)
|| (reg >= SLJIT_TMP_REGISTER_BASE && reg < (SLJIT_TMP_REGISTER_BASE + SLJIT_NUMBER_OF_TEMPORARY_REGISTERS)));
} else {
}
#if (defined SLJIT_SEPARATE_VECTOR_REGISTERS && SLJIT_SEPARATE_VECTOR_REGISTERS)
else if (((type >> 12) == 0 || ((type >> 12) >= 3 && (type >> 12) <= 6))) {
CHECK_ARGUMENT((reg > 0 && reg <= SLJIT_NUMBER_OF_VECTOR_REGISTERS)
|| (reg >= SLJIT_TMP_VREGISTER_BASE && reg < (SLJIT_TMP_VREGISTER_BASE + SLJIT_NUMBER_OF_TEMPORARY_VECTOR_REGISTERS)));
}
#endif
else {
CHECK_ARGUMENT(type == SLJIT_FLOAT_REGISTER || ((type >> 12) == 0 || ((type >> 12) >= 3 && (type >> 12) <= 6)));
CHECK_ARGUMENT((reg > 0 && reg <= SLJIT_NUMBER_OF_FLOAT_REGISTERS)
|| (reg >= SLJIT_TMP_FREGISTER_BASE && reg < (SLJIT_TMP_FREGISTER_BASE + SLJIT_NUMBER_OF_TEMPORARY_FLOAT_REGISTERS)));
Expand Down Expand Up @@ -2767,7 +2852,7 @@ static SLJIT_INLINE CHECK_RETURN_TYPE check_sljit_emit_simd_mov(struct sljit_com
CHECK_ARGUMENT(SLJIT_SIMD_CHECK_REG(type));
CHECK_ARGUMENT(SLJIT_SIMD_GET_ELEM_SIZE(type) <= SLJIT_SIMD_GET_REG_SIZE(type));
CHECK_ARGUMENT(SLJIT_SIMD_GET_ELEM2_SIZE(type) <= (srcdst & SLJIT_MEM) ? SLJIT_SIMD_GET_REG_SIZE(type) : 0);
CHECK_ARGUMENT(FUNCTION_CHECK_IS_FREG(vreg, 0));
CHECK_ARGUMENT(FUNCTION_CHECK_IS_VREG(vreg, 0));
FUNCTION_FCHECK(srcdst, srcdstw, 0);
#endif
#if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
Expand Down
6 changes: 6 additions & 0 deletions sljit_src/sljitLir.h
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,12 @@ struct sljit_compiler {
sljit_s32 fscratches;
/* Available float saved registers. */
sljit_s32 fsaveds;
#if (defined SLJIT_SEPARATE_VECTOR_REGISTERS && SLJIT_SEPARATE_VECTOR_REGISTERS)
/* Available vector scratch registers. */
sljit_s32 vscratches;
/* Available vector saved registers. */
sljit_s32 vsaveds;
#endif /* SLJIT_SEPARATE_VECTOR_REGISTERS */
/* Local stack size. */
sljit_s32 local_size;
/* Maximum code size. */
Expand Down
6 changes: 3 additions & 3 deletions sljit_src/sljitNativeRISCV_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -2383,10 +2383,10 @@ SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 type, slji
if (type == SLJIT_GP_REGISTER)
return reg_map[reg];

if (type != SLJIT_FLOAT_REGISTER)
return -1;
if (type == SLJIT_FLOAT_REGISTER)
return freg_map[reg];

return freg_map[reg];
return vreg_map[reg];
}

SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
Expand Down

0 comments on commit a3c03d4

Please sign in to comment.