Introduce MonoThreadUnwindState and a bunch overloads to mono_walk_stack.
[mono.git] / mono / mini / mini-s390x.c
index 6fabd3d48f67065afb08522728ccf7d05c2e7899..c94b7b74e7d62e84384cd579b744c7d431604045 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)                      \
@@ -158,23 +128,19 @@ if (ins->flags & MONO_INST_BRLABEL) {                                                     \
 
 #define MONO_EMIT_NEW_MOVE(cfg,dest,offset,src,imm,size) do {                  \
                 MonoInst *inst;                                                \
-               int tmpr = 0;                                                   \
                int sReg, dReg;                                                 \
-               MONO_INST_NEW (cfg, inst, OP_NOP);                                                              \
+               MONO_INST_NEW (cfg, inst, OP_NOP);                              \
                if (size > 256) {                                               \
-                       tmpr = mono_alloc_preg (cfg); \
-                       MONO_EMIT_NEW_ICONST(cfg,tmpr,size);                    \
                        inst->dreg        = dest;                               \
                        inst->inst_offset = offset;                             \
                        inst->sreg1       = src;                                \
                        inst->inst_imm    = imm;                                \
-                       inst->sreg2       = tmpr;                               \
                } else {                                                        \
                        if (s390_is_uimm12(offset)) {                           \
                                inst->dreg        = dest;                       \
                                inst->inst_offset = offset;                     \
                        } else {                                                \
-                               dReg = mono_alloc_preg (cfg); \
+                               dReg = mono_alloc_preg (cfg);                   \
                                MONO_EMIT_NEW_BIALU_IMM(cfg, OP_ADD_IMM,        \
                                        dReg, dest, offset);                    \
                                inst->dreg        = dReg;                       \
@@ -184,16 +150,16 @@ if (ins->flags & MONO_INST_BRLABEL) {                                                     \
                                inst->sreg1       = src;                        \
                                inst->inst_imm    = imm;                        \
                        } else {                                                \
-                               sReg = mono_alloc_preg (cfg); \
+                               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); \
+                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 {                            \
@@ -219,7 +185,7 @@ if (ins->flags & MONO_INST_BRLABEL) {                                                       \
                                reg, sr, so);                                   \
                break;                                                          \
        }                                                                       \
-       mono_call_inst_add_outarg_reg(cfg, call, reg, dr, FALSE);       \
+       mono_call_inst_add_outarg_reg(cfg, call, reg, dr, FALSE);               \
 } while (0)
 
 #define MONO_OUTPUT_VTS(cfg, size, dr, dx, sr, so) do {                                \
@@ -239,14 +205,14 @@ if (ins->flags & MONO_INST_BRLABEL) {                                                     \
                                dr, dx, tmpr);                                  \
                break;                                                          \
                case 2:                                                         \
-                       tmpr = mono_alloc_preg (cfg); \
+                       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);   \
+                       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,  \
@@ -267,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 =========================*/
 
 /*------------------------------------------------------------------*/
@@ -330,7 +306,8 @@ typedef enum {
        RegTypeFPR4,
        RegTypeStructByVal,
        RegTypeStructByValInFP,
-       RegTypeStructByAddr
+       RegTypeStructByAddr,
+       RegTypeStructByAddrOnStack
 } ArgStorage;
 
 typedef struct {
@@ -351,6 +328,7 @@ typedef struct {
        ArgInfo ret;
        ArgInfo sigCookie;
        size_data sz;
+       int vret_arg_index;
        ArgInfo args [1];
 } CallInfo;
 
@@ -379,7 +357,7 @@ static guchar * emit_float_to_int (MonoCompile *, guchar *, int, int, int, gbool
 gpointer mono_arch_get_lmf_addr (void);
 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);
+static __inline__ void emit_unwind_regs(MonoCompile *, guint8 *, int, int, long);
 
 /*========================= End of Prototypes ======================*/
 
@@ -402,6 +380,8 @@ pthread_key_t lmf_addr_key;
 
 gboolean lmf_addr_key_inited = FALSE; 
 
+facilityList_t facs;
+
 #if 0
 
 extern __thread MonoDomain *tls_appdomain;
@@ -506,7 +486,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);
 
@@ -529,6 +509,28 @@ mono_arch_get_argument_info (MonoMethodSignature *csig,
 
 /*========================= End of Function ========================*/
 
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - emit_unwind_regs.                                 */
+/*                                                                  */
+/* Function    - Determines if a value can be returned in one or   */
+/*                two registers.                                    */
+/*                                                                  */
+/*------------------------------------------------------------------*/
+
+static void __inline__
+emit_unwind_regs(MonoCompile *cfg, guint8 *code, int start, int end, long offset)
+{
+       int i;
+
+       for (i = start; i < end; i++) {
+               mono_emit_unwind_op_offset (cfg, code, i, offset);
+               offset += sizeof(gulong);
+       }
+}
+
+/*========================= End of Function ========================*/
+
 /*------------------------------------------------------------------*/
 /*                                                                  */
 /* Name                - retFitsInReg.                                     */
@@ -569,6 +571,9 @@ backUpStackPtr(MonoCompile *cfg, guint8 *code)
 {
        int stackSize = cfg->stack_usage;
 
+       if (cfg->frame_reg != STK_BASE)
+               s390_lgr (code, STK_BASE, cfg->frame_reg);
+
        if (s390_is_imm16 (stackSize)) {
                s390_aghi  (code, STK_BASE, stackSize);
        } else { 
@@ -718,7 +723,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;
                                }
@@ -1022,11 +1027,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);
 
@@ -1048,17 +1052,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;
@@ -1156,23 +1173,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                       */
@@ -1226,7 +1271,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;
@@ -1288,7 +1333,7 @@ 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;
 
@@ -1366,18 +1411,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;
 }
@@ -1417,7 +1463,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  */
@@ -1429,7 +1475,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;
@@ -1506,7 +1552,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)
@@ -1514,20 +1560,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;
@@ -1535,10 +1577,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);
        }
 
        /*----------------------------------------------------------*/
