2007-02-03 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / mini-sparc.c
index a439859b67019174a2342005ea73903a45f6221e..cd4547e1c3341702940d22de18b42f72418f3fa0 100644 (file)
@@ -50,8 +50,8 @@
  * Register usage:
  * - %i0..%i<n> hold the incoming arguments, these are never written by JITted 
  * code. Unused input registers are used for global register allocation.
- * - %l0..%l7 is used for local register allocation
- * - %o0..%o6 is used for outgoing arguments
+ * - %o0..%o5 and %l7 is used for local register allocation and passing arguments
+ * - %l0..%l6 is used for global register allocation
  * - %o7 and %g1 is used as scratch registers in opcodes
  * - all floating point registers are used for local register allocation except %f0. 
  *   Only double precision registers are used.
  * Possible optimizations:
  * - delay slot scheduling
  * - allocate large constants to registers
- * - use %o registers for local allocation
- * - implement unwinding through native frames
  * - add more mul/div/rem optimizations
  */
 
@@ -184,6 +182,24 @@ mono_arch_regname (int reg) {
        return "unknown";
 }
 
+const char*
+mono_arch_fregname (int reg) {
+       static const char *rnames [] = {
+               "sparc_f0", "sparc_f1", "sparc_f2", "sparc_f3", "sparc_f4", 
+               "sparc_f5", "sparc_f6", "sparc_f7", "sparc_f8", "sparc_f9",
+               "sparc_f10", "sparc_f11", "sparc_f12", "sparc_f13", "sparc_f14", 
+               "sparc_f15", "sparc_f16", "sparc_f17", "sparc_f18", "sparc_f19",
+               "sparc_f20", "sparc_f21", "sparc_f22", "sparc_f23", "sparc_f24", 
+               "sparc_f25", "sparc_f26", "sparc_f27", "sparc_f28", "sparc_f29",
+               "sparc_f30", "sparc_f31"
+       };
+
+       if (reg >= 0 && reg < 32)
+               return rnames [reg];
+       else
+               return "unknown";
+}
+
 /*
  * Initialize the cpu to execute managed code.
  */
@@ -237,7 +253,7 @@ mono_arch_cpu_optimizazions (guint32 *exclude_mask)
 }
 
 static void
-mono_sparc_break (void)
+mono_arch_break (void)
 {
 }
 
@@ -300,6 +316,8 @@ mono_sparc_flushw (void)
 
                g_assert ((code - start) < 64);
 
+               mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
+
                flushw = (gpointer)start;
 
                inited = 1;
@@ -530,6 +548,12 @@ get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
                case MONO_TYPE_ARRAY:
                        add_general (&gr, &stack_size, ainfo, FALSE);
                        break;
+               case MONO_TYPE_GENERICINST:
+                       if (!mono_type_generic_inst_is_valuetype (sig->params [i])) {
+                               add_general (&gr, &stack_size, ainfo, FALSE);
+                               break;
+                       }
+                       /* Fall through */
                case MONO_TYPE_VALUETYPE:
 #ifdef SPARCV9
                        if (sig->pinvoke)
@@ -622,6 +646,15 @@ get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
                        cinfo->ret.storage = ArgInFReg;
                        cinfo->ret.reg = sparc_f0;
                        break;
+               case MONO_TYPE_GENERICINST:
+                       if (!mono_type_generic_inst_is_valuetype (sig->ret)) {
+                               cinfo->ret.storage = ArgInIReg;
+                               cinfo->ret.reg = sparc_i0;
+                               if (gr < 1)
+                                       gr = 1;
+                               break;
+                       }
+                       /* Fall through */
                case MONO_TYPE_VALUETYPE:
                        if (v64) {
                                if (sig->pinvoke)
@@ -657,39 +690,6 @@ get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
        return cinfo;
 }
 
-static gboolean
-is_regsize_var (MonoType *t) {
-       if (t->byref)
-               return TRUE;
-       switch (mono_type_get_underlying_type (t)->type) {
-       case MONO_TYPE_BOOLEAN:
-       case MONO_TYPE_CHAR:
-       case MONO_TYPE_I1:
-       case MONO_TYPE_U1:
-       case MONO_TYPE_I2:
-       case MONO_TYPE_U2:
-       case MONO_TYPE_I4:
-       case MONO_TYPE_U4:
-       case MONO_TYPE_I:
-       case MONO_TYPE_U:
-               return TRUE;
-       case MONO_TYPE_OBJECT:
-       case MONO_TYPE_STRING:
-       case MONO_TYPE_CLASS:
-       case MONO_TYPE_SZARRAY:
-       case MONO_TYPE_ARRAY:
-               return TRUE;
-       case MONO_TYPE_VALUETYPE:
-               return FALSE;
-#ifdef SPARCV9
-       case MONO_TYPE_I8:
-       case MONO_TYPE_U8:
-               return TRUE;
-#endif
-       }
-       return FALSE;
-}
-
 GList *
 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
 {
@@ -713,7 +713,7 @@ mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
                if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode == OP_REGVAR) || (ins->opcode == OP_ARG))
                        continue;
 
