Merge pull request #444 from knocte/xbuild_improvements
[mono.git] / mono / mini / mini-s390x.c
index 4336365f4f555fec778c724a38b1fa1dac00394f..f37b087e49d4f7d356256acf358ea642f768c88e 100644 (file)
 /*                 D e f i n e s                                    */
 /*------------------------------------------------------------------*/
 
-#define EMIT_COND_BRANCH(ins,cond)                                                     \
-{                                                                                      \
+#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 +                                         \
@@ -36,8 +38,8 @@ if (ins->inst_true_bb->native_offset) {                                       \
 }                                                                              \
 }
 
-#define EMIT_UNCOND_BRANCH(ins)                                                        \
-{                                                                                      \
+#define EMIT_UNCOND_BRANCH(ins)                                                \
+{                                                                              \
 if (ins->inst_target_bb->native_offset) {                                      \
        int displace;                                                           \
        displace = ((cfg->native_code +                                         \
@@ -126,23 +128,19 @@ if (ins->inst_target_bb->native_offset) {                                         \
 
 #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;                       \
@@ -152,16 +150,16 @@ if (ins->inst_target_bb->native_offset) {                                         \
                                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 {                            \
@@ -187,7 +185,7 @@ if (ins->inst_target_bb->native_offset) {                                   \
                                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 {                                \
@@ -207,14 +205,14 @@ if (ins->inst_target_bb->native_offset) {                                         \
                                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,  \
@@ -233,8 +231,21 @@ if (ins->inst_target_bb->native_offset) {                                  \
 
 #define S390_TRACE_STACK_SIZE (5*sizeof(gpointer)+4*sizeof(gdouble))
 
+#define BREAKPOINT_SIZE                sizeof(breakpoint_t)
+#define S390X_NOP_SIZE         sizeof(I_Format)
+
 #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 =========================*/
 
 /*------------------------------------------------------------------*/
@@ -248,11 +259,13 @@ if (ins->inst_target_bb->native_offset) {                                         \
 #include <mono/metadata/debug-helpers.h>
 #include <mono/metadata/profiler-private.h>
 #include <mono/utils/mono-math.h>
+#include <mono/utils/mono-mmap.h>
 
 #include "mini-s390x.h"
 #include "cpu-s390x.h"
 #include "jit-icalls.h"
 #include "ir-emit.h"
+#include "trace.h"
 
 /*========================= End of Includes ========================*/
 
@@ -320,6 +333,7 @@ typedef struct {
        ArgInfo ret;
        ArgInfo sigCookie;
        size_data sz;
+       int vret_arg_index;
        ArgInfo args [1];
 } CallInfo;
 
@@ -328,6 +342,14 @@ typedef struct {
        gdouble fp[3];          /* F0-F2                            */
 } __attribute__ ((packed)) RegParm;
 
+typedef struct {
+       RR_Format  basr;
+       RI_Format  j;
+       void       *pTrigger;
+       RXY_Format lg;
+       RXY_Format trigger;
+} __attribute__ ((packed)) breakpoint_t;
+
 /*========================= End of Typedefs ========================*/
 
 /*------------------------------------------------------------------*/
@@ -345,9 +367,9 @@ static inline void add_stackParm (guint *, size_data *, ArgInfo *, gint);
 static inline void add_float (guint *, size_data *, ArgInfo *);
 static CallInfo * get_call_info (MonoCompile *, MonoMemPool *, MonoMethodSignature *, gboolean);
 static guchar * emit_float_to_int (MonoCompile *, guchar *, int, int, int, gboolean);
-gpointer mono_arch_get_lmf_addr (void);
 static guint8 * emit_load_volatile_arguments (guint8 *, MonoCompile *);
 static void catch_SIGILL(int, siginfo_t *, void *);
+static __inline__ void emit_unwind_regs(MonoCompile *, guint8 *, int, int, long);
 
 /*========================= End of Prototypes ======================*/
 
@@ -361,15 +383,16 @@ static int indent_level = 0;
 
 int has_ld = 0;
 
-static gboolean tls_offset_inited = FALSE;
-
-static int appdomain_tls_offset = -1,
-           thread_tls_offset = -1;
+static gint appdomain_tls_offset = -1,
+           lmf_tls_offset = -1,
+           lmf_addr_tls_offset = -1;
 
 pthread_key_t lmf_addr_key;
 
 gboolean lmf_addr_key_inited = FALSE; 
 
+facilityList_t facs;
+
 #if 0
 
 extern __thread MonoDomain *tls_appdomain;
@@ -378,6 +401,19 @@ extern __thread gpointer   mono_lmf_addr;
                
 #endif
 
+/*
+ * The code generated for sequence points reads from this location, 
+ * which is made read-only when single stepping is enabled.
+ */
+static gpointer ss_trigger_page;
+
+/*
+ * Enabled breakpoints read from this trigger page
+ */
+static gpointer bp_trigger_page;
+
+breakpoint_t breakpointCode;
+
 /*====================== End of Global Variables ===================*/
 
 /*------------------------------------------------------------------*/
@@ -449,7 +485,7 @@ mono_arch_fregname (int reg) {
 /*------------------------------------------------------------------*/
 
 int
-mono_arch_get_argument_info (MonoMethodSignature *csig, 
+mono_arch_get_argument_info (MonoGenericSharingContext *gsctx, MonoMethodSignature *csig, 
                             int param_count, 
                             MonoJitArgumentInfo *arg_info)
 {
@@ -497,6 +533,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.                                     */
@@ -537,6 +595,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 { 
@@ -575,6 +636,97 @@ indent (int diff) {
 
 /*========================= End of Function ========================*/
 
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - cvtMonoType                                       */
+/*                                                                  */
+/* Function    - Convert a mono-type to a string.                  */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+static const char *
+cvtMonoType(MonoTypeEnum t)
+{
+  switch(t)
+    {
+    case MONO_TYPE_END:
+      return "MONO_TYPE_END";
+    case MONO_TYPE_VOID:
+      return "MONO_TYPE_VOID";
+    case MONO_TYPE_BOOLEAN:
+      return "MONO_TYPE_BOOLEAN";
+    case MONO_TYPE_CHAR:
+      return "MONO_TYPE_CHAR";
+    case MONO_TYPE_I1:
+      return "MONO_TYPE_I1";
+    case MONO_TYPE_U1:
+      return "MONO_TYPE_U1";
+    case MONO_TYPE_I2:
+      return "MONO_TYPE_I2";
+    case MONO_TYPE_U2:
+      return "MONO_TYPE_U2";
+    case MONO_TYPE_I4:
+      return "MONO_TYPE_I4";
+    case MONO_TYPE_U4:
+      return "MONO_TYPE_U4";
+    case MONO_TYPE_I8:
+      return "MONO_TYPE_I8";
+    case MONO_TYPE_U8:
+      return "MONO_TYPE_U8";
+    case MONO_TYPE_R4:
+      return "MONO_TYPE_R4";
+    case MONO_TYPE_R8:
+      return "MONO_TYPE_R8";
+    case MONO_TYPE_STRING:
+      return "MONO_TYPE_STRING";
+    case MONO_TYPE_PTR:
+      return "MONO_TYPE_PTR";
+    case MONO_TYPE_BYREF:
+      return "MONO_TYPE_BYREF";
+    case MONO_TYPE_VALUETYPE:
+      return "MONO_TYPE_VALUETYPE";
+    case MONO_TYPE_CLASS:
+      return "MONO_TYPE_CLASS";
+    case MONO_TYPE_VAR:
+      return "MONO_TYPE_VAR";
+    case MONO_TYPE_ARRAY:
+      return "MONO_TYPE_ARRAY";
+    case MONO_TYPE_GENERICINST:
+      return "MONO_TYPE_GENERICINST";
+    case MONO_TYPE_TYPEDBYREF:
+      return "MONO_TYPE_TYPEDBYREF";
+    case MONO_TYPE_I:
+      return "MONO_TYPE_I";
+    case MONO_TYPE_U:
+      return "MONO_TYPE_U";
+    case MONO_TYPE_FNPTR:
+      return "MONO_TYPE_FNPTR";
+    case MONO_TYPE_OBJECT:
+      return "MONO_TYPE_OBJECT";
+    case MONO_TYPE_SZARRAY:
+      return "MONO_TYPE_SZARRAY";
+    case MONO_TYPE_MVAR:
+      return "MONO_TYPE_MVAR";
+    case MONO_TYPE_CMOD_REQD:
+      return "MONO_TYPE_CMOD_REQD";
+    case MONO_TYPE_CMOD_OPT:
+      return "MONO_TYPE_CMOD_OPT";
+    case MONO_TYPE_INTERNAL:
+      return "MONO_TYPE_INTERNAL";
+    case MONO_TYPE_MODIFIER:
+      return "MONO_TYPE_MODIFIER";
+    case MONO_TYPE_SENTINEL:
+      return "MONO_TYPE_SENTINEL";
+    case MONO_TYPE_PINNED:
+      return "MONO_TYPE_PINNED";
+    default:
+      ;
+    }
+  return "unknown";
+}
+
+/*========================= End of Function ========================*/
+
 /*------------------------------------------------------------------*/
 /*                                                                  */
 /* Name                - decodeParm                                        */
@@ -710,7 +862,7 @@ enum_parmtype:
                                printf("[VALUETYPE:");
                                for (i = 0; i < size; i++)
                                        printf("%02x,", *((guint8 *)curParm+i));
-                               printf("]");
+                               printf("]");
                                break;
                        }
                        case MONO_TYPE_TYPEDBYREF: {
@@ -722,7 +874,7 @@ enum_parmtype:
                                break;
                        }
                        default :
-                               printf("[?? - %d], ",simpleType);
+                               printf("[%s], ",cvtMonoType(simpleType));
                }
        }
 }
@@ -774,24 +926,32 @@ enter_method (MonoMethod *method, RegParm *rParm, char *sp)
        if (sig->hasthis) {
                gpointer *this = (gpointer *) rParm->gr[iParm];
                obj = (MonoObject *) this;
-               if (method->klass->valuetype) { 
+               switch(method->klass->this_arg.type) {
+               case MONO_TYPE_VALUETYPE:
                        if (obj) {
-                               printf("this:[value:%p:%016lx], ", 
-                                      this, *((guint64 *)(this+sizeof(MonoObject))));
+                               guint64 *value = (guint64 *) ((uintptr_t)this + sizeof(MonoObject));
+                               printf("this:[value:%p:%016lx], ", this, *value);
                        } else 
                                printf ("this:[NULL], ");
-               } else {
+                       break;
+               case MONO_TYPE_STRING:
                        if (obj) {
-                               class = obj->vtable->klass;
-                               if (class == mono_defaults.string_class) {
-                                       printf ("this:[STRING:%p:%s], ", 
-                                               obj, mono_string_to_utf8 ((MonoString *)obj));
-                               } else {
-                                       printf ("this:%p[%s.%s], ", 
-                                               obj, class->name_space, class->name);
-                               }
+                               if (obj->vtable) {
+                                       class = obj->vtable->klass;
+                                       if (class == mono_defaults.string_class) {
+                                               printf ("this:[STRING:%p:%s], ", 
+                                                       obj, mono_string_to_utf8 ((MonoString *)obj));
+                                       } else {
+                                               printf ("this:%p[%s.%s], ", 
+                                                       obj, class->name_space, class->name);
+                                       }
+                               } else 
+                                       printf("vtable:[NULL], ");
                        } else 
-                               printf ("this:NULL, ");
+                               printf ("this:[NULL], ");
+                       break;
+               default :
+                       printf("this[%s]: %p, ",cvtMonoType(method->klass->this_arg.type),this);
                }
                oParm++;
        }
@@ -1136,33 +1296,73 @@ 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)
 {
+       guint8 *code;
+
+#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
+
+       ss_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ);
+       bp_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ);
+       mono_mprotect (bp_trigger_page, mono_pagesize (), 0);
+       
+       code = (guint8 *) &breakpointCode;
+       s390_basr(code, s390_r13, 0);
+       s390_j(code, 6);
+       s390_llong(code, 0);
+       s390_lg(code, s390_r13, 0, s390_r13, 4);
+       s390_lg(code, s390_r0, 0, s390_r13, 0);
 }
 
-/*
- * 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                       */
+/* Name                - mono_arch_cpu_optimizations                       */
 /*                                                                  */
 /* Function    - Returns the optimizations supported on this CPU   */
 /*                                                                 */
 /*------------------------------------------------------------------*/
 
 guint32
-mono_arch_cpu_optimizazions (guint32 *exclude_mask)
+mono_arch_cpu_optimizations (guint32 *exclude_mask)
 {
        guint32 opts = 0;
 
@@ -1176,6 +1376,21 @@ mono_arch_cpu_optimizazions (guint32 *exclude_mask)
 
 /*========================= End of Function ========================*/
 
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name         - mono_arch_cpu_enumerate_simd_versions             */
+/*                                                                  */
+/* Function     - Returns the SIMD instruction sets on this CPU     */
+/*                                                                  */
+/*------------------------------------------------------------------*/
+guint32
+mono_arch_cpu_enumerate_simd_versions (void)
+{
+       /* SIMD is currently unimplemented */
+       return 0;
+}
+/*========================= End of Function ========================*/
+
 /*------------------------------------------------------------------*/
 /*                                                                  */
 /* Name                -                                                   */
@@ -1398,7 +1613,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  */
@@ -1410,7 +1625,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;
@@ -1435,6 +1650,8 @@ get_call_info (MonoCompile *cfg, MonoMemPool *mp, MonoMethodSignature *sig, gboo
        sz->code_size     = 0;
        sz->parm_size     = 0;
        sz->local_size    = 0;
+       align             = 0;
+       size              = 0;
 
        /*----------------------------------------------------------*/
        /* We determine the size of the return code/stack in case we*/
@@ -1495,20 +1712,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;
@@ -1516,10 +1729,51 @@ 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;
+               fr = S390_LAST_FPARG_REG + 1;
+
+               /* Emit the signature cookie just before the implicit arguments */
+               add_general (&gr, sz, &cinfo->sigCookie);
        }
 
        /*----------------------------------------------------------*/
@@ -1528,7 +1782,7 @@ enum_retvalue:
        /* parameters.                                              */
        /*----------------------------------------------------------*/
 
-       for (i = 0; i < sig->param_count; ++i) {
+       for (i = pstart; i < sig->param_count; ++i) {
                MonoType *ptype;
 
                /*--------------------------------------------------*/
@@ -1538,6 +1792,7 @@ enum_retvalue:
                if ((sig->call_convention == MONO_CALL_VARARG) &&
                    (i == sig->sentinelpos)) {
                        gr = S390_LAST_ARG_REG + 1;
+                       fr = S390_LAST_FPARG_REG + 1;
                        add_general (&gr, sz, &cinfo->sigCookie);
                }
 
@@ -1702,8 +1957,11 @@ 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;
+               fr = S390_LAST_FPARG_REG + 1;
                add_general (&gr, sz, &cinfo->sigCookie);
        }
 
@@ -1804,7 +2062,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) {
@@ -1848,15 +2110,15 @@ 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;
+                               *indir          = *inst;
+                               inst->opcode    = OP_VTARG_ADDR;
                                inst->inst_left = indir;
                        }
                                break;
@@ -1884,9 +2146,9 @@ mono_arch_allocate_vars (MonoCompile *cfg)
                        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) {
@@ -1913,20 +2175,28 @@ 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;
                        }
+#if 0
                        if ((sig->call_convention == MONO_CALL_VARARG) && 
                            (cinfo->args[iParm].regtype != RegTypeGeneral) &&
                            (iParm < sig->sentinelpos)) 
                                cfg->sig_cookie += size;
+printf("%s %4d cookine %x\n",__FUNCTION__,__LINE__,cfg->sig_cookie);
+#endif
 
-                       offset += size;
+                       offset += MAX(size, 8);
                }
                curinst++;
        }
 
+       cfg->locals_min_stack_offset = offset;
+
        curinst = cfg->locals_start;
        for (iVar = curinst; iVar < cfg->num_varinfo; ++iVar) {
                inst = cfg->varinfo [iVar];
@@ -1935,9 +2205,9 @@ 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), 
@@ -1954,6 +2224,8 @@ mono_arch_allocate_vars (MonoCompile *cfg)
                                iVar, inst->inst_offset, size));
        }
 
+       cfg->locals_max_stack_offset = offset;
+
        /*------------------------------------------------------*/
        /* Allow space for the trace method stack area if needed*/
        /*------------------------------------------------------*/
@@ -1971,13 +2243,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
@@ -2001,6 +2285,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)
 {
@@ -2033,6 +2323,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)
 {
@@ -2061,12 +2359,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                               */
 /*                                                                  */
 /*------------------------------------------------------------------*/
 
@@ -2079,7 +2379,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;
@@ -2087,7 +2389,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);
@@ -2101,6 +2404,12 @@ 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) {
                MonoType *t;
 
@@ -2114,6 +2423,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);
                }
@@ -2178,33 +2488,40 @@ 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, 
-                                                                                STK_BASE, ainfo->offparm);
+                                                        frmReg, ainfo->offparm);
                                MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG,
-                                                                                        ainfo->reg, ainfo->offset, treg);
+                                                            ainfo->reg, ainfo->offset, treg);
+
+                               if (cfg->compute_gc_maps) {
+                                       MonoInst *def;
+
+                                       EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, ainfo->offset, t);
+                               }
                        }
                        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)) {
@@ -2222,6 +2539,7 @@ mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
                                                break;
                                        }
                                }
