2009-02-18 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / mini / mini-sparc.c
index d2660fc4cf43fa1d1088e94a9b94fa756e36bcdf..e44f8ffdb579bc89458cdd7e86df65b082c06225 100644 (file)
 #include <thread.h>
 #endif
 
+#include <unistd.h>
+#include <sys/mman.h>
+
 #include <mono/metadata/appdomain.h>
 #include <mono/metadata/debug-helpers.h>
+#include <mono/metadata/tokentype.h>
 #include <mono/utils/mono-math.h>
 
 #include "mini-sparc.h"
-#include "inssel.h"
 #include "trace.h"
 #include "cpu-sparc.h"
+#include "jit-icalls.h"
+#include "ir-emit.h"
 
 /*
  * Sparc V9 means two things:
@@ -46,8 +51,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
  */
 
 #endif
 #endif
 
-#define NOT_IMPLEMENTED do { g_assert_not_reached (); } while (0)
-
 #define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
 
 #define SIGNAL_STACK_SIZE (64 * 1024)
@@ -161,9 +162,6 @@ static gboolean v64 = FALSE;
 
 static gpointer mono_arch_get_lmf_addr (void);
 
-static int
-mono_spillvar_offset_float (MonoCompile *cfg, int spillvar);
-
 const char*
 mono_arch_regname (int reg) {
        static const char * rnames[] = {
@@ -180,6 +178,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.
  */
@@ -191,6 +207,22 @@ mono_arch_cpu_init (void)
        mono_arch_cpu_optimizazions(&dummy);
 }
 
+/*
+ * Initialize architecture specific code.
+ */
+void
+mono_arch_init (void)
+{
+}
+
+/*
+ * Cleanup architecture specific code.
+ */
+void
+mono_arch_cleanup (void)
+{
+}
+
 /*
  * This function returns the optimizations supported on this cpu.
  */
@@ -232,11 +264,6 @@ mono_arch_cpu_optimizazions (guint32 *exclude_mask)
        return opts;
 }
 
-static void
-mono_sparc_break (void)
-{
-}
-
 #ifdef __GNUC__
 #define flushi(addr)    __asm__ __volatile__ ("iflush %0"::"r"(addr):"memory")
 #else /* assume Sun's compiler */
@@ -257,18 +284,28 @@ mono_arch_flush_icache (guint8 *code, gint size)
        /* Hopefully this is optimized based on the actual CPU */
        sync_instruction_memory (code, size);
 #else
-       guint64 *p = (guint64*)code;
-       guint64 *end = (guint64*)(code + ((size + 8) /8));
-
-       /* 
-        * FIXME: Flushing code in dword chunks in _slow_.
+       gulong start = (gulong) code;
+       gulong end = start + size;
+       gulong align;
+
+       /* Sparcv9 chips only need flushes on 32 byte
+        * cacheline boundaries.
+        *
+        * Sparcv8 needs a flush every 8 bytes.
         */
-       while (p < end)
+       align = (sparcv9 ? 32 : 8);
+
+       start &= ~(align - 1);
+       end = (end + (align - 1)) & ~(align - 1);
+
+       while (start < end) {
 #ifdef __GNUC__
-               __asm__ __volatile__ ("iflush %0"::"r"(p++));
+               __asm__ __volatile__ ("iflush %0"::"r"(start));
 #else
-                       flushi (p ++);
+               flushi (start);
 #endif
+               start += align;
+       }
 #endif
 }
 
@@ -296,6 +333,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;
@@ -446,12 +485,14 @@ add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean single)
  * the 'Sparc Compliance Definition 2.4' document.
  */
 static CallInfo*
-get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
+get_call_info (MonoCompile *cfg, MonoMethodSignature *sig, gboolean is_pinvoke)
 {
        guint32 i, gr, fr;
        int n = sig->hasthis + sig->param_count;
        guint32 stack_size = 0;
        CallInfo *cinfo;
+       MonoType *ret_type;
+       MonoGenericSharingContext *gsctx = cfg ? cfg->generic_sharing_context : NULL;
 
        cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
 
@@ -479,6 +520,7 @@ get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
 
        for (i = 0; i < sig->param_count; ++i) {
                ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
+               MonoType *ptype;
 
                if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
                        gr = PARAM_REGS;
@@ -494,7 +536,9 @@ get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
                        add_general (&gr, &stack_size, ainfo, FALSE);
                        continue;
                }
-               switch (mono_type_get_underlying_type (sig->params [i])->type) {
+               ptype = mono_type_get_underlying_type (sig->params [i]);
+               ptype = mini_get_basic_type_from_generic (gsctx, ptype);
+               switch (ptype->type) {
                case MONO_TYPE_BOOLEAN:
                case MONO_TYPE_I1:
                case MONO_TYPE_U1:
@@ -518,6 +562,7 @@ get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
                case MONO_TYPE_I:
                case MONO_TYPE_U:
                case MONO_TYPE_PTR:
+               case MONO_TYPE_FNPTR:
                case MONO_TYPE_CLASS:
                case MONO_TYPE_OBJECT:
                case MONO_TYPE_STRING:
@@ -525,6 +570,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)
@@ -574,114 +625,91 @@ get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
        }
 
        /* return value */
-       {
-               switch (mono_type_get_underlying_type (sig->ret)->type) {
-               case MONO_TYPE_BOOLEAN:
-               case MONO_TYPE_I1:
-               case MONO_TYPE_U1:
-               case MONO_TYPE_I2:
-               case MONO_TYPE_U2:
-               case MONO_TYPE_CHAR:
-               case MONO_TYPE_I4:
-               case MONO_TYPE_U4:
-               case MONO_TYPE_I:
-               case MONO_TYPE_U:
-               case MONO_TYPE_PTR:
-               case MONO_TYPE_CLASS:
-               case MONO_TYPE_OBJECT:
-               case MONO_TYPE_SZARRAY:
-               case MONO_TYPE_ARRAY:
-               case MONO_TYPE_STRING:
-                       cinfo->ret.storage = ArgInIReg;
-                       cinfo->ret.reg = sparc_i0;
-                       if (gr < 1)
-                               gr = 1;
-                       break;
-               case MONO_TYPE_U8:
-               case MONO_TYPE_I8:
-#ifdef SPARCV9
-                       cinfo->ret.storage = ArgInIReg;
-                       cinfo->ret.reg = sparc_i0;
-                       if (gr < 1)
-                               gr = 1;
-#else
-                       cinfo->ret.storage = ArgInIRegPair;
-                       cinfo->ret.reg = sparc_i0;
-                       if (gr < 2)
-                               gr = 2;
-#endif
-                       break;
-               case MONO_TYPE_R4:
-               case MONO_TYPE_R8:
-                       cinfo->ret.storage = ArgInFReg;
-                       cinfo->ret.reg = sparc_f0;
-                       break;
-               case MONO_TYPE_VALUETYPE:
-                       if (v64) {
-                               if (sig->pinvoke)
-                                       NOT_IMPLEMENTED;
-                               else
-                                       /* Already done */
-                                       ;
-                       }
-                       else
-                               cinfo->ret.storage = ArgOnStack;
-                       break;
-               case MONO_TYPE_TYPEDBYREF:
-                       if (v64) {
-                               if (sig->pinvoke)
-                                       /* Same as a valuetype with size 24 */
-                                       NOT_IMPLEMENTED;
-                               else
-                                       /* Already done */
-                                       ;
-                       }
-                       else
-                               cinfo->ret.storage = ArgOnStack;
-                       break;
-               case MONO_TYPE_VOID:
-                       break;
-               default:
-                       g_error ("Can't handle as return value 0x%x", sig->ret->type);
-               }
-       }
-
-       cinfo->stack_usage = stack_size;
-       cinfo->reg_usage = gr;
-       return cinfo;
-}
-
-static gboolean
-is_regsize_var (MonoType *t) {
-       if (t->byref)
-               return TRUE;
-       switch (mono_type_get_underlying_type (t)->type) {
+       ret_type = mono_type_get_underlying_type (sig->ret);
+       ret_type = mini_get_basic_type_from_generic (gsctx, ret_type);
+       switch (ret_type->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_CHAR:
        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_PTR:
+       case MONO_TYPE_FNPTR:
        case MONO_TYPE_CLASS:
+       case MONO_TYPE_OBJECT:
        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_STRING:
+               cinfo->ret.storage = ArgInIReg;
+               cinfo->ret.reg = sparc_i0;
+               if (gr < 1)
+                       gr = 1;
+               break;
        case MONO_TYPE_U8:
-               return TRUE;
+       case MONO_TYPE_I8:
+#ifdef SPARCV9
+               cinfo->ret.storage = ArgInIReg;
+               cinfo->ret.reg = sparc_i0;
+               if (gr < 1)
+                       gr = 1;
+#else
+               cinfo->ret.storage = ArgInIRegPair;
+               cinfo->ret.reg = sparc_i0;
+               if (gr < 2)
+                       gr = 2;
 #endif
+               break;
+       case MONO_TYPE_R4:
+       case MONO_TYPE_R8:
+               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)
+                               NOT_IMPLEMENTED;
+                       else
+                               /* Already done */
+                               ;
+               }
+               else
+                       cinfo->ret.storage = ArgOnStack;
+               break;
+       case MONO_TYPE_TYPEDBYREF:
+               if (v64) {
+                       if (sig->pinvoke)
+                               /* Same as a valuetype with size 24 */
+                               NOT_IMPLEMENTED;
+                       else
+                               /* Already done */
+                               ;
+               }
+               else
+                       cinfo->ret.storage = ArgOnStack;
+               break;
+       case MONO_TYPE_VOID:
+               break;
+       default:
+               g_error ("Can't handle as return value 0x%x", sig->ret->type);
        }
-       return FALSE;
+
+       cinfo->stack_usage = stack_size;
+       cinfo->reg_usage = gr;
+       return cinfo;
 }
 
 GList *
@@ -707,7 +735,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);
 
@@ -726,16 +754,16 @@ mono_arch_get_global_int_regs (MonoCompile *cfg)
        MonoMethodSignature *sig;
        CallInfo *cinfo;
 
-       sig = cfg->method->signature;
+       sig = mono_method_signature (cfg->method);
 
-       cinfo = get_call_info (sig, FALSE);
+       cinfo = get_call_info (cfg, sig, FALSE);
 
        /* Use unused input registers */
        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);
@@ -760,8 +788,9 @@ mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
  * Set var information according to the calling convention. sparc version.
  * The locals var stuff should most likely be split in another method.
  */
+
 void
