Merge pull request #439 from mono-soc-2012/garyb/iconfix
[mono.git] / mono / mini / mini-ppc.c
old mode 100755 (executable)
new mode 100644 (file)
index 6c962ba..e779862
@@ -190,7 +190,7 @@ emit_memcpy (guint8 *code, int size, int dreg, int doffset, int sreg, int soffse
 
                ppc_load (code, ppc_r0, shifted);
                ppc_mtctr (code, ppc_r0);
-               g_assert (sreg == ppc_r11);
+               //g_assert (sreg == ppc_r11);
                ppc_addi (code, ppc_r12, dreg, (doffset - sizeof (gpointer)));
                ppc_addi (code, ppc_r11, sreg, (soffset - sizeof (gpointer)));
                copy_loop_start = code;
@@ -274,7 +274,7 @@ emit_memcpy (guint8 *code, int size, int dreg, int doffset, int sreg, int soffse
  * Returns the size of the activation frame.
  */
 int
-mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
+mono_arch_get_argument_info (MonoGenericSharingContext *gsctx, MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
 {
 #ifdef __mono_ppc64__
        NOT_IMPLEMENTED;
@@ -341,6 +341,9 @@ is_load_sequence (guint32 *seq)
 #define ppc_load_get_off(l)    ((gint16)((l) & 0xffff))
 #endif
 
+/* ld || lwz */
+#define ppc_is_load_op(opcode) (ppc_opcode ((opcode)) == 58 || ppc_opcode ((opcode)) == 32)
+
 /* code must point to the blrl */
 gboolean
 mono_ppc_is_direct_call_sequence (guint32 *code)
@@ -350,12 +353,12 @@ mono_ppc_is_direct_call_sequence (guint32 *code)
 
        /* the thunk-less direct call sequence: lis/ori/sldi/oris/ori/mtlr/blrl */
        if (ppc_opcode (code [-1]) == 31) { /* mtlr */
-               if (ppc_opcode (code [-2]) == 58 && ppc_opcode (code [-3]) == 58) { /* ld/ld */
+               if (ppc_is_load_op (code [-2]) && ppc_is_load_op (code [-3])) { /* ld/ld */
                        if (!is_load_sequence (&code [-8]))
                                return FALSE;
-                       /* one of the loads must be "ld r2,8(rX)" */
-                       return (ppc_load_get_dest (code [-2]) == ppc_r2 && ppc_load_get_off (code [-2]) == 8) ||
-                               (ppc_load_get_dest (code [-3]) == ppc_r2 && ppc_load_get_off (code [-3]) == 8);
+                       /* one of the loads must be "ld r2,8(rX)" or "ld r2,4(rX) for ilp32 */
+                       return (ppc_load_get_dest (code [-2]) == ppc_r2 && ppc_load_get_off (code [-2]) == sizeof (gpointer)) ||
+                               (ppc_load_get_dest (code [-3]) == ppc_r2 && ppc_load_get_off (code [-3]) == sizeof (gpointer));
                }
                if (ppc_opcode (code [-2]) == 24 && ppc_opcode (code [-3]) == 31) /* mr/nop */
                        return is_load_sequence (&code [-8]);
@@ -373,53 +376,6 @@ mono_ppc_is_direct_call_sequence (guint32 *code)
 #endif
 }
 
-gpointer
-mono_arch_get_vcall_slot (guint8 *code_ptr, mgreg_t *regs, int *displacement)
-{
-       char *o = NULL;
-       int reg, offset = 0;
-       guint32* code = (guint32*)code_ptr;
-       mgreg_t *r = (mgreg_t*)regs;
-
-       *displacement = 0;
-
-       /* This is the 'blrl' instruction */
-       --code;
-
-       /* Sanity check: instruction must be 'blrl' */
-       if (*code != 0x4e800021)
-               return NULL;
-
-       if (mono_ppc_is_direct_call_sequence (code))
-               return NULL;
-
-       /* FIXME: more sanity checks here */
-       /* OK, we're now at the 'blrl' instruction. Now walk backwards
-       till we get to a 'mtlr rA' */
-       for (; --code;) {
-               if((*code & 0x7c0803a6) == 0x7c0803a6) {
-                       gint16 soff;
-                       /* Here we are: we reached the 'mtlr rA'.
-                       Extract the register from the instruction */
-                       reg = (*code & 0x03e00000) >> 21;
-                       --code;
-                       /* ok, this is a lwz reg, offset (vtreg) 
-                        * it is emitted with:
-                        * ppc_emit32 (c, (32 << 26) | ((D) << 21) | ((a) << 16) | (guint16)(d))
-                        */
-                       soff = (*code & 0xffff);
-                       offset = soff;
-                       reg = (*code >> 16) & 0x1f;
-                       g_assert (reg != ppc_r1);
-                       /*g_print ("patching reg is %d\n", reg);*/
-                       o = (gpointer)(gsize)r [reg];
-                       break;
-               }
-       }
-       *displacement = offset;
-       return o;
-}
-
 #define MAX_ARCH_DELEGATE_PARAMS 7
 
 static gpointer
@@ -488,11 +444,11 @@ mono_arch_get_delegate_invoke_impls (void)
        int i;
 
        code = get_delegate_invoke_impl (TRUE, 0, &code_len, TRUE);
-       res = g_slist_prepend (res, mono_aot_tramp_info_create (g_strdup ("delegate_invoke_impl_has_target"), code, code_len));
+       res = g_slist_prepend (res, mono_tramp_info_create (g_strdup ("delegate_invoke_impl_has_target"), code, code_len, NULL, NULL));
 
        for (i = 0; i < MAX_ARCH_DELEGATE_PARAMS; ++i) {
                code = get_delegate_invoke_impl (FALSE, i, &code_len, TRUE);
-               res = g_slist_prepend (res, mono_aot_tramp_info_create (g_strdup_printf ("delegate_invoke_impl_target_%d", i), code, code_len));
+               res = g_slist_prepend (res, mono_tramp_info_create (g_strdup_printf ("delegate_invoke_impl_target_%d", i), code, code_len, NULL, NULL));
        }
 
        return res;
@@ -514,7 +470,7 @@ mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_targe
                        return cached;
 
                if (mono_aot_only)
-                       start = mono_aot_get_named_code ("delegate_invoke_impl_has_target");
+                       start = mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
                else
                        start = get_delegate_invoke_impl (TRUE, 0, NULL, FALSE);
 
@@ -538,7 +494,7 @@ mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_targe
 
                if (mono_aot_only) {
                        char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
-                       start = mono_aot_get_named_code (name);
+                       start = mono_aot_get_trampoline (name);
                        g_free (name);
                } else {
                        start = get_delegate_invoke_impl (FALSE, sig->param_count, NULL, FALSE);
@@ -552,13 +508,10 @@ mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_targe
 }
 
 gpointer
-mono_arch_get_this_arg_from_call (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, mgreg_t *regs, guint8 *code)
+mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
 {
        mgreg_t *r = (mgreg_t*)regs;
 
-       /* FIXME: handle returning a struct */
-       if (MONO_TYPE_ISSTRUCT (sig->ret))
-               return (gpointer)(gsize)r [ppc_r4];
        return (gpointer)(gsize)r [ppc_r3];
 }
 
@@ -611,7 +564,16 @@ linux_find_auxv (int *count)
 void
 mono_arch_cpu_init (void)
 {
-#ifdef __APPLE__
+}
+
+/*
+ * Initialize architecture specific code.
+ */
+void
+mono_arch_init (void)
+{
+#if defined(MONO_CROSS_COMPILE)
+#elif defined(__APPLE__)
        int mib [3];
        size_t len;
        mib [0] = CTL_HW;
@@ -665,7 +627,6 @@ mono_arch_cpu_init (void)
 #elif defined(G_COMPILER_CODEWARRIOR)
        cachelinesize = 32;
        cachelineinc = 32;
-#elif defined(MONO_CROSS_COMPILE)
 #else
 //#error Need a way to get cache line size
 #endif
@@ -676,19 +637,13 @@ mono_arch_cpu_init (void)
 
        if (mono_cpu_count () > 1)
                cpu_hw_caps |= PPC_SMP_CAPABLE;
-}
-
-/*
- * Initialize architecture specific code.
- */
-void
-mono_arch_init (void)
-{
        InitializeCriticalSection (&mini_arch_mutex);
 
        ss_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_32BIT);
        bp_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_32BIT);
        mono_mprotect (bp_trigger_page, mono_pagesize (), 0);
+
+       mono_aot_register_jit_icall ("mono_ppc_throw_exception", mono_ppc_throw_exception);
 }
 
 /*
@@ -704,7 +659,7 @@ mono_arch_cleanup (void)
  * This function returns the optimizations supported on this cpu.
  */
 guint32
-mono_arch_cpu_optimizazions (guint32 *exclude_mask)
+mono_arch_cpu_optimizations (guint32 *exclude_mask)
 {
        guint32 opts = 0;
 
@@ -713,6 +668,19 @@ mono_arch_cpu_optimizazions (guint32 *exclude_mask)
        return opts;
 }
 
+/*
+ * This function test for all SIMD functions supported.
+ *
+ * Returns a bitmask corresponding to all supported versions.
+ *
+ */
+guint32
+mono_arch_cpu_enumerate_simd_versions (void)
+{
+       /* SIMD is currently unimplemented */
+       return 0;
+}
+
 #ifdef __mono_ppc64__
 #define CASE_PPC32(c)
 #define CASE_PPC64(c)  case c:
@@ -937,6 +905,8 @@ typedef struct {
        guint32 struct_ret;
        ArgInfo ret;
        ArgInfo sig_cookie;
+       gboolean vtype_retaddr;
+       int vret_arg_index;
        ArgInfo args [1];
 } CallInfo;
 
@@ -1004,30 +974,59 @@ has_only_a_r48_field (MonoClass *klass)
 #endif
 
 static CallInfo*
-calculate_sizes (MonoMethodSignature *sig, gboolean is_pinvoke)
+get_call_info (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig)
 {
-       guint i, fr, gr;
+       guint i, fr, gr, pstart;
        int n = sig->hasthis + sig->param_count;
        MonoType *simpletype;
        guint32 stack_size = 0;
        CallInfo *cinfo = g_malloc0 (sizeof (CallInfo) + sizeof (ArgInfo) * n);
+       gboolean is_pinvoke = sig->pinvoke;
 
        fr = PPC_FIRST_FPARG_REG;
        gr = PPC_FIRST_ARG_REG;
 
        /* FIXME: handle returning a struct */
        if (MONO_TYPE_ISSTRUCT (sig->ret)) {
-               add_general (&gr, &stack_size, &cinfo->ret, TRUE);
-               cinfo->struct_ret = PPC_FIRST_ARG_REG;
+               cinfo->vtype_retaddr = TRUE;
        }
 
+       pstart = 0;
        n = 0;
-       if (sig->hasthis) {
-               add_general (&gr, &stack_size, cinfo->args + n, TRUE);
-               n++;
+       /*
+        * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
+        * the first argument, allowing 'this' to be always passed in the first arg reg.
+        * Also do this if the first argument is a reference type, since virtual calls
+        * are sometimes made using calli without sig->hasthis set, like in the delegate
+        * invoke wrappers.
+        */
+       if (cinfo->vtype_retaddr && !is_pinvoke && (sig->hasthis || (sig->param_count > 0 && MONO_TYPE_IS_REFERENCE (mini_type_get_underlying_type (gsctx, sig->params [0]))))) {
+               if (sig->hasthis) {
+                       add_general (&gr, &stack_size, cinfo->args + 0, TRUE);
+                       n ++;
+               } else {
+                       add_general (&gr, &stack_size, &cinfo->args [sig->hasthis + 0], TRUE);
+                       pstart = 1;
+                       n ++;
+               }
+               add_general (&gr, &stack_size, &cinfo->ret, TRUE);
+               cinfo->struct_ret = cinfo->ret.reg;
+               cinfo->vret_arg_index = 1;
+       } else {
+               /* this */
+               if (sig->hasthis) {
+                       add_general (&gr, &stack_size, cinfo->args + 0, TRUE);
+                       n ++;
+               }
+
+               if (cinfo->vtype_retaddr) {
+                       add_general (&gr, &stack_size, &cinfo->ret, TRUE);
+                       cinfo->struct_ret = cinfo->ret.reg;
+               }
        }
+
         DEBUG(printf("params: %d\n", sig->param_count));
-       for (i = 0; i < sig->param_count; ++i) {
+       for (i = pstart; i < sig->param_count; ++i) {
                if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
                         /* Prevent implicit arguments and sig_cookie from
                           being passed in registers */
@@ -1213,6 +1212,7 @@ calculate_sizes (MonoMethodSignature *sig, gboolean is_pinvoke)
                        g_error ("Can't trampoline 0x%x", sig->params [i]->type);
                }
        }
+       cinfo->nargs = n;
 
        if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
                /* Prevent implicit arguments and sig_cookie from
@@ -1277,33 +1277,34 @@ calculate_sizes (MonoMethodSignature *sig, gboolean is_pinvoke)
        return cinfo;
 }
 
-static void
-allocate_tailcall_valuetype_addrs (MonoCompile *cfg)
+gboolean
+mono_ppc_tail_call_supported (MonoMethodSignature *caller_sig, MonoMethodSignature *callee_sig)
 {
-#if !PPC_PASS_STRUCTS_BY_VALUE
-       MonoMethodSignature *sig = mono_method_signature (cfg->method);
-       int num_structs = 0;
+       CallInfo *c1, *c2;
+       gboolean res;
        int i;
 
-       if (!(cfg->flags & MONO_CFG_HAS_TAIL))
-               return;
-
-       for (i = 0; i < sig->param_count; ++i) {
-               MonoType *type = mono_type_get_underlying_type (sig->params [i]);
-               if (type->type == MONO_TYPE_VALUETYPE)
-                       num_structs++;
+       c1 = get_call_info (NULL, caller_sig);
+       c2 = get_call_info (NULL, callee_sig);
+       res = c1->stack_usage >= c2->stack_usage;
+       if (callee_sig->ret && MONO_TYPE_ISSTRUCT (callee_sig->ret))
+               /* An address on the callee's stack is passed as the first argument */
+               res = FALSE;
+       for (i = 0; i < c2->nargs; ++i) {
+               if (c2->args [i].regtype == RegTypeStructByAddr)
+                       /* An address on the callee's stack is passed as the argument */
+                       res = FALSE;
        }
 
-       if (num_structs) {
-               cfg->tailcall_valuetype_addrs =
-                       mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * num_structs);
-               for (i = 0; i < num_structs; ++i) {
-                       cfg->tailcall_valuetype_addrs [i] =
-                               mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
-                       cfg->tailcall_valuetype_addrs [i]->flags |= MONO_INST_INDIRECT;
-               }
-       }
-#endif
+       /*
+       if (!mono_debug_count ())
+               res = FALSE;
+       */
+
+       g_free (c1);
+       g_free (c2);
+
+       return res;
 }
 
 /*
@@ -1321,8 +1322,6 @@ mono_arch_allocate_vars (MonoCompile *m)
        gint32 *offsets;
        guint32 locals_stack_size, locals_stack_align;
 
-       allocate_tailcall_valuetype_addrs (m);
-
        m->flags |= MONO_CFG_HAS_SPILLUP;
 
        /* allow room for the vararg method args: void* and long/double */
@@ -1431,7 +1430,7 @@ mono_arch_allocate_vars (MonoCompile *m)
                offset += sizeof(gpointer);
        }
 
-       offsets = mono_allocate_stack_slots_full (m, FALSE, &locals_stack_size, &locals_stack_align);
+       offsets = mono_allocate_stack_slots (m, FALSE, &locals_stack_size, &locals_stack_align);
        if (locals_stack_align) {
                offset += (locals_stack_align - 1);
                offset &= ~(locals_stack_align - 1);
@@ -1477,6 +1476,11 @@ mono_arch_allocate_vars (MonoCompile *m)
                        }
                        if (MONO_TYPE_ISSTRUCT (sig->params [i]) && size < sizeof (gpointer))
                                size = align = sizeof (gpointer);
+                       /* 
+                        * Use at least 4/8 byte alignment, since these might be passed in registers, and
+                        * they are saved using std in the prolog.
+                        */
+                       align = sizeof (gpointer);
                        offset += align - 1;
                        offset &= ~(align - 1);
                        inst->inst_offset = offset;
@@ -1499,7 +1503,7 @@ mono_arch_allocate_vars (MonoCompile *m)
        m->stack_offset = offset;
 
        if (sig->call_convention == MONO_CALL_VARARG) {
-               CallInfo *cinfo = calculate_sizes (m->method->signature, m->method->signature->pinvoke);
+               CallInfo *cinfo = get_call_info (m->generic_sharing_context, m->method->signature);
 
                m->sig_cookie = cinfo->sig_cookie.offset;
 
@@ -1545,7 +1549,7 @@ mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
        sig = call->signature;
        n = sig->param_count + sig->hasthis;
        
-       cinfo = calculate_sizes (sig, sig->pinvoke);
+       cinfo = get_call_info (cfg->generic_sharing_context, sig);
 
        for (i = 0; i < n; ++i) {
                ArgInfo *ainfo = cinfo->args + i;
@@ -1672,6 +1676,8 @@ mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
        g_free (cinfo);
 }
 
+#ifndef DISABLE_JIT
+
 void
 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
 {
@@ -1787,6 +1793,8 @@ mono_arch_is_inst_imm (gint64 imm)
        return TRUE;
 }
 
+#endif /* DISABLE_JIT */
+
 /*
  * Allow tracing to work with this interface (with an optional argument)
  */
@@ -2278,12 +2286,9 @@ mono_arch_decompose_long_opts (MonoCompile *cfg, MonoInst *ins)
                NULLIFY_INS (ins);
                break;
        case OP_LNEG:
-               /* This is the old version from inssel-long32.brg */
-               MONO_EMIT_NEW_UNALU (cfg, OP_INOT, ins->dreg + 1, ins->sreg1 + 1);
-               MONO_EMIT_NEW_UNALU (cfg, OP_INOT, ins->dreg + 2, ins->sreg1 + 2);
-               /* ADC sets the condition codes */
-               MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADC_IMM, ins->dreg + 1, ins->dreg + 1, 1);
-               MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADC_IMM, ins->dreg + 2, ins->dreg + 2, 0);
+               /* From gcc generated code */
+               MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PPC_SUBFIC, ins->dreg + 1, ins->sreg1 + 1, 0);
+               MONO_EMIT_NEW_UNALU (cfg, OP_PPC_SUBFZE, ins->dreg + 2, ins->sreg1 + 2);
                NULLIFY_INS (ins);
                break;
        default:
