2006-01-03 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / exceptions-ia64.c
index 7bcd000fc99c8b90d35c0ebe8dec89aadb5b7bd6..0399356f03371ebd5978e6be1a231b8817625d44 100644 (file)
 #define GP_SCRATCH_REG 31
 #define GP_SCRATCH_REG2 30
 
+G_GNUC_UNUSED static void
+print_ctx (MonoContext *ctx)
+{
+       char name[256];
+       unw_word_t off, ip, sp;
+       unw_proc_info_t pi;
+       int res;
+
+       unw_get_proc_name (&ctx->cursor, name, 256, &off);
+       unw_get_proc_info(&ctx->cursor, &pi);
+       res = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
+       g_assert (res == 0);
+       res = unw_get_reg (&ctx->cursor, UNW_IA64_SP, &sp);
+       g_assert (res == 0);
+
+       printf ("%s:%lx [%lx-%lx] SP: %lx\n", name, ip - pi.start_ip, pi.start_ip, pi.end_ip, sp);
+}
+
 static gpointer
 ia64_create_ftnptr (gpointer ptr)
 {
@@ -56,6 +74,10 @@ static void
 restore_context (MonoContext *ctx)
 {
        int res;
+       unw_word_t ip;
+
+       res = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
+       g_assert (res == 0);
 
        /* Set this to 0 to tell OP_START_HANDLER that it doesn't have to set the frame pointer */
        res = unw_set_reg (&ctx->cursor, UNW_IA64_GR + 15, 0);
@@ -94,17 +116,18 @@ get_real_call_filter (void)
        /* int call_filter (guint64 fp, guint64 ip) */
 
        /*
-        * We have to create a register+stack frame similar to the frame which contains
-        * the filter. 
+        * We have to create a register+stack frame similar to the frame which 
+        * contains the filter. 
         * - setting fp
         * - setting up a register stack frame
-        * These cannot be set up in this function, because the fp register is a stacked
-        * register which is different in each method. Also, the register stack frame is
-        * different in each method. So we pass the FP value in a a non-stacked
-        * register and the code generated by the OP_START_HANDLER opcode will copy it
-        * to the appropriate register after setting up the register stack frame.
+        * These cannot be set up in this function, because the fp register is a 
+        * stacked register which is different in each method. Also, the register 
+        * stack frame is different in each method. So we pass the FP value in a a 
+        * non-stacked register and the code generated by the OP_START_HANDLER 
+        * opcode will copy it to the appropriate register after setting up the 
+        * register stack frame.
         * The stacked registers are not need to be set since variables used in
-        * handler registers are never allocated to registers.
+        * handler regions are never allocated to registers.
         */
 
        in0 = 32;
@@ -130,25 +153,13 @@ get_real_call_filter (void)
        /* Target ip */
        ia64_mov_to_br (code, IA64_B6, in0 + 1);
 
-       /* Return address */
-       ia64_mov_from_ip (code, GP_SCRATCH_REG);
-       ia64_adds_imm (code, GP_SCRATCH_REG, 3 * 16, GP_SCRATCH_REG);
-
        /* Call the filter */
        ia64_br_call_reg (code, IA64_B0, IA64_B6);
 
        /* R8 contains the result of the filter */
-       /* R9 contains the saved apr_pfs value */
 
        /* FIXME: Add unwind info for this */
 
-       /* The filter returns using br_cond_reg, so have to do another return */
-       ia64_mov_to_ar_i (code, IA64_PFS, IA64_R9);
-       ia64_mov_from_ip (code, GP_SCRATCH_REG);        
-       ia64_adds_imm (code, GP_SCRATCH_REG, 4 * 16, GP_SCRATCH_REG);
-       ia64_mov_to_br (code, IA64_B0, GP_SCRATCH_REG);
-       ia64_br_ret_reg (code, IA64_B0);
-
        ia64_begin_bundle (code);
 
        r_body = mono_ia64_create_unwind_region (&code);
@@ -221,7 +232,7 @@ throw_exception (MonoObject *exc, guint64 rethrow)
        unw_context_t unw_ctx;
        MonoContext ctx;
        MonoJitInfo *ji;
-       unw_word_t ip;
+       unw_word_t ip, sp;
        int res;
 
        if (mono_object_isinst (exc, mono_defaults.exception_class)) {
@@ -244,8 +255,13 @@ throw_exception (MonoObject *exc, guint64 rethrow)
                res = unw_get_reg (&ctx.cursor, UNW_IA64_IP, &ip);
                g_assert (res == 0);
 
+               res = unw_get_reg (&ctx.cursor, UNW_IA64_SP, &sp);
+               g_assert (res == 0);
+
                ji = mono_jit_info_table_find (mono_domain_get (), (gpointer)ip);
 
+               //printf ("UN: %s %lx %lx\n", ji ? ji->method->name : "", ip, sp);
+
                if (ji)
                        break;
 
@@ -396,7 +412,7 @@ mono_arch_get_throw_exception_by_name (void)
  *
  * Returns a function pointer which can be used to raise 
  * corlib exceptions. The returned function has the following 
- * signature: void (*func) (guint32 ex_token, guint32 offset); 
+ * signature: void (*func) (guint32 ex_token_index, guint32 offset); 
  * Here, offset is the offset which needs to be substracted from the caller IP 
  * to get the IP of the throw. Passing the offset has the advantage that it 
  * needs no relocations in the caller.
@@ -439,6 +455,8 @@ mono_arch_get_throw_corlib_exception (void)
        /* Call exception_from_token */
        ia64_movl (code, out0 + 0, mono_defaults.exception_class->image);
        ia64_mov (code, out0 + 1, in0 + 0);
+       ia64_movl (code, GP_SCRATCH_REG, MONO_TOKEN_TYPE_DEF);
+       ia64_add (code, out0 + 1, in0 + 0, GP_SCRATCH_REG);
        ptr = mono_exception_from_token;
        ia64_movl (code, GP_SCRATCH_REG, ptr);
        ia64_ld8_inc_imm (code, GP_SCRATCH_REG2, GP_SCRATCH_REG, 8);
@@ -548,9 +566,13 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInf
        }
 
        if (ji) {
+               //print_ctx (new_ctx);
+
                err = unw_step (&new_ctx->cursor);
                g_assert (err >= 0);
 
+               //print_ctx (new_ctx);
+
                return ji;
        }
        else
@@ -605,16 +627,7 @@ mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
 gpointer
 mono_arch_ip_from_context (void *sigctx)
 {
-       /* On IA64, these two are equal */
-       unw_context_t *ctx = (unw_context_t*)sigctx;
-       unw_cursor_t cursor;
-       int res;
-       unw_word_t w;
-
-       res = unw_init_local (&cursor, ctx);
-       g_assert (res == 0);
-       res = unw_get_reg (&cursor, UNW_IA64_IP, &w);
-       g_assert (res == 0);
+       ucontext_t *ctx = (ucontext_t*)sigctx;
 
-       return (gpointer)w;
+       return (gpointer)ctx->uc_mcontext.sc_ip;
 }