-mono_arch_allocate_vars (MonoCompile *m)
+mono_arch_allocate_vars (MonoCompile *cfg)
 {
        MonoMethodSignature *sig;
        MonoMethodHeader *header;
@@ -769,34 +798,46 @@ mono_arch_allocate_vars (MonoCompile *m)
        int i, offset, size, align, curinst;
        CallInfo *cinfo;
 
-       header = mono_method_get_header (m->method);
+       header = mono_method_get_header (cfg->method);
 
-       sig = m->method->signature;
+       sig = mono_method_signature (cfg->method);
 
-       cinfo = get_call_info (sig, FALSE);
+       cinfo = get_call_info (cfg, sig, FALSE);
 
        if (sig->ret->type != MONO_TYPE_VOID) {
                switch (cinfo->ret.storage) {
                case ArgInIReg:
                case ArgInFReg:
+                       cfg->ret->opcode = OP_REGVAR;
+                       cfg->ret->inst_c0 = cinfo->ret.reg;
+                       break;
                case ArgInIRegPair:
-                       m->ret->opcode = OP_REGVAR;
-                       m->ret->inst_c0 = cinfo->ret.reg;
+                       if (((sig->ret->type == MONO_TYPE_I8) || (sig->ret->type == MONO_TYPE_U8))) {
+                               MonoInst *low = get_vreg_to_inst (cfg, cfg->ret->dreg + 1);
+                               MonoInst *high = get_vreg_to_inst (cfg, cfg->ret->dreg + 2);
+
+                               low->opcode = OP_REGVAR;
+                               low->dreg = cinfo->ret.reg + 1;
+                               high->opcode = OP_REGVAR;
+                               high->dreg = cinfo->ret.reg;
+                       }
+                       cfg->ret->opcode = OP_REGVAR;
+                       cfg->ret->inst_c0 = cinfo->ret.reg;
                        break;
                case ArgOnStack:
 #ifdef SPARCV9
                        g_assert_not_reached ();
 #else
                        /* valuetypes */
-                       m->ret->opcode = OP_REGOFFSET;
-                       m->ret->inst_basereg = sparc_fp;
-                       m->ret->inst_offset = 64;
+                       cfg->vret_addr->opcode = OP_REGOFFSET;
+                       cfg->vret_addr->inst_basereg = sparc_fp;
+                       cfg->vret_addr->inst_offset = 64;
 #endif
                        break;
                default:
                        NOT_IMPLEMENTED;
                }
-               m->ret->dreg = m->ret->inst_c0;
+               cfg->ret->dreg = cfg->ret->inst_c0;
        }
 
        /*
@@ -806,7 +847,7 @@ mono_arch_allocate_vars (MonoCompile *m)
         */
 
        /* Locals are allocated backwards from %fp */
-       m->frame_reg = sparc_fp;
+       cfg->frame_reg = sparc_fp;
        offset = 0;
 
        /* 
@@ -816,26 +857,29 @@ mono_arch_allocate_vars (MonoCompile *m)
        if (header->num_clauses)
                offset += sizeof (gpointer) * 2;
 
-       if (m->method->save_lmf) {
+       if (cfg->method->save_lmf) {
                offset += sizeof (MonoLMF);
-               m->arch.lmf_offset = offset;
+               cfg->arch.lmf_offset = offset;
        }
 
-       curinst = m->locals_start;
-       for (i = curinst; i < m->num_varinfo; ++i) {
-               inst = m->varinfo [i];
+       curinst = cfg->locals_start;
+       for (i = curinst; i < cfg->num_varinfo; ++i) {
+               inst = cfg->varinfo [i];
 
-               if (inst->opcode == OP_REGVAR) {
+               if ((inst->opcode == OP_REGVAR) || (inst->opcode == OP_REGOFFSET)) {
                        //g_print ("allocating local %d to %s\n", i, mono_arch_regname (inst->dreg));
                        continue;
                }
 
-               /* inst->unused indicates native sized value types, this is used by the
+               if (inst->flags & MONO_INST_IS_DEAD)
+                       continue;
+
+               /* 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);
+                       size = mini_type_stack_size (cfg->generic_sharing_context, inst->inst_vtype, &align);
 
                /* 
                 * This is needed since structures containing doubles must be doubleword 
@@ -861,11 +905,11 @@ mono_arch_allocate_vars (MonoCompile *m)
        }
 
        if (sig->call_convention == MONO_CALL_VARARG) {
-               m->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
+               cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
        }
 
        for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
-               inst = m->varinfo [i];
+               inst = cfg->args [i];
                if (inst->opcode != OP_REGVAR) {
                        ArgInfo *ainfo = &cinfo->args [i];
                        gboolean inreg = TRUE;
@@ -888,6 +932,7 @@ mono_arch_allocate_vars (MonoCompile *m)
 #endif
 
                        /* FIXME: Allocate volatile arguments to registers */
+                       /* FIXME: This makes the argument holding a vtype address into volatile */
                        if (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
                                inreg = FALSE;
 
@@ -904,10 +949,22 @@ mono_arch_allocate_vars (MonoCompile *m)
 
                        switch (storage) {
                        case ArgInIReg:
-                       case ArgInIRegPair:
                                inst->opcode = OP_REGVAR;
                                inst->dreg = sparc_i0 + ainfo->reg;
                                break;
+                       case ArgInIRegPair:
+                               if (inst->type == STACK_I8) {
+                                       MonoInst *low = get_vreg_to_inst (cfg, inst->dreg + 1);
+                                       MonoInst *high = get_vreg_to_inst (cfg, inst->dreg + 2);
+
+                                       low->opcode = OP_REGVAR;
+                                       low->dreg = sparc_i0 + ainfo->reg + 1;
+                                       high->opcode = OP_REGVAR;
+                                       high->dreg = sparc_i0 + ainfo->reg;
+                               }
+                               inst->opcode = OP_REGVAR;
+                               inst->dreg = sparc_i0 + ainfo->reg;                                     
+                               break;
                        case ArgInFloatReg:
                        case ArgInDoubleReg:
                                /* 
@@ -952,291 +1009,403 @@ mono_arch_allocate_vars (MonoCompile *m)
                                 * are destructively modified in a lot of places in inssel.brg.
                                 */
                                MonoInst *indir;
-                               MONO_INST_NEW (m, indir, 0);
+                               MONO_INST_NEW (cfg, indir, 0);
                                *indir = *inst;
-                               inst->opcode = OP_SPARC_INARG_VT;
+                               inst->opcode = OP_VTARG_ADDR;
                                inst->inst_left = indir;
                        }
                }
        }
 
+       /* Add a properly aligned dword for use by int<->float conversion opcodes */
+       offset += 8;
+       offset = ALIGN_TO (offset, 8);
+       cfg->arch.float_spill_slot_offset = offset;
+
        /* 
         * spillvars are stored between the normal locals and the storage reserved
         * by the ABI.
         */
 
-       m->stack_offset = offset;
-
-       /* Add a properly aligned dword for use by int<->float conversion opcodes */
-       m->spill_count ++;
-       mono_spillvar_offset_float (m, 0);
+       cfg->stack_offset = offset;
 
        g_free (cinfo);
 }
 
-/* 
- * take the arguments and generate the arch-specific
- * instructions to properly call the function in call.
- * This includes pushing, moving arguments to the right register
- * etc.
- */
-MonoCallInst*
-mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
-       MonoInst *arg, *in;
+void
+mono_arch_create_vars (MonoCompile *cfg)
+{
        MonoMethodSignature *sig;
-       int i, n;
-       CallInfo *cinfo;
-       ArgInfo *ainfo;
-       guint32 extra_space = 0;
 
-       sig = call->signature;
-       n = sig->param_count + sig->hasthis;
-       
-       cinfo = get_call_info (sig, sig->pinvoke);
+       sig = mono_method_signature (cfg->method);
 
-       for (i = 0; i < n; ++i) {
-               ainfo = cinfo->args + i;
+       if (MONO_TYPE_ISSTRUCT ((sig->ret))) {
+               cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
+               if (G_UNLIKELY (cfg->verbose_level > 1)) {
+                       printf ("vret_addr = ");
+                       mono_print_ins (cfg->vret_addr);
+               }
+       }
+}
 
-               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;
+static void
+add_outarg_reg (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int reg, guint32 sreg)
+{
+       MonoInst *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_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;
-               }
+       MONO_INST_NEW (cfg, arg, 0);
 
-               if (is_virtual && i == 0) {
-                       /* the argument will be attached to the call instruction */
-                       in = call->args [i];
-               } else {
-                       MONO_INST_NEW (cfg, arg, OP_OUTARG);
-                       in = call->args [i];
-                       arg->cil_code = in->cil_code;
-                       arg->inst_left = in;
-                       arg->type = in->type;
-                       /* prepend, we'll need to reverse them later */
-                       arg->next = call->out_args;
-                       call->out_args = arg;
-
-                       if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(sig->params [i - sig->hasthis]))) {
-                               MonoInst *inst;
-                               gint align;
-                               guint32 offset, pad;
-                               guint32 size;
+       arg->sreg1 = sreg;
 
-#ifdef SPARCV9
-                               if (sig->pinvoke)
-                                       NOT_IMPLEMENTED;
-#endif
+       switch (storage) {
+       case ArgInIReg:
+               arg->opcode = OP_MOVE;
+               arg->dreg = mono_alloc_ireg (cfg);
 
-                               if (sig->params [i - sig->hasthis]->type == MONO_TYPE_TYPEDBYREF) {
-                                       size = sizeof (MonoTypedRef);
-                                       align = sizeof (gpointer);
-                               }
-                               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);
+               mono_call_inst_add_outarg_reg (cfg, call, arg->dreg, reg, FALSE);
+               break;
+       case ArgInFloatReg:
+               arg->opcode = OP_FMOVE;
+               arg->dreg = mono_alloc_freg (cfg);
 
-                               /* 
-                                * We use OP_OUTARG_VT to copy the valuetype to a stack location, then
-                                * use the normal OUTARG opcodes to pass the address of the location to
-                                * the callee.
-                                */
-                               MONO_INST_NEW (cfg, inst, OP_OUTARG_VT);
-                               inst->inst_left = in;
+               mono_call_inst_add_outarg_reg (cfg, call, arg->dreg, reg, TRUE);
+               break;
+       default:
+               g_assert_not_reached ();
+       }
 
-                               /* The first 6 argument locations are reserved */
-                               if (cinfo->stack_usage < 6 * sizeof (gpointer))
-                                       cinfo->stack_usage = 6 * sizeof (gpointer);
+       MONO_ADD_INS (cfg->cbb, arg);
+}
 
-                               offset = ALIGN_TO ((ARGS_OFFSET - STACK_BIAS) + cinfo->stack_usage, align);
-                               pad = offset - ((ARGS_OFFSET - STACK_BIAS) + cinfo->stack_usage);
+static void
+add_outarg_load (MonoCompile *cfg, MonoCallInst *call, int opcode, int basereg, int offset, int reg)
+{
+       MonoInst *arg;
+       int dreg = mono_alloc_ireg (cfg);
 
-                               inst->inst_c1 = STACK_BIAS + offset;
-                               inst->unused = size;
-                               arg->inst_left = inst;
+       EMIT_NEW_LOAD_MEMBASE (cfg, arg, OP_LOAD_MEMBASE, dreg, sparc_sp, offset);
+       MONO_ADD_INS (cfg->cbb, arg);
 
-                               cinfo->stack_usage += size;
-                               cinfo->stack_usage += pad;
-                       }
+       mono_call_inst_add_outarg_reg (cfg, call, dreg, reg, FALSE);
+}
 
