2008-09-07 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / tramp-ia64.c
index fd7e42d22a28cfc71105fc864635cf7b33b0f26e..cb962da6251a1190457437f7b6d15d5dd0824ff3 100644 (file)
 #include <mono/metadata/appdomain.h>
 #include <mono/metadata/marshal.h>
 #include <mono/metadata/tabledefs.h>
-#include <mono/arch/ia64/ia64-codegen.h>
 #include <mono/metadata/mono-debug-debugger.h>
+#include <mono/arch/ia64/ia64-codegen.h>
 
 #include "mini.h"
 #include "mini-ia64.h"
 
-#define NOT_IMPLEMENTED g_assert_not_reached ()
-
 #define GP_SCRATCH_REG 31
 #define GP_SCRATCH_REG2 30
 
 /*
  * mono_arch_get_unbox_trampoline:
+ * @gsctx: the generic sharing context
  * @m: method pointer
  * @addr: pointer to native code for @m
  *
  * 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 *buf;
        gpointer func_addr, func_gp;
        Ia64CodegenState code;
        int this_reg = 0;
+       gpointer *desc;
        MonoDomain *domain = mono_domain_get ();
 
        /* FIXME: Optimize this */
 
-       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_reg = 1;
 
        func_addr = ((gpointer*)addr) [0];
@@ -66,9 +66,10 @@ mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
 
        g_assert (code.buf - buf < 256);
 
-       /* FIXME: */
+       mono_arch_flush_icache (buf, code.buf - buf);
 
-       gpointer *desc = g_malloc0 (sizeof (gpointer) * 2);
+       /* FIXME: */
+       desc = g_malloc0 (sizeof (gpointer) * 2);
        desc [0] = buf;
        desc [1] = func_gp;
 
@@ -76,11 +77,12 @@ mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
 }
 
 void
-mono_arch_patch_callsite (guint8 *code, guint8 *addr)
+mono_arch_patch_callsite (guint8 *method_start, guint8 *code, guint8 *addr)
 {
        guint8 *callsite_begin;
        guint64 *callsite = (guint64*)(gpointer)(code - 16);
-       guint64 instructions [3];
+       guint64 *next_bundle;
+       guint64 ins, instructions [3];
        guint64 buf [16];
        Ia64CodegenState gen;
        gpointer func = ((gpointer*)(gpointer)addr)[0];
@@ -90,6 +92,15 @@ mono_arch_patch_callsite (guint8 *code, guint8 *addr)
                callsite -= 2;
        callsite_begin = (guint8*)callsite;
 
+       next_bundle = callsite + 2;
+       ins = ia64_bundle_ins1 (next_bundle);
+       if (ia64_ins_opcode (ins) == 5) {
+               /* ld8_inc_imm -> indirect call through a function pointer */
+               g_assert (ia64_ins_r1 (ins) == GP_SCRATCH_REG2);
+               g_assert (ia64_ins_r3 (ins) == GP_SCRATCH_REG);
+               return;
+       }
+
        /* Patch the code generated by emit_call */
 
        instructions [0] = ia64_bundle_ins1 (callsite);
@@ -112,6 +123,12 @@ mono_arch_patch_callsite (guint8 *code, guint8 *addr)
        mono_arch_flush_icache (callsite_begin, code - callsite_begin);
 }
 
+void
+mono_arch_patch_plt_entry (guint8 *code, guint8 *addr)
+{
+       g_assert_not_reached ();
+}
+
 void
 mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
 {
@@ -159,21 +176,9 @@ mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
 }
 
 void
-mono_arch_patch_delegate_trampoline (guint8 *code, guint8 *tramp, gssize *regs, guint8 *addr)
+mono_arch_nullify_plt_entry (guint8 *code)
 {
-       /* 
-        * This is called by the code generated by OP_CALL_REG:
-        * ld8 r30=[r8],8
-        * nop.i 0x0;;
-        * mov.sptk b6=r30
-        * ld8 r1=[r8]
-        * br.call.sptk.few b0=b6
-        */
-
-       /* We patch the function descriptor instead of delegate->method_ptr */
-       //g_assert (((gpointer*)(regs [8] - 8))[0] == tramp);
-       ((gpointer*)(regs [8] - 8))[0] = mono_get_addr_from_ftnptr (addr);
-       ((gpointer*)(regs [8] - 8))[1] = NULL;
+       g_assert_not_reached ();
 }
 
 guchar*
