2009-05-09 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / mini-s390x.c
index e5c30c5b60794229de0f0c2a606d5e06b69489c2..a74b99038961766f2bbb26c0f80169af3400a93c 100644 (file)
@@ -156,6 +156,108 @@ if (ins->flags & MONO_INST_BRLABEL) {                                                     \
                s390_ldr (code, ins->dreg, ins->sreg1);                 \
        }
 
+#define MONO_EMIT_NEW_MOVE(cfg,dest,offset,src,imm,size) do {                  \
+                MonoInst *inst;                                                \
+               int tmpr = 0;                                                   \
+               int sReg, dReg;                                                 \
+               MONO_INST_NEW (cfg, inst, OP_NOP);                                                              \
+               if (size > 256) {                                               \
+                       tmpr = mono_alloc_preg (cfg); \
+                       MONO_EMIT_NEW_ICONST(cfg,tmpr,size);                    \
+                       inst->dreg        = dest;                               \
+                       inst->inst_offset = offset;                             \
+                       inst->sreg1       = src;                                \
+                       inst->inst_imm    = imm;                                \
+                       inst->sreg2       = tmpr;                               \
+               } else {                                                        \
+                       if (s390_is_uimm12(offset)) {                           \
+                               inst->dreg        = dest;                       \
+                               inst->inst_offset = offset;                     \
+                       } else {                                                \
+                               dReg = mono_alloc_preg (cfg); \
+                               MONO_EMIT_NEW_BIALU_IMM(cfg, OP_ADD_IMM,        \
+                                       dReg, dest, offset);                    \
+                               inst->dreg        = dReg;                       \
+                               inst->inst_offset = 0;                          \
+                       }                                                       \
+                       if (s390_is_uimm12(imm)) {                              \
+                               inst->sreg1       = src;                        \
+                               inst->inst_imm    = imm;                        \
+                       } else {                                                \
+                               sReg = mono_alloc_preg (cfg); \
+                               MONO_EMIT_NEW_BIALU_IMM(cfg, OP_ADD_IMM,        \
+                                       sReg, src, imm);                        \
+                               inst->sreg1       = sReg;                       \
+                               inst->inst_imm    = 0;                          \
+                       }                                                       \
+               }                                                               \
+                inst->opcode     = OP_S390_MOVE;                               \
+               inst->backend.size        = size;                                       \
+        MONO_ADD_INS (cfg->cbb, inst); \
+       } while (0)
+
+#define MONO_OUTPUT_VTR(cfg, size, dr, sr, so) do {                            \
+       int reg = mono_alloc_preg (cfg); \
+       switch (size) {                                                         \
+               case 0:                                                         \
+                       MONO_EMIT_NEW_ICONST(cfg, reg, 0);                      \
+               break;                                                          \
+               case 1:                                                         \
+                       MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg, OP_LOADU1_MEMBASE,   \
+                               reg, sr, so);                                   \
+               break;                                                          \
+               case 2:                                                         \
+                       MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg, OP_LOADU2_MEMBASE,   \
+                               reg, sr, so);                                   \
+               break;                                                          \
+               case 4:                                                         \
+                       MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg, OP_LOADI4_MEMBASE,   \
+                               reg, sr, so);                                   \
+               break;                                                          \
+               case 8:                                                         \
+                       MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg, OP_LOADI8_MEMBASE,   \
+                               reg, sr, so);                                   \
+               break;                                                          \
+       }                                                                       \
+       mono_call_inst_add_outarg_reg(cfg, call, reg, dr, FALSE);       \
+} while (0)
+
+#define MONO_OUTPUT_VTS(cfg, size, dr, dx, sr, so) do {                                \
+       int tmpr;                                                               \
+       switch (size) {                                                         \
+               case 0:                                                         \
+                       tmpr = mono_alloc_preg (cfg); \
+                       MONO_EMIT_NEW_ICONST(cfg, tmpr, 0);                     \
+                       MONO_EMIT_NEW_STORE_MEMBASE(cfg, OP_STORE_MEMBASE_REG,  \
+                               dr, dx, tmpr);                                  \
+               break;                                                          \
+               case 1:                                                         \
+                       tmpr = mono_alloc_preg (cfg); \
+                       MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg, OP_LOADU1_MEMBASE,   \
+                               tmpr, sr, so);                                  \
+                       MONO_EMIT_NEW_STORE_MEMBASE(cfg, OP_STORE_MEMBASE_REG,  \
+                               dr, dx, tmpr);                                  \
+               break;                                                          \
+               case 2:                                                         \
+                       tmpr = mono_alloc_preg (cfg); \
+                       MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg, OP_LOADU2_MEMBASE,   \
+                               tmpr, sr, so);                                  \
+                       MONO_EMIT_NEW_STORE_MEMBASE(cfg, OP_STORE_MEMBASE_REG,  \
+                               dr, dx, tmpr);                                  \
+               break;                                                          \
+               case 4:                                                         \
+                       tmpr = mono_alloc_preg (cfg);   \
+                       MONO_EMIT_NEW_LOAD_MEMBASE_OP(cfg, OP_LOADI4_MEMBASE,   \
+                               tmpr, sr, so);                                  \
+                       MONO_EMIT_NEW_STORE_MEMBASE(cfg, OP_STORE_MEMBASE_REG,  \
+                               dr, dx, tmpr);                                  \
+               break;                                                          \
+               case 8:                                                         \
+                       MONO_EMIT_NEW_MOVE (cfg, dr, dx, sr, so, size);         \
+               break;                                                          \
+       }                                                                       \
+} while (0)
+
 #undef DEBUG
 #define DEBUG(a) if (cfg->verbose_level > 1) a
 
