3 * transform CIL into different opcodes for more
4 * efficient interpretation
6 * Written by Bernie Solomon (bernard@ugsolutions.com)
11 #include <mono/metadata/appdomain.h>
12 #include <mono/metadata/debug-helpers.h>
13 #include <mono/metadata/exception.h>
14 #include <mono/metadata/mono-endian.h>
15 #include <mono/metadata/marshal.h>
16 #include <mono/metadata/profiler-private.h>
17 #include <mono/metadata/tabledefs.h>
19 #include <mono/mini/mini.h>
22 #include "interp-internals.h"
25 // TODO: export from marshal.c
26 MonoDelegate* mono_ftnptr_to_delegate (MonoClass *klass, gpointer ftn);
40 MonoMethodHeader *header;
42 const unsigned char *il_code;
43 const unsigned char *ip;
44 const unsigned char *last_ip;
45 const unsigned char *in_start;
49 StackInfo **stack_state;
52 unsigned char *is_bb_start;
53 unsigned short *new_code;
54 unsigned short *new_code_end;
55 unsigned short *new_ip;
56 unsigned short *last_new_ip;
57 unsigned int max_code_size;
60 unsigned int max_stack_height;
62 unsigned int max_vt_sp;
66 GHashTable *data_hash;
69 #define MINT_TYPE_I1 0
70 #define MINT_TYPE_U1 1
71 #define MINT_TYPE_I2 2
72 #define MINT_TYPE_U2 3
73 #define MINT_TYPE_I4 4
74 #define MINT_TYPE_I8 5
75 #define MINT_TYPE_R4 6
76 #define MINT_TYPE_R8 7
79 #define MINT_TYPE_VT 10
81 #define STACK_TYPE_I4 0
82 #define STACK_TYPE_I8 1
83 #define STACK_TYPE_R8 2
84 #define STACK_TYPE_O 3
85 #define STACK_TYPE_VT 4
86 #define STACK_TYPE_MP 5
87 #define STACK_TYPE_F 6
89 static const char *stack_type_string [] = { "I4", "I8", "R8", "O ", "VT", "MP", "F " };
91 #if SIZEOF_VOID_P == 8
92 #define STACK_TYPE_I STACK_TYPE_I8
94 #define STACK_TYPE_I STACK_TYPE_I4
97 static int stack_type [] = {
100 STACK_TYPE_I4, /*I2*/
101 STACK_TYPE_I4, /*U2*/
102 STACK_TYPE_I4, /*I4*/
103 STACK_TYPE_I8, /*I8*/
104 STACK_TYPE_R8, /*R4*/
105 STACK_TYPE_R8, /*R8*/
112 grow_code (TransformData *td)
114 unsigned int old_ip_offset = td->new_ip - td->new_code;
115 unsigned int old_last_ip_offset = td->last_new_ip - td->new_code;
116 g_assert (old_ip_offset <= td->max_code_size);
117 td->new_code = g_realloc (td->new_code, (td->max_code_size *= 2) * sizeof (td->new_code [0]));
118 td->new_code_end = td->new_code + td->max_code_size;
119 td->new_ip = td->new_code + old_ip_offset;
120 td->last_new_ip = td->new_code + old_last_ip_offset;
123 #define ENSURE_CODE(td, n) \
125 if ((td)->new_ip + (n) > (td)->new_code_end) \
129 #define ADD_CODE(td, n) \
131 if ((td)->new_ip == (td)->new_code_end) \
133 *(td)->new_ip++ = (n); \
136 #define CHECK_STACK(td, n) \
138 int stack_size = (td)->sp - (td)->stack; \
139 if (stack_size < (n)) \
140 g_warning ("%s.%s: not enough values (%d < %d) on stack at %04x", \
141 (td)->method->klass->name, (td)->method->name, \
142 stack_size, n, (td)->ip - (td)->il_code); \
145 #define ENSURE_I4(td, sp_off) \
147 if ((td)->sp [-sp_off].type == STACK_TYPE_I8) \
148 ADD_CODE(td, sp_off == 1 ? MINT_CONV_I4_I8 : MINT_CONV_I4_I8_SP); \
152 handle_branch(TransformData *td, int short_op, int long_op, int offset)
154 int shorten_branch = 0;
155 int target = td->ip + offset - td->il_code;
156 if (target < 0 || target >= td->code_size)
157 g_assert_not_reached ();
158 if (offset > 0 && td->stack_height [target] < 0) {
159 td->stack_height [target] = td->sp - td->stack;
160 if (td->stack_height [target] > 0)
161 td->stack_state [target] = g_memdup (td->stack, td->stack_height [target] * sizeof (td->stack [0]));
162 td->vt_stack_size [target] = td->vt_sp;
165 offset = td->in_offsets [target] - (td->new_ip - td->new_code);
166 if (offset >= -32768) {
170 int prev = td->forward_refs [target];
171 td->forward_refs [td->ip - td->il_code] = prev;
172 td->forward_refs [target] = td->ip - td->il_code;
174 if (td->header->code_size <= 25000) /* FIX to be precise somehow? */
177 if (shorten_branch) {
178 ADD_CODE(td, short_op);
179 ADD_CODE(td, offset);
181 ADD_CODE(td, long_op);
182 ADD_CODE(td, * (unsigned short *)(&offset));
183 ADD_CODE(td, * ((unsigned short *)&offset + 1));
188 one_arg_branch(TransformData *td, int mint_op, int offset)
190 int type = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
191 int long_op = mint_op + type - STACK_TYPE_I4;
192 int short_op = long_op + MINT_BRFALSE_I4_S - MINT_BRFALSE_I4;
195 handle_branch (td, short_op, long_op, offset);
199 two_arg_branch(TransformData *td, int mint_op, int offset)
201 int type1 = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
202 int type2 = td->sp [-2].type == STACK_TYPE_O || td->sp [-2].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-2].type;
203 int long_op = mint_op + type1 - STACK_TYPE_I4;
204 int short_op = long_op + MINT_BEQ_I4_S - MINT_BEQ_I4;
206 if (type1 == STACK_TYPE_I4 && type2 == STACK_TYPE_I8) {
207 ADD_CODE(td, MINT_CONV_I8_I4);
208 td->in_offsets [td->ip - td->il_code]++;
209 } else if (type1 == STACK_TYPE_I8 && type2 == STACK_TYPE_I4) {
210 ADD_CODE(td, MINT_CONV_I8_I4_SP);
211 td->in_offsets [td->ip - td->il_code]++;
212 } else if (type1 != type2) {
213 g_warning("%s.%s: branch type mismatch %d %d",
214 td->method->klass->name, td->method->name,
215 td->sp [-1].type, td->sp [-2].type);
218 handle_branch (td, short_op, long_op, offset);
222 unary_arith_op(TransformData *td, int mint_op)
224 int op = mint_op + td->sp [-1].type - STACK_TYPE_I4;
230 binary_arith_op(TransformData *td, int mint_op)
232 int type1 = td->sp [-2].type;
233 int type2 = td->sp [-1].type;
235 #if SIZEOF_VOID_P == 8
236 if ((type1 == STACK_TYPE_MP || type1 == STACK_TYPE_I8) && type2 == STACK_TYPE_I4) {
237 ADD_CODE(td, MINT_CONV_I8_I4);
238 type2 = STACK_TYPE_I8;
240 if (type1 == STACK_TYPE_I4 && (type2 == STACK_TYPE_MP || type2 == STACK_TYPE_I8)) {
241 ADD_CODE(td, MINT_CONV_I8_I4_SP);
242 type1 = STACK_TYPE_I8;
243 td->sp [-2].type = STACK_TYPE_I8;
246 if (type1 == STACK_TYPE_MP)
247 type1 = STACK_TYPE_I;
248 if (type2 == STACK_TYPE_MP)
249 type2 = STACK_TYPE_I;
250 if (type1 != type2) {
251 g_warning("%s.%s: %04x arith type mismatch %s %d %d",
252 td->method->klass->name, td->method->name,
253 td->ip - td->il_code, mono_interp_opname[mint_op], type1, type2);
255 op = mint_op + type1 - STACK_TYPE_I4;
262 binary_int_op(TransformData *td, int mint_op)
264 int op = mint_op + td->sp [-1].type - STACK_TYPE_I4;
266 if (td->sp [-1].type != td->sp [-2].type)
267 g_warning("%s.%s: int type mismatch", td->method->klass->name, td->method->name);
273 shift_op(TransformData *td, int mint_op)
275 int op = mint_op + td->sp [-2].type - STACK_TYPE_I4;
277 if (td->sp [-1].type != STACK_TYPE_I4) {
278 g_warning("%s.%s: shift type mismatch %d",
279 td->method->klass->name, td->method->name,
287 mint_type(MonoType *type)
292 switch (type->type) {
296 case MONO_TYPE_BOOLEAN:
308 #if SIZEOF_VOID_P == 4
322 case MONO_TYPE_STRING:
323 case MONO_TYPE_SZARRAY:
324 case MONO_TYPE_CLASS:
325 case MONO_TYPE_OBJECT:
326 case MONO_TYPE_ARRAY:
328 case MONO_TYPE_VALUETYPE:
329 if (type->data.klass->enumtype) {
330 type = mono_class_enum_basetype (type->data.klass);
334 case MONO_TYPE_GENERICINST:
335 type = &type->data.generic_class->container_class->byval_arg;
338 g_warning ("got type 0x%02x", type->type);
339 g_assert_not_reached ();
345 can_store (int stack_type, int var_type)
347 if (stack_type == STACK_TYPE_O || stack_type == STACK_TYPE_MP)
348 stack_type = STACK_TYPE_I;
349 if (var_type == STACK_TYPE_O || var_type == STACK_TYPE_MP)
350 var_type = STACK_TYPE_I;
351 return stack_type == var_type;
354 #define SET_SIMPLE_TYPE(s, ty) \
361 #define SET_TYPE(s, ty, k) \
368 #define PUSH_SIMPLE_TYPE(td, ty) \
372 sp_height = (td)->sp - (td)->stack; \
373 if (sp_height > (td)->max_stack_height) \
374 (td)->max_stack_height = sp_height; \
375 SET_SIMPLE_TYPE((td)->sp - 1, ty); \
378 #define PUSH_TYPE(td, ty, k) \
382 sp_height = (td)->sp - (td)->stack; \
383 if (sp_height > (td)->max_stack_height) \
384 (td)->max_stack_height = sp_height; \
385 SET_TYPE((td)->sp - 1, ty, k); \
388 #define PUSH_VT(td, size) \
390 (td)->vt_sp += ((size) + 7) & ~7; \
391 if ((td)->vt_sp > (td)->max_vt_sp) \
392 (td)->max_vt_sp = (td)->vt_sp; \
395 #define POP_VT(td, size) \
397 (td)->vt_sp -= ((size) + 7) & ~7; \
400 #if NO_UNALIGNED_ACCESS
401 #define WRITE32(td, v) \
403 ENSURE_CODE(td, 2); \
404 * (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
405 * ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
409 #define WRITE64(td, v) \
411 ENSURE_CODE(td, 4); \
412 * (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
413 * ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
414 * ((guint16 *)((td)->new_ip) + 2) = * ((guint16 *)(v) + 2); \
415 * ((guint16 *)((td)->new_ip) + 3) = * ((guint16 *)(v) + 3); \
419 #define WRITE32(td, v) \
421 ENSURE_CODE(td, 2); \
422 * (guint32 *)((td)->new_ip) = * (guint32 *)(v); \
426 #define WRITE64(td, v) \
428 ENSURE_CODE(td, 4); \
429 * (guint64 *)((td)->new_ip) = * (guint64 *)(v); \
436 load_arg(TransformData *td, int n)
439 MonoClass *klass = NULL;
442 gboolean hasthis = mono_method_signature (td->method)->hasthis;
443 if (hasthis && n == 0)
444 type = &td->method->klass->byval_arg;
446 type = mono_method_signature (td->method)->params [hasthis ? n - 1 : n];
448 mt = mint_type (type);
449 if (mt == MINT_TYPE_VT) {
451 klass = mono_class_from_mono_type (type);
452 if (mono_method_signature (td->method)->pinvoke)
453 size = mono_class_native_size (klass, NULL);
455 size = mono_class_value_size (klass, NULL);
457 if (hasthis && n == 0) {
459 ADD_CODE (td, MINT_LDARG_P);
460 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
464 ADD_CODE (td, MINT_LDARG_VT);
465 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
469 if (hasthis && n == 0) {
471 ADD_CODE (td, MINT_LDARG_P);
472 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
475 ADD_CODE(td, MINT_LDARG_I1 + (mt - MINT_TYPE_I1));
476 ADD_CODE(td, td->rtm->arg_offsets [n]); /* FIX for large offset */
477 if (mt == MINT_TYPE_O)
478 klass = mono_class_from_mono_type (type);
481 PUSH_TYPE(td, stack_type[mt], klass);
485 store_arg(TransformData *td, int n)
491 gboolean hasthis = mono_method_signature (td->method)->hasthis;
492 if (hasthis && n == 0)
493 type = &td->method->klass->byval_arg;
495 type = mono_method_signature (td->method)->params [n - !!hasthis];
497 mt = mint_type (type);
498 if (mt == MINT_TYPE_VT) {
500 MonoClass *klass = mono_class_from_mono_type (type);
501 if (mono_method_signature (td->method)->pinvoke)
502 size = mono_class_native_size (klass, NULL);
504 size = mono_class_value_size (klass, NULL);
505 ADD_CODE(td, MINT_STARG_VT);
506 ADD_CODE(td, td->rtm->arg_offsets [n]);
508 if (td->sp [-1].type == STACK_TYPE_VT)
511 ADD_CODE(td, MINT_STARG_I1 + (mt - MINT_TYPE_I1));
512 ADD_CODE(td, td->rtm->arg_offsets [n]);
518 store_inarg(TransformData *td, int n)
521 gboolean hasthis = mono_method_signature (td->method)->hasthis;
522 if (hasthis && n == 0)
523 type = &td->method->klass->byval_arg;
525 type = mono_method_signature (td->method)->params [n - !!hasthis];
527 int mt = mint_type (type);
528 if (hasthis && n == 0) {
529 ADD_CODE (td, MINT_STINARG_P);
533 if (mt == MINT_TYPE_VT) {
534 MonoClass *klass = mono_class_from_mono_type (type);
536 if (mono_method_signature (td->method)->pinvoke)
537 size = mono_class_native_size (klass, NULL);
539 size = mono_class_value_size (klass, NULL);
540 ADD_CODE(td, MINT_STINARG_VT);
544 ADD_CODE(td, MINT_STINARG_I1 + (mt - MINT_TYPE_I1));
550 load_local(TransformData *td, int n)
552 MonoType *type = td->header->locals [n];
553 int mt = mint_type (type);
554 int offset = td->rtm->local_offsets [n];
555 MonoClass *klass = NULL;
556 if (mt == MINT_TYPE_VT) {
557 klass = mono_class_from_mono_type (type);
558 gint32 size = mono_class_value_size (klass, NULL);
560 ADD_CODE(td, MINT_LDLOC_VT);
561 ADD_CODE(td, offset); /*FIX for large offset */
564 g_assert (mt < MINT_TYPE_VT);
565 if (mt == MINT_TYPE_I4 && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
566 td->last_new_ip [0] == MINT_STLOC_I4 && td->last_new_ip [1] == offset) {
567 td->last_new_ip [0] = MINT_STLOC_NP_I4;
568 } else if (mt == MINT_TYPE_O && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
569 td->last_new_ip [0] == MINT_STLOC_O && td->last_new_ip [1] == offset) {
570 td->last_new_ip [0] = MINT_STLOC_NP_O;
572 ADD_CODE(td, MINT_LDLOC_I1 + (mt - MINT_TYPE_I1));
573 ADD_CODE(td, offset); /*FIX for large offset */
575 if (mt == MINT_TYPE_O)
576 klass = mono_class_from_mono_type (type);
578 PUSH_TYPE(td, stack_type[mt], klass);
582 store_local(TransformData *td, int n)
584 MonoType *type = td->header->locals [n];
585 int mt = mint_type (type);
586 int offset = td->rtm->local_offsets [n];
588 #if SIZEOF_VOID_P == 8
589 if (td->sp [-1].type == STACK_TYPE_I4 && stack_type [mt] == STACK_TYPE_I8) {
590 ADD_CODE(td, MINT_CONV_I8_I4);
591 td->sp [-1].type = STACK_TYPE_I8;
594 if (!can_store(td->sp [-1].type, stack_type [mt])) {
595 g_warning("%s.%s: Store local stack type mismatch %d %d",
596 td->method->klass->name, td->method->name,
597 stack_type [mt], td->sp [-1].type);
599 if (mt == MINT_TYPE_VT) {
600 MonoClass *klass = mono_class_from_mono_type (type);
601 gint32 size = mono_class_value_size (klass, NULL);
602 ADD_CODE(td, MINT_STLOC_VT);
603 ADD_CODE(td, offset); /*FIX for large offset */
605 if (td->sp [-1].type == STACK_TYPE_VT)
608 g_assert (mt < MINT_TYPE_VT);
609 ADD_CODE(td, MINT_STLOC_I1 + (mt - MINT_TYPE_I1));
610 ADD_CODE(td, offset); /*FIX for large offset */
615 #define SIMPLE_OP(td, op) \
622 get_data_item_index (TransformData *td, void *ptr)
624 gpointer p = g_hash_table_lookup (td->data_hash, ptr);
627 return GPOINTER_TO_UINT (p) - 1;
628 if (td->max_data_items == td->n_data_items) {
629 td->max_data_items = td->n_data_items == 0 ? 16 : 2 * td->max_data_items;
630 td->data_items = g_realloc (td->data_items, td->max_data_items * sizeof(td->data_items [0]));
632 index = td->n_data_items;
633 td->data_items [index] = ptr;
635 g_hash_table_insert (td->data_hash, ptr, GUINT_TO_POINTER (index + 1));
640 jit_call_supported (MonoMethod *method, MonoMethodSignature *sig)
644 if (sig->param_count > 6)
648 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
650 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
652 if (method->is_inflated)
654 if (method->string_ctor)
657 for (l = jit_classes; l; l = l->next) {
658 char *class_name = l->data;
660 if (!strcmp (method->klass->name, class_name))
669 interp_transform_call (TransformData *td, MonoMethod *method, MonoMethod *target_method, MonoDomain *domain, MonoGenericContext *generic_context, unsigned char *is_bb_start, int body_start_offset, MonoClass *constrained_class, gboolean readonly)
671 MonoImage *image = method->klass->image;
672 MonoMethodSignature *csignature;
674 int virtual = *td->ip == CEE_CALLVIRT;
675 int calli = *td->ip == CEE_CALLI || *td->ip == CEE_MONO_CALLI_EXTRA_ARG;
677 guint32 vt_stack_used = 0;
678 guint32 vt_res_size = 0;
683 guint32 token = read32 (td->ip + 1);
685 if (target_method == NULL) {
688 native = (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE && td->sp [-1].type == STACK_TYPE_I);
690 if (method->wrapper_type != MONO_WRAPPER_NONE)
691 csignature = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
693 csignature = mono_metadata_parse_signature (image, token);
695 if (generic_context) {
696 csignature = mono_inflate_generic_signature (csignature, generic_context, &error);
697 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
700 target_method = NULL;
702 if (method->wrapper_type == MONO_WRAPPER_NONE)
703 target_method = mono_get_method_full (image, token, NULL, generic_context);
705 target_method = (MonoMethod *)mono_method_get_wrapper_data (method, token);
706 csignature = mono_method_signature (target_method);
707 if (target_method->klass == mono_defaults.string_class) {
708 if (target_method->name [0] == 'g') {
709 if (strcmp (target_method->name, "get_Chars") == 0)
711 else if (strcmp (target_method->name, "get_Length") == 0)
714 } else if (mono_class_is_subclass_of (target_method->klass, mono_defaults.array_class, FALSE)) {
715 if (!strcmp (target_method->name, "get_Rank")) {
716 op = MINT_ARRAY_RANK;
717 } else if (!strcmp (target_method->name, "get_Length")) {
719 } else if (!strcmp (target_method->name, "Address")) {
720 op = readonly ? MINT_LDELEMA : MINT_LDELEMA_TC;
722 } else if (target_method && generic_context) {
723 csignature = mono_inflate_generic_signature (csignature, generic_context, &error);
724 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
725 target_method = mono_class_inflate_generic_method_checked (target_method, generic_context, &error);
726 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
730 csignature = mono_method_signature (target_method);
733 if (constrained_class) {
734 if (constrained_class->enumtype && !strcmp (target_method->name, "GetHashCode")) {
735 /* Use the corresponding method from the base type to avoid boxing */
736 MonoType *base_type = mono_class_enum_basetype (constrained_class);
737 g_assert (base_type);
738 constrained_class = mono_class_from_mono_type (base_type);
739 target_method = mono_class_get_method_from_name (constrained_class, target_method->name, 0);
740 g_assert (target_method);
744 if (constrained_class) {
745 mono_class_setup_vtable (constrained_class);
747 g_print ("CONSTRAINED.CALLVIRT: %s::%s. %s (%p) ->\n", target_method->klass->name, target_method->name, mono_signature_full_name (target_method->signature), target_method);
749 target_method = mono_get_method_constrained_with_method (image, target_method, constrained_class, generic_context, &error);
751 g_print (" : %s::%s. %s (%p)\n", target_method->klass->name, target_method->name, mono_signature_full_name (target_method->signature), target_method);
753 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
754 mono_class_setup_vtable (target_method->klass);
756 if (constrained_class->valuetype && (target_method->klass == mono_defaults.object_class || target_method->klass == mono_defaults.enum_class->parent || target_method->klass == mono_defaults.enum_class)) {
757 if (target_method->klass == mono_defaults.enum_class && (td->sp - csignature->param_count - 1)->type == STACK_TYPE_MP) {
758 /* managed pointer on the stack, we need to deref that puppy */
759 ADD_CODE (td, MINT_LDIND_I);
760 ADD_CODE (td, csignature->param_count);
762 ADD_CODE (td, MINT_BOX);
763 ADD_CODE (td, get_data_item_index (td, constrained_class));
764 ADD_CODE (td, csignature->param_count);
765 } else if (!constrained_class->valuetype) {
766 /* managed pointer on the stack, we need to deref that puppy */
767 ADD_CODE (td, MINT_LDIND_I);
768 ADD_CODE (td, csignature->param_count);
770 if (target_method->klass->valuetype) {
773 /* Interface method */
776 mono_class_setup_vtable (constrained_class);
777 ioffset = mono_class_interface_offset (constrained_class, target_method->klass);
779 g_error ("type load error: constrained_class");
780 slot = mono_method_get_vtable_slot (target_method);
782 g_error ("type load error: target_method->klass");
783 target_method = constrained_class->vtable [ioffset + slot];
785 if (target_method->klass == mono_defaults.enum_class) {
786 if ((td->sp - csignature->param_count - 1)->type == STACK_TYPE_MP) {
787 /* managed pointer on the stack, we need to deref that puppy */
788 ADD_CODE (td, MINT_LDIND_I);
789 ADD_CODE (td, csignature->param_count);
791 ADD_CODE (td, MINT_BOX);
792 ADD_CODE (td, get_data_item_index (td, constrained_class));
793 ADD_CODE (td, csignature->param_count);
801 mono_class_init (target_method->klass);
803 CHECK_STACK (td, csignature->param_count + csignature->hasthis);
804 if (!calli && (!virtual || (target_method->flags & METHOD_ATTRIBUTE_VIRTUAL) == 0) &&
805 (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
806 (target_method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) == 0) {
807 int called_inited = mono_class_vtable (domain, target_method->klass)->initialized;
808 MonoMethodHeader *mheader = mono_method_get_header (target_method);
810 if (/*mono_metadata_signature_equal (method->signature, target_method->signature) */ method == target_method && *(td->ip + 5) == CEE_RET) {
812 if (mono_interp_traceopt)
813 g_print ("Optimize tail call of %s.%s\n", target_method->klass->name, target_method->name);
814 for (i = csignature->param_count - 1; i >= 0; --i)
815 store_arg (td, i + csignature->hasthis);
817 ADD_CODE(td, MINT_BR_S);
818 offset = body_start_offset - ((td->new_ip - 1) - td->new_code);
819 ADD_CODE(td, offset);
820 if (!is_bb_start [td->ip + 5 - td->il_code])
821 ++td->ip; /* gobble the CEE_RET if it isn't branched to */
825 /* mheader might not exist if this is a delegate invoc, etc */
826 if (mheader && *mheader->code == CEE_RET && called_inited) {
827 if (mono_interp_traceopt)
828 g_print ("Inline (empty) call of %s.%s\n", target_method->klass->name, target_method->name);
829 for (i = 0; i < csignature->param_count; i++)
830 ADD_CODE (td, MINT_POP); /*FIX: vt */
832 if (csignature->hasthis) {
834 ADD_CODE(td, MINT_CKNULL);
835 ADD_CODE (td, MINT_POP);
838 td->sp -= csignature->param_count + csignature->hasthis;
844 if (method->wrapper_type == MONO_WRAPPER_NONE && target_method != NULL) {
845 if (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
846 target_method = mono_marshal_get_native_wrapper (target_method, FALSE, FALSE);
847 if (!virtual && target_method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
848 target_method = mono_marshal_get_synchronized_wrapper (target_method);
850 g_assert (csignature->call_convention == MONO_CALL_DEFAULT || csignature->call_convention == MONO_CALL_C);
851 td->sp -= csignature->param_count + !!csignature->hasthis;
852 for (i = 0; i < csignature->param_count; ++i) {
853 if (td->sp [i + !!csignature->hasthis].type == STACK_TYPE_VT) {
855 MonoClass *klass = mono_class_from_mono_type (csignature->params [i]);
856 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
857 size = mono_class_native_size (klass, NULL);
859 size = mono_class_value_size (klass, NULL);
860 size = (size + 7) & ~7;
861 vt_stack_used += size;
865 /* need to handle typedbyref ... */
866 if (csignature->ret->type != MONO_TYPE_VOID) {
867 int mt = mint_type(csignature->ret);
868 MonoClass *klass = mono_class_from_mono_type (csignature->ret);
869 if (mt == MINT_TYPE_VT) {
870 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
871 vt_res_size = mono_class_native_size (klass, NULL);
873 vt_res_size = mono_class_value_size (klass, NULL);
874 PUSH_VT(td, vt_res_size);
876 PUSH_TYPE(td, stack_type[mt], klass);
882 #if SIZEOF_VOID_P == 8
883 if (op == MINT_LDLEN)
884 ADD_CODE (td, MINT_CONV_I4_I8);
886 if (op == MINT_LDELEMA || op == MINT_LDELEMA_TC) {
887 ADD_CODE (td, get_data_item_index (td, target_method->klass));
888 ADD_CODE (td, 1 + target_method->klass->rank);
890 } else if (!calli && !virtual && jit_call_supported (target_method, csignature)) {
891 ADD_CODE(td, MINT_JIT_CALL);
892 ADD_CODE(td, get_data_item_index (td, (void *)mono_interp_get_runtime_method (domain, target_method, &error)));
893 mono_error_assert_ok (&error);
896 ADD_CODE(td, native ? MINT_CALLI_NAT : MINT_CALLI);
898 ADD_CODE(td, is_void ? MINT_VCALLVIRT : MINT_CALLVIRT);
900 ADD_CODE(td, is_void ? MINT_VCALL : MINT_CALL);
903 ADD_CODE(td, get_data_item_index (td, (void *)csignature));
905 ADD_CODE(td, get_data_item_index (td, (void *)mono_interp_get_runtime_method (domain, target_method, &error)));
906 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
910 if (vt_stack_used != 0 || vt_res_size != 0) {
911 ADD_CODE(td, MINT_VTRESULT);
912 ADD_CODE(td, vt_res_size);
913 WRITE32(td, &vt_stack_used);
914 td->vt_sp -= vt_stack_used;
919 generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, MonoGenericContext *generic_context)
921 MonoMethodHeader *header = mono_method_get_header (method);
922 MonoMethodSignature *signature = mono_method_signature (method);
923 MonoImage *image = method->klass->image;
924 MonoDomain *domain = mono_domain_get ();
925 MonoClass *constrained_class = NULL;
927 int offset, mt, i, i32;
928 gboolean readonly = FALSE;
930 MonoClassField *field;
931 const unsigned char *end;
932 int new_in_start_offset;
933 int body_start_offset;
937 int generating_code = 1;
939 memset(&td, 0, sizeof(td));
942 td.is_bb_start = is_bb_start;
943 td.il_code = header->code;
944 td.code_size = header->code_size;
946 td.max_code_size = td.code_size;
947 td.new_code = (unsigned short *)g_malloc(td.max_code_size * sizeof(gushort));
948 td.new_code_end = td.new_code + td.max_code_size;
949 td.in_offsets = g_malloc0(header->code_size * sizeof(int));
950 td.forward_refs = g_malloc(header->code_size * sizeof(int));
951 td.stack_state = g_malloc0(header->code_size * sizeof(StackInfo *));
952 td.stack_height = g_malloc(header->code_size * sizeof(int));
953 td.vt_stack_size = g_malloc(header->code_size * sizeof(int));
955 td.max_data_items = 0;
956 td.data_items = NULL;
957 td.data_hash = g_hash_table_new (NULL, NULL);
958 rtm->data_items = td.data_items;
959 for (i = 0; i < header->code_size; i++) {
960 td.forward_refs [i] = -1;
961 td.stack_height [i] = -1;
963 td.new_ip = td.new_code;
964 td.last_new_ip = NULL;
966 td.stack = g_malloc0 ((header->max_stack + 1) * sizeof (td.stack [0]));
968 td.max_stack_height = 0;
970 for (i = 0; i < header->num_clauses; i++) {
971 MonoExceptionClause *c = header->clauses + i;
972 td.stack_height [c->handler_offset] = 0;
973 td.vt_stack_size [c->handler_offset] = 0;
974 td.is_bb_start [c->handler_offset] = 1;
976 td.stack_height [c->handler_offset] = 1;
977 td.stack_state [c->handler_offset] = g_malloc0(sizeof(StackInfo));
978 td.stack_state [c->handler_offset][0].type = STACK_TYPE_O;
979 td.stack_state [c->handler_offset][0].klass = NULL; /*FIX*/
982 td.ip = header->code;
983 end = td.ip + header->code_size;
985 if (mono_interp_traceopt) {
986 char *tmp = mono_disasm_code (NULL, method, td.ip, end);
987 char *name = mono_method_full_name (method, TRUE);
988 g_print ("Method %s, original code:\n", name);
989 g_print ("%s\n", tmp);
994 if (signature->hasthis)
995 store_inarg (&td, 0);
996 for (i = 0; i < signature->param_count; i++)
997 store_inarg (&td, i + !!signature->hasthis);
999 body_start_offset = td.new_ip - td.new_code;
1001 for (i = 0; i < header->num_locals; i++) {
1002 int mt = mint_type(header->locals [i]);
1003 if (mt == MINT_TYPE_VT || mt == MINT_TYPE_O || mt == MINT_TYPE_P) {
1004 ADD_CODE(&td, MINT_INITLOCALS);
1009 while (td.ip < end) {
1012 g_assert (td.sp >= td.stack);
1013 g_assert (td.vt_sp < 0x10000000);
1014 in_offset = td.ip - header->code;
1015 td.in_offsets [in_offset] = td.new_ip - td.new_code;
1016 new_in_start_offset = td.new_ip - td.new_code;
1017 td.in_start = td.ip;
1018 while (td.forward_refs [in_offset] >= 0) {
1019 int j = td.forward_refs [in_offset];
1021 td.forward_refs [in_offset] = td.forward_refs [j];
1022 if (td.in_offsets [j] < 0) {
1023 int old_switch_offset = -td.in_offsets [j];
1024 int new_switch_offset = td.in_offsets [old_switch_offset];
1025 int switch_case = (j - old_switch_offset - 5) / 4;
1026 int n_cases = read32 (header->code + old_switch_offset + 1);
1027 offset = (td.new_ip - td.new_code) - (new_switch_offset + 2 * n_cases + 3);
1028 slot = new_switch_offset + 3 + 2 * switch_case;
1029 td.new_code [slot] = * (unsigned short *)(&offset);
1030 td.new_code [slot + 1] = * ((unsigned short *)&offset + 1);
1032 int op = td.new_code [td.in_offsets [j]];
1033 if (mono_interp_opargtype [op] == MintOpShortBranch) {
1034 offset = (td.new_ip - td.new_code) - td.in_offsets [j];
1035 g_assert (offset <= 32767);
1036 slot = td.in_offsets [j] + 1;
1037 td.new_code [slot] = offset;
1039 offset = (td.new_ip - td.new_code) - td.in_offsets [j];
1040 slot = td.in_offsets [j] + 1;
1041 td.new_code [slot] = * (unsigned short *)(&offset);
1042 td.new_code [slot + 1] = * ((unsigned short *)&offset + 1);
1046 if (td.stack_height [in_offset] >= 0) {
1047 g_assert (is_bb_start [in_offset]);
1048 if (td.stack_height [in_offset] > 0)
1049 memcpy (td.stack, td.stack_state [in_offset], td.stack_height [in_offset] * sizeof(td.stack [0]));
1050 td.sp = td.stack + td.stack_height [in_offset];
1051 td.vt_sp = td.vt_stack_size [in_offset];
1053 if (is_bb_start [in_offset]) {
1054 generating_code = 1;
1056 if (!generating_code) {
1057 while (td.ip < end && !is_bb_start [td.ip - td.il_code])
1061 if (mono_interp_traceopt > 1) {
1062 printf("IL_%04lx %s %-10s -> IL_%04lx, sp %ld, %s %-12s vt_sp %u (max %u)\n",
1064 td.is_bb_start [td.ip - td.il_code] == 3 ? "<>" :
1065 td.is_bb_start [td.ip - td.il_code] == 2 ? "< " :
1066 td.is_bb_start [td.ip - td.il_code] == 1 ? " >" : " ",
1067 mono_opcode_name (*td.ip), td.new_ip - td.new_code, td.sp - td.stack,
1068 td.sp > td.stack ? stack_type_string [td.sp [-1].type] : " ",
1069 (td.sp > td.stack && (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_VT)) ? (td.sp [-1].klass == NULL ? "?" : td.sp [-1].klass->name) : "",
1070 td.vt_sp, td.max_vt_sp);
1078 SIMPLE_OP(td, MINT_BREAK);
1084 load_arg (&td, *td.ip - CEE_LDARG_0);
1091 load_local (&td, *td.ip - CEE_LDLOC_0);
1098 store_local (&td, *td.ip - CEE_STLOC_0);
1102 load_arg (&td, ((guint8 *)td.ip)[1]);
1105 case CEE_LDARGA_S: {
1106 /* NOTE: n includes this */
1107 int n = ((guint8 *) td.ip) [1];
1108 ADD_CODE (&td, MINT_LDARGA);
1109 ADD_CODE (&td, td.rtm->arg_offsets [n]);
1110 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
1115 store_arg (&td, ((guint8 *)td.ip)[1]);
1119 load_local (&td, ((guint8 *)td.ip)[1]);
1123 ADD_CODE(&td, MINT_LDLOCA_S);
1124 ADD_CODE(&td, td.rtm->local_offsets [((guint8 *)td.ip)[1]]);
1125 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
1129 store_local (&td, ((guint8 *)td.ip)[1]);
1133 SIMPLE_OP(td, MINT_LDNULL);
1134 PUSH_TYPE(&td, STACK_TYPE_O, NULL);
1137 SIMPLE_OP(td, MINT_LDC_I4_M1);
1138 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1141 if (!td.is_bb_start[td.ip + 1 - td.il_code] && td.ip [1] == 0xfe && td.ip [2] == CEE_CEQ &&
1142 td.sp > td.stack && td.sp [-1].type == STACK_TYPE_I4) {
1143 SIMPLE_OP(td, MINT_CEQ0_I4);
1146 SIMPLE_OP(td, MINT_LDC_I4_0);
1147 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1151 if (!td.is_bb_start[td.ip + 1 - td.il_code] &&
1152 (td.ip [1] == CEE_ADD || td.ip [1] == CEE_SUB) && td.sp [-1].type == STACK_TYPE_I4) {
1153 ADD_CODE(&td, td.ip [1] == CEE_ADD ? MINT_ADD1_I4 : MINT_SUB1_I4);
1156 SIMPLE_OP(td, MINT_LDC_I4_1);
1157 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1167 SIMPLE_OP(td, (*td.ip - CEE_LDC_I4_0) + MINT_LDC_I4_0);
1168 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1171 ADD_CODE(&td, MINT_LDC_I4_S);
1172 ADD_CODE(&td, ((gint8 *) td.ip) [1]);
1174 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1177 i32 = read32 (td.ip + 1);
1178 ADD_CODE(&td, MINT_LDC_I4);
1181 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1184 gint64 val = read64 (td.ip + 1);
1185 ADD_CODE(&td, MINT_LDC_I8);
1188 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I8);
1193 readr4 (td.ip + 1, &val);
1194 ADD_CODE(&td, MINT_LDC_R4);
1197 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_R8);
1202 readr8 (td.ip + 1, &val);
1203 ADD_CODE(&td, MINT_LDC_R8);
1206 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_R8);
1210 int type = td.sp [-1].type;
1211 MonoClass *klass = td.sp [-1].klass;
1212 if (td.sp [-1].type == STACK_TYPE_VT) {
1213 gint32 size = mono_class_value_size (klass, NULL);
1215 ADD_CODE(&td, MINT_DUP_VT);
1216 WRITE32(&td, &size);
1219 SIMPLE_OP(td, MINT_DUP);
1220 PUSH_TYPE(&td, type, klass);
1224 CHECK_STACK(&td, 1);
1225 SIMPLE_OP(td, MINT_POP);
1227 if (td.sp [-1].type == STACK_TYPE_VT) {
1228 int size = mono_class_value_size (td.sp [-1].klass, NULL);
1229 size = (size + 7) & ~7;
1230 ADD_CODE(&td, MINT_VTRESULT);
1232 WRITE32(&td, &size);
1239 if (td.sp > td.stack)
1240 g_warning ("CEE_JMP: stack must be empty");
1241 token = read32 (td.ip + 1);
1242 m = mono_get_method_full (image, token, NULL, generic_context);
1243 ADD_CODE (&td, MINT_JMP);
1244 ADD_CODE (&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
1245 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1249 case CEE_CALLVIRT: /* Fall through */
1250 case CEE_CALLI: /* Fall through */
1252 interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, constrained_class, readonly);
1253 constrained_class = NULL;
1259 if (signature->ret->type != MONO_TYPE_VOID) {
1261 MonoClass *klass = mono_class_from_mono_type (signature->ret);
1262 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1263 vt_size = mono_class_value_size (klass, NULL);
1264 vt_size = (vt_size + 7) & ~7;
1267 if (td.sp > td.stack)
1268 g_warning ("%s.%s: CEE_RET: more values on stack: %d", td.method->klass->name, td.method->name, td.sp - td.stack);
1269 if (td.vt_sp != vt_size)
1270 g_error ("%s.%s: CEE_RET: value type stack: %d vs. %d", td.method->klass->name, td.method->name, td.vt_sp, vt_size);
1272 SIMPLE_OP(td, signature->ret->type == MONO_TYPE_VOID ? MINT_RET_VOID : MINT_RET);
1274 ADD_CODE(&td, MINT_RET_VT);
1275 WRITE32(&td, &vt_size);
1278 generating_code = 0;
1282 handle_branch (&td, MINT_BR_S, MINT_BR, 5 + read32 (td.ip + 1));
1284 generating_code = 0;
1287 handle_branch (&td, MINT_BR_S, MINT_BR, 2 + (gint8)td.ip [1]);
1289 generating_code = 0;
1292 one_arg_branch (&td, MINT_BRFALSE_I4, 5 + read32 (td.ip + 1));
1296 one_arg_branch (&td, MINT_BRFALSE_I4, 2 + (gint8)td.ip [1]);
1300 one_arg_branch (&td, MINT_BRTRUE_I4, 5 + read32 (td.ip + 1));
1304 one_arg_branch (&td, MINT_BRTRUE_I4, 2 + (gint8)td.ip [1]);
1308 two_arg_branch (&td, MINT_BEQ_I4, 5 + read32 (td.ip + 1));
1312 two_arg_branch (&td, MINT_BEQ_I4, 2 + (gint8) td.ip [1]);
1316 two_arg_branch (&td, MINT_BGE_I4, 5 + read32 (td.ip + 1));
1320 two_arg_branch (&td, MINT_BGE_I4, 2 + (gint8) td.ip [1]);
1324 two_arg_branch (&td, MINT_BGT_I4, 5 + read32 (td.ip + 1));
1328 two_arg_branch (&td, MINT_BGT_I4, 2 + (gint8) td.ip [1]);
1332 two_arg_branch (&td, MINT_BLT_I4, 5 + read32 (td.ip + 1));
1336 two_arg_branch (&td, MINT_BLT_I4, 2 + (gint8) td.ip [1]);
1340 two_arg_branch (&td, MINT_BLE_I4, 5 + read32 (td.ip + 1));
1344 two_arg_branch (&td, MINT_BLE_I4, 2 + (gint8) td.ip [1]);
1348 two_arg_branch (&td, MINT_BNE_UN_I4, 5 + read32 (td.ip + 1));
1352 two_arg_branch (&td, MINT_BNE_UN_I4, 2 + (gint8) td.ip [1]);
1356 two_arg_branch (&td, MINT_BGE_UN_I4, 5 + read32 (td.ip + 1));
1360 two_arg_branch (&td, MINT_BGE_UN_I4, 2 + (gint8) td.ip [1]);
1364 two_arg_branch (&td, MINT_BGT_UN_I4, 5 + read32 (td.ip + 1));
1368 two_arg_branch (&td, MINT_BGT_UN_I4, 2 + (gint8) td.ip [1]);
1372 two_arg_branch (&td, MINT_BLE_UN_I4, 5 + read32 (td.ip + 1));
1376 two_arg_branch (&td, MINT_BLE_UN_I4, 2 + (gint8) td.ip [1]);
1380 two_arg_branch (&td, MINT_BLT_UN_I4, 5 + read32 (td.ip + 1));
1384 two_arg_branch (&td, MINT_BLT_UN_I4, 2 + (gint8) td.ip [1]);
1389 const unsigned char *next_ip;
1390 const unsigned char *base_ip = td.ip;
1391 unsigned short *next_new_ip;
1394 ADD_CODE (&td, MINT_SWITCH);
1397 next_ip = td.ip + n * 4;
1398 next_new_ip = td.new_ip + n * 2;
1400 int stack_height = td.sp - td.stack;
1401 for (i = 0; i < n; i++) {
1402 offset = read32 (td.ip);
1403 target = next_ip - td.il_code + offset;
1406 if (stack_height > 0 && stack_height != td.stack_height [target])
1407 g_warning ("SWITCH with back branch and non-empty stack");
1409 target = td.in_offsets [target] - (next_new_ip - td.new_code);
1411 td.stack_height [target] = stack_height;
1412 td.vt_stack_size [target] = td.vt_sp;
1413 if (stack_height > 0)
1414 td.stack_state [target] = g_memdup (td.stack, stack_height * sizeof (td.stack [0]));
1415 int prev = td.forward_refs [target];
1416 td.forward_refs [td.ip - td.il_code] = prev;
1417 td.forward_refs [target] = td.ip - td.il_code;
1418 td.in_offsets [td.ip - td.il_code] = - (base_ip - td.il_code);
1420 WRITE32 (&td, &target);
1426 CHECK_STACK (&td, 1);
1427 SIMPLE_OP (td, MINT_LDIND_I1);
1428 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1431 CHECK_STACK (&td, 1);
1432 SIMPLE_OP (td, MINT_LDIND_U1);
1433 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1436 CHECK_STACK (&td, 1);
1437 SIMPLE_OP (td, MINT_LDIND_I2);
1438 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1441 CHECK_STACK (&td, 1);
1442 SIMPLE_OP (td, MINT_LDIND_U2);
1443 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1446 CHECK_STACK (&td, 1);
1447 SIMPLE_OP (td, MINT_LDIND_I4);
1448 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1451 CHECK_STACK (&td, 1);
1452 SIMPLE_OP (td, MINT_LDIND_U4);
1453 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1456 CHECK_STACK (&td, 1);
1457 SIMPLE_OP (td, MINT_LDIND_I8);
1458 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1461 CHECK_STACK (&td, 1);
1462 SIMPLE_OP (td, MINT_LDIND_I);
1464 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1467 CHECK_STACK (&td, 1);
1468 SIMPLE_OP (td, MINT_LDIND_R4);
1469 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1472 CHECK_STACK (&td, 1);
1473 SIMPLE_OP (td, MINT_LDIND_R8);
1474 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1477 CHECK_STACK (&td, 1);
1478 SIMPLE_OP (td, MINT_LDIND_REF);
1479 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
1482 CHECK_STACK (&td, 2);
1483 SIMPLE_OP (td, MINT_STIND_REF);
1487 CHECK_STACK (&td, 2);
1488 SIMPLE_OP (td, MINT_STIND_I1);
1492 CHECK_STACK (&td, 2);
1493 SIMPLE_OP (td, MINT_STIND_I2);
1497 CHECK_STACK (&td, 2);
1498 SIMPLE_OP (td, MINT_STIND_I4);
1502 CHECK_STACK (&td, 2);
1503 SIMPLE_OP (td, MINT_STIND_I);
1507 CHECK_STACK (&td, 2);
1508 SIMPLE_OP (td, MINT_STIND_I8);
1512 CHECK_STACK (&td, 2);
1513 SIMPLE_OP (td, MINT_STIND_R4);
1517 CHECK_STACK (&td, 2);
1518 SIMPLE_OP (td, MINT_STIND_R8);
1522 binary_arith_op(&td, MINT_ADD_I4);
1526 binary_arith_op(&td, MINT_SUB_I4);
1530 binary_arith_op(&td, MINT_MUL_I4);
1534 binary_arith_op(&td, MINT_DIV_I4);
1538 binary_arith_op(&td, MINT_DIV_UN_I4);
1542 binary_int_op (&td, MINT_REM_I4);
1546 binary_int_op (&td, MINT_REM_UN_I4);
1550 binary_int_op (&td, MINT_AND_I4);
1554 binary_int_op (&td, MINT_OR_I4);
1558 binary_int_op (&td, MINT_XOR_I4);
1562 shift_op (&td, MINT_SHL_I4);
1566 shift_op (&td, MINT_SHR_I4);
1570 shift_op (&td, MINT_SHR_UN_I4);
1574 unary_arith_op (&td, MINT_NEG_I4);
1578 unary_arith_op (&td, MINT_NOT_I4);
1582 CHECK_STACK (&td, 1);
1583 switch (td.sp [-1].type) {
1585 ADD_CODE(&td, MINT_CONV_U1_R8);
1588 ADD_CODE(&td, MINT_CONV_U1_I4);
1591 ADD_CODE(&td, MINT_CONV_U1_I8);
1594 g_assert_not_reached ();
1597 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1600 CHECK_STACK (&td, 1);
1601 switch (td.sp [-1].type) {
1603 ADD_CODE(&td, MINT_CONV_I1_R8);
1606 ADD_CODE(&td, MINT_CONV_I1_I4);
1609 ADD_CODE(&td, MINT_CONV_I1_I8);
1612 g_assert_not_reached ();
1615 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1618 CHECK_STACK (&td, 1);
1619 switch (td.sp [-1].type) {
1621 ADD_CODE(&td, MINT_CONV_U2_R8);
1624 ADD_CODE(&td, MINT_CONV_U2_I4);
1627 ADD_CODE(&td, MINT_CONV_U2_I8);
1630 g_assert_not_reached ();
1633 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1636 CHECK_STACK (&td, 1);
1637 switch (td.sp [-1].type) {
1639 ADD_CODE(&td, MINT_CONV_I2_R8);
1642 ADD_CODE(&td, MINT_CONV_I2_I4);
1645 ADD_CODE(&td, MINT_CONV_I2_I8);
1648 g_assert_not_reached ();
1651 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1654 CHECK_STACK (&td, 1);
1655 switch (td.sp [-1].type) {
1657 #if SIZEOF_VOID_P == 4
1658 ADD_CODE(&td, MINT_CONV_U4_R8);
1660 ADD_CODE(&td, MINT_CONV_U8_R8);
1664 #if SIZEOF_VOID_P == 8
1665 ADD_CODE(&td, MINT_CONV_U8_I4);
1669 #if SIZEOF_VOID_P == 4
1670 ADD_CODE(&td, MINT_CONV_U4_I8);
1676 g_assert_not_reached ();
1679 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1682 CHECK_STACK (&td, 1);
1683 switch (td.sp [-1].type) {
1685 #if SIZEOF_VOID_P == 8
1686 ADD_CODE(&td, MINT_CONV_I8_R8);
1688 ADD_CODE(&td, MINT_CONV_I4_R8);
1692 #if SIZEOF_VOID_P == 8
1693 ADD_CODE(&td, MINT_CONV_I8_I4);
1701 #if SIZEOF_VOID_P == 4
1702 ADD_CODE(&td, MINT_CONV_I4_I8);
1706 g_assert_not_reached ();
1709 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1712 CHECK_STACK (&td, 1);
1713 switch (td.sp [-1].type) {
1715 ADD_CODE(&td, MINT_CONV_U4_R8);
1720 ADD_CODE(&td, MINT_CONV_U4_I8);
1723 #if SIZEOF_VOID_P == 8
1724 ADD_CODE(&td, MINT_CONV_U4_I8);
1728 g_assert_not_reached ();
1731 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1734 CHECK_STACK (&td, 1);
1735 switch (td.sp [-1].type) {
1737 ADD_CODE(&td, MINT_CONV_I4_R8);
1742 ADD_CODE(&td, MINT_CONV_I4_I8);
1745 #if SIZEOF_VOID_P == 8
1746 ADD_CODE(&td, MINT_CONV_I4_I8);
1750 g_assert_not_reached ();
1753 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1756 CHECK_STACK (&td, 1);
1757 switch (td.sp [-1].type) {
1759 ADD_CODE(&td, MINT_CONV_I8_R8);
1762 ADD_CODE(&td, MINT_CONV_I8_I4);
1767 #if SIZEOF_VOID_P == 4
1768 ADD_CODE(&td, MINT_CONV_I8_I4);
1772 g_assert_not_reached ();
1775 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1778 CHECK_STACK (&td, 1);
1779 switch (td.sp [-1].type) {
1781 ADD_CODE(&td, MINT_CONV_R4_R8);
1784 ADD_CODE(&td, MINT_CONV_R4_I8);
1787 ADD_CODE(&td, MINT_CONV_R4_I4);
1790 g_assert_not_reached ();
1793 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1796 CHECK_STACK (&td, 1);
1797 switch (td.sp [-1].type) {
1799 ADD_CODE(&td, MINT_CONV_R8_I4);
1802 ADD_CODE(&td, MINT_CONV_R8_I8);
1807 g_assert_not_reached ();
1810 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1813 CHECK_STACK (&td, 1);
1814 switch (td.sp [-1].type) {
1816 ADD_CODE(&td, MINT_CONV_U8_I4);
1821 ADD_CODE(&td, MINT_CONV_U8_R8);
1824 #if SIZEOF_VOID_P == 4
1825 ADD_CODE(&td, MINT_CONV_U8_I4);
1829 g_assert_not_reached ();
1832 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1835 CHECK_STACK (&td, 2);
1837 token = read32 (td.ip + 1);
1838 klass = mono_class_get_full (image, token, generic_context);
1840 if (klass->valuetype) {
1841 ADD_CODE (&td, MINT_CPOBJ);
1842 ADD_CODE (&td, get_data_item_index(&td, klass));
1844 ADD_CODE (&td, MINT_LDIND_REF);
1845 ADD_CODE (&td, MINT_STIND_REF);
1853 CHECK_STACK (&td, 1);
1855 token = read32 (td.ip + 1);
1857 if (method->wrapper_type != MONO_WRAPPER_NONE)
1858 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
1860 klass = mono_class_get_full (image, token, generic_context);
1862 ADD_CODE(&td, MINT_LDOBJ);
1863 ADD_CODE(&td, get_data_item_index(&td, klass));
1864 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1865 size = mono_class_value_size (klass, NULL);
1869 SET_TYPE(td.sp - 1, stack_type[mint_type(&klass->byval_arg)], klass);
1874 token = mono_metadata_token_index (read32 (td.ip + 1));
1876 if (method->wrapper_type != MONO_WRAPPER_NONE) {
1877 s = mono_string_new_wrapper(
1878 mono_method_get_wrapper_data (method, token));
1881 s = mono_ldstr (domain, image, token);
1882 ADD_CODE(&td, MINT_LDSTR);
1883 ADD_CODE(&td, get_data_item_index (&td, s));
1884 PUSH_TYPE(&td, STACK_TYPE_O, mono_defaults.string_class);
1889 MonoMethodSignature *csignature;
1890 guint32 vt_stack_used = 0;
1891 guint32 vt_res_size = 0;
1894 token = read32 (td.ip);
1897 if (method->wrapper_type != MONO_WRAPPER_NONE)
1898 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
1900 m = mono_get_method_full (image, token, NULL, generic_context);
1902 csignature = mono_method_signature (m);
1904 td.sp -= csignature->param_count;
1905 ADD_CODE(&td, MINT_NEWOBJ);
1906 ADD_CODE(&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
1907 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1909 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1910 vt_res_size = mono_class_value_size (klass, NULL);
1911 PUSH_VT (&td, vt_res_size);
1913 for (i = 0; i < csignature->param_count; ++i) {
1914 int mt = mint_type(csignature->params [i]);
1915 if (mt == MINT_TYPE_VT) {
1916 MonoClass *k = mono_class_from_mono_type (csignature->params [i]);
1917 gint32 size = mono_class_value_size (k, NULL);
1918 size = (size + 7) & ~7;
1919 vt_stack_used += size;
1922 if (vt_stack_used != 0 || vt_res_size != 0) {
1923 ADD_CODE(&td, MINT_VTRESULT);
1924 ADD_CODE(&td, vt_res_size);
1925 WRITE32(&td, &vt_stack_used);
1926 td.vt_sp -= vt_stack_used;
1928 PUSH_TYPE (&td, stack_type [mint_type (&klass->byval_arg)], klass);
1932 CHECK_STACK (&td, 1);
1933 token = read32 (td.ip + 1);
1934 klass = mono_class_get_full (image, token, generic_context);
1935 ADD_CODE(&td, MINT_CASTCLASS);
1936 ADD_CODE(&td, get_data_item_index (&td, klass));
1937 td.sp [-1].klass = klass;
1941 CHECK_STACK (&td, 1);
1942 token = read32 (td.ip + 1);
1943 klass = mono_class_get_full (image, token, generic_context);
1944 ADD_CODE(&td, MINT_ISINST);
1945 ADD_CODE(&td, get_data_item_index (&td, klass));
1949 switch (td.sp [-1].type) {
1953 ADD_CODE(&td, MINT_CONV_R_UN_I8);
1956 ADD_CODE(&td, MINT_CONV_R_UN_I4);
1959 g_assert_not_reached ();
1961 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1965 CHECK_STACK (&td, 1);
1966 token = read32 (td.ip + 1);
1968 if (method->wrapper_type != MONO_WRAPPER_NONE)
1969 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
1971 klass = mono_class_get_full (image, token, generic_context);
1973 if (mono_class_is_nullable (klass)) {
1974 g_error ("cee_unbox: implement Nullable");
1977 ADD_CODE(&td, MINT_UNBOX);
1978 ADD_CODE(&td, get_data_item_index (&td, klass));
1979 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
1983 CHECK_STACK (&td, 1);
1984 token = read32 (td.ip + 1);
1986 g_assert (method->wrapper_type == MONO_WRAPPER_NONE);
1987 klass = mono_class_get_full (image, token, generic_context);
1989 if (mini_type_is_reference (&klass->byval_arg)) {
1990 ADD_CODE (&td, MINT_CASTCLASS);
1991 ADD_CODE (&td, get_data_item_index (&td, klass));
1992 SET_TYPE (td.sp - 1, stack_type [mt], klass);
1994 } else if (mono_class_is_nullable (klass)) {
1995 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Unbox", 1);
1996 /* td.ip is incremented by interp_transform_call */
1997 interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
1999 int mt = mint_type (&klass->byval_arg);
2000 ADD_CODE (&td, MINT_UNBOX);
2001 ADD_CODE (&td, get_data_item_index (&td, klass));
2003 ADD_CODE (&td, MINT_LDOBJ);
2004 ADD_CODE (&td, get_data_item_index(&td, klass));
2005 SET_TYPE (td.sp - 1, stack_type [mt], klass);
2007 if (mt == MINT_TYPE_VT) {
2008 int size = mono_class_value_size (klass, NULL);
2009 PUSH_VT (&td, size);
2016 CHECK_STACK (&td, 1);
2017 SIMPLE_OP (td, MINT_THROW);
2019 generating_code = 0;
2022 CHECK_STACK (&td, 1);
2023 token = read32 (td.ip + 1);
2024 field = mono_field_from_token (image, token, &klass, generic_context);
2025 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2026 mono_class_init (klass);
2028 ADD_CODE (&td, MINT_POP);
2030 ADD_CODE (&td, MINT_LDSFLDA);
2031 ADD_CODE (&td, get_data_item_index (&td, field));
2033 if ((td.sp - 1)->type == STACK_TYPE_O) {
2034 ADD_CODE (&td, MINT_LDFLDA);
2036 g_assert ((td.sp -1)->type == STACK_TYPE_MP);
2037 ADD_CODE (&td, MINT_LDFLDA_UNSAFE);
2039 ADD_CODE (&td, klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
2042 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2045 CHECK_STACK (&td, 1);
2046 token = read32 (td.ip + 1);
2047 field = mono_field_from_token (image, token, &klass, generic_context);
2048 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2049 mono_class_init (klass);
2051 MonoClass *field_klass = mono_class_from_mono_type (field->type);
2052 mt = mint_type (&field_klass->byval_arg);
2053 if (klass->marshalbyref) {
2054 g_assert (!is_static);
2055 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_LDRMFLD_VT : MINT_LDRMFLD);
2056 ADD_CODE(&td, get_data_item_index (&td, field));
2059 ADD_CODE (&td, MINT_POP);
2061 ADD_CODE (&td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2062 ADD_CODE (&td, get_data_item_index (&td, field));
2064 ADD_CODE (&td, MINT_LDFLD_I1 + mt - MINT_TYPE_I1);
2065 ADD_CODE (&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
2068 if (mt == MINT_TYPE_VT) {
2069 int size = mono_class_value_size (field_klass, NULL);
2071 WRITE32(&td, &size);
2073 if (td.sp [-1].type == STACK_TYPE_VT) {
2074 int size = mono_class_value_size (klass, NULL);
2075 size = (size + 7) & ~7;
2077 ADD_CODE (&td, MINT_VTRESULT);
2079 WRITE32 (&td, &size);
2082 SET_TYPE(td.sp - 1, stack_type [mt], field_klass);
2086 CHECK_STACK (&td, 2);
2087 token = read32 (td.ip + 1);
2088 field = mono_field_from_token (image, token, &klass, generic_context);
2089 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2090 mono_class_init (klass);
2091 mt = mint_type(field->type);
2093 if (klass->marshalbyref) {
2094 g_assert (!is_static);
2095 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STRMFLD_VT : MINT_STRMFLD);
2096 ADD_CODE(&td, get_data_item_index (&td, field));
2099 ADD_CODE (&td, MINT_POP);
2101 ADD_CODE (&td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
2102 ADD_CODE (&td, get_data_item_index (&td, field));
2104 ADD_CODE (&td, MINT_STFLD_I1 + mt - MINT_TYPE_I1);
2105 ADD_CODE (&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
2108 if (mt == MINT_TYPE_VT) {
2109 MonoClass *klass = mono_class_from_mono_type (field->type);
2110 int size = mono_class_value_size (klass, NULL);
2112 WRITE32(&td, &size);
2119 token = read32 (td.ip + 1);
2120 field = mono_field_from_token (image, token, &klass, generic_context);
2121 ADD_CODE(&td, MINT_LDSFLDA);
2122 ADD_CODE(&td, get_data_item_index (&td, field));
2124 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
2127 token = read32 (td.ip + 1);
2128 field = mono_field_from_token (image, token, &klass, generic_context);
2129 mt = mint_type(field->type);
2130 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2131 ADD_CODE(&td, get_data_item_index (&td, field));
2133 if (mt == MINT_TYPE_VT) {
2134 MonoClass *klass = mono_class_from_mono_type (field->type);
2135 int size = mono_class_value_size (klass, NULL);
2137 WRITE32(&td, &size);
2138 klass = field->type->data.klass;
2140 if (mt == MINT_TYPE_O)
2141 klass = mono_class_from_mono_type (field->type);
2144 PUSH_TYPE(&td, stack_type [mt], klass);
2147 CHECK_STACK (&td, 1);
2148 token = read32 (td.ip + 1);
2149 field = mono_field_from_token (image, token, &klass, generic_context);
2150 mt = mint_type(field->type);
2151 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
2152 ADD_CODE(&td, get_data_item_index (&td, field));
2153 if (mt == MINT_TYPE_VT) {
2154 MonoClass *klass = mono_class_from_mono_type (field->type);
2155 int size = mono_class_value_size (klass, NULL);
2157 WRITE32 (&td, &size);
2164 token = read32 (td.ip + 1);
2166 if (method->wrapper_type != MONO_WRAPPER_NONE)
2167 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2169 klass = mono_class_get_full (image, token, generic_context);
2171 ADD_CODE(&td, td.sp [-1].type == STACK_TYPE_VT ? MINT_STOBJ_VT : MINT_STOBJ);
2172 ADD_CODE(&td, get_data_item_index (&td, klass));
2173 if (td.sp [-1].type == STACK_TYPE_VT) {
2174 size = mono_class_value_size (klass, NULL);
2175 size = (size + 7) & ~7;
2182 case CEE_CONV_OVF_I_UN:
2183 case CEE_CONV_OVF_U_UN:
2184 CHECK_STACK (&td, 1);
2185 switch (td.sp [-1].type) {
2187 #if SIZEOF_VOID_P == 8
2188 ADD_CODE(&td, MINT_CONV_OVF_I8_UN_R8);
2190 ADD_CODE(&td, MINT_CONV_OVF_I4_UN_R8);
2194 #if SIZEOF_VOID_P == 4
2195 ADD_CODE (&td, MINT_CONV_OVF_I4_UN_I8);
2199 #if SIZEOF_VOID_P == 8
2200 ADD_CODE(&td, MINT_CONV_I8_U4);
2204 g_assert_not_reached ();
2207 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2210 case CEE_CONV_OVF_I8_UN:
2211 case CEE_CONV_OVF_U8_UN:
2212 CHECK_STACK (&td, 1);
2213 switch (td.sp [-1].type) {
2215 ADD_CODE(&td, MINT_CONV_OVF_I8_UN_R8);
2218 if (*td.ip == CEE_CONV_OVF_I8_UN)
2219 ADD_CODE (&td, MINT_CONV_OVF_I8_U8);
2222 ADD_CODE(&td, MINT_CONV_I8_U4);
2225 g_assert_not_reached ();
2228 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2233 CHECK_STACK (&td, 1);
2234 token = read32 (td.ip + 1);
2235 if (method->wrapper_type != MONO_WRAPPER_NONE)
2236 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2238 klass = mono_class_get_full (image, token, generic_context);
2240 if (mono_class_is_nullable (klass)) {
2241 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Box", 1);
2242 /* td.ip is incremented by interp_transform_call */
2243 interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
2244 } else if (!klass->valuetype) {
2245 /* already boxed, do nothing. */
2248 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT && !klass->enumtype) {
2249 size = mono_class_value_size (klass, NULL);
2250 size = (size + 7) & ~7;
2253 ADD_CODE(&td, MINT_BOX);
2254 ADD_CODE(&td, get_data_item_index (&td, klass));
2256 SET_TYPE(td.sp - 1, STACK_TYPE_O, klass);
2263 CHECK_STACK (&td, 1);
2264 token = read32 (td.ip + 1);
2266 if (method->wrapper_type != MONO_WRAPPER_NONE)
2267 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2269 klass = mono_class_get_full (image, token, generic_context);
2271 unsigned char lentype = (td.sp - 1)->type;
2272 if (lentype == STACK_TYPE_I8) {
2273 /* mimic mini behaviour */
2274 ADD_CODE (&td, MINT_CONV_OVF_U4_I8);
2276 g_assert (lentype == STACK_TYPE_I4);
2277 ADD_CODE (&td, MINT_CONV_OVF_U4_I4);
2279 SET_SIMPLE_TYPE (td.sp - 1, STACK_TYPE_I4);
2280 ADD_CODE (&td, MINT_NEWARR);
2281 ADD_CODE (&td, get_data_item_index (&td, klass));
2282 SET_TYPE (td.sp - 1, STACK_TYPE_O, klass);
2287 CHECK_STACK (&td, 1);
2288 SIMPLE_OP (td, MINT_LDLEN);
2289 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2292 CHECK_STACK (&td, 2);
2294 token = read32 (td.ip + 1);
2296 if (method->wrapper_type != MONO_WRAPPER_NONE)
2297 klass = (MonoClass *) mono_method_get_wrapper_data (method, token);
2299 klass = mono_class_get_full (image, token, generic_context);
2301 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
2302 ADD_CODE (&td, MINT_LDELEMA_TC);
2304 ADD_CODE (&td, MINT_LDELEMA);
2306 ADD_CODE (&td, get_data_item_index (&td, klass));
2307 /* according to spec, ldelema bytecode is only used for 1-dim arrays */
2313 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2316 CHECK_STACK (&td, 2);
2318 SIMPLE_OP (td, MINT_LDELEM_I1);
2320 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2323 CHECK_STACK (&td, 2);
2325 SIMPLE_OP (td, MINT_LDELEM_U1);
2327 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2330 CHECK_STACK (&td, 2);
2332 SIMPLE_OP (td, MINT_LDELEM_I2);
2334 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2337 CHECK_STACK (&td, 2);
2339 SIMPLE_OP (td, MINT_LDELEM_U2);
2341 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2344 CHECK_STACK (&td, 2);
2346 SIMPLE_OP (td, MINT_LDELEM_I4);
2348 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2351 CHECK_STACK (&td, 2);
2353 SIMPLE_OP (td, MINT_LDELEM_U4);
2355 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2358 CHECK_STACK (&td, 2);
2360 SIMPLE_OP (td, MINT_LDELEM_I8);
2362 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2365 CHECK_STACK (&td, 2);
2367 SIMPLE_OP (td, MINT_LDELEM_I);
2369 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2372 CHECK_STACK (&td, 2);
2374 SIMPLE_OP (td, MINT_LDELEM_R4);
2376 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2379 CHECK_STACK (&td, 2);
2381 SIMPLE_OP (td, MINT_LDELEM_R8);
2383 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2385 case CEE_LDELEM_REF:
2386 CHECK_STACK (&td, 2);
2388 SIMPLE_OP (td, MINT_LDELEM_REF);
2390 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
2393 CHECK_STACK (&td, 2);
2394 token = read32 (td.ip + 1);
2395 klass = mono_class_get_full (image, token, generic_context);
2396 switch (mint_type (&klass->byval_arg)) {
2399 SIMPLE_OP (td, MINT_LDELEM_I1);
2401 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2405 SIMPLE_OP (td, MINT_LDELEM_U1);
2407 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2411 SIMPLE_OP (td, MINT_LDELEM_U2);
2413 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2417 SIMPLE_OP (td, MINT_LDELEM_I2);
2419 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2423 SIMPLE_OP (td, MINT_LDELEM_I4);
2425 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2429 SIMPLE_OP (td, MINT_LDELEM_I8);
2431 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2435 SIMPLE_OP (td, MINT_LDELEM_R4);
2437 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2441 SIMPLE_OP (td, MINT_LDELEM_R8);
2443 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2447 SIMPLE_OP (td, MINT_LDELEM_REF);
2449 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
2451 case MINT_TYPE_VT: {
2452 int size = mono_class_value_size (klass, NULL);
2454 SIMPLE_OP (td, MINT_LDELEM_VT);
2455 ADD_CODE (&td, get_data_item_index (&td, klass));
2456 WRITE32 (&td, &size);
2458 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_VT);
2459 PUSH_VT (&td, size);
2463 GString *res = g_string_new ("");
2464 mono_type_get_desc (res, &klass->byval_arg, TRUE);
2465 g_print ("LDELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
2466 g_string_free (res, TRUE);
2474 CHECK_STACK (&td, 3);
2476 SIMPLE_OP (td, MINT_STELEM_I);
2480 CHECK_STACK (&td, 3);
2482 SIMPLE_OP (td, MINT_STELEM_I1);
2486 CHECK_STACK (&td, 3);
2488 SIMPLE_OP (td, MINT_STELEM_I2);
2492 CHECK_STACK (&td, 3);
2494 SIMPLE_OP (td, MINT_STELEM_I4);
2498 CHECK_STACK (&td, 3);
2500 SIMPLE_OP (td, MINT_STELEM_I8);
2504 CHECK_STACK (&td, 3);
2506 SIMPLE_OP (td, MINT_STELEM_R4);
2510 CHECK_STACK (&td, 3);
2512 SIMPLE_OP (td, MINT_STELEM_R8);
2515 case CEE_STELEM_REF:
2516 CHECK_STACK (&td, 3);
2518 SIMPLE_OP (td, MINT_STELEM_REF);
2522 CHECK_STACK (&td, 3);
2524 token = read32 (td.ip + 1);
2525 klass = mono_class_get_full (image, token, generic_context);
2526 switch (mint_type (&klass->byval_arg)) {
2528 SIMPLE_OP (td, MINT_STELEM_U1);
2531 SIMPLE_OP (td, MINT_STELEM_I4);
2534 SIMPLE_OP (td, MINT_STELEM_I8);
2537 SIMPLE_OP (td, MINT_STELEM_REF);
2539 case MINT_TYPE_VT: {
2540 int size = mono_class_value_size (klass, NULL);
2541 SIMPLE_OP (td, MINT_STELEM_VT);
2542 ADD_CODE (&td, get_data_item_index (&td, klass));
2543 WRITE32 (&td, &size);
2548 GString *res = g_string_new ("");
2549 mono_type_get_desc (res, &klass->byval_arg, TRUE);
2550 g_print ("STELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
2551 g_string_free (res, TRUE);
2560 case CEE_CONV_OVF_U1:
2562 case CEE_CONV_OVF_I8:
2564 #if SIZEOF_VOID_P == 8
2565 case CEE_CONV_OVF_U:
2567 case CEE_REFANYVAL: ves_abort(); break;
2570 CHECK_STACK (&td, 1);
2571 SIMPLE_OP (td, MINT_CKFINITE);
2573 case CEE_CONV_OVF_I1:
2574 case CEE_CONV_OVF_I1_UN:
2575 CHECK_STACK (&td, 1);
2576 switch (td.sp [-1].type) {
2578 ADD_CODE(&td, MINT_CONV_OVF_I1_R8);
2581 ADD_CODE(&td, MINT_CONV_OVF_I1_I4);
2584 ADD_CODE(&td, MINT_CONV_OVF_I1_I8);
2587 g_assert_not_reached ();
2590 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2592 case CEE_CONV_OVF_U1:
2593 case CEE_CONV_OVF_U1_UN:
2594 CHECK_STACK (&td, 1);
2595 switch (td.sp [-1].type) {
2597 ADD_CODE(&td, MINT_CONV_OVF_U1_R8);
2600 ADD_CODE(&td, MINT_CONV_OVF_U1_I4);
2603 ADD_CODE(&td, MINT_CONV_OVF_U1_I8);
2606 g_assert_not_reached ();
2609 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2611 case CEE_CONV_OVF_I2:
2612 case CEE_CONV_OVF_I2_UN:
2613 CHECK_STACK (&td, 1);
2614 switch (td.sp [-1].type) {
2616 ADD_CODE(&td, MINT_CONV_OVF_I2_R8);
2619 ADD_CODE(&td, MINT_CONV_OVF_I2_I4);
2622 ADD_CODE(&td, MINT_CONV_OVF_I2_I8);
2625 g_assert_not_reached ();
2628 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2630 case CEE_CONV_OVF_U2_UN:
2631 case CEE_CONV_OVF_U2:
2632 CHECK_STACK (&td, 1);
2633 switch (td.sp [-1].type) {
2635 ADD_CODE(&td, MINT_CONV_OVF_U2_R8);
2638 ADD_CODE(&td, MINT_CONV_OVF_U2_I4);
2641 ADD_CODE(&td, MINT_CONV_OVF_U2_I8);
2644 g_assert_not_reached ();
2647 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2649 #if SIZEOF_VOID_P == 4
2650 case CEE_CONV_OVF_I:
2652 case CEE_CONV_OVF_I4:
2653 case CEE_CONV_OVF_I4_UN:
2654 CHECK_STACK (&td, 1);
2655 switch (td.sp [-1].type) {
2657 ADD_CODE(&td, MINT_CONV_OVF_I4_R8);
2660 if (*td.ip == CEE_CONV_OVF_I4_UN)
2661 ADD_CODE(&td, MINT_CONV_OVF_I4_U4);
2664 if (*td.ip == CEE_CONV_OVF_I4_UN)
2665 ADD_CODE (&td, MINT_CONV_OVF_I4_U8);
2667 ADD_CODE (&td, MINT_CONV_OVF_I4_I8);
2670 g_assert_not_reached ();
2673 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2675 #if SIZEOF_VOID_P == 4
2676 case CEE_CONV_OVF_U:
2678 case CEE_CONV_OVF_U4:
2679 case CEE_CONV_OVF_U4_UN:
2680 CHECK_STACK (&td, 1);
2681 switch (td.sp [-1].type) {
2683 ADD_CODE(&td, MINT_CONV_OVF_U4_R8);
2686 if (*td.ip != CEE_CONV_OVF_U4_UN)
2687 ADD_CODE(&td, MINT_CONV_OVF_U4_I4);
2690 ADD_CODE(&td, MINT_CONV_OVF_U4_I8);
2693 g_assert_not_reached ();
2696 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2698 #if SIZEOF_VOID_P == 8
2699 case CEE_CONV_OVF_I:
2701 case CEE_CONV_OVF_I8:
2702 CHECK_STACK (&td, 1);
2703 switch (td.sp [-1].type) {
2705 ADD_CODE(&td, MINT_CONV_OVF_I8_R8);
2708 ADD_CODE(&td, MINT_CONV_I8_I4);
2713 g_assert_not_reached ();
2716 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2718 #if SIZEOF_VOID_P == 8
2719 case CEE_CONV_OVF_U:
2721 case CEE_CONV_OVF_U8:
2722 CHECK_STACK (&td, 1);
2723 switch (td.sp [-1].type) {
2725 ADD_CODE(&td, MINT_CONV_OVF_U8_R8);
2728 ADD_CODE(&td, MINT_CONV_OVF_U8_I4);
2731 ADD_CODE (&td, MINT_CONV_OVF_U8_I8);
2734 g_assert_not_reached ();
2737 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2742 token = read32 (td.ip + 1);
2743 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
2744 handle = mono_method_get_wrapper_data (method, token);
2745 klass = (MonoClass *) mono_method_get_wrapper_data (method, token + 1);
2746 if (klass == mono_defaults.typehandle_class)
2747 handle = &((MonoClass *) handle)->byval_arg;
2749 if (generic_context) {
2750 handle = mono_class_inflate_generic_type_checked (handle, generic_context, &error);
2751 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2754 handle = mono_ldtoken (image, token, &klass, generic_context);
2756 mono_class_init (klass);
2757 mt = mint_type (&klass->byval_arg);
2758 g_assert (mt == MINT_TYPE_VT);
2759 size = mono_class_value_size (klass, NULL);
2760 g_assert (size == sizeof(gpointer));
2761 PUSH_VT (&td, sizeof(gpointer));
2762 ADD_CODE (&td, MINT_LDTOKEN);
2763 ADD_CODE (&td, get_data_item_index (&td, handle));
2765 SET_TYPE (td.sp, stack_type [mt], klass);
2771 binary_arith_op(&td, MINT_ADD_OVF_I4);
2774 case CEE_ADD_OVF_UN:
2775 binary_arith_op(&td, MINT_ADD_OVF_UN_I4);
2779 binary_arith_op(&td, MINT_MUL_OVF_I4);
2782 case CEE_MUL_OVF_UN:
2783 binary_arith_op(&td, MINT_MUL_OVF_UN_I4);
2787 binary_arith_op(&td, MINT_SUB_OVF_I4);
2790 case CEE_SUB_OVF_UN:
2791 binary_arith_op(&td, MINT_SUB_OVF_UN_I4);
2794 case CEE_ENDFINALLY:
2796 SIMPLE_OP (td, MINT_ENDFINALLY);
2797 generating_code = 0;
2801 handle_branch (&td, MINT_LEAVE_S, MINT_LEAVE, 5 + read32 (td.ip + 1));
2803 generating_code = 0;
2807 handle_branch (&td, MINT_LEAVE_S, MINT_LEAVE, 2 + (gint8)td.ip [1]);
2809 generating_code = 0;
2814 case CEE_MONO_CALLI_EXTRA_ARG:
2815 /* Same as CEE_CALLI, except that we drop the extra arg required for llvm specific behaviour */
2816 ADD_CODE (&td, MINT_POP);
2819 interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
2821 case CEE_MONO_JIT_ICALL_ADDR: {
2824 MonoJitICallInfo *info;
2826 token = read32 (td.ip + 1);
2828 func = mono_method_get_wrapper_data (method, token);
2829 info = mono_find_jit_icall_by_addr (func);
2831 ADD_CODE (&td, MINT_LDFTN);
2832 ADD_CODE (&td, get_data_item_index (&td, func));
2833 PUSH_SIMPLE_TYPE (&td, STACK_TYPE_I);
2836 case CEE_MONO_ICALL: {
2839 MonoJitICallInfo *info;
2841 token = read32 (td.ip + 1);
2843 func = mono_method_get_wrapper_data (method, token);
2844 info = mono_find_jit_icall_by_addr (func);
2847 CHECK_STACK (&td, info->sig->param_count);
2848 switch (info->sig->param_count) {
2850 if (MONO_TYPE_IS_VOID (info->sig->ret))
2851 ADD_CODE (&td,MINT_ICALL_V_V);
2853 ADD_CODE (&td, MINT_ICALL_V_P);
2856 if (MONO_TYPE_IS_VOID (info->sig->ret))
2857 ADD_CODE (&td,MINT_ICALL_P_V);
2859 ADD_CODE (&td,MINT_ICALL_P_P);
2862 if (MONO_TYPE_IS_VOID (info->sig->ret)) {
2863 if (info->sig->params [1]->type == MONO_TYPE_I4)
2864 ADD_CODE (&td,MINT_ICALL_PI_V);
2866 ADD_CODE (&td,MINT_ICALL_PP_V);
2868 if (info->sig->params [1]->type == MONO_TYPE_I4)
2869 ADD_CODE (&td,MINT_ICALL_PI_P);
2871 ADD_CODE (&td,MINT_ICALL_PP_P);
2875 g_assert (MONO_TYPE_IS_VOID (info->sig->ret));
2876 if (info->sig->params [2]->type == MONO_TYPE_I4)
2877 ADD_CODE (&td,MINT_ICALL_PPI_V);
2879 ADD_CODE (&td,MINT_ICALL_PPP_V);
2882 g_assert_not_reached ();
2885 if (func == mono_ftnptr_to_delegate) {
2886 g_error ("TODO: ?");
2888 ADD_CODE(&td, get_data_item_index (&td, func));
2889 td.sp -= info->sig->param_count;
2891 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2893 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2897 case CEE_MONO_VTADDR: {
2899 CHECK_STACK (&td, 1);
2900 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
2901 size = mono_class_native_size(td.sp [-1].klass, NULL);
2903 size = mono_class_value_size(td.sp [-1].klass, NULL);
2904 size = (size + 7) & ~7;
2905 ADD_CODE(&td, MINT_VTRESULT);
2907 WRITE32(&td, &size);
2910 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2913 case CEE_MONO_LDPTR:
2914 case CEE_MONO_CLASSCONST:
2915 token = read32 (td.ip + 1);
2917 ADD_CODE(&td, MINT_MONO_LDPTR);
2918 ADD_CODE(&td, get_data_item_index (&td, mono_method_get_wrapper_data (method, token)));
2919 td.sp [0].type = STACK_TYPE_I;
2922 case CEE_MONO_OBJADDR:
2923 CHECK_STACK (&td, 1);
2925 td.sp[-1].type = STACK_TYPE_MP;
2928 case CEE_MONO_NEWOBJ:
2929 token = read32 (td.ip + 1);
2931 ADD_CODE(&td, MINT_MONO_NEWOBJ);
2932 ADD_CODE(&td, get_data_item_index (&td, mono_method_get_wrapper_data (method, token)));
2933 td.sp [0].type = STACK_TYPE_O;
2936 case CEE_MONO_RETOBJ:
2937 CHECK_STACK (&td, 1);
2938 token = read32 (td.ip + 1);
2940 ADD_CODE(&td, MINT_MONO_RETOBJ);
2943 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2945 /*stackval_from_data (signature->ret, frame->retval, sp->data.vt, signature->pinvoke);*/
2947 if (td.sp > td.stack)
2948 g_warning ("CEE_MONO_RETOBJ: more values on stack: %d", td.sp-td.stack);
2950 case CEE_MONO_LDNATIVEOBJ:
2951 token = read32 (td.ip + 1);
2953 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2954 g_assert(klass->valuetype);
2955 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2957 case CEE_MONO_SAVE_LMF:
2958 case CEE_MONO_RESTORE_LMF:
2959 case CEE_MONO_NOT_TAKEN:
2962 case CEE_MONO_LDPTR_INT_REQ_FLAG:
2963 ADD_CODE (&td, MINT_MONO_LDPTR);
2964 ADD_CODE (&td, get_data_item_index (&td, mono_thread_interruption_request_flag ()));
2965 PUSH_TYPE (&td, STACK_TYPE_MP, NULL);
2969 g_error ("transform.c: Unimplemented opcode: 0xF0 %02x at 0x%x\n", *td.ip, td.ip-header->code);
2979 case CEE_PREFIXREF: ves_abort(); break;
2982 * Note: Exceptions thrown when executing a prefixed opcode need
2983 * to take into account the number of prefix bytes (usually the
2984 * throw point is just (ip - n_prefix_bytes).
2990 case CEE_ARGLIST: ves_abort(); break;
2993 CHECK_STACK(&td, 2);
2994 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2995 ADD_CODE(&td, MINT_CEQ_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2997 ADD_CODE(&td, MINT_CEQ_I4 + td.sp [-1].type - STACK_TYPE_I4);
2999 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3003 CHECK_STACK(&td, 2);
3004 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
3005 ADD_CODE(&td, MINT_CGT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3007 ADD_CODE(&td, MINT_CGT_I4 + td.sp [-1].type - STACK_TYPE_I4);
3009 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3013 CHECK_STACK(&td, 2);
3014 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
3015 ADD_CODE(&td, MINT_CGT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3017 ADD_CODE(&td, MINT_CGT_UN_I4 + td.sp [-1].type - STACK_TYPE_I4);
3019 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3023 CHECK_STACK(&td, 2);
3024 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
3025 ADD_CODE(&td, MINT_CLT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3027 ADD_CODE(&td, MINT_CLT_I4 + td.sp [-1].type - STACK_TYPE_I4);
3029 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3033 CHECK_STACK(&td, 2);
3034 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
3035 ADD_CODE(&td, MINT_CLT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3037 ADD_CODE(&td, MINT_CLT_UN_I4 + td.sp [-1].type - STACK_TYPE_I4);
3039 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3042 case CEE_LDVIRTFTN: /* fallthrough */
3045 if (*td.ip == CEE_LDVIRTFTN) {
3046 CHECK_STACK (&td, 1);
3049 token = read32 (td.ip + 1);
3050 if (method->wrapper_type != MONO_WRAPPER_NONE)
3051 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3053 m = mono_get_method_full (image, token, NULL, generic_context);
3055 if (method->wrapper_type == MONO_WRAPPER_NONE && m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
3056 m = mono_marshal_get_synchronized_wrapper (m);
3058 ADD_CODE(&td, *td.ip == CEE_LDFTN ? MINT_LDFTN : MINT_LDVIRTFTN);
3059 ADD_CODE(&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
3060 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3062 PUSH_SIMPLE_TYPE (&td, STACK_TYPE_F);
3066 load_arg (&td, read16 (td.ip + 1));
3070 int n = read16 (td.ip + 1);
3071 ADD_CODE (&td, MINT_LDARGA);
3072 ADD_CODE (&td, td.rtm->arg_offsets [n]); /* FIX for large offsets */
3073 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
3078 store_arg (&td, read16 (td.ip + 1));
3082 load_local (&td, read16 (td.ip + 1));
3086 ADD_CODE(&td, MINT_LDLOCA_S);
3087 ADD_CODE(&td, td.rtm->local_offsets [read16 (td.ip + 1)]);
3088 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
3092 store_local (&td, read16 (td.ip + 1));
3096 CHECK_STACK (&td, 1);
3097 #if SIZEOF_VOID_P == 8
3098 if (td.sp [-1].type == STACK_TYPE_I8)
3099 ADD_CODE(&td, MINT_CONV_I4_I8);
3101 ADD_CODE(&td, MINT_LOCALLOC);
3102 if (td.sp != td.stack + 1)
3103 g_warning("CEE_LOCALLOC: stack not empty");
3105 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
3108 case CEE_UNUSED57: ves_abort(); break;
3109 case CEE_ENDFILTER: ves_abort(); break;
3111 case CEE_UNALIGNED_:
3113 /* FIX: should do something? */;
3117 /* FIX: should do something? */;
3121 /* FIX: should do something? */;
3124 CHECK_STACK(&td, 1);
3125 token = read32 (td.ip + 1);
3126 klass = mono_class_get_full (image, token, generic_context);
3127 if (klass->valuetype) {
3128 ADD_CODE (&td, MINT_INITOBJ);
3129 i32 = mono_class_value_size (klass, NULL);
3130 WRITE32 (&td, &i32);
3132 ADD_CODE (&td, MINT_LDNULL);
3133 ADD_CODE (&td, MINT_STIND_REF);
3139 CHECK_STACK(&td, 3);
3140 /* FIX? convert length to I8? */
3141 ADD_CODE(&td, MINT_CPBLK);
3149 case CEE_CONSTRAINED_:
3150 token = read32 (td.ip + 1);
3151 constrained_class = mono_class_get_full (image, token, generic_context);
3152 mono_class_init (constrained_class);
3156 CHECK_STACK(&td, 3);
3157 ADD_CODE(&td, MINT_INITBLK);
3163 /* FIXME: implement */
3168 SIMPLE_OP (td, MINT_RETHROW);
3169 generating_code = 0;
3173 token = read32 (td.ip + 1);
3175 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC && !image_is_dynamic (method->klass->image) && !generic_context) {
3177 MonoType *type = mono_type_create_from_typespec (image, token);
3178 size = mono_type_size (type, &align);
3181 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
3182 mono_class_init (szclass);
3184 if (!szclass->valuetype)
3185 THROW_EX (mono_exception_from_name (mono_defaults.corlib, "System", "InvalidProgramException"), ip - 5);
3187 size = mono_type_size (&szclass->byval_arg, &align);
3189 ADD_CODE(&td, MINT_LDC_I4);
3190 WRITE32(&td, &size);
3191 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
3195 case CEE_REFANYTYPE: ves_abort(); break;
3198 g_error ("transform.c: Unimplemented opcode: 0xFE %02x (%s) at 0x%x\n", *td.ip, mono_opcode_name (256 + *td.ip), td.ip-header->code);
3202 g_error ("transform.c: Unimplemented opcode: %02x at 0x%x\n", *td.ip, td.ip-header->code);
3205 if (td.new_ip - td.new_code != new_in_start_offset)
3206 td.last_new_ip = td.new_code + new_in_start_offset;
3207 else if (td.is_bb_start [td.in_start - td.il_code])
3208 td.is_bb_start [td.ip - td.il_code] = 1;
3210 td.last_ip = td.in_start;
3213 if (mono_interp_traceopt) {
3214 const guint16 *p = td.new_code;
3215 printf("Runtime method: %p, VT stack size: %d\n", rtm, td.max_vt_sp);
3216 printf("Calculated stack size: %d, stated size: %d\n", td.max_stack_height, header->max_stack);
3217 while (p < td.new_ip) {
3218 p = mono_interp_dis_mintop(td.new_code, p);
3222 g_assert (td.max_stack_height <= (header->max_stack + 1));
3224 rtm->clauses = mono_domain_alloc0 (domain, header->num_clauses * sizeof (MonoExceptionClause));
3225 memcpy (rtm->clauses, header->clauses, header->num_clauses * sizeof(MonoExceptionClause));
3226 rtm->code = mono_domain_alloc0 (domain, (td.new_ip - td.new_code) * sizeof (gushort));
3227 memcpy (rtm->code, td.new_code, (td.new_ip - td.new_code) * sizeof(gushort));
3228 g_free (td.new_code);
3229 rtm->new_body_start = rtm->code + body_start_offset;
3230 rtm->num_clauses = header->num_clauses;
3231 for (i = 0; i < header->num_clauses; i++) {
3232 MonoExceptionClause *c = rtm->clauses + i;
3233 int end_off = c->try_offset + c->try_len;
3234 c->try_offset = td.in_offsets [c->try_offset];
3235 c->try_len = td.in_offsets [end_off] - c->try_offset;
3236 end_off = c->handler_offset + c->handler_len;
3237 c->handler_offset = td.in_offsets [c->handler_offset];
3238 c->handler_len = td.in_offsets [end_off] - c->handler_offset;
3240 rtm->vt_stack_size = td.max_vt_sp;
3241 rtm->alloca_size = rtm->locals_size + rtm->args_size + rtm->vt_stack_size + rtm->stack_size;
3242 rtm->data_items = mono_domain_alloc0 (domain, td.n_data_items * sizeof (td.data_items [0]));
3243 memcpy (rtm->data_items, td.data_items, td.n_data_items * sizeof (td.data_items [0]));
3244 g_free (td.in_offsets);
3245 g_free (td.forward_refs);
3246 for (i = 0; i < header->code_size; ++i)
3247 g_free (td.stack_state [i]);
3248 g_free (td.stack_state);
3249 g_free (td.stack_height);
3250 g_free (td.vt_stack_size);
3251 g_free (td.data_items);
3253 g_hash_table_destroy (td.data_hash);
3256 static mono_mutex_t calc_section;
3259 mono_interp_transform_init (void)
3261 mono_os_mutex_init_recursive(&calc_section);
3265 mono_interp_transform_method (RuntimeMethod *runtime_method, ThreadContext *context)
3267 int i, align, size, offset;
3268 MonoMethod *method = runtime_method->method;
3269 MonoImage *image = method->klass->image;
3270 MonoMethodHeader *header = mono_method_get_header (method);
3271 MonoMethodSignature *signature = mono_method_signature (method);
3272 register const unsigned char *ip, *end;
3273 const MonoOpcode *opcode;
3276 MonoDomain *domain = mono_domain_get ();
3277 unsigned char *is_bb_start;
3279 MonoVTable *method_class_vt;
3281 MonoGenericContext *generic_context = NULL;
3283 // g_printerr ("TRANSFORM(0x%016lx): begin %s::%s\n", mono_thread_current (), method->klass->name, method->name);
3284 method_class_vt = mono_class_vtable (domain, runtime_method->method->klass);
3285 if (!method_class_vt->initialized) {
3288 MonoInvocation *last_env_frame = context->env_frame;
3289 jmp_buf *old_env = context->current_env;
3290 error_init (&error);
3293 MonoException *failed = context->env_frame->ex;
3294 context->env_frame->ex = NULL;
3295 context->env_frame = last_env_frame;
3296 context->current_env = old_env;
3299 context->env_frame = context->current_frame;
3300 context->current_env = &env;
3301 mono_runtime_class_init_full (method_class_vt, &error);
3302 if (!mono_error_ok (&error)) {
3303 return mono_error_convert_to_exception (&error);
3305 context->env_frame = last_env_frame;
3306 context->current_env = old_env;
3309 mono_profiler_method_jit (method); /* sort of... */
3311 if (mono_method_signature (method)->is_inflated)
3312 generic_context = mono_method_get_context (method);
3314 MonoGenericContainer *generic_container = mono_method_get_generic_container (method);
3315 if (generic_container)
3316 generic_context = &generic_container->context;
3319 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
3320 MonoMethod *nm = NULL;
3321 mono_os_mutex_lock(&calc_section);
3322 if (runtime_method->transformed) {
3323 mono_os_mutex_unlock(&calc_section);
3324 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3328 /* assumes all internal calls with an array this are built in... */
3329 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL && (! mono_method_signature (method)->hasthis || method->klass->rank == 0)) {
3330 nm = mono_marshal_get_native_wrapper (method, TRUE, FALSE);
3331 signature = mono_method_signature (nm);
3333 const char *name = method->name;
3334 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
3335 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
3336 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ("ves_icall_mono_delegate_ctor");
3338 char *wrapper_name = g_strdup_printf ("__icall_wrapper_%s", mi->name);
3339 nm = mono_marshal_get_icall_wrapper (mi->sig, wrapper_name, mi->func, TRUE);
3340 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
3341 nm = mono_marshal_get_delegate_invoke (method, NULL);
3342 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
3343 nm = mono_marshal_get_delegate_begin_invoke (method);
3344 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
3345 nm = mono_marshal_get_delegate_end_invoke (method);
3349 runtime_method->code = g_malloc(sizeof(short));
3350 runtime_method->code[0] = MINT_CALLRUN;
3354 runtime_method->stack_size = sizeof (stackval); /* for tracing */
3355 runtime_method->alloca_size = runtime_method->stack_size;
3356 runtime_method->transformed = TRUE;
3357 mono_os_mutex_unlock(&calc_section);
3358 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3362 header = mono_method_get_header (nm);
3363 mono_os_mutex_unlock(&calc_section);
3364 } else if (method->klass == mono_defaults.array_class) {
3365 if (!strcmp (method->name, "UnsafeMov")) {
3366 mono_os_mutex_lock (&calc_section);
3367 if (!runtime_method->transformed) {
3368 runtime_method->code = g_malloc (sizeof (short));
3369 runtime_method->code[0] = MINT_CALLRUN;
3370 runtime_method->stack_size = sizeof (stackval); /* for tracing */
3371 runtime_method->alloca_size = runtime_method->stack_size;
3372 runtime_method->transformed = TRUE;
3374 mono_os_mutex_unlock(&calc_section);
3375 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3377 } else if (!strcmp (method->name, "UnsafeStore)")) {
3379 } else if (!strcmp (method->name, "UnsafeLoad)")) {
3383 g_assert ((signature->param_count + signature->hasthis) < 1000);
3384 g_assert (header->max_stack < 10000);
3385 /* intern the strings in the method. */
3387 end = ip + header->code_size;
3389 is_bb_start = g_malloc0(header->code_size);
3390 is_bb_start [0] = 1;
3397 else if (in == 0xf0) {
3399 in = *ip + MONO_CEE_MONO_ICALL;
3401 opcode = &mono_opcodes [in];
3402 switch (opcode->argument) {
3403 case MonoInlineNone:
3406 case MonoInlineString:
3407 if (method->wrapper_type == MONO_WRAPPER_NONE)
3408 mono_ldstr (domain, image, mono_metadata_token_index (read32 (ip + 1)));
3411 case MonoInlineType:
3412 if (method->wrapper_type == MONO_WRAPPER_NONE) {
3413 class = mono_class_get_full (image, read32 (ip + 1), generic_context);
3414 mono_class_init (class);
3415 /* quick fix to not do this for the fake ptr classes - probably should not be getting the vtable at all here */
3417 g_error ("FIXME: interface method lookup: %s (in method %s)", class->name, method->name);
3418 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE) && class->interface_offsets != NULL)
3419 mono_class_vtable (domain, class);
3424 case MonoInlineMethod:
3425 if (method->wrapper_type == MONO_WRAPPER_NONE && *ip != CEE_CALLI) {
3426 m = mono_get_method_full (image, read32 (ip + 1), NULL, generic_context);
3428 g_free (is_bb_start);
3429 g_error ("FIXME: where to get method and class string?");
3431 // return mono_get_exception_missing_method ();
3433 mono_class_init (m->klass);
3434 if (!mono_class_is_interface (m->klass))
3435 mono_class_vtable (domain, m->klass);
3439 case MonoInlineField:
3443 case MonoShortInlineR:
3446 case MonoInlineBrTarget:
3447 offset = read32 (ip + 1);
3449 backwards = offset < 0;
3450 offset += ip - header->code;
3451 g_assert (offset >= 0 && offset < header->code_size);
3452 is_bb_start [offset] |= backwards ? 2 : 1;
3454 case MonoShortInlineBrTarget:
3455 offset = ((gint8 *)ip) [1];
3457 backwards = offset < 0;
3458 offset += ip - header->code;
3459 g_assert (offset >= 0 && offset < header->code_size);
3460 is_bb_start [offset] |= backwards ? 2 : 1;
3465 case MonoShortInlineVar:
3466 case MonoShortInlineI:
3469 case MonoInlineSwitch: {
3471 const unsigned char *next_ip;
3475 next_ip = ip + 4 * n;
3476 for (i = 0; i < n; i++) {
3477 offset = read32 (ip);
3478 backwards = offset < 0;
3479 offset += next_ip - header->code;
3480 g_assert (offset >= 0 && offset < header->code_size);
3481 is_bb_start [offset] |= backwards ? 2 : 1;
3491 g_assert_not_reached ();
3494 // g_printerr ("TRANSFORM(0x%016lx): end %s::%s\n", mono_thread_current (), method->klass->name, method->name);
3496 /* the rest needs to be locked so it is only done once */
3497 mono_os_mutex_lock(&calc_section);
3498 if (runtime_method->transformed) {
3499 mono_os_mutex_unlock(&calc_section);
3500 g_free (is_bb_start);
3501 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3505 runtime_method->local_offsets = g_malloc (header->num_locals * sizeof(guint32));
3506 runtime_method->stack_size = (sizeof (stackval) + 2) * header->max_stack; /* + 1 for returns of called functions + 1 for 0-ing in trace*/
3507 runtime_method->stack_size = (runtime_method->stack_size + 7) & ~7;
3509 for (i = 0; i < header->num_locals; ++i) {
3510 size = mono_type_size (header->locals [i], &align);
3511 offset += align - 1;
3512 offset &= ~(align - 1);
3513 runtime_method->local_offsets [i] = offset;
3516 offset = (offset + 7) & ~7;
3517 runtime_method->locals_size = offset;
3518 g_assert (runtime_method->locals_size < 65536);
3520 runtime_method->arg_offsets = g_malloc ((!!signature->hasthis + signature->param_count) * sizeof(guint32));
3522 if (signature->hasthis) {
3523 g_assert (!signature->pinvoke);
3524 size = mono_type_stack_size (&method->klass->byval_arg, &align);
3525 offset += align - 1;
3526 offset &= ~(align - 1);
3527 runtime_method->arg_offsets [0] = offset;
3531 for (i = 0; i < signature->param_count; ++i) {
3532 if (signature->pinvoke) {
3534 size = mono_type_native_stack_size (signature->params [i], &dummy);
3538 size = mono_type_stack_size (signature->params [i], &align);
3539 offset += align - 1;
3540 offset &= ~(align - 1);
3541 runtime_method->arg_offsets [i + !!signature->hasthis] = offset;
3544 offset = (offset + 7) & ~7;
3545 runtime_method->args_size = offset;
3546 g_assert (runtime_method->args_size < 10000);
3548 generate (method, runtime_method, is_bb_start, generic_context);
3550 g_free (is_bb_start);
3552 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3553 runtime_method->transformed = TRUE;
3554 mono_os_mutex_unlock(&calc_section);