-                       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;
-                               call->used_iregs |= 1 << ainfo->reg;
-
-                               if ((i >= sig->hasthis) && (sig->params [i - sig->hasthis]->type == MONO_TYPE_R8)) {
-                                       /*
-                                        * The OUTARG (freg) implementation needs an extra dword to store
-                                        * the temporary value.
-                                        */
-                                       extra_space += 8;
-                               }
-                               break;
-                       case ArgOnStack:
-                               arg->opcode = OP_SPARC_OUTARG_MEM;
-                               break;
-                       case ArgOnStackPair:
-                               arg->opcode = OP_SPARC_OUTARG_MEMPAIR;
-                               break;
-                       case ArgInSplitRegStack:
-                               arg->opcode = OP_SPARC_OUTARG_SPLIT_REG_STACK;
-                               arg->unused = 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;
-                               break;
-                       case ArgInDoubleReg:
-                               arg->opcode = OP_SPARC_OUTARG_DOUBLE_REG;
-                               arg->unused = sparc_f0 + ainfo->reg;
-                               break;
-                       default:
-                               NOT_IMPLEMENTED;
-                       }
+static void
+emit_pass_long (MonoCompile *cfg, MonoCallInst *call, ArgInfo *ainfo, MonoInst *in)
+{
+       int offset = ARGS_OFFSET + ainfo->offset;
 
-                       arg->inst_basereg = sparc_sp;
-                       arg->inst_imm = ARGS_OFFSET + ainfo->offset;
-               }
+       switch (ainfo->storage) {
+       case ArgInIRegPair:
+               add_outarg_reg (cfg, call, ArgInIReg, sparc_o0 + ainfo->reg + 1, in->dreg + 1);
+               add_outarg_reg (cfg, call, ArgInIReg, sparc_o0 + ainfo->reg, in->dreg + 2);
+               break;
+       case ArgOnStackPair:
+               MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, sparc_sp, offset, in->dreg + 2);
+               MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, sparc_sp, offset + 4, in->dreg + 1);
+               break;
+       case ArgInSplitRegStack:
+               add_outarg_reg (cfg, call, ArgInIReg, sparc_o0 + ainfo->reg, in->dreg + 2);
+               MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, sparc_sp, offset + 4, in->dreg + 1);
+               break;
+       default:
+               g_assert_not_reached ();
        }
+}
 
-       /*
-        * Reverse the call->out_args list.
-        */
-       {
-               MonoInst *prev = NULL, *list = call->out_args, *next;
-               while (list) {
-                       next = list->next;
-                       list->next = prev;
-                       prev = list;
-                       list = next;
-               }
-               call->out_args = prev;
-       }
-       call->stack_usage = cinfo->stack_usage + extra_space;
-       cfg->param_area = MAX (cfg->param_area, call->stack_usage);
-       cfg->flags |= MONO_CFG_HAS_CALLS;
+static void
+emit_pass_double (MonoCompile *cfg, MonoCallInst *call, ArgInfo *ainfo, MonoInst *in)
+{
+       int offset = ARGS_OFFSET + ainfo->offset;
 
-       g_free (cinfo);
-       return call;
+       switch (ainfo->storage) {
+       case ArgInIRegPair:
+               /* floating-point <-> integer transfer must go through memory */
+               MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, sparc_sp, offset, in->dreg);
+
+               /* Load into a register pair */
+               add_outarg_load (cfg, call, OP_LOADI4_MEMBASE, sparc_sp, offset, sparc_o0 + ainfo->reg);
+               add_outarg_load (cfg, call, OP_LOADI4_MEMBASE, sparc_sp, offset + 4, sparc_o0 + ainfo->reg + 1);
+               break;
+       case ArgOnStackPair:
+               MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, sparc_sp, offset, in->dreg);
+               break;
+       case ArgInSplitRegStack:
+               /* floating-point <-> integer transfer must go through memory */
+               MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, sparc_sp, offset, in->dreg);
+               /* Load most significant word into register */
+               add_outarg_load (cfg, call, OP_LOADI4_MEMBASE, sparc_sp, offset, sparc_o0 + ainfo->reg);
+               break;
+       default:
+               g_assert_not_reached ();
+       }
 }
 
-/* Map opcode to the sparc condition codes */
-static inline SparcCond
-opcode_to_sparc_cond (int opcode)
+static void
+emit_pass_float (MonoCompile *cfg, MonoCallInst *call, ArgInfo *ainfo, MonoInst *in)
 {
-       switch (opcode) {
-       case OP_FBGE:
-               return sparc_fbge;
-       case OP_FBLE:
-               return sparc_fble;
-       case OP_FBEQ:
-       case OP_FCEQ:
-               return sparc_fbe;
-       case OP_FBLT:
-       case OP_FCLT:
-       case OP_FCLT_UN:
-               return sparc_fbl;
-       case OP_FBGT:
-       case OP_FCGT:
-       case OP_FCGT_UN:
-               return sparc_fbg;
-       case CEE_BEQ:
-       case OP_IBEQ:
-       case OP_CEQ:
-       case OP_ICEQ:
-       case OP_COND_EXC_EQ:
-               return sparc_be;
-       case CEE_BNE_UN:
-       case OP_COND_EXC_NE_UN:
-       case OP_IBNE_UN:
-               return sparc_bne;
-       case CEE_BLT:
-       case OP_IBLT:
-       case OP_CLT:
-       case OP_ICLT:
-       case OP_COND_EXC_LT:
-               return sparc_bl;
-       case CEE_BLT_UN:
-       case OP_IBLT_UN:
-       case OP_CLT_UN:
-       case OP_ICLT_UN:
-       case OP_COND_EXC_LT_UN:
-               return sparc_blu;
-       case CEE_BGT:
-       case OP_IBGT:
-       case OP_CGT:
-       case OP_ICGT:
-       case OP_COND_EXC_GT:
-               return sparc_bg;
-       case CEE_BGT_UN:
-       case OP_IBGT_UN:
-       case OP_CGT_UN:
-       case OP_ICGT_UN:
-       case OP_COND_EXC_GT_UN:
-               return sparc_bgu;
-       case CEE_BGE:
-       case OP_IBGE:
-       case OP_COND_EXC_GE:
-               return sparc_bge;
-       case CEE_BGE_UN:
-       case OP_IBGE_UN:
-       case OP_COND_EXC_GE_UN:
-               return sparc_beu;
-       case CEE_BLE:
-       case OP_IBLE:
-       case OP_COND_EXC_LE:
-               return sparc_ble;
-       case CEE_BLE_UN:
-       case OP_IBLE_UN:
-       case OP_COND_EXC_LE_UN:
-               return sparc_bleu;
-       case OP_COND_EXC_OV:
-       case OP_COND_EXC_IOV:
-               return sparc_bvs;
-       case OP_COND_EXC_C:
-       case OP_COND_EXC_IC:
-               return sparc_bcs;
-       case OP_COND_EXC_NO:
-       case OP_COND_EXC_NC:
-               NOT_IMPLEMENTED;
+       int offset = ARGS_OFFSET + ainfo->offset;
+
+       switch (ainfo->storage) {
+       case ArgInIReg:
+               /* floating-point <-> integer transfer must go through memory */
+               MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, sparc_sp, offset, in->dreg);
+               add_outarg_load (cfg, call, OP_LOADI4_MEMBASE, sparc_sp, offset, sparc_o0 + ainfo->reg);
+               break;
+       case ArgOnStack:
+               MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, sparc_sp, offset, in->dreg);
+               break;
        default:
                g_assert_not_reached ();
-               return sparc_be;
        }
 }
 