+#endif
 
                                MONO_ADD_INS (cfg->cbb, ins);
                        }
@@ -2236,6 +2554,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);
        }
@@ -2245,7 +2564,7 @@ mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
 
 /*------------------------------------------------------------------*/
 /*                                                                  */
-/* Name                - mono_arch_emit_outarg_vt                              */
+/* Name                - mono_arch_emit_outarg_vt                          */
 /*                                                                  */
 /*------------------------------------------------------------------*/
 
@@ -2284,14 +2603,31 @@ 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);
+
+               if (cfg->compute_gc_maps) {
+                       MonoInst *def;
+
+                       EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, ainfo->offset, &ins->klass->byval_arg);
+               }
        }
 }
 
+/*========================= End of Function ========================*/
+
 /*------------------------------------------------------------------*/
 /*                                                                  */
-/* Name                - mono_arch_emit_setret                                 */
+/* Name                - mono_arch_emit_setret                             */
 /*                                                                  */
 /*------------------------------------------------------------------*/
 
@@ -2521,14 +2857,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.                             */
@@ -2547,6 +2894,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)
 {
@@ -2611,8 +2964,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]);
@@ -3773,6 +4126,21 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        s390_ledbr (code, ins->dreg, ins->sreg1);
                }
                        break;
+               case OP_TLS_GET: {
+                       if (s390_is_imm16 (ins->inst_offset)) {
+                               s390_lghi (code, s390_r13, ins->inst_offset);
+                       } else {
+                               s390_bras (code, s390_r13, 0);
+                               s390_j    (code, 4);
+                               s390_llong(code, ins->inst_offset);
+                               s390_lg   (code, s390_r13, 0, s390_r13, 4);
+                       }
+                       s390_ear (code, s390_r1, 0);
+                       s390_sllg(code, s390_r1, s390_r1, 0, 32);
+                       s390_ear (code, s390_r1, 1);
+                       s390_lg  (code, ins->dreg, s390_r13, s390_r1, 0);
+               }
+                       break;
                case OP_JMP: {
                        if (cfg->method->save_lmf)
                                restoreLMF(code, cfg->frame_reg, cfg->stack_usage);
@@ -3782,7 +4150,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);
@@ -3885,8 +4253,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);
@@ -3955,7 +4330,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);
                }
