2009-08-11 Gonzalo Paniagua Javier <gonzalo@novell.com>
[mono.git] / mono / mini / exceptions-arm.c
index 8558fdd6b7e6be0fbd4e7b40ba8eb83ef33a8426..bebd9419835ebbba785c2303ab60623e6f2da364 100644 (file)
@@ -155,6 +155,8 @@ mono_arch_get_call_filter_full (guint32 *code_size, MonoJumpInfo **ji, gboolean
        guint8 *code;
        guint8* start;
 
+       *ji = NULL;
+
        /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
        start = code = mono_global_codeman_reserve (320);
 
@@ -181,8 +183,8 @@ mono_arch_get_call_filter_full (guint32 *code_size, MonoJumpInfo **ji, gboolean
        return start;
 }
 
-static void
-throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs)
+void
+mono_arm_throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs)
 {
        static void (*restore_context) (MonoContext *);
        MonoContext ctx;
@@ -214,6 +216,12 @@ throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gulong *
        g_assert_not_reached ();
 }
 
+void
+mono_arm_throw_exception_by_token (guint32 type_token, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs)
+{
+       mono_arm_throw_exception ((MonoObject*)mono_exception_from_token (mono_defaults.corlib, type_token), eip, esp, int_regs, fp_regs);
+}
+
 /**
  * arch_get_throw_exception_generic:
  *
@@ -237,17 +245,6 @@ mono_arch_get_throw_exception_generic (int size, int by_token, gboolean rethrow,
        ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
        ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
 
-       if (by_token) {
-               /* r0 has the type token of the exception: get the object */
-               ARM_PUSH1 (code, ARMREG_R1);
-               ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
-               code = mono_arm_emit_load_imm (code, ARMREG_R0, GPOINTER_TO_UINT (mono_defaults.corlib));
-               code = mono_arm_emit_load_imm (code, ARMREG_IP, GPOINTER_TO_UINT (mono_exception_from_token));
-               ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
-               ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
-               ARM_POP1 (code, ARMREG_R1);
-       }
-
        /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
        /* caller sp */
        ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, 10 * 4); /* 10 saved regs */