@@ -201,8 +206,6 @@ mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
 
        ia64_codegen_init (code, buf);
 
-       /* FIXME: Save/restore lmf */
-
        /* Stacked Registers */
        in0 = 32;
        local0 = in0 + 8;
@@ -230,7 +233,9 @@ mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
         * 16 locals
         * 4 output (number of parameters passed to trampoline)
         */
+       ia64_unw_save_reg (code, UNW_IA64_AR_PFS, UNW_IA64_GR + l5);
        ia64_alloc (code, l5, local0 - in0, out0 - local0, 4, 0);
+       ia64_unw_save_reg (code, UNW_IA64_SP, UNW_IA64_GR + l8);
        ia64_mov (code, l8, IA64_SP);
        ia64_adds_imm (code, IA64_SP, (-framesize), IA64_SP);
 
@@ -240,22 +245,12 @@ mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
        ia64_mov (code, l6, GP_SCRATCH_REG);
 
        /* Save the calling address */
+       ia64_unw_save_reg (code, UNW_IA64_RP, UNW_IA64_GR + local0 + 7);
        ia64_mov_from_br (code, l7, IA64_B0);
 
-       /* FIXME: Emit prolog/epilog manually */
-
        /* Create unwind info for the prolog */
-       r_pro = g_malloc0 (_U_dyn_region_info_size (3));
-       r_pro->op_count = 3;
-       r_pro->insn_count = 16;
-       i = 0;
-       _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 2,
-                                               /* reg=*/ UNW_IA64_AR_PFS, /* dst=*/ UNW_IA64_GR + local0 + 5);
-       _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 5,
-                                               /* reg=*/ UNW_IA64_SP, /* dst=*/ UNW_IA64_GR + local0 + 8);
-       _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 14,
-                                               /* reg=*/ UNW_IA64_RP, /* dst=*/ UNW_IA64_GR + local0 + 7);
-       g_assert ((unsigned) i <= r_pro->op_count);     
+       ia64_begin_bundle (code);
+       r_pro = mono_ia64_create_unwind_region (&code);
 
        /* Save registers */
        /* Not needed for jump trampolines */
@@ -301,14 +296,7 @@ mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
        /* FIXME: */
        ia64_mov (code, o3, 0);
 
-       if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
-               tramp = (guint8*)mono_class_init_trampoline;
-       else if (tramp_type == MONO_TRAMPOLINE_AOT)
-               tramp = (guint8*)mono_aot_trampoline;
-       else if (tramp_type == MONO_TRAMPOLINE_DELEGATE)
-               tramp = (guint8*)mono_delegate_trampoline;
-       else
-               tramp = (guint8*)mono_magic_trampoline;
+       tramp = (guint8*)mono_get_trampoline_func (tramp_type);
 
        /* Call the trampoline using an indirect call */
        ia64_movl (code, l0, tramp);
@@ -317,6 +305,19 @@ mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
        ia64_ld8 (code, IA64_GP, l0);
        ia64_br_call_reg (code, 0, IA64_B6);
 
+       /* Check for thread interruption */
+       /* This is not perf critical code so no need to check the interrupt flag */
+       ia64_mov (code, l2, IA64_R8);
+
+       tramp = (guint8*)mono_thread_force_interruption_checkpoint;
+       ia64_movl (code, l0, tramp);
+       ia64_ld8_inc_imm (code, l1, l0, 8);
+       ia64_mov_to_br (code, IA64_B6, l1);
+       ia64_ld8 (code, IA64_GP, l0);
+       ia64_br_call_reg (code, 0, IA64_B6);
+
+       ia64_mov (code, IA64_R8, l2);
+
        /* Restore fp regs */
        ia64_adds_imm (code, l1, saved_fpregs_offset, IA64_SP);
        for (i = 0; i < 8; ++i)
@@ -415,9 +416,9 @@ mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
 }
 
 gpointer
-mono_debugger_create_notification_function (gpointer *notification_address)
+mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 encoded_offset)
 {
-       NOT_IMPLEMENTED;
-
+       /* FIXME: implement! */
+       g_assert_not_reached ();
        return NULL;
 }