In preparation for TARGET_TB_PCREL, reduce reliance on absolute values.
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
target/arm/translate.c | 37 +++++++++++++++++++++----------------
1 file changed, 21 insertions(+), 16 deletions(-)
diff --git a/target/arm/translate.c b/target/arm/translate.c
index e0b1d415a2..fd35db8c8c 100644
--- a/target/arm/translate.c
+++ b/target/arm/translate.c
@@ -270,6 +270,12 @@ static uint32_t read_pc(DisasContext *s)
return s->pc_curr + (s->thumb ? 4 : 8);
}
+/* The pc_curr difference for an architectural jump. */
+static target_long jmp_diff(DisasContext *s, target_long diff)
+{
+ return diff + (s->thumb ? 4 : 8);
+}
+
/* Set a variable to the value of a CPU register. */
void load_reg_var(DisasContext *s, TCGv_i32 var, int reg)
{
@@ -2596,7 +2602,7 @@ static void gen_goto_ptr(void)
* cpu_loop_exec. Any live exit_requests will be processed as we
* enter the next TB.
*/
-static void gen_goto_tb(DisasContext *s, int n, int diff)
+static void gen_goto_tb(DisasContext *s, int n, target_long diff)
{
target_ulong dest = s->pc_curr + diff;
@@ -2612,10 +2618,8 @@ static void gen_goto_tb(DisasContext *s, int n, int diff)
}
/* Jump, specifying which TB number to use if we gen_goto_tb() */
-static inline void gen_jmp_tb(DisasContext *s, uint32_t dest, int tbno)
+static void gen_jmp_tb(DisasContext *s, target_long diff, int tbno)
{
- int diff = dest - s->pc_curr;
-
if (unlikely(s->ss_active)) {
/* An indirect jump so that we still trigger the debug exception. */
gen_update_pc(s, diff);
@@ -2657,9 +2661,9 @@ static inline void gen_jmp_tb(DisasContext *s, uint32_t dest, int tbno)
}
}
-static inline void gen_jmp(DisasContext *s, uint32_t dest)
+static inline void gen_jmp(DisasContext *s, target_long diff)
{
- gen_jmp_tb(s, dest, 0);
+ gen_jmp_tb(s, diff, 0);
}
static inline void gen_mulxy(TCGv_i32 t0, TCGv_i32 t1, int x, int y)
@@ -8326,7 +8330,7 @@ static bool trans_CLRM(DisasContext *s, arg_CLRM *a)
static bool trans_B(DisasContext *s, arg_i *a)
{
- gen_jmp(s, read_pc(s) + a->imm);
+ gen_jmp(s, jmp_diff(s, a->imm));
return true;
}
@@ -8341,14 +8345,14 @@ static bool trans_B_cond_thumb(DisasContext *s, arg_ci *a)
return true;
}
arm_skip_unless(s, a->cond);
- gen_jmp(s, read_pc(s) + a->imm);
+ gen_jmp(s, jmp_diff(s, a->imm));
return true;
}
static bool trans_BL(DisasContext *s, arg_i *a)
{
tcg_gen_movi_i32(cpu_R[14], s->base.pc_next | s->thumb);
- gen_jmp(s, read_pc(s) + a->imm);
+ gen_jmp(s, jmp_diff(s, a->imm));
return true;
}
@@ -8368,7 +8372,8 @@ static bool trans_BLX_i(DisasContext *s, arg_BLX_i *a)
}
tcg_gen_movi_i32(cpu_R[14], s->base.pc_next | s->thumb);
store_cpu_field_constant(!s->thumb, thumb);
- gen_jmp(s, (read_pc(s) & ~3) + a->imm);
+ /* This difference computes a page offset so ok for TARGET_TB_PCREL. */
+ gen_jmp(s, (read_pc(s) & ~3) - s->pc_curr + a->imm);
return true;
}
@@ -8529,10 +8534,10 @@ static bool trans_WLS(DisasContext *s, arg_WLS *a)
* when we take this upcoming exit from this TB, so gen_jmp_tb() is OK.
*/
}
- gen_jmp_tb(s, s->base.pc_next, 1);
+ gen_jmp_tb(s, curr_insn_len(s), 1);
gen_set_label(nextlabel);
- gen_jmp(s, read_pc(s) + a->imm);
+ gen_jmp(s, jmp_diff(s, a->imm));
return true;
}
@@ -8612,7 +8617,7 @@ static bool trans_LE(DisasContext *s, arg_LE *a)
if (a->f) {
/* Loop-forever: just jump back to the loop start */
- gen_jmp(s, read_pc(s) - a->imm);
+ gen_jmp(s, jmp_diff(s, -a->imm));
return true;
}
@@ -8643,7 +8648,7 @@ static bool trans_LE(DisasContext *s, arg_LE *a)
tcg_temp_free_i32(decr);
}
/* Jump back to the loop start */
- gen_jmp(s, read_pc(s) - a->imm);
+ gen_jmp(s, jmp_diff(s, -a->imm));
gen_set_label(loopend);
if (a->tp) {
@@ -8651,7 +8656,7 @@ static bool trans_LE(DisasContext *s, arg_LE *a)
store_cpu_field(tcg_constant_i32(4), v7m.ltpsize);
}
/* End TB, continuing to following insn */
- gen_jmp_tb(s, s->base.pc_next, 1);
+ gen_jmp_tb(s, curr_insn_len(s), 1);
return true;
}
@@ -8750,7 +8755,7 @@ static bool trans_CBZ(DisasContext *s, arg_CBZ *a)
tcg_gen_brcondi_i32(a->nz ? TCG_COND_EQ : TCG_COND_NE,
tmp, 0, s->condlabel);
tcg_temp_free_i32(tmp);
- gen_jmp(s, read_pc(s) + a->imm);
+ gen_jmp(s, jmp_diff(s, a->imm));
return true;
}
--
2.34.1
On Fri, 30 Sept 2022 at 23:10, Richard Henderson
<richard.henderson@linaro.org> wrote:
>
> In preparation for TARGET_TB_PCREL, reduce reliance on absolute values.
>
> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
> ---
> target/arm/translate.c | 37 +++++++++++++++++++++----------------
> 1 file changed, 21 insertions(+), 16 deletions(-)
> @@ -8368,7 +8372,8 @@ static bool trans_BLX_i(DisasContext *s, arg_BLX_i *a)
> }
> tcg_gen_movi_i32(cpu_R[14], s->base.pc_next | s->thumb);
> store_cpu_field_constant(!s->thumb, thumb);
> - gen_jmp(s, (read_pc(s) & ~3) + a->imm);
> + /* This difference computes a page offset so ok for TARGET_TB_PCREL. */
> + gen_jmp(s, (read_pc(s) & ~3) - s->pc_curr + a->imm);
Could we just calculate the offset of the jump target instead?
read_pc() returns s->pc_curr + a constant, so the s->pc_curr cancels
out anyway:
(read_pc(s) & ~3) - s->pc_curr + a->imm
==
(pc_curr + (s->thumb ? 4 : 8) & ~3) - pc_curr + imm
== pc_curr - pc_curr_low_bits - pc_curr + 4-or-8 + imm
== imm + 4-or-8 - low_bits_of_pc
That's then more obviously not dependent on the absolute value
of the PC.
-- PMM
On 10/4/22 08:58, Peter Maydell wrote: > On Fri, 30 Sept 2022 at 23:10, Richard Henderson > <richard.henderson@linaro.org> wrote: >> >> In preparation for TARGET_TB_PCREL, reduce reliance on absolute values. >> >> Signed-off-by: Richard Henderson <richard.henderson@linaro.org> >> --- >> target/arm/translate.c | 37 +++++++++++++++++++++---------------- >> 1 file changed, 21 insertions(+), 16 deletions(-) > >> @@ -8368,7 +8372,8 @@ static bool trans_BLX_i(DisasContext *s, arg_BLX_i *a) >> } >> tcg_gen_movi_i32(cpu_R[14], s->base.pc_next | s->thumb); >> store_cpu_field_constant(!s->thumb, thumb); >> - gen_jmp(s, (read_pc(s) & ~3) + a->imm); >> + /* This difference computes a page offset so ok for TARGET_TB_PCREL. */ >> + gen_jmp(s, (read_pc(s) & ~3) - s->pc_curr + a->imm); > > Could we just calculate the offset of the jump target instead? > read_pc() returns s->pc_curr + a constant, so the s->pc_curr cancels > out anyway: > > (read_pc(s) & ~3) - s->pc_curr + a->imm > == > (pc_curr + (s->thumb ? 4 : 8) & ~3) - pc_curr + imm > == pc_curr - pc_curr_low_bits - pc_curr + 4-or-8 + imm > == imm + 4-or-8 - low_bits_of_pc > > That's then more obviously not dependent on the absolute value > of the PC. Yes, this works: - gen_jmp(s, (read_pc(s) & ~3) + a->imm); + /* This jump is computed from an aligned PC: subtract off the low bits. */ + gen_jmp(s, jmp_diff(s, a->imm - (s->pc_curr & 3))); r~
On Tue, 4 Oct 2022 at 21:57, Richard Henderson <richard.henderson@linaro.org> wrote: > > On 10/4/22 08:58, Peter Maydell wrote: > > On Fri, 30 Sept 2022 at 23:10, Richard Henderson > > <richard.henderson@linaro.org> wrote: > >> > >> In preparation for TARGET_TB_PCREL, reduce reliance on absolute values. > >> > >> Signed-off-by: Richard Henderson <richard.henderson@linaro.org> > >> --- > >> target/arm/translate.c | 37 +++++++++++++++++++++---------------- > >> 1 file changed, 21 insertions(+), 16 deletions(-) > > > >> @@ -8368,7 +8372,8 @@ static bool trans_BLX_i(DisasContext *s, arg_BLX_i *a) > >> } > >> tcg_gen_movi_i32(cpu_R[14], s->base.pc_next | s->thumb); > >> store_cpu_field_constant(!s->thumb, thumb); > >> - gen_jmp(s, (read_pc(s) & ~3) + a->imm); > >> + /* This difference computes a page offset so ok for TARGET_TB_PCREL. */ > >> + gen_jmp(s, (read_pc(s) & ~3) - s->pc_curr + a->imm); > > > > Could we just calculate the offset of the jump target instead? > > read_pc() returns s->pc_curr + a constant, so the s->pc_curr cancels > > out anyway: > > > > (read_pc(s) & ~3) - s->pc_curr + a->imm > > == > > (pc_curr + (s->thumb ? 4 : 8) & ~3) - pc_curr + imm > > == pc_curr - pc_curr_low_bits - pc_curr + 4-or-8 + imm > > == imm + 4-or-8 - low_bits_of_pc > > > > That's then more obviously not dependent on the absolute value > > of the PC. > > Yes, this works: > > - gen_jmp(s, (read_pc(s) & ~3) + a->imm); > > + /* This jump is computed from an aligned PC: subtract off the low bits. */ > > + gen_jmp(s, jmp_diff(s, a->imm - (s->pc_curr & 3))); Cool, that looks a lot clearer. With that change, Reviewed-by: Peter Maydell <peter.maydell@linaro.org> thanks -- PMM
© 2016 - 2026 Red Hat, Inc.