-#define COMPUTE_DISP(ins) \
+static void
+emit_pass_other (MonoCompile *cfg, MonoCallInst *call, ArgInfo *ainfo, MonoType *arg_type, MonoInst *in);
+
+static void
+emit_pass_vtype (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo, ArgInfo *ainfo, MonoType *arg_type, MonoInst *in, gboolean pinvoke)
+{
+       MonoInst *arg;
+       guint32 align, offset, pad, size;
+
+       if (arg_type->type == MONO_TYPE_TYPEDBYREF) {
+               size = sizeof (MonoTypedRef);
+               align = sizeof (gpointer);
+       }
+       else if (pinvoke)
+               size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
+       else {
+               /* 
+                * Other backends use mono_type_stack_size (), but that
+                * aligns the size to 8, 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.
+                */
+               size = mono_class_value_size (in->klass, &align);
+       }
+
+       /* The first 6 argument locations are reserved */
+       if (cinfo->stack_usage < 6 * sizeof (gpointer))
+               cinfo->stack_usage = 6 * sizeof (gpointer);
+
+       offset = ALIGN_TO ((ARGS_OFFSET - STACK_BIAS) + cinfo->stack_usage, align);
+       pad = offset - ((ARGS_OFFSET - STACK_BIAS) + cinfo->stack_usage);
+
+       cinfo->stack_usage += size;
+       cinfo->stack_usage += pad;
+
+       /* 
+        * We use OP_OUTARG_VT to copy the valuetype to a stack location, then
+        * use the normal OUTARG opcodes to pass the address of the location to
+        * the callee.
+        */
+       if (size > 0) {
+               MONO_INST_NEW (cfg, arg, OP_OUTARG_VT);
+               arg->sreg1 = in->dreg;
+               arg->klass = in->klass;
+               arg->backend.size = size;
+               arg->inst_p0 = call;
+               arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
+               memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
+               ((ArgInfo*)(arg->inst_p1))->offset = STACK_BIAS + offset;
+               MONO_ADD_INS (cfg->cbb, arg);
+
+               MONO_INST_NEW (cfg, arg, OP_ADD_IMM);
+               arg->dreg = mono_alloc_preg (cfg);
+               arg->sreg1 = sparc_sp;
+               arg->inst_imm = STACK_BIAS + offset;
+               MONO_ADD_INS (cfg->cbb, arg);
+
+               emit_pass_other (cfg, call, ainfo, NULL, arg);
+       }
+}
+
+static void
+emit_pass_other (MonoCompile *cfg, MonoCallInst *call, ArgInfo *ainfo, MonoType *arg_type, MonoInst *in)
+{
+       int offset = ARGS_OFFSET + ainfo->offset;
+       int opcode;
+
+       switch (ainfo->storage) {
+       case ArgInIReg:
+               add_outarg_reg (cfg, call, ArgInIReg, sparc_o0 + ainfo->reg, in->dreg);
+               break;
+       case ArgOnStack:
+#ifdef SPARCV9
+               NOT_IMPLEMENTED;
+#else
+               if (offset & 0x1)
+                       opcode = OP_STOREI1_MEMBASE_REG;
+               else if (offset & 0x2)
+                       opcode = OP_STOREI2_MEMBASE_REG;
+               else
+                       opcode = OP_STOREI4_MEMBASE_REG;
+               MONO_EMIT_NEW_STORE_MEMBASE (cfg, opcode, sparc_sp, offset, in->dreg);
+#endif
+               break;
+       default:
+               g_assert_not_reached ();
+       }
+}
+
+static void
+emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
+{
+       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_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, sparc_sp, ARGS_OFFSET + cinfo->sig_cookie.offset, tmp_sig);
+}
+
+void
+mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
+{
+       MonoInst *in;
+       MonoMethodSignature *sig;
+       int i, n;
+       CallInfo *cinfo;
+       ArgInfo *ainfo;
+       guint32 extra_space = 0;
+
+       sig = call->signature;
+       n = sig->param_count + sig->hasthis;
+       
+       cinfo = get_call_info (cfg, sig, sig->pinvoke);
+
+       if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
+               /* Set the 'struct/union return pointer' location on the stack */
+               MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, sparc_sp, 64, call->vret_var->dreg);
+       }
+
+       for (i = 0; i < n; ++i) {
+               MonoType *arg_type;
+
+               ainfo = cinfo->args + i;
+
+               if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
+                       /* Emit the signature cookie just before the first implicit argument */
+                       emit_sig_cookie (cfg, call, cinfo);
+               }
+
+               in = call->args [i];
+
+               if (sig->hasthis && (i == 0))
+                       arg_type = &mono_defaults.object_class->byval_arg;
+               else
+                       arg_type = sig->params [i - sig->hasthis];
+
+               if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(sig->params [i - sig->hasthis])))
+                       emit_pass_vtype (cfg, call, cinfo, ainfo, arg_type, in, sig->pinvoke);
+               else if (!arg_type->byref && ((arg_type->type == MONO_TYPE_I8) || (arg_type->type == MONO_TYPE_U8)))
+                       emit_pass_long (cfg, call, ainfo, in);
+               else if (!arg_type->byref && (arg_type->type == MONO_TYPE_R8))
+                       emit_pass_double (cfg, call, ainfo, in);
+               else if (!arg_type->byref && (arg_type->type == MONO_TYPE_R4))
+                       emit_pass_float (cfg, call, ainfo, in);
+               else
+                       emit_pass_other (cfg, call, ainfo, arg_type, in);
+       }
+
+       /* 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);
+       }
+
+       call->stack_usage = cinfo->stack_usage + extra_space;
+
+       g_free (cinfo);
+}
+
+void
+mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
+{
+       ArgInfo *ainfo = (ArgInfo*)ins->inst_p1;
+       int size = ins->backend.size;
+
+       mini_emit_memcpy (cfg, sparc_sp, ainfo->offset, src->dreg, 0, size, 0);
+}
+
+void
+mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
+{
+       CallInfo *cinfo = get_call_info (cfg, mono_method_signature (method), FALSE);
+
+       switch (cinfo->ret.storage) {
+       case ArgInIReg:
+               MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
+               break;
+       case ArgInIRegPair:
+               MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg + 2);
+               MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg + 1, val->dreg + 1);
+               break;
+       case ArgInFReg:
+               if (mono_method_signature (method)->ret->type == MONO_TYPE_R4)
+                       MONO_EMIT_NEW_UNALU (cfg, OP_SETFRET, cfg->ret->dreg, val->dreg);
+               else
+                       MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
+               break;
+       default:
+               g_assert_not_reached ();
+       }
+
+       g_assert (cinfo);
+}
+
+int cond_to_sparc_cond [][3] = {
+       {sparc_be,   sparc_be,   sparc_fbe},
+       {sparc_bne,  sparc_bne,  0},
+       {sparc_ble,  sparc_ble,  sparc_fble},
+       {sparc_bge,  sparc_bge,  sparc_fbge},
+       {sparc_bl,   sparc_bl,   sparc_fbl},
+       {sparc_bg,   sparc_bg,   sparc_fbg},
+       {sparc_bleu, sparc_bleu, 0},
+       {sparc_beu,  sparc_beu,  0},
+       {sparc_blu,  sparc_blu,  sparc_fbl},
+       {sparc_bgu,  sparc_bgu,  sparc_fbg}
+};
+
+/* Map opcode to the sparc condition codes */
+static inline SparcCond
+opcode_to_sparc_cond (int opcode)
+{
+       CompRelation rel;
+       CompType t;
+
+       switch (opcode) {
+       case OP_COND_EXC_OV:
+       case OP_COND_EXC_IOV:
+               return sparc_bvs;
+       case OP_COND_EXC_C:
+       case OP_COND_EXC_IC:
+               return sparc_bcs;
+       case OP_COND_EXC_NO:
+       case OP_COND_EXC_NC:
+               NOT_IMPLEMENTED;
+       default:
+               rel = mono_opcode_to_cond (opcode);
+               t = mono_opcode_to_type (opcode, -1);
+
+               return cond_to_sparc_cond [rel][t];
+               break;
+       }
+
+       return -1;
+}
+
+#define COMPUTE_DISP(ins) \
 if (ins->flags & MONO_INST_BRLABEL) { \
         if (ins->inst_i0->inst_c0) \
            disp = (ins->inst_i0->inst_c0 - ((guint8*)code - cfg->native_code)) >> 2; \
@@ -1322,7 +1491,7 @@ if (ins->flags & MONO_INST_BRLABEL) { \
 #define EMIT_COND_SYSTEM_EXCEPTION_GENERAL(ins,cond,sexc_name,filldelay,icc) do {     \
                mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code,   \
                                    MONO_PATCH_INFO_EXC, sexc_name);  \
-        if (sparcv9) { \
+        if (sparcv9 && ((icc) != sparc_icc_short)) {          \
            sparc_branchp (code, 0, (cond), (icc), 0, 0); \
         } \
         else { \
@@ -1425,14 +1594,18 @@ emit_call (MonoCompile *cfg, guint32 *code, guint32 patch_type, gconstpointer da
        return code;
 }
 
-static void
-peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
+void
+mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
 {
-       MonoInst *ins, *last_ins = NULL;
-       ins = bb->code;
+}
 
-       while (ins) {
+void
+mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
+{
+       MonoInst *ins, *n, *last_ins = NULL;
+       ins = bb->code;
 
+       MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
                switch (ins->opcode) {
                case OP_MUL_IMM: 
                        /* remove unnecessary multiplication with 1 */
@@ -1440,8 +1613,7 @@ peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                                if (ins->dreg != ins->sreg1) {
                                        ins->opcode = OP_MOVE;
                                } else {
-                                       last_ins->next = ins->next;                             
-                                       ins = ins->next;                                
+                                       MONO_DELETE_INS (bb, ins);
                                        continue;
                                }
                        }
@@ -1458,8 +1630,7 @@ peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                            ins->inst_basereg == last_ins->inst_destbasereg &&
                            ins->inst_offset == last_ins->inst_offset) {
                                if (ins->dreg == last_ins->sreg1) {
-                                       last_ins->next = ins->next;                             
-                                       ins = ins->next;                                
+                                       MONO_DELETE_INS (bb, ins);
                                        continue;
                                } else {
                                        //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
@@ -1482,8 +1653,7 @@ peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                              ins->inst_offset == last_ins->inst_offset) {
 
                                if (ins->dreg == last_ins->dreg) {
-                                       last_ins->next = ins->next;                             
-                                       ins = ins->next;                                
+                                       MONO_DELETE_INS (bb, ins);
                                        continue;
                                } else {
                                        ins->opcode = OP_MOVE;
@@ -1512,14 +1682,12 @@ 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 &&
                                        ins->inst_offset == last_ins->inst_offset) {
                                if (ins->dreg == last_ins->sreg1) {
-                                       last_ins->next = ins->next;                             
-                                       ins = ins->next;                                
+                                       MONO_DELETE_INS (bb, ins);
                                        continue;
                                } else {
                                        //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
@@ -1528,14 +1696,12 @@ 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 &&
                                        ins->inst_offset == last_ins->inst_offset) {
                                if (ins->dreg == last_ins->sreg1) {
-                                       last_ins->next = ins->next;                             
-                                       ins = ins->next;                                
+                                       MONO_DELETE_INS (bb, ins);
                                        continue;
                                } else {
                                        //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
@@ -1559,18 +1725,17 @@ peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                                if (sparcv9) {
                                        last_ins->opcode = OP_STOREI8_MEMBASE_IMM;
                                        last_ins->inst_offset = ins->inst_offset;
-                                       last_ins->next = ins->next;                             
-                                       ins = ins->next;
+                                       MONO_DELETE_INS (bb, ins);
                                        continue;
                                }
                        }
                        break;
-               case CEE_BEQ:
-               case CEE_BNE_UN:
-               case CEE_BLT:
-               case CEE_BGT:
-               case CEE_BGE:
-               case CEE_BLE:
+               case OP_IBEQ:
+               case OP_IBNE_UN:
+               case OP_IBLT:
+               case OP_IBGT:
+               case OP_IBGE:
+               case OP_IBLE:
                case OP_COND_EXC_EQ:
                case OP_COND_EXC_GE:
                case OP_COND_EXC_GT:
@@ -1589,24 +1754,23 @@ peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                        if (v64 && (mono_method_get_header (cfg->method)->code_size < 10000) && last_ins && 
                                (last_ins->opcode == OP_COMPARE_IMM) &&
                                (last_ins->inst_imm == 0)) {
-                               MonoInst *next = ins->next;
                                switch (ins->opcode) {
-                               case CEE_BEQ:
+                               case OP_IBEQ:
                                        ins->opcode = OP_SPARC_BRZ;
                                        break;
-                               case CEE_BNE_UN:
+                               case OP_IBNE_UN:
                                        ins->opcode = OP_SPARC_BRNZ;
                                        break;
-                               case CEE_BLT:
+                               case OP_IBLT:
                                        ins->opcode = OP_SPARC_BRLZ;
                                        break;
-                               case CEE_BGT:
+                               case OP_IBGT:
                                        ins->opcode = OP_SPARC_BRGZ;
                                        break;
-                               case CEE_BGE:
+                               case OP_IBGE:
                                        ins->opcode = OP_SPARC_BRGEZ;
                                        break;
-                               case CEE_BLE:
+                               case OP_IBLE:
                                        ins->opcode = OP_SPARC_BRLEZ;
                                        break;
                                case OP_COND_EXC_EQ:
@@ -1632,21 +1796,16 @@ peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                                }
                                ins->sreg1 = last_ins->sreg1;
                                *last_ins = *ins;
-                               last_ins->next = next;
-                               ins = next;
+                               MONO_DELETE_INS (bb, ins);
                                continue;
                        }
                        break;
-               case CEE_CONV_I4:
-               case CEE_CONV_U4:
                case OP_MOVE:
                        /* 
                         * OP_MOVE reg, reg 
                         */
                        if (ins->dreg == ins->sreg1) {
-                               if (last_ins)
-                                       last_ins->next = ins->next;                             
-                               ins = ins->next;
+                               MONO_DELETE_INS (bb, ins);
                                continue;
                        }
                        /* 
@@ -1656,8 +1815,7 @@ peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                        if (last_ins && last_ins->opcode == OP_MOVE &&
                            ins->sreg1 == last_ins->dreg &&
                            ins->dreg == last_ins->sreg1) {
-                               last_ins->next = ins->next;                             
-                               ins = ins->next;                                
+                               MONO_DELETE_INS (bb, ins);
                                continue;
                        }
                        break;
@@ -1668,815 +1826,13 @@ 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)
-{
-       MonoSpillInfo **si, *info;
-       int i = 0;
-
-       si = &cfg->spill_info_float; 
-       
-       while (i <= spillvar) {
-
-               if (!*si) {
-                       *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
-                       info->next = NULL;
-                       cfg->stack_offset += sizeof (double);
-                       cfg->stack_offset = ALIGN_TO (cfg->stack_offset, 8);
-                       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;
-}
-
-/*
- * 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)
+void
+mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
 {
-       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;
-       }
-}
-
 static void
 sparc_patch (guint32 *code, const gpointer target)
 {
@@ -2650,7 +2006,7 @@ emit_save_sp_to_lmf (MonoCompile *cfg, guint32 *code)
 }
 
 static guint32*
-emit_vret_token (MonoInst *ins, guint32 *code)
+emit_vret_token (MonoGenericSharingContext *gsctx, MonoInst *ins, guint32 *code)
 {
        MonoCallInst *call = (MonoCallInst*)ins;
        guint32 size;
@@ -2661,7 +2017,7 @@ emit_vret_token (MonoInst *ins, guint32 *code)
         */
        if (call->signature->pinvoke && MONO_TYPE_ISSTRUCT(call->signature->ret)) {
                if (call->signature->ret->type == MONO_TYPE_TYPEDBYREF)
-                       size = mono_type_stack_size (call->signature->ret, NULL);
+                       size = mini_type_stack_size (gsctx, call->signature->ret, NULL);
                else
                        size = mono_class_native_size (call->signature->ret->data.klass, NULL);
                sparc_unimp (code, size & 0xfff);
@@ -2674,29 +2030,28 @@ 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:
        case OP_VOIDCALL_MEMBASE:
                break;
-       case CEE_CALL:
+       case OP_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:
@@ -2720,6 +2075,9 @@ emit_move_return_value (MonoInst *ins, guint32 *code)
        case OP_VCALL:
        case OP_VCALL_REG:
        case OP_VCALL_MEMBASE:
+       case OP_VCALL2:
+       case OP_VCALL2_REG:
+       case OP_VCALL2_MEMBASE:
                break;
        default:
                NOT_IMPLEMENTED;
@@ -2745,9 +2103,9 @@ emit_load_volatile_arguments (MonoCompile *cfg, guint32 *code)
 
        /* FIXME: Generate intermediate code instead */
 
-       sig = method->signature;
+       sig = mono_method_signature (method);
 
-       cinfo = get_call_info (sig, FALSE);
+       cinfo = get_call_info (cfg, sig, FALSE);
        
        /* This is the opposite of the code in emit_prolog */
 
@@ -2755,7 +2113,8 @@ emit_load_volatile_arguments (MonoCompile *cfg, guint32 *code)
                ArgInfo *ainfo = cinfo->args + i;
                gint32 stack_offset;
                MonoType *arg_type;
-               inst = cfg->varinfo [i];
+
+               inst = cfg->args [i];
 
                if (sig->hasthis && (i == 0))
                        arg_type = &mono_defaults.object_class->byval_arg;
@@ -2881,42 +2240,205 @@ mono_sparc_is_virtual_call (guint32 *code)
 }
 
 /*
- * mono_sparc_get_vcall_slot_addr:
+ * mono_arch_get_vcall_slot:
  *
  *  Determine the vtable slot used by a virtual call.
  */
-gpointer*
-mono_sparc_get_vcall_slot_addr (guint32 *code, gpointer *fp)
+gpointer
+mono_arch_get_vcall_slot (guint8 *code8, gpointer *regs, int *displacement)
 {
+       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);
+       *displacement = 0;
+
+       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);
+                       gint32 disp = (((gint32)(sparc_inst_imm13 (prev_ins))) << 19) >> 19;
                        gpointer base_val;
 
                        g_assert (sparc_inst_rd (prev_ins) == sparc_inst_rs1 (ins));
 
                        g_assert ((base >= sparc_o0) && (base <= sparc_i7));
-                       
-                       base_val = fp [base - 16];
 
-                       return (gpointer)((guint8*)base_val + disp);
-               }
-               else
-                       g_assert_not_reached ();
-       }
-       else
-               g_assert_not_reached ();
+                       base_val = regs [base];
+
+                       *displacement = disp;
+
+                       return (gpointer)base_val;
+               }
+               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];
+
+                       *displacement = disp;
+
+                       return (gpointer)base_val;
+               } else
+                       g_assert_not_reached ();
+       }
+       else
+               g_assert_not_reached ();
+
+       return NULL;
+}
+
+gpointer*
+mono_arch_get_vcall_slot_addr (guint8 *code, gpointer *regs)
+{
+       gpointer vt;
+       int displacement;
+       vt = mono_arch_get_vcall_slot (code, regs, &displacement);
+       if (!vt)
+               return NULL;
+       return (gpointer*)((char*)vt + displacement);
+}
+
+#define CMP_SIZE 3
+#define BR_SMALL_SIZE 2
+#define BR_LARGE_SIZE 2
+#define JUMP_IMM_SIZE 5
+#define ENABLE_WRONG_METHOD_CHECK 0
+
+/*
+ * LOCKING: called with the domain lock held
+ */
+gpointer
+mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
+       gpointer fail_tramp)
+{
+       int i;
+       int size = 0;
+       guint32 *code, *start;
+
+       g_assert (!fail_tramp);
+
+       for (i = 0; i < count; ++i) {
+               MonoIMTCheckItem *item = imt_entries [i];
+               if (item->is_equals) {
+                       if (item->check_target_idx) {
+                               if (!item->compare_done)
+                                       item->chunk_size += CMP_SIZE;
+                               item->chunk_size += BR_SMALL_SIZE + JUMP_IMM_SIZE;
+                       } else {
+                               item->chunk_size += JUMP_IMM_SIZE;
+#if ENABLE_WRONG_METHOD_CHECK
+                               item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
+#endif
+                       }
+               } else {
+                       item->chunk_size += CMP_SIZE + BR_LARGE_SIZE;
+                       imt_entries [item->check_target_idx]->compare_done = TRUE;
+               }
+               size += item->chunk_size;
+       }
+       code = mono_domain_code_reserve (domain, size * 4);
+       start = code;
+
+       for (i = 0; i < count; ++i) {
+               MonoIMTCheckItem *item = imt_entries [i];
+               item->code_target = (guint8*)code;
+               if (item->is_equals) {
+                       if (item->check_target_idx) {
+                               if (!item->compare_done) {
+                                       sparc_set (code, (guint32)item->key, sparc_g5);
+                                       sparc_cmp (code, MONO_ARCH_IMT_REG, sparc_g5);
+                               }
+                               item->jmp_code = (guint8*)code;
+                               sparc_branch (code, 0, sparc_bne, 0);
+                               sparc_nop (code);
+                               sparc_set (code, ((guint32)(&(vtable->vtable [item->value.vtable_slot]))), sparc_g5);
+                               sparc_ld (code, sparc_g5, 0, sparc_g5);
+                               sparc_jmpl (code, sparc_g5, sparc_g0, sparc_g0);
+                               sparc_nop (code);
+                       } else {
+                               /* enable the commented code to assert on wrong method */
+#if ENABLE_WRONG_METHOD_CHECK
+                               g_assert_not_reached ();
+#endif
+                               sparc_set (code, ((guint32)(&(vtable->vtable [item->value.vtable_slot]))), sparc_g5);
+                               sparc_ld (code, sparc_g5, 0, sparc_g5);
+                               sparc_jmpl (code, sparc_g5, sparc_g0, sparc_g0);
+                               sparc_nop (code);
+#if ENABLE_WRONG_METHOD_CHECK
+                               g_assert_not_reached ();
+#endif
+                       }
+               } else {
+                       sparc_set (code, (guint32)item->key, sparc_g5);
+                       sparc_cmp (code, MONO_ARCH_IMT_REG, sparc_g5);
+                       item->jmp_code = (guint8*)code;
+                       sparc_branch (code, 0, sparc_beu, 0);
+                       sparc_nop (code);
+               }
+       }
+       /* patch the branches to get to the target items */
+       for (i = 0; i < count; ++i) {
+               MonoIMTCheckItem *item = imt_entries [i];
+               if (item->jmp_code) {
+                       if (item->check_target_idx) {
+                               sparc_patch ((guint32*)item->jmp_code, imt_entries [item->check_target_idx]->code_target);
+                       }
+               }
+       }
+
+       mono_arch_flush_icache ((guint8*)start, (code - start) * 4);
+
+       mono_stats.imt_thunks_size += (code - start) * 4;
+       g_assert (code - start <= size);
+       return start;
+}
+
+MonoMethod*
+mono_arch_find_imt_method (gpointer *regs, guint8 *code)
+{
+#ifdef SPARCV9
+       g_assert_not_reached ();
+#endif
+
+       return (MonoMethod*)regs [sparc_g1];
+}
+
+MonoObject*
+mono_arch_find_this_argument (gpointer *regs, MonoMethod *method, MonoGenericSharingContext *gsctx)
+{
+       mono_sparc_flushw ();
 
-       return FALSE;
+       return (gpointer)regs [sparc_o0];
 }
 
 /*
@@ -2936,9 +2458,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
        int max_len, cpos;
        const char *spec;
 
-       if (cfg->opt & MONO_OPT_PEEPHOLE)
-               peephole_pass (cfg, bb);
-
        if (cfg->verbose_level > 2)
                g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
 
@@ -2948,15 +2467,12 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                NOT_IMPLEMENTED;
        }
 
-       ins = bb->code;
-       while (ins) {
+       MONO_BB_FOR_EACH_INS (bb, ins) {
                guint8* code_start;
 
                offset = (guint8*)code - cfg->native_code;
 
-               spec = ins_spec [ins->opcode];
-               if (!spec)
-                       spec = ins_spec [CEE_ADD];
+               spec = ins_get_spec (ins->opcode);
 
                max_len = ((guint8 *)spec)[MONO_INST_LEN];
 
@@ -3013,23 +2529,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_STORE_MEMBASE_REG:
                        EMIT_STORE_MEMBASE_REG (ins, sti);
                        break;
-               case CEE_LDIND_I:
-#ifdef SPARCV9
-                       sparc_ldx (code, ins->inst_c0, sparc_g0, ins->dreg);
-#else
-                       sparc_ld (code, ins->inst_c0, sparc_g0, ins->dreg);
-#endif
-                       break;
-               case CEE_LDIND_I4:
-#ifdef SPARCV9
-                       sparc_ldsw (code, ins->inst_c0, sparc_g0, ins->dreg);
-#else
-                       sparc_ld (code, ins->inst_c0, sparc_g0, ins->dreg);
-#endif
-                       break;
-               case CEE_LDIND_U4:
-                       sparc_ld (code, ins->inst_c0, sparc_g0, ins->dreg);
-                       break;
                case OP_LOADU4_MEM:
                        sparc_set (code, ins->inst_c0, ins->dreg);
                        sparc_ld (code, ins->dreg, sparc_g0, ins->dreg);
@@ -3068,22 +2567,23 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        EMIT_LOAD_MEMBASE (ins, ldx);
                        break;
 #endif
-               case CEE_CONV_I1:
+               case OP_ICONV_TO_I1:
                        sparc_sll_imm (code, ins->sreg1, 24, sparc_o7);
                        sparc_sra_imm (code, sparc_o7, 24, ins->dreg);
                        break;
-               case CEE_CONV_I2:
+               case OP_ICONV_TO_I2:
                        sparc_sll_imm (code, ins->sreg1, 16, sparc_o7);
                        sparc_sra_imm (code, sparc_o7, 16, ins->dreg);
                        break;
-               case CEE_CONV_U1:
+               case OP_ICONV_TO_U1:
                        sparc_and_imm (code, FALSE, ins->sreg1, 0xff, ins->dreg);
                        break;
-               case CEE_CONV_U2:
+               case OP_ICONV_TO_U2:
                        sparc_sll_imm (code, ins->sreg1, 16, sparc_o7);
                        sparc_srl_imm (code, sparc_o7, 16, ins->dreg);
                        break;
-               case CEE_CONV_OVF_U4:
+               case OP_LCONV_TO_OVF_U4:
+               case OP_ICONV_TO_OVF_U4:
                        /* Only used on V9 */
                        sparc_cmp_imm (code, ins->sreg1, 0);
                        mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code,