@@ -180,9 +282,9 @@ if (ins->flags & MONO_INST_BRLABEL) {                                                       \
 #include <mono/utils/mono-math.h>
 
 #include "mini-s390x.h"
-#include "inssel.h"
 #include "cpu-s390x.h"
 #include "jit-icalls.h"
+#include "ir-emit.h"
 
 /*========================= End of Includes ========================*/
 
@@ -221,20 +323,23 @@ struct InstList {
        MonoInst *data;
 };
 
-enum {
+typedef enum {
        RegTypeGeneral,
        RegTypeBase,
        RegTypeFP,
+       RegTypeFPR4,
        RegTypeStructByVal,
-       RegTypeStructByAddr
-};
+       RegTypeStructByValInFP,
+       RegTypeStructByAddr,
+       RegTypeStructByAddrOnStack
+} ArgStorage;
 
 typedef struct {
        gint32  offset;         /* offset from caller's stack */
        gint32  offparm;        /* offset from callee's stack */
        guint16 vtsize;         /* in param area */
        guint8  reg;
-       guint8  regtype;        /* See RegType* */
+       ArgStorage regtype;
        guint32 size;           /* Size of structure used by RegTypeStructByVal */
        gint32  type;           /* Data type of argument */
 } ArgInfo;
@@ -246,6 +351,7 @@ typedef struct {
        guint32 struct_ret;
        ArgInfo ret;
        ArgInfo sigCookie;
+       size_data sz;
        ArgInfo args [1];
 } CallInfo;
 
@@ -269,12 +375,11 @@ static gboolean is_regsize_var (MonoType *);
 static inline void add_general (guint *, size_data *, ArgInfo *);
 static inline void add_stackParm (guint *, size_data *, ArgInfo *, gint);
 static inline void add_float (guint *, size_data *, ArgInfo *);
-static CallInfo * calculate_sizes (MonoCompile *, MonoMethodSignature *, size_data *, gboolean);
+static CallInfo * get_call_info (MonoCompile *, MonoMemPool *, MonoMethodSignature *, gboolean);
 static guchar * emit_float_to_int (MonoCompile *, guchar *, int, int, int, gboolean);
 gpointer mono_arch_get_lmf_addr (void);
-static guint8 * emit_load_volatile_registers (guint8 *, MonoCompile *);
+static guint8 * emit_load_volatile_arguments (guint8 *, MonoCompile *);
 static void catch_SIGILL(int, siginfo_t *, void *);
-static void emit_sig_cookie (MonoCompile *, MonoCallInst *, CallInfo *, int);
 
 /*========================= End of Prototypes ======================*/
 
@@ -291,7 +396,6 @@ int has_ld = 0;
 static gboolean tls_offset_inited = FALSE;
 
 static int appdomain_tls_offset = -1,
-                  lmf_tls_offset = -1,
            thread_tls_offset = -1;
 
 pthread_key_t lmf_addr_key;
@@ -614,7 +718,7 @@ enum_parmtype:
                                MonoMarshalType *info;
 
                                if (type->data.klass->enumtype) {
-                                       simpleType = type->data.klass->enum_basetype->type;
+                                       simpleType = mono_class_enum_basetype (type->data.klass)->type;
                                        printf("{VALUETYPE} - ");
                                        goto enum_parmtype;
                                }
@@ -677,7 +781,6 @@ enter_method (MonoMethod *method, RegParm *rParm, char *sp)
        guint64 ip;
        CallInfo *cinfo;
        ArgInfo *ainfo;
-       size_data sz;
        void *curParm;
 
        fname = mono_method_full_name (method, TRUE);
@@ -693,7 +796,7 @@ enter_method (MonoMethod *method, RegParm *rParm, char *sp)
        
        sig = mono_method_signature (method);
        
-       cinfo = calculate_sizes (NULL, sig, &sz, sig->pinvoke);
+       cinfo = get_call_info (NULL, NULL, sig, sig->pinvoke);
 
        if (cinfo->struct_ret) {
                printf ("[STRUCTRET:%p], ", (gpointer) rParm->gr[0]);
@@ -919,7 +1022,7 @@ handle_enum:
        case MONO_TYPE_VALUETYPE: {
                MonoMarshalType *info;
                if (type->data.klass->enumtype) {
-                       type = type->data.klass->enum_basetype;
+                       type = mono_class_enum_basetype (type->data.klass);
                        goto handle_enum;
                } else {
                        guint8 *p = va_arg (ap, gpointer);
@@ -1123,7 +1226,7 @@ is_regsize_var (MonoType *t) {
                return FALSE;
        case MONO_TYPE_VALUETYPE:
                if (t->data.klass->enumtype)
-                       return is_regsize_var (t->data.klass->enum_basetype);
+                       return is_regsize_var (mono_class_enum_basetype (t->data.klass));
                return FALSE;
        }
        return FALSE;
@@ -1189,8 +1292,11 @@ mono_arch_get_global_int_regs (MonoCompile *cfg)
        if ((cfg->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
                cfg->frame_reg = s390_r11;
 
+
+       /* FIXME: s390_r12 is reserved for bkchain_reg. Only reserve it if needed */
+       top = 12;
        for (i = 8; i < top; ++i) {
-               if (cfg->frame_reg != i) 
+               if (cfg->frame_reg != i)
                        regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
        }
 
@@ -1258,18 +1364,21 @@ add_stackParm (guint *gr, size_data *sz, ArgInfo *ainfo, gint size)
 {
        if (*gr > S390_LAST_ARG_REG) {
                sz->stack_size  = S390_ALIGN(sz->stack_size, sizeof(long));
-               ainfo->reg      = STK_BASE;
-               sz->stack_size += sizeof(gpointer);
+               ainfo->reg          = STK_BASE;
+               ainfo->offset   = sz->stack_size;
+               ainfo->regtype  = RegTypeStructByAddrOnStack; 
+               sz->stack_size += sizeof (gpointer);
+               sz->parm_size  += sizeof(gpointer);
                sz->offStruct  += sizeof(gpointer);
        } else {
                ainfo->reg      = *gr;
+               ainfo->offset   = sz->stack_size;
+               ainfo->regtype  = RegTypeStructByAddr; 
        }
        (*gr) ++;
-       ainfo->offset   = sz->stack_size;
        ainfo->offparm  = sz->offset;
        sz->offset      = S390_ALIGN(sz->offset+size, sizeof(long));
        ainfo->size     = size;
-       ainfo->regtype  = RegTypeStructByAddr; 
        ainfo->vtsize   = size;
        sz->parm_size  += size;
 }
@@ -1309,7 +1418,7 @@ add_float (guint *fr,  size_data *sz, ArgInfo *ainfo)
 
 /*------------------------------------------------------------------*/
 /*                                                                  */
-/* Name                - calculate_sizes                                   */
+/* Name                - get_call_info                                   */
 /*                                                                  */
 /* Function    - Determine the amount of space required for code   */
 /*               and stack. In addition determine starting points  */
@@ -1319,20 +1428,26 @@ add_float (guint *fr,  size_data *sz, ArgInfo *ainfo)
 /*------------------------------------------------------------------*/
 
 static CallInfo *
-calculate_sizes (MonoCompile *cfg, MonoMethodSignature *sig, size_data *sz, 
-                gboolean string_ctor)
+get_call_info (MonoCompile *cfg, MonoMemPool *mp, MonoMethodSignature *sig, gboolean is_pinvoke)
 {
        guint i, fr, gr, size;
        int nParm = sig->hasthis + sig->param_count;
        MonoType *ret_type;
        guint32 simpletype, align;
-       CallInfo *cinfo = g_malloc0 (sizeof (CallInfo) + sizeof (ArgInfo) * nParm);
+       CallInfo *cinfo;
+       size_data *sz;
        MonoGenericSharingContext *gsctx = cfg ? cfg->generic_sharing_context : NULL;
 
+       if (mp)
+               cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + sizeof (ArgInfo) * nParm);
+       else
+               cinfo = g_malloc0 (sizeof (CallInfo) + sizeof (ArgInfo) * nParm);
+
        fr                = 0;
        gr                = s390_r2;
        nParm             = 0;
        cinfo->struct_ret = 0;
+       sz                = &cinfo->sz;
        sz->retStruct     = 0;
        sz->offset        = 0;
        sz->offStruct     = S390_MINIMAL_STACK_SIZE;
@@ -1392,7 +1507,7 @@ enum_retvalue:
                case MONO_TYPE_VALUETYPE: {
                        MonoClass *klass = mono_class_from_mono_type (sig->ret);
                        if (klass->enumtype) {
-                               simpletype = klass->enum_basetype->type;
+                               simpletype = mono_class_enum_basetype (klass)->type;
                                goto enum_retvalue;
                        }
                        if (sig->pinvoke)
@@ -1530,6 +1645,7 @@ enum_retvalue:
                            (info->fields[0].field->type->type == MONO_TYPE_R4)) {
                                cinfo->args[nParm].size = sizeof(float);
                                add_float(&fr, sz, cinfo->args+nParm);
+                               nParm ++;
                                break;
                        }
 
@@ -1538,6 +1654,7 @@ enum_retvalue:
                            (info->fields[0].field->type->type == MONO_TYPE_R8)) {
                                cinfo->args[nParm].size = sizeof(double);
                                add_float(&fr, sz, cinfo->args+nParm);
+                               nParm ++;
                                break;
                        }
 
@@ -1658,7 +1775,6 @@ mono_arch_allocate_vars (MonoCompile *cfg)
        MonoMethodHeader *header;
        MonoInst *inst;
        CallInfo *cinfo;
-       size_data sz;
        int iParm, iVar, offset, size, align, curinst;
        int frame_reg = STK_BASE;
        int sArg, eArg;
@@ -1682,17 +1798,16 @@ mono_arch_allocate_vars (MonoCompile *cfg)
 
        cfg->frame_reg = frame_reg;
 
+       cfg->arch.bkchain_reg = -1;
+
        if (frame_reg != STK_BASE) 
                cfg->used_int_regs |= 1 << frame_reg;           
 
        sig     = mono_method_signature (cfg->method);
        
-       cinfo   = calculate_sizes (cfg, sig, &sz, sig->pinvoke);
+       cinfo   = get_call_info (cfg, cfg->mempool, sig, sig->pinvoke);
 
-       if (cinfo->struct_ret) {
-               cfg->vret_addr->opcode = OP_REGVAR;
-               cfg->vret_addr->inst_c0 = s390_r2;
-       } else {
+       if (!cinfo->struct_ret) {
                switch (mono_type_get_underlying_type (sig->ret)->type) {
                case MONO_TYPE_VOID:
                        break;
@@ -1719,9 +1834,6 @@ mono_arch_allocate_vars (MonoCompile *cfg)
                inst->opcode       = OP_REGOFFSET;
                inst->inst_basereg = frame_reg;
                offset            += sizeof(gpointer);
-               if ((sig->call_convention == MONO_CALL_VARARG) &&
-                   (!retFitsInReg (cinfo->ret.size)))
-                       cfg->sig_cookie += cinfo->ret.size;
                if (G_UNLIKELY (cfg->verbose_level > 1)) {
                        printf ("vret_addr =");
                        mono_print_ins (cfg->vret_addr);
@@ -1751,51 +1863,79 @@ mono_arch_allocate_vars (MonoCompile *cfg)
                inst = cfg->args [curinst];
                if (inst->opcode != OP_REGVAR) {
                        switch (cinfo->args[iParm].regtype) {
-                               case RegTypeStructByAddr :
-                               if (cinfo->args[iParm].reg == STK_BASE) {
-                                       inst->opcode       = OP_S390_LOADARG;
-                                       inst->inst_basereg = frame_reg;
-                                       offset             = S390_ALIGN(offset, sizeof(long));
-                                       inst->inst_offset  = offset; 
-                                       size               = abs(cinfo->args[iParm].vtsize);
-                                       inst->backend.arg_info     = cinfo->args[iParm].offset;
+                       case RegTypeStructByAddr : {
+                               MonoInst *indir;
+
+                               size = sizeof (gpointer);
+
+                               inst->opcode = OP_REGOFFSET;
+                               inst->inst_basereg = frame_reg;
+                               offset = S390_ALIGN (offset, sizeof (gpointer));
+                               inst->inst_offset = offset;
+
+                               /* Add a level of indirection */
+                               MONO_INST_NEW (cfg, indir, 0);
+                               *indir = *inst;
+                               inst->opcode = OP_VTARG_ADDR;
+                               inst->inst_left = indir;
+                       }
+                               break;
+                       case RegTypeStructByAddrOnStack : {
+                               MonoInst *indir;
+
+                               size = sizeof (gpointer);
+
+                               /* Similar to the == STK_BASE case below */
+                               cfg->arch.bkchain_reg = s390_r12;
+                               cfg->used_int_regs |= 1 << cfg->arch.bkchain_reg;
+
+                               inst->opcode = OP_REGOFFSET;
+                               inst->dreg = mono_alloc_preg (cfg);
+                               inst->inst_basereg = cfg->arch.bkchain_reg;
+                               inst->inst_offset = cinfo->args [iParm].offset;
+
+                               /* Add a level of indirection */
+                               MONO_INST_NEW (cfg, indir, 0);
+                               *indir = *inst;
+                               inst->opcode = OP_VTARG_ADDR;
+                               inst->inst_left = indir;
+                               break;
+                       }
+                       case RegTypeStructByVal :
+                               size               = cinfo->args[iParm].size;
+                               offset             = S390_ALIGN(offset, size);
+                               inst->opcode = OP_REGOFFSET;
+                               inst->inst_basereg = frame_reg;
+                               inst->inst_offset = offset;
+                               break;
+                       default :
+                               if (cinfo->args [iParm].reg == STK_BASE) {
+                                       /*
+                                        * These arguments are in the previous frame, so we can't 
+                                        * compute their offset from the current frame pointer right
+                                        * now, since cfg->stack_offset is not yet known, so dedicate a 
+                                        * register holding the previous frame pointer.
+                                        */
+                                       cfg->arch.bkchain_reg = s390_r12;
+                                       cfg->used_int_regs |= 1 << cfg->arch.bkchain_reg;
+
+                                       inst->opcode       = OP_REGOFFSET;
+                                       inst->inst_basereg = cfg->arch.bkchain_reg;
+                                       size               = (cinfo->args[iParm].size < 8
+                                                                         ? 8 - cinfo->args[iParm].size
+                                                                         : 0);
+                                       inst->inst_offset  = cinfo->args [iParm].offset + size;
+                                       size = sizeof (long);
                                } else {
-                                       inst->opcode       = OP_S390_ARGREG;
-                                       inst->inst_basereg = frame_reg;
-                                       size               = sizeof(gpointer);
-                                       offset             = S390_ALIGN(offset, size);
-                                       inst->inst_offset  = offset;
-                                       inst->backend.arg_info     = cinfo->args[iParm].offset;
-                               }
-                                       break;
-                               case RegTypeStructByVal :
-                                       inst->opcode       = OP_S390_ARGPTR;
-                                       inst->inst_basereg = frame_reg;
-                                       size               = cinfo->args[iParm].size;
-                                       offset             = S390_ALIGN(offset, size);
-                                       inst->inst_offset  = offset;
-                                       inst->backend.arg_info     = cinfo->args[iParm].offset;
-                                       break;
-                               default :
-                               if (cinfo->args[iParm].reg != STK_BASE) {
                                        inst->opcode       = OP_REGOFFSET;
                                        inst->inst_basereg = frame_reg;
                                        size               = (cinfo->args[iParm].size < 8
-                                                             ? sizeof(int)
-                                                             : sizeof(long));
+                                                                         ? sizeof(int)  
+                                                                         : sizeof(long));
                                        offset             = S390_ALIGN(offset, size);
                                        inst->inst_offset  = offset;
-                               } else {
-                                       inst->opcode       = OP_S390_STKARG;
-                                       inst->inst_basereg = frame_reg;
-                                       size               = ((cinfo->args[iParm].size < 8) 
-                                                             ? 8 - cinfo->args[iParm].size
-                                                             : 0);
-                                       inst->inst_offset  = cinfo->args[iParm].offset + 
-                                                            size;
-                                       inst->backend.arg_info       = 0;
-                                       size               = sizeof(long);
-                               } 
+                               }
+                               break;
                        }
                        if ((sig->call_convention == MONO_CALL_VARARG) && 
                            (cinfo->args[iParm].regtype != RegTypeGeneral) &&
@@ -1864,11 +2004,10 @@ mono_arch_create_vars (MonoCompile *cfg)
 {
        MonoMethodSignature *sig;
        CallInfo *cinfo;
-       size_data sz;
 
        sig = mono_method_signature (cfg->method);
 
-       cinfo = calculate_sizes (cfg, sig, &sz, sig->pinvoke);
+       cinfo = get_call_info (cfg, cfg->mempool, sig, sig->pinvoke);
 
        if (cinfo->struct_ret) {
                cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
@@ -1877,163 +2016,320 @@ mono_arch_create_vars (MonoCompile *cfg)
                        mono_print_ins (cfg->vret_addr);
                }
        }
-
-       g_free (cinfo);
 }
 
 /*========================= End of Function ========================*/
 
+static void
+add_outarg_reg2 (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int reg, MonoInst *tree)
+{
+       MonoInst *ins;
+
+       switch (storage) {
+       case RegTypeGeneral:
+               MONO_INST_NEW (cfg, ins, OP_MOVE);
+               ins->dreg = mono_alloc_ireg (cfg);
+               ins->sreg1 = tree->dreg;
+               MONO_ADD_INS (cfg->cbb, ins);
+               mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, FALSE);
+               break;
+       case RegTypeFP:
+               MONO_INST_NEW (cfg, ins, OP_FMOVE);
+               ins->dreg = mono_alloc_freg (cfg);
+               ins->sreg1 = tree->dreg;
+               MONO_ADD_INS (cfg->cbb, ins);
+               mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
+               break;
+       case RegTypeFPR4:
+               MONO_INST_NEW (cfg, ins, OP_S390_SETF4RET);
+               ins->dreg = mono_alloc_freg (cfg);
+               ins->sreg1 = tree->dreg;
+               MONO_ADD_INS (cfg->cbb, ins);
+               mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
+               break;
+       default:
+               g_assert_not_reached ();
+       }
+}
+
+static void
+emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
+{
+       MonoMethodSignature *tmpSig;
+       MonoInst *sig_arg;
+                       
+       cfg->disable_aot = TRUE;
+
+       /*----------------------------------------------------------*/
+       /* mono_ArgIterator_Setup assumes the signature cookie is   */
+       /* passed first and all the arguments which were before it  */
+       /* passed on the stack after the signature. So compensate   */
+       /* by passing a different signature.                        */
+       /*----------------------------------------------------------*/
+       tmpSig = mono_metadata_signature_dup (call->signature);
+       tmpSig->param_count -= call->signature->sentinelpos;
+       tmpSig->sentinelpos  = 0;
+       if (tmpSig->param_count > 0)
+               memcpy (tmpSig->params, 
+                       call->signature->params + call->signature->sentinelpos, 
+                       tmpSig->param_count * sizeof(MonoType *));
+
+       MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
+       sig_arg->dreg = mono_alloc_ireg (cfg);
+       sig_arg->inst_p0 = tmpSig;
+       MONO_ADD_INS (cfg->cbb, sig_arg);
+
+       MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, STK_BASE, 
+                                                                cinfo->sigCookie.offset, sig_arg->dreg);
+}
+
 /*------------------------------------------------------------------*/
 /*                                                                  */
-/* Name                - mono_arch_call_opcode                             */
+/* Name                - mono_arch_emit_call                                   */
 /*                                                                  */
-/* Function    - Take the arguments and generate the arch-specific */
-/*               instructions to properly call the function. This  */
-/*               includes pushing, moving argments to the correct  */
-/*               etc.                                              */
-/*                                                                 */
 /*------------------------------------------------------------------*/
 
-MonoCallInst*
-mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, 
-                      MonoCallInst *call, int is_virtual) {
+void
+mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
+{
        MonoInst *in;
-       MonoCallArgParm *arg;
        MonoMethodSignature *sig;
+       MonoInst *ins;
        int i, n, lParamArea;
        CallInfo *cinfo;
-       ArgInfo *ainfo;
-       size_data sz;
+       ArgInfo *ainfo = NULL;
+       int stackSize;
 
        sig = call->signature;
        n = sig->param_count + sig->hasthis;
        DEBUG (g_print ("Call requires: %d parameters\n",n));
        
-       cinfo = calculate_sizes (cfg, sig, &sz, sig->pinvoke);
+       cinfo = get_call_info (cfg, cfg->mempool, sig, sig->pinvoke);
 
-       call->stack_usage = MAX(sz.stack_size, call->stack_usage);
-       lParamArea        = MAX((call->stack_usage - S390_MINIMAL_STACK_SIZE - sz.parm_size), 0);
-       cfg->param_area   = MAX (((signed) cfg->param_area), lParamArea);
+       stackSize         = cinfo->sz.stack_size + cinfo->sz.local_size + cinfo->sz.parm_size + cinfo->sz.offset;
+       call->stack_usage = MAX(stackSize, call->stack_usage);
+       lParamArea        = MAX((call->stack_usage-S390_MINIMAL_STACK_SIZE-cinfo->sz.parm_size), 0);
+       cfg->param_area   = MAX(((signed) cfg->param_area), lParamArea);
        cfg->flags       |= MONO_CFG_HAS_CALLS;
 
-       if (cinfo->struct_ret)
-               call->used_iregs |= 1 << cinfo->ret.reg;
+       if (cinfo->struct_ret) {
+               MONO_INST_NEW (cfg, ins, OP_MOVE);
+               ins->sreg1 = call->vret_var->dreg;
+               ins->dreg = mono_alloc_preg (cfg);
+               MONO_ADD_INS (cfg->cbb, ins);
+               mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, cinfo->ret.reg, FALSE);
+       }
 
        for (i = 0; i < n; ++i) {
                ainfo = cinfo->args + i;
+               MonoType *t;
+
+               if (i >= sig->hasthis)
+                       t = sig->params [i - sig->hasthis];
+               else
+                       t = &mono_defaults.int_class->byval_arg;
+               t = mono_type_get_underlying_type (t);
+
+               in = call->args [i];
 
-               if (!(sig->pinvoke) &&
-                   (sig->call_convention == MONO_CALL_VARARG) &&
+               if ((sig->call_convention == MONO_CALL_VARARG) &&
                    (i == sig->sentinelpos)) {
-                       emit_sig_cookie (cfg, call, cinfo, ainfo->size);
+                       emit_sig_cookie (cfg, call, cinfo);
                }
 
-               if (is_virtual && i == 0) {
-                       /* the argument will be attached to the call instrucion */
-                       in = call->args [i];
-                       call->used_iregs |= 1 << ainfo->reg;
-               } else {
-                       MONO_INST_NEW_CALL_ARG (cfg, arg, OP_OUTARG);
-                       in                  = call->args [i];
-                       arg->ins.cil_code   = in->cil_code;
-                       arg->ins.inst_left  = in;
-                       arg->ins.type       = in->type;
-                       MONO_INST_LIST_ADD_TAIL (&arg->ins.node, &call->out_args);
-                       arg->ins.inst_right = (MonoInst *) call;
-                       if (ainfo->regtype == RegTypeGeneral) {
-                               arg->ins.backend.reg3 = ainfo->reg;
-                               call->used_iregs |= 1 << ainfo->reg;
-                       } else if (ainfo->regtype == RegTypeStructByAddr) {
-                               call->used_iregs |= 1 << ainfo->reg;
-                               arg->ins.sreg1    = ainfo->reg;
-                               arg->ins.opcode   = OP_OUTARG_VT;
-                               arg->size         = -ainfo->vtsize;
-                               arg->offset       = ainfo->offset;
-                               arg->offPrm       = ainfo->offparm + sz.offStruct;
-                       } else if (ainfo->regtype == RegTypeStructByVal) {
-                               if (ainfo->reg != STK_BASE) 
-                                       call->used_iregs |= 1 << ainfo->reg;
-                               arg->ins.sreg1  = ainfo->reg;
-                               arg->ins.opcode = OP_OUTARG_VT;
-                               arg->size       = ainfo->size;
-                               arg->offset     = ainfo->offset;
-                               arg->offPrm     = ainfo->offparm + sz.offStruct;
-                       } else if (ainfo->regtype == RegTypeBase) {
-                               arg->ins.opcode   = OP_OUTARG_MEMBASE;
-                               arg->ins.sreg1    = ainfo->reg;
-                               arg->size         = ainfo->size;
-                               arg->offset       = ainfo->offset;
-                               call->used_iregs |= 1 << ainfo->reg;
-                       } else if (ainfo->regtype == RegTypeFP) {
-                               arg->ins.backend.reg3 = ainfo->reg;
-                               call->used_fregs |= 1 << ainfo->reg;
+               switch (ainfo->regtype) {
+               case RegTypeGeneral:
+                       add_outarg_reg2 (cfg, call, ainfo->regtype, ainfo->reg, in);
+                       break;
+               case RegTypeFP:
+                       if (MONO_TYPE_ISSTRUCT (t)) {
+                               /* Valuetype passed in one fp register */
+                               ainfo->regtype = RegTypeStructByValInFP;
+                               /* Fall through */
+                       } else {
                                if (ainfo->size == 4)
-                                       arg->ins.opcode = OP_OUTARG_R4;
-                               else
-                                       arg->ins.opcode = OP_OUTARG_R8;
+                                       ainfo->regtype = RegTypeFPR4;
+                               add_outarg_reg2 (cfg, call, ainfo->regtype, ainfo->reg, in);
+                               break;
+                       }
+               case RegTypeStructByVal:
+               case RegTypeStructByAddr:
+               case RegTypeStructByAddrOnStack: {
+                       guint32 align;
+                       guint32 size;
+
+                       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 {
+                                       /* 
+                                        * 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);
+                               }
+
+                       g_assert (in->klass);
+
+                       ainfo->offparm += cinfo->sz.offStruct;
+
+                       MONO_INST_NEW (cfg, ins, OP_OUTARG_VT);
+                       ins->sreg1 = in->dreg;
+                       ins->klass = in->klass;
+                       ins->backend.size = ainfo->size;
+                       ins->inst_p0 = call;
+                       ins->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
+                       memcpy (ins->inst_p1, ainfo, sizeof (ArgInfo));
+
+                       MONO_ADD_INS (cfg->cbb, ins);
+
+                       if (ainfo->regtype == RegTypeStructByAddr) {
+                               /* 
+                                * 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.
+                                */
+                               int treg = mono_alloc_preg (cfg);
+                               MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, treg, 
+                                                                                STK_BASE, ainfo->offparm);
+                               mono_call_inst_add_outarg_reg (cfg, call, treg, ainfo->reg, FALSE);
+                       } else if (ainfo->regtype == RegTypeStructByAddrOnStack) {
+                               /* The address of the valuetype is passed on the stack */
+                               int treg = mono_alloc_preg (cfg);
+                               MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, treg, 
+                                                                                STK_BASE, ainfo->offparm);
+                               MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG,
+                                                                                        ainfo->reg, ainfo->offset, treg);
+                       }
+                       break;
+               }
+               case RegTypeBase:
+                       if (!t->byref && t->type == MONO_TYPE_R4) {
+                               MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, 
+                                                                                        STK_BASE, ainfo->offset + 4,
+                                                                                        in->dreg);
+                       } else if (!t->byref && (t->type == MONO_TYPE_R8)) {
+                               MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, 
+                                                                                        STK_BASE, ainfo->offset,
+                                                                                        in->dreg);
                        } else {
-                               g_assert_not_reached ();
+                               MONO_INST_NEW (cfg, ins, OP_STORE_MEMBASE_REG);
+                               ins->inst_destbasereg = STK_BASE;
+                               ins->inst_offset = ainfo->offset;
+                               ins->sreg1 = in->dreg;
+
+                               /* This is needed by MonoTypedRef->value to point to the correct data */
+                               if ((sig->call_convention == MONO_CALL_VARARG) &&
+                                       (i >= sig->sentinelpos)) {
+                                       switch (ainfo->size) {
+                                       case 1:
+                                               ins->opcode = OP_STOREI1_MEMBASE_REG;
+                                               break;
+                                       case 2:
+                                               ins->opcode = OP_STOREI2_MEMBASE_REG;
+                                               break;
+                                       case 4:
+                                               ins->opcode = OP_STOREI4_MEMBASE_REG;
+                                               break;
+                                       default:
+                                               break;
+                                       }
+                               }
+
+                               MONO_ADD_INS (cfg->cbb, ins);
                        }
+                       break;
+               default:
+                       g_assert_not_reached ();
+                       break;
                }
        }
 
        /*
         * 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, sizeof(MonoType *));
+       if ((sig->call_convention == MONO_CALL_VARARG) &&
+           (i == sig->sentinelpos)) {
+               emit_sig_cookie (cfg, call, cinfo);
        }
-
-       g_free (cinfo);
-       return call;
 }
 
 /*========================= End of Function ========================*/
 
 /*------------------------------------------------------------------*/
 /*                                                                  */
-/* Name                - emit_sig_cookie.                                  */
+/* Name                - mono_arch_emit_outarg_vt                              */
 /*                                                                  */
-/* Function    - For variable length parameter lists construct a   */
-/*               signature cookie and emit it.                     */
-/*                                                                 */
 /*------------------------------------------------------------------*/
 
-static void
-emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, 
-                CallInfo *cinfo, int argSize)
+void
+mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
 {
-       MonoCallArgParm *arg;
-       MonoMethodSignature *tmpSig;
-       MonoInst *sigArg;
-                       
-       cfg->disable_aot = TRUE;
+       MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
+       ArgInfo *ainfo = (ArgInfo*)ins->inst_p1;
+       int size = ins->backend.size;
 
-       /*----------------------------------------------------------*/
-       /* mono_ArgIterator_Setup assumes the signature cookie is   */
-       /* passed first and all the arguments which were before it  */
-       /* passed on the stack after the signature. So compensate   */
-       /* by passing a different signature.                        */
-       /*----------------------------------------------------------*/
-       tmpSig = mono_metadata_signature_dup (call->signature);
-       tmpSig->param_count -= call->signature->sentinelpos;
-       tmpSig->sentinelpos  = 0;
-       if (tmpSig->param_count > 0)
-               memcpy (tmpSig->params, 
-                       call->signature->params + call->signature->sentinelpos, 
-                       tmpSig->param_count * sizeof(MonoType *));
+       if (ainfo->regtype == RegTypeStructByVal) {
+               /*
+                               arg->ins.sreg1  = ainfo->reg;
+                               arg->ins.opcode = OP_OUTARG_VT;
+                               arg->size       = ainfo->size;
+                               arg->offset     = ainfo->offset;
+                               arg->offPrm     = ainfo->offparm + cinfo->sz.offStruct;
+               */
+               if (ainfo->reg != STK_BASE) {
+                       MONO_OUTPUT_VTR (cfg, size, ainfo->reg, src->dreg, 0);
+               } else {
+                       MONO_OUTPUT_VTS (cfg, size, ainfo->reg, ainfo->offset,
+                                                         src->dreg, 0);
+               }       
+       } else if (ainfo->regtype == RegTypeStructByValInFP) {
+               int dreg = mono_alloc_freg (cfg);
+
+               if (ainfo->size == 4) {
+                       MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADR4_MEMBASE, dreg, src->dreg, 0);
+                       MONO_EMIT_NEW_UNALU (cfg, OP_S390_SETF4RET, dreg, dreg);
+               } else {
+                       g_assert (ainfo->size == 8);
 
-       MONO_INST_NEW (cfg, sigArg, OP_ICONST);
-       sigArg->inst_p0 = tmpSig;
+                       MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADR8_MEMBASE, dreg, src->dreg, 0);
+               }
 
-       MONO_INST_NEW_CALL_ARG (cfg, arg, OP_OUTARG_MEMBASE);
-       arg->ins.inst_left   = sigArg;
-       arg->ins.inst_right  = (MonoInst *) call;
-       arg->size            = argSize;
-       arg->offset          = cinfo->sigCookie.offset;
-       MONO_INST_LIST_ADD_TAIL (&arg->ins.node, &call->out_args);
+               mono_call_inst_add_outarg_reg (cfg, call, dreg, ainfo->reg, TRUE);
+       } else {
+               MONO_EMIT_NEW_MOVE (cfg, STK_BASE, ainfo->offparm,
+                                                        src->dreg, 0, size);
+       }
+}
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_emit_setret                                 */
+/*                                                                  */
+/*------------------------------------------------------------------*/
+
+void
+mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
+{
+       MonoType *ret = mono_type_get_underlying_type (mono_method_signature (method)->ret);
+
+       if (!ret->byref) {
+               if (ret->type == MONO_TYPE_R4) {
+                       MONO_EMIT_NEW_UNALU (cfg, OP_S390_SETF4RET, s390_f0, val->dreg);
+                       return;
+               } else if (ret->type == MONO_TYPE_R8) {
+                       MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, s390_f0, val->dreg);
+                       return;
+               }
+       }
+                       
+       MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
 }
 
 /*========================= End of Function ========================*/
@@ -2177,7 +2473,7 @@ handle_enum:
                break;
        case MONO_TYPE_VALUETYPE:
                if (mono_method_signature (method)->ret->data.klass->enumtype) {
-                       rtype = mono_method_signature (method)->ret->data.klass->enum_basetype->type;
+                       rtype = mono_class_enum_basetype (mono_method_signature (method)->ret->data.klass)->type;
                        goto handle_enum;
                }
                save_mode = SAVE_STRUCT;
@@ -2264,100 +2560,7 @@ mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
        MonoInst *ins, *n;
 
        MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
-               MonoInst *last_ins = mono_inst_list_prev (&ins->node, &bb->ins_list);
-
-               switch (ins->opcode) {
-               case OP_MUL_IMM: 
-                       /* remove unnecessary multiplication with 1 */
-                       if (ins->inst_imm == 1) {
-                               if (ins->dreg != ins->sreg1) {
-                                       ins->opcode = OP_MOVE;
-                               } else {
-                                       MONO_DELETE_INS (bb, ins);
-                                       continue;
-                               }
-                       }
-                       break;
-               case OP_LOAD_MEMBASE:
-               case OP_LOADI4_MEMBASE:
-                       /* 
-                        * OP_STORE_MEMBASE_REG reg, offset(basereg) 
-                        * OP_LOAD_MEMBASE offset(basereg), reg
-                        */
-                       if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG 
-                                        || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
-                           ins->inst_basereg == last_ins->inst_destbasereg &&
-                           ins->inst_offset == last_ins->inst_offset) {
-                               if (ins->dreg == last_ins->sreg1) {
-                                       MONO_DELETE_INS (bb, ins);
-                                       continue;
-                               } else {
-                                       ins->opcode = OP_MOVE;
-                                       ins->sreg1 = last_ins->sreg1;
-                               }
-
-                       /* 
-                        * Note: reg1 must be different from the basereg in the second load
-                        * OP_LOAD_MEMBASE offset(basereg), reg1
-                        * OP_LOAD_MEMBASE offset(basereg), reg2
-                        * -->
-                        * OP_LOAD_MEMBASE offset(basereg), reg1
-                        * OP_MOVE reg1, reg2
-                        */
-                       } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
-                                          || last_ins->opcode == OP_LOAD_MEMBASE) &&
-                             ins->inst_basereg != last_ins->dreg &&
-                             ins->inst_basereg == last_ins->inst_basereg &&
-                             ins->inst_offset == last_ins->inst_offset) {
-
-                               if (ins->dreg == last_ins->dreg) {
-                                       MONO_DELETE_INS (bb, ins);
-                                       continue;
-                               } else {
-                                       ins->opcode = OP_MOVE;
-                                       ins->sreg1 = last_ins->dreg;
-                               }
-
-                       }
-                       break;
-               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) {
-                               ins->opcode = (ins->opcode == OP_LOADI1_MEMBASE) ? OP_LCONV_TO_I1 : OP_LCONV_TO_U1;
-                               ins->sreg1 = last_ins->sreg1;                           
-                       }
-                       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) {
-                               ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_LCONV_TO_I2 : OP_LCONV_TO_U2;
-                               ins->sreg1 = last_ins->sreg1;                           
-                       }
-                       break;
-               case OP_MOVE:
-                       /* 
-                        * OP_MOVE reg, reg 
-                        */
-                       if (ins->dreg == ins->sreg1) {
-                               MONO_DELETE_INS (bb, ins);
-                               continue;
-                       }
-                       /* 
-                        * OP_MOVE sreg, dreg 
-                        * OP_MOVE dreg, sreg
-                        */
-                       if (last_ins && last_ins->opcode == OP_MOVE &&
-                           ins->sreg1 == last_ins->dreg &&
-                           ins->dreg == last_ins->sreg1) {
-                               MONO_DELETE_INS (bb, ins);
-                               continue;
-                       }
-                       break;
-               }
+               mono_peephole_ins (bb, ins);
        }
 }
 