-               if (is_regsize_var (ins->inst_vtype)) {
+               if (mono_is_regsize_var (ins->inst_vtype)) {
                        g_assert (MONO_VARINFO (cfg, i)->reg == -1);
                        g_assert (i == vmv->idx);
 
@@ -740,8 +740,8 @@ mono_arch_get_global_int_regs (MonoCompile *cfg)
        for (i = cinfo->reg_usage; i < 6; ++i)
                regs = g_list_prepend (regs, GUINT_TO_POINTER (sparc_i0 + i));
 
-       /* Use %l0..%l3 as global registers */
-       for (i = sparc_l0; i < sparc_l4; ++i)
+       /* Use %l0..%l6 as global registers */
+       for (i = sparc_l0; i < sparc_l7; ++i)
                regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
 
        g_free (cinfo);
@@ -839,9 +839,9 @@ mono_arch_allocate_vars (MonoCompile *m)
                if (inst->flags & MONO_INST_IS_DEAD)
                        continue;
 
-               /* inst->unused indicates native sized value types, this is used by the
+               /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
                * pinvoke wrappers when they call functions returning structure */
-               if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
+               if (inst->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
                        size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
                else
                        size = mono_type_stack_size (inst->inst_vtype, &align);
@@ -983,6 +983,51 @@ mono_arch_allocate_vars (MonoCompile *m)
        g_free (cinfo);
 }
 
+static MonoInst *
+make_group (MonoCompile *cfg, MonoInst *left, int basereg, int offset)
+{
+       MonoInst *group;
+
+       MONO_INST_NEW (cfg, group, OP_GROUP);
+       group->inst_left = left;
+       group->inst_basereg = basereg;
+       group->inst_imm = offset;
+
+       return group;
+}
+
+static void
+emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
+{
+       MonoInst *arg;
+       MonoMethodSignature *tmp_sig;
+       MonoInst *sig_arg;
+
+       /*
+        * mono_ArgIterator_Setup assumes the signature cookie is 
+        * passed first and all the arguments which were before it are
+        * passed on the stack after the signature. So compensate by 
+        * passing a different signature.
+        */
+       tmp_sig = mono_metadata_signature_dup (call->signature);
+       tmp_sig->param_count -= call->signature->sentinelpos;
+       tmp_sig->sentinelpos = 0;
+       memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
+
+       /* FIXME: Add support for signature tokens to AOT */
+       cfg->disable_aot = TRUE;
+       /* We allways pass the signature on the stack for simplicity */
+       MONO_INST_NEW (cfg, arg, OP_SPARC_OUTARG_MEM);
+       arg->inst_right = make_group (cfg, (MonoInst*)call, sparc_sp, ARGS_OFFSET + cinfo->sig_cookie.offset);
+       MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
+       sig_arg->inst_p0 = tmp_sig;
+       arg->inst_left = sig_arg;
+       arg->type = STACK_PTR;
+       /* prepend, so they get reversed */
+       arg->next = call->out_args;
+       call->out_args = arg;
+}
+
 /* 
  * take the arguments and generate the arch-specific
  * instructions to properly call the function in call.
@@ -1008,33 +1053,7 @@ mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call,
 
                if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
                        /* Emit the signature cookie just before the first implicit argument */
-                       MonoInst *sig_arg;
-                       MonoMethodSignature *tmp_sig;
-
-                       /*
-                        * mono_ArgIterator_Setup assumes the signature cookie is 
-                        * passed first and all the arguments which were before it are
-                        * passed on the stack after the signature. So compensate by 
-                        * passing a different signature.
-                        */
-                       tmp_sig = mono_metadata_signature_dup (call->signature);
-                       tmp_sig->param_count -= call->signature->sentinelpos;
-                       tmp_sig->sentinelpos = 0;
-                       memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
-
-                       /* FIXME: Add support for signature tokens to AOT */
-                       cfg->disable_aot = TRUE;
-                       /* We allways pass the signature on the stack for simplicity */
-                       MONO_INST_NEW (cfg, arg, OP_SPARC_OUTARG_MEM);
-                       arg->inst_basereg = sparc_sp;
-                       arg->inst_imm = ARGS_OFFSET + cinfo->sig_cookie.offset;
-                       MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
-                       sig_arg->inst_p0 = tmp_sig;
-                       arg->inst_left = sig_arg;
-                       arg->type = STACK_PTR;
-                       /* prepend, so they get reversed */
-                       arg->next = call->out_args;
-                       call->out_args = arg;
+                       emit_sig_cookie (cfg, call, cinfo);
                }
 
                if (is_virtual && i == 0) {
@@ -1068,8 +1087,16 @@ mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call,
                                else
                                if (sig->pinvoke)
                                        size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
-                               else
-                                       size = mono_type_stack_size (&in->klass->byval_arg, &align);
+                               else {
+                                       /* 
+                                        * Can't use mono_type_stack_size (), but that
+                                        * aligns the size to sizeof (gpointer), which is larger 
+                                        * than the size of the source, leading to reads of invalid
+                                        * memory if the source is at the end of address space or
+                                        * misaligned reads.
+                                        */
+                                       size = mono_class_value_size (in->klass, &align);
+                               }
 
                                /* 
                                 * We use OP_OUTARG_VT to copy the valuetype to a stack location, then
@@ -1087,27 +1114,36 @@ mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call,
                                pad = offset - ((ARGS_OFFSET - STACK_BIAS) + cinfo->stack_usage);
 
                                inst->inst_c1 = STACK_BIAS + offset;
-                               inst->unused = size;
+                               inst->backend.size = size;
                                arg->inst_left = inst;
 
                                cinfo->stack_usage += size;
                                cinfo->stack_usage += pad;
                        }
 
+                       arg->inst_right = make_group (cfg, (MonoInst*)call, sparc_sp, ARGS_OFFSET + ainfo->offset);
+
                        switch (ainfo->storage) {
                        case ArgInIReg:
                        case ArgInFReg:
                        case ArgInIRegPair:
                                if (ainfo->storage == ArgInIRegPair)
                                        arg->opcode = OP_SPARC_OUTARG_REGPAIR;
-                               arg->unused = sparc_o0 + ainfo->reg;
+                               arg->backend.reg3 = sparc_o0 + ainfo->reg;
                                call->used_iregs |= 1 << ainfo->reg;
 
-                               if ((i >= sig->hasthis) && (sig->params [i - sig->hasthis]->type == MONO_TYPE_R8)) {
+                               if ((i >= sig->hasthis) && !sig->params [i - sig->hasthis]->byref && ((sig->params [i - sig->hasthis]->type == MONO_TYPE_R8) || (sig->params [i - sig->hasthis]->type == MONO_TYPE_R4))) {
+                                       /* An fp value is passed in an ireg */
+
+                                       if (arg->opcode == OP_SPARC_OUTARG_REGPAIR)
+                                               arg->opcode = OP_SPARC_OUTARG_REGPAIR_FLOAT;
+                                       else
+                                               arg->opcode = OP_SPARC_OUTARG_FLOAT;
+
                                        /*
                                         * The OUTARG (freg) implementation needs an extra dword to store
                                         * the temporary value.
-                                        */
+                                        */                                     
                                        extra_space += 8;
                                }
                                break;
@@ -1119,26 +1155,28 @@ mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call,
                                break;
                        case ArgInSplitRegStack:
                                arg->opcode = OP_SPARC_OUTARG_SPLIT_REG_STACK;
-                               arg->unused = sparc_o0 + ainfo->reg;
+                               arg->backend.reg3 = sparc_o0 + ainfo->reg;
                                call->used_iregs |= 1 << ainfo->reg;
                                break;
                        case ArgInFloatReg:
                                arg->opcode = OP_SPARC_OUTARG_FLOAT_REG;
-                               arg->unused = sparc_f0 + ainfo->reg;
+                               arg->backend.reg3 = sparc_f0 + ainfo->reg;
                                break;
                        case ArgInDoubleReg:
                                arg->opcode = OP_SPARC_OUTARG_DOUBLE_REG;
-                               arg->unused = sparc_f0 + ainfo->reg;
+                               arg->backend.reg3 = sparc_f0 + ainfo->reg;
                                break;
                        default:
                                NOT_IMPLEMENTED;
                        }
-
-                       arg->inst_basereg = sparc_sp;
-                       arg->inst_imm = ARGS_OFFSET + ainfo->offset;
                }
        }
 
+       /* Handle the case where there are no implicit arguments */
+       if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos)) {
+               emit_sig_cookie (cfg, call, cinfo);
+       }
+
        /*
         * Reverse the call->out_args list.
         */
@@ -1153,6 +1191,8 @@ mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call,
                call->out_args = prev;
        }
        call->stack_usage = cinfo->stack_usage + extra_space;
+       call->out_ireg_args = NULL;
+       call->out_freg_args = NULL;
        cfg->param_area = MAX (cfg->param_area, call->stack_usage);
        cfg->flags |= MONO_CFG_HAS_CALLS;
 
@@ -1521,7 +1561,6 @@ peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                        }
                        break;
 #endif
-               case OP_LOADU1_MEMBASE:
                case OP_LOADI1_MEMBASE:
                        if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
                                        ins->inst_basereg == last_ins->inst_destbasereg &&
@@ -1537,7 +1576,6 @@ peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                                }
                        }
                        break;
-               case OP_LOADU2_MEMBASE:
                case OP_LOADI2_MEMBASE:
                        if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
                                        ins->inst_basereg == last_ins->inst_destbasereg &&
@@ -1677,151 +1715,6 @@ peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
        bb->last_ins = last_ins;
 }
 
