Turn eval test into normal test case
[mono.git] / mono / mini / exceptions-x86.c
index 1550b99c16cb36b3147d1d27960b7a8c1c15943e..e64f84139d887e27f8bc1a53a07a42bc47ff7252 100644 (file)
@@ -261,24 +261,22 @@ void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
  * Returns a pointer to a method which restores a previously saved sigcontext.
  */
 gpointer
-mono_arch_get_restore_context (void)
+mono_arch_get_restore_context_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
 {
-       static guint8 *start = NULL;
+       guint8 *start = NULL;
        guint8 *code;
 
-       if (start)
-               return start;
-
        /* restore_contect (MonoContext *ctx) */
-       /* we do not restore X86_EDX */
+
+       *ji = NULL;
 
        start = code = mono_global_codeman_reserve (128);
        
        /* load ctx */
        x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
 
-       /* get return address, stored in EDX */
-       x86_mov_reg_membase (code, X86_EDX, X86_EAX,  G_STRUCT_OFFSET (MonoContext, eip), 4);
+       /* get return address, stored in ECX */
+       x86_mov_reg_membase (code, X86_ECX, X86_EAX,  G_STRUCT_OFFSET (MonoContext, eip), 4);
        /* restore EBX */
        x86_mov_reg_membase (code, X86_EBX, X86_EAX,  G_STRUCT_OFFSET (MonoContext, ebx), 4);
        /* restore EDI */
@@ -287,13 +285,21 @@ mono_arch_get_restore_context (void)
        x86_mov_reg_membase (code, X86_ESI, X86_EAX,  G_STRUCT_OFFSET (MonoContext, esi), 4);
        /* restore ESP */
        x86_mov_reg_membase (code, X86_ESP, X86_EAX,  G_STRUCT_OFFSET (MonoContext, esp), 4);
+       /* save the return addr to the restored stack */
+       x86_push_reg (code, X86_ECX);
        /* restore EBP */
        x86_mov_reg_membase (code, X86_EBP, X86_EAX,  G_STRUCT_OFFSET (MonoContext, ebp), 4);
+       /* restore ECX */
+       x86_mov_reg_membase (code, X86_ECX, X86_EAX,  G_STRUCT_OFFSET (MonoContext, ecx), 4);
+       /* restore EDX */
+       x86_mov_reg_membase (code, X86_EDX, X86_EAX,  G_STRUCT_OFFSET (MonoContext, edx), 4);
        /* restore EAX */
        x86_mov_reg_membase (code, X86_EAX, X86_EAX,  G_STRUCT_OFFSET (MonoContext, eax), 4);
 
        /* jump to the saved IP */
-       x86_jump_reg (code, X86_EDX);
+       x86_ret (code);
+
+       *code_size = code - start;
 
        return start;
 }
@@ -306,16 +312,13 @@ mono_arch_get_restore_context (void)
  * @exc object in this case).
  */
 gpointer
-mono_arch_get_call_filter (void)
+mono_arch_get_call_filter_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
 {
-       static guint8* start;
-       static int inited = 0;
+       guint8* start;
        guint8 *code;
 
-       if (inited)
-               return start;
+       *ji = NULL;
 
-       inited = 1;
        /* call_filter (MonoContext *ctx, unsigned long eip) */
        start = code = mono_global_codeman_reserve (64);
 
@@ -362,31 +365,36 @@ mono_arch_get_call_filter (void)
        x86_leave (code);
        x86_ret (code);
 
+       *code_size = code - start;
+
        g_assert ((code - start) < 64);
        return start;
 }
 
-static void
-throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsigned long ebx,
-                unsigned long esi, unsigned long edi, unsigned long ebp, MonoObject *exc,
-                unsigned long eip,  unsigned long esp, gboolean rethrow)
+/*
+ * mono_x86_throw_exception:
+ *
+ *   C function called from the throw trampolines.
+ */
+void
+mono_x86_throw_exception (mgreg_t *regs, MonoObject *exc, 
+                                                 mgreg_t eip, gboolean rethrow)
 {
        static void (*restore_context) (MonoContext *);
        MonoContext ctx;
 
        if (!restore_context)
-               restore_context = mono_arch_get_restore_context ();
+               restore_context = mono_get_restore_context ();
 
-       /* Pop alignment added in get_throw_exception (), the return address, plus the argument and the alignment added at the call site */
-       ctx.esp = esp + 8 + MONO_ARCH_FRAME_ALIGNMENT;
+       ctx.esp = regs [X86_ESP];
        ctx.eip = eip;
-       ctx.ebp = ebp;
-       ctx.edi = edi;
-       ctx.esi = esi;
-       ctx.ebx = ebx;
-       ctx.edx = edx;
-       ctx.ecx = ecx;
-       ctx.eax = eax;
+       ctx.ebp = regs [X86_EBP];
+       ctx.edi = regs [X86_EDI];
+       ctx.esi = regs [X86_ESI];
+       ctx.ebx = regs [X86_EBX];
+       ctx.edx = regs [X86_EDX];
+       ctx.ecx = regs [X86_ECX];
+       ctx.eax = regs [X86_EAX];
 
 #ifdef __APPLE__
        /* The OSX ABI specifies 16 byte alignment at call sites */
@@ -426,107 +434,161 @@ throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsign
        g_assert_not_reached ();
 }
 
