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;
1968 SET_TYPE(td.sp - 1, stack_type [mt], field_klass);
1972 CHECK_STACK (&td, 2);
1973 token = read32 (td.ip + 1);
1974 field = mono_field_from_token (image, token, &klass, generic_context);
1975 mono_class_init (klass);
1976 mt = mint_type(field->type);
1977 if (klass->marshalbyref) {
1978 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STRMFLD_VT : MINT_STRMFLD);
1979 ADD_CODE(&td, get_data_item_index (&td, field));
1981 ADD_CODE(&td, MINT_STFLD_I1 + mt - MINT_TYPE_I1);
1982 ADD_CODE(&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
1984 if (mt == MINT_TYPE_VT) {
1985 MonoClass *klass = mono_class_from_mono_type (field->type);
1986 int size = mono_class_value_size (klass, NULL);
1988 WRITE32(&td, &size);
1994 token = read32 (td.ip + 1);
1995 field = mono_field_from_token (image, token, &klass, generic_context);
1996 ADD_CODE(&td, MINT_LDSFLDA);
1997 ADD_CODE(&td, get_data_item_index (&td, field));
1999 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
2002 token = read32 (td.ip + 1);
2003 field = mono_field_from_token (image, token, &klass, generic_context);
2004 mt = mint_type(field->type);
2005 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2006 ADD_CODE(&td, get_data_item_index (&td, field));
2008 if (mt == MINT_TYPE_VT) {
2009 g_error ("data.klass");
2010 int size = mono_class_value_size (field->type->data.klass, NULL);
2012 WRITE32(&td, &size);
2013 klass = field->type->data.klass;
2015 if (mt == MINT_TYPE_O)
2016 klass = mono_class_from_mono_type (field->type);
2019 PUSH_TYPE(&td, stack_type [mt], klass);
2022 CHECK_STACK (&td, 1);
2023 token = read32 (td.ip + 1);
2024 field = mono_field_from_token (image, token, &klass, generic_context);
2025 mt = mint_type(field->type);
2026 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
2027 ADD_CODE(&td, get_data_item_index (&td, field));
2028 if (mt == MINT_TYPE_VT) {
2029 g_error ("data.klass");
2030 int size = mono_class_value_size (field->type->data.klass, NULL);
2032 WRITE32(&td, &size);
2039 token = read32 (td.ip + 1);
2041 if (method->wrapper_type != MONO_WRAPPER_NONE)
2042 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2044 klass = mono_class_get_full (image, token, generic_context);
2046 ADD_CODE(&td, td.sp [-1].type == STACK_TYPE_VT ? MINT_STOBJ_VT : MINT_STOBJ);
2047 ADD_CODE(&td, get_data_item_index (&td, klass));
2048 if (td.sp [-1].type == STACK_TYPE_VT) {
2049 size = mono_class_value_size (klass, NULL);
2050 size = (size + 7) & ~7;
2057 case CEE_CONV_OVF_I_UN:
2058 CHECK_STACK (&td, 1);
2059 switch (td.sp [-1].type) {
2061 #if SIZEOF_VOID_P == 8
2062 ADD_CODE(&td, MINT_CONV_OVF_I8_UN_R8);
2064 ADD_CODE(&td, MINT_CONV_OVF_I4_UN_R8);
2071 #if SIZEOF_VOID_P == 8
2072 ADD_CODE(&td, MINT_CONV_I8_U4);
2076 g_assert_not_reached ();
2079 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2082 case CEE_CONV_OVF_I8_UN:
2083 CHECK_STACK (&td, 1);
2084 switch (td.sp [-1].type) {
2086 ADD_CODE(&td, MINT_CONV_OVF_I8_UN_R8);
2091 ADD_CODE(&td, MINT_CONV_I8_U4);
2094 g_assert_not_reached ();
2097 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2102 CHECK_STACK (&td, 1);
2103 token = read32 (td.ip + 1);
2104 if (method->wrapper_type != MONO_WRAPPER_NONE)
2105 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2107 klass = mono_class_get_full (image, token, generic_context);
2108 g_assert (klass->valuetype);
2110 if (mono_class_is_nullable (klass)) {
2111 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Box", 1);
2112 /* td.ip is incremented by interp_transform_call */
2113 interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL);
2115 if (klass->byval_arg.type == MONO_TYPE_VALUETYPE && !klass->enumtype) {
2116 size = mono_class_value_size (klass, NULL);
2117 size = (size + 7) & ~7;
2120 ADD_CODE(&td, MINT_BOX);
2121 ADD_CODE(&td, get_data_item_index (&td, klass));
2123 SET_TYPE(td.sp - 1, STACK_TYPE_O, klass);
2130 CHECK_STACK (&td, 1);
2131 token = read32 (td.ip + 1);
2133 if (method->wrapper_type != MONO_WRAPPER_NONE)
2134 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2136 klass = mono_class_get_full (image, token, generic_context);
2138 ADD_CODE(&td, MINT_NEWARR);
2139 ADD_CODE(&td, get_data_item_index (&td, klass));
2140 SET_TYPE(td.sp - 1, STACK_TYPE_O, klass);
2144 CHECK_STACK (&td, 1);
2145 SIMPLE_OP (td, MINT_LDLEN);
2146 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2149 CHECK_STACK (&td, 2);
2151 token = read32 (td.ip + 1);
2153 if (method->wrapper_type != MONO_WRAPPER_NONE)
2154 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2156 klass = mono_class_get_full (image, token, generic_context);
2158 ADD_CODE(&td, MINT_LDELEMA);
2159 ADD_CODE(&td, get_data_item_index (&td, klass));
2162 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2165 CHECK_STACK (&td, 2);
2167 SIMPLE_OP (td, MINT_LDELEM_I1);
2169 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2172 CHECK_STACK (&td, 2);
2174 SIMPLE_OP (td, MINT_LDELEM_U1);
2176 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2179 CHECK_STACK (&td, 2);
2181 SIMPLE_OP (td, MINT_LDELEM_I2);
2183 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2186 CHECK_STACK (&td, 2);
2188 SIMPLE_OP (td, MINT_LDELEM_U2);
2190 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2193 CHECK_STACK (&td, 2);
2195 SIMPLE_OP (td, MINT_LDELEM_I4);
2197 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2200 CHECK_STACK (&td, 2);
2202 SIMPLE_OP (td, MINT_LDELEM_U4);
2204 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2207 CHECK_STACK (&td, 2);
2209 SIMPLE_OP (td, MINT_LDELEM_I8);
2211 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2214 CHECK_STACK (&td, 2);
2216 SIMPLE_OP (td, MINT_LDELEM_I);
2218 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2221 CHECK_STACK (&td, 2);
2223 SIMPLE_OP (td, MINT_LDELEM_R4);
2225 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2228 CHECK_STACK (&td, 2);
2230 SIMPLE_OP (td, MINT_LDELEM_R8);
2232 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2234 case CEE_LDELEM_REF:
2235 CHECK_STACK (&td, 2);
2237 SIMPLE_OP (td, MINT_LDELEM_REF);
2239 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
2242 CHECK_STACK (&td, 2);
2243 token = read32 (td.ip + 1);
2244 klass = mono_class_get_full (image, token, generic_context);
2245 switch (mint_type (&klass->byval_arg)) {
2248 SIMPLE_OP (td, MINT_LDELEM_I4);
2250 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2252 case MINT_TYPE_VT: {
2253 int size = mono_class_value_size (klass, NULL);
2255 SIMPLE_OP (td, MINT_LDELEM_VT);
2256 ADD_CODE (&td, get_data_item_index (&td, klass));
2257 WRITE32 (&td, &size);
2259 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_VT);
2260 PUSH_VT (&td, size);
2264 GString *res = g_string_new ("");
2265 mono_type_get_desc (res, &klass->byval_arg, TRUE);
2266 g_print ("LDELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
2267 g_string_free (res, TRUE);
2275 CHECK_STACK (&td, 3);
2277 SIMPLE_OP (td, MINT_STELEM_I);
2281 CHECK_STACK (&td, 3);
2283 SIMPLE_OP (td, MINT_STELEM_I1);
2287 CHECK_STACK (&td, 3);
2289 SIMPLE_OP (td, MINT_STELEM_I2);
2293 CHECK_STACK (&td, 3);
2295 SIMPLE_OP (td, MINT_STELEM_I4);
2299 CHECK_STACK (&td, 3);
2301 SIMPLE_OP (td, MINT_STELEM_I8);
2305 CHECK_STACK (&td, 3);
2307 SIMPLE_OP (td, MINT_STELEM_R4);
2311 CHECK_STACK (&td, 3);
2313 SIMPLE_OP (td, MINT_STELEM_R8);
2316 case CEE_STELEM_REF:
2317 CHECK_STACK (&td, 3);
2319 SIMPLE_OP (td, MINT_STELEM_REF);
2323 CHECK_STACK (&td, 3);
2325 token = read32 (td.ip + 1);
2326 klass = mono_class_get_full (image, token, generic_context);
2327 switch (mint_type (&klass->byval_arg)) {
2329 SIMPLE_OP (td, MINT_STELEM_I4);
2332 SIMPLE_OP (td, MINT_STELEM_REF);
2334 case MINT_TYPE_VT: {
2335 int size = mono_class_value_size (klass, NULL);
2336 SIMPLE_OP (td, MINT_STELEM_VT);
2337 ADD_CODE (&td, get_data_item_index (&td, klass));
2338 WRITE32 (&td, &size);
2343 GString *res = g_string_new ("");
2344 mono_type_get_desc (res, &klass->byval_arg, TRUE);
2345 g_print ("STELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
2346 g_string_free (res, TRUE);
2355 case CEE_CONV_OVF_U1:
2357 case CEE_CONV_OVF_I8:
2359 #if SIZEOF_VOID_P == 8
2360 case CEE_CONV_OVF_U:
2362 case CEE_REFANYVAL: ves_abort(); break;
2365 CHECK_STACK (&td, 1);
2366 SIMPLE_OP (td, MINT_CKFINITE);
2368 case CEE_CONV_OVF_I1:
2369 case CEE_CONV_OVF_I1_UN:
2370 CHECK_STACK (&td, 1);
2371 switch (td.sp [-1].type) {
2373 ADD_CODE(&td, MINT_CONV_OVF_I1_R8);
2376 ADD_CODE(&td, MINT_CONV_OVF_I1_I4);
2379 ADD_CODE(&td, MINT_CONV_OVF_I1_I8);
2382 g_assert_not_reached ();
2385 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2387 case CEE_CONV_OVF_U1:
2388 case CEE_CONV_OVF_U1_UN:
2389 CHECK_STACK (&td, 1);
2390 switch (td.sp [-1].type) {
2392 ADD_CODE(&td, MINT_CONV_OVF_U1_R8);
2395 ADD_CODE(&td, MINT_CONV_OVF_U1_I4);
2398 ADD_CODE(&td, MINT_CONV_OVF_U1_I8);
2401 g_assert_not_reached ();
2404 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2406 case CEE_CONV_OVF_I2:
2407 case CEE_CONV_OVF_I2_UN:
2408 CHECK_STACK (&td, 1);
2409 switch (td.sp [-1].type) {
2411 ADD_CODE(&td, MINT_CONV_OVF_I2_R8);
2414 ADD_CODE(&td, MINT_CONV_OVF_I2_I4);
2417 ADD_CODE(&td, MINT_CONV_OVF_I2_I8);
2420 g_assert_not_reached ();
2423 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2425 case CEE_CONV_OVF_U2_UN:
2426 case CEE_CONV_OVF_U2:
2427 CHECK_STACK (&td, 1);
2428 switch (td.sp [-1].type) {
2430 ADD_CODE(&td, MINT_CONV_OVF_U2_R8);
2433 ADD_CODE(&td, MINT_CONV_OVF_U2_I4);
2436 ADD_CODE(&td, MINT_CONV_OVF_U2_I8);
2439 g_assert_not_reached ();
2442 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2444 #if SIZEOF_VOID_P == 4
2445 case CEE_CONV_OVF_I:
2447 case CEE_CONV_OVF_I4:
2448 case CEE_CONV_OVF_I4_UN:
2449 CHECK_STACK (&td, 1);
2450 switch (td.sp [-1].type) {
2452 ADD_CODE(&td, MINT_CONV_OVF_I4_R8);
2455 if (*td.ip == CEE_CONV_OVF_I4_UN)
2456 ADD_CODE(&td, MINT_CONV_OVF_I4_U4);
2459 ADD_CODE(&td, MINT_CONV_OVF_I4_I8);
2462 g_assert_not_reached ();
2465 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2467 #if SIZEOF_VOID_P == 4
2468 case CEE_CONV_OVF_U:
2470 case CEE_CONV_OVF_U4:
2471 case CEE_CONV_OVF_U4_UN:
2472 CHECK_STACK (&td, 1);
2473 switch (td.sp [-1].type) {
2475 ADD_CODE(&td, MINT_CONV_OVF_U4_R8);
2478 if (*td.ip != CEE_CONV_OVF_U4_UN)
2479 ADD_CODE(&td, MINT_CONV_OVF_U4_I4);
2482 ADD_CODE(&td, MINT_CONV_OVF_U4_I8);
2485 g_assert_not_reached ();
2488 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2490 #if SIZEOF_VOID_P == 8
2491 case CEE_CONV_OVF_I:
2493 case CEE_CONV_OVF_I8:
2494 CHECK_STACK (&td, 1);
2495 switch (td.sp [-1].type) {
2497 ADD_CODE(&td, MINT_CONV_OVF_I8_R8);
2500 ADD_CODE(&td, MINT_CONV_I8_I4);
2505 g_assert_not_reached ();
2508 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2510 #if SIZEOF_VOID_P == 8
2511 case CEE_CONV_OVF_U:
2513 case CEE_CONV_OVF_U8:
2514 CHECK_STACK (&td, 1);
2515 switch (td.sp [-1].type) {
2517 ADD_CODE(&td, MINT_CONV_OVF_U8_R8);
2520 ADD_CODE(&td, MINT_CONV_OVF_U8_I4);
2525 g_assert_not_reached ();
2528 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2533 token = read32 (td.ip + 1);
2534 handle = mono_ldtoken (image, token, &klass, generic_context);
2535 mt = mint_type(&klass->byval_arg);
2536 g_assert (mt == MINT_TYPE_VT);
2537 size = mono_class_value_size (klass, NULL);
2538 g_assert (size == sizeof(gpointer));
2539 PUSH_VT(&td, sizeof(gpointer));
2540 ADD_CODE(&td, MINT_LDTOKEN);
2541 ADD_CODE(&td, get_data_item_index (&td, handle));
2542 PUSH_SIMPLE_TYPE(&td, stack_type [mt]);
2547 binary_arith_op(&td, MINT_ADD_OVF_I4);
2550 case CEE_ADD_OVF_UN:
2551 binary_arith_op(&td, MINT_ADD_OVF_UN_I4);
2555 binary_arith_op(&td, MINT_MUL_OVF_I4);
2558 case CEE_MUL_OVF_UN:
2559 binary_arith_op(&td, MINT_MUL_OVF_UN_I4);
2563 binary_arith_op(&td, MINT_SUB_OVF_I4);
2566 case CEE_SUB_OVF_UN:
2567 binary_arith_op(&td, MINT_SUB_OVF_UN_I4);
2570 case CEE_ENDFINALLY:
2571 SIMPLE_OP (td, MINT_ENDFINALLY);
2572 generating_code = 0;
2576 handle_branch (&td, MINT_LEAVE_S, MINT_LEAVE, 5 + read32 (td.ip + 1));
2578 generating_code = 0;
2582 handle_branch (&td, MINT_LEAVE_S, MINT_LEAVE, 2 + (gint8)td.ip [1]);
2584 generating_code = 0;
2589 case CEE_MONO_CALLI_EXTRA_ARG:
2590 /* Same as CEE_CALLI, llvm specific */
2591 interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, NULL);
2593 case CEE_MONO_ICALL: {
2596 MonoJitICallInfo *info;
2598 token = read32 (td.ip + 1);
2600 func = mono_method_get_wrapper_data (method, token);
2601 info = mono_find_jit_icall_by_addr (func);
2604 CHECK_STACK (&td, info->sig->param_count);
2605 switch (info->sig->param_count) {
2607 if (MONO_TYPE_IS_VOID (info->sig->ret))
2608 ADD_CODE (&td,MINT_ICALL_V_V);
2610 ADD_CODE (&td, MINT_ICALL_V_P);
2613 if (MONO_TYPE_IS_VOID (info->sig->ret))
2614 ADD_CODE (&td,MINT_ICALL_P_V);
2616 ADD_CODE (&td,MINT_ICALL_P_P);
2619 if (MONO_TYPE_IS_VOID (info->sig->ret)) {
2620 if (info->sig->params [1]->type == MONO_TYPE_I4)
2621 ADD_CODE (&td,MINT_ICALL_PI_V);
2623 ADD_CODE (&td,MINT_ICALL_PP_V);
2625 if (info->sig->params [1]->type == MONO_TYPE_I4)
2626 ADD_CODE (&td,MINT_ICALL_PI_P);
2628 ADD_CODE (&td,MINT_ICALL_PP_P);
2632 g_assert (MONO_TYPE_IS_VOID (info->sig->ret));
2633 if (info->sig->params [2]->type == MONO_TYPE_I4)
2634 ADD_CODE (&td,MINT_ICALL_PPI_V);
2636 ADD_CODE (&td,MINT_ICALL_PPP_V);
2639 g_assert_not_reached ();
2642 if (func == mono_ftnptr_to_delegate) {
2643 g_error ("TODO: ?");
2644 func = mono_interp_ftnptr_to_delegate;
2646 ADD_CODE(&td, get_data_item_index (&td, func));
2647 td.sp -= info->sig->param_count;
2649 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2651 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2655 case CEE_MONO_VTADDR: {
2657 CHECK_STACK (&td, 1);
2658 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
2659 size = mono_class_native_size(td.sp [-1].klass, NULL);
2661 size = mono_class_value_size(td.sp [-1].klass, NULL);
2662 size = (size + 7) & ~7;
2663 ADD_CODE(&td, MINT_VTRESULT);
2665 WRITE32(&td, &size);
2668 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2671 case CEE_MONO_LDPTR:
2672 case CEE_MONO_CLASSCONST:
2673 token = read32 (td.ip + 1);
2675 ADD_CODE(&td, MINT_MONO_LDPTR);
2676 ADD_CODE(&td, get_data_item_index (&td, mono_method_get_wrapper_data (method, token)));
2677 td.sp [0].type = STACK_TYPE_I;
2680 case CEE_MONO_OBJADDR:
2681 CHECK_STACK (&td, 1);
2683 td.sp[-1].type = STACK_TYPE_MP;
2686 case CEE_MONO_NEWOBJ:
2687 token = read32 (td.ip + 1);
2689 ADD_CODE(&td, MINT_MONO_NEWOBJ);
2690 ADD_CODE(&td, get_data_item_index (&td, mono_method_get_wrapper_data (method, token)));
2691 td.sp [0].type = STACK_TYPE_O;
2694 case CEE_MONO_RETOBJ:
2695 CHECK_STACK (&td, 1);
2696 token = read32 (td.ip + 1);
2698 ADD_CODE(&td, MINT_MONO_RETOBJ);
2701 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2703 /*stackval_from_data (signature->ret, frame->retval, sp->data.vt, signature->pinvoke);*/
2705 if (td.sp > td.stack)
2706 g_warning ("CEE_MONO_RETOBJ: more values on stack: %d", td.sp-td.stack);
2708 case CEE_MONO_LDNATIVEOBJ:
2709 token = read32 (td.ip + 1);
2711 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2712 g_assert(klass->valuetype);
2713 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2715 case CEE_MONO_SAVE_LMF:
2716 case CEE_MONO_RESTORE_LMF:
2717 case CEE_MONO_NOT_TAKEN:
2720 case CEE_MONO_LDPTR_INT_REQ_FLAG:
2721 ADD_CODE (&td, MINT_MONO_LDPTR);
2722 ADD_CODE (&td, get_data_item_index (&td, mono_thread_interruption_request_flag ()));
2723 PUSH_TYPE (&td, STACK_TYPE_MP, NULL);
2727 g_error ("transform.c: Unimplemented opcode: 0xF0 %02x at 0x%x\n", *td.ip, td.ip-header->code);
2737 case CEE_PREFIXREF: ves_abort(); break;
2740 * Note: Exceptions thrown when executing a prefixed opcode need
2741 * to take into account the number of prefix bytes (usually the
2742 * throw point is just (ip - n_prefix_bytes).
2748 case CEE_ARGLIST: ves_abort(); break;
2751 CHECK_STACK(&td, 2);
2752 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2753 ADD_CODE(&td, MINT_CEQ_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2755 ADD_CODE(&td, MINT_CEQ_I4 + td.sp [-1].type - STACK_TYPE_I4);
2757 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2761 CHECK_STACK(&td, 2);
2762 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2763 ADD_CODE(&td, MINT_CGT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2765 ADD_CODE(&td, MINT_CGT_I4 + td.sp [-1].type - STACK_TYPE_I4);
2767 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2771 CHECK_STACK(&td, 2);
2772 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2773 ADD_CODE(&td, MINT_CGT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2775 ADD_CODE(&td, MINT_CGT_UN_I4 + td.sp [-1].type - STACK_TYPE_I4);
2777 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2781 CHECK_STACK(&td, 2);
2782 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2783 ADD_CODE(&td, MINT_CLT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2785 ADD_CODE(&td, MINT_CLT_I4 + td.sp [-1].type - STACK_TYPE_I4);
2787 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2791 CHECK_STACK(&td, 2);
2792 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2793 ADD_CODE(&td, MINT_CLT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2795 ADD_CODE(&td, MINT_CLT_UN_I4 + td.sp [-1].type - STACK_TYPE_I4);
2797 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2800 case CEE_LDVIRTFTN: /* fallthrough */
2803 if (*td.ip == CEE_LDVIRTFTN) {
2804 CHECK_STACK (&td, 1);
2807 token = read32 (td.ip + 1);
2808 if (method->wrapper_type != MONO_WRAPPER_NONE)
2809 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
2811 m = mono_get_method_full (image, token, NULL, generic_context);
2813 if (method->wrapper_type == MONO_WRAPPER_NONE && m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
2814 m = mono_marshal_get_synchronized_wrapper (m);
2816 ADD_CODE(&td, *td.ip == CEE_LDFTN ? MINT_LDFTN : MINT_LDVIRTFTN);
2817 ADD_CODE(&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
2818 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2820 PUSH_SIMPLE_TYPE (&td, STACK_TYPE_F);
2824 load_arg (&td, read16 (td.ip + 1));
2828 int n = read16 (td.ip + 1);
2829 if (n == 0 && signature->hasthis) {
2830 g_error ("LDTHISA: NOPE");
2831 ADD_CODE(&td, MINT_LDTHISA);
2834 ADD_CODE(&td, MINT_LDARGA);
2835 ADD_CODE(&td, td.rtm->arg_offsets [n]); /* FIX for large offsets */
2837 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
2842 store_arg (&td, read16 (td.ip + 1));
2846 load_local (&td, read16 (td.ip + 1));
2850 ADD_CODE(&td, MINT_LDLOCA_S);
2851 ADD_CODE(&td, td.rtm->local_offsets [read16 (td.ip + 1)]);
2852 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
2856 store_local (&td, read16 (td.ip + 1));
2860 CHECK_STACK (&td, 1);
2861 #if SIZEOF_VOID_P == 8
2862 if (td.sp [-1].type == STACK_TYPE_I8)
2863 ADD_CODE(&td, MINT_CONV_I4_I8);
2865 ADD_CODE(&td, MINT_LOCALLOC);
2866 if (td.sp != td.stack + 1)
2867 g_warning("CEE_LOCALLOC: stack not empty");
2869 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2872 case CEE_UNUSED57: ves_abort(); break;
2873 case CEE_ENDFILTER: ves_abort(); break;
2875 case CEE_UNALIGNED_:
2877 /* FIX: should do something? */;
2881 /* FIX: should do something? */;
2885 /* FIX: should do something? */;
2888 CHECK_STACK(&td, 1);
2889 token = read32 (td.ip + 1);
2890 klass = mono_class_get_full (image, token, generic_context);
2891 ADD_CODE(&td, MINT_INITOBJ);
2892 i32 = mono_class_value_size (klass, NULL);
2898 CHECK_STACK(&td, 3);
2899 /* FIX? convert length to I8? */
2900 ADD_CODE(&td, MINT_CPBLK);
2904 case CEE_CONSTRAINED_:
2905 token = read32 (td.ip + 1);
2906 constrained_class = mono_class_get_full (image, token, generic_context);
2907 mono_class_init (constrained_class);
2911 CHECK_STACK(&td, 3);
2912 ADD_CODE(&td, MINT_INITBLK);
2917 /* FIXME: implement */
2922 SIMPLE_OP (td, MINT_RETHROW);
2923 generating_code = 0;
2927 token = read32 (td.ip + 1);
2929 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
2931 MonoType *type = mono_type_create_from_typespec (image, token);
2932 size = mono_type_size (type, &align);
2935 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
2936 mono_class_init (szclass);
2938 if (!szclass->valuetype)
2939 THROW_EX (mono_exception_from_name (mono_defaults.corlib, "System", "InvalidProgramException"), ip - 5);
2941 size = mono_class_value_size (szclass, &align);
2943 ADD_CODE(&td, MINT_LDC_I4);
2944 WRITE32(&td, &size);
2945 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
2949 case CEE_REFANYTYPE: ves_abort(); break;
2952 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);
2956 g_error ("transform.c: Unimplemented opcode: %02x at 0x%x\n", *td.ip, td.ip-header->code);
2959 if (td.new_ip - td.new_code != new_in_start_offset)
2960 td.last_new_ip = td.new_code + new_in_start_offset;
2961 else if (td.is_bb_start [td.in_start - td.il_code])
2962 td.is_bb_start [td.ip - td.il_code] = 1;
2964 td.last_ip = td.in_start;
2967 if (mono_interp_traceopt) {
2968 const guint16 *p = td.new_code;
2969 printf("Runtime method: %p, VT stack size: %d\n", rtm, td.max_vt_sp);
2970 printf("Calculated stack size: %d, stated size: %d\n", td.max_stack_height, header->max_stack);
2971 while (p < td.new_ip) {
2972 p = mono_interp_dis_mintop(td.new_code, p);
2977 rtm->clauses = mono_mempool_alloc (domain->mp, header->num_clauses * sizeof(MonoExceptionClause));
2978 memcpy (rtm->clauses, header->clauses, header->num_clauses * sizeof(MonoExceptionClause));
2979 rtm->code = mono_mempool_alloc (domain->mp, (td.new_ip - td.new_code) * sizeof(gushort));
2980 memcpy (rtm->code, td.new_code, (td.new_ip - td.new_code) * sizeof(gushort));
2981 g_free (td.new_code);
2982 rtm->new_body_start = rtm->code + body_start_offset;
2983 rtm->num_clauses = header->num_clauses;
2984 for (i = 0; i < header->num_clauses; i++) {
2985 MonoExceptionClause *c = rtm->clauses + i;
2986 int end_off = c->try_offset + c->try_len;
2987 c->try_offset = td.in_offsets [c->try_offset];
2988 c->try_len = td.in_offsets [end_off] - c->try_offset;
2989 end_off = c->handler_offset + c->handler_len;
2990 c->handler_offset = td.in_offsets [c->handler_offset];
2991 c->handler_len = td.in_offsets [end_off] - c->handler_offset;
2993 rtm->vt_stack_size = td.max_vt_sp;
2994 rtm->alloca_size = rtm->locals_size + rtm->args_size + rtm->vt_stack_size + rtm->stack_size;
2995 rtm->data_items = mono_mempool_alloc (domain->mp, td.n_data_items * sizeof (td.data_items [0]));
2996 memcpy (rtm->data_items, td.data_items, td.n_data_items * sizeof (td.data_items [0]));
2997 g_free (td.in_offsets);
2998 g_free (td.forward_refs);
2999 for (i = 0; i < header->code_size; ++i)
3000 g_free (td.stack_state [i]);
3001 g_free (td.stack_state);
3002 g_free (td.stack_height);
3003 g_free (td.vt_stack_size);
3004 g_free (td.data_items);
3005 g_hash_table_destroy (td.data_hash);
3008 static mono_mutex_t calc_section;
3011 mono_interp_transform_init (void)
3013 mono_os_mutex_init_recursive(&calc_section);
3017 mono_interp_transform_method (RuntimeMethod *runtime_method, ThreadContext *context)
3019 int i, align, size, offset;
3020 MonoMethod *method = runtime_method->method;
3021 MonoImage *image = method->klass->image;
3022 MonoMethodHeader *header = mono_method_get_header (method);
3023 MonoMethodSignature *signature = mono_method_signature (method);
3024 register const unsigned char *ip, *end;
3025 const MonoOpcode *opcode;
3028 MonoDomain *domain = mono_domain_get ();
3029 unsigned char *is_bb_start;
3031 MonoVTable *method_class_vt;
3033 MonoGenericContext *generic_context = NULL;
3035 // g_printerr ("TRANSFORM(0x%016lx): begin %s::%s\n", mono_thread_current (), method->klass->name, method->name);
3036 method_class_vt = mono_class_vtable (domain, runtime_method->method->klass);
3037 if (!method_class_vt->initialized) {
3039 MonoInvocation *last_env_frame = context->env_frame;
3040 jmp_buf *old_env = context->current_env;
3043 MonoException *failed = context->env_frame->ex;
3044 context->env_frame->ex = NULL;
3045 context->env_frame = last_env_frame;
3046 context->current_env = old_env;
3049 context->env_frame = context->current_frame;
3050 context->current_env = &env;
3051 mono_runtime_class_init (method_class_vt);
3052 context->env_frame = last_env_frame;
3053 context->current_env = old_env;
3056 mono_profiler_method_jit (method); /* sort of... */
3058 if (mono_method_signature (method)->is_inflated)
3059 generic_context = &((MonoMethodInflated *) method)->context;
3061 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
3062 MonoMethod *nm = NULL;
3063 mono_os_mutex_lock(&calc_section);
3064 if (runtime_method->transformed) {
3065 mono_os_mutex_unlock(&calc_section);
3066 g_error ("FIXME: no jit info?");
3067 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3071 /* assumes all internal calls with an array this are built in... */
3072 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL && (! mono_method_signature (method)->hasthis || method->klass->rank == 0)) {
3073 nm = mono_marshal_get_native_wrapper (method, TRUE, FALSE);
3074 signature = mono_method_signature (nm);
3076 const char *name = method->name;
3077 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
3078 if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
3079 nm = mono_marshal_get_delegate_invoke (method, NULL);
3080 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
3081 nm = mono_marshal_get_delegate_begin_invoke (method);
3082 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
3083 nm = mono_marshal_get_delegate_end_invoke (method);
3087 runtime_method->code = g_malloc(sizeof(short));
3088 runtime_method->code[0] = MINT_CALLRUN;
3092 runtime_method->stack_size = sizeof (stackval); /* for tracing */
3093 runtime_method->alloca_size = runtime_method->stack_size;
3094 runtime_method->transformed = TRUE;
3095 mono_os_mutex_unlock(&calc_section);
3096 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3100 header = mono_method_get_header (nm);
3101 mono_os_mutex_unlock(&calc_section);
3103 g_assert ((signature->param_count + signature->hasthis) < 1000);
3104 g_assert (header->max_stack < 10000);
3105 /* intern the strings in the method. */
3107 end = ip + header->code_size;
3109 is_bb_start = g_malloc0(header->code_size);
3110 is_bb_start [0] = 1;
3117 else if (in == 0xf0) {
3119 in = *ip + MONO_CEE_MONO_ICALL;
3121 opcode = &mono_opcodes [in];
3122 switch (opcode->argument) {
3123 case MonoInlineNone:
3126 case MonoInlineString:
3127 if (method->wrapper_type == MONO_WRAPPER_NONE)
3128 mono_ldstr (domain, image, mono_metadata_token_index (read32 (ip + 1)));
3131 case MonoInlineType:
3132 if (method->wrapper_type == MONO_WRAPPER_NONE) {
3133 class = mono_class_get_full (image, read32 (ip + 1), generic_context);
3134 mono_class_init (class);
3135 /* quick fix to not do this for the fake ptr classes - probably should not be getting the vtable at all here */
3137 g_error ("FIXME: interface method lookup: %s (in method %s)", class->name, method->name);
3138 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE) && class->interface_offsets != NULL)
3139 mono_class_vtable (domain, class);
3144 case MonoInlineMethod:
3145 if (method->wrapper_type == MONO_WRAPPER_NONE && *ip != CEE_CALLI) {
3146 m = mono_get_method_full (image, read32 (ip + 1), NULL, generic_context);
3148 g_free (is_bb_start);
3149 g_error ("FIXME: where to get method and class string?");
3151 // return mono_get_exception_missing_method ();
3153 mono_class_init (m->klass);
3154 if (!mono_class_is_interface (m->klass))
3155 mono_class_vtable (domain, m->klass);
3159 case MonoInlineField:
3163 case MonoShortInlineR:
3166 case MonoInlineBrTarget:
3167 offset = read32 (ip + 1);
3169 backwards = offset < 0;
3170 offset += ip - header->code;
3171 g_assert (offset >= 0 && offset < header->code_size);
3172 is_bb_start [offset] |= backwards ? 2 : 1;
3174 case MonoShortInlineBrTarget:
3175 offset = ((gint8 *)ip) [1];
3177 backwards = offset < 0;
3178 offset += ip - header->code;
3179 g_assert (offset >= 0 && offset < header->code_size);
3180 is_bb_start [offset] |= backwards ? 2 : 1;
3185 case MonoShortInlineVar:
3186 case MonoShortInlineI:
3189 case MonoInlineSwitch: {
3191 const unsigned char *next_ip;
3195 next_ip = ip + 4 * n;
3196 for (i = 0; i < n; i++) {
3197 offset = read32 (ip);
3198 backwards = offset < 0;
3199 offset += next_ip - header->code;
3200 g_assert (offset >= 0 && offset < header->code_size);
3201 is_bb_start [offset] |= backwards ? 2 : 1;
3211 g_assert_not_reached ();
3214 // g_printerr ("TRANSFORM(0x%016lx): end %s::%s\n", mono_thread_current (), method->klass->name, method->name);
3216 /* the rest needs to be locked so it is only done once */
3217 mono_os_mutex_lock(&calc_section);
3218 if (runtime_method->transformed) {
3219 mono_os_mutex_unlock(&calc_section);
3220 g_free (is_bb_start);
3221 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3225 runtime_method->local_offsets = g_malloc (header->num_locals * sizeof(guint32));
3226 runtime_method->stack_size = (sizeof (stackval) + 2) * header->max_stack; /* + 1 for returns of called functions + 1 for 0-ing in trace*/
3227 runtime_method->stack_size = (runtime_method->stack_size + 7) & ~7;
3229 for (i = 0; i < header->num_locals; ++i) {
3230 size = mono_type_size (header->locals [i], &align);
3231 offset += align - 1;
3232 offset &= ~(align - 1);
3233 runtime_method->local_offsets [i] = offset;
3236 offset = (offset + 7) & ~7;
3237 runtime_method->locals_size = offset;
3238 g_assert (runtime_method->locals_size < 65536);
3240 runtime_method->arg_offsets = g_malloc ((!!signature->hasthis + signature->param_count) * sizeof(guint32));
3242 if (signature->hasthis) {
3243 g_assert (!signature->pinvoke);
3244 size = mono_type_stack_size (&method->klass->byval_arg, &align);
3245 offset += align - 1;
3246 offset &= ~(align - 1);
3247 runtime_method->arg_offsets [0] = offset;
3251 for (i = 0; i < signature->param_count; ++i) {
3252 if (signature->pinvoke) {
3254 size = mono_type_native_stack_size (signature->params [i], &dummy);
3258 size = mono_type_stack_size (signature->params [i], &align);
3259 offset += align - 1;
3260 offset &= ~(align - 1);
3261 runtime_method->arg_offsets [i + !!signature->hasthis] = offset;
3264 offset = (offset + 7) & ~7;
3265 runtime_method->args_size = offset;
3266 g_assert (runtime_method->args_size < 10000);
3268 generate(method, runtime_method, is_bb_start);
3270 g_free (is_bb_start);
3272 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3273 runtime_method->transformed = TRUE;
3274 mono_os_mutex_unlock(&calc_section);