5544c1b
From 5767c23140f2c92b899d9caeaa8e08711cb63868 Mon Sep 17 00:00:00 2001
5544c1b
From: Richard Henderson <rth@twiddle.net>
5544c1b
Date: Sun, 25 Mar 2012 21:21:46 +0200
5544c1b
Subject: [PATCH] tcg-sparc: Clean up cruft stemming from attempts to use
5544c1b
 global registers.
5544c1b
5544c1b
Don't use -ffixed-gN.  Don't link statically.  Don't save/restore
5544c1b
AREG0 around calls.  Don't allocate space on the stack for AREG0 save.
5544c1b
5544c1b
Signed-off-by: Richard Henderson <rth@twiddle.net>
5544c1b
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
5544c1b
---
5544c1b
 configure              | 12 -----------
5544c1b
 tcg/sparc/tcg-target.c | 55 +++++++++++++++++---------------------------------
5544c1b
 tcg/sparc/tcg-target.h | 18 +++++++----------
5544c1b
 3 files changed, 26 insertions(+), 59 deletions(-)
5544c1b
5544c1b
diff --git a/configure b/configure
5544c1b
index 9139b5c..8ffddf4 100755
5544c1b
--- a/configure
5544c1b
+++ b/configure
5544c1b
@@ -868,19 +868,11 @@ case "$cpu" in
5544c1b
     sparc)
5544c1b
            LDFLAGS="-m32 $LDFLAGS"
5544c1b
            QEMU_CFLAGS="-m32 -mcpu=ultrasparc $QEMU_CFLAGS"
5544c1b
-           QEMU_CFLAGS="-ffixed-g2 -ffixed-g3 $QEMU_CFLAGS"
5544c1b
-           if test "$solaris" = "no" ; then
5544c1b
-             QEMU_CFLAGS="-ffixed-g1 -ffixed-g6 $QEMU_CFLAGS"
5544c1b
-           fi
5544c1b
            host_guest_base="yes"
5544c1b
            ;;
5544c1b
     sparc64)
5544c1b
            LDFLAGS="-m64 $LDFLAGS"
5544c1b
            QEMU_CFLAGS="-m64 -mcpu=ultrasparc $QEMU_CFLAGS"
5544c1b
-           QEMU_CFLAGS="-ffixed-g5 -ffixed-g6 -ffixed-g7 $QEMU_CFLAGS"
5544c1b
-           if test "$solaris" != "no" ; then
5544c1b
-             QEMU_CFLAGS="-ffixed-g1 $QEMU_CFLAGS"
5544c1b
-           fi
5544c1b
            host_guest_base="yes"
5544c1b
            ;;
5544c1b
     s390)
5544c1b
@@ -4055,10 +4047,6 @@ fi
5544c1b
 
5544c1b
 if test "$target_linux_user" = "yes" -o "$target_bsd_user" = "yes" ; then
5544c1b
   case "$ARCH" in
5544c1b
-  sparc)
5544c1b
-    # -static is used to avoid g1/g3 usage by the dynamic linker
5544c1b
-    ldflags="$linker_script -static $ldflags"
5544c1b
-    ;;
5544c1b
   alpha | s390x)
5544c1b
     # The default placement of the application is fine.
5544c1b
     ;;
5544c1b
diff --git a/tcg/sparc/tcg-target.c b/tcg/sparc/tcg-target.c
5544c1b
index 9ab5746..e625aa3 100644
5544c1b
--- a/tcg/sparc/tcg-target.c
5544c1b
+++ b/tcg/sparc/tcg-target.c
5544c1b
@@ -161,7 +161,6 @@ static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str)
5544c1b
         tcg_regset_reset_reg(ct->u.regs, TCG_REG_O0);
5544c1b
         tcg_regset_reset_reg(ct->u.regs, TCG_REG_O1);
5544c1b
         tcg_regset_reset_reg(ct->u.regs, TCG_REG_O2);
5544c1b
-        tcg_regset_reset_reg(ct->u.regs, TCG_REG_O3);
5544c1b
         break;
5544c1b
     case 'I':
5544c1b
         ct->ct |= TCG_CT_CONST_S11;
