svn path=/trunk/mono/; revision=53658
[mono.git] / mono / mini / jit-icalls.c
index 59e1ba7b07b728fda0f10bd321390d843d2e363d..8f0c7aa0efead853447ecfe1df8c923e0bfd7fde 100644 (file)
@@ -19,7 +19,7 @@ mono_ldftn (MonoMethod *method)
 
        addr = mono_create_jump_trampoline (mono_domain_get (), method, TRUE);
 
-       return addr;
+       return mono_create_ftnptr (mono_domain_get (), addr);
 }
 
 /*
@@ -35,7 +35,7 @@ mono_ldftn_nosync (MonoMethod *method)
 
        addr = mono_create_jump_trampoline (mono_domain_get (), method, FALSE);
 
-       return addr;
+       return mono_create_ftnptr (mono_domain_get (), addr);
 }
 
 static void*
@@ -43,35 +43,14 @@ mono_ldvirtfn (MonoObject *obj, MonoMethod *method)
 {
        MONO_ARCH_SAVE_REGS;
 
+       if (obj == NULL)
+               mono_raise_exception (mono_get_exception_null_reference ());
+
        method = mono_object_get_virtual_method (obj, method);
 
        return mono_ldftn (method);
 }
 
-static void
-helper_initobj (void *addr, int size)
-{
-       MONO_ARCH_SAVE_REGS;
-
-       memset (addr, 0, size);
-}
-
-static void
-helper_memcpy (void *addr, void *src, int size)
-{
-       MONO_ARCH_SAVE_REGS;
-
-       memcpy (addr, src, size);
-}
-
-static void
-helper_memset (void *addr, int val, int size)
-{
-       MONO_ARCH_SAVE_REGS;
-
-       memset (addr, val, size);
-}
-
 static void
 helper_stelem_ref (MonoArray *array, int index, MonoObject *val)
 {
@@ -95,6 +74,8 @@ helper_stelem_ref_check (MonoArray *array, MonoObject *val)
                mono_raise_exception (mono_get_exception_array_type_mismatch ());
 }
 
+#ifndef MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS
+
 static gint64 
 mono_llmult (gint64 a, gint64 b)
 {
@@ -134,7 +115,6 @@ mono_llmult_ovf_un (guint64 a, guint64 b)
        return 0;
 }
 
-
 static guint64  
 mono_llmult_ovf (gint64 a, gint64 b) 
 {
@@ -216,6 +196,8 @@ mono_llmult_ovf (gint64 a, gint64 b)
           in a 64 bit result */
        if (ah && bh)
                goto raise_exception;
+       if ((gint64)((gint64)ah * (gint64)bl) > (gint64)0x80000000 || (gint64)((gint64)al * (gint64)bh) > (gint64)0x80000000)
+               goto raise_exception;
 
        /* do the AlBl term first */
        t1 = (gint64)al * (gint64)bl;
@@ -224,8 +206,8 @@ mono_llmult_ovf (gint64 a, gint64 b)
 
        /* now do the [(Ah-Al)(Bl-Bh)+AlBl]R term */
        t1 += (gint64)(ah - al) * (gint64)(bl - bh);
-       t1 <<= 32;
        /* check for overflow */
+       t1 <<= 32;
        if (t1 > (0x7FFFFFFFFFFFFFFFLL - res))
                goto raise_exception;
 
@@ -244,6 +226,112 @@ mono_llmult_ovf (gint64 a, gint64 b)
        return 0;
 }
 
