2 * transform CIL into different opcodes for more
3 * efficient interpretation
5 * Written by Bernie Solomon (bernard@ugsolutions.com)
10 #include <mono/metadata/appdomain.h>
11 #include <mono/metadata/debug-helpers.h>
12 #include <mono/metadata/exception.h>
13 #include <mono/metadata/mono-endian.h>
14 #include <mono/metadata/marshal.h>
15 #include <mono/metadata/profiler-private.h>
16 #include <mono/metadata/tabledefs.h>
18 #include <mono/mini/mini.h>
21 #include "interp-internals.h"
24 // TODO: export from marshal.c
25 MonoDelegate* mono_ftnptr_to_delegate (MonoClass *klass, gpointer ftn);
39 MonoMethodHeader *header;
41 const unsigned char *il_code;
42 const unsigned char *ip;
43 const unsigned char *last_ip;
44 const unsigned char *in_start;
48 StackInfo **stack_state;
51 unsigned char *is_bb_start;
52 unsigned short *new_code;
53 unsigned short *new_code_end;
54 unsigned short *new_ip;
55 unsigned short *last_new_ip;
56 unsigned int max_code_size;
59 unsigned int max_stack_height;
61 unsigned int max_vt_sp;
65 GHashTable *data_hash;
68 #define MINT_TYPE_I1 0
69 #define MINT_TYPE_U1 1
70 #define MINT_TYPE_I2 2
71 #define MINT_TYPE_U2 3
72 #define MINT_TYPE_I4 4
73 #define MINT_TYPE_I8 5
74 #define MINT_TYPE_R4 6
75 #define MINT_TYPE_R8 7
78 #define MINT_TYPE_VT 10
80 #define STACK_TYPE_I4 0
81 #define STACK_TYPE_I8 1
82 #define STACK_TYPE_R8 2
83 #define STACK_TYPE_O 3
84 #define STACK_TYPE_VT 4
85 #define STACK_TYPE_MP 5
86 #define STACK_TYPE_F 6
88 static const char *stack_type_string [] = { "I4", "I8", "R8", "O ", "VT", "MP", "F " };
90 #if SIZEOF_VOID_P == 8
91 #define STACK_TYPE_I STACK_TYPE_I8
93 #define STACK_TYPE_I STACK_TYPE_I4
96 static int stack_type [] = {
100 STACK_TYPE_I4, /*U2*/
101 STACK_TYPE_I4, /*I4*/
102 STACK_TYPE_I8, /*I8*/
103 STACK_TYPE_R8, /*R4*/
104 STACK_TYPE_R8, /*R8*/
111 grow_code (TransformData *td)
113 unsigned int old_ip_offset = td->new_ip - td->new_code;
114 unsigned int old_last_ip_offset = td->last_new_ip - td->new_code;
115 g_assert (old_ip_offset <= td->max_code_size);
116 td->new_code = g_realloc (td->new_code, (td->max_code_size *= 2) * sizeof (td->new_code [0]));
117 td->new_code_end = td->new_code + td->max_code_size;
118 td->new_ip = td->new_code + old_ip_offset;
119 td->last_new_ip = td->new_code + old_last_ip_offset;
122 #define ENSURE_CODE(td, n) \
124 if ((td)->new_ip + (n) > (td)->new_code_end) \
128 #define ADD_CODE(td, n) \
130 if ((td)->new_ip == (td)->new_code_end) \
132 *(td)->new_ip++ = (n); \
135 #define CHECK_STACK(td, n) \
137 int stack_size = (td)->sp - (td)->stack; \
138 if (stack_size < (n)) \
139 g_warning ("%s.%s: not enough values (%d < %d) on stack at %04x", \
140 (td)->method->klass->name, (td)->method->name, \
141 stack_size, n, (td)->ip - (td)->il_code); \
144 #define ENSURE_I4(td, sp_off) \
146 if ((td)->sp [-sp_off].type == STACK_TYPE_I8) \
147 ADD_CODE(td, sp_off == 1 ? MINT_CONV_I4_I8 : MINT_CONV_I4_I8_SP); \
151 handle_branch(TransformData *td, int short_op, int long_op, int offset)
153 int shorten_branch = 0;
154 int target = td->ip + offset - td->il_code;
155 if (target < 0 || target >= td->code_size)
156 g_assert_not_reached ();
157 if (offset > 0 && td->stack_height [target] < 0) {
158 td->stack_height [target] = td->sp - td->stack;
159 if (td->stack_height [target] > 0)
160 td->stack_state [target] = g_memdup (td->stack, td->stack_height [target] * sizeof (td->stack [0]));
161 td->vt_stack_size [target] = td->vt_sp;
164 offset = td->in_offsets [target] - (td->new_ip - td->new_code);
165 if (offset >= -32768) {
169 int prev = td->forward_refs [target];
170 td->forward_refs [td->ip - td->il_code] = prev;
171 td->forward_refs [target] = td->ip - td->il_code;
173 if (td->header->code_size <= 25000) /* FIX to be precise somehow? */
176 if (shorten_branch) {
177 ADD_CODE(td, short_op);
178 ADD_CODE(td, offset);
180 ADD_CODE(td, long_op);
181 ADD_CODE(td, * (unsigned short *)(&offset));
182 ADD_CODE(td, * ((unsigned short *)&offset + 1));
187 one_arg_branch(TransformData *td, int mint_op, int offset)
189 int type = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
190 int long_op = mint_op + type - STACK_TYPE_I4;
191 int short_op = long_op + MINT_BRFALSE_I4_S - MINT_BRFALSE_I4;
194 handle_branch (td, short_op, long_op, offset);
198 two_arg_branch(TransformData *td, int mint_op, int offset)
200 int type1 = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
201 int type2 = td->sp [-2].type == STACK_TYPE_O || td->sp [-2].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-2].type;
202 int long_op = mint_op + type1 - STACK_TYPE_I4;
203 int short_op = long_op + MINT_BEQ_I4_S - MINT_BEQ_I4;
205 if (type1 == STACK_TYPE_I4 && type2 == STACK_TYPE_I8) {
206 ADD_CODE(td, MINT_CONV_I8_I4);
207 td->in_offsets [td->ip - td->il_code]++;
208 } else if (type1 == STACK_TYPE_I8 && type2 == STACK_TYPE_I4) {
209 ADD_CODE(td, MINT_CONV_I8_I4_SP);
210 td->in_offsets [td->ip - td->il_code]++;
211 } else if (type1 != type2) {
212 g_warning("%s.%s: branch type mismatch %d %d",
213 td->method->klass->name, td->method->name,
214 td->sp [-1].type, td->sp [-2].type);
217 handle_branch (td, short_op, long_op, offset);
221 unary_arith_op(TransformData *td, int mint_op)
223 int op = mint_op + td->sp [-1].type - STACK_TYPE_I4;
229 binary_arith_op(TransformData *td, int mint_op)
231 int type1 = td->sp [-2].type;
232 int type2 = td->sp [-1].type;
234 #if SIZEOF_VOID_P == 8
235 if ((type1 == STACK_TYPE_MP || type1 == STACK_TYPE_I8) && type2 == STACK_TYPE_I4) {
236 ADD_CODE(td, MINT_CONV_I8_I4);
237 type2 = STACK_TYPE_I8;
239 if (type1 == STACK_TYPE_I4 && (type2 == STACK_TYPE_MP || type2 == STACK_TYPE_I8)) {
240 ADD_CODE(td, MINT_CONV_I8_I4_SP);
241 type1 = STACK_TYPE_I8;
242 td->sp [-2].type = STACK_TYPE_I8;
245 if (type1 == STACK_TYPE_MP)
246 type1 = STACK_TYPE_I;
247 if (type2 == STACK_TYPE_MP)
248 type2 = STACK_TYPE_I;
249 if (type1 != type2) {
250 g_warning("%s.%s: %04x arith type mismatch %s %d %d",
251 td->method->klass->name, td->method->name,
252 td->ip - td->il_code, mono_interp_opname[mint_op], type1, type2);
254 op = mint_op + type1 - STACK_TYPE_I4;
261 binary_int_op(TransformData *td, int mint_op)
263 int op = mint_op + td->sp [-1].type - STACK_TYPE_I4;
265 if (td->sp [-1].type != td->sp [-2].type)
266 g_warning("%s.%s: int type mismatch", td->method->klass->name, td->method->name);
272 shift_op(TransformData *td, int mint_op)
274 int op = mint_op + td->sp [-2].type - STACK_TYPE_I4;
276 if (td->sp [-1].type != STACK_TYPE_I4) {
277 g_warning("%s.%s: shift type mismatch %d",
278 td->method->klass->name, td->method->name,
286 mint_type(MonoType *type)
291 switch (type->type) {
295 case MONO_TYPE_BOOLEAN:
307 #if SIZEOF_VOID_P == 4
321 case MONO_TYPE_STRING:
322 case MONO_TYPE_SZARRAY:
323 case MONO_TYPE_CLASS:
324 case MONO_TYPE_OBJECT:
325 case MONO_TYPE_ARRAY:
327 case MONO_TYPE_VALUETYPE:
328 if (type->data.klass->enumtype) {
329 type = mono_class_enum_basetype (type->data.klass);
333 case MONO_TYPE_GENERICINST:
334 type = &type->data.generic_class->container_class->byval_arg;
337 g_warning ("got type 0x%02x", type->type);
338 g_assert_not_reached ();
344 can_store (int stack_type, int var_type)
346 if (stack_type == STACK_TYPE_O || stack_type == STACK_TYPE_MP)
347 stack_type = STACK_TYPE_I;
348 if (var_type == STACK_TYPE_O || var_type == STACK_TYPE_MP)
349 var_type = STACK_TYPE_I;
350 return stack_type == var_type;
353 #define SET_SIMPLE_TYPE(s, ty) \
360 #define SET_TYPE(s, ty, k) \
367 #define PUSH_SIMPLE_TYPE(td, ty) \
371 sp_height = (td)->sp - (td)->stack; \
372 if (sp_height > (td)->max_stack_height) \
373 (td)->max_stack_height = sp_height; \
374 SET_SIMPLE_TYPE((td)->sp - 1, ty); \
377 #define PUSH_TYPE(td, ty, k) \
381 sp_height = (td)->sp - (td)->stack; \
382 if (sp_height > (td)->max_stack_height) \
383 (td)->max_stack_height = sp_height; \
384 SET_TYPE((td)->sp - 1, ty, k); \
387 #define PUSH_VT(td, size) \
389 (td)->vt_sp += ((size) + 7) & ~7; \
390 if ((td)->vt_sp > (td)->max_vt_sp) \
391 (td)->max_vt_sp = (td)->vt_sp; \
394 #define POP_VT(td, size) \
396 (td)->vt_sp -= ((size) + 7) & ~7; \
399 #if NO_UNALIGNED_ACCESS
400 #define WRITE32(td, v) \
402 ENSURE_CODE(td, 2); \
403 * (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
404 * ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
408 #define WRITE64(td, v) \
410 ENSURE_CODE(td, 4); \
411 * (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
412 * ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
413 * ((guint16 *)((td)->new_ip) + 2) = * ((guint16 *)(v) + 2); \
414 * ((guint16 *)((td)->new_ip) + 3) = * ((guint16 *)(v) + 3); \
418 #define WRITE32(td, v) \
420 ENSURE_CODE(td, 2); \
421 * (guint32 *)((td)->new_ip) = * (guint32 *)(v); \
425 #define WRITE64(td, v) \
427 ENSURE_CODE(td, 4); \
428 * (guint64 *)((td)->new_ip) = * (guint64 *)(v); \
435 load_arg(TransformData *td, int n)
438 MonoClass *klass = NULL;
441 gboolean hasthis = mono_method_signature (td->method)->hasthis;
442 if (hasthis && n == 0)
443 type = &td->method->klass->byval_arg;
445 type = mono_method_signature (td->method)->params [hasthis ? n - 1 : n];
447 mt = mint_type (type);
448 if (mt == MINT_TYPE_VT) {
450 klass = mono_class_from_mono_type (type);
451 if (mono_method_signature (td->method)->pinvoke)
452 size = mono_class_native_size (klass, NULL);
454 size = mono_class_value_size (klass, NULL);
456 if (hasthis && n == 0) {
458 ADD_CODE (td, MINT_LDARG_P);
459 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
463 ADD_CODE (td, MINT_LDARG_VT);
464 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
468 if (hasthis && n == 0) {
470 ADD_CODE (td, MINT_LDARG_P);
471 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
474 ADD_CODE(td, MINT_LDARG_I1 + (mt - MINT_TYPE_I1));
475 ADD_CODE(td, td->rtm->arg_offsets [n]); /* FIX for large offset */
476 if (mt == MINT_TYPE_O)
477 klass = mono_class_from_mono_type (type);
480 PUSH_TYPE(td, stack_type[mt], klass);
484 store_arg(TransformData *td, int n)
490 gboolean hasthis = mono_method_signature (td->method)->hasthis;
491 if (hasthis && n == 0)
492 type = &td->method->klass->byval_arg;
494 type = mono_method_signature (td->method)->params [n - !!hasthis];
496 mt = mint_type (type);
497 if (mt == MINT_TYPE_VT) {
499 g_error ("data.klass");
500 if (mono_method_signature (td->method)->pinvoke)
501 size = mono_class_native_size (type->data.klass, NULL);
503 size = mono_class_value_size (type->data.klass, NULL);
504 ADD_CODE(td, MINT_STARG_VT);
507 if (td->sp [-1].type == STACK_TYPE_VT)
510 ADD_CODE(td, MINT_STARG_I1 + (mt - MINT_TYPE_I1));
511 ADD_CODE(td, td->rtm->arg_offsets [n]);
517 store_inarg(TransformData *td, int n)
520 gboolean hasthis = mono_method_signature (td->method)->hasthis;
521 if (hasthis && n == 0)
522 type = &td->method->klass->byval_arg;
524 type = mono_method_signature (td->method)->params [n - !!hasthis];
526 int mt = mint_type (type);
527 if (hasthis && n == 0) {
528 ADD_CODE (td, MINT_STINARG_P);
532 if (mt == MINT_TYPE_VT) {
533 MonoClass *klass = mono_class_from_mono_type (type);
535 if (mono_method_signature (td->method)->pinvoke)
536 size = mono_class_native_size (klass, NULL);
538 size = mono_class_value_size (klass, NULL);
539 ADD_CODE(td, MINT_STINARG_VT);
543 ADD_CODE(td, MINT_STINARG_I1 + (mt - MINT_TYPE_I1));
549 load_local(TransformData *td, int n)
551 MonoType *type = td->header->locals [n];
552 int mt = mint_type (type);
553 int offset = td->rtm->local_offsets [n];
554 MonoClass *klass = NULL;
555 if (mt == MINT_TYPE_VT) {
556 klass = mono_class_from_mono_type (type);
557 gint32 size = mono_class_value_size (klass, NULL);
559 ADD_CODE(td, MINT_LDLOC_VT);
560 ADD_CODE(td, offset); /*FIX for large offset */
563 g_assert (mt < MINT_TYPE_VT);
564 if (mt == MINT_TYPE_I4 && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
565 td->last_new_ip [0] == MINT_STLOC_I4 && td->last_new_ip [1] == offset) {
566 td->last_new_ip [0] = MINT_STLOC_NP_I4;
567 } else if (mt == MINT_TYPE_O && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
568 td->last_new_ip [0] == MINT_STLOC_O && td->last_new_ip [1] == offset) {
569 td->last_new_ip [0] = MINT_STLOC_NP_O;
571 ADD_CODE(td, MINT_LDLOC_I1 + (mt - MINT_TYPE_I1));
572 ADD_CODE(td, offset); /*FIX for large offset */
574 if (mt == MINT_TYPE_O)
575 klass = mono_class_from_mono_type (type);
577 PUSH_TYPE(td, stack_type[mt], klass);
581 store_local(TransformData *td, int n)
583 MonoType *type = td->header->locals [n];
584 int mt = mint_type (type);
585 int offset = td->rtm->local_offsets [n];
587 #if SIZEOF_VOID_P == 8
588 if (td->sp [-1].type == STACK_TYPE_I4 && stack_type [mt] == STACK_TYPE_I8) {
589 ADD_CODE(td, MINT_CONV_I8_I4);
590 td->sp [-1].type = STACK_TYPE_I8;
593 if (!can_store(td->sp [-1].type, stack_type [mt])) {
594 g_warning("%s.%s: Store local stack type mismatch %d %d",
595 td->method->klass->name, td->method->name,
596 stack_type [mt], td->sp [-1].type);
598 if (mt == MINT_TYPE_VT) {
599 MonoClass *klass = mono_class_from_mono_type (type);
600 gint32 size = mono_class_value_size (klass, NULL);
601 ADD_CODE(td, MINT_STLOC_VT);
602 ADD_CODE(td, offset); /*FIX for large offset */
604 if (td->sp [-1].type == STACK_TYPE_VT)
607 g_assert (mt < MINT_TYPE_VT);
608 ADD_CODE(td, MINT_STLOC_I1 + (mt - MINT_TYPE_I1));
609 ADD_CODE(td, offset); /*FIX for large offset */
614 #define SIMPLE_OP(td, op) \
621 get_data_item_index (TransformData *td, void *ptr)
623 gpointer p = g_hash_table_lookup (td->data_hash, ptr);
626 return GPOINTER_TO_UINT (p) - 1;
627 if (td->max_data_items == td->n_data_items) {
628 td->max_data_items = td->n_data_items == 0 ? 16 : 2 * td->max_data_items;
629 td->data_items = g_realloc (td->data_items, td->max_data_items * sizeof(td->data_items [0]));
631 index = td->n_data_items;
632 td->data_items [index] = ptr;
634 g_hash_table_insert (td->data_hash, ptr, GUINT_TO_POINTER (index + 1));
639 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)
641 MonoImage *image = method->klass->image;
642 MonoMethodSignature *csignature;
644 int virtual = *td->ip == CEE_CALLVIRT;
645 int calli = *td->ip == CEE_CALLI || *td->ip == CEE_MONO_CALLI_EXTRA_ARG;
647 guint32 vt_stack_used = 0;
648 guint32 vt_res_size = 0;
653 guint32 token = read32 (td->ip + 1);
655 if (target_method == NULL) {
658 native = (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE && td->sp [-1].type == STACK_TYPE_I);
660 if (method->wrapper_type != MONO_WRAPPER_NONE)
661 csignature = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
663 csignature = mono_metadata_parse_signature (image, token);
665 if (generic_context) {
666 csignature = mono_inflate_generic_signature (csignature, generic_context, &error);
667 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
670 target_method = NULL;
672 if (method->wrapper_type == MONO_WRAPPER_NONE)
673 target_method = mono_get_method_full (image, token, NULL, generic_context);
675 target_method = (MonoMethod *)mono_method_get_wrapper_data (method, token);
676 csignature = mono_method_signature (target_method);
677 if (target_method->klass == mono_defaults.string_class) {
678 if (target_method->name [0] == 'g') {
679 if (strcmp (target_method->name, "get_Chars") == 0)
681 else if (strcmp (target_method->name, "get_Length") == 0)
684 } else if (target_method->klass == mono_defaults.array_class) {
685 if (strcmp (target_method->name, "get_Rank") == 0)
686 op = MINT_ARRAY_RANK;
687 else if (strcmp (target_method->name, "get_Length") == 0)
689 } else if (target_method && generic_context) {
690 csignature = mono_inflate_generic_signature (csignature, generic_context, &error);
691 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
692 target_method = mono_class_inflate_generic_method_checked (target_method, generic_context, &error);
693 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
697 csignature = mono_method_signature (target_method);
700 if (constrained_class) {
701 if (constrained_class->enumtype && !strcmp (target_method->name, "GetHashCode")) {
702 /* Use the corresponding method from the base type to avoid boxing */
703 MonoType *base_type = mono_class_enum_basetype (constrained_class);
704 g_assert (base_type);
705 constrained_class = mono_class_from_mono_type (base_type);
706 target_method = mono_class_get_method_from_name (constrained_class, target_method->name, 0);
707 g_assert (target_method);
711 if (constrained_class) {
712 mono_class_setup_vtable (constrained_class);
714 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);
716 target_method = mono_get_method_constrained_with_method (image, target_method, constrained_class, generic_context, &error);
718 g_print (" : %s::%s. %s (%p)\n", target_method->klass->name, target_method->name, mono_signature_full_name (target_method->signature), target_method);
720 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
721 mono_class_setup_vtable (target_method->klass);
723 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)) {
724 if (target_method->klass == mono_defaults.enum_class && (td->sp - csignature->param_count - 1)->type == STACK_TYPE_MP) {
725 /* managed pointer on the stack, we need to deref that puppy */
726 ADD_CODE (td, MINT_LDIND_I);
727 ADD_CODE (td, csignature->param_count);
729 ADD_CODE (td, MINT_BOX);
730 ADD_CODE (td, get_data_item_index (td, constrained_class));
731 ADD_CODE (td, csignature->param_count);
732 } else if (!constrained_class->valuetype) {
733 /* managed pointer on the stack, we need to deref that puppy */
734 ADD_CODE (td, MINT_LDIND_I);
735 ADD_CODE (td, csignature->param_count);
737 if (target_method->klass->valuetype) {
740 /* Interface method */
743 mono_class_setup_vtable (constrained_class);
744 ioffset = mono_class_interface_offset (constrained_class, target_method->klass);
746 g_error ("type load error: constrained_class");
747 slot = mono_method_get_vtable_slot (target_method);
749 g_error ("type load error: target_method->klass");
750 target_method = constrained_class->vtable [ioffset + slot];
752 if (target_method->klass == mono_defaults.enum_class) {
753 if ((td->sp - csignature->param_count - 1)->type == STACK_TYPE_MP) {
754 /* managed pointer on the stack, we need to deref that puppy */
755 ADD_CODE (td, MINT_LDIND_I);
756 ADD_CODE (td, csignature->param_count);
758 ADD_CODE (td, MINT_BOX);
759 ADD_CODE (td, get_data_item_index (td, constrained_class));
760 ADD_CODE (td, csignature->param_count);
768 mono_class_init (target_method->klass);
770 CHECK_STACK (td, csignature->param_count + csignature->hasthis);
771 if (!calli && (!virtual || (target_method->flags & METHOD_ATTRIBUTE_VIRTUAL) == 0) &&
772 (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
773 (target_method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) == 0) {
774 int called_inited = mono_class_vtable (domain, target_method->klass)->initialized;
775 MonoMethodHeader *mheader = mono_method_get_header (target_method);
777 if (/*mono_metadata_signature_equal (method->signature, target_method->signature) */ method == target_method && *(td->ip + 5) == CEE_RET) {
779 if (mono_interp_traceopt)
780 g_print ("Optimize tail call of %s.%s\n", target_method->klass->name, target_method->name);
781 for (i = csignature->param_count - 1; i >= 0; --i)
782 store_arg (td, i + csignature->hasthis);
784 if (csignature->hasthis) {
785 g_error ("STTHIS removal");
786 // ADD_CODE(td, MINT_STTHIS);
789 ADD_CODE(td, MINT_BR_S);
790 offset = body_start_offset - ((td->new_ip - 1) - td->new_code);
791 ADD_CODE(td, offset);
792 if (!is_bb_start [td->ip + 5 - td->il_code])
793 ++td->ip; /* gobble the CEE_RET if it isn't branched to */
797 /* mheader might not exist if this is a delegate invoc, etc */
798 if (mheader && *mheader->code == CEE_RET && called_inited) {
799 if (mono_interp_traceopt)
800 g_print ("Inline (empty) call of %s.%s\n", target_method->klass->name, target_method->name);
801 for (i = 0; i < csignature->param_count; i++)
802 ADD_CODE (td, MINT_POP); /*FIX: vt */
804 if (csignature->hasthis) {
806 ADD_CODE(td, MINT_CKNULL);
807 ADD_CODE (td, MINT_POP);
810 td->sp -= csignature->param_count + csignature->hasthis;
816 if (method->wrapper_type == MONO_WRAPPER_NONE && target_method != NULL) {
817 if (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
818 target_method = mono_marshal_get_native_wrapper (target_method, FALSE, FALSE);
819 if (!virtual && target_method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
820 target_method = mono_marshal_get_synchronized_wrapper (target_method);
822 g_assert (csignature->call_convention == MONO_CALL_DEFAULT || csignature->call_convention == MONO_CALL_C);
823 td->sp -= csignature->param_count + !!csignature->hasthis;
824 for (i = 0; i < csignature->param_count; ++i) {
825 if (td->sp [i + !!csignature->hasthis].type == STACK_TYPE_VT) {
827 MonoClass *klass = mono_class_from_mono_type (csignature->params [i]);
828 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
829 size = mono_class_native_size (klass, NULL);
831 size = mono_class_value_size (klass, NULL);
832 size = (size + 7) & ~7;
833 vt_stack_used += size;
837 /* need to handle typedbyref ... */
838 if (csignature->ret->type != MONO_TYPE_VOID) {
839 int mt = mint_type(csignature->ret);
840 MonoClass *klass = mono_class_from_mono_type (csignature->ret);
841 if (mt == MINT_TYPE_VT) {
842 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
843 vt_res_size = mono_class_native_size (klass, NULL);
845 vt_res_size = mono_class_value_size (klass, NULL);
846 PUSH_VT(td, vt_res_size);
848 PUSH_TYPE(td, stack_type[mt], klass);
854 #if SIZEOF_VOID_P == 8
855 if (op == MINT_LDLEN)
856 ADD_CODE(td, MINT_CONV_I4_I8);
860 ADD_CODE(td, native ? MINT_CALLI_NAT : MINT_CALLI);
862 ADD_CODE(td, is_void ? MINT_VCALLVIRT : MINT_CALLVIRT);
864 ADD_CODE(td, is_void ? MINT_VCALL : MINT_CALL);
867 ADD_CODE(td, get_data_item_index (td, (void *)csignature));
869 ADD_CODE(td, get_data_item_index (td, (void *)mono_interp_get_runtime_method (domain, target_method, &error)));
870 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
874 if (vt_stack_used != 0 || vt_res_size != 0) {
875 ADD_CODE(td, MINT_VTRESULT);
876 ADD_CODE(td, vt_res_size);
877 WRITE32(td, &vt_stack_used);
878 td->vt_sp -= vt_stack_used;
883 generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, MonoGenericContext *generic_context)
885 MonoMethodHeader *header = mono_method_get_header (method);
886 MonoMethodSignature *signature = mono_method_signature (method);
887 MonoImage *image = method->klass->image;
888 MonoDomain *domain = mono_domain_get ();
889 MonoClass *constrained_class = NULL;
891 int offset, mt, i, i32;
892 gboolean readonly = FALSE;
894 MonoClassField *field;
895 const unsigned char *end;
896 int new_in_start_offset;
897 int body_start_offset;
901 int generating_code = 1;
903 memset(&td, 0, sizeof(td));
906 td.is_bb_start = is_bb_start;
907 td.il_code = header->code;
908 td.code_size = header->code_size;
910 td.max_code_size = td.code_size;
911 td.new_code = (unsigned short *)g_malloc(td.max_code_size * sizeof(gushort));
912 td.new_code_end = td.new_code + td.max_code_size;
913 td.in_offsets = g_malloc0(header->code_size * sizeof(int));
914 td.forward_refs = g_malloc(header->code_size * sizeof(int));
915 td.stack_state = g_malloc0(header->code_size * sizeof(StackInfo *));
916 td.stack_height = g_malloc(header->code_size * sizeof(int));
917 td.vt_stack_size = g_malloc(header->code_size * sizeof(int));
919 td.max_data_items = 0;
920 td.data_items = NULL;
921 td.data_hash = g_hash_table_new (NULL, NULL);
922 rtm->data_items = td.data_items;
923 for (i = 0; i < header->code_size; i++) {
924 td.forward_refs [i] = -1;
925 td.stack_height [i] = -1;
927 td.new_ip = td.new_code;
928 td.last_new_ip = NULL;
930 td.stack = g_malloc0(header->max_stack * sizeof(td.stack[0]));
932 td.max_stack_height = 0;
934 for (i = 0; i < header->num_clauses; i++) {
935 MonoExceptionClause *c = header->clauses + i;
936 td.stack_height [c->handler_offset] = 0;
937 td.vt_stack_size [c->handler_offset] = 0;
938 td.is_bb_start [c->handler_offset] = 1;
940 td.stack_height [c->handler_offset] = 1;
941 td.stack_state [c->handler_offset] = g_malloc0(sizeof(StackInfo));
942 td.stack_state [c->handler_offset][0].type = STACK_TYPE_O;
943 td.stack_state [c->handler_offset][0].klass = NULL; /*FIX*/
946 td.ip = header->code;
947 end = td.ip + header->code_size;
949 if (mono_interp_traceopt) {
950 char *tmp = mono_disasm_code (NULL, method, td.ip, end);
951 char *name = mono_method_full_name (method, TRUE);
952 g_print ("Method %s, original code:\n", name);
953 g_print ("%s\n", tmp);
958 if (signature->hasthis)
959 store_inarg (&td, 0);
960 for (i = 0; i < signature->param_count; i++)
961 store_inarg (&td, i + !!signature->hasthis);
963 body_start_offset = td.new_ip - td.new_code;
965 for (i = 0; i < header->num_locals; i++) {
966 int mt = mint_type(header->locals [i]);
967 if (mt == MINT_TYPE_VT || mt == MINT_TYPE_O || mt == MINT_TYPE_P) {
968 ADD_CODE(&td, MINT_INITLOCALS);
973 while (td.ip < end) {
976 g_assert (td.sp >= td.stack);
977 g_assert (td.vt_sp < 0x10000000);
978 in_offset = td.ip - header->code;
979 td.in_offsets [in_offset] = td.new_ip - td.new_code;
980 new_in_start_offset = td.new_ip - td.new_code;
982 while (td.forward_refs [in_offset] >= 0) {
983 int j = td.forward_refs [in_offset];
985 td.forward_refs [in_offset] = td.forward_refs [j];
986 if (td.in_offsets [j] < 0) {
987 int old_switch_offset = -td.in_offsets [j];
988 int new_switch_offset = td.in_offsets [old_switch_offset];
989 int switch_case = (j - old_switch_offset - 5) / 4;
990 int n_cases = read32 (header->code + old_switch_offset + 1);
991 offset = (td.new_ip - td.new_code) - (new_switch_offset + 2 * n_cases + 3);
992 slot = new_switch_offset + 3 + 2 * switch_case;
993 td.new_code [slot] = * (unsigned short *)(&offset);
994 td.new_code [slot + 1] = * ((unsigned short *)&offset + 1);
996 int op = td.new_code [td.in_offsets [j]];
997 if (mono_interp_opargtype [op] == MintOpShortBranch) {
998 offset = (td.new_ip - td.new_code) - td.in_offsets [j];
999 g_assert (offset <= 32767);
1000 slot = td.in_offsets [j] + 1;
1001 td.new_code [slot] = offset;
1003 offset = (td.new_ip - td.new_code) - td.in_offsets [j];
1004 slot = td.in_offsets [j] + 1;
1005 td.new_code [slot] = * (unsigned short *)(&offset);
1006 td.new_code [slot + 1] = * ((unsigned short *)&offset + 1);
1010 if (td.stack_height [in_offset] >= 0) {
1011 g_assert (is_bb_start [in_offset]);
1012 if (td.stack_height [in_offset] > 0)
1013 memcpy (td.stack, td.stack_state [in_offset], td.stack_height [in_offset] * sizeof(td.stack [0]));
1014 td.sp = td.stack + td.stack_height [in_offset];
1015 td.vt_sp = td.vt_stack_size [in_offset];
1017 if (is_bb_start [in_offset]) {
1018 generating_code = 1;
1020 if (!generating_code) {
1021 while (td.ip < end && !is_bb_start [td.ip - td.il_code])
1025 if (mono_interp_traceopt > 1) {
1026 printf("IL_%04lx %s %-10s -> IL_%04lx, sp %ld, %s %-12s vt_sp %u (max %u)\n",
1028 td.is_bb_start [td.ip - td.il_code] == 3 ? "<>" :
1029 td.is_bb_start [td.ip - td.il_code] == 2 ? "< " :
1030 td.is_bb_start [td.ip - td.il_code] == 1 ? " >" : " ",
1031 mono_opcode_name (*td.ip), td.new_ip - td.new_code, td.sp - td.stack,
1032 td.sp > td.stack ? stack_type_string [td.sp [-1].type] : " ",
1033 (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) : "",
1034 td.vt_sp, td.max_vt_sp);
1042 SIMPLE_OP(td, MINT_BREAK);
1048 load_arg (&td, *td.ip - CEE_LDARG_0);
1055 load_local (&td, *td.ip - CEE_LDLOC_0);
1062 store_local (&td, *td.ip - CEE_STLOC_0);
1066 load_arg (&td, ((guint8 *)td.ip)[1]);
1069 case CEE_LDARGA_S: {
1070 /* NOTE: n includes this */
1071 int n = ((guint8 *)td.ip)[1];
1072 if (n == 0 && signature->hasthis) {
1073 g_error ("LDTHISA: NOPE");
1074 ADD_CODE(&td, MINT_LDTHISA);
1077 ADD_CODE(&td, MINT_LDARGA);
1078 ADD_CODE(&td, td.rtm->arg_offsets [n]);
1080 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
1085 store_arg (&td, ((guint8 *)td.ip)[1]);
1089 load_local (&td, ((guint8 *)td.ip)[1]);
1093 ADD_CODE(&td, MINT_LDLOCA_S);
1094 ADD_CODE(&td, td.rtm->local_offsets [((guint8 *)td.ip)[1]]);
1095 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
1099 store_local (&td, ((guint8 *)td.ip)[1]);
1103 SIMPLE_OP(td, MINT_LDNULL);
1104 PUSH_TYPE(&td, STACK_TYPE_O, NULL);
1107 SIMPLE_OP(td, MINT_LDC_I4_M1);
1108 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1111 if (!td.is_bb_start[td.ip + 1 - td.il_code] && td.ip [1] == 0xfe && td.ip [2] == CEE_CEQ &&
1112 td.sp > td.stack && td.sp [-1].type == STACK_TYPE_I4) {
1113 SIMPLE_OP(td, MINT_CEQ0_I4);
1116 SIMPLE_OP(td, MINT_LDC_I4_0);
1117 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1121 if (!td.is_bb_start[td.ip + 1 - td.il_code] &&
1122 (td.ip [1] == CEE_ADD || td.ip [1] == CEE_SUB) && td.sp [-1].type == STACK_TYPE_I4) {
1123 ADD_CODE(&td, td.ip [1] == CEE_ADD ? MINT_ADD1_I4 : MINT_SUB1_I4);
1126 SIMPLE_OP(td, MINT_LDC_I4_1);
1127 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1137 SIMPLE_OP(td, (*td.ip - CEE_LDC_I4_0) + MINT_LDC_I4_0);
1138 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1141 ADD_CODE(&td, MINT_LDC_I4_S);
1142 ADD_CODE(&td, ((gint8 *) td.ip) [1]);
1144 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1147 i32 = read32 (td.ip + 1);
1148 ADD_CODE(&td, MINT_LDC_I4);
1151 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1154 gint64 val = read64 (td.ip + 1);
1155 ADD_CODE(&td, MINT_LDC_I8);
1158 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I8);
1163 readr4 (td.ip + 1, &val);
1164 ADD_CODE(&td, MINT_LDC_R4);
1167 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_R8);
1172 readr8 (td.ip + 1, &val);
1173 ADD_CODE(&td, MINT_LDC_R8);
1176 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_R8);
1180 int type = td.sp [-1].type;
1181 MonoClass *klass = td.sp [-1].klass;
1182 if (td.sp [-1].type == STACK_TYPE_VT) {
1183 gint32 size = mono_class_value_size (klass, NULL);
1185 ADD_CODE(&td, MINT_DUP_VT);
1186 WRITE32(&td, &size);
1189 SIMPLE_OP(td, MINT_DUP);
1190 PUSH_TYPE(&td, type, klass);
1194 CHECK_STACK(&td, 1);
1195 SIMPLE_OP(td, MINT_POP);
1197 if (td.sp [-1].type == STACK_TYPE_VT) {
1198 int size = mono_class_value_size (td.sp [-1].klass, NULL);
1199 size = (size + 7) & ~7;
1200 ADD_CODE(&td, MINT_VTRESULT);
1202 WRITE32(&td, &size);
1209 if (td.sp > td.stack)
1210 g_warning ("CEE_JMP: stack must be empty");
1211 token = read32 (td.ip + 1);
1212 m = mono_get_method_full (image, token, NULL, generic_context);
1213 ADD_CODE (&td, MINT_JMP);
1214 ADD_CODE (&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
1215 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1219 case CEE_CALLVIRT: /* Fall through */
1220 case CEE_CALLI: /* Fall through */
1222 interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, constrained_class);
1223 constrained_class = NULL;
1228 if (signature->ret->type != MONO_TYPE_VOID) {
1230 MonoClass *klass = mono_class_from_mono_type (signature->ret);
1231 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1232 vt_size = mono_class_value_size (klass, NULL);
1233 vt_size = (vt_size + 7) & ~7;
1236 if (td.sp > td.stack)
1237 g_warning ("%s.%s: CEE_RET: more values on stack: %d", td.method->klass->name, td.method->name, td.sp - td.stack);
1238 if (td.vt_sp != vt_size)
1239 g_error ("%s.%s: CEE_RET: value type stack: %d vs. %d", td.method->klass->name, td.method->name, td.vt_sp, vt_size);
1241 SIMPLE_OP(td, signature->ret->type == MONO_TYPE_VOID ? MINT_RET_VOID : MINT_RET);
1243 ADD_CODE(&td, MINT_RET_VT);
1244 WRITE32(&td, &vt_size);
1247 generating_code = 0;
1251 handle_branch (&td, MINT_BR_S, MINT_BR, 5 + read32 (td.ip + 1));
1253 generating_code = 0;
1256 handle_branch (&td, MINT_BR_S, MINT_BR, 2 + (gint8)td.ip [1]);
1258 generating_code = 0;
1261 one_arg_branch (&td, MINT_BRFALSE_I4, 5 + read32 (td.ip + 1));
1265 one_arg_branch (&td, MINT_BRFALSE_I4, 2 + (gint8)td.ip [1]);
1269 one_arg_branch (&td, MINT_BRTRUE_I4, 5 + read32 (td.ip + 1));
1273 one_arg_branch (&td, MINT_BRTRUE_I4, 2 + (gint8)td.ip [1]);
1277 two_arg_branch (&td, MINT_BEQ_I4, 5 + read32 (td.ip + 1));
1281 two_arg_branch (&td, MINT_BEQ_I4, 2 + (gint8) td.ip [1]);
1285 two_arg_branch (&td, MINT_BGE_I4, 5 + read32 (td.ip + 1));
1289 two_arg_branch (&td, MINT_BGE_I4, 2 + (gint8) td.ip [1]);
1293 two_arg_branch (&td, MINT_BGT_I4, 5 + read32 (td.ip + 1));
1297 two_arg_branch (&td, MINT_BGT_I4, 2 + (gint8) td.ip [1]);
1301 two_arg_branch (&td, MINT_BLT_I4, 5 + read32 (td.ip + 1));
1305 two_arg_branch (&td, MINT_BLT_I4, 2 + (gint8) td.ip [1]);
1309 two_arg_branch (&td, MINT_BLE_I4, 5 + read32 (td.ip + 1));
1313 two_arg_branch (&td, MINT_BLE_I4, 2 + (gint8) td.ip [1]);
1317 two_arg_branch (&td, MINT_BNE_UN_I4, 5 + read32 (td.ip + 1));
1321 two_arg_branch (&td, MINT_BNE_UN_I4, 2 + (gint8) td.ip [1]);
1325 two_arg_branch (&td, MINT_BGE_UN_I4, 5 + read32 (td.ip + 1));
1329 two_arg_branch (&td, MINT_BGE_UN_I4, 2 + (gint8) td.ip [1]);
1333 two_arg_branch (&td, MINT_BGT_UN_I4, 5 + read32 (td.ip + 1));
1337 two_arg_branch (&td, MINT_BGT_UN_I4, 2 + (gint8) td.ip [1]);
1341 two_arg_branch (&td, MINT_BLE_UN_I4, 5 + read32 (td.ip + 1));
1345 two_arg_branch (&td, MINT_BLE_UN_I4, 2 + (gint8) td.ip [1]);
1349 two_arg_branch (&td, MINT_BLT_UN_I4, 5 + read32 (td.ip + 1));
1353 two_arg_branch (&td, MINT_BLT_UN_I4, 2 + (gint8) td.ip [1]);
1358 const unsigned char *next_ip;
1359 const unsigned char *base_ip = td.ip;
1360 unsigned short *next_new_ip;
1363 ADD_CODE(&td, MINT_SWITCH);
1364 ADD_CODE(&td, * (unsigned short *)(&n));
1365 ADD_CODE(&td, * ((unsigned short *)&n + 1));
1367 next_ip = td.ip + n * 4;
1368 next_new_ip = td.new_ip + n * 2;
1369 for (i = 0; i < n; i++) {
1370 offset = read32 (td.ip);
1371 target = next_ip - td.il_code + offset;
1373 target = td.in_offsets [target] - (next_new_ip - td.new_code);
1375 int prev = td.forward_refs [target];
1376 td.forward_refs [td.ip - td.il_code] = prev;
1377 td.forward_refs [target] = td.ip - td.il_code;
1378 td.in_offsets [td.ip - td.il_code] = - (base_ip - td.il_code);
1380 ADD_CODE(&td, * (unsigned short *)(&target));
1381 ADD_CODE(&td, * ((unsigned short *)&target + 1));
1388 CHECK_STACK (&td, 1);
1389 SIMPLE_OP (td, MINT_LDIND_I1);
1390 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1393 CHECK_STACK (&td, 1);
1394 SIMPLE_OP (td, MINT_LDIND_U1);
1395 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1398 CHECK_STACK (&td, 1);
1399 SIMPLE_OP (td, MINT_LDIND_I2);
1400 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1403 CHECK_STACK (&td, 1);
1404 SIMPLE_OP (td, MINT_LDIND_U2);
1405 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1408 CHECK_STACK (&td, 1);
1409 SIMPLE_OP (td, MINT_LDIND_I4);
1410 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1413 CHECK_STACK (&td, 1);
1414 SIMPLE_OP (td, MINT_LDIND_U4);
1415 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1418 CHECK_STACK (&td, 1);
1419 SIMPLE_OP (td, MINT_LDIND_I8);
1420 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1423 CHECK_STACK (&td, 1);
1424 SIMPLE_OP (td, MINT_LDIND_I);
1426 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1429 CHECK_STACK (&td, 1);
1430 SIMPLE_OP (td, MINT_LDIND_R4);
1431 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1434 CHECK_STACK (&td, 1);
1435 SIMPLE_OP (td, MINT_LDIND_R8);
1436 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1439 CHECK_STACK (&td, 1);
1440 SIMPLE_OP (td, MINT_LDIND_REF);
1441 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
1444 CHECK_STACK (&td, 2);
1445 SIMPLE_OP (td, MINT_STIND_REF);
1449 CHECK_STACK (&td, 2);
1450 SIMPLE_OP (td, MINT_STIND_I1);
1454 CHECK_STACK (&td, 2);
1455 SIMPLE_OP (td, MINT_STIND_I2);
1459 CHECK_STACK (&td, 2);
1460 SIMPLE_OP (td, MINT_STIND_I4);
1464 CHECK_STACK (&td, 2);
1465 SIMPLE_OP (td, MINT_STIND_I);
1469 CHECK_STACK (&td, 2);
1470 SIMPLE_OP (td, MINT_STIND_I8);
1474 CHECK_STACK (&td, 2);
1475 SIMPLE_OP (td, MINT_STIND_R4);
1479 CHECK_STACK (&td, 2);
1480 SIMPLE_OP (td, MINT_STIND_R8);
1484 binary_arith_op(&td, MINT_ADD_I4);
1488 binary_arith_op(&td, MINT_SUB_I4);
1492 binary_arith_op(&td, MINT_MUL_I4);
1496 binary_arith_op(&td, MINT_DIV_I4);
1500 binary_arith_op(&td, MINT_DIV_UN_I4);
1504 binary_int_op (&td, MINT_REM_I4);
1508 binary_int_op (&td, MINT_REM_UN_I4);
1512 binary_int_op (&td, MINT_AND_I4);
1516 binary_int_op (&td, MINT_OR_I4);
1520 binary_int_op (&td, MINT_XOR_I4);
1524 shift_op (&td, MINT_SHL_I4);
1528 shift_op (&td, MINT_SHR_I4);
1532 shift_op (&td, MINT_SHR_UN_I4);
1536 unary_arith_op (&td, MINT_NEG_I4);
1540 unary_arith_op (&td, MINT_NOT_I4);
1544 CHECK_STACK (&td, 1);
1545 switch (td.sp [-1].type) {
1547 ADD_CODE(&td, MINT_CONV_U1_R8);
1550 ADD_CODE(&td, MINT_CONV_U1_I4);
1553 ADD_CODE(&td, MINT_CONV_U1_I8);
1556 g_assert_not_reached ();
1559 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1562 CHECK_STACK (&td, 1);
1563 switch (td.sp [-1].type) {
1565 ADD_CODE(&td, MINT_CONV_I1_R8);
1568 ADD_CODE(&td, MINT_CONV_I1_I4);
1571 ADD_CODE(&td, MINT_CONV_I1_I8);
1574 g_assert_not_reached ();
1577 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1580 CHECK_STACK (&td, 1);
1581 switch (td.sp [-1].type) {
1583 ADD_CODE(&td, MINT_CONV_U2_R8);
1586 ADD_CODE(&td, MINT_CONV_U2_I4);
1589 ADD_CODE(&td, MINT_CONV_U2_I8);
1592 g_assert_not_reached ();
1595 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1598 CHECK_STACK (&td, 1);
1599 switch (td.sp [-1].type) {
1601 ADD_CODE(&td, MINT_CONV_I2_R8);
1604 ADD_CODE(&td, MINT_CONV_I2_I4);
1607 ADD_CODE(&td, MINT_CONV_I2_I8);
1610 g_assert_not_reached ();
1613 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1616 CHECK_STACK (&td, 1);
1617 switch (td.sp [-1].type) {
1619 #if SIZEOF_VOID_P == 4
1620 ADD_CODE(&td, MINT_CONV_U4_R8);
1622 ADD_CODE(&td, MINT_CONV_U8_R8);
1626 #if SIZEOF_VOID_P == 8
1627 ADD_CODE(&td, MINT_CONV_U8_I4);
1631 #if SIZEOF_VOID_P == 4
1632 ADD_CODE(&td, MINT_CONV_U4_I8);
1638 g_assert_not_reached ();
1641 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1644 CHECK_STACK (&td, 1);
1645 switch (td.sp [-1].type) {
1647 #if SIZEOF_VOID_P == 8
1648 ADD_CODE(&td, MINT_CONV_I8_R8);
1650 ADD_CODE(&td, MINT_CONV_I4_R8);
1654 #if SIZEOF_VOID_P == 8
1655 ADD_CODE(&td, MINT_CONV_I8_I4);
1663 #if SIZEOF_VOID_P == 4
1664 ADD_CODE(&td, MINT_CONV_I4_I8);
1668 g_assert_not_reached ();
1671 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1674 CHECK_STACK (&td, 1);
1675 switch (td.sp [-1].type) {
1677 ADD_CODE(&td, MINT_CONV_U4_R8);
1682 ADD_CODE(&td, MINT_CONV_U4_I8);
1685 #if SIZEOF_VOID_P == 8
1686 ADD_CODE(&td, MINT_CONV_U4_I8);
1690 g_assert_not_reached ();
1693 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1696 CHECK_STACK (&td, 1);
1697 switch (td.sp [-1].type) {
1699 ADD_CODE(&td, MINT_CONV_I4_R8);
1704 ADD_CODE(&td, MINT_CONV_I4_I8);
1707 #if SIZEOF_VOID_P == 8
1708 ADD_CODE(&td, MINT_CONV_I4_I8);
1712 g_assert_not_reached ();
1715 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1718 CHECK_STACK (&td, 1);
1719 switch (td.sp [-1].type) {
1721 ADD_CODE(&td, MINT_CONV_I8_R8);
1724 ADD_CODE(&td, MINT_CONV_I8_I4);
1729 #if SIZEOF_VOID_P == 4
1730 ADD_CODE(&td, MINT_CONV_I8_I4);
1734 g_assert_not_reached ();
1737 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1740 CHECK_STACK (&td, 1);
1741 switch (td.sp [-1].type) {
1743 ADD_CODE(&td, MINT_CONV_R4_R8);
1746 ADD_CODE(&td, MINT_CONV_R4_I8);
1749 ADD_CODE(&td, MINT_CONV_R4_I4);
1752 g_assert_not_reached ();
1755 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1758 CHECK_STACK (&td, 1);
1759 switch (td.sp [-1].type) {
1761 ADD_CODE(&td, MINT_CONV_R8_I4);
1764 ADD_CODE(&td, MINT_CONV_R8_I8);
1769 g_assert_not_reached ();
1772 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1775 CHECK_STACK (&td, 1);
1776 switch (td.sp [-1].type) {
1778 ADD_CODE(&td, MINT_CONV_U8_I4);
1783 ADD_CODE(&td, MINT_CONV_U8_R8);
1786 #if SIZEOF_VOID_P == 4
1787 ADD_CODE(&td, MINT_CONV_U8_I4);
1791 g_assert_not_reached ();
1794 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1800 vtklass = mono_class_get_full (image, read32 (ip), generic_context);
1803 memcpy (sp [0].data.p, sp [1].data.p, mono_class_value_size (vtklass, NULL));
1809 CHECK_STACK (&td, 1);
1811 token = read32 (td.ip + 1);
1813 if (method->wrapper_type != MONO_WRAPPER_NONE)
1814 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
1816 klass = mono_class_get_full (image, token, generic_context);
1818 ADD_CODE(&td, MINT_LDOBJ);
1819 ADD_CODE(&td, get_data_item_index(&td, klass));
1820 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1821 size = mono_class_value_size (klass, NULL);
1825 SET_TYPE(td.sp - 1, stack_type[mint_type(&klass->byval_arg)], klass);
1830 token = mono_metadata_token_index (read32 (td.ip + 1));
1832 if (method->wrapper_type != MONO_WRAPPER_NONE) {
1833 s = mono_string_new_wrapper(
1834 mono_method_get_wrapper_data (method, token));
1837 s = mono_ldstr (domain, image, token);
1838 ADD_CODE(&td, MINT_LDSTR);
1839 ADD_CODE(&td, get_data_item_index (&td, s));
1840 PUSH_TYPE(&td, STACK_TYPE_O, mono_defaults.string_class);
1845 MonoMethodSignature *csignature;
1846 guint32 vt_stack_used = 0;
1847 guint32 vt_res_size = 0;
1850 token = read32 (td.ip);
1853 if (method->wrapper_type != MONO_WRAPPER_NONE)
1854 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
1856 m = mono_get_method_full (image, token, NULL, generic_context);
1858 csignature = mono_method_signature (m);
1860 td.sp -= csignature->param_count;
1861 ADD_CODE(&td, MINT_NEWOBJ);
1862 ADD_CODE(&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
1863 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1865 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1866 vt_res_size = mono_class_value_size (klass, NULL);
1867 PUSH_VT (&td, vt_res_size);
1869 for (i = 0; i < csignature->param_count; ++i) {
1870 int mt = mint_type(csignature->params [i]);
1871 if (mt == MINT_TYPE_VT) {
1872 MonoClass *k = mono_class_from_mono_type (csignature->params [i]);
1873 gint32 size = mono_class_value_size (k, NULL);
1874 size = (size + 7) & ~7;
1875 vt_stack_used += size;
1878 if (vt_stack_used != 0 || vt_res_size != 0) {
1879 ADD_CODE(&td, MINT_VTRESULT);
1880 ADD_CODE(&td, vt_res_size);
1881 WRITE32(&td, &vt_stack_used);
1882 td.vt_sp -= vt_stack_used;
1884 PUSH_TYPE (&td, stack_type [mint_type (&klass->byval_arg)], klass);
1888 CHECK_STACK (&td, 1);
1889 token = read32 (td.ip + 1);
1890 klass = mono_class_get_full (image, token, generic_context);
1891 ADD_CODE(&td, MINT_CASTCLASS);
1892 ADD_CODE(&td, get_data_item_index (&td, klass));
1893 td.sp [-1].klass = klass;
1897 CHECK_STACK (&td, 1);
1898 token = read32 (td.ip + 1);
1899 klass = mono_class_get_full (image, token, generic_context);
1900 ADD_CODE(&td, MINT_ISINST);
1901 ADD_CODE(&td, get_data_item_index (&td, klass));
1905 switch (td.sp [-1].type) {
1909 ADD_CODE(&td, MINT_CONV_R_UN_I8);
1912 ADD_CODE(&td, MINT_CONV_R_UN_I4);
1915 g_assert_not_reached ();
1917 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1921 CHECK_STACK (&td, 1);
1922 token = read32 (td.ip + 1);
1924 if (method->wrapper_type != MONO_WRAPPER_NONE)
1925 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
1927 klass = mono_class_get_full (image, token, generic_context);
1929 if (mono_class_is_nullable (klass)) {
1930 g_error ("cee_unbox: implement Nullable");
1933 ADD_CODE(&td, MINT_UNBOX);
1934 ADD_CODE(&td, get_data_item_index (&td, klass));
1935 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
1939 CHECK_STACK (&td, 1);
1940 token = read32 (td.ip + 1);
1942 g_assert (method->wrapper_type == MONO_WRAPPER_NONE);
1943 klass = mono_class_get_full (image, token, generic_context);
1945 if (mini_type_is_reference (&klass->byval_arg)) {
1946 g_error ("unbox_any: generic class is reference type");
1947 } else if (mono_class_is_nullable (klass)) {
1948 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Unbox", 1);
1949 /* td.ip is incremented by interp_transform_call */
1950 interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL);
1952 int mt = mint_type (&klass->byval_arg);
1953 ADD_CODE (&td, MINT_UNBOX);
1954 ADD_CODE (&td, get_data_item_index (&td, klass));
1956 ADD_CODE (&td, MINT_LDOBJ);
1957 ADD_CODE (&td, get_data_item_index(&td, klass));
1958 SET_TYPE (td.sp - 1, stack_type [mt], klass);
1960 if (mt == MINT_TYPE_VT) {
1961 int size = mono_class_value_size (klass, NULL);
1962 PUSH_VT (&td, size);
1969 CHECK_STACK (&td, 1);
1970 SIMPLE_OP (td, MINT_THROW);
1972 generating_code = 0;
1975 CHECK_STACK (&td, 1);
1976 token = read32 (td.ip + 1);
1977 field = mono_field_from_token (image, token, &klass, generic_context);
1978 mono_class_init (klass);
1979 mt = mint_type(field->type);
1980 ADD_CODE(&td, MINT_LDFLDA);
1981 ADD_CODE(&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
1983 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
1986 CHECK_STACK (&td, 1);
1987 token = read32 (td.ip + 1);
1988 field = mono_field_from_token (image, token, &klass, generic_context);
1989 mono_class_init (klass);
1991 MonoClass *field_klass = mono_class_from_mono_type (field->type);
1992 mt = mint_type (&field_klass->byval_arg);
1993 if (klass->marshalbyref) {
1994 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_LDRMFLD_VT : MINT_LDRMFLD);
1995 ADD_CODE(&td, get_data_item_index (&td, field));
1997 ADD_CODE(&td, MINT_LDFLD_I1 + mt - MINT_TYPE_I1);
1998 ADD_CODE(&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
2000 if (mt == MINT_TYPE_VT) {
2001 int size = mono_class_value_size (field_klass, NULL);
2003 WRITE32(&td, &size);
2005 if (td.sp [-1].type == STACK_TYPE_VT) {
2006 int size = mono_class_value_size (klass, NULL);
2007 size = (size + 7) & ~7;
2009 ADD_CODE (&td, MINT_VTRESULT);
2011 WRITE32 (&td, &size);
2014 SET_TYPE(td.sp - 1, stack_type [mt], field_klass);
2018 CHECK_STACK (&td, 2);
2019 token = read32 (td.ip + 1);
2020 field = mono_field_from_token (image, token, &klass, generic_context);
2021 mono_class_init (klass);
2022 mt = mint_type(field->type);
2023 if (klass->marshalbyref) {
2024 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STRMFLD_VT : MINT_STRMFLD);
2025 ADD_CODE(&td, get_data_item_index (&td, field));
2027 ADD_CODE(&td, MINT_STFLD_I1 + mt - MINT_TYPE_I1);
2028 ADD_CODE(&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
2030 if (mt == MINT_TYPE_VT) {
2031 MonoClass *klass = mono_class_from_mono_type (field->type);
2032 int size = mono_class_value_size (klass, NULL);
2034 WRITE32(&td, &size);
2040 token = read32 (td.ip + 1);
2041 field = mono_field_from_token (image, token, &klass, generic_context);
2042 ADD_CODE(&td, MINT_LDSFLDA);
2043 ADD_CODE(&td, get_data_item_index (&td, field));
2045 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
2048 token = read32 (td.ip + 1);
2049 field = mono_field_from_token (image, token, &klass, generic_context);
2050 mt = mint_type(field->type);
2051 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2052 ADD_CODE(&td, get_data_item_index (&td, field));
2054 if (mt == MINT_TYPE_VT) {
2055 MonoClass *klass = mono_class_from_mono_type (field->type);
2056 int size = mono_class_value_size (klass, NULL);
2058 WRITE32(&td, &size);
2059 klass = field->type->data.klass;
2061 if (mt == MINT_TYPE_O)
2062 klass = mono_class_from_mono_type (field->type);
2065 PUSH_TYPE(&td, stack_type [mt], klass);
2068 CHECK_STACK (&td, 1);
2069 token = read32 (td.ip + 1);
2070 field = mono_field_from_token (image, token, &klass, generic_context);
2071 mt = mint_type(field->type);
2072 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
2073 ADD_CODE(&td, get_data_item_index (&td, field));
2074 if (mt == MINT_TYPE_VT) {
2075 MonoClass *klass = mono_class_from_mono_type (field->type);
2076 int size = mono_class_value_size (klass, NULL);
2078 WRITE32 (&td, &size);
2085 token = read32 (td.ip + 1);
2087 if (method->wrapper_type != MONO_WRAPPER_NONE)
2088 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2090 klass = mono_class_get_full (image, token, generic_context);
2092 ADD_CODE(&td, td.sp [-1].type == STACK_TYPE_VT ? MINT_STOBJ_VT : MINT_STOBJ);
2093 ADD_CODE(&td, get_data_item_index (&td, klass));
2094 if (td.sp [-1].type == STACK_TYPE_VT) {
2095 size = mono_class_value_size (klass, NULL);
2096 size = (size + 7) & ~7;
2103 case CEE_CONV_OVF_I_UN:
2104 case CEE_CONV_OVF_U_UN:
2105 CHECK_STACK (&td, 1);
2106 switch (td.sp [-1].type) {
2108 #if SIZEOF_VOID_P == 8
2109 ADD_CODE(&td, MINT_CONV_OVF_I8_UN_R8);
2111 ADD_CODE(&td, MINT_CONV_OVF_I4_UN_R8);
2115 #if SIZEOF_VOID_P == 4
2116 ADD_CODE (&td, MINT_CONV_OVF_I4_UN_I8);
2120 #if SIZEOF_VOID_P == 8
2121 ADD_CODE(&td, MINT_CONV_I8_U4);
2125 g_assert_not_reached ();
2128 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2131 case CEE_CONV_OVF_I8_UN:
2132 case CEE_CONV_OVF_U8_UN:
2133 CHECK_STACK (&td, 1);
2134 switch (td.sp [-1].type) {
2136 ADD_CODE(&td, MINT_CONV_OVF_I8_UN_R8);
2141 ADD_CODE(&td, MINT_CONV_I8_U4);
2144 g_assert_not_reached ();
2147 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2152 CHECK_STACK (&td, 1);
2153 token = read32 (td.ip + 1);
2154 if (method->wrapper_type != MONO_WRAPPER_NONE)
2155 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2157 klass = mono_class_get_full (image, token, generic_context);
2159 if (mono_class_is_nullable (klass)) {
2160 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Box", 1);
2161 /* td.ip is incremented by interp_transform_call */
2162 interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL);
2163 } else if (!klass->valuetype) {
2164 /* already boxed, do nothing. */
2167 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT && !klass->enumtype) {
2168 size = mono_class_value_size (klass, NULL);
2169 size = (size + 7) & ~7;
2172 ADD_CODE(&td, MINT_BOX);
2173 ADD_CODE(&td, get_data_item_index (&td, klass));
2175 SET_TYPE(td.sp - 1, STACK_TYPE_O, klass);
2182 CHECK_STACK (&td, 1);
2183 token = read32 (td.ip + 1);
2185 if (method->wrapper_type != MONO_WRAPPER_NONE)
2186 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2188 klass = mono_class_get_full (image, token, generic_context);
2190 ADD_CODE(&td, MINT_NEWARR);
2191 ADD_CODE(&td, get_data_item_index (&td, klass));
2192 SET_TYPE(td.sp - 1, STACK_TYPE_O, klass);
2196 CHECK_STACK (&td, 1);
2197 SIMPLE_OP (td, MINT_LDLEN);
2198 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2201 CHECK_STACK (&td, 2);
2203 token = read32 (td.ip + 1);
2205 if (method->wrapper_type != MONO_WRAPPER_NONE)
2206 klass = (MonoClass *) mono_method_get_wrapper_data (method, token);
2208 klass = mono_class_get_full (image, token, generic_context);
2210 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
2211 ADD_CODE (&td, MINT_LDELEMA_TC);
2213 ADD_CODE (&td, MINT_LDELEMA);
2215 ADD_CODE(&td, get_data_item_index (&td, klass));
2220 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2223 CHECK_STACK (&td, 2);
2225 SIMPLE_OP (td, MINT_LDELEM_I1);
2227 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2230 CHECK_STACK (&td, 2);
2232 SIMPLE_OP (td, MINT_LDELEM_U1);
2234 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2237 CHECK_STACK (&td, 2);
2239 SIMPLE_OP (td, MINT_LDELEM_I2);
2241 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2244 CHECK_STACK (&td, 2);
2246 SIMPLE_OP (td, MINT_LDELEM_U2);
2248 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2251 CHECK_STACK (&td, 2);
2253 SIMPLE_OP (td, MINT_LDELEM_I4);
2255 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2258 CHECK_STACK (&td, 2);
2260 SIMPLE_OP (td, MINT_LDELEM_U4);
2262 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2265 CHECK_STACK (&td, 2);
2267 SIMPLE_OP (td, MINT_LDELEM_I8);
2269 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2272 CHECK_STACK (&td, 2);
2274 SIMPLE_OP (td, MINT_LDELEM_I);
2276 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2279 CHECK_STACK (&td, 2);
2281 SIMPLE_OP (td, MINT_LDELEM_R4);
2283 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2286 CHECK_STACK (&td, 2);
2288 SIMPLE_OP (td, MINT_LDELEM_R8);
2290 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2292 case CEE_LDELEM_REF:
2293 CHECK_STACK (&td, 2);
2295 SIMPLE_OP (td, MINT_LDELEM_REF);
2297 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
2300 CHECK_STACK (&td, 2);
2301 token = read32 (td.ip + 1);
2302 klass = mono_class_get_full (image, token, generic_context);
2303 switch (mint_type (&klass->byval_arg)) {
2306 SIMPLE_OP (td, MINT_LDELEM_I1);
2308 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2312 SIMPLE_OP (td, MINT_LDELEM_U1);
2314 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2318 SIMPLE_OP (td, MINT_LDELEM_U2);
2320 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2324 SIMPLE_OP (td, MINT_LDELEM_I2);
2326 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2330 SIMPLE_OP (td, MINT_LDELEM_I4);
2332 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2336 SIMPLE_OP (td, MINT_LDELEM_I8);
2338 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2342 SIMPLE_OP (td, MINT_LDELEM_R4);
2344 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2348 SIMPLE_OP (td, MINT_LDELEM_R8);
2350 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2354 SIMPLE_OP (td, MINT_LDELEM_REF);
2356 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
2358 case MINT_TYPE_VT: {
2359 int size = mono_class_value_size (klass, NULL);
2361 SIMPLE_OP (td, MINT_LDELEM_VT);
2362 ADD_CODE (&td, get_data_item_index (&td, klass));
2363 WRITE32 (&td, &size);
2365 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_VT);
2366 PUSH_VT (&td, size);
2370 GString *res = g_string_new ("");
2371 mono_type_get_desc (res, &klass->byval_arg, TRUE);
2372 g_print ("LDELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
2373 g_string_free (res, TRUE);
2381 CHECK_STACK (&td, 3);
2383 SIMPLE_OP (td, MINT_STELEM_I);
2387 CHECK_STACK (&td, 3);
2389 SIMPLE_OP (td, MINT_STELEM_I1);
2393 CHECK_STACK (&td, 3);
2395 SIMPLE_OP (td, MINT_STELEM_I2);
2399 CHECK_STACK (&td, 3);
2401 SIMPLE_OP (td, MINT_STELEM_I4);
2405 CHECK_STACK (&td, 3);
2407 SIMPLE_OP (td, MINT_STELEM_I8);
2411 CHECK_STACK (&td, 3);
2413 SIMPLE_OP (td, MINT_STELEM_R4);
2417 CHECK_STACK (&td, 3);
2419 SIMPLE_OP (td, MINT_STELEM_R8);
2422 case CEE_STELEM_REF:
2423 CHECK_STACK (&td, 3);
2425 SIMPLE_OP (td, MINT_STELEM_REF);
2429 CHECK_STACK (&td, 3);
2431 token = read32 (td.ip + 1);
2432 klass = mono_class_get_full (image, token, generic_context);
2433 switch (mint_type (&klass->byval_arg)) {
2435 SIMPLE_OP (td, MINT_STELEM_I4);
2438 SIMPLE_OP (td, MINT_STELEM_REF);
2440 case MINT_TYPE_VT: {
2441 int size = mono_class_value_size (klass, NULL);
2442 SIMPLE_OP (td, MINT_STELEM_VT);
2443 ADD_CODE (&td, get_data_item_index (&td, klass));
2444 WRITE32 (&td, &size);
2449 GString *res = g_string_new ("");
2450 mono_type_get_desc (res, &klass->byval_arg, TRUE);
2451 g_print ("STELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
2452 g_string_free (res, TRUE);
2461 case CEE_CONV_OVF_U1:
2463 case CEE_CONV_OVF_I8:
2465 #if SIZEOF_VOID_P == 8
2466 case CEE_CONV_OVF_U:
2468 case CEE_REFANYVAL: ves_abort(); break;
2471 CHECK_STACK (&td, 1);
2472 SIMPLE_OP (td, MINT_CKFINITE);
2474 case CEE_CONV_OVF_I1:
2475 case CEE_CONV_OVF_I1_UN:
2476 CHECK_STACK (&td, 1);
2477 switch (td.sp [-1].type) {
2479 ADD_CODE(&td, MINT_CONV_OVF_I1_R8);
2482 ADD_CODE(&td, MINT_CONV_OVF_I1_I4);
2485 ADD_CODE(&td, MINT_CONV_OVF_I1_I8);
2488 g_assert_not_reached ();
2491 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2493 case CEE_CONV_OVF_U1:
2494 case CEE_CONV_OVF_U1_UN:
2495 CHECK_STACK (&td, 1);
2496 switch (td.sp [-1].type) {
2498 ADD_CODE(&td, MINT_CONV_OVF_U1_R8);
2501 ADD_CODE(&td, MINT_CONV_OVF_U1_I4);
2504 ADD_CODE(&td, MINT_CONV_OVF_U1_I8);
2507 g_assert_not_reached ();
2510 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2512 case CEE_CONV_OVF_I2:
2513 case CEE_CONV_OVF_I2_UN:
2514 CHECK_STACK (&td, 1);
2515 switch (td.sp [-1].type) {
2517 ADD_CODE(&td, MINT_CONV_OVF_I2_R8);
2520 ADD_CODE(&td, MINT_CONV_OVF_I2_I4);
2523 ADD_CODE(&td, MINT_CONV_OVF_I2_I8);
2526 g_assert_not_reached ();
2529 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2531 case CEE_CONV_OVF_U2_UN:
2532 case CEE_CONV_OVF_U2:
2533 CHECK_STACK (&td, 1);
2534 switch (td.sp [-1].type) {
2536 ADD_CODE(&td, MINT_CONV_OVF_U2_R8);
2539 ADD_CODE(&td, MINT_CONV_OVF_U2_I4);
2542 ADD_CODE(&td, MINT_CONV_OVF_U2_I8);
2545 g_assert_not_reached ();
2548 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2550 #if SIZEOF_VOID_P == 4
2551 case CEE_CONV_OVF_I:
2553 case CEE_CONV_OVF_I4:
2554 case CEE_CONV_OVF_I4_UN:
2555 CHECK_STACK (&td, 1);
2556 switch (td.sp [-1].type) {
2558 ADD_CODE(&td, MINT_CONV_OVF_I4_R8);
2561 if (*td.ip == CEE_CONV_OVF_I4_UN)
2562 ADD_CODE(&td, MINT_CONV_OVF_I4_U4);
2565 ADD_CODE(&td, MINT_CONV_OVF_I4_I8);
2568 g_assert_not_reached ();
2571 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2573 #if SIZEOF_VOID_P == 4
2574 case CEE_CONV_OVF_U:
2576 case CEE_CONV_OVF_U4:
2577 case CEE_CONV_OVF_U4_UN:
2578 CHECK_STACK (&td, 1);
2579 switch (td.sp [-1].type) {
2581 ADD_CODE(&td, MINT_CONV_OVF_U4_R8);
2584 if (*td.ip != CEE_CONV_OVF_U4_UN)
2585 ADD_CODE(&td, MINT_CONV_OVF_U4_I4);
2588 ADD_CODE(&td, MINT_CONV_OVF_U4_I8);
2591 g_assert_not_reached ();
2594 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2596 #if SIZEOF_VOID_P == 8
2597 case CEE_CONV_OVF_I:
2599 case CEE_CONV_OVF_I8:
2600 CHECK_STACK (&td, 1);
2601 switch (td.sp [-1].type) {
2603 ADD_CODE(&td, MINT_CONV_OVF_I8_R8);
2606 ADD_CODE(&td, MINT_CONV_I8_I4);
2611 g_assert_not_reached ();
2614 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2616 #if SIZEOF_VOID_P == 8
2617 case CEE_CONV_OVF_U:
2619 case CEE_CONV_OVF_U8:
2620 CHECK_STACK (&td, 1);
2621 switch (td.sp [-1].type) {
2623 ADD_CODE(&td, MINT_CONV_OVF_U8_R8);
2626 ADD_CODE(&td, MINT_CONV_OVF_U8_I4);
2631 g_assert_not_reached ();
2634 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2639 token = read32 (td.ip + 1);
2640 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
2641 handle = mono_method_get_wrapper_data (method, token);
2642 klass = (MonoClass *) mono_method_get_wrapper_data (method, token + 1);
2643 if (klass == mono_defaults.typehandle_class)
2644 handle = &((MonoClass *) handle)->byval_arg;
2646 handle = mono_ldtoken (image, token, &klass, generic_context);
2648 mt = mint_type(&klass->byval_arg);
2649 g_assert (mt == MINT_TYPE_VT);
2650 size = mono_class_value_size (klass, NULL);
2651 g_assert (size == sizeof(gpointer));
2652 PUSH_VT(&td, sizeof(gpointer));
2653 ADD_CODE(&td, MINT_LDTOKEN);
2654 ADD_CODE(&td, get_data_item_index (&td, handle));
2655 PUSH_SIMPLE_TYPE(&td, stack_type [mt]);
2660 binary_arith_op(&td, MINT_ADD_OVF_I4);
2663 case CEE_ADD_OVF_UN:
2664 binary_arith_op(&td, MINT_ADD_OVF_UN_I4);
2668 binary_arith_op(&td, MINT_MUL_OVF_I4);
2671 case CEE_MUL_OVF_UN:
2672 binary_arith_op(&td, MINT_MUL_OVF_UN_I4);
2676 binary_arith_op(&td, MINT_SUB_OVF_I4);
2679 case CEE_SUB_OVF_UN:
2680 binary_arith_op(&td, MINT_SUB_OVF_UN_I4);
2683 case CEE_ENDFINALLY:
2684 SIMPLE_OP (td, MINT_ENDFINALLY);
2685 generating_code = 0;
2689 handle_branch (&td, MINT_LEAVE_S, MINT_LEAVE, 5 + read32 (td.ip + 1));
2691 generating_code = 0;
2695 handle_branch (&td, MINT_LEAVE_S, MINT_LEAVE, 2 + (gint8)td.ip [1]);
2697 generating_code = 0;
2702 case CEE_MONO_CALLI_EXTRA_ARG:
2703 /* Same as CEE_CALLI, except that we drop the extra arg required for llvm specific behaviour */
2704 ADD_CODE (&td, MINT_POP);
2707 interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, NULL);
2709 case CEE_MONO_JIT_ICALL_ADDR: {
2712 MonoJitICallInfo *info;
2714 token = read32 (td.ip + 1);
2716 func = mono_method_get_wrapper_data (method, token);
2717 info = mono_find_jit_icall_by_addr (func);
2719 ADD_CODE (&td, MINT_LDFTN);
2720 ADD_CODE (&td, get_data_item_index (&td, func));
2721 PUSH_SIMPLE_TYPE (&td, STACK_TYPE_I);
2724 case CEE_MONO_ICALL: {
2727 MonoJitICallInfo *info;
2729 token = read32 (td.ip + 1);
2731 func = mono_method_get_wrapper_data (method, token);
2732 info = mono_find_jit_icall_by_addr (func);
2735 CHECK_STACK (&td, info->sig->param_count);
2736 switch (info->sig->param_count) {
2738 if (MONO_TYPE_IS_VOID (info->sig->ret))
2739 ADD_CODE (&td,MINT_ICALL_V_V);
2741 ADD_CODE (&td, MINT_ICALL_V_P);
2744 if (MONO_TYPE_IS_VOID (info->sig->ret))
2745 ADD_CODE (&td,MINT_ICALL_P_V);
2747 ADD_CODE (&td,MINT_ICALL_P_P);
2750 if (MONO_TYPE_IS_VOID (info->sig->ret)) {
2751 if (info->sig->params [1]->type == MONO_TYPE_I4)
2752 ADD_CODE (&td,MINT_ICALL_PI_V);
2754 ADD_CODE (&td,MINT_ICALL_PP_V);
2756 if (info->sig->params [1]->type == MONO_TYPE_I4)
2757 ADD_CODE (&td,MINT_ICALL_PI_P);
2759 ADD_CODE (&td,MINT_ICALL_PP_P);
2763 g_assert (MONO_TYPE_IS_VOID (info->sig->ret));
2764 if (info->sig->params [2]->type == MONO_TYPE_I4)
2765 ADD_CODE (&td,MINT_ICALL_PPI_V);
2767 ADD_CODE (&td,MINT_ICALL_PPP_V);
2770 g_assert_not_reached ();
2773 if (func == mono_ftnptr_to_delegate) {
2774 g_error ("TODO: ?");
2776 ADD_CODE(&td, get_data_item_index (&td, func));
2777 td.sp -= info->sig->param_count;
2779 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2781 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2785 case CEE_MONO_VTADDR: {
2787 CHECK_STACK (&td, 1);
2788 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
2789 size = mono_class_native_size(td.sp [-1].klass, NULL);
2791 size = mono_class_value_size(td.sp [-1].klass, NULL);
2792 size = (size + 7) & ~7;
2793 ADD_CODE(&td, MINT_VTRESULT);
2795 WRITE32(&td, &size);
2798 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2801 case CEE_MONO_LDPTR:
2802 case CEE_MONO_CLASSCONST:
2803 token = read32 (td.ip + 1);
2805 ADD_CODE(&td, MINT_MONO_LDPTR);
2806 ADD_CODE(&td, get_data_item_index (&td, mono_method_get_wrapper_data (method, token)));
2807 td.sp [0].type = STACK_TYPE_I;
2810 case CEE_MONO_OBJADDR:
2811 CHECK_STACK (&td, 1);
2813 td.sp[-1].type = STACK_TYPE_MP;
2816 case CEE_MONO_NEWOBJ:
2817 token = read32 (td.ip + 1);
2819 ADD_CODE(&td, MINT_MONO_NEWOBJ);
2820 ADD_CODE(&td, get_data_item_index (&td, mono_method_get_wrapper_data (method, token)));
2821 td.sp [0].type = STACK_TYPE_O;
2824 case CEE_MONO_RETOBJ:
2825 CHECK_STACK (&td, 1);
2826 token = read32 (td.ip + 1);
2828 ADD_CODE(&td, MINT_MONO_RETOBJ);
2831 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2833 /*stackval_from_data (signature->ret, frame->retval, sp->data.vt, signature->pinvoke);*/
2835 if (td.sp > td.stack)
2836 g_warning ("CEE_MONO_RETOBJ: more values on stack: %d", td.sp-td.stack);
2838 case CEE_MONO_LDNATIVEOBJ:
2839 token = read32 (td.ip + 1);
2841 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2842 g_assert(klass->valuetype);
2843 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2845 case CEE_MONO_SAVE_LMF:
2846 case CEE_MONO_RESTORE_LMF:
2847 case CEE_MONO_NOT_TAKEN:
2850 case CEE_MONO_LDPTR_INT_REQ_FLAG:
2851 ADD_CODE (&td, MINT_MONO_LDPTR);
2852 ADD_CODE (&td, get_data_item_index (&td, mono_thread_interruption_request_flag ()));
2853 PUSH_TYPE (&td, STACK_TYPE_MP, NULL);
2857 g_error ("transform.c: Unimplemented opcode: 0xF0 %02x at 0x%x\n", *td.ip, td.ip-header->code);
2867 case CEE_PREFIXREF: ves_abort(); break;
2870 * Note: Exceptions thrown when executing a prefixed opcode need
2871 * to take into account the number of prefix bytes (usually the
2872 * throw point is just (ip - n_prefix_bytes).
2878 case CEE_ARGLIST: ves_abort(); break;
2881 CHECK_STACK(&td, 2);
2882 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2883 ADD_CODE(&td, MINT_CEQ_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2885 ADD_CODE(&td, MINT_CEQ_I4 + td.sp [-1].type - STACK_TYPE_I4);
2887 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2891 CHECK_STACK(&td, 2);
2892 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2893 ADD_CODE(&td, MINT_CGT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2895 ADD_CODE(&td, MINT_CGT_I4 + td.sp [-1].type - STACK_TYPE_I4);
2897 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2901 CHECK_STACK(&td, 2);
2902 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2903 ADD_CODE(&td, MINT_CGT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2905 ADD_CODE(&td, MINT_CGT_UN_I4 + td.sp [-1].type - STACK_TYPE_I4);
2907 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2911 CHECK_STACK(&td, 2);
2912 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2913 ADD_CODE(&td, MINT_CLT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2915 ADD_CODE(&td, MINT_CLT_I4 + td.sp [-1].type - STACK_TYPE_I4);
2917 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2921 CHECK_STACK(&td, 2);
2922 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2923 ADD_CODE(&td, MINT_CLT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2925 ADD_CODE(&td, MINT_CLT_UN_I4 + td.sp [-1].type - STACK_TYPE_I4);
2927 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2930 case CEE_LDVIRTFTN: /* fallthrough */
2933 if (*td.ip == CEE_LDVIRTFTN) {
2934 CHECK_STACK (&td, 1);
2937 token = read32 (td.ip + 1);
2938 if (method->wrapper_type != MONO_WRAPPER_NONE)
2939 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
2941 m = mono_get_method_full (image, token, NULL, generic_context);
2943 if (method->wrapper_type == MONO_WRAPPER_NONE && m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
2944 m = mono_marshal_get_synchronized_wrapper (m);
2946 ADD_CODE(&td, *td.ip == CEE_LDFTN ? MINT_LDFTN : MINT_LDVIRTFTN);
2947 ADD_CODE(&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
2948 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2950 PUSH_SIMPLE_TYPE (&td, STACK_TYPE_F);
2954 load_arg (&td, read16 (td.ip + 1));
2958 int n = read16 (td.ip + 1);
2959 if (n == 0 && signature->hasthis) {
2960 g_error ("LDTHISA: NOPE");
2961 ADD_CODE(&td, MINT_LDTHISA);
2964 ADD_CODE(&td, MINT_LDARGA);
2965 ADD_CODE(&td, td.rtm->arg_offsets [n]); /* FIX for large offsets */
2967 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
2972 store_arg (&td, read16 (td.ip + 1));
2976 load_local (&td, read16 (td.ip + 1));
2980 ADD_CODE(&td, MINT_LDLOCA_S);
2981 ADD_CODE(&td, td.rtm->local_offsets [read16 (td.ip + 1)]);
2982 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
2986 store_local (&td, read16 (td.ip + 1));
2990 CHECK_STACK (&td, 1);
2991 #if SIZEOF_VOID_P == 8
2992 if (td.sp [-1].type == STACK_TYPE_I8)
2993 ADD_CODE(&td, MINT_CONV_I4_I8);
2995 ADD_CODE(&td, MINT_LOCALLOC);
2996 if (td.sp != td.stack + 1)
2997 g_warning("CEE_LOCALLOC: stack not empty");
2999 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
3002 case CEE_UNUSED57: ves_abort(); break;
3003 case CEE_ENDFILTER: ves_abort(); break;
3005 case CEE_UNALIGNED_:
3007 /* FIX: should do something? */;
3011 /* FIX: should do something? */;
3015 /* FIX: should do something? */;
3018 CHECK_STACK(&td, 1);
3019 token = read32 (td.ip + 1);
3020 klass = mono_class_get_full (image, token, generic_context);
3021 ADD_CODE(&td, MINT_INITOBJ);
3022 i32 = mono_class_value_size (klass, NULL);
3028 CHECK_STACK(&td, 3);
3029 /* FIX? convert length to I8? */
3030 ADD_CODE(&td, MINT_CPBLK);
3038 case CEE_CONSTRAINED_:
3039 token = read32 (td.ip + 1);
3040 constrained_class = mono_class_get_full (image, token, generic_context);
3041 mono_class_init (constrained_class);
3045 CHECK_STACK(&td, 3);
3046 ADD_CODE(&td, MINT_INITBLK);
3051 /* FIXME: implement */
3056 SIMPLE_OP (td, MINT_RETHROW);
3057 generating_code = 0;
3061 token = read32 (td.ip + 1);
3063 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
3065 MonoType *type = mono_type_create_from_typespec (image, token);
3066 size = mono_type_size (type, &align);
3069 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
3070 mono_class_init (szclass);
3072 if (!szclass->valuetype)
3073 THROW_EX (mono_exception_from_name (mono_defaults.corlib, "System", "InvalidProgramException"), ip - 5);
3075 size = mono_class_value_size (szclass, &align);
3077 ADD_CODE(&td, MINT_LDC_I4);
3078 WRITE32(&td, &size);
3079 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
3083 case CEE_REFANYTYPE: ves_abort(); break;
3086 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);
3090 g_error ("transform.c: Unimplemented opcode: %02x at 0x%x\n", *td.ip, td.ip-header->code);
3093 if (td.new_ip - td.new_code != new_in_start_offset)
3094 td.last_new_ip = td.new_code + new_in_start_offset;
3095 else if (td.is_bb_start [td.in_start - td.il_code])
3096 td.is_bb_start [td.ip - td.il_code] = 1;
3098 td.last_ip = td.in_start;
3101 if (mono_interp_traceopt) {
3102 const guint16 *p = td.new_code;
3103 printf("Runtime method: %p, VT stack size: %d\n", rtm, td.max_vt_sp);
3104 printf("Calculated stack size: %d, stated size: %d\n", td.max_stack_height, header->max_stack);
3105 while (p < td.new_ip) {
3106 p = mono_interp_dis_mintop(td.new_code, p);
3111 rtm->clauses = mono_mempool_alloc (domain->mp, header->num_clauses * sizeof(MonoExceptionClause));
3112 memcpy (rtm->clauses, header->clauses, header->num_clauses * sizeof(MonoExceptionClause));
3113 rtm->code = mono_mempool_alloc (domain->mp, (td.new_ip - td.new_code) * sizeof(gushort));
3114 memcpy (rtm->code, td.new_code, (td.new_ip - td.new_code) * sizeof(gushort));
3115 g_free (td.new_code);
3116 rtm->new_body_start = rtm->code + body_start_offset;
3117 rtm->num_clauses = header->num_clauses;
3118 for (i = 0; i < header->num_clauses; i++) {
3119 MonoExceptionClause *c = rtm->clauses + i;
3120 int end_off = c->try_offset + c->try_len;
3121 c->try_offset = td.in_offsets [c->try_offset];
3122 c->try_len = td.in_offsets [end_off] - c->try_offset;
3123 end_off = c->handler_offset + c->handler_len;
3124 c->handler_offset = td.in_offsets [c->handler_offset];
3125 c->handler_len = td.in_offsets [end_off] - c->handler_offset;
3127 rtm->vt_stack_size = td.max_vt_sp;
3128 rtm->alloca_size = rtm->locals_size + rtm->args_size + rtm->vt_stack_size + rtm->stack_size;
3129 rtm->data_items = mono_mempool_alloc (domain->mp, td.n_data_items * sizeof (td.data_items [0]));
3130 memcpy (rtm->data_items, td.data_items, td.n_data_items * sizeof (td.data_items [0]));
3131 g_free (td.in_offsets);
3132 g_free (td.forward_refs);
3133 for (i = 0; i < header->code_size; ++i)
3134 g_free (td.stack_state [i]);
3135 g_free (td.stack_state);
3136 g_free (td.stack_height);
3137 g_free (td.vt_stack_size);
3138 g_free (td.data_items);
3139 g_hash_table_destroy (td.data_hash);
3142 static mono_mutex_t calc_section;
3145 mono_interp_transform_init (void)
3147 mono_os_mutex_init_recursive(&calc_section);
3151 mono_interp_transform_method (RuntimeMethod *runtime_method, ThreadContext *context)
3153 int i, align, size, offset;
3154 MonoMethod *method = runtime_method->method;
3155 MonoImage *image = method->klass->image;
3156 MonoMethodHeader *header = mono_method_get_header (method);
3157 MonoMethodSignature *signature = mono_method_signature (method);
3158 register const unsigned char *ip, *end;
3159 const MonoOpcode *opcode;
3162 MonoDomain *domain = mono_domain_get ();
3163 unsigned char *is_bb_start;
3165 MonoVTable *method_class_vt;
3167 MonoGenericContext *generic_context = NULL;
3169 // g_printerr ("TRANSFORM(0x%016lx): begin %s::%s\n", mono_thread_current (), method->klass->name, method->name);
3170 method_class_vt = mono_class_vtable (domain, runtime_method->method->klass);
3171 if (!method_class_vt->initialized) {
3173 MonoInvocation *last_env_frame = context->env_frame;
3174 jmp_buf *old_env = context->current_env;
3177 MonoException *failed = context->env_frame->ex;
3178 context->env_frame->ex = NULL;
3179 context->env_frame = last_env_frame;
3180 context->current_env = old_env;
3183 context->env_frame = context->current_frame;
3184 context->current_env = &env;
3185 mono_runtime_class_init (method_class_vt);
3186 context->env_frame = last_env_frame;
3187 context->current_env = old_env;
3190 mono_profiler_method_jit (method); /* sort of... */
3192 if (mono_method_signature (method)->is_inflated)
3193 generic_context = mono_method_get_context (method);
3195 MonoGenericContainer *generic_container = mono_method_get_generic_container (method);
3196 if (generic_container)
3197 generic_context = &generic_container->context;
3200 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
3201 MonoMethod *nm = NULL;
3202 mono_os_mutex_lock(&calc_section);
3203 if (runtime_method->transformed) {
3204 mono_os_mutex_unlock(&calc_section);
3205 g_error ("FIXME: no jit info?");
3206 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3210 /* assumes all internal calls with an array this are built in... */
3211 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL && (! mono_method_signature (method)->hasthis || method->klass->rank == 0)) {
3212 nm = mono_marshal_get_native_wrapper (method, TRUE, FALSE);
3213 signature = mono_method_signature (nm);
3215 const char *name = method->name;
3216 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
3217 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
3218 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ("ves_icall_mono_delegate_ctor");
3220 char *wrapper_name = g_strdup_printf ("__icall_wrapper_%s", mi->name);
3221 nm = mono_marshal_get_icall_wrapper (mi->sig, wrapper_name, mi->func, TRUE);
3222 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
3223 nm = mono_marshal_get_delegate_invoke (method, NULL);
3224 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
3225 nm = mono_marshal_get_delegate_begin_invoke (method);
3226 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
3227 nm = mono_marshal_get_delegate_end_invoke (method);
3231 runtime_method->code = g_malloc(sizeof(short));
3232 runtime_method->code[0] = MINT_CALLRUN;
3236 runtime_method->stack_size = sizeof (stackval); /* for tracing */
3237 runtime_method->alloca_size = runtime_method->stack_size;
3238 runtime_method->transformed = TRUE;
3239 mono_os_mutex_unlock(&calc_section);
3240 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3244 header = mono_method_get_header (nm);
3245 mono_os_mutex_unlock(&calc_section);
3247 g_assert ((signature->param_count + signature->hasthis) < 1000);
3248 g_assert (header->max_stack < 10000);
3249 /* intern the strings in the method. */
3251 end = ip + header->code_size;
3253 is_bb_start = g_malloc0(header->code_size);
3254 is_bb_start [0] = 1;
3261 else if (in == 0xf0) {
3263 in = *ip + MONO_CEE_MONO_ICALL;
3265 opcode = &mono_opcodes [in];
3266 switch (opcode->argument) {
3267 case MonoInlineNone:
3270 case MonoInlineString:
3271 if (method->wrapper_type == MONO_WRAPPER_NONE)
3272 mono_ldstr (domain, image, mono_metadata_token_index (read32 (ip + 1)));
3275 case MonoInlineType:
3276 if (method->wrapper_type == MONO_WRAPPER_NONE) {
3277 class = mono_class_get_full (image, read32 (ip + 1), generic_context);
3278 mono_class_init (class);
3279 /* quick fix to not do this for the fake ptr classes - probably should not be getting the vtable at all here */
3281 g_error ("FIXME: interface method lookup: %s (in method %s)", class->name, method->name);
3282 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE) && class->interface_offsets != NULL)
3283 mono_class_vtable (domain, class);
3288 case MonoInlineMethod:
3289 if (method->wrapper_type == MONO_WRAPPER_NONE && *ip != CEE_CALLI) {
3290 m = mono_get_method_full (image, read32 (ip + 1), NULL, generic_context);
3292 g_free (is_bb_start);
3293 g_error ("FIXME: where to get method and class string?");
3295 // return mono_get_exception_missing_method ();
3297 mono_class_init (m->klass);
3298 if (!mono_class_is_interface (m->klass))
3299 mono_class_vtable (domain, m->klass);
3303 case MonoInlineField:
3307 case MonoShortInlineR:
3310 case MonoInlineBrTarget:
3311 offset = read32 (ip + 1);
3313 backwards = offset < 0;
3314 offset += ip - header->code;
3315 g_assert (offset >= 0 && offset < header->code_size);
3316 is_bb_start [offset] |= backwards ? 2 : 1;
3318 case MonoShortInlineBrTarget:
3319 offset = ((gint8 *)ip) [1];
3321 backwards = offset < 0;
3322 offset += ip - header->code;
3323 g_assert (offset >= 0 && offset < header->code_size);
3324 is_bb_start [offset] |= backwards ? 2 : 1;
3329 case MonoShortInlineVar:
3330 case MonoShortInlineI:
3333 case MonoInlineSwitch: {
3335 const unsigned char *next_ip;
3339 next_ip = ip + 4 * n;
3340 for (i = 0; i < n; i++) {
3341 offset = read32 (ip);
3342 backwards = offset < 0;
3343 offset += next_ip - header->code;
3344 g_assert (offset >= 0 && offset < header->code_size);
3345 is_bb_start [offset] |= backwards ? 2 : 1;
3355 g_assert_not_reached ();
3358 // g_printerr ("TRANSFORM(0x%016lx): end %s::%s\n", mono_thread_current (), method->klass->name, method->name);
3360 /* the rest needs to be locked so it is only done once */
3361 mono_os_mutex_lock(&calc_section);
3362 if (runtime_method->transformed) {
3363 mono_os_mutex_unlock(&calc_section);
3364 g_free (is_bb_start);
3365 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3369 runtime_method->local_offsets = g_malloc (header->num_locals * sizeof(guint32));
3370 runtime_method->stack_size = (sizeof (stackval) + 2) * header->max_stack; /* + 1 for returns of called functions + 1 for 0-ing in trace*/
3371 runtime_method->stack_size = (runtime_method->stack_size + 7) & ~7;
3373 for (i = 0; i < header->num_locals; ++i) {
3374 size = mono_type_size (header->locals [i], &align);
3375 offset += align - 1;
3376 offset &= ~(align - 1);
3377 runtime_method->local_offsets [i] = offset;
3380 offset = (offset + 7) & ~7;
3381 runtime_method->locals_size = offset;
3382 g_assert (runtime_method->locals_size < 65536);
3384 runtime_method->arg_offsets = g_malloc ((!!signature->hasthis + signature->param_count) * sizeof(guint32));
3386 if (signature->hasthis) {
3387 g_assert (!signature->pinvoke);
3388 size = mono_type_stack_size (&method->klass->byval_arg, &align);
3389 offset += align - 1;
3390 offset &= ~(align - 1);
3391 runtime_method->arg_offsets [0] = offset;
3395 for (i = 0; i < signature->param_count; ++i) {
3396 if (signature->pinvoke) {
3398 size = mono_type_native_stack_size (signature->params [i], &dummy);
3402 size = mono_type_stack_size (signature->params [i], &align);
3403 offset += align - 1;
3404 offset &= ~(align - 1);
3405 runtime_method->arg_offsets [i + !!signature->hasthis] = offset;
3408 offset = (offset + 7) & ~7;
3409 runtime_method->args_size = offset;
3410 g_assert (runtime_method->args_size < 10000);
3412 generate (method, runtime_method, is_bb_start, generic_context);
3414 g_free (is_bb_start);
3416 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3417 runtime_method->transformed = TRUE;
3418 mono_os_mutex_unlock(&calc_section);