2 * mini-x86.c: x86 backend for the Mono code generator
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
9 * (C) 2003 Ximian, Inc.
18 #include <mono/metadata/appdomain.h>
19 #include <mono/metadata/debug-helpers.h>
20 #include <mono/metadata/threads.h>
21 #include <mono/metadata/profiler-private.h>
22 #include <mono/metadata/mono-debug.h>
23 #include <mono/metadata/gc-internal.h>
24 #include <mono/utils/mono-math.h>
25 #include <mono/utils/mono-counters.h>
26 #include <mono/utils/mono-mmap.h>
33 /* On windows, these hold the key returned by TlsAlloc () */
34 static gint lmf_tls_offset = -1;
35 static gint lmf_addr_tls_offset = -1;
36 static gint appdomain_tls_offset = -1;
39 static gboolean optimize_for_xen = TRUE;
41 #define optimize_for_xen 0
45 static gboolean is_win32 = TRUE;
47 static gboolean is_win32 = FALSE;
50 /* This mutex protects architecture specific caches */
51 #define mono_mini_arch_lock() EnterCriticalSection (&mini_arch_mutex)
52 #define mono_mini_arch_unlock() LeaveCriticalSection (&mini_arch_mutex)
53 static CRITICAL_SECTION mini_arch_mutex;
55 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
60 /* Under windows, the default pinvoke calling convention is stdcall */
61 #define CALLCONV_IS_STDCALL(sig) ((((sig)->call_convention) == MONO_CALL_STDCALL) || ((sig)->pinvoke && ((sig)->call_convention) == MONO_CALL_DEFAULT))
63 #define CALLCONV_IS_STDCALL(sig) (((sig)->call_convention) == MONO_CALL_STDCALL)
67 mono_breakpoint_info [MONO_BREAKPOINT_ARRAY_SIZE];
70 mono_realloc_native_code (MonoCompile *cfg)
72 #ifdef __native_client_codegen__
75 guint alignment_check;
77 /* Save the old alignment offset so we can re-align after the realloc. */
78 old_padding = (guint)(cfg->native_code - cfg->native_code_alloc);
80 cfg->native_code_alloc = g_realloc (cfg->native_code_alloc,
81 cfg->code_size + kNaClAlignment);
83 /* Align native_code to next nearest kNaClAlignment byte. */
84 native_code = (guint)cfg->native_code_alloc + kNaClAlignment;
85 native_code = (guint)native_code & ~kNaClAlignmentMask;
87 /* Shift the data to be 32-byte aligned again. */
88 memmove (native_code, cfg->native_code_alloc + old_padding, cfg->code_size);
90 alignment_check = (guint)native_code & kNaClAlignmentMask;
91 g_assert (alignment_check == 0);
94 return g_realloc (cfg->native_code, cfg->code_size);
98 #ifdef __native_client_codegen__
100 /* mono_arch_nacl_pad: Add pad bytes of alignment instructions at code, */
101 /* Check that alignment doesn't cross an alignment boundary. */
103 mono_arch_nacl_pad (guint8 *code, int pad)
105 const int kMaxPadding = 7; /* see x86-codegen.h: x86_padding() */
107 if (pad == 0) return code;
108 /* assertion: alignment cannot cross a block boundary */
109 g_assert(((uintptr_t)code & (~kNaClAlignmentMask)) ==
110 (((uintptr_t)code + pad - 1) & (~kNaClAlignmentMask)));
111 while (pad >= kMaxPadding) {
112 x86_padding (code, kMaxPadding);
115 if (pad != 0) x86_padding (code, pad);
120 mono_arch_nacl_skip_nops (guint8 *code)
122 x86_skip_nops (code);
126 #endif /* __native_client_codegen__ */
129 * The code generated for sequence points reads from this location, which is
130 * made read-only when single stepping is enabled.
132 static gpointer ss_trigger_page;
134 /* Enabled breakpoints read from this trigger page */
135 static gpointer bp_trigger_page;
138 mono_arch_regname (int reg)
141 case X86_EAX: return "%eax";
142 case X86_EBX: return "%ebx";
143 case X86_ECX: return "%ecx";
144 case X86_EDX: return "%edx";
145 case X86_ESP: return "%esp";
146 case X86_EBP: return "%ebp";
147 case X86_EDI: return "%edi";
148 case X86_ESI: return "%esi";
154 mono_arch_fregname (int reg)
179 mono_arch_xregname (int reg)
204 mono_x86_patch (unsigned char* code, gpointer target)
206 x86_patch (code, (unsigned char*)target);
225 /* Only if storage == ArgValuetypeInReg */
226 ArgStorage pair_storage [2];
235 gboolean need_stack_align;
236 guint32 stack_align_amount;
237 gboolean vtype_retaddr;
238 /* The index of the vret arg in the argument list */
247 #define FLOAT_PARAM_REGS 0
249 static X86_Reg_No param_regs [] = { 0 };
251 #if defined(TARGET_WIN32) || defined(__APPLE__) || defined(__FreeBSD__)
252 #define SMALL_STRUCTS_IN_REGS
253 static X86_Reg_No return_regs [] = { X86_EAX, X86_EDX };
257 add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
259 ainfo->offset = *stack_size;
261 if (*gr >= PARAM_REGS) {
262 ainfo->storage = ArgOnStack;
263 (*stack_size) += sizeof (gpointer);
266 ainfo->storage = ArgInIReg;
267 ainfo->reg = param_regs [*gr];
273 add_general_pair (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
275 ainfo->offset = *stack_size;
277 g_assert (PARAM_REGS == 0);
279 ainfo->storage = ArgOnStack;
280 (*stack_size) += sizeof (gpointer) * 2;
284 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
286 ainfo->offset = *stack_size;
288 if (*gr >= FLOAT_PARAM_REGS) {
289 ainfo->storage = ArgOnStack;
290 (*stack_size) += is_double ? 8 : 4;
293 /* A double register */
295 ainfo->storage = ArgInDoubleSSEReg;
297 ainfo->storage = ArgInFloatSSEReg;
305 add_valuetype (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
307 guint32 *gr, guint32 *fr, guint32 *stack_size)
312 klass = mono_class_from_mono_type (type);
313 size = mini_type_stack_size_full (gsctx, &klass->byval_arg, NULL, sig->pinvoke);
315 #ifdef SMALL_STRUCTS_IN_REGS
316 if (sig->pinvoke && is_return) {
317 MonoMarshalType *info;
320 * the exact rules are not very well documented, the code below seems to work with the
321 * code generated by gcc 3.3.3 -mno-cygwin.
323 info = mono_marshal_load_type_info (klass);
326 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
328 /* Special case structs with only a float member */
329 if ((info->native_size == 8) && (info->num_fields == 1) && (info->fields [0].field->type->type == MONO_TYPE_R8)) {
330 ainfo->storage = ArgValuetypeInReg;
331 ainfo->pair_storage [0] = ArgOnDoubleFpStack;
334 if ((info->native_size == 4) && (info->num_fields == 1) && (info->fields [0].field->type->type == MONO_TYPE_R4)) {
335 ainfo->storage = ArgValuetypeInReg;
336 ainfo->pair_storage [0] = ArgOnFloatFpStack;
339 if ((info->native_size == 1) || (info->native_size == 2) || (info->native_size == 4) || (info->native_size == 8)) {
340 ainfo->storage = ArgValuetypeInReg;
341 ainfo->pair_storage [0] = ArgInIReg;
342 ainfo->pair_regs [0] = return_regs [0];
343 if (info->native_size > 4) {
344 ainfo->pair_storage [1] = ArgInIReg;
345 ainfo->pair_regs [1] = return_regs [1];
352 ainfo->offset = *stack_size;
353 ainfo->storage = ArgOnStack;
354 *stack_size += ALIGN_TO (size, sizeof (gpointer));
360 * Obtain information about a call according to the calling convention.
361 * For x86 ELF, see the "System V Application Binary Interface Intel386
362 * Architecture Processor Supplment, Fourth Edition" document for more
364 * For x86 win32, see ???.
367 get_call_info_internal (MonoGenericSharingContext *gsctx, CallInfo *cinfo, MonoMethodSignature *sig, gboolean is_pinvoke)
369 guint32 i, gr, fr, pstart;
371 int n = sig->hasthis + sig->param_count;
372 guint32 stack_size = 0;
379 ret_type = mini_type_get_underlying_type (gsctx, sig->ret);
380 switch (ret_type->type) {
381 case MONO_TYPE_BOOLEAN:
392 case MONO_TYPE_FNPTR:
393 case MONO_TYPE_CLASS:
394 case MONO_TYPE_OBJECT:
395 case MONO_TYPE_SZARRAY:
396 case MONO_TYPE_ARRAY:
397 case MONO_TYPE_STRING:
398 cinfo->ret.storage = ArgInIReg;
399 cinfo->ret.reg = X86_EAX;
403 cinfo->ret.storage = ArgInIReg;
404 cinfo->ret.reg = X86_EAX;
407 cinfo->ret.storage = ArgOnFloatFpStack;
410 cinfo->ret.storage = ArgOnDoubleFpStack;
412 case MONO_TYPE_GENERICINST:
413 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
414 cinfo->ret.storage = ArgInIReg;
415 cinfo->ret.reg = X86_EAX;
419 case MONO_TYPE_VALUETYPE: {
420 guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
422 add_valuetype (gsctx, sig, &cinfo->ret, sig->ret, TRUE, &tmp_gr, &tmp_fr, &tmp_stacksize);
423 if (cinfo->ret.storage == ArgOnStack) {
424 cinfo->vtype_retaddr = TRUE;
425 /* The caller passes the address where the value is stored */
429 case MONO_TYPE_TYPEDBYREF:
430 /* Same as a valuetype with size 12 */
431 cinfo->vtype_retaddr = TRUE;
434 cinfo->ret.storage = ArgNone;
437 g_error ("Can't handle as return value 0x%x", sig->ret->type);
443 * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
444 * the first argument, allowing 'this' to be always passed in the first arg reg.
445 * Also do this if the first argument is a reference type, since virtual calls
446 * are sometimes made using calli without sig->hasthis set, like in the delegate
449 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]))))) {
451 add_general (&gr, &stack_size, cinfo->args + 0);
453 add_general (&gr, &stack_size, &cinfo->args [sig->hasthis + 0]);
456 add_general (&gr, &stack_size, &cinfo->ret);
457 cinfo->vret_arg_index = 1;
461 add_general (&gr, &stack_size, cinfo->args + 0);
463 if (cinfo->vtype_retaddr)
464 add_general (&gr, &stack_size, &cinfo->ret);
467 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
469 fr = FLOAT_PARAM_REGS;
471 /* Emit the signature cookie just before the implicit arguments */
472 add_general (&gr, &stack_size, &cinfo->sig_cookie);
475 for (i = pstart; i < sig->param_count; ++i) {
476 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
479 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
480 /* We allways pass the sig cookie on the stack for simplicity */
482 * Prevent implicit arguments + the sig cookie from being passed
486 fr = FLOAT_PARAM_REGS;
488 /* Emit the signature cookie just before the implicit arguments */
489 add_general (&gr, &stack_size, &cinfo->sig_cookie);
492 if (sig->params [i]->byref) {
493 add_general (&gr, &stack_size, ainfo);
496 ptype = mini_type_get_underlying_type (gsctx, sig->params [i]);
497 switch (ptype->type) {
498 case MONO_TYPE_BOOLEAN:
501 add_general (&gr, &stack_size, ainfo);
506 add_general (&gr, &stack_size, ainfo);
510 add_general (&gr, &stack_size, ainfo);
515 case MONO_TYPE_FNPTR:
516 case MONO_TYPE_CLASS:
517 case MONO_TYPE_OBJECT:
518 case MONO_TYPE_STRING:
519 case MONO_TYPE_SZARRAY:
520 case MONO_TYPE_ARRAY:
521 add_general (&gr, &stack_size, ainfo);
523 case MONO_TYPE_GENERICINST:
524 if (!mono_type_generic_inst_is_valuetype (ptype)) {
525 add_general (&gr, &stack_size, ainfo);
529 case MONO_TYPE_VALUETYPE:
530 add_valuetype (gsctx, sig, ainfo, sig->params [i], FALSE, &gr, &fr, &stack_size);
532 case MONO_TYPE_TYPEDBYREF:
533 stack_size += sizeof (MonoTypedRef);
534 ainfo->storage = ArgOnStack;
538 add_general_pair (&gr, &stack_size, ainfo);
541 add_float (&fr, &stack_size, ainfo, FALSE);
544 add_float (&fr, &stack_size, ainfo, TRUE);
547 g_error ("unexpected type 0x%x", ptype->type);
548 g_assert_not_reached ();
552 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
554 fr = FLOAT_PARAM_REGS;
556 /* Emit the signature cookie just before the implicit arguments */
557 add_general (&gr, &stack_size, &cinfo->sig_cookie);
560 if (mono_do_x86_stack_align && (stack_size % MONO_ARCH_FRAME_ALIGNMENT) != 0) {
561 cinfo->need_stack_align = TRUE;
562 cinfo->stack_align_amount = MONO_ARCH_FRAME_ALIGNMENT - (stack_size % MONO_ARCH_FRAME_ALIGNMENT);
563 stack_size += cinfo->stack_align_amount;
566 cinfo->stack_usage = stack_size;
567 cinfo->reg_usage = gr;
568 cinfo->freg_usage = fr;
573 get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSignature *sig, gboolean is_pinvoke)
575 int n = sig->hasthis + sig->param_count;
579 cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
581 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
583 return get_call_info_internal (gsctx, cinfo, sig, is_pinvoke);
587 * mono_arch_get_argument_info:
588 * @csig: a method signature
589 * @param_count: the number of parameters to consider
590 * @arg_info: an array to store the result infos
592 * Gathers information on parameters such as size, alignment and
593 * padding. arg_info should be large enought to hold param_count + 1 entries.
595 * Returns the size of the argument area on the stack.
596 * This should be signal safe, since it is called from
597 * mono_arch_find_jit_info ().
598 * FIXME: The metadata calls might not be signal safe.
601 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
603 int len, k, args_size = 0;
609 /* Avoid g_malloc as it is not signal safe */
610 len = sizeof (CallInfo) + (sizeof (ArgInfo) * (csig->param_count + 1));
611 cinfo = (CallInfo*)g_newa (guint8*, len);
612 memset (cinfo, 0, len);
614 cinfo = get_call_info_internal (NULL, cinfo, csig, FALSE);
616 arg_info [0].offset = offset;
618 if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 0) {
619 args_size += sizeof (gpointer);
624 args_size += sizeof (gpointer);
628 if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 1 && csig->hasthis) {
629 /* Emitted after this */
630 args_size += sizeof (gpointer);
634 arg_info [0].size = args_size;
636 for (k = 0; k < param_count; k++) {
637 size = mini_type_stack_size_full (NULL, csig->params [k], &align, csig->pinvoke);
639 /* ignore alignment for now */
642 args_size += pad = (align - (args_size & (align - 1))) & (align - 1);
643 arg_info [k].pad = pad;
645 arg_info [k + 1].pad = 0;
646 arg_info [k + 1].size = size;
648 arg_info [k + 1].offset = offset;
651 if (k == 0 && cinfo->vtype_retaddr && cinfo->vret_arg_index == 1 && !csig->hasthis) {
652 /* Emitted after the first arg */
653 args_size += sizeof (gpointer);
658 if (mono_do_x86_stack_align && !CALLCONV_IS_STDCALL (csig))
659 align = MONO_ARCH_FRAME_ALIGNMENT;
662 args_size += pad = (align - (args_size & (align - 1))) & (align - 1);
663 arg_info [k].pad = pad;
668 static const guchar cpuid_impl [] = {
669 0x55, /* push %ebp */
670 0x89, 0xe5, /* mov %esp,%ebp */
671 0x53, /* push %ebx */
672 0x8b, 0x45, 0x08, /* mov 0x8(%ebp),%eax */
673 0x0f, 0xa2, /* cpuid */
674 0x50, /* push %eax */
675 0x8b, 0x45, 0x10, /* mov 0x10(%ebp),%eax */
676 0x89, 0x18, /* mov %ebx,(%eax) */
677 0x8b, 0x45, 0x14, /* mov 0x14(%ebp),%eax */
678 0x89, 0x08, /* mov %ecx,(%eax) */
679 0x8b, 0x45, 0x18, /* mov 0x18(%ebp),%eax */
680 0x89, 0x10, /* mov %edx,(%eax) */
682 0x8b, 0x55, 0x0c, /* mov 0xc(%ebp),%edx */
683 0x89, 0x02, /* mov %eax,(%edx) */
689 typedef void (*CpuidFunc) (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx);
692 cpuid (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx)
694 #if defined(__native_client__)
695 /* Taken from below, the bug listed in the comment is */
696 /* only valid for non-static cases. */
697 __asm__ __volatile__ ("cpuid"
698 : "=a" (*p_eax), "=b" (*p_ebx), "=c" (*p_ecx), "=d" (*p_edx)
704 __asm__ __volatile__ (
707 "movl %%eax, %%edx\n"
708 "xorl $0x200000, %%eax\n"
713 "xorl %%edx, %%eax\n"
714 "andl $0x200000, %%eax\n"
736 /* Have to use the code manager to get around WinXP DEP */
737 static CpuidFunc func = NULL;
740 ptr = mono_global_codeman_reserve (sizeof (cpuid_impl));
741 memcpy (ptr, cpuid_impl, sizeof (cpuid_impl));
742 func = (CpuidFunc)ptr;
744 func (id, p_eax, p_ebx, p_ecx, p_edx);
747 * We use this approach because of issues with gcc and pic code, see:
748 * http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=7329
749 __asm__ __volatile__ ("cpuid"
750 : "=a" (*p_eax), "=b" (*p_ebx), "=c" (*p_ecx), "=d" (*p_edx)
760 * Initialize the cpu to execute managed code.
763 mono_arch_cpu_init (void)
765 /* spec compliance requires running with double precision */
769 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
770 fpcw &= ~X86_FPCW_PRECC_MASK;
771 fpcw |= X86_FPCW_PREC_DOUBLE;
772 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
773 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
775 _control87 (_PC_53, MCW_PC);
780 * Initialize architecture specific code.
783 mono_arch_init (void)
785 InitializeCriticalSection (&mini_arch_mutex);
787 ss_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ);
788 bp_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_32BIT);
789 mono_mprotect (bp_trigger_page, mono_pagesize (), 0);
791 mono_aot_register_jit_icall ("mono_x86_throw_exception", mono_x86_throw_exception);
792 mono_aot_register_jit_icall ("mono_x86_throw_corlib_exception", mono_x86_throw_corlib_exception);
796 * Cleanup architecture specific code.
799 mono_arch_cleanup (void)
801 DeleteCriticalSection (&mini_arch_mutex);
805 * This function returns the optimizations supported on this cpu.
808 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
810 #if !defined(__native_client__)
811 int eax, ebx, ecx, edx;
817 /* The cpuid function allocates from the global codeman */
820 /* Feature Flags function, flags returned in EDX. */
821 if (cpuid (1, &eax, &ebx, &ecx, &edx)) {
822 if (edx & (1 << 15)) {
823 opts |= MONO_OPT_CMOV;
825 opts |= MONO_OPT_FCMOV;
827 *exclude_mask |= MONO_OPT_FCMOV;
829 *exclude_mask |= MONO_OPT_CMOV;
831 opts |= MONO_OPT_SSE2;
833 *exclude_mask |= MONO_OPT_SSE2;
835 #ifdef MONO_ARCH_SIMD_INTRINSICS
836 /*SIMD intrinsics require at least SSE2.*/
837 if (!(opts & MONO_OPT_SSE2))
838 *exclude_mask |= MONO_OPT_SIMD;
843 return MONO_OPT_CMOV | MONO_OPT_FCMOV | MONO_OPT_SSE2;
848 * This function test for all SSE functions supported.
850 * Returns a bitmask corresponding to all supported versions.
854 mono_arch_cpu_enumerate_simd_versions (void)
856 int eax, ebx, ecx, edx;
857 guint32 sse_opts = 0;
860 /* The cpuid function allocates from the global codeman */
863 if (cpuid (1, &eax, &ebx, &ecx, &edx)) {
865 sse_opts |= SIMD_VERSION_SSE1;
867 sse_opts |= SIMD_VERSION_SSE2;
869 sse_opts |= SIMD_VERSION_SSE3;
871 sse_opts |= SIMD_VERSION_SSSE3;
873 sse_opts |= SIMD_VERSION_SSE41;
875 sse_opts |= SIMD_VERSION_SSE42;
878 /* Yes, all this needs to be done to check for sse4a.
879 See: "Amd: CPUID Specification"
881 if (cpuid (0x80000000, &eax, &ebx, &ecx, &edx)) {
882 /* eax greater or equal than 0x80000001, ebx = 'htuA', ecx = DMAc', edx = 'itne'*/
883 if ((((unsigned int) eax) >= 0x80000001) && (ebx == 0x68747541) && (ecx == 0x444D4163) && (edx == 0x69746E65)) {
884 cpuid (0x80000001, &eax, &ebx, &ecx, &edx);
886 sse_opts |= SIMD_VERSION_SSE4a;
895 * Determine whenever the trap whose info is in SIGINFO is caused by
899 mono_arch_is_int_overflow (void *sigctx, void *info)
904 mono_arch_sigctx_to_monoctx (sigctx, &ctx);
906 ip = (guint8*)ctx.eip;
908 if ((ip [0] == 0xf7) && (x86_modrm_mod (ip [1]) == 0x3) && (x86_modrm_reg (ip [1]) == 0x7)) {
912 switch (x86_modrm_rm (ip [1])) {
932 g_assert_not_reached ();
944 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
949 for (i = 0; i < cfg->num_varinfo; i++) {
950 MonoInst *ins = cfg->varinfo [i];
951 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
954 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
957 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
958 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
961 /* we dont allocate I1 to registers because there is no simply way to sign extend
962 * 8bit quantities in caller saved registers on x86 */
963 if (mono_is_regsize_var (ins->inst_vtype) && (ins->inst_vtype->type != MONO_TYPE_I1)) {
964 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
965 g_assert (i == vmv->idx);
966 vars = g_list_prepend (vars, vmv);
970 vars = mono_varlist_sort (cfg, vars, 0);
976 mono_arch_get_global_int_regs (MonoCompile *cfg)
980 /* we can use 3 registers for global allocation */
981 regs = g_list_prepend (regs, (gpointer)X86_EBX);
982 regs = g_list_prepend (regs, (gpointer)X86_ESI);
983 regs = g_list_prepend (regs, (gpointer)X86_EDI);
989 * mono_arch_regalloc_cost:
991 * Return the cost, in number of memory references, of the action of
992 * allocating the variable VMV into a register during global register
996 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
998 MonoInst *ins = cfg->varinfo [vmv->idx];
1000 if (cfg->method->save_lmf)
1001 /* The register is already saved */
1002 return (ins->opcode == OP_ARG) ? 1 : 0;
1004 /* push+pop+possible load if it is an argument */
1005 return (ins->opcode == OP_ARG) ? 3 : 2;
1009 set_needs_stack_frame (MonoCompile *cfg, gboolean flag)
1011 static int inited = FALSE;
1012 static int count = 0;
1014 if (cfg->arch.need_stack_frame_inited) {
1015 g_assert (cfg->arch.need_stack_frame == flag);
1019 cfg->arch.need_stack_frame = flag;
1020 cfg->arch.need_stack_frame_inited = TRUE;
1026 mono_counters_register ("Could eliminate stack frame", MONO_COUNTER_INT|MONO_COUNTER_JIT, &count);
1031 //g_print ("will eliminate %s.%s.%s\n", cfg->method->klass->name_space, cfg->method->klass->name, cfg->method->name);
1035 needs_stack_frame (MonoCompile *cfg)
1037 MonoMethodSignature *sig;
1038 MonoMethodHeader *header;
1039 gboolean result = FALSE;
1041 #if defined(__APPLE__)
1042 /*OSX requires stack frame code to have the correct alignment. */
1046 if (cfg->arch.need_stack_frame_inited)
1047 return cfg->arch.need_stack_frame;
1049 header = cfg->header;
1050 sig = mono_method_signature (cfg->method);
1052 if (cfg->disable_omit_fp)
1054 else if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1056 else if (cfg->method->save_lmf)
1058 else if (cfg->stack_offset)
1060 else if (cfg->param_area)
1062 else if (cfg->flags & (MONO_CFG_HAS_CALLS | MONO_CFG_HAS_ALLOCA | MONO_CFG_HAS_TAIL))
1064 else if (header->num_clauses)
1066 else if (sig->param_count + sig->hasthis)
1068 else if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1070 else if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) ||
1071 (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
1074 set_needs_stack_frame (cfg, result);
1076 return cfg->arch.need_stack_frame;
1080 * Set var information according to the calling convention. X86 version.
1081 * The locals var stuff should most likely be split in another method.
1084 mono_arch_allocate_vars (MonoCompile *cfg)
1086 MonoMethodSignature *sig;
1087 MonoMethodHeader *header;
1089 guint32 locals_stack_size, locals_stack_align;
1094 header = cfg->header;
1095 sig = mono_method_signature (cfg->method);
1097 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, FALSE);
1099 cfg->frame_reg = X86_EBP;
1102 /* Reserve space to save LMF and caller saved registers */
1104 if (cfg->method->save_lmf) {
1105 offset += sizeof (MonoLMF);
1107 if (cfg->used_int_regs & (1 << X86_EBX)) {
1111 if (cfg->used_int_regs & (1 << X86_EDI)) {
1115 if (cfg->used_int_regs & (1 << X86_ESI)) {
1120 switch (cinfo->ret.storage) {
1121 case ArgValuetypeInReg:
1122 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
1124 cfg->ret->opcode = OP_REGOFFSET;
1125 cfg->ret->inst_basereg = X86_EBP;
1126 cfg->ret->inst_offset = - offset;
1132 /* Allocate locals */
1133 offsets = mono_allocate_stack_slots (cfg, &locals_stack_size, &locals_stack_align);
1134 if (locals_stack_size > MONO_ARCH_MAX_FRAME_SIZE) {
1135 char *mname = mono_method_full_name (cfg->method, TRUE);
1136 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
1137 cfg->exception_message = g_strdup_printf ("Method %s stack is too big.", mname);
1141 if (locals_stack_align) {
1142 offset += (locals_stack_align - 1);
1143 offset &= ~(locals_stack_align - 1);
1146 * EBP is at alignment 8 % MONO_ARCH_FRAME_ALIGNMENT, so if we
1147 * have locals larger than 8 bytes we need to make sure that
1148 * they have the appropriate offset.
1150 if (MONO_ARCH_FRAME_ALIGNMENT > 8 && locals_stack_align > 8)
1151 offset += MONO_ARCH_FRAME_ALIGNMENT - sizeof (gpointer) * 2;
1152 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1153 if (offsets [i] != -1) {
1154 MonoInst *inst = cfg->varinfo [i];
1155 inst->opcode = OP_REGOFFSET;
1156 inst->inst_basereg = X86_EBP;
1157 inst->inst_offset = - (offset + offsets [i]);
1158 //printf ("allocated local %d to ", i); mono_print_tree_nl (inst);
1161 offset += locals_stack_size;
1165 * Allocate arguments+return value
1168 switch (cinfo->ret.storage) {
1170 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
1172 * In the new IR, the cfg->vret_addr variable represents the
1173 * vtype return value.
1175 cfg->vret_addr->opcode = OP_REGOFFSET;
1176 cfg->vret_addr->inst_basereg = cfg->frame_reg;
1177 cfg->vret_addr->inst_offset = cinfo->ret.offset + ARGS_OFFSET;
1178 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1179 printf ("vret_addr =");
1180 mono_print_ins (cfg->vret_addr);
1183 cfg->ret->opcode = OP_REGOFFSET;
1184 cfg->ret->inst_basereg = X86_EBP;
1185 cfg->ret->inst_offset = cinfo->ret.offset + ARGS_OFFSET;
1188 case ArgValuetypeInReg:
1191 cfg->ret->opcode = OP_REGVAR;
1192 cfg->ret->inst_c0 = cinfo->ret.reg;
1193 cfg->ret->dreg = cinfo->ret.reg;
1196 case ArgOnFloatFpStack:
1197 case ArgOnDoubleFpStack:
1200 g_assert_not_reached ();
1203 if (sig->call_convention == MONO_CALL_VARARG) {
1204 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
1205 cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
1208 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1209 ArgInfo *ainfo = &cinfo->args [i];
1210 inst = cfg->args [i];
1211 if (inst->opcode != OP_REGVAR) {
1212 inst->opcode = OP_REGOFFSET;
1213 inst->inst_basereg = X86_EBP;
1215 inst->inst_offset = ainfo->offset + ARGS_OFFSET;
1218 cfg->stack_offset = offset;
1222 mono_arch_create_vars (MonoCompile *cfg)
1224 MonoMethodSignature *sig;
1227 sig = mono_method_signature (cfg->method);
1229 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, FALSE);
1231 if (cinfo->ret.storage == ArgValuetypeInReg)
1232 cfg->ret_var_is_local = TRUE;
1233 if ((cinfo->ret.storage != ArgValuetypeInReg) && MONO_TYPE_ISSTRUCT (sig->ret)) {
1234 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
1239 * It is expensive to adjust esp for each individual fp argument pushed on the stack
1240 * so we try to do it just once when we have multiple fp arguments in a row.
1241 * We don't use this mechanism generally because for int arguments the generated code
1242 * is slightly bigger and new generation cpus optimize away the dependency chains
1243 * created by push instructions on the esp value.
1244 * fp_arg_setup is the first argument in the execution sequence where the esp register
1247 static G_GNUC_UNUSED int
1248 collect_fp_stack_space (MonoMethodSignature *sig, int start_arg, int *fp_arg_setup)
1253 for (; start_arg < sig->param_count; ++start_arg) {
1254 t = mini_type_get_underlying_type (NULL, sig->params [start_arg]);
1255 if (!t->byref && t->type == MONO_TYPE_R8) {
1256 fp_space += sizeof (double);
1257 *fp_arg_setup = start_arg;
1266 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
1268 MonoMethodSignature *tmp_sig;
1270 /* FIXME: Add support for signature tokens to AOT */
1271 cfg->disable_aot = TRUE;
1274 * mono_ArgIterator_Setup assumes the signature cookie is
1275 * passed first and all the arguments which were before it are
1276 * passed on the stack after the signature. So compensate by
1277 * passing a different signature.
1279 tmp_sig = mono_metadata_signature_dup (call->signature);
1280 tmp_sig->param_count -= call->signature->sentinelpos;
1281 tmp_sig->sentinelpos = 0;
1282 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
1284 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_X86_PUSH_IMM, -1, -1, tmp_sig);
1289 mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
1294 LLVMCallInfo *linfo;
1297 n = sig->param_count + sig->hasthis;
1299 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, sig->pinvoke);
1301 linfo = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
1304 * LLVM always uses the native ABI while we use our own ABI, the
1305 * only difference is the handling of vtypes:
1306 * - we only pass/receive them in registers in some cases, and only
1307 * in 1 or 2 integer registers.
1309 if (cinfo->ret.storage == ArgValuetypeInReg) {
1311 cfg->exception_message = g_strdup ("pinvoke + vtypes");
1312 cfg->disable_llvm = TRUE;
1316 cfg->exception_message = g_strdup ("vtype ret in call");
1317 cfg->disable_llvm = TRUE;
1319 linfo->ret.storage = LLVMArgVtypeInReg;
1320 for (j = 0; j < 2; ++j)
1321 linfo->ret.pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, cinfo->ret.pair_storage [j]);
1325 if (MONO_TYPE_ISSTRUCT (sig->ret) && cinfo->ret.storage == ArgInIReg) {
1326 /* Vtype returned using a hidden argument */
1327 linfo->ret.storage = LLVMArgVtypeRetAddr;
1328 linfo->vret_arg_index = cinfo->vret_arg_index;
1331 if (MONO_TYPE_ISSTRUCT (sig->ret) && cinfo->ret.storage != ArgInIReg) {
1333 cfg->exception_message = g_strdup ("vtype ret in call");
1334 cfg->disable_llvm = TRUE;
1337 for (i = 0; i < n; ++i) {
1338 ainfo = cinfo->args + i;
1340 if (i >= sig->hasthis)
1341 t = sig->params [i - sig->hasthis];
1343 t = &mono_defaults.int_class->byval_arg;
1345 linfo->args [i].storage = LLVMArgNone;
1347 switch (ainfo->storage) {
1349 linfo->args [i].storage = LLVMArgInIReg;
1351 case ArgInDoubleSSEReg:
1352 case ArgInFloatSSEReg:
1353 linfo->args [i].storage = LLVMArgInFPReg;
1356 if (MONO_TYPE_ISSTRUCT (t)) {
1357 if (mono_class_value_size (mono_class_from_mono_type (t), NULL) == 0)
1358 /* LLVM seems to allocate argument space for empty structures too */
1359 linfo->args [i].storage = LLVMArgNone;
1361 linfo->args [i].storage = LLVMArgVtypeByVal;
1363 linfo->args [i].storage = LLVMArgInIReg;
1365 if (t->type == MONO_TYPE_R4)
1366 linfo->args [i].storage = LLVMArgInFPReg;
1367 else if (t->type == MONO_TYPE_R8)
1368 linfo->args [i].storage = LLVMArgInFPReg;
1372 case ArgValuetypeInReg:
1374 cfg->exception_message = g_strdup ("pinvoke + vtypes");
1375 cfg->disable_llvm = TRUE;
1379 cfg->exception_message = g_strdup ("vtype arg");
1380 cfg->disable_llvm = TRUE;
1382 linfo->args [i].storage = LLVMArgVtypeInReg;
1383 for (j = 0; j < 2; ++j)
1384 linfo->args [i].pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, ainfo->pair_storage [j]);
1388 cfg->exception_message = g_strdup ("ainfo->storage");
1389 cfg->disable_llvm = TRUE;
1399 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
1402 MonoMethodSignature *sig;
1405 int sentinelpos = 0;
1407 sig = call->signature;
1408 n = sig->param_count + sig->hasthis;
1410 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, FALSE);
1412 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1413 sentinelpos = sig->sentinelpos + (sig->hasthis ? 1 : 0);
1415 if (cinfo->need_stack_align) {
1416 MONO_INST_NEW (cfg, arg, OP_SUB_IMM);
1417 arg->dreg = X86_ESP;
1418 arg->sreg1 = X86_ESP;
1419 arg->inst_imm = cinfo->stack_align_amount;
1420 MONO_ADD_INS (cfg->cbb, arg);
1423 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
1424 if (cinfo->ret.storage == ArgValuetypeInReg) {
1426 * Tell the JIT to use a more efficient calling convention: call using
1427 * OP_CALL, compute the result location after the call, and save the
1430 call->vret_in_reg = TRUE;
1432 NULLIFY_INS (call->vret_var);
1436 /* Handle the case where there are no implicit arguments */
1437 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sentinelpos)) {
1438 emit_sig_cookie (cfg, call, cinfo);
1441 /* Arguments are pushed in the reverse order */
1442 for (i = n - 1; i >= 0; i --) {
1443 ArgInfo *ainfo = cinfo->args + i;
1446 if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 1 && i == 0) {
1447 /* Push the vret arg before the first argument */
1449 MONO_INST_NEW (cfg, vtarg, OP_X86_PUSH);
1450 vtarg->type = STACK_MP;
1451 vtarg->sreg1 = call->vret_var->dreg;
1452 MONO_ADD_INS (cfg->cbb, vtarg);
1455 if (i >= sig->hasthis)
1456 t = sig->params [i - sig->hasthis];
1458 t = &mono_defaults.int_class->byval_arg;
1459 t = mini_type_get_underlying_type (cfg->generic_sharing_context, t);
1461 MONO_INST_NEW (cfg, arg, OP_X86_PUSH);
1463 in = call->args [i];
1464 arg->cil_code = in->cil_code;
1465 arg->sreg1 = in->dreg;
1466 arg->type = in->type;
1468 g_assert (in->dreg != -1);
1470 if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(t))) {
1474 g_assert (in->klass);
1476 if (t->type == MONO_TYPE_TYPEDBYREF) {
1477 size = sizeof (MonoTypedRef);
1478 align = sizeof (gpointer);
1481 size = mini_type_stack_size_full (cfg->generic_sharing_context, &in->klass->byval_arg, &align, sig->pinvoke);
1485 arg->opcode = OP_OUTARG_VT;
1486 arg->sreg1 = in->dreg;
1487 arg->klass = in->klass;
1488 arg->backend.size = size;
1490 MONO_ADD_INS (cfg->cbb, arg);
1494 switch (ainfo->storage) {
1496 arg->opcode = OP_X86_PUSH;
1498 if (t->type == MONO_TYPE_R4) {
1499 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, 4);
1500 arg->opcode = OP_STORER4_MEMBASE_REG;
1501 arg->inst_destbasereg = X86_ESP;
1502 arg->inst_offset = 0;
1503 } else if (t->type == MONO_TYPE_R8) {
1504 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, 8);
1505 arg->opcode = OP_STORER8_MEMBASE_REG;
1506 arg->inst_destbasereg = X86_ESP;
1507 arg->inst_offset = 0;
1508 } else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_U8) {
1510 MONO_EMIT_NEW_UNALU (cfg, OP_X86_PUSH, -1, in->dreg + 2);
1515 g_assert_not_reached ();
1518 MONO_ADD_INS (cfg->cbb, arg);
1521 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sentinelpos)) {
1522 /* Emit the signature cookie just before the implicit arguments */
1523 emit_sig_cookie (cfg, call, cinfo);
1527 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
1530 if (cinfo->ret.storage == ArgValuetypeInReg) {
1533 else if (cinfo->ret.storage == ArgInIReg) {
1535 /* The return address is passed in a register */
1536 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
1537 vtarg->sreg1 = call->inst.dreg;
1538 vtarg->dreg = mono_alloc_ireg (cfg);
1539 MONO_ADD_INS (cfg->cbb, vtarg);
1541 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
1542 } else if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 0) {
1544 MONO_INST_NEW (cfg, vtarg, OP_X86_PUSH);
1545 vtarg->type = STACK_MP;
1546 vtarg->sreg1 = call->vret_var->dreg;
1547 MONO_ADD_INS (cfg->cbb, vtarg);
1550 /* if the function returns a struct on stack, the called method already does a ret $0x4 */
1551 if (cinfo->ret.storage != ArgValuetypeInReg)
1552 cinfo->stack_usage -= 4;
1555 call->stack_usage = cinfo->stack_usage;
1559 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
1562 int size = ins->backend.size;
1565 MONO_INST_NEW (cfg, arg, OP_X86_PUSH_MEMBASE);
1566 arg->sreg1 = src->dreg;
1568 MONO_ADD_INS (cfg->cbb, arg);
1569 } else if (size <= 20) {
1570 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, ALIGN_TO (size, 4));
1571 mini_emit_memcpy (cfg, X86_ESP, 0, src->dreg, 0, size, 4);
1573 MONO_INST_NEW (cfg, arg, OP_X86_PUSH_OBJ);
1574 arg->inst_basereg = src->dreg;
1575 arg->inst_offset = 0;
1576 arg->inst_imm = size;
1578 MONO_ADD_INS (cfg->cbb, arg);
1583 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
1585 MonoType *ret = mini_type_get_underlying_type (cfg->generic_sharing_context, mono_method_signature (method)->ret);
1588 if (ret->type == MONO_TYPE_R4) {
1589 if (COMPILE_LLVM (cfg))
1590 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
1593 } else if (ret->type == MONO_TYPE_R8) {
1594 if (COMPILE_LLVM (cfg))
1595 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
1598 } else if (ret->type == MONO_TYPE_I8 || ret->type == MONO_TYPE_U8) {
1599 if (COMPILE_LLVM (cfg))
1600 MONO_EMIT_NEW_UNALU (cfg, OP_LMOVE, cfg->ret->dreg, val->dreg);
1602 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, X86_EAX, val->dreg + 1);
1603 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, X86_EDX, val->dreg + 2);
1609 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
1613 * Allow tracing to work with this interface (with an optional argument)
1616 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1620 g_assert (MONO_ARCH_FRAME_ALIGNMENT >= 8);
1621 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 8);
1623 /* if some args are passed in registers, we need to save them here */
1624 x86_push_reg (code, X86_EBP);
1626 if (cfg->compile_aot) {
1627 x86_push_imm (code, cfg->method);
1628 x86_mov_reg_imm (code, X86_EAX, func);
1629 x86_call_reg (code, X86_EAX);
1631 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
1632 x86_push_imm (code, cfg->method);
1633 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1634 x86_call_code (code, 0);
1636 x86_alu_reg_imm (code, X86_ADD, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT);
1650 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
1653 int arg_size = 0, stack_usage = 0, save_mode = SAVE_NONE;
1654 MonoMethod *method = cfg->method;
1655 MonoType *ret_type = mini_type_get_underlying_type (cfg->generic_sharing_context, mono_method_signature (method)->ret);
1657 switch (ret_type->type) {
1658 case MONO_TYPE_VOID:
1659 /* special case string .ctor icall */
1660 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class) {
1661 save_mode = SAVE_EAX;
1662 stack_usage = enable_arguments ? 8 : 4;
1664 save_mode = SAVE_NONE;
1668 save_mode = SAVE_EAX_EDX;
1669 stack_usage = enable_arguments ? 16 : 8;
1673 save_mode = SAVE_FP;
1674 stack_usage = enable_arguments ? 16 : 8;
1676 case MONO_TYPE_GENERICINST:
1677 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
1678 save_mode = SAVE_EAX;
1679 stack_usage = enable_arguments ? 8 : 4;
1683 case MONO_TYPE_VALUETYPE:
1684 // FIXME: Handle SMALL_STRUCT_IN_REG here for proper alignment on darwin-x86
1685 save_mode = SAVE_STRUCT;
1686 stack_usage = enable_arguments ? 4 : 0;
1689 save_mode = SAVE_EAX;
1690 stack_usage = enable_arguments ? 8 : 4;
1694 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - stack_usage - 4);
1696 switch (save_mode) {
1698 x86_push_reg (code, X86_EDX);
1699 x86_push_reg (code, X86_EAX);
1700 if (enable_arguments) {
1701 x86_push_reg (code, X86_EDX);
1702 x86_push_reg (code, X86_EAX);
1707 x86_push_reg (code, X86_EAX);
1708 if (enable_arguments) {
1709 x86_push_reg (code, X86_EAX);
1714 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1715 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1716 if (enable_arguments) {
1717 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1718 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1723 if (enable_arguments) {
1724 x86_push_membase (code, X86_EBP, 8);
1733 if (cfg->compile_aot) {
1734 x86_push_imm (code, method);
1735 x86_mov_reg_imm (code, X86_EAX, func);
1736 x86_call_reg (code, X86_EAX);
1738 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
1739 x86_push_imm (code, method);
1740 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1741 x86_call_code (code, 0);
1744 x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
1746 switch (save_mode) {
1748 x86_pop_reg (code, X86_EAX);
1749 x86_pop_reg (code, X86_EDX);
1752 x86_pop_reg (code, X86_EAX);
1755 x86_fld_membase (code, X86_ESP, 0, TRUE);
1756 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1763 x86_alu_reg_imm (code, X86_ADD, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - stack_usage);
1768 #define EMIT_COND_BRANCH(ins,cond,sign) \
1769 if (ins->inst_true_bb->native_offset) { \
1770 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
1772 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1773 if ((cfg->opt & MONO_OPT_BRANCH) && \
1774 x86_is_imm8 (ins->inst_true_bb->max_offset - cpos)) \
1775 x86_branch8 (code, cond, 0, sign); \
1777 x86_branch32 (code, cond, 0, sign); \
1781 * Emit an exception if condition is fail and
1782 * if possible do a directly branch to target
1784 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
1786 MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
1787 if (tins == NULL) { \
1788 mono_add_patch_info (cfg, code - cfg->native_code, \
1789 MONO_PATCH_INFO_EXC, exc_name); \
1790 x86_branch32 (code, cond, 0, signed); \
1792 EMIT_COND_BRANCH (tins, cond, signed); \
1796 #define EMIT_FPCOMPARE(code) do { \
1797 x86_fcompp (code); \
1798 x86_fnstsw (code); \
1803 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data)
1805 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
1806 x86_call_code (code, 0);
1811 #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)))
1814 * mono_peephole_pass_1:
1816 * Perform peephole opts which should/can be performed before local regalloc
1819 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
1823 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
1824 MonoInst *last_ins = ins->prev;
1826 switch (ins->opcode) {
1829 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS)) {
1831 * X86_LEA is like ADD, but doesn't have the
1832 * sreg1==dreg restriction.
1834 ins->opcode = OP_X86_LEA_MEMBASE;
1835 ins->inst_basereg = ins->sreg1;
1836 } else if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1837 ins->opcode = OP_X86_INC_REG;
1841 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS)) {
1842 ins->opcode = OP_X86_LEA_MEMBASE;
1843 ins->inst_basereg = ins->sreg1;
1844 ins->inst_imm = -ins->inst_imm;
1845 } else if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1846 ins->opcode = OP_X86_DEC_REG;
1848 case OP_COMPARE_IMM:
1849 case OP_ICOMPARE_IMM:
1850 /* OP_COMPARE_IMM (reg, 0)
1852 * OP_X86_TEST_NULL (reg)
1855 ins->opcode = OP_X86_TEST_NULL;
1857 case OP_X86_COMPARE_MEMBASE_IMM:
1859 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1860 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
1862 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1863 * OP_COMPARE_IMM reg, imm
1865 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
1867 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
1868 ins->inst_basereg == last_ins->inst_destbasereg &&
1869 ins->inst_offset == last_ins->inst_offset) {
1870 ins->opcode = OP_COMPARE_IMM;
1871 ins->sreg1 = last_ins->sreg1;
1873 /* check if we can remove cmp reg,0 with test null */
1875 ins->opcode = OP_X86_TEST_NULL;
1879 case OP_X86_PUSH_MEMBASE:
1880 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG ||
1881 last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1882 ins->inst_basereg == last_ins->inst_destbasereg &&
1883 ins->inst_offset == last_ins->inst_offset) {
1884 ins->opcode = OP_X86_PUSH;
1885 ins->sreg1 = last_ins->sreg1;
1890 mono_peephole_ins (bb, ins);
1895 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
1899 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
1900 switch (ins->opcode) {
1902 /* reg = 0 -> XOR (reg, reg) */
1903 /* XOR sets cflags on x86, so we cant do it always */
1904 if (ins->inst_c0 == 0 && (!ins->next || (ins->next && INST_IGNORES_CFLAGS (ins->next->opcode)))) {
1907 ins->opcode = OP_IXOR;
1908 ins->sreg1 = ins->dreg;
1909 ins->sreg2 = ins->dreg;
1912 * Convert succeeding STORE_MEMBASE_IMM 0 ins to STORE_MEMBASE_REG
1913 * since it takes 3 bytes instead of 7.
1915 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
1916 if ((ins2->opcode == OP_STORE_MEMBASE_IMM) && (ins2->inst_imm == 0)) {
1917 ins2->opcode = OP_STORE_MEMBASE_REG;
1918 ins2->sreg1 = ins->dreg;
1920 else if ((ins2->opcode == OP_STOREI4_MEMBASE_IMM) && (ins2->inst_imm == 0)) {
1921 ins2->opcode = OP_STOREI4_MEMBASE_REG;
1922 ins2->sreg1 = ins->dreg;
1924 else if ((ins2->opcode == OP_STOREI1_MEMBASE_IMM) || (ins2->opcode == OP_STOREI2_MEMBASE_IMM)) {
1925 /* Continue iteration */
1934 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1935 ins->opcode = OP_X86_INC_REG;
1939 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1940 ins->opcode = OP_X86_DEC_REG;
1944 mono_peephole_ins (bb, ins);
1949 * mono_arch_lowering_pass:
1951 * Converts complex opcodes into simpler ones so that each IR instruction
1952 * corresponds to one machine instruction.
1955 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1957 MonoInst *ins, *next;
1960 * FIXME: Need to add more instructions, but the current machine
1961 * description can't model some parts of the composite instructions like
1964 MONO_BB_FOR_EACH_INS_SAFE (bb, next, ins) {
1965 switch (ins->opcode) {
1968 case OP_IDIV_UN_IMM:
1969 case OP_IREM_UN_IMM:
1971 * Keep the cases where we could generated optimized code, otherwise convert
1972 * to the non-imm variant.
1974 if ((ins->opcode == OP_IREM_IMM) && mono_is_power_of_two (ins->inst_imm) >= 0)
1976 mono_decompose_op_imm (cfg, bb, ins);
1983 bb->max_vreg = cfg->next_vreg;
1987 branch_cc_table [] = {
1988 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1989 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1990 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
1993 /* Maps CMP_... constants to X86_CC_... constants */
1996 X86_CC_EQ, X86_CC_NE, X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT,
1997 X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT
2001 cc_signed_table [] = {
2002 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
2003 FALSE, FALSE, FALSE, FALSE
2006 static unsigned char*
2007 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
2009 #define XMM_TEMP_REG 0
2010 /*This SSE2 optimization must not be done which OPT_SIMD in place as it clobbers xmm0.*/
2011 /*The xmm pass decomposes OP_FCONV_ ops anyway anyway.*/
2012 if (cfg->opt & MONO_OPT_SSE2 && size < 8 && !(cfg->opt & MONO_OPT_SIMD)) {
2013 /* optimize by assigning a local var for this use so we avoid
2014 * the stack manipulations */
2015 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2016 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
2017 x86_movsd_reg_membase (code, XMM_TEMP_REG, X86_ESP, 0);
2018 x86_cvttsd2si (code, dreg, XMM_TEMP_REG);
2019 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
2021 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
2023 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
2026 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
2027 x86_fnstcw_membase(code, X86_ESP, 0);
2028 x86_mov_reg_membase (code, dreg, X86_ESP, 0, 2);
2029 x86_alu_reg_imm (code, X86_OR, dreg, 0xc00);
2030 x86_mov_membase_reg (code, X86_ESP, 2, dreg, 2);
2031 x86_fldcw_membase (code, X86_ESP, 2);
2033 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2034 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
2035 x86_pop_reg (code, dreg);
2036 /* FIXME: need the high register
2037 * x86_pop_reg (code, dreg_high);
2040 x86_push_reg (code, X86_EAX); // SP = SP - 4
2041 x86_fist_pop_membase (code, X86_ESP, 0, FALSE);
2042 x86_pop_reg (code, dreg);
2044 x86_fldcw_membase (code, X86_ESP, 0);
2045 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2048 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
2050 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
2054 static unsigned char*
2055 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
2057 int sreg = tree->sreg1;
2058 int need_touch = FALSE;
2060 #if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
2069 * If requested stack size is larger than one page,
2070 * perform stack-touch operation
2073 * Generate stack probe code.
2074 * Under Windows, it is necessary to allocate one page at a time,
2075 * "touching" stack after each successful sub-allocation. This is
2076 * because of the way stack growth is implemented - there is a
2077 * guard page before the lowest stack page that is currently commited.
2078 * Stack normally grows sequentially so OS traps access to the
2079 * guard page and commits more pages when needed.
2081 x86_test_reg_imm (code, sreg, ~0xFFF);
2082 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2084 br[2] = code; /* loop */
2085 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
2086 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
2089 * By the end of the loop, sreg2 is smaller than 0x1000, so the init routine
2090 * that follows only initializes the last part of the area.
2092 /* Same as the init code below with size==0x1000 */
2093 if (tree->flags & MONO_INST_INIT) {
2094 x86_push_reg (code, X86_EAX);
2095 x86_push_reg (code, X86_ECX);
2096 x86_push_reg (code, X86_EDI);
2097 x86_mov_reg_imm (code, X86_ECX, (0x1000 >> 2));
2098 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2099 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
2101 x86_prefix (code, X86_REP_PREFIX);
2103 x86_pop_reg (code, X86_EDI);
2104 x86_pop_reg (code, X86_ECX);
2105 x86_pop_reg (code, X86_EAX);
2108 x86_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
2109 x86_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
2110 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
2111 x86_patch (br[3], br[2]);
2112 x86_test_reg_reg (code, sreg, sreg);
2113 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2114 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
2116 br[1] = code; x86_jump8 (code, 0);
2118 x86_patch (br[0], code);
2119 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
2120 x86_patch (br[1], code);
2121 x86_patch (br[4], code);
2124 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
2126 if (tree->flags & MONO_INST_INIT) {
2128 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
2129 x86_push_reg (code, X86_EAX);
2132 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
2133 x86_push_reg (code, X86_ECX);
2136 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
2137 x86_push_reg (code, X86_EDI);
2141 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
2142 if (sreg != X86_ECX)
2143 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
2144 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2146 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
2148 x86_prefix (code, X86_REP_PREFIX);
2151 if (tree->dreg != X86_EDI && sreg != X86_EDI)
2152 x86_pop_reg (code, X86_EDI);
2153 if (tree->dreg != X86_ECX && sreg != X86_ECX)
2154 x86_pop_reg (code, X86_ECX);
2155 if (tree->dreg != X86_EAX && sreg != X86_EAX)
2156 x86_pop_reg (code, X86_EAX);
2163 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
2165 /* Move return value to the target register */
2166 switch (ins->opcode) {
2169 case OP_CALL_MEMBASE:
2170 if (ins->dreg != X86_EAX)
2171 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
2181 mono_x86_have_tls_get (void)
2184 guint32 *ins = (guint32*)pthread_getspecific;
2186 * We're looking for these two instructions:
2188 * mov 0x4(%esp),%eax
2189 * mov %gs:0x48(,%eax,4),%eax
2191 return ins [0] == 0x0424448b && ins [1] == 0x85048b65 && ins [2] == 0x00000048;
2198 * mono_x86_emit_tls_get:
2199 * @code: buffer to store code to
2200 * @dreg: hard register where to place the result
2201 * @tls_offset: offset info
2203 * mono_x86_emit_tls_get emits in @code the native code that puts in
2204 * the dreg register the item in the thread local storage identified
2207 * Returns: a pointer to the end of the stored code
2210 mono_x86_emit_tls_get (guint8* code, int dreg, int tls_offset)
2212 #if defined(__APPLE__)
2213 x86_prefix (code, X86_GS_PREFIX);
2214 x86_mov_reg_mem (code, dreg, 0x48 + tls_offset * 4, 4);
2215 #elif defined(TARGET_WIN32)
2217 * See the Under the Hood article in the May 1996 issue of Microsoft Systems
2218 * Journal and/or a disassembly of the TlsGet () function.
2220 g_assert (tls_offset < 64);
2221 x86_prefix (code, X86_FS_PREFIX);
2222 x86_mov_reg_mem (code, dreg, 0x18, 4);
2223 /* Dunno what this does but TlsGetValue () contains it */
2224 x86_alu_membase_imm (code, X86_AND, dreg, 0x34, 0);
2225 x86_mov_reg_membase (code, dreg, dreg, 3600 + (tls_offset * 4), 4);
2227 if (optimize_for_xen) {
2228 x86_prefix (code, X86_GS_PREFIX);
2229 x86_mov_reg_mem (code, dreg, 0, 4);
2230 x86_mov_reg_membase (code, dreg, dreg, tls_offset, 4);
2232 x86_prefix (code, X86_GS_PREFIX);
2233 x86_mov_reg_mem (code, dreg, tls_offset, 4);
2240 * emit_load_volatile_arguments:
2242 * Load volatile arguments from the stack to the original input registers.
2243 * Required before a tail call.
2246 emit_load_volatile_arguments (MonoCompile *cfg, guint8 *code)
2248 MonoMethod *method = cfg->method;
2249 MonoMethodSignature *sig;
2254 /* FIXME: Generate intermediate code instead */
2256 sig = mono_method_signature (method);
2258 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, FALSE);
2260 /* This is the opposite of the code in emit_prolog */
2262 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2263 ArgInfo *ainfo = cinfo->args + i;
2265 inst = cfg->args [i];
2267 if (sig->hasthis && (i == 0))
2268 arg_type = &mono_defaults.object_class->byval_arg;
2270 arg_type = sig->params [i - sig->hasthis];
2273 * On x86, the arguments are either in their original stack locations, or in
2276 if (inst->opcode == OP_REGVAR) {
2277 g_assert (ainfo->storage == ArgOnStack);
2279 x86_mov_membase_reg (code, X86_EBP, inst->inst_offset, inst->dreg, 4);
2286 #define REAL_PRINT_REG(text,reg) \
2287 mono_assert (reg >= 0); \
2288 x86_push_reg (code, X86_EAX); \
2289 x86_push_reg (code, X86_EDX); \
2290 x86_push_reg (code, X86_ECX); \
2291 x86_push_reg (code, reg); \
2292 x86_push_imm (code, reg); \
2293 x86_push_imm (code, text " %d %p\n"); \
2294 x86_mov_reg_imm (code, X86_EAX, printf); \
2295 x86_call_reg (code, X86_EAX); \
2296 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 3*4); \
2297 x86_pop_reg (code, X86_ECX); \
2298 x86_pop_reg (code, X86_EDX); \
2299 x86_pop_reg (code, X86_EAX);
2301 /* REAL_PRINT_REG does not appear to be used, and was not adapted to work with Native Client. */
2302 #ifdef __native__client_codegen__
2303 #define REAL_PRINT_REG(text, reg) g_assert_not_reached()
2306 /* benchmark and set based on cpu */
2307 #define LOOP_ALIGNMENT 8
2308 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
2313 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2318 guint8 *code = cfg->native_code + cfg->code_len;
2321 if (cfg->opt & MONO_OPT_LOOP) {
2322 int pad, align = LOOP_ALIGNMENT;
2323 /* set alignment depending on cpu */
2324 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
2326 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
2327 x86_padding (code, pad);
2328 cfg->code_len += pad;
2329 bb->native_offset = cfg->code_len;
2332 #ifdef __native_client_codegen__
2334 /* For Native Client, all indirect call/jump targets must be */
2335 /* 32-byte aligned. Exception handler blocks are jumped to */
2336 /* indirectly as well. */
2337 gboolean bb_needs_alignment = (bb->flags & BB_INDIRECT_JUMP_TARGET) ||
2338 (bb->flags & BB_EXCEPTION_HANDLER);
2340 /* if ((cfg->code_len & kNaClAlignmentMask) != 0) { */
2341 if ( bb_needs_alignment && ((cfg->code_len & kNaClAlignmentMask) != 0)) {
2342 int pad = kNaClAlignment - (cfg->code_len & kNaClAlignmentMask);
2343 if (pad != kNaClAlignment) code = mono_arch_nacl_pad(code, pad);
2344 cfg->code_len += pad;
2345 bb->native_offset = cfg->code_len;
2348 #endif /* __native_client_codegen__ */
2349 if (cfg->verbose_level > 2)
2350 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2352 cpos = bb->max_offset;
2354 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2355 MonoProfileCoverageInfo *cov = cfg->coverage_info;
2356 g_assert (!cfg->compile_aot);
2359 cov->data [bb->dfn].cil_code = bb->cil_code;
2360 /* this is not thread save, but good enough */
2361 x86_inc_mem (code, &cov->data [bb->dfn].count);
2364 offset = code - cfg->native_code;
2366 mono_debug_open_block (cfg, bb, offset);
2368 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)
2369 x86_breakpoint (code);
2371 MONO_BB_FOR_EACH_INS (bb, ins) {
2372 offset = code - cfg->native_code;
2374 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
2376 #define EXTRA_CODE_SPACE (NACL_SIZE (16, 16 + kNaClAlignment))
2378 if (G_UNLIKELY (offset > (cfg->code_size - max_len - EXTRA_CODE_SPACE))) {
2379 cfg->code_size *= 2;
2380 cfg->native_code = mono_realloc_native_code(cfg);
2381 code = cfg->native_code + offset;
2382 mono_jit_stats.code_reallocs++;
2385 if (cfg->debug_info)
2386 mono_debug_record_line_number (cfg, ins, offset);
2388 switch (ins->opcode) {
2390 x86_mul_reg (code, ins->sreg2, TRUE);
2393 x86_mul_reg (code, ins->sreg2, FALSE);
2395 case OP_X86_SETEQ_MEMBASE:
2396 case OP_X86_SETNE_MEMBASE:
2397 x86_set_membase (code, ins->opcode == OP_X86_SETEQ_MEMBASE ? X86_CC_EQ : X86_CC_NE,
2398 ins->inst_basereg, ins->inst_offset, TRUE);
2400 case OP_STOREI1_MEMBASE_IMM:
2401 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
2403 case OP_STOREI2_MEMBASE_IMM:
2404 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
2406 case OP_STORE_MEMBASE_IMM:
2407 case OP_STOREI4_MEMBASE_IMM:
2408 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
2410 case OP_STOREI1_MEMBASE_REG:
2411 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
2413 case OP_STOREI2_MEMBASE_REG:
2414 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
2416 case OP_STORE_MEMBASE_REG:
2417 case OP_STOREI4_MEMBASE_REG:
2418 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
2420 case OP_STORE_MEM_IMM:
2421 x86_mov_mem_imm (code, ins->inst_p0, ins->inst_c0, 4);
2424 x86_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
2428 /* These are created by the cprop pass so they use inst_imm as the source */
2429 x86_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
2432 x86_widen_mem (code, ins->dreg, ins->inst_imm, FALSE, FALSE);
2435 x86_widen_mem (code, ins->dreg, ins->inst_imm, FALSE, TRUE);
2437 case OP_LOAD_MEMBASE:
2438 case OP_LOADI4_MEMBASE:
2439 case OP_LOADU4_MEMBASE:
2440 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
2442 case OP_LOADU1_MEMBASE:
2443 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
2445 case OP_LOADI1_MEMBASE:
2446 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
2448 case OP_LOADU2_MEMBASE:
2449 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
2451 case OP_LOADI2_MEMBASE:
2452 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
2454 case OP_ICONV_TO_I1:
2456 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2458 case OP_ICONV_TO_I2:
2460 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2462 case OP_ICONV_TO_U1:
2463 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
2465 case OP_ICONV_TO_U2:
2466 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
2470 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
2472 case OP_COMPARE_IMM:
2473 case OP_ICOMPARE_IMM:
2474 x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
2476 case OP_X86_COMPARE_MEMBASE_REG:
2477 x86_alu_membase_reg (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2479 case OP_X86_COMPARE_MEMBASE_IMM:
2480 x86_alu_membase_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2482 case OP_X86_COMPARE_MEMBASE8_IMM:
2483 x86_alu_membase8_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2485 case OP_X86_COMPARE_REG_MEMBASE:
2486 x86_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
2488 case OP_X86_COMPARE_MEM_IMM:
2489 x86_alu_mem_imm (code, X86_CMP, ins->inst_offset, ins->inst_imm);
2491 case OP_X86_TEST_NULL:
2492 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2494 case OP_X86_ADD_MEMBASE_IMM:
2495 x86_alu_membase_imm (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2497 case OP_X86_ADD_REG_MEMBASE:
2498 x86_alu_reg_membase (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset);
2500 case OP_X86_SUB_MEMBASE_IMM:
2501 x86_alu_membase_imm (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2503 case OP_X86_SUB_REG_MEMBASE:
2504 x86_alu_reg_membase (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset);
2506 case OP_X86_AND_MEMBASE_IMM:
2507 x86_alu_membase_imm (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2509 case OP_X86_OR_MEMBASE_IMM:
2510 x86_alu_membase_imm (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2512 case OP_X86_XOR_MEMBASE_IMM:
2513 x86_alu_membase_imm (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2515 case OP_X86_ADD_MEMBASE_REG:
2516 x86_alu_membase_reg (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2518 case OP_X86_SUB_MEMBASE_REG:
2519 x86_alu_membase_reg (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2521 case OP_X86_AND_MEMBASE_REG:
2522 x86_alu_membase_reg (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2524 case OP_X86_OR_MEMBASE_REG:
2525 x86_alu_membase_reg (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2527 case OP_X86_XOR_MEMBASE_REG:
2528 x86_alu_membase_reg (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2530 case OP_X86_INC_MEMBASE:
2531 x86_inc_membase (code, ins->inst_basereg, ins->inst_offset);
2533 case OP_X86_INC_REG:
2534 x86_inc_reg (code, ins->dreg);
2536 case OP_X86_DEC_MEMBASE:
2537 x86_dec_membase (code, ins->inst_basereg, ins->inst_offset);
2539 case OP_X86_DEC_REG:
2540 x86_dec_reg (code, ins->dreg);
2542 case OP_X86_MUL_REG_MEMBASE:
2543 x86_imul_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
2545 case OP_X86_AND_REG_MEMBASE:
2546 x86_alu_reg_membase (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset);
2548 case OP_X86_OR_REG_MEMBASE:
2549 x86_alu_reg_membase (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset);
2551 case OP_X86_XOR_REG_MEMBASE:
2552 x86_alu_reg_membase (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset);
2555 x86_breakpoint (code);
2557 case OP_RELAXED_NOP:
2558 x86_prefix (code, X86_REP_PREFIX);
2566 case OP_DUMMY_STORE:
2567 case OP_NOT_REACHED:
2570 case OP_SEQ_POINT: {
2573 if (cfg->compile_aot)
2577 * Read from the single stepping trigger page. This will cause a
2578 * SIGSEGV when single stepping is enabled.
2579 * We do this _before_ the breakpoint, so single stepping after
2580 * a breakpoint is hit will step to the next IL offset.
2582 if (ins->flags & MONO_INST_SINGLE_STEP_LOC)
2583 x86_alu_reg_mem (code, X86_CMP, X86_EAX, (guint32)ss_trigger_page);
2585 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
2588 * A placeholder for a possible breakpoint inserted by
2589 * mono_arch_set_breakpoint ().
2591 for (i = 0; i < 6; ++i)
2598 x86_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
2602 x86_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
2607 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
2611 x86_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
2616 x86_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
2620 x86_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
2625 x86_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
2629 x86_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
2632 x86_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
2636 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
2641 * The code is the same for div/rem, the allocator will allocate dreg
2642 * to RAX/RDX as appropriate.
2644 if (ins->sreg2 == X86_EDX) {
2645 /* cdq clobbers this */
2646 x86_push_reg (code, ins->sreg2);
2648 x86_div_membase (code, X86_ESP, 0, TRUE);
2649 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2652 x86_div_reg (code, ins->sreg2, TRUE);
2657 if (ins->sreg2 == X86_EDX) {
2658 x86_push_reg (code, ins->sreg2);
2659 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2660 x86_div_membase (code, X86_ESP, 0, FALSE);
2661 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2663 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2664 x86_div_reg (code, ins->sreg2, FALSE);
2668 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2670 x86_div_reg (code, ins->sreg2, TRUE);
2673 int power = mono_is_power_of_two (ins->inst_imm);
2675 g_assert (ins->sreg1 == X86_EAX);
2676 g_assert (ins->dreg == X86_EAX);
2677 g_assert (power >= 0);
2680 /* Based on http://compilers.iecc.com/comparch/article/93-04-079 */
2682 x86_alu_reg_imm (code, X86_AND, X86_EAX, 1);
2684 * If the divident is >= 0, this does not nothing. If it is positive, it
2685 * it transforms %eax=0 into %eax=0, and %eax=1 into %eax=-1.
2687 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EDX);
2688 x86_alu_reg_reg (code, X86_SUB, X86_EAX, X86_EDX);
2689 } else if (power == 0) {
2690 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2692 /* Based on gcc code */
2694 /* Add compensation for negative dividents */
2696 x86_shift_reg_imm (code, X86_SHR, X86_EDX, 32 - power);
2697 x86_alu_reg_reg (code, X86_ADD, X86_EAX, X86_EDX);
2698 /* Compute remainder */
2699 x86_alu_reg_imm (code, X86_AND, X86_EAX, (1 << power) - 1);
2700 /* Remove compensation */
2701 x86_alu_reg_reg (code, X86_SUB, X86_EAX, X86_EDX);
2706 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
2710 x86_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
2713 x86_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
2717 x86_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
2720 g_assert (ins->sreg2 == X86_ECX);
2721 x86_shift_reg (code, X86_SHL, ins->dreg);
2724 g_assert (ins->sreg2 == X86_ECX);
2725 x86_shift_reg (code, X86_SAR, ins->dreg);
2729 x86_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
2732 case OP_ISHR_UN_IMM:
2733 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
2736 g_assert (ins->sreg2 == X86_ECX);
2737 x86_shift_reg (code, X86_SHR, ins->dreg);
2741 x86_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
2744 guint8 *jump_to_end;
2746 /* handle shifts below 32 bits */
2747 x86_shld_reg (code, ins->backend.reg3, ins->sreg1);
2748 x86_shift_reg (code, X86_SHL, ins->sreg1);
2750 x86_test_reg_imm (code, X86_ECX, 32);
2751 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
2753 /* handle shift over 32 bit */
2754 x86_mov_reg_reg (code, ins->backend.reg3, ins->sreg1, 4);
2755 x86_clear_reg (code, ins->sreg1);
2757 x86_patch (jump_to_end, code);
2761 guint8 *jump_to_end;
2763 /* handle shifts below 32 bits */
2764 x86_shrd_reg (code, ins->sreg1, ins->backend.reg3);
2765 x86_shift_reg (code, X86_SAR, ins->backend.reg3);
2767 x86_test_reg_imm (code, X86_ECX, 32);
2768 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
2770 /* handle shifts over 31 bits */
2771 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2772 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, 31);
2774 x86_patch (jump_to_end, code);
2778 guint8 *jump_to_end;
2780 /* handle shifts below 32 bits */
2781 x86_shrd_reg (code, ins->sreg1, ins->backend.reg3);
2782 x86_shift_reg (code, X86_SHR, ins->backend.reg3);
2784 x86_test_reg_imm (code, X86_ECX, 32);
2785 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
2787 /* handle shifts over 31 bits */
2788 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2789 x86_clear_reg (code, ins->backend.reg3);
2791 x86_patch (jump_to_end, code);
2795 if (ins->inst_imm >= 32) {
2796 x86_mov_reg_reg (code, ins->backend.reg3, ins->sreg1, 4);
2797 x86_clear_reg (code, ins->sreg1);
2798 x86_shift_reg_imm (code, X86_SHL, ins->backend.reg3, ins->inst_imm - 32);
2800 x86_shld_reg_imm (code, ins->backend.reg3, ins->sreg1, ins->inst_imm);
2801 x86_shift_reg_imm (code, X86_SHL, ins->sreg1, ins->inst_imm);
2805 if (ins->inst_imm >= 32) {
2806 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2807 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, 0x1f);
2808 x86_shift_reg_imm (code, X86_SAR, ins->sreg1, ins->inst_imm - 32);
2810 x86_shrd_reg_imm (code, ins->sreg1, ins->backend.reg3, ins->inst_imm);
2811 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, ins->inst_imm);
2814 case OP_LSHR_UN_IMM:
2815 if (ins->inst_imm >= 32) {
2816 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2817 x86_clear_reg (code, ins->backend.reg3);
2818 x86_shift_reg_imm (code, X86_SHR, ins->sreg1, ins->inst_imm - 32);
2820 x86_shrd_reg_imm (code, ins->sreg1, ins->backend.reg3, ins->inst_imm);
2821 x86_shift_reg_imm (code, X86_SHR, ins->backend.reg3, ins->inst_imm);
2825 x86_not_reg (code, ins->sreg1);
2828 x86_neg_reg (code, ins->sreg1);
2832 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2836 switch (ins->inst_imm) {
2840 if (ins->dreg != ins->sreg1)
2841 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2842 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
2845 /* LEA r1, [r2 + r2*2] */
2846 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
2849 /* LEA r1, [r2 + r2*4] */
2850 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2853 /* LEA r1, [r2 + r2*2] */
2855 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
2856 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
2859 /* LEA r1, [r2 + r2*8] */
2860 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
2863 /* LEA r1, [r2 + r2*4] */
2865 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2866 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
2869 /* LEA r1, [r2 + r2*2] */
2871 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
2872 x86_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
2875 /* LEA r1, [r2 + r2*4] */
2876 /* LEA r1, [r1 + r1*4] */
2877 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2878 x86_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
2881 /* LEA r1, [r2 + r2*4] */
2883 /* LEA r1, [r1 + r1*4] */
2884 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2885 x86_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
2886 x86_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
2889 x86_imul_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_imm);
2894 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2895 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2897 case OP_IMUL_OVF_UN: {
2898 /* the mul operation and the exception check should most likely be split */
2899 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
2900 /*g_assert (ins->sreg2 == X86_EAX);
2901 g_assert (ins->dreg == X86_EAX);*/
2902 if (ins->sreg2 == X86_EAX) {
2903 non_eax_reg = ins->sreg1;
2904 } else if (ins->sreg1 == X86_EAX) {
2905 non_eax_reg = ins->sreg2;
2907 /* no need to save since we're going to store to it anyway */
2908 if (ins->dreg != X86_EAX) {
2910 x86_push_reg (code, X86_EAX);
2912 x86_mov_reg_reg (code, X86_EAX, ins->sreg1, 4);
2913 non_eax_reg = ins->sreg2;
2915 if (ins->dreg == X86_EDX) {
2918 x86_push_reg (code, X86_EAX);
2920 } else if (ins->dreg != X86_EAX) {
2922 x86_push_reg (code, X86_EDX);
2924 x86_mul_reg (code, non_eax_reg, FALSE);
2925 /* save before the check since pop and mov don't change the flags */
2926 if (ins->dreg != X86_EAX)
2927 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
2929 x86_pop_reg (code, X86_EDX);
2931 x86_pop_reg (code, X86_EAX);
2932 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2936 x86_mov_reg_imm (code, ins->dreg, ins->inst_c0);
2939 g_assert_not_reached ();
2940 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2941 x86_mov_reg_imm (code, ins->dreg, 0);
2944 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2945 x86_mov_reg_imm (code, ins->dreg, 0);
2947 case OP_LOAD_GOTADDR:
2948 g_assert (ins->dreg == MONO_ARCH_GOT_REG);
2949 code = mono_arch_emit_load_got_addr (cfg->native_code, code, cfg, NULL);
2952 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
2953 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, 0xf0f0f0f0, 4);
2955 case OP_X86_PUSH_GOT_ENTRY:
2956 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
2957 x86_push_membase (code, ins->inst_basereg, 0xf0f0f0f0);
2960 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2964 * Note: this 'frame destruction' logic is useful for tail calls, too.
2965 * Keep in sync with the code in emit_epilog.
2969 /* FIXME: no tracing support... */
2970 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
2971 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
2972 /* reset offset to make max_len work */
2973 offset = code - cfg->native_code;
2975 g_assert (!cfg->method->save_lmf);
2977 code = emit_load_volatile_arguments (cfg, code);
2979 if (cfg->used_int_regs & (1 << X86_EBX))
2981 if (cfg->used_int_regs & (1 << X86_EDI))
2983 if (cfg->used_int_regs & (1 << X86_ESI))
2986 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
2988 if (cfg->used_int_regs & (1 << X86_ESI))
2989 x86_pop_reg (code, X86_ESI);
2990 if (cfg->used_int_regs & (1 << X86_EDI))
2991 x86_pop_reg (code, X86_EDI);
2992 if (cfg->used_int_regs & (1 << X86_EBX))
2993 x86_pop_reg (code, X86_EBX);
2995 /* restore ESP/EBP */
2997 offset = code - cfg->native_code;
2998 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2999 x86_jump32 (code, 0);
3001 cfg->disable_aot = TRUE;
3005 /* ensure ins->sreg1 is not NULL
3006 * note that cmp DWORD PTR [eax], eax is one byte shorter than
3007 * cmp DWORD PTR [eax], 0
3009 x86_alu_membase_reg (code, X86_CMP, ins->sreg1, 0, ins->sreg1);
3012 int hreg = ins->sreg1 == X86_EAX? X86_ECX: X86_EAX;
3013 x86_push_reg (code, hreg);
3014 x86_lea_membase (code, hreg, X86_EBP, cfg->sig_cookie);
3015 x86_mov_membase_reg (code, ins->sreg1, 0, hreg, 4);
3016 x86_pop_reg (code, hreg);
3025 call = (MonoCallInst*)ins;
3026 if (ins->flags & MONO_INST_HAS_METHOD)
3027 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method);
3029 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr);
3030 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
3031 /* a pop is one byte, while an add reg, imm is 3. So if there are 4 or 8
3032 * bytes to pop, we want to use pops. GCC does this (note it won't happen
3033 * for P4 or i686 because gcc will avoid using pop push at all. But we aren't
3034 * smart enough to do that optimization yet
3036 * It turns out that on my P4, doing two pops for 8 bytes on the stack makes
3037 * mcs botstrap slow down. However, doing 1 pop for 4 bytes creates a small,
3038 * (most likely from locality benefits). People with other processors should
3039 * check on theirs to see what happens.
3041 if (call->stack_usage == 4) {
3042 /* we want to use registers that won't get used soon, so use
3043 * ecx, as eax will get allocated first. edx is used by long calls,
3044 * so we can't use that.
3047 x86_pop_reg (code, X86_ECX);
3049 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
3052 code = emit_move_return_value (cfg, ins, code);
3058 case OP_VOIDCALL_REG:
3060 call = (MonoCallInst*)ins;
3061 x86_call_reg (code, ins->sreg1);
3062 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
3063 if (call->stack_usage == 4)
3064 x86_pop_reg (code, X86_ECX);
3066 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
3068 code = emit_move_return_value (cfg, ins, code);
3070 case OP_FCALL_MEMBASE:
3071 case OP_LCALL_MEMBASE:
3072 case OP_VCALL_MEMBASE:
3073 case OP_VCALL2_MEMBASE:
3074 case OP_VOIDCALL_MEMBASE:
3075 case OP_CALL_MEMBASE:
3076 call = (MonoCallInst*)ins;
3078 x86_call_membase (code, ins->sreg1, ins->inst_offset);
3079 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
3080 if (call->stack_usage == 4)
3081 x86_pop_reg (code, X86_ECX);
3083 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
3085 code = emit_move_return_value (cfg, ins, code);
3088 x86_push_reg (code, ins->sreg1);
3090 case OP_X86_PUSH_IMM:
3091 x86_push_imm (code, ins->inst_imm);
3093 case OP_X86_PUSH_MEMBASE:
3094 x86_push_membase (code, ins->inst_basereg, ins->inst_offset);
3096 case OP_X86_PUSH_OBJ:
3097 x86_alu_reg_imm (code, X86_SUB, X86_ESP, ins->inst_imm);
3098 x86_push_reg (code, X86_EDI);
3099 x86_push_reg (code, X86_ESI);
3100 x86_push_reg (code, X86_ECX);
3101 if (ins->inst_offset)
3102 x86_lea_membase (code, X86_ESI, ins->inst_basereg, ins->inst_offset);
3104 x86_mov_reg_reg (code, X86_ESI, ins->inst_basereg, 4);
3105 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
3106 x86_mov_reg_imm (code, X86_ECX, (ins->inst_imm >> 2));
3108 x86_prefix (code, X86_REP_PREFIX);
3110 x86_pop_reg (code, X86_ECX);
3111 x86_pop_reg (code, X86_ESI);
3112 x86_pop_reg (code, X86_EDI);
3115 x86_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
3117 case OP_X86_LEA_MEMBASE:
3118 x86_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
3121 x86_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
3124 /* keep alignment */
3125 x86_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_LOCALLOC_ALIGNMENT - 1);
3126 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_LOCALLOC_ALIGNMENT - 1));
3127 code = mono_emit_stack_alloc (code, ins);
3128 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
3130 case OP_LOCALLOC_IMM: {
3131 guint32 size = ins->inst_imm;
3132 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
3134 if (ins->flags & MONO_INST_INIT) {
3135 /* FIXME: Optimize this */
3136 x86_mov_reg_imm (code, ins->dreg, size);
3137 ins->sreg1 = ins->dreg;
3139 code = mono_emit_stack_alloc (code, ins);
3140 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
3142 x86_alu_reg_imm (code, X86_SUB, X86_ESP, size);
3143 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
3148 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3149 x86_push_reg (code, ins->sreg1);
3150 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
3151 (gpointer)"mono_arch_throw_exception");
3155 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3156 x86_push_reg (code, ins->sreg1);
3157 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
3158 (gpointer)"mono_arch_rethrow_exception");
3161 case OP_CALL_HANDLER:
3162 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3163 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3164 x86_call_imm (code, 0);
3165 mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
3166 x86_alu_reg_imm (code, X86_ADD, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3168 case OP_START_HANDLER: {
3169 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3170 x86_mov_membase_reg (code, spvar->inst_basereg, spvar->inst_offset, X86_ESP, 4);
3173 case OP_ENDFINALLY: {
3174 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3175 x86_mov_reg_membase (code, X86_ESP, spvar->inst_basereg, spvar->inst_offset, 4);
3179 case OP_ENDFILTER: {
3180 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3181 x86_mov_reg_membase (code, X86_ESP, spvar->inst_basereg, spvar->inst_offset, 4);
3182 /* The local allocator will put the result into EAX */
3188 ins->inst_c0 = code - cfg->native_code;
3191 if (ins->inst_target_bb->native_offset) {
3192 x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
3194 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3195 if ((cfg->opt & MONO_OPT_BRANCH) &&
3196 x86_is_imm8 (ins->inst_target_bb->max_offset - cpos))
3197 x86_jump8 (code, 0);
3199 x86_jump32 (code, 0);
3203 x86_jump_reg (code, ins->sreg1);
3216 x86_set_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
3217 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3219 case OP_COND_EXC_EQ:
3220 case OP_COND_EXC_NE_UN:
3221 case OP_COND_EXC_LT:
3222 case OP_COND_EXC_LT_UN:
3223 case OP_COND_EXC_GT:
3224 case OP_COND_EXC_GT_UN:
3225 case OP_COND_EXC_GE:
3226 case OP_COND_EXC_GE_UN:
3227 case OP_COND_EXC_LE:
3228 case OP_COND_EXC_LE_UN:
3229 case OP_COND_EXC_IEQ:
3230 case OP_COND_EXC_INE_UN:
3231 case OP_COND_EXC_ILT:
3232 case OP_COND_EXC_ILT_UN:
3233 case OP_COND_EXC_IGT:
3234 case OP_COND_EXC_IGT_UN:
3235 case OP_COND_EXC_IGE:
3236 case OP_COND_EXC_IGE_UN:
3237 case OP_COND_EXC_ILE:
3238 case OP_COND_EXC_ILE_UN:
3239 EMIT_COND_SYSTEM_EXCEPTION (cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)], ins->inst_p1);
3241 case OP_COND_EXC_OV:
3242 case OP_COND_EXC_NO:
3244 case OP_COND_EXC_NC:
3245 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ], (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
3247 case OP_COND_EXC_IOV:
3248 case OP_COND_EXC_INO:
3249 case OP_COND_EXC_IC:
3250 case OP_COND_EXC_INC:
3251 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_IEQ], (ins->opcode < OP_COND_EXC_INE_UN), ins->inst_p1);
3263 EMIT_COND_BRANCH (ins, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
3271 case OP_CMOV_INE_UN:
3272 case OP_CMOV_IGE_UN:
3273 case OP_CMOV_IGT_UN:
3274 case OP_CMOV_ILE_UN:
3275 case OP_CMOV_ILT_UN:
3276 g_assert (ins->dreg == ins->sreg1);
3277 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);
3280 /* floating point opcodes */
3282 double d = *(double *)ins->inst_p0;
3284 if ((d == 0.0) && (mono_signbit (d) == 0)) {
3286 } else if (d == 1.0) {
3289 if (cfg->compile_aot) {
3290 guint32 *val = (guint32*)&d;
3291 x86_push_imm (code, val [1]);
3292 x86_push_imm (code, val [0]);
3293 x86_fld_membase (code, X86_ESP, 0, TRUE);
3294 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3297 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R8, ins->inst_p0);
3298 x86_fld (code, NULL, TRUE);
3304 float f = *(float *)ins->inst_p0;
3306 if ((f == 0.0) && (mono_signbit (f) == 0)) {
3308 } else if (f == 1.0) {
3311 if (cfg->compile_aot) {
3312 guint32 val = *(guint32*)&f;
3313 x86_push_imm (code, val);
3314 x86_fld_membase (code, X86_ESP, 0, FALSE);
3315 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3318 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R4, ins->inst_p0);
3319 x86_fld (code, NULL, FALSE);
3324 case OP_STORER8_MEMBASE_REG:
3325 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, TRUE, TRUE);
3327 case OP_LOADR8_MEMBASE:
3328 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
3330 case OP_STORER4_MEMBASE_REG:
3331 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, FALSE, TRUE);
3333 case OP_LOADR4_MEMBASE:
3334 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
3336 case OP_ICONV_TO_R4:
3337 x86_push_reg (code, ins->sreg1);
3338 x86_fild_membase (code, X86_ESP, 0, FALSE);
3339 /* Change precision */
3340 x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
3341 x86_fld_membase (code, X86_ESP, 0, FALSE);
3342 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3344 case OP_ICONV_TO_R8:
3345 x86_push_reg (code, ins->sreg1);
3346 x86_fild_membase (code, X86_ESP, 0, FALSE);
3347 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3349 case OP_ICONV_TO_R_UN:
3350 x86_push_imm (code, 0);
3351 x86_push_reg (code, ins->sreg1);
3352 x86_fild_membase (code, X86_ESP, 0, TRUE);
3353 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3355 case OP_X86_FP_LOAD_I8:
3356 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
3358 case OP_X86_FP_LOAD_I4:
3359 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
3361 case OP_FCONV_TO_R4:
3362 /* Change precision */
3363 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
3364 x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
3365 x86_fld_membase (code, X86_ESP, 0, FALSE);
3366 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3368 case OP_FCONV_TO_I1:
3369 code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
3371 case OP_FCONV_TO_U1:
3372 code = emit_float_to_int (cfg, code, ins->dreg, 1, FALSE);
3374 case OP_FCONV_TO_I2:
3375 code = emit_float_to_int (cfg, code, ins->dreg, 2, TRUE);
3377 case OP_FCONV_TO_U2:
3378 code = emit_float_to_int (cfg, code, ins->dreg, 2, FALSE);
3380 case OP_FCONV_TO_I4:
3382 code = emit_float_to_int (cfg, code, ins->dreg, 4, TRUE);
3384 case OP_FCONV_TO_I8:
3385 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
3386 x86_fnstcw_membase(code, X86_ESP, 0);
3387 x86_mov_reg_membase (code, ins->dreg, X86_ESP, 0, 2);
3388 x86_alu_reg_imm (code, X86_OR, ins->dreg, 0xc00);
3389 x86_mov_membase_reg (code, X86_ESP, 2, ins->dreg, 2);
3390 x86_fldcw_membase (code, X86_ESP, 2);
3391 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
3392 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
3393 x86_pop_reg (code, ins->dreg);
3394 x86_pop_reg (code, ins->backend.reg3);
3395 x86_fldcw_membase (code, X86_ESP, 0);
3396 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3398 case OP_LCONV_TO_R8_2:
3399 x86_push_reg (code, ins->sreg2);
3400 x86_push_reg (code, ins->sreg1);
3401 x86_fild_membase (code, X86_ESP, 0, TRUE);
3402 /* Change precision */
3403 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
3404 x86_fld_membase (code, X86_ESP, 0, TRUE);
3405 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3407 case OP_LCONV_TO_R4_2:
3408 x86_push_reg (code, ins->sreg2);
3409 x86_push_reg (code, ins->sreg1);
3410 x86_fild_membase (code, X86_ESP, 0, TRUE);
3411 /* Change precision */
3412 x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
3413 x86_fld_membase (code, X86_ESP, 0, FALSE);
3414 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3416 case OP_LCONV_TO_R_UN_2: {
3417 static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
3420 /* load 64bit integer to FP stack */
3421 x86_push_reg (code, ins->sreg2);
3422 x86_push_reg (code, ins->sreg1);
3423 x86_fild_membase (code, X86_ESP, 0, TRUE);
3425 /* test if lreg is negative */
3426 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
3427 br = code; x86_branch8 (code, X86_CC_GEZ, 0, TRUE);
3429 /* add correction constant mn */
3430 x86_fld80_mem (code, mn);
3431 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3433 x86_patch (br, code);
3435 /* Change precision */
3436 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
3437 x86_fld_membase (code, X86_ESP, 0, TRUE);
3439 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3443 case OP_LCONV_TO_OVF_I:
3444 case OP_LCONV_TO_OVF_I4_2: {
3445 guint8 *br [3], *label [1];
3449 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
3451 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
3453 /* If the low word top bit is set, see if we are negative */
3454 br [0] = code; x86_branch8 (code, X86_CC_LT, 0, TRUE);
3455 /* We are not negative (no top bit set, check for our top word to be zero */
3456 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
3457 br [1] = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
3460 /* throw exception */
3461 tins = mono_branch_optimize_exception_target (cfg, bb, "OverflowException");
3463 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, tins->inst_true_bb);
3464 if ((cfg->opt & MONO_OPT_BRANCH) && x86_is_imm8 (tins->inst_true_bb->max_offset - cpos))
3465 x86_jump8 (code, 0);
3467 x86_jump32 (code, 0);
3469 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
3470 x86_jump32 (code, 0);
3474 x86_patch (br [0], code);
3475 /* our top bit is set, check that top word is 0xfffffff */
3476 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0xffffffff);
3478 x86_patch (br [1], code);
3479 /* nope, emit exception */
3480 br [2] = code; x86_branch8 (code, X86_CC_NE, 0, TRUE);
3481 x86_patch (br [2], label [0]);
3483 if (ins->dreg != ins->sreg1)
3484 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
3488 /* Not needed on the fp stack */
3491 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3494 x86_fp_op_reg (code, X86_FSUB, 1, TRUE);
3497 x86_fp_op_reg (code, X86_FMUL, 1, TRUE);
3500 x86_fp_op_reg (code, X86_FDIV, 1, TRUE);
3508 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3513 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3520 * it really doesn't make sense to inline all this code,
3521 * it's here just to show that things may not be as simple
3524 guchar *check_pos, *end_tan, *pop_jump;
3525 x86_push_reg (code, X86_EAX);
3528 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
3530 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3531 x86_fstp (code, 0); /* pop the 1.0 */
3533 x86_jump8 (code, 0);
3535 x86_fp_op (code, X86_FADD, 0);
3539 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
3541 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3544 x86_patch (pop_jump, code);
3545 x86_fstp (code, 0); /* pop the 1.0 */
3546 x86_patch (check_pos, code);
3547 x86_patch (end_tan, code);
3549 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3550 x86_pop_reg (code, X86_EAX);
3557 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3566 g_assert (cfg->opt & MONO_OPT_CMOV);
3567 g_assert (ins->dreg == ins->sreg1);
3568 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3569 x86_cmov_reg (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2);
3572 g_assert (cfg->opt & MONO_OPT_CMOV);
3573 g_assert (ins->dreg == ins->sreg1);
3574 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3575 x86_cmov_reg (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2);
3578 g_assert (cfg->opt & MONO_OPT_CMOV);
3579 g_assert (ins->dreg == ins->sreg1);
3580 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3581 x86_cmov_reg (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2);
3584 g_assert (cfg->opt & MONO_OPT_CMOV);
3585 g_assert (ins->dreg == ins->sreg1);
3586 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3587 x86_cmov_reg (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2);
3593 x86_fxch (code, ins->inst_imm);
3598 x86_push_reg (code, X86_EAX);
3599 /* we need to exchange ST(0) with ST(1) */
3602 /* this requires a loop, because fprem somtimes
3603 * returns a partial remainder */
3605 /* looks like MS is using fprem instead of the IEEE compatible fprem1 */
3606 /* x86_fprem1 (code); */
3609 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_C2);
3611 x86_branch8 (code, X86_CC_NE, l1 - l2, FALSE);
3616 x86_pop_reg (code, X86_EAX);
3620 if (cfg->opt & MONO_OPT_FCMOV) {
3621 x86_fcomip (code, 1);
3625 /* this overwrites EAX */
3626 EMIT_FPCOMPARE(code);
3627 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3630 if (cfg->opt & MONO_OPT_FCMOV) {
3631 /* zeroing the register at the start results in
3632 * shorter and faster code (we can also remove the widening op)
3634 guchar *unordered_check;
3635 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3636 x86_fcomip (code, 1);
3638 unordered_check = code;
3639 x86_branch8 (code, X86_CC_P, 0, FALSE);
3640 x86_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
3641 x86_patch (unordered_check, code);
3644 if (ins->dreg != X86_EAX)
3645 x86_push_reg (code, X86_EAX);
3647 EMIT_FPCOMPARE(code);
3648 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3649 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
3650 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3651 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3653 if (ins->dreg != X86_EAX)
3654 x86_pop_reg (code, X86_EAX);
3658 if (cfg->opt & MONO_OPT_FCMOV) {
3659 /* zeroing the register at the start results in
3660 * shorter and faster code (we can also remove the widening op)
3662 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3663 x86_fcomip (code, 1);
3665 if (ins->opcode == OP_FCLT_UN) {
3666 guchar *unordered_check = code;
3667 guchar *jump_to_end;
3668 x86_branch8 (code, X86_CC_P, 0, FALSE);
3669 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
3671 x86_jump8 (code, 0);
3672 x86_patch (unordered_check, code);
3673 x86_inc_reg (code, ins->dreg);
3674 x86_patch (jump_to_end, code);
3676 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
3680 if (ins->dreg != X86_EAX)
3681 x86_push_reg (code, X86_EAX);
3683 EMIT_FPCOMPARE(code);
3684 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3685 if (ins->opcode == OP_FCLT_UN) {
3686 guchar *is_not_zero_check, *end_jump;
3687 is_not_zero_check = code;
3688 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3690 x86_jump8 (code, 0);
3691 x86_patch (is_not_zero_check, code);
3692 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3694 x86_patch (end_jump, code);
3696 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3697 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3699 if (ins->dreg != X86_EAX)
3700 x86_pop_reg (code, X86_EAX);
3704 if (cfg->opt & MONO_OPT_FCMOV) {
3705 /* zeroing the register at the start results in
3706 * shorter and faster code (we can also remove the widening op)
3708 guchar *unordered_check;
3709 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3710 x86_fcomip (code, 1);
3712 if (ins->opcode == OP_FCGT) {
3713 unordered_check = code;
3714 x86_branch8 (code, X86_CC_P, 0, FALSE);
3715 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
3716 x86_patch (unordered_check, code);
3718 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
3722 if (ins->dreg != X86_EAX)
3723 x86_push_reg (code, X86_EAX);
3725 EMIT_FPCOMPARE(code);
3726 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3727 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3728 if (ins->opcode == OP_FCGT_UN) {
3729 guchar *is_not_zero_check, *end_jump;
3730 is_not_zero_check = code;
3731 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3733 x86_jump8 (code, 0);
3734 x86_patch (is_not_zero_check, code);
3735 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3737 x86_patch (end_jump, code);
3739 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3740 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3742 if (ins->dreg != X86_EAX)
3743 x86_pop_reg (code, X86_EAX);
3746 if (cfg->opt & MONO_OPT_FCMOV) {
3747 guchar *jump = code;
3748 x86_branch8 (code, X86_CC_P, 0, TRUE);
3749 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3750 x86_patch (jump, code);
3753 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
3754 EMIT_COND_BRANCH (ins, X86_CC_EQ, TRUE);
3757 /* Branch if C013 != 100 */
3758 if (cfg->opt & MONO_OPT_FCMOV) {
3759 /* branch if !ZF or (PF|CF) */
3760 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3761 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3762 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
3765 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
3766 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3769 if (cfg->opt & MONO_OPT_FCMOV) {
3770 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
3773 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3776 if (cfg->opt & MONO_OPT_FCMOV) {
3777 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3778 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
3781 if (ins->opcode == OP_FBLT_UN) {
3782 guchar *is_not_zero_check, *end_jump;
3783 is_not_zero_check = code;
3784 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3786 x86_jump8 (code, 0);
3787 x86_patch (is_not_zero_check, code);
3788 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3790 x86_patch (end_jump, code);
3792 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3796 if (cfg->opt & MONO_OPT_FCMOV) {
3797 if (ins->opcode == OP_FBGT) {
3800 /* skip branch if C1=1 */
3802 x86_branch8 (code, X86_CC_P, 0, FALSE);
3803 /* branch if (C0 | C3) = 1 */
3804 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
3805 x86_patch (br1, code);
3807 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
3811 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3812 if (ins->opcode == OP_FBGT_UN) {
3813 guchar *is_not_zero_check, *end_jump;
3814 is_not_zero_check = code;
3815 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3817 x86_jump8 (code, 0);
3818 x86_patch (is_not_zero_check, code);
3819 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3821 x86_patch (end_jump, code);
3823 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3826 /* Branch if C013 == 100 or 001 */
3827 if (cfg->opt & MONO_OPT_FCMOV) {
3830 /* skip branch if C1=1 */
3832 x86_branch8 (code, X86_CC_P, 0, FALSE);
3833 /* branch if (C0 | C3) = 1 */
3834 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
3835 x86_patch (br1, code);
3838 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3839 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3840 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
3841 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3844 /* Branch if C013 == 000 */
3845 if (cfg->opt & MONO_OPT_FCMOV) {
3846 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
3849 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3852 /* Branch if C013=000 or 100 */
3853 if (cfg->opt & MONO_OPT_FCMOV) {
3856 /* skip branch if C1=1 */
3858 x86_branch8 (code, X86_CC_P, 0, FALSE);
3859 /* branch if C0=0 */
3860 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
3861 x86_patch (br1, code);
3864 x86_alu_reg_imm (code, X86_AND, X86_EAX, (X86_FP_C0|X86_FP_C1));
3865 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0);
3866 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3869 /* Branch if C013 != 001 */
3870 if (cfg->opt & MONO_OPT_FCMOV) {
3871 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3872 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
3875 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3876 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3880 x86_push_reg (code, X86_EAX);
3883 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
3884 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3885 x86_pop_reg (code, X86_EAX);
3887 /* Have to clean up the fp stack before throwing the exception */
3889 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3892 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
3894 x86_patch (br1, code);
3898 code = mono_x86_emit_tls_get (code, ins->dreg, ins->inst_offset);
3901 case OP_MEMORY_BARRIER: {
3902 /* http://blogs.sun.com/dave/resource/NHM-Pipeline-Blog-V2.txt */
3903 x86_prefix (code, X86_LOCK_PREFIX);
3904 x86_alu_membase_imm (code, X86_ADD, X86_ESP, 0, 0);
3907 case OP_ATOMIC_ADD_I4: {
3908 int dreg = ins->dreg;
3910 if (dreg == ins->inst_basereg) {
3911 x86_push_reg (code, ins->sreg2);
3915 if (dreg != ins->sreg2)
3916 x86_mov_reg_reg (code, ins->dreg, ins->sreg2, 4);
3918 x86_prefix (code, X86_LOCK_PREFIX);
3919 x86_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, 4);
3921 if (dreg != ins->dreg) {
3922 x86_mov_reg_reg (code, ins->dreg, dreg, 4);
3923 x86_pop_reg (code, dreg);
3928 case OP_ATOMIC_ADD_NEW_I4: {
3929 int dreg = ins->dreg;
3931 /* hack: limit in regalloc, dreg != sreg1 && dreg != sreg2 */
3932 if (ins->sreg2 == dreg) {
3933 if (dreg == X86_EBX) {
3935 if (ins->inst_basereg == X86_EDI)
3939 if (ins->inst_basereg == X86_EBX)
3942 } else if (ins->inst_basereg == dreg) {
3943 if (dreg == X86_EBX) {
3945 if (ins->sreg2 == X86_EDI)
3949 if (ins->sreg2 == X86_EBX)
3954 if (dreg != ins->dreg) {
3955 x86_push_reg (code, dreg);
3958 x86_mov_reg_reg (code, dreg, ins->sreg2, 4);
3959 x86_prefix (code, X86_LOCK_PREFIX);
3960 x86_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, 4);
3961 /* dreg contains the old value, add with sreg2 value */
3962 x86_alu_reg_reg (code, X86_ADD, dreg, ins->sreg2);
3964 if (ins->dreg != dreg) {
3965 x86_mov_reg_reg (code, ins->dreg, dreg, 4);
3966 x86_pop_reg (code, dreg);
3971 case OP_ATOMIC_EXCHANGE_I4: {
3973 int sreg2 = ins->sreg2;
3974 int breg = ins->inst_basereg;
3976 /* cmpxchg uses eax as comperand, need to make sure we can use it
3977 * hack to overcome limits in x86 reg allocator
3978 * (req: dreg == eax and sreg2 != eax and breg != eax)
3980 g_assert (ins->dreg == X86_EAX);
3982 /* We need the EAX reg for the cmpxchg */
3983 if (ins->sreg2 == X86_EAX) {
3984 sreg2 = (breg == X86_EDX) ? X86_EBX : X86_EDX;
3985 x86_push_reg (code, sreg2);
3986 x86_mov_reg_reg (code, sreg2, X86_EAX, 4);
3989 if (breg == X86_EAX) {
3990 breg = (sreg2 == X86_ESI) ? X86_EDI : X86_ESI;
3991 x86_push_reg (code, breg);
3992 x86_mov_reg_reg (code, breg, X86_EAX, 4);
3995 x86_mov_reg_membase (code, X86_EAX, breg, ins->inst_offset, 4);
3997 br [0] = code; x86_prefix (code, X86_LOCK_PREFIX);
3998 x86_cmpxchg_membase_reg (code, breg, ins->inst_offset, sreg2);
3999 br [1] = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
4000 x86_patch (br [1], br [0]);
4002 if (breg != ins->inst_basereg)
4003 x86_pop_reg (code, breg);
4005 if (ins->sreg2 != sreg2)
4006 x86_pop_reg (code, sreg2);
4010 case OP_ATOMIC_CAS_I4: {
4011 g_assert (ins->dreg == X86_EAX);
4012 g_assert (ins->sreg3 == X86_EAX);
4013 g_assert (ins->sreg1 != X86_EAX);
4014 g_assert (ins->sreg1 != ins->sreg2);
4016 x86_prefix (code, X86_LOCK_PREFIX);
4017 x86_cmpxchg_membase_reg (code, ins->sreg1, ins->inst_offset, ins->sreg2);
4020 case OP_CARD_TABLE_WBARRIER: {
4021 int ptr = ins->sreg1;
4022 int value = ins->sreg2;
4024 int nursery_shift, card_table_shift;
4025 gpointer card_table_mask;
4026 size_t nursery_size;
4027 gulong card_table = (gulong)mono_gc_get_card_table (&card_table_shift, &card_table_mask);
4028 gulong nursery_start = (gulong)mono_gc_get_nursery (&nursery_shift, &nursery_size);
4031 * We need one register we can clobber, we choose EDX and make sreg1
4032 * fixed EAX to work around limitations in the local register allocator.
4033 * sreg2 might get allocated to EDX, but that is not a problem since
4034 * we use it before clobbering EDX.
4036 g_assert (ins->sreg1 == X86_EAX);
4039 * This is the code we produce:
4042 * edx >>= nursery_shift
4043 * cmp edx, (nursery_start >> nursery_shift)
4046 * edx >>= card_table_shift
4047 * card_table[edx] = 1
4051 if (value != X86_EDX)
4052 x86_mov_reg_reg (code, X86_EDX, value, 4);
4053 x86_shift_reg_imm (code, X86_SHR, X86_EDX, nursery_shift);
4054 x86_alu_reg_imm (code, X86_CMP, X86_EDX, nursery_start >> nursery_shift);
4055 br = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
4056 x86_mov_reg_reg (code, X86_EDX, ptr, 4);
4057 x86_shift_reg_imm (code, X86_SHR, X86_EDX, card_table_shift);
4058 if (card_table_mask)
4059 x86_alu_reg_imm (code, X86_AND, X86_EDX, (int)card_table_mask);
4060 x86_mov_membase_imm (code, X86_EDX, card_table, 1, 1);
4061 x86_patch (br, code);
4064 #ifdef MONO_ARCH_SIMD_INTRINSICS
4066 x86_sse_alu_ps_reg_reg (code, X86_SSE_ADD, ins->sreg1, ins->sreg2);
4069 x86_sse_alu_ps_reg_reg (code, X86_SSE_DIV, ins->sreg1, ins->sreg2);
4072 x86_sse_alu_ps_reg_reg (code, X86_SSE_MUL, ins->sreg1, ins->sreg2);
4075 x86_sse_alu_ps_reg_reg (code, X86_SSE_SUB, ins->sreg1, ins->sreg2);
4078 x86_sse_alu_ps_reg_reg (code, X86_SSE_MAX, ins->sreg1, ins->sreg2);
4081 x86_sse_alu_ps_reg_reg (code, X86_SSE_MIN, ins->sreg1, ins->sreg2);
4084 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
4085 x86_sse_alu_ps_reg_reg_imm (code, X86_SSE_COMP, ins->sreg1, ins->sreg2, ins->inst_c0);
4088 x86_sse_alu_ps_reg_reg (code, X86_SSE_AND, ins->sreg1, ins->sreg2);
4091 x86_sse_alu_ps_reg_reg (code, X86_SSE_ANDN, ins->sreg1, ins->sreg2);
4094 x86_sse_alu_ps_reg_reg (code, X86_SSE_OR, ins->sreg1, ins->sreg2);
4097 x86_sse_alu_ps_reg_reg (code, X86_SSE_XOR, ins->sreg1, ins->sreg2);
4100 x86_sse_alu_ps_reg_reg (code, X86_SSE_SQRT, ins->dreg, ins->sreg1);
4103 x86_sse_alu_ps_reg_reg (code, X86_SSE_RSQRT, ins->dreg, ins->sreg1);
4106 x86_sse_alu_ps_reg_reg (code, X86_SSE_RCP, ins->dreg, ins->sreg1);
4109 x86_sse_alu_sd_reg_reg (code, X86_SSE_ADDSUB, ins->sreg1, ins->sreg2);
4112 x86_sse_alu_sd_reg_reg (code, X86_SSE_HADD, ins->sreg1, ins->sreg2);
4115 x86_sse_alu_sd_reg_reg (code, X86_SSE_HSUB, ins->sreg1, ins->sreg2);
4118 x86_sse_alu_ss_reg_reg (code, X86_SSE_MOVSHDUP, ins->dreg, ins->sreg1);
4121 x86_sse_alu_ss_reg_reg (code, X86_SSE_MOVSLDUP, ins->dreg, ins->sreg1);
4124 case OP_PSHUFLEW_HIGH:
4125 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
4126 x86_pshufw_reg_reg (code, ins->dreg, ins->sreg1, ins->inst_c0, 1);
4128 case OP_PSHUFLEW_LOW:
4129 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
4130 x86_pshufw_reg_reg (code, ins->dreg, ins->sreg1, ins->inst_c0, 0);
4133 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
4134 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->sreg1, ins->inst_c0);
4138 x86_sse_alu_pd_reg_reg (code, X86_SSE_ADD, ins->sreg1, ins->sreg2);
4141 x86_sse_alu_pd_reg_reg (code, X86_SSE_DIV, ins->sreg1, ins->sreg2);
4144 x86_sse_alu_pd_reg_reg (code, X86_SSE_MUL, ins->sreg1, ins->sreg2);
4147 x86_sse_alu_pd_reg_reg (code, X86_SSE_SUB, ins->sreg1, ins->sreg2);
4150 x86_sse_alu_pd_reg_reg (code, X86_SSE_MAX, ins->sreg1, ins->sreg2);
4153 x86_sse_alu_pd_reg_reg (code, X86_SSE_MIN, ins->sreg1, ins->sreg2);
4156 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
4157 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_COMP, ins->sreg1, ins->sreg2, ins->inst_c0);
4160 x86_sse_alu_pd_reg_reg (code, X86_SSE_AND, ins->sreg1, ins->sreg2);
4163 x86_sse_alu_pd_reg_reg (code, X86_SSE_ANDN, ins->sreg1, ins->sreg2);
4166 x86_sse_alu_pd_reg_reg (code, X86_SSE_OR, ins->sreg1, ins->sreg2);
4169 x86_sse_alu_pd_reg_reg (code, X86_SSE_XOR, ins->sreg1, ins->sreg2);
4172 x86_sse_alu_pd_reg_reg (code, X86_SSE_SQRT, ins->dreg, ins->sreg1);
4175 x86_sse_alu_pd_reg_reg (code, X86_SSE_ADDSUB, ins->sreg1, ins->sreg2);
4178 x86_sse_alu_pd_reg_reg (code, X86_SSE_HADD, ins->sreg1, ins->sreg2);
4181 x86_sse_alu_pd_reg_reg (code, X86_SSE_HSUB, ins->sreg1, ins->sreg2);
4184 x86_sse_alu_sd_reg_reg (code, X86_SSE_MOVDDUP, ins->dreg, ins->sreg1);
4187 case OP_EXTRACT_MASK:
4188 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMOVMSKB, ins->dreg, ins->sreg1);
4192 x86_sse_alu_pd_reg_reg (code, X86_SSE_PAND, ins->sreg1, ins->sreg2);
4195 x86_sse_alu_pd_reg_reg (code, X86_SSE_POR, ins->sreg1, ins->sreg2);
4198 x86_sse_alu_pd_reg_reg (code, X86_SSE_PXOR, ins->sreg1, ins->sreg2);
4202 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDB, ins->sreg1, ins->sreg2);
4205 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDW, ins->sreg1, ins->sreg2);
4208 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDD, ins->sreg1, ins->sreg2);
4211 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDQ, ins->sreg1, ins->sreg2);
4215 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBB, ins->sreg1, ins->sreg2);
4218 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBW, ins->sreg1, ins->sreg2);
4221 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBD, ins->sreg1, ins->sreg2);
4224 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBQ, ins->sreg1, ins->sreg2);
4228 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMAXUB, ins->sreg1, ins->sreg2);
4231 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXUW, ins->sreg1, ins->sreg2);
4234 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXUD, ins->sreg1, ins->sreg2);
4238 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXSB, ins->sreg1, ins->sreg2);
4241 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMAXSW, ins->sreg1, ins->sreg2);
4244 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXSD, ins->sreg1, ins->sreg2);
4248 x86_sse_alu_pd_reg_reg (code, X86_SSE_PAVGB, ins->sreg1, ins->sreg2);
4251 x86_sse_alu_pd_reg_reg (code, X86_SSE_PAVGW, ins->sreg1, ins->sreg2);
4255 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMINUB, ins->sreg1, ins->sreg2);
4258 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINUW, ins->sreg1, ins->sreg2);
4261 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINUD, ins->sreg1, ins->sreg2);
4265 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINSB, ins->sreg1, ins->sreg2);
4268 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMINSW, ins->sreg1, ins->sreg2);
4271 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINSD, ins->sreg1, ins->sreg2);
4275 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQB, ins->sreg1, ins->sreg2);
4278 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQW, ins->sreg1, ins->sreg2);
4281 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQD, ins->sreg1, ins->sreg2);
4284 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PCMPEQQ, ins->sreg1, ins->sreg2);
4288 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPGTB, ins->sreg1, ins->sreg2);
4291 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPGTW, ins->sreg1, ins->sreg2);
4294 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPGTD, ins->sreg1, ins->sreg2);
4297 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PCMPGTQ, ins->sreg1, ins->sreg2);
4300 case OP_PSUM_ABS_DIFF:
4301 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSADBW, ins->sreg1, ins->sreg2);
4304 case OP_UNPACK_LOWB:
4305 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLBW, ins->sreg1, ins->sreg2);
4307 case OP_UNPACK_LOWW:
4308 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLWD, ins->sreg1, ins->sreg2);
4310 case OP_UNPACK_LOWD:
4311 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLDQ, ins->sreg1, ins->sreg2);
4313 case OP_UNPACK_LOWQ:
4314 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLQDQ, ins->sreg1, ins->sreg2);
4316 case OP_UNPACK_LOWPS:
4317 x86_sse_alu_ps_reg_reg (code, X86_SSE_UNPCKL, ins->sreg1, ins->sreg2);
4319 case OP_UNPACK_LOWPD:
4320 x86_sse_alu_pd_reg_reg (code, X86_SSE_UNPCKL, ins->sreg1, ins->sreg2);
4323 case OP_UNPACK_HIGHB:
4324 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHBW, ins->sreg1, ins->sreg2);
4326 case OP_UNPACK_HIGHW:
4327 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHWD, ins->sreg1, ins->sreg2);
4329 case OP_UNPACK_HIGHD:
4330 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHDQ, ins->sreg1, ins->sreg2);
4332 case OP_UNPACK_HIGHQ:
4333 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHQDQ, ins->sreg1, ins->sreg2);
4335 case OP_UNPACK_HIGHPS:
4336 x86_sse_alu_ps_reg_reg (code, X86_SSE_UNPCKH, ins->sreg1, ins->sreg2);
4338 case OP_UNPACK_HIGHPD:
4339 x86_sse_alu_pd_reg_reg (code, X86_SSE_UNPCKH, ins->sreg1, ins->sreg2);
4343 x86_sse_alu_pd_reg_reg (code, X86_SSE_PACKSSWB, ins->sreg1, ins->sreg2);
4346 x86_sse_alu_pd_reg_reg (code, X86_SSE_PACKSSDW, ins->sreg1, ins->sreg2);
4349 x86_sse_alu_pd_reg_reg (code, X86_SSE_PACKUSWB, ins->sreg1, ins->sreg2);
4352 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PACKUSDW, ins->sreg1, ins->sreg2);
4355 case OP_PADDB_SAT_UN:
4356 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDUSB, ins->sreg1, ins->sreg2);
4358 case OP_PSUBB_SAT_UN:
4359 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBUSB, ins->sreg1, ins->sreg2);
4361 case OP_PADDW_SAT_UN:
4362 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDUSW, ins->sreg1, ins->sreg2);
4364 case OP_PSUBW_SAT_UN:
4365 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBUSW, ins->sreg1, ins->sreg2);
4369 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDSB, ins->sreg1, ins->sreg2);
4372 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBSB, ins->sreg1, ins->sreg2);
4375 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDSW, ins->sreg1, ins->sreg2);
4378 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBSW, ins->sreg1, ins->sreg2);
4382 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULLW, ins->sreg1, ins->sreg2);
4385 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMULLD, ins->sreg1, ins->sreg2);
4388 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULUDQ, ins->sreg1, ins->sreg2);
4390 case OP_PMULW_HIGH_UN:
4391 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULHUW, ins->sreg1, ins->sreg2);
4394 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULHW, ins->sreg1, ins->sreg2);
4398 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTW, X86_SSE_SHR, ins->dreg, ins->inst_imm);
4401 x86_sse_shift_reg_reg (code, X86_SSE_PSRLW_REG, ins->dreg, ins->sreg2);
4405 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTW, X86_SSE_SAR, ins->dreg, ins->inst_imm);
4408 x86_sse_shift_reg_reg (code, X86_SSE_PSRAW_REG, ins->dreg, ins->sreg2);
4412 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTW, X86_SSE_SHL, ins->dreg, ins->inst_imm);
4415 x86_sse_shift_reg_reg (code, X86_SSE_PSLLW_REG, ins->dreg, ins->sreg2);
4419 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTD, X86_SSE_SHR, ins->dreg, ins->inst_imm);
4422 x86_sse_shift_reg_reg (code, X86_SSE_PSRLD_REG, ins->dreg, ins->sreg2);
4426 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTD, X86_SSE_SAR, ins->dreg, ins->inst_imm);
4429 x86_sse_shift_reg_reg (code, X86_SSE_PSRAD_REG, ins->dreg, ins->sreg2);
4433 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTD, X86_SSE_SHL, ins->dreg, ins->inst_imm);
4436 x86_sse_shift_reg_reg (code, X86_SSE_PSLLD_REG, ins->dreg, ins->sreg2);
4440 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTQ, X86_SSE_SHR, ins->dreg, ins->inst_imm);
4443 x86_sse_shift_reg_reg (code, X86_SSE_PSRLQ_REG, ins->dreg, ins->sreg2);
4447 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTQ, X86_SSE_SHL, ins->dreg, ins->inst_imm);
4450 x86_sse_shift_reg_reg (code, X86_SSE_PSLLQ_REG, ins->dreg, ins->sreg2);
4454 x86_movd_xreg_reg (code, ins->dreg, ins->sreg1);
4457 x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
4461 x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
4463 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_c0 * 8);
4464 x86_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I1, FALSE);
4468 x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
4470 x86_shift_reg_imm (code, X86_SHR, ins->dreg, 16);
4471 x86_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I2, TRUE);
4475 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);
4477 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);
4478 x86_fld_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE);
4482 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->sreg1, ins->sreg2, ins->inst_c0);
4484 case OP_EXTRACTX_U2:
4485 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PEXTRW, ins->dreg, ins->sreg1, ins->inst_c0);
4487 case OP_INSERTX_U1_SLOW:
4488 /*sreg1 is the extracted ireg (scratch)
4489 /sreg2 is the to be inserted ireg (scratch)
4490 /dreg is the xreg to receive the value*/
4492 /*clear the bits from the extracted word*/
4493 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_c0 & 1 ? 0x00FF : 0xFF00);
4494 /*shift the value to insert if needed*/
4495 if (ins->inst_c0 & 1)
4496 x86_shift_reg_imm (code, X86_SHL, ins->sreg2, 8);
4497 /*join them together*/
4498 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
4499 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, ins->inst_c0 / 2);
4501 case OP_INSERTX_I4_SLOW:
4502 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg2, ins->inst_c0 * 2);
4503 x86_shift_reg_imm (code, X86_SHR, ins->sreg2, 16);
4504 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg2, ins->inst_c0 * 2 + 1);
4507 case OP_INSERTX_R4_SLOW:
4508 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE, TRUE);
4509 /*TODO if inst_c0 == 0 use movss*/
4510 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);
4511 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);
4513 case OP_INSERTX_R8_SLOW:
4514 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
4516 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);
4518 x86_sse_alu_pd_reg_membase (code, X86_SSE_MOVSD_REG_MEMBASE, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
4521 case OP_STOREX_MEMBASE_REG:
4522 case OP_STOREX_MEMBASE:
4523 x86_movups_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
4525 case OP_LOADX_MEMBASE:
4526 x86_movups_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
4528 case OP_LOADX_ALIGNED_MEMBASE:
4529 x86_movaps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
4531 case OP_STOREX_ALIGNED_MEMBASE_REG:
4532 x86_movaps_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
4534 case OP_STOREX_NTA_MEMBASE_REG:
4535 x86_sse_alu_reg_membase (code, X86_SSE_MOVNTPS, ins->dreg, ins->sreg1, ins->inst_offset);
4537 case OP_PREFETCH_MEMBASE:
4538 x86_sse_alu_reg_membase (code, X86_SSE_PREFETCH, ins->backend.arg_info, ins->sreg1, ins->inst_offset);
4542 /*FIXME the peephole pass should have killed this*/
4543 if (ins->dreg != ins->sreg1)
4544 x86_movaps_reg_reg (code, ins->dreg, ins->sreg1);
4547 x86_sse_alu_pd_reg_reg (code, X86_SSE_PXOR, ins->dreg, ins->dreg);
4549 case OP_ICONV_TO_R8_RAW:
4550 x86_mov_membase_reg (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, ins->sreg1, 4);
4551 x86_fld_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE);
4554 case OP_FCONV_TO_R8_X:
4555 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
4556 x86_movsd_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
4559 case OP_XCONV_R8_TO_I4:
4560 x86_cvttsd2si (code, ins->dreg, ins->sreg1);
4561 switch (ins->backend.source_opcode) {
4562 case OP_FCONV_TO_I1:
4563 x86_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
4565 case OP_FCONV_TO_U1:
4566 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
4568 case OP_FCONV_TO_I2:
4569 x86_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
4571 case OP_FCONV_TO_U2:
4572 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
4578 /*FIXME this causes a partial register stall, maybe it would not be that bad to use shift + mask + or*/
4579 /*The +4 is to get a mov ?h, ?l over the same reg.*/
4580 x86_mov_reg_reg (code, ins->dreg + 4, ins->dreg, 1);
4581 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 0);
4582 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 1);
4583 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
4586 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 0);
4587 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 1);
4588 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
4591 x86_movd_xreg_reg (code, ins->dreg, ins->sreg1);
4592 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
4595 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE, TRUE);
4596 x86_movd_xreg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
4597 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
4600 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
4601 x86_movsd_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
4602 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0x44);
4605 case OP_LIVERANGE_START: {
4606 if (cfg->verbose_level > 1)
4607 printf ("R%d START=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
4608 MONO_VARINFO (cfg, ins->inst_c0)->live_range_start = code - cfg->native_code;
4611 case OP_LIVERANGE_END: {
4612 if (cfg->verbose_level > 1)
4613 printf ("R%d END=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
4614 MONO_VARINFO (cfg, ins->inst_c0)->live_range_end = code - cfg->native_code;
4618 g_warning ("unknown opcode %s\n", mono_inst_name (ins->opcode));
4619 g_assert_not_reached ();
4622 if (G_UNLIKELY ((code - cfg->native_code - offset) > max_len)) {
4623 #ifndef __native_client_codegen__
4624 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
4625 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
4626 g_assert_not_reached ();
4627 #endif /* __native_client_codegen__ */
4633 cfg->code_len = code - cfg->native_code;
4636 #endif /* DISABLE_JIT */
4639 mono_arch_register_lowlevel_calls (void)
4644 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
4646 MonoJumpInfo *patch_info;
4647 gboolean compile_aot = !run_cctors;
4649 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
4650 unsigned char *ip = patch_info->ip.i + code;
4651 const unsigned char *target;
4653 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
4656 switch (patch_info->type) {
4657 case MONO_PATCH_INFO_BB:
4658 case MONO_PATCH_INFO_LABEL:
4661 /* No need to patch these */
4666 switch (patch_info->type) {
4667 case MONO_PATCH_INFO_IP:
4668 *((gconstpointer *)(ip)) = target;
4670 case MONO_PATCH_INFO_CLASS_INIT: {
4672 /* Might already been changed to a nop */
4673 x86_call_code (code, 0);
4674 x86_patch (ip, target);
4677 case MONO_PATCH_INFO_ABS:
4678 case MONO_PATCH_INFO_METHOD:
4679 case MONO_PATCH_INFO_METHOD_JUMP:
4680 case MONO_PATCH_INFO_INTERNAL_METHOD:
4681 case MONO_PATCH_INFO_BB:
4682 case MONO_PATCH_INFO_LABEL:
4683 case MONO_PATCH_INFO_RGCTX_FETCH:
4684 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
4685 case MONO_PATCH_INFO_MONITOR_ENTER:
4686 case MONO_PATCH_INFO_MONITOR_EXIT:
4687 x86_patch (ip, target);
4689 case MONO_PATCH_INFO_NONE:
4692 guint32 offset = mono_arch_get_patch_offset (ip);
4693 *((gconstpointer *)(ip + offset)) = target;
4701 mono_arch_emit_prolog (MonoCompile *cfg)
4703 MonoMethod *method = cfg->method;
4705 MonoMethodSignature *sig;
4707 int alloc_size, pos, max_offset, i, cfa_offset;
4709 gboolean need_stack_frame;
4710 #ifdef __native_client_codegen__
4711 guint alignment_check;
4714 cfg->code_size = MAX (cfg->header->code_size * 4, 10240);
4716 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
4717 cfg->code_size += 512;
4719 #ifdef __native_client_codegen__
4720 /* native_code_alloc is not 32-byte aligned, native_code is. */
4721 cfg->native_code_alloc = g_malloc (cfg->code_size + kNaClAlignment);
4723 /* Align native_code to next nearest kNaclAlignment byte. */
4724 cfg->native_code = (guint)cfg->native_code_alloc + kNaClAlignment;
4725 cfg->native_code = (guint)cfg->native_code & ~kNaClAlignmentMask;
4727 code = cfg->native_code;
4729 alignment_check = (guint)cfg->native_code & kNaClAlignmentMask;
4730 g_assert(alignment_check == 0);
4732 code = cfg->native_code = g_malloc (cfg->code_size);
4735 /* Offset between RSP and the CFA */
4739 cfa_offset = sizeof (gpointer);
4740 mono_emit_unwind_op_def_cfa (cfg, code, X86_ESP, sizeof (gpointer));
4741 // IP saved at CFA - 4
4742 /* There is no IP reg on x86 */
4743 mono_emit_unwind_op_offset (cfg, code, X86_NREG, -cfa_offset);
4745 need_stack_frame = needs_stack_frame (cfg);
4747 if (need_stack_frame) {
4748 x86_push_reg (code, X86_EBP);
4749 cfa_offset += sizeof (gpointer);
4750 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
4751 mono_emit_unwind_op_offset (cfg, code, X86_EBP, - cfa_offset);
4752 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
4753 mono_emit_unwind_op_def_cfa_reg (cfg, code, X86_EBP);
4756 alloc_size = cfg->stack_offset;
4759 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
4760 /* Might need to attach the thread to the JIT or change the domain for the callback */
4761 if (appdomain_tls_offset != -1 && lmf_tls_offset != -1) {
4762 guint8 *buf, *no_domain_branch;
4764 code = mono_x86_emit_tls_get (code, X86_EAX, appdomain_tls_offset);
4765 x86_alu_reg_imm (code, X86_CMP, X86_EAX, GPOINTER_TO_UINT (cfg->domain));
4766 no_domain_branch = code;
4767 x86_branch8 (code, X86_CC_NE, 0, 0);
4768 code = mono_x86_emit_tls_get ( code, X86_EAX, lmf_tls_offset);
4769 x86_test_reg_reg (code, X86_EAX, X86_EAX);
4771 x86_branch8 (code, X86_CC_NE, 0, 0);
4772 x86_patch (no_domain_branch, code);
4773 x86_push_imm (code, cfg->domain);
4774 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_jit_thread_attach");
4775 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
4776 x86_patch (buf, code);
4778 /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
4779 /* FIXME: Add a separate key for LMF to avoid this */
4780 x86_alu_reg_imm (code, X86_ADD, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
4784 if (cfg->compile_aot) {
4786 * This goes before the saving of callee saved regs, so save the got reg
4789 x86_push_reg (code, MONO_ARCH_GOT_REG);
4790 code = mono_arch_emit_load_got_addr (cfg->native_code, code, cfg, NULL);
4791 x86_push_imm (code, 0);
4793 x86_push_imm (code, cfg->domain);
4795 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_jit_thread_attach");
4796 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
4797 if (cfg->compile_aot)
4798 x86_pop_reg (code, MONO_ARCH_GOT_REG);
4802 if (method->save_lmf) {
4803 pos += sizeof (MonoLMF);
4805 /* save the current IP */
4806 if (cfg->compile_aot) {
4807 /* This pushes the current ip */
4808 x86_call_imm (code, 0);
4810 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
4811 x86_push_imm_template (code);
4813 cfa_offset += sizeof (gpointer);
4815 /* save all caller saved regs */
4816 x86_push_reg (code, X86_EBP);
4817 cfa_offset += sizeof (gpointer);
4818 x86_push_reg (code, X86_ESI);
4819 cfa_offset += sizeof (gpointer);
4820 mono_emit_unwind_op_offset (cfg, code, X86_ESI, - cfa_offset);
4821 x86_push_reg (code, X86_EDI);
4822 cfa_offset += sizeof (gpointer);
4823 mono_emit_unwind_op_offset (cfg, code, X86_EDI, - cfa_offset);
4824 x86_push_reg (code, X86_EBX);
4825 cfa_offset += sizeof (gpointer);
4826 mono_emit_unwind_op_offset (cfg, code, X86_EBX, - cfa_offset);
4828 if ((lmf_tls_offset != -1) && !is_win32 && !optimize_for_xen) {
4830 * Optimized version which uses the mono_lmf TLS variable instead of indirection
4831 * through the mono_lmf_addr TLS variable.
4833 /* %eax = previous_lmf */
4834 x86_prefix (code, X86_GS_PREFIX);
4835 x86_mov_reg_mem (code, X86_EAX, lmf_tls_offset, 4);
4836 /* skip esp + method_info + lmf */
4837 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 12);
4838 /* push previous_lmf */
4839 x86_push_reg (code, X86_EAX);
4841 x86_prefix (code, X86_GS_PREFIX);
4842 x86_mov_mem_reg (code, lmf_tls_offset, X86_ESP, 4);
4844 /* get the address of lmf for the current thread */
4846 * This is performance critical so we try to use some tricks to make
4850 if (lmf_addr_tls_offset != -1) {
4851 /* Load lmf quicky using the GS register */
4852 code = mono_x86_emit_tls_get (code, X86_EAX, lmf_addr_tls_offset);
4854 /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
4855 /* FIXME: Add a separate key for LMF to avoid this */
4856 x86_alu_reg_imm (code, X86_ADD, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
4859 if (cfg->compile_aot)
4860 code = mono_arch_emit_load_got_addr (cfg->native_code, code, cfg, NULL);
4861 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_get_lmf_addr");
4864 /* Skip esp + method info */
4865 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
4868 x86_push_reg (code, X86_EAX);
4869 /* push *lfm (previous_lmf) */
4870 x86_push_membase (code, X86_EAX, 0);
4872 x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
4876 if (cfg->used_int_regs & (1 << X86_EBX)) {
4877 x86_push_reg (code, X86_EBX);
4879 cfa_offset += sizeof (gpointer);
4880 mono_emit_unwind_op_offset (cfg, code, X86_EBX, - cfa_offset);
4883 if (cfg->used_int_regs & (1 << X86_EDI)) {
4884 x86_push_reg (code, X86_EDI);
4886 cfa_offset += sizeof (gpointer);
4887 mono_emit_unwind_op_offset (cfg, code, X86_EDI, - cfa_offset);
4890 if (cfg->used_int_regs & (1 << X86_ESI)) {
4891 x86_push_reg (code, X86_ESI);
4893 cfa_offset += sizeof (gpointer);
4894 mono_emit_unwind_op_offset (cfg, code, X86_ESI, - cfa_offset);
4900 /* the original alloc_size is already aligned: there is %ebp and retip pushed, so realign */
4901 if (mono_do_x86_stack_align && need_stack_frame) {
4902 int tot = alloc_size + pos + 4; /* ret ip */
4903 if (need_stack_frame)
4905 tot &= MONO_ARCH_FRAME_ALIGNMENT - 1;
4907 alloc_size += MONO_ARCH_FRAME_ALIGNMENT - tot;
4911 /* See mono_emit_stack_alloc */
4912 #if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
4913 guint32 remaining_size = alloc_size;
4914 /*FIXME handle unbounded code expansion, we should use a loop in case of more than X interactions*/
4915 guint32 required_code_size = ((remaining_size / 0x1000) + 1) * 8; /*8 is the max size of x86_alu_reg_imm + x86_test_membase_reg*/
4916 guint32 offset = code - cfg->native_code;
4917 if (G_UNLIKELY (required_code_size >= (cfg->code_size - offset))) {
4918 while (required_code_size >= (cfg->code_size - offset))
4919 cfg->code_size *= 2;
4920 cfg->native_code = mono_realloc_native_code(cfg);
4921 code = cfg->native_code + offset;
4922 mono_jit_stats.code_reallocs++;
4924 while (remaining_size >= 0x1000) {
4925 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
4926 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
4927 remaining_size -= 0x1000;
4930 x86_alu_reg_imm (code, X86_SUB, X86_ESP, remaining_size);
4932 x86_alu_reg_imm (code, X86_SUB, X86_ESP, alloc_size);
4935 g_assert (need_stack_frame);
4938 if (cfg->method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED ||
4939 cfg->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
4940 x86_alu_reg_imm (code, X86_AND, X86_ESP, -MONO_ARCH_FRAME_ALIGNMENT);
4943 #if DEBUG_STACK_ALIGNMENT
4944 /* check the stack is aligned */
4945 if (need_stack_frame && method->wrapper_type == MONO_WRAPPER_NONE) {
4946 x86_mov_reg_reg (code, X86_ECX, X86_ESP, 4);
4947 x86_alu_reg_imm (code, X86_AND, X86_ECX, MONO_ARCH_FRAME_ALIGNMENT - 1);
4948 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
4949 x86_branch_disp (code, X86_CC_EQ, 3, FALSE);
4950 x86_breakpoint (code);
4954 /* compute max_offset in order to use short forward jumps */
4956 if (cfg->opt & MONO_OPT_BRANCH) {
4957 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4959 bb->max_offset = max_offset;
4961 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
4963 /* max alignment for loops */
4964 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
4965 max_offset += LOOP_ALIGNMENT;
4966 #ifdef __native_client_codegen__
4967 /* max alignment for native client */
4968 max_offset += kNaClAlignment;
4970 MONO_BB_FOR_EACH_INS (bb, ins) {
4971 if (ins->opcode == OP_LABEL)
4972 ins->inst_c1 = max_offset;
4973 #ifdef __native_client_codegen__
4975 int space_in_block = kNaClAlignment -
4976 ((max_offset + cfg->code_len) & kNaClAlignmentMask);
4977 int max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
4978 if (space_in_block < max_len && max_len < kNaClAlignment) {
4979 max_offset += space_in_block;
4982 #endif /* __native_client_codegen__ */
4983 max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
4988 /* store runtime generic context */
4989 if (cfg->rgctx_var) {
4990 g_assert (cfg->rgctx_var->opcode == OP_REGOFFSET && cfg->rgctx_var->inst_basereg == X86_EBP);
4992 x86_mov_membase_reg (code, X86_EBP, cfg->rgctx_var->inst_offset, MONO_ARCH_RGCTX_REG, 4);
4995 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
4996 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
4998 /* load arguments allocated to register from the stack */
4999 sig = mono_method_signature (method);
5002 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
5003 inst = cfg->args [pos];
5004 if (inst->opcode == OP_REGVAR) {
5005 g_assert (need_stack_frame);
5006 x86_mov_reg_membase (code, inst->dreg, X86_EBP, inst->inst_offset, 4);
5007 if (cfg->verbose_level > 2)
5008 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
5013 cfg->code_len = code - cfg->native_code;
5015 g_assert (cfg->code_len < cfg->code_size);
5021 mono_arch_emit_epilog (MonoCompile *cfg)
5023 MonoMethod *method = cfg->method;
5024 MonoMethodSignature *sig = mono_method_signature (method);
5026 guint32 stack_to_pop;
5028 int max_epilog_size = 16;
5030 gboolean need_stack_frame = needs_stack_frame (cfg);
5032 if (cfg->method->save_lmf)
5033 max_epilog_size += 128;
5035 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
5036 cfg->code_size *= 2;
5037 cfg->native_code = mono_realloc_native_code(cfg);
5038 mono_jit_stats.code_reallocs++;
5041 code = cfg->native_code + cfg->code_len;
5043 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
5044 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
5046 /* the code restoring the registers must be kept in sync with OP_JMP */
5049 if (method->save_lmf) {
5050 gint32 prev_lmf_reg;
5051 gint32 lmf_offset = -sizeof (MonoLMF);
5053 /* check if we need to restore protection of the stack after a stack overflow */
5054 if (mono_get_jit_tls_offset () != -1) {
5056 code = mono_x86_emit_tls_get (code, X86_ECX, mono_get_jit_tls_offset ());
5057 /* we load the value in a separate instruction: this mechanism may be
5058 * used later as a safer way to do thread interruption
5060 x86_mov_reg_membase (code, X86_ECX, X86_ECX, G_STRUCT_OFFSET (MonoJitTlsData, restore_stack_prot), 4);
5061 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
5063 x86_branch8 (code, X86_CC_Z, 0, FALSE);
5064 /* note that the call trampoline will preserve eax/edx */
5065 x86_call_reg (code, X86_ECX);
5066 x86_patch (patch, code);
5068 /* FIXME: maybe save the jit tls in the prolog */
5070 if ((lmf_tls_offset != -1) && !is_win32 && !optimize_for_xen) {
5072 * Optimized version which uses the mono_lmf TLS variable instead of indirection
5073 * through the mono_lmf_addr TLS variable.
5075 /* reg = previous_lmf */
5076 x86_mov_reg_membase (code, X86_ECX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 4);
5078 /* lmf = previous_lmf */
5079 x86_prefix (code, X86_GS_PREFIX);
5080 x86_mov_mem_reg (code, lmf_tls_offset, X86_ECX, 4);
5082 /* Find a spare register */
5083 switch (mini_type_get_underlying_type (cfg->generic_sharing_context, sig->ret)->type) {
5086 prev_lmf_reg = X86_EDI;
5087 cfg->used_int_regs |= (1 << X86_EDI);
5090 prev_lmf_reg = X86_EDX;
5094 /* reg = previous_lmf */
5095 x86_mov_reg_membase (code, prev_lmf_reg, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 4);
5098 x86_mov_reg_membase (code, X86_ECX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), 4);
5100 /* *(lmf) = previous_lmf */
5101 x86_mov_membase_reg (code, X86_ECX, 0, prev_lmf_reg, 4);
5104 /* restore caller saved regs */
5105 if (cfg->used_int_regs & (1 << X86_EBX)) {
5106 x86_mov_reg_membase (code, X86_EBX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebx), 4);
5109 if (cfg->used_int_regs & (1 << X86_EDI)) {
5110 x86_mov_reg_membase (code, X86_EDI, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, edi), 4);
5112 if (cfg->used_int_regs & (1 << X86_ESI)) {
5113 x86_mov_reg_membase (code, X86_ESI, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esi), 4);
5116 /* EBP is restored by LEAVE */
5118 if (cfg->used_int_regs & (1 << X86_EBX)) {
5121 if (cfg->used_int_regs & (1 << X86_EDI)) {
5124 if (cfg->used_int_regs & (1 << X86_ESI)) {
5129 g_assert (need_stack_frame);
5130 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
5133 if (cfg->used_int_regs & (1 << X86_ESI)) {
5134 x86_pop_reg (code, X86_ESI);
5136 if (cfg->used_int_regs & (1 << X86_EDI)) {
5137 x86_pop_reg (code, X86_EDI);
5139 if (cfg->used_int_regs & (1 << X86_EBX)) {
5140 x86_pop_reg (code, X86_EBX);
5144 /* Load returned vtypes into registers if needed */
5145 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, FALSE);
5146 if (cinfo->ret.storage == ArgValuetypeInReg) {
5147 for (quad = 0; quad < 2; quad ++) {
5148 switch (cinfo->ret.pair_storage [quad]) {
5150 x86_mov_reg_membase (code, cinfo->ret.pair_regs [quad], cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), 4);
5152 case ArgOnFloatFpStack:
5153 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), FALSE);
5155 case ArgOnDoubleFpStack:
5156 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), TRUE);
5161 g_assert_not_reached ();
5166 if (need_stack_frame)
5169 if (CALLCONV_IS_STDCALL (sig)) {
5170 MonoJitArgumentInfo *arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
5172 stack_to_pop = mono_arch_get_argument_info (sig, sig->param_count, arg_info);
5173 } else if (MONO_TYPE_ISSTRUCT (mono_method_signature (cfg->method)->ret) && (cinfo->ret.storage == ArgOnStack))
5179 g_assert (need_stack_frame);
5180 x86_ret_imm (code, stack_to_pop);
5185 cfg->code_len = code - cfg->native_code;
5187 g_assert (cfg->code_len < cfg->code_size);
5191 mono_arch_emit_exceptions (MonoCompile *cfg)
5193 MonoJumpInfo *patch_info;
5196 MonoClass *exc_classes [16];
5197 guint8 *exc_throw_start [16], *exc_throw_end [16];
5201 /* Compute needed space */
5202 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5203 if (patch_info->type == MONO_PATCH_INFO_EXC)
5208 * make sure we have enough space for exceptions
5209 * 16 is the size of two push_imm instructions and a call
5211 if (cfg->compile_aot)
5212 code_size = exc_count * 32;
5214 code_size = exc_count * 16;
5216 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
5217 cfg->code_size *= 2;
5218 cfg->native_code = mono_realloc_native_code(cfg);
5219 mono_jit_stats.code_reallocs++;
5222 code = cfg->native_code + cfg->code_len;
5225 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5226 switch (patch_info->type) {
5227 case MONO_PATCH_INFO_EXC: {
5228 MonoClass *exc_class;
5232 x86_patch (patch_info->ip.i + cfg->native_code, code);
5234 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
5235 g_assert (exc_class);
5236 throw_ip = patch_info->ip.i;
5238 /* Find a throw sequence for the same exception class */
5239 for (i = 0; i < nthrows; ++i)
5240 if (exc_classes [i] == exc_class)
5243 x86_push_imm (code, (exc_throw_end [i] - cfg->native_code) - throw_ip);
5244 x86_jump_code (code, exc_throw_start [i]);
5245 patch_info->type = MONO_PATCH_INFO_NONE;
5250 /* Compute size of code following the push <OFFSET> */
5251 #ifdef __native_client_codegen__
5252 code = mono_nacl_align (code);
5253 size = kNaClAlignment;
5257 /*This is aligned to 16 bytes by the callee. This way we save a few bytes here.*/
5259 if ((code - cfg->native_code) - throw_ip < 126 - size) {
5260 /* Use the shorter form */
5262 x86_push_imm (code, 0);
5266 x86_push_imm (code, 0xf0f0f0f0);
5271 exc_classes [nthrows] = exc_class;
5272 exc_throw_start [nthrows] = code;
5275 x86_push_imm (code, exc_class->type_token - MONO_TOKEN_TYPE_DEF);
5276 patch_info->data.name = "mono_arch_throw_corlib_exception";
5277 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
5278 patch_info->ip.i = code - cfg->native_code;
5279 x86_call_code (code, 0);
5280 x86_push_imm (buf, (code - cfg->native_code) - throw_ip);
5285 exc_throw_end [nthrows] = code;
5297 cfg->code_len = code - cfg->native_code;
5299 g_assert (cfg->code_len < cfg->code_size);
5303 mono_arch_flush_icache (guint8 *code, gint size)
5309 mono_arch_flush_register_windows (void)
5314 mono_arch_is_inst_imm (gint64 imm)
5320 * Support for fast access to the thread-local lmf structure using the GS
5321 * segment register on NPTL + kernel 2.6.x.
5324 static gboolean tls_offset_inited = FALSE;
5327 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
5329 if (!tls_offset_inited) {
5330 if (!getenv ("MONO_NO_TLS")) {
5333 * We need to init this multiple times, since when we are first called, the key might not
5334 * be initialized yet.
5336 appdomain_tls_offset = mono_domain_get_tls_key ();
5337 lmf_tls_offset = mono_get_jit_tls_key ();
5339 /* Only 64 tls entries can be accessed using inline code */
5340 if (appdomain_tls_offset >= 64)
5341 appdomain_tls_offset = -1;
5342 if (lmf_tls_offset >= 64)
5343 lmf_tls_offset = -1;
5346 optimize_for_xen = access ("/proc/xen", F_OK) == 0;
5348 tls_offset_inited = TRUE;
5349 appdomain_tls_offset = mono_domain_get_tls_offset ();
5350 lmf_tls_offset = mono_get_lmf_tls_offset ();
5351 lmf_addr_tls_offset = mono_get_lmf_addr_tls_offset ();
5358 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
5362 #ifdef MONO_ARCH_HAVE_IMT
5364 // Linear handler, the bsearch head compare is shorter
5365 //[2 + 4] x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
5366 //[1 + 1] x86_branch8(inst,cond,imm,is_signed)
5367 // x86_patch(ins,target)
5368 //[1 + 5] x86_jump_mem(inst,mem)
5371 #ifdef __native_client_codegen__
5372 /* These constants should be coming from cpu-x86.md */
5373 /* I suspect the size calculation below is actually incorrect. */
5374 /* TODO: fix the calculation that uses these sizes. */
5375 #define BR_SMALL_SIZE 16
5376 #define BR_LARGE_SIZE 12
5378 #define BR_SMALL_SIZE 2
5379 #define BR_LARGE_SIZE 5
5380 #endif /* __native_client_codegen__ */
5381 #define JUMP_IMM_SIZE 6
5382 #define ENABLE_WRONG_METHOD_CHECK 0
5386 imt_branch_distance (MonoIMTCheckItem **imt_entries, int start, int target)
5388 int i, distance = 0;
5389 for (i = start; i < target; ++i)
5390 distance += imt_entries [i]->chunk_size;
5395 * LOCKING: called with the domain lock held
5398 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
5399 gpointer fail_tramp)
5403 guint8 *code, *start;
5405 #ifdef __native_client_codegen__
5406 /* g_print("mono_arch_build_imt_thunk needs to be aligned.\n"); */
5408 for (i = 0; i < count; ++i) {
5409 MonoIMTCheckItem *item = imt_entries [i];
5410 if (item->is_equals) {
5411 if (item->check_target_idx) {
5412 if (!item->compare_done)
5413 item->chunk_size += CMP_SIZE;
5414 item->chunk_size += BR_SMALL_SIZE + JUMP_IMM_SIZE;
5417 item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + JUMP_IMM_SIZE * 2;
5419 item->chunk_size += JUMP_IMM_SIZE;
5420 #if ENABLE_WRONG_METHOD_CHECK
5421 item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
5426 item->chunk_size += CMP_SIZE + BR_LARGE_SIZE;
5427 imt_entries [item->check_target_idx]->compare_done = TRUE;
5429 size += item->chunk_size;
5432 code = mono_method_alloc_generic_virtual_thunk (domain, size);
5434 code = mono_domain_code_reserve (domain, size);
5436 for (i = 0; i < count; ++i) {
5437 MonoIMTCheckItem *item = imt_entries [i];
5438 item->code_target = code;
5439 if (item->is_equals) {
5440 if (item->check_target_idx) {
5441 if (!item->compare_done)
5442 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5443 item->jmp_code = code;
5444 x86_branch8 (code, X86_CC_NE, 0, FALSE);
5445 if (item->has_target_code)
5446 x86_jump_code (code, item->value.target_code);
5448 x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
5451 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5452 item->jmp_code = code;
5453 x86_branch8 (code, X86_CC_NE, 0, FALSE);
5454 if (item->has_target_code)
5455 x86_jump_code (code, item->value.target_code);
5457 x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
5458 x86_patch (item->jmp_code, code);
5459 x86_jump_code (code, fail_tramp);
5460 item->jmp_code = NULL;
5462 /* enable the commented code to assert on wrong method */
5463 #if ENABLE_WRONG_METHOD_CHECK
5464 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5465 item->jmp_code = code;
5466 x86_branch8 (code, X86_CC_NE, 0, FALSE);
5468 if (item->has_target_code)
5469 x86_jump_code (code, item->value.target_code);
5471 x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
5472 #if ENABLE_WRONG_METHOD_CHECK
5473 x86_patch (item->jmp_code, code);
5474 x86_breakpoint (code);
5475 item->jmp_code = NULL;
5480 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5481 item->jmp_code = code;
5482 if (x86_is_imm8 (imt_branch_distance (imt_entries, i, item->check_target_idx)))
5483 x86_branch8 (code, X86_CC_GE, 0, FALSE);
5485 x86_branch32 (code, X86_CC_GE, 0, FALSE);
5488 /* patch the branches to get to the target items */
5489 for (i = 0; i < count; ++i) {
5490 MonoIMTCheckItem *item = imt_entries [i];
5491 if (item->jmp_code) {
5492 if (item->check_target_idx) {
5493 x86_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
5499 mono_stats.imt_thunks_size += code - start;
5500 g_assert (code - start <= size);
5504 char *buff = g_strdup_printf ("thunk_for_class_%s_%s_entries_%d", vtable->klass->name_space, vtable->klass->name, count);
5505 mono_disassemble_code (NULL, (guint8*)start, code - start, buff);
5509 if (mono_jit_map_is_enabled ()) {
5512 buff = g_strdup_printf ("imt_%s_%s_entries_%d", vtable->klass->name_space, vtable->klass->name, count);
5514 buff = g_strdup_printf ("imt_thunk_entries_%d", count);
5515 mono_emit_jit_tramp (start, code - start, buff);
5523 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
5525 return (MonoMethod*) regs [MONO_ARCH_IMT_REG];
5530 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
5532 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
5536 mono_arch_get_cie_program (void)
5540 mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
5541 mono_add_unwind_op_offset (l, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
5547 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5549 MonoInst *ins = NULL;
5552 if (cmethod->klass == mono_defaults.math_class) {
5553 if (strcmp (cmethod->name, "Sin") == 0) {
5555 } else if (strcmp (cmethod->name, "Cos") == 0) {
5557 } else if (strcmp (cmethod->name, "Tan") == 0) {
5559 } else if (strcmp (cmethod->name, "Atan") == 0) {
5561 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
5563 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
5565 } else if (strcmp (cmethod->name, "Round") == 0 && fsig->param_count == 1 && fsig->params [0]->type == MONO_TYPE_R8) {
5570 MONO_INST_NEW (cfg, ins, opcode);
5571 ins->type = STACK_R8;
5572 ins->dreg = mono_alloc_freg (cfg);
5573 ins->sreg1 = args [0]->dreg;
5574 MONO_ADD_INS (cfg->cbb, ins);
5577 if (cfg->opt & MONO_OPT_CMOV) {
5580 if (strcmp (cmethod->name, "Min") == 0) {
5581 if (fsig->params [0]->type == MONO_TYPE_I4)
5583 } else if (strcmp (cmethod->name, "Max") == 0) {
5584 if (fsig->params [0]->type == MONO_TYPE_I4)
5589 MONO_INST_NEW (cfg, ins, opcode);
5590 ins->type = STACK_I4;
5591 ins->dreg = mono_alloc_ireg (cfg);
5592 ins->sreg1 = args [0]->dreg;
5593 ins->sreg2 = args [1]->dreg;
5594 MONO_ADD_INS (cfg->cbb, ins);
5599 /* OP_FREM is not IEEE compatible */
5600 else if (strcmp (cmethod->name, "IEEERemainder") == 0) {
5601 MONO_INST_NEW (cfg, ins, OP_FREM);
5602 ins->inst_i0 = args [0];
5603 ins->inst_i1 = args [1];
5612 mono_arch_print_tree (MonoInst *tree, int arity)
5617 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
5623 if (appdomain_tls_offset == -1)
5626 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
5627 ins->inst_offset = appdomain_tls_offset;
5632 mono_arch_get_patch_offset (guint8 *code)
5634 if ((code [0] == 0x8b) && (x86_modrm_mod (code [1]) == 0x2))
5636 else if ((code [0] == 0xba))
5638 else if ((code [0] == 0x68))
5641 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x6))
5642 /* push <OFFSET>(<REG>) */
5644 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x2))
5645 /* call *<OFFSET>(<REG>) */
5647 else if ((code [0] == 0xdd) || (code [0] == 0xd9))
5650 else if ((code [0] == 0x58) && (code [1] == 0x05))
5651 /* pop %eax; add <OFFSET>, %eax */
5653 else if ((code [0] >= 0x58) && (code [0] <= 0x58 + X86_NREG) && (code [1] == 0x81))
5654 /* pop <REG>; add <OFFSET>, <REG> */
5656 else if ((code [0] >= 0xb8) && (code [0] < 0xb8 + 8))
5657 /* mov <REG>, imm */
5660 g_assert_not_reached ();
5666 * mono_breakpoint_clean_code:
5668 * Copy @size bytes from @code - @offset to the buffer @buf. If the debugger inserted software
5669 * breakpoints in the original code, they are removed in the copy.
5671 * Returns TRUE if no sw breakpoint was present.
5674 mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size)
5677 gboolean can_write = TRUE;
5679 * If method_start is non-NULL we need to perform bound checks, since we access memory
5680 * at code - offset we could go before the start of the method and end up in a different
5681 * page of memory that is not mapped or read incorrect data anyway. We zero-fill the bytes
5684 if (!method_start || code - offset >= method_start) {
5685 memcpy (buf, code - offset, size);
5687 int diff = code - method_start;
5688 memset (buf, 0, size);
5689 memcpy (buf + offset - diff, method_start, diff + size - offset);
5692 for (i = 0; i < MONO_BREAKPOINT_ARRAY_SIZE; ++i) {
5693 int idx = mono_breakpoint_info_index [i];
5697 ptr = mono_breakpoint_info [idx].address;
5698 if (ptr >= code && ptr < code + size) {
5699 guint8 saved_byte = mono_breakpoint_info [idx].saved_byte;
5701 /*g_print ("patching %p with 0x%02x (was: 0x%02x)\n", ptr, saved_byte, buf [ptr - code]);*/
5702 buf [ptr - code] = saved_byte;
5709 * mono_x86_get_this_arg_offset:
5711 * Return the offset of the stack location where this is passed during a virtual
5715 mono_x86_get_this_arg_offset (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig)
5721 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
5723 guint32 esp = regs [X86_ESP];
5724 CallInfo *cinfo = NULL;
5731 * The stack looks like:
5735 * <4 pointers pushed by mono_arch_create_trampoline_code ()>
5737 res = (((MonoObject**)esp) [5 + (offset / 4)]);
5743 #define MAX_ARCH_DELEGATE_PARAMS 10
5746 get_delegate_invoke_impl (gboolean has_target, guint32 param_count, guint32 *code_len)
5748 guint8 *code, *start;
5751 * The stack contains:
5757 start = code = mono_global_codeman_reserve (64);
5759 /* Replace the this argument with the target */
5760 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
5761 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoDelegate, target), 4);
5762 x86_mov_membase_reg (code, X86_ESP, 4, X86_ECX, 4);
5763 x86_jump_membase (code, X86_EAX, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
5765 g_assert ((code - start) < 64);
5768 /* 8 for mov_reg and jump, plus 8 for each parameter */
5769 #ifdef __native_client_codegen__
5770 /* TODO: calculate this size correctly */
5771 int code_reserve = 13 + (param_count * 8) + 2 * kNaClAlignment;
5773 int code_reserve = 8 + (param_count * 8);
5774 #endif /* __native_client_codegen__ */
5776 * The stack contains:
5777 * <args in reverse order>
5782 * <args in reverse order>
5785 * without unbalancing the stack.
5786 * So move each arg up a spot in the stack (overwriting un-needed 'this' arg)
5787 * and leaving original spot of first arg as placeholder in stack so
5788 * when callee pops stack everything works.
5791 start = code = mono_global_codeman_reserve (code_reserve);
5793 /* store delegate for access to method_ptr */
5794 x86_mov_reg_membase (code, X86_ECX, X86_ESP, 4, 4);
5797 for (i = 0; i < param_count; ++i) {
5798 x86_mov_reg_membase (code, X86_EAX, X86_ESP, (i+2)*4, 4);
5799 x86_mov_membase_reg (code, X86_ESP, (i+1)*4, X86_EAX, 4);
5802 x86_jump_membase (code, X86_ECX, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
5804 g_assert ((code - start) < code_reserve);
5807 mono_debug_add_delegate_trampoline (start, code - start);
5810 *code_len = code - start;
5812 if (mono_jit_map_is_enabled ()) {
5815 buff = (char*)"delegate_invoke_has_target";
5817 buff = g_strdup_printf ("delegate_invoke_no_target_%d", param_count);
5818 mono_emit_jit_tramp (start, code - start, buff);
5827 mono_arch_get_delegate_invoke_impls (void)
5834 code = get_delegate_invoke_impl (TRUE, 0, &code_len);
5835 res = g_slist_prepend (res, mono_tramp_info_create (g_strdup ("delegate_invoke_impl_has_target"), code, code_len, NULL, NULL));
5837 for (i = 0; i < MAX_ARCH_DELEGATE_PARAMS; ++i) {
5838 code = get_delegate_invoke_impl (FALSE, i, &code_len);
5839 res = g_slist_prepend (res, mono_tramp_info_create (g_strdup_printf ("delegate_invoke_impl_target_%d", i), code, code_len, NULL, NULL));
5846 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
5848 guint8 *code, *start;
5850 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
5853 /* FIXME: Support more cases */
5854 if (MONO_TYPE_ISSTRUCT (sig->ret))
5858 * The stack contains:
5864 static guint8* cached = NULL;
5869 start = mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
5871 start = get_delegate_invoke_impl (TRUE, 0, NULL);
5873 mono_memory_barrier ();
5877 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
5880 for (i = 0; i < sig->param_count; ++i)
5881 if (!mono_is_regsize_var (sig->params [i]))
5884 code = cache [sig->param_count];
5888 if (mono_aot_only) {
5889 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
5890 start = mono_aot_get_trampoline (name);
5893 start = get_delegate_invoke_impl (FALSE, sig->param_count, NULL);
5896 mono_memory_barrier ();
5898 cache [sig->param_count] = start;
5905 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
5908 case X86_EAX: return (gpointer)ctx->eax;
5909 case X86_EBX: return (gpointer)ctx->ebx;
5910 case X86_ECX: return (gpointer)ctx->ecx;
5911 case X86_EDX: return (gpointer)ctx->edx;
5912 case X86_ESP: return (gpointer)ctx->esp;
5913 case X86_EBP: return (gpointer)ctx->ebp;
5914 case X86_ESI: return (gpointer)ctx->esi;
5915 case X86_EDI: return (gpointer)ctx->edi;
5916 default: g_assert_not_reached ();
5920 #ifdef MONO_ARCH_SIMD_INTRINSICS
5923 get_float_to_x_spill_area (MonoCompile *cfg)
5925 if (!cfg->fconv_to_r8_x_var) {
5926 cfg->fconv_to_r8_x_var = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
5927 cfg->fconv_to_r8_x_var->flags |= MONO_INST_VOLATILE; /*FIXME, use the don't regalloc flag*/
5929 return cfg->fconv_to_r8_x_var;
5933 * Convert all fconv opts that MONO_OPT_SSE2 would get wrong.
5936 mono_arch_decompose_opts (MonoCompile *cfg, MonoInst *ins)
5939 int dreg, src_opcode;
5941 if (!(cfg->opt & MONO_OPT_SSE2) || !(cfg->opt & MONO_OPT_SIMD) || COMPILE_LLVM (cfg))
5944 switch (src_opcode = ins->opcode) {
5945 case OP_FCONV_TO_I1:
5946 case OP_FCONV_TO_U1:
5947 case OP_FCONV_TO_I2:
5948 case OP_FCONV_TO_U2:
5949 case OP_FCONV_TO_I4:
5956 /* dreg is the IREG and sreg1 is the FREG */
5957 MONO_INST_NEW (cfg, fconv, OP_FCONV_TO_R8_X);
5958 fconv->klass = NULL; /*FIXME, what can I use here as the Mono.Simd lib might not be loaded yet*/
5959 fconv->sreg1 = ins->sreg1;
5960 fconv->dreg = mono_alloc_ireg (cfg);
5961 fconv->type = STACK_VTYPE;
5962 fconv->backend.spill_var = get_float_to_x_spill_area (cfg);
5964 mono_bblock_insert_before_ins (cfg->cbb, ins, fconv);
5968 ins->opcode = OP_XCONV_R8_TO_I4;
5970 ins->klass = mono_defaults.int32_class;
5971 ins->sreg1 = fconv->dreg;
5973 ins->type = STACK_I4;
5974 ins->backend.source_opcode = src_opcode;
5977 #endif /* #ifdef MONO_ARCH_SIMD_INTRINSICS */
5980 mono_arch_decompose_long_opts (MonoCompile *cfg, MonoInst *long_ins)
5985 if (long_ins->opcode == OP_LNEG) {
5987 MONO_EMIT_NEW_UNALU (cfg, OP_INEG, ins->dreg + 1, ins->sreg1 + 1);
5988 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADC_IMM, ins->dreg + 2, ins->sreg1 + 2, 0);
5989 MONO_EMIT_NEW_UNALU (cfg, OP_INEG, ins->dreg + 2, ins->dreg + 2);
5994 #ifdef MONO_ARCH_SIMD_INTRINSICS
5996 if (!(cfg->opt & MONO_OPT_SIMD))
5999 /*TODO move this to simd-intrinsic.c once we support sse 4.1 dword extractors since we need the runtime caps info */
6000 switch (long_ins->opcode) {
6002 vreg = long_ins->sreg1;
6004 if (long_ins->inst_c0) {
6005 MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
6006 ins->klass = long_ins->klass;
6007 ins->sreg1 = long_ins->sreg1;
6009 ins->type = STACK_VTYPE;
6010 ins->dreg = vreg = alloc_ireg (cfg);
6011 MONO_ADD_INS (cfg->cbb, ins);
6014 MONO_INST_NEW (cfg, ins, OP_EXTRACT_I4);
6015 ins->klass = mono_defaults.int32_class;
6017 ins->type = STACK_I4;
6018 ins->dreg = long_ins->dreg + 1;
6019 MONO_ADD_INS (cfg->cbb, ins);
6021 MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
6022 ins->klass = long_ins->klass;
6023 ins->sreg1 = long_ins->sreg1;
6024 ins->inst_c0 = long_ins->inst_c0 ? 3 : 1;
6025 ins->type = STACK_VTYPE;
6026 ins->dreg = vreg = alloc_ireg (cfg);
6027 MONO_ADD_INS (cfg->cbb, ins);
6029 MONO_INST_NEW (cfg, ins, OP_EXTRACT_I4);
6030 ins->klass = mono_defaults.int32_class;
6032 ins->type = STACK_I4;
6033 ins->dreg = long_ins->dreg + 2;
6034 MONO_ADD_INS (cfg->cbb, ins);
6036 long_ins->opcode = OP_NOP;
6038 case OP_INSERTX_I8_SLOW:
6039 MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
6040 ins->dreg = long_ins->dreg;
6041 ins->sreg1 = long_ins->dreg;
6042 ins->sreg2 = long_ins->sreg2 + 1;
6043 ins->inst_c0 = long_ins->inst_c0 * 2;
6044 MONO_ADD_INS (cfg->cbb, ins);
6046 MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
6047 ins->dreg = long_ins->dreg;
6048 ins->sreg1 = long_ins->dreg;
6049 ins->sreg2 = long_ins->sreg2 + 2;
6050 ins->inst_c0 = long_ins->inst_c0 * 2 + 1;
6051 MONO_ADD_INS (cfg->cbb, ins);
6053 long_ins->opcode = OP_NOP;
6056 MONO_INST_NEW (cfg, ins, OP_ICONV_TO_X);
6057 ins->dreg = long_ins->dreg;
6058 ins->sreg1 = long_ins->sreg1 + 1;
6059 ins->klass = long_ins->klass;
6060 ins->type = STACK_VTYPE;
6061 MONO_ADD_INS (cfg->cbb, ins);
6063 MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
6064 ins->dreg = long_ins->dreg;
6065 ins->sreg1 = long_ins->dreg;
6066 ins->sreg2 = long_ins->sreg1 + 2;
6068 ins->klass = long_ins->klass;
6069 ins->type = STACK_VTYPE;
6070 MONO_ADD_INS (cfg->cbb, ins);
6072 MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
6073 ins->dreg = long_ins->dreg;
6074 ins->sreg1 = long_ins->dreg;;
6075 ins->inst_c0 = 0x44; /*Magic number for swizzling (X,Y,X,Y)*/
6076 ins->klass = long_ins->klass;
6077 ins->type = STACK_VTYPE;
6078 MONO_ADD_INS (cfg->cbb, ins);
6080 long_ins->opcode = OP_NOP;
6083 #endif /* MONO_ARCH_SIMD_INTRINSICS */
6086 /*MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD*/
6088 mono_arch_install_handler_block_guard (MonoJitInfo *ji, MonoJitExceptionInfo *clause, MonoContext *ctx, gpointer new_value)
6091 gpointer *sp, old_value;
6093 const unsigned char *handler;
6095 /*Decode the first instruction to figure out where did we store the spvar*/
6096 /*Our jit MUST generate the following:
6098 Which is encoded as: 0x89 mod_rm.
6099 mod_rm (esp, ebp, imm) which can be: (imm will never be zero)
6100 mod (reg + imm8): 01 reg(esp): 100 rm(ebp): 101 -> 01100101 (0x65)
6101 mod (reg + imm32): 10 reg(esp): 100 rm(ebp): 101 -> 10100101 (0xA5)
6103 handler = clause->handler_start;
6105 if (*handler != 0x89)
6110 if (*handler == 0x65)
6111 offset = *(signed char*)(handler + 1);
6112 else if (*handler == 0xA5)
6113 offset = *(int*)(handler + 1);
6118 bp = MONO_CONTEXT_GET_BP (ctx);
6119 sp = *(gpointer*)(bp + offset);
6122 if (old_value < ji->code_start || (char*)old_value > ((char*)ji->code_start + ji->code_size))
6131 * mono_aot_emit_load_got_addr:
6133 * Emit code to load the got address.
6134 * On x86, the result is placed into EBX.
6137 mono_arch_emit_load_got_addr (guint8 *start, guint8 *code, MonoCompile *cfg, MonoJumpInfo **ji)
6139 x86_call_imm (code, 0);
6141 * The patch needs to point to the pop, since the GOT offset needs
6142 * to be added to that address.
6145 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GOT_OFFSET, NULL);
6147 *ji = mono_patch_info_list_prepend (*ji, code - start, MONO_PATCH_INFO_GOT_OFFSET, NULL);
6148 x86_pop_reg (code, MONO_ARCH_GOT_REG);
6149 x86_alu_reg_imm (code, X86_ADD, MONO_ARCH_GOT_REG, 0xf0f0f0f0);
6155 * mono_ppc_emit_load_aotconst:
6157 * Emit code to load the contents of the GOT slot identified by TRAMP_TYPE and
6158 * TARGET from the mscorlib GOT in full-aot code.
6159 * On x86, the GOT address is assumed to be in EBX, and the result is placed into
6163 mono_arch_emit_load_aotconst (guint8 *start, guint8 *code, MonoJumpInfo **ji, int tramp_type, gconstpointer target)
6165 /* Load the mscorlib got address */
6166 x86_mov_reg_membase (code, X86_EAX, MONO_ARCH_GOT_REG, sizeof (gpointer), 4);
6167 *ji = mono_patch_info_list_prepend (*ji, code - start, tramp_type, target);
6168 /* arch_emit_got_access () patches this */
6169 x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0xf0f0f0f0, 4);
6174 /* Can't put this into mini-x86.h */
6176 mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot);
6179 mono_arch_get_trampolines (gboolean aot)
6181 MonoTrampInfo *info;
6182 GSList *tramps = NULL;
6184 mono_x86_get_signal_exception_trampoline (&info, aot);
6186 tramps = g_slist_append (tramps, info);
6193 #define DBG_SIGNAL SIGBUS
6195 #define DBG_SIGNAL SIGSEGV
6198 /* Soft Debug support */
6199 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
6202 * mono_arch_set_breakpoint:
6204 * Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
6205 * The location should contain code emitted by OP_SEQ_POINT.
6208 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
6213 * In production, we will use int3 (has to fix the size in the md
6214 * file). But that could confuse gdb, so during development, we emit a SIGSEGV
6217 g_assert (code [0] == 0x90);
6218 x86_alu_reg_mem (code, X86_CMP, X86_EAX, (guint32)bp_trigger_page);
6222 * mono_arch_clear_breakpoint:
6224 * Clear the breakpoint at IP.
6227 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
6232 for (i = 0; i < 6; ++i)
6237 * mono_arch_start_single_stepping:
6239 * Start single stepping.
6242 mono_arch_start_single_stepping (void)
6244 mono_mprotect (ss_trigger_page, mono_pagesize (), 0);
6248 * mono_arch_stop_single_stepping:
6250 * Stop single stepping.
6253 mono_arch_stop_single_stepping (void)
6255 mono_mprotect (ss_trigger_page, mono_pagesize (), MONO_MMAP_READ);
6259 * mono_arch_is_single_step_event:
6261 * Return whenever the machine state in SIGCTX corresponds to a single
6265 mono_arch_is_single_step_event (void *info, void *sigctx)
6268 EXCEPTION_RECORD* einfo = (EXCEPTION_RECORD*)info; /* Sometimes the address is off by 4 */
6269 if ((einfo->ExceptionInformation[1] >= ss_trigger_page && (guint8*)einfo->ExceptionInformation[1] <= (guint8*)ss_trigger_page + 128))
6274 siginfo_t* sinfo = (siginfo_t*) info;
6275 /* Sometimes the address is off by 4 */
6276 if (sinfo->si_signo == DBG_SIGNAL && (sinfo->si_addr >= ss_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)ss_trigger_page + 128))
6284 mono_arch_is_breakpoint_event (void *info, void *sigctx)
6287 EXCEPTION_RECORD* einfo = (EXCEPTION_RECORD*)info; /* Sometimes the address is off by 4 */
6288 if ((einfo->ExceptionInformation[1] >= bp_trigger_page && (guint8*)einfo->ExceptionInformation[1] <= (guint8*)bp_trigger_page + 128))
6293 siginfo_t* sinfo = (siginfo_t*)info;
6294 /* Sometimes the address is off by 4 */
6295 if (sinfo->si_signo == DBG_SIGNAL && (sinfo->si_addr >= bp_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)bp_trigger_page + 128))
6303 * mono_arch_get_ip_for_breakpoint:
6305 * See mini-amd64.c for docs.
6308 mono_arch_get_ip_for_breakpoint (MonoJitInfo *ji, MonoContext *ctx)
6310 guint8 *ip = MONO_CONTEXT_GET_IP (ctx);
6315 #define BREAKPOINT_SIZE 6
6318 * mono_arch_get_ip_for_single_step:
6320 * See mini-amd64.c for docs.
6323 mono_arch_get_ip_for_single_step (MonoJitInfo *ji, MonoContext *ctx)
6325 guint8 *ip = MONO_CONTEXT_GET_IP (ctx);
6327 /* Size of x86_alu_reg_imm */
6334 * mono_arch_skip_breakpoint:
6336 * See mini-amd64.c for docs.
6339 mono_arch_skip_breakpoint (MonoContext *ctx)
6341 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + BREAKPOINT_SIZE);
6345 * mono_arch_skip_single_step:
6347 * See mini-amd64.c for docs.
6350 mono_arch_skip_single_step (MonoContext *ctx)
6352 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 6);
6356 * mono_arch_get_seq_point_info:
6358 * See mini-amd64.c for docs.
6361 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)