@@ -3965,7 +4340,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);
@@ -3974,7 +4349,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);
@@ -3999,6 +4374,35 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_NOT_NULL: {
                }
                        break;
+               case OP_SEQ_POINT: {
+                       int i;
+
+                       if (cfg->compile_aot)
+                               NOT_IMPLEMENTED;
+
+                       /* 
+                        * Read from the single stepping trigger page. This will cause a
+                        * SIGSEGV when single stepping is enabled.
+                        * We do this _before_ the breakpoint, so single stepping after
+                        * a breakpoint is hit will step to the next IL offset.
+                        */
+                       if (ins->flags & MONO_INST_SINGLE_STEP_LOC) {
+                               breakpointCode.pTrigger = ss_trigger_page;
+                               memcpy(code, (void *) &breakpointCode, BREAKPOINT_SIZE);
+                               code += BREAKPOINT_SIZE;
+                       }
+
+                       mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
+
+                       /* 
+                        * A placeholder for a possible breakpoint inserted by
+                        * mono_arch_set_breakpoint ().
+                        */
+                       for (i = 0; i < (BREAKPOINT_SIZE / S390X_NOP_SIZE); ++i)
+                               s390_nop (code);
+                       break;
+               }
+       
                case OP_BR: 
                        EMIT_UNCOND_BRANCH(ins);
                        break;
