[interp] fix stack alloc size
[mono.git] / mono / mini / interp / transform.c
index 480fe0bdb3a38540a1f2e5fedce207efac955ab4..8c2ac6f0a50b28a98bd9def44274a731f8eae1ad 100644 (file)
@@ -1,4 +1,5 @@
-/*
+/**
+ * \file
  * transform CIL into different opcodes for more
  * efficient interpretation
  *
@@ -502,7 +503,7 @@ store_arg(TransformData *td, int n)
                else
                        size = mono_class_value_size (klass, NULL);
                ADD_CODE(td, MINT_STARG_VT);
-               ADD_CODE(td, n);
+               ADD_CODE(td, td->rtm->arg_offsets [n]);
                WRITE32(td, &size);
                if (td->sp [-1].type == STACK_TYPE_VT)
                        POP_VT(td, size);
@@ -635,6 +636,35 @@ get_data_item_index (TransformData *td, void *ptr)
        return index;
 }
 
+static gboolean
+jit_call_supported (MonoMethod *method, MonoMethodSignature *sig)
+{
+       GSList *l;
+
+       if (sig->param_count > 6)
+               return FALSE;
+       if (sig->pinvoke)
+               return FALSE;
+       if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
+               return FALSE;
+       if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
+               return FALSE;
+       if (method->is_inflated)
+               return FALSE;
+       if (method->string_ctor)
+               return FALSE;
+
+       for (l = jit_classes; l; l = l->next) {
+               char *class_name = l->data;
+               // FIXME: Namespaces
+               if (!strcmp (method->klass->name, class_name))
+                       return TRUE;
+       }
+
+       //return TRUE;
+       return FALSE;
+}
+
 static void
 interp_transform_call (TransformData *td, MonoMethod *method, MonoMethod *target_method, MonoDomain *domain, MonoGenericContext *generic_context, unsigned char *is_bb_start, int body_start_offset, MonoClass *constrained_class, gboolean readonly)
 {
@@ -857,6 +887,10 @@ interp_transform_call (TransformData *td, MonoMethod *method, MonoMethod *target
                        ADD_CODE (td, get_data_item_index (td, target_method->klass));
                        ADD_CODE (td, 1 + target_method->klass->rank);
                }
+       } else if (!calli && !virtual && jit_call_supported (target_method, csignature)) {
+               ADD_CODE(td, MINT_JIT_CALL);
+               ADD_CODE(td, get_data_item_index (td, (void *)mono_interp_get_runtime_method (domain, target_method, &error)));
+               mono_error_assert_ok (&error);
        } else {
                if (calli)
                        ADD_CODE(td, native ? MINT_CALLI_NAT : MINT_CALLI);
@@ -864,7 +898,7 @@ interp_transform_call (TransformData *td, MonoMethod *method, MonoMethod *target
                        ADD_CODE(td, is_void ? MINT_VCALLVIRT : MINT_CALLVIRT);
                else
                        ADD_CODE(td, is_void ? MINT_VCALL : MINT_CALL);
-               
+
                if (calli) {
                        ADD_CODE(td, get_data_item_index (td, (void *)csignature));
                } else {
@@ -943,6 +977,17 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                td.stack_state [c->handler_offset] = g_malloc0(sizeof(StackInfo));
                td.stack_state [c->handler_offset][0].type = STACK_TYPE_O;
                td.stack_state [c->handler_offset][0].klass = NULL; /*FIX*/
+
+               if (c->flags & MONO_EXCEPTION_CLAUSE_FILTER) {
+                       td.stack_height [c->data.filter_offset] = 0;
+                       td.vt_stack_size [c->data.filter_offset] = 0;
+                       td.is_bb_start [c->data.filter_offset] = 1;
+
+                       td.stack_height [c->data.filter_offset] = 1;
+                       td.stack_state [c->data.filter_offset] = g_malloc0(sizeof(StackInfo));
+                       td.stack_state [c->data.filter_offset][0].type = STACK_TYPE_O;
+                       td.stack_state [c->data.filter_offset][0].klass = NULL; /*FIX*/
+               }
        }
 
        td.ip = header->code;
