[interp] Remove address-taking of vt_sp. (#16215)
[mono-project.git] / mono / mini / interp / interp.c
blob5661068421a99b3191b3621a7afc0b08b5626b4a
1 /**
2 * \file
4 * interp.c: Interpreter for CIL byte codes
6 * Authors:
7 * Paolo Molaro (lupus@ximian.com)
8 * Miguel de Icaza (miguel@ximian.com)
9 * Dietmar Maurer (dietmar@ximian.com)
11 * (C) 2001, 2002 Ximian, Inc.
13 #ifndef __USE_ISOC99
14 #define __USE_ISOC99
15 #endif
16 #include "config.h"
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <glib.h>
22 #include <math.h>
23 #include <locale.h>
25 #include <mono/utils/gc_wrapper.h>
26 #include <mono/utils/mono-math.h>
27 #include <mono/utils/mono-counters.h>
29 #ifdef HAVE_ALLOCA_H
30 # include <alloca.h>
31 #else
32 # ifdef __CYGWIN__
33 # define alloca __builtin_alloca
34 # endif
35 #endif
37 /* trim excessive headers */
38 #include <mono/metadata/image.h>
39 #include <mono/metadata/assembly-internals.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/exception.h>
51 #include <mono/metadata/verify.h>
52 #include <mono/metadata/opcodes.h>
53 #include <mono/metadata/debug-helpers.h>
54 #include <mono/metadata/mono-config.h>
55 #include <mono/metadata/marshal.h>
56 #include <mono/metadata/environment.h>
57 #include <mono/metadata/mono-debug.h>
58 #include <mono/metadata/gc-internals.h>
59 #include <mono/utils/atomic.h>
61 #include "interp.h"
62 #include "interp-internals.h"
63 #include "mintops.h"
65 #include <mono/mini/mini.h>
66 #include <mono/mini/mini-runtime.h>
67 #include <mono/mini/aot-runtime.h>
68 #include <mono/mini/llvm-runtime.h>
69 #include <mono/mini/llvmonly-runtime.h>
70 #include <mono/mini/jit-icalls.h>
71 #include <mono/mini/debugger-agent.h>
72 #include <mono/mini/ee.h>
73 #include <mono/mini/trace.h>
75 #ifdef TARGET_ARM
76 #include <mono/mini/mini-arm.h>
77 #endif
78 #include <mono/metadata/icall-decl.h>
80 #ifdef _MSC_VER
81 #pragma warning(disable:4102) // label' : unreferenced label
82 #endif
84 /* Arguments that are passed when invoking only a finally/filter clause from the frame */
85 typedef struct {
86 /* Where we start the frame execution from */
87 guint16 *start_with_ip;
89 * End ip of the exit_clause. We need it so we know whether the resume
90 * state is for this frame (which is called from EH) or for the original
91 * frame further down the stack.
93 guint16 *end_at_ip;
94 /* When exiting this clause we also exit the frame */
95 int exit_clause;
96 /* Exception that we are filtering */
97 MonoException *filter_exception;
98 InterpFrame *base_frame;
99 } FrameClauseArgs;
101 static inline void
102 init_frame (InterpFrame *frame, InterpFrame *parent_frame, InterpMethod *rmethod, stackval *method_args, stackval *method_retval)
104 frame->parent = parent_frame;
105 frame->stack_args = method_args;
106 frame->retval = method_retval;
107 frame->imethod = rmethod;
108 frame->ex = NULL;
109 frame->ip = NULL;
112 #define interp_exec_method(frame, context, error) interp_exec_method_full ((frame), (context), NULL, error)
115 * List of classes whose methods will be executed by transitioning to JITted code.
116 * Used for testing.
118 GSList *mono_interp_jit_classes;
119 /* Optimizations enabled with interpreter */
120 int mono_interp_opt = INTERP_OPT_INLINE;
121 /* If TRUE, interpreted code will be interrupted at function entry/backward branches */
122 static gboolean ss_enabled;
124 static gboolean interp_init_done = FALSE;
126 static void interp_exec_method_full (InterpFrame *frame, ThreadContext *context, FrameClauseArgs *clause_args, MonoError *error);
127 static InterpMethod* lookup_method_pointer (gpointer addr);
129 typedef void (*ICallMethod) (InterpFrame *frame);
131 static MonoNativeTlsKey thread_context_id;
133 #define DEBUG_INTERP 0
134 #define COUNT_OPS 0
136 #if DEBUG_INTERP
137 int mono_interp_traceopt = 2;
138 /* If true, then we output the opcodes as we interpret them */
139 static int global_tracing = 2;
141 static int debug_indent_level = 0;
143 static int break_on_method = 0;
144 static int nested_trace = 0;
145 static GList *db_methods = NULL;
146 static char* dump_args (InterpFrame *inv);
148 static void
149 output_indent (void)
151 int h;
153 for (h = 0; h < debug_indent_level; h++)
154 g_print (" ");
157 static void
158 db_match_method (gpointer data, gpointer user_data)
160 MonoMethod *m = (MonoMethod*)user_data;
161 MonoMethodDesc *desc = data;
163 if (mono_method_desc_full_match (desc, m))
164 break_on_method = 1;
167 static void
168 debug_enter (InterpFrame *frame, int *tracing)
170 if (db_methods) {
171 g_list_foreach (db_methods, db_match_method, (gpointer)frame->imethod->method);
172 if (break_on_method)
173 *tracing = nested_trace ? (global_tracing = 2, 3) : 2;
174 break_on_method = 0;
176 if (*tracing) {
177 MonoMethod *method = frame->imethod->method;
178 char *mn, *args = dump_args (frame);
179 debug_indent_level++;
180 output_indent ();
181 mn = mono_method_full_name (method, FALSE);
182 g_print ("(%p) Entering %s (", mono_thread_internal_current (), mn);
183 g_free (mn);
184 g_print ("%s)\n", args);
185 g_free (args);
190 #define DEBUG_LEAVE() \
191 if (tracing) { \
192 char *mn, *args; \
193 args = dump_retval (frame); \
194 output_indent (); \
195 mn = mono_method_full_name (frame->imethod->method, FALSE); \
196 g_print ("(%p) Leaving %s", mono_thread_internal_current (), mn); \
197 g_free (mn); \
198 g_print (" => %s\n", args); \
199 g_free (args); \
200 debug_indent_level--; \
201 if (tracing == 3) global_tracing = 0; \
204 #else
206 int mono_interp_traceopt = 0;
207 #define DEBUG_LEAVE()
209 #endif
211 #if defined(__GNUC__) && !defined(TARGET_WASM)
212 #define USE_COMPUTED_GOTO 1
213 #endif
214 #if USE_COMPUTED_GOTO
215 #define MINT_IN_SWITCH(op) COUNT_OP(op); goto *in_labels[op];
216 #define MINT_IN_CASE(x) LAB_ ## x:
217 #define MINT_IN_DISPATCH(op) goto *in_labels[op];
218 #if DEBUG_INTERP
219 #define MINT_IN_BREAK if (tracing > 1) { MINT_IN_DISPATCH(*ip); } else { COUNT_OP(*ip); goto *in_labels[*ip]; }
220 #else
221 #define MINT_IN_BREAK { COUNT_OP(*ip); goto *in_labels[*ip]; }
222 #endif
223 #define MINT_IN_DEFAULT mint_default: if (0) goto mint_default; /* make gcc shut up */
224 #else
225 #define MINT_IN_SWITCH(op) switch (op)
226 #define MINT_IN_CASE(x) case x:
227 #define MINT_IN_DISPATCH(op) goto main_loop;
228 #define MINT_IN_BREAK break
229 #define MINT_IN_DEFAULT default:
230 #endif
232 static MONO_NEVER_INLINE void // Inlining this causes caller to use more stack.
233 set_resume_state (ThreadContext *context, InterpFrame *frame)
235 /* We have thrown an exception from a finally block. Some of the leave targets were unwinded already */
236 while (frame->finally_ips &&
237 frame->finally_ips->data >= context->handler_ei->try_start &&
238 frame->finally_ips->data < context->handler_ei->try_end)
239 frame->finally_ips = g_slist_remove (frame->finally_ips, frame->finally_ips->data);
240 frame->ex = NULL;
241 context->has_resume_state = 0;
242 context->handler_frame = NULL;
243 context->handler_ei = NULL;
246 /* Set the current execution state to the resume state in context */
247 #define SET_RESUME_STATE(context) do { \
248 ip = (const guint16*)(context)->handler_ip; \
249 /* spec says stack should be empty at endfinally so it should be at the start too */ \
250 sp = frame->stack; \
251 vt_sp = (unsigned char *) sp + imethod->stack_size; \
252 if (frame->ex) { \
253 sp->data.p = frame->ex; \
254 ++sp; \
256 set_resume_state ((context), (frame)); \
257 MINT_IN_DISPATCH(*ip); \
258 } while (0)
261 * If this bit is set, it means the call has thrown the exception, and we
262 * reached this point because the EH code in mono_handle_exception ()
263 * unwound all the JITted frames below us. mono_interp_set_resume_state ()
264 * has set the fields in context to indicate where we have to resume execution.
266 #define CHECK_RESUME_STATE(context) do { \
267 if ((context)->has_resume_state) { \
268 if (frame == (context)->handler_frame && (!clause_args || (context)->handler_ip < clause_args->end_at_ip)) \
269 SET_RESUME_STATE (context); \
270 else \
271 goto exit_frame; \
273 } while (0);
275 static void
276 set_context (ThreadContext *context)
278 mono_native_tls_set_value (thread_context_id, context);
280 if (!context)
281 return;
283 MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
284 g_assertf (jit_tls, "ThreadContext needs initialized JIT TLS");
286 /* jit_tls assumes ownership of 'context' */
287 jit_tls->interp_context = context;
290 static ThreadContext *
291 get_context (void)
293 ThreadContext *context = (ThreadContext *) mono_native_tls_get_value (thread_context_id);
294 if (context == NULL) {
295 context = g_new0 (ThreadContext, 1);
296 set_context (context);
298 return context;
301 static MONO_NEVER_INLINE void
302 ves_real_abort (int line, MonoMethod *mh,
303 const unsigned short *ip, stackval *stack, stackval *sp)
305 ERROR_DECL (error);
306 MonoMethodHeader *header = mono_method_get_header_checked (mh, error);
307 mono_error_cleanup (error); /* FIXME: don't swallow the error */
308 g_printerr ("Execution aborted in method: %s::%s\n", m_class_get_name (mh->klass), mh->name);
309 g_printerr ("Line=%d IP=0x%04lx, Aborted execution\n", line, ip-(const unsigned short *) header->code);
310 g_printerr ("0x%04x %02x\n", ip-(const unsigned short *) header->code, *ip);
311 mono_metadata_free_mh (header);
314 #define ves_abort() \
315 do {\
316 ves_real_abort(__LINE__, frame->imethod->method, ip, frame->stack, sp); \
317 THROW_EX (mono_get_exception_execution_engine (NULL), ip); \
318 } while (0);
320 static InterpMethod*
321 lookup_imethod (MonoDomain *domain, MonoMethod *method)
323 InterpMethod *imethod;
324 MonoJitDomainInfo *info;
326 info = domain_jit_info (domain);
327 mono_domain_jit_code_hash_lock (domain);
328 imethod = (InterpMethod*)mono_internal_hash_table_lookup (&info->interp_code_hash, method);
329 mono_domain_jit_code_hash_unlock (domain);
330 return imethod;
333 static gpointer
334 interp_get_remoting_invoke (MonoMethod *method, gpointer addr, MonoError *error)
336 #ifndef DISABLE_REMOTING
337 InterpMethod *imethod;
339 if (addr) {
340 imethod = lookup_method_pointer (addr);
341 } else {
342 g_assert (method);
343 imethod = mono_interp_get_imethod (mono_domain_get (), method, error);
344 return_val_if_nok (error, NULL);
346 g_assert (imethod);
347 g_assert (mono_use_interpreter);
349 MonoMethod *remoting_invoke_method = mono_marshal_get_remoting_invoke (imethod->method, error);
350 return_val_if_nok (error, NULL);
351 return mono_interp_get_imethod (mono_domain_get (), remoting_invoke_method, error);
352 #else
353 g_assert_not_reached ();
354 return NULL;
355 #endif
358 InterpMethod*
359 mono_interp_get_imethod (MonoDomain *domain, MonoMethod *method, MonoError *error)
361 InterpMethod *imethod;
362 MonoJitDomainInfo *info;
363 MonoMethodSignature *sig;
364 int i;
366 error_init (error);
368 info = domain_jit_info (domain);
369 mono_domain_jit_code_hash_lock (domain);
370 imethod = (InterpMethod*)mono_internal_hash_table_lookup (&info->interp_code_hash, method);
371 mono_domain_jit_code_hash_unlock (domain);
372 if (imethod)
373 return imethod;
375 sig = mono_method_signature_internal (method);
377 imethod = (InterpMethod*)mono_domain_alloc0 (domain, sizeof (InterpMethod));
378 imethod->method = method;
379 imethod->domain = domain;
380 imethod->param_count = sig->param_count;
381 imethod->hasthis = sig->hasthis;
382 imethod->vararg = sig->call_convention == MONO_CALL_VARARG;
383 imethod->rtype = mini_get_underlying_type (sig->ret);
384 imethod->param_types = (MonoType**)mono_domain_alloc0 (domain, sizeof (MonoType*) * sig->param_count);
385 for (i = 0; i < sig->param_count; ++i)
386 imethod->param_types [i] = mini_get_underlying_type (sig->params [i]);
388 mono_domain_jit_code_hash_lock (domain);
389 if (!mono_internal_hash_table_lookup (&info->interp_code_hash, method))
390 mono_internal_hash_table_insert (&info->interp_code_hash, method, imethod);
391 mono_domain_jit_code_hash_unlock (domain);
393 imethod->prof_flags = mono_profiler_get_call_instrumentation_flags (imethod->method);
395 return imethod;
398 #if defined (MONO_CROSS_COMPILE) || defined (HOST_WASM)
399 #define INTERP_PUSH_LMF_WITH_CTX_BODY(ext, exit_label) \
400 (ext).kind = MONO_LMFEXT_INTERP_EXIT;
402 #elif defined(MONO_ARCH_HAS_NO_PROPER_MONOCTX)
403 /* some platforms, e.g. appleTV, don't provide us a precise MonoContext
404 * (registers are not accurate), thus resuming to the label does not work. */
405 #define INTERP_PUSH_LMF_WITH_CTX_BODY(ext, exit_label) \
406 (ext).kind = MONO_LMFEXT_INTERP_EXIT;
407 #elif defined (_MSC_VER)
408 #define INTERP_PUSH_LMF_WITH_CTX_BODY(ext, exit_label) \
409 (ext).kind = MONO_LMFEXT_INTERP_EXIT_WITH_CTX; \
410 (ext).interp_exit_label_set = FALSE; \
411 MONO_CONTEXT_GET_CURRENT ((ext).ctx); \
412 if ((ext).interp_exit_label_set == FALSE) \
413 mono_arch_do_ip_adjustment (&(ext).ctx); \
414 if ((ext).interp_exit_label_set == TRUE) \
415 goto exit_label; \
416 (ext).interp_exit_label_set = TRUE;
417 #elif defined(MONO_ARCH_HAS_MONO_CONTEXT)
418 #define INTERP_PUSH_LMF_WITH_CTX_BODY(ext, exit_label) \
419 (ext).kind = MONO_LMFEXT_INTERP_EXIT_WITH_CTX; \
420 MONO_CONTEXT_GET_CURRENT ((ext).ctx); \
421 MONO_CONTEXT_SET_IP (&(ext).ctx, (&&exit_label)); \
422 mono_arch_do_ip_adjustment (&(ext).ctx);
423 #else
424 #define INTERP_PUSH_LMF_WITH_CTX_BODY(ext, exit_label) g_error ("requires working mono-context");
425 #endif
427 /* INTERP_PUSH_LMF_WITH_CTX:
429 * same as interp_push_lmf, but retrieving and attaching MonoContext to it.
430 * This is needed to resume into the interp when the exception is thrown from
431 * native code (see ./mono/tests/install_eh_callback.exe).
433 * This must be a macro in order to retrieve the right register values for
434 * MonoContext.
436 #define INTERP_PUSH_LMF_WITH_CTX(frame, ext, exit_label) \
437 memset (&(ext), 0, sizeof (MonoLMFExt)); \
438 (ext).interp_exit_data = (frame); \
439 INTERP_PUSH_LMF_WITH_CTX_BODY ((ext), exit_label); \
440 mono_push_lmf (&(ext));
443 * interp_push_lmf:
445 * Push an LMF frame on the LMF stack
446 * to mark the transition to native code.
447 * This is needed for the native code to
448 * be able to do stack walks.
450 static void
451 interp_push_lmf (MonoLMFExt *ext, InterpFrame *frame)
453 memset (ext, 0, sizeof (MonoLMFExt));
454 ext->kind = MONO_LMFEXT_INTERP_EXIT;
455 ext->interp_exit_data = frame;
457 mono_push_lmf (ext);
460 static void
461 interp_pop_lmf (MonoLMFExt *ext)
463 mono_pop_lmf (&ext->lmf);
466 static MONO_NEVER_INLINE InterpMethod*
467 get_virtual_method (InterpMethod *imethod, MonoVTable *vtable)
469 MonoMethod *m = imethod->method;
470 MonoDomain *domain = imethod->domain;
471 InterpMethod *ret = NULL;
472 ERROR_DECL (error);
474 #ifndef DISABLE_REMOTING
475 if (mono_class_is_transparent_proxy (vtable->klass)) {
476 MonoMethod *remoting_invoke_method = mono_marshal_get_remoting_invoke_with_check (m, error);
477 mono_error_assert_ok (error);
478 ret = mono_interp_get_imethod (domain, remoting_invoke_method, error);
479 mono_error_assert_ok (error);
480 return ret;
482 #endif
484 if ((m->flags & METHOD_ATTRIBUTE_FINAL) || !(m->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
485 if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
486 ret = mono_interp_get_imethod (domain, mono_marshal_get_synchronized_wrapper (m), error);
487 mono_error_cleanup (error); /* FIXME: don't swallow the error */
488 } else {
489 ret = imethod;
491 return ret;
494 mono_class_setup_vtable (vtable->klass);
496 int slot = mono_method_get_vtable_slot (m);
497 if (mono_class_is_interface (m->klass)) {
498 g_assert (vtable->klass != m->klass);
499 /* TODO: interface offset lookup is slow, go through IMT instead */
500 gboolean non_exact_match;
501 slot += mono_class_interface_offset_with_variance (vtable->klass, m->klass, &non_exact_match);
504 MonoMethod *virtual_method = m_class_get_vtable (vtable->klass) [slot];
505 if (m->is_inflated && mono_method_get_context (m)->method_inst) {
506 MonoGenericContext context = { NULL, NULL };
508 if (mono_class_is_ginst (virtual_method->klass))
509 context.class_inst = mono_class_get_generic_class (virtual_method->klass)->context.class_inst;
510 else if (mono_class_is_gtd (virtual_method->klass))
511 context.class_inst = mono_class_get_generic_container (virtual_method->klass)->context.class_inst;
512 context.method_inst = mono_method_get_context (m)->method_inst;
514 virtual_method = mono_class_inflate_generic_method_checked (virtual_method, &context, error);
515 mono_error_cleanup (error); /* FIXME: don't swallow the error */
518 if (virtual_method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
519 virtual_method = mono_marshal_get_native_wrapper (virtual_method, FALSE, FALSE);
522 if (virtual_method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
523 virtual_method = mono_marshal_get_synchronized_wrapper (virtual_method);
526 InterpMethod *virtual_imethod = mono_interp_get_imethod (domain, virtual_method, error);
527 mono_error_cleanup (error); /* FIXME: don't swallow the error */
528 return virtual_imethod;
531 typedef struct {
532 InterpMethod *imethod;
533 InterpMethod *target_imethod;
534 } InterpVTableEntry;
536 /* domain lock must be held */
537 static GSList*
538 append_imethod (MonoDomain *domain, GSList *list, InterpMethod *imethod, InterpMethod *target_imethod)
540 GSList *ret;
541 InterpVTableEntry *entry;
543 entry = (InterpVTableEntry*) mono_mempool_alloc (domain->mp, sizeof (InterpVTableEntry));
544 entry->imethod = imethod;
545 entry->target_imethod = target_imethod;
546 ret = g_slist_append_mempool (domain->mp, list, entry);
548 return ret;
551 static InterpMethod*
552 get_target_imethod (GSList *list, InterpMethod *imethod)
554 while (list != NULL) {
555 InterpVTableEntry *entry = (InterpVTableEntry*) list->data;
556 if (entry->imethod == imethod)
557 return entry->target_imethod;
558 list = list->next;
560 return NULL;
563 static gpointer*
564 get_method_table (MonoVTable *vtable, int offset)
566 if (offset >= 0)
567 return vtable->interp_vtable;
568 else
569 return (gpointer*)vtable;
572 static gpointer*
573 alloc_method_table (MonoVTable *vtable, int offset)
575 gpointer *table;
577 if (offset >= 0) {
578 table = mono_domain_alloc0 (vtable->domain, m_class_get_vtable_size (vtable->klass) * sizeof (gpointer));
579 vtable->interp_vtable = table;
580 } else {
581 table = (gpointer*)vtable;
584 return table;
587 static MONO_NEVER_INLINE InterpMethod* // Inlining causes additional stack use in caller.
588 get_virtual_method_fast (InterpMethod *imethod, MonoVTable *vtable, int offset)
590 gpointer *table;
592 #ifndef DISABLE_REMOTING
593 /* FIXME Remoting */
594 if (mono_class_is_transparent_proxy (vtable->klass))
595 return get_virtual_method (imethod, vtable);
596 #endif
598 table = get_method_table (vtable, offset);
600 if (!table) {
601 /* Lazily allocate method table */
602 mono_domain_lock (vtable->domain);
603 table = get_method_table (vtable, offset);
604 if (!table)
605 table = alloc_method_table (vtable, offset);
606 mono_domain_unlock (vtable->domain);
609 if (!table [offset]) {
610 InterpMethod *target_imethod = get_virtual_method (imethod, vtable);
611 /* Lazily initialize the method table slot */
612 mono_domain_lock (vtable->domain);
613 if (!table [offset]) {
614 if (imethod->method->is_inflated || offset < 0)
615 table [offset] = append_imethod (vtable->domain, NULL, imethod, target_imethod);
616 else
617 table [offset] = (gpointer) ((gsize)target_imethod | 0x1);
619 mono_domain_unlock (vtable->domain);
622 if ((gsize)table [offset] & 0x1) {
623 /* Non generic virtual call. Only one method in slot */
624 return (InterpMethod*) ((gsize)table [offset] & ~0x1);
625 } else {
626 /* Virtual generic or interface call. Multiple methods in slot */
627 InterpMethod *target_imethod = get_target_imethod ((GSList*)table [offset], imethod);
629 if (!target_imethod) {
630 target_imethod = get_virtual_method (imethod, vtable);
631 mono_domain_lock (vtable->domain);
632 if (!get_target_imethod ((GSList*)table [offset], imethod))
633 table [offset] = append_imethod (vtable->domain, (GSList*)table [offset], imethod, target_imethod);
634 mono_domain_unlock (vtable->domain);
636 return target_imethod;
640 static void inline
641 stackval_from_data (MonoType *type_, stackval *result, void *data, gboolean pinvoke)
643 MonoType *type = mini_native_type_replace_type (type_);
644 if (type->byref) {
645 switch (type->type) {
646 case MONO_TYPE_OBJECT:
647 case MONO_TYPE_CLASS:
648 case MONO_TYPE_STRING:
649 case MONO_TYPE_ARRAY:
650 case MONO_TYPE_SZARRAY:
651 break;
652 default:
653 break;
655 result->data.p = *(gpointer*)data;
656 return;
658 switch (type->type) {
659 case MONO_TYPE_VOID:
660 return;
661 case MONO_TYPE_I1:
662 result->data.i = *(gint8*)data;
663 return;
664 case MONO_TYPE_U1:
665 case MONO_TYPE_BOOLEAN:
666 result->data.i = *(guint8*)data;
667 return;
668 case MONO_TYPE_I2:
669 result->data.i = *(gint16*)data;
670 return;
671 case MONO_TYPE_U2:
672 case MONO_TYPE_CHAR:
673 result->data.i = *(guint16*)data;
674 return;
675 case MONO_TYPE_I4:
676 result->data.i = *(gint32*)data;
677 return;
678 case MONO_TYPE_U:
679 case MONO_TYPE_I:
680 result->data.nati = *(mono_i*)data;
681 return;
682 case MONO_TYPE_PTR:
683 result->data.p = *(gpointer*)data;
684 return;
685 case MONO_TYPE_U4:
686 result->data.i = *(guint32*)data;
687 return;
688 case MONO_TYPE_R4:
689 /* memmove handles unaligned case */
690 memmove (&result->data.f_r4, data, sizeof (float));
691 return;
692 case MONO_TYPE_I8:
693 case MONO_TYPE_U8:
694 memmove (&result->data.l, data, sizeof (gint64));
695 return;
696 case MONO_TYPE_R8:
697 memmove (&result->data.f, data, sizeof (double));
698 return;
699 case MONO_TYPE_STRING:
700 case MONO_TYPE_SZARRAY:
701 case MONO_TYPE_CLASS:
702 case MONO_TYPE_OBJECT:
703 case MONO_TYPE_ARRAY:
704 result->data.p = *(gpointer*)data;
705 return;
706 case MONO_TYPE_VALUETYPE:
707 if (m_class_is_enumtype (type->data.klass)) {
708 stackval_from_data (mono_class_enum_basetype_internal (type->data.klass), result, data, pinvoke);
709 return;
710 } else if (pinvoke) {
711 memcpy (result->data.vt, data, mono_class_native_size (type->data.klass, NULL));
712 } else {
713 mono_value_copy_internal (result->data.vt, data, type->data.klass);
715 return;
716 case MONO_TYPE_GENERICINST: {
717 if (mono_type_generic_inst_is_valuetype (type)) {
718 mono_value_copy_internal (result->data.vt, data, mono_class_from_mono_type_internal (type));
719 return;
721 stackval_from_data (m_class_get_byval_arg (type->data.generic_class->container_class), result, data, pinvoke);
722 return;
724 default:
725 g_error ("got type 0x%02x", type->type);
729 static void inline
730 stackval_to_data (MonoType *type_, stackval *val, void *data, gboolean pinvoke)
732 MonoType *type = mini_native_type_replace_type (type_);
733 if (type->byref) {
734 gpointer *p = (gpointer*)data;
735 *p = val->data.p;
736 return;
738 /* printf ("TODAT0 %p\n", data); */
739 switch (type->type) {
740 case MONO_TYPE_I1:
741 case MONO_TYPE_U1: {
742 guint8 *p = (guint8*)data;
743 *p = val->data.i;
744 return;
746 case MONO_TYPE_BOOLEAN: {
747 guint8 *p = (guint8*)data;
748 *p = (val->data.i != 0);
749 return;
751 case MONO_TYPE_I2:
752 case MONO_TYPE_U2:
753 case MONO_TYPE_CHAR: {
754 guint16 *p = (guint16*)data;
755 *p = val->data.i;
756 return;
758 case MONO_TYPE_I: {
759 mono_i *p = (mono_i*)data;
760 /* In theory the value used by stloc should match the local var type
761 but in practice it sometimes doesn't (a int32 gets dup'd and stloc'd into
762 a native int - both by csc and mcs). Not sure what to do about sign extension
763 as it is outside the spec... doing the obvious */
764 *p = (mono_i)val->data.nati;
765 return;
767 case MONO_TYPE_U: {
768 mono_u *p = (mono_u*)data;
769 /* see above. */
770 *p = (mono_u)val->data.nati;
771 return;
773 case MONO_TYPE_I4:
774 case MONO_TYPE_U4: {
775 gint32 *p = (gint32*)data;
776 *p = val->data.i;
777 return;
779 case MONO_TYPE_I8:
780 case MONO_TYPE_U8: {
781 memmove (data, &val->data.l, sizeof (gint64));
782 return;
784 case MONO_TYPE_R4: {
785 /* memmove handles unaligned case */
786 memmove (data, &val->data.f_r4, sizeof (float));
787 return;
789 case MONO_TYPE_R8: {
790 memmove (data, &val->data.f, sizeof (double));
791 return;
793 case MONO_TYPE_STRING:
794 case MONO_TYPE_SZARRAY:
795 case MONO_TYPE_CLASS:
796 case MONO_TYPE_OBJECT:
797 case MONO_TYPE_ARRAY: {
798 gpointer *p = (gpointer *) data;
799 mono_gc_wbarrier_generic_store_internal (p, val->data.o);
800 return;
802 case MONO_TYPE_PTR: {
803 gpointer *p = (gpointer *) data;
804 *p = val->data.p;
805 return;
807 case MONO_TYPE_VALUETYPE:
808 if (m_class_is_enumtype (type->data.klass)) {
809 stackval_to_data (mono_class_enum_basetype_internal (type->data.klass), val, data, pinvoke);
810 return;
811 } else if (pinvoke) {
812 memcpy (data, val->data.vt, mono_class_native_size (type->data.klass, NULL));
813 } else {
814 mono_value_copy_internal (data, val->data.vt, type->data.klass);
816 return;
817 case MONO_TYPE_GENERICINST: {
818 MonoClass *container_class = type->data.generic_class->container_class;
820 if (m_class_is_valuetype (container_class) && !m_class_is_enumtype (container_class)) {
821 mono_value_copy_internal (data, val->data.vt, mono_class_from_mono_type_internal (type));
822 return;
824 stackval_to_data (m_class_get_byval_arg (type->data.generic_class->container_class), val, data, pinvoke);
825 return;
827 default:
828 g_error ("got type %x", type->type);
833 * Same as stackval_to_data but return address of storage instead
834 * of copying the value.
836 static gpointer
837 stackval_to_data_addr (MonoType *type_, stackval *val)
839 MonoType *type = mini_native_type_replace_type (type_);
840 if (type->byref)
841 return &val->data.p;
843 switch (type->type) {
844 case MONO_TYPE_I1:
845 case MONO_TYPE_U1:
846 case MONO_TYPE_BOOLEAN:
847 case MONO_TYPE_I2:
848 case MONO_TYPE_U2:
849 case MONO_TYPE_CHAR:
850 case MONO_TYPE_I4:
851 case MONO_TYPE_U4:
852 return &val->data.i;
853 case MONO_TYPE_I:
854 case MONO_TYPE_U:
855 return &val->data.nati;
856 case MONO_TYPE_I8:
857 case MONO_TYPE_U8:
858 return &val->data.l;
859 case MONO_TYPE_R4:
860 return &val->data.f_r4;
861 case MONO_TYPE_R8:
862 return &val->data.f;
863 case MONO_TYPE_STRING:
864 case MONO_TYPE_SZARRAY:
865 case MONO_TYPE_CLASS:
866 case MONO_TYPE_OBJECT:
867 case MONO_TYPE_ARRAY:
868 case MONO_TYPE_PTR:
869 return &val->data.p;
870 case MONO_TYPE_VALUETYPE:
871 if (m_class_is_enumtype (type->data.klass))
872 return stackval_to_data_addr (mono_class_enum_basetype_internal (type->data.klass), val);
873 else
874 return val->data.vt;
875 case MONO_TYPE_TYPEDBYREF:
876 return val->data.vt;
877 case MONO_TYPE_GENERICINST: {
878 MonoClass *container_class = type->data.generic_class->container_class;
880 if (m_class_is_valuetype (container_class) && !m_class_is_enumtype (container_class))
881 return val->data.vt;
882 return stackval_to_data_addr (m_class_get_byval_arg (type->data.generic_class->container_class), val);
884 default:
885 g_error ("got type %x", type->type);
890 * interp_throw:
891 * Throw an exception from the interpreter.
893 static MONO_NEVER_INLINE void
894 interp_throw (ThreadContext *context, MonoException *ex, InterpFrame *frame, gconstpointer ip, gboolean rethrow)
896 ERROR_DECL (error);
897 MonoLMFExt ext;
899 interp_push_lmf (&ext, frame);
900 frame->ip = (const guint16*)ip;
901 frame->ex = ex;
903 if (mono_object_isinst_checked ((MonoObject *) ex, mono_defaults.exception_class, error)) {
904 MonoException *mono_ex = (MonoException *) ex;
905 if (!rethrow) {
906 mono_ex->stack_trace = NULL;
907 mono_ex->trace_ips = NULL;
910 mono_error_assert_ok (error);
912 MonoContext ctx;
913 memset (&ctx, 0, sizeof (MonoContext));
914 MONO_CONTEXT_SET_SP (&ctx, frame);
917 * Call the JIT EH code. The EH code will call back to us using:
918 * - mono_interp_set_resume_state ()/run_finally ()/run_filter ().
919 * Since ctx.ip is 0, this will start unwinding from the LMF frame
920 * pushed above, which points to our frames.
922 mono_handle_exception (&ctx, (MonoObject*)ex);
923 if (MONO_CONTEXT_GET_IP (&ctx) != 0) {
924 /* We need to unwind into non-interpreter code */
925 mono_restore_context (&ctx);
926 g_assert_not_reached ();
929 interp_pop_lmf (&ext);
931 g_assert (context->has_resume_state);
934 #define THROW_EX_GENERAL(exception,ex_ip, rethrow) \
935 do { \
936 interp_throw (context, (exception), (frame), (ex_ip), (rethrow)); \
937 CHECK_RESUME_STATE(context); \
938 } while (0)
940 #define THROW_EX(exception,ex_ip) THROW_EX_GENERAL ((exception), (ex_ip), FALSE)
942 #define THROW_EX_OVF(ip) THROW_EX (mono_get_exception_overflow (), ip)
944 #define THROW_EX_DIV_ZERO(ip) THROW_EX (mono_get_exception_divide_by_zero (), ip)
946 #define NULL_CHECK(o) do { \
947 if (G_UNLIKELY (!(o))) \
948 THROW_EX (mono_get_exception_null_reference (), ip); \
949 } while (0)
951 #define EXCEPTION_CHECKPOINT \
952 do { \
953 if (*mono_thread_interruption_request_flag () && !mono_threads_is_critical_method (imethod->method)) { \
954 MonoException *exc = mono_thread_interruption_checkpoint (); \
955 if (exc) \
956 THROW_EX (exc, ip); \
958 } while (0)
961 static MonoObject*
962 ves_array_create (MonoDomain *domain, MonoClass *klass, int param_count, stackval *values, MonoError *error)
964 uintptr_t *lengths;
965 intptr_t *lower_bounds;
966 int i;
968 lengths = g_newa (uintptr_t, m_class_get_rank (klass) * 2);
969 for (i = 0; i < param_count; ++i) {
970 lengths [i] = values->data.i;
971 values ++;
973 if (m_class_get_rank (klass) == param_count) {
974 /* Only lengths provided. */
975 lower_bounds = NULL;
976 } else {
977 /* lower bounds are first. */
978 lower_bounds = (intptr_t *) lengths;
979 lengths += m_class_get_rank (klass);
981 return (MonoObject*) mono_array_new_full_checked (domain, klass, lengths, lower_bounds, error);
984 static gint32
985 ves_array_calculate_index (MonoArray *ao, stackval *sp, InterpFrame *frame, gboolean safe)
987 g_assert (!frame->ex);
988 MonoClass *ac = ((MonoObject *) ao)->vtable->klass;
990 guint32 pos = 0;
991 if (ao->bounds) {
992 for (gint32 i = 0; i < m_class_get_rank (ac); i++) {
993 guint32 idx = sp [i].data.i;
994 guint32 lower = ao->bounds [i].lower_bound;
995 guint32 len = ao->bounds [i].length;
996 if (safe && (idx < lower || (idx - lower) >= len)) {
997 frame->ex = mono_get_exception_index_out_of_range ();
998 return -1;
1000 pos = (pos * len) + idx - lower;
1002 } else {
1003 pos = sp [0].data.i;
1004 if (safe && pos >= ao->max_length) {
1005 frame->ex = mono_get_exception_index_out_of_range ();
1006 return -1;
1009 return pos;
1012 static MONO_NEVER_INLINE void
1013 ves_array_set (InterpFrame *frame, stackval *sp, MonoMethodSignature *sig)
1015 MonoObject *o = sp->data.o;
1016 MonoArray *ao = (MonoArray *) o;
1017 MonoClass *ac = o->vtable->klass;
1019 g_assert (m_class_get_rank (ac) >= 1);
1021 gint32 pos = ves_array_calculate_index (ao, sp + 1, frame, TRUE);
1022 if (frame->ex)
1023 return;
1025 int val_index = 1 + m_class_get_rank (ac);
1026 if (sp [val_index].data.p && !m_class_is_valuetype (m_class_get_element_class (mono_object_class (o)))) {
1027 ERROR_DECL (error);
1028 MonoObject *isinst = mono_object_isinst_checked (sp [val_index].data.o, m_class_get_element_class (mono_object_class (o)), error);
1029 mono_error_cleanup (error);
1030 if (!isinst) {
1031 frame->ex = mono_get_exception_array_type_mismatch ();
1032 return;
1036 gint32 esize = mono_array_element_size (ac);
1037 gpointer ea = mono_array_addr_with_size_fast (ao, esize, pos);
1039 MonoType *mt = sig->params [m_class_get_rank (ac)];
1040 stackval_to_data (mt, &sp [val_index], ea, FALSE);
1043 static void
1044 ves_array_get (InterpFrame *frame, stackval *sp, stackval *retval, MonoMethodSignature *sig, gboolean safe)
1046 MonoObject *o = sp->data.o;
1047 MonoArray *ao = (MonoArray *) o;
1048 MonoClass *ac = o->vtable->klass;
1050 g_assert (m_class_get_rank (ac) >= 1);
1052 gint32 pos = ves_array_calculate_index (ao, sp + 1, frame, safe);
1053 if (frame->ex)
1054 return;
1056 gint32 esize = mono_array_element_size (ac);
1057 gpointer ea = mono_array_addr_with_size_fast (ao, esize, pos);
1059 MonoType *mt = sig->ret;
1060 stackval_from_data (mt, retval, ea, FALSE);
1063 static gpointer
1064 ves_array_element_address (InterpFrame *frame, MonoClass *required_type, MonoArray *ao, stackval *sp, gboolean needs_typecheck)
1066 MonoClass *ac = ((MonoObject *) ao)->vtable->klass;
1068 g_assert (m_class_get_rank (ac) >= 1);
1070 gint32 pos = ves_array_calculate_index (ao, sp, frame, TRUE);
1071 if (frame->ex)
1072 return NULL;
1074 if (needs_typecheck && !mono_class_is_assignable_from_internal (m_class_get_element_class (mono_object_class ((MonoObject *) ao)), required_type)) {
1075 frame->ex = mono_get_exception_array_type_mismatch ();
1076 return NULL;
1078 gint32 esize = mono_array_element_size (ac);
1079 return mono_array_addr_with_size_fast (ao, esize, pos);
1082 #ifdef MONO_ARCH_HAVE_INTERP_ENTRY_TRAMPOLINE
1083 static MonoFuncV mono_native_to_interp_trampoline = NULL;
1084 #endif
1086 #ifndef MONO_ARCH_HAVE_INTERP_PINVOKE_TRAMP
1087 static InterpMethodArguments* build_args_from_sig (MonoMethodSignature *sig, InterpFrame *frame)
1089 InterpMethodArguments *margs = g_malloc0 (sizeof (InterpMethodArguments));
1091 #ifdef TARGET_ARM
1092 g_assert (mono_arm_eabi_supported ());
1093 int i8_align = mono_arm_i8_align ();
1094 #endif
1096 #ifdef TARGET_WASM
1097 margs->sig = sig;
1098 #endif
1100 if (sig->hasthis)
1101 margs->ilen++;
1103 for (int i = 0; i < sig->param_count; i++) {
1104 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
1105 switch (ptype) {
1106 case MONO_TYPE_BOOLEAN:
1107 case MONO_TYPE_CHAR:
1108 case MONO_TYPE_I1:
1109 case MONO_TYPE_U1:
1110 case MONO_TYPE_I2:
1111 case MONO_TYPE_U2:
1112 case MONO_TYPE_I4:
1113 case MONO_TYPE_U4:
1114 case MONO_TYPE_I:
1115 case MONO_TYPE_U:
1116 case MONO_TYPE_PTR:
1117 case MONO_TYPE_SZARRAY:
1118 case MONO_TYPE_CLASS:
1119 case MONO_TYPE_OBJECT:
1120 case MONO_TYPE_STRING:
1121 case MONO_TYPE_VALUETYPE:
1122 case MONO_TYPE_GENERICINST:
1123 #if SIZEOF_VOID_P == 8
1124 case MONO_TYPE_I8:
1125 case MONO_TYPE_U8:
1126 #endif
1127 margs->ilen++;
1128 break;
1129 #if SIZEOF_VOID_P == 4
1130 case MONO_TYPE_I8:
1131 case MONO_TYPE_U8:
1132 #ifdef TARGET_ARM
1133 /* pairs begin at even registers */
1134 if (i8_align == 8 && margs->ilen & 1)
1135 margs->ilen++;
1136 #endif
1137 margs->ilen += 2;
1138 break;
1139 #endif
1140 case MONO_TYPE_R4:
1141 #if SIZEOF_VOID_P == 8
1142 case MONO_TYPE_R8:
1143 #endif
1144 margs->flen++;
1145 break;
1146 #if SIZEOF_VOID_P == 4
1147 case MONO_TYPE_R8:
1148 margs->flen += 2;
1149 break;
1150 #endif
1151 default:
1152 g_error ("build_args_from_sig: not implemented yet (1): 0x%x\n", ptype);
1156 if (margs->ilen > 0)
1157 margs->iargs = g_malloc0 (sizeof (gpointer) * margs->ilen);
1159 if (margs->flen > 0)
1160 margs->fargs = g_malloc0 (sizeof (double) * margs->flen);
1162 if (margs->ilen > INTERP_ICALL_TRAMP_IARGS)
1163 g_error ("build_args_from_sig: TODO, allocate gregs: %d\n", margs->ilen);
1165 if (margs->flen > INTERP_ICALL_TRAMP_FARGS)
1166 g_error ("build_args_from_sig: TODO, allocate fregs: %d\n", margs->flen);
1169 size_t int_i = 0;
1170 size_t int_f = 0;
1172 if (sig->hasthis) {
1173 margs->iargs [0] = frame->stack_args->data.p;
1174 int_i++;
1177 for (int i = 0; i < sig->param_count; i++) {
1178 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
1179 switch (ptype) {
1180 case MONO_TYPE_BOOLEAN:
1181 case MONO_TYPE_CHAR:
1182 case MONO_TYPE_I1:
1183 case MONO_TYPE_U1:
1184 case MONO_TYPE_I2:
1185 case MONO_TYPE_U2:
1186 case MONO_TYPE_I4:
1187 case MONO_TYPE_U4:
1188 case MONO_TYPE_I:
1189 case MONO_TYPE_U:
1190 case MONO_TYPE_PTR:
1191 case MONO_TYPE_SZARRAY:
1192 case MONO_TYPE_CLASS:
1193 case MONO_TYPE_OBJECT:
1194 case MONO_TYPE_STRING:
1195 case MONO_TYPE_VALUETYPE:
1196 case MONO_TYPE_GENERICINST:
1197 #if SIZEOF_VOID_P == 8
1198 case MONO_TYPE_I8:
1199 case MONO_TYPE_U8:
1200 #endif
1201 margs->iargs [int_i] = frame->stack_args [i].data.p;
1202 #if DEBUG_INTERP
1203 g_print ("build_args_from_sig: margs->iargs [%d]: %p (frame @ %d)\n", int_i, margs->iargs [int_i], i);
1204 #endif
1205 int_i++;
1206 break;
1207 #if SIZEOF_VOID_P == 4
1208 case MONO_TYPE_I8:
1209 case MONO_TYPE_U8: {
1210 stackval *sarg = &frame->stack_args [i];
1211 #ifdef TARGET_ARM
1212 /* pairs begin at even registers */
1213 if (i8_align == 8 && int_i & 1)
1214 int_i++;
1215 #endif
1216 margs->iargs [int_i] = (gpointer) sarg->data.pair.lo;
1217 int_i++;
1218 margs->iargs [int_i] = (gpointer) sarg->data.pair.hi;
1219 #if DEBUG_INTERP
1220 g_print ("build_args_from_sig: margs->iargs [%d/%d]: 0x%016llx, hi=0x%08x lo=0x%08x (frame @ %d)\n", int_i - 1, int_i, *((guint64 *) &margs->iargs [int_i - 1]), sarg->data.pair.hi, sarg->data.pair.lo, i);
1221 #endif
1222 int_i++;
1223 break;
1225 #endif
1226 case MONO_TYPE_R4:
1227 case MONO_TYPE_R8:
1228 if (ptype == MONO_TYPE_R4)
1229 * (float *) &(margs->fargs [int_f]) = frame->stack_args [i].data.f_r4;
1230 else
1231 margs->fargs [int_f] = frame->stack_args [i].data.f;
1232 #if DEBUG_INTERP
1233 g_print ("build_args_from_sig: margs->fargs [%d]: %p (%f) (frame @ %d)\n", int_f, margs->fargs [int_f], margs->fargs [int_f], i);
1234 #endif
1235 #if SIZEOF_VOID_P == 4
1236 int_f += 2;
1237 #else
1238 int_f++;
1239 #endif
1240 break;
1241 default:
1242 g_error ("build_args_from_sig: not implemented yet (2): 0x%x\n", ptype);
1246 switch (sig->ret->type) {
1247 case MONO_TYPE_BOOLEAN:
1248 case MONO_TYPE_CHAR:
1249 case MONO_TYPE_I1:
1250 case MONO_TYPE_U1:
1251 case MONO_TYPE_I2:
1252 case MONO_TYPE_U2:
1253 case MONO_TYPE_I4:
1254 case MONO_TYPE_U4:
1255 case MONO_TYPE_I:
1256 case MONO_TYPE_U:
1257 case MONO_TYPE_PTR:
1258 case MONO_TYPE_SZARRAY:
1259 case MONO_TYPE_CLASS:
1260 case MONO_TYPE_OBJECT:
1261 case MONO_TYPE_STRING:
1262 case MONO_TYPE_I8:
1263 case MONO_TYPE_U8:
1264 case MONO_TYPE_VALUETYPE:
1265 case MONO_TYPE_GENERICINST:
1266 margs->retval = &(frame->retval->data.p);
1267 margs->is_float_ret = 0;
1268 break;
1269 case MONO_TYPE_R4:
1270 case MONO_TYPE_R8:
1271 margs->retval = &(frame->retval->data.p);
1272 margs->is_float_ret = 1;
1273 break;
1274 case MONO_TYPE_VOID:
1275 margs->retval = NULL;
1276 break;
1277 default:
1278 g_error ("build_args_from_sig: ret type not implemented yet: 0x%x\n", sig->ret->type);
1281 return margs;
1283 #endif
1285 static void
1286 interp_frame_arg_to_data (MonoInterpFrameHandle frame, MonoMethodSignature *sig, int index, gpointer data)
1288 InterpFrame *iframe = (InterpFrame*)frame;
1290 if (index == -1)
1291 stackval_to_data (sig->ret, iframe->retval, data, sig->pinvoke);
1292 else
1293 stackval_to_data (sig->params [index], &iframe->stack_args [index], data, sig->pinvoke);
1296 static void
1297 interp_data_to_frame_arg (MonoInterpFrameHandle frame, MonoMethodSignature *sig, int index, gpointer data)
1299 InterpFrame *iframe = (InterpFrame*)frame;
1301 if (index == -1)
1302 stackval_from_data (sig->ret, iframe->retval, data, sig->pinvoke);
1303 else if (sig->hasthis && index == 0)
1304 iframe->stack_args [index].data.p = *(gpointer*)data;
1305 else
1306 stackval_from_data (sig->params [index - sig->hasthis], &iframe->stack_args [index], data, sig->pinvoke);
1309 static gpointer
1310 interp_frame_arg_to_storage (MonoInterpFrameHandle frame, MonoMethodSignature *sig, int index)
1312 InterpFrame *iframe = (InterpFrame*)frame;
1314 if (index == -1)
1315 return stackval_to_data_addr (sig->ret, iframe->retval);
1316 else
1317 return stackval_to_data_addr (sig->params [index], &iframe->stack_args [index]);
1320 static void
1321 interp_frame_arg_set_storage (MonoInterpFrameHandle frame, MonoMethodSignature *sig, int index, gpointer storage)
1323 InterpFrame *iframe = (InterpFrame*)frame;
1324 stackval *val = (index == -1) ? iframe->retval : &iframe->stack_args [index];
1325 MonoType *type = (index == -1) ? sig->ret : sig->params [index];
1327 switch (type->type) {
1328 case MONO_TYPE_GENERICINST:
1329 if (!MONO_TYPE_IS_REFERENCE (type))
1330 val->data.vt = storage;
1331 break;
1332 case MONO_TYPE_VALUETYPE:
1333 val->data.vt = storage;
1334 break;
1335 default:
1336 g_assert_not_reached ();
1340 static MonoPIFunc
1341 get_interp_to_native_trampoline (void)
1343 static MonoPIFunc trampoline = NULL;
1345 if (!trampoline) {
1346 if (mono_ee_features.use_aot_trampolines) {
1347 trampoline = (MonoPIFunc) mono_aot_get_trampoline ("interp_to_native_trampoline");
1348 } else {
1349 MonoTrampInfo *info;
1350 trampoline = (MonoPIFunc) mono_arch_get_interp_to_native_trampoline (&info);
1351 mono_tramp_info_register (info, NULL);
1353 mono_memory_barrier ();
1355 return trampoline;
1358 static void
1359 interp_to_native_trampoline (gpointer addr, gpointer ccontext)
1361 get_interp_to_native_trampoline () (addr, ccontext);
1364 /* MONO_NO_OPTIMIATION is needed due to usage of INTERP_PUSH_LMF_WITH_CTX. */
1365 #ifdef _MSC_VER
1366 #pragma optimize ("", off)
1367 #endif
1368 static MONO_NO_OPTIMIZATION MONO_NEVER_INLINE void
1369 ves_pinvoke_method (InterpFrame *frame, MonoMethodSignature *sig, MonoFuncV addr, gboolean string_ctor, ThreadContext *context, gboolean save_last_error)
1371 MonoLMFExt ext;
1372 gpointer args;
1374 frame->ex = NULL;
1376 g_assert (!frame->imethod);
1378 static MonoPIFunc entry_func = NULL;
1379 if (!entry_func) {
1380 #ifdef MONO_ARCH_HAS_NO_PROPER_MONOCTX
1381 ERROR_DECL (error);
1382 entry_func = (MonoPIFunc) mono_jit_compile_method_jit_only (mini_get_interp_lmf_wrapper ("mono_interp_to_native_trampoline", (gpointer) mono_interp_to_native_trampoline), error);
1383 mono_error_assert_ok (error);
1384 #else
1385 entry_func = get_interp_to_native_trampoline ();
1386 #endif
1387 mono_memory_barrier ();
1390 #ifdef ENABLE_NETCORE
1391 if (save_last_error) {
1392 mono_marshal_clear_last_error ();
1394 #endif
1396 #ifdef MONO_ARCH_HAVE_INTERP_PINVOKE_TRAMP
1397 CallContext ccontext;
1398 mono_arch_set_native_call_context_args (&ccontext, frame, sig);
1399 args = &ccontext;
1400 #else
1401 InterpMethodArguments *margs = build_args_from_sig (sig, frame);
1402 args = margs;
1403 #endif
1405 INTERP_PUSH_LMF_WITH_CTX (frame, ext, exit_pinvoke);
1406 entry_func ((gpointer) addr, args);
1407 if (save_last_error)
1408 mono_marshal_set_last_error ();
1409 interp_pop_lmf (&ext);
1411 #ifdef MONO_ARCH_HAVE_INTERP_PINVOKE_TRAMP
1412 if (!frame->ex)
1413 mono_arch_get_native_call_context_ret (&ccontext, frame, sig);
1415 if (ccontext.stack != NULL)
1416 g_free (ccontext.stack);
1417 #else
1418 if (!frame->ex && !MONO_TYPE_ISSTRUCT (sig->ret))
1419 stackval_from_data (sig->ret, frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
1421 g_free (margs->iargs);
1422 g_free (margs->fargs);
1423 g_free (margs);
1424 #endif
1425 goto exit_pinvoke; // prevent unused label warning in some configurations
1426 exit_pinvoke:
1427 return;
1429 #ifdef _MSC_VER
1430 #pragma optimize ("", on)
1431 #endif
1434 * interp_init_delegate:
1436 * Initialize del->interp_method.
1438 static void
1439 interp_init_delegate (MonoDelegate *del, MonoError *error)
1441 MonoMethod *method;
1443 if (del->interp_method) {
1444 /* Delegate created by a call to ves_icall_mono_delegate_ctor_interp () */
1445 del->method = ((InterpMethod *)del->interp_method)->method;
1446 } else if (del->method) {
1447 /* Delegate created dynamically */
1448 del->interp_method = mono_interp_get_imethod (del->object.vtable->domain, del->method, error);
1449 } else {
1450 /* Created from JITted code */
1451 g_assert_not_reached ();
1454 method = ((InterpMethod*)del->interp_method)->method;
1455 if (del->target &&
1456 method &&
1457 method->flags & METHOD_ATTRIBUTE_VIRTUAL &&
1458 method->flags & METHOD_ATTRIBUTE_ABSTRACT &&
1459 mono_class_is_abstract (method->klass))
1460 del->interp_method = get_virtual_method ((InterpMethod*)del->interp_method, del->target->vtable);
1462 method = ((InterpMethod*)del->interp_method)->method;
1463 if (method && m_class_get_parent (method->klass) == mono_defaults.multicastdelegate_class) {
1464 const char *name = method->name;
1465 if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
1467 * When invoking the delegate interp_method is executed directly. If it's an
1468 * invoke make sure we replace it with the appropriate delegate invoke wrapper.
1470 * FIXME We should do this later, when we also know the delegate on which the
1471 * target method is called.
1473 del->interp_method = mono_interp_get_imethod (del->object.vtable->domain, mono_marshal_get_delegate_invoke (method, NULL), error);
1474 mono_error_assert_ok (error);
1478 if (!((InterpMethod *) del->interp_method)->transformed && method_is_dynamic (method)) {
1479 /* Return any errors from method compilation */
1480 mono_interp_transform_method ((InterpMethod *) del->interp_method, get_context (), error);
1481 return_if_nok (error);
1485 static void
1486 interp_delegate_ctor (MonoObjectHandle this_obj, MonoObjectHandle target, gpointer addr, MonoError *error)
1489 * addr is the result of an LDFTN opcode, i.e. an InterpMethod
1491 InterpMethod *imethod = (InterpMethod*)addr;
1493 if (!(imethod->method->flags & METHOD_ATTRIBUTE_STATIC)) {
1494 MonoMethod *invoke = mono_get_delegate_invoke_internal (mono_handle_class (this_obj));
1495 /* virtual invoke delegates must not have null check */
1496 if (mono_method_signature_internal (imethod->method)->param_count == mono_method_signature_internal (invoke)->param_count
1497 && MONO_HANDLE_IS_NULL (target)) {
1498 mono_error_set_argument (error, "this", "Delegate to an instance method cannot have null 'this'");
1499 return;
1503 g_assert (imethod->method);
1504 gpointer entry = mini_get_interp_callbacks ()->create_method_pointer (imethod->method, FALSE, error);
1505 return_if_nok (error);
1507 MONO_HANDLE_SETVAL (MONO_HANDLE_CAST (MonoDelegate, this_obj), interp_method, gpointer, imethod);
1509 mono_delegate_ctor (this_obj, target, entry, error);
1513 * From the spec:
1514 * runtime specifies that the implementation of the method is automatically
1515 * provided by the runtime and is primarily used for the methods of delegates.
1517 static MONO_NEVER_INLINE void
1518 ves_imethod (InterpFrame *frame, MonoMethod *method, MonoMethodSignature *sig, stackval *sp, stackval *retval)
1520 const char *name = method->name;
1521 mono_class_init_internal (method->klass);
1523 if (method->klass == mono_defaults.array_class) {
1524 if (!strcmp (name, "UnsafeMov")) {
1525 /* TODO: layout checks */
1526 stackval_from_data (sig->ret, retval, (char*) sp, FALSE);
1527 return;
1529 if (!strcmp (name, "UnsafeLoad")) {
1530 ves_array_get (frame, sp, retval, sig, FALSE);
1531 return;
1533 } else if (mini_class_is_system_array (method->klass)) {
1534 MonoObject *obj = (MonoObject*) sp->data.p;
1535 if (!obj) {
1536 frame->ex = mono_get_exception_null_reference ();
1537 return;
1539 if (*name == 'S' && (strcmp (name, "Set") == 0)) {
1540 ves_array_set (frame, sp, sig);
1541 return;
1543 if (*name == 'G' && (strcmp (name, "Get") == 0)) {
1544 ves_array_get (frame, sp, retval, sig, TRUE);
1545 return;
1549 g_error ("Don't know how to exec runtime method %s.%s::%s",
1550 m_class_get_name_space (method->klass), m_class_get_name (method->klass),
1551 method->name);
1554 #if DEBUG_INTERP
1555 static char*
1556 dump_stack (stackval *stack, stackval *sp)
1558 stackval *s = stack;
1559 GString *str = g_string_new ("");
1561 if (sp == stack)
1562 return g_string_free (str, FALSE);
1564 while (s < sp) {
1565 g_string_append_printf (str, "[%p (%lld)] ", s->data.l, s->data.l);
1566 ++s;
1568 return g_string_free (str, FALSE);
1571 static void
1572 dump_stackval (GString *str, stackval *s, MonoType *type)
1574 switch (type->type) {
1575 case MONO_TYPE_I1:
1576 case MONO_TYPE_U1:
1577 case MONO_TYPE_I2:
1578 case MONO_TYPE_U2:
1579 case MONO_TYPE_I4:
1580 case MONO_TYPE_U4:
1581 case MONO_TYPE_CHAR:
1582 case MONO_TYPE_BOOLEAN:
1583 g_string_append_printf (str, "[%d] ", s->data.i);
1584 break;
1585 case MONO_TYPE_STRING:
1586 case MONO_TYPE_SZARRAY:
1587 case MONO_TYPE_CLASS:
1588 case MONO_TYPE_OBJECT:
1589 case MONO_TYPE_ARRAY:
1590 case MONO_TYPE_PTR:
1591 case MONO_TYPE_I:
1592 case MONO_TYPE_U:
1593 g_string_append_printf (str, "[%p] ", s->data.p);
1594 break;
1595 case MONO_TYPE_VALUETYPE:
1596 if (m_class_is_enumtype (type->data.klass))
1597 g_string_append_printf (str, "[%d] ", s->data.i);
1598 else
1599 g_string_append_printf (str, "[vt:%p] ", s->data.p);
1600 break;
1601 case MONO_TYPE_R4:
1602 g_string_append_printf (str, "[%g] ", s->data.f_r4);
1603 break;
1604 case MONO_TYPE_R8:
1605 g_string_append_printf (str, "[%g] ", s->data.f);
1606 break;
1607 case MONO_TYPE_I8:
1608 case MONO_TYPE_U8:
1609 default: {
1610 GString *res = g_string_new ("");
1611 mono_type_get_desc (res, type, TRUE);
1612 g_string_append_printf (str, "[{%s} %lld/0x%0llx] ", res->str, s->data.l, s->data.l);
1613 g_string_free (res, TRUE);
1614 break;
1619 static char*
1620 dump_retval (InterpFrame *inv)
1622 GString *str = g_string_new ("");
1623 MonoType *ret = mono_method_signature_internal (inv->imethod->method)->ret;
1625 if (ret->type != MONO_TYPE_VOID)
1626 dump_stackval (str, inv->retval, ret);
1628 return g_string_free (str, FALSE);
1631 static char*
1632 dump_args (InterpFrame *inv)
1634 GString *str = g_string_new ("");
1635 int i;
1636 MonoMethodSignature *signature = mono_method_signature_internal (inv->imethod->method);
1638 if (signature->param_count == 0 && !signature->hasthis)
1639 return g_string_free (str, FALSE);
1641 if (signature->hasthis) {
1642 MonoMethod *method = inv->imethod->method;
1643 dump_stackval (str, inv->stack_args, m_class_get_byval_arg (method->klass));
1646 for (i = 0; i < signature->param_count; ++i)
1647 dump_stackval (str, inv->stack_args + (!!signature->hasthis) + i, signature->params [i]);
1649 return g_string_free (str, FALSE);
1651 #endif
1653 #define CHECK_ADD_OVERFLOW(a,b) \
1654 (gint32)(b) >= 0 ? (gint32)(G_MAXINT32) - (gint32)(b) < (gint32)(a) ? -1 : 0 \
1655 : (gint32)(G_MININT32) - (gint32)(b) > (gint32)(a) ? +1 : 0
1657 #define CHECK_SUB_OVERFLOW(a,b) \
1658 (gint32)(b) < 0 ? (gint32)(G_MAXINT32) + (gint32)(b) < (gint32)(a) ? -1 : 0 \
1659 : (gint32)(G_MININT32) + (gint32)(b) > (gint32)(a) ? +1 : 0
1661 #define CHECK_ADD_OVERFLOW_UN(a,b) \
1662 (guint32)(G_MAXUINT32) - (guint32)(b) < (guint32)(a) ? -1 : 0
1664 #define CHECK_SUB_OVERFLOW_UN(a,b) \
1665 (guint32)(a) < (guint32)(b) ? -1 : 0
1667 #define CHECK_ADD_OVERFLOW64(a,b) \
1668 (gint64)(b) >= 0 ? (gint64)(G_MAXINT64) - (gint64)(b) < (gint64)(a) ? -1 : 0 \
1669 : (gint64)(G_MININT64) - (gint64)(b) > (gint64)(a) ? +1 : 0
1671 #define CHECK_SUB_OVERFLOW64(a,b) \
1672 (gint64)(b) < 0 ? (gint64)(G_MAXINT64) + (gint64)(b) < (gint64)(a) ? -1 : 0 \
1673 : (gint64)(G_MININT64) + (gint64)(b) > (gint64)(a) ? +1 : 0
1675 #define CHECK_ADD_OVERFLOW64_UN(a,b) \
1676 (guint64)(G_MAXUINT64) - (guint64)(b) < (guint64)(a) ? -1 : 0
1678 #define CHECK_SUB_OVERFLOW64_UN(a,b) \
1679 (guint64)(a) < (guint64)(b) ? -1 : 0
1681 #if SIZEOF_VOID_P == 4
1682 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW(a,b)
1683 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW_UN(a,b)
1684 #else
1685 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW64(a,b)
1686 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW64_UN(a,b)
1687 #endif
1689 /* Resolves to TRUE if the operands would overflow */
1690 #define CHECK_MUL_OVERFLOW(a,b) \
1691 ((gint32)(a) == 0) || ((gint32)(b) == 0) ? 0 : \
1692 (((gint32)(a) > 0) && ((gint32)(b) == -1)) ? FALSE : \
1693 (((gint32)(a) < 0) && ((gint32)(b) == -1)) ? (a == G_MININT32) : \
1694 (((gint32)(a) > 0) && ((gint32)(b) > 0)) ? (gint32)(a) > ((G_MAXINT32) / (gint32)(b)) : \
1695 (((gint32)(a) > 0) && ((gint32)(b) < 0)) ? (gint32)(a) > ((G_MININT32) / (gint32)(b)) : \
1696 (((gint32)(a) < 0) && ((gint32)(b) > 0)) ? (gint32)(a) < ((G_MININT32) / (gint32)(b)) : \
1697 (gint32)(a) < ((G_MAXINT32) / (gint32)(b))
1699 #define CHECK_MUL_OVERFLOW_UN(a,b) \
1700 ((guint32)(a) == 0) || ((guint32)(b) == 0) ? 0 : \
1701 (guint32)(b) > ((G_MAXUINT32) / (guint32)(a))
1703 #define CHECK_MUL_OVERFLOW64(a,b) \
1704 ((gint64)(a) == 0) || ((gint64)(b) == 0) ? 0 : \
1705 (((gint64)(a) > 0) && ((gint64)(b) == -1)) ? FALSE : \
1706 (((gint64)(a) < 0) && ((gint64)(b) == -1)) ? (a == G_MININT64) : \
1707 (((gint64)(a) > 0) && ((gint64)(b) > 0)) ? (gint64)(a) > ((G_MAXINT64) / (gint64)(b)) : \
1708 (((gint64)(a) > 0) && ((gint64)(b) < 0)) ? (gint64)(a) > ((G_MININT64) / (gint64)(b)) : \
1709 (((gint64)(a) < 0) && ((gint64)(b) > 0)) ? (gint64)(a) < ((G_MININT64) / (gint64)(b)) : \
1710 (gint64)(a) < ((G_MAXINT64) / (gint64)(b))
1712 #define CHECK_MUL_OVERFLOW64_UN(a,b) \
1713 ((guint64)(a) == 0) || ((guint64)(b) == 0) ? 0 : \
1714 (guint64)(b) > ((G_MAXUINT64) / (guint64)(a))
1716 #if SIZEOF_VOID_P == 4
1717 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW(a,b)
1718 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW_UN(a,b)
1719 #else
1720 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW64(a,b)
1721 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW64_UN(a,b)
1722 #endif
1724 static MonoObject*
1725 interp_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc, MonoError *error)
1727 InterpFrame frame;
1728 ThreadContext *context = get_context ();
1729 MonoMethodSignature *sig = mono_method_signature_internal (method);
1730 MonoClass *klass = mono_class_from_mono_type_internal (sig->ret);
1731 stackval result;
1732 MonoMethod *target_method = method;
1734 error_init (error);
1735 if (exc)
1736 *exc = NULL;
1738 frame.ex = NULL;
1740 MonoDomain *domain = mono_domain_get ();
1742 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
1743 target_method = mono_marshal_get_native_wrapper (target_method, FALSE, FALSE);
1744 MonoMethod *invoke_wrapper = mono_marshal_get_runtime_invoke_full (target_method, FALSE, TRUE);
1746 //* <code>MonoObject *runtime_invoke (MonoObject *this_obj, void **params, MonoObject **exc, void* method)</code>
1748 result.data.vt = alloca (mono_class_instance_size (klass));
1749 stackval args [4];
1751 if (sig->hasthis)
1752 args [0].data.p = obj;
1753 else
1754 args [0].data.p = NULL;
1755 args [1].data.p = params;
1756 args [2].data.p = exc;
1757 args [3].data.p = target_method;
1759 InterpMethod *imethod = mono_interp_get_imethod (domain, invoke_wrapper, error);
1760 mono_error_assert_ok (error);
1761 init_frame (&frame, NULL, imethod, args, &result);
1763 interp_exec_method (&frame, context, error);
1765 if (frame.ex) {
1766 if (exc) {
1767 *exc = (MonoObject*) frame.ex;
1768 return NULL;
1770 mono_error_set_exception_instance (error, frame.ex);
1771 return NULL;
1773 return (MonoObject*)result.data.p;
1776 typedef struct {
1777 InterpMethod *rmethod;
1778 gpointer this_arg;
1779 gpointer res;
1780 gpointer args [16];
1781 gpointer *many_args;
1782 } InterpEntryData;
1784 /* Main function for entering the interpreter from compiled code */
1785 static void
1786 interp_entry (InterpEntryData *data)
1788 InterpFrame frame;
1789 InterpMethod *rmethod;
1790 ThreadContext *context;
1791 stackval result;
1792 stackval *args;
1793 MonoMethod *method;
1794 MonoMethodSignature *sig;
1795 MonoType *type;
1796 gpointer orig_domain = NULL, attach_cookie;
1797 int i;
1799 if ((gsize)data->rmethod & 1) {
1800 /* Unbox */
1801 data->this_arg = mono_object_unbox_internal ((MonoObject*)data->this_arg);
1802 data->rmethod = (InterpMethod*)(gpointer)((gsize)data->rmethod & ~1);
1804 rmethod = data->rmethod;
1806 if (rmethod->needs_thread_attach)
1807 orig_domain = mono_threads_attach_coop (mono_domain_get (), &attach_cookie);
1809 context = get_context ();
1811 method = rmethod->method;
1812 sig = mono_method_signature_internal (method);
1814 // FIXME: Optimize this
1816 //printf ("%s\n", mono_method_full_name (method, 1));
1818 frame.ex = NULL;
1820 args = g_newa (stackval, sig->param_count + (sig->hasthis ? 1 : 0));
1821 if (sig->hasthis)
1822 args [0].data.p = data->this_arg;
1824 gpointer *params;
1825 if (data->many_args)
1826 params = data->many_args;
1827 else
1828 params = data->args;
1829 for (i = 0; i < sig->param_count; ++i) {
1830 int a_index = i + (sig->hasthis ? 1 : 0);
1831 if (sig->params [i]->byref) {
1832 args [a_index].data.p = params [i];
1833 continue;
1835 type = rmethod->param_types [i];
1836 switch (type->type) {
1837 case MONO_TYPE_VALUETYPE:
1838 args [a_index].data.p = params [i];
1839 break;
1840 case MONO_TYPE_GENERICINST:
1841 if (MONO_TYPE_IS_REFERENCE (type))
1842 args [a_index].data.p = *(gpointer*)params [i];
1843 else
1844 args [a_index].data.vt = params [i];
1845 break;
1846 default:
1847 stackval_from_data (type, &args [a_index], params [i], FALSE);
1848 break;
1852 memset (&result, 0, sizeof (result));
1853 init_frame (&frame, NULL, data->rmethod, args, &result);
1855 type = rmethod->rtype;
1856 switch (type->type) {
1857 case MONO_TYPE_GENERICINST:
1858 if (!MONO_TYPE_IS_REFERENCE (type))
1859 frame.retval->data.vt = data->res;
1860 break;
1861 case MONO_TYPE_VALUETYPE:
1862 frame.retval->data.vt = data->res;
1863 break;
1864 default:
1865 break;
1868 ERROR_DECL (error);
1869 interp_exec_method (&frame, context, error);
1871 if (rmethod->needs_thread_attach)
1872 mono_threads_detach_coop (orig_domain, &attach_cookie);
1874 if (mono_llvm_only) {
1875 if (frame.ex)
1876 mono_llvm_reraise_exception (frame.ex);
1877 } else {
1878 g_assert (frame.ex == NULL);
1881 type = rmethod->rtype;
1882 switch (type->type) {
1883 case MONO_TYPE_VOID:
1884 break;
1885 case MONO_TYPE_OBJECT:
1886 /* No need for a write barrier */
1887 *(MonoObject**)data->res = (MonoObject*)frame.retval->data.p;
1888 break;
1889 case MONO_TYPE_GENERICINST:
1890 if (MONO_TYPE_IS_REFERENCE (type)) {
1891 *(MonoObject**)data->res = (MonoObject*)frame.retval->data.p;
1892 } else {
1893 /* Already set before the call */
1895 break;
1896 case MONO_TYPE_VALUETYPE:
1897 /* Already set before the call */
1898 break;
1899 default:
1900 stackval_to_data (type, frame.retval, data->res, FALSE);
1901 break;
1905 static stackval *
1906 do_icall (InterpFrame *frame, MonoMethodSignature *sig, int op, stackval *sp, gpointer ptr, gboolean save_last_error)
1908 #ifdef ENABLE_NETCORE
1909 if (save_last_error)
1910 mono_marshal_clear_last_error ();
1911 #endif
1913 switch (op) {
1914 case MINT_ICALL_V_V: {
1915 typedef void (*T)(void);
1916 T func = (T)ptr;
1917 func ();
1918 break;
1920 case MINT_ICALL_V_P: {
1921 typedef gpointer (*T)(void);
1922 T func = (T)ptr;
1923 sp++;
1924 sp [-1].data.p = func ();
1925 break;
1927 case MINT_ICALL_P_V: {
1928 typedef void (*T)(gpointer);
1929 T func = (T)ptr;
1930 func (sp [-1].data.p);
1931 sp --;
1932 break;
1934 case MINT_ICALL_P_P: {
1935 typedef gpointer (*T)(gpointer);
1936 T func = (T)ptr;
1937 sp [-1].data.p = func (sp [-1].data.p);
1938 break;
1940 case MINT_ICALL_PP_V: {
1941 typedef void (*T)(gpointer,gpointer);
1942 T func = (T)ptr;
1943 sp -= 2;
1944 func (sp [0].data.p, sp [1].data.p);
1945 break;
1947 case MINT_ICALL_PP_P: {
1948 typedef gpointer (*T)(gpointer,gpointer);
1949 T func = (T)ptr;
1950 --sp;
1951 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.p);
1952 break;
1954 case MINT_ICALL_PPP_V: {
1955 typedef void (*T)(gpointer,gpointer,gpointer);
1956 T func = (T)ptr;
1957 sp -= 3;
1958 func (sp [0].data.p, sp [1].data.p, sp [2].data.p);
1959 break;
1961 case MINT_ICALL_PPP_P: {
1962 typedef gpointer (*T)(gpointer,gpointer,gpointer);
1963 T func = (T)ptr;
1964 sp -= 2;
1965 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.p, sp [1].data.p);
1966 break;
1968 case MINT_ICALL_PPPP_V: {
1969 typedef void (*T)(gpointer,gpointer,gpointer,gpointer);
1970 T func = (T)ptr;
1971 sp -= 4;
1972 func (sp [0].data.p, sp [1].data.p, sp [2].data.p, sp [3].data.p);
1973 break;
1975 case MINT_ICALL_PPPP_P: {
1976 typedef gpointer (*T)(gpointer,gpointer,gpointer,gpointer);
1977 T func = (T)ptr;
1978 sp -= 3;
1979 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.p, sp [1].data.p, sp [2].data.p);
1980 break;
1982 case MINT_ICALL_PPPPP_V: {
1983 typedef void (*T)(gpointer,gpointer,gpointer,gpointer,gpointer);
1984 T func = (T)ptr;
1985 sp -= 5;
1986 func (sp [0].data.p, sp [1].data.p, sp [2].data.p, sp [3].data.p, sp [4].data.p);
1987 break;
1989 case MINT_ICALL_PPPPP_P: {
1990 typedef gpointer (*T)(gpointer,gpointer,gpointer,gpointer,gpointer);
1991 T func = (T)ptr;
1992 sp -= 4;
1993 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.p, sp [1].data.p, sp [2].data.p, sp [3].data.p);
1994 break;
1996 case MINT_ICALL_PPPPPP_V: {
1997 typedef void (*T)(gpointer,gpointer,gpointer,gpointer,gpointer,gpointer);
1998 T func = (T)ptr;
1999 sp -= 6;
2000 func (sp [0].data.p, sp [1].data.p, sp [2].data.p, sp [3].data.p, sp [4].data.p, sp [5].data.p);
2001 break;
2003 case MINT_ICALL_PPPPPP_P: {
2004 typedef gpointer (*T)(gpointer,gpointer,gpointer,gpointer,gpointer,gpointer);
2005 T func = (T)ptr;
2006 sp -= 5;
2007 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.p, sp [1].data.p, sp [2].data.p, sp [3].data.p, sp [4].data.p);
2008 break;
2010 default:
2011 g_assert_not_reached ();
2014 if (save_last_error)
2015 mono_marshal_set_last_error ();
2017 /* convert the native representation to the stackval representation */
2018 if (sig)
2019 stackval_from_data (sig->ret, &sp [-1], (char*) &sp [-1].data.p, sig->pinvoke);
2021 return sp;
2024 /* MONO_NO_OPTIMIATION is needed due to usage of INTERP_PUSH_LMF_WITH_CTX. */
2025 #ifdef _MSC_VER
2026 #pragma optimize ("", off)
2027 #endif
2028 static MONO_NO_OPTIMIZATION MONO_NEVER_INLINE stackval *
2029 do_icall_wrapper (InterpFrame *frame, MonoMethodSignature *sig, int op, stackval *sp, gpointer ptr, gboolean save_last_error)
2031 MonoLMFExt ext;
2032 INTERP_PUSH_LMF_WITH_CTX (frame, ext, exit_icall);
2034 sp = do_icall (frame, sig, op, sp, ptr, save_last_error);
2036 interp_pop_lmf (&ext);
2038 goto exit_icall; // prevent unused label warning in some configurations
2039 exit_icall:
2040 return sp;
2042 #ifdef _MSC_VER
2043 #pragma optimize ("", on)
2044 #endif
2046 typedef struct {
2047 int pindex;
2048 gpointer jit_wrapper;
2049 gpointer *args;
2050 MonoFtnDesc *ftndesc;
2051 } JitCallCbData;
2053 static void
2054 jit_call_cb (gpointer arg)
2056 JitCallCbData *cb_data = (JitCallCbData*)arg;
2057 gpointer jit_wrapper = cb_data->jit_wrapper;
2058 int pindex = cb_data->pindex;
2059 gpointer *args = cb_data->args;
2060 MonoFtnDesc ftndesc = *cb_data->ftndesc;
2062 switch (pindex) {
2063 case 0: {
2064 typedef void (*T)(gpointer);
2065 T func = (T)jit_wrapper;
2067 func (&ftndesc);
2068 break;
2070 case 1: {
2071 typedef void (*T)(gpointer, gpointer);
2072 T func = (T)jit_wrapper;
2074 func (args [0], &ftndesc);
2075 break;
2077 case 2: {
2078 typedef void (*T)(gpointer, gpointer, gpointer);
2079 T func = (T)jit_wrapper;
2081 func (args [0], args [1], &ftndesc);
2082 break;
2084 case 3: {
2085 typedef void (*T)(gpointer, gpointer, gpointer, gpointer);
2086 T func = (T)jit_wrapper;
2088 func (args [0], args [1], args [2], &ftndesc);
2089 break;
2091 case 4: {
2092 typedef void (*T)(gpointer, gpointer, gpointer, gpointer, gpointer);
2093 T func = (T)jit_wrapper;
2095 func (args [0], args [1], args [2], args [3], &ftndesc);
2096 break;
2098 case 5: {
2099 typedef void (*T)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer);
2100 T func = (T)jit_wrapper;
2102 func (args [0], args [1], args [2], args [3], args [4], &ftndesc);
2103 break;
2105 case 6: {
2106 typedef void (*T)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer);
2107 T func = (T)jit_wrapper;
2109 func (args [0], args [1], args [2], args [3], args [4], args [5], &ftndesc);
2110 break;
2112 case 7: {
2113 typedef void (*T)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer);
2114 T func = (T)jit_wrapper;
2116 func (args [0], args [1], args [2], args [3], args [4], args [5], args [6], &ftndesc);
2117 break;
2119 case 8: {
2120 typedef void (*T)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer);
2121 T func = (T)jit_wrapper;
2123 func (args [0], args [1], args [2], args [3], args [4], args [5], args [6], args [7], &ftndesc);
2124 break;
2126 default:
2127 g_assert_not_reached ();
2128 break;
2132 static MONO_NEVER_INLINE stackval *
2133 do_jit_call (stackval *sp, unsigned char *vt_sp, ThreadContext *context, InterpFrame *frame, InterpMethod *rmethod, MonoError *error)
2135 MonoMethodSignature *sig;
2136 MonoFtnDesc ftndesc;
2137 guint8 res_buf [256];
2138 MonoType *type;
2139 MonoLMFExt ext;
2141 //printf ("jit_call: %s\n", mono_method_full_name (rmethod->method, 1));
2144 * Call JITted code through a gsharedvt_out wrapper. These wrappers receive every argument
2145 * by ref and return a return value using an explicit return value argument.
2147 if (!rmethod->jit_wrapper) {
2148 MonoMethod *method = rmethod->method;
2150 sig = mono_method_signature_internal (method);
2151 g_assert (sig);
2153 MonoMethod *wrapper = mini_get_gsharedvt_out_sig_wrapper (sig);
2154 //printf ("J: %s %s\n", mono_method_full_name (method, 1), mono_method_full_name (wrapper, 1));
2156 gpointer jit_wrapper = mono_jit_compile_method_jit_only (wrapper, error);
2157 mono_error_assert_ok (error);
2159 gpointer addr = mono_jit_compile_method_jit_only (method, error);
2160 return_val_if_nok (error, NULL);
2161 g_assert (addr);
2163 rmethod->jit_addr = addr;
2164 rmethod->jit_sig = sig;
2165 mono_memory_barrier ();
2166 rmethod->jit_wrapper = jit_wrapper;
2168 } else {
2169 sig = rmethod->jit_sig;
2172 sp -= sig->param_count;
2173 if (sig->hasthis)
2174 --sp;
2176 ftndesc.addr = rmethod->jit_addr;
2177 ftndesc.arg = NULL;
2179 // FIXME: Optimize this
2181 gpointer args [32];
2182 int pindex = 0;
2183 int stack_index = 0;
2184 if (rmethod->hasthis) {
2185 args [pindex ++] = sp [0].data.p;
2186 stack_index ++;
2188 type = rmethod->rtype;
2189 if (type->type != MONO_TYPE_VOID) {
2190 if (MONO_TYPE_ISSTRUCT (type))
2191 args [pindex ++] = vt_sp;
2192 else
2193 args [pindex ++] = res_buf;
2195 for (int i = 0; i < rmethod->param_count; ++i) {
2196 MonoType *t = rmethod->param_types [i];
2197 stackval *sval = &sp [stack_index + i];
2198 if (sig->params [i]->byref) {
2199 args [pindex ++] = sval->data.p;
2200 } else if (MONO_TYPE_ISSTRUCT (t)) {
2201 args [pindex ++] = sval->data.p;
2202 } else if (MONO_TYPE_IS_REFERENCE (t)) {
2203 args [pindex ++] = &sval->data.p;
2204 } else {
2205 switch (t->type) {
2206 case MONO_TYPE_I1:
2207 case MONO_TYPE_U1:
2208 case MONO_TYPE_I2:
2209 case MONO_TYPE_U2:
2210 case MONO_TYPE_I4:
2211 case MONO_TYPE_U4:
2212 case MONO_TYPE_VALUETYPE:
2213 args [pindex ++] = &sval->data.i;
2214 break;
2215 case MONO_TYPE_PTR:
2216 case MONO_TYPE_FNPTR:
2217 case MONO_TYPE_I:
2218 case MONO_TYPE_U:
2219 case MONO_TYPE_OBJECT:
2220 args [pindex ++] = &sval->data.p;
2221 break;
2222 case MONO_TYPE_I8:
2223 case MONO_TYPE_U8:
2224 args [pindex ++] = &sval->data.l;
2225 break;
2226 case MONO_TYPE_R4:
2227 args [pindex ++] = &sval->data.f_r4;
2228 break;
2229 case MONO_TYPE_R8:
2230 args [pindex ++] = &sval->data.f;
2231 break;
2232 default:
2233 printf ("%s\n", mono_type_full_name (t));
2234 g_assert_not_reached ();
2239 interp_push_lmf (&ext, frame);
2241 JitCallCbData cb_data;
2242 memset (&cb_data, 0, sizeof (cb_data));
2243 cb_data.jit_wrapper = rmethod->jit_wrapper;
2244 cb_data.pindex = pindex;
2245 cb_data.args = args;
2246 cb_data.ftndesc = &ftndesc;
2248 if (mono_aot_mode == MONO_AOT_MODE_LLVMONLY_INTERP) {
2249 /* Catch the exception thrown by the native code using a try-catch */
2250 gboolean thrown = FALSE;
2251 mono_llvm_cpp_catch_exception (jit_call_cb, &cb_data, &thrown);
2252 interp_pop_lmf (&ext);
2253 if (thrown) {
2254 MonoObject *obj = mono_llvm_load_exception ();
2255 g_assert (obj);
2256 mono_error_set_exception_instance (error, (MonoException*)obj);
2257 return sp;
2259 } else {
2260 jit_call_cb (&cb_data);
2261 interp_pop_lmf (&ext);
2264 MonoType *rtype = rmethod->rtype;
2265 switch (rtype->type) {
2266 case MONO_TYPE_VOID:
2267 case MONO_TYPE_OBJECT:
2268 case MONO_TYPE_STRING:
2269 case MONO_TYPE_CLASS:
2270 case MONO_TYPE_ARRAY:
2271 case MONO_TYPE_SZARRAY:
2272 case MONO_TYPE_I:
2273 case MONO_TYPE_U:
2274 case MONO_TYPE_PTR:
2275 sp->data.p = *(gpointer*)res_buf;
2276 break;
2277 case MONO_TYPE_I1:
2278 sp->data.i = *(gint8*)res_buf;
2279 break;
2280 case MONO_TYPE_U1:
2281 sp->data.i = *(guint8*)res_buf;
2282 break;
2283 case MONO_TYPE_I2:
2284 sp->data.i = *(gint16*)res_buf;
2285 break;
2286 case MONO_TYPE_U2:
2287 sp->data.i = *(guint16*)res_buf;
2288 break;
2289 case MONO_TYPE_I4:
2290 sp->data.i = *(gint32*)res_buf;
2291 break;
2292 case MONO_TYPE_U4:
2293 sp->data.i = *(guint32*)res_buf;
2294 break;
2295 case MONO_TYPE_I8:
2296 sp->data.l = *(gint64*)res_buf;
2297 break;
2298 case MONO_TYPE_U8:
2299 sp->data.l = *(guint64*)res_buf;
2300 break;
2301 case MONO_TYPE_R4:
2302 sp->data.f_r4 = *(float*)res_buf;
2303 break;
2304 case MONO_TYPE_R8:
2305 sp->data.f = *(double*)res_buf;
2306 break;
2307 case MONO_TYPE_TYPEDBYREF:
2308 case MONO_TYPE_VALUETYPE:
2309 /* The result was written to vt_sp */
2310 sp->data.p = vt_sp;
2311 break;
2312 case MONO_TYPE_GENERICINST:
2313 if (MONO_TYPE_IS_REFERENCE (rtype)) {
2314 sp->data.p = *(gpointer*)res_buf;
2315 } else {
2316 /* The result was written to vt_sp */
2317 sp->data.p = vt_sp;
2319 break;
2320 default:
2321 g_print ("%s\n", mono_type_full_name (rtype));
2322 g_assert_not_reached ();
2323 break;
2326 return sp;
2329 static MONO_NEVER_INLINE void
2330 do_debugger_tramp (void (*tramp) (void), InterpFrame *frame)
2332 MonoLMFExt ext;
2333 interp_push_lmf (&ext, frame);
2334 tramp ();
2335 interp_pop_lmf (&ext);
2338 static MONO_NEVER_INLINE void
2339 do_transform_method (InterpFrame *frame, ThreadContext *context)
2341 MonoLMFExt ext;
2342 /* Don't push lmf if we have no interp data */
2343 gboolean push_lmf = frame->parent != NULL;
2344 ERROR_DECL (error);
2346 /* Use the parent frame as the current frame is not complete yet */
2347 if (push_lmf)
2348 interp_push_lmf (&ext, frame->parent);
2350 mono_interp_transform_method (frame->imethod, context, error);
2351 frame->ex = mono_error_convert_to_exception (error);
2353 if (push_lmf)
2354 interp_pop_lmf (&ext);
2357 static MONO_NEVER_INLINE guchar*
2358 copy_varargs_vtstack (MonoMethodSignature *csig, stackval *sp, guchar *vt_sp)
2360 stackval *first_arg = sp - csig->param_count;
2363 * We need to have the varargs linearly on the stack so the ArgIterator
2364 * can iterate over them. We pass the signature first and then copy them
2365 * one by one on the vtstack.
2367 *(gpointer*)vt_sp = csig;
2368 vt_sp += sizeof (gpointer);
2370 for (int i = csig->sentinelpos; i < csig->param_count; i++) {
2371 int align, arg_size;
2372 arg_size = mono_type_stack_size (csig->params [i], &align);
2373 vt_sp = (guchar*)ALIGN_PTR_TO (vt_sp, align);
2375 stackval_to_data (csig->params [i], &first_arg [i], vt_sp, FALSE);
2376 vt_sp += arg_size;
2379 return (guchar*)ALIGN_PTR_TO (vt_sp, MINT_VT_ALIGNMENT);
2383 * These functions are the entry points into the interpreter from compiled code.
2384 * They are called by the interp_in wrappers. They have the following signature:
2385 * void (<optional this_arg>, <optional retval pointer>, <arg1>, ..., <argn>, <method ptr>)
2386 * They pack up their arguments into an InterpEntryData structure and call interp_entry ().
2387 * It would be possible for the wrappers to pack up the arguments etc, but that would make them bigger, and there are
2388 * more wrappers then these functions.
2389 * this/static * ret/void * 16 arguments -> 64 functions.
2392 #define MAX_INTERP_ENTRY_ARGS 8
2394 #define INTERP_ENTRY_BASE(_method, _this_arg, _res) \
2395 InterpEntryData data; \
2396 (data).rmethod = (_method); \
2397 (data).res = (_res); \
2398 (data).this_arg = (_this_arg); \
2399 (data).many_args = NULL;
2401 #define INTERP_ENTRY0(_this_arg, _res, _method) { \
2402 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2403 interp_entry (&data); \
2405 #define INTERP_ENTRY1(_this_arg, _res, _method) { \
2406 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2407 (data).args [0] = arg1; \
2408 interp_entry (&data); \
2410 #define INTERP_ENTRY2(_this_arg, _res, _method) { \
2411 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2412 (data).args [0] = arg1; \
2413 (data).args [1] = arg2; \
2414 interp_entry (&data); \
2416 #define INTERP_ENTRY3(_this_arg, _res, _method) { \
2417 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2418 (data).args [0] = arg1; \
2419 (data).args [1] = arg2; \
2420 (data).args [2] = arg3; \
2421 interp_entry (&data); \
2423 #define INTERP_ENTRY4(_this_arg, _res, _method) { \
2424 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2425 (data).args [0] = arg1; \
2426 (data).args [1] = arg2; \
2427 (data).args [2] = arg3; \
2428 (data).args [3] = arg4; \
2429 interp_entry (&data); \
2431 #define INTERP_ENTRY5(_this_arg, _res, _method) { \
2432 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2433 (data).args [0] = arg1; \
2434 (data).args [1] = arg2; \
2435 (data).args [2] = arg3; \
2436 (data).args [3] = arg4; \
2437 (data).args [4] = arg5; \
2438 interp_entry (&data); \
2440 #define INTERP_ENTRY6(_this_arg, _res, _method) { \
2441 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2442 (data).args [0] = arg1; \
2443 (data).args [1] = arg2; \
2444 (data).args [2] = arg3; \
2445 (data).args [3] = arg4; \
2446 (data).args [4] = arg5; \
2447 (data).args [5] = arg6; \
2448 interp_entry (&data); \
2450 #define INTERP_ENTRY7(_this_arg, _res, _method) { \
2451 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2452 (data).args [0] = arg1; \
2453 (data).args [1] = arg2; \
2454 (data).args [2] = arg3; \
2455 (data).args [3] = arg4; \
2456 (data).args [4] = arg5; \
2457 (data).args [5] = arg6; \
2458 (data).args [6] = arg7; \
2459 interp_entry (&data); \
2461 #define INTERP_ENTRY8(_this_arg, _res, _method) { \
2462 INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2463 (data).args [0] = arg1; \
2464 (data).args [1] = arg2; \
2465 (data).args [2] = arg3; \
2466 (data).args [3] = arg4; \
2467 (data).args [4] = arg5; \
2468 (data).args [5] = arg6; \
2469 (data).args [6] = arg7; \
2470 (data).args [7] = arg8; \
2471 interp_entry (&data); \
2474 #define ARGLIST0 InterpMethod *rmethod
2475 #define ARGLIST1 gpointer arg1, InterpMethod *rmethod
2476 #define ARGLIST2 gpointer arg1, gpointer arg2, InterpMethod *rmethod
2477 #define ARGLIST3 gpointer arg1, gpointer arg2, gpointer arg3, InterpMethod *rmethod
2478 #define ARGLIST4 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, InterpMethod *rmethod
2479 #define ARGLIST5 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, InterpMethod *rmethod
2480 #define ARGLIST6 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, gpointer arg6, InterpMethod *rmethod
2481 #define ARGLIST7 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, gpointer arg6, gpointer arg7, InterpMethod *rmethod
2482 #define ARGLIST8 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, gpointer arg6, gpointer arg7, gpointer arg8, InterpMethod *rmethod
2484 static void interp_entry_static_0 (ARGLIST0) INTERP_ENTRY0 (NULL, NULL, rmethod)
2485 static void interp_entry_static_1 (ARGLIST1) INTERP_ENTRY1 (NULL, NULL, rmethod)
2486 static void interp_entry_static_2 (ARGLIST2) INTERP_ENTRY2 (NULL, NULL, rmethod)
2487 static void interp_entry_static_3 (ARGLIST3) INTERP_ENTRY3 (NULL, NULL, rmethod)
2488 static void interp_entry_static_4 (ARGLIST4) INTERP_ENTRY4 (NULL, NULL, rmethod)
2489 static void interp_entry_static_5 (ARGLIST5) INTERP_ENTRY5 (NULL, NULL, rmethod)
2490 static void interp_entry_static_6 (ARGLIST6) INTERP_ENTRY6 (NULL, NULL, rmethod)
2491 static void interp_entry_static_7 (ARGLIST7) INTERP_ENTRY7 (NULL, NULL, rmethod)
2492 static void interp_entry_static_8 (ARGLIST8) INTERP_ENTRY8 (NULL, NULL, rmethod)
2493 static void interp_entry_static_ret_0 (gpointer res, ARGLIST0) INTERP_ENTRY0 (NULL, res, rmethod)
2494 static void interp_entry_static_ret_1 (gpointer res, ARGLIST1) INTERP_ENTRY1 (NULL, res, rmethod)
2495 static void interp_entry_static_ret_2 (gpointer res, ARGLIST2) INTERP_ENTRY2 (NULL, res, rmethod)
2496 static void interp_entry_static_ret_3 (gpointer res, ARGLIST3) INTERP_ENTRY3 (NULL, res, rmethod)
2497 static void interp_entry_static_ret_4 (gpointer res, ARGLIST4) INTERP_ENTRY4 (NULL, res, rmethod)
2498 static void interp_entry_static_ret_5 (gpointer res, ARGLIST5) INTERP_ENTRY5 (NULL, res, rmethod)
2499 static void interp_entry_static_ret_6 (gpointer res, ARGLIST6) INTERP_ENTRY6 (NULL, res, rmethod)
2500 static void interp_entry_static_ret_7 (gpointer res, ARGLIST7) INTERP_ENTRY7 (NULL, res, rmethod)
2501 static void interp_entry_static_ret_8 (gpointer res, ARGLIST8) INTERP_ENTRY8 (NULL, res, rmethod)
2502 static void interp_entry_instance_0 (gpointer this_arg, ARGLIST0) INTERP_ENTRY0 (this_arg, NULL, rmethod)
2503 static void interp_entry_instance_1 (gpointer this_arg, ARGLIST1) INTERP_ENTRY1 (this_arg, NULL, rmethod)
2504 static void interp_entry_instance_2 (gpointer this_arg, ARGLIST2) INTERP_ENTRY2 (this_arg, NULL, rmethod)
2505 static void interp_entry_instance_3 (gpointer this_arg, ARGLIST3) INTERP_ENTRY3 (this_arg, NULL, rmethod)
2506 static void interp_entry_instance_4 (gpointer this_arg, ARGLIST4) INTERP_ENTRY4 (this_arg, NULL, rmethod)
2507 static void interp_entry_instance_5 (gpointer this_arg, ARGLIST5) INTERP_ENTRY5 (this_arg, NULL, rmethod)
2508 static void interp_entry_instance_6 (gpointer this_arg, ARGLIST6) INTERP_ENTRY6 (this_arg, NULL, rmethod)
2509 static void interp_entry_instance_7 (gpointer this_arg, ARGLIST7) INTERP_ENTRY7 (this_arg, NULL, rmethod)
2510 static void interp_entry_instance_8 (gpointer this_arg, ARGLIST8) INTERP_ENTRY8 (this_arg, NULL, rmethod)
2511 static void interp_entry_instance_ret_0 (gpointer this_arg, gpointer res, ARGLIST0) INTERP_ENTRY0 (this_arg, res, rmethod)
2512 static void interp_entry_instance_ret_1 (gpointer this_arg, gpointer res, ARGLIST1) INTERP_ENTRY1 (this_arg, res, rmethod)
2513 static void interp_entry_instance_ret_2 (gpointer this_arg, gpointer res, ARGLIST2) INTERP_ENTRY2 (this_arg, res, rmethod)
2514 static void interp_entry_instance_ret_3 (gpointer this_arg, gpointer res, ARGLIST3) INTERP_ENTRY3 (this_arg, res, rmethod)
2515 static void interp_entry_instance_ret_4 (gpointer this_arg, gpointer res, ARGLIST4) INTERP_ENTRY4 (this_arg, res, rmethod)
2516 static void interp_entry_instance_ret_5 (gpointer this_arg, gpointer res, ARGLIST5) INTERP_ENTRY5 (this_arg, res, rmethod)
2517 static void interp_entry_instance_ret_6 (gpointer this_arg, gpointer res, ARGLIST6) INTERP_ENTRY6 (this_arg, res, rmethod)
2518 static void interp_entry_instance_ret_7 (gpointer this_arg, gpointer res, ARGLIST7) INTERP_ENTRY7 (this_arg, res, rmethod)
2519 static void interp_entry_instance_ret_8 (gpointer this_arg, gpointer res, ARGLIST8) INTERP_ENTRY8 (this_arg, res, rmethod)
2521 #define INTERP_ENTRY_FUNCLIST(type) (gpointer)interp_entry_ ## type ## _0, (gpointer)interp_entry_ ## type ## _1, (gpointer)interp_entry_ ## type ## _2, (gpointer)interp_entry_ ## type ## _3, (gpointer)interp_entry_ ## type ## _4, (gpointer)interp_entry_ ## type ## _5, (gpointer)interp_entry_ ## type ## _6, (gpointer)interp_entry_ ## type ## _7, (gpointer)interp_entry_ ## type ## _8
2523 static gpointer entry_funcs_static [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (static) };
2524 static gpointer entry_funcs_static_ret [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (static_ret) };
2525 static gpointer entry_funcs_instance [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (instance) };
2526 static gpointer entry_funcs_instance_ret [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (instance_ret) };
2528 /* General version for methods with more than MAX_INTERP_ENTRY_ARGS arguments */
2529 static void
2530 interp_entry_general (gpointer this_arg, gpointer res, gpointer *args, gpointer rmethod)
2532 INTERP_ENTRY_BASE ((InterpMethod*)rmethod, this_arg, res);
2533 data.many_args = args;
2534 interp_entry (&data);
2537 #ifdef MONO_ARCH_HAVE_INTERP_ENTRY_TRAMPOLINE
2539 // inline so we can alloc on stack
2540 #define alloc_storage_for_stackval(s, t, p) do { \
2541 if ((t)->type == MONO_TYPE_GENERICINST && !MONO_TYPE_IS_REFERENCE (t)) { \
2542 (s)->data.vt = alloca (mono_class_value_size (mono_class_from_mono_type_internal (t), NULL)); \
2543 } else if ((t)->type == MONO_TYPE_VALUETYPE) { \
2544 if (p) \
2545 (s)->data.vt = alloca (mono_class_native_size ((t)->data.klass, NULL)); \
2546 else \
2547 (s)->data.vt = alloca (mono_class_value_size ((t)->data.klass, NULL)); \
2549 } while (0)
2551 static void
2552 interp_entry_from_trampoline (gpointer ccontext_untyped, gpointer rmethod_untyped)
2554 InterpFrame frame;
2555 ThreadContext *context;
2556 stackval result;
2557 stackval *args;
2558 MonoMethod *method;
2559 MonoMethodSignature *sig;
2560 CallContext *ccontext = (CallContext*) ccontext_untyped;
2561 InterpMethod *rmethod = (InterpMethod*) rmethod_untyped;
2562 gpointer orig_domain = NULL, attach_cookie;
2563 int i;
2565 if (rmethod->needs_thread_attach)
2566 orig_domain = mono_threads_attach_coop (mono_domain_get (), &attach_cookie);
2568 context = get_context ();
2570 method = rmethod->method;
2571 sig = mono_method_signature_internal (method);
2573 frame.ex = NULL;
2575 args = (stackval*)alloca (sizeof (stackval) * (sig->param_count + (sig->hasthis ? 1 : 0)));
2577 init_frame (&frame, NULL, rmethod, args, &result);
2579 /* Allocate storage for value types */
2580 for (i = 0; i < sig->param_count; i++) {
2581 MonoType *type = sig->params [i];
2582 alloc_storage_for_stackval (&frame.stack_args [i + sig->hasthis], type, sig->pinvoke);
2585 if (sig->ret->type != MONO_TYPE_VOID)
2586 alloc_storage_for_stackval (frame.retval, sig->ret, sig->pinvoke);
2588 /* Copy the args saved in the trampoline to the frame stack */
2589 mono_arch_get_native_call_context_args (ccontext, &frame, sig);
2591 ERROR_DECL (error);
2592 interp_exec_method (&frame, context, error);
2594 if (rmethod->needs_thread_attach)
2595 mono_threads_detach_coop (orig_domain, &attach_cookie);
2597 // FIXME:
2598 g_assert (frame.ex == NULL);
2600 /* Write back the return value */
2601 mono_arch_set_native_call_context_ret (ccontext, &frame, sig);
2604 #else
2606 static void
2607 interp_entry_from_trampoline (gpointer ccontext_untyped, gpointer rmethod_untyped)
2609 g_assert_not_reached ();
2612 #endif /* MONO_ARCH_HAVE_INTERP_ENTRY_TRAMPOLINE */
2614 static InterpMethod*
2615 lookup_method_pointer (gpointer addr)
2617 MonoDomain *domain = mono_domain_get ();
2618 MonoJitDomainInfo *info = domain_jit_info (domain);
2619 InterpMethod *res = NULL;
2621 mono_domain_lock (domain);
2622 if (info->interp_method_pointer_hash)
2623 res = (InterpMethod*)g_hash_table_lookup (info->interp_method_pointer_hash, addr);
2624 mono_domain_unlock (domain);
2626 return res;
2629 #ifndef MONO_ARCH_HAVE_INTERP_NATIVE_TO_MANAGED
2630 static void
2631 interp_no_native_to_managed (void)
2633 g_error ("interpreter: native-to-managed transition not available on this platform");
2635 #endif
2637 static void
2638 no_llvmonly_interp_method_pointer (void)
2640 g_assert_not_reached ();
2644 * interp_create_method_pointer_llvmonly:
2646 * Return an ftndesc for entering the interpreter and executing METHOD.
2648 static MonoFtnDesc*
2649 interp_create_method_pointer_llvmonly (MonoMethod *method, gboolean unbox, MonoError *error)
2651 MonoDomain *domain = mono_domain_get ();
2652 gpointer addr, entry_func, entry_wrapper;
2653 MonoMethodSignature *sig;
2654 MonoMethod *wrapper;
2655 MonoJitDomainInfo *info;
2656 InterpMethod *imethod;
2658 imethod = mono_interp_get_imethod (domain, method, error);
2659 return_val_if_nok (error, NULL);
2661 if (unbox) {
2662 if (imethod->llvmonly_unbox_entry)
2663 return (MonoFtnDesc*)imethod->llvmonly_unbox_entry;
2664 } else {
2665 if (imethod->jit_entry)
2666 return (MonoFtnDesc*)imethod->jit_entry;
2669 sig = mono_method_signature_internal (method);
2672 * The entry functions need access to the method to call, so we have
2673 * to use a ftndesc. The caller uses a normal signature, while the
2674 * entry functions use a gsharedvt_in signature, so wrap the entry function in
2675 * a gsharedvt_in_sig wrapper.
2677 wrapper = mini_get_gsharedvt_in_sig_wrapper (sig);
2679 entry_wrapper = mono_jit_compile_method_jit_only (wrapper, error);
2680 mono_error_assertf_ok (error, "couldn't compile wrapper \"%s\" for \"%s\"",
2681 mono_method_get_name_full (wrapper, TRUE, TRUE, MONO_TYPE_NAME_FORMAT_IL),
2682 mono_method_get_name_full (method, TRUE, TRUE, MONO_TYPE_NAME_FORMAT_IL));
2684 if (sig->param_count > MAX_INTERP_ENTRY_ARGS) {
2685 g_assert_not_reached ();
2686 //entry_func = (gpointer)interp_entry_general;
2687 } else if (sig->hasthis) {
2688 if (sig->ret->type == MONO_TYPE_VOID)
2689 entry_func = entry_funcs_instance [sig->param_count];
2690 else
2691 entry_func = entry_funcs_instance_ret [sig->param_count];
2692 } else {
2693 if (sig->ret->type == MONO_TYPE_VOID)
2694 entry_func = entry_funcs_static [sig->param_count];
2695 else
2696 entry_func = entry_funcs_static_ret [sig->param_count];
2698 g_assert (entry_func);
2700 /* Encode unbox in the lower bit of imethod */
2701 gpointer entry_arg = imethod;
2702 if (unbox)
2703 entry_arg = (gpointer)(((gsize)entry_arg) | 1);
2704 MonoFtnDesc *entry_ftndesc = mini_llvmonly_create_ftndesc (mono_domain_get (), entry_func, entry_arg);
2706 addr = mini_llvmonly_create_ftndesc (mono_domain_get (), entry_wrapper, entry_ftndesc);
2708 info = domain_jit_info (domain);
2709 mono_domain_lock (domain);
2710 if (!info->interp_method_pointer_hash)
2711 info->interp_method_pointer_hash = g_hash_table_new (NULL, NULL);
2712 g_hash_table_insert (info->interp_method_pointer_hash, addr, imethod);
2713 mono_domain_unlock (domain);
2715 mono_memory_barrier ();
2716 if (unbox)
2717 imethod->llvmonly_unbox_entry = addr;
2718 else
2719 imethod->jit_entry = addr;
2721 return (MonoFtnDesc*)addr;
2725 * interp_create_method_pointer:
2727 * Return a function pointer which can be used to call METHOD using the
2728 * interpreter. Return NULL for methods which are not supported.
2730 static gpointer
2731 interp_create_method_pointer (MonoMethod *method, gboolean compile, MonoError *error)
2733 #ifndef MONO_ARCH_HAVE_INTERP_NATIVE_TO_MANAGED
2734 if (mono_llvm_only)
2735 return (gpointer)no_llvmonly_interp_method_pointer;
2736 return (gpointer)interp_no_native_to_managed;
2737 #else
2738 gpointer addr, entry_func, entry_wrapper = NULL;
2739 MonoDomain *domain = mono_domain_get ();
2740 MonoJitDomainInfo *info;
2741 InterpMethod *imethod = mono_interp_get_imethod (domain, method, error);
2743 if (mono_llvm_only)
2744 return (gpointer)no_llvmonly_interp_method_pointer;
2746 if (imethod->jit_entry)
2747 return imethod->jit_entry;
2749 if (compile && !imethod->transformed) {
2750 /* Return any errors from method compilation */
2751 mono_interp_transform_method (imethod, get_context (), error);
2752 return_val_if_nok (error, NULL);
2755 MonoMethodSignature *sig = mono_method_signature_internal (method);
2757 if (mono_llvm_only)
2758 /* The caller should call interp_create_method_pointer_llvmonly */
2759 g_assert_not_reached ();
2761 if (method->wrapper_type && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
2762 return imethod;
2764 #ifndef MONO_ARCH_HAVE_FTNPTR_ARG_TRAMPOLINE
2766 * Interp in wrappers get the argument in the rgctx register. If
2767 * MONO_ARCH_HAVE_FTNPTR_ARG_TRAMPOLINE is defined it means that
2768 * on that arch the rgctx register is not scratch, so we use a
2769 * separate temp register. We should update the wrappers for this
2770 * if we really care about those architectures (arm).
2772 MonoMethod *wrapper = mini_get_interp_in_wrapper (sig);
2774 entry_wrapper = mono_jit_compile_method_jit_only (wrapper, error);
2775 #endif
2776 if (entry_wrapper) {
2777 if (sig->param_count > MAX_INTERP_ENTRY_ARGS) {
2778 entry_func = (gpointer)interp_entry_general;
2779 } else if (sig->hasthis) {
2780 if (sig->ret->type == MONO_TYPE_VOID)
2781 entry_func = entry_funcs_instance [sig->param_count];
2782 else
2783 entry_func = entry_funcs_instance_ret [sig->param_count];
2784 } else {
2785 if (sig->ret->type == MONO_TYPE_VOID)
2786 entry_func = entry_funcs_static [sig->param_count];
2787 else
2788 entry_func = entry_funcs_static_ret [sig->param_count];
2790 } else {
2791 #ifndef MONO_ARCH_HAVE_INTERP_ENTRY_TRAMPOLINE
2792 mono_error_assertf_ok (error, "couldn't compile wrapper \"%s\" for \"%s\"",
2793 mono_method_get_name_full (wrapper, TRUE, TRUE, MONO_TYPE_NAME_FORMAT_IL),
2794 mono_method_get_name_full (method, TRUE, TRUE, MONO_TYPE_NAME_FORMAT_IL));
2795 #else
2796 mono_error_cleanup (error);
2797 error_init_reuse (error);
2798 if (!mono_native_to_interp_trampoline) {
2799 if (mono_aot_only) {
2800 mono_native_to_interp_trampoline = (MonoFuncV)mono_aot_get_trampoline ("native_to_interp_trampoline");
2801 } else {
2802 MonoTrampInfo *info;
2803 mono_native_to_interp_trampoline = (MonoFuncV)mono_arch_get_native_to_interp_trampoline (&info);
2804 mono_tramp_info_register (info, NULL);
2807 entry_wrapper = (gpointer)mono_native_to_interp_trampoline;
2808 /* We need the lmf wrapper only when being called from mixed mode */
2809 if (sig->pinvoke)
2810 entry_func = (gpointer)interp_entry_from_trampoline;
2811 else {
2812 static gpointer cached_func = NULL;
2813 if (!cached_func) {
2814 cached_func = mono_jit_compile_method_jit_only (mini_get_interp_lmf_wrapper ("mono_interp_entry_from_trampoline", (gpointer) mono_interp_entry_from_trampoline), error);
2815 mono_memory_barrier ();
2817 entry_func = cached_func;
2819 #endif
2822 g_assert (entry_func);
2823 /* This is the argument passed to the interp_in wrapper by the static rgctx trampoline */
2824 MonoFtnDesc *ftndesc = g_new0 (MonoFtnDesc, 1);
2825 ftndesc->addr = entry_func;
2826 ftndesc->arg = imethod;
2827 mono_error_assert_ok (error);
2830 * The wrapper is called by compiled code, which doesn't pass the extra argument, so we pass it in the
2831 * rgctx register using a trampoline.
2834 addr = mono_create_ftnptr_arg_trampoline (ftndesc, entry_wrapper);
2836 info = domain_jit_info (domain);
2837 mono_domain_lock (domain);
2838 if (!info->interp_method_pointer_hash)
2839 info->interp_method_pointer_hash = g_hash_table_new (NULL, NULL);
2840 g_hash_table_insert (info->interp_method_pointer_hash, addr, imethod);
2841 mono_domain_unlock (domain);
2843 mono_memory_barrier ();
2844 imethod->jit_entry = addr;
2846 return addr;
2847 #endif
2850 #if COUNT_OPS
2851 static int opcode_counts[512];
2853 #define COUNT_OP(op) opcode_counts[op]++
2854 #else
2855 #define COUNT_OP(op)
2856 #endif
2858 #if DEBUG_INTERP
2859 #define DUMP_INSTR() \
2860 if (tracing > 1) { \
2861 char *ins; \
2862 if (sp > frame->stack) { \
2863 ins = dump_stack (frame->stack, sp); \
2864 } else { \
2865 ins = g_strdup (""); \
2867 sp->data.l = 0; \
2868 output_indent (); \
2869 char *mn = mono_method_full_name (frame->imethod->method, FALSE); \
2870 char *disasm = mono_interp_dis_mintop(imethod->code, ip); \
2871 g_print ("(%p) %s -> %s\t%d:%s\n", mono_thread_internal_current (), mn, disasm, vt_sp - vtalloc, ins); \
2872 g_free (mn); \
2873 g_free (ins); \
2874 g_free (disasm); \
2876 #else
2877 #define DUMP_INSTR()
2878 #endif
2880 #define INIT_VTABLE(vtable) do { \
2881 if (G_UNLIKELY (!(vtable)->initialized)) { \
2882 mono_runtime_class_init_full ((vtable), error); \
2883 if (!is_ok (error)) \
2884 THROW_EX (mono_error_convert_to_exception (error), ip); \
2886 } while (0);
2889 * GC SAFETY:
2891 * The interpreter executes in gc unsafe (non-preempt) mode. On wasm, the C stack is
2892 * scannable but the wasm stack is not, so to make the code GC safe, the following rules
2893 * should be followed:
2894 * - every objref handled by the code needs to have a copy stored inside InterpFrame,
2895 * in stackval->data.o. For objrefs which are not yet on the IL stack, they can be stored
2896 * in frame->o. This will ensure the objects are pinned. The 'frame' local is assumed
2897 * to be allocated to the C stack and not to registers.
2898 * - minimalize the number of MonoObject* locals/arguments.
2901 #ifdef TARGET_WASM
2902 #define frame_objref(frame) (frame)->o
2903 #else
2904 #define frame_objref(frame) o
2905 #endif
2907 // This function is outlined to help save stack in its caller, on the premise
2908 // that it is relatively rarely called. This also lets it use alloca.
2909 static MONO_NEVER_INLINE void
2910 mono_interp_calli_nat_dynamic_pinvoke (
2911 // Parameters are sorted by name.
2912 InterpFrame* child_frame,
2913 guchar* code,
2914 ThreadContext *context,
2915 MonoMethodSignature* csignature,
2916 MonoError* error,
2917 InterpMethod* imethod)
2919 g_assert (imethod->method->dynamic && csignature->pinvoke);
2921 /* Pinvoke call is missing the wrapper. See mono_get_native_calli_wrapper */
2922 MonoMarshalSpec** mspecs = g_newa (MonoMarshalSpec*, csignature->param_count + 1);
2923 memset (mspecs, 0, sizeof (MonoMarshalSpec*) * (csignature->param_count + 1));
2925 MonoMethodPInvoke iinfo;
2926 memset (&iinfo, 0, sizeof (iinfo));
2928 MonoMethod* m = mono_marshal_get_native_func_wrapper (m_class_get_image (imethod->method->klass), csignature, &iinfo, mspecs, code);
2930 for (int i = csignature->param_count; i >= 0; i--)
2931 if (mspecs [i])
2932 mono_metadata_free_marshal_spec (mspecs [i]);
2934 child_frame->imethod = mono_interp_get_imethod (imethod->domain, m, error);
2935 mono_error_cleanup (error); /* FIXME: don't swallow the error */
2937 interp_exec_method (child_frame, context, error);
2941 * If EXIT_AT_FINALLY is not -1, exit after exiting the finally clause with that index.
2942 * If BASE_FRAME is not NULL, copy arguments/locals from BASE_FRAME.
2943 * The ERROR argument is used to avoid declaring an error object for every interp frame, its not used
2944 * to return error information.
2946 static void
2947 interp_exec_method_full (InterpFrame *frame, ThreadContext *context, FrameClauseArgs *clause_args, MonoError *error)
2949 InterpFrame child_frame;
2950 const unsigned short *ip = NULL;
2951 stackval *sp;
2952 InterpMethod *imethod = NULL;
2953 #if DEBUG_INTERP
2954 gint tracing = global_tracing;
2955 unsigned char *vtalloc;
2956 #endif
2957 int i32;
2958 unsigned char *vt_sp;
2959 unsigned char *locals = NULL;
2960 // See the comment about GC safety above
2961 MonoObject *o = NULL;
2962 MonoClass *c;
2963 #if USE_COMPUTED_GOTO
2964 static void *in_labels[] = {
2965 #define OPDEF(a,b,c,d) \
2966 &&LAB_ ## a,
2967 #include "mintops.def"
2968 0 };
2969 #endif
2971 frame->ex = NULL;
2972 frame->finally_ips = NULL;
2973 frame->endfinally_ip = NULL;
2975 #if DEBUG_INTERP
2976 debug_enter (frame, &tracing);
2977 #endif
2979 imethod = frame->imethod;
2980 if (!imethod->transformed) {
2981 #if DEBUG_INTERP
2982 char *mn = mono_method_full_name (imethod->method, TRUE);
2983 g_print ("(%p) Transforming %s\n", mono_thread_internal_current (), mn);
2984 g_free (mn);
2985 #endif
2987 frame->ip = NULL;
2988 do_transform_method (frame, context);
2989 if (frame->ex)
2990 THROW_EX (frame->ex, NULL);
2991 EXCEPTION_CHECKPOINT;
2994 if (!clause_args) {
2995 frame->args = g_newa (char, imethod->alloca_size);
2996 ip = imethod->code;
2997 } else {
2998 ip = clause_args->start_with_ip;
2999 if (clause_args->base_frame) {
3000 frame->args = g_newa (char, imethod->alloca_size);
3001 memcpy (frame->args, clause_args->base_frame->args, imethod->alloca_size);
3004 sp = frame->stack = (stackval *) (char *) frame->args;
3005 vt_sp = (unsigned char *) sp + imethod->stack_size;
3006 #if DEBUG_INTERP
3007 vtalloc = vt_sp;
3008 #endif
3009 locals = (unsigned char *) vt_sp + imethod->vt_stack_size;
3010 frame->locals = locals;
3011 child_frame.parent = frame;
3013 if (clause_args && clause_args->filter_exception) {
3014 sp->data.p = clause_args->filter_exception;
3015 sp++;
3018 //g_print ("(%p) Call %s\n", mono_thread_internal_current (), mono_method_get_full_name (frame->imethod->method));
3021 * using while (ip < end) may result in a 15% performance drop,
3022 * but it may be useful for debug
3024 while (1) {
3025 #ifndef USE_COMPUTED_GOTO
3026 main_loop:
3027 #endif
3028 /* g_assert (sp >= frame->stack); */
3029 /* g_assert(vt_sp - vtalloc <= imethod->vt_stack_size); */
3030 DUMP_INSTR();
3031 MINT_IN_SWITCH (*ip) {
3032 MINT_IN_CASE(MINT_INITLOCALS)
3033 memset (locals, 0, imethod->locals_size);
3034 ++ip;
3035 MINT_IN_BREAK;
3036 MINT_IN_CASE(MINT_NOP)
3037 ++ip;
3038 MINT_IN_BREAK;
3039 MINT_IN_CASE(MINT_NIY)
3040 g_error ("mint_niy: instruction not implemented yet. This shouldn't happen.");
3041 MINT_IN_BREAK;
3042 MINT_IN_CASE(MINT_BREAK)
3043 ++ip;
3044 do_debugger_tramp (mini_get_dbg_callbacks ()->user_break, frame);
3045 MINT_IN_BREAK;
3046 MINT_IN_CASE(MINT_BREAKPOINT)
3047 ++ip;
3048 mono_break ();
3049 MINT_IN_BREAK;
3050 MINT_IN_CASE(MINT_LDNULL)
3051 sp->data.p = NULL;
3052 ++ip;
3053 ++sp;
3054 MINT_IN_BREAK;
3055 MINT_IN_CASE(MINT_ARGLIST)
3056 g_assert (frame->varargs);
3057 sp->data.p = vt_sp;
3058 *(gpointer*)sp->data.p = frame->varargs;
3059 vt_sp += ALIGN_TO (sizeof (gpointer), MINT_VT_ALIGNMENT);
3060 ++ip;
3061 ++sp;
3062 MINT_IN_BREAK;
3063 MINT_IN_CASE(MINT_VTRESULT) {
3064 int ret_size = * (guint16 *)(ip + 1);
3065 unsigned char *ret_vt_sp = vt_sp;
3066 vt_sp -= READ32(ip + 2);
3067 if (ret_size > 0) {
3068 memmove (vt_sp, ret_vt_sp, ret_size);
3069 sp [-1].data.p = vt_sp;
3070 vt_sp += ALIGN_TO (ret_size, MINT_VT_ALIGNMENT);
3072 ip += 4;
3073 MINT_IN_BREAK;
3075 #define LDC(n) do { sp->data.i = (n); ++ip; ++sp; } while (0)
3076 MINT_IN_CASE(MINT_LDC_I4_M1)
3077 LDC(-1);
3078 MINT_IN_BREAK;
3079 MINT_IN_CASE(MINT_LDC_I4_0)
3080 LDC(0);
3081 MINT_IN_BREAK;
3082 MINT_IN_CASE(MINT_LDC_I4_1)
3083 LDC(1);
3084 MINT_IN_BREAK;
3085 MINT_IN_CASE(MINT_LDC_I4_2)
3086 LDC(2);
3087 MINT_IN_BREAK;
3088 MINT_IN_CASE(MINT_LDC_I4_3)
3089 LDC(3);
3090 MINT_IN_BREAK;
3091 MINT_IN_CASE(MINT_LDC_I4_4)
3092 LDC(4);
3093 MINT_IN_BREAK;
3094 MINT_IN_CASE(MINT_LDC_I4_5)
3095 LDC(5);
3096 MINT_IN_BREAK;
3097 MINT_IN_CASE(MINT_LDC_I4_6)
3098 LDC(6);
3099 MINT_IN_BREAK;
3100 MINT_IN_CASE(MINT_LDC_I4_7)
3101 LDC(7);
3102 MINT_IN_BREAK;
3103 MINT_IN_CASE(MINT_LDC_I4_8)
3104 LDC(8);
3105 MINT_IN_BREAK;
3106 MINT_IN_CASE(MINT_LDC_I4_S)
3107 sp->data.i = *(const short *)(ip + 1);
3108 ip += 2;
3109 ++sp;
3110 MINT_IN_BREAK;
3111 MINT_IN_CASE(MINT_LDC_I4)
3112 ++ip;
3113 sp->data.i = READ32 (ip);
3114 ip += 2;
3115 ++sp;
3116 MINT_IN_BREAK;
3117 MINT_IN_CASE(MINT_LDC_I8)
3118 ++ip;
3119 sp->data.l = READ64 (ip);
3120 ip += 4;
3121 ++sp;
3122 MINT_IN_BREAK;
3123 MINT_IN_CASE(MINT_LDC_I8_S)
3124 sp->data.l = *(const short *)(ip + 1);
3125 ip += 2;
3126 ++sp;
3127 MINT_IN_BREAK;
3128 MINT_IN_CASE(MINT_LDC_R4) {
3129 guint32 val;
3130 ++ip;
3131 val = READ32(ip);
3132 sp->data.f_r4 = * (float *)&val;
3133 ip += 2;
3134 ++sp;
3135 MINT_IN_BREAK;
3137 MINT_IN_CASE(MINT_LDC_R8)
3138 sp->data.l = READ64 (ip + 1); /* note union usage */
3139 ip += 5;
3140 ++sp;
3141 MINT_IN_BREAK;
3142 MINT_IN_CASE(MINT_DUP)
3143 sp [0] = sp[-1];
3144 ++sp;
3145 ++ip;
3146 MINT_IN_BREAK;
3147 MINT_IN_CASE(MINT_DUP_VT)
3148 i32 = READ32 (ip + 1);
3149 sp->data.p = vt_sp;
3150 memcpy(sp->data.p, sp [-1].data.p, i32);
3151 vt_sp += ALIGN_TO (i32, MINT_VT_ALIGNMENT);
3152 ++sp;
3153 ip += 3;
3154 MINT_IN_BREAK;
3155 MINT_IN_CASE(MINT_POP) {
3156 guint16 u16 = (* (guint16 *)(ip + 1)) + 1;
3157 if (u16 > 1)
3158 memmove (sp - u16, sp - 1, (u16 - 1) * sizeof (stackval));
3159 sp--;
3160 ip += 2;
3161 MINT_IN_BREAK;
3163 MINT_IN_CASE(MINT_JMP) {
3164 InterpMethod *new_method = (InterpMethod*)imethod->data_items [* (guint16 *)(ip + 1)];
3165 gboolean realloc_frame = new_method->alloca_size > imethod->alloca_size;
3167 if (imethod->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_TAIL_CALL)
3168 MONO_PROFILER_RAISE (method_tail_call, (imethod->method, new_method->method));
3170 if (!new_method->transformed) {
3171 MONO_API_ERROR_INIT (error);
3173 frame->ip = ip;
3174 mono_interp_transform_method (new_method, context, error);
3175 frame->ex = mono_error_convert_to_exception (error);
3176 if (frame->ex)
3177 goto exit_frame;
3179 ip += 2;
3180 imethod = frame->imethod = new_method;
3182 * We allocate the stack frame from scratch and store the arguments in the
3183 * locals again since it's possible for the caller stack frame to be smaller
3184 * than the callee stack frame (at the interp level)
3186 if (realloc_frame) {
3187 frame->args = g_newa (char, imethod->alloca_size);
3188 memset (frame->args, 0, imethod->alloca_size);
3189 sp = frame->stack = (stackval *) frame->args;
3191 vt_sp = (unsigned char *) sp + imethod->stack_size;
3192 #if DEBUG_INTERP
3193 vtalloc = vt_sp;
3194 #endif
3195 locals = vt_sp + imethod->vt_stack_size;
3196 frame->locals = locals;
3197 ip = imethod->code;
3198 MINT_IN_BREAK;
3200 MINT_IN_CASE(MINT_CALLI) {
3201 MonoMethodSignature *csignature;
3202 stackval *endsp = sp;
3204 frame->ip = ip;
3206 csignature = (MonoMethodSignature*)imethod->data_items [* (guint16 *)(ip + 1)];
3207 ip += 2;
3208 --sp;
3209 --endsp;
3210 child_frame.imethod = (InterpMethod*)sp->data.p;
3212 sp->data.p = vt_sp;
3213 child_frame.retval = sp;
3214 /* decrement by the actual number of args */
3215 sp -= csignature->param_count;
3216 if (csignature->hasthis)
3217 --sp;
3218 child_frame.stack_args = sp;
3220 if (child_frame.imethod->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
3221 child_frame.imethod = mono_interp_get_imethod (imethod->domain, mono_marshal_get_native_wrapper (child_frame.imethod->method, FALSE, FALSE), error);
3222 mono_error_cleanup (error); /* FIXME: don't swallow the error */
3225 if (csignature->hasthis) {
3226 MonoObject *this_arg = (MonoObject*)sp->data.p;
3228 if (m_class_is_valuetype (this_arg->vtable->klass)) {
3229 gpointer unboxed = mono_object_unbox_internal (this_arg);
3230 sp [0].data.p = unboxed;
3234 interp_exec_method (&child_frame, context, error);
3236 CHECK_RESUME_STATE (context);
3238 /* need to handle typedbyref ... */
3239 if (csignature->ret->type != MONO_TYPE_VOID) {
3240 *sp = *endsp;
3241 sp++;
3243 MINT_IN_BREAK;
3245 MINT_IN_CASE(MINT_CALLI_NAT_FAST) {
3246 gpointer target_ip = sp [-1].data.p;
3247 MonoMethodSignature *csignature = (MonoMethodSignature*)imethod->data_items [* (guint16 *)(ip + 1)];
3248 int opcode = *(guint16 *)(ip + 2);
3249 gboolean save_last_error = *(guint16 *)(ip + 3);
3251 sp--;
3252 frame->ip = ip;
3254 sp = do_icall_wrapper (frame, csignature, opcode, sp, target_ip, save_last_error);
3255 EXCEPTION_CHECKPOINT;
3256 CHECK_RESUME_STATE (context);
3257 ip += 4;
3258 MINT_IN_BREAK;
3260 MINT_IN_CASE(MINT_CALLI_NAT) {
3262 stackval *endsp = sp;
3263 frame->ip = ip;
3265 MonoMethodSignature* csignature = (MonoMethodSignature*)imethod->data_items [* (guint16 *)(ip + 1)];
3267 ip += 3;
3268 --sp;
3269 --endsp;
3270 guchar* const code = (guchar*)sp->data.p;
3271 child_frame.imethod = NULL;
3273 sp->data.p = vt_sp;
3274 child_frame.retval = sp;
3275 /* decrement by the actual number of args */
3276 sp -= csignature->param_count;
3277 if (csignature->hasthis)
3278 --sp;
3279 child_frame.stack_args = sp;
3281 if (imethod->method->dynamic && csignature->pinvoke) {
3282 mono_interp_calli_nat_dynamic_pinvoke (&child_frame, code, context, csignature, error, imethod);
3283 } else {
3284 const gboolean save_last_error = *(guint16 *)(ip - 3 + 2);
3285 ves_pinvoke_method (&child_frame, csignature, (MonoFuncV) code, FALSE, context, save_last_error);
3288 CHECK_RESUME_STATE (context);
3290 /* need to handle typedbyref ... */
3291 if (csignature->ret->type != MONO_TYPE_VOID) {
3292 *sp = *endsp;
3293 sp++;
3295 MINT_IN_BREAK;
3297 MINT_IN_CASE(MINT_CALLVIRT_FAST)
3298 MINT_IN_CASE(MINT_VCALLVIRT_FAST) {
3299 MonoObject *this_arg;
3300 MonoClass *this_class;
3301 gboolean is_void = *ip == MINT_VCALLVIRT_FAST;
3302 InterpMethod *target_imethod;
3303 stackval *endsp = sp;
3304 int slot;
3306 // FIXME Have it handle also remoting calls and use a single opcode for virtual calls
3308 frame->ip = ip;
3310 target_imethod = (InterpMethod*)imethod->data_items [* (guint16 *)(ip + 1)];
3311 slot = *(gint16*)(ip + 2);
3312 ip += 3;
3313 sp->data.p = vt_sp;
3314 child_frame.retval = sp;
3316 /* decrement by the actual number of args */
3317 sp -= target_imethod->param_count + target_imethod->hasthis;
3318 child_frame.stack_args = sp;
3320 this_arg = (MonoObject*)sp->data.p;
3321 this_class = this_arg->vtable->klass;
3323 child_frame.imethod = get_virtual_method_fast (target_imethod, this_arg->vtable, slot);
3324 if (m_class_is_valuetype (this_class) && m_class_is_valuetype (child_frame.imethod->method->klass)) {
3325 /* unbox */
3326 gpointer unboxed = mono_object_unbox_internal (this_arg);
3327 sp [0].data.p = unboxed;
3330 interp_exec_method (&child_frame, context, error);
3332 CHECK_RESUME_STATE (context);
3334 if (!is_void) {
3335 /* need to handle typedbyref ... */
3336 *sp = *endsp;
3337 sp++;
3339 MINT_IN_BREAK;
3341 MINT_IN_CASE(MINT_CALL_VARARG) {
3342 stackval *endsp = sp;
3343 int num_varargs = 0;
3344 MonoMethodSignature *csig;
3346 frame->ip = ip;
3348 child_frame.imethod = (InterpMethod*)imethod->data_items [* (guint16 *)(ip + 1)];
3349 /* The real signature for vararg calls */
3350 csig = (MonoMethodSignature*) imethod->data_items [* (guint16*) (ip + 2)];
3351 /* Push all vararg arguments from normal sp to vt_sp together with the signature */
3352 num_varargs = csig->param_count - csig->sentinelpos;
3353 child_frame.varargs = (char*) vt_sp;
3354 vt_sp = copy_varargs_vtstack (csig, sp, vt_sp);
3356 ip += 3;
3357 sp->data.p = vt_sp;
3358 child_frame.retval = sp;
3360 /* decrement by the actual number of args */
3361 sp -= child_frame.imethod->param_count + child_frame.imethod->hasthis + num_varargs;
3362 child_frame.stack_args = sp;
3364 interp_exec_method (&child_frame, context, error);
3366 CHECK_RESUME_STATE (context);
3368 if (csig->ret->type != MONO_TYPE_VOID) {
3369 *sp = *endsp;
3370 sp++;
3372 MINT_IN_BREAK;
3374 MINT_IN_CASE(MINT_CALL)
3375 MINT_IN_CASE(MINT_VCALL)
3376 MINT_IN_CASE(MINT_CALLVIRT)
3377 MINT_IN_CASE(MINT_VCALLVIRT) {
3378 gboolean is_void = *ip == MINT_VCALL || *ip == MINT_VCALLVIRT;
3379 gboolean is_virtual = *ip == MINT_CALLVIRT || *ip == MINT_VCALLVIRT;
3380 stackval *endsp = sp;
3382 frame->ip = ip;
3384 child_frame.imethod = (InterpMethod*)imethod->data_items [* (guint16 *)(ip + 1)];
3385 ip += 2;
3386 sp->data.p = vt_sp;
3387 child_frame.retval = sp;
3389 /* decrement by the actual number of args */
3390 sp -= child_frame.imethod->param_count + child_frame.imethod->hasthis;
3391 child_frame.stack_args = sp;
3393 if (is_virtual) {
3394 MonoObject *this_arg = (MonoObject*)sp->data.p;
3395 MonoClass *this_class = this_arg->vtable->klass;
3397 child_frame.imethod = get_virtual_method (child_frame.imethod, this_arg->vtable);
3398 if (m_class_is_valuetype (this_class) && m_class_is_valuetype (child_frame.imethod->method->klass)) {
3399 /* unbox */
3400 gpointer unboxed = mono_object_unbox_internal (this_arg);
3401 sp [0].data.p = unboxed;
3405 interp_exec_method (&child_frame, context, error);
3407 CHECK_RESUME_STATE (context);
3409 if (!is_void) {
3410 /* need to handle typedbyref ... */
3411 *sp = *endsp;
3412 sp++;
3414 MINT_IN_BREAK;
3416 MINT_IN_CASE(MINT_JIT_CALL) {
3417 InterpMethod *rmethod = (InterpMethod*)imethod->data_items [* (guint16 *)(ip + 1)];
3418 MONO_API_ERROR_INIT (error);
3419 frame->ip = ip;
3420 sp = do_jit_call (sp, vt_sp, context, frame, rmethod, error);
3421 if (!is_ok (error)) {
3422 MonoException *ex = mono_error_convert_to_exception (error);
3423 THROW_EX (ex, ip);
3425 ip += 2;
3427 CHECK_RESUME_STATE (context);
3429 if (rmethod->rtype->type != MONO_TYPE_VOID)
3430 sp++;
3432 MINT_IN_BREAK;
3434 MINT_IN_CASE(MINT_CALLRUN) {
3435 MonoMethod *target_method = (MonoMethod*) imethod->data_items [* (guint16 *)(ip + 1)];
3436 MonoMethodSignature *sig = (MonoMethodSignature*) imethod->data_items [* (guint16 *)(ip + 2)];
3437 stackval *retval;
3439 sp->data.p = vt_sp;
3440 retval = sp;
3442 sp -= sig->param_count;
3443 if (sig->hasthis)
3444 sp--;
3446 ves_imethod (frame, target_method, sig, sp, retval);
3447 if (frame->ex)
3448 THROW_EX (frame->ex, ip);
3450 if (sig->ret->type != MONO_TYPE_VOID) {
3451 *sp = *retval;
3452 sp++;
3454 ip += 3;
3455 MINT_IN_BREAK;
3457 MINT_IN_CASE(MINT_RET)
3458 --sp;
3459 *frame->retval = *sp;
3460 if (sp > frame->stack)
3461 g_warning ("ret: more values on stack: %d", sp-frame->stack);
3462 goto exit_frame;
3463 MINT_IN_CASE(MINT_RET_VOID)
3464 if (sp > frame->stack)
3465 g_warning ("ret.void: more values on stack: %d %s", sp-frame->stack, mono_method_full_name (imethod->method, TRUE));
3466 goto exit_frame;
3467 MINT_IN_CASE(MINT_RET_VT)
3468 i32 = READ32(ip + 1);
3469 --sp;
3470 memcpy(frame->retval->data.p, sp->data.p, i32);
3471 if (sp > frame->stack)
3472 g_warning ("ret.vt: more values on stack: %d", sp-frame->stack);
3473 goto exit_frame;
3474 MINT_IN_CASE(MINT_BR_S)
3475 ip += (short) *(ip + 1);
3476 MINT_IN_BREAK;
3477 MINT_IN_CASE(MINT_BR)
3478 ip += (gint32) READ32(ip + 1);
3479 MINT_IN_BREAK;
3480 #define ZEROP_S(datamem, op) \
3481 --sp; \
3482 if (sp->data.datamem op 0) \
3483 ip += * (gint16 *)(ip + 1); \
3484 else \
3485 ip += 2;
3487 #define ZEROP(datamem, op) \
3488 --sp; \
3489 if (sp->data.datamem op 0) \
3490 ip += (gint32)READ32(ip + 1); \
3491 else \
3492 ip += 3;
3494 MINT_IN_CASE(MINT_BRFALSE_I4_S)
3495 ZEROP_S(i, ==);
3496 MINT_IN_BREAK;
3497 MINT_IN_CASE(MINT_BRFALSE_I8_S)
3498 ZEROP_S(l, ==);
3499 MINT_IN_BREAK;
3500 MINT_IN_CASE(MINT_BRFALSE_R4_S)
3501 ZEROP_S(f_r4, ==);
3502 MINT_IN_BREAK;
3503 MINT_IN_CASE(MINT_BRFALSE_R8_S)
3504 ZEROP_S(f, ==);
3505 MINT_IN_BREAK;
3506 MINT_IN_CASE(MINT_BRFALSE_I4)
3507 ZEROP(i, ==);
3508 MINT_IN_BREAK;
3509 MINT_IN_CASE(MINT_BRFALSE_I8)
3510 ZEROP(l, ==);
3511 MINT_IN_BREAK;
3512 MINT_IN_CASE(MINT_BRFALSE_R4)
3513 ZEROP_S(f_r4, ==);
3514 MINT_IN_BREAK;
3515 MINT_IN_CASE(MINT_BRFALSE_R8)
3516 ZEROP_S(f, ==);
3517 MINT_IN_BREAK;
3518 MINT_IN_CASE(MINT_BRTRUE_I4_S)
3519 ZEROP_S(i, !=);
3520 MINT_IN_BREAK;
3521 MINT_IN_CASE(MINT_BRTRUE_I8_S)
3522 ZEROP_S(l, !=);
3523 MINT_IN_BREAK;
3524 MINT_IN_CASE(MINT_BRTRUE_R4_S)
3525 ZEROP_S(f_r4, !=);
3526 MINT_IN_BREAK;
3527 MINT_IN_CASE(MINT_BRTRUE_R8_S)
3528 ZEROP_S(f, !=);
3529 MINT_IN_BREAK;
3530 MINT_IN_CASE(MINT_BRTRUE_I4)
3531 ZEROP(i, !=);
3532 MINT_IN_BREAK;
3533 MINT_IN_CASE(MINT_BRTRUE_I8)
3534 ZEROP(l, !=);
3535 MINT_IN_BREAK;
3536 MINT_IN_CASE(MINT_BRTRUE_R4)
3537 ZEROP(f_r4, !=);
3538 MINT_IN_BREAK;
3539 MINT_IN_CASE(MINT_BRTRUE_R8)
3540 ZEROP(f, !=);
3541 MINT_IN_BREAK;
3542 #define CONDBR_S(cond) \
3543 sp -= 2; \
3544 if (cond) \
3545 ip += * (gint16 *)(ip + 1); \
3546 else \
3547 ip += 2;
3548 #define BRELOP_S(datamem, op) \
3549 CONDBR_S(sp[0].data.datamem op sp[1].data.datamem)
3551 #define CONDBR(cond) \
3552 sp -= 2; \
3553 if (cond) \
3554 ip += (gint32)READ32(ip + 1); \
3555 else \
3556 ip += 3;
3558 #define BRELOP(datamem, op) \
3559 CONDBR(sp[0].data.datamem op sp[1].data.datamem)
3561 MINT_IN_CASE(MINT_BEQ_I4_S)
3562 BRELOP_S(i, ==)
3563 MINT_IN_BREAK;
3564 MINT_IN_CASE(MINT_BEQ_I8_S)
3565 BRELOP_S(l, ==)
3566 MINT_IN_BREAK;
3567 MINT_IN_CASE(MINT_BEQ_R4_S)
3568 CONDBR_S(!isunordered (sp [0].data.f_r4, sp [1].data.f_r4) && sp[0].data.f_r4 == sp[1].data.f_r4)
3569 MINT_IN_BREAK;
3570 MINT_IN_CASE(MINT_BEQ_R8_S)
3571 CONDBR_S(!mono_isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
3572 MINT_IN_BREAK;
3573 MINT_IN_CASE(MINT_BEQ_I4)
3574 BRELOP(i, ==)
3575 MINT_IN_BREAK;
3576 MINT_IN_CASE(MINT_BEQ_I8)
3577 BRELOP(l, ==)
3578 MINT_IN_BREAK;
3579 MINT_IN_CASE(MINT_BEQ_R4)
3580 CONDBR(!isunordered (sp [0].data.f_r4, sp [1].data.f_r4) && sp[0].data.f_r4 == sp[1].data.f_r4)
3581 MINT_IN_BREAK;
3582 MINT_IN_CASE(MINT_BEQ_R8)
3583 CONDBR(!mono_isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
3584 MINT_IN_BREAK;
3585 MINT_IN_CASE(MINT_BGE_I4_S)
3586 BRELOP_S(i, >=)
3587 MINT_IN_BREAK;
3588 MINT_IN_CASE(MINT_BGE_I8_S)
3589 BRELOP_S(l, >=)
3590 MINT_IN_BREAK;
3591 MINT_IN_CASE(MINT_BGE_R4_S)
3592 CONDBR_S(!isunordered (sp [0].data.f_r4, sp [1].data.f_r4) && sp[0].data.f_r4 >= sp[1].data.f_r4)
3593 MINT_IN_BREAK;
3594 MINT_IN_CASE(MINT_BGE_R8_S)
3595 CONDBR_S(!mono_isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
3596 MINT_IN_BREAK;
3597 MINT_IN_CASE(MINT_BGE_I4)
3598 BRELOP(i, >=)
3599 MINT_IN_BREAK;
3600 MINT_IN_CASE(MINT_BGE_I8)
3601 BRELOP(l, >=)
3602 MINT_IN_BREAK;
3603 MINT_IN_CASE(MINT_BGE_R4)
3604 CONDBR(!isunordered (sp [0].data.f_r4, sp [1].data.f_r4) && sp[0].data.f_r4 >= sp[1].data.f_r4)
3605 MINT_IN_BREAK;
3606 MINT_IN_CASE(MINT_BGE_R8)
3607 CONDBR(!mono_isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
3608 MINT_IN_BREAK;
3609 MINT_IN_CASE(MINT_BGT_I4_S)
3610 BRELOP_S(i, >)
3611 MINT_IN_BREAK;
3612 MINT_IN_CASE(MINT_BGT_I8_S)
3613 BRELOP_S(l, >)
3614 MINT_IN_BREAK;
3615 MINT_IN_CASE(MINT_BGT_R4_S)
3616 CONDBR_S(!isunordered (sp [0].data.f_r4, sp [1].data.f_r4) && sp[0].data.f_r4 > sp[1].data.f_r4)
3617 MINT_IN_BREAK;
3618 MINT_IN_CASE(MINT_BGT_R8_S)
3619 CONDBR_S(!mono_isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
3620 MINT_IN_BREAK;
3621 MINT_IN_CASE(MINT_BGT_I4)
3622 BRELOP(i, >)
3623 MINT_IN_BREAK;
3624 MINT_IN_CASE(MINT_BGT_I8)
3625 BRELOP(l, >)
3626 MINT_IN_BREAK;
3627 MINT_IN_CASE(MINT_BGT_R4)
3628 CONDBR(!isunordered (sp [0].data.f_r4, sp [1].data.f_r4) && sp[0].data.f_r4 > sp[1].data.f_r4)
3629 MINT_IN_BREAK;
3630 MINT_IN_CASE(MINT_BGT_R8)
3631 CONDBR(!mono_isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
3632 MINT_IN_BREAK;
3633 MINT_IN_CASE(MINT_BLT_I4_S)
3634 BRELOP_S(i, <)
3635 MINT_IN_BREAK;
3636 MINT_IN_CASE(MINT_BLT_I8_S)
3637 BRELOP_S(l, <)
3638 MINT_IN_BREAK;
3639 MINT_IN_CASE(MINT_BLT_R4_S)
3640 CONDBR_S(!isunordered (sp [0].data.f_r4, sp [1].data.f_r4) && sp[0].data.f_r4 < sp[1].data.f_r4)
3641 MINT_IN_BREAK;
3642 MINT_IN_CASE(MINT_BLT_R8_S)
3643 CONDBR_S(!mono_isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
3644 MINT_IN_BREAK;
3645 MINT_IN_CASE(MINT_BLT_I4)
3646 BRELOP(i, <)
3647 MINT_IN_BREAK;
3648 MINT_IN_CASE(MINT_BLT_I8)
3649 BRELOP(l, <)
3650 MINT_IN_BREAK;
3651 MINT_IN_CASE(MINT_BLT_R4)
3652 CONDBR(!isunordered (sp [0].data.f_r4, sp [1].data.f_r4) && sp[0].data.f_r4 < sp[1].data.f_r4)
3653 MINT_IN_BREAK;
3654 MINT_IN_CASE(MINT_BLT_R8)
3655 CONDBR(!mono_isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
3656 MINT_IN_BREAK;
3657 MINT_IN_CASE(MINT_BLE_I4_S)
3658 BRELOP_S(i, <=)
3659 MINT_IN_BREAK;
3660 MINT_IN_CASE(MINT_BLE_I8_S)
3661 BRELOP_S(l, <=)
3662 MINT_IN_BREAK;
3663 MINT_IN_CASE(MINT_BLE_R4_S)
3664 CONDBR_S(!isunordered (sp [0].data.f_r4, sp [1].data.f_r4) && sp[0].data.f_r4 <= sp[1].data.f_r4)
3665 MINT_IN_BREAK;
3666 MINT_IN_CASE(MINT_BLE_R8_S)
3667 CONDBR_S(!mono_isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
3668 MINT_IN_BREAK;
3669 MINT_IN_CASE(MINT_BLE_I4)
3670 BRELOP(i, <=)
3671 MINT_IN_BREAK;
3672 MINT_IN_CASE(MINT_BLE_I8)
3673 BRELOP(l, <=)
3674 MINT_IN_BREAK;
3675 MINT_IN_CASE(MINT_BLE_R4)
3676 CONDBR(!isunordered (sp [0].data.f_r4, sp [1].data.f_r4) && sp[0].data.f_r4 <= sp[1].data.f_r4)
3677 MINT_IN_BREAK;
3678 MINT_IN_CASE(MINT_BLE_R8)
3679 CONDBR(!mono_isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
3680 MINT_IN_BREAK;
3681 MINT_IN_CASE(MINT_BNE_UN_I4_S)
3682 BRELOP_S(i, !=)
3683 MINT_IN_BREAK;
3684 MINT_IN_CASE(MINT_BNE_UN_I8_S)
3685 BRELOP_S(l, !=)
3686 MINT_IN_BREAK;
3687 MINT_IN_CASE(MINT_BNE_UN_R4_S)
3688 CONDBR_S(isunordered (sp [0].data.f_r4, sp [1].data.f_r4) || sp[0].data.f_r4 != sp[1].data.f_r4)
3689 MINT_IN_BREAK;
3690 MINT_IN_CASE(MINT_BNE_UN_R8_S)
3691 CONDBR_S(mono_isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
3692 MINT_IN_BREAK;
3693 MINT_IN_CASE(MINT_BNE_UN_I4)
3694 BRELOP(i, !=)
3695 MINT_IN_BREAK;
3696 MINT_IN_CASE(MINT_BNE_UN_I8)
3697 BRELOP(l, !=)
3698 MINT_IN_BREAK;
3699 MINT_IN_CASE(MINT_BNE_UN_R4)
3700 CONDBR(isunordered (sp [0].data.f_r4, sp [1].data.f_r4) || sp[0].data.f_r4 != sp[1].data.f_r4)
3701 MINT_IN_BREAK;
3702 MINT_IN_CASE(MINT_BNE_UN_R8)
3703 CONDBR(mono_isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
3704 MINT_IN_BREAK;
3706 #define BRELOP_S_CAST(datamem, op, type) \
3707 sp -= 2; \
3708 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
3709 ip += * (gint16 *)(ip + 1); \
3710 else \
3711 ip += 2;
3713 #define BRELOP_CAST(datamem, op, type) \
3714 sp -= 2; \
3715 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
3716 ip += (gint32)READ32(ip + 1); \
3717 else \
3718 ip += 3;
3720 MINT_IN_CASE(MINT_BGE_UN_I4_S)
3721 BRELOP_S_CAST(i, >=, guint32);
3722 MINT_IN_BREAK;
3723 MINT_IN_CASE(MINT_BGE_UN_I8_S)
3724 BRELOP_S_CAST(l, >=, guint64);
3725 MINT_IN_BREAK;
3726 MINT_IN_CASE(MINT_BGE_UN_R4_S)
3727 CONDBR_S(isunordered (sp [0].data.f_r4, sp [1].data.f_r4) || sp[0].data.f_r4 >= sp[1].data.f_r4)
3728 MINT_IN_BREAK;
3729 MINT_IN_CASE(MINT_BGE_UN_R8_S)
3730 CONDBR_S(mono_isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
3731 MINT_IN_BREAK;
3732 MINT_IN_CASE(MINT_BGE_UN_I4)
3733 BRELOP_CAST(i, >=, guint32);
3734 MINT_IN_BREAK;
3735 MINT_IN_CASE(MINT_BGE_UN_I8)
3736 BRELOP_CAST(l, >=, guint64);
3737 MINT_IN_BREAK;
3738 MINT_IN_CASE(MINT_BGE_UN_R4)
3739 CONDBR(isunordered (sp [0].data.f_r4, sp [1].data.f_r4) || sp[0].data.f_r4 >= sp[1].data.f_r4)
3740 MINT_IN_BREAK;
3741 MINT_IN_CASE(MINT_BGE_UN_R8)
3742 CONDBR(mono_isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
3743 MINT_IN_BREAK;
3744 MINT_IN_CASE(MINT_BGT_UN_I4_S)
3745 BRELOP_S_CAST(i, >, guint32);
3746 MINT_IN_BREAK;
3747 MINT_IN_CASE(MINT_BGT_UN_I8_S)
3748 BRELOP_S_CAST(l, >, guint64);
3749 MINT_IN_BREAK;
3750 MINT_IN_CASE(MINT_BGT_UN_R4_S)
3751 CONDBR_S(isunordered (sp [0].data.f_r4, sp [1].data.f_r4) || sp[0].data.f_r4 > sp[1].data.f_r4)
3752 MINT_IN_BREAK;
3753 MINT_IN_CASE(MINT_BGT_UN_R8_S)
3754 CONDBR_S(mono_isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
3755 MINT_IN_BREAK;
3756 MINT_IN_CASE(MINT_BGT_UN_I4)
3757 BRELOP_CAST(i, >, guint32);
3758 MINT_IN_BREAK;
3759 MINT_IN_CASE(MINT_BGT_UN_I8)
3760 BRELOP_CAST(l, >, guint64);
3761 MINT_IN_BREAK;
3762 MINT_IN_CASE(MINT_BGT_UN_R4)
3763 CONDBR(isunordered (sp [0].data.f_r4, sp [1].data.f_r4) || sp[0].data.f_r4 > sp[1].data.f_r4)
3764 MINT_IN_BREAK;
3765 MINT_IN_CASE(MINT_BGT_UN_R8)
3766 CONDBR(mono_isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
3767 MINT_IN_BREAK;
3768 MINT_IN_CASE(MINT_BLE_UN_I4_S)
3769 BRELOP_S_CAST(i, <=, guint32);
3770 MINT_IN_BREAK;
3771 MINT_IN_CASE(MINT_BLE_UN_I8_S)
3772 BRELOP_S_CAST(l, <=, guint64);
3773 MINT_IN_BREAK;
3774 MINT_IN_CASE(MINT_BLE_UN_R4_S)
3775 CONDBR_S(isunordered (sp [0].data.f_r4, sp [1].data.f_r4) || sp[0].data.f_r4 <= sp[1].data.f_r4)
3776 MINT_IN_BREAK;
3777 MINT_IN_CASE(MINT_BLE_UN_R8_S)
3778 CONDBR_S(mono_isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
3779 MINT_IN_BREAK;
3780 MINT_IN_CASE(MINT_BLE_UN_I4)
3781 BRELOP_CAST(i, <=, guint32);
3782 MINT_IN_BREAK;
3783 MINT_IN_CASE(MINT_BLE_UN_I8)
3784 BRELOP_CAST(l, <=, guint64);
3785 MINT_IN_BREAK;
3786 MINT_IN_CASE(MINT_BLE_UN_R4)
3787 CONDBR(isunordered (sp [0].data.f_r4, sp [1].data.f_r4) || sp[0].data.f_r4 <= sp[1].data.f_r4)
3788 MINT_IN_BREAK;
3789 MINT_IN_CASE(MINT_BLE_UN_R8)
3790 CONDBR(mono_isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
3791 MINT_IN_BREAK;
3792 MINT_IN_CASE(MINT_BLT_UN_I4_S)
3793 BRELOP_S_CAST(i, <, guint32);
3794 MINT_IN_BREAK;
3795 MINT_IN_CASE(MINT_BLT_UN_I8_S)
3796 BRELOP_S_CAST(l, <, guint64);
3797 MINT_IN_BREAK;
3798 MINT_IN_CASE(MINT_BLT_UN_R4_S)
3799 CONDBR_S(isunordered (sp [0].data.f_r4, sp [1].data.f_r4) || sp[0].data.f_r4 < sp[1].data.f_r4)
3800 MINT_IN_BREAK;
3801 MINT_IN_CASE(MINT_BLT_UN_R8_S)
3802 CONDBR_S(mono_isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
3803 MINT_IN_BREAK;
3804 MINT_IN_CASE(MINT_BLT_UN_I4)
3805 BRELOP_CAST(i, <, guint32);
3806 MINT_IN_BREAK;
3807 MINT_IN_CASE(MINT_BLT_UN_I8)
3808 BRELOP_CAST(l, <, guint64);
3809 MINT_IN_BREAK;
3810 MINT_IN_CASE(MINT_BLT_UN_R4)
3811 CONDBR(isunordered (sp [0].data.f_r4, sp [1].data.f_r4) || sp[0].data.f_r4 < sp[1].data.f_r4)
3812 MINT_IN_BREAK;
3813 MINT_IN_CASE(MINT_BLT_UN_R8)
3814 CONDBR(mono_isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
3815 MINT_IN_BREAK;
3816 MINT_IN_CASE(MINT_SWITCH) {
3817 guint32 n;
3818 const unsigned short *st;
3819 ++ip;
3820 n = READ32 (ip);
3821 ip += 2;
3822 st = ip + 2 * n;
3823 --sp;
3824 if ((guint32)sp->data.i < n) {
3825 gint offset;
3826 ip += 2 * (guint32)sp->data.i;
3827 offset = READ32 (ip);
3828 ip = ip + offset;
3829 } else {
3830 ip = st;
3832 MINT_IN_BREAK;
3834 MINT_IN_CASE(MINT_LDIND_I1_CHECK)
3835 NULL_CHECK (sp [-1].data.p);
3836 ++ip;
3837 sp[-1].data.i = *(gint8*)sp[-1].data.p;
3838 MINT_IN_BREAK;
3839 MINT_IN_CASE(MINT_LDIND_U1_CHECK)
3840 NULL_CHECK (sp [-1].data.p);
3841 ++ip;
3842 sp[-1].data.i = *(guint8*)sp[-1].data.p;
3843 MINT_IN_BREAK;
3844 MINT_IN_CASE(MINT_LDIND_I2_CHECK)
3845 NULL_CHECK (sp [-1].data.p);
3846 ++ip;
3847 sp[-1].data.i = *(gint16*)sp[-1].data.p;
3848 MINT_IN_BREAK;
3849 MINT_IN_CASE(MINT_LDIND_U2_CHECK)
3850 NULL_CHECK (sp [-1].data.p);
3851 ++ip;
3852 sp[-1].data.i = *(guint16*)sp[-1].data.p;
3853 MINT_IN_BREAK;
3854 MINT_IN_CASE(MINT_LDIND_I4_CHECK) /* Fall through */
3855 MINT_IN_CASE(MINT_LDIND_U4_CHECK)
3856 NULL_CHECK (sp [-1].data.p);
3857 ++ip;
3858 sp[-1].data.i = *(gint32*)sp[-1].data.p;
3859 MINT_IN_BREAK;
3860 MINT_IN_CASE(MINT_LDIND_I8_CHECK)
3861 NULL_CHECK (sp [-1].data.p);
3862 ++ip;
3863 #ifdef NO_UNALIGNED_ACCESS
3864 if ((gsize)sp [-1].data.p % SIZEOF_VOID_P)
3865 memcpy (&sp [-1].data.l, sp [-1].data.p, sizeof (gint64));
3866 else
3867 #endif
3868 sp[-1].data.l = *(gint64*)sp[-1].data.p;
3869 MINT_IN_BREAK;
3870 MINT_IN_CASE(MINT_LDIND_I) {
3871 guint16 offset = * (guint16 *)(ip + 1);
3872 sp[-1 - offset].data.p = *(gpointer*)sp[-1 - offset].data.p;
3873 ip += 2;
3874 MINT_IN_BREAK;
3876 MINT_IN_CASE(MINT_LDIND_I8) {
3877 guint16 offset = * (guint16 *)(ip + 1);
3878 #ifdef NO_UNALIGNED_ACCESS
3879 if ((gsize)sp [-1 - offset].data.p % SIZEOF_VOID_P)
3880 memcpy (&sp [-1 - offset].data.l, sp [-1 - offset].data.p, sizeof (gint64));
3881 else
3882 #endif
3883 sp[-1 - offset].data.l = *(gint64*)sp[-1 - offset].data.p;
3884 ip += 2;
3885 MINT_IN_BREAK;
3887 MINT_IN_CASE(MINT_LDIND_R4_CHECK)
3888 NULL_CHECK (sp [-1].data.p);
3889 ++ip;
3890 sp[-1].data.f_r4 = *(gfloat*)sp[-1].data.p;
3891 MINT_IN_BREAK;
3892 MINT_IN_CASE(MINT_LDIND_R8_CHECK)
3893 NULL_CHECK (sp [-1].data.p);
3894 ++ip;
3895 #ifdef NO_UNALIGNED_ACCESS
3896 if ((gsize)sp [-1].data.p % SIZEOF_VOID_P)
3897 memcpy (&sp [-1].data.f, sp [-1].data.p, sizeof (gdouble));
3898 else
3899 #endif
3900 sp[-1].data.f = *(gdouble*)sp[-1].data.p;
3901 MINT_IN_BREAK;
3902 MINT_IN_CASE(MINT_LDIND_REF)
3903 ++ip;
3904 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
3905 MINT_IN_BREAK;
3906 MINT_IN_CASE(MINT_LDIND_REF_CHECK) {
3907 NULL_CHECK (sp [-1].data.p);
3908 ++ip;
3909 sp [-1].data.p = *(gpointer*)sp [-1].data.p;
3910 MINT_IN_BREAK;
3912 MINT_IN_CASE(MINT_STIND_REF)
3913 ++ip;
3914 sp -= 2;
3915 mono_gc_wbarrier_generic_store_internal (sp->data.p, sp [1].data.o);
3916 MINT_IN_BREAK;
3917 MINT_IN_CASE(MINT_STIND_I1)
3918 ++ip;
3919 sp -= 2;
3920 * (gint8 *) sp->data.p = (gint8)sp[1].data.i;
3921 MINT_IN_BREAK;
3922 MINT_IN_CASE(MINT_STIND_I2)
3923 ++ip;
3924 sp -= 2;
3925 * (gint16 *) sp->data.p = (gint16)sp[1].data.i;
3926 MINT_IN_BREAK;
3927 MINT_IN_CASE(MINT_STIND_I4)
3928 ++ip;
3929 sp -= 2;
3930 * (gint32 *) sp->data.p = sp[1].data.i;
3931 MINT_IN_BREAK;
3932 MINT_IN_CASE(MINT_STIND_I)
3933 ++ip;
3934 sp -= 2;
3935 * (mono_i *) sp->data.p = (mono_i)sp[1].data.p;
3936 MINT_IN_BREAK;
3937 MINT_IN_CASE(MINT_STIND_I8)
3938 ++ip;
3939 sp -= 2;
3940 #ifdef NO_UNALIGNED_ACCESS
3941 if ((gsize)sp->data.p % SIZEOF_VOID_P)
3942 memcpy (sp->data.p, &sp [1].data.l, sizeof (gint64));
3943 else
3944 #endif
3945 * (gint64 *) sp->data.p = sp[1].data.l;
3946 MINT_IN_BREAK;
3947 MINT_IN_CASE(MINT_STIND_R4)
3948 ++ip;
3949 sp -= 2;
3950 * (float *) sp->data.p = sp[1].data.f_r4;
3951 MINT_IN_BREAK;
3952 MINT_IN_CASE(MINT_STIND_R8)
3953 ++ip;
3954 sp -= 2;
3955 #ifdef NO_UNALIGNED_ACCESS
3956 if ((gsize)sp->data.p % SIZEOF_VOID_P)
3957 memcpy (sp->data.p, &sp [1].data.f, sizeof (double));
3958 else
3959 #endif
3960 * (double *) sp->data.p = sp[1].data.f;
3961 MINT_IN_BREAK;
3962 MINT_IN_CASE(MINT_MONO_ATOMIC_STORE_I4)
3963 ++ip;
3964 sp -= 2;
3965 mono_atomic_store_i32 ((gint32 *) sp->data.p, sp [1].data.i);
3966 MINT_IN_BREAK;
3967 #define BINOP(datamem, op) \
3968 --sp; \
3969 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.datamem; \
3970 ++ip;
3971 MINT_IN_CASE(MINT_ADD_I4)
3972 BINOP(i, +);
3973 MINT_IN_BREAK;
3974 MINT_IN_CASE(MINT_ADD_I8)
3975 BINOP(l, +);
3976 MINT_IN_BREAK;
3977 MINT_IN_CASE(MINT_ADD_R4)
3978 BINOP(f_r4, +);
3979 MINT_IN_BREAK;
3980 MINT_IN_CASE(MINT_ADD_R8)
3981 BINOP(f, +);
3982 MINT_IN_BREAK;
3983 MINT_IN_CASE(MINT_ADD1_I4)
3984 ++sp [-1].data.i;
3985 ++ip;
3986 MINT_IN_BREAK;
3987 MINT_IN_CASE(MINT_ADD1_I8)
3988 ++sp [-1].data.l;
3989 ++ip;
3990 MINT_IN_BREAK;
3991 MINT_IN_CASE(MINT_SUB_I4)
3992 BINOP(i, -);
3993 MINT_IN_BREAK;
3994 MINT_IN_CASE(MINT_SUB_I8)
3995 BINOP(l, -);
3996 MINT_IN_BREAK;
3997 MINT_IN_CASE(MINT_SUB_R4)
3998 BINOP(f_r4, -);
3999 MINT_IN_BREAK;
4000 MINT_IN_CASE(MINT_SUB_R8)
4001 BINOP(f, -);
4002 MINT_IN_BREAK;
4003 MINT_IN_CASE(MINT_SUB1_I4)
4004 --sp [-1].data.i;
4005 ++ip;
4006 MINT_IN_BREAK;
4007 MINT_IN_CASE(MINT_SUB1_I8)
4008 --sp [-1].data.l;
4009 ++ip;
4010 MINT_IN_BREAK;
4011 MINT_IN_CASE(MINT_MUL_I4)
4012 BINOP(i, *);
4013 MINT_IN_BREAK;
4014 MINT_IN_CASE(MINT_MUL_I8)
4015 BINOP(l, *);
4016 MINT_IN_BREAK;
4017 MINT_IN_CASE(MINT_MUL_R4)
4018 BINOP(f_r4, *);
4019 MINT_IN_BREAK;
4020 MINT_IN_CASE(MINT_MUL_R8)
4021 BINOP(f, *);
4022 MINT_IN_BREAK;
4023 MINT_IN_CASE(MINT_DIV_I4)
4024 if (sp [-1].data.i == 0)
4025 THROW_EX_DIV_ZERO (ip);
4026 if (sp [-1].data.i == (-1) && sp [-2].data.i == G_MININT32)
4027 THROW_EX_OVF (ip);
4028 BINOP(i, /);
4029 MINT_IN_BREAK;
4030 MINT_IN_CASE(MINT_DIV_I8)
4031 if (sp [-1].data.l == 0)
4032 THROW_EX_DIV_ZERO (ip);
4033 if (sp [-1].data.l == (-1) && sp [-2].data.l == G_MININT64)
4034 THROW_EX_OVF (ip);
4035 BINOP(l, /);
4036 MINT_IN_BREAK;
4037 MINT_IN_CASE(MINT_DIV_R4)
4038 BINOP(f_r4, /);
4039 MINT_IN_BREAK;
4040 MINT_IN_CASE(MINT_DIV_R8)
4041 BINOP(f, /);
4042 MINT_IN_BREAK;
4044 #define BINOP_CAST(datamem, op, type) \
4045 --sp; \
4046 sp [-1].data.datamem = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
4047 ++ip;
4048 MINT_IN_CASE(MINT_DIV_UN_I4)
4049 if (sp [-1].data.i == 0)
4050 THROW_EX_DIV_ZERO (ip);
4051 BINOP_CAST(i, /, guint32);
4052 MINT_IN_BREAK;
4053 MINT_IN_CASE(MINT_DIV_UN_I8)
4054 if (sp [-1].data.l == 0)
4055 THROW_EX_DIV_ZERO (ip);
4056 BINOP_CAST(l, /, guint64);
4057 MINT_IN_BREAK;
4058 MINT_IN_CASE(MINT_REM_I4)
4059 if (sp [-1].data.i == 0)
4060 THROW_EX_DIV_ZERO (ip);
4061 if (sp [-1].data.i == (-1) && sp [-2].data.i == G_MININT32)
4062 THROW_EX_OVF (ip);
4063 BINOP(i, %);
4064 MINT_IN_BREAK;
4065 MINT_IN_CASE(MINT_REM_I8)
4066 if (sp [-1].data.l == 0)
4067 THROW_EX_DIV_ZERO (ip);
4068 if (sp [-1].data.l == (-1) && sp [-2].data.l == G_MININT64)
4069 THROW_EX_OVF (ip);
4070 BINOP(l, %);
4071 MINT_IN_BREAK;
4072 MINT_IN_CASE(MINT_REM_R4)
4073 /* FIXME: what do we actually do here? */
4074 --sp;
4075 sp [-1].data.f_r4 = fmodf (sp [-1].data.f_r4, sp [0].data.f_r4);
4076 ++ip;
4077 MINT_IN_BREAK;
4078 MINT_IN_CASE(MINT_REM_R8)
4079 /* FIXME: what do we actually do here? */
4080 --sp;
4081 sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
4082 ++ip;
4083 MINT_IN_BREAK;
4084 MINT_IN_CASE(MINT_REM_UN_I4)
4085 if (sp [-1].data.i == 0)
4086 THROW_EX_DIV_ZERO (ip);
4087 BINOP_CAST(i, %, guint32);
4088 MINT_IN_BREAK;
4089 MINT_IN_CASE(MINT_REM_UN_I8)
4090 if (sp [-1].data.l == 0)
4091 THROW_EX_DIV_ZERO (ip);
4092 BINOP_CAST(l, %, guint64);
4093 MINT_IN_BREAK;
4094 MINT_IN_CASE(MINT_AND_I4)
4095 BINOP(i, &);
4096 MINT_IN_BREAK;
4097 MINT_IN_CASE(MINT_AND_I8)
4098 BINOP(l, &);
4099 MINT_IN_BREAK;
4100 MINT_IN_CASE(MINT_OR_I4)
4101 BINOP(i, |);
4102 MINT_IN_BREAK;
4103 MINT_IN_CASE(MINT_OR_I8)
4104 BINOP(l, |);
4105 MINT_IN_BREAK;
4106 MINT_IN_CASE(MINT_XOR_I4)
4107 BINOP(i, ^);
4108 MINT_IN_BREAK;
4109 MINT_IN_CASE(MINT_XOR_I8)
4110 BINOP(l, ^);
4111 MINT_IN_BREAK;
4113 #define SHIFTOP(datamem, op) \
4114 --sp; \
4115 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.i; \
4116 ++ip;
4118 MINT_IN_CASE(MINT_SHL_I4)
4119 SHIFTOP(i, <<);
4120 MINT_IN_BREAK;
4121 MINT_IN_CASE(MINT_SHL_I8)
4122 SHIFTOP(l, <<);
4123 MINT_IN_BREAK;
4124 MINT_IN_CASE(MINT_SHR_I4)
4125 SHIFTOP(i, >>);
4126 MINT_IN_BREAK;
4127 MINT_IN_CASE(MINT_SHR_I8)
4128 SHIFTOP(l, >>);
4129 MINT_IN_BREAK;
4130 MINT_IN_CASE(MINT_SHR_UN_I4)
4131 --sp;
4132 sp [-1].data.i = (guint32)sp [-1].data.i >> sp [0].data.i;
4133 ++ip;
4134 MINT_IN_BREAK;
4135 MINT_IN_CASE(MINT_SHR_UN_I8)
4136 --sp;
4137 sp [-1].data.l = (guint64)sp [-1].data.l >> sp [0].data.i;
4138 ++ip;
4139 MINT_IN_BREAK;
4140 MINT_IN_CASE(MINT_NEG_I4)
4141 sp [-1].data.i = - sp [-1].data.i;
4142 ++ip;
4143 MINT_IN_BREAK;
4144 MINT_IN_CASE(MINT_NEG_I8)
4145 sp [-1].data.l = - sp [-1].data.l;
4146 ++ip;
4147 MINT_IN_BREAK;
4148 MINT_IN_CASE(MINT_NEG_R4)
4149 sp [-1].data.f_r4 = - sp [-1].data.f_r4;
4150 ++ip;
4151 MINT_IN_BREAK;
4152 MINT_IN_CASE(MINT_NEG_R8)
4153 sp [-1].data.f = - sp [-1].data.f;
4154 ++ip;
4155 MINT_IN_BREAK;
4156 MINT_IN_CASE(MINT_NOT_I4)
4157 sp [-1].data.i = ~ sp [-1].data.i;
4158 ++ip;
4159 MINT_IN_BREAK;
4160 MINT_IN_CASE(MINT_NOT_I8)
4161 sp [-1].data.l = ~ sp [-1].data.l;
4162 ++ip;
4163 MINT_IN_BREAK;
4164 MINT_IN_CASE(MINT_CONV_I1_I4)
4165 sp [-1].data.i = (gint8)sp [-1].data.i;
4166 ++ip;
4167 MINT_IN_BREAK;
4168 MINT_IN_CASE(MINT_CONV_I1_I8)
4169 sp [-1].data.i = (gint8)sp [-1].data.l;
4170 ++ip;
4171 MINT_IN_BREAK;
4172 MINT_IN_CASE(MINT_CONV_I1_R4)
4173 sp [-1].data.i = (gint8) (gint32) sp [-1].data.f_r4;
4174 ++ip;
4175 MINT_IN_BREAK;
4176 MINT_IN_CASE(MINT_CONV_I1_R8)
4177 /* without gint32 cast, C compiler is allowed to use undefined
4178 * behaviour if data.f is bigger than >255. See conv.fpint section
4179 * in C standard:
4180 * > The conversion truncates; that is, the fractional part
4181 * > is discarded. The behavior is undefined if the truncated
4182 * > value cannot be represented in the destination type.
4183 * */
4184 sp [-1].data.i = (gint8) (gint32) sp [-1].data.f;
4185 ++ip;
4186 MINT_IN_BREAK;
4187 MINT_IN_CASE(MINT_CONV_U1_I4)
4188 sp [-1].data.i = (guint8)sp [-1].data.i;
4189 ++ip;
4190 MINT_IN_BREAK;
4191 MINT_IN_CASE(MINT_CONV_U1_I8)
4192 sp [-1].data.i = (guint8)sp [-1].data.l;
4193 ++ip;
4194 MINT_IN_BREAK;
4195 MINT_IN_CASE(MINT_CONV_U1_R4)
4196 sp [-1].data.i = (guint8) (guint32) sp [-1].data.f_r4;
4197 ++ip;
4198 MINT_IN_BREAK;
4199 MINT_IN_CASE(MINT_CONV_U1_R8)
4200 sp [-1].data.i = (guint8) (guint32) sp [-1].data.f;
4201 ++ip;
4202 MINT_IN_BREAK;
4203 MINT_IN_CASE(MINT_CONV_I2_I4)
4204 sp [-1].data.i = (gint16)sp [-1].data.i;
4205 ++ip;
4206 MINT_IN_BREAK;
4207 MINT_IN_CASE(MINT_CONV_I2_I8)
4208 sp [-1].data.i = (gint16)sp [-1].data.l;
4209 ++ip;
4210 MINT_IN_BREAK;
4211 MINT_IN_CASE(MINT_CONV_I2_R4)
4212 sp [-1].data.i = (gint16) (gint32) sp [-1].data.f_r4;
4213 ++ip;
4214 MINT_IN_BREAK;
4215 MINT_IN_CASE(MINT_CONV_I2_R8)
4216 sp [-1].data.i = (gint16) (gint32) sp [-1].data.f;
4217 ++ip;
4218 MINT_IN_BREAK;
4219 MINT_IN_CASE(MINT_CONV_U2_I4)
4220 sp [-1].data.i = (guint16)sp [-1].data.i;
4221 ++ip;
4222 MINT_IN_BREAK;
4223 MINT_IN_CASE(MINT_CONV_U2_I8)
4224 sp [-1].data.i = (guint16)sp [-1].data.l;
4225 ++ip;
4226 MINT_IN_BREAK;
4227 MINT_IN_CASE(MINT_CONV_U2_R4)
4228 sp [-1].data.i = (guint16) (guint32) sp [-1].data.f_r4;
4229 ++ip;
4230 MINT_IN_BREAK;
4231 MINT_IN_CASE(MINT_CONV_U2_R8)
4232 sp [-1].data.i = (guint16) (guint32) sp [-1].data.f;
4233 ++ip;
4234 MINT_IN_BREAK;
4235 MINT_IN_CASE(MINT_CONV_I4_R4)
4236 sp [-1].data.i = (gint32) sp [-1].data.f_r4;
4237 ++ip;
4238 MINT_IN_BREAK;
4239 MINT_IN_CASE(MINT_CONV_I4_R8)
4240 sp [-1].data.i = (gint32)sp [-1].data.f;
4241 ++ip;
4242 MINT_IN_BREAK;
4243 MINT_IN_CASE(MINT_CONV_U4_I8)
4244 MINT_IN_CASE(MINT_CONV_I4_I8)
4245 sp [-1].data.i = (gint32)sp [-1].data.l;
4246 ++ip;
4247 MINT_IN_BREAK;
4248 MINT_IN_CASE(MINT_CONV_I4_I8_SP)
4249 sp [-2].data.i = (gint32)sp [-2].data.l;
4250 ++ip;
4251 MINT_IN_BREAK;
4252 MINT_IN_CASE(MINT_CONV_U4_R4)
4253 #ifdef MONO_ARCH_EMULATE_FCONV_TO_U4
4254 sp [-1].data.i = mono_rconv_u4 (sp [-1].data.f_r4);
4255 #else
4256 sp [-1].data.i = (guint32) sp [-1].data.f_r4;
4257 #endif
4258 ++ip;
4259 MINT_IN_BREAK;
4260 MINT_IN_CASE(MINT_CONV_U4_R8)
4261 #ifdef MONO_ARCH_EMULATE_FCONV_TO_U4
4262 sp [-1].data.i = mono_fconv_u4_2 (sp [-1].data.f);
4263 #else
4264 sp [-1].data.i = (guint32) sp [-1].data.f;
4265 #endif
4266 ++ip;
4267 MINT_IN_BREAK;
4268 MINT_IN_CASE(MINT_CONV_I8_I4)
4269 sp [-1].data.l = sp [-1].data.i;
4270 ++ip;
4271 MINT_IN_BREAK;
4272 MINT_IN_CASE(MINT_CONV_I8_I4_SP)
4273 sp [-2].data.l = sp [-2].data.i;
4274 ++ip;
4275 MINT_IN_BREAK;
4276 MINT_IN_CASE(MINT_CONV_I8_U4)
4277 sp [-1].data.l = (guint32)sp [-1].data.i;
4278 ++ip;
4279 MINT_IN_BREAK;
4280 MINT_IN_CASE(MINT_CONV_I8_R4)
4281 sp [-1].data.l = (gint64) sp [-1].data.f_r4;
4282 ++ip;
4283 MINT_IN_BREAK;
4284 MINT_IN_CASE(MINT_CONV_I8_R8)
4285 sp [-1].data.l = (gint64)sp [-1].data.f;
4286 ++ip;
4287 MINT_IN_BREAK;
4288 MINT_IN_CASE(MINT_CONV_R4_I4)
4289 sp [-1].data.f_r4 = (float)sp [-1].data.i;
4290 ++ip;
4291 MINT_IN_BREAK;
4292 MINT_IN_CASE(MINT_CONV_R4_I8)
4293 sp [-1].data.f_r4 = (float)sp [-1].data.l;
4294 ++ip;
4295 MINT_IN_BREAK;
4296 MINT_IN_CASE(MINT_CONV_R4_R8)
4297 sp [-1].data.f_r4 = (float)sp [-1].data.f;
4298 ++ip;
4299 MINT_IN_BREAK;
4300 MINT_IN_CASE(MINT_CONV_R8_I4)
4301 sp [-1].data.f = (double)sp [-1].data.i;
4302 ++ip;
4303 MINT_IN_BREAK;
4304 MINT_IN_CASE(MINT_CONV_R8_I8)
4305 sp [-1].data.f = (double)sp [-1].data.l;
4306 ++ip;
4307 MINT_IN_BREAK;
4308 MINT_IN_CASE(MINT_CONV_R8_R4)
4309 sp [-1].data.f = (double) sp [-1].data.f_r4;
4310 ++ip;
4311 MINT_IN_BREAK;
4312 MINT_IN_CASE(MINT_CONV_R8_R4_SP)
4313 sp [-2].data.f = (double) sp [-2].data.f_r4;
4314 ++ip;
4315 MINT_IN_BREAK;
4316 MINT_IN_CASE(MINT_CONV_U8_I4)
4317 sp [-1].data.l = sp [-1].data.i & 0xffffffff;
4318 ++ip;
4319 MINT_IN_BREAK;
4320 MINT_IN_CASE(MINT_CONV_U8_R4)
4321 #ifdef MONO_ARCH_EMULATE_FCONV_TO_U8
4322 sp [-1].data.l = mono_rconv_u8 (sp [-1].data.f_r4);
4323 #else
4324 sp [-1].data.l = (guint64) sp [-1].data.f_r4;
4325 #endif
4326 ++ip;
4327 MINT_IN_BREAK;
4328 MINT_IN_CASE(MINT_CONV_U8_R8)
4329 #ifdef MONO_ARCH_EMULATE_FCONV_TO_U8
4330 sp [-1].data.l = mono_fconv_u8_2 (sp [-1].data.f);
4331 #else
4332 sp [-1].data.l = (guint64)sp [-1].data.f;
4333 #endif
4334 ++ip;
4335 MINT_IN_BREAK;
4336 MINT_IN_CASE(MINT_CPOBJ) {
4337 c = (MonoClass*)imethod->data_items[* (guint16 *)(ip + 1)];
4338 g_assert (m_class_is_valuetype (c));
4339 /* if this assertion fails, we need to add a write barrier */
4340 g_assert (!MONO_TYPE_IS_REFERENCE (m_class_get_byval_arg (c)));
4341 stackval_from_data (m_class_get_byval_arg (c), (stackval*)sp [-2].data.p, sp [-1].data.p, FALSE);
4342 ip += 2;
4343 sp -= 2;
4344 MINT_IN_BREAK;
4346 MINT_IN_CASE(MINT_CPOBJ_VT) {
4347 c = (MonoClass*)imethod->data_items[* (guint16 *)(ip + 1)];
4348 mono_value_copy_internal (sp [-2].data.vt, sp [-1].data.vt, c);
4349 ip += 2;
4350 sp -= 2;
4351 MINT_IN_BREAK;
4353 MINT_IN_CASE(MINT_LDOBJ_VT) {
4354 int size = READ32(ip + 1);
4355 ip += 3;
4356 memcpy (vt_sp, sp [-1].data.p, size);
4357 sp [-1].data.p = vt_sp;
4358 vt_sp += ALIGN_TO (size, MINT_VT_ALIGNMENT);
4359 MINT_IN_BREAK;
4361 MINT_IN_CASE(MINT_LDSTR)
4362 sp->data.p = imethod->data_items [* (guint16 *)(ip + 1)];
4363 ++sp;
4364 ip += 2;
4365 MINT_IN_BREAK;
4366 MINT_IN_CASE(MINT_LDSTR_TOKEN) {
4367 MonoString *s = NULL;
4368 guint32 strtoken = (guint32)(gsize)imethod->data_items [* (guint16 *)(ip + 1)];
4370 MonoMethod *method = imethod->method;
4371 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
4372 s = (MonoString*)mono_method_get_wrapper_data (method, strtoken);
4373 } else if (method->wrapper_type != MONO_WRAPPER_NONE) {
4374 s = mono_string_new_wrapper_internal ((const char*)mono_method_get_wrapper_data (method, strtoken));
4375 } else {
4376 g_assert_not_reached ();
4378 sp->data.p = s;
4379 ++sp;
4380 ip += 2;
4381 MINT_IN_BREAK;
4383 MINT_IN_CASE(MINT_NEWOBJ_ARRAY) {
4384 MonoClass *newobj_class;
4385 guint32 token = * (guint16 *)(ip + 1);
4386 guint16 param_count = * (guint16 *)(ip + 2);
4388 newobj_class = (MonoClass*) imethod->data_items [token];
4390 sp -= param_count;
4391 sp->data.o = ves_array_create (imethod->domain, newobj_class, param_count, sp, error);
4392 if (!is_ok (error))
4393 THROW_EX (mono_error_convert_to_exception (error), ip);
4395 ++sp;
4396 ip += 3;
4397 MINT_IN_BREAK;
4399 MINT_IN_CASE(MINT_NEWOBJ_FAST) {
4400 guint16 param_count;
4401 guint16 imethod_index = *(guint16*) (ip + 1);
4403 const gboolean is_inlined = imethod_index == 0xffff;
4405 param_count = *(guint16*)(ip + 2);
4407 if (param_count) {
4408 sp -= param_count;
4409 memmove (sp + 1 + is_inlined, sp, param_count * sizeof (stackval));
4412 MonoVTable *vtable = (MonoVTable*) imethod->data_items [*(guint16*)(ip + 3)];
4413 INIT_VTABLE (vtable);
4415 frame_objref (frame) = mono_gc_alloc_obj (vtable, m_class_get_instance_size (vtable->klass));
4416 if (G_UNLIKELY (!frame_objref (frame))) {
4417 mono_error_set_out_of_memory (error, "Could not allocate %i bytes", m_class_get_instance_size (vtable->klass));
4418 THROW_EX (mono_error_convert_to_exception (error), ip);
4421 sp [0].data.o = frame_objref (frame);
4422 if (is_inlined) {
4423 sp [1].data.o = frame_objref (frame);
4424 sp += param_count + 2;
4425 } else {
4426 InterpMethod *ctor_method = (InterpMethod*) imethod->data_items [imethod_index];
4427 frame->ip = ip;
4429 child_frame.imethod = ctor_method;
4430 child_frame.stack_args = sp;
4432 interp_exec_method (&child_frame, context, error);
4433 CHECK_RESUME_STATE (context);
4434 sp [0].data.o = frame_objref (frame);
4435 sp++;
4437 ip += 4;
4439 MINT_IN_BREAK;
4441 MINT_IN_CASE(MINT_NEWOBJ_VT_FAST)
4442 MINT_IN_CASE(MINT_NEWOBJ_VTST_FAST) {
4443 guint16 param_count;
4444 stackval valuetype_this;
4446 frame->ip = ip;
4448 child_frame.imethod = (InterpMethod*) imethod->data_items [*(guint16*)(ip + 1)];
4449 param_count = *(guint16*)(ip + 2);
4451 if (param_count) {
4452 sp -= param_count;
4453 memmove (sp + 1, sp, param_count * sizeof (stackval));
4455 child_frame.stack_args = sp;
4457 gboolean vtst = *ip == MINT_NEWOBJ_VTST_FAST;
4458 if (vtst) {
4459 memset (vt_sp, 0, *(guint16*)(ip + 3));
4460 sp->data.p = vt_sp;
4461 valuetype_this.data.p = vt_sp;
4462 ip += 4;
4463 } else {
4464 memset (&valuetype_this, 0, sizeof (stackval));
4465 sp->data.p = &valuetype_this;
4466 ip += 3;
4469 interp_exec_method (&child_frame, context, error);
4471 CHECK_RESUME_STATE (context);
4473 *sp = valuetype_this;
4474 ++sp;
4475 MINT_IN_BREAK;
4477 MINT_IN_CASE(MINT_NEWOBJ) {
4478 MonoClass *newobj_class;
4479 MonoMethodSignature *csig;
4480 stackval valuetype_this;
4481 guint32 token;
4482 stackval retval;
4484 frame->ip = ip;
4486 token = * (guint16 *)(ip + 1);
4487 ip += 2;
4489 child_frame.ip = NULL;
4490 child_frame.ex = NULL;
4492 child_frame.imethod = (InterpMethod*)imethod->data_items [token];
4493 csig = mono_method_signature_internal (child_frame.imethod->method);
4494 newobj_class = child_frame.imethod->method->klass;
4495 /*if (profiling_classes) {
4496 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
4497 count++;
4498 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
4501 g_assert (csig->hasthis);
4502 if (csig->param_count) {
4503 sp -= csig->param_count;
4504 memmove (sp + 1, sp, csig->param_count * sizeof (stackval));
4506 child_frame.stack_args = sp;
4509 * First arg is the object.
4511 if (m_class_is_valuetype (newobj_class)) {
4512 MonoType *t = m_class_get_byval_arg (newobj_class);
4513 memset (&valuetype_this, 0, sizeof (stackval));
4514 if (!m_class_is_enumtype (newobj_class) && (t->type == MONO_TYPE_VALUETYPE || (t->type == MONO_TYPE_GENERICINST && mono_type_generic_inst_is_valuetype (t)))) {
4515 sp->data.p = vt_sp;
4516 valuetype_this.data.p = vt_sp;
4517 } else {
4518 sp->data.p = &valuetype_this;
4520 } else {
4521 if (newobj_class != mono_defaults.string_class) {
4522 MonoVTable *vtable = mono_class_vtable_checked (imethod->domain, newobj_class, error);
4523 if (!is_ok (error) || !mono_runtime_class_init_full (vtable, error))
4524 THROW_EX (mono_error_convert_to_exception (error), ip);
4525 frame_objref (frame) = mono_object_new_checked (imethod->domain, newobj_class, error);
4526 mono_error_cleanup (error); /* FIXME: don't swallow the error */
4527 EXCEPTION_CHECKPOINT;
4528 sp->data.o = frame_objref (frame);
4529 #ifndef DISABLE_REMOTING
4530 if (mono_object_is_transparent_proxy (frame_objref (frame))) {
4531 MonoMethod *remoting_invoke_method = mono_marshal_get_remoting_invoke_with_check (child_frame.imethod->method, error);
4532 mono_error_assert_ok (error);
4533 child_frame.imethod = mono_interp_get_imethod (imethod->domain, remoting_invoke_method, error);
4534 mono_error_assert_ok (error);
4536 #endif
4537 } else {
4538 sp->data.p = NULL;
4539 child_frame.retval = &retval;
4543 interp_exec_method (&child_frame, context, error);
4545 CHECK_RESUME_STATE (context);
4548 * a constructor returns void, but we need to return the object we created
4550 if (m_class_is_valuetype (newobj_class) && !m_class_is_enumtype (newobj_class)) {
4551 *sp = valuetype_this;
4552 } else if (newobj_class == mono_defaults.string_class) {
4553 *sp = retval;
4554 } else {
4555 sp->data.o = frame_objref (frame);
4557 ++sp;
4558 MINT_IN_BREAK;
4560 MINT_IN_CASE(MINT_NEWOBJ_MAGIC) {
4561 frame->ip = ip;
4562 ip += 2;
4564 MINT_IN_BREAK;
4566 MINT_IN_CASE(MINT_INTRINS_BYREFERENCE_CTOR) {
4567 MonoMethodSignature *csig;
4568 guint32 token;
4570 frame->ip = ip;
4571 token = * (guint16 *)(ip + 1);
4572 ip += 2;
4574 InterpMethod *cmethod = (InterpMethod*)imethod->data_items [token];
4575 csig = mono_method_signature_internal (cmethod->method);
4577 g_assert (csig->hasthis);
4578 sp -= csig->param_count;
4580 gpointer arg0 = sp [0].data.p;
4582 gpointer *byreference_this = (gpointer*)vt_sp;
4583 *byreference_this = arg0;
4585 /* Followed by a VTRESULT opcode which will push the result on the stack */
4586 ++sp;
4587 MINT_IN_BREAK;
4589 MINT_IN_CASE(MINT_INTRINS_BYREFERENCE_GET_VALUE) {
4590 gpointer *byreference_this = (gpointer*)sp [-1].data.p;
4591 sp [-1].data.p = *byreference_this;
4592 ++ip;
4593 MINT_IN_BREAK;
4595 MINT_IN_CASE(MINT_INTRINS_UNSAFE_ADD_BYTE_OFFSET) {
4596 sp -= 2;
4597 sp [0].data.p = (guint8*)sp [0].data.p + sp [1].data.nati;
4598 sp ++;
4599 ++ip;
4600 MINT_IN_BREAK;
4602 MINT_IN_CASE(MINT_INTRINS_UNSAFE_BYTE_OFFSET) {
4603 sp -= 2;
4604 sp [0].data.nati = (guint8*)sp [1].data.p - (guint8*)sp [0].data.p;
4605 sp ++;
4606 ++ip;
4607 MINT_IN_BREAK;
4609 MINT_IN_CASE(MINT_INTRINS_RUNTIMEHELPERS_OBJECT_HAS_COMPONENT_SIZE) {
4610 sp -= 1;
4611 MonoObject *obj = sp [0].data.o;
4612 sp [0].data.i = (obj->vtable->flags & MONO_VT_FLAG_ARRAY_OR_STRING) != 0;
4613 sp ++;
4614 ++ip;
4615 MINT_IN_BREAK;
4617 MINT_IN_CASE(MINT_CASTCLASS_INTERFACE)
4618 MINT_IN_CASE(MINT_ISINST_INTERFACE) {
4619 gboolean isinst_instr = *ip == MINT_ISINST_INTERFACE;
4620 c = (MonoClass*)imethod->data_items [*(guint16 *)(ip + 1)];
4621 if ((o = sp [-1].data.o)) {
4622 gboolean isinst;
4623 if (MONO_VTABLE_IMPLEMENTS_INTERFACE (o->vtable, m_class_get_interface_id (c))) {
4624 isinst = TRUE;
4625 } else if (m_class_is_array_special_interface (c) || mono_object_is_transparent_proxy (o)) {
4626 /* slow path */
4627 isinst = mono_object_isinst_checked (o, c, error) != NULL;
4628 mono_error_cleanup (error); /* FIXME: don't swallow the error */
4629 } else {
4630 isinst = FALSE;
4633 if (!isinst) {
4634 if (isinst_instr)
4635 sp [-1].data.p = NULL;
4636 else
4637 THROW_EX (mono_get_exception_invalid_cast (), ip);
4640 ip += 2;
4641 MINT_IN_BREAK;
4643 MINT_IN_CASE(MINT_CASTCLASS_COMMON)
4644 MINT_IN_CASE(MINT_ISINST_COMMON) {
4645 gboolean isinst_instr = *ip == MINT_ISINST_COMMON;
4646 c = (MonoClass*)imethod->data_items [*(guint16 *)(ip + 1)];
4647 if ((o = sp [-1].data.o)) {
4648 gboolean isinst = mono_class_has_parent_fast (o->vtable->klass, c);
4650 if (!isinst) {
4651 if (isinst_instr)
4652 sp [-1].data.p = NULL;
4653 else
4654 THROW_EX (mono_get_exception_invalid_cast (), ip);
4657 ip += 2;
4658 MINT_IN_BREAK;
4660 MINT_IN_CASE(MINT_CASTCLASS)
4661 MINT_IN_CASE(MINT_ISINST) {
4662 gboolean isinst_instr = *ip == MINT_ISINST;
4663 c = (MonoClass*)imethod->data_items [*(guint16 *)(ip + 1)];
4664 if ((o = sp [-1].data.o)) {
4665 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, error);
4666 mono_error_cleanup (error); /* FIXME: don't swallow the error */
4667 if (!isinst_obj) {
4668 if (isinst_instr)
4669 sp [-1].data.p = NULL;
4670 else
4671 THROW_EX (mono_get_exception_invalid_cast (), ip);
4674 ip += 2;
4675 MINT_IN_BREAK;
4677 MINT_IN_CASE(MINT_CONV_R_UN_I4)
4678 sp [-1].data.f = (double)(guint32)sp [-1].data.i;
4679 ++ip;
4680 MINT_IN_BREAK;
4681 MINT_IN_CASE(MINT_CONV_R_UN_I8)
4682 sp [-1].data.f = (double)(guint64)sp [-1].data.l;
4683 ++ip;
4684 MINT_IN_BREAK;
4685 MINT_IN_CASE(MINT_UNBOX)
4686 c = (MonoClass*)imethod->data_items[*(guint16 *)(ip + 1)];
4688 o = sp [-1].data.o;
4689 NULL_CHECK (o);
4691 if (!(m_class_get_rank (o->vtable->klass) == 0 && m_class_get_element_class (o->vtable->klass) == m_class_get_element_class (c)))
4692 THROW_EX (mono_get_exception_invalid_cast (), ip);
4694 sp [-1].data.p = mono_object_unbox_internal (o);
4695 ip += 2;
4696 MINT_IN_BREAK;
4697 MINT_IN_CASE(MINT_THROW)
4698 --sp;
4699 if (!sp->data.p)
4700 sp->data.p = mono_get_exception_null_reference ();
4702 THROW_EX ((MonoException *)sp->data.p, ip);
4703 MINT_IN_BREAK;
4704 MINT_IN_CASE(MINT_CHECKPOINT)
4705 /* Do synchronous checking of abort requests */
4706 EXCEPTION_CHECKPOINT;
4707 ++ip;
4708 MINT_IN_BREAK;
4709 MINT_IN_CASE(MINT_SAFEPOINT)
4710 /* Do synchronous checking of abort requests */
4711 EXCEPTION_CHECKPOINT;
4712 /* Poll safepoint */
4713 mono_threads_safepoint ();
4714 ++ip;
4715 MINT_IN_BREAK;
4716 MINT_IN_CASE(MINT_LDFLDA_UNSAFE)
4717 o = sp [-1].data.o;
4718 sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
4719 ip += 2;
4720 MINT_IN_BREAK;
4721 MINT_IN_CASE(MINT_LDFLDA)
4722 o = sp [-1].data.o;
4723 NULL_CHECK (o);
4724 sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
4725 ip += 2;
4726 MINT_IN_BREAK;
4727 MINT_IN_CASE(MINT_CKNULL_N) {
4728 /* Same as CKNULL, but further down the stack */
4729 int n = *(guint16*)(ip + 1);
4730 o = sp [-n].data.o;
4731 NULL_CHECK (o);
4732 ip += 2;
4733 MINT_IN_BREAK;
4736 #define LDFLD_UNALIGNED(datamem, fieldtype, unaligned) \
4737 o = sp [-1].data.o; \
4738 NULL_CHECK (o); \
4739 if (unaligned) \
4740 memcpy (&sp[-1].data.datamem, (char *)o + * (guint16 *)(ip + 1), sizeof (fieldtype)); \
4741 else \
4742 sp[-1].data.datamem = * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) ; \
4743 ip += 2;
4745 #define LDFLD(datamem, fieldtype) LDFLD_UNALIGNED(datamem, fieldtype, FALSE)
4747 MINT_IN_CASE(MINT_LDFLD_I1) LDFLD(i, gint8); MINT_IN_BREAK;
4748 MINT_IN_CASE(MINT_LDFLD_U1) LDFLD(i, guint8); MINT_IN_BREAK;
4749 MINT_IN_CASE(MINT_LDFLD_I2) LDFLD(i, gint16); MINT_IN_BREAK;
4750 MINT_IN_CASE(MINT_LDFLD_U2) LDFLD(i, guint16); MINT_IN_BREAK;
4751 MINT_IN_CASE(MINT_LDFLD_I4) LDFLD(i, gint32); MINT_IN_BREAK;
4752 MINT_IN_CASE(MINT_LDFLD_I8) LDFLD(l, gint64); MINT_IN_BREAK;
4753 MINT_IN_CASE(MINT_LDFLD_R4) LDFLD(f_r4, float); MINT_IN_BREAK;
4754 MINT_IN_CASE(MINT_LDFLD_R8) LDFLD(f, double); MINT_IN_BREAK;
4755 MINT_IN_CASE(MINT_LDFLD_O) LDFLD(p, gpointer); MINT_IN_BREAK;
4756 MINT_IN_CASE(MINT_LDFLD_P) LDFLD(p, gpointer); MINT_IN_BREAK;
4757 MINT_IN_CASE(MINT_LDFLD_I8_UNALIGNED) LDFLD_UNALIGNED(l, gint64, TRUE); MINT_IN_BREAK;
4758 MINT_IN_CASE(MINT_LDFLD_R8_UNALIGNED) LDFLD_UNALIGNED(f, double, TRUE); MINT_IN_BREAK;
4760 MINT_IN_CASE(MINT_LDFLD_VT) {
4761 o = sp [-1].data.o;
4762 NULL_CHECK (o);
4764 int size = READ32(ip + 2);
4765 sp [-1].data.p = vt_sp;
4766 memcpy (sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), size);
4767 vt_sp += ALIGN_TO (size, MINT_VT_ALIGNMENT);
4768 ip += 4;
4769 MINT_IN_BREAK;
4772 MINT_IN_CASE(MINT_LDRMFLD) {
4773 MonoClassField *field;
4774 char *addr;
4776 o = sp [-1].data.o;
4777 NULL_CHECK (o);
4778 field = (MonoClassField*)imethod->data_items[* (guint16 *)(ip + 1)];
4779 ip += 2;
4780 #ifndef DISABLE_REMOTING
4781 gpointer tmp;
4782 if (mono_object_is_transparent_proxy (o)) {
4783 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
4785 addr = (char*)mono_load_remote_field_checked (o, klass, field, &tmp, error);
4786 mono_error_cleanup (error); /* FIXME: don't swallow the error */
4787 } else
4788 #endif
4789 addr = (char*)o + field->offset;
4791 stackval_from_data (field->type, &sp [-1], addr, FALSE);
4792 MINT_IN_BREAK;
4795 MINT_IN_CASE(MINT_LDRMFLD_VT) {
4796 MonoClassField *field;
4797 char *addr;
4799 o = sp [-1].data.o;
4800 NULL_CHECK (o);
4802 field = (MonoClassField*)imethod->data_items[* (guint16 *)(ip + 1)];
4803 MonoClass *klass = mono_class_from_mono_type_internal (field->type);
4804 i32 = mono_class_value_size (klass, NULL);
4806 ip += 2;
4807 #ifndef DISABLE_REMOTING
4808 gpointer tmp;
4809 if (mono_object_is_transparent_proxy (o)) {
4810 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
4811 addr = (char*)mono_load_remote_field_checked (o, klass, field, &tmp, error);
4812 mono_error_cleanup (error); /* FIXME: don't swallow the error */
4813 } else
4814 #endif
4815 addr = (char*)o + field->offset;
4817 sp [-1].data.p = vt_sp;
4818 vt_sp += ALIGN_TO (i32, MINT_VT_ALIGNMENT);
4819 memcpy(sp [-1].data.p, addr, i32);
4820 MINT_IN_BREAK;
4823 #define STFLD_UNALIGNED(datamem, fieldtype, unaligned) \
4824 o = sp [-2].data.o; \
4825 NULL_CHECK (o); \
4826 sp -= 2; \
4827 if (unaligned) \
4828 memcpy ((char *)o + * (guint16 *)(ip + 1), &sp[1].data.datamem, sizeof (fieldtype)); \
4829 else \
4830 * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) = sp[1].data.datamem; \
4831 ip += 2;
4833 #define STFLD(datamem, fieldtype) STFLD_UNALIGNED(datamem, fieldtype, FALSE)
4835 MINT_IN_CASE(MINT_STFLD_I1) STFLD(i, gint8); MINT_IN_BREAK;
4836 MINT_IN_CASE(MINT_STFLD_U1) STFLD(i, guint8); MINT_IN_BREAK;
4837 MINT_IN_CASE(MINT_STFLD_I2) STFLD(i, gint16); MINT_IN_BREAK;
4838 MINT_IN_CASE(MINT_STFLD_U2) STFLD(i, guint16); MINT_IN_BREAK;
4839 MINT_IN_CASE(MINT_STFLD_I4) STFLD(i, gint32); MINT_IN_BREAK;
4840 MINT_IN_CASE(MINT_STFLD_I8) STFLD(l, gint64); MINT_IN_BREAK;
4841 MINT_IN_CASE(MINT_STFLD_R4) STFLD(f_r4, float); MINT_IN_BREAK;
4842 MINT_IN_CASE(MINT_STFLD_R8) STFLD(f, double); MINT_IN_BREAK;
4843 MINT_IN_CASE(MINT_STFLD_P) STFLD(p, gpointer); MINT_IN_BREAK;
4844 MINT_IN_CASE(MINT_STFLD_O)
4845 o = sp [-2].data.o;
4846 NULL_CHECK (o);
4847 sp -= 2;
4848 mono_gc_wbarrier_set_field_internal (o, (char *) o + * (guint16 *)(ip + 1), sp [1].data.o);
4849 ip += 2;
4850 MINT_IN_BREAK;
4851 MINT_IN_CASE(MINT_STFLD_I8_UNALIGNED) STFLD_UNALIGNED(l, gint64, TRUE); MINT_IN_BREAK;
4852 MINT_IN_CASE(MINT_STFLD_R8_UNALIGNED) STFLD_UNALIGNED(f, double, TRUE); MINT_IN_BREAK;
4854 MINT_IN_CASE(MINT_STFLD_VT) {
4855 o = sp [-2].data.o;
4856 NULL_CHECK (o);
4857 sp -= 2;
4859 MonoClass *klass = (MonoClass*)imethod->data_items[* (guint16 *)(ip + 2)];
4860 i32 = mono_class_value_size (klass, NULL);
4862 guint16 offset = * (guint16 *)(ip + 1);
4863 mono_value_copy_internal ((char *) o + offset, sp [1].data.p, klass);
4865 vt_sp -= ALIGN_TO (i32, MINT_VT_ALIGNMENT);
4866 ip += 3;
4867 MINT_IN_BREAK;
4869 MINT_IN_CASE(MINT_STRMFLD) {
4870 MonoClassField *field;
4872 o = sp [-2].data.o;
4873 NULL_CHECK (o);
4875 field = (MonoClassField*)imethod->data_items[* (guint16 *)(ip + 1)];
4876 ip += 2;
4878 #ifndef DISABLE_REMOTING
4879 if (mono_object_is_transparent_proxy (o)) {
4880 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
4881 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, error);
4882 mono_error_cleanup (error); /* FIXME: don't swallow the error */
4883 } else
4884 #endif
4885 stackval_to_data (field->type, &sp [-1], (char*)o + field->offset, FALSE);
4887 sp -= 2;
4888 MINT_IN_BREAK;
4890 MINT_IN_CASE(MINT_STRMFLD_VT) {
4891 MonoClassField *field;
4893 o = sp [-2].data.o;
4894 NULL_CHECK (o);
4895 field = (MonoClassField*)imethod->data_items[* (guint16 *)(ip + 1)];
4896 MonoClass *klass = mono_class_from_mono_type_internal (field->type);
4897 i32 = mono_class_value_size (klass, NULL);
4898 ip += 2;
4900 #ifndef DISABLE_REMOTING
4901 if (mono_object_is_transparent_proxy (o)) {
4902 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
4903 mono_store_remote_field_checked (o, klass, field, sp [-1].data.p, error);
4904 mono_error_cleanup (error); /* FIXME: don't swallow the error */
4905 } else
4906 #endif
4907 mono_value_copy_internal ((char *) o + field->offset, sp [-1].data.p, klass);
4909 sp -= 2;
4910 vt_sp -= ALIGN_TO (i32, MINT_VT_ALIGNMENT);
4911 MINT_IN_BREAK;
4913 MINT_IN_CASE(MINT_LDSFLDA) {
4914 MonoVTable *vtable = (MonoVTable*) imethod->data_items [*(guint16*)(ip + 1)];
4915 INIT_VTABLE (vtable);
4916 sp->data.p = imethod->data_items [*(guint16*)(ip + 2)];
4917 ip += 3;
4918 ++sp;
4919 MINT_IN_BREAK;
4922 MINT_IN_CASE(MINT_LDSSFLDA) {
4923 guint32 offset = READ32(ip + 1);
4924 sp->data.p = mono_get_special_static_data (offset);
4925 ip += 3;
4926 ++sp;
4927 MINT_IN_BREAK;
4930 /* We init class here to preserve cctor order */
4931 #define LDSFLD(datamem, fieldtype) { \
4932 MonoVTable *vtable = (MonoVTable*) imethod->data_items [*(guint16*)(ip + 1)]; \
4933 INIT_VTABLE (vtable); \
4934 sp[0].data.datamem = * (fieldtype *)(imethod->data_items [* (guint16 *)(ip + 2)]) ; \
4935 ip += 3; \
4936 sp++; \
4939 MINT_IN_CASE(MINT_LDSFLD_I1) LDSFLD(i, gint8); MINT_IN_BREAK;
4940 MINT_IN_CASE(MINT_LDSFLD_U1) LDSFLD(i, guint8); MINT_IN_BREAK;
4941 MINT_IN_CASE(MINT_LDSFLD_I2) LDSFLD(i, gint16); MINT_IN_BREAK;
4942 MINT_IN_CASE(MINT_LDSFLD_U2) LDSFLD(i, guint16); MINT_IN_BREAK;
4943 MINT_IN_CASE(MINT_LDSFLD_I4) LDSFLD(i, gint32); MINT_IN_BREAK;
4944 MINT_IN_CASE(MINT_LDSFLD_I8) LDSFLD(l, gint64); MINT_IN_BREAK;
4945 MINT_IN_CASE(MINT_LDSFLD_R4) LDSFLD(f_r4, float); MINT_IN_BREAK;
4946 MINT_IN_CASE(MINT_LDSFLD_R8) LDSFLD(f, double); MINT_IN_BREAK;
4947 MINT_IN_CASE(MINT_LDSFLD_O) LDSFLD(p, gpointer); MINT_IN_BREAK;
4948 MINT_IN_CASE(MINT_LDSFLD_P) LDSFLD(p, gpointer); MINT_IN_BREAK;
4950 MINT_IN_CASE(MINT_LDSFLD_VT) {
4951 MonoVTable *vtable = (MonoVTable*) imethod->data_items [*(guint16*)(ip + 1)];
4952 gpointer addr = imethod->data_items [*(guint16*)(ip + 2)];
4953 i32 = READ32(ip + 3);
4954 INIT_VTABLE (vtable);
4955 sp->data.p = vt_sp;
4957 memcpy (vt_sp, addr, i32);
4958 vt_sp += ALIGN_TO (i32, MINT_VT_ALIGNMENT);
4959 ip += 5;
4960 ++sp;
4961 MINT_IN_BREAK;
4964 #define LDTSFLD(datamem, fieldtype) { \
4965 guint32 offset = READ32(ip + 1); \
4966 MonoInternalThread *thread = mono_thread_internal_current (); \
4967 gpointer addr = ((char*)thread->static_data [offset & 0x3f]) + (offset >> 6); \
4968 sp[0].data.datamem = *(fieldtype*)addr; \
4969 ip += 3; \
4970 ++sp; \
4972 MINT_IN_CASE(MINT_LDTSFLD_I1) LDTSFLD(i, gint8); MINT_IN_BREAK;
4973 MINT_IN_CASE(MINT_LDTSFLD_U1) LDTSFLD(i, guint8); MINT_IN_BREAK;
4974 MINT_IN_CASE(MINT_LDTSFLD_I2) LDTSFLD(i, gint16); MINT_IN_BREAK;
4975 MINT_IN_CASE(MINT_LDTSFLD_U2) LDTSFLD(i, guint16); MINT_IN_BREAK;
4976 MINT_IN_CASE(MINT_LDTSFLD_I4) LDTSFLD(i, gint32); MINT_IN_BREAK;
4977 MINT_IN_CASE(MINT_LDTSFLD_I8) LDTSFLD(l, gint64); MINT_IN_BREAK;
4978 MINT_IN_CASE(MINT_LDTSFLD_R4) LDTSFLD(f_r4, float); MINT_IN_BREAK;
4979 MINT_IN_CASE(MINT_LDTSFLD_R8) LDTSFLD(f, double); MINT_IN_BREAK;
4980 MINT_IN_CASE(MINT_LDTSFLD_O) LDTSFLD(p, gpointer); MINT_IN_BREAK;
4981 MINT_IN_CASE(MINT_LDTSFLD_P) LDTSFLD(p, gpointer); MINT_IN_BREAK;
4983 MINT_IN_CASE(MINT_LDSSFLD) {
4984 MonoClassField *field = (MonoClassField*)imethod->data_items [* (guint16 *)(ip + 1)];
4985 guint32 offset = READ32(ip + 2);
4986 gpointer addr = mono_get_special_static_data (offset);
4987 stackval_from_data (field->type, sp, addr, FALSE);
4988 ip += 4;
4989 ++sp;
4990 MINT_IN_BREAK;
4992 MINT_IN_CASE(MINT_LDSSFLD_VT) {
4993 guint32 offset = READ32(ip + 1);
4994 gpointer addr = mono_get_special_static_data (offset);
4996 int size = READ32 (ip + 3);
4997 memcpy (vt_sp, addr, size);
4998 sp->data.p = vt_sp;
4999 vt_sp += ALIGN_TO (size, MINT_VT_ALIGNMENT);
5000 ip += 5;
5001 ++sp;
5002 MINT_IN_BREAK;
5004 #define STSFLD(datamem, fieldtype) { \
5005 MonoVTable *vtable = (MonoVTable*) imethod->data_items [*(guint16*)(ip + 1)]; \
5006 INIT_VTABLE (vtable); \
5007 sp --; \
5008 * (fieldtype *)(imethod->data_items [* (guint16 *)(ip + 2)]) = sp[0].data.datamem; \
5009 ip += 3; \
5012 MINT_IN_CASE(MINT_STSFLD_I1) STSFLD(i, gint8); MINT_IN_BREAK;
5013 MINT_IN_CASE(MINT_STSFLD_U1) STSFLD(i, guint8); MINT_IN_BREAK;
5014 MINT_IN_CASE(MINT_STSFLD_I2) STSFLD(i, gint16); MINT_IN_BREAK;
5015 MINT_IN_CASE(MINT_STSFLD_U2) STSFLD(i, guint16); MINT_IN_BREAK;
5016 MINT_IN_CASE(MINT_STSFLD_I4) STSFLD(i, gint32); MINT_IN_BREAK;
5017 MINT_IN_CASE(MINT_STSFLD_I8) STSFLD(l, gint64); MINT_IN_BREAK;
5018 MINT_IN_CASE(MINT_STSFLD_R4) STSFLD(f_r4, float); MINT_IN_BREAK;
5019 MINT_IN_CASE(MINT_STSFLD_R8) STSFLD(f, double); MINT_IN_BREAK;
5020 MINT_IN_CASE(MINT_STSFLD_P) STSFLD(p, gpointer); MINT_IN_BREAK;
5021 MINT_IN_CASE(MINT_STSFLD_O) STSFLD(p, gpointer); MINT_IN_BREAK;
5023 MINT_IN_CASE(MINT_STSFLD_VT) {
5024 MonoVTable *vtable = (MonoVTable*) imethod->data_items [*(guint16*)(ip + 1)];
5025 gpointer addr = imethod->data_items [*(guint16*)(ip + 2)];
5026 i32 = READ32(ip + 3);
5027 INIT_VTABLE (vtable);
5029 memcpy (addr, sp [-1].data.vt, i32);
5030 vt_sp -= ALIGN_TO (i32, MINT_VT_ALIGNMENT);
5031 ip += 4;
5032 --sp;
5033 MINT_IN_BREAK;
5036 #define STTSFLD(datamem, fieldtype) { \
5037 guint32 offset = READ32(ip + 1); \
5038 MonoInternalThread *thread = mono_thread_internal_current (); \
5039 gpointer addr = ((char*)thread->static_data [offset & 0x3f]) + (offset >> 6); \
5040 sp--; \
5041 *(fieldtype*)addr = sp[0].data.datamem; \
5042 ip += 3; \
5045 MINT_IN_CASE(MINT_STTSFLD_I1) STTSFLD(i, gint8); MINT_IN_BREAK;
5046 MINT_IN_CASE(MINT_STTSFLD_U1) STTSFLD(i, guint8); MINT_IN_BREAK;
5047 MINT_IN_CASE(MINT_STTSFLD_I2) STTSFLD(i, gint16); MINT_IN_BREAK;
5048 MINT_IN_CASE(MINT_STTSFLD_U2) STTSFLD(i, guint16); MINT_IN_BREAK;
5049 MINT_IN_CASE(MINT_STTSFLD_I4) STTSFLD(i, gint32); MINT_IN_BREAK;
5050 MINT_IN_CASE(MINT_STTSFLD_I8) STTSFLD(l, gint64); MINT_IN_BREAK;
5051 MINT_IN_CASE(MINT_STTSFLD_R4) STTSFLD(f_r4, float); MINT_IN_BREAK;
5052 MINT_IN_CASE(MINT_STTSFLD_R8) STTSFLD(f, double); MINT_IN_BREAK;
5053 MINT_IN_CASE(MINT_STTSFLD_P) STTSFLD(p, gpointer); MINT_IN_BREAK;
5054 MINT_IN_CASE(MINT_STTSFLD_O) STTSFLD(p, gpointer); MINT_IN_BREAK;
5056 MINT_IN_CASE(MINT_STSSFLD) {
5057 MonoClassField *field = (MonoClassField*)imethod->data_items [* (guint16 *)(ip + 1)];
5058 guint32 offset = READ32(ip + 2);
5059 gpointer addr = mono_get_special_static_data (offset);
5060 --sp;
5061 stackval_to_data (field->type, sp, addr, FALSE);
5062 ip += 4;
5063 MINT_IN_BREAK;
5065 MINT_IN_CASE(MINT_STSSFLD_VT) {
5066 guint32 offset = READ32(ip + 1);
5067 gpointer addr = mono_get_special_static_data (offset);
5068 --sp;
5069 int size = READ32 (ip + 3);
5070 memcpy (addr, sp->data.vt, size);
5071 vt_sp -= ALIGN_TO (size, MINT_VT_ALIGNMENT);
5072 ip += 5;
5073 MINT_IN_BREAK;
5076 MINT_IN_CASE(MINT_STOBJ_VT) {
5077 int size;
5078 c = (MonoClass*)imethod->data_items[* (guint16 *)(ip + 1)];
5079 ip += 2;
5080 size = mono_class_value_size (c, NULL);
5081 mono_value_copy_internal (sp [-2].data.p, sp [-1].data.p, c);
5082 vt_sp -= ALIGN_TO (size, MINT_VT_ALIGNMENT);
5083 sp -= 2;
5084 MINT_IN_BREAK;
5086 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_R8)
5087 if (sp [-1].data.f < 0 || sp [-1].data.f > G_MAXINT32)
5088 THROW_EX_OVF (ip);
5089 sp [-1].data.i = (gint32)sp [-1].data.f;
5090 ++ip;
5091 MINT_IN_BREAK;
5092 MINT_IN_CASE(MINT_CONV_OVF_U8_I4)
5093 if (sp [-1].data.i < 0)
5094 THROW_EX_OVF (ip);
5095 sp [-1].data.l = sp [-1].data.i;
5096 ++ip;
5097 MINT_IN_BREAK;
5098 MINT_IN_CASE(MINT_CONV_OVF_U8_I8)
5099 if (sp [-1].data.l < 0)
5100 THROW_EX_OVF (ip);
5101 ++ip;
5102 MINT_IN_BREAK;
5103 MINT_IN_CASE(MINT_CONV_OVF_I8_U8)
5104 if ((guint64) sp [-1].data.l > G_MAXINT64)
5105 THROW_EX_OVF (ip);
5106 ++ip;
5107 MINT_IN_BREAK;
5108 MINT_IN_CASE(MINT_CONV_OVF_U8_R4)
5109 if (sp [-1].data.f_r4 < 0 || sp [-1].data.f_r4 > G_MAXUINT64 || isnan (sp [-1].data.f_r4))
5110 THROW_EX_OVF (ip);
5111 sp [-1].data.l = (guint64)sp [-1].data.f_r4;
5112 ++ip;
5113 MINT_IN_BREAK;
5114 MINT_IN_CASE(MINT_CONV_OVF_U8_R8)
5115 if (sp [-1].data.f < 0 || sp [-1].data.f > G_MAXUINT64 || isnan (sp [-1].data.f))
5116 THROW_EX_OVF (ip);
5117 sp [-1].data.l = (guint64)sp [-1].data.f;
5118 ++ip;
5119 MINT_IN_BREAK;
5120 MINT_IN_CASE(MINT_CONV_OVF_I8_UN_R8)
5121 if (sp [-1].data.f < 0 || sp [-1].data.f > G_MAXINT64)
5122 THROW_EX_OVF (ip);
5123 sp [-1].data.l = (gint64)sp [-1].data.f;
5124 ++ip;
5125 MINT_IN_BREAK;
5126 MINT_IN_CASE(MINT_CONV_OVF_I8_UN_R4)
5127 if (sp [-1].data.f_r4 < 0 || sp [-1].data.f_r4 > G_MAXINT64)
5128 THROW_EX_OVF (ip);
5129 sp [-1].data.l = (gint64)sp [-1].data.f_r4;
5130 ++ip;
5131 MINT_IN_BREAK;
5132 MINT_IN_CASE(MINT_CONV_OVF_I8_R4)
5133 if (sp [-1].data.f_r4 < G_MININT64 || sp [-1].data.f_r4 > G_MAXINT64 || isnan (sp [-1].data.f_r4))
5134 THROW_EX_OVF (ip);
5135 sp [-1].data.l = (gint64)sp [-1].data.f_r4;
5136 ++ip;
5137 MINT_IN_BREAK;
5138 MINT_IN_CASE(MINT_CONV_OVF_I8_R8)
5139 if (sp [-1].data.f < G_MININT64 || sp [-1].data.f > G_MAXINT64 || isnan (sp [-1].data.f))
5140 THROW_EX_OVF (ip);
5141 sp [-1].data.l = (gint64)sp [-1].data.f;
5142 ++ip;
5143 MINT_IN_BREAK;
5144 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_I8)
5145 if ((guint64)sp [-1].data.l > G_MAXINT32)
5146 THROW_EX_OVF (ip);
5147 sp [-1].data.i = (gint32)sp [-1].data.l;
5148 ++ip;
5149 MINT_IN_BREAK;
5150 MINT_IN_CASE(MINT_BOX) {
5151 MonoVTable *vtable = (MonoVTable*)imethod->data_items [* (guint16 *)(ip + 1)];
5152 guint16 offset = * (guint16 *)(ip + 2);
5154 frame_objref (frame) = mono_gc_alloc_obj (vtable, m_class_get_instance_size (vtable->klass));
5155 stackval_to_data (m_class_get_byval_arg (vtable->klass), &sp [-1 - offset], mono_object_get_data (frame_objref (frame)), FALSE);
5157 sp [-1 - offset].data.p = frame_objref (frame);
5159 ip += 3;
5160 MINT_IN_BREAK;
5162 MINT_IN_CASE(MINT_BOX_VT) {
5163 MonoVTable *vtable = (MonoVTable*)imethod->data_items [* (guint16 *)(ip + 1)];
5164 c = vtable->klass;
5165 guint16 offset = * (guint16 *)(ip + 2);
5166 gboolean pop_vt_sp = !(offset & BOX_NOT_CLEAR_VT_SP);
5167 offset &= ~BOX_NOT_CLEAR_VT_SP;
5169 int size = mono_class_value_size (c, NULL);
5170 frame_objref (frame) = mono_gc_alloc_obj (vtable, m_class_get_instance_size (vtable->klass));
5171 mono_value_copy_internal (mono_object_get_data (frame_objref (frame)), sp [-1 - offset].data.p, c);
5173 sp [-1 - offset].data.p = frame_objref (frame);
5174 size = ALIGN_TO (size, MINT_VT_ALIGNMENT);
5175 if (pop_vt_sp)
5176 vt_sp -= size;
5178 ip += 3;
5179 MINT_IN_BREAK;
5181 MINT_IN_CASE(MINT_BOX_NULLABLE) {
5182 c = (MonoClass*)imethod->data_items [* (guint16 *)(ip + 1)];
5183 guint16 offset = * (guint16 *)(ip + 2);
5184 gboolean pop_vt_sp = !(offset & BOX_NOT_CLEAR_VT_SP);
5185 offset &= ~BOX_NOT_CLEAR_VT_SP;
5187 int size = mono_class_value_size (c, NULL);
5189 sp [-1 - offset].data.o = mono_nullable_box (sp [-1 - offset].data.p, c, error);
5190 mono_error_cleanup (error); /* FIXME: don't swallow the error */
5192 size = ALIGN_TO (size, MINT_VT_ALIGNMENT);
5193 if (pop_vt_sp)
5194 vt_sp -= size;
5196 ip += 3;
5197 MINT_IN_BREAK;
5199 MINT_IN_CASE(MINT_NEWARR) {
5200 MonoVTable *vtable = (MonoVTable*)imethod->data_items[*(guint16 *)(ip + 1)];
5201 sp [-1].data.o = (MonoObject*) mono_array_new_specific_checked (vtable, sp [-1].data.i, error);
5202 if (!is_ok (error)) {
5203 THROW_EX (mono_error_convert_to_exception (error), ip);
5205 mono_error_cleanup (error); /* FIXME: don't swallow the error */
5206 ip += 2;
5207 /*if (profiling_classes) {
5208 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
5209 count++;
5210 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
5213 MINT_IN_BREAK;
5215 MINT_IN_CASE(MINT_LDLEN)
5216 o = sp [-1].data.o;
5217 NULL_CHECK (o);
5218 sp [-1].data.nati = mono_array_length_internal ((MonoArray *)o);
5219 ++ip;
5220 MINT_IN_BREAK;
5221 MINT_IN_CASE(MINT_LDLEN_SPAN) {
5222 o = sp [-1].data.o;
5223 NULL_CHECK (o);
5224 gsize offset_length = (gsize) *(gint16 *) (ip + 1);
5225 sp [-1].data.nati = *(gint32 *) ((guint8 *) o + offset_length);
5226 ip += 2;
5227 MINT_IN_BREAK;
5229 MINT_IN_CASE(MINT_GETCHR) {
5230 MonoString *s;
5231 s = (MonoString*)sp [-2].data.p;
5232 NULL_CHECK (s);
5233 i32 = sp [-1].data.i;
5234 if (i32 < 0 || i32 >= mono_string_length_internal (s))
5235 THROW_EX (mono_get_exception_index_out_of_range (), ip);
5236 --sp;
5237 sp [-1].data.i = mono_string_chars_internal (s)[i32];
5238 ++ip;
5239 MINT_IN_BREAK;
5241 MINT_IN_CASE(MINT_GETITEM_SPAN) {
5242 guint8 *span = (guint8 *) sp [-2].data.p;
5243 int index = sp [-1].data.i;
5244 gsize element_size = (gsize) *(gint16 *) (ip + 1);
5245 gsize offset_length = (gsize) *(gint16 *) (ip + 2);
5246 gsize offset_pointer = (gsize) *(gint16 *) (ip + 3);
5247 sp--;
5249 NULL_CHECK (span);
5251 gint32 length = *(gint32 *) (span + offset_length);
5252 if (index < 0 || index >= length)
5253 THROW_EX (mono_get_exception_index_out_of_range (), ip);
5255 gpointer pointer = *(gpointer *)(span + offset_pointer);
5256 sp [-1].data.p = (guint8 *) pointer + index * element_size;
5258 ip += 4;
5259 MINT_IN_BREAK;
5261 MINT_IN_CASE(MINT_STRLEN)
5262 ++ip;
5263 o = sp [-1].data.o;
5264 NULL_CHECK (o);
5265 sp [-1].data.i = mono_string_length_internal ((MonoString*) o);
5266 MINT_IN_BREAK;
5267 MINT_IN_CASE(MINT_ARRAY_RANK)
5268 o = sp [-1].data.o;
5269 NULL_CHECK (o);
5270 sp [-1].data.i = m_class_get_rank (mono_object_class (sp [-1].data.p));
5271 ip++;
5272 MINT_IN_BREAK;
5273 MINT_IN_CASE(MINT_LDELEMA_FAST) {
5274 /* No bounds, one direction */
5275 gint32 size = READ32 (ip + 1);
5276 gint32 index = sp [-1].data.i;
5278 MonoArray *ao = (MonoArray*)sp [-2].data.o;
5279 NULL_CHECK (ao);
5280 if (index >= ao->max_length)
5281 THROW_EX (mono_get_exception_index_out_of_range (), ip);
5282 sp [-2].data.p = mono_array_addr_with_size_fast (ao, size, index);
5283 ip += 3;
5284 sp --;
5286 MINT_IN_BREAK;
5288 MINT_IN_CASE(MINT_LDELEMA)
5289 MINT_IN_CASE(MINT_LDELEMA_TC) {
5290 gboolean needs_typecheck = *ip == MINT_LDELEMA_TC;
5292 MonoClass *klass = (MonoClass*)imethod->data_items [*(guint16 *) (ip + 1)];
5293 guint16 numargs = *(guint16 *) (ip + 2);
5294 ip += 3;
5295 sp -= numargs;
5297 o = sp [0].data.o;
5298 NULL_CHECK (o);
5299 sp->data.p = ves_array_element_address (frame, klass, (MonoArray *) o, &sp [1], needs_typecheck);
5300 if (frame->ex)
5301 THROW_EX (frame->ex, ip);
5302 ++sp;
5304 MINT_IN_BREAK;
5306 MINT_IN_CASE(MINT_LDELEM_I1) /* fall through */
5307 MINT_IN_CASE(MINT_LDELEM_U1) /* fall through */
5308 MINT_IN_CASE(MINT_LDELEM_I2) /* fall through */
5309 MINT_IN_CASE(MINT_LDELEM_U2) /* fall through */
5310 MINT_IN_CASE(MINT_LDELEM_I4) /* fall through */
5311 MINT_IN_CASE(MINT_LDELEM_U4) /* fall through */
5312 MINT_IN_CASE(MINT_LDELEM_I8) /* fall through */
5313 MINT_IN_CASE(MINT_LDELEM_I) /* fall through */
5314 MINT_IN_CASE(MINT_LDELEM_R4) /* fall through */
5315 MINT_IN_CASE(MINT_LDELEM_R8) /* fall through */
5316 MINT_IN_CASE(MINT_LDELEM_REF) /* fall through */
5317 MINT_IN_CASE(MINT_LDELEM_VT) {
5318 MonoArray *o;
5319 mono_u aindex;
5321 sp -= 2;
5323 o = (MonoArray*)sp [0].data.p;
5324 NULL_CHECK (o);
5326 aindex = sp [1].data.i;
5327 if (aindex >= mono_array_length_internal (o))
5328 THROW_EX (mono_get_exception_index_out_of_range (), ip);
5331 * FIXME: throw mono_get_exception_array_type_mismatch () if needed
5333 switch (*ip) {
5334 case MINT_LDELEM_I1:
5335 sp [0].data.i = mono_array_get_fast (o, gint8, aindex);
5336 break;
5337 case MINT_LDELEM_U1:
5338 sp [0].data.i = mono_array_get_fast (o, guint8, aindex);
5339 break;
5340 case MINT_LDELEM_I2:
5341 sp [0].data.i = mono_array_get_fast (o, gint16, aindex);
5342 break;
5343 case MINT_LDELEM_U2:
5344 sp [0].data.i = mono_array_get_fast (o, guint16, aindex);
5345 break;
5346 case MINT_LDELEM_I:
5347 sp [0].data.nati = mono_array_get_fast (o, mono_i, aindex);
5348 break;
5349 case MINT_LDELEM_I4:
5350 sp [0].data.i = mono_array_get_fast (o, gint32, aindex);
5351 break;
5352 case MINT_LDELEM_U4:
5353 sp [0].data.i = mono_array_get_fast (o, guint32, aindex);
5354 break;
5355 case MINT_LDELEM_I8:
5356 sp [0].data.l = mono_array_get_fast (o, guint64, aindex);
5357 break;
5358 case MINT_LDELEM_R4:
5359 sp [0].data.f_r4 = mono_array_get_fast (o, float, aindex);
5360 break;
5361 case MINT_LDELEM_R8:
5362 sp [0].data.f = mono_array_get_fast (o, double, aindex);
5363 break;
5364 case MINT_LDELEM_REF:
5365 sp [0].data.p = mono_array_get_fast (o, gpointer, aindex);
5366 break;
5367 case MINT_LDELEM_VT: {
5368 i32 = READ32 (ip + 1);
5369 char *src_addr = mono_array_addr_with_size_fast ((MonoArray *) o, i32, aindex);
5370 sp [0].data.vt = vt_sp;
5371 // Copying to vtstack. No wbarrier needed
5372 memcpy (sp [0].data.vt, src_addr, i32);
5373 vt_sp += ALIGN_TO (i32, MINT_VT_ALIGNMENT);
5374 ip += 2;
5375 break;
5377 default:
5378 ves_abort();
5381 ++ip;
5382 ++sp;
5383 MINT_IN_BREAK;
5385 MINT_IN_CASE(MINT_STELEM_I) /* fall through */
5386 MINT_IN_CASE(MINT_STELEM_I1) /* fall through */
5387 MINT_IN_CASE(MINT_STELEM_U1) /* fall through */
5388 MINT_IN_CASE(MINT_STELEM_I2) /* fall through */
5389 MINT_IN_CASE(MINT_STELEM_U2) /* fall through */
5390 MINT_IN_CASE(MINT_STELEM_I4) /* fall through */
5391 MINT_IN_CASE(MINT_STELEM_I8) /* fall through */
5392 MINT_IN_CASE(MINT_STELEM_R4) /* fall through */
5393 MINT_IN_CASE(MINT_STELEM_R8) /* fall through */
5394 MINT_IN_CASE(MINT_STELEM_REF) /* fall through */
5395 MINT_IN_CASE(MINT_STELEM_VT) {
5396 mono_u aindex;
5398 sp -= 3;
5400 o = sp [0].data.o;
5401 NULL_CHECK (o);
5403 aindex = sp [1].data.i;
5404 if (aindex >= mono_array_length_internal ((MonoArray *)o))
5405 THROW_EX (mono_get_exception_index_out_of_range (), ip);
5407 switch (*ip) {
5408 case MINT_STELEM_I:
5409 mono_array_set_fast ((MonoArray *)o, mono_i, aindex, sp [2].data.nati);
5410 break;
5411 case MINT_STELEM_I1:
5412 mono_array_set_fast ((MonoArray *)o, gint8, aindex, sp [2].data.i);
5413 break;
5414 case MINT_STELEM_U1:
5415 mono_array_set_fast ((MonoArray *) o, guint8, aindex, sp [2].data.i);
5416 break;
5417 case MINT_STELEM_I2:
5418 mono_array_set_fast ((MonoArray *)o, gint16, aindex, sp [2].data.i);
5419 break;
5420 case MINT_STELEM_U2:
5421 mono_array_set_fast ((MonoArray *)o, guint16, aindex, sp [2].data.i);
5422 break;
5423 case MINT_STELEM_I4:
5424 mono_array_set_fast ((MonoArray *)o, gint32, aindex, sp [2].data.i);
5425 break;
5426 case MINT_STELEM_I8:
5427 mono_array_set_fast ((MonoArray *)o, gint64, aindex, sp [2].data.l);
5428 break;
5429 case MINT_STELEM_R4:
5430 mono_array_set_fast ((MonoArray *)o, float, aindex, sp [2].data.f_r4);
5431 break;
5432 case MINT_STELEM_R8:
5433 mono_array_set_fast ((MonoArray *)o, double, aindex, sp [2].data.f);
5434 break;
5435 case MINT_STELEM_REF: {
5436 MonoObject *isinst_obj = mono_object_isinst_checked (sp [2].data.o, m_class_get_element_class (mono_object_class (o)), error);
5437 mono_error_cleanup (error); /* FIXME: don't swallow the error */
5438 if (sp [2].data.p && !isinst_obj)
5439 THROW_EX (mono_get_exception_array_type_mismatch (), ip);
5440 mono_array_setref_fast ((MonoArray *) o, aindex, sp [2].data.p);
5441 break;
5443 case MINT_STELEM_VT: {
5444 MonoClass *klass_vt = (MonoClass*)imethod->data_items [*(guint16 *) (ip + 1)];
5445 i32 = READ32 (ip + 2);
5446 char *dst_addr = mono_array_addr_with_size_fast ((MonoArray *) o, i32, aindex);
5448 mono_value_copy_internal (dst_addr, sp [2].data.vt, klass_vt);
5449 vt_sp -= ALIGN_TO (i32, MINT_VT_ALIGNMENT);
5450 ip += 3;
5451 break;
5453 default:
5454 ves_abort();
5457 ++ip;
5458 MINT_IN_BREAK;
5460 MINT_IN_CASE(MINT_CONV_OVF_I4_U4)
5461 if (sp [-1].data.i < 0)
5462 THROW_EX_OVF (ip);
5463 ++ip;
5464 MINT_IN_BREAK;
5465 MINT_IN_CASE(MINT_CONV_OVF_I4_I8)
5466 if (sp [-1].data.l < G_MININT32 || sp [-1].data.l > G_MAXINT32)
5467 THROW_EX_OVF (ip);
5468 sp [-1].data.i = (gint32) sp [-1].data.l;
5469 ++ip;
5470 MINT_IN_BREAK;
5471 MINT_IN_CASE(MINT_CONV_OVF_I4_U8)
5472 if (sp [-1].data.l < 0 || sp [-1].data.l > G_MAXINT32)
5473 THROW_EX_OVF (ip);
5474 sp [-1].data.i = (gint32) sp [-1].data.l;
5475 ++ip;
5476 MINT_IN_BREAK;
5477 MINT_IN_CASE(MINT_CONV_OVF_I4_R4)
5478 if (sp [-1].data.f_r4 < G_MININT32 || sp [-1].data.f_r4 > G_MAXINT32)
5479 THROW_EX_OVF (ip);
5480 sp [-1].data.i = (gint32) sp [-1].data.f_r4;
5481 ++ip;
5482 MINT_IN_BREAK;
5483 MINT_IN_CASE(MINT_CONV_OVF_I4_R8)
5484 if (sp [-1].data.f < G_MININT32 || sp [-1].data.f > G_MAXINT32)
5485 THROW_EX_OVF (ip);
5486 sp [-1].data.i = (gint32) sp [-1].data.f;
5487 ++ip;
5488 MINT_IN_BREAK;
5489 MINT_IN_CASE(MINT_CONV_OVF_U4_I4)
5490 if (sp [-1].data.i < 0)
5491 THROW_EX_OVF (ip);
5492 ++ip;
5493 MINT_IN_BREAK;
5494 MINT_IN_CASE(MINT_CONV_OVF_U4_I8)
5495 if (sp [-1].data.l < 0 || sp [-1].data.l > G_MAXUINT32)
5496 THROW_EX_OVF (ip);
5497 sp [-1].data.i = (guint32) sp [-1].data.l;
5498 ++ip;
5499 MINT_IN_BREAK;
5500 MINT_IN_CASE(MINT_CONV_OVF_U4_R4)
5501 if (sp [-1].data.f_r4 < 0 || sp [-1].data.f_r4 > G_MAXUINT32)
5502 THROW_EX_OVF (ip);
5503 sp [-1].data.i = (guint32) sp [-1].data.f_r4;
5504 ++ip;
5505 MINT_IN_BREAK;
5506 MINT_IN_CASE(MINT_CONV_OVF_U4_R8)
5507 if (sp [-1].data.f < 0 || sp [-1].data.f > G_MAXUINT32)
5508 THROW_EX_OVF (ip);
5509 sp [-1].data.i = (guint32) sp [-1].data.f;
5510 ++ip;
5511 MINT_IN_BREAK;
5512 MINT_IN_CASE(MINT_CONV_OVF_I2_I4)
5513 if (sp [-1].data.i < G_MININT16 || sp [-1].data.i > G_MAXINT16)
5514 THROW_EX_OVF (ip);
5515 ++ip;
5516 MINT_IN_BREAK;
5517 MINT_IN_CASE(MINT_CONV_OVF_I2_U4)
5518 if (sp [-1].data.i < 0 || sp [-1].data.i > G_MAXINT16)
5519 THROW_EX_OVF (ip);
5520 ++ip;
5521 MINT_IN_BREAK;
5522 MINT_IN_CASE(MINT_CONV_OVF_I2_I8)
5523 if (sp [-1].data.l < G_MININT16 || sp [-1].data.l > G_MAXINT16)
5524 THROW_EX_OVF (ip);
5525 sp [-1].data.i = (gint16) sp [-1].data.l;
5526 ++ip;
5527 MINT_IN_BREAK;
5528 MINT_IN_CASE(MINT_CONV_OVF_I2_U8)
5529 if (sp [-1].data.l < 0 || sp [-1].data.l > G_MAXINT16)
5530 THROW_EX_OVF (ip);
5531 sp [-1].data.i = (gint16) sp [-1].data.l;
5532 ++ip;
5533 MINT_IN_BREAK;
5534 MINT_IN_CASE(MINT_CONV_OVF_I2_R8)
5535 if (sp [-1].data.f < G_MININT16 || sp [-1].data.f > G_MAXINT16)
5536 THROW_EX_OVF (ip);
5537 sp [-1].data.i = (gint16) sp [-1].data.f;
5538 ++ip;
5539 MINT_IN_BREAK;
5540 MINT_IN_CASE(MINT_CONV_OVF_I2_UN_R8)
5541 if (sp [-1].data.f < 0 || sp [-1].data.f > G_MAXINT16)
5542 THROW_EX_OVF (ip);
5543 sp [-1].data.i = (gint16) sp [-1].data.f;
5544 ++ip;
5545 MINT_IN_BREAK;
5546 MINT_IN_CASE(MINT_CONV_OVF_U2_I4)
5547 if (sp [-1].data.i < 0 || sp [-1].data.i > G_MAXUINT16)
5548 THROW_EX_OVF (ip);
5549 ++ip;
5550 MINT_IN_BREAK;
5551 MINT_IN_CASE(MINT_CONV_OVF_U2_I8)
5552 if (sp [-1].data.l < 0 || sp [-1].data.l > G_MAXUINT16)
5553 THROW_EX_OVF (ip);
5554 sp [-1].data.i = (guint16) sp [-1].data.l;
5555 ++ip;
5556 MINT_IN_BREAK;
5557 MINT_IN_CASE(MINT_CONV_OVF_U2_R8)
5558 if (sp [-1].data.f < 0 || sp [-1].data.f > G_MAXUINT16)
5559 THROW_EX_OVF (ip);
5560 sp [-1].data.i = (guint16) sp [-1].data.f;
5561 ++ip;
5562 MINT_IN_BREAK;
5563 MINT_IN_CASE(MINT_CONV_OVF_I1_I4)
5564 if (sp [-1].data.i < G_MININT8 || sp [-1].data.i > G_MAXINT8)
5565 THROW_EX_OVF (ip);
5566 ++ip;
5567 MINT_IN_BREAK;
5568 MINT_IN_CASE(MINT_CONV_OVF_I1_U4)
5569 if (sp [-1].data.i < 0 || sp [-1].data.i > G_MAXINT8)
5570 THROW_EX_OVF (ip);
5571 ++ip;
5572 MINT_IN_BREAK;
5573 MINT_IN_CASE(MINT_CONV_OVF_I1_I8)
5574 if (sp [-1].data.l < G_MININT8 || sp [-1].data.l > G_MAXINT8)
5575 THROW_EX_OVF (ip);
5576 sp [-1].data.i = (gint8) sp [-1].data.l;
5577 ++ip;
5578 MINT_IN_BREAK;
5579 MINT_IN_CASE(MINT_CONV_OVF_I1_U8)
5580 if (sp [-1].data.l < 0 || sp [-1].data.l > G_MAXINT8)
5581 THROW_EX_OVF (ip);
5582 sp [-1].data.i = (gint8) sp [-1].data.l;
5583 ++ip;
5584 MINT_IN_BREAK;
5585 MINT_IN_CASE(MINT_CONV_OVF_I1_R8)
5586 if (sp [-1].data.f < G_MININT8 || sp [-1].data.f > G_MAXINT8)
5587 THROW_EX_OVF (ip);
5588 sp [-1].data.i = (gint8) sp [-1].data.f;
5589 ++ip;
5590 MINT_IN_BREAK;
5591 MINT_IN_CASE(MINT_CONV_OVF_I1_UN_R8)
5592 if (sp [-1].data.f < 0 || sp [-1].data.f > G_MAXINT8)
5593 THROW_EX_OVF (ip);
5594 sp [-1].data.i = (gint8) sp [-1].data.f;
5595 ++ip;
5596 MINT_IN_BREAK;
5597 MINT_IN_CASE(MINT_CONV_OVF_U1_I4)
5598 if (sp [-1].data.i < 0 || sp [-1].data.i > G_MAXUINT8)
5599 THROW_EX_OVF (ip);
5600 ++ip;
5601 MINT_IN_BREAK;
5602 MINT_IN_CASE(MINT_CONV_OVF_U1_I8)
5603 if (sp [-1].data.l < 0 || sp [-1].data.l > G_MAXUINT8)
5604 THROW_EX_OVF (ip);
5605 sp [-1].data.i = (guint8) sp [-1].data.l;
5606 ++ip;
5607 MINT_IN_BREAK;
5608 MINT_IN_CASE(MINT_CONV_OVF_U1_R8)
5609 if (sp [-1].data.f < 0 || sp [-1].data.f > G_MAXUINT8)
5610 THROW_EX_OVF (ip);
5611 sp [-1].data.i = (guint8) sp [-1].data.f;
5612 ++ip;
5613 MINT_IN_BREAK;
5614 MINT_IN_CASE(MINT_CKFINITE)
5615 if (!mono_isfinite (sp [-1].data.f))
5616 THROW_EX (mono_get_exception_arithmetic (), ip);
5617 ++ip;
5618 MINT_IN_BREAK;
5619 MINT_IN_CASE(MINT_MKREFANY) {
5620 c = (MonoClass*)imethod->data_items [*(guint16 *)(ip + 1)];
5622 /* The value address is on the stack */
5623 gpointer addr = sp [-1].data.p;
5624 /* Push the typedref value on the stack */
5625 sp [-1].data.p = vt_sp;
5626 vt_sp += ALIGN_TO (sizeof (MonoTypedRef), MINT_VT_ALIGNMENT);
5628 MonoTypedRef *tref = (MonoTypedRef*)sp [-1].data.p;
5629 tref->klass = c;
5630 tref->type = m_class_get_byval_arg (c);
5631 tref->value = addr;
5633 ip += 2;
5634 MINT_IN_BREAK;
5636 MINT_IN_CASE(MINT_REFANYTYPE) {
5637 MonoTypedRef *tref = (MonoTypedRef*)sp [-1].data.p;
5638 MonoType *type = tref->type;
5640 vt_sp -= ALIGN_TO (sizeof (MonoTypedRef), MINT_VT_ALIGNMENT);
5641 sp [-1].data.p = vt_sp;
5642 vt_sp += 8;
5643 *(gpointer*)sp [-1].data.p = type;
5644 ip ++;
5645 MINT_IN_BREAK;
5647 MINT_IN_CASE(MINT_REFANYVAL) {
5648 MonoTypedRef *tref = (MonoTypedRef*)sp [-1].data.p;
5649 gpointer addr = tref->value;
5651 c = (MonoClass*)imethod->data_items [*(guint16 *)(ip + 1)];
5652 if (c != tref->klass)
5653 THROW_EX (mono_get_exception_invalid_cast (), ip);
5655 vt_sp -= ALIGN_TO (sizeof (MonoTypedRef), MINT_VT_ALIGNMENT);
5657 sp [-1].data.p = addr;
5658 ip += 2;
5659 MINT_IN_BREAK;
5661 MINT_IN_CASE(MINT_LDTOKEN)
5662 sp->data.p = vt_sp;
5663 vt_sp += 8;
5664 * (gpointer *)sp->data.p = imethod->data_items[*(guint16 *)(ip + 1)];
5665 ip += 2;
5666 ++sp;
5667 MINT_IN_BREAK;
5668 MINT_IN_CASE(MINT_ADD_OVF_I4)
5669 if (CHECK_ADD_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
5670 THROW_EX_OVF (ip);
5671 BINOP(i, +);
5672 MINT_IN_BREAK;
5673 MINT_IN_CASE(MINT_ADD_OVF_I8)
5674 if (CHECK_ADD_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
5675 THROW_EX_OVF (ip);
5676 BINOP(l, +);
5677 MINT_IN_BREAK;
5678 MINT_IN_CASE(MINT_ADD_OVF_UN_I4)
5679 if (CHECK_ADD_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
5680 THROW_EX_OVF (ip);
5681 BINOP_CAST(i, +, guint32);
5682 MINT_IN_BREAK;
5683 MINT_IN_CASE(MINT_ADD_OVF_UN_I8)
5684 if (CHECK_ADD_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
5685 THROW_EX_OVF (ip);
5686 BINOP_CAST(l, +, guint64);
5687 MINT_IN_BREAK;
5688 MINT_IN_CASE(MINT_MUL_OVF_I4)
5689 if (CHECK_MUL_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
5690 THROW_EX_OVF (ip);
5691 BINOP(i, *);
5692 MINT_IN_BREAK;
5693 MINT_IN_CASE(MINT_MUL_OVF_I8)
5694 if (CHECK_MUL_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
5695 THROW_EX_OVF (ip);
5696 BINOP(l, *);
5697 MINT_IN_BREAK;
5698 MINT_IN_CASE(MINT_MUL_OVF_UN_I4)
5699 if (CHECK_MUL_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
5700 THROW_EX_OVF (ip);
5701 BINOP_CAST(i, *, guint32);
5702 MINT_IN_BREAK;
5703 MINT_IN_CASE(MINT_MUL_OVF_UN_I8)
5704 if (CHECK_MUL_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
5705 THROW_EX_OVF (ip);
5706 BINOP_CAST(l, *, guint64);
5707 MINT_IN_BREAK;
5708 MINT_IN_CASE(MINT_SUB_OVF_I4)
5709 if (CHECK_SUB_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
5710 THROW_EX_OVF (ip);
5711 BINOP(i, -);
5712 MINT_IN_BREAK;
5713 MINT_IN_CASE(MINT_SUB_OVF_I8)
5714 if (CHECK_SUB_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
5715 THROW_EX_OVF (ip);
5716 BINOP(l, -);
5717 MINT_IN_BREAK;
5718 MINT_IN_CASE(MINT_SUB_OVF_UN_I4)
5719 if (CHECK_SUB_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
5720 THROW_EX_OVF (ip);
5721 BINOP_CAST(i, -, guint32);
5722 MINT_IN_BREAK;
5723 MINT_IN_CASE(MINT_SUB_OVF_UN_I8)
5724 if (CHECK_SUB_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
5725 THROW_EX_OVF (ip);
5726 BINOP_CAST(l, -, guint64);
5727 MINT_IN_BREAK;
5728 MINT_IN_CASE(MINT_START_ABORT_PROT)
5729 mono_threads_begin_abort_protected_block ();
5730 ip ++;
5731 MINT_IN_BREAK;
5732 MINT_IN_CASE(MINT_ENDFINALLY) {
5733 ip ++;
5734 int clause_index = *ip;
5735 gboolean pending_abort = mono_threads_end_abort_protected_block ();
5737 if (clause_args && clause_index == clause_args->exit_clause)
5738 goto exit_frame;
5739 g_assert (sp >= frame->stack);
5740 sp = frame->stack;
5742 if (frame->finally_ips) {
5743 ip = (const guint16*)frame->finally_ips->data;
5744 frame->finally_ips = g_slist_remove (frame->finally_ips, ip);
5745 /* Throw abort after the last finally block to avoid confusing EH */
5746 if (pending_abort && !frame->finally_ips)
5747 EXCEPTION_CHECKPOINT;
5748 MINT_IN_DISPATCH(*ip);
5750 ves_abort();
5751 MINT_IN_BREAK;
5754 MINT_IN_CASE(MINT_LEAVE)
5755 MINT_IN_CASE(MINT_LEAVE_S)
5756 MINT_IN_CASE(MINT_LEAVE_CHECK)
5757 MINT_IN_CASE(MINT_LEAVE_S_CHECK) {
5758 g_assert (sp >= frame->stack);
5759 sp = frame->stack;
5760 frame->ip = ip;
5762 if (*ip == MINT_LEAVE_S_CHECK || *ip == MINT_LEAVE_CHECK) {
5763 if (imethod->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE) {
5764 stackval tmp_sp;
5766 child_frame.parent = frame;
5767 child_frame.imethod = NULL;
5769 * We need for mono_thread_get_undeniable_exception to be able to unwind
5770 * to check the abort threshold. For this to work we use child_frame as a
5771 * dummy frame that is stored in the lmf and serves as the transition frame
5773 do_icall_wrapper (&child_frame, NULL, MINT_ICALL_V_P, &tmp_sp, (gpointer)mono_thread_get_undeniable_exception, FALSE);
5775 MonoException *abort_exc = (MonoException*)tmp_sp.data.p;
5776 if (abort_exc)
5777 THROW_EX (abort_exc, frame->ip);
5781 if (*ip == MINT_LEAVE_S || *ip == MINT_LEAVE_S_CHECK) {
5782 ip += (short) *(ip + 1);
5783 } else {
5784 ip += (gint32) READ32 (ip + 1);
5786 frame->endfinally_ip = ip;
5788 guint32 ip_offset;
5789 MonoExceptionClause *clause;
5790 GSList *old_list = frame->finally_ips;
5791 MonoMethod *method = imethod->method;
5793 #if DEBUG_INTERP
5794 if (tracing)
5795 g_print ("* Handle finally IL_%04x\n", frame->endfinally_ip == NULL ? 0 : frame->endfinally_ip - imethod->code);
5796 #endif
5797 if (imethod == NULL || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
5798 || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
5799 goto exit_frame;
5801 ip_offset = frame->ip - imethod->code;
5803 if (frame->endfinally_ip != NULL)
5804 frame->finally_ips = g_slist_prepend(frame->finally_ips, (void *)frame->endfinally_ip);
5806 for (int i = imethod->num_clauses - 1; i >= 0; i--) {
5807 clause = &imethod->clauses [i];
5808 if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && (frame->endfinally_ip == NULL || !(MONO_OFFSET_IN_CLAUSE (clause, frame->endfinally_ip - imethod->code)))) {
5809 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
5810 ip = imethod->code + clause->handler_offset;
5811 frame->finally_ips = g_slist_prepend (frame->finally_ips, (gpointer) ip);
5812 #if DEBUG_INTERP
5813 if (tracing)
5814 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
5815 #endif
5820 frame->endfinally_ip = NULL;
5822 if (old_list != frame->finally_ips && frame->finally_ips) {
5823 ip = (const guint16*)frame->finally_ips->data;
5824 frame->finally_ips = g_slist_remove (frame->finally_ips, ip);
5825 sp = frame->stack; /* spec says stack should be empty at endfinally so it should be at the start too */
5826 vt_sp = (unsigned char *) sp + imethod->stack_size;
5827 MINT_IN_DISPATCH (*ip);
5830 ves_abort();
5831 MINT_IN_BREAK;
5833 MINT_IN_CASE(MINT_ICALL_V_V)
5834 MINT_IN_CASE(MINT_ICALL_V_P)
5835 MINT_IN_CASE(MINT_ICALL_P_V)
5836 MINT_IN_CASE(MINT_ICALL_P_P)
5837 MINT_IN_CASE(MINT_ICALL_PP_V)
5838 MINT_IN_CASE(MINT_ICALL_PP_P)
5839 MINT_IN_CASE(MINT_ICALL_PPP_V)
5840 MINT_IN_CASE(MINT_ICALL_PPP_P)
5841 MINT_IN_CASE(MINT_ICALL_PPPP_V)
5842 MINT_IN_CASE(MINT_ICALL_PPPP_P)
5843 MINT_IN_CASE(MINT_ICALL_PPPPP_V)
5844 MINT_IN_CASE(MINT_ICALL_PPPPP_P)
5845 MINT_IN_CASE(MINT_ICALL_PPPPPP_V)
5846 MINT_IN_CASE(MINT_ICALL_PPPPPP_P)
5847 frame->ip = ip;
5848 sp = do_icall_wrapper (frame, NULL, *ip, sp, imethod->data_items [*(guint16 *)(ip + 1)], FALSE);
5849 EXCEPTION_CHECKPOINT;
5850 CHECK_RESUME_STATE (context);
5851 ip += 2;
5852 MINT_IN_BREAK;
5853 MINT_IN_CASE(MINT_MONO_LDPTR)
5854 sp->data.p = imethod->data_items [*(guint16 *)(ip + 1)];
5855 ip += 2;
5856 ++sp;
5857 MINT_IN_BREAK;
5858 MINT_IN_CASE(MINT_MONO_NEWOBJ)
5859 sp->data.p = mono_object_new_checked (imethod->domain, (MonoClass*)imethod->data_items [*(guint16 *)(ip + 1)], error);
5860 mono_error_cleanup (error); /* FIXME: don't swallow the error */
5861 ip += 2;
5862 sp++;
5863 MINT_IN_BREAK;
5864 MINT_IN_CASE(MINT_MONO_FREE)
5865 ++ip;
5866 --sp;
5867 g_error ("that doesn't seem right");
5868 g_free (sp->data.p);
5869 MINT_IN_BREAK;
5870 MINT_IN_CASE(MINT_MONO_RETOBJ)
5871 ++ip;
5872 sp--;
5873 stackval_from_data (mono_method_signature_internal (imethod->method)->ret, frame->retval, sp->data.p,
5874 mono_method_signature_internal (imethod->method)->pinvoke);
5875 if (sp > frame->stack)
5876 g_warning ("retobj: more values on stack: %d", sp-frame->stack);
5877 goto exit_frame;
5878 MINT_IN_CASE(MINT_MONO_SGEN_THREAD_INFO)
5879 sp->data.p = mono_tls_get_sgen_thread_info ();
5880 sp++;
5881 ++ip;
5882 MINT_IN_BREAK;
5883 MINT_IN_CASE(MINT_MONO_MEMORY_BARRIER) {
5884 ++ip;
5885 mono_memory_barrier ();
5886 MINT_IN_BREAK;
5888 MINT_IN_CASE(MINT_MONO_LDDOMAIN)
5889 sp->data.p = mono_domain_get ();
5890 ++sp;
5891 ++ip;
5892 MINT_IN_BREAK;
5893 MINT_IN_CASE(MINT_SDB_INTR_LOC)
5894 if (G_UNLIKELY (ss_enabled)) {
5895 typedef void (*T) (void);
5896 static T ss_tramp;
5898 if (!ss_tramp) {
5899 void *tramp = mini_get_single_step_trampoline ();
5900 mono_memory_barrier ();
5901 ss_tramp = (T)tramp;
5905 * Make this point to the MINT_SDB_SEQ_POINT instruction which follows this since
5906 * the address of that instruction is stored as the seq point address.
5908 frame->ip = ip + 1;
5911 * Use the same trampoline as the JIT. This ensures that
5912 * the debugger has the context for the last interpreter
5913 * native frame.
5915 do_debugger_tramp (ss_tramp, frame);
5917 CHECK_RESUME_STATE (context);
5919 ++ip;
5920 MINT_IN_BREAK;
5921 MINT_IN_CASE(MINT_SDB_SEQ_POINT)
5922 /* Just a placeholder for a breakpoint */
5923 ++ip;
5924 MINT_IN_BREAK;
5925 MINT_IN_CASE(MINT_SDB_BREAKPOINT) {
5926 typedef void (*T) (void);
5927 static T bp_tramp;
5928 if (!bp_tramp) {
5929 void *tramp = mini_get_breakpoint_trampoline ();
5930 mono_memory_barrier ();
5931 bp_tramp = (T)tramp;
5934 frame->ip = ip;
5936 /* Use the same trampoline as the JIT */
5937 do_debugger_tramp (bp_tramp, frame);
5939 CHECK_RESUME_STATE (context);
5941 ++ip;
5942 MINT_IN_BREAK;
5945 #define RELOP(datamem, op) \
5946 --sp; \
5947 sp [-1].data.i = sp [-1].data.datamem op sp [0].data.datamem; \
5948 ++ip;
5950 #define RELOP_FP(datamem, op, noorder) \
5951 --sp; \
5952 if (mono_isunordered (sp [-1].data.datamem, sp [0].data.datamem)) \
5953 sp [-1].data.i = noorder; \
5954 else \
5955 sp [-1].data.i = sp [-1].data.datamem op sp [0].data.datamem; \
5956 ++ip;
5958 MINT_IN_CASE(MINT_CEQ_I4)
5959 RELOP(i, ==);
5960 MINT_IN_BREAK;
5961 MINT_IN_CASE(MINT_CEQ0_I4)
5962 sp [-1].data.i = (sp [-1].data.i == 0);
5963 ++ip;
5964 MINT_IN_BREAK;
5965 MINT_IN_CASE(MINT_CEQ_I8)
5966 RELOP(l, ==);
5967 MINT_IN_BREAK;
5968 MINT_IN_CASE(MINT_CEQ_R4)
5969 RELOP_FP(f_r4, ==, 0);
5970 MINT_IN_BREAK;
5971 MINT_IN_CASE(MINT_CEQ_R8)
5972 RELOP_FP(f, ==, 0);
5973 MINT_IN_BREAK;
5974 MINT_IN_CASE(MINT_CNE_I4)
5975 RELOP(i, !=);
5976 MINT_IN_BREAK;
5977 MINT_IN_CASE(MINT_CNE_I8)
5978 RELOP(l, !=);
5979 MINT_IN_BREAK;
5980 MINT_IN_CASE(MINT_CNE_R4)
5981 RELOP_FP(f_r4, !=, 1);
5982 MINT_IN_BREAK;
5983 MINT_IN_CASE(MINT_CNE_R8)
5984 RELOP_FP(f, !=, 1);
5985 MINT_IN_BREAK;
5986 MINT_IN_CASE(MINT_CGT_I4)
5987 RELOP(i, >);
5988 MINT_IN_BREAK;
5989 MINT_IN_CASE(MINT_CGT_I8)
5990 RELOP(l, >);
5991 MINT_IN_BREAK;
5992 MINT_IN_CASE(MINT_CGT_R4)
5993 RELOP_FP(f_r4, >, 0);
5994 MINT_IN_BREAK;
5995 MINT_IN_CASE(MINT_CGT_R8)
5996 RELOP_FP(f, >, 0);
5997 MINT_IN_BREAK;
5998 MINT_IN_CASE(MINT_CGE_I4)
5999 RELOP(i, >=);
6000 MINT_IN_BREAK;
6001 MINT_IN_CASE(MINT_CGE_I8)
6002 RELOP(l, >=);
6003 MINT_IN_BREAK;
6004 MINT_IN_CASE(MINT_CGE_R4)
6005 RELOP_FP(f_r4, >=, 0);
6006 MINT_IN_BREAK;
6007 MINT_IN_CASE(MINT_CGE_R8)
6008 RELOP_FP(f, >=, 0);
6009 MINT_IN_BREAK;
6011 #define RELOP_CAST(datamem, op, type) \
6012 --sp; \
6013 sp [-1].data.i = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
6014 ++ip;
6016 MINT_IN_CASE(MINT_CGE_UN_I4)
6017 RELOP_CAST(l, >=, guint32);
6018 MINT_IN_BREAK;
6019 MINT_IN_CASE(MINT_CGE_UN_I8)
6020 RELOP_CAST(l, >=, guint64);
6021 MINT_IN_BREAK;
6023 MINT_IN_CASE(MINT_CGT_UN_I4)
6024 RELOP_CAST(i, >, guint32);
6025 MINT_IN_BREAK;
6026 MINT_IN_CASE(MINT_CGT_UN_I8)
6027 RELOP_CAST(l, >, guint64);
6028 MINT_IN_BREAK;
6029 MINT_IN_CASE(MINT_CGT_UN_R4)
6030 RELOP_FP(f_r4, >, 1);
6031 MINT_IN_BREAK;
6032 MINT_IN_CASE(MINT_CGT_UN_R8)
6033 RELOP_FP(f, >, 1);
6034 MINT_IN_BREAK;
6035 MINT_IN_CASE(MINT_CLT_I4)
6036 RELOP(i, <);
6037 MINT_IN_BREAK;
6038 MINT_IN_CASE(MINT_CLT_I8)
6039 RELOP(l, <);
6040 MINT_IN_BREAK;
6041 MINT_IN_CASE(MINT_CLT_R4)
6042 RELOP_FP(f_r4, <, 0);
6043 MINT_IN_BREAK;
6044 MINT_IN_CASE(MINT_CLT_R8)
6045 RELOP_FP(f, <, 0);
6046 MINT_IN_BREAK;
6047 MINT_IN_CASE(MINT_CLT_UN_I4)
6048 RELOP_CAST(i, <, guint32);
6049 MINT_IN_BREAK;
6050 MINT_IN_CASE(MINT_CLT_UN_I8)
6051 RELOP_CAST(l, <, guint64);
6052 MINT_IN_BREAK;
6053 MINT_IN_CASE(MINT_CLT_UN_R4)
6054 RELOP_FP(f_r4, <, 1);
6055 MINT_IN_BREAK;
6056 MINT_IN_CASE(MINT_CLT_UN_R8)
6057 RELOP_FP(f, <, 1);
6058 MINT_IN_BREAK;
6059 MINT_IN_CASE(MINT_CLE_I4)
6060 RELOP(i, <=);
6061 MINT_IN_BREAK;
6062 MINT_IN_CASE(MINT_CLE_I8)
6063 RELOP(l, <=);
6064 MINT_IN_BREAK;
6065 MINT_IN_CASE(MINT_CLE_UN_I4)
6066 RELOP_CAST(l, <=, guint32);
6067 MINT_IN_BREAK;
6068 MINT_IN_CASE(MINT_CLE_UN_I8)
6069 RELOP_CAST(l, <=, guint64);
6070 MINT_IN_BREAK;
6071 MINT_IN_CASE(MINT_CLE_R4)
6072 RELOP_FP(f_r4, <=, 0);
6073 MINT_IN_BREAK;
6074 MINT_IN_CASE(MINT_CLE_R8)
6075 RELOP_FP(f, <=, 0);
6076 MINT_IN_BREAK;
6078 #undef RELOP
6079 #undef RELOP_FP
6080 #undef RELOP_CAST
6082 MINT_IN_CASE(MINT_LDFTN) {
6083 sp->data.p = imethod->data_items [* (guint16 *)(ip + 1)];
6084 ++sp;
6085 ip += 2;
6086 MINT_IN_BREAK;
6088 MINT_IN_CASE(MINT_LDVIRTFTN) {
6089 InterpMethod *m = (InterpMethod*)imethod->data_items [* (guint16 *)(ip + 1)];
6090 --sp;
6091 NULL_CHECK (sp->data.p);
6093 sp->data.p = get_virtual_method (m, sp->data.o->vtable);
6094 ip += 2;
6095 ++sp;
6096 MINT_IN_BREAK;
6098 MINT_IN_CASE(MINT_LDFTN_DYNAMIC) {
6099 MONO_API_ERROR_INIT (error);
6100 InterpMethod *m = mono_interp_get_imethod (mono_domain_get (), (MonoMethod*) sp [-1].data.p, error);
6101 mono_error_assert_ok (error);
6102 sp [-1].data.p = m;
6103 ip++;
6104 MINT_IN_BREAK;
6107 #define LDARG(datamem, argtype) \
6108 sp->data.datamem = (argtype) frame->stack_args [*(guint16 *)(ip + 1)].data.datamem; \
6109 ip += 2; \
6110 ++sp;
6112 MINT_IN_CASE(MINT_LDARG_I1) LDARG(i, gint8); MINT_IN_BREAK;
6113 MINT_IN_CASE(MINT_LDARG_U1) LDARG(i, guint8); MINT_IN_BREAK;
6114 MINT_IN_CASE(MINT_LDARG_I2) LDARG(i, gint16); MINT_IN_BREAK;
6115 MINT_IN_CASE(MINT_LDARG_U2) LDARG(i, guint16); MINT_IN_BREAK;
6116 MINT_IN_CASE(MINT_LDARG_I4) LDARG(i, gint32); MINT_IN_BREAK;
6117 MINT_IN_CASE(MINT_LDARG_I8) LDARG(l, gint64); MINT_IN_BREAK;
6118 MINT_IN_CASE(MINT_LDARG_R4) LDARG(f_r4, float); MINT_IN_BREAK;
6119 MINT_IN_CASE(MINT_LDARG_R8) LDARG(f, double); MINT_IN_BREAK;
6120 MINT_IN_CASE(MINT_LDARG_O) LDARG(p, gpointer); MINT_IN_BREAK;
6121 MINT_IN_CASE(MINT_LDARG_P) LDARG(p, gpointer); MINT_IN_BREAK;
6123 MINT_IN_CASE(MINT_LDARG_VT)
6124 sp->data.p = vt_sp;
6125 i32 = READ32(ip + 2);
6126 memcpy(sp->data.p, frame->stack_args [* (guint16 *)(ip + 1)].data.p, i32);
6127 vt_sp += ALIGN_TO (i32, MINT_VT_ALIGNMENT);
6128 ip += 4;
6129 ++sp;
6130 MINT_IN_BREAK;
6132 #define STARG(datamem, argtype) \
6133 --sp; \
6134 frame->stack_args [*(guint16 *)(ip + 1)].data.datamem = (argtype) sp->data.datamem; \
6135 ip += 2; \
6137 MINT_IN_CASE(MINT_STARG_I1) STARG(i, gint8); MINT_IN_BREAK;
6138 MINT_IN_CASE(MINT_STARG_U1) STARG(i, guint8); MINT_IN_BREAK;
6139 MINT_IN_CASE(MINT_STARG_I2) STARG(i, gint16); MINT_IN_BREAK;
6140 MINT_IN_CASE(MINT_STARG_U2) STARG(i, guint16); MINT_IN_BREAK;
6141 MINT_IN_CASE(MINT_STARG_I4) STARG(i, gint32); MINT_IN_BREAK;
6142 MINT_IN_CASE(MINT_STARG_I8) STARG(l, gint64); MINT_IN_BREAK;
6143 MINT_IN_CASE(MINT_STARG_R4) STARG(f_r4, float); MINT_IN_BREAK;
6144 MINT_IN_CASE(MINT_STARG_R8) STARG(f, double); MINT_IN_BREAK;
6145 MINT_IN_CASE(MINT_STARG_O) STARG(p, gpointer); MINT_IN_BREAK;
6146 MINT_IN_CASE(MINT_STARG_P) STARG(p, gpointer); MINT_IN_BREAK;
6148 MINT_IN_CASE(MINT_STARG_VT)
6149 i32 = READ32(ip + 2);
6150 --sp;
6151 memcpy(frame->stack_args [* (guint16 *)(ip + 1)].data.p, sp->data.p, i32);
6152 vt_sp -= ALIGN_TO (i32, MINT_VT_ALIGNMENT);
6153 ip += 4;
6154 MINT_IN_BREAK;
6156 MINT_IN_CASE(MINT_PROF_ENTER) {
6157 ip += 1;
6159 if (MONO_PROFILER_ENABLED (method_enter)) {
6160 MonoProfilerCallContext *prof_ctx = NULL;
6162 if (imethod->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_ENTER_CONTEXT) {
6163 prof_ctx = g_new0 (MonoProfilerCallContext, 1);
6164 prof_ctx->interp_frame = frame;
6165 prof_ctx->method = imethod->method;
6168 MONO_PROFILER_RAISE (method_enter, (imethod->method, prof_ctx));
6170 g_free (prof_ctx);
6173 MINT_IN_BREAK;
6176 MINT_IN_CASE(MINT_TRACE_ENTER) {
6177 ip += 1;
6179 MonoProfilerCallContext *prof_ctx = g_alloca (sizeof (MonoProfilerCallContext));
6180 prof_ctx->interp_frame = frame;
6181 prof_ctx->method = imethod->method;
6183 mono_trace_enter_method (imethod->method, prof_ctx);
6184 MINT_IN_BREAK;
6187 MINT_IN_CASE(MINT_TRACE_EXIT) {
6188 // Set retval
6189 i32 = READ32(ip + 1);
6190 --sp;
6191 if (i32 == -1)
6193 else if (i32)
6194 memcpy(frame->retval->data.p, sp->data.p, i32);
6195 else
6196 *frame->retval = *sp;
6198 MonoProfilerCallContext *prof_ctx = g_alloca (sizeof (MonoProfilerCallContext));
6199 prof_ctx->interp_frame = frame;
6200 prof_ctx->method = imethod->method;
6202 mono_trace_leave_method (imethod->method, prof_ctx);
6203 ip += 3;
6204 goto exit_frame;
6207 MINT_IN_CASE(MINT_LDARGA)
6208 sp->data.p = &frame->stack_args [* (guint16 *)(ip + 1)];
6209 ip += 2;
6210 ++sp;
6211 MINT_IN_BREAK;
6213 MINT_IN_CASE(MINT_LDARGA_VT)
6214 sp->data.p = frame->stack_args [* (guint16 *)(ip + 1)].data.p;
6215 ip += 2;
6216 ++sp;
6217 MINT_IN_BREAK;
6219 #define LDLOC(datamem, argtype) \
6220 sp->data.datamem = * (argtype *)(locals + * (guint16 *)(ip + 1)); \
6221 ip += 2; \
6222 ++sp;
6224 MINT_IN_CASE(MINT_LDLOC_I1) LDLOC(i, gint8); MINT_IN_BREAK;
6225 MINT_IN_CASE(MINT_LDLOC_U1) LDLOC(i, guint8); MINT_IN_BREAK;
6226 MINT_IN_CASE(MINT_LDLOC_I2) LDLOC(i, gint16); MINT_IN_BREAK;
6227 MINT_IN_CASE(MINT_LDLOC_U2) LDLOC(i, guint16); MINT_IN_BREAK;
6228 MINT_IN_CASE(MINT_LDLOC_I4) LDLOC(i, gint32); MINT_IN_BREAK;
6229 MINT_IN_CASE(MINT_LDLOC_I8) LDLOC(l, gint64); MINT_IN_BREAK;
6230 MINT_IN_CASE(MINT_LDLOC_R4) LDLOC(f_r4, float); MINT_IN_BREAK;
6231 MINT_IN_CASE(MINT_LDLOC_R8) LDLOC(f, double); MINT_IN_BREAK;
6232 MINT_IN_CASE(MINT_LDLOC_O) LDLOC(p, gpointer); MINT_IN_BREAK;
6233 MINT_IN_CASE(MINT_LDLOC_P) LDLOC(p, gpointer); MINT_IN_BREAK;
6235 MINT_IN_CASE(MINT_LDLOC_VT)
6236 sp->data.p = vt_sp;
6237 i32 = READ32(ip + 2);
6238 memcpy(sp->data.p, locals + * (guint16 *)(ip + 1), i32);
6239 vt_sp += ALIGN_TO (i32, MINT_VT_ALIGNMENT);
6240 ip += 4;
6241 ++sp;
6242 MINT_IN_BREAK;
6244 MINT_IN_CASE(MINT_LDLOCA_S)
6245 sp->data.p = locals + * (guint16 *)(ip + 1);
6246 ip += 2;
6247 ++sp;
6248 MINT_IN_BREAK;
6250 #define STLOC(datamem, argtype) \
6251 --sp; \
6252 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp->data.datamem; \
6253 ip += 2;
6255 MINT_IN_CASE(MINT_STLOC_I1) STLOC(i, gint8); MINT_IN_BREAK;
6256 MINT_IN_CASE(MINT_STLOC_U1) STLOC(i, guint8); MINT_IN_BREAK;
6257 MINT_IN_CASE(MINT_STLOC_I2) STLOC(i, gint16); MINT_IN_BREAK;
6258 MINT_IN_CASE(MINT_STLOC_U2) STLOC(i, guint16); MINT_IN_BREAK;
6259 MINT_IN_CASE(MINT_STLOC_I4) STLOC(i, gint32); MINT_IN_BREAK;
6260 MINT_IN_CASE(MINT_STLOC_I8) STLOC(l, gint64); MINT_IN_BREAK;
6261 MINT_IN_CASE(MINT_STLOC_R4) STLOC(f_r4, float); MINT_IN_BREAK;
6262 MINT_IN_CASE(MINT_STLOC_R8) STLOC(f, double); MINT_IN_BREAK;
6263 MINT_IN_CASE(MINT_STLOC_O) STLOC(p, gpointer); MINT_IN_BREAK;
6264 MINT_IN_CASE(MINT_STLOC_P) STLOC(p, gpointer); MINT_IN_BREAK;
6266 #define STLOC_NP(datamem, argtype) \
6267 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp [-1].data.datamem; \
6268 ip += 2;
6270 MINT_IN_CASE(MINT_STLOC_NP_I4) STLOC_NP(i, gint32); MINT_IN_BREAK;
6271 MINT_IN_CASE(MINT_STLOC_NP_O) STLOC_NP(p, gpointer); MINT_IN_BREAK;
6273 MINT_IN_CASE(MINT_STLOC_VT)
6274 i32 = READ32(ip + 2);
6275 --sp;
6276 memcpy(locals + * (guint16 *)(ip + 1), sp->data.p, i32);
6277 vt_sp -= ALIGN_TO (i32, MINT_VT_ALIGNMENT);
6278 ip += 4;
6279 MINT_IN_BREAK;
6281 MINT_IN_CASE(MINT_LOCALLOC) {
6282 if (sp != frame->stack + 1) /*FIX?*/
6283 THROW_EX (mono_get_exception_execution_engine (NULL), ip);
6285 int len = sp [-1].data.i;
6286 sp [-1].data.p = alloca (len);
6288 if (imethod->init_locals)
6289 memset (sp [-1].data.p, 0, len);
6290 ++ip;
6291 MINT_IN_BREAK;
6293 MINT_IN_CASE(MINT_ENDFILTER)
6294 /* top of stack is result of filter */
6295 frame->retval = &sp [-1];
6296 goto exit_frame;
6297 MINT_IN_CASE(MINT_INITOBJ)
6298 --sp;
6299 memset (sp->data.vt, 0, READ32(ip + 1));
6300 ip += 3;
6301 MINT_IN_BREAK;
6302 MINT_IN_CASE(MINT_CPBLK)
6303 sp -= 3;
6304 if (!sp [0].data.p || !sp [1].data.p)
6305 THROW_EX (mono_get_exception_null_reference(), ip - 1);
6306 ++ip;
6307 /* FIXME: value and size may be int64... */
6308 memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
6309 MINT_IN_BREAK;
6310 #if 0
6311 MINT_IN_CASE(MINT_CONSTRAINED_) {
6312 guint32 token;
6313 /* FIXME: implement */
6314 ++ip;
6315 token = READ32 (ip);
6316 ip += 2;
6317 MINT_IN_BREAK;
6319 #endif
6320 MINT_IN_CASE(MINT_INITBLK)
6321 sp -= 3;
6322 NULL_CHECK (sp [0].data.p);
6323 ++ip;
6324 /* FIXME: value and size may be int64... */
6325 memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
6326 MINT_IN_BREAK;
6327 #if 0
6328 MINT_IN_CASE(MINT_NO_)
6329 /* FIXME: implement */
6330 ip += 2;
6331 MINT_IN_BREAK;
6332 #endif
6333 MINT_IN_CASE(MINT_RETHROW) {
6334 int exvar_offset = *(guint16*)(ip + 1);
6335 THROW_EX_GENERAL (*(MonoException**)(frame->locals + exvar_offset), ip, TRUE);
6336 MINT_IN_BREAK;
6338 MINT_IN_CASE(MINT_MONO_RETHROW) {
6340 * need to clarify what this should actually do:
6342 * Takes an exception from the stack and rethrows it.
6343 * This is useful for wrappers that don't want to have to
6344 * use CEE_THROW and lose the exception stacktrace.
6347 --sp;
6348 if (!sp->data.p)
6349 sp->data.p = mono_get_exception_null_reference ();
6351 THROW_EX_GENERAL ((MonoException *)sp->data.p, ip, TRUE);
6352 MINT_IN_BREAK;
6354 MINT_IN_CASE(MINT_LD_DELEGATE_METHOD_PTR) {
6355 MonoDelegate *del;
6357 --sp;
6358 del = (MonoDelegate*)sp->data.p;
6359 if (!del->interp_method) {
6360 /* Not created from interpreted code */
6361 MONO_API_ERROR_INIT (error);
6362 g_assert (del->method);
6363 del->interp_method = mono_interp_get_imethod (del->object.vtable->domain, del->method, error);
6364 mono_error_assert_ok (error);
6366 g_assert (del->interp_method);
6367 sp->data.p = del->interp_method;
6368 ++sp;
6369 ip += 1;
6370 MINT_IN_BREAK;
6372 MINT_IN_CASE(MINT_LD_DELEGATE_INVOKE_IMPL) {
6373 MonoDelegate *del;
6374 int n = *(guint16*)(ip + 1);
6375 del = (MonoDelegate*)sp [-n].data.p;
6376 if (!del->interp_invoke_impl) {
6378 * First time we are called. Set up the invoke wrapper. We might be able to do this
6379 * in ctor but we would need to handle AllocDelegateLike_internal separately
6381 MONO_API_ERROR_INIT (error);
6382 MonoMethod *invoke = mono_get_delegate_invoke_internal (del->object.vtable->klass);
6383 del->interp_invoke_impl = mono_interp_get_imethod (del->object.vtable->domain, mono_marshal_get_delegate_invoke (invoke, del), error);
6384 mono_error_assert_ok (error);
6386 sp ++;
6387 sp [-1].data.p = del->interp_invoke_impl;
6388 ip += 2;
6389 MINT_IN_BREAK;
6392 #define MATH_UNOP(mathfunc) \
6393 sp [-1].data.f = mathfunc (sp [-1].data.f); \
6394 ++ip;
6396 MINT_IN_CASE(MINT_ABS) MATH_UNOP(fabs); MINT_IN_BREAK;
6397 MINT_IN_CASE(MINT_ASIN) MATH_UNOP(asin); MINT_IN_BREAK;
6398 MINT_IN_CASE(MINT_ASINH) MATH_UNOP(asinh); MINT_IN_BREAK;
6399 MINT_IN_CASE(MINT_ACOS) MATH_UNOP(acos); MINT_IN_BREAK;
6400 MINT_IN_CASE(MINT_ACOSH) MATH_UNOP(acosh); MINT_IN_BREAK;
6401 MINT_IN_CASE(MINT_ATAN) MATH_UNOP(atan); MINT_IN_BREAK;
6402 MINT_IN_CASE(MINT_ATANH) MATH_UNOP(atanh); MINT_IN_BREAK;
6403 MINT_IN_CASE(MINT_COS) MATH_UNOP(cos); MINT_IN_BREAK;
6404 MINT_IN_CASE(MINT_CBRT) MATH_UNOP(cbrt); MINT_IN_BREAK;
6405 MINT_IN_CASE(MINT_COSH) MATH_UNOP(cosh); MINT_IN_BREAK;
6406 MINT_IN_CASE(MINT_SIN) MATH_UNOP(sin); MINT_IN_BREAK;
6407 MINT_IN_CASE(MINT_SQRT) MATH_UNOP(sqrt); MINT_IN_BREAK;
6408 MINT_IN_CASE(MINT_SINH) MATH_UNOP(sinh); MINT_IN_BREAK;
6409 MINT_IN_CASE(MINT_TAN) MATH_UNOP(tan); MINT_IN_BREAK;
6410 MINT_IN_CASE(MINT_TANH) MATH_UNOP(tanh); MINT_IN_BREAK;
6412 MINT_IN_CASE(MINT_INTRINS_ENUM_HASFLAG) {
6413 MonoClass *klass = (MonoClass*)imethod->data_items[* (guint16 *)(ip + 1)];
6414 guint64 a_val = 0, b_val = 0;
6416 stackval_to_data (m_class_get_byval_arg (klass), &sp [-2], &a_val, FALSE);
6417 stackval_to_data (m_class_get_byval_arg (klass), &sp [-1], &b_val, FALSE);
6418 sp--;
6419 sp [-1].data.i = (a_val & b_val) == b_val;
6420 ip += 2;
6421 MINT_IN_BREAK;
6423 MINT_IN_CASE(MINT_INTRINS_GET_HASHCODE) {
6424 sp [-1].data.i = mono_object_hash_internal (sp [-1].data.o);
6425 ip++;
6426 MINT_IN_BREAK;
6428 MINT_IN_CASE(MINT_INTRINS_GET_TYPE) {
6429 NULL_CHECK (sp [-1].data.p);
6430 sp [-1].data.o = (MonoObject*) sp [-1].data.o->vtable->type;
6431 ip++;
6432 MINT_IN_BREAK;
6435 MINT_IN_DEFAULT
6436 g_error ("Unimplemented opcode: %04x %s at 0x%x\n", *ip, mono_interp_opname[*ip], ip-imethod->code);
6440 g_assert_not_reached ();
6442 exit_frame:
6443 error_init_reuse (error);
6445 if (clause_args && clause_args->base_frame)
6446 memcpy (clause_args->base_frame->args, frame->args, imethod->alloca_size);
6448 if (!frame->ex && MONO_PROFILER_ENABLED (method_leave) &&
6449 imethod->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_LEAVE) {
6450 MonoProfilerCallContext *prof_ctx = NULL;
6452 if (imethod->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_LEAVE_CONTEXT) {
6453 prof_ctx = g_new0 (MonoProfilerCallContext, 1);
6454 prof_ctx->interp_frame = frame;
6455 prof_ctx->method = imethod->method;
6457 MonoType *rtype = mono_method_signature_internal (imethod->method)->ret;
6459 switch (rtype->type) {
6460 case MONO_TYPE_VOID:
6461 break;
6462 case MONO_TYPE_VALUETYPE:
6463 prof_ctx->return_value = frame->retval->data.p;
6464 break;
6465 default:
6466 prof_ctx->return_value = frame->retval;
6467 break;
6471 MONO_PROFILER_RAISE (method_leave, (imethod->method, prof_ctx));
6473 g_free (prof_ctx);
6474 } else if (frame->ex && imethod->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_EXCEPTION_LEAVE)
6475 MONO_PROFILER_RAISE (method_exception_leave, (imethod->method, &frame->ex->object));
6477 DEBUG_LEAVE ();
6480 static void
6481 interp_parse_options (const char *options)
6483 char **args, **ptr;
6485 if (!options)
6486 return;
6488 args = g_strsplit (options, ",", -1);
6489 for (ptr = args; ptr && *ptr; ptr ++) {
6490 char *arg = *ptr;
6492 if (strncmp (arg, "jit=", 4) == 0)
6493 mono_interp_jit_classes = g_slist_prepend (mono_interp_jit_classes, arg + 4);
6494 if (strncmp (arg, "interp-only=", 4) == 0)
6495 mono_interp_only_classes = g_slist_prepend (mono_interp_only_classes, arg + strlen ("interp-only="));
6496 if (strncmp (arg, "-inline", 7) == 0)
6497 mono_interp_opt &= ~INTERP_OPT_INLINE;
6502 * interp_set_resume_state:
6504 * Set the state the interpeter will continue to execute from after execution returns to the interpreter.
6506 static void
6507 interp_set_resume_state (MonoJitTlsData *jit_tls, MonoException *ex, MonoJitExceptionInfo *ei, MonoInterpFrameHandle interp_frame, gpointer handler_ip)
6509 ThreadContext *context;
6511 g_assert (jit_tls);
6512 context = (ThreadContext*)jit_tls->interp_context;
6513 g_assert (context);
6515 context->has_resume_state = TRUE;
6516 context->handler_frame = (InterpFrame*)interp_frame;
6517 context->handler_ei = ei;
6518 /* This is on the stack, so it doesn't need a wbarrier */
6519 context->handler_frame->ex = ex;
6520 /* Ditto */
6521 if (ei)
6522 *(MonoException**)(context->handler_frame->locals + ei->exvar_offset) = ex;
6523 context->handler_ip = (guint16*) handler_ip;
6526 static void
6527 interp_get_resume_state (const MonoJitTlsData *jit_tls, gboolean *has_resume_state, MonoInterpFrameHandle *interp_frame, gpointer *handler_ip)
6529 g_assert (jit_tls);
6530 ThreadContext *context = (ThreadContext*)jit_tls->interp_context;
6531 g_assert (context);
6532 *has_resume_state = context->has_resume_state;
6533 if (context->has_resume_state) {
6534 *interp_frame = context->handler_frame;
6535 *handler_ip = context->handler_ip;
6540 * interp_run_finally:
6542 * Run the finally clause identified by CLAUSE_INDEX in the intepreter frame given by
6543 * frame->interp_frame.
6544 * Return TRUE if the finally clause threw an exception.
6546 static gboolean
6547 interp_run_finally (StackFrameInfo *frame, int clause_index, gpointer handler_ip, gpointer handler_ip_end)
6549 InterpFrame *iframe = (InterpFrame*)frame->interp_frame;
6550 ThreadContext *context = get_context ();
6551 const unsigned short *old_ip = iframe->ip;
6552 FrameClauseArgs clause_args;
6554 memset (&clause_args, 0, sizeof (FrameClauseArgs));
6555 clause_args.start_with_ip = (guint16*) handler_ip;
6556 clause_args.end_at_ip = (guint16*) handler_ip_end;
6557 clause_args.exit_clause = clause_index;
6559 ERROR_DECL (error);
6560 interp_exec_method_full (iframe, context, &clause_args, error);
6561 if (context->has_resume_state) {
6562 return TRUE;
6563 } else {
6564 iframe->ip = old_ip;
6565 return FALSE;
6570 * interp_run_filter:
6572 * Run the filter clause identified by CLAUSE_INDEX in the intepreter frame given by
6573 * frame->interp_frame.
6575 static gboolean
6576 interp_run_filter (StackFrameInfo *frame, MonoException *ex, int clause_index, gpointer handler_ip, gpointer handler_ip_end)
6578 InterpFrame *iframe = (InterpFrame*)frame->interp_frame;
6579 ThreadContext *context = get_context ();
6580 InterpFrame child_frame;
6581 stackval retval;
6582 FrameClauseArgs clause_args;
6585 * Have to run the clause in a new frame which is a copy of IFRAME, since
6586 * during debugging, there are two copies of the frame on the stack.
6588 memset (&child_frame, 0, sizeof (InterpFrame));
6589 child_frame.imethod = iframe->imethod;
6590 child_frame.retval = &retval;
6591 child_frame.parent = iframe;
6592 child_frame.stack_args = iframe->stack_args;
6594 memset (&clause_args, 0, sizeof (FrameClauseArgs));
6595 clause_args.start_with_ip = (guint16*) handler_ip;
6596 clause_args.end_at_ip = (guint16*) handler_ip_end;
6597 clause_args.filter_exception = ex;
6598 clause_args.base_frame = iframe;
6600 ERROR_DECL (error);
6601 interp_exec_method_full (&child_frame, context, &clause_args, error);
6602 /* ENDFILTER stores the result into child_frame->retval */
6603 return child_frame.retval->data.i ? TRUE : FALSE;
6606 typedef struct {
6607 InterpFrame *current;
6608 } StackIter;
6611 * interp_frame_iter_init:
6613 * Initialize an iterator for iterating through interpreted frames.
6615 static void
6616 interp_frame_iter_init (MonoInterpStackIter *iter, gpointer interp_exit_data)
6618 StackIter *stack_iter = (StackIter*)iter;
6620 stack_iter->current = (InterpFrame*)interp_exit_data;
6624 * interp_frame_iter_next:
6626 * Fill out FRAME with date for the next interpreter frame.
6628 static gboolean
6629 interp_frame_iter_next (MonoInterpStackIter *iter, StackFrameInfo *frame)
6631 StackIter *stack_iter = (StackIter*)iter;
6632 InterpFrame *iframe = stack_iter->current;
6634 memset (frame, 0, sizeof (StackFrameInfo));
6635 /* pinvoke frames doesn't have imethod set */
6636 while (iframe && !(iframe->imethod && iframe->imethod->code && iframe->imethod->jinfo))
6637 iframe = iframe->parent;
6638 if (!iframe)
6639 return FALSE;
6641 MonoMethod *method = iframe->imethod->method;
6642 frame->domain = iframe->imethod->domain;
6643 frame->interp_frame = iframe;
6644 frame->method = method;
6645 frame->actual_method = method;
6646 if (method && ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)))) {
6647 frame->native_offset = -1;
6648 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
6649 } else {
6650 frame->type = FRAME_TYPE_INTERP;
6651 /* This is the offset in the interpreter IR */
6652 frame->native_offset = (guint8*)iframe->ip - (guint8*)iframe->imethod->code;
6653 if (!method->wrapper_type || method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
6654 frame->managed = TRUE;
6656 frame->ji = iframe->imethod->jinfo;
6657 frame->frame_addr = iframe;
6659 stack_iter->current = iframe->parent;
6661 return TRUE;
6664 static MonoJitInfo*
6665 interp_find_jit_info (MonoDomain *domain, MonoMethod *method)
6667 InterpMethod* imethod;
6669 imethod = lookup_imethod (domain, method);
6670 if (imethod)
6671 return imethod->jinfo;
6672 else
6673 return NULL;
6676 static void
6677 interp_set_breakpoint (MonoJitInfo *jinfo, gpointer ip)
6679 guint16 *code = (guint16*)ip;
6680 g_assert (*code == MINT_SDB_SEQ_POINT);
6681 *code = MINT_SDB_BREAKPOINT;
6684 static void
6685 interp_clear_breakpoint (MonoJitInfo *jinfo, gpointer ip)
6687 guint16 *code = (guint16*)ip;
6688 g_assert (*code == MINT_SDB_BREAKPOINT);
6689 *code = MINT_SDB_SEQ_POINT;
6692 static MonoJitInfo*
6693 interp_frame_get_jit_info (MonoInterpFrameHandle frame)
6695 InterpFrame *iframe = (InterpFrame*)frame;
6697 g_assert (iframe->imethod);
6698 return iframe->imethod->jinfo;
6701 static gpointer
6702 interp_frame_get_ip (MonoInterpFrameHandle frame)
6704 InterpFrame *iframe = (InterpFrame*)frame;
6706 g_assert (iframe->imethod);
6707 return (gpointer)iframe->ip;
6710 static gpointer
6711 interp_frame_get_arg (MonoInterpFrameHandle frame, int pos)
6713 InterpFrame *iframe = (InterpFrame*)frame;
6714 MonoMethodSignature *sig;
6716 g_assert (iframe->imethod);
6718 sig = mono_method_signature_internal (iframe->imethod->method);
6719 return stackval_to_data_addr (sig->params [pos], &iframe->stack_args [pos + !!iframe->imethod->hasthis]);
6722 static gpointer
6723 interp_frame_get_local (MonoInterpFrameHandle frame, int pos)
6725 InterpFrame *iframe = (InterpFrame*)frame;
6727 g_assert (iframe->imethod);
6729 return iframe->locals + iframe->imethod->local_offsets [pos];
6732 static gpointer
6733 interp_frame_get_this (MonoInterpFrameHandle frame)
6735 InterpFrame *iframe = (InterpFrame*)frame;
6737 g_assert (iframe->imethod);
6738 g_assert (iframe->imethod->hasthis);
6739 return &iframe->stack_args [0].data.p;
6742 static MonoInterpFrameHandle
6743 interp_frame_get_parent (MonoInterpFrameHandle frame)
6745 InterpFrame *iframe = (InterpFrame*)frame;
6747 return iframe->parent;
6750 static gpointer
6751 interp_frame_get_res (MonoInterpFrameHandle frame)
6753 InterpFrame *iframe = (InterpFrame*)frame;
6754 MonoMethodSignature *sig;
6756 g_assert (iframe->imethod);
6757 sig = mono_method_signature_internal (iframe->imethod->method);
6758 if (sig->ret->type == MONO_TYPE_VOID)
6759 return NULL;
6760 else
6761 return stackval_to_data_addr (sig->ret, iframe->retval);
6764 static void
6765 interp_start_single_stepping (void)
6767 ss_enabled = TRUE;
6770 static void
6771 interp_stop_single_stepping (void)
6773 ss_enabled = FALSE;
6776 static void
6777 register_interp_stats (void)
6779 mono_counters_init ();
6780 mono_counters_register ("Total transform time", MONO_COUNTER_INTERP | MONO_COUNTER_LONG | MONO_COUNTER_TIME, &mono_interp_stats.transform_time);
6781 mono_counters_register ("Methods inlined", MONO_COUNTER_INTERP | MONO_COUNTER_INT, &mono_interp_stats.inlined_methods);
6782 mono_counters_register ("Inline failures", MONO_COUNTER_INTERP | MONO_COUNTER_INT, &mono_interp_stats.inline_failures);
6785 #undef MONO_EE_CALLBACK
6786 #define MONO_EE_CALLBACK(ret, name, sig) interp_ ## name,
6788 static const MonoEECallbacks mono_interp_callbacks = {
6789 MONO_EE_CALLBACKS
6792 void
6793 mono_ee_interp_init (const char *opts)
6795 g_assert (mono_ee_api_version () == MONO_EE_API_VERSION);
6796 g_assert (!interp_init_done);
6797 interp_init_done = TRUE;
6799 mono_native_tls_alloc (&thread_context_id, NULL);
6800 set_context (NULL);
6802 interp_parse_options (opts);
6803 if (mini_get_debug_options ()->mdb_optimizations)
6804 mono_interp_opt &= ~INTERP_OPT_INLINE;
6805 mono_interp_transform_init ();
6807 mini_install_interp_callbacks (&mono_interp_callbacks);
6809 register_interp_stats ();