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>
23 // TODO: export from marshal.c
24 MonoDelegate* mono_ftnptr_to_delegate (MonoClass *klass, gpointer ftn);
38 MonoMethodHeader *header;
40 const unsigned char *il_code;
41 const unsigned char *ip;
42 const unsigned char *last_ip;
43 const unsigned char *in_start;
47 StackInfo **stack_state;
50 unsigned char *is_bb_start;
51 unsigned short *new_code;
52 unsigned short *new_code_end;
53 unsigned short *new_ip;
54 unsigned short *last_new_ip;
55 unsigned int max_code_size;
58 unsigned int max_stack_height;
60 unsigned int max_vt_sp;
64 GHashTable *data_hash;
67 #define MINT_TYPE_I1 0
68 #define MINT_TYPE_U1 1
69 #define MINT_TYPE_I2 2
70 #define MINT_TYPE_U2 3
71 #define MINT_TYPE_I4 4
72 #define MINT_TYPE_I8 5
73 #define MINT_TYPE_R4 6
74 #define MINT_TYPE_R8 7
77 #define MINT_TYPE_VT 10
79 #define STACK_TYPE_I4 0
80 #define STACK_TYPE_I8 1
81 #define STACK_TYPE_R8 2
82 #define STACK_TYPE_O 3
83 #define STACK_TYPE_VT 4
84 #define STACK_TYPE_MP 5
85 #define STACK_TYPE_F 6
87 static const char *stack_type_string [] = { "I4", "I8", "R8", "O ", "VT", "MP", "F " };
89 #if SIZEOF_VOID_P == 8
90 #define STACK_TYPE_I STACK_TYPE_I8
92 #define STACK_TYPE_I STACK_TYPE_I4
95 static int stack_type [] = {
100 STACK_TYPE_I4, /*I4*/
101 STACK_TYPE_I8, /*I8*/
102 STACK_TYPE_R8, /*R4*/
103 STACK_TYPE_R8, /*R8*/
110 grow_code (TransformData *td)
112 unsigned int old_ip_offset = td->new_ip - td->new_code;
113 unsigned int old_last_ip_offset = td->last_new_ip - td->new_code;
114 g_assert (old_ip_offset <= td->max_code_size);
115 td->new_code = g_realloc (td->new_code, (td->max_code_size *= 2) * sizeof (td->new_code [0]));
116 td->new_code_end = td->new_code + td->max_code_size;
117 td->new_ip = td->new_code + old_ip_offset;
118 td->last_new_ip = td->new_code + old_last_ip_offset;
121 #define ENSURE_CODE(td, n) \
123 if ((td)->new_ip + (n) > (td)->new_code_end) \
127 #define ADD_CODE(td, n) \
129 if ((td)->new_ip == (td)->new_code_end) \
131 *(td)->new_ip++ = (n); \
134 #define CHECK_STACK(td, n) \
136 int stack_size = (td)->sp - (td)->stack; \
137 if (stack_size < (n)) \
138 g_warning ("%s.%s: not enough values (%d < %d) on stack at %04x", \
139 (td)->method->klass->name, (td)->method->name, \
140 stack_size, n, (td)->ip - (td)->il_code); \
143 #define ENSURE_I4(td, sp_off) \
145 if ((td)->sp [-sp_off].type == STACK_TYPE_I8) \
146 ADD_CODE(td, sp_off == 1 ? MINT_CONV_I4_I8 : MINT_CONV_I4_I8_SP); \
150 handle_branch(TransformData *td, int short_op, int long_op, int offset)
152 int shorten_branch = 0;
153 int target = td->ip + offset - td->il_code;
154 if (target < 0 || target >= td->code_size)
155 g_assert_not_reached ();
156 if (offset > 0 && td->stack_height [target] < 0) {
157 td->stack_height [target] = td->sp - td->stack;
158 if (td->stack_height [target] > 0)
159 td->stack_state [target] = g_memdup (td->stack, td->stack_height [target] * sizeof (td->stack [0]));
160 td->vt_stack_size [target] = td->vt_sp;
163 offset = td->in_offsets [target] - (td->new_ip - td->new_code);
164 if (offset >= -32768) {
168 int prev = td->forward_refs [target];
169 td->forward_refs [td->ip - td->il_code] = prev;
170 td->forward_refs [target] = td->ip - td->il_code;
172 if (td->header->code_size <= 25000) /* FIX to be precise somehow? */
175 if (shorten_branch) {
176 ADD_CODE(td, short_op);
177 ADD_CODE(td, offset);
179 ADD_CODE(td, long_op);
180 ADD_CODE(td, * (unsigned short *)(&offset));
181 ADD_CODE(td, * ((unsigned short *)&offset + 1));
186 one_arg_branch(TransformData *td, int mint_op, int offset)
188 int type = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
189 int long_op = mint_op + type - STACK_TYPE_I4;
190 int short_op = long_op + MINT_BRFALSE_I4_S - MINT_BRFALSE_I4;
193 handle_branch (td, short_op, long_op, offset);
197 two_arg_branch(TransformData *td, int mint_op, int offset)
199 int type1 = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
200 int type2 = td->sp [-2].type == STACK_TYPE_O || td->sp [-2].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-2].type;
201 int long_op = mint_op + type1 - STACK_TYPE_I4;
202 int short_op = long_op + MINT_BEQ_I4_S - MINT_BEQ_I4;
204 if (type1 == STACK_TYPE_I4 && type2 == STACK_TYPE_I8) {
205 ADD_CODE(td, MINT_CONV_I8_I4);
206 td->in_offsets [td->ip - td->il_code]++;
207 } else if (type1 == STACK_TYPE_I8 && type2 == STACK_TYPE_I4) {
208 ADD_CODE(td, MINT_CONV_I8_I4_SP);
209 td->in_offsets [td->ip - td->il_code]++;
210 } else if (type1 != type2) {
211 g_warning("%s.%s: branch type mismatch %d %d",
212 td->method->klass->name, td->method->name,
213 td->sp [-1].type, td->sp [-2].type);
216 handle_branch (td, short_op, long_op, offset);
220 unary_arith_op(TransformData *td, int mint_op)
222 int op = mint_op + td->sp [-1].type - STACK_TYPE_I4;
228 binary_arith_op(TransformData *td, int mint_op)
230 int type1 = td->sp [-2].type;
231 int type2 = td->sp [-1].type;
233 #if SIZEOF_VOID_P == 8
234 if ((type1 == STACK_TYPE_MP || type1 == STACK_TYPE_I8) && type2 == STACK_TYPE_I4) {
235 ADD_CODE(td, MINT_CONV_I8_I4);
236 type2 = STACK_TYPE_I8;
238 if (type1 == STACK_TYPE_I4 && (type2 == STACK_TYPE_MP || type2 == STACK_TYPE_I8)) {
239 ADD_CODE(td, MINT_CONV_I8_I4_SP);
240 type1 = STACK_TYPE_I8;
241 td->sp [-2].type = STACK_TYPE_I8;
244 if (type1 == STACK_TYPE_MP)
245 type1 = STACK_TYPE_I;
246 if (type2 == STACK_TYPE_MP)
247 type2 = STACK_TYPE_I;
248 if (type1 != type2) {
249 g_warning("%s.%s: %04x arith type mismatch %s %d %d",
250 td->method->klass->name, td->method->name,
251 td->ip - td->il_code, mono_interp_opname[mint_op], type1, type2);
253 op = mint_op + type1 - STACK_TYPE_I4;
260 binary_int_op(TransformData *td, int mint_op)
262 int op = mint_op + td->sp [-1].type - STACK_TYPE_I4;
264 if (td->sp [-1].type != td->sp [-2].type)
265 g_warning("%s.%s: int type mismatch", td->method->klass->name, td->method->name);
271 shift_op(TransformData *td, int mint_op)
273 int op = mint_op + td->sp [-2].type - STACK_TYPE_I4;
275 if (td->sp [-1].type != STACK_TYPE_I4) {
276 g_warning("%s.%s: shift type mismatch %d",
277 td->method->klass->name, td->method->name,
285 mint_type(MonoType *type)
290 switch (type->type) {
294 case MONO_TYPE_BOOLEAN:
306 #if SIZEOF_VOID_P == 4
320 case MONO_TYPE_STRING:
321 case MONO_TYPE_SZARRAY:
322 case MONO_TYPE_CLASS:
323 case MONO_TYPE_OBJECT:
324 case MONO_TYPE_ARRAY:
326 case MONO_TYPE_VALUETYPE:
327 if (type->data.klass->enumtype) {
328 type = mono_class_enum_basetype (type->data.klass);
332 case MONO_TYPE_GENERICINST:
333 type = &type->data.generic_class->container_class->byval_arg;
336 g_warning ("got type 0x%02x", type->type);
337 g_assert_not_reached ();
343 can_store (int stack_type, int var_type)
345 if (stack_type == STACK_TYPE_O || stack_type == STACK_TYPE_MP)
346 stack_type = STACK_TYPE_I;
347 if (var_type == STACK_TYPE_O || var_type == STACK_TYPE_MP)
348 var_type = STACK_TYPE_I;
349 return stack_type == var_type;
352 #define SET_SIMPLE_TYPE(s, ty) \
359 #define SET_TYPE(s, ty, k) \
366 #define PUSH_SIMPLE_TYPE(td, ty) \
370 sp_height = (td)->sp - (td)->stack; \
371 if (sp_height > (td)->max_stack_height) \
372 (td)->max_stack_height = sp_height; \
373 SET_SIMPLE_TYPE((td)->sp - 1, ty); \
376 #define PUSH_TYPE(td, ty, k) \
380 sp_height = (td)->sp - (td)->stack; \
381 if (sp_height > (td)->max_stack_height) \
382 (td)->max_stack_height = sp_height; \
383 SET_TYPE((td)->sp - 1, ty, k); \
386 #define PUSH_VT(td, size) \
388 (td)->vt_sp += ((size) + 7) & ~7; \
389 if ((td)->vt_sp > (td)->max_vt_sp) \
390 (td)->max_vt_sp = (td)->vt_sp; \
393 #define POP_VT(td, size) \
395 (td)->vt_sp -= ((size) + 7) & ~7; \
398 #if NO_UNALIGNED_ACCESS
399 #define WRITE32(td, v) \
401 ENSURE_CODE(td, 2); \
402 * (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
403 * ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
407 #define WRITE64(td, v) \
409 ENSURE_CODE(td, 4); \
410 * (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
411 * ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
412 * ((guint16 *)((td)->new_ip) + 2) = * ((guint16 *)(v) + 2); \
413 * ((guint16 *)((td)->new_ip) + 3) = * ((guint16 *)(v) + 3); \
417 #define WRITE32(td, v) \
419 ENSURE_CODE(td, 2); \
420 * (guint32 *)((td)->new_ip) = * (guint32 *)(v); \
424 #define WRITE64(td, v) \
426 ENSURE_CODE(td, 4); \
427 * (guint64 *)((td)->new_ip) = * (guint64 *)(v); \
434 load_arg(TransformData *td, int n)
437 MonoClass *klass = NULL;
440 gboolean hasthis = mono_method_signature (td->method)->hasthis;
441 if (hasthis && n == 0)
442 type = &td->method->klass->byval_arg;
444 type = mono_method_signature (td->method)->params [hasthis ? n - 1 : n];
446 mt = mint_type (type);
447 if (mt == MINT_TYPE_VT) {
449 klass = mono_class_from_mono_type (type);
450 if (mono_method_signature (td->method)->pinvoke)
451 size = mono_class_native_size (klass, NULL);
453 size = mono_class_value_size (klass, NULL);
455 if (hasthis && n == 0) {
457 ADD_CODE (td, MINT_LDARG_P);
458 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
462 ADD_CODE (td, MINT_LDARG_VT);
463 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
467 if (hasthis && n == 0) {
469 ADD_CODE (td, MINT_LDARG_P);
470 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
473 ADD_CODE(td, MINT_LDARG_I1 + (mt - MINT_TYPE_I1));
474 ADD_CODE(td, td->rtm->arg_offsets [n]); /* FIX for large offset */
475 if (mt == MINT_TYPE_O)
476 klass = mono_class_from_mono_type (type);
479 PUSH_TYPE(td, stack_type[mt], klass);
483 store_arg(TransformData *td, int n)
489 gboolean hasthis = mono_method_signature (td->method)->hasthis;
490 if (hasthis && n == 0)
491 type = &td->method->klass->byval_arg;
493 type = mono_method_signature (td->method)->params [n - !!hasthis];
495 mt = mint_type (type);
496 if (mt == MINT_TYPE_VT) {
498 g_error ("data.klass");
499 if (mono_method_signature (td->method)->pinvoke)
500 size = mono_class_native_size (type->data.klass, NULL);
502 size = mono_class_value_size (type->data.klass, NULL);
503 ADD_CODE(td, MINT_STARG_VT);
506 if (td->sp [-1].type == STACK_TYPE_VT)
509 ADD_CODE(td, MINT_STARG_I1 + (mt - MINT_TYPE_I1));
510 ADD_CODE(td, td->rtm->arg_offsets [n]);
516 store_inarg(TransformData *td, int n)
519 gboolean hasthis = mono_method_signature (td->method)->hasthis;
520 if (hasthis && n == 0)
521 type = &td->method->klass->byval_arg;
523 type = mono_method_signature (td->method)->params [n - !!hasthis];
525 int mt = mint_type (type);
526 if (hasthis && n == 0) {
527 ADD_CODE (td, MINT_STINARG_P);
531 if (mt == MINT_TYPE_VT) {
532 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_STINARG_VT);
542 ADD_CODE(td, MINT_STINARG_I1 + (mt - MINT_TYPE_I1));
548 load_local(TransformData *td, int n)
550 MonoType *type = td->header->locals [n];
551 int mt = mint_type (type);
552 int offset = td->rtm->local_offsets [n];
553 MonoClass *klass = NULL;
554 if (mt == MINT_TYPE_VT) {
555 klass = mono_class_from_mono_type (type);
556 gint32 size = mono_class_value_size (klass, NULL);
558 ADD_CODE(td, MINT_LDLOC_VT);
559 ADD_CODE(td, offset); /*FIX for large offset */
562 g_assert (mt < MINT_TYPE_VT);
563 if (mt == MINT_TYPE_I4 && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
564 td->last_new_ip [0] == MINT_STLOC_I4 && td->last_new_ip [1] == offset) {
565 td->last_new_ip [0] = MINT_STLOC_NP_I4;
566 } else if (mt == MINT_TYPE_O && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
567 td->last_new_ip [0] == MINT_STLOC_O && td->last_new_ip [1] == offset) {
568 td->last_new_ip [0] = MINT_STLOC_NP_O;
570 ADD_CODE(td, MINT_LDLOC_I1 + (mt - MINT_TYPE_I1));
571 ADD_CODE(td, offset); /*FIX for large offset */
573 if (mt == MINT_TYPE_O)
574 klass = mono_class_from_mono_type (type);
576 PUSH_TYPE(td, stack_type[mt], klass);
580 store_local(TransformData *td, int n)
582 MonoType *type = td->header->locals [n];
583 int mt = mint_type (type);
584 int offset = td->rtm->local_offsets [n];
586 #if SIZEOF_VOID_P == 8
587 if (td->sp [-1].type == STACK_TYPE_I4 && stack_type [mt] == STACK_TYPE_I8) {
588 ADD_CODE(td, MINT_CONV_I8_I4);
589 td->sp [-1].type = STACK_TYPE_I8;
592 if (!can_store(td->sp [-1].type, stack_type [mt])) {
593 g_warning("%s.%s: Store local stack type mismatch %d %d",
594 td->method->klass->name, td->method->name,
595 stack_type [mt], td->sp [-1].type);
597 if (mt == MINT_TYPE_VT) {
598 MonoClass *klass = mono_class_from_mono_type (type);
599 gint32 size = mono_class_value_size (klass, NULL);
600 ADD_CODE(td, MINT_STLOC_VT);
601 ADD_CODE(td, offset); /*FIX for large offset */
603 if (td->sp [-1].type == STACK_TYPE_VT)
606 g_assert (mt < MINT_TYPE_VT);
607 ADD_CODE(td, MINT_STLOC_I1 + (mt - MINT_TYPE_I1));
608 ADD_CODE(td, offset); /*FIX for large offset */
613 #define SIMPLE_OP(td, op) \
620 get_data_item_index (TransformData *td, void *ptr)
622 gpointer p = g_hash_table_lookup (td->data_hash, ptr);
625 return GPOINTER_TO_UINT (p) - 1;
626 if (td->max_data_items == td->n_data_items) {
627 td->max_data_items = td->n_data_items == 0 ? 16 : 2 * td->max_data_items;
628 td->data_items = g_realloc (td->data_items, td->max_data_items * sizeof(td->data_items [0]));
630 index = td->n_data_items;
631 td->data_items [index] = ptr;
633 g_hash_table_insert (td->data_hash, ptr, GUINT_TO_POINTER (index + 1));
638 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)
640 MonoImage *image = method->klass->image;
641 MonoMethodSignature *csignature;
643 int virtual = *td->ip == CEE_CALLVIRT;
644 int calli = *td->ip == CEE_CALLI || *td->ip == CEE_MONO_CALLI_EXTRA_ARG;
646 guint32 vt_stack_used = 0;
647 guint32 vt_res_size = 0;
652 guint32 token = read32 (td->ip + 1);
654 if (target_method == NULL) {
657 native = (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE && td->sp [-1].type == STACK_TYPE_I);
659 if (method->wrapper_type != MONO_WRAPPER_NONE)
660 csignature = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
662 csignature = mono_metadata_parse_signature (image, token);
663 target_method = NULL;
665 if (method->wrapper_type == MONO_WRAPPER_NONE)
666 target_method = mono_get_method_full (image, token, NULL, generic_context);
668 target_method = (MonoMethod *)mono_method_get_wrapper_data (method, token);
669 csignature = mono_method_signature (target_method);
670 if (target_method->klass == mono_defaults.string_class) {
671 if (target_method->name [0] == 'g') {
672 if (strcmp (target_method->name, "get_Chars") == 0)
674 else if (strcmp (target_method->name, "get_Length") == 0)
677 } else if (target_method->klass == mono_defaults.array_class) {
678 if (strcmp (target_method->name, "get_Rank") == 0)
679 op = MINT_ARRAY_RANK;
680 else if (strcmp (target_method->name, "get_Length") == 0)
685 csignature = mono_method_signature (target_method);
688 if (constrained_class) {
689 if (constrained_class->enumtype && !strcmp (target_method->name, "GetHashCode")) {
690 /* Use the corresponding method from the base type to avoid boxing */
691 MonoType *base_type = mono_class_enum_basetype (constrained_class);
692 g_assert (base_type);
693 constrained_class = mono_class_from_mono_type (base_type);
694 target_method = mono_class_get_method_from_name (constrained_class, target_method->name, 0);
695 g_assert (target_method);
699 if (constrained_class) {
700 mono_class_setup_vtable (constrained_class);
702 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);
704 target_method = mono_get_method_constrained_with_method (image, target_method, constrained_class, generic_context, &error);
706 g_print (" : %s::%s. %s (%p)\n", target_method->klass->name, target_method->name, mono_signature_full_name (target_method->signature), target_method);
708 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
709 mono_class_setup_vtable (target_method->klass);
711 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)) {
712 ADD_CODE (td, MINT_BOX);
713 ADD_CODE (td, get_data_item_index (td, constrained_class));
714 ADD_CODE (td, csignature->param_count);
715 } else if (!constrained_class->valuetype) {
716 /* managed pointer on the stack, we need to deref that puppy */
717 ADD_CODE (td, MINT_LDIND_I);
718 ADD_CODE (td, csignature->param_count);
720 g_assert (target_method->klass->valuetype);
726 mono_class_init (target_method->klass);
728 CHECK_STACK (td, csignature->param_count + csignature->hasthis);
729 if (!calli && (!virtual || (target_method->flags & METHOD_ATTRIBUTE_VIRTUAL) == 0) &&
730 (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
731 (target_method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) == 0) {
732 int called_inited = mono_class_vtable (domain, target_method->klass)->initialized;
733 MonoMethodHeader *mheader = mono_method_get_header (target_method);
735 if (/*mono_metadata_signature_equal (method->signature, target_method->signature) */ method == target_method && *(td->ip + 5) == CEE_RET) {
737 if (mono_interp_traceopt)
738 g_print ("Optimize tail call of %s.%s\n", target_method->klass->name, target_method->name);
739 for (i = csignature->param_count - 1; i >= 0; --i)
740 store_arg (td, i + csignature->hasthis);
742 if (csignature->hasthis) {
743 g_error ("STTHIS removal");
744 // ADD_CODE(td, MINT_STTHIS);
747 ADD_CODE(td, MINT_BR_S);
748 offset = body_start_offset - ((td->new_ip - 1) - td->new_code);
749 ADD_CODE(td, offset);
750 if (!is_bb_start [td->ip + 5 - td->il_code])
751 ++td->ip; /* gobble the CEE_RET if it isn't branched to */
755 /* mheader might not exist if this is a delegate invoc, etc */
756 if (mheader && *mheader->code == CEE_RET && called_inited) {
757 if (mono_interp_traceopt)
758 g_print ("Inline (empty) call of %s.%s\n", target_method->klass->name, target_method->name);
759 for (i = 0; i < csignature->param_count; i++)
760 ADD_CODE(td, MINT_POP); /*FIX: vt */
761 if (csignature->hasthis) {
763 ADD_CODE(td, MINT_CKNULL);
764 ADD_CODE(td, MINT_POP);
766 td->sp -= csignature->param_count + csignature->hasthis;
772 if (method->wrapper_type == MONO_WRAPPER_NONE && target_method != NULL) {
773 if (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
774 target_method = mono_marshal_get_native_wrapper (target_method, FALSE, FALSE);
775 if (!virtual && target_method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
776 target_method = mono_marshal_get_synchronized_wrapper (target_method);
778 g_assert (csignature->call_convention == MONO_CALL_DEFAULT || csignature->call_convention == MONO_CALL_C);
779 td->sp -= csignature->param_count + !!csignature->hasthis;
780 for (i = 0; i < csignature->param_count; ++i) {
781 if (td->sp [i + !!csignature->hasthis].type == STACK_TYPE_VT) {
783 MonoClass *klass = mono_class_from_mono_type (csignature->params [i]);
784 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
785 size = mono_class_native_size (klass, NULL);
787 size = mono_class_value_size (klass, NULL);
788 size = (size + 7) & ~7;
789 vt_stack_used += size;
793 /* need to handle typedbyref ... */
794 if (csignature->ret->type != MONO_TYPE_VOID) {
795 int mt = mint_type(csignature->ret);
796 MonoClass *klass = mono_class_from_mono_type (csignature->ret);
797 if (mt == MINT_TYPE_VT) {
798 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
799 vt_res_size = mono_class_native_size (klass, NULL);
801 vt_res_size = mono_class_value_size (klass, NULL);
802 PUSH_VT(td, vt_res_size);
804 PUSH_TYPE(td, stack_type[mt], klass);
810 #if SIZEOF_VOID_P == 8
811 if (op == MINT_LDLEN)
812 ADD_CODE(td, MINT_CONV_I4_I8);
816 ADD_CODE(td, native ? MINT_CALLI_NAT : MINT_CALLI);
818 ADD_CODE(td, is_void ? MINT_VCALLVIRT : MINT_CALLVIRT);
820 ADD_CODE(td, is_void ? MINT_VCALL : MINT_CALL);
823 ADD_CODE(td, get_data_item_index (td, (void *)csignature));
825 ADD_CODE(td, get_data_item_index (td, (void *)mono_interp_get_runtime_method (domain, target_method, &error)));
826 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
830 if (vt_stack_used != 0 || vt_res_size != 0) {
831 ADD_CODE(td, MINT_VTRESULT);
832 ADD_CODE(td, vt_res_size);
833 WRITE32(td, &vt_stack_used);
834 td->vt_sp -= vt_stack_used;
839 generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start)
841 MonoMethodHeader *header = mono_method_get_header (method);
842 MonoMethodSignature *signature = mono_method_signature (method);
843 MonoImage *image = method->klass->image;
844 MonoDomain *domain = mono_domain_get ();
845 MonoGenericContext *generic_context = NULL;
846 MonoClass *constrained_class = NULL;
852 MonoClassField *field;
853 const unsigned char *end;
854 int new_in_start_offset;
855 int body_start_offset;
859 int generating_code = 1;
861 if (mono_method_signature (method)->is_inflated)
862 generic_context = &((MonoMethodInflated *) method)->context;
864 memset(&td, 0, sizeof(td));
867 td.is_bb_start = is_bb_start;
868 td.il_code = header->code;
869 td.code_size = header->code_size;
871 td.max_code_size = td.code_size;
872 td.new_code = (unsigned short *)g_malloc(td.max_code_size * sizeof(gushort));
873 td.new_code_end = td.new_code + td.max_code_size;
874 td.in_offsets = g_malloc0(header->code_size * sizeof(int));
875 td.forward_refs = g_malloc(header->code_size * sizeof(int));
876 td.stack_state = g_malloc0(header->code_size * sizeof(StackInfo *));
877 td.stack_height = g_malloc(header->code_size * sizeof(int));
878 td.vt_stack_size = g_malloc(header->code_size * sizeof(int));
880 td.max_data_items = 0;
881 td.data_items = NULL;
882 td.data_hash = g_hash_table_new (NULL, NULL);
883 rtm->data_items = td.data_items;
884 for (i = 0; i < header->code_size; i++) {
885 td.forward_refs [i] = -1;
886 td.stack_height [i] = -1;
888 td.new_ip = td.new_code;
889 td.last_new_ip = NULL;
891 td.stack = g_malloc0(header->max_stack * sizeof(td.stack[0]));
893 td.max_stack_height = 0;
895 for (i = 0; i < header->num_clauses; i++) {
896 MonoExceptionClause *c = header->clauses + i;
897 td.stack_height [c->handler_offset] = 0;
898 td.vt_stack_size [c->handler_offset] = 0;
899 td.is_bb_start [c->handler_offset] = 1;
900 if (c->flags == MONO_EXCEPTION_CLAUSE_NONE) {
901 td.stack_height [c->handler_offset] = 1;
902 td.stack_state [c->handler_offset] = g_malloc0(sizeof(StackInfo));
903 td.stack_state [c->handler_offset][0].type = STACK_TYPE_O;
904 td.stack_state [c->handler_offset][0].klass = NULL; /*FIX*/
908 td.ip = header->code;
909 end = td.ip + header->code_size;
911 if (mono_interp_traceopt) {
912 char *tmp = mono_disasm_code (NULL, method, td.ip, end);
913 char *name = mono_method_full_name (method, TRUE);
914 g_print ("Method %s, original code:\n", name);
915 g_print ("%s\n", tmp);
920 if (signature->hasthis)
921 store_inarg (&td, 0);
922 for (i = 0; i < signature->param_count; i++)
923 store_inarg (&td, i + !!signature->hasthis);
925 body_start_offset = td.new_ip - td.new_code;
927 for (i = 0; i < header->num_locals; i++) {
928 int mt = mint_type(header->locals [i]);
929 if (mt == MINT_TYPE_VT || mt == MINT_TYPE_O) {
930 ADD_CODE(&td, MINT_INITLOCALS);
935 while (td.ip < end) {
938 g_assert (td.sp >= td.stack);
939 g_assert (td.vt_sp < 0x10000000);
940 in_offset = td.ip - header->code;
941 td.in_offsets [in_offset] = td.new_ip - td.new_code;
942 new_in_start_offset = td.new_ip - td.new_code;
944 while (td.forward_refs [in_offset] >= 0) {
945 int j = td.forward_refs [in_offset];
947 td.forward_refs [in_offset] = td.forward_refs [j];
948 if (td.in_offsets [j] < 0) {
949 int old_switch_offset = -td.in_offsets [j];
950 int new_switch_offset = td.in_offsets [old_switch_offset];
951 int switch_case = (j - old_switch_offset - 5) / 4;
952 int n_cases = read32 (header->code + old_switch_offset + 1);
953 offset = (td.new_ip - td.new_code) - (new_switch_offset + 2 * n_cases + 3);
954 slot = new_switch_offset + 3 + 2 * switch_case;
955 td.new_code [slot] = * (unsigned short *)(&offset);
956 td.new_code [slot + 1] = * ((unsigned short *)&offset + 1);
958 int op = td.new_code [td.in_offsets [j]];
959 if (mono_interp_opargtype [op] == MintOpShortBranch) {
960 offset = (td.new_ip - td.new_code) - td.in_offsets [j];
961 g_assert (offset <= 32767);
962 slot = td.in_offsets [j] + 1;
963 td.new_code [slot] = offset;
965 offset = (td.new_ip - td.new_code) - td.in_offsets [j];
966 slot = td.in_offsets [j] + 1;
967 td.new_code [slot] = * (unsigned short *)(&offset);
968 td.new_code [slot + 1] = * ((unsigned short *)&offset + 1);
972 if (td.stack_height [in_offset] >= 0) {
973 g_assert (is_bb_start [in_offset]);
974 if (td.stack_height [in_offset] > 0)
975 memcpy (td.stack, td.stack_state [in_offset], td.stack_height [in_offset] * sizeof(td.stack [0]));
976 td.sp = td.stack + td.stack_height [in_offset];
977 td.vt_sp = td.vt_stack_size [in_offset];
979 if (is_bb_start [in_offset]) {
982 if (!generating_code) {
983 while (td.ip < end && !is_bb_start [td.ip - td.il_code])
987 if (mono_interp_traceopt > 1) {
988 printf("IL_%04lx %s %-10s -> IL_%04lx, sp %ld, %s %-12s vt_sp %u (max %u)\n",
990 td.is_bb_start [td.ip - td.il_code] == 3 ? "<>" :
991 td.is_bb_start [td.ip - td.il_code] == 2 ? "< " :
992 td.is_bb_start [td.ip - td.il_code] == 1 ? " >" : " ",
993 mono_opcode_name (*td.ip), td.new_ip - td.new_code, td.sp - td.stack,
994 td.sp > td.stack ? stack_type_string [td.sp [-1].type] : " ",
995 (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) : "",
996 td.vt_sp, td.max_vt_sp);
1004 SIMPLE_OP(td, MINT_BREAK);
1010 load_arg (&td, *td.ip - CEE_LDARG_0);
1017 load_local (&td, *td.ip - CEE_LDLOC_0);
1024 store_local (&td, *td.ip - CEE_STLOC_0);
1028 load_arg (&td, ((guint8 *)td.ip)[1]);
1031 case CEE_LDARGA_S: {
1032 /* NOTE: n includes this */
1033 int n = ((guint8 *)td.ip)[1];
1034 if (n == 0 && signature->hasthis) {
1035 g_error ("LDTHISA: NOPE");
1036 ADD_CODE(&td, MINT_LDTHISA);
1039 ADD_CODE(&td, MINT_LDARGA);
1040 ADD_CODE(&td, td.rtm->arg_offsets [n]);
1042 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
1047 store_arg (&td, ((guint8 *)td.ip)[1]);
1051 load_local (&td, ((guint8 *)td.ip)[1]);
1055 ADD_CODE(&td, MINT_LDLOCA_S);
1056 ADD_CODE(&td, td.rtm->local_offsets [((guint8 *)td.ip)[1]]);
1057 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
1061 store_local (&td, ((guint8 *)td.ip)[1]);
1065 SIMPLE_OP(td, MINT_LDNULL);
1066 PUSH_TYPE(&td, STACK_TYPE_O, NULL);
1069 SIMPLE_OP(td, MINT_LDC_I4_M1);
1070 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1073 if (!td.is_bb_start[td.ip + 1 - td.il_code] && td.ip [1] == 0xfe && td.ip [2] == CEE_CEQ &&
1074 td.sp > td.stack && td.sp [-1].type == STACK_TYPE_I4) {
1075 SIMPLE_OP(td, MINT_CEQ0_I4);
1078 SIMPLE_OP(td, MINT_LDC_I4_0);
1079 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1083 if (!td.is_bb_start[td.ip + 1 - td.il_code] &&
1084 (td.ip [1] == CEE_ADD || td.ip [1] == CEE_SUB) && td.sp [-1].type == STACK_TYPE_I4) {
1085 ADD_CODE(&td, td.ip [1] == CEE_ADD ? MINT_ADD1_I4 : MINT_SUB1_I4);
1088 SIMPLE_OP(td, MINT_LDC_I4_1);
1089 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1099 SIMPLE_OP(td, (*td.ip - CEE_LDC_I4_0) + MINT_LDC_I4_0);
1100 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1103 ADD_CODE(&td, MINT_LDC_I4_S);
1104 ADD_CODE(&td, ((gint8 *) td.ip) [1]);
1106 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1109 i32 = read32 (td.ip + 1);
1110 ADD_CODE(&td, MINT_LDC_I4);
1113 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1116 gint64 val = read64 (td.ip + 1);
1117 ADD_CODE(&td, MINT_LDC_I8);
1120 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I8);
1125 readr4 (td.ip + 1, &val);
1126 ADD_CODE(&td, MINT_LDC_R4);
1129 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_R8);
1134 readr8 (td.ip + 1, &val);
1135 ADD_CODE(&td, MINT_LDC_R8);
1138 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_R8);
1142 int type = td.sp [-1].type;
1143 MonoClass *klass = td.sp [-1].klass;
1144 if (td.sp [-1].type == STACK_TYPE_VT) {
1145 gint32 size = mono_class_value_size (klass, NULL);
1147 ADD_CODE(&td, MINT_DUP_VT);
1148 WRITE32(&td, &size);
1151 SIMPLE_OP(td, MINT_DUP);
1152 PUSH_TYPE(&td, type, klass);
1156 CHECK_STACK(&td, 1);
1157 SIMPLE_OP(td, MINT_POP);
1158 if (td.sp [-1].type == STACK_TYPE_VT) {
1159 int size = mono_class_value_size (td.sp [-1].klass, NULL);
1160 size = (size + 7) & ~7;
1161 ADD_CODE(&td, MINT_VTRESULT);
1163 WRITE32(&td, &size);
1170 if (td.sp > td.stack)
1171 g_warning ("CEE_JMP: stack must be empty");
1172 token = read32 (td.ip + 1);
1173 m = mono_get_method_full (image, token, NULL, generic_context);
1174 ADD_CODE (&td, MINT_JMP);
1175 ADD_CODE (&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
1176 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1180 case CEE_CALLVIRT: /* Fall through */
1181 case CEE_CALLI: /* Fall through */
1183 interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, constrained_class);
1184 constrained_class = NULL;
1189 if (signature->ret->type != MONO_TYPE_VOID) {
1191 MonoClass *klass = mono_class_from_mono_type (signature->ret);
1192 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1193 vt_size = mono_class_value_size (klass, NULL);
1194 vt_size = (vt_size + 7) & ~7;
1197 if (td.sp > td.stack)
1198 g_warning ("%s.%s: CEE_RET: more values on stack: %d", td.method->klass->name, td.method->name, td.sp - td.stack);
1199 if (td.vt_sp != vt_size)
1200 g_error ("%s.%s: CEE_RET: value type stack: %d vs. %d", td.method->klass->name, td.method->name, td.vt_sp, vt_size);
1202 SIMPLE_OP(td, signature->ret->type == MONO_TYPE_VOID ? MINT_RET_VOID : MINT_RET);
1204 ADD_CODE(&td, MINT_RET_VT);
1205 WRITE32(&td, &vt_size);
1208 generating_code = 0;
1212 handle_branch (&td, MINT_BR_S, MINT_BR, 5 + read32 (td.ip + 1));
1214 generating_code = 0;
1217 handle_branch (&td, MINT_BR_S, MINT_BR, 2 + (gint8)td.ip [1]);
1219 generating_code = 0;
1222 one_arg_branch (&td, MINT_BRFALSE_I4, 5 + read32 (td.ip + 1));
1226 one_arg_branch (&td, MINT_BRFALSE_I4, 2 + (gint8)td.ip [1]);
1230 one_arg_branch (&td, MINT_BRTRUE_I4, 5 + read32 (td.ip + 1));
1234 one_arg_branch (&td, MINT_BRTRUE_I4, 2 + (gint8)td.ip [1]);
1238 two_arg_branch (&td, MINT_BEQ_I4, 5 + read32 (td.ip + 1));
1242 two_arg_branch (&td, MINT_BEQ_I4, 2 + (gint8) td.ip [1]);
1246 two_arg_branch (&td, MINT_BGE_I4, 5 + read32 (td.ip + 1));
1250 two_arg_branch (&td, MINT_BGE_I4, 2 + (gint8) td.ip [1]);
1254 two_arg_branch (&td, MINT_BGT_I4, 5 + read32 (td.ip + 1));
1258 two_arg_branch (&td, MINT_BGT_I4, 2 + (gint8) td.ip [1]);
1262 two_arg_branch (&td, MINT_BLT_I4, 5 + read32 (td.ip + 1));
1266 two_arg_branch (&td, MINT_BLT_I4, 2 + (gint8) td.ip [1]);
1270 two_arg_branch (&td, MINT_BLE_I4, 5 + read32 (td.ip + 1));
1274 two_arg_branch (&td, MINT_BLE_I4, 2 + (gint8) td.ip [1]);
1278 two_arg_branch (&td, MINT_BNE_UN_I4, 5 + read32 (td.ip + 1));
1282 two_arg_branch (&td, MINT_BNE_UN_I4, 2 + (gint8) td.ip [1]);
1286 two_arg_branch (&td, MINT_BGE_UN_I4, 5 + read32 (td.ip + 1));
1290 two_arg_branch (&td, MINT_BGE_UN_I4, 2 + (gint8) td.ip [1]);
1294 two_arg_branch (&td, MINT_BGT_UN_I4, 5 + read32 (td.ip + 1));
1298 two_arg_branch (&td, MINT_BGT_UN_I4, 2 + (gint8) td.ip [1]);
1302 two_arg_branch (&td, MINT_BLE_UN_I4, 5 + read32 (td.ip + 1));
1306 two_arg_branch (&td, MINT_BLE_UN_I4, 2 + (gint8) td.ip [1]);
1310 two_arg_branch (&td, MINT_BLT_UN_I4, 5 + read32 (td.ip + 1));
1314 two_arg_branch (&td, MINT_BLT_UN_I4, 2 + (gint8) td.ip [1]);
1319 const unsigned char *next_ip;
1320 const unsigned char *base_ip = td.ip;
1321 unsigned short *next_new_ip;
1324 ADD_CODE(&td, MINT_SWITCH);
1325 ADD_CODE(&td, * (unsigned short *)(&n));
1326 ADD_CODE(&td, * ((unsigned short *)&n + 1));
1328 next_ip = td.ip + n * 4;
1329 next_new_ip = td.new_ip + n * 2;
1330 for (i = 0; i < n; i++) {
1331 offset = read32 (td.ip);
1332 target = next_ip - td.il_code + offset;
1334 target = td.in_offsets [target] - (next_new_ip - td.new_code);
1336 int prev = td.forward_refs [target];
1337 td.forward_refs [td.ip - td.il_code] = prev;
1338 td.forward_refs [target] = td.ip - td.il_code;
1339 td.in_offsets [td.ip - td.il_code] = - (base_ip - td.il_code);
1341 ADD_CODE(&td, * (unsigned short *)(&target));
1342 ADD_CODE(&td, * ((unsigned short *)&target + 1));
1349 CHECK_STACK (&td, 1);
1350 SIMPLE_OP (td, MINT_LDIND_I1);
1351 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1354 CHECK_STACK (&td, 1);
1355 SIMPLE_OP (td, MINT_LDIND_U1);
1356 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1359 CHECK_STACK (&td, 1);
1360 SIMPLE_OP (td, MINT_LDIND_I2);
1361 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1364 CHECK_STACK (&td, 1);
1365 SIMPLE_OP (td, MINT_LDIND_U2);
1366 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1369 CHECK_STACK (&td, 1);
1370 SIMPLE_OP (td, MINT_LDIND_I4);
1371 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1374 CHECK_STACK (&td, 1);
1375 SIMPLE_OP (td, MINT_LDIND_U4);
1376 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1379 CHECK_STACK (&td, 1);
1380 SIMPLE_OP (td, MINT_LDIND_I8);
1381 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1384 CHECK_STACK (&td, 1);
1385 SIMPLE_OP (td, MINT_LDIND_I);
1387 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1390 CHECK_STACK (&td, 1);
1391 SIMPLE_OP (td, MINT_LDIND_R4);
1392 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1395 CHECK_STACK (&td, 1);
1396 SIMPLE_OP (td, MINT_LDIND_R8);
1397 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1400 CHECK_STACK (&td, 1);
1401 SIMPLE_OP (td, MINT_LDIND_REF);
1402 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
1405 CHECK_STACK (&td, 2);
1406 SIMPLE_OP (td, MINT_STIND_REF);
1410 CHECK_STACK (&td, 2);
1411 SIMPLE_OP (td, MINT_STIND_I1);
1415 CHECK_STACK (&td, 2);
1416 SIMPLE_OP (td, MINT_STIND_I2);
1420 CHECK_STACK (&td, 2);
1421 SIMPLE_OP (td, MINT_STIND_I4);
1425 CHECK_STACK (&td, 2);
1426 SIMPLE_OP (td, MINT_STIND_I);
1430 CHECK_STACK (&td, 2);
1431 SIMPLE_OP (td, MINT_STIND_I8);
1435 CHECK_STACK (&td, 2);
1436 SIMPLE_OP (td, MINT_STIND_R4);
1440 CHECK_STACK (&td, 2);
1441 SIMPLE_OP (td, MINT_STIND_R8);
1445 binary_arith_op(&td, MINT_ADD_I4);
1449 binary_arith_op(&td, MINT_SUB_I4);
1453 binary_arith_op(&td, MINT_MUL_I4);
1457 binary_arith_op(&td, MINT_DIV_I4);
1461 binary_arith_op(&td, MINT_DIV_UN_I4);
1465 binary_int_op (&td, MINT_REM_I4);
1469 binary_int_op (&td, MINT_REM_UN_I4);
1473 binary_int_op (&td, MINT_AND_I4);
1477 binary_int_op (&td, MINT_OR_I4);
1481 binary_int_op (&td, MINT_XOR_I4);
1485 shift_op (&td, MINT_SHL_I4);
1489 shift_op (&td, MINT_SHR_I4);
1493 shift_op (&td, MINT_SHR_UN_I4);
1497 unary_arith_op (&td, MINT_NEG_I4);
1501 unary_arith_op (&td, MINT_NOT_I4);
1505 CHECK_STACK (&td, 1);
1506 switch (td.sp [-1].type) {
1508 ADD_CODE(&td, MINT_CONV_U1_R8);
1511 ADD_CODE(&td, MINT_CONV_U1_I4);
1514 ADD_CODE(&td, MINT_CONV_U1_I8);
1517 g_assert_not_reached ();
1520 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1523 CHECK_STACK (&td, 1);
1524 switch (td.sp [-1].type) {
1526 ADD_CODE(&td, MINT_CONV_I1_R8);
1529 ADD_CODE(&td, MINT_CONV_I1_I4);
1532 ADD_CODE(&td, MINT_CONV_I1_I8);
1535 g_assert_not_reached ();
1538 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1541 CHECK_STACK (&td, 1);
1542 switch (td.sp [-1].type) {
1544 ADD_CODE(&td, MINT_CONV_U2_R8);
1547 ADD_CODE(&td, MINT_CONV_U2_I4);
1550 ADD_CODE(&td, MINT_CONV_U2_I8);
1553 g_assert_not_reached ();
1556 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1559 CHECK_STACK (&td, 1);
1560 switch (td.sp [-1].type) {
1562 ADD_CODE(&td, MINT_CONV_I2_R8);
1565 ADD_CODE(&td, MINT_CONV_I2_I4);
1568 ADD_CODE(&td, MINT_CONV_I2_I8);
1571 g_assert_not_reached ();
1574 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1577 CHECK_STACK (&td, 1);
1578 switch (td.sp [-1].type) {
1580 #if SIZEOF_VOID_P == 4
1581 ADD_CODE(&td, MINT_CONV_U4_R8);
1583 ADD_CODE(&td, MINT_CONV_U8_R8);
1587 #if SIZEOF_VOID_P == 8
1588 ADD_CODE(&td, MINT_CONV_U8_I4);
1592 #if SIZEOF_VOID_P == 4
1593 ADD_CODE(&td, MINT_CONV_U4_I8);
1599 g_assert_not_reached ();
1602 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1605 CHECK_STACK (&td, 1);
1606 switch (td.sp [-1].type) {
1608 #if SIZEOF_VOID_P == 8
1609 ADD_CODE(&td, MINT_CONV_I8_R8);
1611 ADD_CODE(&td, MINT_CONV_I4_R8);
1615 #if SIZEOF_VOID_P == 8
1616 ADD_CODE(&td, MINT_CONV_I8_I4);
1624 #if SIZEOF_VOID_P == 4
1625 ADD_CODE(&td, MINT_CONV_I4_I8);
1629 g_assert_not_reached ();
1632 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1635 CHECK_STACK (&td, 1);
1636 switch (td.sp [-1].type) {
1638 ADD_CODE(&td, MINT_CONV_U4_R8);
1643 ADD_CODE(&td, MINT_CONV_U4_I8);
1646 #if SIZEOF_VOID_P == 8
1647 ADD_CODE(&td, MINT_CONV_U4_I8);
1651 g_assert_not_reached ();
1654 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1657 CHECK_STACK (&td, 1);
1658 switch (td.sp [-1].type) {
1660 ADD_CODE(&td, MINT_CONV_I4_R8);
1665 ADD_CODE(&td, MINT_CONV_I4_I8);
1668 #if SIZEOF_VOID_P == 8
1669 ADD_CODE(&td, MINT_CONV_I4_I8);
1673 g_assert_not_reached ();
1676 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1679 CHECK_STACK (&td, 1);
1680 switch (td.sp [-1].type) {
1682 ADD_CODE(&td, MINT_CONV_I8_R8);
1685 ADD_CODE(&td, MINT_CONV_I8_I4);
1690 #if SIZEOF_VOID_P == 4
1691 ADD_CODE(&td, MINT_CONV_I8_I4);
1695 g_assert_not_reached ();
1698 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1701 CHECK_STACK (&td, 1);
1702 switch (td.sp [-1].type) {
1704 ADD_CODE(&td, MINT_CONV_R4_R8);
1707 ADD_CODE(&td, MINT_CONV_R4_I8);
1710 ADD_CODE(&td, MINT_CONV_R4_I4);
1713 g_assert_not_reached ();
1716 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1719 CHECK_STACK (&td, 1);
1720 switch (td.sp [-1].type) {
1722 ADD_CODE(&td, MINT_CONV_R8_I4);
1725 ADD_CODE(&td, MINT_CONV_R8_I8);
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_U8_I4);
1744 ADD_CODE(&td, MINT_CONV_U8_R8);
1747 #if SIZEOF_VOID_P == 4
1748 ADD_CODE(&td, MINT_CONV_U8_I4);
1752 g_assert_not_reached ();
1755 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1761 vtklass = mono_class_get_full (image, read32 (ip), generic_context);
1764 memcpy (sp [0].data.p, sp [1].data.p, mono_class_value_size (vtklass, NULL));
1770 CHECK_STACK (&td, 1);
1772 token = read32 (td.ip + 1);
1774 if (method->wrapper_type != MONO_WRAPPER_NONE)
1775 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
1777 klass = mono_class_get_full (image, token, generic_context);
1779 ADD_CODE(&td, MINT_LDOBJ);
1780 ADD_CODE(&td, get_data_item_index(&td, klass));
1781 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1782 size = mono_class_value_size (klass, NULL);
1786 SET_TYPE(td.sp - 1, stack_type[mint_type(&klass->byval_arg)], klass);
1791 token = mono_metadata_token_index (read32 (td.ip + 1));
1793 if (method->wrapper_type != MONO_WRAPPER_NONE) {
1794 s = mono_string_new_wrapper(
1795 mono_method_get_wrapper_data (method, token));
1798 s = mono_ldstr (domain, image, token);
1799 ADD_CODE(&td, MINT_LDSTR);
1800 ADD_CODE(&td, get_data_item_index (&td, s));
1801 PUSH_TYPE(&td, STACK_TYPE_O, mono_defaults.string_class);
1806 MonoMethodSignature *csignature;
1807 guint32 vt_stack_used = 0;
1808 guint32 vt_res_size = 0;
1811 token = read32 (td.ip);
1814 if (method->wrapper_type != MONO_WRAPPER_NONE)
1815 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
1817 m = mono_get_method_full (image, token, NULL, generic_context);
1819 csignature = mono_method_signature (m);
1821 td.sp -= csignature->param_count;
1822 ADD_CODE(&td, MINT_NEWOBJ);
1823 ADD_CODE(&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
1824 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1826 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1827 vt_res_size = mono_class_value_size (klass, NULL);
1828 PUSH_VT (&td, vt_res_size);
1830 for (i = 0; i < csignature->param_count; ++i) {
1831 int mt = mint_type(csignature->params [i]);
1832 if (mt == MINT_TYPE_VT) {
1833 MonoClass *k = mono_class_from_mono_type (csignature->params [i]);
1834 gint32 size = mono_class_value_size (k, NULL);
1835 size = (size + 7) & ~7;
1836 vt_stack_used += size;
1839 if (vt_stack_used != 0 || vt_res_size != 0) {
1840 ADD_CODE(&td, MINT_VTRESULT);
1841 ADD_CODE(&td, vt_res_size);
1842 WRITE32(&td, &vt_stack_used);
1843 td.vt_sp -= vt_stack_used;
1845 PUSH_TYPE (&td, stack_type [mint_type (&klass->byval_arg)], klass);
1849 CHECK_STACK (&td, 1);
1850 token = read32 (td.ip + 1);
1851 klass = mono_class_get_full (image, token, generic_context);
1852 ADD_CODE(&td, MINT_CASTCLASS);
1853 ADD_CODE(&td, get_data_item_index (&td, klass));
1854 td.sp [-1].klass = klass;
1858 CHECK_STACK (&td, 1);
1859 token = read32 (td.ip + 1);
1860 klass = mono_class_get_full (image, token, generic_context);
1861 ADD_CODE(&td, MINT_ISINST);
1862 ADD_CODE(&td, get_data_item_index (&td, klass));
1866 switch (td.sp [-1].type) {
1870 ADD_CODE(&td, MINT_CONV_R_UN_I8);
1873 ADD_CODE(&td, MINT_CONV_R_UN_I4);
1876 g_assert_not_reached ();
1878 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1882 CHECK_STACK (&td, 1);
1883 token = read32 (td.ip + 1);
1885 if (method->wrapper_type != MONO_WRAPPER_NONE)
1886 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
1888 klass = mono_class_get_full (image, token, generic_context);
1890 if (mono_class_is_nullable (klass)) {
1891 g_error ("cee_unbox: implement Nullable");
1894 ADD_CODE(&td, MINT_UNBOX);
1895 ADD_CODE(&td, get_data_item_index (&td, klass));
1896 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
1900 CHECK_STACK (&td, 1);
1901 token = read32 (td.ip + 1);
1903 g_assert (method->wrapper_type == MONO_WRAPPER_NONE);
1904 klass = mono_class_get_full (image, token, generic_context);
1906 if (mini_type_is_reference (&klass->byval_arg)) {
1907 g_error ("unbox_any: generic class is reference type");
1908 } else if (mono_class_is_nullable (klass)) {
1909 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Unbox", 1);
1910 /* td.ip is incremented by interp_transform_call */
1911 interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL);
1913 int mt = mint_type (&klass->byval_arg);
1914 ADD_CODE (&td, MINT_UNBOX);
1915 ADD_CODE (&td, get_data_item_index (&td, klass));
1916 SET_TYPE (td.sp - 1, stack_type [mt], klass);
1917 if (mt == MINT_TYPE_VT) {
1918 int size = mono_class_value_size (klass, NULL);
1919 PUSH_VT (&td, size);
1926 CHECK_STACK (&td, 1);
1927 SIMPLE_OP (td, MINT_THROW);
1929 generating_code = 0;
1932 CHECK_STACK (&td, 1);
1933 token = read32 (td.ip + 1);
1934 field = mono_field_from_token (image, token, &klass, generic_context);
1935 mono_class_init (klass);
1936 mt = mint_type(field->type);
1937 ADD_CODE(&td, MINT_LDFLDA);
1938 ADD_CODE(&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
1940 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
1943 CHECK_STACK (&td, 1);
1944 token = read32 (td.ip + 1);
1945 field = mono_field_from_token (image, token, &klass, generic_context);
1946 mono_class_init (klass);
1948 MonoClass *field_klass = mono_class_from_mono_type (field->type);
1949 mt = mint_type (&field_klass->byval_arg);
1950 if (klass->marshalbyref) {
1951 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_LDRMFLD_VT : MINT_LDRMFLD);
1952 ADD_CODE(&td, get_data_item_index (&td, field));
1954 ADD_CODE(&td, MINT_LDFLD_I1 + mt - MINT_TYPE_I1);
1955 ADD_CODE(&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
1957 if (mt == MINT_TYPE_VT) {
1958 int size = mono_class_value_size (field_klass, NULL);
1960 WRITE32(&td, &size);
1962 if (td.sp [-1].type == STACK_TYPE_VT) {
1963 int size = mono_class_value_size (klass, NULL);
1964 size = (size + 7) & ~7;
1966 ADD_CODE (&td, MINT_VTRESULT);
1968 WRITE32 (&td, &size);
1971 SET_TYPE(td.sp - 1, stack_type [mt], field_klass);
1975 CHECK_STACK (&td, 2);
1976 token = read32 (td.ip + 1);
1977 field = mono_field_from_token (image, token, &klass, generic_context);
1978 mono_class_init (klass);
1979 mt = mint_type(field->type);
1980 if (klass->marshalbyref) {
1981 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STRMFLD_VT : MINT_STRMFLD);
1982 ADD_CODE(&td, get_data_item_index (&td, field));
1984 ADD_CODE(&td, MINT_STFLD_I1 + mt - MINT_TYPE_I1);
1985 ADD_CODE(&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
1987 if (mt == MINT_TYPE_VT) {
1988 MonoClass *klass = mono_class_from_mono_type (field->type);
1989 int size = mono_class_value_size (klass, NULL);
1991 WRITE32(&td, &size);
1997 token = read32 (td.ip + 1);
1998 field = mono_field_from_token (image, token, &klass, generic_context);
1999 ADD_CODE(&td, MINT_LDSFLDA);
2000 ADD_CODE(&td, get_data_item_index (&td, field));
2002 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
2005 token = read32 (td.ip + 1);
2006 field = mono_field_from_token (image, token, &klass, generic_context);
2007 mt = mint_type(field->type);
2008 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2009 ADD_CODE(&td, get_data_item_index (&td, field));
2011 if (mt == MINT_TYPE_VT) {
2012 MonoClass *klass = mono_class_from_mono_type (field->type);
2013 int size = mono_class_value_size (klass, NULL);
2015 WRITE32(&td, &size);
2016 klass = field->type->data.klass;
2018 if (mt == MINT_TYPE_O)
2019 klass = mono_class_from_mono_type (field->type);
2022 PUSH_TYPE(&td, stack_type [mt], klass);
2025 CHECK_STACK (&td, 1);
2026 token = read32 (td.ip + 1);
2027 field = mono_field_from_token (image, token, &klass, generic_context);
2028 mt = mint_type(field->type);
2029 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
2030 ADD_CODE(&td, get_data_item_index (&td, field));
2031 if (mt == MINT_TYPE_VT) {
2032 MonoClass *klass = mono_class_from_mono_type (field->type);
2033 int size = mono_class_value_size (klass, NULL);
2035 WRITE32 (&td, &size);
2042 token = read32 (td.ip + 1);
2044 if (method->wrapper_type != MONO_WRAPPER_NONE)
2045 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2047 klass = mono_class_get_full (image, token, generic_context);
2049 ADD_CODE(&td, td.sp [-1].type == STACK_TYPE_VT ? MINT_STOBJ_VT : MINT_STOBJ);
2050 ADD_CODE(&td, get_data_item_index (&td, klass));
2051 if (td.sp [-1].type == STACK_TYPE_VT) {
2052 size = mono_class_value_size (klass, NULL);
2053 size = (size + 7) & ~7;
2060 case CEE_CONV_OVF_I_UN:
2061 CHECK_STACK (&td, 1);
2062 switch (td.sp [-1].type) {
2064 #if SIZEOF_VOID_P == 8
2065 ADD_CODE(&td, MINT_CONV_OVF_I8_UN_R8);
2067 ADD_CODE(&td, MINT_CONV_OVF_I4_UN_R8);
2074 #if SIZEOF_VOID_P == 8
2075 ADD_CODE(&td, MINT_CONV_I8_U4);
2079 g_assert_not_reached ();
2082 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2085 case CEE_CONV_OVF_I8_UN:
2086 CHECK_STACK (&td, 1);
2087 switch (td.sp [-1].type) {
2089 ADD_CODE(&td, MINT_CONV_OVF_I8_UN_R8);
2094 ADD_CODE(&td, MINT_CONV_I8_U4);
2097 g_assert_not_reached ();
2100 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2105 CHECK_STACK (&td, 1);
2106 token = read32 (td.ip + 1);
2107 if (method->wrapper_type != MONO_WRAPPER_NONE)
2108 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2110 klass = mono_class_get_full (image, token, generic_context);
2112 if (mono_class_is_nullable (klass)) {
2113 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Box", 1);
2114 /* td.ip is incremented by interp_transform_call */
2115 interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL);
2116 } else if (!klass->valuetype) {
2117 /* already boxed, do nothing. */
2120 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT && !klass->enumtype) {
2121 size = mono_class_value_size (klass, NULL);
2122 size = (size + 7) & ~7;
2125 ADD_CODE(&td, MINT_BOX);
2126 ADD_CODE(&td, get_data_item_index (&td, klass));
2128 SET_TYPE(td.sp - 1, STACK_TYPE_O, klass);
2135 CHECK_STACK (&td, 1);
2136 token = read32 (td.ip + 1);
2138 if (method->wrapper_type != MONO_WRAPPER_NONE)
2139 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2141 klass = mono_class_get_full (image, token, generic_context);
2143 ADD_CODE(&td, MINT_NEWARR);
2144 ADD_CODE(&td, get_data_item_index (&td, klass));
2145 SET_TYPE(td.sp - 1, STACK_TYPE_O, klass);
2149 CHECK_STACK (&td, 1);
2150 SIMPLE_OP (td, MINT_LDLEN);
2151 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2154 CHECK_STACK (&td, 2);
2156 token = read32 (td.ip + 1);
2158 if (method->wrapper_type != MONO_WRAPPER_NONE)
2159 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2161 klass = mono_class_get_full (image, token, generic_context);
2163 ADD_CODE(&td, MINT_LDELEMA);
2164 ADD_CODE(&td, get_data_item_index (&td, klass));
2167 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2170 CHECK_STACK (&td, 2);
2172 SIMPLE_OP (td, MINT_LDELEM_I1);
2174 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2177 CHECK_STACK (&td, 2);
2179 SIMPLE_OP (td, MINT_LDELEM_U1);
2181 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2184 CHECK_STACK (&td, 2);
2186 SIMPLE_OP (td, MINT_LDELEM_I2);
2188 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2191 CHECK_STACK (&td, 2);
2193 SIMPLE_OP (td, MINT_LDELEM_U2);
2195 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2198 CHECK_STACK (&td, 2);
2200 SIMPLE_OP (td, MINT_LDELEM_I4);
2202 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2205 CHECK_STACK (&td, 2);
2207 SIMPLE_OP (td, MINT_LDELEM_U4);
2209 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2212 CHECK_STACK (&td, 2);
2214 SIMPLE_OP (td, MINT_LDELEM_I8);
2216 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2219 CHECK_STACK (&td, 2);
2221 SIMPLE_OP (td, MINT_LDELEM_I);
2223 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2226 CHECK_STACK (&td, 2);
2228 SIMPLE_OP (td, MINT_LDELEM_R4);
2230 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2233 CHECK_STACK (&td, 2);
2235 SIMPLE_OP (td, MINT_LDELEM_R8);
2237 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2239 case CEE_LDELEM_REF:
2240 CHECK_STACK (&td, 2);
2242 SIMPLE_OP (td, MINT_LDELEM_REF);
2244 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
2247 CHECK_STACK (&td, 2);
2248 token = read32 (td.ip + 1);
2249 klass = mono_class_get_full (image, token, generic_context);
2250 switch (mint_type (&klass->byval_arg)) {
2253 SIMPLE_OP (td, MINT_LDELEM_I4);
2255 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2257 case MINT_TYPE_VT: {
2258 int size = mono_class_value_size (klass, NULL);
2260 SIMPLE_OP (td, MINT_LDELEM_VT);
2261 ADD_CODE (&td, get_data_item_index (&td, klass));
2262 WRITE32 (&td, &size);
2264 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_VT);
2265 PUSH_VT (&td, size);
2269 GString *res = g_string_new ("");
2270 mono_type_get_desc (res, &klass->byval_arg, TRUE);
2271 g_print ("LDELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
2272 g_string_free (res, TRUE);
2280 CHECK_STACK (&td, 3);
2282 SIMPLE_OP (td, MINT_STELEM_I);
2286 CHECK_STACK (&td, 3);
2288 SIMPLE_OP (td, MINT_STELEM_I1);
2292 CHECK_STACK (&td, 3);
2294 SIMPLE_OP (td, MINT_STELEM_I2);
2298 CHECK_STACK (&td, 3);
2300 SIMPLE_OP (td, MINT_STELEM_I4);
2304 CHECK_STACK (&td, 3);
2306 SIMPLE_OP (td, MINT_STELEM_I8);
2310 CHECK_STACK (&td, 3);
2312 SIMPLE_OP (td, MINT_STELEM_R4);
2316 CHECK_STACK (&td, 3);
2318 SIMPLE_OP (td, MINT_STELEM_R8);
2321 case CEE_STELEM_REF:
2322 CHECK_STACK (&td, 3);
2324 SIMPLE_OP (td, MINT_STELEM_REF);
2328 CHECK_STACK (&td, 3);
2330 token = read32 (td.ip + 1);
2331 klass = mono_class_get_full (image, token, generic_context);
2332 switch (mint_type (&klass->byval_arg)) {
2334 SIMPLE_OP (td, MINT_STELEM_I4);
2337 SIMPLE_OP (td, MINT_STELEM_REF);
2339 case MINT_TYPE_VT: {
2340 int size = mono_class_value_size (klass, NULL);
2341 SIMPLE_OP (td, MINT_STELEM_VT);
2342 ADD_CODE (&td, get_data_item_index (&td, klass));
2343 WRITE32 (&td, &size);
2348 GString *res = g_string_new ("");
2349 mono_type_get_desc (res, &klass->byval_arg, TRUE);
2350 g_print ("STELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
2351 g_string_free (res, TRUE);
2360 case CEE_CONV_OVF_U1:
2362 case CEE_CONV_OVF_I8:
2364 #if SIZEOF_VOID_P == 8
2365 case CEE_CONV_OVF_U:
2367 case CEE_REFANYVAL: ves_abort(); break;
2370 CHECK_STACK (&td, 1);
2371 SIMPLE_OP (td, MINT_CKFINITE);
2373 case CEE_CONV_OVF_I1:
2374 case CEE_CONV_OVF_I1_UN:
2375 CHECK_STACK (&td, 1);
2376 switch (td.sp [-1].type) {
2378 ADD_CODE(&td, MINT_CONV_OVF_I1_R8);
2381 ADD_CODE(&td, MINT_CONV_OVF_I1_I4);
2384 ADD_CODE(&td, MINT_CONV_OVF_I1_I8);
2387 g_assert_not_reached ();
2390 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2392 case CEE_CONV_OVF_U1:
2393 case CEE_CONV_OVF_U1_UN:
2394 CHECK_STACK (&td, 1);
2395 switch (td.sp [-1].type) {
2397 ADD_CODE(&td, MINT_CONV_OVF_U1_R8);
2400 ADD_CODE(&td, MINT_CONV_OVF_U1_I4);
2403 ADD_CODE(&td, MINT_CONV_OVF_U1_I8);
2406 g_assert_not_reached ();
2409 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2411 case CEE_CONV_OVF_I2:
2412 case CEE_CONV_OVF_I2_UN:
2413 CHECK_STACK (&td, 1);
2414 switch (td.sp [-1].type) {
2416 ADD_CODE(&td, MINT_CONV_OVF_I2_R8);
2419 ADD_CODE(&td, MINT_CONV_OVF_I2_I4);
2422 ADD_CODE(&td, MINT_CONV_OVF_I2_I8);
2425 g_assert_not_reached ();
2428 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2430 case CEE_CONV_OVF_U2_UN:
2431 case CEE_CONV_OVF_U2:
2432 CHECK_STACK (&td, 1);
2433 switch (td.sp [-1].type) {
2435 ADD_CODE(&td, MINT_CONV_OVF_U2_R8);
2438 ADD_CODE(&td, MINT_CONV_OVF_U2_I4);
2441 ADD_CODE(&td, MINT_CONV_OVF_U2_I8);
2444 g_assert_not_reached ();
2447 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2449 #if SIZEOF_VOID_P == 4
2450 case CEE_CONV_OVF_I:
2452 case CEE_CONV_OVF_I4:
2453 case CEE_CONV_OVF_I4_UN:
2454 CHECK_STACK (&td, 1);
2455 switch (td.sp [-1].type) {
2457 ADD_CODE(&td, MINT_CONV_OVF_I4_R8);
2460 if (*td.ip == CEE_CONV_OVF_I4_UN)
2461 ADD_CODE(&td, MINT_CONV_OVF_I4_U4);
2464 ADD_CODE(&td, MINT_CONV_OVF_I4_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_U:
2475 case CEE_CONV_OVF_U4:
2476 case CEE_CONV_OVF_U4_UN:
2477 CHECK_STACK (&td, 1);
2478 switch (td.sp [-1].type) {
2480 ADD_CODE(&td, MINT_CONV_OVF_U4_R8);
2483 if (*td.ip != CEE_CONV_OVF_U4_UN)
2484 ADD_CODE(&td, MINT_CONV_OVF_U4_I4);
2487 ADD_CODE(&td, MINT_CONV_OVF_U4_I8);
2490 g_assert_not_reached ();
2493 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2495 #if SIZEOF_VOID_P == 8
2496 case CEE_CONV_OVF_I:
2498 case CEE_CONV_OVF_I8:
2499 CHECK_STACK (&td, 1);
2500 switch (td.sp [-1].type) {
2502 ADD_CODE(&td, MINT_CONV_OVF_I8_R8);
2505 ADD_CODE(&td, MINT_CONV_I8_I4);
2510 g_assert_not_reached ();
2513 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2515 #if SIZEOF_VOID_P == 8
2516 case CEE_CONV_OVF_U:
2518 case CEE_CONV_OVF_U8:
2519 CHECK_STACK (&td, 1);
2520 switch (td.sp [-1].type) {
2522 ADD_CODE(&td, MINT_CONV_OVF_U8_R8);
2525 ADD_CODE(&td, MINT_CONV_OVF_U8_I4);
2530 g_assert_not_reached ();
2533 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2538 token = read32 (td.ip + 1);
2539 handle = mono_ldtoken (image, token, &klass, generic_context);
2540 mt = mint_type(&klass->byval_arg);
2541 g_assert (mt == MINT_TYPE_VT);
2542 size = mono_class_value_size (klass, NULL);
2543 g_assert (size == sizeof(gpointer));
2544 PUSH_VT(&td, sizeof(gpointer));
2545 ADD_CODE(&td, MINT_LDTOKEN);
2546 ADD_CODE(&td, get_data_item_index (&td, handle));
2547 PUSH_SIMPLE_TYPE(&td, stack_type [mt]);
2552 binary_arith_op(&td, MINT_ADD_OVF_I4);
2555 case CEE_ADD_OVF_UN:
2556 binary_arith_op(&td, MINT_ADD_OVF_UN_I4);
2560 binary_arith_op(&td, MINT_MUL_OVF_I4);
2563 case CEE_MUL_OVF_UN:
2564 binary_arith_op(&td, MINT_MUL_OVF_UN_I4);
2568 binary_arith_op(&td, MINT_SUB_OVF_I4);
2571 case CEE_SUB_OVF_UN:
2572 binary_arith_op(&td, MINT_SUB_OVF_UN_I4);
2575 case CEE_ENDFINALLY:
2576 SIMPLE_OP (td, MINT_ENDFINALLY);
2577 generating_code = 0;
2581 handle_branch (&td, MINT_LEAVE_S, MINT_LEAVE, 5 + read32 (td.ip + 1));
2583 generating_code = 0;
2587 handle_branch (&td, MINT_LEAVE_S, MINT_LEAVE, 2 + (gint8)td.ip [1]);
2589 generating_code = 0;
2594 case CEE_MONO_CALLI_EXTRA_ARG:
2595 /* Same as CEE_CALLI, llvm specific */
2596 interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, NULL);
2598 case CEE_MONO_ICALL: {
2601 MonoJitICallInfo *info;
2603 token = read32 (td.ip + 1);
2605 func = mono_method_get_wrapper_data (method, token);
2606 info = mono_find_jit_icall_by_addr (func);
2609 CHECK_STACK (&td, info->sig->param_count);
2610 switch (info->sig->param_count) {
2612 if (MONO_TYPE_IS_VOID (info->sig->ret))
2613 ADD_CODE (&td,MINT_ICALL_V_V);
2615 ADD_CODE (&td, MINT_ICALL_V_P);
2618 if (MONO_TYPE_IS_VOID (info->sig->ret))
2619 ADD_CODE (&td,MINT_ICALL_P_V);
2621 ADD_CODE (&td,MINT_ICALL_P_P);
2624 if (MONO_TYPE_IS_VOID (info->sig->ret)) {
2625 if (info->sig->params [1]->type == MONO_TYPE_I4)
2626 ADD_CODE (&td,MINT_ICALL_PI_V);
2628 ADD_CODE (&td,MINT_ICALL_PP_V);
2630 if (info->sig->params [1]->type == MONO_TYPE_I4)
2631 ADD_CODE (&td,MINT_ICALL_PI_P);
2633 ADD_CODE (&td,MINT_ICALL_PP_P);
2637 g_assert (MONO_TYPE_IS_VOID (info->sig->ret));
2638 if (info->sig->params [2]->type == MONO_TYPE_I4)
2639 ADD_CODE (&td,MINT_ICALL_PPI_V);
2641 ADD_CODE (&td,MINT_ICALL_PPP_V);
2644 g_assert_not_reached ();
2647 if (func == mono_ftnptr_to_delegate) {
2648 g_error ("TODO: ?");
2649 func = mono_interp_ftnptr_to_delegate;
2651 ADD_CODE(&td, get_data_item_index (&td, func));
2652 td.sp -= info->sig->param_count;
2654 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2656 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2660 case CEE_MONO_VTADDR: {
2662 CHECK_STACK (&td, 1);
2663 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
2664 size = mono_class_native_size(td.sp [-1].klass, NULL);
2666 size = mono_class_value_size(td.sp [-1].klass, NULL);
2667 size = (size + 7) & ~7;
2668 ADD_CODE(&td, MINT_VTRESULT);
2670 WRITE32(&td, &size);
2673 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2676 case CEE_MONO_LDPTR:
2677 case CEE_MONO_CLASSCONST:
2678 token = read32 (td.ip + 1);
2680 ADD_CODE(&td, MINT_MONO_LDPTR);
2681 ADD_CODE(&td, get_data_item_index (&td, mono_method_get_wrapper_data (method, token)));
2682 td.sp [0].type = STACK_TYPE_I;
2685 case CEE_MONO_OBJADDR:
2686 CHECK_STACK (&td, 1);
2688 td.sp[-1].type = STACK_TYPE_MP;
2691 case CEE_MONO_NEWOBJ:
2692 token = read32 (td.ip + 1);
2694 ADD_CODE(&td, MINT_MONO_NEWOBJ);
2695 ADD_CODE(&td, get_data_item_index (&td, mono_method_get_wrapper_data (method, token)));
2696 td.sp [0].type = STACK_TYPE_O;
2699 case CEE_MONO_RETOBJ:
2700 CHECK_STACK (&td, 1);
2701 token = read32 (td.ip + 1);
2703 ADD_CODE(&td, MINT_MONO_RETOBJ);
2706 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2708 /*stackval_from_data (signature->ret, frame->retval, sp->data.vt, signature->pinvoke);*/
2710 if (td.sp > td.stack)
2711 g_warning ("CEE_MONO_RETOBJ: more values on stack: %d", td.sp-td.stack);
2713 case CEE_MONO_LDNATIVEOBJ:
2714 token = read32 (td.ip + 1);
2716 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2717 g_assert(klass->valuetype);
2718 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2720 case CEE_MONO_SAVE_LMF:
2721 case CEE_MONO_RESTORE_LMF:
2722 case CEE_MONO_NOT_TAKEN:
2725 case CEE_MONO_LDPTR_INT_REQ_FLAG:
2726 ADD_CODE (&td, MINT_MONO_LDPTR);
2727 ADD_CODE (&td, get_data_item_index (&td, mono_thread_interruption_request_flag ()));
2728 PUSH_TYPE (&td, STACK_TYPE_MP, NULL);
2732 g_error ("transform.c: Unimplemented opcode: 0xF0 %02x at 0x%x\n", *td.ip, td.ip-header->code);
2742 case CEE_PREFIXREF: ves_abort(); break;
2745 * Note: Exceptions thrown when executing a prefixed opcode need
2746 * to take into account the number of prefix bytes (usually the
2747 * throw point is just (ip - n_prefix_bytes).
2753 case CEE_ARGLIST: ves_abort(); break;
2756 CHECK_STACK(&td, 2);
2757 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2758 ADD_CODE(&td, MINT_CEQ_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2760 ADD_CODE(&td, MINT_CEQ_I4 + td.sp [-1].type - STACK_TYPE_I4);
2762 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2766 CHECK_STACK(&td, 2);
2767 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2768 ADD_CODE(&td, MINT_CGT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2770 ADD_CODE(&td, MINT_CGT_I4 + td.sp [-1].type - STACK_TYPE_I4);
2772 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2776 CHECK_STACK(&td, 2);
2777 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2778 ADD_CODE(&td, MINT_CGT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2780 ADD_CODE(&td, MINT_CGT_UN_I4 + td.sp [-1].type - STACK_TYPE_I4);
2782 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2786 CHECK_STACK(&td, 2);
2787 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2788 ADD_CODE(&td, MINT_CLT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2790 ADD_CODE(&td, MINT_CLT_I4 + td.sp [-1].type - STACK_TYPE_I4);
2792 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
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_CLT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2800 ADD_CODE(&td, MINT_CLT_UN_I4 + td.sp [-1].type - STACK_TYPE_I4);
2802 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2805 case CEE_LDVIRTFTN: /* fallthrough */
2808 if (*td.ip == CEE_LDVIRTFTN) {
2809 CHECK_STACK (&td, 1);
2812 token = read32 (td.ip + 1);
2813 if (method->wrapper_type != MONO_WRAPPER_NONE)
2814 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
2816 m = mono_get_method_full (image, token, NULL, generic_context);
2818 if (method->wrapper_type == MONO_WRAPPER_NONE && m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
2819 m = mono_marshal_get_synchronized_wrapper (m);
2821 ADD_CODE(&td, *td.ip == CEE_LDFTN ? MINT_LDFTN : MINT_LDVIRTFTN);
2822 ADD_CODE(&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
2823 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2825 PUSH_SIMPLE_TYPE (&td, STACK_TYPE_F);
2829 load_arg (&td, read16 (td.ip + 1));
2833 int n = read16 (td.ip + 1);
2834 if (n == 0 && signature->hasthis) {
2835 g_error ("LDTHISA: NOPE");
2836 ADD_CODE(&td, MINT_LDTHISA);
2839 ADD_CODE(&td, MINT_LDARGA);
2840 ADD_CODE(&td, td.rtm->arg_offsets [n]); /* FIX for large offsets */
2842 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
2847 store_arg (&td, read16 (td.ip + 1));
2851 load_local (&td, read16 (td.ip + 1));
2855 ADD_CODE(&td, MINT_LDLOCA_S);
2856 ADD_CODE(&td, td.rtm->local_offsets [read16 (td.ip + 1)]);
2857 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
2861 store_local (&td, read16 (td.ip + 1));
2865 CHECK_STACK (&td, 1);
2866 #if SIZEOF_VOID_P == 8
2867 if (td.sp [-1].type == STACK_TYPE_I8)
2868 ADD_CODE(&td, MINT_CONV_I4_I8);
2870 ADD_CODE(&td, MINT_LOCALLOC);
2871 if (td.sp != td.stack + 1)
2872 g_warning("CEE_LOCALLOC: stack not empty");
2874 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2877 case CEE_UNUSED57: ves_abort(); break;
2878 case CEE_ENDFILTER: ves_abort(); break;
2880 case CEE_UNALIGNED_:
2882 /* FIX: should do something? */;
2886 /* FIX: should do something? */;
2890 /* FIX: should do something? */;
2893 CHECK_STACK(&td, 1);
2894 token = read32 (td.ip + 1);
2895 klass = mono_class_get_full (image, token, generic_context);
2896 ADD_CODE(&td, MINT_INITOBJ);
2897 i32 = mono_class_value_size (klass, NULL);
2903 CHECK_STACK(&td, 3);
2904 /* FIX? convert length to I8? */
2905 ADD_CODE(&td, MINT_CPBLK);
2909 case CEE_CONSTRAINED_:
2910 token = read32 (td.ip + 1);
2911 constrained_class = mono_class_get_full (image, token, generic_context);
2912 mono_class_init (constrained_class);
2916 CHECK_STACK(&td, 3);
2917 ADD_CODE(&td, MINT_INITBLK);
2922 /* FIXME: implement */
2927 SIMPLE_OP (td, MINT_RETHROW);
2928 generating_code = 0;
2932 token = read32 (td.ip + 1);
2934 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
2936 MonoType *type = mono_type_create_from_typespec (image, token);
2937 size = mono_type_size (type, &align);
2940 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
2941 mono_class_init (szclass);
2943 if (!szclass->valuetype)
2944 THROW_EX (mono_exception_from_name (mono_defaults.corlib, "System", "InvalidProgramException"), ip - 5);
2946 size = mono_class_value_size (szclass, &align);
2948 ADD_CODE(&td, MINT_LDC_I4);
2949 WRITE32(&td, &size);
2950 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
2954 case CEE_REFANYTYPE: ves_abort(); break;
2957 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);
2961 g_error ("transform.c: Unimplemented opcode: %02x at 0x%x\n", *td.ip, td.ip-header->code);
2964 if (td.new_ip - td.new_code != new_in_start_offset)
2965 td.last_new_ip = td.new_code + new_in_start_offset;
2966 else if (td.is_bb_start [td.in_start - td.il_code])
2967 td.is_bb_start [td.ip - td.il_code] = 1;
2969 td.last_ip = td.in_start;
2972 if (mono_interp_traceopt) {
2973 const guint16 *p = td.new_code;
2974 printf("Runtime method: %p, VT stack size: %d\n", rtm, td.max_vt_sp);
2975 printf("Calculated stack size: %d, stated size: %d\n", td.max_stack_height, header->max_stack);
2976 while (p < td.new_ip) {
2977 p = mono_interp_dis_mintop(td.new_code, p);
2982 rtm->clauses = mono_mempool_alloc (domain->mp, header->num_clauses * sizeof(MonoExceptionClause));
2983 memcpy (rtm->clauses, header->clauses, header->num_clauses * sizeof(MonoExceptionClause));
2984 rtm->code = mono_mempool_alloc (domain->mp, (td.new_ip - td.new_code) * sizeof(gushort));
2985 memcpy (rtm->code, td.new_code, (td.new_ip - td.new_code) * sizeof(gushort));
2986 g_free (td.new_code);
2987 rtm->new_body_start = rtm->code + body_start_offset;
2988 rtm->num_clauses = header->num_clauses;
2989 for (i = 0; i < header->num_clauses; i++) {
2990 MonoExceptionClause *c = rtm->clauses + i;
2991 int end_off = c->try_offset + c->try_len;
2992 c->try_offset = td.in_offsets [c->try_offset];
2993 c->try_len = td.in_offsets [end_off] - c->try_offset;
2994 end_off = c->handler_offset + c->handler_len;
2995 c->handler_offset = td.in_offsets [c->handler_offset];
2996 c->handler_len = td.in_offsets [end_off] - c->handler_offset;
2998 rtm->vt_stack_size = td.max_vt_sp;
2999 rtm->alloca_size = rtm->locals_size + rtm->args_size + rtm->vt_stack_size + rtm->stack_size;
3000 rtm->data_items = mono_mempool_alloc (domain->mp, td.n_data_items * sizeof (td.data_items [0]));
3001 memcpy (rtm->data_items, td.data_items, td.n_data_items * sizeof (td.data_items [0]));
3002 g_free (td.in_offsets);
3003 g_free (td.forward_refs);
3004 for (i = 0; i < header->code_size; ++i)
3005 g_free (td.stack_state [i]);
3006 g_free (td.stack_state);
3007 g_free (td.stack_height);
3008 g_free (td.vt_stack_size);
3009 g_free (td.data_items);
3010 g_hash_table_destroy (td.data_hash);
3013 static mono_mutex_t calc_section;
3016 mono_interp_transform_init (void)
3018 mono_os_mutex_init_recursive(&calc_section);
3022 mono_interp_transform_method (RuntimeMethod *runtime_method, ThreadContext *context)
3024 int i, align, size, offset;
3025 MonoMethod *method = runtime_method->method;
3026 MonoImage *image = method->klass->image;
3027 MonoMethodHeader *header = mono_method_get_header (method);
3028 MonoMethodSignature *signature = mono_method_signature (method);
3029 register const unsigned char *ip, *end;
3030 const MonoOpcode *opcode;
3033 MonoDomain *domain = mono_domain_get ();
3034 unsigned char *is_bb_start;
3036 MonoVTable *method_class_vt;
3038 MonoGenericContext *generic_context = NULL;
3040 // g_printerr ("TRANSFORM(0x%016lx): begin %s::%s\n", mono_thread_current (), method->klass->name, method->name);
3041 method_class_vt = mono_class_vtable (domain, runtime_method->method->klass);
3042 if (!method_class_vt->initialized) {
3044 MonoInvocation *last_env_frame = context->env_frame;
3045 jmp_buf *old_env = context->current_env;
3048 MonoException *failed = context->env_frame->ex;
3049 context->env_frame->ex = NULL;
3050 context->env_frame = last_env_frame;
3051 context->current_env = old_env;
3054 context->env_frame = context->current_frame;
3055 context->current_env = &env;
3056 mono_runtime_class_init (method_class_vt);
3057 context->env_frame = last_env_frame;
3058 context->current_env = old_env;
3061 mono_profiler_method_jit (method); /* sort of... */
3063 if (mono_method_signature (method)->is_inflated)
3064 generic_context = &((MonoMethodInflated *) method)->context;
3066 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
3067 MonoMethod *nm = NULL;
3068 mono_os_mutex_lock(&calc_section);
3069 if (runtime_method->transformed) {
3070 mono_os_mutex_unlock(&calc_section);
3071 g_error ("FIXME: no jit info?");
3072 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3076 /* assumes all internal calls with an array this are built in... */
3077 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL && (! mono_method_signature (method)->hasthis || method->klass->rank == 0)) {
3078 nm = mono_marshal_get_native_wrapper (method, TRUE, FALSE);
3079 signature = mono_method_signature (nm);
3081 const char *name = method->name;
3082 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
3083 if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
3084 nm = mono_marshal_get_delegate_invoke (method, NULL);
3085 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
3086 nm = mono_marshal_get_delegate_begin_invoke (method);
3087 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
3088 nm = mono_marshal_get_delegate_end_invoke (method);
3092 runtime_method->code = g_malloc(sizeof(short));
3093 runtime_method->code[0] = MINT_CALLRUN;
3097 runtime_method->stack_size = sizeof (stackval); /* for tracing */
3098 runtime_method->alloca_size = runtime_method->stack_size;
3099 runtime_method->transformed = TRUE;
3100 mono_os_mutex_unlock(&calc_section);
3101 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3105 header = mono_method_get_header (nm);
3106 mono_os_mutex_unlock(&calc_section);
3108 g_assert ((signature->param_count + signature->hasthis) < 1000);
3109 g_assert (header->max_stack < 10000);
3110 /* intern the strings in the method. */
3112 end = ip + header->code_size;
3114 is_bb_start = g_malloc0(header->code_size);
3115 is_bb_start [0] = 1;
3122 else if (in == 0xf0) {
3124 in = *ip + MONO_CEE_MONO_ICALL;
3126 opcode = &mono_opcodes [in];
3127 switch (opcode->argument) {
3128 case MonoInlineNone:
3131 case MonoInlineString:
3132 if (method->wrapper_type == MONO_WRAPPER_NONE)
3133 mono_ldstr (domain, image, mono_metadata_token_index (read32 (ip + 1)));
3136 case MonoInlineType:
3137 if (method->wrapper_type == MONO_WRAPPER_NONE) {
3138 class = mono_class_get_full (image, read32 (ip + 1), generic_context);
3139 mono_class_init (class);
3140 /* quick fix to not do this for the fake ptr classes - probably should not be getting the vtable at all here */
3142 g_error ("FIXME: interface method lookup: %s (in method %s)", class->name, method->name);
3143 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE) && class->interface_offsets != NULL)
3144 mono_class_vtable (domain, class);
3149 case MonoInlineMethod:
3150 if (method->wrapper_type == MONO_WRAPPER_NONE && *ip != CEE_CALLI) {
3151 m = mono_get_method_full (image, read32 (ip + 1), NULL, generic_context);
3153 g_free (is_bb_start);
3154 g_error ("FIXME: where to get method and class string?");
3156 // return mono_get_exception_missing_method ();
3158 mono_class_init (m->klass);
3159 if (!mono_class_is_interface (m->klass))
3160 mono_class_vtable (domain, m->klass);
3164 case MonoInlineField:
3168 case MonoShortInlineR:
3171 case MonoInlineBrTarget:
3172 offset = read32 (ip + 1);
3174 backwards = offset < 0;
3175 offset += ip - header->code;
3176 g_assert (offset >= 0 && offset < header->code_size);
3177 is_bb_start [offset] |= backwards ? 2 : 1;
3179 case MonoShortInlineBrTarget:
3180 offset = ((gint8 *)ip) [1];
3182 backwards = offset < 0;
3183 offset += ip - header->code;
3184 g_assert (offset >= 0 && offset < header->code_size);
3185 is_bb_start [offset] |= backwards ? 2 : 1;
3190 case MonoShortInlineVar:
3191 case MonoShortInlineI:
3194 case MonoInlineSwitch: {
3196 const unsigned char *next_ip;
3200 next_ip = ip + 4 * n;
3201 for (i = 0; i < n; i++) {
3202 offset = read32 (ip);
3203 backwards = offset < 0;
3204 offset += next_ip - header->code;
3205 g_assert (offset >= 0 && offset < header->code_size);
3206 is_bb_start [offset] |= backwards ? 2 : 1;
3216 g_assert_not_reached ();
3219 // g_printerr ("TRANSFORM(0x%016lx): end %s::%s\n", mono_thread_current (), method->klass->name, method->name);
3221 /* the rest needs to be locked so it is only done once */
3222 mono_os_mutex_lock(&calc_section);
3223 if (runtime_method->transformed) {
3224 mono_os_mutex_unlock(&calc_section);
3225 g_free (is_bb_start);
3226 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3230 runtime_method->local_offsets = g_malloc (header->num_locals * sizeof(guint32));
3231 runtime_method->stack_size = (sizeof (stackval) + 2) * header->max_stack; /* + 1 for returns of called functions + 1 for 0-ing in trace*/
3232 runtime_method->stack_size = (runtime_method->stack_size + 7) & ~7;
3234 for (i = 0; i < header->num_locals; ++i) {
3235 size = mono_type_size (header->locals [i], &align);
3236 offset += align - 1;
3237 offset &= ~(align - 1);
3238 runtime_method->local_offsets [i] = offset;
3241 offset = (offset + 7) & ~7;
3242 runtime_method->locals_size = offset;
3243 g_assert (runtime_method->locals_size < 65536);
3245 runtime_method->arg_offsets = g_malloc ((!!signature->hasthis + signature->param_count) * sizeof(guint32));
3247 if (signature->hasthis) {
3248 g_assert (!signature->pinvoke);
3249 size = mono_type_stack_size (&method->klass->byval_arg, &align);
3250 offset += align - 1;
3251 offset &= ~(align - 1);
3252 runtime_method->arg_offsets [0] = offset;
3256 for (i = 0; i < signature->param_count; ++i) {
3257 if (signature->pinvoke) {
3259 size = mono_type_native_stack_size (signature->params [i], &dummy);
3263 size = mono_type_stack_size (signature->params [i], &align);
3264 offset += align - 1;
3265 offset &= ~(align - 1);
3266 runtime_method->arg_offsets [i + !!signature->hasthis] = offset;
3269 offset = (offset + 7) & ~7;
3270 runtime_method->args_size = offset;
3271 g_assert (runtime_method->args_size < 10000);
3273 generate(method, runtime_method, is_bb_start);
3275 g_free (is_bb_start);
3277 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3278 runtime_method->transformed = TRUE;
3279 mono_os_mutex_unlock(&calc_section);