2008-08-24 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / tramp-x86.c
index 6900d0c5e7dfbf468e18e74e5d79c5ccfdab9b52..7eb361736e2c073f30777dcd15de4387024716a0 100644 (file)
@@ -29,6 +29,7 @@ static guint8* nullified_class_init_trampoline;
 
 /*
  * mono_arch_get_unbox_trampoline:
+ * @gsctx: the generic sharing context
  * @m: method pointer
  * @addr: pointer to native code for @m
  *
@@ -37,13 +38,13 @@ static guint8* nullified_class_init_trampoline;
  * unboxing before calling the method
  */
 gpointer
-mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
+mono_arch_get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr)
 {
        guint8 *code, *start;
        int this_pos = 4;
        MonoDomain *domain = mono_domain_get ();
 
-       if (!mono_method_signature (m)->ret->byref && MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
+       if (MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
                this_pos = 8;
            
        mono_domain_lock (domain);
@@ -197,30 +198,6 @@ mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
         * the ret address is at: esp + (pushed_args + 1) * sizeof (gpointer)
         */
 
-       /* If this is a generic class init the argument is not on the
-        * stack yet but in MONO_ARCH_VTABLE_REG.  We first check
-        * whether the vtable is already initialized in which case we
-        * just return.  Otherwise we push it and continue.
-        */
-       if (tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
-               static int byte_offset = -1;
-               static guint8 bitmask;
-
-               guint8 *jump;
-
-               if (byte_offset < 0)
-                       mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
-
-               x86_test_membase_imm (buf, MONO_ARCH_VTABLE_REG, byte_offset, bitmask);
-               jump = buf;
-               x86_branch8 (buf, X86_CC_Z, -1, 1);
-
-               x86_ret (buf);
-
-               x86_patch (jump, buf);
-               x86_push_reg (buf, MONO_ARCH_VTABLE_REG);
-       }
-
        /* Put all registers into an array on the stack
         * If this code is changed, make sure to update the offset value in
         * mono_arch_find_this_argument () in mini-x86.c.
@@ -266,7 +243,7 @@ mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
        pushed_args ++;
 
        /* save method info */
-       if ((tramp_type == MONO_TRAMPOLINE_GENERIC) || (tramp_type == MONO_TRAMPOLINE_JUMP))
+       if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
                x86_push_membase (buf, X86_ESP, pushed_args * sizeof (gpointer));
        else
                x86_push_imm (buf, 0);
@@ -337,7 +314,7 @@ mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
        /* Check for thread interruption */
        /* This is not perf critical code so no need to check the interrupt flag */
        x86_push_reg (buf, X86_EAX);
-       x86_call_code (buf, (guint8*)mono_thread_interruption_checkpoint);
+       x86_call_code (buf, (guint8*)mono_thread_force_interruption_checkpoint);
        x86_pop_reg (buf, X86_EAX);
 
        /* Restore LMF */
@@ -434,84 +411,126 @@ mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_ty
 }
 
 gpointer
-mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 encoded_offset)
+mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot)
 {
-       guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_RGCTX_LAZY_FETCH);
-       gboolean indirect = MONO_RGCTX_OFFSET_IS_INDIRECT (encoded_offset);
-       int offset = indirect ? MONO_RGCTX_OFFSET_INDIRECT_OFFSET (encoded_offset) :
-               MONO_RGCTX_OFFSET_DIRECT_OFFSET (encoded_offset);
-       guint8 *code, *buf, *jump;
-       guint8 *dummy;
+       guint8 *tramp;
+       guint8 *code, *buf;
+       guint8 **rgctx_null_jumps;
+       int tramp_size;
+       int depth, index;
+       int i;
+       gboolean mrgctx;
+
+       mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
+       index = MONO_RGCTX_SLOT_INDEX (slot);
+       if (mrgctx)
+               index += sizeof (MonoMethodRuntimeGenericContext) / sizeof (gpointer);
+       for (depth = 0; ; ++depth) {
+               int size = mono_class_rgctx_get_array_size (depth, mrgctx);
+
+               if (index < size - 1)
+                       break;
+               index -= size - 1;
+       }
+
+       tramp_size = 36 + 6 * depth;
 
-       g_assert (tramp);
-       if (indirect)
-               g_assert (MONO_RGCTX_OFFSET_INDIRECT_SLOT (encoded_offset) == 0);
+       code = buf = mono_global_codeman_reserve (tramp_size);
 
-       code = buf = mono_global_codeman_reserve (32);
+       rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
 
-       /* load rgctx ptr */
+       /* load vtable/mrgctx ptr */
        x86_mov_reg_membase (buf, X86_EAX, X86_ESP, 4, 4);
