2 * mini-x86.c: x86 backend for the Mono code generator
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
9 * Copyright 2003 Ximian, Inc.
10 * Copyright 2003-2011 Novell Inc.
11 * Copyright 2011 Xamarin Inc.
20 #include <mono/metadata/appdomain.h>
21 #include <mono/metadata/debug-helpers.h>
22 #include <mono/metadata/threads.h>
23 #include <mono/metadata/profiler-private.h>
24 #include <mono/metadata/mono-debug.h>
25 #include <mono/metadata/gc-internal.h>
26 #include <mono/utils/mono-math.h>
27 #include <mono/utils/mono-counters.h>
28 #include <mono/utils/mono-mmap.h>
29 #include <mono/utils/mono-memory-model.h>
30 #include <mono/utils/mono-hwcap-x86.h>
38 /* On windows, these hold the key returned by TlsAlloc () */
39 static gint lmf_tls_offset = -1;
41 static gint jit_tls_offset = -1;
43 static gint lmf_addr_tls_offset = -1;
45 static gint appdomain_tls_offset = -1;
48 static gboolean optimize_for_xen = TRUE;
50 #define optimize_for_xen 0
54 static gboolean is_win32 = TRUE;
56 static gboolean is_win32 = FALSE;
59 /* This mutex protects architecture specific caches */
60 #define mono_mini_arch_lock() EnterCriticalSection (&mini_arch_mutex)
61 #define mono_mini_arch_unlock() LeaveCriticalSection (&mini_arch_mutex)
62 static CRITICAL_SECTION mini_arch_mutex;
64 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
69 /* Under windows, the default pinvoke calling convention is stdcall */
70 #define CALLCONV_IS_STDCALL(sig) ((((sig)->call_convention) == MONO_CALL_STDCALL) || ((sig)->pinvoke && ((sig)->call_convention) == MONO_CALL_DEFAULT) || ((sig)->pinvoke && ((sig)->call_convention) == MONO_CALL_THISCALL))
72 #define CALLCONV_IS_STDCALL(sig) (((sig)->call_convention) == MONO_CALL_STDCALL || ((sig)->pinvoke && ((sig)->call_convention) == MONO_CALL_THISCALL))
75 #define X86_IS_CALLEE_SAVED_REG(reg) (((reg) == X86_EBX) || ((reg) == X86_EDI) || ((reg) == X86_ESI))
78 mono_breakpoint_info [MONO_BREAKPOINT_ARRAY_SIZE];
81 emit_load_aotconst (guint8 *start, guint8 *code, MonoCompile *cfg, MonoJumpInfo **ji, int dreg, int tramp_type, gconstpointer target);
83 #ifdef __native_client_codegen__
85 /* Default alignment for Native Client is 32-byte. */
86 gint8 nacl_align_byte = -32; /* signed version of 0xe0 */
88 /* mono_arch_nacl_pad: Add pad bytes of alignment instructions at code, */
89 /* Check that alignment doesn't cross an alignment boundary. */
91 mono_arch_nacl_pad (guint8 *code, int pad)
93 const int kMaxPadding = 7; /* see x86-codegen.h: x86_padding() */
95 if (pad == 0) return code;
96 /* assertion: alignment cannot cross a block boundary */
97 g_assert(((uintptr_t)code & (~kNaClAlignmentMask)) ==
98 (((uintptr_t)code + pad - 1) & (~kNaClAlignmentMask)));
99 while (pad >= kMaxPadding) {
100 x86_padding (code, kMaxPadding);
103 if (pad != 0) x86_padding (code, pad);
108 mono_arch_nacl_skip_nops (guint8 *code)
110 x86_skip_nops (code);
114 #endif /* __native_client_codegen__ */
117 * The code generated for sequence points reads from this location, which is
118 * made read-only when single stepping is enabled.
120 static gpointer ss_trigger_page;
122 /* Enabled breakpoints read from this trigger page */
123 static gpointer bp_trigger_page;
126 mono_arch_regname (int reg)
129 case X86_EAX: return "%eax";
130 case X86_EBX: return "%ebx";
131 case X86_ECX: return "%ecx";
132 case X86_EDX: return "%edx";
133 case X86_ESP: return "%esp";
134 case X86_EBP: return "%ebp";
135 case X86_EDI: return "%edi";
136 case X86_ESI: return "%esi";
142 mono_arch_fregname (int reg)
167 mono_arch_xregname (int reg)
192 mono_x86_patch (unsigned char* code, gpointer target)
194 x86_patch (code, (unsigned char*)target);
205 /* gsharedvt argument passed by addr */
217 /* Only if storage == ArgValuetypeInReg */
218 ArgStorage pair_storage [2];
227 gboolean need_stack_align;
228 guint32 stack_align_amount;
229 gboolean vtype_retaddr;
230 /* The index of the vret arg in the argument list */
238 #define FLOAT_PARAM_REGS 0
240 static const guint32 thiscall_param_regs [] = { X86_ECX, X86_NREG };
242 static const guint32 *callconv_param_regs(MonoMethodSignature *sig)
247 switch (sig->call_convention) {
248 case MONO_CALL_THISCALL:
249 return thiscall_param_regs;
255 #if defined(TARGET_WIN32) || defined(__APPLE__) || defined(__FreeBSD__)
256 #define SMALL_STRUCTS_IN_REGS
257 static X86_Reg_No return_regs [] = { X86_EAX, X86_EDX };
261 add_general (guint32 *gr, const guint32 *param_regs, guint32 *stack_size, ArgInfo *ainfo)
263 ainfo->offset = *stack_size;
265 if (!param_regs || param_regs [*gr] == X86_NREG) {
266 ainfo->storage = ArgOnStack;
268 (*stack_size) += sizeof (gpointer);
271 ainfo->storage = ArgInIReg;
272 ainfo->reg = param_regs [*gr];
278 add_general_pair (guint32 *gr, const guint32 *param_regs , guint32 *stack_size, ArgInfo *ainfo)
280 ainfo->offset = *stack_size;
282 g_assert(!param_regs || param_regs[*gr] == X86_NREG);
284 ainfo->storage = ArgOnStack;
285 (*stack_size) += sizeof (gpointer) * 2;
290 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
292 ainfo->offset = *stack_size;
294 if (*gr >= FLOAT_PARAM_REGS) {
295 ainfo->storage = ArgOnStack;
296 (*stack_size) += is_double ? 8 : 4;
297 ainfo->nslots = is_double ? 2 : 1;
300 /* A double register */
302 ainfo->storage = ArgInDoubleSSEReg;
304 ainfo->storage = ArgInFloatSSEReg;
312 add_valuetype (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
314 guint32 *gr, const guint32 *param_regs, guint32 *fr, guint32 *stack_size)
319 klass = mono_class_from_mono_type (type);
320 size = mini_type_stack_size_full (gsctx, &klass->byval_arg, NULL, sig->pinvoke);
322 #ifdef SMALL_STRUCTS_IN_REGS
323 if (sig->pinvoke && is_return) {
324 MonoMarshalType *info;
327 * the exact rules are not very well documented, the code below seems to work with the
328 * code generated by gcc 3.3.3 -mno-cygwin.
330 info = mono_marshal_load_type_info (klass);
333 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
335 /* Special case structs with only a float member */
336 if (info->num_fields == 1) {
337 int ftype = info->fields [0].field->type->type;
338 if ((info->native_size == 8) && (ftype == MONO_TYPE_R8)) {
339 ainfo->storage = ArgValuetypeInReg;
340 ainfo->pair_storage [0] = ArgOnDoubleFpStack;
343 if ((info->native_size == 4) && (ftype == MONO_TYPE_R4)) {
344 ainfo->storage = ArgValuetypeInReg;
345 ainfo->pair_storage [0] = ArgOnFloatFpStack;
349 if ((info->native_size == 1) || (info->native_size == 2) || (info->native_size == 4) || (info->native_size == 8)) {
350 ainfo->storage = ArgValuetypeInReg;
351 ainfo->pair_storage [0] = ArgInIReg;
352 ainfo->pair_regs [0] = return_regs [0];
353 if (info->native_size > 4) {
354 ainfo->pair_storage [1] = ArgInIReg;
355 ainfo->pair_regs [1] = return_regs [1];
362 if (param_regs && param_regs [*gr] != X86_NREG && !is_return) {
363 g_assert (size <= 4);
364 ainfo->storage = ArgValuetypeInReg;
365 ainfo->reg = param_regs [*gr];
370 ainfo->offset = *stack_size;
371 ainfo->storage = ArgOnStack;
372 *stack_size += ALIGN_TO (size, sizeof (gpointer));
373 ainfo->nslots = ALIGN_TO (size, sizeof (gpointer)) / sizeof (gpointer);
379 * Obtain information about a call according to the calling convention.
380 * For x86 ELF, see the "System V Application Binary Interface Intel386
381 * Architecture Processor Supplment, Fourth Edition" document for more
383 * For x86 win32, see ???.
386 get_call_info_internal (MonoGenericSharingContext *gsctx, CallInfo *cinfo, MonoMethodSignature *sig)
388 guint32 i, gr, fr, pstart;
389 const guint32 *param_regs;
391 int n = sig->hasthis + sig->param_count;
392 guint32 stack_size = 0;
393 gboolean is_pinvoke = sig->pinvoke;
399 param_regs = callconv_param_regs(sig);
403 ret_type = mini_type_get_underlying_type (gsctx, sig->ret);
404 switch (ret_type->type) {
405 case MONO_TYPE_BOOLEAN:
416 case MONO_TYPE_FNPTR:
417 case MONO_TYPE_CLASS:
418 case MONO_TYPE_OBJECT:
419 case MONO_TYPE_SZARRAY:
420 case MONO_TYPE_ARRAY:
421 case MONO_TYPE_STRING:
422 cinfo->ret.storage = ArgInIReg;
423 cinfo->ret.reg = X86_EAX;
427 cinfo->ret.storage = ArgInIReg;
428 cinfo->ret.reg = X86_EAX;
429 cinfo->ret.is_pair = TRUE;
432 cinfo->ret.storage = ArgOnFloatFpStack;
435 cinfo->ret.storage = ArgOnDoubleFpStack;
437 case MONO_TYPE_GENERICINST:
438 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
439 cinfo->ret.storage = ArgInIReg;
440 cinfo->ret.reg = X86_EAX;
443 if (mini_is_gsharedvt_type_gsctx (gsctx, ret_type)) {
444 cinfo->ret.storage = ArgOnStack;
445 cinfo->vtype_retaddr = TRUE;
449 case MONO_TYPE_VALUETYPE:
450 case MONO_TYPE_TYPEDBYREF: {
451 guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
453 add_valuetype (gsctx, sig, &cinfo->ret, ret_type, TRUE, &tmp_gr, NULL, &tmp_fr, &tmp_stacksize);
454 if (cinfo->ret.storage == ArgOnStack) {
455 cinfo->vtype_retaddr = TRUE;
456 /* The caller passes the address where the value is stored */
462 g_assert (mini_is_gsharedvt_type_gsctx (gsctx, ret_type));
463 cinfo->ret.storage = ArgOnStack;
464 cinfo->vtype_retaddr = TRUE;
467 cinfo->ret.storage = ArgNone;
470 g_error ("Can't handle as return value 0x%x", ret_type->type);
476 * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
477 * the first argument, allowing 'this' to be always passed in the first arg reg.
478 * Also do this if the first argument is a reference type, since virtual calls
479 * are sometimes made using calli without sig->hasthis set, like in the delegate
482 if (cinfo->vtype_retaddr && !is_pinvoke && (sig->hasthis || (sig->param_count > 0 && MONO_TYPE_IS_REFERENCE (mini_type_get_underlying_type (gsctx, sig->params [0]))))) {
484 add_general (&gr, param_regs, &stack_size, cinfo->args + 0);
486 add_general (&gr, param_regs, &stack_size, &cinfo->args [sig->hasthis + 0]);
489 cinfo->vret_arg_offset = stack_size;
490 add_general (&gr, NULL, &stack_size, &cinfo->ret);
491 cinfo->vret_arg_index = 1;
495 add_general (&gr, param_regs, &stack_size, cinfo->args + 0);
497 if (cinfo->vtype_retaddr)
498 add_general (&gr, NULL, &stack_size, &cinfo->ret);
501 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
502 fr = FLOAT_PARAM_REGS;
504 /* Emit the signature cookie just before the implicit arguments */
505 add_general (&gr, param_regs, &stack_size, &cinfo->sig_cookie);
508 for (i = pstart; i < sig->param_count; ++i) {
509 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
512 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
513 /* We allways pass the sig cookie on the stack for simplicity */
515 * Prevent implicit arguments + the sig cookie from being passed
518 fr = FLOAT_PARAM_REGS;
520 /* Emit the signature cookie just before the implicit arguments */
521 add_general (&gr, param_regs, &stack_size, &cinfo->sig_cookie);
524 if (sig->params [i]->byref) {
525 add_general (&gr, param_regs, &stack_size, ainfo);
528 ptype = mini_type_get_underlying_type (gsctx, sig->params [i]);
529 switch (ptype->type) {
530 case MONO_TYPE_BOOLEAN:
533 add_general (&gr, param_regs, &stack_size, ainfo);
538 add_general (&gr, param_regs, &stack_size, ainfo);
542 add_general (&gr, param_regs, &stack_size, ainfo);
547 case MONO_TYPE_FNPTR:
548 case MONO_TYPE_CLASS:
549 case MONO_TYPE_OBJECT:
550 case MONO_TYPE_STRING:
551 case MONO_TYPE_SZARRAY:
552 case MONO_TYPE_ARRAY:
553 add_general (&gr, param_regs, &stack_size, ainfo);
555 case MONO_TYPE_GENERICINST:
556 if (!mono_type_generic_inst_is_valuetype (ptype)) {
557 add_general (&gr, param_regs, &stack_size, ainfo);
560 if (mini_is_gsharedvt_type_gsctx (gsctx, ptype)) {
561 /* gsharedvt arguments are passed by ref */
562 add_general (&gr, param_regs, &stack_size, ainfo);
563 g_assert (ainfo->storage == ArgOnStack);
564 ainfo->storage = ArgGSharedVt;
568 case MONO_TYPE_VALUETYPE:
569 case MONO_TYPE_TYPEDBYREF:
570 add_valuetype (gsctx, sig, ainfo, ptype, FALSE, &gr, param_regs, &fr, &stack_size);
574 add_general_pair (&gr, param_regs, &stack_size, ainfo);
577 add_float (&fr, &stack_size, ainfo, FALSE);
580 add_float (&fr, &stack_size, ainfo, TRUE);
584 /* gsharedvt arguments are passed by ref */
585 g_assert (mini_is_gsharedvt_type_gsctx (gsctx, ptype));
586 add_general (&gr, param_regs, &stack_size, ainfo);
587 g_assert (ainfo->storage == ArgOnStack);
588 ainfo->storage = ArgGSharedVt;
591 g_error ("unexpected type 0x%x", ptype->type);
592 g_assert_not_reached ();
596 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
597 fr = FLOAT_PARAM_REGS;
599 /* Emit the signature cookie just before the implicit arguments */
600 add_general (&gr, param_regs, &stack_size, &cinfo->sig_cookie);
603 if (mono_do_x86_stack_align && (stack_size % MONO_ARCH_FRAME_ALIGNMENT) != 0) {
604 cinfo->need_stack_align = TRUE;
605 cinfo->stack_align_amount = MONO_ARCH_FRAME_ALIGNMENT - (stack_size % MONO_ARCH_FRAME_ALIGNMENT);
606 stack_size += cinfo->stack_align_amount;
609 cinfo->stack_usage = stack_size;
610 cinfo->reg_usage = gr;
611 cinfo->freg_usage = fr;
616 get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSignature *sig)
618 int n = sig->hasthis + sig->param_count;
622 cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
624 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
626 return get_call_info_internal (gsctx, cinfo, sig);
630 * mono_arch_get_argument_info:
631 * @csig: a method signature
632 * @param_count: the number of parameters to consider
633 * @arg_info: an array to store the result infos
635 * Gathers information on parameters such as size, alignment and
636 * padding. arg_info should be large enought to hold param_count + 1 entries.
638 * Returns the size of the argument area on the stack.
639 * This should be signal safe, since it is called from
640 * mono_arch_find_jit_info ().
641 * FIXME: The metadata calls might not be signal safe.
644 mono_arch_get_argument_info (MonoGenericSharingContext *gsctx, MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
646 int len, k, args_size = 0;
652 /* Avoid g_malloc as it is not signal safe */
653 len = sizeof (CallInfo) + (sizeof (ArgInfo) * (csig->param_count + 1));
654 cinfo = (CallInfo*)g_newa (guint8*, len);
655 memset (cinfo, 0, len);
657 cinfo = get_call_info_internal (gsctx, cinfo, csig);
659 arg_info [0].offset = offset;
661 if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 0) {
662 args_size += sizeof (gpointer);
667 args_size += sizeof (gpointer);
671 if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 1 && csig->hasthis) {
672 /* Emitted after this */
673 args_size += sizeof (gpointer);
677 arg_info [0].size = args_size;
679 for (k = 0; k < param_count; k++) {
680 size = mini_type_stack_size_full (NULL, csig->params [k], &align, csig->pinvoke);
682 /* ignore alignment for now */
685 args_size += pad = (align - (args_size & (align - 1))) & (align - 1);
686 arg_info [k].pad = pad;
688 arg_info [k + 1].pad = 0;
689 arg_info [k + 1].size = size;
691 arg_info [k + 1].offset = offset;
694 if (k == 0 && cinfo->vtype_retaddr && cinfo->vret_arg_index == 1 && !csig->hasthis) {
695 /* Emitted after the first arg */
696 args_size += sizeof (gpointer);
701 if (mono_do_x86_stack_align && !CALLCONV_IS_STDCALL (csig))
702 align = MONO_ARCH_FRAME_ALIGNMENT;
705 args_size += pad = (align - (args_size & (align - 1))) & (align - 1);
706 arg_info [k].pad = pad;
712 mono_arch_tail_call_supported (MonoMethodSignature *caller_sig, MonoMethodSignature *callee_sig)
714 MonoType *callee_ret;
718 c1 = get_call_info (NULL, NULL, caller_sig);
719 c2 = get_call_info (NULL, NULL, callee_sig);
721 * Tail calls with more callee stack usage than the caller cannot be supported, since
722 * the extra stack space would be left on the stack after the tail call.
724 res = c1->stack_usage >= c2->stack_usage;
725 callee_ret = callee_sig->ret;
726 if (callee_ret && MONO_TYPE_ISSTRUCT (callee_ret) && c2->ret.storage != ArgValuetypeInReg)
727 /* An address on the callee's stack is passed as the first argument */
737 * Initialize the cpu to execute managed code.
740 mono_arch_cpu_init (void)
742 /* spec compliance requires running with double precision */
746 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
747 fpcw &= ~X86_FPCW_PRECC_MASK;
748 fpcw |= X86_FPCW_PREC_DOUBLE;
749 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
750 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
752 _control87 (_PC_53, MCW_PC);
757 * Initialize architecture specific code.
760 mono_arch_init (void)
762 InitializeCriticalSection (&mini_arch_mutex);
764 ss_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ);
765 bp_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_32BIT);
766 mono_mprotect (bp_trigger_page, mono_pagesize (), 0);
768 mono_aot_register_jit_icall ("mono_x86_throw_exception", mono_x86_throw_exception);
769 mono_aot_register_jit_icall ("mono_x86_throw_corlib_exception", mono_x86_throw_corlib_exception);
770 #if defined(MONOTOUCH) || defined(MONO_EXTENSIONS)
771 mono_aot_register_jit_icall ("mono_x86_start_gsharedvt_call", mono_x86_start_gsharedvt_call);
776 * Cleanup architecture specific code.
779 mono_arch_cleanup (void)
782 mono_vfree (ss_trigger_page, mono_pagesize ());
784 mono_vfree (bp_trigger_page, mono_pagesize ());
785 DeleteCriticalSection (&mini_arch_mutex);
789 * This function returns the optimizations supported on this cpu.
792 mono_arch_cpu_optimizations (guint32 *exclude_mask)
794 #if !defined(__native_client__)
799 if (mono_hwcap_x86_has_cmov) {
800 opts |= MONO_OPT_CMOV;
802 if (mono_hwcap_x86_has_fcmov)
803 opts |= MONO_OPT_FCMOV;
805 *exclude_mask |= MONO_OPT_FCMOV;
807 *exclude_mask |= MONO_OPT_CMOV;
810 if (mono_hwcap_x86_has_sse2)
811 opts |= MONO_OPT_SSE2;
813 *exclude_mask |= MONO_OPT_SSE2;
815 #ifdef MONO_ARCH_SIMD_INTRINSICS
816 /*SIMD intrinsics require at least SSE2.*/
817 if (!mono_hwcap_x86_has_sse2)
818 *exclude_mask |= MONO_OPT_SIMD;
823 return MONO_OPT_CMOV | MONO_OPT_FCMOV | MONO_OPT_SSE2;
828 * This function test for all SSE functions supported.
830 * Returns a bitmask corresponding to all supported versions.
834 mono_arch_cpu_enumerate_simd_versions (void)
836 guint32 sse_opts = 0;
838 if (mono_hwcap_x86_has_sse1)
839 sse_opts |= SIMD_VERSION_SSE1;
841 if (mono_hwcap_x86_has_sse2)
842 sse_opts |= SIMD_VERSION_SSE2;
844 if (mono_hwcap_x86_has_sse3)
845 sse_opts |= SIMD_VERSION_SSE3;
847 if (mono_hwcap_x86_has_ssse3)
848 sse_opts |= SIMD_VERSION_SSSE3;
850 if (mono_hwcap_x86_has_sse41)
851 sse_opts |= SIMD_VERSION_SSE41;
853 if (mono_hwcap_x86_has_sse42)
854 sse_opts |= SIMD_VERSION_SSE42;
856 if (mono_hwcap_x86_has_sse4a)
857 sse_opts |= SIMD_VERSION_SSE4a;
863 * Determine whenever the trap whose info is in SIGINFO is caused by
867 mono_arch_is_int_overflow (void *sigctx, void *info)
872 mono_arch_sigctx_to_monoctx (sigctx, &ctx);
874 ip = (guint8*)ctx.eip;
876 if ((ip [0] == 0xf7) && (x86_modrm_mod (ip [1]) == 0x3) && (x86_modrm_reg (ip [1]) == 0x7)) {
880 switch (x86_modrm_rm (ip [1])) {
900 g_assert_not_reached ();
912 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
917 for (i = 0; i < cfg->num_varinfo; i++) {
918 MonoInst *ins = cfg->varinfo [i];
919 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
922 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
925 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
926 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
929 /* we dont allocate I1 to registers because there is no simply way to sign extend
930 * 8bit quantities in caller saved registers on x86 */
931 if (mono_is_regsize_var (ins->inst_vtype) && (ins->inst_vtype->type != MONO_TYPE_I1)) {
932 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
933 g_assert (i == vmv->idx);
934 vars = g_list_prepend (vars, vmv);
938 vars = mono_varlist_sort (cfg, vars, 0);
944 mono_arch_get_global_int_regs (MonoCompile *cfg)
948 /* we can use 3 registers for global allocation */
949 regs = g_list_prepend (regs, (gpointer)X86_EBX);
950 regs = g_list_prepend (regs, (gpointer)X86_ESI);
951 regs = g_list_prepend (regs, (gpointer)X86_EDI);
957 * mono_arch_regalloc_cost:
959 * Return the cost, in number of memory references, of the action of
960 * allocating the variable VMV into a register during global register
964 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
966 MonoInst *ins = cfg->varinfo [vmv->idx];
968 if (cfg->method->save_lmf)
969 /* The register is already saved */
970 return (ins->opcode == OP_ARG) ? 1 : 0;
972 /* push+pop+possible load if it is an argument */
973 return (ins->opcode == OP_ARG) ? 3 : 2;
977 set_needs_stack_frame (MonoCompile *cfg, gboolean flag)
979 static int inited = FALSE;
980 static int count = 0;
982 if (cfg->arch.need_stack_frame_inited) {
983 g_assert (cfg->arch.need_stack_frame == flag);
987 cfg->arch.need_stack_frame = flag;
988 cfg->arch.need_stack_frame_inited = TRUE;
994 mono_counters_register ("Could eliminate stack frame", MONO_COUNTER_INT|MONO_COUNTER_JIT, &count);
999 //g_print ("will eliminate %s.%s.%s\n", cfg->method->klass->name_space, cfg->method->klass->name, cfg->method->name);
1003 needs_stack_frame (MonoCompile *cfg)
1005 MonoMethodSignature *sig;
1006 MonoMethodHeader *header;
1007 gboolean result = FALSE;
1009 #if defined(__APPLE__)
1010 /*OSX requires stack frame code to have the correct alignment. */
1014 if (cfg->arch.need_stack_frame_inited)
1015 return cfg->arch.need_stack_frame;
1017 header = cfg->header;
1018 sig = mono_method_signature (cfg->method);
1020 if (cfg->disable_omit_fp)
1022 else if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1024 else if (cfg->method->save_lmf)
1026 else if (cfg->stack_offset)
1028 else if (cfg->param_area)
1030 else if (cfg->flags & (MONO_CFG_HAS_CALLS | MONO_CFG_HAS_ALLOCA | MONO_CFG_HAS_TAIL))
1032 else if (header->num_clauses)
1034 else if (sig->param_count + sig->hasthis)
1036 else if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1038 else if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) ||
1039 (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
1042 set_needs_stack_frame (cfg, result);
1044 return cfg->arch.need_stack_frame;
1048 * Set var information according to the calling convention. X86 version.
1049 * The locals var stuff should most likely be split in another method.
1052 mono_arch_allocate_vars (MonoCompile *cfg)
1054 MonoMethodSignature *sig;
1055 MonoMethodHeader *header;
1057 guint32 locals_stack_size, locals_stack_align;
1062 header = cfg->header;
1063 sig = mono_method_signature (cfg->method);
1065 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1067 cfg->frame_reg = X86_EBP;
1070 /* Reserve space to save LMF and caller saved registers */
1072 if (cfg->method->save_lmf) {
1073 /* The LMF var is allocated normally */
1075 if (cfg->used_int_regs & (1 << X86_EBX)) {
1079 if (cfg->used_int_regs & (1 << X86_EDI)) {
1083 if (cfg->used_int_regs & (1 << X86_ESI)) {
1088 switch (cinfo->ret.storage) {
1089 case ArgValuetypeInReg:
1090 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
1092 cfg->ret->opcode = OP_REGOFFSET;
1093 cfg->ret->inst_basereg = X86_EBP;
1094 cfg->ret->inst_offset = - offset;
1100 /* Allocate locals */
1101 offsets = mono_allocate_stack_slots (cfg, TRUE, &locals_stack_size, &locals_stack_align);
1102 if (locals_stack_size > MONO_ARCH_MAX_FRAME_SIZE) {
1103 char *mname = mono_method_full_name (cfg->method, TRUE);
1104 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
1105 cfg->exception_message = g_strdup_printf ("Method %s stack is too big.", mname);
1109 if (locals_stack_align) {
1110 int prev_offset = offset;
1112 offset += (locals_stack_align - 1);
1113 offset &= ~(locals_stack_align - 1);
1115 while (prev_offset < offset) {
1117 mini_gc_set_slot_type_from_fp (cfg, - prev_offset, SLOT_NOREF);
1120 cfg->locals_min_stack_offset = - (offset + locals_stack_size);
1121 cfg->locals_max_stack_offset = - offset;
1123 * EBP is at alignment 8 % MONO_ARCH_FRAME_ALIGNMENT, so if we
1124 * have locals larger than 8 bytes we need to make sure that
1125 * they have the appropriate offset.
1127 if (MONO_ARCH_FRAME_ALIGNMENT > 8 && locals_stack_align > 8)
1128 offset += MONO_ARCH_FRAME_ALIGNMENT - sizeof (gpointer) * 2;
1129 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1130 if (offsets [i] != -1) {
1131 MonoInst *inst = cfg->varinfo [i];
1132 inst->opcode = OP_REGOFFSET;
1133 inst->inst_basereg = X86_EBP;
1134 inst->inst_offset = - (offset + offsets [i]);
1135 //printf ("allocated local %d to ", i); mono_print_tree_nl (inst);
1138 offset += locals_stack_size;
1142 * Allocate arguments+return value
1145 switch (cinfo->ret.storage) {
1147 if (cfg->vret_addr) {
1149 * In the new IR, the cfg->vret_addr variable represents the
1150 * vtype return value.
1152 cfg->vret_addr->opcode = OP_REGOFFSET;
1153 cfg->vret_addr->inst_basereg = cfg->frame_reg;
1154 cfg->vret_addr->inst_offset = cinfo->ret.offset + ARGS_OFFSET;
1155 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1156 printf ("vret_addr =");
1157 mono_print_ins (cfg->vret_addr);
1160 cfg->ret->opcode = OP_REGOFFSET;
1161 cfg->ret->inst_basereg = X86_EBP;
1162 cfg->ret->inst_offset = cinfo->ret.offset + ARGS_OFFSET;
1165 case ArgValuetypeInReg:
1168 cfg->ret->opcode = OP_REGVAR;
1169 cfg->ret->inst_c0 = cinfo->ret.reg;
1170 cfg->ret->dreg = cinfo->ret.reg;
1173 case ArgOnFloatFpStack:
1174 case ArgOnDoubleFpStack:
1177 g_assert_not_reached ();
1180 if (sig->call_convention == MONO_CALL_VARARG) {
1181 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
1182 cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
1185 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1186 ArgInfo *ainfo = &cinfo->args [i];
1187 inst = cfg->args [i];
1188 if (inst->opcode != OP_REGVAR) {
1189 inst->opcode = OP_REGOFFSET;
1190 inst->inst_basereg = X86_EBP;
1192 inst->inst_offset = ainfo->offset + ARGS_OFFSET;
1195 cfg->stack_offset = offset;
1199 mono_arch_create_vars (MonoCompile *cfg)
1202 MonoMethodSignature *sig;
1205 sig = mono_method_signature (cfg->method);
1207 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1210 if (cinfo->ret.storage == ArgValuetypeInReg)
1211 cfg->ret_var_is_local = TRUE;
1212 if ((cinfo->ret.storage != ArgValuetypeInReg) && (MONO_TYPE_ISSTRUCT (sig_ret) || mini_is_gsharedvt_variable_type (cfg, sig_ret))) {
1213 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
1216 if (cfg->method->save_lmf)
1217 cfg->create_lmf_var = TRUE;
1219 cfg->arch_eh_jit_info = 1;
1223 * It is expensive to adjust esp for each individual fp argument pushed on the stack
1224 * so we try to do it just once when we have multiple fp arguments in a row.
1225 * We don't use this mechanism generally because for int arguments the generated code
1226 * is slightly bigger and new generation cpus optimize away the dependency chains
1227 * created by push instructions on the esp value.
1228 * fp_arg_setup is the first argument in the execution sequence where the esp register
1231 static G_GNUC_UNUSED int
1232 collect_fp_stack_space (MonoMethodSignature *sig, int start_arg, int *fp_arg_setup)
1237 for (; start_arg < sig->param_count; ++start_arg) {
1238 t = mini_type_get_underlying_type (NULL, sig->params [start_arg]);
1239 if (!t->byref && t->type == MONO_TYPE_R8) {
1240 fp_space += sizeof (double);
1241 *fp_arg_setup = start_arg;
1250 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
1252 MonoMethodSignature *tmp_sig;
1256 * mono_ArgIterator_Setup assumes the signature cookie is
1257 * passed first and all the arguments which were before it are
1258 * passed on the stack after the signature. So compensate by
1259 * passing a different signature.
1261 tmp_sig = mono_metadata_signature_dup (call->signature);
1262 tmp_sig->param_count -= call->signature->sentinelpos;
1263 tmp_sig->sentinelpos = 0;
1264 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
1266 if (cfg->compile_aot) {
1267 sig_reg = mono_alloc_ireg (cfg);
1268 MONO_EMIT_NEW_SIGNATURECONST (cfg, sig_reg, tmp_sig);
1269 MONO_EMIT_NEW_UNALU (cfg, OP_X86_PUSH, -1, sig_reg);
1271 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_X86_PUSH_IMM, -1, -1, tmp_sig);
1277 mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
1282 LLVMCallInfo *linfo;
1283 MonoType *t, *sig_ret;
1285 n = sig->param_count + sig->hasthis;
1287 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1290 linfo = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
1293 * LLVM always uses the native ABI while we use our own ABI, the
1294 * only difference is the handling of vtypes:
1295 * - we only pass/receive them in registers in some cases, and only
1296 * in 1 or 2 integer registers.
1298 if (cinfo->ret.storage == ArgValuetypeInReg) {
1300 cfg->exception_message = g_strdup ("pinvoke + vtypes");
1301 cfg->disable_llvm = TRUE;
1305 cfg->exception_message = g_strdup ("vtype ret in call");
1306 cfg->disable_llvm = TRUE;
1308 linfo->ret.storage = LLVMArgVtypeInReg;
1309 for (j = 0; j < 2; ++j)
1310 linfo->ret.pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, cinfo->ret.pair_storage [j]);
1314 if (mini_type_is_vtype (cfg, sig_ret) && cinfo->ret.storage == ArgInIReg) {
1315 /* Vtype returned using a hidden argument */
1316 linfo->ret.storage = LLVMArgVtypeRetAddr;
1317 linfo->vret_arg_index = cinfo->vret_arg_index;
1320 if (mini_type_is_vtype (cfg, sig_ret) && cinfo->ret.storage != ArgInIReg) {
1322 cfg->exception_message = g_strdup ("vtype ret in call");
1323 cfg->disable_llvm = TRUE;
1326 for (i = 0; i < n; ++i) {
1327 ainfo = cinfo->args + i;
1329 if (i >= sig->hasthis)
1330 t = sig->params [i - sig->hasthis];
1332 t = &mono_defaults.int_class->byval_arg;
1334 linfo->args [i].storage = LLVMArgNone;
1336 switch (ainfo->storage) {
1338 linfo->args [i].storage = LLVMArgInIReg;
1340 case ArgInDoubleSSEReg:
1341 case ArgInFloatSSEReg:
1342 linfo->args [i].storage = LLVMArgInFPReg;
1345 if (mini_type_is_vtype (cfg, t)) {
1346 if (mono_class_value_size (mono_class_from_mono_type (t), NULL) == 0)
1347 /* LLVM seems to allocate argument space for empty structures too */
1348 linfo->args [i].storage = LLVMArgNone;
1350 linfo->args [i].storage = LLVMArgVtypeByVal;
1352 linfo->args [i].storage = LLVMArgInIReg;
1354 if (t->type == MONO_TYPE_R4)
1355 linfo->args [i].storage = LLVMArgInFPReg;
1356 else if (t->type == MONO_TYPE_R8)
1357 linfo->args [i].storage = LLVMArgInFPReg;
1361 case ArgValuetypeInReg:
1363 cfg->exception_message = g_strdup ("pinvoke + vtypes");
1364 cfg->disable_llvm = TRUE;
1368 cfg->exception_message = g_strdup ("vtype arg");
1369 cfg->disable_llvm = TRUE;
1371 linfo->args [i].storage = LLVMArgVtypeInReg;
1372 for (j = 0; j < 2; ++j)
1373 linfo->args [i].pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, ainfo->pair_storage [j]);
1377 linfo->args [i].storage = LLVMArgGSharedVt;
1380 cfg->exception_message = g_strdup ("ainfo->storage");
1381 cfg->disable_llvm = TRUE;
1391 emit_gc_param_slot_def (MonoCompile *cfg, int sp_offset, MonoType *t)
1393 if (cfg->compute_gc_maps) {
1396 /* On x86, the offsets are from the sp value before the start of the call sequence */
1398 t = &mono_defaults.int_class->byval_arg;
1399 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, sp_offset, t);
1404 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
1408 MonoMethodSignature *sig;
1411 int sentinelpos = 0, sp_offset = 0;
1413 sig = call->signature;
1414 n = sig->param_count + sig->hasthis;
1417 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1419 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1420 sentinelpos = sig->sentinelpos + (sig->hasthis ? 1 : 0);
1422 if (cinfo->need_stack_align) {
1423 MONO_INST_NEW (cfg, arg, OP_SUB_IMM);
1424 arg->dreg = X86_ESP;
1425 arg->sreg1 = X86_ESP;
1426 arg->inst_imm = cinfo->stack_align_amount;
1427 MONO_ADD_INS (cfg->cbb, arg);
1428 for (i = 0; i < cinfo->stack_align_amount; i += sizeof (mgreg_t)) {
1431 emit_gc_param_slot_def (cfg, sp_offset, NULL);
1435 if (sig_ret && MONO_TYPE_ISSTRUCT (sig_ret)) {
1436 if (cinfo->ret.storage == ArgValuetypeInReg) {
1438 * Tell the JIT to use a more efficient calling convention: call using
1439 * OP_CALL, compute the result location after the call, and save the
1442 call->vret_in_reg = TRUE;
1444 NULLIFY_INS (call->vret_var);
1448 // FIXME: Emit EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF everywhere
1450 /* Handle the case where there are no implicit arguments */
1451 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sentinelpos)) {
1452 emit_sig_cookie (cfg, call, cinfo);
1454 emit_gc_param_slot_def (cfg, sp_offset, NULL);
1457 /* Arguments are pushed in the reverse order */
1458 for (i = n - 1; i >= 0; i --) {
1459 ArgInfo *ainfo = cinfo->args + i;
1460 MonoType *orig_type, *t;
1463 if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 1 && i == 0) {
1464 /* Push the vret arg before the first argument */
1466 MONO_INST_NEW (cfg, vtarg, OP_X86_PUSH);
1467 vtarg->type = STACK_MP;
1468 vtarg->sreg1 = call->vret_var->dreg;
1469 MONO_ADD_INS (cfg->cbb, vtarg);
1471 emit_gc_param_slot_def (cfg, sp_offset, NULL);
1474 if (i >= sig->hasthis)
1475 t = sig->params [i - sig->hasthis];
1477 t = &mono_defaults.int_class->byval_arg;
1479 t = mini_type_get_underlying_type (cfg->generic_sharing_context, t);
1481 MONO_INST_NEW (cfg, arg, OP_X86_PUSH);
1483 in = call->args [i];
1484 arg->cil_code = in->cil_code;
1485 arg->sreg1 = in->dreg;
1486 arg->type = in->type;
1488 g_assert (in->dreg != -1);
1490 if (ainfo->storage == ArgGSharedVt) {
1491 arg->opcode = OP_OUTARG_VT;
1492 arg->sreg1 = in->dreg;
1493 arg->klass = in->klass;
1494 arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
1495 memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
1497 MONO_ADD_INS (cfg->cbb, arg);
1498 } else if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(t))) {
1502 g_assert (in->klass);
1504 if (t->type == MONO_TYPE_TYPEDBYREF) {
1505 size = sizeof (MonoTypedRef);
1506 align = sizeof (gpointer);
1509 size = mini_type_stack_size_full (cfg->generic_sharing_context, &in->klass->byval_arg, &align, sig->pinvoke);
1513 arg->opcode = OP_OUTARG_VT;
1514 arg->sreg1 = in->dreg;
1515 arg->klass = in->klass;
1516 arg->backend.size = size;
1517 arg->inst_p0 = call;
1518 arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
1519 memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
1521 MONO_ADD_INS (cfg->cbb, arg);
1522 if (ainfo->storage != ArgValuetypeInReg) {
1524 emit_gc_param_slot_def (cfg, sp_offset, orig_type);
1530 switch (ainfo->storage) {
1532 arg->opcode = OP_X86_PUSH;
1534 if (t->type == MONO_TYPE_R4) {
1535 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, 4);
1536 arg->opcode = OP_STORER4_MEMBASE_REG;
1537 arg->inst_destbasereg = X86_ESP;
1538 arg->inst_offset = 0;
1540 } else if (t->type == MONO_TYPE_R8) {
1541 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, 8);
1542 arg->opcode = OP_STORER8_MEMBASE_REG;
1543 arg->inst_destbasereg = X86_ESP;
1544 arg->inst_offset = 0;
1546 } else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_U8) {
1548 MONO_EMIT_NEW_UNALU (cfg, OP_X86_PUSH, -1, in->dreg + 2);
1554 arg->opcode = OP_MOVE;
1555 arg->dreg = ainfo->reg;
1559 g_assert_not_reached ();
1562 MONO_ADD_INS (cfg->cbb, arg);
1564 sp_offset += argsize;
1566 if (cfg->compute_gc_maps) {
1568 /* FIXME: The == STACK_OBJ check might be fragile ? */
1569 if (sig->hasthis && i == 0 && call->args [i]->type == STACK_OBJ) {
1571 if (call->need_unbox_trampoline)
1572 /* The unbox trampoline transforms this into a managed pointer */
1573 emit_gc_param_slot_def (cfg, sp_offset, &mono_defaults.int_class->this_arg);
1575 emit_gc_param_slot_def (cfg, sp_offset, &mono_defaults.object_class->byval_arg);
1577 emit_gc_param_slot_def (cfg, sp_offset, orig_type);
1581 for (j = 0; j < argsize; j += 4)
1582 emit_gc_param_slot_def (cfg, sp_offset - j, NULL);
1587 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sentinelpos)) {
1588 /* Emit the signature cookie just before the implicit arguments */
1589 emit_sig_cookie (cfg, call, cinfo);
1591 emit_gc_param_slot_def (cfg, sp_offset, NULL);
1595 if (sig_ret && (MONO_TYPE_ISSTRUCT (sig_ret) || cinfo->vtype_retaddr)) {
1598 if (cinfo->ret.storage == ArgValuetypeInReg) {
1601 else if (cinfo->ret.storage == ArgInIReg) {
1603 /* The return address is passed in a register */
1604 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
1605 vtarg->sreg1 = call->inst.dreg;
1606 vtarg->dreg = mono_alloc_ireg (cfg);
1607 MONO_ADD_INS (cfg->cbb, vtarg);
1609 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
1610 } else if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 0) {
1612 MONO_INST_NEW (cfg, vtarg, OP_X86_PUSH);
1613 vtarg->type = STACK_MP;
1614 vtarg->sreg1 = call->vret_var->dreg;
1615 MONO_ADD_INS (cfg->cbb, vtarg);
1617 emit_gc_param_slot_def (cfg, sp_offset, NULL);
1620 /* if the function returns a struct on stack, the called method already does a ret $0x4 */
1621 if (cinfo->ret.storage != ArgValuetypeInReg)
1622 cinfo->stack_usage -= 4;
1625 call->stack_usage = cinfo->stack_usage;
1626 call->stack_align_amount = cinfo->stack_align_amount;
1627 cfg->arch.param_area_size = MAX (cfg->arch.param_area_size, sp_offset);
1631 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
1633 MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
1634 ArgInfo *ainfo = ins->inst_p1;
1636 int size = ins->backend.size;
1638 if (ainfo->storage == ArgValuetypeInReg) {
1639 int dreg = mono_alloc_ireg (cfg);
1642 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, dreg, src->dreg, 0);
1645 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, dreg, src->dreg, 0);
1648 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, src->dreg, 0);
1652 g_assert_not_reached ();
1654 mono_call_inst_add_outarg_reg (cfg, call, dreg, ainfo->reg, FALSE);
1657 if (cfg->gsharedvt && mini_is_gsharedvt_klass (cfg, ins->klass)) {
1659 MONO_INST_NEW (cfg, arg, OP_X86_PUSH);
1660 arg->sreg1 = src->dreg;
1661 MONO_ADD_INS (cfg->cbb, arg);
1662 } else if (size <= 4) {
1663 MONO_INST_NEW (cfg, arg, OP_X86_PUSH_MEMBASE);
1664 arg->sreg1 = src->dreg;
1666 MONO_ADD_INS (cfg->cbb, arg);
1667 } else if (size <= 20) {
1668 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, ALIGN_TO (size, 4));
1669 mini_emit_memcpy (cfg, X86_ESP, 0, src->dreg, 0, size, 4);
1671 MONO_INST_NEW (cfg, arg, OP_X86_PUSH_OBJ);
1672 arg->inst_basereg = src->dreg;
1673 arg->inst_offset = 0;
1674 arg->inst_imm = size;
1676 MONO_ADD_INS (cfg->cbb, arg);
1682 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
1684 MonoType *ret = mini_type_get_underlying_type (cfg->generic_sharing_context, mono_method_signature (method)->ret);
1687 if (ret->type == MONO_TYPE_R4) {
1688 if (COMPILE_LLVM (cfg))
1689 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
1692 } else if (ret->type == MONO_TYPE_R8) {
1693 if (COMPILE_LLVM (cfg))
1694 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
1697 } else if (ret->type == MONO_TYPE_I8 || ret->type == MONO_TYPE_U8) {
1698 if (COMPILE_LLVM (cfg))
1699 MONO_EMIT_NEW_UNALU (cfg, OP_LMOVE, cfg->ret->dreg, val->dreg);
1701 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, X86_EAX, val->dreg + 1);
1702 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, X86_EDX, val->dreg + 2);
1708 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
1712 * Allow tracing to work with this interface (with an optional argument)
1715 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1719 g_assert (MONO_ARCH_FRAME_ALIGNMENT >= 8);
1720 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 8);
1722 /* if some args are passed in registers, we need to save them here */
1723 x86_push_reg (code, X86_EBP);
1725 if (cfg->compile_aot) {
1726 x86_push_imm (code, cfg->method);
1727 x86_mov_reg_imm (code, X86_EAX, func);
1728 x86_call_reg (code, X86_EAX);
1730 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
1731 x86_push_imm (code, cfg->method);
1732 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1733 x86_call_code (code, 0);
1735 x86_alu_reg_imm (code, X86_ADD, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT);
1749 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
1752 int arg_size = 0, stack_usage = 0, save_mode = SAVE_NONE;
1753 MonoMethod *method = cfg->method;
1754 MonoType *ret_type = mini_type_get_underlying_type (cfg->generic_sharing_context, mono_method_signature (method)->ret);
1756 switch (ret_type->type) {
1757 case MONO_TYPE_VOID:
1758 /* special case string .ctor icall */
1759 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class) {
1760 save_mode = SAVE_EAX;
1761 stack_usage = enable_arguments ? 8 : 4;
1763 save_mode = SAVE_NONE;
1767 save_mode = SAVE_EAX_EDX;
1768 stack_usage = enable_arguments ? 16 : 8;
1772 save_mode = SAVE_FP;
1773 stack_usage = enable_arguments ? 16 : 8;
1775 case MONO_TYPE_GENERICINST:
1776 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
1777 save_mode = SAVE_EAX;
1778 stack_usage = enable_arguments ? 8 : 4;
1782 case MONO_TYPE_VALUETYPE:
1783 // FIXME: Handle SMALL_STRUCT_IN_REG here for proper alignment on darwin-x86
1784 save_mode = SAVE_STRUCT;
1785 stack_usage = enable_arguments ? 4 : 0;
1788 save_mode = SAVE_EAX;
1789 stack_usage = enable_arguments ? 8 : 4;
1793 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - stack_usage - 4);
1795 switch (save_mode) {
1797 x86_push_reg (code, X86_EDX);
1798 x86_push_reg (code, X86_EAX);
1799 if (enable_arguments) {
1800 x86_push_reg (code, X86_EDX);
1801 x86_push_reg (code, X86_EAX);
1806 x86_push_reg (code, X86_EAX);
1807 if (enable_arguments) {
1808 x86_push_reg (code, X86_EAX);
1813 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1814 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1815 if (enable_arguments) {
1816 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1817 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1822 if (enable_arguments) {
1823 x86_push_membase (code, X86_EBP, 8);
1832 if (cfg->compile_aot) {
1833 x86_push_imm (code, method);
1834 x86_mov_reg_imm (code, X86_EAX, func);
1835 x86_call_reg (code, X86_EAX);
1837 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
1838 x86_push_imm (code, method);
1839 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1840 x86_call_code (code, 0);
1843 x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
1845 switch (save_mode) {
1847 x86_pop_reg (code, X86_EAX);
1848 x86_pop_reg (code, X86_EDX);
1851 x86_pop_reg (code, X86_EAX);
1854 x86_fld_membase (code, X86_ESP, 0, TRUE);
1855 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1862 x86_alu_reg_imm (code, X86_ADD, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - stack_usage);
1867 #define EMIT_COND_BRANCH(ins,cond,sign) \
1868 if (ins->inst_true_bb->native_offset) { \
1869 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
1871 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1872 if ((cfg->opt & MONO_OPT_BRANCH) && \
1873 x86_is_imm8 (ins->inst_true_bb->max_offset - cpos)) \
1874 x86_branch8 (code, cond, 0, sign); \
1876 x86_branch32 (code, cond, 0, sign); \
1880 * Emit an exception if condition is fail and
1881 * if possible do a directly branch to target
1883 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
1885 MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
1886 if (tins == NULL) { \
1887 mono_add_patch_info (cfg, code - cfg->native_code, \
1888 MONO_PATCH_INFO_EXC, exc_name); \
1889 x86_branch32 (code, cond, 0, signed); \
1891 EMIT_COND_BRANCH (tins, cond, signed); \
1895 #define EMIT_FPCOMPARE(code) do { \
1896 x86_fcompp (code); \
1897 x86_fnstsw (code); \
1902 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data)
1904 gboolean needs_paddings = TRUE;
1906 MonoJumpInfo *jinfo = NULL;
1908 if (cfg->abs_patches) {
1909 jinfo = g_hash_table_lookup (cfg->abs_patches, data);
1910 if (jinfo && jinfo->type == MONO_PATCH_INFO_JIT_ICALL_ADDR)
1911 needs_paddings = FALSE;
1914 if (cfg->compile_aot)
1915 needs_paddings = FALSE;
1916 /*The address must be 4 bytes aligned to avoid spanning multiple cache lines.
1917 This is required for code patching to be safe on SMP machines.
1919 pad_size = (guint32)(code + 1 - cfg->native_code) & 0x3;
1920 #ifndef __native_client_codegen__
1921 if (needs_paddings && pad_size)
1922 x86_padding (code, 4 - pad_size);
1925 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
1926 x86_call_code (code, 0);
1931 #define INST_IGNORES_CFLAGS(opcode) (!(((opcode) == OP_ADC) || ((opcode) == OP_IADC) || ((opcode) == OP_ADC_IMM) || ((opcode) == OP_IADC_IMM) || ((opcode) == OP_SBB) || ((opcode) == OP_ISBB) || ((opcode) == OP_SBB_IMM) || ((opcode) == OP_ISBB_IMM)))
1934 * mono_peephole_pass_1:
1936 * Perform peephole opts which should/can be performed before local regalloc
1939 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
1943 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
1944 MonoInst *last_ins = ins->prev;
1946 switch (ins->opcode) {
1949 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS)) {
1951 * X86_LEA is like ADD, but doesn't have the
1952 * sreg1==dreg restriction.
1954 ins->opcode = OP_X86_LEA_MEMBASE;
1955 ins->inst_basereg = ins->sreg1;
1956 } else if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1957 ins->opcode = OP_X86_INC_REG;
1961 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS)) {
1962 ins->opcode = OP_X86_LEA_MEMBASE;
1963 ins->inst_basereg = ins->sreg1;
1964 ins->inst_imm = -ins->inst_imm;
1965 } else if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1966 ins->opcode = OP_X86_DEC_REG;
1968 case OP_COMPARE_IMM:
1969 case OP_ICOMPARE_IMM:
1970 /* OP_COMPARE_IMM (reg, 0)
1972 * OP_X86_TEST_NULL (reg)
1975 ins->opcode = OP_X86_TEST_NULL;
1977 case OP_X86_COMPARE_MEMBASE_IMM:
1979 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1980 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
1982 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1983 * OP_COMPARE_IMM reg, imm
1985 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
1987 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
1988 ins->inst_basereg == last_ins->inst_destbasereg &&
1989 ins->inst_offset == last_ins->inst_offset) {
1990 ins->opcode = OP_COMPARE_IMM;
1991 ins->sreg1 = last_ins->sreg1;
1993 /* check if we can remove cmp reg,0 with test null */
1995 ins->opcode = OP_X86_TEST_NULL;
1999 case OP_X86_PUSH_MEMBASE:
2000 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG ||
2001 last_ins->opcode == OP_STORE_MEMBASE_REG) &&
2002 ins->inst_basereg == last_ins->inst_destbasereg &&
2003 ins->inst_offset == last_ins->inst_offset) {
2004 ins->opcode = OP_X86_PUSH;
2005 ins->sreg1 = last_ins->sreg1;
2010 mono_peephole_ins (bb, ins);
2015 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
2019 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
2020 switch (ins->opcode) {
2022 /* reg = 0 -> XOR (reg, reg) */
2023 /* XOR sets cflags on x86, so we cant do it always */
2024 if (ins->inst_c0 == 0 && (!ins->next || (ins->next && INST_IGNORES_CFLAGS (ins->next->opcode)))) {
2027 ins->opcode = OP_IXOR;
2028 ins->sreg1 = ins->dreg;
2029 ins->sreg2 = ins->dreg;
2032 * Convert succeeding STORE_MEMBASE_IMM 0 ins to STORE_MEMBASE_REG
2033 * since it takes 3 bytes instead of 7.
2035 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
2036 if ((ins2->opcode == OP_STORE_MEMBASE_IMM) && (ins2->inst_imm == 0)) {
2037 ins2->opcode = OP_STORE_MEMBASE_REG;
2038 ins2->sreg1 = ins->dreg;
2040 else if ((ins2->opcode == OP_STOREI4_MEMBASE_IMM) && (ins2->inst_imm == 0)) {
2041 ins2->opcode = OP_STOREI4_MEMBASE_REG;
2042 ins2->sreg1 = ins->dreg;
2044 else if ((ins2->opcode == OP_STOREI1_MEMBASE_IMM) || (ins2->opcode == OP_STOREI2_MEMBASE_IMM)) {
2045 /* Continue iteration */
2054 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
2055 ins->opcode = OP_X86_INC_REG;
2059 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
2060 ins->opcode = OP_X86_DEC_REG;
2064 mono_peephole_ins (bb, ins);
2069 * mono_arch_lowering_pass:
2071 * Converts complex opcodes into simpler ones so that each IR instruction
2072 * corresponds to one machine instruction.
2075 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
2077 MonoInst *ins, *next;
2080 * FIXME: Need to add more instructions, but the current machine
2081 * description can't model some parts of the composite instructions like
2084 MONO_BB_FOR_EACH_INS_SAFE (bb, next, ins) {
2085 switch (ins->opcode) {
2088 case OP_IDIV_UN_IMM:
2089 case OP_IREM_UN_IMM:
2091 * Keep the cases where we could generated optimized code, otherwise convert
2092 * to the non-imm variant.
2094 if ((ins->opcode == OP_IREM_IMM) && mono_is_power_of_two (ins->inst_imm) >= 0)
2096 mono_decompose_op_imm (cfg, bb, ins);
2103 bb->max_vreg = cfg->next_vreg;
2107 branch_cc_table [] = {
2108 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
2109 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
2110 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
2113 /* Maps CMP_... constants to X86_CC_... constants */
2116 X86_CC_EQ, X86_CC_NE, X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT,
2117 X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT
2121 cc_signed_table [] = {
2122 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
2123 FALSE, FALSE, FALSE, FALSE
2126 static unsigned char*
2127 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
2129 #define XMM_TEMP_REG 0
2130 /*This SSE2 optimization must not be done which OPT_SIMD in place as it clobbers xmm0.*/
2131 /*The xmm pass decomposes OP_FCONV_ ops anyway anyway.*/
2132 if (cfg->opt & MONO_OPT_SSE2 && size < 8 && !(cfg->opt & MONO_OPT_SIMD)) {
2133 /* optimize by assigning a local var for this use so we avoid
2134 * the stack manipulations */
2135 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2136 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
2137 x86_movsd_reg_membase (code, XMM_TEMP_REG, X86_ESP, 0);
2138 x86_cvttsd2si (code, dreg, XMM_TEMP_REG);
2139 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
2141 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
2143 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
2146 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
2147 x86_fnstcw_membase(code, X86_ESP, 0);
2148 x86_mov_reg_membase (code, dreg, X86_ESP, 0, 2);
2149 x86_alu_reg_imm (code, X86_OR, dreg, 0xc00);
2150 x86_mov_membase_reg (code, X86_ESP, 2, dreg, 2);
2151 x86_fldcw_membase (code, X86_ESP, 2);
2153 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2154 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
2155 x86_pop_reg (code, dreg);
2156 /* FIXME: need the high register
2157 * x86_pop_reg (code, dreg_high);
2160 x86_push_reg (code, X86_EAX); // SP = SP - 4
2161 x86_fist_pop_membase (code, X86_ESP, 0, FALSE);
2162 x86_pop_reg (code, dreg);
2164 x86_fldcw_membase (code, X86_ESP, 0);
2165 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2168 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
2170 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
2174 static unsigned char*
2175 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
2177 int sreg = tree->sreg1;
2178 int need_touch = FALSE;
2180 #if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
2189 * If requested stack size is larger than one page,
2190 * perform stack-touch operation
2193 * Generate stack probe code.
2194 * Under Windows, it is necessary to allocate one page at a time,
2195 * "touching" stack after each successful sub-allocation. This is
2196 * because of the way stack growth is implemented - there is a
2197 * guard page before the lowest stack page that is currently commited.
2198 * Stack normally grows sequentially so OS traps access to the
2199 * guard page and commits more pages when needed.
2201 x86_test_reg_imm (code, sreg, ~0xFFF);
2202 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2204 br[2] = code; /* loop */
2205 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
2206 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
2209 * By the end of the loop, sreg2 is smaller than 0x1000, so the init routine
2210 * that follows only initializes the last part of the area.
2212 /* Same as the init code below with size==0x1000 */
2213 if (tree->flags & MONO_INST_INIT) {
2214 x86_push_reg (code, X86_EAX);
2215 x86_push_reg (code, X86_ECX);
2216 x86_push_reg (code, X86_EDI);
2217 x86_mov_reg_imm (code, X86_ECX, (0x1000 >> 2));
2218 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2219 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
2221 x86_prefix (code, X86_REP_PREFIX);
2223 x86_pop_reg (code, X86_EDI);
2224 x86_pop_reg (code, X86_ECX);
2225 x86_pop_reg (code, X86_EAX);
2228 x86_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
2229 x86_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
2230 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
2231 x86_patch (br[3], br[2]);
2232 x86_test_reg_reg (code, sreg, sreg);
2233 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2234 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
2236 br[1] = code; x86_jump8 (code, 0);
2238 x86_patch (br[0], code);
2239 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
2240 x86_patch (br[1], code);
2241 x86_patch (br[4], code);
2244 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
2246 if (tree->flags & MONO_INST_INIT) {
2248 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
2249 x86_push_reg (code, X86_EAX);
2252 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
2253 x86_push_reg (code, X86_ECX);
2256 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
2257 x86_push_reg (code, X86_EDI);
2261 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
2262 if (sreg != X86_ECX)
2263 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
2264 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2266 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
2268 x86_prefix (code, X86_REP_PREFIX);
2271 if (tree->dreg != X86_EDI && sreg != X86_EDI)
2272 x86_pop_reg (code, X86_EDI);
2273 if (tree->dreg != X86_ECX && sreg != X86_ECX)
2274 x86_pop_reg (code, X86_ECX);
2275 if (tree->dreg != X86_EAX && sreg != X86_EAX)
2276 x86_pop_reg (code, X86_EAX);
2283 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
2285 /* Move return value to the target register */
2286 switch (ins->opcode) {
2289 case OP_CALL_MEMBASE:
2290 if (ins->dreg != X86_EAX)
2291 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
2301 static int tls_gs_offset;
2305 mono_x86_have_tls_get (void)
2308 static gboolean have_tls_get = FALSE;
2309 static gboolean inited = FALSE;
2313 return have_tls_get;
2315 ins = (guint32*)pthread_getspecific;
2317 * We're looking for these two instructions:
2319 * mov 0x4(%esp),%eax
2320 * mov %gs:[offset](,%eax,4),%eax
2322 have_tls_get = ins [0] == 0x0424448b && ins [1] == 0x85048b65;
2323 tls_gs_offset = ins [2];
2327 return have_tls_get;
2328 #elif defined(TARGET_ANDROID)
2336 mono_x86_emit_tls_set (guint8* code, int sreg, int tls_offset)
2338 #if defined(__APPLE__)
2339 x86_prefix (code, X86_GS_PREFIX);
2340 x86_mov_mem_reg (code, tls_gs_offset + (tls_offset * 4), sreg, 4);
2341 #elif defined(TARGET_WIN32)
2342 g_assert_not_reached ();
2344 x86_prefix (code, X86_GS_PREFIX);
2345 x86_mov_mem_reg (code, tls_offset, sreg, 4);
2351 * mono_x86_emit_tls_get:
2352 * @code: buffer to store code to
2353 * @dreg: hard register where to place the result
2354 * @tls_offset: offset info
2356 * mono_x86_emit_tls_get emits in @code the native code that puts in
2357 * the dreg register the item in the thread local storage identified
2360 * Returns: a pointer to the end of the stored code
2363 mono_x86_emit_tls_get (guint8* code, int dreg, int tls_offset)
2365 #if defined(__APPLE__)
2366 x86_prefix (code, X86_GS_PREFIX);
2367 x86_mov_reg_mem (code, dreg, tls_gs_offset + (tls_offset * 4), 4);
2368 #elif defined(TARGET_WIN32)
2370 * See the Under the Hood article in the May 1996 issue of Microsoft Systems
2371 * Journal and/or a disassembly of the TlsGet () function.
2373 g_assert (tls_offset < 64);
2374 x86_prefix (code, X86_FS_PREFIX);
2375 x86_mov_reg_mem (code, dreg, 0x18, 4);
2376 /* Dunno what this does but TlsGetValue () contains it */
2377 x86_alu_membase_imm (code, X86_AND, dreg, 0x34, 0);
2378 x86_mov_reg_membase (code, dreg, dreg, 3600 + (tls_offset * 4), 4);
2380 if (optimize_for_xen) {
2381 x86_prefix (code, X86_GS_PREFIX);
2382 x86_mov_reg_mem (code, dreg, 0, 4);
2383 x86_mov_reg_membase (code, dreg, dreg, tls_offset, 4);
2385 x86_prefix (code, X86_GS_PREFIX);
2386 x86_mov_reg_mem (code, dreg, tls_offset, 4);
2393 emit_tls_get_reg (guint8* code, int dreg, int offset_reg)
2395 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
2396 #if defined(__APPLE__) || defined(__linux__)
2397 if (dreg != offset_reg)
2398 x86_mov_reg_reg (code, dreg, offset_reg, sizeof (mgreg_t));
2399 x86_prefix (code, X86_GS_PREFIX);
2400 x86_mov_reg_membase (code, dreg, dreg, 0, sizeof (mgreg_t));
2402 g_assert_not_reached ();
2408 emit_tls_set_reg (guint8* code, int sreg, int offset_reg)
2410 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
2412 g_assert_not_reached ();
2413 #elif defined(__APPLE__) || defined(__linux__)
2414 x86_prefix (code, X86_GS_PREFIX);
2415 x86_mov_membase_reg (code, offset_reg, 0, sreg, sizeof (mgreg_t));
2417 g_assert_not_reached ();
2423 * mono_arch_translate_tls_offset:
2425 * Translate the TLS offset OFFSET computed by MONO_THREAD_VAR_OFFSET () into a format usable by OP_TLS_GET_REG/OP_TLS_SET_REG.
2428 mono_arch_translate_tls_offset (int offset)
2431 return tls_gs_offset + (offset * 4);
2440 * Emit code to initialize an LMF structure at LMF_OFFSET.
2443 emit_setup_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset, int cfa_offset)
2445 /* save all caller saved regs */
2446 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebx), X86_EBX, sizeof (mgreg_t));
2447 mono_emit_unwind_op_offset (cfg, code, X86_EBX, - cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebx));
2448 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, edi), X86_EDI, sizeof (mgreg_t));
2449 mono_emit_unwind_op_offset (cfg, code, X86_EDI, - cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, edi));
2450 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esi), X86_ESI, sizeof (mgreg_t));
2451 mono_emit_unwind_op_offset (cfg, code, X86_ESI, - cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, esi));
2452 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebp), X86_EBP, sizeof (mgreg_t));
2454 /* save the current IP */
2455 if (cfg->compile_aot) {
2456 /* This pushes the current ip */
2457 x86_call_imm (code, 0);
2458 x86_pop_reg (code, X86_EAX);
2460 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
2461 x86_mov_reg_imm (code, X86_EAX, 0);
2463 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, eip), X86_EAX, sizeof (mgreg_t));
2465 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, eip), SLOT_NOREF);
2466 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebp), SLOT_NOREF);
2467 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, esi), SLOT_NOREF);
2468 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, edi), SLOT_NOREF);
2469 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebx), SLOT_NOREF);
2470 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, esp), SLOT_NOREF);
2471 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), SLOT_NOREF);
2472 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), SLOT_NOREF);
2473 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), SLOT_NOREF);
2481 * Emit code to push an LMF structure on the LMF stack.
2484 emit_push_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset)
2486 if (!cfg->compile_aot && (lmf_tls_offset != -1) && !is_win32 && !optimize_for_xen) {
2488 * Optimized version which uses the mono_lmf TLS variable instead of indirection
2489 * through the mono_lmf_addr TLS variable.
2491 /* %eax = previous_lmf */
2492 code = mono_x86_emit_tls_get (code, X86_EAX, lmf_tls_offset);
2493 /* set previous_lmf */
2494 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), X86_EAX, sizeof (mgreg_t));
2495 x86_lea_membase (code, X86_EAX, cfg->frame_reg, lmf_offset);
2497 code = mono_x86_emit_tls_set (code, X86_EAX, lmf_tls_offset);
2499 /* get the address of lmf for the current thread */
2501 * This is performance critical so we try to use some tricks to make
2504 gboolean have_fastpath = FALSE;
2507 if (jit_tls_offset != -1) {
2508 code = mono_x86_emit_tls_get (code, X86_EAX, jit_tls_offset);
2509 x86_alu_reg_imm (code, X86_ADD, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
2510 have_fastpath = TRUE;
2513 if (!cfg->compile_aot && lmf_addr_tls_offset != -1) {
2514 code = mono_x86_emit_tls_get (code, X86_EAX, lmf_addr_tls_offset);
2515 have_fastpath = TRUE;
2518 if (!have_fastpath) {
2519 if (cfg->compile_aot)
2520 code = mono_arch_emit_load_got_addr (cfg->native_code, code, cfg, NULL);
2521 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_get_lmf_addr");
2525 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), X86_EAX, sizeof (mgreg_t));
2526 /* save previous_lmf */
2527 x86_mov_reg_membase (code, X86_ECX, X86_EAX, 0, sizeof (mgreg_t));
2528 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), X86_ECX, sizeof (mgreg_t));
2530 x86_lea_membase (code, X86_ECX, cfg->frame_reg, lmf_offset);
2531 x86_mov_membase_reg (code, X86_EAX, 0, X86_ECX, sizeof (mgreg_t));
2539 * Emit code to pop an LMF structure from the LMF stack.
2540 * Preserves the return registers.
2543 emit_pop_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset)
2545 MonoMethodSignature *sig = mono_method_signature (cfg->method);
2548 if (!cfg->compile_aot && (lmf_tls_offset != -1) && !is_win32 && !optimize_for_xen) {
2550 * Optimized version which uses the mono_lmf TLS variable instead of indirection
2551 * through the mono_lmf_addr TLS variable.
2553 /* reg = previous_lmf */
2554 x86_mov_reg_membase (code, X86_ECX, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 4);
2556 /* lmf = previous_lmf */
2557 code = mono_x86_emit_tls_set (code, X86_ECX, lmf_tls_offset);
2559 /* Find a spare register */
2560 switch (mini_type_get_underlying_type (cfg->generic_sharing_context, sig->ret)->type) {
2563 prev_lmf_reg = X86_EDI;
2564 cfg->used_int_regs |= (1 << X86_EDI);
2567 prev_lmf_reg = X86_EDX;
2571 /* reg = previous_lmf */
2572 x86_mov_reg_membase (code, prev_lmf_reg, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 4);
2575 x86_mov_reg_membase (code, X86_ECX, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), 4);
2577 /* *(lmf) = previous_lmf */
2578 x86_mov_membase_reg (code, X86_ECX, 0, prev_lmf_reg, 4);
2583 #define REAL_PRINT_REG(text,reg) \
2584 mono_assert (reg >= 0); \
2585 x86_push_reg (code, X86_EAX); \
2586 x86_push_reg (code, X86_EDX); \
2587 x86_push_reg (code, X86_ECX); \
2588 x86_push_reg (code, reg); \
2589 x86_push_imm (code, reg); \
2590 x86_push_imm (code, text " %d %p\n"); \
2591 x86_mov_reg_imm (code, X86_EAX, printf); \
2592 x86_call_reg (code, X86_EAX); \
2593 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 3*4); \
2594 x86_pop_reg (code, X86_ECX); \
2595 x86_pop_reg (code, X86_EDX); \
2596 x86_pop_reg (code, X86_EAX);
2598 /* REAL_PRINT_REG does not appear to be used, and was not adapted to work with Native Client. */
2599 #ifdef __native__client_codegen__
2600 #define REAL_PRINT_REG(text, reg) g_assert_not_reached()
2603 /* benchmark and set based on cpu */
2604 #define LOOP_ALIGNMENT 8
2605 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
2609 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2614 guint8 *code = cfg->native_code + cfg->code_len;
2617 if (cfg->opt & MONO_OPT_LOOP) {
2618 int pad, align = LOOP_ALIGNMENT;
2619 /* set alignment depending on cpu */
2620 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
2622 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
2623 x86_padding (code, pad);
2624 cfg->code_len += pad;
2625 bb->native_offset = cfg->code_len;
2628 #ifdef __native_client_codegen__
2630 /* For Native Client, all indirect call/jump targets must be */
2631 /* 32-byte aligned. Exception handler blocks are jumped to */
2632 /* indirectly as well. */
2633 gboolean bb_needs_alignment = (bb->flags & BB_INDIRECT_JUMP_TARGET) ||
2634 (bb->flags & BB_EXCEPTION_HANDLER);
2636 /* if ((cfg->code_len & kNaClAlignmentMask) != 0) { */
2637 if ( bb_needs_alignment && ((cfg->code_len & kNaClAlignmentMask) != 0)) {
2638 int pad = kNaClAlignment - (cfg->code_len & kNaClAlignmentMask);
2639 if (pad != kNaClAlignment) code = mono_arch_nacl_pad(code, pad);
2640 cfg->code_len += pad;
2641 bb->native_offset = cfg->code_len;
2644 #endif /* __native_client_codegen__ */
2645 if (cfg->verbose_level > 2)
2646 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2648 cpos = bb->max_offset;
2650 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2651 MonoProfileCoverageInfo *cov = cfg->coverage_info;
2652 g_assert (!cfg->compile_aot);
2655 cov->data [bb->dfn].cil_code = bb->cil_code;
2656 /* this is not thread save, but good enough */
2657 x86_inc_mem (code, &cov->data [bb->dfn].count);
2660 offset = code - cfg->native_code;
2662 mono_debug_open_block (cfg, bb, offset);
2664 if (mono_break_at_bb_method && mono_method_desc_full_match (mono_break_at_bb_method, cfg->method) && bb->block_num == mono_break_at_bb_bb_num)
2665 x86_breakpoint (code);
2667 MONO_BB_FOR_EACH_INS (bb, ins) {
2668 offset = code - cfg->native_code;
2670 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
2672 #define EXTRA_CODE_SPACE (NACL_SIZE (16, 16 + kNaClAlignment))
2674 if (G_UNLIKELY (offset > (cfg->code_size - max_len - EXTRA_CODE_SPACE))) {
2675 cfg->code_size *= 2;
2676 cfg->native_code = mono_realloc_native_code(cfg);
2677 code = cfg->native_code + offset;
2678 cfg->stat_code_reallocs++;
2681 if (cfg->debug_info)
2682 mono_debug_record_line_number (cfg, ins, offset);
2684 switch (ins->opcode) {
2686 x86_mul_reg (code, ins->sreg2, TRUE);
2689 x86_mul_reg (code, ins->sreg2, FALSE);
2691 case OP_X86_SETEQ_MEMBASE:
2692 case OP_X86_SETNE_MEMBASE:
2693 x86_set_membase (code, ins->opcode == OP_X86_SETEQ_MEMBASE ? X86_CC_EQ : X86_CC_NE,
2694 ins->inst_basereg, ins->inst_offset, TRUE);
2696 case OP_STOREI1_MEMBASE_IMM:
2697 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
2699 case OP_STOREI2_MEMBASE_IMM:
2700 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
2702 case OP_STORE_MEMBASE_IMM:
2703 case OP_STOREI4_MEMBASE_IMM:
2704 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
2706 case OP_STOREI1_MEMBASE_REG:
2707 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
2709 case OP_STOREI2_MEMBASE_REG:
2710 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
2712 case OP_STORE_MEMBASE_REG:
2713 case OP_STOREI4_MEMBASE_REG:
2714 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
2716 case OP_STORE_MEM_IMM:
2717 x86_mov_mem_imm (code, ins->inst_p0, ins->inst_c0, 4);
2720 x86_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
2724 /* These are created by the cprop pass so they use inst_imm as the source */
2725 x86_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
2728 x86_widen_mem (code, ins->dreg, ins->inst_imm, FALSE, FALSE);
2731 x86_widen_mem (code, ins->dreg, ins->inst_imm, FALSE, TRUE);
2733 case OP_LOAD_MEMBASE:
2734 case OP_LOADI4_MEMBASE:
2735 case OP_LOADU4_MEMBASE:
2736 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
2738 case OP_LOADU1_MEMBASE:
2739 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
2741 case OP_LOADI1_MEMBASE:
2742 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
2744 case OP_LOADU2_MEMBASE:
2745 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
2747 case OP_LOADI2_MEMBASE:
2748 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
2750 case OP_ICONV_TO_I1:
2752 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2754 case OP_ICONV_TO_I2:
2756 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2758 case OP_ICONV_TO_U1:
2759 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
2761 case OP_ICONV_TO_U2:
2762 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
2766 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
2768 case OP_COMPARE_IMM:
2769 case OP_ICOMPARE_IMM:
2770 x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
2772 case OP_X86_COMPARE_MEMBASE_REG:
2773 x86_alu_membase_reg (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2775 case OP_X86_COMPARE_MEMBASE_IMM:
2776 x86_alu_membase_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2778 case OP_X86_COMPARE_MEMBASE8_IMM:
2779 x86_alu_membase8_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2781 case OP_X86_COMPARE_REG_MEMBASE:
2782 x86_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
2784 case OP_X86_COMPARE_MEM_IMM:
2785 x86_alu_mem_imm (code, X86_CMP, ins->inst_offset, ins->inst_imm);
2787 case OP_X86_TEST_NULL:
2788 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2790 case OP_X86_ADD_MEMBASE_IMM:
2791 x86_alu_membase_imm (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2793 case OP_X86_ADD_REG_MEMBASE:
2794 x86_alu_reg_membase (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset);
2796 case OP_X86_SUB_MEMBASE_IMM:
2797 x86_alu_membase_imm (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2799 case OP_X86_SUB_REG_MEMBASE:
2800 x86_alu_reg_membase (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset);
2802 case OP_X86_AND_MEMBASE_IMM:
2803 x86_alu_membase_imm (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2805 case OP_X86_OR_MEMBASE_IMM:
2806 x86_alu_membase_imm (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2808 case OP_X86_XOR_MEMBASE_IMM:
2809 x86_alu_membase_imm (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2811 case OP_X86_ADD_MEMBASE_REG:
2812 x86_alu_membase_reg (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2814 case OP_X86_SUB_MEMBASE_REG:
2815 x86_alu_membase_reg (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2817 case OP_X86_AND_MEMBASE_REG:
2818 x86_alu_membase_reg (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2820 case OP_X86_OR_MEMBASE_REG:
2821 x86_alu_membase_reg (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2823 case OP_X86_XOR_MEMBASE_REG:
2824 x86_alu_membase_reg (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2826 case OP_X86_INC_MEMBASE:
2827 x86_inc_membase (code, ins->inst_basereg, ins->inst_offset);
2829 case OP_X86_INC_REG:
2830 x86_inc_reg (code, ins->dreg);
2832 case OP_X86_DEC_MEMBASE:
2833 x86_dec_membase (code, ins->inst_basereg, ins->inst_offset);
2835 case OP_X86_DEC_REG:
2836 x86_dec_reg (code, ins->dreg);
2838 case OP_X86_MUL_REG_MEMBASE:
2839 x86_imul_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
2841 case OP_X86_AND_REG_MEMBASE:
2842 x86_alu_reg_membase (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset);
2844 case OP_X86_OR_REG_MEMBASE:
2845 x86_alu_reg_membase (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset);
2847 case OP_X86_XOR_REG_MEMBASE:
2848 x86_alu_reg_membase (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset);
2851 x86_breakpoint (code);
2853 case OP_RELAXED_NOP:
2854 x86_prefix (code, X86_REP_PREFIX);
2862 case OP_DUMMY_STORE:
2863 case OP_NOT_REACHED:
2866 case OP_SEQ_POINT: {
2869 if (cfg->compile_aot)
2873 * Read from the single stepping trigger page. This will cause a
2874 * SIGSEGV when single stepping is enabled.
2875 * We do this _before_ the breakpoint, so single stepping after
2876 * a breakpoint is hit will step to the next IL offset.
2878 if (ins->flags & MONO_INST_SINGLE_STEP_LOC)
2879 x86_alu_reg_mem (code, X86_CMP, X86_EAX, (guint32)ss_trigger_page);
2881 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
2884 * A placeholder for a possible breakpoint inserted by
2885 * mono_arch_set_breakpoint ().
2887 for (i = 0; i < 6; ++i)
2890 * Add an additional nop so skipping the bp doesn't cause the ip to point
2891 * to another IL offset.
2899 x86_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
2903 x86_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
2908 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
2912 x86_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
2917 x86_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
2921 x86_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
2926 x86_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
2930 x86_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
2933 x86_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
2937 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
2941 #if defined( __native_client_codegen__ )
2942 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
2943 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
2946 * The code is the same for div/rem, the allocator will allocate dreg
2947 * to RAX/RDX as appropriate.
2949 if (ins->sreg2 == X86_EDX) {
2950 /* cdq clobbers this */
2951 x86_push_reg (code, ins->sreg2);
2953 x86_div_membase (code, X86_ESP, 0, TRUE);
2954 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2957 x86_div_reg (code, ins->sreg2, TRUE);
2962 #if defined( __native_client_codegen__ )
2963 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
2964 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
2966 if (ins->sreg2 == X86_EDX) {
2967 x86_push_reg (code, ins->sreg2);
2968 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2969 x86_div_membase (code, X86_ESP, 0, FALSE);
2970 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2972 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2973 x86_div_reg (code, ins->sreg2, FALSE);
2977 #if defined( __native_client_codegen__ )
2978 if (ins->inst_imm == 0) {
2979 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "DivideByZeroException");
2980 x86_jump32 (code, 0);
2984 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2986 x86_div_reg (code, ins->sreg2, TRUE);
2989 int power = mono_is_power_of_two (ins->inst_imm);
2991 g_assert (ins->sreg1 == X86_EAX);
2992 g_assert (ins->dreg == X86_EAX);
2993 g_assert (power >= 0);
2996 /* Based on http://compilers.iecc.com/comparch/article/93-04-079 */
2998 x86_alu_reg_imm (code, X86_AND, X86_EAX, 1);
3000 * If the divident is >= 0, this does not nothing. If it is positive, it
3001 * it transforms %eax=0 into %eax=0, and %eax=1 into %eax=-1.
3003 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EDX);
3004 x86_alu_reg_reg (code, X86_SUB, X86_EAX, X86_EDX);
3005 } else if (power == 0) {
3006 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3008 /* Based on gcc code */
3010 /* Add compensation for negative dividents */
3012 x86_shift_reg_imm (code, X86_SHR, X86_EDX, 32 - power);
3013 x86_alu_reg_reg (code, X86_ADD, X86_EAX, X86_EDX);
3014 /* Compute remainder */
3015 x86_alu_reg_imm (code, X86_AND, X86_EAX, (1 << power) - 1);
3016 /* Remove compensation */
3017 x86_alu_reg_reg (code, X86_SUB, X86_EAX, X86_EDX);
3022 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
3026 x86_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
3029 x86_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
3033 x86_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
3036 g_assert (ins->sreg2 == X86_ECX);
3037 x86_shift_reg (code, X86_SHL, ins->dreg);
3040 g_assert (ins->sreg2 == X86_ECX);
3041 x86_shift_reg (code, X86_SAR, ins->dreg);
3045 x86_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
3048 case OP_ISHR_UN_IMM:
3049 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
3052 g_assert (ins->sreg2 == X86_ECX);
3053 x86_shift_reg (code, X86_SHR, ins->dreg);
3057 x86_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
3060 guint8 *jump_to_end;
3062 /* handle shifts below 32 bits */
3063 x86_shld_reg (code, ins->backend.reg3, ins->sreg1);
3064 x86_shift_reg (code, X86_SHL, ins->sreg1);
3066 x86_test_reg_imm (code, X86_ECX, 32);
3067 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
3069 /* handle shift over 32 bit */
3070 x86_mov_reg_reg (code, ins->backend.reg3, ins->sreg1, 4);
3071 x86_clear_reg (code, ins->sreg1);
3073 x86_patch (jump_to_end, code);
3077 guint8 *jump_to_end;
3079 /* handle shifts below 32 bits */
3080 x86_shrd_reg (code, ins->sreg1, ins->backend.reg3);
3081 x86_shift_reg (code, X86_SAR, ins->backend.reg3);
3083 x86_test_reg_imm (code, X86_ECX, 32);
3084 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
3086 /* handle shifts over 31 bits */
3087 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
3088 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, 31);
3090 x86_patch (jump_to_end, code);
3094 guint8 *jump_to_end;
3096 /* handle shifts below 32 bits */
3097 x86_shrd_reg (code, ins->sreg1, ins->backend.reg3);
3098 x86_shift_reg (code, X86_SHR, ins->backend.reg3);
3100 x86_test_reg_imm (code, X86_ECX, 32);
3101 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
3103 /* handle shifts over 31 bits */
3104 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
3105 x86_clear_reg (code, ins->backend.reg3);
3107 x86_patch (jump_to_end, code);
3111 if (ins->inst_imm >= 32) {
3112 x86_mov_reg_reg (code, ins->backend.reg3, ins->sreg1, 4);
3113 x86_clear_reg (code, ins->sreg1);
3114 x86_shift_reg_imm (code, X86_SHL, ins->backend.reg3, ins->inst_imm - 32);
3116 x86_shld_reg_imm (code, ins->backend.reg3, ins->sreg1, ins->inst_imm);
3117 x86_shift_reg_imm (code, X86_SHL, ins->sreg1, ins->inst_imm);
3121 if (ins->inst_imm >= 32) {
3122 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
3123 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, 0x1f);
3124 x86_shift_reg_imm (code, X86_SAR, ins->sreg1, ins->inst_imm - 32);
3126 x86_shrd_reg_imm (code, ins->sreg1, ins->backend.reg3, ins->inst_imm);
3127 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, ins->inst_imm);
3130 case OP_LSHR_UN_IMM:
3131 if (ins->inst_imm >= 32) {
3132 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
3133 x86_clear_reg (code, ins->backend.reg3);
3134 x86_shift_reg_imm (code, X86_SHR, ins->sreg1, ins->inst_imm - 32);
3136 x86_shrd_reg_imm (code, ins->sreg1, ins->backend.reg3, ins->inst_imm);
3137 x86_shift_reg_imm (code, X86_SHR, ins->backend.reg3, ins->inst_imm);
3141 x86_not_reg (code, ins->sreg1);
3144 x86_neg_reg (code, ins->sreg1);
3148 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
3152 switch (ins->inst_imm) {
3156 if (ins->dreg != ins->sreg1)
3157 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
3158 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
3161 /* LEA r1, [r2 + r2*2] */
3162 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
3165 /* LEA r1, [r2 + r2*4] */
3166 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3169 /* LEA r1, [r2 + r2*2] */
3171 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
3172 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
3175 /* LEA r1, [r2 + r2*8] */
3176 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
3179 /* LEA r1, [r2 + r2*4] */
3181 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3182 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
3185 /* LEA r1, [r2 + r2*2] */
3187 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
3188 x86_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
3191 /* LEA r1, [r2 + r2*4] */
3192 /* LEA r1, [r1 + r1*4] */
3193 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3194 x86_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
3197 /* LEA r1, [r2 + r2*4] */
3199 /* LEA r1, [r1 + r1*4] */
3200 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3201 x86_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
3202 x86_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
3205 x86_imul_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_imm);
3210 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
3211 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
3213 case OP_IMUL_OVF_UN: {
3214 /* the mul operation and the exception check should most likely be split */
3215 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
3216 /*g_assert (ins->sreg2 == X86_EAX);
3217 g_assert (ins->dreg == X86_EAX);*/
3218 if (ins->sreg2 == X86_EAX) {
3219 non_eax_reg = ins->sreg1;
3220 } else if (ins->sreg1 == X86_EAX) {
3221 non_eax_reg = ins->sreg2;
3223 /* no need to save since we're going to store to it anyway */
3224 if (ins->dreg != X86_EAX) {
3226 x86_push_reg (code, X86_EAX);
3228 x86_mov_reg_reg (code, X86_EAX, ins->sreg1, 4);
3229 non_eax_reg = ins->sreg2;
3231 if (ins->dreg == X86_EDX) {
3234 x86_push_reg (code, X86_EAX);
3236 } else if (ins->dreg != X86_EAX) {
3238 x86_push_reg (code, X86_EDX);
3240 x86_mul_reg (code, non_eax_reg, FALSE);
3241 /* save before the check since pop and mov don't change the flags */
3242 if (ins->dreg != X86_EAX)
3243 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
3245 x86_pop_reg (code, X86_EDX);
3247 x86_pop_reg (code, X86_EAX);
3248 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
3252 x86_mov_reg_imm (code, ins->dreg, ins->inst_c0);
3255 g_assert_not_reached ();
3256 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3257 x86_mov_reg_imm (code, ins->dreg, 0);
3260 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3261 x86_mov_reg_imm (code, ins->dreg, 0);
3263 case OP_LOAD_GOTADDR:
3264 g_assert (ins->dreg == MONO_ARCH_GOT_REG);
3265 code = mono_arch_emit_load_got_addr (cfg->native_code, code, cfg, NULL);
3268 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
3269 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, 0xf0f0f0f0, 4);
3271 case OP_X86_PUSH_GOT_ENTRY:
3272 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
3273 x86_push_membase (code, ins->inst_basereg, 0xf0f0f0f0);
3276 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
3279 MonoCallInst *call = (MonoCallInst*)ins;
3282 ins->flags |= MONO_INST_GC_CALLSITE;
3283 ins->backend.pc_offset = code - cfg->native_code;
3285 /* FIXME: no tracing support... */
3286 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3287 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
3288 /* reset offset to make max_len work */
3289 offset = code - cfg->native_code;
3291 g_assert (!cfg->method->save_lmf);
3293 /* restore callee saved registers */
3294 for (i = 0; i < X86_NREG; ++i)
3295 if (X86_IS_CALLEE_SAVED_REG (i) && cfg->used_int_regs & (1 << i))
3297 if (cfg->used_int_regs & (1 << X86_ESI)) {
3298 x86_mov_reg_membase (code, X86_ESI, X86_EBP, pos, 4);
3301 if (cfg->used_int_regs & (1 << X86_EDI)) {
3302 x86_mov_reg_membase (code, X86_EDI, X86_EBP, pos, 4);
3305 if (cfg->used_int_regs & (1 << X86_EBX)) {
3306 x86_mov_reg_membase (code, X86_EBX, X86_EBP, pos, 4);
3310 /* Copy arguments on the stack to our argument area */
3311 for (i = 0; i < call->stack_usage - call->stack_align_amount; i += 4) {
3312 x86_mov_reg_membase (code, X86_EAX, X86_ESP, i, 4);
3313 x86_mov_membase_reg (code, X86_EBP, 8 + i, X86_EAX, 4);
3316 /* restore ESP/EBP */
3318 offset = code - cfg->native_code;
3319 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD_JUMP, call->method);
3320 x86_jump32 (code, 0);
3322 ins->flags |= MONO_INST_GC_CALLSITE;
3323 cfg->disable_aot = TRUE;
3327 /* ensure ins->sreg1 is not NULL
3328 * note that cmp DWORD PTR [eax], eax is one byte shorter than
3329 * cmp DWORD PTR [eax], 0
3331 x86_alu_membase_reg (code, X86_CMP, ins->sreg1, 0, ins->sreg1);
3334 int hreg = ins->sreg1 == X86_EAX? X86_ECX: X86_EAX;
3335 x86_push_reg (code, hreg);
3336 x86_lea_membase (code, hreg, X86_EBP, cfg->sig_cookie);
3337 x86_mov_membase_reg (code, ins->sreg1, 0, hreg, 4);
3338 x86_pop_reg (code, hreg);
3347 call = (MonoCallInst*)ins;
3348 if (ins->flags & MONO_INST_HAS_METHOD)
3349 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method);
3351 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr);
3352 ins->flags |= MONO_INST_GC_CALLSITE;
3353 ins->backend.pc_offset = code - cfg->native_code;
3354 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
3355 /* a pop is one byte, while an add reg, imm is 3. So if there are 4 or 8
3356 * bytes to pop, we want to use pops. GCC does this (note it won't happen
3357 * for P4 or i686 because gcc will avoid using pop push at all. But we aren't
3358 * smart enough to do that optimization yet
3360 * It turns out that on my P4, doing two pops for 8 bytes on the stack makes
3361 * mcs botstrap slow down. However, doing 1 pop for 4 bytes creates a small,
3362 * (most likely from locality benefits). People with other processors should
3363 * check on theirs to see what happens.
3365 if (call->stack_usage == 4) {
3366 /* we want to use registers that won't get used soon, so use
3367 * ecx, as eax will get allocated first. edx is used by long calls,
3368 * so we can't use that.
3371 x86_pop_reg (code, X86_ECX);
3373 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
3376 code = emit_move_return_value (cfg, ins, code);
3382 case OP_VOIDCALL_REG:
3384 call = (MonoCallInst*)ins;
3385 x86_call_reg (code, ins->sreg1);
3386 ins->flags |= MONO_INST_GC_CALLSITE;
3387 ins->backend.pc_offset = code - cfg->native_code;
3388 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
3389 if (call->stack_usage == 4)
3390 x86_pop_reg (code, X86_ECX);
3392 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
3394 code = emit_move_return_value (cfg, ins, code);
3396 case OP_FCALL_MEMBASE:
3397 case OP_LCALL_MEMBASE:
3398 case OP_VCALL_MEMBASE:
3399 case OP_VCALL2_MEMBASE:
3400 case OP_VOIDCALL_MEMBASE:
3401 case OP_CALL_MEMBASE:
3402 call = (MonoCallInst*)ins;
3404 x86_call_membase (code, ins->sreg1, ins->inst_offset);
3405 ins->flags |= MONO_INST_GC_CALLSITE;
3406 ins->backend.pc_offset = code - cfg->native_code;
3407 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
3408 if (call->stack_usage == 4)
3409 x86_pop_reg (code, X86_ECX);
3411 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
3413 code = emit_move_return_value (cfg, ins, code);
3416 x86_push_reg (code, ins->sreg1);
3418 case OP_X86_PUSH_IMM:
3419 x86_push_imm (code, ins->inst_imm);
3421 case OP_X86_PUSH_MEMBASE:
3422 x86_push_membase (code, ins->inst_basereg, ins->inst_offset);
3424 case OP_X86_PUSH_OBJ:
3425 x86_alu_reg_imm (code, X86_SUB, X86_ESP, ins->inst_imm);
3426 x86_push_reg (code, X86_EDI);
3427 x86_push_reg (code, X86_ESI);
3428 x86_push_reg (code, X86_ECX);
3429 if (ins->inst_offset)
3430 x86_lea_membase (code, X86_ESI, ins->inst_basereg, ins->inst_offset);
3432 x86_mov_reg_reg (code, X86_ESI, ins->inst_basereg, 4);
3433 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
3434 x86_mov_reg_imm (code, X86_ECX, (ins->inst_imm >> 2));
3436 x86_prefix (code, X86_REP_PREFIX);
3438 x86_pop_reg (code, X86_ECX);
3439 x86_pop_reg (code, X86_ESI);
3440 x86_pop_reg (code, X86_EDI);
3443 x86_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
3445 case OP_X86_LEA_MEMBASE:
3446 x86_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
3449 x86_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
3452 /* keep alignment */
3453 x86_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_LOCALLOC_ALIGNMENT - 1);
3454 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_LOCALLOC_ALIGNMENT - 1));
3455 code = mono_emit_stack_alloc (code, ins);
3456 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
3458 case OP_LOCALLOC_IMM: {
3459 guint32 size = ins->inst_imm;
3460 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
3462 if (ins->flags & MONO_INST_INIT) {
3463 /* FIXME: Optimize this */
3464 x86_mov_reg_imm (code, ins->dreg, size);
3465 ins->sreg1 = ins->dreg;
3467 code = mono_emit_stack_alloc (code, ins);
3468 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
3470 x86_alu_reg_imm (code, X86_SUB, X86_ESP, size);
3471 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
3476 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3477 x86_push_reg (code, ins->sreg1);
3478 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
3479 (gpointer)"mono_arch_throw_exception");
3480 ins->flags |= MONO_INST_GC_CALLSITE;
3481 ins->backend.pc_offset = code - cfg->native_code;
3485 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3486 x86_push_reg (code, ins->sreg1);
3487 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
3488 (gpointer)"mono_arch_rethrow_exception");
3489 ins->flags |= MONO_INST_GC_CALLSITE;
3490 ins->backend.pc_offset = code - cfg->native_code;
3493 case OP_CALL_HANDLER:
3494 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3495 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3496 x86_call_imm (code, 0);
3497 mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
3498 x86_alu_reg_imm (code, X86_ADD, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3500 case OP_START_HANDLER: {
3501 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3502 x86_mov_membase_reg (code, spvar->inst_basereg, spvar->inst_offset, X86_ESP, 4);
3505 case OP_ENDFINALLY: {
3506 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3507 x86_mov_reg_membase (code, X86_ESP, spvar->inst_basereg, spvar->inst_offset, 4);
3511 case OP_ENDFILTER: {
3512 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3513 x86_mov_reg_membase (code, X86_ESP, spvar->inst_basereg, spvar->inst_offset, 4);
3514 /* The local allocator will put the result into EAX */
3520 ins->inst_c0 = code - cfg->native_code;
3523 if (ins->inst_target_bb->native_offset) {
3524 x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
3526 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3527 if ((cfg->opt & MONO_OPT_BRANCH) &&
3528 x86_is_imm8 (ins->inst_target_bb->max_offset - cpos))
3529 x86_jump8 (code, 0);
3531 x86_jump32 (code, 0);
3535 x86_jump_reg (code, ins->sreg1);
3554 x86_set_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
3555 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3557 case OP_COND_EXC_EQ:
3558 case OP_COND_EXC_NE_UN:
3559 case OP_COND_EXC_LT:
3560 case OP_COND_EXC_LT_UN:
3561 case OP_COND_EXC_GT:
3562 case OP_COND_EXC_GT_UN:
3563 case OP_COND_EXC_GE:
3564 case OP_COND_EXC_GE_UN:
3565 case OP_COND_EXC_LE:
3566 case OP_COND_EXC_LE_UN:
3567 case OP_COND_EXC_IEQ:
3568 case OP_COND_EXC_INE_UN:
3569 case OP_COND_EXC_ILT:
3570 case OP_COND_EXC_ILT_UN:
3571 case OP_COND_EXC_IGT:
3572 case OP_COND_EXC_IGT_UN:
3573 case OP_COND_EXC_IGE:
3574 case OP_COND_EXC_IGE_UN:
3575 case OP_COND_EXC_ILE:
3576 case OP_COND_EXC_ILE_UN:
3577 EMIT_COND_SYSTEM_EXCEPTION (cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)], ins->inst_p1);
3579 case OP_COND_EXC_OV:
3580 case OP_COND_EXC_NO:
3582 case OP_COND_EXC_NC:
3583 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ], (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
3585 case OP_COND_EXC_IOV:
3586 case OP_COND_EXC_INO:
3587 case OP_COND_EXC_IC:
3588 case OP_COND_EXC_INC:
3589 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_IEQ], (ins->opcode < OP_COND_EXC_INE_UN), ins->inst_p1);
3601 EMIT_COND_BRANCH (ins, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
3609 case OP_CMOV_INE_UN:
3610 case OP_CMOV_IGE_UN:
3611 case OP_CMOV_IGT_UN:
3612 case OP_CMOV_ILE_UN:
3613 case OP_CMOV_ILT_UN:
3614 g_assert (ins->dreg == ins->sreg1);
3615 x86_cmov_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, ins->sreg2);
3618 /* floating point opcodes */
3620 double d = *(double *)ins->inst_p0;
3622 if ((d == 0.0) && (mono_signbit (d) == 0)) {
3624 } else if (d == 1.0) {
3627 if (cfg->compile_aot) {
3628 guint32 *val = (guint32*)&d;
3629 x86_push_imm (code, val [1]);
3630 x86_push_imm (code, val [0]);
3631 x86_fld_membase (code, X86_ESP, 0, TRUE);
3632 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3635 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R8, ins->inst_p0);
3636 x86_fld (code, NULL, TRUE);
3642 float f = *(float *)ins->inst_p0;
3644 if ((f == 0.0) && (mono_signbit (f) == 0)) {
3646 } else if (f == 1.0) {
3649 if (cfg->compile_aot) {
3650 guint32 val = *(guint32*)&f;
3651 x86_push_imm (code, val);
3652 x86_fld_membase (code, X86_ESP, 0, FALSE);
3653 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3656 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R4, ins->inst_p0);
3657 x86_fld (code, NULL, FALSE);
3662 case OP_STORER8_MEMBASE_REG:
3663 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, TRUE, TRUE);
3665 case OP_LOADR8_MEMBASE:
3666 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
3668 case OP_STORER4_MEMBASE_REG:
3669 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, FALSE, TRUE);
3671 case OP_LOADR4_MEMBASE:
3672 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
3674 case OP_ICONV_TO_R4:
3675 x86_push_reg (code, ins->sreg1);
3676 x86_fild_membase (code, X86_ESP, 0, FALSE);
3677 /* Change precision */
3678 x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
3679 x86_fld_membase (code, X86_ESP, 0, FALSE);
3680 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3682 case OP_ICONV_TO_R8:
3683 x86_push_reg (code, ins->sreg1);
3684 x86_fild_membase (code, X86_ESP, 0, FALSE);
3685 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3687 case OP_ICONV_TO_R_UN:
3688 x86_push_imm (code, 0);
3689 x86_push_reg (code, ins->sreg1);
3690 x86_fild_membase (code, X86_ESP, 0, TRUE);
3691 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3693 case OP_X86_FP_LOAD_I8:
3694 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
3696 case OP_X86_FP_LOAD_I4:
3697 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
3699 case OP_FCONV_TO_R4:
3700 /* Change precision */
3701 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
3702 x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
3703 x86_fld_membase (code, X86_ESP, 0, FALSE);
3704 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3706 case OP_FCONV_TO_I1:
3707 code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
3709 case OP_FCONV_TO_U1:
3710 code = emit_float_to_int (cfg, code, ins->dreg, 1, FALSE);
3712 case OP_FCONV_TO_I2:
3713 code = emit_float_to_int (cfg, code, ins->dreg, 2, TRUE);
3715 case OP_FCONV_TO_U2:
3716 code = emit_float_to_int (cfg, code, ins->dreg, 2, FALSE);
3718 case OP_FCONV_TO_I4:
3720 code = emit_float_to_int (cfg, code, ins->dreg, 4, TRUE);
3722 case OP_FCONV_TO_I8:
3723 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
3724 x86_fnstcw_membase(code, X86_ESP, 0);
3725 x86_mov_reg_membase (code, ins->dreg, X86_ESP, 0, 2);
3726 x86_alu_reg_imm (code, X86_OR, ins->dreg, 0xc00);
3727 x86_mov_membase_reg (code, X86_ESP, 2, ins->dreg, 2);
3728 x86_fldcw_membase (code, X86_ESP, 2);
3729 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
3730 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
3731 x86_pop_reg (code, ins->dreg);
3732 x86_pop_reg (code, ins->backend.reg3);
3733 x86_fldcw_membase (code, X86_ESP, 0);
3734 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3736 case OP_LCONV_TO_R8_2:
3737 x86_push_reg (code, ins->sreg2);
3738 x86_push_reg (code, ins->sreg1);
3739 x86_fild_membase (code, X86_ESP, 0, TRUE);
3740 /* Change precision */
3741 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
3742 x86_fld_membase (code, X86_ESP, 0, TRUE);
3743 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3745 case OP_LCONV_TO_R4_2:
3746 x86_push_reg (code, ins->sreg2);
3747 x86_push_reg (code, ins->sreg1);
3748 x86_fild_membase (code, X86_ESP, 0, TRUE);
3749 /* Change precision */
3750 x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
3751 x86_fld_membase (code, X86_ESP, 0, FALSE);
3752 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3754 case OP_LCONV_TO_R_UN_2: {
3755 static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
3758 /* load 64bit integer to FP stack */
3759 x86_push_reg (code, ins->sreg2);
3760 x86_push_reg (code, ins->sreg1);
3761 x86_fild_membase (code, X86_ESP, 0, TRUE);
3763 /* test if lreg is negative */
3764 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
3765 br = code; x86_branch8 (code, X86_CC_GEZ, 0, TRUE);
3767 /* add correction constant mn */
3768 if (cfg->compile_aot) {
3769 x86_push_imm (code, (((guint32)mn [9]) << 24) | ((guint32)mn [8] << 16) | ((guint32)mn [7] << 8) | ((guint32)mn [6]));
3770 x86_push_imm (code, (((guint32)mn [5]) << 24) | ((guint32)mn [4] << 16) | ((guint32)mn [3] << 8) | ((guint32)mn [2]));
3771 x86_push_imm (code, (((guint32)mn [1]) << 24) | ((guint32)mn [0] << 16));
3772 x86_fld80_membase (code, X86_ESP, 2);
3773 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
3775 x86_fld80_mem (code, mn);
3777 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3779 x86_patch (br, code);
3781 /* Change precision */
3782 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
3783 x86_fld_membase (code, X86_ESP, 0, TRUE);
3785 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3789 case OP_LCONV_TO_OVF_I:
3790 case OP_LCONV_TO_OVF_I4_2: {
3791 guint8 *br [3], *label [1];
3795 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
3797 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
3799 /* If the low word top bit is set, see if we are negative */
3800 br [0] = code; x86_branch8 (code, X86_CC_LT, 0, TRUE);
3801 /* We are not negative (no top bit set, check for our top word to be zero */
3802 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
3803 br [1] = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
3806 /* throw exception */
3807 tins = mono_branch_optimize_exception_target (cfg, bb, "OverflowException");
3809 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, tins->inst_true_bb);
3810 if ((cfg->opt & MONO_OPT_BRANCH) && x86_is_imm8 (tins->inst_true_bb->max_offset - cpos))
3811 x86_jump8 (code, 0);
3813 x86_jump32 (code, 0);
3815 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
3816 x86_jump32 (code, 0);
3820 x86_patch (br [0], code);
3821 /* our top bit is set, check that top word is 0xfffffff */
3822 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0xffffffff);
3824 x86_patch (br [1], code);
3825 /* nope, emit exception */
3826 br [2] = code; x86_branch8 (code, X86_CC_NE, 0, TRUE);
3827 x86_patch (br [2], label [0]);
3829 if (ins->dreg != ins->sreg1)
3830 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
3834 /* Not needed on the fp stack */
3837 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3840 x86_fp_op_reg (code, X86_FSUB, 1, TRUE);
3843 x86_fp_op_reg (code, X86_FMUL, 1, TRUE);
3846 x86_fp_op_reg (code, X86_FDIV, 1, TRUE);
3854 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3859 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3866 * it really doesn't make sense to inline all this code,
3867 * it's here just to show that things may not be as simple
3870 guchar *check_pos, *end_tan, *pop_jump;
3871 x86_push_reg (code, X86_EAX);
3874 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
3876 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3877 x86_fstp (code, 0); /* pop the 1.0 */
3879 x86_jump8 (code, 0);
3881 x86_fp_op (code, X86_FADD, 0);
3885 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
3887 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3890 x86_patch (pop_jump, code);
3891 x86_fstp (code, 0); /* pop the 1.0 */
3892 x86_patch (check_pos, code);
3893 x86_patch (end_tan, code);
3895 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3896 x86_pop_reg (code, X86_EAX);
3903 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3912 g_assert (cfg->opt & MONO_OPT_CMOV);
3913 g_assert (ins->dreg == ins->sreg1);
3914 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3915 x86_cmov_reg (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2);
3918 g_assert (cfg->opt & MONO_OPT_CMOV);
3919 g_assert (ins->dreg == ins->sreg1);
3920 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3921 x86_cmov_reg (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2);
3924 g_assert (cfg->opt & MONO_OPT_CMOV);
3925 g_assert (ins->dreg == ins->sreg1);
3926 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3927 x86_cmov_reg (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2);
3930 g_assert (cfg->opt & MONO_OPT_CMOV);
3931 g_assert (ins->dreg == ins->sreg1);
3932 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3933 x86_cmov_reg (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2);
3939 x86_fxch (code, ins->inst_imm);
3944 x86_push_reg (code, X86_EAX);
3945 /* we need to exchange ST(0) with ST(1) */
3948 /* this requires a loop, because fprem somtimes
3949 * returns a partial remainder */
3951 /* looks like MS is using fprem instead of the IEEE compatible fprem1 */
3952 /* x86_fprem1 (code); */
3955 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_C2);
3957 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3963 x86_pop_reg (code, X86_EAX);
3967 if (cfg->opt & MONO_OPT_FCMOV) {
3968 x86_fcomip (code, 1);
3972 /* this overwrites EAX */
3973 EMIT_FPCOMPARE(code);
3974 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3978 if (cfg->opt & MONO_OPT_FCMOV) {
3979 /* zeroing the register at the start results in
3980 * shorter and faster code (we can also remove the widening op)
3982 guchar *unordered_check;
3983 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3984 x86_fcomip (code, 1);
3986 unordered_check = code;
3987 x86_branch8 (code, X86_CC_P, 0, FALSE);
3988 if (ins->opcode == OP_FCEQ) {
3989 x86_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
3990 x86_patch (unordered_check, code);
3992 guchar *jump_to_end;
3993 x86_set_reg (code, X86_CC_NE, ins->dreg, FALSE);
3995 x86_jump8 (code, 0);
3996 x86_patch (unordered_check, code);
3997 x86_inc_reg (code, ins->dreg);
3998 x86_patch (jump_to_end, code);
4003 if (ins->dreg != X86_EAX)
4004 x86_push_reg (code, X86_EAX);
4006 EMIT_FPCOMPARE(code);
4007 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
4008 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
4009 x86_set_reg (code, ins->opcode == OP_FCEQ ? X86_CC_EQ : X86_CC_NE, ins->dreg, TRUE);
4010 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
4012 if (ins->dreg != X86_EAX)
4013 x86_pop_reg (code, X86_EAX);
4017 if (cfg->opt & MONO_OPT_FCMOV) {
4018 /* zeroing the register at the start results in
4019 * shorter and faster code (we can also remove the widening op)
4021 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4022 x86_fcomip (code, 1);
4024 if (ins->opcode == OP_FCLT_UN) {
4025 guchar *unordered_check = code;
4026 guchar *jump_to_end;
4027 x86_branch8 (code, X86_CC_P, 0, FALSE);
4028 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
4030 x86_jump8 (code, 0);
4031 x86_patch (unordered_check, code);
4032 x86_inc_reg (code, ins->dreg);
4033 x86_patch (jump_to_end, code);
4035 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
4039 if (ins->dreg != X86_EAX)
4040 x86_push_reg (code, X86_EAX);
4042 EMIT_FPCOMPARE(code);
4043 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
4044 if (ins->opcode == OP_FCLT_UN) {
4045 guchar *is_not_zero_check, *end_jump;
4046 is_not_zero_check = code;
4047 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
4049 x86_jump8 (code, 0);
4050 x86_patch (is_not_zero_check, code);
4051 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
4053 x86_patch (end_jump, code);
4055 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
4056 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
4058 if (ins->dreg != X86_EAX)
4059 x86_pop_reg (code, X86_EAX);
4062 guchar *unordered_check;
4063 guchar *jump_to_end;
4064 if (cfg->opt & MONO_OPT_FCMOV) {
4065 /* zeroing the register at the start results in
4066 * shorter and faster code (we can also remove the widening op)
4068 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4069 x86_fcomip (code, 1);
4071 unordered_check = code;
4072 x86_branch8 (code, X86_CC_P, 0, FALSE);
4073 x86_set_reg (code, X86_CC_NB, ins->dreg, FALSE);
4074 x86_patch (unordered_check, code);
4077 if (ins->dreg != X86_EAX)
4078 x86_push_reg (code, X86_EAX);
4080 EMIT_FPCOMPARE(code);
4081 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
4082 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
4083 unordered_check = code;
4084 x86_branch8 (code, X86_CC_EQ, 0, FALSE);
4086 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4087 x86_set_reg (code, X86_CC_NE, ins->dreg, TRUE);
4088 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
4090 x86_jump8 (code, 0);
4091 x86_patch (unordered_check, code);
4092 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4093 x86_patch (jump_to_end, code);
4095 if (ins->dreg != X86_EAX)
4096 x86_pop_reg (code, X86_EAX);
4101 if (cfg->opt & MONO_OPT_FCMOV) {
4102 /* zeroing the register at the start results in
4103 * shorter and faster code (we can also remove the widening op)
4105 guchar *unordered_check;
4106 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4107 x86_fcomip (code, 1);
4109 if (ins->opcode == OP_FCGT) {
4110 unordered_check = code;
4111 x86_branch8 (code, X86_CC_P, 0, FALSE);
4112 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
4113 x86_patch (unordered_check, code);
4115 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
4119 if (ins->dreg != X86_EAX)
4120 x86_push_reg (code, X86_EAX);
4122 EMIT_FPCOMPARE(code);
4123 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
4124 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4125 if (ins->opcode == OP_FCGT_UN) {
4126 guchar *is_not_zero_check, *end_jump;
4127 is_not_zero_check = code;
4128 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
4130 x86_jump8 (code, 0);
4131 x86_patch (is_not_zero_check, code);
4132 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
4134 x86_patch (end_jump, code);
4136 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
4137 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
4139 if (ins->dreg != X86_EAX)
4140 x86_pop_reg (code, X86_EAX);
4143 guchar *unordered_check;
4144 guchar *jump_to_end;
4145 if (cfg->opt & MONO_OPT_FCMOV) {
4146 /* zeroing the register at the start results in
4147 * shorter and faster code (we can also remove the widening op)
4149 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4150 x86_fcomip (code, 1);
4152 unordered_check = code;
4153 x86_branch8 (code, X86_CC_P, 0, FALSE);
4154 x86_set_reg (code, X86_CC_NA, ins->dreg, FALSE);
4155 x86_patch (unordered_check, code);
4158 if (ins->dreg != X86_EAX)
4159 x86_push_reg (code, X86_EAX);
4161 EMIT_FPCOMPARE(code);
4162 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
4163 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
4164 unordered_check = code;
4165 x86_branch8 (code, X86_CC_EQ, 0, FALSE);
4167 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4168 x86_set_reg (code, X86_CC_GE, ins->dreg, TRUE);
4169 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
4171 x86_jump8 (code, 0);
4172 x86_patch (unordered_check, code);
4173 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4174 x86_patch (jump_to_end, code);
4176 if (ins->dreg != X86_EAX)
4177 x86_pop_reg (code, X86_EAX);
4181 if (cfg->opt & MONO_OPT_FCMOV) {
4182 guchar *jump = code;
4183 x86_branch8 (code, X86_CC_P, 0, TRUE);
4184 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4185 x86_patch (jump, code);
4188 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
4189 EMIT_COND_BRANCH (ins, X86_CC_EQ, TRUE);
4192 /* Branch if C013 != 100 */
4193 if (cfg->opt & MONO_OPT_FCMOV) {
4194 /* branch if !ZF or (PF|CF) */
4195 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
4196 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
4197 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
4200 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
4201 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
4204 if (cfg->opt & MONO_OPT_FCMOV) {
4205 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
4208 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4211 if (cfg->opt & MONO_OPT_FCMOV) {
4212 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
4213 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
4216 if (ins->opcode == OP_FBLT_UN) {
4217 guchar *is_not_zero_check, *end_jump;
4218 is_not_zero_check = code;
4219 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
4221 x86_jump8 (code, 0);
4222 x86_patch (is_not_zero_check, code);
4223 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
4225 x86_patch (end_jump, code);
4227 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4231 if (cfg->opt & MONO_OPT_FCMOV) {
4232 if (ins->opcode == OP_FBGT) {
4235 /* skip branch if C1=1 */
4237 x86_branch8 (code, X86_CC_P, 0, FALSE);
4238 /* branch if (C0 | C3) = 1 */
4239 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
4240 x86_patch (br1, code);
4242 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
4246 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4247 if (ins->opcode == OP_FBGT_UN) {
4248 guchar *is_not_zero_check, *end_jump;
4249 is_not_zero_check = code;
4250 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
4252 x86_jump8 (code, 0);
4253 x86_patch (is_not_zero_check, code);
4254 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
4256 x86_patch (end_jump, code);
4258 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4261 /* Branch if C013 == 100 or 001 */
4262 if (cfg->opt & MONO_OPT_FCMOV) {
4265 /* skip branch if C1=1 */
4267 x86_branch8 (code, X86_CC_P, 0, FALSE);
4268 /* branch if (C0 | C3) = 1 */
4269 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
4270 x86_patch (br1, code);
4273 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4274 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4275 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
4276 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4279 /* Branch if C013 == 000 */
4280 if (cfg->opt & MONO_OPT_FCMOV) {
4281 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
4284 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
4287 /* Branch if C013=000 or 100 */
4288 if (cfg->opt & MONO_OPT_FCMOV) {
4291 /* skip branch if C1=1 */
4293 x86_branch8 (code, X86_CC_P, 0, FALSE);
4294 /* branch if C0=0 */
4295 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
4296 x86_patch (br1, code);
4299 x86_alu_reg_imm (code, X86_AND, X86_EAX, (X86_FP_C0|X86_FP_C1));
4300 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0);
4301 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4304 /* Branch if C013 != 001 */
4305 if (cfg->opt & MONO_OPT_FCMOV) {
4306 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
4307 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
4310 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4311 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
4315 x86_push_reg (code, X86_EAX);
4318 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
4319 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4320 x86_pop_reg (code, X86_EAX);
4322 /* Have to clean up the fp stack before throwing the exception */
4324 x86_branch8 (code, X86_CC_NE, 0, FALSE);
4327 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
4329 x86_patch (br1, code);
4333 code = mono_x86_emit_tls_get (code, ins->dreg, ins->inst_offset);
4336 case OP_TLS_GET_REG: {
4337 code = emit_tls_get_reg (code, ins->dreg, ins->sreg1);
4341 code = mono_x86_emit_tls_set (code, ins->sreg1, ins->inst_offset);
4344 case OP_TLS_SET_REG: {
4345 code = emit_tls_set_reg (code, ins->sreg1, ins->sreg2);
4348 case OP_MEMORY_BARRIER: {
4349 /* x86 only needs barrier for StoreLoad and FullBarrier */
4350 switch (ins->backend.memory_barrier_kind) {
4351 case StoreLoadBarrier:
4353 /* http://blogs.sun.com/dave/resource/NHM-Pipeline-Blog-V2.txt */
4354 x86_prefix (code, X86_LOCK_PREFIX);
4355 x86_alu_membase_imm (code, X86_ADD, X86_ESP, 0, 0);
4360 case OP_ATOMIC_ADD_I4: {
4361 int dreg = ins->dreg;
4363 if (dreg == ins->inst_basereg) {
4364 x86_push_reg (code, ins->sreg2);
4368 if (dreg != ins->sreg2)
4369 x86_mov_reg_reg (code, ins->dreg, ins->sreg2, 4);
4371 x86_prefix (code, X86_LOCK_PREFIX);
4372 x86_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, 4);
4374 if (dreg != ins->dreg) {
4375 x86_mov_reg_reg (code, ins->dreg, dreg, 4);
4376 x86_pop_reg (code, dreg);
4381 case OP_ATOMIC_ADD_NEW_I4: {
4382 int dreg = ins->dreg;
4384 /* hack: limit in regalloc, dreg != sreg1 && dreg != sreg2 */
4385 if (ins->sreg2 == dreg) {
4386 if (dreg == X86_EBX) {
4388 if (ins->inst_basereg == X86_EDI)
4392 if (ins->inst_basereg == X86_EBX)
4395 } else if (ins->inst_basereg == dreg) {
4396 if (dreg == X86_EBX) {
4398 if (ins->sreg2 == X86_EDI)
4402 if (ins->sreg2 == X86_EBX)
4407 if (dreg != ins->dreg) {
4408 x86_push_reg (code, dreg);
4411 x86_mov_reg_reg (code, dreg, ins->sreg2, 4);
4412 x86_prefix (code, X86_LOCK_PREFIX);
4413 x86_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, 4);
4414 /* dreg contains the old value, add with sreg2 value */
4415 x86_alu_reg_reg (code, X86_ADD, dreg, ins->sreg2);
4417 if (ins->dreg != dreg) {
4418 x86_mov_reg_reg (code, ins->dreg, dreg, 4);
4419 x86_pop_reg (code, dreg);
4424 case OP_ATOMIC_EXCHANGE_I4: {
4426 int sreg2 = ins->sreg2;
4427 int breg = ins->inst_basereg;
4429 /* cmpxchg uses eax as comperand, need to make sure we can use it
4430 * hack to overcome limits in x86 reg allocator
4431 * (req: dreg == eax and sreg2 != eax and breg != eax)
4433 g_assert (ins->dreg == X86_EAX);
4435 /* We need the EAX reg for the cmpxchg */
4436 if (ins->sreg2 == X86_EAX) {
4437 sreg2 = (breg == X86_EDX) ? X86_EBX : X86_EDX;
4438 x86_push_reg (code, sreg2);
4439 x86_mov_reg_reg (code, sreg2, X86_EAX, 4);
4442 if (breg == X86_EAX) {
4443 breg = (sreg2 == X86_ESI) ? X86_EDI : X86_ESI;
4444 x86_push_reg (code, breg);
4445 x86_mov_reg_reg (code, breg, X86_EAX, 4);
4448 x86_mov_reg_membase (code, X86_EAX, breg, ins->inst_offset, 4);
4450 br [0] = code; x86_prefix (code, X86_LOCK_PREFIX);
4451 x86_cmpxchg_membase_reg (code, breg, ins->inst_offset, sreg2);
4452 br [1] = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
4453 x86_patch (br [1], br [0]);
4455 if (breg != ins->inst_basereg)
4456 x86_pop_reg (code, breg);
4458 if (ins->sreg2 != sreg2)
4459 x86_pop_reg (code, sreg2);
4463 case OP_ATOMIC_CAS_I4: {
4464 g_assert (ins->dreg == X86_EAX);
4465 g_assert (ins->sreg3 == X86_EAX);
4466 g_assert (ins->sreg1 != X86_EAX);
4467 g_assert (ins->sreg1 != ins->sreg2);
4469 x86_prefix (code, X86_LOCK_PREFIX);
4470 x86_cmpxchg_membase_reg (code, ins->sreg1, ins->inst_offset, ins->sreg2);
4473 case OP_CARD_TABLE_WBARRIER: {
4474 int ptr = ins->sreg1;
4475 int value = ins->sreg2;
4477 int nursery_shift, card_table_shift;
4478 gpointer card_table_mask;
4479 size_t nursery_size;
4480 gulong card_table = (gulong)mono_gc_get_card_table (&card_table_shift, &card_table_mask);
4481 gulong nursery_start = (gulong)mono_gc_get_nursery (&nursery_shift, &nursery_size);
4482 gboolean card_table_nursery_check = mono_gc_card_table_nursery_check ();
4485 * We need one register we can clobber, we choose EDX and make sreg1
4486 * fixed EAX to work around limitations in the local register allocator.
4487 * sreg2 might get allocated to EDX, but that is not a problem since
4488 * we use it before clobbering EDX.
4490 g_assert (ins->sreg1 == X86_EAX);
4493 * This is the code we produce:
4496 * edx >>= nursery_shift
4497 * cmp edx, (nursery_start >> nursery_shift)
4500 * edx >>= card_table_shift
4501 * card_table[edx] = 1
4505 if (card_table_nursery_check) {
4506 if (value != X86_EDX)
4507 x86_mov_reg_reg (code, X86_EDX, value, 4);
4508 x86_shift_reg_imm (code, X86_SHR, X86_EDX, nursery_shift);
4509 x86_alu_reg_imm (code, X86_CMP, X86_EDX, nursery_start >> nursery_shift);
4510 br = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
4512 x86_mov_reg_reg (code, X86_EDX, ptr, 4);
4513 x86_shift_reg_imm (code, X86_SHR, X86_EDX, card_table_shift);
4514 if (card_table_mask)
4515 x86_alu_reg_imm (code, X86_AND, X86_EDX, (int)card_table_mask);
4516 x86_mov_membase_imm (code, X86_EDX, card_table, 1, 1);
4517 if (card_table_nursery_check)
4518 x86_patch (br, code);
4521 #ifdef MONO_ARCH_SIMD_INTRINSICS
4523 x86_sse_alu_ps_reg_reg (code, X86_SSE_ADD, ins->sreg1, ins->sreg2);
4526 x86_sse_alu_ps_reg_reg (code, X86_SSE_DIV, ins->sreg1, ins->sreg2);
4529 x86_sse_alu_ps_reg_reg (code, X86_SSE_MUL, ins->sreg1, ins->sreg2);
4532 x86_sse_alu_ps_reg_reg (code, X86_SSE_SUB, ins->sreg1, ins->sreg2);
4535 x86_sse_alu_ps_reg_reg (code, X86_SSE_MAX, ins->sreg1, ins->sreg2);
4538 x86_sse_alu_ps_reg_reg (code, X86_SSE_MIN, ins->sreg1, ins->sreg2);
4541 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
4542 x86_sse_alu_ps_reg_reg_imm (code, X86_SSE_COMP, ins->sreg1, ins->sreg2, ins->inst_c0);
4545 x86_sse_alu_ps_reg_reg (code, X86_SSE_AND, ins->sreg1, ins->sreg2);
4548 x86_sse_alu_ps_reg_reg (code, X86_SSE_ANDN, ins->sreg1, ins->sreg2);
4551 x86_sse_alu_ps_reg_reg (code, X86_SSE_OR, ins->sreg1, ins->sreg2);
4554 x86_sse_alu_ps_reg_reg (code, X86_SSE_XOR, ins->sreg1, ins->sreg2);
4557 x86_sse_alu_ps_reg_reg (code, X86_SSE_SQRT, ins->dreg, ins->sreg1);
4560 x86_sse_alu_ps_reg_reg (code, X86_SSE_RSQRT, ins->dreg, ins->sreg1);
4563 x86_sse_alu_ps_reg_reg (code, X86_SSE_RCP, ins->dreg, ins->sreg1);
4566 x86_sse_alu_sd_reg_reg (code, X86_SSE_ADDSUB, ins->sreg1, ins->sreg2);
4569 x86_sse_alu_sd_reg_reg (code, X86_SSE_HADD, ins->sreg1, ins->sreg2);
4572 x86_sse_alu_sd_reg_reg (code, X86_SSE_HSUB, ins->sreg1, ins->sreg2);
4575 x86_sse_alu_ss_reg_reg (code, X86_SSE_MOVSHDUP, ins->dreg, ins->sreg1);
4578 x86_sse_alu_ss_reg_reg (code, X86_SSE_MOVSLDUP, ins->dreg, ins->sreg1);
4581 case OP_PSHUFLEW_HIGH:
4582 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
4583 x86_pshufw_reg_reg (code, ins->dreg, ins->sreg1, ins->inst_c0, 1);
4585 case OP_PSHUFLEW_LOW:
4586 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
4587 x86_pshufw_reg_reg (code, ins->dreg, ins->sreg1, ins->inst_c0, 0);
4590 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
4591 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->sreg1, ins->inst_c0);
4594 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
4595 x86_sse_alu_reg_reg_imm8 (code, X86_SSE_SHUFP, ins->sreg1, ins->sreg2, ins->inst_c0);
4598 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0x3);
4599 x86_sse_alu_pd_reg_reg_imm8 (code, X86_SSE_SHUFP, ins->sreg1, ins->sreg2, ins->inst_c0);
4603 x86_sse_alu_pd_reg_reg (code, X86_SSE_ADD, ins->sreg1, ins->sreg2);
4606 x86_sse_alu_pd_reg_reg (code, X86_SSE_DIV, ins->sreg1, ins->sreg2);
4609 x86_sse_alu_pd_reg_reg (code, X86_SSE_MUL, ins->sreg1, ins->sreg2);
4612 x86_sse_alu_pd_reg_reg (code, X86_SSE_SUB, ins->sreg1, ins->sreg2);
4615 x86_sse_alu_pd_reg_reg (code, X86_SSE_MAX, ins->sreg1, ins->sreg2);
4618 x86_sse_alu_pd_reg_reg (code, X86_SSE_MIN, ins->sreg1, ins->sreg2);
4621 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
4622 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_COMP, ins->sreg1, ins->sreg2, ins->inst_c0);
4625 x86_sse_alu_pd_reg_reg (code, X86_SSE_AND, ins->sreg1, ins->sreg2);
4628 x86_sse_alu_pd_reg_reg (code, X86_SSE_ANDN, ins->sreg1, ins->sreg2);
4631 x86_sse_alu_pd_reg_reg (code, X86_SSE_OR, ins->sreg1, ins->sreg2);
4634 x86_sse_alu_pd_reg_reg (code, X86_SSE_XOR, ins->sreg1, ins->sreg2);
4637 x86_sse_alu_pd_reg_reg (code, X86_SSE_SQRT, ins->dreg, ins->sreg1);
4640 x86_sse_alu_pd_reg_reg (code, X86_SSE_ADDSUB, ins->sreg1, ins->sreg2);
4643 x86_sse_alu_pd_reg_reg (code, X86_SSE_HADD, ins->sreg1, ins->sreg2);
4646 x86_sse_alu_pd_reg_reg (code, X86_SSE_HSUB, ins->sreg1, ins->sreg2);
4649 x86_sse_alu_sd_reg_reg (code, X86_SSE_MOVDDUP, ins->dreg, ins->sreg1);
4652 case OP_EXTRACT_MASK:
4653 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMOVMSKB, ins->dreg, ins->sreg1);
4657 x86_sse_alu_pd_reg_reg (code, X86_SSE_PAND, ins->sreg1, ins->sreg2);
4660 x86_sse_alu_pd_reg_reg (code, X86_SSE_POR, ins->sreg1, ins->sreg2);
4663 x86_sse_alu_pd_reg_reg (code, X86_SSE_PXOR, ins->sreg1, ins->sreg2);
4667 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDB, ins->sreg1, ins->sreg2);
4670 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDW, ins->sreg1, ins->sreg2);
4673 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDD, ins->sreg1, ins->sreg2);
4676 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDQ, ins->sreg1, ins->sreg2);
4680 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBB, ins->sreg1, ins->sreg2);
4683 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBW, ins->sreg1, ins->sreg2);
4686 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBD, ins->sreg1, ins->sreg2);
4689 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBQ, ins->sreg1, ins->sreg2);
4693 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMAXUB, ins->sreg1, ins->sreg2);
4696 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXUW, ins->sreg1, ins->sreg2);
4699 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXUD, ins->sreg1, ins->sreg2);
4703 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXSB, ins->sreg1, ins->sreg2);
4706 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMAXSW, ins->sreg1, ins->sreg2);
4709 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXSD, ins->sreg1, ins->sreg2);
4713 x86_sse_alu_pd_reg_reg (code, X86_SSE_PAVGB, ins->sreg1, ins->sreg2);
4716 x86_sse_alu_pd_reg_reg (code, X86_SSE_PAVGW, ins->sreg1, ins->sreg2);
4720 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMINUB, ins->sreg1, ins->sreg2);
4723 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINUW, ins->sreg1, ins->sreg2);
4726 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINUD, ins->sreg1, ins->sreg2);
4730 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINSB, ins->sreg1, ins->sreg2);
4733 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMINSW, ins->sreg1, ins->sreg2);
4736 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINSD, ins->sreg1, ins->sreg2);
4740 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQB, ins->sreg1, ins->sreg2);
4743 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQW, ins->sreg1, ins->sreg2);
4746 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQD, ins->sreg1, ins->sreg2);
4749 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PCMPEQQ, ins->sreg1, ins->sreg2);
4753 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPGTB, ins->sreg1, ins->sreg2);
4756 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPGTW, ins->sreg1, ins->sreg2);
4759 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPGTD, ins->sreg1, ins->sreg2);
4762 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PCMPGTQ, ins->sreg1, ins->sreg2);
4765 case OP_PSUM_ABS_DIFF:
4766 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSADBW, ins->sreg1, ins->sreg2);
4769 case OP_UNPACK_LOWB:
4770 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLBW, ins->sreg1, ins->sreg2);
4772 case OP_UNPACK_LOWW:
4773 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLWD, ins->sreg1, ins->sreg2);
4775 case OP_UNPACK_LOWD:
4776 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLDQ, ins->sreg1, ins->sreg2);
4778 case OP_UNPACK_LOWQ:
4779 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLQDQ, ins->sreg1, ins->sreg2);
4781 case OP_UNPACK_LOWPS:
4782 x86_sse_alu_ps_reg_reg (code, X86_SSE_UNPCKL, ins->sreg1, ins->sreg2);
4784 case OP_UNPACK_LOWPD:
4785 x86_sse_alu_pd_reg_reg (code, X86_SSE_UNPCKL, ins->sreg1, ins->sreg2);
4788 case OP_UNPACK_HIGHB:
4789 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHBW, ins->sreg1, ins->sreg2);
4791 case OP_UNPACK_HIGHW:
4792 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHWD, ins->sreg1, ins->sreg2);
4794 case OP_UNPACK_HIGHD:
4795 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHDQ, ins->sreg1, ins->sreg2);
4797 case OP_UNPACK_HIGHQ:
4798 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHQDQ, ins->sreg1, ins->sreg2);
4800 case OP_UNPACK_HIGHPS:
4801 x86_sse_alu_ps_reg_reg (code, X86_SSE_UNPCKH, ins->sreg1, ins->sreg2);
4803 case OP_UNPACK_HIGHPD:
4804 x86_sse_alu_pd_reg_reg (code, X86_SSE_UNPCKH, ins->sreg1, ins->sreg2);
4808 x86_sse_alu_pd_reg_reg (code, X86_SSE_PACKSSWB, ins->sreg1, ins->sreg2);
4811 x86_sse_alu_pd_reg_reg (code, X86_SSE_PACKSSDW, ins->sreg1, ins->sreg2);
4814 x86_sse_alu_pd_reg_reg (code, X86_SSE_PACKUSWB, ins->sreg1, ins->sreg2);
4817 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PACKUSDW, ins->sreg1, ins->sreg2);
4820 case OP_PADDB_SAT_UN:
4821 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDUSB, ins->sreg1, ins->sreg2);
4823 case OP_PSUBB_SAT_UN:
4824 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBUSB, ins->sreg1, ins->sreg2);
4826 case OP_PADDW_SAT_UN:
4827 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDUSW, ins->sreg1, ins->sreg2);
4829 case OP_PSUBW_SAT_UN:
4830 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBUSW, ins->sreg1, ins->sreg2);
4834 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDSB, ins->sreg1, ins->sreg2);
4837 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBSB, ins->sreg1, ins->sreg2);
4840 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDSW, ins->sreg1, ins->sreg2);
4843 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBSW, ins->sreg1, ins->sreg2);
4847 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULLW, ins->sreg1, ins->sreg2);
4850 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMULLD, ins->sreg1, ins->sreg2);
4853 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULUDQ, ins->sreg1, ins->sreg2);
4855 case OP_PMULW_HIGH_UN:
4856 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULHUW, ins->sreg1, ins->sreg2);
4859 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULHW, ins->sreg1, ins->sreg2);
4863 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTW, X86_SSE_SHR, ins->dreg, ins->inst_imm);
4866 x86_sse_shift_reg_reg (code, X86_SSE_PSRLW_REG, ins->dreg, ins->sreg2);
4870 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTW, X86_SSE_SAR, ins->dreg, ins->inst_imm);
4873 x86_sse_shift_reg_reg (code, X86_SSE_PSRAW_REG, ins->dreg, ins->sreg2);
4877 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTW, X86_SSE_SHL, ins->dreg, ins->inst_imm);
4880 x86_sse_shift_reg_reg (code, X86_SSE_PSLLW_REG, ins->dreg, ins->sreg2);
4884 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTD, X86_SSE_SHR, ins->dreg, ins->inst_imm);
4887 x86_sse_shift_reg_reg (code, X86_SSE_PSRLD_REG, ins->dreg, ins->sreg2);
4891 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTD, X86_SSE_SAR, ins->dreg, ins->inst_imm);
4894 x86_sse_shift_reg_reg (code, X86_SSE_PSRAD_REG, ins->dreg, ins->sreg2);
4898 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTD, X86_SSE_SHL, ins->dreg, ins->inst_imm);
4901 x86_sse_shift_reg_reg (code, X86_SSE_PSLLD_REG, ins->dreg, ins->sreg2);
4905 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTQ, X86_SSE_SHR, ins->dreg, ins->inst_imm);
4908 x86_sse_shift_reg_reg (code, X86_SSE_PSRLQ_REG, ins->dreg, ins->sreg2);
4912 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTQ, X86_SSE_SHL, ins->dreg, ins->inst_imm);
4915 x86_sse_shift_reg_reg (code, X86_SSE_PSLLQ_REG, ins->dreg, ins->sreg2);
4919 x86_movd_xreg_reg (code, ins->dreg, ins->sreg1);
4922 x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
4926 x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
4928 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_c0 * 8);
4929 x86_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I1, FALSE);
4933 x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
4935 x86_shift_reg_imm (code, X86_SHR, ins->dreg, 16);
4936 x86_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I2, TRUE);
4940 x86_sse_alu_pd_membase_reg (code, X86_SSE_MOVHPD_MEMBASE_REG, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, ins->sreg1);
4942 x86_sse_alu_sd_membase_reg (code, X86_SSE_MOVSD_MEMBASE_REG, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, ins->sreg1);
4943 x86_fld_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE);
4947 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->sreg1, ins->sreg2, ins->inst_c0);
4949 case OP_EXTRACTX_U2:
4950 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PEXTRW, ins->dreg, ins->sreg1, ins->inst_c0);
4952 case OP_INSERTX_U1_SLOW:
4953 /*sreg1 is the extracted ireg (scratch)
4954 /sreg2 is the to be inserted ireg (scratch)
4955 /dreg is the xreg to receive the value*/
4957 /*clear the bits from the extracted word*/
4958 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_c0 & 1 ? 0x00FF : 0xFF00);
4959 /*shift the value to insert if needed*/
4960 if (ins->inst_c0 & 1)
4961 x86_shift_reg_imm (code, X86_SHL, ins->sreg2, 8);
4962 /*join them together*/
4963 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
4964 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, ins->inst_c0 / 2);
4966 case OP_INSERTX_I4_SLOW:
4967 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg2, ins->inst_c0 * 2);
4968 x86_shift_reg_imm (code, X86_SHR, ins->sreg2, 16);
4969 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg2, ins->inst_c0 * 2 + 1);
4972 case OP_INSERTX_R4_SLOW:
4973 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE, TRUE);
4974 /*TODO if inst_c0 == 0 use movss*/
4975 x86_sse_alu_pd_reg_membase_imm (code, X86_SSE_PINSRW, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset + 0, ins->inst_c0 * 2);
4976 x86_sse_alu_pd_reg_membase_imm (code, X86_SSE_PINSRW, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset + 2, ins->inst_c0 * 2 + 1);
4978 case OP_INSERTX_R8_SLOW:
4979 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
4980 if (cfg->verbose_level)
4981 printf ("CONVERTING a OP_INSERTX_R8_SLOW %d offset %x\n", ins->inst_c0, offset);
4983 x86_sse_alu_pd_reg_membase (code, X86_SSE_MOVHPD_REG_MEMBASE, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
4985 x86_movsd_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
4988 case OP_STOREX_MEMBASE_REG:
4989 case OP_STOREX_MEMBASE:
4990 x86_movups_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
4992 case OP_LOADX_MEMBASE:
4993 x86_movups_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
4995 case OP_LOADX_ALIGNED_MEMBASE:
4996 x86_movaps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
4998 case OP_STOREX_ALIGNED_MEMBASE_REG:
4999 x86_movaps_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
5001 case OP_STOREX_NTA_MEMBASE_REG:
5002 x86_sse_alu_reg_membase (code, X86_SSE_MOVNTPS, ins->dreg, ins->sreg1, ins->inst_offset);
5004 case OP_PREFETCH_MEMBASE:
5005 x86_sse_alu_reg_membase (code, X86_SSE_PREFETCH, ins->backend.arg_info, ins->sreg1, ins->inst_offset);
5009 /*FIXME the peephole pass should have killed this*/
5010 if (ins->dreg != ins->sreg1)
5011 x86_movaps_reg_reg (code, ins->dreg, ins->sreg1);
5014 x86_sse_alu_pd_reg_reg (code, X86_SSE_PXOR, ins->dreg, ins->dreg);
5016 case OP_ICONV_TO_R8_RAW:
5017 x86_mov_membase_reg (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, ins->sreg1, 4);
5018 x86_fld_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE);
5021 case OP_FCONV_TO_R8_X:
5022 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
5023 x86_movsd_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
5026 case OP_XCONV_R8_TO_I4:
5027 x86_cvttsd2si (code, ins->dreg, ins->sreg1);
5028 switch (ins->backend.source_opcode) {
5029 case OP_FCONV_TO_I1:
5030 x86_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
5032 case OP_FCONV_TO_U1:
5033 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
5035 case OP_FCONV_TO_I2:
5036 x86_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
5038 case OP_FCONV_TO_U2:
5039 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
5045 /*FIXME this causes a partial register stall, maybe it would not be that bad to use shift + mask + or*/
5046 /*The +4 is to get a mov ?h, ?l over the same reg.*/
5047 x86_mov_reg_reg (code, ins->dreg + 4, ins->dreg, 1);
5048 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 0);
5049 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 1);
5050 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
5053 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 0);
5054 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 1);
5055 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
5058 x86_movd_xreg_reg (code, ins->dreg, ins->sreg1);
5059 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
5062 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE, TRUE);
5063 x86_movd_xreg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
5064 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
5067 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
5068 x86_movsd_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
5069 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0x44);
5073 x86_sse_alu_ss_reg_reg (code, X86_SSE_CVTDQ2PD, ins->dreg, ins->sreg1);
5076 x86_sse_alu_ps_reg_reg (code, X86_SSE_CVTDQ2PS, ins->dreg, ins->sreg1);
5079 x86_sse_alu_sd_reg_reg (code, X86_SSE_CVTPD2DQ, ins->dreg, ins->sreg1);
5082 x86_sse_alu_pd_reg_reg (code, X86_SSE_CVTPD2PS, ins->dreg, ins->sreg1);
5085 x86_sse_alu_pd_reg_reg (code, X86_SSE_CVTPS2DQ, ins->dreg, ins->sreg1);
5088 x86_sse_alu_ps_reg_reg (code, X86_SSE_CVTPS2PD, ins->dreg, ins->sreg1);
5091 x86_sse_alu_pd_reg_reg (code, X86_SSE_CVTTPD2DQ, ins->dreg, ins->sreg1);
5094 x86_sse_alu_ss_reg_reg (code, X86_SSE_CVTTPS2DQ, ins->dreg, ins->sreg1);
5098 case OP_LIVERANGE_START: {
5099 if (cfg->verbose_level > 1)
5100 printf ("R%d START=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
5101 MONO_VARINFO (cfg, ins->inst_c0)->live_range_start = code - cfg->native_code;
5104 case OP_LIVERANGE_END: {
5105 if (cfg->verbose_level > 1)
5106 printf ("R%d END=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
5107 MONO_VARINFO (cfg, ins->inst_c0)->live_range_end = code - cfg->native_code;
5110 case OP_NACL_GC_SAFE_POINT: {
5111 #if defined(__native_client_codegen__) && defined(__native_client_gc__)
5112 if (cfg->compile_aot)
5113 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)mono_nacl_gc);
5117 x86_test_mem_imm8 (code, (gpointer)&__nacl_thread_suspension_needed, 0xFFFFFFFF);
5118 br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
5119 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)mono_nacl_gc);
5120 x86_patch (br[0], code);
5125 case OP_GC_LIVENESS_DEF:
5126 case OP_GC_LIVENESS_USE:
5127 case OP_GC_PARAM_SLOT_LIVENESS_DEF:
5128 ins->backend.pc_offset = code - cfg->native_code;
5130 case OP_GC_SPILL_SLOT_LIVENESS_DEF:
5131 ins->backend.pc_offset = code - cfg->native_code;
5132 bb->spill_slot_defs = g_slist_prepend_mempool (cfg->mempool, bb->spill_slot_defs, ins);
5135 g_warning ("unknown opcode %s\n", mono_inst_name (ins->opcode));
5136 g_assert_not_reached ();
5139 if (G_UNLIKELY ((code - cfg->native_code - offset) > max_len)) {
5140 #ifndef __native_client_codegen__
5141 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
5142 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
5143 g_assert_not_reached ();
5144 #endif /* __native_client_codegen__ */
5150 cfg->code_len = code - cfg->native_code;
5153 #endif /* DISABLE_JIT */
5156 mono_arch_register_lowlevel_calls (void)
5161 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, MonoCodeManager *dyn_code_mp, gboolean run_cctors)
5163 MonoJumpInfo *patch_info;
5164 gboolean compile_aot = !run_cctors;
5166 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
5167 unsigned char *ip = patch_info->ip.i + code;
5168 const unsigned char *target;
5171 switch (patch_info->type) {
5172 case MONO_PATCH_INFO_BB:
5173 case MONO_PATCH_INFO_LABEL:
5176 /* No need to patch these */
5181 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
5183 switch (patch_info->type) {
5184 case MONO_PATCH_INFO_IP:
5185 *((gconstpointer *)(ip)) = target;
5187 case MONO_PATCH_INFO_CLASS_INIT: {
5189 /* Might already been changed to a nop */
5190 x86_call_code (code, 0);
5191 x86_patch (ip, target);
5194 case MONO_PATCH_INFO_ABS:
5195 case MONO_PATCH_INFO_METHOD:
5196 case MONO_PATCH_INFO_METHOD_JUMP:
5197 case MONO_PATCH_INFO_INTERNAL_METHOD:
5198 case MONO_PATCH_INFO_BB:
5199 case MONO_PATCH_INFO_LABEL:
5200 case MONO_PATCH_INFO_RGCTX_FETCH:
5201 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
5202 case MONO_PATCH_INFO_MONITOR_ENTER:
5203 case MONO_PATCH_INFO_MONITOR_EXIT:
5204 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
5205 #if defined(__native_client_codegen__) && defined(__native_client__)
5206 if (nacl_is_code_address (code)) {
5207 /* For tail calls, code is patched after being installed */
5208 /* but not through the normal "patch callsite" method. */
5209 unsigned char buf[kNaClAlignment];
5210 unsigned char *aligned_code = (uintptr_t)code & ~kNaClAlignmentMask;
5211 unsigned char *_target = target;
5213 /* All patch targets modified in x86_patch */
5214 /* are IP relative. */
5215 _target = _target + (uintptr_t)buf - (uintptr_t)aligned_code;
5216 memcpy (buf, aligned_code, kNaClAlignment);
5217 /* Patch a temp buffer of bundle size, */
5218 /* then install to actual location. */
5219 x86_patch (buf + ((uintptr_t)code - (uintptr_t)aligned_code), _target);
5220 ret = nacl_dyncode_modify (aligned_code, buf, kNaClAlignment);
5221 g_assert (ret == 0);
5224 x86_patch (ip, target);
5227 x86_patch (ip, target);
5230 case MONO_PATCH_INFO_NONE:
5232 case MONO_PATCH_INFO_R4:
5233 case MONO_PATCH_INFO_R8: {
5234 guint32 offset = mono_arch_get_patch_offset (ip);
5235 *((gconstpointer *)(ip + offset)) = target;
5239 guint32 offset = mono_arch_get_patch_offset (ip);
5240 #if !defined(__native_client__)
5241 *((gconstpointer *)(ip + offset)) = target;
5243 *((gconstpointer *)(ip + offset)) = nacl_modify_patch_target (target);
5251 static G_GNUC_UNUSED void
5252 stack_unaligned (MonoMethod *m, gpointer caller)
5254 printf ("%s\n", mono_method_full_name (m, TRUE));
5255 g_assert_not_reached ();
5259 mono_arch_emit_prolog (MonoCompile *cfg)
5261 MonoMethod *method = cfg->method;
5263 MonoMethodSignature *sig;
5265 int alloc_size, pos, max_offset, i, cfa_offset;
5267 gboolean need_stack_frame;
5268 #ifdef __native_client_codegen__
5269 guint alignment_check;
5272 cfg->code_size = MAX (cfg->header->code_size * 4, 10240);
5274 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
5275 cfg->code_size += 512;
5277 #if defined(__default_codegen__)
5278 code = cfg->native_code = g_malloc (cfg->code_size);
5279 #elif defined(__native_client_codegen__)
5280 /* native_code_alloc is not 32-byte aligned, native_code is. */
5281 cfg->code_size = NACL_BUNDLE_ALIGN_UP (cfg->code_size);
5282 cfg->native_code_alloc = g_malloc (cfg->code_size + kNaClAlignment);
5284 /* Align native_code to next nearest kNaclAlignment byte. */
5285 cfg->native_code = (guint)cfg->native_code_alloc + kNaClAlignment;
5286 cfg->native_code = (guint)cfg->native_code & ~kNaClAlignmentMask;
5288 code = cfg->native_code;
5290 alignment_check = (guint)cfg->native_code & kNaClAlignmentMask;
5291 g_assert(alignment_check == 0);
5298 /* Check that the stack is aligned on osx */
5299 x86_mov_reg_reg (code, X86_EAX, X86_ESP, sizeof (mgreg_t));
5300 x86_alu_reg_imm (code, X86_AND, X86_EAX, 15);
5301 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0xc);
5303 x86_branch_disp (code, X86_CC_Z, 0, FALSE);
5304 x86_push_membase (code, X86_ESP, 0);
5305 x86_push_imm (code, cfg->method);
5306 x86_mov_reg_imm (code, X86_EAX, stack_unaligned);
5307 x86_call_reg (code, X86_EAX);
5308 x86_patch (br [0], code);
5312 /* Offset between RSP and the CFA */
5316 cfa_offset = sizeof (gpointer);
5317 mono_emit_unwind_op_def_cfa (cfg, code, X86_ESP, sizeof (gpointer));
5318 // IP saved at CFA - 4
5319 /* There is no IP reg on x86 */
5320 mono_emit_unwind_op_offset (cfg, code, X86_NREG, -cfa_offset);
5321 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
5323 need_stack_frame = needs_stack_frame (cfg);
5325 if (need_stack_frame) {
5326 x86_push_reg (code, X86_EBP);
5327 cfa_offset += sizeof (gpointer);
5328 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
5329 mono_emit_unwind_op_offset (cfg, code, X86_EBP, - cfa_offset);
5330 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
5331 mono_emit_unwind_op_def_cfa_reg (cfg, code, X86_EBP);
5332 /* These are handled automatically by the stack marking code */
5333 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
5335 cfg->frame_reg = X86_ESP;
5338 alloc_size = cfg->stack_offset;
5341 if (!method->save_lmf) {
5342 for (i = 0; i < X86_NREG; ++i) {
5343 if ((cfg->used_int_regs & X86_CALLER_REGS & (1 << i)) && (i != X86_EBP)) {
5344 x86_push_reg (code, i);
5346 cfa_offset += sizeof (gpointer);
5347 mono_emit_unwind_op_offset (cfg, code, i, - cfa_offset);
5348 /* These are handled automatically by the stack marking code */
5349 mini_gc_set_slot_type_from_cfa (cfg, - cfa_offset, SLOT_NOREF);
5356 /* the original alloc_size is already aligned: there is %ebp and retip pushed, so realign */
5357 if (mono_do_x86_stack_align && need_stack_frame) {
5358 int tot = alloc_size + pos + 4; /* ret ip */
5359 if (need_stack_frame)
5361 tot &= MONO_ARCH_FRAME_ALIGNMENT - 1;
5363 alloc_size += MONO_ARCH_FRAME_ALIGNMENT - tot;
5364 for (i = 0; i < MONO_ARCH_FRAME_ALIGNMENT - tot; i += sizeof (mgreg_t))
5365 mini_gc_set_slot_type_from_fp (cfg, - (alloc_size + pos - i), SLOT_NOREF);
5369 cfg->arch.sp_fp_offset = alloc_size + pos;
5372 /* See mono_emit_stack_alloc */
5373 #if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
5374 guint32 remaining_size = alloc_size;
5375 /*FIXME handle unbounded code expansion, we should use a loop in case of more than X interactions*/
5376 guint32 required_code_size = ((remaining_size / 0x1000) + 1) * 8; /*8 is the max size of x86_alu_reg_imm + x86_test_membase_reg*/
5377 guint32 offset = code - cfg->native_code;
5378 if (G_UNLIKELY (required_code_size >= (cfg->code_size - offset))) {
5379 while (required_code_size >= (cfg->code_size - offset))
5380 cfg->code_size *= 2;
5381 cfg->native_code = mono_realloc_native_code(cfg);
5382 code = cfg->native_code + offset;
5383 cfg->stat_code_reallocs++;
5385 while (remaining_size >= 0x1000) {
5386 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
5387 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
5388 remaining_size -= 0x1000;
5391 x86_alu_reg_imm (code, X86_SUB, X86_ESP, remaining_size);
5393 x86_alu_reg_imm (code, X86_SUB, X86_ESP, alloc_size);
5396 g_assert (need_stack_frame);
5399 if (cfg->method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED ||
5400 cfg->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
5401 x86_alu_reg_imm (code, X86_AND, X86_ESP, -MONO_ARCH_FRAME_ALIGNMENT);
5404 #if DEBUG_STACK_ALIGNMENT
5405 /* check the stack is aligned */
5406 if (need_stack_frame && method->wrapper_type == MONO_WRAPPER_NONE) {
5407 x86_mov_reg_reg (code, X86_ECX, X86_ESP, 4);
5408 x86_alu_reg_imm (code, X86_AND, X86_ECX, MONO_ARCH_FRAME_ALIGNMENT - 1);
5409 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
5410 x86_branch_disp (code, X86_CC_EQ, 3, FALSE);
5411 x86_breakpoint (code);
5415 /* compute max_offset in order to use short forward jumps */
5417 if (cfg->opt & MONO_OPT_BRANCH) {
5418 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5420 bb->max_offset = max_offset;
5422 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
5424 /* max alignment for loops */
5425 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
5426 max_offset += LOOP_ALIGNMENT;
5427 #ifdef __native_client_codegen__
5428 /* max alignment for native client */
5429 if (bb->flags & BB_INDIRECT_JUMP_TARGET || bb->flags & BB_EXCEPTION_HANDLER)
5430 max_offset += kNaClAlignment;
5432 MONO_BB_FOR_EACH_INS (bb, ins) {
5433 if (ins->opcode == OP_LABEL)
5434 ins->inst_c1 = max_offset;
5435 #ifdef __native_client_codegen__
5436 switch (ins->opcode)
5448 case OP_VOIDCALL_REG:
5450 case OP_FCALL_MEMBASE:
5451 case OP_LCALL_MEMBASE:
5452 case OP_VCALL_MEMBASE:
5453 case OP_VCALL2_MEMBASE:
5454 case OP_VOIDCALL_MEMBASE:
5455 case OP_CALL_MEMBASE:
5456 max_offset += kNaClAlignment;
5459 max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN] - 1;
5462 #endif /* __native_client_codegen__ */
5463 max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
5468 /* store runtime generic context */
5469 if (cfg->rgctx_var) {
5470 g_assert (cfg->rgctx_var->opcode == OP_REGOFFSET && cfg->rgctx_var->inst_basereg == X86_EBP);
5472 x86_mov_membase_reg (code, X86_EBP, cfg->rgctx_var->inst_offset, MONO_ARCH_RGCTX_REG, 4);
5475 if (method->save_lmf) {
5476 code = emit_setup_lmf (cfg, code, cfg->lmf_var->inst_offset, cfa_offset);
5477 code = emit_push_lmf (cfg, code, cfg->lmf_var->inst_offset);
5480 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
5481 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
5483 /* load arguments allocated to register from the stack */
5484 sig = mono_method_signature (method);
5487 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
5488 inst = cfg->args [pos];
5489 if (inst->opcode == OP_REGVAR) {
5490 g_assert (need_stack_frame);
5491 x86_mov_reg_membase (code, inst->dreg, X86_EBP, inst->inst_offset, 4);
5492 if (cfg->verbose_level > 2)
5493 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
5498 cfg->code_len = code - cfg->native_code;
5500 g_assert (cfg->code_len < cfg->code_size);
5506 mono_arch_emit_epilog (MonoCompile *cfg)
5508 MonoMethod *method = cfg->method;
5509 MonoMethodSignature *sig = mono_method_signature (method);
5511 guint32 stack_to_pop;
5513 int max_epilog_size = 16;
5515 gboolean need_stack_frame = needs_stack_frame (cfg);
5517 if (cfg->method->save_lmf)
5518 max_epilog_size += 128;
5520 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
5521 cfg->code_size *= 2;
5522 cfg->native_code = mono_realloc_native_code(cfg);
5523 cfg->stat_code_reallocs++;
5526 code = cfg->native_code + cfg->code_len;
5528 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
5529 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
5531 /* the code restoring the registers must be kept in sync with OP_TAILCALL */
5534 if (method->save_lmf) {
5535 gint32 lmf_offset = cfg->lmf_var->inst_offset;
5537 gboolean supported = FALSE;
5539 if (cfg->compile_aot) {
5540 #if defined(__APPLE__) || defined(__linux__)
5543 } else if (mono_get_jit_tls_offset () != -1) {
5547 /* check if we need to restore protection of the stack after a stack overflow */
5549 if (cfg->compile_aot) {
5550 code = emit_load_aotconst (NULL, code, cfg, NULL, X86_ECX, MONO_PATCH_INFO_TLS_OFFSET, GINT_TO_POINTER (TLS_KEY_JIT_TLS));
5552 code = emit_tls_get_reg (code, X86_ECX, X86_ECX);
5554 code = mono_x86_emit_tls_get (code, X86_ECX, mono_get_jit_tls_offset ());
5557 /* we load the value in a separate instruction: this mechanism may be
5558 * used later as a safer way to do thread interruption
5560 x86_mov_reg_membase (code, X86_ECX, X86_ECX, G_STRUCT_OFFSET (MonoJitTlsData, restore_stack_prot), 4);
5561 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
5563 x86_branch8 (code, X86_CC_Z, 0, FALSE);
5564 /* note that the call trampoline will preserve eax/edx */
5565 x86_call_reg (code, X86_ECX);
5566 x86_patch (patch, code);
5568 /* FIXME: maybe save the jit tls in the prolog */
5571 code = emit_pop_lmf (cfg, code, lmf_offset);
5573 /* restore caller saved regs */
5574 if (cfg->used_int_regs & (1 << X86_EBX)) {
5575 x86_mov_reg_membase (code, X86_EBX, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebx), 4);
5578 if (cfg->used_int_regs & (1 << X86_EDI)) {
5579 x86_mov_reg_membase (code, X86_EDI, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, edi), 4);
5581 if (cfg->used_int_regs & (1 << X86_ESI)) {
5582 x86_mov_reg_membase (code, X86_ESI, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esi), 4);
5585 /* EBP is restored by LEAVE */
5587 for (i = 0; i < X86_NREG; ++i) {
5588 if ((cfg->used_int_regs & X86_CALLER_REGS & (1 << i)) && (i != X86_EBP)) {
5594 g_assert (need_stack_frame);
5595 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
5598 for (i = X86_NREG - 1; i >= 0; --i) {
5599 if ((cfg->used_int_regs & X86_CALLER_REGS & (1 << i)) && (i != X86_EBP))
5600 x86_pop_reg (code, i);
5604 /* Load returned vtypes into registers if needed */
5605 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
5606 if (cinfo->ret.storage == ArgValuetypeInReg) {
5607 for (quad = 0; quad < 2; quad ++) {
5608 switch (cinfo->ret.pair_storage [quad]) {
5610 x86_mov_reg_membase (code, cinfo->ret.pair_regs [quad], cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), 4);
5612 case ArgOnFloatFpStack:
5613 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), FALSE);
5615 case ArgOnDoubleFpStack:
5616 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), TRUE);
5621 g_assert_not_reached ();
5626 if (need_stack_frame)
5629 if (CALLCONV_IS_STDCALL (sig)) {
5630 MonoJitArgumentInfo *arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
5632 stack_to_pop = mono_arch_get_argument_info (NULL, sig, sig->param_count, arg_info);
5633 } else if (cinfo->vtype_retaddr)
5639 g_assert (need_stack_frame);
5640 x86_ret_imm (code, stack_to_pop);
5645 cfg->code_len = code - cfg->native_code;
5647 g_assert (cfg->code_len < cfg->code_size);
5651 mono_arch_emit_exceptions (MonoCompile *cfg)
5653 MonoJumpInfo *patch_info;
5656 MonoClass *exc_classes [16];
5657 guint8 *exc_throw_start [16], *exc_throw_end [16];
5661 /* Compute needed space */
5662 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5663 if (patch_info->type == MONO_PATCH_INFO_EXC)
5668 * make sure we have enough space for exceptions
5669 * 16 is the size of two push_imm instructions and a call
5671 if (cfg->compile_aot)
5672 code_size = exc_count * 32;
5674 code_size = exc_count * 16;
5676 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
5677 cfg->code_size *= 2;
5678 cfg->native_code = mono_realloc_native_code(cfg);
5679 cfg->stat_code_reallocs++;
5682 code = cfg->native_code + cfg->code_len;
5685 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5686 switch (patch_info->type) {
5687 case MONO_PATCH_INFO_EXC: {
5688 MonoClass *exc_class;
5692 x86_patch (patch_info->ip.i + cfg->native_code, code);
5694 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
5695 g_assert (exc_class);
5696 throw_ip = patch_info->ip.i;
5698 /* Find a throw sequence for the same exception class */
5699 for (i = 0; i < nthrows; ++i)
5700 if (exc_classes [i] == exc_class)
5703 x86_push_imm (code, (exc_throw_end [i] - cfg->native_code) - throw_ip);
5704 x86_jump_code (code, exc_throw_start [i]);
5705 patch_info->type = MONO_PATCH_INFO_NONE;
5710 /* Compute size of code following the push <OFFSET> */
5711 #if defined(__default_codegen__)
5713 #elif defined(__native_client_codegen__)
5714 code = mono_nacl_align (code);
5715 size = kNaClAlignment;
5717 /*This is aligned to 16 bytes by the callee. This way we save a few bytes here.*/
5719 if ((code - cfg->native_code) - throw_ip < 126 - size) {
5720 /* Use the shorter form */
5722 x86_push_imm (code, 0);
5726 x86_push_imm (code, 0xf0f0f0f0);
5731 exc_classes [nthrows] = exc_class;
5732 exc_throw_start [nthrows] = code;
5735 x86_push_imm (code, exc_class->type_token - MONO_TOKEN_TYPE_DEF);
5736 patch_info->data.name = "mono_arch_throw_corlib_exception";
5737 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
5738 patch_info->ip.i = code - cfg->native_code;
5739 x86_call_code (code, 0);
5740 x86_push_imm (buf, (code - cfg->native_code) - throw_ip);
5745 exc_throw_end [nthrows] = code;
5757 cfg->code_len = code - cfg->native_code;
5759 g_assert (cfg->code_len < cfg->code_size);
5763 mono_arch_flush_icache (guint8 *code, gint size)
5769 mono_arch_flush_register_windows (void)
5774 mono_arch_is_inst_imm (gint64 imm)
5780 mono_arch_finish_init (void)
5782 if (!g_getenv ("MONO_NO_TLS")) {
5785 * We need to init this multiple times, since when we are first called, the key might not
5786 * be initialized yet.
5788 appdomain_tls_offset = mono_domain_get_tls_key ();
5789 jit_tls_offset = mono_get_jit_tls_key ();
5791 /* Only 64 tls entries can be accessed using inline code */
5792 if (appdomain_tls_offset >= 64)
5793 appdomain_tls_offset = -1;
5794 if (jit_tls_offset >= 64)
5795 jit_tls_offset = -1;
5798 optimize_for_xen = access ("/proc/xen", F_OK) == 0;
5800 appdomain_tls_offset = mono_domain_get_tls_offset ();
5801 lmf_tls_offset = mono_get_lmf_tls_offset ();
5802 lmf_addr_tls_offset = mono_get_lmf_addr_tls_offset ();
5808 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
5812 #ifdef MONO_ARCH_HAVE_IMT
5814 // Linear handler, the bsearch head compare is shorter
5815 //[2 + 4] x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
5816 //[1 + 1] x86_branch8(inst,cond,imm,is_signed)
5817 // x86_patch(ins,target)
5818 //[1 + 5] x86_jump_mem(inst,mem)
5821 #if defined(__default_codegen__)
5822 #define BR_SMALL_SIZE 2
5823 #define BR_LARGE_SIZE 5
5824 #elif defined(__native_client_codegen__)
5825 /* I suspect the size calculation below is actually incorrect. */
5826 /* TODO: fix the calculation that uses these sizes. */
5827 #define BR_SMALL_SIZE 16
5828 #define BR_LARGE_SIZE 12
5829 #endif /*__native_client_codegen__*/
5830 #define JUMP_IMM_SIZE 6
5831 #define ENABLE_WRONG_METHOD_CHECK 0
5835 imt_branch_distance (MonoIMTCheckItem **imt_entries, int start, int target)
5837 int i, distance = 0;
5838 for (i = start; i < target; ++i)
5839 distance += imt_entries [i]->chunk_size;
5844 * LOCKING: called with the domain lock held
5847 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
5848 gpointer fail_tramp)
5852 guint8 *code, *start;
5854 for (i = 0; i < count; ++i) {
5855 MonoIMTCheckItem *item = imt_entries [i];
5856 if (item->is_equals) {
5857 if (item->check_target_idx) {
5858 if (!item->compare_done)
5859 item->chunk_size += CMP_SIZE;
5860 item->chunk_size += BR_SMALL_SIZE + JUMP_IMM_SIZE;
5863 item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + JUMP_IMM_SIZE * 2;
5865 item->chunk_size += JUMP_IMM_SIZE;
5866 #if ENABLE_WRONG_METHOD_CHECK
5867 item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
5872 item->chunk_size += CMP_SIZE + BR_LARGE_SIZE;
5873 imt_entries [item->check_target_idx]->compare_done = TRUE;
5875 size += item->chunk_size;
5877 #if defined(__native_client__) && defined(__native_client_codegen__)
5878 /* In Native Client, we don't re-use thunks, allocate from the */
5879 /* normal code manager paths. */
5880 size = NACL_BUNDLE_ALIGN_UP (size);
5881 code = mono_domain_code_reserve (domain, size);
5884 code = mono_method_alloc_generic_virtual_thunk (domain, size);
5886 code = mono_domain_code_reserve (domain, size);
5889 for (i = 0; i < count; ++i) {
5890 MonoIMTCheckItem *item = imt_entries [i];
5891 item->code_target = code;
5892 if (item->is_equals) {
5893 if (item->check_target_idx) {
5894 if (!item->compare_done)
5895 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5896 item->jmp_code = code;
5897 x86_branch8 (code, X86_CC_NE, 0, FALSE);
5898 if (item->has_target_code)
5899 x86_jump_code (code, item->value.target_code);
5901 x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
5904 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5905 item->jmp_code = code;
5906 x86_branch8 (code, X86_CC_NE, 0, FALSE);
5907 if (item->has_target_code)
5908 x86_jump_code (code, item->value.target_code);
5910 x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
5911 x86_patch (item->jmp_code, code);
5912 x86_jump_code (code, fail_tramp);
5913 item->jmp_code = NULL;
5915 /* enable the commented code to assert on wrong method */
5916 #if ENABLE_WRONG_METHOD_CHECK
5917 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5918 item->jmp_code = code;
5919 x86_branch8 (code, X86_CC_NE, 0, FALSE);
5921 if (item->has_target_code)
5922 x86_jump_code (code, item->value.target_code);
5924 x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
5925 #if ENABLE_WRONG_METHOD_CHECK
5926 x86_patch (item->jmp_code, code);
5927 x86_breakpoint (code);
5928 item->jmp_code = NULL;
5933 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5934 item->jmp_code = code;
5935 if (x86_is_imm8 (imt_branch_distance (imt_entries, i, item->check_target_idx)))
5936 x86_branch8 (code, X86_CC_GE, 0, FALSE);
5938 x86_branch32 (code, X86_CC_GE, 0, FALSE);
5941 /* patch the branches to get to the target items */
5942 for (i = 0; i < count; ++i) {
5943 MonoIMTCheckItem *item = imt_entries [i];
5944 if (item->jmp_code) {
5945 if (item->check_target_idx) {
5946 x86_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
5952 mono_stats.imt_thunks_size += code - start;
5953 g_assert (code - start <= size);
5957 char *buff = g_strdup_printf ("thunk_for_class_%s_%s_entries_%d", vtable->klass->name_space, vtable->klass->name, count);
5958 mono_disassemble_code (NULL, (guint8*)start, code - start, buff);
5962 if (mono_jit_map_is_enabled ()) {
5965 buff = g_strdup_printf ("imt_%s_%s_entries_%d", vtable->klass->name_space, vtable->klass->name, count);
5967 buff = g_strdup_printf ("imt_thunk_entries_%d", count);
5968 mono_emit_jit_tramp (start, code - start, buff);
5972 nacl_domain_code_validate (domain, &start, size, &code);
5978 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
5980 return (MonoMethod*) regs [MONO_ARCH_IMT_REG];
5985 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
5987 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
5991 mono_arch_get_cie_program (void)
5995 mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
5996 mono_add_unwind_op_offset (l, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
6002 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
6004 MonoInst *ins = NULL;
6007 if (cmethod->klass == mono_defaults.math_class) {
6008 if (strcmp (cmethod->name, "Sin") == 0) {
6010 } else if (strcmp (cmethod->name, "Cos") == 0) {
6012 } else if (strcmp (cmethod->name, "Tan") == 0) {
6014 } else if (strcmp (cmethod->name, "Atan") == 0) {
6016 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
6018 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
6020 } else if (strcmp (cmethod->name, "Round") == 0 && fsig->param_count == 1 && fsig->params [0]->type == MONO_TYPE_R8) {
6025 MONO_INST_NEW (cfg, ins, opcode);
6026 ins->type = STACK_R8;
6027 ins->dreg = mono_alloc_freg (cfg);
6028 ins->sreg1 = args [0]->dreg;
6029 MONO_ADD_INS (cfg->cbb, ins);
6032 if (cfg->opt & MONO_OPT_CMOV) {
6035 if (strcmp (cmethod->name, "Min") == 0) {
6036 if (fsig->params [0]->type == MONO_TYPE_I4)
6038 } else if (strcmp (cmethod->name, "Max") == 0) {
6039 if (fsig->params [0]->type == MONO_TYPE_I4)
6044 MONO_INST_NEW (cfg, ins, opcode);
6045 ins->type = STACK_I4;
6046 ins->dreg = mono_alloc_ireg (cfg);
6047 ins->sreg1 = args [0]->dreg;
6048 ins->sreg2 = args [1]->dreg;
6049 MONO_ADD_INS (cfg->cbb, ins);
6054 /* OP_FREM is not IEEE compatible */
6055 else if (strcmp (cmethod->name, "IEEERemainder") == 0) {
6056 MONO_INST_NEW (cfg, ins, OP_FREM);
6057 ins->inst_i0 = args [0];
6058 ins->inst_i1 = args [1];
6067 mono_arch_print_tree (MonoInst *tree, int arity)
6072 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
6078 if (appdomain_tls_offset == -1)
6081 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
6082 ins->inst_offset = appdomain_tls_offset;
6087 mono_arch_get_patch_offset (guint8 *code)
6089 if ((code [0] == 0x8b) && (x86_modrm_mod (code [1]) == 0x2))
6091 else if (code [0] == 0xba)
6093 else if (code [0] == 0x68)
6096 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x6))
6097 /* push <OFFSET>(<REG>) */
6099 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x2))
6100 /* call *<OFFSET>(<REG>) */
6102 else if ((code [0] == 0xdd) || (code [0] == 0xd9))
6105 else if ((code [0] == 0x58) && (code [1] == 0x05))
6106 /* pop %eax; add <OFFSET>, %eax */
6108 else if ((code [0] >= 0x58) && (code [0] <= 0x58 + X86_NREG) && (code [1] == 0x81))
6109 /* pop <REG>; add <OFFSET>, <REG> */
6111 else if ((code [0] >= 0xb8) && (code [0] < 0xb8 + 8))
6112 /* mov <REG>, imm */
6115 g_assert_not_reached ();
6121 * mono_breakpoint_clean_code:
6123 * Copy @size bytes from @code - @offset to the buffer @buf. If the debugger inserted software
6124 * breakpoints in the original code, they are removed in the copy.
6126 * Returns TRUE if no sw breakpoint was present.
6129 mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size)
6132 gboolean can_write = TRUE;
6134 * If method_start is non-NULL we need to perform bound checks, since we access memory
6135 * at code - offset we could go before the start of the method and end up in a different
6136 * page of memory that is not mapped or read incorrect data anyway. We zero-fill the bytes
6139 if (!method_start || code - offset >= method_start) {
6140 memcpy (buf, code - offset, size);
6142 int diff = code - method_start;
6143 memset (buf, 0, size);
6144 memcpy (buf + offset - diff, method_start, diff + size - offset);
6147 for (i = 0; i < MONO_BREAKPOINT_ARRAY_SIZE; ++i) {
6148 int idx = mono_breakpoint_info_index [i];
6152 ptr = mono_breakpoint_info [idx].address;
6153 if (ptr >= code && ptr < code + size) {
6154 guint8 saved_byte = mono_breakpoint_info [idx].saved_byte;
6156 /*g_print ("patching %p with 0x%02x (was: 0x%02x)\n", ptr, saved_byte, buf [ptr - code]);*/
6157 buf [ptr - code] = saved_byte;
6164 * mono_x86_get_this_arg_offset:
6166 * Return the offset of the stack location where this is passed during a virtual
6170 mono_x86_get_this_arg_offset (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig)
6176 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
6178 guint32 esp = regs [X86_ESP];
6179 CallInfo *cinfo = NULL;
6186 * The stack looks like:
6190 * <4 pointers pushed by mono_arch_create_trampoline_code ()>
6192 res = (((MonoObject**)esp) [5 + (offset / 4)]);
6198 #define MAX_ARCH_DELEGATE_PARAMS 10
6201 get_delegate_invoke_impl (gboolean has_target, guint32 param_count, guint32 *code_len)
6203 guint8 *code, *start;
6204 int code_reserve = 64;
6207 * The stack contains:
6213 start = code = mono_global_codeman_reserve (code_reserve);
6215 /* Replace the this argument with the target */
6216 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
6217 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoDelegate, target), 4);
6218 x86_mov_membase_reg (code, X86_ESP, 4, X86_ECX, 4);
6219 x86_jump_membase (code, X86_EAX, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
6221 g_assert ((code - start) < code_reserve);
6224 /* 8 for mov_reg and jump, plus 8 for each parameter */
6225 #ifdef __native_client_codegen__
6226 /* TODO: calculate this size correctly */
6227 code_reserve = 13 + (param_count * 8) + 2 * kNaClAlignment;
6229 code_reserve = 8 + (param_count * 8);
6230 #endif /* __native_client_codegen__ */
6232 * The stack contains:
6233 * <args in reverse order>
6238 * <args in reverse order>
6241 * without unbalancing the stack.
6242 * So move each arg up a spot in the stack (overwriting un-needed 'this' arg)
6243 * and leaving original spot of first arg as placeholder in stack so
6244 * when callee pops stack everything works.
6247 start = code = mono_global_codeman_reserve (code_reserve);
6249 /* store delegate for access to method_ptr */
6250 x86_mov_reg_membase (code, X86_ECX, X86_ESP, 4, 4);
6253 for (i = 0; i < param_count; ++i) {
6254 x86_mov_reg_membase (code, X86_EAX, X86_ESP, (i+2)*4, 4);
6255 x86_mov_membase_reg (code, X86_ESP, (i+1)*4, X86_EAX, 4);
6258 x86_jump_membase (code, X86_ECX, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
6260 g_assert ((code - start) < code_reserve);
6263 nacl_global_codeman_validate(&start, code_reserve, &code);
6264 mono_debug_add_delegate_trampoline (start, code - start);
6267 *code_len = code - start;
6269 if (mono_jit_map_is_enabled ()) {
6272 buff = (char*)"delegate_invoke_has_target";
6274 buff = g_strdup_printf ("delegate_invoke_no_target_%d", param_count);
6275 mono_emit_jit_tramp (start, code - start, buff);
6284 mono_arch_get_delegate_invoke_impls (void)
6292 code = get_delegate_invoke_impl (TRUE, 0, &code_len);
6293 res = g_slist_prepend (res, mono_tramp_info_create ("delegate_invoke_impl_has_target", code, code_len, NULL, NULL));
6295 for (i = 0; i < MAX_ARCH_DELEGATE_PARAMS; ++i) {
6296 code = get_delegate_invoke_impl (FALSE, i, &code_len);
6297 tramp_name = g_strdup_printf ("delegate_invoke_impl_target_%d", i);
6298 res = g_slist_prepend (res, mono_tramp_info_create (tramp_name, code, code_len, NULL, NULL));
6299 g_free (tramp_name);
6306 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
6308 guint8 *code, *start;
6310 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
6313 /* FIXME: Support more cases */
6314 if (MONO_TYPE_ISSTRUCT (sig->ret))
6318 * The stack contains:
6324 static guint8* cached = NULL;
6329 start = mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
6331 start = get_delegate_invoke_impl (TRUE, 0, NULL);
6333 mono_memory_barrier ();
6337 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
6340 for (i = 0; i < sig->param_count; ++i)
6341 if (!mono_is_regsize_var (sig->params [i]))
6344 code = cache [sig->param_count];
6348 if (mono_aot_only) {
6349 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
6350 start = mono_aot_get_trampoline (name);
6353 start = get_delegate_invoke_impl (FALSE, sig->param_count, NULL);
6356 mono_memory_barrier ();
6358 cache [sig->param_count] = start;
6365 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
6368 case X86_EAX: return ctx->eax;
6369 case X86_EBX: return ctx->ebx;
6370 case X86_ECX: return ctx->ecx;
6371 case X86_EDX: return ctx->edx;
6372 case X86_ESP: return ctx->esp;
6373 case X86_EBP: return ctx->ebp;
6374 case X86_ESI: return ctx->esi;
6375 case X86_EDI: return ctx->edi;
6377 g_assert_not_reached ();
6383 mono_arch_context_set_int_reg (MonoContext *ctx, int reg, mgreg_t val)
6411 g_assert_not_reached ();
6415 #ifdef MONO_ARCH_SIMD_INTRINSICS
6418 get_float_to_x_spill_area (MonoCompile *cfg)
6420 if (!cfg->fconv_to_r8_x_var) {
6421 cfg->fconv_to_r8_x_var = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
6422 cfg->fconv_to_r8_x_var->flags |= MONO_INST_VOLATILE; /*FIXME, use the don't regalloc flag*/
6424 return cfg->fconv_to_r8_x_var;
6428 * Convert all fconv opts that MONO_OPT_SSE2 would get wrong.
6431 mono_arch_decompose_opts (MonoCompile *cfg, MonoInst *ins)
6434 int dreg, src_opcode;
6436 if (!(cfg->opt & MONO_OPT_SSE2) || !(cfg->opt & MONO_OPT_SIMD) || COMPILE_LLVM (cfg))
6439 switch (src_opcode = ins->opcode) {
6440 case OP_FCONV_TO_I1:
6441 case OP_FCONV_TO_U1:
6442 case OP_FCONV_TO_I2:
6443 case OP_FCONV_TO_U2:
6444 case OP_FCONV_TO_I4:
6451 /* dreg is the IREG and sreg1 is the FREG */
6452 MONO_INST_NEW (cfg, fconv, OP_FCONV_TO_R8_X);
6453 fconv->klass = NULL; /*FIXME, what can I use here as the Mono.Simd lib might not be loaded yet*/
6454 fconv->sreg1 = ins->sreg1;
6455 fconv->dreg = mono_alloc_ireg (cfg);
6456 fconv->type = STACK_VTYPE;
6457 fconv->backend.spill_var = get_float_to_x_spill_area (cfg);
6459 mono_bblock_insert_before_ins (cfg->cbb, ins, fconv);
6463 ins->opcode = OP_XCONV_R8_TO_I4;
6465 ins->klass = mono_defaults.int32_class;
6466 ins->sreg1 = fconv->dreg;
6468 ins->type = STACK_I4;
6469 ins->backend.source_opcode = src_opcode;
6472 #endif /* #ifdef MONO_ARCH_SIMD_INTRINSICS */
6475 mono_arch_decompose_long_opts (MonoCompile *cfg, MonoInst *long_ins)
6480 if (long_ins->opcode == OP_LNEG) {
6482 MONO_EMIT_NEW_UNALU (cfg, OP_INEG, ins->dreg + 1, ins->sreg1 + 1);
6483 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADC_IMM, ins->dreg + 2, ins->sreg1 + 2, 0);
6484 MONO_EMIT_NEW_UNALU (cfg, OP_INEG, ins->dreg + 2, ins->dreg + 2);
6489 #ifdef MONO_ARCH_SIMD_INTRINSICS
6491 if (!(cfg->opt & MONO_OPT_SIMD))
6494 /*TODO move this to simd-intrinsic.c once we support sse 4.1 dword extractors since we need the runtime caps info */
6495 switch (long_ins->opcode) {
6497 vreg = long_ins->sreg1;
6499 if (long_ins->inst_c0) {
6500 MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
6501 ins->klass = long_ins->klass;
6502 ins->sreg1 = long_ins->sreg1;
6504 ins->type = STACK_VTYPE;
6505 ins->dreg = vreg = alloc_ireg (cfg);
6506 MONO_ADD_INS (cfg->cbb, ins);
6509 MONO_INST_NEW (cfg, ins, OP_EXTRACT_I4);
6510 ins->klass = mono_defaults.int32_class;
6512 ins->type = STACK_I4;
6513 ins->dreg = long_ins->dreg + 1;
6514 MONO_ADD_INS (cfg->cbb, ins);
6516 MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
6517 ins->klass = long_ins->klass;
6518 ins->sreg1 = long_ins->sreg1;
6519 ins->inst_c0 = long_ins->inst_c0 ? 3 : 1;
6520 ins->type = STACK_VTYPE;
6521 ins->dreg = vreg = alloc_ireg (cfg);
6522 MONO_ADD_INS (cfg->cbb, ins);
6524 MONO_INST_NEW (cfg, ins, OP_EXTRACT_I4);
6525 ins->klass = mono_defaults.int32_class;
6527 ins->type = STACK_I4;
6528 ins->dreg = long_ins->dreg + 2;
6529 MONO_ADD_INS (cfg->cbb, ins);
6531 long_ins->opcode = OP_NOP;
6533 case OP_INSERTX_I8_SLOW:
6534 MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
6535 ins->dreg = long_ins->dreg;
6536 ins->sreg1 = long_ins->dreg;
6537 ins->sreg2 = long_ins->sreg2 + 1;
6538 ins->inst_c0 = long_ins->inst_c0 * 2;
6539 MONO_ADD_INS (cfg->cbb, ins);
6541 MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
6542 ins->dreg = long_ins->dreg;
6543 ins->sreg1 = long_ins->dreg;
6544 ins->sreg2 = long_ins->sreg2 + 2;
6545 ins->inst_c0 = long_ins->inst_c0 * 2 + 1;
6546 MONO_ADD_INS (cfg->cbb, ins);
6548 long_ins->opcode = OP_NOP;
6551 MONO_INST_NEW (cfg, ins, OP_ICONV_TO_X);
6552 ins->dreg = long_ins->dreg;
6553 ins->sreg1 = long_ins->sreg1 + 1;
6554 ins->klass = long_ins->klass;
6555 ins->type = STACK_VTYPE;
6556 MONO_ADD_INS (cfg->cbb, ins);
6558 MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
6559 ins->dreg = long_ins->dreg;
6560 ins->sreg1 = long_ins->dreg;
6561 ins->sreg2 = long_ins->sreg1 + 2;
6563 ins->klass = long_ins->klass;
6564 ins->type = STACK_VTYPE;
6565 MONO_ADD_INS (cfg->cbb, ins);
6567 MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
6568 ins->dreg = long_ins->dreg;
6569 ins->sreg1 = long_ins->dreg;;
6570 ins->inst_c0 = 0x44; /*Magic number for swizzling (X,Y,X,Y)*/
6571 ins->klass = long_ins->klass;
6572 ins->type = STACK_VTYPE;
6573 MONO_ADD_INS (cfg->cbb, ins);
6575 long_ins->opcode = OP_NOP;
6578 #endif /* MONO_ARCH_SIMD_INTRINSICS */
6581 /*MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD*/
6583 mono_arch_install_handler_block_guard (MonoJitInfo *ji, MonoJitExceptionInfo *clause, MonoContext *ctx, gpointer new_value)
6586 gpointer *sp, old_value;
6588 const unsigned char *handler;
6590 /*Decode the first instruction to figure out where did we store the spvar*/
6591 /*Our jit MUST generate the following:
6593 Which is encoded as: 0x89 mod_rm.
6594 mod_rm (esp, ebp, imm) which can be: (imm will never be zero)
6595 mod (reg + imm8): 01 reg(esp): 100 rm(ebp): 101 -> 01100101 (0x65)
6596 mod (reg + imm32): 10 reg(esp): 100 rm(ebp): 101 -> 10100101 (0xA5)
6598 handler = clause->handler_start;
6600 if (*handler != 0x89)
6605 if (*handler == 0x65)
6606 offset = *(signed char*)(handler + 1);
6607 else if (*handler == 0xA5)
6608 offset = *(int*)(handler + 1);
6613 bp = MONO_CONTEXT_GET_BP (ctx);
6614 sp = *(gpointer*)(bp + offset);
6617 if (old_value < ji->code_start || (char*)old_value > ((char*)ji->code_start + ji->code_size))
6626 * mono_aot_emit_load_got_addr:
6628 * Emit code to load the got address.
6629 * On x86, the result is placed into EBX.
6632 mono_arch_emit_load_got_addr (guint8 *start, guint8 *code, MonoCompile *cfg, MonoJumpInfo **ji)
6634 x86_call_imm (code, 0);
6636 * The patch needs to point to the pop, since the GOT offset needs
6637 * to be added to that address.
6640 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GOT_OFFSET, NULL);
6642 *ji = mono_patch_info_list_prepend (*ji, code - start, MONO_PATCH_INFO_GOT_OFFSET, NULL);
6643 x86_pop_reg (code, MONO_ARCH_GOT_REG);
6644 x86_alu_reg_imm (code, X86_ADD, MONO_ARCH_GOT_REG, 0xf0f0f0f0);
6650 emit_load_aotconst (guint8 *start, guint8 *code, MonoCompile *cfg, MonoJumpInfo **ji, int dreg, int tramp_type, gconstpointer target)
6653 mono_add_patch_info (cfg, code - cfg->native_code, tramp_type, target);
6655 g_assert_not_reached ();
6656 x86_mov_reg_membase (code, dreg, MONO_ARCH_GOT_REG, 0xf0f0f0f0, 4);
6661 * mono_arch_emit_load_aotconst:
6663 * Emit code to load the contents of the GOT slot identified by TRAMP_TYPE and
6664 * TARGET from the mscorlib GOT in full-aot code.
6665 * On x86, the GOT address is assumed to be in EBX, and the result is placed into
6669 mono_arch_emit_load_aotconst (guint8 *start, guint8 *code, MonoJumpInfo **ji, int tramp_type, gconstpointer target)
6671 /* Load the mscorlib got address */
6672 x86_mov_reg_membase (code, X86_EAX, MONO_ARCH_GOT_REG, sizeof (gpointer), 4);
6673 *ji = mono_patch_info_list_prepend (*ji, code - start, tramp_type, target);
6674 /* arch_emit_got_access () patches this */
6675 x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0xf0f0f0f0, 4);
6680 /* Can't put this into mini-x86.h */
6682 mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot);
6685 mono_arch_get_trampolines (gboolean aot)
6687 MonoTrampInfo *info;
6688 GSList *tramps = NULL;
6690 mono_x86_get_signal_exception_trampoline (&info, aot);
6692 tramps = g_slist_append (tramps, info);
6699 #define DBG_SIGNAL SIGBUS
6701 #define DBG_SIGNAL SIGSEGV
6704 /* Soft Debug support */
6705 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
6708 * mono_arch_set_breakpoint:
6710 * Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
6711 * The location should contain code emitted by OP_SEQ_POINT.
6714 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
6719 * In production, we will use int3 (has to fix the size in the md
6720 * file). But that could confuse gdb, so during development, we emit a SIGSEGV
6723 g_assert (code [0] == 0x90);
6724 x86_alu_reg_mem (code, X86_CMP, X86_EAX, (guint32)bp_trigger_page);
6728 * mono_arch_clear_breakpoint:
6730 * Clear the breakpoint at IP.
6733 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
6738 for (i = 0; i < 6; ++i)
6743 * mono_arch_start_single_stepping:
6745 * Start single stepping.
6748 mono_arch_start_single_stepping (void)
6750 mono_mprotect (ss_trigger_page, mono_pagesize (), 0);
6754 * mono_arch_stop_single_stepping:
6756 * Stop single stepping.
6759 mono_arch_stop_single_stepping (void)
6761 mono_mprotect (ss_trigger_page, mono_pagesize (), MONO_MMAP_READ);
6765 * mono_arch_is_single_step_event:
6767 * Return whenever the machine state in SIGCTX corresponds to a single
6771 mono_arch_is_single_step_event (void *info, void *sigctx)
6774 EXCEPTION_RECORD* einfo = ((EXCEPTION_POINTERS*)info)->ExceptionRecord; /* Sometimes the address is off by 4 */
6776 if (((gpointer)einfo->ExceptionInformation[1] >= ss_trigger_page && (guint8*)einfo->ExceptionInformation[1] <= (guint8*)ss_trigger_page + 128))
6781 siginfo_t* sinfo = (siginfo_t*) info;
6782 /* Sometimes the address is off by 4 */
6783 if (sinfo->si_signo == DBG_SIGNAL && (sinfo->si_addr >= ss_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)ss_trigger_page + 128))
6791 mono_arch_is_breakpoint_event (void *info, void *sigctx)
6794 EXCEPTION_RECORD* einfo = ((EXCEPTION_POINTERS*)info)->ExceptionRecord; /* Sometimes the address is off by 4 */
6795 if (((gpointer)einfo->ExceptionInformation[1] >= bp_trigger_page && (guint8*)einfo->ExceptionInformation[1] <= (guint8*)bp_trigger_page + 128))
6800 siginfo_t* sinfo = (siginfo_t*)info;
6801 /* Sometimes the address is off by 4 */
6802 if (sinfo->si_signo == DBG_SIGNAL && (sinfo->si_addr >= bp_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)bp_trigger_page + 128))
6809 #define BREAKPOINT_SIZE 6
6812 * mono_arch_skip_breakpoint:
6814 * See mini-amd64.c for docs.
6817 mono_arch_skip_breakpoint (MonoContext *ctx, MonoJitInfo *ji)
6819 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + BREAKPOINT_SIZE);
6823 * mono_arch_skip_single_step:
6825 * See mini-amd64.c for docs.
6828 mono_arch_skip_single_step (MonoContext *ctx)
6830 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 6);
6834 * mono_arch_get_seq_point_info:
6836 * See mini-amd64.c for docs.
6839 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
6846 mono_arch_init_lmf_ext (MonoLMFExt *ext, gpointer prev_lmf)
6848 ext->lmf.previous_lmf = (gsize)prev_lmf;
6849 /* Mark that this is a MonoLMFExt */
6850 ext->lmf.previous_lmf = (gsize)(gpointer)(((gssize)ext->lmf.previous_lmf) | 2);
6851 ext->lmf.ebp = (gssize)ext;
6856 #if defined(MONOTOUCH) || defined(MONO_EXTENSIONS)
6858 #include "../../../mono-extensions/mono/mini/mini-x86-gsharedvt.c"
6860 #endif /* !MONOTOUCH */