-static guint8*
-get_throw_exception (gboolean rethrow)
+void
+mono_x86_throw_corlib_exception (mgreg_t *regs, guint32 ex_token_index, 
+                                                                mgreg_t eip, gint32 pc_offset)
 {
-       guint8 *start, *code;
+       guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
+       MonoException *ex;
 
-       start = code = mono_global_codeman_reserve (64);
+       ex = mono_exception_from_token (mono_defaults.exception_class->image, ex_token);
 
-       /* 
-        * Align the stack on apple, since we push 10 args, and the call pushed 4 bytes.
-        */
-       x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
-       x86_push_reg (code, X86_ESP);
-       x86_push_membase (code, X86_ESP, 8); /* IP */
-       x86_push_membase (code, X86_ESP, 16); /* exception */
-       x86_push_reg (code, X86_EBP);
-       x86_push_reg (code, X86_EDI);
-       x86_push_reg (code, X86_ESI);
-       x86_push_reg (code, X86_EBX);
-       x86_push_reg (code, X86_EDX);
-       x86_push_reg (code, X86_ECX);
-       x86_push_reg (code, X86_EAX);
-       x86_call_code (code, throw_exception);
-       /* we should never reach this breakpoint */
-       x86_breakpoint (code);
-
-       g_assert ((code - start) < 64);
+       eip -= pc_offset;
 
-       return start;
+       mono_x86_throw_exception (regs, (MonoObject*)ex, eip, FALSE);
 }
 
-/**
- * mono_arch_get_throw_exception:
- *
- * Returns a function pointer which can be used to raise 
- * exceptions. The returned function has the following 
- * signature: void (*func) (MonoException *exc); 
- * For example to raise an arithmetic exception you can use:
- *
- * x86_push_imm (code, mono_get_exception_arithmetic ()); 
- * x86_call_code (code, arch_get_throw_exception ()); 
+/*
+ * get_throw_exception:
  *
+ *  Generate a call to mono_x86_throw_exception/
+ * mono_x86_throw_corlib_exception.
+ * If LLVM is true, generate code which assumes the caller is LLVM generated code, 
+ * which doesn't push the arguments.
  */