@@ -2366,6 +2569,33 @@ mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
 void
 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
 {
+       MonoInst *ins, *next;
+
+       MONO_BB_FOR_EACH_INS_SAFE (bb, next, ins) {
+               switch (ins->opcode) {
+               case OP_DIV_IMM:
+               case OP_REM_IMM:
+               case OP_IDIV_IMM:
+               case OP_IREM_IMM:
+               case OP_IDIV_UN_IMM:
+               case OP_IREM_UN_IMM:
+               case OP_LAND_IMM:
+               case OP_LOR_IMM:
+               case OP_LXOR_IMM:
+               case OP_LOCALLOC_IMM:
+                       mono_decompose_op_imm (cfg, bb, ins);
+                       break;
+               case OP_LADD_IMM:
+                       if (!s390_is_imm16 (ins->inst_imm))
+                               /* This is created by the memcpy code which ignores is_inst_imm */
+                               mono_decompose_op_imm (cfg, bb, ins);
+                       break;
+               default:
+                       break;
+               }
+       }
+
+       bb->max_vreg = cfg->next_vreg;
 }
 
 /*========================= End of Function ========================*/
@@ -2427,6 +2657,28 @@ emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size,
 
 /*========================= End of Function ========================*/
 
+static gboolean is_unsigned (MonoInst *next)
+{
+       if ((next) && 
+               (((next->opcode >= OP_IBNE_UN) &&
+                 (next->opcode <= OP_IBLT_UN)) || 
+                ((next->opcode >= OP_LBNE_UN) &&
+                 (next->opcode <= OP_LBLT_UN)) ||
+                ((next->opcode >= OP_COND_EXC_NE_UN) &&
+                 (next->opcode <= OP_COND_EXC_LT_UN)) ||
+                ((next->opcode >= OP_COND_EXC_INE_UN) &&
+                 (next->opcode <= OP_COND_EXC_ILT_UN)) ||
+                ((next->opcode == OP_CLT_UN) ||
+                 (next->opcode == OP_CGT_UN)) ||
+                ((next->opcode == OP_ICLT_UN) ||
+                 (next->opcode == OP_ICGT_UN) ||
+                 (next->opcode == OP_LCLT_UN) ||
+                 (next->opcode == OP_LCGT_UN))))
+               return TRUE;
+       else
+               return FALSE;
+}
+
 /*------------------------------------------------------------------*/
 /*                                                                  */
 /* Name                - mono_arch_output_basic_block                      */
