Merge remote branch 'upstream/master'
[mono.git] / mono / mini / mini-x86.h
index 3870046bb140122728d80902a3e28f4d4ccc5a6d..3cc5b440367f1c37fcb09a7938a040e2afb67c1a 100644 (file)
@@ -2,7 +2,16 @@
 #define __MONO_MINI_X86_H__
 
 #include <mono/arch/x86/x86-codegen.h>
-#ifdef PLATFORM_WIN32
+#include <mono/utils/mono-sigcontext.h>
+
+#ifdef __native_client_codegen__
+#define kNaClAlignmentX86 32
+#define kNaClAlignmentMaskX86 (kNaClAlignmentX86 - 1)
+
+#define kNaClLengthOfCallImm kx86NaClLengthOfCallImm
+#endif
+
+#ifdef HOST_WIN32
 #include <windows.h>
 /* use SIG* defines if possible */
 #ifdef HAVE_SIGNAL_H
@@ -22,7 +31,7 @@ struct sigcontext {
        unsigned int eip;
 };
 
-typedef void (* MonoW32ExceptionHandler) (int);
+typedef void (* MonoW32ExceptionHandler) (int _dummy, EXCEPTION_RECORD *info, void *context);
 void win32_seh_init(void);
 void win32_seh_cleanup(void);
 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler);
@@ -41,13 +50,24 @@ void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler);
 
 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep);
 
-#endif /* PLATFORM_WIN32 */
+#endif /* HOST_WIN32 */
+
+#ifdef __HAIKU__
+struct sigcontext {
+       vregs regs;
+};
+#endif /* __HAIKU__ */
 
-#if defined( __linux__) || defined(__sun) || defined(__APPLE__) || defined(__NetBSD__)
+#if defined( __linux__) || defined(__sun) || defined(__APPLE__) || defined(__NetBSD__) || \
+       defined(__FreeBSD__) || defined(__OpenBSD__)
 #define MONO_ARCH_USE_SIGACTION
 #endif
 
-#ifndef PLATFORM_WIN32
+#if defined(__native_client__)
+#undef MONO_ARCH_USE_SIGACTION
+#endif
+
+#ifndef HOST_WIN32
 
 #ifdef HAVE_WORKING_SIGALTSTACK
 /* 
@@ -57,18 +77,31 @@ LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep);
 #ifndef __sun
 #define MONO_ARCH_SIGSEGV_ON_ALTSTACK
 #endif
+/* Haiku doesn't have SA_SIGINFO */
+#ifndef __HAIKU__
 #define MONO_ARCH_USE_SIGACTION
+#endif /* __HAIKU__ */
 
 #endif /* HAVE_WORKING_SIGALTSTACK */
-#endif /* !PLATFORM_WIN32 */
+#endif /* !HOST_WIN32 */
+
+#define MONO_ARCH_SUPPORT_SIMD_INTRINSICS 1
+#define MONO_ARCH_SUPPORT_TASKLETS 1
+
+#ifndef DISABLE_SIMD
+#define MONO_ARCH_SIMD_INTRINSICS 1
+#define MONO_ARCH_NEED_SIMD_BANK 1
+#endif
 
 /* we should lower this size and make sure we don't call heavy stack users in the segv handler */
 #define MONO_ARCH_SIGNAL_STACK_SIZE (16 * 1024)
+#define MONO_ARCH_HAVE_RESTORE_STACK_SUPPORT 1
 
 #define MONO_ARCH_CPU_SPEC x86_desc
 
 #define MONO_MAX_IREGS 8
 #define MONO_MAX_FREGS 8
+#define MONO_MAX_XREGS 8
 
 /* Parameters used by the register allocator */
 #define MONO_ARCH_CALLEE_REGS X86_CALLEE_REGS
