3 * PLEASE NOTE: This is a research prototype.
6 * interp.c: Interpreter for CIL byte codes
9 * Paolo Molaro (lupus@ximian.com)
10 * Miguel de Icaza (miguel@ximian.com)
11 * Dietmar Maurer (dietmar@ximian.com)
13 * (C) 2001, 2002 Ximian, Inc.
28 #include <mono/utils/gc_wrapper.h>
34 # define alloca __builtin_alloca
38 /* trim excessive headers */
39 #include <mono/metadata/image.h>
40 #include <mono/metadata/assembly-internals.h>
41 #include <mono/metadata/cil-coff.h>
42 #include <mono/metadata/mono-endian.h>
43 #include <mono/metadata/tabledefs.h>
44 #include <mono/metadata/tokentype.h>
45 #include <mono/metadata/loader.h>
46 #include <mono/metadata/threads.h>
47 #include <mono/metadata/threadpool.h>
48 #include <mono/metadata/profiler-private.h>
49 #include <mono/metadata/appdomain.h>
50 #include <mono/metadata/reflection.h>
51 #include <mono/metadata/reflection-internals.h>
52 #include <mono/metadata/exception.h>
53 #include <mono/metadata/verify.h>
54 #include <mono/metadata/opcodes.h>
55 #include <mono/metadata/debug-helpers.h>
56 #include <mono/metadata/mono-config.h>
57 #include <mono/metadata/marshal.h>
58 #include <mono/metadata/environment.h>
59 #include <mono/metadata/mono-debug.h>
60 #include <mono/utils/atomic.h>
63 #include "interp-internals.h"
67 #include <mono/mini/mini.h>
68 #include <mono/mini/jit-icalls.h>
69 #include <mono/mini/debugger-agent.h>
72 #include <mono/mini/mini-arm.h>
75 /* Mingw 2.1 doesnt need this any more, but leave it in for now for older versions */
78 #define finite _finite
82 #define finite isfinite
87 init_frame (MonoInvocation *frame, MonoInvocation *parent_frame, RuntimeMethod *rmethod, stackval *method_args, stackval *method_retval)
89 frame->parent = parent_frame;
90 frame->stack_args = method_args;
91 frame->retval = method_retval;
92 frame->runtime_method = rmethod;
95 frame->invoke_trap = 0;
98 #define INIT_FRAME(frame,parent_frame,method_args,method_retval,domain,mono_method,error) do { \
99 RuntimeMethod *_rmethod = mono_interp_get_runtime_method ((domain), (mono_method), (error)); \
100 init_frame ((frame), (parent_frame), _rmethod, (method_args), (method_retval)); \
104 * List of classes whose methods will be executed by transitioning to JITted code.
108 /* If TRUE, interpreted code will be interrupted at function entry/backward branches */
109 static gboolean ss_enabled;
111 void ves_exec_method (MonoInvocation *frame);
113 static char* dump_frame (MonoInvocation *inv);
114 static MonoArray *get_trace_ips (MonoDomain *domain, MonoInvocation *top);
115 static void ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, unsigned short *start_with_ip, MonoException *filter_exception, int exit_at_finally);
117 typedef void (*ICallMethod) (MonoInvocation *frame);
119 static guint32 die_on_exception = 0;
120 static MonoNativeTlsKey thread_context_id;
122 static char* dump_args (MonoInvocation *inv);
124 #define DEBUG_INTERP 0
127 int mono_interp_traceopt = 2;
128 /* If true, then we output the opcodes as we interpret them */
129 static int global_tracing = 2;
131 static int debug_indent_level = 0;
133 static int break_on_method = 0;
134 static int nested_trace = 0;
135 static GList *db_methods = NULL;
142 for (h = 0; h < debug_indent_level; h++)
147 db_match_method (gpointer data, gpointer user_data)
149 MonoMethod *m = (MonoMethod*)user_data;
150 MonoMethodDesc *desc = data;
152 if (mono_method_desc_full_match (desc, m))
157 debug_enter (MonoInvocation *frame, int *tracing)
160 g_list_foreach (db_methods, db_match_method, (gpointer)frame->runtime_method->method);
162 *tracing = nested_trace ? (global_tracing = 2, 3) : 2;
166 MonoMethod *method = frame->runtime_method->method;
167 char *mn, *args = dump_args (frame);
168 debug_indent_level++;
170 mn = mono_method_full_name (method, FALSE);
171 g_print ("(%p) Entering %s (", mono_thread_internal_current (), mn);
173 g_print ("%s)\n", args);
176 if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE)
177 mono_profiler_method_enter (frame->runtime_method->method);
181 #define DEBUG_LEAVE() \
184 args = dump_retval (frame); \
186 mn = mono_method_full_name (frame->runtime_method->method, FALSE); \
187 g_print ("(%p) Leaving %s", mono_thread_internal_current (), mn); \
189 g_print (" => %s\n", args); \
191 debug_indent_level--; \
192 if (tracing == 3) global_tracing = 0; \
194 if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE) \
195 mono_profiler_method_leave (frame->runtime_method->method);
199 int mono_interp_traceopt = 0;
200 static void debug_enter (MonoInvocation *frame, int *tracing)
203 #define DEBUG_LEAVE()
207 /* Set the current execution state to the resume state in context */
208 #define SET_RESUME_STATE(context) do { \
209 ip = (context)->handler_ip; \
211 sp->data.p = frame->ex; \
215 (context)->has_resume_state = 0; \
216 (context)->handler_frame = NULL; \
221 set_context (ThreadContext *context)
223 MonoJitTlsData *jit_tls;
225 mono_native_tls_set_value (thread_context_id, context);
226 jit_tls = mono_tls_get_jit_tls ();
228 jit_tls->interp_context = context;
232 ves_real_abort (int line, MonoMethod *mh,
233 const unsigned short *ip, stackval *stack, stackval *sp)
236 fprintf (stderr, "Execution aborted in method: %s::%s\n", mh->klass->name, mh->name);
237 fprintf (stderr, "Line=%d IP=0x%04lx, Aborted execution\n", line,
238 ip-(const unsigned short *)mono_method_get_header_checked (mh, &error)->code);
239 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
240 g_print ("0x%04x %02x\n",
241 ip-(const unsigned short *)mono_method_get_header_checked (mh, &error)->code, *ip);
242 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
244 printf ("\t[%ld] 0x%08x %0.5f\n", sp-stack, sp[-1].data.i, sp[-1].data.f);
247 #define ves_abort() \
249 ves_real_abort(__LINE__, frame->runtime_method->method, ip, frame->stack, sp); \
250 THROW_EX (mono_get_exception_execution_engine (NULL), ip); \
253 static RuntimeMethod*
254 lookup_runtime_method (MonoDomain *domain, MonoMethod *method)
257 MonoJitDomainInfo *info;
259 info = domain_jit_info (domain);
260 mono_domain_jit_code_hash_lock (domain);
261 rtm = mono_internal_hash_table_lookup (&info->interp_code_hash, method);
262 mono_domain_jit_code_hash_unlock (domain);
267 mono_interp_get_runtime_method (MonoDomain *domain, MonoMethod *method, MonoError *error)
270 MonoJitDomainInfo *info;
271 MonoMethodSignature *sig;
276 info = domain_jit_info (domain);
277 mono_domain_jit_code_hash_lock (domain);
278 rtm = mono_internal_hash_table_lookup (&info->interp_code_hash, method);
279 mono_domain_jit_code_hash_unlock (domain);
283 sig = mono_method_signature (method);
285 rtm = mono_domain_alloc0 (domain, sizeof (RuntimeMethod));
286 rtm->method = method;
287 rtm->param_count = sig->param_count;
288 rtm->hasthis = sig->hasthis;
289 rtm->rtype = mini_get_underlying_type (sig->ret);
290 rtm->param_types = mono_domain_alloc0 (domain, sizeof (MonoType*) * sig->param_count);
291 for (i = 0; i < sig->param_count; ++i)
292 rtm->param_types [i] = mini_get_underlying_type (sig->params [i]);
294 mono_domain_jit_code_hash_lock (domain);
295 if (!mono_internal_hash_table_lookup (&info->interp_code_hash, method))
296 mono_internal_hash_table_insert (&info->interp_code_hash, method, rtm);
297 mono_domain_jit_code_hash_unlock (domain);
303 mono_interp_create_trampoline (MonoDomain *domain, MonoMethod *method, MonoError *error)
305 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
306 method = mono_marshal_get_synchronized_wrapper (method);
307 return mono_interp_get_runtime_method (domain, method, error);
313 * Push an LMF frame on the LMF stack
314 * to mark the transition to native code.
315 * This is needed for the native code to
316 * be able to do stack walks.
319 interp_push_lmf (MonoLMFExt *ext, MonoInvocation *frame)
321 memset (ext, 0, sizeof (MonoLMFExt));
322 ext->interp_exit = TRUE;
323 ext->interp_exit_data = frame;
329 interp_pop_lmf (MonoLMFExt *ext)
331 mono_pop_lmf (&ext->lmf);
334 static inline RuntimeMethod*
335 get_virtual_method (MonoDomain *domain, RuntimeMethod *runtime_method, MonoObject *obj)
337 MonoMethod *m = runtime_method->method;
340 if ((m->flags & METHOD_ATTRIBUTE_FINAL) || !(m->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
341 RuntimeMethod *ret = NULL;
342 if (mono_object_is_transparent_proxy (obj)) {
343 ret = mono_interp_get_runtime_method (domain, mono_marshal_get_remoting_invoke (m), &error);
344 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
345 } else if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
346 ret = mono_interp_get_runtime_method (domain, mono_marshal_get_synchronized_wrapper (m), &error);
347 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
349 ret = runtime_method;
354 mono_class_setup_vtable (obj->vtable->klass);
356 int slot = mono_method_get_vtable_slot (m);
357 if (mono_class_is_interface (m->klass)) {
358 g_assert (obj->vtable->klass != m->klass);
359 /* TODO: interface offset lookup is slow, go through IMT instead */
360 gboolean non_exact_match;
361 slot += mono_class_interface_offset_with_variance (obj->vtable->klass, m->klass, &non_exact_match);
364 MonoMethod *virtual_method = obj->vtable->klass->vtable [slot];
365 if (m->is_inflated && mono_method_get_context (m)->method_inst) {
366 MonoGenericContext context = { NULL, NULL };
368 if (mono_class_is_ginst (virtual_method->klass))
369 context.class_inst = mono_class_get_generic_class (virtual_method->klass)->context.class_inst;
370 else if (mono_class_is_gtd (virtual_method->klass))
371 context.class_inst = mono_class_get_generic_container (virtual_method->klass)->context.class_inst;
372 context.method_inst = mono_method_get_context (m)->method_inst;
374 virtual_method = mono_class_inflate_generic_method_checked (virtual_method, &context, &error);
375 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
378 if (virtual_method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
379 virtual_method = mono_marshal_get_synchronized_wrapper (virtual_method);
382 RuntimeMethod *virtual_runtime_method = mono_interp_get_runtime_method (domain, virtual_method, &error);
383 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
384 return virtual_runtime_method;
388 stackval_from_data (MonoType *type, stackval *result, char *data, gboolean pinvoke)
391 switch (type->type) {
392 case MONO_TYPE_OBJECT:
393 case MONO_TYPE_CLASS:
394 case MONO_TYPE_STRING:
395 case MONO_TYPE_ARRAY:
396 case MONO_TYPE_SZARRAY:
401 result->data.p = *(gpointer*)data;
404 switch (type->type) {
408 result->data.i = *(gint8*)data;
411 case MONO_TYPE_BOOLEAN:
412 result->data.i = *(guint8*)data;
415 result->data.i = *(gint16*)data;
419 result->data.i = *(guint16*)data;
422 result->data.i = *(gint32*)data;
426 result->data.nati = *(mono_i*)data;
429 result->data.p = *(gpointer*)data;
432 result->data.i = *(guint32*)data;
436 /* memmove handles unaligned case */
437 memmove (&tmp, data, sizeof (float));
438 result->data.f = tmp;
443 memmove (&result->data.l, data, sizeof (gint64));
446 memmove (&result->data.f, data, sizeof (double));
448 case MONO_TYPE_STRING:
449 case MONO_TYPE_SZARRAY:
450 case MONO_TYPE_CLASS:
451 case MONO_TYPE_OBJECT:
452 case MONO_TYPE_ARRAY:
453 result->data.p = *(gpointer*)data;
455 case MONO_TYPE_VALUETYPE:
456 if (type->data.klass->enumtype) {
457 stackval_from_data (mono_class_enum_basetype (type->data.klass), result, data, pinvoke);
460 mono_value_copy (result->data.vt, data, type->data.klass);
462 case MONO_TYPE_GENERICINST: {
463 if (mono_type_generic_inst_is_valuetype (type)) {
464 mono_value_copy (result->data.vt, data, mono_class_from_mono_type (type));
467 stackval_from_data (&type->data.generic_class->container_class->byval_arg, result, data, pinvoke);
471 g_warning ("got type 0x%02x", type->type);
472 g_assert_not_reached ();
477 stackval_to_data (MonoType *type, stackval *val, char *data, gboolean pinvoke)
480 gpointer *p = (gpointer*)data;
484 /* printf ("TODAT0 %p\n", data); */
485 switch (type->type) {
488 guint8 *p = (guint8*)data;
492 case MONO_TYPE_BOOLEAN: {
493 guint8 *p = (guint8*)data;
494 *p = (val->data.i != 0);
499 case MONO_TYPE_CHAR: {
500 guint16 *p = (guint16*)data;
505 mono_i *p = (mono_i*)data;
506 /* In theory the value used by stloc should match the local var type
507 but in practice it sometimes doesn't (a int32 gets dup'd and stloc'd into
508 a native int - both by csc and mcs). Not sure what to do about sign extension
509 as it is outside the spec... doing the obvious */
510 *p = (mono_i)val->data.nati;
514 mono_u *p = (mono_u*)data;
516 *p = (mono_u)val->data.nati;
521 gint32 *p = (gint32*)data;
527 gint64 *p = (gint64*)data;
532 float *p = (float*)data;
537 double *p = (double*)data;
541 case MONO_TYPE_STRING:
542 case MONO_TYPE_SZARRAY:
543 case MONO_TYPE_CLASS:
544 case MONO_TYPE_OBJECT:
545 case MONO_TYPE_ARRAY: {
546 gpointer *p = (gpointer *) data;
547 mono_gc_wbarrier_generic_store (p, val->data.p);
550 case MONO_TYPE_PTR: {
551 gpointer *p = (gpointer *) data;
555 case MONO_TYPE_VALUETYPE:
556 if (type->data.klass->enumtype) {
557 stackval_to_data (mono_class_enum_basetype (type->data.klass), val, data, pinvoke);
560 mono_value_copy (data, val->data.vt, type->data.klass);
562 case MONO_TYPE_GENERICINST: {
563 MonoClass *container_class = type->data.generic_class->container_class;
565 if (container_class->valuetype && !container_class->enumtype) {
566 mono_value_copy (data, val->data.vt, mono_class_from_mono_type (type));
569 stackval_to_data (&type->data.generic_class->container_class->byval_arg, val, data, pinvoke);
573 g_warning ("got type %x", type->type);
574 g_assert_not_reached ();
579 fill_in_trace (MonoException *exception, MonoInvocation *frame)
582 char *stack_trace = dump_frame (frame);
583 MonoDomain *domain = mono_domain_get();
584 (exception)->stack_trace = mono_string_new_checked (domain, stack_trace, &error);
585 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
586 (exception)->trace_ips = get_trace_ips (domain, frame);
587 g_free (stack_trace);
590 #define FILL_IN_TRACE(exception, frame) fill_in_trace(exception, frame)
592 #define THROW_EX(exception,ex_ip) \
594 frame->ip = (ex_ip); \
595 frame->ex = (MonoException*)(exception); \
596 FILL_IN_TRACE(frame->ex, frame); \
597 goto handle_exception; \
601 ves_array_create (MonoInvocation *frame, MonoDomain *domain, MonoClass *klass, MonoMethodSignature *sig, stackval *values)
604 intptr_t *lower_bounds;
609 lengths = alloca (sizeof (uintptr_t) * klass->rank * 2);
610 for (i = 0; i < sig->param_count; ++i) {
611 lengths [i] = values->data.i;
614 if (klass->rank == sig->param_count) {
615 /* Only lengths provided. */
618 /* lower bounds are first. */
619 lower_bounds = (intptr_t *) lengths;
620 lengths += klass->rank;
622 obj = (MonoObject*) mono_array_new_full_checked (domain, klass, lengths, lower_bounds, &error);
623 if (!mono_error_ok (&error)) {
624 frame->ex = mono_error_convert_to_exception (&error);
625 FILL_IN_TRACE (frame->ex, frame);
627 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
632 ves_array_calculate_index (MonoArray *ao, stackval *sp, MonoInvocation *frame, gboolean safe)
634 g_assert (!frame->ex);
635 MonoClass *ac = ((MonoObject *) ao)->vtable->klass;
639 for (gint32 i = 0; i < ac->rank; i++) {
640 guint32 idx = sp [i].data.i;
641 guint32 lower = ao->bounds [i].lower_bound;
642 guint32 len = ao->bounds [i].length;
643 if (safe && (idx < lower || (idx - lower) >= len)) {
644 frame->ex = mono_get_exception_index_out_of_range ();
645 FILL_IN_TRACE (frame->ex, frame);
648 pos = (pos * len) + idx - lower;
652 if (safe && pos >= ao->max_length) {
653 frame->ex = mono_get_exception_index_out_of_range ();
654 FILL_IN_TRACE (frame->ex, frame);
662 ves_array_set (MonoInvocation *frame)
664 stackval *sp = frame->stack_args + 1;
666 MonoObject *o = frame->stack_args->data.p;
667 MonoArray *ao = (MonoArray *) o;
668 MonoClass *ac = o->vtable->klass;
670 g_assert (ac->rank >= 1);
672 gint32 pos = ves_array_calculate_index (ao, sp, frame, TRUE);
676 if (sp [ac->rank].data.p && !mono_object_class (o)->element_class->valuetype) {
678 MonoObject *isinst = mono_object_isinst_checked (sp [ac->rank].data.p, mono_object_class (o)->element_class, &error);
679 mono_error_cleanup (&error);
681 frame->ex = mono_get_exception_array_type_mismatch ();
682 FILL_IN_TRACE (frame->ex, frame);
687 gint32 esize = mono_array_element_size (ac);
688 gpointer ea = mono_array_addr_with_size (ao, esize, pos);
690 MonoType *mt = mono_method_signature (frame->runtime_method->method)->params [ac->rank];
691 stackval_to_data (mt, &sp [ac->rank], ea, FALSE);
695 ves_array_get (MonoInvocation *frame, gboolean safe)
697 stackval *sp = frame->stack_args + 1;
699 MonoObject *o = frame->stack_args->data.p;
700 MonoArray *ao = (MonoArray *) o;
701 MonoClass *ac = o->vtable->klass;
703 g_assert (ac->rank >= 1);
705 gint32 pos = ves_array_calculate_index (ao, sp, frame, safe);
709 gint32 esize = mono_array_element_size (ac);
710 gpointer ea = mono_array_addr_with_size (ao, esize, pos);
712 MonoType *mt = mono_method_signature (frame->runtime_method->method)->ret;
713 stackval_from_data (mt, frame->retval, ea, FALSE);
717 ves_array_element_address (MonoInvocation *frame, MonoClass *required_type, MonoArray *ao, stackval *sp, gboolean needs_typecheck)
719 MonoClass *ac = ((MonoObject *) ao)->vtable->klass;
721 g_assert (ac->rank >= 1);
723 gint32 pos = ves_array_calculate_index (ao, sp, frame, TRUE);
727 if (needs_typecheck && !mono_class_is_assignable_from (mono_object_class ((MonoObject *) ao)->element_class, required_type->element_class)) {
728 frame->ex = mono_get_exception_array_type_mismatch ();
729 FILL_IN_TRACE (frame->ex, frame);
732 gint32 esize = mono_array_element_size (ac);
733 return mono_array_addr_with_size (ao, esize, pos);
737 interp_walk_stack_with_ctx (MonoInternalStackWalk func, MonoContext *ctx, MonoUnwindOptions options, void *user_data)
740 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
745 MonoInvocation *frame = context->current_frame;
748 MonoStackFrameInfo fi;
749 memset (&fi, 0, sizeof (MonoStackFrameInfo));
751 /* TODO: hack to make some asserts happy. */
752 fi.ji = (MonoJitInfo *) frame->runtime_method;
754 if (frame->runtime_method)
755 fi.method = fi.actual_method = frame->runtime_method->method;
757 if (!fi.method || (fi.method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) || (fi.method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
759 fi.type = FRAME_TYPE_MANAGED_TO_NATIVE;
761 MonoMethodHeader *hd = mono_method_get_header_checked (fi.method, &error);
762 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
763 fi.type = FRAME_TYPE_MANAGED;
764 fi.il_offset = frame->ip - (const unsigned short *) hd->code;
765 if (!fi.method->wrapper_type)
769 if (func (&fi, ctx, user_data))
771 frame = frame->parent;
775 static MonoPIFunc mono_interp_enter_icall_trampoline = NULL;
777 static InterpMethodArguments* build_args_from_sig (MonoMethodSignature *sig, MonoInvocation *frame)
779 InterpMethodArguments *margs = g_malloc0 (sizeof (InterpMethodArguments));
782 g_assert (mono_arm_eabi_supported ());
783 int i8_align = mono_arm_i8_align ();
789 for (int i = 0; i < sig->param_count; i++) {
790 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
792 case MONO_TYPE_BOOLEAN:
803 case MONO_TYPE_SZARRAY:
804 case MONO_TYPE_CLASS:
805 case MONO_TYPE_OBJECT:
806 case MONO_TYPE_STRING:
807 case MONO_TYPE_VALUETYPE:
808 case MONO_TYPE_GENERICINST:
809 #if SIZEOF_VOID_P == 8
814 #if SIZEOF_VOID_P == 4
817 /* pairs begin at even registers */
818 if (i8_align == 8 && margs->ilen & 1)
825 #if SIZEOF_VOID_P == 8
830 #if SIZEOF_VOID_P == 4
836 g_error ("build_args_from_sig: not implemented yet (1): 0x%x\n", ptype);
841 margs->iargs = g_malloc0 (sizeof (gpointer) * margs->ilen);
844 margs->fargs = g_malloc0 (sizeof (double) * margs->flen);
846 if (margs->ilen > INTERP_ICALL_TRAMP_IARGS)
847 g_error ("build_args_from_sig: TODO, allocate gregs: %d\n", margs->ilen);
849 if (margs->flen > INTERP_ICALL_TRAMP_FARGS)
850 g_error ("build_args_from_sig: TODO, allocate fregs: %d\n", margs->flen);
857 margs->iargs [0] = frame->stack_args->data.p;
861 for (int i = 0; i < sig->param_count; i++) {
862 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
864 case MONO_TYPE_BOOLEAN:
875 case MONO_TYPE_SZARRAY:
876 case MONO_TYPE_CLASS:
877 case MONO_TYPE_OBJECT:
878 case MONO_TYPE_STRING:
879 case MONO_TYPE_VALUETYPE:
880 case MONO_TYPE_GENERICINST:
881 #if SIZEOF_VOID_P == 8
884 margs->iargs [int_i] = frame->stack_args [i].data.p;
886 g_print ("build_args_from_sig: margs->iargs [%d]: %p (frame @ %d)\n", int_i, margs->iargs [int_i], i);
890 #if SIZEOF_VOID_P == 4
892 stackval *sarg = &frame->stack_args [i];
894 /* pairs begin at even registers */
895 if (i8_align == 8 && int_i & 1)
898 margs->iargs [int_i] = (gpointer) sarg->data.pair.lo;
900 margs->iargs [int_i] = (gpointer) sarg->data.pair.hi;
902 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);
910 if (ptype == MONO_TYPE_R4)
911 * (float *) &(margs->fargs [int_f]) = (float) frame->stack_args [i].data.f;
913 margs->fargs [int_f] = frame->stack_args [i].data.f;
915 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);
917 #if SIZEOF_VOID_P == 4
924 g_error ("build_args_from_sig: not implemented yet (2): 0x%x\n", ptype);
928 switch (sig->ret->type) {
929 case MONO_TYPE_BOOLEAN:
940 case MONO_TYPE_SZARRAY:
941 case MONO_TYPE_CLASS:
942 case MONO_TYPE_OBJECT:
943 case MONO_TYPE_STRING:
945 case MONO_TYPE_VALUETYPE:
946 case MONO_TYPE_GENERICINST:
947 margs->retval = &(frame->retval->data.p);
948 margs->is_float_ret = 0;
952 margs->retval = &(frame->retval->data.p);
953 margs->is_float_ret = 1;
956 margs->retval = NULL;
959 g_error ("build_args_from_sig: ret type not implemented yet: 0x%x\n", sig->ret->type);
966 ves_pinvoke_method (MonoInvocation *frame, MonoMethodSignature *sig, MonoFuncV addr, gboolean string_ctor, ThreadContext *context)
969 MonoInvocation *old_frame = context->current_frame;
970 MonoInvocation *old_env_frame = context->env_frame;
971 jmp_buf *old_env = context->current_env;
975 context->current_frame = old_frame;
976 context->env_frame = old_env_frame;
977 context->current_env = old_env;
978 context->managed_code = 1;
983 context->env_frame = frame;
984 context->current_env = &env;
986 g_assert (!frame->runtime_method);
987 if (!mono_interp_enter_icall_trampoline) {
989 mono_interp_enter_icall_trampoline = mono_arch_get_enter_icall_trampoline (&info);
991 // mono_tramp_info_register (info, NULL);
994 InterpMethodArguments *margs = build_args_from_sig (sig, frame);
996 g_print ("ICALL: mono_interp_enter_icall_trampoline = %p, addr = %p\n", mono_interp_enter_icall_trampoline, addr);
997 g_print ("margs(out): ilen=%d, flen=%d\n", margs->ilen, margs->flen);
1000 context->current_frame = frame;
1001 context->managed_code = 0;
1003 interp_push_lmf (&ext, frame);
1005 mono_interp_enter_icall_trampoline (addr, margs);
1007 interp_pop_lmf (&ext);
1009 context->managed_code = 1;
1010 /* domain can only be changed by native code */
1011 context->domain = mono_domain_get ();
1013 if (*mono_thread_interruption_request_flag ()) {
1014 MonoException *exc = mono_thread_interruption_checkpoint ();
1017 context->search_for_handler = 1;
1021 if (!frame->ex && !MONO_TYPE_ISSTRUCT (sig->ret))
1022 stackval_from_data (sig->ret, frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
1024 context->current_frame = old_frame;
1025 context->env_frame = old_env_frame;
1026 context->current_env = old_env;
1028 g_free (margs->iargs);
1029 g_free (margs->fargs);
1034 mono_interp_init_delegate (MonoDelegate *del)
1038 /* shouldn't need a write barrier because we don't write a MonoObject into the field */
1039 del->method = ((RuntimeMethod *) del->method_ptr)->method;
1044 * runtime specifies that the implementation of the method is automatically
1045 * provided by the runtime and is primarily used for the methods of delegates.
1048 ves_runtime_method (MonoInvocation *frame, ThreadContext *context)
1050 MonoMethod *method = frame->runtime_method->method;
1051 const char *name = method->name;
1052 MonoObject *obj = (MonoObject*) frame->stack_args->data.p;
1053 MonoObject *isinst_obj;
1056 mono_class_init (method->klass);
1058 if (method->klass == mono_defaults.array_class) {
1059 if (!strcmp (method->name, "UnsafeMov")) {
1060 /* TODO: layout checks */
1061 MonoType *mt = mono_method_signature (method)->ret;
1062 stackval_from_data (mt, frame->retval, (char *) frame->stack_args, FALSE);
1065 if (!strcmp (method->name, "UnsafeLoad")) {
1066 ves_array_get (frame, FALSE);
1071 isinst_obj = mono_object_isinst_checked (obj, mono_defaults.array_class, &error);
1072 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1073 if (obj && isinst_obj) {
1074 if (*name == 'S' && (strcmp (name, "Set") == 0)) {
1075 ves_array_set (frame);
1078 if (*name == 'G' && (strcmp (name, "Get") == 0)) {
1079 ves_array_get (frame, TRUE);
1084 g_error ("Don't know how to exec runtime method %s.%s::%s",
1085 method->klass->name_space, method->klass->name,
1091 dump_stack (stackval *stack, stackval *sp)
1093 stackval *s = stack;
1094 GString *str = g_string_new ("");
1097 return g_string_free (str, FALSE);
1100 g_string_append_printf (str, "[%p (%lld)] ", s->data.l, s->data.l);
1103 return g_string_free (str, FALSE);
1108 dump_stackval (GString *str, stackval *s, MonoType *type)
1110 switch (type->type) {
1117 case MONO_TYPE_CHAR:
1118 case MONO_TYPE_BOOLEAN:
1119 g_string_append_printf (str, "[%d] ", s->data.i);
1121 case MONO_TYPE_STRING:
1122 case MONO_TYPE_SZARRAY:
1123 case MONO_TYPE_CLASS:
1124 case MONO_TYPE_OBJECT:
1125 case MONO_TYPE_ARRAY:
1129 g_string_append_printf (str, "[%p] ", s->data.p);
1131 case MONO_TYPE_VALUETYPE:
1132 if (type->data.klass->enumtype)
1133 g_string_append_printf (str, "[%d] ", s->data.i);
1135 g_string_append_printf (str, "[vt:%p] ", s->data.p);
1139 g_string_append_printf (str, "[%g] ", s->data.f);
1144 GString *res = g_string_new ("");
1145 mono_type_get_desc (res, type, TRUE);
1146 g_string_append_printf (str, "[{%s} %lld/0x%0llx] ", res->str, s->data.l, s->data.l);
1147 g_string_free (res, TRUE);
1155 dump_retval (MonoInvocation *inv)
1157 GString *str = g_string_new ("");
1158 MonoType *ret = mono_method_signature (inv->runtime_method->method)->ret;
1160 if (ret->type != MONO_TYPE_VOID)
1161 dump_stackval (str, inv->retval, ret);
1163 return g_string_free (str, FALSE);
1168 dump_args (MonoInvocation *inv)
1170 GString *str = g_string_new ("");
1172 MonoMethodSignature *signature = mono_method_signature (inv->runtime_method->method);
1174 if (signature->param_count == 0 && !signature->hasthis)
1175 return g_string_free (str, FALSE);
1177 if (signature->hasthis) {
1178 MonoMethod *method = inv->runtime_method->method;
1179 dump_stackval (str, inv->stack_args, &method->klass->byval_arg);
1182 for (i = 0; i < signature->param_count; ++i)
1183 dump_stackval (str, inv->stack_args + (!!signature->hasthis) + i, signature->params [i]);
1185 return g_string_free (str, FALSE);
1189 dump_frame (MonoInvocation *inv)
1191 GString *str = g_string_new ("");
1196 for (i = 0; inv; inv = inv->parent) {
1197 if (inv->runtime_method != NULL) {
1198 MonoMethod *method = inv->runtime_method->method;
1202 const char * opname = "";
1204 gchar *source = NULL;
1208 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
1209 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) == 0) {
1210 MonoMethodHeader *hd = mono_method_get_header_checked (method, &error);
1211 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1215 opname = mono_interp_opname [*inv->ip];
1216 codep = inv->ip - inv->runtime_method->code;
1217 source = g_strdup_printf ("%s:%d // (TODO: proper stacktrace)", method->name, codep);
1222 MonoDebugSourceLocation *minfo = mono_debug_lookup_method (method);
1223 source = mono_debug_method_lookup_location (minfo, codep);
1227 args = dump_args (inv);
1228 name = mono_method_full_name (method, TRUE);
1230 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s) at %s\n", i, codep, opname, name, args, source);
1232 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s)\n", i, codep, opname, name, args);
1239 return g_string_free (str, FALSE);
1243 get_trace_ips (MonoDomain *domain, MonoInvocation *top)
1247 MonoInvocation *inv;
1250 for (i = 0, inv = top; inv; inv = inv->parent)
1251 if (inv->runtime_method != NULL)
1254 res = mono_array_new_checked (domain, mono_defaults.int_class, 2 * i, &error);
1255 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1257 for (i = 0, inv = top; inv; inv = inv->parent)
1258 if (inv->runtime_method != NULL) {
1259 mono_array_set (res, gpointer, i, inv->runtime_method);
1261 mono_array_set (res, gpointer, i, (gpointer)inv->ip);
1269 #define MYGUINT64_MAX 18446744073709551615ULL
1270 #define MYGINT64_MAX 9223372036854775807LL
1271 #define MYGINT64_MIN (-MYGINT64_MAX -1LL)
1273 #define MYGUINT32_MAX 4294967295U
1274 #define MYGINT32_MAX 2147483647
1275 #define MYGINT32_MIN (-MYGINT32_MAX -1)
1277 #define CHECK_ADD_OVERFLOW(a,b) \
1278 (gint32)(b) >= 0 ? (gint32)(MYGINT32_MAX) - (gint32)(b) < (gint32)(a) ? -1 : 0 \
1279 : (gint32)(MYGINT32_MIN) - (gint32)(b) > (gint32)(a) ? +1 : 0
1281 #define CHECK_SUB_OVERFLOW(a,b) \
1282 (gint32)(b) < 0 ? (gint32)(MYGINT32_MAX) + (gint32)(b) < (gint32)(a) ? -1 : 0 \
1283 : (gint32)(MYGINT32_MIN) + (gint32)(b) > (gint32)(a) ? +1 : 0
1285 #define CHECK_ADD_OVERFLOW_UN(a,b) \
1286 (guint32)(MYGUINT32_MAX) - (guint32)(b) < (guint32)(a) ? -1 : 0
1288 #define CHECK_SUB_OVERFLOW_UN(a,b) \
1289 (guint32)(a) < (guint32)(b) ? -1 : 0
1291 #define CHECK_ADD_OVERFLOW64(a,b) \
1292 (gint64)(b) >= 0 ? (gint64)(MYGINT64_MAX) - (gint64)(b) < (gint64)(a) ? -1 : 0 \
1293 : (gint64)(MYGINT64_MIN) - (gint64)(b) > (gint64)(a) ? +1 : 0
1295 #define CHECK_SUB_OVERFLOW64(a,b) \
1296 (gint64)(b) < 0 ? (gint64)(MYGINT64_MAX) + (gint64)(b) < (gint64)(a) ? -1 : 0 \
1297 : (gint64)(MYGINT64_MIN) + (gint64)(b) > (gint64)(a) ? +1 : 0
1299 #define CHECK_ADD_OVERFLOW64_UN(a,b) \
1300 (guint64)(MYGUINT64_MAX) - (guint64)(b) < (guint64)(a) ? -1 : 0
1302 #define CHECK_SUB_OVERFLOW64_UN(a,b) \
1303 (guint64)(a) < (guint64)(b) ? -1 : 0
1305 #if SIZEOF_VOID_P == 4
1306 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW(a,b)
1307 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW_UN(a,b)
1309 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW64(a,b)
1310 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW64_UN(a,b)
1313 /* Resolves to TRUE if the operands would overflow */
1314 #define CHECK_MUL_OVERFLOW(a,b) \
1315 ((gint32)(a) == 0) || ((gint32)(b) == 0) ? 0 : \
1316 (((gint32)(a) > 0) && ((gint32)(b) == -1)) ? FALSE : \
1317 (((gint32)(a) < 0) && ((gint32)(b) == -1)) ? (a == - MYGINT32_MAX) : \
1318 (((gint32)(a) > 0) && ((gint32)(b) > 0)) ? (gint32)(a) > ((MYGINT32_MAX) / (gint32)(b)) : \
1319 (((gint32)(a) > 0) && ((gint32)(b) < 0)) ? (gint32)(a) > ((MYGINT32_MIN) / (gint32)(b)) : \
1320 (((gint32)(a) < 0) && ((gint32)(b) > 0)) ? (gint32)(a) < ((MYGINT32_MIN) / (gint32)(b)) : \
1321 (gint32)(a) < ((MYGINT32_MAX) / (gint32)(b))
1323 #define CHECK_MUL_OVERFLOW_UN(a,b) \
1324 ((guint32)(a) == 0) || ((guint32)(b) == 0) ? 0 : \
1325 (guint32)(b) > ((MYGUINT32_MAX) / (guint32)(a))
1327 #define CHECK_MUL_OVERFLOW64(a,b) \
1328 ((gint64)(a) == 0) || ((gint64)(b) == 0) ? 0 : \
1329 (((gint64)(a) > 0) && ((gint64)(b) == -1)) ? FALSE : \
1330 (((gint64)(a) < 0) && ((gint64)(b) == -1)) ? (a == - MYGINT64_MAX) : \
1331 (((gint64)(a) > 0) && ((gint64)(b) > 0)) ? (gint64)(a) > ((MYGINT64_MAX) / (gint64)(b)) : \
1332 (((gint64)(a) > 0) && ((gint64)(b) < 0)) ? (gint64)(a) > ((MYGINT64_MIN) / (gint64)(b)) : \
1333 (((gint64)(a) < 0) && ((gint64)(b) > 0)) ? (gint64)(a) < ((MYGINT64_MIN) / (gint64)(b)) : \
1334 (gint64)(a) < ((MYGINT64_MAX) / (gint64)(b))
1336 #define CHECK_MUL_OVERFLOW64_UN(a,b) \
1337 ((guint64)(a) == 0) || ((guint64)(b) == 0) ? 0 : \
1338 (guint64)(b) > ((MYGUINT64_MAX) / (guint64)(a))
1340 #if SIZEOF_VOID_P == 4
1341 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW(a,b)
1342 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW_UN(a,b)
1344 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW64(a,b)
1345 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW64_UN(a,b)
1349 mono_interp_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc, MonoError *error)
1351 MonoInvocation frame;
1352 ThreadContext * volatile context = mono_native_tls_get_value (thread_context_id);
1353 MonoObject *retval = NULL;
1354 MonoMethodSignature *sig = mono_method_signature (method);
1355 MonoClass *klass = mono_class_from_mono_type (sig->ret);
1356 int i, type, isobject = 0;
1360 ThreadContext context_struct;
1361 MonoInvocation *old_frame = NULL;
1371 if (context != &context_struct) {
1372 context->domain = mono_domain_get ();
1373 context->current_frame = old_frame;
1374 context->managed_code = 0;
1378 *exc = (MonoObject *)frame.ex;
1382 if (context == NULL) {
1383 context = &context_struct;
1384 memset (context, 0, sizeof (ThreadContext));
1385 context_struct.base_frame = &frame;
1386 context_struct.env_frame = &frame;
1387 context_struct.current_env = &env;
1388 set_context (context);
1391 old_frame = context->current_frame;
1393 context->domain = mono_domain_get ();
1395 switch (sig->ret->type) {
1396 case MONO_TYPE_VOID:
1398 case MONO_TYPE_STRING:
1399 case MONO_TYPE_OBJECT:
1400 case MONO_TYPE_CLASS:
1401 case MONO_TYPE_ARRAY:
1402 case MONO_TYPE_SZARRAY:
1405 case MONO_TYPE_VALUETYPE:
1406 retval = mono_object_new_checked (context->domain, klass, error);
1407 ret = mono_object_unbox (retval);
1408 if (!sig->ret->data.klass->enumtype)
1409 result.data.vt = ret;
1411 result.data.vt = alloca (mono_class_instance_size (klass));
1413 case MONO_TYPE_GENERICINST:
1414 if (!MONO_TYPE_IS_REFERENCE (sig->ret)) {
1415 retval = mono_object_new_checked (context->domain, klass, error);
1416 ret = mono_object_unbox (retval);
1417 if (!sig->ret->data.klass->enumtype)
1418 result.data.vt = ret;
1420 result.data.vt = alloca (mono_class_instance_size (klass));
1427 retval = mono_object_new_checked (context->domain, mono_defaults.int_class, error);
1428 ret = mono_object_unbox (retval);
1431 retval = mono_object_new_checked (context->domain, klass, error);
1432 ret = mono_object_unbox (retval);
1436 args = alloca (sizeof (stackval) * (sig->param_count + !!sig->hasthis));
1438 args [0].data.p = obj;
1440 for (i = 0; i < sig->param_count; ++i) {
1441 int a_index = i + !!sig->hasthis;
1442 if (sig->params [i]->byref) {
1443 args [a_index].data.p = params [i];
1446 type = sig->params [i]->type;
1451 case MONO_TYPE_BOOLEAN:
1452 args [a_index].data.i = *(MonoBoolean*)params [i];
1456 case MONO_TYPE_CHAR:
1457 args [a_index].data.i = *(gint16*)params [i];
1459 #if SIZEOF_VOID_P == 4
1460 case MONO_TYPE_U: /* use VAL_POINTER? */
1465 args [a_index].data.i = *(gint32*)params [i];
1467 #if SIZEOF_VOID_P == 8
1473 args [a_index].data.l = *(gint64*)params [i];
1476 args [a_index].data.f = *(gfloat *) params [i];
1479 args [a_index].data.f = *(gdouble *) params [i];
1481 case MONO_TYPE_VALUETYPE:
1482 if (sig->params [i]->data.klass->enumtype) {
1483 type = mono_class_enum_basetype (sig->params [i]->data.klass)->type;
1486 args [a_index].data.p = params [i];
1489 case MONO_TYPE_STRING:
1491 case MONO_TYPE_CLASS:
1492 case MONO_TYPE_ARRAY:
1493 case MONO_TYPE_SZARRAY:
1494 case MONO_TYPE_OBJECT:
1495 case MONO_TYPE_GENERICINST:
1496 args [a_index].data.p = params [i];
1499 g_error ("type 0x%x not handled in runtime invoke", sig->params [i]->type);
1503 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
1504 method = mono_marshal_get_native_wrapper (method, FALSE, FALSE);
1505 INIT_FRAME (&frame,context->current_frame,args,&result,mono_get_root_domain (),method,error);
1508 frame.invoke_trap = 1;
1509 context->managed_code = 1;
1510 ves_exec_method_with_context (&frame, context, NULL, NULL, -1);
1511 context->managed_code = 0;
1512 if (context == &context_struct)
1515 context->current_frame = old_frame;
1516 if (frame.ex != NULL) {
1518 *exc = (MonoObject*) frame.ex;
1521 if (context->current_env != NULL) {
1522 context->env_frame->ex = frame.ex;
1523 longjmp(*context->current_env, 1);
1526 printf("dropped exception...\n");
1528 if (sig->ret->type == MONO_TYPE_VOID && !method->string_ctor)
1530 if (isobject || method->string_ctor)
1531 return result.data.p;
1532 stackval_to_data (sig->ret, &result, ret, sig->pinvoke);
1537 RuntimeMethod *rmethod;
1541 gpointer *many_args;
1544 /* Main function for entering the interpreter from compiled code */
1546 interp_entry (InterpEntryData *data)
1548 MonoInvocation frame;
1549 RuntimeMethod *rmethod = data->rmethod;
1550 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
1551 ThreadContext context_struct;
1552 MonoInvocation *old_frame;
1556 MonoMethodSignature *sig;
1560 method = rmethod->method;
1561 sig = mono_method_signature (method);
1563 // FIXME: Optimize this
1565 //printf ("%s\n", mono_method_full_name (method, 1));
1568 if (context == NULL) {
1569 context = &context_struct;
1570 memset (context, 0, sizeof (ThreadContext));
1571 context_struct.base_frame = &frame;
1572 context_struct.env_frame = &frame;
1573 set_context (context);
1575 old_frame = context->current_frame;
1577 context->domain = mono_domain_get ();
1579 args = alloca (sizeof (stackval) * (sig->param_count + (sig->hasthis ? 1 : 0)));
1581 args [0].data.p = data->this_arg;
1584 if (data->many_args)
1585 params = data->many_args;
1587 params = data->args;
1588 for (i = 0; i < sig->param_count; ++i) {
1589 int a_index = i + (sig->hasthis ? 1 : 0);
1590 if (sig->params [i]->byref) {
1591 args [a_index].data.p = params [i];
1594 type = rmethod->param_types [i];
1595 switch (type->type) {
1598 args [a_index].data.i = *(MonoBoolean*)params [i];
1602 args [a_index].data.i = *(gint16*)params [i];
1605 #if SIZEOF_VOID_P == 4
1606 args [a_index].data.p = GINT_TO_POINTER (*(guint32*)params [i]);
1608 args [a_index].data.p = GINT_TO_POINTER (*(guint64*)params [i]);
1612 #if SIZEOF_VOID_P == 4
1613 args [a_index].data.p = GINT_TO_POINTER (*(gint32*)params [i]);
1615 args [a_index].data.p = GINT_TO_POINTER (*(gint64*)params [i]);
1619 args [a_index].data.i = *(guint32*)params [i];
1622 args [a_index].data.i = *(gint32*)params [i];
1625 args [a_index].data.l = *(guint64*)params [i];
1628 args [a_index].data.l = *(gint64*)params [i];
1631 case MONO_TYPE_OBJECT:
1632 args [a_index].data.p = *(MonoObject**)params [i];
1634 case MONO_TYPE_VALUETYPE:
1635 args [a_index].data.p = params [i];
1637 case MONO_TYPE_GENERICINST:
1638 if (MONO_TYPE_IS_REFERENCE (type))
1639 args [a_index].data.p = params [i];
1641 args [a_index].data.vt = params [i];
1644 printf ("%s\n", mono_type_full_name (sig->params [i]));
1650 init_frame (&frame, NULL, data->rmethod, args, &result);
1651 context->managed_code = 1;
1653 type = rmethod->rtype;
1654 switch (type->type) {
1655 case MONO_TYPE_GENERICINST:
1656 if (!MONO_TYPE_IS_REFERENCE (type))
1657 frame.retval->data.vt = data->res;
1659 case MONO_TYPE_VALUETYPE:
1660 frame.retval->data.vt = data->res;
1666 ves_exec_method_with_context (&frame, context, NULL, NULL, -1);
1667 context->managed_code = 0;
1668 if (context == &context_struct)
1671 context->current_frame = old_frame;
1674 g_assert (frame.ex == NULL);
1676 type = rmethod->rtype;
1677 switch (type->type) {
1678 case MONO_TYPE_VOID:
1681 *(gint8*)data->res = frame.retval->data.i;
1684 *(guint8*)data->res = frame.retval->data.i;
1687 *(gint16*)data->res = frame.retval->data.i;
1690 *(guint16*)data->res = frame.retval->data.i;
1693 *(gint32*)data->res = frame.retval->data.i;
1696 *(guint64*)data->res = frame.retval->data.i;
1699 *(gint64*)data->res = frame.retval->data.i;
1702 *(guint64*)data->res = frame.retval->data.i;
1705 #if SIZEOF_VOID_P == 8
1706 *(gint64*)data->res = (gint64)frame.retval->data.p;
1708 *(gint32*)data->res = (gint32)frame.retval->data.p;
1712 #if SIZEOF_VOID_P == 8
1713 *(guint64*)data->res = (guint64)frame.retval->data.p;
1715 *(guint32*)data->res = (guint32)frame.retval->data.p;
1718 case MONO_TYPE_OBJECT:
1719 /* No need for a write barrier */
1720 *(MonoObject**)data->res = (MonoObject*)frame.retval->data.p;
1722 case MONO_TYPE_GENERICINST:
1723 if (MONO_TYPE_IS_REFERENCE (type)) {
1724 *(MonoObject**)data->res = *(MonoObject**)frame.retval->data.p;
1726 /* Already set before the call */
1729 case MONO_TYPE_VALUETYPE:
1730 /* Already set before the call */
1733 printf ("%s\n", mono_type_full_name (sig->ret));
1740 do_icall (ThreadContext *context, int op, stackval *sp, gpointer ptr)
1742 MonoInvocation *old_frame = context->current_frame;
1743 MonoInvocation *old_env_frame = context->env_frame;
1744 jmp_buf *old_env = context->current_env;
1748 context->current_frame = old_frame;
1749 context->env_frame = old_env_frame;
1750 context->current_env = old_env;
1751 context->managed_code = 1;
1755 context->env_frame = context->current_frame;
1756 context->current_env = &env;
1757 context->managed_code = 0;
1760 case MINT_ICALL_V_V: {
1761 void (*func)(void) = ptr;
1765 case MINT_ICALL_V_P: {
1766 gpointer (*func)(void) = ptr;
1768 sp [-1].data.p = func ();
1771 case MINT_ICALL_P_V: {
1772 void (*func)(gpointer) = ptr;
1773 func (sp [-1].data.p);
1777 case MINT_ICALL_P_P: {
1778 gpointer (*func)(gpointer) = ptr;
1779 sp [-1].data.p = func (sp [-1].data.p);
1782 case MINT_ICALL_PP_V: {
1783 void (*func)(gpointer,gpointer) = ptr;
1785 func (sp [0].data.p, sp [1].data.p);
1788 case MINT_ICALL_PI_V: {
1789 void (*func)(gpointer,int) = ptr;
1791 func (sp [0].data.p, sp [1].data.i);
1794 case MINT_ICALL_PP_P: {
1795 gpointer (*func)(gpointer,gpointer) = ptr;
1797 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.p);
1800 case MINT_ICALL_PI_P: {
1801 gpointer (*func)(gpointer,int) = ptr;
1803 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.i);
1806 case MINT_ICALL_PPP_V: {
1807 void (*func)(gpointer,gpointer,gpointer) = ptr;
1809 func (sp [0].data.p, sp [1].data.p, sp [2].data.p);
1812 case MINT_ICALL_PPI_V: {
1813 void (*func)(gpointer,gpointer,int) = ptr;
1815 func (sp [0].data.p, sp [1].data.p, sp [2].data.i);
1819 g_assert_not_reached ();
1822 context->env_frame = old_env_frame;
1823 context->current_env = old_env;
1829 * These functions are the entry points into the interpreter from compiled code.
1830 * They are called by the interp_in wrappers. They have the following signature:
1831 * void (<optional this_arg>, <optional retval pointer>, <arg1>, ..., <argn>, <method ptr>)
1832 * They pack up their arguments into an InterpEntryData structure and call interp_entry ().
1833 * It would be possible for the wrappers to pack up the arguments etc, but that would make them bigger, and there are
1834 * more wrappers then these functions.
1835 * this/static * ret/void * 16 arguments -> 64 functions.
1838 #define MAX_INTERP_ENTRY_ARGS 8
1840 #define INTERP_ENTRY_BASE(_method, _this_arg, _res) \
1841 InterpEntryData data; \
1842 (data).rmethod = (_method); \
1843 (data).res = (_res); \
1844 (data).this_arg = (_this_arg); \
1845 (data).many_args = NULL;
1847 #define INTERP_ENTRY0(_this_arg, _res, _method) { \
1848 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
1849 interp_entry (&data); \
1851 #define INTERP_ENTRY1(_this_arg, _res, _method) { \
1852 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
1853 (data).args [0] = arg1; \
1854 interp_entry (&data); \
1856 #define INTERP_ENTRY2(_this_arg, _res, _method) { \
1857 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
1858 (data).args [0] = arg1; \
1859 (data).args [1] = arg2; \
1860 interp_entry (&data); \
1862 #define INTERP_ENTRY3(_this_arg, _res, _method) { \
1863 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
1864 (data).args [0] = arg1; \
1865 (data).args [1] = arg2; \
1866 (data).args [2] = arg3; \
1867 interp_entry (&data); \
1869 #define INTERP_ENTRY4(_this_arg, _res, _method) { \
1870 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
1871 (data).args [0] = arg1; \
1872 (data).args [1] = arg2; \
1873 (data).args [2] = arg3; \
1874 (data).args [3] = arg4; \
1875 interp_entry (&data); \
1877 #define INTERP_ENTRY5(_this_arg, _res, _method) { \
1878 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
1879 (data).args [0] = arg1; \
1880 (data).args [1] = arg2; \
1881 (data).args [2] = arg3; \
1882 (data).args [3] = arg4; \
1883 (data).args [4] = arg5; \
1884 interp_entry (&data); \
1886 #define INTERP_ENTRY6(_this_arg, _res, _method) { \
1887 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
1888 (data).args [0] = arg1; \
1889 (data).args [1] = arg2; \
1890 (data).args [2] = arg3; \
1891 (data).args [3] = arg4; \
1892 (data).args [4] = arg5; \
1893 (data).args [5] = arg6; \
1894 interp_entry (&data); \
1896 #define INTERP_ENTRY7(_this_arg, _res, _method) { \
1897 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
1898 (data).args [0] = arg1; \
1899 (data).args [1] = arg2; \
1900 (data).args [2] = arg3; \
1901 (data).args [3] = arg4; \
1902 (data).args [4] = arg5; \
1903 (data).args [5] = arg6; \
1904 (data).args [6] = arg7; \
1905 interp_entry (&data); \
1907 #define INTERP_ENTRY8(_this_arg, _res, _method) { \
1908 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
1909 (data).args [0] = arg1; \
1910 (data).args [1] = arg2; \
1911 (data).args [2] = arg3; \
1912 (data).args [3] = arg4; \
1913 (data).args [4] = arg5; \
1914 (data).args [5] = arg6; \
1915 (data).args [6] = arg7; \
1916 (data).args [7] = arg8; \
1917 interp_entry (&data); \
1920 #define ARGLIST0 RuntimeMethod *rmethod
1921 #define ARGLIST1 gpointer arg1, RuntimeMethod *rmethod
1922 #define ARGLIST2 gpointer arg1, gpointer arg2, RuntimeMethod *rmethod
1923 #define ARGLIST3 gpointer arg1, gpointer arg2, gpointer arg3, RuntimeMethod *rmethod
1924 #define ARGLIST4 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, RuntimeMethod *rmethod
1925 #define ARGLIST5 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, RuntimeMethod *rmethod
1926 #define ARGLIST6 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, gpointer arg6, RuntimeMethod *rmethod
1927 #define ARGLIST7 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, gpointer arg6, gpointer arg7, RuntimeMethod *rmethod
1928 #define ARGLIST8 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, gpointer arg6, gpointer arg7, gpointer arg8, RuntimeMethod *rmethod
1930 static void interp_entry_static_0 (ARGLIST0) INTERP_ENTRY0 (NULL, NULL, rmethod)
1931 static void interp_entry_static_1 (ARGLIST1) INTERP_ENTRY1 (NULL, NULL, rmethod)
1932 static void interp_entry_static_2 (ARGLIST2) INTERP_ENTRY2 (NULL, NULL, rmethod)
1933 static void interp_entry_static_3 (ARGLIST3) INTERP_ENTRY3 (NULL, NULL, rmethod)
1934 static void interp_entry_static_4 (ARGLIST4) INTERP_ENTRY4 (NULL, NULL, rmethod)
1935 static void interp_entry_static_5 (ARGLIST5) INTERP_ENTRY5 (NULL, NULL, rmethod)
1936 static void interp_entry_static_6 (ARGLIST6) INTERP_ENTRY6 (NULL, NULL, rmethod)
1937 static void interp_entry_static_7 (ARGLIST7) INTERP_ENTRY7 (NULL, NULL, rmethod)
1938 static void interp_entry_static_8 (ARGLIST8) INTERP_ENTRY8 (NULL, NULL, rmethod)
1939 static void interp_entry_static_ret_0 (gpointer res, ARGLIST0) INTERP_ENTRY0 (NULL, res, rmethod)
1940 static void interp_entry_static_ret_1 (gpointer res, ARGLIST1) INTERP_ENTRY1 (NULL, res, rmethod)
1941 static void interp_entry_static_ret_2 (gpointer res, ARGLIST2) INTERP_ENTRY2 (NULL, res, rmethod)
1942 static void interp_entry_static_ret_3 (gpointer res, ARGLIST3) INTERP_ENTRY3 (NULL, res, rmethod)
1943 static void interp_entry_static_ret_4 (gpointer res, ARGLIST4) INTERP_ENTRY4 (NULL, res, rmethod)
1944 static void interp_entry_static_ret_5 (gpointer res, ARGLIST5) INTERP_ENTRY5 (NULL, res, rmethod)
1945 static void interp_entry_static_ret_6 (gpointer res, ARGLIST6) INTERP_ENTRY6 (NULL, res, rmethod)
1946 static void interp_entry_static_ret_7 (gpointer res, ARGLIST7) INTERP_ENTRY7 (NULL, res, rmethod)
1947 static void interp_entry_static_ret_8 (gpointer res, ARGLIST8) INTERP_ENTRY8 (NULL, res, rmethod)
1948 static void interp_entry_instance_0 (gpointer this_arg, ARGLIST0) INTERP_ENTRY0 (this_arg, NULL, rmethod)
1949 static void interp_entry_instance_1 (gpointer this_arg, ARGLIST1) INTERP_ENTRY1 (this_arg, NULL, rmethod)
1950 static void interp_entry_instance_2 (gpointer this_arg, ARGLIST2) INTERP_ENTRY2 (this_arg, NULL, rmethod)
1951 static void interp_entry_instance_3 (gpointer this_arg, ARGLIST3) INTERP_ENTRY3 (this_arg, NULL, rmethod)
1952 static void interp_entry_instance_4 (gpointer this_arg, ARGLIST4) INTERP_ENTRY4 (this_arg, NULL, rmethod)
1953 static void interp_entry_instance_5 (gpointer this_arg, ARGLIST5) INTERP_ENTRY5 (this_arg, NULL, rmethod)
1954 static void interp_entry_instance_6 (gpointer this_arg, ARGLIST6) INTERP_ENTRY6 (this_arg, NULL, rmethod)
1955 static void interp_entry_instance_7 (gpointer this_arg, ARGLIST7) INTERP_ENTRY7 (this_arg, NULL, rmethod)
1956 static void interp_entry_instance_8 (gpointer this_arg, ARGLIST8) INTERP_ENTRY8 (this_arg, NULL, rmethod)
1957 static void interp_entry_instance_ret_0 (gpointer this_arg, gpointer res, ARGLIST0) INTERP_ENTRY0 (this_arg, res, rmethod)
1958 static void interp_entry_instance_ret_1 (gpointer this_arg, gpointer res, ARGLIST1) INTERP_ENTRY1 (this_arg, res, rmethod)
1959 static void interp_entry_instance_ret_2 (gpointer this_arg, gpointer res, ARGLIST2) INTERP_ENTRY2 (this_arg, res, rmethod)
1960 static void interp_entry_instance_ret_3 (gpointer this_arg, gpointer res, ARGLIST3) INTERP_ENTRY3 (this_arg, res, rmethod)
1961 static void interp_entry_instance_ret_4 (gpointer this_arg, gpointer res, ARGLIST4) INTERP_ENTRY4 (this_arg, res, rmethod)
1962 static void interp_entry_instance_ret_5 (gpointer this_arg, gpointer res, ARGLIST5) INTERP_ENTRY5 (this_arg, res, rmethod)
1963 static void interp_entry_instance_ret_6 (gpointer this_arg, gpointer res, ARGLIST6) INTERP_ENTRY6 (this_arg, res, rmethod)
1964 static void interp_entry_instance_ret_7 (gpointer this_arg, gpointer res, ARGLIST7) INTERP_ENTRY6 (this_arg, res, rmethod)
1965 static void interp_entry_instance_ret_8 (gpointer this_arg, gpointer res, ARGLIST8) INTERP_ENTRY6 (this_arg, res, rmethod)
1967 #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
1969 gpointer entry_funcs_static [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (static) };
1970 gpointer entry_funcs_static_ret [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (static_ret) };
1971 gpointer entry_funcs_instance [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (instance) };
1972 gpointer entry_funcs_instance_ret [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (instance_ret) };
1974 /* General version for methods with more than MAX_INTERP_ENTRY_ARGS arguments */
1976 interp_entry_general (gpointer this_arg, gpointer res, gpointer *args, gpointer rmethod)
1978 INTERP_ENTRY_BASE (rmethod, this_arg, res);
1979 data.many_args = args;
1980 interp_entry (&data);
1984 * mono_interp_create_method_pointer:
1986 * Return a function pointer which can be used to call METHOD using the
1987 * interpreter. Return NULL for methods which are not supported.
1990 mono_interp_create_method_pointer (MonoMethod *method, MonoError *error)
1993 MonoMethodSignature *sig = mono_method_signature (method);
1994 MonoMethod *wrapper;
1995 RuntimeMethod *rmethod;
1997 /* HACK: method_ptr of delegate should point to a runtime method*/
1998 if (method->wrapper_type && method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
1999 return mono_interp_get_runtime_method (mono_domain_get (), method, error);
2001 rmethod = mono_interp_get_runtime_method (mono_domain_get (), method, error);
2002 if (rmethod->jit_entry)
2003 return rmethod->jit_entry;
2004 wrapper = mini_get_interp_in_wrapper (sig);
2006 gpointer jit_wrapper = mono_jit_compile_method_jit_only (wrapper, error);
2007 mono_error_assert_ok (error);
2009 //printf ("%s %s\n", mono_method_full_name (method, 1), mono_method_full_name (wrapper, 1));
2010 gpointer entry_func;
2011 if (sig->param_count > MAX_INTERP_ENTRY_ARGS) {
2012 entry_func = interp_entry_general;
2013 } else if (sig->hasthis) {
2014 if (sig->ret->type == MONO_TYPE_VOID)
2015 entry_func = entry_funcs_instance [sig->param_count];
2017 entry_func = entry_funcs_instance_ret [sig->param_count];
2019 if (sig->ret->type == MONO_TYPE_VOID)
2020 entry_func = entry_funcs_static [sig->param_count];
2022 entry_func = entry_funcs_static_ret [sig->param_count];
2024 g_assert (entry_func);
2026 /* This is the argument passed to the interp_in wrapper by the static rgctx trampoline */
2027 MonoFtnDesc *ftndesc = g_new0 (MonoFtnDesc, 1);
2028 ftndesc->addr = entry_func;
2029 ftndesc->arg = rmethod;
2030 mono_error_assert_ok (error);
2033 * The wrapper is called by compiled code, which doesn't pass the extra argument, so we pass it in the
2034 * rgctx register using a trampoline.
2038 g_assert (!mono_aot_only);
2039 addr = mono_arch_get_static_rgctx_trampoline (ftndesc, jit_wrapper);
2041 mono_memory_barrier ();
2042 rmethod->jit_entry = addr;
2048 static int opcode_counts[512];
2050 #define COUNT_OP(op) opcode_counts[op]++
2052 #define COUNT_OP(op)
2056 #define DUMP_INSTR() \
2057 if (tracing > 1) { \
2059 if (sp > frame->stack) { \
2060 ins = dump_stack (frame->stack, sp); \
2062 ins = g_strdup (""); \
2066 char *mn = mono_method_full_name (frame->runtime_method->method, FALSE); \
2067 g_print ("(%p) %s -> ", mono_thread_internal_current (), mn); \
2069 mono_interp_dis_mintop(rtm->code, ip); \
2070 g_print ("\t%d:%s\n", vt_sp - vtalloc, ins); \
2074 #define DUMP_INSTR()
2078 #define USE_COMPUTED_GOTO 1
2080 #if USE_COMPUTED_GOTO
2081 #define MINT_IN_SWITCH(op) COUNT_OP(op); goto *in_labels[op];
2082 #define MINT_IN_CASE(x) LAB_ ## x:
2084 #define MINT_IN_BREAK if (tracing > 1) goto main_loop; else { COUNT_OP(*ip); goto *in_labels[*ip]; }
2086 #define MINT_IN_BREAK { COUNT_OP(*ip); goto *in_labels[*ip]; }
2088 #define MINT_IN_DEFAULT mint_default: if (0) goto mint_default; /* make gcc shut up */
2090 #define MINT_IN_SWITCH(op) switch (op)
2091 #define MINT_IN_CASE(x) case x:
2092 #define MINT_IN_BREAK break
2093 #define MINT_IN_DEFAULT default:
2097 * If EXIT_AT_FINALLY is not -1, exit after exiting the finally clause with that index.
2100 ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, unsigned short *start_with_ip, MonoException *filter_exception, int exit_at_finally)
2102 MonoInvocation child_frame;
2103 GSList *finally_ips = NULL;
2104 const unsigned short *endfinally_ip = NULL;
2105 const unsigned short *ip = NULL;
2106 register stackval *sp;
2109 gint tracing = global_tracing;
2110 unsigned char *vtalloc;
2115 unsigned char *vt_sp;
2116 unsigned char *locals;
2118 MonoObject *o = NULL;
2120 #if USE_COMPUTED_GOTO
2121 static void *in_labels[] = {
2122 #define OPDEF(a,b,c,d) \
2124 #include "mintops.def"
2129 frame->ex_handler = NULL;
2131 context->current_frame = frame;
2133 debug_enter (frame, &tracing);
2135 if (!frame->runtime_method->transformed) {
2136 context->managed_code = 0;
2138 char *mn = mono_method_full_name (frame->runtime_method->method, TRUE);
2139 g_print ("(%p) Transforming %s\n", mono_thread_internal_current (), mn);
2145 /* Use the parent frame as the current frame is not complete yet */
2146 interp_push_lmf (&ext, frame->parent);
2148 frame->ex = mono_interp_transform_method (frame->runtime_method, context);
2149 context->managed_code = 1;
2151 interp_pop_lmf (&ext);
2160 rtm = frame->runtime_method;
2161 if (!start_with_ip ) {
2162 frame->args = alloca (rtm->alloca_size);
2163 memset (frame->args, 0, rtm->alloca_size);
2169 sp = frame->stack = (stackval *) ((char *) frame->args + rtm->args_size);
2170 vt_sp = (unsigned char *) sp + rtm->stack_size;
2174 locals = (unsigned char *) vt_sp + rtm->vt_stack_size;
2175 frame->locals = locals;
2176 child_frame.parent = frame;
2178 if (filter_exception) {
2179 sp->data.p = filter_exception;
2184 * using while (ip < end) may result in a 15% performance drop,
2185 * but it may be useful for debug
2189 /* g_assert (sp >= frame->stack); */
2190 /* g_assert(vt_sp - vtalloc <= rtm->vt_stack_size); */
2192 MINT_IN_SWITCH (*ip) {
2193 MINT_IN_CASE(MINT_INITLOCALS)
2194 memset (locals, 0, rtm->locals_size);
2197 MINT_IN_CASE(MINT_NOP)
2200 MINT_IN_CASE(MINT_BREAK) {
2205 interp_push_lmf (&ext, frame);
2207 mono_debugger_agent_user_break ();
2209 interp_pop_lmf (&ext);
2212 MINT_IN_CASE(MINT_LDNULL)
2217 MINT_IN_CASE(MINT_VTRESULT) {
2218 int ret_size = * (guint16 *)(ip + 1);
2219 unsigned char *ret_vt_sp = vt_sp;
2220 vt_sp -= READ32(ip + 2);
2222 memmove (vt_sp, ret_vt_sp, ret_size);
2223 sp [-1].data.p = vt_sp;
2224 vt_sp += (ret_size + 7) & ~7;
2229 #define LDC(n) do { sp->data.i = (n); ++ip; ++sp; } while (0)
2230 MINT_IN_CASE(MINT_LDC_I4_M1)
2233 MINT_IN_CASE(MINT_LDC_I4_0)
2236 MINT_IN_CASE(MINT_LDC_I4_1)
2239 MINT_IN_CASE(MINT_LDC_I4_2)
2242 MINT_IN_CASE(MINT_LDC_I4_3)
2245 MINT_IN_CASE(MINT_LDC_I4_4)
2248 MINT_IN_CASE(MINT_LDC_I4_5)
2251 MINT_IN_CASE(MINT_LDC_I4_6)
2254 MINT_IN_CASE(MINT_LDC_I4_7)
2257 MINT_IN_CASE(MINT_LDC_I4_8)
2260 MINT_IN_CASE(MINT_LDC_I4_S)
2261 sp->data.i = *(const short *)(ip + 1);
2265 MINT_IN_CASE(MINT_LDC_I4)
2267 sp->data.i = READ32 (ip);
2271 MINT_IN_CASE(MINT_LDC_I8)
2273 sp->data.l = READ64 (ip);
2277 MINT_IN_CASE(MINT_LDC_R4) {
2281 sp->data.f = * (float *)&val;
2286 MINT_IN_CASE(MINT_LDC_R8)
2287 sp->data.l = READ64 (ip + 1); /* note union usage */
2291 MINT_IN_CASE(MINT_DUP)
2296 MINT_IN_CASE(MINT_DUP_VT)
2297 i32 = READ32 (ip + 1);
2299 memcpy(sp->data.p, sp [-1].data.p, i32);
2300 vt_sp += (i32 + 7) & ~7;
2304 MINT_IN_CASE(MINT_POP) {
2305 guint16 u16 = (* (guint16 *)(ip + 1)) + 1;
2307 memmove (sp - u16, sp - 1, (u16 - 1) * sizeof (stackval));
2312 MINT_IN_CASE(MINT_JMP) {
2313 RuntimeMethod *new_method = rtm->data_items [* (guint16 *)(ip + 1)];
2314 if (!new_method->transformed) {
2316 frame->ex = mono_interp_transform_method (new_method, context);
2321 if (new_method->alloca_size > rtm->alloca_size)
2322 g_error ("MINT_JMP to method which needs more stack space (%d > %d)", new_method->alloca_size, rtm->alloca_size);
2323 rtm = frame->runtime_method = new_method;
2324 vt_sp = (unsigned char *) sp + rtm->stack_size;
2328 locals = vt_sp + rtm->vt_stack_size;
2329 frame->locals = locals;
2330 ip = rtm->new_body_start; /* bypass storing input args from callers frame */
2333 MINT_IN_CASE(MINT_CALLI) {
2334 MonoMethodSignature *csignature;
2335 stackval *endsp = sp;
2339 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
2343 child_frame.runtime_method = sp->data.p;
2346 child_frame.retval = sp;
2347 /* decrement by the actual number of args */
2348 sp -= csignature->param_count;
2349 if (csignature->hasthis)
2351 child_frame.stack_args = sp;
2353 /* `this' can be NULL for string:.ctor */
2354 if (csignature->hasthis && sp->data.p && mono_object_is_transparent_proxy (sp->data.p)) {
2355 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
2356 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2357 } else if (child_frame.runtime_method->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
2358 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_native_wrapper (child_frame.runtime_method->method, FALSE, FALSE), &error);
2359 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2362 if (csignature->hasthis) {
2363 MonoObject *this_arg = sp->data.p;
2365 if (this_arg->vtable->klass->valuetype) {
2366 gpointer *unboxed = mono_object_unbox (this_arg);
2367 sp [0].data.p = unboxed;
2371 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2373 context->current_frame = frame;
2375 if (context->has_resume_state) {
2376 if (frame == context->handler_frame)
2377 SET_RESUME_STATE (context);
2382 if (child_frame.ex) {
2384 * An exception occurred, need to run finally, fault and catch handlers..
2386 frame->ex = child_frame.ex;
2387 goto handle_finally;
2390 /* need to handle typedbyref ... */
2391 if (csignature->ret->type != MONO_TYPE_VOID) {
2397 MINT_IN_CASE(MINT_CALLI_NAT) {
2398 MonoMethodSignature *csignature;
2399 stackval *endsp = sp;
2400 unsigned char *code = NULL;
2404 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
2409 child_frame.runtime_method = NULL;
2412 child_frame.retval = sp;
2413 /* decrement by the actual number of args */
2414 sp -= csignature->param_count;
2415 if (csignature->hasthis)
2417 child_frame.stack_args = sp;
2418 ves_pinvoke_method (&child_frame, csignature, (MonoFuncV) code, FALSE, context);
2420 context->current_frame = frame;
2422 if (context->has_resume_state) {
2423 if (frame == context->handler_frame)
2424 SET_RESUME_STATE (context);
2429 if (child_frame.ex) {
2431 * An exception occurred, need to run finally, fault and catch handlers..
2433 frame->ex = child_frame.ex;
2434 if (context->search_for_handler) {
2435 context->search_for_handler = 0;
2436 goto handle_exception;
2438 goto handle_finally;
2441 /* need to handle typedbyref ... */
2442 if (csignature->ret->type != MONO_TYPE_VOID) {
2448 MINT_IN_CASE(MINT_CALL) {
2449 stackval *endsp = sp;
2453 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
2456 child_frame.retval = sp;
2457 /* decrement by the actual number of args */
2458 sp -= child_frame.runtime_method->param_count;
2459 if (child_frame.runtime_method->hasthis)
2461 child_frame.stack_args = sp;
2463 /* `this' can be NULL for string:.ctor */
2464 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->method->klass->valuetype && sp->data.p && mono_object_is_transparent_proxy (sp->data.p)) {
2465 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
2466 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2469 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2471 context->current_frame = frame;
2473 if (context->has_resume_state) {
2474 if (frame == context->handler_frame)
2475 SET_RESUME_STATE (context);
2480 if (child_frame.ex) {
2482 * An exception occurred, need to run finally, fault and catch handlers..
2484 frame->ex = child_frame.ex;
2485 goto handle_exception;;
2488 /* need to handle typedbyref ... */
2493 MINT_IN_CASE(MINT_VCALL) {
2496 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
2500 child_frame.retval = sp;
2501 /* decrement by the actual number of args */
2502 sp -= child_frame.runtime_method->param_count;
2503 if (child_frame.runtime_method->hasthis) {
2505 MonoObject *this_arg = sp->data.p;
2507 THROW_EX (mono_get_exception_null_reference(), ip - 2);
2509 child_frame.stack_args = sp;
2511 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->method->klass->valuetype && mono_object_is_transparent_proxy (sp->data.p)) {
2512 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
2513 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2516 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2518 context->current_frame = frame;
2520 if (context->has_resume_state) {
2521 if (frame == context->handler_frame)
2522 SET_RESUME_STATE (context);
2527 if (child_frame.ex) {
2529 * An exception occurred, need to run finally, fault and catch handlers..
2531 frame->ex = child_frame.ex;
2532 goto handle_finally;
2537 MINT_IN_CASE(MINT_JIT_CALL) {
2538 MonoMethodSignature *sig;
2539 RuntimeMethod *rmethod = rtm->data_items [* (guint16 *)(ip + 1)];
2540 MonoFtnDesc ftndesc;
2541 guint8 res_buf [256];
2545 //printf ("%s\n", mono_method_full_name (rmethod->method, 1));
2548 * Call JITted code through a gsharedvt_out wrapper. These wrappers receive every argument
2549 * by ref and return a return value using an explicit return value argument.
2551 if (!rmethod->jit_wrapper) {
2552 MonoMethod *method = rmethod->method;
2555 sig = mono_method_signature (method);
2558 MonoMethod *wrapper = mini_get_gsharedvt_out_sig_wrapper (sig);
2559 //printf ("J: %s %s\n", mono_method_full_name (method, 1), mono_method_full_name (wrapper, 1));
2561 gpointer jit_wrapper = mono_jit_compile_method_jit_only (wrapper, &error);
2562 mono_error_assert_ok (&error);
2564 gpointer addr = mono_jit_compile_method_jit_only (method, &error);
2566 mono_error_assert_ok (&error);
2568 rmethod->jit_addr = addr;
2569 rmethod->jit_sig = sig;
2570 mono_memory_barrier ();
2571 rmethod->jit_wrapper = jit_wrapper;
2574 sig = rmethod->jit_sig;
2579 sp -= sig->param_count;
2583 ftndesc.addr = rmethod->jit_addr;
2586 // FIXME: Optimize this
2590 int stack_index = 0;
2591 if (rmethod->hasthis) {
2592 args [pindex ++] = sp [0].data.p;
2595 type = rmethod->rtype;
2596 if (type->type != MONO_TYPE_VOID) {
2597 if (MONO_TYPE_ISSTRUCT (type))
2598 args [pindex ++] = vt_sp;
2600 args [pindex ++] = res_buf;
2602 for (int i = 0; i < rmethod->param_count; ++i) {
2603 MonoType *t = rmethod->param_types [i];
2604 stackval *sval = &sp [stack_index + i];
2605 if (sig->params [i]->byref) {
2606 args [pindex ++] = sval->data.p;
2607 } else if (MONO_TYPE_ISSTRUCT (t)) {
2608 args [pindex ++] = sval->data.p;
2609 } else if (MONO_TYPE_IS_REFERENCE (t)) {
2610 args [pindex ++] = &sval->data.p;
2619 case MONO_TYPE_VALUETYPE:
2620 args [pindex ++] = &sval->data.i;
2623 case MONO_TYPE_FNPTR:
2626 case MONO_TYPE_OBJECT:
2627 args [pindex ++] = &sval->data.p;
2631 args [pindex ++] = &sval->data.l;
2634 printf ("%s\n", mono_type_full_name (t));
2635 g_assert_not_reached ();
2640 interp_push_lmf (&ext, frame);
2644 void (*func)(gpointer) = rmethod->jit_wrapper;
2650 void (*func)(gpointer, gpointer) = rmethod->jit_wrapper;
2652 func (args [0], &ftndesc);
2656 void (*func)(gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
2658 func (args [0], args [1], &ftndesc);
2662 void (*func)(gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
2664 func (args [0], args [1], args [2], &ftndesc);
2668 void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
2670 func (args [0], args [1], args [2], args [3], &ftndesc);
2674 void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
2676 func (args [0], args [1], args [2], args [3], args [4], &ftndesc);
2680 void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
2682 func (args [0], args [1], args [2], args [3], args [4], args [5], &ftndesc);
2686 void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
2688 func (args [0], args [1], args [2], args [3], args [4], args [5], args [6], &ftndesc);
2692 g_assert_not_reached ();
2696 interp_pop_lmf (&ext);
2698 if (context->has_resume_state) {
2700 * If this bit is set, it means the call has thrown the exception, and we
2701 * reached this point because the EH code in mono_handle_exception ()
2702 * unwound all the JITted frames below us. mono_interp_set_resume_state ()
2703 * has set the fields in context to indicate where we have to resume execution.
2705 if (frame == context->handler_frame)
2706 SET_RESUME_STATE (context);
2711 MonoType *rtype = rmethod->rtype;
2712 switch (rtype->type) {
2713 case MONO_TYPE_VOID:
2714 case MONO_TYPE_OBJECT:
2715 case MONO_TYPE_STRING:
2716 case MONO_TYPE_CLASS:
2717 case MONO_TYPE_ARRAY:
2718 case MONO_TYPE_SZARRAY:
2721 sp->data.p = *(gpointer*)res_buf;
2724 sp->data.i = *(gint8*)res_buf;
2727 sp->data.i = *(guint8*)res_buf;
2730 sp->data.i = *(gint16*)res_buf;
2733 sp->data.i = *(guint16*)res_buf;
2736 sp->data.i = *(gint32*)res_buf;
2739 sp->data.i = *(guint32*)res_buf;
2741 case MONO_TYPE_VALUETYPE:
2742 /* The result was written to vt_sp */
2745 case MONO_TYPE_GENERICINST:
2746 if (MONO_TYPE_IS_REFERENCE (rtype)) {
2747 sp->data.p = *(gpointer*)res_buf;
2749 /* The result was written to vt_sp */
2754 printf ("%s\n", mono_type_full_name (rtype));
2755 g_assert_not_reached ();
2758 if (rtype->type != MONO_TYPE_VOID)
2763 MINT_IN_CASE(MINT_CALLVIRT) {
2764 stackval *endsp = sp;
2765 MonoObject *this_arg;
2770 token = * (unsigned short *)(ip + 1);
2772 child_frame.runtime_method = rtm->data_items [token];
2774 child_frame.retval = sp;
2776 /* decrement by the actual number of args */
2777 sp -= child_frame.runtime_method->param_count + 1;
2778 child_frame.stack_args = sp;
2779 this_arg = sp->data.p;
2781 THROW_EX (mono_get_exception_null_reference(), ip - 2);
2782 child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
2784 MonoClass *this_class = this_arg->vtable->klass;
2785 if (this_class->valuetype && child_frame.runtime_method->method->klass->valuetype) {
2787 gpointer *unboxed = mono_object_unbox (this_arg);
2788 sp [0].data.p = unboxed;
2791 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2793 context->current_frame = frame;
2795 if (context->has_resume_state) {
2796 if (frame == context->handler_frame)
2797 SET_RESUME_STATE (context);
2802 if (child_frame.ex) {
2804 * An exception occurred, need to run finally, fault and catch handlers..
2806 frame->ex = child_frame.ex;
2807 if (context->search_for_handler) {
2808 context->search_for_handler = 0;
2809 goto handle_exception;
2811 goto handle_finally;
2814 /* need to handle typedbyref ... */
2819 MINT_IN_CASE(MINT_VCALLVIRT) {
2820 MonoObject *this_arg;
2825 token = * (unsigned short *)(ip + 1);
2827 child_frame.runtime_method = rtm->data_items [token];
2829 child_frame.retval = sp;
2831 /* decrement by the actual number of args */
2832 sp -= child_frame.runtime_method->param_count + 1;
2833 child_frame.stack_args = sp;
2834 this_arg = sp->data.p;
2836 THROW_EX (mono_get_exception_null_reference(), ip - 2);
2837 child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
2839 MonoClass *this_class = this_arg->vtable->klass;
2840 if (this_class->valuetype && child_frame.runtime_method->method->klass->valuetype) {
2841 gpointer *unboxed = mono_object_unbox (this_arg);
2842 sp [0].data.p = unboxed;
2845 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2847 context->current_frame = frame;
2849 if (context->has_resume_state) {
2850 if (frame == context->handler_frame)
2851 SET_RESUME_STATE (context);
2856 if (child_frame.ex) {
2858 * An exception occurred, need to run finally, fault and catch handlers..
2860 frame->ex = child_frame.ex;
2861 if (context->search_for_handler) {
2862 context->search_for_handler = 0;
2863 goto handle_exception;
2865 goto handle_finally;
2869 MINT_IN_CASE(MINT_CALLRUN)
2870 ves_runtime_method (frame, context);
2873 goto handle_exception;
2876 MINT_IN_CASE(MINT_RET)
2878 *frame->retval = *sp;
2879 if (sp > frame->stack)
2880 g_warning ("ret: more values on stack: %d", sp-frame->stack);
2882 MINT_IN_CASE(MINT_RET_VOID)
2883 if (sp > frame->stack)
2884 g_warning ("ret.void: more values on stack: %d %s", sp-frame->stack, mono_method_full_name (frame->runtime_method->method, TRUE));
2886 MINT_IN_CASE(MINT_RET_VT)
2887 i32 = READ32(ip + 1);
2889 memcpy(frame->retval->data.p, sp->data.p, i32);
2890 if (sp > frame->stack)
2891 g_warning ("ret.vt: more values on stack: %d", sp-frame->stack);
2893 MINT_IN_CASE(MINT_BR_S)
2894 ip += (short) *(ip + 1);
2896 MINT_IN_CASE(MINT_BR)
2897 ip += (gint32) READ32(ip + 1);
2899 #define ZEROP_S(datamem, op) \
2901 if (sp->data.datamem op 0) \
2902 ip += * (gint16 *)(ip + 1); \
2906 #define ZEROP(datamem, op) \
2908 if (sp->data.datamem op 0) \
2909 ip += READ32(ip + 1); \
2913 MINT_IN_CASE(MINT_BRFALSE_I4_S)
2916 MINT_IN_CASE(MINT_BRFALSE_I8_S)
2919 MINT_IN_CASE(MINT_BRFALSE_R8_S)
2922 MINT_IN_CASE(MINT_BRFALSE_I4)
2925 MINT_IN_CASE(MINT_BRFALSE_I8)
2928 MINT_IN_CASE(MINT_BRFALSE_R8)
2931 MINT_IN_CASE(MINT_BRTRUE_I4_S)
2934 MINT_IN_CASE(MINT_BRTRUE_I8_S)
2937 MINT_IN_CASE(MINT_BRTRUE_R8_S)
2940 MINT_IN_CASE(MINT_BRTRUE_I4)
2943 MINT_IN_CASE(MINT_BRTRUE_I8)
2946 MINT_IN_CASE(MINT_BRTRUE_R8)
2949 #define CONDBR_S(cond) \
2952 ip += * (gint16 *)(ip + 1); \
2955 #define BRELOP_S(datamem, op) \
2956 CONDBR_S(sp[0].data.datamem op sp[1].data.datamem)
2958 #define CONDBR(cond) \
2961 ip += READ32(ip + 1); \
2965 #define BRELOP(datamem, op) \
2966 CONDBR(sp[0].data.datamem op sp[1].data.datamem)
2968 MINT_IN_CASE(MINT_BEQ_I4_S)
2971 MINT_IN_CASE(MINT_BEQ_I8_S)
2974 MINT_IN_CASE(MINT_BEQ_R8_S)
2975 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2977 MINT_IN_CASE(MINT_BEQ_I4)
2980 MINT_IN_CASE(MINT_BEQ_I8)
2983 MINT_IN_CASE(MINT_BEQ_R8)
2984 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2986 MINT_IN_CASE(MINT_BGE_I4_S)
2989 MINT_IN_CASE(MINT_BGE_I8_S)
2992 MINT_IN_CASE(MINT_BGE_R8_S)
2993 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
2995 MINT_IN_CASE(MINT_BGE_I4)
2998 MINT_IN_CASE(MINT_BGE_I8)
3001 MINT_IN_CASE(MINT_BGE_R8)
3002 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
3004 MINT_IN_CASE(MINT_BGT_I4_S)
3007 MINT_IN_CASE(MINT_BGT_I8_S)
3010 MINT_IN_CASE(MINT_BGT_R8_S)
3011 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
3013 MINT_IN_CASE(MINT_BGT_I4)
3016 MINT_IN_CASE(MINT_BGT_I8)
3019 MINT_IN_CASE(MINT_BGT_R8)
3020 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
3022 MINT_IN_CASE(MINT_BLT_I4_S)
3025 MINT_IN_CASE(MINT_BLT_I8_S)
3028 MINT_IN_CASE(MINT_BLT_R8_S)
3029 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
3031 MINT_IN_CASE(MINT_BLT_I4)
3034 MINT_IN_CASE(MINT_BLT_I8)
3037 MINT_IN_CASE(MINT_BLT_R8)
3038 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
3040 MINT_IN_CASE(MINT_BLE_I4_S)
3043 MINT_IN_CASE(MINT_BLE_I8_S)
3046 MINT_IN_CASE(MINT_BLE_R8_S)
3047 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
3049 MINT_IN_CASE(MINT_BLE_I4)
3052 MINT_IN_CASE(MINT_BLE_I8)
3055 MINT_IN_CASE(MINT_BLE_R8)
3056 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
3058 MINT_IN_CASE(MINT_BNE_UN_I4_S)
3061 MINT_IN_CASE(MINT_BNE_UN_I8_S)
3064 MINT_IN_CASE(MINT_BNE_UN_R8_S)
3065 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
3067 MINT_IN_CASE(MINT_BNE_UN_I4)
3070 MINT_IN_CASE(MINT_BNE_UN_I8)
3073 MINT_IN_CASE(MINT_BNE_UN_R8)
3074 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
3077 #define BRELOP_S_CAST(datamem, op, type) \
3079 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
3080 ip += * (gint16 *)(ip + 1); \
3084 #define BRELOP_CAST(datamem, op, type) \
3086 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
3087 ip += READ32(ip + 1); \
3091 MINT_IN_CASE(MINT_BGE_UN_I4_S)
3092 BRELOP_S_CAST(i, >=, guint32);
3094 MINT_IN_CASE(MINT_BGE_UN_I8_S)
3095 BRELOP_S_CAST(l, >=, guint64);
3097 MINT_IN_CASE(MINT_BGE_UN_R8_S)
3098 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
3100 MINT_IN_CASE(MINT_BGE_UN_I4)
3101 BRELOP_CAST(i, >=, guint32);
3103 MINT_IN_CASE(MINT_BGE_UN_I8)
3104 BRELOP_CAST(l, >=, guint64);
3106 MINT_IN_CASE(MINT_BGE_UN_R8)
3107 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
3109 MINT_IN_CASE(MINT_BGT_UN_I4_S)
3110 BRELOP_S_CAST(i, >, guint32);
3112 MINT_IN_CASE(MINT_BGT_UN_I8_S)
3113 BRELOP_S_CAST(l, >, guint64);
3115 MINT_IN_CASE(MINT_BGT_UN_R8_S)
3116 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
3118 MINT_IN_CASE(MINT_BGT_UN_I4)
3119 BRELOP_CAST(i, >, guint32);
3121 MINT_IN_CASE(MINT_BGT_UN_I8)
3122 BRELOP_CAST(l, >, guint64);
3124 MINT_IN_CASE(MINT_BGT_UN_R8)
3125 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
3127 MINT_IN_CASE(MINT_BLE_UN_I4_S)
3128 BRELOP_S_CAST(i, <=, guint32);
3130 MINT_IN_CASE(MINT_BLE_UN_I8_S)
3131 BRELOP_S_CAST(l, <=, guint64);
3133 MINT_IN_CASE(MINT_BLE_UN_R8_S)
3134 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
3136 MINT_IN_CASE(MINT_BLE_UN_I4)
3137 BRELOP_CAST(i, <=, guint32);
3139 MINT_IN_CASE(MINT_BLE_UN_I8)
3140 BRELOP_CAST(l, <=, guint64);
3142 MINT_IN_CASE(MINT_BLE_UN_R8)
3143 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
3145 MINT_IN_CASE(MINT_BLT_UN_I4_S)
3146 BRELOP_S_CAST(i, <, guint32);
3148 MINT_IN_CASE(MINT_BLT_UN_I8_S)
3149 BRELOP_S_CAST(l, <, guint64);
3151 MINT_IN_CASE(MINT_BLT_UN_R8_S)
3152 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
3154 MINT_IN_CASE(MINT_BLT_UN_I4)
3155 BRELOP_CAST(i, <, guint32);
3157 MINT_IN_CASE(MINT_BLT_UN_I8)
3158 BRELOP_CAST(l, <, guint64);
3160 MINT_IN_CASE(MINT_BLT_UN_R8)
3161 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
3163 MINT_IN_CASE(MINT_SWITCH) {
3165 const unsigned short *st;
3171 if ((guint32)sp->data.i < n) {
3173 ip += 2 * (guint32)sp->data.i;
3174 offset = READ32 (ip);
3181 MINT_IN_CASE(MINT_LDIND_I1)
3183 sp[-1].data.i = *(gint8*)sp[-1].data.p;
3185 MINT_IN_CASE(MINT_LDIND_U1)
3187 sp[-1].data.i = *(guint8*)sp[-1].data.p;
3189 MINT_IN_CASE(MINT_LDIND_I2)
3191 sp[-1].data.i = *(gint16*)sp[-1].data.p;
3193 MINT_IN_CASE(MINT_LDIND_U2)
3195 sp[-1].data.i = *(guint16*)sp[-1].data.p;
3197 MINT_IN_CASE(MINT_LDIND_I4) /* Fall through */
3198 MINT_IN_CASE(MINT_LDIND_U4)
3200 sp[-1].data.i = *(gint32*)sp[-1].data.p;
3202 MINT_IN_CASE(MINT_LDIND_I8)
3204 /* memmove handles unaligned case */
3205 memmove (&sp [-1].data.l, sp [-1].data.p, sizeof (gint64));
3207 MINT_IN_CASE(MINT_LDIND_I) {
3208 guint16 offset = * (guint16 *)(ip + 1);
3209 sp[-1 - offset].data.p = *(gpointer*)sp[-1 - offset].data.p;
3213 MINT_IN_CASE(MINT_LDIND_R4)
3215 sp[-1].data.f = *(gfloat*)sp[-1].data.p;
3217 MINT_IN_CASE(MINT_LDIND_R8)
3219 sp[-1].data.f = *(gdouble*)sp[-1].data.p;
3221 MINT_IN_CASE(MINT_LDIND_REF)
3223 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
3225 MINT_IN_CASE(MINT_STIND_REF)
3228 mono_gc_wbarrier_generic_store (sp->data.p, sp [1].data.p);
3230 MINT_IN_CASE(MINT_STIND_I1)
3233 * (gint8 *) sp->data.p = (gint8)sp[1].data.i;
3235 MINT_IN_CASE(MINT_STIND_I2)
3238 * (gint16 *) sp->data.p = (gint16)sp[1].data.i;
3240 MINT_IN_CASE(MINT_STIND_I4)
3243 * (gint32 *) sp->data.p = sp[1].data.i;
3245 MINT_IN_CASE(MINT_STIND_I)
3248 * (mono_i *) sp->data.p = (mono_i)sp[1].data.p;
3250 MINT_IN_CASE(MINT_STIND_I8)
3253 * (gint64 *) sp->data.p = sp[1].data.l;
3255 MINT_IN_CASE(MINT_STIND_R4)
3258 * (float *) sp->data.p = (gfloat)sp[1].data.f;
3260 MINT_IN_CASE(MINT_STIND_R8)
3263 * (double *) sp->data.p = sp[1].data.f;
3265 MINT_IN_CASE(MINT_MONO_ATOMIC_STORE_I4)
3268 InterlockedWrite ((gint32 *) sp->data.p, sp [1].data.i);
3270 #define BINOP(datamem, op) \
3272 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.datamem; \
3274 MINT_IN_CASE(MINT_ADD_I4)
3277 MINT_IN_CASE(MINT_ADD_I8)
3280 MINT_IN_CASE(MINT_ADD_R8)
3283 MINT_IN_CASE(MINT_ADD1_I4)
3287 MINT_IN_CASE(MINT_SUB_I4)
3290 MINT_IN_CASE(MINT_SUB_I8)
3293 MINT_IN_CASE(MINT_SUB_R8)
3296 MINT_IN_CASE(MINT_SUB1_I4)
3300 MINT_IN_CASE(MINT_MUL_I4)
3303 MINT_IN_CASE(MINT_MUL_I8)
3306 MINT_IN_CASE(MINT_MUL_R8)
3309 MINT_IN_CASE(MINT_DIV_I4)
3310 if (sp [-1].data.i == 0)
3311 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3312 if (sp [-1].data.i == (-1))
3313 THROW_EX (mono_get_exception_overflow (), ip);
3316 MINT_IN_CASE(MINT_DIV_I8)
3317 if (sp [-1].data.l == 0)
3318 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3319 if (sp [-1].data.l == (-1))
3320 THROW_EX (mono_get_exception_overflow (), ip);
3323 MINT_IN_CASE(MINT_DIV_R8)
3327 #define BINOP_CAST(datamem, op, type) \
3329 sp [-1].data.datamem = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
3331 MINT_IN_CASE(MINT_DIV_UN_I4)
3332 if (sp [-1].data.i == 0)
3333 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3334 BINOP_CAST(i, /, guint32);
3336 MINT_IN_CASE(MINT_DIV_UN_I8)
3337 if (sp [-1].data.l == 0)
3338 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3339 BINOP_CAST(l, /, guint64);
3341 MINT_IN_CASE(MINT_REM_I4)
3342 if (sp [-1].data.i == 0)
3343 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3344 if (sp [-1].data.i == (-1))
3345 THROW_EX (mono_get_exception_overflow (), ip);
3348 MINT_IN_CASE(MINT_REM_I8)
3349 if (sp [-1].data.l == 0)
3350 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3351 if (sp [-1].data.l == (-1))
3352 THROW_EX (mono_get_exception_overflow (), ip);
3355 MINT_IN_CASE(MINT_REM_R8)
3356 /* FIXME: what do we actually do here? */
3358 sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
3361 MINT_IN_CASE(MINT_REM_UN_I4)
3362 if (sp [-1].data.i == 0)
3363 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3364 BINOP_CAST(i, %, guint32);
3366 MINT_IN_CASE(MINT_REM_UN_I8)
3367 if (sp [-1].data.l == 0)
3368 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3369 BINOP_CAST(l, %, guint64);
3371 MINT_IN_CASE(MINT_AND_I4)
3374 MINT_IN_CASE(MINT_AND_I8)
3377 MINT_IN_CASE(MINT_OR_I4)
3380 MINT_IN_CASE(MINT_OR_I8)
3383 MINT_IN_CASE(MINT_XOR_I4)
3386 MINT_IN_CASE(MINT_XOR_I8)
3390 #define SHIFTOP(datamem, op) \
3392 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.i; \
3395 MINT_IN_CASE(MINT_SHL_I4)
3398 MINT_IN_CASE(MINT_SHL_I8)
3401 MINT_IN_CASE(MINT_SHR_I4)
3404 MINT_IN_CASE(MINT_SHR_I8)
3407 MINT_IN_CASE(MINT_SHR_UN_I4)
3409 sp [-1].data.i = (guint32)sp [-1].data.i >> sp [0].data.i;
3412 MINT_IN_CASE(MINT_SHR_UN_I8)
3414 sp [-1].data.l = (guint64)sp [-1].data.l >> sp [0].data.i;
3417 MINT_IN_CASE(MINT_NEG_I4)
3418 sp [-1].data.i = - sp [-1].data.i;
3421 MINT_IN_CASE(MINT_NEG_I8)
3422 sp [-1].data.l = - sp [-1].data.l;
3425 MINT_IN_CASE(MINT_NEG_R8)
3426 sp [-1].data.f = - sp [-1].data.f;
3429 MINT_IN_CASE(MINT_NOT_I4)
3430 sp [-1].data.i = ~ sp [-1].data.i;
3433 MINT_IN_CASE(MINT_NOT_I8)
3434 sp [-1].data.l = ~ sp [-1].data.l;
3437 MINT_IN_CASE(MINT_CONV_I1_I4)
3438 sp [-1].data.i = (gint8)sp [-1].data.i;
3441 MINT_IN_CASE(MINT_CONV_I1_I8)
3442 sp [-1].data.i = (gint8)sp [-1].data.l;
3445 MINT_IN_CASE(MINT_CONV_I1_R8)
3446 sp [-1].data.i = (gint8)sp [-1].data.f;
3449 MINT_IN_CASE(MINT_CONV_U1_I4)
3450 sp [-1].data.i = (guint8)sp [-1].data.i;
3453 MINT_IN_CASE(MINT_CONV_U1_I8)
3454 sp [-1].data.i = (guint8)sp [-1].data.l;
3457 MINT_IN_CASE(MINT_CONV_U1_R8)
3458 sp [-1].data.i = (guint8)sp [-1].data.f;
3461 MINT_IN_CASE(MINT_CONV_I2_I4)
3462 sp [-1].data.i = (gint16)sp [-1].data.i;
3465 MINT_IN_CASE(MINT_CONV_I2_I8)
3466 sp [-1].data.i = (gint16)sp [-1].data.l;
3469 MINT_IN_CASE(MINT_CONV_I2_R8)
3470 sp [-1].data.i = (gint16)sp [-1].data.f;
3473 MINT_IN_CASE(MINT_CONV_U2_I4)
3474 sp [-1].data.i = (guint16)sp [-1].data.i;
3477 MINT_IN_CASE(MINT_CONV_U2_I8)
3478 sp [-1].data.i = (guint16)sp [-1].data.l;
3481 MINT_IN_CASE(MINT_CONV_U2_R8)
3482 sp [-1].data.i = (guint16)sp [-1].data.f;
3485 MINT_IN_CASE(MINT_CONV_I4_R8)
3486 sp [-1].data.i = (gint32)sp [-1].data.f;
3489 MINT_IN_CASE(MINT_CONV_U4_I8)
3490 MINT_IN_CASE(MINT_CONV_I4_I8)
3491 sp [-1].data.i = (gint32)sp [-1].data.l;
3494 MINT_IN_CASE(MINT_CONV_I4_I8_SP)
3495 sp [-2].data.i = (gint32)sp [-2].data.l;
3498 MINT_IN_CASE(MINT_CONV_U4_R8)
3499 /* needed on arm64 */
3500 if (isinf (sp [-1].data.f))
3503 sp [-1].data.i = (guint32)sp [-1].data.f;
3506 MINT_IN_CASE(MINT_CONV_I8_I4)
3507 sp [-1].data.l = sp [-1].data.i;
3510 MINT_IN_CASE(MINT_CONV_I8_I4_SP)
3511 sp [-2].data.l = sp [-2].data.i;
3514 MINT_IN_CASE(MINT_CONV_I8_U4)
3515 sp [-1].data.l = (guint32)sp [-1].data.i;
3518 MINT_IN_CASE(MINT_CONV_I8_R8)
3519 sp [-1].data.l = (gint64)sp [-1].data.f;
3522 MINT_IN_CASE(MINT_CONV_R4_I4)
3523 sp [-1].data.f = (float)sp [-1].data.i;
3526 MINT_IN_CASE(MINT_CONV_R4_I8)
3527 sp [-1].data.f = (float)sp [-1].data.l;
3530 MINT_IN_CASE(MINT_CONV_R4_R8)
3531 sp [-1].data.f = (float)sp [-1].data.f;
3534 MINT_IN_CASE(MINT_CONV_R8_I4)
3535 sp [-1].data.f = (double)sp [-1].data.i;
3538 MINT_IN_CASE(MINT_CONV_R8_I8)
3539 sp [-1].data.f = (double)sp [-1].data.l;
3542 MINT_IN_CASE(MINT_CONV_U8_I4)
3543 sp [-1].data.l = sp [-1].data.i & 0xffffffff;
3546 MINT_IN_CASE(MINT_CONV_U8_R8)
3547 sp [-1].data.l = (guint64)sp [-1].data.f;
3550 MINT_IN_CASE(MINT_CPOBJ) {
3551 c = rtm->data_items[* (guint16 *)(ip + 1)];
3552 g_assert (c->valuetype);
3553 /* if this assertion fails, we need to add a write barrier */
3554 g_assert (!MONO_TYPE_IS_REFERENCE (&c->byval_arg));
3555 if (c->byval_arg.type == MONO_TYPE_VALUETYPE)
3556 stackval_from_data (&c->byval_arg, &sp [-2], sp [-1].data.p, FALSE);
3558 stackval_from_data (&c->byval_arg, sp [-2].data.p, sp [-1].data.p, FALSE);
3563 MINT_IN_CASE(MINT_LDOBJ) {
3565 c = rtm->data_items[* (guint16 *)(ip + 1)];
3568 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
3569 int size = mono_class_value_size (c, NULL);
3570 sp [-1].data.p = vt_sp;
3571 vt_sp += (size + 7) & ~7;
3573 stackval_from_data (&c->byval_arg, &sp [-1], p, FALSE);
3576 MINT_IN_CASE(MINT_LDSTR)
3577 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
3581 MINT_IN_CASE(MINT_NEWOBJ) {
3582 MonoClass *newobj_class;
3583 MonoMethodSignature *csig;
3584 stackval valuetype_this;
3590 token = * (guint16 *)(ip + 1);
3593 child_frame.ip = NULL;
3594 child_frame.ex = NULL;
3596 child_frame.runtime_method = rtm->data_items [token];
3597 csig = mono_method_signature (child_frame.runtime_method->method);
3598 newobj_class = child_frame.runtime_method->method->klass;
3599 /*if (profiling_classes) {
3600 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
3602 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
3605 if (newobj_class->parent == mono_defaults.array_class) {
3606 sp -= csig->param_count;
3607 child_frame.stack_args = sp;
3608 o = ves_array_create (&child_frame, context->domain, newobj_class, csig, sp);
3610 THROW_EX (child_frame.ex, ip);
3611 goto array_constructed;
3614 g_assert (csig->hasthis);
3615 if (csig->param_count) {
3616 sp -= csig->param_count;
3617 memmove (sp + 1, sp, csig->param_count * sizeof (stackval));
3619 child_frame.stack_args = sp;
3622 * First arg is the object.
3624 if (newobj_class->valuetype) {
3625 MonoType *t = &newobj_class->byval_arg;
3626 memset (&valuetype_this, 0, sizeof (stackval));
3627 if (!newobj_class->enumtype && (t->type == MONO_TYPE_VALUETYPE || (t->type == MONO_TYPE_GENERICINST && mono_type_generic_inst_is_valuetype (t)))) {
3629 valuetype_this.data.p = vt_sp;
3631 sp->data.p = &valuetype_this;
3634 if (newobj_class != mono_defaults.string_class) {
3635 context->managed_code = 0;
3636 o = mono_object_new_checked (context->domain, newobj_class, &error);
3637 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3638 context->managed_code = 1;
3639 if (*mono_thread_interruption_request_flag ())
3640 mono_thread_interruption_checkpoint ();
3644 child_frame.retval = &retval;
3648 g_assert (csig->call_convention == MONO_CALL_DEFAULT);
3650 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
3652 context->current_frame = frame;
3654 if (context->has_resume_state) {
3655 if (frame == context->handler_frame)
3656 SET_RESUME_STATE (context);
3661 if (child_frame.ex) {
3663 * An exception occurred, need to run finally, fault and catch handlers..
3665 frame->ex = child_frame.ex;
3666 goto handle_finally;
3669 * a constructor returns void, but we need to return the object we created
3672 if (newobj_class->valuetype && !newobj_class->enumtype) {
3673 *sp = valuetype_this;
3674 } else if (newobj_class == mono_defaults.string_class) {
3682 MINT_IN_CASE(MINT_CASTCLASS)
3683 c = rtm->data_items [*(guint16 *)(ip + 1)];
3684 if ((o = sp [-1].data.p)) {
3685 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
3686 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3688 THROW_EX (mono_get_exception_invalid_cast (), ip);
3692 MINT_IN_CASE(MINT_ISINST)
3693 c = rtm->data_items [*(guint16 *)(ip + 1)];
3694 if ((o = sp [-1].data.p)) {
3695 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
3696 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3698 sp [-1].data.p = NULL;
3702 MINT_IN_CASE(MINT_CONV_R_UN_I4)
3703 sp [-1].data.f = (double)(guint32)sp [-1].data.i;
3706 MINT_IN_CASE(MINT_CONV_R_UN_I8)
3707 sp [-1].data.f = (double)(guint64)sp [-1].data.l;
3710 MINT_IN_CASE(MINT_UNBOX)
3711 c = rtm->data_items[*(guint16 *)(ip + 1)];
3715 THROW_EX (mono_get_exception_null_reference (), ip);
3717 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
3718 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3719 if (!(isinst_obj || ((o->vtable->klass->rank == 0) && (o->vtable->klass->element_class == c->element_class))))
3720 THROW_EX (mono_get_exception_invalid_cast (), ip);
3722 sp [-1].data.p = mono_object_unbox (o);
3725 MINT_IN_CASE(MINT_THROW)
3727 frame->ex_handler = NULL;
3729 sp->data.p = mono_get_exception_null_reference ();
3731 THROW_EX ((MonoException *)sp->data.p, ip);
3733 MINT_IN_CASE(MINT_LDFLDA_UNSAFE)
3735 sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
3738 MINT_IN_CASE(MINT_LDFLDA)
3741 THROW_EX (mono_get_exception_null_reference (), ip);
3742 sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
3745 MINT_IN_CASE(MINT_CKNULL)
3748 THROW_EX (mono_get_exception_null_reference (), ip);
3752 #define LDFLD(datamem, fieldtype) \
3753 o = sp [-1].data.p; \
3755 THROW_EX (mono_get_exception_null_reference (), ip); \
3756 sp[-1].data.datamem = * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) ; \
3759 MINT_IN_CASE(MINT_LDFLD_I1) LDFLD(i, gint8); MINT_IN_BREAK;
3760 MINT_IN_CASE(MINT_LDFLD_U1) LDFLD(i, guint8); MINT_IN_BREAK;
3761 MINT_IN_CASE(MINT_LDFLD_I2) LDFLD(i, gint16); MINT_IN_BREAK;
3762 MINT_IN_CASE(MINT_LDFLD_U2) LDFLD(i, guint16); MINT_IN_BREAK;
3763 MINT_IN_CASE(MINT_LDFLD_I4) LDFLD(i, gint32); MINT_IN_BREAK;
3764 MINT_IN_CASE(MINT_LDFLD_I8) LDFLD(l, gint64); MINT_IN_BREAK;
3765 MINT_IN_CASE(MINT_LDFLD_R4) LDFLD(f, float); MINT_IN_BREAK;
3766 MINT_IN_CASE(MINT_LDFLD_R8) LDFLD(f, double); MINT_IN_BREAK;
3767 MINT_IN_CASE(MINT_LDFLD_O) LDFLD(p, gpointer); MINT_IN_BREAK;
3768 MINT_IN_CASE(MINT_LDFLD_P) LDFLD(p, gpointer); MINT_IN_BREAK;
3770 MINT_IN_CASE(MINT_LDFLD_VT)
3773 THROW_EX (mono_get_exception_null_reference (), ip);
3774 i32 = READ32(ip + 2);
3775 sp [-1].data.p = vt_sp;
3776 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
3777 vt_sp += (i32 + 7) & ~7;
3781 MINT_IN_CASE(MINT_LDRMFLD) {
3783 MonoClassField *field;
3788 THROW_EX (mono_get_exception_null_reference (), ip);
3789 field = rtm->data_items[* (guint16 *)(ip + 1)];
3791 if (mono_object_is_transparent_proxy (o)) {
3792 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3794 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
3795 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3797 addr = (char*)o + field->offset;
3800 stackval_from_data (field->type, &sp [-1], addr, FALSE);
3804 MINT_IN_CASE(MINT_LDRMFLD_VT) {
3805 MonoClassField *field;
3811 THROW_EX (mono_get_exception_null_reference (), ip);
3812 field = rtm->data_items[* (guint16 *)(ip + 1)];
3813 i32 = READ32(ip + 2);
3815 if (mono_object_is_transparent_proxy (o)) {
3816 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3817 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
3818 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3820 addr = (char*)o + field->offset;
3823 sp [-1].data.p = vt_sp;
3824 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
3825 vt_sp += (i32 + 7) & ~7;
3826 memcpy(sp [-1].data.p, addr, i32);
3830 #define STFLD(datamem, fieldtype) \
3831 o = sp [-2].data.p; \
3833 THROW_EX (mono_get_exception_null_reference (), ip); \
3835 * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) = sp[1].data.datamem; \
3838 MINT_IN_CASE(MINT_STFLD_I1) STFLD(i, gint8); MINT_IN_BREAK;
3839 MINT_IN_CASE(MINT_STFLD_U1) STFLD(i, guint8); MINT_IN_BREAK;
3840 MINT_IN_CASE(MINT_STFLD_I2) STFLD(i, gint16); MINT_IN_BREAK;
3841 MINT_IN_CASE(MINT_STFLD_U2) STFLD(i, guint16); MINT_IN_BREAK;
3842 MINT_IN_CASE(MINT_STFLD_I4) STFLD(i, gint32); MINT_IN_BREAK;
3843 MINT_IN_CASE(MINT_STFLD_I8) STFLD(l, gint64); MINT_IN_BREAK;
3844 MINT_IN_CASE(MINT_STFLD_R4) STFLD(f, float); MINT_IN_BREAK;
3845 MINT_IN_CASE(MINT_STFLD_R8) STFLD(f, double); MINT_IN_BREAK;
3846 MINT_IN_CASE(MINT_STFLD_P) STFLD(p, gpointer); MINT_IN_BREAK;
3847 MINT_IN_CASE(MINT_STFLD_O)
3850 THROW_EX (mono_get_exception_null_reference (), ip);
3852 mono_gc_wbarrier_set_field (o, (char *) o + * (guint16 *)(ip + 1), sp [1].data.p);
3856 MINT_IN_CASE(MINT_STFLD_VT)
3859 THROW_EX (mono_get_exception_null_reference (), ip);
3860 i32 = READ32(ip + 2);
3862 memcpy((char *)o + * (guint16 *)(ip + 1), sp [1].data.p, i32);
3863 vt_sp -= (i32 + 7) & ~7;
3867 MINT_IN_CASE(MINT_STRMFLD) {
3868 MonoClassField *field;
3872 THROW_EX (mono_get_exception_null_reference (), ip);
3874 field = rtm->data_items[* (guint16 *)(ip + 1)];
3877 if (mono_object_is_transparent_proxy (o)) {
3878 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3879 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
3880 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3882 stackval_to_data (field->type, &sp [-1], (char*)o + field->offset, FALSE);
3887 MINT_IN_CASE(MINT_STRMFLD_VT) {
3888 MonoClassField *field;
3892 THROW_EX (mono_get_exception_null_reference (), ip);
3893 field = rtm->data_items[* (guint16 *)(ip + 1)];
3894 i32 = READ32(ip + 2);
3897 if (mono_object_is_transparent_proxy (o)) {
3898 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3899 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
3900 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3902 memcpy((char*)o + field->offset, sp [-1].data.p, i32);
3905 vt_sp -= (i32 + 7) & ~7;
3908 MINT_IN_CASE(MINT_LDSFLDA) {
3909 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3910 sp->data.p = mono_class_static_field_address (context->domain, field);
3915 MINT_IN_CASE(MINT_LDSFLD) {
3916 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3917 gpointer addr = mono_class_static_field_address (context->domain, field);
3918 stackval_from_data (field->type, sp, addr, FALSE);
3923 MINT_IN_CASE(MINT_LDSFLD_VT) {
3924 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3925 gpointer addr = mono_class_static_field_address (context->domain, field);
3926 int size = READ32 (ip + 2);
3930 vt_sp += (size + 7) & ~7;
3931 stackval_from_data (field->type, sp, addr, FALSE);
3935 MINT_IN_CASE(MINT_STSFLD) {
3936 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3937 gpointer addr = mono_class_static_field_address (context->domain, field);
3940 stackval_to_data (field->type, sp, addr, FALSE);
3943 MINT_IN_CASE(MINT_STSFLD_VT) {
3944 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3945 gpointer addr = mono_class_static_field_address (context->domain, field);
3946 int size = READ32 (ip + 2);
3950 stackval_to_data (field->type, sp, addr, FALSE);
3951 vt_sp -= (size + 7) & ~7;
3954 MINT_IN_CASE(MINT_STOBJ_VT) {
3956 c = rtm->data_items[* (guint16 *)(ip + 1)];
3958 size = mono_class_value_size (c, NULL);
3959 memcpy(sp [-2].data.p, sp [-1].data.p, size);
3960 vt_sp -= (size + 7) & ~7;
3964 MINT_IN_CASE(MINT_STOBJ) {
3965 c = rtm->data_items[* (guint16 *)(ip + 1)];
3968 g_assert (!c->byval_arg.byref);
3969 if (MONO_TYPE_IS_REFERENCE (&c->byval_arg))
3970 mono_gc_wbarrier_generic_store (sp [-2].data.p, sp [-1].data.p);
3972 stackval_from_data (&c->byval_arg, sp [-2].data.p, (char *) &sp [-1].data.p, FALSE);
3976 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_R8)
3977 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3978 THROW_EX (mono_get_exception_overflow (), ip);
3979 sp [-1].data.i = (guint32)sp [-1].data.f;
3982 MINT_IN_CASE(MINT_CONV_OVF_U8_I4)
3983 if (sp [-1].data.i < 0)
3984 THROW_EX (mono_get_exception_overflow (), ip);
3985 sp [-1].data.l = sp [-1].data.i;
3988 MINT_IN_CASE(MINT_CONV_OVF_U8_I8)
3989 if (sp [-1].data.l < 0)
3990 THROW_EX (mono_get_exception_overflow (), ip);
3993 MINT_IN_CASE(MINT_CONV_OVF_I8_U8)
3994 if ((guint64) sp [-1].data.l > MYGINT64_MAX)
3995 THROW_EX (mono_get_exception_overflow (), ip);
3998 MINT_IN_CASE(MINT_CONV_OVF_U8_R8)
3999 MINT_IN_CASE(MINT_CONV_OVF_I8_UN_R8)
4000 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGINT64_MAX)
4001 THROW_EX (mono_get_exception_overflow (), ip);
4002 sp [-1].data.l = (guint64)sp [-1].data.f;
4005 MINT_IN_CASE(MINT_CONV_OVF_I8_R8)
4006 if (sp [-1].data.f < MYGINT64_MIN || sp [-1].data.f > MYGINT64_MAX)
4007 THROW_EX (mono_get_exception_overflow (), ip);
4008 sp [-1].data.l = (gint64)sp [-1].data.f;
4011 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_I8)
4012 if ((mono_u)sp [-1].data.l > MYGUINT32_MAX)
4013 THROW_EX (mono_get_exception_overflow (), ip);
4014 sp [-1].data.i = (mono_u)sp [-1].data.l;
4017 MINT_IN_CASE(MINT_BOX) {
4018 c = rtm->data_items [* (guint16 *)(ip + 1)];
4019 guint16 offset = * (guint16 *)(ip + 2);
4021 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
4022 int size = mono_class_value_size (c, NULL);
4023 sp [-1 - offset].data.p = mono_value_box_checked (context->domain, c, sp [-1 - offset].data.p, &error);
4024 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4025 size = (size + 7) & ~7;
4028 stackval_to_data (&c->byval_arg, &sp [-1 - offset], (char *) &sp [-1 - offset], FALSE);
4029 sp [-1 - offset].data.p = mono_value_box_checked (context->domain, c, &sp [-1 - offset], &error);
4030 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4035 MINT_IN_CASE(MINT_NEWARR)
4036 sp [-1].data.p = (MonoObject*) mono_array_new_checked (context->domain, rtm->data_items[*(guint16 *)(ip + 1)], sp [-1].data.i, &error);
4037 if (!mono_error_ok (&error)) {
4038 THROW_EX (mono_error_convert_to_exception (&error), ip);
4040 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4042 /*if (profiling_classes) {
4043 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
4045 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
4049 MINT_IN_CASE(MINT_LDLEN)
4052 THROW_EX (mono_get_exception_null_reference (), ip);
4053 sp [-1].data.nati = mono_array_length ((MonoArray *)o);
4056 MINT_IN_CASE(MINT_GETCHR) {
4060 THROW_EX (mono_get_exception_null_reference (), ip);
4061 i32 = sp [-1].data.i;
4062 if (i32 < 0 || i32 >= mono_string_length (s))
4063 THROW_EX (mono_get_exception_index_out_of_range (), ip);
4065 sp [-1].data.i = mono_string_chars(s)[i32];
4069 MINT_IN_CASE(MINT_STRLEN)
4073 THROW_EX (mono_get_exception_null_reference (), ip);
4074 sp [-1].data.i = mono_string_length ((MonoString*) o);
4076 MINT_IN_CASE(MINT_ARRAY_RANK)
4079 THROW_EX (mono_get_exception_null_reference (), ip);
4080 sp [-1].data.i = mono_object_class (sp [-1].data.p)->rank;
4083 MINT_IN_CASE(MINT_LDELEMA)
4084 MINT_IN_CASE(MINT_LDELEMA_TC) {
4085 gboolean needs_typecheck = *ip == MINT_LDELEMA_TC;
4087 MonoClass *klass = rtm->data_items [*(guint16 *) (ip + 1)];
4088 guint16 numargs = *(guint16 *) (ip + 2);
4093 sp->data.p = ves_array_element_address (frame, klass, (MonoArray *) o, &sp [1], needs_typecheck);
4095 THROW_EX (frame->ex, ip);
4100 MINT_IN_CASE(MINT_LDELEM_I1) /* fall through */
4101 MINT_IN_CASE(MINT_LDELEM_U1) /* fall through */
4102 MINT_IN_CASE(MINT_LDELEM_I2) /* fall through */
4103 MINT_IN_CASE(MINT_LDELEM_U2) /* fall through */
4104 MINT_IN_CASE(MINT_LDELEM_I4) /* fall through */
4105 MINT_IN_CASE(MINT_LDELEM_U4) /* fall through */
4106 MINT_IN_CASE(MINT_LDELEM_I8) /* fall through */
4107 MINT_IN_CASE(MINT_LDELEM_I) /* fall through */
4108 MINT_IN_CASE(MINT_LDELEM_R4) /* fall through */
4109 MINT_IN_CASE(MINT_LDELEM_R8) /* fall through */
4110 MINT_IN_CASE(MINT_LDELEM_REF) /* fall through */
4111 MINT_IN_CASE(MINT_LDELEM_VT) {
4119 THROW_EX (mono_get_exception_null_reference (), ip);
4121 aindex = sp [1].data.i;
4122 if (aindex >= mono_array_length (o))
4123 THROW_EX (mono_get_exception_index_out_of_range (), ip);
4126 * FIXME: throw mono_get_exception_array_type_mismatch () if needed
4129 case MINT_LDELEM_I1:
4130 sp [0].data.i = mono_array_get (o, gint8, aindex);
4132 case MINT_LDELEM_U1:
4133 sp [0].data.i = mono_array_get (o, guint8, aindex);
4135 case MINT_LDELEM_I2:
4136 sp [0].data.i = mono_array_get (o, gint16, aindex);
4138 case MINT_LDELEM_U2:
4139 sp [0].data.i = mono_array_get (o, guint16, aindex);
4142 sp [0].data.nati = mono_array_get (o, mono_i, aindex);
4144 case MINT_LDELEM_I4:
4145 sp [0].data.i = mono_array_get (o, gint32, aindex);
4147 case MINT_LDELEM_U4:
4148 sp [0].data.i = mono_array_get (o, guint32, aindex);
4150 case MINT_LDELEM_I8:
4151 sp [0].data.l = mono_array_get (o, guint64, aindex);
4153 case MINT_LDELEM_R4:
4154 sp [0].data.f = mono_array_get (o, float, aindex);
4156 case MINT_LDELEM_R8:
4157 sp [0].data.f = mono_array_get (o, double, aindex);
4159 case MINT_LDELEM_REF:
4160 sp [0].data.p = mono_array_get (o, gpointer, aindex);
4162 case MINT_LDELEM_VT: {
4163 MonoClass *klass_vt = rtm->data_items [*(guint16 *) (ip + 1)];
4164 i32 = READ32 (ip + 2);
4165 char *src_addr = mono_array_addr_with_size ((MonoArray *) o, i32, aindex);
4166 sp [0].data.vt = vt_sp;
4167 stackval_from_data (&klass_vt->byval_arg, sp, src_addr, FALSE);
4168 vt_sp += (i32 + 7) & ~7;
4180 MINT_IN_CASE(MINT_STELEM_I) /* fall through */
4181 MINT_IN_CASE(MINT_STELEM_I1) /* fall through */
4182 MINT_IN_CASE(MINT_STELEM_U1) /* fall through */
4183 MINT_IN_CASE(MINT_STELEM_I2) /* fall through */
4184 MINT_IN_CASE(MINT_STELEM_U2) /* fall through */
4185 MINT_IN_CASE(MINT_STELEM_I4) /* fall through */
4186 MINT_IN_CASE(MINT_STELEM_I8) /* fall through */
4187 MINT_IN_CASE(MINT_STELEM_R4) /* fall through */
4188 MINT_IN_CASE(MINT_STELEM_R8) /* fall through */
4189 MINT_IN_CASE(MINT_STELEM_REF) /* fall through */
4190 MINT_IN_CASE(MINT_STELEM_VT) {
4197 THROW_EX (mono_get_exception_null_reference (), ip);
4199 aindex = sp [1].data.i;
4200 if (aindex >= mono_array_length ((MonoArray *)o))
4201 THROW_EX (mono_get_exception_index_out_of_range (), ip);
4205 mono_array_set ((MonoArray *)o, mono_i, aindex, sp [2].data.nati);
4207 case MINT_STELEM_I1:
4208 mono_array_set ((MonoArray *)o, gint8, aindex, sp [2].data.i);
4210 case MINT_STELEM_U1:
4211 mono_array_set ((MonoArray *) o, guint8, aindex, sp [2].data.i);
4213 case MINT_STELEM_I2:
4214 mono_array_set ((MonoArray *)o, gint16, aindex, sp [2].data.i);
4216 case MINT_STELEM_U2:
4217 mono_array_set ((MonoArray *)o, guint16, aindex, sp [2].data.i);
4219 case MINT_STELEM_I4:
4220 mono_array_set ((MonoArray *)o, gint32, aindex, sp [2].data.i);
4222 case MINT_STELEM_I8:
4223 mono_array_set ((MonoArray *)o, gint64, aindex, sp [2].data.l);
4225 case MINT_STELEM_R4:
4226 mono_array_set ((MonoArray *)o, float, aindex, sp [2].data.f);
4228 case MINT_STELEM_R8:
4229 mono_array_set ((MonoArray *)o, double, aindex, sp [2].data.f);
4231 case MINT_STELEM_REF: {
4232 MonoObject *isinst_obj = mono_object_isinst_checked (sp [2].data.p, mono_object_class (o)->element_class, &error);
4233 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4234 if (sp [2].data.p && !isinst_obj)
4235 THROW_EX (mono_get_exception_array_type_mismatch (), ip);
4236 mono_array_setref ((MonoArray *) o, aindex, sp [2].data.p);
4239 case MINT_STELEM_VT: {
4240 MonoClass *klass_vt = rtm->data_items [*(guint16 *) (ip + 1)];
4241 i32 = READ32 (ip + 2);
4242 char *dst_addr = mono_array_addr_with_size ((MonoArray *) o, i32, aindex);
4244 stackval_to_data (&klass_vt->byval_arg, &sp [2], dst_addr, FALSE);
4245 vt_sp -= (i32 + 7) & ~7;
4256 MINT_IN_CASE(MINT_CONV_OVF_I4_U4)
4257 if (sp [-1].data.i < 0)
4258 THROW_EX (mono_get_exception_overflow (), ip);
4261 MINT_IN_CASE(MINT_CONV_OVF_I4_I8)
4262 if (sp [-1].data.l < MYGINT32_MIN || sp [-1].data.l > MYGINT32_MAX)
4263 THROW_EX (mono_get_exception_overflow (), ip);
4264 sp [-1].data.i = (gint32) sp [-1].data.l;
4267 MINT_IN_CASE(MINT_CONV_OVF_I4_U8)
4268 if (sp [-1].data.l < 0 || sp [-1].data.l > MYGINT32_MAX)
4269 THROW_EX (mono_get_exception_overflow (), ip);
4270 sp [-1].data.i = (gint32) sp [-1].data.l;
4273 MINT_IN_CASE(MINT_CONV_OVF_I4_R8)
4274 if (sp [-1].data.f < MYGINT32_MIN || sp [-1].data.f > MYGINT32_MAX)
4275 THROW_EX (mono_get_exception_overflow (), ip);
4276 sp [-1].data.i = (gint32) sp [-1].data.f;
4279 MINT_IN_CASE(MINT_CONV_OVF_U4_I4)
4280 if (sp [-1].data.i < 0)
4281 THROW_EX (mono_get_exception_overflow (), ip);
4284 MINT_IN_CASE(MINT_CONV_OVF_U4_I8)
4285 if (sp [-1].data.l < 0 || sp [-1].data.l > MYGUINT32_MAX)
4286 THROW_EX (mono_get_exception_overflow (), ip);
4287 sp [-1].data.i = (guint32) sp [-1].data.l;
4290 MINT_IN_CASE(MINT_CONV_OVF_U4_R8)
4291 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
4292 THROW_EX (mono_get_exception_overflow (), ip);
4293 sp [-1].data.i = (guint32) sp [-1].data.f;
4296 MINT_IN_CASE(MINT_CONV_OVF_I2_I4)
4297 if (sp [-1].data.i < -32768 || sp [-1].data.i > 32767)
4298 THROW_EX (mono_get_exception_overflow (), ip);
4301 MINT_IN_CASE(MINT_CONV_OVF_I2_I8)
4302 if (sp [-1].data.l < -32768 || sp [-1].data.l > 32767)
4303 THROW_EX (mono_get_exception_overflow (), ip);
4304 sp [-1].data.i = (gint16) sp [-1].data.l;
4307 MINT_IN_CASE(MINT_CONV_OVF_I2_R8)
4308 if (sp [-1].data.f < -32768 || sp [-1].data.f > 32767)
4309 THROW_EX (mono_get_exception_overflow (), ip);
4310 sp [-1].data.i = (gint16) sp [-1].data.f;
4313 MINT_IN_CASE(MINT_CONV_OVF_U2_I4)
4314 if (sp [-1].data.i < 0 || sp [-1].data.i > 65535)
4315 THROW_EX (mono_get_exception_overflow (), ip);
4318 MINT_IN_CASE(MINT_CONV_OVF_U2_I8)
4319 if (sp [-1].data.l < 0 || sp [-1].data.l > 65535)
4320 THROW_EX (mono_get_exception_overflow (), ip);
4321 sp [-1].data.i = (guint16) sp [-1].data.l;
4324 MINT_IN_CASE(MINT_CONV_OVF_U2_R8)
4325 if (sp [-1].data.f < 0 || sp [-1].data.f > 65535)
4326 THROW_EX (mono_get_exception_overflow (), ip);
4327 sp [-1].data.i = (guint16) sp [-1].data.f;
4330 MINT_IN_CASE(MINT_CONV_OVF_I1_I4)
4331 if (sp [-1].data.i < -128 || sp [-1].data.i > 127)
4332 THROW_EX (mono_get_exception_overflow (), ip);
4335 MINT_IN_CASE(MINT_CONV_OVF_I1_I8)
4336 if (sp [-1].data.l < -128 || sp [-1].data.l > 127)
4337 THROW_EX (mono_get_exception_overflow (), ip);
4338 sp [-1].data.i = (gint8) sp [-1].data.l;
4341 MINT_IN_CASE(MINT_CONV_OVF_I1_R8)
4342 if (sp [-1].data.f < -128 || sp [-1].data.f > 127)
4343 THROW_EX (mono_get_exception_overflow (), ip);
4344 sp [-1].data.i = (gint8) sp [-1].data.f;
4347 MINT_IN_CASE(MINT_CONV_OVF_U1_I4)
4348 if (sp [-1].data.i < 0 || sp [-1].data.i > 255)
4349 THROW_EX (mono_get_exception_overflow (), ip);
4352 MINT_IN_CASE(MINT_CONV_OVF_U1_I8)
4353 if (sp [-1].data.l < 0 || sp [-1].data.l > 255)
4354 THROW_EX (mono_get_exception_overflow (), ip);
4355 sp [-1].data.i = (guint8) sp [-1].data.l;
4358 MINT_IN_CASE(MINT_CONV_OVF_U1_R8)
4359 if (sp [-1].data.f < 0 || sp [-1].data.f > 255)
4360 THROW_EX (mono_get_exception_overflow (), ip);
4361 sp [-1].data.i = (guint8) sp [-1].data.f;
4365 MINT_IN_CASE(MINT_LDELEM)
4366 MINT_IN_CASE(MINT_STELEM)
4367 MINT_IN_CASE(MINT_UNBOX_ANY)
4369 MINT_IN_CASE(MINT_CKFINITE)
4370 if (!isfinite(sp [-1].data.f))
4371 THROW_EX (mono_get_exception_arithmetic (), ip);
4374 MINT_IN_CASE(MINT_MKREFANY) {
4375 c = rtm->data_items [*(guint16 *)(ip + 1)];
4377 /* The value address is on the stack */
4378 gpointer addr = sp [-1].data.p;
4379 /* Push the typedref value on the stack */
4380 sp [-1].data.p = vt_sp;
4381 vt_sp += sizeof (MonoTypedRef);
4383 MonoTypedRef *tref = sp [-1].data.p;
4385 tref->type = &c->byval_arg;
4391 MINT_IN_CASE(MINT_REFANYTYPE) {
4392 MonoTypedRef *tref = sp [-1].data.p;
4393 MonoType *type = tref->type;
4395 vt_sp -= sizeof (MonoTypedRef);
4396 sp [-1].data.p = vt_sp;
4398 *(gpointer*)sp [-1].data.p = type;
4402 MINT_IN_CASE(MINT_REFANYVAL) {
4403 MonoTypedRef *tref = sp [-1].data.p;
4404 gpointer addr = tref->value;
4406 vt_sp -= sizeof (MonoTypedRef);
4408 sp [-1].data.p = addr;
4412 MINT_IN_CASE(MINT_LDTOKEN)
4415 * (gpointer *)sp->data.p = rtm->data_items[*(guint16 *)(ip + 1)];
4419 MINT_IN_CASE(MINT_ADD_OVF_I4)
4420 if (CHECK_ADD_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
4421 THROW_EX (mono_get_exception_overflow (), ip);
4424 MINT_IN_CASE(MINT_ADD_OVF_I8)
4425 if (CHECK_ADD_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
4426 THROW_EX (mono_get_exception_overflow (), ip);
4429 MINT_IN_CASE(MINT_ADD_OVF_UN_I4)
4430 if (CHECK_ADD_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
4431 THROW_EX (mono_get_exception_overflow (), ip);
4432 BINOP_CAST(i, +, guint32);
4434 MINT_IN_CASE(MINT_ADD_OVF_UN_I8)
4435 if (CHECK_ADD_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
4436 THROW_EX (mono_get_exception_overflow (), ip);
4437 BINOP_CAST(l, +, guint64);
4439 MINT_IN_CASE(MINT_MUL_OVF_I4)
4440 if (CHECK_MUL_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
4441 THROW_EX (mono_get_exception_overflow (), ip);
4444 MINT_IN_CASE(MINT_MUL_OVF_I8)
4445 if (CHECK_MUL_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
4446 THROW_EX (mono_get_exception_overflow (), ip);
4449 MINT_IN_CASE(MINT_MUL_OVF_UN_I4)
4450 if (CHECK_MUL_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
4451 THROW_EX (mono_get_exception_overflow (), ip);
4452 BINOP_CAST(i, *, guint32);
4454 MINT_IN_CASE(MINT_MUL_OVF_UN_I8)
4455 if (CHECK_MUL_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
4456 THROW_EX (mono_get_exception_overflow (), ip);
4457 BINOP_CAST(l, *, guint64);
4459 MINT_IN_CASE(MINT_SUB_OVF_I4)
4460 if (CHECK_SUB_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
4461 THROW_EX (mono_get_exception_overflow (), ip);
4464 MINT_IN_CASE(MINT_SUB_OVF_I8)
4465 if (CHECK_SUB_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
4466 THROW_EX (mono_get_exception_overflow (), ip);
4469 MINT_IN_CASE(MINT_SUB_OVF_UN_I4)
4470 if (CHECK_SUB_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
4471 THROW_EX (mono_get_exception_overflow (), ip);
4472 BINOP_CAST(i, -, guint32);
4474 MINT_IN_CASE(MINT_SUB_OVF_UN_I8)
4475 if (CHECK_SUB_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
4476 THROW_EX (mono_get_exception_overflow (), ip);
4477 BINOP_CAST(l, -, guint64);
4479 MINT_IN_CASE(MINT_ENDFINALLY)
4481 int clause_index = *ip;
4482 if (clause_index == exit_at_finally)
4484 while (sp > frame->stack) {
4488 ip = finally_ips->data;
4489 finally_ips = g_slist_remove (finally_ips, ip);
4496 MINT_IN_CASE(MINT_LEAVE) /* Fall through */
4497 MINT_IN_CASE(MINT_LEAVE_S)
4498 while (sp > frame->stack) {
4502 if (*ip == MINT_LEAVE_S) {
4503 ip += (short) *(ip + 1);
4505 ip += (gint32) READ32 (ip + 1);
4508 if (frame->ex_handler != NULL && MONO_OFFSET_IN_HANDLER(frame->ex_handler, frame->ip - rtm->code)) {
4509 frame->ex_handler = NULL;
4512 goto handle_finally;
4514 MINT_IN_CASE(MINT_ICALL_V_V)
4515 MINT_IN_CASE(MINT_ICALL_V_P)
4516 MINT_IN_CASE(MINT_ICALL_P_V)
4517 MINT_IN_CASE(MINT_ICALL_P_P)
4518 MINT_IN_CASE(MINT_ICALL_PP_V)
4519 MINT_IN_CASE(MINT_ICALL_PI_V)
4520 MINT_IN_CASE(MINT_ICALL_PP_P)
4521 MINT_IN_CASE(MINT_ICALL_PI_P)
4522 MINT_IN_CASE(MINT_ICALL_PPP_V)
4523 MINT_IN_CASE(MINT_ICALL_PPI_V)
4524 sp = do_icall (context, *ip, sp, rtm->data_items [*(guint16 *)(ip + 1)]);
4525 if (*mono_thread_interruption_request_flag ()) {
4526 MonoException *exc = mono_thread_interruption_checkpoint ();
4529 context->search_for_handler = 1;
4532 if (frame->ex != NULL)
4533 goto handle_exception;
4536 MINT_IN_CASE(MINT_MONO_LDPTR)
4537 sp->data.p = rtm->data_items [*(guint16 *)(ip + 1)];
4541 MINT_IN_CASE(MINT_MONO_NEWOBJ)
4542 sp->data.p = mono_object_new_checked (context->domain, rtm->data_items [*(guint16 *)(ip + 1)], &error);
4543 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4547 MINT_IN_CASE(MINT_MONO_FREE)
4550 g_error ("that doesn't seem right");
4551 g_free (sp->data.p);
4553 MINT_IN_CASE(MINT_MONO_RETOBJ)
4556 stackval_from_data (mono_method_signature (frame->runtime_method->method)->ret, frame->retval, sp->data.p,
4557 mono_method_signature (frame->runtime_method->method)->pinvoke);
4558 if (sp > frame->stack)
4559 g_warning ("retobj: more values on stack: %d", sp-frame->stack);
4561 MINT_IN_CASE(MINT_MONO_TLS) {
4562 MonoTlsKey key = *(gint32 *)(ip + 1);
4563 sp->data.p = ((gpointer (*)()) mono_tls_get_tls_getter (key, FALSE)) ();
4568 MINT_IN_CASE(MINT_MONO_JIT_ATTACH) {
4571 context->original_domain = NULL;
4572 MonoDomain *tls_domain = (MonoDomain *) ((gpointer (*)()) mono_tls_get_tls_getter (TLS_KEY_DOMAIN, FALSE)) ();
4573 gpointer tls_jit = ((gpointer (*)()) mono_tls_get_tls_getter (TLS_KEY_DOMAIN, FALSE)) ();
4575 if (tls_domain != context->domain || !tls_jit)
4576 context->original_domain = mono_jit_thread_attach (context->domain);
4579 MINT_IN_CASE(MINT_MONO_JIT_DETACH)
4581 mono_jit_set_domain (context->original_domain);
4583 MINT_IN_CASE(MINT_SDB_INTR_LOC)
4584 if (G_UNLIKELY (ss_enabled)) {
4586 static void (*ss_tramp) (void);
4589 void *tramp = mini_get_single_step_trampoline ();
4590 mono_memory_barrier ();
4595 * Make this point to the MINT_SDB_SEQ_POINT instruction which follows this since
4596 * the address of that instruction is stored as the seq point address.
4600 interp_push_lmf (&ext, frame);
4602 * Use the same trampoline as the JIT. This ensures that
4603 * the debugger has the context for the last interpreter
4607 interp_pop_lmf (&ext);
4609 if (context->has_resume_state) {
4610 if (frame == context->handler_frame)
4611 SET_RESUME_STATE (context);
4618 MINT_IN_CASE(MINT_SDB_SEQ_POINT)
4619 /* Just a placeholder for a breakpoint */
4622 MINT_IN_CASE(MINT_SDB_BREAKPOINT) {
4625 static void (*bp_tramp) (void);
4627 void *tramp = mini_get_breakpoint_trampoline ();
4628 mono_memory_barrier ();
4634 interp_push_lmf (&ext, frame);
4635 /* Use the same trampoline as the JIT */
4637 interp_pop_lmf (&ext);
4639 if (context->has_resume_state) {
4640 if (frame == context->handler_frame)
4641 SET_RESUME_STATE (context);
4650 #define RELOP(datamem, op) \
4652 sp [-1].data.i = sp [-1].data.datamem op sp [0].data.datamem; \
4654 MINT_IN_CASE(MINT_CEQ_I4)
4657 MINT_IN_CASE(MINT_CEQ0_I4)
4658 sp [-1].data.i = (sp [-1].data.i == 0);
4661 MINT_IN_CASE(MINT_CEQ_I8)
4664 MINT_IN_CASE(MINT_CEQ_R8)
4666 if (isunordered (sp [-1].data.f, sp [0].data.f))
4669 sp [-1].data.i = sp [-1].data.f == sp [0].data.f;
4672 MINT_IN_CASE(MINT_CGT_I4)
4675 MINT_IN_CASE(MINT_CGT_I8)
4678 MINT_IN_CASE(MINT_CGT_R8)
4680 if (isunordered (sp [-1].data.f, sp [0].data.f))
4683 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
4687 #define RELOP_CAST(datamem, op, type) \
4689 sp [-1].data.i = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
4692 MINT_IN_CASE(MINT_CGT_UN_I4)
4693 RELOP_CAST(i, >, guint32);
4695 MINT_IN_CASE(MINT_CGT_UN_I8)
4696 RELOP_CAST(l, >, guint64);
4698 MINT_IN_CASE(MINT_CGT_UN_R8)
4700 if (isunordered (sp [-1].data.f, sp [0].data.f))
4703 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
4706 MINT_IN_CASE(MINT_CLT_I4)
4709 MINT_IN_CASE(MINT_CLT_I8)
4712 MINT_IN_CASE(MINT_CLT_R8)
4714 if (isunordered (sp [-1].data.f, sp [0].data.f))
4717 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
4720 MINT_IN_CASE(MINT_CLT_UN_I4)
4721 RELOP_CAST(i, <, guint32);
4723 MINT_IN_CASE(MINT_CLT_UN_I8)
4724 RELOP_CAST(l, <, guint64);
4726 MINT_IN_CASE(MINT_CLT_UN_R8)
4728 if (isunordered (sp [-1].data.f, sp [0].data.f))
4731 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
4734 MINT_IN_CASE(MINT_LDFTN) {
4735 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
4740 MINT_IN_CASE(MINT_LDVIRTFTN) {
4741 RuntimeMethod *m = rtm->data_items [* (guint16 *)(ip + 1)];
4745 THROW_EX (mono_get_exception_null_reference (), ip - 2);
4747 sp->data.p = get_virtual_method (context->domain, m, sp->data.p);
4752 #define LDARG(datamem, argtype) \
4753 sp->data.datamem = * (argtype *)(frame->args + * (guint16 *)(ip + 1)); \
4757 MINT_IN_CASE(MINT_LDARG_I1) LDARG(i, gint8); MINT_IN_BREAK;
4758 MINT_IN_CASE(MINT_LDARG_U1) LDARG(i, guint8); MINT_IN_BREAK;
4759 MINT_IN_CASE(MINT_LDARG_I2) LDARG(i, gint16); MINT_IN_BREAK;
4760 MINT_IN_CASE(MINT_LDARG_U2) LDARG(i, guint16); MINT_IN_BREAK;
4761 MINT_IN_CASE(MINT_LDARG_I4) LDARG(i, gint32); MINT_IN_BREAK;
4762 MINT_IN_CASE(MINT_LDARG_I8) LDARG(l, gint64); MINT_IN_BREAK;
4763 MINT_IN_CASE(MINT_LDARG_R4) LDARG(f, float); MINT_IN_BREAK;
4764 MINT_IN_CASE(MINT_LDARG_R8) LDARG(f, double); MINT_IN_BREAK;
4765 MINT_IN_CASE(MINT_LDARG_O) LDARG(p, gpointer); MINT_IN_BREAK;
4766 MINT_IN_CASE(MINT_LDARG_P) LDARG(p, gpointer); MINT_IN_BREAK;
4768 MINT_IN_CASE(MINT_LDARG_VT)
4770 i32 = READ32(ip + 2);
4771 memcpy(sp->data.p, frame->args + * (guint16 *)(ip + 1), i32);
4772 vt_sp += (i32 + 7) & ~7;
4777 #define STARG(datamem, argtype) \
4779 * (argtype *)(frame->args + * (guint16 *)(ip + 1)) = sp->data.datamem; \
4782 MINT_IN_CASE(MINT_STARG_I1) STARG(i, gint8); MINT_IN_BREAK;
4783 MINT_IN_CASE(MINT_STARG_U1) STARG(i, guint8); MINT_IN_BREAK;
4784 MINT_IN_CASE(MINT_STARG_I2) STARG(i, gint16); MINT_IN_BREAK;
4785 MINT_IN_CASE(MINT_STARG_U2) STARG(i, guint16); MINT_IN_BREAK;
4786 MINT_IN_CASE(MINT_STARG_I4) STARG(i, gint32); MINT_IN_BREAK;
4787 MINT_IN_CASE(MINT_STARG_I8) STARG(l, gint64); MINT_IN_BREAK;
4788 MINT_IN_CASE(MINT_STARG_R4) STARG(f, float); MINT_IN_BREAK;
4789 MINT_IN_CASE(MINT_STARG_R8) STARG(f, double); MINT_IN_BREAK;
4790 MINT_IN_CASE(MINT_STARG_O) STARG(p, gpointer); MINT_IN_BREAK;
4791 MINT_IN_CASE(MINT_STARG_P) STARG(p, gpointer); MINT_IN_BREAK;
4793 MINT_IN_CASE(MINT_STARG_VT)
4794 i32 = READ32(ip + 2);
4796 memcpy(frame->args + * (guint16 *)(ip + 1), sp->data.p, i32);
4797 vt_sp -= (i32 + 7) & ~7;
4801 #define STINARG(datamem, argtype) \
4803 int n = * (guint16 *)(ip + 1); \
4804 * (argtype *)(frame->args + rtm->arg_offsets [n]) = frame->stack_args [n].data.datamem; \
4808 MINT_IN_CASE(MINT_STINARG_I1) STINARG(i, gint8); MINT_IN_BREAK;
4809 MINT_IN_CASE(MINT_STINARG_U1) STINARG(i, guint8); MINT_IN_BREAK;
4810 MINT_IN_CASE(MINT_STINARG_I2) STINARG(i, gint16); MINT_IN_BREAK;
4811 MINT_IN_CASE(MINT_STINARG_U2) STINARG(i, guint16); MINT_IN_BREAK;
4812 MINT_IN_CASE(MINT_STINARG_I4) STINARG(i, gint32); MINT_IN_BREAK;
4813 MINT_IN_CASE(MINT_STINARG_I8) STINARG(l, gint64); MINT_IN_BREAK;
4814 MINT_IN_CASE(MINT_STINARG_R4) STINARG(f, float); MINT_IN_BREAK;
4815 MINT_IN_CASE(MINT_STINARG_R8) STINARG(f, double); MINT_IN_BREAK;
4816 MINT_IN_CASE(MINT_STINARG_O) STINARG(p, gpointer); MINT_IN_BREAK;
4817 MINT_IN_CASE(MINT_STINARG_P) STINARG(p, gpointer); MINT_IN_BREAK;
4819 MINT_IN_CASE(MINT_STINARG_VT) {
4820 int n = * (guint16 *)(ip + 1);
4821 i32 = READ32(ip + 2);
4822 memcpy (frame->args + rtm->arg_offsets [n], frame->stack_args [n].data.p, i32);
4827 MINT_IN_CASE(MINT_LDARGA)
4828 sp->data.p = frame->args + * (guint16 *)(ip + 1);
4833 #define LDLOC(datamem, argtype) \
4834 sp->data.datamem = * (argtype *)(locals + * (guint16 *)(ip + 1)); \
4838 MINT_IN_CASE(MINT_LDLOC_I1) LDLOC(i, gint8); MINT_IN_BREAK;
4839 MINT_IN_CASE(MINT_LDLOC_U1) LDLOC(i, guint8); MINT_IN_BREAK;
4840 MINT_IN_CASE(MINT_LDLOC_I2) LDLOC(i, gint16); MINT_IN_BREAK;
4841 MINT_IN_CASE(MINT_LDLOC_U2) LDLOC(i, guint16); MINT_IN_BREAK;
4842 MINT_IN_CASE(MINT_LDLOC_I4) LDLOC(i, gint32); MINT_IN_BREAK;
4843 MINT_IN_CASE(MINT_LDLOC_I8) LDLOC(l, gint64); MINT_IN_BREAK;
4844 MINT_IN_CASE(MINT_LDLOC_R4) LDLOC(f, float); MINT_IN_BREAK;
4845 MINT_IN_CASE(MINT_LDLOC_R8) LDLOC(f, double); MINT_IN_BREAK;
4846 MINT_IN_CASE(MINT_LDLOC_O) LDLOC(p, gpointer); MINT_IN_BREAK;
4847 MINT_IN_CASE(MINT_LDLOC_P) LDLOC(p, gpointer); MINT_IN_BREAK;
4849 MINT_IN_CASE(MINT_LDLOC_VT)
4851 i32 = READ32(ip + 2);
4852 memcpy(sp->data.p, locals + * (guint16 *)(ip + 1), i32);
4853 vt_sp += (i32 + 7) & ~7;
4858 MINT_IN_CASE(MINT_LDLOCA_S)
4859 sp->data.p = locals + * (guint16 *)(ip + 1);
4864 #define STLOC(datamem, argtype) \
4866 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp->data.datamem; \
4869 MINT_IN_CASE(MINT_STLOC_I1) STLOC(i, gint8); MINT_IN_BREAK;
4870 MINT_IN_CASE(MINT_STLOC_U1) STLOC(i, guint8); MINT_IN_BREAK;
4871 MINT_IN_CASE(MINT_STLOC_I2) STLOC(i, gint16); MINT_IN_BREAK;
4872 MINT_IN_CASE(MINT_STLOC_U2) STLOC(i, guint16); MINT_IN_BREAK;
4873 MINT_IN_CASE(MINT_STLOC_I4) STLOC(i, gint32); MINT_IN_BREAK;
4874 MINT_IN_CASE(MINT_STLOC_I8) STLOC(l, gint64); MINT_IN_BREAK;
4875 MINT_IN_CASE(MINT_STLOC_R4) STLOC(f, float); MINT_IN_BREAK;
4876 MINT_IN_CASE(MINT_STLOC_R8) STLOC(f, double); MINT_IN_BREAK;
4877 MINT_IN_CASE(MINT_STLOC_O) STLOC(p, gpointer); MINT_IN_BREAK;
4878 MINT_IN_CASE(MINT_STLOC_P) STLOC(p, gpointer); MINT_IN_BREAK;
4880 #define STLOC_NP(datamem, argtype) \
4881 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp [-1].data.datamem; \
4884 MINT_IN_CASE(MINT_STLOC_NP_I4) STLOC_NP(i, gint32); MINT_IN_BREAK;
4885 MINT_IN_CASE(MINT_STLOC_NP_O) STLOC_NP(p, gpointer); MINT_IN_BREAK;
4887 MINT_IN_CASE(MINT_STLOC_VT)
4888 i32 = READ32(ip + 2);
4890 memcpy(locals + * (guint16 *)(ip + 1), sp->data.p, i32);
4891 vt_sp -= (i32 + 7) & ~7;
4895 MINT_IN_CASE(MINT_LOCALLOC) {
4896 if (sp != frame->stack + 1) /*FIX?*/
4897 THROW_EX (mono_get_exception_execution_engine (NULL), ip);
4899 int len = sp [-1].data.i;
4900 sp [-1].data.p = alloca (len);
4901 MonoMethodHeader *header = mono_method_get_header_checked (frame->runtime_method->method, &error);
4902 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4903 if (header && header->init_locals)
4904 memset (sp [-1].data.p, 0, len);
4908 MINT_IN_CASE(MINT_ENDFILTER)
4909 /* top of stack is result of filter */
4910 frame->retval = &sp [-1];
4912 MINT_IN_CASE(MINT_INITOBJ)
4914 memset (sp->data.vt, 0, READ32(ip + 1));
4917 MINT_IN_CASE(MINT_CPBLK)
4919 if (!sp [0].data.p || !sp [1].data.p)
4920 THROW_EX (mono_get_exception_null_reference(), ip - 1);
4922 /* FIXME: value and size may be int64... */
4923 memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
4926 MINT_IN_CASE(MINT_CONSTRAINED_) {
4928 /* FIXME: implement */
4930 token = READ32 (ip);
4935 MINT_IN_CASE(MINT_INITBLK)
4938 THROW_EX (mono_get_exception_null_reference(), ip - 1);
4940 /* FIXME: value and size may be int64... */
4941 memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
4944 MINT_IN_CASE(MINT_NO_)
4945 /* FIXME: implement */
4949 MINT_IN_CASE(MINT_RETHROW)
4951 * need to clarify what this should actually do:
4952 * start the search from the last found handler in
4953 * this method or continue in the caller or what.
4954 * Also, do we need to run finally/fault handlers after a retrow?
4955 * Well, this implementation will follow the usual search
4956 * for an handler, considering the current ip as throw spot.
4957 * We need to NULL frame->ex_handler for the later code to
4958 * actually run the new found handler.
4960 frame->ex_handler = NULL;
4961 THROW_EX (frame->ex, ip - 1);
4964 g_print ("Unimplemented opcode: %04x %s at 0x%x\n", *ip, mono_interp_opname[*ip], ip-rtm->code);
4965 THROW_EX (mono_get_exception_execution_engine ("Unimplemented opcode"), ip);
4969 g_assert_not_reached ();
4971 * Exception handling code.
4972 * The exception object is stored in frame->ex.
4979 MonoInvocation *inv;
4980 MonoExceptionClause *clause;
4986 g_print ("* Handling exception '%s' at IL_%04x\n",
4987 frame->ex == NULL ? "** Unknown **" : mono_object_class (frame->ex)->name,
4988 rtm == NULL ? 0 : frame->ip - rtm->code);
4990 if (die_on_exception)
4993 for (inv = frame; inv; inv = inv->parent) {
4995 if (inv->runtime_method == NULL)
4997 method = inv->runtime_method->method;
4998 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
5000 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
5002 if (inv->ip == NULL)
5004 ip_offset = inv->ip - inv->runtime_method->code;
5005 inv->ex_handler = NULL; /* clear this in case we are trhowing an exception while handling one - this one wins */
5006 for (i = 0; i < inv->runtime_method->num_clauses; ++i) {
5007 clause = &inv->runtime_method->clauses [i];
5009 g_print ("* clause [%d]: %p\n", i, clause);
5011 if (!MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
5014 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
5017 g_print ("* Filter found at '%s'\n", method->name);
5019 MonoInvocation dup_frame;
5021 memcpy (&dup_frame, inv, sizeof (MonoInvocation));
5022 dup_frame.retval = &retval;
5023 ves_exec_method_with_context (&dup_frame, context, inv->runtime_method->code + clause->data.filter_offset, frame->ex, -1);
5024 if (dup_frame.retval->data.i) {
5027 g_print ("* Matched Filter at '%s'\n", method->name);
5029 inv->ex_handler = clause;
5030 goto handle_finally;
5032 } else if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
5033 MonoObject *isinst_obj = mono_object_isinst_checked ((MonoObject*)frame->ex, clause->data.catch_class, &error);
5034 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
5037 * OK, we found an handler, now we need to execute the finally
5038 * and fault blocks before branching to the handler code.
5042 g_print ("* Found handler at '%s'\n", method->name);
5044 inv->ex_handler = clause;
5045 goto handle_finally;
5051 * If we get here, no handler was found: print a stack trace.
5053 for (inv = frame; inv; inv = inv->parent) {
5054 if (inv->invoke_trap)
5055 goto handle_finally;
5058 ex_obj = (MonoObject *) frame->ex;
5059 mono_unhandled_exception (ex_obj);
5060 MonoJitTlsData *jit_tls = (MonoJitTlsData *) mono_tls_get_jit_tls ();
5061 jit_tls->abort_func (ex_obj);
5062 g_assert_not_reached ();
5068 MonoExceptionClause *clause;
5069 GSList *old_list = finally_ips;
5070 MonoMethod *method = frame->runtime_method->method;
5071 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
5072 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
5076 g_print ("* Handle finally IL_%04x\n", endfinally_ip == NULL ? 0 : endfinally_ip - rtm->code);
5078 if (rtm == NULL || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
5079 || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
5082 ip_offset = frame->ip - rtm->code;
5084 if (endfinally_ip != NULL)
5085 finally_ips = g_slist_prepend(finally_ips, (void *)endfinally_ip);
5086 for (i = 0; i < header->num_clauses; ++i)
5087 if (frame->ex_handler == &rtm->clauses [i])
5091 clause = &rtm->clauses [i];
5092 if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && (endfinally_ip == NULL || !(MONO_OFFSET_IN_CLAUSE (clause, endfinally_ip - rtm->code)))) {
5093 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
5094 ip = rtm->code + clause->handler_offset;
5095 finally_ips = g_slist_prepend (finally_ips, (gpointer) ip);
5098 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
5104 endfinally_ip = NULL;
5106 if (old_list != finally_ips && finally_ips) {
5107 ip = finally_ips->data;
5108 finally_ips = g_slist_remove (finally_ips, ip);
5109 sp = frame->stack; /* spec says stack should be empty at endfinally so it should be at the start too */
5114 * If an exception is set, we need to execute the fault handler, too,
5115 * otherwise, we continue normally.
5125 MonoExceptionClause *clause;
5126 MonoMethod *method = frame->runtime_method->method;
5127 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
5128 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
5132 g_print ("* Handle fault\n");
5134 ip_offset = frame->ip - rtm->code;
5135 for (i = 0; i < header->num_clauses; ++i) {
5136 clause = &rtm->clauses [i];
5137 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
5138 ip = rtm->code + clause->handler_offset;
5141 g_print ("* Executing handler at IL_%04x\n", clause->handler_offset);
5147 * If the handler for the exception was found in this method, we jump
5148 * to it right away, otherwise we return and let the caller run
5149 * the finally, fault and catch blocks.
5150 * This same code should be present in the endfault opcode, but it
5151 * is corrently not assigned in the ECMA specs: LAMESPEC.
5153 if (frame->ex_handler) {
5156 g_print ("* Executing handler at IL_%04x\n", frame->ex_handler->handler_offset);
5158 ip = rtm->code + frame->ex_handler->handler_offset;
5160 vt_sp = (unsigned char *) sp + rtm->stack_size;
5161 sp->data.p = frame->ex;
5172 ves_exec_method (MonoInvocation *frame)
5174 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
5175 ThreadContext context_struct;
5182 mono_unhandled_exception ((MonoObject*)frame->ex);
5185 if (context == NULL) {
5186 context = &context_struct;
5187 context_struct.domain = mono_domain_get ();
5188 context_struct.base_frame = frame;
5189 context_struct.current_frame = NULL;
5190 context_struct.env_frame = frame;
5191 context_struct.current_env = &env;
5192 context_struct.search_for_handler = 0;
5193 context_struct.managed_code = 0;
5194 set_context (context);
5197 frame->parent = context->current_frame;
5198 frame->runtime_method = mono_interp_get_runtime_method (context->domain, frame->method, &error);
5199 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
5200 context->managed_code = 1;
5201 ves_exec_method_with_context (frame, context, NULL, NULL, -1);
5202 context->managed_code = 0;
5204 if (context != &context_struct && context->current_env) {
5205 context->env_frame->ex = frame->ex;
5206 longjmp (*context->current_env, 1);
5209 mono_unhandled_exception ((MonoObject*)frame->ex);
5211 if (context->base_frame == frame)
5214 context->current_frame = frame->parent;
5218 mono_interp_parse_options (const char *options)
5222 args = g_strsplit (options, ",", -1);
5223 for (ptr = args; ptr && *ptr; ptr ++) {
5226 if (strncmp (arg, "jit=", 4) == 0)
5227 jit_classes = g_slist_prepend (jit_classes, arg + 4);
5234 mono_native_tls_alloc (&thread_context_id, NULL);
5237 mono_interp_transform_init ();
5240 typedef int (*TestMethod) (void);
5243 interp_regression_step (MonoImage *image, int verbose, int *total_run, int *total, GTimer *timer, MonoDomain *domain)
5245 int result, expected, failed, cfailed, run;
5246 double elapsed, transform_time;
5248 MonoObject *result_obj;
5249 static gboolean filter_method_init = FALSE;
5250 static const char *filter_method = NULL;
5252 g_print ("Test run: image=%s\n", mono_image_get_filename (image));
5253 cfailed = failed = run = 0;
5254 transform_time = elapsed = 0.0;
5256 g_timer_start (timer);
5257 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
5258 MonoObject *exc = NULL;
5260 MonoMethod *method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
5262 mono_error_cleanup (&error); /* FIXME don't swallow the error */
5266 if (!filter_method_init) {
5267 filter_method = g_getenv ("INTERP_FILTER_METHOD");
5268 filter_method_init = TRUE;
5270 gboolean filter = FALSE;
5271 if (filter_method) {
5272 const char *name = filter_method;
5274 if ((strchr (name, '.') > name) || strchr (name, ':')) {
5275 MonoMethodDesc *desc = mono_method_desc_new (name, TRUE);
5276 filter = mono_method_desc_full_match (desc, method);
5277 mono_method_desc_free (desc);
5279 filter = strcmp (method->name, name) == 0;
5281 } else { /* no filter, check for `Category' attribute on method */
5283 MonoCustomAttrInfo* ainfo = mono_custom_attrs_from_method_checked (method, &error);
5284 mono_error_cleanup (&error);
5288 for (j = 0; j < ainfo->num_attrs && filter; ++j) {
5289 MonoCustomAttrEntry *centry = &ainfo->attrs [j];
5290 if (centry->ctor == NULL)
5293 MonoClass *klass = centry->ctor->klass;
5294 if (strcmp (klass->name, "CategoryAttribute"))
5297 MonoObject *obj = mono_custom_attrs_get_attr_checked (ainfo, klass, &error);
5298 /* FIXME: there is an ordering problem if there're multiple attributes, do this instead:
5299 * MonoObject *obj = create_custom_attr (ainfo->image, centry->ctor, centry->data, centry->data_size, &error); */
5300 mono_error_cleanup (&error);
5301 MonoMethod *getter = mono_class_get_method_from_name (klass, "get_Category", -1);
5302 MonoObject *str = mono_interp_runtime_invoke (getter, obj, NULL, &exc, &error);
5303 mono_error_cleanup (&error);
5304 char *utf8_str = mono_string_to_utf8_checked ((MonoString *) str, &error);
5305 mono_error_cleanup (&error);
5306 if (!strcmp (utf8_str, "!INTERPRETER")) {
5307 g_print ("skip %s...\n", method->name);
5313 if (strncmp (method->name, "test_", 5) == 0 && filter) {
5314 MonoError interp_error;
5315 MonoObject *exc = NULL;
5317 result_obj = mono_interp_runtime_invoke (method, NULL, NULL, &exc, &interp_error);
5318 if (!mono_error_ok (&interp_error)) {
5320 g_print ("Test '%s' execution failed.\n", method->name);
5321 } else if (exc != NULL) {
5322 g_print ("Exception in Test '%s' occured:\n", method->name);
5323 mono_object_describe (exc);
5327 result = *(gint32 *) mono_object_unbox (result_obj);
5328 expected = atoi (method->name + 5); // FIXME: oh no.
5331 if (result != expected) {
5333 g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
5338 g_timer_stop (timer);
5339 elapsed = g_timer_elapsed (timer, NULL);
5340 if (failed > 0 || cfailed > 0){
5341 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
5342 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
5344 g_print ("Results: total tests: %d, all pass \n", run);
5347 g_print ("Elapsed time: %f secs (%f, %f)\n\n", elapsed,
5348 elapsed - transform_time, transform_time);
5349 *total += failed + cfailed;
5354 interp_regression (MonoImage *image, int verbose, int *total_run)
5357 GTimer *timer = g_timer_new ();
5358 MonoDomain *domain = mono_domain_get ();
5362 /* load the metadata */
5363 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
5365 method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
5367 mono_error_cleanup (&error);
5370 mono_class_init (method->klass);
5375 interp_regression_step (image, verbose, total_run, &total, timer, domain);
5377 g_timer_destroy (timer);
5382 mono_interp_regression_list (int verbose, int count, char *images [])
5384 int i, total, total_run, run;
5386 total_run = total = 0;
5387 for (i = 0; i < count; ++i) {
5388 MonoAssembly *ass = mono_assembly_open_predicate (images [i], FALSE, FALSE, NULL, NULL, NULL);
5390 g_warning ("failed to load assembly: %s", images [i]);
5393 total += interp_regression (mono_assembly_get_image (ass), verbose, &run);
5397 g_print ("Overall results: tests: %d, failed: %d (pass: %.2f%%)\n", total_run, total, 100.0*(total_run-total)/total_run);
5399 g_print ("Overall results: tests: %d, 100%% pass\n", total_run);
5406 * mono_interp_set_resume_state:
5408 * Set the state the interpeter will continue to execute from after execution returns to the interpreter.
5411 mono_interp_set_resume_state (MonoJitTlsData *jit_tls, MonoException *ex, MonoInterpFrameHandle interp_frame, gpointer handler_ip)
5413 ThreadContext *context;
5416 context = jit_tls->interp_context;
5419 context->has_resume_state = TRUE;
5420 context->handler_frame = interp_frame;
5421 /* This is on the stack, so it doesn't need a wbarrier */
5422 context->handler_frame->ex = ex;
5423 context->handler_ip = handler_ip;
5427 * mono_interp_run_finally:
5429 * Run the finally clause identified by CLAUSE_INDEX in the intepreter frame given by
5430 * frame->interp_frame.
5433 mono_interp_run_finally (StackFrameInfo *frame, int clause_index, gpointer handler_ip)
5435 MonoInvocation *iframe = frame->interp_frame;
5436 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
5438 ves_exec_method_with_context (iframe, context, handler_ip, NULL, clause_index);
5442 MonoInvocation *current;
5446 * mono_interp_frame_iter_init:
5448 * Initialize an iterator for iterating through interpreted frames.
5451 mono_interp_frame_iter_init (MonoInterpStackIter *iter, gpointer interp_exit_data)
5453 StackIter *stack_iter = (StackIter*)iter;
5455 stack_iter->current = (MonoInvocation*)interp_exit_data;
5459 mono_interp_frame_iter_next (MonoInterpStackIter *iter, StackFrameInfo *frame)
5461 StackIter *stack_iter = (StackIter*)iter;
5462 MonoInvocation *iframe = stack_iter->current;
5464 memset (frame, 0, sizeof (StackFrameInfo));
5465 /* pinvoke frames doesn't have runtime_method set */
5466 while (iframe && !(iframe->runtime_method && iframe->runtime_method->code))
5467 iframe = iframe->parent;
5471 frame->type = FRAME_TYPE_INTERP;
5473 frame->domain = mono_domain_get ();
5474 frame->interp_frame = iframe;
5475 frame->method = iframe->runtime_method->method;
5476 frame->actual_method = frame->method;
5477 /* This is the offset in the interpreter IR */
5478 frame->native_offset = (guint8*)iframe->ip - (guint8*)iframe->runtime_method->code;
5479 frame->ji = iframe->runtime_method->jinfo;
5481 stack_iter->current = iframe->parent;
5487 mono_interp_find_jit_info (MonoDomain *domain, MonoMethod *method)
5491 rtm = lookup_runtime_method (domain, method);
5499 mono_interp_set_breakpoint (MonoJitInfo *jinfo, gpointer ip)
5501 guint16 *code = (guint16*)ip;
5502 g_assert (*code == MINT_SDB_SEQ_POINT);
5503 *code = MINT_SDB_BREAKPOINT;
5507 mono_interp_clear_breakpoint (MonoJitInfo *jinfo, gpointer ip)
5509 guint16 *code = (guint16*)ip;
5510 g_assert (*code == MINT_SDB_BREAKPOINT);
5511 *code = MINT_SDB_SEQ_POINT;
5515 mono_interp_frame_get_jit_info (MonoInterpFrameHandle frame)
5517 MonoInvocation *iframe = (MonoInvocation*)frame;
5519 g_assert (iframe->runtime_method);
5520 return iframe->runtime_method->jinfo;
5524 mono_interp_frame_get_ip (MonoInterpFrameHandle frame)
5526 MonoInvocation *iframe = (MonoInvocation*)frame;
5528 g_assert (iframe->runtime_method);
5529 return (gpointer)iframe->ip;
5533 mono_interp_frame_get_arg (MonoInterpFrameHandle frame, int pos)
5535 MonoInvocation *iframe = (MonoInvocation*)frame;
5537 g_assert (iframe->runtime_method);
5539 int arg_offset = iframe->runtime_method->arg_offsets [pos + (iframe->runtime_method->hasthis ? 1 : 0)];
5541 return iframe->args + arg_offset;
5545 mono_interp_frame_get_local (MonoInterpFrameHandle frame, int pos)
5547 MonoInvocation *iframe = (MonoInvocation*)frame;
5549 g_assert (iframe->runtime_method);
5551 return iframe->locals + iframe->runtime_method->local_offsets [pos];
5555 mono_interp_frame_get_this (MonoInterpFrameHandle frame)
5557 MonoInvocation *iframe = (MonoInvocation*)frame;
5559 g_assert (iframe->runtime_method);
5560 g_assert (iframe->runtime_method->hasthis);
5562 int arg_offset = iframe->runtime_method->arg_offsets [0];
5564 return iframe->args + arg_offset;
5568 mono_interp_start_single_stepping (void)
5574 mono_interp_stop_single_stepping (void)