-/* Parameters used by the register allocator */
-
-/* Use %l4..%l7 as local registers */
-#define ARCH_CALLER_REGS (0xf0<<16)
-
-#ifdef SPARCV9
-/* Use %d34..%d62 as the double precision floating point local registers */
-/* %d32 has the same encoding as %f1, so %d36%d38 == 0b1010 == 0xa */
-#define ARCH_CALLER_FREGS (0xaaaaaaa8)
-#else
-/* Use %f2..%f30 as the double precision floating point local registers */
-#define ARCH_CALLER_FREGS (0x55555554)
-#endif
-
-#undef DEBUG
-#define DEBUG(a) if (cfg->verbose_level > 1) a
-//#define DEBUG(a)
-#define reg_is_freeable(r) ((1 << (r)) & ARCH_CALLER_REGS)
-#define freg_is_freeable(r) (((1) << (r)) & ARCH_CALLER_FREGS)
-
-typedef struct {
-       int born_in;
-       int killed_in;
-       int last_use;
-       int prev_use;
-} RegTrack;
-
-static const char*const * ins_spec = sparc_desc;
-
-static inline const char*
-get_ins_spec (int opcode)
-{
-       if (ins_spec [opcode])
-               return ins_spec [opcode];
-       else
-               return ins_spec [CEE_ADD];
-}
-
-static void
-print_ins (int i, MonoInst *ins)
-{
-       const char *spec = get_ins_spec (ins->opcode);
-       g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
-       if (spec [MONO_INST_DEST]) {
-               if (ins->dreg >= MONO_MAX_IREGS)
-                       g_print (" R%d <-", ins->dreg);
-               else
-                       if (spec [MONO_INST_DEST] == 'b')
-                               g_print (" [%s + 0x%lx] <-", mono_arch_regname (ins->dreg), (long)ins->inst_offset);
-               else
-                       g_print (" %s <-", mono_arch_regname (ins->dreg));
-       }
-       if (spec [MONO_INST_SRC1]) {
-               if (ins->sreg1 >= MONO_MAX_IREGS)
-                       g_print (" R%d", ins->sreg1);
-               else
-                       if (spec [MONO_INST_SRC1] == 'b')
-                               g_print (" [%s + 0x%lx]", mono_arch_regname (ins->sreg1), (long)ins->inst_offset);
-               else
-                       g_print (" %s", mono_arch_regname (ins->sreg1));
-       }
-       if (spec [MONO_INST_SRC2]) {
-               if (ins->sreg2 >= MONO_MAX_IREGS)
-                       g_print (" R%d", ins->sreg2);
-               else
-                       g_print (" %s", mono_arch_regname (ins->sreg2));
-       }
-       if (spec [MONO_INST_CLOB])
-               g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
-       g_print ("\n");
-}
-
-static void
-print_regtrack (RegTrack *t, int num)
-{
-       int i;
-       char buf [32];
-       const char *r;
-       
-       for (i = 0; i < num; ++i) {
-               if (!t [i].born_in)
-                       continue;
-               if (i >= MONO_MAX_IREGS) {
-                       g_snprintf (buf, sizeof(buf), "R%d", i);
-                       r = buf;
-               } else
-                       r = mono_arch_regname (i);
-               g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
-       }
-}
-
-typedef struct InstList InstList;
-
-struct InstList {
-       InstList *prev;
-       InstList *next;
-       MonoInst *data;
-};
-
-static inline InstList*
-inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
-{
-       InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
-       item->data = data;
-       item->prev = NULL;
-       item->next = list;
-       if (list)
-               list->prev = item;
-       return item;
-}
-
-#define STACK_OFFSETS_POSITIVE
-
-/*
- * returns the offset used by spillvar. It allocates a new
- * spill variable if necessary.
- */
-static int
-mono_spillvar_offset (MonoCompile *cfg, int spillvar)
-{
-       MonoSpillInfo **si, *info;
-       int i = 0;
-
-       si = &cfg->spill_info; 
-       
-       while (i <= spillvar) {
-
-               if (!*si) {
-                       *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
-                       info->next = NULL;
-                       cfg->stack_offset += sizeof (gpointer);
-                       info->offset = - cfg->stack_offset;
-               }
-
-               if (i == spillvar)
-                       return MONO_SPARC_STACK_BIAS + (*si)->offset;
-
-               i++;
-               si = &(*si)->next;
-       }
-
-       g_assert_not_reached ();
-       return 0;
-}
-
 static int
 mono_spillvar_offset_float (MonoCompile *cfg, int spillvar)
 {
@@ -1851,639 +1744,12 @@ mono_spillvar_offset_float (MonoCompile *cfg, int spillvar)
        return 0;
 }
 