-gpointer 
-mono_arch_get_throw_exception (void)
+static guint8*
+get_throw_exception (const char *name, gboolean rethrow, gboolean llvm, gboolean corlib, guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
 {
-       static guint8 *start;
-       static int inited = 0;
+       guint8 *start, *code;
+       GSList *unwind_ops = NULL;
+       int i, stack_size, stack_offset, arg_offsets [5], regs_offset;
 
-       if (inited)
-               return start;
+       if (ji)
+               *ji = NULL;
 
-       start = get_throw_exception (FALSE);
+       start = code = mono_global_codeman_reserve (128);
 
-       inited = 1;
+       stack_size = 128;
 
-       return start;
-}
+       /* 
+        * On apple, the stack is misaligned by the pushing of the return address.
+        */
+       if (!llvm && corlib)
+               /* On OSX, we don't generate alignment code to save space */
+               stack_size += 4;
+       else
+               stack_size += MONO_ARCH_FRAME_ALIGNMENT - 4;
+
+       /*
+        * The stack looks like this:
+        * <pc offset> (only if corlib is TRUE)
+        * <exception object>/<type token>
+        * <return addr> <- esp (unaligned on apple)
+        */
 
-gpointer 
-mono_arch_get_rethrow_exception (void)
-{
-       static guint8 *start;
-       static int inited = 0;
+       mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
+       mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
+
+       /* Alloc frame */
+       x86_alu_reg_imm (code, X86_SUB, X86_ESP, stack_size);
+       mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 4);
+
+       arg_offsets [0] = 0;
+       arg_offsets [1] = 4;
+       arg_offsets [2] = 8;
+       arg_offsets [3] = 12;
+       regs_offset = 16;
+
+       /* Save registers */
+       for (i = 0; i < X86_NREG; ++i)
+               if (i != X86_ESP)
+                       x86_mov_membase_reg (code, X86_ESP, regs_offset + (i * 4), i, 4);
+       /* Calculate the offset between the current sp and the sp of the caller */
+       if (llvm) {
+               /* LLVM doesn't push the arguments */
+               stack_offset = stack_size + 4;
+       } else {
+               if (corlib) {
+                       /* Two arguments */
+                       stack_offset = stack_size + 4 + 8;
+#ifdef __APPLE__
+                       /* We don't generate stack alignment code on osx to save space */
+#endif
+               } else {
+                       /* One argument */
+                       stack_offset = stack_size + 4 + 4;
+#ifdef __APPLE__
+                       /* Pop the alignment added by OP_THROW too */
+                       stack_offset += MONO_ARCH_FRAME_ALIGNMENT - 4;
+#endif
+               }
+       }
+       /* Save ESP */
+       x86_lea_membase (code, X86_EAX, X86_ESP, stack_offset);
+       x86_mov_membase_reg (code, X86_ESP, regs_offset + (X86_ESP * 4), X86_EAX, 4);
+
+       /* Set arg1 == regs */
+       x86_lea_membase (code, X86_EAX, X86_ESP, regs_offset);
+       x86_mov_membase_reg (code, X86_ESP, arg_offsets [0], X86_EAX, 4);
+       /* Set arg2 == exc */
+       x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 4, 4);
+       x86_mov_membase_reg (code, X86_ESP, arg_offsets [1], X86_EAX, 4);
+       /* Set arg3 == eip */
+       x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size, 4);
+       x86_mov_membase_reg (code, X86_ESP, arg_offsets [2], X86_EAX, 4);
+       if (corlib) {
+               /* Set arg4 == offset */
+               x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 8, 4);
+               x86_mov_membase_reg (code, X86_ESP, arg_offsets [3], X86_EAX, 4);
+       } else {
+               /* Set arg4 == rethrow */
+               x86_mov_membase_imm (code, X86_ESP, arg_offsets [3], rethrow, 4);
+       }
+       /* Make the call */
+       if (aot) {
+               // This can be called from runtime code, which can't guarantee that
+               // ebx contains the got address.
+               // So emit the got address loading code too
+               code = mono_arch_emit_load_got_addr (start, code, NULL, ji);
+               code = mono_arch_emit_load_aotconst (start, code, ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, corlib ? "mono_x86_throw_corlib_exception" : "mono_x86_throw_exception");
+               x86_call_reg (code, X86_EAX);
+       } else {
+               x86_call_code (code, corlib ? (gpointer)mono_x86_throw_corlib_exception : (gpointer)mono_x86_throw_exception);
+       }
+       x86_breakpoint (code);
 
-       if (inited)
-               return start;
+       g_assert ((code - start) < 128);
 
-       start = get_throw_exception (TRUE);
+       if (code_size)
+               *code_size = code - start;
 
-       inited = 1;
+       mono_save_trampoline_xdebug_info (corlib ? "llvm_throw_corlib_exception_trampoline" : "llvm_throw_exception_trampoline", start, code - start, unwind_ops);
 
        return start;
 }
 
 /**
- * mono_arch_get_throw_exception_by_name:
+ * mono_arch_get_throw_exception:
  *
  * Returns a function pointer which can be used to raise 
- * corlib exceptions. The returned function has the following 
- * signature: void (*func) (gpointer ip, char *exc_name); 
+ * exceptions. The returned function has the following 
+ * signature: void (*func) (MonoException *exc); 
  * For example to raise an arithmetic exception you can use:
  *
- * x86_push_imm (code, "ArithmeticException"); 
- * x86_push_imm (code, <IP>)
- * x86_jump_code (code, arch_get_throw_exception_by_name ()); 
+ * x86_push_imm (code, mono_get_exception_arithmetic ()); 
+ * x86_call_code (code, arch_get_throw_exception ()); 
  *
  */
 gpointer 
-mono_arch_get_throw_exception_by_name (void)
+mono_arch_get_throw_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
 {
-       guint8* start;
-       guint8 *code;
-
-       start = code = mono_global_codeman_reserve (32);
-
-       /* Not used */
-       x86_breakpoint (code);
-
-       mono_arch_flush_icache (start, code - start);
+       return get_throw_exception ("throw_exception_trampoline", FALSE, FALSE, FALSE, code_size, ji, aot);
+}
 
-       return start;
+gpointer 
+mono_arch_get_rethrow_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
+{
+       return get_throw_exception ("rethow_exception_trampoline", TRUE, FALSE, FALSE, code_size, ji, aot);
 }
 
 /**
@@ -540,49 +602,27 @@ mono_arch_get_throw_exception_by_name (void)
  * needs no relocations in the caller.
  */
 gpointer 
