Patchwork [14/22] tcg-i386: Tidy calls.

login
register
mail settings
Submitter Richard Henderson
Date April 14, 2010, 6:02 p.m.
Message ID <2a9043d9d022d3e8cfcd819f82a6ee29f3c75260.1272479073.git.rth@twiddle.net>
Download mbox | patch
Permalink /patch/51206/
State New
Headers show

Comments

Richard Henderson - April 14, 2010, 6:02 p.m.
Define OPC_CALL_Jz, generated by tcg_out_calli; use the later
throughout.  Unify the calls within qemu_st; adjust the stack
with a single pop if applicable.

Define and use EXT_CALLN_Ev for indirect calls.

Signed-off-by: Richard Henderson <rth@twiddle.net>
---
 tcg/i386/tcg-target.c |   49 +++++++++++++++++++++++++++----------------------
 1 files changed, 27 insertions(+), 22 deletions(-)
Aurelien Jarno - May 21, 2010, 9:40 a.m.
On Wed, Apr 14, 2010 at 11:02:32AM -0700, Richard Henderson wrote:
> Define OPC_CALL_Jz, generated by tcg_out_calli; use the later
> throughout.  Unify the calls within qemu_st; adjust the stack
> with a single pop if applicable.
> 
> Define and use EXT_CALLN_Ev for indirect calls.
> 
> Signed-off-by: Richard Henderson <rth@twiddle.net>

Acked-by: Aurelien Jarno <aurelien@aurel32.net>

