Merge pull request #1857 from slluis/fix-assembly-resolver
[mono.git] / mono / mini / mini-amd64.c
old mode 100755 (executable)
new mode 100644 (file)
index 1a31eab..c1332ec
@@ -32,6 +32,7 @@
 #include <mono/utils/mono-memory-model.h>
 #include <mono/utils/mono-tls.h>
 #include <mono/utils/mono-hwcap-x86.h>
+#include <mono/utils/mono-threads.h>
 
 #include "trace.h"
 #include "ir-emit.h"
@@ -52,7 +53,7 @@ static gboolean optimize_for_xen = TRUE;
 
 #define IS_REX(inst) (((inst) >= 0x40) && ((inst) <= 0x4f))
 
-#ifdef HOST_WIN32
+#ifdef TARGET_WIN32
 /* Under windows, the calling convention is never stdcall */
 #define CALLCONV_IS_STDCALL(call_conv) (FALSE)
 #else
@@ -85,6 +86,9 @@ static int breakpoint_fault_size;
 /* The size of the single step instruction causing the actual fault */
 static int single_step_fault_size;
 
+/* The single step trampoline */
+static gpointer ss_trampoline;
+
 /* Offset between fp and the first argument in the callee */
 #define ARGS_OFFSET 16
 #define GP_SCRATCH_REG AMD64_R11
@@ -523,7 +527,7 @@ typedef struct {
 
 #define DEBUG(a) if (cfg->verbose_level > 1) a
 
-#ifdef HOST_WIN32
+#ifdef TARGET_WIN32
 static AMD64_Reg_No param_regs [] = { AMD64_RCX, AMD64_RDX, AMD64_R8, AMD64_R9 };
 
 static AMD64_Reg_No return_regs [] = { AMD64_RAX, AMD64_RDX };
@@ -551,7 +555,7 @@ add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
     }
 }
 
-#ifdef HOST_WIN32
+#ifdef TARGET_WIN32
 #define FLOAT_PARAM_REGS 4
 #else
 #define FLOAT_PARAM_REGS 8
