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.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 #include <mono/mini/mini.h>
68 /* Mingw 2.1 doesnt need this any more, but leave it in for now for older versions */
71 #define finite _finite
75 #define finite isfinite
79 static gint *abort_requested;
81 int mono_interp_traceopt = 0;
83 #define INIT_FRAME(frame,parent_frame,obj_this,method_args,method_retval,domain,mono_method,error) \
85 (frame)->parent = (parent_frame); \
86 (frame)->obj = (obj_this); \
87 (frame)->stack_args = (method_args); \
88 (frame)->retval = (method_retval); \
89 (frame)->runtime_method = mono_interp_get_runtime_method ((domain), (mono_method), (error)); \
92 (frame)->invoke_trap = 0; \
95 void ves_exec_method (MonoInvocation *frame);
97 static char* dump_stack (stackval *stack, stackval *sp);
98 static char* dump_frame (MonoInvocation *inv);
99 static MonoArray *get_trace_ips (MonoDomain *domain, MonoInvocation *top);
100 static void ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context);
102 typedef void (*ICallMethod) (MonoInvocation *frame);
104 static guint32 die_on_exception = 0;
105 static MonoNativeTlsKey thread_context_id;
107 static char* dump_args (MonoInvocation *inv);
109 #define DEBUG_INTERP 0
112 /* If true, then we output the opcodes as we interpret them */
113 static int global_tracing = 1;
115 static int debug_indent_level = 0;
117 static int break_on_method = 0;
118 static int nested_trace = 0;
119 static GList *db_methods = NULL;
126 for (h = 0; h < debug_indent_level; h++)
131 db_match_method (gpointer data, gpointer user_data)
133 MonoMethod *m = (MonoMethod*)user_data;
134 MonoMethodDesc *desc = data;
136 if (mono_method_desc_full_match (desc, m))
140 static void debug_enter (MonoInvocation *frame, int *tracing)
143 g_list_foreach (db_methods, db_match_method, (gpointer)frame->runtime_method->method);
145 *tracing = nested_trace ? (global_tracing = 2, 3) : 2;
149 MonoMethod *method = frame->runtime_method->method;
150 char *mn, *args = dump_args (frame);
151 debug_indent_level++;
153 mn = mono_method_full_name (method, FALSE);
154 g_printerr ("(0x%08x) Entering %s (", mono_thread_internal_current (), mn);
156 g_printerr ("%s)\n", args);
159 if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE)
160 mono_profiler_method_enter (frame->runtime_method->method);
164 #define DEBUG_LEAVE() \
167 args = dump_retval (frame); \
169 mn = mono_method_full_name (frame->runtime_method->method, FALSE); \
170 g_printerr ("(0x%08x) Leaving %s", mono_thread_internal_current (), mn); \
172 g_printerr (" => %s\n", args); \
174 debug_indent_level--; \
175 if (tracing == 3) global_tracing = 0; \
177 if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE) \
178 mono_profiler_method_leave (frame->runtime_method->method);
182 static void debug_enter (MonoInvocation *frame, int *tracing)
185 #define DEBUG_LEAVE()
190 interp_ex_handler (MonoException *ex) {
192 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
196 stack_trace = dump_frame (context->current_frame);
197 ex->stack_trace = mono_string_new (mono_domain_get(), stack_trace);
198 g_free (stack_trace);
199 if (context->current_env == NULL || strcmp(ex->object.vtable->klass->name, "ExecutionEngineException") == 0) {
200 char *strace = mono_string_to_utf8_checked (ex->stack_trace, &error);
201 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
202 fprintf(stderr, "Nothing can catch this exception: ");
203 fprintf(stderr, "%s", ex->object.vtable->klass->name);
204 if (ex->message != NULL) {
205 char *m = mono_string_to_utf8_checked (ex->message, &error);
206 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
207 fprintf(stderr, ": %s", m);
210 fprintf(stderr, "\n%s\n", strace);
212 if (ex->inner_ex != NULL) {
213 ex = (MonoException *)ex->inner_ex;
214 fprintf(stderr, "Inner exception: %s", ex->object.vtable->klass->name);
215 if (ex->message != NULL) {
216 char *m = mono_string_to_utf8_checked (ex->message, &error);
217 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
218 fprintf(stderr, ": %s", m);
221 strace = mono_string_to_utf8_checked (ex->stack_trace, &error);
222 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
223 fprintf(stderr, "\n");
224 fprintf(stderr, "%s\n", strace);
227 /* wait for other threads to also collapse */
228 // Sleep(1000); // TODO: proper sleep
231 context->env_frame->ex = ex;
232 context->search_for_handler = 1;
233 longjmp (*context->current_env, 1);
237 ves_real_abort (int line, MonoMethod *mh,
238 const unsigned short *ip, stackval *stack, stackval *sp)
241 fprintf (stderr, "Execution aborted in method: %s::%s\n", mh->klass->name, mh->name);
242 fprintf (stderr, "Line=%d IP=0x%04lx, Aborted execution\n", line,
243 ip-(const unsigned short *)mono_method_get_header_checked (mh, &error)->code);
244 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
245 g_print ("0x%04x %02x\n",
246 ip-(const unsigned short *)mono_method_get_header_checked (mh, &error)->code, *ip);
247 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
249 printf ("\t[%ld] 0x%08x %0.5f\n", sp-stack, sp[-1].data.i, sp[-1].data.f);
252 #define ves_abort() \
254 ves_real_abort(__LINE__, frame->runtime_method->method, ip, frame->stack, sp); \
255 THROW_EX (mono_get_exception_execution_engine (NULL), ip); \
259 interp_create_remoting_trampoline (MonoDomain *domain, MonoMethod *method, MonoRemotingTarget target, MonoError *error)
261 g_error ("FIXME: use domain and error");
262 // return mono_interp_get_runtime_method (mono_marshal_get_remoting_invoke_for_target (method, target));
265 static mono_mutex_t runtime_method_lookup_section;
268 mono_interp_get_runtime_method (MonoDomain *domain, MonoMethod *method, MonoError *error)
271 mono_error_init (error);
273 mono_os_mutex_lock (&runtime_method_lookup_section);
274 if ((rtm = mono_internal_hash_table_lookup (&domain->jit_code_hash, method))) {
275 mono_os_mutex_unlock (&runtime_method_lookup_section);
278 rtm = mono_mempool_alloc (domain->mp, sizeof (RuntimeMethod));
279 memset (rtm, 0, sizeof (*rtm));
280 rtm->method = method;
281 rtm->param_count = mono_method_signature (method)->param_count;
282 rtm->hasthis = mono_method_signature (method)->hasthis;
283 rtm->valuetype = method->klass->valuetype;
284 mono_internal_hash_table_insert (&domain->jit_code_hash, method, rtm);
285 mono_os_mutex_unlock (&runtime_method_lookup_section);
291 interp_create_trampoline (MonoDomain *domain, MonoMethod *method, MonoError *error)
293 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
294 method = mono_marshal_get_synchronized_wrapper (method);
295 return mono_interp_get_runtime_method (domain, method, error);
298 static inline RuntimeMethod*
299 get_virtual_method (MonoDomain *domain, RuntimeMethod *runtime_method, MonoObject *obj)
301 MonoMethod *m = runtime_method->method;
304 if ((m->flags & METHOD_ATTRIBUTE_FINAL) || !(m->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
305 RuntimeMethod *ret = NULL;
306 if (mono_object_is_transparent_proxy (obj)) {
307 ret = mono_interp_get_runtime_method (domain, mono_marshal_get_remoting_invoke (m), &error);
308 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
309 } else if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
310 ret = mono_interp_get_runtime_method (domain, mono_marshal_get_synchronized_wrapper (m), &error);
311 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
313 ret = runtime_method;
318 int slot = mono_method_get_vtable_slot (m);
319 if (mono_class_is_interface (m->klass)) {
320 g_assert (obj->vtable->klass != m->klass);
321 /* TODO: interface offset lookup is slow, go through IMT instead */
322 slot += mono_class_interface_offset (obj->vtable->klass, m->klass);
324 MonoMethod *virtual_method = obj->vtable->klass->vtable [slot];
325 RuntimeMethod *virtual_runtime_method = mono_interp_get_runtime_method (domain, virtual_method, &error);
326 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
327 return virtual_runtime_method;
331 stackval_from_data (MonoType *type, stackval *result, char *data, gboolean pinvoke)
334 switch (type->type) {
335 case MONO_TYPE_OBJECT:
336 case MONO_TYPE_CLASS:
337 case MONO_TYPE_STRING:
338 case MONO_TYPE_ARRAY:
339 case MONO_TYPE_SZARRAY:
344 result->data.p = *(gpointer*)data;
347 switch (type->type) {
351 result->data.i = *(gint8*)data;
354 case MONO_TYPE_BOOLEAN:
355 result->data.i = *(guint8*)data;
358 result->data.i = *(gint16*)data;
362 result->data.i = *(guint16*)data;
365 result->data.i = *(gint32*)data;
369 result->data.nati = *(mono_i*)data;
372 result->data.p = *(gpointer*)data;
375 result->data.i = *(guint32*)data;
378 result->data.f = *(float*)data;
382 result->data.l = *(gint64*)data;
385 result->data.f = *(double*)data;
387 case MONO_TYPE_STRING:
388 case MONO_TYPE_SZARRAY:
389 case MONO_TYPE_CLASS:
390 case MONO_TYPE_OBJECT:
391 case MONO_TYPE_ARRAY:
392 result->data.p = *(gpointer*)data;
394 case MONO_TYPE_VALUETYPE:
395 if (type->data.klass->enumtype) {
396 stackval_from_data (mono_class_enum_basetype (type->data.klass), result, data, pinvoke);
402 size = mono_class_native_size (type->data.klass, NULL);
404 size = mono_class_value_size (type->data.klass, NULL);
405 memcpy (result->data.vt, data, size);
409 g_warning ("got type 0x%02x", type->type);
410 g_assert_not_reached ();
415 stackval_to_data (MonoType *type, stackval *val, char *data, gboolean pinvoke)
418 gpointer *p = (gpointer*)data;
422 /* printf ("TODAT0 %p\n", data); */
423 switch (type->type) {
426 guint8 *p = (guint8*)data;
430 case MONO_TYPE_BOOLEAN: {
431 guint8 *p = (guint8*)data;
432 *p = (val->data.i != 0);
437 case MONO_TYPE_CHAR: {
438 guint16 *p = (guint16*)data;
443 mono_i *p = (mono_i*)data;
444 /* In theory the value used by stloc should match the local var type
445 but in practice it sometimes doesn't (a int32 gets dup'd and stloc'd into
446 a native int - both by csc and mcs). Not sure what to do about sign extension
447 as it is outside the spec... doing the obvious */
448 *p = (mono_i)val->data.nati;
452 mono_u *p = (mono_u*)data;
454 *p = (mono_u)val->data.nati;
459 gint32 *p = (gint32*)data;
465 gint64 *p = (gint64*)data;
470 float *p = (float*)data;
475 double *p = (double*)data;
479 case MONO_TYPE_STRING:
480 case MONO_TYPE_SZARRAY:
481 case MONO_TYPE_CLASS:
482 case MONO_TYPE_OBJECT:
483 case MONO_TYPE_ARRAY:
484 case MONO_TYPE_PTR: {
485 gpointer *p = (gpointer*)data;
489 case MONO_TYPE_VALUETYPE:
490 if (type->data.klass->enumtype) {
491 stackval_to_data (mono_class_enum_basetype (type->data.klass), val, data, pinvoke);
497 size = mono_class_native_size (type->data.klass, NULL);
499 size = mono_class_value_size (type->data.klass, NULL);
501 memcpy (data, val->data.p, size);
505 g_warning ("got type %x", type->type);
506 g_assert_not_reached ();
511 fill_in_trace (MonoException *exception, MonoInvocation *frame)
513 char *stack_trace = dump_frame (frame);
514 MonoDomain *domain = mono_domain_get();
515 (exception)->stack_trace = mono_string_new (domain, stack_trace);
516 (exception)->trace_ips = get_trace_ips (domain, frame);
517 g_free (stack_trace);
520 #define FILL_IN_TRACE(exception, frame) fill_in_trace(exception, frame)
522 #define THROW_EX(exception,ex_ip) \
524 frame->ip = (ex_ip); \
525 frame->ex = (MonoException*)(exception); \
526 FILL_IN_TRACE(frame->ex, frame); \
527 goto handle_exception; \
531 ves_array_create (MonoDomain *domain, MonoClass *klass, MonoMethodSignature *sig, stackval *values)
534 intptr_t *lower_bounds;
539 lengths = alloca (sizeof (uintptr_t) * klass->rank * 2);
540 for (i = 0; i < sig->param_count; ++i) {
541 lengths [i] = values->data.i;
544 if (klass->rank == sig->param_count) {
545 /* Only lengths provided. */
548 /* lower bounds are first. */
549 lower_bounds = (intptr_t *) lengths;
550 lengths += klass->rank;
552 obj = (MonoObject*) mono_array_new_full_checked (domain, klass, lengths, lower_bounds, &error);
553 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
558 ves_array_set (MonoInvocation *frame)
560 stackval *sp = frame->stack_args;
564 gint32 i, t, pos, esize;
570 ac = o->vtable->klass;
572 g_assert (ac->rank >= 1);
575 if (ao->bounds != NULL) {
576 pos -= ao->bounds [0].lower_bound;
577 for (i = 1; i < ac->rank; i++) {
578 if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >=
579 ao->bounds [i].length) {
580 frame->ex = mono_get_exception_index_out_of_range ();
581 FILL_IN_TRACE(frame->ex, frame);
584 pos = pos*ao->bounds [i].length + sp [i].data.i -
585 ao->bounds [i].lower_bound;
587 } else if (pos >= ao->max_length) {
588 frame->ex = mono_get_exception_index_out_of_range ();
589 FILL_IN_TRACE(frame->ex, frame);
593 if (sp [ac->rank].data.p && !mono_object_class (o)->element_class->valuetype) {
595 MonoObject *isinst = mono_object_isinst_checked (sp [ac->rank].data.p, mono_object_class (o)->element_class, &error);
596 mono_error_cleanup (&error);
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;
727 static MonoPIFunc mono_interp_enter_icall_trampoline = NULL;
729 struct _MethodArguments {
737 typedef struct _MethodArguments MethodArguments;
739 // TODO: this function is also arch dependent (register width).
740 static MethodArguments* build_args_from_sig (MonoMethodSignature *sig, MonoInvocation *frame)
742 // TODO: don't malloc this data structure.
743 MethodArguments *margs = g_malloc0 (sizeof (MethodArguments));
748 for (int i = 0; i < sig->param_count; i++) {
749 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
751 case MONO_TYPE_BOOLEAN:
762 case MONO_TYPE_SZARRAY:
763 case MONO_TYPE_CLASS:
764 case MONO_TYPE_OBJECT:
765 case MONO_TYPE_STRING:
770 g_error ("build_args_from_sig: not implemented yet (1): 0x%x\n", ptype);
775 margs->iargs = g_malloc0 (sizeof (gpointer) * margs->ilen);
778 g_error ("build_args_from_sig: TODO, more than two iregs: %d\n", margs->ilen);
781 g_error ("build_args_from_sig: TODO, allocate floats: %d\n", margs->flen);
787 margs->iargs [0] = frame->obj;
791 for (int i = 0; i < sig->param_count; i++) {
792 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
794 case MONO_TYPE_BOOLEAN:
805 case MONO_TYPE_SZARRAY:
806 case MONO_TYPE_CLASS:
807 case MONO_TYPE_OBJECT:
808 case MONO_TYPE_STRING:
810 margs->iargs [int_i] = frame->stack_args [i].data.p;
812 g_printerr ("build_args_from_sig: margs->iargs[%d]: %p (frame @ %d)\n", int_i, margs->iargs[int_i], i);
817 g_error ("build_args_from_sig: not implemented yet (2): 0x%x\n", ptype);
821 if (sig->ret->type != MONO_TYPE_VOID) {
822 margs->retval = &(frame->retval->data.p);
824 margs->retval = NULL;
831 ves_pinvoke_method (MonoInvocation *frame, MonoMethodSignature *sig, MonoFuncV addr, gboolean string_ctor, ThreadContext *context)
834 MonoInvocation *old_frame = context->current_frame;
835 MonoInvocation *old_env_frame = context->env_frame;
836 jmp_buf *old_env = context->current_env;
839 context->current_frame = old_frame;
840 context->env_frame = old_env_frame;
841 context->current_env = old_env;
842 context->managed_code = 1;
847 context->env_frame = frame;
848 context->current_env = &env;
850 g_assert (!frame->runtime_method);
851 if (!mono_interp_enter_icall_trampoline) {
853 mono_interp_enter_icall_trampoline = mono_arch_get_enter_icall_trampoline (&info);
855 // mono_tramp_info_register (info, NULL);
858 MethodArguments *margs = build_args_from_sig (sig, frame);
860 g_printerr ("ICALL: mono_interp_enter_icall_trampoline = %p, addr = %p\n", mono_interp_enter_icall_trampoline, addr);
861 g_printerr ("margs(out): ilen=%d, flen=%d\n", margs->ilen, margs->flen);
864 context->current_frame = frame;
865 context->managed_code = 0;
867 mono_interp_enter_icall_trampoline (addr, margs);
869 context->managed_code = 1;
870 /* domain can only be changed by native code */
871 context->domain = mono_domain_get ();
873 if (*abort_requested)
874 mono_thread_interruption_checkpoint ();
876 if (!MONO_TYPE_ISSTRUCT (sig->ret))
877 stackval_from_data (sig->ret, frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
879 context->current_frame = old_frame;
880 context->env_frame = old_env_frame;
881 context->current_env = old_env;
883 g_free (margs->iargs);
884 g_free (margs->fargs);
889 interp_delegate_ctor (MonoDomain *domain, MonoObject *this, MonoObject *target, RuntimeMethod *runtime_method)
891 MonoDelegate *delegate = (MonoDelegate *)this;
894 delegate->method_info = mono_method_get_object_checked (domain, runtime_method->method, NULL, &error);
895 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
896 delegate->target = target;
898 if (target && mono_object_is_transparent_proxy (target)) {
899 MonoMethod *method = mono_marshal_get_remoting_invoke (runtime_method->method);
900 delegate->method_ptr = mono_interp_get_runtime_method (domain, method, &error);
901 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
903 delegate->method_ptr = runtime_method;
908 mono_interp_ftnptr_to_delegate (MonoClass *klass, gpointer ftn)
912 MonoDomain *domain = mono_domain_get ();
915 d = (MonoDelegate*)mono_object_new_checked (domain, klass, &error);
916 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
918 ji = mono_jit_info_table_find (domain, ftn);
920 mono_raise_exception (mono_get_exception_argument ("", "Function pointer was not created by a Delegate."));
922 /* FIXME: discard the wrapper and call the original method */
923 interp_delegate_ctor (domain, (MonoObject*)d, NULL, mono_interp_get_runtime_method (domain, ji->d.method, &error));
924 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
931 * runtime specifies that the implementation of the method is automatically
932 * provided by the runtime and is primarily used for the methods of delegates.
935 ves_runtime_method (MonoInvocation *frame, ThreadContext *context)
937 MonoMethod *method = frame->runtime_method->method;
938 const char *name = method->name;
939 MonoObject *obj = (MonoObject*)frame->obj;
940 MonoObject *isinst_obj;
943 mono_class_init (method->klass);
945 isinst_obj = mono_object_isinst_checked (obj, mono_defaults.multicastdelegate_class, &error);
946 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
947 if (obj && isinst_obj) {
948 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
949 interp_delegate_ctor (context->domain, obj, frame->stack_args[0].data.p, frame->stack_args[1].data.p);
954 isinst_obj = mono_object_isinst_checked (obj, mono_defaults.array_class, &error);
955 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
956 if (obj && isinst_obj) {
957 if (*name == 'S' && (strcmp (name, "Set") == 0)) {
958 ves_array_set (frame);
961 if (*name == 'G' && (strcmp (name, "Get") == 0)) {
962 ves_array_get (frame);
965 if (*name == 'A' && (strcmp (name, "Address") == 0)) {
966 ves_array_element_address (frame);
971 g_error ("Don't know how to exec runtime method %s.%s::%s",
972 method->klass->name_space, method->klass->name,
977 dump_stack (stackval *stack, stackval *sp)
980 GString *str = g_string_new ("");
983 return g_string_free (str, FALSE);
986 g_string_append_printf (str, "[%lld/0x%0llx] ", s->data.l, s->data.l);
989 return g_string_free (str, FALSE);
993 dump_stackval (GString *str, stackval *s, MonoType *type)
995 switch (type->type) {
1002 case MONO_TYPE_CHAR:
1003 case MONO_TYPE_BOOLEAN:
1004 g_string_append_printf (str, "[%d] ", s->data.i);
1006 case MONO_TYPE_STRING:
1007 case MONO_TYPE_SZARRAY:
1008 case MONO_TYPE_CLASS:
1009 case MONO_TYPE_OBJECT:
1010 case MONO_TYPE_ARRAY:
1014 g_string_append_printf (str, "[%p] ", s->data.p);
1016 case MONO_TYPE_VALUETYPE:
1017 if (type->data.klass->enumtype)
1018 g_string_append_printf (str, "[%d] ", s->data.i);
1020 g_string_append_printf (str, "[vt:%p] ", s->data.p);
1024 g_string_append_printf (str, "[%g] ", s->data.f);
1029 g_string_append_printf (str, "[%lld/0x%0llx] ", s->data.l, s->data.l);
1035 dump_args (MonoInvocation *inv)
1037 GString *str = g_string_new ("");
1039 MonoMethodSignature *signature = mono_method_signature (inv->runtime_method->method);
1041 if (signature->param_count == 0)
1042 return g_string_free (str, FALSE);
1044 if (signature->hasthis)
1045 g_string_append_printf (str, "%p ", inv->obj);
1047 for (i = 0; i < signature->param_count; ++i)
1048 dump_stackval (str, inv->stack_args + i, signature->params [i]);
1050 return g_string_free (str, FALSE);
1054 dump_retval (MonoInvocation *inv)
1056 GString *str = g_string_new ("");
1057 MonoType *ret = mono_method_signature (inv->runtime_method->method)->ret;
1059 if (ret->type != MONO_TYPE_VOID)
1060 dump_stackval (str, inv->retval, ret);
1062 return g_string_free (str, FALSE);
1066 dump_frame (MonoInvocation *inv)
1068 GString *str = g_string_new ("");
1073 for (i = 0; inv; inv = inv->parent) {
1074 if (inv->runtime_method != NULL) {
1075 MonoMethod *method = inv->runtime_method->method;
1079 const char * opname = "";
1081 gchar *source = NULL;
1085 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
1086 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) == 0) {
1087 MonoMethodHeader *hd = mono_method_get_header_checked (method, &error);
1088 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1092 opname = mono_interp_opname [*inv->ip];
1093 codep = inv->ip - inv->runtime_method->code;
1094 source = g_strdup_printf ("%s:%d // (TODO: proper stacktrace)", method->name, codep);
1099 MonoDebugSourceLocation *minfo = mono_debug_lookup_method (method);
1100 source = mono_debug_method_lookup_location (minfo, codep);
1104 args = dump_args (inv);
1105 name = mono_method_full_name (method, TRUE);
1107 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s) at %s\n", i, codep, opname, name, args, source);
1109 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s)\n", i, codep, opname, name, args);
1116 return g_string_free (str, FALSE);
1120 get_trace_ips (MonoDomain *domain, MonoInvocation *top)
1124 MonoInvocation *inv;
1127 for (i = 0, inv = top; inv; inv = inv->parent)
1128 if (inv->runtime_method != NULL)
1131 res = mono_array_new_checked (domain, mono_defaults.int_class, 2 * i, &error);
1132 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1134 for (i = 0, inv = top; inv; inv = inv->parent)
1135 if (inv->runtime_method != NULL) {
1136 mono_array_set (res, gpointer, i, inv->runtime_method);
1138 mono_array_set (res, gpointer, i, (gpointer)inv->ip);
1146 #define MYGUINT64_MAX 18446744073709551615ULL
1147 #define MYGINT64_MAX 9223372036854775807LL
1148 #define MYGINT64_MIN (-MYGINT64_MAX -1LL)
1150 #define MYGUINT32_MAX 4294967295U
1151 #define MYGINT32_MAX 2147483647
1152 #define MYGINT32_MIN (-MYGINT32_MAX -1)
1154 #define CHECK_ADD_OVERFLOW(a,b) \
1155 (gint32)(b) >= 0 ? (gint32)(MYGINT32_MAX) - (gint32)(b) < (gint32)(a) ? -1 : 0 \
1156 : (gint32)(MYGINT32_MIN) - (gint32)(b) > (gint32)(a) ? +1 : 0
1158 #define CHECK_SUB_OVERFLOW(a,b) \
1159 (gint32)(b) < 0 ? (gint32)(MYGINT32_MAX) + (gint32)(b) < (gint32)(a) ? -1 : 0 \
1160 : (gint32)(MYGINT32_MIN) + (gint32)(b) > (gint32)(a) ? +1 : 0
1162 #define CHECK_ADD_OVERFLOW_UN(a,b) \
1163 (guint32)(MYGUINT32_MAX) - (guint32)(b) < (guint32)(a) ? -1 : 0
1165 #define CHECK_SUB_OVERFLOW_UN(a,b) \
1166 (guint32)(a) < (guint32)(b) ? -1 : 0
1168 #define CHECK_ADD_OVERFLOW64(a,b) \
1169 (gint64)(b) >= 0 ? (gint64)(MYGINT64_MAX) - (gint64)(b) < (gint64)(a) ? -1 : 0 \
1170 : (gint64)(MYGINT64_MIN) - (gint64)(b) > (gint64)(a) ? +1 : 0
1172 #define CHECK_SUB_OVERFLOW64(a,b) \
1173 (gint64)(b) < 0 ? (gint64)(MYGINT64_MAX) + (gint64)(b) < (gint64)(a) ? -1 : 0 \
1174 : (gint64)(MYGINT64_MIN) + (gint64)(b) > (gint64)(a) ? +1 : 0
1176 #define CHECK_ADD_OVERFLOW64_UN(a,b) \
1177 (guint64)(MYGUINT64_MAX) - (guint64)(b) < (guint64)(a) ? -1 : 0
1179 #define CHECK_SUB_OVERFLOW64_UN(a,b) \
1180 (guint64)(a) < (guint64)(b) ? -1 : 0
1182 #if SIZEOF_VOID_P == 4
1183 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW(a,b)
1184 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW_UN(a,b)
1186 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW64(a,b)
1187 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW64_UN(a,b)
1190 /* Resolves to TRUE if the operands would overflow */
1191 #define CHECK_MUL_OVERFLOW(a,b) \
1192 ((gint32)(a) == 0) || ((gint32)(b) == 0) ? 0 : \
1193 (((gint32)(a) > 0) && ((gint32)(b) == -1)) ? FALSE : \
1194 (((gint32)(a) < 0) && ((gint32)(b) == -1)) ? (a == - MYGINT32_MAX) : \
1195 (((gint32)(a) > 0) && ((gint32)(b) > 0)) ? (gint32)(a) > ((MYGINT32_MAX) / (gint32)(b)) : \
1196 (((gint32)(a) > 0) && ((gint32)(b) < 0)) ? (gint32)(a) > ((MYGINT32_MIN) / (gint32)(b)) : \
1197 (((gint32)(a) < 0) && ((gint32)(b) > 0)) ? (gint32)(a) < ((MYGINT32_MIN) / (gint32)(b)) : \
1198 (gint32)(a) < ((MYGINT32_MAX) / (gint32)(b))
1200 #define CHECK_MUL_OVERFLOW_UN(a,b) \
1201 ((guint32)(a) == 0) || ((guint32)(b) == 0) ? 0 : \
1202 (guint32)(b) > ((MYGUINT32_MAX) / (guint32)(a))
1204 #define CHECK_MUL_OVERFLOW64(a,b) \
1205 ((gint64)(a) == 0) || ((gint64)(b) == 0) ? 0 : \
1206 (((gint64)(a) > 0) && ((gint64)(b) == -1)) ? FALSE : \
1207 (((gint64)(a) < 0) && ((gint64)(b) == -1)) ? (a == - MYGINT64_MAX) : \
1208 (((gint64)(a) > 0) && ((gint64)(b) > 0)) ? (gint64)(a) > ((MYGINT64_MAX) / (gint64)(b)) : \
1209 (((gint64)(a) > 0) && ((gint64)(b) < 0)) ? (gint64)(a) > ((MYGINT64_MIN) / (gint64)(b)) : \
1210 (((gint64)(a) < 0) && ((gint64)(b) > 0)) ? (gint64)(a) < ((MYGINT64_MIN) / (gint64)(b)) : \
1211 (gint64)(a) < ((MYGINT64_MAX) / (gint64)(b))
1213 #define CHECK_MUL_OVERFLOW64_UN(a,b) \
1214 ((guint64)(a) == 0) || ((guint64)(b) == 0) ? 0 : \
1215 (guint64)(b) > ((MYGUINT64_MAX) / (guint64)(a))
1217 #if SIZEOF_VOID_P == 4
1218 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW(a,b)
1219 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW_UN(a,b)
1221 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW64(a,b)
1222 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW64_UN(a,b)
1226 interp_mono_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc, MonoError *error)
1228 MonoInvocation frame;
1229 ThreadContext * volatile context = mono_native_tls_get_value (thread_context_id);
1230 MonoObject *retval = NULL;
1231 MonoMethodSignature *sig = mono_method_signature (method);
1232 MonoClass *klass = mono_class_from_mono_type (sig->ret);
1233 int i, type, isobject = 0;
1236 stackval *args = alloca (sizeof (stackval) * sig->param_count);
1237 ThreadContext context_struct;
1238 MonoInvocation *old_frame = NULL;
1241 mono_error_init (error);
1246 if (context != &context_struct) {
1247 context->domain = mono_domain_get ();
1248 context->current_frame = old_frame;
1249 context->managed_code = 0;
1251 mono_native_tls_set_value (thread_context_id, NULL);
1253 *exc = (MonoObject *)frame.ex;
1257 if (context == NULL) {
1258 context = &context_struct;
1259 context_struct.base_frame = &frame;
1260 context_struct.current_frame = NULL;
1261 context_struct.env_frame = &frame;
1262 context_struct.current_env = &env;
1263 context_struct.search_for_handler = 0;
1264 context_struct.managed_code = 0;
1265 mono_native_tls_set_value (thread_context_id, context);
1268 old_frame = context->current_frame;
1270 context->domain = mono_domain_get ();
1272 switch (sig->ret->type) {
1273 case MONO_TYPE_VOID:
1275 case MONO_TYPE_STRING:
1276 case MONO_TYPE_OBJECT:
1277 case MONO_TYPE_CLASS:
1278 case MONO_TYPE_ARRAY:
1279 case MONO_TYPE_SZARRAY:
1282 case MONO_TYPE_VALUETYPE:
1283 retval = mono_object_new_checked (context->domain, klass, error);
1284 ret = ((char*)retval) + sizeof (MonoObject);
1285 if (!sig->ret->data.klass->enumtype)
1286 result.data.vt = ret;
1289 retval = mono_object_new_checked (context->domain, klass, error);
1290 ret = ((char*)retval) + sizeof (MonoObject);
1294 for (i = 0; i < sig->param_count; ++i) {
1295 if (sig->params [i]->byref) {
1296 args [i].data.p = params [i];
1299 type = sig->params [i]->type;
1304 case MONO_TYPE_BOOLEAN:
1305 args [i].data.i = *(MonoBoolean*)params [i];
1309 case MONO_TYPE_CHAR:
1310 args [i].data.i = *(gint16*)params [i];
1312 #if SIZEOF_VOID_P == 4
1313 case MONO_TYPE_U: /* use VAL_POINTER? */
1318 args [i].data.i = *(gint32*)params [i];
1320 #if SIZEOF_VOID_P == 8
1326 args [i].data.l = *(gint64*)params [i];
1328 case MONO_TYPE_VALUETYPE:
1329 if (sig->params [i]->data.klass->enumtype) {
1330 type = mono_class_enum_basetype (sig->params [i]->data.klass)->type;
1333 args [i].data.p = params [i];
1336 case MONO_TYPE_STRING:
1337 case MONO_TYPE_CLASS:
1338 case MONO_TYPE_ARRAY:
1339 case MONO_TYPE_SZARRAY:
1340 case MONO_TYPE_OBJECT:
1341 args [i].data.p = params [i];
1344 g_error ("type 0x%x not handled in runtime invoke", sig->params [i]->type);
1348 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
1349 method = mono_marshal_get_native_wrapper (method, FALSE, FALSE);
1350 INIT_FRAME(&frame,context->current_frame,obj,args,&result,mono_get_root_domain (),method,error);
1352 frame.invoke_trap = 1;
1353 context->managed_code = 1;
1354 ves_exec_method_with_context (&frame, context);
1355 context->managed_code = 0;
1356 if (context == &context_struct)
1357 mono_native_tls_set_value (thread_context_id, NULL);
1359 context->current_frame = old_frame;
1360 if (frame.ex != NULL) {
1362 *exc = (MonoObject*) frame.ex;
1365 if (context->current_env != NULL) {
1366 context->env_frame->ex = frame.ex;
1367 longjmp(*context->current_env, 1);
1370 printf("dropped exception...\n");
1372 if (sig->ret->type == MONO_TYPE_VOID && !method->string_ctor)
1374 if (isobject || method->string_ctor)
1375 return result.data.p;
1376 stackval_to_data (sig->ret, &result, ret, sig->pinvoke);
1381 do_icall (ThreadContext *context, int op, stackval *sp, gpointer ptr)
1383 MonoInvocation *old_frame = context->current_frame;
1384 MonoInvocation *old_env_frame = context->env_frame;
1385 jmp_buf *old_env = context->current_env;
1389 context->current_frame = old_frame;
1390 context->env_frame = old_env_frame;
1391 context->current_env = old_env;
1392 context->managed_code = 1;
1396 context->env_frame = context->current_frame;
1397 context->current_env = &env;
1398 context->managed_code = 0;
1401 case MINT_ICALL_V_V: {
1402 void (*func)() = ptr;
1406 case MINT_ICALL_V_P: {
1407 gpointer (*func)() = ptr;
1409 sp [-1].data.p = func ();
1412 case MINT_ICALL_P_V: {
1413 void (*func)(gpointer) = ptr;
1414 func (sp [-1].data.p);
1418 case MINT_ICALL_P_P: {
1419 gpointer (*func)(gpointer) = ptr;
1420 sp [-1].data.p = func (sp [-1].data.p);
1423 case MINT_ICALL_PP_V: {
1424 void (*func)(gpointer,gpointer) = ptr;
1426 func (sp [0].data.p, sp [1].data.p);
1429 case MINT_ICALL_PI_V: {
1430 void (*func)(gpointer,int) = ptr;
1432 func (sp [0].data.p, sp [1].data.i);
1435 case MINT_ICALL_PP_P: {
1436 gpointer (*func)(gpointer,gpointer) = ptr;
1438 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.p);
1441 case MINT_ICALL_PI_P: {
1442 gpointer (*func)(gpointer,int) = ptr;
1444 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.i);
1447 case MINT_ICALL_PPP_V: {
1448 void (*func)(gpointer,gpointer,gpointer) = ptr;
1450 func (sp [0].data.p, sp [1].data.p, sp [2].data.p);
1453 case MINT_ICALL_PPI_V: {
1454 void (*func)(gpointer,gpointer,int) = ptr;
1456 func (sp [0].data.p, sp [1].data.p, sp [2].data.i);
1460 g_assert_not_reached ();
1463 context->env_frame = old_env_frame;
1464 context->current_env = old_env;
1469 static mono_mutex_t create_method_pointer_mutex;
1471 static GHashTable *method_pointer_hash = NULL;
1474 mono_create_method_pointer (MonoMethod *method, MonoError *error)
1478 mono_error_init (error);
1480 mono_os_mutex_lock (&create_method_pointer_mutex);
1481 if (!method_pointer_hash) {
1482 // FIXME: is registering method table as GC root really necessary?
1483 // MONO_GC_REGISTER_ROOT_FIXED (method_pointer_hash);
1484 method_pointer_hash = g_hash_table_new (NULL, NULL);
1486 addr = g_hash_table_lookup (method_pointer_hash, method);
1488 mono_os_mutex_unlock (&create_method_pointer_mutex);
1493 * If it is a static P/Invoke method, we can just return the pointer
1494 * to the method implementation.
1496 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL && ((MonoMethodPInvoke*) method)->addr) {
1497 ji = g_new0 (MonoJitInfo, 1);
1498 ji->d.method = method;
1500 ji->code_start = addr = ((MonoMethodPInvoke*) method)->addr;
1502 mono_jit_info_table_add (mono_get_root_domain (), ji);
1505 g_error ("FIXME: not available? figure out new API");
1507 addr = mono_arch_create_method_pointer (method);
1511 g_hash_table_insert (method_pointer_hash, method, addr);
1512 mono_os_mutex_unlock (&create_method_pointer_mutex);
1518 static int opcode_counts[512];
1520 #define COUNT_OP(op) opcode_counts[op]++
1522 #define COUNT_OP(op)
1526 #define DUMP_INSTR() \
1527 if (tracing > 1) { \
1529 if (sp > frame->stack) { \
1530 ins = dump_stack (frame->stack, sp); \
1532 ins = g_strdup (""); \
1536 g_print ("(%u) ", mono_thread_internal_current ()); \
1537 mono_interp_dis_mintop(rtm->code, ip); \
1538 g_print ("\t%d:%s\n", vt_sp - vtalloc, ins); \
1542 #define DUMP_INSTR()
1546 #define USE_COMPUTED_GOTO 1
1548 #if USE_COMPUTED_GOTO
1549 #define MINT_IN_SWITCH(op) COUNT_OP(op); goto *in_labels[op];
1550 #define MINT_IN_CASE(x) LAB_ ## x:
1552 #define MINT_IN_BREAK if (tracing > 1) goto main_loop; else { COUNT_OP(*ip); goto *in_labels[*ip]; }
1554 #define MINT_IN_BREAK { COUNT_OP(*ip); goto *in_labels[*ip]; }
1556 #define MINT_IN_DEFAULT mint_default: if (0) goto mint_default; /* make gcc shut up */
1558 #define MINT_IN_SWITCH(op) switch (op)
1559 #define MINT_IN_CASE(x) case x:
1560 #define MINT_IN_BREAK break
1561 #define MINT_IN_DEFAULT default:
1565 * Defining this causes register allocation errors in some versions of gcc:
1566 * error: unable to find a register to spill in class `SIREG'
1568 /* #define MINT_USE_DEDICATED_IP_REG */
1571 ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context)
1573 MonoInvocation child_frame;
1574 GSList *finally_ips = NULL;
1575 const unsigned short *endfinally_ip = NULL;
1576 #if defined(__GNUC__) && defined (i386) && defined (MINT_USE_DEDICATED_IP_REG)
1577 register const unsigned short *ip asm ("%esi");
1579 register const unsigned short *ip;
1581 register stackval *sp;
1584 gint tracing = global_tracing;
1585 unsigned char *vtalloc;
1588 unsigned char *vt_sp;
1589 unsigned char *locals;
1591 MonoObject *o = NULL;
1593 #if USE_COMPUTED_GOTO
1594 static void *in_labels[] = {
1595 #define OPDEF(a,b,c,d) \
1597 #include "mintops.def"
1602 frame->ex_handler = NULL;
1604 context->current_frame = frame;
1607 debug_enter (frame, &tracing);
1610 if (!frame->runtime_method->transformed) {
1611 context->managed_code = 0;
1613 char *mn = mono_method_full_name (frame->runtime_method->method, FALSE);
1614 g_printerr ("(0x%08x) Transforming %s\n", mono_thread_internal_current (), mn);
1617 frame->ex = mono_interp_transform_method (frame->runtime_method, context);
1618 context->managed_code = 1;
1626 rtm = frame->runtime_method;
1627 frame->args = alloca (rtm->alloca_size);
1628 sp = frame->stack = (stackval *)((char *)frame->args + rtm->args_size);
1631 memset(sp, 0, rtm->stack_size);
1633 vt_sp = (unsigned char *) sp + rtm->stack_size;
1637 locals = (unsigned char *) vt_sp + rtm->vt_stack_size;
1639 child_frame.parent = frame;
1645 * using while (ip < end) may result in a 15% performance drop,
1646 * but it may be useful for debug
1650 /* g_assert (sp >= frame->stack); */
1651 /* g_assert(vt_sp - vtalloc <= rtm->vt_stack_size); */
1653 MINT_IN_SWITCH (*ip) {
1654 MINT_IN_CASE(MINT_INITLOCALS)
1655 memset (locals, 0, rtm->locals_size);
1658 MINT_IN_CASE(MINT_NOP)
1661 MINT_IN_CASE(MINT_BREAK)
1663 G_BREAKPOINT (); /* this is not portable... */
1665 MINT_IN_CASE(MINT_LDNULL)
1670 MINT_IN_CASE(MINT_VTRESULT) {
1671 int ret_size = * (guint16 *)(ip + 1);
1672 unsigned char *ret_vt_sp = vt_sp;
1673 vt_sp -= READ32(ip + 2);
1675 memmove (vt_sp, ret_vt_sp, ret_size);
1676 vt_sp += (ret_size + 7) & ~7;
1681 #define LDC(n) do { sp->data.i = (n); ++ip; ++sp; } while (0)
1682 MINT_IN_CASE(MINT_LDC_I4_M1)
1685 MINT_IN_CASE(MINT_LDC_I4_0)
1688 MINT_IN_CASE(MINT_LDC_I4_1)
1691 MINT_IN_CASE(MINT_LDC_I4_2)
1694 MINT_IN_CASE(MINT_LDC_I4_3)
1697 MINT_IN_CASE(MINT_LDC_I4_4)
1700 MINT_IN_CASE(MINT_LDC_I4_5)
1703 MINT_IN_CASE(MINT_LDC_I4_6)
1706 MINT_IN_CASE(MINT_LDC_I4_7)
1709 MINT_IN_CASE(MINT_LDC_I4_8)
1712 MINT_IN_CASE(MINT_LDC_I4_S)
1713 sp->data.i = *(const short *)(ip + 1);
1717 MINT_IN_CASE(MINT_LDC_I4)
1719 sp->data.i = READ32 (ip);
1723 MINT_IN_CASE(MINT_LDC_I8)
1725 sp->data.l = READ64 (ip);
1729 MINT_IN_CASE(MINT_LDC_R4) {
1733 sp->data.f = * (float *)&val;
1738 MINT_IN_CASE(MINT_LDC_R8)
1739 sp->data.l = READ64 (ip + 1); /* note union usage */
1743 MINT_IN_CASE(MINT_DUP)
1748 MINT_IN_CASE(MINT_DUP_VT)
1749 i32 = READ32 (ip + 1);
1751 memcpy(sp->data.p, sp [-1].data.p, i32);
1752 vt_sp += (i32 + 7) & ~7;
1756 MINT_IN_CASE(MINT_POP)
1760 MINT_IN_CASE(MINT_JMP) {
1761 RuntimeMethod *new_method = rtm->data_items [* (guint16 *)(ip + 1)];
1762 if (!new_method->transformed) {
1764 frame->ex = mono_interp_transform_method (new_method, context);
1769 if (new_method->alloca_size > rtm->alloca_size)
1770 g_error ("MINT_JMP to method which needs more stack space (%d > %d)", new_method->alloca_size, rtm->alloca_size);
1771 rtm = frame->runtime_method = new_method;
1772 vt_sp = (unsigned char *) sp + rtm->stack_size;
1776 locals = vt_sp + rtm->vt_stack_size;
1777 ip = rtm->new_body_start; /* bypass storing input args from callers frame */
1780 MINT_IN_CASE(MINT_CALLI) {
1781 MonoMethodSignature *csignature;
1782 stackval *endsp = sp;
1786 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1790 child_frame.runtime_method = sp->data.p;
1793 child_frame.retval = sp;
1794 /* decrement by the actual number of args */
1795 sp -= csignature->param_count;
1796 child_frame.stack_args = sp;
1797 if (csignature->hasthis) {
1799 child_frame.obj = sp->data.p;
1801 child_frame.obj = NULL;
1803 if (csignature->hasthis && mono_object_is_transparent_proxy (child_frame.obj)) {
1804 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1805 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1806 } else if (child_frame.runtime_method->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
1807 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_native_wrapper (child_frame.runtime_method->method, FALSE, FALSE), &error);
1808 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1811 ves_exec_method_with_context (&child_frame, context);
1813 context->current_frame = frame;
1815 if (child_frame.ex) {
1817 * An exception occurred, need to run finally, fault and catch handlers..
1819 frame->ex = child_frame.ex;
1820 goto handle_finally;
1823 /* need to handle typedbyref ... */
1824 if (csignature->ret->type != MONO_TYPE_VOID) {
1830 MINT_IN_CASE(MINT_CALLI_NAT) {
1831 MonoMethodSignature *csignature;
1832 stackval *endsp = sp;
1833 unsigned char *code = NULL;
1837 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1842 child_frame.runtime_method = NULL;
1845 child_frame.retval = sp;
1846 /* decrement by the actual number of args */
1847 sp -= csignature->param_count;
1848 child_frame.stack_args = sp;
1849 if (csignature->hasthis) {
1851 child_frame.obj = sp->data.p;
1853 child_frame.obj = NULL;
1855 ves_pinvoke_method (&child_frame, csignature, (MonoFuncV) code, FALSE, context);
1857 context->current_frame = frame;
1859 if (child_frame.ex) {
1861 * An exception occurred, need to run finally, fault and catch handlers..
1863 frame->ex = child_frame.ex;
1864 if (context->search_for_handler) {
1865 context->search_for_handler = 0;
1866 goto handle_exception;
1868 goto handle_finally;
1871 /* need to handle typedbyref ... */
1872 if (csignature->ret->type != MONO_TYPE_VOID) {
1878 MINT_IN_CASE(MINT_CALL) {
1879 stackval *endsp = sp;
1883 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1886 child_frame.retval = sp;
1887 /* decrement by the actual number of args */
1888 sp -= child_frame.runtime_method->param_count;
1889 child_frame.stack_args = sp;
1890 if (child_frame.runtime_method->hasthis) {
1892 child_frame.obj = sp->data.p;
1894 child_frame.obj = NULL;
1896 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->valuetype && mono_object_is_transparent_proxy (child_frame.obj)) {
1897 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1898 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1900 ves_exec_method_with_context (&child_frame, context);
1902 context->current_frame = frame;
1904 if (child_frame.ex) {
1906 * An exception occurred, need to run finally, fault and catch handlers..
1908 frame->ex = child_frame.ex;
1909 goto handle_finally;
1912 /* need to handle typedbyref ... */
1917 MINT_IN_CASE(MINT_VCALL) {
1920 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1924 child_frame.retval = sp;
1925 /* decrement by the actual number of args */
1926 sp -= child_frame.runtime_method->param_count;
1927 child_frame.stack_args = sp;
1928 if (child_frame.runtime_method->hasthis) {
1930 child_frame.obj = sp->data.p;
1932 child_frame.obj = NULL;
1935 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->valuetype && mono_object_is_transparent_proxy (child_frame.obj)) {
1936 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1937 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1940 ves_exec_method_with_context (&child_frame, context);
1942 context->current_frame = frame;
1944 if (child_frame.ex) {
1946 * An exception occurred, need to run finally, fault and catch handlers..
1948 frame->ex = child_frame.ex;
1949 goto handle_finally;
1953 MINT_IN_CASE(MINT_CALLVIRT) {
1954 stackval *endsp = sp;
1955 MonoObject *this_arg;
1960 token = * (unsigned short *)(ip + 1);
1962 child_frame.runtime_method = rtm->data_items [token];
1964 child_frame.retval = sp;
1966 /* decrement by the actual number of args */
1967 sp -= child_frame.runtime_method->param_count;
1968 child_frame.stack_args = sp;
1970 child_frame.obj = this_arg = sp->data.p;
1972 THROW_EX (mono_get_exception_null_reference(), ip - 2);
1973 child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
1975 if (this_arg->vtable->klass->valuetype && child_frame.runtime_method->valuetype) {
1976 child_frame.obj = (char *)this_arg + sizeof(MonoObject);
1979 ves_exec_method_with_context (&child_frame, context);
1981 context->current_frame = frame;
1983 if (child_frame.ex) {
1985 * An exception occurred, need to run finally, fault and catch handlers..
1987 frame->ex = child_frame.ex;
1988 if (context->search_for_handler) {
1989 context->search_for_handler = 0;
1990 goto handle_exception;
1992 goto handle_finally;
1995 /* need to handle typedbyref ... */
2000 MINT_IN_CASE(MINT_VCALLVIRT) {
2001 MonoObject *this_arg;
2006 token = * (unsigned short *)(ip + 1);
2008 child_frame.runtime_method = rtm->data_items [token];
2010 child_frame.retval = sp;
2012 /* decrement by the actual number of args */
2013 sp -= child_frame.runtime_method->param_count;
2014 child_frame.stack_args = sp;
2016 child_frame.obj = this_arg = sp->data.p;
2018 THROW_EX (mono_get_exception_null_reference(), ip - 2);
2019 child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
2021 if (this_arg->vtable->klass->valuetype && child_frame.runtime_method->valuetype) {
2022 child_frame.obj = (char *)this_arg + sizeof(MonoObject);
2025 ves_exec_method_with_context (&child_frame, context);
2027 context->current_frame = frame;
2029 if (child_frame.ex) {
2031 * An exception occurred, need to run finally, fault and catch handlers..
2033 frame->ex = child_frame.ex;
2034 if (context->search_for_handler) {
2035 context->search_for_handler = 0;
2036 goto handle_exception;
2038 goto handle_finally;
2042 MINT_IN_CASE(MINT_CALLRUN)
2043 ves_runtime_method (frame, context);
2046 goto handle_exception;
2049 MINT_IN_CASE(MINT_RET)
2051 *frame->retval = *sp;
2052 if (sp > frame->stack)
2053 g_warning ("ret: more values on stack: %d", sp-frame->stack);
2055 MINT_IN_CASE(MINT_RET_VOID)
2056 if (sp > frame->stack)
2057 g_warning ("ret.void: more values on stack: %d", sp-frame->stack);
2059 MINT_IN_CASE(MINT_RET_VT)
2060 i32 = READ32(ip + 1);
2062 memcpy(frame->retval->data.p, sp->data.p, i32);
2063 if (sp > frame->stack)
2064 g_warning ("ret.vt: more values on stack: %d", sp-frame->stack);
2066 MINT_IN_CASE(MINT_BR_S)
2067 ip += (short) *(ip + 1);
2069 MINT_IN_CASE(MINT_BR)
2070 ip += (gint32) READ32(ip + 1);
2072 #define ZEROP_S(datamem, op) \
2074 if (sp->data.datamem op 0) \
2075 ip += * (gint16 *)(ip + 1); \
2079 #define ZEROP(datamem, op) \
2081 if (sp->data.datamem op 0) \
2082 ip += READ32(ip + 1); \
2086 MINT_IN_CASE(MINT_BRFALSE_I4_S)
2089 MINT_IN_CASE(MINT_BRFALSE_I8_S)
2092 MINT_IN_CASE(MINT_BRFALSE_R8_S)
2095 MINT_IN_CASE(MINT_BRFALSE_I4)
2098 MINT_IN_CASE(MINT_BRFALSE_I8)
2101 MINT_IN_CASE(MINT_BRFALSE_R8)
2104 MINT_IN_CASE(MINT_BRTRUE_I4_S)
2107 MINT_IN_CASE(MINT_BRTRUE_I8_S)
2110 MINT_IN_CASE(MINT_BRTRUE_R8_S)
2113 MINT_IN_CASE(MINT_BRTRUE_I4)
2116 MINT_IN_CASE(MINT_BRTRUE_I8)
2119 MINT_IN_CASE(MINT_BRTRUE_R8)
2122 #define CONDBR_S(cond) \
2125 ip += * (gint16 *)(ip + 1); \
2128 #define BRELOP_S(datamem, op) \
2129 CONDBR_S(sp[0].data.datamem op sp[1].data.datamem)
2131 #define CONDBR(cond) \
2134 ip += READ32(ip + 1); \
2138 #define BRELOP(datamem, op) \
2139 CONDBR(sp[0].data.datamem op sp[1].data.datamem)
2141 MINT_IN_CASE(MINT_BEQ_I4_S)
2144 MINT_IN_CASE(MINT_BEQ_I8_S)
2147 MINT_IN_CASE(MINT_BEQ_R8_S)
2148 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2150 MINT_IN_CASE(MINT_BEQ_I4)
2153 MINT_IN_CASE(MINT_BEQ_I8)
2156 MINT_IN_CASE(MINT_BEQ_R8)
2157 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2159 MINT_IN_CASE(MINT_BGE_I4_S)
2162 MINT_IN_CASE(MINT_BGE_I8_S)
2165 MINT_IN_CASE(MINT_BGE_R8_S)
2166 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
2168 MINT_IN_CASE(MINT_BGE_I4)
2171 MINT_IN_CASE(MINT_BGE_I8)
2174 MINT_IN_CASE(MINT_BGE_R8)
2175 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
2177 MINT_IN_CASE(MINT_BGT_I4_S)
2180 MINT_IN_CASE(MINT_BGT_I8_S)
2183 MINT_IN_CASE(MINT_BGT_R8_S)
2184 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2186 MINT_IN_CASE(MINT_BGT_I4)
2189 MINT_IN_CASE(MINT_BGT_I8)
2192 MINT_IN_CASE(MINT_BGT_R8)
2193 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2195 MINT_IN_CASE(MINT_BLT_I4_S)
2198 MINT_IN_CASE(MINT_BLT_I8_S)
2201 MINT_IN_CASE(MINT_BLT_R8_S)
2202 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2204 MINT_IN_CASE(MINT_BLT_I4)
2207 MINT_IN_CASE(MINT_BLT_I8)
2210 MINT_IN_CASE(MINT_BLT_R8)
2211 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2213 MINT_IN_CASE(MINT_BLE_I4_S)
2216 MINT_IN_CASE(MINT_BLE_I8_S)
2219 MINT_IN_CASE(MINT_BLE_R8_S)
2220 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2222 MINT_IN_CASE(MINT_BLE_I4)
2225 MINT_IN_CASE(MINT_BLE_I8)
2228 MINT_IN_CASE(MINT_BLE_R8)
2229 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2231 MINT_IN_CASE(MINT_BNE_UN_I4_S)
2234 MINT_IN_CASE(MINT_BNE_UN_I8_S)
2237 MINT_IN_CASE(MINT_BNE_UN_R8_S)
2238 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
2240 MINT_IN_CASE(MINT_BNE_UN_I4)
2243 MINT_IN_CASE(MINT_BNE_UN_I8)
2246 MINT_IN_CASE(MINT_BNE_UN_R8)
2247 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
2250 #define BRELOP_S_CAST(datamem, op, type) \
2252 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
2253 ip += * (gint16 *)(ip + 1); \
2257 #define BRELOP_CAST(datamem, op, type) \
2259 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
2260 ip += READ32(ip + 1); \
2264 MINT_IN_CASE(MINT_BGE_UN_I4_S)
2265 BRELOP_S_CAST(i, >=, guint32);
2267 MINT_IN_CASE(MINT_BGE_UN_I8_S)
2268 BRELOP_S_CAST(l, >=, guint64);
2270 MINT_IN_CASE(MINT_BGE_UN_R8_S)
2271 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
2273 MINT_IN_CASE(MINT_BGE_UN_I4)
2274 BRELOP_CAST(i, >=, guint32);
2276 MINT_IN_CASE(MINT_BGE_UN_I8)
2277 BRELOP_CAST(l, >=, guint64);
2279 MINT_IN_CASE(MINT_BGE_UN_R8)
2280 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
2282 MINT_IN_CASE(MINT_BGT_UN_I4_S)
2283 BRELOP_S_CAST(i, >, guint32);
2285 MINT_IN_CASE(MINT_BGT_UN_I8_S)
2286 BRELOP_S_CAST(l, >, guint64);
2288 MINT_IN_CASE(MINT_BGT_UN_R8_S)
2289 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2291 MINT_IN_CASE(MINT_BGT_UN_I4)
2292 BRELOP_CAST(i, >, guint32);
2294 MINT_IN_CASE(MINT_BGT_UN_I8)
2295 BRELOP_CAST(l, >, guint64);
2297 MINT_IN_CASE(MINT_BGT_UN_R8)
2298 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2300 MINT_IN_CASE(MINT_BLE_UN_I4_S)
2301 BRELOP_S_CAST(i, <=, guint32);
2303 MINT_IN_CASE(MINT_BLE_UN_I8_S)
2304 BRELOP_S_CAST(l, <=, guint64);
2306 MINT_IN_CASE(MINT_BLE_UN_R8_S)
2307 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2309 MINT_IN_CASE(MINT_BLE_UN_I4)
2310 BRELOP_CAST(i, <=, guint32);
2312 MINT_IN_CASE(MINT_BLE_UN_I8)
2313 BRELOP_CAST(l, <=, guint64);
2315 MINT_IN_CASE(MINT_BLE_UN_R8)
2316 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2318 MINT_IN_CASE(MINT_BLT_UN_I4_S)
2319 BRELOP_S_CAST(i, <, guint32);
2321 MINT_IN_CASE(MINT_BLT_UN_I8_S)
2322 BRELOP_S_CAST(l, <, guint64);
2324 MINT_IN_CASE(MINT_BLT_UN_R8_S)
2325 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2327 MINT_IN_CASE(MINT_BLT_UN_I4)
2328 BRELOP_CAST(i, <, guint32);
2330 MINT_IN_CASE(MINT_BLT_UN_I8)
2331 BRELOP_CAST(l, <, guint64);
2333 MINT_IN_CASE(MINT_BLT_UN_R8)
2334 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2336 MINT_IN_CASE(MINT_SWITCH) {
2338 const unsigned short *st;
2344 if ((guint32)sp->data.i < n) {
2346 ip += 2 * (guint32)sp->data.i;
2347 offset = READ32 (ip);
2354 MINT_IN_CASE(MINT_LDIND_I1)
2356 sp[-1].data.i = *(gint8*)sp[-1].data.p;
2358 MINT_IN_CASE(MINT_LDIND_U1)
2360 sp[-1].data.i = *(guint8*)sp[-1].data.p;
2362 MINT_IN_CASE(MINT_LDIND_I2)
2364 sp[-1].data.i = *(gint16*)sp[-1].data.p;
2366 MINT_IN_CASE(MINT_LDIND_U2)
2368 sp[-1].data.i = *(guint16*)sp[-1].data.p;
2370 MINT_IN_CASE(MINT_LDIND_I4) /* Fall through */
2371 MINT_IN_CASE(MINT_LDIND_U4)
2373 sp[-1].data.i = *(gint32*)sp[-1].data.p;
2375 MINT_IN_CASE(MINT_LDIND_I8)
2377 sp[-1].data.l = *(gint64*)sp[-1].data.p;
2379 MINT_IN_CASE(MINT_LDIND_I)
2381 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2383 MINT_IN_CASE(MINT_LDIND_R4)
2385 sp[-1].data.f = *(gfloat*)sp[-1].data.p;
2387 MINT_IN_CASE(MINT_LDIND_R8)
2389 sp[-1].data.f = *(gdouble*)sp[-1].data.p;
2391 MINT_IN_CASE(MINT_LDIND_REF)
2393 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2395 MINT_IN_CASE(MINT_STIND_REF)
2398 * (gpointer *) sp->data.p = sp[1].data.p;
2400 MINT_IN_CASE(MINT_STIND_I1)
2403 * (gint8 *) sp->data.p = (gint8)sp[1].data.i;
2405 MINT_IN_CASE(MINT_STIND_I2)
2408 * (gint16 *) sp->data.p = (gint16)sp[1].data.i;
2410 MINT_IN_CASE(MINT_STIND_I4)
2413 * (gint32 *) sp->data.p = sp[1].data.i;
2415 MINT_IN_CASE(MINT_STIND_I)
2418 * (mono_i *) sp->data.p = (mono_i)sp[1].data.p;
2420 MINT_IN_CASE(MINT_STIND_I8)
2423 * (gint64 *) sp->data.p = sp[1].data.l;
2425 MINT_IN_CASE(MINT_STIND_R4)
2428 * (float *) sp->data.p = (gfloat)sp[1].data.f;
2430 MINT_IN_CASE(MINT_STIND_R8)
2433 * (double *) sp->data.p = sp[1].data.f;
2435 #define BINOP(datamem, op) \
2437 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.datamem; \
2439 MINT_IN_CASE(MINT_ADD_I4)
2442 MINT_IN_CASE(MINT_ADD_I8)
2445 MINT_IN_CASE(MINT_ADD_R8)
2448 MINT_IN_CASE(MINT_ADD1_I4)
2452 MINT_IN_CASE(MINT_SUB_I4)
2455 MINT_IN_CASE(MINT_SUB_I8)
2458 MINT_IN_CASE(MINT_SUB_R8)
2461 MINT_IN_CASE(MINT_SUB1_I4)
2465 MINT_IN_CASE(MINT_MUL_I4)
2468 MINT_IN_CASE(MINT_MUL_I8)
2471 MINT_IN_CASE(MINT_MUL_R8)
2474 MINT_IN_CASE(MINT_DIV_I4)
2475 if (sp [-1].data.i == 0)
2476 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2477 if (sp [-1].data.i == (-1))
2478 THROW_EX (mono_get_exception_overflow (), ip);
2481 MINT_IN_CASE(MINT_DIV_I8)
2482 if (sp [-1].data.l == 0)
2483 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2484 if (sp [-1].data.l == (-1))
2485 THROW_EX (mono_get_exception_overflow (), ip);
2488 MINT_IN_CASE(MINT_DIV_R8)
2492 #define BINOP_CAST(datamem, op, type) \
2494 sp [-1].data.datamem = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
2496 MINT_IN_CASE(MINT_DIV_UN_I4)
2497 if (sp [-1].data.i == 0)
2498 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2499 BINOP_CAST(i, /, guint32);
2501 MINT_IN_CASE(MINT_DIV_UN_I8)
2502 if (sp [-1].data.l == 0)
2503 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2504 BINOP_CAST(l, /, guint64);
2506 MINT_IN_CASE(MINT_REM_I4)
2507 if (sp [-1].data.i == 0)
2508 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2511 MINT_IN_CASE(MINT_REM_I8)
2512 if (sp [-1].data.l == 0)
2513 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2516 MINT_IN_CASE(MINT_REM_R8)
2517 /* FIXME: what do we actually do here? */
2519 sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
2522 MINT_IN_CASE(MINT_REM_UN_I4)
2523 if (sp [-1].data.i == 0)
2524 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2525 BINOP_CAST(i, %, guint32);
2527 MINT_IN_CASE(MINT_REM_UN_I8)
2528 if (sp [-1].data.l == 0)
2529 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2530 BINOP_CAST(l, %, guint64);
2532 MINT_IN_CASE(MINT_AND_I4)
2535 MINT_IN_CASE(MINT_AND_I8)
2538 MINT_IN_CASE(MINT_OR_I4)
2541 MINT_IN_CASE(MINT_OR_I8)
2544 MINT_IN_CASE(MINT_XOR_I4)
2547 MINT_IN_CASE(MINT_XOR_I8)
2551 #define SHIFTOP(datamem, op) \
2553 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.i; \
2556 MINT_IN_CASE(MINT_SHL_I4)
2559 MINT_IN_CASE(MINT_SHL_I8)
2562 MINT_IN_CASE(MINT_SHR_I4)
2565 MINT_IN_CASE(MINT_SHR_I8)
2568 MINT_IN_CASE(MINT_SHR_UN_I4)
2570 sp [-1].data.i = (guint32)sp [-1].data.i >> sp [0].data.i;
2573 MINT_IN_CASE(MINT_SHR_UN_I8)
2575 sp [-1].data.l = (guint64)sp [-1].data.l >> sp [0].data.i;
2578 MINT_IN_CASE(MINT_NEG_I4)
2579 sp [-1].data.i = - sp [-1].data.i;
2582 MINT_IN_CASE(MINT_NEG_I8)
2583 sp [-1].data.l = - sp [-1].data.l;
2586 MINT_IN_CASE(MINT_NEG_R8)
2587 sp [-1].data.f = - sp [-1].data.f;
2590 MINT_IN_CASE(MINT_NOT_I4)
2591 sp [-1].data.i = ~ sp [-1].data.i;
2594 MINT_IN_CASE(MINT_NOT_I8)
2595 sp [-1].data.l = ~ sp [-1].data.l;
2598 MINT_IN_CASE(MINT_CONV_I1_I4)
2599 sp [-1].data.i = (gint8)sp [-1].data.i;
2602 MINT_IN_CASE(MINT_CONV_I1_I8)
2603 sp [-1].data.i = (gint8)sp [-1].data.l;
2606 MINT_IN_CASE(MINT_CONV_I1_R8)
2607 sp [-1].data.i = (gint8)sp [-1].data.f;
2610 MINT_IN_CASE(MINT_CONV_U1_I4)
2611 sp [-1].data.i = (guint8)sp [-1].data.i;
2614 MINT_IN_CASE(MINT_CONV_U1_I8)
2615 sp [-1].data.i = (guint8)sp [-1].data.l;
2618 MINT_IN_CASE(MINT_CONV_U1_R8)
2619 sp [-1].data.i = (guint8)sp [-1].data.f;
2622 MINT_IN_CASE(MINT_CONV_I2_I4)
2623 sp [-1].data.i = (gint16)sp [-1].data.i;
2626 MINT_IN_CASE(MINT_CONV_I2_I8)
2627 sp [-1].data.i = (gint16)sp [-1].data.l;
2630 MINT_IN_CASE(MINT_CONV_I2_R8)
2631 sp [-1].data.i = (gint16)sp [-1].data.f;
2634 MINT_IN_CASE(MINT_CONV_U2_I4)
2635 sp [-1].data.i = (guint16)sp [-1].data.i;
2638 MINT_IN_CASE(MINT_CONV_U2_I8)
2639 sp [-1].data.i = (guint16)sp [-1].data.l;
2642 MINT_IN_CASE(MINT_CONV_U2_R8)
2643 sp [-1].data.i = (guint16)sp [-1].data.f;
2646 MINT_IN_CASE(MINT_CONV_I4_R8)
2647 sp [-1].data.i = (gint32)sp [-1].data.f;
2650 MINT_IN_CASE(MINT_CONV_U4_I8)
2651 MINT_IN_CASE(MINT_CONV_I4_I8)
2652 sp [-1].data.i = (gint32)sp [-1].data.l;
2655 MINT_IN_CASE(MINT_CONV_I4_I8_SP)
2656 sp [-2].data.i = (gint32)sp [-2].data.l;
2659 MINT_IN_CASE(MINT_CONV_U4_R8)
2660 sp [-1].data.i = (guint32)sp [-1].data.f;
2663 MINT_IN_CASE(MINT_CONV_I8_I4)
2664 sp [-1].data.l = sp [-1].data.i;
2667 MINT_IN_CASE(MINT_CONV_I8_I4_SP)
2668 sp [-2].data.l = sp [-2].data.i;
2671 MINT_IN_CASE(MINT_CONV_I8_U4)
2672 sp [-1].data.l = (guint32)sp [-1].data.i;
2675 MINT_IN_CASE(MINT_CONV_I8_R8)
2676 sp [-1].data.l = (gint64)sp [-1].data.f;
2679 MINT_IN_CASE(MINT_CONV_R4_I4)
2680 sp [-1].data.f = (float)sp [-1].data.i;
2683 MINT_IN_CASE(MINT_CONV_R4_I8)
2684 sp [-1].data.f = (float)sp [-1].data.l;
2687 MINT_IN_CASE(MINT_CONV_R4_R8)
2688 sp [-1].data.f = (float)sp [-1].data.f;
2691 MINT_IN_CASE(MINT_CONV_R8_I4)
2692 sp [-1].data.f = (double)sp [-1].data.i;
2695 MINT_IN_CASE(MINT_CONV_R8_I8)
2696 sp [-1].data.f = (double)sp [-1].data.l;
2699 MINT_IN_CASE(MINT_CONV_U8_I4)
2700 sp [-1].data.l = sp [-1].data.i & 0xffffffff;
2703 MINT_IN_CASE(MINT_CONV_U8_R8)
2704 sp [-1].data.l = (guint64)sp [-1].data.f;
2708 MINT_IN_CASE(MINT_CPOBJ) {
2711 vtklass = rtm->data_items[READ32 (ip)];
2714 memcpy (sp [0].data.p, sp [1].data.p, mono_class_value_size (vtklass, NULL));
2718 MINT_IN_CASE(MINT_LDOBJ) {
2721 c = rtm->data_items[* (guint16 *)(ip + 1)];
2723 if (c->byval_arg.type != MONO_TYPE_VALUETYPE || c->byval_arg.data.klass->enumtype) {
2725 stackval_from_data (&c->byval_arg, &sp [-1], p, FALSE);
2727 size = mono_class_value_size (c, NULL);
2729 sp [-1].data.p = vt_sp;
2730 memcpy(vt_sp, p, size);
2731 vt_sp += (size + 7) & ~7;
2735 MINT_IN_CASE(MINT_LDSTR)
2736 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
2740 MINT_IN_CASE(MINT_NEWOBJ) {
2741 MonoClass *newobj_class;
2742 MonoMethodSignature *csig;
2743 stackval valuetype_this;
2749 token = * (guint16 *)(ip + 1);
2752 child_frame.runtime_method = rtm->data_items [token];
2753 csig = mono_method_signature (child_frame.runtime_method->method);
2754 newobj_class = child_frame.runtime_method->method->klass;
2755 /*if (profiling_classes) {
2756 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
2758 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
2762 if (newobj_class->parent == mono_defaults.array_class) {
2763 sp -= csig->param_count;
2764 o = ves_array_create (context->domain, newobj_class, csig, sp);
2765 goto array_constructed;
2769 * First arg is the object.
2771 if (newobj_class->valuetype) {
2772 if (!newobj_class->enumtype && (newobj_class->byval_arg.type == MONO_TYPE_VALUETYPE)) {
2773 child_frame.obj = vt_sp;
2774 valuetype_this.data.p = vt_sp;
2776 memset (&valuetype_this, 0, sizeof (stackval));
2777 child_frame.obj = &valuetype_this;
2780 if (newobj_class != mono_defaults.string_class) {
2781 context->managed_code = 0;
2782 o = mono_object_new_checked (context->domain, newobj_class, &error);
2783 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2784 context->managed_code = 1;
2785 if (*abort_requested)
2786 mono_thread_interruption_checkpoint ();
2787 child_frame.obj = o;
2789 child_frame.retval = &retval;
2793 if (csig->param_count) {
2794 sp -= csig->param_count;
2795 child_frame.stack_args = sp;
2797 child_frame.stack_args = NULL;
2800 g_assert (csig->call_convention == MONO_CALL_DEFAULT);
2802 child_frame.ip = NULL;
2803 child_frame.ex = NULL;
2805 ves_exec_method_with_context (&child_frame, context);
2807 context->current_frame = frame;
2809 if (child_frame.ex) {
2811 * An exception occurred, need to run finally, fault and catch handlers..
2813 frame->ex = child_frame.ex;
2814 goto handle_finally;
2817 * a constructor returns void, but we need to return the object we created
2820 if (newobj_class->valuetype && !newobj_class->enumtype) {
2821 *sp = valuetype_this;
2822 } else if (newobj_class == mono_defaults.string_class) {
2830 MINT_IN_CASE(MINT_CASTCLASS)
2831 c = rtm->data_items [*(guint16 *)(ip + 1)];
2832 if ((o = sp [-1].data.p)) {
2833 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2834 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2836 THROW_EX (mono_get_exception_invalid_cast (), ip);
2840 MINT_IN_CASE(MINT_ISINST)
2841 c = rtm->data_items [*(guint16 *)(ip + 1)];
2842 if ((o = sp [-1].data.p)) {
2843 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2844 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2846 sp [-1].data.p = NULL;
2850 MINT_IN_CASE(MINT_CONV_R_UN_I4)
2851 sp [-1].data.f = (double)(guint32)sp [-1].data.i;
2854 MINT_IN_CASE(MINT_CONV_R_UN_I8)
2855 sp [-1].data.f = (double)(guint64)sp [-1].data.l;
2858 MINT_IN_CASE(MINT_UNBOX)
2859 c = rtm->data_items[*(guint16 *)(ip + 1)];
2863 THROW_EX (mono_get_exception_null_reference (), ip);
2865 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2866 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2867 if (!(isinst_obj || ((o->vtable->klass->rank == 0) && (o->vtable->klass->element_class == c->element_class))))
2868 THROW_EX (mono_get_exception_invalid_cast (), ip);
2870 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
2871 g_error ("unbox: implement vt");
2873 stackval_from_data (&c->byval_arg, &sp [-1], mono_object_unbox (o), FALSE);
2877 MINT_IN_CASE(MINT_THROW)
2879 frame->ex_handler = NULL;
2881 sp->data.p = mono_get_exception_null_reference ();
2882 THROW_EX ((MonoException *)sp->data.p, ip);
2884 MINT_IN_CASE(MINT_LDFLDA)
2887 THROW_EX (mono_get_exception_null_reference (), ip);
2888 sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
2891 MINT_IN_CASE(MINT_CKNULL)
2894 THROW_EX (mono_get_exception_null_reference (), ip);
2898 #define LDFLD(datamem, fieldtype) \
2899 o = sp [-1].data.p; \
2901 THROW_EX (mono_get_exception_null_reference (), ip); \
2902 sp[-1].data.datamem = * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) ; \
2905 MINT_IN_CASE(MINT_LDFLD_I1) LDFLD(i, gint8); MINT_IN_BREAK;
2906 MINT_IN_CASE(MINT_LDFLD_U1) LDFLD(i, guint8); MINT_IN_BREAK;
2907 MINT_IN_CASE(MINT_LDFLD_I2) LDFLD(i, gint16); MINT_IN_BREAK;
2908 MINT_IN_CASE(MINT_LDFLD_U2) LDFLD(i, guint16); MINT_IN_BREAK;
2909 MINT_IN_CASE(MINT_LDFLD_I4) LDFLD(i, gint32); MINT_IN_BREAK;
2910 MINT_IN_CASE(MINT_LDFLD_I8) LDFLD(l, gint64); MINT_IN_BREAK;
2911 MINT_IN_CASE(MINT_LDFLD_R4) LDFLD(f, float); MINT_IN_BREAK;
2912 MINT_IN_CASE(MINT_LDFLD_R8) LDFLD(f, double); MINT_IN_BREAK;
2913 MINT_IN_CASE(MINT_LDFLD_O) LDFLD(p, gpointer); MINT_IN_BREAK;
2914 MINT_IN_CASE(MINT_LDFLD_P) LDFLD(p, gpointer); MINT_IN_BREAK;
2916 MINT_IN_CASE(MINT_LDFLD_VT)
2919 THROW_EX (mono_get_exception_null_reference (), ip);
2920 i32 = READ32(ip + 2);
2921 sp [-1].data.p = vt_sp;
2922 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
2923 vt_sp += (i32 + 7) & ~7;
2927 MINT_IN_CASE(MINT_LDRMFLD) {
2929 MonoClassField *field;
2934 THROW_EX (mono_get_exception_null_reference (), ip);
2935 field = rtm->data_items[* (guint16 *)(ip + 1)];
2937 if (mono_object_is_transparent_proxy (o)) {
2938 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2940 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
2941 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2943 addr = (char*)o + field->offset;
2946 stackval_from_data (field->type, &sp [-1], addr, FALSE);
2950 MINT_IN_CASE(MINT_LDRMFLD_VT) {
2951 MonoClassField *field;
2957 THROW_EX (mono_get_exception_null_reference (), ip);
2958 field = rtm->data_items[* (guint16 *)(ip + 1)];
2959 i32 = READ32(ip + 2);
2961 if (mono_object_is_transparent_proxy (o)) {
2962 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2963 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
2964 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2966 addr = (char*)o + field->offset;
2969 sp [-1].data.p = vt_sp;
2970 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
2971 vt_sp += (i32 + 7) & ~7;
2972 memcpy(sp [-1].data.p, addr, i32);
2976 #define STFLD(datamem, fieldtype) \
2977 o = sp [-2].data.p; \
2979 THROW_EX (mono_get_exception_null_reference (), ip); \
2981 * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) = sp[1].data.datamem; \
2984 MINT_IN_CASE(MINT_STFLD_I1) STFLD(i, gint8); MINT_IN_BREAK;
2985 MINT_IN_CASE(MINT_STFLD_U1) STFLD(i, guint8); MINT_IN_BREAK;
2986 MINT_IN_CASE(MINT_STFLD_I2) STFLD(i, gint16); MINT_IN_BREAK;
2987 MINT_IN_CASE(MINT_STFLD_U2) STFLD(i, guint16); MINT_IN_BREAK;
2988 MINT_IN_CASE(MINT_STFLD_I4) STFLD(i, gint32); MINT_IN_BREAK;
2989 MINT_IN_CASE(MINT_STFLD_I8) STFLD(l, gint64); MINT_IN_BREAK;
2990 MINT_IN_CASE(MINT_STFLD_R4) STFLD(f, float); MINT_IN_BREAK;
2991 MINT_IN_CASE(MINT_STFLD_R8) STFLD(f, double); MINT_IN_BREAK;
2992 MINT_IN_CASE(MINT_STFLD_O) STFLD(p, gpointer); MINT_IN_BREAK;
2993 MINT_IN_CASE(MINT_STFLD_P) STFLD(p, gpointer); MINT_IN_BREAK;
2995 MINT_IN_CASE(MINT_STFLD_VT)
2998 THROW_EX (mono_get_exception_null_reference (), ip);
2999 i32 = READ32(ip + 2);
3001 memcpy((char *)o + * (guint16 *)(ip + 1), sp [1].data.p, i32);
3002 vt_sp -= (i32 + 7) & ~7;
3006 MINT_IN_CASE(MINT_STRMFLD) {
3007 MonoClassField *field;
3011 THROW_EX (mono_get_exception_null_reference (), ip);
3013 field = rtm->data_items[* (guint16 *)(ip + 1)];
3016 if (mono_object_is_transparent_proxy (o)) {
3017 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3018 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
3019 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3021 stackval_to_data (field->type, &sp [-1], (char*)o + field->offset, FALSE);
3026 MINT_IN_CASE(MINT_STRMFLD_VT) {
3027 MonoClassField *field;
3031 THROW_EX (mono_get_exception_null_reference (), ip);
3032 field = rtm->data_items[* (guint16 *)(ip + 1)];
3033 i32 = READ32(ip + 2);
3036 if (mono_object_is_transparent_proxy (o)) {
3037 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3038 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
3039 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3041 memcpy((char*)o + field->offset, sp [-1].data.p, i32);
3044 vt_sp -= (i32 + 7) & ~7;
3047 MINT_IN_CASE(MINT_LDSFLDA) {
3048 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3049 MonoVTable *vt = mono_class_vtable (context->domain, field->parent);
3052 if (!vt->initialized) {
3054 mono_runtime_class_init_full (vt, &error);
3055 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3059 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3060 sp->data.p = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3062 sp->data.p = (char*)(vt->vtable) + field->offset;
3066 MINT_IN_CASE(MINT_LDSFLD) {
3068 MonoClassField *field;
3071 field = rtm->data_items[*(guint16 *)(ip + 1)];
3072 vt = rtm->data_items [*(guint16 *)(ip + 2)];
3073 if (!vt->initialized) {
3075 mono_runtime_class_init_full (vt, &error);
3076 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3079 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3080 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3082 addr = (char*)(vt->vtable) + field->offset;
3084 stackval_from_data (field->type, sp, addr, FALSE);
3088 MINT_IN_CASE(MINT_LDSFLD_I4) {
3089 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3090 MonoVTable *vt = rtm->data_items [*(guint16 *)(ip + 2)];
3091 if (!vt->initialized) {
3093 mono_runtime_class_init_full (vt, &error);
3094 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3097 sp->data.i = * (gint32 *)((char*)(vt->vtable) + field->offset);
3101 MINT_IN_CASE(MINT_LDSFLD_O) {
3102 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3103 MonoVTable *vt = rtm->data_items [*(guint16 *)(ip + 2)];
3104 if (!vt->initialized) {
3106 mono_runtime_class_init_full (vt, &error);
3107 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3110 sp->data.p = * (gpointer *)((char*)(vt->vtable) + field->offset);
3114 MINT_IN_CASE(MINT_LDSFLD_VT) {
3116 MonoClassField *field;
3121 token = * (guint16 *)(ip + 1);
3122 size = READ32(ip + 2);
3123 field = rtm->data_items[token];
3126 vt = mono_class_vtable (context->domain, field->parent);
3127 if (!vt->initialized) {
3129 mono_runtime_class_init_full (vt, &error);
3130 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3133 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3134 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3136 addr = (char*)(vt->vtable) + field->offset;
3139 vt_sp += (size + 7) & ~7;
3140 stackval_from_data (field->type, sp, addr, FALSE);
3144 MINT_IN_CASE(MINT_STSFLD) {
3146 MonoClassField *field;
3150 token = * (guint16 *)(ip + 1);
3151 field = rtm->data_items[token];
3155 vt = mono_class_vtable (context->domain, field->parent);
3156 if (!vt->initialized) {
3158 mono_runtime_class_init_full (vt, &error);
3159 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3162 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3163 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3165 addr = (char*)(vt->vtable) + field->offset;
3167 stackval_to_data (field->type, sp, addr, FALSE);
3170 MINT_IN_CASE(MINT_STSFLD_VT) {
3172 MonoClassField *field;
3177 token = * (guint16 *)(ip + 1);
3178 size = READ32(ip + 2);
3179 field = rtm->data_items[token];
3182 vt = mono_class_vtable (context->domain, field->parent);
3183 if (!vt->initialized) {
3185 mono_runtime_class_init_full (vt, &error);
3186 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3189 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3190 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3192 addr = (char*)(vt->vtable) + field->offset;
3194 stackval_to_data (field->type, sp, addr, FALSE);
3195 vt_sp -= (size + 7) & ~7;
3198 MINT_IN_CASE(MINT_STOBJ_VT) {
3200 c = rtm->data_items[* (guint16 *)(ip + 1)];
3202 size = mono_class_value_size (c, NULL);
3203 memcpy(sp [-2].data.p, sp [-1].data.p, size);
3204 vt_sp -= (size + 7) & ~7;
3208 MINT_IN_CASE(MINT_STOBJ) {
3210 c = rtm->data_items[* (guint16 *)(ip + 1)];
3212 size = mono_class_value_size (c, NULL);
3213 memcpy(sp [-2].data.p, &sp [-1].data, size);
3217 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_R8)
3218 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3219 THROW_EX (mono_get_exception_overflow (), ip);
3220 sp [-1].data.i = (guint32)sp [-1].data.f;
3223 MINT_IN_CASE(MINT_CONV_OVF_U8_I4)
3224 if (sp [-1].data.i < 0)
3225 THROW_EX (mono_get_exception_overflow (), ip);
3226 sp [-1].data.l = sp [-1].data.i;
3229 MINT_IN_CASE(MINT_CONV_OVF_U8_R8)
3230 MINT_IN_CASE(MINT_CONV_OVF_I8_UN_R8)
3231 if (sp [-1].data.f < 0 || sp [-1].data.f > 9223372036854775807LL)
3232 THROW_EX (mono_get_exception_overflow (), ip);
3233 sp [-1].data.l = (guint64)sp [-1].data.f;
3236 MINT_IN_CASE(MINT_CONV_OVF_I8_R8)
3237 if (sp [-1].data.f < MYGINT64_MIN || sp [-1].data.f > MYGINT64_MAX)
3238 THROW_EX (mono_get_exception_overflow (), ip);
3239 sp [-1].data.l = (gint64)sp [-1].data.f;
3242 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_I8)
3243 if ((mono_u)sp [-1].data.l > MYGUINT32_MAX)
3244 THROW_EX (mono_get_exception_overflow (), ip);
3245 sp [-1].data.i = (mono_u)sp [-1].data.l;
3248 MINT_IN_CASE(MINT_BOX)
3249 c = rtm->data_items [* (guint16 *)(ip + 1)];
3251 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
3252 int size = mono_class_value_size (c, NULL);
3253 sp [-1].data.p = mono_value_box_checked (context->domain, c, sp [-1].data.p, &error);
3254 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3255 size = (size + 7) & ~7;
3259 stackval_to_data (&c->byval_arg, &sp [-1], (char*)&sp [-1], FALSE);
3260 sp [-1].data.p = mono_value_box_checked (context->domain, c, &sp [-1], &error);
3261 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3265 MINT_IN_CASE(MINT_NEWARR)
3266 sp [-1].data.p = (MonoObject*) mono_array_new_checked (context->domain, rtm->data_items[*(guint16 *)(ip + 1)], sp [-1].data.i, &error);
3267 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3269 /*if (profiling_classes) {
3270 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
3272 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
3276 MINT_IN_CASE(MINT_LDLEN)
3279 THROW_EX (mono_get_exception_null_reference (), ip);
3280 sp [-1].data.nati = mono_array_length ((MonoArray *)o);
3283 MINT_IN_CASE(MINT_GETCHR) {
3287 THROW_EX (mono_get_exception_null_reference (), ip);
3288 i32 = sp [-1].data.i;
3289 if (i32 < 0 || i32 >= mono_string_length (s))
3290 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3292 sp [-1].data.i = mono_string_chars(s)[i32];
3296 MINT_IN_CASE(MINT_STRLEN)
3298 sp [-1].data.i = mono_string_length ((MonoString*)sp [-1].data.p);
3300 MINT_IN_CASE(MINT_ARRAY_RANK)
3303 THROW_EX (mono_get_exception_null_reference (), ip);
3304 sp [-1].data.i = mono_object_class (sp [-1].data.p)->rank;
3307 MINT_IN_CASE(MINT_LDELEMA) {
3311 /*token = READ32 (ip)*/;
3317 aindex = sp [1].data.i;
3318 if (aindex >= mono_array_length ((MonoArray *) o))
3319 THROW_EX (mono_get_exception_index_out_of_range (), ip - 2);
3321 /* check the array element corresponds to token */
3322 esize = mono_array_element_size (((MonoArray *) o)->obj.vtable->klass);
3324 sp->data.p = mono_array_addr_with_size ((MonoArray *) o, esize, aindex);
3328 MINT_IN_CASE(MINT_LDELEM_I1) /* fall through */
3329 MINT_IN_CASE(MINT_LDELEM_U1) /* fall through */
3330 MINT_IN_CASE(MINT_LDELEM_I2) /* fall through */
3331 MINT_IN_CASE(MINT_LDELEM_U2) /* fall through */
3332 MINT_IN_CASE(MINT_LDELEM_I4) /* fall through */
3333 MINT_IN_CASE(MINT_LDELEM_U4) /* fall through */
3334 MINT_IN_CASE(MINT_LDELEM_I8) /* fall through */
3335 MINT_IN_CASE(MINT_LDELEM_I) /* fall through */
3336 MINT_IN_CASE(MINT_LDELEM_R4) /* fall through */
3337 MINT_IN_CASE(MINT_LDELEM_R8) /* fall through */
3338 MINT_IN_CASE(MINT_LDELEM_REF) {
3346 THROW_EX (mono_get_exception_null_reference (), ip);
3348 aindex = sp [1].data.i;
3349 if (aindex >= mono_array_length (o))
3350 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3353 * FIXME: throw mono_get_exception_array_type_mismatch () if needed
3356 case MINT_LDELEM_I1:
3357 sp [0].data.i = mono_array_get (o, gint8, aindex);
3359 case MINT_LDELEM_U1:
3360 sp [0].data.i = mono_array_get (o, guint8, aindex);
3362 case MINT_LDELEM_I2:
3363 sp [0].data.i = mono_array_get (o, gint16, aindex);
3365 case MINT_LDELEM_U2:
3366 sp [0].data.i = mono_array_get (o, guint16, aindex);
3369 sp [0].data.nati = mono_array_get (o, mono_i, aindex);
3371 case MINT_LDELEM_I4:
3372 sp [0].data.i = mono_array_get (o, gint32, aindex);
3374 case MINT_LDELEM_U4:
3375 sp [0].data.i = mono_array_get (o, guint32, aindex);
3377 case MINT_LDELEM_I8:
3378 sp [0].data.l = mono_array_get (o, guint64, aindex);
3380 case MINT_LDELEM_R4:
3381 sp [0].data.f = mono_array_get (o, float, aindex);
3383 case MINT_LDELEM_R8:
3384 sp [0].data.f = mono_array_get (o, double, aindex);
3386 case MINT_LDELEM_REF:
3387 sp [0].data.p = mono_array_get (o, gpointer, aindex);
3397 MINT_IN_CASE(MINT_STELEM_I) /* fall through */
3398 MINT_IN_CASE(MINT_STELEM_I1) /* fall through */
3399 MINT_IN_CASE(MINT_STELEM_I2) /* fall through */
3400 MINT_IN_CASE(MINT_STELEM_I4) /* fall through */
3401 MINT_IN_CASE(MINT_STELEM_I8) /* fall through */
3402 MINT_IN_CASE(MINT_STELEM_R4) /* fall through */
3403 MINT_IN_CASE(MINT_STELEM_R8) /* fall through */
3404 MINT_IN_CASE(MINT_STELEM_REF) {
3411 THROW_EX (mono_get_exception_null_reference (), ip);
3413 aindex = sp [1].data.i;
3414 if (aindex >= mono_array_length ((MonoArray *)o))
3415 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3419 mono_array_set ((MonoArray *)o, mono_i, aindex, sp [2].data.nati);
3421 case MINT_STELEM_I1:
3422 mono_array_set ((MonoArray *)o, gint8, aindex, sp [2].data.i);
3424 case MINT_STELEM_I2:
3425 mono_array_set ((MonoArray *)o, gint16, aindex, sp [2].data.i);
3427 case MINT_STELEM_I4:
3428 mono_array_set ((MonoArray *)o, gint32, aindex, sp [2].data.i);
3430 case MINT_STELEM_I8:
3431 mono_array_set ((MonoArray *)o, gint64, aindex, sp [2].data.l);
3433 case MINT_STELEM_R4:
3434 mono_array_set ((MonoArray *)o, float, aindex, sp [2].data.f);
3436 case MINT_STELEM_R8:
3437 mono_array_set ((MonoArray *)o, double, aindex, sp [2].data.f);
3439 case MINT_STELEM_REF: {
3440 MonoObject *isinst_obj = mono_object_isinst_checked (sp [2].data.p, mono_object_class (o)->element_class, &error);
3441 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3442 if (sp [2].data.p && !isinst_obj)
3443 THROW_EX (mono_get_exception_array_type_mismatch (), ip);
3444 mono_array_set ((MonoArray *)o, gpointer, aindex, sp [2].data.p);
3453 MINT_IN_CASE(MINT_CONV_OVF_I4_U4)
3454 if (sp [-1].data.i < 0)
3455 THROW_EX (mono_get_exception_overflow (), ip);
3458 MINT_IN_CASE(MINT_CONV_OVF_I4_I8)
3459 if (sp [-1].data.l < MYGINT32_MIN || sp [-1].data.l > MYGINT32_MAX)
3460 THROW_EX (mono_get_exception_overflow (), ip);
3461 sp [-1].data.i = (gint32) sp [-1].data.l;
3464 MINT_IN_CASE(MINT_CONV_OVF_I4_R8)
3465 if (sp [-1].data.f < MYGINT32_MIN || sp [-1].data.f > MYGINT32_MAX)
3466 THROW_EX (mono_get_exception_overflow (), ip);
3467 sp [-1].data.i = (gint32) sp [-1].data.f;
3470 MINT_IN_CASE(MINT_CONV_OVF_U4_I4)
3471 if (sp [-1].data.i < 0)
3472 THROW_EX (mono_get_exception_overflow (), ip);
3475 MINT_IN_CASE(MINT_CONV_OVF_U4_I8)
3476 if (sp [-1].data.l < 0 || sp [-1].data.l > MYGUINT32_MAX)
3477 THROW_EX (mono_get_exception_overflow (), ip);
3478 sp [-1].data.i = (guint32) sp [-1].data.l;
3481 MINT_IN_CASE(MINT_CONV_OVF_U4_R8)
3482 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3483 THROW_EX (mono_get_exception_overflow (), ip);
3484 sp [-1].data.i = (guint32) sp [-1].data.f;
3487 MINT_IN_CASE(MINT_CONV_OVF_I2_I4)
3488 if (sp [-1].data.i < -32768 || sp [-1].data.i > 32767)
3489 THROW_EX (mono_get_exception_overflow (), ip);
3492 MINT_IN_CASE(MINT_CONV_OVF_I2_I8)
3493 if (sp [-1].data.l < -32768 || sp [-1].data.l > 32767)
3494 THROW_EX (mono_get_exception_overflow (), ip);
3495 sp [-1].data.i = (gint16) sp [-1].data.l;
3498 MINT_IN_CASE(MINT_CONV_OVF_I2_R8)
3499 if (sp [-1].data.f < -32768 || sp [-1].data.f > 32767)
3500 THROW_EX (mono_get_exception_overflow (), ip);
3501 sp [-1].data.i = (gint16) sp [-1].data.f;
3504 MINT_IN_CASE(MINT_CONV_OVF_U2_I4)
3505 if (sp [-1].data.i < 0 || sp [-1].data.i > 65535)
3506 THROW_EX (mono_get_exception_overflow (), ip);
3509 MINT_IN_CASE(MINT_CONV_OVF_U2_I8)
3510 if (sp [-1].data.l < 0 || sp [-1].data.l > 65535)
3511 THROW_EX (mono_get_exception_overflow (), ip);
3512 sp [-1].data.i = (guint16) sp [-1].data.l;
3515 MINT_IN_CASE(MINT_CONV_OVF_U2_R8)
3516 if (sp [-1].data.f < 0 || sp [-1].data.f > 65535)
3517 THROW_EX (mono_get_exception_overflow (), ip);
3518 sp [-1].data.i = (guint16) sp [-1].data.f;
3521 MINT_IN_CASE(MINT_CONV_OVF_I1_I4)
3522 if (sp [-1].data.i < -128 || sp [-1].data.i > 127)
3523 THROW_EX (mono_get_exception_overflow (), ip);
3526 MINT_IN_CASE(MINT_CONV_OVF_I1_I8)
3527 if (sp [-1].data.l < -128 || sp [-1].data.l > 127)
3528 THROW_EX (mono_get_exception_overflow (), ip);
3529 sp [-1].data.i = (gint8) sp [-1].data.l;
3532 MINT_IN_CASE(MINT_CONV_OVF_I1_R8)
3533 if (sp [-1].data.f < -128 || sp [-1].data.f > 127)
3534 THROW_EX (mono_get_exception_overflow (), ip);
3535 sp [-1].data.i = (gint8) sp [-1].data.f;
3538 MINT_IN_CASE(MINT_CONV_OVF_U1_I4)
3539 if (sp [-1].data.i < 0 || sp [-1].data.i > 255)
3540 THROW_EX (mono_get_exception_overflow (), ip);
3543 MINT_IN_CASE(MINT_CONV_OVF_U1_I8)
3544 if (sp [-1].data.l < 0 || sp [-1].data.l > 255)
3545 THROW_EX (mono_get_exception_overflow (), ip);
3546 sp [-1].data.i = (guint8) sp [-1].data.l;
3549 MINT_IN_CASE(MINT_CONV_OVF_U1_R8)
3550 if (sp [-1].data.f < 0 || sp [-1].data.f > 255)
3551 THROW_EX (mono_get_exception_overflow (), ip);
3552 sp [-1].data.i = (guint8) sp [-1].data.f;
3556 MINT_IN_CASE(MINT_LDELEM)
3557 MINT_IN_CASE(MINT_STELEM)
3558 MINT_IN_CASE(MINT_UNBOX_ANY)
3560 MINT_IN_CASE(MINT_REFANYVAL) ves_abort(); MINT_IN_BREAK;
3562 MINT_IN_CASE(MINT_CKFINITE)
3563 if (!isfinite(sp [-1].data.f))
3564 THROW_EX (mono_get_exception_arithmetic (), ip);
3568 MINT_IN_CASE(MINT_MKREFANY) ves_abort(); MINT_IN_BREAK;
3570 MINT_IN_CASE(MINT_LDTOKEN)
3573 * (gpointer *)sp->data.p = rtm->data_items[*(guint16 *)(ip + 1)];
3577 MINT_IN_CASE(MINT_ADD_OVF_I4)
3578 if (CHECK_ADD_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3579 THROW_EX (mono_get_exception_overflow (), ip);
3582 MINT_IN_CASE(MINT_ADD_OVF_I8)
3583 if (CHECK_ADD_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3584 THROW_EX (mono_get_exception_overflow (), ip);
3587 MINT_IN_CASE(MINT_ADD_OVF_UN_I4)
3588 if (CHECK_ADD_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3589 THROW_EX (mono_get_exception_overflow (), ip);
3590 BINOP_CAST(i, +, guint32);
3592 MINT_IN_CASE(MINT_ADD_OVF_UN_I8)
3593 if (CHECK_ADD_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3594 THROW_EX (mono_get_exception_overflow (), ip);
3595 BINOP_CAST(l, +, guint64);
3597 MINT_IN_CASE(MINT_MUL_OVF_I4)
3598 if (CHECK_MUL_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3599 THROW_EX (mono_get_exception_overflow (), ip);
3602 MINT_IN_CASE(MINT_MUL_OVF_I8)
3603 if (CHECK_MUL_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3604 THROW_EX (mono_get_exception_overflow (), ip);
3607 MINT_IN_CASE(MINT_MUL_OVF_UN_I4)
3608 if (CHECK_MUL_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3609 THROW_EX (mono_get_exception_overflow (), ip);
3610 BINOP_CAST(i, *, guint32);
3612 MINT_IN_CASE(MINT_MUL_OVF_UN_I8)
3613 if (CHECK_MUL_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3614 THROW_EX (mono_get_exception_overflow (), ip);
3615 BINOP_CAST(l, *, guint64);
3617 MINT_IN_CASE(MINT_SUB_OVF_I4)
3618 if (CHECK_SUB_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3619 THROW_EX (mono_get_exception_overflow (), ip);
3622 MINT_IN_CASE(MINT_SUB_OVF_I8)
3623 if (CHECK_SUB_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3624 THROW_EX (mono_get_exception_overflow (), ip);
3627 MINT_IN_CASE(MINT_SUB_OVF_UN_I4)
3628 if (CHECK_SUB_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3629 THROW_EX (mono_get_exception_overflow (), ip);
3630 BINOP_CAST(i, -, guint32);
3632 MINT_IN_CASE(MINT_SUB_OVF_UN_I8)
3633 if (CHECK_SUB_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3634 THROW_EX (mono_get_exception_overflow (), ip);
3635 BINOP_CAST(l, -, guint64);
3637 MINT_IN_CASE(MINT_ENDFINALLY)
3639 ip = finally_ips->data;
3640 finally_ips = g_slist_remove (finally_ips, ip);
3647 MINT_IN_CASE(MINT_LEAVE) /* Fall through */
3648 MINT_IN_CASE(MINT_LEAVE_S)
3649 while (sp > frame->stack) {
3653 if (*ip == MINT_LEAVE_S) {
3654 ip += (short) *(ip + 1);
3656 ip += (gint32) READ32 (ip + 1);
3659 if (frame->ex_handler != NULL && MONO_OFFSET_IN_HANDLER(frame->ex_handler, frame->ip - rtm->code)) {
3660 frame->ex_handler = NULL;
3663 goto handle_finally;
3665 MINT_IN_CASE(MINT_ICALL_V_V)
3666 MINT_IN_CASE(MINT_ICALL_V_P)
3667 MINT_IN_CASE(MINT_ICALL_P_V)
3668 MINT_IN_CASE(MINT_ICALL_P_P)
3669 MINT_IN_CASE(MINT_ICALL_PP_V)
3670 MINT_IN_CASE(MINT_ICALL_PI_V)
3671 MINT_IN_CASE(MINT_ICALL_PP_P)
3672 MINT_IN_CASE(MINT_ICALL_PI_P)
3673 MINT_IN_CASE(MINT_ICALL_PPP_V)
3674 MINT_IN_CASE(MINT_ICALL_PPI_V)
3675 sp = do_icall (context, *ip, sp, rtm->data_items [*(guint16 *)(ip + 1)]);
3676 if (frame->ex != NULL)
3677 goto handle_exception;
3680 MINT_IN_CASE(MINT_MONO_LDPTR)
3681 sp->data.p = rtm->data_items [*(guint16 *)(ip + 1)];
3685 MINT_IN_CASE(MINT_MONO_NEWOBJ)
3686 sp->data.p = mono_object_new_checked (context->domain, rtm->data_items [*(guint16 *)(ip + 1)], &error);
3687 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3691 MINT_IN_CASE(MINT_MONO_FREE)
3694 g_error ("that doesn't seem right");
3695 g_free (sp->data.p);
3697 MINT_IN_CASE(MINT_MONO_RETOBJ)
3700 stackval_from_data (mono_method_signature (frame->runtime_method->method)->ret, frame->retval, sp->data.p,
3701 mono_method_signature (frame->runtime_method->method)->pinvoke);
3702 if (sp > frame->stack)
3703 g_warning ("retobj: more values on stack: %d", sp-frame->stack);
3706 #define RELOP(datamem, op) \
3708 sp [-1].data.i = sp [-1].data.datamem op sp [0].data.datamem; \
3710 MINT_IN_CASE(MINT_CEQ_I4)
3713 MINT_IN_CASE(MINT_CEQ0_I4)
3714 sp [-1].data.i = (sp [-1].data.i == 0);
3717 MINT_IN_CASE(MINT_CEQ_I8)
3720 MINT_IN_CASE(MINT_CEQ_R8)
3722 if (isunordered (sp [-1].data.f, sp [0].data.f))
3725 sp [-1].data.i = sp [-1].data.f == sp [0].data.f;
3728 MINT_IN_CASE(MINT_CGT_I4)
3731 MINT_IN_CASE(MINT_CGT_I8)
3734 MINT_IN_CASE(MINT_CGT_R8)
3736 if (isunordered (sp [-1].data.f, sp [0].data.f))
3739 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3743 #define RELOP_CAST(datamem, op, type) \
3745 sp [-1].data.i = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
3748 MINT_IN_CASE(MINT_CGT_UN_I4)
3749 RELOP_CAST(i, >, guint32);
3751 MINT_IN_CASE(MINT_CGT_UN_I8)
3752 RELOP_CAST(l, >, guint64);
3754 MINT_IN_CASE(MINT_CGT_UN_R8)
3756 if (isunordered (sp [-1].data.f, sp [0].data.f))
3759 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3762 MINT_IN_CASE(MINT_CLT_I4)
3765 MINT_IN_CASE(MINT_CLT_I8)
3768 MINT_IN_CASE(MINT_CLT_R8)
3770 if (isunordered (sp [-1].data.f, sp [0].data.f))
3773 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3776 MINT_IN_CASE(MINT_CLT_UN_I4)
3777 RELOP_CAST(i, <, guint32);
3779 MINT_IN_CASE(MINT_CLT_UN_I8)
3780 RELOP_CAST(l, <, guint64);
3782 MINT_IN_CASE(MINT_CLT_UN_R8)
3784 if (isunordered (sp [-1].data.f, sp [0].data.f))
3787 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3790 MINT_IN_CASE(MINT_LDFTN) {
3791 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
3796 MINT_IN_CASE(MINT_LDVIRTFTN) {
3797 RuntimeMethod *m = rtm->data_items [* (guint16 *)(ip + 1)];
3801 THROW_EX (mono_get_exception_null_reference (), ip - 2);
3803 sp->data.p = get_virtual_method (context->domain, m, sp->data.p);
3808 MINT_IN_CASE(MINT_LDTHIS)
3809 sp->data.p = frame->obj;
3813 MINT_IN_CASE(MINT_STTHIS)
3815 frame->obj = sp->data.p;
3818 MINT_IN_CASE(MINT_LDTHISA)
3819 sp->data.p = &frame->obj;
3824 #define LDARG(datamem, argtype) \
3825 sp->data.datamem = * (argtype *)(frame->args + * (guint16 *)(ip + 1)); \
3829 MINT_IN_CASE(MINT_LDARG_I1) LDARG(i, gint8); MINT_IN_BREAK;
3830 MINT_IN_CASE(MINT_LDARG_U1) LDARG(i, guint8); MINT_IN_BREAK;
3831 MINT_IN_CASE(MINT_LDARG_I2) LDARG(i, gint16); MINT_IN_BREAK;
3832 MINT_IN_CASE(MINT_LDARG_U2) LDARG(i, guint16); MINT_IN_BREAK;
3833 MINT_IN_CASE(MINT_LDARG_I4) LDARG(i, gint32); MINT_IN_BREAK;
3834 MINT_IN_CASE(MINT_LDARG_I8) LDARG(l, gint64); MINT_IN_BREAK;
3835 MINT_IN_CASE(MINT_LDARG_R4) LDARG(f, float); MINT_IN_BREAK;
3836 MINT_IN_CASE(MINT_LDARG_R8) LDARG(f, double); MINT_IN_BREAK;
3837 MINT_IN_CASE(MINT_LDARG_O) LDARG(p, gpointer); MINT_IN_BREAK;
3838 MINT_IN_CASE(MINT_LDARG_P) LDARG(p, gpointer); MINT_IN_BREAK;
3840 MINT_IN_CASE(MINT_LDARG_VT)
3842 i32 = READ32(ip + 2);
3843 memcpy(sp->data.p, frame->args + * (guint16 *)(ip + 1), i32);
3844 vt_sp += (i32 + 7) & ~7;
3849 #define STARG(datamem, argtype) \
3851 * (argtype *)(frame->args + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3854 MINT_IN_CASE(MINT_STARG_I1) STARG(i, gint8); MINT_IN_BREAK;
3855 MINT_IN_CASE(MINT_STARG_U1) STARG(i, guint8); MINT_IN_BREAK;
3856 MINT_IN_CASE(MINT_STARG_I2) STARG(i, gint16); MINT_IN_BREAK;
3857 MINT_IN_CASE(MINT_STARG_U2) STARG(i, guint16); MINT_IN_BREAK;
3858 MINT_IN_CASE(MINT_STARG_I4) STARG(i, gint32); MINT_IN_BREAK;
3859 MINT_IN_CASE(MINT_STARG_I8) STARG(l, gint64); MINT_IN_BREAK;
3860 MINT_IN_CASE(MINT_STARG_R4) STARG(f, float); MINT_IN_BREAK;
3861 MINT_IN_CASE(MINT_STARG_R8) STARG(f, double); MINT_IN_BREAK;
3862 MINT_IN_CASE(MINT_STARG_O) STARG(p, gpointer); MINT_IN_BREAK;
3863 MINT_IN_CASE(MINT_STARG_P) STARG(p, gpointer); MINT_IN_BREAK;
3865 MINT_IN_CASE(MINT_STARG_VT)
3866 i32 = READ32(ip + 2);
3868 memcpy(frame->args + * (guint16 *)(ip + 1), sp->data.p, i32);
3869 vt_sp -= (i32 + 7) & ~7;
3873 #define STINARG(datamem, argtype) \
3875 int n = * (guint16 *)(ip + 1); \
3876 * (argtype *)(frame->args + rtm->arg_offsets [n]) = frame->stack_args [n].data.datamem; \
3880 MINT_IN_CASE(MINT_STINARG_I1) STINARG(i, gint8); MINT_IN_BREAK;
3881 MINT_IN_CASE(MINT_STINARG_U1) STINARG(i, guint8); MINT_IN_BREAK;
3882 MINT_IN_CASE(MINT_STINARG_I2) STINARG(i, gint16); MINT_IN_BREAK;
3883 MINT_IN_CASE(MINT_STINARG_U2) STINARG(i, guint16); MINT_IN_BREAK;
3884 MINT_IN_CASE(MINT_STINARG_I4) STINARG(i, gint32); MINT_IN_BREAK;
3885 MINT_IN_CASE(MINT_STINARG_I8) STINARG(l, gint64); MINT_IN_BREAK;
3886 MINT_IN_CASE(MINT_STINARG_R4) STINARG(f, float); MINT_IN_BREAK;
3887 MINT_IN_CASE(MINT_STINARG_R8) STINARG(f, double); MINT_IN_BREAK;
3888 MINT_IN_CASE(MINT_STINARG_O) STINARG(p, gpointer); MINT_IN_BREAK;
3889 MINT_IN_CASE(MINT_STINARG_P) STINARG(p, gpointer); MINT_IN_BREAK;
3891 MINT_IN_CASE(MINT_STINARG_VT) {
3892 int n = * (guint16 *)(ip + 1);
3893 i32 = READ32(ip + 2);
3894 memcpy (frame->args + rtm->arg_offsets [n], frame->stack_args [n].data.p, i32);
3899 MINT_IN_CASE(MINT_LDARGA)
3900 sp->data.p = frame->args + * (guint16 *)(ip + 1);
3905 #define LDLOC(datamem, argtype) \
3906 sp->data.datamem = * (argtype *)(locals + * (guint16 *)(ip + 1)); \
3910 MINT_IN_CASE(MINT_LDLOC_I1) LDLOC(i, gint8); MINT_IN_BREAK;
3911 MINT_IN_CASE(MINT_LDLOC_U1) LDLOC(i, guint8); MINT_IN_BREAK;
3912 MINT_IN_CASE(MINT_LDLOC_I2) LDLOC(i, gint16); MINT_IN_BREAK;
3913 MINT_IN_CASE(MINT_LDLOC_U2) LDLOC(i, guint16); MINT_IN_BREAK;
3914 MINT_IN_CASE(MINT_LDLOC_I4) LDLOC(i, gint32); MINT_IN_BREAK;
3915 MINT_IN_CASE(MINT_LDLOC_I8) LDLOC(l, gint64); MINT_IN_BREAK;
3916 MINT_IN_CASE(MINT_LDLOC_R4) LDLOC(f, float); MINT_IN_BREAK;
3917 MINT_IN_CASE(MINT_LDLOC_R8) LDLOC(f, double); MINT_IN_BREAK;
3918 MINT_IN_CASE(MINT_LDLOC_O) LDLOC(p, gpointer); MINT_IN_BREAK;
3919 MINT_IN_CASE(MINT_LDLOC_P) LDLOC(p, gpointer); MINT_IN_BREAK;
3921 MINT_IN_CASE(MINT_LDLOC_VT)
3923 i32 = READ32(ip + 2);
3924 memcpy(sp->data.p, locals + * (guint16 *)(ip + 1), i32);
3925 vt_sp += (i32 + 7) & ~7;
3930 MINT_IN_CASE(MINT_LDLOCA_S)
3931 sp->data.p = locals + * (guint16 *)(ip + 1);
3936 #define STLOC(datamem, argtype) \
3938 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3941 MINT_IN_CASE(MINT_STLOC_I1) STLOC(i, gint8); MINT_IN_BREAK;
3942 MINT_IN_CASE(MINT_STLOC_U1) STLOC(i, guint8); MINT_IN_BREAK;
3943 MINT_IN_CASE(MINT_STLOC_I2) STLOC(i, gint16); MINT_IN_BREAK;
3944 MINT_IN_CASE(MINT_STLOC_U2) STLOC(i, guint16); MINT_IN_BREAK;
3945 MINT_IN_CASE(MINT_STLOC_I4) STLOC(i, gint32); MINT_IN_BREAK;
3946 MINT_IN_CASE(MINT_STLOC_I8) STLOC(l, gint64); MINT_IN_BREAK;
3947 MINT_IN_CASE(MINT_STLOC_R4) STLOC(f, float); MINT_IN_BREAK;
3948 MINT_IN_CASE(MINT_STLOC_R8) STLOC(f, double); MINT_IN_BREAK;
3949 MINT_IN_CASE(MINT_STLOC_O) STLOC(p, gpointer); MINT_IN_BREAK;
3950 MINT_IN_CASE(MINT_STLOC_P) STLOC(p, gpointer); MINT_IN_BREAK;
3952 #define STLOC_NP(datamem, argtype) \
3953 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp [-1].data.datamem; \
3956 MINT_IN_CASE(MINT_STLOC_NP_I4) STLOC_NP(i, gint32); MINT_IN_BREAK;
3957 MINT_IN_CASE(MINT_STLOC_NP_O) STLOC_NP(p, gpointer); MINT_IN_BREAK;
3959 MINT_IN_CASE(MINT_STLOC_VT)
3960 i32 = READ32(ip + 2);
3962 memcpy(locals + * (guint16 *)(ip + 1), sp->data.p, i32);
3963 vt_sp -= (i32 + 7) & ~7;
3967 MINT_IN_CASE(MINT_LOCALLOC)
3968 if (sp != frame->stack + 1) /*FIX?*/
3969 THROW_EX (mono_get_exception_execution_engine (NULL), ip);
3970 sp [-1].data.p = alloca (sp [-1].data.i);
3974 MINT_IN_CASE(MINT_ENDFILTER) ves_abort(); MINT_IN_BREAK;
3976 MINT_IN_CASE(MINT_INITOBJ)
3978 memset (sp->data.vt, 0, READ32(ip + 1));
3981 MINT_IN_CASE(MINT_CPBLK)
3983 if (!sp [0].data.p || !sp [1].data.p)
3984 THROW_EX (mono_get_exception_null_reference(), ip - 1);
3986 /* FIXME: value and size may be int64... */
3987 memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
3990 MINT_IN_CASE(MINT_CONSTRAINED_) {
3992 /* FIXME: implement */
3994 token = READ32 (ip);
3999 MINT_IN_CASE(MINT_INITBLK)
4002 THROW_EX (mono_get_exception_null_reference(), ip - 1);
4004 /* FIXME: value and size may be int64... */
4005 memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
4008 MINT_IN_CASE(MINT_NO_)
4009 /* FIXME: implement */
4013 MINT_IN_CASE(MINT_RETHROW)
4015 * need to clarify what this should actually do:
4016 * start the search from the last found handler in
4017 * this method or continue in the caller or what.
4018 * Also, do we need to run finally/fault handlers after a retrow?
4019 * Well, this implementation will follow the usual search
4020 * for an handler, considering the current ip as throw spot.
4021 * We need to NULL frame->ex_handler for the later code to
4022 * actually run the new found handler.
4024 frame->ex_handler = NULL;
4025 THROW_EX (frame->ex, ip - 1);
4028 g_print ("Unimplemented opcode: %04x %s at 0x%x\n", *ip, mono_interp_opname[*ip], ip-rtm->code);
4029 THROW_EX (mono_get_exception_execution_engine ("Unimplemented opcode"), ip);
4033 g_assert_not_reached ();
4035 * Exception handling code.
4036 * The exception object is stored in frame->ex.
4043 MonoInvocation *inv;
4044 MonoExceptionClause *clause;
4050 g_print ("* Handling exception '%s' at IL_%04x\n",
4051 frame->ex == NULL ? "** Unknown **" : mono_object_class (frame->ex)->name,
4052 rtm == NULL ? 0 : frame->ip - rtm->code);
4054 if (die_on_exception)
4057 for (inv = frame; inv; inv = inv->parent) {
4059 if (inv->runtime_method == NULL)
4061 method = inv->runtime_method->method;
4062 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
4064 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
4066 if (inv->ip == NULL)
4068 ip_offset = inv->ip - inv->runtime_method->code;
4069 inv->ex_handler = NULL; /* clear this in case we are trhowing an exception while handling one - this one wins */
4070 for (i = 0; i < inv->runtime_method->num_clauses; ++i) {
4071 clause = &inv->runtime_method->clauses [i];
4072 if (clause->flags <= 1 && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4073 if (!clause->flags) {
4074 MonoObject *isinst_obj = mono_object_isinst_checked ((MonoObject*)frame->ex, clause->data.catch_class, &error);
4075 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4078 * OK, we found an handler, now we need to execute the finally
4079 * and fault blocks before branching to the handler code.
4081 inv->ex_handler = clause;
4084 g_print ("* Found handler at '%s'\n", method->name);
4086 goto handle_finally;
4089 /* FIXME: handle filter clauses */
4096 * If we get here, no handler was found: print a stack trace.
4098 for (inv = frame; inv; inv = inv->parent) {
4099 if (inv->invoke_trap)
4100 goto handle_finally;
4103 ex_obj = (MonoObject*)frame->ex;
4104 mono_unhandled_exception (ex_obj);
4111 MonoExceptionClause *clause;
4112 GSList *old_list = finally_ips;
4113 MonoMethod *method = frame->runtime_method->method;
4114 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
4115 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4119 g_print ("* Handle finally IL_%04x\n", endfinally_ip == NULL ? 0 : endfinally_ip - rtm->code);
4121 if (rtm == NULL || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
4122 || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
4125 ip_offset = frame->ip - rtm->code;
4127 if (endfinally_ip != NULL)
4128 finally_ips = g_slist_prepend(finally_ips, (void *)endfinally_ip);
4129 for (i = 0; i < header->num_clauses; ++i)
4130 if (frame->ex_handler == &rtm->clauses [i])
4134 clause = &rtm->clauses [i];
4135 if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && (endfinally_ip == NULL || !(MONO_OFFSET_IN_CLAUSE (clause, endfinally_ip - rtm->code)))) {
4136 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
4137 ip = rtm->code + clause->handler_offset;
4138 finally_ips = g_slist_prepend (finally_ips, (gpointer) ip);
4141 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
4147 endfinally_ip = NULL;
4149 if (old_list != finally_ips && finally_ips) {
4150 ip = finally_ips->data;
4151 finally_ips = g_slist_remove (finally_ips, ip);
4152 sp = frame->stack; /* spec says stack should be empty at endfinally so it should be at the start too */
4157 * If an exception is set, we need to execute the fault handler, too,
4158 * otherwise, we continue normally.
4168 MonoExceptionClause *clause;
4169 MonoMethod *method = frame->runtime_method->method;
4170 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
4171 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4175 g_print ("* Handle fault\n");
4177 ip_offset = frame->ip - rtm->code;
4178 for (i = 0; i < header->num_clauses; ++i) {
4179 clause = &rtm->clauses [i];
4180 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4181 ip = rtm->code + clause->handler_offset;
4184 g_print ("* Executing handler at IL_%04x\n", clause->handler_offset);
4190 * If the handler for the exception was found in this method, we jump
4191 * to it right away, otherwise we return and let the caller run
4192 * the finally, fault and catch blocks.
4193 * This same code should be present in the endfault opcode, but it
4194 * is corrently not assigned in the ECMA specs: LAMESPEC.
4196 if (frame->ex_handler) {
4199 g_print ("* Executing handler at IL_%04x\n", frame->ex_handler->handler_offset);
4201 ip = rtm->code + frame->ex_handler->handler_offset;
4203 vt_sp = (unsigned char *) sp + rtm->stack_size;
4204 sp->data.p = frame->ex;
4215 ves_exec_method (MonoInvocation *frame)
4217 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4218 ThreadContext context_struct;
4225 mono_unhandled_exception ((MonoObject*)frame->ex);
4228 if (context == NULL) {
4229 context = &context_struct;
4230 context_struct.domain = mono_domain_get ();
4231 context_struct.base_frame = frame;
4232 context_struct.current_frame = NULL;
4233 context_struct.env_frame = frame;
4234 context_struct.current_env = &env;
4235 context_struct.search_for_handler = 0;
4236 context_struct.managed_code = 0;
4237 mono_native_tls_set_value (thread_context_id, context);
4240 frame->parent = context->current_frame;
4241 frame->runtime_method = mono_interp_get_runtime_method (context->domain, frame->method, &error);
4242 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4243 context->managed_code = 1;
4244 ves_exec_method_with_context (frame, context);
4245 context->managed_code = 0;
4247 if (context != &context_struct && context->current_env) {
4248 context->env_frame->ex = frame->ex;
4249 longjmp (*context->current_env, 1);
4252 mono_unhandled_exception ((MonoObject*)frame->ex);
4254 if (context->base_frame == frame)
4255 mono_native_tls_set_value (thread_context_id, NULL);
4257 context->current_frame = frame->parent;
4261 ves_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4263 MonoImage *image = mono_assembly_get_image (assembly);
4268 method = mono_get_method_checked (image, mono_image_get_entry_point (image), NULL, NULL, &error);
4269 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4272 g_error ("No entry point method found in %s", mono_image_get_filename (image));
4274 rval = mono_runtime_run_main_checked (method, argc, argv, &error);
4275 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4283 "mint %s, the Mono ECMA CLI interpreter, (C) 2001, 2002 Ximian, Inc.\n\n"
4284 "Usage is: mint [options] executable args...\n\n", VERSION);
4286 "Runtime Debugging:\n"
4291 " --noptr\t\t\tdon't print pointer addresses in trace output\n"
4294 " --traceclassinit\n"
4297 " --debug method_name\n"
4304 " --config filename load the specified config file instead of the default\n"
4305 " --workers n maximum number of worker threads\n"
4311 add_signal_handler (int signo, void (*handler)(int))
4314 signal (signo, handler);
4316 struct sigaction sa;
4318 sa.sa_handler = handler;
4319 sigemptyset (&sa.sa_mask);
4322 g_assert (sigaction (signo, &sa, NULL) != -1);
4327 segv_handler (int signum)
4329 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4330 MonoException *segv_exception;
4332 if (context == NULL)
4334 segv_exception = mono_get_exception_null_reference ();
4335 segv_exception->message = mono_string_new (mono_domain_get (), "Null Reference (SIGSEGV)");
4336 mono_raise_exception (segv_exception);
4341 quit_handler (int signum)
4343 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4344 MonoException *quit_exception;
4346 if (context == NULL)
4348 quit_exception = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
4349 mono_raise_exception (quit_exception);
4353 abrt_handler (int signum)
4355 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4356 MonoException *abrt_exception;
4358 if (context == NULL)
4360 abrt_exception = mono_get_exception_execution_engine ("Abort (SIGABRT).");
4361 mono_raise_exception (abrt_exception);
4366 thread_abort_handler (int signum)
4368 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4371 if (context == NULL)
4374 exc = mono_thread_request_interruption (context->managed_code);
4375 if (exc) mono_raise_exception (exc);
4380 interp_ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
4381 MonoReflectionMethod **method,
4382 gint32 *iloffset, gint32 *native_offset,
4383 MonoString **file, gint32 *line, gint32 *column)
4385 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4386 MonoInvocation *inv = context->current_frame;
4390 for (i = 0; inv && i < skip; inv = inv->parent)
4391 if (inv->runtime_method != NULL)
4402 *method = mono_method_get_object_checked (context->domain, inv->runtime_method->method, NULL, &error);
4403 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4408 if (need_file_info) {
4412 *file = mono_string_new (mono_domain_get (), "unknown");
4419 interp_ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
4421 MonoDomain *domain = mono_domain_get ();
4423 MonoArray *ta = exc->trace_ips;
4428 /* Exception is not thrown yet */
4429 MonoArray *array = mono_array_new_checked (domain, mono_defaults.stack_frame_class, 0, &error);
4430 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4434 len = mono_array_length (ta);
4436 res = mono_array_new_checked (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0, &error);
4437 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4439 for (i = skip; i < len / 2; i++) {
4440 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new_checked (domain, mono_defaults.stack_frame_class, &error);
4441 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4442 gushort *ip = mono_array_get (ta, gpointer, 2 * i + 1);
4443 RuntimeMethod *rtm = mono_array_get (ta, gpointer, 2 * i);
4446 sf->method = mono_method_get_object_checked (domain, rtm->method, NULL, &error);
4447 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4448 sf->native_offset = ip - rtm->code;
4452 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
4454 if (need_file_info) {
4457 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
4459 sf->filename = filename? mono_string_new (domain, filename): NULL;
4466 mono_array_set (res, gpointer, i, sf);
4473 ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionType *type, MonoObject *target, MonoReflectionMethod *info)
4475 MonoClass *delegate_class = mono_class_from_mono_type (type->type);
4476 MonoObject *delegate;
4479 g_assert (delegate_class->parent == mono_defaults.multicastdelegate_class);
4481 delegate = mono_object_new_checked (mono_object_domain (type), delegate_class, &error);
4482 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4484 interp_delegate_ctor (mono_object_domain (type), delegate, target, mono_interp_get_runtime_method (mono_get_root_domain (), info->method, &error));
4485 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4494 int enable_debugging;
4500 static void main_thread_handler (gpointer user_data)
4502 MainThreadArgs *main_args=(MainThreadArgs *)user_data;
4503 MonoAssembly *assembly;
4505 if (main_args->enable_debugging) {
4506 mono_debug_init (MONO_DEBUG_FORMAT_MONO);
4509 assembly = mono_domain_assembly_open (main_args->domain, main_args->file);
4512 fprintf (stderr, "Can not open image %s\n", main_args->file);
4516 ves_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
4520 interp_mono_runtime_install_handlers (void)
4522 add_signal_handler (SIGSEGV, segv_handler);
4523 add_signal_handler (SIGINT, quit_handler);
4524 add_signal_handler (SIGABRT, abrt_handler);
4526 add_signal_handler (mono_thread_get_abort_signal (), thread_abort_handler);
4531 quit_function (MonoDomain *domain, gpointer user_data)
4533 mono_profiler_shutdown ();
4535 mono_runtime_cleanup (domain);
4536 mono_domain_free (domain, TRUE);
4541 mono_interp_cleanup(MonoDomain *domain)
4543 quit_function (domain, NULL);
4547 mono_interp_exec(MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4549 return ves_exec (domain, assembly, argc, argv);
4553 interp_get_imt_trampoline (MonoVTable *vtable, int imt_slot_index)
4555 // FIXME: implement me
4560 interp_create_ftnptr (MonoDomain *domain, gpointer addr)
4562 // FIXME: true on all arch?
4568 mono_current_thread_has_handle_block_guard (void)
4575 mono_above_abort_threshold (void)
4581 mono_interp_init(const char *file)
4584 MonoRuntimeCallbacks callbacks;
4585 MonoRuntimeExceptionHandlingCallbacks ecallbacks;
4588 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
4589 g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
4591 mono_native_tls_alloc (&thread_context_id, NULL);
4592 mono_native_tls_set_value (thread_context_id, NULL);
4593 mono_os_mutex_init_recursive (&runtime_method_lookup_section);
4594 mono_os_mutex_init_recursive (&create_method_pointer_mutex);
4596 mono_tls_init_runtime_keys ();
4598 // TODO: use callbacks?
4599 interp_mono_runtime_install_handlers ();
4600 mono_interp_transform_init ();
4602 memset (&callbacks, 0, sizeof (callbacks));
4603 // TODO: replace with `mono_install_callbacks`
4604 callbacks.compile_method = mono_create_method_pointer;
4605 callbacks.runtime_invoke = interp_mono_runtime_invoke;
4606 callbacks.get_imt_trampoline = interp_get_imt_trampoline;
4607 callbacks.create_ftnptr = interp_create_ftnptr;
4608 #ifndef DISABLE_REMOTING
4609 callbacks.create_remoting_trampoline = interp_create_remoting_trampoline;
4611 callbacks.create_jit_trampoline = interp_create_trampoline;
4612 mono_install_callbacks (&callbacks);
4615 memset (&ecallbacks, 0, sizeof (ecallbacks));
4616 ecallbacks.mono_raise_exception = interp_ex_handler;
4617 ecallbacks.mono_current_thread_has_handle_block_guard = mono_current_thread_has_handle_block_guard;
4618 ecallbacks.mono_above_abort_threshold = mono_above_abort_threshold;
4621 mono_install_stack_walk (interp_walk_stack);
4623 mono_install_eh_callbacks (&ecallbacks);
4625 mono_install_runtime_cleanup (quit_function);
4627 abort_requested = mono_thread_interruption_request_flag ();
4629 domain = mono_init_from_assembly (file, file);
4630 #ifdef __hpux /* generates very big stack frames */
4631 mono_threads_set_default_stacksize(32*1024*1024);
4634 /* TODO: this should use ves_icall_get_frame from mini-exceptions.c? */
4635 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info", interp_ves_icall_get_frame_info);
4636 /* TODO: this should use ves_icall_get_trace from mini-exceptions.c? */
4637 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace", interp_ves_icall_get_trace);
4638 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers", interp_mono_runtime_install_handlers);
4639 mono_add_internal_call ("System.Delegate::CreateDelegate_internal", ves_icall_System_Delegate_CreateDelegate_internal);
4641 mono_register_jit_icall (mono_thread_interruption_checkpoint, "mono_thread_interruption_checkpoint", mono_create_icall_signature ("object"), FALSE);
4643 mono_runtime_init_checked (domain, NULL, NULL, &error);
4644 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4646 mono_thread_attach (domain);
4650 typedef int (*TestMethod) (void);
4653 interp_regression_step (MonoImage *image, int verbose, int *total_run, int *total, GTimer *timer, MonoDomain *domain)
4655 int result, expected, failed, cfailed, run;
4656 double elapsed, transform_time;
4658 MonoObject *result_obj;
4659 static gboolean filter_method_init = FALSE;
4660 static const char *filter_method = NULL;
4662 g_print ("Test run: image=%s\n", mono_image_get_filename (image));
4663 cfailed = failed = run = 0;
4664 transform_time = elapsed = 0.0;
4667 /* fixme: ugly hack - delete all previously compiled methods */
4668 if (domain_jit_info (domain)) {
4669 g_hash_table_destroy (domain_jit_info (domain)->jit_trampoline_hash);
4670 domain_jit_info (domain)->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
4671 mono_internal_hash_table_destroy (&(domain->jit_code_hash));
4672 mono_jit_code_hash_init (&(domain->jit_code_hash));
4676 g_timer_start (timer);
4677 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
4679 MonoMethod *method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
4681 mono_error_cleanup (&error); /* FIXME don't swallow the error */
4685 if (!filter_method_init) {
4686 filter_method = g_getenv ("INTERP_FILTER_METHOD");
4687 filter_method_init = TRUE;
4689 gboolean filter = FALSE;
4690 if (filter_method) {
4691 const char *name = filter_method;
4693 if ((strchr (name, '.') > name) || strchr (name, ':')) {
4694 MonoMethodDesc *desc = mono_method_desc_new (name, TRUE);
4695 filter = mono_method_desc_full_match (desc, method);
4696 mono_method_desc_free (desc);
4698 filter = strcmp (method->name, name) == 0;
4700 } else { // no filter
4703 if (strncmp (method->name, "test_", 5) == 0 && filter) {
4704 MonoError interp_error;
4705 MonoObject *exc = NULL;
4707 result_obj = interp_mono_runtime_invoke (method, NULL, NULL, &exc, &interp_error);
4708 if (!mono_error_ok (&interp_error)) {
4710 g_print ("Test '%s' execution failed.\n", method->name);
4711 } else if (exc != NULL) {
4712 g_print ("Exception in Test '%s' occured:\n", method->name);
4713 mono_object_describe (exc);
4717 result = *(gint32 *) mono_object_unbox (result_obj);
4718 expected = atoi (method->name + 5); // FIXME: oh no.
4721 if (result != expected) {
4723 g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
4728 g_timer_stop (timer);
4729 elapsed = g_timer_elapsed (timer, NULL);
4730 if (failed > 0 || cfailed > 0){
4731 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
4732 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
4734 g_print ("Results: total tests: %d, all pass \n", run);
4737 g_print ("Elapsed time: %f secs (%f, %f)\n\n", elapsed,
4738 elapsed - transform_time, transform_time);
4739 *total += failed + cfailed;
4743 interp_regression (MonoImage *image, int verbose, int *total_run)
4746 GTimer *timer = g_timer_new ();
4747 MonoDomain *domain = mono_domain_get ();
4751 /* load the metadata */
4752 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
4754 method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
4756 mono_error_cleanup (&error);
4759 mono_class_init (method->klass);
4764 interp_regression_step (image, verbose, total_run, &total, timer, domain);
4766 g_timer_destroy (timer);
4771 interp_regression_list (int verbose, int count, char *images [])
4773 int i, total, total_run, run;
4775 total_run = total = 0;
4776 for (i = 0; i < count; ++i) {
4777 MonoAssembly *ass = mono_assembly_open (images [i], NULL);
4779 g_warning ("failed to load assembly: %s", images [i]);
4782 total += interp_regression (mono_assembly_get_image (ass), verbose, &run);
4786 g_print ("Overall results: tests: %d, failed: %d (pass: %.2f%%)\n", total_run, total, 100.0*(total_run-total)/total_run);
4788 g_print ("Overall results: tests: %d, 100%% pass\n", total_run);