@@ -2439,7 +2691,7 @@ emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size,
 void
 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 {
-       MonoInst *ins, *next;
+       MonoInst *ins;
        MonoCallInst *call;
        guint offset;
        guint8 *code = cfg->native_code + cfg->code_len;
@@ -2612,29 +2864,77 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        break;
                case OP_LCONV_TO_U2: {
                        s390_lghi (code, s390_r0, -1);
-                       s390_sllg (code, s390_r0, s390_r0, 0, 16);
-                       s390_srlg (code, s390_r0, s390_r0, 0, 16);
+                       s390_sllg (code, s390_r0, s390_r0, 0, 48);
+                       s390_srlg (code, s390_r0, s390_r0, 0, 48);
                        if (ins->dreg != ins->sreg1) {
                                s390_lgr  (code, ins->dreg, ins->sreg1);
                        }
                        s390_ngr  (code, ins->dreg, s390_r0);
                }
                        break;
+               case OP_ICONV_TO_I1: {
+                       s390_lhi  (code, s390_r0, 0x80);
+                       if (ins->dreg != ins->sreg1) {
+                               s390_lr   (code, ins->dreg, ins->sreg1);
+                       }
+                       s390_nr   (code, s390_r0, ins->sreg1);
+                       s390_jz   (code, 7);
+                       s390_lhi  (code, s390_r13, -1);
+                       s390_sll  (code, s390_r13, 0, 8);
+                       s390_or   (code, ins->dreg, s390_r13);
+               }
+                       break;
+               case OP_ICONV_TO_I2: {
+                       s390_lhi  (code, s390_r0, 0x80);
+                       s390_sll  (code, s390_r0, 0, 8);
+                       if (ins->dreg != ins->sreg1) {
+                               s390_lr   (code, ins->dreg, ins->sreg1);
+                       }
+                       s390_nr   (code, s390_r0, ins->sreg1);
+                       s390_jz   (code, 7);
+                       s390_lhi  (code, s390_r13, -1);
+                       s390_sll  (code, s390_r13, 0, 16);
+                       s390_or   (code, ins->dreg, s390_r13);
+               }
+                       break;
+               case OP_ICONV_TO_U1: {
+                       s390_lhi  (code, s390_r0, 0xff);
+                       if (ins->dreg != ins->sreg1) {
+                               s390_lr   (code, ins->dreg, ins->sreg1);
+                       }
+                       s390_nr   (code, ins->dreg, s390_r0);
+               }
+                       break;
+               case OP_ICONV_TO_U2: {
+                       s390_lhi  (code, s390_r0, -1);
+                       s390_sll  (code, s390_r0, 0, 16);
+                       s390_srl  (code, s390_r0, 0, 16);
+                       if (ins->dreg != ins->sreg1) {
+                               s390_lr   (code, ins->dreg, ins->sreg1);
+                       }
+                       s390_nr   (code, ins->dreg, s390_r0);
+               }
+                       break;
                case OP_COMPARE: 
                case OP_LCOMPARE: {
-                       next = mono_inst_list_next (&ins->node, &bb->ins_list);
-                       if ((next) && (mono_opcode_to_cond(next->opcode) >= CMP_LE_UN))
+                       if (is_unsigned (ins->next))
                                s390_clgr (code, ins->sreg1, ins->sreg2);
                        else
                                s390_cgr  (code, ins->sreg1, ins->sreg2);
                }
                        break;
-               case OP_COMPARE_IMM: {
-                       next = mono_inst_list_next (&ins->node, &bb->ins_list);
+               case OP_ICOMPARE: {
+                       if (is_unsigned (ins->next))
+                               s390_clr  (code, ins->sreg1, ins->sreg2);
+                       else
+                               s390_cr   (code, ins->sreg1, ins->sreg2);
+               }
+                       break;
+               case OP_COMPARE_IMM:
+               case OP_LCOMPARE_IMM: {
                        if (s390_is_imm16 (ins->inst_imm)) {
                                s390_lghi (code, s390_r0, ins->inst_imm);
-                               if ((next) && 
-                                   (mono_opcode_to_cond(next->opcode) >= CMP_LE_UN))
+                               if (is_unsigned (ins->next))
                                        s390_clgr (code, ins->sreg1, s390_r0);
                                else
                                        s390_cgr  (code, ins->sreg1, s390_r0);
@@ -2642,28 +2942,17 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                                s390_basr (code, s390_r13, 0);
                                s390_j    (code, 6);
                                s390_llong(code, ins->inst_imm);
-                               if ((next) && 
-                                   (mono_opcode_to_cond(next->opcode) >= CMP_LE_UN))
+                               if (is_unsigned (ins->next))
                                        s390_clg  (code, ins->sreg1, 0, s390_r13, 4);
                                else
                                        s390_cg   (code, ins->sreg1, 0, s390_r13, 4);
                        }
                }
                        break;
-               case OP_ICOMPARE: {
-                       next = mono_inst_list_next (&ins->node, &bb->ins_list);
-                       if ((next) && (mono_opcode_to_cond(next->opcode) >= CMP_LE_UN))
-                               s390_clr  (code, ins->sreg1, ins->sreg2);
-                       else
-                               s390_cr   (code, ins->sreg1, ins->sreg2);
-               }
-                       break;
                case OP_ICOMPARE_IMM: {
-                       next = mono_inst_list_next (&ins->node, &bb->ins_list);
                        if (s390_is_imm16 (ins->inst_imm)) {
                                s390_lghi (code, s390_r0, ins->inst_imm);
-                               if ((next) && 
-                                   (mono_opcode_to_cond(next->opcode) >= CMP_LE_UN))
+                               if (is_unsigned (ins->next))
                                        s390_clr  (code, ins->sreg1, s390_r0);
                                else
                                        s390_cr   (code, ins->sreg1, s390_r0);
@@ -2672,8 +2961,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                                s390_basr (code, s390_r13, 0);
                                s390_j    (code, 4);
                                s390_word (code, ins->inst_imm);
-                               if ((next) && 
-                                   (mono_opcode_to_cond(next->opcode) >= CMP_LE_UN))
+                               if (is_unsigned (ins->next))
                                        s390_cl  (code, ins->sreg1, 0, s390_r13, 4);
                                else
                                        s390_c   (code, ins->sreg1, 0, s390_r13, 4);
@@ -2692,7 +2980,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        break;
                case OP_ADDCC: {
                        CHECK_SRCDST_COM;
-                       s390_algr  (code, ins->dreg, src2);
+                       s390_agr  (code, ins->dreg, src2);
                }
                        break;
                case OP_LADD: {
@@ -2719,6 +3007,14 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        }
                }
                        break;
+               case OP_LADD_IMM: {
+                       if (ins->dreg != ins->sreg1) {
+                               s390_lgr  (code, ins->dreg, ins->sreg1);
+                       }
+                       g_assert (s390_is_imm16 (ins->inst_imm));
+                       s390_aghi (code, ins->dreg, ins->inst_imm);
+               }
+                       break;
                case OP_ADC_IMM: {
                        if (ins->dreg != ins->sreg1) {
                                s390_lgr  (code, ins->dreg, ins->sreg1);
@@ -2735,14 +3031,16 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        }
                }
                        break;
-               case OP_IADD_OVF: {
+               case OP_IADD_OVF:
+               case OP_S390_IADD_OVF: {
                        CHECK_SRCDST_COM;
                        s390_ar    (code, ins->dreg, src2);
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
                        s390_lgfr  (code, ins->dreg, ins->dreg);
                }
                        break;
-               case OP_IADD_OVF_UN: {
+               case OP_IADD_OVF_UN:
+               case OP_S390_IADD_OVF_UN: {
                        CHECK_SRCDST_COM;
                        s390_algr  (code, ins->dreg, src2);
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_CY, "OverflowException");
@@ -2768,7 +3066,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        break;
                case OP_SUBCC: {
                        CHECK_SRCDST_NCOM;
-                       s390_slgr (code, ins->dreg, src2);
+                       s390_sgr (code, ins->dreg, src2);
                }
                        break;
                case OP_LSUB: {
@@ -2796,6 +3094,21 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        }
                }
                        break;
+               case OP_LSUB_IMM: {
+                       if (ins->dreg != ins->sreg1) {
+                               s390_lgr   (code, ins->dreg, ins->sreg1);
+                       }
+                       if (s390_is_imm16 (-ins->inst_imm)) {
+                               s390_lghi  (code, s390_r0, ins->inst_imm);
+                               s390_slgr  (code, ins->dreg, s390_r0);
+                       } else {
+                               s390_basr (code, s390_r13, 0);
+                               s390_j    (code, 6);
+                               s390_llong(code, ins->inst_imm);
+                               s390_slg  (code, ins->dreg, 0, s390_r13, 4);
+                       }
+               }
+                       break;
                case OP_SBB_IMM: {
                        if (ins->dreg != ins->sreg1) {
                                s390_lgr   (code, ins->dreg, ins->sreg1);
@@ -2871,21 +3184,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        s390_lgr   (code, ins->dreg, s390_r1);
                }
                        break;
-               case OP_DIV_IMM: {
-                       if (s390_is_imm16 (ins->inst_imm)) {
-                               s390_lghi (code, s390_r13, ins->inst_imm);
-                               s390_lgr  (code, s390_r1, ins->sreg1);
-                       } else {
-                               s390_basr (code, s390_r13, 0);
-                               s390_j    (code, 6);
-                               s390_llong(code, ins->inst_imm);
-                               s390_lgr  (code, s390_r1, ins->sreg1);
-                               s390_lg   (code, s390_r13, 0, s390_r13, 4);
-                       }
-                       s390_dsgr (code, s390_r0, s390_r13);
-                       s390_lgr  (code, ins->dreg, s390_r1);
-               }
-                       break;
                case OP_LREM: {
                        s390_lgr  (code, s390_r1, ins->sreg1);
                        s390_dsgr (code, s390_r0, ins->sreg2);
@@ -2899,21 +3197,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        s390_lgr   (code, ins->dreg, s390_r0);
                }
                        break;
-               case OP_REM_IMM: {
-                       if (s390_is_imm16 (ins->inst_imm)) {
-                               s390_lghi  (code, s390_r13, ins->inst_imm);
-                               s390_lgr   (code, s390_r0, ins->sreg1);
-                       } else {
-                               s390_basr (code, s390_r13, 0);
-                               s390_j    (code, 6);
-                               s390_llong(code, ins->inst_imm);
-                               s390_lgr  (code, s390_r0, ins->sreg1);
-                               s390_lg   (code, s390_r13, 0, s390_r13, 4);
-                       }
-                       s390_dsgr(code, s390_r0, s390_r13);
-                       s390_lgr (code, ins->dreg, s390_r0);
-               }
-                       break;
                case OP_LOR: {
                        if (ins->sreg1 == ins->dreg) {
                                s390_ogr  (code, ins->dreg, ins->sreg2);
@@ -3121,13 +3404,15 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        }
                }
                        break;
-               case OP_LADD_OVF: {
+               case OP_LADD_OVF:
+               case OP_S390_LADD_OVF: {
                        CHECK_SRCDST_COM;
                        s390_agr    (code, ins->dreg, src2);
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
                }
                        break;
-               case OP_LADD_OVF_UN: {
+               case OP_LADD_OVF_UN:
+               case OP_S390_LADD_OVF_UN: {
                        CHECK_SRCDST_COM;
                        s390_algr  (code, ins->dreg, src2);
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_CY, "OverflowException");
@@ -3169,27 +3454,31 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        s390_slgf (code, ins->dreg, 0, s390_r13, 4);
                }
                        break;
-               case OP_ISUB_OVF: {
+               case OP_ISUB_OVF:
+               case OP_S390_ISUB_OVF: {
                        CHECK_SRCDST_NCOM;
                        s390_sr   (code, ins->dreg, src2);
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
                        s390_lgfr (code, ins->dreg, ins->dreg);
                }
                        break;
-               case OP_ISUB_OVF_UN: {
+               case OP_ISUB_OVF_UN:
+               case OP_S390_ISUB_OVF_UN: {
                        CHECK_SRCDST_NCOM;
                        s390_slr  (code, ins->dreg, src2);
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NC, "OverflowException");
                        s390_llgfr(code, ins->dreg, ins->dreg);
                }
                        break;
-               case OP_LSUB_OVF: {
+               case OP_LSUB_OVF:
+               case OP_S390_LSUB_OVF: {
                        CHECK_SRCDST_NCOM;
                        s390_sgr   (code, ins->dreg, src2);
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
                }
                        break;
-               case OP_LSUB_OVF_UN: {
+               case OP_LSUB_OVF_UN:
+               case OP_S390_LSUB_OVF_UN: {
                        CHECK_SRCDST_NCOM;
                        s390_slgr  (code, ins->dreg, src2);
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NC, "OverflowException");
@@ -3315,10 +3604,8 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                }
                        break;
                case OP_ISHL: {
-                       if (ins->sreg1 != ins->dreg) {
-                               s390_lgfr (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_sll  (code, ins->dreg, ins->sreg2, 0);
+                       CHECK_SRCDST_NCOM;
+                       s390_sll  (code, ins->dreg, src2, 0);
                }
                        break;
                case OP_ISHL_IMM: {
@@ -3329,10 +3616,8 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                }
                        break;
                case OP_ISHR: {
-                       if (ins->sreg1 != ins->dreg) {
-                               s390_lgfr (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_sra (code, ins->dreg, ins->sreg2, 0);
+                       CHECK_SRCDST_NCOM;
+                       s390_sra (code, ins->dreg, src2, 0);
                }
                        break;
                case OP_ISHR_IMM: {
@@ -3350,10 +3635,8 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                }
                        break;
                case OP_ISHR_UN: {
-                       if (ins->sreg1 != ins->dreg) {
-                               s390_lgfr (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_srl  (code, ins->dreg, ins->sreg2, 0);
+                       CHECK_SRCDST_NCOM;
+                       s390_srl  (code, ins->dreg, src2, 0);
                }
                        break;
                case OP_INOT: {
@@ -3437,6 +3720,15 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        s390_lg   (code,ins->dreg, 0, s390_r13, 4);
                }
                        break;
+               case OP_JUMP_TABLE: {
+                       mono_add_patch_info (cfg, code - cfg->native_code, 
+                               (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
+                       s390_basr  (code, s390_r13, 0);
+                       s390_j     (code, 6);
+                       s390_llong (code, 0);
+                       s390_lg    (code, ins->dreg, 0, s390_r13, 4);
+               }
+                       break;
                case OP_MOVE:
                        if (ins->dreg != ins->sreg1) {
                                s390_lgr (code, ins->dreg, ins->sreg1);
@@ -3476,7 +3768,8 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_LT, "OverflowException");
                        s390_lgfr (code, ins->dreg, ins->sreg1);
                        break;
-               case OP_FMOVE: {
+               case OP_FMOVE:
+               case OP_FCONV_TO_R4: {
                        if (ins->dreg != ins->sreg1) {
                                s390_ldr   (code, ins->dreg, ins->sreg1);
                        }
@@ -3486,21 +3779,12 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        s390_ledbr (code, ins->dreg, ins->sreg1);
                }
                        break;
-               case OP_FCONV_TO_R4: {
-                       g_error ("Shouldn't be reached");
-                       next = mono_inst_list_next (&ins->node, &bb->ins_list);
-                       if ((next) &&
-                           (next->opcode != OP_FMOVE) &&
-                           (next->opcode != OP_STORER4_MEMBASE_REG))
-                               s390_ledbr (code, ins->dreg, ins->sreg1);
-               }
-                       break;
                case OP_JMP: {
                        if (cfg->method->save_lmf)
                                restoreLMF(code, cfg->frame_reg, cfg->stack_usage);
 
                        if (cfg->flags & MONO_CFG_HAS_TAIL) {
-                               code =  emit_load_volatile_registers(code, cfg);
+                               code =  emit_load_volatile_arguments (code, cfg);
                        }
 
                        code = backUpStackPtr(cfg, code);
@@ -3553,6 +3837,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        break;
                case OP_LCALL:
                case OP_VCALL:
+               case OP_VCALL2:
                case OP_VOIDCALL:
                case OP_CALL: {
                        s390_basr (code, s390_r13, 0);
@@ -3581,6 +3866,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        break;
                case OP_LCALL_REG:
                case OP_VCALL_REG:
+               case OP_VCALL2_REG:
                case OP_VOIDCALL_REG:
                case OP_CALL_REG: {
                        s390_lgr  (code, s390_r1, ins->sreg1);
@@ -3597,15 +3883,13 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        break;
                case OP_LCALL_MEMBASE:
                case OP_VCALL_MEMBASE:
+               case OP_VCALL2_MEMBASE:
                case OP_VOIDCALL_MEMBASE:
                case OP_CALL_MEMBASE: {
                        s390_lg   (code, s390_r1, 0, ins->sreg1, ins->inst_offset);
                        s390_basr (code, s390_r14, s390_r1);
                }
                        break;
-               case OP_OUTARG: 
-                       g_assert_not_reached ();
-                       break;
                case OP_LOCALLOC: {
                        int alloca_skip = S390_MINIMAL_STACK_SIZE + cfg->param_area;
                        int area_offset = S390_ALIGN(alloca_skip, S390_STACK_ALIGNMENT);
@@ -3675,24 +3959,30 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                }
                        break;
                case OP_START_HANDLER: {
+                       MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
+
                        S390_LONG (code, stg, stg, s390_r14, 0, 
-                                  ins->inst_left->inst_basereg, 
-                                  ins->inst_left->inst_offset);
+                                  spvar->inst_basereg, 
+                                  spvar->inst_offset);
                }
                        break;
                case OP_ENDFILTER: {
+                       MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
+
                        if (ins->sreg1 != s390_r2)
                                s390_lgr(code, s390_r2, ins->sreg1);
                        S390_LONG (code, lg, lg, s390_r14, 0, 
-                                  ins->inst_left->inst_basereg, 
-                                  ins->inst_left->inst_offset);
+                                  spvar->inst_basereg, 
+                                  spvar->inst_offset);
                        s390_br  (code, s390_r14);
                }
                        break;
                case OP_ENDFINALLY: {
+                       MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
+
                        S390_LONG (code, lg, lg, s390_r14, 0, 
-                                  ins->inst_left->inst_basereg, 
-                                  ins->inst_left->inst_offset);
+                                  spvar->inst_basereg, 
+                                  spvar->inst_offset);
                        s390_br  (code, s390_r14);
                }
                        break;
@@ -3704,6 +3994,14 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        break;
                case OP_LABEL: {
                        ins->inst_c0 = code - cfg->native_code;
+               }
+                       break;
+               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: 
@@ -3714,72 +4012,91 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                }
                        break;
                case OP_CEQ: 
-               case OP_ICEQ: {
+               case OP_ICEQ:
+               case OP_LCEQ: {
                        s390_lghi(code, ins->dreg, 1);
                        s390_jz  (code, 4);
                        s390_lghi(code, ins->dreg, 0);
                }
                        break;
                case OP_CLT: 
-               case OP_ICLT: {
+               case OP_ICLT:
+               case OP_LCLT: {
                        s390_lghi(code, ins->dreg, 1);
                        s390_jl  (code, 4);
                        s390_lghi(code, ins->dreg, 0);
                }
                        break;
                case OP_CLT_UN:
-               case OP_ICLT_UN: {
+               case OP_ICLT_UN:
+               case OP_LCLT_UN: {
                        s390_lghi(code, ins->dreg, 1);
                        s390_jlo (code, 4);
                        s390_lghi(code, ins->dreg, 0);
                }
                        break;
                case OP_CGT: 
-               case OP_ICGT: {
+               case OP_ICGT:
+               case OP_LCGT: {
                        s390_lghi(code, ins->dreg, 1);
                        s390_jh  (code, 4);
                        s390_lghi(code, ins->dreg, 0);
                }
                        break;
                case OP_CGT_UN:
-               case OP_ICGT_UN: {
+               case OP_ICGT_UN:
+               case OP_LCGT_UN: {
                        s390_lghi(code, ins->dreg, 1);
                        s390_jho (code, 4);
                        s390_lghi(code, ins->dreg, 0);
                }
                        break;
                case OP_COND_EXC_EQ:
+               case OP_COND_EXC_IEQ:
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_EQ, ins->inst_p1);
                        break;
                case OP_COND_EXC_NE_UN:
+               case OP_COND_EXC_INE_UN:
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NE, ins->inst_p1);
                        break;
                case OP_COND_EXC_LT:
+               case OP_COND_EXC_ILT:
                case OP_COND_EXC_LT_UN:
+               case OP_COND_EXC_ILT_UN:
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_LT, ins->inst_p1);
                        break;
                case OP_COND_EXC_GT:
+               case OP_COND_EXC_IGT:
                case OP_COND_EXC_GT_UN:
+               case OP_COND_EXC_IGT_UN:
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_GT, ins->inst_p1);
                        break;
                case OP_COND_EXC_GE:
+               case OP_COND_EXC_IGE:
                case OP_COND_EXC_GE_UN:
+               case OP_COND_EXC_IGE_UN:
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_GE, ins->inst_p1);
                        break;
                case OP_COND_EXC_LE:
+               case OP_COND_EXC_ILE:
                case OP_COND_EXC_LE_UN:
+               case OP_COND_EXC_ILE_UN:
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_LE, ins->inst_p1);
                        break;
                case OP_COND_EXC_OV:
+               case OP_COND_EXC_IOV:
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, ins->inst_p1);
                        break;
                case OP_COND_EXC_NO:
+               case OP_COND_EXC_INO:
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NO, ins->inst_p1);
                        break;
                case OP_COND_EXC_C:
+               case OP_COND_EXC_IC:
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_CY, ins->inst_p1);
                        break;
                case OP_COND_EXC_NC:
+               case OP_COND_EXC_INC:
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NC, ins->inst_p1);
                        break;
                case OP_LBEQ:
@@ -3862,6 +4179,17 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        s390_ldebr (code, ins->dreg, s390_f15);
                }
                        break;
+               case OP_ICONV_TO_R_UN: {
+                       s390_cdfbr (code, ins->dreg, ins->sreg1);
+                       s390_ltr   (code, ins->sreg1, ins->sreg1);
+                       s390_jnl   (code, 12);
+                       s390_basr  (code, s390_r13, 0);
+                       s390_j     (code, 6);
+                       s390_word  (code, 0x41f00000);
+                       s390_word  (code, 0);
+                       s390_adb   (code, ins->dreg, 0, s390_r13, 4);
+               }
+                       break;
                case OP_LCONV_TO_R_UN: {
                        s390_cdgbr (code, ins->dreg, ins->sreg1);
                        s390_ltgr  (code, ins->sreg1, ins->sreg1);
@@ -4270,7 +4598,7 @@ mono_arch_patch_code (MonoMethod *method, MonoDomain *domain,
 
 /*------------------------------------------------------------------*/
 /*                                                                  */
-/* Name         - emit_load_volatile_registers                      */
+/* Name         - emit_load_volatile_arguments                      */
 /*                                                                  */
 /* Function     - Emit the instructions to reload parameter regist- */
 /*                registers for use with "tail" operations.         */
@@ -4283,16 +4611,15 @@ mono_arch_patch_code (MonoMethod *method, MonoDomain *domain,
 /*------------------------------------------------------------------*/
 
 guint8 *
-emit_load_volatile_registers (guint8 *code, MonoCompile *cfg)
+emit_load_volatile_arguments (guint8 *code, MonoCompile *cfg)
 {
        MonoInst *inst;
        MonoMethod *method = cfg->method;
        MonoMethodSignature *sig = mono_method_signature(method);
        int pos = 0, i;
        CallInfo *cinfo;
-       size_data sz;
 
-       cinfo = calculate_sizes (NULL, sig, &sz, sig->pinvoke);
+       cinfo = get_call_info (NULL, NULL, sig, sig->pinvoke);
 
        if (cinfo->struct_ret) {
                ArgInfo *ainfo = &cinfo->ret;
@@ -4372,8 +4699,6 @@ emit_load_volatile_registers (guint8 *code, MonoCompile *cfg)
                pos++;
        }
 
-       g_free (cinfo);
-
        return code;
 }
 
@@ -4398,7 +4723,6 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        int alloc_size, pos, max_offset, i;
        guint8 *code;
        CallInfo *cinfo;
-       size_data sz;
        int tracing = 0;
        int lmfOffset;
 
@@ -4416,6 +4740,9 @@ mono_arch_emit_prolog (MonoCompile *cfg)
 
        s390_stmg (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
 
+       if (cfg->arch.bkchain_reg != -1)
+               s390_lgr (code, cfg->arch.bkchain_reg, STK_BASE);
+
        if (cfg->flags & MONO_CFG_HAS_ALLOCA) {
                cfg->used_int_regs |= 1 << 11;
        }
@@ -4459,7 +4786,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        sig = mono_method_signature (method);
        pos = 0;
 
-       cinfo = calculate_sizes (cfg, sig, &sz, sig->pinvoke);
+       cinfo = get_call_info (cfg, cfg->mempool, sig, sig->pinvoke);
 
        if (cinfo->struct_ret) {
                ArgInfo *ainfo = &cinfo->ret;
@@ -4471,6 +4798,9 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
                ArgInfo *ainfo = cinfo->args + i;
                inst = cfg->args [pos];
+               
+               if (inst->opcode == OP_VTARG_ADDR)
+                       inst = inst->inst_left;
 
                if (inst->opcode == OP_REGVAR) {
                        if (ainfo->regtype == RegTypeGeneral)
@@ -4554,6 +4884,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                                }
                        } else if (ainfo->regtype == RegTypeStructByAddr) {
                                s390_stg (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
+                       } else if (ainfo->regtype == RegTypeStructByAddrOnStack) {
                        } else
                                g_assert_not_reached ();
                }
@@ -4649,8 +4980,6 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        cfg->code_len = code - cfg->native_code;
        g_assert (cfg->code_len < cfg->code_size);
 
-       g_free (cinfo);
-
        return code;
 }
 
@@ -4881,78 +5210,59 @@ mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
 void
 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
 {
-
 }
 
 /*========================= End of Function ========================*/
 
 /*------------------------------------------------------------------*/
 /*                                                                  */
-/* Name                - mono_arch_emit_this_vret_args                     */
+/* Name                - mono_arch_emit_inst_for_method                        */
 /*                                                                  */
-/* Function    -                                                   */
-/*                                                                 */
 /*------------------------------------------------------------------*/
 
-void
-mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
+MonoInst*
+mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
 {
-       int this_dreg = s390_r2;
-       
-       if (vt_reg != -1)
-               this_dreg = s390_r3;
-
-       /* add the this argument */
-       if (this_reg != -1) {
-               MonoInst *this;
-               MONO_INST_NEW (cfg, this, OP_MOVE);
-               this->type  = this_type;
-               this->sreg1 = this_reg;
-               this->dreg  = mono_regstate_next_int (cfg->rs);
-               mono_bblock_add_inst (cfg->cbb, this);
-               mono_call_inst_add_outarg_reg (cfg, inst, this->dreg, this_dreg, FALSE);
-       }
-
-       if (vt_reg != -1) {
-               MonoInst *vtarg;
-               MONO_INST_NEW (cfg, vtarg, OP_MOVE);
-               vtarg->type  = STACK_MP;
-               vtarg->sreg1 = vt_reg;
-               vtarg->dreg  = mono_regstate_next_int (cfg->rs);
-               mono_bblock_add_inst (cfg->cbb, vtarg);
-               mono_call_inst_add_outarg_reg (cfg, inst, vtarg->dreg, s390_r2, FALSE);
-       }
+       return NULL;
 }
 
 /*========================= End of Function ========================*/
 
-/*------------------------------------------------------------------*/
-/*                                                                  */
-/* Name                - mono_arch_get_inst_for_method                   */
-/*                                                                  */
-/* Function    - Check for opcodes we can handle directly in       */
-/*               hardware.                                         */
-/*                                                                 */
-/*------------------------------------------------------------------*/
-
-MonoInst*
-mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, 
-                              MonoMethodSignature *fsig, MonoInst **args)
+void
+mono_arch_decompose_opts (MonoCompile *cfg, MonoInst *ins)
 {
-       MonoInst *ins = NULL;
-
-       if (cmethod->klass == mono_defaults.math_class) {
-               if (strcmp (cmethod->name, "Sqrt") == 0) {
-                       MONO_INST_NEW (cfg, ins, OP_SQRT);
-                       ins->inst_i0 = args [0];
-               }
-//             if (strcmp (cmethod->name, "Abs") == 0) {
-//                     MONO_INST_NEW (cfg, ins, OP_ABS);
-//                     ins->inst_i0 = args [0];
-//             }
+       /* 
+        * Have to rename these to avoid being decomposed normally, since the normal 
+        * decomposition does not work on S390.
+        */
+       switch (ins->opcode) {
+       case OP_ISUB_OVF:
+               ins->opcode = OP_S390_ISUB_OVF;
+               break;
+       case OP_ISUB_OVF_UN:
+               ins->opcode = OP_S390_ISUB_OVF_UN;
+               break;
+       case OP_IADD_OVF:
+               ins->opcode = OP_S390_IADD_OVF;
+               break;
+       case OP_IADD_OVF_UN:
+               ins->opcode = OP_S390_IADD_OVF_UN;
+               break;
+       case OP_LADD_OVF:
+               ins->opcode = OP_S390_LADD_OVF;
+               break;
+       case OP_LADD_OVF_UN:
+               ins->opcode = OP_S390_LADD_OVF_UN;
+               break;
+       case OP_LSUB_OVF:
+               ins->opcode = OP_S390_LSUB_OVF;
+               break;
+       case OP_LSUB_OVF_UN:
+               ins->opcode = OP_S390_LSUB_OVF_UN;
+               break;
+       default:
+               break;
        }
-
-       return ins;
 }
 
 /*========================= End of Function ========================*/
@@ -5144,8 +5454,6 @@ mono_arch_is_inst_imm (gint64 imm)
 
 /*========================= End of Function ========================*/
 
-/*========================= End of Function ========================*/
-
 /*------------------------------------------------------------------*/
 /*                                                                  */
 /* Name                - mono_arch_get_patch_offset                        */
@@ -5169,4 +5477,5 @@ mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
 {
        /* FIXME: implement */
        g_assert_not_reached ();
+       return NULL;
 }