MIPS: Fix IMT binary search code generation in backend
[mono.git] / mono / mini / mini-s390x.c
index 1f560c30b8e6d386420dd63e3b390d6b98f1c584..4ebe0b00b2ec054da4aef6b29d69fd88f3e539d2 100644 (file)
 /*                 D e f i n e s                                    */
 /*------------------------------------------------------------------*/
 
-#define EMIT_COND_BRANCH(ins,cond)                                                     \
-{                                                                                      \
-if (ins->flags & MONO_INST_BRLABEL) {                                                  \
-        if (ins->inst_i0->inst_c0) {                                                   \
-               int displace;                                                           \
-               displace = ((cfg->native_code + ins->inst_i0->inst_c0) - code) / 2;     \
-               if (s390_is_imm16(displace)) {                                          \
-                       s390_brc (code, cond, displace);                                \
-               } else {                                                                \
-                       s390_jcl (code, cond, displace);                                \
-               }                                                                       \
-        } else {                                                                       \
-               mono_add_patch_info (cfg, code - cfg->native_code,                      \
-                                    MONO_PATCH_INFO_LABEL, ins->inst_i0);              \
-               s390_jcl (code, cond, 0);                                               \
-        }                                                                              \
-} else {                                                                               \
-        if (ins->inst_true_bb->native_offset) {                                        \
-               int displace;                                                           \
-               displace = ((cfg->native_code +                                         \
-                           ins->inst_true_bb->native_offset) - code) / 2;              \
-               if (s390_is_imm16(displace)) {                                          \
-                       s390_brc (code, cond, displace);                                \
-               } else {                                                                \
-                       s390_jcl (code, cond, displace);                                \
-               }                                                                       \
-        } else {                                                                       \
-               mono_add_patch_info (cfg, code - cfg->native_code,                      \
-                                    MONO_PATCH_INFO_BB, ins->inst_true_bb);            \
-               s390_jcl (code, cond, 0);                                               \
-        }                                                                              \
-}                                                                                      \
+#define MAX_ARCH_DELEGATE_PARAMS 7
+
+#define EMIT_COND_BRANCH(ins,cond)                                             \
+{                                                                              \
+if (ins->inst_true_bb->native_offset) {                                        \
+       int displace;                                                           \
+       displace = ((cfg->native_code +                                         \
+                   ins->inst_true_bb->native_offset) - code) / 2;              \
+       if (s390_is_imm16(displace)) {                                          \
+               s390_brc (code, cond, displace);                                \
+       } else {                                                                \
+               s390_jcl (code, cond, displace);                                \
+       }                                                                       \
+} else {                                                                       \
+       mono_add_patch_info (cfg, code - cfg->native_code,                      \
+                            MONO_PATCH_INFO_BB, ins->inst_true_bb);            \
+       s390_jcl (code, cond, 0);                                               \
+}                                                                              \
 }
 
-#define EMIT_UNCOND_BRANCH(ins)                                                        \
-{                                                                                      \
-if (ins->flags & MONO_INST_BRLABEL) {                                                  \
-        if (ins->inst_i0->inst_c0) {                                                   \
-               int displace;                                                           \
-               displace = ((cfg->native_code + ins->inst_i0->inst_c0) - code) / 2;     \
-               if (s390_is_imm16(displace)) {                                          \
-                       s390_brc (code, S390_CC_UN, displace);                          \
-               } else {                                                                \
-                       s390_jcl (code, S390_CC_UN, displace);                          \
-               }                                                                       \
-        } else {                                                                       \
-               mono_add_patch_info (cfg, code - cfg->native_code,                      \
-                                    MONO_PATCH_INFO_LABEL, ins->inst_i0);              \
-               s390_jcl (code, S390_CC_UN, 0);                                         \
-        }                                                                              \
-} else {                                                                               \
-        if (ins->inst_target_bb->native_offset) {                                      \
-               int displace;                                                           \
-               displace = ((cfg->native_code +                                         \
-                           ins->inst_target_bb->native_offset) - code) / 2;            \
-               if (s390_is_imm16(displace)) {                                          \
-                       s390_brc (code, S390_CC_UN, displace);                          \
-               } else {                                                                \
-                       s390_jcl (code, S390_CC_UN, displace);                          \
-               }                                                                       \
-        } else {                                                                       \
-               mono_add_patch_info (cfg, code - cfg->native_code,                      \
-                                    MONO_PATCH_INFO_BB, ins->inst_target_bb);          \
-               s390_jcl (code, S390_CC_UN, 0);                                         \
-        }                                                                              \
-}                                                                                      \
+#define EMIT_UNCOND_BRANCH(ins)                                                \
+{                                                                              \
+if (ins->inst_target_bb->native_offset) {                                      \
+       int displace;                                                           \
+       displace = ((cfg->native_code +                                         \
+                   ins->inst_target_bb->native_offset) - code) / 2;            \
+       if (s390_is_imm16(displace)) {                                          \
+               s390_brc (code, S390_CC_UN, displace);                          \
+       } else {                                                                \
+               s390_jcl (code, S390_CC_UN, displace);                          \
+       }                                                                       \
+} else {                                                                       \
+       mono_add_patch_info (cfg, code - cfg->native_code,                      \
+                            MONO_PATCH_INFO_BB, ins->inst_target_bb);          \
+       s390_jcl (code, S390_CC_UN, 0);                                         \
+}                                                                              \
 }
 
 #define EMIT_COND_SYSTEM_EXCEPTION(cond,exc_name)                      \
@@ -156,6 +126,104 @@ 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 sReg, dReg;                                                 \
+               MONO_INST_NEW (cfg, inst, OP_NOP);                              \
+               if (size > 256) {                                               \
+                       inst->dreg        = dest;                               \
+                       inst->inst_offset = offset;                             \
+                       inst->sreg1       = src;                                \
+                       inst->inst_imm    = imm;                                \
+               } 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
 
@@ -165,6 +233,16 @@ if (ins->flags & MONO_INST_BRLABEL) {                                                      \
 
 #define MAX(a, b) ((a) > (b) ? (a) : (b))
 
+/*
+ * imt thunking size values
+ */
+#define CMP_SIZE       24
+#define LOADCON_SIZE   20
+#define LOAD_SIZE      6
+#define BR_SIZE                2
+#define JUMP_SIZE      6
+#define ENABLE_WRONG_METHOD_CHECK 0
+
 /*========================= End of Defines =========================*/
 
 /*------------------------------------------------------------------*/
@@ -180,9 +258,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 +299,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;
@@ -247,6 +328,7 @@ typedef struct {
        ArgInfo ret;
        ArgInfo sigCookie;
        size_data sz;
+       int vret_arg_index;
        ArgInfo args [1];
 } CallInfo;
 
@@ -273,9 +355,8 @@ static inline void add_float (guint *, size_data *, ArgInfo *);
 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 ======================*/
 
@@ -298,6 +379,8 @@ pthread_key_t lmf_addr_key;
 
 gboolean lmf_addr_key_inited = FALSE; 
 
+facilityList_t facs;
+
 #if 0
 
 extern __thread MonoDomain *tls_appdomain;
@@ -402,7 +485,7 @@ mono_arch_get_argument_info (MonoMethodSignature *csig,
        for (k = 0; k < param_count; k++) {
                
                if (csig->pinvoke)
-                       size = mono_type_native_stack_size (csig->params [k], &align);
+                       size = mono_type_native_stack_size (csig->params [k], (guint32 *) &align);
                else
                        size = mini_type_stack_size (NULL, csig->params [k], &align);
 
@@ -614,7 +697,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;
                                }
@@ -918,11 +1001,10 @@ 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);
-                       int j, size, align;
+                       int size, align;
 
                        info = mono_marshal_load_type_info (type->data.klass);
 
@@ -944,17 +1026,30 @@ handle_enum:
 
                        size = mono_type_size (type, &align);
                        switch (size) {
-                       case 1:
-                       case 2:
-                       case 4:
-                       case 8:
-                               printf ("[");
-                               for (j = 0; p && j < size; j++)
-                                       printf ("%02x,", p [j]);
-                               printf ("]\n");
-                               break;
-                       default:
-                               printf ("[VALUETYPE]\n");
+                               case 1: {
+                                       guint32 p = va_arg (ap, guint32);
+                                       printf ("[%02x]\n",p);
+                                       break;
+                               }
+                               case 2: {
+                                       guint32 p = va_arg (ap, guint32);
+                                       printf ("[%04x]\n",p);
+                                       break;
+                               }
+                               case 4: {
+                                       guint32 p = va_arg (ap, guint32);
+                                       printf ("[%08x]\n",p);
+                                       break;
+                               }
+                               case 8: {
+                                       guint64 p = va_arg (ap, guint64);
+                                       printf ("[%016lx]\n",p);
+                                       break;
+                               }
+                               default: {
+                                       gpointer p = va_arg (ap, gpointer);
+                                       printf ("[VALUETYPE] %p\n",p);
+                               }
                        }
                }
                break;
@@ -1052,23 +1147,51 @@ mono_arch_cpu_init (void)
 
 /*========================= End of Function ========================*/
 
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_init.                                   */
+/*                                                                  */
+/* Function    - Initialize architecture specific code.            */
+/*                                                                 */
+/*------------------------------------------------------------------*/
 
-/*
- * Initialize architecture specific code.
- */
 void
 mono_arch_init (void)
 {
+#if 0
+       /*
+        * When we do an architectural level set at z9 or better 
+        * we can use the STFLE instruction to show us
+        * what hardware facilities are available
+        */
+       int lFacility = sizeof(facs) % 8;
+
+       memset((char *) &facs, 0, sizeof(facs));
+
+       __asm__ ("      lgfr    0,%1\n"
+                "      stfle   %0\n"
+                : "=m" (facs) : "r" (lFacility) : "0", "cc");
+#endif
+
 }
 
-/*
- * Cleanup architecture specific code.
- */
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_cleanup.                                */
+/*                                                                  */
+/* Function    - Cleanup architecture specific code    .           */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
 void
 mono_arch_cleanup (void)
 {
 }
 
+/*========================= End of Function ========================*/
+
 /*------------------------------------------------------------------*/
 /*                                                                  */
 /* Name                - mono_arch_cpu_optimizazions                       */
@@ -1122,7 +1245,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;
@@ -1184,12 +1307,15 @@ mono_arch_get_global_int_regs (MonoCompile *cfg)
        MonoMethodHeader *header;
        int i, top = 13;
 
-       header = mono_method_get_header (cfg->method);
+       header = cfg->header;
        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));
        }
 
