[interp] intrinsify System.Array.UnsafeLoad
[mono.git] / mono / mini / interp / transform.c
index d76c9af55a7106f54ad06fd220174e5a14bdf4fd..9354aa8737c61109ddc52c4e8e9744d18e05b506 100644 (file)
@@ -64,6 +64,7 @@ typedef struct
        int max_data_items;
        void **data_items;
        GHashTable *data_hash;
+       int *clause_indexes;
 } TransformData;
 
 #define MINT_TYPE_I1 0
@@ -258,17 +259,6 @@ binary_arith_op(TransformData *td, int mint_op)
        --td->sp;
 }
 
-static void
-binary_int_op(TransformData *td, int mint_op)
-{
-       int op = mint_op + td->sp [-1].type - STACK_TYPE_I4;
-       CHECK_STACK(td, 2);
-       if (td->sp [-1].type != td->sp [-2].type)
-               g_warning("%s.%s: int type mismatch", td->method->klass->name, td->method->name);
-       ADD_CODE(td, op);
-       --td->sp;
-}
-
 static void
 shift_op(TransformData *td, int mint_op)
 {
@@ -331,6 +321,8 @@ enum_type:
                        goto enum_type;
                } else
                        return MINT_TYPE_VT;
+       case MONO_TYPE_TYPEDBYREF:
+               return MINT_TYPE_VT;
        case MONO_TYPE_GENERICINST:
                type = &type->data.generic_class->container_class->byval_arg;
                goto enum_type;