@@ -77,6 +110,10 @@ LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep);
 #define MONO_ARCH_CALLEE_FREGS (0xff & ~(regmask (MONO_ARCH_FPSTACK_SIZE)))
 #define MONO_ARCH_CALLEE_SAVED_FREGS 0
 
+/* All registers are clobered by a call */
+#define MONO_ARCH_CALLEE_XREGS (0xff & ~(regmask (MONO_MAX_XREGS)))
+#define MONO_ARCH_CALLEE_SAVED_XREGS 0
+
 #define MONO_ARCH_USE_FPSTACK TRUE
 #define MONO_ARCH_FPSTACK_SIZE 6
 
@@ -98,27 +135,30 @@ LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep);
 #define MONO_ARCH_INST_IS_REGPAIR(desc) (desc == 'l' || desc == 'L')
 #define MONO_ARCH_INST_REGPAIR_REG2(desc,hreg1) (desc == 'l' ? X86_EDX : -1)
 
-#define MONO_ARCH_FRAME_ALIGNMENT 4
+/* must be at a power of 2 and >= 8 */
+#define MONO_ARCH_FRAME_ALIGNMENT 16
 
 /* fixme: align to 16byte instead of 32byte (we align to 32byte to get 
  * reproduceable results for benchmarks */
 #define MONO_ARCH_CODE_ALIGNMENT 32
 
-#define MONO_ARCH_BASEREG X86_EBP
 #define MONO_ARCH_RETREG1 X86_EAX
 #define MONO_ARCH_RETREG2 X86_EDX
 
-#define MONO_ARCH_ENCODE_LREG(r1,r2) (r1 | (r2<<3))
+/*This is the max size of the locals area of a given frame. I think 1MB is a safe default for now*/
+#define MONO_ARCH_MAX_FRAME_SIZE 0x100000
 