-/*
- * Force the spilling of the variable in the symbolic register 'reg'.
- */
-G_GNUC_UNUSED static int
-get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
-{
-       MonoInst *load;
-       int i, sel, spill;
-       
-       sel = cfg->rs->iassign [reg];
-       /*i = cfg->rs->isymbolic [sel];
-       g_assert (i == reg);*/
-       i = reg;
-       spill = ++cfg->spill_count;
-       cfg->rs->iassign [i] = -spill - 1;
-       mono_regstate_free_int (cfg->rs, sel);
-       /* we need to create a spill var and insert a load to sel after the current instruction */
-       MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
-       load->dreg = sel;
-       load->inst_basereg = cfg->frame_reg;
-       load->inst_offset = mono_spillvar_offset (cfg, spill);
-       if (item->prev) {
-               while (ins->next != item->prev->data)
-                       ins = ins->next;
-       }
-       load->next = ins->next;
-       ins->next = load;
-       DEBUG (g_print ("SPILLED LOAD (%d at 0x%08lx(%%sp)) R%d (freed %s)\n", spill, (long)load->inst_offset, i, mono_arch_regname (sel)));
-       i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
-       g_assert (i == sel);
-
-       return sel;
-}
-
-static int
-get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
-{
-       MonoInst *load;
-       int i, sel, spill;
-
-       DEBUG (g_print ("start regmask to assign R%d: 0x%08x (R%d <- R%d R%d)\n", reg, regmask, ins->dreg, ins->sreg1, ins->sreg2));
-       /* exclude the registers in the current instruction */
-       if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
-               if (ins->sreg1 >= MONO_MAX_IREGS)
-                       regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
-               else
-                       regmask &= ~ (1 << ins->sreg1);
-               DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
-       }
-       if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
-               if (ins->sreg2 >= MONO_MAX_IREGS)
-                       regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
-               else
-                       regmask &= ~ (1 << ins->sreg2);
-               DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
-       }
-       if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
-               regmask &= ~ (1 << ins->dreg);
-               DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
-       }
-
-       DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
-       g_assert (regmask); /* need at least a register we can free */
-       sel = -1;
-       /* we should track prev_use and spill the register that's farther */
-       for (i = 0; i < MONO_MAX_IREGS; ++i) {
-               if (regmask & (1 << i)) {
-                       sel = i;
-                       DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
-                       break;
-               }
-       }
-       i = cfg->rs->isymbolic [sel];
-       spill = ++cfg->spill_count;
-       cfg->rs->iassign [i] = -spill - 1;
-       mono_regstate_free_int (cfg->rs, sel);
-       /* we need to create a spill var and insert a load to sel after the current instruction */
-       MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
-       load->dreg = sel;
-       load->inst_basereg = cfg->frame_reg;
-       load->inst_offset = mono_spillvar_offset (cfg, spill);
-       if (item->prev) {
-               while (ins->next != item->prev->data)
-                       ins = ins->next;
-       }
-       load->next = ins->next;
-       ins->next = load;
-       DEBUG (g_print ("SPILLED LOAD (%d at 0x%08lx(%%sp)) R%d (freed %s)\n", spill, (long)load->inst_offset, i, mono_arch_regname (sel)));
-       i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
-       g_assert (i == sel);
-       
-       return sel;
-}
-
-static int
-get_float_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
-{
-       MonoInst *load;
-       int i, sel, spill;
-
-       DEBUG (g_print ("start regmask to assign R%d: 0x%08x (R%d <- R%d R%d)\n", reg, regmask, ins->dreg, ins->sreg1, ins->sreg2));
-       /* exclude the registers in the current instruction */
-       if (reg != ins->sreg1 && (freg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_FREGS && cfg->rs->fassign [ins->sreg1] >= 0))) {
-               if (ins->sreg1 >= MONO_MAX_FREGS)
-                       regmask &= ~ (1 << cfg->rs->fassign [ins->sreg1]);
-               else
-                       regmask &= ~ (1 << ins->sreg1);
-               DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
-       }
-       if (reg != ins->sreg2 && (freg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_FREGS && cfg->rs->fassign [ins->sreg2] >= 0))) {
-               if (ins->sreg2 >= MONO_MAX_FREGS)
-                       regmask &= ~ (1 << cfg->rs->fassign [ins->sreg2]);
-               else
-                       regmask &= ~ (1 << ins->sreg2);
-               DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
-       }
-       if (reg != ins->dreg && freg_is_freeable (ins->dreg)) {
-               regmask &= ~ (1 << ins->dreg);
-               DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
-       }
-
-       DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
-       g_assert (regmask); /* need at least a register we can free */
-       sel = -1;
-       /* we should track prev_use and spill the register that's farther */
-       for (i = 0; i < MONO_MAX_FREGS; ++i) {
-               if (regmask & (1 << i)) {
-                       sel = i;
-                       DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->fassign [sel]));
-                       break;
-               }
-       }
-       i = cfg->rs->fsymbolic [sel];
-       spill = ++cfg->spill_count;
-       cfg->rs->fassign [i] = -spill - 1;
-       mono_regstate_free_float(cfg->rs, sel);
-       /* we need to create a spill var and insert a load to sel after the current instruction */
-       MONO_INST_NEW (cfg, load, OP_LOADR8_MEMBASE);
-       load->dreg = sel;
-       load->inst_basereg = cfg->frame_reg;
-       load->inst_offset = mono_spillvar_offset_float (cfg, spill);
-       if (item->prev) {
-               while (ins->next != item->prev->data)
-                       ins = ins->next;
-       }
-       load->next = ins->next;
-       ins->next = load;
-       DEBUG (g_print ("SPILLED LOAD (%d at 0x%08lx(%%sp)) R%d (freed %s)\n", spill, (long)load->inst_offset, i, mono_arch_regname (sel)));
-       i = mono_regstate_alloc_float (cfg->rs, 1 << sel);
-       g_assert (i == sel);
-       
-       return sel;
-}
-
-static MonoInst*
-create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
-{
-       MonoInst *copy;
-       MONO_INST_NEW (cfg, copy, OP_MOVE);
-       copy->dreg = dest;
-       copy->sreg1 = src;
-       if (ins) {
-               copy->next = ins->next;
-               ins->next = copy;
-       }
-       DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
-       return copy;
-}
-
-G_GNUC_UNUSED static MonoInst*
-create_copy_ins_float (MonoCompile *cfg, int dest, int src, MonoInst *ins)
-{
-       MonoInst *copy;
-       MONO_INST_NEW (cfg, copy, OP_FMOVE);
-       copy->dreg = dest;
-       copy->sreg1 = src;
-       if (ins) {
-               copy->next = ins->next;
-               ins->next = copy;
-       }
-       DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
-       return copy;
-}
-
-static MonoInst*
-create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
-{
-       MonoInst *store;
-       MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
-       store->sreg1 = reg;
-       store->inst_destbasereg = cfg->frame_reg;
-       store->inst_offset = mono_spillvar_offset (cfg, spill);
-       if (ins) {
-               store->next = ins->next;
-               ins->next = store;
-       }
-       DEBUG (g_print ("SPILLED STORE (%d at 0x%08lx(%%sp)) R%d (from %s)\n", spill, (long)store->inst_offset, prev_reg, mono_arch_regname (reg)));
-       return store;
-}
-
-static MonoInst*
-create_spilled_store_float (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
-{
-       MonoInst *store;
-       MONO_INST_NEW (cfg, store, OP_STORER8_MEMBASE_REG);
-       store->sreg1 = reg;
-       store->inst_destbasereg = cfg->frame_reg;
-       store->inst_offset = mono_spillvar_offset_float (cfg, spill);
-       if (ins) {
-               store->next = ins->next;
-               ins->next = store;
-       }
-       DEBUG (g_print ("SPILLED STORE (%d at 0x%08lx(%%sp)) R%d (from %s)\n", spill, (long)store->inst_offset, prev_reg, mono_arch_regname (reg)));
-       return store;
-}
-
-static void
-insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
-{
-       MonoInst *prev;
-       g_assert (item->next);
-       prev = item->next->data;
-
-       while (prev->next != ins)
-               prev = prev->next;
-       to_insert->next = ins;
-       prev->next = to_insert;
-       /* 
-        * needed otherwise in the next instruction we can add an ins to the 
-        * end and that would get past this instruction.
-        */
-       item->data = to_insert; 
-}
-
-G_GNUC_UNUSED static int
-alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
-{
-       int val = cfg->rs->iassign [sym_reg];
-       if (val < 0) {
-               int spill = 0;
-               if (val < -1) {
-                       /* the register gets spilled after this inst */
-                       spill = -val -1;
-               }
-               val = mono_regstate_alloc_int (cfg->rs, allow_mask);
-               if (val < 0)
-                       val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
-               cfg->rs->iassign [sym_reg] = val;
-               /* add option to store before the instruction for src registers */
-               if (spill)
-                       create_spilled_store (cfg, spill, val, sym_reg, ins);
-       }
-       cfg->rs->isymbolic [val] = sym_reg;
-       return val;
-}
-
 /* FIXME: Strange loads from the stack in basic-float.cs:test_2_rem */
 
-/*
- * Local register allocation.
- * We first scan the list of instructions and we save the liveness info of
- * each register (when the register is first used, when it's value is set etc.).
- * We also reverse the list of instructions (in the InstList list) because assigning
- * registers backwards allows for more tricks to be used.
- */
 void
 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
 {
-       MonoInst *ins;
-       MonoRegState *rs = cfg->rs;
-       int i, val;
-       RegTrack *reginfo, *reginfof;
-       RegTrack *reginfo1, *reginfo2, *reginfod;
-       InstList *tmp, *reversed = NULL;
-       const char *spec;
-       guint32 src1_mask, src2_mask, dest_mask;
-       guint32 cur_iregs, cur_fregs;
-
-       /* FIXME: Use caller saved regs and %i1-%2 for allocation */
-
-       if (!bb->code)
-               return;
-       rs->next_vireg = bb->max_ireg;
-       rs->next_vfreg = bb->max_freg;
-       mono_regstate_assign (rs);
-       reginfo = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vireg);
-       reginfof = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vfreg);
-       rs->ifree_mask = ARCH_CALLER_REGS;
-       rs->ffree_mask = ARCH_CALLER_FREGS;
-
-       ins = bb->code;
-       i = 1;
-       DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
-       /* forward pass on the instructions to collect register liveness info */
-       while (ins) {
-               spec = ins_spec [ins->opcode];
-               if (!spec) {
-                       /* Use a default */
-                       spec = ins_spec [CEE_ADD];
-               }
-               DEBUG (print_ins (i, ins));
-
-               if (spec [MONO_INST_SRC1]) {
-                       if (spec [MONO_INST_SRC1] == 'f')
-                               reginfo1 = reginfof;
-                       else
-                               reginfo1 = reginfo;
-                       reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
-                       reginfo1 [ins->sreg1].last_use = i;
-               } else {
-                       ins->sreg1 = -1;
-               }
-               if (spec [MONO_INST_SRC2]) {
-                       if (spec [MONO_INST_SRC2] == 'f')
-                               reginfo2 = reginfof;
-                       else
-                               reginfo2 = reginfo;
-                       reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
-                       reginfo2 [ins->sreg2].last_use = i;
-               } else {
-                       ins->sreg2 = -1;
-               }
-               if (spec [MONO_INST_DEST]) {
-                       if (spec [MONO_INST_DEST] == 'f')
-                               reginfod = reginfof;
-                       else
-                               reginfod = reginfo;
-                       if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
-                               reginfod [ins->dreg].killed_in = i;
-                       reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
-                       reginfod [ins->dreg].last_use = i;
-                       if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
-                               reginfod [ins->dreg].born_in = i;
-                       if (!v64 && (spec [MONO_INST_DEST] == 'l')) {
-                               /* result in a regpair, the virtual register is allocated sequentially */
-                               reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
-                               reginfod [ins->dreg + 1].last_use = i;
-                               if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
-                                       reginfod [ins->dreg + 1].born_in = i;
-                       }
-               } else {
-                       ins->dreg = -1;
-               }
-               reversed = inst_list_prepend (cfg->mempool, reversed, ins);
-               ++i;
-               ins = ins->next;
-       }
-
-       cur_iregs = ARCH_CALLER_REGS;
-       cur_fregs = ARCH_CALLER_FREGS;
-
-       DEBUG (print_regtrack (reginfo, rs->next_vireg));
-       DEBUG (print_regtrack (reginfof, rs->next_vfreg));
-       tmp = reversed;
-       while (tmp) {
-               int prev_dreg, prev_sreg1, prev_sreg2;
-               --i;
-               ins = tmp->data;
-               spec = ins_spec [ins->opcode];
-               if (!spec)
-                       spec = ins_spec [CEE_ADD];
-               DEBUG (g_print ("processing:"));
-               DEBUG (print_ins (i, ins));
-
-               /* make the register available for allocation: FIXME add fp reg */
-               if (ins->opcode == OP_SETREG || ins->opcode == OP_SETREGIMM) {
-                       /* Dont free register which can't be allocated */
-                       if (reg_is_freeable (ins->dreg)) {
-                               cur_iregs |= 1 << ins->dreg;
-                               DEBUG (g_print ("adding %d to cur_iregs\n", ins->dreg));
-                       }
-               } else if (ins->opcode == OP_SETFREG) {
-                       if (freg_is_freeable (ins->dreg)) {
-                               cur_fregs |= 1 << ins->dreg;
-                               DEBUG (g_print ("adding %d to cur_fregs\n", ins->dreg));
-                       }
-               } else if (spec [MONO_INST_CLOB] == 'c') {
-                       MonoCallInst *cinst = (MonoCallInst*)ins;
-                       DEBUG (g_print ("excluding regs 0x%lx from cur_iregs (0x%x)\n", (long)cinst->used_iregs, cur_iregs));
-                       cur_iregs &= ~cinst->used_iregs;
-                       cur_fregs &= ~cinst->used_fregs;
-                       DEBUG (g_print ("available cur_iregs: 0x%x\n", cur_iregs));
-                       /* registers used by the calling convention are excluded from 
-                        * allocation: they will be selectively enabled when they are 
-                        * assigned by the special SETREG opcodes.
-                        */
-               }
-               dest_mask = src1_mask = src2_mask = cur_iregs;
-
-               /*
-                * DEST
-                */
-               /* update for use with FP regs... */
-               if (spec [MONO_INST_DEST] == 'f') {
-                       if (ins->dreg >= MONO_MAX_FREGS) {
-                               val = rs->fassign [ins->dreg];
-                               prev_dreg = ins->dreg;
-                               if (val < 0) {
-                                       int spill = 0;
-                                       if (val < -1) {
-                                               /* the register gets spilled after this inst */
-                                               spill = -val -1;
-                                       }
-                                       dest_mask = cur_fregs;
-                                       val = mono_regstate_alloc_float (rs, dest_mask);
-                                       if (val < 0)
-                                               val = get_float_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
-                                       rs->fassign [ins->dreg] = val;
-                                       if (spill)
-                                               create_spilled_store_float (cfg, spill, val, prev_dreg, ins);
-                               }
-                               DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
-                               rs->fsymbolic [val] = prev_dreg;
-                               ins->dreg = val;
-                       } else {
-                               prev_dreg = -1;
-                       }
-                       if (freg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfo [prev_dreg].born_in >= i || !(cur_fregs & (1 << ins->dreg)))) {
-                               DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
-                               mono_regstate_free_float (rs, ins->dreg);
-                       }
-               } else if (ins->dreg >= MONO_MAX_IREGS) {
-                       val = rs->iassign [ins->dreg];
-                       prev_dreg = ins->dreg;
-                       if (val < 0) {
-                               int spill = 0;
-                               if (val < -1) {
-                                       /* the register gets spilled after this inst */
-                                       spill = -val -1;
-                               }
-                               val = mono_regstate_alloc_int (rs, dest_mask);
-                               if (val < 0)
-                                       val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
-                               rs->iassign [ins->dreg] = val;
-                               if (spill)
-                                       create_spilled_store (cfg, spill, val, prev_dreg, ins);
-                       }
-                       DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
-                       rs->isymbolic [val] = prev_dreg;
-                       ins->dreg = val;
-                       if (!v64 && spec [MONO_INST_DEST] == 'l') {
-                               int hreg = prev_dreg + 1;
-                               val = rs->iassign [hreg];
-                               if (val < 0) {
-                                       int spill = 0;
-                                       if (val < -1) {
-                                               /* the register gets spilled after this inst */
-                                               spill = -val -1;
-                                       }
-                                       /* The second register must be a pair of the first */
-                                       dest_mask = 1 << (rs->iassign [prev_dreg] + 1);
-                                       val = mono_regstate_alloc_int (rs, dest_mask);
-                                       if (val < 0)
-                                               val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
-                                       rs->iassign [hreg] = val;
-                                       if (spill)
-                                               create_spilled_store (cfg, spill, val, hreg, ins);
-                               }
-                               else {
-                                       /* The second register must be a pair of the first */
-                                       if (val != rs->iassign [prev_dreg] + 1) {
-                                               dest_mask = 1 << (rs->iassign [prev_dreg] + 1);
-
-                                               val = mono_regstate_alloc_int (rs, dest_mask);
-                                               if (val < 0)
-                                                       val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
-
-                                               create_copy_ins (cfg, rs->iassign [hreg], val, ins);
-
-                                               rs->iassign [hreg] = val;
-                                       }
-                               }                                       
-
-                               DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
-                               rs->isymbolic [val] = hreg;
-
-                               if (reg_is_freeable (val) && hreg >= 0 && (reginfo [hreg].born_in >= i && !(cur_iregs & (1 << val)))) {
-                                       DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
-                                       mono_regstate_free_int (rs, val);
-                               }
-                       }
-               } else {
-                       prev_dreg = -1;
-               }
-               if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfo [prev_dreg].born_in >= i)) {
-                       DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
-                       mono_regstate_free_int (rs, ins->dreg);
-               }
-
-               /*
-                * SRC1
-                */
-               if (spec [MONO_INST_SRC1] == 'f') {
-                       if (ins->sreg1 >= MONO_MAX_FREGS) {
-                               val = rs->fassign [ins->sreg1];
-                               prev_sreg1 = ins->sreg1;
-                               if (val < 0) {
-                                       int spill = 0;
-                                       if (val < -1) {
-                                               /* the register gets spilled after this inst */
-                                               spill = -val -1;
-                                       }
-                                       //g_assert (val == -1); /* source cannot be spilled */
-                                       src1_mask = cur_fregs;
-                                       val = mono_regstate_alloc_float (rs, src1_mask);
-                                       if (val < 0)
-                                               val = get_float_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
-                                       rs->fassign [ins->sreg1] = val;
-                                       DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
-                                       if (spill) {
-                                               MonoInst *store = create_spilled_store_float (cfg, spill, val, prev_sreg1, NULL);
-                                               insert_before_ins (ins, tmp, store);
-                                       }
-                               }
-                               rs->fsymbolic [val] = prev_sreg1;
-                               ins->sreg1 = val;
-                       } else {
-                               prev_sreg1 = -1;
-                       }
-               } else if (ins->sreg1 >= MONO_MAX_IREGS) {
-                       val = rs->iassign [ins->sreg1];
-                       prev_sreg1 = ins->sreg1;
-                       if (val < 0) {
-                               int spill = 0;
-                               if (val < -1) {
-                                       /* the register gets spilled after this inst */
-                                       spill = -val -1;
-                               }
-                               if (0 && (ins->opcode == OP_MOVE) && reg_is_freeable (ins->dreg)) {
-                                       /* 
-                                        * small optimization: the dest register is already allocated
-                                        * but the src one is not: we can simply assign the same register
-                                        * here and peephole will get rid of the instruction later.
-                                        * This optimization may interfere with the clobbering handling:
-                                        * it removes a mov operation that will be added again to handle clobbering.
-                                        * There are also some other issues that should with make testjit.
-                                        */
-                                       mono_regstate_alloc_int (rs, 1 << ins->dreg);
-                                       val = rs->iassign [ins->sreg1] = ins->dreg;
-                                       //g_assert (val >= 0);
-                                       DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
-                               } else {
-                                       //g_assert (val == -1); /* source cannot be spilled */
-                                       val = mono_regstate_alloc_int (rs, src1_mask);
-                                       if (val < 0)
-                                               val = get_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
-                                       rs->iassign [ins->sreg1] = val;
-                                       DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
-                               }
-                               if (spill) {
-                                       MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
-                                       insert_before_ins (ins, tmp, store);
-                               }
-                       }
-                       rs->isymbolic [val] = prev_sreg1;
-                       ins->sreg1 = val;
-               } else {
-                       prev_sreg1 = -1;
-               }
-
-               /*
-                * SRC2
-                */
-               if (spec [MONO_INST_SRC2] == 'f') {
-                       if (ins->sreg2 >= MONO_MAX_FREGS) {
-                               val = rs->fassign [ins->sreg2];
-                               prev_sreg2 = ins->sreg2;
-                               if (val < 0) {
-                                       int spill = 0;
-                                       if (val < -1) {
-                                               /* the register gets spilled after this inst */
-                                               spill = -val -1;
-                                       }
-                                       src2_mask = cur_fregs;
-                                       val = mono_regstate_alloc_float (rs, src2_mask);
-                                       if (val < 0)
-                                               val = get_float_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
-                                       rs->fassign [ins->sreg2] = val;
-                                       DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
-                                       if (spill)
-                                               create_spilled_store_float (cfg, spill, val, prev_sreg2, ins);
-                               }
-                               rs->fsymbolic [val] = prev_sreg2;
-                               ins->sreg2 = val;
-                       } else {
-                               prev_sreg2 = -1;
-                       }
-               } else if (ins->sreg2 >= MONO_MAX_IREGS) {
-                       val = rs->iassign [ins->sreg2];
-                       prev_sreg2 = ins->sreg2;
-                       if (val < 0) {
-                               int spill = 0;
-                               if (val < -1) {
-                                       /* the register gets spilled after this inst */
-                                       spill = -val -1;
-                               }
-                               val = mono_regstate_alloc_int (rs, src2_mask);
-                               if (val < 0)
-                                       val = get_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
-                               rs->iassign [ins->sreg2] = val;
-                               DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
-                               if (spill)
-                                       create_spilled_store (cfg, spill, val, prev_sreg2, ins);
-                       }
-                       rs->isymbolic [val] = prev_sreg2;
-                       ins->sreg2 = val;
-               } else {
-                       prev_sreg2 = -1;
-               }
-
-               if (spec [MONO_INST_CLOB] == 'c') {
-                       int j, s;
-                       guint32 clob_mask = ARCH_CALLER_REGS;
-                       for (j = 0; j < MONO_MAX_IREGS; ++j) {
-                               s = 1 << j;
-                               if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
-                                       //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
-                               }
-                       }
-               }
-               /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
-                       DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
-                       mono_regstate_free_int (rs, ins->sreg1);
-               }
-               if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
-                       DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
-                       mono_regstate_free_int (rs, ins->sreg2);
-               }*/
-               
-               //DEBUG (print_ins (i, ins));
-
-               tmp = tmp->next;
-       }
+       mono_local_regalloc (cfg, bb);
 }
 
 static void