@@ -4129,7 +4533,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 
                /* floating point opcodes */
                case OP_R8CONST: {
-                       if (*((float *) ins->inst_p0) == 0) {
+                       if (*((double *) ins->inst_p0) == 0) {
                                s390_lzdr (code, ins->dreg);
                        } else {
                                s390_basr  (code, s390_r13, 0);
@@ -4397,24 +4801,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);
@@ -4466,7 +4881,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);
@@ -4487,6 +4902,15 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_MEMORY_BARRIER: {
                }
                        break;
+               case OP_GC_LIVENESS_DEF:
+               case OP_GC_LIVENESS_USE:
+               case OP_GC_PARAM_SLOT_LIVENESS_DEF:
+                       ins->backend.pc_offset = code - cfg->native_code;
+                       break;
+               case OP_GC_SPILL_SLOT_LIVENESS_DEF:
+                       ins->backend.pc_offset = code - cfg->native_code;
+                       bb->spill_slot_defs = g_slist_prepend_mempool (cfg->mempool, bb->spill_slot_defs, ins);
+                       break;
                default:
                        g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
                        g_assert_not_reached ();
@@ -4519,7 +4943,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 void
 mono_arch_register_lowlevel_calls (void)
 {
-       mono_register_jit_icall (mono_arch_get_lmf_addr, "mono_arch_get_lmf_addr", NULL, TRUE);
 }
 
 /*========================= End of Function ========================*/
@@ -4536,7 +4959,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;
 
@@ -4715,8 +5138,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;
@@ -4733,7 +5157,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);
@@ -4744,7 +5171,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);
@@ -4761,6 +5189,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 
@@ -4784,8 +5214,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);
        }