@@ -1547,7 +1629,7 @@ enum_retvalue:
        /* parameters.                                              */
        /*----------------------------------------------------------*/
 
-       for (i = 0; i < sig->param_count; ++i) {
+       for (i = pstart; i < sig->param_count; ++i) {
                MonoType *ptype;
 
                /*--------------------------------------------------*/
@@ -1721,6 +1803,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);
@@ -1774,11 +1858,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;
 
@@ -1823,7 +1907,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) {
@@ -1867,24 +1955,45 @@ mono_arch_allocate_vars (MonoCompile *cfg)
 
                                size = sizeof (gpointer);
 
-                               inst->opcode = OP_REGOFFSET;
+                               inst->opcode       = OP_REGOFFSET;
                                inst->inst_basereg = frame_reg;
-                               offset = S390_ALIGN (offset, sizeof (gpointer));
-                               inst->inst_offset = offset;
+                               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->opcode       = OP_REGOFFSET;
                                inst->inst_basereg = frame_reg;
-                               inst->inst_offset = offset;
+                               inst->inst_offset  = offset;
                                break;
                        default :
                                if (cinfo->args [iParm].reg == STK_BASE) {
@@ -1911,7 +2020,10 @@ mono_arch_allocate_vars (MonoCompile *cfg)
                                                                          ? sizeof(int)  
                                                                          : sizeof(long));
                                        offset             = S390_ALIGN(offset, size);
-                                       inst->inst_offset  = offset;
+                                       if (cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) 
+                                               inst->inst_offset  = offset;
+                                       else
+                                               inst->inst_offset  = offset + (8 - size);
                                }
                                break;
                        }
@@ -1920,7 +2032,7 @@ mono_arch_allocate_vars (MonoCompile *cfg)
                            (iParm < sig->sentinelpos)) 
                                cfg->sig_cookie += size;
 
-                       offset += size;
+                       offset += MAX(size, 8);
                }
                curinst++;
        }
@@ -1933,12 +2045,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);
 