@@ -1259,18 +1385,19 @@ add_stackParm (guint *gr, size_data *sz, ArgInfo *ainfo, gint size)
                sz->stack_size  = S390_ALIGN(sz->stack_size, sizeof(long));
                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->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;
 }
@@ -1310,7 +1437,7 @@ add_float (guint *fr,  size_data *sz, ArgInfo *ainfo)
 
 /*------------------------------------------------------------------*/
 /*                                                                  */
-/* Name                - get_call_info                                   */
+/* Name                - get_call_info                                     */
 /*                                                                  */
 /* Function    - Determine the amount of space required for code   */
 /*               and stack. In addition determine starting points  */
@@ -1322,7 +1449,7 @@ add_float (guint *fr,  size_data *sz, ArgInfo *ainfo)
 static CallInfo *
 get_call_info (MonoCompile *cfg, MonoMemPool *mp, MonoMethodSignature *sig, gboolean is_pinvoke)
 {
-       guint i, fr, gr, size;
+       guint i, fr, gr, size, pstart;
        int nParm = sig->hasthis + sig->param_count;
        MonoType *ret_type;
        guint32 simpletype, align;
@@ -1399,7 +1526,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)
@@ -1407,20 +1534,16 @@ enum_retvalue:
                        else
                                size = mono_class_value_size (klass, &align);
        
-                       cinfo->ret.reg    = s390_r2;
                        cinfo->struct_ret = 1;
                        cinfo->ret.size   = size;
                        cinfo->ret.vtsize = size;
-                       gr++;
                         break;
                }
                case MONO_TYPE_TYPEDBYREF:
                        size = sizeof (MonoTypedRef);
-                       cinfo->ret.reg    = s390_r2;
                        cinfo->struct_ret = 1;
                        cinfo->ret.size   = size;
                        cinfo->ret.vtsize = size;
-                       gr++;
                        break;
                case MONO_TYPE_VOID:
                        break;
@@ -1428,10 +1551,50 @@ enum_retvalue:
                        g_error ("Can't handle as return value 0x%x", sig->ret->type);
        }
 
