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;
1497 frame->ex = mono_interp_transform_method (frame->runtime_method, context);
1498 context->managed_code = 1;
1506 rtm = frame->runtime_method;
1507 frame->args = alloca (rtm->alloca_size);
1508 sp = frame->stack = (stackval *)((char *)frame->args + rtm->args_size);
1511 memset(sp, 0, rtm->stack_size);
1513 vt_sp = (unsigned char *) sp + rtm->stack_size;
1517 locals = (unsigned char *) vt_sp + rtm->vt_stack_size;
1519 child_frame.parent = frame;
1525 * using while (ip < end) may result in a 15% performance drop,
1526 * but it may be useful for debug
1530 /* g_assert (sp >= frame->stack); */
1531 /* g_assert(vt_sp - vtalloc <= rtm->vt_stack_size); */
1533 MINT_IN_SWITCH (*ip) {
1534 MINT_IN_CASE(MINT_INITLOCALS)
1535 memset (locals, 0, rtm->locals_size);
1538 MINT_IN_CASE(MINT_NOP)
1541 MINT_IN_CASE(MINT_BREAK)
1543 G_BREAKPOINT (); /* this is not portable... */
1545 MINT_IN_CASE(MINT_LDNULL)
1550 MINT_IN_CASE(MINT_VTRESULT) {
1551 int ret_size = * (guint16 *)(ip + 1);
1552 unsigned char *ret_vt_sp = vt_sp;
1553 vt_sp -= READ32(ip + 2);
1555 memmove (vt_sp, ret_vt_sp, ret_size);
1556 vt_sp += (ret_size + 7) & ~7;
1561 #define LDC(n) do { sp->data.i = (n); ++ip; ++sp; } while (0)
1562 MINT_IN_CASE(MINT_LDC_I4_M1)
1565 MINT_IN_CASE(MINT_LDC_I4_0)
1568 MINT_IN_CASE(MINT_LDC_I4_1)
1571 MINT_IN_CASE(MINT_LDC_I4_2)
1574 MINT_IN_CASE(MINT_LDC_I4_3)
1577 MINT_IN_CASE(MINT_LDC_I4_4)
1580 MINT_IN_CASE(MINT_LDC_I4_5)
1583 MINT_IN_CASE(MINT_LDC_I4_6)
1586 MINT_IN_CASE(MINT_LDC_I4_7)
1589 MINT_IN_CASE(MINT_LDC_I4_8)
1592 MINT_IN_CASE(MINT_LDC_I4_S)
1593 sp->data.i = *(const short *)(ip + 1);
1597 MINT_IN_CASE(MINT_LDC_I4)
1599 sp->data.i = READ32 (ip);
1603 MINT_IN_CASE(MINT_LDC_I8)
1605 sp->data.l = READ64 (ip);
1609 MINT_IN_CASE(MINT_LDC_R4) {
1613 sp->data.f = * (float *)&val;
1618 MINT_IN_CASE(MINT_LDC_R8)
1619 sp->data.l = READ64 (ip + 1); /* note union usage */
1623 MINT_IN_CASE(MINT_DUP)
1628 MINT_IN_CASE(MINT_DUP_VT)
1629 i32 = READ32 (ip + 1);
1631 memcpy(sp->data.p, sp [-1].data.p, i32);
1632 vt_sp += (i32 + 7) & ~7;
1636 MINT_IN_CASE(MINT_POP)
1640 MINT_IN_CASE(MINT_JMP) {
1641 RuntimeMethod *new_method = rtm->data_items [* (guint16 *)(ip + 1)];
1642 if (!new_method->transformed) {
1644 frame->ex = mono_interp_transform_method (new_method, context);
1649 if (new_method->alloca_size > rtm->alloca_size)
1650 g_error ("MINT_JMP to method which needs more stack space (%d > %d)", new_method->alloca_size, rtm->alloca_size);
1651 rtm = frame->runtime_method = new_method;
1652 vt_sp = (unsigned char *) sp + rtm->stack_size;
1656 locals = vt_sp + rtm->vt_stack_size;
1657 ip = rtm->new_body_start; /* bypass storing input args from callers frame */
1660 MINT_IN_CASE(MINT_CALLI) {
1661 MonoMethodSignature *csignature;
1662 stackval *endsp = sp;
1666 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1670 child_frame.runtime_method = sp->data.p;
1673 child_frame.retval = sp;
1674 /* decrement by the actual number of args */
1675 sp -= csignature->param_count;
1676 child_frame.stack_args = sp;
1677 if (csignature->hasthis) {
1679 child_frame.obj = sp->data.p;
1681 child_frame.obj = NULL;
1683 if (csignature->hasthis && ((MonoObject *)child_frame.obj)->vtable->klass == mono_defaults.transparent_proxy_class) {
1684 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1685 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1686 } else if (child_frame.runtime_method->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
1687 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_native_wrapper (child_frame.runtime_method->method, FALSE, FALSE), &error);
1688 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1691 ves_exec_method_with_context (&child_frame, context);
1693 context->current_frame = frame;
1695 if (child_frame.ex) {
1697 * An exception occurred, need to run finally, fault and catch handlers..
1699 frame->ex = child_frame.ex;
1700 goto handle_finally;
1703 /* need to handle typedbyref ... */
1704 if (csignature->ret->type != MONO_TYPE_VOID) {
1710 MINT_IN_CASE(MINT_CALLI_NAT) {
1711 MonoMethodSignature *csignature;
1712 stackval *endsp = sp;
1713 unsigned char *code = NULL;
1717 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1722 child_frame.runtime_method = NULL;
1725 child_frame.retval = sp;
1726 /* decrement by the actual number of args */
1727 sp -= csignature->param_count;
1728 child_frame.stack_args = sp;
1729 if (csignature->hasthis) {
1731 child_frame.obj = sp->data.p;
1733 child_frame.obj = NULL;
1735 ves_pinvoke_method (&child_frame, csignature, (MonoFuncV) code, FALSE, context);
1737 context->current_frame = frame;
1739 if (child_frame.ex) {
1741 * An exception occurred, need to run finally, fault and catch handlers..
1743 frame->ex = child_frame.ex;
1744 if (context->search_for_handler) {
1745 context->search_for_handler = 0;
1746 goto handle_exception;
1748 goto handle_finally;
1751 /* need to handle typedbyref ... */
1752 if (csignature->ret->type != MONO_TYPE_VOID) {
1758 MINT_IN_CASE(MINT_CALL) {
1759 stackval *endsp = sp;
1763 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1766 child_frame.retval = sp;
1767 /* decrement by the actual number of args */
1768 sp -= child_frame.runtime_method->param_count;
1769 child_frame.stack_args = sp;
1770 if (child_frame.runtime_method->hasthis) {
1772 child_frame.obj = sp->data.p;
1774 child_frame.obj = NULL;
1776 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->valuetype && ((MonoObject *)child_frame.obj)->vtable->klass == mono_defaults.transparent_proxy_class) {
1777 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1778 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1780 ves_exec_method_with_context (&child_frame, context);
1782 context->current_frame = frame;
1784 if (child_frame.ex) {
1786 * An exception occurred, need to run finally, fault and catch handlers..
1788 frame->ex = child_frame.ex;
1789 goto handle_finally;
1792 /* need to handle typedbyref ... */
1797 MINT_IN_CASE(MINT_VCALL) {
1800 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1804 child_frame.retval = sp;
1805 /* decrement by the actual number of args */
1806 sp -= child_frame.runtime_method->param_count;
1807 child_frame.stack_args = sp;
1808 if (child_frame.runtime_method->hasthis) {
1810 child_frame.obj = sp->data.p;
1812 child_frame.obj = NULL;
1815 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->valuetype && ((MonoObject *)child_frame.obj)->vtable->klass == mono_defaults.transparent_proxy_class) {
1816 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1817 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1820 ves_exec_method_with_context (&child_frame, context);
1822 context->current_frame = frame;
1824 if (child_frame.ex) {
1826 * An exception occurred, need to run finally, fault and catch handlers..
1828 frame->ex = child_frame.ex;
1829 goto handle_finally;
1833 MINT_IN_CASE(MINT_CALLVIRT) {
1834 stackval *endsp = sp;
1835 MonoObject *this_arg;
1840 token = * (unsigned short *)(ip + 1);
1842 child_frame.runtime_method = rtm->data_items [token];
1844 child_frame.retval = sp;
1846 /* decrement by the actual number of args */
1847 sp -= child_frame.runtime_method->param_count;
1848 child_frame.stack_args = sp;
1850 child_frame.obj = this_arg = sp->data.p;
1852 THROW_EX (mono_get_exception_null_reference(), ip - 2);
1853 child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
1855 if (this_arg->vtable->klass->valuetype && child_frame.runtime_method->valuetype) {
1856 child_frame.obj = (char *)this_arg + sizeof(MonoObject);
1859 ves_exec_method_with_context (&child_frame, context);
1861 context->current_frame = frame;
1863 if (child_frame.ex) {
1865 * An exception occurred, need to run finally, fault and catch handlers..
1867 frame->ex = child_frame.ex;
1868 if (context->search_for_handler) {
1869 context->search_for_handler = 0;
1870 goto handle_exception;
1872 goto handle_finally;
1875 /* need to handle typedbyref ... */
1880 MINT_IN_CASE(MINT_VCALLVIRT) {
1881 MonoObject *this_arg;
1886 token = * (unsigned short *)(ip + 1);
1888 child_frame.runtime_method = rtm->data_items [token];
1890 child_frame.retval = sp;
1892 /* decrement by the actual number of args */
1893 sp -= child_frame.runtime_method->param_count;
1894 child_frame.stack_args = sp;
1896 child_frame.obj = this_arg = sp->data.p;
1898 THROW_EX (mono_get_exception_null_reference(), ip - 2);
1899 child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
1901 if (this_arg->vtable->klass->valuetype && child_frame.runtime_method->valuetype) {
1902 child_frame.obj = (char *)this_arg + sizeof(MonoObject);
1905 ves_exec_method_with_context (&child_frame, context);
1907 context->current_frame = frame;
1909 if (child_frame.ex) {
1911 * An exception occurred, need to run finally, fault and catch handlers..
1913 frame->ex = child_frame.ex;
1914 if (context->search_for_handler) {
1915 context->search_for_handler = 0;
1916 goto handle_exception;
1918 goto handle_finally;
1922 MINT_IN_CASE(MINT_CALLINT)
1923 ves_pinvoke_method (frame, mono_method_signature (frame->runtime_method->method), ((MonoMethodPInvoke*) frame->runtime_method->method)->addr,
1924 frame->runtime_method->method->string_ctor, context);
1927 goto handle_exception;
1930 MINT_IN_CASE(MINT_CALLRUN)
1931 ves_runtime_method (frame, context);
1934 goto handle_exception;
1937 MINT_IN_CASE(MINT_RET)
1939 *frame->retval = *sp;
1940 if (sp > frame->stack)
1941 g_warning ("ret: more values on stack: %d", sp-frame->stack);
1943 MINT_IN_CASE(MINT_RET_VOID)
1944 if (sp > frame->stack)
1945 g_warning ("ret.void: more values on stack: %d", sp-frame->stack);
1947 MINT_IN_CASE(MINT_RET_VT)
1948 i32 = READ32(ip + 1);
1950 memcpy(frame->retval->data.p, sp->data.p, i32);
1951 if (sp > frame->stack)
1952 g_warning ("ret.vt: more values on stack: %d", sp-frame->stack);
1954 MINT_IN_CASE(MINT_BR_S)
1955 ip += (short) *(ip + 1);
1957 MINT_IN_CASE(MINT_BR)
1958 ip += (gint32) READ32(ip + 1);
1960 #define ZEROP_S(datamem, op) \
1962 if (sp->data.datamem op 0) \
1963 ip += * (gint16 *)(ip + 1); \
1967 #define ZEROP(datamem, op) \
1969 if (sp->data.datamem op 0) \
1970 ip += READ32(ip + 1); \
1974 MINT_IN_CASE(MINT_BRFALSE_I4_S)
1977 MINT_IN_CASE(MINT_BRFALSE_I8_S)
1980 MINT_IN_CASE(MINT_BRFALSE_R8_S)
1983 MINT_IN_CASE(MINT_BRFALSE_I4)
1986 MINT_IN_CASE(MINT_BRFALSE_I8)
1989 MINT_IN_CASE(MINT_BRFALSE_R8)
1992 MINT_IN_CASE(MINT_BRTRUE_I4_S)
1995 MINT_IN_CASE(MINT_BRTRUE_I8_S)
1998 MINT_IN_CASE(MINT_BRTRUE_R8_S)
2001 MINT_IN_CASE(MINT_BRTRUE_I4)
2004 MINT_IN_CASE(MINT_BRTRUE_I8)
2007 MINT_IN_CASE(MINT_BRTRUE_R8)
2010 #define CONDBR_S(cond) \
2013 ip += * (gint16 *)(ip + 1); \
2016 #define BRELOP_S(datamem, op) \
2017 CONDBR_S(sp[0].data.datamem op sp[1].data.datamem)
2019 #define CONDBR(cond) \
2022 ip += READ32(ip + 1); \
2026 #define BRELOP(datamem, op) \
2027 CONDBR(sp[0].data.datamem op sp[1].data.datamem)
2029 MINT_IN_CASE(MINT_BEQ_I4_S)
2032 MINT_IN_CASE(MINT_BEQ_I8_S)
2035 MINT_IN_CASE(MINT_BEQ_R8_S)
2036 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2038 MINT_IN_CASE(MINT_BEQ_I4)
2041 MINT_IN_CASE(MINT_BEQ_I8)
2044 MINT_IN_CASE(MINT_BEQ_R8)
2045 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2047 MINT_IN_CASE(MINT_BGE_I4_S)
2050 MINT_IN_CASE(MINT_BGE_I8_S)
2053 MINT_IN_CASE(MINT_BGE_R8_S)
2054 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
2056 MINT_IN_CASE(MINT_BGE_I4)
2059 MINT_IN_CASE(MINT_BGE_I8)
2062 MINT_IN_CASE(MINT_BGE_R8)
2063 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
2065 MINT_IN_CASE(MINT_BGT_I4_S)
2068 MINT_IN_CASE(MINT_BGT_I8_S)
2071 MINT_IN_CASE(MINT_BGT_R8_S)
2072 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2074 MINT_IN_CASE(MINT_BGT_I4)
2077 MINT_IN_CASE(MINT_BGT_I8)
2080 MINT_IN_CASE(MINT_BGT_R8)
2081 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2083 MINT_IN_CASE(MINT_BLT_I4_S)
2086 MINT_IN_CASE(MINT_BLT_I8_S)
2089 MINT_IN_CASE(MINT_BLT_R8_S)
2090 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2092 MINT_IN_CASE(MINT_BLT_I4)
2095 MINT_IN_CASE(MINT_BLT_I8)
2098 MINT_IN_CASE(MINT_BLT_R8)
2099 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2101 MINT_IN_CASE(MINT_BLE_I4_S)
2104 MINT_IN_CASE(MINT_BLE_I8_S)
2107 MINT_IN_CASE(MINT_BLE_R8_S)
2108 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2110 MINT_IN_CASE(MINT_BLE_I4)
2113 MINT_IN_CASE(MINT_BLE_I8)
2116 MINT_IN_CASE(MINT_BLE_R8)
2117 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2119 MINT_IN_CASE(MINT_BNE_UN_I4_S)
2122 MINT_IN_CASE(MINT_BNE_UN_I8_S)
2125 MINT_IN_CASE(MINT_BNE_UN_R8_S)
2126 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
2128 MINT_IN_CASE(MINT_BNE_UN_I4)
2131 MINT_IN_CASE(MINT_BNE_UN_I8)
2134 MINT_IN_CASE(MINT_BNE_UN_R8)
2135 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
2138 #define BRELOP_S_CAST(datamem, op, type) \
2140 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
2141 ip += * (gint16 *)(ip + 1); \
2145 #define BRELOP_CAST(datamem, op, type) \
2147 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
2148 ip += READ32(ip + 1); \
2152 MINT_IN_CASE(MINT_BGE_UN_I4_S)
2153 BRELOP_S_CAST(i, >=, guint32);
2155 MINT_IN_CASE(MINT_BGE_UN_I8_S)
2156 BRELOP_S_CAST(l, >=, guint64);
2158 MINT_IN_CASE(MINT_BGE_UN_R8_S)
2159 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
2161 MINT_IN_CASE(MINT_BGE_UN_I4)
2162 BRELOP_CAST(i, >=, guint32);
2164 MINT_IN_CASE(MINT_BGE_UN_I8)
2165 BRELOP_CAST(l, >=, guint64);
2167 MINT_IN_CASE(MINT_BGE_UN_R8)
2168 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
2170 MINT_IN_CASE(MINT_BGT_UN_I4_S)
2171 BRELOP_S_CAST(i, >, guint32);
2173 MINT_IN_CASE(MINT_BGT_UN_I8_S)
2174 BRELOP_S_CAST(l, >, guint64);
2176 MINT_IN_CASE(MINT_BGT_UN_R8_S)
2177 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2179 MINT_IN_CASE(MINT_BGT_UN_I4)
2180 BRELOP_CAST(i, >, guint32);
2182 MINT_IN_CASE(MINT_BGT_UN_I8)
2183 BRELOP_CAST(l, >, guint64);
2185 MINT_IN_CASE(MINT_BGT_UN_R8)
2186 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2188 MINT_IN_CASE(MINT_BLE_UN_I4_S)
2189 BRELOP_S_CAST(i, <=, guint32);
2191 MINT_IN_CASE(MINT_BLE_UN_I8_S)
2192 BRELOP_S_CAST(l, <=, guint64);
2194 MINT_IN_CASE(MINT_BLE_UN_R8_S)
2195 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2197 MINT_IN_CASE(MINT_BLE_UN_I4)
2198 BRELOP_CAST(i, <=, guint32);
2200 MINT_IN_CASE(MINT_BLE_UN_I8)
2201 BRELOP_CAST(l, <=, guint64);
2203 MINT_IN_CASE(MINT_BLE_UN_R8)
2204 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2206 MINT_IN_CASE(MINT_BLT_UN_I4_S)
2207 BRELOP_S_CAST(i, <, guint32);
2209 MINT_IN_CASE(MINT_BLT_UN_I8_S)
2210 BRELOP_S_CAST(l, <, guint64);
2212 MINT_IN_CASE(MINT_BLT_UN_R8_S)
2213 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2215 MINT_IN_CASE(MINT_BLT_UN_I4)
2216 BRELOP_CAST(i, <, guint32);
2218 MINT_IN_CASE(MINT_BLT_UN_I8)
2219 BRELOP_CAST(l, <, guint64);
2221 MINT_IN_CASE(MINT_BLT_UN_R8)
2222 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2224 MINT_IN_CASE(MINT_SWITCH) {
2226 const unsigned short *st;
2232 if ((guint32)sp->data.i < n) {
2234 ip += 2 * (guint32)sp->data.i;
2235 offset = READ32 (ip);
2242 MINT_IN_CASE(MINT_LDIND_I1)
2244 sp[-1].data.i = *(gint8*)sp[-1].data.p;
2246 MINT_IN_CASE(MINT_LDIND_U1)
2248 sp[-1].data.i = *(guint8*)sp[-1].data.p;
2250 MINT_IN_CASE(MINT_LDIND_I2)
2252 sp[-1].data.i = *(gint16*)sp[-1].data.p;
2254 MINT_IN_CASE(MINT_LDIND_U2)
2256 sp[-1].data.i = *(guint16*)sp[-1].data.p;
2258 MINT_IN_CASE(MINT_LDIND_I4) /* Fall through */
2259 MINT_IN_CASE(MINT_LDIND_U4)
2261 sp[-1].data.i = *(gint32*)sp[-1].data.p;
2263 MINT_IN_CASE(MINT_LDIND_I8)
2265 sp[-1].data.l = *(gint64*)sp[-1].data.p;
2267 MINT_IN_CASE(MINT_LDIND_I)
2269 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2271 MINT_IN_CASE(MINT_LDIND_R4)
2273 sp[-1].data.f = *(gfloat*)sp[-1].data.p;
2275 MINT_IN_CASE(MINT_LDIND_R8)
2277 sp[-1].data.f = *(gdouble*)sp[-1].data.p;
2279 MINT_IN_CASE(MINT_LDIND_REF)
2281 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2283 MINT_IN_CASE(MINT_STIND_REF)
2286 * (gpointer *) sp->data.p = sp[1].data.p;
2288 MINT_IN_CASE(MINT_STIND_I1)
2291 * (gint8 *) sp->data.p = (gint8)sp[1].data.i;
2293 MINT_IN_CASE(MINT_STIND_I2)
2296 * (gint16 *) sp->data.p = (gint16)sp[1].data.i;
2298 MINT_IN_CASE(MINT_STIND_I4)
2301 * (gint32 *) sp->data.p = sp[1].data.i;
2303 MINT_IN_CASE(MINT_STIND_I)
2306 * (mono_i *) sp->data.p = (mono_i)sp[1].data.p;
2308 MINT_IN_CASE(MINT_STIND_I8)
2311 * (gint64 *) sp->data.p = sp[1].data.l;
2313 MINT_IN_CASE(MINT_STIND_R4)
2316 * (float *) sp->data.p = (gfloat)sp[1].data.f;
2318 MINT_IN_CASE(MINT_STIND_R8)
2321 * (double *) sp->data.p = sp[1].data.f;
2323 #define BINOP(datamem, op) \
2325 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.datamem; \
2327 MINT_IN_CASE(MINT_ADD_I4)
2330 MINT_IN_CASE(MINT_ADD_I8)
2333 MINT_IN_CASE(MINT_ADD_R8)
2336 MINT_IN_CASE(MINT_ADD1_I4)
2340 MINT_IN_CASE(MINT_SUB_I4)
2343 MINT_IN_CASE(MINT_SUB_I8)
2346 MINT_IN_CASE(MINT_SUB_R8)
2349 MINT_IN_CASE(MINT_SUB1_I4)
2353 MINT_IN_CASE(MINT_MUL_I4)
2356 MINT_IN_CASE(MINT_MUL_I8)
2359 MINT_IN_CASE(MINT_MUL_R8)
2362 MINT_IN_CASE(MINT_DIV_I4)
2363 if (sp [-1].data.i == 0)
2364 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2365 if (sp [-1].data.i == (-1))
2366 THROW_EX (mono_get_exception_overflow (), ip);
2369 MINT_IN_CASE(MINT_DIV_I8)
2370 if (sp [-1].data.l == 0)
2371 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2372 if (sp [-1].data.l == (-1))
2373 THROW_EX (mono_get_exception_overflow (), ip);
2376 MINT_IN_CASE(MINT_DIV_R8)
2380 #define BINOP_CAST(datamem, op, type) \
2382 sp [-1].data.datamem = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
2384 MINT_IN_CASE(MINT_DIV_UN_I4)
2385 if (sp [-1].data.i == 0)
2386 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2387 BINOP_CAST(i, /, guint32);
2389 MINT_IN_CASE(MINT_DIV_UN_I8)
2390 if (sp [-1].data.l == 0)
2391 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2392 BINOP_CAST(l, /, guint64);
2394 MINT_IN_CASE(MINT_REM_I4)
2395 if (sp [-1].data.i == 0)
2396 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2399 MINT_IN_CASE(MINT_REM_I8)
2400 if (sp [-1].data.l == 0)
2401 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2404 MINT_IN_CASE(MINT_REM_R8)
2405 /* FIXME: what do we actually do here? */
2407 sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
2410 MINT_IN_CASE(MINT_REM_UN_I4)
2411 if (sp [-1].data.i == 0)
2412 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2413 BINOP_CAST(i, %, guint32);
2415 MINT_IN_CASE(MINT_REM_UN_I8)
2416 if (sp [-1].data.l == 0)
2417 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2418 BINOP_CAST(l, %, guint64);
2420 MINT_IN_CASE(MINT_AND_I4)
2423 MINT_IN_CASE(MINT_AND_I8)
2426 MINT_IN_CASE(MINT_OR_I4)
2429 MINT_IN_CASE(MINT_OR_I8)
2432 MINT_IN_CASE(MINT_XOR_I4)
2435 MINT_IN_CASE(MINT_XOR_I8)
2439 #define SHIFTOP(datamem, op) \
2441 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.i; \
2444 MINT_IN_CASE(MINT_SHL_I4)
2447 MINT_IN_CASE(MINT_SHL_I8)
2450 MINT_IN_CASE(MINT_SHR_I4)
2453 MINT_IN_CASE(MINT_SHR_I8)
2456 MINT_IN_CASE(MINT_SHR_UN_I4)
2458 sp [-1].data.i = (guint32)sp [-1].data.i >> sp [0].data.i;
2461 MINT_IN_CASE(MINT_SHR_UN_I8)
2463 sp [-1].data.l = (guint64)sp [-1].data.l >> sp [0].data.i;
2466 MINT_IN_CASE(MINT_NEG_I4)
2467 sp [-1].data.i = - sp [-1].data.i;
2470 MINT_IN_CASE(MINT_NEG_I8)
2471 sp [-1].data.l = - sp [-1].data.l;
2474 MINT_IN_CASE(MINT_NEG_R8)
2475 sp [-1].data.f = - sp [-1].data.f;
2478 MINT_IN_CASE(MINT_NOT_I4)
2479 sp [-1].data.i = ~ sp [-1].data.i;
2482 MINT_IN_CASE(MINT_NOT_I8)
2483 sp [-1].data.l = ~ sp [-1].data.l;
2486 MINT_IN_CASE(MINT_CONV_I1_I4)
2487 sp [-1].data.i = (gint8)sp [-1].data.i;
2490 MINT_IN_CASE(MINT_CONV_I1_I8)
2491 sp [-1].data.i = (gint8)sp [-1].data.l;
2494 MINT_IN_CASE(MINT_CONV_I1_R8)
2495 sp [-1].data.i = (gint8)sp [-1].data.f;
2498 MINT_IN_CASE(MINT_CONV_U1_I4)
2499 sp [-1].data.i = (guint8)sp [-1].data.i;
2502 MINT_IN_CASE(MINT_CONV_U1_I8)
2503 sp [-1].data.i = (guint8)sp [-1].data.l;
2506 MINT_IN_CASE(MINT_CONV_U1_R8)
2507 sp [-1].data.i = (guint8)sp [-1].data.f;
2510 MINT_IN_CASE(MINT_CONV_I2_I4)
2511 sp [-1].data.i = (gint16)sp [-1].data.i;
2514 MINT_IN_CASE(MINT_CONV_I2_I8)
2515 sp [-1].data.i = (gint16)sp [-1].data.l;
2518 MINT_IN_CASE(MINT_CONV_I2_R8)
2519 sp [-1].data.i = (gint16)sp [-1].data.f;
2522 MINT_IN_CASE(MINT_CONV_U2_I4)
2523 sp [-1].data.i = (guint16)sp [-1].data.i;
2526 MINT_IN_CASE(MINT_CONV_U2_I8)
2527 sp [-1].data.i = (guint16)sp [-1].data.l;
2530 MINT_IN_CASE(MINT_CONV_U2_R8)
2531 sp [-1].data.i = (guint16)sp [-1].data.f;
2534 MINT_IN_CASE(MINT_CONV_I4_R8)
2535 sp [-1].data.i = (gint32)sp [-1].data.f;
2538 MINT_IN_CASE(MINT_CONV_U4_I8)
2539 MINT_IN_CASE(MINT_CONV_I4_I8)
2540 sp [-1].data.i = (gint32)sp [-1].data.l;
2543 MINT_IN_CASE(MINT_CONV_I4_I8_SP)
2544 sp [-2].data.i = (gint32)sp [-2].data.l;
2547 MINT_IN_CASE(MINT_CONV_U4_R8)
2548 sp [-1].data.i = (guint32)sp [-1].data.f;
2551 MINT_IN_CASE(MINT_CONV_I8_I4)
2552 sp [-1].data.l = sp [-1].data.i;
2555 MINT_IN_CASE(MINT_CONV_I8_I4_SP)
2556 sp [-2].data.l = sp [-2].data.i;
2559 MINT_IN_CASE(MINT_CONV_I8_U4)
2560 sp [-1].data.l = (guint32)sp [-1].data.i;
2563 MINT_IN_CASE(MINT_CONV_I8_R8)
2564 sp [-1].data.l = (gint64)sp [-1].data.f;
2567 MINT_IN_CASE(MINT_CONV_R4_I4)
2568 sp [-1].data.f = (float)sp [-1].data.i;
2571 MINT_IN_CASE(MINT_CONV_R4_I8)
2572 sp [-1].data.f = (float)sp [-1].data.l;
2575 MINT_IN_CASE(MINT_CONV_R4_R8)
2576 sp [-1].data.f = (float)sp [-1].data.f;
2579 MINT_IN_CASE(MINT_CONV_R8_I4)
2580 sp [-1].data.f = (double)sp [-1].data.i;
2583 MINT_IN_CASE(MINT_CONV_R8_I8)
2584 sp [-1].data.f = (double)sp [-1].data.l;
2587 MINT_IN_CASE(MINT_CONV_U8_I4)
2588 sp [-1].data.l = sp [-1].data.i & 0xffffffff;
2591 MINT_IN_CASE(MINT_CONV_U8_R8)
2592 sp [-1].data.l = (guint64)sp [-1].data.f;
2596 MINT_IN_CASE(MINT_CPOBJ) {
2599 vtklass = rtm->data_items[READ32 (ip)];
2602 memcpy (sp [0].data.p, sp [1].data.p, mono_class_value_size (vtklass, NULL));
2606 MINT_IN_CASE(MINT_LDOBJ) {
2609 c = rtm->data_items[* (guint16 *)(ip + 1)];
2611 if (c->byval_arg.type != MONO_TYPE_VALUETYPE || c->byval_arg.data.klass->enumtype) {
2613 stackval_from_data (&c->byval_arg, &sp [-1], p, FALSE);
2615 size = mono_class_value_size (c, NULL);
2617 sp [-1].data.p = vt_sp;
2618 memcpy(vt_sp, p, size);
2619 vt_sp += (size + 7) & ~7;
2623 MINT_IN_CASE(MINT_LDSTR)
2624 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
2628 MINT_IN_CASE(MINT_NEWOBJ) {
2629 MonoClass *newobj_class;
2630 MonoMethodSignature *csig;
2631 stackval valuetype_this;
2637 token = * (guint16 *)(ip + 1);
2640 child_frame.runtime_method = rtm->data_items [token];
2641 csig = mono_method_signature (child_frame.runtime_method->method);
2642 newobj_class = child_frame.runtime_method->method->klass;
2643 /*if (profiling_classes) {
2644 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
2646 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
2650 if (newobj_class->parent == mono_defaults.array_class) {
2651 sp -= csig->param_count;
2652 o = ves_array_create (context->domain, newobj_class, csig, sp);
2653 goto array_constructed;
2657 * First arg is the object.
2659 if (newobj_class->valuetype) {
2660 if (!newobj_class->enumtype && (newobj_class->byval_arg.type == MONO_TYPE_VALUETYPE)) {
2661 child_frame.obj = vt_sp;
2662 valuetype_this.data.p = vt_sp;
2664 memset (&valuetype_this, 0, sizeof (stackval));
2665 child_frame.obj = &valuetype_this;
2668 if (newobj_class != mono_defaults.string_class) {
2669 context->managed_code = 0;
2670 o = mono_object_new_checked (context->domain, newobj_class, &error);
2671 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2672 context->managed_code = 1;
2673 if (*abort_requested)
2674 mono_thread_interruption_checkpoint ();
2675 child_frame.obj = o;
2677 child_frame.retval = &retval;
2681 if (csig->param_count) {
2682 sp -= csig->param_count;
2683 child_frame.stack_args = sp;
2685 child_frame.stack_args = NULL;
2688 g_assert (csig->call_convention == MONO_CALL_DEFAULT);
2690 child_frame.ip = NULL;
2691 child_frame.ex = NULL;
2693 ves_exec_method_with_context (&child_frame, context);
2695 context->current_frame = frame;
2697 if (child_frame.ex) {
2699 * An exception occurred, need to run finally, fault and catch handlers..
2701 frame->ex = child_frame.ex;
2702 goto handle_finally;
2705 * a constructor returns void, but we need to return the object we created
2708 if (newobj_class->valuetype && !newobj_class->enumtype) {
2709 *sp = valuetype_this;
2710 } else if (newobj_class == mono_defaults.string_class) {
2718 MINT_IN_CASE(MINT_CASTCLASS)
2719 c = rtm->data_items [*(guint16 *)(ip + 1)];
2720 if ((o = sp [-1].data.p)) {
2721 if (c->marshalbyref) {
2722 MonoObject *isinst_obj = mono_object_isinst_mbyref_checked (o, c, &error);
2723 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2725 THROW_EX (mono_get_exception_invalid_cast (), ip);
2727 MonoVTable *vt = o->vtable;
2728 MonoClass *oklass = vt->klass;
2729 if (c->flags & TYPE_ATTRIBUTE_INTERFACE) {
2730 g_error ("FIXME: interface method lookup");
2731 if (c->interface_id > vt->max_interface_id /* || vt->interface_offsets [c->interface_id] == 0 */) {
2732 THROW_EX (mono_get_exception_invalid_cast (), ip);
2734 } else if (c->rank) {
2735 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2736 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2738 THROW_EX (mono_get_exception_invalid_cast (), ip);
2739 } else if (!mono_class_has_parent (oklass, c)) {
2740 THROW_EX (mono_get_exception_invalid_cast (), ip);
2746 MINT_IN_CASE(MINT_ISINST)
2747 c = rtm->data_items [*(guint16 *)(ip + 1)];
2748 if ((o = sp [-1].data.p)) {
2749 if (c->marshalbyref) {
2750 MonoObject *isinst_obj = mono_object_isinst_mbyref_checked (o, c, &error);
2751 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2753 sp [-1].data.p = NULL;
2755 MonoVTable *vt = o->vtable;
2756 MonoClass *oklass = vt->klass;
2757 if (c->flags & TYPE_ATTRIBUTE_INTERFACE) {
2758 g_error ("FIXME: interface method lookup");
2759 if (c->interface_id > vt->max_interface_id /* || vt->interface_offsets [c->interface_id] == 0 */) {
2760 sp [-1].data.p = NULL;
2762 } else if (c->rank) {
2763 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2764 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2766 sp [-1].data.p = NULL;
2767 } else if (!mono_class_has_parent (oklass, c)) {
2768 sp [-1].data.p = NULL;
2774 MINT_IN_CASE(MINT_CONV_R_UN_I4)
2775 sp [-1].data.f = (double)(guint32)sp [-1].data.i;
2778 MINT_IN_CASE(MINT_CONV_R_UN_I8)
2779 sp [-1].data.f = (double)(guint64)sp [-1].data.l;
2782 MINT_IN_CASE(MINT_UNBOX)
2783 c = rtm->data_items[*(guint16 *)(ip + 1)];
2787 THROW_EX (mono_get_exception_null_reference(), ip);
2789 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2790 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2792 ((o->vtable->klass->rank == 0) &&
2793 (o->vtable->klass->element_class == c->element_class))))
2794 THROW_EX (mono_get_exception_invalid_cast (), ip);
2796 sp [-1].data.p = (char *)o + sizeof (MonoObject);
2799 MINT_IN_CASE(MINT_THROW)
2801 frame->ex_handler = NULL;
2803 sp->data.p = mono_get_exception_null_reference ();
2804 THROW_EX ((MonoException *)sp->data.p, ip);
2806 MINT_IN_CASE(MINT_LDFLDA)
2809 THROW_EX (mono_get_exception_null_reference (), ip);
2810 sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
2813 MINT_IN_CASE(MINT_CKNULL)
2816 THROW_EX (mono_get_exception_null_reference (), ip);
2820 #define LDFLD(datamem, fieldtype) \
2821 o = sp [-1].data.p; \
2823 THROW_EX (mono_get_exception_null_reference (), ip); \
2824 sp[-1].data.datamem = * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) ; \
2827 MINT_IN_CASE(MINT_LDFLD_I1) LDFLD(i, gint8); MINT_IN_BREAK;
2828 MINT_IN_CASE(MINT_LDFLD_U1) LDFLD(i, guint8); MINT_IN_BREAK;
2829 MINT_IN_CASE(MINT_LDFLD_I2) LDFLD(i, gint16); MINT_IN_BREAK;
2830 MINT_IN_CASE(MINT_LDFLD_U2) LDFLD(i, guint16); MINT_IN_BREAK;
2831 MINT_IN_CASE(MINT_LDFLD_I4) LDFLD(i, gint32); MINT_IN_BREAK;
2832 MINT_IN_CASE(MINT_LDFLD_I8) LDFLD(l, gint64); MINT_IN_BREAK;
2833 MINT_IN_CASE(MINT_LDFLD_R4) LDFLD(f, float); MINT_IN_BREAK;
2834 MINT_IN_CASE(MINT_LDFLD_R8) LDFLD(f, double); MINT_IN_BREAK;
2835 MINT_IN_CASE(MINT_LDFLD_O) LDFLD(p, gpointer); MINT_IN_BREAK;
2836 MINT_IN_CASE(MINT_LDFLD_P) LDFLD(p, gpointer); MINT_IN_BREAK;
2838 MINT_IN_CASE(MINT_LDFLD_VT)
2841 THROW_EX (mono_get_exception_null_reference (), ip);
2842 i32 = READ32(ip + 2);
2843 sp [-1].data.p = vt_sp;
2844 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
2845 vt_sp += (i32 + 7) & ~7;
2849 MINT_IN_CASE(MINT_LDRMFLD) {
2851 MonoClassField *field;
2856 THROW_EX (mono_get_exception_null_reference (), ip);
2857 field = rtm->data_items[* (guint16 *)(ip + 1)];
2859 if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2860 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2862 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
2863 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2865 addr = (char*)o + field->offset;
2868 stackval_from_data (field->type, &sp [-1], addr, FALSE);
2872 MINT_IN_CASE(MINT_LDRMFLD_VT) {
2873 MonoClassField *field;
2879 THROW_EX (mono_get_exception_null_reference (), ip);
2880 field = rtm->data_items[* (guint16 *)(ip + 1)];
2881 i32 = READ32(ip + 2);
2883 if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2884 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2885 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
2886 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2888 addr = (char*)o + field->offset;
2891 sp [-1].data.p = vt_sp;
2892 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
2893 vt_sp += (i32 + 7) & ~7;
2894 memcpy(sp [-1].data.p, addr, i32);
2898 #define STFLD(datamem, fieldtype) \
2899 o = sp [-2].data.p; \
2901 THROW_EX (mono_get_exception_null_reference (), ip); \
2903 * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) = sp[1].data.datamem; \
2906 MINT_IN_CASE(MINT_STFLD_I1) STFLD(i, gint8); MINT_IN_BREAK;
2907 MINT_IN_CASE(MINT_STFLD_U1) STFLD(i, guint8); MINT_IN_BREAK;
2908 MINT_IN_CASE(MINT_STFLD_I2) STFLD(i, gint16); MINT_IN_BREAK;
2909 MINT_IN_CASE(MINT_STFLD_U2) STFLD(i, guint16); MINT_IN_BREAK;
2910 MINT_IN_CASE(MINT_STFLD_I4) STFLD(i, gint32); MINT_IN_BREAK;
2911 MINT_IN_CASE(MINT_STFLD_I8) STFLD(l, gint64); MINT_IN_BREAK;
2912 MINT_IN_CASE(MINT_STFLD_R4) STFLD(f, float); MINT_IN_BREAK;
2913 MINT_IN_CASE(MINT_STFLD_R8) STFLD(f, double); MINT_IN_BREAK;
2914 MINT_IN_CASE(MINT_STFLD_O) STFLD(p, gpointer); MINT_IN_BREAK;
2915 MINT_IN_CASE(MINT_STFLD_P) STFLD(p, gpointer); MINT_IN_BREAK;
2917 MINT_IN_CASE(MINT_STFLD_VT)
2920 THROW_EX (mono_get_exception_null_reference (), ip);
2921 i32 = READ32(ip + 2);
2923 memcpy((char *)o + * (guint16 *)(ip + 1), sp [1].data.p, i32);
2924 vt_sp -= (i32 + 7) & ~7;
2928 MINT_IN_CASE(MINT_STRMFLD) {
2929 MonoClassField *field;
2933 THROW_EX (mono_get_exception_null_reference (), ip);
2935 field = rtm->data_items[* (guint16 *)(ip + 1)];
2938 if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2939 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2940 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
2941 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2943 stackval_to_data (field->type, &sp [-1], (char*)o + field->offset, FALSE);
2948 MINT_IN_CASE(MINT_STRMFLD_VT) {
2949 MonoClassField *field;
2953 THROW_EX (mono_get_exception_null_reference (), ip);
2954 field = rtm->data_items[* (guint16 *)(ip + 1)];
2955 i32 = READ32(ip + 2);
2958 if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2959 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2960 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
2961 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2963 memcpy((char*)o + field->offset, sp [-1].data.p, i32);
2966 vt_sp -= (i32 + 7) & ~7;
2969 MINT_IN_CASE(MINT_LDSFLDA) {
2970 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
2971 MonoVTable *vt = mono_class_vtable (context->domain, field->parent);
2974 if (!vt->initialized) {
2976 mono_runtime_class_init_full (vt, &error);
2977 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2981 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
2982 sp->data.p = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
2984 sp->data.p = (char*)(vt->vtable) + field->offset;
2988 MINT_IN_CASE(MINT_LDSFLD) {
2990 MonoClassField *field;
2993 field = rtm->data_items[*(guint16 *)(ip + 1)];
2994 vt = rtm->data_items [*(guint16 *)(ip + 2)];
2995 if (!vt->initialized) {
2997 mono_runtime_class_init_full (vt, &error);
2998 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3001 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3002 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3004 addr = (char*)(vt->vtable) + field->offset;
3006 stackval_from_data (field->type, sp, addr, FALSE);
3010 MINT_IN_CASE(MINT_LDSFLD_I4) {
3011 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3012 MonoVTable *vt = rtm->data_items [*(guint16 *)(ip + 2)];
3013 if (!vt->initialized) {
3015 mono_runtime_class_init_full (vt, &error);
3016 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3019 sp->data.i = * (gint32 *)((char*)(vt->vtable) + field->offset);
3023 MINT_IN_CASE(MINT_LDSFLD_O) {
3024 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3025 MonoVTable *vt = rtm->data_items [*(guint16 *)(ip + 2)];
3026 if (!vt->initialized) {
3028 mono_runtime_class_init_full (vt, &error);
3029 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3032 sp->data.p = * (gpointer *)((char*)(vt->vtable) + field->offset);
3036 MINT_IN_CASE(MINT_LDSFLD_VT) {
3038 MonoClassField *field;
3043 token = * (guint16 *)(ip + 1);
3044 size = READ32(ip + 2);
3045 field = rtm->data_items[token];
3048 vt = mono_class_vtable (context->domain, field->parent);
3049 if (!vt->initialized) {
3051 mono_runtime_class_init_full (vt, &error);
3052 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3055 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3056 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3058 addr = (char*)(vt->vtable) + field->offset;
3061 vt_sp += (size + 7) & ~7;
3062 stackval_from_data (field->type, sp, addr, FALSE);
3066 MINT_IN_CASE(MINT_STSFLD) {
3068 MonoClassField *field;
3072 token = * (guint16 *)(ip + 1);
3073 field = rtm->data_items[token];
3077 vt = mono_class_vtable (context->domain, field->parent);
3078 if (!vt->initialized) {
3080 mono_runtime_class_init_full (vt, &error);
3081 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3084 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3085 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3087 addr = (char*)(vt->vtable) + field->offset;
3089 stackval_to_data (field->type, sp, addr, FALSE);
3092 MINT_IN_CASE(MINT_STSFLD_VT) {
3094 MonoClassField *field;
3099 token = * (guint16 *)(ip + 1);
3100 size = READ32(ip + 2);
3101 field = rtm->data_items[token];
3104 vt = mono_class_vtable (context->domain, field->parent);
3105 if (!vt->initialized) {
3107 mono_runtime_class_init_full (vt, &error);
3108 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3111 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3112 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3114 addr = (char*)(vt->vtable) + field->offset;
3116 stackval_to_data (field->type, sp, addr, FALSE);
3117 vt_sp -= (size + 7) & ~7;
3120 MINT_IN_CASE(MINT_STOBJ_VT) {
3122 c = rtm->data_items[* (guint16 *)(ip + 1)];
3124 size = mono_class_value_size (c, NULL);
3125 memcpy(sp [-2].data.p, sp [-1].data.p, size);
3126 vt_sp -= (size + 7) & ~7;
3130 MINT_IN_CASE(MINT_STOBJ) {
3132 c = rtm->data_items[* (guint16 *)(ip + 1)];
3134 size = mono_class_value_size (c, NULL);
3135 memcpy(sp [-2].data.p, &sp [-1].data, size);
3139 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_R8)
3140 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3141 THROW_EX (mono_get_exception_overflow (), ip);
3142 sp [-1].data.i = (guint32)sp [-1].data.f;
3145 MINT_IN_CASE(MINT_CONV_OVF_U8_I4)
3146 if (sp [-1].data.i < 0)
3147 THROW_EX (mono_get_exception_overflow (), ip);
3148 sp [-1].data.l = sp [-1].data.i;
3151 MINT_IN_CASE(MINT_CONV_OVF_U8_R8)
3152 MINT_IN_CASE(MINT_CONV_OVF_I8_UN_R8)
3153 if (sp [-1].data.f < 0 || sp [-1].data.f > 9223372036854775807LL)
3154 THROW_EX (mono_get_exception_overflow (), ip);
3155 sp [-1].data.l = (guint64)sp [-1].data.f;
3158 MINT_IN_CASE(MINT_CONV_OVF_I8_R8)
3159 if (sp [-1].data.f < MYGINT64_MIN || sp [-1].data.f > MYGINT64_MAX)
3160 THROW_EX (mono_get_exception_overflow (), ip);
3161 sp [-1].data.l = (gint64)sp [-1].data.f;
3164 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_I8)
3165 if ((mono_u)sp [-1].data.l > MYGUINT32_MAX)
3166 THROW_EX (mono_get_exception_overflow (), ip);
3167 sp [-1].data.i = (mono_u)sp [-1].data.l;
3170 MINT_IN_CASE(MINT_BOX)
3171 c = rtm->data_items [* (guint16 *)(ip + 1)];
3173 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
3174 int size = mono_class_value_size (c, NULL);
3175 sp [-1].data.p = mono_value_box_checked (context->domain, c, sp [-1].data.p, &error);
3176 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3177 size = (size + 7) & ~7;
3181 stackval_to_data (&c->byval_arg, &sp [-1], (char*)&sp [-1], FALSE);
3182 sp [-1].data.p = mono_value_box_checked (context->domain, c, &sp [-1], &error);
3183 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3187 MINT_IN_CASE(MINT_NEWARR)
3188 sp [-1].data.p = (MonoObject*) mono_array_new_checked (context->domain, rtm->data_items[*(guint16 *)(ip + 1)], sp [-1].data.i, &error);
3189 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3191 /*if (profiling_classes) {
3192 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
3194 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
3198 MINT_IN_CASE(MINT_LDLEN)
3201 THROW_EX (mono_get_exception_null_reference (), ip);
3202 sp [-1].data.nati = mono_array_length ((MonoArray *)o);
3205 MINT_IN_CASE(MINT_GETCHR) {
3209 THROW_EX (mono_get_exception_null_reference (), ip);
3210 i32 = sp [-1].data.i;
3211 if (i32 < 0 || i32 >= mono_string_length (s))
3212 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3214 sp [-1].data.i = mono_string_chars(s)[i32];
3218 MINT_IN_CASE(MINT_STRLEN)
3220 sp [-1].data.i = mono_string_length ((MonoString*)sp [-1].data.p);
3222 MINT_IN_CASE(MINT_ARRAY_RANK)
3225 THROW_EX (mono_get_exception_null_reference (), ip);
3226 sp [-1].data.i = mono_object_class (sp [-1].data.p)->rank;
3229 MINT_IN_CASE(MINT_LDELEMA) {
3233 /*token = READ32 (ip)*/;
3239 aindex = sp [1].data.i;
3240 if (aindex >= mono_array_length ((MonoArray *) o))
3241 THROW_EX (mono_get_exception_index_out_of_range (), ip - 2);
3243 /* check the array element corresponds to token */
3244 esize = mono_array_element_size (((MonoArray *) o)->obj.vtable->klass);
3246 sp->data.p = mono_array_addr_with_size ((MonoArray *) o, esize, aindex);
3250 MINT_IN_CASE(MINT_LDELEM_I1) /* fall through */
3251 MINT_IN_CASE(MINT_LDELEM_U1) /* fall through */
3252 MINT_IN_CASE(MINT_LDELEM_I2) /* fall through */
3253 MINT_IN_CASE(MINT_LDELEM_U2) /* fall through */
3254 MINT_IN_CASE(MINT_LDELEM_I4) /* fall through */
3255 MINT_IN_CASE(MINT_LDELEM_U4) /* fall through */
3256 MINT_IN_CASE(MINT_LDELEM_I8) /* fall through */
3257 MINT_IN_CASE(MINT_LDELEM_I) /* fall through */
3258 MINT_IN_CASE(MINT_LDELEM_R4) /* fall through */
3259 MINT_IN_CASE(MINT_LDELEM_R8) /* fall through */
3260 MINT_IN_CASE(MINT_LDELEM_REF) {
3268 THROW_EX (mono_get_exception_null_reference (), ip);
3270 aindex = sp [1].data.i;
3271 if (aindex >= mono_array_length (o))
3272 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3275 * FIXME: throw mono_get_exception_array_type_mismatch () if needed
3278 case MINT_LDELEM_I1:
3279 sp [0].data.i = mono_array_get (o, gint8, aindex);
3281 case MINT_LDELEM_U1:
3282 sp [0].data.i = mono_array_get (o, guint8, aindex);
3284 case MINT_LDELEM_I2:
3285 sp [0].data.i = mono_array_get (o, gint16, aindex);
3287 case MINT_LDELEM_U2:
3288 sp [0].data.i = mono_array_get (o, guint16, aindex);
3291 sp [0].data.nati = mono_array_get (o, mono_i, aindex);
3293 case MINT_LDELEM_I4:
3294 sp [0].data.i = mono_array_get (o, gint32, aindex);
3296 case MINT_LDELEM_U4:
3297 sp [0].data.i = mono_array_get (o, guint32, aindex);
3299 case MINT_LDELEM_I8:
3300 sp [0].data.l = mono_array_get (o, guint64, aindex);
3302 case MINT_LDELEM_R4:
3303 sp [0].data.f = mono_array_get (o, float, aindex);
3305 case MINT_LDELEM_R8:
3306 sp [0].data.f = mono_array_get (o, double, aindex);
3308 case MINT_LDELEM_REF:
3309 sp [0].data.p = mono_array_get (o, gpointer, aindex);
3319 MINT_IN_CASE(MINT_STELEM_I) /* fall through */
3320 MINT_IN_CASE(MINT_STELEM_I1) /* fall through */
3321 MINT_IN_CASE(MINT_STELEM_I2) /* fall through */
3322 MINT_IN_CASE(MINT_STELEM_I4) /* fall through */
3323 MINT_IN_CASE(MINT_STELEM_I8) /* fall through */
3324 MINT_IN_CASE(MINT_STELEM_R4) /* fall through */
3325 MINT_IN_CASE(MINT_STELEM_R8) /* fall through */
3326 MINT_IN_CASE(MINT_STELEM_REF) {
3333 THROW_EX (mono_get_exception_null_reference (), ip);
3335 aindex = sp [1].data.i;
3336 if (aindex >= mono_array_length ((MonoArray *)o))
3337 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3341 mono_array_set ((MonoArray *)o, mono_i, aindex, sp [2].data.nati);
3343 case MINT_STELEM_I1:
3344 mono_array_set ((MonoArray *)o, gint8, aindex, sp [2].data.i);
3346 case MINT_STELEM_I2:
3347 mono_array_set ((MonoArray *)o, gint16, aindex, sp [2].data.i);
3349 case MINT_STELEM_I4:
3350 mono_array_set ((MonoArray *)o, gint32, aindex, sp [2].data.i);
3352 case MINT_STELEM_I8:
3353 mono_array_set ((MonoArray *)o, gint64, aindex, sp [2].data.l);
3355 case MINT_STELEM_R4:
3356 mono_array_set ((MonoArray *)o, float, aindex, sp [2].data.f);
3358 case MINT_STELEM_R8:
3359 mono_array_set ((MonoArray *)o, double, aindex, sp [2].data.f);
3361 case MINT_STELEM_REF: {
3362 MonoObject *isinst_obj = mono_object_isinst_checked (sp [2].data.p, mono_object_class (o)->element_class, &error);
3363 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3364 if (sp [2].data.p && !isinst_obj)
3365 THROW_EX (mono_get_exception_array_type_mismatch (), ip);
3366 mono_array_set ((MonoArray *)o, gpointer, aindex, sp [2].data.p);
3375 MINT_IN_CASE(MINT_CONV_OVF_I4_U4)
3376 if (sp [-1].data.i < 0)
3377 THROW_EX (mono_get_exception_overflow (), ip);
3380 MINT_IN_CASE(MINT_CONV_OVF_I4_I8)
3381 if (sp [-1].data.l < MYGINT32_MIN || sp [-1].data.l > MYGINT32_MAX)
3382 THROW_EX (mono_get_exception_overflow (), ip);
3383 sp [-1].data.i = (gint32) sp [-1].data.l;
3386 MINT_IN_CASE(MINT_CONV_OVF_I4_R8)
3387 if (sp [-1].data.f < MYGINT32_MIN || sp [-1].data.f > MYGINT32_MAX)
3388 THROW_EX (mono_get_exception_overflow (), ip);
3389 sp [-1].data.i = (gint32) sp [-1].data.f;
3392 MINT_IN_CASE(MINT_CONV_OVF_U4_I4)
3393 if (sp [-1].data.i < 0)
3394 THROW_EX (mono_get_exception_overflow (), ip);
3397 MINT_IN_CASE(MINT_CONV_OVF_U4_I8)
3398 if (sp [-1].data.l < 0 || sp [-1].data.l > MYGUINT32_MAX)
3399 THROW_EX (mono_get_exception_overflow (), ip);
3400 sp [-1].data.i = (guint32) sp [-1].data.l;
3403 MINT_IN_CASE(MINT_CONV_OVF_U4_R8)
3404 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3405 THROW_EX (mono_get_exception_overflow (), ip);
3406 sp [-1].data.i = (guint32) sp [-1].data.f;
3409 MINT_IN_CASE(MINT_CONV_OVF_I2_I4)
3410 if (sp [-1].data.i < -32768 || sp [-1].data.i > 32767)
3411 THROW_EX (mono_get_exception_overflow (), ip);
3414 MINT_IN_CASE(MINT_CONV_OVF_I2_I8)
3415 if (sp [-1].data.l < -32768 || sp [-1].data.l > 32767)
3416 THROW_EX (mono_get_exception_overflow (), ip);
3417 sp [-1].data.i = (gint16) sp [-1].data.l;
3420 MINT_IN_CASE(MINT_CONV_OVF_I2_R8)
3421 if (sp [-1].data.f < -32768 || sp [-1].data.f > 32767)
3422 THROW_EX (mono_get_exception_overflow (), ip);
3423 sp [-1].data.i = (gint16) sp [-1].data.f;
3426 MINT_IN_CASE(MINT_CONV_OVF_U2_I4)
3427 if (sp [-1].data.i < 0 || sp [-1].data.i > 65535)
3428 THROW_EX (mono_get_exception_overflow (), ip);
3431 MINT_IN_CASE(MINT_CONV_OVF_U2_I8)
3432 if (sp [-1].data.l < 0 || sp [-1].data.l > 65535)
3433 THROW_EX (mono_get_exception_overflow (), ip);
3434 sp [-1].data.i = (guint16) sp [-1].data.l;
3437 MINT_IN_CASE(MINT_CONV_OVF_U2_R8)
3438 if (sp [-1].data.f < 0 || sp [-1].data.f > 65535)
3439 THROW_EX (mono_get_exception_overflow (), ip);
3440 sp [-1].data.i = (guint16) sp [-1].data.f;
3443 MINT_IN_CASE(MINT_CONV_OVF_I1_I4)
3444 if (sp [-1].data.i < -128 || sp [-1].data.i > 127)
3445 THROW_EX (mono_get_exception_overflow (), ip);
3448 MINT_IN_CASE(MINT_CONV_OVF_I1_I8)
3449 if (sp [-1].data.l < -128 || sp [-1].data.l > 127)
3450 THROW_EX (mono_get_exception_overflow (), ip);
3451 sp [-1].data.i = (gint8) sp [-1].data.l;
3454 MINT_IN_CASE(MINT_CONV_OVF_I1_R8)
3455 if (sp [-1].data.f < -128 || sp [-1].data.f > 127)
3456 THROW_EX (mono_get_exception_overflow (), ip);
3457 sp [-1].data.i = (gint8) sp [-1].data.f;
3460 MINT_IN_CASE(MINT_CONV_OVF_U1_I4)
3461 if (sp [-1].data.i < 0 || sp [-1].data.i > 255)
3462 THROW_EX (mono_get_exception_overflow (), ip);
3465 MINT_IN_CASE(MINT_CONV_OVF_U1_I8)
3466 if (sp [-1].data.l < 0 || sp [-1].data.l > 255)
3467 THROW_EX (mono_get_exception_overflow (), ip);
3468 sp [-1].data.i = (guint8) sp [-1].data.l;
3471 MINT_IN_CASE(MINT_CONV_OVF_U1_R8)
3472 if (sp [-1].data.f < 0 || sp [-1].data.f > 255)
3473 THROW_EX (mono_get_exception_overflow (), ip);
3474 sp [-1].data.i = (guint8) sp [-1].data.f;
3478 MINT_IN_CASE(MINT_LDELEM)
3479 MINT_IN_CASE(MINT_STELEM)
3480 MINT_IN_CASE(MINT_UNBOX_ANY)
3482 MINT_IN_CASE(MINT_REFANYVAL) ves_abort(); MINT_IN_BREAK;
3484 MINT_IN_CASE(MINT_CKFINITE)
3485 if (!isfinite(sp [-1].data.f))
3486 THROW_EX (mono_get_exception_arithmetic (), ip);
3490 MINT_IN_CASE(MINT_MKREFANY) ves_abort(); MINT_IN_BREAK;
3492 MINT_IN_CASE(MINT_LDTOKEN)
3495 * (gpointer *)sp->data.p = rtm->data_items[*(guint16 *)(ip + 1)];
3499 MINT_IN_CASE(MINT_ADD_OVF_I4)
3500 if (CHECK_ADD_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3501 THROW_EX (mono_get_exception_overflow (), ip);
3504 MINT_IN_CASE(MINT_ADD_OVF_I8)
3505 if (CHECK_ADD_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3506 THROW_EX (mono_get_exception_overflow (), ip);
3509 MINT_IN_CASE(MINT_ADD_OVF_UN_I4)
3510 if (CHECK_ADD_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3511 THROW_EX (mono_get_exception_overflow (), ip);
3512 BINOP_CAST(i, +, guint32);
3514 MINT_IN_CASE(MINT_ADD_OVF_UN_I8)
3515 if (CHECK_ADD_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3516 THROW_EX (mono_get_exception_overflow (), ip);
3517 BINOP_CAST(l, +, guint64);
3519 MINT_IN_CASE(MINT_MUL_OVF_I4)
3520 if (CHECK_MUL_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3521 THROW_EX (mono_get_exception_overflow (), ip);
3524 MINT_IN_CASE(MINT_MUL_OVF_I8)
3525 if (CHECK_MUL_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3526 THROW_EX (mono_get_exception_overflow (), ip);
3529 MINT_IN_CASE(MINT_MUL_OVF_UN_I4)
3530 if (CHECK_MUL_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3531 THROW_EX (mono_get_exception_overflow (), ip);
3532 BINOP_CAST(i, *, guint32);
3534 MINT_IN_CASE(MINT_MUL_OVF_UN_I8)
3535 if (CHECK_MUL_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3536 THROW_EX (mono_get_exception_overflow (), ip);
3537 BINOP_CAST(l, *, guint64);
3539 MINT_IN_CASE(MINT_SUB_OVF_I4)
3540 if (CHECK_SUB_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3541 THROW_EX (mono_get_exception_overflow (), ip);
3544 MINT_IN_CASE(MINT_SUB_OVF_I8)
3545 if (CHECK_SUB_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3546 THROW_EX (mono_get_exception_overflow (), ip);
3549 MINT_IN_CASE(MINT_SUB_OVF_UN_I4)
3550 if (CHECK_SUB_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3551 THROW_EX (mono_get_exception_overflow (), ip);
3552 BINOP_CAST(i, -, guint32);
3554 MINT_IN_CASE(MINT_SUB_OVF_UN_I8)
3555 if (CHECK_SUB_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3556 THROW_EX (mono_get_exception_overflow (), ip);
3557 BINOP_CAST(l, -, guint64);
3559 MINT_IN_CASE(MINT_ENDFINALLY)
3561 ip = finally_ips->data;
3562 finally_ips = g_slist_remove (finally_ips, ip);
3569 MINT_IN_CASE(MINT_LEAVE) /* Fall through */
3570 MINT_IN_CASE(MINT_LEAVE_S)
3571 while (sp > frame->stack) {
3575 if (*ip == MINT_LEAVE_S) {
3576 ip += (short) *(ip + 1);
3578 ip += (gint32) READ32 (ip + 1);
3581 if (frame->ex_handler != NULL && MONO_OFFSET_IN_HANDLER(frame->ex_handler, frame->ip - rtm->code)) {
3582 frame->ex_handler = NULL;
3585 goto handle_finally;
3587 MINT_IN_CASE(MINT_ICALL_V_V)
3588 MINT_IN_CASE(MINT_ICALL_P_V)
3589 MINT_IN_CASE(MINT_ICALL_P_P)
3590 MINT_IN_CASE(MINT_ICALL_PP_V)
3591 MINT_IN_CASE(MINT_ICALL_PI_V)
3592 MINT_IN_CASE(MINT_ICALL_PP_P)
3593 MINT_IN_CASE(MINT_ICALL_PI_P)
3594 MINT_IN_CASE(MINT_ICALL_PPP_V)
3595 MINT_IN_CASE(MINT_ICALL_PPI_V)
3596 sp = do_icall (context, *ip, sp, rtm->data_items [*(guint16 *)(ip + 1)]);
3597 if (frame->ex != NULL)
3598 goto handle_exception;
3601 MINT_IN_CASE(MINT_MONO_LDPTR)
3602 sp->data.p = rtm->data_items [*(guint16 *)(ip + 1)];
3606 MINT_IN_CASE(MINT_MONO_NEWOBJ)
3607 sp->data.p = mono_object_new_checked (context->domain, rtm->data_items [*(guint16 *)(ip + 1)], &error);
3608 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3612 MINT_IN_CASE(MINT_MONO_FREE)
3615 g_error ("that doesn't seem right");
3616 g_free (sp->data.p);
3618 MINT_IN_CASE(MINT_MONO_RETOBJ)
3621 stackval_from_data (mono_method_signature (frame->runtime_method->method)->ret, frame->retval, sp->data.p,
3622 mono_method_signature (frame->runtime_method->method)->pinvoke);
3623 if (sp > frame->stack)
3624 g_warning ("retobj: more values on stack: %d", sp-frame->stack);
3627 #define RELOP(datamem, op) \
3629 sp [-1].data.i = sp [-1].data.datamem op sp [0].data.datamem; \
3631 MINT_IN_CASE(MINT_CEQ_I4)
3634 MINT_IN_CASE(MINT_CEQ0_I4)
3635 sp [-1].data.i = (sp [-1].data.i == 0);
3638 MINT_IN_CASE(MINT_CEQ_I8)
3641 MINT_IN_CASE(MINT_CEQ_R8)
3643 if (isunordered (sp [-1].data.f, sp [0].data.f))
3646 sp [-1].data.i = sp [-1].data.f == sp [0].data.f;
3649 MINT_IN_CASE(MINT_CGT_I4)
3652 MINT_IN_CASE(MINT_CGT_I8)
3655 MINT_IN_CASE(MINT_CGT_R8)
3657 if (isunordered (sp [-1].data.f, sp [0].data.f))
3660 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3664 #define RELOP_CAST(datamem, op, type) \
3666 sp [-1].data.i = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
3669 MINT_IN_CASE(MINT_CGT_UN_I4)
3670 RELOP_CAST(i, >, guint32);
3672 MINT_IN_CASE(MINT_CGT_UN_I8)
3673 RELOP_CAST(l, >, guint64);
3675 MINT_IN_CASE(MINT_CGT_UN_R8)
3677 if (isunordered (sp [-1].data.f, sp [0].data.f))
3680 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3683 MINT_IN_CASE(MINT_CLT_I4)
3686 MINT_IN_CASE(MINT_CLT_I8)
3689 MINT_IN_CASE(MINT_CLT_R8)
3691 if (isunordered (sp [-1].data.f, sp [0].data.f))
3694 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3697 MINT_IN_CASE(MINT_CLT_UN_I4)
3698 RELOP_CAST(i, <, guint32);
3700 MINT_IN_CASE(MINT_CLT_UN_I8)
3701 RELOP_CAST(l, <, guint64);
3703 MINT_IN_CASE(MINT_CLT_UN_R8)
3705 if (isunordered (sp [-1].data.f, sp [0].data.f))
3708 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3711 MINT_IN_CASE(MINT_LDFTN) {
3712 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
3717 MINT_IN_CASE(MINT_LDVIRTFTN) {
3718 RuntimeMethod *m = rtm->data_items [* (guint16 *)(ip + 1)];
3722 THROW_EX (mono_get_exception_null_reference (), ip - 2);
3724 sp->data.p = get_virtual_method (context->domain, m, sp->data.p);
3729 MINT_IN_CASE(MINT_LDTHIS)
3730 sp->data.p = frame->obj;
3734 MINT_IN_CASE(MINT_STTHIS)
3736 frame->obj = sp->data.p;
3739 MINT_IN_CASE(MINT_LDTHISA)
3740 sp->data.p = &frame->obj;
3745 #define LDARG(datamem, argtype) \
3746 sp->data.datamem = * (argtype *)(frame->args + * (guint16 *)(ip + 1)); \
3750 MINT_IN_CASE(MINT_LDARG_I1) LDARG(i, gint8); MINT_IN_BREAK;
3751 MINT_IN_CASE(MINT_LDARG_U1) LDARG(i, guint8); MINT_IN_BREAK;
3752 MINT_IN_CASE(MINT_LDARG_I2) LDARG(i, gint16); MINT_IN_BREAK;
3753 MINT_IN_CASE(MINT_LDARG_U2) LDARG(i, guint16); MINT_IN_BREAK;
3754 MINT_IN_CASE(MINT_LDARG_I4) LDARG(i, gint32); MINT_IN_BREAK;
3755 MINT_IN_CASE(MINT_LDARG_I8) LDARG(l, gint64); MINT_IN_BREAK;
3756 MINT_IN_CASE(MINT_LDARG_R4) LDARG(f, float); MINT_IN_BREAK;
3757 MINT_IN_CASE(MINT_LDARG_R8) LDARG(f, double); MINT_IN_BREAK;
3758 MINT_IN_CASE(MINT_LDARG_O) LDARG(p, gpointer); MINT_IN_BREAK;
3759 MINT_IN_CASE(MINT_LDARG_P) LDARG(p, gpointer); MINT_IN_BREAK;
3761 MINT_IN_CASE(MINT_LDARG_VT)
3763 i32 = READ32(ip + 2);
3764 memcpy(sp->data.p, frame->args + * (guint16 *)(ip + 1), i32);
3765 vt_sp += (i32 + 7) & ~7;
3770 #define STARG(datamem, argtype) \
3772 * (argtype *)(frame->args + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3775 MINT_IN_CASE(MINT_STARG_I1) STARG(i, gint8); MINT_IN_BREAK;
3776 MINT_IN_CASE(MINT_STARG_U1) STARG(i, guint8); MINT_IN_BREAK;
3777 MINT_IN_CASE(MINT_STARG_I2) STARG(i, gint16); MINT_IN_BREAK;
3778 MINT_IN_CASE(MINT_STARG_U2) STARG(i, guint16); MINT_IN_BREAK;
3779 MINT_IN_CASE(MINT_STARG_I4) STARG(i, gint32); MINT_IN_BREAK;
3780 MINT_IN_CASE(MINT_STARG_I8) STARG(l, gint64); MINT_IN_BREAK;
3781 MINT_IN_CASE(MINT_STARG_R4) STARG(f, float); MINT_IN_BREAK;
3782 MINT_IN_CASE(MINT_STARG_R8) STARG(f, double); MINT_IN_BREAK;
3783 MINT_IN_CASE(MINT_STARG_O) STARG(p, gpointer); MINT_IN_BREAK;
3784 MINT_IN_CASE(MINT_STARG_P) STARG(p, gpointer); MINT_IN_BREAK;
3786 MINT_IN_CASE(MINT_STARG_VT)
3787 i32 = READ32(ip + 2);
3789 memcpy(frame->args + * (guint16 *)(ip + 1), sp->data.p, i32);
3790 vt_sp -= (i32 + 7) & ~7;
3794 #define STINARG(datamem, argtype) \
3796 int n = * (guint16 *)(ip + 1); \
3797 * (argtype *)(frame->args + rtm->arg_offsets [n]) = frame->stack_args [n].data.datamem; \
3801 MINT_IN_CASE(MINT_STINARG_I1) STINARG(i, gint8); MINT_IN_BREAK;
3802 MINT_IN_CASE(MINT_STINARG_U1) STINARG(i, guint8); MINT_IN_BREAK;
3803 MINT_IN_CASE(MINT_STINARG_I2) STINARG(i, gint16); MINT_IN_BREAK;
3804 MINT_IN_CASE(MINT_STINARG_U2) STINARG(i, guint16); MINT_IN_BREAK;
3805 MINT_IN_CASE(MINT_STINARG_I4) STINARG(i, gint32); MINT_IN_BREAK;
3806 MINT_IN_CASE(MINT_STINARG_I8) STINARG(l, gint64); MINT_IN_BREAK;
3807 MINT_IN_CASE(MINT_STINARG_R4) STINARG(f, float); MINT_IN_BREAK;
3808 MINT_IN_CASE(MINT_STINARG_R8) STINARG(f, double); MINT_IN_BREAK;
3809 MINT_IN_CASE(MINT_STINARG_O) STINARG(p, gpointer); MINT_IN_BREAK;
3810 MINT_IN_CASE(MINT_STINARG_P) STINARG(p, gpointer); MINT_IN_BREAK;
3812 MINT_IN_CASE(MINT_STINARG_VT) {
3813 int n = * (guint16 *)(ip + 1);
3814 i32 = READ32(ip + 2);
3815 memcpy (frame->args + rtm->arg_offsets [n], frame->stack_args [n].data.p, i32);
3820 MINT_IN_CASE(MINT_LDARGA)
3821 sp->data.p = frame->args + * (guint16 *)(ip + 1);
3826 #define LDLOC(datamem, argtype) \
3827 sp->data.datamem = * (argtype *)(locals + * (guint16 *)(ip + 1)); \
3831 MINT_IN_CASE(MINT_LDLOC_I1) LDLOC(i, gint8); MINT_IN_BREAK;
3832 MINT_IN_CASE(MINT_LDLOC_U1) LDLOC(i, guint8); MINT_IN_BREAK;
3833 MINT_IN_CASE(MINT_LDLOC_I2) LDLOC(i, gint16); MINT_IN_BREAK;
3834 MINT_IN_CASE(MINT_LDLOC_U2) LDLOC(i, guint16); MINT_IN_BREAK;
3835 MINT_IN_CASE(MINT_LDLOC_I4) LDLOC(i, gint32); MINT_IN_BREAK;
3836 MINT_IN_CASE(MINT_LDLOC_I8) LDLOC(l, gint64); MINT_IN_BREAK;
3837 MINT_IN_CASE(MINT_LDLOC_R4) LDLOC(f, float); MINT_IN_BREAK;
3838 MINT_IN_CASE(MINT_LDLOC_R8) LDLOC(f, double); MINT_IN_BREAK;
3839 MINT_IN_CASE(MINT_LDLOC_O) LDLOC(p, gpointer); MINT_IN_BREAK;
3840 MINT_IN_CASE(MINT_LDLOC_P) LDLOC(p, gpointer); MINT_IN_BREAK;
3842 MINT_IN_CASE(MINT_LDLOC_VT)
3844 i32 = READ32(ip + 2);
3845 memcpy(sp->data.p, locals + * (guint16 *)(ip + 1), i32);
3846 vt_sp += (i32 + 7) & ~7;
3851 MINT_IN_CASE(MINT_LDLOCA_S)
3852 sp->data.p = locals + * (guint16 *)(ip + 1);
3857 #define STLOC(datamem, argtype) \
3859 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3862 MINT_IN_CASE(MINT_STLOC_I1) STLOC(i, gint8); MINT_IN_BREAK;
3863 MINT_IN_CASE(MINT_STLOC_U1) STLOC(i, guint8); MINT_IN_BREAK;
3864 MINT_IN_CASE(MINT_STLOC_I2) STLOC(i, gint16); MINT_IN_BREAK;
3865 MINT_IN_CASE(MINT_STLOC_U2) STLOC(i, guint16); MINT_IN_BREAK;
3866 MINT_IN_CASE(MINT_STLOC_I4) STLOC(i, gint32); MINT_IN_BREAK;
3867 MINT_IN_CASE(MINT_STLOC_I8) STLOC(l, gint64); MINT_IN_BREAK;
3868 MINT_IN_CASE(MINT_STLOC_R4) STLOC(f, float); MINT_IN_BREAK;
3869 MINT_IN_CASE(MINT_STLOC_R8) STLOC(f, double); MINT_IN_BREAK;
3870 MINT_IN_CASE(MINT_STLOC_O) STLOC(p, gpointer); MINT_IN_BREAK;
3871 MINT_IN_CASE(MINT_STLOC_P) STLOC(p, gpointer); MINT_IN_BREAK;
3873 #define STLOC_NP(datamem, argtype) \
3874 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp [-1].data.datamem; \
3877 MINT_IN_CASE(MINT_STLOC_NP_I4) STLOC_NP(i, gint32); MINT_IN_BREAK;
3878 MINT_IN_CASE(MINT_STLOC_NP_O) STLOC_NP(p, gpointer); MINT_IN_BREAK;
3880 MINT_IN_CASE(MINT_STLOC_VT)
3881 i32 = READ32(ip + 2);
3883 memcpy(locals + * (guint16 *)(ip + 1), sp->data.p, i32);
3884 vt_sp -= (i32 + 7) & ~7;
3888 MINT_IN_CASE(MINT_LOCALLOC)
3889 if (sp != frame->stack + 1) /*FIX?*/
3890 THROW_EX (mono_get_exception_execution_engine (NULL), ip);
3891 sp [-1].data.p = alloca (sp [-1].data.i);
3895 MINT_IN_CASE(MINT_ENDFILTER) ves_abort(); MINT_IN_BREAK;
3897 MINT_IN_CASE(MINT_INITOBJ)
3899 memset (sp->data.vt, 0, READ32(ip + 1));
3902 MINT_IN_CASE(MINT_CPBLK)
3904 if (!sp [0].data.p || !sp [1].data.p)
3905 THROW_EX (mono_get_exception_null_reference(), ip - 1);
3907 /* FIXME: value and size may be int64... */
3908 memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
3911 MINT_IN_CASE(MINT_CONSTRAINED_) {
3913 /* FIXME: implement */
3915 token = READ32 (ip);
3920 MINT_IN_CASE(MINT_INITBLK)
3923 THROW_EX (mono_get_exception_null_reference(), ip - 1);
3925 /* FIXME: value and size may be int64... */
3926 memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
3929 MINT_IN_CASE(MINT_NO_)
3930 /* FIXME: implement */
3934 MINT_IN_CASE(MINT_RETHROW)
3936 * need to clarify what this should actually do:
3937 * start the search from the last found handler in
3938 * this method or continue in the caller or what.
3939 * Also, do we need to run finally/fault handlers after a retrow?
3940 * Well, this implementation will follow the usual search
3941 * for an handler, considering the current ip as throw spot.
3942 * We need to NULL frame->ex_handler for the later code to
3943 * actually run the new found handler.
3945 frame->ex_handler = NULL;
3946 THROW_EX (frame->ex, ip - 1);
3949 g_print ("Unimplemented opcode: %04x %s at 0x%x\n", *ip, mono_interp_opname[*ip], ip-rtm->code);
3950 THROW_EX (mono_get_exception_execution_engine ("Unimplemented opcode"), ip);
3954 g_assert_not_reached ();
3956 * Exception handling code.
3957 * The exception object is stored in frame->ex.
3964 MonoInvocation *inv;
3965 MonoExceptionClause *clause;
3971 g_print ("* Handling exception '%s' at IL_%04x\n",
3972 frame->ex == NULL ? "** Unknown **" : mono_object_class (frame->ex)->name,
3973 rtm == NULL ? 0 : frame->ip - rtm->code);
3975 if (die_on_exception)
3978 for (inv = frame; inv; inv = inv->parent) {
3980 if (inv->runtime_method == NULL)
3982 method = inv->runtime_method->method;
3983 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
3985 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
3987 if (inv->ip == NULL)
3989 ip_offset = inv->ip - inv->runtime_method->code;
3990 inv->ex_handler = NULL; /* clear this in case we are trhowing an exception while handling one - this one wins */
3991 for (i = 0; i < inv->runtime_method->num_clauses; ++i) {
3992 clause = &inv->runtime_method->clauses [i];
3993 if (clause->flags <= 1 && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
3994 if (!clause->flags) {
3995 MonoObject *isinst_obj = mono_object_isinst_checked ((MonoObject*)frame->ex, clause->data.catch_class, &error);
3996 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3999 * OK, we found an handler, now we need to execute the finally
4000 * and fault blocks before branching to the handler code.
4002 inv->ex_handler = clause;
4005 g_print ("* Found handler at '%s'\n", method->name);
4007 goto handle_finally;
4010 /* FIXME: handle filter clauses */
4017 * If we get here, no handler was found: print a stack trace.
4019 for (inv = frame; inv; inv = inv->parent) {
4020 if (inv->invoke_trap)
4021 goto handle_finally;
4024 ex_obj = (MonoObject*)frame->ex;
4025 mono_unhandled_exception (ex_obj);
4032 MonoExceptionClause *clause;
4033 GSList *old_list = finally_ips;
4034 MonoMethod *method = frame->runtime_method->method;
4035 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
4036 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4040 g_print ("* Handle finally IL_%04x\n", endfinally_ip == NULL ? 0 : endfinally_ip - rtm->code);
4042 if (rtm == NULL || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
4043 || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
4046 ip_offset = frame->ip - rtm->code;
4048 if (endfinally_ip != NULL)
4049 finally_ips = g_slist_prepend(finally_ips, (void *)endfinally_ip);
4050 for (i = 0; i < header->num_clauses; ++i)
4051 if (frame->ex_handler == &rtm->clauses [i])
4055 clause = &rtm->clauses [i];
4056 if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && (endfinally_ip == NULL || !(MONO_OFFSET_IN_CLAUSE (clause, endfinally_ip - rtm->code)))) {
4057 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
4058 ip = rtm->code + clause->handler_offset;
4059 finally_ips = g_slist_prepend (finally_ips, (gpointer) ip);
4062 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
4068 endfinally_ip = NULL;
4070 if (old_list != finally_ips && finally_ips) {
4071 ip = finally_ips->data;
4072 finally_ips = g_slist_remove (finally_ips, ip);
4073 sp = frame->stack; /* spec says stack should be empty at endfinally so it should be at the start too */
4078 * If an exception is set, we need to execute the fault handler, too,
4079 * otherwise, we continue normally.
4089 MonoExceptionClause *clause;
4090 MonoMethod *method = frame->runtime_method->method;
4091 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
4092 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4096 g_print ("* Handle fault\n");
4098 ip_offset = frame->ip - rtm->code;
4099 for (i = 0; i < header->num_clauses; ++i) {
4100 clause = &rtm->clauses [i];
4101 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4102 ip = rtm->code + clause->handler_offset;
4105 g_print ("* Executing handler at IL_%04x\n", clause->handler_offset);
4111 * If the handler for the exception was found in this method, we jump
4112 * to it right away, otherwise we return and let the caller run
4113 * the finally, fault and catch blocks.
4114 * This same code should be present in the endfault opcode, but it
4115 * is corrently not assigned in the ECMA specs: LAMESPEC.
4117 if (frame->ex_handler) {
4120 g_print ("* Executing handler at IL_%04x\n", frame->ex_handler->handler_offset);
4122 ip = rtm->code + frame->ex_handler->handler_offset;
4124 vt_sp = (unsigned char *) sp + rtm->stack_size;
4125 sp->data.p = frame->ex;
4136 ves_exec_method (MonoInvocation *frame)
4138 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4139 ThreadContext context_struct;
4146 mono_unhandled_exception ((MonoObject*)frame->ex);
4149 if (context == NULL) {
4150 context = &context_struct;
4151 context_struct.domain = mono_domain_get ();
4152 context_struct.base_frame = frame;
4153 context_struct.current_frame = NULL;
4154 context_struct.env_frame = frame;
4155 context_struct.current_env = &env;
4156 context_struct.search_for_handler = 0;
4157 context_struct.managed_code = 0;
4158 mono_native_tls_set_value (thread_context_id, context);
4161 frame->parent = context->current_frame;
4162 frame->runtime_method = mono_interp_get_runtime_method (context->domain, frame->method, &error);
4163 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4164 context->managed_code = 1;
4165 ves_exec_method_with_context (frame, context);
4166 context->managed_code = 0;
4168 if (context != &context_struct && context->current_env) {
4169 context->env_frame->ex = frame->ex;
4170 longjmp (*context->current_env, 1);
4173 mono_unhandled_exception ((MonoObject*)frame->ex);
4175 if (context->base_frame == frame)
4176 mono_native_tls_set_value (thread_context_id, NULL);
4178 context->current_frame = frame->parent;
4182 ves_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4184 MonoImage *image = mono_assembly_get_image (assembly);
4189 method = mono_get_method_checked (image, mono_image_get_entry_point (image), NULL, NULL, &error);
4190 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4193 g_error ("No entry point method found in %s", mono_image_get_filename (image));
4195 rval = mono_runtime_run_main_checked (method, argc, argv, &error);
4196 return_val_and_set_pending_if_nok (&error, rval);
4203 "mint %s, the Mono ECMA CLI interpreter, (C) 2001, 2002 Ximian, Inc.\n\n"
4204 "Usage is: mint [options] executable args...\n\n", VERSION);
4206 "Runtime Debugging:\n"
4211 " --noptr\t\t\tdon't print pointer addresses in trace output\n"
4214 " --traceclassinit\n"
4217 " --debug method_name\n"
4224 " --config filename load the specified config file instead of the default\n"
4225 " --workers n maximum number of worker threads\n"
4231 add_signal_handler (int signo, void (*handler)(int))
4234 signal (signo, handler);
4236 struct sigaction sa;
4238 sa.sa_handler = handler;
4239 sigemptyset (&sa.sa_mask);
4242 g_assert (sigaction (signo, &sa, NULL) != -1);
4247 segv_handler (int signum)
4249 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4250 MonoException *segv_exception;
4252 if (context == NULL)
4254 segv_exception = mono_get_exception_null_reference ();
4255 segv_exception->message = mono_string_new (mono_domain_get (), "Null Reference (SIGSEGV)");
4256 mono_raise_exception (segv_exception);
4261 quit_handler (int signum)
4263 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4264 MonoException *quit_exception;
4266 if (context == NULL)
4268 quit_exception = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
4269 mono_raise_exception (quit_exception);
4273 abrt_handler (int signum)
4275 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4276 MonoException *abrt_exception;
4278 if (context == NULL)
4280 abrt_exception = mono_get_exception_execution_engine ("Abort (SIGABRT).");
4281 mono_raise_exception (abrt_exception);
4286 thread_abort_handler (int signum)
4288 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4291 if (context == NULL)
4294 exc = mono_thread_request_interruption (context->managed_code);
4295 if (exc) mono_raise_exception (exc);
4300 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
4301 MonoReflectionMethod **method,
4302 gint32 *iloffset, gint32 *native_offset,
4303 MonoString **file, gint32 *line, gint32 *column)
4305 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4306 MonoInvocation *inv = context->current_frame;
4310 for (i = 0; inv && i < skip; inv = inv->parent)
4311 if (inv->runtime_method != NULL)
4322 *method = mono_method_get_object_checked (context->domain, inv->runtime_method->method, NULL, &error);
4323 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4328 if (need_file_info) {
4332 *file = mono_string_new (mono_domain_get (), "unknown");
4339 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
4341 MonoDomain *domain = mono_domain_get ();
4343 MonoArray *ta = exc->trace_ips;
4348 /* Exception is not thrown yet */
4349 MonoArray *array = mono_array_new_checked (domain, mono_defaults.stack_frame_class, 0, &error);
4350 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4354 len = mono_array_length (ta);
4356 res = mono_array_new_checked (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0, &error);
4357 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4359 for (i = skip; i < len / 2; i++) {
4360 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new_checked (domain, mono_defaults.stack_frame_class, &error);
4361 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4362 gushort *ip = mono_array_get (ta, gpointer, 2 * i + 1);
4363 RuntimeMethod *rtm = mono_array_get (ta, gpointer, 2 * i);
4366 sf->method = mono_method_get_object_checked (domain, rtm->method, NULL, &error);
4367 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4368 sf->native_offset = ip - rtm->code;
4372 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
4374 if (need_file_info) {
4377 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
4379 sf->filename = filename? mono_string_new (domain, filename): NULL;
4386 mono_array_set (res, gpointer, i, sf);
4393 ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionType *type, MonoObject *target, MonoReflectionMethod *info)
4395 MonoClass *delegate_class = mono_class_from_mono_type (type->type);
4396 MonoObject *delegate;
4399 g_assert (delegate_class->parent == mono_defaults.multicastdelegate_class);
4401 delegate = mono_object_new_checked (mono_object_domain (type), delegate_class, &error);
4402 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4404 interp_delegate_ctor (mono_object_domain (type), delegate, target, mono_interp_get_runtime_method (mono_get_root_domain (), info->method, &error));
4405 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4414 int enable_debugging;
4420 static void main_thread_handler (gpointer user_data)
4422 MainThreadArgs *main_args=(MainThreadArgs *)user_data;
4423 MonoAssembly *assembly;
4425 if (main_args->enable_debugging) {
4426 mono_debug_init (MONO_DEBUG_FORMAT_MONO);
4429 assembly = mono_domain_assembly_open (main_args->domain, main_args->file);
4432 fprintf (stderr, "Can not open image %s\n", main_args->file);
4436 ves_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
4440 mono_runtime_install_handlers (void)
4442 add_signal_handler (SIGSEGV, segv_handler);
4443 add_signal_handler (SIGINT, quit_handler);
4444 add_signal_handler (SIGABRT, abrt_handler);
4446 add_signal_handler (mono_thread_get_abort_signal (), thread_abort_handler);
4451 quit_function (MonoDomain *domain, gpointer user_data)
4453 mono_profiler_shutdown ();
4455 mono_runtime_cleanup (domain);
4456 mono_domain_free (domain, TRUE);
4461 mono_interp_cleanup(MonoDomain *domain)
4463 quit_function (domain, NULL);
4467 mono_interp_exec(MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4469 return ves_exec (domain, assembly, argc, argv);
4473 interp_get_imt_trampoline (MonoVTable *vtable, int imt_slot_index)
4475 // FIXME: implement me
4480 interp_create_ftnptr (MonoDomain *domain, gpointer addr)
4482 // FIXME: true on all arch?
4487 mono_interp_init(const char *file)
4490 MonoRuntimeCallbacks callbacks;
4491 MonoRuntimeExceptionHandlingCallbacks ecallbacks;
4494 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
4495 g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
4497 mono_native_tls_alloc (&thread_context_id, NULL);
4498 mono_native_tls_set_value (thread_context_id, NULL);
4499 mono_os_mutex_init_recursive (&runtime_method_lookup_section);
4500 mono_os_mutex_init_recursive (&create_method_pointer_mutex);
4502 // TODO: use callbacks?
4503 mono_runtime_install_handlers ();
4504 mono_interp_transform_init ();
4506 memset (&callbacks, 0, sizeof (callbacks));
4507 // TODO: replace with `mono_install_callbacks`
4508 callbacks.compile_method = mono_create_method_pointer;
4509 callbacks.runtime_invoke = interp_mono_runtime_invoke;
4510 callbacks.get_imt_trampoline = interp_get_imt_trampoline;
4511 callbacks.create_ftnptr = interp_create_ftnptr;
4512 #ifndef DISABLE_REMOTING
4513 mono_install_remoting_trampoline (interp_create_remoting_trampoline);
4515 callbacks.create_jit_trampoline = interp_create_trampoline;
4516 mono_install_callbacks (&callbacks);
4519 memset (&ecallbacks, 0, sizeof (ecallbacks));
4520 ecallbacks.mono_raise_exception = interp_ex_handler;
4523 mono_install_stack_walk (interp_walk_stack);
4525 mono_install_eh_callbacks (&ecallbacks);
4527 mono_install_runtime_cleanup (quit_function);
4529 abort_requested = mono_thread_interruption_request_flag ();
4531 domain = mono_init_from_assembly (file, file);
4532 #ifdef __hpux /* generates very big stack frames */
4533 mono_threads_set_default_stacksize(32*1024*1024);
4536 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info", ves_icall_get_frame_info);
4537 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace", ves_icall_get_trace);
4538 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers", mono_runtime_install_handlers);
4539 mono_add_internal_call ("System.Delegate::CreateDelegate_internal", ves_icall_System_Delegate_CreateDelegate_internal);
4541 mono_register_jit_icall (mono_thread_interruption_checkpoint, "mono_thread_interruption_checkpoint", mono_create_icall_signature ("void"), FALSE);
4543 mono_runtime_init_checked (domain, NULL, NULL, &error);
4544 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4546 mono_thread_attach (domain);
4550 typedef int (*TestMethod) (void);
4553 interp_regression_step (MonoImage *image, int verbose, int *total_run, int *total, GTimer *timer, MonoDomain *domain)
4555 int result, expected, failed, cfailed, run;
4557 double elapsed, transform_time, start_time;
4559 MonoObject *result_obj;
4561 g_print ("Test run: image=%s\n", mono_image_get_filename (image));
4562 cfailed = failed = run = 0;
4563 transform_time = elapsed = 0.0;
4566 /* fixme: ugly hack - delete all previously compiled methods */
4567 if (domain_jit_info (domain)) {
4568 g_hash_table_destroy (domain_jit_info (domain)->jit_trampoline_hash);
4569 domain_jit_info (domain)->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
4570 mono_internal_hash_table_destroy (&(domain->jit_code_hash));
4571 mono_jit_code_hash_init (&(domain->jit_code_hash));
4575 g_timer_start (timer);
4576 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
4578 MonoMethod *method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
4580 mono_error_cleanup (&error); /* FIXME don't swallow the error */
4583 if (strncmp (method->name, "test_", 5) == 0) {
4584 MonoError interp_error;
4585 RuntimeMethod *runtime_method;
4586 MonoInvocation frame;
4587 ThreadContext context;
4588 stackval frame_result;
4590 result_obj = interp_mono_runtime_invoke (method, NULL, NULL, NULL, &interp_error);
4591 if (!mono_error_ok (&interp_error)) {
4593 g_print ("Test '%s' execution failed.\n", method->name);
4595 result = *(gint32 *) mono_object_unbox (result_obj);
4596 expected = atoi (method->name + 5); // FIXME: oh no.
4599 if (result != expected) {
4601 g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
4606 g_timer_stop (timer);
4607 elapsed = g_timer_elapsed (timer, NULL);
4608 if (failed > 0 || cfailed > 0){
4609 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
4610 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
4612 g_print ("Results: total tests: %d, all pass \n", run);
4615 g_print ("Elapsed time: %f secs (%f, %f)\n\n", elapsed,
4616 elapsed - transform_time, transform_time);
4617 *total += failed + cfailed;
4621 interp_regression (MonoImage *image, int verbose, int *total_run)
4625 GTimer *timer = g_timer_new ();
4626 MonoDomain *domain = mono_domain_get ();
4627 guint32 i, exclude = 0;
4630 /* load the metadata */
4631 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
4633 method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
4635 mono_error_cleanup (&error);
4638 mono_class_init (method->klass);
4643 interp_regression_step (image, verbose, total_run, &total, timer, domain);
4645 g_timer_destroy (timer);
4650 interp_regression_list (int verbose, int count, char *images [])
4652 int i, total, total_run, run;
4654 total_run = total = 0;
4655 for (i = 0; i < count; ++i) {
4656 MonoAssembly *ass = mono_assembly_open (images [i], NULL);
4658 g_warning ("failed to load assembly: %s", images [i]);
4661 total += interp_regression (mono_assembly_get_image (ass), verbose, &run);
4665 g_print ("Overall results: tests: %d, failed: %d (pass: %.2f%%)\n", total_run, total, 100.0*(total_run-total)/total_run);
4667 g_print ("Overall results: tests: %d, 100%% pass\n", total_run);