-       if (indirect) {
-               /* if indirect, load extra_other_infos ptr */
-               x86_mov_reg_membase (buf, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoRuntimeGenericContext, extra_other_infos), 4);
+       if (!mrgctx) {
+               /* load rgctx ptr from vtable */
+               x86_mov_reg_membase (buf, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 4);
+               /* is the rgctx ptr null? */
+               x86_test_reg_reg (buf, X86_EAX, X86_EAX);
+               /* if yes, jump to actual trampoline */
+               rgctx_null_jumps [0] = buf;
+               x86_branch8 (buf, X86_CC_Z, -1, 1);
        }
-       /* fetch slot */
-       x86_mov_reg_membase (buf, X86_EAX, X86_EAX, offset, 4);
 
-       dummy = buf;
+       for (i = 0; i < depth; ++i) {
+               /* load ptr to next array */
+               if (mrgctx && i == 0)
+                       x86_mov_reg_membase (buf, X86_EAX, X86_EAX, sizeof (MonoMethodRuntimeGenericContext), 4);
+               else
+                       x86_mov_reg_membase (buf, X86_EAX, X86_EAX, 0, 4);
+               /* is the ptr null? */
+               x86_test_reg_reg (buf, X86_EAX, X86_EAX);
+               /* if yes, jump to actual trampoline */
+               rgctx_null_jumps [i + 1] = buf;
+               x86_branch8 (buf, X86_CC_Z, -1, 1);
+       }
 
-       /* is slot null? */
+       /* fetch slot */
+       x86_mov_reg_membase (buf, X86_EAX, X86_EAX, sizeof (gpointer) * (index + 1), 4);
+       /* is the slot null? */
        x86_test_reg_reg (buf, X86_EAX, X86_EAX);
-       jump = buf;
        /* if yes, jump to actual trampoline */
+       rgctx_null_jumps [depth + 1] = buf;
        x86_branch8 (buf, X86_CC_Z, -1, 1);
-
-       /* if no, just return */
+       /* otherwise return */
        x86_ret (buf);
 
-       /*
-        * our stack looks like this (tos on top):
-        *
-        * | ret addr  |
-        * | rgctx ptr |
-        * | ...       |
-        *
-        * the trampoline code expects it to look like this:
-        *
-        * | rgctx ptr |
-        * | ret addr  |
-        * | ...       |
-        *
-        * whereas our caller expects to still have one argument on
-        * the stack when we return, so we transform the stack into
-        * this:
-        *
-        * | rgctx ptr |
-        * | ret addr  |
-        * | dummy     |
-        * | ...       |
-        *
-        * which actually only requires us to push the rgctx ptr, and
-        * the "old" rgctx ptr becomes the dummy.
-        */
+       for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
+               x86_patch (rgctx_null_jumps [i], buf);
 
-       x86_patch (jump, buf);
-       x86_push_membase (buf, X86_ESP, 4);
+       g_free (rgctx_null_jumps);
 
-       x86_mov_reg_imm (buf, X86_EAX, encoded_offset);
+       x86_mov_reg_membase (buf, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
+
+       tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
+
+       /* jump to the actual trampoline */
        x86_jump_code (buf, tramp);
 
        mono_arch_flush_icache (code, buf - code);
 
-       g_assert (buf - code <= 32);
+       g_assert (buf - code <= tramp_size);
 
        return code;
 }
 
-guint32
-mono_arch_get_rgctx_lazy_fetch_offset (gpointer *regs)
+gpointer
+mono_arch_create_generic_class_init_trampoline (void)
 {
-       return (guint32)(regs [X86_EAX]);
+       guint8 *tramp;
+       guint8 *code, *buf;
+       static int byte_offset = -1;
+       static guint8 bitmask;
+       guint8 *jump;
+       int tramp_size;
+
+       tramp_size = 64;
+
+       code = buf = mono_global_codeman_reserve (tramp_size);
+
+       if (byte_offset < 0)
+               mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
+
+       x86_test_membase_imm (code, MONO_ARCH_VTABLE_REG, byte_offset, bitmask);
+       jump = code;
+       x86_branch8 (code, X86_CC_Z, -1, 1);
+
+       x86_ret (code);
+
+       x86_patch (jump, code);
+
+       /* Push the vtable so the stack is the same as in a specific trampoline */
+       x86_push_reg (code, MONO_ARCH_VTABLE_REG);
+
+       tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
+
+       /* jump to the actual trampoline */
+       x86_jump_code (code, tramp);
+
+       mono_arch_flush_icache (code, code - buf);
+
+       g_assert (code - buf <= tramp_size);
+
+       return buf;
 }
 
 void