@@ -4855,7 +5285,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);
@@ -4888,7 +5323,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
 
        if (method->save_lmf) {
                /*---------------------------------------------------------------*/
-               /* we build the MonoLMF structure on the stack - see mini-s390.h */
+               /* build the MonoLMF structure on the stack - see mini-s390x.h   */
                /*---------------------------------------------------------------*/
                lmfOffset = alloc_size - sizeof(MonoLMF);       
                                                                                        
@@ -4904,14 +5339,24 @@ 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_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_basr (code, s390_r14, s390_r1);
+               if (lmf_addr_tls_offset == -1) {
+                       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_r14, 4);
+                       s390_basr (code, s390_r14, s390_r1);
+               } else {
+                       /*-------------------------------------------------------*/
+                       /* Get LMF by getting value from thread level storage    */
+                       /*-------------------------------------------------------*/
+                       s390_ear (code, s390_r1, 0);
+                       s390_sllg(code, s390_r1, s390_r1, 0, 32);
+                       s390_ear (code, s390_r1, 1);
+                       s390_lg  (code, s390_r2, 0, s390_r1, lmf_addr_tls_offset);
+               }
 
                /*---------------------------------------------------------------*/     
                /* Set lmf.lmf_addr = jit_tls->lmf                               */     
@@ -5008,7 +5453,7 @@ mono_arch_emit_epilog (MonoCompile *cfg)
        while ((cfg->code_len + max_epilog_size) > (cfg->code_size - 16)) {
                cfg->code_size  *= 2;
                cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
-               mono_jit_stats.code_reallocs++;
+               cfg->stat_code_reallocs++;
        }
 
        code = cfg->native_code + cfg->code_len;