> ---
>  tcg/i386/tcg-target.c |   49 +++++++++++++++++++++++++++----------------------
>  1 files changed, 27 insertions(+), 22 deletions(-)
> 
> diff --git a/tcg/i386/tcg-target.c b/tcg/i386/tcg-target.c
> index 58fcd23..e82788d 100644
> --- a/tcg/i386/tcg-target.c
> +++ b/tcg/i386/tcg-target.c
> @@ -167,6 +167,7 @@ static inline int tcg_target_const_match(tcg_target_long val,
>  #define OPC_ARITH_EvIb	(0x83)
>  #define OPC_ARITH_GvEv	(0x03)		/* ... plus (ARITH_FOO << 3) */
>  #define OPC_ADD_GvEv	(OPC_ARITH_GvEv | (ARITH_ADD << 3))
> +#define OPC_CALL_Jz	(0xe8)
>  #define OPC_CMP_GvEv	(OPC_ARITH_GvEv | (ARITH_CMP << 3))
>  #define OPC_DEC_r32	(0x48)
>  #define OPC_BSWAP	(0xc8 | P_EXT)
> @@ -210,6 +211,7 @@ static inline int tcg_target_const_match(tcg_target_long val,
>  #define SHIFT_SAR 7
>  
>  /* Group 5 opcode extensions for 0xff.  */
> +#define EXT_CALLN_Ev	2
>  #define EXT_JMPN_Ev	4
>  
>  /* Condition codes to be added to OPC_JCC_{long,short}.  */
> @@ -644,6 +646,12 @@ static void tcg_out_setcond2(TCGContext *s, const TCGArg *args,
>      }
>  }
>  
> +static void tcg_out_calli(TCGContext *s, tcg_target_long dest)
> +{
> +    tcg_out_opc(s, OPC_CALL_Jz);
> +    tcg_out32(s, dest - (tcg_target_long)s->code_ptr - 4);
> +}
> +
>  #if defined(CONFIG_SOFTMMU)
>  
>  #include "../../softmmu_defs.h"
> @@ -748,9 +756,7 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
>      tcg_out_mov(s, TCG_REG_EDX, addr_reg2);
>      tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_ECX, mem_index);
>  #endif
> -    tcg_out8(s, 0xe8);
> -    tcg_out32(s, (tcg_target_long)qemu_ld_helpers[s_bits] - 
> -              (tcg_target_long)s->code_ptr - 4);
> +    tcg_out_calli(s, (tcg_target_long)qemu_ld_helpers[s_bits]);
>  
>      switch(opc) {
>      case 0 | 4:
> @@ -865,6 +871,7 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
>  {
>      int addr_reg, data_reg, data_reg2, r0, r1, mem_index, s_bits, bswap;
>  #if defined(CONFIG_SOFTMMU)
> +    int stack_adjust;
>      uint8_t *label1_ptr, *label2_ptr;
>  #endif
>  #if TARGET_LONG_BITS == 64
> @@ -938,10 +945,7 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
>          tcg_out_mov(s, TCG_REG_EDX, data_reg);
>          tcg_out_mov(s, TCG_REG_ECX, data_reg2);
>          tcg_out_pushi(s, mem_index);
> -        tcg_out8(s, 0xe8);
> -        tcg_out32(s, (tcg_target_long)qemu_st_helpers[s_bits] - 
> -                  (tcg_target_long)s->code_ptr - 4);
> -        tcg_out_addi(s, TCG_REG_ESP, 4);
> +        stack_adjust = 4;
>      } else {
>          switch(opc) {
>          case 0:
> @@ -955,9 +959,7 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
>              break;
>          }
>          tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_ECX, mem_index);
> -        tcg_out8(s, 0xe8);
> -        tcg_out32(s, (tcg_target_long)qemu_st_helpers[s_bits] - 
> -                  (tcg_target_long)s->code_ptr - 4);
> +        stack_adjust = 0;
>      }
>  #else
>      if (opc == 3) {
> @@ -965,10 +967,7 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
>          tcg_out_pushi(s, mem_index);
>          tcg_out_push(s, data_reg2);
>          tcg_out_push(s, data_reg);
> -        tcg_out8(s, 0xe8);
> -        tcg_out32(s, (tcg_target_long)qemu_st_helpers[s_bits] - 
> -                  (tcg_target_long)s->code_ptr - 4);
> -        tcg_out_addi(s, TCG_REG_ESP, 12);
> +        stack_adjust = 12;
>      } else {
>          tcg_out_mov(s, TCG_REG_EDX, addr_reg2);
>          switch(opc) {
> @@ -983,13 +982,19 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
>              break;
>          }
>          tcg_out_pushi(s, mem_index);
> -        tcg_out8(s, 0xe8);
> -        tcg_out32(s, (tcg_target_long)qemu_st_helpers[s_bits] - 
> -                  (tcg_target_long)s->code_ptr - 4);
> -        tcg_out_addi(s, TCG_REG_ESP, 4);
> +        stack_adjust = 4;
>      }
>  #endif
> -    
> +
> +    tcg_out_calli(s, (tcg_target_long)qemu_st_helpers[s_bits]);
> +
> +    if (stack_adjust == 4) {
> +        /* Pop and discard.  This is 2 bytes smaller than the add.  */
> +        tcg_out_pop(s, TCG_REG_ECX);
> +    } else if (stack_adjust != 0) {
> +        tcg_out_addi(s, TCG_REG_ESP, stack_adjust);
> +    }
> +
>      /* jmp label2 */
>      tcg_out8(s, OPC_JMP_short);
>      label2_ptr = s->code_ptr;
> @@ -1082,10 +1087,10 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
>          break;
>      case INDEX_op_call:
>          if (const_args[0]) {
> -            tcg_out8(s, 0xe8);
> -            tcg_out32(s, args[0] - (tcg_target_long)s->code_ptr - 4);
> +            tcg_out_calli(s, args[0]);
>          } else {
> -            tcg_out_modrm(s, 0xff, 2, args[0]);
> +            /* call *reg */
> +            tcg_out_modrm(s, 0xff, EXT_CALLN_Ev, args[0]);
>          }
>          break;
>      case INDEX_op_jmp:
> -- 
> 1.6.6.1
> 
> 
> 
>

Patch

diff --git a/tcg/i386/tcg-target.c b/tcg/i386/tcg-target.c
index 58fcd23..e82788d 100644
--- a/tcg/i386/tcg-target.c
+++ b/tcg/i386/tcg-target.c
@@ -167,6 +167,7 @@  static inline int tcg_target_const_match(tcg_target_long val,
 #define OPC_ARITH_EvIb	(0x83)
 #define OPC_ARITH_GvEv	(0x03)		/* ... plus (ARITH_FOO << 3) */
 #define OPC_ADD_GvEv	(OPC_ARITH_GvEv | (ARITH_ADD << 3))
+#define OPC_CALL_Jz	(0xe8)
 #define OPC_CMP_GvEv	(OPC_ARITH_GvEv | (ARITH_CMP << 3))
 #define OPC_DEC_r32	(0x48)
 #define OPC_BSWAP	(0xc8 | P_EXT)
@@ -210,6 +211,7 @@  static inline int tcg_target_const_match(tcg_target_long val,
 #define SHIFT_SAR 7
 
 /* Group 5 opcode extensions for 0xff.  */
+#define EXT_CALLN_Ev	2
 #define EXT_JMPN_Ev	4
 
 /* Condition codes to be added to OPC_JCC_{long,short}.  */
@@ -644,6 +646,12 @@  static void tcg_out_setcond2(TCGContext *s, const TCGArg *args,
     }
 }
 
+static void tcg_out_calli(TCGContext *s, tcg_target_long dest)
+{
+    tcg_out_opc(s, OPC_CALL_Jz);
+    tcg_out32(s, dest - (tcg_target_long)s->code_ptr - 4);
+}
+
 #if defined(CONFIG_SOFTMMU)
 
 #include "../../softmmu_defs.h"
@@ -748,9 +756,7 @@  static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
     tcg_out_mov(s, TCG_REG_EDX, addr_reg2);
     tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_ECX, mem_index);
 #endif
