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>
71 /* Mingw 2.1 doesnt need this any more, but leave it in for now for older versions */
74 #define finite _finite
78 #define finite isfinite
83 init_frame (MonoInvocation *frame, MonoInvocation *parent_frame, RuntimeMethod *rmethod, stackval *method_args, stackval *method_retval)
85 frame->parent = parent_frame;
86 frame->stack_args = method_args;
87 frame->retval = method_retval;
88 frame->runtime_method = rmethod;
91 frame->invoke_trap = 0;
94 #define INIT_FRAME(frame,parent_frame,method_args,method_retval,domain,mono_method,error) do { \
95 RuntimeMethod *_rmethod = mono_interp_get_runtime_method ((domain), (mono_method), (error)); \
96 init_frame ((frame), (parent_frame), _rmethod, (method_args), (method_retval)); \
100 * List of classes whose methods will be executed by transitioning to JITted code.
105 void ves_exec_method (MonoInvocation *frame);
107 static char* dump_stack (stackval *stack, stackval *sp);
108 static char* dump_frame (MonoInvocation *inv);
109 static MonoArray *get_trace_ips (MonoDomain *domain, MonoInvocation *top);
110 static void ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, unsigned short *start_with_ip, MonoException *filter_exception, int exit_at_finally);
112 typedef void (*ICallMethod) (MonoInvocation *frame);
114 static guint32 die_on_exception = 0;
115 static MonoNativeTlsKey thread_context_id;
117 static char* dump_args (MonoInvocation *inv);
119 #define DEBUG_INTERP 0
122 int mono_interp_traceopt = 2;
123 /* If true, then we output the opcodes as we interpret them */
124 static int global_tracing = 2;
126 static int debug_indent_level = 0;
128 static int break_on_method = 0;
129 static int nested_trace = 0;
130 static GList *db_methods = NULL;
137 for (h = 0; h < debug_indent_level; h++)
142 db_match_method (gpointer data, gpointer user_data)
144 MonoMethod *m = (MonoMethod*)user_data;
145 MonoMethodDesc *desc = data;
147 if (mono_method_desc_full_match (desc, m))
152 debug_enter (MonoInvocation *frame, int *tracing)
155 g_list_foreach (db_methods, db_match_method, (gpointer)frame->runtime_method->method);
157 *tracing = nested_trace ? (global_tracing = 2, 3) : 2;
161 MonoMethod *method = frame->runtime_method->method;
162 char *mn, *args = dump_args (frame);
163 debug_indent_level++;
165 mn = mono_method_full_name (method, FALSE);
166 g_print ("(%p) Entering %s (", mono_thread_internal_current (), mn);
168 g_print ("%s)\n", args);
171 if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE)
172 mono_profiler_method_enter (frame->runtime_method->method);
176 #define DEBUG_LEAVE() \
179 args = dump_retval (frame); \
181 mn = mono_method_full_name (frame->runtime_method->method, FALSE); \
182 g_print ("(%p) Leaving %s", mono_thread_internal_current (), mn); \
184 g_print (" => %s\n", args); \
186 debug_indent_level--; \
187 if (tracing == 3) global_tracing = 0; \
189 if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE) \
190 mono_profiler_method_leave (frame->runtime_method->method);
194 int mono_interp_traceopt = 0;
195 static void debug_enter (MonoInvocation *frame, int *tracing)
198 #define DEBUG_LEAVE()
202 /* Set the current execution state to the resume state in context */
203 #define SET_RESUME_STATE(context) do { \
204 ip = (context)->handler_ip; \
205 sp->data.p = frame->ex; \
208 (context)->has_resume_state = 0; \
209 (context)->handler_frame = NULL; \
214 interp_ex_handler (MonoException *ex) {
216 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
220 stack_trace = dump_frame (context->current_frame);
221 ex->stack_trace = mono_string_new (mono_domain_get(), stack_trace);
222 g_free (stack_trace);
223 if (context->current_env == NULL || strcmp(ex->object.vtable->klass->name, "ExecutionEngineException") == 0) {
224 char *strace = mono_string_to_utf8_checked (ex->stack_trace, &error);
225 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
226 fprintf(stderr, "Nothing can catch this exception: ");
227 fprintf(stderr, "%s", ex->object.vtable->klass->name);
228 if (ex->message != NULL) {
229 char *m = mono_string_to_utf8_checked (ex->message, &error);
230 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
231 fprintf(stderr, ": %s", m);
234 fprintf(stderr, "\n%s\n", strace);
236 if (ex->inner_ex != NULL) {
237 ex = (MonoException *)ex->inner_ex;
238 fprintf(stderr, "Inner exception: %s", ex->object.vtable->klass->name);
239 if (ex->message != NULL) {
240 char *m = mono_string_to_utf8_checked (ex->message, &error);
241 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
242 fprintf(stderr, ": %s", m);
245 strace = mono_string_to_utf8_checked (ex->stack_trace, &error);
246 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
247 fprintf(stderr, "\n");
248 fprintf(stderr, "%s\n", strace);
251 /* wait for other threads to also collapse */
252 // Sleep(1000); // TODO: proper sleep
255 context->env_frame->ex = ex;
256 context->search_for_handler = 1;
257 longjmp (*context->current_env, 1);
261 ves_real_abort (int line, MonoMethod *mh,
262 const unsigned short *ip, stackval *stack, stackval *sp)
265 fprintf (stderr, "Execution aborted in method: %s::%s\n", mh->klass->name, mh->name);
266 fprintf (stderr, "Line=%d IP=0x%04lx, Aborted execution\n", line,
267 ip-(const unsigned short *)mono_method_get_header_checked (mh, &error)->code);
268 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
269 g_print ("0x%04x %02x\n",
270 ip-(const unsigned short *)mono_method_get_header_checked (mh, &error)->code, *ip);
271 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
273 printf ("\t[%ld] 0x%08x %0.5f\n", sp-stack, sp[-1].data.i, sp[-1].data.f);
276 #define ves_abort() \
278 ves_real_abort(__LINE__, frame->runtime_method->method, ip, frame->stack, sp); \
279 THROW_EX (mono_get_exception_execution_engine (NULL), ip); \
283 mono_interp_get_runtime_method (MonoDomain *domain, MonoMethod *method, MonoError *error)
286 MonoJitDomainInfo *info;
287 MonoMethodSignature *sig;
292 info = domain_jit_info (domain);
293 mono_domain_jit_code_hash_lock (domain);
294 rtm = mono_internal_hash_table_lookup (&info->interp_code_hash, method);
295 mono_domain_jit_code_hash_unlock (domain);
299 sig = mono_method_signature (method);
301 rtm = mono_domain_alloc0 (domain, sizeof (RuntimeMethod));
302 rtm->method = method;
303 rtm->param_count = sig->param_count;
304 rtm->hasthis = sig->hasthis;
305 rtm->rtype = mini_get_underlying_type (sig->ret);
306 rtm->param_types = mono_domain_alloc0 (domain, sizeof (MonoType*) * sig->param_count);
307 for (i = 0; i < sig->param_count; ++i)
308 rtm->param_types [i] = mini_get_underlying_type (sig->params [i]);
310 mono_domain_jit_code_hash_lock (domain);
311 if (!mono_internal_hash_table_lookup (&info->interp_code_hash, method))
312 mono_internal_hash_table_insert (&info->interp_code_hash, method, rtm);
313 mono_domain_jit_code_hash_unlock (domain);
319 mono_interp_create_trampoline (MonoDomain *domain, MonoMethod *method, MonoError *error)
321 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
322 method = mono_marshal_get_synchronized_wrapper (method);
323 return mono_interp_get_runtime_method (domain, method, error);
326 static inline RuntimeMethod*
327 get_virtual_method (MonoDomain *domain, RuntimeMethod *runtime_method, MonoObject *obj)
329 MonoMethod *m = runtime_method->method;
332 if ((m->flags & METHOD_ATTRIBUTE_FINAL) || !(m->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
333 RuntimeMethod *ret = NULL;
334 if (mono_object_is_transparent_proxy (obj)) {
335 ret = mono_interp_get_runtime_method (domain, mono_marshal_get_remoting_invoke (m), &error);
336 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
337 } else if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
338 ret = mono_interp_get_runtime_method (domain, mono_marshal_get_synchronized_wrapper (m), &error);
339 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
341 ret = runtime_method;
346 mono_class_setup_vtable (obj->vtable->klass);
348 int slot = mono_method_get_vtable_slot (m);
349 if (mono_class_is_interface (m->klass)) {
350 g_assert (obj->vtable->klass != m->klass);
351 /* TODO: interface offset lookup is slow, go through IMT instead */
352 gboolean non_exact_match;
353 slot += mono_class_interface_offset_with_variance (obj->vtable->klass, m->klass, &non_exact_match);
356 MonoMethod *virtual_method = obj->vtable->klass->vtable [slot];
357 if (m->is_inflated && mono_method_get_context (m)->method_inst) {
358 MonoGenericContext context = { NULL, NULL };
360 if (mono_class_is_ginst (virtual_method->klass))
361 context.class_inst = mono_class_get_generic_class (virtual_method->klass)->context.class_inst;
362 else if (mono_class_is_gtd (virtual_method->klass))
363 context.class_inst = mono_class_get_generic_container (virtual_method->klass)->context.class_inst;
364 context.method_inst = mono_method_get_context (m)->method_inst;
366 virtual_method = mono_class_inflate_generic_method_checked (virtual_method, &context, &error);
367 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
370 if (virtual_method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
371 virtual_method = mono_marshal_get_synchronized_wrapper (virtual_method);
374 RuntimeMethod *virtual_runtime_method = mono_interp_get_runtime_method (domain, virtual_method, &error);
375 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
376 return virtual_runtime_method;
380 stackval_from_data (MonoType *type, stackval *result, char *data, gboolean pinvoke)
383 switch (type->type) {
384 case MONO_TYPE_OBJECT:
385 case MONO_TYPE_CLASS:
386 case MONO_TYPE_STRING:
387 case MONO_TYPE_ARRAY:
388 case MONO_TYPE_SZARRAY:
393 result->data.p = *(gpointer*)data;
396 switch (type->type) {
400 result->data.i = *(gint8*)data;
403 case MONO_TYPE_BOOLEAN:
404 result->data.i = *(guint8*)data;
407 result->data.i = *(gint16*)data;
411 result->data.i = *(guint16*)data;
414 result->data.i = *(gint32*)data;
418 result->data.nati = *(mono_i*)data;
421 result->data.p = *(gpointer*)data;
424 result->data.i = *(guint32*)data;
427 result->data.f = *(float*)data;
431 result->data.l = *(gint64*)data;
434 result->data.f = *(double*)data;
436 case MONO_TYPE_STRING:
437 case MONO_TYPE_SZARRAY:
438 case MONO_TYPE_CLASS:
439 case MONO_TYPE_OBJECT:
440 case MONO_TYPE_ARRAY:
441 result->data.p = *(gpointer*)data;
443 case MONO_TYPE_VALUETYPE:
444 if (type->data.klass->enumtype) {
445 stackval_from_data (mono_class_enum_basetype (type->data.klass), result, data, pinvoke);
448 mono_value_copy (result->data.vt, data, type->data.klass);
450 case MONO_TYPE_GENERICINST:
451 stackval_from_data (&type->data.generic_class->container_class->byval_arg, result, data, pinvoke);
454 g_warning ("got type 0x%02x", type->type);
455 g_assert_not_reached ();
460 stackval_to_data (MonoType *type, stackval *val, char *data, gboolean pinvoke)
463 gpointer *p = (gpointer*)data;
467 /* printf ("TODAT0 %p\n", data); */
468 switch (type->type) {
471 guint8 *p = (guint8*)data;
475 case MONO_TYPE_BOOLEAN: {
476 guint8 *p = (guint8*)data;
477 *p = (val->data.i != 0);
482 case MONO_TYPE_CHAR: {
483 guint16 *p = (guint16*)data;
488 mono_i *p = (mono_i*)data;
489 /* In theory the value used by stloc should match the local var type
490 but in practice it sometimes doesn't (a int32 gets dup'd and stloc'd into
491 a native int - both by csc and mcs). Not sure what to do about sign extension
492 as it is outside the spec... doing the obvious */
493 *p = (mono_i)val->data.nati;
497 mono_u *p = (mono_u*)data;
499 *p = (mono_u)val->data.nati;
504 gint32 *p = (gint32*)data;
510 gint64 *p = (gint64*)data;
515 float *p = (float*)data;
520 double *p = (double*)data;
524 case MONO_TYPE_STRING:
525 case MONO_TYPE_SZARRAY:
526 case MONO_TYPE_CLASS:
527 case MONO_TYPE_OBJECT:
528 case MONO_TYPE_ARRAY: {
529 gpointer *p = (gpointer *) data;
530 mono_gc_wbarrier_generic_store (p, val->data.p);
533 case MONO_TYPE_PTR: {
534 gpointer *p = (gpointer *) data;
538 case MONO_TYPE_VALUETYPE:
539 if (type->data.klass->enumtype) {
540 stackval_to_data (mono_class_enum_basetype (type->data.klass), val, data, pinvoke);
543 mono_value_copy (data, val->data.vt, type->data.klass);
545 case MONO_TYPE_GENERICINST: {
546 MonoClass *container_class = type->data.generic_class->container_class;
548 if (container_class->valuetype && !container_class->enumtype) {
549 mono_value_copy (data, val->data.vt, mono_class_from_mono_type (type));
552 stackval_to_data (&type->data.generic_class->container_class->byval_arg, val, data, pinvoke);
556 g_warning ("got type %x", type->type);
557 g_assert_not_reached ();
562 fill_in_trace (MonoException *exception, MonoInvocation *frame)
564 char *stack_trace = dump_frame (frame);
565 MonoDomain *domain = mono_domain_get();
566 (exception)->stack_trace = mono_string_new (domain, stack_trace);
567 (exception)->trace_ips = get_trace_ips (domain, frame);
568 g_free (stack_trace);
571 #define FILL_IN_TRACE(exception, frame) fill_in_trace(exception, frame)
573 #define THROW_EX(exception,ex_ip) \
575 frame->ip = (ex_ip); \
576 frame->ex = (MonoException*)(exception); \
577 FILL_IN_TRACE(frame->ex, frame); \
578 goto handle_exception; \
582 ves_array_create (MonoInvocation *frame, MonoDomain *domain, MonoClass *klass, MonoMethodSignature *sig, stackval *values)
585 intptr_t *lower_bounds;
590 lengths = alloca (sizeof (uintptr_t) * klass->rank * 2);
591 for (i = 0; i < sig->param_count; ++i) {
592 lengths [i] = values->data.i;
595 if (klass->rank == sig->param_count) {
596 /* Only lengths provided. */
599 /* lower bounds are first. */
600 lower_bounds = (intptr_t *) lengths;
601 lengths += klass->rank;
603 obj = (MonoObject*) mono_array_new_full_checked (domain, klass, lengths, lower_bounds, &error);
604 if (!mono_error_ok (&error)) {
605 frame->ex = mono_error_convert_to_exception (&error);
606 FILL_IN_TRACE (frame->ex, frame);
608 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
613 ves_array_calculate_index (MonoArray *ao, stackval *sp, MonoInvocation *frame)
615 g_assert (!frame->ex);
616 MonoClass *ac = ((MonoObject *) ao)->vtable->klass;
620 for (gint32 i = 0; i < ac->rank; i++) {
621 guint32 idx = sp [i].data.i;
622 guint32 lower = ao->bounds [i].lower_bound;
623 guint32 len = ao->bounds [i].length;
624 if (idx < lower || (idx - lower) >= len) {
625 frame->ex = mono_get_exception_index_out_of_range ();
626 FILL_IN_TRACE (frame->ex, frame);
629 pos = (pos * len) + idx - lower;
633 if (pos >= ao->max_length) {
634 frame->ex = mono_get_exception_index_out_of_range ();
635 FILL_IN_TRACE (frame->ex, frame);
643 ves_array_set (MonoInvocation *frame)
645 stackval *sp = frame->stack_args + 1;
647 MonoObject *o = frame->stack_args->data.p;
648 MonoArray *ao = (MonoArray *) o;
649 MonoClass *ac = o->vtable->klass;
651 g_assert (ac->rank >= 1);
653 gint32 pos = ves_array_calculate_index (ao, sp, frame);
657 if (sp [ac->rank].data.p && !mono_object_class (o)->element_class->valuetype) {
659 MonoObject *isinst = mono_object_isinst_checked (sp [ac->rank].data.p, mono_object_class (o)->element_class, &error);
660 mono_error_cleanup (&error);
662 frame->ex = mono_get_exception_array_type_mismatch ();
663 FILL_IN_TRACE (frame->ex, frame);
668 gint32 esize = mono_array_element_size (ac);
669 gpointer ea = mono_array_addr_with_size (ao, esize, pos);
671 MonoType *mt = mono_method_signature (frame->runtime_method->method)->params [ac->rank];
672 stackval_to_data (mt, &sp [ac->rank], ea, FALSE);
676 ves_array_get (MonoInvocation *frame)
678 stackval *sp = frame->stack_args + 1;
680 MonoObject *o = frame->stack_args->data.p;
681 MonoArray *ao = (MonoArray *) o;
682 MonoClass *ac = o->vtable->klass;
684 g_assert (ac->rank >= 1);
686 gint32 pos = ves_array_calculate_index (ao, sp, frame);
690 gint32 esize = mono_array_element_size (ac);
691 gpointer ea = mono_array_addr_with_size (ao, esize, pos);
693 MonoType *mt = mono_method_signature (frame->runtime_method->method)->ret;
694 stackval_from_data (mt, frame->retval, ea, FALSE);
698 ves_array_element_address (MonoInvocation *frame, MonoClass *required_type, MonoArray *ao, stackval *sp, gboolean needs_typecheck)
700 MonoClass *ac = ((MonoObject *) ao)->vtable->klass;
702 g_assert (ac->rank >= 1);
704 gint32 pos = ves_array_calculate_index (ao, sp, frame);
708 if (needs_typecheck && !mono_class_is_assignable_from (mono_object_class ((MonoObject *) ao)->element_class, required_type->element_class)) {
709 frame->ex = mono_get_exception_array_type_mismatch ();
710 FILL_IN_TRACE (frame->ex, frame);
713 gint32 esize = mono_array_element_size (ac);
714 return mono_array_addr_with_size (ao, esize, pos);
718 interp_walk_stack_with_ctx (MonoInternalStackWalk func, MonoContext *ctx, MonoUnwindOptions options, void *user_data)
721 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
726 MonoInvocation *frame = context->current_frame;
729 MonoStackFrameInfo fi;
730 memset (&fi, 0, sizeof (MonoStackFrameInfo));
732 /* TODO: hack to make some asserts happy. */
733 fi.ji = (MonoJitInfo *) frame->runtime_method;
735 if (frame->runtime_method)
736 fi.method = fi.actual_method = frame->runtime_method->method;
738 if (!fi.method || (fi.method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) || (fi.method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
740 fi.type = FRAME_TYPE_MANAGED_TO_NATIVE;
742 MonoMethodHeader *hd = mono_method_get_header_checked (fi.method, &error);
743 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
744 fi.type = FRAME_TYPE_MANAGED;
745 fi.il_offset = frame->ip - (const unsigned short *) hd->code;
746 if (!fi.method->wrapper_type)
750 if (func (&fi, ctx, user_data))
752 frame = frame->parent;
756 static MonoPIFunc mono_interp_enter_icall_trampoline = NULL;
758 struct _MethodArguments {
767 typedef struct _MethodArguments MethodArguments;
769 // TODO: this function is also arch dependent (register width).
770 static MethodArguments* build_args_from_sig (MonoMethodSignature *sig, MonoInvocation *frame)
772 // TODO: don't malloc this data structure.
773 MethodArguments *margs = g_malloc0 (sizeof (MethodArguments));
778 for (int i = 0; i < sig->param_count; i++) {
779 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
781 case MONO_TYPE_BOOLEAN:
792 case MONO_TYPE_SZARRAY:
793 case MONO_TYPE_CLASS:
794 case MONO_TYPE_OBJECT:
795 case MONO_TYPE_STRING:
797 case MONO_TYPE_VALUETYPE:
798 case MONO_TYPE_GENERICINST:
806 g_error ("build_args_from_sig: not implemented yet (1): 0x%x\n", ptype);
811 margs->iargs = g_malloc0 (sizeof (gpointer) * margs->ilen);
814 margs->fargs = g_malloc0 (sizeof (double) * margs->flen);
816 if (margs->ilen > 12)
817 g_error ("build_args_from_sig: TODO, allocate gregs: %d\n", margs->ilen);
820 g_error ("build_args_from_sig: TODO, allocate fregs: %d\n", margs->flen);
827 margs->iargs [0] = frame->stack_args->data.p;
831 for (int i = 0; i < sig->param_count; i++) {
832 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
834 case MONO_TYPE_BOOLEAN:
845 case MONO_TYPE_SZARRAY:
846 case MONO_TYPE_CLASS:
847 case MONO_TYPE_OBJECT:
848 case MONO_TYPE_STRING:
850 case MONO_TYPE_VALUETYPE:
851 case MONO_TYPE_GENERICINST:
852 margs->iargs [int_i] = frame->stack_args [i].data.p;
854 g_print ("build_args_from_sig: margs->iargs [%d]: %p (frame @ %d)\n", int_i, margs->iargs [int_i], i);
860 if (ptype == MONO_TYPE_R4)
861 * (float *) &(margs->fargs [int_f]) = (float) frame->stack_args [i].data.f;
863 margs->fargs [int_f] = frame->stack_args [i].data.f;
865 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);
870 g_error ("build_args_from_sig: not implemented yet (2): 0x%x\n", ptype);
874 switch (sig->ret->type) {
875 case MONO_TYPE_BOOLEAN:
886 case MONO_TYPE_SZARRAY:
887 case MONO_TYPE_CLASS:
888 case MONO_TYPE_OBJECT:
889 case MONO_TYPE_STRING:
891 case MONO_TYPE_VALUETYPE:
892 case MONO_TYPE_GENERICINST:
893 margs->retval = &(frame->retval->data.p);
897 margs->retval = &(frame->retval->data.p);
898 margs->is_float_ret = 1;
901 margs->retval = NULL;
904 g_error ("build_args_from_sig: ret type not implemented yet: 0x%x\n", sig->ret->type);
911 ves_pinvoke_method (MonoInvocation *frame, MonoMethodSignature *sig, MonoFuncV addr, gboolean string_ctor, ThreadContext *context)
914 MonoInvocation *old_frame = context->current_frame;
915 MonoInvocation *old_env_frame = context->env_frame;
916 jmp_buf *old_env = context->current_env;
920 context->current_frame = old_frame;
921 context->env_frame = old_env_frame;
922 context->current_env = old_env;
923 context->managed_code = 1;
928 context->env_frame = frame;
929 context->current_env = &env;
931 g_assert (!frame->runtime_method);
932 if (!mono_interp_enter_icall_trampoline) {
934 mono_interp_enter_icall_trampoline = mono_arch_get_enter_icall_trampoline (&info);
936 // mono_tramp_info_register (info, NULL);
939 MethodArguments *margs = build_args_from_sig (sig, frame);
941 g_print ("ICALL: mono_interp_enter_icall_trampoline = %p, addr = %p\n", mono_interp_enter_icall_trampoline, addr);
942 g_print ("margs(out): ilen=%d, flen=%d\n", margs->ilen, margs->flen);
945 context->current_frame = frame;
946 context->managed_code = 0;
949 * Push an LMF frame on the LMF stack
950 * to mark the transition to native code.
952 memset (&ext, 0, sizeof (ext));
953 ext.interp_exit = TRUE;
954 ext.interp_exit_data = frame;
956 mono_push_lmf (&ext);
958 mono_interp_enter_icall_trampoline (addr, margs);
960 mono_pop_lmf (&ext.lmf);
962 context->managed_code = 1;
963 /* domain can only be changed by native code */
964 context->domain = mono_domain_get ();
966 if (*mono_thread_interruption_request_flag ()) {
967 MonoException *exc = mono_thread_interruption_checkpoint ();
970 context->search_for_handler = 1;
974 if (!frame->ex && !MONO_TYPE_ISSTRUCT (sig->ret))
975 stackval_from_data (sig->ret, frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
977 context->current_frame = old_frame;
978 context->env_frame = old_env_frame;
979 context->current_env = old_env;
981 g_free (margs->iargs);
982 g_free (margs->fargs);
987 mono_interp_init_delegate (MonoDelegate *del)
991 /* shouldn't need a write barrier because we don't write a MonoObject into the field */
992 del->method = ((RuntimeMethod *) del->method_ptr)->method;
997 * runtime specifies that the implementation of the method is automatically
998 * provided by the runtime and is primarily used for the methods of delegates.
1001 ves_runtime_method (MonoInvocation *frame, ThreadContext *context)
1003 MonoMethod *method = frame->runtime_method->method;
1004 const char *name = method->name;
1005 MonoObject *obj = (MonoObject*) frame->stack_args->data.p;
1006 MonoObject *isinst_obj;
1009 mono_class_init (method->klass);
1011 if (method->klass == mono_defaults.array_class) {
1012 if (!strcmp (method->name, "UnsafeMov")) {
1013 /* TODO: layout checks */
1014 MonoType *mt = mono_method_signature (method)->ret;
1015 stackval_from_data (mt, frame->retval, (char *) frame->stack_args, FALSE);
1020 isinst_obj = mono_object_isinst_checked (obj, mono_defaults.array_class, &error);
1021 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1022 if (obj && isinst_obj) {
1023 if (*name == 'S' && (strcmp (name, "Set") == 0)) {
1024 ves_array_set (frame);
1027 if (*name == 'G' && (strcmp (name, "Get") == 0)) {
1028 ves_array_get (frame);
1033 g_error ("Don't know how to exec runtime method %s.%s::%s",
1034 method->klass->name_space, method->klass->name,
1039 dump_stack (stackval *stack, stackval *sp)
1041 stackval *s = stack;
1042 GString *str = g_string_new ("");
1045 return g_string_free (str, FALSE);
1048 g_string_append_printf (str, "[%p (%lld)] ", s->data.l, s->data.l);
1051 return g_string_free (str, FALSE);
1055 dump_stackval (GString *str, stackval *s, MonoType *type)
1057 switch (type->type) {
1064 case MONO_TYPE_CHAR:
1065 case MONO_TYPE_BOOLEAN:
1066 g_string_append_printf (str, "[%d] ", s->data.i);
1068 case MONO_TYPE_STRING:
1069 case MONO_TYPE_SZARRAY:
1070 case MONO_TYPE_CLASS:
1071 case MONO_TYPE_OBJECT:
1072 case MONO_TYPE_ARRAY:
1076 g_string_append_printf (str, "[%p] ", s->data.p);
1078 case MONO_TYPE_VALUETYPE:
1079 if (type->data.klass->enumtype)
1080 g_string_append_printf (str, "[%d] ", s->data.i);
1082 g_string_append_printf (str, "[vt:%p] ", s->data.p);
1086 g_string_append_printf (str, "[%g] ", s->data.f);
1091 GString *res = g_string_new ("");
1092 mono_type_get_desc (res, type, TRUE);
1093 g_string_append_printf (str, "[{%s} %lld/0x%0llx] ", res->str, s->data.l, s->data.l);
1094 g_string_free (res, TRUE);
1101 dump_args (MonoInvocation *inv)
1103 GString *str = g_string_new ("");
1105 MonoMethodSignature *signature = mono_method_signature (inv->runtime_method->method);
1107 if (signature->param_count == 0 && !signature->hasthis)
1108 return g_string_free (str, FALSE);
1110 if (signature->hasthis) {
1111 MonoMethod *method = inv->runtime_method->method;
1112 dump_stackval (str, inv->stack_args, &method->klass->byval_arg);
1115 for (i = 0; i < signature->param_count; ++i)
1116 dump_stackval (str, inv->stack_args + (!!signature->hasthis) + i, signature->params [i]);
1118 return g_string_free (str, FALSE);
1122 dump_retval (MonoInvocation *inv)
1124 GString *str = g_string_new ("");
1125 MonoType *ret = mono_method_signature (inv->runtime_method->method)->ret;
1127 if (ret->type != MONO_TYPE_VOID)
1128 dump_stackval (str, inv->retval, ret);
1130 return g_string_free (str, FALSE);
1134 dump_frame (MonoInvocation *inv)
1136 GString *str = g_string_new ("");
1141 for (i = 0; inv; inv = inv->parent) {
1142 if (inv->runtime_method != NULL) {
1143 MonoMethod *method = inv->runtime_method->method;
1147 const char * opname = "";
1149 gchar *source = NULL;
1153 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
1154 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) == 0) {
1155 MonoMethodHeader *hd = mono_method_get_header_checked (method, &error);
1156 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1160 opname = mono_interp_opname [*inv->ip];
1161 codep = inv->ip - inv->runtime_method->code;
1162 source = g_strdup_printf ("%s:%d // (TODO: proper stacktrace)", method->name, codep);
1167 MonoDebugSourceLocation *minfo = mono_debug_lookup_method (method);
1168 source = mono_debug_method_lookup_location (minfo, codep);
1172 args = dump_args (inv);
1173 name = mono_method_full_name (method, TRUE);
1175 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s) at %s\n", i, codep, opname, name, args, source);
1177 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s)\n", i, codep, opname, name, args);
1184 return g_string_free (str, FALSE);
1188 get_trace_ips (MonoDomain *domain, MonoInvocation *top)
1192 MonoInvocation *inv;
1195 for (i = 0, inv = top; inv; inv = inv->parent)
1196 if (inv->runtime_method != NULL)
1199 res = mono_array_new_checked (domain, mono_defaults.int_class, 2 * i, &error);
1200 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1202 for (i = 0, inv = top; inv; inv = inv->parent)
1203 if (inv->runtime_method != NULL) {
1204 mono_array_set (res, gpointer, i, inv->runtime_method);
1206 mono_array_set (res, gpointer, i, (gpointer)inv->ip);
1214 #define MYGUINT64_MAX 18446744073709551615ULL
1215 #define MYGINT64_MAX 9223372036854775807LL
1216 #define MYGINT64_MIN (-MYGINT64_MAX -1LL)
1218 #define MYGUINT32_MAX 4294967295U
1219 #define MYGINT32_MAX 2147483647
1220 #define MYGINT32_MIN (-MYGINT32_MAX -1)
1222 #define CHECK_ADD_OVERFLOW(a,b) \
1223 (gint32)(b) >= 0 ? (gint32)(MYGINT32_MAX) - (gint32)(b) < (gint32)(a) ? -1 : 0 \
1224 : (gint32)(MYGINT32_MIN) - (gint32)(b) > (gint32)(a) ? +1 : 0
1226 #define CHECK_SUB_OVERFLOW(a,b) \
1227 (gint32)(b) < 0 ? (gint32)(MYGINT32_MAX) + (gint32)(b) < (gint32)(a) ? -1 : 0 \
1228 : (gint32)(MYGINT32_MIN) + (gint32)(b) > (gint32)(a) ? +1 : 0
1230 #define CHECK_ADD_OVERFLOW_UN(a,b) \
1231 (guint32)(MYGUINT32_MAX) - (guint32)(b) < (guint32)(a) ? -1 : 0
1233 #define CHECK_SUB_OVERFLOW_UN(a,b) \
1234 (guint32)(a) < (guint32)(b) ? -1 : 0
1236 #define CHECK_ADD_OVERFLOW64(a,b) \
1237 (gint64)(b) >= 0 ? (gint64)(MYGINT64_MAX) - (gint64)(b) < (gint64)(a) ? -1 : 0 \
1238 : (gint64)(MYGINT64_MIN) - (gint64)(b) > (gint64)(a) ? +1 : 0
1240 #define CHECK_SUB_OVERFLOW64(a,b) \
1241 (gint64)(b) < 0 ? (gint64)(MYGINT64_MAX) + (gint64)(b) < (gint64)(a) ? -1 : 0 \
1242 : (gint64)(MYGINT64_MIN) + (gint64)(b) > (gint64)(a) ? +1 : 0
1244 #define CHECK_ADD_OVERFLOW64_UN(a,b) \
1245 (guint64)(MYGUINT64_MAX) - (guint64)(b) < (guint64)(a) ? -1 : 0
1247 #define CHECK_SUB_OVERFLOW64_UN(a,b) \
1248 (guint64)(a) < (guint64)(b) ? -1 : 0
1250 #if SIZEOF_VOID_P == 4
1251 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW(a,b)
1252 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW_UN(a,b)
1254 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW64(a,b)
1255 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW64_UN(a,b)
1258 /* Resolves to TRUE if the operands would overflow */
1259 #define CHECK_MUL_OVERFLOW(a,b) \
1260 ((gint32)(a) == 0) || ((gint32)(b) == 0) ? 0 : \
1261 (((gint32)(a) > 0) && ((gint32)(b) == -1)) ? FALSE : \
1262 (((gint32)(a) < 0) && ((gint32)(b) == -1)) ? (a == - MYGINT32_MAX) : \
1263 (((gint32)(a) > 0) && ((gint32)(b) > 0)) ? (gint32)(a) > ((MYGINT32_MAX) / (gint32)(b)) : \
1264 (((gint32)(a) > 0) && ((gint32)(b) < 0)) ? (gint32)(a) > ((MYGINT32_MIN) / (gint32)(b)) : \
1265 (((gint32)(a) < 0) && ((gint32)(b) > 0)) ? (gint32)(a) < ((MYGINT32_MIN) / (gint32)(b)) : \
1266 (gint32)(a) < ((MYGINT32_MAX) / (gint32)(b))
1268 #define CHECK_MUL_OVERFLOW_UN(a,b) \
1269 ((guint32)(a) == 0) || ((guint32)(b) == 0) ? 0 : \
1270 (guint32)(b) > ((MYGUINT32_MAX) / (guint32)(a))
1272 #define CHECK_MUL_OVERFLOW64(a,b) \
1273 ((gint64)(a) == 0) || ((gint64)(b) == 0) ? 0 : \
1274 (((gint64)(a) > 0) && ((gint64)(b) == -1)) ? FALSE : \
1275 (((gint64)(a) < 0) && ((gint64)(b) == -1)) ? (a == - MYGINT64_MAX) : \
1276 (((gint64)(a) > 0) && ((gint64)(b) > 0)) ? (gint64)(a) > ((MYGINT64_MAX) / (gint64)(b)) : \
1277 (((gint64)(a) > 0) && ((gint64)(b) < 0)) ? (gint64)(a) > ((MYGINT64_MIN) / (gint64)(b)) : \
1278 (((gint64)(a) < 0) && ((gint64)(b) > 0)) ? (gint64)(a) < ((MYGINT64_MIN) / (gint64)(b)) : \
1279 (gint64)(a) < ((MYGINT64_MAX) / (gint64)(b))
1281 #define CHECK_MUL_OVERFLOW64_UN(a,b) \
1282 ((guint64)(a) == 0) || ((guint64)(b) == 0) ? 0 : \
1283 (guint64)(b) > ((MYGUINT64_MAX) / (guint64)(a))
1285 #if SIZEOF_VOID_P == 4
1286 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW(a,b)
1287 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW_UN(a,b)
1289 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW64(a,b)
1290 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW64_UN(a,b)
1294 mono_interp_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc, MonoError *error)
1296 MonoInvocation frame;
1297 ThreadContext * volatile context = mono_native_tls_get_value (thread_context_id);
1298 MonoObject *retval = NULL;
1299 MonoMethodSignature *sig = mono_method_signature (method);
1300 MonoClass *klass = mono_class_from_mono_type (sig->ret);
1301 int i, type, isobject = 0;
1304 stackval *args = alloca (sizeof (stackval) * (sig->param_count + !!sig->hasthis));
1305 ThreadContext context_struct;
1306 MonoInvocation *old_frame = NULL;
1316 if (context != &context_struct) {
1317 context->domain = mono_domain_get ();
1318 context->current_frame = old_frame;
1319 context->managed_code = 0;
1321 mono_native_tls_set_value (thread_context_id, NULL);
1323 *exc = (MonoObject *)frame.ex;
1327 if (context == NULL) {
1328 context = &context_struct;
1329 memset (context, 0, sizeof (ThreadContext));
1330 context_struct.base_frame = &frame;
1331 context_struct.env_frame = &frame;
1332 context_struct.current_env = &env;
1333 mono_native_tls_set_value (thread_context_id, context);
1336 old_frame = context->current_frame;
1338 context->domain = mono_domain_get ();
1340 switch (sig->ret->type) {
1341 case MONO_TYPE_VOID:
1343 case MONO_TYPE_STRING:
1344 case MONO_TYPE_OBJECT:
1345 case MONO_TYPE_CLASS:
1346 case MONO_TYPE_ARRAY:
1347 case MONO_TYPE_SZARRAY:
1350 case MONO_TYPE_VALUETYPE:
1351 retval = mono_object_new_checked (context->domain, klass, error);
1352 ret = mono_object_unbox (retval);
1353 if (!sig->ret->data.klass->enumtype)
1354 result.data.vt = ret;
1356 result.data.vt = alloca (mono_class_instance_size (klass));
1358 case MONO_TYPE_GENERICINST:
1359 if (!MONO_TYPE_IS_REFERENCE (sig->ret)) {
1360 retval = mono_object_new_checked (context->domain, klass, error);
1361 ret = mono_object_unbox (retval);
1362 if (!sig->ret->data.klass->enumtype)
1363 result.data.vt = ret;
1365 result.data.vt = alloca (mono_class_instance_size (klass));
1372 retval = mono_object_new_checked (context->domain, mono_defaults.int_class, error);
1373 ret = mono_object_unbox (retval);
1376 retval = mono_object_new_checked (context->domain, klass, error);
1377 ret = mono_object_unbox (retval);
1382 args [0].data.p = obj;
1384 for (i = 0; i < sig->param_count; ++i) {
1385 int a_index = i + !!sig->hasthis;
1386 if (sig->params [i]->byref) {
1387 args [a_index].data.p = params [i];
1390 type = sig->params [i]->type;
1395 case MONO_TYPE_BOOLEAN:
1396 args [a_index].data.i = *(MonoBoolean*)params [i];
1400 case MONO_TYPE_CHAR:
1401 args [a_index].data.i = *(gint16*)params [i];
1403 #if SIZEOF_VOID_P == 4
1404 case MONO_TYPE_U: /* use VAL_POINTER? */
1409 args [a_index].data.i = *(gint32*)params [i];
1411 #if SIZEOF_VOID_P == 8
1417 args [a_index].data.l = *(gint64*)params [i];
1420 args [a_index].data.f = *(gfloat *) params [i];
1423 args [a_index].data.f = *(gdouble *) params [i];
1425 case MONO_TYPE_VALUETYPE:
1426 if (sig->params [i]->data.klass->enumtype) {
1427 type = mono_class_enum_basetype (sig->params [i]->data.klass)->type;
1430 args [a_index].data.p = params [i];
1433 case MONO_TYPE_STRING:
1435 case MONO_TYPE_CLASS:
1436 case MONO_TYPE_ARRAY:
1437 case MONO_TYPE_SZARRAY:
1438 case MONO_TYPE_OBJECT:
1439 case MONO_TYPE_GENERICINST:
1440 args [a_index].data.p = params [i];
1443 g_error ("type 0x%x not handled in runtime invoke", sig->params [i]->type);
1447 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
1448 method = mono_marshal_get_native_wrapper (method, FALSE, FALSE);
1449 INIT_FRAME (&frame,context->current_frame,args,&result,mono_get_root_domain (),method,error);
1451 frame.invoke_trap = 1;
1452 context->managed_code = 1;
1453 ves_exec_method_with_context (&frame, context, NULL, NULL, -1);
1454 context->managed_code = 0;
1455 if (context == &context_struct)
1456 mono_native_tls_set_value (thread_context_id, NULL);
1458 context->current_frame = old_frame;
1459 if (frame.ex != NULL) {
1461 *exc = (MonoObject*) frame.ex;
1464 if (context->current_env != NULL) {
1465 context->env_frame->ex = frame.ex;
1466 longjmp(*context->current_env, 1);
1469 printf("dropped exception...\n");
1471 if (sig->ret->type == MONO_TYPE_VOID && !method->string_ctor)
1473 if (isobject || method->string_ctor)
1474 return result.data.p;
1475 stackval_to_data (sig->ret, &result, ret, sig->pinvoke);
1480 RuntimeMethod *rmethod;
1484 gpointer *many_args;
1487 /* Main function for entering the interpreter from compiled code */
1489 interp_entry (InterpEntryData *data)
1491 MonoInvocation frame;
1492 RuntimeMethod *rmethod = data->rmethod;
1493 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
1494 ThreadContext context_struct;
1495 MonoInvocation *old_frame;
1499 MonoMethodSignature *sig;
1503 method = rmethod->method;
1504 sig = mono_method_signature (method);
1506 // FIXME: Optimize this
1508 //printf ("%s\n", mono_method_full_name (method, 1));
1511 if (context == NULL) {
1512 context = &context_struct;
1513 memset (context, 0, sizeof (ThreadContext));
1514 context_struct.base_frame = &frame;
1515 context_struct.env_frame = &frame;
1516 mono_native_tls_set_value (thread_context_id, context);
1519 old_frame = context->current_frame;
1520 context->domain = mono_domain_get ();
1522 args = alloca (sizeof (stackval) * (sig->param_count + (sig->hasthis ? 1 : 0)));
1524 args [0].data.p = data->this_arg;
1527 if (data->many_args)
1528 params = data->many_args;
1530 params = data->args;
1531 for (i = 0; i < sig->param_count; ++i) {
1532 int a_index = i + (sig->hasthis ? 1 : 0);
1533 if (sig->params [i]->byref) {
1534 args [a_index].data.p = params [i];
1537 type = rmethod->param_types [i];
1538 switch (type->type) {
1541 args [a_index].data.i = *(MonoBoolean*)params [i];
1545 args [a_index].data.i = *(gint16*)params [i];
1548 #if SIZEOF_VOID_P == 4
1549 args [a_index].data.p = GINT_TO_POINTER (*(guint32*)params [i]);
1551 args [a_index].data.p = GINT_TO_POINTER (*(guint64*)params [i]);
1555 #if SIZEOF_VOID_P == 4
1556 args [a_index].data.p = GINT_TO_POINTER (*(gint32*)params [i]);
1558 args [a_index].data.p = GINT_TO_POINTER (*(gint64*)params [i]);
1562 args [a_index].data.i = *(guint32*)params [i];
1565 args [a_index].data.i = *(gint32*)params [i];
1568 args [a_index].data.l = *(guint64*)params [i];
1571 args [a_index].data.l = *(gint64*)params [i];
1574 case MONO_TYPE_OBJECT:
1575 args [a_index].data.p = *(MonoObject**)params [i];
1577 case MONO_TYPE_VALUETYPE:
1578 args [a_index].data.p = params [i];
1580 case MONO_TYPE_GENERICINST:
1581 if (MONO_TYPE_IS_REFERENCE (type))
1582 args [a_index].data.p = params [i];
1584 args [a_index].data.vt = params [i];
1587 printf ("%s\n", mono_type_full_name (sig->params [i]));
1593 init_frame (&frame, NULL, data->rmethod, args, &result);
1594 context->managed_code = 1;
1596 type = rmethod->rtype;
1597 switch (type->type) {
1598 case MONO_TYPE_GENERICINST:
1599 if (!MONO_TYPE_IS_REFERENCE (type))
1600 frame.retval->data.vt = data->res;
1602 case MONO_TYPE_VALUETYPE:
1603 frame.retval->data.vt = data->res;
1609 ves_exec_method_with_context (&frame, context, NULL, NULL, -1);
1610 context->managed_code = 0;
1611 if (context == &context_struct)
1612 mono_native_tls_set_value (thread_context_id, NULL);
1614 context->current_frame = old_frame;
1617 g_assert (frame.ex == NULL);
1619 type = rmethod->rtype;
1620 switch (type->type) {
1621 case MONO_TYPE_VOID:
1624 *(gint8*)data->res = frame.retval->data.i;
1627 *(guint8*)data->res = frame.retval->data.i;
1630 *(gint16*)data->res = frame.retval->data.i;
1633 *(guint16*)data->res = frame.retval->data.i;
1636 *(gint32*)data->res = frame.retval->data.i;
1639 *(guint64*)data->res = frame.retval->data.i;
1642 *(gint64*)data->res = frame.retval->data.i;
1645 *(guint64*)data->res = frame.retval->data.i;
1648 #if SIZEOF_VOID_P == 8
1649 *(gint64*)data->res = (gint64)frame.retval->data.p;
1651 *(gint32*)data->res = (gint32)frame.retval->data.p;
1655 #if SIZEOF_VOID_P == 8
1656 *(guint64*)data->res = (guint64)frame.retval->data.p;
1658 *(guint32*)data->res = (guint32)frame.retval->data.p;
1661 case MONO_TYPE_OBJECT:
1662 /* No need for a write barrier */
1663 *(MonoObject**)data->res = (MonoObject*)frame.retval->data.p;
1665 case MONO_TYPE_GENERICINST:
1666 if (MONO_TYPE_IS_REFERENCE (type)) {
1667 *(MonoObject**)data->res = *(MonoObject**)frame.retval->data.p;
1669 /* Already set before the call */
1672 case MONO_TYPE_VALUETYPE:
1673 /* Already set before the call */
1676 printf ("%s\n", mono_type_full_name (sig->ret));
1683 do_icall (ThreadContext *context, int op, stackval *sp, gpointer ptr)
1685 MonoInvocation *old_frame = context->current_frame;
1686 MonoInvocation *old_env_frame = context->env_frame;
1687 jmp_buf *old_env = context->current_env;
1691 context->current_frame = old_frame;
1692 context->env_frame = old_env_frame;
1693 context->current_env = old_env;
1694 context->managed_code = 1;
1698 context->env_frame = context->current_frame;
1699 context->current_env = &env;
1700 context->managed_code = 0;
1703 case MINT_ICALL_V_V: {
1704 void (*func)() = ptr;
1708 case MINT_ICALL_V_P: {
1709 gpointer (*func)() = ptr;
1711 sp [-1].data.p = func ();
1714 case MINT_ICALL_P_V: {
1715 void (*func)(gpointer) = ptr;
1716 func (sp [-1].data.p);
1720 case MINT_ICALL_P_P: {
1721 gpointer (*func)(gpointer) = ptr;
1722 sp [-1].data.p = func (sp [-1].data.p);
1725 case MINT_ICALL_PP_V: {
1726 void (*func)(gpointer,gpointer) = ptr;
1728 func (sp [0].data.p, sp [1].data.p);
1731 case MINT_ICALL_PI_V: {
1732 void (*func)(gpointer,int) = ptr;
1734 func (sp [0].data.p, sp [1].data.i);
1737 case MINT_ICALL_PP_P: {
1738 gpointer (*func)(gpointer,gpointer) = ptr;
1740 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.p);
1743 case MINT_ICALL_PI_P: {
1744 gpointer (*func)(gpointer,int) = ptr;
1746 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.i);
1749 case MINT_ICALL_PPP_V: {
1750 void (*func)(gpointer,gpointer,gpointer) = ptr;
1752 func (sp [0].data.p, sp [1].data.p, sp [2].data.p);
1755 case MINT_ICALL_PPI_V: {
1756 void (*func)(gpointer,gpointer,int) = ptr;
1758 func (sp [0].data.p, sp [1].data.p, sp [2].data.i);
1762 g_assert_not_reached ();
1765 context->env_frame = old_env_frame;
1766 context->current_env = old_env;
1772 * These functions are the entry points into the interpreter from compiled code.
1773 * They are called by the interp_in wrappers. They have the following signature:
1774 * void (<optional this_arg>, <optional retval pointer>, <arg1>, ..., <argn>, <method ptr>)
1775 * They pack up their arguments into an InterpEntryData structure and call interp_entry ().
1776 * It would be possible for the wrappers to pack up the arguments etc, but that would make them bigger, and there are
1777 * more wrappers then these functions.
1778 * this/static * ret/void * 16 arguments -> 64 functions.
1781 #define MAX_INTERP_ENTRY_ARGS 8
1783 #define INTERP_ENTRY_BASE(_method, _this_arg, _res) \
1784 InterpEntryData data; \
1785 (data).rmethod = (_method); \
1786 (data).res = (_res); \
1787 (data).this_arg = (_this_arg); \
1788 (data).many_args = NULL;
1790 #define INTERP_ENTRY0(_this_arg, _res, _method) { \
1791 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
1792 interp_entry (&data); \
1794 #define INTERP_ENTRY1(_this_arg, _res, _method) { \
1795 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
1796 (data).args [0] = arg1; \
1797 interp_entry (&data); \
1799 #define INTERP_ENTRY2(_this_arg, _res, _method) { \
1800 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
1801 (data).args [0] = arg1; \
1802 (data).args [1] = arg2; \
1803 interp_entry (&data); \
1805 #define INTERP_ENTRY3(_this_arg, _res, _method) { \
1806 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
1807 (data).args [0] = arg1; \
1808 (data).args [1] = arg2; \
1809 (data).args [2] = arg3; \
1810 interp_entry (&data); \
1812 #define INTERP_ENTRY4(_this_arg, _res, _method) { \
1813 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
1814 (data).args [0] = arg1; \
1815 (data).args [1] = arg2; \
1816 (data).args [2] = arg3; \
1817 (data).args [3] = arg4; \
1818 interp_entry (&data); \
1820 #define INTERP_ENTRY5(_this_arg, _res, _method) { \
1821 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
1822 (data).args [0] = arg1; \
1823 (data).args [1] = arg2; \
1824 (data).args [2] = arg3; \
1825 (data).args [3] = arg4; \
1826 (data).args [4] = arg5; \
1827 interp_entry (&data); \
1829 #define INTERP_ENTRY6(_this_arg, _res, _method) { \
1830 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
1831 (data).args [0] = arg1; \
1832 (data).args [1] = arg2; \
1833 (data).args [2] = arg3; \
1834 (data).args [3] = arg4; \
1835 (data).args [4] = arg5; \
1836 (data).args [5] = arg6; \
1837 interp_entry (&data); \
1839 #define INTERP_ENTRY7(_this_arg, _res, _method) { \
1840 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
1841 (data).args [0] = arg1; \
1842 (data).args [1] = arg2; \
1843 (data).args [2] = arg3; \
1844 (data).args [3] = arg4; \
1845 (data).args [4] = arg5; \
1846 (data).args [5] = arg6; \
1847 (data).args [6] = arg7; \
1848 interp_entry (&data); \
1850 #define INTERP_ENTRY8(_this_arg, _res, _method) { \
1851 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
1852 (data).args [0] = arg1; \
1853 (data).args [1] = arg2; \
1854 (data).args [2] = arg3; \
1855 (data).args [3] = arg4; \
1856 (data).args [4] = arg5; \
1857 (data).args [5] = arg6; \
1858 (data).args [6] = arg7; \
1859 (data).args [7] = arg8; \
1860 interp_entry (&data); \
1863 #define ARGLIST0 RuntimeMethod *rmethod
1864 #define ARGLIST1 gpointer arg1, RuntimeMethod *rmethod
1865 #define ARGLIST2 gpointer arg1, gpointer arg2, RuntimeMethod *rmethod
1866 #define ARGLIST3 gpointer arg1, gpointer arg2, gpointer arg3, RuntimeMethod *rmethod
1867 #define ARGLIST4 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, RuntimeMethod *rmethod
1868 #define ARGLIST5 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, RuntimeMethod *rmethod
1869 #define ARGLIST6 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, gpointer arg6, RuntimeMethod *rmethod
1870 #define ARGLIST7 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, gpointer arg6, gpointer arg7, RuntimeMethod *rmethod
1871 #define ARGLIST8 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, gpointer arg6, gpointer arg7, gpointer arg8, RuntimeMethod *rmethod
1873 static void interp_entry_static_0 (ARGLIST0) INTERP_ENTRY0 (NULL, NULL, rmethod)
1874 static void interp_entry_static_1 (ARGLIST1) INTERP_ENTRY1 (NULL, NULL, rmethod)
1875 static void interp_entry_static_2 (ARGLIST2) INTERP_ENTRY2 (NULL, NULL, rmethod)
1876 static void interp_entry_static_3 (ARGLIST3) INTERP_ENTRY3 (NULL, NULL, rmethod)
1877 static void interp_entry_static_4 (ARGLIST4) INTERP_ENTRY4 (NULL, NULL, rmethod)
1878 static void interp_entry_static_5 (ARGLIST5) INTERP_ENTRY5 (NULL, NULL, rmethod)
1879 static void interp_entry_static_6 (ARGLIST6) INTERP_ENTRY6 (NULL, NULL, rmethod)
1880 static void interp_entry_static_7 (ARGLIST7) INTERP_ENTRY7 (NULL, NULL, rmethod)
1881 static void interp_entry_static_8 (ARGLIST8) INTERP_ENTRY8 (NULL, NULL, rmethod)
1882 static void interp_entry_static_ret_0 (gpointer res, ARGLIST0) INTERP_ENTRY0 (NULL, res, rmethod)
1883 static void interp_entry_static_ret_1 (gpointer res, ARGLIST1) INTERP_ENTRY1 (NULL, res, rmethod)
1884 static void interp_entry_static_ret_2 (gpointer res, ARGLIST2) INTERP_ENTRY2 (NULL, res, rmethod)
1885 static void interp_entry_static_ret_3 (gpointer res, ARGLIST3) INTERP_ENTRY3 (NULL, res, rmethod)
1886 static void interp_entry_static_ret_4 (gpointer res, ARGLIST4) INTERP_ENTRY4 (NULL, res, rmethod)
1887 static void interp_entry_static_ret_5 (gpointer res, ARGLIST5) INTERP_ENTRY5 (NULL, res, rmethod)
1888 static void interp_entry_static_ret_6 (gpointer res, ARGLIST6) INTERP_ENTRY6 (NULL, res, rmethod)
1889 static void interp_entry_static_ret_7 (gpointer res, ARGLIST7) INTERP_ENTRY7 (NULL, res, rmethod)
1890 static void interp_entry_static_ret_8 (gpointer res, ARGLIST8) INTERP_ENTRY8 (NULL, res, rmethod)
1891 static void interp_entry_instance_0 (gpointer this_arg, ARGLIST0) INTERP_ENTRY0 (this_arg, NULL, rmethod)
1892 static void interp_entry_instance_1 (gpointer this_arg, ARGLIST1) INTERP_ENTRY1 (this_arg, NULL, rmethod)
1893 static void interp_entry_instance_2 (gpointer this_arg, ARGLIST2) INTERP_ENTRY2 (this_arg, NULL, rmethod)
1894 static void interp_entry_instance_3 (gpointer this_arg, ARGLIST3) INTERP_ENTRY3 (this_arg, NULL, rmethod)
1895 static void interp_entry_instance_4 (gpointer this_arg, ARGLIST4) INTERP_ENTRY4 (this_arg, NULL, rmethod)
1896 static void interp_entry_instance_5 (gpointer this_arg, ARGLIST5) INTERP_ENTRY5 (this_arg, NULL, rmethod)
1897 static void interp_entry_instance_6 (gpointer this_arg, ARGLIST6) INTERP_ENTRY6 (this_arg, NULL, rmethod)
1898 static void interp_entry_instance_7 (gpointer this_arg, ARGLIST7) INTERP_ENTRY7 (this_arg, NULL, rmethod)
1899 static void interp_entry_instance_8 (gpointer this_arg, ARGLIST8) INTERP_ENTRY8 (this_arg, NULL, rmethod)
1900 static void interp_entry_instance_ret_0 (gpointer this_arg, gpointer res, ARGLIST0) INTERP_ENTRY0 (this_arg, res, rmethod)
1901 static void interp_entry_instance_ret_1 (gpointer this_arg, gpointer res, ARGLIST1) INTERP_ENTRY1 (this_arg, res, rmethod)
1902 static void interp_entry_instance_ret_2 (gpointer this_arg, gpointer res, ARGLIST2) INTERP_ENTRY2 (this_arg, res, rmethod)
1903 static void interp_entry_instance_ret_3 (gpointer this_arg, gpointer res, ARGLIST3) INTERP_ENTRY3 (this_arg, res, rmethod)
1904 static void interp_entry_instance_ret_4 (gpointer this_arg, gpointer res, ARGLIST4) INTERP_ENTRY4 (this_arg, res, rmethod)
1905 static void interp_entry_instance_ret_5 (gpointer this_arg, gpointer res, ARGLIST5) INTERP_ENTRY5 (this_arg, res, rmethod)
1906 static void interp_entry_instance_ret_6 (gpointer this_arg, gpointer res, ARGLIST6) INTERP_ENTRY6 (this_arg, res, rmethod)
1907 static void interp_entry_instance_ret_7 (gpointer this_arg, gpointer res, ARGLIST7) INTERP_ENTRY6 (this_arg, res, rmethod)
1908 static void interp_entry_instance_ret_8 (gpointer this_arg, gpointer res, ARGLIST8) INTERP_ENTRY6 (this_arg, res, rmethod)
1910 #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
1912 gpointer entry_funcs_static [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (static) };
1913 gpointer entry_funcs_static_ret [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (static_ret) };
1914 gpointer entry_funcs_instance [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (instance) };
1915 gpointer entry_funcs_instance_ret [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (instance_ret) };
1917 /* General version for methods with more than MAX_INTERP_ENTRY_ARGS arguments */
1919 interp_entry_general (gpointer this_arg, gpointer res, gpointer *args, gpointer rmethod)
1921 INTERP_ENTRY_BASE (rmethod, this_arg, res);
1922 data.many_args = args;
1923 interp_entry (&data);
1927 * mono_interp_create_method_pointer:
1929 * Return a function pointer which can be used to call METHOD using the
1930 * interpreter. Return NULL for methods which are not supported.
1933 mono_interp_create_method_pointer (MonoMethod *method, MonoError *error)
1936 MonoMethodSignature *sig = mono_method_signature (method);
1937 MonoMethod *wrapper;
1938 RuntimeMethod *rmethod;
1940 /* HACK: method_ptr of delegate should point to a runtime method*/
1941 if (method->wrapper_type && method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
1942 return mono_interp_get_runtime_method (mono_domain_get (), method, error);
1944 rmethod = mono_interp_get_runtime_method (mono_domain_get (), method, error);
1945 if (rmethod->jit_entry)
1946 return rmethod->jit_entry;
1947 wrapper = mini_get_interp_in_wrapper (sig);
1949 gpointer jit_wrapper = mono_jit_compile_method_jit_only (wrapper, error);
1950 mono_error_assert_ok (error);
1952 //printf ("%s %s\n", mono_method_full_name (method, 1), mono_method_full_name (wrapper, 1));
1953 gpointer entry_func;
1954 if (sig->param_count > MAX_INTERP_ENTRY_ARGS) {
1955 entry_func = interp_entry_general;
1956 } else if (sig->hasthis) {
1957 if (sig->ret->type == MONO_TYPE_VOID)
1958 entry_func = entry_funcs_instance [sig->param_count];
1960 entry_func = entry_funcs_instance_ret [sig->param_count];
1962 if (sig->ret->type == MONO_TYPE_VOID)
1963 entry_func = entry_funcs_static [sig->param_count];
1965 entry_func = entry_funcs_static_ret [sig->param_count];
1967 g_assert (entry_func);
1969 /* This is the argument passed to the interp_in wrapper by the static rgctx trampoline */
1970 MonoFtnDesc *ftndesc = g_new0 (MonoFtnDesc, 1);
1971 ftndesc->addr = entry_func;
1972 ftndesc->arg = rmethod;
1973 mono_error_assert_ok (error);
1976 * The wrapper is called by compiled code, which doesn't pass the extra argument, so we pass it in the
1977 * rgctx register using a trampoline.
1981 g_assert (!mono_aot_only);
1982 addr = mono_arch_get_static_rgctx_trampoline (ftndesc, jit_wrapper);
1984 mono_memory_barrier ();
1985 rmethod->jit_entry = addr;
1991 static int opcode_counts[512];
1993 #define COUNT_OP(op) opcode_counts[op]++
1995 #define COUNT_OP(op)
1999 #define DUMP_INSTR() \
2000 if (tracing > 1) { \
2002 if (sp > frame->stack) { \
2003 ins = dump_stack (frame->stack, sp); \
2005 ins = g_strdup (""); \
2009 char *mn = mono_method_full_name (frame->runtime_method->method, FALSE); \
2010 g_print ("(%p) %s -> ", mono_thread_internal_current (), mn); \
2012 mono_interp_dis_mintop(rtm->code, ip); \
2013 g_print ("\t%d:%s\n", vt_sp - vtalloc, ins); \
2017 #define DUMP_INSTR()
2021 #define USE_COMPUTED_GOTO 1
2023 #if USE_COMPUTED_GOTO
2024 #define MINT_IN_SWITCH(op) COUNT_OP(op); goto *in_labels[op];
2025 #define MINT_IN_CASE(x) LAB_ ## x:
2027 #define MINT_IN_BREAK if (tracing > 1) goto main_loop; else { COUNT_OP(*ip); goto *in_labels[*ip]; }
2029 #define MINT_IN_BREAK { COUNT_OP(*ip); goto *in_labels[*ip]; }
2031 #define MINT_IN_DEFAULT mint_default: if (0) goto mint_default; /* make gcc shut up */
2033 #define MINT_IN_SWITCH(op) switch (op)
2034 #define MINT_IN_CASE(x) case x:
2035 #define MINT_IN_BREAK break
2036 #define MINT_IN_DEFAULT default:
2040 * If EXIT_AT_FINALLY is not -1, exit after exiting the finally clause with that index.
2043 ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, unsigned short *start_with_ip, MonoException *filter_exception, int exit_at_finally)
2045 MonoInvocation child_frame;
2046 GSList *finally_ips = NULL;
2047 const unsigned short *endfinally_ip = NULL;
2048 const unsigned short *ip = NULL;
2049 register stackval *sp;
2052 gint tracing = global_tracing;
2053 unsigned char *vtalloc;
2056 unsigned char *vt_sp;
2057 unsigned char *locals;
2059 MonoObject *o = NULL;
2061 #if USE_COMPUTED_GOTO
2062 static void *in_labels[] = {
2063 #define OPDEF(a,b,c,d) \
2065 #include "mintops.def"
2070 frame->ex_handler = NULL;
2072 context->current_frame = frame;
2075 debug_enter (frame, &tracing);
2078 if (!frame->runtime_method->transformed) {
2079 context->managed_code = 0;
2081 char *mn = mono_method_full_name (frame->runtime_method->method, TRUE);
2082 g_print ("(%p) Transforming %s\n", mono_thread_internal_current (), mn);
2085 frame->ex = mono_interp_transform_method (frame->runtime_method, context);
2086 context->managed_code = 1;
2094 rtm = frame->runtime_method;
2095 if (!start_with_ip ) {
2096 frame->args = alloca (rtm->alloca_size);
2097 memset (frame->args, 0, rtm->alloca_size);
2103 sp = frame->stack = (stackval *) ((char *) frame->args + rtm->args_size);
2104 vt_sp = (unsigned char *) sp + rtm->stack_size;
2108 locals = (unsigned char *) vt_sp + rtm->vt_stack_size;
2109 child_frame.parent = frame;
2111 if (filter_exception) {
2112 sp->data.p = filter_exception;
2117 * using while (ip < end) may result in a 15% performance drop,
2118 * but it may be useful for debug
2122 /* g_assert (sp >= frame->stack); */
2123 /* g_assert(vt_sp - vtalloc <= rtm->vt_stack_size); */
2125 MINT_IN_SWITCH (*ip) {
2126 MINT_IN_CASE(MINT_INITLOCALS)
2127 memset (locals, 0, rtm->locals_size);
2130 MINT_IN_CASE(MINT_NOP)
2133 MINT_IN_CASE(MINT_BREAK)
2135 G_BREAKPOINT (); /* this is not portable... */
2137 MINT_IN_CASE(MINT_LDNULL)
2142 MINT_IN_CASE(MINT_VTRESULT) {
2143 int ret_size = * (guint16 *)(ip + 1);
2144 unsigned char *ret_vt_sp = vt_sp;
2145 vt_sp -= READ32(ip + 2);
2147 memmove (vt_sp, ret_vt_sp, ret_size);
2148 sp [-1].data.p = vt_sp;
2149 vt_sp += (ret_size + 7) & ~7;
2154 #define LDC(n) do { sp->data.i = (n); ++ip; ++sp; } while (0)
2155 MINT_IN_CASE(MINT_LDC_I4_M1)
2158 MINT_IN_CASE(MINT_LDC_I4_0)
2161 MINT_IN_CASE(MINT_LDC_I4_1)
2164 MINT_IN_CASE(MINT_LDC_I4_2)
2167 MINT_IN_CASE(MINT_LDC_I4_3)
2170 MINT_IN_CASE(MINT_LDC_I4_4)
2173 MINT_IN_CASE(MINT_LDC_I4_5)
2176 MINT_IN_CASE(MINT_LDC_I4_6)
2179 MINT_IN_CASE(MINT_LDC_I4_7)
2182 MINT_IN_CASE(MINT_LDC_I4_8)
2185 MINT_IN_CASE(MINT_LDC_I4_S)
2186 sp->data.i = *(const short *)(ip + 1);
2190 MINT_IN_CASE(MINT_LDC_I4)
2192 sp->data.i = READ32 (ip);
2196 MINT_IN_CASE(MINT_LDC_I8)
2198 sp->data.l = READ64 (ip);
2202 MINT_IN_CASE(MINT_LDC_R4) {
2206 sp->data.f = * (float *)&val;
2211 MINT_IN_CASE(MINT_LDC_R8)
2212 sp->data.l = READ64 (ip + 1); /* note union usage */
2216 MINT_IN_CASE(MINT_DUP)
2221 MINT_IN_CASE(MINT_DUP_VT)
2222 i32 = READ32 (ip + 1);
2224 memcpy(sp->data.p, sp [-1].data.p, i32);
2225 vt_sp += (i32 + 7) & ~7;
2229 MINT_IN_CASE(MINT_POP) {
2230 guint16 u16 = (* (guint16 *)(ip + 1)) + 1;
2232 memmove (sp - u16, sp - 1, (u16 - 1) * sizeof (stackval));
2237 MINT_IN_CASE(MINT_JMP) {
2238 RuntimeMethod *new_method = rtm->data_items [* (guint16 *)(ip + 1)];
2239 if (!new_method->transformed) {
2241 frame->ex = mono_interp_transform_method (new_method, context);
2246 if (new_method->alloca_size > rtm->alloca_size)
2247 g_error ("MINT_JMP to method which needs more stack space (%d > %d)", new_method->alloca_size, rtm->alloca_size);
2248 rtm = frame->runtime_method = new_method;
2249 vt_sp = (unsigned char *) sp + rtm->stack_size;
2253 locals = vt_sp + rtm->vt_stack_size;
2254 ip = rtm->new_body_start; /* bypass storing input args from callers frame */
2257 MINT_IN_CASE(MINT_CALLI) {
2258 MonoMethodSignature *csignature;
2259 stackval *endsp = sp;
2263 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
2267 child_frame.runtime_method = sp->data.p;
2270 child_frame.retval = sp;
2271 /* decrement by the actual number of args */
2272 sp -= csignature->param_count;
2273 if (csignature->hasthis)
2275 child_frame.stack_args = sp;
2277 /* `this' can be NULL for string:.ctor */
2278 if (csignature->hasthis && sp->data.p && mono_object_is_transparent_proxy (sp->data.p)) {
2279 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
2280 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2281 } else if (child_frame.runtime_method->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
2282 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_native_wrapper (child_frame.runtime_method->method, FALSE, FALSE), &error);
2283 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2286 if (csignature->hasthis) {
2287 MonoObject *this_arg = sp->data.p;
2289 if (this_arg->vtable->klass->valuetype) {
2290 gpointer *unboxed = mono_object_unbox (this_arg);
2291 sp [0].data.p = unboxed;
2295 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2297 context->current_frame = frame;
2299 if (context->has_resume_state) {
2300 if (frame == context->handler_frame)
2301 SET_RESUME_STATE (context);
2306 if (child_frame.ex) {
2308 * An exception occurred, need to run finally, fault and catch handlers..
2310 frame->ex = child_frame.ex;
2311 goto handle_finally;
2314 /* need to handle typedbyref ... */
2315 if (csignature->ret->type != MONO_TYPE_VOID) {
2321 MINT_IN_CASE(MINT_CALLI_NAT) {
2322 MonoMethodSignature *csignature;
2323 stackval *endsp = sp;
2324 unsigned char *code = NULL;
2328 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
2333 child_frame.runtime_method = NULL;
2336 child_frame.retval = sp;
2337 /* decrement by the actual number of args */
2338 sp -= csignature->param_count;
2339 if (csignature->hasthis)
2341 child_frame.stack_args = sp;
2342 ves_pinvoke_method (&child_frame, csignature, (MonoFuncV) code, FALSE, context);
2344 context->current_frame = frame;
2346 if (context->has_resume_state) {
2347 if (frame == context->handler_frame)
2348 SET_RESUME_STATE (context);
2353 if (child_frame.ex) {
2355 * An exception occurred, need to run finally, fault and catch handlers..
2357 frame->ex = child_frame.ex;
2358 if (context->search_for_handler) {
2359 context->search_for_handler = 0;
2360 goto handle_exception;
2362 goto handle_finally;
2365 /* need to handle typedbyref ... */
2366 if (csignature->ret->type != MONO_TYPE_VOID) {
2372 MINT_IN_CASE(MINT_CALL) {
2373 stackval *endsp = sp;
2377 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
2380 child_frame.retval = sp;
2381 /* decrement by the actual number of args */
2382 sp -= child_frame.runtime_method->param_count;
2383 if (child_frame.runtime_method->hasthis)
2385 child_frame.stack_args = sp;
2387 /* `this' can be NULL for string:.ctor */
2388 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->method->klass->valuetype && sp->data.p && mono_object_is_transparent_proxy (sp->data.p)) {
2389 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
2390 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2393 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2395 context->current_frame = frame;
2397 if (context->has_resume_state) {
2398 if (frame == context->handler_frame)
2399 SET_RESUME_STATE (context);
2404 if (child_frame.ex) {
2406 * An exception occurred, need to run finally, fault and catch handlers..
2408 frame->ex = child_frame.ex;
2409 goto handle_exception;;
2412 /* need to handle typedbyref ... */
2417 MINT_IN_CASE(MINT_VCALL) {
2420 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
2424 child_frame.retval = sp;
2425 /* decrement by the actual number of args */
2426 sp -= child_frame.runtime_method->param_count;
2427 if (child_frame.runtime_method->hasthis) {
2429 MonoObject *this_arg = sp->data.p;
2431 THROW_EX (mono_get_exception_null_reference(), ip - 2);
2433 child_frame.stack_args = sp;
2435 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->method->klass->valuetype && mono_object_is_transparent_proxy (sp->data.p)) {
2436 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
2437 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2440 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2442 context->current_frame = frame;
2444 if (context->has_resume_state) {
2445 if (frame == context->handler_frame)
2446 SET_RESUME_STATE (context);
2451 if (child_frame.ex) {
2453 * An exception occurred, need to run finally, fault and catch handlers..
2455 frame->ex = child_frame.ex;
2456 goto handle_finally;
2461 MINT_IN_CASE(MINT_JIT_CALL) {
2462 MonoMethodSignature *sig;
2463 RuntimeMethod *rmethod = rtm->data_items [* (guint16 *)(ip + 1)];
2464 MonoFtnDesc ftndesc;
2465 guint8 res_buf [256];
2469 //printf ("%s\n", mono_method_full_name (rmethod->method, 1));
2472 * Call JITted code through a gsharedvt_out wrapper. These wrappers receive every argument
2473 * by ref and return a return value using an explicit return value argument.
2475 if (!rmethod->jit_wrapper) {
2476 MonoMethod *method = rmethod->method;
2479 sig = mono_method_signature (method);
2482 MonoMethod *wrapper = mini_get_gsharedvt_out_sig_wrapper (sig);
2483 //printf ("J: %s %s\n", mono_method_full_name (method, 1), mono_method_full_name (wrapper, 1));
2485 gpointer jit_wrapper = mono_jit_compile_method_jit_only (wrapper, &error);
2486 mono_error_assert_ok (&error);
2488 gpointer addr = mono_jit_compile_method_jit_only (method, &error);
2490 mono_error_assert_ok (&error);
2492 rmethod->jit_addr = addr;
2493 rmethod->jit_sig = sig;
2494 mono_memory_barrier ();
2495 rmethod->jit_wrapper = jit_wrapper;
2498 sig = rmethod->jit_sig;
2503 sp -= sig->param_count;
2507 ftndesc.addr = rmethod->jit_addr;
2510 // FIXME: Optimize this
2514 int stack_index = 0;
2515 if (rmethod->hasthis) {
2516 args [pindex ++] = sp [0].data.p;
2519 type = rmethod->rtype;
2520 if (type->type != MONO_TYPE_VOID) {
2521 if (MONO_TYPE_ISSTRUCT (type))
2522 args [pindex ++] = vt_sp;
2524 args [pindex ++] = res_buf;
2526 for (int i = 0; i < rmethod->param_count; ++i) {
2527 MonoType *t = rmethod->param_types [i];
2528 stackval *sval = &sp [stack_index + i];
2529 if (sig->params [i]->byref) {
2530 args [pindex ++] = sval->data.p;
2531 } else if (MONO_TYPE_ISSTRUCT (t)) {
2532 args [pindex ++] = sval->data.p;
2533 } else if (MONO_TYPE_IS_REFERENCE (t)) {
2534 args [pindex ++] = &sval->data.p;
2543 case MONO_TYPE_VALUETYPE:
2544 args [pindex ++] = &sval->data.i;
2547 case MONO_TYPE_FNPTR:
2550 case MONO_TYPE_OBJECT:
2551 args [pindex ++] = &sval->data.p;
2555 args [pindex ++] = &sval->data.l;
2558 printf ("%s\n", mono_type_full_name (t));
2559 g_assert_not_reached ();
2565 * Push an LMF frame on the LMF stack
2566 * to mark the transition to compiled code.
2568 memset (&ext, 0, sizeof (ext));
2569 ext.interp_exit = TRUE;
2570 ext.interp_exit_data = frame;
2572 mono_push_lmf (&ext);
2576 void (*func)(gpointer) = rmethod->jit_wrapper;
2582 void (*func)(gpointer, gpointer) = rmethod->jit_wrapper;
2584 func (args [0], &ftndesc);
2588 void (*func)(gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
2590 func (args [0], args [1], &ftndesc);
2594 void (*func)(gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
2596 func (args [0], args [1], args [2], &ftndesc);
2600 void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
2602 func (args [0], args [1], args [2], args [3], &ftndesc);
2606 void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
2608 func (args [0], args [1], args [2], args [3], args [4], &ftndesc);
2612 void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
2614 func (args [0], args [1], args [2], args [3], args [4], args [5], &ftndesc);
2618 void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
2620 func (args [0], args [1], args [2], args [3], args [4], args [5], args [6], &ftndesc);
2624 g_assert_not_reached ();
2628 mono_pop_lmf (&ext.lmf);
2630 if (context->has_resume_state) {
2632 * If this bit is set, it means the call has thrown the exception, and we
2633 * reached this point because the EH code in mono_handle_exception ()
2634 * unwound all the JITted frames below us. mono_interp_set_resume_state ()
2635 * has set the fields in context to indicate where we have to resume execution.
2637 if (frame == context->handler_frame)
2638 SET_RESUME_STATE (context);
2643 MonoType *rtype = rmethod->rtype;
2644 switch (rtype->type) {
2645 case MONO_TYPE_VOID:
2646 case MONO_TYPE_OBJECT:
2647 case MONO_TYPE_STRING:
2648 case MONO_TYPE_CLASS:
2649 case MONO_TYPE_ARRAY:
2650 case MONO_TYPE_SZARRAY:
2653 sp->data.p = *(gpointer*)res_buf;
2656 sp->data.i = *(gint8*)res_buf;
2659 sp->data.i = *(guint8*)res_buf;
2662 sp->data.i = *(gint16*)res_buf;
2665 sp->data.i = *(guint16*)res_buf;
2668 sp->data.i = *(gint32*)res_buf;
2671 sp->data.i = *(guint32*)res_buf;
2673 case MONO_TYPE_VALUETYPE:
2674 /* The result was written to vt_sp */
2677 case MONO_TYPE_GENERICINST:
2678 if (MONO_TYPE_IS_REFERENCE (rtype)) {
2679 sp->data.p = *(gpointer*)res_buf;
2681 /* The result was written to vt_sp */
2686 printf ("%s\n", mono_type_full_name (rtype));
2687 g_assert_not_reached ();
2690 if (rtype->type != MONO_TYPE_VOID)
2695 MINT_IN_CASE(MINT_CALLVIRT) {
2696 stackval *endsp = sp;
2697 MonoObject *this_arg;
2702 token = * (unsigned short *)(ip + 1);
2704 child_frame.runtime_method = rtm->data_items [token];
2706 child_frame.retval = sp;
2708 /* decrement by the actual number of args */
2709 sp -= child_frame.runtime_method->param_count + 1;
2710 child_frame.stack_args = sp;
2711 this_arg = sp->data.p;
2713 THROW_EX (mono_get_exception_null_reference(), ip - 2);
2714 child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
2716 MonoClass *this_class = this_arg->vtable->klass;
2717 if (this_class->valuetype && child_frame.runtime_method->method->klass->valuetype) {
2719 gpointer *unboxed = mono_object_unbox (this_arg);
2720 sp [0].data.p = unboxed;
2723 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2725 context->current_frame = frame;
2727 if (context->has_resume_state) {
2728 if (frame == context->handler_frame)
2729 SET_RESUME_STATE (context);
2734 if (child_frame.ex) {
2736 * An exception occurred, need to run finally, fault and catch handlers..
2738 frame->ex = child_frame.ex;
2739 if (context->search_for_handler) {
2740 context->search_for_handler = 0;
2741 goto handle_exception;
2743 goto handle_finally;
2746 /* need to handle typedbyref ... */
2751 MINT_IN_CASE(MINT_VCALLVIRT) {
2752 MonoObject *this_arg;
2757 token = * (unsigned short *)(ip + 1);
2759 child_frame.runtime_method = rtm->data_items [token];
2761 child_frame.retval = sp;
2763 /* decrement by the actual number of args */
2764 sp -= child_frame.runtime_method->param_count + 1;
2765 child_frame.stack_args = sp;
2766 this_arg = sp->data.p;
2768 THROW_EX (mono_get_exception_null_reference(), ip - 2);
2769 child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
2771 MonoClass *this_class = this_arg->vtable->klass;
2772 if (this_class->valuetype && child_frame.runtime_method->method->klass->valuetype) {
2773 gpointer *unboxed = mono_object_unbox (this_arg);
2774 sp [0].data.p = unboxed;
2777 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2779 context->current_frame = frame;
2781 if (context->has_resume_state) {
2782 if (frame == context->handler_frame)
2783 SET_RESUME_STATE (context);
2788 if (child_frame.ex) {
2790 * An exception occurred, need to run finally, fault and catch handlers..
2792 frame->ex = child_frame.ex;
2793 if (context->search_for_handler) {
2794 context->search_for_handler = 0;
2795 goto handle_exception;
2797 goto handle_finally;
2801 MINT_IN_CASE(MINT_CALLRUN)
2802 ves_runtime_method (frame, context);
2805 goto handle_exception;
2808 MINT_IN_CASE(MINT_RET)
2810 *frame->retval = *sp;
2811 if (sp > frame->stack)
2812 g_warning ("ret: more values on stack: %d", sp-frame->stack);
2814 MINT_IN_CASE(MINT_RET_VOID)
2815 if (sp > frame->stack)
2816 g_warning ("ret.void: more values on stack: %d", sp-frame->stack);
2818 MINT_IN_CASE(MINT_RET_VT)
2819 i32 = READ32(ip + 1);
2821 memcpy(frame->retval->data.p, sp->data.p, i32);
2822 if (sp > frame->stack)
2823 g_warning ("ret.vt: more values on stack: %d", sp-frame->stack);
2825 MINT_IN_CASE(MINT_BR_S)
2826 ip += (short) *(ip + 1);
2828 MINT_IN_CASE(MINT_BR)
2829 ip += (gint32) READ32(ip + 1);
2831 #define ZEROP_S(datamem, op) \
2833 if (sp->data.datamem op 0) \
2834 ip += * (gint16 *)(ip + 1); \
2838 #define ZEROP(datamem, op) \
2840 if (sp->data.datamem op 0) \
2841 ip += READ32(ip + 1); \
2845 MINT_IN_CASE(MINT_BRFALSE_I4_S)
2848 MINT_IN_CASE(MINT_BRFALSE_I8_S)
2851 MINT_IN_CASE(MINT_BRFALSE_R8_S)
2854 MINT_IN_CASE(MINT_BRFALSE_I4)
2857 MINT_IN_CASE(MINT_BRFALSE_I8)
2860 MINT_IN_CASE(MINT_BRFALSE_R8)
2863 MINT_IN_CASE(MINT_BRTRUE_I4_S)
2866 MINT_IN_CASE(MINT_BRTRUE_I8_S)
2869 MINT_IN_CASE(MINT_BRTRUE_R8_S)
2872 MINT_IN_CASE(MINT_BRTRUE_I4)
2875 MINT_IN_CASE(MINT_BRTRUE_I8)
2878 MINT_IN_CASE(MINT_BRTRUE_R8)
2881 #define CONDBR_S(cond) \
2884 ip += * (gint16 *)(ip + 1); \
2887 #define BRELOP_S(datamem, op) \
2888 CONDBR_S(sp[0].data.datamem op sp[1].data.datamem)
2890 #define CONDBR(cond) \
2893 ip += READ32(ip + 1); \
2897 #define BRELOP(datamem, op) \
2898 CONDBR(sp[0].data.datamem op sp[1].data.datamem)
2900 MINT_IN_CASE(MINT_BEQ_I4_S)
2903 MINT_IN_CASE(MINT_BEQ_I8_S)
2906 MINT_IN_CASE(MINT_BEQ_R8_S)
2907 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2909 MINT_IN_CASE(MINT_BEQ_I4)
2912 MINT_IN_CASE(MINT_BEQ_I8)
2915 MINT_IN_CASE(MINT_BEQ_R8)
2916 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2918 MINT_IN_CASE(MINT_BGE_I4_S)
2921 MINT_IN_CASE(MINT_BGE_I8_S)
2924 MINT_IN_CASE(MINT_BGE_R8_S)
2925 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
2927 MINT_IN_CASE(MINT_BGE_I4)
2930 MINT_IN_CASE(MINT_BGE_I8)
2933 MINT_IN_CASE(MINT_BGE_R8)
2934 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
2936 MINT_IN_CASE(MINT_BGT_I4_S)
2939 MINT_IN_CASE(MINT_BGT_I8_S)
2942 MINT_IN_CASE(MINT_BGT_R8_S)
2943 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2945 MINT_IN_CASE(MINT_BGT_I4)
2948 MINT_IN_CASE(MINT_BGT_I8)
2951 MINT_IN_CASE(MINT_BGT_R8)
2952 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2954 MINT_IN_CASE(MINT_BLT_I4_S)
2957 MINT_IN_CASE(MINT_BLT_I8_S)
2960 MINT_IN_CASE(MINT_BLT_R8_S)
2961 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2963 MINT_IN_CASE(MINT_BLT_I4)
2966 MINT_IN_CASE(MINT_BLT_I8)
2969 MINT_IN_CASE(MINT_BLT_R8)
2970 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2972 MINT_IN_CASE(MINT_BLE_I4_S)
2975 MINT_IN_CASE(MINT_BLE_I8_S)
2978 MINT_IN_CASE(MINT_BLE_R8_S)
2979 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2981 MINT_IN_CASE(MINT_BLE_I4)
2984 MINT_IN_CASE(MINT_BLE_I8)
2987 MINT_IN_CASE(MINT_BLE_R8)
2988 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2990 MINT_IN_CASE(MINT_BNE_UN_I4_S)
2993 MINT_IN_CASE(MINT_BNE_UN_I8_S)
2996 MINT_IN_CASE(MINT_BNE_UN_R8_S)
2997 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
2999 MINT_IN_CASE(MINT_BNE_UN_I4)
3002 MINT_IN_CASE(MINT_BNE_UN_I8)
3005 MINT_IN_CASE(MINT_BNE_UN_R8)
3006 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
3009 #define BRELOP_S_CAST(datamem, op, type) \
3011 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
3012 ip += * (gint16 *)(ip + 1); \
3016 #define BRELOP_CAST(datamem, op, type) \
3018 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
3019 ip += READ32(ip + 1); \
3023 MINT_IN_CASE(MINT_BGE_UN_I4_S)
3024 BRELOP_S_CAST(i, >=, guint32);
3026 MINT_IN_CASE(MINT_BGE_UN_I8_S)
3027 BRELOP_S_CAST(l, >=, guint64);
3029 MINT_IN_CASE(MINT_BGE_UN_R8_S)
3030 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
3032 MINT_IN_CASE(MINT_BGE_UN_I4)
3033 BRELOP_CAST(i, >=, guint32);
3035 MINT_IN_CASE(MINT_BGE_UN_I8)
3036 BRELOP_CAST(l, >=, guint64);
3038 MINT_IN_CASE(MINT_BGE_UN_R8)
3039 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
3041 MINT_IN_CASE(MINT_BGT_UN_I4_S)
3042 BRELOP_S_CAST(i, >, guint32);
3044 MINT_IN_CASE(MINT_BGT_UN_I8_S)
3045 BRELOP_S_CAST(l, >, guint64);
3047 MINT_IN_CASE(MINT_BGT_UN_R8_S)
3048 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
3050 MINT_IN_CASE(MINT_BGT_UN_I4)
3051 BRELOP_CAST(i, >, guint32);
3053 MINT_IN_CASE(MINT_BGT_UN_I8)
3054 BRELOP_CAST(l, >, guint64);
3056 MINT_IN_CASE(MINT_BGT_UN_R8)
3057 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
3059 MINT_IN_CASE(MINT_BLE_UN_I4_S)
3060 BRELOP_S_CAST(i, <=, guint32);
3062 MINT_IN_CASE(MINT_BLE_UN_I8_S)
3063 BRELOP_S_CAST(l, <=, guint64);
3065 MINT_IN_CASE(MINT_BLE_UN_R8_S)
3066 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
3068 MINT_IN_CASE(MINT_BLE_UN_I4)
3069 BRELOP_CAST(i, <=, guint32);
3071 MINT_IN_CASE(MINT_BLE_UN_I8)
3072 BRELOP_CAST(l, <=, guint64);
3074 MINT_IN_CASE(MINT_BLE_UN_R8)
3075 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
3077 MINT_IN_CASE(MINT_BLT_UN_I4_S)
3078 BRELOP_S_CAST(i, <, guint32);
3080 MINT_IN_CASE(MINT_BLT_UN_I8_S)
3081 BRELOP_S_CAST(l, <, guint64);
3083 MINT_IN_CASE(MINT_BLT_UN_R8_S)
3084 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
3086 MINT_IN_CASE(MINT_BLT_UN_I4)
3087 BRELOP_CAST(i, <, guint32);
3089 MINT_IN_CASE(MINT_BLT_UN_I8)
3090 BRELOP_CAST(l, <, guint64);
3092 MINT_IN_CASE(MINT_BLT_UN_R8)
3093 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
3095 MINT_IN_CASE(MINT_SWITCH) {
3097 const unsigned short *st;
3103 if ((guint32)sp->data.i < n) {
3105 ip += 2 * (guint32)sp->data.i;
3106 offset = READ32 (ip);
3113 MINT_IN_CASE(MINT_LDIND_I1)
3115 sp[-1].data.i = *(gint8*)sp[-1].data.p;
3117 MINT_IN_CASE(MINT_LDIND_U1)
3119 sp[-1].data.i = *(guint8*)sp[-1].data.p;
3121 MINT_IN_CASE(MINT_LDIND_I2)
3123 sp[-1].data.i = *(gint16*)sp[-1].data.p;
3125 MINT_IN_CASE(MINT_LDIND_U2)
3127 sp[-1].data.i = *(guint16*)sp[-1].data.p;
3129 MINT_IN_CASE(MINT_LDIND_I4) /* Fall through */
3130 MINT_IN_CASE(MINT_LDIND_U4)
3132 sp[-1].data.i = *(gint32*)sp[-1].data.p;
3134 MINT_IN_CASE(MINT_LDIND_I8)
3136 sp[-1].data.l = *(gint64*)sp[-1].data.p;
3138 MINT_IN_CASE(MINT_LDIND_I) {
3139 guint16 offset = * (guint16 *)(ip + 1);
3140 sp[-1 - offset].data.p = *(gpointer*)sp[-1 - offset].data.p;
3144 MINT_IN_CASE(MINT_LDIND_R4)
3146 sp[-1].data.f = *(gfloat*)sp[-1].data.p;
3148 MINT_IN_CASE(MINT_LDIND_R8)
3150 sp[-1].data.f = *(gdouble*)sp[-1].data.p;
3152 MINT_IN_CASE(MINT_LDIND_REF)
3154 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
3156 MINT_IN_CASE(MINT_STIND_REF)
3159 mono_gc_wbarrier_generic_store (sp->data.p, sp [1].data.p);
3161 MINT_IN_CASE(MINT_STIND_I1)
3164 * (gint8 *) sp->data.p = (gint8)sp[1].data.i;
3166 MINT_IN_CASE(MINT_STIND_I2)
3169 * (gint16 *) sp->data.p = (gint16)sp[1].data.i;
3171 MINT_IN_CASE(MINT_STIND_I4)
3174 * (gint32 *) sp->data.p = sp[1].data.i;
3176 MINT_IN_CASE(MINT_STIND_I)
3179 * (mono_i *) sp->data.p = (mono_i)sp[1].data.p;
3181 MINT_IN_CASE(MINT_STIND_I8)
3184 * (gint64 *) sp->data.p = sp[1].data.l;
3186 MINT_IN_CASE(MINT_STIND_R4)
3189 * (float *) sp->data.p = (gfloat)sp[1].data.f;
3191 MINT_IN_CASE(MINT_STIND_R8)
3194 * (double *) sp->data.p = sp[1].data.f;
3196 MINT_IN_CASE(MINT_MONO_ATOMIC_STORE_I4)
3199 InterlockedWrite ((gint32 *) sp->data.p, sp [1].data.i);
3201 #define BINOP(datamem, op) \
3203 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.datamem; \
3205 MINT_IN_CASE(MINT_ADD_I4)
3208 MINT_IN_CASE(MINT_ADD_I8)
3211 MINT_IN_CASE(MINT_ADD_R8)
3214 MINT_IN_CASE(MINT_ADD1_I4)
3218 MINT_IN_CASE(MINT_SUB_I4)
3221 MINT_IN_CASE(MINT_SUB_I8)
3224 MINT_IN_CASE(MINT_SUB_R8)
3227 MINT_IN_CASE(MINT_SUB1_I4)
3231 MINT_IN_CASE(MINT_MUL_I4)
3234 MINT_IN_CASE(MINT_MUL_I8)
3237 MINT_IN_CASE(MINT_MUL_R8)
3240 MINT_IN_CASE(MINT_DIV_I4)
3241 if (sp [-1].data.i == 0)
3242 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3243 if (sp [-1].data.i == (-1))
3244 THROW_EX (mono_get_exception_overflow (), ip);
3247 MINT_IN_CASE(MINT_DIV_I8)
3248 if (sp [-1].data.l == 0)
3249 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3250 if (sp [-1].data.l == (-1))
3251 THROW_EX (mono_get_exception_overflow (), ip);
3254 MINT_IN_CASE(MINT_DIV_R8)
3258 #define BINOP_CAST(datamem, op, type) \
3260 sp [-1].data.datamem = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
3262 MINT_IN_CASE(MINT_DIV_UN_I4)
3263 if (sp [-1].data.i == 0)
3264 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3265 BINOP_CAST(i, /, guint32);
3267 MINT_IN_CASE(MINT_DIV_UN_I8)
3268 if (sp [-1].data.l == 0)
3269 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3270 BINOP_CAST(l, /, guint64);
3272 MINT_IN_CASE(MINT_REM_I4)
3273 if (sp [-1].data.i == 0)
3274 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3275 if (sp [-1].data.i == (-1))
3276 THROW_EX (mono_get_exception_overflow (), ip);
3279 MINT_IN_CASE(MINT_REM_I8)
3280 if (sp [-1].data.l == 0)
3281 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3282 if (sp [-1].data.l == (-1))
3283 THROW_EX (mono_get_exception_overflow (), ip);
3286 MINT_IN_CASE(MINT_REM_R8)
3287 /* FIXME: what do we actually do here? */
3289 sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
3292 MINT_IN_CASE(MINT_REM_UN_I4)
3293 if (sp [-1].data.i == 0)
3294 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3295 BINOP_CAST(i, %, guint32);
3297 MINT_IN_CASE(MINT_REM_UN_I8)
3298 if (sp [-1].data.l == 0)
3299 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3300 BINOP_CAST(l, %, guint64);
3302 MINT_IN_CASE(MINT_AND_I4)
3305 MINT_IN_CASE(MINT_AND_I8)
3308 MINT_IN_CASE(MINT_OR_I4)
3311 MINT_IN_CASE(MINT_OR_I8)
3314 MINT_IN_CASE(MINT_XOR_I4)
3317 MINT_IN_CASE(MINT_XOR_I8)
3321 #define SHIFTOP(datamem, op) \
3323 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.i; \
3326 MINT_IN_CASE(MINT_SHL_I4)
3329 MINT_IN_CASE(MINT_SHL_I8)
3332 MINT_IN_CASE(MINT_SHR_I4)
3335 MINT_IN_CASE(MINT_SHR_I8)
3338 MINT_IN_CASE(MINT_SHR_UN_I4)
3340 sp [-1].data.i = (guint32)sp [-1].data.i >> sp [0].data.i;
3343 MINT_IN_CASE(MINT_SHR_UN_I8)
3345 sp [-1].data.l = (guint64)sp [-1].data.l >> sp [0].data.i;
3348 MINT_IN_CASE(MINT_NEG_I4)
3349 sp [-1].data.i = - sp [-1].data.i;
3352 MINT_IN_CASE(MINT_NEG_I8)
3353 sp [-1].data.l = - sp [-1].data.l;
3356 MINT_IN_CASE(MINT_NEG_R8)
3357 sp [-1].data.f = - sp [-1].data.f;
3360 MINT_IN_CASE(MINT_NOT_I4)
3361 sp [-1].data.i = ~ sp [-1].data.i;
3364 MINT_IN_CASE(MINT_NOT_I8)
3365 sp [-1].data.l = ~ sp [-1].data.l;
3368 MINT_IN_CASE(MINT_CONV_I1_I4)
3369 sp [-1].data.i = (gint8)sp [-1].data.i;
3372 MINT_IN_CASE(MINT_CONV_I1_I8)
3373 sp [-1].data.i = (gint8)sp [-1].data.l;
3376 MINT_IN_CASE(MINT_CONV_I1_R8)
3377 sp [-1].data.i = (gint8)sp [-1].data.f;
3380 MINT_IN_CASE(MINT_CONV_U1_I4)
3381 sp [-1].data.i = (guint8)sp [-1].data.i;
3384 MINT_IN_CASE(MINT_CONV_U1_I8)
3385 sp [-1].data.i = (guint8)sp [-1].data.l;
3388 MINT_IN_CASE(MINT_CONV_U1_R8)
3389 sp [-1].data.i = (guint8)sp [-1].data.f;
3392 MINT_IN_CASE(MINT_CONV_I2_I4)
3393 sp [-1].data.i = (gint16)sp [-1].data.i;
3396 MINT_IN_CASE(MINT_CONV_I2_I8)
3397 sp [-1].data.i = (gint16)sp [-1].data.l;
3400 MINT_IN_CASE(MINT_CONV_I2_R8)
3401 sp [-1].data.i = (gint16)sp [-1].data.f;
3404 MINT_IN_CASE(MINT_CONV_U2_I4)
3405 sp [-1].data.i = (guint16)sp [-1].data.i;
3408 MINT_IN_CASE(MINT_CONV_U2_I8)
3409 sp [-1].data.i = (guint16)sp [-1].data.l;
3412 MINT_IN_CASE(MINT_CONV_U2_R8)
3413 sp [-1].data.i = (guint16)sp [-1].data.f;
3416 MINT_IN_CASE(MINT_CONV_I4_R8)
3417 sp [-1].data.i = (gint32)sp [-1].data.f;
3420 MINT_IN_CASE(MINT_CONV_U4_I8)
3421 MINT_IN_CASE(MINT_CONV_I4_I8)
3422 sp [-1].data.i = (gint32)sp [-1].data.l;
3425 MINT_IN_CASE(MINT_CONV_I4_I8_SP)
3426 sp [-2].data.i = (gint32)sp [-2].data.l;
3429 MINT_IN_CASE(MINT_CONV_U4_R8)
3430 sp [-1].data.i = (guint32)sp [-1].data.f;
3433 MINT_IN_CASE(MINT_CONV_I8_I4)
3434 sp [-1].data.l = sp [-1].data.i;
3437 MINT_IN_CASE(MINT_CONV_I8_I4_SP)
3438 sp [-2].data.l = sp [-2].data.i;
3441 MINT_IN_CASE(MINT_CONV_I8_U4)
3442 sp [-1].data.l = (guint32)sp [-1].data.i;
3445 MINT_IN_CASE(MINT_CONV_I8_R8)
3446 sp [-1].data.l = (gint64)sp [-1].data.f;
3449 MINT_IN_CASE(MINT_CONV_R4_I4)
3450 sp [-1].data.f = (float)sp [-1].data.i;
3453 MINT_IN_CASE(MINT_CONV_R4_I8)
3454 sp [-1].data.f = (float)sp [-1].data.l;
3457 MINT_IN_CASE(MINT_CONV_R4_R8)
3458 sp [-1].data.f = (float)sp [-1].data.f;
3461 MINT_IN_CASE(MINT_CONV_R8_I4)
3462 sp [-1].data.f = (double)sp [-1].data.i;
3465 MINT_IN_CASE(MINT_CONV_R8_I8)
3466 sp [-1].data.f = (double)sp [-1].data.l;
3469 MINT_IN_CASE(MINT_CONV_U8_I4)
3470 sp [-1].data.l = sp [-1].data.i & 0xffffffff;
3473 MINT_IN_CASE(MINT_CONV_U8_R8)
3474 sp [-1].data.l = (guint64)sp [-1].data.f;
3477 MINT_IN_CASE(MINT_CPOBJ) {
3478 c = rtm->data_items[* (guint16 *)(ip + 1)];
3479 g_assert (c->byval_arg.type == MONO_TYPE_VALUETYPE);
3480 /* if this assertion fails, we need to add a write barrier */
3481 g_assert (!MONO_TYPE_IS_REFERENCE (&c->byval_arg));
3482 stackval_from_data (&c->byval_arg, &sp [-2], sp [-1].data.p, FALSE);
3487 MINT_IN_CASE(MINT_LDOBJ) {
3489 c = rtm->data_items[* (guint16 *)(ip + 1)];
3492 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
3493 int size = mono_class_value_size (c, NULL);
3494 sp [-1].data.p = vt_sp;
3495 vt_sp += (size + 7) & ~7;
3497 stackval_from_data (&c->byval_arg, &sp [-1], p, FALSE);
3500 MINT_IN_CASE(MINT_LDSTR)
3501 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
3505 MINT_IN_CASE(MINT_NEWOBJ) {
3506 MonoClass *newobj_class;
3507 MonoMethodSignature *csig;
3508 stackval valuetype_this;
3514 token = * (guint16 *)(ip + 1);
3517 child_frame.ip = NULL;
3518 child_frame.ex = NULL;
3520 child_frame.runtime_method = rtm->data_items [token];
3521 csig = mono_method_signature (child_frame.runtime_method->method);
3522 newobj_class = child_frame.runtime_method->method->klass;
3523 /*if (profiling_classes) {
3524 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
3526 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
3529 if (newobj_class->parent == mono_defaults.array_class) {
3530 sp -= csig->param_count;
3531 child_frame.stack_args = sp;
3532 o = ves_array_create (&child_frame, context->domain, newobj_class, csig, sp);
3534 THROW_EX (child_frame.ex, ip);
3535 goto array_constructed;
3538 g_assert (csig->hasthis);
3539 if (csig->param_count) {
3540 sp -= csig->param_count;
3541 memmove (sp + 1, sp, csig->param_count * sizeof (stackval));
3543 child_frame.stack_args = sp;
3546 * First arg is the object.
3548 if (newobj_class->valuetype) {
3549 MonoType *t = &newobj_class->byval_arg;
3550 memset (&valuetype_this, 0, sizeof (stackval));
3551 if (!newobj_class->enumtype && (t->type == MONO_TYPE_VALUETYPE || (t->type == MONO_TYPE_GENERICINST && mono_type_generic_inst_is_valuetype (t)))) {
3553 valuetype_this.data.p = vt_sp;
3555 sp->data.p = &valuetype_this;
3558 if (newobj_class != mono_defaults.string_class) {
3559 context->managed_code = 0;
3560 o = mono_object_new_checked (context->domain, newobj_class, &error);
3561 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3562 context->managed_code = 1;
3563 if (*mono_thread_interruption_request_flag ())
3564 mono_thread_interruption_checkpoint ();
3568 child_frame.retval = &retval;
3572 g_assert (csig->call_convention == MONO_CALL_DEFAULT);
3574 ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
3576 context->current_frame = frame;
3578 if (context->has_resume_state) {
3579 if (frame == context->handler_frame)
3580 SET_RESUME_STATE (context);
3585 if (child_frame.ex) {
3587 * An exception occurred, need to run finally, fault and catch handlers..
3589 frame->ex = child_frame.ex;
3590 goto handle_finally;
3593 * a constructor returns void, but we need to return the object we created
3596 if (newobj_class->valuetype && !newobj_class->enumtype) {
3597 *sp = valuetype_this;
3598 } else if (newobj_class == mono_defaults.string_class) {
3606 MINT_IN_CASE(MINT_CASTCLASS)
3607 c = rtm->data_items [*(guint16 *)(ip + 1)];
3608 if ((o = sp [-1].data.p)) {
3609 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
3610 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3612 THROW_EX (mono_get_exception_invalid_cast (), ip);
3616 MINT_IN_CASE(MINT_ISINST)
3617 c = rtm->data_items [*(guint16 *)(ip + 1)];
3618 if ((o = sp [-1].data.p)) {
3619 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
3620 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3622 sp [-1].data.p = NULL;
3626 MINT_IN_CASE(MINT_CONV_R_UN_I4)
3627 sp [-1].data.f = (double)(guint32)sp [-1].data.i;
3630 MINT_IN_CASE(MINT_CONV_R_UN_I8)
3631 sp [-1].data.f = (double)(guint64)sp [-1].data.l;
3634 MINT_IN_CASE(MINT_UNBOX)
3635 c = rtm->data_items[*(guint16 *)(ip + 1)];
3639 THROW_EX (mono_get_exception_null_reference (), ip);
3641 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
3642 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3643 if (!(isinst_obj || ((o->vtable->klass->rank == 0) && (o->vtable->klass->element_class == c->element_class))))
3644 THROW_EX (mono_get_exception_invalid_cast (), ip);
3646 sp [-1].data.p = mono_object_unbox (o);
3649 MINT_IN_CASE(MINT_THROW)
3651 frame->ex_handler = NULL;
3653 sp->data.p = mono_get_exception_null_reference ();
3654 THROW_EX ((MonoException *)sp->data.p, ip);
3656 MINT_IN_CASE(MINT_LDFLDA_UNSAFE)
3658 sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
3661 MINT_IN_CASE(MINT_LDFLDA)
3664 THROW_EX (mono_get_exception_null_reference (), ip);
3665 sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
3668 MINT_IN_CASE(MINT_CKNULL)
3671 THROW_EX (mono_get_exception_null_reference (), ip);
3675 #define LDFLD(datamem, fieldtype) \
3676 o = sp [-1].data.p; \
3678 THROW_EX (mono_get_exception_null_reference (), ip); \
3679 sp[-1].data.datamem = * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) ; \
3682 MINT_IN_CASE(MINT_LDFLD_I1) LDFLD(i, gint8); MINT_IN_BREAK;
3683 MINT_IN_CASE(MINT_LDFLD_U1) LDFLD(i, guint8); MINT_IN_BREAK;
3684 MINT_IN_CASE(MINT_LDFLD_I2) LDFLD(i, gint16); MINT_IN_BREAK;
3685 MINT_IN_CASE(MINT_LDFLD_U2) LDFLD(i, guint16); MINT_IN_BREAK;
3686 MINT_IN_CASE(MINT_LDFLD_I4) LDFLD(i, gint32); MINT_IN_BREAK;
3687 MINT_IN_CASE(MINT_LDFLD_I8) LDFLD(l, gint64); MINT_IN_BREAK;
3688 MINT_IN_CASE(MINT_LDFLD_R4) LDFLD(f, float); MINT_IN_BREAK;
3689 MINT_IN_CASE(MINT_LDFLD_R8) LDFLD(f, double); MINT_IN_BREAK;
3690 MINT_IN_CASE(MINT_LDFLD_O) LDFLD(p, gpointer); MINT_IN_BREAK;
3691 MINT_IN_CASE(MINT_LDFLD_P) LDFLD(p, gpointer); MINT_IN_BREAK;
3693 MINT_IN_CASE(MINT_LDFLD_VT)
3696 THROW_EX (mono_get_exception_null_reference (), ip);
3697 i32 = READ32(ip + 2);
3698 sp [-1].data.p = vt_sp;
3699 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
3700 vt_sp += (i32 + 7) & ~7;
3704 MINT_IN_CASE(MINT_LDRMFLD) {
3706 MonoClassField *field;
3711 THROW_EX (mono_get_exception_null_reference (), ip);
3712 field = rtm->data_items[* (guint16 *)(ip + 1)];
3714 if (mono_object_is_transparent_proxy (o)) {
3715 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3717 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
3718 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3720 addr = (char*)o + field->offset;
3723 stackval_from_data (field->type, &sp [-1], addr, FALSE);
3727 MINT_IN_CASE(MINT_LDRMFLD_VT) {
3728 MonoClassField *field;
3734 THROW_EX (mono_get_exception_null_reference (), ip);
3735 field = rtm->data_items[* (guint16 *)(ip + 1)];
3736 i32 = READ32(ip + 2);
3738 if (mono_object_is_transparent_proxy (o)) {
3739 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3740 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
3741 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3743 addr = (char*)o + field->offset;
3746 sp [-1].data.p = vt_sp;
3747 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
3748 vt_sp += (i32 + 7) & ~7;
3749 memcpy(sp [-1].data.p, addr, i32);
3753 #define STFLD(datamem, fieldtype) \
3754 o = sp [-2].data.p; \
3756 THROW_EX (mono_get_exception_null_reference (), ip); \
3758 * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) = sp[1].data.datamem; \
3761 MINT_IN_CASE(MINT_STFLD_I1) STFLD(i, gint8); MINT_IN_BREAK;
3762 MINT_IN_CASE(MINT_STFLD_U1) STFLD(i, guint8); MINT_IN_BREAK;
3763 MINT_IN_CASE(MINT_STFLD_I2) STFLD(i, gint16); MINT_IN_BREAK;
3764 MINT_IN_CASE(MINT_STFLD_U2) STFLD(i, guint16); MINT_IN_BREAK;
3765 MINT_IN_CASE(MINT_STFLD_I4) STFLD(i, gint32); MINT_IN_BREAK;
3766 MINT_IN_CASE(MINT_STFLD_I8) STFLD(l, gint64); MINT_IN_BREAK;
3767 MINT_IN_CASE(MINT_STFLD_R4) STFLD(f, float); MINT_IN_BREAK;
3768 MINT_IN_CASE(MINT_STFLD_R8) STFLD(f, double); MINT_IN_BREAK;
3769 MINT_IN_CASE(MINT_STFLD_P) STFLD(p, gpointer); MINT_IN_BREAK;
3770 MINT_IN_CASE(MINT_STFLD_O)
3773 THROW_EX (mono_get_exception_null_reference (), ip);
3775 mono_gc_wbarrier_set_field (o, (char *) o + * (guint16 *)(ip + 1), sp [1].data.p);
3779 MINT_IN_CASE(MINT_STFLD_VT)
3782 THROW_EX (mono_get_exception_null_reference (), ip);
3783 i32 = READ32(ip + 2);
3785 memcpy((char *)o + * (guint16 *)(ip + 1), sp [1].data.p, i32);
3786 vt_sp -= (i32 + 7) & ~7;
3790 MINT_IN_CASE(MINT_STRMFLD) {
3791 MonoClassField *field;
3795 THROW_EX (mono_get_exception_null_reference (), ip);
3797 field = rtm->data_items[* (guint16 *)(ip + 1)];
3800 if (mono_object_is_transparent_proxy (o)) {
3801 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3802 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
3803 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3805 stackval_to_data (field->type, &sp [-1], (char*)o + field->offset, FALSE);
3810 MINT_IN_CASE(MINT_STRMFLD_VT) {
3811 MonoClassField *field;
3815 THROW_EX (mono_get_exception_null_reference (), ip);
3816 field = rtm->data_items[* (guint16 *)(ip + 1)];
3817 i32 = READ32(ip + 2);
3820 if (mono_object_is_transparent_proxy (o)) {
3821 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3822 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
3823 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3825 memcpy((char*)o + field->offset, sp [-1].data.p, i32);
3828 vt_sp -= (i32 + 7) & ~7;
3831 MINT_IN_CASE(MINT_LDSFLDA) {
3832 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3833 sp->data.p = mono_class_static_field_address (context->domain, field);
3838 MINT_IN_CASE(MINT_LDSFLD) {
3839 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3840 gpointer addr = mono_class_static_field_address (context->domain, field);
3841 stackval_from_data (field->type, sp, addr, FALSE);
3846 MINT_IN_CASE(MINT_LDSFLD_VT) {
3847 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3848 gpointer addr = mono_class_static_field_address (context->domain, field);
3849 int size = READ32 (ip + 2);
3853 vt_sp += (size + 7) & ~7;
3854 stackval_from_data (field->type, sp, addr, FALSE);
3858 MINT_IN_CASE(MINT_STSFLD) {
3859 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3860 gpointer addr = mono_class_static_field_address (context->domain, field);
3863 stackval_to_data (field->type, sp, addr, FALSE);
3866 MINT_IN_CASE(MINT_STSFLD_VT) {
3867 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3868 gpointer addr = mono_class_static_field_address (context->domain, field);
3869 int size = READ32 (ip + 2);
3873 stackval_to_data (field->type, sp, addr, FALSE);
3874 vt_sp -= (size + 7) & ~7;
3877 MINT_IN_CASE(MINT_STOBJ_VT) {
3879 c = rtm->data_items[* (guint16 *)(ip + 1)];
3881 size = mono_class_value_size (c, NULL);
3882 memcpy(sp [-2].data.p, sp [-1].data.p, size);
3883 vt_sp -= (size + 7) & ~7;
3887 MINT_IN_CASE(MINT_STOBJ) {
3888 c = rtm->data_items[* (guint16 *)(ip + 1)];
3891 g_assert (!c->byval_arg.byref);
3892 if (MONO_TYPE_IS_REFERENCE (&c->byval_arg))
3893 mono_gc_wbarrier_generic_store (sp [-2].data.p, sp [-1].data.p);
3895 stackval_from_data (&c->byval_arg, sp [-2].data.p, (char *) &sp [-1].data.p, FALSE);
3899 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_R8)
3900 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3901 THROW_EX (mono_get_exception_overflow (), ip);
3902 sp [-1].data.i = (guint32)sp [-1].data.f;
3905 MINT_IN_CASE(MINT_CONV_OVF_U8_I4)
3906 if (sp [-1].data.i < 0)
3907 THROW_EX (mono_get_exception_overflow (), ip);
3908 sp [-1].data.l = sp [-1].data.i;
3911 MINT_IN_CASE(MINT_CONV_OVF_U8_I8)
3912 if (sp [-1].data.l < 0)
3913 THROW_EX (mono_get_exception_overflow (), ip);
3916 MINT_IN_CASE(MINT_CONV_OVF_I8_U8)
3917 if ((guint64) sp [-1].data.l > MYGINT64_MAX)
3918 THROW_EX (mono_get_exception_overflow (), ip);
3921 MINT_IN_CASE(MINT_CONV_OVF_U8_R8)
3922 MINT_IN_CASE(MINT_CONV_OVF_I8_UN_R8)
3923 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGINT64_MAX)
3924 THROW_EX (mono_get_exception_overflow (), ip);
3925 sp [-1].data.l = (guint64)sp [-1].data.f;
3928 MINT_IN_CASE(MINT_CONV_OVF_I8_R8)
3929 if (sp [-1].data.f < MYGINT64_MIN || sp [-1].data.f > MYGINT64_MAX)
3930 THROW_EX (mono_get_exception_overflow (), ip);
3931 sp [-1].data.l = (gint64)sp [-1].data.f;
3934 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_I8)
3935 if ((mono_u)sp [-1].data.l > MYGUINT32_MAX)
3936 THROW_EX (mono_get_exception_overflow (), ip);
3937 sp [-1].data.i = (mono_u)sp [-1].data.l;
3940 MINT_IN_CASE(MINT_BOX) {
3941 c = rtm->data_items [* (guint16 *)(ip + 1)];
3942 guint16 offset = * (guint16 *)(ip + 2);
3944 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
3945 int size = mono_class_value_size (c, NULL);
3946 sp [-1 - offset].data.p = mono_value_box_checked (context->domain, c, sp [-1 - offset].data.p, &error);
3947 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3948 size = (size + 7) & ~7;
3951 stackval_to_data (&c->byval_arg, &sp [-1 - offset], (char *) &sp [-1 - offset], FALSE);
3952 sp [-1 - offset].data.p = mono_value_box_checked (context->domain, c, &sp [-1 - offset], &error);
3953 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3958 MINT_IN_CASE(MINT_NEWARR)
3959 sp [-1].data.p = (MonoObject*) mono_array_new_checked (context->domain, rtm->data_items[*(guint16 *)(ip + 1)], sp [-1].data.i, &error);
3960 if (!mono_error_ok (&error)) {
3961 THROW_EX (mono_error_convert_to_exception (&error), ip);
3963 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3965 /*if (profiling_classes) {
3966 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
3968 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
3972 MINT_IN_CASE(MINT_LDLEN)
3975 THROW_EX (mono_get_exception_null_reference (), ip);
3976 sp [-1].data.nati = mono_array_length ((MonoArray *)o);
3979 MINT_IN_CASE(MINT_GETCHR) {
3983 THROW_EX (mono_get_exception_null_reference (), ip);
3984 i32 = sp [-1].data.i;
3985 if (i32 < 0 || i32 >= mono_string_length (s))
3986 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3988 sp [-1].data.i = mono_string_chars(s)[i32];
3992 MINT_IN_CASE(MINT_STRLEN)
3996 THROW_EX (mono_get_exception_null_reference (), ip);
3997 sp [-1].data.i = mono_string_length ((MonoString*) o);
3999 MINT_IN_CASE(MINT_ARRAY_RANK)
4002 THROW_EX (mono_get_exception_null_reference (), ip);
4003 sp [-1].data.i = mono_object_class (sp [-1].data.p)->rank;
4006 MINT_IN_CASE(MINT_LDELEMA)
4007 MINT_IN_CASE(MINT_LDELEMA_TC) {
4008 gboolean needs_typecheck = *ip == MINT_LDELEMA_TC;
4010 MonoClass *klass = rtm->data_items [*(guint16 *) (ip + 1)];
4011 guint16 numargs = *(guint16 *) (ip + 2);
4016 sp->data.p = ves_array_element_address (frame, klass, (MonoArray *) o, &sp [1], needs_typecheck);
4018 THROW_EX (frame->ex, ip);
4023 MINT_IN_CASE(MINT_LDELEM_I1) /* fall through */
4024 MINT_IN_CASE(MINT_LDELEM_U1) /* fall through */
4025 MINT_IN_CASE(MINT_LDELEM_I2) /* fall through */
4026 MINT_IN_CASE(MINT_LDELEM_U2) /* fall through */
4027 MINT_IN_CASE(MINT_LDELEM_I4) /* fall through */
4028 MINT_IN_CASE(MINT_LDELEM_U4) /* fall through */
4029 MINT_IN_CASE(MINT_LDELEM_I8) /* fall through */
4030 MINT_IN_CASE(MINT_LDELEM_I) /* fall through */
4031 MINT_IN_CASE(MINT_LDELEM_R4) /* fall through */
4032 MINT_IN_CASE(MINT_LDELEM_R8) /* fall through */
4033 MINT_IN_CASE(MINT_LDELEM_REF) /* fall through */
4034 MINT_IN_CASE(MINT_LDELEM_VT) {
4042 THROW_EX (mono_get_exception_null_reference (), ip);
4044 aindex = sp [1].data.i;
4045 if (aindex >= mono_array_length (o))
4046 THROW_EX (mono_get_exception_index_out_of_range (), ip);
4049 * FIXME: throw mono_get_exception_array_type_mismatch () if needed
4052 case MINT_LDELEM_I1:
4053 sp [0].data.i = mono_array_get (o, gint8, aindex);
4055 case MINT_LDELEM_U1:
4056 sp [0].data.i = mono_array_get (o, guint8, aindex);
4058 case MINT_LDELEM_I2:
4059 sp [0].data.i = mono_array_get (o, gint16, aindex);
4061 case MINT_LDELEM_U2:
4062 sp [0].data.i = mono_array_get (o, guint16, aindex);
4065 sp [0].data.nati = mono_array_get (o, mono_i, aindex);
4067 case MINT_LDELEM_I4:
4068 sp [0].data.i = mono_array_get (o, gint32, aindex);
4070 case MINT_LDELEM_U4:
4071 sp [0].data.i = mono_array_get (o, guint32, aindex);
4073 case MINT_LDELEM_I8:
4074 sp [0].data.l = mono_array_get (o, guint64, aindex);
4076 case MINT_LDELEM_R4:
4077 sp [0].data.f = mono_array_get (o, float, aindex);
4079 case MINT_LDELEM_R8:
4080 sp [0].data.f = mono_array_get (o, double, aindex);
4082 case MINT_LDELEM_REF:
4083 sp [0].data.p = mono_array_get (o, gpointer, aindex);
4085 case MINT_LDELEM_VT: {
4086 MonoClass *klass_vt = rtm->data_items [*(guint16 *) (ip + 1)];
4087 i32 = READ32 (ip + 2);
4088 char *src_addr = mono_array_addr_with_size ((MonoArray *) o, i32, aindex);
4089 sp [0].data.vt = vt_sp;
4090 stackval_from_data (&klass_vt->byval_arg, sp, src_addr, FALSE);
4091 vt_sp += (i32 + 7) & ~7;
4103 MINT_IN_CASE(MINT_STELEM_I) /* fall through */
4104 MINT_IN_CASE(MINT_STELEM_I1) /* fall through */
4105 MINT_IN_CASE(MINT_STELEM_U1) /* fall through */
4106 MINT_IN_CASE(MINT_STELEM_I2) /* fall through */
4107 MINT_IN_CASE(MINT_STELEM_U2) /* fall through */
4108 MINT_IN_CASE(MINT_STELEM_I4) /* fall through */
4109 MINT_IN_CASE(MINT_STELEM_I8) /* fall through */
4110 MINT_IN_CASE(MINT_STELEM_R4) /* fall through */
4111 MINT_IN_CASE(MINT_STELEM_R8) /* fall through */
4112 MINT_IN_CASE(MINT_STELEM_REF) /* fall through */
4113 MINT_IN_CASE(MINT_STELEM_VT) {
4120 THROW_EX (mono_get_exception_null_reference (), ip);
4122 aindex = sp [1].data.i;
4123 if (aindex >= mono_array_length ((MonoArray *)o))
4124 THROW_EX (mono_get_exception_index_out_of_range (), ip);
4128 mono_array_set ((MonoArray *)o, mono_i, aindex, sp [2].data.nati);
4130 case MINT_STELEM_I1:
4131 mono_array_set ((MonoArray *)o, gint8, aindex, sp [2].data.i);
4133 case MINT_STELEM_U1:
4134 mono_array_set ((MonoArray *) o, guint8, aindex, sp [2].data.i);
4136 case MINT_STELEM_I2:
4137 mono_array_set ((MonoArray *)o, gint16, aindex, sp [2].data.i);
4139 case MINT_STELEM_U2:
4140 mono_array_set ((MonoArray *)o, guint16, aindex, sp [2].data.i);
4142 case MINT_STELEM_I4:
4143 mono_array_set ((MonoArray *)o, gint32, aindex, sp [2].data.i);
4145 case MINT_STELEM_I8:
4146 mono_array_set ((MonoArray *)o, gint64, aindex, sp [2].data.l);
4148 case MINT_STELEM_R4:
4149 mono_array_set ((MonoArray *)o, float, aindex, sp [2].data.f);
4151 case MINT_STELEM_R8:
4152 mono_array_set ((MonoArray *)o, double, aindex, sp [2].data.f);
4154 case MINT_STELEM_REF: {
4155 MonoObject *isinst_obj = mono_object_isinst_checked (sp [2].data.p, mono_object_class (o)->element_class, &error);
4156 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4157 if (sp [2].data.p && !isinst_obj)
4158 THROW_EX (mono_get_exception_array_type_mismatch (), ip);
4159 mono_array_setref ((MonoArray *) o, aindex, sp [2].data.p);
4162 case MINT_STELEM_VT: {
4163 MonoClass *klass_vt = rtm->data_items [*(guint16 *) (ip + 1)];
4164 i32 = READ32 (ip + 2);
4165 char *dst_addr = mono_array_addr_with_size ((MonoArray *) o, i32, aindex);
4167 stackval_to_data (&klass_vt->byval_arg, &sp [2], dst_addr, FALSE);
4168 vt_sp -= (i32 + 7) & ~7;
4179 MINT_IN_CASE(MINT_CONV_OVF_I4_U4)
4180 if (sp [-1].data.i < 0)
4181 THROW_EX (mono_get_exception_overflow (), ip);
4184 MINT_IN_CASE(MINT_CONV_OVF_I4_I8)
4185 if (sp [-1].data.l < MYGINT32_MIN || sp [-1].data.l > MYGINT32_MAX)
4186 THROW_EX (mono_get_exception_overflow (), ip);
4187 sp [-1].data.i = (gint32) sp [-1].data.l;
4190 MINT_IN_CASE(MINT_CONV_OVF_I4_U8)
4191 if (sp [-1].data.l < 0 || sp [-1].data.l > MYGINT32_MAX)
4192 THROW_EX (mono_get_exception_overflow (), ip);
4193 sp [-1].data.i = (gint32) sp [-1].data.l;
4196 MINT_IN_CASE(MINT_CONV_OVF_I4_R8)
4197 if (sp [-1].data.f < MYGINT32_MIN || sp [-1].data.f > MYGINT32_MAX)
4198 THROW_EX (mono_get_exception_overflow (), ip);
4199 sp [-1].data.i = (gint32) sp [-1].data.f;
4202 MINT_IN_CASE(MINT_CONV_OVF_U4_I4)
4203 if (sp [-1].data.i < 0)
4204 THROW_EX (mono_get_exception_overflow (), ip);
4207 MINT_IN_CASE(MINT_CONV_OVF_U4_I8)
4208 if (sp [-1].data.l < 0 || sp [-1].data.l > MYGUINT32_MAX)
4209 THROW_EX (mono_get_exception_overflow (), ip);
4210 sp [-1].data.i = (guint32) sp [-1].data.l;
4213 MINT_IN_CASE(MINT_CONV_OVF_U4_R8)
4214 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
4215 THROW_EX (mono_get_exception_overflow (), ip);
4216 sp [-1].data.i = (guint32) sp [-1].data.f;
4219 MINT_IN_CASE(MINT_CONV_OVF_I2_I4)
4220 if (sp [-1].data.i < -32768 || sp [-1].data.i > 32767)
4221 THROW_EX (mono_get_exception_overflow (), ip);
4224 MINT_IN_CASE(MINT_CONV_OVF_I2_I8)
4225 if (sp [-1].data.l < -32768 || sp [-1].data.l > 32767)
4226 THROW_EX (mono_get_exception_overflow (), ip);
4227 sp [-1].data.i = (gint16) sp [-1].data.l;
4230 MINT_IN_CASE(MINT_CONV_OVF_I2_R8)
4231 if (sp [-1].data.f < -32768 || sp [-1].data.f > 32767)
4232 THROW_EX (mono_get_exception_overflow (), ip);
4233 sp [-1].data.i = (gint16) sp [-1].data.f;
4236 MINT_IN_CASE(MINT_CONV_OVF_U2_I4)
4237 if (sp [-1].data.i < 0 || sp [-1].data.i > 65535)
4238 THROW_EX (mono_get_exception_overflow (), ip);
4241 MINT_IN_CASE(MINT_CONV_OVF_U2_I8)
4242 if (sp [-1].data.l < 0 || sp [-1].data.l > 65535)
4243 THROW_EX (mono_get_exception_overflow (), ip);
4244 sp [-1].data.i = (guint16) sp [-1].data.l;
4247 MINT_IN_CASE(MINT_CONV_OVF_U2_R8)
4248 if (sp [-1].data.f < 0 || sp [-1].data.f > 65535)
4249 THROW_EX (mono_get_exception_overflow (), ip);
4250 sp [-1].data.i = (guint16) sp [-1].data.f;
4253 MINT_IN_CASE(MINT_CONV_OVF_I1_I4)
4254 if (sp [-1].data.i < -128 || sp [-1].data.i > 127)
4255 THROW_EX (mono_get_exception_overflow (), ip);
4258 MINT_IN_CASE(MINT_CONV_OVF_I1_I8)
4259 if (sp [-1].data.l < -128 || sp [-1].data.l > 127)
4260 THROW_EX (mono_get_exception_overflow (), ip);
4261 sp [-1].data.i = (gint8) sp [-1].data.l;
4264 MINT_IN_CASE(MINT_CONV_OVF_I1_R8)
4265 if (sp [-1].data.f < -128 || sp [-1].data.f > 127)
4266 THROW_EX (mono_get_exception_overflow (), ip);
4267 sp [-1].data.i = (gint8) sp [-1].data.f;
4270 MINT_IN_CASE(MINT_CONV_OVF_U1_I4)
4271 if (sp [-1].data.i < 0 || sp [-1].data.i > 255)
4272 THROW_EX (mono_get_exception_overflow (), ip);
4275 MINT_IN_CASE(MINT_CONV_OVF_U1_I8)
4276 if (sp [-1].data.l < 0 || sp [-1].data.l > 255)
4277 THROW_EX (mono_get_exception_overflow (), ip);
4278 sp [-1].data.i = (guint8) sp [-1].data.l;
4281 MINT_IN_CASE(MINT_CONV_OVF_U1_R8)
4282 if (sp [-1].data.f < 0 || sp [-1].data.f > 255)
4283 THROW_EX (mono_get_exception_overflow (), ip);
4284 sp [-1].data.i = (guint8) sp [-1].data.f;
4288 MINT_IN_CASE(MINT_LDELEM)
4289 MINT_IN_CASE(MINT_STELEM)
4290 MINT_IN_CASE(MINT_UNBOX_ANY)
4292 MINT_IN_CASE(MINT_REFANYVAL) ves_abort(); MINT_IN_BREAK;
4294 MINT_IN_CASE(MINT_CKFINITE)
4295 if (!isfinite(sp [-1].data.f))
4296 THROW_EX (mono_get_exception_arithmetic (), ip);
4300 MINT_IN_CASE(MINT_MKREFANY) ves_abort(); MINT_IN_BREAK;
4302 MINT_IN_CASE(MINT_LDTOKEN)
4305 * (gpointer *)sp->data.p = rtm->data_items[*(guint16 *)(ip + 1)];
4309 MINT_IN_CASE(MINT_ADD_OVF_I4)
4310 if (CHECK_ADD_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
4311 THROW_EX (mono_get_exception_overflow (), ip);
4314 MINT_IN_CASE(MINT_ADD_OVF_I8)
4315 if (CHECK_ADD_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
4316 THROW_EX (mono_get_exception_overflow (), ip);
4319 MINT_IN_CASE(MINT_ADD_OVF_UN_I4)
4320 if (CHECK_ADD_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
4321 THROW_EX (mono_get_exception_overflow (), ip);
4322 BINOP_CAST(i, +, guint32);
4324 MINT_IN_CASE(MINT_ADD_OVF_UN_I8)
4325 if (CHECK_ADD_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
4326 THROW_EX (mono_get_exception_overflow (), ip);
4327 BINOP_CAST(l, +, guint64);
4329 MINT_IN_CASE(MINT_MUL_OVF_I4)
4330 if (CHECK_MUL_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
4331 THROW_EX (mono_get_exception_overflow (), ip);
4334 MINT_IN_CASE(MINT_MUL_OVF_I8)
4335 if (CHECK_MUL_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
4336 THROW_EX (mono_get_exception_overflow (), ip);
4339 MINT_IN_CASE(MINT_MUL_OVF_UN_I4)
4340 if (CHECK_MUL_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
4341 THROW_EX (mono_get_exception_overflow (), ip);
4342 BINOP_CAST(i, *, guint32);
4344 MINT_IN_CASE(MINT_MUL_OVF_UN_I8)
4345 if (CHECK_MUL_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
4346 THROW_EX (mono_get_exception_overflow (), ip);
4347 BINOP_CAST(l, *, guint64);
4349 MINT_IN_CASE(MINT_SUB_OVF_I4)
4350 if (CHECK_SUB_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
4351 THROW_EX (mono_get_exception_overflow (), ip);
4354 MINT_IN_CASE(MINT_SUB_OVF_I8)
4355 if (CHECK_SUB_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
4356 THROW_EX (mono_get_exception_overflow (), ip);
4359 MINT_IN_CASE(MINT_SUB_OVF_UN_I4)
4360 if (CHECK_SUB_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
4361 THROW_EX (mono_get_exception_overflow (), ip);
4362 BINOP_CAST(i, -, guint32);
4364 MINT_IN_CASE(MINT_SUB_OVF_UN_I8)
4365 if (CHECK_SUB_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
4366 THROW_EX (mono_get_exception_overflow (), ip);
4367 BINOP_CAST(l, -, guint64);
4369 MINT_IN_CASE(MINT_ENDFINALLY)
4371 int clause_index = *ip;
4372 if (clause_index == exit_at_finally)
4374 while (sp > frame->stack) {
4378 ip = finally_ips->data;
4379 finally_ips = g_slist_remove (finally_ips, ip);
4386 MINT_IN_CASE(MINT_LEAVE) /* Fall through */
4387 MINT_IN_CASE(MINT_LEAVE_S)
4388 while (sp > frame->stack) {
4392 if (*ip == MINT_LEAVE_S) {
4393 ip += (short) *(ip + 1);
4395 ip += (gint32) READ32 (ip + 1);
4398 if (frame->ex_handler != NULL && MONO_OFFSET_IN_HANDLER(frame->ex_handler, frame->ip - rtm->code)) {
4399 frame->ex_handler = NULL;
4402 goto handle_finally;
4404 MINT_IN_CASE(MINT_ICALL_V_V)
4405 MINT_IN_CASE(MINT_ICALL_V_P)
4406 MINT_IN_CASE(MINT_ICALL_P_V)
4407 MINT_IN_CASE(MINT_ICALL_P_P)
4408 MINT_IN_CASE(MINT_ICALL_PP_V)
4409 MINT_IN_CASE(MINT_ICALL_PI_V)
4410 MINT_IN_CASE(MINT_ICALL_PP_P)
4411 MINT_IN_CASE(MINT_ICALL_PI_P)
4412 MINT_IN_CASE(MINT_ICALL_PPP_V)
4413 MINT_IN_CASE(MINT_ICALL_PPI_V)
4414 sp = do_icall (context, *ip, sp, rtm->data_items [*(guint16 *)(ip + 1)]);
4415 if (*mono_thread_interruption_request_flag ()) {
4416 MonoException *exc = mono_thread_interruption_checkpoint ();
4419 context->search_for_handler = 1;
4422 if (frame->ex != NULL)
4423 goto handle_exception;
4426 MINT_IN_CASE(MINT_MONO_LDPTR)
4427 sp->data.p = rtm->data_items [*(guint16 *)(ip + 1)];
4431 MINT_IN_CASE(MINT_MONO_NEWOBJ)
4432 sp->data.p = mono_object_new_checked (context->domain, rtm->data_items [*(guint16 *)(ip + 1)], &error);
4433 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4437 MINT_IN_CASE(MINT_MONO_FREE)
4440 g_error ("that doesn't seem right");
4441 g_free (sp->data.p);
4443 MINT_IN_CASE(MINT_MONO_RETOBJ)
4446 stackval_from_data (mono_method_signature (frame->runtime_method->method)->ret, frame->retval, sp->data.p,
4447 mono_method_signature (frame->runtime_method->method)->pinvoke);
4448 if (sp > frame->stack)
4449 g_warning ("retobj: more values on stack: %d", sp-frame->stack);
4451 MINT_IN_CASE(MINT_MONO_TLS) {
4452 MonoTlsKey key = *(gint32 *)(ip + 1);
4453 sp->data.p = ((gpointer (*)()) mono_tls_get_tls_getter (key, FALSE)) ();
4458 MINT_IN_CASE(MINT_MONO_JIT_ATTACH) {
4461 context->original_domain = NULL;
4462 MonoDomain *tls_domain = (MonoDomain *) ((gpointer (*)()) mono_tls_get_tls_getter (TLS_KEY_DOMAIN, FALSE)) ();
4463 gpointer tls_jit = ((gpointer (*)()) mono_tls_get_tls_getter (TLS_KEY_DOMAIN, FALSE)) ();
4465 if (tls_domain != context->domain || !tls_jit)
4466 context->original_domain = mono_jit_thread_attach (context->domain);
4469 MINT_IN_CASE(MINT_MONO_JIT_DETACH)
4471 mono_jit_set_domain (context->original_domain);
4474 #define RELOP(datamem, op) \
4476 sp [-1].data.i = sp [-1].data.datamem op sp [0].data.datamem; \
4478 MINT_IN_CASE(MINT_CEQ_I4)
4481 MINT_IN_CASE(MINT_CEQ0_I4)
4482 sp [-1].data.i = (sp [-1].data.i == 0);
4485 MINT_IN_CASE(MINT_CEQ_I8)
4488 MINT_IN_CASE(MINT_CEQ_R8)
4490 if (isunordered (sp [-1].data.f, sp [0].data.f))
4493 sp [-1].data.i = sp [-1].data.f == sp [0].data.f;
4496 MINT_IN_CASE(MINT_CGT_I4)
4499 MINT_IN_CASE(MINT_CGT_I8)
4502 MINT_IN_CASE(MINT_CGT_R8)
4504 if (isunordered (sp [-1].data.f, sp [0].data.f))
4507 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
4511 #define RELOP_CAST(datamem, op, type) \
4513 sp [-1].data.i = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
4516 MINT_IN_CASE(MINT_CGT_UN_I4)
4517 RELOP_CAST(i, >, guint32);
4519 MINT_IN_CASE(MINT_CGT_UN_I8)
4520 RELOP_CAST(l, >, guint64);
4522 MINT_IN_CASE(MINT_CGT_UN_R8)
4524 if (isunordered (sp [-1].data.f, sp [0].data.f))
4527 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
4530 MINT_IN_CASE(MINT_CLT_I4)
4533 MINT_IN_CASE(MINT_CLT_I8)
4536 MINT_IN_CASE(MINT_CLT_R8)
4538 if (isunordered (sp [-1].data.f, sp [0].data.f))
4541 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
4544 MINT_IN_CASE(MINT_CLT_UN_I4)
4545 RELOP_CAST(i, <, guint32);
4547 MINT_IN_CASE(MINT_CLT_UN_I8)
4548 RELOP_CAST(l, <, guint64);
4550 MINT_IN_CASE(MINT_CLT_UN_R8)
4552 if (isunordered (sp [-1].data.f, sp [0].data.f))
4555 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
4558 MINT_IN_CASE(MINT_LDFTN) {
4559 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
4564 MINT_IN_CASE(MINT_LDVIRTFTN) {
4565 RuntimeMethod *m = rtm->data_items [* (guint16 *)(ip + 1)];
4569 THROW_EX (mono_get_exception_null_reference (), ip - 2);
4571 sp->data.p = get_virtual_method (context->domain, m, sp->data.p);
4576 #define LDARG(datamem, argtype) \
4577 sp->data.datamem = * (argtype *)(frame->args + * (guint16 *)(ip + 1)); \
4581 MINT_IN_CASE(MINT_LDARG_I1) LDARG(i, gint8); MINT_IN_BREAK;
4582 MINT_IN_CASE(MINT_LDARG_U1) LDARG(i, guint8); MINT_IN_BREAK;
4583 MINT_IN_CASE(MINT_LDARG_I2) LDARG(i, gint16); MINT_IN_BREAK;
4584 MINT_IN_CASE(MINT_LDARG_U2) LDARG(i, guint16); MINT_IN_BREAK;
4585 MINT_IN_CASE(MINT_LDARG_I4) LDARG(i, gint32); MINT_IN_BREAK;
4586 MINT_IN_CASE(MINT_LDARG_I8) LDARG(l, gint64); MINT_IN_BREAK;
4587 MINT_IN_CASE(MINT_LDARG_R4) LDARG(f, float); MINT_IN_BREAK;
4588 MINT_IN_CASE(MINT_LDARG_R8) LDARG(f, double); MINT_IN_BREAK;
4589 MINT_IN_CASE(MINT_LDARG_O) LDARG(p, gpointer); MINT_IN_BREAK;
4590 MINT_IN_CASE(MINT_LDARG_P) LDARG(p, gpointer); MINT_IN_BREAK;
4592 MINT_IN_CASE(MINT_LDARG_VT)
4594 i32 = READ32(ip + 2);
4595 memcpy(sp->data.p, frame->args + * (guint16 *)(ip + 1), i32);
4596 vt_sp += (i32 + 7) & ~7;
4601 #define STARG(datamem, argtype) \
4603 * (argtype *)(frame->args + * (guint16 *)(ip + 1)) = sp->data.datamem; \
4606 MINT_IN_CASE(MINT_STARG_I1) STARG(i, gint8); MINT_IN_BREAK;
4607 MINT_IN_CASE(MINT_STARG_U1) STARG(i, guint8); MINT_IN_BREAK;
4608 MINT_IN_CASE(MINT_STARG_I2) STARG(i, gint16); MINT_IN_BREAK;
4609 MINT_IN_CASE(MINT_STARG_U2) STARG(i, guint16); MINT_IN_BREAK;
4610 MINT_IN_CASE(MINT_STARG_I4) STARG(i, gint32); MINT_IN_BREAK;
4611 MINT_IN_CASE(MINT_STARG_I8) STARG(l, gint64); MINT_IN_BREAK;
4612 MINT_IN_CASE(MINT_STARG_R4) STARG(f, float); MINT_IN_BREAK;
4613 MINT_IN_CASE(MINT_STARG_R8) STARG(f, double); MINT_IN_BREAK;
4614 MINT_IN_CASE(MINT_STARG_O) STARG(p, gpointer); MINT_IN_BREAK;
4615 MINT_IN_CASE(MINT_STARG_P) STARG(p, gpointer); MINT_IN_BREAK;
4617 MINT_IN_CASE(MINT_STARG_VT)
4618 i32 = READ32(ip + 2);
4620 memcpy(frame->args + * (guint16 *)(ip + 1), sp->data.p, i32);
4621 vt_sp -= (i32 + 7) & ~7;
4625 #define STINARG(datamem, argtype) \
4627 int n = * (guint16 *)(ip + 1); \
4628 * (argtype *)(frame->args + rtm->arg_offsets [n]) = frame->stack_args [n].data.datamem; \
4632 MINT_IN_CASE(MINT_STINARG_I1) STINARG(i, gint8); MINT_IN_BREAK;
4633 MINT_IN_CASE(MINT_STINARG_U1) STINARG(i, guint8); MINT_IN_BREAK;
4634 MINT_IN_CASE(MINT_STINARG_I2) STINARG(i, gint16); MINT_IN_BREAK;
4635 MINT_IN_CASE(MINT_STINARG_U2) STINARG(i, guint16); MINT_IN_BREAK;
4636 MINT_IN_CASE(MINT_STINARG_I4) STINARG(i, gint32); MINT_IN_BREAK;
4637 MINT_IN_CASE(MINT_STINARG_I8) STINARG(l, gint64); MINT_IN_BREAK;
4638 MINT_IN_CASE(MINT_STINARG_R4) STINARG(f, float); MINT_IN_BREAK;
4639 MINT_IN_CASE(MINT_STINARG_R8) STINARG(f, double); MINT_IN_BREAK;
4640 MINT_IN_CASE(MINT_STINARG_O) STINARG(p, gpointer); MINT_IN_BREAK;
4641 MINT_IN_CASE(MINT_STINARG_P) STINARG(p, gpointer); MINT_IN_BREAK;
4643 MINT_IN_CASE(MINT_STINARG_VT) {
4644 int n = * (guint16 *)(ip + 1);
4645 i32 = READ32(ip + 2);
4646 memcpy (frame->args + rtm->arg_offsets [n], frame->stack_args [n].data.p, i32);
4651 MINT_IN_CASE(MINT_LDARGA)
4652 sp->data.p = frame->args + * (guint16 *)(ip + 1);
4657 #define LDLOC(datamem, argtype) \
4658 sp->data.datamem = * (argtype *)(locals + * (guint16 *)(ip + 1)); \
4662 MINT_IN_CASE(MINT_LDLOC_I1) LDLOC(i, gint8); MINT_IN_BREAK;
4663 MINT_IN_CASE(MINT_LDLOC_U1) LDLOC(i, guint8); MINT_IN_BREAK;
4664 MINT_IN_CASE(MINT_LDLOC_I2) LDLOC(i, gint16); MINT_IN_BREAK;
4665 MINT_IN_CASE(MINT_LDLOC_U2) LDLOC(i, guint16); MINT_IN_BREAK;
4666 MINT_IN_CASE(MINT_LDLOC_I4) LDLOC(i, gint32); MINT_IN_BREAK;
4667 MINT_IN_CASE(MINT_LDLOC_I8) LDLOC(l, gint64); MINT_IN_BREAK;
4668 MINT_IN_CASE(MINT_LDLOC_R4) LDLOC(f, float); MINT_IN_BREAK;
4669 MINT_IN_CASE(MINT_LDLOC_R8) LDLOC(f, double); MINT_IN_BREAK;
4670 MINT_IN_CASE(MINT_LDLOC_O) LDLOC(p, gpointer); MINT_IN_BREAK;
4671 MINT_IN_CASE(MINT_LDLOC_P) LDLOC(p, gpointer); MINT_IN_BREAK;
4673 MINT_IN_CASE(MINT_LDLOC_VT)
4675 i32 = READ32(ip + 2);
4676 memcpy(sp->data.p, locals + * (guint16 *)(ip + 1), i32);
4677 vt_sp += (i32 + 7) & ~7;
4682 MINT_IN_CASE(MINT_LDLOCA_S)
4683 sp->data.p = locals + * (guint16 *)(ip + 1);
4688 #define STLOC(datamem, argtype) \
4690 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp->data.datamem; \
4693 MINT_IN_CASE(MINT_STLOC_I1) STLOC(i, gint8); MINT_IN_BREAK;
4694 MINT_IN_CASE(MINT_STLOC_U1) STLOC(i, guint8); MINT_IN_BREAK;
4695 MINT_IN_CASE(MINT_STLOC_I2) STLOC(i, gint16); MINT_IN_BREAK;
4696 MINT_IN_CASE(MINT_STLOC_U2) STLOC(i, guint16); MINT_IN_BREAK;
4697 MINT_IN_CASE(MINT_STLOC_I4) STLOC(i, gint32); MINT_IN_BREAK;
4698 MINT_IN_CASE(MINT_STLOC_I8) STLOC(l, gint64); MINT_IN_BREAK;
4699 MINT_IN_CASE(MINT_STLOC_R4) STLOC(f, float); MINT_IN_BREAK;
4700 MINT_IN_CASE(MINT_STLOC_R8) STLOC(f, double); MINT_IN_BREAK;
4701 MINT_IN_CASE(MINT_STLOC_O) STLOC(p, gpointer); MINT_IN_BREAK;
4702 MINT_IN_CASE(MINT_STLOC_P) STLOC(p, gpointer); MINT_IN_BREAK;
4704 #define STLOC_NP(datamem, argtype) \
4705 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp [-1].data.datamem; \
4708 MINT_IN_CASE(MINT_STLOC_NP_I4) STLOC_NP(i, gint32); MINT_IN_BREAK;
4709 MINT_IN_CASE(MINT_STLOC_NP_O) STLOC_NP(p, gpointer); MINT_IN_BREAK;
4711 MINT_IN_CASE(MINT_STLOC_VT)
4712 i32 = READ32(ip + 2);
4714 memcpy(locals + * (guint16 *)(ip + 1), sp->data.p, i32);
4715 vt_sp -= (i32 + 7) & ~7;
4719 MINT_IN_CASE(MINT_LOCALLOC) {
4720 if (sp != frame->stack + 1) /*FIX?*/
4721 THROW_EX (mono_get_exception_execution_engine (NULL), ip);
4723 int len = sp [-1].data.i;
4724 sp [-1].data.p = alloca (len);
4725 MonoMethodHeader *header = mono_method_get_header_checked (frame->runtime_method->method, &error);
4726 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4727 if (header && header->init_locals)
4728 memset (sp [-1].data.p, 0, len);
4732 MINT_IN_CASE(MINT_ENDFILTER)
4733 /* top of stack is result of filter */
4734 frame->retval = &sp [-1];
4736 MINT_IN_CASE(MINT_INITOBJ)
4738 memset (sp->data.vt, 0, READ32(ip + 1));
4741 MINT_IN_CASE(MINT_CPBLK)
4743 if (!sp [0].data.p || !sp [1].data.p)
4744 THROW_EX (mono_get_exception_null_reference(), ip - 1);
4746 /* FIXME: value and size may be int64... */
4747 memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
4750 MINT_IN_CASE(MINT_CONSTRAINED_) {
4752 /* FIXME: implement */
4754 token = READ32 (ip);
4759 MINT_IN_CASE(MINT_INITBLK)
4762 THROW_EX (mono_get_exception_null_reference(), ip - 1);
4764 /* FIXME: value and size may be int64... */
4765 memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
4768 MINT_IN_CASE(MINT_NO_)
4769 /* FIXME: implement */
4773 MINT_IN_CASE(MINT_RETHROW)
4775 * need to clarify what this should actually do:
4776 * start the search from the last found handler in
4777 * this method or continue in the caller or what.
4778 * Also, do we need to run finally/fault handlers after a retrow?
4779 * Well, this implementation will follow the usual search
4780 * for an handler, considering the current ip as throw spot.
4781 * We need to NULL frame->ex_handler for the later code to
4782 * actually run the new found handler.
4784 frame->ex_handler = NULL;
4785 THROW_EX (frame->ex, ip - 1);
4788 g_print ("Unimplemented opcode: %04x %s at 0x%x\n", *ip, mono_interp_opname[*ip], ip-rtm->code);
4789 THROW_EX (mono_get_exception_execution_engine ("Unimplemented opcode"), ip);
4793 g_assert_not_reached ();
4795 * Exception handling code.
4796 * The exception object is stored in frame->ex.
4803 MonoInvocation *inv;
4804 MonoExceptionClause *clause;
4810 g_print ("* Handling exception '%s' at IL_%04x\n",
4811 frame->ex == NULL ? "** Unknown **" : mono_object_class (frame->ex)->name,
4812 rtm == NULL ? 0 : frame->ip - rtm->code);
4814 if (die_on_exception)
4817 for (inv = frame; inv; inv = inv->parent) {
4819 if (inv->runtime_method == NULL)
4821 method = inv->runtime_method->method;
4822 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
4824 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
4826 if (inv->ip == NULL)
4828 ip_offset = inv->ip - inv->runtime_method->code;
4829 inv->ex_handler = NULL; /* clear this in case we are trhowing an exception while handling one - this one wins */
4830 for (i = 0; i < inv->runtime_method->num_clauses; ++i) {
4831 clause = &inv->runtime_method->clauses [i];
4833 g_print ("* clause [%d]: %p\n", i, clause);
4835 if (!MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4838 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4841 g_print ("* Filter found at '%s'\n", method->name);
4843 MonoInvocation dup_frame;
4845 memcpy (&dup_frame, inv, sizeof (MonoInvocation));
4846 dup_frame.retval = &retval;
4847 ves_exec_method_with_context (&dup_frame, context, inv->runtime_method->code + clause->data.filter_offset, frame->ex, -1);
4848 if (dup_frame.retval->data.i) {
4851 g_print ("* Matched Filter at '%s'\n", method->name);
4853 inv->ex_handler = clause;
4854 goto handle_finally;
4856 } else if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
4857 MonoObject *isinst_obj = mono_object_isinst_checked ((MonoObject*)frame->ex, clause->data.catch_class, &error);
4858 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4861 * OK, we found an handler, now we need to execute the finally
4862 * and fault blocks before branching to the handler code.
4866 g_print ("* Found handler at '%s'\n", method->name);
4868 inv->ex_handler = clause;
4869 goto handle_finally;
4875 * If we get here, no handler was found: print a stack trace.
4877 for (inv = frame; inv; inv = inv->parent) {
4878 if (inv->invoke_trap)
4879 goto handle_finally;
4882 ex_obj = (MonoObject *) frame->ex;
4883 mono_unhandled_exception (ex_obj);
4884 MonoJitTlsData *jit_tls = (MonoJitTlsData *) mono_tls_get_jit_tls ();
4885 jit_tls->abort_func (ex_obj);
4886 g_assert_not_reached ();
4892 MonoExceptionClause *clause;
4893 GSList *old_list = finally_ips;
4894 MonoMethod *method = frame->runtime_method->method;
4895 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
4896 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4900 g_print ("* Handle finally IL_%04x\n", endfinally_ip == NULL ? 0 : endfinally_ip - rtm->code);
4902 if (rtm == NULL || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
4903 || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
4906 ip_offset = frame->ip - rtm->code;
4908 if (endfinally_ip != NULL)
4909 finally_ips = g_slist_prepend(finally_ips, (void *)endfinally_ip);
4910 for (i = 0; i < header->num_clauses; ++i)
4911 if (frame->ex_handler == &rtm->clauses [i])
4915 clause = &rtm->clauses [i];
4916 if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && (endfinally_ip == NULL || !(MONO_OFFSET_IN_CLAUSE (clause, endfinally_ip - rtm->code)))) {
4917 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
4918 ip = rtm->code + clause->handler_offset;
4919 finally_ips = g_slist_prepend (finally_ips, (gpointer) ip);
4922 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
4928 endfinally_ip = NULL;
4930 if (old_list != finally_ips && finally_ips) {
4931 ip = finally_ips->data;
4932 finally_ips = g_slist_remove (finally_ips, ip);
4933 sp = frame->stack; /* spec says stack should be empty at endfinally so it should be at the start too */
4938 * If an exception is set, we need to execute the fault handler, too,
4939 * otherwise, we continue normally.
4949 MonoExceptionClause *clause;
4950 MonoMethod *method = frame->runtime_method->method;
4951 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
4952 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4956 g_print ("* Handle fault\n");
4958 ip_offset = frame->ip - rtm->code;
4959 for (i = 0; i < header->num_clauses; ++i) {
4960 clause = &rtm->clauses [i];
4961 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4962 ip = rtm->code + clause->handler_offset;
4965 g_print ("* Executing handler at IL_%04x\n", clause->handler_offset);
4971 * If the handler for the exception was found in this method, we jump
4972 * to it right away, otherwise we return and let the caller run
4973 * the finally, fault and catch blocks.
4974 * This same code should be present in the endfault opcode, but it
4975 * is corrently not assigned in the ECMA specs: LAMESPEC.
4977 if (frame->ex_handler) {
4980 g_print ("* Executing handler at IL_%04x\n", frame->ex_handler->handler_offset);
4982 ip = rtm->code + frame->ex_handler->handler_offset;
4984 vt_sp = (unsigned char *) sp + rtm->stack_size;
4985 sp->data.p = frame->ex;
4996 ves_exec_method (MonoInvocation *frame)
4998 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4999 ThreadContext context_struct;
5006 mono_unhandled_exception ((MonoObject*)frame->ex);
5009 if (context == NULL) {
5010 context = &context_struct;
5011 context_struct.domain = mono_domain_get ();
5012 context_struct.base_frame = frame;
5013 context_struct.current_frame = NULL;
5014 context_struct.env_frame = frame;
5015 context_struct.current_env = &env;
5016 context_struct.search_for_handler = 0;
5017 context_struct.managed_code = 0;
5018 mono_native_tls_set_value (thread_context_id, context);
5021 frame->parent = context->current_frame;
5022 frame->runtime_method = mono_interp_get_runtime_method (context->domain, frame->method, &error);
5023 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
5024 context->managed_code = 1;
5025 ves_exec_method_with_context (frame, context, NULL, NULL, -1);
5026 context->managed_code = 0;
5028 if (context != &context_struct && context->current_env) {
5029 context->env_frame->ex = frame->ex;
5030 longjmp (*context->current_env, 1);
5033 mono_unhandled_exception ((MonoObject*)frame->ex);
5035 if (context->base_frame == frame)
5036 mono_native_tls_set_value (thread_context_id, NULL);
5038 context->current_frame = frame->parent;
5042 ves_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
5044 MonoImage *image = mono_assembly_get_image (assembly);
5049 method = mono_get_method_checked (image, mono_image_get_entry_point (image), NULL, NULL, &error);
5050 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
5053 g_error ("No entry point method found in %s", mono_image_get_filename (image));
5055 rval = mono_runtime_run_main_checked (method, argc, argv, &error);
5056 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
5064 "mint %s, the Mono ECMA CLI interpreter, (C) 2001, 2002 Ximian, Inc.\n\n"
5065 "Usage is: mint [options] executable args...\n\n", VERSION);
5067 "Runtime Debugging:\n"
5072 " --noptr\t\t\tdon't print pointer addresses in trace output\n"
5075 " --traceclassinit\n"
5078 " --debug method_name\n"
5085 " --config filename load the specified config file instead of the default\n"
5086 " --workers n maximum number of worker threads\n"
5092 interp_ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
5093 MonoReflectionMethod **method,
5094 gint32 *iloffset, gint32 *native_offset,
5095 MonoString **file, gint32 *line, gint32 *column)
5097 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
5098 MonoInvocation *inv = context->current_frame;
5102 for (i = 0; inv && i < skip; inv = inv->parent)
5103 if (inv->runtime_method != NULL)
5114 *method = mono_method_get_object_checked (context->domain, inv->runtime_method->method, NULL, &error);
5115 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
5120 if (need_file_info) {
5124 *file = mono_string_new (mono_domain_get (), "unknown");
5131 interp_ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
5133 MonoDomain *domain = mono_domain_get ();
5135 MonoArray *ta = exc->trace_ips;
5140 /* Exception is not thrown yet */
5141 MonoArray *array = mono_array_new_checked (domain, mono_defaults.stack_frame_class, 0, &error);
5142 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
5146 len = mono_array_length (ta);
5148 res = mono_array_new_checked (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0, &error);
5149 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
5151 for (i = skip; i < len / 2; i++) {
5152 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new_checked (domain, mono_defaults.stack_frame_class, &error);
5153 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
5154 gushort *ip = mono_array_get (ta, gpointer, 2 * i + 1);
5155 RuntimeMethod *rtm = mono_array_get (ta, gpointer, 2 * i);
5158 sf->method = mono_method_get_object_checked (domain, rtm->method, NULL, &error);
5159 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
5160 sf->native_offset = ip - rtm->code;
5164 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
5166 if (need_file_info) {
5169 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
5171 sf->filename = filename? mono_string_new (domain, filename): NULL;
5178 mono_array_set (res, gpointer, i, sf);
5185 mono_interp_parse_options (const char *options)
5189 args = g_strsplit (options, ",", -1);
5190 for (ptr = args; ptr && *ptr; ptr ++) {
5193 if (strncmp (arg, "jit=", 4) == 0)
5194 jit_classes = g_slist_prepend (jit_classes, arg + 4);
5201 mono_native_tls_alloc (&thread_context_id, NULL);
5202 mono_native_tls_set_value (thread_context_id, NULL);
5204 mono_interp_transform_init ();
5207 typedef int (*TestMethod) (void);
5210 interp_regression_step (MonoImage *image, int verbose, int *total_run, int *total, GTimer *timer, MonoDomain *domain)
5212 int result, expected, failed, cfailed, run;
5213 double elapsed, transform_time;
5215 MonoObject *result_obj;
5216 static gboolean filter_method_init = FALSE;
5217 static const char *filter_method = NULL;
5219 g_print ("Test run: image=%s\n", mono_image_get_filename (image));
5220 cfailed = failed = run = 0;
5221 transform_time = elapsed = 0.0;
5223 g_timer_start (timer);
5224 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
5225 MonoObject *exc = NULL;
5227 MonoMethod *method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
5229 mono_error_cleanup (&error); /* FIXME don't swallow the error */
5233 if (!filter_method_init) {
5234 filter_method = g_getenv ("INTERP_FILTER_METHOD");
5235 filter_method_init = TRUE;
5237 gboolean filter = FALSE;
5238 if (filter_method) {
5239 const char *name = filter_method;
5241 if ((strchr (name, '.') > name) || strchr (name, ':')) {
5242 MonoMethodDesc *desc = mono_method_desc_new (name, TRUE);
5243 filter = mono_method_desc_full_match (desc, method);
5244 mono_method_desc_free (desc);
5246 filter = strcmp (method->name, name) == 0;
5248 } else { /* no filter, check for `Category' attribute on method */
5250 MonoCustomAttrInfo* ainfo = mono_custom_attrs_from_method_checked (method, &error);
5251 mono_error_cleanup (&error);
5255 for (j = 0; j < ainfo->num_attrs && filter; ++j) {
5256 MonoCustomAttrEntry *centry = &ainfo->attrs [j];
5257 if (centry->ctor == NULL)
5260 MonoClass *klass = centry->ctor->klass;
5261 if (strcmp (klass->name, "CategoryAttribute"))
5264 MonoObject *obj = mono_custom_attrs_get_attr_checked (ainfo, klass, &error);
5265 /* FIXME: there is an ordering problem if there're multiple attributes, do this instead:
5266 * MonoObject *obj = create_custom_attr (ainfo->image, centry->ctor, centry->data, centry->data_size, &error); */
5267 mono_error_cleanup (&error);
5268 MonoMethod *getter = mono_class_get_method_from_name (klass, "get_Category", -1);
5269 MonoObject *str = mono_interp_runtime_invoke (getter, obj, NULL, &exc, &error);
5270 mono_error_cleanup (&error);
5271 char *utf8_str = mono_string_to_utf8_checked ((MonoString *) str, &error);
5272 mono_error_cleanup (&error);
5273 if (!strcmp (utf8_str, "!INTERPRETER")) {
5274 g_print ("skip %s...\n", method->name);
5280 if (strncmp (method->name, "test_", 5) == 0 && filter) {
5281 MonoError interp_error;
5282 MonoObject *exc = NULL;
5284 result_obj = mono_interp_runtime_invoke (method, NULL, NULL, &exc, &interp_error);
5285 if (!mono_error_ok (&interp_error)) {
5287 g_print ("Test '%s' execution failed.\n", method->name);
5288 } else if (exc != NULL) {
5289 g_print ("Exception in Test '%s' occured:\n", method->name);
5290 mono_object_describe (exc);
5294 result = *(gint32 *) mono_object_unbox (result_obj);
5295 expected = atoi (method->name + 5); // FIXME: oh no.
5298 if (result != expected) {
5300 g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
5305 g_timer_stop (timer);
5306 elapsed = g_timer_elapsed (timer, NULL);
5307 if (failed > 0 || cfailed > 0){
5308 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
5309 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
5311 g_print ("Results: total tests: %d, all pass \n", run);
5314 g_print ("Elapsed time: %f secs (%f, %f)\n\n", elapsed,
5315 elapsed - transform_time, transform_time);
5316 *total += failed + cfailed;
5321 interp_regression (MonoImage *image, int verbose, int *total_run)
5324 GTimer *timer = g_timer_new ();
5325 MonoDomain *domain = mono_domain_get ();
5329 /* load the metadata */
5330 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
5332 method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
5334 mono_error_cleanup (&error);
5337 mono_class_init (method->klass);
5342 interp_regression_step (image, verbose, total_run, &total, timer, domain);
5344 g_timer_destroy (timer);
5349 mono_interp_regression_list (int verbose, int count, char *images [])
5351 int i, total, total_run, run;
5353 total_run = total = 0;
5354 for (i = 0; i < count; ++i) {
5355 MonoAssembly *ass = mono_assembly_open_predicate (images [i], FALSE, FALSE, NULL, NULL, NULL);
5357 g_warning ("failed to load assembly: %s", images [i]);
5360 total += interp_regression (mono_assembly_get_image (ass), verbose, &run);
5364 g_print ("Overall results: tests: %d, failed: %d (pass: %.2f%%)\n", total_run, total, 100.0*(total_run-total)/total_run);
5366 g_print ("Overall results: tests: %d, 100%% pass\n", total_run);
5373 * mono_interp_set_resume_state:
5375 * Set the state the interpeter will continue to execute from after execution returns to the interpreter.
5378 mono_interp_set_resume_state (MonoException *ex, StackFrameInfo *frame, gpointer handler_ip)
5380 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
5382 context->has_resume_state = TRUE;
5383 context->handler_frame = frame->interp_frame;
5384 /* This is on the stack, so it doesn't need a wbarrier */
5385 context->handler_frame->ex = ex;
5386 context->handler_ip = handler_ip;
5390 * mono_interp_run_finally:
5392 * Run the finally clause identified by CLAUSE_INDEX in the intepreter frame given by
5393 * frame->interp_frame.
5396 mono_interp_run_finally (StackFrameInfo *frame, int clause_index, gpointer handler_ip)
5398 MonoInvocation *iframe = frame->interp_frame;
5399 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
5401 ves_exec_method_with_context (iframe, context, handler_ip, NULL, clause_index);
5405 MonoInvocation *current;
5409 * mono_interp_frame_iter_init:
5411 * Initialize an iterator for iterating through interpreted frames.
5414 mono_interp_frame_iter_init (MonoInterpStackIter *iter, gpointer interp_exit_data)
5416 StackIter *stack_iter = (StackIter*)iter;
5418 stack_iter->current = (MonoInvocation*)interp_exit_data;
5422 mono_interp_frame_iter_next (MonoInterpStackIter *iter, StackFrameInfo *frame)
5424 StackIter *stack_iter = (StackIter*)iter;
5425 MonoInvocation *iframe = stack_iter->current;
5427 memset (frame, 0, sizeof (StackFrameInfo));
5428 /* pinvoke frames doesn't have runtime_method set */
5429 while (iframe && !iframe->runtime_method)
5430 iframe = iframe->parent;
5434 frame->type = FRAME_TYPE_INTERP;
5435 frame->interp_frame = iframe;
5436 frame->method = iframe->runtime_method->method;
5437 frame->actual_method = frame->method;
5438 /* This is the offset in the interpreter IR */
5439 frame->native_offset = iframe->ip - iframe->runtime_method->code;
5440 frame->ji = iframe->runtime_method->jinfo;
5442 stack_iter->current = iframe->parent;