@@ -1968,13 +2081,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
@@ -1998,6 +2123,12 @@ mono_arch_create_vars (MonoCompile *cfg)
 
 /*========================= End of Function ========================*/
 
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - add_outarg_reg2.                                  */
+/*                                                                  */
+/*------------------------------------------------------------------*/
+
 static void
 add_outarg_reg2 (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int reg, MonoInst *tree)
 {
@@ -2030,6 +2161,14 @@ add_outarg_reg2 (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int r
        }
 }
 
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - emit_sig_cookie.                                  */
+/*                                                                  */
+/*------------------------------------------------------------------*/
+
 static void
 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
 {
@@ -2058,12 +2197,14 @@ emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
        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);
+                                    cinfo->sigCookie.offset, sig_arg->dreg);
 }
 
+/*========================= End of Function ========================*/
+
 /*------------------------------------------------------------------*/
 /*                                                                  */
-/* Name                - mono_arch_emit_call                                   */
+/* Name                - mono_arch_emit_call                               */
 /*                                                                  */
 /*------------------------------------------------------------------*/
 
@@ -2076,7 +2217,9 @@ mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
        int i, n, lParamArea;
        CallInfo *cinfo;
        ArgInfo *ainfo = NULL;
-       int stackSize;
+       int stackSize;    
+       MonoMethodHeader *header;
+       int frmReg;
 
        sig = call->signature;
        n = sig->param_count + sig->hasthis;
@@ -2084,7 +2227,8 @@ mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
        
        cinfo = get_call_info (cfg, cfg->mempool, sig, sig->pinvoke);
 
-       stackSize         = cinfo->sz.stack_size + cinfo->sz.local_size + cinfo->sz.parm_size + cinfo->sz.offset;
+       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);
@@ -2098,10 +2242,16 @@ mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
                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) {
-               ainfo = cinfo->args + i;
                MonoType *t;
 
+               ainfo = cinfo->args + i;
                if (i >= sig->hasthis)
                        t = sig->params [i - sig->hasthis];
                else
@@ -2111,6 +2261,7 @@ mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
                in = call->args [i];
 
                if ((sig->call_convention == MONO_CALL_VARARG) &&
+                   (!sig->pinvoke) &&
                    (i == sig->sentinelpos)) {
                        emit_sig_cookie (cfg, call, cinfo);
                }
@@ -2131,7 +2282,8 @@ mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
                                break;
                        }
                case RegTypeStructByVal:
-               case RegTypeStructByAddr: {
+               case RegTypeStructByAddr:
+               case RegTypeStructByAddrOnStack: {
                        guint32 align;
                        guint32 size;
 
@@ -2174,26 +2326,34 @@ mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
                                 */
                                int treg = mono_alloc_preg (cfg);
                                MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, treg, 
-                                                                                STK_BASE, ainfo->offparm);
+                                                        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);
+                                                            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);
+                                                            STK_BASE, ainfo->offset,
+                                                            in->dreg);
                        } else {
                                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)) {
@@ -2211,6 +2371,7 @@ mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
                                                break;
                                        }
                                }
+#endif
 
                                MONO_ADD_INS (cfg->cbb, ins);
                        }
@@ -2225,6 +2386,7 @@ mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
         * Handle the case where there are no implicit arguments 
         */
        if ((sig->call_convention == MONO_CALL_VARARG) &&
+           (!sig->pinvoke) &&
            (i == sig->sentinelpos)) {
                emit_sig_cookie (cfg, call, cinfo);
        }
@@ -2234,7 +2396,7 @@ mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
 
 /*------------------------------------------------------------------*/
 /*                                                                  */
-/* Name                - mono_arch_emit_outarg_vt                              */
+/* Name                - mono_arch_emit_outarg_vt                          */
 /*                                                                  */
 /*------------------------------------------------------------------*/
 