@@ -615,7 +619,7 @@ merge_argument_class_from_type (MonoGenericSharingContext *gsctx, MonoType *type
                break;
        case MONO_TYPE_R4:
        case MONO_TYPE_R8:
-#ifdef HOST_WIN32
+#ifdef TARGET_WIN32
                class2 = ARG_CLASS_INTEGER;
 #else
                class2 = ARG_CLASS_SSE;
@@ -748,7 +752,7 @@ add_valuetype (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, ArgIn
 
        klass = mono_class_from_mono_type (type);
        size = mini_type_stack_size_full (gsctx, &klass->byval_arg, NULL, sig->pinvoke);
-#ifndef HOST_WIN32
+#ifndef TARGET_WIN32
        if (!sig->pinvoke && ((is_return && (size == 8)) || (!is_return && (size <= 16)))) {
                /* We pass and return vtypes of size 8 in a register */
        } else if (!sig->pinvoke || (size == 0) || (size > 16)) {
@@ -830,7 +834,7 @@ add_valuetype (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, ArgIn
                g_assert (info);
                g_assert (fields);
 
-#ifndef HOST_WIN32
+#ifndef TARGET_WIN32
                if (info->native_size > 16) {
                        ainfo->offset = *stack_size;
                        *stack_size += ALIGN_TO (info->native_size, 8);
@@ -1003,7 +1007,7 @@ get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSign
        gr = 0;
        fr = 0;
 
-#ifdef HOST_WIN32
+#ifdef TARGET_WIN32
        /* Reserve space where the callee can save the argument registers */
        stack_size = 4 * sizeof (mgreg_t);
 #endif
@@ -1112,7 +1116,7 @@ get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSign
                ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
                MonoType *ptype;
 
-#ifdef HOST_WIN32
+#ifdef TARGET_WIN32
                /* The float param registers and other param registers must be the same index on Windows x64.*/
                if (gr > fr)
                        fr = gr;
@@ -1505,43 +1509,21 @@ mono_arch_get_global_int_regs (MonoCompile *cfg)
 
        mono_arch_compute_omit_fp (cfg);
 
-       if (cfg->globalra) {
-               if (cfg->arch.omit_fp)
-                       regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
-               regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
-               regs = g_list_prepend (regs, (gpointer)AMD64_R12);
-               regs = g_list_prepend (regs, (gpointer)AMD64_R13);
-               regs = g_list_prepend (regs, (gpointer)AMD64_R14);
-#ifndef __native_client_codegen__
-               regs = g_list_prepend (regs, (gpointer)AMD64_R15);
-#endif
-               regs = g_list_prepend (regs, (gpointer)AMD64_R10);
-               regs = g_list_prepend (regs, (gpointer)AMD64_R9);
-               regs = g_list_prepend (regs, (gpointer)AMD64_R8);
-               regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
-               regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
-               regs = g_list_prepend (regs, (gpointer)AMD64_RDX);
-               regs = g_list_prepend (regs, (gpointer)AMD64_RCX);
-               regs = g_list_prepend (regs, (gpointer)AMD64_RAX);
-       } else {
-               if (cfg->arch.omit_fp)
-                       regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
+       if (cfg->arch.omit_fp)
+               regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
 
-               /* We use the callee saved registers for global allocation */
-               regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
-               regs = g_list_prepend (regs, (gpointer)AMD64_R12);
-               regs = g_list_prepend (regs, (gpointer)AMD64_R13);
-               regs = g_list_prepend (regs, (gpointer)AMD64_R14);
+       /* We use the callee saved registers for global allocation */
+       regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
+       regs = g_list_prepend (regs, (gpointer)AMD64_R12);
+       regs = g_list_prepend (regs, (gpointer)AMD64_R13);
+       regs = g_list_prepend (regs, (gpointer)AMD64_R14);
 #ifndef __native_client_codegen__
-               regs = g_list_prepend (regs, (gpointer)AMD64_R15);
+       regs = g_list_prepend (regs, (gpointer)AMD64_R15);
 #endif
-#ifdef HOST_WIN32
-               regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
-               regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
+#ifdef TARGET_WIN32
+       regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
+       regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
 #endif
-       }
 
        return regs;
 }
@@ -1772,24 +1754,19 @@ mono_arch_allocate_vars (MonoCompile *cfg)
                case ArgInFloatSSEReg:
                case ArgInDoubleSSEReg:
                        if ((MONO_TYPE_ISSTRUCT (sig_ret) && !mono_class_from_mono_type (sig_ret)->enumtype) || ((sig_ret->type == MONO_TYPE_TYPEDBYREF) && cinfo->vtype_retaddr)) {
-                               if (cfg->globalra) {
-                                       cfg->vret_addr->opcode = OP_REGVAR;
-                                       cfg->vret_addr->inst_c0 = cinfo->ret.reg;
+                               /* The register is volatile */
+                               cfg->vret_addr->opcode = OP_REGOFFSET;
+                               cfg->vret_addr->inst_basereg = cfg->frame_reg;
+                               if (cfg->arch.omit_fp) {
+                                       cfg->vret_addr->inst_offset = offset;
+                                       offset += 8;
                                } else {
-                                       /* The register is volatile */
-                                       cfg->vret_addr->opcode = OP_REGOFFSET;
-                                       cfg->vret_addr->inst_basereg = cfg->frame_reg;
-                                       if (cfg->arch.omit_fp) {
-                                               cfg->vret_addr->inst_offset = offset;
-                                               offset += 8;
-                                       } else {
-                                               offset += 8;
-                                               cfg->vret_addr->inst_offset = -offset;
-                                       }
-                                       if (G_UNLIKELY (cfg->verbose_level > 1)) {
-                                               printf ("vret_addr =");
-                                               mono_print_ins (cfg->vret_addr);
-                                       }
+                                       offset += 8;
+                                       cfg->vret_addr->inst_offset = -offset;
+                               }
+                               if (G_UNLIKELY (cfg->verbose_level > 1)) {
+                                       printf ("vret_addr =");
+                                       mono_print_ins (cfg->vret_addr);
                                }
                        }
                        else {
@@ -1812,47 +1789,44 @@ mono_arch_allocate_vars (MonoCompile *cfg)
                default:
                        g_assert_not_reached ();
                }
-               if (!cfg->globalra)
-                       cfg->ret->dreg = cfg->ret->inst_c0;
+               cfg->ret->dreg = cfg->ret->inst_c0;
        }
 
        /* Allocate locals */
-       if (!cfg->globalra) {
-               offsets = mono_allocate_stack_slots (cfg, cfg->arch.omit_fp ? FALSE: TRUE, &locals_stack_size, &locals_stack_align);
-               if (locals_stack_size > MONO_ARCH_MAX_FRAME_SIZE) {
-                       char *mname = mono_method_full_name (cfg->method, TRUE);
-                       cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
-                       cfg->exception_message = g_strdup_printf ("Method %s stack is too big.", mname);
-                       g_free (mname);
-                       return;
-               }
+       offsets = mono_allocate_stack_slots (cfg, cfg->arch.omit_fp ? FALSE: TRUE, &locals_stack_size, &locals_stack_align);
+       if (locals_stack_size > MONO_ARCH_MAX_FRAME_SIZE) {
+               char *mname = mono_method_full_name (cfg->method, TRUE);
+               cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
+               cfg->exception_message = g_strdup_printf ("Method %s stack is too big.", mname);
+               g_free (mname);
+               return;
+       }
                
-               if (locals_stack_align) {
-                       offset += (locals_stack_align - 1);
-                       offset &= ~(locals_stack_align - 1);
-               }
-               if (cfg->arch.omit_fp) {
-                       cfg->locals_min_stack_offset = offset;
-                       cfg->locals_max_stack_offset = offset + locals_stack_size;
-               } else {
-                       cfg->locals_min_stack_offset = - (offset + locals_stack_size);
-                       cfg->locals_max_stack_offset = - offset;
-               }
+       if (locals_stack_align) {
+               offset += (locals_stack_align - 1);
+               offset &= ~(locals_stack_align - 1);
+       }
+       if (cfg->arch.omit_fp) {
+               cfg->locals_min_stack_offset = offset;
+               cfg->locals_max_stack_offset = offset + locals_stack_size;
+       } else {
+               cfg->locals_min_stack_offset = - (offset + locals_stack_size);
+               cfg->locals_max_stack_offset = - offset;
+       }
                
-               for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
-                       if (offsets [i] != -1) {
-                               MonoInst *ins = cfg->varinfo [i];
-                               ins->opcode = OP_REGOFFSET;
-                               ins->inst_basereg = cfg->frame_reg;
-                               if (cfg->arch.omit_fp)
-                                       ins->inst_offset = (offset + offsets [i]);
-                               else
-                                       ins->inst_offset = - (offset + offsets [i]);
-                               //printf ("allocated local %d to ", i); mono_print_tree_nl (ins);
-                       }
+       for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
+               if (offsets [i] != -1) {
+                       MonoInst *ins = cfg->varinfo [i];
+                       ins->opcode = OP_REGOFFSET;
+                       ins->inst_basereg = cfg->frame_reg;
+                       if (cfg->arch.omit_fp)
+                               ins->inst_offset = (offset + offsets [i]);
+                       else
+                               ins->inst_offset = - (offset + offsets [i]);
+                       //printf ("allocated local %d to ", i); mono_print_tree_nl (ins);
                }
-               offset += locals_stack_size;
        }
+       offset += locals_stack_size;
 
        if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG)) {
                g_assert (!cfg->arch.omit_fp);
@@ -1866,41 +1840,6 @@ mono_arch_allocate_vars (MonoCompile *cfg)
                        ArgInfo *ainfo = &cinfo->args [i];
                        gboolean inreg = TRUE;
 
-                       if (cfg->globalra) {
-                               /* The new allocator needs info about the original locations of the arguments */
-                               switch (ainfo->storage) {
-                               case ArgInIReg:
-                               case ArgInFloatSSEReg:
-                               case ArgInDoubleSSEReg:
-                                       ins->opcode = OP_REGVAR;
-                                       ins->inst_c0 = ainfo->reg;
-                                       break;
-                               case ArgOnStack:
-                                       g_assert (!cfg->arch.omit_fp);
-                                       ins->opcode = OP_REGOFFSET;
-                                       ins->inst_basereg = cfg->frame_reg;
-                                       ins->inst_offset = ainfo->offset + ARGS_OFFSET;
-                                       break;
-                               case ArgValuetypeInReg:
-                                       ins->opcode = OP_REGOFFSET;
-                                       ins->inst_basereg = cfg->frame_reg;
-                                       /* These arguments are saved to the stack in the prolog */
-                                       offset = ALIGN_TO (offset, sizeof(mgreg_t));
-                                       if (cfg->arch.omit_fp) {
-                                               ins->inst_offset = offset;
-                                               offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
-                                       } else {
-                                               offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
-                                               ins->inst_offset = - offset;
-                                       }
-                                       break;
-                               default:
-                                       g_assert_not_reached ();
-                               }
-
-                               continue;
-                       }
-
                        /* FIXME: Allocate volatile arguments to registers */
                        if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
                                inreg = FALSE;
@@ -2005,13 +1944,17 @@ mono_arch_create_vars (MonoCompile *cfg)
                }
        }
 
-       if (cfg->gen_seq_points_debug_data) {
+       if (cfg->gen_sdb_seq_points) {
                MonoInst *ins;
 
                if (cfg->compile_aot) {
                        MonoInst *ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
                        ins->flags |= MONO_INST_VOLATILE;
                        cfg->arch.seq_point_info_var = ins;
+
+                       ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
+                       ins->flags |= MONO_INST_VOLATILE;
+                       cfg->arch.ss_tramp_var = ins;
                }
 
            ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
@@ -2024,7 +1967,7 @@ mono_arch_create_vars (MonoCompile *cfg)
 
        if (cfg->method->save_lmf) {
                cfg->lmf_ir = TRUE;
-#if !defined(HOST_WIN32)
+#if !defined(TARGET_WIN32)
                if (mono_get_lmf_tls_offset () != -1 && !optimize_for_xen)
                        cfg->lmf_ir_mono_lmf = TRUE;
 #endif
@@ -2995,12 +2938,12 @@ emit_call_body (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointe
 static inline guint8*
 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data, gboolean win64_adjust_stack)
 {
-#ifdef HOST_WIN32
+#ifdef TARGET_WIN32
        if (win64_adjust_stack)
                amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 32);
 #endif
        code = emit_call_body (cfg, code, patch_type, data);
-#ifdef HOST_WIN32
+#ifdef TARGET_WIN32
        if (win64_adjust_stack)
                amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 32);
 #endif 
@@ -3340,7 +3283,7 @@ mono_emit_stack_alloc (MonoCompile *cfg, guchar *code, MonoInst* tree)
        int sreg = tree->sreg1;
        int need_touch = FALSE;
 
-#if defined(HOST_WIN32)
+#if defined(TARGET_WIN32)
        need_touch = TRUE;
 #elif defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
        if (!tree->flags & MONO_INST_INIT)
@@ -3584,7 +3527,7 @@ mono_amd64_get_tls_gs_offset (void)
 guint8*
 mono_amd64_emit_tls_get (guint8* code, int dreg, int tls_offset)
 {
-#ifdef HOST_WIN32
+#ifdef TARGET_WIN32
        if (tls_offset < 64) {
                x86_prefix (code, X86_GS_PREFIX);
                amd64_mov_reg_mem (code, dreg, (tls_offset * 8) + 0x1480, 8);
@@ -3650,7 +3593,7 @@ emit_tls_get_reg (guint8* code, int dreg, int offset_reg)
 static guint8*
 amd64_emit_tls_set (guint8 *code, int sreg, int tls_offset)
 {
-#ifdef HOST_WIN32
+#ifdef TARGET_WIN32
        g_assert_not_reached ();
 #elif defined(__APPLE__)
        x86_prefix (code, X86_GS_PREFIX);
@@ -3667,7 +3610,7 @@ static guint8*
 amd64_emit_tls_set_reg (guint8 *code, int sreg, int offset_reg)
 {
        /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
-#ifdef HOST_WIN32
+#ifdef TARGET_WIN32
        g_assert_not_reached ();
 #elif defined(__APPLE__)
        x86_prefix (code, X86_GS_PREFIX);
@@ -4169,17 +4112,33 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_SEQ_POINT: {
                        int i;
 
-                       /* 
-                        * Read from the single stepping trigger page. This will cause a
-                        * SIGSEGV when single stepping is enabled.
-                        * We do this _before_ the breakpoint, so single stepping after
-                        * a breakpoint is hit will step to the next IL offset.
-                        */
                        if (ins->flags & MONO_INST_SINGLE_STEP_LOC) {
-                               MonoInst *var = cfg->arch.ss_trigger_page_var;
+                               if (cfg->compile_aot) {
+                                       MonoInst *var = cfg->arch.ss_tramp_var;
+                                       guint8 *label;
+
+                                       /* Load ss_tramp_var */
+                                       amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
+                                       /* Load the trampoline address */
+                                       amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
+                                       /* Call it if it is non-null */
+                                       amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
+                                       label = code;
+                                       amd64_branch8 (code, X86_CC_Z, 0, FALSE);
+                                       amd64_call_reg (code, AMD64_R11);
+                                       amd64_patch (label, code);
+                               } else {
+                                       /* 
+                                        * Read from the single stepping trigger page. This will cause a
+                                        * SIGSEGV when single stepping is enabled.
+                                        * We do this _before_ the breakpoint, so single stepping after
+                                        * a breakpoint is hit will step to the next IL offset.
+                                        */
+                                       MonoInst *var = cfg->arch.ss_trigger_page_var;
 
-                               amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
-                               amd64_alu_membase_imm_size (code, X86_CMP, AMD64_R11, 0, 0, 4);
+                                       amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
+                                       amd64_alu_membase_imm_size (code, X86_CMP, AMD64_R11, 0, 0, 4);
+                               }
                        }
 
                        /* 
@@ -4191,13 +4150,19 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                                guint32 offset = code - cfg->native_code;
                                guint32 val;
                                MonoInst *info_var = cfg->arch.seq_point_info_var;
+                               guint8 *label;
 
                                /* Load info var */
                                amd64_mov_reg_membase (code, AMD64_R11, info_var->inst_basereg, info_var->inst_offset, 8);
                                val = ((offset) * sizeof (guint8*)) + MONO_STRUCT_OFFSET (SeqPointInfo, bp_addrs);
-                               /* Load the info->bp_addrs [offset], which is either a valid address or the address of a trigger page */
+                               /* Load the info->bp_addrs [offset], which is either NULL or the address of the breakpoint trampoline */
                                amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, val, 8);
-                               amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
+                               amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
+                               label = code;
+                               amd64_branch8 (code, X86_CC_Z, 0, FALSE);
+                               /* Call the trampoline */
+                               amd64_call_reg (code, AMD64_R11);
+                               amd64_patch (label, code);
                        } else {
                                /* 
                                 * A placeholder for a possible breakpoint inserted by
@@ -4866,6 +4831,28 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        amd64_pop_reg (code, AMD64_RDI);
                        break;
                }
+               case OP_GENERIC_CLASS_INIT: {
+                       static int byte_offset = -1;
+                       static guint8 bitmask;
+                       guint8 *jump;
+
+                       g_assert (ins->sreg1 == MONO_AMD64_ARG_REG1);
+
+                       if (byte_offset < 0)
+                               mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
+
+                       amd64_test_membase_imm_size (code, ins->sreg1, byte_offset, bitmask, 1);
+                       jump = code;
+                       amd64_branch8 (code, X86_CC_NZ, -1, 1);
+
+                       code = emit_call (cfg, code, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init", FALSE);
+                       ins->flags |= MONO_INST_GC_CALLSITE;
+                       ins->backend.pc_offset = code - cfg->native_code;
+
+                       x86_patch (jump, code);
+                       break;
+               }
+
                case OP_X86_LEA:
                        amd64_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
                        break;
@@ -4965,7 +4952,10 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        amd64_ret (code);
                        break;
                }
-
+               case OP_GET_EX_OBJ:
+                       if (ins->dreg != AMD64_RAX)
+                               amd64_mov_reg_reg (code, ins->dreg, AMD64_RAX, sizeof (gpointer));
+                       break;
                case OP_LABEL:
                        ins->inst_c0 = code - cfg->native_code;
                        break;
@@ -6523,22 +6513,28 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        MONO_VARINFO (cfg, ins->inst_c0)->live_range_end = code - cfg->native_code;
                        break;
                }
-               case OP_NACL_GC_SAFE_POINT: {
-#if defined(__native_client_codegen__) && defined(__native_client_gc__)
-                       if (cfg->compile_aot)
-                               code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)mono_nacl_gc, TRUE);
-                       else {
-                               guint8 *br [1];
+               case OP_GC_SAFE_POINT: {
+                       const char *polling_func = NULL;
+                       int compare_val = 0;
+                       guint8 *br [1];
 
-                               amd64_mov_reg_imm_size (code, AMD64_R11, (gpointer)&__nacl_thread_suspension_needed, 4);
-                               amd64_test_membase_imm_size (code, AMD64_R11, 0, 0xFFFFFFFF, 4);
-                               br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
-                               code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)mono_nacl_gc, TRUE);
-                               amd64_patch (br[0], code);
-                       }
+#if defined (USE_COOP_GC)
+                       polling_func = "mono_threads_state_poll";
+                       compare_val = 1;
+#elif defined(__native_client_codegen__) && defined(__native_client_gc__)
+                       polling_func = "mono_nacl_gc";
+                       compare_val = 0xFFFFFFFF;
 #endif
+                       if (!polling_func)
+                               break;
+
+                       amd64_test_membase_imm_size (code, ins->sreg1, 0, compare_val, 4);
+                       br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
+                       code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, polling_func, FALSE);
+                       amd64_patch (br[0], code);
                        break;
                }
+
                case OP_GC_LIVENESS_DEF:
                case OP_GC_LIVENESS_USE:
                case OP_GC_PARAM_SLOT_LIVENESS_DEF:
@@ -6575,67 +6571,34 @@ mono_arch_register_lowlevel_calls (void)
 }
 
 void
-mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, MonoCodeManager *dyn_code_mp, gboolean run_cctors)
+mono_arch_patch_code_new (MonoCompile *cfg, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gpointer target)
 {
-       MonoJumpInfo *patch_info;
-       gboolean compile_aot = !run_cctors;
-
-       for (patch_info = ji; patch_info; patch_info = patch_info->next) {
-               unsigned char *ip = patch_info->ip.i + code;
-               unsigned char *target;
+       unsigned char *ip = ji->ip.i + code;
 
-               if (compile_aot) {
-                       switch (patch_info->type) {
-                       case MONO_PATCH_INFO_BB:
-                       case MONO_PATCH_INFO_LABEL:
+       /*
+        * Debug code to help track down problems where the target of a near call is
+        * is not valid.
+        */
+       if (amd64_is_near_call (ip)) {
+               gint64 disp = (guint8*)target - (guint8*)ip;
+
+               if (!amd64_is_imm32 (disp)) {
+                       printf ("TYPE: %d\n", ji->type);
+                       switch (ji->type) {
+                       case MONO_PATCH_INFO_INTERNAL_METHOD:
+                               printf ("V: %s\n", ji->data.name);
+                               break;
+                       case MONO_PATCH_INFO_METHOD_JUMP:
+                       case MONO_PATCH_INFO_METHOD:
+                               printf ("V: %s\n", ji->data.method->name);
                                break;
                        default:
-                               /* No need to patch these */
-                               continue;
-                       }
-               }
-
-               target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
-
-               switch (patch_info->type) {
-               case MONO_PATCH_INFO_NONE:
-                       continue;
-               case MONO_PATCH_INFO_METHOD_REL:
-               case MONO_PATCH_INFO_R8:
-               case MONO_PATCH_INFO_R4:
-                       g_assert_not_reached ();
-                       continue;
-               case MONO_PATCH_INFO_BB:
-                       break;
-               default:
-                       break;
-               }
-
-               /* 
-                * Debug code to help track down problems where the target of a near call is
-                * is not valid.
-                */
-               if (amd64_is_near_call (ip)) {
-                       gint64 disp = (guint8*)target - (guint8*)ip;
-
-                       if (!amd64_is_imm32 (disp)) {
-                               printf ("TYPE: %d\n", patch_info->type);
-                               switch (patch_info->type) {
-                               case MONO_PATCH_INFO_INTERNAL_METHOD:
-                                       printf ("V: %s\n", patch_info->data.name);
-                                       break;
-                               case MONO_PATCH_INFO_METHOD_JUMP:
-                               case MONO_PATCH_INFO_METHOD:
-                                       printf ("V: %s\n", patch_info->data.method->name);
-                                       break;
-                               default:
-                                       break;
-                               }
+                               break;
                        }
                }
-
-               amd64_patch (ip, (gpointer)target);
        }
+
+       amd64_patch (ip, (gpointer)target);
 }
 
 #ifndef DISABLE_JIT
@@ -6745,7 +6708,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
                mono_emit_unwind_op_offset (cfg, code, AMD64_RBP, - cfa_offset);
                async_exc_point (code);
-#ifdef HOST_WIN32
+#ifdef TARGET_WIN32
                mono_arch_unwindinfo_add_push_nonvol (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
 #endif
                /* These are handled automatically by the stack marking code */
@@ -6754,7 +6717,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof(mgreg_t));
                mono_emit_unwind_op_def_cfa_reg (cfg, code, AMD64_RBP);
                async_exc_point (code);
-#ifdef HOST_WIN32
+#ifdef TARGET_WIN32
                mono_arch_unwindinfo_add_set_fpreg (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
 #endif
        }
@@ -6795,7 +6758,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        /* Allocate stack frame */
        if (alloc_size) {
                /* See mono_emit_stack_alloc */
-#if defined(HOST_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
+#if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
                guint32 remaining_size = alloc_size;
                /*FIXME handle unbounded code expansion, we should use a loop in case of more than X interactions*/
                guint32 required_code_size = ((remaining_size / 0x1000) + 1) * 10; /*10 is the max size of amd64_alu_reg_imm + amd64_test_membase_reg*/
@@ -6815,7 +6778,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                                mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
                        }
                        async_exc_point (code);
-#ifdef HOST_WIN32
+#ifdef TARGET_WIN32
                        if (cfg->arch.omit_fp) 
                                mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, 0x1000);
 #endif
@@ -6830,7 +6793,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                                mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
                                async_exc_point (code);
                        }
-#ifdef HOST_WIN32
+#ifdef TARGET_WIN32
                        if (cfg->arch.omit_fp) 
                                mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, remaining_size);
 #endif
@@ -6991,38 +6954,6 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                        /* Unused arguments */
                        continue;
 
-               if (cfg->globalra) {
-                       /* All the other moves are done by the register allocator */
-                       switch (ainfo->storage) {
-                       case ArgInFloatSSEReg:
-                               amd64_sse_cvtss2sd_reg_reg (code, ainfo->reg, ainfo->reg);
-                               break;
-                       case ArgValuetypeInReg:
-                               for (quad = 0; quad < 2; quad ++) {
-                                       switch (ainfo->pair_storage [quad]) {
-                                       case ArgInIReg:
-                                               amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad], sizeof(mgreg_t));
-                                               break;
-                                       case ArgInFloatSSEReg:
-                                               amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
-                                               break;
-                                       case ArgInDoubleSSEReg:
-                                               amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
-                                               break;
-                                       case ArgNone:
-                                               break;
-                                       default:
-                                               g_assert_not_reached ();
-                                       }
-                               }
-                               break;
-                       default:
-                               break;
-                       }
-
-                       continue;
-               }
-
                /* Save volatile arguments to the stack */
                if (ins->opcode != OP_REGVAR) {
                        switch (ainfo->storage) {
@@ -7184,7 +7115,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                }
        }
 
-       if (cfg->gen_seq_points_debug_data) {
+       if (cfg->gen_sdb_seq_points) {
                MonoInst *info_var = cfg->arch.seq_point_info_var;
 
                /* Initialize seq_point_info_var */
@@ -7197,18 +7128,23 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                        amd64_mov_membase_reg (code, info_var->inst_basereg, info_var->inst_offset, AMD64_R11, 8);
                }
 
-               /* Initialize ss_trigger_page_var */
-               ins = cfg->arch.ss_trigger_page_var;
-
-               g_assert (ins->opcode == OP_REGOFFSET);
-
                if (cfg->compile_aot) {
+                       /* Initialize ss_tramp_var */
+                       ins = cfg->arch.ss_tramp_var;
+                       g_assert (ins->opcode == OP_REGOFFSET);
+
                        amd64_mov_reg_membase (code, AMD64_R11, info_var->inst_basereg, info_var->inst_offset, 8);
-                       amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, MONO_STRUCT_OFFSET (SeqPointInfo, ss_trigger_page), 8);
+                       amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, MONO_STRUCT_OFFSET (SeqPointInfo, ss_tramp_addr), 8);
+                       amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
                } else {
+                       /* Initialize ss_trigger_page_var */
+                       ins = cfg->arch.ss_trigger_page_var;
+
+                       g_assert (ins->opcode == OP_REGOFFSET);
+
                        amd64_mov_reg_imm (code, AMD64_R11, (guint64)ss_trigger_page);
+                       amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
                }