@@ -3099,20 +2599,11 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        sparc_nop (code);
                        sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
                        break;
-               case CEE_CONV_OVF_I4_UN:
+               case OP_LCONV_TO_OVF_I4_UN:
+               case OP_ICONV_TO_OVF_I4_UN:
                        /* Only used on V9 */
                        NOT_IMPLEMENTED;
                        break;
-               case CEE_CONV_U:
-               case CEE_CONV_U8:
-                       /* Only used on V9 */
-                       sparc_srl_imm (code, ins->sreg1, 0, ins->dreg);
-                       break;
-               case CEE_CONV_I:
-               case CEE_CONV_I8:
-                       /* Only used on V9 */
-                       sparc_sra_imm (code, ins->sreg1, 0, ins->dreg);
-                       break;
                case OP_COMPARE:
                case OP_LCOMPARE:
                case OP_ICOMPARE:
@@ -3127,24 +2618,20 @@ 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:
+               case OP_BREAK:
                        /*
                         * gdb does not like encountering 'ta 1' in the debugged code. So 
                         * instead of emitting a trap, we emit a call a C function and place a 
                         * 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_break);
                        EMIT_CALL();
                        break;
                case OP_ADDCC:
                case OP_IADDCC:
                        sparc_add (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
                        break;
-               case CEE_ADD:
                case OP_IADD:
                        sparc_add (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
                        break;
@@ -3167,7 +2654,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_ISUBCC:
                        sparc_sub (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
                        break;
-               case CEE_SUB:
                case OP_ISUB:
                        sparc_sub (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
                        break;
@@ -3186,7 +2672,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_ISBB_IMM:
                        EMIT_ALU_IMM (ins, subx, TRUE);
                        break;
-               case CEE_AND:
                case OP_IAND:
                        sparc_and (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
                        break;
@@ -3194,7 +2679,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_IAND_IMM:
                        EMIT_ALU_IMM (ins, and, FALSE);
                        break;
-               case CEE_DIV:
                case OP_IDIV:
                        /* Sign extend sreg1 into %y */
                        sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