@@ -2915,12 +2920,12 @@ ppc_patch_full (guchar *code, const guchar *target, gboolean is_fd)
                /* the trampoline code will try to patch the blrl, blr, bcctr */
                if (ins == 0x4e800021 || ins == 0x4e800020 || ins == 0x4e800420) {
                        branch_ins = seq;
-                       if (ppc_opcode (seq [-3]) == 58 || ppc_opcode (seq [-3]) == 31) /* ld || mr */
+                       if (ppc_is_load_op (seq [-3]) || ppc_opcode (seq [-3]) == 31) /* ld || lwz || mr */
                                code -= 32;
                        else
                                code -= 24;
                } else {
-                       if (ppc_opcode (seq [5]) == 58 || ppc_opcode (seq [5]) == 31) /* ld || mr */
+                       if (ppc_is_load_op (seq [5]) || ppc_opcode (seq [5]) == 31) /* ld || lwz || mr */
                                branch_ins = seq + 8;
                        else
                                branch_ins = seq + 6;
@@ -2930,8 +2935,8 @@ ppc_patch_full (guchar *code, const guchar *target, gboolean is_fd)
                /* this is the lis/ori/sldi/oris/ori/(ld/ld|mr/nop)/mtlr/blrl sequence */
                g_assert (mono_ppc_is_direct_call_sequence (branch_ins));
 
-               if (ppc_opcode (seq [5]) == 58) {       /* ld */
-                       g_assert (ppc_opcode (seq [6]) == 58); /* ld */
+               if (ppc_is_load_op (seq [5])) {
+                       g_assert (ppc_is_load_op (seq [6]));
 
                        if (!is_fd) {
                                guint8 *buf = (guint8*)&seq [5];
@@ -2997,201 +3002,10 @@ emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
        return code;
 }
 
-/*
- * emit_load_volatile_arguments:
- *
- *  Load volatile arguments from the stack to the original input registers.
- * Required before a tail call.
- */
-static guint8*
-emit_load_volatile_arguments (MonoCompile *cfg, guint8 *code)
-{
-       MonoMethod *method = cfg->method;
-       MonoMethodSignature *sig;
-       MonoInst *inst;
-       CallInfo *cinfo;
-       guint32 i, pos;
-       int struct_index = 0;
-
-       sig = mono_method_signature (method);
-
-       /* This is the opposite of the code in emit_prolog */
-
-       pos = 0;
-
-       cinfo = calculate_sizes (sig, sig->pinvoke);
-
-       if (MONO_TYPE_ISSTRUCT (sig->ret)) {
-               ArgInfo *ainfo = &cinfo->ret;
-               inst = cfg->vret_addr;
-               g_assert (ppc_is_imm16 (inst->inst_offset));
-               ppc_ldptr (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
-       }
-       for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
-               ArgInfo *ainfo = cinfo->args + i;
-               inst = cfg->args [pos];
-
-               g_assert (inst->opcode != OP_REGVAR);
-               g_assert (ppc_is_imm16 (inst->inst_offset));
-
-               switch (ainfo->regtype) {
-               case RegTypeGeneral:
-                       switch (ainfo->size) {
-                               case 1:
-                                       ppc_lbz (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
-                                       break;
-                               case 2:
-                                       ppc_lhz (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
-                                       break;
-#ifdef __mono_ppc64__
-                               case 4:
-                                       ppc_lwz (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
-                                       break;
-#endif
-                               default:
-                                       ppc_ldptr (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
-                                       break;
-                       }
-                       break;
-
-               case RegTypeFP:
-                       switch (ainfo->size) {
-                               case 4:
-                                       ppc_lfs (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
-                                       break;
-                               case 8:
-                                       ppc_lfd (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
-                                       break;
-                               default:
-                                       g_assert_not_reached ();
-                       }
-                       break;
-
-               case RegTypeBase: {
-                       MonoType *type = mini_type_get_underlying_type (cfg->generic_sharing_context,
-                               &inst->klass->byval_arg);
-
-#ifndef __mono_ppc64__
-                       if (type->type == MONO_TYPE_I8)
-                               NOT_IMPLEMENTED;
-#endif
-
-                       if (MONO_TYPE_IS_REFERENCE (type) || type->type == MONO_TYPE_I8) {
-                               ppc_ldptr (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
-                               ppc_stptr (code, ppc_r0, ainfo->offset, ainfo->reg);
-                       } else if (type->type == MONO_TYPE_I4) {
-                               ppc_lwz (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
-                               ppc_stw (code, ppc_r0, ainfo->offset, ainfo->reg);
-                       } else {
-                               NOT_IMPLEMENTED;
-                       }
-
-                       break;
-               }
-
-               case RegTypeStructByVal: {
-#ifdef __APPLE__
-                       guint32 size = 0;
-#endif
-                       int j;
-
-                       /* FIXME: */
-                       if (ainfo->vtsize)
-                               NOT_IMPLEMENTED;
-#ifdef __APPLE__
-                       /*
-                        * Darwin pinvokes needs some special handling
-                        * for 1 and 2 byte arguments
-                        */
-                       if (method->signature->pinvoke)
-                               size = mono_class_native_size (inst->klass, NULL);
-                       if (size == 1 || size == 2) {
-                               /* FIXME: */
-                               NOT_IMPLEMENTED;
-                       } else
-#endif
-                               for (j = 0; j < ainfo->vtregs; ++j) {
-                                       ppc_ldptr (code, ainfo->reg + j,
-                                                       inst->inst_offset + j * sizeof (gpointer),
-                                                       inst->inst_basereg);
-                                       /* FIXME: shift to the right */
-                                       if (ainfo->bytes)
-                                               NOT_IMPLEMENTED;
-                               }
-                       break;
-               }
-
-               case RegTypeStructByAddr: {
-                       MonoInst *addr = cfg->tailcall_valuetype_addrs [struct_index];
-
-                       g_assert (ppc_is_imm16 (addr->inst_offset));
-                       g_assert (!ainfo->offset);
-                       ppc_ldptr (code, ainfo->reg, addr->inst_offset, addr->inst_basereg);
-
-                       struct_index++;
-                       break;
-               }
-
-               default:
-                       g_assert_not_reached ();
-               }
-
-               pos ++;
-       }
-
-       g_free (cinfo);
-
-       return code;
-}
-
-/* This must be kept in sync with emit_load_volatile_arguments(). */
 static int
 ins_native_length (MonoCompile *cfg, MonoInst *ins)
 {
-       int len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
-       MonoMethodSignature *sig;
-       MonoCallInst *call;
-       CallInfo *cinfo;
-       int i;
-
-       if (ins->opcode != OP_JMP)
-               return len;
-
-       call = (MonoCallInst*)ins;
-       sig = mono_method_signature (cfg->method);
-       cinfo = calculate_sizes (sig, sig->pinvoke);
-
-       if (MONO_TYPE_ISSTRUCT (sig->ret))
-               len += 4;
-       for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
-               ArgInfo *ainfo = cinfo->args + i;
-
-               switch (ainfo->regtype) {
-               case RegTypeGeneral:
-               case RegTypeFP:
-                       len += 4;
-                       break;
-
-               case RegTypeBase:
-                       len += 8;
-                       break;
-
-               case RegTypeStructByVal:
-                       len += 4 * ainfo->size;
-                       break;
-
-               case RegTypeStructByAddr:
-                       len += 4;
-                       break;
-
-               default:
-                       g_assert_not_reached ();
-               }
-       }
-
-       g_free (cinfo);
-
-       return len;
+       return ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
 }
 
 static guint8*
@@ -3560,7 +3374,22 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        }
                        break;
                case OP_BREAK:
-                       ppc_break (code);
+                       /*
+                        * gdb does not like encountering a trap in the debugged code. So 
+                        * instead of emitting a trap, we emit a call a C function and place a 
+                        * breakpoint there.
+                        */
+                       //ppc_break (code);
+                       ppc_mr (code, ppc_r3, ins->sreg1);
+                       mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
+                                            (gpointer)"mono_break");
+                       if ((FORCE_INDIR_CALL || cfg->method->dynamic) && !cfg->compile_aot) {
+                               ppc_load_func (code, ppc_r0, 0);
+                               ppc_mtlr (code, ppc_r0);
+                               ppc_blrl (code);
+                       } else {
+                               ppc_bl (code, 0);
+                       }
                        break;
                case OP_ADDCC:
                case OP_IADDCC:
@@ -3715,7 +3544,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                                ppc_sldi (code, ppc_r0, ppc_r0, 32);
 #endif
                        ppc_compare (code, 0, ins->sreg1, ppc_r0);
-                       EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "ArithmeticException");
+                       EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
                        ppc_patch (divisor_is_m1, code);
                         /* XER format: SO, OV, CA, reserved [21 bits], count [8 bits]
                         */
@@ -3900,9 +3729,10 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_FCONV_TO_R4:
                        ppc_frsp (code, ins->dreg, ins->sreg1);
                        break;
-               case OP_JMP: {
+               case OP_TAILCALL: {
                        int i, pos;
-                       
+                       MonoCallInst *call = (MonoCallInst*)ins;
+
                        /*
                         * Keep in sync with mono_arch_emit_epilog
                         */
@@ -3922,8 +3752,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                                ppc_mtlr (code, ppc_r0);
                        }
 
-                       code = emit_load_volatile_arguments (cfg, code);
-
                        if (ppc_is_imm16 (cfg->stack_usage)) {
                                ppc_addi (code, ppc_r11, cfg->frame_reg, cfg->stack_usage);
                        } else {
@@ -3933,12 +3761,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                                ppc_addi (code, ppc_r11, ppc_r11, cfg->stack_usage);
                        }
                        if (!cfg->method->save_lmf) {
-                               /*for (i = 31; i >= 14; --i) {
-                                       if (cfg->used_float_regs & (1 << i)) {
-                                               pos += sizeof (double);
-                                               ppc_lfd (code, i, -pos, cfg->frame_reg);
-                                       }
-                               }*/
                                pos = 0;
                                for (i = 31; i >= 13; --i) {
                                        if (cfg->used_int_regs & (1 << i)) {
@@ -3949,6 +3771,22 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        } else {
                                /* FIXME restore from MonoLMF: though this can't happen yet */
                        }
+
+                       /* Copy arguments on the stack to our argument area */
+                       if (call->stack_usage) {
+                               code = emit_memcpy (code, call->stack_usage, ppc_r11, PPC_STACK_PARAM_OFFSET, ppc_sp, PPC_STACK_PARAM_OFFSET);
+                               /* r11 was clobbered */
+                               g_assert (cfg->frame_reg == ppc_sp);
+                               if (ppc_is_imm16 (cfg->stack_usage)) {
+                                       ppc_addi (code, ppc_r11, cfg->frame_reg, cfg->stack_usage);
+                               } else {
+                                       /* cfg->stack_usage is an int, so we can use
+                                        * an addis/addi sequence here even in 64-bit.  */
+                                       ppc_addis (code, ppc_r11, cfg->frame_reg, ppc_ha(cfg->stack_usage));
+                                       ppc_addi (code, ppc_r11, ppc_r11, cfg->stack_usage);
+                               }
+                       }
+
                        ppc_mr (code, ppc_sp, ppc_r11);
                        mono_add_patch_info (cfg, (guint8*) code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
                        if (cfg->compile_aot) {
@@ -4474,7 +4312,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        EMIT_COND_SYSTEM_EXCEPTION (CEE_BEQ - CEE_BEQ, "ArithmeticException");
                        break;
                case OP_JUMP_TABLE:
-                       mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
+                       mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_c1, ins->inst_p0);
 #ifdef __mono_ppc64__
                        ppc_load_sequence (code, ins->dreg, (guint64)0x0f0f0f0f0f0f0f0fLL);
 #else
@@ -4685,7 +4523,7 @@ mono_arch_register_lowlevel_calls (void)
 
 #ifndef DISABLE_JIT
 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, MonoCodeManager *dyn_code_mp, gboolean run_cctors)
 {
        MonoJumpInfo *patch_info;
        gboolean compile_aot = !run_cctors;
@@ -4841,7 +4679,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                tracing = 1;
 
        sig = mono_method_signature (method);
-       cfg->code_size = MONO_PPC_32_64_CASE (260, 384) + sig->param_count * 20;
+       cfg->code_size = 512 + sig->param_count * 32;
        code = cfg->native_code = g_malloc (cfg->code_size);
 
        cfa_offset = 0;
@@ -4925,7 +4763,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        /* load arguments allocated to register from the stack */
        pos = 0;
 
-       cinfo = calculate_sizes (sig, sig->pinvoke);
+       cinfo = get_call_info (cfg->generic_sharing_context, sig);
 
        if (MONO_TYPE_ISSTRUCT (sig->ret)) {
                ArgInfo *ainfo = &cinfo->ret;
@@ -4971,7 +4809,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                                        } else {
                                                if (ppc_is_imm32 (inst->inst_offset)) {
                                                        ppc_addis (code, ppc_r11, inst->inst_basereg, ppc_ha(inst->inst_offset));
-                                                       ppc_stb (code, ainfo->reg, ppc_r11, inst->inst_offset);
+                                                       ppc_stb (code, ainfo->reg, inst->inst_offset, ppc_r11);
                                                } else {
                                                        ppc_load (code, ppc_r11, inst->inst_offset);
                                                        ppc_stbx (code, ainfo->reg, inst->inst_basereg, ppc_r11);
@@ -4984,7 +4822,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                                        } else {
                                                if (ppc_is_imm32 (inst->inst_offset)) {
                                                        ppc_addis (code, ppc_r11, inst->inst_basereg, ppc_ha(inst->inst_offset));
-                                                       ppc_sth (code, ainfo->reg, ppc_r11, inst->inst_offset);
+                                                       ppc_sth (code, ainfo->reg, inst->inst_offset, ppc_r11);
                                                } else {
                                                        ppc_load (code, ppc_r11, inst->inst_offset);
                                                        ppc_sthx (code, ainfo->reg, inst->inst_basereg, ppc_r11);
@@ -4998,7 +4836,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                                        } else {
                                                if (ppc_is_imm32 (inst->inst_offset)) {
                                                        ppc_addis (code, ppc_r11, inst->inst_basereg, ppc_ha(inst->inst_offset));
-                                                       ppc_stw (code, ainfo->reg, ppc_r11, inst->inst_offset);
+                                                       ppc_stw (code, ainfo->reg, inst->inst_offset, ppc_r11);
                                                } else {
                                                        ppc_load (code, ppc_r11, inst->inst_offset);
                                                        ppc_stwx (code, ainfo->reg, inst->inst_basereg, ppc_r11);
@@ -5032,7 +4870,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                                        } else {
                                                if (ppc_is_imm32 (inst->inst_offset)) {
                                                        ppc_addis (code, ppc_r11, inst->inst_basereg, ppc_ha(inst->inst_offset));
-                                                       ppc_stptr (code, ainfo->reg, ppc_r11, inst->inst_offset);
+                                                       ppc_stptr (code, ainfo->reg, inst->inst_offset, ppc_r11);
                                                } else {
                                                        ppc_load (code, ppc_r11, inst->inst_offset);
                                                        ppc_stptr_indexed (code, ainfo->reg, inst->inst_basereg, ppc_r11);
@@ -5052,7 +4890,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                                        } else {
                                                if (ppc_is_imm32 (inst->inst_offset)) {
                                                        ppc_addis (code, ppc_r11, inst->inst_basereg, ppc_ha(inst->inst_offset));
-                                                       ppc_stb (code, ppc_r0, ppc_r11, inst->inst_offset);
+                                                       ppc_stb (code, ppc_r0, inst->inst_offset, ppc_r11);
                                                } else {
                                                        ppc_load (code, ppc_r11, inst->inst_offset);
                                                        ppc_stbx (code, ppc_r0, inst->inst_basereg, ppc_r11);
@@ -5065,7 +4903,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                                        } else {
                                                if (ppc_is_imm32 (inst->inst_offset)) {
                                                        ppc_addis (code, ppc_r11, inst->inst_basereg, ppc_ha(inst->inst_offset));
-                                                       ppc_sth (code, ppc_r0, ppc_r11, inst->inst_offset);
+                                                       ppc_sth (code, ppc_r0, inst->inst_offset, ppc_r11);
                                                } else {
                                                        ppc_load (code, ppc_r11, inst->inst_offset);
                                                        ppc_sthx (code, ppc_r0, inst->inst_basereg, ppc_r11);
@@ -5079,7 +4917,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                                        } else {
                                                if (ppc_is_imm32 (inst->inst_offset)) {
                                                        ppc_addis (code, ppc_r11, inst->inst_basereg, ppc_ha(inst->inst_offset));
-                                                       ppc_stw (code, ppc_r0, ppc_r11, inst->inst_offset);
+                                                       ppc_stw (code, ppc_r0, inst->inst_offset, ppc_r11);
                                                } else {
                                                        ppc_load (code, ppc_r11, inst->inst_offset);
                                                        ppc_stwx (code, ppc_r0, inst->inst_basereg, ppc_r11);
@@ -5117,7 +4955,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                                        } else {
                                                if (ppc_is_imm32 (inst->inst_offset)) {
                                                        ppc_addis (code, ppc_r11, inst->inst_basereg, ppc_ha(inst->inst_offset));
-                                                       ppc_stptr (code, ppc_r0, ppc_r11, inst->inst_offset);
+                                                       ppc_stptr (code, ppc_r0, inst->inst_offset, ppc_r11);
                                                } else {
                                                        ppc_load (code, ppc_r11, inst->inst_offset);
                                                        ppc_stptr_indexed (code, ppc_r0, inst->inst_basereg, ppc_r11);
@@ -5219,22 +5057,6 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                pos++;
        }
 
-       if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
-               if (cfg->compile_aot)
-                       /* AOT code is only used in the root domain */
-                       ppc_load_ptr (code, ppc_r3, 0);
-               else
-                       ppc_load_ptr (code, ppc_r3, cfg->domain);
-               mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_jit_thread_attach");
-               if ((FORCE_INDIR_CALL || cfg->method->dynamic) && !cfg->compile_aot) {
-                       ppc_load_func (code, ppc_r0, 0);
-                       ppc_mtlr (code, ppc_r0);
-                       ppc_blrl (code);
-               } else {
-                       ppc_bl (code, 0);
-               }
-       }
-
        if (method->save_lmf) {
                if (lmf_pthread_key != -1) {
                        emit_tls_access (code, ppc_r3, lmf_pthread_key);
@@ -5323,7 +5145,7 @@ mono_arch_emit_epilog (MonoCompile *cfg)
        while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
                cfg->code_size *= 2;
                cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
-               mono_jit_stats.code_reallocs++;
+               cfg->stat_code_reallocs++;
        }
 
        /*
@@ -5439,6 +5261,8 @@ exception_id_by_name (const char *name)
                return MONO_EXC_NULL_REF;
        if (strcmp (name, "ArrayTypeMismatchException") == 0)
                return MONO_EXC_ARRAY_TYPE_MISMATCH;
+       if (strcmp (name, "ArgumentException") == 0)
+               return MONO_EXC_ARGUMENT;
        g_error ("Unknown intrinsic exception %s\n", name);
        return 0;
 }
@@ -5450,10 +5274,15 @@ mono_arch_emit_exceptions (MonoCompile *cfg)
        MonoJumpInfo *patch_info;
        int i;
        guint8 *code;
-       const guint8* exc_throw_pos [MONO_EXC_INTRINS_NUM] = {NULL};
-       guint8 exc_throw_found [MONO_EXC_INTRINS_NUM] = {0};
+       guint8* exc_throw_pos [MONO_EXC_INTRINS_NUM];
+       guint8 exc_throw_found [MONO_EXC_INTRINS_NUM];
        int max_epilog_size = 50;
 
+       for (i = 0; i < MONO_EXC_INTRINS_NUM; i++) {
+               exc_throw_pos [i] = NULL;
+               exc_throw_found [i] = 0;
+       }
+
        /* count the number of exception infos */
      
        /* 
@@ -5482,7 +5311,7 @@ mono_arch_emit_exceptions (MonoCompile *cfg)
        while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
                cfg->code_size *= 2;
                cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
-               mono_jit_stats.code_reallocs++;
+               cfg->stat_code_reallocs++;
        }
 
        code = cfg->native_code + cfg->code_len;
@@ -5705,21 +5534,19 @@ setup_tls_access (void)
           mono_domain_get_tls_offset returning -1) then use keyed access. */
        if (monodomain_key == -1) {
                ptk = mono_domain_get_tls_key ();
-               if (ptk < 1024) {
-                       ptk = mono_pthread_key_for_tls (ptk);
-                       if (ptk < 1024) {
-                               monodomain_key = ptk;
-                       }
-               }
+               if (ptk < 1024)
+                   monodomain_key = ptk;
        }
 
        if ((lmf_pthread_key == -1) && (tls_mode == TLS_MODE_NPTL)) {
                lmf_pthread_key = mono_get_lmf_addr_tls_offset();
        }
+
+#if 0
        /* if not TLS_MODE_NPTL or local dynamic (as indicated by
           mono_get_lmf_addr_tls_offset returning -1) then use keyed access. */
        if (lmf_pthread_key == -1) {
-               ptk = mono_pthread_key_for_tls (mono_jit_tls_id);
+               ptk = mono_jit_tls_id;
                if (ptk < 1024) {
                        /*g_print ("MonoLMF at: %d\n", ptk);*/
                        /*if (!try_offset_access (mono_get_lmf_addr (), ptk)) {
@@ -5730,10 +5557,12 @@ setup_tls_access (void)
                }
        }
 #endif
+
+#endif
 }
 
 void
-mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
+mono_arch_finish_init (void)
 {
        setup_tls_access ();
 }
@@ -5910,6 +5739,16 @@ mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
        return (MonoVTable*)(gsize) r [MONO_ARCH_RGCTX_REG];
 }
 
+GSList*
+mono_arch_get_cie_program (void)
+{
+       GSList *l = NULL;
+
+       mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, ppc_r1, 0);
+
+       return l;
+}
+
 MonoInst*
 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
 {
@@ -5936,15 +5775,15 @@ MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
        return ins;
 }
 
-gpointer
+mgreg_t
 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
 {
        if (reg == ppc_r1)
-               return MONO_CONTEXT_GET_SP (ctx);
+               return (mgreg_t)MONO_CONTEXT_GET_SP (ctx);
 
        g_assert (reg >= ppc_r13);
 
-       return (gpointer)(gsize)ctx->regs [reg - ppc_r13];
+       return ctx->regs [reg - ppc_r13];
 }
 
 guint32
@@ -6062,29 +5901,13 @@ mono_arch_is_breakpoint_event (void *info, void *sigctx)
                return FALSE;
 }
 
-/*
- * mono_arch_get_ip_for_breakpoint:
- *
- *   See mini-amd64.c for docs.
- */
-guint8*
-mono_arch_get_ip_for_breakpoint (MonoJitInfo *ji, MonoContext *ctx)
-{
-       guint8 *ip = MONO_CONTEXT_GET_IP (ctx);
-
-       /* ip points at the ldptr instruction */
-       ip -= PPC_LOAD_SEQUENCE_LENGTH;
-
-       return ip;
-}
-
 /*
  * mono_arch_skip_breakpoint:
  *
  *   See mini-amd64.c for docs.
  */
 void
-mono_arch_skip_breakpoint (MonoContext *ctx)
+mono_arch_skip_breakpoint (MonoContext *ctx, MonoJitInfo *ji)
 {
        /* skip the ldptr */
        MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 4);
@@ -6132,20 +5955,6 @@ mono_arch_is_single_step_event (void *info, void *sigctx)
                return FALSE;
 }
 
-/*
- * mono_arch_get_ip_for_single_step:
- *
- *   See mini-amd64.c for docs.
- */
-guint8*
-mono_arch_get_ip_for_single_step (MonoJitInfo *ji, MonoContext *ctx)
-{
-       guint8 *ip = MONO_CONTEXT_GET_IP (ctx);
-
-       /* ip points after the ldptr instruction */
-       return ip;
-}
-
 /*
  * mono_arch_skip_single_step:
  *