-               amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
        }
 
        cfg->code_len = code - cfg->native_code;
@@ -7746,7 +7682,7 @@ mono_arch_is_int_overflow (void *sigctx, void *info)
 
        mono_sigctx_to_monoctx (sigctx, &ctx);
 
-       rip = (guint8*)ctx.rip;
+       rip = (guint8*)ctx.gregs [AMD64_RIP];
 
        if (IS_REX (rip [0])) {
                reg = amd64_rex_b (rip [0]);
@@ -7759,47 +7695,7 @@ mono_arch_is_int_overflow (void *sigctx, void *info)
                /* idiv REG */
                reg += x86_modrm_rm (rip [1]);
 
-               switch (reg) {
-               case AMD64_RAX:
-                       value = ctx.rax;
-                       break;
-               case AMD64_RBX:
-                       value = ctx.rbx;
-                       break;
-               case AMD64_RCX:
-                       value = ctx.rcx;
-                       break;
-               case AMD64_RDX:
-                       value = ctx.rdx;
-                       break;
-               case AMD64_RBP:
-                       value = ctx.rbp;
-                       break;
-               case AMD64_RSP:
-                       value = ctx.rsp;
-                       break;
-               case AMD64_RSI:
-                       value = ctx.rsi;
-                       break;
-               case AMD64_RDI:
-                       value = ctx.rdi;
-                       break;
-               case AMD64_R12:
-                       value = ctx.r12;
-                       break;
-               case AMD64_R13:
-                       value = ctx.r13;
-                       break;
-               case AMD64_R14:
-                       value = ctx.r14;
-                       break;
-               case AMD64_R15:
-                       value = ctx.r15;
-                       break;
-               default:
-                       g_assert_not_reached ();
-                       reg = -1;
-               }                       
+               value = ctx.gregs [reg];
 
                if (value == -1)
                        return TRUE;
@@ -7919,7 +7815,7 @@ get_delegate_invoke_impl (gboolean has_target, guint32 param_count, guint32 *cod
                        /* We have to shift the arguments left */
                        amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
                        for (i = 0; i < param_count; ++i) {
-#ifdef HOST_WIN32
+#ifdef TARGET_WIN32
                                if (i < 3)
                                        amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
                                else
@@ -7935,6 +7831,7 @@ get_delegate_invoke_impl (gboolean has_target, guint32 param_count, guint32 *cod
        }
 
        nacl_global_codeman_validate (&start, 64, &code);
+       mono_arch_flush_icache (start, code - start);
 
        if (code_len)
                *code_len = code - start;
@@ -8375,44 +8272,16 @@ mono_arch_print_tree (MonoInst *tree, int arity)
        return 0;
 }
 
-#define _CTX_REG(ctx,fld,i) ((&ctx->fld)[i])
-
 mgreg_t
 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
 {
-       switch (reg) {
-       case AMD64_RCX: return ctx->rcx;
-       case AMD64_RDX: return ctx->rdx;
-       case AMD64_RBX: return ctx->rbx;
-       case AMD64_RBP: return ctx->rbp;
-       case AMD64_RSP: return ctx->rsp;
-       default:
-               return _CTX_REG (ctx, rax, reg);
-       }
+       return ctx->gregs [reg];
 }
 
 void
 mono_arch_context_set_int_reg (MonoContext *ctx, int reg, mgreg_t val)
 {
-       switch (reg) {
-       case AMD64_RCX:
-               ctx->rcx = val;
-               break;
-       case AMD64_RDX: 
-               ctx->rdx = val;
-               break;
-       case AMD64_RBX:
-               ctx->rbx = val;
-               break;
-       case AMD64_RBP:
-               ctx->rbp = val;
-               break;
-       case AMD64_RSP:
-               ctx->rsp = val;
-               break;
-       default:
-               _CTX_REG (ctx, rax, reg) = val;
-       }
+       ctx->gregs [reg] = val;
 }
 
 gpointer
@@ -8482,7 +8351,7 @@ mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
                SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
 
                g_assert (info->bp_addrs [native_offset] == 0);
-               info->bp_addrs [native_offset] = bp_trigger_page;
+               info->bp_addrs [native_offset] = mini_get_breakpoint_trampoline ();
        } else {
                /* 
                 * In production, we will use int3 (has to fix the size in the md 
@@ -8516,8 +8385,7 @@ mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
                guint32 native_offset = ip - (guint8*)ji->code_start;
                SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
 
-               g_assert (info->bp_addrs [native_offset] == 0);
-               info->bp_addrs [native_offset] = info;
+               info->bp_addrs [native_offset] = NULL;
        } else {
                for (i = 0; i < breakpoint_size; ++i)
                        x86_nop (code);
@@ -8553,8 +8421,7 @@ void
 mono_arch_skip_breakpoint (MonoContext *ctx, MonoJitInfo *ji)
 {
        if (ji->from_aot) {
-               /* amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8) */
-               MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 3);
+               /* The breakpoint instruction is a call */
        } else {
                MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + breakpoint_fault_size);
        }
