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 (InterpFrame *frame, InterpFrame *parent_frame, InterpMethod *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->imethod = rmethod;
95 frame->invoke_trap = 0;
98 #define INIT_FRAME(frame,parent_frame,method_args,method_retval,domain,mono_method,error) do { \
99 InterpMethod *_rmethod = mono_interp_get_imethod ((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 (InterpFrame *frame);
113 static char* dump_frame (InterpFrame *inv);
114 static MonoArray *get_trace_ips (MonoDomain *domain, InterpFrame *top);
115 static void ves_exec_method_with_context (InterpFrame *frame, ThreadContext *context, unsigned short *start_with_ip, MonoException *filter_exception, int exit_at_finally);
117 typedef void (*ICallMethod) (InterpFrame *frame);
119 static guint32 die_on_exception = 0;
120 static MonoNativeTlsKey thread_context_id;
122 static char* dump_args (InterpFrame *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 (InterpFrame *frame, int *tracing)
160 g_list_foreach (db_methods, db_match_method, (gpointer)frame->imethod->method);
162 *tracing = nested_trace ? (global_tracing = 2, 3) : 2;
166 MonoMethod *method = frame->imethod->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);
179 #define DEBUG_LEAVE() \
182 args = dump_retval (frame); \
184 mn = mono_method_full_name (frame->imethod->method, FALSE); \
185 g_print ("(%p) Leaving %s", mono_thread_internal_current (), mn); \
187 g_print (" => %s\n", args); \
189 debug_indent_level--; \
190 if (tracing == 3) global_tracing = 0; \
195 int mono_interp_traceopt = 0;
196 static void debug_enter (InterpFrame *frame, int *tracing)
199 #define DEBUG_LEAVE()
203 /* Set the current execution state to the resume state in context */
204 #define SET_RESUME_STATE(context) do { \
205 ip = (context)->handler_ip; \
207 sp->data.p = frame->ex; \
211 (context)->has_resume_state = 0; \
212 (context)->handler_frame = NULL; \
217 set_context (ThreadContext *context)
219 MonoJitTlsData *jit_tls;
221 mono_native_tls_set_value (thread_context_id, context);
222 jit_tls = mono_tls_get_jit_tls ();
224 jit_tls->interp_context = context;
228 ves_real_abort (int line, MonoMethod *mh,
229 const unsigned short *ip, stackval *stack, stackval *sp)
232 fprintf (stderr, "Execution aborted in method: %s::%s\n", mh->klass->name, mh->name);
233 fprintf (stderr, "Line=%d IP=0x%04lx, Aborted execution\n", line,
234 ip-(const unsigned short *)mono_method_get_header_checked (mh, &error)->code);
235 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
236 g_print ("0x%04x %02x\n",
237 ip-(const unsigned short *)mono_method_get_header_checked (mh, &error)->code, *ip);
238 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
240 printf ("\t[%ld] 0x%08x %0.5f\n", sp-stack, sp[-1].data.i, sp[-1].data.f);
243 #define ves_abort() \
245 ves_real_abort(__LINE__, frame->imethod->method, ip, frame->stack, sp); \
246 THROW_EX (mono_get_exception_execution_engine (NULL), ip); \
250 lookup_imethod (MonoDomain *domain, MonoMethod *method)
253 MonoJitDomainInfo *info;
255 info = domain_jit_info (domain);
256 mono_domain_jit_code_hash_lock (domain);
257 rtm = mono_internal_hash_table_lookup (&info->interp_code_hash, method);
258 mono_domain_jit_code_hash_unlock (domain);
263 mono_interp_get_imethod (MonoDomain *domain, MonoMethod *method, MonoError *error)
266 MonoJitDomainInfo *info;
267 MonoMethodSignature *sig;
272 info = domain_jit_info (domain);
273 mono_domain_jit_code_hash_lock (domain);
274 rtm = mono_internal_hash_table_lookup (&info->interp_code_hash, method);
275 mono_domain_jit_code_hash_unlock (domain);
279 sig = mono_method_signature (method);
281 rtm = mono_domain_alloc0 (domain, sizeof (InterpMethod));
282 rtm->method = method;
283 rtm->domain = domain;
284 rtm->param_count = sig->param_count;
285 rtm->hasthis = sig->hasthis;
286 rtm->rtype = mini_get_underlying_type (sig->ret);
287 rtm->param_types = mono_domain_alloc0 (domain, sizeof (MonoType*) * sig->param_count);
288 for (i = 0; i < sig->param_count; ++i)
289 rtm->param_types [i] = mini_get_underlying_type (sig->params [i]);
291 mono_domain_jit_code_hash_lock (domain);
292 if (!mono_internal_hash_table_lookup (&info->interp_code_hash, method))
293 mono_internal_hash_table_insert (&info->interp_code_hash, method, rtm);
294 mono_domain_jit_code_hash_unlock (domain);
296 rtm->prof_flags = mono_profiler_get_call_instrumentation_flags (rtm->method);
302 mono_interp_create_trampoline (MonoDomain *domain, MonoMethod *method, MonoError *error)
304 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
305 method = mono_marshal_get_synchronized_wrapper (method);
306 return mono_interp_get_imethod (domain, method, error);
312 * Push an LMF frame on the LMF stack
313 * to mark the transition to native code.
314 * This is needed for the native code to
315 * be able to do stack walks.
318 interp_push_lmf (MonoLMFExt *ext, InterpFrame *frame)
320 memset (ext, 0, sizeof (MonoLMFExt));
321 ext->interp_exit = TRUE;
322 ext->interp_exit_data = frame;
328 interp_pop_lmf (MonoLMFExt *ext)
330 mono_pop_lmf (&ext->lmf);
333 static inline InterpMethod*
334 get_virtual_method (InterpMethod *imethod, MonoObject *obj)
336 MonoMethod *m = imethod->method;
337 MonoDomain *domain = imethod->domain;
338 InterpMethod *ret = NULL;
341 #ifndef DISABLE_REMOTING
342 if (mono_object_is_transparent_proxy (obj)) {
343 ret = mono_interp_get_imethod (domain, mono_marshal_get_remoting_invoke (m), &error);
344 mono_error_assert_ok (&error);
349 if ((m->flags & METHOD_ATTRIBUTE_FINAL) || !(m->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
350 if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
351 ret = mono_interp_get_imethod (domain, mono_marshal_get_synchronized_wrapper (m), &error);
352 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
359 mono_class_setup_vtable (obj->vtable->klass);
361 int slot = mono_method_get_vtable_slot (m);
362 if (mono_class_is_interface (m->klass)) {
363 g_assert (obj->vtable->klass != m->klass);
364 /* TODO: interface offset lookup is slow, go through IMT instead */
365 gboolean non_exact_match;
366 slot += mono_class_interface_offset_with_variance (obj->vtable->klass, m->klass, &non_exact_match);
369 MonoMethod *virtual_method = obj->vtable->klass->vtable [slot];
370 if (m->is_inflated && mono_method_get_context (m)->method_inst) {
371 MonoGenericContext context = { NULL, NULL };
373 if (mono_class_is_ginst (virtual_method->klass))
374 context.class_inst = mono_class_get_generic_class (virtual_method->klass)->context.class_inst;
375 else if (mono_class_is_gtd (virtual_method->klass))
376 context.class_inst = mono_class_get_generic_container (virtual_method->klass)->context.class_inst;
377 context.method_inst = mono_method_get_context (m)->method_inst;
379 virtual_method = mono_class_inflate_generic_method_checked (virtual_method, &context, &error);
380 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
383 if (virtual_method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
384 virtual_method = mono_marshal_get_synchronized_wrapper (virtual_method);
387 InterpMethod *virtual_imethod = mono_interp_get_imethod (domain, virtual_method, &error);
388 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
389 return virtual_imethod;
393 stackval_from_data (MonoType *type, stackval *result, char *data, gboolean pinvoke)
396 switch (type->type) {
397 case MONO_TYPE_OBJECT:
398 case MONO_TYPE_CLASS:
399 case MONO_TYPE_STRING:
400 case MONO_TYPE_ARRAY:
401 case MONO_TYPE_SZARRAY:
406 result->data.p = *(gpointer*)data;
409 switch (type->type) {
413 result->data.i = *(gint8*)data;
416 case MONO_TYPE_BOOLEAN:
417 result->data.i = *(guint8*)data;
420 result->data.i = *(gint16*)data;
424 result->data.i = *(guint16*)data;
427 result->data.i = *(gint32*)data;
431 result->data.nati = *(mono_i*)data;
434 result->data.p = *(gpointer*)data;
437 result->data.i = *(guint32*)data;
441 /* memmove handles unaligned case */
442 memmove (&tmp, data, sizeof (float));
443 result->data.f = tmp;
448 memmove (&result->data.l, data, sizeof (gint64));
451 memmove (&result->data.f, data, sizeof (double));
453 case MONO_TYPE_STRING:
454 case MONO_TYPE_SZARRAY:
455 case MONO_TYPE_CLASS:
456 case MONO_TYPE_OBJECT:
457 case MONO_TYPE_ARRAY:
458 result->data.p = *(gpointer*)data;
460 case MONO_TYPE_VALUETYPE:
461 if (type->data.klass->enumtype) {
462 stackval_from_data (mono_class_enum_basetype (type->data.klass), result, data, pinvoke);
465 mono_value_copy (result->data.vt, data, type->data.klass);
467 case MONO_TYPE_GENERICINST: {
468 if (mono_type_generic_inst_is_valuetype (type)) {
469 mono_value_copy (result->data.vt, data, mono_class_from_mono_type (type));
472 stackval_from_data (&type->data.generic_class->container_class->byval_arg, result, data, pinvoke);
476 g_warning ("got type 0x%02x", type->type);
477 g_assert_not_reached ();
482 stackval_to_data (MonoType *type, stackval *val, char *data, gboolean pinvoke)
485 gpointer *p = (gpointer*)data;
489 /* printf ("TODAT0 %p\n", data); */
490 switch (type->type) {
493 guint8 *p = (guint8*)data;
497 case MONO_TYPE_BOOLEAN: {
498 guint8 *p = (guint8*)data;
499 *p = (val->data.i != 0);
504 case MONO_TYPE_CHAR: {
505 guint16 *p = (guint16*)data;
510 mono_i *p = (mono_i*)data;
511 /* In theory the value used by stloc should match the local var type
512 but in practice it sometimes doesn't (a int32 gets dup'd and stloc'd into
513 a native int - both by csc and mcs). Not sure what to do about sign extension
514 as it is outside the spec... doing the obvious */
515 *p = (mono_i)val->data.nati;
519 mono_u *p = (mono_u*)data;
521 *p = (mono_u)val->data.nati;
526 gint32 *p = (gint32*)data;
532 gint64 *p = (gint64*)data;
537 float *p = (float*)data;
542 double *p = (double*)data;
546 case MONO_TYPE_STRING:
547 case MONO_TYPE_SZARRAY:
548 case MONO_TYPE_CLASS:
549 case MONO_TYPE_OBJECT:
550 case MONO_TYPE_ARRAY: {
551 gpointer *p = (gpointer *) data;
552 mono_gc_wbarrier_generic_store (p, val->data.p);
555 case MONO_TYPE_PTR: {
556 gpointer *p = (gpointer *) data;
560 case MONO_TYPE_VALUETYPE:
561 if (type->data.klass->enumtype) {
562 stackval_to_data (mono_class_enum_basetype (type->data.klass), val, data, pinvoke);
565 mono_value_copy (data, val->data.vt, type->data.klass);
567 case MONO_TYPE_GENERICINST: {
568 MonoClass *container_class = type->data.generic_class->container_class;
570 if (container_class->valuetype && !container_class->enumtype) {
571 mono_value_copy (data, val->data.vt, mono_class_from_mono_type (type));
574 stackval_to_data (&type->data.generic_class->container_class->byval_arg, val, data, pinvoke);
578 g_warning ("got type %x", type->type);
579 g_assert_not_reached ();
584 fill_in_trace (MonoException *exception, InterpFrame *frame)
587 char *stack_trace = dump_frame (frame);
588 MonoDomain *domain = frame->imethod->domain;
589 (exception)->stack_trace = mono_string_new_checked (domain, stack_trace, &error);
590 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
591 (exception)->trace_ips = get_trace_ips (domain, frame);
592 g_free (stack_trace);
595 #define FILL_IN_TRACE(exception, frame) fill_in_trace(exception, frame)
597 #define THROW_EX_GENERAL(exception,ex_ip,rethrow) \
599 frame->ip = (ex_ip); \
600 frame->ex = (MonoException*)(exception); \
602 FILL_IN_TRACE(frame->ex, frame); \
604 MONO_PROFILER_RAISE (exception_throw, ((MonoObject *) exception)); \
605 goto handle_exception; \
608 #define THROW_EX(exception,ex_ip) THROW_EX_GENERAL ((exception), (ex_ip), FALSE)
611 ves_array_create (InterpFrame *frame, MonoDomain *domain, MonoClass *klass, MonoMethodSignature *sig, stackval *values)
614 intptr_t *lower_bounds;
619 lengths = alloca (sizeof (uintptr_t) * klass->rank * 2);
620 for (i = 0; i < sig->param_count; ++i) {
621 lengths [i] = values->data.i;
624 if (klass->rank == sig->param_count) {
625 /* Only lengths provided. */
628 /* lower bounds are first. */
629 lower_bounds = (intptr_t *) lengths;
630 lengths += klass->rank;
632 obj = (MonoObject*) mono_array_new_full_checked (domain, klass, lengths, lower_bounds, &error);
633 if (!mono_error_ok (&error)) {
634 frame->ex = mono_error_convert_to_exception (&error);
635 FILL_IN_TRACE (frame->ex, frame);
637 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
642 ves_array_calculate_index (MonoArray *ao, stackval *sp, InterpFrame *frame, gboolean safe)
644 g_assert (!frame->ex);
645 MonoClass *ac = ((MonoObject *) ao)->vtable->klass;
649 for (gint32 i = 0; i < ac->rank; i++) {
650 guint32 idx = sp [i].data.i;
651 guint32 lower = ao->bounds [i].lower_bound;
652 guint32 len = ao->bounds [i].length;
653 if (safe && (idx < lower || (idx - lower) >= len)) {
654 frame->ex = mono_get_exception_index_out_of_range ();
655 FILL_IN_TRACE (frame->ex, frame);
658 pos = (pos * len) + idx - lower;
662 if (safe && pos >= ao->max_length) {
663 frame->ex = mono_get_exception_index_out_of_range ();
664 FILL_IN_TRACE (frame->ex, frame);
672 ves_array_set (InterpFrame *frame)
674 stackval *sp = frame->stack_args + 1;
676 MonoObject *o = frame->stack_args->data.p;
677 MonoArray *ao = (MonoArray *) o;
678 MonoClass *ac = o->vtable->klass;
680 g_assert (ac->rank >= 1);
682 gint32 pos = ves_array_calculate_index (ao, sp, frame, TRUE);
686 if (sp [ac->rank].data.p && !mono_object_class (o)->element_class->valuetype) {
688 MonoObject *isinst = mono_object_isinst_checked (sp [ac->rank].data.p, mono_object_class (o)->element_class, &error);
689 mono_error_cleanup (&error);
691 frame->ex = mono_get_exception_array_type_mismatch ();
692 FILL_IN_TRACE (frame->ex, frame);
697 gint32 esize = mono_array_element_size (ac);
698 gpointer ea = mono_array_addr_with_size (ao, esize, pos);
700 MonoType *mt = mono_method_signature (frame->imethod->method)->params [ac->rank];
701 stackval_to_data (mt, &sp [ac->rank], ea, FALSE);
705 ves_array_get (InterpFrame *frame, gboolean safe)
707 stackval *sp = frame->stack_args + 1;
709 MonoObject *o = frame->stack_args->data.p;
710 MonoArray *ao = (MonoArray *) o;
711 MonoClass *ac = o->vtable->klass;
713 g_assert (ac->rank >= 1);
715 gint32 pos = ves_array_calculate_index (ao, sp, frame, safe);
719 gint32 esize = mono_array_element_size (ac);
720 gpointer ea = mono_array_addr_with_size (ao, esize, pos);
722 MonoType *mt = mono_method_signature (frame->imethod->method)->ret;
723 stackval_from_data (mt, frame->retval, ea, FALSE);
727 ves_array_element_address (InterpFrame *frame, MonoClass *required_type, MonoArray *ao, stackval *sp, gboolean needs_typecheck)
729 MonoClass *ac = ((MonoObject *) ao)->vtable->klass;
731 g_assert (ac->rank >= 1);
733 gint32 pos = ves_array_calculate_index (ao, sp, frame, TRUE);
737 if (needs_typecheck && !mono_class_is_assignable_from (mono_object_class ((MonoObject *) ao)->element_class, required_type->element_class)) {
738 frame->ex = mono_get_exception_array_type_mismatch ();
739 FILL_IN_TRACE (frame->ex, frame);
742 gint32 esize = mono_array_element_size (ac);
743 return mono_array_addr_with_size (ao, esize, pos);
747 interp_walk_stack_with_ctx (MonoInternalStackWalk func, MonoContext *ctx, MonoUnwindOptions options, void *user_data)
750 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
755 InterpFrame *frame = context->current_frame;
758 MonoStackFrameInfo fi;
759 memset (&fi, 0, sizeof (MonoStackFrameInfo));
761 /* TODO: hack to make some asserts happy. */
762 fi.ji = (MonoJitInfo *) frame->imethod;
765 fi.method = fi.actual_method = frame->imethod->method;
767 if (!fi.method || (fi.method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) || (fi.method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
769 fi.type = FRAME_TYPE_MANAGED_TO_NATIVE;
771 MonoMethodHeader *hd = mono_method_get_header_checked (fi.method, &error);
772 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
773 fi.type = FRAME_TYPE_MANAGED;
774 fi.il_offset = frame->ip - (const unsigned short *) hd->code;
775 if (!fi.method->wrapper_type)
779 if (func (&fi, ctx, user_data))
781 frame = frame->parent;
785 static MonoPIFunc mono_interp_enter_icall_trampoline = NULL;
787 static InterpMethodArguments* build_args_from_sig (MonoMethodSignature *sig, InterpFrame *frame)
789 InterpMethodArguments *margs = g_malloc0 (sizeof (InterpMethodArguments));
792 g_assert (mono_arm_eabi_supported ());
793 int i8_align = mono_arm_i8_align ();
799 for (int i = 0; i < sig->param_count; i++) {
800 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
802 case MONO_TYPE_BOOLEAN:
813 case MONO_TYPE_SZARRAY:
814 case MONO_TYPE_CLASS:
815 case MONO_TYPE_OBJECT:
816 case MONO_TYPE_STRING:
817 case MONO_TYPE_VALUETYPE:
818 case MONO_TYPE_GENERICINST:
819 #if SIZEOF_VOID_P == 8
824 #if SIZEOF_VOID_P == 4
827 /* pairs begin at even registers */
828 if (i8_align == 8 && margs->ilen & 1)
835 #if SIZEOF_VOID_P == 8
840 #if SIZEOF_VOID_P == 4
846 g_error ("build_args_from_sig: not implemented yet (1): 0x%x\n", ptype);
851 margs->iargs = g_malloc0 (sizeof (gpointer) * margs->ilen);
854 margs->fargs = g_malloc0 (sizeof (double) * margs->flen);
856 if (margs->ilen > INTERP_ICALL_TRAMP_IARGS)
857 g_error ("build_args_from_sig: TODO, allocate gregs: %d\n", margs->ilen);
859 if (margs->flen > INTERP_ICALL_TRAMP_FARGS)
860 g_error ("build_args_from_sig: TODO, allocate fregs: %d\n", margs->flen);
867 margs->iargs [0] = frame->stack_args->data.p;
871 for (int i = 0; i < sig->param_count; i++) {
872 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
874 case MONO_TYPE_BOOLEAN:
885 case MONO_TYPE_SZARRAY:
886 case MONO_TYPE_CLASS:
887 case MONO_TYPE_OBJECT:
888 case MONO_TYPE_STRING:
889 case MONO_TYPE_VALUETYPE:
890 case MONO_TYPE_GENERICINST:
891 #if SIZEOF_VOID_P == 8
894 margs->iargs [int_i] = frame->stack_args [i].data.p;
896 g_print ("build_args_from_sig: margs->iargs [%d]: %p (frame @ %d)\n", int_i, margs->iargs [int_i], i);
900 #if SIZEOF_VOID_P == 4
902 stackval *sarg = &frame->stack_args [i];
904 /* pairs begin at even registers */
905 if (i8_align == 8 && int_i & 1)
908 margs->iargs [int_i] = (gpointer) sarg->data.pair.lo;
910 margs->iargs [int_i] = (gpointer) sarg->data.pair.hi;
912 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);
920 if (ptype == MONO_TYPE_R4)
921 * (float *) &(margs->fargs [int_f]) = (float) frame->stack_args [i].data.f;
923 margs->fargs [int_f] = frame->stack_args [i].data.f;
925 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);
927 #if SIZEOF_VOID_P == 4
934 g_error ("build_args_from_sig: not implemented yet (2): 0x%x\n", ptype);
938 switch (sig->ret->type) {
939 case MONO_TYPE_BOOLEAN:
950 case MONO_TYPE_SZARRAY:
951 case MONO_TYPE_CLASS:
952 case MONO_TYPE_OBJECT:
953 case MONO_TYPE_STRING:
955 case MONO_TYPE_VALUETYPE:
956 case MONO_TYPE_GENERICINST:
957 margs->retval = &(frame->retval->data.p);
958 margs->is_float_ret = 0;
962 margs->retval = &(frame->retval->data.p);
963 margs->is_float_ret = 1;
966 margs->retval = NULL;
969 g_error ("build_args_from_sig: ret type not implemented yet: 0x%x\n", sig->ret->type);
976 ves_pinvoke_method (InterpFrame *frame, MonoMethodSignature *sig, MonoFuncV addr, gboolean string_ctor, ThreadContext *context)
979 InterpFrame *old_frame = context->current_frame;
980 InterpFrame *old_env_frame = context->env_frame;
981 jmp_buf *old_env = context->current_env;
985 context->current_frame = old_frame;
986 context->env_frame = old_env_frame;
987 context->current_env = old_env;
988 context->managed_code = 1;
993 context->env_frame = frame;
994 context->current_env = &env;
996 g_assert (!frame->imethod);
997 if (!mono_interp_enter_icall_trampoline) {
999 mono_interp_enter_icall_trampoline = mono_aot_get_trampoline ("enter_icall_trampoline");
1001 MonoTrampInfo *info;
1002 mono_interp_enter_icall_trampoline = mono_arch_get_enter_icall_trampoline (&info);
1004 // mono_tramp_info_register (info, NULL);
1008 InterpMethodArguments *margs = build_args_from_sig (sig, frame);
1010 g_print ("ICALL: mono_interp_enter_icall_trampoline = %p, addr = %p\n", mono_interp_enter_icall_trampoline, addr);
1011 g_print ("margs(out): ilen=%d, flen=%d\n", margs->ilen, margs->flen);
1014 context->current_frame = frame;
1015 context->managed_code = 0;
1017 interp_push_lmf (&ext, frame);
1019 mono_interp_enter_icall_trampoline (addr, margs);
1021 interp_pop_lmf (&ext);
1023 context->managed_code = 1;
1025 if (*mono_thread_interruption_request_flag ()) {
1026 MonoException *exc = mono_thread_interruption_checkpoint ();
1029 context->search_for_handler = 1;
1033 if (!frame->ex && !MONO_TYPE_ISSTRUCT (sig->ret))
1034 stackval_from_data (sig->ret, frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
1036 context->current_frame = old_frame;
1037 context->env_frame = old_env_frame;
1038 context->current_env = old_env;
1040 g_free (margs->iargs);
1041 g_free (margs->fargs);
1046 mono_interp_init_delegate (MonoDelegate *del)
1050 /* shouldn't need a write barrier because we don't write a MonoObject into the field */
1051 del->method = ((InterpMethod *) del->method_ptr)->method;
1056 * runtime specifies that the implementation of the method is automatically
1057 * provided by the runtime and is primarily used for the methods of delegates.
1060 ves_imethod (InterpFrame *frame, ThreadContext *context)
1062 MonoMethod *method = frame->imethod->method;
1063 const char *name = method->name;
1064 MonoObject *obj = (MonoObject*) frame->stack_args->data.p;
1065 MonoObject *isinst_obj;
1068 mono_class_init (method->klass);
1070 if (method->klass == mono_defaults.array_class) {
1071 if (!strcmp (method->name, "UnsafeMov")) {
1072 /* TODO: layout checks */
1073 MonoType *mt = mono_method_signature (method)->ret;
1074 stackval_from_data (mt, frame->retval, (char *) frame->stack_args, FALSE);
1077 if (!strcmp (method->name, "UnsafeLoad")) {
1078 ves_array_get (frame, FALSE);
1083 isinst_obj = mono_object_isinst_checked (obj, mono_defaults.array_class, &error);
1084 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1085 if (obj && isinst_obj) {
1086 if (*name == 'S' && (strcmp (name, "Set") == 0)) {
1087 ves_array_set (frame);
1090 if (*name == 'G' && (strcmp (name, "Get") == 0)) {
1091 ves_array_get (frame, TRUE);
1096 g_error ("Don't know how to exec runtime method %s.%s::%s",
1097 method->klass->name_space, method->klass->name,
1103 dump_stack (stackval *stack, stackval *sp)
1105 stackval *s = stack;
1106 GString *str = g_string_new ("");
1109 return g_string_free (str, FALSE);
1112 g_string_append_printf (str, "[%p (%lld)] ", s->data.l, s->data.l);
1115 return g_string_free (str, FALSE);
1120 dump_stackval (GString *str, stackval *s, MonoType *type)
1122 switch (type->type) {
1129 case MONO_TYPE_CHAR:
1130 case MONO_TYPE_BOOLEAN:
1131 g_string_append_printf (str, "[%d] ", s->data.i);
1133 case MONO_TYPE_STRING:
1134 case MONO_TYPE_SZARRAY:
1135 case MONO_TYPE_CLASS:
1136 case MONO_TYPE_OBJECT:
1137 case MONO_TYPE_ARRAY:
1141 g_string_append_printf (str, "[%p] ", s->data.p);
1143 case MONO_TYPE_VALUETYPE:
1144 if (type->data.klass->enumtype)
1145 g_string_append_printf (str, "[%d] ", s->data.i);
1147 g_string_append_printf (str, "[vt:%p] ", s->data.p);
1151 g_string_append_printf (str, "[%g] ", s->data.f);
1156 GString *res = g_string_new ("");
1157 mono_type_get_desc (res, type, TRUE);
1158 g_string_append_printf (str, "[{%s} %lld/0x%0llx] ", res->str, s->data.l, s->data.l);
1159 g_string_free (res, TRUE);
1167 dump_retval (InterpFrame *inv)
1169 GString *str = g_string_new ("");
1170 MonoType *ret = mono_method_signature (inv->imethod->method)->ret;
1172 if (ret->type != MONO_TYPE_VOID)
1173 dump_stackval (str, inv->retval, ret);
1175 return g_string_free (str, FALSE);
1180 dump_args (InterpFrame *inv)
1182 GString *str = g_string_new ("");
1184 MonoMethodSignature *signature = mono_method_signature (inv->imethod->method);
1186 if (signature->param_count == 0 && !signature->hasthis)
1187 return g_string_free (str, FALSE);
1189 if (signature->hasthis) {
1190 MonoMethod *method = inv->imethod->method;
1191 dump_stackval (str, inv->stack_args, &method->klass->byval_arg);
1194 for (i = 0; i < signature->param_count; ++i)
1195 dump_stackval (str, inv->stack_args + (!!signature->hasthis) + i, signature->params [i]);
1197 return g_string_free (str, FALSE);
1201 dump_frame (InterpFrame *inv)
1203 GString *str = g_string_new ("");
1208 for (i = 0; inv; inv = inv->parent) {
1209 if (inv->imethod != NULL) {
1210 MonoMethod *method = inv->imethod->method;
1214 const char * opname = "";
1216 gchar *source = NULL;
1220 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
1221 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) == 0) {
1222 MonoMethodHeader *hd = mono_method_get_header_checked (method, &error);
1223 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1227 opname = mono_interp_opname [*inv->ip];
1228 codep = inv->ip - inv->imethod->code;
1229 source = g_strdup_printf ("%s:%d // (TODO: proper stacktrace)", method->name, codep);
1234 MonoDebugSourceLocation *minfo = mono_debug_lookup_method (method);
1235 source = mono_debug_method_lookup_location (minfo, codep);
1239 args = dump_args (inv);
1240 name = mono_method_full_name (method, TRUE);
1242 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s) at %s\n", i, codep, opname, name, args, source);
1244 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s)\n", i, codep, opname, name, args);
1251 return g_string_free (str, FALSE);
1255 get_trace_ips (MonoDomain *domain, InterpFrame *top)
1262 for (i = 0, inv = top; inv; inv = inv->parent)
1263 if (inv->imethod != NULL)
1266 res = mono_array_new_checked (domain, mono_defaults.int_class, 2 * i, &error);
1267 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1269 for (i = 0, inv = top; inv; inv = inv->parent)
1270 if (inv->imethod != NULL) {
1271 mono_array_set (res, gpointer, i, inv->imethod);
1273 mono_array_set (res, gpointer, i, (gpointer)inv->ip);
1281 #define MYGUINT64_MAX 18446744073709551615ULL
1282 #define MYGINT64_MAX 9223372036854775807LL
1283 #define MYGINT64_MIN (-MYGINT64_MAX -1LL)
1285 #define MYGUINT32_MAX 4294967295U
1286 #define MYGINT32_MAX 2147483647
1287 #define MYGINT32_MIN (-MYGINT32_MAX -1)
1289 #define CHECK_ADD_OVERFLOW(a,b) \
1290 (gint32)(b) >= 0 ? (gint32)(MYGINT32_MAX) - (gint32)(b) < (gint32)(a) ? -1 : 0 \
1291 : (gint32)(MYGINT32_MIN) - (gint32)(b) > (gint32)(a) ? +1 : 0
1293 #define CHECK_SUB_OVERFLOW(a,b) \
1294 (gint32)(b) < 0 ? (gint32)(MYGINT32_MAX) + (gint32)(b) < (gint32)(a) ? -1 : 0 \
1295 : (gint32)(MYGINT32_MIN) + (gint32)(b) > (gint32)(a) ? +1 : 0
1297 #define CHECK_ADD_OVERFLOW_UN(a,b) \
1298 (guint32)(MYGUINT32_MAX) - (guint32)(b) < (guint32)(a) ? -1 : 0
1300 #define CHECK_SUB_OVERFLOW_UN(a,b) \
1301 (guint32)(a) < (guint32)(b) ? -1 : 0
1303 #define CHECK_ADD_OVERFLOW64(a,b) \
1304 (gint64)(b) >= 0 ? (gint64)(MYGINT64_MAX) - (gint64)(b) < (gint64)(a) ? -1 : 0 \
1305 : (gint64)(MYGINT64_MIN) - (gint64)(b) > (gint64)(a) ? +1 : 0
1307 #define CHECK_SUB_OVERFLOW64(a,b) \
1308 (gint64)(b) < 0 ? (gint64)(MYGINT64_MAX) + (gint64)(b) < (gint64)(a) ? -1 : 0 \
1309 : (gint64)(MYGINT64_MIN) + (gint64)(b) > (gint64)(a) ? +1 : 0
1311 #define CHECK_ADD_OVERFLOW64_UN(a,b) \
1312 (guint64)(MYGUINT64_MAX) - (guint64)(b) < (guint64)(a) ? -1 : 0
1314 #define CHECK_SUB_OVERFLOW64_UN(a,b) \
1315 (guint64)(a) < (guint64)(b) ? -1 : 0
1317 #if SIZEOF_VOID_P == 4
1318 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW(a,b)
1319 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW_UN(a,b)
1321 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW64(a,b)
1322 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW64_UN(a,b)
1325 /* Resolves to TRUE if the operands would overflow */
1326 #define CHECK_MUL_OVERFLOW(a,b) \
1327 ((gint32)(a) == 0) || ((gint32)(b) == 0) ? 0 : \
1328 (((gint32)(a) > 0) && ((gint32)(b) == -1)) ? FALSE : \
1329 (((gint32)(a) < 0) && ((gint32)(b) == -1)) ? (a == - MYGINT32_MAX) : \
1330 (((gint32)(a) > 0) && ((gint32)(b) > 0)) ? (gint32)(a) > ((MYGINT32_MAX) / (gint32)(b)) : \
1331 (((gint32)(a) > 0) && ((gint32)(b) < 0)) ? (gint32)(a) > ((MYGINT32_MIN) / (gint32)(b)) : \
1332 (((gint32)(a) < 0) && ((gint32)(b) > 0)) ? (gint32)(a) < ((MYGINT32_MIN) / (gint32)(b)) : \
1333 (gint32)(a) < ((MYGINT32_MAX) / (gint32)(b))
1335 #define CHECK_MUL_OVERFLOW_UN(a,b) \
1336 ((guint32)(a) == 0) || ((guint32)(b) == 0) ? 0 : \
1337 (guint32)(b) > ((MYGUINT32_MAX) / (guint32)(a))
1339 #define CHECK_MUL_OVERFLOW64(a,b) \
1340 ((gint64)(a) == 0) || ((gint64)(b) == 0) ? 0 : \
1341 (((gint64)(a) > 0) && ((gint64)(b) == -1)) ? FALSE : \
1342 (((gint64)(a) < 0) && ((gint64)(b) == -1)) ? (a == - MYGINT64_MAX) : \
1343 (((gint64)(a) > 0) && ((gint64)(b) > 0)) ? (gint64)(a) > ((MYGINT64_MAX) / (gint64)(b)) : \
1344 (((gint64)(a) > 0) && ((gint64)(b) < 0)) ? (gint64)(a) > ((MYGINT64_MIN) / (gint64)(b)) : \
1345 (((gint64)(a) < 0) && ((gint64)(b) > 0)) ? (gint64)(a) < ((MYGINT64_MIN) / (gint64)(b)) : \
1346 (gint64)(a) < ((MYGINT64_MAX) / (gint64)(b))
1348 #define CHECK_MUL_OVERFLOW64_UN(a,b) \
1349 ((guint64)(a) == 0) || ((guint64)(b) == 0) ? 0 : \
1350 (guint64)(b) > ((MYGUINT64_MAX) / (guint64)(a))
1352 #if SIZEOF_VOID_P == 4
1353 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW(a,b)
1354 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW_UN(a,b)
1356 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW64(a,b)
1357 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW64_UN(a,b)
1361 mono_interp_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc, MonoError *error)
1364 ThreadContext * volatile context = mono_native_tls_get_value (thread_context_id);
1365 MonoObject *retval = NULL;
1366 MonoMethodSignature *sig = mono_method_signature (method);
1367 MonoClass *klass = mono_class_from_mono_type (sig->ret);
1370 ThreadContext context_struct;
1371 InterpFrame *old_frame = NULL;
1381 if (context != &context_struct) {
1382 context->current_frame = old_frame;
1383 context->managed_code = 0;
1387 *exc = (MonoObject *)frame.ex;
1391 if (context == NULL) {
1392 context = &context_struct;
1393 memset (context, 0, sizeof (ThreadContext));
1394 context_struct.base_frame = &frame;
1395 context_struct.env_frame = &frame;
1396 context_struct.current_env = &env;
1397 set_context (context);
1400 old_frame = context->current_frame;
1402 MonoDomain *domain = mono_domain_get ();
1404 MonoMethod *invoke_wrapper = mono_marshal_get_runtime_invoke_full (method, FALSE, TRUE);
1406 //* <code>MonoObject *runtime_invoke (MonoObject *this_obj, void **params, MonoObject **exc, void* method)</code>
1408 result.data.vt = alloca (mono_class_instance_size (klass));
1409 args = alloca (sizeof (stackval) * 4);
1412 args [0].data.p = obj;
1414 args [0].data.p = NULL;
1415 args [1].data.p = params;
1416 args [2].data.p = exc;
1417 args [3].data.p = method;
1419 INIT_FRAME (&frame, context->current_frame, args, &result, domain, invoke_wrapper, error);
1422 frame.invoke_trap = 1;
1423 context->managed_code = 1;
1424 ves_exec_method_with_context (&frame, context, NULL, NULL, -1);
1425 context->managed_code = 0;
1426 if (context == &context_struct)
1429 context->current_frame = old_frame;
1430 if (frame.ex != NULL) {
1432 *exc = (MonoObject*) frame.ex;
1435 if (context->current_env != NULL) {
1436 context->env_frame->ex = frame.ex;
1437 longjmp(*context->current_env, 1);
1440 printf("dropped exception...\n");
1442 return result.data.p;
1446 InterpMethod *rmethod;
1450 gpointer *many_args;
1453 /* Main function for entering the interpreter from compiled code */
1455 interp_entry (InterpEntryData *data)
1458 InterpMethod *rmethod = data->rmethod;
1459 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
1460 ThreadContext context_struct;
1461 InterpFrame *old_frame;
1465 MonoMethodSignature *sig;
1469 method = rmethod->method;
1470 sig = mono_method_signature (method);
1472 // FIXME: Optimize this
1474 //printf ("%s\n", mono_method_full_name (method, 1));
1477 if (context == NULL) {
1478 context = &context_struct;
1479 memset (context, 0, sizeof (ThreadContext));
1480 context_struct.base_frame = &frame;
1481 context_struct.env_frame = &frame;
1482 set_context (context);
1484 old_frame = context->current_frame;
1487 args = alloca (sizeof (stackval) * (sig->param_count + (sig->hasthis ? 1 : 0)));
1489 args [0].data.p = data->this_arg;
1492 if (data->many_args)
1493 params = data->many_args;
1495 params = data->args;
1496 for (i = 0; i < sig->param_count; ++i) {
1497 int a_index = i + (sig->hasthis ? 1 : 0);
1498 if (sig->params [i]->byref) {
1499 args [a_index].data.p = params [i];
1502 type = rmethod->param_types [i];
1503 switch (type->type) {
1506 args [a_index].data.i = *(MonoBoolean*)params [i];
1510 args [a_index].data.i = *(gint16*)params [i];
1513 #if SIZEOF_VOID_P == 4
1514 args [a_index].data.p = GINT_TO_POINTER (*(guint32*)params [i]);
1516 args [a_index].data.p = GINT_TO_POINTER (*(guint64*)params [i]);
1520 #if SIZEOF_VOID_P == 4
1521 args [a_index].data.p = GINT_TO_POINTER (*(gint32*)params [i]);
1523 args [a_index].data.p = GINT_TO_POINTER (*(gint64*)params [i]);
1527 args [a_index].data.i = *(guint32*)params [i];
1530 args [a_index].data.i = *(gint32*)params [i];
1533 args [a_index].data.l = *(guint64*)params [i];
1536 args [a_index].data.l = *(gint64*)params [i];
1539 case MONO_TYPE_OBJECT:
1540 args [a_index].data.p = *(MonoObject**)params [i];
1542 case MONO_TYPE_VALUETYPE:
1543 args [a_index].data.p = params [i];
1545 case MONO_TYPE_GENERICINST:
1546 if (MONO_TYPE_IS_REFERENCE (type))
1547 args [a_index].data.p = params [i];
1549 args [a_index].data.vt = params [i];
1552 printf ("%s\n", mono_type_full_name (sig->params [i]));
1558 init_frame (&frame, NULL, data->rmethod, args, &result);
1559 context->managed_code = 1;
1561 type = rmethod->rtype;
1562 switch (type->type) {
1563 case MONO_TYPE_GENERICINST:
1564 if (!MONO_TYPE_IS_REFERENCE (type))
1565 frame.retval->data.vt = data->res;
1567 case MONO_TYPE_VALUETYPE:
1568 frame.retval->data.vt = data->res;
1574 ves_exec_method_with_context (&frame, context, NULL, NULL, -1);
1575 context->managed_code = 0;
1576 if (context == &context_struct)
1579 context->current_frame = old_frame;
1582 g_assert (frame.ex == NULL);
1584 type = rmethod->rtype;
1585 switch (type->type) {
1586 case MONO_TYPE_VOID:
1589 *(gint8*)data->res = frame.retval->data.i;
1592 *(guint8*)data->res = frame.retval->data.i;
1595 *(gint16*)data->res = frame.retval->data.i;
1598 *(guint16*)data->res = frame.retval->data.i;
1601 *(gint32*)data->res = frame.retval->data.i;
1604 *(guint64*)data->res = frame.retval->data.i;
1607 *(gint64*)data->res = frame.retval->data.i;
1610 *(guint64*)data->res = frame.retval->data.i;
1613 #if SIZEOF_VOID_P == 8
1614 *(gint64*)data->res = (gint64)frame.retval->data.p;
1616 *(gint32*)data->res = (gint32)frame.retval->data.p;
1620 #if SIZEOF_VOID_P == 8
1621 *(guint64*)data->res = (guint64)frame.retval->data.p;
1623 *(guint32*)data->res = (guint32)frame.retval->data.p;
1626 case MONO_TYPE_OBJECT:
1627 /* No need for a write barrier */
1628 *(MonoObject**)data->res = (MonoObject*)frame.retval->data.p;
1630 case MONO_TYPE_GENERICINST:
1631 if (MONO_TYPE_IS_REFERENCE (type)) {
1632 *(MonoObject**)data->res = *(MonoObject**)frame.retval->data.p;
1634 /* Already set before the call */
1637 case MONO_TYPE_VALUETYPE:
1638 /* Already set before the call */
1641 printf ("%s\n", mono_type_full_name (sig->ret));
1648 do_icall (ThreadContext *context, int op, stackval *sp, gpointer ptr)
1650 InterpFrame *old_frame = context->current_frame;
1651 InterpFrame *old_env_frame = context->env_frame;
1652 jmp_buf *old_env = context->current_env;
1656 context->current_frame = old_frame;
1657 context->env_frame = old_env_frame;
1658 context->current_env = old_env;
1659 context->managed_code = 1;
1663 context->env_frame = context->current_frame;
1664 context->current_env = &env;
1665 context->managed_code = 0;
1668 case MINT_ICALL_V_V: {
1669 void (*func)(void) = ptr;
1673 case MINT_ICALL_V_P: {
1674 gpointer (*func)(void) = ptr;
1676 sp [-1].data.p = func ();
1679 case MINT_ICALL_P_V: {
1680 void (*func)(gpointer) = ptr;
1681 func (sp [-1].data.p);
1685 case MINT_ICALL_P_P: {
1686 gpointer (*func)(gpointer) = ptr;
1687 sp [-1].data.p = func (sp [-1].data.p);
1690 case MINT_ICALL_PP_V: {
1691 void (*func)(gpointer,gpointer) = ptr;
1693 func (sp [0].data.p, sp [1].data.p);
1696 case MINT_ICALL_PI_V: {
1697 void (*func)(gpointer,int) = ptr;
1699 func (sp [0].data.p, sp [1].data.i);
1702 case MINT_ICALL_PP_P: {
1703 gpointer (*func)(gpointer,gpointer) = ptr;
1705 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.p);
1708 case MINT_ICALL_PI_P: {
1709 gpointer (*func)(gpointer,int) = ptr;
1711 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.i);
1714 case MINT_ICALL_PPP_V: {
1715 void (*func)(gpointer,gpointer,gpointer) = ptr;
1717 func (sp [0].data.p, sp [1].data.p, sp [2].data.p);
1720 case MINT_ICALL_PPI_V: {
1721 void (*func)(gpointer,gpointer,int) = ptr;
1723 func (sp [0].data.p, sp [1].data.p, sp [2].data.i);
1727 g_assert_not_reached ();
1730 context->env_frame = old_env_frame;
1731 context->current_env = old_env;
1737 do_jit_call (stackval *sp, unsigned char *vt_sp, ThreadContext *context, InterpFrame *frame, InterpMethod *rmethod)
1739 MonoMethodSignature *sig;
1740 MonoFtnDesc ftndesc;
1741 guint8 res_buf [256];
1745 //printf ("%s\n", mono_method_full_name (rmethod->method, 1));
1748 * Call JITted code through a gsharedvt_out wrapper. These wrappers receive every argument
1749 * by ref and return a return value using an explicit return value argument.
1751 if (!rmethod->jit_wrapper) {
1752 MonoMethod *method = rmethod->method;
1755 sig = mono_method_signature (method);
1758 MonoMethod *wrapper = mini_get_gsharedvt_out_sig_wrapper (sig);
1759 //printf ("J: %s %s\n", mono_method_full_name (method, 1), mono_method_full_name (wrapper, 1));
1761 gpointer jit_wrapper = mono_jit_compile_method_jit_only (wrapper, &error);
1762 mono_error_assert_ok (&error);
1764 gpointer addr = mono_jit_compile_method_jit_only (method, &error);
1766 mono_error_assert_ok (&error);
1768 rmethod->jit_addr = addr;
1769 rmethod->jit_sig = sig;
1770 mono_memory_barrier ();
1771 rmethod->jit_wrapper = jit_wrapper;
1774 sig = rmethod->jit_sig;
1777 sp -= sig->param_count;
1781 ftndesc.addr = rmethod->jit_addr;
1784 // FIXME: Optimize this
1788 int stack_index = 0;
1789 if (rmethod->hasthis) {
1790 args [pindex ++] = sp [0].data.p;
1793 type = rmethod->rtype;
1794 if (type->type != MONO_TYPE_VOID) {
1795 if (MONO_TYPE_ISSTRUCT (type))
1796 args [pindex ++] = vt_sp;
1798 args [pindex ++] = res_buf;
1800 for (int i = 0; i < rmethod->param_count; ++i) {
1801 MonoType *t = rmethod->param_types [i];
1802 stackval *sval = &sp [stack_index + i];
1803 if (sig->params [i]->byref) {
1804 args [pindex ++] = sval->data.p;
1805 } else if (MONO_TYPE_ISSTRUCT (t)) {
1806 args [pindex ++] = sval->data.p;
1807 } else if (MONO_TYPE_IS_REFERENCE (t)) {
1808 args [pindex ++] = &sval->data.p;
1817 case MONO_TYPE_VALUETYPE:
1818 args [pindex ++] = &sval->data.i;
1821 case MONO_TYPE_FNPTR:
1824 case MONO_TYPE_OBJECT:
1825 args [pindex ++] = &sval->data.p;
1829 args [pindex ++] = &sval->data.l;
1832 printf ("%s\n", mono_type_full_name (t));
1833 g_assert_not_reached ();
1838 interp_push_lmf (&ext, frame);
1842 void (*func)(gpointer) = rmethod->jit_wrapper;
1848 void (*func)(gpointer, gpointer) = rmethod->jit_wrapper;
1850 func (args [0], &ftndesc);
1854 void (*func)(gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
1856 func (args [0], args [1], &ftndesc);
1860 void (*func)(gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
1862 func (args [0], args [1], args [2], &ftndesc);
1866 void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
1868 func (args [0], args [1], args [2], args [3], &ftndesc);
1872 void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
1874 func (args [0], args [1], args [2], args [3], args [4], &ftndesc);
1878 void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
1880 func (args [0], args [1], args [2], args [3], args [4], args [5], &ftndesc);
1884 void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
1886 func (args [0], args [1], args [2], args [3], args [4], args [5], args [6], &ftndesc);
1890 g_assert_not_reached ();
1894 interp_pop_lmf (&ext);
1896 MonoType *rtype = rmethod->rtype;
1897 switch (rtype->type) {
1898 case MONO_TYPE_VOID:
1899 case MONO_TYPE_OBJECT:
1900 case MONO_TYPE_STRING:
1901 case MONO_TYPE_CLASS:
1902 case MONO_TYPE_ARRAY:
1903 case MONO_TYPE_SZARRAY:
1906 sp->data.p = *(gpointer*)res_buf;
1909 sp->data.i = *(gint8*)res_buf;
1912 sp->data.i = *(guint8*)res_buf;
1915 sp->data.i = *(gint16*)res_buf;
1918 sp->data.i = *(guint16*)res_buf;
1921 sp->data.i = *(gint32*)res_buf;
1924 sp->data.i = *(guint32*)res_buf;
1926 case MONO_TYPE_VALUETYPE:
1927 /* The result was written to vt_sp */
1930 case MONO_TYPE_GENERICINST:
1931 if (MONO_TYPE_IS_REFERENCE (rtype)) {
1932 sp->data.p = *(gpointer*)res_buf;
1934 /* The result was written to vt_sp */
1939 printf ("%s\n", mono_type_full_name (rtype));
1940 g_assert_not_reached ();
1948 do_debugger_tramp (void (*tramp) (void), InterpFrame *frame)
1951 interp_push_lmf (&ext, frame);
1953 interp_pop_lmf (&ext);
1957 do_transform_method (InterpFrame *frame, ThreadContext *context)
1961 /* Use the parent frame as the current frame is not complete yet */
1962 interp_push_lmf (&ext, frame->parent);
1964 frame->ex = mono_interp_transform_method (frame->imethod, context);
1965 context->managed_code = 1;
1967 interp_pop_lmf (&ext);
1971 * These functions are the entry points into the interpreter from compiled code.
1972 * They are called by the interp_in wrappers. They have the following signature:
1973 * void (<optional this_arg>, <optional retval pointer>, <arg1>, ..., <argn>, <method ptr>)
1974 * They pack up their arguments into an InterpEntryData structure and call interp_entry ().
1975 * It would be possible for the wrappers to pack up the arguments etc, but that would make them bigger, and there are
1976 * more wrappers then these functions.
1977 * this/static * ret/void * 16 arguments -> 64 functions.
1980 #define MAX_INTERP_ENTRY_ARGS 8
1982 #define INTERP_ENTRY_BASE(_method, _this_arg, _res) \
1983 InterpEntryData data; \
1984 (data).rmethod = (_method); \
1985 (data).res = (_res); \
1986 (data).this_arg = (_this_arg); \
1987 (data).many_args = NULL;
1989 #define INTERP_ENTRY0(_this_arg, _res, _method) { \
1990 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
1991 interp_entry (&data); \
1993 #define INTERP_ENTRY1(_this_arg, _res, _method) { \
1994 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
1995 (data).args [0] = arg1; \
1996 interp_entry (&data); \
1998 #define INTERP_ENTRY2(_this_arg, _res, _method) { \
1999 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2000 (data).args [0] = arg1; \
2001 (data).args [1] = arg2; \
2002 interp_entry (&data); \
2004 #define INTERP_ENTRY3(_this_arg, _res, _method) { \
2005 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2006 (data).args [0] = arg1; \
2007 (data).args [1] = arg2; \
2008 (data).args [2] = arg3; \
2009 interp_entry (&data); \
2011 #define INTERP_ENTRY4(_this_arg, _res, _method) { \
2012 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2013 (data).args [0] = arg1; \
2014 (data).args [1] = arg2; \
2015 (data).args [2] = arg3; \
2016 (data).args [3] = arg4; \
2017 interp_entry (&data); \
2019 #define INTERP_ENTRY5(_this_arg, _res, _method) { \
2020 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2021 (data).args [0] = arg1; \
2022 (data).args [1] = arg2; \
2023 (data).args [2] = arg3; \
2024 (data).args [3] = arg4; \
2025 (data).args [4] = arg5; \
2026 interp_entry (&data); \
2028 #define INTERP_ENTRY6(_this_arg, _res, _method) { \
2029 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2030 (data).args [0] = arg1; \
2031 (data).args [1] = arg2; \
2032 (data).args [2] = arg3; \
2033 (data).args [3] = arg4; \
2034 (data).args [4] = arg5; \
2035 (data).args [5] = arg6; \
2036 interp_entry (&data); \
2038 #define INTERP_ENTRY7(_this_arg, _res, _method) { \
2039 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2040 (data).args [0] = arg1; \
2041 (data).args [1] = arg2; \
2042 (data).args [2] = arg3; \
2043 (data).args [3] = arg4; \
2044 (data).args [4] = arg5; \
2045 (data).args [5] = arg6; \
2046 (data).args [6] = arg7; \
2047 interp_entry (&data); \
2049 #define INTERP_ENTRY8(_this_arg, _res, _method) { \
2050 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2051 (data).args [0] = arg1; \
2052 (data).args [1] = arg2; \
2053 (data).args [2] = arg3; \
2054 (data).args [3] = arg4; \
2055 (data).args [4] = arg5; \
2056 (data).args [5] = arg6; \
2057 (data).args [6] = arg7; \
2058 (data).args [7] = arg8; \
2059 interp_entry (&data); \
2062 #define ARGLIST0 InterpMethod *rmethod
2063 #define ARGLIST1 gpointer arg1, InterpMethod *rmethod
2064 #define ARGLIST2 gpointer arg1, gpointer arg2, InterpMethod *rmethod
2065 #define ARGLIST3 gpointer arg1, gpointer arg2, gpointer arg3, InterpMethod *rmethod
2066 #define ARGLIST4 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, InterpMethod *rmethod
2067 #define ARGLIST5 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, InterpMethod *rmethod
2068 #define ARGLIST6 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, gpointer arg6, InterpMethod *rmethod
2069 #define ARGLIST7 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, gpointer arg6, gpointer arg7, InterpMethod *rmethod
2070 #define ARGLIST8 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, gpointer arg6, gpointer arg7, gpointer arg8, InterpMethod *rmethod
2072 static void interp_entry_static_0 (ARGLIST0) INTERP_ENTRY0 (NULL, NULL, rmethod)
2073 static void interp_entry_static_1 (ARGLIST1) INTERP_ENTRY1 (NULL, NULL, rmethod)
2074 static void interp_entry_static_2 (ARGLIST2) INTERP_ENTRY2 (NULL, NULL, rmethod)
2075 static void interp_entry_static_3 (ARGLIST3) INTERP_ENTRY3 (NULL, NULL, rmethod)
2076 static void interp_entry_static_4 (ARGLIST4) INTERP_ENTRY4 (NULL, NULL, rmethod)
2077 static void interp_entry_static_5 (ARGLIST5) INTERP_ENTRY5 (NULL, NULL, rmethod)
2078 static void interp_entry_static_6 (ARGLIST6) INTERP_ENTRY6 (NULL, NULL, rmethod)
2079 static void interp_entry_static_7 (ARGLIST7) INTERP_ENTRY7 (NULL, NULL, rmethod)
2080 static void interp_entry_static_8 (ARGLIST8) INTERP_ENTRY8 (NULL, NULL, rmethod)
2081 static void interp_entry_static_ret_0 (gpointer res, ARGLIST0) INTERP_ENTRY0 (NULL, res, rmethod)
2082 static void interp_entry_static_ret_1 (gpointer res, ARGLIST1) INTERP_ENTRY1 (NULL, res, rmethod)
2083 static void interp_entry_static_ret_2 (gpointer res, ARGLIST2) INTERP_ENTRY2 (NULL, res, rmethod)
2084 static void interp_entry_static_ret_3 (gpointer res, ARGLIST3) INTERP_ENTRY3 (NULL, res, rmethod)
2085 static void interp_entry_static_ret_4 (gpointer res, ARGLIST4) INTERP_ENTRY4 (NULL, res, rmethod)
2086 static void interp_entry_static_ret_5 (gpointer res, ARGLIST5) INTERP_ENTRY5 (NULL, res, rmethod)
2087 static void interp_entry_static_ret_6 (gpointer res, ARGLIST6) INTERP_ENTRY6 (NULL, res, rmethod)
2088 static void interp_entry_static_ret_7 (gpointer res, ARGLIST7) INTERP_ENTRY7 (NULL, res, rmethod)
2089 static void interp_entry_static_ret_8 (gpointer res, ARGLIST8) INTERP_ENTRY8 (NULL, res, rmethod)
2090 static void interp_entry_instance_0 (gpointer this_arg, ARGLIST0) INTERP_ENTRY0 (this_arg, NULL, rmethod)
2091 static void interp_entry_instance_1 (gpointer this_arg, ARGLIST1) INTERP_ENTRY1 (this_arg, NULL, rmethod)
2092 static void interp_entry_instance_2 (gpointer this_arg, ARGLIST2) INTERP_ENTRY2 (this_arg, NULL, rmethod)
2093 static void interp_entry_instance_3 (gpointer this_arg, ARGLIST3) INTERP_ENTRY3 (this_arg, NULL, rmethod)
2094 static void interp_entry_instance_4 (gpointer this_arg, ARGLIST4) INTERP_ENTRY4 (this_arg, NULL, rmethod)
2095 static void interp_entry_instance_5 (gpointer this_arg, ARGLIST5) INTERP_ENTRY5 (this_arg, NULL, rmethod)
2096 static void interp_entry_instance_6 (gpointer this_arg, ARGLIST6) INTERP_ENTRY6 (this_arg, NULL, rmethod)
2097 static void interp_entry_instance_7 (gpointer this_arg, ARGLIST7) INTERP_ENTRY7 (this_arg, NULL, rmethod)
2098 static void interp_entry_instance_8 (gpointer this_arg, ARGLIST8) INTERP_ENTRY8 (this_arg, NULL, rmethod)
2099 static void interp_entry_instance_ret_0 (gpointer this_arg, gpointer res, ARGLIST0) INTERP_ENTRY0 (this_arg, res, rmethod)
2100 static void interp_entry_instance_ret_1 (gpointer this_arg, gpointer res, ARGLIST1) INTERP_ENTRY1 (this_arg, res, rmethod)
2101 static void interp_entry_instance_ret_2 (gpointer this_arg, gpointer res, ARGLIST2) INTERP_ENTRY2 (this_arg, res, rmethod)
2102 static void interp_entry_instance_ret_3 (gpointer this_arg, gpointer res, ARGLIST3) INTERP_ENTRY3 (this_arg, res, rmethod)
2103 static void interp_entry_instance_ret_4 (gpointer this_arg, gpointer res, ARGLIST4) INTERP_ENTRY4 (this_arg, res, rmethod)
2104 static void interp_entry_instance_ret_5 (gpointer this_arg, gpointer res, ARGLIST5) INTERP_ENTRY5 (this_arg, res, rmethod)
2105 static void interp_entry_instance_ret_6 (gpointer this_arg, gpointer res, ARGLIST6) INTERP_ENTRY6 (this_arg, res, rmethod)
2106 static void interp_entry_instance_ret_7 (gpointer this_arg, gpointer res, ARGLIST7) INTERP_ENTRY6 (this_arg, res, rmethod)
2107 static void interp_entry_instance_ret_8 (gpointer this_arg, gpointer res, ARGLIST8) INTERP_ENTRY6 (this_arg, res, rmethod)
2109 #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
2111 gpointer entry_funcs_static [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (static) };
2112 gpointer entry_funcs_static_ret [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (static_ret) };
2113 gpointer entry_funcs_instance [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (instance) };
2114 gpointer entry_funcs_instance_ret [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (instance_ret) };
2116 /* General version for methods with more than MAX_INTERP_ENTRY_ARGS arguments */
2118 interp_entry_general (gpointer this_arg, gpointer res, gpointer *args, gpointer rmethod)
2120 INTERP_ENTRY_BASE (rmethod, this_arg, res);
2121 data.many_args = args;
2122 interp_entry (&data);
2126 * mono_interp_create_method_pointer:
2128 * Return a function pointer which can be used to call METHOD using the
2129 * interpreter. Return NULL for methods which are not supported.
2132 mono_interp_create_method_pointer (MonoMethod *method, MonoError *error)
2135 MonoMethodSignature *sig = mono_method_signature (method);
2136 MonoMethod *wrapper;
2137 InterpMethod *rmethod = mono_interp_get_imethod (mono_domain_get (), method, error);
2139 /* HACK: method_ptr of delegate should point to a runtime method*/
2140 if (method->wrapper_type && method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
2143 if (rmethod->jit_entry)
2144 return rmethod->jit_entry;
2145 wrapper = mini_get_interp_in_wrapper (sig);
2147 gpointer jit_wrapper = mono_jit_compile_method_jit_only (wrapper, error);
2148 mono_error_assert_ok (error);
2150 //printf ("%s %s\n", mono_method_full_name (method, 1), mono_method_full_name (wrapper, 1));
2151 gpointer entry_func;
2152 if (sig->param_count > MAX_INTERP_ENTRY_ARGS) {
2153 entry_func = interp_entry_general;
2154 } else if (sig->hasthis) {
2155 if (sig->ret->type == MONO_TYPE_VOID)
2156 entry_func = entry_funcs_instance [sig->param_count];
2158 entry_func = entry_funcs_instance_ret [sig->param_count];
2160 if (sig->ret->type == MONO_TYPE_VOID)
2161 entry_func = entry_funcs_static [sig->param_count];
2163 entry_func = entry_funcs_static_ret [sig->param_count];
2165 g_assert (entry_func);
2167 /* This is the argument passed to the interp_in wrapper by the static rgctx trampoline */
2168 MonoFtnDesc *ftndesc = g_new0 (MonoFtnDesc, 1);
2169 ftndesc->addr = entry_func;
2170 ftndesc->arg = rmethod;
2171 mono_error_assert_ok (error);
2174 * The wrapper is called by compiled code, which doesn't pass the extra argument, so we pass it in the
2175 * rgctx register using a trampoline.
2179 addr = mono_aot_get_static_rgctx_trampoline (ftndesc, jit_wrapper);
2181 addr = mono_arch_get_static_rgctx_trampoline (ftndesc, jit_wrapper);
2183 mono_memory_barrier ();
2184 rmethod->jit_entry = addr;
2190 static int opcode_counts[512];
2192 #define COUNT_OP(op) opcode_counts[op]++
2194 #define COUNT_OP(op)
2198 #define DUMP_INSTR() \
2199 if (tracing > 1) { \
2201 if (sp > frame->stack) { \
2202 ins = dump_stack (frame->stack, sp); \
2204 ins = g_strdup (""); \
2208 char *mn = mono_method_full_name (frame->imethod->method, FALSE); \
2209 g_print ("(%p) %s -> ", mono_thread_internal_current (), mn); \
2211 mono_interp_dis_mintop(rtm->code, ip); \
2212 g_print ("\t%d:%s\n", vt_sp - vtalloc, ins); \
2216 #define DUMP_INSTR()
2220 #define USE_COMPUTED_GOTO 1
2222 #if USE_COMPUTED_GOTO
2223 #define MINT_IN_SWITCH(op) COUNT_OP(op); goto *in_labels[op];
2224 #define MINT_IN_CASE(x) LAB_ ## x:
2226 #define MINT_IN_BREAK if (tracing > 1) goto main_loop; else { COUNT_OP(*ip); goto *in_labels[*ip]; }
2228 #define MINT_IN_BREAK { COUNT_OP(*ip); goto *in_labels[*ip]; }
2230 #define MINT_IN_DEFAULT mint_default: if (0) goto mint_default; /* make gcc shut up */
2232 #define MINT_IN_SWITCH(op) switch (op)
2233 #define MINT_IN_CASE(x) case x:
2234 #define MINT_IN_BREAK break
2235 #define MINT_IN_DEFAULT default:
2239 * If EXIT_AT_FINALLY is not -1, exit after exiting the finally clause with that index.
2242 ves_exec_method_with_context (InterpFrame *frame, ThreadContext *context, unsigned short *start_with_ip, MonoException *filter_exception, int exit_at_finally)
2244 InterpFrame child_frame;
2245 GSList *finally_ips = NULL;
2246 const unsigned short *endfinally_ip = NULL;
2247 const unsigned short *ip = NULL;
2248 register stackval *sp;
2251 gint tracing = global_tracing;
2252 unsigned char *vtalloc;
2257 unsigned char *vt_sp;
2258 unsigned char *locals;
2260 MonoObject *o = NULL;
2262 #if USE_COMPUTED_GOTO
2263 static void *in_labels[] = {
2264 #define OPDEF(a,b,c,d) \
2266 #include "mintops.def"
2271 frame->ex_handler = NULL;
2273 context->current_frame = frame;
2275 debug_enter (frame, &tracing);
2277 if (!frame->imethod->transformed) {
2278 context->managed_code = 0;
2280 char *mn = mono_method_full_name (frame->imethod->method, TRUE);
2281 g_print ("(%p) Transforming %s\n", mono_thread_internal_current (), mn);
2285 do_transform_method (frame, context);
2293 rtm = frame->imethod;
2294 if (!start_with_ip ) {
2295 frame->args = alloca (rtm->alloca_size);
2296 memset (frame->args, 0, rtm->alloca_size);
2302 sp = frame->stack = (stackval *) ((char *) frame->args + rtm->args_size);
2303 vt_sp = (unsigned char *) sp + rtm->stack_size;
2307 locals = (unsigned char *) vt_sp + rtm->vt_stack_size;
2308 frame->locals = locals;
2309 child_frame.parent = frame;
2311 if (filter_exception) {
2312 sp->data.p = filter_exception;
2317 * using while (ip < end) may result in a 15% performance drop,
2318 * but it may be useful for debug
2322 /* g_assert (sp >= frame->stack); */
2323 /* g_assert(vt_sp - vtalloc <= rtm->vt_stack_size); */
2325 MINT_IN_SWITCH (*ip) {
2326 MINT_IN_CASE(MINT_INITLOCALS)
2327 memset (locals, 0, rtm->locals_size);
2330 MINT_IN_CASE(MINT_NOP)
2333 MINT_IN_CASE(MINT_BREAK)
2335 do_debugger_tramp (mono_debugger_agent_user_break, frame);
2337 MINT_IN_CASE(MINT_LDNULL)
2342 MINT_IN_CASE(MINT_VTRESULT) {
2343 int ret_size = * (guint16 *)(ip + 1);
2344 unsigned char *ret_vt_sp = vt_sp;
2345 vt_sp -= READ32(ip + 2);
2347 memmove (vt_sp, ret_vt_sp, ret_size);
2348 sp [-1].data.p = vt_sp;
2349 vt_sp += (ret_size + 7) & ~7;
2354 #define LDC(n) do { sp->data.i = (n); ++ip; ++sp; } while (0)
2355 MINT_IN_CASE(MINT_LDC_I4_M1)
2358 MINT_IN_CASE(MINT_LDC_I4_0)
2361 MINT_IN_CASE(MINT_LDC_I4_1)
2364 MINT_IN_CASE(MINT_LDC_I4_2)
2367 MINT_IN_CASE(MINT_LDC_I4_3)
2370 MINT_IN_CASE(MINT_LDC_I4_4)
2373 MINT_IN_CASE(MINT_LDC_I4_5)
2376 MINT_IN_CASE(MINT_LDC_I4_6)
2379 MINT_IN_CASE(MINT_LDC_I4_7)
2382 MINT_IN_CASE(MINT_LDC_I4_8)
2385 MINT_IN_CASE(MINT_LDC_I4_S)
2386 sp->data.i = *(const short *)(ip + 1);
2390 MINT_IN_CASE(MINT_LDC_I4)
2392 sp->data.i = READ32 (ip);
2396 MINT_IN_CASE(MINT_LDC_I8)
2398 sp->data.l = READ64 (ip);
2402 MINT_IN_CASE(MINT_LDC_R4) {
2406 sp->data.f = * (float *)&val;
2411 MINT_IN_CASE(MINT_LDC_R8)
2412 sp->data.l = READ64 (ip + 1); /* note union usage */
2416 MINT_IN_CASE(MINT_DUP)
2421 MINT_IN_CASE(MINT_DUP_VT)
2422 i32 = READ32 (ip + 1);
2424 memcpy(sp->data.p, sp [-1].data.p, i32);
2425 vt_sp += (i32 + 7) & ~7;
2429 MINT_IN_CASE(MINT_POP) {
2430 guint16 u16 = (* (guint16 *)(ip + 1)) + 1;
2432 memmove (sp - u16, sp - 1, (u16 - 1) * sizeof (stackval));
2437 MINT_IN_CASE(MINT_JMP) {
2438 InterpMethod *new_method = rtm->data_items [* (guint16 *)(ip + 1)];
2439 if (!new_method->transformed) {
2441 frame->ex = mono_interp_transform_method (new_method, context);
2446 if (new_method->alloca_size > rtm->alloca_size)
2447 g_error ("MINT_JMP to method which needs more stack space (%d > %d)", new_method->alloca_size, rtm->alloca_size);
2448 rtm = frame->imethod = new_method;
2449 vt_sp = (unsigned char *) sp + rtm->stack_size;
2453 locals = vt_sp + rtm->vt_stack_size;
2454 frame->locals = locals;
2455 ip = rtm->new_body_start; /* bypass storing input args from callers frame */
2458 MINT_IN_CASE(MINT_CALLI) {
2459 MonoMethodSignature *csignature;
2460 stackval *endsp = sp;
2464 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
2468 child_frame.imethod = sp->data.p;
2471 child_frame.retval = sp;
2472 /* decrement by the actual number of args */
2473 sp -= csignature->param_count;
2474 if (csignature->hasthis)
2476 child_frame.stack_args = sp;
2478 #ifndef DISABLE_REMOTING
2479 /* `this' can be NULL for string:.ctor */
2480 if (csignature->hasthis && sp->data.p && mono_object_is_transparent_proxy (sp->data.p)) {
2481 child_frame.imethod = mono_interp_get_imethod (rtm->domain, mono_marshal_get_remoting_invoke (child_frame.imethod->method), &error);
2482 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2485 if (child_frame.imethod->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
2486 child_frame.imethod = mono_interp_get_imethod (rtm->domain, mono_marshal_get_native_wrapper (child_frame.imethod->method, FALSE, FALSE), &error);
2487 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2490 if (csignature->hasthis) {
2491 MonoObject *this_arg = sp->data.p;
2493 if (this_arg->vtable->klass->valuetype) {
2494 gpointer *unboxed = mono_object_unbox (this_arg);
2495 sp [0].data.p = unboxed;
2499 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2501 context->current_frame = frame;
2503 if (context->has_resume_state) {
2504 if (frame == context->handler_frame)
2505 SET_RESUME_STATE (context);
2510 if (child_frame.ex) {
2512 * An exception occurred, need to run finally, fault and catch handlers..
2514 frame->ex = child_frame.ex;
2515 goto handle_finally;
2518 /* need to handle typedbyref ... */
2519 if (csignature->ret->type != MONO_TYPE_VOID) {
2525 MINT_IN_CASE(MINT_CALLI_NAT) {
2526 MonoMethodSignature *csignature;
2527 stackval *endsp = sp;
2528 unsigned char *code = NULL;
2532 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
2537 child_frame.imethod = NULL;
2540 child_frame.retval = sp;
2541 /* decrement by the actual number of args */
2542 sp -= csignature->param_count;
2543 if (csignature->hasthis)
2545 child_frame.stack_args = sp;
2546 ves_pinvoke_method (&child_frame, csignature, (MonoFuncV) code, FALSE, context);
2548 context->current_frame = frame;
2550 if (context->has_resume_state) {
2551 if (frame == context->handler_frame)
2552 SET_RESUME_STATE (context);
2557 if (child_frame.ex) {
2559 * An exception occurred, need to run finally, fault and catch handlers..
2561 frame->ex = child_frame.ex;
2562 if (context->search_for_handler) {
2563 context->search_for_handler = 0;
2564 goto handle_exception;
2566 goto handle_finally;
2569 /* need to handle typedbyref ... */
2570 if (csignature->ret->type != MONO_TYPE_VOID) {
2576 MINT_IN_CASE(MINT_CALL) {
2577 stackval *endsp = sp;
2581 child_frame.imethod = rtm->data_items [* (guint16 *)(ip + 1)];
2584 child_frame.retval = sp;
2585 /* decrement by the actual number of args */
2586 sp -= child_frame.imethod->param_count;
2587 if (child_frame.imethod->hasthis)
2589 child_frame.stack_args = sp;
2591 #ifndef DISABLE_REMOTING
2592 /* `this' can be NULL for string:.ctor */
2593 if (child_frame.imethod->hasthis && !child_frame.imethod->method->klass->valuetype && sp->data.p && mono_object_is_transparent_proxy (sp->data.p)) {
2594 child_frame.imethod = mono_interp_get_imethod (rtm->domain, mono_marshal_get_remoting_invoke (child_frame.imethod->method), &error);
2595 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2599 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2601 context->current_frame = frame;
2603 if (context->has_resume_state) {
2604 if (frame == context->handler_frame)
2605 SET_RESUME_STATE (context);
2610 if (child_frame.ex) {
2612 * An exception occurred, need to run finally, fault and catch handlers..
2614 frame->ex = child_frame.ex;
2615 goto handle_exception;;
2618 /* need to handle typedbyref ... */
2623 MINT_IN_CASE(MINT_VCALL) {
2626 child_frame.imethod = rtm->data_items [* (guint16 *)(ip + 1)];
2630 child_frame.retval = sp;
2631 /* decrement by the actual number of args */
2632 sp -= child_frame.imethod->param_count;
2633 if (child_frame.imethod->hasthis) {
2635 MonoObject *this_arg = sp->data.p;
2637 THROW_EX (mono_get_exception_null_reference(), ip - 2);
2639 child_frame.stack_args = sp;
2641 #ifndef DISABLE_REMOTING
2642 if (child_frame.imethod->hasthis && !child_frame.imethod->method->klass->valuetype && mono_object_is_transparent_proxy (sp->data.p)) {
2643 child_frame.imethod = mono_interp_get_imethod (rtm->domain, mono_marshal_get_remoting_invoke (child_frame.imethod->method), &error);
2644 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2648 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2650 context->current_frame = frame;
2652 if (context->has_resume_state) {
2653 if (frame == context->handler_frame)
2654 SET_RESUME_STATE (context);
2659 if (child_frame.ex) {
2661 * An exception occurred, need to run finally, fault and catch handlers..
2663 frame->ex = child_frame.ex;
2664 goto handle_finally;
2669 MINT_IN_CASE(MINT_JIT_CALL) {
2670 InterpMethod *rmethod = rtm->data_items [* (guint16 *)(ip + 1)];
2673 sp = do_jit_call (sp, vt_sp, context, frame, rmethod);
2675 if (context->has_resume_state) {
2677 * If this bit is set, it means the call has thrown the exception, and we
2678 * reached this point because the EH code in mono_handle_exception ()
2679 * unwound all the JITted frames below us. mono_interp_set_resume_state ()
2680 * has set the fields in context to indicate where we have to resume execution.
2682 if (frame == context->handler_frame)
2683 SET_RESUME_STATE (context);
2687 if (rmethod->rtype->type != MONO_TYPE_VOID)
2693 MINT_IN_CASE(MINT_CALLVIRT) {
2694 stackval *endsp = sp;
2695 MonoObject *this_arg;
2700 token = * (unsigned short *)(ip + 1);
2702 child_frame.imethod = rtm->data_items [token];
2704 child_frame.retval = sp;
2706 /* decrement by the actual number of args */
2707 sp -= child_frame.imethod->param_count + 1;
2708 child_frame.stack_args = sp;
2709 this_arg = sp->data.p;
2711 THROW_EX (mono_get_exception_null_reference(), ip - 2);
2712 child_frame.imethod = get_virtual_method (child_frame.imethod, this_arg);
2714 MonoClass *this_class = this_arg->vtable->klass;
2715 if (this_class->valuetype && child_frame.imethod->method->klass->valuetype) {
2717 gpointer *unboxed = mono_object_unbox (this_arg);
2718 sp [0].data.p = unboxed;
2721 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2723 context->current_frame = frame;
2725 if (context->has_resume_state) {
2726 if (frame == context->handler_frame)
2727 SET_RESUME_STATE (context);
2732 if (child_frame.ex) {
2734 * An exception occurred, need to run finally, fault and catch handlers..
2736 frame->ex = child_frame.ex;
2737 if (context->search_for_handler) {
2738 context->search_for_handler = 0;
2739 goto handle_exception;
2741 goto handle_finally;
2744 /* need to handle typedbyref ... */
2749 MINT_IN_CASE(MINT_VCALLVIRT) {
2750 MonoObject *this_arg;
2755 token = * (unsigned short *)(ip + 1);
2757 child_frame.imethod = rtm->data_items [token];
2759 child_frame.retval = sp;
2761 /* decrement by the actual number of args */
2762 sp -= child_frame.imethod->param_count + 1;
2763 child_frame.stack_args = sp;
2764 this_arg = sp->data.p;
2766 THROW_EX (mono_get_exception_null_reference(), ip - 2);
2767 child_frame.imethod = get_virtual_method (child_frame.imethod, this_arg);
2769 MonoClass *this_class = this_arg->vtable->klass;
2770 if (this_class->valuetype && child_frame.imethod->method->klass->valuetype) {
2771 gpointer *unboxed = mono_object_unbox (this_arg);
2772 sp [0].data.p = unboxed;
2775 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2777 context->current_frame = frame;
2779 if (context->has_resume_state) {
2780 if (frame == context->handler_frame)
2781 SET_RESUME_STATE (context);
2786 if (child_frame.ex) {
2788 * An exception occurred, need to run finally, fault and catch handlers..
2790 frame->ex = child_frame.ex;
2791 if (context->search_for_handler) {
2792 context->search_for_handler = 0;
2793 goto handle_exception;
2795 goto handle_finally;
2799 MINT_IN_CASE(MINT_CALLRUN)
2800 ves_imethod (frame, context);
2803 goto handle_exception;
2806 MINT_IN_CASE(MINT_RET)
2808 *frame->retval = *sp;
2809 if (sp > frame->stack)
2810 g_warning ("ret: more values on stack: %d", sp-frame->stack);
2812 MINT_IN_CASE(MINT_RET_VOID)
2813 if (sp > frame->stack)
2814 g_warning ("ret.void: more values on stack: %d %s", sp-frame->stack, mono_method_full_name (frame->imethod->method, TRUE));
2816 MINT_IN_CASE(MINT_RET_VT)
2817 i32 = READ32(ip + 1);
2819 memcpy(frame->retval->data.p, sp->data.p, i32);
2820 if (sp > frame->stack)
2821 g_warning ("ret.vt: more values on stack: %d", sp-frame->stack);
2823 MINT_IN_CASE(MINT_BR_S)
2824 ip += (short) *(ip + 1);
2826 MINT_IN_CASE(MINT_BR)
2827 ip += (gint32) READ32(ip + 1);
2829 #define ZEROP_S(datamem, op) \
2831 if (sp->data.datamem op 0) \
2832 ip += * (gint16 *)(ip + 1); \
2836 #define ZEROP(datamem, op) \
2838 if (sp->data.datamem op 0) \
2839 ip += READ32(ip + 1); \
2843 MINT_IN_CASE(MINT_BRFALSE_I4_S)
2846 MINT_IN_CASE(MINT_BRFALSE_I8_S)
2849 MINT_IN_CASE(MINT_BRFALSE_R8_S)
2852 MINT_IN_CASE(MINT_BRFALSE_I4)
2855 MINT_IN_CASE(MINT_BRFALSE_I8)
2858 MINT_IN_CASE(MINT_BRFALSE_R8)
2861 MINT_IN_CASE(MINT_BRTRUE_I4_S)
2864 MINT_IN_CASE(MINT_BRTRUE_I8_S)
2867 MINT_IN_CASE(MINT_BRTRUE_R8_S)
2870 MINT_IN_CASE(MINT_BRTRUE_I4)
2873 MINT_IN_CASE(MINT_BRTRUE_I8)
2876 MINT_IN_CASE(MINT_BRTRUE_R8)
2879 #define CONDBR_S(cond) \
2882 ip += * (gint16 *)(ip + 1); \
2885 #define BRELOP_S(datamem, op) \
2886 CONDBR_S(sp[0].data.datamem op sp[1].data.datamem)
2888 #define CONDBR(cond) \
2891 ip += READ32(ip + 1); \
2895 #define BRELOP(datamem, op) \
2896 CONDBR(sp[0].data.datamem op sp[1].data.datamem)
2898 MINT_IN_CASE(MINT_BEQ_I4_S)
2901 MINT_IN_CASE(MINT_BEQ_I8_S)
2904 MINT_IN_CASE(MINT_BEQ_R8_S)
2905 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2907 MINT_IN_CASE(MINT_BEQ_I4)
2910 MINT_IN_CASE(MINT_BEQ_I8)
2913 MINT_IN_CASE(MINT_BEQ_R8)
2914 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2916 MINT_IN_CASE(MINT_BGE_I4_S)
2919 MINT_IN_CASE(MINT_BGE_I8_S)
2922 MINT_IN_CASE(MINT_BGE_R8_S)
2923 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
2925 MINT_IN_CASE(MINT_BGE_I4)
2928 MINT_IN_CASE(MINT_BGE_I8)
2931 MINT_IN_CASE(MINT_BGE_R8)
2932 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
2934 MINT_IN_CASE(MINT_BGT_I4_S)
2937 MINT_IN_CASE(MINT_BGT_I8_S)
2940 MINT_IN_CASE(MINT_BGT_R8_S)
2941 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2943 MINT_IN_CASE(MINT_BGT_I4)
2946 MINT_IN_CASE(MINT_BGT_I8)
2949 MINT_IN_CASE(MINT_BGT_R8)
2950 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2952 MINT_IN_CASE(MINT_BLT_I4_S)
2955 MINT_IN_CASE(MINT_BLT_I8_S)
2958 MINT_IN_CASE(MINT_BLT_R8_S)
2959 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2961 MINT_IN_CASE(MINT_BLT_I4)
2964 MINT_IN_CASE(MINT_BLT_I8)
2967 MINT_IN_CASE(MINT_BLT_R8)
2968 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2970 MINT_IN_CASE(MINT_BLE_I4_S)
2973 MINT_IN_CASE(MINT_BLE_I8_S)
2976 MINT_IN_CASE(MINT_BLE_R8_S)
2977 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2979 MINT_IN_CASE(MINT_BLE_I4)
2982 MINT_IN_CASE(MINT_BLE_I8)
2985 MINT_IN_CASE(MINT_BLE_R8)
2986 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2988 MINT_IN_CASE(MINT_BNE_UN_I4_S)
2991 MINT_IN_CASE(MINT_BNE_UN_I8_S)
2994 MINT_IN_CASE(MINT_BNE_UN_R8_S)
2995 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
2997 MINT_IN_CASE(MINT_BNE_UN_I4)
3000 MINT_IN_CASE(MINT_BNE_UN_I8)
3003 MINT_IN_CASE(MINT_BNE_UN_R8)
3004 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
3007 #define BRELOP_S_CAST(datamem, op, type) \
3009 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
3010 ip += * (gint16 *)(ip + 1); \
3014 #define BRELOP_CAST(datamem, op, type) \
3016 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
3017 ip += READ32(ip + 1); \
3021 MINT_IN_CASE(MINT_BGE_UN_I4_S)
3022 BRELOP_S_CAST(i, >=, guint32);
3024 MINT_IN_CASE(MINT_BGE_UN_I8_S)
3025 BRELOP_S_CAST(l, >=, guint64);
3027 MINT_IN_CASE(MINT_BGE_UN_R8_S)
3028 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
3030 MINT_IN_CASE(MINT_BGE_UN_I4)
3031 BRELOP_CAST(i, >=, guint32);
3033 MINT_IN_CASE(MINT_BGE_UN_I8)
3034 BRELOP_CAST(l, >=, guint64);
3036 MINT_IN_CASE(MINT_BGE_UN_R8)
3037 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
3039 MINT_IN_CASE(MINT_BGT_UN_I4_S)
3040 BRELOP_S_CAST(i, >, guint32);
3042 MINT_IN_CASE(MINT_BGT_UN_I8_S)
3043 BRELOP_S_CAST(l, >, guint64);
3045 MINT_IN_CASE(MINT_BGT_UN_R8_S)
3046 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
3048 MINT_IN_CASE(MINT_BGT_UN_I4)
3049 BRELOP_CAST(i, >, guint32);
3051 MINT_IN_CASE(MINT_BGT_UN_I8)
3052 BRELOP_CAST(l, >, guint64);
3054 MINT_IN_CASE(MINT_BGT_UN_R8)
3055 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
3057 MINT_IN_CASE(MINT_BLE_UN_I4_S)
3058 BRELOP_S_CAST(i, <=, guint32);
3060 MINT_IN_CASE(MINT_BLE_UN_I8_S)
3061 BRELOP_S_CAST(l, <=, guint64);
3063 MINT_IN_CASE(MINT_BLE_UN_R8_S)
3064 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
3066 MINT_IN_CASE(MINT_BLE_UN_I4)
3067 BRELOP_CAST(i, <=, guint32);
3069 MINT_IN_CASE(MINT_BLE_UN_I8)
3070 BRELOP_CAST(l, <=, guint64);
3072 MINT_IN_CASE(MINT_BLE_UN_R8)
3073 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
3075 MINT_IN_CASE(MINT_BLT_UN_I4_S)
3076 BRELOP_S_CAST(i, <, guint32);
3078 MINT_IN_CASE(MINT_BLT_UN_I8_S)
3079 BRELOP_S_CAST(l, <, guint64);
3081 MINT_IN_CASE(MINT_BLT_UN_R8_S)
3082 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
3084 MINT_IN_CASE(MINT_BLT_UN_I4)
3085 BRELOP_CAST(i, <, guint32);
3087 MINT_IN_CASE(MINT_BLT_UN_I8)
3088 BRELOP_CAST(l, <, guint64);
3090 MINT_IN_CASE(MINT_BLT_UN_R8)
3091 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
3093 MINT_IN_CASE(MINT_SWITCH) {
3095 const unsigned short *st;
3101 if ((guint32)sp->data.i < n) {
3103 ip += 2 * (guint32)sp->data.i;
3104 offset = READ32 (ip);
3111 MINT_IN_CASE(MINT_LDIND_I1)
3113 sp[-1].data.i = *(gint8*)sp[-1].data.p;
3115 MINT_IN_CASE(MINT_LDIND_U1)
3117 sp[-1].data.i = *(guint8*)sp[-1].data.p;
3119 MINT_IN_CASE(MINT_LDIND_I2)
3121 sp[-1].data.i = *(gint16*)sp[-1].data.p;
3123 MINT_IN_CASE(MINT_LDIND_U2)
3125 sp[-1].data.i = *(guint16*)sp[-1].data.p;
3127 MINT_IN_CASE(MINT_LDIND_I4) /* Fall through */
3128 MINT_IN_CASE(MINT_LDIND_U4)
3130 sp[-1].data.i = *(gint32*)sp[-1].data.p;
3132 MINT_IN_CASE(MINT_LDIND_I8)
3134 /* memmove handles unaligned case */
3135 memmove (&sp [-1].data.l, sp [-1].data.p, sizeof (gint64));
3137 MINT_IN_CASE(MINT_LDIND_I) {
3138 guint16 offset = * (guint16 *)(ip + 1);
3139 sp[-1 - offset].data.p = *(gpointer*)sp[-1 - offset].data.p;
3143 MINT_IN_CASE(MINT_LDIND_R4)
3145 sp[-1].data.f = *(gfloat*)sp[-1].data.p;
3147 MINT_IN_CASE(MINT_LDIND_R8)
3149 sp[-1].data.f = *(gdouble*)sp[-1].data.p;
3151 MINT_IN_CASE(MINT_LDIND_REF)
3153 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
3155 MINT_IN_CASE(MINT_STIND_REF)
3158 mono_gc_wbarrier_generic_store (sp->data.p, sp [1].data.p);
3160 MINT_IN_CASE(MINT_STIND_I1)
3163 * (gint8 *) sp->data.p = (gint8)sp[1].data.i;
3165 MINT_IN_CASE(MINT_STIND_I2)
3168 * (gint16 *) sp->data.p = (gint16)sp[1].data.i;
3170 MINT_IN_CASE(MINT_STIND_I4)
3173 * (gint32 *) sp->data.p = sp[1].data.i;
3175 MINT_IN_CASE(MINT_STIND_I)
3178 * (mono_i *) sp->data.p = (mono_i)sp[1].data.p;
3180 MINT_IN_CASE(MINT_STIND_I8)
3183 * (gint64 *) sp->data.p = sp[1].data.l;
3185 MINT_IN_CASE(MINT_STIND_R4)
3188 * (float *) sp->data.p = (gfloat)sp[1].data.f;
3190 MINT_IN_CASE(MINT_STIND_R8)
3193 * (double *) sp->data.p = sp[1].data.f;
3195 MINT_IN_CASE(MINT_MONO_ATOMIC_STORE_I4)
3198 InterlockedWrite ((gint32 *) sp->data.p, sp [1].data.i);
3200 #define BINOP(datamem, op) \
3202 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.datamem; \
3204 MINT_IN_CASE(MINT_ADD_I4)
3207 MINT_IN_CASE(MINT_ADD_I8)
3210 MINT_IN_CASE(MINT_ADD_R8)
3213 MINT_IN_CASE(MINT_ADD1_I4)
3217 MINT_IN_CASE(MINT_SUB_I4)
3220 MINT_IN_CASE(MINT_SUB_I8)
3223 MINT_IN_CASE(MINT_SUB_R8)
3226 MINT_IN_CASE(MINT_SUB1_I4)
3230 MINT_IN_CASE(MINT_MUL_I4)
3233 MINT_IN_CASE(MINT_MUL_I8)
3236 MINT_IN_CASE(MINT_MUL_R8)
3239 MINT_IN_CASE(MINT_DIV_I4)
3240 if (sp [-1].data.i == 0)
3241 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3242 if (sp [-1].data.i == (-1))
3243 THROW_EX (mono_get_exception_overflow (), ip);
3246 MINT_IN_CASE(MINT_DIV_I8)
3247 if (sp [-1].data.l == 0)
3248 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3249 if (sp [-1].data.l == (-1))
3250 THROW_EX (mono_get_exception_overflow (), ip);
3253 MINT_IN_CASE(MINT_DIV_R8)
3257 #define BINOP_CAST(datamem, op, type) \
3259 sp [-1].data.datamem = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
3261 MINT_IN_CASE(MINT_DIV_UN_I4)
3262 if (sp [-1].data.i == 0)
3263 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3264 BINOP_CAST(i, /, guint32);
3266 MINT_IN_CASE(MINT_DIV_UN_I8)
3267 if (sp [-1].data.l == 0)
3268 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3269 BINOP_CAST(l, /, guint64);
3271 MINT_IN_CASE(MINT_REM_I4)
3272 if (sp [-1].data.i == 0)
3273 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3274 if (sp [-1].data.i == (-1))
3275 THROW_EX (mono_get_exception_overflow (), ip);
3278 MINT_IN_CASE(MINT_REM_I8)
3279 if (sp [-1].data.l == 0)
3280 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3281 if (sp [-1].data.l == (-1))
3282 THROW_EX (mono_get_exception_overflow (), ip);
3285 MINT_IN_CASE(MINT_REM_R8)
3286 /* FIXME: what do we actually do here? */
3288 sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
3291 MINT_IN_CASE(MINT_REM_UN_I4)
3292 if (sp [-1].data.i == 0)
3293 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3294 BINOP_CAST(i, %, guint32);
3296 MINT_IN_CASE(MINT_REM_UN_I8)
3297 if (sp [-1].data.l == 0)
3298 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3299 BINOP_CAST(l, %, guint64);
3301 MINT_IN_CASE(MINT_AND_I4)
3304 MINT_IN_CASE(MINT_AND_I8)
3307 MINT_IN_CASE(MINT_OR_I4)
3310 MINT_IN_CASE(MINT_OR_I8)
3313 MINT_IN_CASE(MINT_XOR_I4)
3316 MINT_IN_CASE(MINT_XOR_I8)
3320 #define SHIFTOP(datamem, op) \
3322 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.i; \
3325 MINT_IN_CASE(MINT_SHL_I4)
3328 MINT_IN_CASE(MINT_SHL_I8)
3331 MINT_IN_CASE(MINT_SHR_I4)
3334 MINT_IN_CASE(MINT_SHR_I8)
3337 MINT_IN_CASE(MINT_SHR_UN_I4)
3339 sp [-1].data.i = (guint32)sp [-1].data.i >> sp [0].data.i;
3342 MINT_IN_CASE(MINT_SHR_UN_I8)
3344 sp [-1].data.l = (guint64)sp [-1].data.l >> sp [0].data.i;
3347 MINT_IN_CASE(MINT_NEG_I4)
3348 sp [-1].data.i = - sp [-1].data.i;
3351 MINT_IN_CASE(MINT_NEG_I8)
3352 sp [-1].data.l = - sp [-1].data.l;
3355 MINT_IN_CASE(MINT_NEG_R8)
3356 sp [-1].data.f = - sp [-1].data.f;
3359 MINT_IN_CASE(MINT_NOT_I4)
3360 sp [-1].data.i = ~ sp [-1].data.i;
3363 MINT_IN_CASE(MINT_NOT_I8)
3364 sp [-1].data.l = ~ sp [-1].data.l;
3367 MINT_IN_CASE(MINT_CONV_I1_I4)
3368 sp [-1].data.i = (gint8)sp [-1].data.i;
3371 MINT_IN_CASE(MINT_CONV_I1_I8)
3372 sp [-1].data.i = (gint8)sp [-1].data.l;
3375 MINT_IN_CASE(MINT_CONV_I1_R8)
3376 sp [-1].data.i = (gint8)sp [-1].data.f;
3379 MINT_IN_CASE(MINT_CONV_U1_I4)
3380 sp [-1].data.i = (guint8)sp [-1].data.i;
3383 MINT_IN_CASE(MINT_CONV_U1_I8)
3384 sp [-1].data.i = (guint8)sp [-1].data.l;
3387 MINT_IN_CASE(MINT_CONV_U1_R8)
3388 sp [-1].data.i = (guint8)sp [-1].data.f;
3391 MINT_IN_CASE(MINT_CONV_I2_I4)
3392 sp [-1].data.i = (gint16)sp [-1].data.i;
3395 MINT_IN_CASE(MINT_CONV_I2_I8)
3396 sp [-1].data.i = (gint16)sp [-1].data.l;
3399 MINT_IN_CASE(MINT_CONV_I2_R8)
3400 sp [-1].data.i = (gint16)sp [-1].data.f;
3403 MINT_IN_CASE(MINT_CONV_U2_I4)
3404 sp [-1].data.i = (guint16)sp [-1].data.i;
3407 MINT_IN_CASE(MINT_CONV_U2_I8)
3408 sp [-1].data.i = (guint16)sp [-1].data.l;
3411 MINT_IN_CASE(MINT_CONV_U2_R8)
3412 sp [-1].data.i = (guint16)sp [-1].data.f;
3415 MINT_IN_CASE(MINT_CONV_I4_R8)
3416 sp [-1].data.i = (gint32)sp [-1].data.f;
3419 MINT_IN_CASE(MINT_CONV_U4_I8)
3420 MINT_IN_CASE(MINT_CONV_I4_I8)
3421 sp [-1].data.i = (gint32)sp [-1].data.l;
3424 MINT_IN_CASE(MINT_CONV_I4_I8_SP)
3425 sp [-2].data.i = (gint32)sp [-2].data.l;
3428 MINT_IN_CASE(MINT_CONV_U4_R8)
3429 /* needed on arm64 */
3430 if (isinf (sp [-1].data.f))
3433 sp [-1].data.i = (guint32)sp [-1].data.f;
3436 MINT_IN_CASE(MINT_CONV_I8_I4)
3437 sp [-1].data.l = sp [-1].data.i;
3440 MINT_IN_CASE(MINT_CONV_I8_I4_SP)
3441 sp [-2].data.l = sp [-2].data.i;
3444 MINT_IN_CASE(MINT_CONV_I8_U4)
3445 sp [-1].data.l = (guint32)sp [-1].data.i;
3448 MINT_IN_CASE(MINT_CONV_I8_R8)
3449 sp [-1].data.l = (gint64)sp [-1].data.f;
3452 MINT_IN_CASE(MINT_CONV_R4_I4)
3453 sp [-1].data.f = (float)sp [-1].data.i;
3456 MINT_IN_CASE(MINT_CONV_R4_I8)
3457 sp [-1].data.f = (float)sp [-1].data.l;
3460 MINT_IN_CASE(MINT_CONV_R4_R8)
3461 sp [-1].data.f = (float)sp [-1].data.f;
3464 MINT_IN_CASE(MINT_CONV_R8_I4)
3465 sp [-1].data.f = (double)sp [-1].data.i;
3468 MINT_IN_CASE(MINT_CONV_R8_I8)
3469 sp [-1].data.f = (double)sp [-1].data.l;
3472 MINT_IN_CASE(MINT_CONV_U8_I4)
3473 sp [-1].data.l = sp [-1].data.i & 0xffffffff;
3476 MINT_IN_CASE(MINT_CONV_U8_R8)
3477 sp [-1].data.l = (guint64)sp [-1].data.f;
3480 MINT_IN_CASE(MINT_CPOBJ) {
3481 c = rtm->data_items[* (guint16 *)(ip + 1)];
3482 g_assert (c->valuetype);
3483 /* if this assertion fails, we need to add a write barrier */
3484 g_assert (!MONO_TYPE_IS_REFERENCE (&c->byval_arg));
3485 if (c->byval_arg.type == MONO_TYPE_VALUETYPE)
3486 stackval_from_data (&c->byval_arg, &sp [-2], sp [-1].data.p, FALSE);
3488 stackval_from_data (&c->byval_arg, sp [-2].data.p, sp [-1].data.p, FALSE);
3493 MINT_IN_CASE(MINT_LDOBJ) {
3495 c = rtm->data_items[* (guint16 *)(ip + 1)];
3498 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
3499 int size = mono_class_value_size (c, NULL);
3500 sp [-1].data.p = vt_sp;
3501 vt_sp += (size + 7) & ~7;
3503 stackval_from_data (&c->byval_arg, &sp [-1], p, FALSE);
3506 MINT_IN_CASE(MINT_LDSTR)
3507 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
3511 MINT_IN_CASE(MINT_NEWOBJ) {
3512 MonoClass *newobj_class;
3513 MonoMethodSignature *csig;
3514 stackval valuetype_this;
3520 token = * (guint16 *)(ip + 1);
3523 child_frame.ip = NULL;
3524 child_frame.ex = NULL;
3526 child_frame.imethod = rtm->data_items [token];
3527 csig = mono_method_signature (child_frame.imethod->method);
3528 newobj_class = child_frame.imethod->method->klass;
3529 /*if (profiling_classes) {
3530 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
3532 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
3535 if (newobj_class->parent == mono_defaults.array_class) {
3536 sp -= csig->param_count;
3537 child_frame.stack_args = sp;
3538 o = ves_array_create (&child_frame, rtm->domain, newobj_class, csig, sp);
3540 THROW_EX (child_frame.ex, ip);
3541 goto array_constructed;
3544 g_assert (csig->hasthis);
3545 if (csig->param_count) {
3546 sp -= csig->param_count;
3547 memmove (sp + 1, sp, csig->param_count * sizeof (stackval));
3549 child_frame.stack_args = sp;
3552 * First arg is the object.
3554 if (newobj_class->valuetype) {
3555 MonoType *t = &newobj_class->byval_arg;
3556 memset (&valuetype_this, 0, sizeof (stackval));
3557 if (!newobj_class->enumtype && (t->type == MONO_TYPE_VALUETYPE || (t->type == MONO_TYPE_GENERICINST && mono_type_generic_inst_is_valuetype (t)))) {
3559 valuetype_this.data.p = vt_sp;
3561 sp->data.p = &valuetype_this;
3564 if (newobj_class != mono_defaults.string_class) {
3565 context->managed_code = 0;
3566 o = mono_object_new_checked (rtm->domain, newobj_class, &error);
3567 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3568 context->managed_code = 1;
3569 if (*mono_thread_interruption_request_flag ())
3570 mono_thread_interruption_checkpoint ();
3574 child_frame.retval = &retval;
3578 g_assert (csig->call_convention == MONO_CALL_DEFAULT);
3580 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
3582 context->current_frame = frame;
3584 if (context->has_resume_state) {
3585 if (frame == context->handler_frame)
3586 SET_RESUME_STATE (context);
3591 if (child_frame.ex) {
3593 * An exception occurred, need to run finally, fault and catch handlers..
3595 frame->ex = child_frame.ex;
3596 goto handle_finally;
3599 * a constructor returns void, but we need to return the object we created
3602 if (newobj_class->valuetype && !newobj_class->enumtype) {
3603 *sp = valuetype_this;
3604 } else if (newobj_class == mono_defaults.string_class) {
3612 MINT_IN_CASE(MINT_CASTCLASS)
3613 c = rtm->data_items [*(guint16 *)(ip + 1)];
3614 if ((o = sp [-1].data.p)) {
3615 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
3616 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3618 THROW_EX (mono_get_exception_invalid_cast (), ip);
3622 MINT_IN_CASE(MINT_ISINST)
3623 c = rtm->data_items [*(guint16 *)(ip + 1)];
3624 if ((o = sp [-1].data.p)) {
3625 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
3626 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3628 sp [-1].data.p = NULL;
3632 MINT_IN_CASE(MINT_CONV_R_UN_I4)
3633 sp [-1].data.f = (double)(guint32)sp [-1].data.i;
3636 MINT_IN_CASE(MINT_CONV_R_UN_I8)
3637 sp [-1].data.f = (double)(guint64)sp [-1].data.l;
3640 MINT_IN_CASE(MINT_UNBOX)
3641 c = rtm->data_items[*(guint16 *)(ip + 1)];
3645 THROW_EX (mono_get_exception_null_reference (), ip);
3647 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
3648 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3649 if (!(isinst_obj || ((o->vtable->klass->rank == 0) && (o->vtable->klass->element_class == c->element_class))))
3650 THROW_EX (mono_get_exception_invalid_cast (), ip);
3652 sp [-1].data.p = mono_object_unbox (o);
3655 MINT_IN_CASE(MINT_THROW)
3657 frame->ex_handler = NULL;
3659 sp->data.p = mono_get_exception_null_reference ();
3661 THROW_EX ((MonoException *)sp->data.p, ip);
3663 MINT_IN_CASE(MINT_LDFLDA_UNSAFE)
3665 sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
3668 MINT_IN_CASE(MINT_LDFLDA)
3671 THROW_EX (mono_get_exception_null_reference (), ip);
3672 sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
3675 MINT_IN_CASE(MINT_CKNULL)
3678 THROW_EX (mono_get_exception_null_reference (), ip);
3682 #define LDFLD(datamem, fieldtype) \
3683 o = sp [-1].data.p; \
3685 THROW_EX (mono_get_exception_null_reference (), ip); \
3686 sp[-1].data.datamem = * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) ; \
3689 MINT_IN_CASE(MINT_LDFLD_I1) LDFLD(i, gint8); MINT_IN_BREAK;
3690 MINT_IN_CASE(MINT_LDFLD_U1) LDFLD(i, guint8); MINT_IN_BREAK;
3691 MINT_IN_CASE(MINT_LDFLD_I2) LDFLD(i, gint16); MINT_IN_BREAK;
3692 MINT_IN_CASE(MINT_LDFLD_U2) LDFLD(i, guint16); MINT_IN_BREAK;
3693 MINT_IN_CASE(MINT_LDFLD_I4) LDFLD(i, gint32); MINT_IN_BREAK;
3694 MINT_IN_CASE(MINT_LDFLD_I8) LDFLD(l, gint64); MINT_IN_BREAK;
3695 MINT_IN_CASE(MINT_LDFLD_R4) LDFLD(f, float); MINT_IN_BREAK;
3696 MINT_IN_CASE(MINT_LDFLD_R8) LDFLD(f, double); MINT_IN_BREAK;
3697 MINT_IN_CASE(MINT_LDFLD_O) LDFLD(p, gpointer); MINT_IN_BREAK;
3698 MINT_IN_CASE(MINT_LDFLD_P) LDFLD(p, gpointer); MINT_IN_BREAK;
3700 MINT_IN_CASE(MINT_LDFLD_VT)
3703 THROW_EX (mono_get_exception_null_reference (), ip);
3704 i32 = READ32(ip + 2);
3705 sp [-1].data.p = vt_sp;
3706 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
3707 vt_sp += (i32 + 7) & ~7;
3711 MINT_IN_CASE(MINT_LDRMFLD) {
3713 MonoClassField *field;
3718 THROW_EX (mono_get_exception_null_reference (), ip);
3719 field = rtm->data_items[* (guint16 *)(ip + 1)];
3721 #ifndef DISABLE_REMOTING
3722 if (mono_object_is_transparent_proxy (o)) {
3723 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3725 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
3726 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3729 addr = (char*)o + field->offset;
3731 stackval_from_data (field->type, &sp [-1], addr, FALSE);
3735 MINT_IN_CASE(MINT_LDRMFLD_VT) {
3736 MonoClassField *field;
3742 THROW_EX (mono_get_exception_null_reference (), ip);
3743 field = rtm->data_items[* (guint16 *)(ip + 1)];
3744 i32 = READ32(ip + 2);
3746 #ifndef DISABLE_REMOTING
3747 if (mono_object_is_transparent_proxy (o)) {
3748 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3749 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
3750 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3753 addr = (char*)o + field->offset;
3755 sp [-1].data.p = vt_sp;
3756 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
3757 vt_sp += (i32 + 7) & ~7;
3758 memcpy(sp [-1].data.p, addr, i32);
3762 #define STFLD(datamem, fieldtype) \
3763 o = sp [-2].data.p; \
3765 THROW_EX (mono_get_exception_null_reference (), ip); \
3767 * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) = sp[1].data.datamem; \
3770 MINT_IN_CASE(MINT_STFLD_I1) STFLD(i, gint8); MINT_IN_BREAK;
3771 MINT_IN_CASE(MINT_STFLD_U1) STFLD(i, guint8); MINT_IN_BREAK;
3772 MINT_IN_CASE(MINT_STFLD_I2) STFLD(i, gint16); MINT_IN_BREAK;
3773 MINT_IN_CASE(MINT_STFLD_U2) STFLD(i, guint16); MINT_IN_BREAK;
3774 MINT_IN_CASE(MINT_STFLD_I4) STFLD(i, gint32); MINT_IN_BREAK;
3775 MINT_IN_CASE(MINT_STFLD_I8) STFLD(l, gint64); MINT_IN_BREAK;
3776 MINT_IN_CASE(MINT_STFLD_R4) STFLD(f, float); MINT_IN_BREAK;
3777 MINT_IN_CASE(MINT_STFLD_R8) STFLD(f, double); MINT_IN_BREAK;
3778 MINT_IN_CASE(MINT_STFLD_P) STFLD(p, gpointer); MINT_IN_BREAK;
3779 MINT_IN_CASE(MINT_STFLD_O)
3782 THROW_EX (mono_get_exception_null_reference (), ip);
3784 mono_gc_wbarrier_set_field (o, (char *) o + * (guint16 *)(ip + 1), sp [1].data.p);
3788 MINT_IN_CASE(MINT_STFLD_VT)
3791 THROW_EX (mono_get_exception_null_reference (), ip);
3792 i32 = READ32(ip + 2);
3794 memcpy((char *)o + * (guint16 *)(ip + 1), sp [1].data.p, i32);
3795 vt_sp -= (i32 + 7) & ~7;
3799 MINT_IN_CASE(MINT_STRMFLD) {
3800 MonoClassField *field;
3804 THROW_EX (mono_get_exception_null_reference (), ip);
3806 field = rtm->data_items[* (guint16 *)(ip + 1)];
3809 #ifndef DISABLE_REMOTING
3810 if (mono_object_is_transparent_proxy (o)) {
3811 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3812 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
3813 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3816 stackval_to_data (field->type, &sp [-1], (char*)o + field->offset, FALSE);
3821 MINT_IN_CASE(MINT_STRMFLD_VT) {
3822 MonoClassField *field;
3826 THROW_EX (mono_get_exception_null_reference (), ip);
3827 field = rtm->data_items[* (guint16 *)(ip + 1)];
3828 i32 = READ32(ip + 2);
3831 #ifndef DISABLE_REMOTING
3832 if (mono_object_is_transparent_proxy (o)) {
3833 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3834 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
3835 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3838 memcpy((char*)o + field->offset, sp [-1].data.p, i32);
3841 vt_sp -= (i32 + 7) & ~7;
3844 MINT_IN_CASE(MINT_LDSFLDA) {
3845 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3846 sp->data.p = mono_class_static_field_address (rtm->domain, field);
3851 MINT_IN_CASE(MINT_LDSFLD) {
3852 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3853 gpointer addr = mono_class_static_field_address (rtm->domain, field);
3854 stackval_from_data (field->type, sp, addr, FALSE);
3859 MINT_IN_CASE(MINT_LDSFLD_VT) {
3860 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3861 gpointer addr = mono_class_static_field_address (rtm->domain, field);
3862 int size = READ32 (ip + 2);
3866 vt_sp += (size + 7) & ~7;
3867 stackval_from_data (field->type, sp, addr, FALSE);
3871 MINT_IN_CASE(MINT_STSFLD) {
3872 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3873 gpointer addr = mono_class_static_field_address (rtm->domain, field);
3876 stackval_to_data (field->type, sp, addr, FALSE);
3879 MINT_IN_CASE(MINT_STSFLD_VT) {
3880 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3881 gpointer addr = mono_class_static_field_address (rtm->domain, field);
3882 int size = READ32 (ip + 2);
3886 stackval_to_data (field->type, sp, addr, FALSE);
3887 vt_sp -= (size + 7) & ~7;
3890 MINT_IN_CASE(MINT_STOBJ_VT) {
3892 c = rtm->data_items[* (guint16 *)(ip + 1)];
3894 size = mono_class_value_size (c, NULL);
3895 memcpy(sp [-2].data.p, sp [-1].data.p, size);
3896 vt_sp -= (size + 7) & ~7;
3900 MINT_IN_CASE(MINT_STOBJ) {
3901 c = rtm->data_items[* (guint16 *)(ip + 1)];
3904 g_assert (!c->byval_arg.byref);
3905 if (MONO_TYPE_IS_REFERENCE (&c->byval_arg))
3906 mono_gc_wbarrier_generic_store (sp [-2].data.p, sp [-1].data.p);
3908 stackval_from_data (&c->byval_arg, sp [-2].data.p, (char *) &sp [-1].data.p, FALSE);
3912 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_R8)
3913 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3914 THROW_EX (mono_get_exception_overflow (), ip);
3915 sp [-1].data.i = (guint32)sp [-1].data.f;
3918 MINT_IN_CASE(MINT_CONV_OVF_U8_I4)
3919 if (sp [-1].data.i < 0)
3920 THROW_EX (mono_get_exception_overflow (), ip);
3921 sp [-1].data.l = sp [-1].data.i;
3924 MINT_IN_CASE(MINT_CONV_OVF_U8_I8)
3925 if (sp [-1].data.l < 0)
3926 THROW_EX (mono_get_exception_overflow (), ip);
3929 MINT_IN_CASE(MINT_CONV_OVF_I8_U8)
3930 if ((guint64) sp [-1].data.l > MYGINT64_MAX)
3931 THROW_EX (mono_get_exception_overflow (), ip);
3934 MINT_IN_CASE(MINT_CONV_OVF_U8_R8)
3935 MINT_IN_CASE(MINT_CONV_OVF_I8_UN_R8)
3936 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGINT64_MAX)
3937 THROW_EX (mono_get_exception_overflow (), ip);
3938 sp [-1].data.l = (guint64)sp [-1].data.f;
3941 MINT_IN_CASE(MINT_CONV_OVF_I8_R8)
3942 if (sp [-1].data.f < MYGINT64_MIN || sp [-1].data.f > MYGINT64_MAX)
3943 THROW_EX (mono_get_exception_overflow (), ip);
3944 sp [-1].data.l = (gint64)sp [-1].data.f;
3947 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_I8)
3948 if ((mono_u)sp [-1].data.l > MYGUINT32_MAX)
3949 THROW_EX (mono_get_exception_overflow (), ip);
3950 sp [-1].data.i = (mono_u)sp [-1].data.l;
3953 MINT_IN_CASE(MINT_BOX) {
3954 c = rtm->data_items [* (guint16 *)(ip + 1)];
3955 guint16 offset = * (guint16 *)(ip + 2);
3957 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
3958 int size = mono_class_value_size (c, NULL);
3959 sp [-1 - offset].data.p = mono_value_box_checked (rtm->domain, c, sp [-1 - offset].data.p, &error);
3960 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3961 size = (size + 7) & ~7;
3964 stackval_to_data (&c->byval_arg, &sp [-1 - offset], (char *) &sp [-1 - offset], FALSE);
3965 sp [-1 - offset].data.p = mono_value_box_checked (rtm->domain, c, &sp [-1 - offset], &error);
3966 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3971 MINT_IN_CASE(MINT_NEWARR)
3972 sp [-1].data.p = (MonoObject*) mono_array_new_checked (rtm->domain, rtm->data_items[*(guint16 *)(ip + 1)], sp [-1].data.i, &error);
3973 if (!mono_error_ok (&error)) {
3974 THROW_EX (mono_error_convert_to_exception (&error), ip);
3976 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3978 /*if (profiling_classes) {
3979 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
3981 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
3985 MINT_IN_CASE(MINT_LDLEN)
3988 THROW_EX (mono_get_exception_null_reference (), ip);
3989 sp [-1].data.nati = mono_array_length ((MonoArray *)o);
3992 MINT_IN_CASE(MINT_GETCHR) {
3996 THROW_EX (mono_get_exception_null_reference (), ip);
3997 i32 = sp [-1].data.i;
3998 if (i32 < 0 || i32 >= mono_string_length (s))
3999 THROW_EX (mono_get_exception_index_out_of_range (), ip);
4001 sp [-1].data.i = mono_string_chars(s)[i32];
4005 MINT_IN_CASE(MINT_STRLEN)
4009 THROW_EX (mono_get_exception_null_reference (), ip);
4010 sp [-1].data.i = mono_string_length ((MonoString*) o);
4012 MINT_IN_CASE(MINT_ARRAY_RANK)
4015 THROW_EX (mono_get_exception_null_reference (), ip);
4016 sp [-1].data.i = mono_object_class (sp [-1].data.p)->rank;
4019 MINT_IN_CASE(MINT_LDELEMA)
4020 MINT_IN_CASE(MINT_LDELEMA_TC) {
4021 gboolean needs_typecheck = *ip == MINT_LDELEMA_TC;
4023 MonoClass *klass = rtm->data_items [*(guint16 *) (ip + 1)];
4024 guint16 numargs = *(guint16 *) (ip + 2);
4029 sp->data.p = ves_array_element_address (frame, klass, (MonoArray *) o, &sp [1], needs_typecheck);
4031 THROW_EX (frame->ex, ip);
4036 MINT_IN_CASE(MINT_LDELEM_I1) /* fall through */
4037 MINT_IN_CASE(MINT_LDELEM_U1) /* fall through */
4038 MINT_IN_CASE(MINT_LDELEM_I2) /* fall through */
4039 MINT_IN_CASE(MINT_LDELEM_U2) /* fall through */
4040 MINT_IN_CASE(MINT_LDELEM_I4) /* fall through */
4041 MINT_IN_CASE(MINT_LDELEM_U4) /* fall through */
4042 MINT_IN_CASE(MINT_LDELEM_I8) /* fall through */
4043 MINT_IN_CASE(MINT_LDELEM_I) /* fall through */
4044 MINT_IN_CASE(MINT_LDELEM_R4) /* fall through */
4045 MINT_IN_CASE(MINT_LDELEM_R8) /* fall through */
4046 MINT_IN_CASE(MINT_LDELEM_REF) /* fall through */
4047 MINT_IN_CASE(MINT_LDELEM_VT) {
4055 THROW_EX (mono_get_exception_null_reference (), ip);
4057 aindex = sp [1].data.i;
4058 if (aindex >= mono_array_length (o))
4059 THROW_EX (mono_get_exception_index_out_of_range (), ip);
4062 * FIXME: throw mono_get_exception_array_type_mismatch () if needed
4065 case MINT_LDELEM_I1:
4066 sp [0].data.i = mono_array_get (o, gint8, aindex);
4068 case MINT_LDELEM_U1:
4069 sp [0].data.i = mono_array_get (o, guint8, aindex);
4071 case MINT_LDELEM_I2:
4072 sp [0].data.i = mono_array_get (o, gint16, aindex);
4074 case MINT_LDELEM_U2:
4075 sp [0].data.i = mono_array_get (o, guint16, aindex);
4078 sp [0].data.nati = mono_array_get (o, mono_i, aindex);
4080 case MINT_LDELEM_I4:
4081 sp [0].data.i = mono_array_get (o, gint32, aindex);
4083 case MINT_LDELEM_U4:
4084 sp [0].data.i = mono_array_get (o, guint32, aindex);
4086 case MINT_LDELEM_I8:
4087 sp [0].data.l = mono_array_get (o, guint64, aindex);
4089 case MINT_LDELEM_R4:
4090 sp [0].data.f = mono_array_get (o, float, aindex);
4092 case MINT_LDELEM_R8:
4093 sp [0].data.f = mono_array_get (o, double, aindex);
4095 case MINT_LDELEM_REF:
4096 sp [0].data.p = mono_array_get (o, gpointer, aindex);
4098 case MINT_LDELEM_VT: {
4099 MonoClass *klass_vt = rtm->data_items [*(guint16 *) (ip + 1)];
4100 i32 = READ32 (ip + 2);
4101 char *src_addr = mono_array_addr_with_size ((MonoArray *) o, i32, aindex);
4102 sp [0].data.vt = vt_sp;
4103 stackval_from_data (&klass_vt->byval_arg, sp, src_addr, FALSE);
4104 vt_sp += (i32 + 7) & ~7;
4116 MINT_IN_CASE(MINT_STELEM_I) /* fall through */
4117 MINT_IN_CASE(MINT_STELEM_I1) /* fall through */
4118 MINT_IN_CASE(MINT_STELEM_U1) /* fall through */
4119 MINT_IN_CASE(MINT_STELEM_I2) /* fall through */
4120 MINT_IN_CASE(MINT_STELEM_U2) /* fall through */
4121 MINT_IN_CASE(MINT_STELEM_I4) /* fall through */
4122 MINT_IN_CASE(MINT_STELEM_I8) /* fall through */
4123 MINT_IN_CASE(MINT_STELEM_R4) /* fall through */
4124 MINT_IN_CASE(MINT_STELEM_R8) /* fall through */
4125 MINT_IN_CASE(MINT_STELEM_REF) /* fall through */
4126 MINT_IN_CASE(MINT_STELEM_VT) {
4133 THROW_EX (mono_get_exception_null_reference (), ip);
4135 aindex = sp [1].data.i;
4136 if (aindex >= mono_array_length ((MonoArray *)o))
4137 THROW_EX (mono_get_exception_index_out_of_range (), ip);
4141 mono_array_set ((MonoArray *)o, mono_i, aindex, sp [2].data.nati);
4143 case MINT_STELEM_I1:
4144 mono_array_set ((MonoArray *)o, gint8, aindex, sp [2].data.i);
4146 case MINT_STELEM_U1:
4147 mono_array_set ((MonoArray *) o, guint8, aindex, sp [2].data.i);
4149 case MINT_STELEM_I2:
4150 mono_array_set ((MonoArray *)o, gint16, aindex, sp [2].data.i);
4152 case MINT_STELEM_U2:
4153 mono_array_set ((MonoArray *)o, guint16, aindex, sp [2].data.i);
4155 case MINT_STELEM_I4:
4156 mono_array_set ((MonoArray *)o, gint32, aindex, sp [2].data.i);
4158 case MINT_STELEM_I8:
4159 mono_array_set ((MonoArray *)o, gint64, aindex, sp [2].data.l);
4161 case MINT_STELEM_R4:
4162 mono_array_set ((MonoArray *)o, float, aindex, sp [2].data.f);
4164 case MINT_STELEM_R8:
4165 mono_array_set ((MonoArray *)o, double, aindex, sp [2].data.f);
4167 case MINT_STELEM_REF: {
4168 MonoObject *isinst_obj = mono_object_isinst_checked (sp [2].data.p, mono_object_class (o)->element_class, &error);
4169 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4170 if (sp [2].data.p && !isinst_obj)
4171 THROW_EX (mono_get_exception_array_type_mismatch (), ip);
4172 mono_array_setref ((MonoArray *) o, aindex, sp [2].data.p);
4175 case MINT_STELEM_VT: {
4176 MonoClass *klass_vt = rtm->data_items [*(guint16 *) (ip + 1)];
4177 i32 = READ32 (ip + 2);
4178 char *dst_addr = mono_array_addr_with_size ((MonoArray *) o, i32, aindex);
4180 stackval_to_data (&klass_vt->byval_arg, &sp [2], dst_addr, FALSE);
4181 vt_sp -= (i32 + 7) & ~7;
4192 MINT_IN_CASE(MINT_CONV_OVF_I4_U4)
4193 if (sp [-1].data.i < 0)
4194 THROW_EX (mono_get_exception_overflow (), ip);
4197 MINT_IN_CASE(MINT_CONV_OVF_I4_I8)
4198 if (sp [-1].data.l < MYGINT32_MIN || sp [-1].data.l > MYGINT32_MAX)
4199 THROW_EX (mono_get_exception_overflow (), ip);
4200 sp [-1].data.i = (gint32) sp [-1].data.l;
4203 MINT_IN_CASE(MINT_CONV_OVF_I4_U8)
4204 if (sp [-1].data.l < 0 || sp [-1].data.l > MYGINT32_MAX)
4205 THROW_EX (mono_get_exception_overflow (), ip);
4206 sp [-1].data.i = (gint32) sp [-1].data.l;
4209 MINT_IN_CASE(MINT_CONV_OVF_I4_R8)
4210 if (sp [-1].data.f < MYGINT32_MIN || sp [-1].data.f > MYGINT32_MAX)
4211 THROW_EX (mono_get_exception_overflow (), ip);
4212 sp [-1].data.i = (gint32) sp [-1].data.f;
4215 MINT_IN_CASE(MINT_CONV_OVF_U4_I4)
4216 if (sp [-1].data.i < 0)
4217 THROW_EX (mono_get_exception_overflow (), ip);
4220 MINT_IN_CASE(MINT_CONV_OVF_U4_I8)
4221 if (sp [-1].data.l < 0 || sp [-1].data.l > MYGUINT32_MAX)
4222 THROW_EX (mono_get_exception_overflow (), ip);
4223 sp [-1].data.i = (guint32) sp [-1].data.l;
4226 MINT_IN_CASE(MINT_CONV_OVF_U4_R8)
4227 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
4228 THROW_EX (mono_get_exception_overflow (), ip);
4229 sp [-1].data.i = (guint32) sp [-1].data.f;
4232 MINT_IN_CASE(MINT_CONV_OVF_I2_I4)
4233 if (sp [-1].data.i < -32768 || sp [-1].data.i > 32767)
4234 THROW_EX (mono_get_exception_overflow (), ip);
4237 MINT_IN_CASE(MINT_CONV_OVF_I2_I8)
4238 if (sp [-1].data.l < -32768 || sp [-1].data.l > 32767)
4239 THROW_EX (mono_get_exception_overflow (), ip);
4240 sp [-1].data.i = (gint16) sp [-1].data.l;
4243 MINT_IN_CASE(MINT_CONV_OVF_I2_R8)
4244 if (sp [-1].data.f < -32768 || sp [-1].data.f > 32767)
4245 THROW_EX (mono_get_exception_overflow (), ip);
4246 sp [-1].data.i = (gint16) sp [-1].data.f;
4249 MINT_IN_CASE(MINT_CONV_OVF_U2_I4)
4250 if (sp [-1].data.i < 0 || sp [-1].data.i > 65535)
4251 THROW_EX (mono_get_exception_overflow (), ip);
4254 MINT_IN_CASE(MINT_CONV_OVF_U2_I8)
4255 if (sp [-1].data.l < 0 || sp [-1].data.l > 65535)
4256 THROW_EX (mono_get_exception_overflow (), ip);
4257 sp [-1].data.i = (guint16) sp [-1].data.l;
4260 MINT_IN_CASE(MINT_CONV_OVF_U2_R8)
4261 if (sp [-1].data.f < 0 || sp [-1].data.f > 65535)
4262 THROW_EX (mono_get_exception_overflow (), ip);
4263 sp [-1].data.i = (guint16) sp [-1].data.f;
4266 MINT_IN_CASE(MINT_CONV_OVF_I1_I4)
4267 if (sp [-1].data.i < -128 || sp [-1].data.i > 127)
4268 THROW_EX (mono_get_exception_overflow (), ip);
4271 MINT_IN_CASE(MINT_CONV_OVF_I1_I8)
4272 if (sp [-1].data.l < -128 || sp [-1].data.l > 127)
4273 THROW_EX (mono_get_exception_overflow (), ip);
4274 sp [-1].data.i = (gint8) sp [-1].data.l;
4277 MINT_IN_CASE(MINT_CONV_OVF_I1_R8)
4278 if (sp [-1].data.f < -128 || sp [-1].data.f > 127)
4279 THROW_EX (mono_get_exception_overflow (), ip);
4280 sp [-1].data.i = (gint8) sp [-1].data.f;
4283 MINT_IN_CASE(MINT_CONV_OVF_U1_I4)
4284 if (sp [-1].data.i < 0 || sp [-1].data.i > 255)
4285 THROW_EX (mono_get_exception_overflow (), ip);
4288 MINT_IN_CASE(MINT_CONV_OVF_U1_I8)
4289 if (sp [-1].data.l < 0 || sp [-1].data.l > 255)
4290 THROW_EX (mono_get_exception_overflow (), ip);
4291 sp [-1].data.i = (guint8) sp [-1].data.l;
4294 MINT_IN_CASE(MINT_CONV_OVF_U1_R8)
4295 if (sp [-1].data.f < 0 || sp [-1].data.f > 255)
4296 THROW_EX (mono_get_exception_overflow (), ip);
4297 sp [-1].data.i = (guint8) sp [-1].data.f;
4301 MINT_IN_CASE(MINT_LDELEM)
4302 MINT_IN_CASE(MINT_STELEM)
4303 MINT_IN_CASE(MINT_UNBOX_ANY)
4305 MINT_IN_CASE(MINT_CKFINITE)
4306 if (!isfinite(sp [-1].data.f))
4307 THROW_EX (mono_get_exception_arithmetic (), ip);
4310 MINT_IN_CASE(MINT_MKREFANY) {
4311 c = rtm->data_items [*(guint16 *)(ip + 1)];
4313 /* The value address is on the stack */
4314 gpointer addr = sp [-1].data.p;
4315 /* Push the typedref value on the stack */
4316 sp [-1].data.p = vt_sp;
4317 vt_sp += sizeof (MonoTypedRef);
4319 MonoTypedRef *tref = sp [-1].data.p;
4321 tref->type = &c->byval_arg;
4327 MINT_IN_CASE(MINT_REFANYTYPE) {
4328 MonoTypedRef *tref = sp [-1].data.p;
4329 MonoType *type = tref->type;
4331 vt_sp -= sizeof (MonoTypedRef);
4332 sp [-1].data.p = vt_sp;
4334 *(gpointer*)sp [-1].data.p = type;
4338 MINT_IN_CASE(MINT_REFANYVAL) {
4339 MonoTypedRef *tref = sp [-1].data.p;
4340 gpointer addr = tref->value;
4342 vt_sp -= sizeof (MonoTypedRef);
4344 sp [-1].data.p = addr;
4348 MINT_IN_CASE(MINT_LDTOKEN)
4351 * (gpointer *)sp->data.p = rtm->data_items[*(guint16 *)(ip + 1)];
4355 MINT_IN_CASE(MINT_ADD_OVF_I4)
4356 if (CHECK_ADD_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
4357 THROW_EX (mono_get_exception_overflow (), ip);
4360 MINT_IN_CASE(MINT_ADD_OVF_I8)
4361 if (CHECK_ADD_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
4362 THROW_EX (mono_get_exception_overflow (), ip);
4365 MINT_IN_CASE(MINT_ADD_OVF_UN_I4)
4366 if (CHECK_ADD_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
4367 THROW_EX (mono_get_exception_overflow (), ip);
4368 BINOP_CAST(i, +, guint32);
4370 MINT_IN_CASE(MINT_ADD_OVF_UN_I8)
4371 if (CHECK_ADD_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
4372 THROW_EX (mono_get_exception_overflow (), ip);
4373 BINOP_CAST(l, +, guint64);
4375 MINT_IN_CASE(MINT_MUL_OVF_I4)
4376 if (CHECK_MUL_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
4377 THROW_EX (mono_get_exception_overflow (), ip);
4380 MINT_IN_CASE(MINT_MUL_OVF_I8)
4381 if (CHECK_MUL_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
4382 THROW_EX (mono_get_exception_overflow (), ip);
4385 MINT_IN_CASE(MINT_MUL_OVF_UN_I4)
4386 if (CHECK_MUL_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
4387 THROW_EX (mono_get_exception_overflow (), ip);
4388 BINOP_CAST(i, *, guint32);
4390 MINT_IN_CASE(MINT_MUL_OVF_UN_I8)
4391 if (CHECK_MUL_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
4392 THROW_EX (mono_get_exception_overflow (), ip);
4393 BINOP_CAST(l, *, guint64);
4395 MINT_IN_CASE(MINT_SUB_OVF_I4)
4396 if (CHECK_SUB_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
4397 THROW_EX (mono_get_exception_overflow (), ip);
4400 MINT_IN_CASE(MINT_SUB_OVF_I8)
4401 if (CHECK_SUB_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
4402 THROW_EX (mono_get_exception_overflow (), ip);
4405 MINT_IN_CASE(MINT_SUB_OVF_UN_I4)
4406 if (CHECK_SUB_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
4407 THROW_EX (mono_get_exception_overflow (), ip);
4408 BINOP_CAST(i, -, guint32);
4410 MINT_IN_CASE(MINT_SUB_OVF_UN_I8)
4411 if (CHECK_SUB_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
4412 THROW_EX (mono_get_exception_overflow (), ip);
4413 BINOP_CAST(l, -, guint64);
4415 MINT_IN_CASE(MINT_ENDFINALLY)
4417 int clause_index = *ip;
4418 if (clause_index == exit_at_finally)
4420 while (sp > frame->stack) {
4424 ip = finally_ips->data;
4425 finally_ips = g_slist_remove (finally_ips, ip);
4432 MINT_IN_CASE(MINT_LEAVE) /* Fall through */
4433 MINT_IN_CASE(MINT_LEAVE_S)
4434 while (sp > frame->stack) {
4438 if (*ip == MINT_LEAVE_S) {
4439 ip += (short) *(ip + 1);
4441 ip += (gint32) READ32 (ip + 1);
4444 if (frame->ex_handler != NULL && MONO_OFFSET_IN_HANDLER(frame->ex_handler, frame->ip - rtm->code)) {
4445 frame->ex_handler = NULL;
4448 goto handle_finally;
4450 MINT_IN_CASE(MINT_ICALL_V_V)
4451 MINT_IN_CASE(MINT_ICALL_V_P)
4452 MINT_IN_CASE(MINT_ICALL_P_V)
4453 MINT_IN_CASE(MINT_ICALL_P_P)
4454 MINT_IN_CASE(MINT_ICALL_PP_V)
4455 MINT_IN_CASE(MINT_ICALL_PI_V)
4456 MINT_IN_CASE(MINT_ICALL_PP_P)
4457 MINT_IN_CASE(MINT_ICALL_PI_P)
4458 MINT_IN_CASE(MINT_ICALL_PPP_V)
4459 MINT_IN_CASE(MINT_ICALL_PPI_V)
4460 sp = do_icall (context, *ip, sp, rtm->data_items [*(guint16 *)(ip + 1)]);
4461 if (*mono_thread_interruption_request_flag ()) {
4462 MonoException *exc = mono_thread_interruption_checkpoint ();
4465 context->search_for_handler = 1;
4468 if (frame->ex != NULL)
4469 goto handle_exception;
4472 MINT_IN_CASE(MINT_MONO_LDPTR)
4473 sp->data.p = rtm->data_items [*(guint16 *)(ip + 1)];
4477 MINT_IN_CASE(MINT_MONO_NEWOBJ)
4478 sp->data.p = mono_object_new_checked (rtm->domain, rtm->data_items [*(guint16 *)(ip + 1)], &error);
4479 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4483 MINT_IN_CASE(MINT_MONO_FREE)
4486 g_error ("that doesn't seem right");
4487 g_free (sp->data.p);
4489 MINT_IN_CASE(MINT_MONO_RETOBJ)
4492 stackval_from_data (mono_method_signature (frame->imethod->method)->ret, frame->retval, sp->data.p,
4493 mono_method_signature (frame->imethod->method)->pinvoke);
4494 if (sp > frame->stack)
4495 g_warning ("retobj: more values on stack: %d", sp-frame->stack);
4497 MINT_IN_CASE(MINT_MONO_TLS) {
4498 MonoTlsKey key = *(gint32 *)(ip + 1);
4499 sp->data.p = ((gpointer (*)()) mono_tls_get_tls_getter (key, FALSE)) ();
4504 MINT_IN_CASE(MINT_MONO_MEMORY_BARRIER) {
4506 mono_memory_barrier ();
4509 MINT_IN_CASE(MINT_MONO_JIT_ATTACH) {
4512 context->original_domain = NULL;
4513 MonoDomain *tls_domain = (MonoDomain *) ((gpointer (*)()) mono_tls_get_tls_getter (TLS_KEY_DOMAIN, FALSE)) ();
4514 gpointer tls_jit = ((gpointer (*)()) mono_tls_get_tls_getter (TLS_KEY_DOMAIN, FALSE)) ();
4516 if (tls_domain != rtm->domain || !tls_jit)
4517 context->original_domain = mono_jit_thread_attach (rtm->domain);
4520 MINT_IN_CASE(MINT_MONO_JIT_DETACH)
4522 mono_jit_set_domain (context->original_domain);
4524 MINT_IN_CASE(MINT_MONO_LDDOMAIN)
4525 sp->data.p = mono_domain_get ();
4529 MINT_IN_CASE(MINT_SDB_INTR_LOC)
4530 if (G_UNLIKELY (ss_enabled)) {
4531 static void (*ss_tramp) (void);
4534 void *tramp = mini_get_single_step_trampoline ();
4535 mono_memory_barrier ();
4540 * Make this point to the MINT_SDB_SEQ_POINT instruction which follows this since
4541 * the address of that instruction is stored as the seq point address.
4546 * Use the same trampoline as the JIT. This ensures that
4547 * the debugger has the context for the last interpreter
4550 do_debugger_tramp (ss_tramp, frame);
4552 if (context->has_resume_state) {
4553 if (frame == context->handler_frame)
4554 SET_RESUME_STATE (context);
4561 MINT_IN_CASE(MINT_SDB_SEQ_POINT)
4562 /* Just a placeholder for a breakpoint */
4565 MINT_IN_CASE(MINT_SDB_BREAKPOINT) {
4566 static void (*bp_tramp) (void);
4568 void *tramp = mini_get_breakpoint_trampoline ();
4569 mono_memory_barrier ();
4575 /* Use the same trampoline as the JIT */
4576 do_debugger_tramp (bp_tramp, frame);
4578 if (context->has_resume_state) {
4579 if (frame == context->handler_frame)
4580 SET_RESUME_STATE (context);
4589 #define RELOP(datamem, op) \
4591 sp [-1].data.i = sp [-1].data.datamem op sp [0].data.datamem; \
4593 MINT_IN_CASE(MINT_CEQ_I4)
4596 MINT_IN_CASE(MINT_CEQ0_I4)
4597 sp [-1].data.i = (sp [-1].data.i == 0);
4600 MINT_IN_CASE(MINT_CEQ_I8)
4603 MINT_IN_CASE(MINT_CEQ_R8)
4605 if (isunordered (sp [-1].data.f, sp [0].data.f))
4608 sp [-1].data.i = sp [-1].data.f == sp [0].data.f;
4611 MINT_IN_CASE(MINT_CGT_I4)
4614 MINT_IN_CASE(MINT_CGT_I8)
4617 MINT_IN_CASE(MINT_CGT_R8)
4619 if (isunordered (sp [-1].data.f, sp [0].data.f))
4622 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
4626 #define RELOP_CAST(datamem, op, type) \
4628 sp [-1].data.i = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
4631 MINT_IN_CASE(MINT_CGT_UN_I4)
4632 RELOP_CAST(i, >, guint32);
4634 MINT_IN_CASE(MINT_CGT_UN_I8)
4635 RELOP_CAST(l, >, guint64);
4637 MINT_IN_CASE(MINT_CGT_UN_R8)
4639 if (isunordered (sp [-1].data.f, sp [0].data.f))
4642 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
4645 MINT_IN_CASE(MINT_CLT_I4)
4648 MINT_IN_CASE(MINT_CLT_I8)
4651 MINT_IN_CASE(MINT_CLT_R8)
4653 if (isunordered (sp [-1].data.f, sp [0].data.f))
4656 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
4659 MINT_IN_CASE(MINT_CLT_UN_I4)
4660 RELOP_CAST(i, <, guint32);
4662 MINT_IN_CASE(MINT_CLT_UN_I8)
4663 RELOP_CAST(l, <, guint64);
4665 MINT_IN_CASE(MINT_CLT_UN_R8)
4667 if (isunordered (sp [-1].data.f, sp [0].data.f))
4670 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
4673 MINT_IN_CASE(MINT_LDFTN) {
4674 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
4679 MINT_IN_CASE(MINT_LDVIRTFTN) {
4680 InterpMethod *m = rtm->data_items [* (guint16 *)(ip + 1)];
4684 THROW_EX (mono_get_exception_null_reference (), ip - 2);
4686 sp->data.p = get_virtual_method (m, sp->data.p);
4691 #define LDARG(datamem, argtype) \
4692 sp->data.datamem = * (argtype *)(frame->args + * (guint16 *)(ip + 1)); \
4696 MINT_IN_CASE(MINT_LDARG_I1) LDARG(i, gint8); MINT_IN_BREAK;
4697 MINT_IN_CASE(MINT_LDARG_U1) LDARG(i, guint8); MINT_IN_BREAK;
4698 MINT_IN_CASE(MINT_LDARG_I2) LDARG(i, gint16); MINT_IN_BREAK;
4699 MINT_IN_CASE(MINT_LDARG_U2) LDARG(i, guint16); MINT_IN_BREAK;
4700 MINT_IN_CASE(MINT_LDARG_I4) LDARG(i, gint32); MINT_IN_BREAK;
4701 MINT_IN_CASE(MINT_LDARG_I8) LDARG(l, gint64); MINT_IN_BREAK;
4702 MINT_IN_CASE(MINT_LDARG_R4) LDARG(f, float); MINT_IN_BREAK;
4703 MINT_IN_CASE(MINT_LDARG_R8) LDARG(f, double); MINT_IN_BREAK;
4704 MINT_IN_CASE(MINT_LDARG_O) LDARG(p, gpointer); MINT_IN_BREAK;
4705 MINT_IN_CASE(MINT_LDARG_P) LDARG(p, gpointer); MINT_IN_BREAK;
4707 MINT_IN_CASE(MINT_LDARG_VT)
4709 i32 = READ32(ip + 2);
4710 memcpy(sp->data.p, frame->args + * (guint16 *)(ip + 1), i32);
4711 vt_sp += (i32 + 7) & ~7;
4716 #define STARG(datamem, argtype) \
4718 * (argtype *)(frame->args + * (guint16 *)(ip + 1)) = sp->data.datamem; \
4721 MINT_IN_CASE(MINT_STARG_I1) STARG(i, gint8); MINT_IN_BREAK;
4722 MINT_IN_CASE(MINT_STARG_U1) STARG(i, guint8); MINT_IN_BREAK;
4723 MINT_IN_CASE(MINT_STARG_I2) STARG(i, gint16); MINT_IN_BREAK;
4724 MINT_IN_CASE(MINT_STARG_U2) STARG(i, guint16); MINT_IN_BREAK;
4725 MINT_IN_CASE(MINT_STARG_I4) STARG(i, gint32); MINT_IN_BREAK;
4726 MINT_IN_CASE(MINT_STARG_I8) STARG(l, gint64); MINT_IN_BREAK;
4727 MINT_IN_CASE(MINT_STARG_R4) STARG(f, float); MINT_IN_BREAK;
4728 MINT_IN_CASE(MINT_STARG_R8) STARG(f, double); MINT_IN_BREAK;
4729 MINT_IN_CASE(MINT_STARG_O) STARG(p, gpointer); MINT_IN_BREAK;
4730 MINT_IN_CASE(MINT_STARG_P) STARG(p, gpointer); MINT_IN_BREAK;
4732 MINT_IN_CASE(MINT_STARG_VT)
4733 i32 = READ32(ip + 2);
4735 memcpy(frame->args + * (guint16 *)(ip + 1), sp->data.p, i32);
4736 vt_sp -= (i32 + 7) & ~7;
4740 #define STINARG(datamem, argtype) \
4742 int n = * (guint16 *)(ip + 1); \
4743 * (argtype *)(frame->args + rtm->arg_offsets [n]) = frame->stack_args [n].data.datamem; \
4747 MINT_IN_CASE(MINT_STINARG_I1) STINARG(i, gint8); MINT_IN_BREAK;
4748 MINT_IN_CASE(MINT_STINARG_U1) STINARG(i, guint8); MINT_IN_BREAK;
4749 MINT_IN_CASE(MINT_STINARG_I2) STINARG(i, gint16); MINT_IN_BREAK;
4750 MINT_IN_CASE(MINT_STINARG_U2) STINARG(i, guint16); MINT_IN_BREAK;
4751 MINT_IN_CASE(MINT_STINARG_I4) STINARG(i, gint32); MINT_IN_BREAK;
4752 MINT_IN_CASE(MINT_STINARG_I8) STINARG(l, gint64); MINT_IN_BREAK;
4753 MINT_IN_CASE(MINT_STINARG_R4) STINARG(f, float); MINT_IN_BREAK;
4754 MINT_IN_CASE(MINT_STINARG_R8) STINARG(f, double); MINT_IN_BREAK;
4755 MINT_IN_CASE(MINT_STINARG_O) STINARG(p, gpointer); MINT_IN_BREAK;
4756 MINT_IN_CASE(MINT_STINARG_P) STINARG(p, gpointer); MINT_IN_BREAK;
4758 MINT_IN_CASE(MINT_STINARG_VT) {
4759 int n = * (guint16 *)(ip + 1);
4760 i32 = READ32(ip + 2);
4761 memcpy (frame->args + rtm->arg_offsets [n], frame->stack_args [n].data.p, i32);
4766 MINT_IN_CASE(MINT_PROF_ENTER) {
4769 if (MONO_PROFILER_ENABLED (method_enter)) {
4770 MonoProfilerCallContext *prof_ctx = NULL;
4772 if (frame->imethod->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_PROLOGUE_CONTEXT) {
4773 prof_ctx = g_new0 (MonoProfilerCallContext, 1);
4774 prof_ctx->interp_frame = frame;
4775 prof_ctx->method = frame->imethod->method;
4778 MONO_PROFILER_RAISE (method_enter, (frame->imethod->method, prof_ctx));
4786 MINT_IN_CASE(MINT_LDARGA)
4787 sp->data.p = frame->args + * (guint16 *)(ip + 1);
4792 #define LDLOC(datamem, argtype) \
4793 sp->data.datamem = * (argtype *)(locals + * (guint16 *)(ip + 1)); \
4797 MINT_IN_CASE(MINT_LDLOC_I1) LDLOC(i, gint8); MINT_IN_BREAK;
4798 MINT_IN_CASE(MINT_LDLOC_U1) LDLOC(i, guint8); MINT_IN_BREAK;
4799 MINT_IN_CASE(MINT_LDLOC_I2) LDLOC(i, gint16); MINT_IN_BREAK;
4800 MINT_IN_CASE(MINT_LDLOC_U2) LDLOC(i, guint16); MINT_IN_BREAK;
4801 MINT_IN_CASE(MINT_LDLOC_I4) LDLOC(i, gint32); MINT_IN_BREAK;
4802 MINT_IN_CASE(MINT_LDLOC_I8) LDLOC(l, gint64); MINT_IN_BREAK;
4803 MINT_IN_CASE(MINT_LDLOC_R4) LDLOC(f, float); MINT_IN_BREAK;
4804 MINT_IN_CASE(MINT_LDLOC_R8) LDLOC(f, double); MINT_IN_BREAK;
4805 MINT_IN_CASE(MINT_LDLOC_O) LDLOC(p, gpointer); MINT_IN_BREAK;
4806 MINT_IN_CASE(MINT_LDLOC_P) LDLOC(p, gpointer); MINT_IN_BREAK;
4808 MINT_IN_CASE(MINT_LDLOC_VT)
4810 i32 = READ32(ip + 2);
4811 memcpy(sp->data.p, locals + * (guint16 *)(ip + 1), i32);
4812 vt_sp += (i32 + 7) & ~7;
4817 MINT_IN_CASE(MINT_LDLOCA_S)
4818 sp->data.p = locals + * (guint16 *)(ip + 1);
4823 #define STLOC(datamem, argtype) \
4825 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp->data.datamem; \
4828 MINT_IN_CASE(MINT_STLOC_I1) STLOC(i, gint8); MINT_IN_BREAK;
4829 MINT_IN_CASE(MINT_STLOC_U1) STLOC(i, guint8); MINT_IN_BREAK;
4830 MINT_IN_CASE(MINT_STLOC_I2) STLOC(i, gint16); MINT_IN_BREAK;
4831 MINT_IN_CASE(MINT_STLOC_U2) STLOC(i, guint16); MINT_IN_BREAK;
4832 MINT_IN_CASE(MINT_STLOC_I4) STLOC(i, gint32); MINT_IN_BREAK;
4833 MINT_IN_CASE(MINT_STLOC_I8) STLOC(l, gint64); MINT_IN_BREAK;
4834 MINT_IN_CASE(MINT_STLOC_R4) STLOC(f, float); MINT_IN_BREAK;
4835 MINT_IN_CASE(MINT_STLOC_R8) STLOC(f, double); MINT_IN_BREAK;
4836 MINT_IN_CASE(MINT_STLOC_O) STLOC(p, gpointer); MINT_IN_BREAK;
4837 MINT_IN_CASE(MINT_STLOC_P) STLOC(p, gpointer); MINT_IN_BREAK;
4839 #define STLOC_NP(datamem, argtype) \
4840 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp [-1].data.datamem; \
4843 MINT_IN_CASE(MINT_STLOC_NP_I4) STLOC_NP(i, gint32); MINT_IN_BREAK;
4844 MINT_IN_CASE(MINT_STLOC_NP_O) STLOC_NP(p, gpointer); MINT_IN_BREAK;
4846 MINT_IN_CASE(MINT_STLOC_VT)
4847 i32 = READ32(ip + 2);
4849 memcpy(locals + * (guint16 *)(ip + 1), sp->data.p, i32);
4850 vt_sp -= (i32 + 7) & ~7;
4854 MINT_IN_CASE(MINT_LOCALLOC) {
4855 if (sp != frame->stack + 1) /*FIX?*/
4856 THROW_EX (mono_get_exception_execution_engine (NULL), ip);
4858 int len = sp [-1].data.i;
4859 sp [-1].data.p = alloca (len);
4860 MonoMethodHeader *header = mono_method_get_header_checked (frame->imethod->method, &error);
4861 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4862 if (header && header->init_locals)
4863 memset (sp [-1].data.p, 0, len);
4867 MINT_IN_CASE(MINT_ENDFILTER)
4868 /* top of stack is result of filter */
4869 frame->retval = &sp [-1];
4871 MINT_IN_CASE(MINT_INITOBJ)
4873 memset (sp->data.vt, 0, READ32(ip + 1));
4876 MINT_IN_CASE(MINT_CPBLK)
4878 if (!sp [0].data.p || !sp [1].data.p)
4879 THROW_EX (mono_get_exception_null_reference(), ip - 1);
4881 /* FIXME: value and size may be int64... */
4882 memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
4885 MINT_IN_CASE(MINT_CONSTRAINED_) {
4887 /* FIXME: implement */
4889 token = READ32 (ip);
4894 MINT_IN_CASE(MINT_INITBLK)
4897 THROW_EX (mono_get_exception_null_reference(), ip - 1);
4899 /* FIXME: value and size may be int64... */
4900 memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
4903 MINT_IN_CASE(MINT_NO_)
4904 /* FIXME: implement */
4908 MINT_IN_CASE(MINT_RETHROW) {
4910 * need to clarify what this should actually do:
4911 * start the search from the last found handler in
4912 * this method or continue in the caller or what.
4913 * Also, do we need to run finally/fault handlers after a retrow?
4914 * Well, this implementation will follow the usual search
4915 * for an handler, considering the current ip as throw spot.
4916 * We need to NULL frame->ex_handler for the later code to
4917 * actually run the new found handler.
4919 int exvar_offset = *(guint16*)(ip + 1);
4920 frame->ex_handler = NULL;
4921 THROW_EX_GENERAL (*(MonoException**)(frame->locals + exvar_offset), ip - 1, TRUE);
4925 g_print ("Unimplemented opcode: %04x %s at 0x%x\n", *ip, mono_interp_opname[*ip], ip-rtm->code);
4926 THROW_EX (mono_get_exception_execution_engine ("Unimplemented opcode"), ip);
4930 g_assert_not_reached ();
4932 * Exception handling code.
4933 * The exception object is stored in frame->ex.
4941 MonoExceptionClause *clause;
4947 g_print ("* Handling exception '%s' at IL_%04x\n",
4948 frame->ex == NULL ? "** Unknown **" : mono_object_class (frame->ex)->name,
4949 rtm == NULL ? 0 : frame->ip - rtm->code);
4951 if (die_on_exception)
4954 for (inv = frame; inv; inv = inv->parent) {
4956 if (inv->imethod == NULL)
4958 method = inv->imethod->method;
4959 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
4961 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
4963 if (inv->ip == NULL)
4965 ip_offset = inv->ip - inv->imethod->code;
4966 inv->ex_handler = NULL; /* clear this in case we are trhowing an exception while handling one - this one wins */
4967 for (i = 0; i < inv->imethod->num_clauses; ++i) {
4968 clause = &inv->imethod->clauses [i];
4970 g_print ("* clause [%d]: %p\n", i, clause);
4972 if (!MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4975 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4978 g_print ("* Filter found at '%s'\n", method->name);
4980 InterpFrame dup_frame;
4982 memcpy (&dup_frame, inv, sizeof (InterpFrame));
4983 dup_frame.retval = &retval;
4984 ves_exec_method_with_context (&dup_frame, context, inv->imethod->code + clause->data.filter_offset, frame->ex, -1);
4985 if (dup_frame.retval->data.i) {
4988 g_print ("* Matched Filter at '%s'\n", method->name);
4990 inv->ex_handler = clause;
4991 *(MonoException**)(inv->locals + inv->imethod->exvar_offsets [i]) = frame->ex;
4992 goto handle_finally;
4994 } else if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
4995 MonoObject *isinst_obj = mono_object_isinst_checked ((MonoObject*)frame->ex, clause->data.catch_class, &error);
4996 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4999 * OK, we found an handler, now we need to execute the finally
5000 * and fault blocks before branching to the handler code.
5004 g_print ("* Found handler at '%s'\n", method->name);
5006 inv->ex_handler = clause;
5007 *(MonoException**)(inv->locals + inv->imethod->exvar_offsets [i]) = frame->ex;
5008 goto handle_finally;
5014 * If we get here, no handler was found: print a stack trace.
5016 for (inv = frame; inv; inv = inv->parent) {
5017 if (inv->invoke_trap)
5018 goto handle_finally;
5021 ex_obj = (MonoObject *) frame->ex;
5022 mono_unhandled_exception (ex_obj);
5023 MonoJitTlsData *jit_tls = (MonoJitTlsData *) mono_tls_get_jit_tls ();
5024 jit_tls->abort_func (ex_obj);
5025 g_assert_not_reached ();
5031 MonoExceptionClause *clause;
5032 GSList *old_list = finally_ips;
5033 MonoMethod *method = frame->imethod->method;
5034 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
5035 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
5039 g_print ("* Handle finally IL_%04x\n", endfinally_ip == NULL ? 0 : endfinally_ip - rtm->code);
5041 if (rtm == NULL || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
5042 || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
5045 ip_offset = frame->ip - rtm->code;
5047 if (endfinally_ip != NULL)
5048 finally_ips = g_slist_prepend(finally_ips, (void *)endfinally_ip);
5049 for (i = 0; i < header->num_clauses; ++i)
5050 if (frame->ex_handler == &rtm->clauses [i])
5054 clause = &rtm->clauses [i];
5055 if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && (endfinally_ip == NULL || !(MONO_OFFSET_IN_CLAUSE (clause, endfinally_ip - rtm->code)))) {
5056 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
5057 ip = rtm->code + clause->handler_offset;
5058 finally_ips = g_slist_prepend (finally_ips, (gpointer) ip);
5061 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
5067 endfinally_ip = NULL;
5069 if (old_list != finally_ips && finally_ips) {
5070 ip = finally_ips->data;
5071 finally_ips = g_slist_remove (finally_ips, ip);
5072 sp = frame->stack; /* spec says stack should be empty at endfinally so it should be at the start too */
5077 * If an exception is set, we need to execute the fault handler, too,
5078 * otherwise, we continue normally.
5088 MonoExceptionClause *clause;
5089 GSList *old_list = finally_ips;
5090 MonoMethod *method = frame->imethod->method;
5091 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
5092 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
5096 g_print ("* Handle fault\n");
5098 ip_offset = frame->ip - rtm->code;
5099 for (i = 0; i < header->num_clauses; ++i) {
5100 clause = &rtm->clauses [i];
5101 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
5102 ip = rtm->code + clause->handler_offset;
5103 finally_ips = g_slist_prepend (finally_ips, (gpointer) ip);
5106 g_print ("* Executing handler at IL_%04x\n", clause->handler_offset);
5111 if (old_list != finally_ips && finally_ips) {
5112 ip = finally_ips->data;
5113 finally_ips = g_slist_remove (finally_ips, ip);
5114 sp = frame->stack; /* spec says stack should be empty at endfinally so it should be at the start too */
5121 * If the handler for the exception was found in this method, we jump
5122 * to it right away, otherwise we return and let the caller run
5123 * the finally, fault and catch blocks.
5124 * This same code should be present in the endfault opcode, but it
5125 * is corrently not assigned in the ECMA specs: LAMESPEC.
5127 if (frame->ex_handler) {
5130 g_print ("* Executing handler at IL_%04x\n", frame->ex_handler->handler_offset);
5132 ip = rtm->code + frame->ex_handler->handler_offset;
5134 vt_sp = (unsigned char *) sp + rtm->stack_size;
5135 sp->data.p = frame->ex;
5144 if (MONO_PROFILER_ENABLED (method_leave) && frame->imethod->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_EPILOGUE) {
5145 MonoProfilerCallContext *prof_ctx = NULL;
5147 if (frame->imethod->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_EPILOGUE_CONTEXT) {
5148 prof_ctx = g_new0 (MonoProfilerCallContext, 1);
5149 prof_ctx->interp_frame = frame;
5150 prof_ctx->method = frame->imethod->method;
5152 MonoType *rtype = mono_method_signature (frame->imethod->method)->ret;
5154 switch (rtype->type) {
5155 case MONO_TYPE_VOID:
5157 case MONO_TYPE_VALUETYPE:
5158 prof_ctx->return_value = frame->retval->data.p;
5161 prof_ctx->return_value = frame->retval;
5166 MONO_PROFILER_RAISE (method_leave, (frame->imethod->method, prof_ctx));
5171 MONO_PROFILER_RAISE (method_exception_leave, (frame->imethod->method, (MonoObject *) frame->ex));
5177 ves_exec_method (InterpFrame *frame)
5179 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
5180 ThreadContext context_struct;
5181 MonoDomain *domain = frame->imethod->domain;
5188 mono_unhandled_exception ((MonoObject*)frame->ex);
5191 if (context == NULL) {
5192 context = &context_struct;
5193 context_struct.base_frame = frame;
5194 context_struct.current_frame = NULL;
5195 context_struct.env_frame = frame;
5196 context_struct.current_env = &env;
5197 context_struct.search_for_handler = 0;
5198 context_struct.managed_code = 0;
5199 set_context (context);
5202 frame->parent = context->current_frame;
5203 frame->imethod = mono_interp_get_imethod (domain, frame->method, &error);
5204 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
5205 context->managed_code = 1;
5206 ves_exec_method_with_context (frame, context, NULL, NULL, -1);
5207 context->managed_code = 0;
5209 if (context != &context_struct && context->current_env) {
5210 context->env_frame->ex = frame->ex;
5211 longjmp (*context->current_env, 1);
5214 mono_unhandled_exception ((MonoObject*)frame->ex);
5216 if (context->base_frame == frame)
5219 context->current_frame = frame->parent;
5223 mono_interp_parse_options (const char *options)
5227 args = g_strsplit (options, ",", -1);
5228 for (ptr = args; ptr && *ptr; ptr ++) {
5231 if (strncmp (arg, "jit=", 4) == 0)
5232 jit_classes = g_slist_prepend (jit_classes, arg + 4);
5239 mono_native_tls_alloc (&thread_context_id, NULL);
5242 mono_interp_transform_init ();
5245 typedef int (*TestMethod) (void);
5248 interp_regression_step (MonoImage *image, int verbose, int *total_run, int *total, GTimer *timer, MonoDomain *domain)
5250 int result, expected, failed, cfailed, run;
5251 double elapsed, transform_time;
5253 MonoObject *result_obj;
5254 static gboolean filter_method_init = FALSE;
5255 static const char *filter_method = NULL;
5257 g_print ("Test run: image=%s\n", mono_image_get_filename (image));
5258 cfailed = failed = run = 0;
5259 transform_time = elapsed = 0.0;
5261 g_timer_start (timer);
5262 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
5263 MonoObject *exc = NULL;
5265 MonoMethod *method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
5267 mono_error_cleanup (&error); /* FIXME don't swallow the error */
5271 if (!filter_method_init) {
5272 filter_method = g_getenv ("INTERP_FILTER_METHOD");
5273 filter_method_init = TRUE;
5275 gboolean filter = FALSE;
5276 if (filter_method) {
5277 const char *name = filter_method;
5279 if ((strchr (name, '.') > name) || strchr (name, ':')) {
5280 MonoMethodDesc *desc = mono_method_desc_new (name, TRUE);
5281 filter = mono_method_desc_full_match (desc, method);
5282 mono_method_desc_free (desc);
5284 filter = strcmp (method->name, name) == 0;
5286 } else { /* no filter, check for `Category' attribute on method */
5288 MonoCustomAttrInfo* ainfo = mono_custom_attrs_from_method_checked (method, &error);
5289 mono_error_cleanup (&error);
5293 for (j = 0; j < ainfo->num_attrs && filter; ++j) {
5294 MonoCustomAttrEntry *centry = &ainfo->attrs [j];
5295 if (centry->ctor == NULL)
5298 MonoClass *klass = centry->ctor->klass;
5299 if (strcmp (klass->name, "CategoryAttribute"))
5302 MonoObject *obj = mono_custom_attrs_get_attr_checked (ainfo, klass, &error);
5303 /* FIXME: there is an ordering problem if there're multiple attributes, do this instead:
5304 * MonoObject *obj = create_custom_attr (ainfo->image, centry->ctor, centry->data, centry->data_size, &error); */
5305 mono_error_cleanup (&error);
5306 MonoMethod *getter = mono_class_get_method_from_name (klass, "get_Category", -1);
5307 MonoObject *str = mono_interp_runtime_invoke (getter, obj, NULL, &exc, &error);
5308 mono_error_cleanup (&error);
5309 char *utf8_str = mono_string_to_utf8_checked ((MonoString *) str, &error);
5310 mono_error_cleanup (&error);
5311 if (!strcmp (utf8_str, "!INTERPRETER")) {
5312 g_print ("skip %s...\n", method->name);
5318 if (strncmp (method->name, "test_", 5) == 0 && filter) {
5319 MonoError interp_error;
5320 MonoObject *exc = NULL;
5322 result_obj = mono_interp_runtime_invoke (method, NULL, NULL, &exc, &interp_error);
5323 if (!mono_error_ok (&interp_error)) {
5325 g_print ("Test '%s' execution failed.\n", method->name);
5326 } else if (exc != NULL) {
5327 g_print ("Exception in Test '%s' occured:\n", method->name);
5328 mono_object_describe (exc);
5332 result = *(gint32 *) mono_object_unbox (result_obj);
5333 expected = atoi (method->name + 5); // FIXME: oh no.
5336 if (result != expected) {
5338 g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
5343 g_timer_stop (timer);
5344 elapsed = g_timer_elapsed (timer, NULL);
5345 if (failed > 0 || cfailed > 0){
5346 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
5347 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
5349 g_print ("Results: total tests: %d, all pass \n", run);
5352 g_print ("Elapsed time: %f secs (%f, %f)\n\n", elapsed,
5353 elapsed - transform_time, transform_time);
5354 *total += failed + cfailed;
5359 interp_regression (MonoImage *image, int verbose, int *total_run)
5362 GTimer *timer = g_timer_new ();
5363 MonoDomain *domain = mono_domain_get ();
5367 /* load the metadata */
5368 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
5370 method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
5372 mono_error_cleanup (&error);
5375 mono_class_init (method->klass);
5380 interp_regression_step (image, verbose, total_run, &total, timer, domain);
5382 g_timer_destroy (timer);
5387 mono_interp_regression_list (int verbose, int count, char *images [])
5389 int i, total, total_run, run;
5391 total_run = total = 0;
5392 for (i = 0; i < count; ++i) {
5393 MonoAssembly *ass = mono_assembly_open_predicate (images [i], FALSE, FALSE, NULL, NULL, NULL);
5395 g_warning ("failed to load assembly: %s", images [i]);
5398 total += interp_regression (mono_assembly_get_image (ass), verbose, &run);
5402 g_print ("Overall results: tests: %d, failed: %d (pass: %.2f%%)\n", total_run, total, 100.0*(total_run-total)/total_run);
5404 g_print ("Overall results: tests: %d, 100%% pass\n", total_run);
5411 * mono_interp_set_resume_state:
5413 * Set the state the interpeter will continue to execute from after execution returns to the interpreter.
5416 mono_interp_set_resume_state (MonoJitTlsData *jit_tls, MonoException *ex, MonoInterpFrameHandle interp_frame, gpointer handler_ip)
5418 ThreadContext *context;
5421 context = jit_tls->interp_context;
5424 context->has_resume_state = TRUE;
5425 context->handler_frame = interp_frame;
5426 /* This is on the stack, so it doesn't need a wbarrier */
5427 context->handler_frame->ex = ex;
5428 context->handler_ip = handler_ip;
5432 * mono_interp_run_finally:
5434 * Run the finally clause identified by CLAUSE_INDEX in the intepreter frame given by
5435 * frame->interp_frame.
5438 mono_interp_run_finally (StackFrameInfo *frame, int clause_index, gpointer handler_ip)
5440 InterpFrame *iframe = frame->interp_frame;
5441 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
5443 ves_exec_method_with_context (iframe, context, handler_ip, NULL, clause_index);
5447 InterpFrame *current;
5451 * mono_interp_frame_iter_init:
5453 * Initialize an iterator for iterating through interpreted frames.
5456 mono_interp_frame_iter_init (MonoInterpStackIter *iter, gpointer interp_exit_data)
5458 StackIter *stack_iter = (StackIter*)iter;
5460 stack_iter->current = (InterpFrame*)interp_exit_data;
5464 mono_interp_frame_iter_next (MonoInterpStackIter *iter, StackFrameInfo *frame)
5466 StackIter *stack_iter = (StackIter*)iter;
5467 InterpFrame *iframe = stack_iter->current;
5469 memset (frame, 0, sizeof (StackFrameInfo));
5470 /* pinvoke frames doesn't have imethod set */
5471 while (iframe && !(iframe->imethod && iframe->imethod->code))
5472 iframe = iframe->parent;
5476 frame->type = FRAME_TYPE_INTERP;
5478 frame->domain = mono_domain_get ();
5479 frame->interp_frame = iframe;
5480 frame->method = iframe->imethod->method;
5481 frame->actual_method = frame->method;
5482 /* This is the offset in the interpreter IR */
5483 frame->native_offset = (guint8*)iframe->ip - (guint8*)iframe->imethod->code;
5484 frame->ji = iframe->imethod->jinfo;
5486 stack_iter->current = iframe->parent;
5492 mono_interp_find_jit_info (MonoDomain *domain, MonoMethod *method)
5496 rtm = lookup_imethod (domain, method);
5504 mono_interp_set_breakpoint (MonoJitInfo *jinfo, gpointer ip)
5506 guint16 *code = (guint16*)ip;
5507 g_assert (*code == MINT_SDB_SEQ_POINT);
5508 *code = MINT_SDB_BREAKPOINT;
5512 mono_interp_clear_breakpoint (MonoJitInfo *jinfo, gpointer ip)
5514 guint16 *code = (guint16*)ip;
5515 g_assert (*code == MINT_SDB_BREAKPOINT);
5516 *code = MINT_SDB_SEQ_POINT;
5520 mono_interp_frame_get_jit_info (MonoInterpFrameHandle frame)
5522 InterpFrame *iframe = (InterpFrame*)frame;
5524 g_assert (iframe->imethod);
5525 return iframe->imethod->jinfo;
5529 mono_interp_frame_get_ip (MonoInterpFrameHandle frame)
5531 InterpFrame *iframe = (InterpFrame*)frame;
5533 g_assert (iframe->imethod);
5534 return (gpointer)iframe->ip;
5538 mono_interp_frame_get_arg (MonoInterpFrameHandle frame, int pos)
5540 InterpFrame *iframe = (InterpFrame*)frame;
5542 g_assert (iframe->imethod);
5544 int arg_offset = iframe->imethod->arg_offsets [pos + (iframe->imethod->hasthis ? 1 : 0)];
5546 return iframe->args + arg_offset;
5550 mono_interp_frame_get_local (MonoInterpFrameHandle frame, int pos)
5552 InterpFrame *iframe = (InterpFrame*)frame;
5554 g_assert (iframe->imethod);
5556 return iframe->locals + iframe->imethod->local_offsets [pos];
5560 mono_interp_frame_get_this (MonoInterpFrameHandle frame)
5562 InterpFrame *iframe = (InterpFrame*)frame;
5564 g_assert (iframe->imethod);
5565 g_assert (iframe->imethod->hasthis);
5567 int arg_offset = iframe->imethod->arg_offsets [0];
5569 return iframe->args + arg_offset;
5573 mono_interp_start_single_stepping (void)
5579 mono_interp_stop_single_stepping (void)