@@ -265,7 +262,16 @@ mono_arch_get_throw_exception_generic (int size, int by_token, gboolean rethrow,
        /* we encode rethrow in the ip, so we avoid args on the stack */
        ARM_ORR_REG_IMM8 (code, ARMREG_R1, ARMREG_R1, rethrow);
 
-       code = mono_arm_emit_load_imm (code, ARMREG_IP, GPOINTER_TO_UINT (throw_exception));
+       if (aot) {
+               *ji = mono_patch_info_list_prepend (*ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, by_token ? "mono_arm_throw_exception_by_token" : "mono_arm_throw_exception");
+               ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
+               ARM_B (code, 0);
+               *(gpointer*)(gpointer)code = NULL;
+               code += 4;
+               ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
+       } else {
+               code = mono_arm_emit_load_imm (code, ARMREG_IP, GPOINTER_TO_UINT (by_token ? (gpointer)mono_arm_throw_exception_by_token : (gpointer)mono_arm_throw_exception));
+       }
        ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
        ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
        /* we should never reach this breakpoint */
@@ -356,8 +362,7 @@ mono_arch_get_throw_corlib_exception_full (guint32 *code_size, MonoJumpInfo **ji
  */
 MonoJitInfo *
 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji,
-                        MonoContext *ctx, MonoContext *new_ctx, char **trace, MonoLMF **lmf,
-                        int *native_offset, gboolean *managed)
+                        MonoContext *ctx, MonoContext *new_ctx, MonoLMF **lmf, gboolean *managed)
 {
        MonoJitInfo *ji;
        gpointer ip = MONO_CONTEXT_GET_IP (ctx);
@@ -372,7 +377,11 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInf
                *managed = FALSE;
 
        if (ji != NULL) {
-               int offset;
+               int i;
+               gssize regs [MONO_MAX_IREGS + 1];
+               guint8 *cfa;
+               guint32 unwind_info_len;
+               guint8 *unwind_info;
 
                *new_ctx = *ctx;
 
@@ -380,44 +389,33 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInf
                        if (!ji->method->wrapper_type)
                                *managed = TRUE;
 
-               /*
-                * Some managed methods like pinvoke wrappers might have save_lmf set.
-                * In this case, register save/restore code is not generated by the 
-                * JIT, so we have to restore callee saved registers from the lmf.
-                */
-               if (ji->method->save_lmf) {
-                       /* 
-                        * We only need to do this if the exception was raised in managed
-                        * code, since otherwise the lmf was already popped of the stack.
-                        */
-                       if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
-                               memcpy (&new_ctx->regs [0], &(*lmf)->iregs [4], sizeof (gulong) * MONO_SAVED_GREGS);
-                       }
-                       new_ctx->esp = (*lmf)->iregs [12];
-                       new_ctx->eip = (*lmf)->iregs [13];
-                       new_ctx->ebp = new_ctx->esp;
-               } else {
-                       int i;
-                       char* sp;
-                       offset = ji->used_regs >> 16;
-                       offset <<= 2;
-                       /* the saved regs are at sp + offset */
-                       /* restore caller saved registers */
-                       sp = (char*)ctx->ebp;
-                       sp += offset;
-                       for (i = 4; i < 16; ++i) {
-                               if (ji->used_regs & (1 << i)) {
-                                       new_ctx->regs [i - 4] = *(gulong*)sp;
-                                       sp += sizeof (gulong);
-                               }
-                       }
-                       /* IP and LR */
-                       new_ctx->esp = *(gulong*)sp;
-                       sp += sizeof (gulong);
-                       new_ctx->eip = *(gulong*)sp;
-                       sp += sizeof (gulong);
-                       new_ctx->ebp = new_ctx->esp;
-               }
+               if (ji->from_aot)
+                       unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
+               else
+                       unwind_info = mono_get_cached_unwind_info (ji->used_regs, &unwind_info_len);
+
+               /* Beautiful numbers */
+               for (i = 4; i < 12; ++i)
+                       regs [i] = new_ctx->regs [i - 4];
+               regs [ARMREG_IP] = new_ctx->regs [8];
+               regs [ARMREG_LR] = new_ctx->regs [9];
+               regs [ARMREG_SP] = new_ctx->esp;
+
+               mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start, 
+                                                  (guint8*)ji->code_start + ji->code_size,
+                                                  ip, regs, MONO_MAX_IREGS, &cfa);
+
+               for (i = 4; i < 12; ++i)
+                       new_ctx->regs [i - 4] = regs [i];
+               new_ctx->regs [8] = regs [ARMREG_IP];
+               new_ctx->regs [9] = regs [ARMREG_LR];
+               new_ctx->eip = regs [ARMREG_LR];
+               new_ctx->esp = (gsize)cfa;
+               new_ctx->ebp = new_ctx->esp;
+
+               /* Needed by get_exception_catch_class () */
+               // FIXME: Is this neded ?
+               new_ctx->regs [ARMREG_R11] = new_ctx->esp;
 
                if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
                        /* remove any unused lmf */
@@ -436,12 +434,13 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInf
                } else {
                        if (!(*lmf)->method)
                                return (gpointer)-1;
-                       memset (res, 0, sizeof (MonoJitInfo));
+                       memset (res, 0, MONO_SIZEOF_JIT_INFO);
                        res->method = (*lmf)->method;
                }
 
                memcpy (&new_ctx->regs [0], &(*lmf)->iregs [4], sizeof (gulong) * MONO_SAVED_GREGS);
-               new_ctx->esp = (*lmf)->iregs [12];
+               /* This is the sp for the current frame */
+               new_ctx->esp = (*lmf)->iregs [ARMREG_FP];
                new_ctx->eip = (*lmf)->iregs [13];
                new_ctx->ebp = new_ctx->esp;