2 * PLEASE NOTE: This is a research prototype.
5 * interp.c: Interpreter for CIL byte codes
8 * Paolo Molaro (lupus@ximian.com)
9 * Miguel de Icaza (miguel@ximian.com)
10 * Dietmar Maurer (dietmar@ximian.com)
12 * (C) 2001, 2002 Ximian, Inc.
27 #include <mono/utils/gc_wrapper.h>
33 # define alloca __builtin_alloca
37 /* trim excessive headers */
38 #include <mono/metadata/image.h>
39 #include <mono/metadata/assembly.h>
40 #include <mono/metadata/cil-coff.h>
41 #include <mono/metadata/mono-endian.h>
42 #include <mono/metadata/tabledefs.h>
43 #include <mono/metadata/tokentype.h>
44 #include <mono/metadata/loader.h>
45 #include <mono/metadata/threads.h>
46 #include <mono/metadata/threadpool-ms.h>
47 #include <mono/metadata/profiler-private.h>
48 #include <mono/metadata/appdomain.h>
49 #include <mono/metadata/reflection.h>
50 #include <mono/metadata/reflection-internals.h>
51 #include <mono/metadata/exception.h>
52 #include <mono/metadata/verify.h>
53 #include <mono/metadata/opcodes.h>
54 #include <mono/metadata/debug-helpers.h>
55 #include <mono/metadata/mono-config.h>
56 #include <mono/metadata/marshal.h>
57 #include <mono/metadata/environment.h>
58 #include <mono/metadata/mono-debug.h>
65 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
69 #include "mono/cil/opcode.def"
74 /* Mingw 2.1 doesnt need this any more, but leave it in for now for older versions */
77 #define finite _finite
81 #define finite isfinite
85 static gint *abort_requested;
87 /* If true, then we output the opcodes as we interpret them */
88 static int global_tracing = 1;
89 static int global_no_pointers = 0;
91 int mono_interp_traceopt = 0;
93 static int debug_indent_level = 0;
95 #define INIT_FRAME(frame,parent_frame,obj_this,method_args,method_retval,domain,mono_method,error) \
97 (frame)->parent = (parent_frame); \
98 (frame)->obj = (obj_this); \
99 (frame)->stack_args = (method_args); \
100 (frame)->retval = (method_retval); \
101 (frame)->runtime_method = mono_interp_get_runtime_method ((domain), (mono_method), (error)); \
102 (frame)->ex = NULL; \
103 (frame)->ip = NULL; \
104 (frame)->invoke_trap = 0; \
107 void ves_exec_method (MonoInvocation *frame);
109 static char* dump_stack (stackval *stack, stackval *sp);
110 static char* dump_frame (MonoInvocation *inv);
111 static MonoArray *get_trace_ips (MonoDomain *domain, MonoInvocation *top);
112 static void ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context);
114 typedef void (*ICallMethod) (MonoInvocation *frame);
116 static guint32 die_on_exception = 0;
117 static MonoNativeTlsKey thread_context_id;
119 static char* dump_args (MonoInvocation *inv);
121 #define DEBUG_INTERP 0
125 static int break_on_method = 0;
126 static int nested_trace = 0;
127 static GList *db_methods = NULL;
134 for (h = 0; h < debug_indent_level; h++)
139 db_match_method (gpointer data, gpointer user_data)
141 MonoMethod *m = (MonoMethod*)user_data;
142 MonoMethodDesc *desc = data;
144 if (mono_method_desc_full_match (desc, m))
148 static void debug_enter (MonoInvocation *frame, int *tracing)
151 g_list_foreach (db_methods, db_match_method, (gpointer)frame->runtime_method->method);
153 *tracing = nested_trace ? (global_tracing = 2, 3) : 2;
157 MonoMethod *method = frame->runtime_method->method;
158 char *mn, *args = dump_args (frame);
159 debug_indent_level++;
161 mn = mono_method_full_name (method, FALSE);
162 g_printerr ("(0x%08x) Entering %s (", mono_thread_internal_current (), mn);
164 g_printerr ("%s)\n", args);
167 if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE)
168 mono_profiler_method_enter (frame->runtime_method->method);
172 #define DEBUG_LEAVE() \
175 args = dump_retval (frame); \
177 mn = mono_method_full_name (frame->runtime_method->method, FALSE); \
178 g_printerr ("(0x%08x) Leaving %s", mono_thread_internal_current (), mn); \
180 g_printerr (" => %s\n", args); \
182 debug_indent_level--; \
183 if (tracing == 3) global_tracing = 0; \
185 if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE) \
186 mono_profiler_method_leave (frame->runtime_method->method);
190 static void debug_enter (MonoInvocation *frame, int *tracing)
193 #define DEBUG_LEAVE()
198 interp_ex_handler (MonoException *ex) {
200 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
204 stack_trace = dump_frame (context->current_frame);
205 ex->stack_trace = mono_string_new (mono_domain_get(), stack_trace);
206 g_free (stack_trace);
207 if (context->current_env == NULL || strcmp(ex->object.vtable->klass->name, "ExecutionEngineException") == 0) {
208 char *strace = mono_string_to_utf8_checked (ex->stack_trace, &error);
209 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
210 fprintf(stderr, "Nothing can catch this exception: ");
211 fprintf(stderr, "%s", ex->object.vtable->klass->name);
212 if (ex->message != NULL) {
213 char *m = mono_string_to_utf8_checked (ex->message, &error);
214 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
215 fprintf(stderr, ": %s", m);
218 fprintf(stderr, "\n%s\n", strace);
220 if (ex->inner_ex != NULL) {
221 ex = (MonoException *)ex->inner_ex;
222 fprintf(stderr, "Inner exception: %s", ex->object.vtable->klass->name);
223 if (ex->message != NULL) {
224 char *m = mono_string_to_utf8_checked (ex->message, &error);
225 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
226 fprintf(stderr, ": %s", m);
229 strace = mono_string_to_utf8_checked (ex->stack_trace, &error);
230 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
231 fprintf(stderr, "\n");
232 fprintf(stderr, "%s\n", strace);
235 /* wait for other threads to also collapse */
236 // Sleep(1000); // TODO: proper sleep
239 context->env_frame->ex = ex;
240 context->search_for_handler = 1;
241 longjmp (*context->current_env, 1);
245 ves_real_abort (int line, MonoMethod *mh,
246 const unsigned short *ip, stackval *stack, stackval *sp)
249 fprintf (stderr, "Execution aborted in method: %s::%s\n", mh->klass->name, mh->name);
250 fprintf (stderr, "Line=%d IP=0x%04lx, Aborted execution\n", line,
251 ip-(const unsigned short *)mono_method_get_header_checked (mh, &error)->code);
252 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
253 g_print ("0x%04x %02x\n",
254 ip-(const unsigned short *)mono_method_get_header_checked (mh, &error)->code, *ip);
255 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
257 printf ("\t[%ld] 0x%08x %0.5f\n", sp-stack, sp[-1].data.i, sp[-1].data.f);
260 #define ves_abort() \
262 ves_real_abort(__LINE__, frame->runtime_method->method, ip, frame->stack, sp); \
263 THROW_EX (mono_get_exception_execution_engine (NULL), ip); \
267 interp_create_remoting_trampoline (MonoDomain *domain, MonoMethod *method, MonoRemotingTarget target, MonoError *error)
269 g_error ("FIXME: use domain and error");
270 // return mono_interp_get_runtime_method (mono_marshal_get_remoting_invoke_for_target (method, target));
273 static mono_mutex_t runtime_method_lookup_section;
276 mono_interp_get_runtime_method (MonoDomain *domain, MonoMethod *method, MonoError *error)
279 mono_error_init (error);
281 mono_os_mutex_lock (&runtime_method_lookup_section);
282 if ((rtm = mono_internal_hash_table_lookup (&domain->jit_code_hash, method))) {
283 mono_os_mutex_unlock (&runtime_method_lookup_section);
286 rtm = mono_mempool_alloc (domain->mp, sizeof (RuntimeMethod));
287 memset (rtm, 0, sizeof (*rtm));
288 rtm->method = method;
289 rtm->param_count = mono_method_signature (method)->param_count;
290 rtm->hasthis = mono_method_signature (method)->hasthis;
291 rtm->valuetype = method->klass->valuetype;
292 mono_internal_hash_table_insert (&domain->jit_code_hash, method, rtm);
293 mono_os_mutex_unlock (&runtime_method_lookup_section);
299 interp_create_trampoline (MonoDomain *domain, MonoMethod *method, MonoError *error)
301 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
302 method = mono_marshal_get_synchronized_wrapper (method);
303 return mono_interp_get_runtime_method (domain, method, error);
306 static inline RuntimeMethod*
307 get_virtual_method (MonoDomain *domain, RuntimeMethod *runtime_method, MonoObject *obj)
309 MonoMethod *m = runtime_method->method;
312 if ((m->flags & METHOD_ATTRIBUTE_FINAL) || !(m->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
313 RuntimeMethod *ret = NULL;
314 if (obj->vtable->klass == mono_defaults.transparent_proxy_class)
315 ret = mono_interp_get_runtime_method (domain, mono_marshal_get_remoting_invoke (m), &error);
316 else if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
317 ret = mono_interp_get_runtime_method (domain, mono_marshal_get_synchronized_wrapper (m), &error);
319 ret = runtime_method;
320 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
324 if (m->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
325 // return ((RuntimeMethod **)obj->vtable->interface_offsets [m->klass->interface_id]) [m->slot];
326 g_error ("FIXME: interface method lookup");
329 return ((RuntimeMethod **)obj->vtable->vtable) [m->slot];
334 stackval_from_data (MonoType *type, stackval *result, char *data, gboolean pinvoke)
337 switch (type->type) {
338 case MONO_TYPE_OBJECT:
339 case MONO_TYPE_CLASS:
340 case MONO_TYPE_STRING:
341 case MONO_TYPE_ARRAY:
342 case MONO_TYPE_SZARRAY:
347 result->data.p = *(gpointer*)data;
350 switch (type->type) {
354 result->data.i = *(gint8*)data;
357 case MONO_TYPE_BOOLEAN:
358 result->data.i = *(guint8*)data;
361 result->data.i = *(gint16*)data;
365 result->data.i = *(guint16*)data;
368 result->data.i = *(gint32*)data;
372 result->data.nati = *(mono_i*)data;
375 result->data.p = *(gpointer*)data;
378 result->data.i = *(guint32*)data;
381 result->data.f = *(float*)data;
385 result->data.l = *(gint64*)data;
388 result->data.f = *(double*)data;
390 case MONO_TYPE_STRING:
391 case MONO_TYPE_SZARRAY:
392 case MONO_TYPE_CLASS:
393 case MONO_TYPE_OBJECT:
394 case MONO_TYPE_ARRAY:
395 result->data.p = *(gpointer*)data;
397 case MONO_TYPE_VALUETYPE:
398 if (type->data.klass->enumtype) {
399 stackval_from_data (mono_class_enum_basetype (type->data.klass), result, data, pinvoke);
405 size = mono_class_native_size (type->data.klass, NULL);
407 size = mono_class_value_size (type->data.klass, NULL);
408 memcpy (result->data.vt, data, size);
412 g_warning ("got type 0x%02x", type->type);
413 g_assert_not_reached ();
418 stackval_to_data (MonoType *type, stackval *val, char *data, gboolean pinvoke)
421 gpointer *p = (gpointer*)data;
425 /* printf ("TODAT0 %p\n", data); */
426 switch (type->type) {
429 guint8 *p = (guint8*)data;
433 case MONO_TYPE_BOOLEAN: {
434 guint8 *p = (guint8*)data;
435 *p = (val->data.i != 0);
440 case MONO_TYPE_CHAR: {
441 guint16 *p = (guint16*)data;
446 mono_i *p = (mono_i*)data;
447 /* In theory the value used by stloc should match the local var type
448 but in practice it sometimes doesn't (a int32 gets dup'd and stloc'd into
449 a native int - both by csc and mcs). Not sure what to do about sign extension
450 as it is outside the spec... doing the obvious */
451 *p = (mono_i)val->data.nati;
455 mono_u *p = (mono_u*)data;
457 *p = (mono_u)val->data.nati;
462 gint32 *p = (gint32*)data;
468 gint64 *p = (gint64*)data;
473 float *p = (float*)data;
478 double *p = (double*)data;
482 case MONO_TYPE_STRING:
483 case MONO_TYPE_SZARRAY:
484 case MONO_TYPE_CLASS:
485 case MONO_TYPE_OBJECT:
486 case MONO_TYPE_ARRAY:
487 case MONO_TYPE_PTR: {
488 gpointer *p = (gpointer*)data;
492 case MONO_TYPE_VALUETYPE:
493 if (type->data.klass->enumtype) {
494 stackval_to_data (mono_class_enum_basetype (type->data.klass), val, data, pinvoke);
500 size = mono_class_native_size (type->data.klass, NULL);
502 size = mono_class_value_size (type->data.klass, NULL);
504 memcpy (data, val->data.p, size);
508 g_warning ("got type %x", type->type);
509 g_assert_not_reached ();
514 fill_in_trace (MonoException *exception, MonoInvocation *frame)
516 char *stack_trace = dump_frame (frame);
517 MonoDomain *domain = mono_domain_get();
518 (exception)->stack_trace = mono_string_new (domain, stack_trace);
519 (exception)->trace_ips = get_trace_ips (domain, frame);
520 g_free (stack_trace);
523 #define FILL_IN_TRACE(exception, frame) fill_in_trace(exception, frame)
525 #define THROW_EX(exception,ex_ip) \
527 frame->ip = (ex_ip); \
528 frame->ex = (MonoException*)(exception); \
529 FILL_IN_TRACE(frame->ex, frame); \
530 goto handle_exception; \
534 ves_array_create (MonoDomain *domain, MonoClass *klass, MonoMethodSignature *sig, stackval *values)
537 intptr_t *lower_bounds;
542 lengths = alloca (sizeof (uintptr_t) * klass->rank * 2);
543 for (i = 0; i < sig->param_count; ++i) {
544 lengths [i] = values->data.i;
547 if (klass->rank == sig->param_count) {
548 /* Only lengths provided. */
551 /* lower bounds are first. */
552 lower_bounds = (intptr_t *) lengths;
553 lengths += klass->rank;
555 obj = (MonoObject*) mono_array_new_full_checked (domain, klass, lengths, lower_bounds, &error);
556 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
561 ves_array_set (MonoInvocation *frame)
563 stackval *sp = frame->stack_args;
567 gint32 i, t, pos, esize;
573 ac = o->vtable->klass;
575 g_assert (ac->rank >= 1);
578 if (ao->bounds != NULL) {
579 pos -= ao->bounds [0].lower_bound;
580 for (i = 1; i < ac->rank; i++) {
581 if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >=
582 ao->bounds [i].length) {
583 frame->ex = mono_get_exception_index_out_of_range ();
584 FILL_IN_TRACE(frame->ex, frame);
587 pos = pos*ao->bounds [i].length + sp [i].data.i -
588 ao->bounds [i].lower_bound;
590 } else if (pos >= ao->max_length) {
591 frame->ex = mono_get_exception_index_out_of_range ();
592 FILL_IN_TRACE(frame->ex, frame);
597 if (sp [ac->rank].data.p && !mono_object_isinst (sp [ac->rank].data.p, mono_object_class (o)->element_class)) {
598 frame->ex = mono_get_exception_array_type_mismatch ();
599 FILL_IN_TRACE (frame->ex, frame);
604 esize = mono_array_element_size (ac);
605 ea = mono_array_addr_with_size (ao, esize, pos);
607 mt = mono_method_signature (frame->runtime_method->method)->params [ac->rank];
608 stackval_to_data (mt, &sp [ac->rank], ea, FALSE);
612 ves_array_get (MonoInvocation *frame)
614 stackval *sp = frame->stack_args;
618 gint32 i, t, pos, esize;
624 ac = o->vtable->klass;
626 g_assert (ac->rank >= 1);
629 if (ao->bounds != NULL) {
630 pos -= ao->bounds [0].lower_bound;
631 for (i = 1; i < ac->rank; i++) {
632 if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >=
633 ao->bounds [i].length) {
634 frame->ex = mono_get_exception_index_out_of_range ();
635 FILL_IN_TRACE(frame->ex, frame);
639 pos = pos*ao->bounds [i].length + sp [i].data.i -
640 ao->bounds [i].lower_bound;
642 } else if (pos >= ao->max_length) {
643 frame->ex = mono_get_exception_index_out_of_range ();
644 FILL_IN_TRACE(frame->ex, frame);
648 esize = mono_array_element_size (ac);
649 ea = mono_array_addr_with_size (ao, esize, pos);
651 mt = mono_method_signature (frame->runtime_method->method)->ret;
652 stackval_from_data (mt, frame->retval, ea, FALSE);
656 ves_array_element_address (MonoInvocation *frame)
658 stackval *sp = frame->stack_args;
662 gint32 i, t, pos, esize;
667 ac = o->vtable->klass;
669 g_assert (ac->rank >= 1);
672 if (ao->bounds != NULL) {
673 pos -= ao->bounds [0].lower_bound;
674 for (i = 1; i < ac->rank; i++) {
675 if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >=
676 ao->bounds [i].length) {
677 frame->ex = mono_get_exception_index_out_of_range ();
678 FILL_IN_TRACE(frame->ex, frame);
681 pos = pos*ao->bounds [i].length + sp [i].data.i -
682 ao->bounds [i].lower_bound;
684 } else if (pos >= ao->max_length) {
685 frame->ex = mono_get_exception_index_out_of_range ();
686 FILL_IN_TRACE(frame->ex, frame);
690 esize = mono_array_element_size (ac);
691 ea = mono_array_addr_with_size (ao, esize, pos);
693 frame->retval->data.p = ea;
697 interp_walk_stack (MonoStackWalk func, gboolean do_il_offset, gpointer user_data)
699 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
700 MonoInvocation *frame;
702 MonoMethodHeader *hd;
705 if (!context) return;
707 frame = context->current_frame;
710 gboolean managed = FALSE;
711 MonoMethod *method = frame->runtime_method->method;
712 if (!method || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)))
715 hd = mono_method_get_header_checked (method, &error);
716 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
717 il_offset = frame->ip - (const unsigned short *)hd->code;
718 if (!method->wrapper_type)
721 if (func (method, -1, il_offset, managed, user_data))
723 frame = frame->parent;
728 ves_pinvoke_method (MonoInvocation *frame, MonoMethodSignature *sig, MonoFuncV addr, gboolean string_ctor, ThreadContext *context)
732 MonoInvocation *old_frame = context->current_frame;
733 MonoInvocation *old_env_frame = context->env_frame;
734 jmp_buf *old_env = context->current_env;
737 context->current_frame = old_frame;
738 context->env_frame = old_env_frame;
739 context->current_env = old_env;
740 context->managed_code = 1;
745 context->env_frame = frame;
746 context->current_env = &env;
748 if (frame->runtime_method) {
749 func = frame->runtime_method->func;
751 g_error ("FIXME: not available?");
753 func = mono_arch_create_trampoline (sig, string_ctor);
757 context->current_frame = frame;
758 context->managed_code = 0;
760 func (addr, &frame->retval->data.p, frame->obj, frame->stack_args);
762 context->managed_code = 1;
763 /* domain can only be changed by native code */
764 context->domain = mono_domain_get ();
766 if (*abort_requested)
767 mono_thread_interruption_checkpoint ();
770 stackval_from_data (&mono_defaults.string_class->byval_arg, frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
771 } else if (!MONO_TYPE_ISSTRUCT (sig->ret))
772 stackval_from_data (sig->ret, frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
774 context->current_frame = old_frame;
775 context->env_frame = old_env_frame;
776 context->current_env = old_env;
780 interp_delegate_ctor (MonoDomain *domain, MonoObject *this, MonoObject *target, RuntimeMethod *runtime_method)
782 MonoDelegate *delegate = (MonoDelegate *)this;
785 delegate->method_info = mono_method_get_object_checked (domain, runtime_method->method, NULL, &error);
786 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
787 delegate->target = target;
789 if (target && target->vtable->klass == mono_defaults.transparent_proxy_class) {
790 MonoMethod *method = mono_marshal_get_remoting_invoke (runtime_method->method);
791 delegate->method_ptr = mono_interp_get_runtime_method (domain, method, &error);
792 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
794 delegate->method_ptr = runtime_method;
799 mono_interp_ftnptr_to_delegate (MonoClass *klass, gpointer ftn)
803 MonoDomain *domain = mono_domain_get ();
806 d = (MonoDelegate*)mono_object_new_checked (domain, klass, &error);
807 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
809 ji = mono_jit_info_table_find (domain, ftn);
811 mono_raise_exception (mono_get_exception_argument ("", "Function pointer was not created by a Delegate."));
813 /* FIXME: discard the wrapper and call the original method */
814 interp_delegate_ctor (domain, (MonoObject*)d, NULL, mono_interp_get_runtime_method (domain, ji->d.method, &error));
815 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
822 * runtime specifies that the implementation of the method is automatically
823 * provided by the runtime and is primarily used for the methods of delegates.
826 ves_runtime_method (MonoInvocation *frame, ThreadContext *context)
828 MonoMethod *method = frame->runtime_method->method;
829 const char *name = method->name;
830 MonoObject *obj = (MonoObject*)frame->obj;
831 MonoObject *isinst_obj;
834 mono_class_init (method->klass);
836 isinst_obj = mono_object_isinst_checked (obj, mono_defaults.multicastdelegate_class, &error);
837 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
838 if (obj && isinst_obj) {
839 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
840 interp_delegate_ctor (context->domain, obj, frame->stack_args[0].data.p, frame->stack_args[1].data.p);
845 isinst_obj = mono_object_isinst_checked (obj, mono_defaults.array_class, &error);
846 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
847 if (obj && isinst_obj) {
848 if (*name == 'S' && (strcmp (name, "Set") == 0)) {
849 ves_array_set (frame);
852 if (*name == 'G' && (strcmp (name, "Get") == 0)) {
853 ves_array_get (frame);
856 if (*name == 'A' && (strcmp (name, "Address") == 0)) {
857 ves_array_element_address (frame);
862 g_error ("Don't know how to exec runtime method %s.%s::%s",
863 method->klass->name_space, method->klass->name,
868 dump_stack (stackval *stack, stackval *sp)
871 GString *str = g_string_new ("");
874 return g_string_free (str, FALSE);
877 g_string_append_printf (str, "[%lld/0x%0llx] ", s->data.l, s->data.l);
880 return g_string_free (str, FALSE);
884 dump_stackval (GString *str, stackval *s, MonoType *type)
886 switch (type->type) {
894 case MONO_TYPE_BOOLEAN:
895 g_string_append_printf (str, "[%d] ", s->data.i);
897 case MONO_TYPE_STRING:
898 case MONO_TYPE_SZARRAY:
899 case MONO_TYPE_CLASS:
900 case MONO_TYPE_OBJECT:
901 case MONO_TYPE_ARRAY:
905 g_string_append_printf (str, "[%p] ", s->data.p);
907 case MONO_TYPE_VALUETYPE:
908 if (type->data.klass->enumtype)
909 g_string_append_printf (str, "[%d] ", s->data.i);
911 g_string_append_printf (str, "[vt:%p] ", s->data.p);
915 g_string_append_printf (str, "[%g] ", s->data.f);
920 g_string_append_printf (str, "[%lld/0x%0llx] ", s->data.l, s->data.l);
926 dump_args (MonoInvocation *inv)
928 GString *str = g_string_new ("");
930 MonoMethodSignature *signature = mono_method_signature (inv->runtime_method->method);
932 if (signature->param_count == 0)
933 return g_string_free (str, FALSE);
935 if (signature->hasthis)
936 g_string_append_printf (str, "%p ", inv->obj);
938 for (i = 0; i < signature->param_count; ++i)
939 dump_stackval (str, inv->stack_args + i, signature->params [i]);
941 return g_string_free (str, FALSE);
945 dump_retval (MonoInvocation *inv)
947 GString *str = g_string_new ("");
948 MonoType *ret = mono_method_signature (inv->runtime_method->method)->ret;
950 if (ret->type != MONO_TYPE_VOID)
951 dump_stackval (str, inv->retval, ret);
953 return g_string_free (str, FALSE);
957 dump_frame (MonoInvocation *inv)
959 GString *str = g_string_new ("");
964 for (i = 0; inv; inv = inv->parent) {
965 if (inv->runtime_method != NULL) {
966 MonoMethod *method = inv->runtime_method->method;
970 const char * opname = "";
972 gchar *source = NULL;
976 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
977 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) == 0) {
978 MonoMethodHeader *hd = mono_method_get_header_checked (method, &error);
979 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
983 opname = mono_interp_opname [*inv->ip];
984 codep = inv->ip - inv->runtime_method->code;
985 source = g_strdup_printf ("%s:%d // (TODO: proper stacktrace)", method->name, codep);
990 MonoDebugSourceLocation *minfo = mono_debug_lookup_method (method);
991 source = mono_debug_method_lookup_location (minfo, codep);
995 args = dump_args (inv);
996 name = mono_method_full_name (method, TRUE);
998 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s) at %s\n", i, codep, opname, name, args, source);
1000 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s)\n", i, codep, opname, name, args);
1007 return g_string_free (str, FALSE);
1011 get_trace_ips (MonoDomain *domain, MonoInvocation *top)
1015 MonoInvocation *inv;
1018 for (i = 0, inv = top; inv; inv = inv->parent)
1019 if (inv->runtime_method != NULL)
1022 res = mono_array_new_checked (domain, mono_defaults.int_class, 2 * i, &error);
1023 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1025 for (i = 0, inv = top; inv; inv = inv->parent)
1026 if (inv->runtime_method != NULL) {
1027 mono_array_set (res, gpointer, i, inv->runtime_method);
1029 mono_array_set (res, gpointer, i, (gpointer)inv->ip);
1037 #define MYGUINT64_MAX 18446744073709551615ULL
1038 #define MYGINT64_MAX 9223372036854775807LL
1039 #define MYGINT64_MIN (-MYGINT64_MAX -1LL)
1041 #define MYGUINT32_MAX 4294967295U
1042 #define MYGINT32_MAX 2147483647
1043 #define MYGINT32_MIN (-MYGINT32_MAX -1)
1045 #define CHECK_ADD_OVERFLOW(a,b) \
1046 (gint32)(b) >= 0 ? (gint32)(MYGINT32_MAX) - (gint32)(b) < (gint32)(a) ? -1 : 0 \
1047 : (gint32)(MYGINT32_MIN) - (gint32)(b) > (gint32)(a) ? +1 : 0
1049 #define CHECK_SUB_OVERFLOW(a,b) \
1050 (gint32)(b) < 0 ? (gint32)(MYGINT32_MAX) + (gint32)(b) < (gint32)(a) ? -1 : 0 \
1051 : (gint32)(MYGINT32_MIN) + (gint32)(b) > (gint32)(a) ? +1 : 0
1053 #define CHECK_ADD_OVERFLOW_UN(a,b) \
1054 (guint32)(MYGUINT32_MAX) - (guint32)(b) < (guint32)(a) ? -1 : 0
1056 #define CHECK_SUB_OVERFLOW_UN(a,b) \
1057 (guint32)(a) < (guint32)(b) ? -1 : 0
1059 #define CHECK_ADD_OVERFLOW64(a,b) \
1060 (gint64)(b) >= 0 ? (gint64)(MYGINT64_MAX) - (gint64)(b) < (gint64)(a) ? -1 : 0 \
1061 : (gint64)(MYGINT64_MIN) - (gint64)(b) > (gint64)(a) ? +1 : 0
1063 #define CHECK_SUB_OVERFLOW64(a,b) \
1064 (gint64)(b) < 0 ? (gint64)(MYGINT64_MAX) + (gint64)(b) < (gint64)(a) ? -1 : 0 \
1065 : (gint64)(MYGINT64_MIN) + (gint64)(b) > (gint64)(a) ? +1 : 0
1067 #define CHECK_ADD_OVERFLOW64_UN(a,b) \
1068 (guint64)(MYGUINT64_MAX) - (guint64)(b) < (guint64)(a) ? -1 : 0
1070 #define CHECK_SUB_OVERFLOW64_UN(a,b) \
1071 (guint64)(a) < (guint64)(b) ? -1 : 0
1073 #if SIZEOF_VOID_P == 4
1074 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW(a,b)
1075 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW_UN(a,b)
1077 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW64(a,b)
1078 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW64_UN(a,b)
1081 /* Resolves to TRUE if the operands would overflow */
1082 #define CHECK_MUL_OVERFLOW(a,b) \
1083 ((gint32)(a) == 0) || ((gint32)(b) == 0) ? 0 : \
1084 (((gint32)(a) > 0) && ((gint32)(b) == -1)) ? FALSE : \
1085 (((gint32)(a) < 0) && ((gint32)(b) == -1)) ? (a == - MYGINT32_MAX) : \
1086 (((gint32)(a) > 0) && ((gint32)(b) > 0)) ? (gint32)(a) > ((MYGINT32_MAX) / (gint32)(b)) : \
1087 (((gint32)(a) > 0) && ((gint32)(b) < 0)) ? (gint32)(a) > ((MYGINT32_MIN) / (gint32)(b)) : \
1088 (((gint32)(a) < 0) && ((gint32)(b) > 0)) ? (gint32)(a) < ((MYGINT32_MIN) / (gint32)(b)) : \
1089 (gint32)(a) < ((MYGINT32_MAX) / (gint32)(b))
1091 #define CHECK_MUL_OVERFLOW_UN(a,b) \
1092 ((guint32)(a) == 0) || ((guint32)(b) == 0) ? 0 : \
1093 (guint32)(b) > ((MYGUINT32_MAX) / (guint32)(a))
1095 #define CHECK_MUL_OVERFLOW64(a,b) \
1096 ((gint64)(a) == 0) || ((gint64)(b) == 0) ? 0 : \
1097 (((gint64)(a) > 0) && ((gint64)(b) == -1)) ? FALSE : \
1098 (((gint64)(a) < 0) && ((gint64)(b) == -1)) ? (a == - MYGINT64_MAX) : \
1099 (((gint64)(a) > 0) && ((gint64)(b) > 0)) ? (gint64)(a) > ((MYGINT64_MAX) / (gint64)(b)) : \
1100 (((gint64)(a) > 0) && ((gint64)(b) < 0)) ? (gint64)(a) > ((MYGINT64_MIN) / (gint64)(b)) : \
1101 (((gint64)(a) < 0) && ((gint64)(b) > 0)) ? (gint64)(a) < ((MYGINT64_MIN) / (gint64)(b)) : \
1102 (gint64)(a) < ((MYGINT64_MAX) / (gint64)(b))
1104 #define CHECK_MUL_OVERFLOW64_UN(a,b) \
1105 ((guint64)(a) == 0) || ((guint64)(b) == 0) ? 0 : \
1106 (guint64)(b) > ((MYGUINT64_MAX) / (guint64)(a))
1108 #if SIZEOF_VOID_P == 4
1109 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW(a,b)
1110 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW_UN(a,b)
1112 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW64(a,b)
1113 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW64_UN(a,b)
1117 interp_mono_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc, MonoError *error)
1119 MonoInvocation frame;
1120 ThreadContext * volatile context = mono_native_tls_get_value (thread_context_id);
1121 MonoObject *retval = NULL;
1122 MonoMethodSignature *sig = mono_method_signature (method);
1123 MonoClass *klass = mono_class_from_mono_type (sig->ret);
1124 int i, type, isobject = 0;
1127 stackval *args = alloca (sizeof (stackval) * sig->param_count);
1128 ThreadContext context_struct;
1129 MonoInvocation *old_frame = NULL;
1132 mono_error_init (error);
1137 if (context != &context_struct) {
1138 context->domain = mono_domain_get ();
1139 context->current_frame = old_frame;
1140 context->managed_code = 0;
1142 mono_native_tls_set_value (thread_context_id, NULL);
1144 *exc = (MonoObject *)frame.ex;
1148 if (context == NULL) {
1149 context = &context_struct;
1150 context_struct.base_frame = &frame;
1151 context_struct.current_frame = NULL;
1152 context_struct.env_frame = &frame;
1153 context_struct.current_env = &env;
1154 context_struct.search_for_handler = 0;
1155 context_struct.managed_code = 0;
1156 mono_native_tls_set_value (thread_context_id, context);
1159 old_frame = context->current_frame;
1161 context->domain = mono_domain_get ();
1163 switch (sig->ret->type) {
1164 case MONO_TYPE_VOID:
1166 case MONO_TYPE_STRING:
1167 case MONO_TYPE_OBJECT:
1168 case MONO_TYPE_CLASS:
1169 case MONO_TYPE_ARRAY:
1170 case MONO_TYPE_SZARRAY:
1173 case MONO_TYPE_VALUETYPE:
1174 retval = mono_object_new_checked (context->domain, klass, error);
1175 ret = ((char*)retval) + sizeof (MonoObject);
1176 if (!sig->ret->data.klass->enumtype)
1177 result.data.vt = ret;
1180 retval = mono_object_new_checked (context->domain, klass, error);
1181 ret = ((char*)retval) + sizeof (MonoObject);
1185 for (i = 0; i < sig->param_count; ++i) {
1186 if (sig->params [i]->byref) {
1187 args [i].data.p = params [i];
1190 type = sig->params [i]->type;
1195 case MONO_TYPE_BOOLEAN:
1196 args [i].data.i = *(MonoBoolean*)params [i];
1200 case MONO_TYPE_CHAR:
1201 args [i].data.i = *(gint16*)params [i];
1203 #if SIZEOF_VOID_P == 4
1204 case MONO_TYPE_U: /* use VAL_POINTER? */
1209 args [i].data.i = *(gint32*)params [i];
1211 #if SIZEOF_VOID_P == 8
1217 args [i].data.l = *(gint64*)params [i];
1219 case MONO_TYPE_VALUETYPE:
1220 if (sig->params [i]->data.klass->enumtype) {
1221 type = mono_class_enum_basetype (sig->params [i]->data.klass)->type;
1224 args [i].data.p = params [i];
1227 case MONO_TYPE_STRING:
1228 case MONO_TYPE_CLASS:
1229 case MONO_TYPE_ARRAY:
1230 case MONO_TYPE_SZARRAY:
1231 case MONO_TYPE_OBJECT:
1232 args [i].data.p = params [i];
1235 g_error ("type 0x%x not handled in runtime invoke", sig->params [i]->type);
1239 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
1240 method = mono_marshal_get_native_wrapper (method, FALSE, FALSE);
1241 INIT_FRAME(&frame,context->current_frame,obj,args,&result,mono_get_root_domain (),method,error);
1243 frame.invoke_trap = 1;
1244 context->managed_code = 1;
1245 ves_exec_method_with_context (&frame, context);
1246 context->managed_code = 0;
1247 if (context == &context_struct)
1248 mono_native_tls_set_value (thread_context_id, NULL);
1250 context->current_frame = old_frame;
1251 if (frame.ex != NULL) {
1253 *exc = (MonoObject*) frame.ex;
1256 if (context->current_env != NULL) {
1257 context->env_frame->ex = frame.ex;
1258 longjmp(*context->current_env, 1);
1261 printf("dropped exception...\n");
1263 if (sig->ret->type == MONO_TYPE_VOID && !method->string_ctor)
1265 if (isobject || method->string_ctor)
1266 return result.data.p;
1267 stackval_to_data (sig->ret, &result, ret, sig->pinvoke);
1272 do_icall (ThreadContext *context, int op, stackval *sp, gpointer ptr)
1274 MonoInvocation *old_frame = context->current_frame;
1275 MonoInvocation *old_env_frame = context->env_frame;
1276 jmp_buf *old_env = context->current_env;
1280 context->current_frame = old_frame;
1281 context->env_frame = old_env_frame;
1282 context->current_env = old_env;
1283 context->managed_code = 1;
1287 context->env_frame = context->current_frame;
1288 context->current_env = &env;
1289 context->managed_code = 0;
1292 case MINT_ICALL_V_V: {
1293 void (*func)() = ptr;
1297 case MINT_ICALL_P_V: {
1298 void (*func)(gpointer) = ptr;
1299 func (sp [-1].data.p);
1303 case MINT_ICALL_P_P: {
1304 gpointer (*func)(gpointer) = ptr;
1305 sp [-1].data.p = func (sp [-1].data.p);
1308 case MINT_ICALL_PP_V: {
1309 void (*func)(gpointer,gpointer) = ptr;
1311 func (sp [0].data.p, sp [1].data.p);
1314 case MINT_ICALL_PI_V: {
1315 void (*func)(gpointer,int) = ptr;
1317 func (sp [0].data.p, sp [1].data.i);
1320 case MINT_ICALL_PP_P: {
1321 gpointer (*func)(gpointer,gpointer) = ptr;
1323 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.p);
1326 case MINT_ICALL_PI_P: {
1327 gpointer (*func)(gpointer,int) = ptr;
1329 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.i);
1332 case MINT_ICALL_PPP_V: {
1333 void (*func)(gpointer,gpointer,gpointer) = ptr;
1335 func (sp [0].data.p, sp [1].data.p, sp [2].data.p);
1338 case MINT_ICALL_PPI_V: {
1339 void (*func)(gpointer,gpointer,int) = ptr;
1341 func (sp [0].data.p, sp [1].data.p, sp [2].data.i);
1345 g_assert_not_reached ();
1348 context->env_frame = old_env_frame;
1349 context->current_env = old_env;
1354 static mono_mutex_t create_method_pointer_mutex;
1356 static GHashTable *method_pointer_hash = NULL;
1359 mono_create_method_pointer (MonoMethod *method, MonoError *error)
1363 mono_error_init (error);
1365 mono_os_mutex_lock (&create_method_pointer_mutex);
1366 if (!method_pointer_hash) {
1367 // FIXME: is registering method table as GC root really necessary?
1368 // MONO_GC_REGISTER_ROOT_FIXED (method_pointer_hash);
1369 method_pointer_hash = g_hash_table_new (NULL, NULL);
1371 addr = g_hash_table_lookup (method_pointer_hash, method);
1373 mono_os_mutex_unlock (&create_method_pointer_mutex);
1378 * If it is a static P/Invoke method, we can just return the pointer
1379 * to the method implementation.
1381 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL && ((MonoMethodPInvoke*) method)->addr) {
1382 ji = g_new0 (MonoJitInfo, 1);
1383 ji->d.method = method;
1385 ji->code_start = addr = ((MonoMethodPInvoke*) method)->addr;
1387 mono_jit_info_table_add (mono_get_root_domain (), ji);
1390 g_error ("FIXME: not available? figure out new API");
1392 addr = mono_arch_create_method_pointer (method);
1396 g_hash_table_insert (method_pointer_hash, method, addr);
1397 mono_os_mutex_unlock (&create_method_pointer_mutex);
1403 static int opcode_counts[512];
1405 #define COUNT_OP(op) opcode_counts[op]++
1407 #define COUNT_OP(op)
1411 #define DUMP_INSTR() \
1412 if (tracing > 1) { \
1414 if (sp > frame->stack) { \
1415 ins = dump_stack (frame->stack, sp); \
1417 ins = g_strdup (""); \
1421 g_print ("(%u) ", mono_thread_internal_current ()); \
1422 mono_interp_dis_mintop(rtm->code, ip); \
1423 g_print ("\t%d:%s\n", vt_sp - vtalloc, ins); \
1427 #define DUMP_INSTR()
1431 #define USE_COMPUTED_GOTO 1
1433 #if USE_COMPUTED_GOTO
1434 #define MINT_IN_SWITCH(op) COUNT_OP(op); goto *in_labels[op];
1435 #define MINT_IN_CASE(x) LAB_ ## x:
1437 #define MINT_IN_BREAK if (tracing > 1) goto main_loop; else { COUNT_OP(*ip); goto *in_labels[*ip]; }
1439 #define MINT_IN_BREAK { COUNT_OP(*ip); goto *in_labels[*ip]; }
1441 #define MINT_IN_DEFAULT mint_default: if (0) goto mint_default; /* make gcc shut up */
1443 #define MINT_IN_SWITCH(op) switch (op)
1444 #define MINT_IN_CASE(x) case x:
1445 #define MINT_IN_BREAK break
1446 #define MINT_IN_DEFAULT default:
1450 * Defining this causes register allocation errors in some versions of gcc:
1451 * error: unable to find a register to spill in class `SIREG'
1453 /* #define MINT_USE_DEDICATED_IP_REG */
1456 ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context)
1458 MonoInvocation child_frame;
1459 GSList *finally_ips = NULL;
1460 const unsigned short *endfinally_ip = NULL;
1461 #if defined(__GNUC__) && defined (i386) && defined (MINT_USE_DEDICATED_IP_REG)
1462 register const unsigned short *ip asm ("%esi");
1464 register const unsigned short *ip;
1466 register stackval *sp;
1469 gint tracing = global_tracing;
1470 unsigned char *vtalloc;
1473 unsigned char *vt_sp;
1474 unsigned char *locals;
1476 MonoObject *o = NULL;
1478 #if USE_COMPUTED_GOTO
1479 static void *in_labels[] = {
1480 #define OPDEF(a,b,c,d) \
1482 #include "mintops.def"
1487 frame->ex_handler = NULL;
1489 context->current_frame = frame;
1492 debug_enter (frame, &tracing);
1495 if (!frame->runtime_method->transformed) {
1496 context->managed_code = 0;
1498 char *mn = mono_method_full_name (frame->runtime_method->method, FALSE);
1499 g_printerr ("(0x%08x) Transforming %s\n", mono_thread_internal_current (), mn);
1502 frame->ex = mono_interp_transform_method (frame->runtime_method, context);
1503 context->managed_code = 1;
1511 rtm = frame->runtime_method;
1512 frame->args = alloca (rtm->alloca_size);
1513 sp = frame->stack = (stackval *)((char *)frame->args + rtm->args_size);
1516 memset(sp, 0, rtm->stack_size);
1518 vt_sp = (unsigned char *) sp + rtm->stack_size;
1522 locals = (unsigned char *) vt_sp + rtm->vt_stack_size;
1524 child_frame.parent = frame;
1530 * using while (ip < end) may result in a 15% performance drop,
1531 * but it may be useful for debug
1535 /* g_assert (sp >= frame->stack); */
1536 /* g_assert(vt_sp - vtalloc <= rtm->vt_stack_size); */
1538 MINT_IN_SWITCH (*ip) {
1539 MINT_IN_CASE(MINT_INITLOCALS)
1540 memset (locals, 0, rtm->locals_size);
1543 MINT_IN_CASE(MINT_NOP)
1546 MINT_IN_CASE(MINT_BREAK)
1548 G_BREAKPOINT (); /* this is not portable... */
1550 MINT_IN_CASE(MINT_LDNULL)
1555 MINT_IN_CASE(MINT_VTRESULT) {
1556 int ret_size = * (guint16 *)(ip + 1);
1557 unsigned char *ret_vt_sp = vt_sp;
1558 vt_sp -= READ32(ip + 2);
1560 memmove (vt_sp, ret_vt_sp, ret_size);
1561 vt_sp += (ret_size + 7) & ~7;
1566 #define LDC(n) do { sp->data.i = (n); ++ip; ++sp; } while (0)
1567 MINT_IN_CASE(MINT_LDC_I4_M1)
1570 MINT_IN_CASE(MINT_LDC_I4_0)
1573 MINT_IN_CASE(MINT_LDC_I4_1)
1576 MINT_IN_CASE(MINT_LDC_I4_2)
1579 MINT_IN_CASE(MINT_LDC_I4_3)
1582 MINT_IN_CASE(MINT_LDC_I4_4)
1585 MINT_IN_CASE(MINT_LDC_I4_5)
1588 MINT_IN_CASE(MINT_LDC_I4_6)
1591 MINT_IN_CASE(MINT_LDC_I4_7)
1594 MINT_IN_CASE(MINT_LDC_I4_8)
1597 MINT_IN_CASE(MINT_LDC_I4_S)
1598 sp->data.i = *(const short *)(ip + 1);
1602 MINT_IN_CASE(MINT_LDC_I4)
1604 sp->data.i = READ32 (ip);
1608 MINT_IN_CASE(MINT_LDC_I8)
1610 sp->data.l = READ64 (ip);
1614 MINT_IN_CASE(MINT_LDC_R4) {
1618 sp->data.f = * (float *)&val;
1623 MINT_IN_CASE(MINT_LDC_R8)
1624 sp->data.l = READ64 (ip + 1); /* note union usage */
1628 MINT_IN_CASE(MINT_DUP)
1633 MINT_IN_CASE(MINT_DUP_VT)
1634 i32 = READ32 (ip + 1);
1636 memcpy(sp->data.p, sp [-1].data.p, i32);
1637 vt_sp += (i32 + 7) & ~7;
1641 MINT_IN_CASE(MINT_POP)
1645 MINT_IN_CASE(MINT_JMP) {
1646 RuntimeMethod *new_method = rtm->data_items [* (guint16 *)(ip + 1)];
1647 if (!new_method->transformed) {
1649 frame->ex = mono_interp_transform_method (new_method, context);
1654 if (new_method->alloca_size > rtm->alloca_size)
1655 g_error ("MINT_JMP to method which needs more stack space (%d > %d)", new_method->alloca_size, rtm->alloca_size);
1656 rtm = frame->runtime_method = new_method;
1657 vt_sp = (unsigned char *) sp + rtm->stack_size;
1661 locals = vt_sp + rtm->vt_stack_size;
1662 ip = rtm->new_body_start; /* bypass storing input args from callers frame */
1665 MINT_IN_CASE(MINT_CALLI) {
1666 MonoMethodSignature *csignature;
1667 stackval *endsp = sp;
1671 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1675 child_frame.runtime_method = sp->data.p;
1678 child_frame.retval = sp;
1679 /* decrement by the actual number of args */
1680 sp -= csignature->param_count;
1681 child_frame.stack_args = sp;
1682 if (csignature->hasthis) {
1684 child_frame.obj = sp->data.p;
1686 child_frame.obj = NULL;
1688 if (csignature->hasthis && ((MonoObject *)child_frame.obj)->vtable->klass == mono_defaults.transparent_proxy_class) {
1689 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1690 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1691 } else if (child_frame.runtime_method->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
1692 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_native_wrapper (child_frame.runtime_method->method, FALSE, FALSE), &error);
1693 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1696 ves_exec_method_with_context (&child_frame, context);
1698 context->current_frame = frame;
1700 if (child_frame.ex) {
1702 * An exception occurred, need to run finally, fault and catch handlers..
1704 frame->ex = child_frame.ex;
1705 goto handle_finally;
1708 /* need to handle typedbyref ... */
1709 if (csignature->ret->type != MONO_TYPE_VOID) {
1715 MINT_IN_CASE(MINT_CALLI_NAT) {
1716 MonoMethodSignature *csignature;
1717 stackval *endsp = sp;
1718 unsigned char *code = NULL;
1722 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1727 child_frame.runtime_method = NULL;
1730 child_frame.retval = sp;
1731 /* decrement by the actual number of args */
1732 sp -= csignature->param_count;
1733 child_frame.stack_args = sp;
1734 if (csignature->hasthis) {
1736 child_frame.obj = sp->data.p;
1738 child_frame.obj = NULL;
1740 ves_pinvoke_method (&child_frame, csignature, (MonoFuncV) code, FALSE, context);
1742 context->current_frame = frame;
1744 if (child_frame.ex) {
1746 * An exception occurred, need to run finally, fault and catch handlers..
1748 frame->ex = child_frame.ex;
1749 if (context->search_for_handler) {
1750 context->search_for_handler = 0;
1751 goto handle_exception;
1753 goto handle_finally;
1756 /* need to handle typedbyref ... */
1757 if (csignature->ret->type != MONO_TYPE_VOID) {
1763 MINT_IN_CASE(MINT_CALL) {
1764 stackval *endsp = sp;
1768 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1771 child_frame.retval = sp;
1772 /* decrement by the actual number of args */
1773 sp -= child_frame.runtime_method->param_count;
1774 child_frame.stack_args = sp;
1775 if (child_frame.runtime_method->hasthis) {
1777 child_frame.obj = sp->data.p;
1779 child_frame.obj = NULL;
1781 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->valuetype && ((MonoObject *)child_frame.obj)->vtable->klass == mono_defaults.transparent_proxy_class) {
1782 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1783 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1785 ves_exec_method_with_context (&child_frame, context);
1787 context->current_frame = frame;
1789 if (child_frame.ex) {
1791 * An exception occurred, need to run finally, fault and catch handlers..
1793 frame->ex = child_frame.ex;
1794 goto handle_finally;
1797 /* need to handle typedbyref ... */
1802 MINT_IN_CASE(MINT_VCALL) {
1805 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1809 child_frame.retval = sp;
1810 /* decrement by the actual number of args */
1811 sp -= child_frame.runtime_method->param_count;
1812 child_frame.stack_args = sp;
1813 if (child_frame.runtime_method->hasthis) {
1815 child_frame.obj = sp->data.p;
1817 child_frame.obj = NULL;
1820 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->valuetype && ((MonoObject *)child_frame.obj)->vtable->klass == mono_defaults.transparent_proxy_class) {
1821 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1822 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1825 ves_exec_method_with_context (&child_frame, context);
1827 context->current_frame = frame;
1829 if (child_frame.ex) {
1831 * An exception occurred, need to run finally, fault and catch handlers..
1833 frame->ex = child_frame.ex;
1834 goto handle_finally;
1838 MINT_IN_CASE(MINT_CALLVIRT) {
1839 stackval *endsp = sp;
1840 MonoObject *this_arg;
1845 token = * (unsigned short *)(ip + 1);
1847 child_frame.runtime_method = rtm->data_items [token];
1849 child_frame.retval = sp;
1851 /* decrement by the actual number of args */
1852 sp -= child_frame.runtime_method->param_count;
1853 child_frame.stack_args = sp;
1855 child_frame.obj = this_arg = sp->data.p;
1857 THROW_EX (mono_get_exception_null_reference(), ip - 2);
1858 child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
1860 if (this_arg->vtable->klass->valuetype && child_frame.runtime_method->valuetype) {
1861 child_frame.obj = (char *)this_arg + sizeof(MonoObject);
1864 ves_exec_method_with_context (&child_frame, context);
1866 context->current_frame = frame;
1868 if (child_frame.ex) {
1870 * An exception occurred, need to run finally, fault and catch handlers..
1872 frame->ex = child_frame.ex;
1873 if (context->search_for_handler) {
1874 context->search_for_handler = 0;
1875 goto handle_exception;
1877 goto handle_finally;
1880 /* need to handle typedbyref ... */
1885 MINT_IN_CASE(MINT_VCALLVIRT) {
1886 MonoObject *this_arg;
1891 token = * (unsigned short *)(ip + 1);
1893 child_frame.runtime_method = rtm->data_items [token];
1895 child_frame.retval = sp;
1897 /* decrement by the actual number of args */
1898 sp -= child_frame.runtime_method->param_count;
1899 child_frame.stack_args = sp;
1901 child_frame.obj = this_arg = sp->data.p;
1903 THROW_EX (mono_get_exception_null_reference(), ip - 2);
1904 child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
1906 if (this_arg->vtable->klass->valuetype && child_frame.runtime_method->valuetype) {
1907 child_frame.obj = (char *)this_arg + sizeof(MonoObject);
1910 ves_exec_method_with_context (&child_frame, context);
1912 context->current_frame = frame;
1914 if (child_frame.ex) {
1916 * An exception occurred, need to run finally, fault and catch handlers..
1918 frame->ex = child_frame.ex;
1919 if (context->search_for_handler) {
1920 context->search_for_handler = 0;
1921 goto handle_exception;
1923 goto handle_finally;
1927 MINT_IN_CASE(MINT_CALLINT)
1928 g_printerr ("doing MINT_CALLINT: %p\n", ((MonoMethodPInvoke*) frame->runtime_method->method)->addr);
1929 ves_pinvoke_method (frame, mono_method_signature (frame->runtime_method->method), ((MonoMethodPInvoke*) frame->runtime_method->method)->addr,
1930 frame->runtime_method->method->string_ctor, context);
1933 goto handle_exception;
1936 MINT_IN_CASE(MINT_CALLRUN)
1937 ves_runtime_method (frame, context);
1940 goto handle_exception;
1943 MINT_IN_CASE(MINT_RET)
1945 *frame->retval = *sp;
1946 if (sp > frame->stack)
1947 g_warning ("ret: more values on stack: %d", sp-frame->stack);
1949 MINT_IN_CASE(MINT_RET_VOID)
1950 if (sp > frame->stack)
1951 g_warning ("ret.void: more values on stack: %d", sp-frame->stack);
1953 MINT_IN_CASE(MINT_RET_VT)
1954 i32 = READ32(ip + 1);
1956 memcpy(frame->retval->data.p, sp->data.p, i32);
1957 if (sp > frame->stack)
1958 g_warning ("ret.vt: more values on stack: %d", sp-frame->stack);
1960 MINT_IN_CASE(MINT_BR_S)
1961 ip += (short) *(ip + 1);
1963 MINT_IN_CASE(MINT_BR)
1964 ip += (gint32) READ32(ip + 1);
1966 #define ZEROP_S(datamem, op) \
1968 if (sp->data.datamem op 0) \
1969 ip += * (gint16 *)(ip + 1); \
1973 #define ZEROP(datamem, op) \
1975 if (sp->data.datamem op 0) \
1976 ip += READ32(ip + 1); \
1980 MINT_IN_CASE(MINT_BRFALSE_I4_S)
1983 MINT_IN_CASE(MINT_BRFALSE_I8_S)
1986 MINT_IN_CASE(MINT_BRFALSE_R8_S)
1989 MINT_IN_CASE(MINT_BRFALSE_I4)
1992 MINT_IN_CASE(MINT_BRFALSE_I8)
1995 MINT_IN_CASE(MINT_BRFALSE_R8)
1998 MINT_IN_CASE(MINT_BRTRUE_I4_S)
2001 MINT_IN_CASE(MINT_BRTRUE_I8_S)
2004 MINT_IN_CASE(MINT_BRTRUE_R8_S)
2007 MINT_IN_CASE(MINT_BRTRUE_I4)
2010 MINT_IN_CASE(MINT_BRTRUE_I8)
2013 MINT_IN_CASE(MINT_BRTRUE_R8)
2016 #define CONDBR_S(cond) \
2019 ip += * (gint16 *)(ip + 1); \
2022 #define BRELOP_S(datamem, op) \
2023 CONDBR_S(sp[0].data.datamem op sp[1].data.datamem)
2025 #define CONDBR(cond) \
2028 ip += READ32(ip + 1); \
2032 #define BRELOP(datamem, op) \
2033 CONDBR(sp[0].data.datamem op sp[1].data.datamem)
2035 MINT_IN_CASE(MINT_BEQ_I4_S)
2038 MINT_IN_CASE(MINT_BEQ_I8_S)
2041 MINT_IN_CASE(MINT_BEQ_R8_S)
2042 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2044 MINT_IN_CASE(MINT_BEQ_I4)
2047 MINT_IN_CASE(MINT_BEQ_I8)
2050 MINT_IN_CASE(MINT_BEQ_R8)
2051 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2053 MINT_IN_CASE(MINT_BGE_I4_S)
2056 MINT_IN_CASE(MINT_BGE_I8_S)
2059 MINT_IN_CASE(MINT_BGE_R8_S)
2060 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
2062 MINT_IN_CASE(MINT_BGE_I4)
2065 MINT_IN_CASE(MINT_BGE_I8)
2068 MINT_IN_CASE(MINT_BGE_R8)
2069 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
2071 MINT_IN_CASE(MINT_BGT_I4_S)
2074 MINT_IN_CASE(MINT_BGT_I8_S)
2077 MINT_IN_CASE(MINT_BGT_R8_S)
2078 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2080 MINT_IN_CASE(MINT_BGT_I4)
2083 MINT_IN_CASE(MINT_BGT_I8)
2086 MINT_IN_CASE(MINT_BGT_R8)
2087 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2089 MINT_IN_CASE(MINT_BLT_I4_S)
2092 MINT_IN_CASE(MINT_BLT_I8_S)
2095 MINT_IN_CASE(MINT_BLT_R8_S)
2096 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2098 MINT_IN_CASE(MINT_BLT_I4)
2101 MINT_IN_CASE(MINT_BLT_I8)
2104 MINT_IN_CASE(MINT_BLT_R8)
2105 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2107 MINT_IN_CASE(MINT_BLE_I4_S)
2110 MINT_IN_CASE(MINT_BLE_I8_S)
2113 MINT_IN_CASE(MINT_BLE_R8_S)
2114 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2116 MINT_IN_CASE(MINT_BLE_I4)
2119 MINT_IN_CASE(MINT_BLE_I8)
2122 MINT_IN_CASE(MINT_BLE_R8)
2123 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2125 MINT_IN_CASE(MINT_BNE_UN_I4_S)
2128 MINT_IN_CASE(MINT_BNE_UN_I8_S)
2131 MINT_IN_CASE(MINT_BNE_UN_R8_S)
2132 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
2134 MINT_IN_CASE(MINT_BNE_UN_I4)
2137 MINT_IN_CASE(MINT_BNE_UN_I8)
2140 MINT_IN_CASE(MINT_BNE_UN_R8)
2141 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
2144 #define BRELOP_S_CAST(datamem, op, type) \
2146 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
2147 ip += * (gint16 *)(ip + 1); \
2151 #define BRELOP_CAST(datamem, op, type) \
2153 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
2154 ip += READ32(ip + 1); \
2158 MINT_IN_CASE(MINT_BGE_UN_I4_S)
2159 BRELOP_S_CAST(i, >=, guint32);
2161 MINT_IN_CASE(MINT_BGE_UN_I8_S)
2162 BRELOP_S_CAST(l, >=, guint64);
2164 MINT_IN_CASE(MINT_BGE_UN_R8_S)
2165 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
2167 MINT_IN_CASE(MINT_BGE_UN_I4)
2168 BRELOP_CAST(i, >=, guint32);
2170 MINT_IN_CASE(MINT_BGE_UN_I8)
2171 BRELOP_CAST(l, >=, guint64);
2173 MINT_IN_CASE(MINT_BGE_UN_R8)
2174 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
2176 MINT_IN_CASE(MINT_BGT_UN_I4_S)
2177 BRELOP_S_CAST(i, >, guint32);
2179 MINT_IN_CASE(MINT_BGT_UN_I8_S)
2180 BRELOP_S_CAST(l, >, guint64);
2182 MINT_IN_CASE(MINT_BGT_UN_R8_S)
2183 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2185 MINT_IN_CASE(MINT_BGT_UN_I4)
2186 BRELOP_CAST(i, >, guint32);
2188 MINT_IN_CASE(MINT_BGT_UN_I8)
2189 BRELOP_CAST(l, >, guint64);
2191 MINT_IN_CASE(MINT_BGT_UN_R8)
2192 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2194 MINT_IN_CASE(MINT_BLE_UN_I4_S)
2195 BRELOP_S_CAST(i, <=, guint32);
2197 MINT_IN_CASE(MINT_BLE_UN_I8_S)
2198 BRELOP_S_CAST(l, <=, guint64);
2200 MINT_IN_CASE(MINT_BLE_UN_R8_S)
2201 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2203 MINT_IN_CASE(MINT_BLE_UN_I4)
2204 BRELOP_CAST(i, <=, guint32);
2206 MINT_IN_CASE(MINT_BLE_UN_I8)
2207 BRELOP_CAST(l, <=, guint64);
2209 MINT_IN_CASE(MINT_BLE_UN_R8)
2210 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2212 MINT_IN_CASE(MINT_BLT_UN_I4_S)
2213 BRELOP_S_CAST(i, <, guint32);
2215 MINT_IN_CASE(MINT_BLT_UN_I8_S)
2216 BRELOP_S_CAST(l, <, guint64);
2218 MINT_IN_CASE(MINT_BLT_UN_R8_S)
2219 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2221 MINT_IN_CASE(MINT_BLT_UN_I4)
2222 BRELOP_CAST(i, <, guint32);
2224 MINT_IN_CASE(MINT_BLT_UN_I8)
2225 BRELOP_CAST(l, <, guint64);
2227 MINT_IN_CASE(MINT_BLT_UN_R8)
2228 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2230 MINT_IN_CASE(MINT_SWITCH) {
2232 const unsigned short *st;
2238 if ((guint32)sp->data.i < n) {
2240 ip += 2 * (guint32)sp->data.i;
2241 offset = READ32 (ip);
2248 MINT_IN_CASE(MINT_LDIND_I1)
2250 sp[-1].data.i = *(gint8*)sp[-1].data.p;
2252 MINT_IN_CASE(MINT_LDIND_U1)
2254 sp[-1].data.i = *(guint8*)sp[-1].data.p;
2256 MINT_IN_CASE(MINT_LDIND_I2)
2258 sp[-1].data.i = *(gint16*)sp[-1].data.p;
2260 MINT_IN_CASE(MINT_LDIND_U2)
2262 sp[-1].data.i = *(guint16*)sp[-1].data.p;
2264 MINT_IN_CASE(MINT_LDIND_I4) /* Fall through */
2265 MINT_IN_CASE(MINT_LDIND_U4)
2267 sp[-1].data.i = *(gint32*)sp[-1].data.p;
2269 MINT_IN_CASE(MINT_LDIND_I8)
2271 sp[-1].data.l = *(gint64*)sp[-1].data.p;
2273 MINT_IN_CASE(MINT_LDIND_I)
2275 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2277 MINT_IN_CASE(MINT_LDIND_R4)
2279 sp[-1].data.f = *(gfloat*)sp[-1].data.p;
2281 MINT_IN_CASE(MINT_LDIND_R8)
2283 sp[-1].data.f = *(gdouble*)sp[-1].data.p;
2285 MINT_IN_CASE(MINT_LDIND_REF)
2287 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2289 MINT_IN_CASE(MINT_STIND_REF)
2292 * (gpointer *) sp->data.p = sp[1].data.p;
2294 MINT_IN_CASE(MINT_STIND_I1)
2297 * (gint8 *) sp->data.p = (gint8)sp[1].data.i;
2299 MINT_IN_CASE(MINT_STIND_I2)
2302 * (gint16 *) sp->data.p = (gint16)sp[1].data.i;
2304 MINT_IN_CASE(MINT_STIND_I4)
2307 * (gint32 *) sp->data.p = sp[1].data.i;
2309 MINT_IN_CASE(MINT_STIND_I)
2312 * (mono_i *) sp->data.p = (mono_i)sp[1].data.p;
2314 MINT_IN_CASE(MINT_STIND_I8)
2317 * (gint64 *) sp->data.p = sp[1].data.l;
2319 MINT_IN_CASE(MINT_STIND_R4)
2322 * (float *) sp->data.p = (gfloat)sp[1].data.f;
2324 MINT_IN_CASE(MINT_STIND_R8)
2327 * (double *) sp->data.p = sp[1].data.f;
2329 #define BINOP(datamem, op) \
2331 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.datamem; \
2333 MINT_IN_CASE(MINT_ADD_I4)
2336 MINT_IN_CASE(MINT_ADD_I8)
2339 MINT_IN_CASE(MINT_ADD_R8)
2342 MINT_IN_CASE(MINT_ADD1_I4)
2346 MINT_IN_CASE(MINT_SUB_I4)
2349 MINT_IN_CASE(MINT_SUB_I8)
2352 MINT_IN_CASE(MINT_SUB_R8)
2355 MINT_IN_CASE(MINT_SUB1_I4)
2359 MINT_IN_CASE(MINT_MUL_I4)
2362 MINT_IN_CASE(MINT_MUL_I8)
2365 MINT_IN_CASE(MINT_MUL_R8)
2368 MINT_IN_CASE(MINT_DIV_I4)
2369 if (sp [-1].data.i == 0)
2370 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2371 if (sp [-1].data.i == (-1))
2372 THROW_EX (mono_get_exception_overflow (), ip);
2375 MINT_IN_CASE(MINT_DIV_I8)
2376 if (sp [-1].data.l == 0)
2377 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2378 if (sp [-1].data.l == (-1))
2379 THROW_EX (mono_get_exception_overflow (), ip);
2382 MINT_IN_CASE(MINT_DIV_R8)
2386 #define BINOP_CAST(datamem, op, type) \
2388 sp [-1].data.datamem = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
2390 MINT_IN_CASE(MINT_DIV_UN_I4)
2391 if (sp [-1].data.i == 0)
2392 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2393 BINOP_CAST(i, /, guint32);
2395 MINT_IN_CASE(MINT_DIV_UN_I8)
2396 if (sp [-1].data.l == 0)
2397 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2398 BINOP_CAST(l, /, guint64);
2400 MINT_IN_CASE(MINT_REM_I4)
2401 if (sp [-1].data.i == 0)
2402 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2405 MINT_IN_CASE(MINT_REM_I8)
2406 if (sp [-1].data.l == 0)
2407 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2410 MINT_IN_CASE(MINT_REM_R8)
2411 /* FIXME: what do we actually do here? */
2413 sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
2416 MINT_IN_CASE(MINT_REM_UN_I4)
2417 if (sp [-1].data.i == 0)
2418 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2419 BINOP_CAST(i, %, guint32);
2421 MINT_IN_CASE(MINT_REM_UN_I8)
2422 if (sp [-1].data.l == 0)
2423 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2424 BINOP_CAST(l, %, guint64);
2426 MINT_IN_CASE(MINT_AND_I4)
2429 MINT_IN_CASE(MINT_AND_I8)
2432 MINT_IN_CASE(MINT_OR_I4)
2435 MINT_IN_CASE(MINT_OR_I8)
2438 MINT_IN_CASE(MINT_XOR_I4)
2441 MINT_IN_CASE(MINT_XOR_I8)
2445 #define SHIFTOP(datamem, op) \
2447 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.i; \
2450 MINT_IN_CASE(MINT_SHL_I4)
2453 MINT_IN_CASE(MINT_SHL_I8)
2456 MINT_IN_CASE(MINT_SHR_I4)
2459 MINT_IN_CASE(MINT_SHR_I8)
2462 MINT_IN_CASE(MINT_SHR_UN_I4)
2464 sp [-1].data.i = (guint32)sp [-1].data.i >> sp [0].data.i;
2467 MINT_IN_CASE(MINT_SHR_UN_I8)
2469 sp [-1].data.l = (guint64)sp [-1].data.l >> sp [0].data.i;
2472 MINT_IN_CASE(MINT_NEG_I4)
2473 sp [-1].data.i = - sp [-1].data.i;
2476 MINT_IN_CASE(MINT_NEG_I8)
2477 sp [-1].data.l = - sp [-1].data.l;
2480 MINT_IN_CASE(MINT_NEG_R8)
2481 sp [-1].data.f = - sp [-1].data.f;
2484 MINT_IN_CASE(MINT_NOT_I4)
2485 sp [-1].data.i = ~ sp [-1].data.i;
2488 MINT_IN_CASE(MINT_NOT_I8)
2489 sp [-1].data.l = ~ sp [-1].data.l;
2492 MINT_IN_CASE(MINT_CONV_I1_I4)
2493 sp [-1].data.i = (gint8)sp [-1].data.i;
2496 MINT_IN_CASE(MINT_CONV_I1_I8)
2497 sp [-1].data.i = (gint8)sp [-1].data.l;
2500 MINT_IN_CASE(MINT_CONV_I1_R8)
2501 sp [-1].data.i = (gint8)sp [-1].data.f;
2504 MINT_IN_CASE(MINT_CONV_U1_I4)
2505 sp [-1].data.i = (guint8)sp [-1].data.i;
2508 MINT_IN_CASE(MINT_CONV_U1_I8)
2509 sp [-1].data.i = (guint8)sp [-1].data.l;
2512 MINT_IN_CASE(MINT_CONV_U1_R8)
2513 sp [-1].data.i = (guint8)sp [-1].data.f;
2516 MINT_IN_CASE(MINT_CONV_I2_I4)
2517 sp [-1].data.i = (gint16)sp [-1].data.i;
2520 MINT_IN_CASE(MINT_CONV_I2_I8)
2521 sp [-1].data.i = (gint16)sp [-1].data.l;
2524 MINT_IN_CASE(MINT_CONV_I2_R8)
2525 sp [-1].data.i = (gint16)sp [-1].data.f;
2528 MINT_IN_CASE(MINT_CONV_U2_I4)
2529 sp [-1].data.i = (guint16)sp [-1].data.i;
2532 MINT_IN_CASE(MINT_CONV_U2_I8)
2533 sp [-1].data.i = (guint16)sp [-1].data.l;
2536 MINT_IN_CASE(MINT_CONV_U2_R8)
2537 sp [-1].data.i = (guint16)sp [-1].data.f;
2540 MINT_IN_CASE(MINT_CONV_I4_R8)
2541 sp [-1].data.i = (gint32)sp [-1].data.f;
2544 MINT_IN_CASE(MINT_CONV_U4_I8)
2545 MINT_IN_CASE(MINT_CONV_I4_I8)
2546 sp [-1].data.i = (gint32)sp [-1].data.l;
2549 MINT_IN_CASE(MINT_CONV_I4_I8_SP)
2550 sp [-2].data.i = (gint32)sp [-2].data.l;
2553 MINT_IN_CASE(MINT_CONV_U4_R8)
2554 sp [-1].data.i = (guint32)sp [-1].data.f;
2557 MINT_IN_CASE(MINT_CONV_I8_I4)
2558 sp [-1].data.l = sp [-1].data.i;
2561 MINT_IN_CASE(MINT_CONV_I8_I4_SP)
2562 sp [-2].data.l = sp [-2].data.i;
2565 MINT_IN_CASE(MINT_CONV_I8_U4)
2566 sp [-1].data.l = (guint32)sp [-1].data.i;
2569 MINT_IN_CASE(MINT_CONV_I8_R8)
2570 sp [-1].data.l = (gint64)sp [-1].data.f;
2573 MINT_IN_CASE(MINT_CONV_R4_I4)
2574 sp [-1].data.f = (float)sp [-1].data.i;
2577 MINT_IN_CASE(MINT_CONV_R4_I8)
2578 sp [-1].data.f = (float)sp [-1].data.l;
2581 MINT_IN_CASE(MINT_CONV_R4_R8)
2582 sp [-1].data.f = (float)sp [-1].data.f;
2585 MINT_IN_CASE(MINT_CONV_R8_I4)
2586 sp [-1].data.f = (double)sp [-1].data.i;
2589 MINT_IN_CASE(MINT_CONV_R8_I8)
2590 sp [-1].data.f = (double)sp [-1].data.l;
2593 MINT_IN_CASE(MINT_CONV_U8_I4)
2594 sp [-1].data.l = sp [-1].data.i & 0xffffffff;
2597 MINT_IN_CASE(MINT_CONV_U8_R8)
2598 sp [-1].data.l = (guint64)sp [-1].data.f;
2602 MINT_IN_CASE(MINT_CPOBJ) {
2605 vtklass = rtm->data_items[READ32 (ip)];
2608 memcpy (sp [0].data.p, sp [1].data.p, mono_class_value_size (vtklass, NULL));
2612 MINT_IN_CASE(MINT_LDOBJ) {
2615 c = rtm->data_items[* (guint16 *)(ip + 1)];
2617 if (c->byval_arg.type != MONO_TYPE_VALUETYPE || c->byval_arg.data.klass->enumtype) {
2619 stackval_from_data (&c->byval_arg, &sp [-1], p, FALSE);
2621 size = mono_class_value_size (c, NULL);
2623 sp [-1].data.p = vt_sp;
2624 memcpy(vt_sp, p, size);
2625 vt_sp += (size + 7) & ~7;
2629 MINT_IN_CASE(MINT_LDSTR)
2630 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
2634 MINT_IN_CASE(MINT_NEWOBJ) {
2635 MonoClass *newobj_class;
2636 MonoMethodSignature *csig;
2637 stackval valuetype_this;
2643 token = * (guint16 *)(ip + 1);
2646 child_frame.runtime_method = rtm->data_items [token];
2647 csig = mono_method_signature (child_frame.runtime_method->method);
2648 newobj_class = child_frame.runtime_method->method->klass;
2649 /*if (profiling_classes) {
2650 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
2652 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
2656 if (newobj_class->parent == mono_defaults.array_class) {
2657 sp -= csig->param_count;
2658 o = ves_array_create (context->domain, newobj_class, csig, sp);
2659 goto array_constructed;
2663 * First arg is the object.
2665 if (newobj_class->valuetype) {
2666 if (!newobj_class->enumtype && (newobj_class->byval_arg.type == MONO_TYPE_VALUETYPE)) {
2667 child_frame.obj = vt_sp;
2668 valuetype_this.data.p = vt_sp;
2670 memset (&valuetype_this, 0, sizeof (stackval));
2671 child_frame.obj = &valuetype_this;
2674 if (newobj_class != mono_defaults.string_class) {
2675 context->managed_code = 0;
2676 o = mono_object_new_checked (context->domain, newobj_class, &error);
2677 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2678 context->managed_code = 1;
2679 if (*abort_requested)
2680 mono_thread_interruption_checkpoint ();
2681 child_frame.obj = o;
2683 child_frame.retval = &retval;
2687 if (csig->param_count) {
2688 sp -= csig->param_count;
2689 child_frame.stack_args = sp;
2691 child_frame.stack_args = NULL;
2694 g_assert (csig->call_convention == MONO_CALL_DEFAULT);
2696 child_frame.ip = NULL;
2697 child_frame.ex = NULL;
2699 ves_exec_method_with_context (&child_frame, context);
2701 context->current_frame = frame;
2703 if (child_frame.ex) {
2705 * An exception occurred, need to run finally, fault and catch handlers..
2707 frame->ex = child_frame.ex;
2708 goto handle_finally;
2711 * a constructor returns void, but we need to return the object we created
2714 if (newobj_class->valuetype && !newobj_class->enumtype) {
2715 *sp = valuetype_this;
2716 } else if (newobj_class == mono_defaults.string_class) {
2724 MINT_IN_CASE(MINT_CASTCLASS)
2725 c = rtm->data_items [*(guint16 *)(ip + 1)];
2726 if ((o = sp [-1].data.p)) {
2727 if (c->marshalbyref) {
2728 MonoObject *isinst_obj = mono_object_isinst_mbyref_checked (o, c, &error);
2729 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2731 THROW_EX (mono_get_exception_invalid_cast (), ip);
2733 MonoVTable *vt = o->vtable;
2734 MonoClass *oklass = vt->klass;
2735 if (c->flags & TYPE_ATTRIBUTE_INTERFACE) {
2736 g_error ("FIXME: interface method lookup");
2737 if (c->interface_id > vt->max_interface_id /* || vt->interface_offsets [c->interface_id] == 0 */) {
2738 THROW_EX (mono_get_exception_invalid_cast (), ip);
2740 } else if (c->rank) {
2741 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2742 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2744 THROW_EX (mono_get_exception_invalid_cast (), ip);
2745 } else if (!mono_class_has_parent (oklass, c)) {
2746 THROW_EX (mono_get_exception_invalid_cast (), ip);
2752 MINT_IN_CASE(MINT_ISINST)
2753 c = rtm->data_items [*(guint16 *)(ip + 1)];
2754 if ((o = sp [-1].data.p)) {
2755 if (c->marshalbyref) {
2756 MonoObject *isinst_obj = mono_object_isinst_mbyref_checked (o, c, &error);
2757 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2759 sp [-1].data.p = NULL;
2761 MonoVTable *vt = o->vtable;
2762 MonoClass *oklass = vt->klass;
2763 if (c->flags & TYPE_ATTRIBUTE_INTERFACE) {
2764 g_error ("FIXME: interface method lookup");
2765 if (c->interface_id > vt->max_interface_id /* || vt->interface_offsets [c->interface_id] == 0 */) {
2766 sp [-1].data.p = NULL;
2768 } else if (c->rank) {
2769 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2770 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2772 sp [-1].data.p = NULL;
2773 } else if (!mono_class_has_parent (oklass, c)) {
2774 sp [-1].data.p = NULL;
2780 MINT_IN_CASE(MINT_CONV_R_UN_I4)
2781 sp [-1].data.f = (double)(guint32)sp [-1].data.i;
2784 MINT_IN_CASE(MINT_CONV_R_UN_I8)
2785 sp [-1].data.f = (double)(guint64)sp [-1].data.l;
2788 MINT_IN_CASE(MINT_UNBOX)
2789 c = rtm->data_items[*(guint16 *)(ip + 1)];
2793 THROW_EX (mono_get_exception_null_reference(), ip);
2795 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2796 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2798 ((o->vtable->klass->rank == 0) &&
2799 (o->vtable->klass->element_class == c->element_class))))
2800 THROW_EX (mono_get_exception_invalid_cast (), ip);
2802 sp [-1].data.p = (char *)o + sizeof (MonoObject);
2805 MINT_IN_CASE(MINT_THROW)
2807 frame->ex_handler = NULL;
2809 sp->data.p = mono_get_exception_null_reference ();
2810 THROW_EX ((MonoException *)sp->data.p, ip);
2812 MINT_IN_CASE(MINT_LDFLDA)
2815 THROW_EX (mono_get_exception_null_reference (), ip);
2816 sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
2819 MINT_IN_CASE(MINT_CKNULL)
2822 THROW_EX (mono_get_exception_null_reference (), ip);
2826 #define LDFLD(datamem, fieldtype) \
2827 o = sp [-1].data.p; \
2829 THROW_EX (mono_get_exception_null_reference (), ip); \
2830 sp[-1].data.datamem = * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) ; \
2833 MINT_IN_CASE(MINT_LDFLD_I1) LDFLD(i, gint8); MINT_IN_BREAK;
2834 MINT_IN_CASE(MINT_LDFLD_U1) LDFLD(i, guint8); MINT_IN_BREAK;
2835 MINT_IN_CASE(MINT_LDFLD_I2) LDFLD(i, gint16); MINT_IN_BREAK;
2836 MINT_IN_CASE(MINT_LDFLD_U2) LDFLD(i, guint16); MINT_IN_BREAK;
2837 MINT_IN_CASE(MINT_LDFLD_I4) LDFLD(i, gint32); MINT_IN_BREAK;
2838 MINT_IN_CASE(MINT_LDFLD_I8) LDFLD(l, gint64); MINT_IN_BREAK;
2839 MINT_IN_CASE(MINT_LDFLD_R4) LDFLD(f, float); MINT_IN_BREAK;
2840 MINT_IN_CASE(MINT_LDFLD_R8) LDFLD(f, double); MINT_IN_BREAK;
2841 MINT_IN_CASE(MINT_LDFLD_O) LDFLD(p, gpointer); MINT_IN_BREAK;
2842 MINT_IN_CASE(MINT_LDFLD_P) LDFLD(p, gpointer); MINT_IN_BREAK;
2844 MINT_IN_CASE(MINT_LDFLD_VT)
2847 THROW_EX (mono_get_exception_null_reference (), ip);
2848 i32 = READ32(ip + 2);
2849 sp [-1].data.p = vt_sp;
2850 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
2851 vt_sp += (i32 + 7) & ~7;
2855 MINT_IN_CASE(MINT_LDRMFLD) {
2857 MonoClassField *field;
2862 THROW_EX (mono_get_exception_null_reference (), ip);
2863 field = rtm->data_items[* (guint16 *)(ip + 1)];
2865 if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2866 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2868 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
2869 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2871 addr = (char*)o + field->offset;
2874 stackval_from_data (field->type, &sp [-1], addr, FALSE);
2878 MINT_IN_CASE(MINT_LDRMFLD_VT) {
2879 MonoClassField *field;
2885 THROW_EX (mono_get_exception_null_reference (), ip);
2886 field = rtm->data_items[* (guint16 *)(ip + 1)];
2887 i32 = READ32(ip + 2);
2889 if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2890 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2891 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
2892 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2894 addr = (char*)o + field->offset;
2897 sp [-1].data.p = vt_sp;
2898 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
2899 vt_sp += (i32 + 7) & ~7;
2900 memcpy(sp [-1].data.p, addr, i32);
2904 #define STFLD(datamem, fieldtype) \
2905 o = sp [-2].data.p; \
2907 THROW_EX (mono_get_exception_null_reference (), ip); \
2909 * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) = sp[1].data.datamem; \
2912 MINT_IN_CASE(MINT_STFLD_I1) STFLD(i, gint8); MINT_IN_BREAK;
2913 MINT_IN_CASE(MINT_STFLD_U1) STFLD(i, guint8); MINT_IN_BREAK;
2914 MINT_IN_CASE(MINT_STFLD_I2) STFLD(i, gint16); MINT_IN_BREAK;
2915 MINT_IN_CASE(MINT_STFLD_U2) STFLD(i, guint16); MINT_IN_BREAK;
2916 MINT_IN_CASE(MINT_STFLD_I4) STFLD(i, gint32); MINT_IN_BREAK;
2917 MINT_IN_CASE(MINT_STFLD_I8) STFLD(l, gint64); MINT_IN_BREAK;
2918 MINT_IN_CASE(MINT_STFLD_R4) STFLD(f, float); MINT_IN_BREAK;
2919 MINT_IN_CASE(MINT_STFLD_R8) STFLD(f, double); MINT_IN_BREAK;
2920 MINT_IN_CASE(MINT_STFLD_O) STFLD(p, gpointer); MINT_IN_BREAK;
2921 MINT_IN_CASE(MINT_STFLD_P) STFLD(p, gpointer); MINT_IN_BREAK;
2923 MINT_IN_CASE(MINT_STFLD_VT)
2926 THROW_EX (mono_get_exception_null_reference (), ip);
2927 i32 = READ32(ip + 2);
2929 memcpy((char *)o + * (guint16 *)(ip + 1), sp [1].data.p, i32);
2930 vt_sp -= (i32 + 7) & ~7;
2934 MINT_IN_CASE(MINT_STRMFLD) {
2935 MonoClassField *field;
2939 THROW_EX (mono_get_exception_null_reference (), ip);
2941 field = rtm->data_items[* (guint16 *)(ip + 1)];
2944 if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2945 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2946 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
2947 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2949 stackval_to_data (field->type, &sp [-1], (char*)o + field->offset, FALSE);
2954 MINT_IN_CASE(MINT_STRMFLD_VT) {
2955 MonoClassField *field;
2959 THROW_EX (mono_get_exception_null_reference (), ip);
2960 field = rtm->data_items[* (guint16 *)(ip + 1)];
2961 i32 = READ32(ip + 2);
2964 if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2965 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2966 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
2967 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2969 memcpy((char*)o + field->offset, sp [-1].data.p, i32);
2972 vt_sp -= (i32 + 7) & ~7;
2975 MINT_IN_CASE(MINT_LDSFLDA) {
2976 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
2977 MonoVTable *vt = mono_class_vtable (context->domain, field->parent);
2980 if (!vt->initialized) {
2982 mono_runtime_class_init_full (vt, &error);
2983 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2987 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
2988 sp->data.p = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
2990 sp->data.p = (char*)(vt->vtable) + field->offset;
2994 MINT_IN_CASE(MINT_LDSFLD) {
2996 MonoClassField *field;
2999 field = rtm->data_items[*(guint16 *)(ip + 1)];
3000 vt = rtm->data_items [*(guint16 *)(ip + 2)];
3001 if (!vt->initialized) {
3003 mono_runtime_class_init_full (vt, &error);
3004 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3007 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3008 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3010 addr = (char*)(vt->vtable) + field->offset;
3012 stackval_from_data (field->type, sp, addr, FALSE);
3016 MINT_IN_CASE(MINT_LDSFLD_I4) {
3017 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3018 MonoVTable *vt = rtm->data_items [*(guint16 *)(ip + 2)];
3019 if (!vt->initialized) {
3021 mono_runtime_class_init_full (vt, &error);
3022 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3025 sp->data.i = * (gint32 *)((char*)(vt->vtable) + field->offset);
3029 MINT_IN_CASE(MINT_LDSFLD_O) {
3030 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3031 MonoVTable *vt = rtm->data_items [*(guint16 *)(ip + 2)];
3032 if (!vt->initialized) {
3034 mono_runtime_class_init_full (vt, &error);
3035 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3038 sp->data.p = * (gpointer *)((char*)(vt->vtable) + field->offset);
3042 MINT_IN_CASE(MINT_LDSFLD_VT) {
3044 MonoClassField *field;
3049 token = * (guint16 *)(ip + 1);
3050 size = READ32(ip + 2);
3051 field = rtm->data_items[token];
3054 vt = mono_class_vtable (context->domain, field->parent);
3055 if (!vt->initialized) {
3057 mono_runtime_class_init_full (vt, &error);
3058 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3061 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3062 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3064 addr = (char*)(vt->vtable) + field->offset;
3067 vt_sp += (size + 7) & ~7;
3068 stackval_from_data (field->type, sp, addr, FALSE);
3072 MINT_IN_CASE(MINT_STSFLD) {
3074 MonoClassField *field;
3078 token = * (guint16 *)(ip + 1);
3079 field = rtm->data_items[token];
3083 vt = mono_class_vtable (context->domain, field->parent);
3084 if (!vt->initialized) {
3086 mono_runtime_class_init_full (vt, &error);
3087 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3090 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3091 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3093 addr = (char*)(vt->vtable) + field->offset;
3095 stackval_to_data (field->type, sp, addr, FALSE);
3098 MINT_IN_CASE(MINT_STSFLD_VT) {
3100 MonoClassField *field;
3105 token = * (guint16 *)(ip + 1);
3106 size = READ32(ip + 2);
3107 field = rtm->data_items[token];
3110 vt = mono_class_vtable (context->domain, field->parent);
3111 if (!vt->initialized) {
3113 mono_runtime_class_init_full (vt, &error);
3114 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3117 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3118 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3120 addr = (char*)(vt->vtable) + field->offset;
3122 stackval_to_data (field->type, sp, addr, FALSE);
3123 vt_sp -= (size + 7) & ~7;
3126 MINT_IN_CASE(MINT_STOBJ_VT) {
3128 c = rtm->data_items[* (guint16 *)(ip + 1)];
3130 size = mono_class_value_size (c, NULL);
3131 memcpy(sp [-2].data.p, sp [-1].data.p, size);
3132 vt_sp -= (size + 7) & ~7;
3136 MINT_IN_CASE(MINT_STOBJ) {
3138 c = rtm->data_items[* (guint16 *)(ip + 1)];
3140 size = mono_class_value_size (c, NULL);
3141 memcpy(sp [-2].data.p, &sp [-1].data, size);
3145 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_R8)
3146 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3147 THROW_EX (mono_get_exception_overflow (), ip);
3148 sp [-1].data.i = (guint32)sp [-1].data.f;
3151 MINT_IN_CASE(MINT_CONV_OVF_U8_I4)
3152 if (sp [-1].data.i < 0)
3153 THROW_EX (mono_get_exception_overflow (), ip);
3154 sp [-1].data.l = sp [-1].data.i;
3157 MINT_IN_CASE(MINT_CONV_OVF_U8_R8)
3158 MINT_IN_CASE(MINT_CONV_OVF_I8_UN_R8)
3159 if (sp [-1].data.f < 0 || sp [-1].data.f > 9223372036854775807LL)
3160 THROW_EX (mono_get_exception_overflow (), ip);
3161 sp [-1].data.l = (guint64)sp [-1].data.f;
3164 MINT_IN_CASE(MINT_CONV_OVF_I8_R8)
3165 if (sp [-1].data.f < MYGINT64_MIN || sp [-1].data.f > MYGINT64_MAX)
3166 THROW_EX (mono_get_exception_overflow (), ip);
3167 sp [-1].data.l = (gint64)sp [-1].data.f;
3170 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_I8)
3171 if ((mono_u)sp [-1].data.l > MYGUINT32_MAX)
3172 THROW_EX (mono_get_exception_overflow (), ip);
3173 sp [-1].data.i = (mono_u)sp [-1].data.l;
3176 MINT_IN_CASE(MINT_BOX)
3177 c = rtm->data_items [* (guint16 *)(ip + 1)];
3179 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
3180 int size = mono_class_value_size (c, NULL);
3181 sp [-1].data.p = mono_value_box_checked (context->domain, c, sp [-1].data.p, &error);
3182 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3183 size = (size + 7) & ~7;
3187 stackval_to_data (&c->byval_arg, &sp [-1], (char*)&sp [-1], FALSE);
3188 sp [-1].data.p = mono_value_box_checked (context->domain, c, &sp [-1], &error);
3189 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3193 MINT_IN_CASE(MINT_NEWARR)
3194 sp [-1].data.p = (MonoObject*) mono_array_new_checked (context->domain, rtm->data_items[*(guint16 *)(ip + 1)], sp [-1].data.i, &error);
3195 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3197 /*if (profiling_classes) {
3198 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
3200 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
3204 MINT_IN_CASE(MINT_LDLEN)
3207 THROW_EX (mono_get_exception_null_reference (), ip);
3208 sp [-1].data.nati = mono_array_length ((MonoArray *)o);
3211 MINT_IN_CASE(MINT_GETCHR) {
3215 THROW_EX (mono_get_exception_null_reference (), ip);
3216 i32 = sp [-1].data.i;
3217 if (i32 < 0 || i32 >= mono_string_length (s))
3218 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3220 sp [-1].data.i = mono_string_chars(s)[i32];
3224 MINT_IN_CASE(MINT_STRLEN)
3226 sp [-1].data.i = mono_string_length ((MonoString*)sp [-1].data.p);
3228 MINT_IN_CASE(MINT_ARRAY_RANK)
3231 THROW_EX (mono_get_exception_null_reference (), ip);
3232 sp [-1].data.i = mono_object_class (sp [-1].data.p)->rank;
3235 MINT_IN_CASE(MINT_LDELEMA) {
3239 /*token = READ32 (ip)*/;
3245 aindex = sp [1].data.i;
3246 if (aindex >= mono_array_length ((MonoArray *) o))
3247 THROW_EX (mono_get_exception_index_out_of_range (), ip - 2);
3249 /* check the array element corresponds to token */
3250 esize = mono_array_element_size (((MonoArray *) o)->obj.vtable->klass);
3252 sp->data.p = mono_array_addr_with_size ((MonoArray *) o, esize, aindex);
3256 MINT_IN_CASE(MINT_LDELEM_I1) /* fall through */
3257 MINT_IN_CASE(MINT_LDELEM_U1) /* fall through */
3258 MINT_IN_CASE(MINT_LDELEM_I2) /* fall through */
3259 MINT_IN_CASE(MINT_LDELEM_U2) /* fall through */
3260 MINT_IN_CASE(MINT_LDELEM_I4) /* fall through */
3261 MINT_IN_CASE(MINT_LDELEM_U4) /* fall through */
3262 MINT_IN_CASE(MINT_LDELEM_I8) /* fall through */
3263 MINT_IN_CASE(MINT_LDELEM_I) /* fall through */
3264 MINT_IN_CASE(MINT_LDELEM_R4) /* fall through */
3265 MINT_IN_CASE(MINT_LDELEM_R8) /* fall through */
3266 MINT_IN_CASE(MINT_LDELEM_REF) {
3274 THROW_EX (mono_get_exception_null_reference (), ip);
3276 aindex = sp [1].data.i;
3277 if (aindex >= mono_array_length (o))
3278 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3281 * FIXME: throw mono_get_exception_array_type_mismatch () if needed
3284 case MINT_LDELEM_I1:
3285 sp [0].data.i = mono_array_get (o, gint8, aindex);
3287 case MINT_LDELEM_U1:
3288 sp [0].data.i = mono_array_get (o, guint8, aindex);
3290 case MINT_LDELEM_I2:
3291 sp [0].data.i = mono_array_get (o, gint16, aindex);
3293 case MINT_LDELEM_U2:
3294 sp [0].data.i = mono_array_get (o, guint16, aindex);
3297 sp [0].data.nati = mono_array_get (o, mono_i, aindex);
3299 case MINT_LDELEM_I4:
3300 sp [0].data.i = mono_array_get (o, gint32, aindex);
3302 case MINT_LDELEM_U4:
3303 sp [0].data.i = mono_array_get (o, guint32, aindex);
3305 case MINT_LDELEM_I8:
3306 sp [0].data.l = mono_array_get (o, guint64, aindex);
3308 case MINT_LDELEM_R4:
3309 sp [0].data.f = mono_array_get (o, float, aindex);
3311 case MINT_LDELEM_R8:
3312 sp [0].data.f = mono_array_get (o, double, aindex);
3314 case MINT_LDELEM_REF:
3315 sp [0].data.p = mono_array_get (o, gpointer, aindex);
3325 MINT_IN_CASE(MINT_STELEM_I) /* fall through */
3326 MINT_IN_CASE(MINT_STELEM_I1) /* fall through */
3327 MINT_IN_CASE(MINT_STELEM_I2) /* fall through */
3328 MINT_IN_CASE(MINT_STELEM_I4) /* fall through */
3329 MINT_IN_CASE(MINT_STELEM_I8) /* fall through */
3330 MINT_IN_CASE(MINT_STELEM_R4) /* fall through */
3331 MINT_IN_CASE(MINT_STELEM_R8) /* fall through */
3332 MINT_IN_CASE(MINT_STELEM_REF) {
3339 THROW_EX (mono_get_exception_null_reference (), ip);
3341 aindex = sp [1].data.i;
3342 if (aindex >= mono_array_length ((MonoArray *)o))
3343 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3347 mono_array_set ((MonoArray *)o, mono_i, aindex, sp [2].data.nati);
3349 case MINT_STELEM_I1:
3350 mono_array_set ((MonoArray *)o, gint8, aindex, sp [2].data.i);
3352 case MINT_STELEM_I2:
3353 mono_array_set ((MonoArray *)o, gint16, aindex, sp [2].data.i);
3355 case MINT_STELEM_I4:
3356 mono_array_set ((MonoArray *)o, gint32, aindex, sp [2].data.i);
3358 case MINT_STELEM_I8:
3359 mono_array_set ((MonoArray *)o, gint64, aindex, sp [2].data.l);
3361 case MINT_STELEM_R4:
3362 mono_array_set ((MonoArray *)o, float, aindex, sp [2].data.f);
3364 case MINT_STELEM_R8:
3365 mono_array_set ((MonoArray *)o, double, aindex, sp [2].data.f);
3367 case MINT_STELEM_REF: {
3368 MonoObject *isinst_obj = mono_object_isinst_checked (sp [2].data.p, mono_object_class (o)->element_class, &error);
3369 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3370 if (sp [2].data.p && !isinst_obj)
3371 THROW_EX (mono_get_exception_array_type_mismatch (), ip);
3372 mono_array_set ((MonoArray *)o, gpointer, aindex, sp [2].data.p);
3381 MINT_IN_CASE(MINT_CONV_OVF_I4_U4)
3382 if (sp [-1].data.i < 0)
3383 THROW_EX (mono_get_exception_overflow (), ip);
3386 MINT_IN_CASE(MINT_CONV_OVF_I4_I8)
3387 if (sp [-1].data.l < MYGINT32_MIN || sp [-1].data.l > MYGINT32_MAX)
3388 THROW_EX (mono_get_exception_overflow (), ip);
3389 sp [-1].data.i = (gint32) sp [-1].data.l;
3392 MINT_IN_CASE(MINT_CONV_OVF_I4_R8)
3393 if (sp [-1].data.f < MYGINT32_MIN || sp [-1].data.f > MYGINT32_MAX)
3394 THROW_EX (mono_get_exception_overflow (), ip);
3395 sp [-1].data.i = (gint32) sp [-1].data.f;
3398 MINT_IN_CASE(MINT_CONV_OVF_U4_I4)
3399 if (sp [-1].data.i < 0)
3400 THROW_EX (mono_get_exception_overflow (), ip);
3403 MINT_IN_CASE(MINT_CONV_OVF_U4_I8)
3404 if (sp [-1].data.l < 0 || sp [-1].data.l > MYGUINT32_MAX)
3405 THROW_EX (mono_get_exception_overflow (), ip);
3406 sp [-1].data.i = (guint32) sp [-1].data.l;
3409 MINT_IN_CASE(MINT_CONV_OVF_U4_R8)
3410 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3411 THROW_EX (mono_get_exception_overflow (), ip);
3412 sp [-1].data.i = (guint32) sp [-1].data.f;
3415 MINT_IN_CASE(MINT_CONV_OVF_I2_I4)
3416 if (sp [-1].data.i < -32768 || sp [-1].data.i > 32767)
3417 THROW_EX (mono_get_exception_overflow (), ip);
3420 MINT_IN_CASE(MINT_CONV_OVF_I2_I8)
3421 if (sp [-1].data.l < -32768 || sp [-1].data.l > 32767)
3422 THROW_EX (mono_get_exception_overflow (), ip);
3423 sp [-1].data.i = (gint16) sp [-1].data.l;
3426 MINT_IN_CASE(MINT_CONV_OVF_I2_R8)
3427 if (sp [-1].data.f < -32768 || sp [-1].data.f > 32767)
3428 THROW_EX (mono_get_exception_overflow (), ip);
3429 sp [-1].data.i = (gint16) sp [-1].data.f;
3432 MINT_IN_CASE(MINT_CONV_OVF_U2_I4)
3433 if (sp [-1].data.i < 0 || sp [-1].data.i > 65535)
3434 THROW_EX (mono_get_exception_overflow (), ip);
3437 MINT_IN_CASE(MINT_CONV_OVF_U2_I8)
3438 if (sp [-1].data.l < 0 || sp [-1].data.l > 65535)
3439 THROW_EX (mono_get_exception_overflow (), ip);
3440 sp [-1].data.i = (guint16) sp [-1].data.l;
3443 MINT_IN_CASE(MINT_CONV_OVF_U2_R8)
3444 if (sp [-1].data.f < 0 || sp [-1].data.f > 65535)
3445 THROW_EX (mono_get_exception_overflow (), ip);
3446 sp [-1].data.i = (guint16) sp [-1].data.f;
3449 MINT_IN_CASE(MINT_CONV_OVF_I1_I4)
3450 if (sp [-1].data.i < -128 || sp [-1].data.i > 127)
3451 THROW_EX (mono_get_exception_overflow (), ip);
3454 MINT_IN_CASE(MINT_CONV_OVF_I1_I8)
3455 if (sp [-1].data.l < -128 || sp [-1].data.l > 127)
3456 THROW_EX (mono_get_exception_overflow (), ip);
3457 sp [-1].data.i = (gint8) sp [-1].data.l;
3460 MINT_IN_CASE(MINT_CONV_OVF_I1_R8)
3461 if (sp [-1].data.f < -128 || sp [-1].data.f > 127)
3462 THROW_EX (mono_get_exception_overflow (), ip);
3463 sp [-1].data.i = (gint8) sp [-1].data.f;
3466 MINT_IN_CASE(MINT_CONV_OVF_U1_I4)
3467 if (sp [-1].data.i < 0 || sp [-1].data.i > 255)
3468 THROW_EX (mono_get_exception_overflow (), ip);
3471 MINT_IN_CASE(MINT_CONV_OVF_U1_I8)
3472 if (sp [-1].data.l < 0 || sp [-1].data.l > 255)
3473 THROW_EX (mono_get_exception_overflow (), ip);
3474 sp [-1].data.i = (guint8) sp [-1].data.l;
3477 MINT_IN_CASE(MINT_CONV_OVF_U1_R8)
3478 if (sp [-1].data.f < 0 || sp [-1].data.f > 255)
3479 THROW_EX (mono_get_exception_overflow (), ip);
3480 sp [-1].data.i = (guint8) sp [-1].data.f;
3484 MINT_IN_CASE(MINT_LDELEM)
3485 MINT_IN_CASE(MINT_STELEM)
3486 MINT_IN_CASE(MINT_UNBOX_ANY)
3488 MINT_IN_CASE(MINT_REFANYVAL) ves_abort(); MINT_IN_BREAK;
3490 MINT_IN_CASE(MINT_CKFINITE)
3491 if (!isfinite(sp [-1].data.f))
3492 THROW_EX (mono_get_exception_arithmetic (), ip);
3496 MINT_IN_CASE(MINT_MKREFANY) ves_abort(); MINT_IN_BREAK;
3498 MINT_IN_CASE(MINT_LDTOKEN)
3501 * (gpointer *)sp->data.p = rtm->data_items[*(guint16 *)(ip + 1)];
3505 MINT_IN_CASE(MINT_ADD_OVF_I4)
3506 if (CHECK_ADD_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3507 THROW_EX (mono_get_exception_overflow (), ip);
3510 MINT_IN_CASE(MINT_ADD_OVF_I8)
3511 if (CHECK_ADD_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3512 THROW_EX (mono_get_exception_overflow (), ip);
3515 MINT_IN_CASE(MINT_ADD_OVF_UN_I4)
3516 if (CHECK_ADD_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3517 THROW_EX (mono_get_exception_overflow (), ip);
3518 BINOP_CAST(i, +, guint32);
3520 MINT_IN_CASE(MINT_ADD_OVF_UN_I8)
3521 if (CHECK_ADD_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3522 THROW_EX (mono_get_exception_overflow (), ip);
3523 BINOP_CAST(l, +, guint64);
3525 MINT_IN_CASE(MINT_MUL_OVF_I4)
3526 if (CHECK_MUL_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3527 THROW_EX (mono_get_exception_overflow (), ip);
3530 MINT_IN_CASE(MINT_MUL_OVF_I8)
3531 if (CHECK_MUL_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3532 THROW_EX (mono_get_exception_overflow (), ip);
3535 MINT_IN_CASE(MINT_MUL_OVF_UN_I4)
3536 if (CHECK_MUL_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3537 THROW_EX (mono_get_exception_overflow (), ip);
3538 BINOP_CAST(i, *, guint32);
3540 MINT_IN_CASE(MINT_MUL_OVF_UN_I8)
3541 if (CHECK_MUL_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3542 THROW_EX (mono_get_exception_overflow (), ip);
3543 BINOP_CAST(l, *, guint64);
3545 MINT_IN_CASE(MINT_SUB_OVF_I4)
3546 if (CHECK_SUB_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3547 THROW_EX (mono_get_exception_overflow (), ip);
3550 MINT_IN_CASE(MINT_SUB_OVF_I8)
3551 if (CHECK_SUB_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3552 THROW_EX (mono_get_exception_overflow (), ip);
3555 MINT_IN_CASE(MINT_SUB_OVF_UN_I4)
3556 if (CHECK_SUB_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3557 THROW_EX (mono_get_exception_overflow (), ip);
3558 BINOP_CAST(i, -, guint32);
3560 MINT_IN_CASE(MINT_SUB_OVF_UN_I8)
3561 if (CHECK_SUB_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3562 THROW_EX (mono_get_exception_overflow (), ip);
3563 BINOP_CAST(l, -, guint64);
3565 MINT_IN_CASE(MINT_ENDFINALLY)
3567 ip = finally_ips->data;
3568 finally_ips = g_slist_remove (finally_ips, ip);
3575 MINT_IN_CASE(MINT_LEAVE) /* Fall through */
3576 MINT_IN_CASE(MINT_LEAVE_S)
3577 while (sp > frame->stack) {
3581 if (*ip == MINT_LEAVE_S) {
3582 ip += (short) *(ip + 1);
3584 ip += (gint32) READ32 (ip + 1);
3587 if (frame->ex_handler != NULL && MONO_OFFSET_IN_HANDLER(frame->ex_handler, frame->ip - rtm->code)) {
3588 frame->ex_handler = NULL;
3591 goto handle_finally;
3593 MINT_IN_CASE(MINT_ICALL_V_V)
3594 MINT_IN_CASE(MINT_ICALL_P_V)
3595 MINT_IN_CASE(MINT_ICALL_P_P)
3596 MINT_IN_CASE(MINT_ICALL_PP_V)
3597 MINT_IN_CASE(MINT_ICALL_PI_V)
3598 MINT_IN_CASE(MINT_ICALL_PP_P)
3599 MINT_IN_CASE(MINT_ICALL_PI_P)
3600 MINT_IN_CASE(MINT_ICALL_PPP_V)
3601 MINT_IN_CASE(MINT_ICALL_PPI_V)
3602 sp = do_icall (context, *ip, sp, rtm->data_items [*(guint16 *)(ip + 1)]);
3603 if (frame->ex != NULL)
3604 goto handle_exception;
3607 MINT_IN_CASE(MINT_MONO_LDPTR)
3608 sp->data.p = rtm->data_items [*(guint16 *)(ip + 1)];
3612 MINT_IN_CASE(MINT_MONO_NEWOBJ)
3613 sp->data.p = mono_object_new_checked (context->domain, rtm->data_items [*(guint16 *)(ip + 1)], &error);
3614 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3618 MINT_IN_CASE(MINT_MONO_FREE)
3621 g_error ("that doesn't seem right");
3622 g_free (sp->data.p);
3624 MINT_IN_CASE(MINT_MONO_RETOBJ)
3627 stackval_from_data (mono_method_signature (frame->runtime_method->method)->ret, frame->retval, sp->data.p,
3628 mono_method_signature (frame->runtime_method->method)->pinvoke);
3629 if (sp > frame->stack)
3630 g_warning ("retobj: more values on stack: %d", sp-frame->stack);
3633 #define RELOP(datamem, op) \
3635 sp [-1].data.i = sp [-1].data.datamem op sp [0].data.datamem; \
3637 MINT_IN_CASE(MINT_CEQ_I4)
3640 MINT_IN_CASE(MINT_CEQ0_I4)
3641 sp [-1].data.i = (sp [-1].data.i == 0);
3644 MINT_IN_CASE(MINT_CEQ_I8)
3647 MINT_IN_CASE(MINT_CEQ_R8)
3649 if (isunordered (sp [-1].data.f, sp [0].data.f))
3652 sp [-1].data.i = sp [-1].data.f == sp [0].data.f;
3655 MINT_IN_CASE(MINT_CGT_I4)
3658 MINT_IN_CASE(MINT_CGT_I8)
3661 MINT_IN_CASE(MINT_CGT_R8)
3663 if (isunordered (sp [-1].data.f, sp [0].data.f))
3666 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3670 #define RELOP_CAST(datamem, op, type) \
3672 sp [-1].data.i = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
3675 MINT_IN_CASE(MINT_CGT_UN_I4)
3676 RELOP_CAST(i, >, guint32);
3678 MINT_IN_CASE(MINT_CGT_UN_I8)
3679 RELOP_CAST(l, >, guint64);
3681 MINT_IN_CASE(MINT_CGT_UN_R8)
3683 if (isunordered (sp [-1].data.f, sp [0].data.f))
3686 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3689 MINT_IN_CASE(MINT_CLT_I4)
3692 MINT_IN_CASE(MINT_CLT_I8)
3695 MINT_IN_CASE(MINT_CLT_R8)
3697 if (isunordered (sp [-1].data.f, sp [0].data.f))
3700 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3703 MINT_IN_CASE(MINT_CLT_UN_I4)
3704 RELOP_CAST(i, <, guint32);
3706 MINT_IN_CASE(MINT_CLT_UN_I8)
3707 RELOP_CAST(l, <, guint64);
3709 MINT_IN_CASE(MINT_CLT_UN_R8)
3711 if (isunordered (sp [-1].data.f, sp [0].data.f))
3714 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3717 MINT_IN_CASE(MINT_LDFTN) {
3718 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
3723 MINT_IN_CASE(MINT_LDVIRTFTN) {
3724 RuntimeMethod *m = rtm->data_items [* (guint16 *)(ip + 1)];
3728 THROW_EX (mono_get_exception_null_reference (), ip - 2);
3730 sp->data.p = get_virtual_method (context->domain, m, sp->data.p);
3735 MINT_IN_CASE(MINT_LDTHIS)
3736 sp->data.p = frame->obj;
3740 MINT_IN_CASE(MINT_STTHIS)
3742 frame->obj = sp->data.p;
3745 MINT_IN_CASE(MINT_LDTHISA)
3746 sp->data.p = &frame->obj;
3751 #define LDARG(datamem, argtype) \
3752 sp->data.datamem = * (argtype *)(frame->args + * (guint16 *)(ip + 1)); \
3756 MINT_IN_CASE(MINT_LDARG_I1) LDARG(i, gint8); MINT_IN_BREAK;
3757 MINT_IN_CASE(MINT_LDARG_U1) LDARG(i, guint8); MINT_IN_BREAK;
3758 MINT_IN_CASE(MINT_LDARG_I2) LDARG(i, gint16); MINT_IN_BREAK;
3759 MINT_IN_CASE(MINT_LDARG_U2) LDARG(i, guint16); MINT_IN_BREAK;
3760 MINT_IN_CASE(MINT_LDARG_I4) LDARG(i, gint32); MINT_IN_BREAK;
3761 MINT_IN_CASE(MINT_LDARG_I8) LDARG(l, gint64); MINT_IN_BREAK;
3762 MINT_IN_CASE(MINT_LDARG_R4) LDARG(f, float); MINT_IN_BREAK;
3763 MINT_IN_CASE(MINT_LDARG_R8) LDARG(f, double); MINT_IN_BREAK;
3764 MINT_IN_CASE(MINT_LDARG_O) LDARG(p, gpointer); MINT_IN_BREAK;
3765 MINT_IN_CASE(MINT_LDARG_P) LDARG(p, gpointer); MINT_IN_BREAK;
3767 MINT_IN_CASE(MINT_LDARG_VT)
3769 i32 = READ32(ip + 2);
3770 memcpy(sp->data.p, frame->args + * (guint16 *)(ip + 1), i32);
3771 vt_sp += (i32 + 7) & ~7;
3776 #define STARG(datamem, argtype) \
3778 * (argtype *)(frame->args + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3781 MINT_IN_CASE(MINT_STARG_I1) STARG(i, gint8); MINT_IN_BREAK;
3782 MINT_IN_CASE(MINT_STARG_U1) STARG(i, guint8); MINT_IN_BREAK;
3783 MINT_IN_CASE(MINT_STARG_I2) STARG(i, gint16); MINT_IN_BREAK;
3784 MINT_IN_CASE(MINT_STARG_U2) STARG(i, guint16); MINT_IN_BREAK;
3785 MINT_IN_CASE(MINT_STARG_I4) STARG(i, gint32); MINT_IN_BREAK;
3786 MINT_IN_CASE(MINT_STARG_I8) STARG(l, gint64); MINT_IN_BREAK;
3787 MINT_IN_CASE(MINT_STARG_R4) STARG(f, float); MINT_IN_BREAK;
3788 MINT_IN_CASE(MINT_STARG_R8) STARG(f, double); MINT_IN_BREAK;
3789 MINT_IN_CASE(MINT_STARG_O) STARG(p, gpointer); MINT_IN_BREAK;
3790 MINT_IN_CASE(MINT_STARG_P) STARG(p, gpointer); MINT_IN_BREAK;
3792 MINT_IN_CASE(MINT_STARG_VT)
3793 i32 = READ32(ip + 2);
3795 memcpy(frame->args + * (guint16 *)(ip + 1), sp->data.p, i32);
3796 vt_sp -= (i32 + 7) & ~7;
3800 #define STINARG(datamem, argtype) \
3802 int n = * (guint16 *)(ip + 1); \
3803 * (argtype *)(frame->args + rtm->arg_offsets [n]) = frame->stack_args [n].data.datamem; \
3807 MINT_IN_CASE(MINT_STINARG_I1) STINARG(i, gint8); MINT_IN_BREAK;
3808 MINT_IN_CASE(MINT_STINARG_U1) STINARG(i, guint8); MINT_IN_BREAK;
3809 MINT_IN_CASE(MINT_STINARG_I2) STINARG(i, gint16); MINT_IN_BREAK;
3810 MINT_IN_CASE(MINT_STINARG_U2) STINARG(i, guint16); MINT_IN_BREAK;
3811 MINT_IN_CASE(MINT_STINARG_I4) STINARG(i, gint32); MINT_IN_BREAK;
3812 MINT_IN_CASE(MINT_STINARG_I8) STINARG(l, gint64); MINT_IN_BREAK;
3813 MINT_IN_CASE(MINT_STINARG_R4) STINARG(f, float); MINT_IN_BREAK;
3814 MINT_IN_CASE(MINT_STINARG_R8) STINARG(f, double); MINT_IN_BREAK;
3815 MINT_IN_CASE(MINT_STINARG_O) STINARG(p, gpointer); MINT_IN_BREAK;
3816 MINT_IN_CASE(MINT_STINARG_P) STINARG(p, gpointer); MINT_IN_BREAK;
3818 MINT_IN_CASE(MINT_STINARG_VT) {
3819 int n = * (guint16 *)(ip + 1);
3820 i32 = READ32(ip + 2);
3821 memcpy (frame->args + rtm->arg_offsets [n], frame->stack_args [n].data.p, i32);
3826 MINT_IN_CASE(MINT_LDARGA)
3827 sp->data.p = frame->args + * (guint16 *)(ip + 1);
3832 #define LDLOC(datamem, argtype) \
3833 sp->data.datamem = * (argtype *)(locals + * (guint16 *)(ip + 1)); \
3837 MINT_IN_CASE(MINT_LDLOC_I1) LDLOC(i, gint8); MINT_IN_BREAK;
3838 MINT_IN_CASE(MINT_LDLOC_U1) LDLOC(i, guint8); MINT_IN_BREAK;
3839 MINT_IN_CASE(MINT_LDLOC_I2) LDLOC(i, gint16); MINT_IN_BREAK;
3840 MINT_IN_CASE(MINT_LDLOC_U2) LDLOC(i, guint16); MINT_IN_BREAK;
3841 MINT_IN_CASE(MINT_LDLOC_I4) LDLOC(i, gint32); MINT_IN_BREAK;
3842 MINT_IN_CASE(MINT_LDLOC_I8) LDLOC(l, gint64); MINT_IN_BREAK;
3843 MINT_IN_CASE(MINT_LDLOC_R4) LDLOC(f, float); MINT_IN_BREAK;
3844 MINT_IN_CASE(MINT_LDLOC_R8) LDLOC(f, double); MINT_IN_BREAK;
3845 MINT_IN_CASE(MINT_LDLOC_O) LDLOC(p, gpointer); MINT_IN_BREAK;
3846 MINT_IN_CASE(MINT_LDLOC_P) LDLOC(p, gpointer); MINT_IN_BREAK;
3848 MINT_IN_CASE(MINT_LDLOC_VT)
3850 i32 = READ32(ip + 2);
3851 memcpy(sp->data.p, locals + * (guint16 *)(ip + 1), i32);
3852 vt_sp += (i32 + 7) & ~7;
3857 MINT_IN_CASE(MINT_LDLOCA_S)
3858 sp->data.p = locals + * (guint16 *)(ip + 1);
3863 #define STLOC(datamem, argtype) \
3865 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3868 MINT_IN_CASE(MINT_STLOC_I1) STLOC(i, gint8); MINT_IN_BREAK;
3869 MINT_IN_CASE(MINT_STLOC_U1) STLOC(i, guint8); MINT_IN_BREAK;
3870 MINT_IN_CASE(MINT_STLOC_I2) STLOC(i, gint16); MINT_IN_BREAK;
3871 MINT_IN_CASE(MINT_STLOC_U2) STLOC(i, guint16); MINT_IN_BREAK;
3872 MINT_IN_CASE(MINT_STLOC_I4) STLOC(i, gint32); MINT_IN_BREAK;
3873 MINT_IN_CASE(MINT_STLOC_I8) STLOC(l, gint64); MINT_IN_BREAK;
3874 MINT_IN_CASE(MINT_STLOC_R4) STLOC(f, float); MINT_IN_BREAK;
3875 MINT_IN_CASE(MINT_STLOC_R8) STLOC(f, double); MINT_IN_BREAK;
3876 MINT_IN_CASE(MINT_STLOC_O) STLOC(p, gpointer); MINT_IN_BREAK;
3877 MINT_IN_CASE(MINT_STLOC_P) STLOC(p, gpointer); MINT_IN_BREAK;
3879 #define STLOC_NP(datamem, argtype) \
3880 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp [-1].data.datamem; \
3883 MINT_IN_CASE(MINT_STLOC_NP_I4) STLOC_NP(i, gint32); MINT_IN_BREAK;
3884 MINT_IN_CASE(MINT_STLOC_NP_O) STLOC_NP(p, gpointer); MINT_IN_BREAK;
3886 MINT_IN_CASE(MINT_STLOC_VT)
3887 i32 = READ32(ip + 2);
3889 memcpy(locals + * (guint16 *)(ip + 1), sp->data.p, i32);
3890 vt_sp -= (i32 + 7) & ~7;
3894 MINT_IN_CASE(MINT_LOCALLOC)
3895 if (sp != frame->stack + 1) /*FIX?*/
3896 THROW_EX (mono_get_exception_execution_engine (NULL), ip);
3897 sp [-1].data.p = alloca (sp [-1].data.i);
3901 MINT_IN_CASE(MINT_ENDFILTER) ves_abort(); MINT_IN_BREAK;
3903 MINT_IN_CASE(MINT_INITOBJ)
3905 memset (sp->data.vt, 0, READ32(ip + 1));
3908 MINT_IN_CASE(MINT_CPBLK)
3910 if (!sp [0].data.p || !sp [1].data.p)
3911 THROW_EX (mono_get_exception_null_reference(), ip - 1);
3913 /* FIXME: value and size may be int64... */
3914 memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
3917 MINT_IN_CASE(MINT_CONSTRAINED_) {
3919 /* FIXME: implement */
3921 token = READ32 (ip);
3926 MINT_IN_CASE(MINT_INITBLK)
3929 THROW_EX (mono_get_exception_null_reference(), ip - 1);
3931 /* FIXME: value and size may be int64... */
3932 memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
3935 MINT_IN_CASE(MINT_NO_)
3936 /* FIXME: implement */
3940 MINT_IN_CASE(MINT_RETHROW)
3942 * need to clarify what this should actually do:
3943 * start the search from the last found handler in
3944 * this method or continue in the caller or what.
3945 * Also, do we need to run finally/fault handlers after a retrow?
3946 * Well, this implementation will follow the usual search
3947 * for an handler, considering the current ip as throw spot.
3948 * We need to NULL frame->ex_handler for the later code to
3949 * actually run the new found handler.
3951 frame->ex_handler = NULL;
3952 THROW_EX (frame->ex, ip - 1);
3955 g_print ("Unimplemented opcode: %04x %s at 0x%x\n", *ip, mono_interp_opname[*ip], ip-rtm->code);
3956 THROW_EX (mono_get_exception_execution_engine ("Unimplemented opcode"), ip);
3960 g_assert_not_reached ();
3962 * Exception handling code.
3963 * The exception object is stored in frame->ex.
3970 MonoInvocation *inv;
3971 MonoExceptionClause *clause;
3977 g_print ("* Handling exception '%s' at IL_%04x\n",
3978 frame->ex == NULL ? "** Unknown **" : mono_object_class (frame->ex)->name,
3979 rtm == NULL ? 0 : frame->ip - rtm->code);
3981 if (die_on_exception)
3984 for (inv = frame; inv; inv = inv->parent) {
3986 if (inv->runtime_method == NULL)
3988 method = inv->runtime_method->method;
3989 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
3991 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
3993 if (inv->ip == NULL)
3995 ip_offset = inv->ip - inv->runtime_method->code;
3996 inv->ex_handler = NULL; /* clear this in case we are trhowing an exception while handling one - this one wins */
3997 for (i = 0; i < inv->runtime_method->num_clauses; ++i) {
3998 clause = &inv->runtime_method->clauses [i];
3999 if (clause->flags <= 1 && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4000 if (!clause->flags) {
4001 MonoObject *isinst_obj = mono_object_isinst_checked ((MonoObject*)frame->ex, clause->data.catch_class, &error);
4002 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4005 * OK, we found an handler, now we need to execute the finally
4006 * and fault blocks before branching to the handler code.
4008 inv->ex_handler = clause;
4011 g_print ("* Found handler at '%s'\n", method->name);
4013 goto handle_finally;
4016 /* FIXME: handle filter clauses */
4023 * If we get here, no handler was found: print a stack trace.
4025 for (inv = frame; inv; inv = inv->parent) {
4026 if (inv->invoke_trap)
4027 goto handle_finally;
4030 ex_obj = (MonoObject*)frame->ex;
4031 mono_unhandled_exception (ex_obj);
4038 MonoExceptionClause *clause;
4039 GSList *old_list = finally_ips;
4040 MonoMethod *method = frame->runtime_method->method;
4041 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
4042 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4046 g_print ("* Handle finally IL_%04x\n", endfinally_ip == NULL ? 0 : endfinally_ip - rtm->code);
4048 if (rtm == NULL || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
4049 || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
4052 ip_offset = frame->ip - rtm->code;
4054 if (endfinally_ip != NULL)
4055 finally_ips = g_slist_prepend(finally_ips, (void *)endfinally_ip);
4056 for (i = 0; i < header->num_clauses; ++i)
4057 if (frame->ex_handler == &rtm->clauses [i])
4061 clause = &rtm->clauses [i];
4062 if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && (endfinally_ip == NULL || !(MONO_OFFSET_IN_CLAUSE (clause, endfinally_ip - rtm->code)))) {
4063 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
4064 ip = rtm->code + clause->handler_offset;
4065 finally_ips = g_slist_prepend (finally_ips, (gpointer) ip);
4068 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
4074 endfinally_ip = NULL;
4076 if (old_list != finally_ips && finally_ips) {
4077 ip = finally_ips->data;
4078 finally_ips = g_slist_remove (finally_ips, ip);
4079 sp = frame->stack; /* spec says stack should be empty at endfinally so it should be at the start too */
4084 * If an exception is set, we need to execute the fault handler, too,
4085 * otherwise, we continue normally.
4095 MonoExceptionClause *clause;
4096 MonoMethod *method = frame->runtime_method->method;
4097 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
4098 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4102 g_print ("* Handle fault\n");
4104 ip_offset = frame->ip - rtm->code;
4105 for (i = 0; i < header->num_clauses; ++i) {
4106 clause = &rtm->clauses [i];
4107 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4108 ip = rtm->code + clause->handler_offset;
4111 g_print ("* Executing handler at IL_%04x\n", clause->handler_offset);
4117 * If the handler for the exception was found in this method, we jump
4118 * to it right away, otherwise we return and let the caller run
4119 * the finally, fault and catch blocks.
4120 * This same code should be present in the endfault opcode, but it
4121 * is corrently not assigned in the ECMA specs: LAMESPEC.
4123 if (frame->ex_handler) {
4126 g_print ("* Executing handler at IL_%04x\n", frame->ex_handler->handler_offset);
4128 ip = rtm->code + frame->ex_handler->handler_offset;
4130 vt_sp = (unsigned char *) sp + rtm->stack_size;
4131 sp->data.p = frame->ex;
4142 ves_exec_method (MonoInvocation *frame)
4144 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4145 ThreadContext context_struct;
4152 mono_unhandled_exception ((MonoObject*)frame->ex);
4155 if (context == NULL) {
4156 context = &context_struct;
4157 context_struct.domain = mono_domain_get ();
4158 context_struct.base_frame = frame;
4159 context_struct.current_frame = NULL;
4160 context_struct.env_frame = frame;
4161 context_struct.current_env = &env;
4162 context_struct.search_for_handler = 0;
4163 context_struct.managed_code = 0;
4164 mono_native_tls_set_value (thread_context_id, context);
4167 frame->parent = context->current_frame;
4168 frame->runtime_method = mono_interp_get_runtime_method (context->domain, frame->method, &error);
4169 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4170 context->managed_code = 1;
4171 ves_exec_method_with_context (frame, context);
4172 context->managed_code = 0;
4174 if (context != &context_struct && context->current_env) {
4175 context->env_frame->ex = frame->ex;
4176 longjmp (*context->current_env, 1);
4179 mono_unhandled_exception ((MonoObject*)frame->ex);
4181 if (context->base_frame == frame)
4182 mono_native_tls_set_value (thread_context_id, NULL);
4184 context->current_frame = frame->parent;
4188 ves_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4190 MonoImage *image = mono_assembly_get_image (assembly);
4195 method = mono_get_method_checked (image, mono_image_get_entry_point (image), NULL, NULL, &error);
4196 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4199 g_error ("No entry point method found in %s", mono_image_get_filename (image));
4201 rval = mono_runtime_run_main_checked (method, argc, argv, &error);
4202 return_val_and_set_pending_if_nok (&error, rval);
4209 "mint %s, the Mono ECMA CLI interpreter, (C) 2001, 2002 Ximian, Inc.\n\n"
4210 "Usage is: mint [options] executable args...\n\n", VERSION);
4212 "Runtime Debugging:\n"
4217 " --noptr\t\t\tdon't print pointer addresses in trace output\n"
4220 " --traceclassinit\n"
4223 " --debug method_name\n"
4230 " --config filename load the specified config file instead of the default\n"
4231 " --workers n maximum number of worker threads\n"
4237 add_signal_handler (int signo, void (*handler)(int))
4240 signal (signo, handler);
4242 struct sigaction sa;
4244 sa.sa_handler = handler;
4245 sigemptyset (&sa.sa_mask);
4248 g_assert (sigaction (signo, &sa, NULL) != -1);
4253 segv_handler (int signum)
4255 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4256 MonoException *segv_exception;
4258 if (context == NULL)
4260 segv_exception = mono_get_exception_null_reference ();
4261 segv_exception->message = mono_string_new (mono_domain_get (), "Null Reference (SIGSEGV)");
4262 mono_raise_exception (segv_exception);
4267 quit_handler (int signum)
4269 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4270 MonoException *quit_exception;
4272 if (context == NULL)
4274 quit_exception = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
4275 mono_raise_exception (quit_exception);
4279 abrt_handler (int signum)
4281 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4282 MonoException *abrt_exception;
4284 if (context == NULL)
4286 abrt_exception = mono_get_exception_execution_engine ("Abort (SIGABRT).");
4287 mono_raise_exception (abrt_exception);
4292 thread_abort_handler (int signum)
4294 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4297 if (context == NULL)
4300 exc = mono_thread_request_interruption (context->managed_code);
4301 if (exc) mono_raise_exception (exc);
4306 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
4307 MonoReflectionMethod **method,
4308 gint32 *iloffset, gint32 *native_offset,
4309 MonoString **file, gint32 *line, gint32 *column)
4311 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4312 MonoInvocation *inv = context->current_frame;
4316 for (i = 0; inv && i < skip; inv = inv->parent)
4317 if (inv->runtime_method != NULL)
4328 *method = mono_method_get_object_checked (context->domain, inv->runtime_method->method, NULL, &error);
4329 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4334 if (need_file_info) {
4338 *file = mono_string_new (mono_domain_get (), "unknown");
4345 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
4347 MonoDomain *domain = mono_domain_get ();
4349 MonoArray *ta = exc->trace_ips;
4354 /* Exception is not thrown yet */
4355 MonoArray *array = mono_array_new_checked (domain, mono_defaults.stack_frame_class, 0, &error);
4356 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4360 len = mono_array_length (ta);
4362 res = mono_array_new_checked (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0, &error);
4363 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4365 for (i = skip; i < len / 2; i++) {
4366 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new_checked (domain, mono_defaults.stack_frame_class, &error);
4367 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4368 gushort *ip = mono_array_get (ta, gpointer, 2 * i + 1);
4369 RuntimeMethod *rtm = mono_array_get (ta, gpointer, 2 * i);
4372 sf->method = mono_method_get_object_checked (domain, rtm->method, NULL, &error);
4373 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4374 sf->native_offset = ip - rtm->code;
4378 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
4380 if (need_file_info) {
4383 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
4385 sf->filename = filename? mono_string_new (domain, filename): NULL;
4392 mono_array_set (res, gpointer, i, sf);
4399 ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionType *type, MonoObject *target, MonoReflectionMethod *info)
4401 MonoClass *delegate_class = mono_class_from_mono_type (type->type);
4402 MonoObject *delegate;
4405 g_assert (delegate_class->parent == mono_defaults.multicastdelegate_class);
4407 delegate = mono_object_new_checked (mono_object_domain (type), delegate_class, &error);
4408 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4410 interp_delegate_ctor (mono_object_domain (type), delegate, target, mono_interp_get_runtime_method (mono_get_root_domain (), info->method, &error));
4411 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4420 int enable_debugging;
4426 static void main_thread_handler (gpointer user_data)
4428 MainThreadArgs *main_args=(MainThreadArgs *)user_data;
4429 MonoAssembly *assembly;
4431 if (main_args->enable_debugging) {
4432 mono_debug_init (MONO_DEBUG_FORMAT_MONO);
4435 assembly = mono_domain_assembly_open (main_args->domain, main_args->file);
4438 fprintf (stderr, "Can not open image %s\n", main_args->file);
4442 ves_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
4446 mono_runtime_install_handlers (void)
4448 add_signal_handler (SIGSEGV, segv_handler);
4449 add_signal_handler (SIGINT, quit_handler);
4450 add_signal_handler (SIGABRT, abrt_handler);
4452 add_signal_handler (mono_thread_get_abort_signal (), thread_abort_handler);
4457 quit_function (MonoDomain *domain, gpointer user_data)
4459 mono_profiler_shutdown ();
4461 mono_runtime_cleanup (domain);
4462 mono_domain_free (domain, TRUE);
4467 mono_interp_cleanup(MonoDomain *domain)
4469 quit_function (domain, NULL);
4473 mono_interp_exec(MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4475 return ves_exec (domain, assembly, argc, argv);
4479 interp_get_imt_trampoline (MonoVTable *vtable, int imt_slot_index)
4481 // FIXME: implement me
4486 interp_create_ftnptr (MonoDomain *domain, gpointer addr)
4488 // FIXME: true on all arch?
4493 mono_interp_init(const char *file)
4496 MonoRuntimeCallbacks callbacks;
4497 MonoRuntimeExceptionHandlingCallbacks ecallbacks;
4500 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
4501 g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
4503 mono_native_tls_alloc (&thread_context_id, NULL);
4504 mono_native_tls_set_value (thread_context_id, NULL);
4505 mono_os_mutex_init_recursive (&runtime_method_lookup_section);
4506 mono_os_mutex_init_recursive (&create_method_pointer_mutex);
4508 // TODO: use callbacks?
4509 mono_runtime_install_handlers ();
4510 mono_interp_transform_init ();
4512 memset (&callbacks, 0, sizeof (callbacks));
4513 // TODO: replace with `mono_install_callbacks`
4514 callbacks.compile_method = mono_create_method_pointer;
4515 callbacks.runtime_invoke = interp_mono_runtime_invoke;
4516 callbacks.get_imt_trampoline = interp_get_imt_trampoline;
4517 callbacks.create_ftnptr = interp_create_ftnptr;
4518 #ifndef DISABLE_REMOTING
4519 mono_install_remoting_trampoline (interp_create_remoting_trampoline);
4521 callbacks.create_jit_trampoline = interp_create_trampoline;
4522 mono_install_callbacks (&callbacks);
4525 memset (&ecallbacks, 0, sizeof (ecallbacks));
4526 ecallbacks.mono_raise_exception = interp_ex_handler;
4529 mono_install_stack_walk (interp_walk_stack);
4531 mono_install_eh_callbacks (&ecallbacks);
4533 mono_install_runtime_cleanup (quit_function);
4535 abort_requested = mono_thread_interruption_request_flag ();
4537 domain = mono_init_from_assembly (file, file);
4538 #ifdef __hpux /* generates very big stack frames */
4539 mono_threads_set_default_stacksize(32*1024*1024);
4542 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info", ves_icall_get_frame_info);
4543 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace", ves_icall_get_trace);
4544 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers", mono_runtime_install_handlers);
4545 mono_add_internal_call ("System.Delegate::CreateDelegate_internal", ves_icall_System_Delegate_CreateDelegate_internal);
4547 mono_register_jit_icall (mono_thread_interruption_checkpoint, "mono_thread_interruption_checkpoint", mono_create_icall_signature ("void"), FALSE);
4549 mono_runtime_init_checked (domain, NULL, NULL, &error);
4550 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4552 mono_thread_attach (domain);
4556 typedef int (*TestMethod) (void);
4559 interp_regression_step (MonoImage *image, int verbose, int *total_run, int *total, GTimer *timer, MonoDomain *domain)
4561 int result, expected, failed, cfailed, run;
4563 double elapsed, transform_time, start_time;
4565 MonoObject *result_obj;
4567 g_print ("Test run: image=%s\n", mono_image_get_filename (image));
4568 cfailed = failed = run = 0;
4569 transform_time = elapsed = 0.0;
4572 /* fixme: ugly hack - delete all previously compiled methods */
4573 if (domain_jit_info (domain)) {
4574 g_hash_table_destroy (domain_jit_info (domain)->jit_trampoline_hash);
4575 domain_jit_info (domain)->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
4576 mono_internal_hash_table_destroy (&(domain->jit_code_hash));
4577 mono_jit_code_hash_init (&(domain->jit_code_hash));
4581 g_timer_start (timer);
4582 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
4584 MonoMethod *method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
4586 mono_error_cleanup (&error); /* FIXME don't swallow the error */
4589 if (strncmp (method->name, "test_", 5) == 0) {
4590 MonoError interp_error;
4591 RuntimeMethod *runtime_method;
4592 MonoInvocation frame;
4593 ThreadContext context;
4594 stackval frame_result;
4596 result_obj = interp_mono_runtime_invoke (method, NULL, NULL, NULL, &interp_error);
4597 if (!mono_error_ok (&interp_error)) {
4599 g_print ("Test '%s' execution failed.\n", method->name);
4601 result = *(gint32 *) mono_object_unbox (result_obj);
4602 expected = atoi (method->name + 5); // FIXME: oh no.
4605 if (result != expected) {
4607 g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
4612 g_timer_stop (timer);
4613 elapsed = g_timer_elapsed (timer, NULL);
4614 if (failed > 0 || cfailed > 0){
4615 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
4616 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
4618 g_print ("Results: total tests: %d, all pass \n", run);
4621 g_print ("Elapsed time: %f secs (%f, %f)\n\n", elapsed,
4622 elapsed - transform_time, transform_time);
4623 *total += failed + cfailed;
4627 interp_regression (MonoImage *image, int verbose, int *total_run)
4631 GTimer *timer = g_timer_new ();
4632 MonoDomain *domain = mono_domain_get ();
4633 guint32 i, exclude = 0;
4636 /* load the metadata */
4637 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
4639 method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
4641 mono_error_cleanup (&error);
4644 mono_class_init (method->klass);
4649 interp_regression_step (image, verbose, total_run, &total, timer, domain);
4651 g_timer_destroy (timer);
4656 interp_regression_list (int verbose, int count, char *images [])
4658 int i, total, total_run, run;
4660 total_run = total = 0;
4661 for (i = 0; i < count; ++i) {
4662 MonoAssembly *ass = mono_assembly_open (images [i], NULL);
4664 g_warning ("failed to load assembly: %s", images [i]);
4667 total += interp_regression (mono_assembly_get_image (ass), verbose, &run);
4671 g_print ("Overall results: tests: %d, failed: %d (pass: %.2f%%)\n", total_run, total, 100.0*(total_run-total)/total_run);
4673 g_print ("Overall results: tests: %d, 100%% pass\n", total_run);