[jit] Fix an unpatched jump in the monitor exit trampoline on amd64.
[mono.git] / mono / mini / mini-x86.c
index c84a1a7c82ea57542b1b1979ddc408bdcad5d7ac..8328084702c68fd9cd9996d1037e93d9d56042d1 100644 (file)
@@ -324,7 +324,7 @@ add_valuetype (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, ArgIn
 
                /* Special case structs with only a float member */
                if (info->num_fields == 1) {
-                       int ftype = mini_replace_type (info->fields [0].field->type)->type;
+                       int ftype = mini_type_get_underlying_type (gsctx, info->fields [0].field->type)->type;
                        if ((info->native_size == 8) && (ftype == MONO_TYPE_R8)) {
                                ainfo->storage = ArgValuetypeInReg;
                                ainfo->pair_storage [0] = ArgOnDoubleFpStack;
@@ -392,12 +392,10 @@ get_call_info_internal (MonoGenericSharingContext *gsctx, CallInfo *cinfo, MonoM
        {
                ret_type = mini_type_get_underlying_type (gsctx, sig->ret);
                switch (ret_type->type) {
-               case MONO_TYPE_BOOLEAN:
                case MONO_TYPE_I1:
                case MONO_TYPE_U1:
                case MONO_TYPE_I2:
                case MONO_TYPE_U2:
-               case MONO_TYPE_CHAR:
                case MONO_TYPE_I4:
                case MONO_TYPE_U4:
                case MONO_TYPE_I:
@@ -517,14 +515,12 @@ get_call_info_internal (MonoGenericSharingContext *gsctx, CallInfo *cinfo, MonoM
                }
                ptype = mini_type_get_underlying_type (gsctx, sig->params [i]);
                switch (ptype->type) {
-               case MONO_TYPE_BOOLEAN:
                case MONO_TYPE_I1:
                case MONO_TYPE_U1:
                        add_general (&gr, param_regs, &stack_size, ainfo);
                        break;
                case MONO_TYPE_I2:
                case MONO_TYPE_U2:
-               case MONO_TYPE_CHAR:
                        add_general (&gr, param_regs, &stack_size, ainfo);
                        break;
                case MONO_TYPE_I4:
@@ -590,17 +586,20 @@ get_call_info_internal (MonoGenericSharingContext *gsctx, CallInfo *cinfo, MonoM
                add_general (&gr, param_regs, &stack_size, &cinfo->sig_cookie);
        }
 
+       if (cinfo->vtype_retaddr) {
+               /* if the function returns a struct on stack, the called method already does a ret $0x4 */
+               cinfo->callee_stack_pop = 4;
+       } else if (CALLCONV_IS_STDCALL (sig) && sig->pinvoke) {
+               /* Have to compensate for the stack space popped by the native callee */
+               cinfo->callee_stack_pop = stack_size;
+       }
+
        if (mono_do_x86_stack_align && (stack_size % MONO_ARCH_FRAME_ALIGNMENT) != 0) {
                cinfo->need_stack_align = TRUE;
                cinfo->stack_align_amount = MONO_ARCH_FRAME_ALIGNMENT - (stack_size % MONO_ARCH_FRAME_ALIGNMENT);
                stack_size += cinfo->stack_align_amount;
        }
 
-       if (cinfo->vtype_retaddr) {
-               /* if the function returns a struct on stack, the called method already does a ret $0x4 */
-               cinfo->callee_stack_pop = 4;
-       }
-
        cinfo->stack_usage = stack_size;
        cinfo->reg_usage = gr;
        cinfo->freg_usage = fr;
@@ -721,7 +720,7 @@ mono_arch_tail_call_supported (MonoCompile *cfg, MonoMethodSignature *caller_sig
         * the extra stack space would be left on the stack after the tail call.
         */
        res = c1->stack_usage >= c2->stack_usage;
-       callee_ret = mini_replace_type (callee_sig->ret);
+       callee_ret = mini_get_underlying_type (cfg, callee_sig->ret);
        if (callee_ret && MONO_TYPE_ISSTRUCT (callee_ret) && c2->ret.storage != ArgValuetypeInReg)
                /* An address on the callee's stack is passed as the first argument */
                res = FALSE;
@@ -1209,7 +1208,7 @@ mono_arch_create_vars (MonoCompile *cfg)
        sig = mono_method_signature (cfg->method);
 
        cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
-       sig_ret = mini_replace_type (sig->ret);
+       sig_ret = mini_get_underlying_type (cfg, sig->ret);
 
        if (cinfo->ret.storage == ArgValuetypeInReg)
                cfg->ret_var_is_local = TRUE;
@@ -1424,7 +1423,7 @@ mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
 
        sig = call->signature;
        n = sig->param_count + sig->hasthis;
-       sig_ret = mini_replace_type (sig->ret);
+       sig_ret = mini_get_underlying_type (cfg, sig->ret);
 
        cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
        call->call_info = cinfo;
@@ -1920,7 +1919,7 @@ mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
        MonoInst *ins, *n;
 
        MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
-               MonoInst *last_ins = ins->prev;
+               MonoInst *last_ins = mono_inst_prev (ins, FILTER_IL_SEQ_POINT);
 
                switch (ins->opcode) {
                case OP_IADD_IMM:
@@ -2011,7 +2010,7 @@ mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
                                 * Convert succeeding STORE_MEMBASE_IMM 0 ins to STORE_MEMBASE_REG 
                                 * since it takes 3 bytes instead of 7.
                                 */
-                               for (ins2 = ins->next; ins2; ins2 = ins2->next) {
+                               for (ins2 = mono_inst_next (ins, FILTER_IL_SEQ_POINT); ins2; ins2 = ins2->next) {
                                        if ((ins2->opcode == OP_STORE_MEMBASE_IMM) && (ins2->inst_imm == 0)) {
                                                ins2->opcode = OP_STORE_MEMBASE_REG;
                                                ins2->sreg1 = ins->dreg;
@@ -2289,7 +2288,7 @@ static int tls_gs_offset;
 gboolean
 mono_x86_have_tls_get (void)
 {
-#ifdef __APPLE__
+#ifdef TARGET_MACH
        static gboolean have_tls_get = FALSE;
        static gboolean inited = FALSE;
        guint32 *ins;
@@ -2543,7 +2542,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 
        cpos = bb->max_offset;
 
-       if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
+       if ((cfg->prof_options & MONO_PROFILE_COVERAGE) && cfg->coverage_info) {
                MonoProfileCoverageInfo *cov = cfg->coverage_info;
                g_assert (!cfg->compile_aot);
                cpos += 6;
@@ -3174,7 +3173,8 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        x86_push_membase (code, ins->inst_basereg, 0xf0f0f0f0);
                        break;
                case OP_MOVE:
-                       x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
+                       if (ins->dreg != ins->sreg1)
+                               x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
                        break;
                case OP_TAILCALL: {
                        MonoCallInst *call = (MonoCallInst*)ins;
@@ -3382,6 +3382,10 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        x86_ret (code);
                        break;
                }
+               case OP_GET_EX_OBJ:
+                       if (ins->dreg != X86_EAX)
+                               x86_mov_reg_reg (code, ins->dreg, X86_EAX, sizeof (gpointer));
+                       break;
 
                case OP_LABEL:
                        ins->inst_c0 = code - cfg->native_code;
@@ -3700,6 +3704,14 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_FMOVE:
                        /* Not needed on the fp stack */
                        break;
+               case OP_MOVE_F_TO_I4:
+                       x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE, TRUE);
+                       x86_mov_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, 4);
+                       break;
+               case OP_MOVE_I4_TO_F:
+                       x86_mov_membase_reg (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, ins->sreg1, 4);
+                       x86_fld_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE);
+                       break;
                case OP_FADD:
                        x86_fp_op_reg (code, X86_FADD, 1, TRUE);
                        break;
@@ -4213,14 +4225,9 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        break;
                }
                case OP_MEMORY_BARRIER: {
-                       /* x86 only needs barrier for StoreLoad and FullBarrier */
-                       switch (ins->backend.memory_barrier_kind) {
-                       case StoreLoadBarrier:
-                       case FullBarrier:
-                               /* http://blogs.sun.com/dave/resource/NHM-Pipeline-Blog-V2.txt */
+                       if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ) {
                                x86_prefix (code, X86_LOCK_PREFIX);
                                x86_alu_membase_imm (code, X86_ADD, X86_ESP, 0, 0);
-                               break;
                        }
                        break;
                }
@@ -4320,6 +4327,69 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        x86_cmpxchg_membase_reg (code, ins->sreg1, ins->inst_offset, ins->sreg2);
                        break;
                }
+               case OP_ATOMIC_LOAD_I1: {
+                       x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
+                       break;
+               }
+               case OP_ATOMIC_LOAD_U1: {
+                       x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
+                       break;
+               }
+               case OP_ATOMIC_LOAD_I2: {
+                       x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
+                       break;
+               }
+               case OP_ATOMIC_LOAD_U2: {
+                       x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
+                       break;
+               }
+               case OP_ATOMIC_LOAD_I4:
+               case OP_ATOMIC_LOAD_U4: {
+                       x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
+                       break;
+               }
+               case OP_ATOMIC_LOAD_R4:
+               case OP_ATOMIC_LOAD_R8: {
+                       x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, ins->opcode == OP_ATOMIC_LOAD_R8);
+                       break;
+               }
+               case OP_ATOMIC_STORE_I1:
+               case OP_ATOMIC_STORE_U1:
+               case OP_ATOMIC_STORE_I2:
+               case OP_ATOMIC_STORE_U2:
+               case OP_ATOMIC_STORE_I4:
+               case OP_ATOMIC_STORE_U4: {
+                       int size;
+
+                       switch (ins->opcode) {
+                       case OP_ATOMIC_STORE_I1:
+                       case OP_ATOMIC_STORE_U1:
+                               size = 1;
+                               break;
+                       case OP_ATOMIC_STORE_I2:
+                       case OP_ATOMIC_STORE_U2:
+                               size = 2;
+                               break;
+                       case OP_ATOMIC_STORE_I4:
+                       case OP_ATOMIC_STORE_U4:
+                               size = 4;
+                               break;
+                       }
+
+                       x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, size);
+
+                       if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
+                               x86_mfence (code);
+                       break;
+               }
+               case OP_ATOMIC_STORE_R4:
+               case OP_ATOMIC_STORE_R8: {
+                       x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, ins->opcode == OP_ATOMIC_STORE_R8, TRUE);
+
+                       if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
+                               x86_mfence (code);
+                       break;
+               }
                case OP_CARD_TABLE_WBARRIER: {
                        int ptr = ins->sreg1;
                        int value = ins->sreg2;
@@ -4863,10 +4933,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_XZERO:
                        x86_sse_alu_pd_reg_reg (code, X86_SSE_PXOR, ins->dreg, ins->dreg);
                        break;
-               case OP_ICONV_TO_R8_RAW:
-                       x86_mov_membase_reg (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, ins->sreg1, 4);
-                       x86_fld_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE);
-                       break;
 
                case OP_FCONV_TO_R8_X:
                        x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
@@ -5056,6 +5122,7 @@ mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, Mono
                case MONO_PATCH_INFO_RGCTX_FETCH:
                case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
                case MONO_PATCH_INFO_MONITOR_ENTER:
+               case MONO_PATCH_INFO_MONITOR_ENTER_V4:
                case MONO_PATCH_INFO_MONITOR_EXIT:
                case MONO_PATCH_INFO_JIT_ICALL_ADDR:
 #if defined(__native_client_codegen__) && defined(__native_client__)
@@ -5882,7 +5949,7 @@ mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMetho
                        opcode = OP_ROUND;
                }
                
-               if (opcode) {
+               if (opcode && fsig->param_count == 1) {
                        MONO_INST_NEW (cfg, ins, opcode);
                        ins->type = STACK_R8;
                        ins->dreg = mono_alloc_freg (cfg);
@@ -5891,7 +5958,7 @@ mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMetho
                }
 
                if (cfg->opt & MONO_OPT_CMOV) {
-                       int opcode = 0;
+                       opcode = 0;
 
                        if (strcmp (cmethod->name, "Min") == 0) {
                                if (fsig->params [0]->type == MONO_TYPE_I4)
@@ -5901,7 +5968,7 @@ mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMetho
                                        opcode = OP_IMAX;
                        }               
 
-                       if (opcode) {
+                       if (opcode && fsig->param_count == 2) {
                                MONO_INST_NEW (cfg, ins, opcode);
                                ins->type = STACK_I4;
                                ins->dreg = mono_alloc_ireg (cfg);
@@ -5913,7 +5980,7 @@ mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMetho
 
 #if 0
                /* OP_FREM is not IEEE compatible */
-               else if (strcmp (cmethod->name, "IEEERemainder") == 0) {
+               else if (strcmp (cmethod->name, "IEEERemainder") == 0 && fsig->param_count == 2) {
                        MONO_INST_NEW (cfg, ins, OP_FREM);
                        ins->inst_i0 = args [0];
                        ins->inst_i1 = args [1];
@@ -5975,8 +6042,6 @@ mono_arch_get_patch_offset (guint8 *code)
 gboolean
 mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size)
 {
-       int i;
-       gboolean can_write = TRUE;
        /*
         * If method_start is non-NULL we need to perform bound checks, since we access memory
         * at code - offset we could go before the start of the method and end up in a different
@@ -5990,21 +6055,7 @@ mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guin
                memset (buf, 0, size);
                memcpy (buf + offset - diff, method_start, diff + size - offset);
        }
-       code -= offset;
-       for (i = 0; i < MONO_BREAKPOINT_ARRAY_SIZE; ++i) {
-               int idx = mono_breakpoint_info_index [i];
-               guint8 *ptr;
-               if (idx < 1)
-                       continue;
-               ptr = mono_breakpoint_info [idx].address;
-               if (ptr >= code && ptr < code + size) {
-                       guint8 saved_byte = mono_breakpoint_info [idx].saved_byte;
-                       can_write = FALSE;
-                       /*g_print ("patching %p with 0x%02x (was: 0x%02x)\n", ptr, saved_byte, buf [ptr - code]);*/
-                       buf [ptr - code] = saved_byte;
-               }
-       }
-       return can_write;
+       return TRUE;
 }
 
 /*
@@ -6712,6 +6763,22 @@ mono_arch_opcode_supported (int opcode)
        case OP_ATOMIC_ADD_I4:
        case OP_ATOMIC_EXCHANGE_I4:
        case OP_ATOMIC_CAS_I4:
+       case OP_ATOMIC_LOAD_I1:
+       case OP_ATOMIC_LOAD_I2:
+       case OP_ATOMIC_LOAD_I4:
+       case OP_ATOMIC_LOAD_U1:
+       case OP_ATOMIC_LOAD_U2:
+       case OP_ATOMIC_LOAD_U4:
+       case OP_ATOMIC_LOAD_R4:
+       case OP_ATOMIC_LOAD_R8:
+       case OP_ATOMIC_STORE_I1:
+       case OP_ATOMIC_STORE_I2:
+       case OP_ATOMIC_STORE_I4:
+       case OP_ATOMIC_STORE_U1:
+       case OP_ATOMIC_STORE_U2:
+       case OP_ATOMIC_STORE_U4:
+       case OP_ATOMIC_STORE_R4:
+       case OP_ATOMIC_STORE_R8:
                return TRUE;
        default:
                return FALSE;