@@ -2683,7 +1949,7 @@ static guint32*
 emit_move_return_value (MonoInst *ins, guint32 *code)
 {
        /* Move return value to the target register */
-       /* FIXME: do this in the local reg allocator */
+       /* FIXME: do more things in the local reg allocator */
        switch (ins->opcode) {
        case OP_VOIDCALL:
        case OP_VOIDCALL_REG:
@@ -2692,20 +1958,19 @@ emit_move_return_value (MonoInst *ins, guint32 *code)
        case CEE_CALL:
        case OP_CALL_REG:
        case OP_CALL_MEMBASE:
-               sparc_mov_reg_reg (code, sparc_o0, ins->dreg);
+               g_assert (ins->dreg == sparc_o0);
                break;
        case OP_LCALL:
        case OP_LCALL_REG:
        case OP_LCALL_MEMBASE:
                /* 
                 * ins->dreg is the least significant reg due to the lreg: LCALL rule
-                * in inssel.brg.
+                * in inssel-long32.brg.
                 */
 #ifdef SPARCV9
                sparc_mov_reg_reg (code, sparc_o0, ins->dreg);
 #else
-               sparc_mov_reg_reg (code, sparc_o0, ins->dreg + 1);
-               sparc_mov_reg_reg (code, sparc_o1, ins->dreg);
+               g_assert (ins->dreg == sparc_o1);
 #endif
                break;
        case OP_FCALL:
@@ -2890,22 +2155,24 @@ mono_sparc_is_virtual_call (guint32 *code)
 }
 
 /*
- * mono_sparc_get_vcall_slot_addr:
+ * mono_arch_get_vcall_slot_addr:
  *
  *  Determine the vtable slot used by a virtual call.
  */
 gpointer*
-mono_sparc_get_vcall_slot_addr (guint32 *code, gpointer *fp)
+mono_arch_get_vcall_slot_addr (guint8 *code8, gpointer *regs)
 {
+       guint32 *code = (guint32*)(gpointer)code8;
        guint32 ins = code [0];
        guint32 prev_ins = code [-1];
 
        mono_sparc_flushw ();
 
-       fp = (gpointer*)((guint8*)fp + MONO_SPARC_STACK_BIAS);
+       if (!mono_sparc_is_virtual_call (code))
+               return NULL;
 
        if ((sparc_inst_op (ins) == 0x2) && (sparc_inst_op3 (ins) == 0x38)) {
-               if ((sparc_inst_op (prev_ins) == 0x3) && (sparc_inst_op3 (prev_ins) == 0 || sparc_inst_op3 (prev_ins) == 0xb)) {
+               if ((sparc_inst_op (prev_ins) == 0x3) && (sparc_inst_i (prev_ins) == 1) && (sparc_inst_op3 (prev_ins) == 0 || sparc_inst_op3 (prev_ins) == 0xb)) {
                        /* ld [r1 + CONST ], r2; call r2 */
                        guint32 base = sparc_inst_rs1 (prev_ins);
                        guint32 disp = sparc_inst_imm13 (prev_ins);
@@ -2914,18 +2181,49 @@ mono_sparc_get_vcall_slot_addr (guint32 *code, gpointer *fp)
                        g_assert (sparc_inst_rd (prev_ins) == sparc_inst_rs1 (ins));
 
                        g_assert ((base >= sparc_o0) && (base <= sparc_i7));
-                       
-                       base_val = fp [base - 16];
+
+                       base_val = regs [base - sparc_o0];
 
                        return (gpointer)((guint8*)base_val + disp);
                }
-               else
+               else if ((sparc_inst_op (prev_ins) == 0x3) && (sparc_inst_i (prev_ins) == 0) && (sparc_inst_op3 (prev_ins) == 0)) {
+                       /* set r1, ICONST; ld [r1 + r2], r2; call r2 */
+                       /* Decode a sparc_set32 */
+                       guint32 base = sparc_inst_rs1 (prev_ins);
+                       guint32 disp;
+                       gpointer base_val;
+                       guint32 s1 = code [-3];
+                       guint32 s2 = code [-2];
+
+#ifdef SPARCV9
+                       NOT_IMPLEMENTED;
+#endif
+
+                       /* sparc_sethi */
+                       g_assert (sparc_inst_op (s1) == 0);
+                       g_assert (sparc_inst_op2 (s1) == 4);
+
+                       /* sparc_or_imm */
+                       g_assert (sparc_inst_op (s2) == 2);
+                       g_assert (sparc_inst_op3 (s2) == 2);
+                       g_assert (sparc_inst_i (s2) == 1);
+                       g_assert (sparc_inst_rs1 (s2) == sparc_inst_rd (s2));
+                       g_assert (sparc_inst_rd (s1) == sparc_inst_rs1 (s2));
+
+                       disp = ((s1 & 0x3fffff) << 10) | sparc_inst_imm13 (s2);
+
+                       g_assert ((base >= sparc_o0) && (base <= sparc_i7));
+
+                       base_val = regs [base - sparc_o0];
+
+                       return (gpointer)((guint8*)base_val + disp);
+               } else
                        g_assert_not_reached ();
        }
        else
                g_assert_not_reached ();
 
-       return FALSE;
+       return NULL;
 }
 
 /*
@@ -2963,9 +2261,10 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 
                offset = (guint8*)code - cfg->native_code;
 
-               spec = ins_spec [ins->opcode];
-               if (!spec)
-                       spec = ins_spec [CEE_ADD];
+               spec = ins_get_spec (ins->opcode);
+               /* I kept this, but this looks a workaround for a bug */
+               if (spec == MONO_ARCH_CPU_SPEC)
+                       spec = ins_get_spec (CEE_ADD);
 
                max_len = ((guint8 *)spec)[MONO_INST_LEN];
 
@@ -3136,9 +2435,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                                sparc_cmp (code, ins->sreg1, sparc_o7);
                        }
                        break;
-               case OP_X86_TEST_NULL:
-                       sparc_cmp_imm (code, ins->sreg1, 0);
-                       break;
                case CEE_BREAK:
                        /*
                         * gdb does not like encountering 'ta 1' in the debugged code. So 
@@ -3146,7 +2442,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                         * breakpoint there.
                         */
                        //sparc_ta (code, 1);
-                       mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, mono_sparc_break);
+                       mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, mono_arch_break);
                        EMIT_CALL();
                        break;
                case OP_ADDCC:
