Merge pull request #2831 from razzfazz/fix_dllimport
[mono.git] / mono / mini / tramp-x86.c
index 6a90838387123b107fba38e4b19250bdfb117d6e..dd560843018d8a2a3a59b8917eb25aada8874680 100644 (file)
@@ -18,7 +18,7 @@
 #include <mono/metadata/mono-debug.h>
 #include <mono/metadata/mono-debug-debugger.h>
 #include <mono/metadata/profiler-private.h>
-#include <mono/metadata/gc-internal.h>
+#include <mono/metadata/gc-internals.h>
 #include <mono/arch/x86/x86-codegen.h>
 
 #include <mono/utils/memcheck.h>
@@ -26,6 +26,7 @@
 #include "mini.h"
 #include "mini-x86.h"
 #include "debugger-agent.h"
+#include "jit-icalls.h"
 
 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
 
@@ -90,37 +91,6 @@ mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericCo
        return start;
 }
 
-gpointer
-mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_offset)
-{
-       guint8 *code, *start;
-       int buf_len;
-       int this_offset;
-
-       buf_len = 32;
-
-       start = code = mono_domain_code_reserve (domain, buf_len);
-
-       this_offset = mono_x86_get_this_arg_offset (mono_method_signature (m));
-
-       /* Set imt arg */
-       x86_mov_reg_imm (code, MONO_ARCH_IMT_REG, m);
-       /* Load this */
-       x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_offset + 4, 4);
-       /* Load vtable address */
-       x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
-       x86_jump_membase (code, X86_EAX, vt_offset);
-
-       g_assert ((code - start) < buf_len);
-
-       nacl_domain_code_validate (domain, &start, buf_len, &code);
-
-       mono_arch_flush_icache (start, code - start);
-       mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE, NULL);
-
-       return start;
-}
-
 void
 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
 {
@@ -253,7 +223,7 @@ guchar*
 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
 {
        char *tramp_name;
-       guint8 *buf, *code, *tramp;
+       guint8 *buf, *code, *tramp, *br_ex_check;
        GSList *unwind_ops = NULL;
        MonoJumpInfo *ji = NULL;
        int i, offset, frame_size, regarray_offset, lmf_offset, caller_ip_offset, arg_offset;
@@ -407,19 +377,60 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
         */
        x86_mov_membase_reg (code, X86_EBP, arg_offset, X86_EAX, 4);
 
+       /* Restore LMF */
+       x86_mov_reg_membase (code, X86_EAX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), sizeof (mgreg_t));
+       x86_mov_reg_membase (code, X86_ECX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), sizeof (mgreg_t));
+       x86_alu_reg_imm (code, X86_SUB, X86_ECX, 1);
+       x86_mov_membase_reg (code, X86_EAX, 0, X86_ECX, sizeof (mgreg_t));
+
        /* Check for interruptions */
        if (aot) {
-               code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
+               code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint_noraise");
                x86_call_reg (code, X86_EAX);
        } else {
-               x86_call_code (code, (guint8*)mono_thread_force_interruption_checkpoint);
+               x86_call_code (code, (guint8*)mono_thread_force_interruption_checkpoint_noraise);
        }
 
-       /* Restore LMF */
-       x86_mov_reg_membase (code, X86_EAX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), sizeof (mgreg_t));
-       x86_mov_reg_membase (code, X86_ECX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), sizeof (mgreg_t));
-       x86_alu_reg_imm (code, X86_SUB, X86_ECX, 1);
-       x86_mov_membase_reg (code, X86_EAX, 0, X86_ECX, sizeof (mgreg_t));
+       x86_test_reg_reg (code, X86_EAX, X86_EAX);
+       br_ex_check = code;
+       x86_branch8 (code, X86_CC_Z, -1, 1);
+
+       /*
+        * Exception case:
+        * We have an exception we want to throw in the caller's frame, so pop
+        * the trampoline frame and throw from the caller.
+        */
+       x86_leave (code);
+       /*
+        * The exception is in eax.
+        * We are calling the throw trampoline used by OP_THROW, so we have to setup the
+        * stack to look the same.
+        * The stack contains the ret addr, and the trampoline argument, the throw trampoline
+        * expects it to contain the ret addr and the exception. It also needs to be aligned
+        * after the exception is pushed.
+        */
+       /* Align stack */
+       x86_push_reg (code, X86_EAX);
+       /* Push the exception */
+       x86_push_reg (code, X86_EAX);
+       //x86_breakpoint (code);
+       /* Push the original return value */
+       x86_push_membase (code, X86_ESP, 3 * 4);
+       /*
+        * EH is initialized after trampolines, so get the address of the variable
+        * which contains throw_exception, and load it from there.
+        */
+       if (aot) {
+               /* Not really a jit icall */
+               code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "throw_exception_addr");
+       } else {
+               x86_mov_reg_imm (code, X86_ECX, (guint8*)mono_get_throw_exception_addr ());
+       }
+       x86_mov_reg_membase (code, X86_ECX, X86_ECX, 0, sizeof(gpointer));
+       x86_jump_reg (code, X86_ECX);
+
+       /* Normal case */
+       mono_x86_patch (br_ex_check, code);
 
        /* Restore registers */
        for (i = X86_EAX; i <= X86_EDI; ++i) {
@@ -789,6 +800,7 @@ mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gbo
        /* Argument area */
        framesize += sizeof (mgreg_t);
 
+       framesize = ALIGN_TO (framesize, 8);
        ctx_offset = framesize;
        framesize += sizeof (MonoContext);
 
@@ -830,7 +842,7 @@ mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gbo
        x86_mov_membase_reg (code, X86_ESP, 0, X86_EAX, sizeof (mgreg_t));
 
        if (aot) {
-               x86_breakpoint ();
+               x86_breakpoint (code);
        } else {
                if (single_step)
                        x86_call_code (code, debugger_agent_single_step_from_context);
@@ -866,17 +878,3 @@ mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gbo
        return buf;
 }
 
-#if defined(ENABLE_GSHAREDVT)
-
-#include "../../../mono-extensions/mono/mini/tramp-x86-gsharedvt.c"
-
-#else
-
-gpointer
-mono_arch_get_gsharedvt_trampoline (MonoTrampInfo **info, gboolean aot)
-{
-       *info = NULL;
-       return NULL;
-}
-
-#endif /* !MONOTOUCH */