@@ -2273,14 +2435,25 @@ mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
 
                mono_call_inst_add_outarg_reg (cfg, call, dreg, ainfo->reg, TRUE);
        } else {
-               MONO_EMIT_NEW_MOVE (cfg, STK_BASE, ainfo->offparm,
+               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                                 */
+/* Name                - mono_arch_emit_setret                             */
 /*                                                                  */
 /*------------------------------------------------------------------*/
 
@@ -2410,7 +2583,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,
@@ -2443,7 +2616,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;
@@ -2510,14 +2683,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.                             */
@@ -2536,6 +2720,12 @@ 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)
 {
@@ -2584,7 +2774,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);
@@ -2600,8 +2790,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]);
@@ -2627,7 +2817,17 @@ emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size,
 
 /*========================= End of Function ========================*/
 
-static gboolean is_unsigned (MonoInst *next)
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* 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) &&
@@ -2649,6 +2849,8 @@ static gboolean is_unsigned (MonoInst *next)
                return FALSE;
 }
 
+/*========================= End of Function ========================*/
+
 /*------------------------------------------------------------------*/
 /*                                                                  */
 /* Name                - mono_arch_output_basic_block                      */
@@ -2800,89 +3002,90 @@ 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: {
-                       s390_lhi  (code, s390_r0, 0x80);
-                       if (ins->dreg != ins->sreg1) {
-                               s390_lr   (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_nr   (code, s390_r0, ins->sreg1);
-                       s390_jz   (code, 7);
-                       s390_lhi  (code, s390_r13, -1);
-                       s390_sll  (code, s390_r13, 0, 8);
-                       s390_or   (code, ins->dreg, s390_r13);
+#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: {
-                       s390_lhi  (code, s390_r0, 0x80);
-                       s390_sll  (code, s390_r0, 0, 8);
-                       if (ins->dreg != ins->sreg1) {
-                               s390_lr   (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_nr   (code, s390_r0, ins->sreg1);
-                       s390_jz   (code, 7);
-                       s390_lhi  (code, s390_r13, -1);
-                       s390_sll  (code, s390_r13, 0, 16);
-                       s390_or   (code, ins->dreg, s390_r13);
+#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);
-                       if (ins->dreg != ins->sreg1) {
-                               s390_lr   (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_nr   (code, ins->dreg, s390_r0);
+                       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_sll  (code, s390_r0, 0, 16);
                        s390_srl  (code, s390_r0, 0, 16);
-                       if (ins->dreg != ins->sreg1) {
-                               s390_lr   (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_nr   (code, ins->dreg, s390_r0);
+                       s390_nr   (code, ins->dreg, s390_r0);
+#endif
                }
                        break;
                case OP_COMPARE: 
@@ -3758,7 +3961,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        }
 
                        code = backUpStackPtr(cfg, code);
-                       s390_lg  (code, s390_r14, 0, STK_BASE, S390_RET_ADDR_OFFSET);
+                       s390_lg  (code, s390_r14, 0, cfg->frame_reg, S390_RET_ADDR_OFFSET);
                        mono_add_patch_info (cfg, code - cfg->native_code,
                                             MONO_PATCH_INFO_METHOD_JUMP,
                                             ins->inst_p0);
@@ -3861,8 +4064,15 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                }
                        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);
@@ -3931,7 +4141,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_START_HANDLER: {
                        MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
 
-                       S390_LONG (code, stg, stg, s390_r14, 0, 
+                       S390_LONG (code, stg, stg, s390_r14, 0,
                                   spvar->inst_basereg, 
                                   spvar->inst_offset);
                }
@@ -3941,7 +4151,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 
                        if (ins->sreg1 != s390_r2)
                                s390_lgr(code, s390_r2, ins->sreg1);
-                       S390_LONG (code, lg, lg, s390_r14, 0, 
+                       S390_LONG (code, lg, lg, s390_r14, 0,
                                   spvar->inst_basereg, 
                                   spvar->inst_offset);
                        s390_br  (code, s390_r14);
@@ -3950,7 +4160,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_ENDFINALLY: {
                        MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
 
-                       S390_LONG (code, lg, lg, s390_r14, 0, 
+                       S390_LONG (code, lg, lg, s390_r14, 0,
                                   spvar->inst_basereg, 
                                   spvar->inst_offset);
                        s390_br  (code, s390_r14);
@@ -3960,6 +4170,7 @@ 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: {
@@ -4372,24 +4583,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);
@@ -4441,7 +4663,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);
@@ -4511,7 +4733,7 @@ mono_arch_register_lowlevel_calls (void)
 
 void
 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, 
-                     guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
+                     guint8 *code, MonoJumpInfo *ji, MonoCodeManager *dyn_code_mp, gboolean run_cctors)
 {
        MonoJumpInfo *patch_info;
 
@@ -4690,8 +4912,9 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        MonoBasicBlock *bb;
        MonoMethodSignature *sig;
        MonoInst *inst;
-       int alloc_size, pos, max_offset, i;
+       long alloc_size, pos, max_offset, i, cfa_offset = 0;
        guint8 *code;
+       guint32 size;
        CallInfo *cinfo;
        int tracing = 0;
        int lmfOffset;
@@ -4708,7 +4931,10 @@ mono_arch_emit_prolog (MonoCompile *cfg)
 
        cfg->native_code = code = g_malloc (cfg->code_size);
 
+       mono_emit_unwind_op_def_cfa (cfg, code, STK_BASE, 0);
+       emit_unwind_regs(cfg, code, s390_r6, s390_r14, S390_REG_SAVE_OFFSET);
        s390_stmg (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
+       mono_emit_unwind_op_offset (cfg, code, s390_r14, S390_RET_ADDR_OFFSET);
 
        if (cfg->arch.bkchain_reg != -1)
                s390_lgr (code, cfg->arch.bkchain_reg, STK_BASE);
@@ -4719,7 +4945,8 @@ mono_arch_emit_prolog (MonoCompile *cfg)
 
        alloc_size = cfg->stack_offset;
 
-       cfg->stack_usage = alloc_size;
+       cfg->stack_usage = cfa_offset = alloc_size;
+       mono_emit_unwind_op_def_cfa_offset (cfg, code, alloc_size);
        s390_lgr  (code, s390_r11, STK_BASE);
        if (s390_is_imm16 (alloc_size)) {
                s390_aghi (code, STK_BASE, -alloc_size);
@@ -4736,6 +4963,8 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        if (cfg->frame_reg != STK_BASE)
                s390_lgr (code, s390_r11, STK_BASE);
 
+       mono_emit_unwind_op_def_cfa_reg (cfg, code, cfg->frame_reg);
+
         /* compute max_offset in order to use short forward jumps
         * we always do it on s390 because the immediate displacement
         * for jumps is too small 
@@ -4759,8 +4988,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);
        }
@@ -4830,7 +5059,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);
@@ -4854,6 +5088,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 ();
                }
@@ -4878,13 +5113,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);
 
                /*---------------------------------------------------------------*/     
@@ -4995,9 +5230,11 @@ mono_arch_emit_epilog (MonoCompile *cfg)
        if (method->save_lmf) 
                restoreLMF(code, cfg->frame_reg, cfg->stack_usage);
 
-       if (cfg->flags & MONO_CFG_HAS_ALLOCA) 
+       if (cfg->flags & MONO_CFG_HAS_ALLOCA) {
+//             if (cfg->frame_reg != STK_BASE)
+//                     s390_lgr (code, STK_BASE, cfg->frame_reg);
                s390_lg  (code, STK_BASE, 0, STK_BASE, 0);
-       else
+       else
                code = backUpStackPtr(cfg, code);
 
        s390_lmg (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
@@ -5089,7 +5326,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    */
                                /*---------------------------------------------*/
@@ -5102,7 +5340,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);
@@ -5197,6 +5435,14 @@ mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMetho
 
 /*========================= End of Function ========================*/
 
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_decompose_opts                          */
+/*                                                                  */
+/* Function    - Decompose opcode into a System z opcode.          */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
 void
 mono_arch_decompose_opts (MonoCompile *cfg, MonoInst *ins)
 {
@@ -5344,31 +5590,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                  */
@@ -5441,6 +5662,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)
 {
@@ -5448,3 +5679,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 ========================*/