@@ -5021,9 +5466,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);
@@ -5069,7 +5516,7 @@ mono_arch_emit_exceptions (MonoCompile *cfg)
        while ((cfg->code_len + code_size) > (cfg->code_size - 16)) {
                cfg->code_size  *= 2;
                cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
-               mono_jit_stats.code_reallocs++; 
+               cfg->stat_code_reallocs++; 
        }
 
        code = cfg->native_code + cfg->code_len;
@@ -5115,7 +5562,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    */
                                /*---------------------------------------------*/
@@ -5128,7 +5576,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);
@@ -5152,44 +5600,18 @@ mono_arch_emit_exceptions (MonoCompile *cfg)
 
 /*------------------------------------------------------------------*/
 /*                                                                  */
-/* Name                - mono_arch_setup_jit_tls_data                      */
+/* Name                - mono_arch_finish_init                                 */
 /*                                                                  */
 /* Function    - Setup the JIT's Thread Level Specific Data.       */
 /*                                                                 */
 /*------------------------------------------------------------------*/
 
 void
-mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
+mono_arch_finish_init (void)
 {
-       if (!tls_offset_inited) {
-               tls_offset_inited = TRUE;
-
-#if HAVE_KW_THREAD
-# if 0
-       __asm__ ("\tear\t%r1,0\n"
-                "\tlr\t%0,%3\n"
-                "\tsr\t%0,%r1\n"
-                "\tlr\t%1,%4\n"
-                "\tsr\t%1,%r1\n"
-                "\tlr\t%2,%5\n"
-                "\tsr\t%2,%r1\n"
-                : "=r" (appdomain_tls_offset),
-                  "=r" (thread_tls_offset),
-                  "=r" (lmf_tls_offset)
-                : "r" (&tls_appdomain),
-                  "r" (&tls_current_object),
-                  "r" (&mono_lmf_addr)
-                : "1", "cc");
-# endif
-#endif
-       }               
-
-       if (!lmf_addr_key_inited) {
-               lmf_addr_key_inited = TRUE;
-               pthread_key_create (&lmf_addr_key, NULL);
-       }
-       pthread_setspecific (lmf_addr_key, &tls->lmf);
-
+       appdomain_tls_offset = mono_domain_get_tls_offset();
+       lmf_tls_offset = mono_get_lmf_tls_offset();
+       lmf_addr_tls_offset = mono_get_lmf_addr_tls_offset();
 }
 
 /*========================= End of Function ========================*/