@@ -803,7 +795,8 @@ interp_transform_call (TransformData *td, MonoMethod *method, MonoMethod *target
        CHECK_STACK (td, csignature->param_count + csignature->hasthis);
        if (!calli && (!virtual || (target_method->flags & METHOD_ATTRIBUTE_VIRTUAL) == 0) &&
                (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 && 
-               (target_method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) == 0) {
+               (target_method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) == 0 &&
+               !(target_method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING)) {
                int called_inited = mono_class_vtable (domain, target_method->klass)->initialized;
                MonoMethodHeader *mheader = mono_method_get_header (target_method);
 
@@ -827,9 +820,10 @@ interp_transform_call (TransformData *td, MonoMethod *method, MonoMethod *target
                        if (mheader && *mheader->code == CEE_RET && called_inited) {
                                if (mono_interp_traceopt)
                                        g_print ("Inline (empty) call of %s.%s\n", target_method->klass->name, target_method->name);
-                               for (i = 0; i < csignature->param_count; i++)
+                               for (i = 0; i < csignature->param_count; i++) {
                                        ADD_CODE (td, MINT_POP); /*FIX: vt */
                                        ADD_CODE (td, 0);
+                               }
                                if (csignature->hasthis) {
                                        if (virtual)
                                                ADD_CODE(td, MINT_CKNULL);
@@ -932,6 +926,35 @@ interp_field_from_token (MonoMethod *method, guint32 token, MonoClass **klass, M
        return field;
 }
 
+static void
+interp_save_debug_info (RuntimeMethod *rtm, MonoMethodHeader *header, TransformData *td, GArray *line_numbers)
+{
+       MonoDebugMethodJitInfo *dinfo;
+       int i;
+
+       if (!mono_debug_enabled ())
+               return;
+
+       /*
+        * We save the debug info in the same way the JIT does it, treating the interpreter IR as the native code.
+        */
+
+       dinfo = g_new0 (MonoDebugMethodJitInfo, 1);
+       dinfo->num_locals = header->num_locals;
+       dinfo->locals = g_new0 (MonoDebugVarInfo, header->num_locals);
+       dinfo->code_start = (guint8*)rtm->code;
+       dinfo->code_size = td->new_ip - td->new_code;
+       dinfo->epilogue_begin = 0;
+       dinfo->has_var_info = FALSE;
+       dinfo->num_line_numbers = line_numbers->len;
+       dinfo->line_numbers = g_new0 (MonoDebugLineNumberEntry, dinfo->num_line_numbers);
+       for (i = 0; i < dinfo->num_line_numbers; i++)
+               dinfo->line_numbers [i] = g_array_index (line_numbers, MonoDebugLineNumberEntry, i);
+       mono_debug_add_method (rtm->method, dinfo, mono_domain_get ());
+
+       mono_debug_free_method_jit_info (dinfo);
+}
+
 static void
 generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, MonoGenericContext *generic_context)
 {
@@ -952,6 +975,7 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
        guint32 token;
        TransformData td;
        int generating_code = 1;
+       GArray *line_numbers;
 
        memset(&td, 0, sizeof(td));
        td.method = method;
@@ -972,10 +996,12 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
        td.max_data_items = 0;
        td.data_items = NULL;
        td.data_hash = g_hash_table_new (NULL, NULL);
+       td.clause_indexes = g_malloc (header->code_size * sizeof (int));
        rtm->data_items = td.data_items;
        for (i = 0; i < header->code_size; i++) {
                td.forward_refs [i] = -1;
                td.stack_height [i] = -1;
+               td.clause_indexes [i] = -1;
        }
        td.new_ip = td.new_code;
        td.last_new_ip = NULL;
@@ -984,6 +1010,8 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
        td.sp = td.stack;
        td.max_stack_height = 0;
 
+       line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
+
        for (i = 0; i < header->num_clauses; i++) {
                MonoExceptionClause *c = header->clauses + i;
                td.stack_height [c->handler_offset] = 0;
@@ -1005,6 +1033,13 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                        td.stack_state [c->data.filter_offset][0].type = STACK_TYPE_O;
                        td.stack_state [c->data.filter_offset][0].klass = NULL; /*FIX*/
                }
+
+               if ((c->flags & MONO_EXCEPTION_CLAUSE_FINALLY) || (c->flags & MONO_EXCEPTION_CLAUSE_FAULT)) {
+                       for (int j = c->handler_offset; j < c->handler_offset + c->handler_len; ++j) {
+                               if (td.clause_indexes [j] == -1)
+                                       td.clause_indexes [j] = i;
+                       }
+               }
        }
 
        td.ip = header->code;
@@ -1043,6 +1078,12 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                td.in_offsets [in_offset] = td.new_ip - td.new_code;
                new_in_start_offset = td.new_ip - td.new_code;
                td.in_start = td.ip;
+
+               MonoDebugLineNumberEntry lne;
+               lne.native_offset = td.new_ip - td.new_code;
+               lne.il_offset = td.ip - header->code;
+               g_array_append_val (line_numbers, lne);
+
                while (td.forward_refs [in_offset] >= 0) {
                        int j = td.forward_refs [in_offset];
                        int slot;
@@ -1567,23 +1608,23 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                        ++td.ip;
                        break;
                case CEE_REM:
-                       binary_int_op (&td, MINT_REM_I4);
+                       binary_arith_op (&td, MINT_REM_I4);
                        ++td.ip;
                        break;
                case CEE_REM_UN:
-                       binary_int_op (&td, MINT_REM_UN_I4);
+                       binary_arith_op (&td, MINT_REM_UN_I4);
                        ++td.ip;
                        break;
                case CEE_AND:
-                       binary_int_op (&td, MINT_AND_I4);
+                       binary_arith_op (&td, MINT_AND_I4);
                        ++td.ip;
                        break;
                case CEE_OR:
-                       binary_int_op (&td, MINT_OR_I4);
+                       binary_arith_op (&td, MINT_OR_I4);
                        ++td.ip;
                        break;
                case CEE_XOR:
-                       binary_int_op (&td, MINT_XOR_I4);
+                       binary_arith_op (&td, MINT_XOR_I4);
                        ++td.ip;
                        break;
                case CEE_SHL:
@@ -2226,6 +2267,9 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                        case STACK_TYPE_I4:
 #if SIZEOF_VOID_P == 8
                                ADD_CODE(&td, MINT_CONV_I8_U4);
+#elif SIZEOF_VOID_P == 4
+                               if (*td.ip == CEE_CONV_OVF_I_UN)
+                                       ADD_CODE(&td, MINT_CONV_OVF_I4_U4);
 #endif
                                break;
                        default:
@@ -2595,12 +2639,37 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
 #if SIZEOF_VOID_P == 8
                case CEE_CONV_OVF_U:
 #endif
-               case CEE_REFANYVAL: ves_abort(); break;
 #endif
                case CEE_CKFINITE:
                        CHECK_STACK (&td, 1);
                        SIMPLE_OP (td, MINT_CKFINITE);
                        break;
+               case CEE_MKREFANY:
+                       CHECK_STACK (&td, 1);
+                       ENSURE_I4 (&td, 2);
+                       token = read32 (td.ip + 1);
+                       klass = mini_get_class (method, token, generic_context);
+
+                       ADD_CODE (&td, MINT_MKREFANY);
+                       ADD_CODE (&td, get_data_item_index (&td, klass));
+
+                       td.ip += 5;
+                       PUSH_VT (&td, sizeof (MonoTypedRef));
+                       SET_TYPE(td.sp - 1, STACK_TYPE_VT, mono_defaults.typed_reference_class);
+                       break;
+               case CEE_REFANYVAL: {
+                       CHECK_STACK (&td, 1);
+                       ENSURE_I4 (&td, 2);
+                       token = read32 (td.ip + 1);
+
+                       ADD_CODE (&td, MINT_REFANYVAL);
+
+                       POP_VT (&td, sizeof (MonoTypedRef));
+                       SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
+
+                       td.ip += 5;
+                       break;
+               }
                case CEE_CONV_OVF_I1:
                case CEE_CONV_OVF_I1_UN:
                        CHECK_STACK (&td, 1);
@@ -2823,8 +2892,10 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                        ++td.ip;
                        break;
                case CEE_ENDFINALLY:
+                       g_assert (td.clause_indexes [in_offset] != -1);
                        td.sp = td.stack;
                        SIMPLE_OP (td, MINT_ENDFINALLY);
+                       ADD_CODE (&td, td.clause_indexes [in_offset]);
                        generating_code = 0;
                        break;
                case CEE_LEAVE:
@@ -3248,9 +3319,13 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                                PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
                                break;
                        }
-#if 0
-                       case CEE_REFANYTYPE: ves_abort(); break;
-#endif
+                       case CEE_REFANYTYPE:
+                               ADD_CODE (&td, MINT_REFANYTYPE);
+                               td.ip += 1;
+                               POP_VT (&td, sizeof (MonoTypedRef));
+                               PUSH_VT (&td, sizeof (gpointer));
+                               SET_TYPE(td.sp - 1, STACK_TYPE_VT, NULL);
+                               break;
                        default:
                                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);
                        }
@@ -3278,6 +3353,8 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
        }
        g_assert (td.max_stack_height <= (header->max_stack + 1));
 
+       int code_len = td.new_ip - td.new_code;
+
        rtm->clauses = mono_domain_alloc0 (domain, header->num_clauses * sizeof (MonoExceptionClause));
        memcpy (rtm->clauses, header->clauses, header->num_clauses * sizeof(MonoExceptionClause));
        rtm->code = mono_domain_alloc0 (domain, (td.new_ip - td.new_code) * sizeof (gushort));
@@ -3300,6 +3377,29 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
        rtm->alloca_size = rtm->locals_size + rtm->args_size + rtm->vt_stack_size + rtm->stack_size;
        rtm->data_items = mono_domain_alloc0 (domain, td.n_data_items * sizeof (td.data_items [0]));
        memcpy (rtm->data_items, td.data_items, td.n_data_items * sizeof (td.data_items [0]));
+
+       /* Save debug info */
+       interp_save_debug_info (rtm, header, &td, line_numbers);
+
+       /* Create a MonoJitInfo for the interpreted method by creating the interpreter IR as the native code. */
+       int jinfo_len = mono_jit_info_size (0, header->num_clauses, 0);
+       MonoJitInfo *jinfo = (MonoJitInfo *)mono_domain_alloc0 (domain, jinfo_len);
+       rtm->jinfo = jinfo;
+       mono_jit_info_init (jinfo, method, (guint8*)rtm->code, code_len, 0, header->num_clauses, 0);
+       for (i = 0; i < jinfo->num_clauses; ++i) {
+               MonoJitExceptionInfo *ei = &jinfo->clauses [i];
+               MonoExceptionClause *c = rtm->clauses + i;
+
+               ei->flags = c->flags;
+               ei->try_start = rtm->code + c->try_offset;
+               ei->try_end = rtm->code + c->try_offset + c->try_len;
+               ei->handler_start = rtm->code + c->handler_offset;
+               if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER || ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
+               } else {
+                       ei->data.catch_class = c->data.catch_class;
+               }
+       }
+
        g_free (td.in_offsets);
        g_free (td.forward_refs);
        for (i = 0; i < header->code_size; ++i)
@@ -3310,6 +3410,8 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
        g_free (td.data_items);
        g_free (td.stack);
        g_hash_table_destroy (td.data_hash);
+       g_free (td.clause_indexes);
+       g_array_free (line_numbers, TRUE);
 }
 
 static mono_mutex_t calc_section;
@@ -3421,7 +3523,7 @@ mono_interp_transform_method (RuntimeMethod *runtime_method, ThreadContext *cont
                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")) {
+               if (!strcmp (method->name, "UnsafeMov") || !strcmp (method->name, "UnsafeLoad")) {
                        mono_os_mutex_lock (&calc_section);
                        if (!runtime_method->transformed) {
                                runtime_method->code = g_malloc (sizeof (short));
@@ -3433,9 +3535,7 @@ mono_interp_transform_method (RuntimeMethod *runtime_method, ThreadContext *cont
                        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)")) {
+               } else if (!strcmp (method->name, "UnsafeStore")) {
                        g_error ("TODO");
                }
        }