Skip to contentSkip to conversation

tcp/mips: Change TCG_AREG0 (fp -> s0)

PATCH In review Stefan Weil 4 participants 8 messages Apr 8, 2010 – Apr 9, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tcp/mips: Change TCG_AREG0 (fp -> s0)

Register fp is a bad choice for compilations without optimisation, because the compiler makes heavy use of this register (so the resulting code crashes).

Register s0 was used for TCG_AREG1 in earlier releases, but was no longer used and is now free for TCG_AREG0.

The resulting code works for compilations without optimisation (tested with qemu mips in qemu mips on x86 host).

Cc Aurelien Jarno <[email protected]>
---
 dyngen-exec.h         |    2 +-
 tcg/mips/tcg-target.h |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
dyngen-exec.h +1 -1
index b9f6969..85a2616 100644
@@ -61,7 +61,7 @@ extern int printf(const char *, ...);
 #elif defined(__hppa__)
 #define AREG0 "r17"
 #elif defined(__mips__)
-#define AREG0 "fp"
+#define AREG0 "s0"
 #elif defined(__sparc__)
 #ifdef CONFIG_SOLARIS
 #define AREG0 "g2"
tcg/mips/tcg-target.h +1 -1
index 0292d33..0028bfa 100644
@@ -97,7 +97,7 @@ enum {
 #undef TCG_TARGET_HAS_ext16u_i32   /* andi rt, rs, 0xffff */
 
 /* Note: must be synced with dyngen-exec.h */
-#define TCG_AREG0 TCG_REG_FP
+#define TCG_AREG0 TCG_REG_S0
 
 /* guest base is supported */
 #define TCG_TARGET_HAS_GUEST_BASE
Signature · 1 line
1.5.6.5
#1 lore raw
Quoted 3 lines · Stefan Weil

Register fp is a bad choice for compilations without optimisation, because the compiler makes heavy use of this register (so the resulting code crashes).

I don't fully understand why the compiler makes use of this register in code where env is declared as register fp.

Quoted 6 lines

Register s0 was used for TCG_AREG1 in earlier releases, but was no longer used and is now free for TCG_AREG0.

The resulting code works for compilations without optimisation (tested with qemu mips in qemu mips on x86 host).

The patch is not complete, at least some changes are missing to tcg_target_callee_save_regs.

Quoted 37 lines
Cc Aurelien Jarno <[email protected]>
---
 dyngen-exec.h         |    2 +-
 tcg/mips/tcg-target.h |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
dyngen-exec.h +1 -1
index b9f6969..85a2616 100644
@@ -61,7 +61,7 @@ extern int printf(const char *, ...);
 #elif defined(__hppa__)
 #define AREG0 "r17"
 #elif defined(__mips__)
-#define AREG0 "fp"
+#define AREG0 "s0"
 #elif defined(__sparc__)
 #ifdef CONFIG_SOLARIS
 #define AREG0 "g2"
tcg/mips/tcg-target.h +1 -1
index 0292d33..0028bfa 100644
@@ -97,7 +97,7 @@ enum {
 #undef TCG_TARGET_HAS_ext16u_i32   /* andi rt, rs, 0xffff */
 
 /* Note: must be synced with dyngen-exec.h */
-#define TCG_AREG0 TCG_REG_FP
+#define TCG_AREG0 TCG_REG_S0
 
 /* guest base is supported */
 #define TCG_TARGET_HAS_GUEST_BASE
Signature · 1 line
1.5.6.5
Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#2 lore raw
Quoted 7 lines · Stefan Weil
Quoted 3 lines

Register fp is a bad choice for compilations without optimisation, because the compiler makes heavy use of this register (so the resulting code crashes).

I don't fully understand why the compiler makes use of this register in code where env is declared as register fp.

The frame pointer (and certain other registers, typically stack pointer, link register, PIC base, TLS base, etc.) are special. They have predefined uses, usually specified by the relevant platform ABI.

Defining a global register variable (or using -ffixed-reg) only excludes these registers from general register allocation. It does not prevent them being used for specific purposes. The user is responsible for ensuring that any assumptions/requirements made by the ABI are still met.

In some cases gcc can diagnose invalid uses. In others the overlap may be legitimate, but not necessarily what we intended in qemu.

Paul

#3 lore raw
In reply to Aurelien Jarno
Quoted 10 lines · Aurelien Jarno
Quoted 4 lines · Stefan Weil

Register fp is a bad choice for compilations without optimisation, because the compiler makes heavy use of this register (so the resulting code crashes).

I don't fully understand why the compiler makes use of this register in code where env is declared as register fp.

fp = frame pointer is special. See Paul's answer.

Quoted 4 lines
Quoted 4 lines

Register s0 was used for TCG_AREG1 in earlier releases, but was no longer used and is now free for TCG_AREG0.

Quoted 8 lines
Quoted 4 lines

The resulting code works for compilations without optimisation (tested with qemu mips in qemu mips on x86 host).

The patch is not complete, at least some changes are missing to tcg_target_callee_save_regs.

Thanks. I'll send an updated patch.

Regards, Stefan

#4 lore raw

PATCH 1/1 tcp/mips: Change TCG_AREG0 (fp -> s0)

Register fp (frame pointer) is a bad choice for compilations without optimisation, because the compiler makes heavy use of this register (so the resulting code crashes).

Register s0 was used for TCG_AREG1 in earlier releases, but was no longer used and is now free for TCG_AREG0.

The resulting code works for compilations without optimisation (tested with qemu mips in qemu mips on x86 host).

v2:

* Remove s0 from tcg_target_callee_save_regs and add fp there. Hint from Aurelien Jarno, thanks.

* Add fp to list of reserved registers.

Cc Aurelien Jarno <[email protected]>
---
 dyngen-exec.h         |    2 +-
 tcg/mips/tcg-target.c |    6 ++++--
 tcg/mips/tcg-target.h |    2 +-
 3 files changed, 6 insertions(+), 4 deletions(-)
dyngen-exec.h +1 -1
index b9f6969..85a2616 100644
@@ -61,7 +61,7 @@ extern int printf(const char *, ...);
 #elif defined(__hppa__)
 #define AREG0 "r17"
 #elif defined(__mips__)
-#define AREG0 "fp"
+#define AREG0 "s0"
 #elif defined(__sparc__)
 #ifdef CONFIG_SOLARIS
 #define AREG0 "g2"
tcg/mips/tcg-target.c +4 -2
index f4fb615..c9094ee 100644
@@ -1450,7 +1450,9 @@ static const TCGTargetOpDef mips_op_defs[] = {
 };
 
 static int tcg_target_callee_save_regs[] = {
+#if 0 /* used for the global env (TCG_AREG0), so no need to save */
     TCG_REG_S0,
+#endif
     TCG_REG_S1,
     TCG_REG_S2,
     TCG_REG_S3,
@@ -1459,8 +1461,7 @@ static int tcg_target_callee_save_regs[] = {
     TCG_REG_S6,
     TCG_REG_S7,
     TCG_REG_GP,
-    /* TCG_REG_FP, */ /* currently used for the global env, so np
-                         need to save */
+    TCG_REG_FP,
     TCG_REG_RA,       /* should be last for ABI compliance */
 };
 
@@ -1524,6 +1525,7 @@ void tcg_target_init(TCGContext *s)
     tcg_regset_set_reg(s->reserved_regs, TCG_REG_AT);   /* internal use */
     tcg_regset_set_reg(s->reserved_regs, TCG_REG_T0);   /* internal use */
     tcg_regset_set_reg(s->reserved_regs, TCG_REG_RA);   /* return address */
+    tcg_regset_set_reg(s->reserved_regs, TCG_REG_FP);   /* frame pointer */
     tcg_regset_set_reg(s->reserved_regs, TCG_REG_SP);   /* stack pointer */
 
     tcg_add_target_add_op_defs(mips_op_defs);
tcg/mips/tcg-target.h +1 -1
index 0292d33..0028bfa 100644
@@ -97,7 +97,7 @@ enum {
 #undef TCG_TARGET_HAS_ext16u_i32   /* andi rt, rs, 0xffff */
 
 /* Note: must be synced with dyngen-exec.h */
-#define TCG_AREG0 TCG_REG_FP
+#define TCG_AREG0 TCG_REG_S0
 
 /* guest base is supported */
 #define TCG_TARGET_HAS_GUEST_BASE
Signature · 1 line
1.7.0
#5 lore raw
Quoted 1 line · Stefan Weil

* Add fp to list of reserved registers.

This is unnecessary. The register is not a frame pointer within tcg generated code. It's available for use as a call-saved register.

r~

#6 lore raw

PATCH 1/1 tcp/mips: Change TCG_AREG0 (fp -> s0)

Register fp (frame pointer) is a bad choice for compilations without optimisation, because the compiler makes heavy use of this register (so the resulting code crashes).

Register s0 had been used for TCG_AREG1 in earlier releases, but was no longer used and is now free for TCG_AREG0.

The resulting code works for compilations without optimisation (tested with qemu mips in qemu mips on x86 host).

v2:

* Remove s0 from tcg_target_callee_save_regs and add fp there. Hint from Aurelien Jarno, thanks.

* Add fp to list of reserved registers.

v3:

* Don't add fp to list of reserved registers
Thanks to Richard Henderson.

* Remove s0 from tcg_target_reg_alloc_order.

Cc Aurelien Jarno <[email protected]>
Cc Richard Henderson <[email protected]>
---
 dyngen-exec.h         |    2 +-
 tcg/mips/tcg-target.c |    7 +++++--
 tcg/mips/tcg-target.h |    2 +-
 3 files changed, 7 insertions(+), 4 deletions(-)
dyngen-exec.h +1 -1
index d04eda8..0700a2d 100644
@@ -59,7 +59,7 @@ extern int printf(const char *, ...);
 #elif defined(__hppa__)
 #define AREG0 "r17"
 #elif defined(__mips__)
-#define AREG0 "fp"
+#define AREG0 "s0"
 #elif defined(__sparc__)
 #ifdef CONFIG_SOLARIS
 #define AREG0 "g2"
tcg/mips/tcg-target.c +5 -2
index f4fb615..31296fc 100644
@@ -69,7 +69,9 @@ static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
 
 /* check if we really need so many registers :P */
 static const int tcg_target_reg_alloc_order[] = {
+#if 0 /* used for the global env (TCG_AREG0), so don't use it here */
     TCG_REG_S0,
+#endif
     TCG_REG_S1,
     TCG_REG_S2,
     TCG_REG_S3,
@@ -1450,7 +1452,9 @@ static const TCGTargetOpDef mips_op_defs[] = {
 };
 
 static int tcg_target_callee_save_regs[] = {
+#if 0 /* used for the global env (TCG_AREG0), so no need to save */
     TCG_REG_S0,
+#endif
     TCG_REG_S1,
     TCG_REG_S2,
     TCG_REG_S3,
@@ -1459,8 +1463,7 @@ static int tcg_target_callee_save_regs[] = {
     TCG_REG_S6,
     TCG_REG_S7,
     TCG_REG_GP,
-    /* TCG_REG_FP, */ /* currently used for the global env, so np
-                         need to save */
+    TCG_REG_FP,
     TCG_REG_RA,       /* should be last for ABI compliance */
 };
 
tcg/mips/tcg-target.h +1 -1
index 0292d33..0028bfa 100644
@@ -97,7 +97,7 @@ enum {
 #undef TCG_TARGET_HAS_ext16u_i32   /* andi rt, rs, 0xffff */
 
 /* Note: must be synced with dyngen-exec.h */
-#define TCG_AREG0 TCG_REG_FP
+#define TCG_AREG0 TCG_REG_S0
 
 /* guest base is supported */
 #define TCG_TARGET_HAS_GUEST_BASE
Signature · 1 line
1.7.0
#7 lore raw
Quoted 52 lines · Stefan Weil

Register fp (frame pointer) is a bad choice for compilations without optimisation, because the compiler makes heavy use of this register (so the resulting code crashes).

Register s0 had been used for TCG_AREG1 in earlier releases, but was no longer used and is now free for TCG_AREG0.

The resulting code works for compilations without optimisation (tested with qemu mips in qemu mips on x86 host).

v2:

* Remove s0 from tcg_target_callee_save_regs and add fp there. Hint from Aurelien Jarno, thanks.

* Add fp to list of reserved registers.

v3:

* Don't add fp to list of reserved registers
Thanks to Richard Henderson.

* Remove s0 from tcg_target_reg_alloc_order.

Cc Aurelien Jarno <[email protected]>
Cc Richard Henderson <[email protected]>
---
 dyngen-exec.h         |    2 +-
 tcg/mips/tcg-target.c |    7 +++++--
 tcg/mips/tcg-target.h |    2 +-
 3 files changed, 7 insertions(+), 4 deletions(-)
dyngen-exec.h +1 -1
index d04eda8..0700a2d 100644
@@ -59,7 +59,7 @@ extern int printf(const char *, ...);
 #elif defined(__hppa__)
 #define AREG0 "r17"
 #elif defined(__mips__)
-#define AREG0 "fp"
+#define AREG0 "s0"
 #elif defined(__sparc__)
 #ifdef CONFIG_SOLARIS
 #define AREG0 "g2"
tcg/mips/tcg-target.c +0 -0
index f4fb615..31296fc 100644

tcg/mips/tcg-target.c static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = { · line 74

Quoted 6 lines
tcg/mips/tcg-target.c +2 -0
@@ -69,7 +69,9 @@ static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
 
 /* check if we really need so many registers :P */
 static const int tcg_target_reg_alloc_order[] = {
+#if 0 /* used for the global env (TCG_AREG0), so don't use it here */
     TCG_REG_S0,
+#endif

This is not something necessary. The fact this register is declared as TCG_AREG0, will make TCG skip this automatically.

Quoted 40 lines
tcg/mips/tcg-target.c +3 -2
     TCG_REG_S1,
     TCG_REG_S2,
     TCG_REG_S3,
@@ -1450,7 +1452,9 @@ static const TCGTargetOpDef mips_op_defs[] = {
 };
 
 static int tcg_target_callee_save_regs[] = {
+#if 0 /* used for the global env (TCG_AREG0), so no need to save */
     TCG_REG_S0,
+#endif
     TCG_REG_S1,
     TCG_REG_S2,
     TCG_REG_S3,
@@ -1459,8 +1463,7 @@ static int tcg_target_callee_save_regs[] = {
     TCG_REG_S6,
     TCG_REG_S7,
     TCG_REG_GP,
-    /* TCG_REG_FP, */ /* currently used for the global env, so np
-                         need to save */
+    TCG_REG_FP,
     TCG_REG_RA,       /* should be last for ABI compliance */
 };
 
tcg/mips/tcg-target.h +1 -1
index 0292d33..0028bfa 100644
@@ -97,7 +97,7 @@ enum {
 #undef TCG_TARGET_HAS_ext16u_i32   /* andi rt, rs, 0xffff */
 
 /* Note: must be synced with dyngen-exec.h */
-#define TCG_AREG0 TCG_REG_FP
+#define TCG_AREG0 TCG_REG_S0
 
 /* guest base is supported */
 #define TCG_TARGET_HAS_GUEST_BASE
Signature · 1 line
1.7.0
Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net