2006-01-03 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / mini-s390.c
index 58125ad8619da37de82d3a664e57387b673bb6c1..7d0a92ec189a23a52134ca81004e9e89ec9353e8 100644 (file)
@@ -27,7 +27,7 @@ if (ins->flags & MONO_INST_BRLABEL) {                                                         \
         if (ins->inst_i0->inst_c0) {                                                   \
                int displace;                                                           \
                displace = ((cfg->native_code + ins->inst_i0->inst_c0) - code) / 2;     \
-               if (s390_is_uimm16(displace)) {                                         \
+               if (s390_is_imm16(displace)) {                                          \
                        s390_brc (code, cond, displace);                                \
                } else {                                                                \
                        s390_jcl (code, cond, displace);                                \
@@ -42,7 +42,7 @@ if (ins->flags & MONO_INST_BRLABEL) {                                                         \
                int displace;                                                           \
                displace = ((cfg->native_code +                                         \
                            ins->inst_true_bb->native_offset) - code) / 2;              \
-               if (s390_is_uimm16(displace)) {                                         \
+               if (s390_is_imm16(displace)) {                                          \
                        s390_brc (code, cond, displace);                                \
                } else {                                                                \
                        s390_jcl (code, cond, displace);                                \
@@ -61,7 +61,7 @@ if (ins->flags & MONO_INST_BRLABEL) {                                                         \
         if (ins->inst_i0->inst_c0) {                                                   \
                int displace;                                                           \
                displace = ((cfg->native_code + ins->inst_i0->inst_c0) - code) / 2;     \
-               if (s390_is_uimm16(displace)) {                                         \
+               if (s390_is_imm16(displace)) {                                          \
                        s390_brc (code, S390_CC_UN, displace);                          \
                } else {                                                                \
                        s390_jcl (code, S390_CC_UN, displace);                          \
@@ -76,7 +76,7 @@ if (ins->flags & MONO_INST_BRLABEL) {                                                         \
                int displace;                                                           \
                displace = ((cfg->native_code +                                         \
                            ins->inst_target_bb->native_offset) - code) / 2;            \
-               if (s390_is_uimm16(displace)) {                                         \
+               if (s390_is_imm16(displace)) {                                          \
                        s390_brc (code, S390_CC_UN, displace);                          \
                } else {                                                                \
                        s390_jcl (code, S390_CC_UN, displace);                          \
@@ -96,23 +96,57 @@ if (ins->flags & MONO_INST_BRLABEL) {                                                       \
                s390_jcl (code, cond, 0);                               \
        } while (0); 
 
+#define CHECK_SRCDST_COM                                               \
+       if (ins->dreg == ins->sreg2) {                                  \
+               src2 = ins->sreg1;                                      \
+       } else {                                                        \
+               src2 = ins->sreg2;                                      \
+               if (ins->dreg != ins->sreg1) {                          \
+                       s390_lr  (code, ins->dreg, ins->sreg1);         \
+               }                                                       \
+       }
+
+#define CHECK_SRCDST_NCOM                                              \
+       if (ins->dreg == ins->sreg2) {                                  \
+               src2 = s390_r13;                                        \
+               s390_lr  (code, s390_r13, ins->sreg2);                  \
+       } else {                                                        \
+               src2 = ins->sreg2;                                      \
+       }                                                               \
+       if (ins->dreg != ins->sreg1) {                                  \
+               s390_lr  (code, ins->dreg, ins->sreg1);                 \
+       }
+
+#define CHECK_SRCDST_COM_F                                             \
+       if (ins->dreg == ins->sreg2) {                                  \
+               src2 = ins->sreg1;                                      \
+       } else {                                                        \
+               src2 = ins->sreg2;                                      \
+               if (ins->dreg != ins->sreg1) {                          \
+                       s390_ldr (code, ins->dreg, ins->sreg1);         \
+               }                                                       \
+       }       
+
+#define CHECK_SRCDST_NCOM_F                                            \
+       if (ins->dreg == ins->sreg2) {                                  \
+               src2 = s390_f15;                                        \
+               s390_ldr (code, s390_r13, ins->sreg2);                  \
+       } else {                                                        \
+               src2 = ins->sreg2;                                      \
+       }                                                               \
+       if (ins->dreg != ins->sreg1) {                                  \
+               s390_ldr (code, ins->dreg, ins->sreg1);                 \
+       }
+
 #undef DEBUG
 #define DEBUG(a) if (cfg->verbose_level > 1) a
-#define reg_is_freeable(r) ((r) >= 3 && (r) <= 10)
-#define freg_is_freeable(r) ((r) >= 1 && (r) <= 14)
 
-/*----------------------------------------*/
-/* use s390_r3-s390_r10 as temp registers */
-/*----------------------------------------*/
-#define S390_CALLER_REGS  (0x03f8)
-
-/*----------------------------------------*/
-/* use s390_f2-s390_f14 as temp registers */
-/*----------------------------------------*/
-#define S390_CALLER_FREGS (0x73f8)
+#define MAX_EXC        16
 
 #define S390_TRACE_STACK_SIZE (5*sizeof(gint32)+3*sizeof(gdouble))
 
+#define MAX (a, b) ((a) > (b) ? (a) : (b))
+
 /*========================= End of Defines =========================*/
 
 /*------------------------------------------------------------------*/
@@ -141,6 +175,9 @@ typedef struct {
        guint stack_size,
              local_size,
              code_size,
+             parm_size,
+             offset,
+             offStruct,
              retStruct;
 } size_data;   
 
@@ -153,16 +190,10 @@ enum {
        SAVE_STRUCT,
        SAVE_ONE,
        SAVE_TWO,
-       SAVE_FP
+       SAVE_R4,
+       SAVE_R8
 };
 
-typedef struct {
-       int born_in;
-       int killed_in;
-       int last_use;
-       int prev_use;
-} RegTrack;
-
 typedef struct InstList InstList;
 
 struct InstList {
@@ -181,7 +212,7 @@ enum {
 
 typedef struct {
        gint32  offset;         /* offset from caller's stack */
-       gint32  offparm;        /* offset on callee's stack */
+       gint32  offparm;        /* offset from callee's stack */
        guint16 vtsize;         /* in param area */
        guint8  reg;
        guint8  regtype;        /* See RegType* */
@@ -193,6 +224,7 @@ typedef struct {
        guint32 stack_usage;
        guint32 struct_ret;
        ArgInfo ret;
+       ArgInfo sigCookie;
        ArgInfo args [1];
 } CallInfo;
 
@@ -207,33 +239,22 @@ typedef struct {
 /*                   P r o t o t y p e s                            */
 /*------------------------------------------------------------------*/
 
-static guint32 * emit_memcpy (guint8 *, int, int, int, int, int);
+static guint8 * emit_memcpy (guint8 *, int, int, int, int, int);
 static void indent (int);
-static guint8 * restoreLMF(MonoCompile *, guint8 *);
-static guint8 * backUpStackPtr(MonoCompile *, guint8 *);
+static guint8 * backUpStackPtr(MonoCompile *, guint8 *, gint);
 static void decodeParm (MonoType *, void *, int);
 static void enter_method (MonoMethod *, RegParm *, char *);
 static void leave_method (MonoMethod *, ...);
 static gboolean is_regsize_var (MonoType *);
-static void add_general (guint *, size_data *, ArgInfo *, gboolean);
+static inline void add_general (guint *, size_data *, ArgInfo *, gboolean);
+static inline void add_stackParm (guint *, size_data *, ArgInfo *, gint);
+static inline void add_float (guint *, size_data *, ArgInfo *);
 static CallInfo * calculate_sizes (MonoMethodSignature *, size_data *, gboolean);
 static void peephole_pass (MonoCompile *, MonoBasicBlock *);
-static int mono_spillvar_offset (MonoCompile *, int);
-static int mono_spillvar_offset_float (MonoCompile *, int);
-static void print_ins (int, MonoInst *);
-static void print_regtrack (RegTrack *, int);
-static InstList * inst_list_prepend (MonoMemPool *, InstList *, MonoInst *);
-static int get_register_force_spilling (MonoCompile *, InstList *, MonoInst *, int);
-static int get_register_spilling (MonoCompile *, InstList *, MonoInst *, guint32, int);
-static int get_float_register_spilling (MonoCompile *, InstList *, MonoInst *, guint32, int);
-static MonoInst * create_copy_ins (MonoCompile *, int, int, MonoInst *);
-static MonoInst * create_copy_ins_float (MonoCompile *, int, int, MonoInst *);
-static MonoInst * create_spilled_store (MonoCompile *, int, int, int, MonoInst *);
-static MonoInst * create_spilled_store_float (MonoCompile *, int, int, int, MonoInst *);
-static void insert_before_ins (MonoInst *, InstList *, MonoInst *);
-static int alloc_int_reg (MonoCompile *, InstList *, MonoInst *, int, guint32);
 static guchar * emit_float_to_int (MonoCompile *, guchar *, int, int, int, gboolean);
-static unsigned char * mono_emit_stack_alloc (guchar *, MonoInst *);
+static void mono_arch_break(void);
+gpointer mono_arch_get_lmf_addr (void);
+static guint8 * emit_load_volatile_registers(guint8 *, MonoCompile *);
 
 /*========================= End of Prototypes ======================*/
 
@@ -245,7 +266,25 @@ int mono_exc_esp_offset = 0;
 
 static int indent_level = 0;
 
-static const char*const * ins_spec = s390;
+static const char*const * ins_spec = s390_cpu_desc;
+
+static gboolean tls_offset_inited = FALSE;
+
+static int appdomain_tls_offset = -1,
+                  lmf_tls_offset = -1,
+           thread_tls_offset = -1;
+
+pthread_key_t lmf_addr_key;
+
+gboolean lmf_addr_key_inited = FALSE; 
+
+#if 0
+
+extern __thread MonoDomain *tls_appdomain;
+extern __thread MonoThread *tls_current_object;
+extern __thread gpointer   mono_lmf_addr;
+               
+#endif
 
 /*====================== End of Global Variables ===================*/
 
@@ -266,9 +305,37 @@ mono_arch_regname (int reg) {
                "s390_r10", "s390_r11", "s390_r12", "s390_r13", "s390_r14",
                "s390_r15"
        };
+
+       if (reg >= 0 && reg < 16)
+               return rnames [reg];
+       else
+               return "unknown";
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_fregname                                */
+/*                                                                  */
+/* Function    - Returns the name of the register specified by     */
+/*               the input parameter.                              */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+const char*
+mono_arch_fregname (int reg) {
+       static const char * rnames[] = {
+               "s390_f0", "s390_f1", "s390_f2", "s390_f3", "s390_f4",
+               "s390_f5", "s390_f6", "s390_f7", "s390_f8", "s390_f9",
+               "s390_f10", "s390_f11", "s390_f12", "s390_f13", "s390_f14",
+               "s390_f15"
+       };
+
        if (reg >= 0 && reg < 16)
                return rnames [reg];
-       return "unknown";
+       else
+               return "unknown";
 }
 
 /*========================= End of Function ========================*/
@@ -282,7 +349,7 @@ mono_arch_regname (int reg) {
 /*                                                                  */
 /*------------------------------------------------------------------*/
 
-static guint32*
+static guint*
 emit_memcpy (guint8 *code, int size, int dreg, int doffset, int sreg, int soffset)
 {
        switch (size) {
@@ -390,42 +457,27 @@ mono_arch_get_argument_info (MonoMethodSignature *csig,
 
 /*------------------------------------------------------------------*/
 /*                                                                  */
-/* Name                - restoreLMF                                        */
+/* Name                - retFitsInReg.                                     */
 /*                                                                  */
-/* Function    - Restore the LMF state prior to exiting a method.  */
+/* Function    - Determines if a value can be returned in one or   */
+/*                two registers.                                    */
 /*                                                                  */
 /*------------------------------------------------------------------*/
 
-static inline guint8 * 
-restoreLMF(MonoCompile *cfg, guint8 *code)
+static inline gboolean
+retFitsInReg(guint32 size)
 {
-       int lmfOffset = 0;
-
-       s390_lr  (code, s390_r13, cfg->frame_reg);
-
-       lmfOffset = cfg->stack_usage -  sizeof(MonoLMF);
-
-       /*-------------------------------------------------*/
-       /* r13 = my lmf                                    */
-       /*-------------------------------------------------*/
-       s390_ahi (code, s390_r13, lmfOffset);
-
-       /*-------------------------------------------------*/
-       /* r6 = &jit_tls->lmf                              */
-       /*-------------------------------------------------*/
-       s390_l   (code, s390_r6, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, lmf_addr));
-
-       /*-------------------------------------------------*/
-       /* r0 = lmf.previous_lmf                           */
-       /*-------------------------------------------------*/
-       s390_l   (code, s390_r0, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, previous_lmf));
-
-       /*-------------------------------------------------*/
-       /* jit_tls->lmf = previous_lmf                     */
-       /*-------------------------------------------------*/
-       s390_l   (code, s390_r13, 0, s390_r6, 0);
-       s390_st  (code, s390_r0, 0, s390_r6, 0);
-       return (code);
+       switch (size) {
+               case 0:
+               case 1:
+               case 2:
+               case 4:
+               case 8:
+                       return (TRUE);
+               break;
+               default:
+                       return (FALSE);
+       }
 }
 
 /*========================= End of Function ========================*/
@@ -439,18 +491,18 @@ restoreLMF(MonoCompile *cfg, guint8 *code)
 /*------------------------------------------------------------------*/
 
 static inline guint8 *
-backUpStackPtr(MonoCompile *cfg, guint8 *code)
+backUpStackPtr(MonoCompile *cfg, guint8 *code, gint framePtr)
 {
        int stackSize = cfg->stack_usage;
 
-       if (s390_is_imm16 (cfg->stack_usage)) {
-               s390_ahi  (code, STK_BASE, cfg->stack_usage);
+       if (s390_is_uimm16 (cfg->stack_usage)) {
+               s390_ahi  (code, framePtr, cfg->stack_usage);
        } else { 
                while (stackSize > 32767) {
-                       s390_ahi  (code, STK_BASE, 32767);
+                       s390_ahi  (code, framePtr, 32767);
                        stackSize -= 32767;
                }
-               s390_ahi  (code, STK_BASE, stackSize);
+               s390_ahi  (code, framePtr, stackSize);
        }
        return (code);
 }
@@ -497,7 +549,7 @@ decodeParm(MonoType *type, void *curParm, int size)
        if (type->byref) {
                printf("[BYREF:%p], ", *((char **) curParm));
        } else {
-               simpleType = type->type;
+               simpleType = mono_type_get_underlying_type(type)->type;
 enum_parmtype:
                switch (simpleType) {
                        case MONO_TYPE_I :
@@ -507,10 +559,10 @@ enum_parmtype:
                                printf ("[UINTPTR:%p], ", *((int **) curParm));
                                break;
                        case MONO_TYPE_BOOLEAN :
-                               printf ("[BOOL:%p], ", *((int *) curParm));
+                               printf ("[BOOL:%d], ", *((int *) curParm));
                                break;
                        case MONO_TYPE_CHAR :
-                               printf ("[CHAR:%p], ", *((int *) curParm));
+                               printf ("[CHAR:%c], ", *((int *) curParm));
                                break;
                        case MONO_TYPE_I1 :
                                printf ("[INT1:%d], ", *((int *) curParm));
@@ -522,16 +574,16 @@ enum_parmtype:
                                printf ("[INT4:%d], ", *((int *) curParm));
                                break; 
                        case MONO_TYPE_U1 :
-                               printf ("[UINT1:%ud], ", *((unsigned int *) curParm));
+                               printf ("[UINT1:%u], ", *((unsigned int *) curParm));
                                break; 
                        case MONO_TYPE_U2 :
-                               printf ("[UINT2:%ud], ", *((guint16 *) curParm));
+                               printf ("[UINT2:%u], ", *((guint16 *) curParm));
                                break; 
                        case MONO_TYPE_U4 :
-                               printf ("[UINT4:%ud], ", *((guint32 *) curParm));
+                               printf ("[UINT4:%u], ", *((guint32 *) curParm));
                                break; 
                        case MONO_TYPE_U8 :
-                               printf ("[UINT8:%ul], ", *((guint64 *) curParm));
+                               printf ("[UINT8:%llu], ", *((guint64 *) curParm));
                                break; 
                        case MONO_TYPE_STRING : {
                                MonoString *s = *((MonoString **) curParm);
@@ -547,18 +599,19 @@ enum_parmtype:
                        case MONO_TYPE_OBJECT : {
                                MonoObject *obj = *((MonoObject **) curParm);
                                MonoClass *class;
-                               if (obj) {
+                               if ((obj) && (obj->vtable)) {
                                        printf("[CLASS/OBJ:");
                                        class = obj->vtable->klass;
-                                       if (class == mono_defaults.string_class) {
-                                               printf("[STRING:%p:%s]", 
-                                                      *obj, mono_string_to_utf8 (obj));
-                                       } else if (class == mono_defaults.int32_class) { 
-                                               printf("[INT32:%p:%d]", 
-                                                       obj, *(gint32 *)((char *)obj + sizeof (MonoObject)));
-                                       } else
-                                               printf("[%s.%s:%p]", 
-                                                      class->name_space, class->name, obj);
+                                       printf("%p [%p] ",obj,curParm);
+//                                     if (class == mono_defaults.string_class) {
+//                                             printf("[STRING:%p:%s]", 
+//                                                    *obj, mono_string_to_utf8 (obj));
+//                                     } else if (class == mono_defaults.int32_class) { 
+//                                             printf("[INT32:%p:%d]", 
+//                                                     obj, *(gint32 *)((char *)obj + sizeof (MonoObject)));
+//                                     } else
+//                                             printf("[%s.%s:%p]", 
+//                                                    class->name_space, class->name, obj);
                                        printf("], ");
                                } else {
                                        printf("[OBJECT:null], ");
@@ -581,24 +634,51 @@ enum_parmtype:
                                printf("[INT8:%lld], ", *((gint64 *) (curParm)));
                                break;
                        case MONO_TYPE_R4 :
-                               printf("[FLOAT4:%f], ", *((float *) (curParm)));
+                               printf("[FLOAT4:%g], ", *((double *) (curParm)));
                                break;
                        case MONO_TYPE_R8 :
                                printf("[FLOAT8:%g], ", *((double *) (curParm)));
                                break;
                        case MONO_TYPE_VALUETYPE : {
                                int i;
+                               MonoMarshalType *info;
+
                                if (type->data.klass->enumtype) {
                                        simpleType = type->data.klass->enum_basetype->type;
                                        printf("{VALUETYPE} - ");
                                        goto enum_parmtype;
                                }
+
+                               info = mono_marshal_load_type_info (type->data.klass);
+
+                               if ((info->native_size == sizeof(float)) &&
+                                   (info->num_fields  == 1) &&
+                                   (info->fields[0].field->type->type == MONO_TYPE_R4)) {
+                                               printf("[FLOAT4:%f], ", *((float *) (curParm)));
+                                       break;
+                               }
+
+                               if ((info->native_size == sizeof(double)) &&
+                                   (info->num_fields  == 1) &&
+                                   (info->fields[0].field->type->type == MONO_TYPE_R8)) {
+                                       printf("[FLOAT8:%g], ", *((double *) (curParm)));
+                                       break;
+                               }
+
                                printf("[VALUETYPE:");
                                for (i = 0; i < size; i++)
                                        printf("%02x,", *((guint8 *)curParm+i));
                                printf("]");
                                break;
                        }
+                       case MONO_TYPE_TYPEDBYREF: {
+                               int i;
+                               printf("[TYPEDBYREF:");
+                               for (i = 0; i < size; i++)
+                                       printf("%02x,", *((guint8 *)curParm+i));
+                               printf("]");
+                               break;
+                       }
                        default :
                                printf("[?? - %d], ",simpleType);
                }
@@ -607,6 +687,7 @@ enum_parmtype:
 
 /*========================= End of Function ========================*/
 
+static int lc = 0;
 /*------------------------------------------------------------------*/
 /*                                                                  */
 /* Name                - enter_method                                      */
@@ -622,7 +703,6 @@ enter_method (MonoMethod *method, RegParm *rParm, char *sp)
        int i, oParm = 0, iParm = 0;
        MonoClass *class;
        MonoObject *obj;
-       MonoJitArgumentInfo *arg_info;
        MonoMethodSignature *sig;
        char *fname;
        guint32 ip;
@@ -631,23 +711,29 @@ enter_method (MonoMethod *method, RegParm *rParm, char *sp)
        size_data sz;
        void *curParm;
 
+
+lc++;
+if (lc > 5000000) {
+fseek(stdout, 0L, SEEK_SET);
+lc = 0;
+}
        fname = mono_method_full_name (method, TRUE);
        indent (1);
        printf ("ENTER: %s(", fname);
        g_free (fname);
 
        ip  = (*(guint32 *) (sp+S390_RET_ADDR_OFFSET)) & 0x7fffffff;
-       printf (") ip: %p sp: %p - ", ip, sp); 
+       printf (") ip: %p sp: %p - ", (gpointer) ip, sp); 
 
        if (rParm == NULL)
                return;
        
-       sig = method->signature;
+       sig = mono_method_signature (method);
        
        cinfo = calculate_sizes (sig, &sz, sig->pinvoke);
 
        if (cinfo->struct_ret) {
-               printf ("[VALUERET:%p], ", rParm->gr[0]);
+               printf ("[STRUCTRET:%p], ", (gpointer) rParm->gr[0]);
                iParm = 1;
        }
 
@@ -662,14 +748,15 @@ enter_method (MonoMethod *method, RegParm *rParm, char *sp)
                                printf ("this:[NULL], ");
                } else {
                        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);
-                               }
+//                             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);
+//                             }
+printf("this:%p, ",obj);
                        } else 
                                printf ("this:NULL, ");
                }
@@ -677,7 +764,7 @@ enter_method (MonoMethod *method, RegParm *rParm, char *sp)
        }
                                        
        for (i = 0; i < sig->param_count; ++i) {
-               ainfo = cinfo->args + (i + oParm);
+               ainfo = &cinfo->args[i + oParm];
                switch (ainfo->regtype) {
                        case RegTypeGeneral :
                                decodeParm(sig->params[i], &(rParm->gr[ainfo->reg-2]), ainfo->size);
@@ -694,21 +781,14 @@ enter_method (MonoMethod *method, RegParm *rParm, char *sp)
                                else
                                        curParm = sp+ainfo->offset;
 
-                               switch (ainfo->vtsize) {
-                                       case 0:
-                                       case 1:
-                                       case 2:
-                                       case 4:
-                                       case 8:
-                                               decodeParm(sig->params[i], 
+                               if (retFitsInReg (ainfo->vtsize)) 
+                                       decodeParm(sig->params[i], 
                                                   curParm,
                                                   ainfo->size);
-                                               break;
-                                       default:
-                                               decodeParm(sig->params[i], 
+                               else
+                                       decodeParm(sig->params[i], 
                                                   *((char **) curParm),
                                                   ainfo->vtsize);
-                                       }
                                break;
                        case RegTypeStructByAddr :
                                if (ainfo->reg != STK_BASE) 
@@ -754,7 +834,7 @@ leave_method (MonoMethod *method, ...)
        printf ("LEAVE: %s", fname);
        g_free (fname);
 
-       type = method->signature->ret;
+       type = mono_method_signature (method)->ret;
 
 handle_enum:
        switch (type->type) {
@@ -805,13 +885,13 @@ handle_enum:
                break;
        }
        case MONO_TYPE_I: {
-               int *val = va_arg (ap, int*);
+               int val = va_arg (ap, int);
                printf ("[INT:%d]", val);
                printf("]");
                break;
        }
        case MONO_TYPE_U: {
-               int *val = va_arg (ap, int*);
+               int val = va_arg (ap, int);
                printf ("[UINT:%d]", val);
                printf("]");
                break;
@@ -830,16 +910,16 @@ handle_enum:
        case MONO_TYPE_OBJECT: {
                MonoObject *o = va_arg (ap, MonoObject *);
 
-               if (o) {
-                       if (o->vtable->klass == mono_defaults.boolean_class) {
-                               printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));            
-                       } else if  (o->vtable->klass == mono_defaults.int32_class) {
-                               printf ("[INT32:%p:%d]", o, *((gint32 *)((char *)o + sizeof (MonoObject))));    
-                       } else if  (o->vtable->klass == mono_defaults.int64_class) {
-                               printf ("[INT64:%p:%lld]", o, *((gint64 *)((char *)o + sizeof (MonoObject))));  
-                       } else
-                               printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
-               } else
+//             if ((o) && (o->vtable)) {
+//                     if (o->vtable->klass == mono_defaults.boolean_class) {
+//                             printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));            
+//                     } else if  (o->vtable->klass == mono_defaults.int32_class) {
+//                             printf ("[INT32:%p:%d]", o, *((gint32 *)((char *)o + sizeof (MonoObject))));    
+//                     } else if  (o->vtable->klass == mono_defaults.int64_class) {
+//                             printf ("[INT64:%p:%lld]", o, *((gint64 *)((char *)o + sizeof (MonoObject))));  
+//                     } else
+//                             printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
+//             } else
                        printf ("[OBJECT:%p]", o);
               
                break;
@@ -857,9 +937,15 @@ handle_enum:
                printf ("[LONG:%lld]", l);
                break;
        }
+       case MONO_TYPE_U8: {
+               guint64 l =  va_arg (ap, guint64);
+               printf ("[ULONG:%llu]", l);
+               break;
+       }
        case MONO_TYPE_R4: {
-               double f = va_arg (ap, double);
-               printf ("[FLOAT4:%f]\n", (float) f);
+               double f;
+               f = va_arg (ap, double);
+               printf ("[FLOAT4:%g]\n", f);
                break;
        }
        case MONO_TYPE_R8: {
@@ -867,27 +953,76 @@ handle_enum:
                printf ("[FLOAT8:%g]\n", f);
                break;
        }
-       case MONO_TYPE_VALUETYPE: 
+       case MONO_TYPE_VALUETYPE: {
+               MonoMarshalType *info;
                if (type->data.klass->enumtype) {
                        type = type->data.klass->enum_basetype;
                        goto handle_enum;
                } else {
                        guint8 *p = va_arg (ap, gpointer);
                        int j, size, align;
+
+                       info = mono_marshal_load_type_info (type->data.klass);
+
+                       if ((info->native_size == sizeof(float)) &&
+                           (info->num_fields  == 1) &&
+                           (info->fields[0].field->type->type == MONO_TYPE_R4)) {
+                               double f = va_arg (ap, double);
+                               printf("[FLOAT4:%g]\n", (double) f);
+                               break;
+                       }
+
+                       if ((info->native_size == sizeof(double)) &&
+                           (info->num_fields  == 1) &&
+                           (info->fields[0].field->type->type == MONO_TYPE_R8)) {
+                               double f = va_arg (ap, double);
+                               printf("[FLOAT8:%g]\n", f);
+                               break;
+                       }
+
                        size = mono_type_size (type, &align);
+                       switch (size) {
+                       case 1:
+                       case 2:
+                       case 4:
+                       case 8:
+                               printf ("[");
+                               for (j = 0; p && j < size; j++)
+                                       printf ("%02x,", p [j]);
+                               printf ("]\n");
+                               break;
+                       default:
+                               printf ("[VALUERET]\n");
+                       }
+               }       
+               break;
+       }
+       case MONO_TYPE_TYPEDBYREF: {
+               guint8 *p = va_arg (ap, gpointer);
+               int j, size, align;
+               size = mono_type_size (type, &align);
+               switch (size) {
+               case 1:
+               case 2:
+               case 4:
+               case 8:
                        printf ("[");
                        for (j = 0; p && j < size; j++)
                                printf ("%02x,", p [j]);
-                       printf ("]");
+                       printf ("]\n");
+                       break;
+               default:
+                       printf ("[TYPEDBYREF]\n");
                }
+       }
                break;
        default:
                printf ("(unknown return type %x)", 
-                       method->signature->ret->type);
+                       mono_method_signature (method)->ret->type);
        }
 
        ip = ((gint32) __builtin_return_address (0)) & 0x7fffffff;
-       printf (" ip: %p\n", ip);
+       printf (" ip: %p\n", (gpointer) ip);
 }
 
 /*========================= End of Function ========================*/
@@ -928,8 +1063,11 @@ mono_arch_cpu_optimizazions (guint32 *exclude_mask)
 {
        guint32 opts = 0;
 
-       /* no s390-specific optimizations yet */
+       /*----------------------------------------------------------*/
+       /* no s390-specific optimizations yet                       */
+       /*----------------------------------------------------------*/
        *exclude_mask = MONO_OPT_INLINE|MONO_OPT_LINEARS;
+//     *exclude_mask = MONO_OPT_INLINE;
        return opts;
 }
 
@@ -947,7 +1085,7 @@ static gboolean
 is_regsize_var (MonoType *t) {
        if (t->byref)
                return TRUE;
-       switch (t->type) {
+       switch (mono_type_get_underlying_type (t)->type) {
        case MONO_TYPE_I4:
        case MONO_TYPE_U4:
        case MONO_TYPE_I:
@@ -988,10 +1126,11 @@ mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
                MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
 
                /* unused vars */
-               if (vmv->range.first_use.abs_pos > vmv->range.last_use.abs_pos)
+               if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
                        continue;
 
-               if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
+               if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || 
+                   (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
                        continue;
 
                /* we can only allocate 32 bit values */
@@ -1019,10 +1158,17 @@ GList *
 mono_arch_get_global_int_regs (MonoCompile *cfg)
 {
        GList *regs = NULL;
-       int i, top = 12;
+       MonoMethodHeader *header;
+       int i, top = 13;
+
+       header = mono_method_get_header (cfg->method);
+       if ((cfg->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
+               cfg->frame_reg = s390_r11;
 
-       for (i = 3; i < top; ++i)
-               regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
+       for (i = 8; i < top; ++i) {
+               if (cfg->frame_reg != i) 
+                       regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
+       }
 
        return regs;
 }
@@ -1063,6 +1209,8 @@ add_general (guint *gr, size_data *sz, ArgInfo *ainfo, gboolean simple)
                        ainfo->reg      = STK_BASE;
                        ainfo->regtype  = RegTypeBase;
                        sz->stack_size += sizeof(int);
+                       sz->local_size += sizeof(int);
+                       sz->offStruct  += sizeof(int);
                        sz->code_size  += 12;    
                } else {
                        ainfo->reg      = *gr;
@@ -1075,6 +1223,8 @@ add_general (guint *gr, size_data *sz, ArgInfo *ainfo, gboolean simple)
                        ainfo->reg      = STK_BASE;
                        ainfo->regtype  = RegTypeBase;
                        sz->stack_size += sizeof(long long);
+                       sz->local_size += sizeof(long long);
+                       sz->offStruct  += sizeof(long long);
                        sz->code_size  += 10;   
                } else {
                        ainfo->reg      = *gr;
@@ -1087,6 +1237,69 @@ add_general (guint *gr, size_data *sz, ArgInfo *ainfo, gboolean simple)
 
 /*========================= End of Function ========================*/
 
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - add_stackParm                                     */
+/*                                                                  */
+/* Function    - Determine code and stack size incremements for a  */
+/*               parameter.                                        */
+/*                                                                  */
+/*------------------------------------------------------------------*/
+
+static void inline
+add_stackParm (guint *gr, size_data *sz, ArgInfo *ainfo, gint size)
+{
+       if (*gr > S390_LAST_ARG_REG) {
+               sz->stack_size  = S390_ALIGN(sz->stack_size, sizeof(long));
+               ainfo->reg      = STK_BASE;
+               sz->parm_size  += sizeof(gpointer);
+               sz->offStruct  += sizeof(gpointer);
+       } else {
+               ainfo->reg      = *gr;
+       }
+       (*gr) ++;
+       ainfo->offset   = sz->stack_size;
+       ainfo->offparm  = sz->offset;
+       sz->offset      = S390_ALIGN(sz->offset+size, sizeof(long));
+       ainfo->size     = size;
+       ainfo->regtype  = RegTypeStructByAddr; 
+       ainfo->vtsize   = size;
+       sz->parm_size  += size;
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - add_float                                         */
+/*                                                                  */
+/* Function    - Determine code and stack size incremements for a  */
+/*               float parameter.                                  */
+/*                                                                  */
+/*------------------------------------------------------------------*/
+
+static void inline
+add_float (guint *fr,  size_data *sz, ArgInfo *ainfo)
+{
+       if ((*fr) <= S390_LAST_FPARG_REG) {
+               ainfo->regtype = RegTypeFP;
+               ainfo->reg     = *fr;
+               sz->code_size += 4;
+               (*fr) += 2;
+       }
+       else {
+               ainfo->offset   = sz->stack_size;
+               ainfo->reg      = STK_BASE;
+               ainfo->regtype  = RegTypeBase;
+               sz->code_size  += 4;
+               sz->stack_size += ainfo->size;
+               sz->local_size += ainfo->size;
+               sz->offStruct  += ainfo->size;
+       }
+}
+
+/*========================= End of Function ========================*/
+
 /*------------------------------------------------------------------*/
 /*                                                                  */
 /* Name                - calculate_sizes                                   */
@@ -1102,7 +1315,7 @@ static CallInfo *
 calculate_sizes (MonoMethodSignature *sig, size_data *sz, 
                 gboolean string_ctor)
 {
-       guint i, fr, gr, size, nWords;
+       guint i, fr, gr, size;
        int nParm = sig->hasthis + sig->param_count;
        guint32 simpletype, align;
        CallInfo *cinfo = g_malloc0 (sizeof (CallInfo) + sizeof (ArgInfo) * nParm);
@@ -1111,9 +1324,12 @@ calculate_sizes (MonoMethodSignature *sig, size_data *sz,
        gr                = s390_r2;
        nParm             = 0;
        cinfo->struct_ret = 0;
+       sz->offset        = 0;
+       sz->offStruct     = S390_MINIMAL_STACK_SIZE;
        sz->retStruct     = 0;
        sz->stack_size    = S390_MINIMAL_STACK_SIZE;
        sz->code_size     = 0;
+       sz->parm_size     = 0;
        sz->local_size    = 0;
 
        /*----------------------------------------------------------*/
@@ -1122,16 +1338,9 @@ calculate_sizes (MonoMethodSignature *sig, size_data *sz,
        /* area that the callee will use.                           */
        /*----------------------------------------------------------*/
 
-//     if (sig->ret->byref || string_ctor) {
-//             sz->code_size += 8;
-//             add_general (&gr, sz, cinfo->args+nParm, TRUE);
-//             cinfo->args[nParm].size = sizeof(gpointer);
-//             nParm++;
-//     } else {
-       {
-               simpletype = sig->ret->type;
+       simpletype = mono_type_get_underlying_type (sig->ret)->type;
 enum_retvalue:
-               switch (simpletype) {
+       switch (simpletype) {
                case MONO_TYPE_BOOLEAN:
                case MONO_TYPE_I1:
                case MONO_TYPE_U1:
@@ -1147,6 +1356,7 @@ enum_retvalue:
                case MONO_TYPE_SZARRAY:
                case MONO_TYPE_ARRAY:
                case MONO_TYPE_PTR:
+               case MONO_TYPE_FNPTR:
                case MONO_TYPE_STRING:
                        cinfo->ret.reg = s390_r2;
                        sz->code_size += 4;
@@ -1161,29 +1371,36 @@ enum_retvalue:
                        cinfo->ret.reg = s390_r2;
                        sz->code_size += 4;
                        break;
-               case MONO_TYPE_VALUETYPE:
+               case MONO_TYPE_VALUETYPE: {
+                       MonoClass *klass = mono_class_from_mono_type (sig->ret);
                        if (sig->ret->data.klass->enumtype) {
                                simpletype = sig->ret->data.klass->enum_basetype->type;
                                goto enum_retvalue;
                        }
                        if (sig->pinvoke)
-                               size = mono_class_native_size (sig->ret->data.klass, &align);
+                               size = mono_class_native_size (klass, &align);
                        else
-                               size = mono_class_value_size (sig->ret->data.klass, &align);
+                               size = mono_class_value_size (klass, &align);
+       
                        cinfo->ret.reg    = s390_r2;
                        cinfo->struct_ret = 1;
                        cinfo->ret.size   = size;
                        cinfo->ret.vtsize = size;
-                       cinfo->ret.offset = sz->stack_size;
-                       sz->stack_size   += S390_ALIGN(size, align);
                        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;
                default:
                        g_error ("Can't handle as return value 0x%x", sig->ret->type);
-               }
        }
 
        if (sig->hasthis) {
@@ -1199,14 +1416,24 @@ enum_retvalue:
        /*----------------------------------------------------------*/
 
        for (i = 0; i < sig->param_count; ++i) {
+               /*--------------------------------------------------*/
+               /* Handle vararg type calls. All args are put on    */
+               /* the stack.                                       */
+               /*--------------------------------------------------*/
+               if ((sig->call_convention == MONO_CALL_VARARG) &&
+                   (i == sig->sentinelpos)) {
+                       gr = S390_LAST_ARG_REG + 1;
+                       add_general (&gr, sz, &cinfo->sigCookie, TRUE);
+               }
+
                if (sig->params [i]->byref) {
                        add_general (&gr, sz, cinfo->args+nParm, TRUE);
                        cinfo->args[nParm].size = sizeof(gpointer);
                        nParm++;
                        continue;
                }
-               simpletype = sig->params [i]->type;
-       enum_calc_size:
+
+               simpletype = mono_type_get_underlying_type(sig->params [i])->type;
                switch (simpletype) {
                case MONO_TYPE_BOOLEAN:
                case MONO_TYPE_I1:
@@ -1231,6 +1458,7 @@ enum_retvalue:
                case MONO_TYPE_I:
                case MONO_TYPE_U:
                case MONO_TYPE_PTR:
+               case MONO_TYPE_FNPTR:
                case MONO_TYPE_CLASS:
                case MONO_TYPE_OBJECT:
                case MONO_TYPE_STRING:
@@ -1240,21 +1468,50 @@ enum_retvalue:
                        add_general (&gr, sz, cinfo->args+nParm, TRUE);
                        nParm++;
                        break;
-               case MONO_TYPE_VALUETYPE:
-                       if (sig->params [i]->data.klass->enumtype) {
-                               simpletype = sig->params [i]->data.klass->enum_basetype->type;
-                               goto enum_calc_size;
-                       }
+               case MONO_TYPE_I8:
+               case MONO_TYPE_U8:
+                       cinfo->args[nParm].size = sizeof(long long);
+                       add_general (&gr, sz, cinfo->args+nParm, FALSE);
+                       nParm++;
+                       break;
+               case MONO_TYPE_R4:
+                       cinfo->args[nParm].size = sizeof(float);
+                       add_float (&fr, sz, cinfo->args+nParm);
+                       nParm++;
+                       break;
+               case MONO_TYPE_R8:
+                       cinfo->args[nParm].size = sizeof(double);
+                       add_float (&fr, sz, cinfo->args+nParm);
+                       nParm++;
+                       break;
+               case MONO_TYPE_VALUETYPE: {
+                       MonoMarshalType *info;
+                       MonoClass *klass = mono_class_from_mono_type (sig->params [i]);
                        if (sig->pinvoke)
-                               size = mono_class_native_size (sig->params [i]->data.klass, &align);
+                               size = mono_class_native_size (klass, &align);
                        else
-                               size = mono_class_value_size (sig->params [i]->data.klass, &align);
-                       nWords = (size + sizeof(gpointer) - 1) /
-                                sizeof(gpointer);
+                               size = mono_class_value_size (klass, &align);
+       
+                       info = mono_marshal_load_type_info (klass);
+
+                       if ((info->native_size == sizeof(float)) &&
+                           (info->num_fields  == 1) &&
+                           (info->fields[0].field->type->type == MONO_TYPE_R4)) {
+                               cinfo->args[nParm].size = sizeof(float);
+                               add_float(&fr, sz, cinfo->args+nParm);
+                               break;
+                       }
+
+                       if ((info->native_size == sizeof(double)) &&
+                           (info->num_fields  == 1) &&
+                           (info->fields[0].field->type->type == MONO_TYPE_R8)) {
+                               cinfo->args[nParm].size = sizeof(double);
+                               add_float(&fr, sz, cinfo->args+nParm);
+                               break;
+                       }
 
                        cinfo->args[nParm].vtsize  = 0;
                        cinfo->args[nParm].size    = 0;
-                       cinfo->args[nParm].offparm = sz->local_size;
 
                        switch (size) {
                                /*----------------------------------*/
@@ -1280,63 +1537,74 @@ enum_retvalue:
                                        sz->local_size            += sizeof(long);
                                        break;
                                default:
-                                       add_general(&gr, sz, cinfo->args+nParm, TRUE);
-                                       cinfo->args[nParm].size    = sizeof(int);
-                                       cinfo->args[nParm].regtype = RegTypeStructByAddr; 
-                                       cinfo->args[nParm].vtsize  = size;
-                                       sz->code_size             += 40;
-                                       sz->local_size            += size;
-                                       if (cinfo->args[nParm].reg == STK_BASE)
-                                               sz->local_size += sizeof(gpointer);
+                                       add_stackParm(&gr, sz, cinfo->args+nParm, size);
                                        nParm++;
                        }
+               }
                        break;
-               case MONO_TYPE_I8:
-               case MONO_TYPE_U8:
-                       cinfo->args[nParm].size = sizeof(long long);
-                       add_general (&gr, sz, cinfo->args+nParm, FALSE);
-                       nParm++;
-                       break;
-               case MONO_TYPE_R4:
-                       cinfo->args[nParm].size = sizeof(float);
-                       if (fr <= S390_LAST_FPARG_REG) {
-                               cinfo->args[nParm].regtype = RegTypeFP;
-                               cinfo->args[nParm].reg     = fr;
-                               sz->code_size += 4;
-                               fr += 2;
-                       }
-                       else {
-                               cinfo->args[nParm].offset  = sz->stack_size;
-                               cinfo->args[nParm].reg     = STK_BASE;
-                               cinfo->args[nParm].regtype = RegTypeBase;
-                               sz->code_size  += 4;
-                               sz->stack_size += sizeof(float);
-                       }
-                       nParm++;
-                       break;
-               case MONO_TYPE_R8:
-                       cinfo->args[nParm].size = sizeof(double);
-                       if (fr <= S390_LAST_FPARG_REG) {
-                               cinfo->args[nParm].regtype = RegTypeFP;
-                               cinfo->args[nParm].reg     = fr;
-                               sz->code_size += 4;
-                               fr += 2;
-                       } else {
-                               cinfo->args[nParm].offset  = sz->stack_size;
-                               cinfo->args[nParm].reg     = STK_BASE;
-                               cinfo->args[nParm].regtype = RegTypeBase;
-                               sz->code_size  += 4;
-                               sz->stack_size += sizeof(double);
+               case MONO_TYPE_TYPEDBYREF: {
+                       int size = sizeof (MonoTypedRef);
+
+                       cinfo->args[nParm].vtsize  = 0;
+                       cinfo->args[nParm].size    = 0;
+
+                       switch (size) {
+                               /*----------------------------------*/
+                               /* On S/390, structures of size 1,  */
+                               /* 2, 4, and 8 bytes are passed in  */
+                               /* (a) register(s).                 */
+                               /*----------------------------------*/
+                               case 0:
+                               case 1:
+                               case 2:
+                               case 4:
+                                       add_general(&gr, sz, cinfo->args+nParm, TRUE);
+                                       cinfo->args[nParm].size    = size;
+                                       cinfo->args[nParm].regtype = RegTypeStructByVal; 
+                                       nParm++;
+                                       sz->local_size            += sizeof(long);
+                                       break;
+                               case 8:
+                                       add_general(&gr, sz, cinfo->args+nParm, FALSE);
+                                       cinfo->args[nParm].size    = sizeof(long long);
+                                       cinfo->args[nParm].regtype = RegTypeStructByVal; 
+                                       nParm++;
+                                       sz->local_size            += sizeof(long);
+                                       break;
+                               default:
+                                       add_stackParm(&gr, sz, cinfo->args+nParm, size);
+                                       nParm++;
                        }
-                       nParm++;
+               }
                        break;
                default:
                        g_error ("Can't trampoline 0x%x", sig->params [i]->type);
                }
        }
 
-       cinfo->stack_usage = S390_ALIGN(sz->stack_size+sz->local_size, 
-                                       S390_STACK_ALIGNMENT);
+       /*----------------------------------------------------------*/
+       /* If we are passing a structure back then if it won't be   */
+       /* in a register(s) then we make room at the end of the     */
+       /* parameters that may have been placed on the stack        */
+       /*----------------------------------------------------------*/
+       if (cinfo->struct_ret) {
+               cinfo->ret.offset = sz->stack_size;
+               switch (cinfo->ret.size) {
+               case 0:
+               case 1:
+               case 2:
+               case 4:
+               case 8:
+                       break;
+               default:
+                       sz->stack_size   += S390_ALIGN(cinfo->ret.size, align);
+               }
+       }
+
+       sz->stack_size  = sz->stack_size + sz->local_size + sz->parm_size + 
+                         sz->offset;
+       sz->stack_size  = S390_ALIGN(sz->stack_size, sizeof(long));
+
        return (cinfo);
 }
 
@@ -1355,7 +1623,7 @@ enum_retvalue:
 /*------------------------------------------------------------------*/
 
 void
-mono_arch_allocate_vars (MonoCompile *m)
+mono_arch_allocate_vars (MonoCompile *cfg)
 {
        MonoMethodSignature *sig;
        MonoMethodHeader *header;
@@ -1366,7 +1634,9 @@ mono_arch_allocate_vars (MonoCompile *m)
        int frame_reg = STK_BASE;
        int sArg, eArg;
 
-       header  = ((MonoMethodNormal *)m->method)->header;
+       header  = mono_method_get_header (cfg->method);
+
+       cfg->flags |= MONO_CFG_HAS_SPILLUP;
 
        /*---------------------------------------------------------*/    
        /* We use the frame register also for any method that has  */ 
@@ -1378,53 +1648,55 @@ mono_arch_allocate_vars (MonoCompile *m)
        /* before stack unwinding happens) when the filter code    */
        /* would call any method.                                  */
        /*---------------------------------------------------------*/    
-       if ((m->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
+       if ((cfg->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
                frame_reg = s390_r11;
 
-       m->frame_reg = frame_reg;
+       cfg->frame_reg = frame_reg;
 
        if (frame_reg != STK_BASE) 
-               m->used_int_regs |= 1 << frame_reg;             
+               cfg->used_int_regs |= 1 << frame_reg;           
 
-       sig     = m->method->signature;
+       sig     = mono_method_signature (cfg->method);
        
        cinfo   = calculate_sizes (sig, &sz, sig->pinvoke);
 
        if (cinfo->struct_ret) {
-               m->ret->opcode = OP_REGVAR;
-               m->ret->inst_c0 = s390_r2;
+               cfg->ret->opcode = OP_REGVAR;
+               cfg->ret->inst_c0 = s390_r2;
        } else {
-               /* FIXME: handle long and FP values */
-               switch (sig->ret->type) {
+               switch (mono_type_get_underlying_type (sig->ret)->type) {
                case MONO_TYPE_VOID:
                        break;
                default:
-                       m->ret->opcode = OP_REGVAR;
-                       m->ret->dreg    = s390_r2;
+                       cfg->ret->opcode = OP_REGVAR;
+                       cfg->ret->dreg   = s390_r2;
                        break;
                }
        }
 
        /*--------------------------------------------------------------*/
        /* local vars are at a positive offset from the stack pointer   */
-       /*                                                              */
        /* also note that if the function uses alloca, we use s390_r11  */
        /* to point at the local variables.                             */
        /* add parameter area size for called functions                 */
        /*--------------------------------------------------------------*/
-       offset = (m->param_area + S390_MINIMAL_STACK_SIZE);
+       offset          = (cfg->param_area + S390_MINIMAL_STACK_SIZE);
+       cfg->sig_cookie = 0;
 
        if (cinfo->struct_ret) {
-               inst               = m->ret;
+               inst               = cfg->ret;
                offset             = S390_ALIGN(offset, sizeof(gpointer));
                inst->inst_offset  = offset;
                inst->opcode       = OP_REGOFFSET;
                inst->inst_basereg = frame_reg;
                offset            += sizeof(gpointer);
+               if ((sig->call_convention == MONO_CALL_VARARG) &&
+                   (!retFitsInReg (cinfo->ret.size)))
+                       cfg->sig_cookie += cinfo->ret.size;
        }
 
        if (sig->hasthis) {
-               inst = m->varinfo [0];
+               inst = cfg->varinfo [0];
                if (inst->opcode != OP_REGVAR) {
                        inst->opcode       = OP_REGOFFSET;
                        inst->inst_basereg = frame_reg;
@@ -1439,16 +1711,29 @@ mono_arch_allocate_vars (MonoCompile *m)
 
        eArg = sig->param_count + sArg;
 
+       if (sig->call_convention == MONO_CALL_VARARG)
+               cfg->sig_cookie += S390_MINIMAL_STACK_SIZE;
+
        for (iParm = sArg; iParm < eArg; ++iParm) {
-               inst = m->varinfo [curinst];
+               inst = cfg->varinfo [curinst];
                if (inst->opcode != OP_REGVAR) {
                        switch (cinfo->args[iParm].regtype) {
                                case RegTypeStructByAddr :
+                               if (cinfo->args[iParm].reg == STK_BASE) {
                                        inst->opcode       = OP_S390_LOADARG;
                                        inst->inst_basereg = frame_reg;
                                        size               = abs(cinfo->args[iParm].vtsize);
-                                       offset             = S390_ALIGN(offset, size);
+                                       offset             = S390_ALIGN(offset, sizeof(long));
                                        inst->inst_offset  = offset; 
+                                       inst->unused       = cinfo->args[iParm].offset;
+                               } else {
+                                       inst->opcode       = OP_S390_ARGREG;
+                                       inst->inst_basereg = frame_reg;
+                                       size               = sizeof(gpointer);
+                                       offset             = S390_ALIGN(offset, size);
+                                       inst->inst_offset  = offset;
+                                       inst->unused       = cinfo->args[iParm].offset;
+                               }
                                        break;
                                case RegTypeStructByVal :
                                        inst->opcode       = OP_S390_ARGPTR;
@@ -1456,6 +1741,7 @@ mono_arch_allocate_vars (MonoCompile *m)
                                        size               = cinfo->args[iParm].size;
                                        offset             = S390_ALIGN(offset, size);
                                        inst->inst_offset  = offset;
+                                       inst->unused       = cinfo->args[iParm].offset;
                                        break;
                                default :
                                if (cinfo->args[iParm].reg != STK_BASE) {
@@ -1478,15 +1764,21 @@ mono_arch_allocate_vars (MonoCompile *m)
                                        size               = sizeof(long);
                                } 
                        }
+                       if ((sig->call_convention == MONO_CALL_VARARG) && 
+                           (cinfo->args[iParm].regtype != RegTypeGeneral) &&
+                           (iParm < sig->sentinelpos)) 
+                               cfg->sig_cookie += size;
+
                        offset += size;
                }
                curinst++;
        }
 
-       curinst = m->locals_start;
-       for (iVar = curinst; iVar < m->num_varinfo; ++iVar) {
-               inst = m->varinfo [iVar];
-               if (inst->opcode == OP_REGVAR)
+       curinst = cfg->locals_start;
+       for (iVar = curinst; iVar < cfg->num_varinfo; ++iVar) {
+               inst = cfg->varinfo [iVar];
+               if ((inst->flags & MONO_INST_IS_DEAD) || 
+                   (inst->opcode == OP_REGVAR))
                        continue;
 
                /*--------------------------------------------------*/
@@ -1495,7 +1787,7 @@ mono_arch_allocate_vars (MonoCompile *m)
                /* call functions returning structure               */
                /*--------------------------------------------------*/
                if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype))
-                       size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
+                       size = mono_class_native_size (mono_class_from_mono_type(inst->inst_vtype), &align);
                else
                        size = mono_type_size (inst->inst_vtype, &align);
 
@@ -1504,25 +1796,26 @@ mono_arch_allocate_vars (MonoCompile *m)
                inst->opcode       = OP_REGOFFSET;
                inst->inst_basereg = frame_reg;
                offset            += size;
-               //DEBUG (g_print("allocating local %d to %d\n", iVar, inst->inst_offset));
+               DEBUG (g_print("allocating local %d to %ld\n", iVar, inst->inst_offset));
        }
 
        /*------------------------------------------------------*/
        /* Allow space for the trace method stack area if needed*/
        /*------------------------------------------------------*/
-       if (mono_jit_trace_calls != NULL && mono_trace_eval (m)) 
+       if (mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) {
                offset += S390_TRACE_STACK_SIZE;
+       }
 
        /*------------------------------------------------------*/
        /* Reserve space to save LMF and caller saved registers */
        /*------------------------------------------------------*/
-       if (m->method->save_lmf)
+       if (cfg->method->save_lmf)
                offset += sizeof (MonoLMF);
 
        /*------------------------------------------------------*/
        /* align the offset                                     */
        /*------------------------------------------------------*/
-       m->stack_offset = S390_ALIGN(offset, S390_STACK_ALIGNMENT);
+       cfg->stack_offset = S390_ALIGN(offset, S390_STACK_ALIGNMENT);
 
 }
 
@@ -1537,22 +1830,19 @@ mono_arch_allocate_vars (MonoCompile *m)
 /*               includes pushing, moving argments to the correct  */
 /*               etc.                                              */
 /*                                                                 */
-/* Note         - FIXME: We need an alignment solution for         */
-/*               enter_method and mono_arch_call_opcode, currently */
-/*               alignment in mono_arch_call_opcode is computed    */
-/*               without arch_get_argument_info.                   */
-/*                                                                 */
 /*------------------------------------------------------------------*/
 
 MonoCallInst*
 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, 
                       MonoCallInst *call, int is_virtual) {
-       MonoInst *arg, *in;
+       MonoInst *in;
+       MonoCallArgParm *arg;
        MonoMethodSignature *sig;
        int i, n, lParamArea;
        CallInfo *cinfo;
        ArgInfo *ainfo;
        size_data sz;
+       int stackSize;
 
        sig = call->signature;
        n = sig->param_count + sig->hasthis;
@@ -1560,43 +1850,62 @@ mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb,
        
        cinfo = calculate_sizes (sig, &sz, sig->pinvoke);
 
-       call->stack_usage = cinfo->stack_usage;
-       lParamArea        = cinfo->stack_usage - S390_MINIMAL_STACK_SIZE;
-       cfg->param_area   = MAX (cfg->param_area, lParamArea);
+       stackSize         = sz.stack_size + sz.local_size + sz.parm_size + sz.offset;
+       call->stack_usage = MAX(stackSize, call->stack_usage);
+       lParamArea        = MAX((call->stack_usage-S390_MINIMAL_STACK_SIZE-sz.parm_size), 0);
+       cfg->param_area   = MAX(((signed) cfg->param_area), lParamArea);
        cfg->flags       |= MONO_CFG_HAS_CALLS;
 
        if (cinfo->struct_ret)
-               call->used_iregs |= 1 << cinfo->struct_ret;
+               call->used_iregs |= 1 << cinfo->ret.reg;
 
        for (i = 0; i < n; ++i) {
                ainfo = cinfo->args + i;
-               DEBUG (g_print ("Parameter %d - Register: %d Type: %d\n",
-                               i+1,ainfo->reg,ainfo->regtype));
+
+               if ((sig->call_convention == MONO_CALL_VARARG) &&
+                   (i == sig->sentinelpos)) {
+                       MonoInst *sigArg;
+                       
+                       cfg->disable_aot = TRUE;
+                       MONO_INST_NEW (cfg, sigArg, OP_ICONST);
+                       sigArg->inst_p0 = call->signature;
+
+                       MONO_INST_NEW_CALL_ARG (cfg, arg, OP_OUTARG_MEMBASE);
+                       arg->ins.inst_left  = sigArg;
+                       arg->ins.inst_right = (MonoInst *) call;
+                       arg->size           = ainfo->size;
+                       arg->offset         = cinfo->sigCookie.offset;
+                       call->used_iregs   |= 1 << ainfo->reg;
+                       arg->ins.next       = call->out_args;
+                       call->out_args      = (MonoInst *) arg;
+               }
+
                if (is_virtual && i == 0) {
                        /* the argument will be attached to the call instrucion */
                        in = call->args [i];
                        call->used_iregs |= 1 << ainfo->reg;
                } else {
-                       MONO_INST_NEW (cfg, arg, OP_OUTARG);
-                       in = call->args [i];
-                       arg->cil_code  = in->cil_code;
-                       arg->inst_left = in;
-                       arg->type      = in->type;
+                       MONO_INST_NEW_CALL_ARG (cfg, arg, OP_OUTARG);
+                       in                  = call->args [i];
+                       arg->ins.cil_code   = in->cil_code;
+                       arg->ins.inst_left  = in;
+                       arg->ins.type       = in->type;
                        /* prepend, we'll need to reverse them later */
-                       arg->next      = call->out_args;
-                       call->out_args = arg;
+                       arg->ins.next       = call->out_args;
+                       call->out_args      = (MonoInst *) arg;
+                       arg->ins.inst_right = (MonoInst *) call;
                        if (ainfo->regtype == RegTypeGeneral) {
-                               arg->unused = ainfo->reg;
+                               arg->ins.unused   = ainfo->reg;
                                call->used_iregs |= 1 << ainfo->reg;
-                               if (arg->type == STACK_I8)
+                               if (arg->ins.type == STACK_I8)
                                        call->used_iregs |= 1 << (ainfo->reg + 1);
                        } else if (ainfo->regtype == RegTypeStructByAddr) {
                                call->used_iregs |= 1 << ainfo->reg;
-                               arg->sreg1     = ainfo->reg;
-                               arg->opcode    = OP_OUTARG_VT;
-                               arg->unused    = -ainfo->vtsize;
-                               arg->inst_imm  = ainfo->offset;
-                               arg->sreg2     = ainfo->offparm + S390_MINIMAL_STACK_SIZE;
+                               arg->ins.sreg1    = ainfo->reg;
+                               arg->ins.opcode   = OP_OUTARG_VT;
+                               arg->size         = -ainfo->vtsize;
+                               arg->offset       = ainfo->offset;
+                               arg->offPrm       = ainfo->offparm + sz.offStruct;
                        } else if (ainfo->regtype == RegTypeStructByVal) {
                                if (ainfo->reg != STK_BASE) {
                                        switch (ainfo->size) {
@@ -1614,28 +1923,24 @@ mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb,
                                                call->used_iregs |= 1 << ainfo->reg;
                                        }
                                } 
-                               arg->sreg1     = ainfo->reg;
-                               arg->opcode    = OP_OUTARG_VT;
-                               arg->unused    = ainfo->size;
-                               arg->inst_imm  = ainfo->offset;
-                               arg->sreg2     = ainfo->offparm + S390_MINIMAL_STACK_SIZE;
+                               arg->ins.sreg1  = ainfo->reg;
+                               arg->ins.opcode = OP_OUTARG_VT;
+                               arg->size       = ainfo->size;
+                               arg->offset     = ainfo->offset;
+                               arg->offPrm     = ainfo->offparm + sz.offStruct;
                        } else if (ainfo->regtype == RegTypeBase) {
-                               arg->opcode = OP_OUTARG;
-                               arg->unused = ainfo->reg | (ainfo->size << 8);
-                               arg->inst_imm = ainfo->offset;
-                               call->used_fregs |= 1 << ainfo->reg;
+                               arg->ins.opcode   = OP_OUTARG_MEMBASE;
+                               arg->ins.sreg1    = ainfo->reg;
+                               arg->size         = ainfo->size;
+                               arg->offset       = ainfo->offset;
+                               call->used_iregs |= 1 << ainfo->reg;
                        } else if (ainfo->regtype == RegTypeFP) {
-                               arg->unused = ainfo->reg;
+                               arg->ins.unused   = ainfo->reg;
                                call->used_fregs |= 1 << ainfo->reg;
-                               if (ainfo->size == 4) {
-                                       MonoInst *conv;
-                                       arg->opcode     = OP_OUTARG_R4;
-                                       MONO_INST_NEW (cfg, conv, OP_FCONV_TO_R4);
-                                       conv->inst_left = arg->inst_left;
-                                       arg->inst_left  = conv;
-                               }
+                               if (ainfo->size == 4)
+                                       arg->ins.opcode = OP_OUTARG_R4;
                                else
-                                       arg->opcode = OP_OUTARG_R8;
+                                       arg->ins.opcode = OP_OUTARG_R8;
                        } else {
                                g_assert_not_reached ();
                        }
@@ -1693,33 +1998,45 @@ void*
 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, 
                             gboolean enable_arguments)
 {
-       guchar *code = p;
+       guchar  *code = p;
        int     parmOffset, 
-               fpOffset;
+               fpOffset,
+               baseReg;
 
        parmOffset = cfg->stack_usage - S390_TRACE_STACK_SIZE;
        if (cfg->method->save_lmf)
                parmOffset -= sizeof(MonoLMF);
        fpOffset   = parmOffset + (5*sizeof(gint32));
+       if (fpOffset > 4096) {
+               s390_lr (code, s390_r12, STK_BASE);
+               baseReg = s390_r12;
+               while (fpOffset > 4096) {
+                       s390_ahi (code, baseReg, 4096);
+                       fpOffset   -= 4096;
+                       parmOffset -= 4096;
+               }
+       } else {
+               baseReg = STK_BASE;
+       }       
 
-       s390_stm  (code, s390_r2, s390_r6, STK_BASE, parmOffset);
-       s390_std  (code, s390_f0, 0, STK_BASE, fpOffset);
-       s390_std  (code, s390_f1, 0, STK_BASE, fpOffset+sizeof(gdouble));
-       s390_std  (code, s390_f2, 0, STK_BASE, fpOffset+2*sizeof(gdouble));
+       s390_stm  (code, s390_r2, s390_r6, baseReg, parmOffset);
+       s390_std  (code, s390_f0, 0, baseReg, fpOffset);
+       s390_std  (code, s390_f1, 0, baseReg, fpOffset+sizeof(gdouble));
+       s390_std  (code, s390_f2, 0, baseReg, fpOffset+2*sizeof(gdouble));
        s390_basr (code, s390_r13, 0);
        s390_j    (code, 6);
        s390_word (code, cfg->method);
        s390_word (code, func);
        s390_l    (code, s390_r2, 0, s390_r13, 4);
-       s390_la   (code, s390_r3, 0, STK_BASE, parmOffset);
+       s390_la   (code, s390_r3, 0, baseReg, parmOffset);
        s390_lr   (code, s390_r4, STK_BASE);
        s390_ahi  (code, s390_r4, cfg->stack_usage);
        s390_l    (code, s390_r1, 0, s390_r13, 8);
        s390_basr (code, s390_r14, s390_r1);
-       s390_ld   (code, s390_f2, 0, STK_BASE, fpOffset+2*sizeof(gdouble));
-       s390_ld   (code, s390_f1, 0, STK_BASE, fpOffset+sizeof(gdouble));
-       s390_ld   (code, s390_f0, 0, STK_BASE, fpOffset);
-       s390_lm   (code, s390_r2, s390_r6, STK_BASE, parmOffset);
+       s390_ld   (code, s390_f2, 0, baseReg, fpOffset+2*sizeof(gdouble));
+       s390_ld   (code, s390_f1, 0, baseReg, fpOffset+sizeof(gdouble));
+       s390_ld   (code, s390_f0, 0, baseReg, fpOffset);
+       s390_lm   (code, s390_r2, s390_r6, baseReg, parmOffset);
 
        return code;
 }
@@ -1742,7 +2059,7 @@ mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean ena
        int        save_mode = SAVE_NONE,
                   saveOffset;
        MonoMethod *method = cfg->method;
-       int        rtype = method->signature->ret->type;
+       int        rtype = mono_type_get_underlying_type (mono_method_signature (method)->ret)->type;
 
        saveOffset = cfg->stack_usage - S390_TRACE_STACK_SIZE;
        if (method->save_lmf)
@@ -1762,12 +2079,14 @@ handle_enum:
                save_mode = SAVE_TWO;
                break;
        case MONO_TYPE_R4:
+               save_mode = SAVE_R4;
+               break;
        case MONO_TYPE_R8:
-               save_mode = SAVE_FP;
+               save_mode = SAVE_R8;
                break;
        case MONO_TYPE_VALUETYPE:
-               if (method->signature->ret->data.klass->enumtype) {
-                       rtype = method->signature->ret->data.klass->enum_basetype->type;
+               if (mono_method_signature (method)->ret->data.klass->enumtype) {
+                       rtype = mono_method_signature (method)->ret->data.klass->enum_basetype->type;
                        goto handle_enum;
                }
                save_mode = SAVE_STRUCT;
@@ -1791,14 +2110,15 @@ handle_enum:
                        s390_lr (code, s390_r3, s390_r2);
                }
                break;
-       case SAVE_FP:
+       case SAVE_R4:
                s390_std (code, s390_f0, 0, cfg->frame_reg, saveOffset);
                if (enable_arguments) {
-                       /* FIXME: what reg?  */
-                       s390_ldr (code, s390_f2, s390_f0);
-                       s390_lm  (code, s390_r3, s390_r4, cfg->frame_reg, saveOffset);
+                       s390_ldebr (code, s390_f0, s390_f0);
                }
                break;
+       case SAVE_R8:
+               s390_std (code, s390_f0, 0, cfg->frame_reg, saveOffset);
+               break;
        case SAVE_STRUCT:
                s390_st (code, s390_r2, 0, cfg->frame_reg, saveOffset);
                if (enable_arguments) {
@@ -1826,7 +2146,8 @@ handle_enum:
        case SAVE_ONE:
                s390_l   (code, s390_r2, 0, cfg->frame_reg, saveOffset);
                break;
-       case SAVE_FP:
+       case SAVE_R4:
+       case SAVE_R8:
                s390_ld  (code, s390_f0, 0, cfg->frame_reg, saveOffset);
                break;
        case SAVE_STRUCT:
@@ -1887,7 +2208,6 @@ peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                                        ins = ins->next;                                
                                        continue;
                                } else {
-                                       //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
                                        ins->opcode = OP_MOVE;
                                        ins->sreg1 = last_ins->sreg1;
                                }
@@ -2000,539 +2320,6 @@ peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
 
 /*========================= End of Function ========================*/
 
-/*------------------------------------------------------------------*/
-/*                                                                  */
-/* Name                - mono_spillvar_offset                              */
-/*                                                                  */
-/* Function    - Returns the offset used by spillvar. It allocates */
-/*               a new spill variable if necessary.                */
-/*                                                                 */
-/*------------------------------------------------------------------*/
-
-static int
-mono_spillvar_offset (MonoCompile *cfg, int spillvar)
-{
-       MonoSpillInfo **si, *info;
-       int i = 0;
-
-       si = &cfg->spill_info; 
-       
-       while (i <= spillvar) {
-
-               if (!*si) {
-                       *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
-                       info->next = NULL;
-                       info->offset = cfg->stack_offset;
-                       cfg->stack_offset += sizeof (gpointer);
-               }
-
-               if (i == spillvar)
-                       return (*si)->offset;
-
-               i++;
-               si = &(*si)->next;
-       }
-
-       g_assert_not_reached ();
-       return 0;
-}
-
-/*========================= End of Function ========================*/
-
-/*------------------------------------------------------------------*/
-/*                                                                  */
-/* Name                - mono_spillvar_offset_float                        */
-/*                                                                  */
-/* Function    -                                                   */
-/*                                                                 */
-/*------------------------------------------------------------------*/
-
-static int
-mono_spillvar_offset_float (MonoCompile *cfg, int spillvar)
-{
-       MonoSpillInfo **si, *info;
-       int i = 0;
-
-       si = &cfg->spill_info_float; 
-       
-       while (i <= spillvar) {
-
-               if (!*si) {
-                       *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
-                       info->next         = NULL;
-                       cfg->stack_offset  = S390_ALIGN(cfg->stack_offset, S390_STACK_ALIGNMENT);
-                       info->offset       = cfg->stack_offset;
-                       cfg->stack_offset += sizeof (double);
-               }
-
-               if (i == spillvar)
-                       return (*si)->offset;
-
-               i++;
-               si = &(*si)->next;
-       }
-
-       g_assert_not_reached ();
-       return 0;
-}
-
-/*========================= End of Function ========================*/
-
-/*------------------------------------------------------------------*/
-/*                                                                  */
-/* Name                - print_ins                                         */
-/*                                                                  */
-/* Function    - Decode and print the instruction for tracing.     */
-/*                                                                 */
-/*------------------------------------------------------------------*/
-
-static void
-print_ins (int i, MonoInst *ins)
-{
-       const char *spec = ins_spec [ins->opcode];
-       g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
-       if (spec [MONO_INST_DEST]) {
-               if (ins->dreg >= MONO_MAX_IREGS)
-                       g_print (" R%d <-", ins->dreg);
-               else
-                       g_print (" %s <-", mono_arch_regname (ins->dreg));
-       }
-       if (spec [MONO_INST_SRC1]) {
-               if (ins->sreg1 >= MONO_MAX_IREGS)
-                       g_print (" R%d", ins->sreg1);
-               else
-                       g_print (" %s", mono_arch_regname (ins->sreg1));
-       }
-       if (spec [MONO_INST_SRC2]) {
-               if (ins->sreg2 >= MONO_MAX_IREGS)
-                       g_print (" R%d", ins->sreg2);
-               else
-                       g_print (" %s", mono_arch_regname (ins->sreg2));
-       }
-       if (spec [MONO_INST_CLOB])
-               g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
-       g_print ("\n");
-}
-
-/*========================= End of Function ========================*/
-
-/*------------------------------------------------------------------*/
-/*                                                                  */
-/* Name                - print_regtrack.                                   */
-/*                                                                  */
-/* Function    -                                                   */
-/*                                                                 */
-/*------------------------------------------------------------------*/
-
-static void
-print_regtrack (RegTrack *t, int num)
-{
-       int i;
-       char buf [32];
-       const char *r;
-       
-       for (i = 0; i < num; ++i) {
-               if (!t [i].born_in)
-                       continue;
-               if (i >= MONO_MAX_IREGS) {
-                       g_snprintf (buf, sizeof(buf), "R%d", i);
-                       r = buf;
-               } else
-                       r = mono_arch_regname (i);
-               g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
-       }
-}
-
-/*========================= End of Function ========================*/
-
-/*------------------------------------------------------------------*/
-/*                                                                  */
-/* Name                - inst_list_prepend                                 */
-/*                                                                  */
-/* Function    - Prepend an instruction to the list.               */
-/*                                                                 */
-/*------------------------------------------------------------------*/
-
-static inline InstList*
-inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
-{
-       InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
-       item->data = data;
-       item->prev = NULL;
-       item->next = list;
-       if (list)
-               list->prev = item;
-       return item;
-}
-
-/*========================= End of Function ========================*/
-
-/*------------------------------------------------------------------*/
-/*                                                                  */
-/* Name                - get_register_force_spilling                       */
-/*                                                                  */
-/* Function    - Force the spilling of the variable in the         */
-/*               symbolic register 'reg'.                          */
-/*                                                                 */
-/*------------------------------------------------------------------*/
-
-static int
-get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
-{
-       MonoInst *load;
-       int i, sel, spill;
-       
-       sel = cfg->rs->iassign [reg];
-       i = reg;
-       spill = ++cfg->spill_count;
-       cfg->rs->iassign [i] = -spill - 1;
-       mono_regstate_free_int (cfg->rs, sel);
-       /*----------------------------------------------------------*/
-       /* we need to create a spill var and insert a load to sel   */
-       /* after the current instruction                            */
-       /*----------------------------------------------------------*/
-       MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
-       load->dreg = sel;
-       load->inst_basereg = cfg->frame_reg;
-       load->inst_offset = mono_spillvar_offset (cfg, spill);
-       if (item->prev) {
-               while (ins->next != item->prev->data)
-                       ins = ins->next;
-       }
-       load->next = ins->next;
-       ins->next  = load;
-       DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", 
-                       spill, load->inst_offset, i, mono_arch_regname (sel)));
-       i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
-       g_assert (i == sel);
-
-       return sel;
-}
-
-/*========================= End of Function ========================*/
-
-/*------------------------------------------------------------------*/
-/*                                                                  */
-/* Name                -  get_register_spilling                            */
-/*                                                                  */
-/* Function    -                                                   */
-/*                                                                 */
-/*------------------------------------------------------------------*/
-
-static int
-get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
-{
-       MonoInst *load;
-       int i, sel, spill;
-
-       DEBUG (g_print ("start regmask to assign R%d: 0x%08x (R%d <- R%d R%d)\n", reg, regmask, ins->dreg, ins->sreg1, ins->sreg2));
-       /* exclude the registers in the current instruction */
-       if (reg != ins->sreg1 && 
-           (reg_is_freeable (ins->sreg1) || 
-            (ins->sreg1 >= MONO_MAX_IREGS && 
-             cfg->rs->iassign [ins->sreg1] >= 0))) {
-               if (ins->sreg1 >= MONO_MAX_IREGS)
-                       regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
-               else
-                       regmask &= ~ (1 << ins->sreg1);
-               DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
-       }
-       if (reg != ins->sreg2 && 
-           (reg_is_freeable (ins->sreg2) || 
-             (ins->sreg2 >= MONO_MAX_IREGS && 
-              cfg->rs->iassign [ins->sreg2] >= 0))) {
-               if (ins->sreg2 >= MONO_MAX_IREGS)
-                       regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
-               else
-                       regmask &= ~ (1 << ins->sreg2);
-               DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
-       }
-       if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
-               regmask &= ~ (1 << ins->dreg);
-               DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
-       }
-
-       DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
-       g_assert (regmask); /* need at least a register we can free */
-       sel = -1;
-       /* we should track prev_use and spill the register that's farther */
-       for (i = 0; i < MONO_MAX_IREGS; ++i) {
-               if (regmask & (1 << i)) {
-                       sel = i;
-                       DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
-                       break;
-               }
-       }
-       i = cfg->rs->isymbolic [sel];
-       spill = ++cfg->spill_count;
-       cfg->rs->iassign [i] = -spill - 1;
-       mono_regstate_free_int (cfg->rs, sel);
-       /* we need to create a spill var and insert a load to sel after the current instruction */
-       MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
-       load->dreg = sel;
-       load->inst_basereg = cfg->frame_reg;
-       load->inst_offset = mono_spillvar_offset (cfg, spill);
-       if (item->prev) {
-               while (ins->next != item->prev->data)
-                       ins = ins->next;
-       }
-       load->next = ins->next;
-       ins->next = load;
-       DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
-       i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
-       g_assert (i == sel);
-       
-       return sel;
-}
-
-/*========================= End of Function ========================*/
-
-/*------------------------------------------------------------------*/
-/*                                                                  */
-/* Name                - get_float_register_spilling                       */
-/*                                                                  */
-/* Function    -                                                   */
-/*                                                                 */
-/*------------------------------------------------------------------*/
-
-static int
-get_float_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
-{
-       MonoInst *load;
-       int i, sel, spill;
-
-       DEBUG (g_print ("start regmask to assign R%d: 0x%08x (R%d <- R%d R%d)\n", reg, regmask, ins->dreg, ins->sreg1, ins->sreg2));
-       /* exclude the registers in the current instruction */
-       if (reg != ins->sreg1 && 
-           (freg_is_freeable (ins->sreg1) || 
-             (ins->sreg1 >= MONO_MAX_FREGS && 
-              cfg->rs->fassign [ins->sreg1] >= 0))) {
-               if (ins->sreg1 >= MONO_MAX_FREGS)
-                       regmask &= ~ (1 << cfg->rs->fassign [ins->sreg1]);
-               else
-                       regmask &= ~ (1 << ins->sreg1);
-               DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
-       }
-       if (reg != ins->sreg2 && 
-            (freg_is_freeable (ins->sreg2) || 
-             (ins->sreg2 >= MONO_MAX_FREGS &&
-              cfg->rs->fassign [ins->sreg2] >= 0))) {
-               if (ins->sreg2 >= MONO_MAX_FREGS)
-                       regmask &= ~ (1 << cfg->rs->fassign [ins->sreg2]);
-               else
-                       regmask &= ~ (1 << ins->sreg2);
-               DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
-       }
-       if (reg != ins->dreg && freg_is_freeable (ins->dreg)) {
-               regmask &= ~ (1 << ins->dreg);
-               DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
-       }
-
-       DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
-       g_assert (regmask); /* need at least a register we can free */
-       sel = -1;
-       /* we should track prev_use and spill the register that's farther */
-       for (i = 0; i < MONO_MAX_FREGS; ++i) {
-               if (regmask & (1 << i)) {
-                       sel = i;
-                       DEBUG (g_print ("selected register %s has assignment %d\n", 
-                                       mono_arch_regname (sel), cfg->rs->fassign [sel]));
-                       break;
-               }
-       }
-       i = cfg->rs->fsymbolic [sel];
-       spill = ++cfg->spill_count;
-       cfg->rs->fassign [i] = -spill - 1;
-       mono_regstate_free_float(cfg->rs, sel);
-       /* we need to create a spill var and insert a load to sel after the current instruction */
-       MONO_INST_NEW (cfg, load, OP_LOADR8_MEMBASE);
-       load->dreg = sel;
-       load->inst_basereg = cfg->frame_reg;
-       load->inst_offset = mono_spillvar_offset_float (cfg, spill);
-       if (item->prev) {
-               while (ins->next != item->prev->data)
-                       ins = ins->next;
-       }
-       load->next = ins->next;
-       ins->next = load;
-       DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
-       i = mono_regstate_alloc_float (cfg->rs, 1 << sel);
-       g_assert (i == sel);
-       
-       return sel;
-}
-
-/*========================= End of Function ========================*/
-
-/*------------------------------------------------------------------*/
-/*                                                                  */
-/* Name                - create_copy_ins                                   */
-/*                                                                  */
-/* Function    - Create an instruction to copy from reg to reg.    */
-/*                                                                 */
-/*------------------------------------------------------------------*/
-
-static MonoInst*
-create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
-{
-       MonoInst *copy;
-       MONO_INST_NEW (cfg, copy, OP_MOVE);
-       copy->dreg = dest;
-       copy->sreg1 = src;
-       if (ins) {
-               copy->next = ins->next;
-               ins->next = copy;
-       }
-       DEBUG (g_print ("\tforced copy from %s to %s\n", 
-                       mono_arch_regname (src), mono_arch_regname (dest)));
-       return copy;
-}
-
-/*========================= End of Function ========================*/
-
-/*------------------------------------------------------------------*/
-/*                                                                  */
-/* Name                - create_copy_ins_float                             */
-/*                                                                  */
-/* Function    - Create an instruction to copy from float reg to   */
-/*               float reg.                                        */
-/*                                                                 */
-/*------------------------------------------------------------------*/
-
-static MonoInst*
-create_copy_ins_float (MonoCompile *cfg, int dest, int src, MonoInst *ins)
-{
-       MonoInst *copy;
-       MONO_INST_NEW (cfg, copy, OP_FMOVE);
-       copy->dreg = dest;
-       copy->sreg1 = src;
-       if (ins) {
-               copy->next = ins->next;
-               ins->next = copy;
-       }
-       DEBUG (g_print ("\tforced copy from %s to %s\n", 
-                       mono_arch_regname (src), mono_arch_regname (dest)));
-       return copy;
-}
-
-/*========================= End of Function ========================*/
-
-/*------------------------------------------------------------------*/
-/*                                                                  */
-/* Name                - create_spilled_store                              */
-/*                                                                  */
-/* Function    - Spill register to storage.                        */
-/*                                                                 */
-/*------------------------------------------------------------------*/
-
-static MonoInst*
-create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
-{
-       MonoInst *store;
-       MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
-       store->sreg1 = reg;
-       store->inst_destbasereg = cfg->frame_reg;
-       store->inst_offset = mono_spillvar_offset (cfg, spill);
-       if (ins) {
-               store->next = ins->next;
-               ins->next = store;
-       }
-       DEBUG (g_print ("SPILLED STORE (%d at 0x%08x(%%sp)) R%d (from %s)\n", 
-                       spill, store->inst_offset, prev_reg, mono_arch_regname (reg)));
-       return store;
-}
-
-/*========================= End of Function ========================*/
-
-/*------------------------------------------------------------------*/
-/*                                                                  */
-/* Name                - create_spilled_store_float                        */
-/*                                                                  */
-/* Function    - Spill floating point register to storage.         */
-/*                                                                 */
-/*------------------------------------------------------------------*/
-
-static MonoInst*
-create_spilled_store_float (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
-{
-       MonoInst *store;
-       MONO_INST_NEW (cfg, store, OP_STORER8_MEMBASE_REG);
-       store->sreg1 = reg;
-       store->inst_destbasereg = cfg->frame_reg;
-       store->inst_offset = mono_spillvar_offset_float (cfg, spill);
-       if (ins) {
-               store->next = ins->next;
-               ins->next = store;
-       }
-       DEBUG (g_print ("SPILLED STORE (%d at 0x%08x(%%sp)) R%d (from %s)\n", 
-                       spill, store->inst_offset, prev_reg, mono_arch_regname (reg)));
-       return store;
-}
-
-/*========================= End of Function ========================*/
-
-/*------------------------------------------------------------------*/
-/*                                                                  */
-/* Name                - insert_before_ins                                 */
-/*                                                                  */
-/* Function    - Insert an instruction before another.             */
-/*                                                                 */
-/*------------------------------------------------------------------*/
-
-static void
-insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
-{
-       MonoInst *prev;
-       g_assert (item->next);
-       prev = item->next->data;
-
-       while (prev->next != ins)
-               prev = prev->next;
-       to_insert->next = ins;
-       prev->next = to_insert;
-       /* 
-        * needed otherwise in the next instruction we can add an ins to the 
-        * end and that would get past this instruction.
-        */
-       item->data = to_insert; 
-}
-
-/*========================= End of Function ========================*/
-
-/*------------------------------------------------------------------*/
-/*                                                                  */
-/* Name                - alloc_int_reg                                     */
-/*                                                                  */
-/* Function    - Allocate a general register.                      */
-/*                                                                 */
-/*------------------------------------------------------------------*/
-
-static int
-alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
-{
-       int val = cfg->rs->iassign [sym_reg];
-       DEBUG (g_print ("Allocating a general register for %d (%d) with mask %08x\n",val,sym_reg,allow_mask));
-       if (val < 0) {
-               int spill = 0;
-               if (val < -1) {
-                       /* the register gets spilled after this inst */
-                       spill = -val -1;
-               }
-               val = mono_regstate_alloc_int (cfg->rs, allow_mask);
-               if (val < 0)
-                       val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
-               cfg->rs->iassign [sym_reg] = val;
-               /* add option to store before the instruction for src registers */
-               if (spill)
-                       create_spilled_store (cfg, spill, val, sym_reg, ins);
-       }
-       DEBUG (g_print ("Allocated %d for %d\n",val,sym_reg));
-       cfg->rs->isymbolic [val] = sym_reg;
-       return val;
-}
-
-/*========================= End of Function ========================*/
-
 /*------------------------------------------------------------------*/
 /*                                                                  */
 /* Name                - mono_arch_local_regalloc.                         */
@@ -2550,388 +2337,7 @@ alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg,
 void
 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
 {
-       MonoInst *ins;
-       MonoRegState *rs = cfg->rs;
-       int i, val;
-       RegTrack *reginfo, *reginfof;
-       RegTrack *reginfo1, *reginfo2, *reginfod;
-       InstList *tmp, *reversed = NULL;
-       const char *spec;
-       guint32 src1_mask, src2_mask, dest_mask;
-       guint32 cur_iregs, cur_fregs;
-
-       if (!bb->code)
-               return;
-       rs->next_vireg = bb->max_ireg;
-       rs->next_vfreg = bb->max_freg;
-       mono_regstate_assign (rs);
-       reginfo = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vireg);
-       reginfof = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vfreg);
-       rs->ifree_mask = S390_CALLER_REGS;
-       rs->ffree_mask = S390_CALLER_FREGS;
-
-       ins = bb->code;
-       i = 1;
-       DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
-       /* forward pass on the instructions to collect register liveness info */
-       while (ins) {
-               spec = ins_spec [ins->opcode];
-               DEBUG (print_ins (i, ins));
-               if (spec [MONO_INST_CLOB] == 'c') {
-                       MonoCallInst * call = (MonoCallInst*)ins;
-                       int j;
-               }
-               if (spec [MONO_INST_SRC1]) {
-                       if (spec [MONO_INST_SRC1] == 'f')
-                               reginfo1 = reginfof;
-                       else
-                               reginfo1 = reginfo;
-                       reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
-                       reginfo1 [ins->sreg1].last_use = i;
-               } else {
-                       ins->sreg1 = -1;
-               }
-               if (spec [MONO_INST_SRC2]) {
-                       if (spec [MONO_INST_SRC2] == 'f')
-                               reginfo2 = reginfof;
-                       else
-                               reginfo2 = reginfo;
-                       reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
-                       reginfo2 [ins->sreg2].last_use = i;
-               } else {
-                       ins->sreg2 = -1;
-               }
-               if (spec [MONO_INST_DEST]) {
-                       if (spec [MONO_INST_DEST] == 'f')
-                               reginfod = reginfof;
-                       else
-                               reginfod = reginfo;
-                       if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
-                               reginfod [ins->dreg].killed_in = i;
-                       reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
-                       reginfod [ins->dreg].last_use = i;
-                       if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
-                               reginfod [ins->dreg].born_in = i;
-                       if (spec [MONO_INST_DEST] == 'l') {
-                               /* result in eax:edx, the virtual register is allocated sequentially */
-                               reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
-                               reginfod [ins->dreg + 1].last_use = i;
-                               if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
-                                       reginfod [ins->dreg + 1].born_in = i;
-                       }
-               } else {
-                       ins->dreg = -1;
-               }
-               reversed = inst_list_prepend (cfg->mempool, reversed, ins);
-               ++i;
-               ins = ins->next;
-       }
-
-       cur_iregs = S390_CALLER_REGS;
-       cur_fregs = S390_CALLER_FREGS;
-
-       DEBUG (print_regtrack (reginfo, rs->next_vireg));
-       DEBUG (print_regtrack (reginfof, rs->next_vfreg));
-       tmp = reversed;
-       while (tmp) {
-               int prev_dreg, prev_sreg1, prev_sreg2;
-               --i;
-               ins = tmp->data;
-               spec = ins_spec [ins->opcode];
-               DEBUG (g_print ("processing:"));
-               DEBUG (print_ins (i, ins));
-               /* make the register available for allocation: FIXME add fp reg */
-               if (ins->opcode == OP_SETREG || ins->opcode == OP_SETREGIMM) {
-                       cur_iregs |= 1 << ins->dreg;
-                       DEBUG (g_print ("adding %d to cur_iregs\n", ins->dreg));
-               } else if (ins->opcode == OP_SETFREG) {
-                       cur_fregs |= 1 << ins->dreg;
-                       DEBUG (g_print ("adding %d to cur_fregs\n", ins->dreg));
-               } else if (spec [MONO_INST_CLOB] == 'c') {
-                       MonoCallInst *cinst = (MonoCallInst*)ins;
-                       DEBUG (g_print ("excluding regs 0x%x from cur_iregs (0x%x)\n", 
-                                       cinst->used_iregs, cur_iregs));
-                       DEBUG (g_print ("excluding fpregs 0x%x from cur_fregs (0x%x)\n", 
-                                       cinst->used_fregs, cur_fregs));
-                       cur_iregs &= ~cinst->used_iregs;
-                       cur_fregs &= ~cinst->used_fregs;
-                       DEBUG (g_print ("available cur_iregs: 0x%x\n", cur_iregs));
-                       DEBUG (g_print ("available cur_fregs: 0x%x\n", cur_fregs));
-                       /*------------------------------------------------------------*/
-                       /* registers used by the calling convention are excluded from */ 
-                       /* allocation: they will be selectively enabled when they are */ 
-                       /* assigned by the special SETREG opcodes.                    */
-                       /*------------------------------------------------------------*/
-               }
-               dest_mask = src1_mask = src2_mask = cur_iregs;
-               /*------------------------------------------------------*/
-               /* update for use with FP regs...                       */
-               /*------------------------------------------------------*/
-               if (spec [MONO_INST_DEST] == 'f') {
-                       dest_mask = cur_fregs;
-                       if (ins->dreg >= MONO_MAX_FREGS) {
-                               val = rs->fassign [ins->dreg];
-                               prev_dreg = ins->dreg;
-                               if (val < 0) {
-                                       int spill = 0;
-                                       if (val < -1) {
-                                               /* the register gets spilled after this inst */
-                                               spill = -val -1;
-                                       }
-                                       val = mono_regstate_alloc_float (rs, dest_mask);
-                                       if (val < 0)
-                                               val = get_float_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
-                                       rs->fassign [ins->dreg] = val;
-                                       if (spill)
-                                               create_spilled_store_float (cfg, spill, val, prev_dreg, ins);
-                               }
-                               DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", 
-                                               mono_arch_regname (val), ins->dreg));
-                               rs->fsymbolic [val] = prev_dreg;
-                               ins->dreg = val;
-                               if (spec [MONO_INST_CLOB] == 'c' && ins->dreg != s390_f0) {
-                                       /* this instruction only outputs to s390_f0, need to copy */
-                                       create_copy_ins_float (cfg, ins->dreg, s390_f0, ins);
-                               }
-                       } else {
-                               prev_dreg = -1;
-                       }
-                       if (freg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfof [prev_dreg].born_in >= i || !(cur_fregs & (1 << ins->dreg)))) {
-                               DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
-                               mono_regstate_free_float (rs, ins->dreg);
-                       }
-               } else if (ins->dreg >= MONO_MAX_IREGS) {
-                       val = rs->iassign [ins->dreg];
-                       prev_dreg = ins->dreg;
-                       if (val < 0) {
-                               int spill = 0;
-                               if (val < -1) {
-                                       /* the register gets spilled after this inst */
-                                       spill = -val -1;
-                               }
-                               val = mono_regstate_alloc_int (rs, dest_mask);
-                               if (val < 0)
-                                       val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
-                               rs->iassign [ins->dreg] = val;
-                               if (spill)
-                                       create_spilled_store (cfg, spill, val, prev_dreg, ins);
-                       }
-                       DEBUG (g_print ("\tassigned dreg %s to dest R%d (prev: R%d)\n", 
-                                       mono_arch_regname (val), ins->dreg, prev_dreg));
-                       rs->isymbolic [val] = prev_dreg;
-                       ins->dreg = val;
-                       if (spec [MONO_INST_DEST] == 'l') {
-                               int hreg = prev_dreg + 1;
-                               val = rs->iassign [hreg];
-                               if (val < 0) {
-                                       int spill = 0;
-                                       if (val < -1) {
-                                               /* the register gets spilled after this inst */
-                                               spill = -val -1;
-                                       }
-                                       val = mono_regstate_alloc_int (rs, dest_mask);
-                                       if (val < 0)
-                                               val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
-                                       rs->iassign [hreg] = val;
-                                       if (spill)
-                                               create_spilled_store (cfg, spill, val, hreg, ins);
-                               }
-                               DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
-                               rs->isymbolic [val] = hreg;
-                               /* FIXME:? ins->dreg = val; */
-                               if (ins->dreg == s390_r3) {
-                                       if (val != s390_r2)
-                                               create_copy_ins (cfg, val, s390_r2, ins);
-                               } else if (ins->dreg == s390_r2) {
-                                       if (val == s390_r3) {
-                                               /* swap */
-                                               create_copy_ins (cfg, s390_r3, s390_r0, ins);
-                                               create_copy_ins (cfg, s390_r2, s390_r3, ins);
-                                               create_copy_ins (cfg, s390_r0, s390_r2, ins);
-                                       } else {
-                                               /* two forced copies */
-                                               create_copy_ins (cfg, ins->dreg, s390_r3, ins);
-                                               create_copy_ins (cfg, val, s390_r2, ins);
-                                       }
-                               } else {
-                                       if (val == s390_r2) {
-                                               create_copy_ins (cfg, ins->dreg, s390_r2, ins);
-                                       } else {
-                                               /* two forced copies */
-                                               create_copy_ins (cfg, val, s390_r2, ins);
-                                               create_copy_ins (cfg, ins->dreg, s390_r3, ins);
-                                       }
-                               }
-                               if (reg_is_freeable (val) && hreg >= 0 && (reginfo [hreg].born_in >= i && !(cur_iregs & (1 << val)))) {
-                                       DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
-                                       mono_regstate_free_int (rs, val);
-                               }
-                       } else if (spec [MONO_INST_DEST] == 'a' && ins->dreg != s390_r2 && spec [MONO_INST_CLOB] != 'd') {
-                               /* this instruction only outputs to s390_r2, need to copy */
-                               create_copy_ins (cfg, ins->dreg, s390_r2, ins);
-                       }
-               } else {
-                       prev_dreg = -1;
-               }
-               if (spec [MONO_INST_DEST] == 'f' && 
-                   freg_is_freeable (ins->dreg) && 
-                   prev_dreg >= 0 && (reginfof [prev_dreg].born_in >= i)) {
-                       DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
-                       mono_regstate_free_float (rs, ins->dreg);
-               } else if (spec [MONO_INST_DEST] != 'f' && 
-                          reg_is_freeable (ins->dreg) && 
-                          prev_dreg >= 0 && (reginfo [prev_dreg].born_in >= i)) {
-                       DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
-                        mono_regstate_free_int (rs, ins->dreg);
-               }
-               if (spec [MONO_INST_SRC1] == 'f') {
-                       src1_mask = cur_fregs;
-                       if (ins->sreg1 >= MONO_MAX_FREGS) {
-                               val = rs->fassign [ins->sreg1];
-                               prev_sreg1 = ins->sreg1;
-                               if (val < 0) {
-                                       int spill = 0;
-                                       if (val < -1) {
-                                               /* the register gets spilled after this inst */
-                                               spill = -val -1;
-                                       }
-                                       //g_assert (val == -1); /* source cannot be spilled */
-                                       val = mono_regstate_alloc_float (rs, src1_mask);
-                                       if (val < 0)
-                                               val = get_float_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
-                                       rs->fassign [ins->sreg1] = val;
-                                       DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
-                                       if (spill) {
-                                               MonoInst *store = create_spilled_store_float (cfg, spill, val, prev_sreg1, NULL);
-                                               insert_before_ins (ins, tmp, store);
-                                       }
-                               }
-                               rs->fsymbolic [val] = prev_sreg1;
-                               ins->sreg1 = val;
-                       } else {
-                               prev_sreg1 = -1;
-                       }
-               } else if (ins->sreg1 >= MONO_MAX_IREGS) {
-                       val = rs->iassign [ins->sreg1];
-                       prev_sreg1 = ins->sreg1;
-                       if (val < 0) {
-                               int spill = 0;
-                               if (val < -1) {
-                                       /* the register gets spilled after this inst */
-                                       spill = -val -1;
-                               }
-                               val = mono_regstate_alloc_int (rs, src1_mask);
-                               if (val < 0)
-                                       val = get_register_spilling (cfg, tmp, ins, 
-                                                                    src1_mask, 
-                                                                    ins->sreg1);
-                               rs->iassign [ins->sreg1] = val;
-                               DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", 
-                                               mono_arch_regname (val), ins->sreg1));
-                               if (spill) {
-                                       MonoInst *store; 
-                                       store = create_spilled_store (cfg, spill, val, 
-                                                                     prev_sreg1, NULL);
-                                       insert_before_ins (ins, tmp, store);
-                               }
-                       }
-                       rs->isymbolic [val] = prev_sreg1;
-                       ins->sreg1 = val;
-               } else {
-                       prev_sreg1 = -1;
-               }
-               /*----------------------------------------------*/
-               /* handle clobbering of sreg1                   */
-               /*----------------------------------------------*/
-               if ((spec [MONO_INST_CLOB] == '1' || 
-                    spec [MONO_INST_CLOB] == 's') && 
-                    ins->dreg != ins->sreg1) {
-                       MonoInst *copy; 
-                       copy = create_copy_ins (cfg, ins->dreg, ins->sreg1, NULL);
-                       DEBUG (g_print ("\tneed to copy sreg1 %s to dreg %s\n", 
-                                       mono_arch_regname (ins->sreg1), 
-                                       mono_arch_regname (ins->dreg)));
-                       if (ins->sreg2 == -1 || spec [MONO_INST_CLOB] == 's') {
-                               /* note: the copy is inserted before the current instruction! */
-                               insert_before_ins (ins, tmp, copy);
-                               /* we set sreg1 to dest as well */
-                               prev_sreg1 = ins->sreg1 = ins->dreg;
-                       } else {
-                               /* inserted after the operation */
-                               copy->next = ins->next;
-                               ins->next  = copy;
-                       }
-               }
-
-               if (spec [MONO_INST_SRC2] == 'f') {
-                       src2_mask = cur_fregs;
-                       if (ins->sreg2 >= MONO_MAX_FREGS) {
-                               val = rs->fassign [ins->sreg2];
-                               prev_sreg2 = ins->sreg2;
-                               if (val < 0) {
-                                       int spill = 0;
-                                       if (val < -1) {
-                                               /* the register gets spilled after this inst */
-                                               spill = -val -1;
-                                       }
-                                       val = mono_regstate_alloc_float (rs, src2_mask);
-                                       if (val < 0)
-                                               val = get_float_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
-                                       rs->fassign [ins->sreg2] = val;
-                                       DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
-                                       if (spill)
-                                               create_spilled_store_float (cfg, spill, val, prev_sreg2, ins);
-                               }
-                               rs->fsymbolic [val] = prev_sreg2;
-                               ins->sreg2 = val;
-                       } else {
-                               prev_sreg2 = -1;
-                       }
-               } else if (ins->sreg2 >= MONO_MAX_IREGS) {
-                       val = rs->iassign [ins->sreg2];
-                       prev_sreg2 = ins->sreg2;
-                       if (val < 0) {
-                               int spill = 0;
-                               if (val < -1) {
-                                       /* the register gets spilled after this inst */
-                                       spill = -val -1;
-                               }
-                               val = mono_regstate_alloc_int (rs, src2_mask);
-                               if (val < 0)
-                                       val = get_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
-                               rs->iassign [ins->sreg2] = val;
-                               DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
-                               if (spill)
-                                       create_spilled_store (cfg, spill, val, prev_sreg2, ins);
-                       }
-                       rs->isymbolic [val] = prev_sreg2;
-                       ins->sreg2 = val;
-               } else {
-                       prev_sreg2 = -1;
-               }
-
-               if (spec [MONO_INST_CLOB] == 'c') {
-                       int j, s;
-                       guint32 clob_mask = S390_CALLER_REGS;
-                       for (j = 0; j < MONO_MAX_IREGS; ++j) {
-                               s = 1 << j;
-                               if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
-                                       //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
-                               }
-                       }
-               }
-               /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
-                       DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
-                       mono_regstate_free_int (rs, ins->sreg1);
-               }
-               if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
-                       DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
-                       mono_regstate_free_int (rs, ins->sreg2);
-               }*/
-               
-               //DEBUG (print_ins (i, ins));
-               tmp = tmp->next;
-       }
+       mono_local_regalloc(cfg, bb);
 }
 
 /*========================= End of Function ========================*/
@@ -2991,22 +2397,6 @@ emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size,
 
 /*========================= End of Function ========================*/
 
-/*------------------------------------------------------------------*/
-/*                                                                  */
-/* Name                - mono_emit_stack_alloc                             */
-/*                                                                  */
-/* Function    -                                                   */
-/*                                                                 */
-/*------------------------------------------------------------------*/
-
-static unsigned char*
-mono_emit_stack_alloc (guchar *code, MonoInst* tree)
-{
-       return code;
-}
-
-/*========================= End of Function ========================*/
-
 /*------------------------------------------------------------------*/
 /*                                                                  */
 /* Name                - mono_arch_output_basic_block                      */
@@ -3025,8 +2415,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
        guint8 *code = cfg->native_code + cfg->code_len;
        MonoInst *last_ins = NULL;
        guint last_offset = 0;
-       int max_len, cpos;
-guint8 cond;
+       int max_len, cpos, src2;
 
        if (cfg->opt & MONO_OPT_PEEPHOLE)
                peephole_pass (cfg, bb);
@@ -3065,49 +2454,49 @@ guint8 cond;
 
                switch (ins->opcode) {
                case OP_STOREI1_MEMBASE_IMM: {
-                       s390_lhi (code, s390_r14, ins->inst_imm);
+                       s390_lhi (code, s390_r0, ins->inst_imm);
                        if (s390_is_uimm12(ins->inst_offset))
-                               s390_stc (code, s390_r14, 0, ins->inst_destbasereg, ins->inst_offset);
+                               s390_stc (code, s390_r0, 0, ins->inst_destbasereg, ins->inst_offset);
                        else {
                                s390_basr (code, s390_r13, 0);
                                s390_j    (code, 4);
                                s390_word (code, ins->inst_offset);
                                s390_l    (code, s390_r13, 0, s390_r13, 4);
-                               s390_stc  (code, s390_r14, s390_r13, ins->inst_destbasereg, 0);
+                               s390_stc  (code, s390_r0, s390_r13, ins->inst_destbasereg, 0);
                        }
                }
                        break;
                case OP_STOREI2_MEMBASE_IMM: {
-                       s390_lhi (code, s390_r14, ins->inst_imm);
+                       s390_lhi (code, s390_r0, ins->inst_imm);
                        if (s390_is_uimm12(ins->inst_offset)) {
-                               s390_sth (code, s390_r14, 0, ins->inst_destbasereg, ins->inst_offset);
+                               s390_sth (code, s390_r0, 0, ins->inst_destbasereg, ins->inst_offset);
                        } else {
                                s390_basr (code, s390_r13, 0);
                                s390_j    (code, 4);
                                s390_word (code, ins->inst_offset);
                                s390_l    (code, s390_r13, 0, s390_r13, 4);
-                               s390_sth  (code, s390_r14, s390_r13, ins->inst_destbasereg, 0);
+                               s390_sth  (code, s390_r0, s390_r13, ins->inst_destbasereg, 0);
                        }
                }
                        break;
                case OP_STORE_MEMBASE_IMM:
                case OP_STOREI4_MEMBASE_IMM: {
                        if (s390_is_imm16(ins->inst_imm)) {
-                               s390_lhi  (code, s390_r14, ins->inst_imm);
+                               s390_lhi  (code, s390_r0, ins->inst_imm);
                        } else {
                                s390_basr (code, s390_r13, 0);
                                s390_j    (code, 4);
                                s390_word (code, ins->inst_imm);
-                               s390_l    (code, s390_r14, 0, s390_r13, 4);
+                               s390_l    (code, s390_r0, 0, s390_r13, 4);
                        }
                        if (s390_is_uimm12(ins->inst_offset)) {
-                               s390_st  (code, s390_r14, 0, ins->inst_destbasereg, ins->inst_offset);
+                               s390_st  (code, s390_r0, 0, ins->inst_destbasereg, ins->inst_offset);
                        } else {
                                s390_basr (code, s390_r13, 0);
                                s390_j    (code, 4);
                                s390_word (code, ins->inst_offset);
                                s390_l    (code, s390_r13, 0, s390_r13, 4);
-                               s390_st   (code, s390_r14, s390_r13, ins->inst_destbasereg, 0);
+                               s390_st   (code, s390_r0, s390_r13, ins->inst_destbasereg, 0);
                        }
                }
                        break;
@@ -3287,12 +2676,12 @@ guint8 cond;
                        break;
                case OP_COMPARE: {
                        if ((ins->next) && 
-                           ((ins->next->opcode >= CEE_BNE_UN) &&
-                            (ins->next->opcode <= CEE_BLT_UN)) || 
-                           ((ins->next->opcode >= OP_COND_EXC_NE_UN) &&
-                            (ins->next->opcode <= OP_COND_EXC_LT_UN)) ||
-                           ((ins->next->opcode == OP_CLT_UN) ||
-                            (ins->next->opcode == OP_CGT_UN)))
+                           (((ins->next->opcode >= CEE_BNE_UN) &&
+                             (ins->next->opcode <= CEE_BLT_UN)) || 
+                            ((ins->next->opcode >= OP_COND_EXC_NE_UN) &&
+                             (ins->next->opcode <= OP_COND_EXC_LT_UN)) ||
+                            ((ins->next->opcode == OP_CLT_UN) ||
+                             (ins->next->opcode == OP_CGT_UN))))
                                s390_clr  (code, ins->sreg1, ins->sreg2);
                        else
                                s390_cr   (code, ins->sreg1, ins->sreg2);
@@ -3302,12 +2691,12 @@ guint8 cond;
                        if (s390_is_imm16 (ins->inst_imm)) {
                                s390_lhi  (code, s390_r0, ins->inst_imm);
                                if ((ins->next) && 
-                                   ((ins->next->opcode >= CEE_BNE_UN) &&
-                                    (ins->next->opcode <= CEE_BLT_UN)) || 
-                                   ((ins->next->opcode >= OP_COND_EXC_NE_UN) &&
-                                    (ins->next->opcode <= OP_COND_EXC_LT_UN)) ||
-                                   ((ins->next->opcode == OP_CLT_UN) ||
-                                    (ins->next->opcode == OP_CGT_UN)))
+                                   (((ins->next->opcode >= CEE_BNE_UN) &&
+                                     (ins->next->opcode <= CEE_BLT_UN)) || 
+                                    ((ins->next->opcode >= OP_COND_EXC_NE_UN) &&
+                                     (ins->next->opcode <= OP_COND_EXC_LT_UN)) ||
+                                    ((ins->next->opcode == OP_CLT_UN) ||
+                                     (ins->next->opcode == OP_CGT_UN))))
                                        s390_clr  (code, ins->sreg1, s390_r0);
                                else
                                        s390_cr   (code, ins->sreg1, s390_r0);
@@ -3317,12 +2706,12 @@ guint8 cond;
                                s390_j    (code, 4);
                                s390_word (code, ins->inst_imm);
                                if ((ins->next) && 
-                                   ((ins->next->opcode >= CEE_BNE_UN) &&
-                                    (ins->next->opcode <= CEE_BLT_UN)) || 
-                                   ((ins->next->opcode >= OP_COND_EXC_NE_UN) &&
-                                    (ins->next->opcode <= OP_COND_EXC_LT_UN)) ||
-                                   ((ins->next->opcode == OP_CLT_UN) &&
-                                    (ins->next->opcode == OP_CGT_UN)))
+                                   (((ins->next->opcode >= CEE_BNE_UN) &&
+                                     (ins->next->opcode <= CEE_BLT_UN)) || 
+                                    ((ins->next->opcode >= OP_COND_EXC_NE_UN) &&
+                                     (ins->next->opcode <= OP_COND_EXC_LT_UN)) ||
+                                    ((ins->next->opcode == OP_CLT_UN) ||
+                                     (ins->next->opcode == OP_CGT_UN))))
                                        s390_cl   (code, ins->sreg1, 0, s390_r13, 4);
                                else
                                        s390_c    (code, ins->sreg1, 0, s390_r13, 4);
@@ -3334,32 +2723,48 @@ guint8 cond;
                }
                        break;
                case CEE_BREAK: {
-                       s390_break (code);
+                       mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, mono_arch_break);
+                        s390_brasl (code, s390_r14, 0);
                }
                        break;
                case OP_ADDCC: {
-                       if (ins->dreg != ins->sreg1) {
-                               s390_lr   (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_alr  (code, ins->dreg, ins->sreg2);
+                       CHECK_SRCDST_COM;
+                       s390_alr  (code, ins->dreg, src2);
                }
                        break;
                case CEE_ADD: {
-                       if (ins->dreg != ins->sreg1) {
-                               s390_lr   (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_ar   (code, ins->dreg, ins->sreg2);
+                       CHECK_SRCDST_COM;
+                       s390_ar   (code, ins->dreg, src2);
                }
                        break;
                case OP_ADC: {
+                       CHECK_SRCDST_COM;
+                       s390_alcr (code, ins->dreg, src2);
+               }
+                       break;
+               case OP_ADD_IMM: {
                        if (ins->dreg != ins->sreg1) {
                                s390_lr   (code, ins->dreg, ins->sreg1);
                        }
-                       s390_alcr (code, ins->dreg, ins->sreg2);
+                       if ((ins->next) &&
+                           (ins->next->opcode == OP_ADC_IMM)) {
+                               s390_basr (code, s390_r13, 0);
+                               s390_j    (code, 4);
+                               s390_word (code, ins->inst_imm);
+                               s390_a (code, ins->dreg, 0, s390_r13, 4);
+                       } else {
+                               if (s390_is_imm16 (ins->inst_imm)) {
+                                       s390_ahi  (code, ins->dreg, ins->inst_imm);
+                               } else {
+                                       s390_basr (code, s390_r13, 0);
+                                       s390_j    (code, 4);
+                                       s390_word (code, ins->inst_imm);
+                                       s390_a (code, ins->dreg, 0, s390_r13, 4);
+                               }
+                       }
                }
                        break;
-               case OP_ADDCC_IMM:
-               case OP_ADD_IMM: {
+               case OP_ADDCC_IMM: {
                        if ((ins->next) &&
                            (ins->next->opcode == OP_ADC_IMM)) {
                                s390_basr (code, s390_r13, 0);
@@ -3374,7 +2779,8 @@ guint8 cond;
                                        if (ins->dreg != ins->sreg1) {
                                                s390_lr   (code, ins->dreg, ins->sreg1);
                                        }
-                                       s390_ahi (code, ins->dreg, ins->inst_imm);
+                                       s390_lhi  (code, s390_r0, ins->inst_imm);
+                                       s390_alcr (code, ins->dreg, s390_r0);
                                } else {
                                        s390_basr (code, s390_r13, 0);
                                        s390_j    (code, 4);
@@ -3382,7 +2788,7 @@ guint8 cond;
                                        if (ins->dreg != ins->sreg1) {
                                                s390_lr   (code, ins->dreg, ins->sreg1);
                                        }
-                                       s390_a    (code, ins->dreg, 0, s390_r13, 4);
+                                       s390_al   (code, ins->dreg, 0, s390_r13, 4);
                                }
                        }
                }
@@ -3404,64 +2810,88 @@ guint8 cond;
                }
                        break;
                case CEE_ADD_OVF: {
-                       if (ins->dreg != ins->sreg1) {
-                               s390_lr   (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_ar   (code, ins->dreg, ins->sreg2);
+                       CHECK_SRCDST_COM;
+                       s390_ar   (code, ins->dreg, src2);
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
                }
                        break;
                case CEE_ADD_OVF_UN: {
-                       if (ins->dreg != ins->sreg1) {
-                               s390_lr   (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_alr  (code, ins->dreg, ins->sreg2);
+                       CHECK_SRCDST_COM;
+                       s390_alr  (code, ins->dreg, src2);
+                       EMIT_COND_SYSTEM_EXCEPTION (S390_CC_CY, "OverflowException");
+               }
+                       break;
+               case OP_LADD_OVF: {
+                       short int *o[1];
+                       s390_alr  (code, s390_r0, ins->sreg1);
+                       s390_jnc  (code, 0); CODEPTR(code, o[0]);
+                       s390_ahi  (code, s390_r1, 1);
+                       EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
+                       PTRSLOT   (code, o[0]);
+                       s390_ar   (code, s390_r1, ins->sreg2);
+                       EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
+                       s390_lr   (code, ins->dreg, s390_r0);
+                       s390_lr   (code, ins->dreg+1, s390_r1);
+               }
+                       break;
+               case OP_LADD_OVF_UN: {
+                       s390_alr  (code, s390_r0, ins->sreg1);
+                       s390_alcr (code, s390_r1, ins->sreg2);
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_CY, "OverflowException");
+                       s390_lr   (code, ins->dreg, s390_r0);
+                       s390_lr   (code, ins->dreg+1, s390_r1);
                }
                        break;
                case OP_ADD_OVF_CARRY: {
-                       if (ins->dreg != ins->sreg1) {
-                               s390_lr   (code, ins->dreg, ins->sreg1);
-                       }
+                       CHECK_SRCDST_COM;
                        s390_lhi  (code, s390_r0, 0);
                        s390_lr   (code, s390_r1, s390_r0);
                        s390_alcr (code, s390_r0, s390_r1);
-                       s390_ar   (code, ins->dreg, ins->sreg2);
+                       s390_ar   (code, ins->dreg, src2);
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
                        s390_ar   (code, ins->dreg, s390_r0);
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
                }
                        break;
                case OP_ADD_OVF_UN_CARRY: {
-                       if (ins->dreg != ins->sreg1) {
-                               s390_lr   (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_alcr (code, ins->dreg, ins->sreg2);
+                       CHECK_SRCDST_COM;
+                       s390_alcr (code, ins->dreg, src2);
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_CY, "OverflowException");
                }
                        break;
                case OP_SUBCC: {
-                       if (ins->dreg != ins->sreg1) {
-                               s390_lr   (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_slr (code, ins->dreg, ins->sreg2);
+                       CHECK_SRCDST_NCOM;
+                       s390_slr (code, ins->dreg, src2);
                }
                        break;
                case CEE_SUB: {
-                       if (ins->dreg != ins->sreg1) {
-                               s390_lr   (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_sr   (code, ins->dreg, ins->sreg2);
+                       CHECK_SRCDST_NCOM;
+                       s390_sr   (code, ins->dreg, src2);
                }
                        break;
                case OP_SBB: {
-                       if (ins->dreg != ins->sreg1) {
-                               s390_lr   (code, ins->dreg, ins->sreg1);
+                       CHECK_SRCDST_NCOM;
+                       s390_slbr (code, ins->dreg, src2);
+               }
+                       break;
+               case OP_SUBCC_IMM: {
+                       if (s390_is_imm16 (-ins->inst_imm)) {
+                               if (ins->dreg != ins->sreg1) {
+                                       s390_lr   (code, ins->dreg, ins->sreg1);
+                               }
+                               s390_lhi  (code, s390_r0, ins->inst_imm);
+                               s390_slr  (code, ins->dreg, s390_r0);
+                       } else {
+                               s390_basr (code, s390_r13, 0);
+                               s390_j    (code, 4);
+                               s390_word (code, ins->inst_imm);
+                               if (ins->dreg != ins->sreg1) {
+                                       s390_lr   (code, ins->dreg, ins->sreg1);
+                               }
+                               s390_sl   (code, ins->dreg, 0, s390_r13, 4);
                        }
-                       s390_slbr (code, ins->dreg, ins->sreg2);
                }
                        break;
-               case OP_SUBCC_IMM:
                case OP_SUB_IMM: {
                        if (s390_is_imm16 (-ins->inst_imm)) {
                                if (ins->dreg != ins->sreg1) {
@@ -3480,46 +2910,68 @@ guint8 cond;
                }
                        break;
                case OP_SBB_IMM: {
-                       s390_basr (code, s390_r13, 0);
-                       s390_j    (code, 4);
-                       s390_word (code, ins->inst_imm);
-                       s390_sl   (code, ins->dreg, 0, s390_r13, 4);
+                       if (ins->dreg != ins->sreg1) {
+                               s390_lr    (code, ins->dreg, ins->sreg1);
+                       }
+                       if (s390_is_imm16 (-ins->inst_imm)) {
+                               s390_lhi   (code, s390_r0, ins->inst_imm);
+                               s390_slbr  (code, ins->dreg, s390_r0);
+                       } else {
+                               s390_basr (code, s390_r13, 0);
+                               s390_j    (code, 4);
+                               s390_word (code, ins->inst_imm);
+                               s390_slb  (code, ins->dreg, 0, s390_r13, 4);
+                       }
                }
                        break;
                case CEE_SUB_OVF: {
-                       if (ins->dreg != ins->sreg1) {
-                               s390_lr   (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_sr   (code, ins->dreg, ins->sreg2);
+                       CHECK_SRCDST_NCOM;
+                       s390_sr   (code, ins->dreg, src2);
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
                }
                        break;
                case CEE_SUB_OVF_UN: {
-                       if (ins->dreg != ins->sreg1) {
-                               s390_lr   (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_slr  (code, ins->dreg, ins->sreg2);
+                       CHECK_SRCDST_NCOM;
+                       s390_slr  (code, ins->dreg, src2);
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NC, "OverflowException");
                }
                        break;
+               case OP_LSUB_OVF: {
+                       short int *o[3];
+                       s390_lr   (code, s390_r14, ins->sreg2);
+                       s390_slr  (code, s390_r0, ins->sreg1);
+                       s390_jnl  (code, 0); CODEPTR(code, o[0]);
+                       s390_ahi  (code, s390_r14, 1);
+                       EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
+                       PTRSLOT   (code, o[0]);
+                       s390_sr   (code, s390_r1, s390_r14);
+                       EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
+                       s390_lr   (code, ins->dreg, s390_r0);
+                       s390_lr   (code, ins->dreg+1, s390_r1);
+               }
+                       break;
+               case OP_LSUB_OVF_UN: {
+                       s390_slr  (code, s390_r0, ins->sreg1);
+                       s390_slbr (code, s390_r1, ins->sreg2);
+                       EMIT_COND_SYSTEM_EXCEPTION (S390_CC_LT, "OverflowException");
+                       s390_lr   (code, ins->dreg, s390_r0);
+                       s390_lr   (code, ins->dreg+1, s390_r1);
+               }
+                       break;
                case OP_SUB_OVF_CARRY: {
-                       if (ins->dreg != ins->sreg1) {
-                               s390_lr   (code, ins->dreg, ins->sreg1);
-                       }
+                       CHECK_SRCDST_NCOM;
                        s390_lhi  (code, s390_r0, 0);
                        s390_lr   (code, s390_r1, s390_r0);
                        s390_slbr (code, s390_r0, s390_r1);
-                       s390_sr   (code, ins->dreg, ins->sreg2);
+                       s390_sr   (code, ins->dreg, src2);
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
                        s390_ar   (code, ins->dreg, s390_r0);
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_OV, "OverflowException");
                }
                        break;
                case OP_SUB_OVF_UN_CARRY: {
-                       if (ins->dreg != ins->sreg1) {
-                               s390_lr   (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_slbr (code, ins->dreg, ins->sreg2);
+                       CHECK_SRCDST_NCOM;
+                       s390_slbr (code, ins->dreg, src2);
                        EMIT_COND_SYSTEM_EXCEPTION (S390_CC_NC, "OverflowException");
                }
                        break;
@@ -3582,7 +3034,7 @@ guint8 cond;
                                s390_l    (code, s390_r13, 0, s390_r13, 4);
                        }
                        s390_srda (code, s390_r0, 0, 32);
-                       s390_dr   (code, s390_r0, ins->sreg2);
+                       s390_dr   (code, s390_r0, s390_r13);
                        s390_lr   (code, ins->dreg, s390_r1);
                }
                        break;
@@ -3611,7 +3063,7 @@ guint8 cond;
                                s390_l    (code, s390_r13, 0, s390_r13, 4);
                        }
                        s390_srda (code, s390_r0, 0, 32);
-                       s390_dr   (code, s390_r0, ins->sreg2);
+                       s390_dr   (code, s390_r0, s390_r13);
                        s390_lr   (code, ins->dreg, s390_r0);
                }
                        break;
@@ -3638,12 +3090,13 @@ guint8 cond;
                                }
                                s390_or   (code, ins->dreg, s390_r0);
                        } else {
-                               s390_bras (code, s390_r13, 4);
+                               s390_basr (code, s390_r13, 0);
+                               s390_j    (code, 4);
                                s390_word (code, ins->inst_imm);
                                if (ins->dreg != ins->sreg1) {
                                        s390_lr   (code, ins->dreg, ins->sreg1);
                                }
-                               s390_o    (code, ins->dreg, 0, s390_r13, 0);
+                               s390_o    (code, ins->dreg, 0, s390_r13, 4);
                        }
                }
                        break;
@@ -3681,10 +3134,8 @@ guint8 cond;
                }
                        break;
                case CEE_SHL: {
-                       if (ins->sreg1 != ins->dreg) {
-                               s390_lr   (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_sll  (code, ins->dreg, ins->sreg2, 0);
+                       CHECK_SRCDST_NCOM;
+                       s390_sll  (code, ins->dreg, src2, 0);
                }
                        break;
                case OP_SHL_IMM: {
@@ -3695,10 +3146,8 @@ guint8 cond;
                }
                        break;
                case CEE_SHR: {
-                       if (ins->sreg1 != ins->dreg) {
-                               s390_lr   (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_sra  (code, ins->dreg, ins->sreg2, 0);
+                       CHECK_SRCDST_NCOM;
+                       s390_sra  (code, ins->dreg, src2, 0);
                }
                        break;
                case OP_SHR_IMM: {
@@ -3716,10 +3165,8 @@ guint8 cond;
                }
                        break;
                case CEE_SHR_UN: {
-                       if (ins->sreg1 != ins->dreg) {
-                               s390_lr   (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_srl  (code, ins->dreg, ins->sreg2, 0);
+                       CHECK_SRCDST_NCOM;
+                       s390_srl  (code, ins->dreg, src2, 0);
                }
                        break;
                case CEE_NOT: {
@@ -3756,11 +3203,14 @@ guint8 cond;
                                s390_basr (code, s390_r13, 0);
                                s390_j    (code, 4);
                                s390_word (code, ins->inst_imm);
-                               if (ins->dreg != ins->sreg1) {
-                                       s390_lr   (code, ins->dreg, ins->sreg1);
-                               }
+//                             if (ins->dreg != ins->sreg1) {
+//                                     s390_lr   (code, ins->dreg, ins->sreg1);
+//                             }
                                s390_l    (code, s390_r13, 0, s390_r13, 4);
                        }
+                       if (ins->dreg != ins->sreg1) {
+                               s390_lr   (code, ins->dreg, ins->sreg1);
+                       }
                        s390_msr  (code, ins->dreg, s390_r13);
                }
                        break;
@@ -3794,16 +3244,16 @@ guint8 cond;
                        break;
                case OP_LMUL: {
                        s390_l    (code, s390_r0, 0, ins->sreg1, 4);
-                       s390_lr   (code, s390_r14, s390_r0);
                        s390_srda (code, s390_r0, 0, 32);
                        s390_m    (code, s390_r0, 0, ins->sreg2, 4);
-                       s390_srl  (code, s390_r14, 0, 31);
-                       s390_a    (code, s390_r14, 0, ins->sreg1, 0);
+                       s390_l    (code, s390_r0, 0, ins->sreg1, 4);
+                       s390_srl  (code, s390_r0, 0, 31);
+                       s390_a    (code, s390_r0, 0, ins->sreg1, 0);
                        s390_l    (code, s390_r13, 0, ins->sreg2, 0);
                        s390_srl  (code, s390_r13, 0, 31);
                        s390_ms   (code, s390_r13, 0, ins->sreg1, 4);
-                       s390_ar   (code, s390_r14, s390_r13);
-                       s390_st   (code, s390_r14, 0, ins->dreg, 0);
+                       s390_ar   (code, s390_r0, s390_r13);
+                       s390_st   (code, s390_r0, 0, ins->dreg, 0);
                        s390_st   (code, s390_r1, 0, ins->dreg, 4);
                }
                        break;  
@@ -3860,26 +3310,36 @@ guint8 cond;
                        s390_ledbr (code, ins->dreg, ins->sreg1);
                }
                        break;
+               case OP_TLS_GET: {
+                       if (s390_is_imm16 (ins->inst_offset)) {
+                               s390_lhi (code, s390_r13, ins->inst_offset);
+                       } else {
+                               s390_bras (code, s390_r13, 0);
+                               s390_j    (code, 4);
+                               s390_word (code, ins->inst_offset);
+                               s390_l    (code, s390_r13, 0, s390_r13, 4);
+                       }
+                       s390_ear (code, s390_r1, 0);
+                       s390_l   (code, ins->dreg, s390_r13, s390_r1, 0);
+               }
+                       break;
                case OP_FCONV_TO_R4: {
+                       NOT_IMPLEMENTED("OP_FCONV_TO_R4");
                        if ((ins->next) &&
-                           (ins->next->opcode != OP_STORER4_MEMBASE_REG))
+                            (ins->next->opcode != OP_FMOVE) &&
+                            (ins->next->opcode != OP_STORER4_MEMBASE_REG))
                                s390_ledbr (code, ins->dreg, ins->sreg1);
                }
                        break;
                case CEE_JMP: {
-                       int fParm;
                        if (cfg->method->save_lmf)
-                               code = restoreLMF(cfg, code);
+                               restoreLMF(code, cfg->frame_reg, cfg->stack_usage);
 
                        if (cfg->flags & MONO_CFG_HAS_TAIL) {
-                               s390_lm (code, s390_r2, s390_r5, STK_BASE, 
-                                        S390_PARM_SAVE_OFFSET);
-                               for (fParm = 0; fParm < 4; fParm++)
-                                       s390_ld (code, fParm, 0, STK_BASE,
-                                          S390_FLOAT_SAVE_OFFSET+fParm*sizeof(double));
+                               code = emit_load_volatile_registers(code, cfg);
                        }
 
-                       code = backUpStackPtr(cfg, code);
+                       code = backUpStackPtr(cfg, code, STK_BASE);
                        s390_l   (code, s390_r14, 0, STK_BASE, S390_RET_ADDR_OFFSET);
                        mono_add_patch_info (cfg, code - cfg->native_code,
                                             MONO_PATCH_INFO_METHOD_JUMP,
@@ -3893,11 +3353,18 @@ guint8 cond;
                }
                        break;
                case OP_ARGLIST: {
-                       NOT_IMPLEMENTED("OP_ARGLIST");
-                       s390_basr (code, s390_r13, 0);
-                       s390_j    (code, 4);
-                       s390_word (code, cfg->sig_cookie);
-                       s390_l    (code, ins->sreg1, 0, s390_r13, 4);
+                       int offset = cfg->sig_cookie + cfg->stack_usage;
+
+                       if (s390_is_imm16 (offset))
+                               s390_lhi  (code, s390_r0, offset);
+                       else {
+                               s390_basr (code, s390_r13, 0);
+                               s390_j    (code, 4);
+                               s390_word (code, offset);
+                               s390_l    (code, s390_r0, 0, s390_r13, 0);
+                       }
+                       s390_ar   (code, s390_r0, cfg->frame_reg);
+                       s390_st   (code, s390_r0, 0, ins->sreg1, 0);
                }
                        break;
                case OP_FCALL: {
@@ -3961,20 +3428,35 @@ guint8 cond;
                        g_assert_not_reached ();
                        break;
                case OP_LOCALLOC: {
-                       int alloca_skip = S390_MINIMAL_STACK_SIZE + cfg->param_area + 
-                                         S390_STACK_ALIGNMENT - 1;
+                       /*------------------------------------------*/
+                       /* To allocate space on the stack we have   */
+                       /* to allow room for parameters passed in   */
+                       /* calls, the backchain pointer and round   */
+                       /* it to our stack alignment requirements   */
+                       /*------------------------------------------*/
+                       int alloca_skip = S390_MINIMAL_STACK_SIZE + cfg->param_area;
                        int area_offset = S390_ALIGN(alloca_skip, S390_STACK_ALIGNMENT);
                        s390_lr   (code, s390_r1, ins->sreg1);
+                       if (ins->flags & MONO_INST_INIT)
+                               s390_lr   (code, s390_r0, ins->sreg1);
                        s390_ahi  (code, s390_r1, 14);
                        s390_srl  (code, s390_r1, 0, 3);
                        s390_sll  (code, s390_r1, 0, 3);
                        s390_l    (code, s390_r13, 0, STK_BASE, 0);
-                       s390_lcr  (code, s390_r1, s390_r1);
-                       s390_la   (code, STK_BASE, STK_BASE, s390_r1, 0);
+                       s390_sr   (code, STK_BASE, s390_r1);
                        s390_st   (code, s390_r13, 0, STK_BASE, 0);
                        s390_la   (code, ins->dreg, 0, STK_BASE, area_offset);
                        s390_srl  (code, ins->dreg, 0, 3);
                        s390_sll  (code, ins->dreg, 0, 3);
+                       if (ins->flags & MONO_INST_INIT) {
+                               s390_lr   (code, s390_r1, s390_r0);
+                               s390_lr   (code, s390_r0, ins->dreg);
+                               s390_lr   (code, s390_r14, s390_r12);
+                               s390_lhi  (code, s390_r13, 0);
+                               s390_mvcle(code, s390_r0, s390_r12, 0, 0);
+                               s390_jo   (code, -2);
+                               s390_lr   (code, s390_r12, s390_r14);
+                       }
                }
                        break;
                case CEE_RET: {
@@ -3988,6 +3470,13 @@ guint8 cond;
                        s390_brasl (code, s390_r14, 0);
                }
                        break;
+               case OP_RETHROW: {
+                       s390_lr (code, s390_r2, ins->sreg1);
+                       mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
+                                            (gpointer)"mono_arch_rethrow_exception");
+                       s390_brasl (code, s390_r14, 0);
+               }
+                       break;
                case OP_START_HANDLER: {
                        if (s390_is_uimm12 (ins->inst_left->inst_offset)) {
                                s390_st   (code, s390_r14, 0, 
@@ -4265,17 +3754,18 @@ guint8 cond;
                case OP_LCONV_TO_OVF_I: {
                        /* Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000 */
                        short int *o[5];
-                       s390_ltr  (code, ins->sreg2, ins->sreg2);
-                       s390_jnl  (code, 0); CODEPTR(code, o[0]);
                        s390_ltr  (code, ins->sreg1, ins->sreg1);
+                       s390_jnl  (code, 0); CODEPTR(code, o[0]);
+                       s390_ltr  (code, ins->sreg2, ins->sreg2);
                        s390_jnl  (code, 0); CODEPTR(code, o[1]);
                        s390_lhi  (code, s390_r13, -1);
-                       s390_cr   (code, ins->sreg1, s390_r13);
+                       s390_cr   (code, ins->sreg2, s390_r13);
                        s390_jnz  (code, 0); CODEPTR(code, o[2]);
-                       if (ins->dreg != ins->sreg2)
-                               s390_lr   (code, ins->dreg, ins->sreg2);
+                       if (ins->dreg != ins->sreg1)
+                               s390_lr   (code, ins->dreg, ins->sreg1);
                        s390_j    (code, 0); CODEPTR(code, o[3]);
                        PTRSLOT(code, o[0]);
+                       s390_ltr  (code, ins->sreg2, ins->sreg2);
                        s390_jz   (code, 0); CODEPTR(code, o[4]);
                        PTRSLOT(code, o[1]);
                        PTRSLOT(code, o[2]);
@@ -4291,47 +3781,23 @@ guint8 cond;
                }
                        break;
                case OP_FADD: {
-                       if (ins->dreg == ins->sreg1)
-                               s390_adbr (code, ins->dreg, ins->sreg2);
-                       else {
-                               if (ins->dreg == ins->sreg2)
-                                       s390_adbr (code, ins->dreg, ins->sreg1);
-                               else {
-                                       s390_ldr  (code, ins->dreg, ins->sreg1);
-                                       s390_adbr (code, ins->dreg, ins->sreg2);
-                               }
-                       }
+                       CHECK_SRCDST_COM_F;
+                       s390_adbr (code, ins->dreg, src2);
                }
                        break;
                case OP_FSUB: {
-                       if (ins->dreg == ins->sreg1)
-                               s390_sdbr (code, ins->dreg, ins->sreg2);
-                       else {
-                               s390_ldr  (code, ins->dreg, ins->sreg1);
-                               s390_sdbr (code, ins->dreg, ins->sreg2);
-                       }
+                       CHECK_SRCDST_NCOM_F;
+                       s390_sdbr (code, ins->dreg, src2);
                }
                        break;          
                case OP_FMUL: {
-                       if (ins->dreg == ins->sreg1)
-                               s390_mdbr (code, ins->dreg, ins->sreg2);
-                       else {
-                               if (ins->dreg == ins->sreg2)
-                                       s390_mdbr (code, ins->dreg, ins->sreg1);
-                               else {
-                                       s390_ldr  (code, ins->dreg, ins->sreg1);
-                                       s390_mdbr (code, ins->dreg, ins->sreg2);
-                               }
-                       }
+                       CHECK_SRCDST_COM_F;
+                       s390_mdbr (code, ins->dreg, src2);
                }
                        break;          
                case OP_FDIV: {
-                       if (ins->dreg == ins->sreg1)
-                               s390_ddbr (code, ins->dreg, ins->sreg2);
-                       else {
-                               s390_ldr  (code, ins->dreg, ins->sreg1);
-                               s390_ddbr (code, ins->dreg, ins->sreg2);
-                       }
+                       CHECK_SRCDST_NCOM_F;
+                       s390_ddbr (code, ins->dreg, src2);
                }
                        break;          
                case OP_FNEG: {
@@ -4339,10 +3805,8 @@ guint8 cond;
                }
                        break;          
                case OP_FREM: {
-                       if (ins->dreg != ins->sreg1) {
-                               s390_ldr  (code, ins->dreg, ins->sreg1);
-                       }
-                       s390_didbr (code, ins->dreg, ins->sreg2, 5, s390_f15);
+                       CHECK_SRCDST_NCOM_F;
+                       s390_didbr (code, ins->dreg, src2, 5, s390_f15);
                }
                        break;
                case OP_FCOMPARE: {
@@ -4440,23 +3904,64 @@ guint8 cond;
                                                s390_word (code, ins->inst_offset);
                                                s390_a    (code, s390_r0, 0, s390_r13, 4);
                                        }
+                                       s390_lr   (code, s390_r14, s390_r12);
                                        s390_lr   (code, s390_r12, ins->sreg1);
                                        if (s390_is_imm16 (ins->inst_imm)) {
                                                s390_ahi  (code, s390_r12, ins->inst_imm);
                                        } else {
                                                s390_basr (code, s390_r13, 0);
                                                s390_j    (code, 4);
-                                                       s390_word (code, ins->inst_imm);
+                                               s390_word (code, ins->inst_imm);
                                                s390_a    (code, s390_r12, 0, s390_r13, 4);
                                        }
                                        s390_lr   (code, s390_r1, ins->sreg1);
                                        s390_lr   (code, s390_r13, s390_r1);
                                        s390_mvcle(code, s390_r0, s390_r12, 0, 0);
                                        s390_jo   (code, -2);
+                                       s390_lr   (code, s390_r12, s390_r14);
                                }
                        }
                }
                        break;
+               case OP_ATOMIC_ADD_I4: {
+                       s390_lr  (code, s390_r1, ins->sreg2);
+                       s390_l   (code, s390_r0, 0, ins->inst_basereg, ins->inst_offset);
+                       s390_a   (code, s390_r1, 0, ins->inst_basereg, ins->inst_offset);
+                       s390_cs  (code, s390_r0, s390_r1, ins->inst_basereg, ins->inst_offset);
+                       s390_jnz (code, -7);
+                       s390_lr  (code, ins->dreg, s390_r1);
+               }
+                       break;  
+               case OP_ATOMIC_ADD_NEW_I4: {
+                       s390_lr  (code, s390_r1, ins->sreg2);
+                       s390_l   (code, s390_r0, 0, ins->inst_basereg, ins->inst_offset);
+                       s390_a   (code, s390_r1, 0, ins->inst_basereg, ins->inst_offset);
+                       s390_cs  (code, s390_r0, s390_r1, ins->inst_basereg, ins->inst_offset);
+                       s390_jnz (code, -7);
+                       s390_lr  (code, ins->dreg, s390_r1);
+               }
+                       break;  
+               case OP_ATOMIC_EXCHANGE_I4: {
+                       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_lr  (code, ins->dreg, s390_r0);
+               }
+                       break;  
+               case OP_S390_BKCHAIN: {
+                       s390_lr  (code, ins->dreg, ins->sreg1);
+                       if (s390_is_imm16 (cfg->stack_offset)) {
+                               s390_ahi (code, ins->dreg, cfg->stack_offset);
+                       } else {
+                               s390_basr (code, s390_r13, 0);
+                               s390_j    (code, 6);
+                               s390_word (code, cfg->stack_offset);
+                               s390_a    (code, ins->dreg, 0, s390_r13, 4);
+                       }
+               }
+               case OP_MEMORY_BARRIER: {
+               }
+                       break;  
                default:
                        g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
                        g_assert_not_reached ();
@@ -4492,8 +3997,24 @@ guint8 cond;
 void
 mono_arch_register_lowlevel_calls (void)
 {
-       mono_register_jit_icall (enter_method, "mono_enter_method", NULL, TRUE);
-       mono_register_jit_icall (leave_method, "mono_leave_method", NULL, TRUE);
+       mono_register_jit_icall (mono_arch_break, "mono_arch_break", NULL, TRUE);
+       mono_register_jit_icall (mono_arch_get_lmf_addr, "mono_arch_get_lmf_addr", NULL, TRUE);
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_patch_code                              */
+/*                                                                  */
+/* Function    - Process the patch data created during the         */
+/*               instruction build process. This resolves jumps,   */
+/*               calls, variables etc.                             */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+static void
+mono_arch_break(void) {
 }
 
 /*========================= End of Function ========================*/
@@ -4509,201 +4030,167 @@ mono_arch_register_lowlevel_calls (void)
 /*------------------------------------------------------------------*/
 
 void
-mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
+mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, 
+                     guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
 {
        MonoJumpInfo *patch_info;
 
        for (patch_info = ji; patch_info; patch_info = patch_info->next) {
                unsigned char *ip = patch_info->ip.i + code;
-               gint32 target = 0;
+               gconstpointer target = NULL;
+
+               target = mono_resolve_patch_target (method, domain, code, 
+                                                   patch_info, run_cctors);
 
                switch (patch_info->type) {
-               case MONO_PATCH_INFO_BB:
-                       target = S390_RELATIVE((patch_info->data.bb->native_offset+code),
-                                              ip);
-                       ip    += 2;     /* Skip over op-code */
-                       break;
-               case MONO_PATCH_INFO_ABS:
-                       target = S390_RELATIVE(patch_info->data.target, ip);
-                       ip    += 2;     /* Skip over op-code */
-                       break;
-               case MONO_PATCH_INFO_LABEL:
-                       target = S390_RELATIVE((patch_info->data.inst->inst_c0+code),ip);
-                       ip    += 2;     /* Skip over op-code */
-                       break;
-               case MONO_PATCH_INFO_IP:
-                       target = ip;
-                       continue;
-               case MONO_PATCH_INFO_METHOD_REL:
-                       g_assert_not_reached ();
-                       *((gpointer *)(ip)) = code + patch_info->data.offset;
-                       continue;
-               case MONO_PATCH_INFO_INTERNAL_METHOD: {
-                       MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
-                       if (!mi) {
-                               g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
+                       case MONO_PATCH_INFO_IP:
+                       case MONO_PATCH_INFO_EXC_NAME:
+                       case MONO_PATCH_INFO_LDSTR:
+                       case MONO_PATCH_INFO_TYPE_FROM_HANDLE: 
+                       case MONO_PATCH_INFO_LDTOKEN: 
+                       case MONO_PATCH_INFO_EXC:
+                               continue;
+                       case MONO_PATCH_INFO_SWITCH: 
+                               /*----------------------------------*/
+                               /* ip points at the basr r13,0/j +4 */
+                               /* instruction the vtable value     */
+                               /* follows this (i.e. ip+6)         */
+                               /*----------------------------------*/
+                               *((gconstpointer *)(ip+6)) = target;
+                               target = NULL;
+                               continue;
+                       case MONO_PATCH_INFO_METHODCONST:
+                       case MONO_PATCH_INFO_CLASS:
+                       case MONO_PATCH_INFO_IMAGE:
+                       case MONO_PATCH_INFO_FIELD:
+                       case MONO_PATCH_INFO_IID:
+                               target = S390_RELATIVE(target, ip);
+                               continue;
+                       case MONO_PATCH_INFO_R4:
+                       case MONO_PATCH_INFO_R8:
+                       case MONO_PATCH_INFO_METHOD_REL:
+                               g_assert_not_reached ();
+                               continue;
+                       default:
+                               target = S390_RELATIVE(target, ip);
+                               ip += 2;
+               }
+               s390_patch (ip, (guint32) target);
+       }
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - emit_load_volatile_registers                      */
+/*                                                                  */
+/* Function    - Create the instruction sequence for loading the   */
+/*               parameter registers for use with the 'tail' op.   */
+/*                                                                 */
+/*               The register loading operations performed here    */
+/*               are the mirror of the store operations performed  */
+/*               in mono_arch_emit_prolog and need to be kept in   */
+/*               synchronization with it.                          */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+guint8 *
+emit_load_volatile_registers(guint8 * code, MonoCompile *cfg)
+{
+       MonoMethod *method = cfg->method;
+       MonoMethodSignature *sig;
+       MonoInst *inst;
+       int pos, i;
+       CallInfo *cinfo;
+       size_data sz;
+
+       sig = mono_method_signature (method);
+       pos = 0;
+
+       cinfo = calculate_sizes (sig, &sz, sig->pinvoke);
+
+       if (cinfo->struct_ret) {
+               ArgInfo *ainfo = &cinfo->ret;
+               inst         = cfg->ret;
+               s390_l (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
+       }
+
+       for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
+               ArgInfo *ainfo = cinfo->args + i;
+               inst = cfg->varinfo [pos];
+               
+               if (inst->opcode == OP_REGVAR) {
+                       if (ainfo->regtype == RegTypeGeneral)
+                               s390_lr (code, ainfo->reg, inst->dreg);
+                       else if (ainfo->regtype == RegTypeFP) {
+                               if (inst->dreg != ainfo->reg) {
+                                       if (ainfo->size == 4) {
+                                               s390_ldebr (code, ainfo->reg, inst->dreg);
+                                       } else {
+                                               s390_ldr   (code, ainfo->reg, inst->dreg);
+                                       }
+                               }
+                       }
+                       else if (ainfo->regtype == RegTypeBase) {
+                       } else
+                               g_assert_not_reached ();
+               } else {
+                       if (ainfo->regtype == RegTypeGeneral) {
+                               if (!((ainfo->reg >= 2) && (ainfo->reg <= 6)))
+                                       g_assert_not_reached();
+                               switch (ainfo->size) {
+                               case 1:
+                                       s390_ic (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
+                                       break;
+                               case 2:
+                                       s390_lh (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
+                                       break;
+                               case 8:
+                                       s390_lm (code, ainfo->reg, ainfo->reg + 1, 
+                                                 inst->inst_basereg, inst->inst_offset);
+                                       break;
+                               default:
+                                       s390_l  (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
+                               }
+                       } else if (ainfo->regtype == RegTypeBase) {
+                       } else if (ainfo->regtype == RegTypeFP) {
+                               if (ainfo->size == 8)
+                                       s390_ld (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
+                               else if (ainfo->size == 4)
+                                       s390_le (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
+                               else
+                                       g_assert_not_reached ();
+                       } else if (ainfo->regtype == RegTypeStructByVal) {
+                               if (ainfo->reg != STK_BASE) {
+                                       switch (ainfo->size) {
+                                       case 1:
+                                               s390_ic (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
+                                               break;
+                                       case 2:
+                                               s390_lh (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
+                                               break;
+                                       case 4:
+                                               s390_l  (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
+                                               break;
+                                       case 8:
+                                               s390_lm (code, ainfo->reg, ainfo->reg+1, inst->inst_basereg, inst->inst_offset);
+                                               break;
+                                       }
+                               }
+                       } else if (ainfo->regtype == RegTypeStructByAddr) {
+                               if (ainfo->reg != STK_BASE) {
+                                       s390_l  (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
+                               }
+                       } else
                                g_assert_not_reached ();
-                       }
-                       target = S390_RELATIVE(mono_icall_get_wrapper (mi), ip);
-                       ip    += 2;     /* Skip over op-code */
-                       break;
-               }
-               case MONO_PATCH_INFO_METHOD_JUMP: {
-                       GSList *list;
-
-                       /*------------------------------------------------------*/
-                       /* get the trampoline to the method from the domain     */
-                       /*------------------------------------------------------*/
-                       target = mono_create_jump_trampoline (domain, 
-                                                     patch_info->data.method, 
-                                                     TRUE);
-                       target = S390_RELATIVE(target, ip);
-                       if (!domain->jump_target_hash)
-                               domain->jump_target_hash = g_hash_table_new (NULL, NULL);
-                       list = g_hash_table_lookup (domain->jump_target_hash, 
-                                                   patch_info->data.method);
-                       list = g_slist_prepend (list, ip);
-                       g_hash_table_insert (domain->jump_target_hash, 
-                                            patch_info->data.method, list);
-                       ip  +=2;
-                       break;
-               }
-               case MONO_PATCH_INFO_METHOD:
-                       if (patch_info->data.method == method) {
-                               target = S390_RELATIVE(code, ip);
-                       } else {
-                               /* get the trampoline to the method from the domain */
-                               target = S390_RELATIVE(mono_arch_create_jit_trampoline (patch_info->data.method), ip);
-                               target = mono_arch_create_jit_trampoline(patch_info->data.method);
-                               target = S390_RELATIVE(target, ip);
-                       }
-                       ip    += 2;     /* Skip over op-code */
-                       break;
-               case MONO_PATCH_INFO_SWITCH: {
-                       gpointer *table = (gpointer *)patch_info->data.target;
-                       int i;
-                       /*------------------------------------------------------*/
-                       /* ip is pointing at the basr r13,0/j +4 instruction    */
-                       /* the vtable value follows this (i.e. ip+6)            */
-                       /*------------------------------------------------------*/
-                       *((gconstpointer *)(ip+6)) = table;
-
-                       for (i = 0; i < patch_info->table_size; i++) {
-                               table [i] = (int)patch_info->data.table [i] + code;
-                       }
-                       continue;
-               }
-               case MONO_PATCH_INFO_METHODCONST:
-               case MONO_PATCH_INFO_CLASS:
-               case MONO_PATCH_INFO_IMAGE:
-               case MONO_PATCH_INFO_FIELD:
-                       target = S390_RELATIVE(patch_info->data.target, ip);
-                       continue;
-               case MONO_PATCH_INFO_R4:
-               case MONO_PATCH_INFO_R8:
-                       g_assert_not_reached ();
-                       *((gconstpointer *)(ip + 2)) = patch_info->data.target;
-                       continue;
-               case MONO_PATCH_INFO_IID:
-                       mono_class_init (patch_info->data.klass);
-                       target = S390_RELATIVE(patch_info->data.klass->interface_id, ip);
-                       continue;                       
-               case MONO_PATCH_INFO_VTABLE:
-                       target = S390_RELATIVE(mono_class_vtable (domain, patch_info->data.klass),ip);
-                       ip += 2;
-                       continue;
-               case MONO_PATCH_INFO_CLASS_INIT:
-                       target = S390_RELATIVE(mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass)), ip);
-                       ip += 2;
-                       break;
-               case MONO_PATCH_INFO_SFLDA: {
-                       MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
-                       if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
-                               /* Done by the generated code */
-                               ;
-                       else {
-                               if (run_cctors)
-                                       mono_runtime_class_init (vtable);
-                       }
-                       target = S390_RELATIVE((char*)vtable->data + patch_info->data.field->offset, ip);
-                       ip += 2;
-                       continue;
-               }
-               case MONO_PATCH_INFO_EXC_NAME:
-                       *((gconstpointer *)(ip)) = patch_info->data.name;
-                       continue;
-               case MONO_PATCH_INFO_LDSTR:
-                       target = mono_ldstr (domain, patch_info->data.token->image, 
-                                            mono_metadata_token_index (patch_info->data.token->token));
-                       continue;
-               case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
-                       gpointer handle;
-                       MonoClass *handle_class;
-
-                       handle = mono_ldtoken (patch_info->data.token->image, 
-                                              patch_info->data.token->token, 
-                                              &handle_class, NULL);
-                       mono_class_init (handle_class);
-                       mono_class_init (mono_class_from_mono_type (handle));
-
-                       target = handle;
-                       continue;
                }
-               case MONO_PATCH_INFO_LDTOKEN: {
-                       gpointer handle;
-                       MonoClass *handle_class;
-
-                       handle = mono_ldtoken (patch_info->data.token->image,
-                                              patch_info->data.token->token, 
-                                              &handle_class, NULL);
-                       mono_class_init (handle_class);
-
-                       target = handle;
-                       continue;
-               }
-               case MONO_PATCH_INFO_EXC:
-                       /* everything is dealt with at epilog output time */
-                       continue;
-               default:
-                       g_assert_not_reached ();
-               }
-               s390_patch (ip, target);
+               pos++;
        }
-}
-
-/*========================= End of Function ========================*/
-
-/*------------------------------------------------------------------*/
-/*                                                                  */
-/* Name                - mono_arch_max_epilog_size                         */
-/*                                                                  */
-/* Function    - Determine the maximum size of the epilog code.    */
-/*                                                                 */
-/*------------------------------------------------------------------*/
-
-int
-mono_arch_max_epilog_size (MonoCompile *cfg)
-{
-       int max_epilog_size = 96;
-       MonoJumpInfo *patch_info;
-       
-       if (cfg->method->save_lmf)
-               max_epilog_size += 128;
-       
-       if (mono_jit_trace_calls != NULL)
-               max_epilog_size += 128;
-
-       if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
-               max_epilog_size += 128;
 
-       /* count the number of exception infos */
-     
-       for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
-               if (patch_info->type == MONO_PATCH_INFO_EXC)
-                       max_epilog_size += 26;
-       }
+       g_free (cinfo);
 
-       return max_epilog_size;
+       return code;
 }
 
 /*========================= End of Function ========================*/
@@ -4724,26 +4211,20 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        MonoBasicBlock *bb;
        MonoMethodSignature *sig;
        MonoInst *inst;
-       int alloc_size, pos, max_offset, i, lmfOffset;
+       int alloc_size, pos, max_offset, i;
        guint8 *code;
        CallInfo *cinfo;
        size_data sz;
        int tracing = 0;
+       int lmfOffset;                                                          \
 
        if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
                tracing = 1;
 
-       cfg->code_size   = 256;
+       cfg->code_size   = 512;
        cfg->native_code = code = g_malloc (cfg->code_size);
 
-       if (cfg->flags & MONO_CFG_HAS_TAIL) {
-               s390_stm (code, s390_r2, s390_r14, STK_BASE, S390_PARM_SAVE_OFFSET);
-               for (pos = 0; pos < 4; pos++)
-                       s390_std (code, pos, 0, STK_BASE, 
-                                 S390_FLOAT_SAVE_OFFSET+pos*sizeof(double));
-       } else { 
-               s390_stm  (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
-        }
+       s390_stm  (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
 
        if (cfg->flags & MONO_CFG_HAS_ALLOCA) {
                cfg->used_int_regs |= 1 << 11;
@@ -4753,7 +4234,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
 
        cfg->stack_usage = alloc_size;
        s390_lr   (code, s390_r11, STK_BASE);
-       if (s390_is_imm16 (-alloc_size)) {
+       if (s390_is_uimm16 (alloc_size)) {
                s390_ahi  (code, STK_BASE, -alloc_size);
        } else { 
                int stackSize = alloc_size;
@@ -4787,7 +4268,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        }
 
        /* load arguments allocated to register from the stack */
-       sig = method->signature;
+       sig = mono_method_signature (method);
        pos = 0;
 
        cinfo = calculate_sizes (sig, &sz, sig->pinvoke);
@@ -4884,20 +4365,8 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                                                break;
                                }
                        } else if (ainfo->regtype == RegTypeStructByAddr) {
-                               if (ainfo->reg == STK_BASE) {
-                                       s390_lr  (code, s390_r13, ainfo->reg);
-                                       s390_ahi (code, s390_r13, alloc_size);
-                                       s390_l   (code, s390_r13, 0, s390_r13, 
-                                                 ainfo->offparm + S390_MINIMAL_STACK_SIZE);
-                                       code = emit_memcpy (code, abs(ainfo->vtsize), 
-                                                           inst->inst_basereg, 
-                                                           inst->inst_offset, s390_r13, 0);
-                               } else {
-                                       code = emit_memcpy (code, abs(ainfo->vtsize), 
-                                                           inst->inst_basereg, 
-                                                           inst->inst_offset, 
-                                                           ainfo->reg, 0);
-                               }
+                               if (ainfo->reg != STK_BASE) 
+                                       s390_st  (code, ainfo->reg, 0, inst->inst_basereg, inst->inst_offset);
                        } else
                                g_assert_not_reached ();
                }
@@ -4905,87 +4374,88 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        }
 
        if (method->save_lmf) {
-               /*---------------------------------------------------------------*/
-               /* Preserve the parameter registers while we fix up the lmf      */
-               /*---------------------------------------------------------------*/
-               s390_lr (code, s390_r7, s390_r2);
-               s390_lr (code, s390_r8, s390_r3);
-               s390_lr (code, s390_r9, s390_r4);
-               s390_lr (code, s390_r10, s390_r5);
-
-               mono_add_patch_info (cfg, code - cfg->native_code, 
-                                    MONO_PATCH_INFO_INTERNAL_METHOD, 
-                                    (gpointer)"mono_get_lmf_addr");
-               /*---------------------------------------------------------------*/
-               /* On return from this call r2 have the address of the &lmf      */
-               /*---------------------------------------------------------------*/
-               s390_brasl (code, s390_r14, 0);
-
                /*---------------------------------------------------------------*/
                /* we build the MonoLMF structure on the stack - see mini-s390.h */
                /*---------------------------------------------------------------*/
-               lmfOffset = alloc_size - sizeof(MonoLMF);
-
-               s390_lr    (code, s390_r13, cfg->frame_reg);
-               s390_ahi   (code, s390_r13, lmfOffset);
-
-               /*---------------------------------------------------------------*/
-               /* Set lmf.lmf_addr = jit_tls->lmf                               */
-               /*---------------------------------------------------------------*/
-               s390_st    (code, s390_r2, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, lmf_addr));
-
-               /*---------------------------------------------------------------*/
-               /* Get current lmf                                               */
-               /*---------------------------------------------------------------*/
-               s390_l     (code, s390_r0, 0, s390_r2, 0);
-
-               /*---------------------------------------------------------------*/
-               /* Set our lmf as the current lmf                                */
+               lmfOffset = alloc_size - sizeof(MonoLMF);       
+                                                                                       
+               s390_lr    (code, s390_r13, cfg->frame_reg);            
+               s390_ahi   (code, s390_r13, lmfOffset);                                 
+                                                                                       
                /*---------------------------------------------------------------*/
-               s390_st    (code, s390_r13, 0, s390_r2, 0);
-
-               /*---------------------------------------------------------------*/
-               /* Have our lmf.previous_lmf point to the last lmf               */
+               /* Preserve the parameter registers while we fix up the lmf      */
                /*---------------------------------------------------------------*/
-               s390_st    (code, s390_r0, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, previous_lmf));
+               s390_stm   (code, s390_r2, s390_r6, s390_r13,
+                           G_STRUCT_OFFSET(MonoLMF, pregs[0]));
 
                /*---------------------------------------------------------------*/
-               /* save method info                                              */
+               /* On return from this call r2 have the address of the &lmf      */
                /*---------------------------------------------------------------*/
-               s390_basr  (code, s390_r1, 0);
-               s390_j     (code, 4);
-               s390_word  (code, method);
-               s390_l     (code, s390_r1, 0, s390_r1, 4);
-               s390_st    (code, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, method));
+               mono_add_patch_info (cfg, code - cfg->native_code, 
+                                    MONO_PATCH_INFO_INTERNAL_METHOD, 
+                                    (gpointer)"mono_get_lmf_addr");
+               s390_brasl (code, s390_r14, 0);
 
-               /*---------------------------------------------------------------*/
-               /* save the current IP                                           */
-               /*---------------------------------------------------------------*/
+               /*---------------------------------------------------------------*/     
+               /* Set lmf.lmf_addr = jit_tls->lmf                               */     
+               /*---------------------------------------------------------------*/     
+               s390_st    (code, s390_r2, 0, s390_r13,                                 
+                           G_STRUCT_OFFSET(MonoLMF, lmf_addr));                        
+                                                                                       
+               /*---------------------------------------------------------------*/     
+               /* Get current lmf                                               */     
+               /*---------------------------------------------------------------*/     
+               s390_l     (code, s390_r0, 0, s390_r2, 0);                              
+                                                                                       
+               /*---------------------------------------------------------------*/     
+               /* Set our lmf as the current lmf                                */     
+               /*---------------------------------------------------------------*/     
+               s390_st    (code, s390_r13, 0, s390_r2, 0);                             
+                                                                                       
+               /*---------------------------------------------------------------*/     
+               /* Have our lmf.previous_lmf point to the last lmf               */     
+               /*---------------------------------------------------------------*/     
+               s390_st    (code, s390_r0, 0, s390_r13,                                 
+                           G_STRUCT_OFFSET(MonoLMF, previous_lmf));                    
+                                                                                       
+               /*---------------------------------------------------------------*/     
+               /* save method info                                              */     
+               /*---------------------------------------------------------------*/     
+               s390_basr  (code, s390_r1, 0);                                          
+               s390_j     (code, 4);                                                   
+               s390_word  (code, method);                                              
+               s390_l     (code, s390_r1, 0, s390_r1, 4);                      
+               s390_st    (code, s390_r1, 0, s390_r13,                                 
+                           G_STRUCT_OFFSET(MonoLMF, method));                          
+                                                                               
+               /*---------------------------------------------------------------*/     
+               /* save the current IP                                           */     
+               /*---------------------------------------------------------------*/     
                s390_lr    (code, s390_r1, cfg->frame_reg);
-               s390_st    (code, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, ebp));
-               s390_l     (code, s390_r1, 0, s390_r1, S390_RET_ADDR_OFFSET);
-               s390_la    (code, s390_r1, 0, s390_r1, 0);
-               s390_st    (code, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, eip));
-
-               /*---------------------------------------------------------------*/
-               /* Save general and floating point registers                     */
-               /*---------------------------------------------------------------*/
-               s390_stm   (code, s390_r2, s390_r12, s390_r13, G_STRUCT_OFFSET(MonoLMF, gregs[2]));
-               for (i = 0; i < 16; i++) {
-                       s390_std  (code, i, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, fregs[i]));
-               }
+               s390_st    (code, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, ebp)); 
+               s390_basr  (code, s390_r1, 0);
+               s390_la    (code, s390_r1, 0, s390_r1, 0);                              
+               s390_st    (code, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, eip)); 
+                                                                                       
+               /*---------------------------------------------------------------*/     
+               /* Save general and floating point registers                     */     
+               /*---------------------------------------------------------------*/     
+               s390_stm   (code, s390_r2, s390_r12, s390_r13,                          
+                           G_STRUCT_OFFSET(MonoLMF, gregs[2]));                        
+               for (i = 0; i < 16; i++) {                                              
+                       s390_std  (code, i, 0, s390_r13,                                
+                                  G_STRUCT_OFFSET(MonoLMF, fregs[i]));                 
+               }                                                                       
 
                /*---------------------------------------------------------------*/
                /* Restore the parameter registers now that we've set up the lmf */
                /*---------------------------------------------------------------*/
-               s390_lr (code, s390_r2, s390_r7);
-               s390_lr (code, s390_r3, s390_r8);
-               s390_lr (code, s390_r4, s390_r9);
-               s390_lr (code, s390_r5, s390_r10);
+               s390_lm    (code, s390_r2, s390_r6, s390_r13,                           
+                           G_STRUCT_OFFSET(MonoLMF, pregs[0]));                        
        }
 
        if (tracing)
-               code = mono_arch_instrument_prolog (cfg, enter_method, code, TRUE);
+               code = mono_arch_instrument_prolog(cfg, enter_method, code, TRUE);
 
        cfg->code_len = code - cfg->native_code;
        g_free (cinfo);
@@ -5006,12 +4476,25 @@ mono_arch_emit_prolog (MonoCompile *cfg)
 void
 mono_arch_emit_epilog (MonoCompile *cfg)
 {
-       MonoJumpInfo *patch_info;
        MonoMethod *method = cfg->method;
-       MonoMethodSignature *sig = method->signature;
-       MonoInst *inst;
-       int i, tracing = 0;
+       int tracing = 0;
        guint8 *code;
+       int max_epilog_size = 96;
+       
+       if (cfg->method->save_lmf)
+               max_epilog_size += 128;
+       
+       if (mono_jit_trace_calls != NULL)
+               max_epilog_size += 128;
+
+       if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
+               max_epilog_size += 128;
+       
+       while ((cfg->code_len + max_epilog_size) > (cfg->code_size - 16)) {
+               cfg->code_size  *= 2;
+               cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
+               mono_jit_stats.code_reallocs++;
+       }
 
        code = cfg->native_code + cfg->code_len;
 
@@ -5021,47 +4504,117 @@ mono_arch_emit_epilog (MonoCompile *cfg)
        }
        
        if (method->save_lmf) 
-               code = restoreLMF(cfg, code);
+               restoreLMF(code, cfg->frame_reg, cfg->stack_usage);
 
        if (cfg->flags & MONO_CFG_HAS_ALLOCA) 
-               s390_l   (code, STK_BASE, 0, STK_BASE, 0);
+               s390_l (code, STK_BASE, 0, STK_BASE, 0);
        else
-               code = backUpStackPtr(cfg, code);
+               code = backUpStackPtr(cfg, code, STK_BASE);
 
        s390_lm  (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
        s390_br  (code, s390_r14);
 
-       /* add code to raise exceptions */
+       cfg->code_len = code - cfg->native_code;
+
+       g_assert (cfg->code_len < cfg->code_size);
+
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_emit_exceptions                         */
+/*                                                                  */
+/* Function    - Emit the blocks to handle exception conditions.   */
+/*                                                                 */
+/*------------------------------------------------------------------*/
+
+void
+mono_arch_emit_exceptions (MonoCompile *cfg) 
+{
+       MonoJumpInfo    *patch_info;
+       guint8          *code;
+       int             nThrows = 0,
+                       exc_count = 0,
+                       iExc;
+       guint32         code_size;
+       MonoClass       *exc_classes [MAX_EXC];
+       guint8          *exc_throw_start [MAX_EXC], 
+                       *exc_throw_end [MAX_EXC];
+
+       for (patch_info = cfg->patch_info; 
+            patch_info; 
+            patch_info = patch_info->next) {
+               if (patch_info->type == MONO_PATCH_INFO_EXC)
+                       exc_count++;
+       }
+
+       code_size = exc_count * 26;
+
+       while ((cfg->code_len + code_size) > (cfg->code_size - 16)) {
+               cfg->code_size  *= 2;
+               cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
+               mono_jit_stats.code_reallocs++; 
+       }
+
+       code = cfg->native_code + cfg->code_len;
+
+       /*---------------------------------------------------------------------*/
+       /* Add code to raise exceptions                                        */
+       /*---------------------------------------------------------------------*/
        for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
                switch (patch_info->type) {
                case MONO_PATCH_INFO_EXC: {
+                       guint8 *ip = patch_info->ip.i + cfg->native_code;
+                       MonoClass *exc_class;
+                       guint32 throw_ip;
+
                        /*-----------------------------------------------------*/
                        /* Patch the branch in epilog to come here             */
                        /*-----------------------------------------------------*/
-                       s390_patch (patch_info->ip.i+cfg->native_code+2, 
-                                   S390_RELATIVE(code,patch_info->ip.i+cfg->native_code));
-                       /*-----------------------------------------------------*/
-                       /* Patch the parameter passed to the handler           */ 
-                       /*-----------------------------------------------------*/
-                       s390_basr (code, s390_r13, 0);
-                       s390_j    (code, 4);
-                       mono_add_patch_info (cfg, code - cfg->native_code,
-                                            MONO_PATCH_INFO_EXC_NAME,
-                                            patch_info->data.target);
-                       s390_word (code, 0);
-                       /*-----------------------------------------------------*/
-                       /* Load the return address and the parameter register  */
-                       /*-----------------------------------------------------*/
-                       s390_larl (code, s390_r14, S390_RELATIVE((patch_info->ip.i +
-                                                  cfg->native_code + 8), code));
-                       s390_l    (code, s390_r2, 0, s390_r13, 4);
-                       /*-----------------------------------------------------*/
-                       /* Reuse the current patch to set the jump             */
-                       /*-----------------------------------------------------*/
-                       patch_info->type      = MONO_PATCH_INFO_INTERNAL_METHOD;
-                       patch_info->data.name = "mono_arch_throw_exception_by_name";
-                       patch_info->ip.i      = code - cfg->native_code;
-                       s390_jcl  (code, S390_CC_UN, 0);
+                       s390_patch (ip + 2, (guint32) (S390_RELATIVE(code,ip)));
+
+                       exc_class = mono_class_from_name (mono_defaults.corlib, 
+                                                         "System", 
+                                                         patch_info->data.name);
+                       g_assert (exc_class);
+                       throw_ip = patch_info->ip.i;
+
+                       for (iExc = 0; iExc < nThrows; ++iExc)
+                               if (exc_classes [iExc] == exc_class)
+                                       break;
+               
+                       if (iExc < nThrows) {
+                               s390_jcl (code, S390_CC_UN, (guint32) exc_throw_start [iExc]);
+                               patch_info->type = MONO_PATCH_INFO_NONE;
+                       } else {
+       
+                               if (nThrows < MAX_EXC) {
+                                       exc_classes [nThrows]     = exc_class;
+                                       exc_throw_start [nThrows] = code;
+                               }
+       
+                               /*---------------------------------------------*/
+                               /* Patch the parameter passed to the handler   */ 
+                               /*---------------------------------------------*/
+                               s390_basr (code, s390_r13, 0);
+                               s390_j    (code, 4);
+                               s390_word (code, patch_info->data.target);
+                               /*---------------------------------------------*/
+                               /* Load return address & parameter register    */
+                               /*---------------------------------------------*/
+                               s390_larl (code, s390_r14, S390_RELATIVE((patch_info->ip.i +
+                                                          cfg->native_code + 8), code));
+                               s390_l    (code, s390_r2, 0, s390_r13, 4);
+                               /*---------------------------------------------*/
+                               /* Reuse the current patch to set the jump     */
+                               /*---------------------------------------------*/
+                               patch_info->type      = MONO_PATCH_INFO_INTERNAL_METHOD;
+                               patch_info->data.name = "mono_arch_throw_exception_by_name";
+                               patch_info->ip.i      = code - cfg->native_code;
+                               s390_jcl  (code, S390_CC_UN, 0);
+                       }
                        break;
                }
                default:
@@ -5089,6 +4642,72 @@ mono_arch_emit_epilog (MonoCompile *cfg)
 void
 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
 {
+#ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
+       pthread_t       self = pthread_self();
+       pthread_attr_t  attr;
+       void            *stAddr = NULL;
+       size_t          stSize  = 0;
+       struct sigaltstack sa;
+#endif
+
+       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
+       }               
+
+#ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
+
+       /*----------------------------------------------------------*/
+       /* Determine stack boundaries                               */
+       /*----------------------------------------------------------*/
+       if (!mono_running_on_valgrind ()) {
+#ifdef HAVE_PTHREAD_GETATTR_NP
+               pthread_getattr_np( self, &attr );
+#elif HAVE_PTHREAD_ATTR_GET_NP
+               pthread_attr_get_np( self, &attr );
+#endif
+               pthread_attr_getstack( &attr, &stAddr, &stSize );
+       }
+
+
+       /*----------------------------------------------------------*/
+       /* Setup an alternate signal stack                          */
+       /*----------------------------------------------------------*/
+       tls->stack_size        = stSize;
+       tls->signal_stack      = mmap (0, SIGNAL_STACK_SIZE, 
+                                      PROT_READ|PROT_WRITE|PROT_EXEC, 
+                                      MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+       tls->signal_stack_size = SIGNAL_STACK_SIZE;
+
+       sa.ss_sp    = tls->signal_stack;
+       sa.ss_size  = SIGNAL_STACK_SIZE;
+       sa.ss_flags = SS_ONSTACK;
+       sigaltstack (&sa, NULL);
+#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);
+
 }
 
 /*========================= End of Function ========================*/
@@ -5104,6 +4723,18 @@ mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
 void
 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
 {
+#ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
+       struct sigaltstack sa;
+
+       sa.ss_sp    = tls->signal_stack;
+       sa.ss_size  = SIGNAL_STACK_SIZE;
+       sa.ss_flags = SS_DISABLE;
+       sigaltstack (&sa, NULL);
+
+       if (tls->signal_stack)
+               munmap(tls->signal_stack, SIGNAL_STACK_SIZE);
+#endif
+
 }
 
 /*========================= End of Function ========================*/
@@ -5128,19 +4759,21 @@ mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_re
        if (this_reg != -1) {
                MonoInst *this;
                MONO_INST_NEW (cfg, this, OP_SETREG);
-               this->type = this_type;
+               this->type  = this_type;
                this->sreg1 = this_reg;
-               this->dreg = this_dreg;
+               this->dreg  = mono_regstate_next_int (cfg->rs);
                mono_bblock_add_inst (cfg->cbb, this);
+               mono_call_inst_add_outarg_reg (inst, this->dreg, this_dreg, FALSE);
        }
 
        if (vt_reg != -1) {
                MonoInst *vtarg;
                MONO_INST_NEW (cfg, vtarg, OP_SETREG);
-               vtarg->type = STACK_MP;
+               vtarg->type  = STACK_MP;
                vtarg->sreg1 = vt_reg;
-               vtarg->dreg = s390_r2;
+               vtarg->dreg  = mono_regstate_next_int (cfg->rs);
                mono_bblock_add_inst (cfg->cbb, vtarg);
+               mono_call_inst_add_outarg_reg (inst, vtarg->dreg, s390_r2, FALSE);
        }
 }
 
@@ -5148,21 +4781,66 @@ mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_re
 
 /*------------------------------------------------------------------*/
 /*                                                                  */
-/* Name                - mono_arch_get_opcode_for_method                   */
+/* Name                - mono_arch_get_inst_for_method                   */
 /*                                                                  */
 /* Function    - Check for opcodes we can handle directly in       */
 /*               hardware.                                         */
 /*                                                                 */
 /*------------------------------------------------------------------*/
 
-gint
-mono_arch_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
+MonoInst*
+mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, 
+                              MonoMethodSignature *fsig, MonoInst **args)
 {
+       MonoInst *ins = NULL;
+
        if (cmethod->klass == mono_defaults.math_class) {
-               if (strcmp (cmethod->name, "Sqrt") == 0)
-                       return OP_SQRT;
+               if (strcmp (cmethod->name, "Sqrt") == 0) {
+                       MONO_INST_NEW (cfg, ins, OP_SQRT);
+                       ins->inst_i0 = args [0];
+               }
+       } else if (cmethod->klass == mono_defaults.thread_class &&
+                          strcmp (cmethod->name, "MemoryBarrier") == 0) {
+               MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
+       } else if(cmethod->klass->image == mono_defaults.corlib &&
+                          (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
+                          (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
+
+               if (strcmp (cmethod->name, "Increment") == 0 && 
+                   fsig->params [0]->type == MONO_TYPE_I4) {
+                       MonoInst *ins_iconst;
+
+                       MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_NEW_I4);
+                       MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
+                       ins_iconst->inst_c0 = 1;
+
+                       ins->inst_i0 = args [0];
+                       ins->inst_i1 = ins_iconst;
+               } else if (strcmp (cmethod->name, "Decrement") == 0 && 
+                          fsig->params [0]->type == MONO_TYPE_I4) {
+                       MonoInst *ins_iconst;
+
+                       MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_NEW_I4);
+                       MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
+                       ins_iconst->inst_c0 = -1;
+
+                       ins->inst_i0 = args [0];
+                       ins->inst_i1 = ins_iconst;
+               } else if (strcmp (cmethod->name, "Exchange") == 0 && 
+                          fsig->params [0]->type == MONO_TYPE_I4) {
+                       MONO_INST_NEW (cfg, ins, OP_ATOMIC_EXCHANGE_I4);
+
+                       ins->inst_i0 = args [0];
+                       ins->inst_i1 = args [1];
+               } else if (strcmp (cmethod->name, "Add") == 0 && 
+                          fsig->params [0]->type == MONO_TYPE_I4) {
+                       MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_I4);
+
+                       ins->inst_i0 = args [0];
+                       ins->inst_i1 = args [1];
+               }
        }
-       return -1;
+       return ins;
 }
 
 /*========================= End of Function ========================*/
@@ -5185,23 +4863,41 @@ mono_arch_print_tree (MonoInst *tree, int arity)
 
        switch (tree->opcode) {
                case OP_S390_LOADARG:
+               case OP_S390_ARGREG:
                case OP_S390_ARGPTR:
-               case OP_S390_STKARG:
-                       printf ("[0x%x(%s)]", tree->inst_offset, 
+                       printf ("[0x%lx(%s)]", tree->inst_offset, 
                                mono_arch_regname (tree->inst_basereg));
                        done = 1;
                        break;
+               case OP_S390_STKARG:
+                       printf ("[0x%lx(previous_frame)]", 
+                               tree->inst_offset); 
+                       done = 1;
+                       break;
                case OP_S390_MOVE:
-                       printf ("[0x%x(%d,%s),0x%x(%s)]",
+                       printf ("[0x%lx(%d,%s),0x%lx(%s)]",
                                tree->inst_offset, tree->unused,
-                               tree->dreg, tree->inst_imm, 
-                               tree->sreg1);
+                               mono_arch_regname(tree->dreg), tree->inst_imm, 
+                               mono_arch_regname(tree->sreg1));
                        done = 1;
                        break;
                case OP_S390_SETF4RET:
-                       printf ("[f%d,f%d]", 
+                       printf ("[f%ld,f%ld]", 
                                mono_arch_regname (tree->dreg),
                                mono_arch_regname (tree->sreg1));
+                       done = 1;
+                       break;
+               case OP_TLS_GET:
+                       printf ("[0x%lx(0x%lx,%s)]", tree->inst_offset,
+                               tree->inst_imm,
+                               mono_arch_regname (tree->sreg1));
+                       done = 1;
+                       break;
+               case OP_S390_BKCHAIN:
+                       printf ("[previous_frame(%s)]", 
+                               mono_arch_regname (tree->sreg1));
+                       done = 1;
+                       break;
                default:
                        done = 0;
        }
@@ -5227,17 +4923,93 @@ guint32
 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
 {
        /* FIXME: */
-       return 3;
+       return 2;
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_get_domain_intrinsic                    */
+/*                                                                  */
+/* Function    -                                                   */
+/*                                                                 */
+/* Returns     -                                                   */
+/*                                                                  */
+/*------------------------------------------------------------------*/
+
+MonoInst * 
+mono_arch_get_domain_intrinsic (MonoCompile* cfg)
+{
+       MonoInst *ins;
+
+       if (appdomain_tls_offset == -1)
+               return NULL;
+       
+       MONO_INST_NEW (cfg, ins, OP_TLS_GET);
+       ins->inst_offset = appdomain_tls_offset;
+       return (ins);
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_get_thread_intrinsic                    */
+/*                                                                  */
+/* Function    -                                                   */
+/*                                                                 */
+/* Returns     -                                                   */
+/*                                                                  */
+/*------------------------------------------------------------------*/
+
+MonoInst * 
+mono_arch_get_thread_intrinsic (MonoCompile* cfg)
+{
+       MonoInst *ins;
+
+       if (thread_tls_offset == -1)
+               return NULL;
+       
+       MONO_INST_NEW (cfg, ins, OP_TLS_GET);
+       ins->inst_offset = thread_tls_offset;
+       return (ins);
 }
 
 /*========================= End of Function ========================*/
 
-MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_flush_register_windows                  */
+/*                                                                  */
+/* Function    -                                                   */
+/*                                                                 */
+/* Returns     -                                                   */
+/*                                                                  */
+/*------------------------------------------------------------------*/
+
+void 
+mono_arch_flush_register_windows (void)
 {
-       return NULL;
 }
 
-MonoInst* mono_arch_get_thread_intrinsic (MonoCompile* cfg)
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_get_lmf_addr                            */
+/*                                                                  */
+/* Function    -                                                   */
+/*                                                                 */
+/* Returns     -                                                   */
+/*                                                                  */
+/*------------------------------------------------------------------*/
+
+gpointer
+mono_arch_get_lmf_addr (void)
 {
-       return NULL;
+        return pthread_getspecific (lmf_addr_key);
 }
+
+
+/*========================= End of Function ========================*/