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>
18 #include <mono/metadata/seq-points-data.h>
20 #include <mono/mini/mini.h>
23 #include "interp-internals.h"
26 // TODO: export from marshal.c
27 MonoDelegate* mono_ftnptr_to_delegate (MonoClass *klass, gpointer ftn);
42 SeqPoint *last_seq_point;
44 // This will hold a list of last sequence points of incoming basic blocks
45 SeqPoint **pred_seq_points;
46 guint num_pred_seq_points;
57 /* In the interpreter IR */
66 MonoMethodHeader *header;
68 const unsigned char *il_code;
69 const unsigned char *ip;
70 const unsigned char *last_ip;
71 const unsigned char *in_start;
74 StackInfo **stack_state;
77 unsigned char *is_bb_start;
78 unsigned short *new_code;
79 unsigned short *new_code_end;
80 unsigned short *new_ip;
81 unsigned short *last_new_ip;
82 unsigned int max_code_size;
85 unsigned int max_stack_height;
87 unsigned int max_vt_sp;
91 GHashTable *data_hash;
93 gboolean gen_sdb_seq_points;
94 GPtrArray *seq_points;
95 InterpBasicBlock **offset_to_bb;
96 InterpBasicBlock *entry_bb;
100 gboolean verbose_level;
103 #define MINT_TYPE_I1 0
104 #define MINT_TYPE_U1 1
105 #define MINT_TYPE_I2 2
106 #define MINT_TYPE_U2 3
107 #define MINT_TYPE_I4 4
108 #define MINT_TYPE_I8 5
109 #define MINT_TYPE_R4 6
110 #define MINT_TYPE_R8 7
111 #define MINT_TYPE_O 8
112 #define MINT_TYPE_P 9
113 #define MINT_TYPE_VT 10
115 #define STACK_TYPE_I4 0
116 #define STACK_TYPE_I8 1
117 #define STACK_TYPE_R8 2
118 #define STACK_TYPE_O 3
119 #define STACK_TYPE_VT 4
120 #define STACK_TYPE_MP 5
121 #define STACK_TYPE_F 6
123 static const char *stack_type_string [] = { "I4", "I8", "R8", "O ", "VT", "MP", "F " };
125 #if SIZEOF_VOID_P == 8
126 #define STACK_TYPE_I STACK_TYPE_I8
128 #define STACK_TYPE_I STACK_TYPE_I4
131 static int stack_type [] = {
132 STACK_TYPE_I4, /*I1*/
133 STACK_TYPE_I4, /*U1*/
134 STACK_TYPE_I4, /*I2*/
135 STACK_TYPE_I4, /*U2*/
136 STACK_TYPE_I4, /*I4*/
137 STACK_TYPE_I8, /*I8*/
138 STACK_TYPE_R8, /*R4*/
139 STACK_TYPE_R8, /*R8*/
146 grow_code (TransformData *td)
148 unsigned int old_ip_offset = td->new_ip - td->new_code;
149 unsigned int old_last_ip_offset = td->last_new_ip - td->new_code;
150 g_assert (old_ip_offset <= td->max_code_size);
151 td->new_code = g_realloc (td->new_code, (td->max_code_size *= 2) * sizeof (td->new_code [0]));
152 td->new_code_end = td->new_code + td->max_code_size;
153 td->new_ip = td->new_code + old_ip_offset;
154 td->last_new_ip = td->new_code + old_last_ip_offset;
157 #define ENSURE_CODE(td, n) \
159 if ((td)->new_ip + (n) > (td)->new_code_end) \
163 #define ADD_CODE(td, n) \
165 if ((td)->new_ip == (td)->new_code_end) \
167 *(td)->new_ip++ = (n); \
170 #define CHECK_STACK(td, n) \
172 int stack_size = (td)->sp - (td)->stack; \
173 if (stack_size < (n)) \
174 g_warning ("%s.%s: not enough values (%d < %d) on stack at %04x", \
175 (td)->method->klass->name, (td)->method->name, \
176 stack_size, n, (td)->ip - (td)->il_code); \
179 #define ENSURE_I4(td, sp_off) \
181 if ((td)->sp [-sp_off].type == STACK_TYPE_I8) \
182 ADD_CODE(td, sp_off == 1 ? MINT_CONV_I4_I8 : MINT_CONV_I4_I8_SP); \
186 handle_branch (TransformData *td, int short_op, int long_op, int offset)
188 int shorten_branch = 0;
189 int target = td->ip + offset - td->il_code;
190 if (target < 0 || target >= td->code_size)
191 g_assert_not_reached ();
192 if (offset > 0 && td->stack_height [target] < 0) {
193 td->stack_height [target] = td->sp - td->stack;
194 if (td->stack_height [target] > 0)
195 td->stack_state [target] = g_memdup (td->stack, td->stack_height [target] * sizeof (td->stack [0]));
196 td->vt_stack_size [target] = td->vt_sp;
199 offset = td->in_offsets [target] - (td->new_ip - td->new_code);
200 if (offset >= -32768) {
204 if (td->header->code_size <= 25000) /* FIX to be precise somehow? */
207 Reloc *reloc = mono_mempool_alloc0 (td->mempool, sizeof (Reloc));
208 if (shorten_branch) {
210 reloc->type = RELOC_SHORT_BRANCH;
213 reloc->type = RELOC_LONG_BRANCH;
215 reloc->offset = td->new_ip - td->new_code;
216 reloc->target = target;
217 g_ptr_array_add (td->relocs, reloc);
219 if (shorten_branch) {
220 ADD_CODE(td, short_op);
221 ADD_CODE(td, offset);
223 ADD_CODE(td, long_op);
224 ADD_CODE(td, * (unsigned short *)(&offset));
225 ADD_CODE(td, * ((unsigned short *)&offset + 1));
230 one_arg_branch(TransformData *td, int mint_op, int offset)
232 int type = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
233 int long_op = mint_op + type - STACK_TYPE_I4;
234 int short_op = long_op + MINT_BRFALSE_I4_S - MINT_BRFALSE_I4;
237 handle_branch (td, short_op, long_op, offset);
241 two_arg_branch(TransformData *td, int mint_op, int offset)
243 int type1 = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
244 int type2 = td->sp [-2].type == STACK_TYPE_O || td->sp [-2].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-2].type;
245 int long_op = mint_op + type1 - STACK_TYPE_I4;
246 int short_op = long_op + MINT_BEQ_I4_S - MINT_BEQ_I4;
248 if (type1 == STACK_TYPE_I4 && type2 == STACK_TYPE_I8) {
249 ADD_CODE(td, MINT_CONV_I8_I4);
250 td->in_offsets [td->ip - td->il_code]++;
251 } else if (type1 == STACK_TYPE_I8 && type2 == STACK_TYPE_I4) {
252 ADD_CODE(td, MINT_CONV_I8_I4_SP);
253 td->in_offsets [td->ip - td->il_code]++;
254 } else if (type1 != type2) {
255 g_warning("%s.%s: branch type mismatch %d %d",
256 td->method->klass->name, td->method->name,
257 td->sp [-1].type, td->sp [-2].type);
260 handle_branch (td, short_op, long_op, offset);
264 unary_arith_op(TransformData *td, int mint_op)
266 int op = mint_op + td->sp [-1].type - STACK_TYPE_I4;
272 binary_arith_op(TransformData *td, int mint_op)
274 int type1 = td->sp [-2].type;
275 int type2 = td->sp [-1].type;
277 #if SIZEOF_VOID_P == 8
278 if ((type1 == STACK_TYPE_MP || type1 == STACK_TYPE_I8) && type2 == STACK_TYPE_I4) {
279 ADD_CODE(td, MINT_CONV_I8_I4);
280 type2 = STACK_TYPE_I8;
282 if (type1 == STACK_TYPE_I4 && (type2 == STACK_TYPE_MP || type2 == STACK_TYPE_I8)) {
283 ADD_CODE(td, MINT_CONV_I8_I4_SP);
284 type1 = STACK_TYPE_I8;
285 td->sp [-2].type = STACK_TYPE_I8;
288 if (type1 == STACK_TYPE_MP)
289 type1 = STACK_TYPE_I;
290 if (type2 == STACK_TYPE_MP)
291 type2 = STACK_TYPE_I;
292 if (type1 != type2) {
293 g_warning("%s.%s: %04x arith type mismatch %s %d %d",
294 td->method->klass->name, td->method->name,
295 td->ip - td->il_code, mono_interp_opname[mint_op], type1, type2);
297 op = mint_op + type1 - STACK_TYPE_I4;
304 shift_op(TransformData *td, int mint_op)
306 int op = mint_op + td->sp [-2].type - STACK_TYPE_I4;
308 if (td->sp [-1].type != STACK_TYPE_I4) {
309 g_warning("%s.%s: shift type mismatch %d",
310 td->method->klass->name, td->method->name,
318 mint_type(MonoType *type)
323 switch (type->type) {
327 case MONO_TYPE_BOOLEAN:
339 #if SIZEOF_VOID_P == 4
353 case MONO_TYPE_STRING:
354 case MONO_TYPE_SZARRAY:
355 case MONO_TYPE_CLASS:
356 case MONO_TYPE_OBJECT:
357 case MONO_TYPE_ARRAY:
359 case MONO_TYPE_VALUETYPE:
360 if (type->data.klass->enumtype) {
361 type = mono_class_enum_basetype (type->data.klass);
365 case MONO_TYPE_TYPEDBYREF:
367 case MONO_TYPE_GENERICINST:
368 type = &type->data.generic_class->container_class->byval_arg;
371 g_warning ("got type 0x%02x", type->type);
372 g_assert_not_reached ();
378 can_store (int stack_type, int var_type)
380 if (stack_type == STACK_TYPE_O || stack_type == STACK_TYPE_MP)
381 stack_type = STACK_TYPE_I;
382 if (var_type == STACK_TYPE_O || var_type == STACK_TYPE_MP)
383 var_type = STACK_TYPE_I;
384 return stack_type == var_type;
387 #define SET_SIMPLE_TYPE(s, ty) \
394 #define SET_TYPE(s, ty, k) \
401 #define PUSH_SIMPLE_TYPE(td, ty) \
405 sp_height = (td)->sp - (td)->stack; \
406 if (sp_height > (td)->max_stack_height) \
407 (td)->max_stack_height = sp_height; \
408 SET_SIMPLE_TYPE((td)->sp - 1, ty); \
411 #define PUSH_TYPE(td, ty, k) \
415 sp_height = (td)->sp - (td)->stack; \
416 if (sp_height > (td)->max_stack_height) \
417 (td)->max_stack_height = sp_height; \
418 SET_TYPE((td)->sp - 1, ty, k); \
421 #define PUSH_VT(td, size) \
423 (td)->vt_sp += ((size) + 7) & ~7; \
424 if ((td)->vt_sp > (td)->max_vt_sp) \
425 (td)->max_vt_sp = (td)->vt_sp; \
428 #define POP_VT(td, size) \
430 (td)->vt_sp -= ((size) + 7) & ~7; \
433 #if NO_UNALIGNED_ACCESS
434 #define WRITE32(td, v) \
436 ENSURE_CODE(td, 2); \
437 * (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
438 * ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
442 #define WRITE64(td, v) \
444 ENSURE_CODE(td, 4); \
445 * (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
446 * ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
447 * ((guint16 *)((td)->new_ip) + 2) = * ((guint16 *)(v) + 2); \
448 * ((guint16 *)((td)->new_ip) + 3) = * ((guint16 *)(v) + 3); \
452 #define WRITE32(td, v) \
454 ENSURE_CODE(td, 2); \
455 * (guint32 *)((td)->new_ip) = * (guint32 *)(v); \
459 #define WRITE64(td, v) \
461 ENSURE_CODE(td, 4); \
462 * (guint64 *)((td)->new_ip) = * (guint64 *)(v); \
469 load_arg(TransformData *td, int n)
472 MonoClass *klass = NULL;
475 gboolean hasthis = mono_method_signature (td->method)->hasthis;
476 if (hasthis && n == 0)
477 type = &td->method->klass->byval_arg;
479 type = mono_method_signature (td->method)->params [hasthis ? n - 1 : n];
481 mt = mint_type (type);
482 if (mt == MINT_TYPE_VT) {
484 klass = mono_class_from_mono_type (type);
485 if (mono_method_signature (td->method)->pinvoke)
486 size = mono_class_native_size (klass, NULL);
488 size = mono_class_value_size (klass, NULL);
490 if (hasthis && n == 0) {
492 ADD_CODE (td, MINT_LDARG_P);
493 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
497 ADD_CODE (td, MINT_LDARG_VT);
498 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
502 if (hasthis && n == 0) {
504 ADD_CODE (td, MINT_LDARG_P);
505 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
508 ADD_CODE(td, MINT_LDARG_I1 + (mt - MINT_TYPE_I1));
509 ADD_CODE(td, td->rtm->arg_offsets [n]); /* FIX for large offset */
510 if (mt == MINT_TYPE_O)
511 klass = mono_class_from_mono_type (type);
514 PUSH_TYPE(td, stack_type[mt], klass);
518 store_arg(TransformData *td, int n)
524 gboolean hasthis = mono_method_signature (td->method)->hasthis;
525 if (hasthis && n == 0)
526 type = &td->method->klass->byval_arg;
528 type = mono_method_signature (td->method)->params [n - !!hasthis];
530 mt = mint_type (type);
531 if (mt == MINT_TYPE_VT) {
533 MonoClass *klass = mono_class_from_mono_type (type);
534 if (mono_method_signature (td->method)->pinvoke)
535 size = mono_class_native_size (klass, NULL);
537 size = mono_class_value_size (klass, NULL);
538 ADD_CODE(td, MINT_STARG_VT);
539 ADD_CODE(td, td->rtm->arg_offsets [n]);
541 if (td->sp [-1].type == STACK_TYPE_VT)
544 ADD_CODE(td, MINT_STARG_I1 + (mt - MINT_TYPE_I1));
545 ADD_CODE(td, td->rtm->arg_offsets [n]);
551 store_inarg(TransformData *td, int n)
554 gboolean hasthis = mono_method_signature (td->method)->hasthis;
555 if (hasthis && n == 0)
556 type = &td->method->klass->byval_arg;
558 type = mono_method_signature (td->method)->params [n - !!hasthis];
560 int mt = mint_type (type);
561 if (hasthis && n == 0) {
562 ADD_CODE (td, MINT_STINARG_P);
566 if (mt == MINT_TYPE_VT) {
567 MonoClass *klass = mono_class_from_mono_type (type);
569 if (mono_method_signature (td->method)->pinvoke)
570 size = mono_class_native_size (klass, NULL);
572 size = mono_class_value_size (klass, NULL);
573 ADD_CODE(td, MINT_STINARG_VT);
577 ADD_CODE(td, MINT_STINARG_I1 + (mt - MINT_TYPE_I1));
583 load_local(TransformData *td, int n)
585 MonoType *type = td->header->locals [n];
586 int mt = mint_type (type);
587 int offset = td->rtm->local_offsets [n];
588 MonoClass *klass = NULL;
589 if (mt == MINT_TYPE_VT) {
590 klass = mono_class_from_mono_type (type);
591 gint32 size = mono_class_value_size (klass, NULL);
593 ADD_CODE(td, MINT_LDLOC_VT);
594 ADD_CODE(td, offset); /*FIX for large offset */
597 g_assert (mt < MINT_TYPE_VT);
598 if (!td->gen_sdb_seq_points &&
599 mt == MINT_TYPE_I4 && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
600 td->last_new_ip [0] == MINT_STLOC_I4 && td->last_new_ip [1] == offset) {
601 td->last_new_ip [0] = MINT_STLOC_NP_I4;
602 } else if (!td->gen_sdb_seq_points &&
603 mt == MINT_TYPE_O && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
604 td->last_new_ip [0] == MINT_STLOC_O && td->last_new_ip [1] == offset) {
605 td->last_new_ip [0] = MINT_STLOC_NP_O;
607 ADD_CODE(td, MINT_LDLOC_I1 + (mt - MINT_TYPE_I1));
608 ADD_CODE(td, offset); /*FIX for large offset */
610 if (mt == MINT_TYPE_O)
611 klass = mono_class_from_mono_type (type);
613 PUSH_TYPE(td, stack_type[mt], klass);
617 store_local(TransformData *td, int n)
619 MonoType *type = td->header->locals [n];
620 int mt = mint_type (type);
621 int offset = td->rtm->local_offsets [n];
623 #if SIZEOF_VOID_P == 8
624 if (td->sp [-1].type == STACK_TYPE_I4 && stack_type [mt] == STACK_TYPE_I8) {
625 ADD_CODE(td, MINT_CONV_I8_I4);
626 td->sp [-1].type = STACK_TYPE_I8;
629 if (!can_store(td->sp [-1].type, stack_type [mt])) {
630 g_warning("%s.%s: Store local stack type mismatch %d %d",
631 td->method->klass->name, td->method->name,
632 stack_type [mt], td->sp [-1].type);
634 if (mt == MINT_TYPE_VT) {
635 MonoClass *klass = mono_class_from_mono_type (type);
636 gint32 size = mono_class_value_size (klass, NULL);
637 ADD_CODE(td, MINT_STLOC_VT);
638 ADD_CODE(td, offset); /*FIX for large offset */
640 if (td->sp [-1].type == STACK_TYPE_VT)
643 g_assert (mt < MINT_TYPE_VT);
644 ADD_CODE(td, MINT_STLOC_I1 + (mt - MINT_TYPE_I1));
645 ADD_CODE(td, offset); /*FIX for large offset */
650 #define SIMPLE_OP(td, op) \
657 get_data_item_index (TransformData *td, void *ptr)
659 gpointer p = g_hash_table_lookup (td->data_hash, ptr);
662 return GPOINTER_TO_UINT (p) - 1;
663 if (td->max_data_items == td->n_data_items) {
664 td->max_data_items = td->n_data_items == 0 ? 16 : 2 * td->max_data_items;
665 td->data_items = g_realloc (td->data_items, td->max_data_items * sizeof(td->data_items [0]));
667 index = td->n_data_items;
668 td->data_items [index] = ptr;
670 g_hash_table_insert (td->data_hash, ptr, GUINT_TO_POINTER (index + 1));
675 jit_call_supported (MonoMethod *method, MonoMethodSignature *sig)
679 if (sig->param_count > 6)
683 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
685 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
687 if (method->is_inflated)
689 if (method->string_ctor)
692 for (l = jit_classes; l; l = l->next) {
693 char *class_name = l->data;
695 if (!strcmp (method->klass->name, class_name))
704 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)
706 MonoImage *image = method->klass->image;
707 MonoMethodSignature *csignature;
709 int virtual = *td->ip == CEE_CALLVIRT;
710 int calli = *td->ip == CEE_CALLI || *td->ip == CEE_MONO_CALLI_EXTRA_ARG;
712 guint32 vt_stack_used = 0;
713 guint32 vt_res_size = 0;
718 guint32 token = read32 (td->ip + 1);
720 if (target_method == NULL) {
723 native = (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE && td->sp [-1].type == STACK_TYPE_I);
725 if (method->wrapper_type != MONO_WRAPPER_NONE)
726 csignature = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
728 csignature = mono_metadata_parse_signature (image, token);
730 if (generic_context) {
731 csignature = mono_inflate_generic_signature (csignature, generic_context, &error);
732 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
735 target_method = NULL;
737 if (method->wrapper_type == MONO_WRAPPER_NONE)
738 target_method = mono_get_method_full (image, token, NULL, generic_context);
740 target_method = (MonoMethod *)mono_method_get_wrapper_data (method, token);
741 csignature = mono_method_signature (target_method);
743 if (generic_context) {
744 csignature = mono_inflate_generic_signature (csignature, generic_context, &error);
745 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
746 target_method = mono_class_inflate_generic_method_checked (target_method, generic_context, &error);
747 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
751 csignature = mono_method_signature (target_method);
754 if (target_method && target_method->string_ctor) {
755 /* Create the real signature */
756 MonoMethodSignature *ctor_sig = mono_metadata_signature_dup_mempool (td->mempool, csignature);
757 ctor_sig->ret = &mono_defaults.string_class->byval_arg;
759 csignature = ctor_sig;
764 if (target_method->klass == mono_defaults.string_class) {
765 if (target_method->name [0] == 'g') {
766 if (strcmp (target_method->name, "get_Chars") == 0)
768 else if (strcmp (target_method->name, "get_Length") == 0)
771 } else if (mono_class_is_subclass_of (target_method->klass, mono_defaults.array_class, FALSE)) {
772 if (!strcmp (target_method->name, "get_Rank")) {
773 op = MINT_ARRAY_RANK;
774 } else if (!strcmp (target_method->name, "get_Length")) {
776 } else if (!strcmp (target_method->name, "Address")) {
777 op = readonly ? MINT_LDELEMA : MINT_LDELEMA_TC;
779 } else if (target_method->klass->image == mono_defaults.corlib &&
780 (strcmp (target_method->klass->name_space, "System.Diagnostics") == 0) &&
781 (strcmp (target_method->klass->name, "Debugger") == 0)) {
782 if (!strcmp (target_method->name, "Break") && csignature->param_count == 0) {
783 if (mini_should_insert_breakpoint (method))
789 if (constrained_class) {
790 if (constrained_class->enumtype && !strcmp (target_method->name, "GetHashCode")) {
791 /* Use the corresponding method from the base type to avoid boxing */
792 MonoType *base_type = mono_class_enum_basetype (constrained_class);
793 g_assert (base_type);
794 constrained_class = mono_class_from_mono_type (base_type);
795 target_method = mono_class_get_method_from_name (constrained_class, target_method->name, 0);
796 g_assert (target_method);
800 if (constrained_class) {
801 mono_class_setup_vtable (constrained_class);
803 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);
805 target_method = mono_get_method_constrained_with_method (image, target_method, constrained_class, generic_context, &error);
807 g_print (" : %s::%s. %s (%p)\n", target_method->klass->name, target_method->name, mono_signature_full_name (target_method->signature), target_method);
809 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
810 mono_class_setup_vtable (target_method->klass);
812 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)) {
813 if (target_method->klass == mono_defaults.enum_class && (td->sp - csignature->param_count - 1)->type == STACK_TYPE_MP) {
814 /* managed pointer on the stack, we need to deref that puppy */
815 ADD_CODE (td, MINT_LDIND_I);
816 ADD_CODE (td, csignature->param_count);
818 ADD_CODE (td, MINT_BOX);
819 ADD_CODE (td, get_data_item_index (td, constrained_class));
820 ADD_CODE (td, csignature->param_count);
821 } else if (!constrained_class->valuetype) {
822 /* managed pointer on the stack, we need to deref that puppy */
823 ADD_CODE (td, MINT_LDIND_I);
824 ADD_CODE (td, csignature->param_count);
826 if (target_method->klass->valuetype) {
829 /* Interface method */
832 mono_class_setup_vtable (constrained_class);
833 ioffset = mono_class_interface_offset (constrained_class, target_method->klass);
835 g_error ("type load error: constrained_class");
836 slot = mono_method_get_vtable_slot (target_method);
838 g_error ("type load error: target_method->klass");
839 target_method = constrained_class->vtable [ioffset + slot];
841 if (target_method->klass == mono_defaults.enum_class) {
842 if ((td->sp - csignature->param_count - 1)->type == STACK_TYPE_MP) {
843 /* managed pointer on the stack, we need to deref that puppy */
844 ADD_CODE (td, MINT_LDIND_I);
845 ADD_CODE (td, csignature->param_count);
847 ADD_CODE (td, MINT_BOX);
848 ADD_CODE (td, get_data_item_index (td, constrained_class));
849 ADD_CODE (td, csignature->param_count);
857 mono_class_init (target_method->klass);
859 CHECK_STACK (td, csignature->param_count + csignature->hasthis);
860 if (!calli && op == -1 && (!virtual || (target_method->flags & METHOD_ATTRIBUTE_VIRTUAL) == 0) &&
861 (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
862 (target_method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) == 0 &&
863 !(target_method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING)) {
864 int called_inited = mono_class_vtable (domain, target_method->klass)->initialized;
865 MonoMethodHeader *mheader = mono_method_get_header (target_method);
867 if (/*mono_metadata_signature_equal (method->signature, target_method->signature) */ method == target_method && *(td->ip + 5) == CEE_RET) {
869 if (td->verbose_level)
870 g_print ("Optimize tail call of %s.%s\n", target_method->klass->name, target_method->name);
872 for (i = csignature->param_count - 1 + !!csignature->hasthis; i >= 0; --i)
875 ADD_CODE(td, MINT_BR_S);
876 offset = body_start_offset - ((td->new_ip - 1) - td->new_code);
877 ADD_CODE(td, offset);
878 if (!is_bb_start [td->ip + 5 - td->il_code])
879 ++td->ip; /* gobble the CEE_RET if it isn't branched to */
883 /* mheader might not exist if this is a delegate invoc, etc */
884 if (mheader && *mheader->code == CEE_RET && called_inited) {
885 if (td->verbose_level)
886 g_print ("Inline (empty) call of %s.%s\n", target_method->klass->name, target_method->name);
887 for (i = 0; i < csignature->param_count; i++) {
888 ADD_CODE (td, MINT_POP); /*FIX: vt */
891 if (csignature->hasthis) {
893 ADD_CODE(td, MINT_CKNULL);
894 ADD_CODE (td, MINT_POP);
897 td->sp -= csignature->param_count + csignature->hasthis;
903 if (method->wrapper_type == MONO_WRAPPER_NONE && target_method != NULL) {
904 if (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
905 target_method = mono_marshal_get_native_wrapper (target_method, FALSE, FALSE);
906 if (!virtual && target_method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
907 target_method = mono_marshal_get_synchronized_wrapper (target_method);
909 g_assert (csignature->call_convention == MONO_CALL_DEFAULT || csignature->call_convention == MONO_CALL_C);
910 td->sp -= csignature->param_count + !!csignature->hasthis;
911 for (i = 0; i < csignature->param_count; ++i) {
912 if (td->sp [i + !!csignature->hasthis].type == STACK_TYPE_VT) {
914 MonoClass *klass = mono_class_from_mono_type (csignature->params [i]);
915 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
916 size = mono_class_native_size (klass, NULL);
918 size = mono_class_value_size (klass, NULL);
919 size = (size + 7) & ~7;
920 vt_stack_used += size;
924 /* need to handle typedbyref ... */
925 if (csignature->ret->type != MONO_TYPE_VOID) {
926 int mt = mint_type(csignature->ret);
927 MonoClass *klass = mono_class_from_mono_type (csignature->ret);
928 if (mt == MINT_TYPE_VT) {
929 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
930 vt_res_size = mono_class_native_size (klass, NULL);
932 vt_res_size = mono_class_value_size (klass, NULL);
933 PUSH_VT(td, vt_res_size);
935 PUSH_TYPE(td, stack_type[mt], klass);
941 #if SIZEOF_VOID_P == 8
942 if (op == MINT_LDLEN)
943 ADD_CODE (td, MINT_CONV_I4_I8);
945 if (op == MINT_LDELEMA || op == MINT_LDELEMA_TC) {
946 ADD_CODE (td, get_data_item_index (td, target_method->klass));
947 ADD_CODE (td, 1 + target_method->klass->rank);
949 } else if (!calli && !virtual && jit_call_supported (target_method, csignature)) {
950 ADD_CODE(td, MINT_JIT_CALL);
951 ADD_CODE(td, get_data_item_index (td, (void *)mono_interp_get_imethod (domain, target_method, &error)));
952 mono_error_assert_ok (&error);
955 ADD_CODE(td, native ? MINT_CALLI_NAT : MINT_CALLI);
957 ADD_CODE(td, is_void ? MINT_VCALLVIRT : MINT_CALLVIRT);
959 ADD_CODE(td, is_void ? MINT_VCALL : MINT_CALL);
962 ADD_CODE(td, get_data_item_index (td, (void *)csignature));
964 ADD_CODE(td, get_data_item_index (td, (void *)mono_interp_get_imethod (domain, target_method, &error)));
965 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
969 if (vt_stack_used != 0 || vt_res_size != 0) {
970 ADD_CODE(td, MINT_VTRESULT);
971 ADD_CODE(td, vt_res_size);
972 WRITE32(td, &vt_stack_used);
973 td->vt_sp -= vt_stack_used;
977 static MonoClassField *
978 interp_field_from_token (MonoMethod *method, guint32 token, MonoClass **klass, MonoGenericContext *generic_context)
980 MonoClassField *field = NULL;
981 if (method->wrapper_type != MONO_WRAPPER_NONE) {
982 field = (MonoClassField *) mono_method_get_wrapper_data (method, token);
983 *klass = field->parent;
987 field = mono_field_from_token_checked (method->klass->image, token, klass, generic_context, &error);
988 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
993 static InterpBasicBlock*
994 get_bb (TransformData *td, InterpBasicBlock *cbb, unsigned char *ip)
996 int offset = ip - td->il_code;
997 InterpBasicBlock *bb = td->offset_to_bb [offset];
1000 bb = mono_mempool_alloc0 (td->mempool, sizeof (InterpBasicBlock));
1002 td->offset_to_bb [offset] = bb;
1004 td->basic_blocks = g_list_append_mempool (td->mempool, td->basic_blocks, bb);
1008 bb->preds = g_slist_prepend_mempool (td->mempool, bb->preds, cbb);
1015 * Compute the set of IL level basic blocks.
1018 get_basic_blocks (TransformData *td)
1020 guint8 *start = (guint8*)td->il_code;
1021 guint8 *end = (guint8*)td->il_code + td->code_size;
1023 unsigned char *target;
1026 const MonoOpcode *opcode;
1027 InterpBasicBlock *cbb;
1029 td->offset_to_bb = mono_mempool_alloc0 (td->mempool, sizeof (InterpBasicBlock*) * (end - start + 1));
1030 td->entry_bb = cbb = get_bb (td, NULL, start);
1033 cli_addr = ip - start;
1034 td->offset_to_bb [cli_addr] = cbb;
1035 i = mono_opcode_value ((const guint8 **)&ip, end);
1036 opcode = &mono_opcodes [i];
1037 switch (opcode->argument) {
1038 case MonoInlineNone:
1041 case MonoInlineString:
1042 case MonoInlineType:
1043 case MonoInlineField:
1044 case MonoInlineMethod:
1047 case MonoShortInlineR:
1054 case MonoShortInlineVar:
1055 case MonoShortInlineI:
1058 case MonoShortInlineBrTarget:
1059 target = start + cli_addr + 2 + (signed char)ip [1];
1060 get_bb (td, cbb, target);
1062 cbb = get_bb (td, cbb, ip);
1064 case MonoInlineBrTarget:
1065 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
1066 get_bb (td, cbb, target);
1068 cbb = get_bb (td, cbb, ip);
1070 case MonoInlineSwitch: {
1071 guint32 n = read32 (ip + 1);
1074 cli_addr += 5 + 4 * n;
1075 target = start + cli_addr;
1076 get_bb (td, cbb, target);
1078 for (j = 0; j < n; ++j) {
1079 target = start + cli_addr + (gint32)read32 (ip);
1080 get_bb (td, cbb, target);
1083 cbb = get_bb (td, cbb, ip);
1091 g_assert_not_reached ();
1097 interp_save_debug_info (InterpMethod *rtm, MonoMethodHeader *header, TransformData *td, GArray *line_numbers)
1099 MonoDebugMethodJitInfo *dinfo;
1102 if (!mono_debug_enabled ())
1106 * We save the debug info in the same way the JIT does it, treating the interpreter IR as the native code.
1109 dinfo = g_new0 (MonoDebugMethodJitInfo, 1);
1110 dinfo->num_params = rtm->param_count;
1111 dinfo->params = g_new0 (MonoDebugVarInfo, dinfo->num_params);
1112 dinfo->num_locals = header->num_locals;
1113 dinfo->locals = g_new0 (MonoDebugVarInfo, header->num_locals);
1114 dinfo->code_start = (guint8*)rtm->code;
1115 dinfo->code_size = td->new_ip - td->new_code;
1116 dinfo->epilogue_begin = 0;
1117 dinfo->has_var_info = TRUE;
1118 dinfo->num_line_numbers = line_numbers->len;
1119 dinfo->line_numbers = g_new0 (MonoDebugLineNumberEntry, dinfo->num_line_numbers);
1121 for (i = 0; i < dinfo->num_params; i++) {
1122 MonoDebugVarInfo *var = &dinfo->params [i];
1123 var->type = rtm->param_types [i];
1125 for (i = 0; i < dinfo->num_locals; i++) {
1126 MonoDebugVarInfo *var = &dinfo->locals [i];
1127 var->type = header->locals [i];
1130 for (i = 0; i < dinfo->num_line_numbers; i++)
1131 dinfo->line_numbers [i] = g_array_index (line_numbers, MonoDebugLineNumberEntry, i);
1132 mono_debug_add_method (rtm->method, dinfo, mono_domain_get ());
1134 mono_debug_free_method_jit_info (dinfo);
1137 /* Same as the code in seq-points.c */
1139 insert_pred_seq_point (SeqPoint *last_sp, SeqPoint *sp, GSList **next)
1142 int src_index = last_sp->next_offset;
1143 int dst_index = sp->next_offset;
1145 /* bb->in_bb might contain duplicates */
1146 for (l = next [src_index]; l; l = l->next)
1147 if (GPOINTER_TO_UINT (l->data) == dst_index)
1150 next [src_index] = g_slist_append (next [src_index], GUINT_TO_POINTER (dst_index));
1154 recursively_make_pred_seq_points (TransformData *td, InterpBasicBlock *bb)
1156 const gpointer MONO_SEQ_SEEN_LOOP = GINT_TO_POINTER(-1);
1159 GArray *predecessors = g_array_new (FALSE, TRUE, sizeof (gpointer));
1160 GHashTable *seen = g_hash_table_new_full (g_direct_hash, NULL, NULL, NULL);
1162 // Insert/remove sentinel into the memoize table to detect loops containing bb
1163 bb->pred_seq_points = MONO_SEQ_SEEN_LOOP;
1165 for (l = bb->preds; l; l = l->next) {
1166 InterpBasicBlock *in_bb = l->data;
1168 // This bb has the last seq point, append it and continue
1169 if (in_bb->last_seq_point != NULL) {
1170 predecessors = g_array_append_val (predecessors, in_bb->last_seq_point);
1174 // We've looped or handled this before, exit early.
1175 // No last sequence points to find.
1176 if (in_bb->pred_seq_points == MONO_SEQ_SEEN_LOOP)
1179 // Take sequence points from incoming basic blocks
1181 if (in_bb == td->entry_bb)
1184 if (in_bb->pred_seq_points == NULL)
1185 recursively_make_pred_seq_points (td, in_bb);
1187 // Union sequence points with incoming bb's
1188 for (int i=0; i < in_bb->num_pred_seq_points; i++) {
1189 if (!g_hash_table_lookup (seen, in_bb->pred_seq_points [i])) {
1190 g_array_append_val (predecessors, in_bb->pred_seq_points [i]);
1191 g_hash_table_insert (seen, in_bb->pred_seq_points [i], (gpointer)&MONO_SEQ_SEEN_LOOP);
1194 // predecessors = g_array_append_vals (predecessors, in_bb->pred_seq_points, in_bb->num_pred_seq_points);
1197 g_hash_table_destroy (seen);
1199 if (predecessors->len != 0) {
1200 bb->pred_seq_points = mono_mempool_alloc0 (td->mempool, sizeof (SeqPoint *) * predecessors->len);
1201 bb->num_pred_seq_points = predecessors->len;
1203 for (int newer = 0; newer < bb->num_pred_seq_points; newer++) {
1204 bb->pred_seq_points [newer] = g_array_index (predecessors, gpointer, newer);
1208 g_array_free (predecessors, TRUE);
1212 collect_pred_seq_points (TransformData *td, InterpBasicBlock *bb, SeqPoint *seqp, GSList **next)
1214 // Doesn't have a last sequence point, must find from incoming basic blocks
1215 if (bb->pred_seq_points == NULL && bb != td->entry_bb)
1216 recursively_make_pred_seq_points (td, bb);
1218 for (int i = 0; i < bb->num_pred_seq_points; i++)
1219 insert_pred_seq_point (bb->pred_seq_points [i], seqp, next);
1225 save_seq_points (TransformData *td)
1227 InterpMethod *rtm = td->rtm;
1229 int i, seq_info_size;
1230 MonoSeqPointInfo *info;
1231 MonoDomain *domain = mono_domain_get ();
1232 GSList **next = NULL;
1235 if (!td->gen_sdb_seq_points)
1239 * For each sequence point, compute the list of sequence points immediately
1240 * following it, this is needed to implement 'step over' in the debugger agent.
1241 * Similar to the code in mono_save_seq_point_info ().
1243 for (i = 0; i < td->seq_points->len; ++i) {
1244 SeqPoint *sp = g_ptr_array_index (td->seq_points, i);
1246 /* Store the seq point index here temporarily */
1247 sp->next_offset = i;
1249 next = mono_mempool_alloc0 (td->mempool, sizeof (GList*) * td->seq_points->len);
1250 for (bblist = td->basic_blocks; bblist; bblist = bblist->next) {
1251 InterpBasicBlock *bb = bblist->data;
1253 GSList *bb_seq_points = g_slist_reverse (bb->seq_points);
1254 SeqPoint *last = NULL;
1255 for (GSList *l = bb_seq_points; l; l = l->next) {
1256 SeqPoint *sp = l->data;
1258 if (sp->il_offset == METHOD_ENTRY_IL_OFFSET || sp->il_offset == METHOD_EXIT_IL_OFFSET)
1259 /* Used to implement method entry/exit events */
1263 /* Link with the previous seq point in the same bb */
1264 next [last->next_offset] = g_slist_append_mempool (td->mempool, next [last->next_offset], GINT_TO_POINTER (sp->next_offset));
1266 /* Link with the last bb in the previous bblocks */
1267 collect_pred_seq_points (td, bb, sp, next);
1273 /* Serialize the seq points into a byte array */
1274 array = g_byte_array_new ();
1275 SeqPoint zero_seq_point = {0};
1276 SeqPoint* last_seq_point = &zero_seq_point;
1277 for (i = 0; i < td->seq_points->len; ++i) {
1278 SeqPoint *sp = (SeqPoint*)g_ptr_array_index (td->seq_points, i);
1280 sp->next_offset = 0;
1281 if (mono_seq_point_info_add_seq_point (array, sp, last_seq_point, next [i], TRUE))
1282 last_seq_point = sp;
1285 if (td->verbose_level) {
1286 g_print ("\nSEQ POINT MAP FOR %s: \n", td->method->name);
1288 for (i = 0; i < td->seq_points->len; ++i) {
1289 SeqPoint *sp = (SeqPoint*)g_ptr_array_index (td->seq_points, i);
1295 g_print ("\tIL0x%x[0x%0x] ->", sp->il_offset, sp->native_offset);
1296 for (l = next [i]; l; l = l->next) {
1297 int next_index = GPOINTER_TO_UINT (l->data);
1298 g_print (" IL0x%x", ((SeqPoint*)g_ptr_array_index (td->seq_points, next_index))->il_offset);
1304 info = mono_seq_point_info_new (array->len, TRUE, array->data, TRUE, &seq_info_size);
1305 mono_jit_stats.allocated_seq_points_size += seq_info_size;
1307 g_byte_array_free (array, TRUE);
1309 mono_domain_lock (domain);
1310 g_hash_table_insert (domain_jit_info (domain)->seq_points, rtm->method, info);
1311 mono_domain_unlock (domain);
1315 emit_seq_point (TransformData *td, int il_offset, InterpBasicBlock *cbb, gboolean nonempty_stack)
1319 seqp = mono_mempool_alloc0 (td->mempool, sizeof (SeqPoint));
1320 seqp->il_offset = il_offset;
1321 seqp->native_offset = (guint8*)td->new_ip - (guint8*)td->new_code;
1323 seqp->flags |= MONO_SEQ_POINT_FLAG_NONEMPTY_STACK;
1325 ADD_CODE (td, MINT_SDB_SEQ_POINT);
1326 g_ptr_array_add (td->seq_points, seqp);
1328 cbb->seq_points = g_slist_prepend_mempool (td->mempool, cbb->seq_points, seqp);
1329 cbb->last_seq_point = seqp;
1332 #define BARRIER_IF_VOLATILE(td) \
1335 ADD_CODE (td, MINT_MONO_MEMORY_BARRIER); \
1336 volatile_ = FALSE; \
1341 generate (MonoMethod *method, InterpMethod *rtm, unsigned char *is_bb_start, MonoGenericContext *generic_context)
1343 MonoMethodHeader *header = mono_method_get_header (method);
1344 MonoMethodSignature *signature = mono_method_signature (method);
1345 MonoImage *image = method->klass->image;
1346 MonoDomain *domain = rtm->domain;
1347 MonoClass *constrained_class = NULL;
1349 int offset, mt, i, i32;
1350 gboolean readonly = FALSE;
1351 gboolean volatile_ = FALSE;
1353 MonoClassField *field;
1354 const unsigned char *end;
1355 int new_in_start_offset;
1356 int body_start_offset;
1359 TransformData transform_data;
1361 int generating_code = 1;
1362 GArray *line_numbers;
1363 MonoDebugMethodInfo *minfo;
1364 MonoBitSet *seq_point_locs = NULL;
1365 MonoBitSet *seq_point_set_locs = NULL;
1366 gboolean sym_seq_points = FALSE;
1367 InterpBasicBlock *bb_exit = NULL;
1368 static gboolean verbose_method_inited;
1369 static char* verbose_method_name;
1371 if (!verbose_method_inited) {
1372 verbose_method_name = getenv ("MONO_VERBOSE_METHOD");
1373 verbose_method_inited = TRUE;
1376 memset (&transform_data, 0, sizeof(transform_data));
1377 td = &transform_data;
1379 td->method = method;
1381 td->is_bb_start = is_bb_start;
1382 td->il_code = header->code;
1383 td->code_size = header->code_size;
1384 td->header = header;
1385 td->max_code_size = td->code_size;
1386 td->new_code = (unsigned short *)g_malloc(td->max_code_size * sizeof(gushort));
1387 td->new_code_end = td->new_code + td->max_code_size;
1388 td->mempool = mono_mempool_new ();
1389 td->in_offsets = g_malloc0(header->code_size * sizeof(int));
1390 td->stack_state = g_malloc0(header->code_size * sizeof(StackInfo *));
1391 td->stack_height = g_malloc(header->code_size * sizeof(int));
1392 td->vt_stack_size = g_malloc(header->code_size * sizeof(int));
1393 td->n_data_items = 0;
1394 td->max_data_items = 0;
1395 td->data_items = NULL;
1396 td->data_hash = g_hash_table_new (NULL, NULL);
1397 td->clause_indexes = g_malloc (header->code_size * sizeof (int));
1398 td->gen_sdb_seq_points = debug_options.gen_sdb_seq_points;
1399 td->seq_points = g_ptr_array_new ();
1400 td->relocs = g_ptr_array_new ();
1401 td->verbose_level = mono_interp_traceopt;
1402 rtm->data_items = td->data_items;
1403 for (i = 0; i < header->code_size; i++) {
1404 td->stack_height [i] = -1;
1405 td->clause_indexes [i] = -1;
1408 if (verbose_method_name) {
1409 const char *name = verbose_method_name;
1411 if ((strchr (name, '.') > name) || strchr (name, ':')) {
1412 MonoMethodDesc *desc;
1414 desc = mono_method_desc_new (name, TRUE);
1415 if (mono_method_desc_full_match (desc, method)) {
1416 td->verbose_level = 4;
1418 mono_method_desc_free (desc);
1420 if (strcmp (method->name, name) == 0)
1421 td->verbose_level = 4;
1425 if (td->gen_sdb_seq_points) {
1426 get_basic_blocks (td);
1428 minfo = mono_debug_lookup_method (method);
1431 MonoSymSeqPoint *sps;
1432 int i, n_il_offsets;
1434 mono_debug_get_seq_points (minfo, NULL, NULL, NULL, &sps, &n_il_offsets);
1436 seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (td->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
1437 seq_point_set_locs = mono_bitset_mem_new (mono_mempool_alloc0 (td->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
1438 sym_seq_points = TRUE;
1440 for (i = 0; i < n_il_offsets; ++i) {
1441 if (sps [i].il_offset < header->code_size)
1442 mono_bitset_set_fast (seq_point_locs, sps [i].il_offset);
1445 } else if (!method->wrapper_type && !method->dynamic && mono_debug_image_has_debug_info (method->klass->image)) {
1446 /* Methods without line number info like auto-generated property accessors */
1447 seq_point_locs = mono_bitset_new (header->code_size, 0);
1448 seq_point_set_locs = mono_bitset_new (header->code_size, 0);
1449 sym_seq_points = TRUE;
1453 td->new_ip = td->new_code;
1454 td->last_new_ip = NULL;
1456 td->stack = g_malloc0 ((header->max_stack + 1) * sizeof (td->stack [0]));
1458 td->max_stack_height = 0;
1460 line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
1462 for (i = 0; i < header->num_clauses; i++) {
1463 MonoExceptionClause *c = header->clauses + i;
1464 td->stack_height [c->handler_offset] = 0;
1465 td->vt_stack_size [c->handler_offset] = 0;
1466 td->is_bb_start [c->handler_offset] = 1;
1468 td->stack_height [c->handler_offset] = 1;
1469 td->stack_state [c->handler_offset] = g_malloc0(sizeof(StackInfo));
1470 td->stack_state [c->handler_offset][0].type = STACK_TYPE_O;
1471 td->stack_state [c->handler_offset][0].klass = NULL; /*FIX*/
1473 if (c->flags & MONO_EXCEPTION_CLAUSE_FILTER) {
1474 td->stack_height [c->data.filter_offset] = 0;
1475 td->vt_stack_size [c->data.filter_offset] = 0;
1476 td->is_bb_start [c->data.filter_offset] = 1;
1478 td->stack_height [c->data.filter_offset] = 1;
1479 td->stack_state [c->data.filter_offset] = g_malloc0(sizeof(StackInfo));
1480 td->stack_state [c->data.filter_offset][0].type = STACK_TYPE_O;
1481 td->stack_state [c->data.filter_offset][0].klass = NULL; /*FIX*/
1484 for (int j = c->handler_offset; j < c->handler_offset + c->handler_len; ++j) {
1485 if (td->clause_indexes [j] == -1)
1486 td->clause_indexes [j] = i;
1490 td->ip = header->code;
1491 end = td->ip + header->code_size;
1493 if (td->verbose_level) {
1494 char *tmp = mono_disasm_code (NULL, method, td->ip, end);
1495 char *name = mono_method_full_name (method, TRUE);
1496 g_print ("Method %s, original code:\n", name);
1497 g_print ("%s\n", tmp);
1502 if (signature->hasthis)
1503 store_inarg (td, 0);
1504 for (i = 0; i < signature->param_count; i++)
1505 store_inarg (td, i + !!signature->hasthis);
1507 body_start_offset = td->new_ip - td->new_code;
1509 for (i = 0; i < header->num_locals; i++) {
1510 int mt = mint_type(header->locals [i]);
1511 if (mt == MINT_TYPE_VT || mt == MINT_TYPE_O || mt == MINT_TYPE_P) {
1512 ADD_CODE(td, MINT_INITLOCALS);
1517 if (sym_seq_points) {
1518 InterpBasicBlock *cbb = td->offset_to_bb [0];
1520 emit_seq_point (td, METHOD_ENTRY_IL_OFFSET, cbb, FALSE);
1523 while (td->ip < end) {
1526 g_assert (td->sp >= td->stack);
1527 g_assert (td->vt_sp < 0x10000000);
1528 in_offset = td->ip - header->code;
1529 td->in_offsets [in_offset] = td->new_ip - td->new_code;
1530 new_in_start_offset = td->new_ip - td->new_code;
1531 td->in_start = td->ip;
1533 MonoDebugLineNumberEntry lne;
1534 lne.native_offset = (guint8*)td->new_ip - (guint8*)td->new_code;
1535 lne.il_offset = in_offset;
1536 g_array_append_val (line_numbers, lne);
1538 if (td->stack_height [in_offset] >= 0) {
1539 g_assert (is_bb_start [in_offset]);
1540 if (td->stack_height [in_offset] > 0)
1541 memcpy (td->stack, td->stack_state [in_offset], td->stack_height [in_offset] * sizeof(td->stack [0]));
1542 td->sp = td->stack + td->stack_height [in_offset];
1543 td->vt_sp = td->vt_stack_size [in_offset];
1545 if (is_bb_start [in_offset]) {
1546 generating_code = 1;
1548 if (!generating_code) {
1549 while (td->ip < end && !is_bb_start [td->ip - td->il_code])
1553 if (td->verbose_level > 1) {
1554 printf("IL_%04lx %s %-10s -> IL_%04lx, sp %ld, %s %-12s vt_sp %u (max %u)\n",
1555 td->ip - td->il_code,
1556 td->is_bb_start [td->ip - td->il_code] == 3 ? "<>" :
1557 td->is_bb_start [td->ip - td->il_code] == 2 ? "< " :
1558 td->is_bb_start [td->ip - td->il_code] == 1 ? " >" : " ",
1559 mono_opcode_name (*td->ip), td->new_ip - td->new_code, td->sp - td->stack,
1560 td->sp > td->stack ? stack_type_string [td->sp [-1].type] : " ",
1561 (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) : "",
1562 td->vt_sp, td->max_vt_sp);
1565 if (sym_seq_points && mono_bitset_test_fast (seq_point_locs, td->ip - header->code)) {
1566 InterpBasicBlock *cbb = td->offset_to_bb [td->ip - header->code];
1570 * Make methods interruptable at the beginning, and at the targets of
1571 * backward branches.
1573 if (in_offset == 0 || g_slist_length (cbb->preds) > 1)
1574 ADD_CODE (td, MINT_SDB_INTR_LOC);
1576 emit_seq_point (td, in_offset, cbb, FALSE);
1578 mono_bitset_set_fast (seq_point_set_locs, td->ip - header->code);
1582 bb_exit = td->offset_to_bb [td->ip - header->code];
1590 SIMPLE_OP(td, MINT_BREAK);
1596 load_arg (td, *td->ip - CEE_LDARG_0);
1603 load_local (td, *td->ip - CEE_LDLOC_0);
1610 store_local (td, *td->ip - CEE_STLOC_0);
1614 load_arg (td, ((guint8 *)td->ip)[1]);
1617 case CEE_LDARGA_S: {
1618 /* NOTE: n includes this */
1619 int n = ((guint8 *) td->ip) [1];
1620 ADD_CODE (td, MINT_LDARGA);
1621 ADD_CODE (td, td->rtm->arg_offsets [n]);
1622 PUSH_SIMPLE_TYPE(td, STACK_TYPE_MP);
1627 store_arg (td, ((guint8 *)td->ip)[1]);
1631 load_local (td, ((guint8 *)td->ip)[1]);
1635 ADD_CODE(td, MINT_LDLOCA_S);
1636 ADD_CODE(td, td->rtm->local_offsets [((guint8 *)td->ip)[1]]);
1637 PUSH_SIMPLE_TYPE(td, STACK_TYPE_MP);
1641 store_local (td, ((guint8 *)td->ip)[1]);
1645 SIMPLE_OP(td, MINT_LDNULL);
1646 PUSH_TYPE(td, STACK_TYPE_O, NULL);
1649 SIMPLE_OP(td, MINT_LDC_I4_M1);
1650 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
1653 if (!td->is_bb_start[td->ip + 1 - td->il_code] && td->ip [1] == 0xfe && td->ip [2] == CEE_CEQ &&
1654 td->sp > td->stack && td->sp [-1].type == STACK_TYPE_I4) {
1655 SIMPLE_OP(td, MINT_CEQ0_I4);
1658 SIMPLE_OP(td, MINT_LDC_I4_0);
1659 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
1663 if (!td->is_bb_start[td->ip + 1 - td->il_code] &&
1664 (td->ip [1] == CEE_ADD || td->ip [1] == CEE_SUB) && td->sp [-1].type == STACK_TYPE_I4) {
1665 ADD_CODE(td, td->ip [1] == CEE_ADD ? MINT_ADD1_I4 : MINT_SUB1_I4);
1668 SIMPLE_OP(td, MINT_LDC_I4_1);
1669 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
1679 SIMPLE_OP(td, (*td->ip - CEE_LDC_I4_0) + MINT_LDC_I4_0);
1680 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
1683 ADD_CODE(td, MINT_LDC_I4_S);
1684 ADD_CODE(td, ((gint8 *) td->ip) [1]);
1686 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
1689 i32 = read32 (td->ip + 1);
1690 ADD_CODE(td, MINT_LDC_I4);
1693 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
1696 gint64 val = read64 (td->ip + 1);
1697 ADD_CODE(td, MINT_LDC_I8);
1700 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I8);
1705 readr4 (td->ip + 1, &val);
1706 ADD_CODE(td, MINT_LDC_R4);
1709 PUSH_SIMPLE_TYPE(td, STACK_TYPE_R8);
1714 readr8 (td->ip + 1, &val);
1715 ADD_CODE(td, MINT_LDC_R8);
1718 PUSH_SIMPLE_TYPE(td, STACK_TYPE_R8);
1722 int type = td->sp [-1].type;
1723 MonoClass *klass = td->sp [-1].klass;
1724 if (td->sp [-1].type == STACK_TYPE_VT) {
1725 gint32 size = mono_class_value_size (klass, NULL);
1727 ADD_CODE(td, MINT_DUP_VT);
1731 SIMPLE_OP(td, MINT_DUP);
1732 PUSH_TYPE(td, type, klass);
1737 SIMPLE_OP(td, MINT_POP);
1739 if (td->sp [-1].type == STACK_TYPE_VT) {
1740 int size = mono_class_value_size (td->sp [-1].klass, NULL);
1741 size = (size + 7) & ~7;
1742 ADD_CODE(td, MINT_VTRESULT);
1751 if (td->sp > td->stack)
1752 g_warning ("CEE_JMP: stack must be empty");
1753 token = read32 (td->ip + 1);
1754 m = mono_get_method_full (image, token, NULL, generic_context);
1755 ADD_CODE (td, MINT_JMP);
1756 ADD_CODE (td, get_data_item_index (td, mono_interp_get_imethod (domain, m, &error)));
1757 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1761 case CEE_CALLVIRT: /* Fall through */
1762 case CEE_CALLI: /* Fall through */
1764 gboolean need_seq_point = FALSE;
1766 if (sym_seq_points && !mono_bitset_test_fast (seq_point_locs, td->ip + 5 - header->code))
1767 need_seq_point = TRUE;
1769 interp_transform_call (td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, constrained_class, readonly);
1771 if (need_seq_point) {
1772 InterpBasicBlock *cbb = td->offset_to_bb [td->ip - header->code];
1775 emit_seq_point (td, td->ip - header->code, cbb, TRUE);
1778 constrained_class = NULL;
1784 if (signature->ret->type != MONO_TYPE_VOID) {
1786 MonoClass *klass = mono_class_from_mono_type (signature->ret);
1787 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1788 vt_size = mono_class_value_size (klass, NULL);
1789 vt_size = (vt_size + 7) & ~7;
1792 if (td->sp > td->stack)
1793 g_warning ("%s.%s: CEE_RET: more values on stack: %d", td->method->klass->name, td->method->name, td->sp - td->stack);
1794 if (td->vt_sp != vt_size)
1795 g_error ("%s.%s: CEE_RET: value type stack: %d vs. %d", td->method->klass->name, td->method->name, td->vt_sp, vt_size);
1797 if (sym_seq_points) {
1798 InterpBasicBlock *cbb = td->offset_to_bb [td->ip - header->code];
1800 emit_seq_point (td, METHOD_EXIT_IL_OFFSET, bb_exit, FALSE);
1804 SIMPLE_OP(td, signature->ret->type == MONO_TYPE_VOID ? MINT_RET_VOID : MINT_RET);
1806 ADD_CODE(td, MINT_RET_VT);
1807 WRITE32(td, &vt_size);
1810 generating_code = 0;
1814 handle_branch (td, MINT_BR_S, MINT_BR, 5 + read32 (td->ip + 1));
1816 generating_code = 0;
1819 handle_branch (td, MINT_BR_S, MINT_BR, 2 + (gint8)td->ip [1]);
1821 generating_code = 0;
1824 one_arg_branch (td, MINT_BRFALSE_I4, 5 + read32 (td->ip + 1));
1828 one_arg_branch (td, MINT_BRFALSE_I4, 2 + (gint8)td->ip [1]);
1832 one_arg_branch (td, MINT_BRTRUE_I4, 5 + read32 (td->ip + 1));
1836 one_arg_branch (td, MINT_BRTRUE_I4, 2 + (gint8)td->ip [1]);
1840 two_arg_branch (td, MINT_BEQ_I4, 5 + read32 (td->ip + 1));
1844 two_arg_branch (td, MINT_BEQ_I4, 2 + (gint8) td->ip [1]);
1848 two_arg_branch (td, MINT_BGE_I4, 5 + read32 (td->ip + 1));
1852 two_arg_branch (td, MINT_BGE_I4, 2 + (gint8) td->ip [1]);
1856 two_arg_branch (td, MINT_BGT_I4, 5 + read32 (td->ip + 1));
1860 two_arg_branch (td, MINT_BGT_I4, 2 + (gint8) td->ip [1]);
1864 two_arg_branch (td, MINT_BLT_I4, 5 + read32 (td->ip + 1));
1868 two_arg_branch (td, MINT_BLT_I4, 2 + (gint8) td->ip [1]);
1872 two_arg_branch (td, MINT_BLE_I4, 5 + read32 (td->ip + 1));
1876 two_arg_branch (td, MINT_BLE_I4, 2 + (gint8) td->ip [1]);
1880 two_arg_branch (td, MINT_BNE_UN_I4, 5 + read32 (td->ip + 1));
1884 two_arg_branch (td, MINT_BNE_UN_I4, 2 + (gint8) td->ip [1]);
1888 two_arg_branch (td, MINT_BGE_UN_I4, 5 + read32 (td->ip + 1));
1892 two_arg_branch (td, MINT_BGE_UN_I4, 2 + (gint8) td->ip [1]);
1896 two_arg_branch (td, MINT_BGT_UN_I4, 5 + read32 (td->ip + 1));
1900 two_arg_branch (td, MINT_BGT_UN_I4, 2 + (gint8) td->ip [1]);
1904 two_arg_branch (td, MINT_BLE_UN_I4, 5 + read32 (td->ip + 1));
1908 two_arg_branch (td, MINT_BLE_UN_I4, 2 + (gint8) td->ip [1]);
1912 two_arg_branch (td, MINT_BLT_UN_I4, 5 + read32 (td->ip + 1));
1916 two_arg_branch (td, MINT_BLT_UN_I4, 2 + (gint8) td->ip [1]);
1921 const unsigned char *next_ip;
1923 n = read32 (td->ip);
1924 ADD_CODE (td, MINT_SWITCH);
1927 next_ip = td->ip + n * 4;
1929 int stack_height = td->sp - td->stack;
1930 for (i = 0; i < n; i++) {
1931 offset = read32 (td->ip);
1932 target = next_ip - td->il_code + offset;
1935 if (stack_height > 0 && stack_height != td->stack_height [target])
1936 g_warning ("SWITCH with back branch and non-empty stack");
1938 target = td->in_offsets [target] - (td->new_ip - td->new_code);
1940 td->stack_height [target] = stack_height;
1941 td->vt_stack_size [target] = td->vt_sp;
1942 if (stack_height > 0)
1943 td->stack_state [target] = g_memdup (td->stack, stack_height * sizeof (td->stack [0]));
1945 Reloc *reloc = mono_mempool_alloc0 (td->mempool, sizeof (Reloc));
1946 reloc->type = RELOC_SWITCH;
1947 reloc->offset = td->new_ip - td->new_code;
1948 reloc->target = target;
1949 g_ptr_array_add (td->relocs, reloc);
1952 WRITE32 (td, &target);
1958 CHECK_STACK (td, 1);
1959 SIMPLE_OP (td, MINT_LDIND_I1);
1960 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
1961 BARRIER_IF_VOLATILE (td);
1964 CHECK_STACK (td, 1);
1965 SIMPLE_OP (td, MINT_LDIND_U1);
1966 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
1967 BARRIER_IF_VOLATILE (td);
1970 CHECK_STACK (td, 1);
1971 SIMPLE_OP (td, MINT_LDIND_I2);
1972 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
1973 BARRIER_IF_VOLATILE (td);
1976 CHECK_STACK (td, 1);
1977 SIMPLE_OP (td, MINT_LDIND_U2);
1978 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
1979 BARRIER_IF_VOLATILE (td);
1982 CHECK_STACK (td, 1);
1983 SIMPLE_OP (td, MINT_LDIND_I4);
1984 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
1985 BARRIER_IF_VOLATILE (td);
1988 CHECK_STACK (td, 1);
1989 SIMPLE_OP (td, MINT_LDIND_U4);
1990 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
1991 BARRIER_IF_VOLATILE (td);
1994 CHECK_STACK (td, 1);
1995 SIMPLE_OP (td, MINT_LDIND_I8);
1996 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
1997 BARRIER_IF_VOLATILE (td);
2000 CHECK_STACK (td, 1);
2001 SIMPLE_OP (td, MINT_LDIND_I);
2003 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
2004 BARRIER_IF_VOLATILE (td);
2007 CHECK_STACK (td, 1);
2008 SIMPLE_OP (td, MINT_LDIND_R4);
2009 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
2010 BARRIER_IF_VOLATILE (td);
2013 CHECK_STACK (td, 1);
2014 SIMPLE_OP (td, MINT_LDIND_R8);
2015 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
2016 BARRIER_IF_VOLATILE (td);
2019 CHECK_STACK (td, 1);
2020 SIMPLE_OP (td, MINT_LDIND_REF);
2021 BARRIER_IF_VOLATILE (td);
2022 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_O);
2025 CHECK_STACK (td, 2);
2026 BARRIER_IF_VOLATILE (td);
2027 SIMPLE_OP (td, MINT_STIND_REF);
2031 CHECK_STACK (td, 2);
2032 BARRIER_IF_VOLATILE (td);
2033 SIMPLE_OP (td, MINT_STIND_I1);
2037 CHECK_STACK (td, 2);
2038 BARRIER_IF_VOLATILE (td);
2039 SIMPLE_OP (td, MINT_STIND_I2);
2043 CHECK_STACK (td, 2);
2044 BARRIER_IF_VOLATILE (td);
2045 SIMPLE_OP (td, MINT_STIND_I4);
2049 CHECK_STACK (td, 2);
2050 BARRIER_IF_VOLATILE (td);
2051 SIMPLE_OP (td, MINT_STIND_I);
2055 CHECK_STACK (td, 2);
2056 BARRIER_IF_VOLATILE (td);
2057 SIMPLE_OP (td, MINT_STIND_I8);
2061 CHECK_STACK (td, 2);
2062 BARRIER_IF_VOLATILE (td);
2063 SIMPLE_OP (td, MINT_STIND_R4);
2067 CHECK_STACK (td, 2);
2068 BARRIER_IF_VOLATILE (td);
2069 SIMPLE_OP (td, MINT_STIND_R8);
2073 binary_arith_op(td, MINT_ADD_I4);
2077 binary_arith_op(td, MINT_SUB_I4);
2081 binary_arith_op(td, MINT_MUL_I4);
2085 binary_arith_op(td, MINT_DIV_I4);
2089 binary_arith_op(td, MINT_DIV_UN_I4);
2093 binary_arith_op (td, MINT_REM_I4);
2097 binary_arith_op (td, MINT_REM_UN_I4);
2101 binary_arith_op (td, MINT_AND_I4);
2105 binary_arith_op (td, MINT_OR_I4);
2109 binary_arith_op (td, MINT_XOR_I4);
2113 shift_op (td, MINT_SHL_I4);
2117 shift_op (td, MINT_SHR_I4);
2121 shift_op (td, MINT_SHR_UN_I4);
2125 unary_arith_op (td, MINT_NEG_I4);
2129 unary_arith_op (td, MINT_NOT_I4);
2133 CHECK_STACK (td, 1);
2134 switch (td->sp [-1].type) {
2136 ADD_CODE(td, MINT_CONV_U1_R8);
2139 ADD_CODE(td, MINT_CONV_U1_I4);
2142 ADD_CODE(td, MINT_CONV_U1_I8);
2145 g_assert_not_reached ();
2148 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2151 CHECK_STACK (td, 1);
2152 switch (td->sp [-1].type) {
2154 ADD_CODE(td, MINT_CONV_I1_R8);
2157 ADD_CODE(td, MINT_CONV_I1_I4);
2160 ADD_CODE(td, MINT_CONV_I1_I8);
2163 g_assert_not_reached ();
2166 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2169 CHECK_STACK (td, 1);
2170 switch (td->sp [-1].type) {
2172 ADD_CODE(td, MINT_CONV_U2_R8);
2175 ADD_CODE(td, MINT_CONV_U2_I4);
2178 ADD_CODE(td, MINT_CONV_U2_I8);
2181 g_assert_not_reached ();
2184 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2187 CHECK_STACK (td, 1);
2188 switch (td->sp [-1].type) {
2190 ADD_CODE(td, MINT_CONV_I2_R8);
2193 ADD_CODE(td, MINT_CONV_I2_I4);
2196 ADD_CODE(td, MINT_CONV_I2_I8);
2199 g_assert_not_reached ();
2202 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2205 CHECK_STACK (td, 1);
2206 switch (td->sp [-1].type) {
2208 #if SIZEOF_VOID_P == 4
2209 ADD_CODE(td, MINT_CONV_U4_R8);
2211 ADD_CODE(td, MINT_CONV_U8_R8);
2215 #if SIZEOF_VOID_P == 8
2216 ADD_CODE(td, MINT_CONV_U8_I4);
2220 #if SIZEOF_VOID_P == 4
2221 ADD_CODE(td, MINT_CONV_U4_I8);
2227 g_assert_not_reached ();
2230 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
2233 CHECK_STACK (td, 1);
2234 switch (td->sp [-1].type) {
2236 #if SIZEOF_VOID_P == 8
2237 ADD_CODE(td, MINT_CONV_I8_R8);
2239 ADD_CODE(td, MINT_CONV_I4_R8);
2243 #if SIZEOF_VOID_P == 8
2244 ADD_CODE(td, MINT_CONV_I8_I4);
2252 #if SIZEOF_VOID_P == 4
2253 ADD_CODE(td, MINT_CONV_I4_I8);
2257 g_assert_not_reached ();
2260 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
2263 CHECK_STACK (td, 1);
2264 switch (td->sp [-1].type) {
2266 ADD_CODE(td, MINT_CONV_U4_R8);
2271 ADD_CODE(td, MINT_CONV_U4_I8);
2274 #if SIZEOF_VOID_P == 8
2275 ADD_CODE(td, MINT_CONV_U4_I8);
2279 g_assert_not_reached ();
2282 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2285 CHECK_STACK (td, 1);
2286 switch (td->sp [-1].type) {
2288 ADD_CODE(td, MINT_CONV_I4_R8);
2293 ADD_CODE(td, MINT_CONV_I4_I8);
2296 #if SIZEOF_VOID_P == 8
2297 ADD_CODE(td, MINT_CONV_I4_I8);
2301 g_assert_not_reached ();
2304 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2307 CHECK_STACK (td, 1);
2308 switch (td->sp [-1].type) {
2310 ADD_CODE(td, MINT_CONV_I8_R8);
2313 ADD_CODE(td, MINT_CONV_I8_I4);
2318 #if SIZEOF_VOID_P == 4
2319 ADD_CODE(td, MINT_CONV_I8_I4);
2323 g_assert_not_reached ();
2326 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
2329 CHECK_STACK (td, 1);
2330 switch (td->sp [-1].type) {
2332 ADD_CODE(td, MINT_CONV_R4_R8);
2335 ADD_CODE(td, MINT_CONV_R4_I8);
2338 ADD_CODE(td, MINT_CONV_R4_I4);
2341 g_assert_not_reached ();
2344 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
2347 CHECK_STACK (td, 1);
2348 switch (td->sp [-1].type) {
2350 ADD_CODE(td, MINT_CONV_R8_I4);
2353 ADD_CODE(td, MINT_CONV_R8_I8);
2358 g_assert_not_reached ();
2361 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
2364 CHECK_STACK (td, 1);
2365 switch (td->sp [-1].type) {
2367 ADD_CODE(td, MINT_CONV_U8_I4);
2372 ADD_CODE(td, MINT_CONV_U8_R8);
2375 #if SIZEOF_VOID_P == 4
2376 ADD_CODE(td, MINT_CONV_U8_I4);
2380 g_assert_not_reached ();
2383 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
2386 CHECK_STACK (td, 2);
2388 token = read32 (td->ip + 1);
2389 klass = mono_class_get_full (image, token, generic_context);
2391 if (klass->valuetype) {
2392 ADD_CODE (td, MINT_CPOBJ);
2393 ADD_CODE (td, get_data_item_index(td, klass));
2395 ADD_CODE (td, MINT_LDIND_REF);
2396 ADD_CODE (td, MINT_STIND_REF);
2404 CHECK_STACK (td, 1);
2406 token = read32 (td->ip + 1);
2408 if (method->wrapper_type != MONO_WRAPPER_NONE)
2409 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2411 klass = mono_class_get_full (image, token, generic_context);
2413 ADD_CODE(td, MINT_LDOBJ);
2414 ADD_CODE(td, get_data_item_index(td, klass));
2415 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
2416 size = mono_class_value_size (klass, NULL);
2420 SET_TYPE(td->sp - 1, stack_type[mint_type(&klass->byval_arg)], klass);
2421 BARRIER_IF_VOLATILE (td);
2426 token = mono_metadata_token_index (read32 (td->ip + 1));
2428 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
2429 s = mono_method_get_wrapper_data (method, token);
2430 } else if (method->wrapper_type != MONO_WRAPPER_NONE) {
2431 s = mono_string_new_wrapper (mono_method_get_wrapper_data (method, token));
2433 s = mono_ldstr (domain, image, token);
2435 ADD_CODE(td, MINT_LDSTR);
2436 ADD_CODE(td, get_data_item_index (td, s));
2437 PUSH_TYPE(td, STACK_TYPE_O, mono_defaults.string_class);
2442 MonoMethodSignature *csignature;
2443 guint32 vt_stack_used = 0;
2444 guint32 vt_res_size = 0;
2447 token = read32 (td->ip);
2450 if (method->wrapper_type != MONO_WRAPPER_NONE)
2451 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
2453 m = mono_get_method_full (image, token, NULL, generic_context);
2455 csignature = mono_method_signature (m);
2457 td->sp -= csignature->param_count;
2458 ADD_CODE(td, MINT_NEWOBJ);
2459 ADD_CODE(td, get_data_item_index (td, mono_interp_get_imethod (domain, m, &error)));
2460 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2462 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
2463 vt_res_size = mono_class_value_size (klass, NULL);
2464 PUSH_VT (td, vt_res_size);
2466 for (i = 0; i < csignature->param_count; ++i) {
2467 int mt = mint_type(csignature->params [i]);
2468 if (mt == MINT_TYPE_VT) {
2469 MonoClass *k = mono_class_from_mono_type (csignature->params [i]);
2470 gint32 size = mono_class_value_size (k, NULL);
2471 size = (size + 7) & ~7;
2472 vt_stack_used += size;
2475 if (vt_stack_used != 0 || vt_res_size != 0) {
2476 ADD_CODE(td, MINT_VTRESULT);
2477 ADD_CODE(td, vt_res_size);
2478 WRITE32(td, &vt_stack_used);
2479 td->vt_sp -= vt_stack_used;
2481 PUSH_TYPE (td, stack_type [mint_type (&klass->byval_arg)], klass);
2485 CHECK_STACK (td, 1);
2486 token = read32 (td->ip + 1);
2487 klass = mini_get_class (method, token, generic_context);
2488 ADD_CODE(td, MINT_CASTCLASS);
2489 ADD_CODE(td, get_data_item_index (td, klass));
2490 td->sp [-1].klass = klass;
2494 CHECK_STACK (td, 1);
2495 token = read32 (td->ip + 1);
2496 klass = mini_get_class (method, token, generic_context);
2497 ADD_CODE(td, MINT_ISINST);
2498 ADD_CODE(td, get_data_item_index (td, klass));
2502 switch (td->sp [-1].type) {
2506 ADD_CODE(td, MINT_CONV_R_UN_I8);
2509 ADD_CODE(td, MINT_CONV_R_UN_I4);
2512 g_assert_not_reached ();
2514 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
2518 CHECK_STACK (td, 1);
2519 token = read32 (td->ip + 1);
2521 if (method->wrapper_type != MONO_WRAPPER_NONE)
2522 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2524 klass = mono_class_get_full (image, token, generic_context);
2526 if (mono_class_is_nullable (klass)) {
2527 g_error ("cee_unbox: implement Nullable");
2530 ADD_CODE(td, MINT_UNBOX);
2531 ADD_CODE(td, get_data_item_index (td, klass));
2532 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
2536 CHECK_STACK (td, 1);
2537 token = read32 (td->ip + 1);
2539 klass = mini_get_class (method, token, generic_context);
2541 if (mini_type_is_reference (&klass->byval_arg)) {
2542 int mt = mint_type (&klass->byval_arg);
2543 ADD_CODE (td, MINT_CASTCLASS);
2544 ADD_CODE (td, get_data_item_index (td, klass));
2545 SET_TYPE (td->sp - 1, stack_type [mt], klass);
2547 } else if (mono_class_is_nullable (klass)) {
2548 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Unbox", 1);
2549 /* td->ip is incremented by interp_transform_call */
2550 interp_transform_call (td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
2552 int mt = mint_type (&klass->byval_arg);
2553 ADD_CODE (td, MINT_UNBOX);
2554 ADD_CODE (td, get_data_item_index (td, klass));
2556 ADD_CODE (td, MINT_LDOBJ);
2557 ADD_CODE (td, get_data_item_index(td, klass));
2558 SET_TYPE (td->sp - 1, stack_type [mt], klass);
2560 if (mt == MINT_TYPE_VT) {
2561 int size = mono_class_value_size (klass, NULL);
2569 CHECK_STACK (td, 1);
2570 SIMPLE_OP (td, MINT_THROW);
2572 generating_code = 0;
2575 CHECK_STACK (td, 1);
2576 token = read32 (td->ip + 1);
2577 field = interp_field_from_token (method, token, &klass, generic_context);
2578 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2579 mono_class_init (klass);
2581 ADD_CODE (td, MINT_POP);
2583 ADD_CODE (td, MINT_LDSFLDA);
2584 ADD_CODE (td, get_data_item_index (td, field));
2586 if ((td->sp - 1)->type == STACK_TYPE_O) {
2587 ADD_CODE (td, MINT_LDFLDA);
2589 g_assert ((td->sp -1)->type == STACK_TYPE_MP);
2590 ADD_CODE (td, MINT_LDFLDA_UNSAFE);
2592 ADD_CODE (td, klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
2595 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
2598 CHECK_STACK (td, 1);
2599 token = read32 (td->ip + 1);
2600 field = interp_field_from_token (method, token, &klass, generic_context);
2601 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2602 mono_class_init (klass);
2604 MonoClass *field_klass = mono_class_from_mono_type (field->type);
2605 mt = mint_type (&field_klass->byval_arg);
2606 #ifndef DISABLE_REMOTING
2607 if (klass->marshalbyref) {
2608 g_assert (!is_static);
2609 ADD_CODE(td, mt == MINT_TYPE_VT ? MINT_LDRMFLD_VT : MINT_LDRMFLD);
2610 ADD_CODE(td, get_data_item_index (td, field));
2615 ADD_CODE (td, MINT_POP);
2617 ADD_CODE (td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2618 ADD_CODE (td, get_data_item_index (td, field));
2620 ADD_CODE (td, MINT_LDFLD_I1 + mt - MINT_TYPE_I1);
2621 ADD_CODE (td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
2624 if (mt == MINT_TYPE_VT) {
2625 int size = mono_class_value_size (field_klass, NULL);
2629 if (td->sp [-1].type == STACK_TYPE_VT) {
2630 int size = mono_class_value_size (klass, NULL);
2631 size = (size + 7) & ~7;
2633 ADD_CODE (td, MINT_VTRESULT);
2635 WRITE32 (td, &size);
2638 SET_TYPE(td->sp - 1, stack_type [mt], field_klass);
2639 BARRIER_IF_VOLATILE (td);
2643 CHECK_STACK (td, 2);
2644 token = read32 (td->ip + 1);
2645 field = interp_field_from_token (method, token, &klass, generic_context);
2646 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2647 mono_class_init (klass);
2648 mt = mint_type(field->type);
2650 BARRIER_IF_VOLATILE (td);
2652 #ifndef DISABLE_REMOTING
2653 if (klass->marshalbyref) {
2654 g_assert (!is_static);
2655 ADD_CODE(td, mt == MINT_TYPE_VT ? MINT_STRMFLD_VT : MINT_STRMFLD);
2656 ADD_CODE(td, get_data_item_index (td, field));
2661 ADD_CODE (td, MINT_POP);
2663 ADD_CODE (td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
2664 ADD_CODE (td, get_data_item_index (td, field));
2666 ADD_CODE (td, MINT_STFLD_I1 + mt - MINT_TYPE_I1);
2667 ADD_CODE (td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
2670 if (mt == MINT_TYPE_VT) {
2671 MonoClass *klass = mono_class_from_mono_type (field->type);
2672 int size = mono_class_value_size (klass, NULL);
2681 token = read32 (td->ip + 1);
2682 field = interp_field_from_token (method, token, &klass, generic_context);
2683 ADD_CODE(td, MINT_LDSFLDA);
2684 ADD_CODE(td, get_data_item_index (td, field));
2686 PUSH_SIMPLE_TYPE(td, STACK_TYPE_MP);
2689 token = read32 (td->ip + 1);
2690 field = interp_field_from_token (method, token, &klass, generic_context);
2691 mt = mint_type(field->type);
2692 ADD_CODE(td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2693 ADD_CODE(td, get_data_item_index (td, field));
2695 if (mt == MINT_TYPE_VT) {
2696 MonoClass *klass = mono_class_from_mono_type (field->type);
2697 int size = mono_class_value_size (klass, NULL);
2700 klass = field->type->data.klass;
2702 if (mt == MINT_TYPE_O)
2703 klass = mono_class_from_mono_type (field->type);
2706 PUSH_TYPE(td, stack_type [mt], klass);
2709 CHECK_STACK (td, 1);
2710 token = read32 (td->ip + 1);
2711 field = interp_field_from_token (method, token, &klass, generic_context);
2712 mt = mint_type(field->type);
2713 ADD_CODE(td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
2714 ADD_CODE(td, get_data_item_index (td, field));
2715 if (mt == MINT_TYPE_VT) {
2716 MonoClass *klass = mono_class_from_mono_type (field->type);
2717 int size = mono_class_value_size (klass, NULL);
2719 WRITE32 (td, &size);
2726 token = read32 (td->ip + 1);
2728 if (method->wrapper_type != MONO_WRAPPER_NONE)
2729 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2731 klass = mini_get_class (method, token, generic_context);
2733 BARRIER_IF_VOLATILE (td);
2734 ADD_CODE(td, td->sp [-1].type == STACK_TYPE_VT ? MINT_STOBJ_VT : MINT_STOBJ);
2735 ADD_CODE(td, get_data_item_index (td, klass));
2736 if (td->sp [-1].type == STACK_TYPE_VT) {
2737 size = mono_class_value_size (klass, NULL);
2738 size = (size + 7) & ~7;
2745 case CEE_CONV_OVF_I_UN:
2746 case CEE_CONV_OVF_U_UN:
2747 CHECK_STACK (td, 1);
2748 switch (td->sp [-1].type) {
2750 #if SIZEOF_VOID_P == 8
2751 ADD_CODE(td, MINT_CONV_OVF_I8_UN_R8);
2753 ADD_CODE(td, MINT_CONV_OVF_I4_UN_R8);
2757 #if SIZEOF_VOID_P == 4
2758 ADD_CODE (td, MINT_CONV_OVF_I4_UN_I8);
2762 #if SIZEOF_VOID_P == 8
2763 ADD_CODE(td, MINT_CONV_I8_U4);
2764 #elif SIZEOF_VOID_P == 4
2765 if (*td->ip == CEE_CONV_OVF_I_UN)
2766 ADD_CODE(td, MINT_CONV_OVF_I4_U4);
2770 g_assert_not_reached ();
2773 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
2776 case CEE_CONV_OVF_I8_UN:
2777 case CEE_CONV_OVF_U8_UN:
2778 CHECK_STACK (td, 1);
2779 switch (td->sp [-1].type) {
2781 ADD_CODE(td, MINT_CONV_OVF_I8_UN_R8);
2784 if (*td->ip == CEE_CONV_OVF_I8_UN)
2785 ADD_CODE (td, MINT_CONV_OVF_I8_U8);
2788 ADD_CODE(td, MINT_CONV_I8_U4);
2791 g_assert_not_reached ();
2794 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
2799 CHECK_STACK (td, 1);
2800 token = read32 (td->ip + 1);
2801 if (method->wrapper_type != MONO_WRAPPER_NONE)
2802 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2804 klass = mini_get_class (method, token, generic_context);
2806 if (mono_class_is_nullable (klass)) {
2807 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Box", 1);
2808 /* td->ip is incremented by interp_transform_call */
2809 interp_transform_call (td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
2810 } else if (!klass->valuetype) {
2811 /* already boxed, do nothing. */
2814 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT && !klass->enumtype) {
2815 size = mono_class_value_size (klass, NULL);
2816 size = (size + 7) & ~7;
2819 ADD_CODE(td, MINT_BOX);
2820 ADD_CODE(td, get_data_item_index (td, klass));
2822 SET_TYPE(td->sp - 1, STACK_TYPE_O, klass);
2829 CHECK_STACK (td, 1);
2830 token = read32 (td->ip + 1);
2832 if (method->wrapper_type != MONO_WRAPPER_NONE)
2833 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2835 klass = mini_get_class (method, token, generic_context);
2837 unsigned char lentype = (td->sp - 1)->type;
2838 if (lentype == STACK_TYPE_I8) {
2839 /* mimic mini behaviour */
2840 ADD_CODE (td, MINT_CONV_OVF_U4_I8);
2842 g_assert (lentype == STACK_TYPE_I4);
2843 ADD_CODE (td, MINT_CONV_OVF_U4_I4);
2845 SET_SIMPLE_TYPE (td->sp - 1, STACK_TYPE_I4);
2846 ADD_CODE (td, MINT_NEWARR);
2847 ADD_CODE (td, get_data_item_index (td, klass));
2848 SET_TYPE (td->sp - 1, STACK_TYPE_O, klass);
2853 CHECK_STACK (td, 1);
2854 SIMPLE_OP (td, MINT_LDLEN);
2855 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
2858 CHECK_STACK (td, 2);
2860 token = read32 (td->ip + 1);
2862 if (method->wrapper_type != MONO_WRAPPER_NONE)
2863 klass = (MonoClass *) mono_method_get_wrapper_data (method, token);
2865 klass = mini_get_class (method, token, generic_context);
2867 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
2868 ADD_CODE (td, MINT_LDELEMA_TC);
2870 ADD_CODE (td, MINT_LDELEMA);
2872 ADD_CODE (td, get_data_item_index (td, klass));
2873 /* according to spec, ldelema bytecode is only used for 1-dim arrays */
2879 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
2882 CHECK_STACK (td, 2);
2884 SIMPLE_OP (td, MINT_LDELEM_I1);
2886 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2889 CHECK_STACK (td, 2);
2891 SIMPLE_OP (td, MINT_LDELEM_U1);
2893 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2896 CHECK_STACK (td, 2);
2898 SIMPLE_OP (td, MINT_LDELEM_I2);
2900 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2903 CHECK_STACK (td, 2);
2905 SIMPLE_OP (td, MINT_LDELEM_U2);
2907 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2910 CHECK_STACK (td, 2);
2912 SIMPLE_OP (td, MINT_LDELEM_I4);
2914 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2917 CHECK_STACK (td, 2);
2919 SIMPLE_OP (td, MINT_LDELEM_U4);
2921 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2924 CHECK_STACK (td, 2);
2926 SIMPLE_OP (td, MINT_LDELEM_I8);
2928 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
2931 CHECK_STACK (td, 2);
2933 SIMPLE_OP (td, MINT_LDELEM_I);
2935 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
2938 CHECK_STACK (td, 2);
2940 SIMPLE_OP (td, MINT_LDELEM_R4);
2942 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
2945 CHECK_STACK (td, 2);
2947 SIMPLE_OP (td, MINT_LDELEM_R8);
2949 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
2951 case CEE_LDELEM_REF:
2952 CHECK_STACK (td, 2);
2954 SIMPLE_OP (td, MINT_LDELEM_REF);
2956 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_O);
2959 CHECK_STACK (td, 2);
2960 token = read32 (td->ip + 1);
2961 klass = mini_get_class (method, token, generic_context);
2962 switch (mint_type (&klass->byval_arg)) {
2965 SIMPLE_OP (td, MINT_LDELEM_I1);
2967 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2971 SIMPLE_OP (td, MINT_LDELEM_U1);
2973 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2977 SIMPLE_OP (td, MINT_LDELEM_U2);
2979 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2983 SIMPLE_OP (td, MINT_LDELEM_I2);
2985 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2989 SIMPLE_OP (td, MINT_LDELEM_I4);
2991 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2995 SIMPLE_OP (td, MINT_LDELEM_I8);
2997 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
3001 SIMPLE_OP (td, MINT_LDELEM_R4);
3003 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
3007 SIMPLE_OP (td, MINT_LDELEM_R8);
3009 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
3013 SIMPLE_OP (td, MINT_LDELEM_REF);
3015 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_O);
3017 case MINT_TYPE_VT: {
3018 int size = mono_class_value_size (klass, NULL);
3020 SIMPLE_OP (td, MINT_LDELEM_VT);
3021 ADD_CODE (td, get_data_item_index (td, klass));
3022 WRITE32 (td, &size);
3024 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_VT);
3029 GString *res = g_string_new ("");
3030 mono_type_get_desc (res, &klass->byval_arg, TRUE);
3031 g_print ("LDELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
3032 g_string_free (res, TRUE);
3040 CHECK_STACK (td, 3);
3042 SIMPLE_OP (td, MINT_STELEM_I);
3046 CHECK_STACK (td, 3);
3048 SIMPLE_OP (td, MINT_STELEM_I1);
3052 CHECK_STACK (td, 3);
3054 SIMPLE_OP (td, MINT_STELEM_I2);
3058 CHECK_STACK (td, 3);
3060 SIMPLE_OP (td, MINT_STELEM_I4);
3064 CHECK_STACK (td, 3);
3066 SIMPLE_OP (td, MINT_STELEM_I8);
3070 CHECK_STACK (td, 3);
3072 SIMPLE_OP (td, MINT_STELEM_R4);
3076 CHECK_STACK (td, 3);
3078 SIMPLE_OP (td, MINT_STELEM_R8);
3081 case CEE_STELEM_REF:
3082 CHECK_STACK (td, 3);
3084 SIMPLE_OP (td, MINT_STELEM_REF);
3088 CHECK_STACK (td, 3);
3090 token = read32 (td->ip + 1);
3091 klass = mini_get_class (method, token, generic_context);
3092 switch (mint_type (&klass->byval_arg)) {
3094 SIMPLE_OP (td, MINT_STELEM_U1);
3097 SIMPLE_OP (td, MINT_STELEM_U2);
3100 SIMPLE_OP (td, MINT_STELEM_I4);
3103 SIMPLE_OP (td, MINT_STELEM_I8);
3106 SIMPLE_OP (td, MINT_STELEM_REF);
3108 case MINT_TYPE_VT: {
3109 int size = mono_class_value_size (klass, NULL);
3110 SIMPLE_OP (td, MINT_STELEM_VT);
3111 ADD_CODE (td, get_data_item_index (td, klass));
3112 WRITE32 (td, &size);
3117 GString *res = g_string_new ("");
3118 mono_type_get_desc (res, &klass->byval_arg, TRUE);
3119 g_print ("STELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
3120 g_string_free (res, TRUE);
3129 case CEE_CONV_OVF_U1:
3131 case CEE_CONV_OVF_I8:
3133 #if SIZEOF_VOID_P == 8
3134 case CEE_CONV_OVF_U:
3138 CHECK_STACK (td, 1);
3139 SIMPLE_OP (td, MINT_CKFINITE);
3142 CHECK_STACK (td, 1);
3144 token = read32 (td->ip + 1);
3145 klass = mini_get_class (method, token, generic_context);
3147 ADD_CODE (td, MINT_MKREFANY);
3148 ADD_CODE (td, get_data_item_index (td, klass));
3151 PUSH_VT (td, sizeof (MonoTypedRef));
3152 SET_TYPE(td->sp - 1, STACK_TYPE_VT, mono_defaults.typed_reference_class);
3154 case CEE_REFANYVAL: {
3155 CHECK_STACK (td, 1);
3157 token = read32 (td->ip + 1);
3159 ADD_CODE (td, MINT_REFANYVAL);
3161 POP_VT (td, sizeof (MonoTypedRef));
3162 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
3167 case CEE_CONV_OVF_I1:
3168 case CEE_CONV_OVF_I1_UN:
3169 CHECK_STACK (td, 1);
3170 switch (td->sp [-1].type) {
3172 ADD_CODE(td, MINT_CONV_OVF_I1_R8);
3175 ADD_CODE(td, MINT_CONV_OVF_I1_I4);
3178 ADD_CODE(td, MINT_CONV_OVF_I1_I8);
3181 g_assert_not_reached ();
3184 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3186 case CEE_CONV_OVF_U1:
3187 case CEE_CONV_OVF_U1_UN:
3188 CHECK_STACK (td, 1);
3189 switch (td->sp [-1].type) {
3191 ADD_CODE(td, MINT_CONV_OVF_U1_R8);
3194 ADD_CODE(td, MINT_CONV_OVF_U1_I4);
3197 ADD_CODE(td, MINT_CONV_OVF_U1_I8);
3200 g_assert_not_reached ();
3203 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3205 case CEE_CONV_OVF_I2:
3206 case CEE_CONV_OVF_I2_UN:
3207 CHECK_STACK (td, 1);
3208 switch (td->sp [-1].type) {
3210 ADD_CODE(td, MINT_CONV_OVF_I2_R8);
3213 ADD_CODE(td, MINT_CONV_OVF_I2_I4);
3216 ADD_CODE(td, MINT_CONV_OVF_I2_I8);
3219 g_assert_not_reached ();
3222 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3224 case CEE_CONV_OVF_U2_UN:
3225 case CEE_CONV_OVF_U2:
3226 CHECK_STACK (td, 1);
3227 switch (td->sp [-1].type) {
3229 ADD_CODE(td, MINT_CONV_OVF_U2_R8);
3232 ADD_CODE(td, MINT_CONV_OVF_U2_I4);
3235 ADD_CODE(td, MINT_CONV_OVF_U2_I8);
3238 g_assert_not_reached ();
3241 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3243 #if SIZEOF_VOID_P == 4
3244 case CEE_CONV_OVF_I:
3246 case CEE_CONV_OVF_I4:
3247 case CEE_CONV_OVF_I4_UN:
3248 CHECK_STACK (td, 1);
3249 switch (td->sp [-1].type) {
3251 ADD_CODE(td, MINT_CONV_OVF_I4_R8);
3254 if (*td->ip == CEE_CONV_OVF_I4_UN)
3255 ADD_CODE(td, MINT_CONV_OVF_I4_U4);
3258 if (*td->ip == CEE_CONV_OVF_I4_UN)
3259 ADD_CODE (td, MINT_CONV_OVF_I4_U8);
3261 ADD_CODE (td, MINT_CONV_OVF_I4_I8);
3264 g_assert_not_reached ();
3267 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3269 #if SIZEOF_VOID_P == 4
3270 case CEE_CONV_OVF_U:
3272 case CEE_CONV_OVF_U4:
3273 case CEE_CONV_OVF_U4_UN:
3274 CHECK_STACK (td, 1);
3275 switch (td->sp [-1].type) {
3277 ADD_CODE(td, MINT_CONV_OVF_U4_R8);
3280 if (*td->ip != CEE_CONV_OVF_U4_UN)
3281 ADD_CODE(td, MINT_CONV_OVF_U4_I4);
3284 ADD_CODE(td, MINT_CONV_OVF_U4_I8);
3287 g_assert_not_reached ();
3290 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3292 #if SIZEOF_VOID_P == 8
3293 case CEE_CONV_OVF_I:
3295 case CEE_CONV_OVF_I8:
3296 CHECK_STACK (td, 1);
3297 switch (td->sp [-1].type) {
3299 ADD_CODE(td, MINT_CONV_OVF_I8_R8);
3302 ADD_CODE(td, MINT_CONV_I8_I4);
3307 g_assert_not_reached ();
3310 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
3312 #if SIZEOF_VOID_P == 8
3313 case CEE_CONV_OVF_U:
3315 case CEE_CONV_OVF_U8:
3316 CHECK_STACK (td, 1);
3317 switch (td->sp [-1].type) {
3319 ADD_CODE(td, MINT_CONV_OVF_U8_R8);
3322 ADD_CODE(td, MINT_CONV_OVF_U8_I4);
3325 ADD_CODE (td, MINT_CONV_OVF_U8_I8);
3328 g_assert_not_reached ();
3331 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
3336 token = read32 (td->ip + 1);
3337 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
3338 handle = mono_method_get_wrapper_data (method, token);
3339 klass = (MonoClass *) mono_method_get_wrapper_data (method, token + 1);
3340 if (klass == mono_defaults.typehandle_class)
3341 handle = &((MonoClass *) handle)->byval_arg;
3343 if (generic_context) {
3344 handle = mono_class_inflate_generic_type_checked (handle, generic_context, &error);
3345 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3348 handle = mono_ldtoken (image, token, &klass, generic_context);
3350 mono_class_init (klass);
3351 mt = mint_type (&klass->byval_arg);
3352 g_assert (mt == MINT_TYPE_VT);
3353 size = mono_class_value_size (klass, NULL);
3354 g_assert (size == sizeof(gpointer));
3355 PUSH_VT (td, sizeof(gpointer));
3356 ADD_CODE (td, MINT_LDTOKEN);
3357 ADD_CODE (td, get_data_item_index (td, handle));
3359 SET_TYPE (td->sp, stack_type [mt], klass);
3365 binary_arith_op(td, MINT_ADD_OVF_I4);
3368 case CEE_ADD_OVF_UN:
3369 binary_arith_op(td, MINT_ADD_OVF_UN_I4);
3373 binary_arith_op(td, MINT_MUL_OVF_I4);
3376 case CEE_MUL_OVF_UN:
3377 binary_arith_op(td, MINT_MUL_OVF_UN_I4);
3381 binary_arith_op(td, MINT_SUB_OVF_I4);
3384 case CEE_SUB_OVF_UN:
3385 binary_arith_op(td, MINT_SUB_OVF_UN_I4);
3388 case CEE_ENDFINALLY:
3389 g_assert (td->clause_indexes [in_offset] != -1);
3391 SIMPLE_OP (td, MINT_ENDFINALLY);
3392 ADD_CODE (td, td->clause_indexes [in_offset]);
3393 generating_code = 0;
3397 handle_branch (td, MINT_LEAVE_S, MINT_LEAVE, 5 + read32 (td->ip + 1));
3399 generating_code = 0;
3403 handle_branch (td, MINT_LEAVE_S, MINT_LEAVE, 2 + (gint8)td->ip [1]);
3405 generating_code = 0;
3410 case CEE_MONO_CALLI_EXTRA_ARG:
3411 /* Same as CEE_CALLI, except that we drop the extra arg required for llvm specific behaviour */
3412 ADD_CODE (td, MINT_POP);
3415 interp_transform_call (td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
3417 case CEE_MONO_JIT_ICALL_ADDR: {
3420 MonoJitICallInfo *info;
3422 token = read32 (td->ip + 1);
3424 func = mono_method_get_wrapper_data (method, token);
3425 info = mono_find_jit_icall_by_addr (func);
3427 ADD_CODE (td, MINT_LDFTN);
3428 ADD_CODE (td, get_data_item_index (td, func));
3429 PUSH_SIMPLE_TYPE (td, STACK_TYPE_I);
3432 case CEE_MONO_ICALL: {
3435 MonoJitICallInfo *info;
3437 token = read32 (td->ip + 1);
3439 func = mono_method_get_wrapper_data (method, token);
3440 info = mono_find_jit_icall_by_addr (func);
3443 CHECK_STACK (td, info->sig->param_count);
3444 switch (info->sig->param_count) {
3446 if (MONO_TYPE_IS_VOID (info->sig->ret))
3447 ADD_CODE (td,MINT_ICALL_V_V);
3449 ADD_CODE (td, MINT_ICALL_V_P);
3452 if (MONO_TYPE_IS_VOID (info->sig->ret))
3453 ADD_CODE (td,MINT_ICALL_P_V);
3455 ADD_CODE (td,MINT_ICALL_P_P);
3458 if (MONO_TYPE_IS_VOID (info->sig->ret)) {
3459 if (info->sig->params [1]->type == MONO_TYPE_I4)
3460 ADD_CODE (td,MINT_ICALL_PI_V);
3462 ADD_CODE (td,MINT_ICALL_PP_V);
3464 if (info->sig->params [1]->type == MONO_TYPE_I4)
3465 ADD_CODE (td,MINT_ICALL_PI_P);
3467 ADD_CODE (td,MINT_ICALL_PP_P);
3471 g_assert (MONO_TYPE_IS_VOID (info->sig->ret));
3472 if (info->sig->params [2]->type == MONO_TYPE_I4)
3473 ADD_CODE (td,MINT_ICALL_PPI_V);
3475 ADD_CODE (td,MINT_ICALL_PPP_V);
3478 g_assert_not_reached ();
3481 if (func == mono_ftnptr_to_delegate) {
3482 g_error ("TODO: ?");
3484 ADD_CODE(td, get_data_item_index (td, func));
3485 td->sp -= info->sig->param_count;
3487 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
3489 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
3493 case CEE_MONO_VTADDR: {
3495 CHECK_STACK (td, 1);
3496 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
3497 size = mono_class_native_size(td->sp [-1].klass, NULL);
3499 size = mono_class_value_size(td->sp [-1].klass, NULL);
3500 size = (size + 7) & ~7;
3501 ADD_CODE(td, MINT_VTRESULT);
3506 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
3509 case CEE_MONO_LDPTR:
3510 case CEE_MONO_CLASSCONST:
3511 token = read32 (td->ip + 1);
3513 ADD_CODE(td, MINT_MONO_LDPTR);
3514 ADD_CODE(td, get_data_item_index (td, mono_method_get_wrapper_data (method, token)));
3515 td->sp [0].type = STACK_TYPE_I;
3518 case CEE_MONO_OBJADDR:
3519 CHECK_STACK (td, 1);
3521 td->sp[-1].type = STACK_TYPE_MP;
3524 case CEE_MONO_NEWOBJ:
3525 token = read32 (td->ip + 1);
3527 ADD_CODE(td, MINT_MONO_NEWOBJ);
3528 ADD_CODE(td, get_data_item_index (td, mono_method_get_wrapper_data (method, token)));
3529 td->sp [0].type = STACK_TYPE_O;
3532 case CEE_MONO_RETOBJ:
3533 CHECK_STACK (td, 1);
3534 token = read32 (td->ip + 1);
3536 ADD_CODE(td, MINT_MONO_RETOBJ);
3539 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3541 /*stackval_from_data (signature->ret, frame->retval, sp->data.vt, signature->pinvoke);*/
3543 if (td->sp > td->stack)
3544 g_warning ("CEE_MONO_RETOBJ: more values on stack: %d", td->sp-td->stack);
3546 case CEE_MONO_LDNATIVEOBJ:
3547 token = read32 (td->ip + 1);
3549 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3550 g_assert(klass->valuetype);
3551 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
3553 case CEE_MONO_TLS: {
3554 gint32 key = read32 (td->ip + 1);
3556 g_assert (key < TLS_KEY_NUM);
3557 ADD_CODE (td, MINT_MONO_TLS);
3559 PUSH_SIMPLE_TYPE (td, STACK_TYPE_MP);
3562 case CEE_MONO_ATOMIC_STORE_I4:
3563 CHECK_STACK (td, 2);
3564 SIMPLE_OP (td, MINT_MONO_ATOMIC_STORE_I4);
3568 case CEE_MONO_SAVE_LMF:
3569 case CEE_MONO_RESTORE_LMF:
3570 case CEE_MONO_NOT_TAKEN:
3573 case CEE_MONO_LDPTR_INT_REQ_FLAG:
3574 ADD_CODE (td, MINT_MONO_LDPTR);
3575 ADD_CODE (td, get_data_item_index (td, mono_thread_interruption_request_flag ()));
3576 PUSH_TYPE (td, STACK_TYPE_MP, NULL);
3579 case CEE_MONO_MEMORY_BARRIER:
3580 ADD_CODE (td, MINT_MONO_MEMORY_BARRIER);
3583 case CEE_MONO_JIT_ATTACH:
3584 ADD_CODE (td, MINT_MONO_JIT_ATTACH);
3587 case CEE_MONO_JIT_DETACH:
3588 ADD_CODE (td, MINT_MONO_JIT_DETACH);
3591 case CEE_MONO_LDDOMAIN:
3592 ADD_CODE (td, MINT_MONO_LDDOMAIN);
3593 td->sp [0].type = STACK_TYPE_I;
3598 g_error ("transform.c: Unimplemented opcode: 0xF0 %02x at 0x%x\n", *td->ip, td->ip-header->code);
3608 case CEE_PREFIXREF: ves_abort(); break;
3611 * Note: Exceptions thrown when executing a prefixed opcode need
3612 * to take into account the number of prefix bytes (usually the
3613 * throw point is just (ip - n_prefix_bytes).
3619 case CEE_ARGLIST: ves_abort(); break;
3623 if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
3624 ADD_CODE(td, MINT_CEQ_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3626 ADD_CODE(td, MINT_CEQ_I4 + td->sp [-1].type - STACK_TYPE_I4);
3628 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3633 if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
3634 ADD_CODE(td, MINT_CGT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3636 ADD_CODE(td, MINT_CGT_I4 + td->sp [-1].type - STACK_TYPE_I4);
3638 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3643 if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
3644 ADD_CODE(td, MINT_CGT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3646 ADD_CODE(td, MINT_CGT_UN_I4 + td->sp [-1].type - STACK_TYPE_I4);
3648 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3653 if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
3654 ADD_CODE(td, MINT_CLT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3656 ADD_CODE(td, MINT_CLT_I4 + td->sp [-1].type - STACK_TYPE_I4);
3658 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3663 if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
3664 ADD_CODE(td, MINT_CLT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3666 ADD_CODE(td, MINT_CLT_UN_I4 + td->sp [-1].type - STACK_TYPE_I4);
3668 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3671 case CEE_LDVIRTFTN: /* fallthrough */
3674 if (*td->ip == CEE_LDVIRTFTN) {
3675 CHECK_STACK (td, 1);
3678 token = read32 (td->ip + 1);
3679 if (method->wrapper_type != MONO_WRAPPER_NONE)
3680 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3682 m = mono_get_method_full (image, token, NULL, generic_context);
3684 if (method->wrapper_type == MONO_WRAPPER_NONE && m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
3685 m = mono_marshal_get_synchronized_wrapper (m);
3687 ADD_CODE(td, *td->ip == CEE_LDFTN ? MINT_LDFTN : MINT_LDVIRTFTN);
3688 ADD_CODE(td, get_data_item_index (td, mono_interp_get_imethod (domain, m, &error)));
3689 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3691 PUSH_SIMPLE_TYPE (td, STACK_TYPE_F);
3695 load_arg (td, read16 (td->ip + 1));
3699 int n = read16 (td->ip + 1);
3700 ADD_CODE (td, MINT_LDARGA);
3701 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offsets */
3702 PUSH_SIMPLE_TYPE(td, STACK_TYPE_MP);
3707 store_arg (td, read16 (td->ip + 1));
3711 load_local (td, read16 (td->ip + 1));
3715 ADD_CODE(td, MINT_LDLOCA_S);
3716 ADD_CODE(td, td->rtm->local_offsets [read16 (td->ip + 1)]);
3717 PUSH_SIMPLE_TYPE(td, STACK_TYPE_MP);
3721 store_local (td, read16 (td->ip + 1));
3725 CHECK_STACK (td, 1);
3726 #if SIZEOF_VOID_P == 8
3727 if (td->sp [-1].type == STACK_TYPE_I8)
3728 ADD_CODE(td, MINT_CONV_I4_I8);
3730 ADD_CODE(td, MINT_LOCALLOC);
3731 if (td->sp != td->stack + 1)
3732 g_warning("CEE_LOCALLOC: stack not empty");
3734 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
3737 case CEE_UNUSED57: ves_abort(); break;
3740 ADD_CODE (td, MINT_ENDFILTER);
3743 case CEE_UNALIGNED_:
3752 /* FIX: should do something? */;
3756 token = read32 (td->ip + 1);
3757 klass = mini_get_class (method, token, generic_context);
3758 if (klass->valuetype) {
3759 ADD_CODE (td, MINT_INITOBJ);
3760 i32 = mono_class_value_size (klass, NULL);
3763 ADD_CODE (td, MINT_LDNULL);
3764 ADD_CODE (td, MINT_STIND_REF);
3771 /* FIX? convert length to I8? */
3773 ADD_CODE (td, MINT_MONO_MEMORY_BARRIER);
3774 ADD_CODE(td, MINT_CPBLK);
3775 BARRIER_IF_VOLATILE (td);
3783 case CEE_CONSTRAINED_:
3784 token = read32 (td->ip + 1);
3785 constrained_class = mini_get_class (method, token, generic_context);
3786 mono_class_init (constrained_class);
3791 BARRIER_IF_VOLATILE (td);
3792 ADD_CODE(td, MINT_INITBLK);
3797 /* FIXME: implement */
3801 int clause_index = td->clause_indexes [in_offset];
3802 g_assert (clause_index != -1);
3803 SIMPLE_OP (td, MINT_RETHROW);
3804 ADD_CODE (td, rtm->exvar_offsets [clause_index]);
3805 generating_code = 0;
3810 token = read32 (td->ip + 1);
3812 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC && !image_is_dynamic (method->klass->image) && !generic_context) {
3814 MonoType *type = mono_type_create_from_typespec (image, token);
3815 size = mono_type_size (type, &align);
3818 MonoClass *szclass = mini_get_class (method, token, generic_context);
3819 mono_class_init (szclass);
3821 if (!szclass->valuetype)
3822 THROW_EX (mono_exception_from_name (mono_defaults.corlib, "System", "InvalidProgramException"), ip - 5);
3824 size = mono_type_size (&szclass->byval_arg, &align);
3826 ADD_CODE(td, MINT_LDC_I4);
3828 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
3831 case CEE_REFANYTYPE:
3832 ADD_CODE (td, MINT_REFANYTYPE);
3834 POP_VT (td, sizeof (MonoTypedRef));
3835 PUSH_VT (td, sizeof (gpointer));
3836 SET_TYPE(td->sp - 1, STACK_TYPE_VT, NULL);
3839 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);
3843 g_error ("transform.c: Unimplemented opcode: %02x at 0x%x\n", *td->ip, td->ip-header->code);
3846 if (td->new_ip - td->new_code != new_in_start_offset)
3847 td->last_new_ip = td->new_code + new_in_start_offset;
3848 else if (td->is_bb_start [td->in_start - td->il_code])
3849 td->is_bb_start [td->ip - td->il_code] = 1;
3851 td->last_ip = td->in_start;
3854 /* Handle relocations */
3855 for (int i = 0; i < td->relocs->len; ++i) {
3856 Reloc *reloc = g_ptr_array_index (td->relocs, i);
3858 int offset = td->in_offsets [reloc->target] - reloc->offset;
3860 switch (reloc->type) {
3861 case RELOC_SHORT_BRANCH:
3862 g_assert (td->new_code [reloc->offset + 1] == 0xffff);
3863 td->new_code [reloc->offset + 1] = offset;
3865 case RELOC_LONG_BRANCH: {
3866 guint16 *v = (guint16 *) &offset;
3867 g_assert (td->new_code [reloc->offset + 1] == 0xbeef);
3868 g_assert (td->new_code [reloc->offset + 2] == 0xdead);
3869 td->new_code [reloc->offset + 1] = *(guint16 *) v;
3870 td->new_code [reloc->offset + 2] = *(guint16 *) (v + 1);
3873 case RELOC_SWITCH: {
3874 guint16 *v = (guint16*)&offset;
3875 td->new_code [reloc->offset] = *(guint16*)v;
3876 td->new_code [reloc->offset + 1] = *(guint16*)(v + 1);
3880 g_assert_not_reached ();
3885 if (td->verbose_level) {
3886 const guint16 *p = td->new_code;
3887 printf("Runtime method: %s %p, VT stack size: %d\n", mono_method_full_name (method, TRUE), rtm, td->max_vt_sp);
3888 printf("Calculated stack size: %d, stated size: %d\n", td->max_stack_height, header->max_stack);
3889 while (p < td->new_ip) {
3890 p = mono_interp_dis_mintop(td->new_code, p);
3894 g_assert (td->max_stack_height <= (header->max_stack + 1));
3896 int code_len = td->new_ip - td->new_code;
3898 rtm->clauses = mono_domain_alloc0 (domain, header->num_clauses * sizeof (MonoExceptionClause));
3899 memcpy (rtm->clauses, header->clauses, header->num_clauses * sizeof(MonoExceptionClause));
3900 rtm->code = mono_domain_alloc0 (domain, (td->new_ip - td->new_code) * sizeof (gushort));
3901 memcpy (rtm->code, td->new_code, (td->new_ip - td->new_code) * sizeof(gushort));
3902 g_free (td->new_code);
3903 rtm->new_body_start = rtm->code + body_start_offset;
3904 rtm->num_clauses = header->num_clauses;
3905 for (i = 0; i < header->num_clauses; i++) {
3906 MonoExceptionClause *c = rtm->clauses + i;
3907 int end_off = c->try_offset + c->try_len;
3908 c->try_offset = td->in_offsets [c->try_offset];
3909 c->try_len = td->in_offsets [end_off] - c->try_offset;
3910 end_off = c->handler_offset + c->handler_len;
3911 c->handler_offset = td->in_offsets [c->handler_offset];
3912 c->handler_len = td->in_offsets [end_off] - c->handler_offset;
3913 if (c->flags & MONO_EXCEPTION_CLAUSE_FILTER)
3914 c->data.filter_offset = td->in_offsets [c->data.filter_offset];
3916 rtm->vt_stack_size = td->max_vt_sp;
3917 rtm->alloca_size = rtm->locals_size + rtm->args_size + rtm->vt_stack_size + rtm->stack_size;
3918 rtm->data_items = mono_domain_alloc0 (domain, td->n_data_items * sizeof (td->data_items [0]));
3919 memcpy (rtm->data_items, td->data_items, td->n_data_items * sizeof (td->data_items [0]));
3921 /* Save debug info */
3922 interp_save_debug_info (rtm, header, td, line_numbers);
3924 /* Create a MonoJitInfo for the interpreted method by creating the interpreter IR as the native code. */
3925 int jinfo_len = mono_jit_info_size (0, header->num_clauses, 0);
3926 MonoJitInfo *jinfo = (MonoJitInfo *)mono_domain_alloc0 (domain, jinfo_len);
3927 jinfo->is_interp = 1;
3929 mono_jit_info_init (jinfo, method, (guint8*)rtm->code, code_len, 0, header->num_clauses, 0);
3930 for (i = 0; i < jinfo->num_clauses; ++i) {
3931 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
3932 MonoExceptionClause *c = rtm->clauses + i;
3934 ei->flags = c->flags;
3935 ei->try_start = (guint8*)(rtm->code + c->try_offset);
3936 ei->try_end = (guint8*)(rtm->code + c->try_offset + c->try_len);
3937 ei->handler_start = (guint8*)(rtm->code + c->handler_offset);
3938 ei->exvar_offset = rtm->exvar_offsets [i];
3939 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3940 ei->data.filter = (guint8*)(rtm->code + c->data.filter_offset);
3941 } else if (ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
3943 ei->data.catch_class = c->data.catch_class;
3947 save_seq_points (td);
3949 g_free (td->in_offsets);
3950 for (i = 0; i < header->code_size; ++i)
3951 g_free (td->stack_state [i]);
3952 g_free (td->stack_state);
3953 g_free (td->stack_height);
3954 g_free (td->vt_stack_size);
3955 g_free (td->data_items);
3957 g_hash_table_destroy (td->data_hash);
3958 g_free (td->clause_indexes);
3959 g_ptr_array_free (td->seq_points, TRUE);
3960 g_array_free (line_numbers, TRUE);
3961 g_ptr_array_free (td->relocs, TRUE);
3962 mono_mempool_destroy (td->mempool);
3965 static mono_mutex_t calc_section;
3968 mono_interp_transform_init (void)
3970 mono_os_mutex_init_recursive(&calc_section);
3974 mono_interp_transform_method (InterpMethod *imethod, ThreadContext *context)
3976 int i, align, size, offset;
3977 MonoMethod *method = imethod->method;
3978 MonoImage *image = method->klass->image;
3979 MonoMethodHeader *header = mono_method_get_header (method);
3980 MonoMethodSignature *signature = mono_method_signature (method);
3981 register const unsigned char *ip, *end;
3982 const MonoOpcode *opcode;
3985 unsigned char *is_bb_start;
3987 MonoVTable *method_class_vt;
3989 MonoGenericContext *generic_context = NULL;
3990 MonoDomain *domain = imethod->domain;
3992 // g_printerr ("TRANSFORM(0x%016lx): begin %s::%s\n", mono_thread_current (), method->klass->name, method->name);
3993 method_class_vt = mono_class_vtable (domain, imethod->method->klass);
3994 if (!method_class_vt->initialized) {
3997 InterpFrame *last_env_frame = context->env_frame;
3998 jmp_buf *old_env = context->current_env;
3999 error_init (&error);
4002 MonoException *failed = context->env_frame->ex;
4003 context->env_frame->ex = NULL;
4004 context->env_frame = last_env_frame;
4005 context->current_env = old_env;
4008 context->env_frame = context->current_frame;
4009 context->current_env = &env;
4010 mono_runtime_class_init_full (method_class_vt, &error);
4011 if (!mono_error_ok (&error)) {
4012 return mono_error_convert_to_exception (&error);
4014 context->env_frame = last_env_frame;
4015 context->current_env = old_env;
4018 MONO_PROFILER_RAISE (jit_begin, (method));
4020 if (mono_method_signature (method)->is_inflated)
4021 generic_context = mono_method_get_context (method);
4023 MonoGenericContainer *generic_container = mono_method_get_generic_container (method);
4024 if (generic_container)
4025 generic_context = &generic_container->context;
4028 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
4029 MonoMethod *nm = NULL;
4030 mono_os_mutex_lock(&calc_section);
4031 if (imethod->transformed) {
4032 mono_os_mutex_unlock(&calc_section);
4033 MONO_PROFILER_RAISE (jit_done, (method, imethod->jinfo));
4037 /* assumes all internal calls with an array this are built in... */
4038 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL && (! mono_method_signature (method)->hasthis || method->klass->rank == 0)) {
4039 nm = mono_marshal_get_native_wrapper (method, TRUE, FALSE);
4040 signature = mono_method_signature (nm);
4042 const char *name = method->name;
4043 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
4044 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
4045 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ("ves_icall_mono_delegate_ctor");
4047 char *wrapper_name = g_strdup_printf ("__icall_wrapper_%s", mi->name);
4048 nm = mono_marshal_get_icall_wrapper (mi->sig, wrapper_name, mi->func, TRUE);
4049 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
4050 nm = mono_marshal_get_delegate_invoke (method, NULL);
4051 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
4052 nm = mono_marshal_get_delegate_begin_invoke (method);
4053 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
4054 nm = mono_marshal_get_delegate_end_invoke (method);
4058 imethod->code = g_malloc(sizeof(short));
4059 imethod->code[0] = MINT_CALLRUN;
4063 imethod->stack_size = sizeof (stackval); /* for tracing */
4064 imethod->alloca_size = imethod->stack_size;
4065 imethod->transformed = TRUE;
4066 mono_os_mutex_unlock(&calc_section);
4067 MONO_PROFILER_RAISE (jit_done, (method, NULL));
4071 header = mono_method_get_header (nm);
4072 mono_os_mutex_unlock(&calc_section);
4073 } else if (method->klass == mono_defaults.array_class) {
4074 if (!strcmp (method->name, "UnsafeMov") || !strcmp (method->name, "UnsafeLoad")) {
4075 mono_os_mutex_lock (&calc_section);
4076 if (!imethod->transformed) {
4077 imethod->code = g_malloc (sizeof (short));
4078 imethod->code[0] = MINT_CALLRUN;
4079 imethod->stack_size = sizeof (stackval); /* for tracing */
4080 imethod->alloca_size = imethod->stack_size;
4081 imethod->transformed = TRUE;
4083 mono_os_mutex_unlock(&calc_section);
4084 MONO_PROFILER_RAISE (jit_done, (method, NULL));
4086 } else if (!strcmp (method->name, "UnsafeStore")) {
4090 g_assert ((signature->param_count + signature->hasthis) < 1000);
4091 g_assert (header->max_stack < 10000);
4092 /* intern the strings in the method. */
4094 end = ip + header->code_size;
4096 is_bb_start = g_malloc0(header->code_size);
4097 is_bb_start [0] = 1;
4104 else if (in == 0xf0) {
4106 in = *ip + MONO_CEE_MONO_ICALL;
4108 opcode = &mono_opcodes [in];
4109 switch (opcode->argument) {
4110 case MonoInlineNone:
4113 case MonoInlineString:
4114 if (method->wrapper_type == MONO_WRAPPER_NONE)
4115 mono_ldstr (domain, image, mono_metadata_token_index (read32 (ip + 1)));
4118 case MonoInlineType:
4119 if (method->wrapper_type == MONO_WRAPPER_NONE) {
4120 class = mini_get_class (method, read32 (ip + 1), generic_context);
4121 mono_class_init (class);
4122 /* quick fix to not do this for the fake ptr classes - probably should not be getting the vtable at all here */
4124 g_error ("FIXME: interface method lookup: %s (in method %s)", class->name, method->name);
4125 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE) && class->interface_offsets != NULL)
4126 mono_class_vtable (domain, class);
4131 case MonoInlineMethod:
4132 if (method->wrapper_type == MONO_WRAPPER_NONE && *ip != CEE_CALLI) {
4133 m = mono_get_method_full (image, read32 (ip + 1), NULL, generic_context);
4135 g_free (is_bb_start);
4136 g_error ("FIXME: where to get method and class string?");
4138 // return mono_get_exception_missing_method ();
4140 mono_class_init (m->klass);
4141 if (!mono_class_is_interface (m->klass))
4142 mono_class_vtable (domain, m->klass);
4146 case MonoInlineField:
4150 case MonoShortInlineR:
4153 case MonoInlineBrTarget:
4154 offset = read32 (ip + 1);
4156 backwards = offset < 0;
4157 offset += ip - header->code;
4158 g_assert (offset >= 0 && offset < header->code_size);
4159 is_bb_start [offset] |= backwards ? 2 : 1;
4161 case MonoShortInlineBrTarget:
4162 offset = ((gint8 *)ip) [1];
4164 backwards = offset < 0;
4165 offset += ip - header->code;
4166 g_assert (offset >= 0 && offset < header->code_size);
4167 is_bb_start [offset] |= backwards ? 2 : 1;
4172 case MonoShortInlineVar:
4173 case MonoShortInlineI:
4176 case MonoInlineSwitch: {
4178 const unsigned char *next_ip;
4182 next_ip = ip + 4 * n;
4183 for (i = 0; i < n; i++) {
4184 offset = read32 (ip);
4185 backwards = offset < 0;
4186 offset += next_ip - header->code;
4187 g_assert (offset >= 0 && offset < header->code_size);
4188 is_bb_start [offset] |= backwards ? 2 : 1;
4198 g_assert_not_reached ();
4201 // g_printerr ("TRANSFORM(0x%016lx): end %s::%s\n", mono_thread_current (), method->klass->name, method->name);
4203 /* the rest needs to be locked so it is only done once */
4204 mono_os_mutex_lock(&calc_section);
4205 if (imethod->transformed) {
4206 mono_os_mutex_unlock(&calc_section);
4207 g_free (is_bb_start);
4208 MONO_PROFILER_RAISE (jit_done, (method, imethod->jinfo));
4212 imethod->local_offsets = g_malloc (header->num_locals * sizeof(guint32));
4213 imethod->stack_size = (sizeof (stackval)) * (header->max_stack + 2); /* + 1 for returns of called functions + 1 for 0-ing in trace*/
4214 imethod->stack_size = (imethod->stack_size + 7) & ~7;
4216 for (i = 0; i < header->num_locals; ++i) {
4217 size = mono_type_size (header->locals [i], &align);
4218 offset += align - 1;
4219 offset &= ~(align - 1);
4220 imethod->local_offsets [i] = offset;
4223 offset = (offset + 7) & ~7;
4225 imethod->exvar_offsets = g_malloc (header->num_clauses * sizeof (guint32));
4226 for (i = 0; i < header->num_clauses; i++) {
4227 offset += sizeof (MonoObject*);
4228 imethod->exvar_offsets [i] = offset;
4230 offset = (offset + 7) & ~7;
4232 imethod->locals_size = offset;
4233 g_assert (imethod->locals_size < 65536);
4235 imethod->arg_offsets = g_malloc ((!!signature->hasthis + signature->param_count) * sizeof(guint32));
4237 if (signature->hasthis) {
4238 g_assert (!signature->pinvoke);
4239 size = mono_type_stack_size (&method->klass->byval_arg, &align);
4240 offset += align - 1;
4241 offset &= ~(align - 1);
4242 imethod->arg_offsets [0] = offset;
4246 for (i = 0; i < signature->param_count; ++i) {
4247 if (signature->pinvoke) {
4249 size = mono_type_native_stack_size (signature->params [i], &dummy);
4253 size = mono_type_stack_size (signature->params [i], &align);
4254 offset += align - 1;
4255 offset &= ~(align - 1);
4256 imethod->arg_offsets [i + !!signature->hasthis] = offset;
4259 offset = (offset + 7) & ~7;
4260 imethod->args_size = offset;
4261 g_assert (imethod->args_size < 10000);
4263 generate (method, imethod, is_bb_start, generic_context);
4265 g_free (is_bb_start);
4267 // FIXME: Add a different callback ?
4268 MONO_PROFILER_RAISE (jit_done, (method, imethod->jinfo));
4269 imethod->transformed = TRUE;
4270 mono_os_mutex_unlock(&calc_section);