-#define inst_dreg_low dreg&7 
-#define inst_dreg_high dreg>>3
-#define inst_sreg1_low sreg1&7 
-#define inst_sreg1_high sreg1>>3
-#define inst_sreg2_low sreg2&7 
-#define inst_sreg2_high sreg2>>3
+/*This is how much a try block must be extended when is is preceeded by a Monitor.Enter() call.
+It's 4 bytes as this is how many bytes + 1 that 'add 0x10, %esp' takes. It is used to pop the arguments from
+the monitor.enter call and must be already protected.*/
+#define MONO_ARCH_MONITOR_ENTER_ADJUSTMENT 4
 
 struct MonoLMF {
-       /* Offset by 1 if this is a trampoline LMF frame */
+       /* 
+        * If the lowest bit is set to 1, then this is a trampoline LMF frame.
+        * If the second lowest bit is set to 1, then this is a MonoLMFExt structure, and
+        * the other fields are not valid.
+        */
        guint32    previous_lmf;
        gpointer    lmf_addr;
        /* Only set in trampoline LMF frames */
@@ -132,63 +172,10 @@ struct MonoLMF {
        guint32     eip;
 };
 
-typedef void* MonoCompileArch;
-
-#if defined(__FreeBSD__) || defined(__APPLE__)
-#include <ucontext.h>
-#endif 
-
-#if defined(__FreeBSD__)
-       #define UCONTEXT_REG_EAX(ctx) ((ctx)->uc_mcontext.mc_eax)
-       #define UCONTEXT_REG_EBX(ctx) ((ctx)->uc_mcontext.mc_ebx)
-       #define UCONTEXT_REG_ECX(ctx) ((ctx)->uc_mcontext.mc_ecx)
-       #define UCONTEXT_REG_EDX(ctx) ((ctx)->uc_mcontext.mc_edx)
-       #define UCONTEXT_REG_EBP(ctx) ((ctx)->uc_mcontext.mc_ebp)
-       #define UCONTEXT_REG_ESP(ctx) ((ctx)->uc_mcontext.mc_esp)
-       #define UCONTEXT_REG_ESI(ctx) ((ctx)->uc_mcontext.mc_esi)
-       #define UCONTEXT_REG_EDI(ctx) ((ctx)->uc_mcontext.mc_edi)
-       #define UCONTEXT_REG_EIP(ctx) ((ctx)->uc_mcontext.mc_eip)
-#elif defined(__APPLE__) && defined(_STRUCT_MCONTEXT)
-       #define UCONTEXT_REG_EAX(ctx) ((ctx)->uc_mcontext->__ss.__eax)
-       #define UCONTEXT_REG_EBX(ctx) ((ctx)->uc_mcontext->__ss.__ebx)
-       #define UCONTEXT_REG_ECX(ctx) ((ctx)->uc_mcontext->__ss.__ecx)
-       #define UCONTEXT_REG_EDX(ctx) ((ctx)->uc_mcontext->__ss.__edx)
-       #define UCONTEXT_REG_EBP(ctx) ((ctx)->uc_mcontext->__ss.__ebp)
-       #define UCONTEXT_REG_ESP(ctx) ((ctx)->uc_mcontext->__ss.__esp)
-       #define UCONTEXT_REG_ESI(ctx) ((ctx)->uc_mcontext->__ss.__esi)
-       #define UCONTEXT_REG_EDI(ctx) ((ctx)->uc_mcontext->__ss.__edi)
-       #define UCONTEXT_REG_EIP(ctx) ((ctx)->uc_mcontext->__ss.__eip)
-#elif defined(__APPLE__) && !defined(_STRUCT_MCONTEXT)
-       #define UCONTEXT_REG_EAX(ctx) ((ctx)->uc_mcontext->ss.eax)
-       #define UCONTEXT_REG_EBX(ctx) ((ctx)->uc_mcontext->ss.ebx)
-       #define UCONTEXT_REG_ECX(ctx) ((ctx)->uc_mcontext->ss.ecx)
-       #define UCONTEXT_REG_EDX(ctx) ((ctx)->uc_mcontext->ss.edx)
-       #define UCONTEXT_REG_EBP(ctx) ((ctx)->uc_mcontext->ss.ebp)
-       #define UCONTEXT_REG_ESP(ctx) ((ctx)->uc_mcontext->ss.esp)
-       #define UCONTEXT_REG_ESI(ctx) ((ctx)->uc_mcontext->ss.esi)
-       #define UCONTEXT_REG_EDI(ctx) ((ctx)->uc_mcontext->ss.edi)
-       #define UCONTEXT_REG_EIP(ctx) ((ctx)->uc_mcontext->ss.eip)
-#elif defined(__NetBSD__)
-       #define UCONTEXT_REG_EAX(ctx) ((ctx)->uc_mcontext.__gregs [_REG_EAX])
-       #define UCONTEXT_REG_EBX(ctx) ((ctx)->uc_mcontext.__gregs [_REG_EBX])
-       #define UCONTEXT_REG_ECX(ctx) ((ctx)->uc_mcontext.__gregs [_REG_ECX])
-       #define UCONTEXT_REG_EDX(ctx) ((ctx)->uc_mcontext.__gregs [_REG_EDX])
-       #define UCONTEXT_REG_EBP(ctx) ((ctx)->uc_mcontext.__gregs [_REG_EBP])
-       #define UCONTEXT_REG_ESP(ctx) ((ctx)->uc_mcontext.__gregs [_REG_ESP])
-       #define UCONTEXT_REG_ESI(ctx) ((ctx)->uc_mcontext.__gregs [_REG_ESI])
-       #define UCONTEXT_REG_EDI(ctx) ((ctx)->uc_mcontext.__gregs [_REG_EDI])
-       #define UCONTEXT_REG_EIP(ctx) ((ctx)->uc_mcontext.__gregs [_REG_EIP])
-#else
-       #define UCONTEXT_REG_EAX(ctx) ((ctx)->uc_mcontext.gregs [REG_EAX])
-       #define UCONTEXT_REG_EBX(ctx) ((ctx)->uc_mcontext.gregs [REG_EBX])
-       #define UCONTEXT_REG_ECX(ctx) ((ctx)->uc_mcontext.gregs [REG_ECX])
-       #define UCONTEXT_REG_EDX(ctx) ((ctx)->uc_mcontext.gregs [REG_EDX])
-       #define UCONTEXT_REG_EBP(ctx) ((ctx)->uc_mcontext.gregs [REG_EBP])
-       #define UCONTEXT_REG_ESP(ctx) ((ctx)->uc_mcontext.gregs [REG_ESP])
-       #define UCONTEXT_REG_ESI(ctx) ((ctx)->uc_mcontext.gregs [REG_ESI])
-       #define UCONTEXT_REG_EDI(ctx) ((ctx)->uc_mcontext.gregs [REG_EDI])
-       #define UCONTEXT_REG_EIP(ctx) ((ctx)->uc_mcontext.gregs [REG_EIP])
-#endif
+typedef struct {
+       gboolean need_stack_frame_inited;
+       gboolean need_stack_frame;
+} MonoCompileArch;
 
 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
 # define SC_EAX sc_eax
@@ -200,6 +187,16 @@ typedef void* MonoCompileArch;
 # define SC_ESP sc_esp
 # define SC_EDI sc_edi
 # define SC_ESI sc_esi
+#elif defined(__HAIKU__)
+# define SC_EAX regs.eax
+# define SC_EBX regs._reserved_2[2]
+# define SC_ECX regs.ecx
+# define SC_EDX regs.edx
+# define SC_EBP regs.ebp
+# define SC_EIP regs.eip
+# define SC_ESP regs.esp
+# define SC_EDI regs._reserved_2[0]
+# define SC_ESI regs._reserved_2[1]
 #else
 # define SC_EAX eax
 # define SC_EBX ebx
@@ -232,6 +229,8 @@ typedef struct {
 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->ebp))
 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->esp))
 