-mono_arch_get_throw_corlib_exception (void)
+mono_arch_get_throw_corlib_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
 {
-       static guint8* start;
-       static int inited = 0;
-       guint8 *code;
+       return get_throw_exception ("throw_corlib_exception_trampoline", FALSE, FALSE, TRUE, code_size, ji, aot);
+}
 
-       if (inited)
-               return start;
+void
+mono_arch_exceptions_init (void)
+{
+       guint8 *tramp;
 
-       inited = 1;
-       code = start = mono_global_codeman_reserve (64);
+       if (mono_aot_only)
+               return;
 
-       /* 
-        * Align the stack on apple, the caller doesn't do this to save space,
-        * two arguments + the return addr are already on the stack.
-        */
-       x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
-       x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4 + 4, 4); /* token */
-       x86_alu_reg_imm (code, X86_ADD, X86_EAX, MONO_TOKEN_TYPE_DEF);
-       /* Align the stack on apple */
-       x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
-       x86_push_reg (code, X86_EAX);
-       x86_push_imm (code, mono_defaults.exception_class->image);
-       x86_call_code (code, mono_exception_from_token);
-       x86_alu_reg_imm (code, X86_ADD, X86_ESP, 16);
-       /* Compute caller ip */
-       x86_mov_reg_membase (code, X86_ECX, X86_ESP, 4, 4);
-       /* Compute offset */
-       x86_mov_reg_membase (code, X86_EDX, X86_ESP, 4 + 4 + 4, 4);
-       /* Pop everything */
-       x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4 + 4 + 4 + 4);
-       x86_alu_reg_reg (code, X86_SUB, X86_ECX, X86_EDX);
-       /* Align the stack on apple, mirrors the sub in OP_THROW. */
-       x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
-       /* Push exception object */
-       x86_push_reg (code, X86_EAX);
-       /* Push throw IP */
-       x86_push_reg (code, X86_ECX);
-       x86_jump_code (code, mono_arch_get_throw_exception ());
+       /* LLVM needs different throw trampolines */
+       tramp = get_throw_exception ("llvm_throw_exception_trampoline", FALSE, TRUE, FALSE, NULL, NULL, FALSE);
 
-       g_assert ((code - start) < 64);
+       mono_register_jit_icall (tramp, "mono_arch_llvm_throw_exception", NULL, TRUE);
 
-       return start;
+       tramp = get_throw_exception ("llvm_throw_corlib_exception_trampoline", FALSE, TRUE, TRUE, NULL, NULL, FALSE);
+
+       mono_register_jit_icall (tramp, "mono_arch_llvm_throw_corlib_exception", NULL, TRUE);
 }
 
 /*
@@ -656,12 +696,18 @@ mono_arch_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
                }
 
                /* Pop arguments off the stack */
+               /* 
+                * FIXME: LLVM doesn't push these, we can't use ji->from_llvm as it describes
+                * the caller.
+                */
+#ifndef ENABLE_LLVM
                {
                        MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (ji->method)->param_count + 1);
 
                        guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (ji->method), mono_method_signature (ji->method)->param_count, arg_info);
                        new_ctx->esp += stack_to_pop;
                }
+#endif
 
                return TRUE;
        } else if (*lmf) {
@@ -710,6 +756,7 @@ mono_arch_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
                        /* Pop arguments off the stack */
                        /* FIXME: Handle the delegate case too ((*lmf)->method == NULL) */
                        /* FIXME: Handle the IMT/vtable case too */
+#ifndef ENABLE_LLVM
                        if ((*lmf)->method && (*lmf)->method != MONO_FAKE_IMT_METHOD && (*lmf)->method != MONO_FAKE_VTABLE_METHOD) {
                                MonoMethod *method = (*lmf)->method;
                                MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (method)->param_count + 1);
@@ -717,6 +764,7 @@ mono_arch_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
                                guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (method), mono_method_signature (method)->param_count, arg_info);
                                new_ctx->esp += stack_to_pop;
                        }
+#endif
                }
                else
                        /* the lmf is always stored on the stack, so the following
@@ -864,7 +912,7 @@ altstack_handle_and_restore (void *sigctx, gpointer obj, gboolean stack_ovf)
        void (*restore_context) (MonoContext *);
        MonoContext mctx;
 
-       restore_context = mono_arch_get_restore_context ();
+       restore_context = mono_get_restore_context ();
        mono_arch_sigctx_to_monoctx (sigctx, &mctx);
 
        if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj)) {