@@ -5395,24 +5817,6 @@ mono_arch_flush_register_windows (void)
 
 /*========================= End of Function ========================*/
 
-/*------------------------------------------------------------------*/
-/*                                                                  */
-/* Name                - mono_arch_get_lmf_addr                            */
-/*                                                                  */
-/* Function    -                                                   */
-/*                                                                 */
-/* Returns     -                                                   */
-/*                                                                  */
-/*------------------------------------------------------------------*/
-
-gpointer
-mono_arch_get_lmf_addr (void)
-{
-        return pthread_getspecific (lmf_addr_key);
-}
-
-/*========================= End of Function ========================*/
-
 /*------------------------------------------------------------------*/
 /*                                                                  */
 /* Name                - mono_arch_is_inst_imm                             */
@@ -5456,16 +5860,550 @@ mono_arch_get_patch_offset (guint8 *code)
 /*                                                                  */
 /* Function    -                                                   */
 /*                                                                 */
-/* Returns     - Offset for patch.                                 */
+/* Returns     - Return a register from the context.               */
 /*                                                                  */
 /*------------------------------------------------------------------*/
 
-gpointer
+mgreg_t
 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
 {
-       /* FIXME: implement */
-       g_assert_not_reached ();
-       return NULL;
+       return ((mgreg_t) ctx->uc_mcontext.gregs[reg]);
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_context_set_int_reg.                    */
+/*                                                                  */
+/* Function    - Set a value in a specified register.              */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+void
+mono_arch_context_set_int_reg (MonoContext *ctx, int reg, mgreg_t val)
+{
+       ctx->uc_mcontext.gregs[reg] = val;
 }
 
 /*========================= 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 ========================*/
+
+#ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_set_breakpoint.                         */
+/*                                                                  */
+/* Function    - Set a breakpoint at the native code corresponding */
+/*               to JI at NATIVE_OFFSET.  The location should      */
+/*               contain code emitted by OP_SEQ_POINT.             */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+void
+mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
+{
+       guint8 *code = ip;
+
+       breakpointCode.pTrigger = bp_trigger_page;
+       memcpy(code, (void *) &breakpointCode, BREAKPOINT_SIZE);
+       code += BREAKPOINT_SIZE;
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_clear_breakpoint.                       */
+/*                                                                  */
+/* Function    - Clear the breakpoint at IP.                       */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+void
+mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
+{
+       guint8 *code = ip;
+       int i;
+
+       for (i = 0; i < (BREAKPOINT_SIZE / S390X_NOP_SIZE); i++)
+               s390_nop(code);
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_is_breakpoint_event.                    */
+/*                                                                  */
+/* Function    -                                                   */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+gboolean
+mono_arch_is_breakpoint_event (void *info, void *sigctx)
+{
+       siginfo_t* sinfo = (siginfo_t*) info;
+       /* Sometimes the address is off by 4 */
+       if (sinfo->si_addr >= bp_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)bp_trigger_page + 128)
+               return TRUE;
+       else
+               return FALSE;
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_skip_breakpoint.                        */
+/*                                                                  */
+/* Function    - Modify the CTX so the IP is placed after the      */
+/*                breakpoint instruction, so when we resume, the    */
+/*               instruction is not executed again.                */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+void
+mono_arch_skip_breakpoint (MonoContext *ctx, MonoJitInfo *ji)
+{
+       MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + BREAKPOINT_SIZE);
+}
+
+/*========================= End of Function ========================*/
+       
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_start_single_stepping.                  */
+/*                                                                  */
+/* Function    - Start single stepping.                            */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+void
+mono_arch_start_single_stepping (void)
+{
+       mono_mprotect (ss_trigger_page, mono_pagesize (), 0);
+}
+
+/*========================= End of Function ========================*/
+       
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_stop_single_stepping.                   */
+/*                                                                  */
+/* Function    - Stop single stepping.                             */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+void
+mono_arch_stop_single_stepping (void)
+{
+       mono_mprotect (ss_trigger_page, mono_pagesize (), MONO_MMAP_READ);
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_is_single_step_event.                   */
+/*                                                                  */
+/* Function    - Return whether the machine state in sigctx cor-   */
+/*               responds to a single step event.                  */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+gboolean
+mono_arch_is_single_step_event (void *info, void *sigctx)
+{
+       siginfo_t* sinfo = (siginfo_t*) info;
+
+       /* Sometimes the address is off by 4 */
+       if (sinfo->si_addr >= ss_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)ss_trigger_page + 128)
+               return TRUE;
+       else
+               return FALSE;
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_skip_single_step.                       */
+/*                                                                  */
+/* Function    - Modify the ctx so the IP is placed after the      */
+/*               single step trigger instruction, so that the      */
+/*               instruction is not executed again.                */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+void
+mono_arch_skip_single_step (MonoContext *ctx)
+{
+       MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + BREAKPOINT_SIZE);
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_create_seq_point_info.                  */
+/*                                                                  */
+/* Function    - Return a pointer to a data struction which is     */
+/*               used by the sequence point implementation in      */
+/*               AOTed code.                                       */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+gpointer
+mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
+{
+       NOT_IMPLEMENTED;
+       return NULL;
+}
+
+/*========================= End of Function ========================*/
+
+#endif