2007-07-09 Mark Probst <mark.probst@gmail.com>
[mono.git] / mono / mini / jit-icalls.c
index 8f0c7aa0efead853447ecfe1df8c923e0bfd7fde..d4dc21b1563773e964a00e3c64070951c91f6cd2 100644 (file)
@@ -10,7 +10,9 @@
 
 #include <math.h>
 
-static void*
+#include "jit-icalls.h"
+
+void*
 mono_ldftn (MonoMethod *method)
 {
        gpointer addr;
@@ -26,7 +28,7 @@ mono_ldftn (MonoMethod *method)
  * Same as mono_ldftn, but do not add a synchronized wrapper. Used in the
  * synchronized wrappers to avoid infinite recursion.
  */
-static void*
+void*
 mono_ldftn_nosync (MonoMethod *method)
 {
        gpointer addr;
@@ -38,7 +40,7 @@ mono_ldftn_nosync (MonoMethod *method)
        return mono_create_ftnptr (mono_domain_get (), addr);
 }
 
-static void*
+void*
 mono_ldvirtfn (MonoObject *obj, MonoMethod *method) 
 {
        MONO_ARCH_SAVE_REGS;
@@ -51,22 +53,8 @@ mono_ldvirtfn (MonoObject *obj, MonoMethod *method)
        return mono_ldftn (method);
 }
 
-static void
-helper_stelem_ref (MonoArray *array, int index, MonoObject *val)
-{
-       MONO_ARCH_SAVE_REGS;
-
-       if (index >= array->max_length)
-               mono_raise_exception (mono_get_exception_index_out_of_range ());
-
-       if (val && !mono_object_isinst (val, array->obj.vtable->klass->element_class))
-               mono_raise_exception (mono_get_exception_array_type_mismatch ());
-
-       mono_array_set (array, gpointer, index, val);
-}
-
-static void
-helper_stelem_ref_check (MonoArray *array, MonoObject *val)
+void
+mono_helper_stelem_ref_check (MonoArray *array, MonoObject *val)
 {
        MONO_ARCH_SAVE_REGS;
 
@@ -76,14 +64,14 @@ helper_stelem_ref_check (MonoArray *array, MonoObject *val)
 
 #ifndef MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS
 
-static gint64 
+gint64 
 mono_llmult (gint64 a, gint64 b)
 {
        /* no need, no exceptions: MONO_ARCH_SAVE_REGS;*/
        return a * b;
 }
 
-static guint64  
+guint64  
 mono_llmult_ovf_un (guint64 a, guint64 b)
 {
        guint32 al = a;
@@ -115,7 +103,7 @@ mono_llmult_ovf_un (guint64 a, guint64 b)
        return 0;
 }
 
-static guint64  
+guint64  
 mono_llmult_ovf (gint64 a, gint64 b) 
 {
        guint32 al = a;
@@ -228,7 +216,7 @@ mono_llmult_ovf (gint64 a, gint64 b)
 
 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
 
-static gint32
+gint32
 mono_idiv (gint32 a, gint32 b)
 {
        MONO_ARCH_SAVE_REGS;
@@ -242,7 +230,7 @@ mono_idiv (gint32 a, gint32 b)
        return a / b;
 }
 
-static guint32
+guint32
 mono_idiv_un (guint32 a, guint32 b)
 {
        MONO_ARCH_SAVE_REGS;
@@ -254,7 +242,7 @@ mono_idiv_un (guint32 a, guint32 b)
        return a / b;
 }
 
-static gint32
+gint32
 mono_irem (gint32 a, gint32 b)
 {
        MONO_ARCH_SAVE_REGS;
@@ -269,7 +257,7 @@ mono_irem (gint32 a, gint32 b)
        return a % b;
 }
 
-static guint32
+guint32
 mono_irem_un (guint32 a, guint32 b)
 {
        MONO_ARCH_SAVE_REGS;
@@ -285,7 +273,7 @@ mono_irem_un (guint32 a, guint32 b)
 
 #ifdef MONO_ARCH_EMULATE_MUL_DIV
 
-static gint32
+gint32
 mono_imul (gint32 a, gint32 b)
 {
        MONO_ARCH_SAVE_REGS;
@@ -293,7 +281,7 @@ mono_imul (gint32 a, gint32 b)
        return a * b;
 }
 
-static gint32
+gint32
 mono_imul_ovf (gint32 a, gint32 b)
 {
        gint64 res;
@@ -308,7 +296,7 @@ mono_imul_ovf (gint32 a, gint32 b)
        return res;
 }
 
-static gint32
+gint32
 mono_imul_ovf_un (guint32 a, guint32 b)
 {
        guint64 res;
@@ -322,8 +310,10 @@ mono_imul_ovf_un (guint32 a, guint32 b)
 
        return res;
 }
+#endif
 
-static double
+#if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_SOFT_FLOAT)
+double
 mono_fdiv (double a, double b)
 {
        MONO_ARCH_SAVE_REGS;
@@ -332,7 +322,7 @@ mono_fdiv (double a, double b)
 }
 #endif
 
-static gint64 
+gint64 
 mono_lldiv (gint64 a, gint64 b)
 {
        MONO_ARCH_SAVE_REGS;
@@ -346,7 +336,7 @@ mono_lldiv (gint64 a, gint64 b)
        return a / b;
 }
 
-static gint64 
+gint64 
 mono_llrem (gint64 a, gint64 b)
 {
        MONO_ARCH_SAVE_REGS;
@@ -360,7 +350,7 @@ mono_llrem (gint64 a, gint64 b)
        return a % b;
 }
 
-static guint64 
+guint64 
 mono_lldiv_un (guint64 a, guint64 b)
 {
        MONO_ARCH_SAVE_REGS;
@@ -372,7 +362,7 @@ mono_lldiv_un (guint64 a, guint64 b)
        return a / b;
 }
 
-static guint64 
+guint64 
 mono_llrem_un (guint64 a, guint64 b)
 {
        MONO_ARCH_SAVE_REGS;
@@ -388,7 +378,7 @@ mono_llrem_un (guint64 a, guint64 b)
 
 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
 
-static guint64 
+guint64 
 mono_lshl (guint64 a, gint32 shamt)
 {
        guint64 res;
@@ -401,7 +391,7 @@ mono_lshl (guint64 a, gint32 shamt)
        return res;
 }
 
-static guint64 
+guint64 
 mono_lshr_un (guint64 a, gint32 shamt)
 {
        guint64 res;
@@ -414,7 +404,7 @@ mono_lshr_un (guint64 a, gint32 shamt)
        return res;
 }
 
-static gint64 
+gint64 
 mono_lshr (gint64 a, gint32 shamt)
 {
        gint64 res;
@@ -429,52 +419,193 @@ mono_lshr (gint64 a, gint32 shamt)
 
 #endif
 
-/**
- * ves_array_element_address:
- * @this: a pointer to the array object
- *
- * Returns: the address of an array element.
- */
-static gpointer 
-ves_array_element_address (MonoArray *this, ...)
+#ifdef MONO_ARCH_SOFT_FLOAT
+
+double
+mono_fsub (double a, double b)
 {
-       MonoClass *class;
-       va_list ap;
-       int i, ind, esize, realidx;
-       gpointer ea;
+       return a - b;
+}
 
-       MONO_ARCH_SAVE_REGS;
+double
+mono_fadd (double a, double b)
+{
+       return a + b;
+}
 
-       g_assert (this != NULL);
+double
+mono_fmul (double a, double b)
+{
+       return a * b;
+}
 
-       va_start(ap, this);
+double
+mono_fneg (double a)
+{
+       return -a;
+}
 
-       class = this->obj.vtable->klass;
+double
+mono_fconv_r4 (double a)
+{
+       return (float)a;
+}
 
-       g_assert (this->bounds != NULL);
+double
+mono_conv_to_r8 (int a)
+{
+       return (double)a;
+}
 
-       esize = mono_array_element_size (class);
-       ind = va_arg(ap, int);
-       ind -= (int)this->bounds [0].lower_bound;
-       if ((guint32)ind >= (guint32)this->bounds [0].length)
-               mono_raise_exception (mono_get_exception_index_out_of_range ());
-       for (i = 1; i < class->rank; i++) {
-               realidx = va_arg(ap, int) - (int)this->bounds [i].lower_bound;
-               if ((guint32)realidx >= (guint32)this->bounds [i].length)
-                       mono_raise_exception (mono_get_exception_index_out_of_range ());
-               ind *= this->bounds [i].length;
-               ind += realidx;
-       }
-       esize *= ind;
+double
+mono_conv_to_r4 (int a)
+{
+       return (double)(float)a;
+}
 
-       ea = (gpointer*)(gpointer)((char*)this->vector + esize);
+gint8
+mono_fconv_i1 (double a)
+{
+       return (gint8)a;
+}
 
-       va_end(ap);
+gint16
+mono_fconv_i2 (double a)
+{
+       return (gint16)a;
+}
 
-       return ea;
+gint32
+mono_fconv_i4 (double a)
+{
+       return (gint32)a;
+}
+
+guint8
+mono_fconv_u1 (double a)
+{
+       return (guint8)a;
+}
+
+guint16
+mono_fconv_u2 (double a)
+{
+       return (guint16)a;
 }
 
-static MonoArray *
+gboolean
+mono_fcmp_eq (double a, double b)
+{
+       return a == b;
+}
+
+gboolean
+mono_fcmp_ge (double a, double b)
+{
+       return a >= b;
+}
+
+gboolean
+mono_fcmp_gt (double a, double b)
+{
+       return a > b;
+}
+
+gboolean
+mono_fcmp_le (double a, double b)
+{
+       return a <= b;
+}
+
+gboolean
+mono_fcmp_lt (double a, double b)
+{
+       return a < b;
+}
+
+gboolean
+mono_fcmp_ne_un (double a, double b)
+{
+       return isunordered (a, b) || a != b;
+}
+
+gboolean
+mono_fcmp_ge_un (double a, double b)
+{
+       return isunordered (a, b) || a >= b;
+}
+
+gboolean
+mono_fcmp_gt_un (double a, double b)
+{
+       return isunordered (a, b) || a > b;
+}
+
+gboolean
+mono_fcmp_le_un (double a, double b)
+{
+       return isunordered (a, b) || a <= b;
+}
+
+gboolean
+mono_fcmp_lt_un (double a, double b)
+{
+       return isunordered (a, b) || a < b;
+}
+
+gboolean
+mono_fceq (double a, double b)
+{
+       return a == b;
+}
+
+gboolean
+mono_fcgt (double a, double b)
+{
+       return a > b;
+}
+
+gboolean
+mono_fcgt_un (double a, double b)
+{
+       return a > b;
+}
+
+gboolean
+mono_fclt (double a, double b)
+{
+       return a < b;
+}
+
+gboolean
+mono_fclt_un (double a, double b)
+{
+       return a < b;
+}
+
+double
+mono_fload_r4 (float *ptr)
+{
+       return *ptr;
+}
+
+void
+mono_fstore_r4 (double val, float *ptr)
+{
+       *ptr = (float)val;
+}
+
+/* returns the integer bitpattern that is passed in the regs or stack */
+guint32
+mono_fload_r4_arg (double val)
+{
+       float v = (float)val;
+       return *(guint32*)&v;
+}
+
+#endif
+
+MonoArray *
 mono_array_new_va (MonoMethod *cm, ...)
 {
        MonoDomain *domain = mono_domain_get ();
@@ -515,7 +646,7 @@ mono_array_new_va (MonoMethod *cm, ...)
        return mono_array_new_full (domain, cm->klass, lengths, lower_bounds);
 }
 
-static gpointer
+gpointer
 mono_class_static_field_address (MonoDomain *domain, MonoClassField *field)
 {
        MonoVTable *vtable;
@@ -541,7 +672,7 @@ mono_class_static_field_address (MonoDomain *domain, MonoClassField *field)
        return addr;
 }
 
-static gpointer
+gpointer
 mono_ldtoken_wrapper (MonoImage *image, int token, MonoGenericContext *context)
 {
        MonoClass *handle_class;
@@ -554,14 +685,14 @@ mono_ldtoken_wrapper (MonoImage *image, int token, MonoGenericContext *context)
        return res;
 }
 
-static guint64
+guint64
 mono_fconv_u8 (double v)
 {
        return (guint64)v;
 }
 
 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
-static gint64
+gint64
 mono_fconv_i8 (double v)
 {
        /* no need, no exceptions: MONO_ARCH_SAVE_REGS;*/
@@ -569,7 +700,7 @@ mono_fconv_i8 (double v)
 }
 #endif
 
-static guint32
+guint32
 mono_fconv_u4 (double v)
 {
        /* no need, no exceptions: MONO_ARCH_SAVE_REGS;*/
@@ -587,7 +718,7 @@ extern long double aintl (long double);
 #endif
 #endif /* HAVE_TRUNC */
 
-static gint64
+gint64
 mono_fconv_ovf_i8 (double v)
 {
        gint64 res;
@@ -602,7 +733,7 @@ mono_fconv_ovf_i8 (double v)
        return res;
 }
 
-static guint64
+guint64
 mono_fconv_ovf_u8 (double v)
 {
        guint64 res;
@@ -618,7 +749,7 @@ mono_fconv_ovf_u8 (double v)
 }
 
 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
-static double
+double
 mono_lconv_to_r8 (gint64 a)
 {
        return (double)a;
@@ -626,7 +757,7 @@ mono_lconv_to_r8 (gint64 a)
 #endif
 
 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
-static float
+float
 mono_lconv_to_r4 (gint64 a)
 {
        return (float)a;
@@ -634,7 +765,7 @@ mono_lconv_to_r4 (gint64 a)
 #endif
 
 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
-static double
+double
 mono_conv_to_r8_un (guint32 a)
 {
        return (double)a;
@@ -642,29 +773,65 @@ mono_conv_to_r8_un (guint32 a)
 #endif
 
 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
-static double
+double
 mono_lconv_to_r8_un (guint64 a)
 {
        return (double)a;
 }
 #endif
 
-static gpointer
-helper_compile_generic_method (MonoObject *obj, MonoMethod *method, MonoGenericContext *context)
+gpointer
+mono_helper_compile_generic_method (MonoObject *obj, MonoMethod *method, MonoGenericContext *context, gpointer *this_arg)
 {
        MonoMethod *vmethod, *inflated;
        gpointer addr;
 
+       mono_jit_stats.generic_virtual_invocations++;
+
+       if (obj == NULL)
+               mono_raise_exception (mono_get_exception_null_reference ());
        vmethod = mono_object_get_virtual_method (obj, method);
+
+       /* 'vmethod' is partially inflated.  All the blanks corresponding to the type parameters of the
+          declaring class have been inflated.  We still need to fully inflate the method parameters.
+
+          FIXME: This code depends on the declaring class being fully inflated, since we inflate it twice with 
+          the same context.
+       */
+       g_assert (!vmethod->klass->generic_container);
+       g_assert (!vmethod->klass->generic_class || !vmethod->klass->generic_class->context.class_inst->is_open);
+       g_assert (!context->method_inst || !context->method_inst->is_open);
        inflated = mono_class_inflate_generic_method (vmethod, context);
        inflated = mono_get_inflated_method (inflated);
        addr = mono_compile_method (inflated);
 
+       /* Since this is a virtual call, have to unbox vtypes */
+       if (obj->vtable->klass->valuetype)
+               *this_arg = mono_object_unbox (obj);
+       else
+               *this_arg = obj;
+
        return addr;
 }
 
-static MonoString*
-helper_ldstr (MonoImage *image, guint32 idx)
+MonoString*
+mono_helper_ldstr (MonoImage *image, guint32 idx)
 {
        return mono_ldstr (mono_domain_get (), image, idx);
 }
+
+MonoString*
+mono_helper_ldstr_mscorlib (guint32 idx)
+{
+       return mono_ldstr (mono_domain_get (), mono_defaults.corlib, idx);
+}
+
+MonoObject*
+mono_helper_newobj_mscorlib (guint32 idx)
+{
+       MonoClass *klass = mono_class_get (mono_defaults.corlib, MONO_TOKEN_TYPE_DEF | idx);
+       
+       g_assert (klass);
+
+       return mono_object_new (mono_domain_get (), klass);
+}