@@ -3202,12 +2686,12 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        sparc_sdiv (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
                        EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
                        break;
-               case CEE_DIV_UN:
                case OP_IDIV_UN:
                        sparc_wry (code, sparc_g0, sparc_g0);
                        sparc_udiv (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
                        break;
-               case OP_DIV_IMM: {
+               case OP_DIV_IMM:
+               case OP_IDIV_IMM: {
                        int i, imm;
 
                        /* Transform division into a shift */
@@ -3240,7 +2724,10 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        }
                        break;
                }
-               case CEE_REM:
+               case OP_IDIV_UN_IMM:
+                       sparc_wry (code, sparc_g0, sparc_g0);
+                       EMIT_ALU_IMM (ins, udiv, FALSE);
+                       break;
                case OP_IREM:
                        /* Sign extend sreg1 into %y */
                        sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
@@ -3250,7 +2737,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        sparc_smul (code, FALSE, ins->sreg2, sparc_o7, sparc_o7);
                        sparc_sub (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
                        break;
-               case CEE_REM_UN:
                case OP_IREM_UN:
                        sparc_wry (code, sparc_g0, sparc_g0);
                        sparc_udiv (code, FALSE, ins->sreg1, ins->sreg2, sparc_o7);
@@ -3275,7 +2761,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        }
                        sparc_sub (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
                        break;
-               case CEE_OR:
                case OP_IOR:
                        sparc_or (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
                        break;
@@ -3283,7 +2768,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_IOR_IMM:
                        EMIT_ALU_IMM (ins, or, FALSE);
                        break;
-               case CEE_XOR:
                case OP_IXOR:
                        sparc_xor (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
                        break;
@@ -3291,7 +2775,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_IXOR_IMM:
                        EMIT_ALU_IMM (ins, xor, FALSE);
                        break;
-               case CEE_SHL:
                case OP_ISHL:
                        sparc_sll (code, ins->sreg1, ins->sreg2, ins->dreg);
                        break;
@@ -3304,7 +2787,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                                sparc_sll (code, ins->sreg1, sparc_o7, ins->dreg);
                        }
                        break;
-               case CEE_SHR:
                case OP_ISHR:
                        sparc_sra (code, ins->sreg1, ins->sreg2, ins->dreg);
                        break;
@@ -3326,7 +2808,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                                sparc_srl (code, ins->sreg1, sparc_o7, ins->dreg);
                        }
                        break;
-               case CEE_SHR_UN:
                case OP_ISHR_UN:
                        sparc_srl (code, ins->sreg1, ins->sreg2, ins->dreg);
                        break;
@@ -3363,17 +2844,14 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                                sparc_srlx (code, ins->sreg1, sparc_o7, ins->dreg);
                        }
                        break;
-               case CEE_NOT:
                case OP_INOT:
                        /* can't use sparc_not */
                        sparc_xnor (code, FALSE, ins->sreg1, sparc_g0, ins->dreg);
                        break;
-               case CEE_NEG:
                case OP_INEG:
                        /* can't use sparc_neg */
                        sparc_sub (code, FALSE, sparc_g0, ins->sreg1, ins->dreg);
                        break;
-               case CEE_MUL:
                case OP_IMUL:
                        sparc_smul (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
                        break;
@@ -3396,7 +2874,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                                EMIT_ALU_IMM (ins, smul, FALSE);
                        break;
                }
-               case CEE_MUL_OVF:
                case OP_IMUL_OVF:
                        sparc_smul (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
                        sparc_rdy (code, sparc_g1);
@@ -3404,7 +2881,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        sparc_cmp (code, sparc_g1, sparc_o7);
                        EMIT_COND_SYSTEM_EXCEPTION_GENERAL (ins, sparc_bne, "OverflowException", TRUE, sparc_icc_short);
                        break;
-               case CEE_MUL_OVF_UN:
                case OP_IMUL_OVF_UN:
                        sparc_umul (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
                        sparc_rdy (code, sparc_o7);
@@ -3412,7 +2888,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:
@@ -3422,23 +2897,26 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
                        sparc_set_template (code, ins->dreg);
                        break;
-               case CEE_CONV_I4:
-               case CEE_CONV_U4:
+               case OP_JUMP_TABLE:
+                       mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
+                       sparc_set_template (code, ins->dreg);
+                       break;
+               case OP_ICONV_TO_I4:
+               case OP_ICONV_TO_U4:
                case OP_MOVE:
-               case OP_SETREG:
                        if (ins->sreg1 != ins->dreg)
                                sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
                        break;
-               case OP_SETFREG:
-                       /* Only used on V9 */
+               case OP_FMOVE:
+#ifdef SPARCV9
                        if (ins->sreg1 != ins->dreg)
                                sparc_fmovd (code, ins->sreg1, ins->dreg);
+#else
+                       sparc_fmovs (code, ins->sreg1, ins->dreg);
+                       sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
+#endif
                        break;
-               case OP_SPARC_SETFREG_FLOAT:
-                       /* Only used on V9 */
-                       sparc_fdtos (code, ins->sreg1, ins->dreg);
-                       break;
-               case CEE_JMP:
+               case OP_JMP:
                        if (cfg->method->save_lmf)
                                NOT_IMPLEMENTED;
 
@@ -3451,7 +2929,8 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        break;
                case OP_CHECK_THIS:
                        /* ensure ins->sreg1 is not NULL */
-                       sparc_ld_imm (code, ins->sreg1, 0, sparc_g0);
+                       /* Might be misaligned in case of vtypes so use a byte load */
+                       sparc_ldsb_imm (code, ins->sreg1, 0, sparc_g0);
                        break;
                case OP_ARGLIST:
                        sparc_add_imm (code, FALSE, sparc_fp, cfg->sig_cookie, sparc_o7);
@@ -3460,8 +2939,9 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_FCALL:
                case OP_LCALL:
                case OP_VCALL:
+               case OP_VCALL2:
                case OP_VOIDCALL:
-               case CEE_CALL:
+               case OP_CALL:
                        call = (MonoCallInst*)ins;
                        g_assert (!call->virtual);
                        code = emit_save_sp_to_lmf (cfg, code);
@@ -3470,12 +2950,13 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        else
                            code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr);
 
-                       code = emit_vret_token (ins, code);
+                       code = emit_vret_token (cfg->generic_sharing_context, ins, code);
                        code = emit_move_return_value (ins, code);
                        break;
                case OP_FCALL_REG:
                case OP_LCALL_REG:
                case OP_VCALL_REG:
+               case OP_VCALL2_REG:
                case OP_VOIDCALL_REG:
                case OP_CALL_REG:
                        call = (MonoCallInst*)ins;
@@ -3491,29 +2972,34 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        else
                                sparc_nop (code);
 
-                       code = emit_vret_token (ins, code);
+                       code = emit_vret_token (cfg->generic_sharing_context, ins, code);
                        code = emit_move_return_value (ins, code);
                        break;
                case OP_FCALL_MEMBASE:
                case OP_LCALL_MEMBASE:
                case OP_VCALL_MEMBASE:
+               case OP_VCALL2_MEMBASE:
                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);
                        else
                                sparc_nop (code);
 
-                       code = emit_vret_token (ins, code);
+                       code = emit_vret_token (cfg->generic_sharing_context, ins, code);
                        code = emit_move_return_value (ins, code);
                        break;
                case OP_SETFRET:
-                       if (cfg->method->signature->ret->type == MONO_TYPE_R4)
+                       if (mono_method_signature (cfg->method)->ret->type == MONO_TYPE_R4)
                                sparc_fdtos (code, ins->sreg1, sparc_f0);
                        else {
 #ifdef SPARCV9
@@ -3525,15 +3011,19 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 #endif
                        }
                        break;
-               case OP_OUTARG:
-                       g_assert_not_reached ();
-                       break;
                case OP_LOCALLOC: {
                        guint32 size_reg;
+                       gint32 offset2;
+
+#ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
+                       /* Perform stack touching */
+                       NOT_IMPLEMENTED;
+#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);
+                       /* Add 4 to compensate for the rounding of localloc_offset */
+                       sparc_add_imm (code, FALSE, ins->sreg1, 4 + 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)) {
@@ -3550,8 +3040,10 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        sparc_sub (code, FALSE, sparc_sp, ins->dreg, ins->dreg);
                        /* Keep %sp valid at all times */
                        sparc_mov_reg_reg (code, ins->dreg, sparc_sp);
-                       g_assert (sparc_is_imm13 (MONO_SPARC_STACK_BIAS + cfg->arch.localloc_offset));
-                       sparc_add_imm (code, FALSE, ins->dreg, MONO_SPARC_STACK_BIAS + cfg->arch.localloc_offset, ins->dreg);
+                       /* Round localloc_offset too so the result is at least 8 aligned */
+                       offset2 = ALIGN_TO (cfg->arch.localloc_offset, 8);
+                       g_assert (sparc_is_imm13 (MONO_SPARC_STACK_BIAS + offset2));
+                       sparc_add_imm (code, FALSE, ins->dreg, MONO_SPARC_STACK_BIAS + offset2, ins->dreg);
 
                        if (ins->flags & MONO_INST_INIT) {
                                guint32 *br [3];
@@ -3578,8 +3070,17 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        }
                        break;
                }
-               case OP_SPARC_LOCALLOC_IMM: {
-                       gint32 offset = ins->inst_c0;
+               case OP_LOCALLOC_IMM: {
+                       gint32 offset = ins->inst_imm;
+                       gint32 offset2;
+                       
+#ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
+                       /* Perform stack touching */
+                       NOT_IMPLEMENTED;
+#endif
+
+                       /* To compensate for the rounding of localloc_offset */
+                       offset += sizeof (gpointer);
                        offset = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
                        if (sparc_is_imm13 (offset))
                                sparc_sub_imm (code, FALSE, sparc_sp, offset, sparc_sp);
@@ -3587,8 +3088,10 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                                sparc_set (code, offset, sparc_o7);
                                sparc_sub (code, FALSE, sparc_sp, sparc_o7, sparc_sp);
                        }
-                       g_assert (sparc_is_imm13 (MONO_SPARC_STACK_BIAS + cfg->arch.localloc_offset));
-                       sparc_add_imm (code, FALSE, sparc_sp, MONO_SPARC_STACK_BIAS + cfg->arch.localloc_offset, ins->dreg);
+                       /* Round localloc_offset too so the result is at least 8 aligned */
+                       offset2 = ALIGN_TO (cfg->arch.localloc_offset, 8);
+                       g_assert (sparc_is_imm13 (MONO_SPARC_STACK_BIAS + offset2));
+                       sparc_add_imm (code, FALSE, sparc_sp, MONO_SPARC_STACK_BIAS + offset2, ins->dreg);
                        if ((ins->flags & MONO_INST_INIT) && (offset > 0)) {
                                guint32 *br [2];
                                int i;
@@ -3625,11 +3128,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        }
                        break;
                }
-               case CEE_RET:
-                       /* The return is done in the epilog */
-                       g_assert_not_reached ();
-                       break;
-               case CEE_THROW:
+               case OP_THROW:
                        sparc_mov_reg_reg (code, ins->sreg1, sparc_o0);
                        mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
                                             (gpointer)"mono_arch_throw_exception");
@@ -3672,7 +3171,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        sparc_mov_reg_reg (code, ins->sreg1, sparc_o0);
                        break;
                }
