2 * mini-x86.c: x86 backend for the Mono code generator
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
9 * Copyright 2003 Ximian, Inc.
10 * Copyright 2003-2011 Novell Inc.
11 * Copyright 2011 Xamarin Inc.
20 #include <mono/metadata/appdomain.h>
21 #include <mono/metadata/debug-helpers.h>
22 #include <mono/metadata/threads.h>
23 #include <mono/metadata/profiler-private.h>
24 #include <mono/metadata/mono-debug.h>
25 #include <mono/metadata/gc-internal.h>
26 #include <mono/utils/mono-math.h>
27 #include <mono/utils/mono-counters.h>
28 #include <mono/utils/mono-mmap.h>
29 #include <mono/utils/mono-memory-model.h>
30 #include <mono/utils/mono-hwcap-x86.h>
38 /* On windows, these hold the key returned by TlsAlloc () */
40 static gint jit_tls_offset = -1;
42 static gint lmf_addr_tls_offset = -1;
46 static gboolean optimize_for_xen = TRUE;
48 #define optimize_for_xen 0
51 /* This mutex protects architecture specific caches */
52 #define mono_mini_arch_lock() EnterCriticalSection (&mini_arch_mutex)
53 #define mono_mini_arch_unlock() LeaveCriticalSection (&mini_arch_mutex)
54 static CRITICAL_SECTION mini_arch_mutex;
56 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
61 /* Under windows, the default pinvoke calling convention is stdcall */
62 #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))
64 #define CALLCONV_IS_STDCALL(sig) (((sig)->call_convention) == MONO_CALL_STDCALL || ((sig)->pinvoke && ((sig)->call_convention) == MONO_CALL_THISCALL))
67 #define X86_IS_CALLEE_SAVED_REG(reg) (((reg) == X86_EBX) || ((reg) == X86_EDI) || ((reg) == X86_ESI))
70 mono_breakpoint_info [MONO_BREAKPOINT_ARRAY_SIZE];
73 emit_load_aotconst (guint8 *start, guint8 *code, MonoCompile *cfg, MonoJumpInfo **ji, int dreg, int tramp_type, gconstpointer target);
75 #ifdef __native_client_codegen__
77 /* Default alignment for Native Client is 32-byte. */
78 gint8 nacl_align_byte = -32; /* signed version of 0xe0 */
80 /* mono_arch_nacl_pad: Add pad bytes of alignment instructions at code, */
81 /* Check that alignment doesn't cross an alignment boundary. */
83 mono_arch_nacl_pad (guint8 *code, int pad)
85 const int kMaxPadding = 7; /* see x86-codegen.h: x86_padding() */
87 if (pad == 0) return code;
88 /* assertion: alignment cannot cross a block boundary */
89 g_assert(((uintptr_t)code & (~kNaClAlignmentMask)) ==
90 (((uintptr_t)code + pad - 1) & (~kNaClAlignmentMask)));
91 while (pad >= kMaxPadding) {
92 x86_padding (code, kMaxPadding);
95 if (pad != 0) x86_padding (code, pad);
100 mono_arch_nacl_skip_nops (guint8 *code)
102 x86_skip_nops (code);
106 #endif /* __native_client_codegen__ */
109 * The code generated for sequence points reads from this location, which is
110 * made read-only when single stepping is enabled.
112 static gpointer ss_trigger_page;
114 /* Enabled breakpoints read from this trigger page */
115 static gpointer bp_trigger_page;
118 mono_arch_regname (int reg)
121 case X86_EAX: return "%eax";
122 case X86_EBX: return "%ebx";
123 case X86_ECX: return "%ecx";
124 case X86_EDX: return "%edx";
125 case X86_ESP: return "%esp";
126 case X86_EBP: return "%ebp";
127 case X86_EDI: return "%edi";
128 case X86_ESI: return "%esi";
134 mono_arch_fregname (int reg)
159 mono_arch_xregname (int reg)
184 mono_x86_patch (unsigned char* code, gpointer target)
186 x86_patch (code, (unsigned char*)target);
197 /* gsharedvt argument passed by addr */
209 /* Only if storage == ArgValuetypeInReg */
210 ArgStorage pair_storage [2];
219 gboolean need_stack_align;
220 guint32 stack_align_amount;
221 gboolean vtype_retaddr;
222 /* The index of the vret arg in the argument list */
230 #define FLOAT_PARAM_REGS 0
232 static const guint32 thiscall_param_regs [] = { X86_ECX, X86_NREG };
234 static const guint32 *callconv_param_regs(MonoMethodSignature *sig)
239 switch (sig->call_convention) {
240 case MONO_CALL_THISCALL:
241 return thiscall_param_regs;
247 #if defined(TARGET_WIN32) || defined(__APPLE__) || defined(__FreeBSD__)
248 #define SMALL_STRUCTS_IN_REGS
249 static X86_Reg_No return_regs [] = { X86_EAX, X86_EDX };
253 add_general (guint32 *gr, const guint32 *param_regs, guint32 *stack_size, ArgInfo *ainfo)
255 ainfo->offset = *stack_size;
257 if (!param_regs || param_regs [*gr] == X86_NREG) {
258 ainfo->storage = ArgOnStack;
260 (*stack_size) += sizeof (gpointer);
263 ainfo->storage = ArgInIReg;
264 ainfo->reg = param_regs [*gr];
270 add_general_pair (guint32 *gr, const guint32 *param_regs , guint32 *stack_size, ArgInfo *ainfo)
272 ainfo->offset = *stack_size;
274 g_assert(!param_regs || param_regs[*gr] == X86_NREG);
276 ainfo->storage = ArgOnStack;
277 (*stack_size) += sizeof (gpointer) * 2;
282 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
284 ainfo->offset = *stack_size;
286 if (*gr >= FLOAT_PARAM_REGS) {
287 ainfo->storage = ArgOnStack;
288 (*stack_size) += is_double ? 8 : 4;
289 ainfo->nslots = is_double ? 2 : 1;
292 /* A double register */
294 ainfo->storage = ArgInDoubleSSEReg;
296 ainfo->storage = ArgInFloatSSEReg;
304 add_valuetype (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
306 guint32 *gr, const guint32 *param_regs, guint32 *fr, guint32 *stack_size)
311 klass = mono_class_from_mono_type (type);
312 size = mini_type_stack_size_full (gsctx, &klass->byval_arg, NULL, sig->pinvoke);
314 #ifdef SMALL_STRUCTS_IN_REGS
315 if (sig->pinvoke && is_return) {
316 MonoMarshalType *info;
319 * the exact rules are not very well documented, the code below seems to work with the
320 * code generated by gcc 3.3.3 -mno-cygwin.
322 info = mono_marshal_load_type_info (klass);
325 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
327 /* Special case structs with only a float member */
328 if (info->num_fields == 1) {
329 int ftype = mini_replace_type (info->fields [0].field->type)->type;
330 if ((info->native_size == 8) && (ftype == MONO_TYPE_R8)) {
331 ainfo->storage = ArgValuetypeInReg;
332 ainfo->pair_storage [0] = ArgOnDoubleFpStack;
335 if ((info->native_size == 4) && (ftype == MONO_TYPE_R4)) {
336 ainfo->storage = ArgValuetypeInReg;
337 ainfo->pair_storage [0] = ArgOnFloatFpStack;
341 if ((info->native_size == 1) || (info->native_size == 2) || (info->native_size == 4) || (info->native_size == 8)) {
342 ainfo->storage = ArgValuetypeInReg;
343 ainfo->pair_storage [0] = ArgInIReg;
344 ainfo->pair_regs [0] = return_regs [0];
345 if (info->native_size > 4) {
346 ainfo->pair_storage [1] = ArgInIReg;
347 ainfo->pair_regs [1] = return_regs [1];
354 if (param_regs && param_regs [*gr] != X86_NREG && !is_return) {
355 g_assert (size <= 4);
356 ainfo->storage = ArgValuetypeInReg;
357 ainfo->reg = param_regs [*gr];
362 ainfo->offset = *stack_size;
363 ainfo->storage = ArgOnStack;
364 *stack_size += ALIGN_TO (size, sizeof (gpointer));
365 ainfo->nslots = ALIGN_TO (size, sizeof (gpointer)) / sizeof (gpointer);
371 * Obtain information about a call according to the calling convention.
372 * For x86 ELF, see the "System V Application Binary Interface Intel386
373 * Architecture Processor Supplment, Fourth Edition" document for more
375 * For x86 win32, see ???.
378 get_call_info_internal (MonoGenericSharingContext *gsctx, CallInfo *cinfo, MonoMethodSignature *sig)
380 guint32 i, gr, fr, pstart;
381 const guint32 *param_regs;
383 int n = sig->hasthis + sig->param_count;
384 guint32 stack_size = 0;
385 gboolean is_pinvoke = sig->pinvoke;
391 param_regs = callconv_param_regs(sig);
395 ret_type = mini_type_get_underlying_type (gsctx, sig->ret);
396 switch (ret_type->type) {
397 case MONO_TYPE_BOOLEAN:
408 case MONO_TYPE_FNPTR:
409 case MONO_TYPE_CLASS:
410 case MONO_TYPE_OBJECT:
411 case MONO_TYPE_SZARRAY:
412 case MONO_TYPE_ARRAY:
413 case MONO_TYPE_STRING:
414 cinfo->ret.storage = ArgInIReg;
415 cinfo->ret.reg = X86_EAX;
419 cinfo->ret.storage = ArgInIReg;
420 cinfo->ret.reg = X86_EAX;
421 cinfo->ret.is_pair = TRUE;
424 cinfo->ret.storage = ArgOnFloatFpStack;
427 cinfo->ret.storage = ArgOnDoubleFpStack;
429 case MONO_TYPE_GENERICINST:
430 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
431 cinfo->ret.storage = ArgInIReg;
432 cinfo->ret.reg = X86_EAX;
435 if (mini_is_gsharedvt_type_gsctx (gsctx, ret_type)) {
436 cinfo->ret.storage = ArgOnStack;
437 cinfo->vtype_retaddr = TRUE;
441 case MONO_TYPE_VALUETYPE:
442 case MONO_TYPE_TYPEDBYREF: {
443 guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
445 add_valuetype (gsctx, sig, &cinfo->ret, ret_type, TRUE, &tmp_gr, NULL, &tmp_fr, &tmp_stacksize);
446 if (cinfo->ret.storage == ArgOnStack) {
447 cinfo->vtype_retaddr = TRUE;
448 /* The caller passes the address where the value is stored */
454 g_assert (mini_is_gsharedvt_type_gsctx (gsctx, ret_type));
455 cinfo->ret.storage = ArgOnStack;
456 cinfo->vtype_retaddr = TRUE;
459 cinfo->ret.storage = ArgNone;
462 g_error ("Can't handle as return value 0x%x", ret_type->type);
468 * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
469 * the first argument, allowing 'this' to be always passed in the first arg reg.
470 * Also do this if the first argument is a reference type, since virtual calls
471 * are sometimes made using calli without sig->hasthis set, like in the delegate
474 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]))))) {
476 add_general (&gr, param_regs, &stack_size, cinfo->args + 0);
478 add_general (&gr, param_regs, &stack_size, &cinfo->args [sig->hasthis + 0]);
481 cinfo->vret_arg_offset = stack_size;
482 add_general (&gr, NULL, &stack_size, &cinfo->ret);
483 cinfo->vret_arg_index = 1;
487 add_general (&gr, param_regs, &stack_size, cinfo->args + 0);
489 if (cinfo->vtype_retaddr)
490 add_general (&gr, NULL, &stack_size, &cinfo->ret);
493 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
494 fr = FLOAT_PARAM_REGS;
496 /* Emit the signature cookie just before the implicit arguments */
497 add_general (&gr, param_regs, &stack_size, &cinfo->sig_cookie);
500 for (i = pstart; i < sig->param_count; ++i) {
501 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
504 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
505 /* We allways pass the sig cookie on the stack for simplicity */
507 * Prevent implicit arguments + the sig cookie from being passed
510 fr = FLOAT_PARAM_REGS;
512 /* Emit the signature cookie just before the implicit arguments */
513 add_general (&gr, param_regs, &stack_size, &cinfo->sig_cookie);
516 if (sig->params [i]->byref) {
517 add_general (&gr, param_regs, &stack_size, ainfo);
520 ptype = mini_type_get_underlying_type (gsctx, sig->params [i]);
521 switch (ptype->type) {
522 case MONO_TYPE_BOOLEAN:
525 add_general (&gr, param_regs, &stack_size, ainfo);
530 add_general (&gr, param_regs, &stack_size, ainfo);
534 add_general (&gr, param_regs, &stack_size, ainfo);
539 case MONO_TYPE_FNPTR:
540 case MONO_TYPE_CLASS:
541 case MONO_TYPE_OBJECT:
542 case MONO_TYPE_STRING:
543 case MONO_TYPE_SZARRAY:
544 case MONO_TYPE_ARRAY:
545 add_general (&gr, param_regs, &stack_size, ainfo);
547 case MONO_TYPE_GENERICINST:
548 if (!mono_type_generic_inst_is_valuetype (ptype)) {
549 add_general (&gr, param_regs, &stack_size, ainfo);
552 if (mini_is_gsharedvt_type_gsctx (gsctx, ptype)) {
553 /* gsharedvt arguments are passed by ref */
554 add_general (&gr, param_regs, &stack_size, ainfo);
555 g_assert (ainfo->storage == ArgOnStack);
556 ainfo->storage = ArgGSharedVt;
560 case MONO_TYPE_VALUETYPE:
561 case MONO_TYPE_TYPEDBYREF:
562 add_valuetype (gsctx, sig, ainfo, ptype, FALSE, &gr, param_regs, &fr, &stack_size);
566 add_general_pair (&gr, param_regs, &stack_size, ainfo);
569 add_float (&fr, &stack_size, ainfo, FALSE);
572 add_float (&fr, &stack_size, ainfo, TRUE);
576 /* gsharedvt arguments are passed by ref */
577 g_assert (mini_is_gsharedvt_type_gsctx (gsctx, ptype));
578 add_general (&gr, param_regs, &stack_size, ainfo);
579 g_assert (ainfo->storage == ArgOnStack);
580 ainfo->storage = ArgGSharedVt;
583 g_error ("unexpected type 0x%x", ptype->type);
584 g_assert_not_reached ();
588 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
589 fr = FLOAT_PARAM_REGS;
591 /* Emit the signature cookie just before the implicit arguments */
592 add_general (&gr, param_regs, &stack_size, &cinfo->sig_cookie);
595 if (mono_do_x86_stack_align && (stack_size % MONO_ARCH_FRAME_ALIGNMENT) != 0) {
596 cinfo->need_stack_align = TRUE;
597 cinfo->stack_align_amount = MONO_ARCH_FRAME_ALIGNMENT - (stack_size % MONO_ARCH_FRAME_ALIGNMENT);
598 stack_size += cinfo->stack_align_amount;
601 cinfo->stack_usage = stack_size;
602 cinfo->reg_usage = gr;
603 cinfo->freg_usage = fr;
608 get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSignature *sig)
610 int n = sig->hasthis + sig->param_count;
614 cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
616 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
618 return get_call_info_internal (gsctx, cinfo, sig);
622 * mono_arch_get_argument_info:
623 * @csig: a method signature
624 * @param_count: the number of parameters to consider
625 * @arg_info: an array to store the result infos
627 * Gathers information on parameters such as size, alignment and
628 * padding. arg_info should be large enought to hold param_count + 1 entries.
630 * Returns the size of the argument area on the stack.
631 * This should be signal safe, since it is called from
632 * mono_arch_find_jit_info ().
633 * FIXME: The metadata calls might not be signal safe.
636 mono_arch_get_argument_info (MonoGenericSharingContext *gsctx, MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
638 int len, k, args_size = 0;
644 /* Avoid g_malloc as it is not signal safe */
645 len = sizeof (CallInfo) + (sizeof (ArgInfo) * (csig->param_count + 1));
646 cinfo = (CallInfo*)g_newa (guint8*, len);
647 memset (cinfo, 0, len);
649 cinfo = get_call_info_internal (gsctx, cinfo, csig);
651 arg_info [0].offset = offset;
653 if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 0) {
654 args_size += sizeof (gpointer);
659 args_size += sizeof (gpointer);
663 if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 1 && csig->hasthis) {
664 /* Emitted after this */
665 args_size += sizeof (gpointer);
669 arg_info [0].size = args_size;
671 for (k = 0; k < param_count; k++) {
672 size = mini_type_stack_size_full (NULL, csig->params [k], &align, csig->pinvoke);
674 /* ignore alignment for now */
677 args_size += pad = (align - (args_size & (align - 1))) & (align - 1);
678 arg_info [k].pad = pad;
680 arg_info [k + 1].pad = 0;
681 arg_info [k + 1].size = size;
683 arg_info [k + 1].offset = offset;
686 if (k == 0 && cinfo->vtype_retaddr && cinfo->vret_arg_index == 1 && !csig->hasthis) {
687 /* Emitted after the first arg */
688 args_size += sizeof (gpointer);
693 if (mono_do_x86_stack_align && !CALLCONV_IS_STDCALL (csig))
694 align = MONO_ARCH_FRAME_ALIGNMENT;
697 args_size += pad = (align - (args_size & (align - 1))) & (align - 1);
698 arg_info [k].pad = pad;
704 mono_arch_tail_call_supported (MonoMethodSignature *caller_sig, MonoMethodSignature *callee_sig)
706 MonoType *callee_ret;
710 c1 = get_call_info (NULL, NULL, caller_sig);
711 c2 = get_call_info (NULL, NULL, callee_sig);
713 * Tail calls with more callee stack usage than the caller cannot be supported, since
714 * the extra stack space would be left on the stack after the tail call.
716 res = c1->stack_usage >= c2->stack_usage;
717 callee_ret = mini_replace_type (callee_sig->ret);
718 if (callee_ret && MONO_TYPE_ISSTRUCT (callee_ret) && c2->ret.storage != ArgValuetypeInReg)
719 /* An address on the callee's stack is passed as the first argument */
729 * Initialize the cpu to execute managed code.
732 mono_arch_cpu_init (void)
734 /* spec compliance requires running with double precision */
738 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
739 fpcw &= ~X86_FPCW_PRECC_MASK;
740 fpcw |= X86_FPCW_PREC_DOUBLE;
741 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
742 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
744 _control87 (_PC_53, MCW_PC);
749 * Initialize architecture specific code.
752 mono_arch_init (void)
754 InitializeCriticalSection (&mini_arch_mutex);
756 ss_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ);
757 bp_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_32BIT);
758 mono_mprotect (bp_trigger_page, mono_pagesize (), 0);
760 mono_aot_register_jit_icall ("mono_x86_throw_exception", mono_x86_throw_exception);
761 mono_aot_register_jit_icall ("mono_x86_throw_corlib_exception", mono_x86_throw_corlib_exception);
762 #if defined(MONOTOUCH) || defined(MONO_EXTENSIONS)
763 mono_aot_register_jit_icall ("mono_x86_start_gsharedvt_call", mono_x86_start_gsharedvt_call);
768 * Cleanup architecture specific code.
771 mono_arch_cleanup (void)
774 mono_vfree (ss_trigger_page, mono_pagesize ());
776 mono_vfree (bp_trigger_page, mono_pagesize ());
777 DeleteCriticalSection (&mini_arch_mutex);
781 * This function returns the optimizations supported on this cpu.
784 mono_arch_cpu_optimizations (guint32 *exclude_mask)
786 #if !defined(__native_client__)
791 if (mono_hwcap_x86_has_cmov) {
792 opts |= MONO_OPT_CMOV;
794 if (mono_hwcap_x86_has_fcmov)
795 opts |= MONO_OPT_FCMOV;
797 *exclude_mask |= MONO_OPT_FCMOV;
799 *exclude_mask |= MONO_OPT_CMOV;
802 if (mono_hwcap_x86_has_sse2)
803 opts |= MONO_OPT_SSE2;
805 *exclude_mask |= MONO_OPT_SSE2;
807 #ifdef MONO_ARCH_SIMD_INTRINSICS
808 /*SIMD intrinsics require at least SSE2.*/
809 if (!mono_hwcap_x86_has_sse2)
810 *exclude_mask |= MONO_OPT_SIMD;
815 return MONO_OPT_CMOV | MONO_OPT_FCMOV | MONO_OPT_SSE2;
820 * This function test for all SSE functions supported.
822 * Returns a bitmask corresponding to all supported versions.
826 mono_arch_cpu_enumerate_simd_versions (void)
828 guint32 sse_opts = 0;
830 if (mono_hwcap_x86_has_sse1)
831 sse_opts |= SIMD_VERSION_SSE1;
833 if (mono_hwcap_x86_has_sse2)
834 sse_opts |= SIMD_VERSION_SSE2;
836 if (mono_hwcap_x86_has_sse3)
837 sse_opts |= SIMD_VERSION_SSE3;
839 if (mono_hwcap_x86_has_ssse3)
840 sse_opts |= SIMD_VERSION_SSSE3;
842 if (mono_hwcap_x86_has_sse41)
843 sse_opts |= SIMD_VERSION_SSE41;
845 if (mono_hwcap_x86_has_sse42)
846 sse_opts |= SIMD_VERSION_SSE42;
848 if (mono_hwcap_x86_has_sse4a)
849 sse_opts |= SIMD_VERSION_SSE4a;
855 * Determine whenever the trap whose info is in SIGINFO is caused by
859 mono_arch_is_int_overflow (void *sigctx, void *info)
864 mono_arch_sigctx_to_monoctx (sigctx, &ctx);
866 ip = (guint8*)ctx.eip;
868 if ((ip [0] == 0xf7) && (x86_modrm_mod (ip [1]) == 0x3) && (x86_modrm_reg (ip [1]) == 0x7)) {
872 switch (x86_modrm_rm (ip [1])) {
892 g_assert_not_reached ();
904 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
909 for (i = 0; i < cfg->num_varinfo; i++) {
910 MonoInst *ins = cfg->varinfo [i];
911 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
914 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
917 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
918 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
921 /* we dont allocate I1 to registers because there is no simply way to sign extend
922 * 8bit quantities in caller saved registers on x86 */
923 if (mono_is_regsize_var (ins->inst_vtype) && (ins->inst_vtype->type != MONO_TYPE_I1)) {
924 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
925 g_assert (i == vmv->idx);
926 vars = g_list_prepend (vars, vmv);
930 vars = mono_varlist_sort (cfg, vars, 0);
936 mono_arch_get_global_int_regs (MonoCompile *cfg)
940 /* we can use 3 registers for global allocation */
941 regs = g_list_prepend (regs, (gpointer)X86_EBX);
942 regs = g_list_prepend (regs, (gpointer)X86_ESI);
943 regs = g_list_prepend (regs, (gpointer)X86_EDI);
949 * mono_arch_regalloc_cost:
951 * Return the cost, in number of memory references, of the action of
952 * allocating the variable VMV into a register during global register
956 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
958 MonoInst *ins = cfg->varinfo [vmv->idx];
960 if (cfg->method->save_lmf)
961 /* The register is already saved */
962 return (ins->opcode == OP_ARG) ? 1 : 0;
964 /* push+pop+possible load if it is an argument */
965 return (ins->opcode == OP_ARG) ? 3 : 2;
969 set_needs_stack_frame (MonoCompile *cfg, gboolean flag)
971 static int inited = FALSE;
972 static int count = 0;
974 if (cfg->arch.need_stack_frame_inited) {
975 g_assert (cfg->arch.need_stack_frame == flag);
979 cfg->arch.need_stack_frame = flag;
980 cfg->arch.need_stack_frame_inited = TRUE;
986 mono_counters_register ("Could eliminate stack frame", MONO_COUNTER_INT|MONO_COUNTER_JIT, &count);
991 //g_print ("will eliminate %s.%s.%s\n", cfg->method->klass->name_space, cfg->method->klass->name, cfg->method->name);
995 needs_stack_frame (MonoCompile *cfg)
997 MonoMethodSignature *sig;
998 MonoMethodHeader *header;
999 gboolean result = FALSE;
1001 #if defined(__APPLE__)
1002 /*OSX requires stack frame code to have the correct alignment. */
1006 if (cfg->arch.need_stack_frame_inited)
1007 return cfg->arch.need_stack_frame;
1009 header = cfg->header;
1010 sig = mono_method_signature (cfg->method);
1012 if (cfg->disable_omit_fp)
1014 else if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1016 else if (cfg->method->save_lmf)
1018 else if (cfg->stack_offset)
1020 else if (cfg->param_area)
1022 else if (cfg->flags & (MONO_CFG_HAS_CALLS | MONO_CFG_HAS_ALLOCA | MONO_CFG_HAS_TAIL))
1024 else if (header->num_clauses)
1026 else if (sig->param_count + sig->hasthis)
1028 else if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1030 else if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) ||
1031 (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
1034 set_needs_stack_frame (cfg, result);
1036 return cfg->arch.need_stack_frame;
1040 * Set var information according to the calling convention. X86 version.
1041 * The locals var stuff should most likely be split in another method.
1044 mono_arch_allocate_vars (MonoCompile *cfg)
1046 MonoMethodSignature *sig;
1047 MonoMethodHeader *header;
1049 guint32 locals_stack_size, locals_stack_align;
1054 header = cfg->header;
1055 sig = mono_method_signature (cfg->method);
1057 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1059 cfg->frame_reg = X86_EBP;
1062 /* Reserve space to save LMF and caller saved registers */
1064 if (cfg->method->save_lmf) {
1065 /* The LMF var is allocated normally */
1067 if (cfg->used_int_regs & (1 << X86_EBX)) {
1071 if (cfg->used_int_regs & (1 << X86_EDI)) {
1075 if (cfg->used_int_regs & (1 << X86_ESI)) {
1080 switch (cinfo->ret.storage) {
1081 case ArgValuetypeInReg:
1082 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
1084 cfg->ret->opcode = OP_REGOFFSET;
1085 cfg->ret->inst_basereg = X86_EBP;
1086 cfg->ret->inst_offset = - offset;
1092 /* Allocate locals */
1093 offsets = mono_allocate_stack_slots (cfg, TRUE, &locals_stack_size, &locals_stack_align);
1094 if (locals_stack_size > MONO_ARCH_MAX_FRAME_SIZE) {
1095 char *mname = mono_method_full_name (cfg->method, TRUE);
1096 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
1097 cfg->exception_message = g_strdup_printf ("Method %s stack is too big.", mname);
1101 if (locals_stack_align) {
1102 int prev_offset = offset;
1104 offset += (locals_stack_align - 1);
1105 offset &= ~(locals_stack_align - 1);
1107 while (prev_offset < offset) {
1109 mini_gc_set_slot_type_from_fp (cfg, - prev_offset, SLOT_NOREF);
1112 cfg->locals_min_stack_offset = - (offset + locals_stack_size);
1113 cfg->locals_max_stack_offset = - offset;
1115 * EBP is at alignment 8 % MONO_ARCH_FRAME_ALIGNMENT, so if we
1116 * have locals larger than 8 bytes we need to make sure that
1117 * they have the appropriate offset.
1119 if (MONO_ARCH_FRAME_ALIGNMENT > 8 && locals_stack_align > 8)
1120 offset += MONO_ARCH_FRAME_ALIGNMENT - sizeof (gpointer) * 2;
1121 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1122 if (offsets [i] != -1) {
1123 MonoInst *inst = cfg->varinfo [i];
1124 inst->opcode = OP_REGOFFSET;
1125 inst->inst_basereg = X86_EBP;
1126 inst->inst_offset = - (offset + offsets [i]);
1127 //printf ("allocated local %d to ", i); mono_print_tree_nl (inst);
1130 offset += locals_stack_size;
1134 * Allocate arguments+return value
1137 switch (cinfo->ret.storage) {
1139 if (cfg->vret_addr) {
1141 * In the new IR, the cfg->vret_addr variable represents the
1142 * vtype return value.
1144 cfg->vret_addr->opcode = OP_REGOFFSET;
1145 cfg->vret_addr->inst_basereg = cfg->frame_reg;
1146 cfg->vret_addr->inst_offset = cinfo->ret.offset + ARGS_OFFSET;
1147 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1148 printf ("vret_addr =");
1149 mono_print_ins (cfg->vret_addr);
1152 cfg->ret->opcode = OP_REGOFFSET;
1153 cfg->ret->inst_basereg = X86_EBP;
1154 cfg->ret->inst_offset = cinfo->ret.offset + ARGS_OFFSET;
1157 case ArgValuetypeInReg:
1160 cfg->ret->opcode = OP_REGVAR;
1161 cfg->ret->inst_c0 = cinfo->ret.reg;
1162 cfg->ret->dreg = cinfo->ret.reg;
1165 case ArgOnFloatFpStack:
1166 case ArgOnDoubleFpStack:
1169 g_assert_not_reached ();
1172 if (sig->call_convention == MONO_CALL_VARARG) {
1173 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
1174 cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
1177 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1178 ArgInfo *ainfo = &cinfo->args [i];
1179 inst = cfg->args [i];
1180 if (inst->opcode != OP_REGVAR) {
1181 inst->opcode = OP_REGOFFSET;
1182 inst->inst_basereg = X86_EBP;
1184 inst->inst_offset = ainfo->offset + ARGS_OFFSET;
1187 cfg->stack_offset = offset;
1191 mono_arch_create_vars (MonoCompile *cfg)
1194 MonoMethodSignature *sig;
1197 sig = mono_method_signature (cfg->method);
1199 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1200 sig_ret = mini_replace_type (sig->ret);
1202 if (cinfo->ret.storage == ArgValuetypeInReg)
1203 cfg->ret_var_is_local = TRUE;
1204 if ((cinfo->ret.storage != ArgValuetypeInReg) && (MONO_TYPE_ISSTRUCT (sig_ret) || mini_is_gsharedvt_variable_type (cfg, sig_ret))) {
1205 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
1208 if (cfg->method->save_lmf) {
1209 cfg->create_lmf_var = TRUE;
1211 if (!optimize_for_xen) {
1213 cfg->lmf_ir_mono_lmf = TRUE;
1218 cfg->arch_eh_jit_info = 1;
1222 * It is expensive to adjust esp for each individual fp argument pushed on the stack
1223 * so we try to do it just once when we have multiple fp arguments in a row.
1224 * We don't use this mechanism generally because for int arguments the generated code
1225 * is slightly bigger and new generation cpus optimize away the dependency chains
1226 * created by push instructions on the esp value.
1227 * fp_arg_setup is the first argument in the execution sequence where the esp register
1230 static G_GNUC_UNUSED int
1231 collect_fp_stack_space (MonoMethodSignature *sig, int start_arg, int *fp_arg_setup)
1236 for (; start_arg < sig->param_count; ++start_arg) {
1237 t = mini_replace_type (sig->params [start_arg]);
1238 if (!t->byref && t->type == MONO_TYPE_R8) {
1239 fp_space += sizeof (double);
1240 *fp_arg_setup = start_arg;
1249 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
1251 MonoMethodSignature *tmp_sig;
1255 * mono_ArgIterator_Setup assumes the signature cookie is
1256 * passed first and all the arguments which were before it are
1257 * passed on the stack after the signature. So compensate by
1258 * passing a different signature.
1260 tmp_sig = mono_metadata_signature_dup (call->signature);
1261 tmp_sig->param_count -= call->signature->sentinelpos;
1262 tmp_sig->sentinelpos = 0;
1263 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
1265 if (cfg->compile_aot) {
1266 sig_reg = mono_alloc_ireg (cfg);
1267 MONO_EMIT_NEW_SIGNATURECONST (cfg, sig_reg, tmp_sig);
1268 MONO_EMIT_NEW_UNALU (cfg, OP_X86_PUSH, -1, sig_reg);
1270 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_X86_PUSH_IMM, -1, -1, tmp_sig);
1276 mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
1281 LLVMCallInfo *linfo;
1282 MonoType *t, *sig_ret;
1284 n = sig->param_count + sig->hasthis;
1286 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1289 linfo = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
1292 * LLVM always uses the native ABI while we use our own ABI, the
1293 * only difference is the handling of vtypes:
1294 * - we only pass/receive them in registers in some cases, and only
1295 * in 1 or 2 integer registers.
1297 if (cinfo->ret.storage == ArgValuetypeInReg) {
1299 cfg->exception_message = g_strdup ("pinvoke + vtypes");
1300 cfg->disable_llvm = TRUE;
1304 cfg->exception_message = g_strdup ("vtype ret in call");
1305 cfg->disable_llvm = TRUE;
1307 linfo->ret.storage = LLVMArgVtypeInReg;
1308 for (j = 0; j < 2; ++j)
1309 linfo->ret.pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, cinfo->ret.pair_storage [j]);
1313 if (mini_type_is_vtype (cfg, sig_ret) && cinfo->ret.storage == ArgInIReg) {
1314 /* Vtype returned using a hidden argument */
1315 linfo->ret.storage = LLVMArgVtypeRetAddr;
1316 linfo->vret_arg_index = cinfo->vret_arg_index;
1319 if (mini_type_is_vtype (cfg, sig_ret) && cinfo->ret.storage != ArgInIReg) {
1321 cfg->exception_message = g_strdup ("vtype ret in call");
1322 cfg->disable_llvm = TRUE;
1325 for (i = 0; i < n; ++i) {
1326 ainfo = cinfo->args + i;
1328 if (i >= sig->hasthis)
1329 t = sig->params [i - sig->hasthis];
1331 t = &mono_defaults.int_class->byval_arg;
1333 linfo->args [i].storage = LLVMArgNone;
1335 switch (ainfo->storage) {
1337 linfo->args [i].storage = LLVMArgInIReg;
1339 case ArgInDoubleSSEReg:
1340 case ArgInFloatSSEReg:
1341 linfo->args [i].storage = LLVMArgInFPReg;
1344 if (mini_type_is_vtype (cfg, t)) {
1345 if (mono_class_value_size (mono_class_from_mono_type (t), NULL) == 0)
1346 /* LLVM seems to allocate argument space for empty structures too */
1347 linfo->args [i].storage = LLVMArgNone;
1349 linfo->args [i].storage = LLVMArgVtypeByVal;
1351 linfo->args [i].storage = LLVMArgInIReg;
1353 if (t->type == MONO_TYPE_R4)
1354 linfo->args [i].storage = LLVMArgInFPReg;
1355 else if (t->type == MONO_TYPE_R8)
1356 linfo->args [i].storage = LLVMArgInFPReg;
1360 case ArgValuetypeInReg:
1362 cfg->exception_message = g_strdup ("pinvoke + vtypes");
1363 cfg->disable_llvm = TRUE;
1367 cfg->exception_message = g_strdup ("vtype arg");
1368 cfg->disable_llvm = TRUE;
1370 linfo->args [i].storage = LLVMArgVtypeInReg;
1371 for (j = 0; j < 2; ++j)
1372 linfo->args [i].pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, ainfo->pair_storage [j]);
1376 linfo->args [i].storage = LLVMArgGSharedVt;
1379 cfg->exception_message = g_strdup ("ainfo->storage");
1380 cfg->disable_llvm = TRUE;
1390 emit_gc_param_slot_def (MonoCompile *cfg, int sp_offset, MonoType *t)
1392 if (cfg->compute_gc_maps) {
1395 /* On x86, the offsets are from the sp value before the start of the call sequence */
1397 t = &mono_defaults.int_class->byval_arg;
1398 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, sp_offset, t);
1403 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
1407 MonoMethodSignature *sig;
1410 int sentinelpos = 0, sp_offset = 0;
1412 sig = call->signature;
1413 n = sig->param_count + sig->hasthis;
1414 sig_ret = mini_replace_type (sig->ret);
1416 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1418 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1419 sentinelpos = sig->sentinelpos + (sig->hasthis ? 1 : 0);
1421 if (cinfo->need_stack_align) {
1422 MONO_INST_NEW (cfg, arg, OP_SUB_IMM);
1423 arg->dreg = X86_ESP;
1424 arg->sreg1 = X86_ESP;
1425 arg->inst_imm = cinfo->stack_align_amount;
1426 MONO_ADD_INS (cfg->cbb, arg);
1427 for (i = 0; i < cinfo->stack_align_amount; i += sizeof (mgreg_t)) {
1430 emit_gc_param_slot_def (cfg, sp_offset, NULL);
1434 if (sig_ret && MONO_TYPE_ISSTRUCT (sig_ret)) {
1435 if (cinfo->ret.storage == ArgValuetypeInReg) {
1437 * Tell the JIT to use a more efficient calling convention: call using
1438 * OP_CALL, compute the result location after the call, and save the
1441 call->vret_in_reg = TRUE;
1443 NULLIFY_INS (call->vret_var);
1447 // FIXME: Emit EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF everywhere
1449 /* Handle the case where there are no implicit arguments */
1450 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sentinelpos)) {
1451 emit_sig_cookie (cfg, call, cinfo);
1453 emit_gc_param_slot_def (cfg, sp_offset, NULL);
1456 /* Arguments are pushed in the reverse order */
1457 for (i = n - 1; i >= 0; i --) {
1458 ArgInfo *ainfo = cinfo->args + i;
1459 MonoType *orig_type, *t;
1462 if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 1 && i == 0) {
1463 /* Push the vret arg before the first argument */
1465 MONO_INST_NEW (cfg, vtarg, OP_X86_PUSH);
1466 vtarg->type = STACK_MP;
1467 vtarg->sreg1 = call->vret_var->dreg;
1468 MONO_ADD_INS (cfg->cbb, vtarg);
1470 emit_gc_param_slot_def (cfg, sp_offset, NULL);
1473 if (i >= sig->hasthis)
1474 t = sig->params [i - sig->hasthis];
1476 t = &mono_defaults.int_class->byval_arg;
1478 t = mini_type_get_underlying_type (cfg->generic_sharing_context, t);
1480 MONO_INST_NEW (cfg, arg, OP_X86_PUSH);
1482 in = call->args [i];
1483 arg->cil_code = in->cil_code;
1484 arg->sreg1 = in->dreg;
1485 arg->type = in->type;
1487 g_assert (in->dreg != -1);
1489 if (ainfo->storage == ArgGSharedVt) {
1490 arg->opcode = OP_OUTARG_VT;
1491 arg->sreg1 = in->dreg;
1492 arg->klass = in->klass;
1493 arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
1494 memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
1496 MONO_ADD_INS (cfg->cbb, arg);
1497 } else if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(t))) {
1501 g_assert (in->klass);
1503 if (t->type == MONO_TYPE_TYPEDBYREF) {
1504 size = sizeof (MonoTypedRef);
1505 align = sizeof (gpointer);
1508 size = mini_type_stack_size_full (cfg->generic_sharing_context, &in->klass->byval_arg, &align, sig->pinvoke);
1512 arg->opcode = OP_OUTARG_VT;
1513 arg->sreg1 = in->dreg;
1514 arg->klass = in->klass;
1515 arg->backend.size = size;
1516 arg->inst_p0 = call;
1517 arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
1518 memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
1520 MONO_ADD_INS (cfg->cbb, arg);
1521 if (ainfo->storage != ArgValuetypeInReg) {
1523 emit_gc_param_slot_def (cfg, sp_offset, orig_type);
1529 switch (ainfo->storage) {
1531 arg->opcode = OP_X86_PUSH;
1533 if (t->type == MONO_TYPE_R4) {
1534 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, 4);
1535 arg->opcode = OP_STORER4_MEMBASE_REG;
1536 arg->inst_destbasereg = X86_ESP;
1537 arg->inst_offset = 0;
1539 } else if (t->type == MONO_TYPE_R8) {
1540 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, 8);
1541 arg->opcode = OP_STORER8_MEMBASE_REG;
1542 arg->inst_destbasereg = X86_ESP;
1543 arg->inst_offset = 0;
1545 } else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_U8) {
1547 MONO_EMIT_NEW_UNALU (cfg, OP_X86_PUSH, -1, in->dreg + 2);
1553 arg->opcode = OP_MOVE;
1554 arg->dreg = ainfo->reg;
1558 g_assert_not_reached ();
1561 MONO_ADD_INS (cfg->cbb, arg);
1563 sp_offset += argsize;
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, sp_offset, &mono_defaults.int_class->this_arg);
1574 emit_gc_param_slot_def (cfg, sp_offset, &mono_defaults.object_class->byval_arg);
1576 emit_gc_param_slot_def (cfg, sp_offset, orig_type);
1580 for (j = 0; j < argsize; j += 4)
1581 emit_gc_param_slot_def (cfg, sp_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);
1590 emit_gc_param_slot_def (cfg, sp_offset, NULL);
1594 if (sig_ret && (MONO_TYPE_ISSTRUCT (sig_ret) || cinfo->vtype_retaddr)) {
1597 if (cinfo->ret.storage == ArgValuetypeInReg) {
1600 else if (cinfo->ret.storage == ArgInIReg) {
1602 /* The return address is passed in a register */
1603 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
1604 vtarg->sreg1 = call->inst.dreg;
1605 vtarg->dreg = mono_alloc_ireg (cfg);
1606 MONO_ADD_INS (cfg->cbb, vtarg);
1608 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
1609 } else if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 0) {
1611 MONO_INST_NEW (cfg, vtarg, OP_X86_PUSH);
1612 vtarg->type = STACK_MP;
1613 vtarg->sreg1 = call->vret_var->dreg;
1614 MONO_ADD_INS (cfg->cbb, vtarg);
1616 emit_gc_param_slot_def (cfg, sp_offset, NULL);
1619 /* if the function returns a struct on stack, the called method already does a ret $0x4 */
1620 if (cinfo->ret.storage != ArgValuetypeInReg)
1621 cinfo->stack_usage -= 4;
1624 call->stack_usage = cinfo->stack_usage;
1625 call->stack_align_amount = cinfo->stack_align_amount;
1626 cfg->arch.param_area_size = MAX (cfg->arch.param_area_size, sp_offset);
1630 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
1632 MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
1633 ArgInfo *ainfo = ins->inst_p1;
1635 int size = ins->backend.size;
1637 if (ainfo->storage == ArgValuetypeInReg) {
1638 int dreg = mono_alloc_ireg (cfg);
1641 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, dreg, src->dreg, 0);
1644 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, dreg, src->dreg, 0);
1647 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, src->dreg, 0);
1651 g_assert_not_reached ();
1653 mono_call_inst_add_outarg_reg (cfg, call, dreg, ainfo->reg, FALSE);
1656 if (cfg->gsharedvt && mini_is_gsharedvt_klass (cfg, ins->klass)) {
1658 MONO_INST_NEW (cfg, arg, OP_X86_PUSH);
1659 arg->sreg1 = src->dreg;
1660 MONO_ADD_INS (cfg->cbb, arg);
1661 } else if (size <= 4) {
1662 MONO_INST_NEW (cfg, arg, OP_X86_PUSH_MEMBASE);
1663 arg->sreg1 = src->dreg;
1665 MONO_ADD_INS (cfg->cbb, arg);
1666 } else if (size <= 20) {
1667 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, ALIGN_TO (size, 4));
1668 mini_emit_memcpy (cfg, X86_ESP, 0, src->dreg, 0, size, 4);
1670 MONO_INST_NEW (cfg, arg, OP_X86_PUSH_OBJ);
1671 arg->inst_basereg = src->dreg;
1672 arg->inst_offset = 0;
1673 arg->inst_imm = size;
1675 MONO_ADD_INS (cfg->cbb, arg);
1681 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
1683 MonoType *ret = mini_type_get_underlying_type (cfg->generic_sharing_context, mono_method_signature (method)->ret);
1686 if (ret->type == MONO_TYPE_R4) {
1687 if (COMPILE_LLVM (cfg))
1688 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
1691 } else if (ret->type == MONO_TYPE_R8) {
1692 if (COMPILE_LLVM (cfg))
1693 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
1696 } else if (ret->type == MONO_TYPE_I8 || ret->type == MONO_TYPE_U8) {
1697 if (COMPILE_LLVM (cfg))
1698 MONO_EMIT_NEW_UNALU (cfg, OP_LMOVE, cfg->ret->dreg, val->dreg);
1700 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, X86_EAX, val->dreg + 1);
1701 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, X86_EDX, val->dreg + 2);
1707 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
1711 * Allow tracing to work with this interface (with an optional argument)
1714 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1718 g_assert (MONO_ARCH_FRAME_ALIGNMENT >= 8);
1719 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 8);
1721 /* if some args are passed in registers, we need to save them here */
1722 x86_push_reg (code, X86_EBP);
1724 if (cfg->compile_aot) {
1725 x86_push_imm (code, cfg->method);
1726 x86_mov_reg_imm (code, X86_EAX, func);
1727 x86_call_reg (code, X86_EAX);
1729 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
1730 x86_push_imm (code, cfg->method);
1731 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1732 x86_call_code (code, 0);
1734 x86_alu_reg_imm (code, X86_ADD, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT);
1748 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
1751 int arg_size = 0, stack_usage = 0, save_mode = SAVE_NONE;
1752 MonoMethod *method = cfg->method;
1753 MonoType *ret_type = mini_type_get_underlying_type (cfg->generic_sharing_context, mono_method_signature (method)->ret);
1755 switch (ret_type->type) {
1756 case MONO_TYPE_VOID:
1757 /* special case string .ctor icall */
1758 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class) {
1759 save_mode = SAVE_EAX;
1760 stack_usage = enable_arguments ? 8 : 4;
1762 save_mode = SAVE_NONE;
1766 save_mode = SAVE_EAX_EDX;
1767 stack_usage = enable_arguments ? 16 : 8;
1771 save_mode = SAVE_FP;
1772 stack_usage = enable_arguments ? 16 : 8;
1774 case MONO_TYPE_GENERICINST:
1775 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
1776 save_mode = SAVE_EAX;
1777 stack_usage = enable_arguments ? 8 : 4;
1781 case MONO_TYPE_VALUETYPE:
1782 // FIXME: Handle SMALL_STRUCT_IN_REG here for proper alignment on darwin-x86
1783 save_mode = SAVE_STRUCT;
1784 stack_usage = enable_arguments ? 4 : 0;
1787 save_mode = SAVE_EAX;
1788 stack_usage = enable_arguments ? 8 : 4;
1792 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - stack_usage - 4);
1794 switch (save_mode) {
1796 x86_push_reg (code, X86_EDX);
1797 x86_push_reg (code, X86_EAX);
1798 if (enable_arguments) {
1799 x86_push_reg (code, X86_EDX);
1800 x86_push_reg (code, X86_EAX);
1805 x86_push_reg (code, X86_EAX);
1806 if (enable_arguments) {
1807 x86_push_reg (code, X86_EAX);
1812 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1813 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1814 if (enable_arguments) {
1815 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1816 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1821 if (enable_arguments) {
1822 x86_push_membase (code, X86_EBP, 8);
1831 if (cfg->compile_aot) {
1832 x86_push_imm (code, method);
1833 x86_mov_reg_imm (code, X86_EAX, func);
1834 x86_call_reg (code, X86_EAX);
1836 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
1837 x86_push_imm (code, method);
1838 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1839 x86_call_code (code, 0);
1842 x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
1844 switch (save_mode) {
1846 x86_pop_reg (code, X86_EAX);
1847 x86_pop_reg (code, X86_EDX);
1850 x86_pop_reg (code, X86_EAX);
1853 x86_fld_membase (code, X86_ESP, 0, TRUE);
1854 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1861 x86_alu_reg_imm (code, X86_ADD, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - stack_usage);
1866 #define EMIT_COND_BRANCH(ins,cond,sign) \
1867 if (ins->inst_true_bb->native_offset) { \
1868 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
1870 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1871 if ((cfg->opt & MONO_OPT_BRANCH) && \
1872 x86_is_imm8 (ins->inst_true_bb->max_offset - cpos)) \
1873 x86_branch8 (code, cond, 0, sign); \
1875 x86_branch32 (code, cond, 0, sign); \
1879 * Emit an exception if condition is fail and
1880 * if possible do a directly branch to target
1882 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
1884 MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
1885 if (tins == NULL) { \
1886 mono_add_patch_info (cfg, code - cfg->native_code, \
1887 MONO_PATCH_INFO_EXC, exc_name); \
1888 x86_branch32 (code, cond, 0, signed); \
1890 EMIT_COND_BRANCH (tins, cond, signed); \
1894 #define EMIT_FPCOMPARE(code) do { \
1895 x86_fcompp (code); \
1896 x86_fnstsw (code); \
1901 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data)
1903 gboolean needs_paddings = TRUE;
1905 MonoJumpInfo *jinfo = NULL;
1907 if (cfg->abs_patches) {
1908 jinfo = g_hash_table_lookup (cfg->abs_patches, data);
1909 if (jinfo && jinfo->type == MONO_PATCH_INFO_JIT_ICALL_ADDR)
1910 needs_paddings = FALSE;
1913 if (cfg->compile_aot)
1914 needs_paddings = FALSE;
1915 /*The address must be 4 bytes aligned to avoid spanning multiple cache lines.
1916 This is required for code patching to be safe on SMP machines.
1918 pad_size = (guint32)(code + 1 - cfg->native_code) & 0x3;
1919 #ifndef __native_client_codegen__
1920 if (needs_paddings && pad_size)
1921 x86_padding (code, 4 - pad_size);
1924 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
1925 x86_call_code (code, 0);
1930 #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)))
1933 * mono_peephole_pass_1:
1935 * Perform peephole opts which should/can be performed before local regalloc
1938 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
1942 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
1943 MonoInst *last_ins = ins->prev;
1945 switch (ins->opcode) {
1948 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS)) {
1950 * X86_LEA is like ADD, but doesn't have the
1951 * sreg1==dreg restriction.
1953 ins->opcode = OP_X86_LEA_MEMBASE;
1954 ins->inst_basereg = ins->sreg1;
1955 } else if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1956 ins->opcode = OP_X86_INC_REG;
1960 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS)) {
1961 ins->opcode = OP_X86_LEA_MEMBASE;
1962 ins->inst_basereg = ins->sreg1;
1963 ins->inst_imm = -ins->inst_imm;
1964 } else if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1965 ins->opcode = OP_X86_DEC_REG;
1967 case OP_COMPARE_IMM:
1968 case OP_ICOMPARE_IMM:
1969 /* OP_COMPARE_IMM (reg, 0)
1971 * OP_X86_TEST_NULL (reg)
1974 ins->opcode = OP_X86_TEST_NULL;
1976 case OP_X86_COMPARE_MEMBASE_IMM:
1978 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1979 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
1981 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1982 * OP_COMPARE_IMM reg, imm
1984 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
1986 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
1987 ins->inst_basereg == last_ins->inst_destbasereg &&
1988 ins->inst_offset == last_ins->inst_offset) {
1989 ins->opcode = OP_COMPARE_IMM;
1990 ins->sreg1 = last_ins->sreg1;
1992 /* check if we can remove cmp reg,0 with test null */
1994 ins->opcode = OP_X86_TEST_NULL;
1998 case OP_X86_PUSH_MEMBASE:
1999 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG ||
2000 last_ins->opcode == OP_STORE_MEMBASE_REG) &&
2001 ins->inst_basereg == last_ins->inst_destbasereg &&
2002 ins->inst_offset == last_ins->inst_offset) {
2003 ins->opcode = OP_X86_PUSH;
2004 ins->sreg1 = last_ins->sreg1;
2009 mono_peephole_ins (bb, ins);
2014 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
2018 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
2019 switch (ins->opcode) {
2021 /* reg = 0 -> XOR (reg, reg) */
2022 /* XOR sets cflags on x86, so we cant do it always */
2023 if (ins->inst_c0 == 0 && (!ins->next || (ins->next && INST_IGNORES_CFLAGS (ins->next->opcode)))) {
2026 ins->opcode = OP_IXOR;
2027 ins->sreg1 = ins->dreg;
2028 ins->sreg2 = ins->dreg;
2031 * Convert succeeding STORE_MEMBASE_IMM 0 ins to STORE_MEMBASE_REG
2032 * since it takes 3 bytes instead of 7.
2034 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
2035 if ((ins2->opcode == OP_STORE_MEMBASE_IMM) && (ins2->inst_imm == 0)) {
2036 ins2->opcode = OP_STORE_MEMBASE_REG;
2037 ins2->sreg1 = ins->dreg;
2039 else if ((ins2->opcode == OP_STOREI4_MEMBASE_IMM) && (ins2->inst_imm == 0)) {
2040 ins2->opcode = OP_STOREI4_MEMBASE_REG;
2041 ins2->sreg1 = ins->dreg;
2043 else if ((ins2->opcode == OP_STOREI1_MEMBASE_IMM) || (ins2->opcode == OP_STOREI2_MEMBASE_IMM)) {
2044 /* Continue iteration */
2053 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
2054 ins->opcode = OP_X86_INC_REG;
2058 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
2059 ins->opcode = OP_X86_DEC_REG;
2063 mono_peephole_ins (bb, ins);
2068 * mono_arch_lowering_pass:
2070 * Converts complex opcodes into simpler ones so that each IR instruction
2071 * corresponds to one machine instruction.
2074 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
2076 MonoInst *ins, *next;
2079 * FIXME: Need to add more instructions, but the current machine
2080 * description can't model some parts of the composite instructions like
2083 MONO_BB_FOR_EACH_INS_SAFE (bb, next, ins) {
2084 switch (ins->opcode) {
2087 case OP_IDIV_UN_IMM:
2088 case OP_IREM_UN_IMM:
2090 * Keep the cases where we could generated optimized code, otherwise convert
2091 * to the non-imm variant.
2093 if ((ins->opcode == OP_IREM_IMM) && mono_is_power_of_two (ins->inst_imm) >= 0)
2095 mono_decompose_op_imm (cfg, bb, ins);
2102 bb->max_vreg = cfg->next_vreg;
2106 branch_cc_table [] = {
2107 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
2108 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
2109 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
2112 /* Maps CMP_... constants to X86_CC_... constants */
2115 X86_CC_EQ, X86_CC_NE, X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT,
2116 X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT
2120 cc_signed_table [] = {
2121 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
2122 FALSE, FALSE, FALSE, FALSE
2125 static unsigned char*
2126 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
2128 #define XMM_TEMP_REG 0
2129 /*This SSE2 optimization must not be done which OPT_SIMD in place as it clobbers xmm0.*/
2130 /*The xmm pass decomposes OP_FCONV_ ops anyway anyway.*/
2131 if (cfg->opt & MONO_OPT_SSE2 && size < 8 && !(cfg->opt & MONO_OPT_SIMD)) {
2132 /* optimize by assigning a local var for this use so we avoid
2133 * the stack manipulations */
2134 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2135 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
2136 x86_movsd_reg_membase (code, XMM_TEMP_REG, X86_ESP, 0);
2137 x86_cvttsd2si (code, dreg, XMM_TEMP_REG);
2138 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
2140 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
2142 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
2145 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
2146 x86_fnstcw_membase(code, X86_ESP, 0);
2147 x86_mov_reg_membase (code, dreg, X86_ESP, 0, 2);
2148 x86_alu_reg_imm (code, X86_OR, dreg, 0xc00);
2149 x86_mov_membase_reg (code, X86_ESP, 2, dreg, 2);
2150 x86_fldcw_membase (code, X86_ESP, 2);
2152 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2153 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
2154 x86_pop_reg (code, dreg);
2155 /* FIXME: need the high register
2156 * x86_pop_reg (code, dreg_high);
2159 x86_push_reg (code, X86_EAX); // SP = SP - 4
2160 x86_fist_pop_membase (code, X86_ESP, 0, FALSE);
2161 x86_pop_reg (code, dreg);
2163 x86_fldcw_membase (code, X86_ESP, 0);
2164 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2167 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
2169 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
2173 static unsigned char*
2174 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
2176 int sreg = tree->sreg1;
2177 int need_touch = FALSE;
2179 #if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
2188 * If requested stack size is larger than one page,
2189 * perform stack-touch operation
2192 * Generate stack probe code.
2193 * Under Windows, it is necessary to allocate one page at a time,
2194 * "touching" stack after each successful sub-allocation. This is
2195 * because of the way stack growth is implemented - there is a
2196 * guard page before the lowest stack page that is currently commited.
2197 * Stack normally grows sequentially so OS traps access to the
2198 * guard page and commits more pages when needed.
2200 x86_test_reg_imm (code, sreg, ~0xFFF);
2201 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2203 br[2] = code; /* loop */
2204 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
2205 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
2208 * By the end of the loop, sreg2 is smaller than 0x1000, so the init routine
2209 * that follows only initializes the last part of the area.
2211 /* Same as the init code below with size==0x1000 */
2212 if (tree->flags & MONO_INST_INIT) {
2213 x86_push_reg (code, X86_EAX);
2214 x86_push_reg (code, X86_ECX);
2215 x86_push_reg (code, X86_EDI);
2216 x86_mov_reg_imm (code, X86_ECX, (0x1000 >> 2));
2217 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2218 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
2220 x86_prefix (code, X86_REP_PREFIX);
2222 x86_pop_reg (code, X86_EDI);
2223 x86_pop_reg (code, X86_ECX);
2224 x86_pop_reg (code, X86_EAX);
2227 x86_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
2228 x86_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
2229 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
2230 x86_patch (br[3], br[2]);
2231 x86_test_reg_reg (code, sreg, sreg);
2232 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2233 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
2235 br[1] = code; x86_jump8 (code, 0);
2237 x86_patch (br[0], code);
2238 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
2239 x86_patch (br[1], code);
2240 x86_patch (br[4], code);
2243 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
2245 if (tree->flags & MONO_INST_INIT) {
2247 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
2248 x86_push_reg (code, X86_EAX);
2251 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
2252 x86_push_reg (code, X86_ECX);
2255 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
2256 x86_push_reg (code, X86_EDI);
2260 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
2261 if (sreg != X86_ECX)
2262 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
2263 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2265 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
2267 x86_prefix (code, X86_REP_PREFIX);
2270 if (tree->dreg != X86_EDI && sreg != X86_EDI)
2271 x86_pop_reg (code, X86_EDI);
2272 if (tree->dreg != X86_ECX && sreg != X86_ECX)
2273 x86_pop_reg (code, X86_ECX);
2274 if (tree->dreg != X86_EAX && sreg != X86_EAX)
2275 x86_pop_reg (code, X86_EAX);
2282 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
2284 /* Move return value to the target register */
2285 switch (ins->opcode) {
2288 case OP_CALL_MEMBASE:
2289 if (ins->dreg != X86_EAX)
2290 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
2300 static int tls_gs_offset;
2304 mono_x86_have_tls_get (void)
2307 static gboolean have_tls_get = FALSE;
2308 static gboolean inited = FALSE;
2312 return have_tls_get;
2314 ins = (guint32*)pthread_getspecific;
2316 * We're looking for these two instructions:
2318 * mov 0x4(%esp),%eax
2319 * mov %gs:[offset](,%eax,4),%eax
2321 have_tls_get = ins [0] == 0x0424448b && ins [1] == 0x85048b65;
2322 tls_gs_offset = ins [2];
2326 return have_tls_get;
2327 #elif defined(TARGET_ANDROID)
2335 mono_x86_emit_tls_set (guint8* code, int sreg, int tls_offset)
2337 #if defined(__APPLE__)
2338 x86_prefix (code, X86_GS_PREFIX);
2339 x86_mov_mem_reg (code, tls_gs_offset + (tls_offset * 4), sreg, 4);
2340 #elif defined(TARGET_WIN32)
2341 g_assert_not_reached ();
2343 x86_prefix (code, X86_GS_PREFIX);
2344 x86_mov_mem_reg (code, tls_offset, sreg, 4);
2350 * mono_x86_emit_tls_get:
2351 * @code: buffer to store code to
2352 * @dreg: hard register where to place the result
2353 * @tls_offset: offset info
2355 * mono_x86_emit_tls_get emits in @code the native code that puts in
2356 * the dreg register the item in the thread local storage identified
2359 * Returns: a pointer to the end of the stored code
2362 mono_x86_emit_tls_get (guint8* code, int dreg, int tls_offset)
2364 #if defined(__APPLE__)
2365 x86_prefix (code, X86_GS_PREFIX);
2366 x86_mov_reg_mem (code, dreg, tls_gs_offset + (tls_offset * 4), 4);
2367 #elif defined(TARGET_WIN32)
2369 * See the Under the Hood article in the May 1996 issue of Microsoft Systems
2370 * Journal and/or a disassembly of the TlsGet () function.
2372 g_assert (tls_offset < 64);
2373 x86_prefix (code, X86_FS_PREFIX);
2374 x86_mov_reg_mem (code, dreg, 0x18, 4);
2375 /* Dunno what this does but TlsGetValue () contains it */
2376 x86_alu_membase_imm (code, X86_AND, dreg, 0x34, 0);
2377 x86_mov_reg_membase (code, dreg, dreg, 3600 + (tls_offset * 4), 4);
2379 if (optimize_for_xen) {
2380 x86_prefix (code, X86_GS_PREFIX);
2381 x86_mov_reg_mem (code, dreg, 0, 4);
2382 x86_mov_reg_membase (code, dreg, dreg, tls_offset, 4);
2384 x86_prefix (code, X86_GS_PREFIX);
2385 x86_mov_reg_mem (code, dreg, tls_offset, 4);
2392 emit_tls_get_reg (guint8* code, int dreg, int offset_reg)
2394 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
2395 #if defined(__APPLE__) || defined(__linux__)
2396 if (dreg != offset_reg)
2397 x86_mov_reg_reg (code, dreg, offset_reg, sizeof (mgreg_t));
2398 x86_prefix (code, X86_GS_PREFIX);
2399 x86_mov_reg_membase (code, dreg, dreg, 0, sizeof (mgreg_t));
2401 g_assert_not_reached ();
2407 emit_tls_set_reg (guint8* code, int sreg, int offset_reg)
2409 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
2411 g_assert_not_reached ();
2412 #elif defined(__APPLE__) || defined(__linux__)
2413 x86_prefix (code, X86_GS_PREFIX);
2414 x86_mov_membase_reg (code, offset_reg, 0, sreg, sizeof (mgreg_t));
2416 g_assert_not_reached ();
2422 * mono_arch_translate_tls_offset:
2424 * Translate the TLS offset OFFSET computed by MONO_THREAD_VAR_OFFSET () into a format usable by OP_TLS_GET_REG/OP_TLS_SET_REG.
2427 mono_arch_translate_tls_offset (int offset)
2430 return tls_gs_offset + (offset * 4);
2439 * Emit code to initialize an LMF structure at LMF_OFFSET.
2442 emit_setup_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset, int cfa_offset)
2444 /* save all caller saved regs */
2445 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebx), X86_EBX, sizeof (mgreg_t));
2446 mono_emit_unwind_op_offset (cfg, code, X86_EBX, - cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebx));
2447 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, edi), X86_EDI, sizeof (mgreg_t));
2448 mono_emit_unwind_op_offset (cfg, code, X86_EDI, - cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, edi));
2449 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esi), X86_ESI, sizeof (mgreg_t));
2450 mono_emit_unwind_op_offset (cfg, code, X86_ESI, - cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, esi));
2451 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebp), X86_EBP, sizeof (mgreg_t));
2453 /* save the current IP */
2454 if (cfg->compile_aot) {
2455 /* This pushes the current ip */
2456 x86_call_imm (code, 0);
2457 x86_pop_reg (code, X86_EAX);
2459 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
2460 x86_mov_reg_imm (code, X86_EAX, 0);
2462 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, eip), X86_EAX, sizeof (mgreg_t));
2464 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, eip), SLOT_NOREF);
2465 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebp), SLOT_NOREF);
2466 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, esi), SLOT_NOREF);
2467 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, edi), SLOT_NOREF);
2468 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebx), SLOT_NOREF);
2469 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, esp), SLOT_NOREF);
2470 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), SLOT_NOREF);
2471 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), SLOT_NOREF);
2472 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), SLOT_NOREF);
2480 * Emit code to push an LMF structure on the LMF stack.
2483 emit_push_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset)
2485 /* get the address of lmf for the current thread */
2487 * This is performance critical so we try to use some tricks to make
2490 gboolean have_fastpath = FALSE;
2493 if (jit_tls_offset != -1) {
2494 code = mono_x86_emit_tls_get (code, X86_EAX, jit_tls_offset);
2495 x86_alu_reg_imm (code, X86_ADD, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
2496 have_fastpath = TRUE;
2499 if (!cfg->compile_aot && lmf_addr_tls_offset != -1) {
2500 code = mono_x86_emit_tls_get (code, X86_EAX, lmf_addr_tls_offset);
2501 have_fastpath = TRUE;
2504 if (!have_fastpath) {
2505 if (cfg->compile_aot)
2506 code = mono_arch_emit_load_got_addr (cfg->native_code, code, cfg, NULL);
2507 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_get_lmf_addr");
2511 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), X86_EAX, sizeof (mgreg_t));
2512 /* save previous_lmf */
2513 x86_mov_reg_membase (code, X86_ECX, X86_EAX, 0, sizeof (mgreg_t));
2514 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), X86_ECX, sizeof (mgreg_t));
2516 x86_lea_membase (code, X86_ECX, cfg->frame_reg, lmf_offset);
2517 x86_mov_membase_reg (code, X86_EAX, 0, X86_ECX, sizeof (mgreg_t));
2525 * Emit code to pop an LMF structure from the LMF stack.
2526 * Preserves the return registers.
2529 emit_pop_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset)
2531 MonoMethodSignature *sig = mono_method_signature (cfg->method);
2534 /* Find a spare register */
2535 switch (mini_type_get_underlying_type (cfg->generic_sharing_context, sig->ret)->type) {
2538 prev_lmf_reg = X86_EDI;
2539 cfg->used_int_regs |= (1 << X86_EDI);
2542 prev_lmf_reg = X86_EDX;
2546 /* reg = previous_lmf */
2547 x86_mov_reg_membase (code, prev_lmf_reg, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 4);
2550 x86_mov_reg_membase (code, X86_ECX, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), 4);
2552 /* *(lmf) = previous_lmf */
2553 x86_mov_membase_reg (code, X86_ECX, 0, prev_lmf_reg, 4);
2558 #define REAL_PRINT_REG(text,reg) \
2559 mono_assert (reg >= 0); \
2560 x86_push_reg (code, X86_EAX); \
2561 x86_push_reg (code, X86_EDX); \
2562 x86_push_reg (code, X86_ECX); \
2563 x86_push_reg (code, reg); \
2564 x86_push_imm (code, reg); \
2565 x86_push_imm (code, text " %d %p\n"); \
2566 x86_mov_reg_imm (code, X86_EAX, printf); \
2567 x86_call_reg (code, X86_EAX); \
2568 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 3*4); \
2569 x86_pop_reg (code, X86_ECX); \
2570 x86_pop_reg (code, X86_EDX); \
2571 x86_pop_reg (code, X86_EAX);
2573 /* REAL_PRINT_REG does not appear to be used, and was not adapted to work with Native Client. */
2574 #ifdef __native__client_codegen__
2575 #define REAL_PRINT_REG(text, reg) g_assert_not_reached()
2578 /* benchmark and set based on cpu */
2579 #define LOOP_ALIGNMENT 8
2580 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
2584 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2589 guint8 *code = cfg->native_code + cfg->code_len;
2592 if (cfg->opt & MONO_OPT_LOOP) {
2593 int pad, align = LOOP_ALIGNMENT;
2594 /* set alignment depending on cpu */
2595 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
2597 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
2598 x86_padding (code, pad);
2599 cfg->code_len += pad;
2600 bb->native_offset = cfg->code_len;
2603 #ifdef __native_client_codegen__
2605 /* For Native Client, all indirect call/jump targets must be */
2606 /* 32-byte aligned. Exception handler blocks are jumped to */
2607 /* indirectly as well. */
2608 gboolean bb_needs_alignment = (bb->flags & BB_INDIRECT_JUMP_TARGET) ||
2609 (bb->flags & BB_EXCEPTION_HANDLER);
2611 /* if ((cfg->code_len & kNaClAlignmentMask) != 0) { */
2612 if ( bb_needs_alignment && ((cfg->code_len & kNaClAlignmentMask) != 0)) {
2613 int pad = kNaClAlignment - (cfg->code_len & kNaClAlignmentMask);
2614 if (pad != kNaClAlignment) code = mono_arch_nacl_pad(code, pad);
2615 cfg->code_len += pad;
2616 bb->native_offset = cfg->code_len;
2619 #endif /* __native_client_codegen__ */
2620 if (cfg->verbose_level > 2)
2621 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2623 cpos = bb->max_offset;
2625 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2626 MonoProfileCoverageInfo *cov = cfg->coverage_info;
2627 g_assert (!cfg->compile_aot);
2630 cov->data [bb->dfn].cil_code = bb->cil_code;
2631 /* this is not thread save, but good enough */
2632 x86_inc_mem (code, &cov->data [bb->dfn].count);
2635 offset = code - cfg->native_code;
2637 mono_debug_open_block (cfg, bb, offset);
2639 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)
2640 x86_breakpoint (code);
2642 MONO_BB_FOR_EACH_INS (bb, ins) {
2643 offset = code - cfg->native_code;
2645 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
2647 #define EXTRA_CODE_SPACE (NACL_SIZE (16, 16 + kNaClAlignment))
2649 if (G_UNLIKELY (offset > (cfg->code_size - max_len - EXTRA_CODE_SPACE))) {
2650 cfg->code_size *= 2;
2651 cfg->native_code = mono_realloc_native_code(cfg);
2652 code = cfg->native_code + offset;
2653 cfg->stat_code_reallocs++;
2656 if (cfg->debug_info)
2657 mono_debug_record_line_number (cfg, ins, offset);
2659 switch (ins->opcode) {
2661 x86_mul_reg (code, ins->sreg2, TRUE);
2664 x86_mul_reg (code, ins->sreg2, FALSE);
2666 case OP_X86_SETEQ_MEMBASE:
2667 case OP_X86_SETNE_MEMBASE:
2668 x86_set_membase (code, ins->opcode == OP_X86_SETEQ_MEMBASE ? X86_CC_EQ : X86_CC_NE,
2669 ins->inst_basereg, ins->inst_offset, TRUE);
2671 case OP_STOREI1_MEMBASE_IMM:
2672 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
2674 case OP_STOREI2_MEMBASE_IMM:
2675 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
2677 case OP_STORE_MEMBASE_IMM:
2678 case OP_STOREI4_MEMBASE_IMM:
2679 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
2681 case OP_STOREI1_MEMBASE_REG:
2682 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
2684 case OP_STOREI2_MEMBASE_REG:
2685 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
2687 case OP_STORE_MEMBASE_REG:
2688 case OP_STOREI4_MEMBASE_REG:
2689 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
2691 case OP_STORE_MEM_IMM:
2692 x86_mov_mem_imm (code, ins->inst_p0, ins->inst_c0, 4);
2695 x86_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
2699 /* These are created by the cprop pass so they use inst_imm as the source */
2700 x86_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
2703 x86_widen_mem (code, ins->dreg, ins->inst_imm, FALSE, FALSE);
2706 x86_widen_mem (code, ins->dreg, ins->inst_imm, FALSE, TRUE);
2708 case OP_LOAD_MEMBASE:
2709 case OP_LOADI4_MEMBASE:
2710 case OP_LOADU4_MEMBASE:
2711 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
2713 case OP_LOADU1_MEMBASE:
2714 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
2716 case OP_LOADI1_MEMBASE:
2717 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
2719 case OP_LOADU2_MEMBASE:
2720 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
2722 case OP_LOADI2_MEMBASE:
2723 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
2725 case OP_ICONV_TO_I1:
2727 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2729 case OP_ICONV_TO_I2:
2731 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2733 case OP_ICONV_TO_U1:
2734 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
2736 case OP_ICONV_TO_U2:
2737 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
2741 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
2743 case OP_COMPARE_IMM:
2744 case OP_ICOMPARE_IMM:
2745 x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
2747 case OP_X86_COMPARE_MEMBASE_REG:
2748 x86_alu_membase_reg (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2750 case OP_X86_COMPARE_MEMBASE_IMM:
2751 x86_alu_membase_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2753 case OP_X86_COMPARE_MEMBASE8_IMM:
2754 x86_alu_membase8_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2756 case OP_X86_COMPARE_REG_MEMBASE:
2757 x86_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
2759 case OP_X86_COMPARE_MEM_IMM:
2760 x86_alu_mem_imm (code, X86_CMP, ins->inst_offset, ins->inst_imm);
2762 case OP_X86_TEST_NULL:
2763 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2765 case OP_X86_ADD_MEMBASE_IMM:
2766 x86_alu_membase_imm (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2768 case OP_X86_ADD_REG_MEMBASE:
2769 x86_alu_reg_membase (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset);
2771 case OP_X86_SUB_MEMBASE_IMM:
2772 x86_alu_membase_imm (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2774 case OP_X86_SUB_REG_MEMBASE:
2775 x86_alu_reg_membase (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset);
2777 case OP_X86_AND_MEMBASE_IMM:
2778 x86_alu_membase_imm (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2780 case OP_X86_OR_MEMBASE_IMM:
2781 x86_alu_membase_imm (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2783 case OP_X86_XOR_MEMBASE_IMM:
2784 x86_alu_membase_imm (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2786 case OP_X86_ADD_MEMBASE_REG:
2787 x86_alu_membase_reg (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2789 case OP_X86_SUB_MEMBASE_REG:
2790 x86_alu_membase_reg (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2792 case OP_X86_AND_MEMBASE_REG:
2793 x86_alu_membase_reg (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2795 case OP_X86_OR_MEMBASE_REG:
2796 x86_alu_membase_reg (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2798 case OP_X86_XOR_MEMBASE_REG:
2799 x86_alu_membase_reg (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2801 case OP_X86_INC_MEMBASE:
2802 x86_inc_membase (code, ins->inst_basereg, ins->inst_offset);
2804 case OP_X86_INC_REG:
2805 x86_inc_reg (code, ins->dreg);
2807 case OP_X86_DEC_MEMBASE:
2808 x86_dec_membase (code, ins->inst_basereg, ins->inst_offset);
2810 case OP_X86_DEC_REG:
2811 x86_dec_reg (code, ins->dreg);
2813 case OP_X86_MUL_REG_MEMBASE:
2814 x86_imul_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
2816 case OP_X86_AND_REG_MEMBASE:
2817 x86_alu_reg_membase (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset);
2819 case OP_X86_OR_REG_MEMBASE:
2820 x86_alu_reg_membase (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset);
2822 case OP_X86_XOR_REG_MEMBASE:
2823 x86_alu_reg_membase (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset);
2826 x86_breakpoint (code);
2828 case OP_RELAXED_NOP:
2829 x86_prefix (code, X86_REP_PREFIX);
2837 case OP_DUMMY_STORE:
2838 case OP_NOT_REACHED:
2841 case OP_SEQ_POINT: {
2844 if (cfg->compile_aot)
2848 * Read from the single stepping trigger page. This will cause a
2849 * SIGSEGV when single stepping is enabled.
2850 * We do this _before_ the breakpoint, so single stepping after
2851 * a breakpoint is hit will step to the next IL offset.
2853 if (ins->flags & MONO_INST_SINGLE_STEP_LOC)
2854 x86_alu_reg_mem (code, X86_CMP, X86_EAX, (guint32)ss_trigger_page);
2856 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
2859 * A placeholder for a possible breakpoint inserted by
2860 * mono_arch_set_breakpoint ().
2862 for (i = 0; i < 6; ++i)
2865 * Add an additional nop so skipping the bp doesn't cause the ip to point
2866 * to another IL offset.
2874 x86_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
2878 x86_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
2883 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
2887 x86_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
2892 x86_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
2896 x86_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
2901 x86_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
2905 x86_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
2908 x86_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
2912 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
2916 #if defined( __native_client_codegen__ )
2917 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
2918 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
2921 * The code is the same for div/rem, the allocator will allocate dreg
2922 * to RAX/RDX as appropriate.
2924 if (ins->sreg2 == X86_EDX) {
2925 /* cdq clobbers this */
2926 x86_push_reg (code, ins->sreg2);
2928 x86_div_membase (code, X86_ESP, 0, TRUE);
2929 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2932 x86_div_reg (code, ins->sreg2, TRUE);
2937 #if defined( __native_client_codegen__ )
2938 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
2939 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
2941 if (ins->sreg2 == X86_EDX) {
2942 x86_push_reg (code, ins->sreg2);
2943 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2944 x86_div_membase (code, X86_ESP, 0, FALSE);
2945 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2947 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2948 x86_div_reg (code, ins->sreg2, FALSE);
2952 #if defined( __native_client_codegen__ )
2953 if (ins->inst_imm == 0) {
2954 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "DivideByZeroException");
2955 x86_jump32 (code, 0);
2959 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2961 x86_div_reg (code, ins->sreg2, TRUE);
2964 int power = mono_is_power_of_two (ins->inst_imm);
2966 g_assert (ins->sreg1 == X86_EAX);
2967 g_assert (ins->dreg == X86_EAX);
2968 g_assert (power >= 0);
2971 /* Based on http://compilers.iecc.com/comparch/article/93-04-079 */
2973 x86_alu_reg_imm (code, X86_AND, X86_EAX, 1);
2975 * If the divident is >= 0, this does not nothing. If it is positive, it
2976 * it transforms %eax=0 into %eax=0, and %eax=1 into %eax=-1.
2978 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EDX);
2979 x86_alu_reg_reg (code, X86_SUB, X86_EAX, X86_EDX);
2980 } else if (power == 0) {
2981 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2983 /* Based on gcc code */
2985 /* Add compensation for negative dividents */
2987 x86_shift_reg_imm (code, X86_SHR, X86_EDX, 32 - power);
2988 x86_alu_reg_reg (code, X86_ADD, X86_EAX, X86_EDX);
2989 /* Compute remainder */
2990 x86_alu_reg_imm (code, X86_AND, X86_EAX, (1 << power) - 1);
2991 /* Remove compensation */
2992 x86_alu_reg_reg (code, X86_SUB, X86_EAX, X86_EDX);
2997 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
3001 x86_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
3004 x86_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
3008 x86_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
3011 g_assert (ins->sreg2 == X86_ECX);
3012 x86_shift_reg (code, X86_SHL, ins->dreg);
3015 g_assert (ins->sreg2 == X86_ECX);
3016 x86_shift_reg (code, X86_SAR, ins->dreg);
3020 x86_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
3023 case OP_ISHR_UN_IMM:
3024 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
3027 g_assert (ins->sreg2 == X86_ECX);
3028 x86_shift_reg (code, X86_SHR, ins->dreg);
3032 x86_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
3035 guint8 *jump_to_end;
3037 /* handle shifts below 32 bits */
3038 x86_shld_reg (code, ins->backend.reg3, ins->sreg1);
3039 x86_shift_reg (code, X86_SHL, ins->sreg1);
3041 x86_test_reg_imm (code, X86_ECX, 32);
3042 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
3044 /* handle shift over 32 bit */
3045 x86_mov_reg_reg (code, ins->backend.reg3, ins->sreg1, 4);
3046 x86_clear_reg (code, ins->sreg1);
3048 x86_patch (jump_to_end, code);
3052 guint8 *jump_to_end;
3054 /* handle shifts below 32 bits */
3055 x86_shrd_reg (code, ins->sreg1, ins->backend.reg3);
3056 x86_shift_reg (code, X86_SAR, ins->backend.reg3);
3058 x86_test_reg_imm (code, X86_ECX, 32);
3059 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
3061 /* handle shifts over 31 bits */
3062 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
3063 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, 31);
3065 x86_patch (jump_to_end, code);
3069 guint8 *jump_to_end;
3071 /* handle shifts below 32 bits */
3072 x86_shrd_reg (code, ins->sreg1, ins->backend.reg3);
3073 x86_shift_reg (code, X86_SHR, ins->backend.reg3);
3075 x86_test_reg_imm (code, X86_ECX, 32);
3076 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
3078 /* handle shifts over 31 bits */
3079 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
3080 x86_clear_reg (code, ins->backend.reg3);
3082 x86_patch (jump_to_end, code);
3086 if (ins->inst_imm >= 32) {
3087 x86_mov_reg_reg (code, ins->backend.reg3, ins->sreg1, 4);
3088 x86_clear_reg (code, ins->sreg1);
3089 x86_shift_reg_imm (code, X86_SHL, ins->backend.reg3, ins->inst_imm - 32);
3091 x86_shld_reg_imm (code, ins->backend.reg3, ins->sreg1, ins->inst_imm);
3092 x86_shift_reg_imm (code, X86_SHL, ins->sreg1, ins->inst_imm);
3096 if (ins->inst_imm >= 32) {
3097 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
3098 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, 0x1f);
3099 x86_shift_reg_imm (code, X86_SAR, ins->sreg1, ins->inst_imm - 32);
3101 x86_shrd_reg_imm (code, ins->sreg1, ins->backend.reg3, ins->inst_imm);
3102 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, ins->inst_imm);
3105 case OP_LSHR_UN_IMM:
3106 if (ins->inst_imm >= 32) {
3107 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
3108 x86_clear_reg (code, ins->backend.reg3);
3109 x86_shift_reg_imm (code, X86_SHR, ins->sreg1, ins->inst_imm - 32);
3111 x86_shrd_reg_imm (code, ins->sreg1, ins->backend.reg3, ins->inst_imm);
3112 x86_shift_reg_imm (code, X86_SHR, ins->backend.reg3, ins->inst_imm);
3116 x86_not_reg (code, ins->sreg1);
3119 x86_neg_reg (code, ins->sreg1);
3123 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
3127 switch (ins->inst_imm) {
3131 if (ins->dreg != ins->sreg1)
3132 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
3133 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
3136 /* LEA r1, [r2 + r2*2] */
3137 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
3140 /* LEA r1, [r2 + r2*4] */
3141 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3144 /* LEA r1, [r2 + r2*2] */
3146 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
3147 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
3150 /* LEA r1, [r2 + r2*8] */
3151 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
3154 /* LEA r1, [r2 + r2*4] */
3156 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3157 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
3160 /* LEA r1, [r2 + r2*2] */
3162 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
3163 x86_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
3166 /* LEA r1, [r2 + r2*4] */
3167 /* LEA r1, [r1 + r1*4] */
3168 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3169 x86_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
3172 /* LEA r1, [r2 + r2*4] */
3174 /* LEA r1, [r1 + r1*4] */
3175 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3176 x86_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
3177 x86_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
3180 x86_imul_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_imm);
3185 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
3186 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
3188 case OP_IMUL_OVF_UN: {
3189 /* the mul operation and the exception check should most likely be split */
3190 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
3191 /*g_assert (ins->sreg2 == X86_EAX);
3192 g_assert (ins->dreg == X86_EAX);*/
3193 if (ins->sreg2 == X86_EAX) {
3194 non_eax_reg = ins->sreg1;
3195 } else if (ins->sreg1 == X86_EAX) {
3196 non_eax_reg = ins->sreg2;
3198 /* no need to save since we're going to store to it anyway */
3199 if (ins->dreg != X86_EAX) {
3201 x86_push_reg (code, X86_EAX);
3203 x86_mov_reg_reg (code, X86_EAX, ins->sreg1, 4);
3204 non_eax_reg = ins->sreg2;
3206 if (ins->dreg == X86_EDX) {
3209 x86_push_reg (code, X86_EAX);
3211 } else if (ins->dreg != X86_EAX) {
3213 x86_push_reg (code, X86_EDX);
3215 x86_mul_reg (code, non_eax_reg, FALSE);
3216 /* save before the check since pop and mov don't change the flags */
3217 if (ins->dreg != X86_EAX)
3218 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
3220 x86_pop_reg (code, X86_EDX);
3222 x86_pop_reg (code, X86_EAX);
3223 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
3227 x86_mov_reg_imm (code, ins->dreg, ins->inst_c0);
3230 g_assert_not_reached ();
3231 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3232 x86_mov_reg_imm (code, ins->dreg, 0);
3235 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3236 x86_mov_reg_imm (code, ins->dreg, 0);
3238 case OP_LOAD_GOTADDR:
3239 g_assert (ins->dreg == MONO_ARCH_GOT_REG);
3240 code = mono_arch_emit_load_got_addr (cfg->native_code, code, cfg, NULL);
3243 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
3244 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, 0xf0f0f0f0, 4);
3246 case OP_X86_PUSH_GOT_ENTRY:
3247 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
3248 x86_push_membase (code, ins->inst_basereg, 0xf0f0f0f0);
3251 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
3254 MonoCallInst *call = (MonoCallInst*)ins;
3257 ins->flags |= MONO_INST_GC_CALLSITE;
3258 ins->backend.pc_offset = code - cfg->native_code;
3260 /* FIXME: no tracing support... */
3261 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3262 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
3263 /* reset offset to make max_len work */
3264 offset = code - cfg->native_code;
3266 g_assert (!cfg->method->save_lmf);
3268 /* restore callee saved registers */
3269 for (i = 0; i < X86_NREG; ++i)
3270 if (X86_IS_CALLEE_SAVED_REG (i) && cfg->used_int_regs & (1 << i))
3272 if (cfg->used_int_regs & (1 << X86_ESI)) {
3273 x86_mov_reg_membase (code, X86_ESI, X86_EBP, pos, 4);
3276 if (cfg->used_int_regs & (1 << X86_EDI)) {
3277 x86_mov_reg_membase (code, X86_EDI, X86_EBP, pos, 4);
3280 if (cfg->used_int_regs & (1 << X86_EBX)) {
3281 x86_mov_reg_membase (code, X86_EBX, X86_EBP, pos, 4);
3285 /* Copy arguments on the stack to our argument area */
3286 for (i = 0; i < call->stack_usage - call->stack_align_amount; i += 4) {
3287 x86_mov_reg_membase (code, X86_EAX, X86_ESP, i, 4);
3288 x86_mov_membase_reg (code, X86_EBP, 8 + i, X86_EAX, 4);
3291 /* restore ESP/EBP */
3293 offset = code - cfg->native_code;
3294 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD_JUMP, call->method);
3295 x86_jump32 (code, 0);
3297 ins->flags |= MONO_INST_GC_CALLSITE;
3298 cfg->disable_aot = TRUE;
3302 /* ensure ins->sreg1 is not NULL
3303 * note that cmp DWORD PTR [eax], eax is one byte shorter than
3304 * cmp DWORD PTR [eax], 0
3306 x86_alu_membase_reg (code, X86_CMP, ins->sreg1, 0, ins->sreg1);
3309 int hreg = ins->sreg1 == X86_EAX? X86_ECX: X86_EAX;
3310 x86_push_reg (code, hreg);
3311 x86_lea_membase (code, hreg, X86_EBP, cfg->sig_cookie);
3312 x86_mov_membase_reg (code, ins->sreg1, 0, hreg, 4);
3313 x86_pop_reg (code, hreg);
3322 call = (MonoCallInst*)ins;
3323 if (ins->flags & MONO_INST_HAS_METHOD)
3324 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method);
3326 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr);
3327 ins->flags |= MONO_INST_GC_CALLSITE;
3328 ins->backend.pc_offset = code - cfg->native_code;
3329 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
3330 /* a pop is one byte, while an add reg, imm is 3. So if there are 4 or 8
3331 * bytes to pop, we want to use pops. GCC does this (note it won't happen
3332 * for P4 or i686 because gcc will avoid using pop push at all. But we aren't
3333 * smart enough to do that optimization yet
3335 * It turns out that on my P4, doing two pops for 8 bytes on the stack makes
3336 * mcs botstrap slow down. However, doing 1 pop for 4 bytes creates a small,
3337 * (most likely from locality benefits). People with other processors should
3338 * check on theirs to see what happens.
3340 if (call->stack_usage == 4) {
3341 /* we want to use registers that won't get used soon, so use
3342 * ecx, as eax will get allocated first. edx is used by long calls,
3343 * so we can't use that.
3346 x86_pop_reg (code, X86_ECX);
3348 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
3351 code = emit_move_return_value (cfg, ins, code);
3357 case OP_VOIDCALL_REG:
3359 call = (MonoCallInst*)ins;
3360 x86_call_reg (code, ins->sreg1);
3361 ins->flags |= MONO_INST_GC_CALLSITE;
3362 ins->backend.pc_offset = code - cfg->native_code;
3363 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
3364 if (call->stack_usage == 4)
3365 x86_pop_reg (code, X86_ECX);
3367 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
3369 code = emit_move_return_value (cfg, ins, code);
3371 case OP_FCALL_MEMBASE:
3372 case OP_LCALL_MEMBASE:
3373 case OP_VCALL_MEMBASE:
3374 case OP_VCALL2_MEMBASE:
3375 case OP_VOIDCALL_MEMBASE:
3376 case OP_CALL_MEMBASE:
3377 call = (MonoCallInst*)ins;
3379 x86_call_membase (code, ins->sreg1, ins->inst_offset);
3380 ins->flags |= MONO_INST_GC_CALLSITE;
3381 ins->backend.pc_offset = code - cfg->native_code;
3382 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
3383 if (call->stack_usage == 4)
3384 x86_pop_reg (code, X86_ECX);
3386 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
3388 code = emit_move_return_value (cfg, ins, code);
3391 x86_push_reg (code, ins->sreg1);
3393 case OP_X86_PUSH_IMM:
3394 x86_push_imm (code, ins->inst_imm);
3396 case OP_X86_PUSH_MEMBASE:
3397 x86_push_membase (code, ins->inst_basereg, ins->inst_offset);
3399 case OP_X86_PUSH_OBJ:
3400 x86_alu_reg_imm (code, X86_SUB, X86_ESP, ins->inst_imm);
3401 x86_push_reg (code, X86_EDI);
3402 x86_push_reg (code, X86_ESI);
3403 x86_push_reg (code, X86_ECX);
3404 if (ins->inst_offset)
3405 x86_lea_membase (code, X86_ESI, ins->inst_basereg, ins->inst_offset);
3407 x86_mov_reg_reg (code, X86_ESI, ins->inst_basereg, 4);
3408 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
3409 x86_mov_reg_imm (code, X86_ECX, (ins->inst_imm >> 2));
3411 x86_prefix (code, X86_REP_PREFIX);
3413 x86_pop_reg (code, X86_ECX);
3414 x86_pop_reg (code, X86_ESI);
3415 x86_pop_reg (code, X86_EDI);
3418 x86_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
3420 case OP_X86_LEA_MEMBASE:
3421 x86_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
3424 x86_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
3427 /* keep alignment */
3428 x86_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_LOCALLOC_ALIGNMENT - 1);
3429 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_LOCALLOC_ALIGNMENT - 1));
3430 code = mono_emit_stack_alloc (code, ins);
3431 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
3433 case OP_LOCALLOC_IMM: {
3434 guint32 size = ins->inst_imm;
3435 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
3437 if (ins->flags & MONO_INST_INIT) {
3438 /* FIXME: Optimize this */
3439 x86_mov_reg_imm (code, ins->dreg, size);
3440 ins->sreg1 = ins->dreg;
3442 code = mono_emit_stack_alloc (code, ins);
3443 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
3445 x86_alu_reg_imm (code, X86_SUB, X86_ESP, size);
3446 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
3451 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3452 x86_push_reg (code, ins->sreg1);
3453 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
3454 (gpointer)"mono_arch_throw_exception");
3455 ins->flags |= MONO_INST_GC_CALLSITE;
3456 ins->backend.pc_offset = code - cfg->native_code;
3460 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3461 x86_push_reg (code, ins->sreg1);
3462 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
3463 (gpointer)"mono_arch_rethrow_exception");
3464 ins->flags |= MONO_INST_GC_CALLSITE;
3465 ins->backend.pc_offset = code - cfg->native_code;
3468 case OP_CALL_HANDLER:
3469 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3470 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3471 x86_call_imm (code, 0);
3472 mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
3473 x86_alu_reg_imm (code, X86_ADD, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3475 case OP_START_HANDLER: {
3476 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3477 x86_mov_membase_reg (code, spvar->inst_basereg, spvar->inst_offset, X86_ESP, 4);
3480 case OP_ENDFINALLY: {
3481 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3482 x86_mov_reg_membase (code, X86_ESP, spvar->inst_basereg, spvar->inst_offset, 4);
3486 case OP_ENDFILTER: {
3487 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3488 x86_mov_reg_membase (code, X86_ESP, spvar->inst_basereg, spvar->inst_offset, 4);
3489 /* The local allocator will put the result into EAX */
3495 ins->inst_c0 = code - cfg->native_code;
3498 if (ins->inst_target_bb->native_offset) {
3499 x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
3501 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3502 if ((cfg->opt & MONO_OPT_BRANCH) &&
3503 x86_is_imm8 (ins->inst_target_bb->max_offset - cpos))
3504 x86_jump8 (code, 0);
3506 x86_jump32 (code, 0);
3510 x86_jump_reg (code, ins->sreg1);
3529 x86_set_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
3530 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3532 case OP_COND_EXC_EQ:
3533 case OP_COND_EXC_NE_UN:
3534 case OP_COND_EXC_LT:
3535 case OP_COND_EXC_LT_UN:
3536 case OP_COND_EXC_GT:
3537 case OP_COND_EXC_GT_UN:
3538 case OP_COND_EXC_GE:
3539 case OP_COND_EXC_GE_UN:
3540 case OP_COND_EXC_LE:
3541 case OP_COND_EXC_LE_UN:
3542 case OP_COND_EXC_IEQ:
3543 case OP_COND_EXC_INE_UN:
3544 case OP_COND_EXC_ILT:
3545 case OP_COND_EXC_ILT_UN:
3546 case OP_COND_EXC_IGT:
3547 case OP_COND_EXC_IGT_UN:
3548 case OP_COND_EXC_IGE:
3549 case OP_COND_EXC_IGE_UN:
3550 case OP_COND_EXC_ILE:
3551 case OP_COND_EXC_ILE_UN:
3552 EMIT_COND_SYSTEM_EXCEPTION (cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)], ins->inst_p1);
3554 case OP_COND_EXC_OV:
3555 case OP_COND_EXC_NO:
3557 case OP_COND_EXC_NC:
3558 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ], (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
3560 case OP_COND_EXC_IOV:
3561 case OP_COND_EXC_INO:
3562 case OP_COND_EXC_IC:
3563 case OP_COND_EXC_INC:
3564 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_IEQ], (ins->opcode < OP_COND_EXC_INE_UN), ins->inst_p1);
3576 EMIT_COND_BRANCH (ins, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
3584 case OP_CMOV_INE_UN:
3585 case OP_CMOV_IGE_UN:
3586 case OP_CMOV_IGT_UN:
3587 case OP_CMOV_ILE_UN:
3588 case OP_CMOV_ILT_UN:
3589 g_assert (ins->dreg == ins->sreg1);
3590 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);
3593 /* floating point opcodes */
3595 double d = *(double *)ins->inst_p0;
3597 if ((d == 0.0) && (mono_signbit (d) == 0)) {
3599 } else if (d == 1.0) {
3602 if (cfg->compile_aot) {
3603 guint32 *val = (guint32*)&d;
3604 x86_push_imm (code, val [1]);
3605 x86_push_imm (code, val [0]);
3606 x86_fld_membase (code, X86_ESP, 0, TRUE);
3607 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3610 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R8, ins->inst_p0);
3611 x86_fld (code, NULL, TRUE);
3617 float f = *(float *)ins->inst_p0;
3619 if ((f == 0.0) && (mono_signbit (f) == 0)) {
3621 } else if (f == 1.0) {
3624 if (cfg->compile_aot) {
3625 guint32 val = *(guint32*)&f;
3626 x86_push_imm (code, val);
3627 x86_fld_membase (code, X86_ESP, 0, FALSE);
3628 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3631 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R4, ins->inst_p0);
3632 x86_fld (code, NULL, FALSE);
3637 case OP_STORER8_MEMBASE_REG:
3638 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, TRUE, TRUE);
3640 case OP_LOADR8_MEMBASE:
3641 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
3643 case OP_STORER4_MEMBASE_REG:
3644 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, FALSE, TRUE);
3646 case OP_LOADR4_MEMBASE:
3647 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
3649 case OP_ICONV_TO_R4:
3650 x86_push_reg (code, ins->sreg1);
3651 x86_fild_membase (code, X86_ESP, 0, FALSE);
3652 /* Change precision */
3653 x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
3654 x86_fld_membase (code, X86_ESP, 0, FALSE);
3655 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3657 case OP_ICONV_TO_R8:
3658 x86_push_reg (code, ins->sreg1);
3659 x86_fild_membase (code, X86_ESP, 0, FALSE);
3660 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3662 case OP_ICONV_TO_R_UN:
3663 x86_push_imm (code, 0);
3664 x86_push_reg (code, ins->sreg1);
3665 x86_fild_membase (code, X86_ESP, 0, TRUE);
3666 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3668 case OP_X86_FP_LOAD_I8:
3669 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
3671 case OP_X86_FP_LOAD_I4:
3672 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
3674 case OP_FCONV_TO_R4:
3675 /* Change precision */
3676 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
3677 x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
3678 x86_fld_membase (code, X86_ESP, 0, FALSE);
3679 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3681 case OP_FCONV_TO_I1:
3682 code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
3684 case OP_FCONV_TO_U1:
3685 code = emit_float_to_int (cfg, code, ins->dreg, 1, FALSE);
3687 case OP_FCONV_TO_I2:
3688 code = emit_float_to_int (cfg, code, ins->dreg, 2, TRUE);
3690 case OP_FCONV_TO_U2:
3691 code = emit_float_to_int (cfg, code, ins->dreg, 2, FALSE);
3693 case OP_FCONV_TO_I4:
3695 code = emit_float_to_int (cfg, code, ins->dreg, 4, TRUE);
3697 case OP_FCONV_TO_I8:
3698 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
3699 x86_fnstcw_membase(code, X86_ESP, 0);
3700 x86_mov_reg_membase (code, ins->dreg, X86_ESP, 0, 2);
3701 x86_alu_reg_imm (code, X86_OR, ins->dreg, 0xc00);
3702 x86_mov_membase_reg (code, X86_ESP, 2, ins->dreg, 2);
3703 x86_fldcw_membase (code, X86_ESP, 2);
3704 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
3705 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
3706 x86_pop_reg (code, ins->dreg);
3707 x86_pop_reg (code, ins->backend.reg3);
3708 x86_fldcw_membase (code, X86_ESP, 0);
3709 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3711 case OP_LCONV_TO_R8_2:
3712 x86_push_reg (code, ins->sreg2);
3713 x86_push_reg (code, ins->sreg1);
3714 x86_fild_membase (code, X86_ESP, 0, TRUE);
3715 /* Change precision */
3716 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
3717 x86_fld_membase (code, X86_ESP, 0, TRUE);
3718 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3720 case OP_LCONV_TO_R4_2:
3721 x86_push_reg (code, ins->sreg2);
3722 x86_push_reg (code, ins->sreg1);
3723 x86_fild_membase (code, X86_ESP, 0, TRUE);
3724 /* Change precision */
3725 x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
3726 x86_fld_membase (code, X86_ESP, 0, FALSE);
3727 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3729 case OP_LCONV_TO_R_UN_2: {
3730 static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
3733 /* load 64bit integer to FP stack */
3734 x86_push_reg (code, ins->sreg2);
3735 x86_push_reg (code, ins->sreg1);
3736 x86_fild_membase (code, X86_ESP, 0, TRUE);
3738 /* test if lreg is negative */
3739 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
3740 br = code; x86_branch8 (code, X86_CC_GEZ, 0, TRUE);
3742 /* add correction constant mn */
3743 if (cfg->compile_aot) {
3744 x86_push_imm (code, (((guint32)mn [9]) << 24) | ((guint32)mn [8] << 16) | ((guint32)mn [7] << 8) | ((guint32)mn [6]));
3745 x86_push_imm (code, (((guint32)mn [5]) << 24) | ((guint32)mn [4] << 16) | ((guint32)mn [3] << 8) | ((guint32)mn [2]));
3746 x86_push_imm (code, (((guint32)mn [1]) << 24) | ((guint32)mn [0] << 16));
3747 x86_fld80_membase (code, X86_ESP, 2);
3748 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
3750 x86_fld80_mem (code, mn);
3752 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3754 x86_patch (br, code);
3756 /* Change precision */
3757 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
3758 x86_fld_membase (code, X86_ESP, 0, TRUE);
3760 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3764 case OP_LCONV_TO_OVF_I:
3765 case OP_LCONV_TO_OVF_I4_2: {
3766 guint8 *br [3], *label [1];
3770 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
3772 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
3774 /* If the low word top bit is set, see if we are negative */
3775 br [0] = code; x86_branch8 (code, X86_CC_LT, 0, TRUE);
3776 /* We are not negative (no top bit set, check for our top word to be zero */
3777 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
3778 br [1] = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
3781 /* throw exception */
3782 tins = mono_branch_optimize_exception_target (cfg, bb, "OverflowException");
3784 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, tins->inst_true_bb);
3785 if ((cfg->opt & MONO_OPT_BRANCH) && x86_is_imm8 (tins->inst_true_bb->max_offset - cpos))
3786 x86_jump8 (code, 0);
3788 x86_jump32 (code, 0);
3790 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
3791 x86_jump32 (code, 0);
3795 x86_patch (br [0], code);
3796 /* our top bit is set, check that top word is 0xfffffff */
3797 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0xffffffff);
3799 x86_patch (br [1], code);
3800 /* nope, emit exception */
3801 br [2] = code; x86_branch8 (code, X86_CC_NE, 0, TRUE);
3802 x86_patch (br [2], label [0]);
3804 if (ins->dreg != ins->sreg1)
3805 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
3809 /* Not needed on the fp stack */
3812 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3815 x86_fp_op_reg (code, X86_FSUB, 1, TRUE);
3818 x86_fp_op_reg (code, X86_FMUL, 1, TRUE);
3821 x86_fp_op_reg (code, X86_FDIV, 1, TRUE);
3829 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3834 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3841 * it really doesn't make sense to inline all this code,
3842 * it's here just to show that things may not be as simple
3845 guchar *check_pos, *end_tan, *pop_jump;
3846 x86_push_reg (code, X86_EAX);
3849 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
3851 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3852 x86_fstp (code, 0); /* pop the 1.0 */
3854 x86_jump8 (code, 0);
3856 x86_fp_op (code, X86_FADD, 0);
3860 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
3862 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3865 x86_patch (pop_jump, code);
3866 x86_fstp (code, 0); /* pop the 1.0 */
3867 x86_patch (check_pos, code);
3868 x86_patch (end_tan, code);
3870 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3871 x86_pop_reg (code, X86_EAX);
3878 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3887 g_assert (cfg->opt & MONO_OPT_CMOV);
3888 g_assert (ins->dreg == ins->sreg1);
3889 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3890 x86_cmov_reg (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2);
3893 g_assert (cfg->opt & MONO_OPT_CMOV);
3894 g_assert (ins->dreg == ins->sreg1);
3895 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3896 x86_cmov_reg (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2);
3899 g_assert (cfg->opt & MONO_OPT_CMOV);
3900 g_assert (ins->dreg == ins->sreg1);
3901 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3902 x86_cmov_reg (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2);
3905 g_assert (cfg->opt & MONO_OPT_CMOV);
3906 g_assert (ins->dreg == ins->sreg1);
3907 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3908 x86_cmov_reg (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2);
3914 x86_fxch (code, ins->inst_imm);
3919 x86_push_reg (code, X86_EAX);
3920 /* we need to exchange ST(0) with ST(1) */
3923 /* this requires a loop, because fprem somtimes
3924 * returns a partial remainder */
3926 /* looks like MS is using fprem instead of the IEEE compatible fprem1 */
3927 /* x86_fprem1 (code); */
3930 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_C2);
3932 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3938 x86_pop_reg (code, X86_EAX);
3942 if (cfg->opt & MONO_OPT_FCMOV) {
3943 x86_fcomip (code, 1);
3947 /* this overwrites EAX */
3948 EMIT_FPCOMPARE(code);
3949 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3953 if (cfg->opt & MONO_OPT_FCMOV) {
3954 /* zeroing the register at the start results in
3955 * shorter and faster code (we can also remove the widening op)
3957 guchar *unordered_check;
3958 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3959 x86_fcomip (code, 1);
3961 unordered_check = code;
3962 x86_branch8 (code, X86_CC_P, 0, FALSE);
3963 if (ins->opcode == OP_FCEQ) {
3964 x86_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
3965 x86_patch (unordered_check, code);
3967 guchar *jump_to_end;
3968 x86_set_reg (code, X86_CC_NE, ins->dreg, FALSE);
3970 x86_jump8 (code, 0);
3971 x86_patch (unordered_check, code);
3972 x86_inc_reg (code, ins->dreg);
3973 x86_patch (jump_to_end, code);
3978 if (ins->dreg != X86_EAX)
3979 x86_push_reg (code, X86_EAX);
3981 EMIT_FPCOMPARE(code);
3982 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3983 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
3984 x86_set_reg (code, ins->opcode == OP_FCEQ ? X86_CC_EQ : X86_CC_NE, ins->dreg, TRUE);
3985 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3987 if (ins->dreg != X86_EAX)
3988 x86_pop_reg (code, X86_EAX);
3992 if (cfg->opt & MONO_OPT_FCMOV) {
3993 /* zeroing the register at the start results in
3994 * shorter and faster code (we can also remove the widening op)
3996 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3997 x86_fcomip (code, 1);
3999 if (ins->opcode == OP_FCLT_UN) {
4000 guchar *unordered_check = code;
4001 guchar *jump_to_end;
4002 x86_branch8 (code, X86_CC_P, 0, FALSE);
4003 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
4005 x86_jump8 (code, 0);
4006 x86_patch (unordered_check, code);
4007 x86_inc_reg (code, ins->dreg);
4008 x86_patch (jump_to_end, code);
4010 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
4014 if (ins->dreg != X86_EAX)
4015 x86_push_reg (code, X86_EAX);
4017 EMIT_FPCOMPARE(code);
4018 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
4019 if (ins->opcode == OP_FCLT_UN) {
4020 guchar *is_not_zero_check, *end_jump;
4021 is_not_zero_check = code;
4022 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
4024 x86_jump8 (code, 0);
4025 x86_patch (is_not_zero_check, code);
4026 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
4028 x86_patch (end_jump, code);
4030 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
4031 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
4033 if (ins->dreg != X86_EAX)
4034 x86_pop_reg (code, X86_EAX);
4037 guchar *unordered_check;
4038 guchar *jump_to_end;
4039 if (cfg->opt & MONO_OPT_FCMOV) {
4040 /* zeroing the register at the start results in
4041 * shorter and faster code (we can also remove the widening op)
4043 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4044 x86_fcomip (code, 1);
4046 unordered_check = code;
4047 x86_branch8 (code, X86_CC_P, 0, FALSE);
4048 x86_set_reg (code, X86_CC_NB, ins->dreg, FALSE);
4049 x86_patch (unordered_check, code);
4052 if (ins->dreg != X86_EAX)
4053 x86_push_reg (code, X86_EAX);
4055 EMIT_FPCOMPARE(code);
4056 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
4057 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
4058 unordered_check = code;
4059 x86_branch8 (code, X86_CC_EQ, 0, FALSE);
4061 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4062 x86_set_reg (code, X86_CC_NE, ins->dreg, TRUE);
4063 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
4065 x86_jump8 (code, 0);
4066 x86_patch (unordered_check, code);
4067 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4068 x86_patch (jump_to_end, code);
4070 if (ins->dreg != X86_EAX)
4071 x86_pop_reg (code, X86_EAX);
4076 if (cfg->opt & MONO_OPT_FCMOV) {
4077 /* zeroing the register at the start results in
4078 * shorter and faster code (we can also remove the widening op)
4080 guchar *unordered_check;
4081 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4082 x86_fcomip (code, 1);
4084 if (ins->opcode == OP_FCGT) {
4085 unordered_check = code;
4086 x86_branch8 (code, X86_CC_P, 0, FALSE);
4087 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
4088 x86_patch (unordered_check, code);
4090 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
4094 if (ins->dreg != X86_EAX)
4095 x86_push_reg (code, X86_EAX);
4097 EMIT_FPCOMPARE(code);
4098 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
4099 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4100 if (ins->opcode == OP_FCGT_UN) {
4101 guchar *is_not_zero_check, *end_jump;
4102 is_not_zero_check = code;
4103 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
4105 x86_jump8 (code, 0);
4106 x86_patch (is_not_zero_check, code);
4107 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
4109 x86_patch (end_jump, code);
4111 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
4112 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
4114 if (ins->dreg != X86_EAX)
4115 x86_pop_reg (code, X86_EAX);
4118 guchar *unordered_check;
4119 guchar *jump_to_end;
4120 if (cfg->opt & MONO_OPT_FCMOV) {
4121 /* zeroing the register at the start results in
4122 * shorter and faster code (we can also remove the widening op)
4124 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4125 x86_fcomip (code, 1);
4127 unordered_check = code;
4128 x86_branch8 (code, X86_CC_P, 0, FALSE);
4129 x86_set_reg (code, X86_CC_NA, ins->dreg, FALSE);
4130 x86_patch (unordered_check, code);
4133 if (ins->dreg != X86_EAX)
4134 x86_push_reg (code, X86_EAX);
4136 EMIT_FPCOMPARE(code);
4137 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
4138 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
4139 unordered_check = code;
4140 x86_branch8 (code, X86_CC_EQ, 0, FALSE);
4142 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4143 x86_set_reg (code, X86_CC_GE, ins->dreg, TRUE);
4144 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
4146 x86_jump8 (code, 0);
4147 x86_patch (unordered_check, code);
4148 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4149 x86_patch (jump_to_end, code);
4151 if (ins->dreg != X86_EAX)
4152 x86_pop_reg (code, X86_EAX);
4156 if (cfg->opt & MONO_OPT_FCMOV) {
4157 guchar *jump = code;
4158 x86_branch8 (code, X86_CC_P, 0, TRUE);
4159 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4160 x86_patch (jump, code);
4163 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
4164 EMIT_COND_BRANCH (ins, X86_CC_EQ, TRUE);
4167 /* Branch if C013 != 100 */
4168 if (cfg->opt & MONO_OPT_FCMOV) {
4169 /* branch if !ZF or (PF|CF) */
4170 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
4171 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
4172 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
4175 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
4176 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
4179 if (cfg->opt & MONO_OPT_FCMOV) {
4180 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
4183 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4186 if (cfg->opt & MONO_OPT_FCMOV) {
4187 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
4188 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
4191 if (ins->opcode == OP_FBLT_UN) {
4192 guchar *is_not_zero_check, *end_jump;
4193 is_not_zero_check = code;
4194 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
4196 x86_jump8 (code, 0);
4197 x86_patch (is_not_zero_check, code);
4198 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
4200 x86_patch (end_jump, code);
4202 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4206 if (cfg->opt & MONO_OPT_FCMOV) {
4207 if (ins->opcode == OP_FBGT) {
4210 /* skip branch if C1=1 */
4212 x86_branch8 (code, X86_CC_P, 0, FALSE);
4213 /* branch if (C0 | C3) = 1 */
4214 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
4215 x86_patch (br1, code);
4217 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
4221 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4222 if (ins->opcode == OP_FBGT_UN) {
4223 guchar *is_not_zero_check, *end_jump;
4224 is_not_zero_check = code;
4225 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
4227 x86_jump8 (code, 0);
4228 x86_patch (is_not_zero_check, code);
4229 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
4231 x86_patch (end_jump, code);
4233 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4236 /* Branch if C013 == 100 or 001 */
4237 if (cfg->opt & MONO_OPT_FCMOV) {
4240 /* skip branch if C1=1 */
4242 x86_branch8 (code, X86_CC_P, 0, FALSE);
4243 /* branch if (C0 | C3) = 1 */
4244 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
4245 x86_patch (br1, code);
4248 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4249 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4250 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
4251 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4254 /* Branch if C013 == 000 */
4255 if (cfg->opt & MONO_OPT_FCMOV) {
4256 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
4259 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
4262 /* Branch if C013=000 or 100 */
4263 if (cfg->opt & MONO_OPT_FCMOV) {
4266 /* skip branch if C1=1 */
4268 x86_branch8 (code, X86_CC_P, 0, FALSE);
4269 /* branch if C0=0 */
4270 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
4271 x86_patch (br1, code);
4274 x86_alu_reg_imm (code, X86_AND, X86_EAX, (X86_FP_C0|X86_FP_C1));
4275 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0);
4276 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4279 /* Branch if C013 != 001 */
4280 if (cfg->opt & MONO_OPT_FCMOV) {
4281 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
4282 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
4285 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4286 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
4290 x86_push_reg (code, X86_EAX);
4293 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
4294 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4295 x86_pop_reg (code, X86_EAX);
4297 /* Have to clean up the fp stack before throwing the exception */
4299 x86_branch8 (code, X86_CC_NE, 0, FALSE);
4302 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
4304 x86_patch (br1, code);
4308 code = mono_x86_emit_tls_get (code, ins->dreg, ins->inst_offset);
4311 case OP_TLS_GET_REG: {
4312 code = emit_tls_get_reg (code, ins->dreg, ins->sreg1);
4316 code = mono_x86_emit_tls_set (code, ins->sreg1, ins->inst_offset);
4319 case OP_TLS_SET_REG: {
4320 code = emit_tls_set_reg (code, ins->sreg1, ins->sreg2);
4323 case OP_MEMORY_BARRIER: {
4324 /* x86 only needs barrier for StoreLoad and FullBarrier */
4325 switch (ins->backend.memory_barrier_kind) {
4326 case StoreLoadBarrier:
4328 /* http://blogs.sun.com/dave/resource/NHM-Pipeline-Blog-V2.txt */
4329 x86_prefix (code, X86_LOCK_PREFIX);
4330 x86_alu_membase_imm (code, X86_ADD, X86_ESP, 0, 0);
4335 case OP_ATOMIC_ADD_I4: {
4336 int dreg = ins->dreg;
4338 if (dreg == ins->inst_basereg) {
4339 x86_push_reg (code, ins->sreg2);
4343 if (dreg != ins->sreg2)
4344 x86_mov_reg_reg (code, ins->dreg, ins->sreg2, 4);
4346 x86_prefix (code, X86_LOCK_PREFIX);
4347 x86_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, 4);
4349 if (dreg != ins->dreg) {
4350 x86_mov_reg_reg (code, ins->dreg, dreg, 4);
4351 x86_pop_reg (code, dreg);
4356 case OP_ATOMIC_ADD_NEW_I4: {
4357 int dreg = ins->dreg;
4359 /* hack: limit in regalloc, dreg != sreg1 && dreg != sreg2 */
4360 if (ins->sreg2 == dreg) {
4361 if (dreg == X86_EBX) {
4363 if (ins->inst_basereg == X86_EDI)
4367 if (ins->inst_basereg == X86_EBX)
4370 } else if (ins->inst_basereg == dreg) {
4371 if (dreg == X86_EBX) {
4373 if (ins->sreg2 == X86_EDI)
4377 if (ins->sreg2 == X86_EBX)
4382 if (dreg != ins->dreg) {
4383 x86_push_reg (code, dreg);
4386 x86_mov_reg_reg (code, dreg, ins->sreg2, 4);
4387 x86_prefix (code, X86_LOCK_PREFIX);
4388 x86_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, 4);
4389 /* dreg contains the old value, add with sreg2 value */
4390 x86_alu_reg_reg (code, X86_ADD, dreg, ins->sreg2);
4392 if (ins->dreg != dreg) {
4393 x86_mov_reg_reg (code, ins->dreg, dreg, 4);
4394 x86_pop_reg (code, dreg);
4399 case OP_ATOMIC_EXCHANGE_I4: {
4401 int sreg2 = ins->sreg2;
4402 int breg = ins->inst_basereg;
4404 /* cmpxchg uses eax as comperand, need to make sure we can use it
4405 * hack to overcome limits in x86 reg allocator
4406 * (req: dreg == eax and sreg2 != eax and breg != eax)
4408 g_assert (ins->dreg == X86_EAX);
4410 /* We need the EAX reg for the cmpxchg */
4411 if (ins->sreg2 == X86_EAX) {
4412 sreg2 = (breg == X86_EDX) ? X86_EBX : X86_EDX;
4413 x86_push_reg (code, sreg2);
4414 x86_mov_reg_reg (code, sreg2, X86_EAX, 4);
4417 if (breg == X86_EAX) {
4418 breg = (sreg2 == X86_ESI) ? X86_EDI : X86_ESI;
4419 x86_push_reg (code, breg);
4420 x86_mov_reg_reg (code, breg, X86_EAX, 4);
4423 x86_mov_reg_membase (code, X86_EAX, breg, ins->inst_offset, 4);
4425 br [0] = code; x86_prefix (code, X86_LOCK_PREFIX);
4426 x86_cmpxchg_membase_reg (code, breg, ins->inst_offset, sreg2);
4427 br [1] = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
4428 x86_patch (br [1], br [0]);
4430 if (breg != ins->inst_basereg)
4431 x86_pop_reg (code, breg);
4433 if (ins->sreg2 != sreg2)
4434 x86_pop_reg (code, sreg2);
4438 case OP_ATOMIC_CAS_I4: {
4439 g_assert (ins->dreg == X86_EAX);
4440 g_assert (ins->sreg3 == X86_EAX);
4441 g_assert (ins->sreg1 != X86_EAX);
4442 g_assert (ins->sreg1 != ins->sreg2);
4444 x86_prefix (code, X86_LOCK_PREFIX);
4445 x86_cmpxchg_membase_reg (code, ins->sreg1, ins->inst_offset, ins->sreg2);
4448 case OP_CARD_TABLE_WBARRIER: {
4449 int ptr = ins->sreg1;
4450 int value = ins->sreg2;
4452 int nursery_shift, card_table_shift;
4453 gpointer card_table_mask;
4454 size_t nursery_size;
4455 gulong card_table = (gulong)mono_gc_get_card_table (&card_table_shift, &card_table_mask);
4456 gulong nursery_start = (gulong)mono_gc_get_nursery (&nursery_shift, &nursery_size);
4457 gboolean card_table_nursery_check = mono_gc_card_table_nursery_check ();
4460 * We need one register we can clobber, we choose EDX and make sreg1
4461 * fixed EAX to work around limitations in the local register allocator.
4462 * sreg2 might get allocated to EDX, but that is not a problem since
4463 * we use it before clobbering EDX.
4465 g_assert (ins->sreg1 == X86_EAX);
4468 * This is the code we produce:
4471 * edx >>= nursery_shift
4472 * cmp edx, (nursery_start >> nursery_shift)
4475 * edx >>= card_table_shift
4476 * card_table[edx] = 1
4480 if (card_table_nursery_check) {
4481 if (value != X86_EDX)
4482 x86_mov_reg_reg (code, X86_EDX, value, 4);
4483 x86_shift_reg_imm (code, X86_SHR, X86_EDX, nursery_shift);
4484 x86_alu_reg_imm (code, X86_CMP, X86_EDX, nursery_start >> nursery_shift);
4485 br = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
4487 x86_mov_reg_reg (code, X86_EDX, ptr, 4);
4488 x86_shift_reg_imm (code, X86_SHR, X86_EDX, card_table_shift);
4489 if (card_table_mask)
4490 x86_alu_reg_imm (code, X86_AND, X86_EDX, (int)card_table_mask);
4491 x86_mov_membase_imm (code, X86_EDX, card_table, 1, 1);
4492 if (card_table_nursery_check)
4493 x86_patch (br, code);
4496 #ifdef MONO_ARCH_SIMD_INTRINSICS
4498 x86_sse_alu_ps_reg_reg (code, X86_SSE_ADD, ins->sreg1, ins->sreg2);
4501 x86_sse_alu_ps_reg_reg (code, X86_SSE_DIV, ins->sreg1, ins->sreg2);
4504 x86_sse_alu_ps_reg_reg (code, X86_SSE_MUL, ins->sreg1, ins->sreg2);
4507 x86_sse_alu_ps_reg_reg (code, X86_SSE_SUB, ins->sreg1, ins->sreg2);
4510 x86_sse_alu_ps_reg_reg (code, X86_SSE_MAX, ins->sreg1, ins->sreg2);
4513 x86_sse_alu_ps_reg_reg (code, X86_SSE_MIN, ins->sreg1, ins->sreg2);
4516 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
4517 x86_sse_alu_ps_reg_reg_imm (code, X86_SSE_COMP, ins->sreg1, ins->sreg2, ins->inst_c0);
4520 x86_sse_alu_ps_reg_reg (code, X86_SSE_AND, ins->sreg1, ins->sreg2);
4523 x86_sse_alu_ps_reg_reg (code, X86_SSE_ANDN, ins->sreg1, ins->sreg2);
4526 x86_sse_alu_ps_reg_reg (code, X86_SSE_OR, ins->sreg1, ins->sreg2);
4529 x86_sse_alu_ps_reg_reg (code, X86_SSE_XOR, ins->sreg1, ins->sreg2);
4532 x86_sse_alu_ps_reg_reg (code, X86_SSE_SQRT, ins->dreg, ins->sreg1);
4535 x86_sse_alu_ps_reg_reg (code, X86_SSE_RSQRT, ins->dreg, ins->sreg1);
4538 x86_sse_alu_ps_reg_reg (code, X86_SSE_RCP, ins->dreg, ins->sreg1);
4541 x86_sse_alu_sd_reg_reg (code, X86_SSE_ADDSUB, ins->sreg1, ins->sreg2);
4544 x86_sse_alu_sd_reg_reg (code, X86_SSE_HADD, ins->sreg1, ins->sreg2);
4547 x86_sse_alu_sd_reg_reg (code, X86_SSE_HSUB, ins->sreg1, ins->sreg2);
4550 x86_sse_alu_ss_reg_reg (code, X86_SSE_MOVSHDUP, ins->dreg, ins->sreg1);
4553 x86_sse_alu_ss_reg_reg (code, X86_SSE_MOVSLDUP, ins->dreg, ins->sreg1);
4556 case OP_PSHUFLEW_HIGH:
4557 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
4558 x86_pshufw_reg_reg (code, ins->dreg, ins->sreg1, ins->inst_c0, 1);
4560 case OP_PSHUFLEW_LOW:
4561 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
4562 x86_pshufw_reg_reg (code, ins->dreg, ins->sreg1, ins->inst_c0, 0);
4565 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
4566 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->sreg1, ins->inst_c0);
4569 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
4570 x86_sse_alu_reg_reg_imm8 (code, X86_SSE_SHUFP, ins->sreg1, ins->sreg2, ins->inst_c0);
4573 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0x3);
4574 x86_sse_alu_pd_reg_reg_imm8 (code, X86_SSE_SHUFP, ins->sreg1, ins->sreg2, ins->inst_c0);
4578 x86_sse_alu_pd_reg_reg (code, X86_SSE_ADD, ins->sreg1, ins->sreg2);
4581 x86_sse_alu_pd_reg_reg (code, X86_SSE_DIV, ins->sreg1, ins->sreg2);
4584 x86_sse_alu_pd_reg_reg (code, X86_SSE_MUL, ins->sreg1, ins->sreg2);
4587 x86_sse_alu_pd_reg_reg (code, X86_SSE_SUB, ins->sreg1, ins->sreg2);
4590 x86_sse_alu_pd_reg_reg (code, X86_SSE_MAX, ins->sreg1, ins->sreg2);
4593 x86_sse_alu_pd_reg_reg (code, X86_SSE_MIN, ins->sreg1, ins->sreg2);
4596 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
4597 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_COMP, ins->sreg1, ins->sreg2, ins->inst_c0);
4600 x86_sse_alu_pd_reg_reg (code, X86_SSE_AND, ins->sreg1, ins->sreg2);
4603 x86_sse_alu_pd_reg_reg (code, X86_SSE_ANDN, ins->sreg1, ins->sreg2);
4606 x86_sse_alu_pd_reg_reg (code, X86_SSE_OR, ins->sreg1, ins->sreg2);
4609 x86_sse_alu_pd_reg_reg (code, X86_SSE_XOR, ins->sreg1, ins->sreg2);
4612 x86_sse_alu_pd_reg_reg (code, X86_SSE_SQRT, ins->dreg, ins->sreg1);
4615 x86_sse_alu_pd_reg_reg (code, X86_SSE_ADDSUB, ins->sreg1, ins->sreg2);
4618 x86_sse_alu_pd_reg_reg (code, X86_SSE_HADD, ins->sreg1, ins->sreg2);
4621 x86_sse_alu_pd_reg_reg (code, X86_SSE_HSUB, ins->sreg1, ins->sreg2);
4624 x86_sse_alu_sd_reg_reg (code, X86_SSE_MOVDDUP, ins->dreg, ins->sreg1);
4627 case OP_EXTRACT_MASK:
4628 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMOVMSKB, ins->dreg, ins->sreg1);
4632 x86_sse_alu_pd_reg_reg (code, X86_SSE_PAND, ins->sreg1, ins->sreg2);
4635 x86_sse_alu_pd_reg_reg (code, X86_SSE_POR, ins->sreg1, ins->sreg2);
4638 x86_sse_alu_pd_reg_reg (code, X86_SSE_PXOR, ins->sreg1, ins->sreg2);
4642 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDB, ins->sreg1, ins->sreg2);
4645 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDW, ins->sreg1, ins->sreg2);
4648 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDD, ins->sreg1, ins->sreg2);
4651 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDQ, ins->sreg1, ins->sreg2);
4655 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBB, ins->sreg1, ins->sreg2);
4658 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBW, ins->sreg1, ins->sreg2);
4661 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBD, ins->sreg1, ins->sreg2);
4664 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBQ, ins->sreg1, ins->sreg2);
4668 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMAXUB, ins->sreg1, ins->sreg2);
4671 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXUW, ins->sreg1, ins->sreg2);
4674 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXUD, ins->sreg1, ins->sreg2);
4678 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXSB, ins->sreg1, ins->sreg2);
4681 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMAXSW, ins->sreg1, ins->sreg2);
4684 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXSD, ins->sreg1, ins->sreg2);
4688 x86_sse_alu_pd_reg_reg (code, X86_SSE_PAVGB, ins->sreg1, ins->sreg2);
4691 x86_sse_alu_pd_reg_reg (code, X86_SSE_PAVGW, ins->sreg1, ins->sreg2);
4695 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMINUB, ins->sreg1, ins->sreg2);
4698 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINUW, ins->sreg1, ins->sreg2);
4701 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINUD, ins->sreg1, ins->sreg2);
4705 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINSB, ins->sreg1, ins->sreg2);
4708 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMINSW, ins->sreg1, ins->sreg2);
4711 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINSD, ins->sreg1, ins->sreg2);
4715 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQB, ins->sreg1, ins->sreg2);
4718 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQW, ins->sreg1, ins->sreg2);
4721 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQD, ins->sreg1, ins->sreg2);
4724 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PCMPEQQ, ins->sreg1, ins->sreg2);
4728 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPGTB, ins->sreg1, ins->sreg2);
4731 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPGTW, ins->sreg1, ins->sreg2);
4734 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPGTD, ins->sreg1, ins->sreg2);
4737 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PCMPGTQ, ins->sreg1, ins->sreg2);
4740 case OP_PSUM_ABS_DIFF:
4741 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSADBW, ins->sreg1, ins->sreg2);
4744 case OP_UNPACK_LOWB:
4745 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLBW, ins->sreg1, ins->sreg2);
4747 case OP_UNPACK_LOWW:
4748 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLWD, ins->sreg1, ins->sreg2);
4750 case OP_UNPACK_LOWD:
4751 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLDQ, ins->sreg1, ins->sreg2);
4753 case OP_UNPACK_LOWQ:
4754 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLQDQ, ins->sreg1, ins->sreg2);
4756 case OP_UNPACK_LOWPS:
4757 x86_sse_alu_ps_reg_reg (code, X86_SSE_UNPCKL, ins->sreg1, ins->sreg2);
4759 case OP_UNPACK_LOWPD:
4760 x86_sse_alu_pd_reg_reg (code, X86_SSE_UNPCKL, ins->sreg1, ins->sreg2);
4763 case OP_UNPACK_HIGHB:
4764 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHBW, ins->sreg1, ins->sreg2);
4766 case OP_UNPACK_HIGHW:
4767 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHWD, ins->sreg1, ins->sreg2);
4769 case OP_UNPACK_HIGHD:
4770 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHDQ, ins->sreg1, ins->sreg2);
4772 case OP_UNPACK_HIGHQ:
4773 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHQDQ, ins->sreg1, ins->sreg2);
4775 case OP_UNPACK_HIGHPS:
4776 x86_sse_alu_ps_reg_reg (code, X86_SSE_UNPCKH, ins->sreg1, ins->sreg2);
4778 case OP_UNPACK_HIGHPD:
4779 x86_sse_alu_pd_reg_reg (code, X86_SSE_UNPCKH, ins->sreg1, ins->sreg2);
4783 x86_sse_alu_pd_reg_reg (code, X86_SSE_PACKSSWB, ins->sreg1, ins->sreg2);
4786 x86_sse_alu_pd_reg_reg (code, X86_SSE_PACKSSDW, ins->sreg1, ins->sreg2);
4789 x86_sse_alu_pd_reg_reg (code, X86_SSE_PACKUSWB, ins->sreg1, ins->sreg2);
4792 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PACKUSDW, ins->sreg1, ins->sreg2);
4795 case OP_PADDB_SAT_UN:
4796 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDUSB, ins->sreg1, ins->sreg2);
4798 case OP_PSUBB_SAT_UN:
4799 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBUSB, ins->sreg1, ins->sreg2);
4801 case OP_PADDW_SAT_UN:
4802 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDUSW, ins->sreg1, ins->sreg2);
4804 case OP_PSUBW_SAT_UN:
4805 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBUSW, ins->sreg1, ins->sreg2);
4809 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDSB, ins->sreg1, ins->sreg2);
4812 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBSB, ins->sreg1, ins->sreg2);
4815 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDSW, ins->sreg1, ins->sreg2);
4818 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBSW, ins->sreg1, ins->sreg2);
4822 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULLW, ins->sreg1, ins->sreg2);
4825 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMULLD, ins->sreg1, ins->sreg2);
4828 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULUDQ, ins->sreg1, ins->sreg2);
4830 case OP_PMULW_HIGH_UN:
4831 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULHUW, ins->sreg1, ins->sreg2);
4834 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULHW, ins->sreg1, ins->sreg2);
4838 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTW, X86_SSE_SHR, ins->dreg, ins->inst_imm);
4841 x86_sse_shift_reg_reg (code, X86_SSE_PSRLW_REG, ins->dreg, ins->sreg2);
4845 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTW, X86_SSE_SAR, ins->dreg, ins->inst_imm);
4848 x86_sse_shift_reg_reg (code, X86_SSE_PSRAW_REG, ins->dreg, ins->sreg2);
4852 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTW, X86_SSE_SHL, ins->dreg, ins->inst_imm);
4855 x86_sse_shift_reg_reg (code, X86_SSE_PSLLW_REG, ins->dreg, ins->sreg2);
4859 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTD, X86_SSE_SHR, ins->dreg, ins->inst_imm);
4862 x86_sse_shift_reg_reg (code, X86_SSE_PSRLD_REG, ins->dreg, ins->sreg2);
4866 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTD, X86_SSE_SAR, ins->dreg, ins->inst_imm);
4869 x86_sse_shift_reg_reg (code, X86_SSE_PSRAD_REG, ins->dreg, ins->sreg2);
4873 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTD, X86_SSE_SHL, ins->dreg, ins->inst_imm);
4876 x86_sse_shift_reg_reg (code, X86_SSE_PSLLD_REG, ins->dreg, ins->sreg2);
4880 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTQ, X86_SSE_SHR, ins->dreg, ins->inst_imm);
4883 x86_sse_shift_reg_reg (code, X86_SSE_PSRLQ_REG, ins->dreg, ins->sreg2);
4887 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTQ, X86_SSE_SHL, ins->dreg, ins->inst_imm);
4890 x86_sse_shift_reg_reg (code, X86_SSE_PSLLQ_REG, ins->dreg, ins->sreg2);
4894 x86_movd_xreg_reg (code, ins->dreg, ins->sreg1);
4897 x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
4901 x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
4903 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_c0 * 8);
4904 x86_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I1, FALSE);
4908 x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
4910 x86_shift_reg_imm (code, X86_SHR, ins->dreg, 16);
4911 x86_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I2, TRUE);
4915 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);
4917 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);
4918 x86_fld_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE);
4922 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->sreg1, ins->sreg2, ins->inst_c0);
4924 case OP_EXTRACTX_U2:
4925 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PEXTRW, ins->dreg, ins->sreg1, ins->inst_c0);
4927 case OP_INSERTX_U1_SLOW:
4928 /*sreg1 is the extracted ireg (scratch)
4929 /sreg2 is the to be inserted ireg (scratch)
4930 /dreg is the xreg to receive the value*/
4932 /*clear the bits from the extracted word*/
4933 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_c0 & 1 ? 0x00FF : 0xFF00);
4934 /*shift the value to insert if needed*/
4935 if (ins->inst_c0 & 1)
4936 x86_shift_reg_imm (code, X86_SHL, ins->sreg2, 8);
4937 /*join them together*/
4938 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
4939 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, ins->inst_c0 / 2);
4941 case OP_INSERTX_I4_SLOW:
4942 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg2, ins->inst_c0 * 2);
4943 x86_shift_reg_imm (code, X86_SHR, ins->sreg2, 16);
4944 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg2, ins->inst_c0 * 2 + 1);
4947 case OP_INSERTX_R4_SLOW:
4948 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE, TRUE);
4949 /*TODO if inst_c0 == 0 use movss*/
4950 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);
4951 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);
4953 case OP_INSERTX_R8_SLOW:
4954 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
4955 if (cfg->verbose_level)
4956 printf ("CONVERTING a OP_INSERTX_R8_SLOW %d offset %x\n", ins->inst_c0, offset);
4958 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);
4960 x86_movsd_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
4963 case OP_STOREX_MEMBASE_REG:
4964 case OP_STOREX_MEMBASE:
4965 x86_movups_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
4967 case OP_LOADX_MEMBASE:
4968 x86_movups_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
4970 case OP_LOADX_ALIGNED_MEMBASE:
4971 x86_movaps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
4973 case OP_STOREX_ALIGNED_MEMBASE_REG:
4974 x86_movaps_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
4976 case OP_STOREX_NTA_MEMBASE_REG:
4977 x86_sse_alu_reg_membase (code, X86_SSE_MOVNTPS, ins->dreg, ins->sreg1, ins->inst_offset);
4979 case OP_PREFETCH_MEMBASE:
4980 x86_sse_alu_reg_membase (code, X86_SSE_PREFETCH, ins->backend.arg_info, ins->sreg1, ins->inst_offset);
4984 /*FIXME the peephole pass should have killed this*/
4985 if (ins->dreg != ins->sreg1)
4986 x86_movaps_reg_reg (code, ins->dreg, ins->sreg1);
4989 x86_sse_alu_pd_reg_reg (code, X86_SSE_PXOR, ins->dreg, ins->dreg);
4991 case OP_ICONV_TO_R8_RAW:
4992 x86_mov_membase_reg (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, ins->sreg1, 4);
4993 x86_fld_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE);
4996 case OP_FCONV_TO_R8_X:
4997 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
4998 x86_movsd_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
5001 case OP_XCONV_R8_TO_I4:
5002 x86_cvttsd2si (code, ins->dreg, ins->sreg1);
5003 switch (ins->backend.source_opcode) {
5004 case OP_FCONV_TO_I1:
5005 x86_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
5007 case OP_FCONV_TO_U1:
5008 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
5010 case OP_FCONV_TO_I2:
5011 x86_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
5013 case OP_FCONV_TO_U2:
5014 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
5020 /*FIXME this causes a partial register stall, maybe it would not be that bad to use shift + mask + or*/
5021 /*The +4 is to get a mov ?h, ?l over the same reg.*/
5022 x86_mov_reg_reg (code, ins->dreg + 4, ins->dreg, 1);
5023 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 0);
5024 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 1);
5025 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
5028 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 0);
5029 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 1);
5030 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
5033 x86_movd_xreg_reg (code, ins->dreg, ins->sreg1);
5034 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
5037 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE, TRUE);
5038 x86_movd_xreg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
5039 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
5042 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
5043 x86_movsd_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
5044 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0x44);
5048 x86_sse_alu_ss_reg_reg (code, X86_SSE_CVTDQ2PD, ins->dreg, ins->sreg1);
5051 x86_sse_alu_ps_reg_reg (code, X86_SSE_CVTDQ2PS, ins->dreg, ins->sreg1);
5054 x86_sse_alu_sd_reg_reg (code, X86_SSE_CVTPD2DQ, ins->dreg, ins->sreg1);
5057 x86_sse_alu_pd_reg_reg (code, X86_SSE_CVTPD2PS, ins->dreg, ins->sreg1);
5060 x86_sse_alu_pd_reg_reg (code, X86_SSE_CVTPS2DQ, ins->dreg, ins->sreg1);
5063 x86_sse_alu_ps_reg_reg (code, X86_SSE_CVTPS2PD, ins->dreg, ins->sreg1);
5066 x86_sse_alu_pd_reg_reg (code, X86_SSE_CVTTPD2DQ, ins->dreg, ins->sreg1);
5069 x86_sse_alu_ss_reg_reg (code, X86_SSE_CVTTPS2DQ, ins->dreg, ins->sreg1);
5073 case OP_LIVERANGE_START: {
5074 if (cfg->verbose_level > 1)
5075 printf ("R%d START=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
5076 MONO_VARINFO (cfg, ins->inst_c0)->live_range_start = code - cfg->native_code;
5079 case OP_LIVERANGE_END: {
5080 if (cfg->verbose_level > 1)
5081 printf ("R%d END=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
5082 MONO_VARINFO (cfg, ins->inst_c0)->live_range_end = code - cfg->native_code;
5085 case OP_NACL_GC_SAFE_POINT: {
5086 #if defined(__native_client_codegen__) && defined(__native_client_gc__)
5087 if (cfg->compile_aot)
5088 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)mono_nacl_gc);
5092 x86_test_mem_imm8 (code, (gpointer)&__nacl_thread_suspension_needed, 0xFFFFFFFF);
5093 br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
5094 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)mono_nacl_gc);
5095 x86_patch (br[0], code);
5100 case OP_GC_LIVENESS_DEF:
5101 case OP_GC_LIVENESS_USE:
5102 case OP_GC_PARAM_SLOT_LIVENESS_DEF:
5103 ins->backend.pc_offset = code - cfg->native_code;
5105 case OP_GC_SPILL_SLOT_LIVENESS_DEF:
5106 ins->backend.pc_offset = code - cfg->native_code;
5107 bb->spill_slot_defs = g_slist_prepend_mempool (cfg->mempool, bb->spill_slot_defs, ins);
5110 g_warning ("unknown opcode %s\n", mono_inst_name (ins->opcode));
5111 g_assert_not_reached ();
5114 if (G_UNLIKELY ((code - cfg->native_code - offset) > max_len)) {
5115 #ifndef __native_client_codegen__
5116 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
5117 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
5118 g_assert_not_reached ();
5119 #endif /* __native_client_codegen__ */
5125 cfg->code_len = code - cfg->native_code;
5128 #endif /* DISABLE_JIT */
5131 mono_arch_register_lowlevel_calls (void)
5136 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, MonoCodeManager *dyn_code_mp, gboolean run_cctors)
5138 MonoJumpInfo *patch_info;
5139 gboolean compile_aot = !run_cctors;
5141 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
5142 unsigned char *ip = patch_info->ip.i + code;
5143 const unsigned char *target;
5146 switch (patch_info->type) {
5147 case MONO_PATCH_INFO_BB:
5148 case MONO_PATCH_INFO_LABEL:
5151 /* No need to patch these */
5156 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
5158 switch (patch_info->type) {
5159 case MONO_PATCH_INFO_IP:
5160 *((gconstpointer *)(ip)) = target;
5162 case MONO_PATCH_INFO_CLASS_INIT: {
5164 /* Might already been changed to a nop */
5165 x86_call_code (code, 0);
5166 x86_patch (ip, target);
5169 case MONO_PATCH_INFO_ABS:
5170 case MONO_PATCH_INFO_METHOD:
5171 case MONO_PATCH_INFO_METHOD_JUMP:
5172 case MONO_PATCH_INFO_INTERNAL_METHOD:
5173 case MONO_PATCH_INFO_BB:
5174 case MONO_PATCH_INFO_LABEL:
5175 case MONO_PATCH_INFO_RGCTX_FETCH:
5176 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
5177 case MONO_PATCH_INFO_MONITOR_ENTER:
5178 case MONO_PATCH_INFO_MONITOR_EXIT:
5179 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
5180 #if defined(__native_client_codegen__) && defined(__native_client__)
5181 if (nacl_is_code_address (code)) {
5182 /* For tail calls, code is patched after being installed */
5183 /* but not through the normal "patch callsite" method. */
5184 unsigned char buf[kNaClAlignment];
5185 unsigned char *aligned_code = (uintptr_t)code & ~kNaClAlignmentMask;
5186 unsigned char *_target = target;
5188 /* All patch targets modified in x86_patch */
5189 /* are IP relative. */
5190 _target = _target + (uintptr_t)buf - (uintptr_t)aligned_code;
5191 memcpy (buf, aligned_code, kNaClAlignment);
5192 /* Patch a temp buffer of bundle size, */
5193 /* then install to actual location. */
5194 x86_patch (buf + ((uintptr_t)code - (uintptr_t)aligned_code), _target);
5195 ret = nacl_dyncode_modify (aligned_code, buf, kNaClAlignment);
5196 g_assert (ret == 0);
5199 x86_patch (ip, target);
5202 x86_patch (ip, target);
5205 case MONO_PATCH_INFO_NONE:
5207 case MONO_PATCH_INFO_R4:
5208 case MONO_PATCH_INFO_R8: {
5209 guint32 offset = mono_arch_get_patch_offset (ip);
5210 *((gconstpointer *)(ip + offset)) = target;
5214 guint32 offset = mono_arch_get_patch_offset (ip);
5215 #if !defined(__native_client__)
5216 *((gconstpointer *)(ip + offset)) = target;
5218 *((gconstpointer *)(ip + offset)) = nacl_modify_patch_target (target);
5226 static G_GNUC_UNUSED void
5227 stack_unaligned (MonoMethod *m, gpointer caller)
5229 printf ("%s\n", mono_method_full_name (m, TRUE));
5230 g_assert_not_reached ();
5234 mono_arch_emit_prolog (MonoCompile *cfg)
5236 MonoMethod *method = cfg->method;
5238 MonoMethodSignature *sig;
5240 int alloc_size, pos, max_offset, i, cfa_offset;
5242 gboolean need_stack_frame;
5243 #ifdef __native_client_codegen__
5244 guint alignment_check;
5247 cfg->code_size = MAX (cfg->header->code_size * 4, 10240);
5249 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
5250 cfg->code_size += 512;
5252 #if defined(__default_codegen__)
5253 code = cfg->native_code = g_malloc (cfg->code_size);
5254 #elif defined(__native_client_codegen__)
5255 /* native_code_alloc is not 32-byte aligned, native_code is. */
5256 cfg->code_size = NACL_BUNDLE_ALIGN_UP (cfg->code_size);
5257 cfg->native_code_alloc = g_malloc (cfg->code_size + kNaClAlignment);
5259 /* Align native_code to next nearest kNaclAlignment byte. */
5260 cfg->native_code = (guint)cfg->native_code_alloc + kNaClAlignment;
5261 cfg->native_code = (guint)cfg->native_code & ~kNaClAlignmentMask;
5263 code = cfg->native_code;
5265 alignment_check = (guint)cfg->native_code & kNaClAlignmentMask;
5266 g_assert(alignment_check == 0);
5273 /* Check that the stack is aligned on osx */
5274 x86_mov_reg_reg (code, X86_EAX, X86_ESP, sizeof (mgreg_t));
5275 x86_alu_reg_imm (code, X86_AND, X86_EAX, 15);
5276 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0xc);
5278 x86_branch_disp (code, X86_CC_Z, 0, FALSE);
5279 x86_push_membase (code, X86_ESP, 0);
5280 x86_push_imm (code, cfg->method);
5281 x86_mov_reg_imm (code, X86_EAX, stack_unaligned);
5282 x86_call_reg (code, X86_EAX);
5283 x86_patch (br [0], code);
5287 /* Offset between RSP and the CFA */
5291 cfa_offset = sizeof (gpointer);
5292 mono_emit_unwind_op_def_cfa (cfg, code, X86_ESP, sizeof (gpointer));
5293 // IP saved at CFA - 4
5294 /* There is no IP reg on x86 */
5295 mono_emit_unwind_op_offset (cfg, code, X86_NREG, -cfa_offset);
5296 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
5298 need_stack_frame = needs_stack_frame (cfg);
5300 if (need_stack_frame) {
5301 x86_push_reg (code, X86_EBP);
5302 cfa_offset += sizeof (gpointer);
5303 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
5304 mono_emit_unwind_op_offset (cfg, code, X86_EBP, - cfa_offset);
5305 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
5306 mono_emit_unwind_op_def_cfa_reg (cfg, code, X86_EBP);
5307 /* These are handled automatically by the stack marking code */
5308 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
5310 cfg->frame_reg = X86_ESP;
5313 alloc_size = cfg->stack_offset;
5316 if (!method->save_lmf) {
5317 if (cfg->used_int_regs & (1 << X86_EBX)) {
5318 x86_push_reg (code, X86_EBX);
5320 cfa_offset += sizeof (gpointer);
5321 mono_emit_unwind_op_offset (cfg, code, X86_EBX, - cfa_offset);
5322 /* These are handled automatically by the stack marking code */
5323 mini_gc_set_slot_type_from_cfa (cfg, - cfa_offset, SLOT_NOREF);
5326 if (cfg->used_int_regs & (1 << X86_EDI)) {
5327 x86_push_reg (code, X86_EDI);
5329 cfa_offset += sizeof (gpointer);
5330 mono_emit_unwind_op_offset (cfg, code, X86_EDI, - cfa_offset);
5331 mini_gc_set_slot_type_from_cfa (cfg, - cfa_offset, SLOT_NOREF);
5334 if (cfg->used_int_regs & (1 << X86_ESI)) {
5335 x86_push_reg (code, X86_ESI);
5337 cfa_offset += sizeof (gpointer);
5338 mono_emit_unwind_op_offset (cfg, code, X86_ESI, - cfa_offset);
5339 mini_gc_set_slot_type_from_cfa (cfg, - cfa_offset, SLOT_NOREF);
5345 /* the original alloc_size is already aligned: there is %ebp and retip pushed, so realign */
5346 if (mono_do_x86_stack_align && need_stack_frame) {
5347 int tot = alloc_size + pos + 4; /* ret ip */
5348 if (need_stack_frame)
5350 tot &= MONO_ARCH_FRAME_ALIGNMENT - 1;
5352 alloc_size += MONO_ARCH_FRAME_ALIGNMENT - tot;
5353 for (i = 0; i < MONO_ARCH_FRAME_ALIGNMENT - tot; i += sizeof (mgreg_t))
5354 mini_gc_set_slot_type_from_fp (cfg, - (alloc_size + pos - i), SLOT_NOREF);
5358 cfg->arch.sp_fp_offset = alloc_size + pos;
5361 /* See mono_emit_stack_alloc */
5362 #if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
5363 guint32 remaining_size = alloc_size;
5364 /*FIXME handle unbounded code expansion, we should use a loop in case of more than X interactions*/
5365 guint32 required_code_size = ((remaining_size / 0x1000) + 1) * 8; /*8 is the max size of x86_alu_reg_imm + x86_test_membase_reg*/
5366 guint32 offset = code - cfg->native_code;
5367 if (G_UNLIKELY (required_code_size >= (cfg->code_size - offset))) {
5368 while (required_code_size >= (cfg->code_size - offset))
5369 cfg->code_size *= 2;
5370 cfg->native_code = mono_realloc_native_code(cfg);
5371 code = cfg->native_code + offset;
5372 cfg->stat_code_reallocs++;
5374 while (remaining_size >= 0x1000) {
5375 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
5376 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
5377 remaining_size -= 0x1000;
5380 x86_alu_reg_imm (code, X86_SUB, X86_ESP, remaining_size);
5382 x86_alu_reg_imm (code, X86_SUB, X86_ESP, alloc_size);
5385 g_assert (need_stack_frame);
5388 if (cfg->method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED ||
5389 cfg->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
5390 x86_alu_reg_imm (code, X86_AND, X86_ESP, -MONO_ARCH_FRAME_ALIGNMENT);
5393 #if DEBUG_STACK_ALIGNMENT
5394 /* check the stack is aligned */
5395 if (need_stack_frame && method->wrapper_type == MONO_WRAPPER_NONE) {
5396 x86_mov_reg_reg (code, X86_ECX, X86_ESP, 4);
5397 x86_alu_reg_imm (code, X86_AND, X86_ECX, MONO_ARCH_FRAME_ALIGNMENT - 1);
5398 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
5399 x86_branch_disp (code, X86_CC_EQ, 3, FALSE);
5400 x86_breakpoint (code);
5404 /* compute max_offset in order to use short forward jumps */
5406 if (cfg->opt & MONO_OPT_BRANCH) {
5407 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5409 bb->max_offset = max_offset;
5411 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
5413 /* max alignment for loops */
5414 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
5415 max_offset += LOOP_ALIGNMENT;
5416 #ifdef __native_client_codegen__
5417 /* max alignment for native client */
5418 if (bb->flags & BB_INDIRECT_JUMP_TARGET || bb->flags & BB_EXCEPTION_HANDLER)
5419 max_offset += kNaClAlignment;
5421 MONO_BB_FOR_EACH_INS (bb, ins) {
5422 if (ins->opcode == OP_LABEL)
5423 ins->inst_c1 = max_offset;
5424 #ifdef __native_client_codegen__
5425 switch (ins->opcode)
5437 case OP_VOIDCALL_REG:
5439 case OP_FCALL_MEMBASE:
5440 case OP_LCALL_MEMBASE:
5441 case OP_VCALL_MEMBASE:
5442 case OP_VCALL2_MEMBASE:
5443 case OP_VOIDCALL_MEMBASE:
5444 case OP_CALL_MEMBASE:
5445 max_offset += kNaClAlignment;
5448 max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN] - 1;
5451 #endif /* __native_client_codegen__ */
5452 max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
5457 /* store runtime generic context */
5458 if (cfg->rgctx_var) {
5459 g_assert (cfg->rgctx_var->opcode == OP_REGOFFSET && cfg->rgctx_var->inst_basereg == X86_EBP);
5461 x86_mov_membase_reg (code, X86_EBP, cfg->rgctx_var->inst_offset, MONO_ARCH_RGCTX_REG, 4);
5464 if (method->save_lmf) {
5465 code = emit_setup_lmf (cfg, code, cfg->lmf_var->inst_offset, cfa_offset);
5467 code = emit_push_lmf (cfg, code, cfg->lmf_var->inst_offset);
5470 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
5471 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
5473 /* load arguments allocated to register from the stack */
5474 sig = mono_method_signature (method);
5477 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
5478 inst = cfg->args [pos];
5479 if (inst->opcode == OP_REGVAR) {
5480 g_assert (need_stack_frame);
5481 x86_mov_reg_membase (code, inst->dreg, X86_EBP, inst->inst_offset, 4);
5482 if (cfg->verbose_level > 2)
5483 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
5488 cfg->code_len = code - cfg->native_code;
5490 g_assert (cfg->code_len < cfg->code_size);
5496 mono_arch_emit_epilog (MonoCompile *cfg)
5498 MonoMethod *method = cfg->method;
5499 MonoMethodSignature *sig = mono_method_signature (method);
5501 guint32 stack_to_pop;
5503 int max_epilog_size = 16;
5505 gboolean need_stack_frame = needs_stack_frame (cfg);
5507 if (cfg->method->save_lmf)
5508 max_epilog_size += 128;
5510 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
5511 cfg->code_size *= 2;
5512 cfg->native_code = mono_realloc_native_code(cfg);
5513 cfg->stat_code_reallocs++;
5516 code = cfg->native_code + cfg->code_len;
5518 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
5519 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
5521 /* the code restoring the registers must be kept in sync with OP_TAILCALL */
5524 if (method->save_lmf) {
5525 gint32 lmf_offset = cfg->lmf_var->inst_offset;
5527 gboolean supported = FALSE;
5529 if (cfg->compile_aot) {
5530 #if defined(__APPLE__) || defined(__linux__)
5533 } else if (mono_get_jit_tls_offset () != -1) {
5537 /* check if we need to restore protection of the stack after a stack overflow */
5539 if (cfg->compile_aot) {
5540 code = emit_load_aotconst (NULL, code, cfg, NULL, X86_ECX, MONO_PATCH_INFO_TLS_OFFSET, GINT_TO_POINTER (TLS_KEY_JIT_TLS));
5542 code = emit_tls_get_reg (code, X86_ECX, X86_ECX);
5544 code = mono_x86_emit_tls_get (code, X86_ECX, mono_get_jit_tls_offset ());
5547 /* we load the value in a separate instruction: this mechanism may be
5548 * used later as a safer way to do thread interruption
5550 x86_mov_reg_membase (code, X86_ECX, X86_ECX, G_STRUCT_OFFSET (MonoJitTlsData, restore_stack_prot), 4);
5551 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
5553 x86_branch8 (code, X86_CC_Z, 0, FALSE);
5554 /* note that the call trampoline will preserve eax/edx */
5555 x86_call_reg (code, X86_ECX);
5556 x86_patch (patch, code);
5558 /* FIXME: maybe save the jit tls in the prolog */
5562 code = emit_pop_lmf (cfg, code, lmf_offset);
5564 /* restore caller saved regs */
5565 if (cfg->used_int_regs & (1 << X86_EBX)) {
5566 x86_mov_reg_membase (code, X86_EBX, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebx), 4);
5569 if (cfg->used_int_regs & (1 << X86_EDI)) {
5570 x86_mov_reg_membase (code, X86_EDI, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, edi), 4);
5572 if (cfg->used_int_regs & (1 << X86_ESI)) {
5573 x86_mov_reg_membase (code, X86_ESI, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esi), 4);
5576 /* EBP is restored by LEAVE */
5578 for (i = 0; i < X86_NREG; ++i) {
5579 if ((cfg->used_int_regs & X86_CALLER_REGS & (1 << i)) && (i != X86_EBP)) {
5585 g_assert (need_stack_frame);
5586 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
5590 g_assert (need_stack_frame);
5591 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
5594 if (cfg->used_int_regs & (1 << X86_ESI)) {
5595 x86_pop_reg (code, X86_ESI);
5597 if (cfg->used_int_regs & (1 << X86_EDI)) {
5598 x86_pop_reg (code, X86_EDI);
5600 if (cfg->used_int_regs & (1 << X86_EBX)) {
5601 x86_pop_reg (code, X86_EBX);
5605 /* Load returned vtypes into registers if needed */
5606 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
5607 if (cinfo->ret.storage == ArgValuetypeInReg) {
5608 for (quad = 0; quad < 2; quad ++) {
5609 switch (cinfo->ret.pair_storage [quad]) {
5611 x86_mov_reg_membase (code, cinfo->ret.pair_regs [quad], cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), 4);
5613 case ArgOnFloatFpStack:
5614 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), FALSE);
5616 case ArgOnDoubleFpStack:
5617 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), TRUE);
5622 g_assert_not_reached ();
5627 if (need_stack_frame)
5630 if (CALLCONV_IS_STDCALL (sig)) {
5631 MonoJitArgumentInfo *arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
5633 stack_to_pop = mono_arch_get_argument_info (NULL, sig, sig->param_count, arg_info);
5634 } else if (cinfo->vtype_retaddr)
5640 g_assert (need_stack_frame);
5641 x86_ret_imm (code, stack_to_pop);
5646 cfg->code_len = code - cfg->native_code;
5648 g_assert (cfg->code_len < cfg->code_size);
5652 mono_arch_emit_exceptions (MonoCompile *cfg)
5654 MonoJumpInfo *patch_info;
5657 MonoClass *exc_classes [16];
5658 guint8 *exc_throw_start [16], *exc_throw_end [16];
5662 /* Compute needed space */
5663 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5664 if (patch_info->type == MONO_PATCH_INFO_EXC)
5669 * make sure we have enough space for exceptions
5670 * 16 is the size of two push_imm instructions and a call
5672 if (cfg->compile_aot)
5673 code_size = exc_count * 32;
5675 code_size = exc_count * 16;
5677 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
5678 cfg->code_size *= 2;
5679 cfg->native_code = mono_realloc_native_code(cfg);
5680 cfg->stat_code_reallocs++;
5683 code = cfg->native_code + cfg->code_len;
5686 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5687 switch (patch_info->type) {
5688 case MONO_PATCH_INFO_EXC: {
5689 MonoClass *exc_class;
5693 x86_patch (patch_info->ip.i + cfg->native_code, code);
5695 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
5696 g_assert (exc_class);
5697 throw_ip = patch_info->ip.i;
5699 /* Find a throw sequence for the same exception class */
5700 for (i = 0; i < nthrows; ++i)
5701 if (exc_classes [i] == exc_class)
5704 x86_push_imm (code, (exc_throw_end [i] - cfg->native_code) - throw_ip);
5705 x86_jump_code (code, exc_throw_start [i]);
5706 patch_info->type = MONO_PATCH_INFO_NONE;
5711 /* Compute size of code following the push <OFFSET> */
5712 #if defined(__default_codegen__)
5714 #elif defined(__native_client_codegen__)
5715 code = mono_nacl_align (code);
5716 size = kNaClAlignment;
5718 /*This is aligned to 16 bytes by the callee. This way we save a few bytes here.*/
5720 if ((code - cfg->native_code) - throw_ip < 126 - size) {
5721 /* Use the shorter form */
5723 x86_push_imm (code, 0);
5727 x86_push_imm (code, 0xf0f0f0f0);
5732 exc_classes [nthrows] = exc_class;
5733 exc_throw_start [nthrows] = code;
5736 x86_push_imm (code, exc_class->type_token - MONO_TOKEN_TYPE_DEF);
5737 patch_info->data.name = "mono_arch_throw_corlib_exception";
5738 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
5739 patch_info->ip.i = code - cfg->native_code;
5740 x86_call_code (code, 0);
5741 x86_push_imm (buf, (code - cfg->native_code) - throw_ip);
5746 exc_throw_end [nthrows] = code;
5758 cfg->code_len = code - cfg->native_code;
5760 g_assert (cfg->code_len < cfg->code_size);
5764 mono_arch_flush_icache (guint8 *code, gint size)
5770 mono_arch_flush_register_windows (void)
5775 mono_arch_is_inst_imm (gint64 imm)
5781 mono_arch_finish_init (void)
5783 if (!g_getenv ("MONO_NO_TLS")) {
5786 * We need to init this multiple times, since when we are first called, the key might not
5787 * be initialized yet.
5789 jit_tls_offset = mono_get_jit_tls_key ();
5791 /* Only 64 tls entries can be accessed using inline code */
5792 if (jit_tls_offset >= 64)
5793 jit_tls_offset = -1;
5796 optimize_for_xen = access ("/proc/xen", F_OK) == 0;
5798 lmf_addr_tls_offset = mono_get_lmf_addr_tls_offset ();
5804 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
5808 #ifdef MONO_ARCH_HAVE_IMT
5810 // Linear handler, the bsearch head compare is shorter
5811 //[2 + 4] x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
5812 //[1 + 1] x86_branch8(inst,cond,imm,is_signed)
5813 // x86_patch(ins,target)
5814 //[1 + 5] x86_jump_mem(inst,mem)
5817 #if defined(__default_codegen__)
5818 #define BR_SMALL_SIZE 2
5819 #define BR_LARGE_SIZE 5
5820 #elif defined(__native_client_codegen__)
5821 /* I suspect the size calculation below is actually incorrect. */
5822 /* TODO: fix the calculation that uses these sizes. */
5823 #define BR_SMALL_SIZE 16
5824 #define BR_LARGE_SIZE 12
5825 #endif /*__native_client_codegen__*/
5826 #define JUMP_IMM_SIZE 6
5827 #define ENABLE_WRONG_METHOD_CHECK 0
5831 imt_branch_distance (MonoIMTCheckItem **imt_entries, int start, int target)
5833 int i, distance = 0;
5834 for (i = start; i < target; ++i)
5835 distance += imt_entries [i]->chunk_size;
5840 * LOCKING: called with the domain lock held
5843 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
5844 gpointer fail_tramp)
5848 guint8 *code, *start;
5850 for (i = 0; i < count; ++i) {
5851 MonoIMTCheckItem *item = imt_entries [i];
5852 if (item->is_equals) {
5853 if (item->check_target_idx) {
5854 if (!item->compare_done)
5855 item->chunk_size += CMP_SIZE;
5856 item->chunk_size += BR_SMALL_SIZE + JUMP_IMM_SIZE;
5859 item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + JUMP_IMM_SIZE * 2;
5861 item->chunk_size += JUMP_IMM_SIZE;
5862 #if ENABLE_WRONG_METHOD_CHECK
5863 item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
5868 item->chunk_size += CMP_SIZE + BR_LARGE_SIZE;
5869 imt_entries [item->check_target_idx]->compare_done = TRUE;
5871 size += item->chunk_size;
5873 #if defined(__native_client__) && defined(__native_client_codegen__)
5874 /* In Native Client, we don't re-use thunks, allocate from the */
5875 /* normal code manager paths. */
5876 size = NACL_BUNDLE_ALIGN_UP (size);
5877 code = mono_domain_code_reserve (domain, size);
5880 code = mono_method_alloc_generic_virtual_thunk (domain, size);
5882 code = mono_domain_code_reserve (domain, size);
5885 for (i = 0; i < count; ++i) {
5886 MonoIMTCheckItem *item = imt_entries [i];
5887 item->code_target = code;
5888 if (item->is_equals) {
5889 if (item->check_target_idx) {
5890 if (!item->compare_done)
5891 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5892 item->jmp_code = code;
5893 x86_branch8 (code, X86_CC_NE, 0, FALSE);
5894 if (item->has_target_code)
5895 x86_jump_code (code, item->value.target_code);
5897 x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
5900 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5901 item->jmp_code = code;
5902 x86_branch8 (code, X86_CC_NE, 0, FALSE);
5903 if (item->has_target_code)
5904 x86_jump_code (code, item->value.target_code);
5906 x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
5907 x86_patch (item->jmp_code, code);
5908 x86_jump_code (code, fail_tramp);
5909 item->jmp_code = NULL;
5911 /* enable the commented code to assert on wrong method */
5912 #if ENABLE_WRONG_METHOD_CHECK
5913 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5914 item->jmp_code = code;
5915 x86_branch8 (code, X86_CC_NE, 0, FALSE);
5917 if (item->has_target_code)
5918 x86_jump_code (code, item->value.target_code);
5920 x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
5921 #if ENABLE_WRONG_METHOD_CHECK
5922 x86_patch (item->jmp_code, code);
5923 x86_breakpoint (code);
5924 item->jmp_code = NULL;
5929 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5930 item->jmp_code = code;
5931 if (x86_is_imm8 (imt_branch_distance (imt_entries, i, item->check_target_idx)))
5932 x86_branch8 (code, X86_CC_GE, 0, FALSE);
5934 x86_branch32 (code, X86_CC_GE, 0, FALSE);
5937 /* patch the branches to get to the target items */
5938 for (i = 0; i < count; ++i) {
5939 MonoIMTCheckItem *item = imt_entries [i];
5940 if (item->jmp_code) {
5941 if (item->check_target_idx) {
5942 x86_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
5948 mono_stats.imt_thunks_size += code - start;
5949 g_assert (code - start <= size);
5953 char *buff = g_strdup_printf ("thunk_for_class_%s_%s_entries_%d", vtable->klass->name_space, vtable->klass->name, count);
5954 mono_disassemble_code (NULL, (guint8*)start, code - start, buff);
5958 if (mono_jit_map_is_enabled ()) {
5961 buff = g_strdup_printf ("imt_%s_%s_entries_%d", vtable->klass->name_space, vtable->klass->name, count);
5963 buff = g_strdup_printf ("imt_thunk_entries_%d", count);
5964 mono_emit_jit_tramp (start, code - start, buff);
5968 nacl_domain_code_validate (domain, &start, size, &code);
5974 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
5976 return (MonoMethod*) regs [MONO_ARCH_IMT_REG];
5981 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
5983 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
5987 mono_arch_get_cie_program (void)
5991 mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
5992 mono_add_unwind_op_offset (l, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
5998 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
6000 MonoInst *ins = NULL;
6003 if (cmethod->klass == mono_defaults.math_class) {
6004 if (strcmp (cmethod->name, "Sin") == 0) {
6006 } else if (strcmp (cmethod->name, "Cos") == 0) {
6008 } else if (strcmp (cmethod->name, "Tan") == 0) {
6010 } else if (strcmp (cmethod->name, "Atan") == 0) {
6012 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
6014 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
6016 } else if (strcmp (cmethod->name, "Round") == 0 && fsig->param_count == 1 && fsig->params [0]->type == MONO_TYPE_R8) {
6021 MONO_INST_NEW (cfg, ins, opcode);
6022 ins->type = STACK_R8;
6023 ins->dreg = mono_alloc_freg (cfg);
6024 ins->sreg1 = args [0]->dreg;
6025 MONO_ADD_INS (cfg->cbb, ins);
6028 if (cfg->opt & MONO_OPT_CMOV) {
6031 if (strcmp (cmethod->name, "Min") == 0) {
6032 if (fsig->params [0]->type == MONO_TYPE_I4)
6034 } else if (strcmp (cmethod->name, "Max") == 0) {
6035 if (fsig->params [0]->type == MONO_TYPE_I4)
6040 MONO_INST_NEW (cfg, ins, opcode);
6041 ins->type = STACK_I4;
6042 ins->dreg = mono_alloc_ireg (cfg);
6043 ins->sreg1 = args [0]->dreg;
6044 ins->sreg2 = args [1]->dreg;
6045 MONO_ADD_INS (cfg->cbb, ins);
6050 /* OP_FREM is not IEEE compatible */
6051 else if (strcmp (cmethod->name, "IEEERemainder") == 0) {
6052 MONO_INST_NEW (cfg, ins, OP_FREM);
6053 ins->inst_i0 = args [0];
6054 ins->inst_i1 = args [1];
6063 mono_arch_print_tree (MonoInst *tree, int arity)
6069 mono_arch_get_patch_offset (guint8 *code)
6071 if ((code [0] == 0x8b) && (x86_modrm_mod (code [1]) == 0x2))
6073 else if (code [0] == 0xba)
6075 else if (code [0] == 0x68)
6078 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x6))
6079 /* push <OFFSET>(<REG>) */
6081 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x2))
6082 /* call *<OFFSET>(<REG>) */
6084 else if ((code [0] == 0xdd) || (code [0] == 0xd9))
6087 else if ((code [0] == 0x58) && (code [1] == 0x05))
6088 /* pop %eax; add <OFFSET>, %eax */
6090 else if ((code [0] >= 0x58) && (code [0] <= 0x58 + X86_NREG) && (code [1] == 0x81))
6091 /* pop <REG>; add <OFFSET>, <REG> */
6093 else if ((code [0] >= 0xb8) && (code [0] < 0xb8 + 8))
6094 /* mov <REG>, imm */
6097 g_assert_not_reached ();
6103 * mono_breakpoint_clean_code:
6105 * Copy @size bytes from @code - @offset to the buffer @buf. If the debugger inserted software
6106 * breakpoints in the original code, they are removed in the copy.
6108 * Returns TRUE if no sw breakpoint was present.
6111 mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size)
6114 gboolean can_write = TRUE;
6116 * If method_start is non-NULL we need to perform bound checks, since we access memory
6117 * at code - offset we could go before the start of the method and end up in a different
6118 * page of memory that is not mapped or read incorrect data anyway. We zero-fill the bytes
6121 if (!method_start || code - offset >= method_start) {
6122 memcpy (buf, code - offset, size);
6124 int diff = code - method_start;
6125 memset (buf, 0, size);
6126 memcpy (buf + offset - diff, method_start, diff + size - offset);
6129 for (i = 0; i < MONO_BREAKPOINT_ARRAY_SIZE; ++i) {
6130 int idx = mono_breakpoint_info_index [i];
6134 ptr = mono_breakpoint_info [idx].address;
6135 if (ptr >= code && ptr < code + size) {
6136 guint8 saved_byte = mono_breakpoint_info [idx].saved_byte;
6138 /*g_print ("patching %p with 0x%02x (was: 0x%02x)\n", ptr, saved_byte, buf [ptr - code]);*/
6139 buf [ptr - code] = saved_byte;
6146 * mono_x86_get_this_arg_offset:
6148 * Return the offset of the stack location where this is passed during a virtual
6152 mono_x86_get_this_arg_offset (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig)
6158 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
6160 guint32 esp = regs [X86_ESP];
6161 CallInfo *cinfo = NULL;
6168 * The stack looks like:
6172 * <4 pointers pushed by mono_arch_create_trampoline_code ()>
6174 res = (((MonoObject**)esp) [5 + (offset / 4)]);
6180 #define MAX_ARCH_DELEGATE_PARAMS 10
6183 get_delegate_invoke_impl (gboolean has_target, guint32 param_count, guint32 *code_len)
6185 guint8 *code, *start;
6186 int code_reserve = 64;
6189 * The stack contains:
6195 start = code = mono_global_codeman_reserve (code_reserve);
6197 /* Replace the this argument with the target */
6198 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
6199 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoDelegate, target), 4);
6200 x86_mov_membase_reg (code, X86_ESP, 4, X86_ECX, 4);
6201 x86_jump_membase (code, X86_EAX, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
6203 g_assert ((code - start) < code_reserve);
6206 /* 8 for mov_reg and jump, plus 8 for each parameter */
6207 #ifdef __native_client_codegen__
6208 /* TODO: calculate this size correctly */
6209 code_reserve = 13 + (param_count * 8) + 2 * kNaClAlignment;
6211 code_reserve = 8 + (param_count * 8);
6212 #endif /* __native_client_codegen__ */
6214 * The stack contains:
6215 * <args in reverse order>
6220 * <args in reverse order>
6223 * without unbalancing the stack.
6224 * So move each arg up a spot in the stack (overwriting un-needed 'this' arg)
6225 * and leaving original spot of first arg as placeholder in stack so
6226 * when callee pops stack everything works.
6229 start = code = mono_global_codeman_reserve (code_reserve);
6231 /* store delegate for access to method_ptr */
6232 x86_mov_reg_membase (code, X86_ECX, X86_ESP, 4, 4);
6235 for (i = 0; i < param_count; ++i) {
6236 x86_mov_reg_membase (code, X86_EAX, X86_ESP, (i+2)*4, 4);
6237 x86_mov_membase_reg (code, X86_ESP, (i+1)*4, X86_EAX, 4);
6240 x86_jump_membase (code, X86_ECX, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
6242 g_assert ((code - start) < code_reserve);
6245 nacl_global_codeman_validate(&start, code_reserve, &code);
6246 mono_debug_add_delegate_trampoline (start, code - start);
6249 *code_len = code - start;
6251 if (mono_jit_map_is_enabled ()) {
6254 buff = (char*)"delegate_invoke_has_target";
6256 buff = g_strdup_printf ("delegate_invoke_no_target_%d", param_count);
6257 mono_emit_jit_tramp (start, code - start, buff);
6266 mono_arch_get_delegate_invoke_impls (void)
6274 code = get_delegate_invoke_impl (TRUE, 0, &code_len);
6275 res = g_slist_prepend (res, mono_tramp_info_create ("delegate_invoke_impl_has_target", code, code_len, NULL, NULL));
6277 for (i = 0; i < MAX_ARCH_DELEGATE_PARAMS; ++i) {
6278 code = get_delegate_invoke_impl (FALSE, i, &code_len);
6279 tramp_name = g_strdup_printf ("delegate_invoke_impl_target_%d", i);
6280 res = g_slist_prepend (res, mono_tramp_info_create (tramp_name, code, code_len, NULL, NULL));
6281 g_free (tramp_name);
6288 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
6290 guint8 *code, *start;
6292 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
6295 /* FIXME: Support more cases */
6296 if (MONO_TYPE_ISSTRUCT (sig->ret))
6300 * The stack contains:
6306 static guint8* cached = NULL;
6311 start = mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
6313 start = get_delegate_invoke_impl (TRUE, 0, NULL);
6315 mono_memory_barrier ();
6319 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
6322 for (i = 0; i < sig->param_count; ++i)
6323 if (!mono_is_regsize_var (sig->params [i]))
6326 code = cache [sig->param_count];
6330 if (mono_aot_only) {
6331 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
6332 start = mono_aot_get_trampoline (name);
6335 start = get_delegate_invoke_impl (FALSE, sig->param_count, NULL);
6338 mono_memory_barrier ();
6340 cache [sig->param_count] = start;
6347 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
6350 case X86_EAX: return ctx->eax;
6351 case X86_EBX: return ctx->ebx;
6352 case X86_ECX: return ctx->ecx;
6353 case X86_EDX: return ctx->edx;
6354 case X86_ESP: return ctx->esp;
6355 case X86_EBP: return ctx->ebp;
6356 case X86_ESI: return ctx->esi;
6357 case X86_EDI: return ctx->edi;
6359 g_assert_not_reached ();
6365 mono_arch_context_set_int_reg (MonoContext *ctx, int reg, mgreg_t val)
6393 g_assert_not_reached ();
6397 #ifdef MONO_ARCH_SIMD_INTRINSICS
6400 get_float_to_x_spill_area (MonoCompile *cfg)
6402 if (!cfg->fconv_to_r8_x_var) {
6403 cfg->fconv_to_r8_x_var = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
6404 cfg->fconv_to_r8_x_var->flags |= MONO_INST_VOLATILE; /*FIXME, use the don't regalloc flag*/
6406 return cfg->fconv_to_r8_x_var;
6410 * Convert all fconv opts that MONO_OPT_SSE2 would get wrong.
6413 mono_arch_decompose_opts (MonoCompile *cfg, MonoInst *ins)
6416 int dreg, src_opcode;
6418 if (!(cfg->opt & MONO_OPT_SSE2) || !(cfg->opt & MONO_OPT_SIMD) || COMPILE_LLVM (cfg))
6421 switch (src_opcode = ins->opcode) {
6422 case OP_FCONV_TO_I1:
6423 case OP_FCONV_TO_U1:
6424 case OP_FCONV_TO_I2:
6425 case OP_FCONV_TO_U2:
6426 case OP_FCONV_TO_I4:
6433 /* dreg is the IREG and sreg1 is the FREG */
6434 MONO_INST_NEW (cfg, fconv, OP_FCONV_TO_R8_X);
6435 fconv->klass = NULL; /*FIXME, what can I use here as the Mono.Simd lib might not be loaded yet*/
6436 fconv->sreg1 = ins->sreg1;
6437 fconv->dreg = mono_alloc_ireg (cfg);
6438 fconv->type = STACK_VTYPE;
6439 fconv->backend.spill_var = get_float_to_x_spill_area (cfg);
6441 mono_bblock_insert_before_ins (cfg->cbb, ins, fconv);
6445 ins->opcode = OP_XCONV_R8_TO_I4;
6447 ins->klass = mono_defaults.int32_class;
6448 ins->sreg1 = fconv->dreg;
6450 ins->type = STACK_I4;
6451 ins->backend.source_opcode = src_opcode;
6454 #endif /* #ifdef MONO_ARCH_SIMD_INTRINSICS */
6457 mono_arch_decompose_long_opts (MonoCompile *cfg, MonoInst *long_ins)
6462 if (long_ins->opcode == OP_LNEG) {
6464 MONO_EMIT_NEW_UNALU (cfg, OP_INEG, ins->dreg + 1, ins->sreg1 + 1);
6465 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADC_IMM, ins->dreg + 2, ins->sreg1 + 2, 0);
6466 MONO_EMIT_NEW_UNALU (cfg, OP_INEG, ins->dreg + 2, ins->dreg + 2);
6471 #ifdef MONO_ARCH_SIMD_INTRINSICS
6473 if (!(cfg->opt & MONO_OPT_SIMD))
6476 /*TODO move this to simd-intrinsic.c once we support sse 4.1 dword extractors since we need the runtime caps info */
6477 switch (long_ins->opcode) {
6479 vreg = long_ins->sreg1;
6481 if (long_ins->inst_c0) {
6482 MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
6483 ins->klass = long_ins->klass;
6484 ins->sreg1 = long_ins->sreg1;
6486 ins->type = STACK_VTYPE;
6487 ins->dreg = vreg = alloc_ireg (cfg);
6488 MONO_ADD_INS (cfg->cbb, ins);
6491 MONO_INST_NEW (cfg, ins, OP_EXTRACT_I4);
6492 ins->klass = mono_defaults.int32_class;
6494 ins->type = STACK_I4;
6495 ins->dreg = long_ins->dreg + 1;
6496 MONO_ADD_INS (cfg->cbb, ins);
6498 MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
6499 ins->klass = long_ins->klass;
6500 ins->sreg1 = long_ins->sreg1;
6501 ins->inst_c0 = long_ins->inst_c0 ? 3 : 1;
6502 ins->type = STACK_VTYPE;
6503 ins->dreg = vreg = alloc_ireg (cfg);
6504 MONO_ADD_INS (cfg->cbb, ins);
6506 MONO_INST_NEW (cfg, ins, OP_EXTRACT_I4);
6507 ins->klass = mono_defaults.int32_class;
6509 ins->type = STACK_I4;
6510 ins->dreg = long_ins->dreg + 2;
6511 MONO_ADD_INS (cfg->cbb, ins);
6513 long_ins->opcode = OP_NOP;
6515 case OP_INSERTX_I8_SLOW:
6516 MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
6517 ins->dreg = long_ins->dreg;
6518 ins->sreg1 = long_ins->dreg;
6519 ins->sreg2 = long_ins->sreg2 + 1;
6520 ins->inst_c0 = long_ins->inst_c0 * 2;
6521 MONO_ADD_INS (cfg->cbb, ins);
6523 MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
6524 ins->dreg = long_ins->dreg;
6525 ins->sreg1 = long_ins->dreg;
6526 ins->sreg2 = long_ins->sreg2 + 2;
6527 ins->inst_c0 = long_ins->inst_c0 * 2 + 1;
6528 MONO_ADD_INS (cfg->cbb, ins);
6530 long_ins->opcode = OP_NOP;
6533 MONO_INST_NEW (cfg, ins, OP_ICONV_TO_X);
6534 ins->dreg = long_ins->dreg;
6535 ins->sreg1 = long_ins->sreg1 + 1;
6536 ins->klass = long_ins->klass;
6537 ins->type = STACK_VTYPE;
6538 MONO_ADD_INS (cfg->cbb, ins);
6540 MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
6541 ins->dreg = long_ins->dreg;
6542 ins->sreg1 = long_ins->dreg;
6543 ins->sreg2 = long_ins->sreg1 + 2;
6545 ins->klass = long_ins->klass;
6546 ins->type = STACK_VTYPE;
6547 MONO_ADD_INS (cfg->cbb, ins);
6549 MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
6550 ins->dreg = long_ins->dreg;
6551 ins->sreg1 = long_ins->dreg;;
6552 ins->inst_c0 = 0x44; /*Magic number for swizzling (X,Y,X,Y)*/
6553 ins->klass = long_ins->klass;
6554 ins->type = STACK_VTYPE;
6555 MONO_ADD_INS (cfg->cbb, ins);
6557 long_ins->opcode = OP_NOP;
6560 #endif /* MONO_ARCH_SIMD_INTRINSICS */
6563 /*MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD*/
6565 mono_arch_install_handler_block_guard (MonoJitInfo *ji, MonoJitExceptionInfo *clause, MonoContext *ctx, gpointer new_value)
6568 gpointer *sp, old_value;
6570 const unsigned char *handler;
6572 /*Decode the first instruction to figure out where did we store the spvar*/
6573 /*Our jit MUST generate the following:
6575 Which is encoded as: 0x89 mod_rm.
6576 mod_rm (esp, ebp, imm) which can be: (imm will never be zero)
6577 mod (reg + imm8): 01 reg(esp): 100 rm(ebp): 101 -> 01100101 (0x65)
6578 mod (reg + imm32): 10 reg(esp): 100 rm(ebp): 101 -> 10100101 (0xA5)
6580 handler = clause->handler_start;
6582 if (*handler != 0x89)
6587 if (*handler == 0x65)
6588 offset = *(signed char*)(handler + 1);
6589 else if (*handler == 0xA5)
6590 offset = *(int*)(handler + 1);
6595 bp = MONO_CONTEXT_GET_BP (ctx);
6596 sp = *(gpointer*)(bp + offset);
6599 if (old_value < ji->code_start || (char*)old_value > ((char*)ji->code_start + ji->code_size))
6608 * mono_aot_emit_load_got_addr:
6610 * Emit code to load the got address.
6611 * On x86, the result is placed into EBX.
6614 mono_arch_emit_load_got_addr (guint8 *start, guint8 *code, MonoCompile *cfg, MonoJumpInfo **ji)
6616 x86_call_imm (code, 0);
6618 * The patch needs to point to the pop, since the GOT offset needs
6619 * to be added to that address.
6622 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GOT_OFFSET, NULL);
6624 *ji = mono_patch_info_list_prepend (*ji, code - start, MONO_PATCH_INFO_GOT_OFFSET, NULL);
6625 x86_pop_reg (code, MONO_ARCH_GOT_REG);
6626 x86_alu_reg_imm (code, X86_ADD, MONO_ARCH_GOT_REG, 0xf0f0f0f0);
6632 emit_load_aotconst (guint8 *start, guint8 *code, MonoCompile *cfg, MonoJumpInfo **ji, int dreg, int tramp_type, gconstpointer target)
6635 mono_add_patch_info (cfg, code - cfg->native_code, tramp_type, target);
6637 g_assert_not_reached ();
6638 x86_mov_reg_membase (code, dreg, MONO_ARCH_GOT_REG, 0xf0f0f0f0, 4);
6643 * mono_arch_emit_load_aotconst:
6645 * Emit code to load the contents of the GOT slot identified by TRAMP_TYPE and
6646 * TARGET from the mscorlib GOT in full-aot code.
6647 * On x86, the GOT address is assumed to be in EBX, and the result is placed into
6651 mono_arch_emit_load_aotconst (guint8 *start, guint8 *code, MonoJumpInfo **ji, int tramp_type, gconstpointer target)
6653 /* Load the mscorlib got address */
6654 x86_mov_reg_membase (code, X86_EAX, MONO_ARCH_GOT_REG, sizeof (gpointer), 4);
6655 *ji = mono_patch_info_list_prepend (*ji, code - start, tramp_type, target);
6656 /* arch_emit_got_access () patches this */
6657 x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0xf0f0f0f0, 4);
6662 /* Can't put this into mini-x86.h */
6664 mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot);
6667 mono_arch_get_trampolines (gboolean aot)
6669 MonoTrampInfo *info;
6670 GSList *tramps = NULL;
6672 mono_x86_get_signal_exception_trampoline (&info, aot);
6674 tramps = g_slist_append (tramps, info);
6681 #define DBG_SIGNAL SIGBUS
6683 #define DBG_SIGNAL SIGSEGV
6686 /* Soft Debug support */
6687 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
6690 * mono_arch_set_breakpoint:
6692 * Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
6693 * The location should contain code emitted by OP_SEQ_POINT.
6696 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
6701 * In production, we will use int3 (has to fix the size in the md
6702 * file). But that could confuse gdb, so during development, we emit a SIGSEGV
6705 g_assert (code [0] == 0x90);
6706 x86_alu_reg_mem (code, X86_CMP, X86_EAX, (guint32)bp_trigger_page);
6710 * mono_arch_clear_breakpoint:
6712 * Clear the breakpoint at IP.
6715 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
6720 for (i = 0; i < 6; ++i)
6725 * mono_arch_start_single_stepping:
6727 * Start single stepping.
6730 mono_arch_start_single_stepping (void)
6732 mono_mprotect (ss_trigger_page, mono_pagesize (), 0);
6736 * mono_arch_stop_single_stepping:
6738 * Stop single stepping.
6741 mono_arch_stop_single_stepping (void)
6743 mono_mprotect (ss_trigger_page, mono_pagesize (), MONO_MMAP_READ);
6747 * mono_arch_is_single_step_event:
6749 * Return whenever the machine state in SIGCTX corresponds to a single
6753 mono_arch_is_single_step_event (void *info, void *sigctx)
6756 EXCEPTION_RECORD* einfo = ((EXCEPTION_POINTERS*)info)->ExceptionRecord; /* Sometimes the address is off by 4 */
6758 if (((gpointer)einfo->ExceptionInformation[1] >= ss_trigger_page && (guint8*)einfo->ExceptionInformation[1] <= (guint8*)ss_trigger_page + 128))
6763 siginfo_t* sinfo = (siginfo_t*) info;
6764 /* Sometimes the address is off by 4 */
6765 if (sinfo->si_signo == DBG_SIGNAL && (sinfo->si_addr >= ss_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)ss_trigger_page + 128))
6773 mono_arch_is_breakpoint_event (void *info, void *sigctx)
6776 EXCEPTION_RECORD* einfo = ((EXCEPTION_POINTERS*)info)->ExceptionRecord; /* Sometimes the address is off by 4 */
6777 if (((gpointer)einfo->ExceptionInformation[1] >= bp_trigger_page && (guint8*)einfo->ExceptionInformation[1] <= (guint8*)bp_trigger_page + 128))
6782 siginfo_t* sinfo = (siginfo_t*)info;
6783 /* Sometimes the address is off by 4 */
6784 if (sinfo->si_signo == DBG_SIGNAL && (sinfo->si_addr >= bp_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)bp_trigger_page + 128))
6791 #define BREAKPOINT_SIZE 6
6794 * mono_arch_skip_breakpoint:
6796 * See mini-amd64.c for docs.
6799 mono_arch_skip_breakpoint (MonoContext *ctx, MonoJitInfo *ji)
6801 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + BREAKPOINT_SIZE);
6805 * mono_arch_skip_single_step:
6807 * See mini-amd64.c for docs.
6810 mono_arch_skip_single_step (MonoContext *ctx)
6812 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 6);
6816 * mono_arch_get_seq_point_info:
6818 * See mini-amd64.c for docs.
6821 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
6828 mono_arch_init_lmf_ext (MonoLMFExt *ext, gpointer prev_lmf)
6830 ext->lmf.previous_lmf = (gsize)prev_lmf;
6831 /* Mark that this is a MonoLMFExt */
6832 ext->lmf.previous_lmf = (gsize)(gpointer)(((gssize)ext->lmf.previous_lmf) | 2);
6833 ext->lmf.ebp = (gssize)ext;
6838 #if defined(MONOTOUCH) || defined(MONO_EXTENSIONS)
6840 #include "../../../mono-extensions/mono/mini/mini-x86-gsharedvt.c"
6842 #endif /* !MONOTOUCH */