Author: zoltan
Date: 2005-02-27 08:44:40 -0500 (Sun, 27 Feb 2005)
New Revision: 41259

Modified:
   trunk/mono/mono/mini/ChangeLog
   trunk/mono/mono/mini/mini-amd64.c
   trunk/mono/mono/mini/mini-amd64.h
   trunk/mono/mono/mini/tramp-amd64.c
Log:
2005-02-27  Zoltan Varga  <[EMAIL PROTECTED]>

        * mini-amd64.c (mono_amd64_get_delegate_method_ptr_addr): New helper 
function.

        * mini-amd64.c (emit_call): If both the caller and the callee is
        guaranteed to have 32 bit addresses, emit a normal call.

        * tramp-amd64.c: Adapt to changes in mini-amd64.c.

        * tramp-amd64.c (amd64_magic_trampoline): Remove patching of 
trampolines. 
        * tramp-amd64.c (amd64_magic_trampoline): Add support for patching the
        method_ptr inside delegates.


Modified: trunk/mono/mono/mini/ChangeLog
===================================================================
--- trunk/mono/mono/mini/ChangeLog      2005-02-27 13:41:12 UTC (rev 41258)
+++ trunk/mono/mono/mini/ChangeLog      2005-02-27 13:44:40 UTC (rev 41259)
@@ -1,3 +1,16 @@
+2005-02-27  Zoltan Varga  <[EMAIL PROTECTED]>
+
+       * mini-amd64.c (mono_amd64_get_delegate_method_ptr_addr): New helper 
function.
+
+       * mini-amd64.c (emit_call): If both the caller and the callee is
+       guaranteed to have 32 bit addresses, emit a normal call.
+
+       * tramp-amd64.c: Adapt to changes in mini-amd64.c.
+
+       * tramp-amd64.c (amd64_magic_trampoline): Remove patching of 
trampolines. 
+       * tramp-amd64.c (amd64_magic_trampoline): Add support for patching the
+       method_ptr inside delegates.
+
 2005-02-26  Zoltan Varga  <[EMAIL PROTECTED]>
 
        * mini.c (mono_jit_free_method): Free the method info even if the 
native code is

Modified: trunk/mono/mono/mini/mini-amd64.c
===================================================================
--- trunk/mono/mono/mini/mini-amd64.c   2005-02-27 13:41:12 UTC (rev 41258)
+++ trunk/mono/mono/mini/mini-amd64.c   2005-02-27 13:44:40 UTC (rev 41259)
@@ -1245,20 +1245,50 @@
 static guint8*
 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer 
data)
 {
-       /*
-        * FIXME: Emitting a call template and patching it later is expensive 
on 
-        * amd64, so try to determine the patch target immediately, and emit 
more 
-        * efficient code if possible.
-        */
-
        mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
 
        if (mono_compile_aot) {
                amd64_call_membase (code, AMD64_RIP, 0);
        }
        else {
-               amd64_set_reg_template (code, GP_SCRATCH_REG);
-               amd64_call_reg (code, GP_SCRATCH_REG);
+               gboolean near_call = FALSE;
+
+               /*
+                * Indirect calls are expensive so try to make a near call if 
possible.
+                * The caller memory is allocated by the code manager so it is 
+                * guaranteed to be at a 32 bit offset.
+                */
+
+               if (patch_type != MONO_PATCH_INFO_ABS)
+                       /* The target is in memory allocated using the code 
manager */
+                       near_call = TRUE;
+               else {
+                       if (mono_find_class_init_trampoline_by_addr (data))
+                               near_call = TRUE;
+                       else {
+                               MonoJitICallInfo *info = 
mono_find_jit_icall_by_addr (data);
+                               if (info) {
+                                       if ((cfg->method->wrapper_type == 
MONO_WRAPPER_MANAGED_TO_NATIVE) && 
+                                               strstr (cfg->method->name, 
info->name)) {
+                                               /* A call to the wrapped 
function */
+                                               if ((((guint64)data) >> 32) == 
0)
+                                                       near_call = TRUE;
+                                       }
+                                       else
+                                               near_call = TRUE;
+                               }
+                               else if ((((guint64)data) >> 32) == 0)
+                                       near_call = TRUE;
+                       }
+               }
+
+               if (near_call) {
+                       amd64_call_code (code, 0);
+               }
+               else {
+                       amd64_set_reg_template (code, GP_SCRATCH_REG);
+                       amd64_call_reg (code, GP_SCRATCH_REG);
+               }
        }
 
        return code;
@@ -4808,8 +4838,7 @@
                        if (mono_compile_aot)
                                amd64_call_membase (ip2, AMD64_RIP, 0);
                        else {
-                               amd64_set_reg_template (ip2, GP_SCRATCH_REG);
-                               amd64_call_reg (ip2, GP_SCRATCH_REG);
+                               amd64_call_code (ip2, 0);
                        }
                        break;
                }
@@ -5609,6 +5638,9 @@
                reg = amd64_modrm_rm (code [6]);
                disp = 0;
        }
+       else if (code [2] == 0xe8)
+               /* call <ADDR> */
+               return NULL;
        else
                g_assert_not_reached ();
 
@@ -5620,6 +5652,28 @@
        return (gpointer)((regs [reg]) + disp);
 }
 