+#if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
+
+static gint32
+mono_idiv (gint32 a, gint32 b)
+{
+       MONO_ARCH_SAVE_REGS;
+
+#ifdef MONO_ARCH_NEED_DIV_CHECK
+       if (!b)
+               mono_raise_exception (mono_get_exception_divide_by_zero ());
+       else if (b == -1 && a == (0x80000000))
+               mono_raise_exception (mono_get_exception_arithmetic ());
+#endif
+       return a / b;
+}
+
+static guint32
+mono_idiv_un (guint32 a, guint32 b)
+{
+       MONO_ARCH_SAVE_REGS;
+
+#ifdef MONO_ARCH_NEED_DIV_CHECK
+       if (!b)
+               mono_raise_exception (mono_get_exception_divide_by_zero ());
+#endif
+       return a / b;
+}
+
+static gint32
+mono_irem (gint32 a, gint32 b)
+{
+       MONO_ARCH_SAVE_REGS;
+
+#ifdef MONO_ARCH_NEED_DIV_CHECK
+       if (!b)
+               mono_raise_exception (mono_get_exception_divide_by_zero ());
+       else if (b == -1 && a == (0x80000000))
+               mono_raise_exception (mono_get_exception_arithmetic ());
+#endif
+
+       return a % b;
+}
+
+static guint32
+mono_irem_un (guint32 a, guint32 b)
+{
+       MONO_ARCH_SAVE_REGS;
+
+#ifdef MONO_ARCH_NEED_DIV_CHECK
+       if (!b)
+               mono_raise_exception (mono_get_exception_divide_by_zero ());
+#endif
+       return a % b;
+}
+
+#endif
+
+#ifdef MONO_ARCH_EMULATE_MUL_DIV
+
+static gint32
+mono_imul (gint32 a, gint32 b)
+{
+       MONO_ARCH_SAVE_REGS;
+
+       return a * b;
+}
+
+static gint32
+mono_imul_ovf (gint32 a, gint32 b)
+{
+       gint64 res;
+
+       MONO_ARCH_SAVE_REGS;
+
+       res = (gint64)a * (gint64)b;
+
+       if ((res > 0x7fffffffL) || (res < -2147483648))
+               mono_raise_exception (mono_get_exception_overflow ());
+
+       return res;
+}
+
+static gint32
+mono_imul_ovf_un (guint32 a, guint32 b)
+{
+       guint64 res;
+
+       MONO_ARCH_SAVE_REGS;
+
+       res = (guint64)a * (guint64)b;
+
+       if ((res >> 32))
+               mono_raise_exception (mono_get_exception_overflow ());
+
+       return res;
+}
+
+static double
+mono_fdiv (double a, double b)
+{
+       MONO_ARCH_SAVE_REGS;
+
+       return a / b;
+}
+#endif
+
 static gint64 
 mono_lldiv (gint64 a, gint64 b)
 {
@@ -296,6 +384,8 @@ mono_llrem_un (guint64 a, guint64 b)
        return a % b;
 }
 
+#endif
+
 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
 
 static guint64 
@@ -377,7 +467,7 @@ ves_array_element_address (MonoArray *this, ...)
        }
        esize *= ind;
 
-       ea = (gpointer*)((char*)this->vector + esize);
+       ea = (gpointer*)(gpointer)((char*)this->vector + esize);
 
        va_end(ap);
 
@@ -397,7 +487,7 @@ mono_array_new_va (MonoMethod *cm, ...)
 
        MONO_ARCH_SAVE_REGS;
 
-       pcount = cm->signature->param_count;
+       pcount = mono_method_signature (cm)->param_count;
        rank = cm->klass->rank;
 
        va_start (ap, cm);
@@ -408,7 +498,12 @@ mono_array_new_va (MonoMethod *cm, ...)
 
        if (rank == pcount) {
                /* Only lengths provided. */
-               lower_bounds = NULL;
+               if (cm->klass->byval_arg.type == MONO_TYPE_ARRAY) {
+                       lower_bounds = alloca (sizeof (guint32) * rank);
+                       memset (lower_bounds, 0, sizeof (guint32) * rank);
+               } else {
+                       lower_bounds = NULL;
+               }
        } else {
                g_assert (pcount == (rank * 2));
                /* lower bounds are first. */
@@ -447,13 +542,13 @@ mono_class_static_field_address (MonoDomain *domain, MonoClassField *field)
 }
 
 static gpointer
-mono_ldtoken_wrapper (MonoImage *image, int token)
+mono_ldtoken_wrapper (MonoImage *image, int token, MonoGenericContext *context)
 {
        MonoClass *handle_class;
        gpointer res;
 
        MONO_ARCH_SAVE_REGS;
-       res = mono_ldtoken (image, token, &handle_class, NULL); 
+       res = mono_ldtoken (image, token, &handle_class, context);      
        mono_class_init (handle_class);
 
        return res;
@@ -488,7 +583,7 @@ extern long double aintl (long double);
 #define trunc aintl
 #else
 /* FIXME: This means we will never throw overflow exceptions */
-#define trunc
+#define trunc(v) res
 #endif
 #endif /* HAVE_TRUNC */
 
@@ -554,3 +649,22 @@ mono_lconv_to_r8_un (guint64 a)
 }
 #endif
 
+static gpointer
+helper_compile_generic_method (MonoObject *obj, MonoMethod *method, MonoGenericContext *context)
+{
+       MonoMethod *vmethod, *inflated;
+       gpointer addr;
+
+       vmethod = mono_object_get_virtual_method (obj, method);
+       inflated = mono_class_inflate_generic_method (vmethod, context);
+       inflated = mono_get_inflated_method (inflated);
+       addr = mono_compile_method (inflated);
+
+       return addr;
+}
+
+static MonoString*
+helper_ldstr (MonoImage *image, guint32 idx)
+{
+       return mono_ldstr (mono_domain_get (), image, idx);
+}