-    tcg_out8(s, 0xe8);
-    tcg_out32(s, (tcg_target_long)qemu_ld_helpers[s_bits] - 
-              (tcg_target_long)s->code_ptr - 4);
+    tcg_out_calli(s, (tcg_target_long)qemu_ld_helpers[s_bits]);
 
     switch(opc) {
     case 0 | 4:
@@ -865,6 +871,7 @@  static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
 {
     int addr_reg, data_reg, data_reg2, r0, r1, mem_index, s_bits, bswap;
 #if defined(CONFIG_SOFTMMU)
+    int stack_adjust;
     uint8_t *label1_ptr, *label2_ptr;
 #endif
 #if TARGET_LONG_BITS == 64
@@ -938,10 +945,7 @@  static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
         tcg_out_mov(s, TCG_REG_EDX, data_reg);
         tcg_out_mov(s, TCG_REG_ECX, data_reg2);
         tcg_out_pushi(s, mem_index);
-        tcg_out8(s, 0xe8);
-        tcg_out32(s, (tcg_target_long)qemu_st_helpers[s_bits] - 
-                  (tcg_target_long)s->code_ptr - 4);
-        tcg_out_addi(s, TCG_REG_ESP, 4);
+        stack_adjust = 4;
     } else {
         switch(opc) {
         case 0:
@@ -955,9 +959,7 @@  static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
             break;
         }
         tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_ECX, mem_index);
-        tcg_out8(s, 0xe8);
-        tcg_out32(s, (tcg_target_long)qemu_st_helpers[s_bits] - 
-                  (tcg_target_long)s->code_ptr - 4);
+        stack_adjust = 0;
     }
 #else
     if (opc == 3) {
@@ -965,10 +967,7 @@  static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
         tcg_out_pushi(s, mem_index);
         tcg_out_push(s, data_reg2);
         tcg_out_push(s, data_reg);
-        tcg_out8(s, 0xe8);
-        tcg_out32(s, (tcg_target_long)qemu_st_helpers[s_bits] - 
-                  (tcg_target_long)s->code_ptr - 4);
-        tcg_out_addi(s, TCG_REG_ESP, 12);
+        stack_adjust = 12;
     } else {
         tcg_out_mov(s, TCG_REG_EDX, addr_reg2);
         switch(opc) {
@@ -983,13 +982,19 @@  static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
             break;
         }
         tcg_out_pushi(s, mem_index);
-        tcg_out8(s, 0xe8);
-        tcg_out32(s, (tcg_target_long)qemu_st_helpers[s_bits] - 
-                  (tcg_target_long)s->code_ptr - 4);
-        tcg_out_addi(s, TCG_REG_ESP, 4);
+        stack_adjust = 4;
     }
 #endif
-    
+
+    tcg_out_calli(s, (tcg_target_long)qemu_st_helpers[s_bits]);
+
+    if (stack_adjust == 4) {
+        /* Pop and discard.  This is 2 bytes smaller than the add.  */
+        tcg_out_pop(s, TCG_REG_ECX);
+    } else if (stack_adjust != 0) {
+        tcg_out_addi(s, TCG_REG_ESP, stack_adjust);
+    }
+
     /* jmp label2 */
     tcg_out8(s, OPC_JMP_short);
     label2_ptr = s->code_ptr;
@@ -1082,10 +1087,10 @@  static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
         break;
     case INDEX_op_call:
         if (const_args[0]) {
-            tcg_out8(s, 0xe8);
-            tcg_out32(s, args[0] - (tcg_target_long)s->code_ptr - 4);
+            tcg_out_calli(s, args[0]);
         } else {
-            tcg_out_modrm(s, 0xff, 2, args[0]);
+            /* call *reg */
+            tcg_out_modrm(s, 0xff, EXT_CALLN_Ev, args[0]);
         }
         break;
     case INDEX_op_jmp: