2 * transform CIL into different opcodes for more
3 * efficient interpretation
5 * Written by Bernie Solomon (bernard@ugsolutions.com)
10 #include <mono/metadata/appdomain.h>
11 #include <mono/metadata/debug-helpers.h>
12 #include <mono/metadata/exception.h>
13 #include <mono/metadata/mono-endian.h>
14 #include <mono/metadata/marshal.h>
15 #include <mono/metadata/profiler-private.h>
16 #include <mono/metadata/tabledefs.h>
18 #include <mono/mini/mini.h>
21 #include "interp-internals.h"
24 // TODO: export from marshal.c
25 MonoDelegate* mono_ftnptr_to_delegate (MonoClass *klass, gpointer ftn);
39 MonoMethodHeader *header;
41 const unsigned char *il_code;
42 const unsigned char *ip;
43 const unsigned char *last_ip;
44 const unsigned char *in_start;
48 StackInfo **stack_state;
51 unsigned char *is_bb_start;
52 unsigned short *new_code;
53 unsigned short *new_code_end;
54 unsigned short *new_ip;
55 unsigned short *last_new_ip;
56 unsigned int max_code_size;
59 unsigned int max_stack_height;
61 unsigned int max_vt_sp;
65 GHashTable *data_hash;
68 #define MINT_TYPE_I1 0
69 #define MINT_TYPE_U1 1
70 #define MINT_TYPE_I2 2
71 #define MINT_TYPE_U2 3
72 #define MINT_TYPE_I4 4
73 #define MINT_TYPE_I8 5
74 #define MINT_TYPE_R4 6
75 #define MINT_TYPE_R8 7
78 #define MINT_TYPE_VT 10
80 #define STACK_TYPE_I4 0
81 #define STACK_TYPE_I8 1
82 #define STACK_TYPE_R8 2
83 #define STACK_TYPE_O 3
84 #define STACK_TYPE_VT 4
85 #define STACK_TYPE_MP 5
86 #define STACK_TYPE_F 6
88 static const char *stack_type_string [] = { "I4", "I8", "R8", "O ", "VT", "MP", "F " };
90 #if SIZEOF_VOID_P == 8
91 #define STACK_TYPE_I STACK_TYPE_I8
93 #define STACK_TYPE_I STACK_TYPE_I4
96 static int stack_type [] = {
100 STACK_TYPE_I4, /*U2*/
101 STACK_TYPE_I4, /*I4*/
102 STACK_TYPE_I8, /*I8*/
103 STACK_TYPE_R8, /*R4*/
104 STACK_TYPE_R8, /*R8*/
111 grow_code (TransformData *td)
113 unsigned int old_ip_offset = td->new_ip - td->new_code;
114 unsigned int old_last_ip_offset = td->last_new_ip - td->new_code;
115 g_assert (old_ip_offset <= td->max_code_size);
116 td->new_code = g_realloc (td->new_code, (td->max_code_size *= 2) * sizeof (td->new_code [0]));
117 td->new_code_end = td->new_code + td->max_code_size;
118 td->new_ip = td->new_code + old_ip_offset;
119 td->last_new_ip = td->new_code + old_last_ip_offset;
122 #define ENSURE_CODE(td, n) \
124 if ((td)->new_ip + (n) > (td)->new_code_end) \
128 #define ADD_CODE(td, n) \
130 if ((td)->new_ip == (td)->new_code_end) \
132 *(td)->new_ip++ = (n); \
135 #define CHECK_STACK(td, n) \
137 int stack_size = (td)->sp - (td)->stack; \
138 if (stack_size < (n)) \
139 g_warning ("%s.%s: not enough values (%d < %d) on stack at %04x", \
140 (td)->method->klass->name, (td)->method->name, \
141 stack_size, n, (td)->ip - (td)->il_code); \
144 #define ENSURE_I4(td, sp_off) \
146 if ((td)->sp [-sp_off].type == STACK_TYPE_I8) \
147 ADD_CODE(td, sp_off == 1 ? MINT_CONV_I4_I8 : MINT_CONV_I4_I8_SP); \
151 handle_branch(TransformData *td, int short_op, int long_op, int offset)
153 int shorten_branch = 0;
154 int target = td->ip + offset - td->il_code;
155 if (target < 0 || target >= td->code_size)
156 g_assert_not_reached ();
157 if (offset > 0 && td->stack_height [target] < 0) {
158 td->stack_height [target] = td->sp - td->stack;
159 if (td->stack_height [target] > 0)
160 td->stack_state [target] = g_memdup (td->stack, td->stack_height [target] * sizeof (td->stack [0]));
161 td->vt_stack_size [target] = td->vt_sp;
164 offset = td->in_offsets [target] - (td->new_ip - td->new_code);
165 if (offset >= -32768) {
169 int prev = td->forward_refs [target];
170 td->forward_refs [td->ip - td->il_code] = prev;
171 td->forward_refs [target] = td->ip - td->il_code;
173 if (td->header->code_size <= 25000) /* FIX to be precise somehow? */
176 if (shorten_branch) {
177 ADD_CODE(td, short_op);
178 ADD_CODE(td, offset);
180 ADD_CODE(td, long_op);
181 ADD_CODE(td, * (unsigned short *)(&offset));
182 ADD_CODE(td, * ((unsigned short *)&offset + 1));
187 one_arg_branch(TransformData *td, int mint_op, int offset)
189 int type = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
190 int long_op = mint_op + type - STACK_TYPE_I4;
191 int short_op = long_op + MINT_BRFALSE_I4_S - MINT_BRFALSE_I4;
194 handle_branch (td, short_op, long_op, offset);
198 two_arg_branch(TransformData *td, int mint_op, int offset)
200 int type1 = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
201 int type2 = td->sp [-2].type == STACK_TYPE_O || td->sp [-2].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-2].type;
202 int long_op = mint_op + type1 - STACK_TYPE_I4;
203 int short_op = long_op + MINT_BEQ_I4_S - MINT_BEQ_I4;
205 if (type1 == STACK_TYPE_I4 && type2 == STACK_TYPE_I8) {
206 ADD_CODE(td, MINT_CONV_I8_I4);
207 td->in_offsets [td->ip - td->il_code]++;
208 } else if (type1 == STACK_TYPE_I8 && type2 == STACK_TYPE_I4) {
209 ADD_CODE(td, MINT_CONV_I8_I4_SP);
210 td->in_offsets [td->ip - td->il_code]++;
211 } else if (type1 != type2) {
212 g_warning("%s.%s: branch type mismatch %d %d",
213 td->method->klass->name, td->method->name,
214 td->sp [-1].type, td->sp [-2].type);
217 handle_branch (td, short_op, long_op, offset);
221 unary_arith_op(TransformData *td, int mint_op)
223 int op = mint_op + td->sp [-1].type - STACK_TYPE_I4;
229 binary_arith_op(TransformData *td, int mint_op)
231 int type1 = td->sp [-2].type;
232 int type2 = td->sp [-1].type;
234 #if SIZEOF_VOID_P == 8
235 if ((type1 == STACK_TYPE_MP || type1 == STACK_TYPE_I8) && type2 == STACK_TYPE_I4) {
236 ADD_CODE(td, MINT_CONV_I8_I4);
237 type2 = STACK_TYPE_I8;
239 if (type1 == STACK_TYPE_I4 && (type2 == STACK_TYPE_MP || type2 == STACK_TYPE_I8)) {
240 ADD_CODE(td, MINT_CONV_I8_I4_SP);
241 type1 = STACK_TYPE_I8;
242 td->sp [-2].type = STACK_TYPE_I8;
245 if (type1 == STACK_TYPE_MP)
246 type1 = STACK_TYPE_I;
247 if (type2 == STACK_TYPE_MP)
248 type2 = STACK_TYPE_I;
249 if (type1 != type2) {
250 g_warning("%s.%s: %04x arith type mismatch %s %d %d",
251 td->method->klass->name, td->method->name,
252 td->ip - td->il_code, mono_interp_opname[mint_op], type1, type2);
254 op = mint_op + type1 - STACK_TYPE_I4;
261 binary_int_op(TransformData *td, int mint_op)
263 int op = mint_op + td->sp [-1].type - STACK_TYPE_I4;
265 if (td->sp [-1].type != td->sp [-2].type)
266 g_warning("%s.%s: int type mismatch", td->method->klass->name, td->method->name);
272 shift_op(TransformData *td, int mint_op)
274 int op = mint_op + td->sp [-2].type - STACK_TYPE_I4;
276 if (td->sp [-1].type != STACK_TYPE_I4) {
277 g_warning("%s.%s: shift type mismatch %d",
278 td->method->klass->name, td->method->name,
286 mint_type(MonoType *type)
291 switch (type->type) {
295 case MONO_TYPE_BOOLEAN:
307 #if SIZEOF_VOID_P == 4
321 case MONO_TYPE_STRING:
322 case MONO_TYPE_SZARRAY:
323 case MONO_TYPE_CLASS:
324 case MONO_TYPE_OBJECT:
325 case MONO_TYPE_ARRAY:
327 case MONO_TYPE_VALUETYPE:
328 if (type->data.klass->enumtype) {
329 type = mono_class_enum_basetype (type->data.klass);
333 case MONO_TYPE_GENERICINST:
334 type = &type->data.generic_class->container_class->byval_arg;
337 g_warning ("got type 0x%02x", type->type);
338 g_assert_not_reached ();
344 can_store (int stack_type, int var_type)
346 if (stack_type == STACK_TYPE_O || stack_type == STACK_TYPE_MP)
347 stack_type = STACK_TYPE_I;
348 if (var_type == STACK_TYPE_O || var_type == STACK_TYPE_MP)
349 var_type = STACK_TYPE_I;
350 return stack_type == var_type;
353 #define SET_SIMPLE_TYPE(s, ty) \
360 #define SET_TYPE(s, ty, k) \
367 #define PUSH_SIMPLE_TYPE(td, ty) \
371 sp_height = (td)->sp - (td)->stack; \
372 if (sp_height > (td)->max_stack_height) \
373 (td)->max_stack_height = sp_height; \
374 SET_SIMPLE_TYPE((td)->sp - 1, ty); \
377 #define PUSH_TYPE(td, ty, k) \
381 sp_height = (td)->sp - (td)->stack; \
382 if (sp_height > (td)->max_stack_height) \
383 (td)->max_stack_height = sp_height; \
384 SET_TYPE((td)->sp - 1, ty, k); \
387 #define PUSH_VT(td, size) \
389 (td)->vt_sp += ((size) + 7) & ~7; \
390 if ((td)->vt_sp > (td)->max_vt_sp) \
391 (td)->max_vt_sp = (td)->vt_sp; \
394 #define POP_VT(td, size) \
396 (td)->vt_sp -= ((size) + 7) & ~7; \
399 #if NO_UNALIGNED_ACCESS
400 #define WRITE32(td, v) \
402 ENSURE_CODE(td, 2); \
403 * (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
404 * ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
408 #define WRITE64(td, v) \
410 ENSURE_CODE(td, 4); \
411 * (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
412 * ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
413 * ((guint16 *)((td)->new_ip) + 2) = * ((guint16 *)(v) + 2); \
414 * ((guint16 *)((td)->new_ip) + 3) = * ((guint16 *)(v) + 3); \
418 #define WRITE32(td, v) \
420 ENSURE_CODE(td, 2); \
421 * (guint32 *)((td)->new_ip) = * (guint32 *)(v); \
425 #define WRITE64(td, v) \
427 ENSURE_CODE(td, 4); \
428 * (guint64 *)((td)->new_ip) = * (guint64 *)(v); \
435 load_arg(TransformData *td, int n)
438 MonoClass *klass = NULL;
441 gboolean hasthis = mono_method_signature (td->method)->hasthis;
442 if (hasthis && n == 0)
443 type = &td->method->klass->byval_arg;
445 type = mono_method_signature (td->method)->params [hasthis ? n - 1 : n];
447 mt = mint_type (type);
448 if (mt == MINT_TYPE_VT) {
450 klass = mono_class_from_mono_type (type);
451 if (mono_method_signature (td->method)->pinvoke)
452 size = mono_class_native_size (klass, NULL);
454 size = mono_class_value_size (klass, NULL);
456 if (hasthis && n == 0) {
458 ADD_CODE (td, MINT_LDARG_P);
459 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
463 ADD_CODE (td, MINT_LDARG_VT);
464 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
468 if (hasthis && n == 0) {
470 ADD_CODE (td, MINT_LDARG_P);
471 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
474 ADD_CODE(td, MINT_LDARG_I1 + (mt - MINT_TYPE_I1));
475 ADD_CODE(td, td->rtm->arg_offsets [n]); /* FIX for large offset */
476 if (mt == MINT_TYPE_O)
477 klass = mono_class_from_mono_type (type);
480 PUSH_TYPE(td, stack_type[mt], klass);
484 store_arg(TransformData *td, int n)
490 gboolean hasthis = mono_method_signature (td->method)->hasthis;
491 if (hasthis && n == 0)
492 type = &td->method->klass->byval_arg;
494 type = mono_method_signature (td->method)->params [n - !!hasthis];
496 mt = mint_type (type);
497 if (mt == MINT_TYPE_VT) {
499 g_error ("data.klass");
500 if (mono_method_signature (td->method)->pinvoke)
501 size = mono_class_native_size (type->data.klass, NULL);
503 size = mono_class_value_size (type->data.klass, NULL);
504 ADD_CODE(td, MINT_STARG_VT);
507 if (td->sp [-1].type == STACK_TYPE_VT)
510 ADD_CODE(td, MINT_STARG_I1 + (mt - MINT_TYPE_I1));
511 ADD_CODE(td, td->rtm->arg_offsets [n]);
517 store_inarg(TransformData *td, int n)
520 gboolean hasthis = mono_method_signature (td->method)->hasthis;
521 if (hasthis && n == 0)
522 type = &td->method->klass->byval_arg;
524 type = mono_method_signature (td->method)->params [n - !!hasthis];
526 int mt = mint_type (type);
527 if (hasthis && n == 0) {
528 ADD_CODE (td, MINT_STINARG_P);
532 if (mt == MINT_TYPE_VT) {
533 MonoClass *klass = mono_class_from_mono_type (type);
535 if (mono_method_signature (td->method)->pinvoke)
536 size = mono_class_native_size (klass, NULL);
538 size = mono_class_value_size (klass, NULL);
539 ADD_CODE(td, MINT_STINARG_VT);
543 ADD_CODE(td, MINT_STINARG_I1 + (mt - MINT_TYPE_I1));
549 load_local(TransformData *td, int n)
551 MonoType *type = td->header->locals [n];
552 int mt = mint_type (type);
553 int offset = td->rtm->local_offsets [n];
554 MonoClass *klass = NULL;
555 if (mt == MINT_TYPE_VT) {
556 klass = mono_class_from_mono_type (type);
557 gint32 size = mono_class_value_size (klass, NULL);
559 ADD_CODE(td, MINT_LDLOC_VT);
560 ADD_CODE(td, offset); /*FIX for large offset */
563 g_assert (mt < MINT_TYPE_VT);
564 if (mt == MINT_TYPE_I4 && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
565 td->last_new_ip [0] == MINT_STLOC_I4 && td->last_new_ip [1] == offset) {
566 td->last_new_ip [0] = MINT_STLOC_NP_I4;
567 } else if (mt == MINT_TYPE_O && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
568 td->last_new_ip [0] == MINT_STLOC_O && td->last_new_ip [1] == offset) {
569 td->last_new_ip [0] = MINT_STLOC_NP_O;
571 ADD_CODE(td, MINT_LDLOC_I1 + (mt - MINT_TYPE_I1));
572 ADD_CODE(td, offset); /*FIX for large offset */
574 if (mt == MINT_TYPE_O)
575 klass = mono_class_from_mono_type (type);
577 PUSH_TYPE(td, stack_type[mt], klass);
581 store_local(TransformData *td, int n)
583 MonoType *type = td->header->locals [n];
584 int mt = mint_type (type);
585 int offset = td->rtm->local_offsets [n];
587 #if SIZEOF_VOID_P == 8
588 if (td->sp [-1].type == STACK_TYPE_I4 && stack_type [mt] == STACK_TYPE_I8) {
589 ADD_CODE(td, MINT_CONV_I8_I4);
590 td->sp [-1].type = STACK_TYPE_I8;
593 if (!can_store(td->sp [-1].type, stack_type [mt])) {
594 g_warning("%s.%s: Store local stack type mismatch %d %d",
595 td->method->klass->name, td->method->name,
596 stack_type [mt], td->sp [-1].type);
598 if (mt == MINT_TYPE_VT) {
599 MonoClass *klass = mono_class_from_mono_type (type);
600 gint32 size = mono_class_value_size (klass, NULL);
601 ADD_CODE(td, MINT_STLOC_VT);
602 ADD_CODE(td, offset); /*FIX for large offset */
604 if (td->sp [-1].type == STACK_TYPE_VT)
607 g_assert (mt < MINT_TYPE_VT);
608 ADD_CODE(td, MINT_STLOC_I1 + (mt - MINT_TYPE_I1));
609 ADD_CODE(td, offset); /*FIX for large offset */
614 #define SIMPLE_OP(td, op) \
621 get_data_item_index (TransformData *td, void *ptr)
623 gpointer p = g_hash_table_lookup (td->data_hash, ptr);
626 return GPOINTER_TO_UINT (p) - 1;
627 if (td->max_data_items == td->n_data_items) {
628 td->max_data_items = td->n_data_items == 0 ? 16 : 2 * td->max_data_items;
629 td->data_items = g_realloc (td->data_items, td->max_data_items * sizeof(td->data_items [0]));
631 index = td->n_data_items;
632 td->data_items [index] = ptr;
634 g_hash_table_insert (td->data_hash, ptr, GUINT_TO_POINTER (index + 1));
639 interp_transform_call (TransformData *td, MonoMethod *method, MonoMethod *target_method, MonoDomain *domain, MonoGenericContext *generic_context, unsigned char *is_bb_start, int body_start_offset, MonoClass *constrained_class)
641 MonoImage *image = method->klass->image;
642 MonoMethodSignature *csignature;
644 int virtual = *td->ip == CEE_CALLVIRT;
645 int calli = *td->ip == CEE_CALLI || *td->ip == CEE_MONO_CALLI_EXTRA_ARG;
647 guint32 vt_stack_used = 0;
648 guint32 vt_res_size = 0;
653 guint32 token = read32 (td->ip + 1);
655 if (target_method == NULL) {
658 native = (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE && td->sp [-1].type == STACK_TYPE_I);
660 if (method->wrapper_type != MONO_WRAPPER_NONE)
661 csignature = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
663 csignature = mono_metadata_parse_signature (image, token);
665 if (generic_context) {
666 csignature = mono_inflate_generic_signature (csignature, generic_context, &error);
667 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
670 target_method = NULL;
672 if (method->wrapper_type == MONO_WRAPPER_NONE)
673 target_method = mono_get_method_full (image, token, NULL, generic_context);
675 target_method = (MonoMethod *)mono_method_get_wrapper_data (method, token);
676 csignature = mono_method_signature (target_method);
677 if (target_method->klass == mono_defaults.string_class) {
678 if (target_method->name [0] == 'g') {
679 if (strcmp (target_method->name, "get_Chars") == 0)
681 else if (strcmp (target_method->name, "get_Length") == 0)
684 } else if (target_method->klass == mono_defaults.array_class) {
685 if (strcmp (target_method->name, "get_Rank") == 0)
686 op = MINT_ARRAY_RANK;
687 else if (strcmp (target_method->name, "get_Length") == 0)
692 csignature = mono_method_signature (target_method);
695 /* TODO: that's oddly specific? */
696 if (generic_context && target_method && !strcmp ("Invoke", target_method->name) && target_method->klass->parent == mono_defaults.multicastdelegate_class) {
697 csignature = mono_inflate_generic_signature (csignature, generic_context, &error);
698 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
701 if (constrained_class) {
702 if (constrained_class->enumtype && !strcmp (target_method->name, "GetHashCode")) {
703 /* Use the corresponding method from the base type to avoid boxing */
704 MonoType *base_type = mono_class_enum_basetype (constrained_class);
705 g_assert (base_type);
706 constrained_class = mono_class_from_mono_type (base_type);
707 target_method = mono_class_get_method_from_name (constrained_class, target_method->name, 0);
708 g_assert (target_method);
712 if (constrained_class) {
713 mono_class_setup_vtable (constrained_class);
715 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);
717 target_method = mono_get_method_constrained_with_method (image, target_method, constrained_class, generic_context, &error);
719 g_print (" : %s::%s. %s (%p)\n", target_method->klass->name, target_method->name, mono_signature_full_name (target_method->signature), target_method);
721 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
722 mono_class_setup_vtable (target_method->klass);
724 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)) {
725 if (target_method->klass == mono_defaults.enum_class && (td->sp - csignature->param_count - 1)->type == STACK_TYPE_MP) {
726 /* managed pointer on the stack, we need to deref that puppy */
727 ADD_CODE (td, MINT_LDIND_I);
728 ADD_CODE (td, csignature->param_count);
730 ADD_CODE (td, MINT_BOX);
731 ADD_CODE (td, get_data_item_index (td, constrained_class));
732 ADD_CODE (td, csignature->param_count);
733 } else if (!constrained_class->valuetype) {
734 /* managed pointer on the stack, we need to deref that puppy */
735 ADD_CODE (td, MINT_LDIND_I);
736 ADD_CODE (td, csignature->param_count);
738 g_assert (target_method->klass->valuetype);
744 mono_class_init (target_method->klass);
746 CHECK_STACK (td, csignature->param_count + csignature->hasthis);
747 if (!calli && (!virtual || (target_method->flags & METHOD_ATTRIBUTE_VIRTUAL) == 0) &&
748 (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
749 (target_method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) == 0) {
750 int called_inited = mono_class_vtable (domain, target_method->klass)->initialized;
751 MonoMethodHeader *mheader = mono_method_get_header (target_method);
753 if (/*mono_metadata_signature_equal (method->signature, target_method->signature) */ method == target_method && *(td->ip + 5) == CEE_RET) {
755 if (mono_interp_traceopt)
756 g_print ("Optimize tail call of %s.%s\n", target_method->klass->name, target_method->name);
757 for (i = csignature->param_count - 1; i >= 0; --i)
758 store_arg (td, i + csignature->hasthis);
760 if (csignature->hasthis) {
761 g_error ("STTHIS removal");
762 // ADD_CODE(td, MINT_STTHIS);
765 ADD_CODE(td, MINT_BR_S);
766 offset = body_start_offset - ((td->new_ip - 1) - td->new_code);
767 ADD_CODE(td, offset);
768 if (!is_bb_start [td->ip + 5 - td->il_code])
769 ++td->ip; /* gobble the CEE_RET if it isn't branched to */
773 /* mheader might not exist if this is a delegate invoc, etc */
774 if (mheader && *mheader->code == CEE_RET && called_inited) {
775 if (mono_interp_traceopt)
776 g_print ("Inline (empty) call of %s.%s\n", target_method->klass->name, target_method->name);
777 for (i = 0; i < csignature->param_count; i++)
778 ADD_CODE (td, MINT_POP); /*FIX: vt */
780 if (csignature->hasthis) {
782 ADD_CODE(td, MINT_CKNULL);
783 ADD_CODE (td, MINT_POP);
786 td->sp -= csignature->param_count + csignature->hasthis;
792 if (method->wrapper_type == MONO_WRAPPER_NONE && target_method != NULL) {
793 if (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
794 target_method = mono_marshal_get_native_wrapper (target_method, FALSE, FALSE);
795 if (!virtual && target_method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
796 target_method = mono_marshal_get_synchronized_wrapper (target_method);
798 g_assert (csignature->call_convention == MONO_CALL_DEFAULT || csignature->call_convention == MONO_CALL_C);
799 td->sp -= csignature->param_count + !!csignature->hasthis;
800 for (i = 0; i < csignature->param_count; ++i) {
801 if (td->sp [i + !!csignature->hasthis].type == STACK_TYPE_VT) {
803 MonoClass *klass = mono_class_from_mono_type (csignature->params [i]);
804 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
805 size = mono_class_native_size (klass, NULL);
807 size = mono_class_value_size (klass, NULL);
808 size = (size + 7) & ~7;
809 vt_stack_used += size;
813 /* need to handle typedbyref ... */
814 if (csignature->ret->type != MONO_TYPE_VOID) {
815 int mt = mint_type(csignature->ret);
816 MonoClass *klass = mono_class_from_mono_type (csignature->ret);
817 if (mt == MINT_TYPE_VT) {
818 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
819 vt_res_size = mono_class_native_size (klass, NULL);
821 vt_res_size = mono_class_value_size (klass, NULL);
822 PUSH_VT(td, vt_res_size);
824 PUSH_TYPE(td, stack_type[mt], klass);
830 #if SIZEOF_VOID_P == 8
831 if (op == MINT_LDLEN)
832 ADD_CODE(td, MINT_CONV_I4_I8);
836 ADD_CODE(td, native ? MINT_CALLI_NAT : MINT_CALLI);
838 ADD_CODE(td, is_void ? MINT_VCALLVIRT : MINT_CALLVIRT);
840 ADD_CODE(td, is_void ? MINT_VCALL : MINT_CALL);
843 ADD_CODE(td, get_data_item_index (td, (void *)csignature));
845 ADD_CODE(td, get_data_item_index (td, (void *)mono_interp_get_runtime_method (domain, target_method, &error)));
846 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
850 if (vt_stack_used != 0 || vt_res_size != 0) {
851 ADD_CODE(td, MINT_VTRESULT);
852 ADD_CODE(td, vt_res_size);
853 WRITE32(td, &vt_stack_used);
854 td->vt_sp -= vt_stack_used;
859 generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, MonoGenericContext *generic_context)
861 MonoMethodHeader *header = mono_method_get_header (method);
862 MonoMethodSignature *signature = mono_method_signature (method);
863 MonoImage *image = method->klass->image;
864 MonoDomain *domain = mono_domain_get ();
865 MonoClass *constrained_class = NULL;
871 MonoClassField *field;
872 const unsigned char *end;
873 int new_in_start_offset;
874 int body_start_offset;
878 int generating_code = 1;
880 memset(&td, 0, sizeof(td));
883 td.is_bb_start = is_bb_start;
884 td.il_code = header->code;
885 td.code_size = header->code_size;
887 td.max_code_size = td.code_size;
888 td.new_code = (unsigned short *)g_malloc(td.max_code_size * sizeof(gushort));
889 td.new_code_end = td.new_code + td.max_code_size;
890 td.in_offsets = g_malloc0(header->code_size * sizeof(int));
891 td.forward_refs = g_malloc(header->code_size * sizeof(int));
892 td.stack_state = g_malloc0(header->code_size * sizeof(StackInfo *));
893 td.stack_height = g_malloc(header->code_size * sizeof(int));
894 td.vt_stack_size = g_malloc(header->code_size * sizeof(int));
896 td.max_data_items = 0;
897 td.data_items = NULL;
898 td.data_hash = g_hash_table_new (NULL, NULL);
899 rtm->data_items = td.data_items;
900 for (i = 0; i < header->code_size; i++) {
901 td.forward_refs [i] = -1;
902 td.stack_height [i] = -1;
904 td.new_ip = td.new_code;
905 td.last_new_ip = NULL;
907 td.stack = g_malloc0(header->max_stack * sizeof(td.stack[0]));
909 td.max_stack_height = 0;
911 for (i = 0; i < header->num_clauses; i++) {
912 MonoExceptionClause *c = header->clauses + i;
913 td.stack_height [c->handler_offset] = 0;
914 td.vt_stack_size [c->handler_offset] = 0;
915 td.is_bb_start [c->handler_offset] = 1;
916 if (c->flags == MONO_EXCEPTION_CLAUSE_NONE) {
917 td.stack_height [c->handler_offset] = 1;
918 td.stack_state [c->handler_offset] = g_malloc0(sizeof(StackInfo));
919 td.stack_state [c->handler_offset][0].type = STACK_TYPE_O;
920 td.stack_state [c->handler_offset][0].klass = NULL; /*FIX*/
924 td.ip = header->code;
925 end = td.ip + header->code_size;
927 if (mono_interp_traceopt) {
928 char *tmp = mono_disasm_code (NULL, method, td.ip, end);
929 char *name = mono_method_full_name (method, TRUE);
930 g_print ("Method %s, original code:\n", name);
931 g_print ("%s\n", tmp);
936 if (signature->hasthis)
937 store_inarg (&td, 0);
938 for (i = 0; i < signature->param_count; i++)
939 store_inarg (&td, i + !!signature->hasthis);
941 body_start_offset = td.new_ip - td.new_code;
943 for (i = 0; i < header->num_locals; i++) {
944 int mt = mint_type(header->locals [i]);
945 if (mt == MINT_TYPE_VT || mt == MINT_TYPE_O) {
946 ADD_CODE(&td, MINT_INITLOCALS);
951 while (td.ip < end) {
954 g_assert (td.sp >= td.stack);
955 g_assert (td.vt_sp < 0x10000000);
956 in_offset = td.ip - header->code;
957 td.in_offsets [in_offset] = td.new_ip - td.new_code;
958 new_in_start_offset = td.new_ip - td.new_code;
960 while (td.forward_refs [in_offset] >= 0) {
961 int j = td.forward_refs [in_offset];
963 td.forward_refs [in_offset] = td.forward_refs [j];
964 if (td.in_offsets [j] < 0) {
965 int old_switch_offset = -td.in_offsets [j];
966 int new_switch_offset = td.in_offsets [old_switch_offset];
967 int switch_case = (j - old_switch_offset - 5) / 4;
968 int n_cases = read32 (header->code + old_switch_offset + 1);
969 offset = (td.new_ip - td.new_code) - (new_switch_offset + 2 * n_cases + 3);
970 slot = new_switch_offset + 3 + 2 * switch_case;
971 td.new_code [slot] = * (unsigned short *)(&offset);
972 td.new_code [slot + 1] = * ((unsigned short *)&offset + 1);
974 int op = td.new_code [td.in_offsets [j]];
975 if (mono_interp_opargtype [op] == MintOpShortBranch) {
976 offset = (td.new_ip - td.new_code) - td.in_offsets [j];
977 g_assert (offset <= 32767);
978 slot = td.in_offsets [j] + 1;
979 td.new_code [slot] = offset;
981 offset = (td.new_ip - td.new_code) - td.in_offsets [j];
982 slot = td.in_offsets [j] + 1;
983 td.new_code [slot] = * (unsigned short *)(&offset);
984 td.new_code [slot + 1] = * ((unsigned short *)&offset + 1);
988 if (td.stack_height [in_offset] >= 0) {
989 g_assert (is_bb_start [in_offset]);
990 if (td.stack_height [in_offset] > 0)
991 memcpy (td.stack, td.stack_state [in_offset], td.stack_height [in_offset] * sizeof(td.stack [0]));
992 td.sp = td.stack + td.stack_height [in_offset];
993 td.vt_sp = td.vt_stack_size [in_offset];
995 if (is_bb_start [in_offset]) {
998 if (!generating_code) {
999 while (td.ip < end && !is_bb_start [td.ip - td.il_code])
1003 if (mono_interp_traceopt > 1) {
1004 printf("IL_%04lx %s %-10s -> IL_%04lx, sp %ld, %s %-12s vt_sp %u (max %u)\n",
1006 td.is_bb_start [td.ip - td.il_code] == 3 ? "<>" :
1007 td.is_bb_start [td.ip - td.il_code] == 2 ? "< " :
1008 td.is_bb_start [td.ip - td.il_code] == 1 ? " >" : " ",
1009 mono_opcode_name (*td.ip), td.new_ip - td.new_code, td.sp - td.stack,
1010 td.sp > td.stack ? stack_type_string [td.sp [-1].type] : " ",
1011 (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) : "",
1012 td.vt_sp, td.max_vt_sp);
1020 SIMPLE_OP(td, MINT_BREAK);
1026 load_arg (&td, *td.ip - CEE_LDARG_0);
1033 load_local (&td, *td.ip - CEE_LDLOC_0);
1040 store_local (&td, *td.ip - CEE_STLOC_0);
1044 load_arg (&td, ((guint8 *)td.ip)[1]);
1047 case CEE_LDARGA_S: {
1048 /* NOTE: n includes this */
1049 int n = ((guint8 *)td.ip)[1];
1050 if (n == 0 && signature->hasthis) {
1051 g_error ("LDTHISA: NOPE");
1052 ADD_CODE(&td, MINT_LDTHISA);
1055 ADD_CODE(&td, MINT_LDARGA);
1056 ADD_CODE(&td, td.rtm->arg_offsets [n]);
1058 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
1063 store_arg (&td, ((guint8 *)td.ip)[1]);
1067 load_local (&td, ((guint8 *)td.ip)[1]);
1071 ADD_CODE(&td, MINT_LDLOCA_S);
1072 ADD_CODE(&td, td.rtm->local_offsets [((guint8 *)td.ip)[1]]);
1073 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
1077 store_local (&td, ((guint8 *)td.ip)[1]);
1081 SIMPLE_OP(td, MINT_LDNULL);
1082 PUSH_TYPE(&td, STACK_TYPE_O, NULL);
1085 SIMPLE_OP(td, MINT_LDC_I4_M1);
1086 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1089 if (!td.is_bb_start[td.ip + 1 - td.il_code] && td.ip [1] == 0xfe && td.ip [2] == CEE_CEQ &&
1090 td.sp > td.stack && td.sp [-1].type == STACK_TYPE_I4) {
1091 SIMPLE_OP(td, MINT_CEQ0_I4);
1094 SIMPLE_OP(td, MINT_LDC_I4_0);
1095 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1099 if (!td.is_bb_start[td.ip + 1 - td.il_code] &&
1100 (td.ip [1] == CEE_ADD || td.ip [1] == CEE_SUB) && td.sp [-1].type == STACK_TYPE_I4) {
1101 ADD_CODE(&td, td.ip [1] == CEE_ADD ? MINT_ADD1_I4 : MINT_SUB1_I4);
1104 SIMPLE_OP(td, MINT_LDC_I4_1);
1105 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1115 SIMPLE_OP(td, (*td.ip - CEE_LDC_I4_0) + MINT_LDC_I4_0);
1116 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1119 ADD_CODE(&td, MINT_LDC_I4_S);
1120 ADD_CODE(&td, ((gint8 *) td.ip) [1]);
1122 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1125 i32 = read32 (td.ip + 1);
1126 ADD_CODE(&td, MINT_LDC_I4);
1129 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1132 gint64 val = read64 (td.ip + 1);
1133 ADD_CODE(&td, MINT_LDC_I8);
1136 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I8);
1141 readr4 (td.ip + 1, &val);
1142 ADD_CODE(&td, MINT_LDC_R4);
1145 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_R8);
1150 readr8 (td.ip + 1, &val);
1151 ADD_CODE(&td, MINT_LDC_R8);
1154 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_R8);
1158 int type = td.sp [-1].type;
1159 MonoClass *klass = td.sp [-1].klass;
1160 if (td.sp [-1].type == STACK_TYPE_VT) {
1161 gint32 size = mono_class_value_size (klass, NULL);
1163 ADD_CODE(&td, MINT_DUP_VT);
1164 WRITE32(&td, &size);
1167 SIMPLE_OP(td, MINT_DUP);
1168 PUSH_TYPE(&td, type, klass);
1172 CHECK_STACK(&td, 1);
1173 SIMPLE_OP(td, MINT_POP);
1175 if (td.sp [-1].type == STACK_TYPE_VT) {
1176 int size = mono_class_value_size (td.sp [-1].klass, NULL);
1177 size = (size + 7) & ~7;
1178 ADD_CODE(&td, MINT_VTRESULT);
1180 WRITE32(&td, &size);
1187 if (td.sp > td.stack)
1188 g_warning ("CEE_JMP: stack must be empty");
1189 token = read32 (td.ip + 1);
1190 m = mono_get_method_full (image, token, NULL, generic_context);
1191 ADD_CODE (&td, MINT_JMP);
1192 ADD_CODE (&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
1193 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1197 case CEE_CALLVIRT: /* Fall through */
1198 case CEE_CALLI: /* Fall through */
1200 interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, constrained_class);
1201 constrained_class = NULL;
1206 if (signature->ret->type != MONO_TYPE_VOID) {
1208 MonoClass *klass = mono_class_from_mono_type (signature->ret);
1209 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1210 vt_size = mono_class_value_size (klass, NULL);
1211 vt_size = (vt_size + 7) & ~7;
1214 if (td.sp > td.stack)
1215 g_warning ("%s.%s: CEE_RET: more values on stack: %d", td.method->klass->name, td.method->name, td.sp - td.stack);
1216 if (td.vt_sp != vt_size)
1217 g_error ("%s.%s: CEE_RET: value type stack: %d vs. %d", td.method->klass->name, td.method->name, td.vt_sp, vt_size);
1219 SIMPLE_OP(td, signature->ret->type == MONO_TYPE_VOID ? MINT_RET_VOID : MINT_RET);
1221 ADD_CODE(&td, MINT_RET_VT);
1222 WRITE32(&td, &vt_size);
1225 generating_code = 0;
1229 handle_branch (&td, MINT_BR_S, MINT_BR, 5 + read32 (td.ip + 1));
1231 generating_code = 0;
1234 handle_branch (&td, MINT_BR_S, MINT_BR, 2 + (gint8)td.ip [1]);
1236 generating_code = 0;
1239 one_arg_branch (&td, MINT_BRFALSE_I4, 5 + read32 (td.ip + 1));
1243 one_arg_branch (&td, MINT_BRFALSE_I4, 2 + (gint8)td.ip [1]);
1247 one_arg_branch (&td, MINT_BRTRUE_I4, 5 + read32 (td.ip + 1));
1251 one_arg_branch (&td, MINT_BRTRUE_I4, 2 + (gint8)td.ip [1]);
1255 two_arg_branch (&td, MINT_BEQ_I4, 5 + read32 (td.ip + 1));
1259 two_arg_branch (&td, MINT_BEQ_I4, 2 + (gint8) td.ip [1]);
1263 two_arg_branch (&td, MINT_BGE_I4, 5 + read32 (td.ip + 1));
1267 two_arg_branch (&td, MINT_BGE_I4, 2 + (gint8) td.ip [1]);
1271 two_arg_branch (&td, MINT_BGT_I4, 5 + read32 (td.ip + 1));
1275 two_arg_branch (&td, MINT_BGT_I4, 2 + (gint8) td.ip [1]);
1279 two_arg_branch (&td, MINT_BLT_I4, 5 + read32 (td.ip + 1));
1283 two_arg_branch (&td, MINT_BLT_I4, 2 + (gint8) td.ip [1]);
1287 two_arg_branch (&td, MINT_BLE_I4, 5 + read32 (td.ip + 1));
1291 two_arg_branch (&td, MINT_BLE_I4, 2 + (gint8) td.ip [1]);
1295 two_arg_branch (&td, MINT_BNE_UN_I4, 5 + read32 (td.ip + 1));
1299 two_arg_branch (&td, MINT_BNE_UN_I4, 2 + (gint8) td.ip [1]);
1303 two_arg_branch (&td, MINT_BGE_UN_I4, 5 + read32 (td.ip + 1));
1307 two_arg_branch (&td, MINT_BGE_UN_I4, 2 + (gint8) td.ip [1]);
1311 two_arg_branch (&td, MINT_BGT_UN_I4, 5 + read32 (td.ip + 1));
1315 two_arg_branch (&td, MINT_BGT_UN_I4, 2 + (gint8) td.ip [1]);
1319 two_arg_branch (&td, MINT_BLE_UN_I4, 5 + read32 (td.ip + 1));
1323 two_arg_branch (&td, MINT_BLE_UN_I4, 2 + (gint8) td.ip [1]);
1327 two_arg_branch (&td, MINT_BLT_UN_I4, 5 + read32 (td.ip + 1));
1331 two_arg_branch (&td, MINT_BLT_UN_I4, 2 + (gint8) td.ip [1]);
1336 const unsigned char *next_ip;
1337 const unsigned char *base_ip = td.ip;
1338 unsigned short *next_new_ip;
1341 ADD_CODE(&td, MINT_SWITCH);
1342 ADD_CODE(&td, * (unsigned short *)(&n));
1343 ADD_CODE(&td, * ((unsigned short *)&n + 1));
1345 next_ip = td.ip + n * 4;
1346 next_new_ip = td.new_ip + n * 2;
1347 for (i = 0; i < n; i++) {
1348 offset = read32 (td.ip);
1349 target = next_ip - td.il_code + offset;
1351 target = td.in_offsets [target] - (next_new_ip - td.new_code);
1353 int prev = td.forward_refs [target];
1354 td.forward_refs [td.ip - td.il_code] = prev;
1355 td.forward_refs [target] = td.ip - td.il_code;
1356 td.in_offsets [td.ip - td.il_code] = - (base_ip - td.il_code);
1358 ADD_CODE(&td, * (unsigned short *)(&target));
1359 ADD_CODE(&td, * ((unsigned short *)&target + 1));
1366 CHECK_STACK (&td, 1);
1367 SIMPLE_OP (td, MINT_LDIND_I1);
1368 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1371 CHECK_STACK (&td, 1);
1372 SIMPLE_OP (td, MINT_LDIND_U1);
1373 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1376 CHECK_STACK (&td, 1);
1377 SIMPLE_OP (td, MINT_LDIND_I2);
1378 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1381 CHECK_STACK (&td, 1);
1382 SIMPLE_OP (td, MINT_LDIND_U2);
1383 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1386 CHECK_STACK (&td, 1);
1387 SIMPLE_OP (td, MINT_LDIND_I4);
1388 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1391 CHECK_STACK (&td, 1);
1392 SIMPLE_OP (td, MINT_LDIND_U4);
1393 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1396 CHECK_STACK (&td, 1);
1397 SIMPLE_OP (td, MINT_LDIND_I8);
1398 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1401 CHECK_STACK (&td, 1);
1402 SIMPLE_OP (td, MINT_LDIND_I);
1404 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1407 CHECK_STACK (&td, 1);
1408 SIMPLE_OP (td, MINT_LDIND_R4);
1409 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1412 CHECK_STACK (&td, 1);
1413 SIMPLE_OP (td, MINT_LDIND_R8);
1414 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1417 CHECK_STACK (&td, 1);
1418 SIMPLE_OP (td, MINT_LDIND_REF);
1419 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
1422 CHECK_STACK (&td, 2);
1423 SIMPLE_OP (td, MINT_STIND_REF);
1427 CHECK_STACK (&td, 2);
1428 SIMPLE_OP (td, MINT_STIND_I1);
1432 CHECK_STACK (&td, 2);
1433 SIMPLE_OP (td, MINT_STIND_I2);
1437 CHECK_STACK (&td, 2);
1438 SIMPLE_OP (td, MINT_STIND_I4);
1442 CHECK_STACK (&td, 2);
1443 SIMPLE_OP (td, MINT_STIND_I);
1447 CHECK_STACK (&td, 2);
1448 SIMPLE_OP (td, MINT_STIND_I8);
1452 CHECK_STACK (&td, 2);
1453 SIMPLE_OP (td, MINT_STIND_R4);
1457 CHECK_STACK (&td, 2);
1458 SIMPLE_OP (td, MINT_STIND_R8);
1462 binary_arith_op(&td, MINT_ADD_I4);
1466 binary_arith_op(&td, MINT_SUB_I4);
1470 binary_arith_op(&td, MINT_MUL_I4);
1474 binary_arith_op(&td, MINT_DIV_I4);
1478 binary_arith_op(&td, MINT_DIV_UN_I4);
1482 binary_int_op (&td, MINT_REM_I4);
1486 binary_int_op (&td, MINT_REM_UN_I4);
1490 binary_int_op (&td, MINT_AND_I4);
1494 binary_int_op (&td, MINT_OR_I4);
1498 binary_int_op (&td, MINT_XOR_I4);
1502 shift_op (&td, MINT_SHL_I4);
1506 shift_op (&td, MINT_SHR_I4);
1510 shift_op (&td, MINT_SHR_UN_I4);
1514 unary_arith_op (&td, MINT_NEG_I4);
1518 unary_arith_op (&td, MINT_NOT_I4);
1522 CHECK_STACK (&td, 1);
1523 switch (td.sp [-1].type) {
1525 ADD_CODE(&td, MINT_CONV_U1_R8);
1528 ADD_CODE(&td, MINT_CONV_U1_I4);
1531 ADD_CODE(&td, MINT_CONV_U1_I8);
1534 g_assert_not_reached ();
1537 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1540 CHECK_STACK (&td, 1);
1541 switch (td.sp [-1].type) {
1543 ADD_CODE(&td, MINT_CONV_I1_R8);
1546 ADD_CODE(&td, MINT_CONV_I1_I4);
1549 ADD_CODE(&td, MINT_CONV_I1_I8);
1552 g_assert_not_reached ();
1555 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1558 CHECK_STACK (&td, 1);
1559 switch (td.sp [-1].type) {
1561 ADD_CODE(&td, MINT_CONV_U2_R8);
1564 ADD_CODE(&td, MINT_CONV_U2_I4);
1567 ADD_CODE(&td, MINT_CONV_U2_I8);
1570 g_assert_not_reached ();
1573 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1576 CHECK_STACK (&td, 1);
1577 switch (td.sp [-1].type) {
1579 ADD_CODE(&td, MINT_CONV_I2_R8);
1582 ADD_CODE(&td, MINT_CONV_I2_I4);
1585 ADD_CODE(&td, MINT_CONV_I2_I8);
1588 g_assert_not_reached ();
1591 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1594 CHECK_STACK (&td, 1);
1595 switch (td.sp [-1].type) {
1597 #if SIZEOF_VOID_P == 4
1598 ADD_CODE(&td, MINT_CONV_U4_R8);
1600 ADD_CODE(&td, MINT_CONV_U8_R8);
1604 #if SIZEOF_VOID_P == 8
1605 ADD_CODE(&td, MINT_CONV_U8_I4);
1609 #if SIZEOF_VOID_P == 4
1610 ADD_CODE(&td, MINT_CONV_U4_I8);
1616 g_assert_not_reached ();
1619 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1622 CHECK_STACK (&td, 1);
1623 switch (td.sp [-1].type) {
1625 #if SIZEOF_VOID_P == 8
1626 ADD_CODE(&td, MINT_CONV_I8_R8);
1628 ADD_CODE(&td, MINT_CONV_I4_R8);
1632 #if SIZEOF_VOID_P == 8
1633 ADD_CODE(&td, MINT_CONV_I8_I4);
1641 #if SIZEOF_VOID_P == 4
1642 ADD_CODE(&td, MINT_CONV_I4_I8);
1646 g_assert_not_reached ();
1649 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1652 CHECK_STACK (&td, 1);
1653 switch (td.sp [-1].type) {
1655 ADD_CODE(&td, MINT_CONV_U4_R8);
1660 ADD_CODE(&td, MINT_CONV_U4_I8);
1663 #if SIZEOF_VOID_P == 8
1664 ADD_CODE(&td, MINT_CONV_U4_I8);
1668 g_assert_not_reached ();
1671 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1674 CHECK_STACK (&td, 1);
1675 switch (td.sp [-1].type) {
1677 ADD_CODE(&td, MINT_CONV_I4_R8);
1682 ADD_CODE(&td, MINT_CONV_I4_I8);
1685 #if SIZEOF_VOID_P == 8
1686 ADD_CODE(&td, MINT_CONV_I4_I8);
1690 g_assert_not_reached ();
1693 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1696 CHECK_STACK (&td, 1);
1697 switch (td.sp [-1].type) {
1699 ADD_CODE(&td, MINT_CONV_I8_R8);
1702 ADD_CODE(&td, MINT_CONV_I8_I4);
1707 #if SIZEOF_VOID_P == 4
1708 ADD_CODE(&td, MINT_CONV_I8_I4);
1712 g_assert_not_reached ();
1715 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1718 CHECK_STACK (&td, 1);
1719 switch (td.sp [-1].type) {
1721 ADD_CODE(&td, MINT_CONV_R4_R8);
1724 ADD_CODE(&td, MINT_CONV_R4_I8);
1727 ADD_CODE(&td, MINT_CONV_R4_I4);
1730 g_assert_not_reached ();
1733 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1736 CHECK_STACK (&td, 1);
1737 switch (td.sp [-1].type) {
1739 ADD_CODE(&td, MINT_CONV_R8_I4);
1742 ADD_CODE(&td, MINT_CONV_R8_I8);
1747 g_assert_not_reached ();
1750 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1753 CHECK_STACK (&td, 1);
1754 switch (td.sp [-1].type) {
1756 ADD_CODE(&td, MINT_CONV_U8_I4);
1761 ADD_CODE(&td, MINT_CONV_U8_R8);
1764 #if SIZEOF_VOID_P == 4
1765 ADD_CODE(&td, MINT_CONV_U8_I4);
1769 g_assert_not_reached ();
1772 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1778 vtklass = mono_class_get_full (image, read32 (ip), generic_context);
1781 memcpy (sp [0].data.p, sp [1].data.p, mono_class_value_size (vtklass, NULL));
1787 CHECK_STACK (&td, 1);
1789 token = read32 (td.ip + 1);
1791 if (method->wrapper_type != MONO_WRAPPER_NONE)
1792 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
1794 klass = mono_class_get_full (image, token, generic_context);
1796 ADD_CODE(&td, MINT_LDOBJ);
1797 ADD_CODE(&td, get_data_item_index(&td, klass));
1798 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1799 size = mono_class_value_size (klass, NULL);
1803 SET_TYPE(td.sp - 1, stack_type[mint_type(&klass->byval_arg)], klass);
1808 token = mono_metadata_token_index (read32 (td.ip + 1));
1810 if (method->wrapper_type != MONO_WRAPPER_NONE) {
1811 s = mono_string_new_wrapper(
1812 mono_method_get_wrapper_data (method, token));
1815 s = mono_ldstr (domain, image, token);
1816 ADD_CODE(&td, MINT_LDSTR);
1817 ADD_CODE(&td, get_data_item_index (&td, s));
1818 PUSH_TYPE(&td, STACK_TYPE_O, mono_defaults.string_class);
1823 MonoMethodSignature *csignature;
1824 guint32 vt_stack_used = 0;
1825 guint32 vt_res_size = 0;
1828 token = read32 (td.ip);
1831 if (method->wrapper_type != MONO_WRAPPER_NONE)
1832 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
1834 m = mono_get_method_full (image, token, NULL, generic_context);
1836 csignature = mono_method_signature (m);
1838 td.sp -= csignature->param_count;
1839 ADD_CODE(&td, MINT_NEWOBJ);
1840 ADD_CODE(&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
1841 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1843 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1844 vt_res_size = mono_class_value_size (klass, NULL);
1845 PUSH_VT (&td, vt_res_size);
1847 for (i = 0; i < csignature->param_count; ++i) {
1848 int mt = mint_type(csignature->params [i]);
1849 if (mt == MINT_TYPE_VT) {
1850 MonoClass *k = mono_class_from_mono_type (csignature->params [i]);
1851 gint32 size = mono_class_value_size (k, NULL);
1852 size = (size + 7) & ~7;
1853 vt_stack_used += size;
1856 if (vt_stack_used != 0 || vt_res_size != 0) {
1857 ADD_CODE(&td, MINT_VTRESULT);
1858 ADD_CODE(&td, vt_res_size);
1859 WRITE32(&td, &vt_stack_used);
1860 td.vt_sp -= vt_stack_used;
1862 PUSH_TYPE (&td, stack_type [mint_type (&klass->byval_arg)], klass);
1866 CHECK_STACK (&td, 1);
1867 token = read32 (td.ip + 1);
1868 klass = mono_class_get_full (image, token, generic_context);
1869 ADD_CODE(&td, MINT_CASTCLASS);
1870 ADD_CODE(&td, get_data_item_index (&td, klass));
1871 td.sp [-1].klass = klass;
1875 CHECK_STACK (&td, 1);
1876 token = read32 (td.ip + 1);
1877 klass = mono_class_get_full (image, token, generic_context);
1878 ADD_CODE(&td, MINT_ISINST);
1879 ADD_CODE(&td, get_data_item_index (&td, klass));
1883 switch (td.sp [-1].type) {
1887 ADD_CODE(&td, MINT_CONV_R_UN_I8);
1890 ADD_CODE(&td, MINT_CONV_R_UN_I4);
1893 g_assert_not_reached ();
1895 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1899 CHECK_STACK (&td, 1);
1900 token = read32 (td.ip + 1);
1902 if (method->wrapper_type != MONO_WRAPPER_NONE)
1903 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
1905 klass = mono_class_get_full (image, token, generic_context);
1907 if (mono_class_is_nullable (klass)) {
1908 g_error ("cee_unbox: implement Nullable");
1911 ADD_CODE(&td, MINT_UNBOX);
1912 ADD_CODE(&td, get_data_item_index (&td, klass));
1913 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
1917 CHECK_STACK (&td, 1);
1918 token = read32 (td.ip + 1);
1920 g_assert (method->wrapper_type == MONO_WRAPPER_NONE);
1921 klass = mono_class_get_full (image, token, generic_context);
1923 if (mini_type_is_reference (&klass->byval_arg)) {
1924 g_error ("unbox_any: generic class is reference type");
1925 } else if (mono_class_is_nullable (klass)) {
1926 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Unbox", 1);
1927 /* td.ip is incremented by interp_transform_call */
1928 interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL);
1930 int mt = mint_type (&klass->byval_arg);
1931 ADD_CODE (&td, MINT_UNBOX);
1932 ADD_CODE (&td, get_data_item_index (&td, klass));
1933 SET_TYPE (td.sp - 1, stack_type [mt], klass);
1934 if (mt == MINT_TYPE_VT) {
1935 int size = mono_class_value_size (klass, NULL);
1936 PUSH_VT (&td, size);
1943 CHECK_STACK (&td, 1);
1944 SIMPLE_OP (td, MINT_THROW);
1946 generating_code = 0;
1949 CHECK_STACK (&td, 1);
1950 token = read32 (td.ip + 1);
1951 field = mono_field_from_token (image, token, &klass, generic_context);
1952 mono_class_init (klass);
1953 mt = mint_type(field->type);
1954 ADD_CODE(&td, MINT_LDFLDA);
1955 ADD_CODE(&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
1957 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
1960 CHECK_STACK (&td, 1);
1961 token = read32 (td.ip + 1);
1962 field = mono_field_from_token (image, token, &klass, generic_context);
1963 mono_class_init (klass);
1965 MonoClass *field_klass = mono_class_from_mono_type (field->type);
1966 mt = mint_type (&field_klass->byval_arg);
1967 if (klass->marshalbyref) {
1968 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_LDRMFLD_VT : MINT_LDRMFLD);
1969 ADD_CODE(&td, get_data_item_index (&td, field));
1971 ADD_CODE(&td, MINT_LDFLD_I1 + mt - MINT_TYPE_I1);
1972 ADD_CODE(&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
1974 if (mt == MINT_TYPE_VT) {
1975 int size = mono_class_value_size (field_klass, NULL);
1977 WRITE32(&td, &size);
1979 if (td.sp [-1].type == STACK_TYPE_VT) {
1980 int size = mono_class_value_size (klass, NULL);
1981 size = (size + 7) & ~7;
1983 ADD_CODE (&td, MINT_VTRESULT);
1985 WRITE32 (&td, &size);
1988 SET_TYPE(td.sp - 1, stack_type [mt], field_klass);
1992 CHECK_STACK (&td, 2);
1993 token = read32 (td.ip + 1);
1994 field = mono_field_from_token (image, token, &klass, generic_context);
1995 mono_class_init (klass);
1996 mt = mint_type(field->type);
1997 if (klass->marshalbyref) {
1998 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STRMFLD_VT : MINT_STRMFLD);
1999 ADD_CODE(&td, get_data_item_index (&td, field));
2001 ADD_CODE(&td, MINT_STFLD_I1 + mt - MINT_TYPE_I1);
2002 ADD_CODE(&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
2004 if (mt == MINT_TYPE_VT) {
2005 MonoClass *klass = mono_class_from_mono_type (field->type);
2006 int size = mono_class_value_size (klass, NULL);
2008 WRITE32(&td, &size);
2014 token = read32 (td.ip + 1);
2015 field = mono_field_from_token (image, token, &klass, generic_context);
2016 ADD_CODE(&td, MINT_LDSFLDA);
2017 ADD_CODE(&td, get_data_item_index (&td, field));
2019 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
2022 token = read32 (td.ip + 1);
2023 field = mono_field_from_token (image, token, &klass, generic_context);
2024 mt = mint_type(field->type);
2025 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2026 ADD_CODE(&td, get_data_item_index (&td, field));
2028 if (mt == MINT_TYPE_VT) {
2029 MonoClass *klass = mono_class_from_mono_type (field->type);
2030 int size = mono_class_value_size (klass, NULL);
2032 WRITE32(&td, &size);
2033 klass = field->type->data.klass;
2035 if (mt == MINT_TYPE_O)
2036 klass = mono_class_from_mono_type (field->type);
2039 PUSH_TYPE(&td, stack_type [mt], klass);
2042 CHECK_STACK (&td, 1);
2043 token = read32 (td.ip + 1);
2044 field = mono_field_from_token (image, token, &klass, generic_context);
2045 mt = mint_type(field->type);
2046 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
2047 ADD_CODE(&td, get_data_item_index (&td, field));
2048 if (mt == MINT_TYPE_VT) {
2049 MonoClass *klass = mono_class_from_mono_type (field->type);
2050 int size = mono_class_value_size (klass, NULL);
2052 WRITE32 (&td, &size);
2059 token = read32 (td.ip + 1);
2061 if (method->wrapper_type != MONO_WRAPPER_NONE)
2062 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2064 klass = mono_class_get_full (image, token, generic_context);
2066 ADD_CODE(&td, td.sp [-1].type == STACK_TYPE_VT ? MINT_STOBJ_VT : MINT_STOBJ);
2067 ADD_CODE(&td, get_data_item_index (&td, klass));
2068 if (td.sp [-1].type == STACK_TYPE_VT) {
2069 size = mono_class_value_size (klass, NULL);
2070 size = (size + 7) & ~7;
2077 case CEE_CONV_OVF_I_UN:
2078 CHECK_STACK (&td, 1);
2079 switch (td.sp [-1].type) {
2081 #if SIZEOF_VOID_P == 8
2082 ADD_CODE(&td, MINT_CONV_OVF_I8_UN_R8);
2084 ADD_CODE(&td, MINT_CONV_OVF_I4_UN_R8);
2091 #if SIZEOF_VOID_P == 8
2092 ADD_CODE(&td, MINT_CONV_I8_U4);
2096 g_assert_not_reached ();
2099 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2102 case CEE_CONV_OVF_I8_UN:
2103 CHECK_STACK (&td, 1);
2104 switch (td.sp [-1].type) {
2106 ADD_CODE(&td, MINT_CONV_OVF_I8_UN_R8);
2111 ADD_CODE(&td, MINT_CONV_I8_U4);
2114 g_assert_not_reached ();
2117 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2122 CHECK_STACK (&td, 1);
2123 token = read32 (td.ip + 1);
2124 if (method->wrapper_type != MONO_WRAPPER_NONE)
2125 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2127 klass = mono_class_get_full (image, token, generic_context);
2129 if (mono_class_is_nullable (klass)) {
2130 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Box", 1);
2131 /* td.ip is incremented by interp_transform_call */
2132 interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL);
2133 } else if (!klass->valuetype) {
2134 /* already boxed, do nothing. */
2137 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT && !klass->enumtype) {
2138 size = mono_class_value_size (klass, NULL);
2139 size = (size + 7) & ~7;
2142 ADD_CODE(&td, MINT_BOX);
2143 ADD_CODE(&td, get_data_item_index (&td, klass));
2145 SET_TYPE(td.sp - 1, STACK_TYPE_O, klass);
2152 CHECK_STACK (&td, 1);
2153 token = read32 (td.ip + 1);
2155 if (method->wrapper_type != MONO_WRAPPER_NONE)
2156 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2158 klass = mono_class_get_full (image, token, generic_context);
2160 ADD_CODE(&td, MINT_NEWARR);
2161 ADD_CODE(&td, get_data_item_index (&td, klass));
2162 SET_TYPE(td.sp - 1, STACK_TYPE_O, klass);
2166 CHECK_STACK (&td, 1);
2167 SIMPLE_OP (td, MINT_LDLEN);
2168 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2171 CHECK_STACK (&td, 2);
2173 token = read32 (td.ip + 1);
2175 if (method->wrapper_type != MONO_WRAPPER_NONE)
2176 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2178 klass = mono_class_get_full (image, token, generic_context);
2180 ADD_CODE(&td, MINT_LDELEMA);
2181 ADD_CODE(&td, get_data_item_index (&td, klass));
2184 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2187 CHECK_STACK (&td, 2);
2189 SIMPLE_OP (td, MINT_LDELEM_I1);
2191 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2194 CHECK_STACK (&td, 2);
2196 SIMPLE_OP (td, MINT_LDELEM_U1);
2198 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2201 CHECK_STACK (&td, 2);
2203 SIMPLE_OP (td, MINT_LDELEM_I2);
2205 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2208 CHECK_STACK (&td, 2);
2210 SIMPLE_OP (td, MINT_LDELEM_U2);
2212 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2215 CHECK_STACK (&td, 2);
2217 SIMPLE_OP (td, MINT_LDELEM_I4);
2219 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2222 CHECK_STACK (&td, 2);
2224 SIMPLE_OP (td, MINT_LDELEM_U4);
2226 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2229 CHECK_STACK (&td, 2);
2231 SIMPLE_OP (td, MINT_LDELEM_I8);
2233 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2236 CHECK_STACK (&td, 2);
2238 SIMPLE_OP (td, MINT_LDELEM_I);
2240 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2243 CHECK_STACK (&td, 2);
2245 SIMPLE_OP (td, MINT_LDELEM_R4);
2247 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2250 CHECK_STACK (&td, 2);
2252 SIMPLE_OP (td, MINT_LDELEM_R8);
2254 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2256 case CEE_LDELEM_REF:
2257 CHECK_STACK (&td, 2);
2259 SIMPLE_OP (td, MINT_LDELEM_REF);
2261 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
2264 CHECK_STACK (&td, 2);
2265 token = read32 (td.ip + 1);
2266 klass = mono_class_get_full (image, token, generic_context);
2267 switch (mint_type (&klass->byval_arg)) {
2270 SIMPLE_OP (td, MINT_LDELEM_I4);
2272 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2276 SIMPLE_OP (td, MINT_LDELEM_REF);
2278 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
2280 case MINT_TYPE_VT: {
2281 int size = mono_class_value_size (klass, NULL);
2283 SIMPLE_OP (td, MINT_LDELEM_VT);
2284 ADD_CODE (&td, get_data_item_index (&td, klass));
2285 WRITE32 (&td, &size);
2287 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_VT);
2288 PUSH_VT (&td, size);
2292 GString *res = g_string_new ("");
2293 mono_type_get_desc (res, &klass->byval_arg, TRUE);
2294 g_print ("LDELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
2295 g_string_free (res, TRUE);
2303 CHECK_STACK (&td, 3);
2305 SIMPLE_OP (td, MINT_STELEM_I);
2309 CHECK_STACK (&td, 3);
2311 SIMPLE_OP (td, MINT_STELEM_I1);
2315 CHECK_STACK (&td, 3);
2317 SIMPLE_OP (td, MINT_STELEM_I2);
2321 CHECK_STACK (&td, 3);
2323 SIMPLE_OP (td, MINT_STELEM_I4);
2327 CHECK_STACK (&td, 3);
2329 SIMPLE_OP (td, MINT_STELEM_I8);
2333 CHECK_STACK (&td, 3);
2335 SIMPLE_OP (td, MINT_STELEM_R4);
2339 CHECK_STACK (&td, 3);
2341 SIMPLE_OP (td, MINT_STELEM_R8);
2344 case CEE_STELEM_REF:
2345 CHECK_STACK (&td, 3);
2347 SIMPLE_OP (td, MINT_STELEM_REF);
2351 CHECK_STACK (&td, 3);
2353 token = read32 (td.ip + 1);
2354 klass = mono_class_get_full (image, token, generic_context);
2355 switch (mint_type (&klass->byval_arg)) {
2357 SIMPLE_OP (td, MINT_STELEM_I4);
2360 SIMPLE_OP (td, MINT_STELEM_REF);
2362 case MINT_TYPE_VT: {
2363 int size = mono_class_value_size (klass, NULL);
2364 SIMPLE_OP (td, MINT_STELEM_VT);
2365 ADD_CODE (&td, get_data_item_index (&td, klass));
2366 WRITE32 (&td, &size);
2371 GString *res = g_string_new ("");
2372 mono_type_get_desc (res, &klass->byval_arg, TRUE);
2373 g_print ("STELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
2374 g_string_free (res, TRUE);
2383 case CEE_CONV_OVF_U1:
2385 case CEE_CONV_OVF_I8:
2387 #if SIZEOF_VOID_P == 8
2388 case CEE_CONV_OVF_U:
2390 case CEE_REFANYVAL: ves_abort(); break;
2393 CHECK_STACK (&td, 1);
2394 SIMPLE_OP (td, MINT_CKFINITE);
2396 case CEE_CONV_OVF_I1:
2397 case CEE_CONV_OVF_I1_UN:
2398 CHECK_STACK (&td, 1);
2399 switch (td.sp [-1].type) {
2401 ADD_CODE(&td, MINT_CONV_OVF_I1_R8);
2404 ADD_CODE(&td, MINT_CONV_OVF_I1_I4);
2407 ADD_CODE(&td, MINT_CONV_OVF_I1_I8);
2410 g_assert_not_reached ();
2413 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2415 case CEE_CONV_OVF_U1:
2416 case CEE_CONV_OVF_U1_UN:
2417 CHECK_STACK (&td, 1);
2418 switch (td.sp [-1].type) {
2420 ADD_CODE(&td, MINT_CONV_OVF_U1_R8);
2423 ADD_CODE(&td, MINT_CONV_OVF_U1_I4);
2426 ADD_CODE(&td, MINT_CONV_OVF_U1_I8);
2429 g_assert_not_reached ();
2432 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2434 case CEE_CONV_OVF_I2:
2435 case CEE_CONV_OVF_I2_UN:
2436 CHECK_STACK (&td, 1);
2437 switch (td.sp [-1].type) {
2439 ADD_CODE(&td, MINT_CONV_OVF_I2_R8);
2442 ADD_CODE(&td, MINT_CONV_OVF_I2_I4);
2445 ADD_CODE(&td, MINT_CONV_OVF_I2_I8);
2448 g_assert_not_reached ();
2451 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2453 case CEE_CONV_OVF_U2_UN:
2454 case CEE_CONV_OVF_U2:
2455 CHECK_STACK (&td, 1);
2456 switch (td.sp [-1].type) {
2458 ADD_CODE(&td, MINT_CONV_OVF_U2_R8);
2461 ADD_CODE(&td, MINT_CONV_OVF_U2_I4);
2464 ADD_CODE(&td, MINT_CONV_OVF_U2_I8);
2467 g_assert_not_reached ();
2470 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2472 #if SIZEOF_VOID_P == 4
2473 case CEE_CONV_OVF_I:
2475 case CEE_CONV_OVF_I4:
2476 case CEE_CONV_OVF_I4_UN:
2477 CHECK_STACK (&td, 1);
2478 switch (td.sp [-1].type) {
2480 ADD_CODE(&td, MINT_CONV_OVF_I4_R8);
2483 if (*td.ip == CEE_CONV_OVF_I4_UN)
2484 ADD_CODE(&td, MINT_CONV_OVF_I4_U4);
2487 ADD_CODE(&td, MINT_CONV_OVF_I4_I8);
2490 g_assert_not_reached ();
2493 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2495 #if SIZEOF_VOID_P == 4
2496 case CEE_CONV_OVF_U:
2498 case CEE_CONV_OVF_U4:
2499 case CEE_CONV_OVF_U4_UN:
2500 CHECK_STACK (&td, 1);
2501 switch (td.sp [-1].type) {
2503 ADD_CODE(&td, MINT_CONV_OVF_U4_R8);
2506 if (*td.ip != CEE_CONV_OVF_U4_UN)
2507 ADD_CODE(&td, MINT_CONV_OVF_U4_I4);
2510 ADD_CODE(&td, MINT_CONV_OVF_U4_I8);
2513 g_assert_not_reached ();
2516 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2518 #if SIZEOF_VOID_P == 8
2519 case CEE_CONV_OVF_I:
2521 case CEE_CONV_OVF_I8:
2522 CHECK_STACK (&td, 1);
2523 switch (td.sp [-1].type) {
2525 ADD_CODE(&td, MINT_CONV_OVF_I8_R8);
2528 ADD_CODE(&td, MINT_CONV_I8_I4);
2533 g_assert_not_reached ();
2536 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2538 #if SIZEOF_VOID_P == 8
2539 case CEE_CONV_OVF_U:
2541 case CEE_CONV_OVF_U8:
2542 CHECK_STACK (&td, 1);
2543 switch (td.sp [-1].type) {
2545 ADD_CODE(&td, MINT_CONV_OVF_U8_R8);
2548 ADD_CODE(&td, MINT_CONV_OVF_U8_I4);
2553 g_assert_not_reached ();
2556 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2561 token = read32 (td.ip + 1);
2562 handle = mono_ldtoken (image, token, &klass, generic_context);
2563 mt = mint_type(&klass->byval_arg);
2564 g_assert (mt == MINT_TYPE_VT);
2565 size = mono_class_value_size (klass, NULL);
2566 g_assert (size == sizeof(gpointer));
2567 PUSH_VT(&td, sizeof(gpointer));
2568 ADD_CODE(&td, MINT_LDTOKEN);
2569 ADD_CODE(&td, get_data_item_index (&td, handle));
2570 PUSH_SIMPLE_TYPE(&td, stack_type [mt]);
2575 binary_arith_op(&td, MINT_ADD_OVF_I4);
2578 case CEE_ADD_OVF_UN:
2579 binary_arith_op(&td, MINT_ADD_OVF_UN_I4);
2583 binary_arith_op(&td, MINT_MUL_OVF_I4);
2586 case CEE_MUL_OVF_UN:
2587 binary_arith_op(&td, MINT_MUL_OVF_UN_I4);
2591 binary_arith_op(&td, MINT_SUB_OVF_I4);
2594 case CEE_SUB_OVF_UN:
2595 binary_arith_op(&td, MINT_SUB_OVF_UN_I4);
2598 case CEE_ENDFINALLY:
2599 SIMPLE_OP (td, MINT_ENDFINALLY);
2600 generating_code = 0;
2604 handle_branch (&td, MINT_LEAVE_S, MINT_LEAVE, 5 + read32 (td.ip + 1));
2606 generating_code = 0;
2610 handle_branch (&td, MINT_LEAVE_S, MINT_LEAVE, 2 + (gint8)td.ip [1]);
2612 generating_code = 0;
2617 case CEE_MONO_CALLI_EXTRA_ARG:
2618 /* Same as CEE_CALLI, except that we drop the extra arg required for llvm specific behaviour */
2619 ADD_CODE (&td, MINT_POP);
2622 interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, NULL);
2624 case CEE_MONO_JIT_ICALL_ADDR: {
2627 MonoJitICallInfo *info;
2629 token = read32 (td.ip + 1);
2631 func = mono_method_get_wrapper_data (method, token);
2632 info = mono_find_jit_icall_by_addr (func);
2634 ADD_CODE (&td, MINT_LDFTN);
2635 ADD_CODE (&td, get_data_item_index (&td, func));
2636 PUSH_SIMPLE_TYPE (&td, STACK_TYPE_I);
2639 case CEE_MONO_ICALL: {
2642 MonoJitICallInfo *info;
2644 token = read32 (td.ip + 1);
2646 func = mono_method_get_wrapper_data (method, token);
2647 info = mono_find_jit_icall_by_addr (func);
2650 CHECK_STACK (&td, info->sig->param_count);
2651 switch (info->sig->param_count) {
2653 if (MONO_TYPE_IS_VOID (info->sig->ret))
2654 ADD_CODE (&td,MINT_ICALL_V_V);
2656 ADD_CODE (&td, MINT_ICALL_V_P);
2659 if (MONO_TYPE_IS_VOID (info->sig->ret))
2660 ADD_CODE (&td,MINT_ICALL_P_V);
2662 ADD_CODE (&td,MINT_ICALL_P_P);
2665 if (MONO_TYPE_IS_VOID (info->sig->ret)) {
2666 if (info->sig->params [1]->type == MONO_TYPE_I4)
2667 ADD_CODE (&td,MINT_ICALL_PI_V);
2669 ADD_CODE (&td,MINT_ICALL_PP_V);
2671 if (info->sig->params [1]->type == MONO_TYPE_I4)
2672 ADD_CODE (&td,MINT_ICALL_PI_P);
2674 ADD_CODE (&td,MINT_ICALL_PP_P);
2678 g_assert (MONO_TYPE_IS_VOID (info->sig->ret));
2679 if (info->sig->params [2]->type == MONO_TYPE_I4)
2680 ADD_CODE (&td,MINT_ICALL_PPI_V);
2682 ADD_CODE (&td,MINT_ICALL_PPP_V);
2685 g_assert_not_reached ();
2688 if (func == mono_ftnptr_to_delegate) {
2689 g_error ("TODO: ?");
2691 ADD_CODE(&td, get_data_item_index (&td, func));
2692 td.sp -= info->sig->param_count;
2694 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2696 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2700 case CEE_MONO_VTADDR: {
2702 CHECK_STACK (&td, 1);
2703 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
2704 size = mono_class_native_size(td.sp [-1].klass, NULL);
2706 size = mono_class_value_size(td.sp [-1].klass, NULL);
2707 size = (size + 7) & ~7;
2708 ADD_CODE(&td, MINT_VTRESULT);
2710 WRITE32(&td, &size);
2713 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2716 case CEE_MONO_LDPTR:
2717 case CEE_MONO_CLASSCONST:
2718 token = read32 (td.ip + 1);
2720 ADD_CODE(&td, MINT_MONO_LDPTR);
2721 ADD_CODE(&td, get_data_item_index (&td, mono_method_get_wrapper_data (method, token)));
2722 td.sp [0].type = STACK_TYPE_I;
2725 case CEE_MONO_OBJADDR:
2726 CHECK_STACK (&td, 1);
2728 td.sp[-1].type = STACK_TYPE_MP;
2731 case CEE_MONO_NEWOBJ:
2732 token = read32 (td.ip + 1);
2734 ADD_CODE(&td, MINT_MONO_NEWOBJ);
2735 ADD_CODE(&td, get_data_item_index (&td, mono_method_get_wrapper_data (method, token)));
2736 td.sp [0].type = STACK_TYPE_O;
2739 case CEE_MONO_RETOBJ:
2740 CHECK_STACK (&td, 1);
2741 token = read32 (td.ip + 1);
2743 ADD_CODE(&td, MINT_MONO_RETOBJ);
2746 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2748 /*stackval_from_data (signature->ret, frame->retval, sp->data.vt, signature->pinvoke);*/
2750 if (td.sp > td.stack)
2751 g_warning ("CEE_MONO_RETOBJ: more values on stack: %d", td.sp-td.stack);
2753 case CEE_MONO_LDNATIVEOBJ:
2754 token = read32 (td.ip + 1);
2756 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2757 g_assert(klass->valuetype);
2758 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2760 case CEE_MONO_SAVE_LMF:
2761 case CEE_MONO_RESTORE_LMF:
2762 case CEE_MONO_NOT_TAKEN:
2765 case CEE_MONO_LDPTR_INT_REQ_FLAG:
2766 ADD_CODE (&td, MINT_MONO_LDPTR);
2767 ADD_CODE (&td, get_data_item_index (&td, mono_thread_interruption_request_flag ()));
2768 PUSH_TYPE (&td, STACK_TYPE_MP, NULL);
2772 g_error ("transform.c: Unimplemented opcode: 0xF0 %02x at 0x%x\n", *td.ip, td.ip-header->code);
2782 case CEE_PREFIXREF: ves_abort(); break;
2785 * Note: Exceptions thrown when executing a prefixed opcode need
2786 * to take into account the number of prefix bytes (usually the
2787 * throw point is just (ip - n_prefix_bytes).
2793 case CEE_ARGLIST: ves_abort(); break;
2796 CHECK_STACK(&td, 2);
2797 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2798 ADD_CODE(&td, MINT_CEQ_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2800 ADD_CODE(&td, MINT_CEQ_I4 + td.sp [-1].type - STACK_TYPE_I4);
2802 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2806 CHECK_STACK(&td, 2);
2807 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2808 ADD_CODE(&td, MINT_CGT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2810 ADD_CODE(&td, MINT_CGT_I4 + td.sp [-1].type - STACK_TYPE_I4);
2812 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2816 CHECK_STACK(&td, 2);
2817 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2818 ADD_CODE(&td, MINT_CGT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2820 ADD_CODE(&td, MINT_CGT_UN_I4 + td.sp [-1].type - STACK_TYPE_I4);
2822 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2826 CHECK_STACK(&td, 2);
2827 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2828 ADD_CODE(&td, MINT_CLT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2830 ADD_CODE(&td, MINT_CLT_I4 + td.sp [-1].type - STACK_TYPE_I4);
2832 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2836 CHECK_STACK(&td, 2);
2837 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2838 ADD_CODE(&td, MINT_CLT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2840 ADD_CODE(&td, MINT_CLT_UN_I4 + td.sp [-1].type - STACK_TYPE_I4);
2842 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2845 case CEE_LDVIRTFTN: /* fallthrough */
2848 if (*td.ip == CEE_LDVIRTFTN) {
2849 CHECK_STACK (&td, 1);
2852 token = read32 (td.ip + 1);
2853 if (method->wrapper_type != MONO_WRAPPER_NONE)
2854 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
2856 m = mono_get_method_full (image, token, NULL, generic_context);
2858 if (method->wrapper_type == MONO_WRAPPER_NONE && m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
2859 m = mono_marshal_get_synchronized_wrapper (m);
2861 ADD_CODE(&td, *td.ip == CEE_LDFTN ? MINT_LDFTN : MINT_LDVIRTFTN);
2862 ADD_CODE(&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
2863 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2865 PUSH_SIMPLE_TYPE (&td, STACK_TYPE_F);
2869 load_arg (&td, read16 (td.ip + 1));
2873 int n = read16 (td.ip + 1);
2874 if (n == 0 && signature->hasthis) {
2875 g_error ("LDTHISA: NOPE");
2876 ADD_CODE(&td, MINT_LDTHISA);
2879 ADD_CODE(&td, MINT_LDARGA);
2880 ADD_CODE(&td, td.rtm->arg_offsets [n]); /* FIX for large offsets */
2882 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
2887 store_arg (&td, read16 (td.ip + 1));
2891 load_local (&td, read16 (td.ip + 1));
2895 ADD_CODE(&td, MINT_LDLOCA_S);
2896 ADD_CODE(&td, td.rtm->local_offsets [read16 (td.ip + 1)]);
2897 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
2901 store_local (&td, read16 (td.ip + 1));
2905 CHECK_STACK (&td, 1);
2906 #if SIZEOF_VOID_P == 8
2907 if (td.sp [-1].type == STACK_TYPE_I8)
2908 ADD_CODE(&td, MINT_CONV_I4_I8);
2910 ADD_CODE(&td, MINT_LOCALLOC);
2911 if (td.sp != td.stack + 1)
2912 g_warning("CEE_LOCALLOC: stack not empty");
2914 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2917 case CEE_UNUSED57: ves_abort(); break;
2918 case CEE_ENDFILTER: ves_abort(); break;
2920 case CEE_UNALIGNED_:
2922 /* FIX: should do something? */;
2926 /* FIX: should do something? */;
2930 /* FIX: should do something? */;
2933 CHECK_STACK(&td, 1);
2934 token = read32 (td.ip + 1);
2935 klass = mono_class_get_full (image, token, generic_context);
2936 ADD_CODE(&td, MINT_INITOBJ);
2937 i32 = mono_class_value_size (klass, NULL);
2943 CHECK_STACK(&td, 3);
2944 /* FIX? convert length to I8? */
2945 ADD_CODE(&td, MINT_CPBLK);
2949 case CEE_CONSTRAINED_:
2950 token = read32 (td.ip + 1);
2951 constrained_class = mono_class_get_full (image, token, generic_context);
2952 mono_class_init (constrained_class);
2956 CHECK_STACK(&td, 3);
2957 ADD_CODE(&td, MINT_INITBLK);
2962 /* FIXME: implement */
2967 SIMPLE_OP (td, MINT_RETHROW);
2968 generating_code = 0;
2972 token = read32 (td.ip + 1);
2974 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
2976 MonoType *type = mono_type_create_from_typespec (image, token);
2977 size = mono_type_size (type, &align);
2980 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
2981 mono_class_init (szclass);
2983 if (!szclass->valuetype)
2984 THROW_EX (mono_exception_from_name (mono_defaults.corlib, "System", "InvalidProgramException"), ip - 5);
2986 size = mono_class_value_size (szclass, &align);
2988 ADD_CODE(&td, MINT_LDC_I4);
2989 WRITE32(&td, &size);
2990 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
2994 case CEE_REFANYTYPE: ves_abort(); break;
2997 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);
3001 g_error ("transform.c: Unimplemented opcode: %02x at 0x%x\n", *td.ip, td.ip-header->code);
3004 if (td.new_ip - td.new_code != new_in_start_offset)
3005 td.last_new_ip = td.new_code + new_in_start_offset;
3006 else if (td.is_bb_start [td.in_start - td.il_code])
3007 td.is_bb_start [td.ip - td.il_code] = 1;
3009 td.last_ip = td.in_start;
3012 if (mono_interp_traceopt) {
3013 const guint16 *p = td.new_code;
3014 printf("Runtime method: %p, VT stack size: %d\n", rtm, td.max_vt_sp);
3015 printf("Calculated stack size: %d, stated size: %d\n", td.max_stack_height, header->max_stack);
3016 while (p < td.new_ip) {
3017 p = mono_interp_dis_mintop(td.new_code, p);
3022 rtm->clauses = mono_mempool_alloc (domain->mp, header->num_clauses * sizeof(MonoExceptionClause));
3023 memcpy (rtm->clauses, header->clauses, header->num_clauses * sizeof(MonoExceptionClause));
3024 rtm->code = mono_mempool_alloc (domain->mp, (td.new_ip - td.new_code) * sizeof(gushort));
3025 memcpy (rtm->code, td.new_code, (td.new_ip - td.new_code) * sizeof(gushort));
3026 g_free (td.new_code);
3027 rtm->new_body_start = rtm->code + body_start_offset;
3028 rtm->num_clauses = header->num_clauses;
3029 for (i = 0; i < header->num_clauses; i++) {
3030 MonoExceptionClause *c = rtm->clauses + i;
3031 int end_off = c->try_offset + c->try_len;
3032 c->try_offset = td.in_offsets [c->try_offset];
3033 c->try_len = td.in_offsets [end_off] - c->try_offset;
3034 end_off = c->handler_offset + c->handler_len;
3035 c->handler_offset = td.in_offsets [c->handler_offset];
3036 c->handler_len = td.in_offsets [end_off] - c->handler_offset;
3038 rtm->vt_stack_size = td.max_vt_sp;
3039 rtm->alloca_size = rtm->locals_size + rtm->args_size + rtm->vt_stack_size + rtm->stack_size;
3040 rtm->data_items = mono_mempool_alloc (domain->mp, td.n_data_items * sizeof (td.data_items [0]));
3041 memcpy (rtm->data_items, td.data_items, td.n_data_items * sizeof (td.data_items [0]));
3042 g_free (td.in_offsets);
3043 g_free (td.forward_refs);
3044 for (i = 0; i < header->code_size; ++i)
3045 g_free (td.stack_state [i]);
3046 g_free (td.stack_state);
3047 g_free (td.stack_height);
3048 g_free (td.vt_stack_size);
3049 g_free (td.data_items);
3050 g_hash_table_destroy (td.data_hash);
3053 static mono_mutex_t calc_section;
3056 mono_interp_transform_init (void)
3058 mono_os_mutex_init_recursive(&calc_section);
3062 mono_interp_transform_method (RuntimeMethod *runtime_method, ThreadContext *context)
3064 int i, align, size, offset;
3065 MonoMethod *method = runtime_method->method;
3066 MonoImage *image = method->klass->image;
3067 MonoMethodHeader *header = mono_method_get_header (method);
3068 MonoMethodSignature *signature = mono_method_signature (method);
3069 register const unsigned char *ip, *end;
3070 const MonoOpcode *opcode;
3073 MonoDomain *domain = mono_domain_get ();
3074 unsigned char *is_bb_start;
3076 MonoVTable *method_class_vt;
3078 MonoGenericContext *generic_context = NULL;
3080 // g_printerr ("TRANSFORM(0x%016lx): begin %s::%s\n", mono_thread_current (), method->klass->name, method->name);
3081 method_class_vt = mono_class_vtable (domain, runtime_method->method->klass);
3082 if (!method_class_vt->initialized) {
3084 MonoInvocation *last_env_frame = context->env_frame;
3085 jmp_buf *old_env = context->current_env;
3088 MonoException *failed = context->env_frame->ex;
3089 context->env_frame->ex = NULL;
3090 context->env_frame = last_env_frame;
3091 context->current_env = old_env;
3094 context->env_frame = context->current_frame;
3095 context->current_env = &env;
3096 mono_runtime_class_init (method_class_vt);
3097 context->env_frame = last_env_frame;
3098 context->current_env = old_env;
3101 mono_profiler_method_jit (method); /* sort of... */
3103 if (mono_method_signature (method)->is_inflated)
3104 generic_context = mono_method_get_context (method);
3106 MonoGenericContainer *generic_container = mono_method_get_generic_container (method);
3107 if (generic_container)
3108 generic_context = &generic_container->context;
3111 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
3112 MonoMethod *nm = NULL;
3113 mono_os_mutex_lock(&calc_section);
3114 if (runtime_method->transformed) {
3115 mono_os_mutex_unlock(&calc_section);
3116 g_error ("FIXME: no jit info?");
3117 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3121 /* assumes all internal calls with an array this are built in... */
3122 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL && (! mono_method_signature (method)->hasthis || method->klass->rank == 0)) {
3123 nm = mono_marshal_get_native_wrapper (method, TRUE, FALSE);
3124 signature = mono_method_signature (nm);
3126 const char *name = method->name;
3127 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
3128 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
3129 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ("ves_icall_mono_delegate_ctor");
3131 char *wrapper_name = g_strdup_printf ("__icall_wrapper_%s", mi->name);
3132 nm = mono_marshal_get_icall_wrapper (mi->sig, wrapper_name, mi->func, TRUE);
3133 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
3134 nm = mono_marshal_get_delegate_invoke (method, NULL);
3135 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
3136 nm = mono_marshal_get_delegate_begin_invoke (method);
3137 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
3138 nm = mono_marshal_get_delegate_end_invoke (method);
3142 runtime_method->code = g_malloc(sizeof(short));
3143 runtime_method->code[0] = MINT_CALLRUN;
3147 runtime_method->stack_size = sizeof (stackval); /* for tracing */
3148 runtime_method->alloca_size = runtime_method->stack_size;
3149 runtime_method->transformed = TRUE;
3150 mono_os_mutex_unlock(&calc_section);
3151 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3155 header = mono_method_get_header (nm);
3156 mono_os_mutex_unlock(&calc_section);
3158 g_assert ((signature->param_count + signature->hasthis) < 1000);
3159 g_assert (header->max_stack < 10000);
3160 /* intern the strings in the method. */
3162 end = ip + header->code_size;
3164 is_bb_start = g_malloc0(header->code_size);
3165 is_bb_start [0] = 1;
3172 else if (in == 0xf0) {
3174 in = *ip + MONO_CEE_MONO_ICALL;
3176 opcode = &mono_opcodes [in];
3177 switch (opcode->argument) {
3178 case MonoInlineNone:
3181 case MonoInlineString:
3182 if (method->wrapper_type == MONO_WRAPPER_NONE)
3183 mono_ldstr (domain, image, mono_metadata_token_index (read32 (ip + 1)));
3186 case MonoInlineType:
3187 if (method->wrapper_type == MONO_WRAPPER_NONE) {
3188 class = mono_class_get_full (image, read32 (ip + 1), generic_context);
3189 mono_class_init (class);
3190 /* quick fix to not do this for the fake ptr classes - probably should not be getting the vtable at all here */
3192 g_error ("FIXME: interface method lookup: %s (in method %s)", class->name, method->name);
3193 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE) && class->interface_offsets != NULL)
3194 mono_class_vtable (domain, class);
3199 case MonoInlineMethod:
3200 if (method->wrapper_type == MONO_WRAPPER_NONE && *ip != CEE_CALLI) {
3201 m = mono_get_method_full (image, read32 (ip + 1), NULL, generic_context);
3203 g_free (is_bb_start);
3204 g_error ("FIXME: where to get method and class string?");
3206 // return mono_get_exception_missing_method ();
3208 mono_class_init (m->klass);
3209 if (!mono_class_is_interface (m->klass))
3210 mono_class_vtable (domain, m->klass);
3214 case MonoInlineField:
3218 case MonoShortInlineR:
3221 case MonoInlineBrTarget:
3222 offset = read32 (ip + 1);
3224 backwards = offset < 0;
3225 offset += ip - header->code;
3226 g_assert (offset >= 0 && offset < header->code_size);
3227 is_bb_start [offset] |= backwards ? 2 : 1;
3229 case MonoShortInlineBrTarget:
3230 offset = ((gint8 *)ip) [1];
3232 backwards = offset < 0;
3233 offset += ip - header->code;
3234 g_assert (offset >= 0 && offset < header->code_size);
3235 is_bb_start [offset] |= backwards ? 2 : 1;
3240 case MonoShortInlineVar:
3241 case MonoShortInlineI:
3244 case MonoInlineSwitch: {
3246 const unsigned char *next_ip;
3250 next_ip = ip + 4 * n;
3251 for (i = 0; i < n; i++) {
3252 offset = read32 (ip);
3253 backwards = offset < 0;
3254 offset += next_ip - header->code;
3255 g_assert (offset >= 0 && offset < header->code_size);
3256 is_bb_start [offset] |= backwards ? 2 : 1;
3266 g_assert_not_reached ();
3269 // g_printerr ("TRANSFORM(0x%016lx): end %s::%s\n", mono_thread_current (), method->klass->name, method->name);
3271 /* the rest needs to be locked so it is only done once */
3272 mono_os_mutex_lock(&calc_section);
3273 if (runtime_method->transformed) {
3274 mono_os_mutex_unlock(&calc_section);
3275 g_free (is_bb_start);
3276 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3280 runtime_method->local_offsets = g_malloc (header->num_locals * sizeof(guint32));
3281 runtime_method->stack_size = (sizeof (stackval) + 2) * header->max_stack; /* + 1 for returns of called functions + 1 for 0-ing in trace*/
3282 runtime_method->stack_size = (runtime_method->stack_size + 7) & ~7;
3284 for (i = 0; i < header->num_locals; ++i) {
3285 size = mono_type_size (header->locals [i], &align);
3286 offset += align - 1;
3287 offset &= ~(align - 1);
3288 runtime_method->local_offsets [i] = offset;
3291 offset = (offset + 7) & ~7;
3292 runtime_method->locals_size = offset;
3293 g_assert (runtime_method->locals_size < 65536);
3295 runtime_method->arg_offsets = g_malloc ((!!signature->hasthis + signature->param_count) * sizeof(guint32));
3297 if (signature->hasthis) {
3298 g_assert (!signature->pinvoke);
3299 size = mono_type_stack_size (&method->klass->byval_arg, &align);
3300 offset += align - 1;
3301 offset &= ~(align - 1);
3302 runtime_method->arg_offsets [0] = offset;
3306 for (i = 0; i < signature->param_count; ++i) {
3307 if (signature->pinvoke) {
3309 size = mono_type_native_stack_size (signature->params [i], &dummy);
3313 size = mono_type_stack_size (signature->params [i], &align);
3314 offset += align - 1;
3315 offset &= ~(align - 1);
3316 runtime_method->arg_offsets [i + !!signature->hasthis] = offset;
3319 offset = (offset + 7) & ~7;
3320 runtime_method->args_size = offset;
3321 g_assert (runtime_method->args_size < 10000);
3323 generate (method, runtime_method, is_bb_start, generic_context);
3325 g_free (is_bb_start);
3327 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3328 runtime_method->transformed = TRUE;
3329 mono_os_mutex_unlock(&calc_section);