5544c1b
@@ -681,11 +680,22 @@ static void tcg_out_setcond2_i32(TCGContext *s, TCGCond cond, TCGArg ret,
5544c1b
 /* Generate global QEMU prologue and epilogue code */
5544c1b
 static void tcg_target_qemu_prologue(TCGContext *s)
5544c1b
 {
5544c1b
-    tcg_set_frame(s, TCG_REG_I6, TCG_TARGET_CALL_STACK_OFFSET,
5544c1b
-                  CPU_TEMP_BUF_NLONGS * (int)sizeof(long));
5544c1b
+    int tmp_buf_size, frame_size;
5544c1b
+
5544c1b
+    /* The TCG temp buffer is at the top of the frame, immediately
5544c1b
+       below the frame pointer.  */
5544c1b
+    tmp_buf_size = CPU_TEMP_BUF_NLONGS * (int)sizeof(long);
5544c1b
+    tcg_set_frame(s, TCG_REG_I6, TCG_TARGET_STACK_BIAS - tmp_buf_size,
5544c1b
+                  tmp_buf_size);
5544c1b
+
5544c1b
+    /* TCG_TARGET_CALL_STACK_OFFSET includes the stack bias, but is
5544c1b
+       otherwise the minimal frame usable by callees.  */
5544c1b
+    frame_size = TCG_TARGET_CALL_STACK_OFFSET - TCG_TARGET_STACK_BIAS;
5544c1b
+    frame_size += TCG_STATIC_CALL_ARGS_SIZE + tmp_buf_size;
5544c1b
+    frame_size += TCG_TARGET_STACK_ALIGN - 1;
5544c1b
+    frame_size &= -TCG_TARGET_STACK_ALIGN;
5544c1b
     tcg_out32(s, SAVE | INSN_RD(TCG_REG_O6) | INSN_RS1(TCG_REG_O6) |
5544c1b
-              INSN_IMM13(-(TCG_TARGET_STACK_MINFRAME +
5544c1b
-                           CPU_TEMP_BUF_NLONGS * (int)sizeof(long))));
5544c1b
+              INSN_IMM13(-frame_size));
5544c1b
 
5544c1b
 #ifdef CONFIG_USE_GUEST_BASE
5544c1b
     if (GUEST_BASE != 0) {
5544c1b
@@ -698,6 +708,8 @@ static void tcg_target_qemu_prologue(TCGContext *s)
5544c1b
               INSN_RS2(TCG_REG_G0));
5544c1b
     /* delay slot */
5544c1b
     tcg_out_nop(s);
5544c1b
+
5544c1b
+    /* No epilogue required.  We issue ret + restore directly in the TB.  */
5544c1b
 }
5544c1b
 
5544c1b
 #if defined(CONFIG_SOFTMMU)
5544c1b
@@ -880,12 +892,6 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args, int sizeop)
5544c1b
     tcg_out_mov(s, TCG_TYPE_REG, tcg_target_call_iarg_regs[n++],
5544c1b
                 args[addrlo_idx]);
5544c1b
 
5544c1b
-    /* Store AREG0 in stack to avoid ugly glibc bugs that mangle
5544c1b
-       global registers */
5544c1b
-    tcg_out_st(s, TCG_TYPE_REG, TCG_AREG0, TCG_REG_CALL_STACK,
5544c1b
-               TCG_TARGET_CALL_STACK_OFFSET - TCG_STATIC_CALL_ARGS_SIZE -
5544c1b
-               sizeof(long));
5544c1b
-
5544c1b
     /* qemu_ld_helper[s_bits](arg0, arg1) */
5544c1b
     tcg_out32(s, CALL | ((((tcg_target_ulong)qemu_ld_helpers[s_bits]
5544c1b
                            - (tcg_target_ulong)s->code_ptr) >> 2)
5544c1b
@@ -893,11 +899,6 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args, int sizeop)
5544c1b
     /* delay slot */
5544c1b
     tcg_out_movi(s, TCG_TYPE_I32, tcg_target_call_iarg_regs[n], memi);
5544c1b
 
5544c1b
-    /* Reload AREG0.  */
5544c1b
-    tcg_out_ld(s, TCG_TYPE_REG, TCG_AREG0, TCG_REG_CALL_STACK,
5544c1b
-               TCG_TARGET_CALL_STACK_OFFSET - TCG_STATIC_CALL_ARGS_SIZE -
5544c1b
-               sizeof(long));
5544c1b
-
5544c1b
     n = tcg_target_call_oarg_regs[0];
5544c1b
     /* datalo = sign_extend(arg0) */
5544c1b
     switch (sizeop) {
5544c1b
@@ -1011,12 +1012,6 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args, int sizeop)
5544c1b
     }
5544c1b
     tcg_out_mov(s, TCG_TYPE_REG, tcg_target_call_iarg_regs[n++], datalo);
5544c1b
 
5544c1b
-    /* Store AREG0 in stack to avoid ugly glibc bugs that mangle
5544c1b
-       global registers */
5544c1b
-    tcg_out_st(s, TCG_TYPE_REG, TCG_AREG0, TCG_REG_CALL_STACK,
5544c1b
-               TCG_TARGET_CALL_STACK_OFFSET - TCG_STATIC_CALL_ARGS_SIZE -
5544c1b
-               sizeof(long));
5544c1b
-
5544c1b
     /* qemu_st_helper[s_bits](arg0, arg1, arg2) */
5544c1b
     tcg_out32(s, CALL | ((((tcg_target_ulong)qemu_st_helpers[sizeop]
5544c1b
                            - (tcg_target_ulong)s->code_ptr) >> 2)
5544c1b
@@ -1024,11 +1019,6 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args, int sizeop)
5544c1b
     /* delay slot */
5544c1b
     tcg_out_movi(s, TCG_TYPE_REG, tcg_target_call_iarg_regs[n], memi);
5544c1b
 
5544c1b
-    /* Reload AREG0.  */
5544c1b
-    tcg_out_ld(s, TCG_TYPE_REG, TCG_AREG0, TCG_REG_CALL_STACK,
5544c1b
-               TCG_TARGET_CALL_STACK_OFFSET - TCG_STATIC_CALL_ARGS_SIZE -
5544c1b
-               sizeof(long));
5544c1b
-
5544c1b
     *label_ptr |= INSN_OFF19((unsigned long)s->code_ptr -
5544c1b
                              (unsigned long)label_ptr);
5544c1b
 #else
5544c1b
@@ -1091,15 +1081,8 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
5544c1b
             tcg_out32(s, JMPL | INSN_RD(TCG_REG_O7) | INSN_RS1(TCG_REG_I5) |
5544c1b
                       INSN_RS2(TCG_REG_G0));
5544c1b
         }
5544c1b
-        /* Store AREG0 in stack to avoid ugly glibc bugs that mangle
5544c1b
-           global registers */
5544c1b
-        // delay slot
5544c1b
-        tcg_out_st(s, TCG_TYPE_REG, TCG_AREG0, TCG_REG_CALL_STACK,
5544c1b
-                   TCG_TARGET_CALL_STACK_OFFSET - TCG_STATIC_CALL_ARGS_SIZE -
5544c1b
-                   sizeof(long));
5544c1b
-        tcg_out_ld(s, TCG_TYPE_REG, TCG_AREG0, TCG_REG_CALL_STACK,
5544c1b
-                   TCG_TARGET_CALL_STACK_OFFSET - TCG_STATIC_CALL_ARGS_SIZE -
5544c1b
-                   sizeof(long));
5544c1b
+        /* delay slot */
5544c1b
+        tcg_out_nop(s);
5544c1b
         break;
5544c1b
     case INDEX_op_jmp:
5544c1b
     case INDEX_op_br:
5544c1b
diff --git a/tcg/sparc/tcg-target.h b/tcg/sparc/tcg-target.h
5544c1b
index ee154d0..6314ffb 100644
5544c1b
--- a/tcg/sparc/tcg-target.h
5544c1b
+++ b/tcg/sparc/tcg-target.h
5544c1b
@@ -66,20 +66,16 @@ typedef enum {
5544c1b
 #define TCG_CT_CONST_S13 0x200
5544c1b
 
5544c1b
 /* used for function call generation */
5544c1b
-#define TCG_REG_CALL_STACK TCG_REG_I6
5544c1b
+#define TCG_REG_CALL_STACK TCG_REG_O6
5544c1b
 
5544c1b
 #if TCG_TARGET_REG_BITS == 64
5544c1b
-// Reserve space for AREG0
5544c1b
-#define TCG_TARGET_STACK_MINFRAME (176 + 4 * (int)sizeof(long) + \
5544c1b
-                                   TCG_STATIC_CALL_ARGS_SIZE)
5544c1b
-#define TCG_TARGET_CALL_STACK_OFFSET (2047 - 16)
5544c1b
-#define TCG_TARGET_STACK_ALIGN 16
5544c1b
+#define TCG_TARGET_STACK_BIAS           2047
5544c1b
+#define TCG_TARGET_STACK_ALIGN          16
5544c1b
+#define TCG_TARGET_CALL_STACK_OFFSET    (128 + 6*8 + TCG_TARGET_STACK_BIAS)
5544c1b
 #else
5544c1b
-// AREG0 + one word for alignment
5544c1b
-#define TCG_TARGET_STACK_MINFRAME (92 + (2 + 1) * (int)sizeof(long) + \
5544c1b
-                                   TCG_STATIC_CALL_ARGS_SIZE)
5544c1b
-#define TCG_TARGET_CALL_STACK_OFFSET TCG_TARGET_STACK_MINFRAME
5544c1b
-#define TCG_TARGET_STACK_ALIGN 8
5544c1b
+#define TCG_TARGET_STACK_BIAS           0
5544c1b
+#define TCG_TARGET_STACK_ALIGN          8
5544c1b
+#define TCG_TARGET_CALL_STACK_OFFSET    (64 + 4 + 6*4)
5544c1b
 #endif
5544c1b
 
5544c1b
 #if TCG_TARGET_REG_BITS == 64