3 * PLEASE NOTE: This is a research prototype.
6 * interp.c: Interpreter for CIL byte codes
9 * Paolo Molaro (lupus@ximian.com)
10 * Miguel de Icaza (miguel@ximian.com)
11 * Dietmar Maurer (dietmar@ximian.com)
13 * (C) 2001, 2002 Ximian, Inc.
28 #include <mono/utils/gc_wrapper.h>
34 # define alloca __builtin_alloca
38 /* trim excessive headers */
39 #include <mono/metadata/image.h>
40 #include <mono/metadata/assembly-internals.h>
41 #include <mono/metadata/cil-coff.h>
42 #include <mono/metadata/mono-endian.h>
43 #include <mono/metadata/tabledefs.h>
44 #include <mono/metadata/tokentype.h>
45 #include <mono/metadata/loader.h>
46 #include <mono/metadata/threads.h>
47 #include <mono/metadata/threadpool.h>
48 #include <mono/metadata/profiler-private.h>
49 #include <mono/metadata/appdomain.h>
50 #include <mono/metadata/reflection.h>
51 #include <mono/metadata/reflection-internals.h>
52 #include <mono/metadata/exception.h>
53 #include <mono/metadata/verify.h>
54 #include <mono/metadata/opcodes.h>
55 #include <mono/metadata/debug-helpers.h>
56 #include <mono/metadata/mono-config.h>
57 #include <mono/metadata/marshal.h>
58 #include <mono/metadata/environment.h>
59 #include <mono/metadata/mono-debug.h>
60 #include <mono/utils/atomic.h>
63 #include "interp-internals.h"
67 #include <mono/mini/mini.h>
68 #include <mono/mini/jit-icalls.h>
69 #include <mono/mini/debugger-agent.h>
72 #include <mono/mini/mini-arm.h>
75 /* Mingw 2.1 doesnt need this any more, but leave it in for now for older versions */
78 #define finite _finite
82 #define finite isfinite
87 init_frame (MonoInvocation *frame, MonoInvocation *parent_frame, RuntimeMethod *rmethod, stackval *method_args, stackval *method_retval)
89 frame->parent = parent_frame;
90 frame->stack_args = method_args;
91 frame->retval = method_retval;
92 frame->runtime_method = rmethod;
95 frame->invoke_trap = 0;
98 #define INIT_FRAME(frame,parent_frame,method_args,method_retval,domain,mono_method,error) do { \
99 RuntimeMethod *_rmethod = mono_interp_get_runtime_method ((domain), (mono_method), (error)); \
100 init_frame ((frame), (parent_frame), _rmethod, (method_args), (method_retval)); \
104 * List of classes whose methods will be executed by transitioning to JITted code.
108 /* If TRUE, interpreted code will be interrupted at function entry/backward branches */
109 static gboolean ss_enabled;
111 void ves_exec_method (MonoInvocation *frame);
113 static char* dump_frame (MonoInvocation *inv);
114 static MonoArray *get_trace_ips (MonoDomain *domain, MonoInvocation *top);
115 static void ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, unsigned short *start_with_ip, MonoException *filter_exception, int exit_at_finally);
117 typedef void (*ICallMethod) (MonoInvocation *frame);
119 static guint32 die_on_exception = 0;
120 static MonoNativeTlsKey thread_context_id;
122 static char* dump_args (MonoInvocation *inv);
124 #define DEBUG_INTERP 0
127 int mono_interp_traceopt = 2;
128 /* If true, then we output the opcodes as we interpret them */
129 static int global_tracing = 2;
131 static int debug_indent_level = 0;
133 static int break_on_method = 0;
134 static int nested_trace = 0;
135 static GList *db_methods = NULL;
142 for (h = 0; h < debug_indent_level; h++)
147 db_match_method (gpointer data, gpointer user_data)
149 MonoMethod *m = (MonoMethod*)user_data;
150 MonoMethodDesc *desc = data;
152 if (mono_method_desc_full_match (desc, m))
157 debug_enter (MonoInvocation *frame, int *tracing)
160 g_list_foreach (db_methods, db_match_method, (gpointer)frame->runtime_method->method);
162 *tracing = nested_trace ? (global_tracing = 2, 3) : 2;
166 MonoMethod *method = frame->runtime_method->method;
167 char *mn, *args = dump_args (frame);
168 debug_indent_level++;
170 mn = mono_method_full_name (method, FALSE);
171 g_print ("(%p) Entering %s (", mono_thread_internal_current (), mn);
173 g_print ("%s)\n", args);
176 if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE)
177 mono_profiler_method_enter (frame->runtime_method->method);
181 #define DEBUG_LEAVE() \
184 args = dump_retval (frame); \
186 mn = mono_method_full_name (frame->runtime_method->method, FALSE); \
187 g_print ("(%p) Leaving %s", mono_thread_internal_current (), mn); \
189 g_print (" => %s\n", args); \
191 debug_indent_level--; \
192 if (tracing == 3) global_tracing = 0; \
194 if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE) \
195 mono_profiler_method_leave (frame->runtime_method->method);
199 int mono_interp_traceopt = 0;
200 static void debug_enter (MonoInvocation *frame, int *tracing)
203 #define DEBUG_LEAVE()
207 /* Set the current execution state to the resume state in context */
208 #define SET_RESUME_STATE(context) do { \
209 ip = (context)->handler_ip; \
211 sp->data.p = frame->ex; \
215 (context)->has_resume_state = 0; \
216 (context)->handler_frame = NULL; \
221 set_context (ThreadContext *context)
223 MonoJitTlsData *jit_tls;
225 mono_native_tls_set_value (thread_context_id, context);
226 jit_tls = mono_tls_get_jit_tls ();
228 jit_tls->interp_context = context;
232 ves_real_abort (int line, MonoMethod *mh,
233 const unsigned short *ip, stackval *stack, stackval *sp)
236 fprintf (stderr, "Execution aborted in method: %s::%s\n", mh->klass->name, mh->name);
237 fprintf (stderr, "Line=%d IP=0x%04lx, Aborted execution\n", line,
238 ip-(const unsigned short *)mono_method_get_header_checked (mh, &error)->code);
239 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
240 g_print ("0x%04x %02x\n",
241 ip-(const unsigned short *)mono_method_get_header_checked (mh, &error)->code, *ip);
242 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
244 printf ("\t[%ld] 0x%08x %0.5f\n", sp-stack, sp[-1].data.i, sp[-1].data.f);
247 #define ves_abort() \
249 ves_real_abort(__LINE__, frame->runtime_method->method, ip, frame->stack, sp); \
250 THROW_EX (mono_get_exception_execution_engine (NULL), ip); \
253 static RuntimeMethod*
254 lookup_runtime_method (MonoDomain *domain, MonoMethod *method)
257 MonoJitDomainInfo *info;
259 info = domain_jit_info (domain);
260 mono_domain_jit_code_hash_lock (domain);
261 rtm = mono_internal_hash_table_lookup (&info->interp_code_hash, method);
262 mono_domain_jit_code_hash_unlock (domain);
267 mono_interp_get_runtime_method (MonoDomain *domain, MonoMethod *method, MonoError *error)
270 MonoJitDomainInfo *info;
271 MonoMethodSignature *sig;
276 info = domain_jit_info (domain);
277 mono_domain_jit_code_hash_lock (domain);
278 rtm = mono_internal_hash_table_lookup (&info->interp_code_hash, method);
279 mono_domain_jit_code_hash_unlock (domain);
283 sig = mono_method_signature (method);
285 rtm = mono_domain_alloc0 (domain, sizeof (RuntimeMethod));
286 rtm->method = method;
287 rtm->domain = domain;
288 rtm->param_count = sig->param_count;
289 rtm->hasthis = sig->hasthis;
290 rtm->rtype = mini_get_underlying_type (sig->ret);
291 rtm->param_types = mono_domain_alloc0 (domain, sizeof (MonoType*) * sig->param_count);
292 for (i = 0; i < sig->param_count; ++i)
293 rtm->param_types [i] = mini_get_underlying_type (sig->params [i]);
295 mono_domain_jit_code_hash_lock (domain);
296 if (!mono_internal_hash_table_lookup (&info->interp_code_hash, method))
297 mono_internal_hash_table_insert (&info->interp_code_hash, method, rtm);
298 mono_domain_jit_code_hash_unlock (domain);
304 mono_interp_create_trampoline (MonoDomain *domain, MonoMethod *method, MonoError *error)
306 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
307 method = mono_marshal_get_synchronized_wrapper (method);
308 return mono_interp_get_runtime_method (domain, method, error);
314 * Push an LMF frame on the LMF stack
315 * to mark the transition to native code.
316 * This is needed for the native code to
317 * be able to do stack walks.
320 interp_push_lmf (MonoLMFExt *ext, MonoInvocation *frame)
322 memset (ext, 0, sizeof (MonoLMFExt));
323 ext->interp_exit = TRUE;
324 ext->interp_exit_data = frame;
330 interp_pop_lmf (MonoLMFExt *ext)
332 mono_pop_lmf (&ext->lmf);
335 static inline RuntimeMethod*
336 get_virtual_method (RuntimeMethod *runtime_method, MonoObject *obj)
338 MonoMethod *m = runtime_method->method;
339 MonoDomain *domain = runtime_method->domain;
340 RuntimeMethod *ret = NULL;
343 #ifndef DISABLE_REMOTING
344 if (mono_object_is_transparent_proxy (obj)) {
345 ret = mono_interp_get_runtime_method (domain, mono_marshal_get_remoting_invoke (m), &error);
346 mono_error_assert_ok (&error);
351 if ((m->flags & METHOD_ATTRIBUTE_FINAL) || !(m->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
352 if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
353 ret = mono_interp_get_runtime_method (domain, mono_marshal_get_synchronized_wrapper (m), &error);
354 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
356 ret = runtime_method;
361 mono_class_setup_vtable (obj->vtable->klass);
363 int slot = mono_method_get_vtable_slot (m);
364 if (mono_class_is_interface (m->klass)) {
365 g_assert (obj->vtable->klass != m->klass);
366 /* TODO: interface offset lookup is slow, go through IMT instead */
367 gboolean non_exact_match;
368 slot += mono_class_interface_offset_with_variance (obj->vtable->klass, m->klass, &non_exact_match);
371 MonoMethod *virtual_method = obj->vtable->klass->vtable [slot];
372 if (m->is_inflated && mono_method_get_context (m)->method_inst) {
373 MonoGenericContext context = { NULL, NULL };
375 if (mono_class_is_ginst (virtual_method->klass))
376 context.class_inst = mono_class_get_generic_class (virtual_method->klass)->context.class_inst;
377 else if (mono_class_is_gtd (virtual_method->klass))
378 context.class_inst = mono_class_get_generic_container (virtual_method->klass)->context.class_inst;
379 context.method_inst = mono_method_get_context (m)->method_inst;
381 virtual_method = mono_class_inflate_generic_method_checked (virtual_method, &context, &error);
382 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
385 if (virtual_method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
386 virtual_method = mono_marshal_get_synchronized_wrapper (virtual_method);
389 RuntimeMethod *virtual_runtime_method = mono_interp_get_runtime_method (domain, virtual_method, &error);
390 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
391 return virtual_runtime_method;
395 stackval_from_data (MonoType *type, stackval *result, char *data, gboolean pinvoke)
398 switch (type->type) {
399 case MONO_TYPE_OBJECT:
400 case MONO_TYPE_CLASS:
401 case MONO_TYPE_STRING:
402 case MONO_TYPE_ARRAY:
403 case MONO_TYPE_SZARRAY:
408 result->data.p = *(gpointer*)data;
411 switch (type->type) {
415 result->data.i = *(gint8*)data;
418 case MONO_TYPE_BOOLEAN:
419 result->data.i = *(guint8*)data;
422 result->data.i = *(gint16*)data;
426 result->data.i = *(guint16*)data;
429 result->data.i = *(gint32*)data;
433 result->data.nati = *(mono_i*)data;
436 result->data.p = *(gpointer*)data;
439 result->data.i = *(guint32*)data;
443 /* memmove handles unaligned case */
444 memmove (&tmp, data, sizeof (float));
445 result->data.f = tmp;
450 memmove (&result->data.l, data, sizeof (gint64));
453 memmove (&result->data.f, data, sizeof (double));
455 case MONO_TYPE_STRING:
456 case MONO_TYPE_SZARRAY:
457 case MONO_TYPE_CLASS:
458 case MONO_TYPE_OBJECT:
459 case MONO_TYPE_ARRAY:
460 result->data.p = *(gpointer*)data;
462 case MONO_TYPE_VALUETYPE:
463 if (type->data.klass->enumtype) {
464 stackval_from_data (mono_class_enum_basetype (type->data.klass), result, data, pinvoke);
467 mono_value_copy (result->data.vt, data, type->data.klass);
469 case MONO_TYPE_GENERICINST: {
470 if (mono_type_generic_inst_is_valuetype (type)) {
471 mono_value_copy (result->data.vt, data, mono_class_from_mono_type (type));
474 stackval_from_data (&type->data.generic_class->container_class->byval_arg, result, data, pinvoke);
478 g_warning ("got type 0x%02x", type->type);
479 g_assert_not_reached ();
484 stackval_to_data (MonoType *type, stackval *val, char *data, gboolean pinvoke)
487 gpointer *p = (gpointer*)data;
491 /* printf ("TODAT0 %p\n", data); */
492 switch (type->type) {
495 guint8 *p = (guint8*)data;
499 case MONO_TYPE_BOOLEAN: {
500 guint8 *p = (guint8*)data;
501 *p = (val->data.i != 0);
506 case MONO_TYPE_CHAR: {
507 guint16 *p = (guint16*)data;
512 mono_i *p = (mono_i*)data;
513 /* In theory the value used by stloc should match the local var type
514 but in practice it sometimes doesn't (a int32 gets dup'd and stloc'd into
515 a native int - both by csc and mcs). Not sure what to do about sign extension
516 as it is outside the spec... doing the obvious */
517 *p = (mono_i)val->data.nati;
521 mono_u *p = (mono_u*)data;
523 *p = (mono_u)val->data.nati;
528 gint32 *p = (gint32*)data;
534 gint64 *p = (gint64*)data;
539 float *p = (float*)data;
544 double *p = (double*)data;
548 case MONO_TYPE_STRING:
549 case MONO_TYPE_SZARRAY:
550 case MONO_TYPE_CLASS:
551 case MONO_TYPE_OBJECT:
552 case MONO_TYPE_ARRAY: {
553 gpointer *p = (gpointer *) data;
554 mono_gc_wbarrier_generic_store (p, val->data.p);
557 case MONO_TYPE_PTR: {
558 gpointer *p = (gpointer *) data;
562 case MONO_TYPE_VALUETYPE:
563 if (type->data.klass->enumtype) {
564 stackval_to_data (mono_class_enum_basetype (type->data.klass), val, data, pinvoke);
567 mono_value_copy (data, val->data.vt, type->data.klass);
569 case MONO_TYPE_GENERICINST: {
570 MonoClass *container_class = type->data.generic_class->container_class;
572 if (container_class->valuetype && !container_class->enumtype) {
573 mono_value_copy (data, val->data.vt, mono_class_from_mono_type (type));
576 stackval_to_data (&type->data.generic_class->container_class->byval_arg, val, data, pinvoke);
580 g_warning ("got type %x", type->type);
581 g_assert_not_reached ();
586 fill_in_trace (MonoException *exception, MonoInvocation *frame)
589 char *stack_trace = dump_frame (frame);
590 MonoDomain *domain = frame->runtime_method->domain;
591 (exception)->stack_trace = mono_string_new_checked (domain, stack_trace, &error);
592 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
593 (exception)->trace_ips = get_trace_ips (domain, frame);
594 g_free (stack_trace);
597 #define FILL_IN_TRACE(exception, frame) fill_in_trace(exception, frame)
599 #define THROW_EX(exception,ex_ip) \
601 frame->ip = (ex_ip); \
602 frame->ex = (MonoException*)(exception); \
603 FILL_IN_TRACE(frame->ex, frame); \
604 goto handle_exception; \
608 ves_array_create (MonoInvocation *frame, MonoDomain *domain, MonoClass *klass, MonoMethodSignature *sig, stackval *values)
611 intptr_t *lower_bounds;
616 lengths = alloca (sizeof (uintptr_t) * klass->rank * 2);
617 for (i = 0; i < sig->param_count; ++i) {
618 lengths [i] = values->data.i;
621 if (klass->rank == sig->param_count) {
622 /* Only lengths provided. */
625 /* lower bounds are first. */
626 lower_bounds = (intptr_t *) lengths;
627 lengths += klass->rank;
629 obj = (MonoObject*) mono_array_new_full_checked (domain, klass, lengths, lower_bounds, &error);
630 if (!mono_error_ok (&error)) {
631 frame->ex = mono_error_convert_to_exception (&error);
632 FILL_IN_TRACE (frame->ex, frame);
634 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
639 ves_array_calculate_index (MonoArray *ao, stackval *sp, MonoInvocation *frame, gboolean safe)
641 g_assert (!frame->ex);
642 MonoClass *ac = ((MonoObject *) ao)->vtable->klass;
646 for (gint32 i = 0; i < ac->rank; i++) {
647 guint32 idx = sp [i].data.i;
648 guint32 lower = ao->bounds [i].lower_bound;
649 guint32 len = ao->bounds [i].length;
650 if (safe && (idx < lower || (idx - lower) >= len)) {
651 frame->ex = mono_get_exception_index_out_of_range ();
652 FILL_IN_TRACE (frame->ex, frame);
655 pos = (pos * len) + idx - lower;
659 if (safe && pos >= ao->max_length) {
660 frame->ex = mono_get_exception_index_out_of_range ();
661 FILL_IN_TRACE (frame->ex, frame);
669 ves_array_set (MonoInvocation *frame)
671 stackval *sp = frame->stack_args + 1;
673 MonoObject *o = frame->stack_args->data.p;
674 MonoArray *ao = (MonoArray *) o;
675 MonoClass *ac = o->vtable->klass;
677 g_assert (ac->rank >= 1);
679 gint32 pos = ves_array_calculate_index (ao, sp, frame, TRUE);
683 if (sp [ac->rank].data.p && !mono_object_class (o)->element_class->valuetype) {
685 MonoObject *isinst = mono_object_isinst_checked (sp [ac->rank].data.p, mono_object_class (o)->element_class, &error);
686 mono_error_cleanup (&error);
688 frame->ex = mono_get_exception_array_type_mismatch ();
689 FILL_IN_TRACE (frame->ex, frame);
694 gint32 esize = mono_array_element_size (ac);
695 gpointer ea = mono_array_addr_with_size (ao, esize, pos);
697 MonoType *mt = mono_method_signature (frame->runtime_method->method)->params [ac->rank];
698 stackval_to_data (mt, &sp [ac->rank], ea, FALSE);
702 ves_array_get (MonoInvocation *frame, gboolean safe)
704 stackval *sp = frame->stack_args + 1;
706 MonoObject *o = frame->stack_args->data.p;
707 MonoArray *ao = (MonoArray *) o;
708 MonoClass *ac = o->vtable->klass;
710 g_assert (ac->rank >= 1);
712 gint32 pos = ves_array_calculate_index (ao, sp, frame, safe);
716 gint32 esize = mono_array_element_size (ac);
717 gpointer ea = mono_array_addr_with_size (ao, esize, pos);
719 MonoType *mt = mono_method_signature (frame->runtime_method->method)->ret;
720 stackval_from_data (mt, frame->retval, ea, FALSE);
724 ves_array_element_address (MonoInvocation *frame, MonoClass *required_type, MonoArray *ao, stackval *sp, gboolean needs_typecheck)
726 MonoClass *ac = ((MonoObject *) ao)->vtable->klass;
728 g_assert (ac->rank >= 1);
730 gint32 pos = ves_array_calculate_index (ao, sp, frame, TRUE);
734 if (needs_typecheck && !mono_class_is_assignable_from (mono_object_class ((MonoObject *) ao)->element_class, required_type->element_class)) {
735 frame->ex = mono_get_exception_array_type_mismatch ();
736 FILL_IN_TRACE (frame->ex, frame);
739 gint32 esize = mono_array_element_size (ac);
740 return mono_array_addr_with_size (ao, esize, pos);
744 interp_walk_stack_with_ctx (MonoInternalStackWalk func, MonoContext *ctx, MonoUnwindOptions options, void *user_data)
747 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
752 MonoInvocation *frame = context->current_frame;
755 MonoStackFrameInfo fi;
756 memset (&fi, 0, sizeof (MonoStackFrameInfo));
758 /* TODO: hack to make some asserts happy. */
759 fi.ji = (MonoJitInfo *) frame->runtime_method;
761 if (frame->runtime_method)
762 fi.method = fi.actual_method = frame->runtime_method->method;
764 if (!fi.method || (fi.method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) || (fi.method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
766 fi.type = FRAME_TYPE_MANAGED_TO_NATIVE;
768 MonoMethodHeader *hd = mono_method_get_header_checked (fi.method, &error);
769 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
770 fi.type = FRAME_TYPE_MANAGED;
771 fi.il_offset = frame->ip - (const unsigned short *) hd->code;
772 if (!fi.method->wrapper_type)
776 if (func (&fi, ctx, user_data))
778 frame = frame->parent;
782 static MonoPIFunc mono_interp_enter_icall_trampoline = NULL;
784 static InterpMethodArguments* build_args_from_sig (MonoMethodSignature *sig, MonoInvocation *frame)
786 InterpMethodArguments *margs = g_malloc0 (sizeof (InterpMethodArguments));
789 g_assert (mono_arm_eabi_supported ());
790 int i8_align = mono_arm_i8_align ();
796 for (int i = 0; i < sig->param_count; i++) {
797 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
799 case MONO_TYPE_BOOLEAN:
810 case MONO_TYPE_SZARRAY:
811 case MONO_TYPE_CLASS:
812 case MONO_TYPE_OBJECT:
813 case MONO_TYPE_STRING:
814 case MONO_TYPE_VALUETYPE:
815 case MONO_TYPE_GENERICINST:
816 #if SIZEOF_VOID_P == 8
821 #if SIZEOF_VOID_P == 4
824 /* pairs begin at even registers */
825 if (i8_align == 8 && margs->ilen & 1)
832 #if SIZEOF_VOID_P == 8
837 #if SIZEOF_VOID_P == 4
843 g_error ("build_args_from_sig: not implemented yet (1): 0x%x\n", ptype);
848 margs->iargs = g_malloc0 (sizeof (gpointer) * margs->ilen);
851 margs->fargs = g_malloc0 (sizeof (double) * margs->flen);
853 if (margs->ilen > INTERP_ICALL_TRAMP_IARGS)
854 g_error ("build_args_from_sig: TODO, allocate gregs: %d\n", margs->ilen);
856 if (margs->flen > INTERP_ICALL_TRAMP_FARGS)
857 g_error ("build_args_from_sig: TODO, allocate fregs: %d\n", margs->flen);
864 margs->iargs [0] = frame->stack_args->data.p;
868 for (int i = 0; i < sig->param_count; i++) {
869 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
871 case MONO_TYPE_BOOLEAN:
882 case MONO_TYPE_SZARRAY:
883 case MONO_TYPE_CLASS:
884 case MONO_TYPE_OBJECT:
885 case MONO_TYPE_STRING:
886 case MONO_TYPE_VALUETYPE:
887 case MONO_TYPE_GENERICINST:
888 #if SIZEOF_VOID_P == 8
891 margs->iargs [int_i] = frame->stack_args [i].data.p;
893 g_print ("build_args_from_sig: margs->iargs [%d]: %p (frame @ %d)\n", int_i, margs->iargs [int_i], i);
897 #if SIZEOF_VOID_P == 4
899 stackval *sarg = &frame->stack_args [i];
901 /* pairs begin at even registers */
902 if (i8_align == 8 && int_i & 1)
905 margs->iargs [int_i] = (gpointer) sarg->data.pair.lo;
907 margs->iargs [int_i] = (gpointer) sarg->data.pair.hi;
909 g_print ("build_args_from_sig: margs->iargs [%d/%d]: 0x%016llx, hi=0x%08x lo=0x%08x (frame @ %d)\n", int_i - 1, int_i, *((guint64 *) &margs->iargs [int_i - 1]), sarg->data.pair.hi, sarg->data.pair.lo, i);
917 if (ptype == MONO_TYPE_R4)
918 * (float *) &(margs->fargs [int_f]) = (float) frame->stack_args [i].data.f;
920 margs->fargs [int_f] = frame->stack_args [i].data.f;
922 g_print ("build_args_from_sig: margs->fargs [%d]: %p (%f) (frame @ %d)\n", int_f, margs->fargs [int_f], margs->fargs [int_f], i);
924 #if SIZEOF_VOID_P == 4
931 g_error ("build_args_from_sig: not implemented yet (2): 0x%x\n", ptype);
935 switch (sig->ret->type) {
936 case MONO_TYPE_BOOLEAN:
947 case MONO_TYPE_SZARRAY:
948 case MONO_TYPE_CLASS:
949 case MONO_TYPE_OBJECT:
950 case MONO_TYPE_STRING:
952 case MONO_TYPE_VALUETYPE:
953 case MONO_TYPE_GENERICINST:
954 margs->retval = &(frame->retval->data.p);
955 margs->is_float_ret = 0;
959 margs->retval = &(frame->retval->data.p);
960 margs->is_float_ret = 1;
963 margs->retval = NULL;
966 g_error ("build_args_from_sig: ret type not implemented yet: 0x%x\n", sig->ret->type);
973 ves_pinvoke_method (MonoInvocation *frame, MonoMethodSignature *sig, MonoFuncV addr, gboolean string_ctor, ThreadContext *context)
976 MonoInvocation *old_frame = context->current_frame;
977 MonoInvocation *old_env_frame = context->env_frame;
978 jmp_buf *old_env = context->current_env;
982 context->current_frame = old_frame;
983 context->env_frame = old_env_frame;
984 context->current_env = old_env;
985 context->managed_code = 1;
990 context->env_frame = frame;
991 context->current_env = &env;
993 g_assert (!frame->runtime_method);
994 if (!mono_interp_enter_icall_trampoline) {
996 mono_interp_enter_icall_trampoline = mono_arch_get_enter_icall_trampoline (&info);
998 // mono_tramp_info_register (info, NULL);
1001 InterpMethodArguments *margs = build_args_from_sig (sig, frame);
1003 g_print ("ICALL: mono_interp_enter_icall_trampoline = %p, addr = %p\n", mono_interp_enter_icall_trampoline, addr);
1004 g_print ("margs(out): ilen=%d, flen=%d\n", margs->ilen, margs->flen);
1007 context->current_frame = frame;
1008 context->managed_code = 0;
1010 interp_push_lmf (&ext, frame);
1012 mono_interp_enter_icall_trampoline (addr, margs);
1014 interp_pop_lmf (&ext);
1016 context->managed_code = 1;
1018 if (*mono_thread_interruption_request_flag ()) {
1019 MonoException *exc = mono_thread_interruption_checkpoint ();
1022 context->search_for_handler = 1;
1026 if (!frame->ex && !MONO_TYPE_ISSTRUCT (sig->ret))
1027 stackval_from_data (sig->ret, frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
1029 context->current_frame = old_frame;
1030 context->env_frame = old_env_frame;
1031 context->current_env = old_env;
1033 g_free (margs->iargs);
1034 g_free (margs->fargs);
1039 mono_interp_init_delegate (MonoDelegate *del)
1043 /* shouldn't need a write barrier because we don't write a MonoObject into the field */
1044 del->method = ((RuntimeMethod *) del->method_ptr)->method;
1049 * runtime specifies that the implementation of the method is automatically
1050 * provided by the runtime and is primarily used for the methods of delegates.
1053 ves_runtime_method (MonoInvocation *frame, ThreadContext *context)
1055 MonoMethod *method = frame->runtime_method->method;
1056 const char *name = method->name;
1057 MonoObject *obj = (MonoObject*) frame->stack_args->data.p;
1058 MonoObject *isinst_obj;
1061 mono_class_init (method->klass);
1063 if (method->klass == mono_defaults.array_class) {
1064 if (!strcmp (method->name, "UnsafeMov")) {
1065 /* TODO: layout checks */
1066 MonoType *mt = mono_method_signature (method)->ret;
1067 stackval_from_data (mt, frame->retval, (char *) frame->stack_args, FALSE);
1070 if (!strcmp (method->name, "UnsafeLoad")) {
1071 ves_array_get (frame, FALSE);
1076 isinst_obj = mono_object_isinst_checked (obj, mono_defaults.array_class, &error);
1077 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1078 if (obj && isinst_obj) {
1079 if (*name == 'S' && (strcmp (name, "Set") == 0)) {
1080 ves_array_set (frame);
1083 if (*name == 'G' && (strcmp (name, "Get") == 0)) {
1084 ves_array_get (frame, TRUE);
1089 g_error ("Don't know how to exec runtime method %s.%s::%s",
1090 method->klass->name_space, method->klass->name,
1096 dump_stack (stackval *stack, stackval *sp)
1098 stackval *s = stack;
1099 GString *str = g_string_new ("");
1102 return g_string_free (str, FALSE);
1105 g_string_append_printf (str, "[%p (%lld)] ", s->data.l, s->data.l);
1108 return g_string_free (str, FALSE);
1113 dump_stackval (GString *str, stackval *s, MonoType *type)
1115 switch (type->type) {
1122 case MONO_TYPE_CHAR:
1123 case MONO_TYPE_BOOLEAN:
1124 g_string_append_printf (str, "[%d] ", s->data.i);
1126 case MONO_TYPE_STRING:
1127 case MONO_TYPE_SZARRAY:
1128 case MONO_TYPE_CLASS:
1129 case MONO_TYPE_OBJECT:
1130 case MONO_TYPE_ARRAY:
1134 g_string_append_printf (str, "[%p] ", s->data.p);
1136 case MONO_TYPE_VALUETYPE:
1137 if (type->data.klass->enumtype)
1138 g_string_append_printf (str, "[%d] ", s->data.i);
1140 g_string_append_printf (str, "[vt:%p] ", s->data.p);
1144 g_string_append_printf (str, "[%g] ", s->data.f);
1149 GString *res = g_string_new ("");
1150 mono_type_get_desc (res, type, TRUE);
1151 g_string_append_printf (str, "[{%s} %lld/0x%0llx] ", res->str, s->data.l, s->data.l);
1152 g_string_free (res, TRUE);
1160 dump_retval (MonoInvocation *inv)
1162 GString *str = g_string_new ("");
1163 MonoType *ret = mono_method_signature (inv->runtime_method->method)->ret;
1165 if (ret->type != MONO_TYPE_VOID)
1166 dump_stackval (str, inv->retval, ret);
1168 return g_string_free (str, FALSE);
1173 dump_args (MonoInvocation *inv)
1175 GString *str = g_string_new ("");
1177 MonoMethodSignature *signature = mono_method_signature (inv->runtime_method->method);
1179 if (signature->param_count == 0 && !signature->hasthis)
1180 return g_string_free (str, FALSE);
1182 if (signature->hasthis) {
1183 MonoMethod *method = inv->runtime_method->method;
1184 dump_stackval (str, inv->stack_args, &method->klass->byval_arg);
1187 for (i = 0; i < signature->param_count; ++i)
1188 dump_stackval (str, inv->stack_args + (!!signature->hasthis) + i, signature->params [i]);
1190 return g_string_free (str, FALSE);
1194 dump_frame (MonoInvocation *inv)
1196 GString *str = g_string_new ("");
1201 for (i = 0; inv; inv = inv->parent) {
1202 if (inv->runtime_method != NULL) {
1203 MonoMethod *method = inv->runtime_method->method;
1207 const char * opname = "";
1209 gchar *source = NULL;
1213 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
1214 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) == 0) {
1215 MonoMethodHeader *hd = mono_method_get_header_checked (method, &error);
1216 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1220 opname = mono_interp_opname [*inv->ip];
1221 codep = inv->ip - inv->runtime_method->code;
1222 source = g_strdup_printf ("%s:%d // (TODO: proper stacktrace)", method->name, codep);
1227 MonoDebugSourceLocation *minfo = mono_debug_lookup_method (method);
1228 source = mono_debug_method_lookup_location (minfo, codep);
1232 args = dump_args (inv);
1233 name = mono_method_full_name (method, TRUE);
1235 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s) at %s\n", i, codep, opname, name, args, source);
1237 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s)\n", i, codep, opname, name, args);
1244 return g_string_free (str, FALSE);
1248 get_trace_ips (MonoDomain *domain, MonoInvocation *top)
1252 MonoInvocation *inv;
1255 for (i = 0, inv = top; inv; inv = inv->parent)
1256 if (inv->runtime_method != NULL)
1259 res = mono_array_new_checked (domain, mono_defaults.int_class, 2 * i, &error);
1260 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1262 for (i = 0, inv = top; inv; inv = inv->parent)
1263 if (inv->runtime_method != NULL) {
1264 mono_array_set (res, gpointer, i, inv->runtime_method);
1266 mono_array_set (res, gpointer, i, (gpointer)inv->ip);
1274 #define MYGUINT64_MAX 18446744073709551615ULL
1275 #define MYGINT64_MAX 9223372036854775807LL
1276 #define MYGINT64_MIN (-MYGINT64_MAX -1LL)
1278 #define MYGUINT32_MAX 4294967295U
1279 #define MYGINT32_MAX 2147483647
1280 #define MYGINT32_MIN (-MYGINT32_MAX -1)
1282 #define CHECK_ADD_OVERFLOW(a,b) \
1283 (gint32)(b) >= 0 ? (gint32)(MYGINT32_MAX) - (gint32)(b) < (gint32)(a) ? -1 : 0 \
1284 : (gint32)(MYGINT32_MIN) - (gint32)(b) > (gint32)(a) ? +1 : 0
1286 #define CHECK_SUB_OVERFLOW(a,b) \
1287 (gint32)(b) < 0 ? (gint32)(MYGINT32_MAX) + (gint32)(b) < (gint32)(a) ? -1 : 0 \
1288 : (gint32)(MYGINT32_MIN) + (gint32)(b) > (gint32)(a) ? +1 : 0
1290 #define CHECK_ADD_OVERFLOW_UN(a,b) \
1291 (guint32)(MYGUINT32_MAX) - (guint32)(b) < (guint32)(a) ? -1 : 0
1293 #define CHECK_SUB_OVERFLOW_UN(a,b) \
1294 (guint32)(a) < (guint32)(b) ? -1 : 0
1296 #define CHECK_ADD_OVERFLOW64(a,b) \
1297 (gint64)(b) >= 0 ? (gint64)(MYGINT64_MAX) - (gint64)(b) < (gint64)(a) ? -1 : 0 \
1298 : (gint64)(MYGINT64_MIN) - (gint64)(b) > (gint64)(a) ? +1 : 0
1300 #define CHECK_SUB_OVERFLOW64(a,b) \
1301 (gint64)(b) < 0 ? (gint64)(MYGINT64_MAX) + (gint64)(b) < (gint64)(a) ? -1 : 0 \
1302 : (gint64)(MYGINT64_MIN) + (gint64)(b) > (gint64)(a) ? +1 : 0
1304 #define CHECK_ADD_OVERFLOW64_UN(a,b) \
1305 (guint64)(MYGUINT64_MAX) - (guint64)(b) < (guint64)(a) ? -1 : 0
1307 #define CHECK_SUB_OVERFLOW64_UN(a,b) \
1308 (guint64)(a) < (guint64)(b) ? -1 : 0
1310 #if SIZEOF_VOID_P == 4
1311 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW(a,b)
1312 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW_UN(a,b)
1314 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW64(a,b)
1315 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW64_UN(a,b)
1318 /* Resolves to TRUE if the operands would overflow */
1319 #define CHECK_MUL_OVERFLOW(a,b) \
1320 ((gint32)(a) == 0) || ((gint32)(b) == 0) ? 0 : \
1321 (((gint32)(a) > 0) && ((gint32)(b) == -1)) ? FALSE : \
1322 (((gint32)(a) < 0) && ((gint32)(b) == -1)) ? (a == - MYGINT32_MAX) : \
1323 (((gint32)(a) > 0) && ((gint32)(b) > 0)) ? (gint32)(a) > ((MYGINT32_MAX) / (gint32)(b)) : \
1324 (((gint32)(a) > 0) && ((gint32)(b) < 0)) ? (gint32)(a) > ((MYGINT32_MIN) / (gint32)(b)) : \
1325 (((gint32)(a) < 0) && ((gint32)(b) > 0)) ? (gint32)(a) < ((MYGINT32_MIN) / (gint32)(b)) : \
1326 (gint32)(a) < ((MYGINT32_MAX) / (gint32)(b))
1328 #define CHECK_MUL_OVERFLOW_UN(a,b) \
1329 ((guint32)(a) == 0) || ((guint32)(b) == 0) ? 0 : \
1330 (guint32)(b) > ((MYGUINT32_MAX) / (guint32)(a))
1332 #define CHECK_MUL_OVERFLOW64(a,b) \
1333 ((gint64)(a) == 0) || ((gint64)(b) == 0) ? 0 : \
1334 (((gint64)(a) > 0) && ((gint64)(b) == -1)) ? FALSE : \
1335 (((gint64)(a) < 0) && ((gint64)(b) == -1)) ? (a == - MYGINT64_MAX) : \
1336 (((gint64)(a) > 0) && ((gint64)(b) > 0)) ? (gint64)(a) > ((MYGINT64_MAX) / (gint64)(b)) : \
1337 (((gint64)(a) > 0) && ((gint64)(b) < 0)) ? (gint64)(a) > ((MYGINT64_MIN) / (gint64)(b)) : \
1338 (((gint64)(a) < 0) && ((gint64)(b) > 0)) ? (gint64)(a) < ((MYGINT64_MIN) / (gint64)(b)) : \
1339 (gint64)(a) < ((MYGINT64_MAX) / (gint64)(b))
1341 #define CHECK_MUL_OVERFLOW64_UN(a,b) \
1342 ((guint64)(a) == 0) || ((guint64)(b) == 0) ? 0 : \
1343 (guint64)(b) > ((MYGUINT64_MAX) / (guint64)(a))
1345 #if SIZEOF_VOID_P == 4
1346 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW(a,b)
1347 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW_UN(a,b)
1349 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW64(a,b)
1350 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW64_UN(a,b)
1354 mono_interp_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc, MonoError *error)
1356 MonoInvocation frame;
1357 ThreadContext * volatile context = mono_native_tls_get_value (thread_context_id);
1358 MonoObject *retval = NULL;
1359 MonoMethodSignature *sig = mono_method_signature (method);
1360 MonoClass *klass = mono_class_from_mono_type (sig->ret);
1361 int i, type, isobject = 0;
1365 ThreadContext context_struct;
1366 MonoInvocation *old_frame = NULL;
1376 if (context != &context_struct) {
1377 context->current_frame = old_frame;
1378 context->managed_code = 0;
1382 *exc = (MonoObject *)frame.ex;
1386 if (context == NULL) {
1387 context = &context_struct;
1388 memset (context, 0, sizeof (ThreadContext));
1389 context_struct.base_frame = &frame;
1390 context_struct.env_frame = &frame;
1391 context_struct.current_env = &env;
1392 set_context (context);
1395 old_frame = context->current_frame;
1397 MonoDomain *domain = mono_domain_get ();
1399 switch (sig->ret->type) {
1400 case MONO_TYPE_VOID:
1402 case MONO_TYPE_STRING:
1403 case MONO_TYPE_OBJECT:
1404 case MONO_TYPE_CLASS:
1405 case MONO_TYPE_ARRAY:
1406 case MONO_TYPE_SZARRAY:
1409 case MONO_TYPE_VALUETYPE:
1410 retval = mono_object_new_checked (domain, klass, error);
1411 ret = mono_object_unbox (retval);
1412 if (!sig->ret->data.klass->enumtype)
1413 result.data.vt = ret;
1415 result.data.vt = alloca (mono_class_instance_size (klass));
1417 case MONO_TYPE_GENERICINST:
1418 if (!MONO_TYPE_IS_REFERENCE (sig->ret)) {
1419 retval = mono_object_new_checked (domain, klass, error);
1420 ret = mono_object_unbox (retval);
1421 if (!sig->ret->data.klass->enumtype)
1422 result.data.vt = ret;
1424 result.data.vt = alloca (mono_class_instance_size (klass));
1431 retval = mono_object_new_checked (domain, mono_defaults.int_class, error);
1432 ret = mono_object_unbox (retval);
1435 retval = mono_object_new_checked (domain, klass, error);
1436 ret = mono_object_unbox (retval);
1440 args = alloca (sizeof (stackval) * (sig->param_count + !!sig->hasthis));
1442 args [0].data.p = obj;
1444 for (i = 0; i < sig->param_count; ++i) {
1445 int a_index = i + !!sig->hasthis;
1446 if (sig->params [i]->byref) {
1447 args [a_index].data.p = params [i];
1450 type = sig->params [i]->type;
1455 case MONO_TYPE_BOOLEAN:
1456 args [a_index].data.i = *(MonoBoolean*)params [i];
1460 case MONO_TYPE_CHAR:
1461 args [a_index].data.i = *(gint16*)params [i];
1463 #if SIZEOF_VOID_P == 4
1464 case MONO_TYPE_U: /* use VAL_POINTER? */
1469 args [a_index].data.i = *(gint32*)params [i];
1471 #if SIZEOF_VOID_P == 8
1477 args [a_index].data.l = *(gint64*)params [i];
1480 args [a_index].data.f = *(gfloat *) params [i];
1483 args [a_index].data.f = *(gdouble *) params [i];
1485 case MONO_TYPE_VALUETYPE:
1486 if (sig->params [i]->data.klass->enumtype) {
1487 type = mono_class_enum_basetype (sig->params [i]->data.klass)->type;
1490 args [a_index].data.p = params [i];
1493 case MONO_TYPE_STRING:
1495 case MONO_TYPE_CLASS:
1496 case MONO_TYPE_ARRAY:
1497 case MONO_TYPE_SZARRAY:
1498 case MONO_TYPE_OBJECT:
1499 case MONO_TYPE_GENERICINST:
1500 args [a_index].data.p = params [i];
1503 g_error ("type 0x%x not handled in runtime invoke", sig->params [i]->type);
1507 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
1508 method = mono_marshal_get_native_wrapper (method, FALSE, FALSE);
1510 INIT_FRAME (&frame,context->current_frame,args,&result,domain,method,error);
1512 frame.invoke_trap = 1;
1513 context->managed_code = 1;
1514 ves_exec_method_with_context (&frame, context, NULL, NULL, -1);
1515 context->managed_code = 0;
1516 if (context == &context_struct)
1519 context->current_frame = old_frame;
1520 if (frame.ex != NULL) {
1522 *exc = (MonoObject*) frame.ex;
1525 if (context->current_env != NULL) {
1526 context->env_frame->ex = frame.ex;
1527 longjmp(*context->current_env, 1);
1530 printf("dropped exception...\n");
1532 if (sig->ret->type == MONO_TYPE_VOID && !method->string_ctor)
1534 if (isobject || method->string_ctor)
1535 return result.data.p;
1536 stackval_to_data (sig->ret, &result, ret, sig->pinvoke);
1541 RuntimeMethod *rmethod;
1545 gpointer *many_args;
1548 /* Main function for entering the interpreter from compiled code */
1550 interp_entry (InterpEntryData *data)
1552 MonoInvocation frame;
1553 RuntimeMethod *rmethod = data->rmethod;
1554 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
1555 ThreadContext context_struct;
1556 MonoInvocation *old_frame;
1560 MonoMethodSignature *sig;
1564 method = rmethod->method;
1565 sig = mono_method_signature (method);
1567 // FIXME: Optimize this
1569 //printf ("%s\n", mono_method_full_name (method, 1));
1572 if (context == NULL) {
1573 context = &context_struct;
1574 memset (context, 0, sizeof (ThreadContext));
1575 context_struct.base_frame = &frame;
1576 context_struct.env_frame = &frame;
1577 set_context (context);
1579 old_frame = context->current_frame;
1582 args = alloca (sizeof (stackval) * (sig->param_count + (sig->hasthis ? 1 : 0)));
1584 args [0].data.p = data->this_arg;
1587 if (data->many_args)
1588 params = data->many_args;
1590 params = data->args;
1591 for (i = 0; i < sig->param_count; ++i) {
1592 int a_index = i + (sig->hasthis ? 1 : 0);
1593 if (sig->params [i]->byref) {
1594 args [a_index].data.p = params [i];
1597 type = rmethod->param_types [i];
1598 switch (type->type) {
1601 args [a_index].data.i = *(MonoBoolean*)params [i];
1605 args [a_index].data.i = *(gint16*)params [i];
1608 #if SIZEOF_VOID_P == 4
1609 args [a_index].data.p = GINT_TO_POINTER (*(guint32*)params [i]);
1611 args [a_index].data.p = GINT_TO_POINTER (*(guint64*)params [i]);
1615 #if SIZEOF_VOID_P == 4
1616 args [a_index].data.p = GINT_TO_POINTER (*(gint32*)params [i]);
1618 args [a_index].data.p = GINT_TO_POINTER (*(gint64*)params [i]);
1622 args [a_index].data.i = *(guint32*)params [i];
1625 args [a_index].data.i = *(gint32*)params [i];
1628 args [a_index].data.l = *(guint64*)params [i];
1631 args [a_index].data.l = *(gint64*)params [i];
1634 case MONO_TYPE_OBJECT:
1635 args [a_index].data.p = *(MonoObject**)params [i];
1637 case MONO_TYPE_VALUETYPE:
1638 args [a_index].data.p = params [i];
1640 case MONO_TYPE_GENERICINST:
1641 if (MONO_TYPE_IS_REFERENCE (type))
1642 args [a_index].data.p = params [i];
1644 args [a_index].data.vt = params [i];
1647 printf ("%s\n", mono_type_full_name (sig->params [i]));
1653 init_frame (&frame, NULL, data->rmethod, args, &result);
1654 context->managed_code = 1;
1656 type = rmethod->rtype;
1657 switch (type->type) {
1658 case MONO_TYPE_GENERICINST:
1659 if (!MONO_TYPE_IS_REFERENCE (type))
1660 frame.retval->data.vt = data->res;
1662 case MONO_TYPE_VALUETYPE:
1663 frame.retval->data.vt = data->res;
1669 ves_exec_method_with_context (&frame, context, NULL, NULL, -1);
1670 context->managed_code = 0;
1671 if (context == &context_struct)
1674 context->current_frame = old_frame;
1677 g_assert (frame.ex == NULL);
1679 type = rmethod->rtype;
1680 switch (type->type) {
1681 case MONO_TYPE_VOID:
1684 *(gint8*)data->res = frame.retval->data.i;
1687 *(guint8*)data->res = frame.retval->data.i;
1690 *(gint16*)data->res = frame.retval->data.i;
1693 *(guint16*)data->res = frame.retval->data.i;
1696 *(gint32*)data->res = frame.retval->data.i;
1699 *(guint64*)data->res = frame.retval->data.i;
1702 *(gint64*)data->res = frame.retval->data.i;
1705 *(guint64*)data->res = frame.retval->data.i;
1708 #if SIZEOF_VOID_P == 8
1709 *(gint64*)data->res = (gint64)frame.retval->data.p;
1711 *(gint32*)data->res = (gint32)frame.retval->data.p;
1715 #if SIZEOF_VOID_P == 8
1716 *(guint64*)data->res = (guint64)frame.retval->data.p;
1718 *(guint32*)data->res = (guint32)frame.retval->data.p;
1721 case MONO_TYPE_OBJECT:
1722 /* No need for a write barrier */
1723 *(MonoObject**)data->res = (MonoObject*)frame.retval->data.p;
1725 case MONO_TYPE_GENERICINST:
1726 if (MONO_TYPE_IS_REFERENCE (type)) {
1727 *(MonoObject**)data->res = *(MonoObject**)frame.retval->data.p;
1729 /* Already set before the call */
1732 case MONO_TYPE_VALUETYPE:
1733 /* Already set before the call */
1736 printf ("%s\n", mono_type_full_name (sig->ret));
1743 do_icall (ThreadContext *context, int op, stackval *sp, gpointer ptr)
1745 MonoInvocation *old_frame = context->current_frame;
1746 MonoInvocation *old_env_frame = context->env_frame;
1747 jmp_buf *old_env = context->current_env;
1751 context->current_frame = old_frame;
1752 context->env_frame = old_env_frame;
1753 context->current_env = old_env;
1754 context->managed_code = 1;
1758 context->env_frame = context->current_frame;
1759 context->current_env = &env;
1760 context->managed_code = 0;
1763 case MINT_ICALL_V_V: {
1764 void (*func)(void) = ptr;
1768 case MINT_ICALL_V_P: {
1769 gpointer (*func)(void) = ptr;
1771 sp [-1].data.p = func ();
1774 case MINT_ICALL_P_V: {
1775 void (*func)(gpointer) = ptr;
1776 func (sp [-1].data.p);
1780 case MINT_ICALL_P_P: {
1781 gpointer (*func)(gpointer) = ptr;
1782 sp [-1].data.p = func (sp [-1].data.p);
1785 case MINT_ICALL_PP_V: {
1786 void (*func)(gpointer,gpointer) = ptr;
1788 func (sp [0].data.p, sp [1].data.p);
1791 case MINT_ICALL_PI_V: {
1792 void (*func)(gpointer,int) = ptr;
1794 func (sp [0].data.p, sp [1].data.i);
1797 case MINT_ICALL_PP_P: {
1798 gpointer (*func)(gpointer,gpointer) = ptr;
1800 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.p);
1803 case MINT_ICALL_PI_P: {
1804 gpointer (*func)(gpointer,int) = ptr;
1806 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.i);
1809 case MINT_ICALL_PPP_V: {
1810 void (*func)(gpointer,gpointer,gpointer) = ptr;
1812 func (sp [0].data.p, sp [1].data.p, sp [2].data.p);
1815 case MINT_ICALL_PPI_V: {
1816 void (*func)(gpointer,gpointer,int) = ptr;
1818 func (sp [0].data.p, sp [1].data.p, sp [2].data.i);
1822 g_assert_not_reached ();
1825 context->env_frame = old_env_frame;
1826 context->current_env = old_env;
1832 do_jit_call (stackval *sp, unsigned char *vt_sp, ThreadContext *context, MonoInvocation *frame, RuntimeMethod *rmethod)
1834 MonoMethodSignature *sig;
1835 MonoFtnDesc ftndesc;
1836 guint8 res_buf [256];
1840 //printf ("%s\n", mono_method_full_name (rmethod->method, 1));
1843 * Call JITted code through a gsharedvt_out wrapper. These wrappers receive every argument
1844 * by ref and return a return value using an explicit return value argument.
1846 if (!rmethod->jit_wrapper) {
1847 MonoMethod *method = rmethod->method;
1850 sig = mono_method_signature (method);
1853 MonoMethod *wrapper = mini_get_gsharedvt_out_sig_wrapper (sig);
1854 //printf ("J: %s %s\n", mono_method_full_name (method, 1), mono_method_full_name (wrapper, 1));
1856 gpointer jit_wrapper = mono_jit_compile_method_jit_only (wrapper, &error);
1857 mono_error_assert_ok (&error);
1859 gpointer addr = mono_jit_compile_method_jit_only (method, &error);
1861 mono_error_assert_ok (&error);
1863 rmethod->jit_addr = addr;
1864 rmethod->jit_sig = sig;
1865 mono_memory_barrier ();
1866 rmethod->jit_wrapper = jit_wrapper;
1869 sig = rmethod->jit_sig;
1872 sp -= sig->param_count;
1876 ftndesc.addr = rmethod->jit_addr;
1879 // FIXME: Optimize this
1883 int stack_index = 0;
1884 if (rmethod->hasthis) {
1885 args [pindex ++] = sp [0].data.p;
1888 type = rmethod->rtype;
1889 if (type->type != MONO_TYPE_VOID) {
1890 if (MONO_TYPE_ISSTRUCT (type))
1891 args [pindex ++] = vt_sp;
1893 args [pindex ++] = res_buf;
1895 for (int i = 0; i < rmethod->param_count; ++i) {
1896 MonoType *t = rmethod->param_types [i];
1897 stackval *sval = &sp [stack_index + i];
1898 if (sig->params [i]->byref) {
1899 args [pindex ++] = sval->data.p;
1900 } else if (MONO_TYPE_ISSTRUCT (t)) {
1901 args [pindex ++] = sval->data.p;
1902 } else if (MONO_TYPE_IS_REFERENCE (t)) {
1903 args [pindex ++] = &sval->data.p;
1912 case MONO_TYPE_VALUETYPE:
1913 args [pindex ++] = &sval->data.i;
1916 case MONO_TYPE_FNPTR:
1919 case MONO_TYPE_OBJECT:
1920 args [pindex ++] = &sval->data.p;
1924 args [pindex ++] = &sval->data.l;
1927 printf ("%s\n", mono_type_full_name (t));
1928 g_assert_not_reached ();
1933 interp_push_lmf (&ext, frame);
1937 void (*func)(gpointer) = rmethod->jit_wrapper;
1943 void (*func)(gpointer, gpointer) = rmethod->jit_wrapper;
1945 func (args [0], &ftndesc);
1949 void (*func)(gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
1951 func (args [0], args [1], &ftndesc);
1955 void (*func)(gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
1957 func (args [0], args [1], args [2], &ftndesc);
1961 void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
1963 func (args [0], args [1], args [2], args [3], &ftndesc);
1967 void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
1969 func (args [0], args [1], args [2], args [3], args [4], &ftndesc);
1973 void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
1975 func (args [0], args [1], args [2], args [3], args [4], args [5], &ftndesc);
1979 void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
1981 func (args [0], args [1], args [2], args [3], args [4], args [5], args [6], &ftndesc);
1985 g_assert_not_reached ();
1989 interp_pop_lmf (&ext);
1991 MonoType *rtype = rmethod->rtype;
1992 switch (rtype->type) {
1993 case MONO_TYPE_VOID:
1994 case MONO_TYPE_OBJECT:
1995 case MONO_TYPE_STRING:
1996 case MONO_TYPE_CLASS:
1997 case MONO_TYPE_ARRAY:
1998 case MONO_TYPE_SZARRAY:
2001 sp->data.p = *(gpointer*)res_buf;
2004 sp->data.i = *(gint8*)res_buf;
2007 sp->data.i = *(guint8*)res_buf;
2010 sp->data.i = *(gint16*)res_buf;
2013 sp->data.i = *(guint16*)res_buf;
2016 sp->data.i = *(gint32*)res_buf;
2019 sp->data.i = *(guint32*)res_buf;
2021 case MONO_TYPE_VALUETYPE:
2022 /* The result was written to vt_sp */
2025 case MONO_TYPE_GENERICINST:
2026 if (MONO_TYPE_IS_REFERENCE (rtype)) {
2027 sp->data.p = *(gpointer*)res_buf;
2029 /* The result was written to vt_sp */
2034 printf ("%s\n", mono_type_full_name (rtype));
2035 g_assert_not_reached ();
2043 do_debugger_tramp (void (*tramp) (void), MonoInvocation *frame)
2046 interp_push_lmf (&ext, frame);
2048 interp_pop_lmf (&ext);
2052 do_transform_method (MonoInvocation *frame, ThreadContext *context)
2056 /* Use the parent frame as the current frame is not complete yet */
2057 interp_push_lmf (&ext, frame->parent);
2059 frame->ex = mono_interp_transform_method (frame->runtime_method, context);
2060 context->managed_code = 1;
2062 interp_pop_lmf (&ext);
2066 * These functions are the entry points into the interpreter from compiled code.
2067 * They are called by the interp_in wrappers. They have the following signature:
2068 * void (<optional this_arg>, <optional retval pointer>, <arg1>, ..., <argn>, <method ptr>)
2069 * They pack up their arguments into an InterpEntryData structure and call interp_entry ().
2070 * It would be possible for the wrappers to pack up the arguments etc, but that would make them bigger, and there are
2071 * more wrappers then these functions.
2072 * this/static * ret/void * 16 arguments -> 64 functions.
2075 #define MAX_INTERP_ENTRY_ARGS 8
2077 #define INTERP_ENTRY_BASE(_method, _this_arg, _res) \
2078 InterpEntryData data; \
2079 (data).rmethod = (_method); \
2080 (data).res = (_res); \
2081 (data).this_arg = (_this_arg); \
2082 (data).many_args = NULL;
2084 #define INTERP_ENTRY0(_this_arg, _res, _method) { \
2085 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2086 interp_entry (&data); \
2088 #define INTERP_ENTRY1(_this_arg, _res, _method) { \
2089 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2090 (data).args [0] = arg1; \
2091 interp_entry (&data); \
2093 #define INTERP_ENTRY2(_this_arg, _res, _method) { \
2094 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2095 (data).args [0] = arg1; \
2096 (data).args [1] = arg2; \
2097 interp_entry (&data); \
2099 #define INTERP_ENTRY3(_this_arg, _res, _method) { \
2100 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2101 (data).args [0] = arg1; \
2102 (data).args [1] = arg2; \
2103 (data).args [2] = arg3; \
2104 interp_entry (&data); \
2106 #define INTERP_ENTRY4(_this_arg, _res, _method) { \
2107 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2108 (data).args [0] = arg1; \
2109 (data).args [1] = arg2; \
2110 (data).args [2] = arg3; \
2111 (data).args [3] = arg4; \
2112 interp_entry (&data); \
2114 #define INTERP_ENTRY5(_this_arg, _res, _method) { \
2115 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2116 (data).args [0] = arg1; \
2117 (data).args [1] = arg2; \
2118 (data).args [2] = arg3; \
2119 (data).args [3] = arg4; \
2120 (data).args [4] = arg5; \
2121 interp_entry (&data); \
2123 #define INTERP_ENTRY6(_this_arg, _res, _method) { \
2124 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2125 (data).args [0] = arg1; \
2126 (data).args [1] = arg2; \
2127 (data).args [2] = arg3; \
2128 (data).args [3] = arg4; \
2129 (data).args [4] = arg5; \
2130 (data).args [5] = arg6; \
2131 interp_entry (&data); \
2133 #define INTERP_ENTRY7(_this_arg, _res, _method) { \
2134 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2135 (data).args [0] = arg1; \
2136 (data).args [1] = arg2; \
2137 (data).args [2] = arg3; \
2138 (data).args [3] = arg4; \
2139 (data).args [4] = arg5; \
2140 (data).args [5] = arg6; \
2141 (data).args [6] = arg7; \
2142 interp_entry (&data); \
2144 #define INTERP_ENTRY8(_this_arg, _res, _method) { \
2145 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2146 (data).args [0] = arg1; \
2147 (data).args [1] = arg2; \
2148 (data).args [2] = arg3; \
2149 (data).args [3] = arg4; \
2150 (data).args [4] = arg5; \
2151 (data).args [5] = arg6; \
2152 (data).args [6] = arg7; \
2153 (data).args [7] = arg8; \
2154 interp_entry (&data); \
2157 #define ARGLIST0 RuntimeMethod *rmethod
2158 #define ARGLIST1 gpointer arg1, RuntimeMethod *rmethod
2159 #define ARGLIST2 gpointer arg1, gpointer arg2, RuntimeMethod *rmethod
2160 #define ARGLIST3 gpointer arg1, gpointer arg2, gpointer arg3, RuntimeMethod *rmethod
2161 #define ARGLIST4 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, RuntimeMethod *rmethod
2162 #define ARGLIST5 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, RuntimeMethod *rmethod
2163 #define ARGLIST6 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, gpointer arg6, RuntimeMethod *rmethod
2164 #define ARGLIST7 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, gpointer arg6, gpointer arg7, RuntimeMethod *rmethod
2165 #define ARGLIST8 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, gpointer arg6, gpointer arg7, gpointer arg8, RuntimeMethod *rmethod
2167 static void interp_entry_static_0 (ARGLIST0) INTERP_ENTRY0 (NULL, NULL, rmethod)
2168 static void interp_entry_static_1 (ARGLIST1) INTERP_ENTRY1 (NULL, NULL, rmethod)
2169 static void interp_entry_static_2 (ARGLIST2) INTERP_ENTRY2 (NULL, NULL, rmethod)
2170 static void interp_entry_static_3 (ARGLIST3) INTERP_ENTRY3 (NULL, NULL, rmethod)
2171 static void interp_entry_static_4 (ARGLIST4) INTERP_ENTRY4 (NULL, NULL, rmethod)
2172 static void interp_entry_static_5 (ARGLIST5) INTERP_ENTRY5 (NULL, NULL, rmethod)
2173 static void interp_entry_static_6 (ARGLIST6) INTERP_ENTRY6 (NULL, NULL, rmethod)
2174 static void interp_entry_static_7 (ARGLIST7) INTERP_ENTRY7 (NULL, NULL, rmethod)
2175 static void interp_entry_static_8 (ARGLIST8) INTERP_ENTRY8 (NULL, NULL, rmethod)
2176 static void interp_entry_static_ret_0 (gpointer res, ARGLIST0) INTERP_ENTRY0 (NULL, res, rmethod)
2177 static void interp_entry_static_ret_1 (gpointer res, ARGLIST1) INTERP_ENTRY1 (NULL, res, rmethod)
2178 static void interp_entry_static_ret_2 (gpointer res, ARGLIST2) INTERP_ENTRY2 (NULL, res, rmethod)
2179 static void interp_entry_static_ret_3 (gpointer res, ARGLIST3) INTERP_ENTRY3 (NULL, res, rmethod)
2180 static void interp_entry_static_ret_4 (gpointer res, ARGLIST4) INTERP_ENTRY4 (NULL, res, rmethod)
2181 static void interp_entry_static_ret_5 (gpointer res, ARGLIST5) INTERP_ENTRY5 (NULL, res, rmethod)
2182 static void interp_entry_static_ret_6 (gpointer res, ARGLIST6) INTERP_ENTRY6 (NULL, res, rmethod)
2183 static void interp_entry_static_ret_7 (gpointer res, ARGLIST7) INTERP_ENTRY7 (NULL, res, rmethod)
2184 static void interp_entry_static_ret_8 (gpointer res, ARGLIST8) INTERP_ENTRY8 (NULL, res, rmethod)
2185 static void interp_entry_instance_0 (gpointer this_arg, ARGLIST0) INTERP_ENTRY0 (this_arg, NULL, rmethod)
2186 static void interp_entry_instance_1 (gpointer this_arg, ARGLIST1) INTERP_ENTRY1 (this_arg, NULL, rmethod)
2187 static void interp_entry_instance_2 (gpointer this_arg, ARGLIST2) INTERP_ENTRY2 (this_arg, NULL, rmethod)
2188 static void interp_entry_instance_3 (gpointer this_arg, ARGLIST3) INTERP_ENTRY3 (this_arg, NULL, rmethod)
2189 static void interp_entry_instance_4 (gpointer this_arg, ARGLIST4) INTERP_ENTRY4 (this_arg, NULL, rmethod)
2190 static void interp_entry_instance_5 (gpointer this_arg, ARGLIST5) INTERP_ENTRY5 (this_arg, NULL, rmethod)
2191 static void interp_entry_instance_6 (gpointer this_arg, ARGLIST6) INTERP_ENTRY6 (this_arg, NULL, rmethod)
2192 static void interp_entry_instance_7 (gpointer this_arg, ARGLIST7) INTERP_ENTRY7 (this_arg, NULL, rmethod)
2193 static void interp_entry_instance_8 (gpointer this_arg, ARGLIST8) INTERP_ENTRY8 (this_arg, NULL, rmethod)
2194 static void interp_entry_instance_ret_0 (gpointer this_arg, gpointer res, ARGLIST0) INTERP_ENTRY0 (this_arg, res, rmethod)
2195 static void interp_entry_instance_ret_1 (gpointer this_arg, gpointer res, ARGLIST1) INTERP_ENTRY1 (this_arg, res, rmethod)
2196 static void interp_entry_instance_ret_2 (gpointer this_arg, gpointer res, ARGLIST2) INTERP_ENTRY2 (this_arg, res, rmethod)
2197 static void interp_entry_instance_ret_3 (gpointer this_arg, gpointer res, ARGLIST3) INTERP_ENTRY3 (this_arg, res, rmethod)
2198 static void interp_entry_instance_ret_4 (gpointer this_arg, gpointer res, ARGLIST4) INTERP_ENTRY4 (this_arg, res, rmethod)
2199 static void interp_entry_instance_ret_5 (gpointer this_arg, gpointer res, ARGLIST5) INTERP_ENTRY5 (this_arg, res, rmethod)
2200 static void interp_entry_instance_ret_6 (gpointer this_arg, gpointer res, ARGLIST6) INTERP_ENTRY6 (this_arg, res, rmethod)
2201 static void interp_entry_instance_ret_7 (gpointer this_arg, gpointer res, ARGLIST7) INTERP_ENTRY6 (this_arg, res, rmethod)
2202 static void interp_entry_instance_ret_8 (gpointer this_arg, gpointer res, ARGLIST8) INTERP_ENTRY6 (this_arg, res, rmethod)
2204 #define INTERP_ENTRY_FUNCLIST(type) interp_entry_ ## type ## _0, interp_entry_ ## type ## _1, interp_entry_ ## type ## _2, interp_entry_ ## type ## _3, interp_entry_ ## type ## _4, interp_entry_ ## type ## _5, interp_entry_ ## type ## _6, interp_entry_ ## type ## _7, interp_entry_ ## type ## _8
2206 gpointer entry_funcs_static [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (static) };
2207 gpointer entry_funcs_static_ret [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (static_ret) };
2208 gpointer entry_funcs_instance [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (instance) };
2209 gpointer entry_funcs_instance_ret [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (instance_ret) };
2211 /* General version for methods with more than MAX_INTERP_ENTRY_ARGS arguments */
2213 interp_entry_general (gpointer this_arg, gpointer res, gpointer *args, gpointer rmethod)
2215 INTERP_ENTRY_BASE (rmethod, this_arg, res);
2216 data.many_args = args;
2217 interp_entry (&data);
2221 * mono_interp_create_method_pointer:
2223 * Return a function pointer which can be used to call METHOD using the
2224 * interpreter. Return NULL for methods which are not supported.
2227 mono_interp_create_method_pointer (MonoMethod *method, MonoError *error)
2230 MonoMethodSignature *sig = mono_method_signature (method);
2231 MonoMethod *wrapper;
2232 RuntimeMethod *rmethod = mono_interp_get_runtime_method (mono_domain_get (), method, error);
2234 /* HACK: method_ptr of delegate should point to a runtime method*/
2235 if (method->wrapper_type && method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
2238 if (rmethod->jit_entry)
2239 return rmethod->jit_entry;
2240 wrapper = mini_get_interp_in_wrapper (sig);
2242 gpointer jit_wrapper = mono_jit_compile_method_jit_only (wrapper, error);
2243 mono_error_assert_ok (error);
2245 //printf ("%s %s\n", mono_method_full_name (method, 1), mono_method_full_name (wrapper, 1));
2246 gpointer entry_func;
2247 if (sig->param_count > MAX_INTERP_ENTRY_ARGS) {
2248 entry_func = interp_entry_general;
2249 } else if (sig->hasthis) {
2250 if (sig->ret->type == MONO_TYPE_VOID)
2251 entry_func = entry_funcs_instance [sig->param_count];
2253 entry_func = entry_funcs_instance_ret [sig->param_count];
2255 if (sig->ret->type == MONO_TYPE_VOID)
2256 entry_func = entry_funcs_static [sig->param_count];
2258 entry_func = entry_funcs_static_ret [sig->param_count];
2260 g_assert (entry_func);
2262 /* This is the argument passed to the interp_in wrapper by the static rgctx trampoline */
2263 MonoFtnDesc *ftndesc = g_new0 (MonoFtnDesc, 1);
2264 ftndesc->addr = entry_func;
2265 ftndesc->arg = rmethod;
2266 mono_error_assert_ok (error);
2269 * The wrapper is called by compiled code, which doesn't pass the extra argument, so we pass it in the
2270 * rgctx register using a trampoline.
2274 g_assert (!mono_aot_only);
2275 addr = mono_arch_get_static_rgctx_trampoline (ftndesc, jit_wrapper);
2277 mono_memory_barrier ();
2278 rmethod->jit_entry = addr;
2284 static int opcode_counts[512];
2286 #define COUNT_OP(op) opcode_counts[op]++
2288 #define COUNT_OP(op)
2292 #define DUMP_INSTR() \
2293 if (tracing > 1) { \
2295 if (sp > frame->stack) { \
2296 ins = dump_stack (frame->stack, sp); \
2298 ins = g_strdup (""); \
2302 char *mn = mono_method_full_name (frame->runtime_method->method, FALSE); \
2303 g_print ("(%p) %s -> ", mono_thread_internal_current (), mn); \
2305 mono_interp_dis_mintop(rtm->code, ip); \
2306 g_print ("\t%d:%s\n", vt_sp - vtalloc, ins); \
2310 #define DUMP_INSTR()
2314 #define USE_COMPUTED_GOTO 1
2316 #if USE_COMPUTED_GOTO
2317 #define MINT_IN_SWITCH(op) COUNT_OP(op); goto *in_labels[op];
2318 #define MINT_IN_CASE(x) LAB_ ## x:
2320 #define MINT_IN_BREAK if (tracing > 1) goto main_loop; else { COUNT_OP(*ip); goto *in_labels[*ip]; }
2322 #define MINT_IN_BREAK { COUNT_OP(*ip); goto *in_labels[*ip]; }
2324 #define MINT_IN_DEFAULT mint_default: if (0) goto mint_default; /* make gcc shut up */
2326 #define MINT_IN_SWITCH(op) switch (op)
2327 #define MINT_IN_CASE(x) case x:
2328 #define MINT_IN_BREAK break
2329 #define MINT_IN_DEFAULT default:
2333 * If EXIT_AT_FINALLY is not -1, exit after exiting the finally clause with that index.
2336 ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, unsigned short *start_with_ip, MonoException *filter_exception, int exit_at_finally)
2338 MonoInvocation child_frame;
2339 GSList *finally_ips = NULL;
2340 const unsigned short *endfinally_ip = NULL;
2341 const unsigned short *ip = NULL;
2342 register stackval *sp;
2345 gint tracing = global_tracing;
2346 unsigned char *vtalloc;
2351 unsigned char *vt_sp;
2352 unsigned char *locals;
2354 MonoObject *o = NULL;
2356 #if USE_COMPUTED_GOTO
2357 static void *in_labels[] = {
2358 #define OPDEF(a,b,c,d) \
2360 #include "mintops.def"
2365 frame->ex_handler = NULL;
2367 context->current_frame = frame;
2369 debug_enter (frame, &tracing);
2371 if (!frame->runtime_method->transformed) {
2372 context->managed_code = 0;
2374 char *mn = mono_method_full_name (frame->runtime_method->method, TRUE);
2375 g_print ("(%p) Transforming %s\n", mono_thread_internal_current (), mn);
2379 do_transform_method (frame, context);
2387 rtm = frame->runtime_method;
2388 if (!start_with_ip ) {
2389 frame->args = alloca (rtm->alloca_size);
2390 memset (frame->args, 0, rtm->alloca_size);
2396 sp = frame->stack = (stackval *) ((char *) frame->args + rtm->args_size);
2397 vt_sp = (unsigned char *) sp + rtm->stack_size;
2401 locals = (unsigned char *) vt_sp + rtm->vt_stack_size;
2402 frame->locals = locals;
2403 child_frame.parent = frame;
2405 if (filter_exception) {
2406 sp->data.p = filter_exception;
2411 * using while (ip < end) may result in a 15% performance drop,
2412 * but it may be useful for debug
2416 /* g_assert (sp >= frame->stack); */
2417 /* g_assert(vt_sp - vtalloc <= rtm->vt_stack_size); */
2419 MINT_IN_SWITCH (*ip) {
2420 MINT_IN_CASE(MINT_INITLOCALS)
2421 memset (locals, 0, rtm->locals_size);
2424 MINT_IN_CASE(MINT_NOP)
2427 MINT_IN_CASE(MINT_BREAK)
2429 do_debugger_tramp (mono_debugger_agent_user_break, frame);
2431 MINT_IN_CASE(MINT_LDNULL)
2436 MINT_IN_CASE(MINT_VTRESULT) {
2437 int ret_size = * (guint16 *)(ip + 1);
2438 unsigned char *ret_vt_sp = vt_sp;
2439 vt_sp -= READ32(ip + 2);
2441 memmove (vt_sp, ret_vt_sp, ret_size);
2442 sp [-1].data.p = vt_sp;
2443 vt_sp += (ret_size + 7) & ~7;
2448 #define LDC(n) do { sp->data.i = (n); ++ip; ++sp; } while (0)
2449 MINT_IN_CASE(MINT_LDC_I4_M1)
2452 MINT_IN_CASE(MINT_LDC_I4_0)
2455 MINT_IN_CASE(MINT_LDC_I4_1)
2458 MINT_IN_CASE(MINT_LDC_I4_2)
2461 MINT_IN_CASE(MINT_LDC_I4_3)
2464 MINT_IN_CASE(MINT_LDC_I4_4)
2467 MINT_IN_CASE(MINT_LDC_I4_5)
2470 MINT_IN_CASE(MINT_LDC_I4_6)
2473 MINT_IN_CASE(MINT_LDC_I4_7)
2476 MINT_IN_CASE(MINT_LDC_I4_8)
2479 MINT_IN_CASE(MINT_LDC_I4_S)
2480 sp->data.i = *(const short *)(ip + 1);
2484 MINT_IN_CASE(MINT_LDC_I4)
2486 sp->data.i = READ32 (ip);
2490 MINT_IN_CASE(MINT_LDC_I8)
2492 sp->data.l = READ64 (ip);
2496 MINT_IN_CASE(MINT_LDC_R4) {
2500 sp->data.f = * (float *)&val;
2505 MINT_IN_CASE(MINT_LDC_R8)
2506 sp->data.l = READ64 (ip + 1); /* note union usage */
2510 MINT_IN_CASE(MINT_DUP)
2515 MINT_IN_CASE(MINT_DUP_VT)
2516 i32 = READ32 (ip + 1);
2518 memcpy(sp->data.p, sp [-1].data.p, i32);
2519 vt_sp += (i32 + 7) & ~7;
2523 MINT_IN_CASE(MINT_POP) {
2524 guint16 u16 = (* (guint16 *)(ip + 1)) + 1;
2526 memmove (sp - u16, sp - 1, (u16 - 1) * sizeof (stackval));
2531 MINT_IN_CASE(MINT_JMP) {
2532 RuntimeMethod *new_method = rtm->data_items [* (guint16 *)(ip + 1)];
2533 if (!new_method->transformed) {
2535 frame->ex = mono_interp_transform_method (new_method, context);
2540 if (new_method->alloca_size > rtm->alloca_size)
2541 g_error ("MINT_JMP to method which needs more stack space (%d > %d)", new_method->alloca_size, rtm->alloca_size);
2542 rtm = frame->runtime_method = new_method;
2543 vt_sp = (unsigned char *) sp + rtm->stack_size;
2547 locals = vt_sp + rtm->vt_stack_size;
2548 frame->locals = locals;
2549 ip = rtm->new_body_start; /* bypass storing input args from callers frame */
2552 MINT_IN_CASE(MINT_CALLI) {
2553 MonoMethodSignature *csignature;
2554 stackval *endsp = sp;
2558 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
2562 child_frame.runtime_method = sp->data.p;
2565 child_frame.retval = sp;
2566 /* decrement by the actual number of args */
2567 sp -= csignature->param_count;
2568 if (csignature->hasthis)
2570 child_frame.stack_args = sp;
2572 #ifndef DISABLE_REMOTING
2573 /* `this' can be NULL for string:.ctor */
2574 if (csignature->hasthis && sp->data.p && mono_object_is_transparent_proxy (sp->data.p)) {
2575 child_frame.runtime_method = mono_interp_get_runtime_method (rtm->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
2576 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2579 if (child_frame.runtime_method->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
2580 child_frame.runtime_method = mono_interp_get_runtime_method (rtm->domain, mono_marshal_get_native_wrapper (child_frame.runtime_method->method, FALSE, FALSE), &error);
2581 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2584 if (csignature->hasthis) {
2585 MonoObject *this_arg = sp->data.p;
2587 if (this_arg->vtable->klass->valuetype) {
2588 gpointer *unboxed = mono_object_unbox (this_arg);
2589 sp [0].data.p = unboxed;
2593 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2595 context->current_frame = frame;
2597 if (context->has_resume_state) {
2598 if (frame == context->handler_frame)
2599 SET_RESUME_STATE (context);
2604 if (child_frame.ex) {
2606 * An exception occurred, need to run finally, fault and catch handlers..
2608 frame->ex = child_frame.ex;
2609 goto handle_finally;
2612 /* need to handle typedbyref ... */
2613 if (csignature->ret->type != MONO_TYPE_VOID) {
2619 MINT_IN_CASE(MINT_CALLI_NAT) {
2620 MonoMethodSignature *csignature;
2621 stackval *endsp = sp;
2622 unsigned char *code = NULL;
2626 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
2631 child_frame.runtime_method = NULL;
2634 child_frame.retval = sp;
2635 /* decrement by the actual number of args */
2636 sp -= csignature->param_count;
2637 if (csignature->hasthis)
2639 child_frame.stack_args = sp;
2640 ves_pinvoke_method (&child_frame, csignature, (MonoFuncV) code, FALSE, context);
2642 context->current_frame = frame;
2644 if (context->has_resume_state) {
2645 if (frame == context->handler_frame)
2646 SET_RESUME_STATE (context);
2651 if (child_frame.ex) {
2653 * An exception occurred, need to run finally, fault and catch handlers..
2655 frame->ex = child_frame.ex;
2656 if (context->search_for_handler) {
2657 context->search_for_handler = 0;
2658 goto handle_exception;
2660 goto handle_finally;
2663 /* need to handle typedbyref ... */
2664 if (csignature->ret->type != MONO_TYPE_VOID) {
2670 MINT_IN_CASE(MINT_CALL) {
2671 stackval *endsp = sp;
2675 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
2678 child_frame.retval = sp;
2679 /* decrement by the actual number of args */
2680 sp -= child_frame.runtime_method->param_count;
2681 if (child_frame.runtime_method->hasthis)
2683 child_frame.stack_args = sp;
2685 #ifndef DISABLE_REMOTING
2686 /* `this' can be NULL for string:.ctor */
2687 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->method->klass->valuetype && sp->data.p && mono_object_is_transparent_proxy (sp->data.p)) {
2688 child_frame.runtime_method = mono_interp_get_runtime_method (rtm->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
2689 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2693 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2695 context->current_frame = frame;
2697 if (context->has_resume_state) {
2698 if (frame == context->handler_frame)
2699 SET_RESUME_STATE (context);
2704 if (child_frame.ex) {
2706 * An exception occurred, need to run finally, fault and catch handlers..
2708 frame->ex = child_frame.ex;
2709 goto handle_exception;;
2712 /* need to handle typedbyref ... */
2717 MINT_IN_CASE(MINT_VCALL) {
2720 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
2724 child_frame.retval = sp;
2725 /* decrement by the actual number of args */
2726 sp -= child_frame.runtime_method->param_count;
2727 if (child_frame.runtime_method->hasthis) {
2729 MonoObject *this_arg = sp->data.p;
2731 THROW_EX (mono_get_exception_null_reference(), ip - 2);
2733 child_frame.stack_args = sp;
2735 #ifndef DISABLE_REMOTING
2736 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->method->klass->valuetype && mono_object_is_transparent_proxy (sp->data.p)) {
2737 child_frame.runtime_method = mono_interp_get_runtime_method (rtm->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
2738 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2742 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2744 context->current_frame = frame;
2746 if (context->has_resume_state) {
2747 if (frame == context->handler_frame)
2748 SET_RESUME_STATE (context);
2753 if (child_frame.ex) {
2755 * An exception occurred, need to run finally, fault and catch handlers..
2757 frame->ex = child_frame.ex;
2758 goto handle_finally;
2763 MINT_IN_CASE(MINT_JIT_CALL) {
2764 RuntimeMethod *rmethod = rtm->data_items [* (guint16 *)(ip + 1)];
2767 sp = do_jit_call (sp, vt_sp, context, frame, rmethod);
2769 if (context->has_resume_state) {
2771 * If this bit is set, it means the call has thrown the exception, and we
2772 * reached this point because the EH code in mono_handle_exception ()
2773 * unwound all the JITted frames below us. mono_interp_set_resume_state ()
2774 * has set the fields in context to indicate where we have to resume execution.
2776 if (frame == context->handler_frame)
2777 SET_RESUME_STATE (context);
2781 if (rmethod->rtype->type != MONO_TYPE_VOID)
2787 MINT_IN_CASE(MINT_CALLVIRT) {
2788 stackval *endsp = sp;
2789 MonoObject *this_arg;
2794 token = * (unsigned short *)(ip + 1);
2796 child_frame.runtime_method = rtm->data_items [token];
2798 child_frame.retval = sp;
2800 /* decrement by the actual number of args */
2801 sp -= child_frame.runtime_method->param_count + 1;
2802 child_frame.stack_args = sp;
2803 this_arg = sp->data.p;
2805 THROW_EX (mono_get_exception_null_reference(), ip - 2);
2806 child_frame.runtime_method = get_virtual_method (child_frame.runtime_method, this_arg);
2808 MonoClass *this_class = this_arg->vtable->klass;
2809 if (this_class->valuetype && child_frame.runtime_method->method->klass->valuetype) {
2811 gpointer *unboxed = mono_object_unbox (this_arg);
2812 sp [0].data.p = unboxed;
2815 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2817 context->current_frame = frame;
2819 if (context->has_resume_state) {
2820 if (frame == context->handler_frame)
2821 SET_RESUME_STATE (context);
2826 if (child_frame.ex) {
2828 * An exception occurred, need to run finally, fault and catch handlers..
2830 frame->ex = child_frame.ex;
2831 if (context->search_for_handler) {
2832 context->search_for_handler = 0;
2833 goto handle_exception;
2835 goto handle_finally;
2838 /* need to handle typedbyref ... */
2843 MINT_IN_CASE(MINT_VCALLVIRT) {
2844 MonoObject *this_arg;
2849 token = * (unsigned short *)(ip + 1);
2851 child_frame.runtime_method = rtm->data_items [token];
2853 child_frame.retval = sp;
2855 /* decrement by the actual number of args */
2856 sp -= child_frame.runtime_method->param_count + 1;
2857 child_frame.stack_args = sp;
2858 this_arg = sp->data.p;
2860 THROW_EX (mono_get_exception_null_reference(), ip - 2);
2861 child_frame.runtime_method = get_virtual_method (child_frame.runtime_method, this_arg);
2863 MonoClass *this_class = this_arg->vtable->klass;
2864 if (this_class->valuetype && child_frame.runtime_method->method->klass->valuetype) {
2865 gpointer *unboxed = mono_object_unbox (this_arg);
2866 sp [0].data.p = unboxed;
2869 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2871 context->current_frame = frame;
2873 if (context->has_resume_state) {
2874 if (frame == context->handler_frame)
2875 SET_RESUME_STATE (context);
2880 if (child_frame.ex) {
2882 * An exception occurred, need to run finally, fault and catch handlers..
2884 frame->ex = child_frame.ex;
2885 if (context->search_for_handler) {
2886 context->search_for_handler = 0;
2887 goto handle_exception;
2889 goto handle_finally;
2893 MINT_IN_CASE(MINT_CALLRUN)
2894 ves_runtime_method (frame, context);
2897 goto handle_exception;
2900 MINT_IN_CASE(MINT_RET)
2902 *frame->retval = *sp;
2903 if (sp > frame->stack)
2904 g_warning ("ret: more values on stack: %d", sp-frame->stack);
2906 MINT_IN_CASE(MINT_RET_VOID)
2907 if (sp > frame->stack)
2908 g_warning ("ret.void: more values on stack: %d %s", sp-frame->stack, mono_method_full_name (frame->runtime_method->method, TRUE));
2910 MINT_IN_CASE(MINT_RET_VT)
2911 i32 = READ32(ip + 1);
2913 memcpy(frame->retval->data.p, sp->data.p, i32);
2914 if (sp > frame->stack)
2915 g_warning ("ret.vt: more values on stack: %d", sp-frame->stack);
2917 MINT_IN_CASE(MINT_BR_S)
2918 ip += (short) *(ip + 1);
2920 MINT_IN_CASE(MINT_BR)
2921 ip += (gint32) READ32(ip + 1);
2923 #define ZEROP_S(datamem, op) \
2925 if (sp->data.datamem op 0) \
2926 ip += * (gint16 *)(ip + 1); \
2930 #define ZEROP(datamem, op) \
2932 if (sp->data.datamem op 0) \
2933 ip += READ32(ip + 1); \
2937 MINT_IN_CASE(MINT_BRFALSE_I4_S)
2940 MINT_IN_CASE(MINT_BRFALSE_I8_S)
2943 MINT_IN_CASE(MINT_BRFALSE_R8_S)
2946 MINT_IN_CASE(MINT_BRFALSE_I4)
2949 MINT_IN_CASE(MINT_BRFALSE_I8)
2952 MINT_IN_CASE(MINT_BRFALSE_R8)
2955 MINT_IN_CASE(MINT_BRTRUE_I4_S)
2958 MINT_IN_CASE(MINT_BRTRUE_I8_S)
2961 MINT_IN_CASE(MINT_BRTRUE_R8_S)
2964 MINT_IN_CASE(MINT_BRTRUE_I4)
2967 MINT_IN_CASE(MINT_BRTRUE_I8)
2970 MINT_IN_CASE(MINT_BRTRUE_R8)
2973 #define CONDBR_S(cond) \
2976 ip += * (gint16 *)(ip + 1); \
2979 #define BRELOP_S(datamem, op) \
2980 CONDBR_S(sp[0].data.datamem op sp[1].data.datamem)
2982 #define CONDBR(cond) \
2985 ip += READ32(ip + 1); \
2989 #define BRELOP(datamem, op) \
2990 CONDBR(sp[0].data.datamem op sp[1].data.datamem)
2992 MINT_IN_CASE(MINT_BEQ_I4_S)
2995 MINT_IN_CASE(MINT_BEQ_I8_S)
2998 MINT_IN_CASE(MINT_BEQ_R8_S)
2999 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
3001 MINT_IN_CASE(MINT_BEQ_I4)
3004 MINT_IN_CASE(MINT_BEQ_I8)
3007 MINT_IN_CASE(MINT_BEQ_R8)
3008 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
3010 MINT_IN_CASE(MINT_BGE_I4_S)
3013 MINT_IN_CASE(MINT_BGE_I8_S)
3016 MINT_IN_CASE(MINT_BGE_R8_S)
3017 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
3019 MINT_IN_CASE(MINT_BGE_I4)
3022 MINT_IN_CASE(MINT_BGE_I8)
3025 MINT_IN_CASE(MINT_BGE_R8)
3026 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
3028 MINT_IN_CASE(MINT_BGT_I4_S)
3031 MINT_IN_CASE(MINT_BGT_I8_S)
3034 MINT_IN_CASE(MINT_BGT_R8_S)
3035 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
3037 MINT_IN_CASE(MINT_BGT_I4)
3040 MINT_IN_CASE(MINT_BGT_I8)
3043 MINT_IN_CASE(MINT_BGT_R8)
3044 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
3046 MINT_IN_CASE(MINT_BLT_I4_S)
3049 MINT_IN_CASE(MINT_BLT_I8_S)
3052 MINT_IN_CASE(MINT_BLT_R8_S)
3053 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
3055 MINT_IN_CASE(MINT_BLT_I4)
3058 MINT_IN_CASE(MINT_BLT_I8)
3061 MINT_IN_CASE(MINT_BLT_R8)
3062 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
3064 MINT_IN_CASE(MINT_BLE_I4_S)
3067 MINT_IN_CASE(MINT_BLE_I8_S)
3070 MINT_IN_CASE(MINT_BLE_R8_S)
3071 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
3073 MINT_IN_CASE(MINT_BLE_I4)
3076 MINT_IN_CASE(MINT_BLE_I8)
3079 MINT_IN_CASE(MINT_BLE_R8)
3080 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
3082 MINT_IN_CASE(MINT_BNE_UN_I4_S)
3085 MINT_IN_CASE(MINT_BNE_UN_I8_S)
3088 MINT_IN_CASE(MINT_BNE_UN_R8_S)
3089 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
3091 MINT_IN_CASE(MINT_BNE_UN_I4)
3094 MINT_IN_CASE(MINT_BNE_UN_I8)
3097 MINT_IN_CASE(MINT_BNE_UN_R8)
3098 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
3101 #define BRELOP_S_CAST(datamem, op, type) \
3103 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
3104 ip += * (gint16 *)(ip + 1); \
3108 #define BRELOP_CAST(datamem, op, type) \
3110 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
3111 ip += READ32(ip + 1); \
3115 MINT_IN_CASE(MINT_BGE_UN_I4_S)
3116 BRELOP_S_CAST(i, >=, guint32);
3118 MINT_IN_CASE(MINT_BGE_UN_I8_S)
3119 BRELOP_S_CAST(l, >=, guint64);
3121 MINT_IN_CASE(MINT_BGE_UN_R8_S)
3122 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
3124 MINT_IN_CASE(MINT_BGE_UN_I4)
3125 BRELOP_CAST(i, >=, guint32);
3127 MINT_IN_CASE(MINT_BGE_UN_I8)
3128 BRELOP_CAST(l, >=, guint64);
3130 MINT_IN_CASE(MINT_BGE_UN_R8)
3131 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
3133 MINT_IN_CASE(MINT_BGT_UN_I4_S)
3134 BRELOP_S_CAST(i, >, guint32);
3136 MINT_IN_CASE(MINT_BGT_UN_I8_S)
3137 BRELOP_S_CAST(l, >, guint64);
3139 MINT_IN_CASE(MINT_BGT_UN_R8_S)
3140 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
3142 MINT_IN_CASE(MINT_BGT_UN_I4)
3143 BRELOP_CAST(i, >, guint32);
3145 MINT_IN_CASE(MINT_BGT_UN_I8)
3146 BRELOP_CAST(l, >, guint64);
3148 MINT_IN_CASE(MINT_BGT_UN_R8)
3149 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
3151 MINT_IN_CASE(MINT_BLE_UN_I4_S)
3152 BRELOP_S_CAST(i, <=, guint32);
3154 MINT_IN_CASE(MINT_BLE_UN_I8_S)
3155 BRELOP_S_CAST(l, <=, guint64);
3157 MINT_IN_CASE(MINT_BLE_UN_R8_S)
3158 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
3160 MINT_IN_CASE(MINT_BLE_UN_I4)
3161 BRELOP_CAST(i, <=, guint32);
3163 MINT_IN_CASE(MINT_BLE_UN_I8)
3164 BRELOP_CAST(l, <=, guint64);
3166 MINT_IN_CASE(MINT_BLE_UN_R8)
3167 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
3169 MINT_IN_CASE(MINT_BLT_UN_I4_S)
3170 BRELOP_S_CAST(i, <, guint32);
3172 MINT_IN_CASE(MINT_BLT_UN_I8_S)
3173 BRELOP_S_CAST(l, <, guint64);
3175 MINT_IN_CASE(MINT_BLT_UN_R8_S)
3176 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
3178 MINT_IN_CASE(MINT_BLT_UN_I4)
3179 BRELOP_CAST(i, <, guint32);
3181 MINT_IN_CASE(MINT_BLT_UN_I8)
3182 BRELOP_CAST(l, <, guint64);
3184 MINT_IN_CASE(MINT_BLT_UN_R8)
3185 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
3187 MINT_IN_CASE(MINT_SWITCH) {
3189 const unsigned short *st;
3195 if ((guint32)sp->data.i < n) {
3197 ip += 2 * (guint32)sp->data.i;
3198 offset = READ32 (ip);
3205 MINT_IN_CASE(MINT_LDIND_I1)
3207 sp[-1].data.i = *(gint8*)sp[-1].data.p;
3209 MINT_IN_CASE(MINT_LDIND_U1)
3211 sp[-1].data.i = *(guint8*)sp[-1].data.p;
3213 MINT_IN_CASE(MINT_LDIND_I2)
3215 sp[-1].data.i = *(gint16*)sp[-1].data.p;
3217 MINT_IN_CASE(MINT_LDIND_U2)
3219 sp[-1].data.i = *(guint16*)sp[-1].data.p;
3221 MINT_IN_CASE(MINT_LDIND_I4) /* Fall through */
3222 MINT_IN_CASE(MINT_LDIND_U4)
3224 sp[-1].data.i = *(gint32*)sp[-1].data.p;
3226 MINT_IN_CASE(MINT_LDIND_I8)
3228 /* memmove handles unaligned case */
3229 memmove (&sp [-1].data.l, sp [-1].data.p, sizeof (gint64));
3231 MINT_IN_CASE(MINT_LDIND_I) {
3232 guint16 offset = * (guint16 *)(ip + 1);
3233 sp[-1 - offset].data.p = *(gpointer*)sp[-1 - offset].data.p;
3237 MINT_IN_CASE(MINT_LDIND_R4)
3239 sp[-1].data.f = *(gfloat*)sp[-1].data.p;
3241 MINT_IN_CASE(MINT_LDIND_R8)
3243 sp[-1].data.f = *(gdouble*)sp[-1].data.p;
3245 MINT_IN_CASE(MINT_LDIND_REF)
3247 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
3249 MINT_IN_CASE(MINT_STIND_REF)
3252 mono_gc_wbarrier_generic_store (sp->data.p, sp [1].data.p);
3254 MINT_IN_CASE(MINT_STIND_I1)
3257 * (gint8 *) sp->data.p = (gint8)sp[1].data.i;
3259 MINT_IN_CASE(MINT_STIND_I2)
3262 * (gint16 *) sp->data.p = (gint16)sp[1].data.i;
3264 MINT_IN_CASE(MINT_STIND_I4)
3267 * (gint32 *) sp->data.p = sp[1].data.i;
3269 MINT_IN_CASE(MINT_STIND_I)
3272 * (mono_i *) sp->data.p = (mono_i)sp[1].data.p;
3274 MINT_IN_CASE(MINT_STIND_I8)
3277 * (gint64 *) sp->data.p = sp[1].data.l;
3279 MINT_IN_CASE(MINT_STIND_R4)
3282 * (float *) sp->data.p = (gfloat)sp[1].data.f;
3284 MINT_IN_CASE(MINT_STIND_R8)
3287 * (double *) sp->data.p = sp[1].data.f;
3289 MINT_IN_CASE(MINT_MONO_ATOMIC_STORE_I4)
3292 InterlockedWrite ((gint32 *) sp->data.p, sp [1].data.i);
3294 #define BINOP(datamem, op) \
3296 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.datamem; \
3298 MINT_IN_CASE(MINT_ADD_I4)
3301 MINT_IN_CASE(MINT_ADD_I8)
3304 MINT_IN_CASE(MINT_ADD_R8)
3307 MINT_IN_CASE(MINT_ADD1_I4)
3311 MINT_IN_CASE(MINT_SUB_I4)
3314 MINT_IN_CASE(MINT_SUB_I8)
3317 MINT_IN_CASE(MINT_SUB_R8)
3320 MINT_IN_CASE(MINT_SUB1_I4)
3324 MINT_IN_CASE(MINT_MUL_I4)
3327 MINT_IN_CASE(MINT_MUL_I8)
3330 MINT_IN_CASE(MINT_MUL_R8)
3333 MINT_IN_CASE(MINT_DIV_I4)
3334 if (sp [-1].data.i == 0)
3335 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3336 if (sp [-1].data.i == (-1))
3337 THROW_EX (mono_get_exception_overflow (), ip);
3340 MINT_IN_CASE(MINT_DIV_I8)
3341 if (sp [-1].data.l == 0)
3342 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3343 if (sp [-1].data.l == (-1))
3344 THROW_EX (mono_get_exception_overflow (), ip);
3347 MINT_IN_CASE(MINT_DIV_R8)
3351 #define BINOP_CAST(datamem, op, type) \
3353 sp [-1].data.datamem = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
3355 MINT_IN_CASE(MINT_DIV_UN_I4)
3356 if (sp [-1].data.i == 0)
3357 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3358 BINOP_CAST(i, /, guint32);
3360 MINT_IN_CASE(MINT_DIV_UN_I8)
3361 if (sp [-1].data.l == 0)
3362 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3363 BINOP_CAST(l, /, guint64);
3365 MINT_IN_CASE(MINT_REM_I4)
3366 if (sp [-1].data.i == 0)
3367 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3368 if (sp [-1].data.i == (-1))
3369 THROW_EX (mono_get_exception_overflow (), ip);
3372 MINT_IN_CASE(MINT_REM_I8)
3373 if (sp [-1].data.l == 0)
3374 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3375 if (sp [-1].data.l == (-1))
3376 THROW_EX (mono_get_exception_overflow (), ip);
3379 MINT_IN_CASE(MINT_REM_R8)
3380 /* FIXME: what do we actually do here? */
3382 sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
3385 MINT_IN_CASE(MINT_REM_UN_I4)
3386 if (sp [-1].data.i == 0)
3387 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3388 BINOP_CAST(i, %, guint32);
3390 MINT_IN_CASE(MINT_REM_UN_I8)
3391 if (sp [-1].data.l == 0)
3392 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3393 BINOP_CAST(l, %, guint64);
3395 MINT_IN_CASE(MINT_AND_I4)
3398 MINT_IN_CASE(MINT_AND_I8)
3401 MINT_IN_CASE(MINT_OR_I4)
3404 MINT_IN_CASE(MINT_OR_I8)
3407 MINT_IN_CASE(MINT_XOR_I4)
3410 MINT_IN_CASE(MINT_XOR_I8)
3414 #define SHIFTOP(datamem, op) \
3416 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.i; \
3419 MINT_IN_CASE(MINT_SHL_I4)
3422 MINT_IN_CASE(MINT_SHL_I8)
3425 MINT_IN_CASE(MINT_SHR_I4)
3428 MINT_IN_CASE(MINT_SHR_I8)
3431 MINT_IN_CASE(MINT_SHR_UN_I4)
3433 sp [-1].data.i = (guint32)sp [-1].data.i >> sp [0].data.i;
3436 MINT_IN_CASE(MINT_SHR_UN_I8)
3438 sp [-1].data.l = (guint64)sp [-1].data.l >> sp [0].data.i;
3441 MINT_IN_CASE(MINT_NEG_I4)
3442 sp [-1].data.i = - sp [-1].data.i;
3445 MINT_IN_CASE(MINT_NEG_I8)
3446 sp [-1].data.l = - sp [-1].data.l;
3449 MINT_IN_CASE(MINT_NEG_R8)
3450 sp [-1].data.f = - sp [-1].data.f;
3453 MINT_IN_CASE(MINT_NOT_I4)
3454 sp [-1].data.i = ~ sp [-1].data.i;
3457 MINT_IN_CASE(MINT_NOT_I8)
3458 sp [-1].data.l = ~ sp [-1].data.l;
3461 MINT_IN_CASE(MINT_CONV_I1_I4)
3462 sp [-1].data.i = (gint8)sp [-1].data.i;
3465 MINT_IN_CASE(MINT_CONV_I1_I8)
3466 sp [-1].data.i = (gint8)sp [-1].data.l;
3469 MINT_IN_CASE(MINT_CONV_I1_R8)
3470 sp [-1].data.i = (gint8)sp [-1].data.f;
3473 MINT_IN_CASE(MINT_CONV_U1_I4)
3474 sp [-1].data.i = (guint8)sp [-1].data.i;
3477 MINT_IN_CASE(MINT_CONV_U1_I8)
3478 sp [-1].data.i = (guint8)sp [-1].data.l;
3481 MINT_IN_CASE(MINT_CONV_U1_R8)
3482 sp [-1].data.i = (guint8)sp [-1].data.f;
3485 MINT_IN_CASE(MINT_CONV_I2_I4)
3486 sp [-1].data.i = (gint16)sp [-1].data.i;
3489 MINT_IN_CASE(MINT_CONV_I2_I8)
3490 sp [-1].data.i = (gint16)sp [-1].data.l;
3493 MINT_IN_CASE(MINT_CONV_I2_R8)
3494 sp [-1].data.i = (gint16)sp [-1].data.f;
3497 MINT_IN_CASE(MINT_CONV_U2_I4)
3498 sp [-1].data.i = (guint16)sp [-1].data.i;
3501 MINT_IN_CASE(MINT_CONV_U2_I8)
3502 sp [-1].data.i = (guint16)sp [-1].data.l;
3505 MINT_IN_CASE(MINT_CONV_U2_R8)
3506 sp [-1].data.i = (guint16)sp [-1].data.f;
3509 MINT_IN_CASE(MINT_CONV_I4_R8)
3510 sp [-1].data.i = (gint32)sp [-1].data.f;
3513 MINT_IN_CASE(MINT_CONV_U4_I8)
3514 MINT_IN_CASE(MINT_CONV_I4_I8)
3515 sp [-1].data.i = (gint32)sp [-1].data.l;
3518 MINT_IN_CASE(MINT_CONV_I4_I8_SP)
3519 sp [-2].data.i = (gint32)sp [-2].data.l;
3522 MINT_IN_CASE(MINT_CONV_U4_R8)
3523 /* needed on arm64 */
3524 if (isinf (sp [-1].data.f))
3527 sp [-1].data.i = (guint32)sp [-1].data.f;
3530 MINT_IN_CASE(MINT_CONV_I8_I4)
3531 sp [-1].data.l = sp [-1].data.i;
3534 MINT_IN_CASE(MINT_CONV_I8_I4_SP)
3535 sp [-2].data.l = sp [-2].data.i;
3538 MINT_IN_CASE(MINT_CONV_I8_U4)
3539 sp [-1].data.l = (guint32)sp [-1].data.i;
3542 MINT_IN_CASE(MINT_CONV_I8_R8)
3543 sp [-1].data.l = (gint64)sp [-1].data.f;
3546 MINT_IN_CASE(MINT_CONV_R4_I4)
3547 sp [-1].data.f = (float)sp [-1].data.i;
3550 MINT_IN_CASE(MINT_CONV_R4_I8)
3551 sp [-1].data.f = (float)sp [-1].data.l;
3554 MINT_IN_CASE(MINT_CONV_R4_R8)
3555 sp [-1].data.f = (float)sp [-1].data.f;
3558 MINT_IN_CASE(MINT_CONV_R8_I4)
3559 sp [-1].data.f = (double)sp [-1].data.i;
3562 MINT_IN_CASE(MINT_CONV_R8_I8)
3563 sp [-1].data.f = (double)sp [-1].data.l;
3566 MINT_IN_CASE(MINT_CONV_U8_I4)
3567 sp [-1].data.l = sp [-1].data.i & 0xffffffff;
3570 MINT_IN_CASE(MINT_CONV_U8_R8)
3571 sp [-1].data.l = (guint64)sp [-1].data.f;
3574 MINT_IN_CASE(MINT_CPOBJ) {
3575 c = rtm->data_items[* (guint16 *)(ip + 1)];
3576 g_assert (c->valuetype);
3577 /* if this assertion fails, we need to add a write barrier */
3578 g_assert (!MONO_TYPE_IS_REFERENCE (&c->byval_arg));
3579 if (c->byval_arg.type == MONO_TYPE_VALUETYPE)
3580 stackval_from_data (&c->byval_arg, &sp [-2], sp [-1].data.p, FALSE);
3582 stackval_from_data (&c->byval_arg, sp [-2].data.p, sp [-1].data.p, FALSE);
3587 MINT_IN_CASE(MINT_LDOBJ) {
3589 c = rtm->data_items[* (guint16 *)(ip + 1)];
3592 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
3593 int size = mono_class_value_size (c, NULL);
3594 sp [-1].data.p = vt_sp;
3595 vt_sp += (size + 7) & ~7;
3597 stackval_from_data (&c->byval_arg, &sp [-1], p, FALSE);
3600 MINT_IN_CASE(MINT_LDSTR)
3601 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
3605 MINT_IN_CASE(MINT_NEWOBJ) {
3606 MonoClass *newobj_class;
3607 MonoMethodSignature *csig;
3608 stackval valuetype_this;
3614 token = * (guint16 *)(ip + 1);
3617 child_frame.ip = NULL;
3618 child_frame.ex = NULL;
3620 child_frame.runtime_method = rtm->data_items [token];
3621 csig = mono_method_signature (child_frame.runtime_method->method);
3622 newobj_class = child_frame.runtime_method->method->klass;
3623 /*if (profiling_classes) {
3624 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
3626 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
3629 if (newobj_class->parent == mono_defaults.array_class) {
3630 sp -= csig->param_count;
3631 child_frame.stack_args = sp;
3632 o = ves_array_create (&child_frame, rtm->domain, newobj_class, csig, sp);
3634 THROW_EX (child_frame.ex, ip);
3635 goto array_constructed;
3638 g_assert (csig->hasthis);
3639 if (csig->param_count) {
3640 sp -= csig->param_count;
3641 memmove (sp + 1, sp, csig->param_count * sizeof (stackval));
3643 child_frame.stack_args = sp;
3646 * First arg is the object.
3648 if (newobj_class->valuetype) {
3649 MonoType *t = &newobj_class->byval_arg;
3650 memset (&valuetype_this, 0, sizeof (stackval));
3651 if (!newobj_class->enumtype && (t->type == MONO_TYPE_VALUETYPE || (t->type == MONO_TYPE_GENERICINST && mono_type_generic_inst_is_valuetype (t)))) {
3653 valuetype_this.data.p = vt_sp;
3655 sp->data.p = &valuetype_this;
3658 if (newobj_class != mono_defaults.string_class) {
3659 context->managed_code = 0;
3660 o = mono_object_new_checked (rtm->domain, newobj_class, &error);
3661 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3662 context->managed_code = 1;
3663 if (*mono_thread_interruption_request_flag ())
3664 mono_thread_interruption_checkpoint ();
3668 child_frame.retval = &retval;
3672 g_assert (csig->call_convention == MONO_CALL_DEFAULT);
3674 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
3676 context->current_frame = frame;
3678 if (context->has_resume_state) {
3679 if (frame == context->handler_frame)
3680 SET_RESUME_STATE (context);
3685 if (child_frame.ex) {
3687 * An exception occurred, need to run finally, fault and catch handlers..
3689 frame->ex = child_frame.ex;
3690 goto handle_finally;
3693 * a constructor returns void, but we need to return the object we created
3696 if (newobj_class->valuetype && !newobj_class->enumtype) {
3697 *sp = valuetype_this;
3698 } else if (newobj_class == mono_defaults.string_class) {
3706 MINT_IN_CASE(MINT_CASTCLASS)
3707 c = rtm->data_items [*(guint16 *)(ip + 1)];
3708 if ((o = sp [-1].data.p)) {
3709 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
3710 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3712 THROW_EX (mono_get_exception_invalid_cast (), ip);
3716 MINT_IN_CASE(MINT_ISINST)
3717 c = rtm->data_items [*(guint16 *)(ip + 1)];
3718 if ((o = sp [-1].data.p)) {
3719 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
3720 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3722 sp [-1].data.p = NULL;
3726 MINT_IN_CASE(MINT_CONV_R_UN_I4)
3727 sp [-1].data.f = (double)(guint32)sp [-1].data.i;
3730 MINT_IN_CASE(MINT_CONV_R_UN_I8)
3731 sp [-1].data.f = (double)(guint64)sp [-1].data.l;
3734 MINT_IN_CASE(MINT_UNBOX)
3735 c = rtm->data_items[*(guint16 *)(ip + 1)];
3739 THROW_EX (mono_get_exception_null_reference (), ip);
3741 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
3742 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3743 if (!(isinst_obj || ((o->vtable->klass->rank == 0) && (o->vtable->klass->element_class == c->element_class))))
3744 THROW_EX (mono_get_exception_invalid_cast (), ip);
3746 sp [-1].data.p = mono_object_unbox (o);
3749 MINT_IN_CASE(MINT_THROW)
3751 frame->ex_handler = NULL;
3753 sp->data.p = mono_get_exception_null_reference ();
3755 THROW_EX ((MonoException *)sp->data.p, ip);
3757 MINT_IN_CASE(MINT_LDFLDA_UNSAFE)
3759 sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
3762 MINT_IN_CASE(MINT_LDFLDA)
3765 THROW_EX (mono_get_exception_null_reference (), ip);
3766 sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
3769 MINT_IN_CASE(MINT_CKNULL)
3772 THROW_EX (mono_get_exception_null_reference (), ip);
3776 #define LDFLD(datamem, fieldtype) \
3777 o = sp [-1].data.p; \
3779 THROW_EX (mono_get_exception_null_reference (), ip); \
3780 sp[-1].data.datamem = * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) ; \
3783 MINT_IN_CASE(MINT_LDFLD_I1) LDFLD(i, gint8); MINT_IN_BREAK;
3784 MINT_IN_CASE(MINT_LDFLD_U1) LDFLD(i, guint8); MINT_IN_BREAK;
3785 MINT_IN_CASE(MINT_LDFLD_I2) LDFLD(i, gint16); MINT_IN_BREAK;
3786 MINT_IN_CASE(MINT_LDFLD_U2) LDFLD(i, guint16); MINT_IN_BREAK;
3787 MINT_IN_CASE(MINT_LDFLD_I4) LDFLD(i, gint32); MINT_IN_BREAK;
3788 MINT_IN_CASE(MINT_LDFLD_I8) LDFLD(l, gint64); MINT_IN_BREAK;
3789 MINT_IN_CASE(MINT_LDFLD_R4) LDFLD(f, float); MINT_IN_BREAK;
3790 MINT_IN_CASE(MINT_LDFLD_R8) LDFLD(f, double); MINT_IN_BREAK;
3791 MINT_IN_CASE(MINT_LDFLD_O) LDFLD(p, gpointer); MINT_IN_BREAK;
3792 MINT_IN_CASE(MINT_LDFLD_P) LDFLD(p, gpointer); MINT_IN_BREAK;
3794 MINT_IN_CASE(MINT_LDFLD_VT)
3797 THROW_EX (mono_get_exception_null_reference (), ip);
3798 i32 = READ32(ip + 2);
3799 sp [-1].data.p = vt_sp;
3800 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
3801 vt_sp += (i32 + 7) & ~7;
3805 MINT_IN_CASE(MINT_LDRMFLD) {
3807 MonoClassField *field;
3812 THROW_EX (mono_get_exception_null_reference (), ip);
3813 field = rtm->data_items[* (guint16 *)(ip + 1)];
3815 #ifndef DISABLE_REMOTING
3816 if (mono_object_is_transparent_proxy (o)) {
3817 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3819 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
3820 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3823 addr = (char*)o + field->offset;
3825 stackval_from_data (field->type, &sp [-1], addr, FALSE);
3829 MINT_IN_CASE(MINT_LDRMFLD_VT) {
3830 MonoClassField *field;
3836 THROW_EX (mono_get_exception_null_reference (), ip);
3837 field = rtm->data_items[* (guint16 *)(ip + 1)];
3838 i32 = READ32(ip + 2);
3840 #ifndef DISABLE_REMOTING
3841 if (mono_object_is_transparent_proxy (o)) {
3842 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3843 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
3844 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3847 addr = (char*)o + field->offset;
3849 sp [-1].data.p = vt_sp;
3850 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
3851 vt_sp += (i32 + 7) & ~7;
3852 memcpy(sp [-1].data.p, addr, i32);
3856 #define STFLD(datamem, fieldtype) \
3857 o = sp [-2].data.p; \
3859 THROW_EX (mono_get_exception_null_reference (), ip); \
3861 * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) = sp[1].data.datamem; \
3864 MINT_IN_CASE(MINT_STFLD_I1) STFLD(i, gint8); MINT_IN_BREAK;
3865 MINT_IN_CASE(MINT_STFLD_U1) STFLD(i, guint8); MINT_IN_BREAK;
3866 MINT_IN_CASE(MINT_STFLD_I2) STFLD(i, gint16); MINT_IN_BREAK;
3867 MINT_IN_CASE(MINT_STFLD_U2) STFLD(i, guint16); MINT_IN_BREAK;
3868 MINT_IN_CASE(MINT_STFLD_I4) STFLD(i, gint32); MINT_IN_BREAK;
3869 MINT_IN_CASE(MINT_STFLD_I8) STFLD(l, gint64); MINT_IN_BREAK;
3870 MINT_IN_CASE(MINT_STFLD_R4) STFLD(f, float); MINT_IN_BREAK;
3871 MINT_IN_CASE(MINT_STFLD_R8) STFLD(f, double); MINT_IN_BREAK;
3872 MINT_IN_CASE(MINT_STFLD_P) STFLD(p, gpointer); MINT_IN_BREAK;
3873 MINT_IN_CASE(MINT_STFLD_O)
3876 THROW_EX (mono_get_exception_null_reference (), ip);
3878 mono_gc_wbarrier_set_field (o, (char *) o + * (guint16 *)(ip + 1), sp [1].data.p);
3882 MINT_IN_CASE(MINT_STFLD_VT)
3885 THROW_EX (mono_get_exception_null_reference (), ip);
3886 i32 = READ32(ip + 2);
3888 memcpy((char *)o + * (guint16 *)(ip + 1), sp [1].data.p, i32);
3889 vt_sp -= (i32 + 7) & ~7;
3893 MINT_IN_CASE(MINT_STRMFLD) {
3894 MonoClassField *field;
3898 THROW_EX (mono_get_exception_null_reference (), ip);
3900 field = rtm->data_items[* (guint16 *)(ip + 1)];
3903 #ifndef DISABLE_REMOTING
3904 if (mono_object_is_transparent_proxy (o)) {
3905 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3906 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
3907 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3910 stackval_to_data (field->type, &sp [-1], (char*)o + field->offset, FALSE);
3915 MINT_IN_CASE(MINT_STRMFLD_VT) {
3916 MonoClassField *field;
3920 THROW_EX (mono_get_exception_null_reference (), ip);
3921 field = rtm->data_items[* (guint16 *)(ip + 1)];
3922 i32 = READ32(ip + 2);
3925 #ifndef DISABLE_REMOTING
3926 if (mono_object_is_transparent_proxy (o)) {
3927 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3928 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
3929 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3932 memcpy((char*)o + field->offset, sp [-1].data.p, i32);
3935 vt_sp -= (i32 + 7) & ~7;
3938 MINT_IN_CASE(MINT_LDSFLDA) {
3939 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3940 sp->data.p = mono_class_static_field_address (rtm->domain, field);
3945 MINT_IN_CASE(MINT_LDSFLD) {
3946 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3947 gpointer addr = mono_class_static_field_address (rtm->domain, field);
3948 stackval_from_data (field->type, sp, addr, FALSE);
3953 MINT_IN_CASE(MINT_LDSFLD_VT) {
3954 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3955 gpointer addr = mono_class_static_field_address (rtm->domain, field);
3956 int size = READ32 (ip + 2);
3960 vt_sp += (size + 7) & ~7;
3961 stackval_from_data (field->type, sp, addr, FALSE);
3965 MINT_IN_CASE(MINT_STSFLD) {
3966 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3967 gpointer addr = mono_class_static_field_address (rtm->domain, field);
3970 stackval_to_data (field->type, sp, addr, FALSE);
3973 MINT_IN_CASE(MINT_STSFLD_VT) {
3974 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3975 gpointer addr = mono_class_static_field_address (rtm->domain, field);
3976 int size = READ32 (ip + 2);
3980 stackval_to_data (field->type, sp, addr, FALSE);
3981 vt_sp -= (size + 7) & ~7;
3984 MINT_IN_CASE(MINT_STOBJ_VT) {
3986 c = rtm->data_items[* (guint16 *)(ip + 1)];
3988 size = mono_class_value_size (c, NULL);
3989 memcpy(sp [-2].data.p, sp [-1].data.p, size);
3990 vt_sp -= (size + 7) & ~7;
3994 MINT_IN_CASE(MINT_STOBJ) {
3995 c = rtm->data_items[* (guint16 *)(ip + 1)];
3998 g_assert (!c->byval_arg.byref);
3999 if (MONO_TYPE_IS_REFERENCE (&c->byval_arg))
4000 mono_gc_wbarrier_generic_store (sp [-2].data.p, sp [-1].data.p);
4002 stackval_from_data (&c->byval_arg, sp [-2].data.p, (char *) &sp [-1].data.p, FALSE);
4006 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_R8)
4007 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
4008 THROW_EX (mono_get_exception_overflow (), ip);
4009 sp [-1].data.i = (guint32)sp [-1].data.f;
4012 MINT_IN_CASE(MINT_CONV_OVF_U8_I4)
4013 if (sp [-1].data.i < 0)
4014 THROW_EX (mono_get_exception_overflow (), ip);
4015 sp [-1].data.l = sp [-1].data.i;
4018 MINT_IN_CASE(MINT_CONV_OVF_U8_I8)
4019 if (sp [-1].data.l < 0)
4020 THROW_EX (mono_get_exception_overflow (), ip);
4023 MINT_IN_CASE(MINT_CONV_OVF_I8_U8)
4024 if ((guint64) sp [-1].data.l > MYGINT64_MAX)
4025 THROW_EX (mono_get_exception_overflow (), ip);
4028 MINT_IN_CASE(MINT_CONV_OVF_U8_R8)
4029 MINT_IN_CASE(MINT_CONV_OVF_I8_UN_R8)
4030 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGINT64_MAX)
4031 THROW_EX (mono_get_exception_overflow (), ip);
4032 sp [-1].data.l = (guint64)sp [-1].data.f;
4035 MINT_IN_CASE(MINT_CONV_OVF_I8_R8)
4036 if (sp [-1].data.f < MYGINT64_MIN || sp [-1].data.f > MYGINT64_MAX)
4037 THROW_EX (mono_get_exception_overflow (), ip);
4038 sp [-1].data.l = (gint64)sp [-1].data.f;
4041 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_I8)
4042 if ((mono_u)sp [-1].data.l > MYGUINT32_MAX)
4043 THROW_EX (mono_get_exception_overflow (), ip);
4044 sp [-1].data.i = (mono_u)sp [-1].data.l;
4047 MINT_IN_CASE(MINT_BOX) {
4048 c = rtm->data_items [* (guint16 *)(ip + 1)];
4049 guint16 offset = * (guint16 *)(ip + 2);
4051 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
4052 int size = mono_class_value_size (c, NULL);
4053 sp [-1 - offset].data.p = mono_value_box_checked (rtm->domain, c, sp [-1 - offset].data.p, &error);
4054 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4055 size = (size + 7) & ~7;
4058 stackval_to_data (&c->byval_arg, &sp [-1 - offset], (char *) &sp [-1 - offset], FALSE);
4059 sp [-1 - offset].data.p = mono_value_box_checked (rtm->domain, c, &sp [-1 - offset], &error);
4060 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4065 MINT_IN_CASE(MINT_NEWARR)
4066 sp [-1].data.p = (MonoObject*) mono_array_new_checked (rtm->domain, rtm->data_items[*(guint16 *)(ip + 1)], sp [-1].data.i, &error);
4067 if (!mono_error_ok (&error)) {
4068 THROW_EX (mono_error_convert_to_exception (&error), ip);
4070 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4072 /*if (profiling_classes) {
4073 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
4075 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
4079 MINT_IN_CASE(MINT_LDLEN)
4082 THROW_EX (mono_get_exception_null_reference (), ip);
4083 sp [-1].data.nati = mono_array_length ((MonoArray *)o);
4086 MINT_IN_CASE(MINT_GETCHR) {
4090 THROW_EX (mono_get_exception_null_reference (), ip);
4091 i32 = sp [-1].data.i;
4092 if (i32 < 0 || i32 >= mono_string_length (s))
4093 THROW_EX (mono_get_exception_index_out_of_range (), ip);
4095 sp [-1].data.i = mono_string_chars(s)[i32];
4099 MINT_IN_CASE(MINT_STRLEN)
4103 THROW_EX (mono_get_exception_null_reference (), ip);
4104 sp [-1].data.i = mono_string_length ((MonoString*) o);
4106 MINT_IN_CASE(MINT_ARRAY_RANK)
4109 THROW_EX (mono_get_exception_null_reference (), ip);
4110 sp [-1].data.i = mono_object_class (sp [-1].data.p)->rank;
4113 MINT_IN_CASE(MINT_LDELEMA)
4114 MINT_IN_CASE(MINT_LDELEMA_TC) {
4115 gboolean needs_typecheck = *ip == MINT_LDELEMA_TC;
4117 MonoClass *klass = rtm->data_items [*(guint16 *) (ip + 1)];
4118 guint16 numargs = *(guint16 *) (ip + 2);
4123 sp->data.p = ves_array_element_address (frame, klass, (MonoArray *) o, &sp [1], needs_typecheck);
4125 THROW_EX (frame->ex, ip);
4130 MINT_IN_CASE(MINT_LDELEM_I1) /* fall through */
4131 MINT_IN_CASE(MINT_LDELEM_U1) /* fall through */
4132 MINT_IN_CASE(MINT_LDELEM_I2) /* fall through */
4133 MINT_IN_CASE(MINT_LDELEM_U2) /* fall through */
4134 MINT_IN_CASE(MINT_LDELEM_I4) /* fall through */
4135 MINT_IN_CASE(MINT_LDELEM_U4) /* fall through */
4136 MINT_IN_CASE(MINT_LDELEM_I8) /* fall through */
4137 MINT_IN_CASE(MINT_LDELEM_I) /* fall through */
4138 MINT_IN_CASE(MINT_LDELEM_R4) /* fall through */
4139 MINT_IN_CASE(MINT_LDELEM_R8) /* fall through */
4140 MINT_IN_CASE(MINT_LDELEM_REF) /* fall through */
4141 MINT_IN_CASE(MINT_LDELEM_VT) {
4149 THROW_EX (mono_get_exception_null_reference (), ip);
4151 aindex = sp [1].data.i;
4152 if (aindex >= mono_array_length (o))
4153 THROW_EX (mono_get_exception_index_out_of_range (), ip);
4156 * FIXME: throw mono_get_exception_array_type_mismatch () if needed
4159 case MINT_LDELEM_I1:
4160 sp [0].data.i = mono_array_get (o, gint8, aindex);
4162 case MINT_LDELEM_U1:
4163 sp [0].data.i = mono_array_get (o, guint8, aindex);
4165 case MINT_LDELEM_I2:
4166 sp [0].data.i = mono_array_get (o, gint16, aindex);
4168 case MINT_LDELEM_U2:
4169 sp [0].data.i = mono_array_get (o, guint16, aindex);
4172 sp [0].data.nati = mono_array_get (o, mono_i, aindex);
4174 case MINT_LDELEM_I4:
4175 sp [0].data.i = mono_array_get (o, gint32, aindex);
4177 case MINT_LDELEM_U4:
4178 sp [0].data.i = mono_array_get (o, guint32, aindex);
4180 case MINT_LDELEM_I8:
4181 sp [0].data.l = mono_array_get (o, guint64, aindex);
4183 case MINT_LDELEM_R4:
4184 sp [0].data.f = mono_array_get (o, float, aindex);
4186 case MINT_LDELEM_R8:
4187 sp [0].data.f = mono_array_get (o, double, aindex);
4189 case MINT_LDELEM_REF:
4190 sp [0].data.p = mono_array_get (o, gpointer, aindex);
4192 case MINT_LDELEM_VT: {
4193 MonoClass *klass_vt = rtm->data_items [*(guint16 *) (ip + 1)];
4194 i32 = READ32 (ip + 2);
4195 char *src_addr = mono_array_addr_with_size ((MonoArray *) o, i32, aindex);
4196 sp [0].data.vt = vt_sp;
4197 stackval_from_data (&klass_vt->byval_arg, sp, src_addr, FALSE);
4198 vt_sp += (i32 + 7) & ~7;
4210 MINT_IN_CASE(MINT_STELEM_I) /* fall through */
4211 MINT_IN_CASE(MINT_STELEM_I1) /* fall through */
4212 MINT_IN_CASE(MINT_STELEM_U1) /* fall through */
4213 MINT_IN_CASE(MINT_STELEM_I2) /* fall through */
4214 MINT_IN_CASE(MINT_STELEM_U2) /* fall through */
4215 MINT_IN_CASE(MINT_STELEM_I4) /* fall through */
4216 MINT_IN_CASE(MINT_STELEM_I8) /* fall through */
4217 MINT_IN_CASE(MINT_STELEM_R4) /* fall through */
4218 MINT_IN_CASE(MINT_STELEM_R8) /* fall through */
4219 MINT_IN_CASE(MINT_STELEM_REF) /* fall through */
4220 MINT_IN_CASE(MINT_STELEM_VT) {
4227 THROW_EX (mono_get_exception_null_reference (), ip);
4229 aindex = sp [1].data.i;
4230 if (aindex >= mono_array_length ((MonoArray *)o))
4231 THROW_EX (mono_get_exception_index_out_of_range (), ip);
4235 mono_array_set ((MonoArray *)o, mono_i, aindex, sp [2].data.nati);
4237 case MINT_STELEM_I1:
4238 mono_array_set ((MonoArray *)o, gint8, aindex, sp [2].data.i);
4240 case MINT_STELEM_U1:
4241 mono_array_set ((MonoArray *) o, guint8, aindex, sp [2].data.i);
4243 case MINT_STELEM_I2:
4244 mono_array_set ((MonoArray *)o, gint16, aindex, sp [2].data.i);
4246 case MINT_STELEM_U2:
4247 mono_array_set ((MonoArray *)o, guint16, aindex, sp [2].data.i);
4249 case MINT_STELEM_I4:
4250 mono_array_set ((MonoArray *)o, gint32, aindex, sp [2].data.i);
4252 case MINT_STELEM_I8:
4253 mono_array_set ((MonoArray *)o, gint64, aindex, sp [2].data.l);
4255 case MINT_STELEM_R4:
4256 mono_array_set ((MonoArray *)o, float, aindex, sp [2].data.f);
4258 case MINT_STELEM_R8:
4259 mono_array_set ((MonoArray *)o, double, aindex, sp [2].data.f);
4261 case MINT_STELEM_REF: {
4262 MonoObject *isinst_obj = mono_object_isinst_checked (sp [2].data.p, mono_object_class (o)->element_class, &error);
4263 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4264 if (sp [2].data.p && !isinst_obj)
4265 THROW_EX (mono_get_exception_array_type_mismatch (), ip);
4266 mono_array_setref ((MonoArray *) o, aindex, sp [2].data.p);
4269 case MINT_STELEM_VT: {
4270 MonoClass *klass_vt = rtm->data_items [*(guint16 *) (ip + 1)];
4271 i32 = READ32 (ip + 2);
4272 char *dst_addr = mono_array_addr_with_size ((MonoArray *) o, i32, aindex);
4274 stackval_to_data (&klass_vt->byval_arg, &sp [2], dst_addr, FALSE);
4275 vt_sp -= (i32 + 7) & ~7;
4286 MINT_IN_CASE(MINT_CONV_OVF_I4_U4)
4287 if (sp [-1].data.i < 0)
4288 THROW_EX (mono_get_exception_overflow (), ip);
4291 MINT_IN_CASE(MINT_CONV_OVF_I4_I8)
4292 if (sp [-1].data.l < MYGINT32_MIN || sp [-1].data.l > MYGINT32_MAX)
4293 THROW_EX (mono_get_exception_overflow (), ip);
4294 sp [-1].data.i = (gint32) sp [-1].data.l;
4297 MINT_IN_CASE(MINT_CONV_OVF_I4_U8)
4298 if (sp [-1].data.l < 0 || sp [-1].data.l > MYGINT32_MAX)
4299 THROW_EX (mono_get_exception_overflow (), ip);
4300 sp [-1].data.i = (gint32) sp [-1].data.l;
4303 MINT_IN_CASE(MINT_CONV_OVF_I4_R8)
4304 if (sp [-1].data.f < MYGINT32_MIN || sp [-1].data.f > MYGINT32_MAX)
4305 THROW_EX (mono_get_exception_overflow (), ip);
4306 sp [-1].data.i = (gint32) sp [-1].data.f;
4309 MINT_IN_CASE(MINT_CONV_OVF_U4_I4)
4310 if (sp [-1].data.i < 0)
4311 THROW_EX (mono_get_exception_overflow (), ip);
4314 MINT_IN_CASE(MINT_CONV_OVF_U4_I8)
4315 if (sp [-1].data.l < 0 || sp [-1].data.l > MYGUINT32_MAX)
4316 THROW_EX (mono_get_exception_overflow (), ip);
4317 sp [-1].data.i = (guint32) sp [-1].data.l;
4320 MINT_IN_CASE(MINT_CONV_OVF_U4_R8)
4321 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
4322 THROW_EX (mono_get_exception_overflow (), ip);
4323 sp [-1].data.i = (guint32) sp [-1].data.f;
4326 MINT_IN_CASE(MINT_CONV_OVF_I2_I4)
4327 if (sp [-1].data.i < -32768 || sp [-1].data.i > 32767)
4328 THROW_EX (mono_get_exception_overflow (), ip);
4331 MINT_IN_CASE(MINT_CONV_OVF_I2_I8)
4332 if (sp [-1].data.l < -32768 || sp [-1].data.l > 32767)
4333 THROW_EX (mono_get_exception_overflow (), ip);
4334 sp [-1].data.i = (gint16) sp [-1].data.l;
4337 MINT_IN_CASE(MINT_CONV_OVF_I2_R8)
4338 if (sp [-1].data.f < -32768 || sp [-1].data.f > 32767)
4339 THROW_EX (mono_get_exception_overflow (), ip);
4340 sp [-1].data.i = (gint16) sp [-1].data.f;
4343 MINT_IN_CASE(MINT_CONV_OVF_U2_I4)
4344 if (sp [-1].data.i < 0 || sp [-1].data.i > 65535)
4345 THROW_EX (mono_get_exception_overflow (), ip);
4348 MINT_IN_CASE(MINT_CONV_OVF_U2_I8)
4349 if (sp [-1].data.l < 0 || sp [-1].data.l > 65535)
4350 THROW_EX (mono_get_exception_overflow (), ip);
4351 sp [-1].data.i = (guint16) sp [-1].data.l;
4354 MINT_IN_CASE(MINT_CONV_OVF_U2_R8)
4355 if (sp [-1].data.f < 0 || sp [-1].data.f > 65535)
4356 THROW_EX (mono_get_exception_overflow (), ip);
4357 sp [-1].data.i = (guint16) sp [-1].data.f;
4360 MINT_IN_CASE(MINT_CONV_OVF_I1_I4)
4361 if (sp [-1].data.i < -128 || sp [-1].data.i > 127)
4362 THROW_EX (mono_get_exception_overflow (), ip);
4365 MINT_IN_CASE(MINT_CONV_OVF_I1_I8)
4366 if (sp [-1].data.l < -128 || sp [-1].data.l > 127)
4367 THROW_EX (mono_get_exception_overflow (), ip);
4368 sp [-1].data.i = (gint8) sp [-1].data.l;
4371 MINT_IN_CASE(MINT_CONV_OVF_I1_R8)
4372 if (sp [-1].data.f < -128 || sp [-1].data.f > 127)
4373 THROW_EX (mono_get_exception_overflow (), ip);
4374 sp [-1].data.i = (gint8) sp [-1].data.f;
4377 MINT_IN_CASE(MINT_CONV_OVF_U1_I4)
4378 if (sp [-1].data.i < 0 || sp [-1].data.i > 255)
4379 THROW_EX (mono_get_exception_overflow (), ip);
4382 MINT_IN_CASE(MINT_CONV_OVF_U1_I8)
4383 if (sp [-1].data.l < 0 || sp [-1].data.l > 255)
4384 THROW_EX (mono_get_exception_overflow (), ip);
4385 sp [-1].data.i = (guint8) sp [-1].data.l;
4388 MINT_IN_CASE(MINT_CONV_OVF_U1_R8)
4389 if (sp [-1].data.f < 0 || sp [-1].data.f > 255)
4390 THROW_EX (mono_get_exception_overflow (), ip);
4391 sp [-1].data.i = (guint8) sp [-1].data.f;
4395 MINT_IN_CASE(MINT_LDELEM)
4396 MINT_IN_CASE(MINT_STELEM)
4397 MINT_IN_CASE(MINT_UNBOX_ANY)
4399 MINT_IN_CASE(MINT_CKFINITE)
4400 if (!isfinite(sp [-1].data.f))
4401 THROW_EX (mono_get_exception_arithmetic (), ip);
4404 MINT_IN_CASE(MINT_MKREFANY) {
4405 c = rtm->data_items [*(guint16 *)(ip + 1)];
4407 /* The value address is on the stack */
4408 gpointer addr = sp [-1].data.p;
4409 /* Push the typedref value on the stack */
4410 sp [-1].data.p = vt_sp;
4411 vt_sp += sizeof (MonoTypedRef);
4413 MonoTypedRef *tref = sp [-1].data.p;
4415 tref->type = &c->byval_arg;
4421 MINT_IN_CASE(MINT_REFANYTYPE) {
4422 MonoTypedRef *tref = sp [-1].data.p;
4423 MonoType *type = tref->type;
4425 vt_sp -= sizeof (MonoTypedRef);
4426 sp [-1].data.p = vt_sp;
4428 *(gpointer*)sp [-1].data.p = type;
4432 MINT_IN_CASE(MINT_REFANYVAL) {
4433 MonoTypedRef *tref = sp [-1].data.p;
4434 gpointer addr = tref->value;
4436 vt_sp -= sizeof (MonoTypedRef);
4438 sp [-1].data.p = addr;
4442 MINT_IN_CASE(MINT_LDTOKEN)
4445 * (gpointer *)sp->data.p = rtm->data_items[*(guint16 *)(ip + 1)];
4449 MINT_IN_CASE(MINT_ADD_OVF_I4)
4450 if (CHECK_ADD_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
4451 THROW_EX (mono_get_exception_overflow (), ip);
4454 MINT_IN_CASE(MINT_ADD_OVF_I8)
4455 if (CHECK_ADD_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
4456 THROW_EX (mono_get_exception_overflow (), ip);
4459 MINT_IN_CASE(MINT_ADD_OVF_UN_I4)
4460 if (CHECK_ADD_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
4461 THROW_EX (mono_get_exception_overflow (), ip);
4462 BINOP_CAST(i, +, guint32);
4464 MINT_IN_CASE(MINT_ADD_OVF_UN_I8)
4465 if (CHECK_ADD_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
4466 THROW_EX (mono_get_exception_overflow (), ip);
4467 BINOP_CAST(l, +, guint64);
4469 MINT_IN_CASE(MINT_MUL_OVF_I4)
4470 if (CHECK_MUL_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
4471 THROW_EX (mono_get_exception_overflow (), ip);
4474 MINT_IN_CASE(MINT_MUL_OVF_I8)
4475 if (CHECK_MUL_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
4476 THROW_EX (mono_get_exception_overflow (), ip);
4479 MINT_IN_CASE(MINT_MUL_OVF_UN_I4)
4480 if (CHECK_MUL_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
4481 THROW_EX (mono_get_exception_overflow (), ip);
4482 BINOP_CAST(i, *, guint32);
4484 MINT_IN_CASE(MINT_MUL_OVF_UN_I8)
4485 if (CHECK_MUL_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
4486 THROW_EX (mono_get_exception_overflow (), ip);
4487 BINOP_CAST(l, *, guint64);
4489 MINT_IN_CASE(MINT_SUB_OVF_I4)
4490 if (CHECK_SUB_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
4491 THROW_EX (mono_get_exception_overflow (), ip);
4494 MINT_IN_CASE(MINT_SUB_OVF_I8)
4495 if (CHECK_SUB_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
4496 THROW_EX (mono_get_exception_overflow (), ip);
4499 MINT_IN_CASE(MINT_SUB_OVF_UN_I4)
4500 if (CHECK_SUB_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
4501 THROW_EX (mono_get_exception_overflow (), ip);
4502 BINOP_CAST(i, -, guint32);
4504 MINT_IN_CASE(MINT_SUB_OVF_UN_I8)
4505 if (CHECK_SUB_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
4506 THROW_EX (mono_get_exception_overflow (), ip);
4507 BINOP_CAST(l, -, guint64);
4509 MINT_IN_CASE(MINT_ENDFINALLY)
4511 int clause_index = *ip;
4512 if (clause_index == exit_at_finally)
4514 while (sp > frame->stack) {
4518 ip = finally_ips->data;
4519 finally_ips = g_slist_remove (finally_ips, ip);
4526 MINT_IN_CASE(MINT_LEAVE) /* Fall through */
4527 MINT_IN_CASE(MINT_LEAVE_S)
4528 while (sp > frame->stack) {
4532 if (*ip == MINT_LEAVE_S) {
4533 ip += (short) *(ip + 1);
4535 ip += (gint32) READ32 (ip + 1);
4538 if (frame->ex_handler != NULL && MONO_OFFSET_IN_HANDLER(frame->ex_handler, frame->ip - rtm->code)) {
4539 frame->ex_handler = NULL;
4542 goto handle_finally;
4544 MINT_IN_CASE(MINT_ICALL_V_V)
4545 MINT_IN_CASE(MINT_ICALL_V_P)
4546 MINT_IN_CASE(MINT_ICALL_P_V)
4547 MINT_IN_CASE(MINT_ICALL_P_P)
4548 MINT_IN_CASE(MINT_ICALL_PP_V)
4549 MINT_IN_CASE(MINT_ICALL_PI_V)
4550 MINT_IN_CASE(MINT_ICALL_PP_P)
4551 MINT_IN_CASE(MINT_ICALL_PI_P)
4552 MINT_IN_CASE(MINT_ICALL_PPP_V)
4553 MINT_IN_CASE(MINT_ICALL_PPI_V)
4554 sp = do_icall (context, *ip, sp, rtm->data_items [*(guint16 *)(ip + 1)]);
4555 if (*mono_thread_interruption_request_flag ()) {
4556 MonoException *exc = mono_thread_interruption_checkpoint ();
4559 context->search_for_handler = 1;
4562 if (frame->ex != NULL)
4563 goto handle_exception;
4566 MINT_IN_CASE(MINT_MONO_LDPTR)
4567 sp->data.p = rtm->data_items [*(guint16 *)(ip + 1)];
4571 MINT_IN_CASE(MINT_MONO_NEWOBJ)
4572 sp->data.p = mono_object_new_checked (rtm->domain, rtm->data_items [*(guint16 *)(ip + 1)], &error);
4573 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4577 MINT_IN_CASE(MINT_MONO_FREE)
4580 g_error ("that doesn't seem right");
4581 g_free (sp->data.p);
4583 MINT_IN_CASE(MINT_MONO_RETOBJ)
4586 stackval_from_data (mono_method_signature (frame->runtime_method->method)->ret, frame->retval, sp->data.p,
4587 mono_method_signature (frame->runtime_method->method)->pinvoke);
4588 if (sp > frame->stack)
4589 g_warning ("retobj: more values on stack: %d", sp-frame->stack);
4591 MINT_IN_CASE(MINT_MONO_TLS) {
4592 MonoTlsKey key = *(gint32 *)(ip + 1);
4593 sp->data.p = ((gpointer (*)()) mono_tls_get_tls_getter (key, FALSE)) ();
4598 MINT_IN_CASE(MINT_MONO_JIT_ATTACH) {
4601 context->original_domain = NULL;
4602 MonoDomain *tls_domain = (MonoDomain *) ((gpointer (*)()) mono_tls_get_tls_getter (TLS_KEY_DOMAIN, FALSE)) ();
4603 gpointer tls_jit = ((gpointer (*)()) mono_tls_get_tls_getter (TLS_KEY_DOMAIN, FALSE)) ();
4605 if (tls_domain != rtm->domain || !tls_jit)
4606 context->original_domain = mono_jit_thread_attach (rtm->domain);
4609 MINT_IN_CASE(MINT_MONO_JIT_DETACH)
4611 mono_jit_set_domain (context->original_domain);
4613 MINT_IN_CASE(MINT_SDB_INTR_LOC)
4614 if (G_UNLIKELY (ss_enabled)) {
4615 static void (*ss_tramp) (void);
4618 void *tramp = mini_get_single_step_trampoline ();
4619 mono_memory_barrier ();
4624 * Make this point to the MINT_SDB_SEQ_POINT instruction which follows this since
4625 * the address of that instruction is stored as the seq point address.
4630 * Use the same trampoline as the JIT. This ensures that
4631 * the debugger has the context for the last interpreter
4634 do_debugger_tramp (ss_tramp, frame);
4636 if (context->has_resume_state) {
4637 if (frame == context->handler_frame)
4638 SET_RESUME_STATE (context);
4645 MINT_IN_CASE(MINT_SDB_SEQ_POINT)
4646 /* Just a placeholder for a breakpoint */
4649 MINT_IN_CASE(MINT_SDB_BREAKPOINT) {
4650 static void (*bp_tramp) (void);
4652 void *tramp = mini_get_breakpoint_trampoline ();
4653 mono_memory_barrier ();
4659 /* Use the same trampoline as the JIT */
4660 do_debugger_tramp (bp_tramp, frame);
4662 if (context->has_resume_state) {
4663 if (frame == context->handler_frame)
4664 SET_RESUME_STATE (context);
4673 #define RELOP(datamem, op) \
4675 sp [-1].data.i = sp [-1].data.datamem op sp [0].data.datamem; \
4677 MINT_IN_CASE(MINT_CEQ_I4)
4680 MINT_IN_CASE(MINT_CEQ0_I4)
4681 sp [-1].data.i = (sp [-1].data.i == 0);
4684 MINT_IN_CASE(MINT_CEQ_I8)
4687 MINT_IN_CASE(MINT_CEQ_R8)
4689 if (isunordered (sp [-1].data.f, sp [0].data.f))
4692 sp [-1].data.i = sp [-1].data.f == sp [0].data.f;
4695 MINT_IN_CASE(MINT_CGT_I4)
4698 MINT_IN_CASE(MINT_CGT_I8)
4701 MINT_IN_CASE(MINT_CGT_R8)
4703 if (isunordered (sp [-1].data.f, sp [0].data.f))
4706 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
4710 #define RELOP_CAST(datamem, op, type) \
4712 sp [-1].data.i = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
4715 MINT_IN_CASE(MINT_CGT_UN_I4)
4716 RELOP_CAST(i, >, guint32);
4718 MINT_IN_CASE(MINT_CGT_UN_I8)
4719 RELOP_CAST(l, >, guint64);
4721 MINT_IN_CASE(MINT_CGT_UN_R8)
4723 if (isunordered (sp [-1].data.f, sp [0].data.f))
4726 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
4729 MINT_IN_CASE(MINT_CLT_I4)
4732 MINT_IN_CASE(MINT_CLT_I8)
4735 MINT_IN_CASE(MINT_CLT_R8)
4737 if (isunordered (sp [-1].data.f, sp [0].data.f))
4740 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
4743 MINT_IN_CASE(MINT_CLT_UN_I4)
4744 RELOP_CAST(i, <, guint32);
4746 MINT_IN_CASE(MINT_CLT_UN_I8)
4747 RELOP_CAST(l, <, guint64);
4749 MINT_IN_CASE(MINT_CLT_UN_R8)
4751 if (isunordered (sp [-1].data.f, sp [0].data.f))
4754 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
4757 MINT_IN_CASE(MINT_LDFTN) {
4758 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
4763 MINT_IN_CASE(MINT_LDVIRTFTN) {
4764 RuntimeMethod *m = rtm->data_items [* (guint16 *)(ip + 1)];
4768 THROW_EX (mono_get_exception_null_reference (), ip - 2);
4770 sp->data.p = get_virtual_method (m, sp->data.p);
4775 #define LDARG(datamem, argtype) \
4776 sp->data.datamem = * (argtype *)(frame->args + * (guint16 *)(ip + 1)); \
4780 MINT_IN_CASE(MINT_LDARG_I1) LDARG(i, gint8); MINT_IN_BREAK;
4781 MINT_IN_CASE(MINT_LDARG_U1) LDARG(i, guint8); MINT_IN_BREAK;
4782 MINT_IN_CASE(MINT_LDARG_I2) LDARG(i, gint16); MINT_IN_BREAK;
4783 MINT_IN_CASE(MINT_LDARG_U2) LDARG(i, guint16); MINT_IN_BREAK;
4784 MINT_IN_CASE(MINT_LDARG_I4) LDARG(i, gint32); MINT_IN_BREAK;
4785 MINT_IN_CASE(MINT_LDARG_I8) LDARG(l, gint64); MINT_IN_BREAK;
4786 MINT_IN_CASE(MINT_LDARG_R4) LDARG(f, float); MINT_IN_BREAK;
4787 MINT_IN_CASE(MINT_LDARG_R8) LDARG(f, double); MINT_IN_BREAK;
4788 MINT_IN_CASE(MINT_LDARG_O) LDARG(p, gpointer); MINT_IN_BREAK;
4789 MINT_IN_CASE(MINT_LDARG_P) LDARG(p, gpointer); MINT_IN_BREAK;
4791 MINT_IN_CASE(MINT_LDARG_VT)
4793 i32 = READ32(ip + 2);
4794 memcpy(sp->data.p, frame->args + * (guint16 *)(ip + 1), i32);
4795 vt_sp += (i32 + 7) & ~7;
4800 #define STARG(datamem, argtype) \
4802 * (argtype *)(frame->args + * (guint16 *)(ip + 1)) = sp->data.datamem; \
4805 MINT_IN_CASE(MINT_STARG_I1) STARG(i, gint8); MINT_IN_BREAK;
4806 MINT_IN_CASE(MINT_STARG_U1) STARG(i, guint8); MINT_IN_BREAK;
4807 MINT_IN_CASE(MINT_STARG_I2) STARG(i, gint16); MINT_IN_BREAK;
4808 MINT_IN_CASE(MINT_STARG_U2) STARG(i, guint16); MINT_IN_BREAK;
4809 MINT_IN_CASE(MINT_STARG_I4) STARG(i, gint32); MINT_IN_BREAK;
4810 MINT_IN_CASE(MINT_STARG_I8) STARG(l, gint64); MINT_IN_BREAK;
4811 MINT_IN_CASE(MINT_STARG_R4) STARG(f, float); MINT_IN_BREAK;
4812 MINT_IN_CASE(MINT_STARG_R8) STARG(f, double); MINT_IN_BREAK;
4813 MINT_IN_CASE(MINT_STARG_O) STARG(p, gpointer); MINT_IN_BREAK;
4814 MINT_IN_CASE(MINT_STARG_P) STARG(p, gpointer); MINT_IN_BREAK;
4816 MINT_IN_CASE(MINT_STARG_VT)
4817 i32 = READ32(ip + 2);
4819 memcpy(frame->args + * (guint16 *)(ip + 1), sp->data.p, i32);
4820 vt_sp -= (i32 + 7) & ~7;
4824 #define STINARG(datamem, argtype) \
4826 int n = * (guint16 *)(ip + 1); \
4827 * (argtype *)(frame->args + rtm->arg_offsets [n]) = frame->stack_args [n].data.datamem; \
4831 MINT_IN_CASE(MINT_STINARG_I1) STINARG(i, gint8); MINT_IN_BREAK;
4832 MINT_IN_CASE(MINT_STINARG_U1) STINARG(i, guint8); MINT_IN_BREAK;
4833 MINT_IN_CASE(MINT_STINARG_I2) STINARG(i, gint16); MINT_IN_BREAK;
4834 MINT_IN_CASE(MINT_STINARG_U2) STINARG(i, guint16); MINT_IN_BREAK;
4835 MINT_IN_CASE(MINT_STINARG_I4) STINARG(i, gint32); MINT_IN_BREAK;
4836 MINT_IN_CASE(MINT_STINARG_I8) STINARG(l, gint64); MINT_IN_BREAK;
4837 MINT_IN_CASE(MINT_STINARG_R4) STINARG(f, float); MINT_IN_BREAK;
4838 MINT_IN_CASE(MINT_STINARG_R8) STINARG(f, double); MINT_IN_BREAK;
4839 MINT_IN_CASE(MINT_STINARG_O) STINARG(p, gpointer); MINT_IN_BREAK;
4840 MINT_IN_CASE(MINT_STINARG_P) STINARG(p, gpointer); MINT_IN_BREAK;
4842 MINT_IN_CASE(MINT_STINARG_VT) {
4843 int n = * (guint16 *)(ip + 1);
4844 i32 = READ32(ip + 2);
4845 memcpy (frame->args + rtm->arg_offsets [n], frame->stack_args [n].data.p, i32);
4850 MINT_IN_CASE(MINT_LDARGA)
4851 sp->data.p = frame->args + * (guint16 *)(ip + 1);
4856 #define LDLOC(datamem, argtype) \
4857 sp->data.datamem = * (argtype *)(locals + * (guint16 *)(ip + 1)); \
4861 MINT_IN_CASE(MINT_LDLOC_I1) LDLOC(i, gint8); MINT_IN_BREAK;
4862 MINT_IN_CASE(MINT_LDLOC_U1) LDLOC(i, guint8); MINT_IN_BREAK;
4863 MINT_IN_CASE(MINT_LDLOC_I2) LDLOC(i, gint16); MINT_IN_BREAK;
4864 MINT_IN_CASE(MINT_LDLOC_U2) LDLOC(i, guint16); MINT_IN_BREAK;
4865 MINT_IN_CASE(MINT_LDLOC_I4) LDLOC(i, gint32); MINT_IN_BREAK;
4866 MINT_IN_CASE(MINT_LDLOC_I8) LDLOC(l, gint64); MINT_IN_BREAK;
4867 MINT_IN_CASE(MINT_LDLOC_R4) LDLOC(f, float); MINT_IN_BREAK;
4868 MINT_IN_CASE(MINT_LDLOC_R8) LDLOC(f, double); MINT_IN_BREAK;
4869 MINT_IN_CASE(MINT_LDLOC_O) LDLOC(p, gpointer); MINT_IN_BREAK;
4870 MINT_IN_CASE(MINT_LDLOC_P) LDLOC(p, gpointer); MINT_IN_BREAK;
4872 MINT_IN_CASE(MINT_LDLOC_VT)
4874 i32 = READ32(ip + 2);
4875 memcpy(sp->data.p, locals + * (guint16 *)(ip + 1), i32);
4876 vt_sp += (i32 + 7) & ~7;
4881 MINT_IN_CASE(MINT_LDLOCA_S)
4882 sp->data.p = locals + * (guint16 *)(ip + 1);
4887 #define STLOC(datamem, argtype) \
4889 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp->data.datamem; \
4892 MINT_IN_CASE(MINT_STLOC_I1) STLOC(i, gint8); MINT_IN_BREAK;
4893 MINT_IN_CASE(MINT_STLOC_U1) STLOC(i, guint8); MINT_IN_BREAK;
4894 MINT_IN_CASE(MINT_STLOC_I2) STLOC(i, gint16); MINT_IN_BREAK;
4895 MINT_IN_CASE(MINT_STLOC_U2) STLOC(i, guint16); MINT_IN_BREAK;
4896 MINT_IN_CASE(MINT_STLOC_I4) STLOC(i, gint32); MINT_IN_BREAK;
4897 MINT_IN_CASE(MINT_STLOC_I8) STLOC(l, gint64); MINT_IN_BREAK;
4898 MINT_IN_CASE(MINT_STLOC_R4) STLOC(f, float); MINT_IN_BREAK;
4899 MINT_IN_CASE(MINT_STLOC_R8) STLOC(f, double); MINT_IN_BREAK;
4900 MINT_IN_CASE(MINT_STLOC_O) STLOC(p, gpointer); MINT_IN_BREAK;
4901 MINT_IN_CASE(MINT_STLOC_P) STLOC(p, gpointer); MINT_IN_BREAK;
4903 #define STLOC_NP(datamem, argtype) \
4904 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp [-1].data.datamem; \
4907 MINT_IN_CASE(MINT_STLOC_NP_I4) STLOC_NP(i, gint32); MINT_IN_BREAK;
4908 MINT_IN_CASE(MINT_STLOC_NP_O) STLOC_NP(p, gpointer); MINT_IN_BREAK;
4910 MINT_IN_CASE(MINT_STLOC_VT)
4911 i32 = READ32(ip + 2);
4913 memcpy(locals + * (guint16 *)(ip + 1), sp->data.p, i32);
4914 vt_sp -= (i32 + 7) & ~7;
4918 MINT_IN_CASE(MINT_LOCALLOC) {
4919 if (sp != frame->stack + 1) /*FIX?*/
4920 THROW_EX (mono_get_exception_execution_engine (NULL), ip);
4922 int len = sp [-1].data.i;
4923 sp [-1].data.p = alloca (len);
4924 MonoMethodHeader *header = mono_method_get_header_checked (frame->runtime_method->method, &error);
4925 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4926 if (header && header->init_locals)
4927 memset (sp [-1].data.p, 0, len);
4931 MINT_IN_CASE(MINT_ENDFILTER)
4932 /* top of stack is result of filter */
4933 frame->retval = &sp [-1];
4935 MINT_IN_CASE(MINT_INITOBJ)
4937 memset (sp->data.vt, 0, READ32(ip + 1));
4940 MINT_IN_CASE(MINT_CPBLK)
4942 if (!sp [0].data.p || !sp [1].data.p)
4943 THROW_EX (mono_get_exception_null_reference(), ip - 1);
4945 /* FIXME: value and size may be int64... */
4946 memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
4949 MINT_IN_CASE(MINT_CONSTRAINED_) {
4951 /* FIXME: implement */
4953 token = READ32 (ip);
4958 MINT_IN_CASE(MINT_INITBLK)
4961 THROW_EX (mono_get_exception_null_reference(), ip - 1);
4963 /* FIXME: value and size may be int64... */
4964 memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
4967 MINT_IN_CASE(MINT_NO_)
4968 /* FIXME: implement */
4972 MINT_IN_CASE(MINT_RETHROW)
4974 * need to clarify what this should actually do:
4975 * start the search from the last found handler in
4976 * this method or continue in the caller or what.
4977 * Also, do we need to run finally/fault handlers after a retrow?
4978 * Well, this implementation will follow the usual search
4979 * for an handler, considering the current ip as throw spot.
4980 * We need to NULL frame->ex_handler for the later code to
4981 * actually run the new found handler.
4983 frame->ex_handler = NULL;
4984 THROW_EX (frame->ex, ip - 1);
4987 g_print ("Unimplemented opcode: %04x %s at 0x%x\n", *ip, mono_interp_opname[*ip], ip-rtm->code);
4988 THROW_EX (mono_get_exception_execution_engine ("Unimplemented opcode"), ip);
4992 g_assert_not_reached ();
4994 * Exception handling code.
4995 * The exception object is stored in frame->ex.
5002 MonoInvocation *inv;
5003 MonoExceptionClause *clause;
5009 g_print ("* Handling exception '%s' at IL_%04x\n",
5010 frame->ex == NULL ? "** Unknown **" : mono_object_class (frame->ex)->name,
5011 rtm == NULL ? 0 : frame->ip - rtm->code);
5013 if (die_on_exception)
5016 for (inv = frame; inv; inv = inv->parent) {
5018 if (inv->runtime_method == NULL)
5020 method = inv->runtime_method->method;
5021 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
5023 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
5025 if (inv->ip == NULL)
5027 ip_offset = inv->ip - inv->runtime_method->code;
5028 inv->ex_handler = NULL; /* clear this in case we are trhowing an exception while handling one - this one wins */
5029 for (i = 0; i < inv->runtime_method->num_clauses; ++i) {
5030 clause = &inv->runtime_method->clauses [i];
5032 g_print ("* clause [%d]: %p\n", i, clause);
5034 if (!MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
5037 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
5040 g_print ("* Filter found at '%s'\n", method->name);
5042 MonoInvocation dup_frame;
5044 memcpy (&dup_frame, inv, sizeof (MonoInvocation));
5045 dup_frame.retval = &retval;
5046 ves_exec_method_with_context (&dup_frame, context, inv->runtime_method->code + clause->data.filter_offset, frame->ex, -1);
5047 if (dup_frame.retval->data.i) {
5050 g_print ("* Matched Filter at '%s'\n", method->name);
5052 inv->ex_handler = clause;
5053 goto handle_finally;
5055 } else if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
5056 MonoObject *isinst_obj = mono_object_isinst_checked ((MonoObject*)frame->ex, clause->data.catch_class, &error);
5057 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
5060 * OK, we found an handler, now we need to execute the finally
5061 * and fault blocks before branching to the handler code.
5065 g_print ("* Found handler at '%s'\n", method->name);
5067 inv->ex_handler = clause;
5068 goto handle_finally;
5074 * If we get here, no handler was found: print a stack trace.
5076 for (inv = frame; inv; inv = inv->parent) {
5077 if (inv->invoke_trap)
5078 goto handle_finally;
5081 ex_obj = (MonoObject *) frame->ex;
5082 mono_unhandled_exception (ex_obj);
5083 MonoJitTlsData *jit_tls = (MonoJitTlsData *) mono_tls_get_jit_tls ();
5084 jit_tls->abort_func (ex_obj);
5085 g_assert_not_reached ();
5091 MonoExceptionClause *clause;
5092 GSList *old_list = finally_ips;
5093 MonoMethod *method = frame->runtime_method->method;
5094 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
5095 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
5099 g_print ("* Handle finally IL_%04x\n", endfinally_ip == NULL ? 0 : endfinally_ip - rtm->code);
5101 if (rtm == NULL || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
5102 || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
5105 ip_offset = frame->ip - rtm->code;
5107 if (endfinally_ip != NULL)
5108 finally_ips = g_slist_prepend(finally_ips, (void *)endfinally_ip);
5109 for (i = 0; i < header->num_clauses; ++i)
5110 if (frame->ex_handler == &rtm->clauses [i])
5114 clause = &rtm->clauses [i];
5115 if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && (endfinally_ip == NULL || !(MONO_OFFSET_IN_CLAUSE (clause, endfinally_ip - rtm->code)))) {
5116 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
5117 ip = rtm->code + clause->handler_offset;
5118 finally_ips = g_slist_prepend (finally_ips, (gpointer) ip);
5121 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
5127 endfinally_ip = NULL;
5129 if (old_list != finally_ips && finally_ips) {
5130 ip = finally_ips->data;
5131 finally_ips = g_slist_remove (finally_ips, ip);
5132 sp = frame->stack; /* spec says stack should be empty at endfinally so it should be at the start too */
5137 * If an exception is set, we need to execute the fault handler, too,
5138 * otherwise, we continue normally.
5148 MonoExceptionClause *clause;
5149 MonoMethod *method = frame->runtime_method->method;
5150 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
5151 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
5155 g_print ("* Handle fault\n");
5157 ip_offset = frame->ip - rtm->code;
5158 for (i = 0; i < header->num_clauses; ++i) {
5159 clause = &rtm->clauses [i];
5160 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
5161 ip = rtm->code + clause->handler_offset;
5164 g_print ("* Executing handler at IL_%04x\n", clause->handler_offset);
5170 * If the handler for the exception was found in this method, we jump
5171 * to it right away, otherwise we return and let the caller run
5172 * the finally, fault and catch blocks.
5173 * This same code should be present in the endfault opcode, but it
5174 * is corrently not assigned in the ECMA specs: LAMESPEC.
5176 if (frame->ex_handler) {
5179 g_print ("* Executing handler at IL_%04x\n", frame->ex_handler->handler_offset);
5181 ip = rtm->code + frame->ex_handler->handler_offset;
5183 vt_sp = (unsigned char *) sp + rtm->stack_size;
5184 sp->data.p = frame->ex;
5195 ves_exec_method (MonoInvocation *frame)
5197 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
5198 ThreadContext context_struct;
5199 MonoDomain *domain = frame->runtime_method->domain;
5206 mono_unhandled_exception ((MonoObject*)frame->ex);
5209 if (context == NULL) {
5210 context = &context_struct;
5211 context_struct.base_frame = frame;
5212 context_struct.current_frame = NULL;
5213 context_struct.env_frame = frame;
5214 context_struct.current_env = &env;
5215 context_struct.search_for_handler = 0;
5216 context_struct.managed_code = 0;
5217 set_context (context);
5220 frame->parent = context->current_frame;
5221 frame->runtime_method = mono_interp_get_runtime_method (domain, frame->method, &error);
5222 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
5223 context->managed_code = 1;
5224 ves_exec_method_with_context (frame, context, NULL, NULL, -1);
5225 context->managed_code = 0;
5227 if (context != &context_struct && context->current_env) {
5228 context->env_frame->ex = frame->ex;
5229 longjmp (*context->current_env, 1);
5232 mono_unhandled_exception ((MonoObject*)frame->ex);
5234 if (context->base_frame == frame)
5237 context->current_frame = frame->parent;
5241 mono_interp_parse_options (const char *options)
5245 args = g_strsplit (options, ",", -1);
5246 for (ptr = args; ptr && *ptr; ptr ++) {
5249 if (strncmp (arg, "jit=", 4) == 0)
5250 jit_classes = g_slist_prepend (jit_classes, arg + 4);
5257 mono_native_tls_alloc (&thread_context_id, NULL);
5260 mono_interp_transform_init ();
5263 typedef int (*TestMethod) (void);
5266 interp_regression_step (MonoImage *image, int verbose, int *total_run, int *total, GTimer *timer, MonoDomain *domain)
5268 int result, expected, failed, cfailed, run;
5269 double elapsed, transform_time;
5271 MonoObject *result_obj;
5272 static gboolean filter_method_init = FALSE;
5273 static const char *filter_method = NULL;
5275 g_print ("Test run: image=%s\n", mono_image_get_filename (image));
5276 cfailed = failed = run = 0;
5277 transform_time = elapsed = 0.0;
5279 g_timer_start (timer);
5280 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
5281 MonoObject *exc = NULL;
5283 MonoMethod *method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
5285 mono_error_cleanup (&error); /* FIXME don't swallow the error */
5289 if (!filter_method_init) {
5290 filter_method = g_getenv ("INTERP_FILTER_METHOD");
5291 filter_method_init = TRUE;
5293 gboolean filter = FALSE;
5294 if (filter_method) {
5295 const char *name = filter_method;
5297 if ((strchr (name, '.') > name) || strchr (name, ':')) {
5298 MonoMethodDesc *desc = mono_method_desc_new (name, TRUE);
5299 filter = mono_method_desc_full_match (desc, method);
5300 mono_method_desc_free (desc);
5302 filter = strcmp (method->name, name) == 0;
5304 } else { /* no filter, check for `Category' attribute on method */
5306 MonoCustomAttrInfo* ainfo = mono_custom_attrs_from_method_checked (method, &error);
5307 mono_error_cleanup (&error);
5311 for (j = 0; j < ainfo->num_attrs && filter; ++j) {
5312 MonoCustomAttrEntry *centry = &ainfo->attrs [j];
5313 if (centry->ctor == NULL)
5316 MonoClass *klass = centry->ctor->klass;
5317 if (strcmp (klass->name, "CategoryAttribute"))
5320 MonoObject *obj = mono_custom_attrs_get_attr_checked (ainfo, klass, &error);
5321 /* FIXME: there is an ordering problem if there're multiple attributes, do this instead:
5322 * MonoObject *obj = create_custom_attr (ainfo->image, centry->ctor, centry->data, centry->data_size, &error); */
5323 mono_error_cleanup (&error);
5324 MonoMethod *getter = mono_class_get_method_from_name (klass, "get_Category", -1);
5325 MonoObject *str = mono_interp_runtime_invoke (getter, obj, NULL, &exc, &error);
5326 mono_error_cleanup (&error);
5327 char *utf8_str = mono_string_to_utf8_checked ((MonoString *) str, &error);
5328 mono_error_cleanup (&error);
5329 if (!strcmp (utf8_str, "!INTERPRETER")) {
5330 g_print ("skip %s...\n", method->name);
5336 if (strncmp (method->name, "test_", 5) == 0 && filter) {
5337 MonoError interp_error;
5338 MonoObject *exc = NULL;
5340 result_obj = mono_interp_runtime_invoke (method, NULL, NULL, &exc, &interp_error);
5341 if (!mono_error_ok (&interp_error)) {
5343 g_print ("Test '%s' execution failed.\n", method->name);
5344 } else if (exc != NULL) {
5345 g_print ("Exception in Test '%s' occured:\n", method->name);
5346 mono_object_describe (exc);
5350 result = *(gint32 *) mono_object_unbox (result_obj);
5351 expected = atoi (method->name + 5); // FIXME: oh no.
5354 if (result != expected) {
5356 g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
5361 g_timer_stop (timer);
5362 elapsed = g_timer_elapsed (timer, NULL);
5363 if (failed > 0 || cfailed > 0){
5364 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
5365 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
5367 g_print ("Results: total tests: %d, all pass \n", run);
5370 g_print ("Elapsed time: %f secs (%f, %f)\n\n", elapsed,
5371 elapsed - transform_time, transform_time);
5372 *total += failed + cfailed;
5377 interp_regression (MonoImage *image, int verbose, int *total_run)
5380 GTimer *timer = g_timer_new ();
5381 MonoDomain *domain = mono_domain_get ();
5385 /* load the metadata */
5386 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
5388 method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
5390 mono_error_cleanup (&error);
5393 mono_class_init (method->klass);
5398 interp_regression_step (image, verbose, total_run, &total, timer, domain);
5400 g_timer_destroy (timer);
5405 mono_interp_regression_list (int verbose, int count, char *images [])
5407 int i, total, total_run, run;
5409 total_run = total = 0;
5410 for (i = 0; i < count; ++i) {
5411 MonoAssembly *ass = mono_assembly_open_predicate (images [i], FALSE, FALSE, NULL, NULL, NULL);
5413 g_warning ("failed to load assembly: %s", images [i]);
5416 total += interp_regression (mono_assembly_get_image (ass), verbose, &run);
5420 g_print ("Overall results: tests: %d, failed: %d (pass: %.2f%%)\n", total_run, total, 100.0*(total_run-total)/total_run);
5422 g_print ("Overall results: tests: %d, 100%% pass\n", total_run);
5429 * mono_interp_set_resume_state:
5431 * Set the state the interpeter will continue to execute from after execution returns to the interpreter.
5434 mono_interp_set_resume_state (MonoJitTlsData *jit_tls, MonoException *ex, MonoInterpFrameHandle interp_frame, gpointer handler_ip)
5436 ThreadContext *context;
5439 context = jit_tls->interp_context;
5442 context->has_resume_state = TRUE;
5443 context->handler_frame = interp_frame;
5444 /* This is on the stack, so it doesn't need a wbarrier */
5445 context->handler_frame->ex = ex;
5446 context->handler_ip = handler_ip;
5450 * mono_interp_run_finally:
5452 * Run the finally clause identified by CLAUSE_INDEX in the intepreter frame given by
5453 * frame->interp_frame.
5456 mono_interp_run_finally (StackFrameInfo *frame, int clause_index, gpointer handler_ip)
5458 MonoInvocation *iframe = frame->interp_frame;
5459 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
5461 ves_exec_method_with_context (iframe, context, handler_ip, NULL, clause_index);
5465 MonoInvocation *current;
5469 * mono_interp_frame_iter_init:
5471 * Initialize an iterator for iterating through interpreted frames.
5474 mono_interp_frame_iter_init (MonoInterpStackIter *iter, gpointer interp_exit_data)
5476 StackIter *stack_iter = (StackIter*)iter;
5478 stack_iter->current = (MonoInvocation*)interp_exit_data;
5482 mono_interp_frame_iter_next (MonoInterpStackIter *iter, StackFrameInfo *frame)
5484 StackIter *stack_iter = (StackIter*)iter;
5485 MonoInvocation *iframe = stack_iter->current;
5487 memset (frame, 0, sizeof (StackFrameInfo));
5488 /* pinvoke frames doesn't have runtime_method set */
5489 while (iframe && !(iframe->runtime_method && iframe->runtime_method->code))
5490 iframe = iframe->parent;
5494 frame->type = FRAME_TYPE_INTERP;
5496 frame->domain = mono_domain_get ();
5497 frame->interp_frame = iframe;
5498 frame->method = iframe->runtime_method->method;
5499 frame->actual_method = frame->method;
5500 /* This is the offset in the interpreter IR */
5501 frame->native_offset = (guint8*)iframe->ip - (guint8*)iframe->runtime_method->code;
5502 frame->ji = iframe->runtime_method->jinfo;
5504 stack_iter->current = iframe->parent;
5510 mono_interp_find_jit_info (MonoDomain *domain, MonoMethod *method)
5514 rtm = lookup_runtime_method (domain, method);
5522 mono_interp_set_breakpoint (MonoJitInfo *jinfo, gpointer ip)
5524 guint16 *code = (guint16*)ip;
5525 g_assert (*code == MINT_SDB_SEQ_POINT);
5526 *code = MINT_SDB_BREAKPOINT;
5530 mono_interp_clear_breakpoint (MonoJitInfo *jinfo, gpointer ip)
5532 guint16 *code = (guint16*)ip;
5533 g_assert (*code == MINT_SDB_BREAKPOINT);
5534 *code = MINT_SDB_SEQ_POINT;
5538 mono_interp_frame_get_jit_info (MonoInterpFrameHandle frame)
5540 MonoInvocation *iframe = (MonoInvocation*)frame;
5542 g_assert (iframe->runtime_method);
5543 return iframe->runtime_method->jinfo;
5547 mono_interp_frame_get_ip (MonoInterpFrameHandle frame)
5549 MonoInvocation *iframe = (MonoInvocation*)frame;
5551 g_assert (iframe->runtime_method);
5552 return (gpointer)iframe->ip;
5556 mono_interp_frame_get_arg (MonoInterpFrameHandle frame, int pos)
5558 MonoInvocation *iframe = (MonoInvocation*)frame;
5560 g_assert (iframe->runtime_method);
5562 int arg_offset = iframe->runtime_method->arg_offsets [pos + (iframe->runtime_method->hasthis ? 1 : 0)];
5564 return iframe->args + arg_offset;
5568 mono_interp_frame_get_local (MonoInterpFrameHandle frame, int pos)
5570 MonoInvocation *iframe = (MonoInvocation*)frame;
5572 g_assert (iframe->runtime_method);
5574 return iframe->locals + iframe->runtime_method->local_offsets [pos];
5578 mono_interp_frame_get_this (MonoInterpFrameHandle frame)
5580 MonoInvocation *iframe = (MonoInvocation*)frame;
5582 g_assert (iframe->runtime_method);
5583 g_assert (iframe->runtime_method->hasthis);
5585 int arg_offset = iframe->runtime_method->arg_offsets [0];
5587 return iframe->args + arg_offset;
5591 mono_interp_start_single_stepping (void)
5597 mono_interp_stop_single_stepping (void)