-               case CEE_ENDFINALLY: {
+               case OP_ENDFINALLY: {
                        MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
                        if (!sparc_is_imm13 (spvar->inst_offset)) {
                                sparc_set (code, spvar->inst_offset, GP_SCRATCH_REG);
@@ -3693,7 +3192,14 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_LABEL:
                        ins->inst_c0 = (guint8*)code - cfg->native_code;
                        break;
-               case CEE_BR:
+               case OP_RELAXED_NOP:
+               case OP_NOP:
+               case OP_DUMMY_USE:
+               case OP_DUMMY_STORE:
+               case OP_NOT_REACHED:
+               case OP_NOT_NULL:
+                       break;
+               case OP_BR:
                        //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
                        if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
                                break;
@@ -3772,7 +3278,25 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_COND_EXC_NO:
                case OP_COND_EXC_C:
                case OP_COND_EXC_NC:
+               case OP_COND_EXC_IEQ:
+               case OP_COND_EXC_INE_UN:
+               case OP_COND_EXC_ILT:
+               case OP_COND_EXC_ILT_UN:
+               case OP_COND_EXC_IGT:
+               case OP_COND_EXC_IGT_UN:
+               case OP_COND_EXC_IGE:
+               case OP_COND_EXC_IGE_UN:
+               case OP_COND_EXC_ILE:
+               case OP_COND_EXC_ILE_UN:
+               case OP_COND_EXC_IOV:
+               case OP_COND_EXC_INO:
+               case OP_COND_EXC_IC:
+               case OP_COND_EXC_INC:
+#ifdef SPARCV9
+                       NOT_IMPLEMENTED;
+#else
                        EMIT_COND_SYSTEM_EXCEPTION (ins, opcode_to_sparc_cond (ins->opcode), ins->inst_p1);
+#endif
                        break;
                case OP_SPARC_COND_EXC_EQZ:
                        EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brz, ins->inst_p1);
@@ -3792,26 +3316,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_SPARC_COND_EXC_NEZ:
                        EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brnz, ins->inst_p1);
                        break;
-               case OP_COND_EXC_IOV:
-               case OP_COND_EXC_IC:
-                       EMIT_COND_SYSTEM_EXCEPTION_GENERAL (ins, opcode_to_sparc_cond (ins->opcode), ins->inst_p1, TRUE, sparc_icc_short);
-                       break;
-               case CEE_BEQ:
-               case CEE_BNE_UN:
-               case CEE_BLT:
-               case CEE_BLT_UN:
-               case CEE_BGT:
-               case CEE_BGT_UN:
-               case CEE_BGE:
-               case CEE_BGE_UN:
-               case CEE_BLE:
-               case CEE_BLE_UN: {
-                       if (sparcv9)
-                               EMIT_COND_BRANCH_PREDICTED (ins, opcode_to_sparc_cond (ins->opcode), 1, 1);
-                       else
-                               EMIT_COND_BRANCH (ins, opcode_to_sparc_cond (ins->opcode), 1, 1);
-                       break;
-               }
 
                case OP_IBEQ:
                case OP_IBNE_UN:
@@ -3823,8 +3327,10 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_IBGE_UN:
                case OP_IBLE:
                case OP_IBLE_UN: {
-                       /* Only used on V9 */
-                       EMIT_COND_BRANCH_ICC (ins, opcode_to_sparc_cond (ins->opcode), 1, 1, sparc_icc_short);
+                       if (sparcv9)
+                               EMIT_COND_BRANCH_PREDICTED (ins, opcode_to_sparc_cond (ins->opcode), 1, 1);
+                       else
+                               EMIT_COND_BRANCH (ins, opcode_to_sparc_cond (ins->opcode), 1, 1);
                        break;
                }
 
@@ -3913,41 +3419,53 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        sparc_fstod (code, FP_SCRATCH_REG, ins->dreg);
                        break;
                }