+#define MONO_CONTEXT_SET_LLVM_EXC_REG(ctx, exc) do { (ctx)->eax = (gsize)exc; } while (0)
+
 #ifdef _MSC_VER
 
 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx, start_func) do { \
@@ -256,6 +255,8 @@ typedef struct {
 
 #endif
 
+#define MONO_ARCH_INIT_TOP_LMF_ENTRY(lmf) do { (lmf)->ebp = -1; } while (0)
+
 /* Enables OP_LSHL, OP_LSHL_IMM, OP_LSHR, OP_LSHR_IMM, OP_LSHR_UN, OP_LSHR_UN_IMM */
 #define MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
 
@@ -264,28 +265,66 @@ typedef struct {
 #define MONO_ARCH_HAVE_IS_INT_OVERFLOW 1
 #define MONO_ARCH_HAVE_INVALIDATE_METHOD 1
 #define MONO_ARCH_NEED_GOT_VAR 1
-#define MONO_ARCH_HAVE_THROW_CORLIB_EXCEPTION 1
-#define MONO_ARCH_ENABLE_EMIT_STATE_OPT 1
+#if !defined(__APPLE__)
 #define MONO_ARCH_ENABLE_MONO_LMF_VAR 1
-#define MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN 1
+#endif
 #define MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE 1
 #define MONO_ARCH_HAVE_ATOMIC_ADD 1
 #define MONO_ARCH_HAVE_ATOMIC_EXCHANGE 1
-#define MONO_ARCH_HAVE_ATOMIC_CAS_IMM 1
+#define MONO_ARCH_HAVE_ATOMIC_CAS 1
 #define MONO_ARCH_HAVE_IMT 1
-#define MONO_ARCH_HAVE_TLS_GET 1
+#define MONO_ARCH_HAVE_TLS_GET (mono_x86_have_tls_get ())
 #define MONO_ARCH_IMT_REG X86_EDX
 #define MONO_ARCH_VTABLE_REG X86_EDX
-#define MONO_ARCH_COMMON_VTABLE_TRAMPOLINE 1
 #define MONO_ARCH_RGCTX_REG X86_EDX
-#define MONO_ARCH_ENABLE_NORMALIZE_OPCODES 1
+#define MONO_ARCH_HAVE_GENERALIZED_IMT_THUNK 1
+#define MONO_ARCH_HAVE_LIVERANGE_OPS 1
+#define MONO_ARCH_HAVE_XP_UNWIND 1
+#define MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX 1
+#if defined(__linux__)
+#define MONO_ARCH_MONITOR_OBJECT_REG X86_EAX
+#endif
+#define MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE 1
+#if !defined (__APPLE__) || defined(__native_client_codegen__)
+#define MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES 1
+#endif
+#define MONO_ARCH_GOT_REG X86_EBX
+#define MONO_ARCH_HAVE_GET_TRAMPOLINES 1
 
 #define MONO_ARCH_HAVE_CMOV_OPS 1
 
-#if !defined(__APPLE__)
+#ifdef MONO_ARCH_SIMD_INTRINSICS
+#define MONO_ARCH_HAVE_DECOMPOSE_OPTS 1
+#endif
+
+#define MONO_ARCH_HAVE_DECOMPOSE_LONG_OPTS 1
+
 #define MONO_ARCH_AOT_SUPPORTED 1
+
+#if defined(__linux__) || defined(__sun)
+#define MONO_ARCH_ENABLE_MONITOR_IL_FASTPATH 1
+#endif
+
+#define MONO_ARCH_GSHARED_SUPPORTED 1
+#define MONO_ARCH_HAVE_LLVM_IMT_TRAMPOLINE 1
+#define MONO_ARCH_LLVM_SUPPORTED 1
+#define MONO_ARCH_THIS_AS_FIRST_ARG 1
+
+#if defined(MONO_ARCH_USE_SIGACTION) || defined(TARGET_WIN32)
+#define MONO_ARCH_SOFT_DEBUG_SUPPORTED 1
 #endif
 
+#define MONO_ARCH_HAVE_EXCEPTIONS_INIT 1
+#define MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD 1
+
+#define MONO_ARCH_HAVE_CARD_TABLE_WBARRIER 1
+#define MONO_ARCH_GC_MAPS_SUPPORTED 1
+
+gboolean
+mono_x86_tail_call_supported (MonoMethodSignature *caller_sig, MonoMethodSignature *callee_sig) MONO_INTERNAL;
+
+#define MONO_ARCH_USE_OP_TAIL_CALL(caller_sig, callee_sig) mono_x86_tail_call_supported (caller_sig, callee_sig)
+
 /* Used for optimization, not complete */
 #define MONO_ARCH_IS_OP_MEMBASE(opcode) ((opcode) == OP_X86_PUSH_MEMBASE)
 
@@ -306,5 +345,25 @@ typedef struct {
 
 extern MonoBreakpointInfo mono_breakpoint_info [MONO_BREAKPOINT_ARRAY_SIZE];
 
+guint8*
+mono_x86_emit_tls_get (guint8* code, int dreg, int tls_offset) MONO_INTERNAL;
+
+guint32
+mono_x86_get_this_arg_offset (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig) MONO_INTERNAL;
+
+gboolean
+mono_x86_have_tls_get (void) MONO_INTERNAL;
+
+void
+mono_x86_throw_exception (mgreg_t *regs, MonoObject *exc, 
+                                                 mgreg_t eip, gboolean rethrow) MONO_INTERNAL;
+
+void
+mono_x86_throw_corlib_exception (mgreg_t *regs, guint32 ex_token_index, 
+                                                                mgreg_t eip, gint32 pc_offset) MONO_INTERNAL;
+
+void 
+mono_x86_patch (unsigned char* code, gpointer target) MONO_INTERNAL;
+
 #endif /* __MONO_MINI_X86_H__ */