@@ -2758,6 +2803,7 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                        ++td.ip;
                        break;
                case CEE_ENDFINALLY:
+                       td.sp = td.stack;
                        SIMPLE_OP (td, MINT_ENDFINALLY);
                        generating_code = 0;
                        break;
@@ -3071,8 +3117,11 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                                break;
 #if 0
                        case CEE_UNUSED57: ves_abort(); break;
-                       case CEE_ENDFILTER: ves_abort(); break;
 #endif
+                       case CEE_ENDFILTER:
+                               ADD_CODE (&td, MINT_ENDFILTER);
+                               ++td.ip;
+                               break;
                        case CEE_UNALIGNED_:
                                ++td.ip;
                                /* FIX: should do something? */;
@@ -3121,6 +3170,7 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                                CHECK_STACK(&td, 3);
                                ADD_CODE(&td, MINT_INITBLK);
                                td.sp -= 3;
+                               td.ip += 1;
                                break;
 #if 0
                        case CEE_NO_:
@@ -3200,6 +3250,8 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                end_off = c->handler_offset + c->handler_len;
                c->handler_offset = td.in_offsets [c->handler_offset];
                c->handler_len = td.in_offsets [end_off] - c->handler_offset;
+               if (c->flags & MONO_EXCEPTION_CLAUSE_FILTER)
+                       c->data.filter_offset = td.in_offsets [c->data.filter_offset];
        }
        rtm->vt_stack_size = td.max_vt_sp;
        rtm->alloca_size = rtm->locals_size + rtm->args_size + rtm->vt_stack_size + rtm->stack_size;
@@ -3308,7 +3360,7 @@ mono_interp_transform_method (RuntimeMethod *runtime_method, ThreadContext *cont
                                } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
                                        nm = mono_marshal_get_delegate_end_invoke (method);
                                }
-                       } 
+                       }
                        if (nm == NULL) {
                                runtime_method->code = g_malloc(sizeof(short));
                                runtime_method->code[0] = MINT_CALLRUN;
@@ -3325,6 +3377,24 @@ mono_interp_transform_method (RuntimeMethod *runtime_method, ThreadContext *cont
                method = nm;
                header = mono_method_get_header (nm);
                mono_os_mutex_unlock(&calc_section);
+       } else if (method->klass == mono_defaults.array_class) {
+               if (!strcmp (method->name, "UnsafeMov")) {
+                       mono_os_mutex_lock (&calc_section);
+                       if (!runtime_method->transformed) {
+                               runtime_method->code = g_malloc (sizeof (short));
+                               runtime_method->code[0] = MINT_CALLRUN;
+                               runtime_method->stack_size = sizeof (stackval); /* for tracing */
+                               runtime_method->alloca_size = runtime_method->stack_size;
+                               runtime_method->transformed = TRUE;
+                       }
+                       mono_os_mutex_unlock(&calc_section);
+                       mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
+                       return NULL;
+               } else if (!strcmp (method->name, "UnsafeStore)")) {
+                       g_error ("TODO");
+               } else if (!strcmp (method->name, "UnsafeLoad)")) {
+                       g_error ("TODO");
+               }
        }
        g_assert ((signature->param_count + signature->hasthis) < 1000);
        g_assert (header->max_stack < 10000);
@@ -3449,7 +3519,7 @@ mono_interp_transform_method (RuntimeMethod *runtime_method, ThreadContext *cont
        }
 
        runtime_method->local_offsets = g_malloc (header->num_locals * sizeof(guint32));
-       runtime_method->stack_size = (sizeof (stackval) + 2) * header->max_stack; /* + 1 for returns of called functions  + 1 for 0-ing in trace*/
+       runtime_method->stack_size = (sizeof (stackval)) * (header->max_stack + 2); /* + 1 for returns of called functions  + 1 for 0-ing in trace*/
        runtime_method->stack_size = (runtime_method->stack_size + 7) & ~7;
        offset = 0;
        for (i = 0; i < header->num_locals; ++i) {