-               case OP_FMOVE:
-#ifdef SPARCV9
-                       sparc_fmovd (code, ins->sreg1, ins->dreg);
-#else
-                       sparc_fmovs (code, ins->sreg1, ins->dreg);
-                       sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
-#endif
-                       break;
-               case CEE_CONV_R4: {
-                       gint32 offset = mono_spillvar_offset_float (cfg, 0);
-                       if (!sparc_is_imm13 (offset))
-                               NOT_IMPLEMENTED;
+               case OP_ICONV_TO_R4: {
+                       gint32 offset = cfg->arch.float_spill_slot_offset;
 #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);
                        break;
                }
-               case CEE_CONV_R8: {
-                       gint32 offset = mono_spillvar_offset_float (cfg, 0);
-                       if (!sparc_is_imm13 (offset))
-                               NOT_IMPLEMENTED;
+               case OP_ICONV_TO_R8: {
+                       gint32 offset = cfg->arch.float_spill_slot_offset;
 #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;
@@ -3962,12 +3480,16 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 #endif
                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;
+                       gint32 offset = cfg->arch.float_spill_slot_offset;
                        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:
@@ -3989,16 +3511,22 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        /* Emulated */
                        g_assert_not_reached ();
                        break;
-               case CEE_CONV_R_UN:
-                       /* Emulated */
-                       g_assert_not_reached ();
+               case OP_FCONV_TO_R4:
+                       /* FIXME: Change precision ? */
+#ifdef SPARCV9
+                       sparc_fmovd (code, ins->sreg1, ins->dreg);
+#else
+                       sparc_fmovs (code, ins->sreg1, ins->dreg);
+                       sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
+#endif
                        break;
                case OP_LCONV_TO_R_UN: { 
                        /* Emulated */
                        g_assert_not_reached ();
                        break;
                }
-               case OP_LCONV_TO_OVF_I: {
+               case OP_LCONV_TO_OVF_I:
+               case OP_LCONV_TO_OVF_I4_2: {
                        guint32 *br [3], *label [1];
 
                        /* 
@@ -4132,12 +3660,16 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        EMIT_FLOAT_COND_BRANCH (ins, sparc_fble, 1, 1);
                        EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
                        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);
+               case OP_CKFINITE: {
+                       gint32 offset = cfg->arch.float_spill_slot_offset;
+                       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);
@@ -4150,6 +3682,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__);
@@ -4168,8 +3705,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                cpos += max_len;
 
                last_ins = ins;
-               
-               ins = ins->next;
        }
 
        cfg->code_len = (guint8*)code - cfg->native_code;
@@ -4178,7 +3713,6 @@ 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_get_lmf_addr, "mono_arch_get_lmf_addr", NULL, TRUE);
 }
 
@@ -4195,6 +3729,8 @@ mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, Mono
                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_CLASS_INIT: {
                        guint32 *ip2 = (guint32*)ip;
                        /* Might already been changed to a nop */
@@ -4219,26 +3755,19 @@ mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, Mono
        }
 }
 
-void
-mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code)
-{
-       *stack = 0;
-       *code = 512;
-}
-
 void*
 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
 {
-       int i, stack, code_size;
+       int i;
        guint32 *code = (guint32*)p;
-       MonoMethodSignature *sig = cfg->method->signature;
+       MonoMethodSignature *sig = mono_method_signature (cfg->method);
        CallInfo *cinfo;
 
        /* Save registers to stack */
        for (i = 0; i < 6; ++i)
                sparc_sti_imm (code, sparc_i0 + i, sparc_fp, ARGS_OFFSET + (i * sizeof (gpointer)));
 
-       cinfo = get_call_info (sig, FALSE);
+       cinfo = get_call_info (cfg, sig, FALSE);
 
        /* Save float regs on V9, since they are caller saved */
        for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
@@ -4282,10 +3811,6 @@ mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean ena
                }
        }
 
-       mono_arch_instrument_mem_needs (cfg->method, &stack, &code_size);
-
-       g_assert ((code - (guint32*)p) <= (code_size * 4));
-
        g_free (cinfo);
 
        return code;
@@ -4306,7 +3831,7 @@ mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean ena
        int save_mode = SAVE_NONE;
        MonoMethod *method = cfg->method;
 
-       switch (mono_type_get_underlying_type (method->signature->ret)->type) {
+       switch (mono_type_get_underlying_type (mono_method_signature (method)->ret)->type) {
        case MONO_TYPE_VOID:
                /* special case string .ctor icall */
                if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
@@ -4395,40 +3920,6 @@ mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean ena
        return code;
 }
 
-int
-mono_arch_max_epilog_size (MonoCompile *cfg)
-{
-       int exc_count = 0, max_epilog_size = 16 + 20*4;
-       MonoJumpInfo *patch_info;
-       
-       if (cfg->method->save_lmf)
-               max_epilog_size += 128;
-       
-       if (mono_jit_trace_calls != NULL)
-               max_epilog_size += 50;
-
-       if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
-               max_epilog_size += 50;
-
-       /* count the number of exception infos */
-     
-       for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
-               if (patch_info->type == MONO_PATCH_INFO_EXC)
-                       exc_count++;
-       }
-
-       /* 
-        * make sure we have enough space for exceptions
-        */
-#ifdef SPARCV9
-       max_epilog_size += exc_count * (20 * 4);
-#else
-       max_epilog_size += exc_count * 24;
-#endif
-
-       return max_epilog_size;
-}
-
 guint8 *
 mono_arch_emit_prolog (MonoCompile *cfg)
 {
@@ -4470,6 +3961,11 @@ mono_arch_emit_prolog (MonoCompile *cfg)
 
        cfg->stack_offset = offset;
 
+#ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
+                       /* Perform stack touching */
+                       NOT_IMPLEMENTED;
+#endif
+
        if (!sparc_is_imm13 (- cfg->stack_offset)) {
                /* Can't use sparc_o7 here, since we're still in the caller's frame */
                sparc_set (code, (- cfg->stack_offset), GP_SCRATCH_REG);
@@ -4486,16 +3982,16 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        }
 */
 
-       sig = method->signature;
+       sig = mono_method_signature (method);
 
-       cinfo = get_call_info (sig, FALSE);
+       cinfo = get_call_info (cfg, sig, FALSE);
 
        /* Keep in sync with emit_load_volatile_arguments */
        for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
                ArgInfo *ainfo = cinfo->args + i;
                gint32 stack_offset;
                MonoType *arg_type;
-               inst = cfg->varinfo [i];
+               inst = cfg->args [i];
 
                if (sig->hasthis && (i == 0))
                        arg_type = &mono_defaults.object_class->byval_arg;
@@ -4644,10 +4140,25 @@ mono_arch_emit_prolog (MonoCompile *cfg)
 void
 mono_arch_emit_epilog (MonoCompile *cfg)
 {
-       MonoJumpInfo *patch_info;
        MonoMethod *method = cfg->method;
        guint32 *code;
        int can_fold = 0;
+       int max_epilog_size = 16 + 20 * 4;
+       
+       if (cfg->method->save_lmf)
+               max_epilog_size += 128;
+       
+       if (mono_jit_trace_calls != NULL)
+               max_epilog_size += 50;
+
+       if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
+               max_epilog_size += 50;
+
+       while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
+               cfg->code_size *= 2;
+               cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
+               mono_jit_stats.code_reallocs++;
+       }
 
        code = (guint32*)(cfg->native_code + cfg->code_len);
 
@@ -4664,7 +4175,7 @@ mono_arch_emit_epilog (MonoCompile *cfg)
         * The V8 ABI requires that calls to functions which return a structure
         * return to %i7+12
         */
-       if (!v64 && cfg->method->signature->pinvoke && MONO_TYPE_ISSTRUCT(cfg->method->signature->ret))
+       if (!v64 && mono_method_signature (cfg->method)->pinvoke && MONO_TYPE_ISSTRUCT(mono_method_signature (cfg->method)->ret))
                sparc_jmpl_imm (code, sparc_i7, 12, sparc_g0);
        else
                sparc_ret (code);
@@ -4674,11 +4185,17 @@ mono_arch_emit_epilog (MonoCompile *cfg)
        if (cfg->bb_exit->in_count == 1 && cfg->bb_exit->in_bb[0]->native_offset != cfg->bb_exit->native_offset)
                can_fold = 1;
 
+       /* 
+        * FIXME: The last instruction might have a branch pointing into it like in 
+        * int_ceq sparc_i0 <-
+        */
+       can_fold = 0;
+
        /* Try folding last instruction into the restore */
        if (can_fold && (sparc_inst_op (code [-2]) == 0x2) && (sparc_inst_op3 (code [-2]) == 0x2) && sparc_inst_imm (code [-2]) && (sparc_inst_rd (code [-2]) == sparc_i0)) {
                /* or reg, imm, %i0 */
                int reg = sparc_inst_rs1 (code [-2]);
-               int imm = sparc_inst_imm13 (code [-2]);
+               int imm = (((gint32)(sparc_inst_imm13 (code [-2]))) << 19) >> 19;
                code [-2] = code [-1];
                code --;
                sparc_restore_imm (code, reg, imm, sparc_o0);
@@ -4695,22 +4212,133 @@ mono_arch_emit_epilog (MonoCompile *cfg)
        else
                sparc_restore_imm (code, sparc_g0, 0, sparc_g0);
 
-       /* add code to raise exceptions */
+       cfg->code_len = (guint8*)code - cfg->native_code;
+
+       g_assert (cfg->code_len < cfg->code_size);
+
+}
+
+void
+mono_arch_emit_exceptions (MonoCompile *cfg)
+{
+       MonoJumpInfo *patch_info;
+       guint32 *code;
+       int nthrows = 0, i;
+       int exc_count = 0;
+       guint32 code_size;
+       MonoClass *exc_classes [16];
+       guint8 *exc_throw_start [16], *exc_throw_end [16];
+
+       /* Compute needed space */
        for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
-               glong offset = patch_info->ip.i;
+               if (patch_info->type == MONO_PATCH_INFO_EXC)
+                       exc_count++;
+       }
+     
+       /* 
+        * make sure we have enough space for exceptions
+        */
+#ifdef SPARCV9
+       code_size = exc_count * (20 * 4);
+#else
+       code_size = exc_count * 24;
+#endif
+
+       while (cfg->code_len + code_size > (cfg->code_size - 16)) {
+               cfg->code_size *= 2;
+               cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
+               mono_jit_stats.code_reallocs++;
+       }
+
+       code = (guint32*)(cfg->native_code + cfg->code_len);
 
+       for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
                switch (patch_info->type) {
-               case MONO_PATCH_INFO_EXC:
+               case MONO_PATCH_INFO_EXC: {
+                       MonoClass *exc_class;
+                       guint32 *buf, *buf2;
+                       guint32 throw_ip, type_idx;
+                       gint32 disp;
+
                        sparc_patch ((guint32*)(cfg->native_code + patch_info->ip.i), code);
-                       mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_EXC_NAME, patch_info->data.target);
-                       sparc_set_template (code, sparc_o0);
-                       mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_METHOD_REL, (gpointer)offset);
-                       sparc_set_template (code, sparc_o1);
-                       patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
-                       patch_info->data.name = "mono_arch_throw_exception_by_name";
-                       patch_info->ip.i = (guint8*)code - cfg->native_code;
-                       EMIT_CALL ();
+
+                       exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
+                       g_assert (exc_class);
+                       type_idx = exc_class->type_token - MONO_TOKEN_TYPE_DEF;
+                       throw_ip = patch_info->ip.i;
+
+                       /* Find a throw sequence for the same exception class */
+                       for (i = 0; i < nthrows; ++i)
+                               if (exc_classes [i] == exc_class)
+                                       break;
+
+                       if (i < nthrows) {
+                               guint32 throw_offset = (((guint8*)exc_throw_end [i] - cfg->native_code) - throw_ip) >> 2;
+                               if (!sparc_is_imm13 (throw_offset))
+                                       sparc_set32 (code, throw_offset, sparc_o1);
+
+                               disp = (exc_throw_start [i] - (guint8*)code) >> 2;
+                               g_assert (sparc_is_imm22 (disp));
+                               sparc_branch (code, 0, sparc_ba, disp);
+                               if (sparc_is_imm13 (throw_offset))
+                                       sparc_set32 (code, throw_offset, sparc_o1);
+                               else
+                                       sparc_nop (code);
+                               patch_info->type = MONO_PATCH_INFO_NONE;
+                       }
+                       else {
+                               /* Emit the template for setting o1 */
+                               buf = code;
+                               if (sparc_is_imm13 (((((guint8*)code - cfg->native_code) - throw_ip) >> 2) - 8))
+                                       /* Can use a short form */
+                                       sparc_nop (code);
+                               else
+                                       sparc_set_template (code, sparc_o1);
+                               buf2 = code;
+
+                               if (nthrows < 16) {
+                                       exc_classes [nthrows] = exc_class;
+                                       exc_throw_start [nthrows] = (guint8*)code;
+                               }
+
+                               /*
+                               mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_ABS, mono_sparc_break);
+                               EMIT_CALL();
+                               */
+
+                               /* first arg = type token */
+                               /* Pass the type index to reduce the size of the sparc_set */
+                               if (!sparc_is_imm13 (type_idx))
+                                       sparc_set32 (code, type_idx, sparc_o0);
+
+                               /* second arg = offset between the throw ip and the current ip */
+                               /* On sparc, the saved ip points to the call instruction */
+                               disp = (((guint8*)code - cfg->native_code) - throw_ip) >> 2;
+                               sparc_set32 (buf, disp, sparc_o1);
+                               while (buf < buf2)
+                                       sparc_nop (buf);
+
+                               if (nthrows < 16) {
+                                       exc_throw_end [nthrows] = (guint8*)code;
+                                       nthrows ++;
+                               }
+
+                               patch_info->data.name = "mono_arch_throw_corlib_exception";
+                               patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
+                               patch_info->ip.i = (guint8*)code - cfg->native_code;
+
+                               EMIT_CALL ();
+
+                               if (sparc_is_imm13 (type_idx)) {
+                                       /* Put it into the delay slot */
+                                       code --;
+                                       buf = code;
+                                       sparc_set32 (code, type_idx, sparc_o0);
+                                       g_assert (code - buf == 1);
+                               }
+                       }
                        break;
+               }
                default:
                        /* do nothing */
                        break;
@@ -4745,34 +4373,20 @@ mono_arch_get_lmf_addr (void)
 #endif
 }
 
-void
-mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
-{
 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
-#ifdef __linux__
-       struct sigaltstack sa;
-#else
-       stack_t         sigstk;
-#endif
-       printf ("SIGALT!\n");
-       /* Setup an alternate signal stack */
-       tls->signal_stack = g_malloc (SIGNAL_STACK_SIZE);
-       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
+
+/*
+ * There seems to be no way to determine stack boundaries under solaris,
+ * so it's not possible to determine whenever a SIGSEGV is caused by stack
+ * overflow or not.
+ */
+#error "--with-sigaltstack=yes not supported on solaris"
+
 #endif
 
+void
+mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
+{
        if (!lmf_addr_key_inited) {
                int res;
 
@@ -4799,41 +4413,12 @@ 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)
-{
-       int this_out_reg = sparc_o0;
-
-       if (vt_reg != -1) {
-#ifdef SPARCV9
-               MonoInst *ins;
-               MONO_INST_NEW (cfg, ins, OP_SETREG);
-               ins->sreg1 = vt_reg;
-               ins->dreg = sparc_o0;
-               mono_bblock_add_inst (cfg->cbb, ins);
-               this_out_reg = sparc_o1;
-#else
-               /* Set the 'struct/union return pointer' location on the stack */
-               MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, sparc_sp, 64, vt_reg);
-#endif
-       }
-
-       /* add the this argument */
-       if (this_reg != -1) {
-               MonoInst *this;
-               MONO_INST_NEW (cfg, this, OP_SETREG);
-               this->type = this_type;
-               this->sreg1 = this_reg;
-               this->dreg = this_out_reg;
-               mono_bblock_add_inst (cfg->cbb, this);
-       }
-}
-
-
 MonoInst*
-mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
+mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
 {
-       return NULL;
+       MonoInst *ins = NULL;
+
+       return ins;
 }
 
 /*
@@ -4854,7 +4439,7 @@ mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJit
        CallInfo *cinfo;
        ArgInfo *ainfo;
 
-       cinfo = get_call_info (csig, FALSE);
+       cinfo = get_call_info (NULL, csig, FALSE);
 
        if (csig->hasthis) {
                ainfo = &cinfo->args [0];
@@ -4888,3 +4473,10 @@ MonoInst* mono_arch_get_thread_intrinsic (MonoCompile* cfg)
 {
        return NULL;
 }
+
+gpointer
+mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
+{
+       /* FIXME: implement */
+       g_assert_not_reached ();
+}