* Paolo Molaro (lupus@ximian.com)
*
* (C) 2002 Ximian, Inc.
+ * Copyright 2003-2011 Novell Inc (http://www.novell.com)
+ * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
*/
-
+#include <config.h>
#include <math.h>
#include <limits.h>
+#ifdef HAVE_ALLOCA_H
+#include <alloca.h>
+#endif
#include "jit-icalls.h"
res = mono_class_inflate_generic_method (res, &context);
}
- /* FIXME: only do this for methods which can be shared! */
- if (res->is_inflated && mono_method_get_context (res)->method_inst &&
- mono_class_generic_sharing_enabled (res->klass)) {
- res = mono_marshal_get_static_rgctx_invoke (res);
- }
+ /* An rgctx wrapper is added by the trampolines no need to do it here */
return mono_ldftn (res);
}
{
MONO_ARCH_SAVE_REGS;
+ if (!array)
+ mono_raise_exception (mono_get_exception_null_reference ());
if (val && !mono_object_isinst (val, array->obj.vtable->klass->element_class))
mono_raise_exception (mono_get_exception_array_type_mismatch ());
}
return 0;
}
-#if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
-
-gint32
-mono_idiv (gint32 a, gint32 b)
+gint64
+mono_lldiv (gint64 a, gint64 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))
+ else if (b == -1 && a == (-9223372036854775807LL - 1LL))
mono_raise_exception (mono_get_exception_arithmetic ());
#endif
return a / b;
}
-guint32
-mono_idiv_un (guint32 a, guint32 b)
+gint64
+mono_llrem (gint64 a, gint64 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 == (-9223372036854775807LL - 1LL))
+ mono_raise_exception (mono_get_exception_arithmetic ());
#endif
- return a / b;
+ return a % b;
}
-gint32
-mono_irem (gint32 a, gint32 b)
+guint64
+mono_lldiv_un (guint64 a, guint64 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;
+ return a / b;
}
-guint32
-mono_irem_un (guint32 a, guint32 b)
+guint64
+mono_llrem_un (guint64 a, guint64 b)
{
MONO_ARCH_SAVE_REGS;
#endif
-#ifdef MONO_ARCH_EMULATE_MUL_DIV
-
-gint32
-mono_imul (gint32 a, gint32 b)
-{
- MONO_ARCH_SAVE_REGS;
-
- return a * b;
-}
+#ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
-gint32
-mono_imul_ovf (gint32 a, gint32 b)
+guint64
+mono_lshl (guint64 a, gint32 shamt)
{
- gint64 res;
-
- MONO_ARCH_SAVE_REGS;
+ guint64 res;
- res = (gint64)a * (gint64)b;
+ /* no need, no exceptions: MONO_ARCH_SAVE_REGS;*/
+ res = a << shamt;
- if ((res > 0x7fffffffL) || (res < -2147483648))
- mono_raise_exception (mono_get_exception_overflow ());
+ /*printf ("TESTL %lld << %d = %lld\n", a, shamt, res);*/
return res;
}
-gint32
-mono_imul_ovf_un (guint32 a, guint32 b)
+guint64
+mono_lshr_un (guint64 a, gint32 shamt)
{
guint64 res;
- MONO_ARCH_SAVE_REGS;
-
- res = (guint64)a * (guint64)b;
+ /* no need, no exceptions: MONO_ARCH_SAVE_REGS;*/
+ res = a >> shamt;
- if ((res >> 32))
- mono_raise_exception (mono_get_exception_overflow ());
+ /*printf ("TESTR %lld >> %d = %lld\n", a, shamt, res);*/
return res;
}
-#endif
-#if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_SOFT_FLOAT)
-double
-mono_fdiv (double a, double b)
+gint64
+mono_lshr (gint64 a, gint32 shamt)
{
- MONO_ARCH_SAVE_REGS;
+ gint64 res;
- return a / b;
+ /* no need, no exceptions: MONO_ARCH_SAVE_REGS;*/
+ res = a >> shamt;
+
+ /*printf ("TESTR %lld >> %d = %lld\n", a, shamt, res);*/
+
+ return res;
}
+
#endif
-gint64
-mono_lldiv (gint64 a, gint64 b)
+#if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
+
+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 == (-9223372036854775807LL - 1LL))
- mono_raise_exception (mono_get_exception_arithmetic ());
+ else if (b == -1 && a == (0x80000000))
+ mono_raise_exception (mono_get_exception_overflow ());
#endif
return a / b;
}
-gint64
-mono_llrem (gint64 a, gint64 b)
+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 ());
- else if (b == -1 && a == (-9223372036854775807LL - 1LL))
- mono_raise_exception (mono_get_exception_arithmetic ());
#endif
- return a % b;
+ return a / b;
}
-guint64
-mono_lldiv_un (guint64 a, guint64 b)
+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_overflow ());
#endif
- return a / b;
+
+ return a % b;
}
-guint64
-mono_llrem_un (guint64 a, guint64 b)
+guint32
+mono_irem_un (guint32 a, guint32 b)
{
MONO_ARCH_SAVE_REGS;
#endif
-#ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
+#if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_MUL_OVF)
-guint64
-mono_lshl (guint64 a, gint32 shamt)
+gint32
+mono_imul (gint32 a, gint32 b)
{
- guint64 res;
-
- /* no need, no exceptions: MONO_ARCH_SAVE_REGS;*/
- res = a << shamt;
-
- /*printf ("TESTL %lld << %d = %lld\n", a, shamt, res);*/
+ MONO_ARCH_SAVE_REGS;
- return res;
+ return a * b;
}
-guint64
-mono_lshr_un (guint64 a, gint32 shamt)
+gint32
+mono_imul_ovf (gint32 a, gint32 b)
{
- guint64 res;
+ gint64 res;
- /* no need, no exceptions: MONO_ARCH_SAVE_REGS;*/
- res = a >> shamt;
+ MONO_ARCH_SAVE_REGS;
- /*printf ("TESTR %lld >> %d = %lld\n", a, shamt, res);*/
+ res = (gint64)a * (gint64)b;
+
+ if ((res > 0x7fffffffL) || (res < -2147483648LL))
+ mono_raise_exception (mono_get_exception_overflow ());
return res;
}
-gint64
-mono_lshr (gint64 a, gint32 shamt)
+gint32
+mono_imul_ovf_un (guint32 a, guint32 b)
{
- gint64 res;
+ guint64 res;
- /* no need, no exceptions: MONO_ARCH_SAVE_REGS;*/
- res = a >> shamt;
+ MONO_ARCH_SAVE_REGS;
- /*printf ("TESTR %lld >> %d = %lld\n", a, shamt, res);*/
+ res = (guint64)a * (guint64)b;
+
+ if ((res >> 32))
+ mono_raise_exception (mono_get_exception_overflow ());
return res;
}
+#endif
+
+#if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_SOFT_FLOAT)
+double
+mono_fdiv (double a, double b)
+{
+ MONO_ARCH_SAVE_REGS;
+ return a / b;
+}
#endif
#ifdef MONO_ARCH_SOFT_FLOAT
return isunordered (a, b) || a < b;
}
+gboolean
+mono_isfinite (double a)
+{
+#ifdef HAVE_ISFINITE
+ return isfinite (a);
+#else
+ g_assert_not_reached ();
+ return TRUE;
+#endif
+}
+
double
mono_fload_r4 (float *ptr)
{
{
MonoDomain *domain = mono_domain_get ();
va_list ap;
- guint32 *lengths;
- guint32 *lower_bounds;
+ uintptr_t *lengths;
+ intptr_t *lower_bounds;
int pcount;
int rank;
int i, d;
va_start (ap, cm);
- lengths = alloca (sizeof (guint32) * pcount);
+ lengths = alloca (sizeof (uintptr_t) * pcount);
for (i = 0; i < pcount; ++i)
lengths [i] = d = va_arg(ap, int);
if (rank == pcount) {
/* Only lengths provided. */
if (cm->klass->byval_arg.type == MONO_TYPE_ARRAY) {
- lower_bounds = alloca (sizeof (guint32) * rank);
- memset (lower_bounds, 0, sizeof (guint32) * rank);
+ lower_bounds = alloca (sizeof (intptr_t) * rank);
+ memset (lower_bounds, 0, sizeof (intptr_t) * rank);
} else {
lower_bounds = NULL;
}
} else {
g_assert (pcount == (rank * 2));
/* lower bounds are first. */
- lower_bounds = lengths;
+ lower_bounds = (intptr_t*)lengths;
lengths += rank;
}
va_end(ap);
mono_array_new_1 (MonoMethod *cm, guint32 length)
{
MonoDomain *domain = mono_domain_get ();
- guint32 lengths [1];
- guint32 *lower_bounds;
+ uintptr_t lengths [1];
+ intptr_t *lower_bounds;
int pcount;
int rank;
g_assert (rank == pcount);
if (cm->klass->byval_arg.type == MONO_TYPE_ARRAY) {
- lower_bounds = alloca (sizeof (guint32) * rank);
- memset (lower_bounds, 0, sizeof (guint32) * rank);
+ lower_bounds = alloca (sizeof (intptr_t) * rank);
+ memset (lower_bounds, 0, sizeof (intptr_t) * rank);
} else {
lower_bounds = NULL;
}
mono_array_new_2 (MonoMethod *cm, guint32 length1, guint32 length2)
{
MonoDomain *domain = mono_domain_get ();
- guint32 lengths [2];
- guint32 *lower_bounds;
+ uintptr_t lengths [2];
+ intptr_t *lower_bounds;
int pcount;
int rank;
g_assert (rank == pcount);
if (cm->klass->byval_arg.type == MONO_TYPE_ARRAY) {
- lower_bounds = alloca (sizeof (guint32) * rank);
- memset (lower_bounds, 0, sizeof (guint32) * rank);
+ lower_bounds = alloca (sizeof (intptr_t) * rank);
+ memset (lower_bounds, 0, sizeof (intptr_t) * rank);
+ } else {
+ lower_bounds = NULL;
+ }
+
+ return mono_array_new_full (domain, cm->klass, lengths, lower_bounds);
+}
+
+MonoArray *
+mono_array_new_3 (MonoMethod *cm, guint32 length1, guint32 length2, guint32 length3)
+{
+ MonoDomain *domain = mono_domain_get ();
+ uintptr_t lengths [3];
+ intptr_t *lower_bounds;
+ int pcount;
+ int rank;
+
+ MONO_ARCH_SAVE_REGS;
+
+ pcount = mono_method_signature (cm)->param_count;
+ rank = cm->klass->rank;
+
+ lengths [0] = length1;
+ lengths [1] = length2;
+ lengths [2] = length3;
+
+ g_assert (rank == pcount);
+
+ if (cm->klass->byval_arg.type == MONO_TYPE_ARRAY) {
+ lower_bounds = alloca (sizeof (intptr_t) * rank);
+ memset (lower_bounds, 0, sizeof (intptr_t) * rank);
} else {
lower_bounds = NULL;
}
mono_class_init (field->parent);
- vtable = mono_class_vtable (domain, field->parent);
+ vtable = mono_class_vtable_full (domain, field->parent, TRUE);
if (!vtable->initialized)
mono_runtime_class_init (vtable);
if (domain->special_static_fields && (addr = g_hash_table_lookup (domain->special_static_fields, field)))
addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
else
- addr = (char*)vtable->data + field->offset;
+ addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
return addr;
}
}
#endif
+#if defined(__native_client_codegen__) || defined(__native_client__)
+/* When we cross-compile to Native Client we can't directly embed calls */
+/* to the math library on the host. This will use the fmod on the target*/
+double
+mono_fmod(double a, double b)
+{
+ return fmod(a, b);
+}
+#endif
+
gpointer
mono_helper_compile_generic_method (MonoObject *obj, MonoMethod *method, gpointer *this_arg)
{
- MonoMethod *vmethod, *inflated;
+ MonoMethod *vmethod;
gpointer addr;
MonoGenericContext *context = mono_method_get_context (method);
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);
- if (mono_class_generic_sharing_enabled (inflated->klass) &&
- mono_method_is_generic_sharable_impl (method, FALSE)) {
- /* The method is shared generic code, so it needs a
- MRGCTX. */
- inflated = mono_marshal_get_static_rgctx_invoke (inflated);
- }
- addr = mono_compile_method (inflated);
+
+ addr = mono_compile_method (vmethod);
+
+ if (mono_method_needs_static_rgctx_invoke (vmethod, FALSE))
+ addr = mono_create_static_rgctx_trampoline (vmethod, addr);
/* Since this is a virtual call, have to unbox vtypes */
if (obj->vtable->klass->valuetype)
MonoObject*
mono_object_castclass (MonoObject *obj, MonoClass *klass)
{
+ MonoJitTlsData *jit_tls = NULL;
+
+ if (mini_get_debug_options ()->better_cast_details) {
+ jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
+ jit_tls->class_cast_from = NULL;
+ }
+
if (!obj)
return NULL;
if (mono_object_isinst (obj, klass))
return obj;
+ if (mini_get_debug_options ()->better_cast_details) {
+ jit_tls->class_cast_from = obj->vtable->klass;
+ jit_tls->class_cast_to = klass;
+ }
+
mono_raise_exception (mono_exception_from_name (mono_defaults.corlib,
"System", "InvalidCastException"));
return NULL;
}
+
+MonoObject*
+mono_object_castclass_with_cache (MonoObject *obj, MonoClass *klass, gpointer *cache)
+{
+ MonoJitTlsData *jit_tls = NULL;
+ gpointer cached_vtable, obj_vtable;
+
+ if (mini_get_debug_options ()->better_cast_details) {
+ jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
+ jit_tls->class_cast_from = NULL;
+ }
+
+ if (!obj)
+ return NULL;
+
+ cached_vtable = *cache;
+ obj_vtable = obj->vtable;
+
+ if (cached_vtable == obj_vtable)
+ return obj;
+
+ if (mono_object_isinst (obj, klass)) {
+ *cache = obj_vtable;
+ return obj;
+ }
+
+ if (mini_get_debug_options ()->better_cast_details) {
+ jit_tls->class_cast_from = obj->vtable->klass;
+ jit_tls->class_cast_to = klass;
+ }
+
+ mono_raise_exception (mono_exception_from_name (mono_defaults.corlib,
+ "System", "InvalidCastException"));
+
+ return NULL;
+}
+
+MonoObject*
+mono_object_isinst_with_cache (MonoObject *obj, MonoClass *klass, gpointer *cache)
+{
+ size_t cached_vtable, obj_vtable;
+
+ if (!obj)
+ return NULL;
+
+ cached_vtable = (size_t)*cache;
+ obj_vtable = (size_t)obj->vtable;
+
+ if ((cached_vtable & ~0x1) == obj_vtable) {
+ return (cached_vtable & 0x1) ? NULL : obj;
+ }
+
+ if (mono_object_isinst (obj, klass)) {
+ *cache = (gpointer)obj_vtable;
+ return obj;
+ } else {
+ /*negative cache*/
+ *cache = (gpointer)(obj_vtable | 0x1);
+ return NULL;
+ }
+}
+
+gpointer
+mono_get_native_calli_wrapper (MonoImage *image, MonoMethodSignature *sig, gpointer func)
+{
+ MonoMarshalSpec **mspecs;
+ MonoMethodPInvoke piinfo;
+ MonoMethod *m;
+
+ mspecs = g_new0 (MonoMarshalSpec*, sig->param_count + 1);
+ memset (&piinfo, 0, sizeof (piinfo));
+
+ m = mono_marshal_get_native_func_wrapper (image, sig, &piinfo, mspecs, func);
+
+ return mono_compile_method (m);
+}