+gpointer*
+mono_amd64_get_delegate_method_ptr_addr (guint8* code, guint64 *regs)
+{
+       guint32 reg;
+       guint32 disp;
+
+       code -= 10;
+
+       if (IS_REX (code [0]) && (code [1] == 0x8b) && (code [3] == 0x48) && 
(code [4] == 0x8b) && (code [5] == 0x40) && (code [7] == 0x48) && (code [8] == 
0xff) && (code [9] == 0xd0)) {
+               /* mov REG, %rax; mov <OFFSET>(%rax), %rax; call *%rax */
+               reg = amd64_rex_b (code [0]) + amd64_modrm_rm (code [2]);
+               disp = code [6];
+
+               if (reg == AMD64_RAX)
+                       return NULL;
+               else
+                       return (gpointer*)(regs [reg] + disp);
+       }
+
+       return NULL;
+}
+
 /*
  * Support for fast access to the thread-local lmf structure using the GS
  * segment register on NPTL + kernel 2.6.x.

Modified: trunk/mono/mono/mini/mini-amd64.h
===================================================================
--- trunk/mono/mono/mini/mini-amd64.h   2005-02-27 13:41:12 UTC (rev 41258)
+++ trunk/mono/mono/mini/mini-amd64.h   2005-02-27 13:44:40 UTC (rev 41259)
@@ -226,6 +226,9 @@
 gpointer*
 mono_amd64_get_vcall_slot_addr (guint8* code, guint64 *regs);
 
+gpointer*
+mono_amd64_get_delegate_method_ptr_addr (guint8* code, guint64 *regs);
+
 void
 mono_amd64_exceptions_init (void);
 

Modified: trunk/mono/mono/mini/tramp-amd64.c
===================================================================
--- trunk/mono/mono/mini/tramp-amd64.c  2005-02-27 13:41:12 UTC (rev 41258)
+++ trunk/mono/mono/mini/tramp-amd64.c  2005-02-27 13:44:40 UTC (rev 41259)
@@ -77,7 +77,7 @@
 amd64_magic_trampoline (long *regs, guint8 *code, MonoMethod *m, guint8* tramp)
 {
        gpointer addr;
-       gpointer *vtable_slot;
+       gpointer *vtable_slot, *method_ptr;
 
        addr = mono_compile_method (m);
        g_assert (addr);
@@ -102,14 +102,21 @@
        else {
                /* Patch calling code */
 
-               if ((code [-13] == 0x49) && (code [-12] == 0xbb)) {
+               if (((code [-13] == 0x49) && (code [-12] == 0xbb)) ||
+                       (code [-5] == 0xe8)) {
                        MonoJitInfo *ji = 
                                mono_jit_info_table_find (mono_domain_get (), 
code);
                        MonoJitInfo *target_ji = 
                                mono_jit_info_table_find (mono_domain_get (), 
addr);
 
                        if (mono_method_same_domain (ji, target_ji)) {
-                               InterlockedExchangePointer ((gpointer*)(code - 
11), addr);
+                               if (code [-5] != 0xe8)
+                                       InterlockedExchangePointer 
((gpointer*)(code - 11), addr);
+                               else {
+                                       g_assert ((((guint64)(addr)) >> 32) == 
0);
+                                       g_assert ((((guint64)(code)) >> 32) == 
0);
+                                       InterlockedExchange ((guint32*)(code - 
4), ((gint64)addr - (gint64)code));
+                               }
                        }
                }
                else if ((code [-7] == 0x41) && (code [-6] == 0xff) && (code 
[-5] == 0x15)) {
@@ -124,19 +131,16 @@
                                InterlockedExchangePointer (got_entry, addr);
                        }
                }
-               else {
-                       /* FIXME: handle more cases */
-               }
+               else if ((method_ptr = mono_amd64_get_delegate_method_ptr_addr 
(code, regs))) {
+                       /* This is a call inside a delegate wrapper to the 
target method */
+                       MonoJitInfo *ji = 
+                               mono_jit_info_table_find (mono_domain_get (), 
code);
+                       MonoJitInfo *target_ji = 
+                               mono_jit_info_table_find (mono_domain_get (), 
addr);
 
-               /* Patch trampoline */
-               /* FIXME: Make this thread safe */
-               if (!m->klass->valuetype) {
-                       /* 
-                        * Valuetype method trampolines can't be patched since 
they are used for virtual
-                        * calls which need an unbox trampoline.
-                        */
-                       amd64_mov_reg_imm (tramp, AMD64_R11, addr);
-                       amd64_jump_reg (tramp, AMD64_R11);
+                       if (ji && (ji->method->wrapper_type == 
MONO_WRAPPER_DELEGATE_INVOKE) && mono_method_same_domain (ji, target_ji)) {
+                               InterlockedExchangePointer (method_ptr, addr);
+                       }
                }
        }
 
@@ -219,8 +223,15 @@
                        buf [11] = 0x66;
                        buf [12] = 0x90;
                }
-       }
-       else if (code [0] == 0x90 || code [0] == 0xeb || code [0] == 0x66)
+       } else if (code [-2] == 0xe8) {
+               guint8 *buf = code - 2;
+
+               buf [0] = 0x66;
+               buf [1] = 0x66;
+               buf [2] = 0x90;
+               buf [3] = 0x66;
+               buf [4] = 0x90;
+       } else if (code [0] == 0x90 || code [0] == 0xeb || code [0] == 0x66)
                /* Already changed by another thread */
                ;
        else if ((code [-4] == 0x41) && (code [-3] == 0xff) && (code [-2] == 
0x15))

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to