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/utils/mono-math.h>
24 #include <mono/utils/mono-counters.h>
25 #include <mono/utils/mono-mmap.h>
32 /* On windows, these hold the key returned by TlsAlloc () */
33 static gint lmf_tls_offset = -1;
34 static gint lmf_addr_tls_offset = -1;
35 static gint appdomain_tls_offset = -1;
38 static gboolean optimize_for_xen = TRUE;
40 #define optimize_for_xen 0
44 static gboolean is_win32 = TRUE;
46 static gboolean is_win32 = FALSE;
49 /* This mutex protects architecture specific caches */
50 #define mono_mini_arch_lock() EnterCriticalSection (&mini_arch_mutex)
51 #define mono_mini_arch_unlock() LeaveCriticalSection (&mini_arch_mutex)
52 static CRITICAL_SECTION mini_arch_mutex;
54 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
59 /* Under windows, the default pinvoke calling convention is stdcall */
60 #define CALLCONV_IS_STDCALL(sig) ((((sig)->call_convention) == MONO_CALL_STDCALL) || ((sig)->pinvoke && ((sig)->call_convention) == MONO_CALL_DEFAULT))
62 #define CALLCONV_IS_STDCALL(sig) (((sig)->call_convention) == MONO_CALL_STDCALL)
66 mono_breakpoint_info [MONO_BREAKPOINT_ARRAY_SIZE];
69 * The code generated for sequence points reads from this location, which is
70 * made read-only when single stepping is enabled.
72 static gpointer ss_trigger_page;
74 /* Enabled breakpoints read from this trigger page */
75 static gpointer bp_trigger_page;
78 mono_arch_regname (int reg)
81 case X86_EAX: return "%eax";
82 case X86_EBX: return "%ebx";
83 case X86_ECX: return "%ecx";
84 case X86_EDX: return "%edx";
85 case X86_ESP: return "%esp";
86 case X86_EBP: return "%ebp";
87 case X86_EDI: return "%edi";
88 case X86_ESI: return "%esi";
94 mono_arch_fregname (int reg)
119 mono_arch_xregname (int reg)
160 /* Only if storage == ArgValuetypeInReg */
161 ArgStorage pair_storage [2];
170 gboolean need_stack_align;
171 guint32 stack_align_amount;
179 #define FLOAT_PARAM_REGS 0
181 static X86_Reg_No param_regs [] = { 0 };
183 #if defined(TARGET_WIN32) || defined(__APPLE__) || defined(__FreeBSD__)
184 #define SMALL_STRUCTS_IN_REGS
185 static X86_Reg_No return_regs [] = { X86_EAX, X86_EDX };
189 add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
191 ainfo->offset = *stack_size;
193 if (*gr >= PARAM_REGS) {
194 ainfo->storage = ArgOnStack;
195 (*stack_size) += sizeof (gpointer);
198 ainfo->storage = ArgInIReg;
199 ainfo->reg = param_regs [*gr];
205 add_general_pair (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
207 ainfo->offset = *stack_size;
209 g_assert (PARAM_REGS == 0);
211 ainfo->storage = ArgOnStack;
212 (*stack_size) += sizeof (gpointer) * 2;
216 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
218 ainfo->offset = *stack_size;
220 if (*gr >= FLOAT_PARAM_REGS) {
221 ainfo->storage = ArgOnStack;
222 (*stack_size) += is_double ? 8 : 4;
225 /* A double register */
227 ainfo->storage = ArgInDoubleSSEReg;
229 ainfo->storage = ArgInFloatSSEReg;
237 add_valuetype (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
239 guint32 *gr, guint32 *fr, guint32 *stack_size)
244 klass = mono_class_from_mono_type (type);
245 size = mini_type_stack_size_full (gsctx, &klass->byval_arg, NULL, sig->pinvoke);
247 #ifdef SMALL_STRUCTS_IN_REGS
248 if (sig->pinvoke && is_return) {
249 MonoMarshalType *info;
252 * the exact rules are not very well documented, the code below seems to work with the
253 * code generated by gcc 3.3.3 -mno-cygwin.
255 info = mono_marshal_load_type_info (klass);
258 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
260 /* Special case structs with only a float member */
261 if ((info->native_size == 8) && (info->num_fields == 1) && (info->fields [0].field->type->type == MONO_TYPE_R8)) {
262 ainfo->storage = ArgValuetypeInReg;
263 ainfo->pair_storage [0] = ArgOnDoubleFpStack;
266 if ((info->native_size == 4) && (info->num_fields == 1) && (info->fields [0].field->type->type == MONO_TYPE_R4)) {
267 ainfo->storage = ArgValuetypeInReg;
268 ainfo->pair_storage [0] = ArgOnFloatFpStack;
271 if ((info->native_size == 1) || (info->native_size == 2) || (info->native_size == 4) || (info->native_size == 8)) {
272 ainfo->storage = ArgValuetypeInReg;
273 ainfo->pair_storage [0] = ArgInIReg;
274 ainfo->pair_regs [0] = return_regs [0];
275 if (info->native_size > 4) {
276 ainfo->pair_storage [1] = ArgInIReg;
277 ainfo->pair_regs [1] = return_regs [1];
284 ainfo->offset = *stack_size;
285 ainfo->storage = ArgOnStack;
286 *stack_size += ALIGN_TO (size, sizeof (gpointer));
292 * Obtain information about a call according to the calling convention.
293 * For x86 ELF, see the "System V Application Binary Interface Intel386
294 * Architecture Processor Supplment, Fourth Edition" document for more
296 * For x86 win32, see ???.
299 get_call_info_internal (MonoGenericSharingContext *gsctx, CallInfo *cinfo, MonoMethodSignature *sig, gboolean is_pinvoke)
303 int n = sig->hasthis + sig->param_count;
304 guint32 stack_size = 0;
311 ret_type = mini_type_get_underlying_type (gsctx, sig->ret);
312 switch (ret_type->type) {
313 case MONO_TYPE_BOOLEAN:
324 case MONO_TYPE_FNPTR:
325 case MONO_TYPE_CLASS:
326 case MONO_TYPE_OBJECT:
327 case MONO_TYPE_SZARRAY:
328 case MONO_TYPE_ARRAY:
329 case MONO_TYPE_STRING:
330 cinfo->ret.storage = ArgInIReg;
331 cinfo->ret.reg = X86_EAX;
335 cinfo->ret.storage = ArgInIReg;
336 cinfo->ret.reg = X86_EAX;
339 cinfo->ret.storage = ArgOnFloatFpStack;
342 cinfo->ret.storage = ArgOnDoubleFpStack;
344 case MONO_TYPE_GENERICINST:
345 if (!mono_type_generic_inst_is_valuetype (sig->ret)) {
346 cinfo->ret.storage = ArgInIReg;
347 cinfo->ret.reg = X86_EAX;
351 case MONO_TYPE_VALUETYPE: {
352 guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
354 add_valuetype (gsctx, sig, &cinfo->ret, sig->ret, TRUE, &tmp_gr, &tmp_fr, &tmp_stacksize);
355 if (cinfo->ret.storage == ArgOnStack)
356 /* The caller passes the address where the value is stored */
357 add_general (&gr, &stack_size, &cinfo->ret);
360 case MONO_TYPE_TYPEDBYREF:
361 /* Same as a valuetype with size 24 */
362 add_general (&gr, &stack_size, &cinfo->ret);
366 cinfo->ret.storage = ArgNone;
369 g_error ("Can't handle as return value 0x%x", sig->ret->type);
375 add_general (&gr, &stack_size, cinfo->args + 0);
377 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
379 fr = FLOAT_PARAM_REGS;
381 /* Emit the signature cookie just before the implicit arguments */
382 add_general (&gr, &stack_size, &cinfo->sig_cookie);
385 for (i = 0; i < sig->param_count; ++i) {
386 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
389 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
390 /* We allways pass the sig cookie on the stack for simplicity */
392 * Prevent implicit arguments + the sig cookie from being passed
396 fr = FLOAT_PARAM_REGS;
398 /* Emit the signature cookie just before the implicit arguments */
399 add_general (&gr, &stack_size, &cinfo->sig_cookie);
402 if (sig->params [i]->byref) {
403 add_general (&gr, &stack_size, ainfo);
406 ptype = mini_type_get_underlying_type (gsctx, sig->params [i]);
407 switch (ptype->type) {
408 case MONO_TYPE_BOOLEAN:
411 add_general (&gr, &stack_size, ainfo);
416 add_general (&gr, &stack_size, ainfo);
420 add_general (&gr, &stack_size, ainfo);
425 case MONO_TYPE_FNPTR:
426 case MONO_TYPE_CLASS:
427 case MONO_TYPE_OBJECT:
428 case MONO_TYPE_STRING:
429 case MONO_TYPE_SZARRAY:
430 case MONO_TYPE_ARRAY:
431 add_general (&gr, &stack_size, ainfo);
433 case MONO_TYPE_GENERICINST:
434 if (!mono_type_generic_inst_is_valuetype (sig->params [i])) {
435 add_general (&gr, &stack_size, ainfo);
439 case MONO_TYPE_VALUETYPE:
440 add_valuetype (gsctx, sig, ainfo, sig->params [i], FALSE, &gr, &fr, &stack_size);
442 case MONO_TYPE_TYPEDBYREF:
443 stack_size += sizeof (MonoTypedRef);
444 ainfo->storage = ArgOnStack;
448 add_general_pair (&gr, &stack_size, ainfo);
451 add_float (&fr, &stack_size, ainfo, FALSE);
454 add_float (&fr, &stack_size, ainfo, TRUE);
457 g_error ("unexpected type 0x%x", ptype->type);
458 g_assert_not_reached ();
462 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
464 fr = FLOAT_PARAM_REGS;
466 /* Emit the signature cookie just before the implicit arguments */
467 add_general (&gr, &stack_size, &cinfo->sig_cookie);
470 if (mono_do_x86_stack_align && (stack_size % MONO_ARCH_FRAME_ALIGNMENT) != 0) {
471 cinfo->need_stack_align = TRUE;
472 cinfo->stack_align_amount = MONO_ARCH_FRAME_ALIGNMENT - (stack_size % MONO_ARCH_FRAME_ALIGNMENT);
473 stack_size += cinfo->stack_align_amount;
476 cinfo->stack_usage = stack_size;
477 cinfo->reg_usage = gr;
478 cinfo->freg_usage = fr;
483 get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSignature *sig, gboolean is_pinvoke)
485 int n = sig->hasthis + sig->param_count;
489 cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
491 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
493 return get_call_info_internal (gsctx, cinfo, sig, is_pinvoke);
497 * mono_arch_get_argument_info:
498 * @csig: a method signature
499 * @param_count: the number of parameters to consider
500 * @arg_info: an array to store the result infos
502 * Gathers information on parameters such as size, alignment and
503 * padding. arg_info should be large enought to hold param_count + 1 entries.
505 * Returns the size of the argument area on the stack.
506 * This should be signal safe, since it is called from
507 * mono_arch_find_jit_info_ext ().
508 * FIXME: The metadata calls might not be signal safe.
511 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
513 int k, args_size = 0;
519 /* Avoid g_malloc as it is not signal safe */
520 cinfo = (CallInfo*)g_newa (guint8*, sizeof (CallInfo) + (sizeof (ArgInfo) * (csig->param_count + 1)));
522 cinfo = get_call_info_internal (NULL, cinfo, csig, FALSE);
524 if (MONO_TYPE_ISSTRUCT (csig->ret) && (cinfo->ret.storage == ArgOnStack)) {
525 args_size += sizeof (gpointer);
529 arg_info [0].offset = offset;
532 args_size += sizeof (gpointer);
536 arg_info [0].size = args_size;
538 for (k = 0; k < param_count; k++) {
539 size = mini_type_stack_size_full (NULL, csig->params [k], &align, csig->pinvoke);
541 /* ignore alignment for now */
544 args_size += pad = (align - (args_size & (align - 1))) & (align - 1);
545 arg_info [k].pad = pad;
547 arg_info [k + 1].pad = 0;
548 arg_info [k + 1].size = size;
550 arg_info [k + 1].offset = offset;
554 if (mono_do_x86_stack_align && !CALLCONV_IS_STDCALL (csig))
555 align = MONO_ARCH_FRAME_ALIGNMENT;
558 args_size += pad = (align - (args_size & (align - 1))) & (align - 1);
559 arg_info [k].pad = pad;
564 static const guchar cpuid_impl [] = {
565 0x55, /* push %ebp */
566 0x89, 0xe5, /* mov %esp,%ebp */
567 0x53, /* push %ebx */
568 0x8b, 0x45, 0x08, /* mov 0x8(%ebp),%eax */
569 0x0f, 0xa2, /* cpuid */
570 0x50, /* push %eax */
571 0x8b, 0x45, 0x10, /* mov 0x10(%ebp),%eax */
572 0x89, 0x18, /* mov %ebx,(%eax) */
573 0x8b, 0x45, 0x14, /* mov 0x14(%ebp),%eax */
574 0x89, 0x08, /* mov %ecx,(%eax) */
575 0x8b, 0x45, 0x18, /* mov 0x18(%ebp),%eax */
576 0x89, 0x10, /* mov %edx,(%eax) */
578 0x8b, 0x55, 0x0c, /* mov 0xc(%ebp),%edx */
579 0x89, 0x02, /* mov %eax,(%edx) */
585 typedef void (*CpuidFunc) (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx);
588 cpuid (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx)
592 __asm__ __volatile__ (
595 "movl %%eax, %%edx\n"
596 "xorl $0x200000, %%eax\n"
601 "xorl %%edx, %%eax\n"
602 "andl $0x200000, %%eax\n"
624 /* Have to use the code manager to get around WinXP DEP */
625 static CpuidFunc func = NULL;
628 ptr = mono_global_codeman_reserve (sizeof (cpuid_impl));
629 memcpy (ptr, cpuid_impl, sizeof (cpuid_impl));
630 func = (CpuidFunc)ptr;
632 func (id, p_eax, p_ebx, p_ecx, p_edx);
635 * We use this approach because of issues with gcc and pic code, see:
636 * http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=7329
637 __asm__ __volatile__ ("cpuid"
638 : "=a" (*p_eax), "=b" (*p_ebx), "=c" (*p_ecx), "=d" (*p_edx)
647 * Initialize the cpu to execute managed code.
650 mono_arch_cpu_init (void)
652 /* spec compliance requires running with double precision */
656 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
657 fpcw &= ~X86_FPCW_PRECC_MASK;
658 fpcw |= X86_FPCW_PREC_DOUBLE;
659 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
660 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
662 _control87 (_PC_53, MCW_PC);
667 * Initialize architecture specific code.
670 mono_arch_init (void)
672 InitializeCriticalSection (&mini_arch_mutex);
674 ss_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ);
675 bp_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_32BIT);
676 mono_mprotect (bp_trigger_page, mono_pagesize (), 0);
680 * Cleanup architecture specific code.
683 mono_arch_cleanup (void)
685 DeleteCriticalSection (&mini_arch_mutex);
689 * This function returns the optimizations supported on this cpu.
692 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
694 int eax, ebx, ecx, edx;
698 /* Feature Flags function, flags returned in EDX. */
699 if (cpuid (1, &eax, &ebx, &ecx, &edx)) {
700 if (edx & (1 << 15)) {
701 opts |= MONO_OPT_CMOV;
703 opts |= MONO_OPT_FCMOV;
705 *exclude_mask |= MONO_OPT_FCMOV;
707 *exclude_mask |= MONO_OPT_CMOV;
709 opts |= MONO_OPT_SSE2;
711 *exclude_mask |= MONO_OPT_SSE2;
713 #ifdef MONO_ARCH_SIMD_INTRINSICS
714 /*SIMD intrinsics require at least SSE2.*/
715 if (!(opts & MONO_OPT_SSE2))
716 *exclude_mask |= MONO_OPT_SIMD;
723 * This function test for all SSE functions supported.
725 * Returns a bitmask corresponding to all supported versions.
729 mono_arch_cpu_enumerate_simd_versions (void)
731 int eax, ebx, ecx, edx;
732 guint32 sse_opts = 0;
734 if (cpuid (1, &eax, &ebx, &ecx, &edx)) {
736 sse_opts |= 1 << SIMD_VERSION_SSE1;
738 sse_opts |= 1 << SIMD_VERSION_SSE2;
740 sse_opts |= 1 << SIMD_VERSION_SSE3;
742 sse_opts |= 1 << SIMD_VERSION_SSSE3;
744 sse_opts |= 1 << SIMD_VERSION_SSE41;
746 sse_opts |= 1 << SIMD_VERSION_SSE42;
749 /* Yes, all this needs to be done to check for sse4a.
750 See: "Amd: CPUID Specification"
752 if (cpuid (0x80000000, &eax, &ebx, &ecx, &edx)) {
753 /* eax greater or equal than 0x80000001, ebx = 'htuA', ecx = DMAc', edx = 'itne'*/
754 if ((((unsigned int) eax) >= 0x80000001) && (ebx == 0x68747541) && (ecx == 0x444D4163) && (edx == 0x69746E65)) {
755 cpuid (0x80000001, &eax, &ebx, &ecx, &edx);
757 sse_opts |= 1 << SIMD_VERSION_SSE4a;
766 * Determine whenever the trap whose info is in SIGINFO is caused by
770 mono_arch_is_int_overflow (void *sigctx, void *info)
775 mono_arch_sigctx_to_monoctx (sigctx, &ctx);
777 ip = (guint8*)ctx.eip;
779 if ((ip [0] == 0xf7) && (x86_modrm_mod (ip [1]) == 0x3) && (x86_modrm_reg (ip [1]) == 0x7)) {
783 switch (x86_modrm_rm (ip [1])) {
803 g_assert_not_reached ();
815 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
820 for (i = 0; i < cfg->num_varinfo; i++) {
821 MonoInst *ins = cfg->varinfo [i];
822 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
825 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
828 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
829 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
832 /* we dont allocate I1 to registers because there is no simply way to sign extend
833 * 8bit quantities in caller saved registers on x86 */
834 if (mono_is_regsize_var (ins->inst_vtype) && (ins->inst_vtype->type != MONO_TYPE_I1)) {
835 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
836 g_assert (i == vmv->idx);
837 vars = g_list_prepend (vars, vmv);
841 vars = mono_varlist_sort (cfg, vars, 0);
847 mono_arch_get_global_int_regs (MonoCompile *cfg)
851 /* we can use 3 registers for global allocation */
852 regs = g_list_prepend (regs, (gpointer)X86_EBX);
853 regs = g_list_prepend (regs, (gpointer)X86_ESI);
854 regs = g_list_prepend (regs, (gpointer)X86_EDI);
860 * mono_arch_regalloc_cost:
862 * Return the cost, in number of memory references, of the action of
863 * allocating the variable VMV into a register during global register
867 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
869 MonoInst *ins = cfg->varinfo [vmv->idx];
871 if (cfg->method->save_lmf)
872 /* The register is already saved */
873 return (ins->opcode == OP_ARG) ? 1 : 0;
875 /* push+pop+possible load if it is an argument */
876 return (ins->opcode == OP_ARG) ? 3 : 2;
880 set_needs_stack_frame (MonoCompile *cfg, gboolean flag)
882 static int inited = FALSE;
883 static int count = 0;
885 if (cfg->arch.need_stack_frame_inited) {
886 g_assert (cfg->arch.need_stack_frame == flag);
890 cfg->arch.need_stack_frame = flag;
891 cfg->arch.need_stack_frame_inited = TRUE;
897 mono_counters_register ("Could eliminate stack frame", MONO_COUNTER_INT|MONO_COUNTER_JIT, &count);
902 //g_print ("will eliminate %s.%s.%s\n", cfg->method->klass->name_space, cfg->method->klass->name, cfg->method->name);
906 needs_stack_frame (MonoCompile *cfg)
908 MonoMethodSignature *sig;
909 MonoMethodHeader *header;
910 gboolean result = FALSE;
912 #if defined(__APPLE__)
913 /*OSX requires stack frame code to have the correct alignment. */
917 if (cfg->arch.need_stack_frame_inited)
918 return cfg->arch.need_stack_frame;
920 header = mono_method_get_header (cfg->method);
921 sig = mono_method_signature (cfg->method);
923 if (cfg->disable_omit_fp)
925 else if (cfg->flags & MONO_CFG_HAS_ALLOCA)
927 else if (cfg->method->save_lmf)
929 else if (cfg->stack_offset)
931 else if (cfg->param_area)
933 else if (cfg->flags & (MONO_CFG_HAS_CALLS | MONO_CFG_HAS_ALLOCA | MONO_CFG_HAS_TAIL))
935 else if (header->num_clauses)
937 else if (sig->param_count + sig->hasthis)
939 else if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
941 else if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) ||
942 (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
945 set_needs_stack_frame (cfg, result);
947 return cfg->arch.need_stack_frame;
951 * Set var information according to the calling convention. X86 version.
952 * The locals var stuff should most likely be split in another method.
955 mono_arch_allocate_vars (MonoCompile *cfg)
957 MonoMethodSignature *sig;
958 MonoMethodHeader *header;
960 guint32 locals_stack_size, locals_stack_align;
965 header = mono_method_get_header (cfg->method);
966 sig = mono_method_signature (cfg->method);
968 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, FALSE);
970 cfg->frame_reg = X86_EBP;
973 /* Reserve space to save LMF and caller saved registers */
975 if (cfg->method->save_lmf) {
976 offset += sizeof (MonoLMF);
978 if (cfg->used_int_regs & (1 << X86_EBX)) {
982 if (cfg->used_int_regs & (1 << X86_EDI)) {
986 if (cfg->used_int_regs & (1 << X86_ESI)) {
991 switch (cinfo->ret.storage) {
992 case ArgValuetypeInReg:
993 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
995 cfg->ret->opcode = OP_REGOFFSET;
996 cfg->ret->inst_basereg = X86_EBP;
997 cfg->ret->inst_offset = - offset;
1003 /* Allocate locals */
1004 offsets = mono_allocate_stack_slots (cfg, &locals_stack_size, &locals_stack_align);
1005 if (locals_stack_size > MONO_ARCH_MAX_FRAME_SIZE) {
1006 char *mname = mono_method_full_name (cfg->method, TRUE);
1007 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
1008 cfg->exception_message = g_strdup_printf ("Method %s stack is too big.", mname);
1012 if (locals_stack_align) {
1013 offset += (locals_stack_align - 1);
1014 offset &= ~(locals_stack_align - 1);
1017 * EBP is at alignment 8 % MONO_ARCH_FRAME_ALIGNMENT, so if we
1018 * have locals larger than 8 bytes we need to make sure that
1019 * they have the appropriate offset.
1021 if (MONO_ARCH_FRAME_ALIGNMENT > 8 && locals_stack_align > 8)
1022 offset += MONO_ARCH_FRAME_ALIGNMENT - sizeof (gpointer) * 2;
1023 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1024 if (offsets [i] != -1) {
1025 MonoInst *inst = cfg->varinfo [i];
1026 inst->opcode = OP_REGOFFSET;
1027 inst->inst_basereg = X86_EBP;
1028 inst->inst_offset = - (offset + offsets [i]);
1029 //printf ("allocated local %d to ", i); mono_print_tree_nl (inst);
1032 offset += locals_stack_size;
1036 * Allocate arguments+return value
1039 switch (cinfo->ret.storage) {
1041 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
1043 * In the new IR, the cfg->vret_addr variable represents the
1044 * vtype return value.
1046 cfg->vret_addr->opcode = OP_REGOFFSET;
1047 cfg->vret_addr->inst_basereg = cfg->frame_reg;
1048 cfg->vret_addr->inst_offset = cinfo->ret.offset + ARGS_OFFSET;
1049 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1050 printf ("vret_addr =");
1051 mono_print_ins (cfg->vret_addr);
1054 cfg->ret->opcode = OP_REGOFFSET;
1055 cfg->ret->inst_basereg = X86_EBP;
1056 cfg->ret->inst_offset = cinfo->ret.offset + ARGS_OFFSET;
1059 case ArgValuetypeInReg:
1062 cfg->ret->opcode = OP_REGVAR;
1063 cfg->ret->inst_c0 = cinfo->ret.reg;
1064 cfg->ret->dreg = cinfo->ret.reg;
1067 case ArgOnFloatFpStack:
1068 case ArgOnDoubleFpStack:
1071 g_assert_not_reached ();
1074 if (sig->call_convention == MONO_CALL_VARARG) {
1075 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
1076 cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
1079 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1080 ArgInfo *ainfo = &cinfo->args [i];
1081 inst = cfg->args [i];
1082 if (inst->opcode != OP_REGVAR) {
1083 inst->opcode = OP_REGOFFSET;
1084 inst->inst_basereg = X86_EBP;
1086 inst->inst_offset = ainfo->offset + ARGS_OFFSET;
1089 cfg->stack_offset = offset;
1093 mono_arch_create_vars (MonoCompile *cfg)
1095 MonoMethodSignature *sig;
1098 sig = mono_method_signature (cfg->method);
1100 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, FALSE);
1102 if (cinfo->ret.storage == ArgValuetypeInReg)
1103 cfg->ret_var_is_local = TRUE;
1104 if ((cinfo->ret.storage != ArgValuetypeInReg) && MONO_TYPE_ISSTRUCT (sig->ret)) {
1105 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
1110 * It is expensive to adjust esp for each individual fp argument pushed on the stack
1111 * so we try to do it just once when we have multiple fp arguments in a row.
1112 * We don't use this mechanism generally because for int arguments the generated code
1113 * is slightly bigger and new generation cpus optimize away the dependency chains
1114 * created by push instructions on the esp value.
1115 * fp_arg_setup is the first argument in the execution sequence where the esp register
1118 static G_GNUC_UNUSED int
1119 collect_fp_stack_space (MonoMethodSignature *sig, int start_arg, int *fp_arg_setup)
1124 for (; start_arg < sig->param_count; ++start_arg) {
1125 t = mini_type_get_underlying_type (NULL, sig->params [start_arg]);
1126 if (!t->byref && t->type == MONO_TYPE_R8) {
1127 fp_space += sizeof (double);
1128 *fp_arg_setup = start_arg;
1137 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
1139 MonoMethodSignature *tmp_sig;
1141 /* FIXME: Add support for signature tokens to AOT */
1142 cfg->disable_aot = TRUE;
1145 * mono_ArgIterator_Setup assumes the signature cookie is
1146 * passed first and all the arguments which were before it are
1147 * passed on the stack after the signature. So compensate by
1148 * passing a different signature.
1150 tmp_sig = mono_metadata_signature_dup (call->signature);
1151 tmp_sig->param_count -= call->signature->sentinelpos;
1152 tmp_sig->sentinelpos = 0;
1153 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
1155 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_X86_PUSH_IMM, -1, -1, tmp_sig);
1160 mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
1166 LLVMCallInfo *linfo;
1168 n = sig->param_count + sig->hasthis;
1170 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, sig->pinvoke);
1172 linfo = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
1175 * LLVM always uses the native ABI while we use our own ABI, the
1176 * only difference is the handling of vtypes:
1177 * - we only pass/receive them in registers in some cases, and only
1178 * in 1 or 2 integer registers.
1180 if (cinfo->ret.storage == ArgValuetypeInReg) {
1182 cfg->exception_message = g_strdup ("pinvoke + vtypes");
1183 cfg->disable_llvm = TRUE;
1187 cfg->exception_message = g_strdup ("vtype ret in call");
1188 cfg->disable_llvm = TRUE;
1190 linfo->ret.storage = LLVMArgVtypeInReg;
1191 for (j = 0; j < 2; ++j)
1192 linfo->ret.pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, cinfo->ret.pair_storage [j]);
1196 if (MONO_TYPE_ISSTRUCT (sig->ret) && cinfo->ret.storage == ArgInIReg) {
1197 /* Vtype returned using a hidden argument */
1198 linfo->ret.storage = LLVMArgVtypeRetAddr;
1201 if (MONO_TYPE_ISSTRUCT (sig->ret) && cinfo->ret.storage != ArgInIReg) {
1203 cfg->exception_message = g_strdup ("vtype ret in call");
1204 cfg->disable_llvm = TRUE;
1207 for (i = 0; i < n; ++i) {
1208 ainfo = cinfo->args + i;
1210 linfo->args [i].storage = LLVMArgNone;
1212 switch (ainfo->storage) {
1214 linfo->args [i].storage = LLVMArgInIReg;
1216 case ArgInDoubleSSEReg:
1217 case ArgInFloatSSEReg:
1218 linfo->args [i].storage = LLVMArgInFPReg;
1221 if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(sig->params [i - sig->hasthis]))) {
1222 linfo->args [i].storage = LLVMArgVtypeByVal;
1224 linfo->args [i].storage = LLVMArgInIReg;
1225 if (!sig->params [i - sig->hasthis]->byref) {
1226 if (sig->params [i - sig->hasthis]->type == MONO_TYPE_R4) {
1227 linfo->args [i].storage = LLVMArgInFPReg;
1228 } else if (sig->params [i - sig->hasthis]->type == MONO_TYPE_R8) {
1229 linfo->args [i].storage = LLVMArgInFPReg;
1234 case ArgValuetypeInReg:
1236 cfg->exception_message = g_strdup ("pinvoke + vtypes");
1237 cfg->disable_llvm = TRUE;
1241 cfg->exception_message = g_strdup ("vtype arg");
1242 cfg->disable_llvm = TRUE;
1244 linfo->args [i].storage = LLVMArgVtypeInReg;
1245 for (j = 0; j < 2; ++j)
1246 linfo->args [i].pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, ainfo->pair_storage [j]);
1250 cfg->exception_message = g_strdup ("ainfo->storage");
1251 cfg->disable_llvm = TRUE;
1261 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
1264 MonoMethodSignature *sig;
1267 int sentinelpos = 0;
1269 sig = call->signature;
1270 n = sig->param_count + sig->hasthis;
1272 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, FALSE);
1274 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1275 sentinelpos = sig->sentinelpos + (sig->hasthis ? 1 : 0);
1277 if (cinfo->need_stack_align) {
1278 MONO_INST_NEW (cfg, arg, OP_SUB_IMM);
1279 arg->dreg = X86_ESP;
1280 arg->sreg1 = X86_ESP;
1281 arg->inst_imm = cinfo->stack_align_amount;
1282 MONO_ADD_INS (cfg->cbb, arg);
1285 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
1286 if (cinfo->ret.storage == ArgValuetypeInReg) {
1288 * Tell the JIT to use a more efficient calling convention: call using
1289 * OP_CALL, compute the result location after the call, and save the
1292 call->vret_in_reg = TRUE;
1294 NULLIFY_INS (call->vret_var);
1298 /* Handle the case where there are no implicit arguments */
1299 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sentinelpos)) {
1300 emit_sig_cookie (cfg, call, cinfo);
1303 /* Arguments are pushed in the reverse order */
1304 for (i = n - 1; i >= 0; i --) {
1305 ArgInfo *ainfo = cinfo->args + i;
1308 if (i >= sig->hasthis)
1309 t = sig->params [i - sig->hasthis];
1311 t = &mono_defaults.int_class->byval_arg;
1312 t = mini_type_get_underlying_type (cfg->generic_sharing_context, t);
1314 MONO_INST_NEW (cfg, arg, OP_X86_PUSH);
1316 in = call->args [i];
1317 arg->cil_code = in->cil_code;
1318 arg->sreg1 = in->dreg;
1319 arg->type = in->type;
1321 g_assert (in->dreg != -1);
1323 if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(t))) {
1327 g_assert (in->klass);
1329 if (t->type == MONO_TYPE_TYPEDBYREF) {
1330 size = sizeof (MonoTypedRef);
1331 align = sizeof (gpointer);
1334 size = mini_type_stack_size_full (cfg->generic_sharing_context, &in->klass->byval_arg, &align, sig->pinvoke);
1338 arg->opcode = OP_OUTARG_VT;
1339 arg->sreg1 = in->dreg;
1340 arg->klass = in->klass;
1341 arg->backend.size = size;
1343 MONO_ADD_INS (cfg->cbb, arg);
1347 switch (ainfo->storage) {
1349 arg->opcode = OP_X86_PUSH;
1351 if (t->type == MONO_TYPE_R4) {
1352 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, 4);
1353 arg->opcode = OP_STORER4_MEMBASE_REG;
1354 arg->inst_destbasereg = X86_ESP;
1355 arg->inst_offset = 0;
1356 } else if (t->type == MONO_TYPE_R8) {
1357 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, 8);
1358 arg->opcode = OP_STORER8_MEMBASE_REG;
1359 arg->inst_destbasereg = X86_ESP;
1360 arg->inst_offset = 0;
1361 } else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_U8) {
1363 MONO_EMIT_NEW_UNALU (cfg, OP_X86_PUSH, -1, in->dreg + 2);
1368 g_assert_not_reached ();
1371 MONO_ADD_INS (cfg->cbb, arg);
1374 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sentinelpos)) {
1375 /* Emit the signature cookie just before the implicit arguments */
1376 emit_sig_cookie (cfg, call, cinfo);
1380 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
1383 if (cinfo->ret.storage == ArgValuetypeInReg) {
1386 else if (cinfo->ret.storage == ArgInIReg) {
1388 /* The return address is passed in a register */
1389 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
1390 vtarg->sreg1 = call->inst.dreg;
1391 vtarg->dreg = mono_alloc_ireg (cfg);
1392 MONO_ADD_INS (cfg->cbb, vtarg);
1394 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
1397 MONO_INST_NEW (cfg, vtarg, OP_X86_PUSH);
1398 vtarg->type = STACK_MP;
1399 vtarg->sreg1 = call->vret_var->dreg;
1400 MONO_ADD_INS (cfg->cbb, vtarg);
1403 /* if the function returns a struct on stack, the called method already does a ret $0x4 */
1404 if (cinfo->ret.storage != ArgValuetypeInReg)
1405 cinfo->stack_usage -= 4;
1408 call->stack_usage = cinfo->stack_usage;
1412 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
1415 int size = ins->backend.size;
1418 MONO_INST_NEW (cfg, arg, OP_X86_PUSH_MEMBASE);
1419 arg->sreg1 = src->dreg;
1421 MONO_ADD_INS (cfg->cbb, arg);
1422 } else if (size <= 20) {
1423 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, ALIGN_TO (size, 4));
1424 mini_emit_memcpy (cfg, X86_ESP, 0, src->dreg, 0, size, 4);
1426 MONO_INST_NEW (cfg, arg, OP_X86_PUSH_OBJ);
1427 arg->inst_basereg = src->dreg;
1428 arg->inst_offset = 0;
1429 arg->inst_imm = size;
1431 MONO_ADD_INS (cfg->cbb, arg);
1436 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
1438 MonoType *ret = mini_type_get_underlying_type (cfg->generic_sharing_context, mono_method_signature (method)->ret);
1441 if (ret->type == MONO_TYPE_R4) {
1442 if (COMPILE_LLVM (cfg))
1443 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
1446 } else if (ret->type == MONO_TYPE_R8) {
1447 if (COMPILE_LLVM (cfg))
1448 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
1451 } else if (ret->type == MONO_TYPE_I8 || ret->type == MONO_TYPE_U8) {
1452 if (COMPILE_LLVM (cfg))
1453 MONO_EMIT_NEW_UNALU (cfg, OP_LMOVE, cfg->ret->dreg, val->dreg);
1455 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, X86_EAX, val->dreg + 1);
1456 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, X86_EDX, val->dreg + 2);
1462 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
1466 * Allow tracing to work with this interface (with an optional argument)
1469 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1473 g_assert (MONO_ARCH_FRAME_ALIGNMENT >= 8);
1474 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 8);
1476 /* if some args are passed in registers, we need to save them here */
1477 x86_push_reg (code, X86_EBP);
1479 if (cfg->compile_aot) {
1480 x86_push_imm (code, cfg->method);
1481 x86_mov_reg_imm (code, X86_EAX, func);
1482 x86_call_reg (code, X86_EAX);
1484 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
1485 x86_push_imm (code, cfg->method);
1486 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1487 x86_call_code (code, 0);
1489 x86_alu_reg_imm (code, X86_ADD, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT);
1503 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
1506 int arg_size = 0, stack_usage = 0, save_mode = SAVE_NONE;
1507 MonoMethod *method = cfg->method;
1509 switch (mini_type_get_underlying_type (cfg->generic_sharing_context, mono_method_signature (method)->ret)->type) {
1510 case MONO_TYPE_VOID:
1511 /* special case string .ctor icall */
1512 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class) {
1513 save_mode = SAVE_EAX;
1514 stack_usage = enable_arguments ? 8 : 4;
1516 save_mode = SAVE_NONE;
1520 save_mode = SAVE_EAX_EDX;
1521 stack_usage = enable_arguments ? 16 : 8;
1525 save_mode = SAVE_FP;
1526 stack_usage = enable_arguments ? 16 : 8;
1528 case MONO_TYPE_GENERICINST:
1529 if (!mono_type_generic_inst_is_valuetype (mono_method_signature (method)->ret)) {
1530 save_mode = SAVE_EAX;
1531 stack_usage = enable_arguments ? 8 : 4;
1535 case MONO_TYPE_VALUETYPE:
1536 // FIXME: Handle SMALL_STRUCT_IN_REG here for proper alignment on darwin-x86
1537 save_mode = SAVE_STRUCT;
1538 stack_usage = enable_arguments ? 4 : 0;
1541 save_mode = SAVE_EAX;
1542 stack_usage = enable_arguments ? 8 : 4;
1546 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - stack_usage - 4);
1548 switch (save_mode) {
1550 x86_push_reg (code, X86_EDX);
1551 x86_push_reg (code, X86_EAX);
1552 if (enable_arguments) {
1553 x86_push_reg (code, X86_EDX);
1554 x86_push_reg (code, X86_EAX);
1559 x86_push_reg (code, X86_EAX);
1560 if (enable_arguments) {
1561 x86_push_reg (code, X86_EAX);
1566 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1567 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1568 if (enable_arguments) {
1569 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1570 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1575 if (enable_arguments) {
1576 x86_push_membase (code, X86_EBP, 8);
1585 if (cfg->compile_aot) {
1586 x86_push_imm (code, method);
1587 x86_mov_reg_imm (code, X86_EAX, func);
1588 x86_call_reg (code, X86_EAX);
1590 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
1591 x86_push_imm (code, method);
1592 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1593 x86_call_code (code, 0);
1596 x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
1598 switch (save_mode) {
1600 x86_pop_reg (code, X86_EAX);
1601 x86_pop_reg (code, X86_EDX);
1604 x86_pop_reg (code, X86_EAX);
1607 x86_fld_membase (code, X86_ESP, 0, TRUE);
1608 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1615 x86_alu_reg_imm (code, X86_ADD, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - stack_usage);
1620 #define EMIT_COND_BRANCH(ins,cond,sign) \
1621 if (ins->inst_true_bb->native_offset) { \
1622 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
1624 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1625 if ((cfg->opt & MONO_OPT_BRANCH) && \
1626 x86_is_imm8 (ins->inst_true_bb->max_offset - cpos)) \
1627 x86_branch8 (code, cond, 0, sign); \
1629 x86_branch32 (code, cond, 0, sign); \
1633 * Emit an exception if condition is fail and
1634 * if possible do a directly branch to target
1636 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
1638 MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
1639 if (tins == NULL) { \
1640 mono_add_patch_info (cfg, code - cfg->native_code, \
1641 MONO_PATCH_INFO_EXC, exc_name); \
1642 x86_branch32 (code, cond, 0, signed); \
1644 EMIT_COND_BRANCH (tins, cond, signed); \
1648 #define EMIT_FPCOMPARE(code) do { \
1649 x86_fcompp (code); \
1650 x86_fnstsw (code); \
1655 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data)
1657 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
1658 x86_call_code (code, 0);
1663 #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)))
1666 * mono_peephole_pass_1:
1668 * Perform peephole opts which should/can be performed before local regalloc
1671 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
1675 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
1676 MonoInst *last_ins = ins->prev;
1678 switch (ins->opcode) {
1681 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS)) {
1683 * X86_LEA is like ADD, but doesn't have the
1684 * sreg1==dreg restriction.
1686 ins->opcode = OP_X86_LEA_MEMBASE;
1687 ins->inst_basereg = ins->sreg1;
1688 } else if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1689 ins->opcode = OP_X86_INC_REG;
1693 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS)) {
1694 ins->opcode = OP_X86_LEA_MEMBASE;
1695 ins->inst_basereg = ins->sreg1;
1696 ins->inst_imm = -ins->inst_imm;
1697 } else if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1698 ins->opcode = OP_X86_DEC_REG;
1700 case OP_COMPARE_IMM:
1701 case OP_ICOMPARE_IMM:
1702 /* OP_COMPARE_IMM (reg, 0)
1704 * OP_X86_TEST_NULL (reg)
1707 ins->opcode = OP_X86_TEST_NULL;
1709 case OP_X86_COMPARE_MEMBASE_IMM:
1711 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1712 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
1714 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1715 * OP_COMPARE_IMM reg, imm
1717 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
1719 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
1720 ins->inst_basereg == last_ins->inst_destbasereg &&
1721 ins->inst_offset == last_ins->inst_offset) {
1722 ins->opcode = OP_COMPARE_IMM;
1723 ins->sreg1 = last_ins->sreg1;
1725 /* check if we can remove cmp reg,0 with test null */
1727 ins->opcode = OP_X86_TEST_NULL;
1731 case OP_X86_PUSH_MEMBASE:
1732 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG ||
1733 last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1734 ins->inst_basereg == last_ins->inst_destbasereg &&
1735 ins->inst_offset == last_ins->inst_offset) {
1736 ins->opcode = OP_X86_PUSH;
1737 ins->sreg1 = last_ins->sreg1;
1742 mono_peephole_ins (bb, ins);
1747 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
1751 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
1752 switch (ins->opcode) {
1754 /* reg = 0 -> XOR (reg, reg) */
1755 /* XOR sets cflags on x86, so we cant do it always */
1756 if (ins->inst_c0 == 0 && (!ins->next || (ins->next && INST_IGNORES_CFLAGS (ins->next->opcode)))) {
1759 ins->opcode = OP_IXOR;
1760 ins->sreg1 = ins->dreg;
1761 ins->sreg2 = ins->dreg;
1764 * Convert succeeding STORE_MEMBASE_IMM 0 ins to STORE_MEMBASE_REG
1765 * since it takes 3 bytes instead of 7.
1767 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
1768 if ((ins2->opcode == OP_STORE_MEMBASE_IMM) && (ins2->inst_imm == 0)) {
1769 ins2->opcode = OP_STORE_MEMBASE_REG;
1770 ins2->sreg1 = ins->dreg;
1772 else if ((ins2->opcode == OP_STOREI4_MEMBASE_IMM) && (ins2->inst_imm == 0)) {
1773 ins2->opcode = OP_STOREI4_MEMBASE_REG;
1774 ins2->sreg1 = ins->dreg;
1776 else if ((ins2->opcode == OP_STOREI1_MEMBASE_IMM) || (ins2->opcode == OP_STOREI2_MEMBASE_IMM)) {
1777 /* Continue iteration */
1786 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1787 ins->opcode = OP_X86_INC_REG;
1791 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1792 ins->opcode = OP_X86_DEC_REG;
1796 mono_peephole_ins (bb, ins);
1801 * mono_arch_lowering_pass:
1803 * Converts complex opcodes into simpler ones so that each IR instruction
1804 * corresponds to one machine instruction.
1807 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1809 MonoInst *ins, *next;
1812 * FIXME: Need to add more instructions, but the current machine
1813 * description can't model some parts of the composite instructions like
1816 MONO_BB_FOR_EACH_INS_SAFE (bb, next, ins) {
1817 switch (ins->opcode) {
1820 case OP_IDIV_UN_IMM:
1821 case OP_IREM_UN_IMM:
1823 * Keep the cases where we could generated optimized code, otherwise convert
1824 * to the non-imm variant.
1826 if ((ins->opcode == OP_IREM_IMM) && mono_is_power_of_two (ins->inst_imm) >= 0)
1828 mono_decompose_op_imm (cfg, bb, ins);
1835 bb->max_vreg = cfg->next_vreg;
1839 branch_cc_table [] = {
1840 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1841 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1842 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
1845 /* Maps CMP_... constants to X86_CC_... constants */
1848 X86_CC_EQ, X86_CC_NE, X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT,
1849 X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT
1853 cc_signed_table [] = {
1854 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
1855 FALSE, FALSE, FALSE, FALSE
1858 static unsigned char*
1859 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
1861 #define XMM_TEMP_REG 0
1862 /*This SSE2 optimization must not be done which OPT_SIMD in place as it clobbers xmm0.*/
1863 /*The xmm pass decomposes OP_FCONV_ ops anyway anyway.*/
1864 if (cfg->opt & MONO_OPT_SSE2 && size < 8 && !(cfg->opt & MONO_OPT_SIMD)) {
1865 /* optimize by assigning a local var for this use so we avoid
1866 * the stack manipulations */
1867 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1868 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1869 x86_movsd_reg_membase (code, XMM_TEMP_REG, X86_ESP, 0);
1870 x86_cvttsd2si (code, dreg, XMM_TEMP_REG);
1871 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1873 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
1875 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
1878 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
1879 x86_fnstcw_membase(code, X86_ESP, 0);
1880 x86_mov_reg_membase (code, dreg, X86_ESP, 0, 2);
1881 x86_alu_reg_imm (code, X86_OR, dreg, 0xc00);
1882 x86_mov_membase_reg (code, X86_ESP, 2, dreg, 2);
1883 x86_fldcw_membase (code, X86_ESP, 2);
1885 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1886 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
1887 x86_pop_reg (code, dreg);
1888 /* FIXME: need the high register
1889 * x86_pop_reg (code, dreg_high);
1892 x86_push_reg (code, X86_EAX); // SP = SP - 4
1893 x86_fist_pop_membase (code, X86_ESP, 0, FALSE);
1894 x86_pop_reg (code, dreg);
1896 x86_fldcw_membase (code, X86_ESP, 0);
1897 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
1900 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
1902 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
1906 static unsigned char*
1907 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
1909 int sreg = tree->sreg1;
1910 int need_touch = FALSE;
1912 #if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
1921 * If requested stack size is larger than one page,
1922 * perform stack-touch operation
1925 * Generate stack probe code.
1926 * Under Windows, it is necessary to allocate one page at a time,
1927 * "touching" stack after each successful sub-allocation. This is
1928 * because of the way stack growth is implemented - there is a
1929 * guard page before the lowest stack page that is currently commited.
1930 * Stack normally grows sequentially so OS traps access to the
1931 * guard page and commits more pages when needed.
1933 x86_test_reg_imm (code, sreg, ~0xFFF);
1934 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
1936 br[2] = code; /* loop */
1937 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
1938 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
1941 * By the end of the loop, sreg2 is smaller than 0x1000, so the init routine
1942 * that follows only initializes the last part of the area.
1944 /* Same as the init code below with size==0x1000 */
1945 if (tree->flags & MONO_INST_INIT) {
1946 x86_push_reg (code, X86_EAX);
1947 x86_push_reg (code, X86_ECX);
1948 x86_push_reg (code, X86_EDI);
1949 x86_mov_reg_imm (code, X86_ECX, (0x1000 >> 2));
1950 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
1951 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
1953 x86_prefix (code, X86_REP_PREFIX);
1955 x86_pop_reg (code, X86_EDI);
1956 x86_pop_reg (code, X86_ECX);
1957 x86_pop_reg (code, X86_EAX);
1960 x86_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
1961 x86_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
1962 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
1963 x86_patch (br[3], br[2]);
1964 x86_test_reg_reg (code, sreg, sreg);
1965 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
1966 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
1968 br[1] = code; x86_jump8 (code, 0);
1970 x86_patch (br[0], code);
1971 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
1972 x86_patch (br[1], code);
1973 x86_patch (br[4], code);
1976 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
1978 if (tree->flags & MONO_INST_INIT) {
1980 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
1981 x86_push_reg (code, X86_EAX);
1984 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
1985 x86_push_reg (code, X86_ECX);
1988 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
1989 x86_push_reg (code, X86_EDI);
1993 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
1994 if (sreg != X86_ECX)
1995 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
1996 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
1998 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
2000 x86_prefix (code, X86_REP_PREFIX);
2003 if (tree->dreg != X86_EDI && sreg != X86_EDI)
2004 x86_pop_reg (code, X86_EDI);
2005 if (tree->dreg != X86_ECX && sreg != X86_ECX)
2006 x86_pop_reg (code, X86_ECX);
2007 if (tree->dreg != X86_EAX && sreg != X86_EAX)
2008 x86_pop_reg (code, X86_EAX);
2015 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
2017 /* Move return value to the target register */
2018 switch (ins->opcode) {
2021 case OP_CALL_MEMBASE:
2022 if (ins->dreg != X86_EAX)
2023 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
2033 * mono_x86_emit_tls_get:
2034 * @code: buffer to store code to
2035 * @dreg: hard register where to place the result
2036 * @tls_offset: offset info
2038 * mono_x86_emit_tls_get emits in @code the native code that puts in
2039 * the dreg register the item in the thread local storage identified
2042 * Returns: a pointer to the end of the stored code
2045 mono_x86_emit_tls_get (guint8* code, int dreg, int tls_offset)
2049 * See the Under the Hood article in the May 1996 issue of Microsoft Systems
2050 * Journal and/or a disassembly of the TlsGet () function.
2052 g_assert (tls_offset < 64);
2053 x86_prefix (code, X86_FS_PREFIX);
2054 x86_mov_reg_mem (code, dreg, 0x18, 4);
2055 /* Dunno what this does but TlsGetValue () contains it */
2056 x86_alu_membase_imm (code, X86_AND, dreg, 0x34, 0);
2057 x86_mov_reg_membase (code, dreg, dreg, 3600 + (tls_offset * 4), 4);
2059 if (optimize_for_xen) {
2060 x86_prefix (code, X86_GS_PREFIX);
2061 x86_mov_reg_mem (code, dreg, 0, 4);
2062 x86_mov_reg_membase (code, dreg, dreg, tls_offset, 4);
2064 x86_prefix (code, X86_GS_PREFIX);
2065 x86_mov_reg_mem (code, dreg, tls_offset, 4);
2072 * emit_load_volatile_arguments:
2074 * Load volatile arguments from the stack to the original input registers.
2075 * Required before a tail call.
2078 emit_load_volatile_arguments (MonoCompile *cfg, guint8 *code)
2080 MonoMethod *method = cfg->method;
2081 MonoMethodSignature *sig;
2086 /* FIXME: Generate intermediate code instead */
2088 sig = mono_method_signature (method);
2090 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, FALSE);
2092 /* This is the opposite of the code in emit_prolog */
2094 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2095 ArgInfo *ainfo = cinfo->args + i;
2097 inst = cfg->args [i];
2099 if (sig->hasthis && (i == 0))
2100 arg_type = &mono_defaults.object_class->byval_arg;
2102 arg_type = sig->params [i - sig->hasthis];
2105 * On x86, the arguments are either in their original stack locations, or in
2108 if (inst->opcode == OP_REGVAR) {
2109 g_assert (ainfo->storage == ArgOnStack);
2111 x86_mov_membase_reg (code, X86_EBP, inst->inst_offset, inst->dreg, 4);
2118 #define REAL_PRINT_REG(text,reg) \
2119 mono_assert (reg >= 0); \
2120 x86_push_reg (code, X86_EAX); \
2121 x86_push_reg (code, X86_EDX); \
2122 x86_push_reg (code, X86_ECX); \
2123 x86_push_reg (code, reg); \
2124 x86_push_imm (code, reg); \
2125 x86_push_imm (code, text " %d %p\n"); \
2126 x86_mov_reg_imm (code, X86_EAX, printf); \
2127 x86_call_reg (code, X86_EAX); \
2128 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 3*4); \
2129 x86_pop_reg (code, X86_ECX); \
2130 x86_pop_reg (code, X86_EDX); \
2131 x86_pop_reg (code, X86_EAX);
2133 /* benchmark and set based on cpu */
2134 #define LOOP_ALIGNMENT 8
2135 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
2140 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2145 guint8 *code = cfg->native_code + cfg->code_len;
2148 if (cfg->opt & MONO_OPT_LOOP) {
2149 int pad, align = LOOP_ALIGNMENT;
2150 /* set alignment depending on cpu */
2151 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
2153 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
2154 x86_padding (code, pad);
2155 cfg->code_len += pad;
2156 bb->native_offset = cfg->code_len;
2160 if (cfg->verbose_level > 2)
2161 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2163 cpos = bb->max_offset;
2165 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2166 MonoProfileCoverageInfo *cov = cfg->coverage_info;
2167 g_assert (!cfg->compile_aot);
2170 cov->data [bb->dfn].cil_code = bb->cil_code;
2171 /* this is not thread save, but good enough */
2172 x86_inc_mem (code, &cov->data [bb->dfn].count);
2175 offset = code - cfg->native_code;
2177 mono_debug_open_block (cfg, bb, offset);
2179 MONO_BB_FOR_EACH_INS (bb, ins) {
2180 offset = code - cfg->native_code;
2182 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
2184 if (G_UNLIKELY (offset > (cfg->code_size - max_len - 16))) {
2185 cfg->code_size *= 2;
2186 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2187 code = cfg->native_code + offset;
2188 mono_jit_stats.code_reallocs++;
2191 if (cfg->debug_info)
2192 mono_debug_record_line_number (cfg, ins, offset);
2194 switch (ins->opcode) {
2196 x86_mul_reg (code, ins->sreg2, TRUE);
2199 x86_mul_reg (code, ins->sreg2, FALSE);
2201 case OP_X86_SETEQ_MEMBASE:
2202 case OP_X86_SETNE_MEMBASE:
2203 x86_set_membase (code, ins->opcode == OP_X86_SETEQ_MEMBASE ? X86_CC_EQ : X86_CC_NE,
2204 ins->inst_basereg, ins->inst_offset, TRUE);
2206 case OP_STOREI1_MEMBASE_IMM:
2207 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
2209 case OP_STOREI2_MEMBASE_IMM:
2210 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
2212 case OP_STORE_MEMBASE_IMM:
2213 case OP_STOREI4_MEMBASE_IMM:
2214 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
2216 case OP_STOREI1_MEMBASE_REG:
2217 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
2219 case OP_STOREI2_MEMBASE_REG:
2220 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
2222 case OP_STORE_MEMBASE_REG:
2223 case OP_STOREI4_MEMBASE_REG:
2224 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
2226 case OP_STORE_MEM_IMM:
2227 x86_mov_mem_imm (code, ins->inst_p0, ins->inst_c0, 4);
2230 x86_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
2234 /* These are created by the cprop pass so they use inst_imm as the source */
2235 x86_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
2238 x86_widen_mem (code, ins->dreg, ins->inst_imm, FALSE, FALSE);
2241 x86_widen_mem (code, ins->dreg, ins->inst_imm, FALSE, TRUE);
2243 case OP_LOAD_MEMBASE:
2244 case OP_LOADI4_MEMBASE:
2245 case OP_LOADU4_MEMBASE:
2246 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
2248 case OP_LOADU1_MEMBASE:
2249 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
2251 case OP_LOADI1_MEMBASE:
2252 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
2254 case OP_LOADU2_MEMBASE:
2255 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
2257 case OP_LOADI2_MEMBASE:
2258 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
2260 case OP_ICONV_TO_I1:
2262 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2264 case OP_ICONV_TO_I2:
2266 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2268 case OP_ICONV_TO_U1:
2269 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
2271 case OP_ICONV_TO_U2:
2272 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
2276 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
2278 case OP_COMPARE_IMM:
2279 case OP_ICOMPARE_IMM:
2280 x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
2282 case OP_X86_COMPARE_MEMBASE_REG:
2283 x86_alu_membase_reg (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2285 case OP_X86_COMPARE_MEMBASE_IMM:
2286 x86_alu_membase_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2288 case OP_X86_COMPARE_MEMBASE8_IMM:
2289 x86_alu_membase8_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2291 case OP_X86_COMPARE_REG_MEMBASE:
2292 x86_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
2294 case OP_X86_COMPARE_MEM_IMM:
2295 x86_alu_mem_imm (code, X86_CMP, ins->inst_offset, ins->inst_imm);
2297 case OP_X86_TEST_NULL:
2298 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2300 case OP_X86_ADD_MEMBASE_IMM:
2301 x86_alu_membase_imm (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2303 case OP_X86_ADD_REG_MEMBASE:
2304 x86_alu_reg_membase (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset);
2306 case OP_X86_SUB_MEMBASE_IMM:
2307 x86_alu_membase_imm (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2309 case OP_X86_SUB_REG_MEMBASE:
2310 x86_alu_reg_membase (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset);
2312 case OP_X86_AND_MEMBASE_IMM:
2313 x86_alu_membase_imm (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2315 case OP_X86_OR_MEMBASE_IMM:
2316 x86_alu_membase_imm (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2318 case OP_X86_XOR_MEMBASE_IMM:
2319 x86_alu_membase_imm (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2321 case OP_X86_ADD_MEMBASE_REG:
2322 x86_alu_membase_reg (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2324 case OP_X86_SUB_MEMBASE_REG:
2325 x86_alu_membase_reg (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2327 case OP_X86_AND_MEMBASE_REG:
2328 x86_alu_membase_reg (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2330 case OP_X86_OR_MEMBASE_REG:
2331 x86_alu_membase_reg (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2333 case OP_X86_XOR_MEMBASE_REG:
2334 x86_alu_membase_reg (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2336 case OP_X86_INC_MEMBASE:
2337 x86_inc_membase (code, ins->inst_basereg, ins->inst_offset);
2339 case OP_X86_INC_REG:
2340 x86_inc_reg (code, ins->dreg);
2342 case OP_X86_DEC_MEMBASE:
2343 x86_dec_membase (code, ins->inst_basereg, ins->inst_offset);
2345 case OP_X86_DEC_REG:
2346 x86_dec_reg (code, ins->dreg);
2348 case OP_X86_MUL_REG_MEMBASE:
2349 x86_imul_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
2351 case OP_X86_AND_REG_MEMBASE:
2352 x86_alu_reg_membase (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset);
2354 case OP_X86_OR_REG_MEMBASE:
2355 x86_alu_reg_membase (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset);
2357 case OP_X86_XOR_REG_MEMBASE:
2358 x86_alu_reg_membase (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset);
2361 x86_breakpoint (code);
2363 case OP_RELAXED_NOP:
2364 x86_prefix (code, X86_REP_PREFIX);
2372 case OP_DUMMY_STORE:
2373 case OP_NOT_REACHED:
2376 case OP_SEQ_POINT: {
2379 if (cfg->compile_aot)
2383 * Read from the single stepping trigger page. This will cause a
2384 * SIGSEGV when single stepping is enabled.
2385 * We do this _before_ the breakpoint, so single stepping after
2386 * a breakpoint is hit will step to the next IL offset.
2388 if (ins->flags & MONO_INST_SINGLE_STEP_LOC)
2389 x86_alu_reg_mem (code, X86_CMP, X86_EAX, (guint32)ss_trigger_page);
2391 il_offset = ins->inst_imm;
2393 if (!cfg->seq_points)
2394 cfg->seq_points = g_ptr_array_new ();
2395 g_ptr_array_add (cfg->seq_points, GUINT_TO_POINTER (il_offset));
2396 g_ptr_array_add (cfg->seq_points, GUINT_TO_POINTER (code - cfg->native_code));
2398 * A placeholder for a possible breakpoint inserted by
2399 * mono_arch_set_breakpoint ().
2401 for (i = 0; i < 6; ++i)
2408 x86_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
2412 x86_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
2417 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
2421 x86_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
2426 x86_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
2430 x86_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
2435 x86_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
2439 x86_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
2442 x86_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
2446 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
2451 * The code is the same for div/rem, the allocator will allocate dreg
2452 * to RAX/RDX as appropriate.
2454 if (ins->sreg2 == X86_EDX) {
2455 /* cdq clobbers this */
2456 x86_push_reg (code, ins->sreg2);
2458 x86_div_membase (code, X86_ESP, 0, TRUE);
2459 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2462 x86_div_reg (code, ins->sreg2, TRUE);
2467 if (ins->sreg2 == X86_EDX) {
2468 x86_push_reg (code, ins->sreg2);
2469 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2470 x86_div_membase (code, X86_ESP, 0, FALSE);
2471 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2473 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2474 x86_div_reg (code, ins->sreg2, FALSE);
2478 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2480 x86_div_reg (code, ins->sreg2, TRUE);
2483 int power = mono_is_power_of_two (ins->inst_imm);
2485 g_assert (ins->sreg1 == X86_EAX);
2486 g_assert (ins->dreg == X86_EAX);
2487 g_assert (power >= 0);
2490 /* Based on http://compilers.iecc.com/comparch/article/93-04-079 */
2492 x86_alu_reg_imm (code, X86_AND, X86_EAX, 1);
2494 * If the divident is >= 0, this does not nothing. If it is positive, it
2495 * it transforms %eax=0 into %eax=0, and %eax=1 into %eax=-1.
2497 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EDX);
2498 x86_alu_reg_reg (code, X86_SUB, X86_EAX, X86_EDX);
2499 } else if (power == 0) {
2500 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2502 /* Based on gcc code */
2504 /* Add compensation for negative dividents */
2506 x86_shift_reg_imm (code, X86_SHR, X86_EDX, 32 - power);
2507 x86_alu_reg_reg (code, X86_ADD, X86_EAX, X86_EDX);
2508 /* Compute remainder */
2509 x86_alu_reg_imm (code, X86_AND, X86_EAX, (1 << power) - 1);
2510 /* Remove compensation */
2511 x86_alu_reg_reg (code, X86_SUB, X86_EAX, X86_EDX);
2516 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
2520 x86_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
2523 x86_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
2527 x86_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
2530 g_assert (ins->sreg2 == X86_ECX);
2531 x86_shift_reg (code, X86_SHL, ins->dreg);
2534 g_assert (ins->sreg2 == X86_ECX);
2535 x86_shift_reg (code, X86_SAR, ins->dreg);
2539 x86_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
2542 case OP_ISHR_UN_IMM:
2543 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
2546 g_assert (ins->sreg2 == X86_ECX);
2547 x86_shift_reg (code, X86_SHR, ins->dreg);
2551 x86_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
2554 guint8 *jump_to_end;
2556 /* handle shifts below 32 bits */
2557 x86_shld_reg (code, ins->backend.reg3, ins->sreg1);
2558 x86_shift_reg (code, X86_SHL, ins->sreg1);
2560 x86_test_reg_imm (code, X86_ECX, 32);
2561 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
2563 /* handle shift over 32 bit */
2564 x86_mov_reg_reg (code, ins->backend.reg3, ins->sreg1, 4);
2565 x86_clear_reg (code, ins->sreg1);
2567 x86_patch (jump_to_end, code);
2571 guint8 *jump_to_end;
2573 /* handle shifts below 32 bits */
2574 x86_shrd_reg (code, ins->sreg1, ins->backend.reg3);
2575 x86_shift_reg (code, X86_SAR, ins->backend.reg3);
2577 x86_test_reg_imm (code, X86_ECX, 32);
2578 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
2580 /* handle shifts over 31 bits */
2581 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2582 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, 31);
2584 x86_patch (jump_to_end, code);
2588 guint8 *jump_to_end;
2590 /* handle shifts below 32 bits */
2591 x86_shrd_reg (code, ins->sreg1, ins->backend.reg3);
2592 x86_shift_reg (code, X86_SHR, ins->backend.reg3);
2594 x86_test_reg_imm (code, X86_ECX, 32);
2595 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
2597 /* handle shifts over 31 bits */
2598 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2599 x86_clear_reg (code, ins->backend.reg3);
2601 x86_patch (jump_to_end, code);
2605 if (ins->inst_imm >= 32) {
2606 x86_mov_reg_reg (code, ins->backend.reg3, ins->sreg1, 4);
2607 x86_clear_reg (code, ins->sreg1);
2608 x86_shift_reg_imm (code, X86_SHL, ins->backend.reg3, ins->inst_imm - 32);
2610 x86_shld_reg_imm (code, ins->backend.reg3, ins->sreg1, ins->inst_imm);
2611 x86_shift_reg_imm (code, X86_SHL, ins->sreg1, ins->inst_imm);
2615 if (ins->inst_imm >= 32) {
2616 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2617 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, 0x1f);
2618 x86_shift_reg_imm (code, X86_SAR, ins->sreg1, ins->inst_imm - 32);
2620 x86_shrd_reg_imm (code, ins->sreg1, ins->backend.reg3, ins->inst_imm);
2621 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, ins->inst_imm);
2624 case OP_LSHR_UN_IMM:
2625 if (ins->inst_imm >= 32) {
2626 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2627 x86_clear_reg (code, ins->backend.reg3);
2628 x86_shift_reg_imm (code, X86_SHR, ins->sreg1, ins->inst_imm - 32);
2630 x86_shrd_reg_imm (code, ins->sreg1, ins->backend.reg3, ins->inst_imm);
2631 x86_shift_reg_imm (code, X86_SHR, ins->backend.reg3, ins->inst_imm);
2635 x86_not_reg (code, ins->sreg1);
2638 x86_neg_reg (code, ins->sreg1);
2642 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2646 switch (ins->inst_imm) {
2650 if (ins->dreg != ins->sreg1)
2651 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2652 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
2655 /* LEA r1, [r2 + r2*2] */
2656 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
2659 /* LEA r1, [r2 + r2*4] */
2660 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2663 /* LEA r1, [r2 + r2*2] */
2665 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
2666 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
2669 /* LEA r1, [r2 + r2*8] */
2670 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
2673 /* LEA r1, [r2 + r2*4] */
2675 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2676 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
2679 /* LEA r1, [r2 + r2*2] */
2681 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
2682 x86_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
2685 /* LEA r1, [r2 + r2*4] */
2686 /* LEA r1, [r1 + r1*4] */
2687 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2688 x86_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
2691 /* LEA r1, [r2 + r2*4] */
2693 /* LEA r1, [r1 + r1*4] */
2694 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2695 x86_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
2696 x86_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
2699 x86_imul_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_imm);
2704 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2705 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2707 case OP_IMUL_OVF_UN: {
2708 /* the mul operation and the exception check should most likely be split */
2709 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
2710 /*g_assert (ins->sreg2 == X86_EAX);
2711 g_assert (ins->dreg == X86_EAX);*/
2712 if (ins->sreg2 == X86_EAX) {
2713 non_eax_reg = ins->sreg1;
2714 } else if (ins->sreg1 == X86_EAX) {
2715 non_eax_reg = ins->sreg2;
2717 /* no need to save since we're going to store to it anyway */
2718 if (ins->dreg != X86_EAX) {
2720 x86_push_reg (code, X86_EAX);
2722 x86_mov_reg_reg (code, X86_EAX, ins->sreg1, 4);
2723 non_eax_reg = ins->sreg2;
2725 if (ins->dreg == X86_EDX) {
2728 x86_push_reg (code, X86_EAX);
2730 } else if (ins->dreg != X86_EAX) {
2732 x86_push_reg (code, X86_EDX);
2734 x86_mul_reg (code, non_eax_reg, FALSE);
2735 /* save before the check since pop and mov don't change the flags */
2736 if (ins->dreg != X86_EAX)
2737 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
2739 x86_pop_reg (code, X86_EDX);
2741 x86_pop_reg (code, X86_EAX);
2742 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2746 x86_mov_reg_imm (code, ins->dreg, ins->inst_c0);
2749 g_assert_not_reached ();
2750 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2751 x86_mov_reg_imm (code, ins->dreg, 0);
2754 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2755 x86_mov_reg_imm (code, ins->dreg, 0);
2757 case OP_LOAD_GOTADDR:
2758 x86_call_imm (code, 0);
2760 * The patch needs to point to the pop, since the GOT offset needs
2761 * to be added to that address.
2763 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GOT_OFFSET, NULL);
2764 x86_pop_reg (code, ins->dreg);
2765 x86_alu_reg_imm (code, X86_ADD, ins->dreg, 0xf0f0f0f0);
2768 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
2769 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, 0xf0f0f0f0, 4);
2771 case OP_X86_PUSH_GOT_ENTRY:
2772 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
2773 x86_push_membase (code, ins->inst_basereg, 0xf0f0f0f0);
2776 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2780 * Note: this 'frame destruction' logic is useful for tail calls, too.
2781 * Keep in sync with the code in emit_epilog.
2785 /* FIXME: no tracing support... */
2786 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
2787 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
2788 /* reset offset to make max_len work */
2789 offset = code - cfg->native_code;
2791 g_assert (!cfg->method->save_lmf);
2793 code = emit_load_volatile_arguments (cfg, code);
2795 if (cfg->used_int_regs & (1 << X86_EBX))
2797 if (cfg->used_int_regs & (1 << X86_EDI))
2799 if (cfg->used_int_regs & (1 << X86_ESI))
2802 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
2804 if (cfg->used_int_regs & (1 << X86_ESI))
2805 x86_pop_reg (code, X86_ESI);
2806 if (cfg->used_int_regs & (1 << X86_EDI))
2807 x86_pop_reg (code, X86_EDI);
2808 if (cfg->used_int_regs & (1 << X86_EBX))
2809 x86_pop_reg (code, X86_EBX);
2811 /* restore ESP/EBP */
2813 offset = code - cfg->native_code;
2814 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2815 x86_jump32 (code, 0);
2817 cfg->disable_aot = TRUE;
2821 /* ensure ins->sreg1 is not NULL
2822 * note that cmp DWORD PTR [eax], eax is one byte shorter than
2823 * cmp DWORD PTR [eax], 0
2825 x86_alu_membase_reg (code, X86_CMP, ins->sreg1, 0, ins->sreg1);
2828 int hreg = ins->sreg1 == X86_EAX? X86_ECX: X86_EAX;
2829 x86_push_reg (code, hreg);
2830 x86_lea_membase (code, hreg, X86_EBP, cfg->sig_cookie);
2831 x86_mov_membase_reg (code, ins->sreg1, 0, hreg, 4);
2832 x86_pop_reg (code, hreg);
2841 call = (MonoCallInst*)ins;
2842 if (ins->flags & MONO_INST_HAS_METHOD)
2843 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method);
2845 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr);
2846 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
2847 /* a pop is one byte, while an add reg, imm is 3. So if there are 4 or 8
2848 * bytes to pop, we want to use pops. GCC does this (note it won't happen
2849 * for P4 or i686 because gcc will avoid using pop push at all. But we aren't
2850 * smart enough to do that optimization yet
2852 * It turns out that on my P4, doing two pops for 8 bytes on the stack makes
2853 * mcs botstrap slow down. However, doing 1 pop for 4 bytes creates a small,
2854 * (most likely from locality benefits). People with other processors should
2855 * check on theirs to see what happens.
2857 if (call->stack_usage == 4) {
2858 /* we want to use registers that won't get used soon, so use
2859 * ecx, as eax will get allocated first. edx is used by long calls,
2860 * so we can't use that.
2863 x86_pop_reg (code, X86_ECX);
2865 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2868 code = emit_move_return_value (cfg, ins, code);
2874 case OP_VOIDCALL_REG:
2876 call = (MonoCallInst*)ins;
2877 x86_call_reg (code, ins->sreg1);
2878 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
2879 if (call->stack_usage == 4)
2880 x86_pop_reg (code, X86_ECX);
2882 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2884 code = emit_move_return_value (cfg, ins, code);
2886 case OP_FCALL_MEMBASE:
2887 case OP_LCALL_MEMBASE:
2888 case OP_VCALL_MEMBASE:
2889 case OP_VCALL2_MEMBASE:
2890 case OP_VOIDCALL_MEMBASE:
2891 case OP_CALL_MEMBASE:
2892 call = (MonoCallInst*)ins;
2895 * Emit a few nops to simplify get_vcall_slot ().
2901 x86_call_membase (code, ins->sreg1, ins->inst_offset);
2902 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
2903 if (call->stack_usage == 4)
2904 x86_pop_reg (code, X86_ECX);
2906 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2908 code = emit_move_return_value (cfg, ins, code);
2911 x86_push_reg (code, ins->sreg1);
2913 case OP_X86_PUSH_IMM:
2914 x86_push_imm (code, ins->inst_imm);
2916 case OP_X86_PUSH_MEMBASE:
2917 x86_push_membase (code, ins->inst_basereg, ins->inst_offset);
2919 case OP_X86_PUSH_OBJ:
2920 x86_alu_reg_imm (code, X86_SUB, X86_ESP, ins->inst_imm);
2921 x86_push_reg (code, X86_EDI);
2922 x86_push_reg (code, X86_ESI);
2923 x86_push_reg (code, X86_ECX);
2924 if (ins->inst_offset)
2925 x86_lea_membase (code, X86_ESI, ins->inst_basereg, ins->inst_offset);
2927 x86_mov_reg_reg (code, X86_ESI, ins->inst_basereg, 4);
2928 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
2929 x86_mov_reg_imm (code, X86_ECX, (ins->inst_imm >> 2));
2931 x86_prefix (code, X86_REP_PREFIX);
2933 x86_pop_reg (code, X86_ECX);
2934 x86_pop_reg (code, X86_ESI);
2935 x86_pop_reg (code, X86_EDI);
2938 x86_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
2940 case OP_X86_LEA_MEMBASE:
2941 x86_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
2944 x86_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
2947 /* keep alignment */
2948 x86_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_LOCALLOC_ALIGNMENT - 1);
2949 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_LOCALLOC_ALIGNMENT - 1));
2950 code = mono_emit_stack_alloc (code, ins);
2951 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
2953 case OP_LOCALLOC_IMM: {
2954 guint32 size = ins->inst_imm;
2955 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
2957 if (ins->flags & MONO_INST_INIT) {
2958 /* FIXME: Optimize this */
2959 x86_mov_reg_imm (code, ins->dreg, size);
2960 ins->sreg1 = ins->dreg;
2962 code = mono_emit_stack_alloc (code, ins);
2963 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
2965 x86_alu_reg_imm (code, X86_SUB, X86_ESP, size);
2966 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
2971 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
2972 x86_push_reg (code, ins->sreg1);
2973 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
2974 (gpointer)"mono_arch_throw_exception");
2978 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
2979 x86_push_reg (code, ins->sreg1);
2980 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
2981 (gpointer)"mono_arch_rethrow_exception");
2984 case OP_CALL_HANDLER:
2985 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
2986 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2987 x86_call_imm (code, 0);
2988 x86_alu_reg_imm (code, X86_ADD, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
2990 case OP_START_HANDLER: {
2991 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
2992 x86_mov_membase_reg (code, spvar->inst_basereg, spvar->inst_offset, X86_ESP, 4);
2995 case OP_ENDFINALLY: {
2996 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
2997 x86_mov_reg_membase (code, X86_ESP, spvar->inst_basereg, spvar->inst_offset, 4);
3001 case OP_ENDFILTER: {
3002 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3003 x86_mov_reg_membase (code, X86_ESP, spvar->inst_basereg, spvar->inst_offset, 4);
3004 /* The local allocator will put the result into EAX */
3010 ins->inst_c0 = code - cfg->native_code;
3013 if (ins->inst_target_bb->native_offset) {
3014 x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
3016 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3017 if ((cfg->opt & MONO_OPT_BRANCH) &&
3018 x86_is_imm8 (ins->inst_target_bb->max_offset - cpos))
3019 x86_jump8 (code, 0);
3021 x86_jump32 (code, 0);
3025 x86_jump_reg (code, ins->sreg1);
3038 x86_set_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
3039 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3041 case OP_COND_EXC_EQ:
3042 case OP_COND_EXC_NE_UN:
3043 case OP_COND_EXC_LT:
3044 case OP_COND_EXC_LT_UN:
3045 case OP_COND_EXC_GT:
3046 case OP_COND_EXC_GT_UN:
3047 case OP_COND_EXC_GE:
3048 case OP_COND_EXC_GE_UN:
3049 case OP_COND_EXC_LE:
3050 case OP_COND_EXC_LE_UN:
3051 case OP_COND_EXC_IEQ:
3052 case OP_COND_EXC_INE_UN:
3053 case OP_COND_EXC_ILT:
3054 case OP_COND_EXC_ILT_UN:
3055 case OP_COND_EXC_IGT:
3056 case OP_COND_EXC_IGT_UN:
3057 case OP_COND_EXC_IGE:
3058 case OP_COND_EXC_IGE_UN:
3059 case OP_COND_EXC_ILE:
3060 case OP_COND_EXC_ILE_UN:
3061 EMIT_COND_SYSTEM_EXCEPTION (cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)], ins->inst_p1);
3063 case OP_COND_EXC_OV:
3064 case OP_COND_EXC_NO:
3066 case OP_COND_EXC_NC:
3067 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ], (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
3069 case OP_COND_EXC_IOV:
3070 case OP_COND_EXC_INO:
3071 case OP_COND_EXC_IC:
3072 case OP_COND_EXC_INC:
3073 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_IEQ], (ins->opcode < OP_COND_EXC_INE_UN), ins->inst_p1);
3085 EMIT_COND_BRANCH (ins, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
3093 case OP_CMOV_INE_UN:
3094 case OP_CMOV_IGE_UN:
3095 case OP_CMOV_IGT_UN:
3096 case OP_CMOV_ILE_UN:
3097 case OP_CMOV_ILT_UN:
3098 g_assert (ins->dreg == ins->sreg1);
3099 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);
3102 /* floating point opcodes */
3104 double d = *(double *)ins->inst_p0;
3106 if ((d == 0.0) && (mono_signbit (d) == 0)) {
3108 } else if (d == 1.0) {
3111 if (cfg->compile_aot) {
3112 guint32 *val = (guint32*)&d;
3113 x86_push_imm (code, val [1]);
3114 x86_push_imm (code, val [0]);
3115 x86_fld_membase (code, X86_ESP, 0, TRUE);
3116 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3119 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R8, ins->inst_p0);
3120 x86_fld (code, NULL, TRUE);
3126 float f = *(float *)ins->inst_p0;
3128 if ((f == 0.0) && (mono_signbit (f) == 0)) {
3130 } else if (f == 1.0) {
3133 if (cfg->compile_aot) {
3134 guint32 val = *(guint32*)&f;
3135 x86_push_imm (code, val);
3136 x86_fld_membase (code, X86_ESP, 0, FALSE);
3137 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3140 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R4, ins->inst_p0);
3141 x86_fld (code, NULL, FALSE);
3146 case OP_STORER8_MEMBASE_REG:
3147 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, TRUE, TRUE);
3149 case OP_LOADR8_MEMBASE:
3150 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
3152 case OP_STORER4_MEMBASE_REG:
3153 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, FALSE, TRUE);
3155 case OP_LOADR4_MEMBASE:
3156 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
3158 case OP_ICONV_TO_R4:
3159 x86_push_reg (code, ins->sreg1);
3160 x86_fild_membase (code, X86_ESP, 0, FALSE);
3161 /* Change precision */
3162 x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
3163 x86_fld_membase (code, X86_ESP, 0, FALSE);
3164 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3166 case OP_ICONV_TO_R8:
3167 x86_push_reg (code, ins->sreg1);
3168 x86_fild_membase (code, X86_ESP, 0, FALSE);
3169 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3171 case OP_ICONV_TO_R_UN:
3172 x86_push_imm (code, 0);
3173 x86_push_reg (code, ins->sreg1);
3174 x86_fild_membase (code, X86_ESP, 0, TRUE);
3175 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3177 case OP_X86_FP_LOAD_I8:
3178 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
3180 case OP_X86_FP_LOAD_I4:
3181 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
3183 case OP_FCONV_TO_R4:
3184 /* Change precision */
3185 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
3186 x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
3187 x86_fld_membase (code, X86_ESP, 0, FALSE);
3188 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3190 case OP_FCONV_TO_I1:
3191 code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
3193 case OP_FCONV_TO_U1:
3194 code = emit_float_to_int (cfg, code, ins->dreg, 1, FALSE);
3196 case OP_FCONV_TO_I2:
3197 code = emit_float_to_int (cfg, code, ins->dreg, 2, TRUE);
3199 case OP_FCONV_TO_U2:
3200 code = emit_float_to_int (cfg, code, ins->dreg, 2, FALSE);
3202 case OP_FCONV_TO_I4:
3204 code = emit_float_to_int (cfg, code, ins->dreg, 4, TRUE);
3206 case OP_FCONV_TO_I8:
3207 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
3208 x86_fnstcw_membase(code, X86_ESP, 0);
3209 x86_mov_reg_membase (code, ins->dreg, X86_ESP, 0, 2);
3210 x86_alu_reg_imm (code, X86_OR, ins->dreg, 0xc00);
3211 x86_mov_membase_reg (code, X86_ESP, 2, ins->dreg, 2);
3212 x86_fldcw_membase (code, X86_ESP, 2);
3213 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
3214 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
3215 x86_pop_reg (code, ins->dreg);
3216 x86_pop_reg (code, ins->backend.reg3);
3217 x86_fldcw_membase (code, X86_ESP, 0);
3218 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3220 case OP_LCONV_TO_R8_2:
3221 x86_push_reg (code, ins->sreg2);
3222 x86_push_reg (code, ins->sreg1);
3223 x86_fild_membase (code, X86_ESP, 0, TRUE);
3224 /* Change precision */
3225 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
3226 x86_fld_membase (code, X86_ESP, 0, TRUE);
3227 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3229 case OP_LCONV_TO_R4_2:
3230 x86_push_reg (code, ins->sreg2);
3231 x86_push_reg (code, ins->sreg1);
3232 x86_fild_membase (code, X86_ESP, 0, TRUE);
3233 /* Change precision */
3234 x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
3235 x86_fld_membase (code, X86_ESP, 0, FALSE);
3236 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3238 case OP_LCONV_TO_R_UN_2: {
3239 static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
3242 /* load 64bit integer to FP stack */
3243 x86_push_reg (code, ins->sreg2);
3244 x86_push_reg (code, ins->sreg1);
3245 x86_fild_membase (code, X86_ESP, 0, TRUE);
3247 /* test if lreg is negative */
3248 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
3249 br = code; x86_branch8 (code, X86_CC_GEZ, 0, TRUE);
3251 /* add correction constant mn */
3252 x86_fld80_mem (code, mn);
3253 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3255 x86_patch (br, code);
3257 /* Change precision */
3258 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
3259 x86_fld_membase (code, X86_ESP, 0, TRUE);
3261 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3265 case OP_LCONV_TO_OVF_I:
3266 case OP_LCONV_TO_OVF_I4_2: {
3267 guint8 *br [3], *label [1];
3271 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
3273 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
3275 /* If the low word top bit is set, see if we are negative */
3276 br [0] = code; x86_branch8 (code, X86_CC_LT, 0, TRUE);
3277 /* We are not negative (no top bit set, check for our top word to be zero */
3278 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
3279 br [1] = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
3282 /* throw exception */
3283 tins = mono_branch_optimize_exception_target (cfg, bb, "OverflowException");
3285 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, tins->inst_true_bb);
3286 if ((cfg->opt & MONO_OPT_BRANCH) && x86_is_imm8 (tins->inst_true_bb->max_offset - cpos))
3287 x86_jump8 (code, 0);
3289 x86_jump32 (code, 0);
3291 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
3292 x86_jump32 (code, 0);
3296 x86_patch (br [0], code);
3297 /* our top bit is set, check that top word is 0xfffffff */
3298 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0xffffffff);
3300 x86_patch (br [1], code);
3301 /* nope, emit exception */
3302 br [2] = code; x86_branch8 (code, X86_CC_NE, 0, TRUE);
3303 x86_patch (br [2], label [0]);
3305 if (ins->dreg != ins->sreg1)
3306 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
3310 /* Not needed on the fp stack */
3313 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3316 x86_fp_op_reg (code, X86_FSUB, 1, TRUE);
3319 x86_fp_op_reg (code, X86_FMUL, 1, TRUE);
3322 x86_fp_op_reg (code, X86_FDIV, 1, TRUE);
3330 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3335 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3342 * it really doesn't make sense to inline all this code,
3343 * it's here just to show that things may not be as simple
3346 guchar *check_pos, *end_tan, *pop_jump;
3347 x86_push_reg (code, X86_EAX);
3350 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
3352 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3353 x86_fstp (code, 0); /* pop the 1.0 */
3355 x86_jump8 (code, 0);
3357 x86_fp_op (code, X86_FADD, 0);
3361 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
3363 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3366 x86_patch (pop_jump, code);
3367 x86_fstp (code, 0); /* pop the 1.0 */
3368 x86_patch (check_pos, code);
3369 x86_patch (end_tan, code);
3371 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3372 x86_pop_reg (code, X86_EAX);
3379 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3388 g_assert (cfg->opt & MONO_OPT_CMOV);
3389 g_assert (ins->dreg == ins->sreg1);
3390 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3391 x86_cmov_reg (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2);
3394 g_assert (cfg->opt & MONO_OPT_CMOV);
3395 g_assert (ins->dreg == ins->sreg1);
3396 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3397 x86_cmov_reg (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2);
3400 g_assert (cfg->opt & MONO_OPT_CMOV);
3401 g_assert (ins->dreg == ins->sreg1);
3402 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3403 x86_cmov_reg (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2);
3406 g_assert (cfg->opt & MONO_OPT_CMOV);
3407 g_assert (ins->dreg == ins->sreg1);
3408 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3409 x86_cmov_reg (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2);
3415 x86_fxch (code, ins->inst_imm);
3420 x86_push_reg (code, X86_EAX);
3421 /* we need to exchange ST(0) with ST(1) */
3424 /* this requires a loop, because fprem somtimes
3425 * returns a partial remainder */
3427 /* looks like MS is using fprem instead of the IEEE compatible fprem1 */
3428 /* x86_fprem1 (code); */
3431 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_C2);
3433 x86_branch8 (code, X86_CC_NE, l1 - l2, FALSE);
3438 x86_pop_reg (code, X86_EAX);
3442 if (cfg->opt & MONO_OPT_FCMOV) {
3443 x86_fcomip (code, 1);
3447 /* this overwrites EAX */
3448 EMIT_FPCOMPARE(code);
3449 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3452 if (cfg->opt & MONO_OPT_FCMOV) {
3453 /* zeroing the register at the start results in
3454 * shorter and faster code (we can also remove the widening op)
3456 guchar *unordered_check;
3457 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3458 x86_fcomip (code, 1);
3460 unordered_check = code;
3461 x86_branch8 (code, X86_CC_P, 0, FALSE);
3462 x86_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
3463 x86_patch (unordered_check, code);
3466 if (ins->dreg != X86_EAX)
3467 x86_push_reg (code, X86_EAX);
3469 EMIT_FPCOMPARE(code);
3470 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3471 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
3472 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3473 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3475 if (ins->dreg != X86_EAX)
3476 x86_pop_reg (code, X86_EAX);
3480 if (cfg->opt & MONO_OPT_FCMOV) {
3481 /* zeroing the register at the start results in
3482 * shorter and faster code (we can also remove the widening op)
3484 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3485 x86_fcomip (code, 1);
3487 if (ins->opcode == OP_FCLT_UN) {
3488 guchar *unordered_check = code;
3489 guchar *jump_to_end;
3490 x86_branch8 (code, X86_CC_P, 0, FALSE);
3491 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
3493 x86_jump8 (code, 0);
3494 x86_patch (unordered_check, code);
3495 x86_inc_reg (code, ins->dreg);
3496 x86_patch (jump_to_end, code);
3498 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
3502 if (ins->dreg != X86_EAX)
3503 x86_push_reg (code, X86_EAX);
3505 EMIT_FPCOMPARE(code);
3506 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3507 if (ins->opcode == OP_FCLT_UN) {
3508 guchar *is_not_zero_check, *end_jump;
3509 is_not_zero_check = code;
3510 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3512 x86_jump8 (code, 0);
3513 x86_patch (is_not_zero_check, code);
3514 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3516 x86_patch (end_jump, code);
3518 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3519 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3521 if (ins->dreg != X86_EAX)
3522 x86_pop_reg (code, X86_EAX);
3526 if (cfg->opt & MONO_OPT_FCMOV) {
3527 /* zeroing the register at the start results in
3528 * shorter and faster code (we can also remove the widening op)
3530 guchar *unordered_check;
3531 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3532 x86_fcomip (code, 1);
3534 if (ins->opcode == OP_FCGT) {
3535 unordered_check = code;
3536 x86_branch8 (code, X86_CC_P, 0, FALSE);
3537 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
3538 x86_patch (unordered_check, code);
3540 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
3544 if (ins->dreg != X86_EAX)
3545 x86_push_reg (code, X86_EAX);
3547 EMIT_FPCOMPARE(code);
3548 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3549 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3550 if (ins->opcode == OP_FCGT_UN) {
3551 guchar *is_not_zero_check, *end_jump;
3552 is_not_zero_check = code;
3553 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3555 x86_jump8 (code, 0);
3556 x86_patch (is_not_zero_check, code);
3557 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3559 x86_patch (end_jump, code);
3561 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3562 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3564 if (ins->dreg != X86_EAX)
3565 x86_pop_reg (code, X86_EAX);
3568 if (cfg->opt & MONO_OPT_FCMOV) {
3569 guchar *jump = code;
3570 x86_branch8 (code, X86_CC_P, 0, TRUE);
3571 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3572 x86_patch (jump, code);
3575 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
3576 EMIT_COND_BRANCH (ins, X86_CC_EQ, TRUE);
3579 /* Branch if C013 != 100 */
3580 if (cfg->opt & MONO_OPT_FCMOV) {
3581 /* branch if !ZF or (PF|CF) */
3582 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3583 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3584 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
3587 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
3588 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3591 if (cfg->opt & MONO_OPT_FCMOV) {
3592 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
3595 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3598 if (cfg->opt & MONO_OPT_FCMOV) {
3599 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3600 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
3603 if (ins->opcode == OP_FBLT_UN) {
3604 guchar *is_not_zero_check, *end_jump;
3605 is_not_zero_check = code;
3606 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3608 x86_jump8 (code, 0);
3609 x86_patch (is_not_zero_check, code);
3610 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3612 x86_patch (end_jump, code);
3614 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3618 if (cfg->opt & MONO_OPT_FCMOV) {
3619 if (ins->opcode == OP_FBGT) {
3622 /* skip branch if C1=1 */
3624 x86_branch8 (code, X86_CC_P, 0, FALSE);
3625 /* branch if (C0 | C3) = 1 */
3626 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
3627 x86_patch (br1, code);
3629 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
3633 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3634 if (ins->opcode == OP_FBGT_UN) {
3635 guchar *is_not_zero_check, *end_jump;
3636 is_not_zero_check = code;
3637 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3639 x86_jump8 (code, 0);
3640 x86_patch (is_not_zero_check, code);
3641 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3643 x86_patch (end_jump, code);
3645 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3648 /* Branch if C013 == 100 or 001 */
3649 if (cfg->opt & MONO_OPT_FCMOV) {
3652 /* skip branch if C1=1 */
3654 x86_branch8 (code, X86_CC_P, 0, FALSE);
3655 /* branch if (C0 | C3) = 1 */
3656 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
3657 x86_patch (br1, code);
3660 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3661 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3662 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
3663 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3666 /* Branch if C013 == 000 */
3667 if (cfg->opt & MONO_OPT_FCMOV) {
3668 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
3671 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3674 /* Branch if C013=000 or 100 */
3675 if (cfg->opt & MONO_OPT_FCMOV) {
3678 /* skip branch if C1=1 */
3680 x86_branch8 (code, X86_CC_P, 0, FALSE);
3681 /* branch if C0=0 */
3682 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
3683 x86_patch (br1, code);
3686 x86_alu_reg_imm (code, X86_AND, X86_EAX, (X86_FP_C0|X86_FP_C1));
3687 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0);
3688 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3691 /* Branch if C013 != 001 */
3692 if (cfg->opt & MONO_OPT_FCMOV) {
3693 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3694 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
3697 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3698 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3702 x86_push_reg (code, X86_EAX);
3705 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
3706 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3707 x86_pop_reg (code, X86_EAX);
3709 /* Have to clean up the fp stack before throwing the exception */
3711 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3714 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
3716 x86_patch (br1, code);
3720 code = mono_x86_emit_tls_get (code, ins->dreg, ins->inst_offset);
3723 case OP_MEMORY_BARRIER: {
3724 /* Not needed on x86 */
3727 case OP_ATOMIC_ADD_I4: {
3728 int dreg = ins->dreg;
3730 if (dreg == ins->inst_basereg) {
3731 x86_push_reg (code, ins->sreg2);
3735 if (dreg != ins->sreg2)
3736 x86_mov_reg_reg (code, ins->dreg, ins->sreg2, 4);
3738 x86_prefix (code, X86_LOCK_PREFIX);
3739 x86_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, 4);
3741 if (dreg != ins->dreg) {
3742 x86_mov_reg_reg (code, ins->dreg, dreg, 4);
3743 x86_pop_reg (code, dreg);
3748 case OP_ATOMIC_ADD_NEW_I4: {
3749 int dreg = ins->dreg;
3751 /* hack: limit in regalloc, dreg != sreg1 && dreg != sreg2 */
3752 if (ins->sreg2 == dreg) {
3753 if (dreg == X86_EBX) {
3755 if (ins->inst_basereg == X86_EDI)
3759 if (ins->inst_basereg == X86_EBX)
3762 } else if (ins->inst_basereg == dreg) {
3763 if (dreg == X86_EBX) {
3765 if (ins->sreg2 == X86_EDI)
3769 if (ins->sreg2 == X86_EBX)
3774 if (dreg != ins->dreg) {
3775 x86_push_reg (code, dreg);
3778 x86_mov_reg_reg (code, dreg, ins->sreg2, 4);
3779 x86_prefix (code, X86_LOCK_PREFIX);
3780 x86_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, 4);
3781 /* dreg contains the old value, add with sreg2 value */
3782 x86_alu_reg_reg (code, X86_ADD, dreg, ins->sreg2);
3784 if (ins->dreg != dreg) {
3785 x86_mov_reg_reg (code, ins->dreg, dreg, 4);
3786 x86_pop_reg (code, dreg);
3791 case OP_ATOMIC_EXCHANGE_I4: {
3793 int sreg2 = ins->sreg2;
3794 int breg = ins->inst_basereg;
3796 /* cmpxchg uses eax as comperand, need to make sure we can use it
3797 * hack to overcome limits in x86 reg allocator
3798 * (req: dreg == eax and sreg2 != eax and breg != eax)
3800 g_assert (ins->dreg == X86_EAX);
3802 /* We need the EAX reg for the cmpxchg */
3803 if (ins->sreg2 == X86_EAX) {
3804 sreg2 = (breg == X86_EDX) ? X86_EBX : X86_EDX;
3805 x86_push_reg (code, sreg2);
3806 x86_mov_reg_reg (code, sreg2, X86_EAX, 4);
3809 if (breg == X86_EAX) {
3810 breg = (sreg2 == X86_ESI) ? X86_EDI : X86_ESI;
3811 x86_push_reg (code, breg);
3812 x86_mov_reg_reg (code, breg, X86_EAX, 4);
3815 x86_mov_reg_membase (code, X86_EAX, breg, ins->inst_offset, 4);
3817 br [0] = code; x86_prefix (code, X86_LOCK_PREFIX);
3818 x86_cmpxchg_membase_reg (code, breg, ins->inst_offset, sreg2);
3819 br [1] = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
3820 x86_patch (br [1], br [0]);
3822 if (breg != ins->inst_basereg)
3823 x86_pop_reg (code, breg);
3825 if (ins->sreg2 != sreg2)
3826 x86_pop_reg (code, sreg2);
3830 case OP_ATOMIC_CAS_I4: {
3831 g_assert (ins->sreg3 == X86_EAX);
3832 g_assert (ins->sreg1 != X86_EAX);
3833 g_assert (ins->sreg1 != ins->sreg2);
3835 x86_prefix (code, X86_LOCK_PREFIX);
3836 x86_cmpxchg_membase_reg (code, ins->sreg1, ins->inst_offset, ins->sreg2);
3838 if (ins->dreg != X86_EAX)
3839 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
3842 #ifdef MONO_ARCH_SIMD_INTRINSICS
3844 x86_sse_alu_ps_reg_reg (code, X86_SSE_ADD, ins->sreg1, ins->sreg2);
3847 x86_sse_alu_ps_reg_reg (code, X86_SSE_DIV, ins->sreg1, ins->sreg2);
3850 x86_sse_alu_ps_reg_reg (code, X86_SSE_MUL, ins->sreg1, ins->sreg2);
3853 x86_sse_alu_ps_reg_reg (code, X86_SSE_SUB, ins->sreg1, ins->sreg2);
3856 x86_sse_alu_ps_reg_reg (code, X86_SSE_MAX, ins->sreg1, ins->sreg2);
3859 x86_sse_alu_ps_reg_reg (code, X86_SSE_MIN, ins->sreg1, ins->sreg2);
3862 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
3863 x86_sse_alu_ps_reg_reg_imm (code, X86_SSE_COMP, ins->sreg1, ins->sreg2, ins->inst_c0);
3866 x86_sse_alu_ps_reg_reg (code, X86_SSE_AND, ins->sreg1, ins->sreg2);
3869 x86_sse_alu_ps_reg_reg (code, X86_SSE_ANDN, ins->sreg1, ins->sreg2);
3872 x86_sse_alu_ps_reg_reg (code, X86_SSE_OR, ins->sreg1, ins->sreg2);
3875 x86_sse_alu_ps_reg_reg (code, X86_SSE_XOR, ins->sreg1, ins->sreg2);
3878 x86_sse_alu_ps_reg_reg (code, X86_SSE_SQRT, ins->dreg, ins->sreg1);
3881 x86_sse_alu_ps_reg_reg (code, X86_SSE_RSQRT, ins->dreg, ins->sreg1);
3884 x86_sse_alu_ps_reg_reg (code, X86_SSE_RCP, ins->dreg, ins->sreg1);
3887 x86_sse_alu_sd_reg_reg (code, X86_SSE_ADDSUB, ins->sreg1, ins->sreg2);
3890 x86_sse_alu_sd_reg_reg (code, X86_SSE_HADD, ins->sreg1, ins->sreg2);
3893 x86_sse_alu_sd_reg_reg (code, X86_SSE_HSUB, ins->sreg1, ins->sreg2);
3896 x86_sse_alu_ss_reg_reg (code, X86_SSE_MOVSHDUP, ins->dreg, ins->sreg1);
3899 x86_sse_alu_ss_reg_reg (code, X86_SSE_MOVSLDUP, ins->dreg, ins->sreg1);
3902 case OP_PSHUFLEW_HIGH:
3903 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
3904 x86_pshufw_reg_reg (code, ins->dreg, ins->sreg1, ins->inst_c0, 1);
3906 case OP_PSHUFLEW_LOW:
3907 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
3908 x86_pshufw_reg_reg (code, ins->dreg, ins->sreg1, ins->inst_c0, 0);
3911 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
3912 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->sreg1, ins->inst_c0);
3916 x86_sse_alu_pd_reg_reg (code, X86_SSE_ADD, ins->sreg1, ins->sreg2);
3919 x86_sse_alu_pd_reg_reg (code, X86_SSE_DIV, ins->sreg1, ins->sreg2);
3922 x86_sse_alu_pd_reg_reg (code, X86_SSE_MUL, ins->sreg1, ins->sreg2);
3925 x86_sse_alu_pd_reg_reg (code, X86_SSE_SUB, ins->sreg1, ins->sreg2);
3928 x86_sse_alu_pd_reg_reg (code, X86_SSE_MAX, ins->sreg1, ins->sreg2);
3931 x86_sse_alu_pd_reg_reg (code, X86_SSE_MIN, ins->sreg1, ins->sreg2);
3934 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
3935 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_COMP, ins->sreg1, ins->sreg2, ins->inst_c0);
3938 x86_sse_alu_pd_reg_reg (code, X86_SSE_AND, ins->sreg1, ins->sreg2);
3941 x86_sse_alu_pd_reg_reg (code, X86_SSE_ANDN, ins->sreg1, ins->sreg2);
3944 x86_sse_alu_pd_reg_reg (code, X86_SSE_OR, ins->sreg1, ins->sreg2);
3947 x86_sse_alu_pd_reg_reg (code, X86_SSE_XOR, ins->sreg1, ins->sreg2);
3950 x86_sse_alu_pd_reg_reg (code, X86_SSE_SQRT, ins->dreg, ins->sreg1);
3953 x86_sse_alu_pd_reg_reg (code, X86_SSE_ADDSUB, ins->sreg1, ins->sreg2);
3956 x86_sse_alu_pd_reg_reg (code, X86_SSE_HADD, ins->sreg1, ins->sreg2);
3959 x86_sse_alu_pd_reg_reg (code, X86_SSE_HSUB, ins->sreg1, ins->sreg2);
3962 x86_sse_alu_sd_reg_reg (code, X86_SSE_MOVDDUP, ins->dreg, ins->sreg1);
3965 case OP_EXTRACT_MASK:
3966 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMOVMSKB, ins->dreg, ins->sreg1);
3970 x86_sse_alu_pd_reg_reg (code, X86_SSE_PAND, ins->sreg1, ins->sreg2);
3973 x86_sse_alu_pd_reg_reg (code, X86_SSE_POR, ins->sreg1, ins->sreg2);
3976 x86_sse_alu_pd_reg_reg (code, X86_SSE_PXOR, ins->sreg1, ins->sreg2);
3980 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDB, ins->sreg1, ins->sreg2);
3983 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDW, ins->sreg1, ins->sreg2);
3986 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDD, ins->sreg1, ins->sreg2);
3989 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDQ, ins->sreg1, ins->sreg2);
3993 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBB, ins->sreg1, ins->sreg2);
3996 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBW, ins->sreg1, ins->sreg2);
3999 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBD, ins->sreg1, ins->sreg2);
4002 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBQ, ins->sreg1, ins->sreg2);
4006 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMAXUB, ins->sreg1, ins->sreg2);
4009 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXUW, ins->sreg1, ins->sreg2);
4012 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXUD, ins->sreg1, ins->sreg2);
4016 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXSB, ins->sreg1, ins->sreg2);
4019 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMAXSW, ins->sreg1, ins->sreg2);
4022 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXSD, ins->sreg1, ins->sreg2);
4026 x86_sse_alu_pd_reg_reg (code, X86_SSE_PAVGB, ins->sreg1, ins->sreg2);
4029 x86_sse_alu_pd_reg_reg (code, X86_SSE_PAVGW, ins->sreg1, ins->sreg2);
4033 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMINUB, ins->sreg1, ins->sreg2);
4036 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINUW, ins->sreg1, ins->sreg2);
4039 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINUD, ins->sreg1, ins->sreg2);
4043 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINSB, ins->sreg1, ins->sreg2);
4046 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMINSW, ins->sreg1, ins->sreg2);
4049 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINSD, ins->sreg1, ins->sreg2);
4053 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQB, ins->sreg1, ins->sreg2);
4056 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQW, ins->sreg1, ins->sreg2);
4059 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQD, ins->sreg1, ins->sreg2);
4062 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PCMPEQQ, ins->sreg1, ins->sreg2);
4066 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPGTB, ins->sreg1, ins->sreg2);
4069 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPGTW, ins->sreg1, ins->sreg2);
4072 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPGTD, ins->sreg1, ins->sreg2);
4075 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PCMPGTQ, ins->sreg1, ins->sreg2);
4078 case OP_PSUM_ABS_DIFF:
4079 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSADBW, ins->sreg1, ins->sreg2);
4082 case OP_UNPACK_LOWB:
4083 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLBW, ins->sreg1, ins->sreg2);
4085 case OP_UNPACK_LOWW:
4086 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLWD, ins->sreg1, ins->sreg2);
4088 case OP_UNPACK_LOWD:
4089 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLDQ, ins->sreg1, ins->sreg2);
4091 case OP_UNPACK_LOWQ:
4092 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLQDQ, ins->sreg1, ins->sreg2);
4094 case OP_UNPACK_LOWPS:
4095 x86_sse_alu_ps_reg_reg (code, X86_SSE_UNPCKL, ins->sreg1, ins->sreg2);
4097 case OP_UNPACK_LOWPD:
4098 x86_sse_alu_pd_reg_reg (code, X86_SSE_UNPCKL, ins->sreg1, ins->sreg2);
4101 case OP_UNPACK_HIGHB:
4102 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHBW, ins->sreg1, ins->sreg2);
4104 case OP_UNPACK_HIGHW:
4105 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHWD, ins->sreg1, ins->sreg2);
4107 case OP_UNPACK_HIGHD:
4108 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHDQ, ins->sreg1, ins->sreg2);
4110 case OP_UNPACK_HIGHQ:
4111 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHQDQ, ins->sreg1, ins->sreg2);
4113 case OP_UNPACK_HIGHPS:
4114 x86_sse_alu_ps_reg_reg (code, X86_SSE_UNPCKH, ins->sreg1, ins->sreg2);
4116 case OP_UNPACK_HIGHPD:
4117 x86_sse_alu_pd_reg_reg (code, X86_SSE_UNPCKH, ins->sreg1, ins->sreg2);
4121 x86_sse_alu_pd_reg_reg (code, X86_SSE_PACKSSWB, ins->sreg1, ins->sreg2);
4124 x86_sse_alu_pd_reg_reg (code, X86_SSE_PACKSSDW, ins->sreg1, ins->sreg2);
4127 x86_sse_alu_pd_reg_reg (code, X86_SSE_PACKUSWB, ins->sreg1, ins->sreg2);
4130 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PACKUSDW, ins->sreg1, ins->sreg2);
4133 case OP_PADDB_SAT_UN:
4134 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDUSB, ins->sreg1, ins->sreg2);
4136 case OP_PSUBB_SAT_UN:
4137 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBUSB, ins->sreg1, ins->sreg2);
4139 case OP_PADDW_SAT_UN:
4140 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDUSW, ins->sreg1, ins->sreg2);
4142 case OP_PSUBW_SAT_UN:
4143 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBUSW, ins->sreg1, ins->sreg2);
4147 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDSB, ins->sreg1, ins->sreg2);
4150 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBSB, ins->sreg1, ins->sreg2);
4153 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDSW, ins->sreg1, ins->sreg2);
4156 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBSW, ins->sreg1, ins->sreg2);
4160 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULLW, ins->sreg1, ins->sreg2);
4163 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMULLD, ins->sreg1, ins->sreg2);
4166 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULUDQ, ins->sreg1, ins->sreg2);
4168 case OP_PMULW_HIGH_UN:
4169 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULHUW, ins->sreg1, ins->sreg2);
4172 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULHW, ins->sreg1, ins->sreg2);
4176 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTW, X86_SSE_SHR, ins->dreg, ins->inst_imm);
4179 x86_sse_shift_reg_reg (code, X86_SSE_PSRLW_REG, ins->dreg, ins->sreg2);
4183 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTW, X86_SSE_SAR, ins->dreg, ins->inst_imm);
4186 x86_sse_shift_reg_reg (code, X86_SSE_PSRAW_REG, ins->dreg, ins->sreg2);
4190 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTW, X86_SSE_SHL, ins->dreg, ins->inst_imm);
4193 x86_sse_shift_reg_reg (code, X86_SSE_PSLLW_REG, ins->dreg, ins->sreg2);
4197 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTD, X86_SSE_SHR, ins->dreg, ins->inst_imm);
4200 x86_sse_shift_reg_reg (code, X86_SSE_PSRLD_REG, ins->dreg, ins->sreg2);
4204 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTD, X86_SSE_SAR, ins->dreg, ins->inst_imm);
4207 x86_sse_shift_reg_reg (code, X86_SSE_PSRAD_REG, ins->dreg, ins->sreg2);
4211 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTD, X86_SSE_SHL, ins->dreg, ins->inst_imm);
4214 x86_sse_shift_reg_reg (code, X86_SSE_PSLLD_REG, ins->dreg, ins->sreg2);
4218 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTQ, X86_SSE_SHR, ins->dreg, ins->inst_imm);
4221 x86_sse_shift_reg_reg (code, X86_SSE_PSRLQ_REG, ins->dreg, ins->sreg2);
4225 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTQ, X86_SSE_SHL, ins->dreg, ins->inst_imm);
4228 x86_sse_shift_reg_reg (code, X86_SSE_PSLLQ_REG, ins->dreg, ins->sreg2);
4232 x86_movd_xreg_reg (code, ins->dreg, ins->sreg1);
4235 x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
4239 x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
4241 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_c0 * 8);
4242 x86_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I1, FALSE);
4246 x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
4248 x86_shift_reg_imm (code, X86_SHR, ins->dreg, 16);
4249 x86_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I2, TRUE);
4253 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);
4255 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);
4256 x86_fld_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE);
4260 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->sreg1, ins->sreg2, ins->inst_c0);
4262 case OP_EXTRACTX_U2:
4263 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PEXTRW, ins->dreg, ins->sreg1, ins->inst_c0);
4265 case OP_INSERTX_U1_SLOW:
4266 /*sreg1 is the extracted ireg (scratch)
4267 /sreg2 is the to be inserted ireg (scratch)
4268 /dreg is the xreg to receive the value*/
4270 /*clear the bits from the extracted word*/
4271 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_c0 & 1 ? 0x00FF : 0xFF00);
4272 /*shift the value to insert if needed*/
4273 if (ins->inst_c0 & 1)
4274 x86_shift_reg_imm (code, X86_SHL, ins->sreg2, 8);
4275 /*join them together*/
4276 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
4277 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, ins->inst_c0 / 2);
4279 case OP_INSERTX_I4_SLOW:
4280 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg2, ins->inst_c0 * 2);
4281 x86_shift_reg_imm (code, X86_SHR, ins->sreg2, 16);
4282 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg2, ins->inst_c0 * 2 + 1);
4285 case OP_INSERTX_R4_SLOW:
4286 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE, TRUE);
4287 /*TODO if inst_c0 == 0 use movss*/
4288 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);
4289 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);
4291 case OP_INSERTX_R8_SLOW:
4292 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
4294 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);
4296 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);
4299 case OP_STOREX_MEMBASE_REG:
4300 case OP_STOREX_MEMBASE:
4301 x86_movups_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
4303 case OP_LOADX_MEMBASE:
4304 x86_movups_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
4306 case OP_LOADX_ALIGNED_MEMBASE:
4307 x86_movaps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
4309 case OP_STOREX_ALIGNED_MEMBASE_REG:
4310 x86_movaps_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
4312 case OP_STOREX_NTA_MEMBASE_REG:
4313 x86_sse_alu_reg_membase (code, X86_SSE_MOVNTPS, ins->dreg, ins->sreg1, ins->inst_offset);
4315 case OP_PREFETCH_MEMBASE:
4316 x86_sse_alu_reg_membase (code, X86_SSE_PREFETCH, ins->backend.arg_info, ins->sreg1, ins->inst_offset);
4320 /*FIXME the peephole pass should have killed this*/
4321 if (ins->dreg != ins->sreg1)
4322 x86_movaps_reg_reg (code, ins->dreg, ins->sreg1);
4325 x86_sse_alu_pd_reg_reg (code, X86_SSE_PXOR, ins->dreg, ins->dreg);
4327 case OP_ICONV_TO_R8_RAW:
4328 x86_mov_membase_reg (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, ins->sreg1, 4);
4329 x86_fld_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE);
4332 case OP_FCONV_TO_R8_X:
4333 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
4334 x86_movsd_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
4337 case OP_XCONV_R8_TO_I4:
4338 x86_cvttsd2si (code, ins->dreg, ins->sreg1);
4339 switch (ins->backend.source_opcode) {
4340 case OP_FCONV_TO_I1:
4341 x86_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
4343 case OP_FCONV_TO_U1:
4344 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
4346 case OP_FCONV_TO_I2:
4347 x86_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
4349 case OP_FCONV_TO_U2:
4350 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
4356 /*FIXME this causes a partial register stall, maybe it would not be that bad to use shift + mask + or*/
4357 /*The +4 is to get a mov ?h, ?l over the same reg.*/
4358 x86_mov_reg_reg (code, ins->dreg + 4, ins->dreg, 1);
4359 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 0);
4360 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 1);
4361 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
4364 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 0);
4365 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 1);
4366 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
4369 x86_movd_xreg_reg (code, ins->dreg, ins->sreg1);
4370 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
4373 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE, TRUE);
4374 x86_movd_xreg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
4375 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
4378 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
4379 x86_movsd_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
4380 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0x44);
4383 case OP_LIVERANGE_START: {
4384 if (cfg->verbose_level > 1)
4385 printf ("R%d START=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
4386 MONO_VARINFO (cfg, ins->inst_c0)->live_range_start = code - cfg->native_code;
4389 case OP_LIVERANGE_END: {
4390 if (cfg->verbose_level > 1)
4391 printf ("R%d END=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
4392 MONO_VARINFO (cfg, ins->inst_c0)->live_range_end = code - cfg->native_code;
4396 g_warning ("unknown opcode %s\n", mono_inst_name (ins->opcode));
4397 g_assert_not_reached ();
4400 if (G_UNLIKELY ((code - cfg->native_code - offset) > max_len)) {
4401 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
4402 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
4403 g_assert_not_reached ();
4409 cfg->code_len = code - cfg->native_code;
4412 #endif /* DISABLE_JIT */
4415 mono_arch_register_lowlevel_calls (void)
4420 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
4422 MonoJumpInfo *patch_info;
4423 gboolean compile_aot = !run_cctors;
4425 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
4426 unsigned char *ip = patch_info->ip.i + code;
4427 const unsigned char *target;
4429 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
4432 switch (patch_info->type) {
4433 case MONO_PATCH_INFO_BB:
4434 case MONO_PATCH_INFO_LABEL:
4437 /* No need to patch these */
4442 switch (patch_info->type) {
4443 case MONO_PATCH_INFO_IP:
4444 *((gconstpointer *)(ip)) = target;
4446 case MONO_PATCH_INFO_CLASS_INIT: {
4448 /* Might already been changed to a nop */
4449 x86_call_code (code, 0);
4450 x86_patch (ip, target);
4453 case MONO_PATCH_INFO_ABS:
4454 case MONO_PATCH_INFO_METHOD:
4455 case MONO_PATCH_INFO_METHOD_JUMP:
4456 case MONO_PATCH_INFO_INTERNAL_METHOD:
4457 case MONO_PATCH_INFO_BB:
4458 case MONO_PATCH_INFO_LABEL:
4459 case MONO_PATCH_INFO_RGCTX_FETCH:
4460 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
4461 case MONO_PATCH_INFO_MONITOR_ENTER:
4462 case MONO_PATCH_INFO_MONITOR_EXIT:
4463 x86_patch (ip, target);
4465 case MONO_PATCH_INFO_NONE:
4468 guint32 offset = mono_arch_get_patch_offset (ip);
4469 *((gconstpointer *)(ip + offset)) = target;
4477 mono_arch_emit_prolog (MonoCompile *cfg)
4479 MonoMethod *method = cfg->method;
4481 MonoMethodSignature *sig;
4483 int alloc_size, pos, max_offset, i, cfa_offset;
4485 gboolean need_stack_frame;
4487 cfg->code_size = MAX (mono_method_get_header (method)->code_size * 4, 10240);
4489 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
4490 cfg->code_size += 512;
4492 code = cfg->native_code = g_malloc (cfg->code_size);
4494 /* Offset between RSP and the CFA */
4498 cfa_offset = sizeof (gpointer);
4499 mono_emit_unwind_op_def_cfa (cfg, code, X86_ESP, sizeof (gpointer));
4500 // IP saved at CFA - 4
4501 /* There is no IP reg on x86 */
4502 mono_emit_unwind_op_offset (cfg, code, X86_NREG, -cfa_offset);
4504 need_stack_frame = needs_stack_frame (cfg);
4506 if (need_stack_frame) {
4507 x86_push_reg (code, X86_EBP);
4508 cfa_offset += sizeof (gpointer);
4509 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
4510 mono_emit_unwind_op_offset (cfg, code, X86_EBP, - cfa_offset);
4511 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
4512 mono_emit_unwind_op_def_cfa_reg (cfg, code, X86_EBP);
4515 alloc_size = cfg->stack_offset;
4518 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
4519 /* Might need to attach the thread to the JIT or change the domain for the callback */
4520 if (appdomain_tls_offset != -1 && lmf_tls_offset != -1) {
4521 guint8 *buf, *no_domain_branch;
4523 code = mono_x86_emit_tls_get (code, X86_EAX, appdomain_tls_offset);
4524 x86_alu_reg_imm (code, X86_CMP, X86_EAX, GPOINTER_TO_UINT (cfg->domain));
4525 no_domain_branch = code;
4526 x86_branch8 (code, X86_CC_NE, 0, 0);
4527 code = mono_x86_emit_tls_get ( code, X86_EAX, lmf_tls_offset);
4528 x86_test_reg_reg (code, X86_EAX, X86_EAX);
4530 x86_branch8 (code, X86_CC_NE, 0, 0);
4531 x86_patch (no_domain_branch, code);
4532 x86_push_imm (code, cfg->domain);
4533 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_jit_thread_attach");
4534 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
4535 x86_patch (buf, code);
4537 /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
4538 /* FIXME: Add a separate key for LMF to avoid this */
4539 x86_alu_reg_imm (code, X86_ADD, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
4543 g_assert (!cfg->compile_aot);
4544 x86_push_imm (code, cfg->domain);
4545 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_jit_thread_attach");
4546 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
4550 if (method->save_lmf) {
4551 pos += sizeof (MonoLMF);
4553 if (cfg->compile_aot)
4554 cfg->disable_aot = TRUE;
4556 /* save the current IP */
4557 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
4558 x86_push_imm_template (code);
4559 cfa_offset += sizeof (gpointer);
4561 /* save all caller saved regs */
4562 x86_push_reg (code, X86_EBP);
4563 cfa_offset += sizeof (gpointer);
4564 x86_push_reg (code, X86_ESI);
4565 cfa_offset += sizeof (gpointer);
4566 mono_emit_unwind_op_offset (cfg, code, X86_ESI, - cfa_offset);
4567 x86_push_reg (code, X86_EDI);
4568 cfa_offset += sizeof (gpointer);
4569 mono_emit_unwind_op_offset (cfg, code, X86_EDI, - cfa_offset);
4570 x86_push_reg (code, X86_EBX);
4571 cfa_offset += sizeof (gpointer);
4572 mono_emit_unwind_op_offset (cfg, code, X86_EBX, - cfa_offset);
4574 if ((lmf_tls_offset != -1) && !is_win32 && !optimize_for_xen) {
4576 * Optimized version which uses the mono_lmf TLS variable instead of indirection
4577 * through the mono_lmf_addr TLS variable.
4579 /* %eax = previous_lmf */
4580 x86_prefix (code, X86_GS_PREFIX);
4581 x86_mov_reg_mem (code, X86_EAX, lmf_tls_offset, 4);
4582 /* skip esp + method_info + lmf */
4583 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 12);
4584 /* push previous_lmf */
4585 x86_push_reg (code, X86_EAX);
4587 x86_prefix (code, X86_GS_PREFIX);
4588 x86_mov_mem_reg (code, lmf_tls_offset, X86_ESP, 4);
4590 /* get the address of lmf for the current thread */
4592 * This is performance critical so we try to use some tricks to make
4596 if (lmf_addr_tls_offset != -1) {
4597 /* Load lmf quicky using the GS register */
4598 code = mono_x86_emit_tls_get (code, X86_EAX, lmf_addr_tls_offset);
4600 /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
4601 /* FIXME: Add a separate key for LMF to avoid this */
4602 x86_alu_reg_imm (code, X86_ADD, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
4605 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_get_lmf_addr");
4608 /* Skip esp + method info */
4609 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
4612 x86_push_reg (code, X86_EAX);
4613 /* push *lfm (previous_lmf) */
4614 x86_push_membase (code, X86_EAX, 0);
4616 x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
4620 if (cfg->used_int_regs & (1 << X86_EBX)) {
4621 x86_push_reg (code, X86_EBX);
4623 cfa_offset += sizeof (gpointer);
4624 mono_emit_unwind_op_offset (cfg, code, X86_EBX, - cfa_offset);
4627 if (cfg->used_int_regs & (1 << X86_EDI)) {
4628 x86_push_reg (code, X86_EDI);
4630 cfa_offset += sizeof (gpointer);
4631 mono_emit_unwind_op_offset (cfg, code, X86_EDI, - cfa_offset);
4634 if (cfg->used_int_regs & (1 << X86_ESI)) {
4635 x86_push_reg (code, X86_ESI);
4637 cfa_offset += sizeof (gpointer);
4638 mono_emit_unwind_op_offset (cfg, code, X86_ESI, - cfa_offset);
4644 /* the original alloc_size is already aligned: there is %ebp and retip pushed, so realign */
4645 if (mono_do_x86_stack_align && need_stack_frame) {
4646 int tot = alloc_size + pos + 4; /* ret ip */
4647 if (need_stack_frame)
4649 tot &= MONO_ARCH_FRAME_ALIGNMENT - 1;
4651 alloc_size += MONO_ARCH_FRAME_ALIGNMENT - tot;
4655 /* See mono_emit_stack_alloc */
4656 #if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
4657 guint32 remaining_size = alloc_size;
4658 /*FIXME handle unbounded code expansion, we should use a loop in case of more than X interactions*/
4659 guint32 required_code_size = ((remaining_size / 0x1000) + 1) * 8; /*8 is the max size of x86_alu_reg_imm + x86_test_membase_reg*/
4660 guint32 offset = code - cfg->native_code;
4661 if (G_UNLIKELY (required_code_size >= (cfg->code_size - offset))) {
4662 while (required_code_size >= (cfg->code_size - offset))
4663 cfg->code_size *= 2;
4664 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4665 code = cfg->native_code + offset;
4666 mono_jit_stats.code_reallocs++;
4668 while (remaining_size >= 0x1000) {
4669 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
4670 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
4671 remaining_size -= 0x1000;
4674 x86_alu_reg_imm (code, X86_SUB, X86_ESP, remaining_size);
4676 x86_alu_reg_imm (code, X86_SUB, X86_ESP, alloc_size);
4679 g_assert (need_stack_frame);
4682 if (cfg->method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED ||
4683 cfg->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
4684 x86_alu_reg_imm (code, X86_AND, X86_ESP, -MONO_ARCH_FRAME_ALIGNMENT);
4687 #if DEBUG_STACK_ALIGNMENT
4688 /* check the stack is aligned */
4689 if (need_stack_frame && method->wrapper_type == MONO_WRAPPER_NONE) {
4690 x86_mov_reg_reg (code, X86_ECX, X86_ESP, 4);
4691 x86_alu_reg_imm (code, X86_AND, X86_ECX, MONO_ARCH_FRAME_ALIGNMENT - 1);
4692 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
4693 x86_branch_disp (code, X86_CC_EQ, 3, FALSE);
4694 x86_breakpoint (code);
4698 /* compute max_offset in order to use short forward jumps */
4700 if (cfg->opt & MONO_OPT_BRANCH) {
4701 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4703 bb->max_offset = max_offset;
4705 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
4707 /* max alignment for loops */
4708 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
4709 max_offset += LOOP_ALIGNMENT;
4711 MONO_BB_FOR_EACH_INS (bb, ins) {
4712 if (ins->opcode == OP_LABEL)
4713 ins->inst_c1 = max_offset;
4715 max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
4720 /* store runtime generic context */
4721 if (cfg->rgctx_var) {
4722 g_assert (cfg->rgctx_var->opcode == OP_REGOFFSET && cfg->rgctx_var->inst_basereg == X86_EBP);
4724 x86_mov_membase_reg (code, X86_EBP, cfg->rgctx_var->inst_offset, MONO_ARCH_RGCTX_REG, 4);
4727 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
4728 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
4730 /* load arguments allocated to register from the stack */
4731 sig = mono_method_signature (method);
4734 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
4735 inst = cfg->args [pos];
4736 if (inst->opcode == OP_REGVAR) {
4737 g_assert (need_stack_frame);
4738 x86_mov_reg_membase (code, inst->dreg, X86_EBP, inst->inst_offset, 4);
4739 if (cfg->verbose_level > 2)
4740 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
4745 cfg->code_len = code - cfg->native_code;
4747 g_assert (cfg->code_len < cfg->code_size);
4753 mono_arch_emit_epilog (MonoCompile *cfg)
4755 MonoMethod *method = cfg->method;
4756 MonoMethodSignature *sig = mono_method_signature (method);
4758 guint32 stack_to_pop;
4760 int max_epilog_size = 16;
4762 gboolean need_stack_frame = needs_stack_frame (cfg);
4764 if (cfg->method->save_lmf)
4765 max_epilog_size += 128;
4767 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
4768 cfg->code_size *= 2;
4769 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4770 mono_jit_stats.code_reallocs++;
4773 code = cfg->native_code + cfg->code_len;
4775 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
4776 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
4778 /* the code restoring the registers must be kept in sync with OP_JMP */
4781 if (method->save_lmf) {
4782 gint32 prev_lmf_reg;
4783 gint32 lmf_offset = -sizeof (MonoLMF);
4785 /* check if we need to restore protection of the stack after a stack overflow */
4786 if (mono_get_jit_tls_offset () != -1) {
4788 code = mono_x86_emit_tls_get (code, X86_ECX, mono_get_jit_tls_offset ());
4789 /* we load the value in a separate instruction: this mechanism may be
4790 * used later as a safer way to do thread interruption
4792 x86_mov_reg_membase (code, X86_ECX, X86_ECX, G_STRUCT_OFFSET (MonoJitTlsData, restore_stack_prot), 4);
4793 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
4795 x86_branch8 (code, X86_CC_Z, 0, FALSE);
4796 /* note that the call trampoline will preserve eax/edx */
4797 x86_call_reg (code, X86_ECX);
4798 x86_patch (patch, code);
4800 /* FIXME: maybe save the jit tls in the prolog */
4802 if ((lmf_tls_offset != -1) && !is_win32 && !optimize_for_xen) {
4804 * Optimized version which uses the mono_lmf TLS variable instead of indirection
4805 * through the mono_lmf_addr TLS variable.
4807 /* reg = previous_lmf */
4808 x86_mov_reg_membase (code, X86_ECX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 4);
4810 /* lmf = previous_lmf */
4811 x86_prefix (code, X86_GS_PREFIX);
4812 x86_mov_mem_reg (code, lmf_tls_offset, X86_ECX, 4);
4814 /* Find a spare register */
4815 switch (mini_type_get_underlying_type (cfg->generic_sharing_context, sig->ret)->type) {
4818 prev_lmf_reg = X86_EDI;
4819 cfg->used_int_regs |= (1 << X86_EDI);
4822 prev_lmf_reg = X86_EDX;
4826 /* reg = previous_lmf */
4827 x86_mov_reg_membase (code, prev_lmf_reg, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 4);
4830 x86_mov_reg_membase (code, X86_ECX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), 4);
4832 /* *(lmf) = previous_lmf */
4833 x86_mov_membase_reg (code, X86_ECX, 0, prev_lmf_reg, 4);
4836 /* restore caller saved regs */
4837 if (cfg->used_int_regs & (1 << X86_EBX)) {
4838 x86_mov_reg_membase (code, X86_EBX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebx), 4);
4841 if (cfg->used_int_regs & (1 << X86_EDI)) {
4842 x86_mov_reg_membase (code, X86_EDI, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, edi), 4);
4844 if (cfg->used_int_regs & (1 << X86_ESI)) {
4845 x86_mov_reg_membase (code, X86_ESI, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esi), 4);
4848 /* EBP is restored by LEAVE */
4850 if (cfg->used_int_regs & (1 << X86_EBX)) {
4853 if (cfg->used_int_regs & (1 << X86_EDI)) {
4856 if (cfg->used_int_regs & (1 << X86_ESI)) {
4861 g_assert (need_stack_frame);
4862 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
4865 if (cfg->used_int_regs & (1 << X86_ESI)) {
4866 x86_pop_reg (code, X86_ESI);
4868 if (cfg->used_int_regs & (1 << X86_EDI)) {
4869 x86_pop_reg (code, X86_EDI);
4871 if (cfg->used_int_regs & (1 << X86_EBX)) {
4872 x86_pop_reg (code, X86_EBX);
4876 /* Load returned vtypes into registers if needed */
4877 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, FALSE);
4878 if (cinfo->ret.storage == ArgValuetypeInReg) {
4879 for (quad = 0; quad < 2; quad ++) {
4880 switch (cinfo->ret.pair_storage [quad]) {
4882 x86_mov_reg_membase (code, cinfo->ret.pair_regs [quad], cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), 4);
4884 case ArgOnFloatFpStack:
4885 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), FALSE);
4887 case ArgOnDoubleFpStack:
4888 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), TRUE);
4893 g_assert_not_reached ();
4898 if (need_stack_frame)
4901 if (CALLCONV_IS_STDCALL (sig)) {
4902 MonoJitArgumentInfo *arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
4904 stack_to_pop = mono_arch_get_argument_info (sig, sig->param_count, arg_info);
4905 } else if (MONO_TYPE_ISSTRUCT (mono_method_signature (cfg->method)->ret) && (cinfo->ret.storage == ArgOnStack))
4911 g_assert (need_stack_frame);
4912 x86_ret_imm (code, stack_to_pop);
4917 cfg->code_len = code - cfg->native_code;
4919 g_assert (cfg->code_len < cfg->code_size);
4923 mono_arch_emit_exceptions (MonoCompile *cfg)
4925 MonoJumpInfo *patch_info;
4928 MonoClass *exc_classes [16];
4929 guint8 *exc_throw_start [16], *exc_throw_end [16];
4933 /* Compute needed space */
4934 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
4935 if (patch_info->type == MONO_PATCH_INFO_EXC)
4940 * make sure we have enough space for exceptions
4941 * 16 is the size of two push_imm instructions and a call
4943 if (cfg->compile_aot)
4944 code_size = exc_count * 32;
4946 code_size = exc_count * 16;
4948 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
4949 cfg->code_size *= 2;
4950 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4951 mono_jit_stats.code_reallocs++;
4954 code = cfg->native_code + cfg->code_len;
4957 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
4958 switch (patch_info->type) {
4959 case MONO_PATCH_INFO_EXC: {
4960 MonoClass *exc_class;
4964 x86_patch (patch_info->ip.i + cfg->native_code, code);
4966 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
4967 g_assert (exc_class);
4968 throw_ip = patch_info->ip.i;
4970 /* Find a throw sequence for the same exception class */
4971 for (i = 0; i < nthrows; ++i)
4972 if (exc_classes [i] == exc_class)
4975 x86_push_imm (code, (exc_throw_end [i] - cfg->native_code) - throw_ip);
4976 x86_jump_code (code, exc_throw_start [i]);
4977 patch_info->type = MONO_PATCH_INFO_NONE;
4982 /* Compute size of code following the push <OFFSET> */
4985 /*This is aligned to 16 bytes by the callee. This way we save a few bytes here.*/
4987 if ((code - cfg->native_code) - throw_ip < 126 - size) {
4988 /* Use the shorter form */
4990 x86_push_imm (code, 0);
4994 x86_push_imm (code, 0xf0f0f0f0);
4999 exc_classes [nthrows] = exc_class;
5000 exc_throw_start [nthrows] = code;
5003 x86_push_imm (code, exc_class->type_token - MONO_TOKEN_TYPE_DEF);
5004 patch_info->data.name = "mono_arch_throw_corlib_exception";
5005 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
5006 patch_info->ip.i = code - cfg->native_code;
5007 x86_call_code (code, 0);
5008 x86_push_imm (buf, (code - cfg->native_code) - throw_ip);
5013 exc_throw_end [nthrows] = code;
5025 cfg->code_len = code - cfg->native_code;
5027 g_assert (cfg->code_len < cfg->code_size);
5031 mono_arch_flush_icache (guint8 *code, gint size)
5037 mono_arch_flush_register_windows (void)
5042 mono_arch_is_inst_imm (gint64 imm)
5048 * Support for fast access to the thread-local lmf structure using the GS
5049 * segment register on NPTL + kernel 2.6.x.
5052 static gboolean tls_offset_inited = FALSE;
5055 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
5057 if (!tls_offset_inited) {
5058 if (!getenv ("MONO_NO_TLS")) {
5061 * We need to init this multiple times, since when we are first called, the key might not
5062 * be initialized yet.
5064 appdomain_tls_offset = mono_domain_get_tls_key ();
5065 lmf_tls_offset = mono_get_jit_tls_key ();
5067 /* Only 64 tls entries can be accessed using inline code */
5068 if (appdomain_tls_offset >= 64)
5069 appdomain_tls_offset = -1;
5070 if (lmf_tls_offset >= 64)
5071 lmf_tls_offset = -1;
5074 optimize_for_xen = access ("/proc/xen", F_OK) == 0;
5076 tls_offset_inited = TRUE;
5077 appdomain_tls_offset = mono_domain_get_tls_offset ();
5078 lmf_tls_offset = mono_get_lmf_tls_offset ();
5079 lmf_addr_tls_offset = mono_get_lmf_addr_tls_offset ();
5086 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
5090 #ifdef MONO_ARCH_HAVE_IMT
5092 // Linear handler, the bsearch head compare is shorter
5093 //[2 + 4] x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
5094 //[1 + 1] x86_branch8(inst,cond,imm,is_signed)
5095 // x86_patch(ins,target)
5096 //[1 + 5] x86_jump_mem(inst,mem)
5099 #define BR_SMALL_SIZE 2
5100 #define BR_LARGE_SIZE 5
5101 #define JUMP_IMM_SIZE 6
5102 #define ENABLE_WRONG_METHOD_CHECK 0
5105 imt_branch_distance (MonoIMTCheckItem **imt_entries, int start, int target)
5107 int i, distance = 0;
5108 for (i = start; i < target; ++i)
5109 distance += imt_entries [i]->chunk_size;
5114 * LOCKING: called with the domain lock held
5117 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
5118 gpointer fail_tramp)
5122 guint8 *code, *start;
5124 for (i = 0; i < count; ++i) {
5125 MonoIMTCheckItem *item = imt_entries [i];
5126 if (item->is_equals) {
5127 if (item->check_target_idx) {
5128 if (!item->compare_done)
5129 item->chunk_size += CMP_SIZE;
5130 item->chunk_size += BR_SMALL_SIZE + JUMP_IMM_SIZE;
5133 item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + JUMP_IMM_SIZE * 2;
5135 item->chunk_size += JUMP_IMM_SIZE;
5136 #if ENABLE_WRONG_METHOD_CHECK
5137 item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
5142 item->chunk_size += CMP_SIZE + BR_LARGE_SIZE;
5143 imt_entries [item->check_target_idx]->compare_done = TRUE;
5145 size += item->chunk_size;
5148 code = mono_method_alloc_generic_virtual_thunk (domain, size);
5150 code = mono_domain_code_reserve (domain, size);
5152 for (i = 0; i < count; ++i) {
5153 MonoIMTCheckItem *item = imt_entries [i];
5154 item->code_target = code;
5155 if (item->is_equals) {
5156 if (item->check_target_idx) {
5157 if (!item->compare_done)
5158 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5159 item->jmp_code = code;
5160 x86_branch8 (code, X86_CC_NE, 0, FALSE);
5161 if (item->has_target_code)
5162 x86_jump_code (code, item->value.target_code);
5164 x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
5167 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5168 item->jmp_code = code;
5169 x86_branch8 (code, X86_CC_NE, 0, FALSE);
5170 if (item->has_target_code)
5171 x86_jump_code (code, item->value.target_code);
5173 x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
5174 x86_patch (item->jmp_code, code);
5175 x86_jump_code (code, fail_tramp);
5176 item->jmp_code = NULL;
5178 /* enable the commented code to assert on wrong method */
5179 #if ENABLE_WRONG_METHOD_CHECK
5180 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5181 item->jmp_code = code;
5182 x86_branch8 (code, X86_CC_NE, 0, FALSE);
5184 if (item->has_target_code)
5185 x86_jump_code (code, item->value.target_code);
5187 x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
5188 #if ENABLE_WRONG_METHOD_CHECK
5189 x86_patch (item->jmp_code, code);
5190 x86_breakpoint (code);
5191 item->jmp_code = NULL;
5196 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5197 item->jmp_code = code;
5198 if (x86_is_imm8 (imt_branch_distance (imt_entries, i, item->check_target_idx)))
5199 x86_branch8 (code, X86_CC_GE, 0, FALSE);
5201 x86_branch32 (code, X86_CC_GE, 0, FALSE);
5204 /* patch the branches to get to the target items */
5205 for (i = 0; i < count; ++i) {
5206 MonoIMTCheckItem *item = imt_entries [i];
5207 if (item->jmp_code) {
5208 if (item->check_target_idx) {
5209 x86_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
5215 mono_stats.imt_thunks_size += code - start;
5216 g_assert (code - start <= size);
5221 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
5223 return (MonoMethod*) regs [MONO_ARCH_IMT_REG];
5227 mono_arch_find_this_argument (mgreg_t *regs, MonoMethod *method, MonoGenericSharingContext *gsctx)
5229 MonoMethodSignature *sig = mono_method_signature (method);
5230 CallInfo *cinfo = get_call_info (gsctx, NULL, sig, FALSE);
5231 int this_argument_offset;
5232 MonoObject *this_argument;
5235 * this is the offset of the this arg from esp as saved at the start of
5236 * mono_arch_create_trampoline_code () in tramp-x86.c.
5238 this_argument_offset = 5;
5239 if (MONO_TYPE_ISSTRUCT (sig->ret) && (cinfo->ret.storage == ArgOnStack))
5240 this_argument_offset++;
5242 this_argument = * (MonoObject**) (((guint8*) regs [X86_ESP]) + this_argument_offset * sizeof (gpointer));
5245 return this_argument;
5250 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
5252 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
5256 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5258 MonoInst *ins = NULL;
5261 if (cmethod->klass == mono_defaults.math_class) {
5262 if (strcmp (cmethod->name, "Sin") == 0) {
5264 } else if (strcmp (cmethod->name, "Cos") == 0) {
5266 } else if (strcmp (cmethod->name, "Tan") == 0) {
5268 } else if (strcmp (cmethod->name, "Atan") == 0) {
5270 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
5272 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
5274 } else if (strcmp (cmethod->name, "Round") == 0 && fsig->param_count == 1 && fsig->params [0]->type == MONO_TYPE_R8) {
5279 MONO_INST_NEW (cfg, ins, opcode);
5280 ins->type = STACK_R8;
5281 ins->dreg = mono_alloc_freg (cfg);
5282 ins->sreg1 = args [0]->dreg;
5283 MONO_ADD_INS (cfg->cbb, ins);
5286 if (cfg->opt & MONO_OPT_CMOV) {
5289 if (strcmp (cmethod->name, "Min") == 0) {
5290 if (fsig->params [0]->type == MONO_TYPE_I4)
5292 } else if (strcmp (cmethod->name, "Max") == 0) {
5293 if (fsig->params [0]->type == MONO_TYPE_I4)
5298 MONO_INST_NEW (cfg, ins, opcode);
5299 ins->type = STACK_I4;
5300 ins->dreg = mono_alloc_ireg (cfg);
5301 ins->sreg1 = args [0]->dreg;
5302 ins->sreg2 = args [1]->dreg;
5303 MONO_ADD_INS (cfg->cbb, ins);
5308 /* OP_FREM is not IEEE compatible */
5309 else if (strcmp (cmethod->name, "IEEERemainder") == 0) {
5310 MONO_INST_NEW (cfg, ins, OP_FREM);
5311 ins->inst_i0 = args [0];
5312 ins->inst_i1 = args [1];
5321 mono_arch_print_tree (MonoInst *tree, int arity)
5326 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
5332 if (appdomain_tls_offset == -1)
5335 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
5336 ins->inst_offset = appdomain_tls_offset;
5341 mono_arch_get_patch_offset (guint8 *code)
5343 if ((code [0] == 0x8b) && (x86_modrm_mod (code [1]) == 0x2))
5345 else if ((code [0] == 0xba))
5347 else if ((code [0] == 0x68))
5350 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x6))
5351 /* push <OFFSET>(<REG>) */
5353 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x2))
5354 /* call *<OFFSET>(<REG>) */
5356 else if ((code [0] == 0xdd) || (code [0] == 0xd9))
5359 else if ((code [0] == 0x58) && (code [1] == 0x05))
5360 /* pop %eax; add <OFFSET>, %eax */
5362 else if ((code [0] >= 0x58) && (code [0] <= 0x58 + X86_NREG) && (code [1] == 0x81))
5363 /* pop <REG>; add <OFFSET>, <REG> */
5365 else if ((code [0] >= 0xb8) && (code [0] < 0xb8 + 8))
5366 /* mov <REG>, imm */
5369 g_assert_not_reached ();
5375 * mono_breakpoint_clean_code:
5377 * Copy @size bytes from @code - @offset to the buffer @buf. If the debugger inserted software
5378 * breakpoints in the original code, they are removed in the copy.
5380 * Returns TRUE if no sw breakpoint was present.
5383 mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size)
5386 gboolean can_write = TRUE;
5388 * If method_start is non-NULL we need to perform bound checks, since we access memory
5389 * at code - offset we could go before the start of the method and end up in a different
5390 * page of memory that is not mapped or read incorrect data anyway. We zero-fill the bytes
5393 if (!method_start || code - offset >= method_start) {
5394 memcpy (buf, code - offset, size);
5396 int diff = code - method_start;
5397 memset (buf, 0, size);
5398 memcpy (buf + offset - diff, method_start, diff + size - offset);
5401 for (i = 0; i < MONO_BREAKPOINT_ARRAY_SIZE; ++i) {
5402 int idx = mono_breakpoint_info_index [i];
5406 ptr = mono_breakpoint_info [idx].address;
5407 if (ptr >= code && ptr < code + size) {
5408 guint8 saved_byte = mono_breakpoint_info [idx].saved_byte;
5410 /*g_print ("patching %p with 0x%02x (was: 0x%02x)\n", ptr, saved_byte, buf [ptr - code]);*/
5411 buf [ptr - code] = saved_byte;
5418 mono_arch_get_vcall_slot (guint8 *code, mgreg_t *regs, int *displacement)
5424 mono_breakpoint_clean_code (NULL, code, 8, buf, sizeof (buf));
5432 * A given byte sequence can match more than case here, so we have to be
5433 * really careful about the ordering of the cases. Longer sequences
5435 * There are two types of calls:
5436 * - direct calls: 0xff address_byte 8/32 bits displacement
5437 * - indirect calls: nop nop nop <call>
5438 * The nops make sure we don't confuse the instruction preceeding an indirect
5439 * call with a direct call.
5441 if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
5442 reg = code [4] & 0x07;
5443 disp = (signed char)code [5];
5444 } else if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
5445 reg = code [1] & 0x07;
5446 disp = *((gint32*)(code + 2));
5447 } else if ((code [1] == 0xe8)) {
5449 } else if ((code [4] == 0xff) && (((code [5] >> 6) & 0x3) == 0) && (((code [5] >> 3) & 0x7) == 2)) {
5451 * This is a interface call
5452 * 8b 40 30 mov 0x30(%eax),%eax
5453 * ff 10 call *(%eax)
5456 reg = code [5] & 0x07;
5461 *displacement = disp;
5462 return (gpointer)regs [reg];
5466 * mono_x86_get_this_arg_offset:
5468 * Return the offset of the stack location where this is passed during a virtual
5472 mono_x86_get_this_arg_offset (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig)
5474 CallInfo *cinfo = NULL;
5477 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
5478 cinfo = get_call_info (gsctx, NULL, sig, FALSE);
5480 offset = cinfo->args [0].offset;
5489 mono_arch_get_this_arg_from_call (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig,
5490 mgreg_t *regs, guint8 *code)
5492 guint32 esp = regs [X86_ESP];
5493 CallInfo *cinfo = NULL;
5498 * Avoid expensive calls to get_generic_context_from_code () + get_call_info
5501 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
5503 gsctx = mono_get_generic_context_from_code (code);
5504 cinfo = get_call_info (gsctx, NULL, sig, FALSE);
5506 offset = cinfo->args [0].offset;
5512 * The stack looks like:
5515 * <possible vtype return address>
5517 * <4 pointers pushed by mono_arch_create_trampoline_code ()>
5519 res = (((MonoObject**)esp) [5 + (offset / 4)]);
5525 #define MAX_ARCH_DELEGATE_PARAMS 10
5528 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
5530 guint8 *code, *start;
5532 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
5535 /* FIXME: Support more cases */
5536 if (MONO_TYPE_ISSTRUCT (sig->ret))
5540 * The stack contains:
5546 static guint8* cached = NULL;
5550 start = code = mono_global_codeman_reserve (64);
5552 /* Replace the this argument with the target */
5553 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
5554 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoDelegate, target), 4);
5555 x86_mov_membase_reg (code, X86_ESP, 4, X86_ECX, 4);
5556 x86_jump_membase (code, X86_EAX, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
5558 g_assert ((code - start) < 64);
5560 mono_debug_add_delegate_trampoline (start, code - start);
5562 mono_memory_barrier ();
5566 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
5568 /* 8 for mov_reg and jump, plus 8 for each parameter */
5569 int code_reserve = 8 + (sig->param_count * 8);
5571 for (i = 0; i < sig->param_count; ++i)
5572 if (!mono_is_regsize_var (sig->params [i]))
5575 code = cache [sig->param_count];
5580 * The stack contains:
5581 * <args in reverse order>
5586 * <args in reverse order>
5589 * without unbalancing the stack.
5590 * So move each arg up a spot in the stack (overwriting un-needed 'this' arg)
5591 * and leaving original spot of first arg as placeholder in stack so
5592 * when callee pops stack everything works.
5595 start = code = mono_global_codeman_reserve (code_reserve);
5597 /* store delegate for access to method_ptr */
5598 x86_mov_reg_membase (code, X86_ECX, X86_ESP, 4, 4);
5601 for (i = 0; i < sig->param_count; ++i) {
5602 x86_mov_reg_membase (code, X86_EAX, X86_ESP, (i+2)*4, 4);
5603 x86_mov_membase_reg (code, X86_ESP, (i+1)*4, X86_EAX, 4);
5606 x86_jump_membase (code, X86_ECX, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
5608 g_assert ((code - start) < code_reserve);
5610 mono_debug_add_delegate_trampoline (start, code - start);
5612 mono_memory_barrier ();
5614 cache [sig->param_count] = start;
5621 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
5624 case X86_EAX: return (gpointer)ctx->eax;
5625 case X86_EBX: return (gpointer)ctx->ebx;
5626 case X86_ECX: return (gpointer)ctx->ecx;
5627 case X86_EDX: return (gpointer)ctx->edx;
5628 case X86_ESP: return (gpointer)ctx->esp;
5629 case X86_EBP: return (gpointer)ctx->ebp;
5630 case X86_ESI: return (gpointer)ctx->esi;
5631 case X86_EDI: return (gpointer)ctx->edi;
5632 default: g_assert_not_reached ();
5636 #ifdef MONO_ARCH_SIMD_INTRINSICS
5639 get_float_to_x_spill_area (MonoCompile *cfg)
5641 if (!cfg->fconv_to_r8_x_var) {
5642 cfg->fconv_to_r8_x_var = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
5643 cfg->fconv_to_r8_x_var->flags |= MONO_INST_VOLATILE; /*FIXME, use the don't regalloc flag*/
5645 return cfg->fconv_to_r8_x_var;
5649 * Convert all fconv opts that MONO_OPT_SSE2 would get wrong.
5652 mono_arch_decompose_opts (MonoCompile *cfg, MonoInst *ins)
5655 int dreg, src_opcode;
5657 if (!(cfg->opt & MONO_OPT_SSE2) || !(cfg->opt & MONO_OPT_SIMD) || COMPILE_LLVM (cfg))
5660 switch (src_opcode = ins->opcode) {
5661 case OP_FCONV_TO_I1:
5662 case OP_FCONV_TO_U1:
5663 case OP_FCONV_TO_I2:
5664 case OP_FCONV_TO_U2:
5665 case OP_FCONV_TO_I4:
5672 /* dreg is the IREG and sreg1 is the FREG */
5673 MONO_INST_NEW (cfg, fconv, OP_FCONV_TO_R8_X);
5674 fconv->klass = NULL; /*FIXME, what can I use here as the Mono.Simd lib might not be loaded yet*/
5675 fconv->sreg1 = ins->sreg1;
5676 fconv->dreg = mono_alloc_ireg (cfg);
5677 fconv->type = STACK_VTYPE;
5678 fconv->backend.spill_var = get_float_to_x_spill_area (cfg);
5680 mono_bblock_insert_before_ins (cfg->cbb, ins, fconv);
5684 ins->opcode = OP_XCONV_R8_TO_I4;
5686 ins->klass = mono_defaults.int32_class;
5687 ins->sreg1 = fconv->dreg;
5689 ins->type = STACK_I4;
5690 ins->backend.source_opcode = src_opcode;
5693 #endif /* #ifdef MONO_ARCH_SIMD_INTRINSICS */
5696 mono_arch_decompose_long_opts (MonoCompile *cfg, MonoInst *long_ins)
5701 if (long_ins->opcode == OP_LNEG) {
5703 MONO_EMIT_NEW_UNALU (cfg, OP_INEG, ins->dreg + 1, ins->sreg1 + 1);
5704 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADC_IMM, ins->dreg + 2, ins->sreg1 + 2, 0);
5705 MONO_EMIT_NEW_UNALU (cfg, OP_INEG, ins->dreg + 2, ins->dreg + 2);
5710 #ifdef MONO_ARCH_SIMD_INTRINSICS
5712 if (!(cfg->opt & MONO_OPT_SIMD))
5715 /*TODO move this to simd-intrinsic.c once we support sse 4.1 dword extractors since we need the runtime caps info */
5716 switch (long_ins->opcode) {
5718 vreg = long_ins->sreg1;
5720 if (long_ins->inst_c0) {
5721 MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
5722 ins->klass = long_ins->klass;
5723 ins->sreg1 = long_ins->sreg1;
5725 ins->type = STACK_VTYPE;
5726 ins->dreg = vreg = alloc_ireg (cfg);
5727 MONO_ADD_INS (cfg->cbb, ins);
5730 MONO_INST_NEW (cfg, ins, OP_EXTRACT_I4);
5731 ins->klass = mono_defaults.int32_class;
5733 ins->type = STACK_I4;
5734 ins->dreg = long_ins->dreg + 1;
5735 MONO_ADD_INS (cfg->cbb, ins);
5737 MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
5738 ins->klass = long_ins->klass;
5739 ins->sreg1 = long_ins->sreg1;
5740 ins->inst_c0 = long_ins->inst_c0 ? 3 : 1;
5741 ins->type = STACK_VTYPE;
5742 ins->dreg = vreg = alloc_ireg (cfg);
5743 MONO_ADD_INS (cfg->cbb, ins);
5745 MONO_INST_NEW (cfg, ins, OP_EXTRACT_I4);
5746 ins->klass = mono_defaults.int32_class;
5748 ins->type = STACK_I4;
5749 ins->dreg = long_ins->dreg + 2;
5750 MONO_ADD_INS (cfg->cbb, ins);
5752 long_ins->opcode = OP_NOP;
5754 case OP_INSERTX_I8_SLOW:
5755 MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
5756 ins->dreg = long_ins->dreg;
5757 ins->sreg1 = long_ins->dreg;
5758 ins->sreg2 = long_ins->sreg2 + 1;
5759 ins->inst_c0 = long_ins->inst_c0 * 2;
5760 MONO_ADD_INS (cfg->cbb, ins);
5762 MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
5763 ins->dreg = long_ins->dreg;
5764 ins->sreg1 = long_ins->dreg;
5765 ins->sreg2 = long_ins->sreg2 + 2;
5766 ins->inst_c0 = long_ins->inst_c0 * 2 + 1;
5767 MONO_ADD_INS (cfg->cbb, ins);
5769 long_ins->opcode = OP_NOP;
5772 MONO_INST_NEW (cfg, ins, OP_ICONV_TO_X);
5773 ins->dreg = long_ins->dreg;
5774 ins->sreg1 = long_ins->sreg1 + 1;
5775 ins->klass = long_ins->klass;
5776 ins->type = STACK_VTYPE;
5777 MONO_ADD_INS (cfg->cbb, ins);
5779 MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
5780 ins->dreg = long_ins->dreg;
5781 ins->sreg1 = long_ins->dreg;
5782 ins->sreg2 = long_ins->sreg1 + 2;
5784 ins->klass = long_ins->klass;
5785 ins->type = STACK_VTYPE;
5786 MONO_ADD_INS (cfg->cbb, ins);
5788 MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
5789 ins->dreg = long_ins->dreg;
5790 ins->sreg1 = long_ins->dreg;;
5791 ins->inst_c0 = 0x44; /*Magic number for swizzling (X,Y,X,Y)*/
5792 ins->klass = long_ins->klass;
5793 ins->type = STACK_VTYPE;
5794 MONO_ADD_INS (cfg->cbb, ins);
5796 long_ins->opcode = OP_NOP;
5799 #endif /* MONO_ARCH_SIMD_INTRINSICS */
5803 #define DBG_SIGNAL SIGBUS
5805 #define DBG_SIGNAL SIGSEGV
5808 /* Soft Debug support */
5809 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
5812 * mono_arch_set_breakpoint:
5814 * Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
5815 * The location should contain code emitted by OP_SEQ_POINT.
5818 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
5823 * In production, we will use int3 (has to fix the size in the md
5824 * file). But that could confuse gdb, so during development, we emit a SIGSEGV
5827 g_assert (code [0] == 0x90);
5828 x86_alu_reg_mem (code, X86_CMP, X86_EAX, (guint32)bp_trigger_page);
5832 * mono_arch_clear_breakpoint:
5834 * Clear the breakpoint at IP.
5837 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
5842 for (i = 0; i < 6; ++i)
5847 * mono_arch_start_single_stepping:
5849 * Start single stepping.
5852 mono_arch_start_single_stepping (void)
5854 mono_mprotect (ss_trigger_page, mono_pagesize (), 0);
5858 * mono_arch_stop_single_stepping:
5860 * Stop single stepping.
5863 mono_arch_stop_single_stepping (void)
5865 mono_mprotect (ss_trigger_page, mono_pagesize (), MONO_MMAP_READ);
5869 * mono_arch_is_single_step_event:
5871 * Return whenever the machine state in SIGCTX corresponds to a single
5875 mono_arch_is_single_step_event (void *info, void *sigctx)
5878 EXCEPTION_RECORD* einfo = (EXCEPTION_RECORD*)info; /* Sometimes the address is off by 4 */
5879 if ((einfo->ExceptionInformation[1] >= ss_trigger_page && (guint8*)einfo->ExceptionInformation[1] <= (guint8*)ss_trigger_page + 128))
5884 siginfo_t* sinfo = (siginfo_t*) info;
5885 /* Sometimes the address is off by 4 */
5886 if (sinfo->si_signo == DBG_SIGNAL && (sinfo->si_addr >= ss_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)ss_trigger_page + 128))
5894 mono_arch_is_breakpoint_event (void *info, void *sigctx)
5897 EXCEPTION_RECORD* einfo = (EXCEPTION_RECORD*)info; /* Sometimes the address is off by 4 */
5898 if ((einfo->ExceptionInformation[1] >= bp_trigger_page && (guint8*)einfo->ExceptionInformation[1] <= (guint8*)bp_trigger_page + 128))
5903 siginfo_t* sinfo = (siginfo_t*)info;
5904 /* Sometimes the address is off by 4 */
5905 if (sinfo->si_signo == DBG_SIGNAL && (sinfo->si_addr >= bp_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)bp_trigger_page + 128))
5913 * mono_arch_get_ip_for_breakpoint:
5915 * See mini-amd64.c for docs.
5918 mono_arch_get_ip_for_breakpoint (MonoJitInfo *ji, MonoContext *ctx)
5920 guint8 *ip = MONO_CONTEXT_GET_IP (ctx);
5925 #define BREAKPOINT_SIZE 6
5928 * mono_arch_get_ip_for_single_step:
5930 * See mini-amd64.c for docs.
5933 mono_arch_get_ip_for_single_step (MonoJitInfo *ji, MonoContext *ctx)
5935 guint8 *ip = MONO_CONTEXT_GET_IP (ctx);
5937 /* Size of x86_alu_reg_imm */
5944 * mono_arch_skip_breakpoint:
5946 * See mini-amd64.c for docs.
5949 mono_arch_skip_breakpoint (MonoContext *ctx)
5951 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + BREAKPOINT_SIZE);
5955 * mono_arch_skip_single_step:
5957 * See mini-amd64.c for docs.
5960 mono_arch_skip_single_step (MonoContext *ctx)
5962 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 6);
5966 * mono_arch_get_seq_point_info:
5968 * See mini-amd64.c for docs.
5971 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)