-       if (sig->hasthis) {
-               cinfo->args[nParm].size = sizeof(gpointer);
-               add_general (&gr, sz, cinfo->args+nParm);
-               nParm++;
+
+       pstart = 0;
+       /*
+        * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
+        * the first argument, allowing 'this' to be always passed in the first arg reg.
+        * Also do this if the first argument is a reference type, since virtual calls
+        * are sometimes made using calli without sig->hasthis set, like in the delegate
+        * invoke wrappers.
+        */
+       if (cinfo->struct_ret && !is_pinvoke && 
+           (sig->hasthis || 
+             (sig->param_count > 0 && 
+             MONO_TYPE_IS_REFERENCE (mini_type_get_underlying_type (gsctx, sig->params [0]))))) {
+               if (sig->hasthis) {
+                       cinfo->args[nParm].size = sizeof (gpointer);
+                       add_general (&gr, sz, cinfo->args + nParm);
+               } else {
+                       cinfo->args[nParm].size = sizeof (gpointer);
+                       add_general (&gr, sz, &cinfo->args [sig->hasthis + nParm]);
+                       pstart = 1;
+               }
+               nParm ++;
+               cinfo->vret_arg_index = 1;
+               cinfo->ret.reg = gr;
+               gr ++;
+       } else {
+               /* this */
+               if (sig->hasthis) {
+                       cinfo->args[nParm].size = sizeof (gpointer);
+                       add_general (&gr, sz, cinfo->args + nParm);
+                       nParm ++;
+               }
+
+               if (cinfo->struct_ret) {
+                       cinfo->ret.reg = gr;
+                       gr ++;
+               }
+       }
+
+       if ((sig->call_convention == MONO_CALL_VARARG) && (sig->param_count == 0)) {
+               gr = S390_LAST_ARG_REG + 1;
+
+               /* Emit the signature cookie just before the implicit arguments */
+               add_general (&gr, sz, &cinfo->sigCookie);
        }
 
        /*----------------------------------------------------------*/
@@ -1440,7 +1603,7 @@ enum_retvalue:
        /* parameters.                                              */
        /*----------------------------------------------------------*/
 
-       for (i = 0; i < sig->param_count; ++i) {
+       for (i = pstart; i < sig->param_count; ++i) {
                MonoType *ptype;
 
                /*--------------------------------------------------*/
@@ -1537,6 +1700,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;
                        }
 
@@ -1545,6 +1709,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;
                        }
 
@@ -1612,6 +1777,8 @@ enum_retvalue:
        /* Handle the case where there are no implicit arguments    */
        /*----------------------------------------------------------*/
        if ((sig->call_convention == MONO_CALL_VARARG) &&
+           (nParm > 0) &&
+           (!sig->pinvoke) &&
            (sig->param_count == sig->sentinelpos)) {
                gr = S390_LAST_ARG_REG + 1;
                add_general (&gr, sz, &cinfo->sigCookie);
@@ -1665,11 +1832,11 @@ mono_arch_allocate_vars (MonoCompile *cfg)
        MonoMethodHeader *header;
        MonoInst *inst;
        CallInfo *cinfo;
-       int iParm, iVar, offset, size, align, curinst;
+       int iParm, iVar, offset, align, size, curinst;
        int frame_reg = STK_BASE;
        int sArg, eArg;
 
-       header  = mono_method_get_header (cfg->method);
+       header  = cfg->header;
 
        cfg->flags |= MONO_CFG_HAS_SPILLUP;
 
@@ -1688,6 +1855,8 @@ 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;           
 
@@ -1695,10 +1864,7 @@ mono_arch_allocate_vars (MonoCompile *cfg)
        
        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;
@@ -1715,7 +1881,11 @@ mono_arch_allocate_vars (MonoCompile *cfg)
        /* to point at the local variables.                             */
        /* add parameter area size for called functions                 */
        /*--------------------------------------------------------------*/
-       offset          = (cfg->param_area + S390_MINIMAL_STACK_SIZE);
+       if (cfg->param_area == 0)
+               offset = S390_MINIMAL_STACK_SIZE;
+       else
+               offset = cfg->param_area;
+
        cfg->sig_cookie = 0;
 
        if (cinfo->struct_ret) {
@@ -1725,9 +1895,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);
@@ -1757,58 +1924,89 @@ 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);
-                               } 
+                                       if (cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) 
+                                               inst->inst_offset  = offset;
+                                       else
+                                               inst->inst_offset  = offset + (8 - size);
+                               }
+                               break;
                        }
                        if ((sig->call_convention == MONO_CALL_VARARG) && 
                            (cinfo->args[iParm].regtype != RegTypeGeneral) &&
                            (iParm < sig->sentinelpos)) 
                                cfg->sig_cookie += size;
 
-                       offset += size;
+                       offset += MAX(size, 8);
                }
                curinst++;
        }
@@ -1821,12 +2019,13 @@ mono_arch_allocate_vars (MonoCompile *cfg)
                        continue;
 
                /*--------------------------------------------------*/
-               /* inst->backend.is_pinvoke indicates native sized value types, */
-               /* this is used by the pinvoke wrappers when they   */
-               /* call functions returning structure               */
+               /* inst->backend.is_pinvoke indicates native sized  */
+               /* value typs this is used by the pinvoke wrappers  */
+               /* when they call functions returning structure     */
                /*--------------------------------------------------*/
                if (inst->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (inst->inst_vtype))
-                       size = mono_class_native_size (mono_class_from_mono_type(inst->inst_vtype), &align);
+                       size = mono_class_native_size (mono_class_from_mono_type(inst->inst_vtype), 
+                                                      (guint32 *) &align);
                else
                        size = mono_type_size (inst->inst_vtype, &align);
 
@@ -1856,13 +2055,25 @@ mono_arch_allocate_vars (MonoCompile *cfg)
        /*------------------------------------------------------*/
        cfg->stack_offset = S390_ALIGN(offset, S390_STACK_ALIGNMENT);
 
+       /*------------------------------------------------------*/
+       /* Fix offsets for args whose value is in parent frame  */
+       /*------------------------------------------------------*/
+       for (iParm = sArg; iParm < eArg; ++iParm) {
+               inst = cfg->args [iParm];
+
+               if (inst->opcode == OP_S390_STKARG) {
+                       inst->opcode = OP_REGOFFSET;
+                       inst->inst_offset += cfg->stack_offset;
+               }
+       }
 }
 
 /*========================= End of Function ========================*/
 
 /*------------------------------------------------------------------*/
 /*                                                                  */
-/* Name                - mono_arch_create_vars                                 */
+/* Name                - mono_arch_create_vars                             */
+/*                                                                  */
 /*------------------------------------------------------------------*/
 
 void
@@ -1888,25 +2099,101 @@ mono_arch_create_vars (MonoCompile *cfg)
 
 /*------------------------------------------------------------------*/
 /*                                                                  */