@@ -3421,7 +2717,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        EMIT_COND_SYSTEM_EXCEPTION_GENERAL (ins, sparc_bne, "OverflowException", TRUE, sparc_icc_short);
                        break;
                case OP_ICONST:
-               case OP_SETREGIMM:
                        sparc_set (code, ins->inst_c0, ins->dreg);
                        break;
                case OP_I8CONST:
@@ -3434,7 +2729,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case CEE_CONV_I4:
                case CEE_CONV_U4:
                case OP_MOVE:
-               case OP_SETREG:
                        if (ins->sreg1 != ins->dreg)
                                sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
                        break;
@@ -3509,9 +2803,13 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_VOIDCALL_MEMBASE:
                case OP_CALL_MEMBASE:
                        call = (MonoCallInst*)ins;
-                       g_assert (sparc_is_imm13 (ins->inst_offset));
                        code = emit_save_sp_to_lmf (cfg, code);
-                       sparc_ldi_imm (code, ins->inst_basereg, ins->inst_offset, sparc_o7);
+                       if (sparc_is_imm13 (ins->inst_offset)) {
+                               sparc_ldi_imm (code, ins->inst_basereg, ins->inst_offset, sparc_o7);
+                       } else {
+                               sparc_set (code, ins->inst_offset, sparc_o7);
+                               sparc_ldi (code, ins->inst_basereg, sparc_o7, sparc_o7);
+                       }
                        sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
                        if (call->virtual)
                                sparc_or_imm (code, FALSE, sparc_g0, 0xca, sparc_g0);
