2 * mini-x86.c: x86 backend for the Mono code generator
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
9 * Copyright 2003 Ximian, Inc.
10 * Copyright 2003-2011 Novell Inc.
11 * Copyright 2011 Xamarin Inc.
20 #include <mono/metadata/abi-details.h>
21 #include <mono/metadata/appdomain.h>
22 #include <mono/metadata/debug-helpers.h>
23 #include <mono/metadata/threads.h>
24 #include <mono/metadata/profiler-private.h>
25 #include <mono/metadata/mono-debug.h>
26 #include <mono/metadata/gc-internal.h>
27 #include <mono/utils/mono-math.h>
28 #include <mono/utils/mono-counters.h>
29 #include <mono/utils/mono-mmap.h>
30 #include <mono/utils/mono-memory-model.h>
31 #include <mono/utils/mono-hwcap-x86.h>
32 #include <mono/utils/mono-threads.h>
42 static gboolean optimize_for_xen = TRUE;
44 #define optimize_for_xen 0
48 /* This mutex protects architecture specific caches */
49 #define mono_mini_arch_lock() mono_mutex_lock (&mini_arch_mutex)
50 #define mono_mini_arch_unlock() mono_mutex_unlock (&mini_arch_mutex)
51 static mono_mutex_t mini_arch_mutex;
53 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
58 /* Under windows, the default pinvoke calling convention is stdcall */
59 #define CALLCONV_IS_STDCALL(sig) ((((sig)->call_convention) == MONO_CALL_STDCALL) || ((sig)->pinvoke && ((sig)->call_convention) == MONO_CALL_DEFAULT) || ((sig)->pinvoke && ((sig)->call_convention) == MONO_CALL_THISCALL))
61 #define CALLCONV_IS_STDCALL(sig) (((sig)->call_convention) == MONO_CALL_STDCALL || ((sig)->pinvoke && ((sig)->call_convention) == MONO_CALL_THISCALL))
64 #define X86_IS_CALLEE_SAVED_REG(reg) (((reg) == X86_EBX) || ((reg) == X86_EDI) || ((reg) == X86_ESI))
67 mono_breakpoint_info [MONO_BREAKPOINT_ARRAY_SIZE];
70 emit_load_aotconst (guint8 *start, guint8 *code, MonoCompile *cfg, MonoJumpInfo **ji, int dreg, int tramp_type, gconstpointer target);
72 #ifdef __native_client_codegen__
74 /* Default alignment for Native Client is 32-byte. */
75 gint8 nacl_align_byte = -32; /* signed version of 0xe0 */
77 /* mono_arch_nacl_pad: Add pad bytes of alignment instructions at code, */
78 /* Check that alignment doesn't cross an alignment boundary. */
80 mono_arch_nacl_pad (guint8 *code, int pad)
82 const int kMaxPadding = 7; /* see x86-codegen.h: x86_padding() */
84 if (pad == 0) return code;
85 /* assertion: alignment cannot cross a block boundary */
86 g_assert(((uintptr_t)code & (~kNaClAlignmentMask)) ==
87 (((uintptr_t)code + pad - 1) & (~kNaClAlignmentMask)));
88 while (pad >= kMaxPadding) {
89 x86_padding (code, kMaxPadding);
92 if (pad != 0) x86_padding (code, pad);
97 mono_arch_nacl_skip_nops (guint8 *code)
103 #endif /* __native_client_codegen__ */
106 * The code generated for sequence points reads from this location, which is
107 * made read-only when single stepping is enabled.
109 static gpointer ss_trigger_page;
111 /* Enabled breakpoints read from this trigger page */
112 static gpointer bp_trigger_page;
115 mono_arch_regname (int reg)
118 case X86_EAX: return "%eax";
119 case X86_EBX: return "%ebx";
120 case X86_ECX: return "%ecx";
121 case X86_EDX: return "%edx";
122 case X86_ESP: return "%esp";
123 case X86_EBP: return "%ebp";
124 case X86_EDI: return "%edi";
125 case X86_ESI: return "%esi";
131 mono_arch_fregname (int reg)
156 mono_arch_xregname (int reg)
181 mono_x86_patch (unsigned char* code, gpointer target)
183 x86_patch (code, (unsigned char*)target);
194 /* gsharedvt argument passed by addr */
206 /* Only if storage == ArgValuetypeInReg */
207 ArgStorage pair_storage [2];
216 gboolean need_stack_align;
217 guint32 stack_align_amount;
218 gboolean vtype_retaddr;
219 /* The index of the vret arg in the argument list */
222 /* Argument space popped by the callee */
223 int callee_stack_pop;
229 #define FLOAT_PARAM_REGS 0
231 static const guint32 thiscall_param_regs [] = { X86_ECX, X86_NREG };
233 static const guint32 *callconv_param_regs(MonoMethodSignature *sig)
238 switch (sig->call_convention) {
239 case MONO_CALL_THISCALL:
240 return thiscall_param_regs;
246 #if defined(TARGET_WIN32) || defined(__APPLE__) || defined(__FreeBSD__)
247 #define SMALL_STRUCTS_IN_REGS
248 static X86_Reg_No return_regs [] = { X86_EAX, X86_EDX };
252 add_general (guint32 *gr, const guint32 *param_regs, guint32 *stack_size, ArgInfo *ainfo)
254 ainfo->offset = *stack_size;
256 if (!param_regs || param_regs [*gr] == X86_NREG) {
257 ainfo->storage = ArgOnStack;
259 (*stack_size) += sizeof (gpointer);
262 ainfo->storage = ArgInIReg;
263 ainfo->reg = param_regs [*gr];
269 add_general_pair (guint32 *gr, const guint32 *param_regs , guint32 *stack_size, ArgInfo *ainfo)
271 ainfo->offset = *stack_size;
273 g_assert(!param_regs || param_regs[*gr] == X86_NREG);
275 ainfo->storage = ArgOnStack;
276 (*stack_size) += sizeof (gpointer) * 2;
281 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
283 ainfo->offset = *stack_size;
285 if (*gr >= FLOAT_PARAM_REGS) {
286 ainfo->storage = ArgOnStack;
287 (*stack_size) += is_double ? 8 : 4;
288 ainfo->nslots = is_double ? 2 : 1;
291 /* A double register */
293 ainfo->storage = ArgInDoubleSSEReg;
295 ainfo->storage = ArgInFloatSSEReg;
303 add_valuetype (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
305 guint32 *gr, const guint32 *param_regs, guint32 *fr, guint32 *stack_size)
310 klass = mono_class_from_mono_type (type);
311 size = mini_type_stack_size_full (gsctx, &klass->byval_arg, NULL, sig->pinvoke);
313 #ifdef SMALL_STRUCTS_IN_REGS
314 if (sig->pinvoke && is_return) {
315 MonoMarshalType *info;
318 * the exact rules are not very well documented, the code below seems to work with the
319 * code generated by gcc 3.3.3 -mno-cygwin.
321 info = mono_marshal_load_type_info (klass);
324 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
326 /* Special case structs with only a float member */
327 if (info->num_fields == 1) {
328 int ftype = mini_type_get_underlying_type (gsctx, info->fields [0].field->type)->type;
329 if ((info->native_size == 8) && (ftype == MONO_TYPE_R8)) {
330 ainfo->storage = ArgValuetypeInReg;
331 ainfo->pair_storage [0] = ArgOnDoubleFpStack;
334 if ((info->native_size == 4) && (ftype == MONO_TYPE_R4)) {
335 ainfo->storage = ArgValuetypeInReg;
336 ainfo->pair_storage [0] = ArgOnFloatFpStack;
340 if ((info->native_size == 1) || (info->native_size == 2) || (info->native_size == 4) || (info->native_size == 8)) {
341 ainfo->storage = ArgValuetypeInReg;
342 ainfo->pair_storage [0] = ArgInIReg;
343 ainfo->pair_regs [0] = return_regs [0];
344 if (info->native_size > 4) {
345 ainfo->pair_storage [1] = ArgInIReg;
346 ainfo->pair_regs [1] = return_regs [1];
353 if (param_regs && param_regs [*gr] != X86_NREG && !is_return) {
354 g_assert (size <= 4);
355 ainfo->storage = ArgValuetypeInReg;
356 ainfo->reg = param_regs [*gr];
361 ainfo->offset = *stack_size;
362 ainfo->storage = ArgOnStack;
363 *stack_size += ALIGN_TO (size, sizeof (gpointer));
364 ainfo->nslots = ALIGN_TO (size, sizeof (gpointer)) / sizeof (gpointer);
370 * Obtain information about a call according to the calling convention.
371 * For x86 ELF, see the "System V Application Binary Interface Intel386
372 * Architecture Processor Supplment, Fourth Edition" document for more
374 * For x86 win32, see ???.
377 get_call_info_internal (MonoGenericSharingContext *gsctx, CallInfo *cinfo, MonoMethodSignature *sig)
379 guint32 i, gr, fr, pstart;
380 const guint32 *param_regs;
382 int n = sig->hasthis + sig->param_count;
383 guint32 stack_size = 0;
384 gboolean is_pinvoke = sig->pinvoke;
390 param_regs = callconv_param_regs(sig);
394 ret_type = mini_type_get_underlying_type (gsctx, sig->ret);
395 switch (ret_type->type) {
405 case MONO_TYPE_FNPTR:
406 case MONO_TYPE_CLASS:
407 case MONO_TYPE_OBJECT:
408 case MONO_TYPE_SZARRAY:
409 case MONO_TYPE_ARRAY:
410 case MONO_TYPE_STRING:
411 cinfo->ret.storage = ArgInIReg;
412 cinfo->ret.reg = X86_EAX;
416 cinfo->ret.storage = ArgInIReg;
417 cinfo->ret.reg = X86_EAX;
418 cinfo->ret.is_pair = TRUE;
421 cinfo->ret.storage = ArgOnFloatFpStack;
424 cinfo->ret.storage = ArgOnDoubleFpStack;
426 case MONO_TYPE_GENERICINST:
427 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
428 cinfo->ret.storage = ArgInIReg;
429 cinfo->ret.reg = X86_EAX;
432 if (mini_is_gsharedvt_type_gsctx (gsctx, ret_type)) {
433 cinfo->ret.storage = ArgOnStack;
434 cinfo->vtype_retaddr = TRUE;
438 case MONO_TYPE_VALUETYPE:
439 case MONO_TYPE_TYPEDBYREF: {
440 guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
442 add_valuetype (gsctx, sig, &cinfo->ret, ret_type, TRUE, &tmp_gr, NULL, &tmp_fr, &tmp_stacksize);
443 if (cinfo->ret.storage == ArgOnStack) {
444 cinfo->vtype_retaddr = TRUE;
445 /* The caller passes the address where the value is stored */
451 g_assert (mini_is_gsharedvt_type_gsctx (gsctx, ret_type));
452 cinfo->ret.storage = ArgOnStack;
453 cinfo->vtype_retaddr = TRUE;
456 cinfo->ret.storage = ArgNone;
459 g_error ("Can't handle as return value 0x%x", ret_type->type);
465 * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
466 * the first argument, allowing 'this' to be always passed in the first arg reg.
467 * Also do this if the first argument is a reference type, since virtual calls
468 * are sometimes made using calli without sig->hasthis set, like in the delegate
471 if (cinfo->vtype_retaddr && !is_pinvoke && (sig->hasthis || (sig->param_count > 0 && MONO_TYPE_IS_REFERENCE (mini_type_get_underlying_type (gsctx, sig->params [0]))))) {
473 add_general (&gr, param_regs, &stack_size, cinfo->args + 0);
475 add_general (&gr, param_regs, &stack_size, &cinfo->args [sig->hasthis + 0]);
478 cinfo->vret_arg_offset = stack_size;
479 add_general (&gr, NULL, &stack_size, &cinfo->ret);
480 cinfo->vret_arg_index = 1;
484 add_general (&gr, param_regs, &stack_size, cinfo->args + 0);
486 if (cinfo->vtype_retaddr)
487 add_general (&gr, NULL, &stack_size, &cinfo->ret);
490 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
491 fr = FLOAT_PARAM_REGS;
493 /* Emit the signature cookie just before the implicit arguments */
494 add_general (&gr, param_regs, &stack_size, &cinfo->sig_cookie);
497 for (i = pstart; i < sig->param_count; ++i) {
498 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
501 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
502 /* We allways pass the sig cookie on the stack for simplicity */
504 * Prevent implicit arguments + the sig cookie from being passed
507 fr = FLOAT_PARAM_REGS;
509 /* Emit the signature cookie just before the implicit arguments */
510 add_general (&gr, param_regs, &stack_size, &cinfo->sig_cookie);
513 if (sig->params [i]->byref) {
514 add_general (&gr, param_regs, &stack_size, ainfo);
517 ptype = mini_type_get_underlying_type (gsctx, sig->params [i]);
518 switch (ptype->type) {
521 add_general (&gr, param_regs, &stack_size, ainfo);
525 add_general (&gr, param_regs, &stack_size, ainfo);
529 add_general (&gr, param_regs, &stack_size, ainfo);
534 case MONO_TYPE_FNPTR:
535 case MONO_TYPE_CLASS:
536 case MONO_TYPE_OBJECT:
537 case MONO_TYPE_STRING:
538 case MONO_TYPE_SZARRAY:
539 case MONO_TYPE_ARRAY:
540 add_general (&gr, param_regs, &stack_size, ainfo);
542 case MONO_TYPE_GENERICINST:
543 if (!mono_type_generic_inst_is_valuetype (ptype)) {
544 add_general (&gr, param_regs, &stack_size, ainfo);
547 if (mini_is_gsharedvt_type_gsctx (gsctx, ptype)) {
548 /* gsharedvt arguments are passed by ref */
549 add_general (&gr, param_regs, &stack_size, ainfo);
550 g_assert (ainfo->storage == ArgOnStack);
551 ainfo->storage = ArgGSharedVt;
555 case MONO_TYPE_VALUETYPE:
556 case MONO_TYPE_TYPEDBYREF:
557 add_valuetype (gsctx, sig, ainfo, ptype, FALSE, &gr, param_regs, &fr, &stack_size);
561 add_general_pair (&gr, param_regs, &stack_size, ainfo);
564 add_float (&fr, &stack_size, ainfo, FALSE);
567 add_float (&fr, &stack_size, ainfo, TRUE);
571 /* gsharedvt arguments are passed by ref */
572 g_assert (mini_is_gsharedvt_type_gsctx (gsctx, ptype));
573 add_general (&gr, param_regs, &stack_size, ainfo);
574 g_assert (ainfo->storage == ArgOnStack);
575 ainfo->storage = ArgGSharedVt;
578 g_error ("unexpected type 0x%x", ptype->type);
579 g_assert_not_reached ();
583 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
584 fr = FLOAT_PARAM_REGS;
586 /* Emit the signature cookie just before the implicit arguments */
587 add_general (&gr, param_regs, &stack_size, &cinfo->sig_cookie);
590 if (cinfo->vtype_retaddr) {
591 /* if the function returns a struct on stack, the called method already does a ret $0x4 */
592 cinfo->callee_stack_pop = 4;
593 } else if (CALLCONV_IS_STDCALL (sig) && sig->pinvoke) {
594 /* Have to compensate for the stack space popped by the native callee */
595 cinfo->callee_stack_pop = stack_size;
598 if (mono_do_x86_stack_align && (stack_size % MONO_ARCH_FRAME_ALIGNMENT) != 0) {
599 cinfo->need_stack_align = TRUE;
600 cinfo->stack_align_amount = MONO_ARCH_FRAME_ALIGNMENT - (stack_size % MONO_ARCH_FRAME_ALIGNMENT);
601 stack_size += cinfo->stack_align_amount;
604 cinfo->stack_usage = stack_size;
605 cinfo->reg_usage = gr;
606 cinfo->freg_usage = fr;
611 get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSignature *sig)
613 int n = sig->hasthis + sig->param_count;
617 cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
619 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
621 return get_call_info_internal (gsctx, cinfo, sig);
625 * mono_arch_get_argument_info:
626 * @csig: a method signature
627 * @param_count: the number of parameters to consider
628 * @arg_info: an array to store the result infos
630 * Gathers information on parameters such as size, alignment and
631 * padding. arg_info should be large enought to hold param_count + 1 entries.
633 * Returns the size of the argument area on the stack.
634 * This should be signal safe, since it is called from
635 * mono_arch_find_jit_info ().
636 * FIXME: The metadata calls might not be signal safe.
639 mono_arch_get_argument_info (MonoGenericSharingContext *gsctx, MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
641 int len, k, args_size = 0;
647 /* Avoid g_malloc as it is not signal safe */
648 len = sizeof (CallInfo) + (sizeof (ArgInfo) * (csig->param_count + 1));
649 cinfo = (CallInfo*)g_newa (guint8*, len);
650 memset (cinfo, 0, len);
652 cinfo = get_call_info_internal (gsctx, cinfo, csig);
654 arg_info [0].offset = offset;
656 if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 0) {
657 args_size += sizeof (gpointer);
662 args_size += sizeof (gpointer);
666 if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 1 && csig->hasthis) {
667 /* Emitted after this */
668 args_size += sizeof (gpointer);
672 arg_info [0].size = args_size;
674 for (k = 0; k < param_count; k++) {
675 size = mini_type_stack_size_full (NULL, csig->params [k], &align, csig->pinvoke);
677 /* ignore alignment for now */
680 args_size += pad = (align - (args_size & (align - 1))) & (align - 1);
681 arg_info [k].pad = pad;
683 arg_info [k + 1].pad = 0;
684 arg_info [k + 1].size = size;
686 arg_info [k + 1].offset = offset;
689 if (k == 0 && cinfo->vtype_retaddr && cinfo->vret_arg_index == 1 && !csig->hasthis) {
690 /* Emitted after the first arg */
691 args_size += sizeof (gpointer);
696 if (mono_do_x86_stack_align && !CALLCONV_IS_STDCALL (csig))
697 align = MONO_ARCH_FRAME_ALIGNMENT;
700 args_size += pad = (align - (args_size & (align - 1))) & (align - 1);
701 arg_info [k].pad = pad;
707 mono_arch_tail_call_supported (MonoCompile *cfg, MonoMethodSignature *caller_sig, MonoMethodSignature *callee_sig)
709 MonoType *callee_ret;
713 if (cfg->compile_aot && !cfg->full_aot)
714 /* OP_TAILCALL doesn't work with AOT */
717 c1 = get_call_info (NULL, NULL, caller_sig);
718 c2 = get_call_info (NULL, NULL, callee_sig);
720 * Tail calls with more callee stack usage than the caller cannot be supported, since
721 * the extra stack space would be left on the stack after the tail call.
723 res = c1->stack_usage >= c2->stack_usage;
724 callee_ret = mini_get_underlying_type (cfg, callee_sig->ret);
725 if (callee_ret && MONO_TYPE_ISSTRUCT (callee_ret) && c2->ret.storage != ArgValuetypeInReg)
726 /* An address on the callee's stack is passed as the first argument */
736 * Initialize the cpu to execute managed code.
739 mono_arch_cpu_init (void)
741 /* spec compliance requires running with double precision */
745 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
746 fpcw &= ~X86_FPCW_PRECC_MASK;
747 fpcw |= X86_FPCW_PREC_DOUBLE;
748 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
749 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
751 _control87 (_PC_53, MCW_PC);
756 * Initialize architecture specific code.
759 mono_arch_init (void)
761 mono_mutex_init_recursive (&mini_arch_mutex);
763 ss_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ);
764 bp_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_32BIT);
765 mono_mprotect (bp_trigger_page, mono_pagesize (), 0);
767 mono_aot_register_jit_icall ("mono_x86_throw_exception", mono_x86_throw_exception);
768 mono_aot_register_jit_icall ("mono_x86_throw_corlib_exception", mono_x86_throw_corlib_exception);
769 #if defined(ENABLE_GSHAREDVT)
770 mono_aot_register_jit_icall ("mono_x86_start_gsharedvt_call", mono_x86_start_gsharedvt_call);
775 * Cleanup architecture specific code.
778 mono_arch_cleanup (void)
781 mono_vfree (ss_trigger_page, mono_pagesize ());
783 mono_vfree (bp_trigger_page, mono_pagesize ());
784 mono_mutex_destroy (&mini_arch_mutex);
788 * This function returns the optimizations supported on this cpu.
791 mono_arch_cpu_optimizations (guint32 *exclude_mask)
793 #if !defined(__native_client__)
798 if (mono_hwcap_x86_has_cmov) {
799 opts |= MONO_OPT_CMOV;
801 if (mono_hwcap_x86_has_fcmov)
802 opts |= MONO_OPT_FCMOV;
804 *exclude_mask |= MONO_OPT_FCMOV;
806 *exclude_mask |= MONO_OPT_CMOV;
809 if (mono_hwcap_x86_has_sse2)
810 opts |= MONO_OPT_SSE2;
812 *exclude_mask |= MONO_OPT_SSE2;
814 #ifdef MONO_ARCH_SIMD_INTRINSICS
815 /*SIMD intrinsics require at least SSE2.*/
816 if (!mono_hwcap_x86_has_sse2)
817 *exclude_mask |= MONO_OPT_SIMD;
822 return MONO_OPT_CMOV | MONO_OPT_FCMOV | MONO_OPT_SSE2;
827 * This function test for all SSE functions supported.
829 * Returns a bitmask corresponding to all supported versions.
833 mono_arch_cpu_enumerate_simd_versions (void)
835 guint32 sse_opts = 0;
837 if (mono_hwcap_x86_has_sse1)
838 sse_opts |= SIMD_VERSION_SSE1;
840 if (mono_hwcap_x86_has_sse2)
841 sse_opts |= SIMD_VERSION_SSE2;
843 if (mono_hwcap_x86_has_sse3)
844 sse_opts |= SIMD_VERSION_SSE3;
846 if (mono_hwcap_x86_has_ssse3)
847 sse_opts |= SIMD_VERSION_SSSE3;
849 if (mono_hwcap_x86_has_sse41)
850 sse_opts |= SIMD_VERSION_SSE41;
852 if (mono_hwcap_x86_has_sse42)
853 sse_opts |= SIMD_VERSION_SSE42;
855 if (mono_hwcap_x86_has_sse4a)
856 sse_opts |= SIMD_VERSION_SSE4a;
862 * Determine whenever the trap whose info is in SIGINFO is caused by
866 mono_arch_is_int_overflow (void *sigctx, void *info)
871 mono_sigctx_to_monoctx (sigctx, &ctx);
873 ip = (guint8*)ctx.eip;
875 if ((ip [0] == 0xf7) && (x86_modrm_mod (ip [1]) == 0x3) && (x86_modrm_reg (ip [1]) == 0x7)) {
879 switch (x86_modrm_rm (ip [1])) {
899 g_assert_not_reached ();
911 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
916 for (i = 0; i < cfg->num_varinfo; i++) {
917 MonoInst *ins = cfg->varinfo [i];
918 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
921 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
924 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
925 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
928 /* we dont allocate I1 to registers because there is no simply way to sign extend
929 * 8bit quantities in caller saved registers on x86 */
930 if (mono_is_regsize_var (ins->inst_vtype) && (ins->inst_vtype->type != MONO_TYPE_I1)) {
931 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
932 g_assert (i == vmv->idx);
933 vars = g_list_prepend (vars, vmv);
937 vars = mono_varlist_sort (cfg, vars, 0);
943 mono_arch_get_global_int_regs (MonoCompile *cfg)
947 /* we can use 3 registers for global allocation */
948 regs = g_list_prepend (regs, (gpointer)X86_EBX);
949 regs = g_list_prepend (regs, (gpointer)X86_ESI);
950 regs = g_list_prepend (regs, (gpointer)X86_EDI);
956 * mono_arch_regalloc_cost:
958 * Return the cost, in number of memory references, of the action of
959 * allocating the variable VMV into a register during global register
963 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
965 MonoInst *ins = cfg->varinfo [vmv->idx];
967 if (cfg->method->save_lmf)
968 /* The register is already saved */
969 return (ins->opcode == OP_ARG) ? 1 : 0;
971 /* push+pop+possible load if it is an argument */
972 return (ins->opcode == OP_ARG) ? 3 : 2;
976 set_needs_stack_frame (MonoCompile *cfg, gboolean flag)
978 static int inited = FALSE;
979 static int count = 0;
981 if (cfg->arch.need_stack_frame_inited) {
982 g_assert (cfg->arch.need_stack_frame == flag);
986 cfg->arch.need_stack_frame = flag;
987 cfg->arch.need_stack_frame_inited = TRUE;
993 mono_counters_register ("Could eliminate stack frame", MONO_COUNTER_INT|MONO_COUNTER_JIT, &count);
998 //g_print ("will eliminate %s.%s.%s\n", cfg->method->klass->name_space, cfg->method->klass->name, cfg->method->name);
1002 needs_stack_frame (MonoCompile *cfg)
1004 MonoMethodSignature *sig;
1005 MonoMethodHeader *header;
1006 gboolean result = FALSE;
1008 #if defined(__APPLE__)
1009 /*OSX requires stack frame code to have the correct alignment. */
1013 if (cfg->arch.need_stack_frame_inited)
1014 return cfg->arch.need_stack_frame;
1016 header = cfg->header;
1017 sig = mono_method_signature (cfg->method);
1019 if (cfg->disable_omit_fp)
1021 else if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1023 else if (cfg->method->save_lmf)
1025 else if (cfg->stack_offset)
1027 else if (cfg->param_area)
1029 else if (cfg->flags & (MONO_CFG_HAS_CALLS | MONO_CFG_HAS_ALLOCA | MONO_CFG_HAS_TAIL))
1031 else if (header->num_clauses)
1033 else if (sig->param_count + sig->hasthis)
1035 else if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1037 else if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) ||
1038 (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
1041 set_needs_stack_frame (cfg, result);
1043 return cfg->arch.need_stack_frame;
1047 * Set var information according to the calling convention. X86 version.
1048 * The locals var stuff should most likely be split in another method.
1051 mono_arch_allocate_vars (MonoCompile *cfg)
1053 MonoMethodSignature *sig;
1054 MonoMethodHeader *header;
1056 guint32 locals_stack_size, locals_stack_align;
1061 header = cfg->header;
1062 sig = mono_method_signature (cfg->method);
1064 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1066 cfg->frame_reg = X86_EBP;
1069 if (cfg->has_atomic_add_i4 || cfg->has_atomic_exchange_i4) {
1070 /* The opcode implementations use callee-saved regs as scratch regs by pushing and pop-ing them, but that is not async safe */
1071 cfg->used_int_regs |= (1 << X86_EBX) | (1 << X86_EDI) | (1 << X86_ESI);
1074 /* Reserve space to save LMF and caller saved registers */
1076 if (cfg->method->save_lmf) {
1077 /* The LMF var is allocated normally */
1079 if (cfg->used_int_regs & (1 << X86_EBX)) {
1083 if (cfg->used_int_regs & (1 << X86_EDI)) {
1087 if (cfg->used_int_regs & (1 << X86_ESI)) {
1092 switch (cinfo->ret.storage) {
1093 case ArgValuetypeInReg:
1094 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
1096 cfg->ret->opcode = OP_REGOFFSET;
1097 cfg->ret->inst_basereg = X86_EBP;
1098 cfg->ret->inst_offset = - offset;
1104 /* Allocate locals */
1105 offsets = mono_allocate_stack_slots (cfg, TRUE, &locals_stack_size, &locals_stack_align);
1106 if (locals_stack_size > MONO_ARCH_MAX_FRAME_SIZE) {
1107 char *mname = mono_method_full_name (cfg->method, TRUE);
1108 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
1109 cfg->exception_message = g_strdup_printf ("Method %s stack is too big.", mname);
1113 if (locals_stack_align) {
1114 int prev_offset = offset;
1116 offset += (locals_stack_align - 1);
1117 offset &= ~(locals_stack_align - 1);
1119 while (prev_offset < offset) {
1121 mini_gc_set_slot_type_from_fp (cfg, - prev_offset, SLOT_NOREF);
1124 cfg->locals_min_stack_offset = - (offset + locals_stack_size);
1125 cfg->locals_max_stack_offset = - offset;
1127 * EBP is at alignment 8 % MONO_ARCH_FRAME_ALIGNMENT, so if we
1128 * have locals larger than 8 bytes we need to make sure that
1129 * they have the appropriate offset.
1131 if (MONO_ARCH_FRAME_ALIGNMENT > 8 && locals_stack_align > 8)
1132 offset += MONO_ARCH_FRAME_ALIGNMENT - sizeof (gpointer) * 2;
1133 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1134 if (offsets [i] != -1) {
1135 MonoInst *inst = cfg->varinfo [i];
1136 inst->opcode = OP_REGOFFSET;
1137 inst->inst_basereg = X86_EBP;
1138 inst->inst_offset = - (offset + offsets [i]);
1139 //printf ("allocated local %d to ", i); mono_print_tree_nl (inst);
1142 offset += locals_stack_size;
1146 * Allocate arguments+return value
1149 switch (cinfo->ret.storage) {
1151 if (cfg->vret_addr) {
1153 * In the new IR, the cfg->vret_addr variable represents the
1154 * vtype return value.
1156 cfg->vret_addr->opcode = OP_REGOFFSET;
1157 cfg->vret_addr->inst_basereg = cfg->frame_reg;
1158 cfg->vret_addr->inst_offset = cinfo->ret.offset + ARGS_OFFSET;
1159 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1160 printf ("vret_addr =");
1161 mono_print_ins (cfg->vret_addr);
1164 cfg->ret->opcode = OP_REGOFFSET;
1165 cfg->ret->inst_basereg = X86_EBP;
1166 cfg->ret->inst_offset = cinfo->ret.offset + ARGS_OFFSET;
1169 case ArgValuetypeInReg:
1172 cfg->ret->opcode = OP_REGVAR;
1173 cfg->ret->inst_c0 = cinfo->ret.reg;
1174 cfg->ret->dreg = cinfo->ret.reg;
1177 case ArgOnFloatFpStack:
1178 case ArgOnDoubleFpStack:
1181 g_assert_not_reached ();
1184 if (sig->call_convention == MONO_CALL_VARARG) {
1185 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
1186 cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
1189 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1190 ArgInfo *ainfo = &cinfo->args [i];
1191 inst = cfg->args [i];
1192 if (inst->opcode != OP_REGVAR) {
1193 inst->opcode = OP_REGOFFSET;
1194 inst->inst_basereg = X86_EBP;
1196 inst->inst_offset = ainfo->offset + ARGS_OFFSET;
1199 cfg->stack_offset = offset;
1203 mono_arch_create_vars (MonoCompile *cfg)
1206 MonoMethodSignature *sig;
1209 sig = mono_method_signature (cfg->method);
1211 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1212 sig_ret = mini_get_underlying_type (cfg, sig->ret);
1214 if (cinfo->ret.storage == ArgValuetypeInReg)
1215 cfg->ret_var_is_local = TRUE;
1216 if ((cinfo->ret.storage != ArgValuetypeInReg) && (MONO_TYPE_ISSTRUCT (sig_ret) || mini_is_gsharedvt_variable_type (cfg, sig_ret))) {
1217 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
1220 if (cfg->method->save_lmf) {
1221 cfg->create_lmf_var = TRUE;
1224 cfg->lmf_ir_mono_lmf = TRUE;
1228 cfg->arch_eh_jit_info = 1;
1232 * It is expensive to adjust esp for each individual fp argument pushed on the stack
1233 * so we try to do it just once when we have multiple fp arguments in a row.
1234 * We don't use this mechanism generally because for int arguments the generated code
1235 * is slightly bigger and new generation cpus optimize away the dependency chains
1236 * created by push instructions on the esp value.
1237 * fp_arg_setup is the first argument in the execution sequence where the esp register
1240 static G_GNUC_UNUSED int
1241 collect_fp_stack_space (MonoMethodSignature *sig, int start_arg, int *fp_arg_setup)
1246 for (; start_arg < sig->param_count; ++start_arg) {
1247 t = mini_replace_type (sig->params [start_arg]);
1248 if (!t->byref && t->type == MONO_TYPE_R8) {
1249 fp_space += sizeof (double);
1250 *fp_arg_setup = start_arg;
1259 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
1261 MonoMethodSignature *tmp_sig;
1265 * mono_ArgIterator_Setup assumes the signature cookie is
1266 * passed first and all the arguments which were before it are
1267 * passed on the stack after the signature. So compensate by
1268 * passing a different signature.
1270 tmp_sig = mono_metadata_signature_dup (call->signature);
1271 tmp_sig->param_count -= call->signature->sentinelpos;
1272 tmp_sig->sentinelpos = 0;
1273 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
1275 if (cfg->compile_aot) {
1276 sig_reg = mono_alloc_ireg (cfg);
1277 MONO_EMIT_NEW_SIGNATURECONST (cfg, sig_reg, tmp_sig);
1278 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, cinfo->sig_cookie.offset, sig_reg);
1280 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, X86_ESP, cinfo->sig_cookie.offset, tmp_sig);
1286 mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
1291 LLVMCallInfo *linfo;
1292 MonoType *t, *sig_ret;
1294 n = sig->param_count + sig->hasthis;
1296 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1299 linfo = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
1302 * LLVM always uses the native ABI while we use our own ABI, the
1303 * only difference is the handling of vtypes:
1304 * - we only pass/receive them in registers in some cases, and only
1305 * in 1 or 2 integer registers.
1307 if (cinfo->ret.storage == ArgValuetypeInReg) {
1309 cfg->exception_message = g_strdup ("pinvoke + vtypes");
1310 cfg->disable_llvm = TRUE;
1314 cfg->exception_message = g_strdup ("vtype ret in call");
1315 cfg->disable_llvm = TRUE;
1317 linfo->ret.storage = LLVMArgVtypeInReg;
1318 for (j = 0; j < 2; ++j)
1319 linfo->ret.pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, cinfo->ret.pair_storage [j]);
1323 if (mini_type_is_vtype (cfg, sig_ret) && cinfo->ret.storage == ArgInIReg) {
1324 /* Vtype returned using a hidden argument */
1325 linfo->ret.storage = LLVMArgVtypeRetAddr;
1326 linfo->vret_arg_index = cinfo->vret_arg_index;
1329 if (mini_type_is_vtype (cfg, sig_ret) && cinfo->ret.storage != ArgInIReg) {
1331 cfg->exception_message = g_strdup ("vtype ret in call");
1332 cfg->disable_llvm = TRUE;
1335 for (i = 0; i < n; ++i) {
1336 ainfo = cinfo->args + i;
1338 if (i >= sig->hasthis)
1339 t = sig->params [i - sig->hasthis];
1341 t = &mono_defaults.int_class->byval_arg;
1343 linfo->args [i].storage = LLVMArgNone;
1345 switch (ainfo->storage) {
1347 linfo->args [i].storage = LLVMArgInIReg;
1349 case ArgInDoubleSSEReg:
1350 case ArgInFloatSSEReg:
1351 linfo->args [i].storage = LLVMArgInFPReg;
1354 if (mini_type_is_vtype (cfg, t)) {
1355 if (mono_class_value_size (mono_class_from_mono_type (t), NULL) == 0)
1356 /* LLVM seems to allocate argument space for empty structures too */
1357 linfo->args [i].storage = LLVMArgNone;
1359 linfo->args [i].storage = LLVMArgVtypeByVal;
1361 linfo->args [i].storage = LLVMArgInIReg;
1363 if (t->type == MONO_TYPE_R4)
1364 linfo->args [i].storage = LLVMArgInFPReg;
1365 else if (t->type == MONO_TYPE_R8)
1366 linfo->args [i].storage = LLVMArgInFPReg;
1370 case ArgValuetypeInReg:
1372 cfg->exception_message = g_strdup ("pinvoke + vtypes");
1373 cfg->disable_llvm = TRUE;
1377 cfg->exception_message = g_strdup ("vtype arg");
1378 cfg->disable_llvm = TRUE;
1380 linfo->args [i].storage = LLVMArgVtypeInReg;
1381 for (j = 0; j < 2; ++j)
1382 linfo->args [i].pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, ainfo->pair_storage [j]);
1386 linfo->args [i].storage = LLVMArgGSharedVt;
1389 cfg->exception_message = g_strdup ("ainfo->storage");
1390 cfg->disable_llvm = TRUE;
1400 emit_gc_param_slot_def (MonoCompile *cfg, int sp_offset, MonoType *t)
1402 if (cfg->compute_gc_maps) {
1405 /* Needs checking if the feature will be enabled again */
1406 g_assert_not_reached ();
1408 /* On x86, the offsets are from the sp value before the start of the call sequence */
1410 t = &mono_defaults.int_class->byval_arg;
1411 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, sp_offset, t);
1416 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
1420 MonoMethodSignature *sig;
1423 int sentinelpos = 0, sp_offset = 0;
1425 sig = call->signature;
1426 n = sig->param_count + sig->hasthis;
1427 sig_ret = mini_get_underlying_type (cfg, sig->ret);
1429 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1430 call->call_info = cinfo;
1432 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1433 sentinelpos = sig->sentinelpos + (sig->hasthis ? 1 : 0);
1435 if (sig_ret && MONO_TYPE_ISSTRUCT (sig_ret)) {
1436 if (cinfo->ret.storage == ArgValuetypeInReg) {
1438 * Tell the JIT to use a more efficient calling convention: call using
1439 * OP_CALL, compute the result location after the call, and save the
1442 call->vret_in_reg = TRUE;
1443 #if defined(__APPLE__)
1444 if (cinfo->ret.pair_storage [0] == ArgOnDoubleFpStack || cinfo->ret.pair_storage [0] == ArgOnFloatFpStack)
1445 call->vret_in_reg_fp = TRUE;
1448 NULLIFY_INS (call->vret_var);
1452 // FIXME: Emit EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF everywhere
1454 /* Handle the case where there are no implicit arguments */
1455 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sentinelpos)) {
1456 emit_sig_cookie (cfg, call, cinfo);
1457 sp_offset = cinfo->sig_cookie.offset;
1458 emit_gc_param_slot_def (cfg, sp_offset, NULL);
1461 /* Arguments are pushed in the reverse order */
1462 for (i = n - 1; i >= 0; i --) {
1463 ArgInfo *ainfo = cinfo->args + i;
1464 MonoType *orig_type, *t;
1467 if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 1 && i == 0) {
1470 /* Push the vret arg before the first argument */
1471 MONO_INST_NEW (cfg, vtarg, OP_STORE_MEMBASE_REG);
1472 vtarg->type = STACK_MP;
1473 vtarg->inst_destbasereg = X86_ESP;
1474 vtarg->sreg1 = call->vret_var->dreg;
1475 vtarg->inst_offset = cinfo->ret.offset;
1476 MONO_ADD_INS (cfg->cbb, vtarg);
1477 emit_gc_param_slot_def (cfg, cinfo->ret.offset, NULL);
1480 if (i >= sig->hasthis)
1481 t = sig->params [i - sig->hasthis];
1483 t = &mono_defaults.int_class->byval_arg;
1485 t = mini_type_get_underlying_type (cfg->generic_sharing_context, t);
1487 MONO_INST_NEW (cfg, arg, OP_X86_PUSH);
1489 in = call->args [i];
1490 arg->cil_code = in->cil_code;
1491 arg->sreg1 = in->dreg;
1492 arg->type = in->type;
1494 g_assert (in->dreg != -1);
1496 if (ainfo->storage == ArgGSharedVt) {
1497 arg->opcode = OP_OUTARG_VT;
1498 arg->sreg1 = in->dreg;
1499 arg->klass = in->klass;
1500 arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
1501 memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
1503 MONO_ADD_INS (cfg->cbb, arg);
1504 } else if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(t))) {
1508 g_assert (in->klass);
1510 if (t->type == MONO_TYPE_TYPEDBYREF) {
1511 size = sizeof (MonoTypedRef);
1512 align = sizeof (gpointer);
1515 size = mini_type_stack_size_full (cfg->generic_sharing_context, &in->klass->byval_arg, &align, sig->pinvoke);
1519 arg->opcode = OP_OUTARG_VT;
1520 arg->sreg1 = in->dreg;
1521 arg->klass = in->klass;
1522 arg->backend.size = size;
1523 arg->inst_p0 = call;
1524 arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
1525 memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
1527 MONO_ADD_INS (cfg->cbb, arg);
1528 if (ainfo->storage != ArgValuetypeInReg) {
1529 emit_gc_param_slot_def (cfg, ainfo->offset, orig_type);
1533 switch (ainfo->storage) {
1536 if (t->type == MONO_TYPE_R4) {
1537 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, X86_ESP, ainfo->offset, in->dreg);
1539 } else if (t->type == MONO_TYPE_R8) {
1540 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, X86_ESP, ainfo->offset, in->dreg);
1542 } else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_U8) {
1543 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, ainfo->offset + 4, in->dreg + 2);
1544 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, ainfo->offset, in->dreg + 1);
1547 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, ainfo->offset, in->dreg);
1551 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, ainfo->offset, in->dreg);
1556 arg->opcode = OP_MOVE;
1557 arg->dreg = ainfo->reg;
1558 MONO_ADD_INS (cfg->cbb, arg);
1562 g_assert_not_reached ();
1565 if (cfg->compute_gc_maps) {
1567 /* FIXME: The == STACK_OBJ check might be fragile ? */
1568 if (sig->hasthis && i == 0 && call->args [i]->type == STACK_OBJ) {
1570 if (call->need_unbox_trampoline)
1571 /* The unbox trampoline transforms this into a managed pointer */
1572 emit_gc_param_slot_def (cfg, ainfo->offset, &mono_defaults.int_class->this_arg);
1574 emit_gc_param_slot_def (cfg, ainfo->offset, &mono_defaults.object_class->byval_arg);
1576 emit_gc_param_slot_def (cfg, ainfo->offset, orig_type);
1580 for (j = 0; j < argsize; j += 4)
1581 emit_gc_param_slot_def (cfg, ainfo->offset + j, NULL);
1586 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sentinelpos)) {
1587 /* Emit the signature cookie just before the implicit arguments */
1588 emit_sig_cookie (cfg, call, cinfo);
1589 emit_gc_param_slot_def (cfg, cinfo->sig_cookie.offset, NULL);
1593 if (sig_ret && (MONO_TYPE_ISSTRUCT (sig_ret) || cinfo->vtype_retaddr)) {
1596 if (cinfo->ret.storage == ArgValuetypeInReg) {
1599 else if (cinfo->ret.storage == ArgInIReg) {
1601 /* The return address is passed in a register */
1602 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
1603 vtarg->sreg1 = call->inst.dreg;
1604 vtarg->dreg = mono_alloc_ireg (cfg);
1605 MONO_ADD_INS (cfg->cbb, vtarg);
1607 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
1608 } else if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 0) {
1609 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, cinfo->ret.offset, call->vret_var->dreg);
1610 emit_gc_param_slot_def (cfg, cinfo->ret.offset, NULL);
1614 call->stack_usage = cinfo->stack_usage;
1615 call->stack_align_amount = cinfo->stack_align_amount;
1619 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
1621 MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
1622 ArgInfo *ainfo = ins->inst_p1;
1623 int size = ins->backend.size;
1625 if (ainfo->storage == ArgValuetypeInReg) {
1626 int dreg = mono_alloc_ireg (cfg);
1629 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, dreg, src->dreg, 0);
1632 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, dreg, src->dreg, 0);
1635 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, src->dreg, 0);
1639 g_assert_not_reached ();
1641 mono_call_inst_add_outarg_reg (cfg, call, dreg, ainfo->reg, FALSE);
1644 if (cfg->gsharedvt && mini_is_gsharedvt_klass (cfg, ins->klass)) {
1646 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, ainfo->offset, src->dreg);
1647 } else if (size <= 4) {
1648 int dreg = mono_alloc_ireg (cfg);
1649 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, src->dreg, 0);
1650 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, ainfo->offset, dreg);
1651 } else if (size <= 20) {
1652 mini_emit_memcpy (cfg, X86_ESP, ainfo->offset, src->dreg, 0, size, 4);
1654 // FIXME: Code growth
1655 mini_emit_memcpy (cfg, X86_ESP, ainfo->offset, src->dreg, 0, size, 4);
1661 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
1663 MonoType *ret = mini_type_get_underlying_type (cfg->generic_sharing_context, mono_method_signature (method)->ret);
1666 if (ret->type == MONO_TYPE_R4) {
1667 if (COMPILE_LLVM (cfg))
1668 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
1671 } else if (ret->type == MONO_TYPE_R8) {
1672 if (COMPILE_LLVM (cfg))
1673 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
1676 } else if (ret->type == MONO_TYPE_I8 || ret->type == MONO_TYPE_U8) {
1677 if (COMPILE_LLVM (cfg))
1678 MONO_EMIT_NEW_UNALU (cfg, OP_LMOVE, cfg->ret->dreg, val->dreg);
1680 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, X86_EAX, val->dreg + 1);
1681 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, X86_EDX, val->dreg + 2);
1687 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
1691 * Allow tracing to work with this interface (with an optional argument)
1694 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1698 g_assert (MONO_ARCH_FRAME_ALIGNMENT >= 8);
1699 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 8);
1701 /* if some args are passed in registers, we need to save them here */
1702 x86_push_reg (code, X86_EBP);
1704 if (cfg->compile_aot) {
1705 x86_push_imm (code, cfg->method);
1706 x86_mov_reg_imm (code, X86_EAX, func);
1707 x86_call_reg (code, X86_EAX);
1709 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
1710 x86_push_imm (code, cfg->method);
1711 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1712 x86_call_code (code, 0);
1714 x86_alu_reg_imm (code, X86_ADD, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT);
1728 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
1731 int arg_size = 0, stack_usage = 0, save_mode = SAVE_NONE;
1732 MonoMethod *method = cfg->method;
1733 MonoType *ret_type = mini_type_get_underlying_type (cfg->generic_sharing_context, mono_method_signature (method)->ret);
1735 switch (ret_type->type) {
1736 case MONO_TYPE_VOID:
1737 /* special case string .ctor icall */
1738 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class) {
1739 save_mode = SAVE_EAX;
1740 stack_usage = enable_arguments ? 8 : 4;
1742 save_mode = SAVE_NONE;
1746 save_mode = SAVE_EAX_EDX;
1747 stack_usage = enable_arguments ? 16 : 8;
1751 save_mode = SAVE_FP;
1752 stack_usage = enable_arguments ? 16 : 8;
1754 case MONO_TYPE_GENERICINST:
1755 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
1756 save_mode = SAVE_EAX;
1757 stack_usage = enable_arguments ? 8 : 4;
1761 case MONO_TYPE_VALUETYPE:
1762 // FIXME: Handle SMALL_STRUCT_IN_REG here for proper alignment on darwin-x86
1763 save_mode = SAVE_STRUCT;
1764 stack_usage = enable_arguments ? 4 : 0;
1767 save_mode = SAVE_EAX;
1768 stack_usage = enable_arguments ? 8 : 4;
1772 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - stack_usage - 4);
1774 switch (save_mode) {
1776 x86_push_reg (code, X86_EDX);
1777 x86_push_reg (code, X86_EAX);
1778 if (enable_arguments) {
1779 x86_push_reg (code, X86_EDX);
1780 x86_push_reg (code, X86_EAX);
1785 x86_push_reg (code, X86_EAX);
1786 if (enable_arguments) {
1787 x86_push_reg (code, X86_EAX);
1792 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1793 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1794 if (enable_arguments) {
1795 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1796 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1801 if (enable_arguments) {
1802 x86_push_membase (code, X86_EBP, 8);
1811 if (cfg->compile_aot) {
1812 x86_push_imm (code, method);
1813 x86_mov_reg_imm (code, X86_EAX, func);
1814 x86_call_reg (code, X86_EAX);
1816 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
1817 x86_push_imm (code, method);
1818 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1819 x86_call_code (code, 0);
1822 x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
1824 switch (save_mode) {
1826 x86_pop_reg (code, X86_EAX);
1827 x86_pop_reg (code, X86_EDX);
1830 x86_pop_reg (code, X86_EAX);
1833 x86_fld_membase (code, X86_ESP, 0, TRUE);
1834 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1841 x86_alu_reg_imm (code, X86_ADD, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - stack_usage);
1846 #define EMIT_COND_BRANCH(ins,cond,sign) \
1847 if (ins->inst_true_bb->native_offset) { \
1848 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
1850 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1851 if ((cfg->opt & MONO_OPT_BRANCH) && \
1852 x86_is_imm8 (ins->inst_true_bb->max_offset - cpos)) \
1853 x86_branch8 (code, cond, 0, sign); \
1855 x86_branch32 (code, cond, 0, sign); \
1859 * Emit an exception if condition is fail and
1860 * if possible do a directly branch to target
1862 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
1864 MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
1865 if (tins == NULL) { \
1866 mono_add_patch_info (cfg, code - cfg->native_code, \
1867 MONO_PATCH_INFO_EXC, exc_name); \
1868 x86_branch32 (code, cond, 0, signed); \
1870 EMIT_COND_BRANCH (tins, cond, signed); \
1874 #define EMIT_FPCOMPARE(code) do { \
1875 x86_fcompp (code); \
1876 x86_fnstsw (code); \
1881 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data)
1883 gboolean needs_paddings = TRUE;
1885 MonoJumpInfo *jinfo = NULL;
1887 if (cfg->abs_patches) {
1888 jinfo = g_hash_table_lookup (cfg->abs_patches, data);
1889 if (jinfo && jinfo->type == MONO_PATCH_INFO_JIT_ICALL_ADDR)
1890 needs_paddings = FALSE;
1893 if (cfg->compile_aot)
1894 needs_paddings = FALSE;
1895 /*The address must be 4 bytes aligned to avoid spanning multiple cache lines.
1896 This is required for code patching to be safe on SMP machines.
1898 pad_size = (guint32)(code + 1 - cfg->native_code) & 0x3;
1899 #ifndef __native_client_codegen__
1900 if (needs_paddings && pad_size)
1901 x86_padding (code, 4 - pad_size);
1904 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
1905 x86_call_code (code, 0);
1910 #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)))
1913 * mono_peephole_pass_1:
1915 * Perform peephole opts which should/can be performed before local regalloc
1918 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
1922 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
1923 MonoInst *last_ins = mono_inst_prev (ins, FILTER_IL_SEQ_POINT);
1925 switch (ins->opcode) {
1928 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS)) {
1930 * X86_LEA is like ADD, but doesn't have the
1931 * sreg1==dreg restriction.
1933 ins->opcode = OP_X86_LEA_MEMBASE;
1934 ins->inst_basereg = ins->sreg1;
1935 } else if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1936 ins->opcode = OP_X86_INC_REG;
1940 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS)) {
1941 ins->opcode = OP_X86_LEA_MEMBASE;
1942 ins->inst_basereg = ins->sreg1;
1943 ins->inst_imm = -ins->inst_imm;
1944 } else if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1945 ins->opcode = OP_X86_DEC_REG;
1947 case OP_COMPARE_IMM:
1948 case OP_ICOMPARE_IMM:
1949 /* OP_COMPARE_IMM (reg, 0)
1951 * OP_X86_TEST_NULL (reg)
1954 ins->opcode = OP_X86_TEST_NULL;
1956 case OP_X86_COMPARE_MEMBASE_IMM:
1958 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1959 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
1961 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1962 * OP_COMPARE_IMM reg, imm
1964 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
1966 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
1967 ins->inst_basereg == last_ins->inst_destbasereg &&
1968 ins->inst_offset == last_ins->inst_offset) {
1969 ins->opcode = OP_COMPARE_IMM;
1970 ins->sreg1 = last_ins->sreg1;
1972 /* check if we can remove cmp reg,0 with test null */
1974 ins->opcode = OP_X86_TEST_NULL;
1978 case OP_X86_PUSH_MEMBASE:
1979 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG ||
1980 last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1981 ins->inst_basereg == last_ins->inst_destbasereg &&
1982 ins->inst_offset == last_ins->inst_offset) {
1983 ins->opcode = OP_X86_PUSH;
1984 ins->sreg1 = last_ins->sreg1;
1989 mono_peephole_ins (bb, ins);
1994 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
1998 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
1999 switch (ins->opcode) {
2001 /* reg = 0 -> XOR (reg, reg) */
2002 /* XOR sets cflags on x86, so we cant do it always */
2003 if (ins->inst_c0 == 0 && (!ins->next || (ins->next && INST_IGNORES_CFLAGS (ins->next->opcode)))) {
2006 ins->opcode = OP_IXOR;
2007 ins->sreg1 = ins->dreg;
2008 ins->sreg2 = ins->dreg;
2011 * Convert succeeding STORE_MEMBASE_IMM 0 ins to STORE_MEMBASE_REG
2012 * since it takes 3 bytes instead of 7.
2014 for (ins2 = mono_inst_next (ins, FILTER_IL_SEQ_POINT); ins2; ins2 = ins2->next) {
2015 if ((ins2->opcode == OP_STORE_MEMBASE_IMM) && (ins2->inst_imm == 0)) {
2016 ins2->opcode = OP_STORE_MEMBASE_REG;
2017 ins2->sreg1 = ins->dreg;
2019 else if ((ins2->opcode == OP_STOREI4_MEMBASE_IMM) && (ins2->inst_imm == 0)) {
2020 ins2->opcode = OP_STOREI4_MEMBASE_REG;
2021 ins2->sreg1 = ins->dreg;
2023 else if ((ins2->opcode == OP_STOREI1_MEMBASE_IMM) || (ins2->opcode == OP_STOREI2_MEMBASE_IMM)) {
2024 /* Continue iteration */
2033 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
2034 ins->opcode = OP_X86_INC_REG;
2038 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
2039 ins->opcode = OP_X86_DEC_REG;
2043 mono_peephole_ins (bb, ins);
2048 * mono_arch_lowering_pass:
2050 * Converts complex opcodes into simpler ones so that each IR instruction
2051 * corresponds to one machine instruction.
2054 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
2056 MonoInst *ins, *next;
2059 * FIXME: Need to add more instructions, but the current machine
2060 * description can't model some parts of the composite instructions like
2063 MONO_BB_FOR_EACH_INS_SAFE (bb, next, ins) {
2064 switch (ins->opcode) {
2067 case OP_IDIV_UN_IMM:
2068 case OP_IREM_UN_IMM:
2070 * Keep the cases where we could generated optimized code, otherwise convert
2071 * to the non-imm variant.
2073 if ((ins->opcode == OP_IREM_IMM) && mono_is_power_of_two (ins->inst_imm) >= 0)
2075 mono_decompose_op_imm (cfg, bb, ins);
2082 bb->max_vreg = cfg->next_vreg;
2086 branch_cc_table [] = {
2087 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
2088 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
2089 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
2092 /* Maps CMP_... constants to X86_CC_... constants */
2095 X86_CC_EQ, X86_CC_NE, X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT,
2096 X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT
2100 cc_signed_table [] = {
2101 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
2102 FALSE, FALSE, FALSE, FALSE
2105 static unsigned char*
2106 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
2108 #define XMM_TEMP_REG 0
2109 /*This SSE2 optimization must not be done which OPT_SIMD in place as it clobbers xmm0.*/
2110 /*The xmm pass decomposes OP_FCONV_ ops anyway anyway.*/
2111 if (cfg->opt & MONO_OPT_SSE2 && size < 8 && !(cfg->opt & MONO_OPT_SIMD)) {
2112 /* optimize by assigning a local var for this use so we avoid
2113 * the stack manipulations */
2114 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2115 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
2116 x86_movsd_reg_membase (code, XMM_TEMP_REG, X86_ESP, 0);
2117 x86_cvttsd2si (code, dreg, XMM_TEMP_REG);
2118 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
2120 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
2122 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
2125 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
2126 x86_fnstcw_membase(code, X86_ESP, 0);
2127 x86_mov_reg_membase (code, dreg, X86_ESP, 0, 2);
2128 x86_alu_reg_imm (code, X86_OR, dreg, 0xc00);
2129 x86_mov_membase_reg (code, X86_ESP, 2, dreg, 2);
2130 x86_fldcw_membase (code, X86_ESP, 2);
2132 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2133 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
2134 x86_pop_reg (code, dreg);
2135 /* FIXME: need the high register
2136 * x86_pop_reg (code, dreg_high);
2139 x86_push_reg (code, X86_EAX); // SP = SP - 4
2140 x86_fist_pop_membase (code, X86_ESP, 0, FALSE);
2141 x86_pop_reg (code, dreg);
2143 x86_fldcw_membase (code, X86_ESP, 0);
2144 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2147 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
2149 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
2153 static unsigned char*
2154 mono_emit_stack_alloc (MonoCompile *cfg, guchar *code, MonoInst* tree)
2156 int sreg = tree->sreg1;
2157 int need_touch = FALSE;
2159 #if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
2168 * If requested stack size is larger than one page,
2169 * perform stack-touch operation
2172 * Generate stack probe code.
2173 * Under Windows, it is necessary to allocate one page at a time,
2174 * "touching" stack after each successful sub-allocation. This is
2175 * because of the way stack growth is implemented - there is a
2176 * guard page before the lowest stack page that is currently commited.
2177 * Stack normally grows sequentially so OS traps access to the
2178 * guard page and commits more pages when needed.
2180 x86_test_reg_imm (code, sreg, ~0xFFF);
2181 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2183 br[2] = code; /* loop */
2184 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
2185 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
2188 * By the end of the loop, sreg2 is smaller than 0x1000, so the init routine
2189 * that follows only initializes the last part of the area.
2191 /* Same as the init code below with size==0x1000 */
2192 if (tree->flags & MONO_INST_INIT) {
2193 x86_push_reg (code, X86_EAX);
2194 x86_push_reg (code, X86_ECX);
2195 x86_push_reg (code, X86_EDI);
2196 x86_mov_reg_imm (code, X86_ECX, (0x1000 >> 2));
2197 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2198 if (cfg->param_area)
2199 x86_lea_membase (code, X86_EDI, X86_ESP, 12 + ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
2201 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
2203 x86_prefix (code, X86_REP_PREFIX);
2205 x86_pop_reg (code, X86_EDI);
2206 x86_pop_reg (code, X86_ECX);
2207 x86_pop_reg (code, X86_EAX);
2210 x86_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
2211 x86_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
2212 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
2213 x86_patch (br[3], br[2]);
2214 x86_test_reg_reg (code, sreg, sreg);
2215 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2216 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
2218 br[1] = code; x86_jump8 (code, 0);
2220 x86_patch (br[0], code);
2221 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
2222 x86_patch (br[1], code);
2223 x86_patch (br[4], code);
2226 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
2228 if (tree->flags & MONO_INST_INIT) {
2230 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
2231 x86_push_reg (code, X86_EAX);
2234 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
2235 x86_push_reg (code, X86_ECX);
2238 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
2239 x86_push_reg (code, X86_EDI);
2243 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
2244 if (sreg != X86_ECX)
2245 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
2246 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2248 if (cfg->param_area)
2249 x86_lea_membase (code, X86_EDI, X86_ESP, offset + ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
2251 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
2253 x86_prefix (code, X86_REP_PREFIX);
2256 if (tree->dreg != X86_EDI && sreg != X86_EDI)
2257 x86_pop_reg (code, X86_EDI);
2258 if (tree->dreg != X86_ECX && sreg != X86_ECX)
2259 x86_pop_reg (code, X86_ECX);
2260 if (tree->dreg != X86_EAX && sreg != X86_EAX)
2261 x86_pop_reg (code, X86_EAX);
2268 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
2270 /* Move return value to the target register */
2271 switch (ins->opcode) {
2274 case OP_CALL_MEMBASE:
2275 if (ins->dreg != X86_EAX)
2276 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
2286 static int tls_gs_offset;
2290 mono_x86_have_tls_get (void)
2293 static gboolean have_tls_get = FALSE;
2294 static gboolean inited = FALSE;
2298 return have_tls_get;
2300 ins = (guint32*)pthread_getspecific;
2302 * We're looking for these two instructions:
2304 * mov 0x4(%esp),%eax
2305 * mov %gs:[offset](,%eax,4),%eax
2307 have_tls_get = ins [0] == 0x0424448b && ins [1] == 0x85048b65;
2308 tls_gs_offset = ins [2];
2312 return have_tls_get;
2313 #elif defined(TARGET_ANDROID)
2321 mono_x86_emit_tls_set (guint8* code, int sreg, int tls_offset)
2323 #if defined(__APPLE__)
2324 x86_prefix (code, X86_GS_PREFIX);
2325 x86_mov_mem_reg (code, tls_gs_offset + (tls_offset * 4), sreg, 4);
2326 #elif defined(TARGET_WIN32)
2327 g_assert_not_reached ();
2329 x86_prefix (code, X86_GS_PREFIX);
2330 x86_mov_mem_reg (code, tls_offset, sreg, 4);
2336 * mono_x86_emit_tls_get:
2337 * @code: buffer to store code to
2338 * @dreg: hard register where to place the result
2339 * @tls_offset: offset info
2341 * mono_x86_emit_tls_get emits in @code the native code that puts in
2342 * the dreg register the item in the thread local storage identified
2345 * Returns: a pointer to the end of the stored code
2348 mono_x86_emit_tls_get (guint8* code, int dreg, int tls_offset)
2350 #if defined(__APPLE__)
2351 x86_prefix (code, X86_GS_PREFIX);
2352 x86_mov_reg_mem (code, dreg, tls_gs_offset + (tls_offset * 4), 4);
2353 #elif defined(TARGET_WIN32)
2355 * See the Under the Hood article in the May 1996 issue of Microsoft Systems
2356 * Journal and/or a disassembly of the TlsGet () function.
2358 x86_prefix (code, X86_FS_PREFIX);
2359 x86_mov_reg_mem (code, dreg, 0x18, 4);
2360 if (tls_offset < 64) {
2361 x86_mov_reg_membase (code, dreg, dreg, 3600 + (tls_offset * 4), 4);
2365 g_assert (tls_offset < 0x440);
2366 /* Load TEB->TlsExpansionSlots */
2367 x86_mov_reg_membase (code, dreg, dreg, 0xf94, 4);
2368 x86_test_reg_reg (code, dreg, dreg);
2370 x86_branch (code, X86_CC_EQ, code, TRUE);
2371 x86_mov_reg_membase (code, dreg, dreg, (tls_offset * 4) - 0x100, 4);
2372 x86_patch (buf [0], code);
2375 if (optimize_for_xen) {
2376 x86_prefix (code, X86_GS_PREFIX);
2377 x86_mov_reg_mem (code, dreg, 0, 4);
2378 x86_mov_reg_membase (code, dreg, dreg, tls_offset, 4);
2380 x86_prefix (code, X86_GS_PREFIX);
2381 x86_mov_reg_mem (code, dreg, tls_offset, 4);
2388 emit_tls_get_reg (guint8* code, int dreg, int offset_reg)
2390 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
2391 #if defined(__APPLE__) || defined(__linux__)
2392 if (dreg != offset_reg)
2393 x86_mov_reg_reg (code, dreg, offset_reg, sizeof (mgreg_t));
2394 x86_prefix (code, X86_GS_PREFIX);
2395 x86_mov_reg_membase (code, dreg, dreg, 0, sizeof (mgreg_t));
2397 g_assert_not_reached ();
2403 mono_x86_emit_tls_get_reg (guint8* code, int dreg, int offset_reg)
2405 return emit_tls_get_reg (code, dreg, offset_reg);
2409 emit_tls_set_reg (guint8* code, int sreg, int offset_reg)
2411 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
2413 g_assert_not_reached ();
2414 #elif defined(__APPLE__) || defined(__linux__)
2415 x86_prefix (code, X86_GS_PREFIX);
2416 x86_mov_membase_reg (code, offset_reg, 0, sreg, sizeof (mgreg_t));
2418 g_assert_not_reached ();
2424 * mono_arch_translate_tls_offset:
2426 * Translate the TLS offset OFFSET computed by MONO_THREAD_VAR_OFFSET () into a format usable by OP_TLS_GET_REG/OP_TLS_SET_REG.
2429 mono_arch_translate_tls_offset (int offset)
2432 return tls_gs_offset + (offset * 4);
2441 * Emit code to initialize an LMF structure at LMF_OFFSET.
2444 emit_setup_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset, int cfa_offset)
2446 /* save all caller saved regs */
2447 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, ebx), X86_EBX, sizeof (mgreg_t));
2448 mono_emit_unwind_op_offset (cfg, code, X86_EBX, - cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, ebx));
2449 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, edi), X86_EDI, sizeof (mgreg_t));
2450 mono_emit_unwind_op_offset (cfg, code, X86_EDI, - cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, edi));
2451 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, esi), X86_ESI, sizeof (mgreg_t));
2452 mono_emit_unwind_op_offset (cfg, code, X86_ESI, - cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, esi));
2453 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, ebp), X86_EBP, sizeof (mgreg_t));
2455 /* save the current IP */
2456 if (cfg->compile_aot) {
2457 /* This pushes the current ip */
2458 x86_call_imm (code, 0);
2459 x86_pop_reg (code, X86_EAX);
2461 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
2462 x86_mov_reg_imm (code, X86_EAX, 0);
2464 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, eip), X86_EAX, sizeof (mgreg_t));
2466 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, eip), SLOT_NOREF);
2467 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, ebp), SLOT_NOREF);
2468 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, esi), SLOT_NOREF);
2469 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, edi), SLOT_NOREF);
2470 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, ebx), SLOT_NOREF);
2471 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, esp), SLOT_NOREF);
2472 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, method), SLOT_NOREF);
2473 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, lmf_addr), SLOT_NOREF);
2474 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), SLOT_NOREF);
2479 #define REAL_PRINT_REG(text,reg) \
2480 mono_assert (reg >= 0); \
2481 x86_push_reg (code, X86_EAX); \
2482 x86_push_reg (code, X86_EDX); \
2483 x86_push_reg (code, X86_ECX); \
2484 x86_push_reg (code, reg); \
2485 x86_push_imm (code, reg); \
2486 x86_push_imm (code, text " %d %p\n"); \
2487 x86_mov_reg_imm (code, X86_EAX, printf); \
2488 x86_call_reg (code, X86_EAX); \
2489 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 3*4); \
2490 x86_pop_reg (code, X86_ECX); \
2491 x86_pop_reg (code, X86_EDX); \
2492 x86_pop_reg (code, X86_EAX);
2494 /* REAL_PRINT_REG does not appear to be used, and was not adapted to work with Native Client. */
2495 #ifdef __native__client_codegen__
2496 #define REAL_PRINT_REG(text, reg) g_assert_not_reached()
2499 /* benchmark and set based on cpu */
2500 #define LOOP_ALIGNMENT 8
2501 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
2505 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2510 guint8 *code = cfg->native_code + cfg->code_len;
2513 if (cfg->opt & MONO_OPT_LOOP) {
2514 int pad, align = LOOP_ALIGNMENT;
2515 /* set alignment depending on cpu */
2516 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
2518 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
2519 x86_padding (code, pad);
2520 cfg->code_len += pad;
2521 bb->native_offset = cfg->code_len;
2524 #ifdef __native_client_codegen__
2526 /* For Native Client, all indirect call/jump targets must be */
2527 /* 32-byte aligned. Exception handler blocks are jumped to */
2528 /* indirectly as well. */
2529 gboolean bb_needs_alignment = (bb->flags & BB_INDIRECT_JUMP_TARGET) ||
2530 (bb->flags & BB_EXCEPTION_HANDLER);
2532 /* if ((cfg->code_len & kNaClAlignmentMask) != 0) { */
2533 if ( bb_needs_alignment && ((cfg->code_len & kNaClAlignmentMask) != 0)) {
2534 int pad = kNaClAlignment - (cfg->code_len & kNaClAlignmentMask);
2535 if (pad != kNaClAlignment) code = mono_arch_nacl_pad(code, pad);
2536 cfg->code_len += pad;
2537 bb->native_offset = cfg->code_len;
2540 #endif /* __native_client_codegen__ */
2541 if (cfg->verbose_level > 2)
2542 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2544 cpos = bb->max_offset;
2546 if ((cfg->prof_options & MONO_PROFILE_COVERAGE) && cfg->coverage_info) {
2547 MonoProfileCoverageInfo *cov = cfg->coverage_info;
2548 g_assert (!cfg->compile_aot);
2551 cov->data [bb->dfn].cil_code = bb->cil_code;
2552 /* this is not thread save, but good enough */
2553 x86_inc_mem (code, &cov->data [bb->dfn].count);
2556 offset = code - cfg->native_code;
2558 mono_debug_open_block (cfg, bb, offset);
2560 if (mono_break_at_bb_method && mono_method_desc_full_match (mono_break_at_bb_method, cfg->method) && bb->block_num == mono_break_at_bb_bb_num)
2561 x86_breakpoint (code);
2563 MONO_BB_FOR_EACH_INS (bb, ins) {
2564 offset = code - cfg->native_code;
2566 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
2568 #define EXTRA_CODE_SPACE (NACL_SIZE (16, 16 + kNaClAlignment))
2570 if (G_UNLIKELY (offset > (cfg->code_size - max_len - EXTRA_CODE_SPACE))) {
2571 cfg->code_size *= 2;
2572 cfg->native_code = mono_realloc_native_code(cfg);
2573 code = cfg->native_code + offset;
2574 cfg->stat_code_reallocs++;
2577 if (cfg->debug_info)
2578 mono_debug_record_line_number (cfg, ins, offset);
2580 switch (ins->opcode) {
2582 x86_mul_reg (code, ins->sreg2, TRUE);
2585 x86_mul_reg (code, ins->sreg2, FALSE);
2587 case OP_X86_SETEQ_MEMBASE:
2588 case OP_X86_SETNE_MEMBASE:
2589 x86_set_membase (code, ins->opcode == OP_X86_SETEQ_MEMBASE ? X86_CC_EQ : X86_CC_NE,
2590 ins->inst_basereg, ins->inst_offset, TRUE);
2592 case OP_STOREI1_MEMBASE_IMM:
2593 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
2595 case OP_STOREI2_MEMBASE_IMM:
2596 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
2598 case OP_STORE_MEMBASE_IMM:
2599 case OP_STOREI4_MEMBASE_IMM:
2600 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
2602 case OP_STOREI1_MEMBASE_REG:
2603 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
2605 case OP_STOREI2_MEMBASE_REG:
2606 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
2608 case OP_STORE_MEMBASE_REG:
2609 case OP_STOREI4_MEMBASE_REG:
2610 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
2612 case OP_STORE_MEM_IMM:
2613 x86_mov_mem_imm (code, ins->inst_p0, ins->inst_c0, 4);
2616 x86_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
2620 /* These are created by the cprop pass so they use inst_imm as the source */
2621 x86_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
2624 x86_widen_mem (code, ins->dreg, ins->inst_imm, FALSE, FALSE);
2627 x86_widen_mem (code, ins->dreg, ins->inst_imm, FALSE, TRUE);
2629 case OP_LOAD_MEMBASE:
2630 case OP_LOADI4_MEMBASE:
2631 case OP_LOADU4_MEMBASE:
2632 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
2634 case OP_LOADU1_MEMBASE:
2635 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
2637 case OP_LOADI1_MEMBASE:
2638 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
2640 case OP_LOADU2_MEMBASE:
2641 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
2643 case OP_LOADI2_MEMBASE:
2644 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
2646 case OP_ICONV_TO_I1:
2648 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2650 case OP_ICONV_TO_I2:
2652 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2654 case OP_ICONV_TO_U1:
2655 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
2657 case OP_ICONV_TO_U2:
2658 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
2662 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
2664 case OP_COMPARE_IMM:
2665 case OP_ICOMPARE_IMM:
2666 x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
2668 case OP_X86_COMPARE_MEMBASE_REG:
2669 x86_alu_membase_reg (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2671 case OP_X86_COMPARE_MEMBASE_IMM:
2672 x86_alu_membase_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2674 case OP_X86_COMPARE_MEMBASE8_IMM:
2675 x86_alu_membase8_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2677 case OP_X86_COMPARE_REG_MEMBASE:
2678 x86_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
2680 case OP_X86_COMPARE_MEM_IMM:
2681 x86_alu_mem_imm (code, X86_CMP, ins->inst_offset, ins->inst_imm);
2683 case OP_X86_TEST_NULL:
2684 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2686 case OP_X86_ADD_MEMBASE_IMM:
2687 x86_alu_membase_imm (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2689 case OP_X86_ADD_REG_MEMBASE:
2690 x86_alu_reg_membase (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset);
2692 case OP_X86_SUB_MEMBASE_IMM:
2693 x86_alu_membase_imm (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2695 case OP_X86_SUB_REG_MEMBASE:
2696 x86_alu_reg_membase (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset);
2698 case OP_X86_AND_MEMBASE_IMM:
2699 x86_alu_membase_imm (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2701 case OP_X86_OR_MEMBASE_IMM:
2702 x86_alu_membase_imm (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2704 case OP_X86_XOR_MEMBASE_IMM:
2705 x86_alu_membase_imm (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2707 case OP_X86_ADD_MEMBASE_REG:
2708 x86_alu_membase_reg (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2710 case OP_X86_SUB_MEMBASE_REG:
2711 x86_alu_membase_reg (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2713 case OP_X86_AND_MEMBASE_REG:
2714 x86_alu_membase_reg (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2716 case OP_X86_OR_MEMBASE_REG:
2717 x86_alu_membase_reg (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2719 case OP_X86_XOR_MEMBASE_REG:
2720 x86_alu_membase_reg (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2722 case OP_X86_INC_MEMBASE:
2723 x86_inc_membase (code, ins->inst_basereg, ins->inst_offset);
2725 case OP_X86_INC_REG:
2726 x86_inc_reg (code, ins->dreg);
2728 case OP_X86_DEC_MEMBASE:
2729 x86_dec_membase (code, ins->inst_basereg, ins->inst_offset);
2731 case OP_X86_DEC_REG:
2732 x86_dec_reg (code, ins->dreg);
2734 case OP_X86_MUL_REG_MEMBASE:
2735 x86_imul_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
2737 case OP_X86_AND_REG_MEMBASE:
2738 x86_alu_reg_membase (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset);
2740 case OP_X86_OR_REG_MEMBASE:
2741 x86_alu_reg_membase (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset);
2743 case OP_X86_XOR_REG_MEMBASE:
2744 x86_alu_reg_membase (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset);
2747 x86_breakpoint (code);
2749 case OP_RELAXED_NOP:
2750 x86_prefix (code, X86_REP_PREFIX);
2758 case OP_DUMMY_STORE:
2759 case OP_DUMMY_ICONST:
2760 case OP_DUMMY_R8CONST:
2761 case OP_NOT_REACHED:
2764 case OP_IL_SEQ_POINT:
2765 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
2767 case OP_SEQ_POINT: {
2770 if (cfg->compile_aot)
2774 * Read from the single stepping trigger page. This will cause a
2775 * SIGSEGV when single stepping is enabled.
2776 * We do this _before_ the breakpoint, so single stepping after
2777 * a breakpoint is hit will step to the next IL offset.
2779 if (ins->flags & MONO_INST_SINGLE_STEP_LOC)
2780 x86_alu_reg_mem (code, X86_CMP, X86_EAX, (guint32)ss_trigger_page);
2782 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
2785 * A placeholder for a possible breakpoint inserted by
2786 * mono_arch_set_breakpoint ().
2788 for (i = 0; i < 6; ++i)
2791 * Add an additional nop so skipping the bp doesn't cause the ip to point
2792 * to another IL offset.
2800 x86_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
2804 x86_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
2809 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
2813 x86_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
2818 x86_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
2822 x86_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
2827 x86_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
2831 x86_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
2834 x86_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
2838 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
2842 #if defined( __native_client_codegen__ )
2843 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
2844 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
2847 * The code is the same for div/rem, the allocator will allocate dreg
2848 * to RAX/RDX as appropriate.
2850 if (ins->sreg2 == X86_EDX) {
2851 /* cdq clobbers this */
2852 x86_push_reg (code, ins->sreg2);
2854 x86_div_membase (code, X86_ESP, 0, TRUE);
2855 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2858 x86_div_reg (code, ins->sreg2, TRUE);
2863 #if defined( __native_client_codegen__ )
2864 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
2865 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
2867 if (ins->sreg2 == X86_EDX) {
2868 x86_push_reg (code, ins->sreg2);
2869 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2870 x86_div_membase (code, X86_ESP, 0, FALSE);
2871 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2873 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2874 x86_div_reg (code, ins->sreg2, FALSE);
2878 #if defined( __native_client_codegen__ )
2879 if (ins->inst_imm == 0) {
2880 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "DivideByZeroException");
2881 x86_jump32 (code, 0);
2885 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2887 x86_div_reg (code, ins->sreg2, TRUE);
2890 int power = mono_is_power_of_two (ins->inst_imm);
2892 g_assert (ins->sreg1 == X86_EAX);
2893 g_assert (ins->dreg == X86_EAX);
2894 g_assert (power >= 0);
2897 /* Based on http://compilers.iecc.com/comparch/article/93-04-079 */
2899 x86_alu_reg_imm (code, X86_AND, X86_EAX, 1);
2901 * If the divident is >= 0, this does not nothing. If it is positive, it
2902 * it transforms %eax=0 into %eax=0, and %eax=1 into %eax=-1.
2904 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EDX);
2905 x86_alu_reg_reg (code, X86_SUB, X86_EAX, X86_EDX);
2906 } else if (power == 0) {
2907 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2909 /* Based on gcc code */
2911 /* Add compensation for negative dividents */
2913 x86_shift_reg_imm (code, X86_SHR, X86_EDX, 32 - power);
2914 x86_alu_reg_reg (code, X86_ADD, X86_EAX, X86_EDX);
2915 /* Compute remainder */
2916 x86_alu_reg_imm (code, X86_AND, X86_EAX, (1 << power) - 1);
2917 /* Remove compensation */
2918 x86_alu_reg_reg (code, X86_SUB, X86_EAX, X86_EDX);
2923 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
2927 x86_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
2930 x86_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
2934 x86_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
2937 g_assert (ins->sreg2 == X86_ECX);
2938 x86_shift_reg (code, X86_SHL, ins->dreg);
2941 g_assert (ins->sreg2 == X86_ECX);
2942 x86_shift_reg (code, X86_SAR, ins->dreg);
2946 x86_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
2949 case OP_ISHR_UN_IMM:
2950 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
2953 g_assert (ins->sreg2 == X86_ECX);
2954 x86_shift_reg (code, X86_SHR, ins->dreg);
2958 x86_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
2961 guint8 *jump_to_end;
2963 /* handle shifts below 32 bits */
2964 x86_shld_reg (code, ins->backend.reg3, ins->sreg1);
2965 x86_shift_reg (code, X86_SHL, ins->sreg1);
2967 x86_test_reg_imm (code, X86_ECX, 32);
2968 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
2970 /* handle shift over 32 bit */
2971 x86_mov_reg_reg (code, ins->backend.reg3, ins->sreg1, 4);
2972 x86_clear_reg (code, ins->sreg1);
2974 x86_patch (jump_to_end, code);
2978 guint8 *jump_to_end;
2980 /* handle shifts below 32 bits */
2981 x86_shrd_reg (code, ins->sreg1, ins->backend.reg3);
2982 x86_shift_reg (code, X86_SAR, ins->backend.reg3);
2984 x86_test_reg_imm (code, X86_ECX, 32);
2985 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
2987 /* handle shifts over 31 bits */
2988 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2989 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, 31);
2991 x86_patch (jump_to_end, code);
2995 guint8 *jump_to_end;
2997 /* handle shifts below 32 bits */
2998 x86_shrd_reg (code, ins->sreg1, ins->backend.reg3);
2999 x86_shift_reg (code, X86_SHR, ins->backend.reg3);
3001 x86_test_reg_imm (code, X86_ECX, 32);
3002 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
3004 /* handle shifts over 31 bits */
3005 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
3006 x86_clear_reg (code, ins->backend.reg3);
3008 x86_patch (jump_to_end, code);
3012 if (ins->inst_imm >= 32) {
3013 x86_mov_reg_reg (code, ins->backend.reg3, ins->sreg1, 4);
3014 x86_clear_reg (code, ins->sreg1);
3015 x86_shift_reg_imm (code, X86_SHL, ins->backend.reg3, ins->inst_imm - 32);
3017 x86_shld_reg_imm (code, ins->backend.reg3, ins->sreg1, ins->inst_imm);
3018 x86_shift_reg_imm (code, X86_SHL, ins->sreg1, ins->inst_imm);
3022 if (ins->inst_imm >= 32) {
3023 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
3024 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, 0x1f);
3025 x86_shift_reg_imm (code, X86_SAR, ins->sreg1, ins->inst_imm - 32);
3027 x86_shrd_reg_imm (code, ins->sreg1, ins->backend.reg3, ins->inst_imm);
3028 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, ins->inst_imm);
3031 case OP_LSHR_UN_IMM:
3032 if (ins->inst_imm >= 32) {
3033 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
3034 x86_clear_reg (code, ins->backend.reg3);
3035 x86_shift_reg_imm (code, X86_SHR, ins->sreg1, ins->inst_imm - 32);
3037 x86_shrd_reg_imm (code, ins->sreg1, ins->backend.reg3, ins->inst_imm);
3038 x86_shift_reg_imm (code, X86_SHR, ins->backend.reg3, ins->inst_imm);
3042 x86_not_reg (code, ins->sreg1);
3045 x86_neg_reg (code, ins->sreg1);
3049 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
3053 switch (ins->inst_imm) {
3057 if (ins->dreg != ins->sreg1)
3058 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
3059 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
3062 /* LEA r1, [r2 + r2*2] */
3063 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
3066 /* LEA r1, [r2 + r2*4] */
3067 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3070 /* LEA r1, [r2 + r2*2] */
3072 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
3073 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
3076 /* LEA r1, [r2 + r2*8] */
3077 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
3080 /* LEA r1, [r2 + r2*4] */
3082 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3083 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
3086 /* LEA r1, [r2 + r2*2] */
3088 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
3089 x86_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
3092 /* LEA r1, [r2 + r2*4] */
3093 /* LEA r1, [r1 + r1*4] */
3094 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3095 x86_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
3098 /* LEA r1, [r2 + r2*4] */
3100 /* LEA r1, [r1 + r1*4] */
3101 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3102 x86_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
3103 x86_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
3106 x86_imul_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_imm);
3111 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
3112 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
3114 case OP_IMUL_OVF_UN: {
3115 /* the mul operation and the exception check should most likely be split */
3116 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
3117 /*g_assert (ins->sreg2 == X86_EAX);
3118 g_assert (ins->dreg == X86_EAX);*/
3119 if (ins->sreg2 == X86_EAX) {
3120 non_eax_reg = ins->sreg1;
3121 } else if (ins->sreg1 == X86_EAX) {
3122 non_eax_reg = ins->sreg2;
3124 /* no need to save since we're going to store to it anyway */
3125 if (ins->dreg != X86_EAX) {
3127 x86_push_reg (code, X86_EAX);
3129 x86_mov_reg_reg (code, X86_EAX, ins->sreg1, 4);
3130 non_eax_reg = ins->sreg2;
3132 if (ins->dreg == X86_EDX) {
3135 x86_push_reg (code, X86_EAX);
3137 } else if (ins->dreg != X86_EAX) {
3139 x86_push_reg (code, X86_EDX);
3141 x86_mul_reg (code, non_eax_reg, FALSE);
3142 /* save before the check since pop and mov don't change the flags */
3143 if (ins->dreg != X86_EAX)
3144 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
3146 x86_pop_reg (code, X86_EDX);
3148 x86_pop_reg (code, X86_EAX);
3149 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
3153 x86_mov_reg_imm (code, ins->dreg, ins->inst_c0);
3156 g_assert_not_reached ();
3157 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3158 x86_mov_reg_imm (code, ins->dreg, 0);
3161 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3162 x86_mov_reg_imm (code, ins->dreg, 0);
3164 case OP_LOAD_GOTADDR:
3165 g_assert (ins->dreg == MONO_ARCH_GOT_REG);
3166 code = mono_arch_emit_load_got_addr (cfg->native_code, code, cfg, NULL);
3169 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
3170 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, 0xf0f0f0f0, 4);
3172 case OP_X86_PUSH_GOT_ENTRY:
3173 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
3174 x86_push_membase (code, ins->inst_basereg, 0xf0f0f0f0);
3177 if (ins->dreg != ins->sreg1)
3178 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
3181 MonoCallInst *call = (MonoCallInst*)ins;
3184 ins->flags |= MONO_INST_GC_CALLSITE;
3185 ins->backend.pc_offset = code - cfg->native_code;
3187 /* reset offset to make max_len work */
3188 offset = code - cfg->native_code;
3190 g_assert (!cfg->method->save_lmf);
3192 /* restore callee saved registers */
3193 for (i = 0; i < X86_NREG; ++i)
3194 if (X86_IS_CALLEE_SAVED_REG (i) && cfg->used_int_regs & (1 << i))
3196 if (cfg->used_int_regs & (1 << X86_ESI)) {
3197 x86_mov_reg_membase (code, X86_ESI, X86_EBP, pos, 4);
3200 if (cfg->used_int_regs & (1 << X86_EDI)) {
3201 x86_mov_reg_membase (code, X86_EDI, X86_EBP, pos, 4);
3204 if (cfg->used_int_regs & (1 << X86_EBX)) {
3205 x86_mov_reg_membase (code, X86_EBX, X86_EBP, pos, 4);
3209 /* Copy arguments on the stack to our argument area */
3210 for (i = 0; i < call->stack_usage - call->stack_align_amount; i += 4) {
3211 x86_mov_reg_membase (code, X86_EAX, X86_ESP, i, 4);
3212 x86_mov_membase_reg (code, X86_EBP, 8 + i, X86_EAX, 4);
3215 /* restore ESP/EBP */
3217 offset = code - cfg->native_code;
3218 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD_JUMP, call->method);
3219 x86_jump32 (code, 0);
3221 ins->flags |= MONO_INST_GC_CALLSITE;
3222 cfg->disable_aot = TRUE;
3226 /* ensure ins->sreg1 is not NULL
3227 * note that cmp DWORD PTR [eax], eax is one byte shorter than
3228 * cmp DWORD PTR [eax], 0
3230 x86_alu_membase_reg (code, X86_CMP, ins->sreg1, 0, ins->sreg1);
3233 int hreg = ins->sreg1 == X86_EAX? X86_ECX: X86_EAX;
3234 x86_push_reg (code, hreg);
3235 x86_lea_membase (code, hreg, X86_EBP, cfg->sig_cookie);
3236 x86_mov_membase_reg (code, ins->sreg1, 0, hreg, 4);
3237 x86_pop_reg (code, hreg);
3250 case OP_VOIDCALL_REG:
3252 case OP_FCALL_MEMBASE:
3253 case OP_LCALL_MEMBASE:
3254 case OP_VCALL_MEMBASE:
3255 case OP_VCALL2_MEMBASE:
3256 case OP_VOIDCALL_MEMBASE:
3257 case OP_CALL_MEMBASE: {
3260 call = (MonoCallInst*)ins;
3261 cinfo = (CallInfo*)call->call_info;
3263 switch (ins->opcode) {
3270 if (ins->flags & MONO_INST_HAS_METHOD)
3271 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method);
3273 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr);
3279 case OP_VOIDCALL_REG:
3281 x86_call_reg (code, ins->sreg1);
3283 case OP_FCALL_MEMBASE:
3284 case OP_LCALL_MEMBASE:
3285 case OP_VCALL_MEMBASE:
3286 case OP_VCALL2_MEMBASE:
3287 case OP_VOIDCALL_MEMBASE:
3288 case OP_CALL_MEMBASE:
3289 x86_call_membase (code, ins->sreg1, ins->inst_offset);
3292 g_assert_not_reached ();
3295 ins->flags |= MONO_INST_GC_CALLSITE;
3296 ins->backend.pc_offset = code - cfg->native_code;
3297 if (cinfo->callee_stack_pop) {
3298 /* Have to compensate for the stack space popped by the callee */
3299 x86_alu_reg_imm (code, X86_SUB, X86_ESP, cinfo->callee_stack_pop);
3301 code = emit_move_return_value (cfg, ins, code);
3305 x86_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
3307 case OP_X86_LEA_MEMBASE:
3308 x86_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
3311 x86_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
3314 /* keep alignment */
3315 x86_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_LOCALLOC_ALIGNMENT - 1);
3316 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_LOCALLOC_ALIGNMENT - 1));
3317 code = mono_emit_stack_alloc (cfg, code, ins);
3318 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
3319 if (cfg->param_area)
3320 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
3322 case OP_LOCALLOC_IMM: {
3323 guint32 size = ins->inst_imm;
3324 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
3326 if (ins->flags & MONO_INST_INIT) {
3327 /* FIXME: Optimize this */
3328 x86_mov_reg_imm (code, ins->dreg, size);
3329 ins->sreg1 = ins->dreg;
3331 code = mono_emit_stack_alloc (cfg, code, ins);
3332 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
3334 x86_alu_reg_imm (code, X86_SUB, X86_ESP, size);
3335 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
3337 if (cfg->param_area)
3338 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
3342 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3343 x86_push_reg (code, ins->sreg1);
3344 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
3345 (gpointer)"mono_arch_throw_exception");
3346 ins->flags |= MONO_INST_GC_CALLSITE;
3347 ins->backend.pc_offset = code - cfg->native_code;
3351 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3352 x86_push_reg (code, ins->sreg1);
3353 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
3354 (gpointer)"mono_arch_rethrow_exception");
3355 ins->flags |= MONO_INST_GC_CALLSITE;
3356 ins->backend.pc_offset = code - cfg->native_code;
3359 case OP_CALL_HANDLER:
3360 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3361 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3362 x86_call_imm (code, 0);
3363 mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
3364 x86_alu_reg_imm (code, X86_ADD, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3366 case OP_START_HANDLER: {
3367 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3368 x86_mov_membase_reg (code, spvar->inst_basereg, spvar->inst_offset, X86_ESP, 4);
3369 if (cfg->param_area)
3370 x86_alu_reg_imm (code, X86_SUB, X86_ESP, ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
3373 case OP_ENDFINALLY: {
3374 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3375 x86_mov_reg_membase (code, X86_ESP, spvar->inst_basereg, spvar->inst_offset, 4);
3379 case OP_ENDFILTER: {
3380 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3381 x86_mov_reg_membase (code, X86_ESP, spvar->inst_basereg, spvar->inst_offset, 4);
3382 /* The local allocator will put the result into EAX */
3387 if (ins->dreg != X86_EAX)
3388 x86_mov_reg_reg (code, ins->dreg, X86_EAX, sizeof (gpointer));
3392 ins->inst_c0 = code - cfg->native_code;
3395 if (ins->inst_target_bb->native_offset) {
3396 x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
3398 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3399 if ((cfg->opt & MONO_OPT_BRANCH) &&
3400 x86_is_imm8 (ins->inst_target_bb->max_offset - cpos))
3401 x86_jump8 (code, 0);
3403 x86_jump32 (code, 0);
3407 x86_jump_reg (code, ins->sreg1);
3426 x86_set_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
3427 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3429 case OP_COND_EXC_EQ:
3430 case OP_COND_EXC_NE_UN:
3431 case OP_COND_EXC_LT:
3432 case OP_COND_EXC_LT_UN:
3433 case OP_COND_EXC_GT:
3434 case OP_COND_EXC_GT_UN:
3435 case OP_COND_EXC_GE:
3436 case OP_COND_EXC_GE_UN:
3437 case OP_COND_EXC_LE:
3438 case OP_COND_EXC_LE_UN:
3439 case OP_COND_EXC_IEQ:
3440 case OP_COND_EXC_INE_UN:
3441 case OP_COND_EXC_ILT:
3442 case OP_COND_EXC_ILT_UN:
3443 case OP_COND_EXC_IGT:
3444 case OP_COND_EXC_IGT_UN:
3445 case OP_COND_EXC_IGE:
3446 case OP_COND_EXC_IGE_UN:
3447 case OP_COND_EXC_ILE:
3448 case OP_COND_EXC_ILE_UN:
3449 EMIT_COND_SYSTEM_EXCEPTION (cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)], ins->inst_p1);
3451 case OP_COND_EXC_OV:
3452 case OP_COND_EXC_NO:
3454 case OP_COND_EXC_NC:
3455 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ], (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
3457 case OP_COND_EXC_IOV:
3458 case OP_COND_EXC_INO:
3459 case OP_COND_EXC_IC:
3460 case OP_COND_EXC_INC:
3461 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_IEQ], (ins->opcode < OP_COND_EXC_INE_UN), ins->inst_p1);
3473 EMIT_COND_BRANCH (ins, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
3481 case OP_CMOV_INE_UN:
3482 case OP_CMOV_IGE_UN:
3483 case OP_CMOV_IGT_UN:
3484 case OP_CMOV_ILE_UN:
3485 case OP_CMOV_ILT_UN:
3486 g_assert (ins->dreg == ins->sreg1);
3487 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);
3490 /* floating point opcodes */
3492 double d = *(double *)ins->inst_p0;
3494 if ((d == 0.0) && (mono_signbit (d) == 0)) {
3496 } else if (d == 1.0) {
3499 if (cfg->compile_aot) {
3500 guint32 *val = (guint32*)&d;
3501 x86_push_imm (code, val [1]);
3502 x86_push_imm (code, val [0]);
3503 x86_fld_membase (code, X86_ESP, 0, TRUE);
3504 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3507 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R8, ins->inst_p0);
3508 x86_fld (code, NULL, TRUE);
3514 float f = *(float *)ins->inst_p0;
3516 if ((f == 0.0) && (mono_signbit (f) == 0)) {
3518 } else if (f == 1.0) {
3521 if (cfg->compile_aot) {
3522 guint32 val = *(guint32*)&f;
3523 x86_push_imm (code, val);
3524 x86_fld_membase (code, X86_ESP, 0, FALSE);
3525 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3528 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R4, ins->inst_p0);
3529 x86_fld (code, NULL, FALSE);
3534 case OP_STORER8_MEMBASE_REG:
3535 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, TRUE, TRUE);
3537 case OP_LOADR8_MEMBASE:
3538 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
3540 case OP_STORER4_MEMBASE_REG:
3541 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, FALSE, TRUE);
3543 case OP_LOADR4_MEMBASE:
3544 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
3546 case OP_ICONV_TO_R4:
3547 x86_push_reg (code, ins->sreg1);
3548 x86_fild_membase (code, X86_ESP, 0, FALSE);
3549 /* Change precision */
3550 x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
3551 x86_fld_membase (code, X86_ESP, 0, FALSE);
3552 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3554 case OP_ICONV_TO_R8:
3555 x86_push_reg (code, ins->sreg1);
3556 x86_fild_membase (code, X86_ESP, 0, FALSE);
3557 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3559 case OP_ICONV_TO_R_UN:
3560 x86_push_imm (code, 0);
3561 x86_push_reg (code, ins->sreg1);
3562 x86_fild_membase (code, X86_ESP, 0, TRUE);
3563 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3565 case OP_X86_FP_LOAD_I8:
3566 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
3568 case OP_X86_FP_LOAD_I4:
3569 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
3571 case OP_FCONV_TO_R4:
3572 /* Change precision */
3573 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
3574 x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
3575 x86_fld_membase (code, X86_ESP, 0, FALSE);
3576 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3578 case OP_FCONV_TO_I1:
3579 code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
3581 case OP_FCONV_TO_U1:
3582 code = emit_float_to_int (cfg, code, ins->dreg, 1, FALSE);
3584 case OP_FCONV_TO_I2:
3585 code = emit_float_to_int (cfg, code, ins->dreg, 2, TRUE);
3587 case OP_FCONV_TO_U2:
3588 code = emit_float_to_int (cfg, code, ins->dreg, 2, FALSE);
3590 case OP_FCONV_TO_I4:
3592 code = emit_float_to_int (cfg, code, ins->dreg, 4, TRUE);
3594 case OP_FCONV_TO_I8:
3595 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
3596 x86_fnstcw_membase(code, X86_ESP, 0);
3597 x86_mov_reg_membase (code, ins->dreg, X86_ESP, 0, 2);
3598 x86_alu_reg_imm (code, X86_OR, ins->dreg, 0xc00);
3599 x86_mov_membase_reg (code, X86_ESP, 2, ins->dreg, 2);
3600 x86_fldcw_membase (code, X86_ESP, 2);
3601 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
3602 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
3603 x86_pop_reg (code, ins->dreg);
3604 x86_pop_reg (code, ins->backend.reg3);
3605 x86_fldcw_membase (code, X86_ESP, 0);
3606 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3608 case OP_LCONV_TO_R8_2:
3609 x86_push_reg (code, ins->sreg2);
3610 x86_push_reg (code, ins->sreg1);
3611 x86_fild_membase (code, X86_ESP, 0, TRUE);
3612 /* Change precision */
3613 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
3614 x86_fld_membase (code, X86_ESP, 0, TRUE);
3615 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3617 case OP_LCONV_TO_R4_2:
3618 x86_push_reg (code, ins->sreg2);
3619 x86_push_reg (code, ins->sreg1);
3620 x86_fild_membase (code, X86_ESP, 0, TRUE);
3621 /* Change precision */
3622 x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
3623 x86_fld_membase (code, X86_ESP, 0, FALSE);
3624 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3626 case OP_LCONV_TO_R_UN_2: {
3627 static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
3630 /* load 64bit integer to FP stack */
3631 x86_push_reg (code, ins->sreg2);
3632 x86_push_reg (code, ins->sreg1);
3633 x86_fild_membase (code, X86_ESP, 0, TRUE);
3635 /* test if lreg is negative */
3636 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
3637 br = code; x86_branch8 (code, X86_CC_GEZ, 0, TRUE);
3639 /* add correction constant mn */
3640 if (cfg->compile_aot) {
3641 x86_push_imm (code, (((guint32)mn [9]) << 24) | ((guint32)mn [8] << 16) | ((guint32)mn [7] << 8) | ((guint32)mn [6]));
3642 x86_push_imm (code, (((guint32)mn [5]) << 24) | ((guint32)mn [4] << 16) | ((guint32)mn [3] << 8) | ((guint32)mn [2]));
3643 x86_push_imm (code, (((guint32)mn [1]) << 24) | ((guint32)mn [0] << 16));
3644 x86_fld80_membase (code, X86_ESP, 2);
3645 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
3647 x86_fld80_mem (code, mn);
3649 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3651 x86_patch (br, code);
3653 /* Change precision */
3654 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
3655 x86_fld_membase (code, X86_ESP, 0, TRUE);
3657 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3661 case OP_LCONV_TO_OVF_I:
3662 case OP_LCONV_TO_OVF_I4_2: {
3663 guint8 *br [3], *label [1];
3667 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
3669 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
3671 /* If the low word top bit is set, see if we are negative */
3672 br [0] = code; x86_branch8 (code, X86_CC_LT, 0, TRUE);
3673 /* We are not negative (no top bit set, check for our top word to be zero */
3674 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
3675 br [1] = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
3678 /* throw exception */
3679 tins = mono_branch_optimize_exception_target (cfg, bb, "OverflowException");
3681 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, tins->inst_true_bb);
3682 if ((cfg->opt & MONO_OPT_BRANCH) && x86_is_imm8 (tins->inst_true_bb->max_offset - cpos))
3683 x86_jump8 (code, 0);
3685 x86_jump32 (code, 0);
3687 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
3688 x86_jump32 (code, 0);
3692 x86_patch (br [0], code);
3693 /* our top bit is set, check that top word is 0xfffffff */
3694 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0xffffffff);
3696 x86_patch (br [1], code);
3697 /* nope, emit exception */
3698 br [2] = code; x86_branch8 (code, X86_CC_NE, 0, TRUE);
3699 x86_patch (br [2], label [0]);
3701 if (ins->dreg != ins->sreg1)
3702 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
3706 /* Not needed on the fp stack */
3708 case OP_MOVE_F_TO_I4:
3709 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE, TRUE);
3710 x86_mov_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, 4);
3712 case OP_MOVE_I4_TO_F:
3713 x86_mov_membase_reg (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, ins->sreg1, 4);
3714 x86_fld_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE);
3717 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3720 x86_fp_op_reg (code, X86_FSUB, 1, TRUE);
3723 x86_fp_op_reg (code, X86_FMUL, 1, TRUE);
3726 x86_fp_op_reg (code, X86_FDIV, 1, TRUE);
3734 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3739 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3746 * it really doesn't make sense to inline all this code,
3747 * it's here just to show that things may not be as simple
3750 guchar *check_pos, *end_tan, *pop_jump;
3751 x86_push_reg (code, X86_EAX);
3754 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
3756 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3757 x86_fstp (code, 0); /* pop the 1.0 */
3759 x86_jump8 (code, 0);
3761 x86_fp_op (code, X86_FADD, 0);
3765 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
3767 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3770 x86_patch (pop_jump, code);
3771 x86_fstp (code, 0); /* pop the 1.0 */
3772 x86_patch (check_pos, code);
3773 x86_patch (end_tan, code);
3775 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3776 x86_pop_reg (code, X86_EAX);
3783 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3792 g_assert (cfg->opt & MONO_OPT_CMOV);
3793 g_assert (ins->dreg == ins->sreg1);
3794 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3795 x86_cmov_reg (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2);
3798 g_assert (cfg->opt & MONO_OPT_CMOV);
3799 g_assert (ins->dreg == ins->sreg1);
3800 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3801 x86_cmov_reg (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2);
3804 g_assert (cfg->opt & MONO_OPT_CMOV);
3805 g_assert (ins->dreg == ins->sreg1);
3806 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3807 x86_cmov_reg (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2);
3810 g_assert (cfg->opt & MONO_OPT_CMOV);
3811 g_assert (ins->dreg == ins->sreg1);
3812 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3813 x86_cmov_reg (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2);
3819 x86_fxch (code, ins->inst_imm);
3824 x86_push_reg (code, X86_EAX);
3825 /* we need to exchange ST(0) with ST(1) */
3828 /* this requires a loop, because fprem somtimes
3829 * returns a partial remainder */
3831 /* looks like MS is using fprem instead of the IEEE compatible fprem1 */
3832 /* x86_fprem1 (code); */
3835 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_C2);
3837 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3843 x86_pop_reg (code, X86_EAX);
3847 if (cfg->opt & MONO_OPT_FCMOV) {
3848 x86_fcomip (code, 1);
3852 /* this overwrites EAX */
3853 EMIT_FPCOMPARE(code);
3854 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3858 if (cfg->opt & MONO_OPT_FCMOV) {
3859 /* zeroing the register at the start results in
3860 * shorter and faster code (we can also remove the widening op)
3862 guchar *unordered_check;
3863 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3864 x86_fcomip (code, 1);
3866 unordered_check = code;
3867 x86_branch8 (code, X86_CC_P, 0, FALSE);
3868 if (ins->opcode == OP_FCEQ) {
3869 x86_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
3870 x86_patch (unordered_check, code);
3872 guchar *jump_to_end;
3873 x86_set_reg (code, X86_CC_NE, ins->dreg, FALSE);
3875 x86_jump8 (code, 0);
3876 x86_patch (unordered_check, code);
3877 x86_inc_reg (code, ins->dreg);
3878 x86_patch (jump_to_end, code);
3883 if (ins->dreg != X86_EAX)
3884 x86_push_reg (code, X86_EAX);
3886 EMIT_FPCOMPARE(code);
3887 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3888 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
3889 x86_set_reg (code, ins->opcode == OP_FCEQ ? X86_CC_EQ : X86_CC_NE, ins->dreg, TRUE);
3890 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3892 if (ins->dreg != X86_EAX)
3893 x86_pop_reg (code, X86_EAX);
3897 if (cfg->opt & MONO_OPT_FCMOV) {
3898 /* zeroing the register at the start results in
3899 * shorter and faster code (we can also remove the widening op)
3901 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3902 x86_fcomip (code, 1);
3904 if (ins->opcode == OP_FCLT_UN) {
3905 guchar *unordered_check = code;
3906 guchar *jump_to_end;
3907 x86_branch8 (code, X86_CC_P, 0, FALSE);
3908 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
3910 x86_jump8 (code, 0);
3911 x86_patch (unordered_check, code);
3912 x86_inc_reg (code, ins->dreg);
3913 x86_patch (jump_to_end, code);
3915 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
3919 if (ins->dreg != X86_EAX)
3920 x86_push_reg (code, X86_EAX);
3922 EMIT_FPCOMPARE(code);
3923 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3924 if (ins->opcode == OP_FCLT_UN) {
3925 guchar *is_not_zero_check, *end_jump;
3926 is_not_zero_check = code;
3927 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3929 x86_jump8 (code, 0);
3930 x86_patch (is_not_zero_check, code);
3931 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3933 x86_patch (end_jump, code);
3935 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3936 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3938 if (ins->dreg != X86_EAX)
3939 x86_pop_reg (code, X86_EAX);
3942 guchar *unordered_check;
3943 guchar *jump_to_end;
3944 if (cfg->opt & MONO_OPT_FCMOV) {
3945 /* zeroing the register at the start results in
3946 * shorter and faster code (we can also remove the widening op)
3948 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3949 x86_fcomip (code, 1);
3951 unordered_check = code;
3952 x86_branch8 (code, X86_CC_P, 0, FALSE);
3953 x86_set_reg (code, X86_CC_NB, ins->dreg, FALSE);
3954 x86_patch (unordered_check, code);
3957 if (ins->dreg != X86_EAX)
3958 x86_push_reg (code, X86_EAX);
3960 EMIT_FPCOMPARE(code);
3961 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3962 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
3963 unordered_check = code;
3964 x86_branch8 (code, X86_CC_EQ, 0, FALSE);
3966 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3967 x86_set_reg (code, X86_CC_NE, ins->dreg, TRUE);
3968 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3970 x86_jump8 (code, 0);
3971 x86_patch (unordered_check, code);
3972 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3973 x86_patch (jump_to_end, code);
3975 if (ins->dreg != X86_EAX)
3976 x86_pop_reg (code, X86_EAX);
3981 if (cfg->opt & MONO_OPT_FCMOV) {
3982 /* zeroing the register at the start results in
3983 * shorter and faster code (we can also remove the widening op)
3985 guchar *unordered_check;
3986 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3987 x86_fcomip (code, 1);
3989 if (ins->opcode == OP_FCGT) {
3990 unordered_check = code;
3991 x86_branch8 (code, X86_CC_P, 0, FALSE);
3992 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
3993 x86_patch (unordered_check, code);
3995 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
3999 if (ins->dreg != X86_EAX)
4000 x86_push_reg (code, X86_EAX);
4002 EMIT_FPCOMPARE(code);
4003 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
4004 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4005 if (ins->opcode == OP_FCGT_UN) {
4006 guchar *is_not_zero_check, *end_jump;
4007 is_not_zero_check = code;
4008 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
4010 x86_jump8 (code, 0);
4011 x86_patch (is_not_zero_check, code);
4012 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
4014 x86_patch (end_jump, code);
4016 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
4017 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
4019 if (ins->dreg != X86_EAX)
4020 x86_pop_reg (code, X86_EAX);
4023 guchar *unordered_check;
4024 guchar *jump_to_end;
4025 if (cfg->opt & MONO_OPT_FCMOV) {
4026 /* zeroing the register at the start results in
4027 * shorter and faster code (we can also remove the widening op)
4029 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4030 x86_fcomip (code, 1);
4032 unordered_check = code;
4033 x86_branch8 (code, X86_CC_P, 0, FALSE);
4034 x86_set_reg (code, X86_CC_NA, ins->dreg, FALSE);
4035 x86_patch (unordered_check, code);
4038 if (ins->dreg != X86_EAX)
4039 x86_push_reg (code, X86_EAX);
4041 EMIT_FPCOMPARE(code);
4042 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
4043 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
4044 unordered_check = code;
4045 x86_branch8 (code, X86_CC_EQ, 0, FALSE);
4047 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4048 x86_set_reg (code, X86_CC_GE, ins->dreg, TRUE);
4049 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
4051 x86_jump8 (code, 0);
4052 x86_patch (unordered_check, code);
4053 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4054 x86_patch (jump_to_end, code);
4056 if (ins->dreg != X86_EAX)
4057 x86_pop_reg (code, X86_EAX);
4061 if (cfg->opt & MONO_OPT_FCMOV) {
4062 guchar *jump = code;
4063 x86_branch8 (code, X86_CC_P, 0, TRUE);
4064 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4065 x86_patch (jump, code);
4068 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
4069 EMIT_COND_BRANCH (ins, X86_CC_EQ, TRUE);
4072 /* Branch if C013 != 100 */
4073 if (cfg->opt & MONO_OPT_FCMOV) {
4074 /* branch if !ZF or (PF|CF) */
4075 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
4076 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
4077 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
4080 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
4081 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
4084 if (cfg->opt & MONO_OPT_FCMOV) {
4085 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
4088 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4091 if (cfg->opt & MONO_OPT_FCMOV) {
4092 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
4093 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
4096 if (ins->opcode == OP_FBLT_UN) {
4097 guchar *is_not_zero_check, *end_jump;
4098 is_not_zero_check = code;
4099 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
4101 x86_jump8 (code, 0);
4102 x86_patch (is_not_zero_check, code);
4103 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
4105 x86_patch (end_jump, code);
4107 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4111 if (cfg->opt & MONO_OPT_FCMOV) {
4112 if (ins->opcode == OP_FBGT) {
4115 /* skip branch if C1=1 */
4117 x86_branch8 (code, X86_CC_P, 0, FALSE);
4118 /* branch if (C0 | C3) = 1 */
4119 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
4120 x86_patch (br1, code);
4122 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
4126 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4127 if (ins->opcode == OP_FBGT_UN) {
4128 guchar *is_not_zero_check, *end_jump;
4129 is_not_zero_check = code;
4130 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
4132 x86_jump8 (code, 0);
4133 x86_patch (is_not_zero_check, code);
4134 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
4136 x86_patch (end_jump, code);
4138 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4141 /* Branch if C013 == 100 or 001 */
4142 if (cfg->opt & MONO_OPT_FCMOV) {
4145 /* skip branch if C1=1 */
4147 x86_branch8 (code, X86_CC_P, 0, FALSE);
4148 /* branch if (C0 | C3) = 1 */
4149 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
4150 x86_patch (br1, code);
4153 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4154 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4155 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
4156 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4159 /* Branch if C013 == 000 */
4160 if (cfg->opt & MONO_OPT_FCMOV) {
4161 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
4164 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
4167 /* Branch if C013=000 or 100 */
4168 if (cfg->opt & MONO_OPT_FCMOV) {
4171 /* skip branch if C1=1 */
4173 x86_branch8 (code, X86_CC_P, 0, FALSE);
4174 /* branch if C0=0 */
4175 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
4176 x86_patch (br1, code);
4179 x86_alu_reg_imm (code, X86_AND, X86_EAX, (X86_FP_C0|X86_FP_C1));
4180 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0);
4181 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4184 /* Branch if C013 != 001 */
4185 if (cfg->opt & MONO_OPT_FCMOV) {
4186 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
4187 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
4190 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4191 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
4195 x86_push_reg (code, X86_EAX);
4198 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
4199 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4200 x86_pop_reg (code, X86_EAX);
4202 /* Have to clean up the fp stack before throwing the exception */
4204 x86_branch8 (code, X86_CC_NE, 0, FALSE);
4207 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
4209 x86_patch (br1, code);
4213 code = mono_x86_emit_tls_get (code, ins->dreg, ins->inst_offset);
4216 case OP_TLS_GET_REG: {
4217 code = emit_tls_get_reg (code, ins->dreg, ins->sreg1);
4221 code = mono_x86_emit_tls_set (code, ins->sreg1, ins->inst_offset);
4224 case OP_TLS_SET_REG: {
4225 code = emit_tls_set_reg (code, ins->sreg1, ins->sreg2);
4228 case OP_MEMORY_BARRIER: {
4229 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ) {
4230 x86_prefix (code, X86_LOCK_PREFIX);
4231 x86_alu_membase_imm (code, X86_ADD, X86_ESP, 0, 0);
4235 case OP_ATOMIC_ADD_I4: {
4236 int dreg = ins->dreg;
4238 g_assert (cfg->has_atomic_add_i4);
4240 /* hack: limit in regalloc, dreg != sreg1 && dreg != sreg2 */
4241 if (ins->sreg2 == dreg) {
4242 if (dreg == X86_EBX) {
4244 if (ins->inst_basereg == X86_EDI)
4248 if (ins->inst_basereg == X86_EBX)
4251 } else if (ins->inst_basereg == dreg) {
4252 if (dreg == X86_EBX) {
4254 if (ins->sreg2 == X86_EDI)
4258 if (ins->sreg2 == X86_EBX)
4263 if (dreg != ins->dreg) {
4264 x86_push_reg (code, dreg);
4267 x86_mov_reg_reg (code, dreg, ins->sreg2, 4);
4268 x86_prefix (code, X86_LOCK_PREFIX);
4269 x86_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, 4);
4270 /* dreg contains the old value, add with sreg2 value */
4271 x86_alu_reg_reg (code, X86_ADD, dreg, ins->sreg2);
4273 if (ins->dreg != dreg) {
4274 x86_mov_reg_reg (code, ins->dreg, dreg, 4);
4275 x86_pop_reg (code, dreg);
4280 case OP_ATOMIC_EXCHANGE_I4: {
4282 int sreg2 = ins->sreg2;
4283 int breg = ins->inst_basereg;
4285 g_assert (cfg->has_atomic_exchange_i4);
4287 /* cmpxchg uses eax as comperand, need to make sure we can use it
4288 * hack to overcome limits in x86 reg allocator
4289 * (req: dreg == eax and sreg2 != eax and breg != eax)
4291 g_assert (ins->dreg == X86_EAX);
4293 /* We need the EAX reg for the cmpxchg */
4294 if (ins->sreg2 == X86_EAX) {
4295 sreg2 = (breg == X86_EDX) ? X86_EBX : X86_EDX;
4296 x86_push_reg (code, sreg2);
4297 x86_mov_reg_reg (code, sreg2, X86_EAX, 4);
4300 if (breg == X86_EAX) {
4301 breg = (sreg2 == X86_ESI) ? X86_EDI : X86_ESI;
4302 x86_push_reg (code, breg);
4303 x86_mov_reg_reg (code, breg, X86_EAX, 4);
4306 x86_mov_reg_membase (code, X86_EAX, breg, ins->inst_offset, 4);
4308 br [0] = code; x86_prefix (code, X86_LOCK_PREFIX);
4309 x86_cmpxchg_membase_reg (code, breg, ins->inst_offset, sreg2);
4310 br [1] = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
4311 x86_patch (br [1], br [0]);
4313 if (breg != ins->inst_basereg)
4314 x86_pop_reg (code, breg);
4316 if (ins->sreg2 != sreg2)
4317 x86_pop_reg (code, sreg2);
4321 case OP_ATOMIC_CAS_I4: {
4322 g_assert (ins->dreg == X86_EAX);
4323 g_assert (ins->sreg3 == X86_EAX);
4324 g_assert (ins->sreg1 != X86_EAX);
4325 g_assert (ins->sreg1 != ins->sreg2);
4327 x86_prefix (code, X86_LOCK_PREFIX);
4328 x86_cmpxchg_membase_reg (code, ins->sreg1, ins->inst_offset, ins->sreg2);
4331 case OP_ATOMIC_LOAD_I1: {
4332 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
4335 case OP_ATOMIC_LOAD_U1: {
4336 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
4339 case OP_ATOMIC_LOAD_I2: {
4340 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
4343 case OP_ATOMIC_LOAD_U2: {
4344 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
4347 case OP_ATOMIC_LOAD_I4:
4348 case OP_ATOMIC_LOAD_U4: {
4349 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
4352 case OP_ATOMIC_LOAD_R4:
4353 case OP_ATOMIC_LOAD_R8: {
4354 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, ins->opcode == OP_ATOMIC_LOAD_R8);
4357 case OP_ATOMIC_STORE_I1:
4358 case OP_ATOMIC_STORE_U1:
4359 case OP_ATOMIC_STORE_I2:
4360 case OP_ATOMIC_STORE_U2:
4361 case OP_ATOMIC_STORE_I4:
4362 case OP_ATOMIC_STORE_U4: {
4365 switch (ins->opcode) {
4366 case OP_ATOMIC_STORE_I1:
4367 case OP_ATOMIC_STORE_U1:
4370 case OP_ATOMIC_STORE_I2:
4371 case OP_ATOMIC_STORE_U2:
4374 case OP_ATOMIC_STORE_I4:
4375 case OP_ATOMIC_STORE_U4:
4380 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, size);
4382 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
4386 case OP_ATOMIC_STORE_R4:
4387 case OP_ATOMIC_STORE_R8: {
4388 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, ins->opcode == OP_ATOMIC_STORE_R8, TRUE);
4390 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
4394 case OP_CARD_TABLE_WBARRIER: {
4395 int ptr = ins->sreg1;
4396 int value = ins->sreg2;
4398 int nursery_shift, card_table_shift;
4399 gpointer card_table_mask;
4400 size_t nursery_size;
4401 gulong card_table = (gulong)mono_gc_get_card_table (&card_table_shift, &card_table_mask);
4402 gulong nursery_start = (gulong)mono_gc_get_nursery (&nursery_shift, &nursery_size);
4403 gboolean card_table_nursery_check = mono_gc_card_table_nursery_check ();
4406 * We need one register we can clobber, we choose EDX and make sreg1
4407 * fixed EAX to work around limitations in the local register allocator.
4408 * sreg2 might get allocated to EDX, but that is not a problem since
4409 * we use it before clobbering EDX.
4411 g_assert (ins->sreg1 == X86_EAX);
4414 * This is the code we produce:
4417 * edx >>= nursery_shift
4418 * cmp edx, (nursery_start >> nursery_shift)
4421 * edx >>= card_table_shift
4422 * card_table[edx] = 1
4426 if (card_table_nursery_check) {
4427 if (value != X86_EDX)
4428 x86_mov_reg_reg (code, X86_EDX, value, 4);
4429 x86_shift_reg_imm (code, X86_SHR, X86_EDX, nursery_shift);
4430 x86_alu_reg_imm (code, X86_CMP, X86_EDX, nursery_start >> nursery_shift);
4431 br = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
4433 x86_mov_reg_reg (code, X86_EDX, ptr, 4);
4434 x86_shift_reg_imm (code, X86_SHR, X86_EDX, card_table_shift);
4435 if (card_table_mask)
4436 x86_alu_reg_imm (code, X86_AND, X86_EDX, (int)card_table_mask);
4437 x86_mov_membase_imm (code, X86_EDX, card_table, 1, 1);
4438 if (card_table_nursery_check)
4439 x86_patch (br, code);
4442 #ifdef MONO_ARCH_SIMD_INTRINSICS
4444 x86_sse_alu_ps_reg_reg (code, X86_SSE_ADD, ins->sreg1, ins->sreg2);
4447 x86_sse_alu_ps_reg_reg (code, X86_SSE_DIV, ins->sreg1, ins->sreg2);
4450 x86_sse_alu_ps_reg_reg (code, X86_SSE_MUL, ins->sreg1, ins->sreg2);
4453 x86_sse_alu_ps_reg_reg (code, X86_SSE_SUB, ins->sreg1, ins->sreg2);
4456 x86_sse_alu_ps_reg_reg (code, X86_SSE_MAX, ins->sreg1, ins->sreg2);
4459 x86_sse_alu_ps_reg_reg (code, X86_SSE_MIN, ins->sreg1, ins->sreg2);
4462 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
4463 x86_sse_alu_ps_reg_reg_imm (code, X86_SSE_COMP, ins->sreg1, ins->sreg2, ins->inst_c0);
4466 x86_sse_alu_ps_reg_reg (code, X86_SSE_AND, ins->sreg1, ins->sreg2);
4469 x86_sse_alu_ps_reg_reg (code, X86_SSE_ANDN, ins->sreg1, ins->sreg2);
4472 x86_sse_alu_ps_reg_reg (code, X86_SSE_OR, ins->sreg1, ins->sreg2);
4475 x86_sse_alu_ps_reg_reg (code, X86_SSE_XOR, ins->sreg1, ins->sreg2);
4478 x86_sse_alu_ps_reg_reg (code, X86_SSE_SQRT, ins->dreg, ins->sreg1);
4481 x86_sse_alu_ps_reg_reg (code, X86_SSE_RSQRT, ins->dreg, ins->sreg1);
4484 x86_sse_alu_ps_reg_reg (code, X86_SSE_RCP, ins->dreg, ins->sreg1);
4487 x86_sse_alu_sd_reg_reg (code, X86_SSE_ADDSUB, ins->sreg1, ins->sreg2);
4490 x86_sse_alu_sd_reg_reg (code, X86_SSE_HADD, ins->sreg1, ins->sreg2);
4493 x86_sse_alu_sd_reg_reg (code, X86_SSE_HSUB, ins->sreg1, ins->sreg2);
4496 x86_sse_alu_ss_reg_reg (code, X86_SSE_MOVSHDUP, ins->dreg, ins->sreg1);
4499 x86_sse_alu_ss_reg_reg (code, X86_SSE_MOVSLDUP, ins->dreg, ins->sreg1);
4502 case OP_PSHUFLEW_HIGH:
4503 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
4504 x86_pshufw_reg_reg (code, ins->dreg, ins->sreg1, ins->inst_c0, 1);
4506 case OP_PSHUFLEW_LOW:
4507 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
4508 x86_pshufw_reg_reg (code, ins->dreg, ins->sreg1, ins->inst_c0, 0);
4511 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
4512 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->sreg1, ins->inst_c0);
4515 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
4516 x86_sse_alu_reg_reg_imm8 (code, X86_SSE_SHUFP, ins->sreg1, ins->sreg2, ins->inst_c0);
4519 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0x3);
4520 x86_sse_alu_pd_reg_reg_imm8 (code, X86_SSE_SHUFP, ins->sreg1, ins->sreg2, ins->inst_c0);
4524 x86_sse_alu_pd_reg_reg (code, X86_SSE_ADD, ins->sreg1, ins->sreg2);
4527 x86_sse_alu_pd_reg_reg (code, X86_SSE_DIV, ins->sreg1, ins->sreg2);
4530 x86_sse_alu_pd_reg_reg (code, X86_SSE_MUL, ins->sreg1, ins->sreg2);
4533 x86_sse_alu_pd_reg_reg (code, X86_SSE_SUB, ins->sreg1, ins->sreg2);
4536 x86_sse_alu_pd_reg_reg (code, X86_SSE_MAX, ins->sreg1, ins->sreg2);
4539 x86_sse_alu_pd_reg_reg (code, X86_SSE_MIN, ins->sreg1, ins->sreg2);
4542 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
4543 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_COMP, ins->sreg1, ins->sreg2, ins->inst_c0);
4546 x86_sse_alu_pd_reg_reg (code, X86_SSE_AND, ins->sreg1, ins->sreg2);
4549 x86_sse_alu_pd_reg_reg (code, X86_SSE_ANDN, ins->sreg1, ins->sreg2);
4552 x86_sse_alu_pd_reg_reg (code, X86_SSE_OR, ins->sreg1, ins->sreg2);
4555 x86_sse_alu_pd_reg_reg (code, X86_SSE_XOR, ins->sreg1, ins->sreg2);
4558 x86_sse_alu_pd_reg_reg (code, X86_SSE_SQRT, ins->dreg, ins->sreg1);
4561 x86_sse_alu_pd_reg_reg (code, X86_SSE_ADDSUB, ins->sreg1, ins->sreg2);
4564 x86_sse_alu_pd_reg_reg (code, X86_SSE_HADD, ins->sreg1, ins->sreg2);
4567 x86_sse_alu_pd_reg_reg (code, X86_SSE_HSUB, ins->sreg1, ins->sreg2);
4570 x86_sse_alu_sd_reg_reg (code, X86_SSE_MOVDDUP, ins->dreg, ins->sreg1);
4573 case OP_EXTRACT_MASK:
4574 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMOVMSKB, ins->dreg, ins->sreg1);
4578 x86_sse_alu_pd_reg_reg (code, X86_SSE_PAND, ins->sreg1, ins->sreg2);
4581 x86_sse_alu_pd_reg_reg (code, X86_SSE_POR, ins->sreg1, ins->sreg2);
4584 x86_sse_alu_pd_reg_reg (code, X86_SSE_PXOR, ins->sreg1, ins->sreg2);
4588 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDB, ins->sreg1, ins->sreg2);
4591 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDW, ins->sreg1, ins->sreg2);
4594 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDD, ins->sreg1, ins->sreg2);
4597 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDQ, ins->sreg1, ins->sreg2);
4601 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBB, ins->sreg1, ins->sreg2);
4604 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBW, ins->sreg1, ins->sreg2);
4607 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBD, ins->sreg1, ins->sreg2);
4610 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBQ, ins->sreg1, ins->sreg2);
4614 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMAXUB, ins->sreg1, ins->sreg2);
4617 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXUW, ins->sreg1, ins->sreg2);
4620 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXUD, ins->sreg1, ins->sreg2);
4624 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXSB, ins->sreg1, ins->sreg2);
4627 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMAXSW, ins->sreg1, ins->sreg2);
4630 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXSD, ins->sreg1, ins->sreg2);
4634 x86_sse_alu_pd_reg_reg (code, X86_SSE_PAVGB, ins->sreg1, ins->sreg2);
4637 x86_sse_alu_pd_reg_reg (code, X86_SSE_PAVGW, ins->sreg1, ins->sreg2);
4641 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMINUB, ins->sreg1, ins->sreg2);
4644 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINUW, ins->sreg1, ins->sreg2);
4647 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINUD, ins->sreg1, ins->sreg2);
4651 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINSB, ins->sreg1, ins->sreg2);
4654 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMINSW, ins->sreg1, ins->sreg2);
4657 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINSD, ins->sreg1, ins->sreg2);
4661 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQB, ins->sreg1, ins->sreg2);
4664 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQW, ins->sreg1, ins->sreg2);
4667 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQD, ins->sreg1, ins->sreg2);
4670 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PCMPEQQ, ins->sreg1, ins->sreg2);
4674 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPGTB, ins->sreg1, ins->sreg2);
4677 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPGTW, ins->sreg1, ins->sreg2);
4680 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPGTD, ins->sreg1, ins->sreg2);
4683 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PCMPGTQ, ins->sreg1, ins->sreg2);
4686 case OP_PSUM_ABS_DIFF:
4687 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSADBW, ins->sreg1, ins->sreg2);
4690 case OP_UNPACK_LOWB:
4691 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLBW, ins->sreg1, ins->sreg2);
4693 case OP_UNPACK_LOWW:
4694 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLWD, ins->sreg1, ins->sreg2);
4696 case OP_UNPACK_LOWD:
4697 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLDQ, ins->sreg1, ins->sreg2);
4699 case OP_UNPACK_LOWQ:
4700 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLQDQ, ins->sreg1, ins->sreg2);
4702 case OP_UNPACK_LOWPS:
4703 x86_sse_alu_ps_reg_reg (code, X86_SSE_UNPCKL, ins->sreg1, ins->sreg2);
4705 case OP_UNPACK_LOWPD:
4706 x86_sse_alu_pd_reg_reg (code, X86_SSE_UNPCKL, ins->sreg1, ins->sreg2);
4709 case OP_UNPACK_HIGHB:
4710 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHBW, ins->sreg1, ins->sreg2);
4712 case OP_UNPACK_HIGHW:
4713 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHWD, ins->sreg1, ins->sreg2);
4715 case OP_UNPACK_HIGHD:
4716 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHDQ, ins->sreg1, ins->sreg2);
4718 case OP_UNPACK_HIGHQ:
4719 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHQDQ, ins->sreg1, ins->sreg2);
4721 case OP_UNPACK_HIGHPS:
4722 x86_sse_alu_ps_reg_reg (code, X86_SSE_UNPCKH, ins->sreg1, ins->sreg2);
4724 case OP_UNPACK_HIGHPD:
4725 x86_sse_alu_pd_reg_reg (code, X86_SSE_UNPCKH, ins->sreg1, ins->sreg2);
4729 x86_sse_alu_pd_reg_reg (code, X86_SSE_PACKSSWB, ins->sreg1, ins->sreg2);
4732 x86_sse_alu_pd_reg_reg (code, X86_SSE_PACKSSDW, ins->sreg1, ins->sreg2);
4735 x86_sse_alu_pd_reg_reg (code, X86_SSE_PACKUSWB, ins->sreg1, ins->sreg2);
4738 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PACKUSDW, ins->sreg1, ins->sreg2);
4741 case OP_PADDB_SAT_UN:
4742 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDUSB, ins->sreg1, ins->sreg2);
4744 case OP_PSUBB_SAT_UN:
4745 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBUSB, ins->sreg1, ins->sreg2);
4747 case OP_PADDW_SAT_UN:
4748 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDUSW, ins->sreg1, ins->sreg2);
4750 case OP_PSUBW_SAT_UN:
4751 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBUSW, ins->sreg1, ins->sreg2);
4755 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDSB, ins->sreg1, ins->sreg2);
4758 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBSB, ins->sreg1, ins->sreg2);
4761 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDSW, ins->sreg1, ins->sreg2);
4764 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBSW, ins->sreg1, ins->sreg2);
4768 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULLW, ins->sreg1, ins->sreg2);
4771 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMULLD, ins->sreg1, ins->sreg2);
4774 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULUDQ, ins->sreg1, ins->sreg2);
4776 case OP_PMULW_HIGH_UN:
4777 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULHUW, ins->sreg1, ins->sreg2);
4780 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULHW, ins->sreg1, ins->sreg2);
4784 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTW, X86_SSE_SHR, ins->dreg, ins->inst_imm);
4787 x86_sse_shift_reg_reg (code, X86_SSE_PSRLW_REG, ins->dreg, ins->sreg2);
4791 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTW, X86_SSE_SAR, ins->dreg, ins->inst_imm);
4794 x86_sse_shift_reg_reg (code, X86_SSE_PSRAW_REG, ins->dreg, ins->sreg2);
4798 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTW, X86_SSE_SHL, ins->dreg, ins->inst_imm);
4801 x86_sse_shift_reg_reg (code, X86_SSE_PSLLW_REG, ins->dreg, ins->sreg2);
4805 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTD, X86_SSE_SHR, ins->dreg, ins->inst_imm);
4808 x86_sse_shift_reg_reg (code, X86_SSE_PSRLD_REG, ins->dreg, ins->sreg2);
4812 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTD, X86_SSE_SAR, ins->dreg, ins->inst_imm);
4815 x86_sse_shift_reg_reg (code, X86_SSE_PSRAD_REG, ins->dreg, ins->sreg2);
4819 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTD, X86_SSE_SHL, ins->dreg, ins->inst_imm);
4822 x86_sse_shift_reg_reg (code, X86_SSE_PSLLD_REG, ins->dreg, ins->sreg2);
4826 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTQ, X86_SSE_SHR, ins->dreg, ins->inst_imm);
4829 x86_sse_shift_reg_reg (code, X86_SSE_PSRLQ_REG, ins->dreg, ins->sreg2);
4833 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTQ, X86_SSE_SHL, ins->dreg, ins->inst_imm);
4836 x86_sse_shift_reg_reg (code, X86_SSE_PSLLQ_REG, ins->dreg, ins->sreg2);
4840 x86_movd_xreg_reg (code, ins->dreg, ins->sreg1);
4843 x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
4847 x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
4849 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_c0 * 8);
4850 x86_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I1, FALSE);
4854 x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
4856 x86_shift_reg_imm (code, X86_SHR, ins->dreg, 16);
4857 x86_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I2, TRUE);
4861 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);
4863 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);
4864 x86_fld_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE);
4868 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->sreg1, ins->sreg2, ins->inst_c0);
4870 case OP_EXTRACTX_U2:
4871 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PEXTRW, ins->dreg, ins->sreg1, ins->inst_c0);
4873 case OP_INSERTX_U1_SLOW:
4874 /*sreg1 is the extracted ireg (scratch)
4875 /sreg2 is the to be inserted ireg (scratch)
4876 /dreg is the xreg to receive the value*/
4878 /*clear the bits from the extracted word*/
4879 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_c0 & 1 ? 0x00FF : 0xFF00);
4880 /*shift the value to insert if needed*/
4881 if (ins->inst_c0 & 1)
4882 x86_shift_reg_imm (code, X86_SHL, ins->sreg2, 8);
4883 /*join them together*/
4884 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
4885 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, ins->inst_c0 / 2);
4887 case OP_INSERTX_I4_SLOW:
4888 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg2, ins->inst_c0 * 2);
4889 x86_shift_reg_imm (code, X86_SHR, ins->sreg2, 16);
4890 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg2, ins->inst_c0 * 2 + 1);
4893 case OP_INSERTX_R4_SLOW:
4894 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE, TRUE);
4895 /*TODO if inst_c0 == 0 use movss*/
4896 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);
4897 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);
4899 case OP_INSERTX_R8_SLOW:
4900 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
4901 if (cfg->verbose_level)
4902 printf ("CONVERTING a OP_INSERTX_R8_SLOW %d offset %x\n", ins->inst_c0, offset);
4904 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);
4906 x86_movsd_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
4909 case OP_STOREX_MEMBASE_REG:
4910 case OP_STOREX_MEMBASE:
4911 x86_movups_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
4913 case OP_LOADX_MEMBASE:
4914 x86_movups_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
4916 case OP_LOADX_ALIGNED_MEMBASE:
4917 x86_movaps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
4919 case OP_STOREX_ALIGNED_MEMBASE_REG:
4920 x86_movaps_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
4922 case OP_STOREX_NTA_MEMBASE_REG:
4923 x86_sse_alu_reg_membase (code, X86_SSE_MOVNTPS, ins->dreg, ins->sreg1, ins->inst_offset);
4925 case OP_PREFETCH_MEMBASE:
4926 x86_sse_alu_reg_membase (code, X86_SSE_PREFETCH, ins->backend.arg_info, ins->sreg1, ins->inst_offset);
4930 /*FIXME the peephole pass should have killed this*/
4931 if (ins->dreg != ins->sreg1)
4932 x86_movaps_reg_reg (code, ins->dreg, ins->sreg1);
4935 x86_sse_alu_pd_reg_reg (code, X86_SSE_PXOR, ins->dreg, ins->dreg);
4938 case OP_FCONV_TO_R8_X:
4939 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
4940 x86_movsd_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
4943 case OP_XCONV_R8_TO_I4:
4944 x86_cvttsd2si (code, ins->dreg, ins->sreg1);
4945 switch (ins->backend.source_opcode) {
4946 case OP_FCONV_TO_I1:
4947 x86_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
4949 case OP_FCONV_TO_U1:
4950 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
4952 case OP_FCONV_TO_I2:
4953 x86_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
4955 case OP_FCONV_TO_U2:
4956 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
4962 /*FIXME this causes a partial register stall, maybe it would not be that bad to use shift + mask + or*/
4963 /*The +4 is to get a mov ?h, ?l over the same reg.*/
4964 x86_mov_reg_reg (code, ins->dreg + 4, ins->dreg, 1);
4965 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 0);
4966 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 1);
4967 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
4970 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 0);
4971 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 1);
4972 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
4975 x86_movd_xreg_reg (code, ins->dreg, ins->sreg1);
4976 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
4979 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE, TRUE);
4980 x86_movd_xreg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
4981 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
4984 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
4985 x86_movsd_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
4986 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0x44);
4990 x86_sse_alu_ss_reg_reg (code, X86_SSE_CVTDQ2PD, ins->dreg, ins->sreg1);
4993 x86_sse_alu_ps_reg_reg (code, X86_SSE_CVTDQ2PS, ins->dreg, ins->sreg1);
4996 x86_sse_alu_sd_reg_reg (code, X86_SSE_CVTPD2DQ, ins->dreg, ins->sreg1);
4999 x86_sse_alu_pd_reg_reg (code, X86_SSE_CVTPD2PS, ins->dreg, ins->sreg1);
5002 x86_sse_alu_pd_reg_reg (code, X86_SSE_CVTPS2DQ, ins->dreg, ins->sreg1);
5005 x86_sse_alu_ps_reg_reg (code, X86_SSE_CVTPS2PD, ins->dreg, ins->sreg1);
5008 x86_sse_alu_pd_reg_reg (code, X86_SSE_CVTTPD2DQ, ins->dreg, ins->sreg1);
5011 x86_sse_alu_ss_reg_reg (code, X86_SSE_CVTTPS2DQ, ins->dreg, ins->sreg1);
5015 case OP_LIVERANGE_START: {
5016 if (cfg->verbose_level > 1)
5017 printf ("R%d START=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
5018 MONO_VARINFO (cfg, ins->inst_c0)->live_range_start = code - cfg->native_code;
5021 case OP_LIVERANGE_END: {
5022 if (cfg->verbose_level > 1)
5023 printf ("R%d END=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
5024 MONO_VARINFO (cfg, ins->inst_c0)->live_range_end = code - cfg->native_code;
5027 case OP_GC_SAFE_POINT: {
5028 const char *polling_func = NULL;
5029 int compare_val = 0;
5032 #if defined (USE_COOP_GC)
5033 polling_func = "mono_threads_state_poll";
5035 #elif defined(__native_client_codegen__) && defined(__native_client_gc__)
5036 polling_func = "mono_nacl_gc";
5037 compare_val = 0xFFFFFFFF;
5042 x86_test_membase_imm (code, ins->sreg1, 0, compare_val);
5043 br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
5044 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, polling_func);
5045 x86_patch (br [0], code);
5049 case OP_GC_LIVENESS_DEF:
5050 case OP_GC_LIVENESS_USE:
5051 case OP_GC_PARAM_SLOT_LIVENESS_DEF:
5052 ins->backend.pc_offset = code - cfg->native_code;
5054 case OP_GC_SPILL_SLOT_LIVENESS_DEF:
5055 ins->backend.pc_offset = code - cfg->native_code;
5056 bb->spill_slot_defs = g_slist_prepend_mempool (cfg->mempool, bb->spill_slot_defs, ins);
5059 x86_mov_reg_reg (code, ins->dreg, X86_ESP, sizeof (mgreg_t));
5062 x86_mov_reg_reg (code, X86_ESP, ins->sreg1, sizeof (mgreg_t));
5065 g_warning ("unknown opcode %s\n", mono_inst_name (ins->opcode));
5066 g_assert_not_reached ();
5069 if (G_UNLIKELY ((code - cfg->native_code - offset) > max_len)) {
5070 #ifndef __native_client_codegen__
5071 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
5072 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
5073 g_assert_not_reached ();
5074 #endif /* __native_client_codegen__ */
5080 cfg->code_len = code - cfg->native_code;
5083 #endif /* DISABLE_JIT */
5086 mono_arch_register_lowlevel_calls (void)
5091 mono_arch_patch_code_new (MonoCompile *cfg, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gpointer target)
5093 unsigned char *ip = ji->ip.i + code;
5096 case MONO_PATCH_INFO_IP:
5097 *((gconstpointer *)(ip)) = target;
5099 case MONO_PATCH_INFO_CLASS_INIT: {
5101 /* Might already been changed to a nop */
5102 x86_call_code (code, 0);
5103 x86_patch (ip, (unsigned char*)target);
5106 case MONO_PATCH_INFO_ABS:
5107 case MONO_PATCH_INFO_METHOD:
5108 case MONO_PATCH_INFO_METHOD_JUMP:
5109 case MONO_PATCH_INFO_INTERNAL_METHOD:
5110 case MONO_PATCH_INFO_BB:
5111 case MONO_PATCH_INFO_LABEL:
5112 case MONO_PATCH_INFO_RGCTX_FETCH:
5113 case MONO_PATCH_INFO_MONITOR_ENTER:
5114 case MONO_PATCH_INFO_MONITOR_ENTER_V4:
5115 case MONO_PATCH_INFO_MONITOR_EXIT:
5116 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
5117 #if defined(__native_client_codegen__) && defined(__native_client__)
5118 if (nacl_is_code_address (code)) {
5119 /* For tail calls, code is patched after being installed */
5120 /* but not through the normal "patch callsite" method. */
5121 unsigned char buf[kNaClAlignment];
5122 unsigned char *aligned_code = (uintptr_t)code & ~kNaClAlignmentMask;
5123 unsigned char *_target = target;
5125 /* All patch targets modified in x86_patch */
5126 /* are IP relative. */
5127 _target = _target + (uintptr_t)buf - (uintptr_t)aligned_code;
5128 memcpy (buf, aligned_code, kNaClAlignment);
5129 /* Patch a temp buffer of bundle size, */
5130 /* then install to actual location. */
5131 x86_patch (buf + ((uintptr_t)code - (uintptr_t)aligned_code), _target);
5132 ret = nacl_dyncode_modify (aligned_code, buf, kNaClAlignment);
5133 g_assert (ret == 0);
5136 x86_patch (ip, (unsigned char*)target);
5139 x86_patch (ip, (unsigned char*)target);
5142 case MONO_PATCH_INFO_NONE:
5144 case MONO_PATCH_INFO_R4:
5145 case MONO_PATCH_INFO_R8: {
5146 guint32 offset = mono_arch_get_patch_offset (ip);
5147 *((gconstpointer *)(ip + offset)) = target;
5151 guint32 offset = mono_arch_get_patch_offset (ip);
5152 #if !defined(__native_client__)
5153 *((gconstpointer *)(ip + offset)) = target;
5155 *((gconstpointer *)(ip + offset)) = nacl_modify_patch_target (target);
5162 static G_GNUC_UNUSED void
5163 stack_unaligned (MonoMethod *m, gpointer caller)
5165 printf ("%s\n", mono_method_full_name (m, TRUE));
5166 g_assert_not_reached ();
5170 mono_arch_emit_prolog (MonoCompile *cfg)
5172 MonoMethod *method = cfg->method;
5174 MonoMethodSignature *sig;
5176 int alloc_size, pos, max_offset, i, cfa_offset;
5178 gboolean need_stack_frame;
5179 #ifdef __native_client_codegen__
5180 guint alignment_check;
5183 cfg->code_size = MAX (cfg->header->code_size * 4, 10240);
5185 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
5186 cfg->code_size += 512;
5188 #if defined(__default_codegen__)
5189 code = cfg->native_code = g_malloc (cfg->code_size);
5190 #elif defined(__native_client_codegen__)
5191 /* native_code_alloc is not 32-byte aligned, native_code is. */
5192 cfg->code_size = NACL_BUNDLE_ALIGN_UP (cfg->code_size);
5193 cfg->native_code_alloc = g_malloc (cfg->code_size + kNaClAlignment);
5195 /* Align native_code to next nearest kNaclAlignment byte. */
5196 cfg->native_code = (guint)cfg->native_code_alloc + kNaClAlignment;
5197 cfg->native_code = (guint)cfg->native_code & ~kNaClAlignmentMask;
5199 code = cfg->native_code;
5201 alignment_check = (guint)cfg->native_code & kNaClAlignmentMask;
5202 g_assert(alignment_check == 0);
5209 /* Check that the stack is aligned on osx */
5210 x86_mov_reg_reg (code, X86_EAX, X86_ESP, sizeof (mgreg_t));
5211 x86_alu_reg_imm (code, X86_AND, X86_EAX, 15);
5212 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0xc);
5214 x86_branch_disp (code, X86_CC_Z, 0, FALSE);
5215 x86_push_membase (code, X86_ESP, 0);
5216 x86_push_imm (code, cfg->method);
5217 x86_mov_reg_imm (code, X86_EAX, stack_unaligned);
5218 x86_call_reg (code, X86_EAX);
5219 x86_patch (br [0], code);
5223 /* Offset between RSP and the CFA */
5227 cfa_offset = sizeof (gpointer);
5228 mono_emit_unwind_op_def_cfa (cfg, code, X86_ESP, sizeof (gpointer));
5229 // IP saved at CFA - 4
5230 /* There is no IP reg on x86 */
5231 mono_emit_unwind_op_offset (cfg, code, X86_NREG, -cfa_offset);
5232 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
5234 need_stack_frame = needs_stack_frame (cfg);
5236 if (need_stack_frame) {
5237 x86_push_reg (code, X86_EBP);
5238 cfa_offset += sizeof (gpointer);
5239 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
5240 mono_emit_unwind_op_offset (cfg, code, X86_EBP, - cfa_offset);
5241 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
5242 mono_emit_unwind_op_def_cfa_reg (cfg, code, X86_EBP);
5243 /* These are handled automatically by the stack marking code */
5244 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
5246 cfg->frame_reg = X86_ESP;
5249 cfg->stack_offset += cfg->param_area;
5250 cfg->stack_offset = ALIGN_TO (cfg->stack_offset, MONO_ARCH_FRAME_ALIGNMENT);
5252 alloc_size = cfg->stack_offset;
5255 if (!method->save_lmf) {
5256 if (cfg->used_int_regs & (1 << X86_EBX)) {
5257 x86_push_reg (code, X86_EBX);
5259 cfa_offset += sizeof (gpointer);
5260 mono_emit_unwind_op_offset (cfg, code, X86_EBX, - cfa_offset);
5261 /* These are handled automatically by the stack marking code */
5262 mini_gc_set_slot_type_from_cfa (cfg, - cfa_offset, SLOT_NOREF);
5265 if (cfg->used_int_regs & (1 << X86_EDI)) {
5266 x86_push_reg (code, X86_EDI);
5268 cfa_offset += sizeof (gpointer);
5269 mono_emit_unwind_op_offset (cfg, code, X86_EDI, - cfa_offset);
5270 mini_gc_set_slot_type_from_cfa (cfg, - cfa_offset, SLOT_NOREF);
5273 if (cfg->used_int_regs & (1 << X86_ESI)) {
5274 x86_push_reg (code, X86_ESI);
5276 cfa_offset += sizeof (gpointer);
5277 mono_emit_unwind_op_offset (cfg, code, X86_ESI, - cfa_offset);
5278 mini_gc_set_slot_type_from_cfa (cfg, - cfa_offset, SLOT_NOREF);
5284 /* the original alloc_size is already aligned: there is %ebp and retip pushed, so realign */
5285 if (mono_do_x86_stack_align && need_stack_frame) {
5286 int tot = alloc_size + pos + 4; /* ret ip */
5287 if (need_stack_frame)
5289 tot &= MONO_ARCH_FRAME_ALIGNMENT - 1;
5291 alloc_size += MONO_ARCH_FRAME_ALIGNMENT - tot;
5292 for (i = 0; i < MONO_ARCH_FRAME_ALIGNMENT - tot; i += sizeof (mgreg_t))
5293 mini_gc_set_slot_type_from_fp (cfg, - (alloc_size + pos - i), SLOT_NOREF);
5297 cfg->arch.sp_fp_offset = alloc_size + pos;
5300 /* See mono_emit_stack_alloc */
5301 #if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
5302 guint32 remaining_size = alloc_size;
5303 /*FIXME handle unbounded code expansion, we should use a loop in case of more than X interactions*/
5304 guint32 required_code_size = ((remaining_size / 0x1000) + 1) * 8; /*8 is the max size of x86_alu_reg_imm + x86_test_membase_reg*/
5305 guint32 offset = code - cfg->native_code;
5306 if (G_UNLIKELY (required_code_size >= (cfg->code_size - offset))) {
5307 while (required_code_size >= (cfg->code_size - offset))
5308 cfg->code_size *= 2;
5309 cfg->native_code = mono_realloc_native_code(cfg);
5310 code = cfg->native_code + offset;
5311 cfg->stat_code_reallocs++;
5313 while (remaining_size >= 0x1000) {
5314 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
5315 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
5316 remaining_size -= 0x1000;
5319 x86_alu_reg_imm (code, X86_SUB, X86_ESP, remaining_size);
5321 x86_alu_reg_imm (code, X86_SUB, X86_ESP, alloc_size);
5324 g_assert (need_stack_frame);
5327 if (cfg->method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED ||
5328 cfg->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
5329 x86_alu_reg_imm (code, X86_AND, X86_ESP, -MONO_ARCH_FRAME_ALIGNMENT);
5332 #if DEBUG_STACK_ALIGNMENT
5333 /* check the stack is aligned */
5334 if (need_stack_frame && method->wrapper_type == MONO_WRAPPER_NONE) {
5335 x86_mov_reg_reg (code, X86_ECX, X86_ESP, 4);
5336 x86_alu_reg_imm (code, X86_AND, X86_ECX, MONO_ARCH_FRAME_ALIGNMENT - 1);
5337 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
5338 x86_branch_disp (code, X86_CC_EQ, 3, FALSE);
5339 x86_breakpoint (code);
5343 /* compute max_offset in order to use short forward jumps */
5345 if (cfg->opt & MONO_OPT_BRANCH) {
5346 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5348 bb->max_offset = max_offset;
5350 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
5352 /* max alignment for loops */
5353 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
5354 max_offset += LOOP_ALIGNMENT;
5355 #ifdef __native_client_codegen__
5356 /* max alignment for native client */
5357 if (bb->flags & BB_INDIRECT_JUMP_TARGET || bb->flags & BB_EXCEPTION_HANDLER)
5358 max_offset += kNaClAlignment;
5360 MONO_BB_FOR_EACH_INS (bb, ins) {
5361 if (ins->opcode == OP_LABEL)
5362 ins->inst_c1 = max_offset;
5363 #ifdef __native_client_codegen__
5364 switch (ins->opcode)
5376 case OP_VOIDCALL_REG:
5378 case OP_FCALL_MEMBASE:
5379 case OP_LCALL_MEMBASE:
5380 case OP_VCALL_MEMBASE:
5381 case OP_VCALL2_MEMBASE:
5382 case OP_VOIDCALL_MEMBASE:
5383 case OP_CALL_MEMBASE:
5384 max_offset += kNaClAlignment;
5387 max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN] - 1;
5390 #endif /* __native_client_codegen__ */
5391 max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
5396 /* store runtime generic context */
5397 if (cfg->rgctx_var) {
5398 g_assert (cfg->rgctx_var->opcode == OP_REGOFFSET && cfg->rgctx_var->inst_basereg == X86_EBP);
5400 x86_mov_membase_reg (code, X86_EBP, cfg->rgctx_var->inst_offset, MONO_ARCH_RGCTX_REG, 4);
5403 if (method->save_lmf)
5404 code = emit_setup_lmf (cfg, code, cfg->lmf_var->inst_offset, cfa_offset);
5406 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
5407 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
5409 /* load arguments allocated to register from the stack */
5410 sig = mono_method_signature (method);
5413 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
5414 inst = cfg->args [pos];
5415 if (inst->opcode == OP_REGVAR) {
5416 g_assert (need_stack_frame);
5417 x86_mov_reg_membase (code, inst->dreg, X86_EBP, inst->inst_offset, 4);
5418 if (cfg->verbose_level > 2)
5419 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
5424 cfg->code_len = code - cfg->native_code;
5426 g_assert (cfg->code_len < cfg->code_size);
5432 mono_arch_emit_epilog (MonoCompile *cfg)
5434 MonoMethod *method = cfg->method;
5435 MonoMethodSignature *sig = mono_method_signature (method);
5437 guint32 stack_to_pop;
5439 int max_epilog_size = 16;
5441 gboolean need_stack_frame = needs_stack_frame (cfg);
5443 if (cfg->method->save_lmf)
5444 max_epilog_size += 128;
5446 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
5447 cfg->code_size *= 2;
5448 cfg->native_code = mono_realloc_native_code(cfg);
5449 cfg->stat_code_reallocs++;
5452 code = cfg->native_code + cfg->code_len;
5454 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
5455 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
5457 /* the code restoring the registers must be kept in sync with OP_TAILCALL */
5460 if (method->save_lmf) {
5461 gint32 lmf_offset = cfg->lmf_var->inst_offset;
5463 gboolean supported = FALSE;
5465 if (cfg->compile_aot) {
5466 #if defined(__APPLE__) || defined(__linux__)
5469 } else if (mono_get_jit_tls_offset () != -1) {
5473 /* check if we need to restore protection of the stack after a stack overflow */
5475 if (cfg->compile_aot) {
5476 code = emit_load_aotconst (NULL, code, cfg, NULL, X86_ECX, MONO_PATCH_INFO_TLS_OFFSET, GINT_TO_POINTER (TLS_KEY_JIT_TLS));
5478 code = emit_tls_get_reg (code, X86_ECX, X86_ECX);
5480 code = mono_x86_emit_tls_get (code, X86_ECX, mono_get_jit_tls_offset ());
5483 /* we load the value in a separate instruction: this mechanism may be
5484 * used later as a safer way to do thread interruption
5486 x86_mov_reg_membase (code, X86_ECX, X86_ECX, MONO_STRUCT_OFFSET (MonoJitTlsData, restore_stack_prot), 4);
5487 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
5489 x86_branch8 (code, X86_CC_Z, 0, FALSE);
5490 /* note that the call trampoline will preserve eax/edx */
5491 x86_call_reg (code, X86_ECX);
5492 x86_patch (patch, code);
5494 /* FIXME: maybe save the jit tls in the prolog */
5497 /* restore caller saved regs */
5498 if (cfg->used_int_regs & (1 << X86_EBX)) {
5499 x86_mov_reg_membase (code, X86_EBX, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, ebx), 4);
5502 if (cfg->used_int_regs & (1 << X86_EDI)) {
5503 x86_mov_reg_membase (code, X86_EDI, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, edi), 4);
5505 if (cfg->used_int_regs & (1 << X86_ESI)) {
5506 x86_mov_reg_membase (code, X86_ESI, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, esi), 4);
5509 /* EBP is restored by LEAVE */
5511 for (i = 0; i < X86_NREG; ++i) {
5512 if ((cfg->used_int_regs & X86_CALLER_REGS & (1 << i)) && (i != X86_EBP)) {
5518 g_assert (need_stack_frame);
5519 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
5523 g_assert (need_stack_frame);
5524 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
5527 if (cfg->used_int_regs & (1 << X86_ESI)) {
5528 x86_pop_reg (code, X86_ESI);
5530 if (cfg->used_int_regs & (1 << X86_EDI)) {
5531 x86_pop_reg (code, X86_EDI);
5533 if (cfg->used_int_regs & (1 << X86_EBX)) {
5534 x86_pop_reg (code, X86_EBX);
5538 /* Load returned vtypes into registers if needed */
5539 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
5540 if (cinfo->ret.storage == ArgValuetypeInReg) {
5541 for (quad = 0; quad < 2; quad ++) {
5542 switch (cinfo->ret.pair_storage [quad]) {
5544 x86_mov_reg_membase (code, cinfo->ret.pair_regs [quad], cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), 4);
5546 case ArgOnFloatFpStack:
5547 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), FALSE);
5549 case ArgOnDoubleFpStack:
5550 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), TRUE);
5555 g_assert_not_reached ();
5560 if (need_stack_frame)
5563 if (CALLCONV_IS_STDCALL (sig)) {
5564 MonoJitArgumentInfo *arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
5566 stack_to_pop = mono_arch_get_argument_info (NULL, sig, sig->param_count, arg_info);
5567 } else if (cinfo->callee_stack_pop)
5568 stack_to_pop = cinfo->callee_stack_pop;
5573 g_assert (need_stack_frame);
5574 x86_ret_imm (code, stack_to_pop);
5579 cfg->code_len = code - cfg->native_code;
5581 g_assert (cfg->code_len < cfg->code_size);
5585 mono_arch_emit_exceptions (MonoCompile *cfg)
5587 MonoJumpInfo *patch_info;
5590 MonoClass *exc_classes [16];
5591 guint8 *exc_throw_start [16], *exc_throw_end [16];
5595 /* Compute needed space */
5596 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5597 if (patch_info->type == MONO_PATCH_INFO_EXC)
5602 * make sure we have enough space for exceptions
5603 * 16 is the size of two push_imm instructions and a call
5605 if (cfg->compile_aot)
5606 code_size = exc_count * 32;
5608 code_size = exc_count * 16;
5610 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
5611 cfg->code_size *= 2;
5612 cfg->native_code = mono_realloc_native_code(cfg);
5613 cfg->stat_code_reallocs++;
5616 code = cfg->native_code + cfg->code_len;
5619 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5620 switch (patch_info->type) {
5621 case MONO_PATCH_INFO_EXC: {
5622 MonoClass *exc_class;
5626 x86_patch (patch_info->ip.i + cfg->native_code, code);
5628 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
5629 g_assert (exc_class);
5630 throw_ip = patch_info->ip.i;
5632 /* Find a throw sequence for the same exception class */
5633 for (i = 0; i < nthrows; ++i)
5634 if (exc_classes [i] == exc_class)
5637 x86_push_imm (code, (exc_throw_end [i] - cfg->native_code) - throw_ip);
5638 x86_jump_code (code, exc_throw_start [i]);
5639 patch_info->type = MONO_PATCH_INFO_NONE;
5644 /* Compute size of code following the push <OFFSET> */
5645 #if defined(__default_codegen__)
5647 #elif defined(__native_client_codegen__)
5648 code = mono_nacl_align (code);
5649 size = kNaClAlignment;
5651 /*This is aligned to 16 bytes by the callee. This way we save a few bytes here.*/
5653 if ((code - cfg->native_code) - throw_ip < 126 - size) {
5654 /* Use the shorter form */
5656 x86_push_imm (code, 0);
5660 x86_push_imm (code, 0xf0f0f0f0);
5665 exc_classes [nthrows] = exc_class;
5666 exc_throw_start [nthrows] = code;
5669 x86_push_imm (code, exc_class->type_token - MONO_TOKEN_TYPE_DEF);
5670 patch_info->data.name = "mono_arch_throw_corlib_exception";
5671 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
5672 patch_info->ip.i = code - cfg->native_code;
5673 x86_call_code (code, 0);
5674 x86_push_imm (buf, (code - cfg->native_code) - throw_ip);
5679 exc_throw_end [nthrows] = code;
5691 cfg->code_len = code - cfg->native_code;
5693 g_assert (cfg->code_len < cfg->code_size);
5697 mono_arch_flush_icache (guint8 *code, gint size)
5703 mono_arch_flush_register_windows (void)
5708 mono_arch_is_inst_imm (gint64 imm)
5714 mono_arch_finish_init (void)
5716 if (!g_getenv ("MONO_NO_TLS")) {
5717 #ifndef TARGET_WIN32
5719 optimize_for_xen = access ("/proc/xen", F_OK) == 0;
5726 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
5730 // Linear handler, the bsearch head compare is shorter
5731 //[2 + 4] x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
5732 //[1 + 1] x86_branch8(inst,cond,imm,is_signed)
5733 // x86_patch(ins,target)
5734 //[1 + 5] x86_jump_mem(inst,mem)
5737 #if defined(__default_codegen__)
5738 #define BR_SMALL_SIZE 2
5739 #define BR_LARGE_SIZE 5
5740 #elif defined(__native_client_codegen__)
5741 /* I suspect the size calculation below is actually incorrect. */
5742 /* TODO: fix the calculation that uses these sizes. */
5743 #define BR_SMALL_SIZE 16
5744 #define BR_LARGE_SIZE 12
5745 #endif /*__native_client_codegen__*/
5746 #define JUMP_IMM_SIZE 6
5747 #define ENABLE_WRONG_METHOD_CHECK 0
5751 imt_branch_distance (MonoIMTCheckItem **imt_entries, int start, int target)
5753 int i, distance = 0;
5754 for (i = start; i < target; ++i)
5755 distance += imt_entries [i]->chunk_size;
5760 * LOCKING: called with the domain lock held
5763 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
5764 gpointer fail_tramp)
5768 guint8 *code, *start;
5770 for (i = 0; i < count; ++i) {
5771 MonoIMTCheckItem *item = imt_entries [i];
5772 if (item->is_equals) {
5773 if (item->check_target_idx) {
5774 if (!item->compare_done)
5775 item->chunk_size += CMP_SIZE;
5776 item->chunk_size += BR_SMALL_SIZE + JUMP_IMM_SIZE;
5779 item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + JUMP_IMM_SIZE * 2;
5781 item->chunk_size += JUMP_IMM_SIZE;
5782 #if ENABLE_WRONG_METHOD_CHECK
5783 item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
5788 item->chunk_size += CMP_SIZE + BR_LARGE_SIZE;
5789 imt_entries [item->check_target_idx]->compare_done = TRUE;
5791 size += item->chunk_size;
5793 #if defined(__native_client__) && defined(__native_client_codegen__)
5794 /* In Native Client, we don't re-use thunks, allocate from the */
5795 /* normal code manager paths. */
5796 size = NACL_BUNDLE_ALIGN_UP (size);
5797 code = mono_domain_code_reserve (domain, size);
5800 code = mono_method_alloc_generic_virtual_thunk (domain, size);
5802 code = mono_domain_code_reserve (domain, size);
5805 for (i = 0; i < count; ++i) {
5806 MonoIMTCheckItem *item = imt_entries [i];
5807 item->code_target = code;
5808 if (item->is_equals) {
5809 if (item->check_target_idx) {
5810 if (!item->compare_done)
5811 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5812 item->jmp_code = code;
5813 x86_branch8 (code, X86_CC_NE, 0, FALSE);
5814 if (item->has_target_code)
5815 x86_jump_code (code, item->value.target_code);
5817 x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
5820 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5821 item->jmp_code = code;
5822 x86_branch8 (code, X86_CC_NE, 0, FALSE);
5823 if (item->has_target_code)
5824 x86_jump_code (code, item->value.target_code);
5826 x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
5827 x86_patch (item->jmp_code, code);
5828 x86_jump_code (code, fail_tramp);
5829 item->jmp_code = NULL;
5831 /* enable the commented code to assert on wrong method */
5832 #if ENABLE_WRONG_METHOD_CHECK
5833 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5834 item->jmp_code = code;
5835 x86_branch8 (code, X86_CC_NE, 0, FALSE);
5837 if (item->has_target_code)
5838 x86_jump_code (code, item->value.target_code);
5840 x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
5841 #if ENABLE_WRONG_METHOD_CHECK
5842 x86_patch (item->jmp_code, code);
5843 x86_breakpoint (code);
5844 item->jmp_code = NULL;
5849 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5850 item->jmp_code = code;
5851 if (x86_is_imm8 (imt_branch_distance (imt_entries, i, item->check_target_idx)))
5852 x86_branch8 (code, X86_CC_GE, 0, FALSE);
5854 x86_branch32 (code, X86_CC_GE, 0, FALSE);
5857 /* patch the branches to get to the target items */
5858 for (i = 0; i < count; ++i) {
5859 MonoIMTCheckItem *item = imt_entries [i];
5860 if (item->jmp_code) {
5861 if (item->check_target_idx) {
5862 x86_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
5868 mono_stats.imt_thunks_size += code - start;
5869 g_assert (code - start <= size);
5873 char *buff = g_strdup_printf ("thunk_for_class_%s_%s_entries_%d", vtable->klass->name_space, vtable->klass->name, count);
5874 mono_disassemble_code (NULL, (guint8*)start, code - start, buff);
5878 if (mono_jit_map_is_enabled ()) {
5881 buff = g_strdup_printf ("imt_%s_%s_entries_%d", vtable->klass->name_space, vtable->klass->name, count);
5883 buff = g_strdup_printf ("imt_thunk_entries_%d", count);
5884 mono_emit_jit_tramp (start, code - start, buff);
5888 nacl_domain_code_validate (domain, &start, size, &code);
5889 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE, NULL);
5895 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
5897 return (MonoMethod*) regs [MONO_ARCH_IMT_REG];
5901 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
5903 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
5907 mono_arch_get_cie_program (void)
5911 mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
5912 mono_add_unwind_op_offset (l, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
5918 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5920 MonoInst *ins = NULL;
5923 if (cmethod->klass == mono_defaults.math_class) {
5924 if (strcmp (cmethod->name, "Sin") == 0) {
5926 } else if (strcmp (cmethod->name, "Cos") == 0) {
5928 } else if (strcmp (cmethod->name, "Tan") == 0) {
5930 } else if (strcmp (cmethod->name, "Atan") == 0) {
5932 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
5934 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
5936 } else if (strcmp (cmethod->name, "Round") == 0 && fsig->param_count == 1 && fsig->params [0]->type == MONO_TYPE_R8) {
5940 if (opcode && fsig->param_count == 1) {
5941 MONO_INST_NEW (cfg, ins, opcode);
5942 ins->type = STACK_R8;
5943 ins->dreg = mono_alloc_freg (cfg);
5944 ins->sreg1 = args [0]->dreg;
5945 MONO_ADD_INS (cfg->cbb, ins);
5948 if (cfg->opt & MONO_OPT_CMOV) {
5951 if (strcmp (cmethod->name, "Min") == 0) {
5952 if (fsig->params [0]->type == MONO_TYPE_I4)
5954 } else if (strcmp (cmethod->name, "Max") == 0) {
5955 if (fsig->params [0]->type == MONO_TYPE_I4)
5959 if (opcode && fsig->param_count == 2) {
5960 MONO_INST_NEW (cfg, ins, opcode);
5961 ins->type = STACK_I4;
5962 ins->dreg = mono_alloc_ireg (cfg);
5963 ins->sreg1 = args [0]->dreg;
5964 ins->sreg2 = args [1]->dreg;
5965 MONO_ADD_INS (cfg->cbb, ins);
5970 /* OP_FREM is not IEEE compatible */
5971 else if (strcmp (cmethod->name, "IEEERemainder") == 0 && fsig->param_count == 2) {
5972 MONO_INST_NEW (cfg, ins, OP_FREM);
5973 ins->inst_i0 = args [0];
5974 ins->inst_i1 = args [1];
5983 mono_arch_print_tree (MonoInst *tree, int arity)
5989 mono_arch_get_patch_offset (guint8 *code)
5991 if ((code [0] == 0x8b) && (x86_modrm_mod (code [1]) == 0x2))
5993 else if (code [0] == 0xba)
5995 else if (code [0] == 0x68)
5998 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x6))
5999 /* push <OFFSET>(<REG>) */
6001 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x2))
6002 /* call *<OFFSET>(<REG>) */
6004 else if ((code [0] == 0xdd) || (code [0] == 0xd9))
6007 else if ((code [0] == 0x58) && (code [1] == 0x05))
6008 /* pop %eax; add <OFFSET>, %eax */
6010 else if ((code [0] >= 0x58) && (code [0] <= 0x58 + X86_NREG) && (code [1] == 0x81))
6011 /* pop <REG>; add <OFFSET>, <REG> */
6013 else if ((code [0] >= 0xb8) && (code [0] < 0xb8 + 8))
6014 /* mov <REG>, imm */
6017 g_assert_not_reached ();
6023 * mono_breakpoint_clean_code:
6025 * Copy @size bytes from @code - @offset to the buffer @buf. If the debugger inserted software
6026 * breakpoints in the original code, they are removed in the copy.
6028 * Returns TRUE if no sw breakpoint was present.
6031 mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size)
6034 * If method_start is non-NULL we need to perform bound checks, since we access memory
6035 * at code - offset we could go before the start of the method and end up in a different
6036 * page of memory that is not mapped or read incorrect data anyway. We zero-fill the bytes
6039 if (!method_start || code - offset >= method_start) {
6040 memcpy (buf, code - offset, size);
6042 int diff = code - method_start;
6043 memset (buf, 0, size);
6044 memcpy (buf + offset - diff, method_start, diff + size - offset);
6050 * mono_x86_get_this_arg_offset:
6052 * Return the offset of the stack location where this is passed during a virtual
6056 mono_x86_get_this_arg_offset (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig)
6062 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
6064 guint32 esp = regs [X86_ESP];
6071 * The stack looks like:
6075 res = ((MonoObject**)esp) [0];
6079 #define MAX_ARCH_DELEGATE_PARAMS 10
6082 get_delegate_invoke_impl (gboolean has_target, guint32 param_count, guint32 *code_len)
6084 guint8 *code, *start;
6085 int code_reserve = 64;
6088 * The stack contains:
6094 start = code = mono_global_codeman_reserve (code_reserve);
6096 /* Replace the this argument with the target */
6097 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
6098 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 4);
6099 x86_mov_membase_reg (code, X86_ESP, 4, X86_ECX, 4);
6100 x86_jump_membase (code, X86_EAX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
6102 g_assert ((code - start) < code_reserve);
6105 /* 8 for mov_reg and jump, plus 8 for each parameter */
6106 #ifdef __native_client_codegen__
6107 /* TODO: calculate this size correctly */
6108 code_reserve = 13 + (param_count * 8) + 2 * kNaClAlignment;
6110 code_reserve = 8 + (param_count * 8);
6111 #endif /* __native_client_codegen__ */
6113 * The stack contains:
6114 * <args in reverse order>
6119 * <args in reverse order>
6122 * without unbalancing the stack.
6123 * So move each arg up a spot in the stack (overwriting un-needed 'this' arg)
6124 * and leaving original spot of first arg as placeholder in stack so
6125 * when callee pops stack everything works.
6128 start = code = mono_global_codeman_reserve (code_reserve);
6130 /* store delegate for access to method_ptr */
6131 x86_mov_reg_membase (code, X86_ECX, X86_ESP, 4, 4);
6134 for (i = 0; i < param_count; ++i) {
6135 x86_mov_reg_membase (code, X86_EAX, X86_ESP, (i+2)*4, 4);
6136 x86_mov_membase_reg (code, X86_ESP, (i+1)*4, X86_EAX, 4);
6139 x86_jump_membase (code, X86_ECX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
6141 g_assert ((code - start) < code_reserve);
6144 nacl_global_codeman_validate (&start, code_reserve, &code);
6147 *code_len = code - start;
6149 if (mono_jit_map_is_enabled ()) {
6152 buff = (char*)"delegate_invoke_has_target";
6154 buff = g_strdup_printf ("delegate_invoke_no_target_%d", param_count);
6155 mono_emit_jit_tramp (start, code - start, buff);
6159 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
6164 #define MAX_VIRTUAL_DELEGATE_OFFSET 32
6167 get_delegate_virtual_invoke_impl (gboolean load_imt_reg, int offset, guint32 *code_size)
6169 guint8 *code, *start;
6173 * The stack contains:
6177 start = code = mono_global_codeman_reserve (size);
6179 /* Replace the this argument with the target */
6180 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
6181 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 4);
6182 x86_mov_membase_reg (code, X86_ESP, 4, X86_ECX, 4);
6185 /* Load the IMT reg */
6186 x86_mov_reg_membase (code, MONO_ARCH_IMT_REG, X86_EAX, MONO_STRUCT_OFFSET (MonoDelegate, method), 4);
6189 /* Load the vtable */
6190 x86_mov_reg_membase (code, X86_EAX, X86_ECX, MONO_STRUCT_OFFSET (MonoObject, vtable), 4);
6191 x86_jump_membase (code, X86_EAX, offset);
6192 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
6195 *code_size = code - start;
6201 mono_arch_get_delegate_invoke_impls (void)
6209 code = get_delegate_invoke_impl (TRUE, 0, &code_len);
6210 res = g_slist_prepend (res, mono_tramp_info_create ("delegate_invoke_impl_has_target", code, code_len, NULL, NULL));
6212 for (i = 0; i < MAX_ARCH_DELEGATE_PARAMS; ++i) {
6213 code = get_delegate_invoke_impl (FALSE, i, &code_len);
6214 tramp_name = g_strdup_printf ("delegate_invoke_impl_target_%d", i);
6215 res = g_slist_prepend (res, mono_tramp_info_create (tramp_name, code, code_len, NULL, NULL));
6216 g_free (tramp_name);
6219 for (i = 0; i < MAX_VIRTUAL_DELEGATE_OFFSET; ++i) {
6220 code = get_delegate_virtual_invoke_impl (TRUE, i * SIZEOF_VOID_P, &code_len);
6221 tramp_name = g_strdup_printf ("delegate_virtual_invoke_imt_%d", i);
6222 res = g_slist_prepend (res, mono_tramp_info_create (tramp_name, code, code_len, NULL, NULL));
6223 g_free (tramp_name);
6225 code = get_delegate_virtual_invoke_impl (FALSE, i * SIZEOF_VOID_P, &code_len);
6226 tramp_name = g_strdup_printf ("delegate_virtual_invoke_%d", i);
6227 res = g_slist_prepend (res, mono_tramp_info_create (tramp_name, code, code_len, NULL, NULL));
6228 g_free (tramp_name);
6235 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
6237 guint8 *code, *start;
6239 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
6242 /* FIXME: Support more cases */
6243 if (MONO_TYPE_ISSTRUCT (sig->ret))
6247 * The stack contains:
6253 static guint8* cached = NULL;
6258 start = mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
6260 start = get_delegate_invoke_impl (TRUE, 0, NULL);
6262 mono_memory_barrier ();
6266 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
6269 for (i = 0; i < sig->param_count; ++i)
6270 if (!mono_is_regsize_var (sig->params [i]))
6273 code = cache [sig->param_count];
6277 if (mono_aot_only) {
6278 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
6279 start = mono_aot_get_trampoline (name);
6282 start = get_delegate_invoke_impl (FALSE, sig->param_count, NULL);
6285 mono_memory_barrier ();
6287 cache [sig->param_count] = start;
6294 mono_arch_get_delegate_virtual_invoke_impl (MonoMethodSignature *sig, MonoMethod *method, int offset, gboolean load_imt_reg)
6296 return get_delegate_virtual_invoke_impl (load_imt_reg, offset, NULL);
6300 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
6303 case X86_EAX: return ctx->eax;
6304 case X86_EBX: return ctx->ebx;
6305 case X86_ECX: return ctx->ecx;
6306 case X86_EDX: return ctx->edx;
6307 case X86_ESP: return ctx->esp;
6308 case X86_EBP: return ctx->ebp;
6309 case X86_ESI: return ctx->esi;
6310 case X86_EDI: return ctx->edi;
6312 g_assert_not_reached ();
6318 mono_arch_context_set_int_reg (MonoContext *ctx, int reg, mgreg_t val)
6346 g_assert_not_reached ();
6350 #ifdef MONO_ARCH_SIMD_INTRINSICS
6353 get_float_to_x_spill_area (MonoCompile *cfg)
6355 if (!cfg->fconv_to_r8_x_var) {
6356 cfg->fconv_to_r8_x_var = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
6357 cfg->fconv_to_r8_x_var->flags |= MONO_INST_VOLATILE; /*FIXME, use the don't regalloc flag*/
6359 return cfg->fconv_to_r8_x_var;
6363 * Convert all fconv opts that MONO_OPT_SSE2 would get wrong.
6366 mono_arch_decompose_opts (MonoCompile *cfg, MonoInst *ins)
6369 int dreg, src_opcode;
6371 if (!(cfg->opt & MONO_OPT_SSE2) || !(cfg->opt & MONO_OPT_SIMD) || COMPILE_LLVM (cfg))
6374 switch (src_opcode = ins->opcode) {
6375 case OP_FCONV_TO_I1:
6376 case OP_FCONV_TO_U1:
6377 case OP_FCONV_TO_I2:
6378 case OP_FCONV_TO_U2:
6379 case OP_FCONV_TO_I4:
6386 /* dreg is the IREG and sreg1 is the FREG */
6387 MONO_INST_NEW (cfg, fconv, OP_FCONV_TO_R8_X);
6388 fconv->klass = NULL; /*FIXME, what can I use here as the Mono.Simd lib might not be loaded yet*/
6389 fconv->sreg1 = ins->sreg1;
6390 fconv->dreg = mono_alloc_ireg (cfg);
6391 fconv->type = STACK_VTYPE;
6392 fconv->backend.spill_var = get_float_to_x_spill_area (cfg);
6394 mono_bblock_insert_before_ins (cfg->cbb, ins, fconv);
6398 ins->opcode = OP_XCONV_R8_TO_I4;
6400 ins->klass = mono_defaults.int32_class;
6401 ins->sreg1 = fconv->dreg;
6403 ins->type = STACK_I4;
6404 ins->backend.source_opcode = src_opcode;
6407 #endif /* #ifdef MONO_ARCH_SIMD_INTRINSICS */
6410 mono_arch_decompose_long_opts (MonoCompile *cfg, MonoInst *long_ins)
6415 if (long_ins->opcode == OP_LNEG) {
6417 MONO_EMIT_NEW_UNALU (cfg, OP_INEG, ins->dreg + 1, ins->sreg1 + 1);
6418 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADC_IMM, ins->dreg + 2, ins->sreg1 + 2, 0);
6419 MONO_EMIT_NEW_UNALU (cfg, OP_INEG, ins->dreg + 2, ins->dreg + 2);
6424 #ifdef MONO_ARCH_SIMD_INTRINSICS
6426 if (!(cfg->opt & MONO_OPT_SIMD))
6429 /*TODO move this to simd-intrinsic.c once we support sse 4.1 dword extractors since we need the runtime caps info */
6430 switch (long_ins->opcode) {
6432 vreg = long_ins->sreg1;
6434 if (long_ins->inst_c0) {
6435 MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
6436 ins->klass = long_ins->klass;
6437 ins->sreg1 = long_ins->sreg1;
6439 ins->type = STACK_VTYPE;
6440 ins->dreg = vreg = alloc_ireg (cfg);
6441 MONO_ADD_INS (cfg->cbb, ins);
6444 MONO_INST_NEW (cfg, ins, OP_EXTRACT_I4);
6445 ins->klass = mono_defaults.int32_class;
6447 ins->type = STACK_I4;
6448 ins->dreg = long_ins->dreg + 1;
6449 MONO_ADD_INS (cfg->cbb, ins);
6451 MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
6452 ins->klass = long_ins->klass;
6453 ins->sreg1 = long_ins->sreg1;
6454 ins->inst_c0 = long_ins->inst_c0 ? 3 : 1;
6455 ins->type = STACK_VTYPE;
6456 ins->dreg = vreg = alloc_ireg (cfg);
6457 MONO_ADD_INS (cfg->cbb, ins);
6459 MONO_INST_NEW (cfg, ins, OP_EXTRACT_I4);
6460 ins->klass = mono_defaults.int32_class;
6462 ins->type = STACK_I4;
6463 ins->dreg = long_ins->dreg + 2;
6464 MONO_ADD_INS (cfg->cbb, ins);
6466 long_ins->opcode = OP_NOP;
6468 case OP_INSERTX_I8_SLOW:
6469 MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
6470 ins->dreg = long_ins->dreg;
6471 ins->sreg1 = long_ins->dreg;
6472 ins->sreg2 = long_ins->sreg2 + 1;
6473 ins->inst_c0 = long_ins->inst_c0 * 2;
6474 MONO_ADD_INS (cfg->cbb, ins);
6476 MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
6477 ins->dreg = long_ins->dreg;
6478 ins->sreg1 = long_ins->dreg;
6479 ins->sreg2 = long_ins->sreg2 + 2;
6480 ins->inst_c0 = long_ins->inst_c0 * 2 + 1;
6481 MONO_ADD_INS (cfg->cbb, ins);
6483 long_ins->opcode = OP_NOP;
6486 MONO_INST_NEW (cfg, ins, OP_ICONV_TO_X);
6487 ins->dreg = long_ins->dreg;
6488 ins->sreg1 = long_ins->sreg1 + 1;
6489 ins->klass = long_ins->klass;
6490 ins->type = STACK_VTYPE;
6491 MONO_ADD_INS (cfg->cbb, ins);
6493 MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
6494 ins->dreg = long_ins->dreg;
6495 ins->sreg1 = long_ins->dreg;
6496 ins->sreg2 = long_ins->sreg1 + 2;
6498 ins->klass = long_ins->klass;
6499 ins->type = STACK_VTYPE;
6500 MONO_ADD_INS (cfg->cbb, ins);
6502 MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
6503 ins->dreg = long_ins->dreg;
6504 ins->sreg1 = long_ins->dreg;;
6505 ins->inst_c0 = 0x44; /*Magic number for swizzling (X,Y,X,Y)*/
6506 ins->klass = long_ins->klass;
6507 ins->type = STACK_VTYPE;
6508 MONO_ADD_INS (cfg->cbb, ins);
6510 long_ins->opcode = OP_NOP;
6513 #endif /* MONO_ARCH_SIMD_INTRINSICS */
6516 /*MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD*/
6518 mono_arch_install_handler_block_guard (MonoJitInfo *ji, MonoJitExceptionInfo *clause, MonoContext *ctx, gpointer new_value)
6521 gpointer *sp, old_value;
6524 offset = clause->exvar_offset;
6527 bp = MONO_CONTEXT_GET_BP (ctx);
6528 sp = *(gpointer*)(bp + offset);
6531 if (old_value < ji->code_start || (char*)old_value > ((char*)ji->code_start + ji->code_size))
6540 * mono_aot_emit_load_got_addr:
6542 * Emit code to load the got address.
6543 * On x86, the result is placed into EBX.
6546 mono_arch_emit_load_got_addr (guint8 *start, guint8 *code, MonoCompile *cfg, MonoJumpInfo **ji)
6548 x86_call_imm (code, 0);
6550 * The patch needs to point to the pop, since the GOT offset needs
6551 * to be added to that address.
6554 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GOT_OFFSET, NULL);
6556 *ji = mono_patch_info_list_prepend (*ji, code - start, MONO_PATCH_INFO_GOT_OFFSET, NULL);
6557 x86_pop_reg (code, MONO_ARCH_GOT_REG);
6558 x86_alu_reg_imm (code, X86_ADD, MONO_ARCH_GOT_REG, 0xf0f0f0f0);
6564 emit_load_aotconst (guint8 *start, guint8 *code, MonoCompile *cfg, MonoJumpInfo **ji, int dreg, int tramp_type, gconstpointer target)
6567 mono_add_patch_info (cfg, code - cfg->native_code, tramp_type, target);
6569 g_assert_not_reached ();
6570 x86_mov_reg_membase (code, dreg, MONO_ARCH_GOT_REG, 0xf0f0f0f0, 4);
6575 * mono_arch_emit_load_aotconst:
6577 * Emit code to load the contents of the GOT slot identified by TRAMP_TYPE and
6578 * TARGET from the mscorlib GOT in full-aot code.
6579 * On x86, the GOT address is assumed to be in EBX, and the result is placed into
6583 mono_arch_emit_load_aotconst (guint8 *start, guint8 *code, MonoJumpInfo **ji, int tramp_type, gconstpointer target)
6585 /* Load the mscorlib got address */
6586 x86_mov_reg_membase (code, X86_EAX, MONO_ARCH_GOT_REG, sizeof (gpointer), 4);
6587 *ji = mono_patch_info_list_prepend (*ji, code - start, tramp_type, target);
6588 /* arch_emit_got_access () patches this */
6589 x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0xf0f0f0f0, 4);
6594 /* Can't put this into mini-x86.h */
6596 mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot);
6599 mono_arch_get_trampolines (gboolean aot)
6601 MonoTrampInfo *info;
6602 GSList *tramps = NULL;
6604 mono_x86_get_signal_exception_trampoline (&info, aot);
6606 tramps = g_slist_append (tramps, info);
6613 #define DBG_SIGNAL SIGBUS
6615 #define DBG_SIGNAL SIGSEGV
6618 /* Soft Debug support */
6619 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
6622 * mono_arch_set_breakpoint:
6624 * Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
6625 * The location should contain code emitted by OP_SEQ_POINT.
6628 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
6633 * In production, we will use int3 (has to fix the size in the md
6634 * file). But that could confuse gdb, so during development, we emit a SIGSEGV
6637 g_assert (code [0] == 0x90);
6638 x86_alu_reg_mem (code, X86_CMP, X86_EAX, (guint32)bp_trigger_page);
6642 * mono_arch_clear_breakpoint:
6644 * Clear the breakpoint at IP.
6647 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
6652 for (i = 0; i < 6; ++i)
6657 * mono_arch_start_single_stepping:
6659 * Start single stepping.
6662 mono_arch_start_single_stepping (void)
6664 mono_mprotect (ss_trigger_page, mono_pagesize (), 0);
6668 * mono_arch_stop_single_stepping:
6670 * Stop single stepping.
6673 mono_arch_stop_single_stepping (void)
6675 mono_mprotect (ss_trigger_page, mono_pagesize (), MONO_MMAP_READ);
6679 * mono_arch_is_single_step_event:
6681 * Return whenever the machine state in SIGCTX corresponds to a single
6685 mono_arch_is_single_step_event (void *info, void *sigctx)
6688 EXCEPTION_RECORD* einfo = ((EXCEPTION_POINTERS*)info)->ExceptionRecord; /* Sometimes the address is off by 4 */
6690 if (((gpointer)einfo->ExceptionInformation[1] >= ss_trigger_page && (guint8*)einfo->ExceptionInformation[1] <= (guint8*)ss_trigger_page + 128))
6695 siginfo_t* sinfo = (siginfo_t*) info;
6696 /* Sometimes the address is off by 4 */
6697 if (sinfo->si_signo == DBG_SIGNAL && (sinfo->si_addr >= ss_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)ss_trigger_page + 128))
6705 mono_arch_is_breakpoint_event (void *info, void *sigctx)
6708 EXCEPTION_RECORD* einfo = ((EXCEPTION_POINTERS*)info)->ExceptionRecord; /* Sometimes the address is off by 4 */
6709 if (((gpointer)einfo->ExceptionInformation[1] >= bp_trigger_page && (guint8*)einfo->ExceptionInformation[1] <= (guint8*)bp_trigger_page + 128))
6714 siginfo_t* sinfo = (siginfo_t*)info;
6715 /* Sometimes the address is off by 4 */
6716 if (sinfo->si_signo == DBG_SIGNAL && (sinfo->si_addr >= bp_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)bp_trigger_page + 128))
6723 #define BREAKPOINT_SIZE 6
6726 * mono_arch_skip_breakpoint:
6728 * See mini-amd64.c for docs.
6731 mono_arch_skip_breakpoint (MonoContext *ctx, MonoJitInfo *ji)
6733 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + BREAKPOINT_SIZE);
6737 * mono_arch_skip_single_step:
6739 * See mini-amd64.c for docs.
6742 mono_arch_skip_single_step (MonoContext *ctx)
6744 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 6);
6748 * mono_arch_get_seq_point_info:
6750 * See mini-amd64.c for docs.
6753 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
6760 mono_arch_init_lmf_ext (MonoLMFExt *ext, gpointer prev_lmf)
6762 ext->lmf.previous_lmf = (gsize)prev_lmf;
6763 /* Mark that this is a MonoLMFExt */
6764 ext->lmf.previous_lmf = (gsize)(gpointer)(((gssize)ext->lmf.previous_lmf) | 2);
6765 ext->lmf.ebp = (gssize)ext;
6771 mono_arch_opcode_supported (int opcode)
6774 case OP_ATOMIC_ADD_I4:
6775 case OP_ATOMIC_EXCHANGE_I4:
6776 case OP_ATOMIC_CAS_I4:
6777 case OP_ATOMIC_LOAD_I1:
6778 case OP_ATOMIC_LOAD_I2:
6779 case OP_ATOMIC_LOAD_I4:
6780 case OP_ATOMIC_LOAD_U1:
6781 case OP_ATOMIC_LOAD_U2:
6782 case OP_ATOMIC_LOAD_U4:
6783 case OP_ATOMIC_LOAD_R4:
6784 case OP_ATOMIC_LOAD_R8:
6785 case OP_ATOMIC_STORE_I1:
6786 case OP_ATOMIC_STORE_I2:
6787 case OP_ATOMIC_STORE_I4:
6788 case OP_ATOMIC_STORE_U1:
6789 case OP_ATOMIC_STORE_U2:
6790 case OP_ATOMIC_STORE_U4:
6791 case OP_ATOMIC_STORE_R4:
6792 case OP_ATOMIC_STORE_R8:
6799 #if defined(ENABLE_GSHAREDVT)
6801 #include "../../../mono-extensions/mono/mini/mini-x86-gsharedvt.c"
6803 #endif /* !MONOTOUCH */