-/* Name                - mono_arch_call_opcode                             */
+/* Name                - add_outarg_reg2.                                  */
 /*                                                                  */
-/* 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)
+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 ();
+       }
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - emit_sig_cookie.                                  */
+/*                                                                  */
+/*------------------------------------------------------------------*/
+
+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);
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_emit_call                               */
+/*                                                                  */
+/*------------------------------------------------------------------*/
+
+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;
+       ArgInfo *ainfo = NULL;
+       int stackSize;    
+       MonoMethodHeader *header;
+       int frmReg;
 
        sig = call->signature;
        n = sig->param_count + sig->hasthis;
@@ -1914,128 +2201,252 @@ mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb,
        
        cinfo = get_call_info (cfg, cfg->mempool, sig, sig->pinvoke);
 
-       call->stack_usage = MAX(cinfo->sz.stack_size, 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);
+       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);
+       }
+
+       header = cfg->header;
+       if ((cfg->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
+               frmReg = s390_r11;
+       else
+               frmReg = STK_BASE;
 
        for (i = 0; i < n; ++i) {
+               MonoType *t;
+
                ainfo = cinfo->args + i;
+               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) &&
+                   (!sig->pinvoke) &&
                    (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 + cinfo->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 + cinfo->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, 
+                                                        frmReg, 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, 
+                                                        frmReg, 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;
+
+#if 0
+                               /* 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;
+                                       }
+                               }
+#endif
+
+                               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) &&
+           (!sig->pinvoke) &&
+           (i == sig->sentinelpos)) {
+               emit_sig_cookie (cfg, call, 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_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADR8_MEMBASE, dreg, src->dreg, 0);
+               }
+
+               mono_call_inst_add_outarg_reg (cfg, call, dreg, ainfo->reg, TRUE);
+       } else {
+               MonoMethodHeader *header;
+               int srcReg;
+
+               header = mono_method_get_header (cfg->method);
+               if ((cfg->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
+                       srcReg = s390_r11;
+               else
+                       srcReg = STK_BASE;
+
+               MONO_EMIT_NEW_MOVE (cfg, srcReg, ainfo->offparm,
+                                                        src->dreg, 0, size);
+       }
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_emit_setret                             */
+/*                                                                  */
+/*------------------------------------------------------------------*/
 
-       MONO_INST_NEW (cfg, sigArg, OP_ICONST);
-       sigArg->inst_p0 = tmpSig;
+void
+mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
+{
+       MonoType *ret = mono_type_get_underlying_type (mono_method_signature (method)->ret);
 
-       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);
+       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 ========================*/
@@ -2146,7 +2557,7 @@ mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p,
 /*------------------------------------------------------------------*/
 
 void*
-mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
+mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
 {
        guchar     *code = p;
        int        save_mode = SAVE_NONE,
@@ -2179,7 +2590,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;
@@ -2246,14 +2657,25 @@ handle_enum:
 
 /*========================= End of Function ========================*/
 
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_peephole_pass_1                         */
+/*                                                                  */
+/* Function    - Form a peephole pass at the code looking for      */
+/*               simple optimizations.                             */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
 void
 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
 {
 }
 
+/*========================= End of Function ========================*/
+
 /*------------------------------------------------------------------*/
 /*                                                                  */
-/* Name                - mono_arch_peephole_pass                                     */
+/* Name                - mono_arch_peephole_pass_2                         */
 /*                                                                  */
 /* Function    - Form a peephole pass at the code looking for      */
 /*               simple optimizations.                             */
@@ -2272,14 +2694,17 @@ mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
 
 /*========================= End of Function ========================*/
 
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_lowering_pass.                          */
+/*                                                                  */
+/*------------------------------------------------------------------*/
+
 void
 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
 {
        MonoInst *ins, *next;
 
-       if (bb->max_vreg > cfg->rs->next_vreg)
-               cfg->rs->next_vreg = bb->max_vreg;
-
        MONO_BB_FOR_EACH_INS_SAFE (bb, next, ins) {
                switch (ins->opcode) {
                case OP_DIV_IMM:
@@ -2288,14 +2713,23 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_IREM_IMM:
                case OP_IDIV_UN_IMM:
                case OP_IREM_UN_IMM:
-                       mono_decompose_op_imm (cfg, ins);
+               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->rs->next_vreg;
+       bb->max_vreg = cfg->next_vreg;
 }
 
 /*========================= End of Function ========================*/
@@ -2314,7 +2748,7 @@ emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size,
 {
        /* sreg is a float, dreg is an integer reg. */
        if (is_signed) {
-               s390_cfdbr (code, dreg, 5, sreg);
+               s390_cgdbr (code, dreg, 5, sreg);
                switch (size) {
                        case 1:
                                s390_lghi (code, s390_r0, 0);
@@ -2330,8 +2764,8 @@ emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size,
                short *o[1];
                s390_basr   (code, s390_r13, 0);
                s390_j      (code, 10);
-               s390_llong  (code, 0x41e0000000000000);
-               s390_llong  (code, 0x41f0000000000000);
+               s390_llong  (code, 0x41e0000000000000llu);
+               s390_llong  (code, 0x41f0000000000000llu);
                s390_ldr    (code, s390_f15, sreg);
                s390_cdb    (code, s390_f15, 0, s390_r13, 4);
                s390_jl     (code, 0); CODEPTR (code, o[0]);
@@ -2357,6 +2791,40 @@ emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size,
 
 /*========================= End of Function ========================*/
 
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - gboolean_is_unsigned.                             */
+/*                                                                  */
+/* Function    - Return TRUE if next opcode is checking for un-    */
+/*               signed value.                                     */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+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;
+}
+
+/*========================= End of Function ========================*/
+
 /*------------------------------------------------------------------*/
 /*                                                                  */
 /* Name                - mono_arch_output_basic_block                      */
@@ -2369,7 +2837,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;
@@ -2508,63 +2976,112 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                }
                        break;
                case OP_LCONV_TO_I1: {
-                       s390_lghi  (code, s390_r0, 0x80);
-                       if (ins->dreg != ins->sreg1) {
-                               s390_lgr  (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_ngr  (code, s390_r0, ins->sreg1);
-                       s390_jz   (code, 9);
-                       s390_lghi (code, s390_r13, -1);
-                       s390_sllg (code, s390_r13, s390_r13, 0, 8);
-                       s390_ogr  (code, ins->dreg, s390_r13);
+#if 0
+                       s390_lgbr (code, ins->dreg, ins->sreg1);
+#else
+                       s390_sllg (code, ins->dreg, ins->sreg1, 0, 56);
+                       s390_srag (code, ins->dreg, ins->dreg, 0, 56);
+#endif
                }
                        break;
                case OP_LCONV_TO_I2: {
-                       s390_lghi (code, s390_r0, 0x80);
-                       s390_sllg (code, s390_r0, s390_r0, 0, 8);
-                       if (ins->dreg != ins->sreg1) {
-                               s390_lgr  (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_ngr  (code, s390_r0, ins->sreg1);
-                       s390_jz   (code, 9);
-                       s390_lghi (code, s390_r13, -1);
-                       s390_sllg (code, s390_r13, s390_r13, 0, 16);
-                       s390_ogr  (code, ins->dreg, s390_r13);
+#if 0
+                       s390_lghr (code, ins->dreg, ins->sreg1);
+#else
+                       s390_sllg (code, ins->dreg, ins->sreg1, 0, 48);
+                       s390_srag (code, ins->dreg, ins->dreg, 0, 48);
+#endif
                }
                        break;
                case OP_LCONV_TO_U1: {
-                       s390_lghi (code, s390_r0, 0xff);
-                       if (ins->dreg != ins->sreg1) {
+#if 0
+                       s390_llghr (code, ins->dreg, ins->sreg1);
+#else
+                       if (ins->dreg != ins->sreg1)
                                s390_lgr  (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_ngr  (code, ins->dreg, s390_r0);
+                       s390_lghi  (code, s390_r0, 0xff);
+                       s390_ngr   (code, ins->dreg, s390_r0);
+#endif
                }
                        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);
-                       if (ins->dreg != ins->sreg1) {
+#if 0
+                       s390_llghr (code, ins->dreg, ins->sreg1);
+#else
+                       if (ins->dreg != ins->sreg1)
                                s390_lgr  (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_ngr  (code, ins->dreg, s390_r0);
+                       s390_lghi  (code, s390_r0, -1);
+                       s390_srlg  (code, s390_r0, s390_r0, 0, 48);
+                       s390_ngr   (code, ins->dreg, s390_r0);
+#endif
+               }
+                       break;
+               case OP_ICONV_TO_I1: {
+#if 0
+                       s390_lbr  (code, ins->dreg, ins->sreg1);
+#else
+                       if (ins->dreg != ins->sreg1)
+                               s390_lr  (code, ins->dreg, ins->sreg1);
+                       s390_sll (code, ins->dreg, 0, 24);
+                       s390_sra (code, ins->dreg, 0, 24);
+                       
+#endif
+               }
+                       break;
+               case OP_ICONV_TO_I2: {
+#if 0
+                       s390_lhr  (code, ins->dreg, ins->sreg1);
+#else
+                       if (ins->dreg != ins->sreg1)
+                               s390_lr  (code, ins->dreg, ins->sreg1);
+                       s390_sll (code, ins->dreg, 0, 16);
+                       s390_sra (code, ins->dreg, 0, 16);
+#endif
+               }
+                       break;
+               case OP_ICONV_TO_U1: {
+#if 0
+                       s390_llcr (code, ins->dreg, ins->sreg1);
+#else
+                       if (ins->dreg != ins->sreg1)
+                               s390_lr  (code, ins->dreg, ins->sreg1);
+                       s390_lhi  (code, s390_r0, 0xff);
+                       s390_nr   (code, ins->dreg, s390_r0);
+#endif
+               }
+                       break;
+               case OP_ICONV_TO_U2: {
+#if 0
+                       s390_llhr (code, ins->dreg, ins->sreg1);
+#else
+                       if (ins->dreg != ins->sreg1)
+                               s390_lr  (code, ins->dreg, ins->sreg1);
+                       s390_lhi  (code, s390_r0, -1);
+                       s390_srl  (code, s390_r0, 0, 16);
+                       s390_nr   (code, ins->dreg, s390_r0);
+#endif
                }
                        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);
@@ -2572,28 +3089,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);
@@ -2602,8 +3108,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);
@@ -2622,7 +3127,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: {
@@ -2649,6 +3154,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);
@@ -2665,14 +3178,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");
@@ -2698,7 +3213,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: {
@@ -2726,6 +3241,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);
@@ -3021,13 +3551,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");
@@ -3069,27 +3601,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");
@@ -3215,10 +3751,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: {
@@ -3228,11 +3762,9 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        s390_sll (code, ins->dreg, 0, (ins->inst_imm & 0x1f));
                }
                        break;
-               case OP_ISHR: {
-                       if (ins->sreg1 != ins->dreg) {
-                               s390_lgfr (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_sra (code, ins->dreg, ins->sreg2, 0);
+               case OP_ISHR: {
+                       CHECK_SRCDST_NCOM;
+                       s390_sra (code, ins->dreg, src2, 0);
                }
                        break;
                case OP_ISHR_IMM: {
@@ -3250,10 +3782,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: {
@@ -3337,6 +3867,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);
@@ -3376,7 +3915,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);
                        }
@@ -3386,21 +3926,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);
@@ -3453,6 +3984,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);
@@ -3481,6 +4013,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);
@@ -3497,18 +4030,23 @@ 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);
+                       int alloca_skip;
+                       int area_offset;
+
+                       if (cfg->param_area == 0)
+                               alloca_skip = S390_MINIMAL_STACK_SIZE;
+                       else
+                               alloca_skip = cfg->param_area;
+
+                       area_offset = S390_ALIGN(alloca_skip, S390_STACK_ALIGNMENT);
                        s390_lgr  (code, s390_r1, ins->sreg1);
                        if (ins->flags & MONO_INST_INIT)
                                s390_lgr  (code, s390_r0, ins->sreg1);
@@ -3575,24 +4113,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;
@@ -3600,10 +4144,19 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        mono_add_patch_info (cfg, code-cfg->native_code, 
                                             MONO_PATCH_INFO_BB, ins->inst_target_bb);
                        s390_brasl (code, s390_r14, 0);
+                       mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, 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: 
@@ -3614,72 +4167,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:
@@ -3762,6 +4334,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);
@@ -3974,24 +4557,35 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                                                   ins->inst_offset, ins->sreg1, ins->inst_imm);
                                } else {
                                        s390_lgr  (code, s390_r0, ins->dreg);
-                                       if (s390_is_imm16 (ins->inst_offset)) {
-                                               s390_aghi (code, s390_r0, ins->inst_offset);
-                                       } else {
-                                               s390_basr (code, s390_r13, 0);
-                                               s390_j    (code, 6);
-                                               s390_llong(code, ins->inst_offset);
-                                               s390_a    (code, s390_r0, 0, s390_r13, 4);
+                                       if (ins->inst_offset > 0) {
+                                               if (s390_is_imm16 (ins->inst_offset)) {
+                                                       s390_aghi (code, s390_r0, ins->inst_offset);
+                                               } else {
+                                                       s390_basr (code, s390_r13, 0);
+                                                       s390_j    (code, 6);
+                                                       s390_llong(code, ins->inst_offset);
+                                                       s390_ag   (code, s390_r0, 0, s390_r13, 4);
+                                               }
                                        }
                                        s390_lgr  (code, s390_r12, ins->sreg1);
-                                       if (s390_is_imm16 (ins->inst_imm)) {
-                                               s390_aghi (code, s390_r12, ins->inst_imm);
+                                       if (ins->inst_imm > 0) {
+                                               if (s390_is_imm16 (ins->inst_imm)) {
+                                                       s390_aghi (code, s390_r12, ins->inst_imm);
+                                               } else {
+                                                       s390_basr (code, s390_r13, 0);
+                                                       s390_j    (code, 6);
+                                                       s390_llong(code, ins->inst_imm);
+                                                       s390_ag   (code, s390_r12, 0, s390_r13, 4);
+                                               }
+                                       }
+                                       if (s390_is_imm16 (ins->backend.size)) {
+                                               s390_lghi (code, s390_r1, ins->backend.size);
                                        } else {
                                                s390_basr (code, s390_r13, 0);
                                                s390_j    (code, 6);
-                                               s390_llong(code, ins->inst_imm);
-                                               s390_ag   (code, s390_r12, 0, s390_r13, 4);
+                                               s390_llong(code, ins->backend.size);
+                                               s390_lg   (code, s390_r1, 0, s390_r13, 4);
                                        }
-                                       s390_lgr  (code, s390_r1, ins->sreg1);
                                        s390_lgr  (code, s390_r13, s390_r1);
                                        s390_mvcle(code, s390_r0, s390_r12, 0, 0);
                                        s390_jo   (code, -2);
@@ -4043,7 +4637,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                }
                        break;  
                case OP_ATOMIC_EXCHANGE_I4: {
-                       s390_lg  (code, s390_r0, 0, ins->inst_basereg, ins->inst_offset);
+                       s390_l   (code, s390_r0, 0, ins->inst_basereg, ins->inst_offset);
                        s390_cs  (code, s390_r0, ins->sreg2, ins->inst_basereg, ins->inst_offset);
                        s390_jnz (code, -4);
                        s390_lgfr(code, ins->dreg, s390_r0);
@@ -4170,7 +4764,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.         */
@@ -4183,7 +4777,7 @@ 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;
@@ -4294,6 +4888,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        MonoInst *inst;
        int alloc_size, pos, max_offset, i;
        guint8 *code;
+       guint32 size;
        CallInfo *cinfo;
        int tracing = 0;
        int lmfOffset;
@@ -4312,6 +4907,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;
        }
@@ -4358,8 +4956,8 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        cinfo = get_call_info (cfg, cfg->mempool, sig, sig->pinvoke);
 
        if (cinfo->struct_ret) {
-               ArgInfo *ainfo = &cinfo->ret;
-               inst         = cfg->vret_addr;
+               ArgInfo *ainfo     = &cinfo->ret;
+               inst               = cfg->vret_addr;
                inst->backend.size = ainfo->vtsize;
                s390_stg (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
        }
@@ -4367,6 +4965,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)
@@ -4426,7 +5027,12 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                                        s390_lgr  (code, s390_r13, STK_BASE);
                                        s390_aghi (code, s390_r13, alloc_size);
                                }
-                               switch (ainfo->size) {
+
+                               size = (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE  
+                                       ? mono_class_native_size(mono_class_from_mono_type(inst->inst_vtype), NULL)
+                                       : ainfo->size);
+
+                               switch (size) {
                                        case 1:
                                                if (ainfo->reg == STK_BASE)
                                                        s390_ic (code, reg, 0, s390_r13, ainfo->offset+7);
@@ -4450,6 +5056,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 ();
                }
@@ -4474,13 +5081,13 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                /*---------------------------------------------------------------*/
                /* On return from this call r2 have the address of the &lmf      */
                /*---------------------------------------------------------------*/
-               s390_basr(code, s390_r10, 0);
+               s390_basr(code, s390_r14, 0);
                s390_j   (code, 6);
                mono_add_patch_info (cfg, code - cfg->native_code, 
                                     MONO_PATCH_INFO_INTERNAL_METHOD, 
                                     (gpointer)"mono_get_lmf_addr");
                s390_llong(code, 0);
-               s390_lg   (code, s390_r1, 0, s390_r10, 4);
+               s390_lg   (code, s390_r1, 0, s390_r14, 4);
                s390_basr (code, s390_r14, s390_r1);
 
                /*---------------------------------------------------------------*/     
@@ -4685,7 +5292,8 @@ mono_arch_emit_exceptions (MonoCompile *cfg)
                                /*---------------------------------------------*/
                                s390_basr (code, s390_r13, 0);
                                s390_j    (code, 6);
-                               s390_llong(code, patch_info->data.target);
+//                             s390_llong(code, patch_info->data.target);
+                               s390_llong(code, exc_class->type_token);
                                /*---------------------------------------------*/
                                /* Load return address & parameter register    */
                                /*---------------------------------------------*/
@@ -4698,7 +5306,7 @@ mono_arch_emit_exceptions (MonoCompile *cfg)
                                s390_basr (code, s390_r13, 0);
                                s390_j    (code, 6);
                                patch_info->type      = MONO_PATCH_INFO_INTERNAL_METHOD;
-                               patch_info->data.name = "mono_arch_throw_exception_by_name";
+                               patch_info->data.name = "mono_arch_throw_corlib_exception";
                                patch_info->ip.i      = code - cfg->native_code;
                                s390_llong(code, 0);
                                s390_lg   (code, s390_r1, 0, s390_r13, 4);
@@ -4775,78 +5383,67 @@ 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                   */
+/* Name                - mono_arch_decompose_opts                          */
 /*                                                                  */
-/* Function    - Check for opcodes we can handle directly in       */
-/*               hardware.                                         */
+/* Function    - Decompose opcode into a System z opcode.          */
 /*                                                                 */
 /*------------------------------------------------------------------*/
 
-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 ========================*/
@@ -4959,31 +5556,6 @@ mono_arch_get_domain_intrinsic (MonoCompile* cfg)
 
 /*========================= End of Function ========================*/
 
-/*------------------------------------------------------------------*/
-/*                                                                  */
-/* Name                - mono_arch_get_thread_intrinsic                    */
-/*                                                                  */
-/* Function    -                                                   */
-/*                                                                 */
-/* Returns     -                                                   */
-/*                                                                  */
-/*------------------------------------------------------------------*/
-
-MonoInst * 
-mono_arch_get_thread_intrinsic (MonoCompile* cfg)
-{
-       MonoInst *ins;
-
-       if (thread_tls_offset == -1)
-               return NULL;
-       
-       MONO_INST_NEW (cfg, ins, OP_TLS_GET);
-       ins->inst_offset = thread_tls_offset;
-       return (ins);
-}
-
-/*========================= End of Function ========================*/
-
 /*------------------------------------------------------------------*/
 /*                                                                  */
 /* Name                - mono_arch_flush_register_windows                  */
@@ -5038,8 +5610,6 @@ mono_arch_is_inst_imm (gint64 imm)
 
 /*========================= End of Function ========================*/
 
-/*========================= End of Function ========================*/
-
 /*------------------------------------------------------------------*/
 /*                                                                  */
 /* Name                - mono_arch_get_patch_offset                        */
@@ -5058,6 +5628,16 @@ mono_arch_get_patch_offset (guint8 *code)
 
 /*========================= End of Function ========================*/
 
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_context_get_int_reg.                    */
+/*                                                                  */
+/* Function    -                                                   */
+/*                                                                 */
+/* Returns     - Offset for patch.                                 */
+/*                                                                  */
+/*------------------------------------------------------------------*/
+
 gpointer
 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
 {
@@ -5065,3 +5645,348 @@ mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
        g_assert_not_reached ();
        return NULL;
 }
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_get_this_arg_from_call.                 */
+/*                                                                  */
+/* Function    -                                                   */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+gpointer
+mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
+{
+       MonoLMF *lmf = (MonoLMF *) ((gchar *) regs - sizeof(MonoLMF));
+
+       return (gpointer) lmf->gregs [s390_r2];
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - get_delegate_invoke_impl.                         */
+/*                                                                  */
+/* Function    -                                                   */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+static gpointer
+get_delegate_invoke_impl (gboolean has_target, guint32 param_count, guint32 *code_len, gboolean aot)
+{
+       guint8 *code, *start;
+
+       if (has_target) {
+               int size = 32;
+
+               start = code = mono_global_codeman_reserve (size);
+
+               /* Replace the this argument with the target */
+               s390_lg   (code, s390_r1, 0, s390_r2, G_STRUCT_OFFSET(MonoDelegate, method_ptr));
+               s390_lg   (code, s390_r2, 0, s390_r2, G_STRUCT_OFFSET(MonoDelegate, target));
+               s390_br   (code, s390_r1);
+               g_assert ((code - start) <= size);
+
+               mono_arch_flush_icache (start, size);
+       } else {
+               int size, i;
+
+               size = 32 + param_count * 8;
+               start = code = mono_global_codeman_reserve (size);
+
+               s390_lg   (code, s390_r1, 0, s390_r2, G_STRUCT_OFFSET(MonoDelegate, method_ptr));
+               /* slide down the arguments */
+               for (i = 0; i < param_count; ++i) {
+                       s390_lgr (code, (s390_r2 + i), (s390_r2 + i + 1));
+               }
+               s390_br   (code, s390_r1);
+
+               g_assert ((code - start) <= size);
+
+               mono_arch_flush_icache (start, size);
+       }
+
+       if (code_len)
+               *code_len = code - start;
+
+       return start;
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_get_delegate_invoke_impls.              */
+/*                                                                  */
+/* Function    -                                                   */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+GSList*
+mono_arch_get_delegate_invoke_impls (void)
+{
+       GSList *res = NULL;
+       guint8 *code;
+       guint32 code_len;
+       int i;
+
+       code = get_delegate_invoke_impl (TRUE, 0, &code_len, TRUE);
+       res = g_slist_prepend (res, mono_tramp_info_create (g_strdup ("delegate_invoke_impl_has_target"), code, code_len, NULL, NULL));
+
+       for (i = 0; i < MAX_ARCH_DELEGATE_PARAMS; ++i) {
+               code = get_delegate_invoke_impl (FALSE, i, &code_len, TRUE);
+               res = g_slist_prepend (res, mono_tramp_info_create (g_strdup_printf ("delegate_invoke_impl_target_%d", i), code, code_len, NULL, NULL));
+       }
+
+       return res;
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_get_delegate_invoke_impl.               */
+/*                                                                  */
+/* Function    -                                                   */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+gpointer
+mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
+{
+       guint8 *code, *start;
+
+       /* FIXME: Support more cases */
+       if (MONO_TYPE_ISSTRUCT (sig->ret))
+               return NULL;
+
+       if (has_target) {
+               static guint8* cached = NULL;
+
+               if (cached)
+                       return cached;
+
+               if (mono_aot_only)
+                       start = mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
+               else
+                       start = get_delegate_invoke_impl (TRUE, 0, NULL, FALSE);
+
+               mono_memory_barrier ();
+
+               cached = start;
+       } else {
+               static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
+               int i;
+
+               if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
+                       return NULL;
+               for (i = 0; i < sig->param_count; ++i)
+                       if (!mono_is_regsize_var (sig->params [i]))
+                               return NULL;
+
+
+               code = cache [sig->param_count];
+               if (code)
+                       return code;
+
+               if (mono_aot_only) {
+                       char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
+                       start = mono_aot_get_trampoline (name);
+                       g_free (name);
+               } else {
+                       start = get_delegate_invoke_impl (FALSE, sig->param_count, NULL, FALSE);
+               }
+
+               mono_memory_barrier ();
+
+               cache [sig->param_count] = start;
+       }
+       return start;
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_build_imt_thunk.                        */
+/*                                                                  */
+/* Function    -                                                   */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+gpointer
+mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, 
+                          MonoIMTCheckItem **imt_entries, int count,
+                          gpointer fail_tramp)
+{
+       int i;
+       int size = 0;
+       guchar *code, *start;
+
+       for (i = 0; i < count; ++i) {
+               MonoIMTCheckItem *item = imt_entries [i];
+               if (item->is_equals) {
+                       if (item->check_target_idx) {
+                               if (!item->compare_done)
+                                       item->chunk_size += CMP_SIZE + JUMP_SIZE;
+                               if (item->has_target_code)
+                                       item->chunk_size += BR_SIZE + JUMP_SIZE + LOADCON_SIZE;
+                               else
+                                       item->chunk_size += BR_SIZE + JUMP_SIZE + LOADCON_SIZE + 
+                                                           LOAD_SIZE;
+                       } else {
+                               if (fail_tramp) {
+                                       item->chunk_size += CMP_SIZE + 2 * BR_SIZE + JUMP_SIZE + 
+                                                           2 * LOADCON_SIZE;
+                                       if (!item->has_target_code)
+                                               item->chunk_size += LOAD_SIZE;
+                               } else {
+                                       item->chunk_size += LOADCON_SIZE + LOAD_SIZE + BR_SIZE;
+#if ENABLE_WRONG_METHOD_CHECK
+                                       item->chunk_size += CMP_SIZE + JUMP_SIZE;
+#endif
+                               }
+                       }
+               } else {
+                       item->chunk_size += CMP_SIZE + JUMP_SIZE;
+                       imt_entries [item->check_target_idx]->compare_done = TRUE;
+               }
+               size += item->chunk_size;
+       }
+
+       if (fail_tramp)
+               code = mono_method_alloc_generic_virtual_thunk (domain, size);
+       else
+               code = mono_domain_code_reserve (domain, size);
+
+       start = code;
+
+       for (i = 0; i < count; ++i) {
+               MonoIMTCheckItem *item = imt_entries [i];
+               item->code_target = (guint8 *) code;
+               if (item->is_equals) {
+                       if (item->check_target_idx) {
+                               if (!item->compare_done) {
+                                       s390_basr (code, s390_r13, s390_r0);
+                                       s390_j    (code, 6);
+                                       s390_llong(code, item->key);
+                                       s390_lg   (code, s390_r0, 0, s390_r13, 4);
+                                       s390_cgr  (code, s390_r0, MONO_ARCH_IMT_REG);
+                               }
+                               item->jmp_code = (guint8*) code;
+                               s390_jcl (code, S390_CC_NE, 0);
+                               
+                               s390_basr (code, s390_r13, s390_r0);
+                               s390_j    (code, 6);
+                               if (item->has_target_code)  {
+                                       s390_llong(code, item->value.target_code);
+                                       s390_lg   (code, s390_r1, 0, s390_r13, 4);
+                               } else {        
+                                       s390_llong(code, (&(vtable->vtable [item->value.vtable_slot])));
+                                       s390_lg   (code, s390_r1, 0, s390_r13, 4);
+                                       s390_lg   (code, s390_r1, 0, s390_r1, 0);
+                               }
+                               s390_br   (code, s390_r1);
+                       } else {
+                               if (fail_tramp) {
+                                       gint64  target;
+
+                                       s390_basr (code, s390_r13, s390_r0);
+                                       s390_j    (code, 6);
+                                       s390_llong(code, item->key);
+                                       s390_lg   (code, s390_r0, 0, s390_r13, 4);
+                                       s390_cgr  (code, s390_r0, MONO_ARCH_IMT_REG);
+                                       item->jmp_code = (guint8*) code;
+                                       s390_jcl  (code, S390_CC_NE, 0);
+                                       s390_basr (code, s390_r13, s390_r0);
+                                       s390_j    (code, 6);
+                                       if (item->has_target_code) {
+                                               s390_llong(code, item->value.target_code);
+                                               s390_lg   (code, s390_r1, 0, s390_r13, 4);
+                                       } else {
+                                               g_assert (vtable);
+                                               s390_llong(code, (&(vtable->vtable [item->value.vtable_slot])));
+                                               s390_lg   (code, s390_r1, 0, s390_r13, 4);
+                                               s390_lg   (code, s390_r1, 0, s390_r1, 0);
+                                       }
+                                       s390_br   (code, s390_r1);
+                                       target = S390_RELATIVE(code, item->jmp_code);
+                                       s390_patch_rel(item->jmp_code+2, target);
+                                       s390_basr (code, s390_r13, s390_r0);
+                                       s390_j    (code, 6);
+                                       s390_llong(code, fail_tramp);
+                                       s390_lg   (code, s390_r1, 0, s390_r13, 4);
+                                       s390_br   (code, s390_r1);
+                                       item->jmp_code = NULL;
+                               } else {
+                               /* enable the commented code to assert on wrong method */
+#if ENABLE_WRONG_METHOD_CHECK
+                                       g_assert_not_reached ();
+#endif
+                                       s390_basr (code, s390_r13, s390_r0);
+                                       s390_j    (code, 6);
+                                       s390_llong(code, (&(vtable->vtable [item->value.vtable_slot])));
+                                       s390_lg   (code, s390_r1, 0, s390_r13, 4);
+                                       s390_lg   (code, s390_r1, 0, s390_r1, 0);
+                                       s390_br   (code, s390_r1);
+#if ENABLE_WRONG_METHOD_CHECK
+                                       g_assert_not_reached ();
+#endif
+                               }
+                       }
+               } else {
+                       s390_basr (code, s390_r13, s390_r0);
+                       s390_j    (code, 6);
+                       s390_llong(code, item->key);
+                       s390_lg   (code, s390_r0, 0, s390_r13, 4);
+                       s390_cgr  (code, MONO_ARCH_IMT_REG, s390_r0);
+                       item->jmp_code = (guint8 *) code;
+                       s390_jcl  (code, S390_CC_GE, 0);
+               }
+       }
+       /* patch the branches to get to the target items */
+       for (i = 0; i < count; ++i) {
+               MonoIMTCheckItem *item = imt_entries [i];
+               if (item->jmp_code) {
+                       if (item->check_target_idx) {
+                               gint64 offset;
+                               offset = S390_RELATIVE(imt_entries [item->check_target_idx]->code_target,
+                                                      item->jmp_code);
+                               s390_patch_rel ((guchar *) item->jmp_code + 2, (guint64) offset);
+                       }
+               }
+       }
+
+       mono_arch_flush_icache ((guint8*)start, (code - start));
+
+       if (!fail_tramp)
+               mono_stats.imt_thunks_size += (code - start);
+
+       g_assert (code - start <= size);
+
+       return (start);
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_find_imt_method.                        */
+/*                                                                  */
+/* Function    - Get the method address from MONO_ARCH_IMT_REG     */
+/*               found in the save area.                           */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+MonoMethod*
+mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
+{
+       MonoLMF *lmf = (MonoLMF *) ((gchar *) regs - sizeof(MonoLMF));
+
+       return ((MonoMethod *) lmf->gregs [MONO_ARCH_IMT_REG]);
+}
+
+/*========================= End of Function ========================*/