@@ -3546,8 +2844,8 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 #endif
 
                        /* Keep alignment */
-                       sparc_add_imm (code, FALSE, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1, ins->dreg);
-                       sparc_set (code, ~(MONO_ARCH_FRAME_ALIGNMENT - 1), sparc_o7);
+                       sparc_add_imm (code, FALSE, ins->sreg1, MONO_ARCH_LOCALLOC_ALIGNMENT - 1, ins->dreg);
+                       sparc_set (code, ~(MONO_ARCH_LOCALLOC_ALIGNMENT - 1), sparc_o7);
                        sparc_and (code, FALSE, ins->dreg, sparc_o7, ins->dreg);
 
                        if ((ins->flags & MONO_INST_INIT) && (ins->sreg1 == ins->dreg)) {
@@ -3600,7 +2898,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        NOT_IMPLEMENTED;
 #endif
 
-                       offset = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
+                       offset = ALIGN_TO (offset, MONO_ARCH_LOCALLOC_ALIGNMENT);
                        if (sparc_is_imm13 (offset))
                                sparc_sub_imm (code, FALSE, sparc_sp, offset, sparc_sp);
                        else {
@@ -3943,15 +3241,25 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        break;
                case CEE_CONV_R4: {
                        gint32 offset = mono_spillvar_offset_float (cfg, 0);
-                       if (!sparc_is_imm13 (offset))
-                               NOT_IMPLEMENTED;
 #ifdef SPARCV9
-                       sparc_stx_imm (code, ins->sreg1, sparc_sp, offset);
-                       sparc_lddf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
+                       if (!sparc_is_imm13 (offset)) {
+                               sparc_set (code, offset, sparc_o7);
+                               sparc_stx (code, ins->sreg1, sparc_sp, offset);
+                               sparc_lddf (code, sparc_sp, offset, FP_SCRATCH_REG);
+                       } else {
+                               sparc_stx_imm (code, ins->sreg1, sparc_sp, offset);
+                               sparc_lddf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
+                       }
                        sparc_fxtos (code, FP_SCRATCH_REG, FP_SCRATCH_REG);
 #else
-                       sparc_st_imm (code, ins->sreg1, sparc_sp, offset);
-                       sparc_ldf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
+                       if (!sparc_is_imm13 (offset)) {
+                               sparc_set (code, offset, sparc_o7);
+                               sparc_st (code, ins->sreg1, sparc_sp, sparc_o7);
+                               sparc_ldf (code, sparc_sp, sparc_o7, FP_SCRATCH_REG);
+                       } else {
+                               sparc_st_imm (code, ins->sreg1, sparc_sp, offset);
+                               sparc_ldf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
+                       }
                        sparc_fitos (code, FP_SCRATCH_REG, FP_SCRATCH_REG);
 #endif
                        sparc_fstod (code, FP_SCRATCH_REG, ins->dreg);
@@ -3959,15 +3267,25 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                }
                case CEE_CONV_R8: {
                        gint32 offset = mono_spillvar_offset_float (cfg, 0);
-                       if (!sparc_is_imm13 (offset))
-                               NOT_IMPLEMENTED;
 #ifdef SPARCV9
-                       sparc_stx_imm (code, ins->sreg1, sparc_sp, offset);
-                       sparc_lddf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
+                       if (!sparc_is_imm13 (offset)) {
+                               sparc_set (code, offset, sparc_o7);
+                               sparc_stx (code, ins->sreg1, sparc_sp, sparc_o7);
+                               sparc_lddf (code, sparc_sp, sparc_o7, FP_SCRATCH_REG);
+                       } else {
+                               sparc_stx_imm (code, ins->sreg1, sparc_sp, offset);
+                               sparc_lddf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
+                       }
                        sparc_fxtod (code, FP_SCRATCH_REG, ins->dreg);
 #else
-                       sparc_st_imm (code, ins->sreg1, sparc_sp, offset);
-                       sparc_ldf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
+                       if (!sparc_is_imm13 (offset)) {
+                               sparc_set (code, offset, sparc_o7);
+                               sparc_st (code, ins->sreg1, sparc_sp, sparc_o7);
+                               sparc_ldf (code, sparc_sp, sparc_o7, FP_SCRATCH_REG);
+                       } else {
+                               sparc_st_imm (code, ins->sreg1, sparc_sp, offset);
+                               sparc_ldf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
+                       }
                        sparc_fitod (code, FP_SCRATCH_REG, ins->dreg);
 #endif
                        break;
@@ -3983,11 +3301,15 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_FCONV_TO_I4:
                case OP_FCONV_TO_U4: {
                        gint32 offset = mono_spillvar_offset_float (cfg, 0);
-                       if (!sparc_is_imm13 (offset))
-                               NOT_IMPLEMENTED;
                        sparc_fdtoi (code, ins->sreg1, FP_SCRATCH_REG);
-                       sparc_stdf_imm (code, FP_SCRATCH_REG, sparc_sp, offset);
-                       sparc_ld_imm (code, sparc_sp, offset, ins->dreg);
+                       if (!sparc_is_imm13 (offset)) {
+                               sparc_set (code, offset, sparc_o7);
+                               sparc_stdf (code, FP_SCRATCH_REG, sparc_sp, sparc_o7);
+                               sparc_ld (code, sparc_sp, sparc_o7, ins->dreg);
+                       } else {
+                               sparc_stdf_imm (code, FP_SCRATCH_REG, sparc_sp, offset);
+                               sparc_ld_imm (code, sparc_sp, offset, ins->dreg);
+                       }
 
                        switch (ins->opcode) {
                        case OP_FCONV_TO_I1:
@@ -4154,10 +3476,14 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        break;
                case CEE_CKFINITE: {
                        gint32 offset = mono_spillvar_offset_float (cfg, 0);
-                       if (!sparc_is_imm13 (offset))
-                               NOT_IMPLEMENTED;
-                       sparc_stdf_imm (code, ins->sreg1, sparc_sp, offset);
-                       sparc_lduh_imm (code, sparc_sp, offset, sparc_o7);
+                       if (!sparc_is_imm13 (offset)) {
+                               sparc_set (code, offset, sparc_o7);
+                               sparc_stdf (code, ins->sreg1, sparc_sp, sparc_o7);
+                               sparc_lduh (code, sparc_sp, sparc_o7, sparc_o7);
+                       } else {
+                               sparc_stdf_imm (code, ins->sreg1, sparc_sp, offset);
+                               sparc_lduh_imm (code, sparc_sp, offset, sparc_o7);
+                       }
                        sparc_srl_imm (code, sparc_o7, 4, sparc_o7);
                        sparc_and_imm (code, FALSE, sparc_o7, 2047, sparc_o7);
                        sparc_cmp_imm (code, sparc_o7, 2047);
@@ -4170,6 +3496,11 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 #endif
                        break;
                }
+
+               case OP_MEMORY_BARRIER:
+                       sparc_membar (code, sparc_membar_all);
+                       break;
+
                default:
 #ifdef __GNUC__
                        g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
@@ -4198,7 +3529,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 void
 mono_arch_register_lowlevel_calls (void)
 {
-       mono_register_jit_icall (mono_sparc_break, "mono_sparc_break", NULL, TRUE);
+       mono_register_jit_icall (mono_arch_break, "mono_arch_break", NULL, TRUE);
        mono_register_jit_icall (mono_arch_get_lmf_addr, "mono_arch_get_lmf_addr", NULL, TRUE);
 }
 
@@ -4862,32 +4193,6 @@ mono_arch_get_lmf_addr (void)
  */
 #error "--with-sigaltstack=yes not supported on solaris"
 
-static void
-setup_stack (MonoJitTlsData *tls)
-{
-#ifdef __linux__
-       struct sigaltstack sa;
-#else
-       stack_t         sigstk;
-#endif
-       /* Setup an alternate signal stack */
-       tls->signal_stack = mmap (0, SIGNAL_STACK_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
-       tls->signal_stack_size = SIGNAL_STACK_SIZE;
-
-#ifdef __linux__
-       sa.ss_sp = tls->signal_stack;
-       sa.ss_size = SIGNAL_STACK_SIZE;
-       sa.ss_flags = 0;
-       g_assert (sigaltstack (&sa, NULL) == 0);
-#else
-       sigstk.ss_sp = tls->signal_stack;
-       sigstk.ss_size = SIGNAL_STACK_SIZE;
-       sigstk.ss_flags = 0;
-       g_assert (sigaltstack (&sigstk, NULL) == 0);
-#endif
-}
-
 #endif
 
 void
@@ -4912,10 +4217,6 @@ mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
 #else
        pthread_setspecific (lmf_addr_key, &tls->lmf);
 #endif
-
-#ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
-       setup_stack (tls);
-#endif
 }
 
 void
@@ -4924,17 +4225,20 @@ mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
 }
 
 void
-mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
+mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *call, int this_reg, int this_type, int vt_reg)
 {
        int this_out_reg = sparc_o0;
 
        if (vt_reg != -1) {
 #ifdef SPARCV9
                MonoInst *ins;
-               MONO_INST_NEW (cfg, ins, OP_SETREG);
+               MONO_INST_NEW (cfg, ins, OP_MOVE);
                ins->sreg1 = vt_reg;
-               ins->dreg = sparc_o0;
+               ins->dreg = mono_regstate_next_int (cfg->rs);
                mono_bblock_add_inst (cfg->cbb, ins);
+
+               mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, sparc_o0, FALSE);
+
                this_out_reg = sparc_o1;
 #else
                /* Set the 'struct/union return pointer' location on the stack */
@@ -4945,11 +4249,13 @@ mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_re
        /* add the this argument */
        if (this_reg != -1) {
                MonoInst *this;
-               MONO_INST_NEW (cfg, this, OP_SETREG);
+               MONO_INST_NEW (cfg, this, OP_MOVE);
                this->type = this_type;
                this->sreg1 = this_reg;
-               this->dreg = this_out_reg;
+               this->dreg = mono_regstate_next_int (cfg->rs);
                mono_bblock_add_inst (cfg->cbb, this);
+
+               mono_call_inst_add_outarg_reg (cfg, call, this->dreg, this_out_reg, FALSE);
        }
 }
 
@@ -4957,7 +4263,15 @@ mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_re
 MonoInst*
 mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
 {
-       return NULL;
+       MonoInst *ins = NULL;
+
+       if (cmethod->klass == mono_defaults.thread_class &&
+               strcmp (cmethod->name, "MemoryBarrier") == 0) {
+               if (sparcv9)
+                       MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
+       }
+
+       return ins;
 }
 
 /*