@@ -8569,6 +8436,7 @@ void
 mono_arch_start_single_stepping (void)
 {
        mono_mprotect (ss_trigger_page, mono_pagesize (), 0);
+       ss_trampoline = mini_get_single_step_trampoline ();
 }
        
 /*
@@ -8580,6 +8448,7 @@ void
 mono_arch_stop_single_stepping (void)
 {
        mono_mprotect (ss_trigger_page, mono_pagesize (), MONO_MMAP_READ);
+       ss_trampoline = NULL;
 }
 
 /*
@@ -8630,7 +8499,6 @@ mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
 {
        SeqPointInfo *info;
        MonoJitInfo *ji;
-       int i;
 
        // FIXME: Add a free function
 
@@ -8646,11 +8514,7 @@ mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
                // FIXME: Optimize the size
                info = g_malloc0 (sizeof (SeqPointInfo) + (ji->code_size * sizeof (gpointer)));
 
-               info->ss_trigger_page = ss_trigger_page;
-               info->bp_trigger_page = bp_trigger_page;
-               /* Initialize to a valid address */
-               for (i = 0; i < ji->code_size; ++i)
-                       info->bp_addrs [i] = info;
+               info->ss_tramp_addr = &ss_trampoline;
 
                mono_domain_lock (domain);
                g_hash_table_insert (domain_jit_info (domain)->arch_seq_points,