3 * x86 backend for the Mono code generator
6 * Paolo Molaro (lupus@ximian.com)
7 * Dietmar Maurer (dietmar@ximian.com)
10 * Copyright 2003 Ximian, Inc.
11 * Copyright 2003-2011 Novell Inc.
12 * Copyright 2011 Xamarin Inc.
13 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
22 #include <mono/metadata/abi-details.h>
23 #include <mono/metadata/appdomain.h>
24 #include <mono/metadata/debug-helpers.h>
25 #include <mono/metadata/threads.h>
26 #include <mono/metadata/profiler-private.h>
27 #include <mono/metadata/mono-debug.h>
28 #include <mono/metadata/gc-internals.h>
29 #include <mono/utils/mono-math.h>
30 #include <mono/utils/mono-counters.h>
31 #include <mono/utils/mono-mmap.h>
32 #include <mono/utils/mono-memory-model.h>
33 #include <mono/utils/mono-hwcap.h>
34 #include <mono/utils/mono-threads.h>
44 static gboolean optimize_for_xen = TRUE;
46 #define optimize_for_xen 0
50 /* The single step trampoline */
51 static gpointer ss_trampoline;
53 /* The breakpoint trampoline */
54 static gpointer bp_trampoline;
56 /* This mutex protects architecture specific caches */
57 #define mono_mini_arch_lock() mono_os_mutex_lock (&mini_arch_mutex)
58 #define mono_mini_arch_unlock() mono_os_mutex_unlock (&mini_arch_mutex)
59 static mono_mutex_t mini_arch_mutex;
61 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
66 /* Under windows, the default pinvoke calling convention is stdcall */
67 #define CALLCONV_IS_STDCALL(sig) ((sig)->pinvoke && ((sig)->call_convention == MONO_CALL_STDCALL || (sig)->call_convention == MONO_CALL_DEFAULT || (sig)->call_convention == MONO_CALL_THISCALL))
69 #define CALLCONV_IS_STDCALL(sig) ((sig)->pinvoke && ((sig)->call_convention == MONO_CALL_STDCALL || (sig)->call_convention == MONO_CALL_THISCALL))
72 #define X86_IS_CALLEE_SAVED_REG(reg) (((reg) == X86_EBX) || ((reg) == X86_EDI) || ((reg) == X86_ESI))
74 #define OP_SEQ_POINT_BP_OFFSET 7
77 emit_load_aotconst (guint8 *start, guint8 *code, MonoCompile *cfg, MonoJumpInfo **ji, int dreg, int tramp_type, gconstpointer target);
80 mono_arch_regname (int reg)
83 case X86_EAX: return "%eax";
84 case X86_EBX: return "%ebx";
85 case X86_ECX: return "%ecx";
86 case X86_EDX: return "%edx";
87 case X86_ESP: return "%esp";
88 case X86_EBP: return "%ebp";
89 case X86_EDI: return "%edi";
90 case X86_ESI: return "%esi";
96 mono_arch_fregname (int reg)
121 mono_arch_xregname (int reg)
146 mono_x86_patch (unsigned char* code, gpointer target)
148 x86_patch (code, (unsigned char*)target);
151 #define FLOAT_PARAM_REGS 0
153 static const guint32 thiscall_param_regs [] = { X86_ECX, X86_NREG };
155 static const guint32 *callconv_param_regs(MonoMethodSignature *sig)
160 switch (sig->call_convention) {
161 case MONO_CALL_THISCALL:
162 return thiscall_param_regs;
168 #if defined(TARGET_WIN32) || defined(__APPLE__) || defined(__FreeBSD__)
169 #define SMALL_STRUCTS_IN_REGS
170 static X86_Reg_No return_regs [] = { X86_EAX, X86_EDX };
174 add_general (guint32 *gr, const guint32 *param_regs, guint32 *stack_size, ArgInfo *ainfo)
176 ainfo->offset = *stack_size;
178 if (!param_regs || param_regs [*gr] == X86_NREG) {
179 ainfo->storage = ArgOnStack;
181 (*stack_size) += sizeof (gpointer);
184 ainfo->storage = ArgInIReg;
185 ainfo->reg = param_regs [*gr];
191 add_general_pair (guint32 *gr, const guint32 *param_regs , guint32 *stack_size, ArgInfo *ainfo)
193 ainfo->offset = *stack_size;
195 g_assert(!param_regs || param_regs[*gr] == X86_NREG);
197 ainfo->storage = ArgOnStack;
198 (*stack_size) += sizeof (gpointer) * 2;
203 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
205 ainfo->offset = *stack_size;
207 if (*gr >= FLOAT_PARAM_REGS) {
208 ainfo->storage = ArgOnStack;
209 (*stack_size) += is_double ? 8 : 4;
210 ainfo->nslots = is_double ? 2 : 1;
213 /* A double register */
215 ainfo->storage = ArgInDoubleSSEReg;
217 ainfo->storage = ArgInFloatSSEReg;
225 add_valuetype (MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
227 guint32 *gr, const guint32 *param_regs, guint32 *fr, guint32 *stack_size)
232 klass = mono_class_from_mono_type (type);
233 size = mini_type_stack_size_full (&klass->byval_arg, NULL, sig->pinvoke);
235 #if defined(TARGET_WIN32)
237 * Standard C and C++ doesn't allow empty structs, empty structs will always have a size of 1 byte.
238 * GCC have an extension to allow empty structs, https://gcc.gnu.org/onlinedocs/gcc/Empty-Structures.html.
239 * This cause a little dilemma since runtime build using none GCC compiler will not be compatible with
240 * GCC build C libraries and the other way around. On platforms where empty structs has size of 1 byte
241 * it must be represented in call and cannot be dropped.
243 if (size == 0 && MONO_TYPE_ISSTRUCT (type) && sig->pinvoke) {
244 /* Empty structs (1 byte size) needs to be represented in a stack slot */
245 ainfo->pass_empty_struct = TRUE;
250 #ifdef SMALL_STRUCTS_IN_REGS
251 if (sig->pinvoke && is_return) {
252 MonoMarshalType *info;
254 info = mono_marshal_load_type_info (klass);
257 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
259 /* Ignore empty struct return value, if used. */
260 if (info->num_fields == 0 && ainfo->pass_empty_struct) {
261 ainfo->storage = ArgValuetypeInReg;
266 * Windows x86 ABI for returning structs of size 4 or 8 bytes (regardless of type) dictates that
267 * values are passed in EDX:EAX register pairs, https://msdn.microsoft.com/en-us/library/984x0h58.aspx.
268 * This is different compared to for example float or double return types (not in struct) that will be returned
269 * in ST(0), https://msdn.microsoft.com/en-us/library/ha59cbfz.aspx.
271 * Apples OSX x86 ABI for returning structs of size 4 or 8 bytes uses a slightly different approach.
272 * If a struct includes only one scalar value, it will be handled with the same rules as scalar values.
273 * This means that structs with one float or double will be returned in ST(0). For more details,
274 * https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/LowLevelABI/130-IA-32_Function_Calling_Conventions/IA32.html.
276 #if !defined(TARGET_WIN32)
278 /* Special case structs with only a float member */
279 if (info->num_fields == 1) {
280 int ftype = mini_get_underlying_type (info->fields [0].field->type)->type;
281 if ((info->native_size == 8) && (ftype == MONO_TYPE_R8)) {
282 ainfo->storage = ArgValuetypeInReg;
283 ainfo->pair_storage [0] = ArgOnDoubleFpStack;
286 if ((info->native_size == 4) && (ftype == MONO_TYPE_R4)) {
287 ainfo->storage = ArgValuetypeInReg;
288 ainfo->pair_storage [0] = ArgOnFloatFpStack;
294 if ((info->native_size == 1) || (info->native_size == 2) || (info->native_size == 4) || (info->native_size == 8)) {
295 ainfo->storage = ArgValuetypeInReg;
296 ainfo->pair_storage [0] = ArgInIReg;
297 ainfo->pair_regs [0] = return_regs [0];
298 if (info->native_size > 4) {
299 ainfo->pair_storage [1] = ArgInIReg;
300 ainfo->pair_regs [1] = return_regs [1];
307 if (param_regs && param_regs [*gr] != X86_NREG && !is_return) {
308 g_assert (size <= 4);
309 ainfo->storage = ArgValuetypeInReg;
310 ainfo->reg = param_regs [*gr];
315 ainfo->offset = *stack_size;
316 ainfo->storage = ArgOnStack;
317 *stack_size += ALIGN_TO (size, sizeof (gpointer));
318 ainfo->nslots = ALIGN_TO (size, sizeof (gpointer)) / sizeof (gpointer);
324 * Obtain information about a call according to the calling convention.
325 * For x86 ELF, see the "System V Application Binary Interface Intel386
326 * Architecture Processor Supplment, Fourth Edition" document for more
328 * For x86 win32, see https://msdn.microsoft.com/en-us/library/984x0h58.aspx.
331 get_call_info_internal (CallInfo *cinfo, MonoMethodSignature *sig)
333 guint32 i, gr, fr, pstart;
334 const guint32 *param_regs;
336 int n = sig->hasthis + sig->param_count;
337 guint32 stack_size = 0;
338 gboolean is_pinvoke = sig->pinvoke;
344 param_regs = callconv_param_regs(sig);
348 ret_type = mini_get_underlying_type (sig->ret);
349 switch (ret_type->type) {
359 case MONO_TYPE_FNPTR:
360 case MONO_TYPE_OBJECT:
361 cinfo->ret.storage = ArgInIReg;
362 cinfo->ret.reg = X86_EAX;
366 cinfo->ret.storage = ArgInIReg;
367 cinfo->ret.reg = X86_EAX;
368 cinfo->ret.is_pair = TRUE;
371 cinfo->ret.storage = ArgOnFloatFpStack;
374 cinfo->ret.storage = ArgOnDoubleFpStack;
376 case MONO_TYPE_GENERICINST:
377 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
378 cinfo->ret.storage = ArgInIReg;
379 cinfo->ret.reg = X86_EAX;
382 if (mini_is_gsharedvt_type (ret_type)) {
383 cinfo->ret.storage = ArgOnStack;
384 cinfo->vtype_retaddr = TRUE;
388 case MONO_TYPE_VALUETYPE:
389 case MONO_TYPE_TYPEDBYREF: {
390 guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
392 add_valuetype (sig, &cinfo->ret, ret_type, TRUE, &tmp_gr, NULL, &tmp_fr, &tmp_stacksize);
393 if (cinfo->ret.storage == ArgOnStack) {
394 cinfo->vtype_retaddr = TRUE;
395 /* The caller passes the address where the value is stored */
401 g_assert (mini_is_gsharedvt_type (ret_type));
402 cinfo->ret.storage = ArgOnStack;
403 cinfo->vtype_retaddr = TRUE;
406 cinfo->ret.storage = ArgNone;
409 g_error ("Can't handle as return value 0x%x", ret_type->type);
415 * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
416 * the first argument, allowing 'this' to be always passed in the first arg reg.
417 * Also do this if the first argument is a reference type, since virtual calls
418 * are sometimes made using calli without sig->hasthis set, like in the delegate
421 if (cinfo->vtype_retaddr && !is_pinvoke && (sig->hasthis || (sig->param_count > 0 && MONO_TYPE_IS_REFERENCE (mini_get_underlying_type (sig->params [0]))))) {
423 add_general (&gr, param_regs, &stack_size, cinfo->args + 0);
425 add_general (&gr, param_regs, &stack_size, &cinfo->args [sig->hasthis + 0]);
428 cinfo->vret_arg_offset = stack_size;
429 add_general (&gr, NULL, &stack_size, &cinfo->ret);
430 cinfo->vret_arg_index = 1;
434 add_general (&gr, param_regs, &stack_size, cinfo->args + 0);
436 if (cinfo->vtype_retaddr)
437 add_general (&gr, NULL, &stack_size, &cinfo->ret);
440 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
441 fr = FLOAT_PARAM_REGS;
443 /* Emit the signature cookie just before the implicit arguments */
444 add_general (&gr, param_regs, &stack_size, &cinfo->sig_cookie);
447 for (i = pstart; i < sig->param_count; ++i) {
448 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
451 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
452 /* We allways pass the sig cookie on the stack for simplicity */
454 * Prevent implicit arguments + the sig cookie from being passed
457 fr = FLOAT_PARAM_REGS;
459 /* Emit the signature cookie just before the implicit arguments */
460 add_general (&gr, param_regs, &stack_size, &cinfo->sig_cookie);
463 if (sig->params [i]->byref) {
464 add_general (&gr, param_regs, &stack_size, ainfo);
467 ptype = mini_get_underlying_type (sig->params [i]);
468 switch (ptype->type) {
471 add_general (&gr, param_regs, &stack_size, ainfo);
475 add_general (&gr, param_regs, &stack_size, ainfo);
479 add_general (&gr, param_regs, &stack_size, ainfo);
484 case MONO_TYPE_FNPTR:
485 case MONO_TYPE_OBJECT:
486 add_general (&gr, param_regs, &stack_size, ainfo);
488 case MONO_TYPE_GENERICINST:
489 if (!mono_type_generic_inst_is_valuetype (ptype)) {
490 add_general (&gr, param_regs, &stack_size, ainfo);
493 if (mini_is_gsharedvt_type (ptype)) {
494 /* gsharedvt arguments are passed by ref */
495 add_general (&gr, param_regs, &stack_size, ainfo);
496 g_assert (ainfo->storage == ArgOnStack);
497 ainfo->storage = ArgGSharedVt;
501 case MONO_TYPE_VALUETYPE:
502 case MONO_TYPE_TYPEDBYREF:
503 add_valuetype (sig, ainfo, ptype, FALSE, &gr, param_regs, &fr, &stack_size);
507 add_general_pair (&gr, param_regs, &stack_size, ainfo);
510 add_float (&fr, &stack_size, ainfo, FALSE);
513 add_float (&fr, &stack_size, ainfo, TRUE);
517 /* gsharedvt arguments are passed by ref */
518 g_assert (mini_is_gsharedvt_type (ptype));
519 add_general (&gr, param_regs, &stack_size, ainfo);
520 g_assert (ainfo->storage == ArgOnStack);
521 ainfo->storage = ArgGSharedVt;
524 g_error ("unexpected type 0x%x", ptype->type);
525 g_assert_not_reached ();
529 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
530 fr = FLOAT_PARAM_REGS;
532 /* Emit the signature cookie just before the implicit arguments */
533 add_general (&gr, param_regs, &stack_size, &cinfo->sig_cookie);
536 if (cinfo->vtype_retaddr) {
537 /* if the function returns a struct on stack, the called method already does a ret $0x4 */
538 cinfo->callee_stack_pop = 4;
539 } else if (CALLCONV_IS_STDCALL (sig)) {
540 /* Have to compensate for the stack space popped by the native callee */
541 cinfo->callee_stack_pop = stack_size;
544 if (mono_do_x86_stack_align && (stack_size % MONO_ARCH_FRAME_ALIGNMENT) != 0) {
545 cinfo->need_stack_align = TRUE;
546 cinfo->stack_align_amount = MONO_ARCH_FRAME_ALIGNMENT - (stack_size % MONO_ARCH_FRAME_ALIGNMENT);
547 stack_size += cinfo->stack_align_amount;
550 cinfo->stack_usage = stack_size;
551 cinfo->reg_usage = gr;
552 cinfo->freg_usage = fr;
557 get_call_info (MonoMemPool *mp, MonoMethodSignature *sig)
559 int n = sig->hasthis + sig->param_count;
563 cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
565 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
567 return get_call_info_internal (cinfo, sig);
571 * mono_arch_get_argument_info:
572 * @csig: a method signature
573 * @param_count: the number of parameters to consider
574 * @arg_info: an array to store the result infos
576 * Gathers information on parameters such as size, alignment and
577 * padding. arg_info should be large enought to hold param_count + 1 entries.
579 * Returns the size of the argument area on the stack.
580 * This should be signal safe, since it is called from
581 * mono_arch_unwind_frame ().
582 * FIXME: The metadata calls might not be signal safe.
585 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
587 int len, k, args_size = 0;
593 /* Avoid g_malloc as it is not signal safe */
594 len = sizeof (CallInfo) + (sizeof (ArgInfo) * (csig->param_count + 1));
595 cinfo = (CallInfo*)g_newa (guint8*, len);
596 memset (cinfo, 0, len);
598 cinfo = get_call_info_internal (cinfo, csig);
600 arg_info [0].offset = offset;
602 if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 0) {
603 args_size += sizeof (gpointer);
608 args_size += sizeof (gpointer);
612 if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 1 && csig->hasthis) {
613 /* Emitted after this */
614 args_size += sizeof (gpointer);
618 arg_info [0].size = args_size;
620 for (k = 0; k < param_count; k++) {
621 size = mini_type_stack_size_full (csig->params [k], &align, csig->pinvoke);
623 /* ignore alignment for now */
626 args_size += pad = (align - (args_size & (align - 1))) & (align - 1);
627 arg_info [k].pad = pad;
629 arg_info [k + 1].pad = 0;
630 arg_info [k + 1].size = size;
632 arg_info [k + 1].offset = offset;
635 if (k == 0 && cinfo->vtype_retaddr && cinfo->vret_arg_index == 1 && !csig->hasthis) {
636 /* Emitted after the first arg */
637 args_size += sizeof (gpointer);
642 if (mono_do_x86_stack_align && !CALLCONV_IS_STDCALL (csig))
643 align = MONO_ARCH_FRAME_ALIGNMENT;
646 args_size += pad = (align - (args_size & (align - 1))) & (align - 1);
647 arg_info [k].pad = pad;
653 mono_arch_tail_call_supported (MonoCompile *cfg, MonoMethodSignature *caller_sig, MonoMethodSignature *callee_sig)
655 MonoType *callee_ret;
659 if (cfg->compile_aot && !cfg->full_aot)
660 /* OP_TAILCALL doesn't work with AOT */
663 c1 = get_call_info (NULL, caller_sig);
664 c2 = get_call_info (NULL, callee_sig);
666 * Tail calls with more callee stack usage than the caller cannot be supported, since
667 * the extra stack space would be left on the stack after the tail call.
669 res = c1->stack_usage >= c2->stack_usage;
670 callee_ret = mini_get_underlying_type (callee_sig->ret);
671 if (callee_ret && MONO_TYPE_ISSTRUCT (callee_ret) && c2->ret.storage != ArgValuetypeInReg)
672 /* An address on the callee's stack is passed as the first argument */
682 * Initialize the cpu to execute managed code.
685 mono_arch_cpu_init (void)
687 /* spec compliance requires running with double precision */
691 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
692 fpcw &= ~X86_FPCW_PRECC_MASK;
693 fpcw |= X86_FPCW_PREC_DOUBLE;
694 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
695 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
697 _control87 (_PC_53, MCW_PC);
702 * Initialize architecture specific code.
705 mono_arch_init (void)
707 mono_os_mutex_init_recursive (&mini_arch_mutex);
710 bp_trampoline = mini_get_breakpoint_trampoline ();
712 mono_aot_register_jit_icall ("mono_x86_throw_exception", mono_x86_throw_exception);
713 mono_aot_register_jit_icall ("mono_x86_throw_corlib_exception", mono_x86_throw_corlib_exception);
714 #if defined(MONO_ARCH_GSHAREDVT_SUPPORTED)
715 mono_aot_register_jit_icall ("mono_x86_start_gsharedvt_call", mono_x86_start_gsharedvt_call);
720 * Cleanup architecture specific code.
723 mono_arch_cleanup (void)
725 mono_os_mutex_destroy (&mini_arch_mutex);
729 * This function returns the optimizations supported on this cpu.
732 mono_arch_cpu_optimizations (guint32 *exclude_mask)
738 if (mono_hwcap_x86_has_cmov) {
739 opts |= MONO_OPT_CMOV;
741 if (mono_hwcap_x86_has_fcmov)
742 opts |= MONO_OPT_FCMOV;
744 *exclude_mask |= MONO_OPT_FCMOV;
746 *exclude_mask |= MONO_OPT_CMOV;
749 if (mono_hwcap_x86_has_sse2)
750 opts |= MONO_OPT_SSE2;
752 *exclude_mask |= MONO_OPT_SSE2;
754 #ifdef MONO_ARCH_SIMD_INTRINSICS
755 /*SIMD intrinsics require at least SSE2.*/
756 if (!mono_hwcap_x86_has_sse2)
757 *exclude_mask |= MONO_OPT_SIMD;
764 * This function test for all SSE functions supported.
766 * Returns a bitmask corresponding to all supported versions.
770 mono_arch_cpu_enumerate_simd_versions (void)
772 guint32 sse_opts = 0;
774 if (mono_hwcap_x86_has_sse1)
775 sse_opts |= SIMD_VERSION_SSE1;
777 if (mono_hwcap_x86_has_sse2)
778 sse_opts |= SIMD_VERSION_SSE2;
780 if (mono_hwcap_x86_has_sse3)
781 sse_opts |= SIMD_VERSION_SSE3;
783 if (mono_hwcap_x86_has_ssse3)
784 sse_opts |= SIMD_VERSION_SSSE3;
786 if (mono_hwcap_x86_has_sse41)
787 sse_opts |= SIMD_VERSION_SSE41;
789 if (mono_hwcap_x86_has_sse42)
790 sse_opts |= SIMD_VERSION_SSE42;
792 if (mono_hwcap_x86_has_sse4a)
793 sse_opts |= SIMD_VERSION_SSE4a;
799 * Determine whenever the trap whose info is in SIGINFO is caused by
803 mono_arch_is_int_overflow (void *sigctx, void *info)
808 mono_sigctx_to_monoctx (sigctx, &ctx);
810 ip = (guint8*)ctx.eip;
812 if ((ip [0] == 0xf7) && (x86_modrm_mod (ip [1]) == 0x3) && (x86_modrm_reg (ip [1]) == 0x7)) {
816 switch (x86_modrm_rm (ip [1])) {
836 g_assert_not_reached ();
848 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
853 for (i = 0; i < cfg->num_varinfo; i++) {
854 MonoInst *ins = cfg->varinfo [i];
855 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
858 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
861 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
862 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
865 /* we dont allocate I1 to registers because there is no simply way to sign extend
866 * 8bit quantities in caller saved registers on x86 */
867 if (mono_is_regsize_var (ins->inst_vtype) && (ins->inst_vtype->type != MONO_TYPE_I1)) {
868 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
869 g_assert (i == vmv->idx);
870 vars = g_list_prepend (vars, vmv);
874 vars = mono_varlist_sort (cfg, vars, 0);
880 mono_arch_get_global_int_regs (MonoCompile *cfg)
884 /* we can use 3 registers for global allocation */
885 regs = g_list_prepend (regs, (gpointer)X86_EBX);
886 regs = g_list_prepend (regs, (gpointer)X86_ESI);
887 regs = g_list_prepend (regs, (gpointer)X86_EDI);
893 * mono_arch_regalloc_cost:
895 * Return the cost, in number of memory references, of the action of
896 * allocating the variable VMV into a register during global register
900 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
902 MonoInst *ins = cfg->varinfo [vmv->idx];
904 if (cfg->method->save_lmf)
905 /* The register is already saved */
906 return (ins->opcode == OP_ARG) ? 1 : 0;
908 /* push+pop+possible load if it is an argument */
909 return (ins->opcode == OP_ARG) ? 3 : 2;
913 set_needs_stack_frame (MonoCompile *cfg, gboolean flag)
915 static int inited = FALSE;
916 static int count = 0;
918 if (cfg->arch.need_stack_frame_inited) {
919 g_assert (cfg->arch.need_stack_frame == flag);
923 cfg->arch.need_stack_frame = flag;
924 cfg->arch.need_stack_frame_inited = TRUE;
930 mono_counters_register ("Could eliminate stack frame", MONO_COUNTER_INT|MONO_COUNTER_JIT, &count);
935 //g_print ("will eliminate %s.%s.%s\n", cfg->method->klass->name_space, cfg->method->klass->name, cfg->method->name);
939 needs_stack_frame (MonoCompile *cfg)
941 MonoMethodSignature *sig;
942 MonoMethodHeader *header;
943 gboolean result = FALSE;
945 #if defined(__APPLE__)
946 /*OSX requires stack frame code to have the correct alignment. */
950 if (cfg->arch.need_stack_frame_inited)
951 return cfg->arch.need_stack_frame;
953 header = cfg->header;
954 sig = mono_method_signature (cfg->method);
956 if (cfg->disable_omit_fp)
958 else if (cfg->flags & MONO_CFG_HAS_ALLOCA)
960 else if (cfg->method->save_lmf)
962 else if (cfg->stack_offset)
964 else if (cfg->param_area)
966 else if (cfg->flags & (MONO_CFG_HAS_CALLS | MONO_CFG_HAS_ALLOCA | MONO_CFG_HAS_TAIL))
968 else if (header->num_clauses)
970 else if (sig->param_count + sig->hasthis)
972 else if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
974 else if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) ||
975 (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
978 set_needs_stack_frame (cfg, result);
980 return cfg->arch.need_stack_frame;
984 * Set var information according to the calling convention. X86 version.
985 * The locals var stuff should most likely be split in another method.
988 mono_arch_allocate_vars (MonoCompile *cfg)
990 MonoMethodSignature *sig;
991 MonoMethodHeader *header;
993 guint32 locals_stack_size, locals_stack_align;
998 header = cfg->header;
999 sig = mono_method_signature (cfg->method);
1001 if (!cfg->arch.cinfo)
1002 cfg->arch.cinfo = get_call_info (cfg->mempool, sig);
1003 cinfo = (CallInfo *)cfg->arch.cinfo;
1005 cfg->frame_reg = X86_EBP;
1008 if (cfg->has_atomic_add_i4 || cfg->has_atomic_exchange_i4) {
1009 /* The opcode implementations use callee-saved regs as scratch regs by pushing and pop-ing them, but that is not async safe */
1010 cfg->used_int_regs |= (1 << X86_EBX) | (1 << X86_EDI) | (1 << X86_ESI);
1013 /* Reserve space to save LMF and caller saved registers */
1015 if (cfg->method->save_lmf) {
1016 /* The LMF var is allocated normally */
1018 if (cfg->used_int_regs & (1 << X86_EBX)) {
1022 if (cfg->used_int_regs & (1 << X86_EDI)) {
1026 if (cfg->used_int_regs & (1 << X86_ESI)) {
1031 switch (cinfo->ret.storage) {
1032 case ArgValuetypeInReg:
1033 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
1035 cfg->ret->opcode = OP_REGOFFSET;
1036 cfg->ret->inst_basereg = X86_EBP;
1037 cfg->ret->inst_offset = - offset;
1043 /* Allocate locals */
1044 offsets = mono_allocate_stack_slots (cfg, TRUE, &locals_stack_size, &locals_stack_align);
1045 if (locals_stack_size > MONO_ARCH_MAX_FRAME_SIZE) {
1046 char *mname = mono_method_full_name (cfg->method, TRUE);
1047 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Method %s stack is too big.", mname));
1051 if (locals_stack_align) {
1052 int prev_offset = offset;
1054 offset += (locals_stack_align - 1);
1055 offset &= ~(locals_stack_align - 1);
1057 while (prev_offset < offset) {
1059 mini_gc_set_slot_type_from_fp (cfg, - prev_offset, SLOT_NOREF);
1062 cfg->locals_min_stack_offset = - (offset + locals_stack_size);
1063 cfg->locals_max_stack_offset = - offset;
1065 * EBP is at alignment 8 % MONO_ARCH_FRAME_ALIGNMENT, so if we
1066 * have locals larger than 8 bytes we need to make sure that
1067 * they have the appropriate offset.
1069 if (MONO_ARCH_FRAME_ALIGNMENT > 8 && locals_stack_align > 8) {
1070 int extra_size = MONO_ARCH_FRAME_ALIGNMENT - sizeof (gpointer) * 2;
1071 offset += extra_size;
1072 locals_stack_size += extra_size;
1074 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1075 if (offsets [i] != -1) {
1076 MonoInst *inst = cfg->varinfo [i];
1077 inst->opcode = OP_REGOFFSET;
1078 inst->inst_basereg = X86_EBP;
1079 inst->inst_offset = - (offset + offsets [i]);
1080 //printf ("allocated local %d to ", i); mono_print_tree_nl (inst);
1083 offset += locals_stack_size;
1087 * Allocate arguments+return value
1090 switch (cinfo->ret.storage) {
1092 if (cfg->vret_addr) {
1094 * In the new IR, the cfg->vret_addr variable represents the
1095 * vtype return value.
1097 cfg->vret_addr->opcode = OP_REGOFFSET;
1098 cfg->vret_addr->inst_basereg = cfg->frame_reg;
1099 cfg->vret_addr->inst_offset = cinfo->ret.offset + ARGS_OFFSET;
1100 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1101 printf ("vret_addr =");
1102 mono_print_ins (cfg->vret_addr);
1105 cfg->ret->opcode = OP_REGOFFSET;
1106 cfg->ret->inst_basereg = X86_EBP;
1107 cfg->ret->inst_offset = cinfo->ret.offset + ARGS_OFFSET;
1110 case ArgValuetypeInReg:
1113 cfg->ret->opcode = OP_REGVAR;
1114 cfg->ret->inst_c0 = cinfo->ret.reg;
1115 cfg->ret->dreg = cinfo->ret.reg;
1118 case ArgOnFloatFpStack:
1119 case ArgOnDoubleFpStack:
1122 g_assert_not_reached ();
1125 if (sig->call_convention == MONO_CALL_VARARG) {
1126 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
1127 cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
1130 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1131 ArgInfo *ainfo = &cinfo->args [i];
1132 inst = cfg->args [i];
1133 if (inst->opcode != OP_REGVAR) {
1134 inst->opcode = OP_REGOFFSET;
1135 inst->inst_basereg = X86_EBP;
1136 inst->inst_offset = ainfo->offset + ARGS_OFFSET;
1140 cfg->stack_offset = offset;
1144 mono_arch_create_vars (MonoCompile *cfg)
1147 MonoMethodSignature *sig;
1150 sig = mono_method_signature (cfg->method);
1152 if (!cfg->arch.cinfo)
1153 cfg->arch.cinfo = get_call_info (cfg->mempool, sig);
1154 cinfo = (CallInfo *)cfg->arch.cinfo;
1156 sig_ret = mini_get_underlying_type (sig->ret);
1158 if (cinfo->ret.storage == ArgValuetypeInReg)
1159 cfg->ret_var_is_local = TRUE;
1160 if ((cinfo->ret.storage != ArgValuetypeInReg) && (MONO_TYPE_ISSTRUCT (sig_ret) || mini_is_gsharedvt_variable_type (sig_ret))) {
1161 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
1164 if (cfg->gen_sdb_seq_points) {
1167 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1168 ins->flags |= MONO_INST_VOLATILE;
1169 cfg->arch.ss_tramp_var = ins;
1171 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1172 ins->flags |= MONO_INST_VOLATILE;
1173 cfg->arch.bp_tramp_var = ins;
1176 if (cfg->method->save_lmf) {
1177 cfg->create_lmf_var = TRUE;
1181 cfg->arch_eh_jit_info = 1;
1185 * It is expensive to adjust esp for each individual fp argument pushed on the stack
1186 * so we try to do it just once when we have multiple fp arguments in a row.
1187 * We don't use this mechanism generally because for int arguments the generated code
1188 * is slightly bigger and new generation cpus optimize away the dependency chains
1189 * created by push instructions on the esp value.
1190 * fp_arg_setup is the first argument in the execution sequence where the esp register
1193 static G_GNUC_UNUSED int
1194 collect_fp_stack_space (MonoMethodSignature *sig, int start_arg, int *fp_arg_setup)
1199 for (; start_arg < sig->param_count; ++start_arg) {
1200 t = mini_get_underlying_type (sig->params [start_arg]);
1201 if (!t->byref && t->type == MONO_TYPE_R8) {
1202 fp_space += sizeof (double);
1203 *fp_arg_setup = start_arg;
1212 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
1214 MonoMethodSignature *tmp_sig;
1218 * mono_ArgIterator_Setup assumes the signature cookie is
1219 * passed first and all the arguments which were before it are
1220 * passed on the stack after the signature. So compensate by
1221 * passing a different signature.
1223 tmp_sig = mono_metadata_signature_dup (call->signature);
1224 tmp_sig->param_count -= call->signature->sentinelpos;
1225 tmp_sig->sentinelpos = 0;
1226 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
1228 if (cfg->compile_aot) {
1229 sig_reg = mono_alloc_ireg (cfg);
1230 MONO_EMIT_NEW_SIGNATURECONST (cfg, sig_reg, tmp_sig);
1231 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, cinfo->sig_cookie.offset, sig_reg);
1233 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, X86_ESP, cinfo->sig_cookie.offset, tmp_sig);
1239 mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
1244 LLVMCallInfo *linfo;
1245 MonoType *t, *sig_ret;
1247 n = sig->param_count + sig->hasthis;
1249 cinfo = get_call_info (cfg->mempool, sig);
1252 linfo = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
1255 * LLVM always uses the native ABI while we use our own ABI, the
1256 * only difference is the handling of vtypes:
1257 * - we only pass/receive them in registers in some cases, and only
1258 * in 1 or 2 integer registers.
1260 if (cinfo->ret.storage == ArgValuetypeInReg) {
1262 cfg->exception_message = g_strdup ("pinvoke + vtypes");
1263 cfg->disable_llvm = TRUE;
1267 cfg->exception_message = g_strdup ("vtype ret in call");
1268 cfg->disable_llvm = TRUE;
1270 linfo->ret.storage = LLVMArgVtypeInReg;
1271 for (j = 0; j < 2; ++j)
1272 linfo->ret.pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, cinfo->ret.pair_storage [j]);
1276 if (mini_type_is_vtype (sig_ret) && cinfo->ret.storage == ArgInIReg) {
1277 /* Vtype returned using a hidden argument */
1278 linfo->ret.storage = LLVMArgVtypeRetAddr;
1279 linfo->vret_arg_index = cinfo->vret_arg_index;
1282 if (mini_type_is_vtype (sig_ret) && cinfo->ret.storage != ArgInIReg) {
1284 cfg->exception_message = g_strdup ("vtype ret in call");
1285 cfg->disable_llvm = TRUE;
1288 for (i = 0; i < n; ++i) {
1289 ainfo = cinfo->args + i;
1291 if (i >= sig->hasthis)
1292 t = sig->params [i - sig->hasthis];
1294 t = &mono_defaults.int_class->byval_arg;
1296 linfo->args [i].storage = LLVMArgNone;
1298 switch (ainfo->storage) {
1300 linfo->args [i].storage = LLVMArgNormal;
1302 case ArgInDoubleSSEReg:
1303 case ArgInFloatSSEReg:
1304 linfo->args [i].storage = LLVMArgNormal;
1307 if (mini_type_is_vtype (t)) {
1308 if (mono_class_value_size (mono_class_from_mono_type (t), NULL) == 0)
1309 /* LLVM seems to allocate argument space for empty structures too */
1310 linfo->args [i].storage = LLVMArgNone;
1312 linfo->args [i].storage = LLVMArgVtypeByVal;
1314 linfo->args [i].storage = LLVMArgNormal;
1317 case ArgValuetypeInReg:
1319 cfg->exception_message = g_strdup ("pinvoke + vtypes");
1320 cfg->disable_llvm = TRUE;
1324 cfg->exception_message = g_strdup ("vtype arg");
1325 cfg->disable_llvm = TRUE;
1327 linfo->args [i].storage = LLVMArgVtypeInReg;
1328 for (j = 0; j < 2; ++j)
1329 linfo->args [i].pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, ainfo->pair_storage [j]);
1333 linfo->args [i].storage = LLVMArgGSharedVt;
1336 cfg->exception_message = g_strdup ("ainfo->storage");
1337 cfg->disable_llvm = TRUE;
1347 emit_gc_param_slot_def (MonoCompile *cfg, int sp_offset, MonoType *t)
1349 if (cfg->compute_gc_maps) {
1352 /* Needs checking if the feature will be enabled again */
1353 g_assert_not_reached ();
1355 /* On x86, the offsets are from the sp value before the start of the call sequence */
1357 t = &mono_defaults.int_class->byval_arg;
1358 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, sp_offset, t);
1363 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
1367 MonoMethodSignature *sig;
1370 int sentinelpos = 0, sp_offset = 0;
1372 sig = call->signature;
1373 n = sig->param_count + sig->hasthis;
1374 sig_ret = mini_get_underlying_type (sig->ret);
1376 cinfo = get_call_info (cfg->mempool, sig);
1377 call->call_info = cinfo;
1379 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1380 sentinelpos = sig->sentinelpos + (sig->hasthis ? 1 : 0);
1382 if (sig_ret && MONO_TYPE_ISSTRUCT (sig_ret)) {
1383 if (cinfo->ret.storage == ArgValuetypeInReg && cinfo->ret.pair_storage[0] != ArgNone ) {
1385 * Tell the JIT to use a more efficient calling convention: call using
1386 * OP_CALL, compute the result location after the call, and save the
1389 call->vret_in_reg = TRUE;
1390 #if defined(__APPLE__)
1391 if (cinfo->ret.pair_storage [0] == ArgOnDoubleFpStack || cinfo->ret.pair_storage [0] == ArgOnFloatFpStack)
1392 call->vret_in_reg_fp = TRUE;
1395 NULLIFY_INS (call->vret_var);
1399 // FIXME: Emit EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF everywhere
1401 /* Handle the case where there are no implicit arguments */
1402 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sentinelpos)) {
1403 emit_sig_cookie (cfg, call, cinfo);
1404 sp_offset = cinfo->sig_cookie.offset;
1405 emit_gc_param_slot_def (cfg, sp_offset, NULL);
1408 /* Arguments are pushed in the reverse order */
1409 for (i = n - 1; i >= 0; i --) {
1410 ArgInfo *ainfo = cinfo->args + i;
1411 MonoType *orig_type, *t;
1414 if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 1 && i == 0) {
1417 /* Push the vret arg before the first argument */
1418 MONO_INST_NEW (cfg, vtarg, OP_STORE_MEMBASE_REG);
1419 vtarg->type = STACK_MP;
1420 vtarg->inst_destbasereg = X86_ESP;
1421 vtarg->sreg1 = call->vret_var->dreg;
1422 vtarg->inst_offset = cinfo->ret.offset;
1423 MONO_ADD_INS (cfg->cbb, vtarg);
1424 emit_gc_param_slot_def (cfg, cinfo->ret.offset, NULL);
1427 if (i >= sig->hasthis)
1428 t = sig->params [i - sig->hasthis];
1430 t = &mono_defaults.int_class->byval_arg;
1432 t = mini_get_underlying_type (t);
1434 MONO_INST_NEW (cfg, arg, OP_X86_PUSH);
1436 in = call->args [i];
1437 arg->cil_code = in->cil_code;
1438 arg->sreg1 = in->dreg;
1439 arg->type = in->type;
1441 g_assert (in->dreg != -1);
1443 if (ainfo->storage == ArgGSharedVt) {
1444 arg->opcode = OP_OUTARG_VT;
1445 arg->sreg1 = in->dreg;
1446 arg->klass = in->klass;
1447 arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
1448 memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
1450 MONO_ADD_INS (cfg->cbb, arg);
1451 } else if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(t))) {
1455 g_assert (in->klass);
1457 if (t->type == MONO_TYPE_TYPEDBYREF) {
1458 size = sizeof (MonoTypedRef);
1459 align = sizeof (gpointer);
1462 size = mini_type_stack_size_full (&in->klass->byval_arg, &align, sig->pinvoke);
1465 if (size > 0 || ainfo->pass_empty_struct) {
1466 arg->opcode = OP_OUTARG_VT;
1467 arg->sreg1 = in->dreg;
1468 arg->klass = in->klass;
1469 arg->backend.size = size;
1470 arg->inst_p0 = call;
1471 arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
1472 memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
1474 MONO_ADD_INS (cfg->cbb, arg);
1475 if (ainfo->storage != ArgValuetypeInReg) {
1476 emit_gc_param_slot_def (cfg, ainfo->offset, orig_type);
1480 switch (ainfo->storage) {
1483 if (t->type == MONO_TYPE_R4) {
1484 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, X86_ESP, ainfo->offset, in->dreg);
1486 } else if (t->type == MONO_TYPE_R8) {
1487 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, X86_ESP, ainfo->offset, in->dreg);
1489 } else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_U8) {
1490 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, ainfo->offset + 4, MONO_LVREG_MS (in->dreg));
1491 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, ainfo->offset, MONO_LVREG_LS (in->dreg));
1494 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, ainfo->offset, in->dreg);
1498 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, ainfo->offset, in->dreg);
1503 arg->opcode = OP_MOVE;
1504 arg->dreg = ainfo->reg;
1505 MONO_ADD_INS (cfg->cbb, arg);
1509 g_assert_not_reached ();
1512 if (cfg->compute_gc_maps) {
1514 /* FIXME: The == STACK_OBJ check might be fragile ? */
1515 if (sig->hasthis && i == 0 && call->args [i]->type == STACK_OBJ) {
1517 if (call->need_unbox_trampoline)
1518 /* The unbox trampoline transforms this into a managed pointer */
1519 emit_gc_param_slot_def (cfg, ainfo->offset, &mono_defaults.int_class->this_arg);
1521 emit_gc_param_slot_def (cfg, ainfo->offset, &mono_defaults.object_class->byval_arg);
1523 emit_gc_param_slot_def (cfg, ainfo->offset, orig_type);
1527 for (j = 0; j < argsize; j += 4)
1528 emit_gc_param_slot_def (cfg, ainfo->offset + j, NULL);
1533 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sentinelpos)) {
1534 /* Emit the signature cookie just before the implicit arguments */
1535 emit_sig_cookie (cfg, call, cinfo);
1536 emit_gc_param_slot_def (cfg, cinfo->sig_cookie.offset, NULL);
1540 if (sig_ret && (MONO_TYPE_ISSTRUCT (sig_ret) || cinfo->vtype_retaddr)) {
1543 if (cinfo->ret.storage == ArgValuetypeInReg) {
1546 else if (cinfo->ret.storage == ArgInIReg) {
1548 /* The return address is passed in a register */
1549 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
1550 vtarg->sreg1 = call->inst.dreg;
1551 vtarg->dreg = mono_alloc_ireg (cfg);
1552 MONO_ADD_INS (cfg->cbb, vtarg);
1554 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
1555 } else if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 0) {
1556 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, cinfo->ret.offset, call->vret_var->dreg);
1557 emit_gc_param_slot_def (cfg, cinfo->ret.offset, NULL);
1561 call->stack_usage = cinfo->stack_usage;
1562 call->stack_align_amount = cinfo->stack_align_amount;
1566 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
1568 MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
1569 ArgInfo *ainfo = ins->inst_p1;
1570 int size = ins->backend.size;
1572 if (ainfo->storage == ArgValuetypeInReg) {
1573 int dreg = mono_alloc_ireg (cfg);
1576 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, dreg, src->dreg, 0);
1579 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, dreg, src->dreg, 0);
1582 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, src->dreg, 0);
1586 g_assert_not_reached ();
1588 mono_call_inst_add_outarg_reg (cfg, call, dreg, ainfo->reg, FALSE);
1591 if (cfg->gsharedvt && mini_is_gsharedvt_klass (ins->klass)) {
1593 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, ainfo->offset, src->dreg);
1594 } else if (size <= 4) {
1595 int dreg = mono_alloc_ireg (cfg);
1596 if (ainfo->pass_empty_struct) {
1597 //Pass empty struct value as 0 on platforms representing empty structs as 1 byte.
1598 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
1600 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, src->dreg, 0);
1602 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, ainfo->offset, dreg);
1603 } else if (size <= 20) {
1604 mini_emit_memcpy (cfg, X86_ESP, ainfo->offset, src->dreg, 0, size, 4);
1606 // FIXME: Code growth
1607 mini_emit_memcpy (cfg, X86_ESP, ainfo->offset, src->dreg, 0, size, 4);
1613 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
1615 MonoType *ret = mini_get_underlying_type (mono_method_signature (method)->ret);
1618 if (ret->type == MONO_TYPE_R4) {
1619 if (COMPILE_LLVM (cfg))
1620 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
1623 } else if (ret->type == MONO_TYPE_R8) {
1624 if (COMPILE_LLVM (cfg))
1625 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
1628 } else if (ret->type == MONO_TYPE_I8 || ret->type == MONO_TYPE_U8) {
1629 if (COMPILE_LLVM (cfg))
1630 MONO_EMIT_NEW_UNALU (cfg, OP_LMOVE, cfg->ret->dreg, val->dreg);
1632 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, X86_EAX, MONO_LVREG_LS (val->dreg));
1633 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, X86_EDX, MONO_LVREG_MS (val->dreg));
1639 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
1643 * Allow tracing to work with this interface (with an optional argument)
1646 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1650 g_assert (MONO_ARCH_FRAME_ALIGNMENT >= 8);
1651 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 8);
1653 /* if some args are passed in registers, we need to save them here */
1654 x86_push_reg (code, X86_EBP);
1656 if (cfg->compile_aot) {
1657 x86_push_imm (code, cfg->method);
1658 x86_mov_reg_imm (code, X86_EAX, func);
1659 x86_call_reg (code, X86_EAX);
1661 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
1662 x86_push_imm (code, cfg->method);
1663 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1664 x86_call_code (code, 0);
1666 x86_alu_reg_imm (code, X86_ADD, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT);
1680 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
1683 int arg_size = 0, stack_usage = 0, save_mode = SAVE_NONE;
1684 MonoMethod *method = cfg->method;
1685 MonoType *ret_type = mini_get_underlying_type (mono_method_signature (method)->ret);
1687 switch (ret_type->type) {
1688 case MONO_TYPE_VOID:
1689 /* special case string .ctor icall */
1690 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class) {
1691 save_mode = SAVE_EAX;
1692 stack_usage = enable_arguments ? 8 : 4;
1694 save_mode = SAVE_NONE;
1698 save_mode = SAVE_EAX_EDX;
1699 stack_usage = enable_arguments ? 16 : 8;
1703 save_mode = SAVE_FP;
1704 stack_usage = enable_arguments ? 16 : 8;
1706 case MONO_TYPE_GENERICINST:
1707 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
1708 save_mode = SAVE_EAX;
1709 stack_usage = enable_arguments ? 8 : 4;
1713 case MONO_TYPE_VALUETYPE:
1714 // FIXME: Handle SMALL_STRUCT_IN_REG here for proper alignment on darwin-x86
1715 save_mode = SAVE_STRUCT;
1716 stack_usage = enable_arguments ? 4 : 0;
1719 save_mode = SAVE_EAX;
1720 stack_usage = enable_arguments ? 8 : 4;
1724 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - stack_usage - 4);
1726 switch (save_mode) {
1728 x86_push_reg (code, X86_EDX);
1729 x86_push_reg (code, X86_EAX);
1730 if (enable_arguments) {
1731 x86_push_reg (code, X86_EDX);
1732 x86_push_reg (code, X86_EAX);
1737 x86_push_reg (code, X86_EAX);
1738 if (enable_arguments) {
1739 x86_push_reg (code, X86_EAX);
1744 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1745 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1746 if (enable_arguments) {
1747 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1748 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1753 if (enable_arguments) {
1754 x86_push_membase (code, X86_EBP, 8);
1763 if (cfg->compile_aot) {
1764 x86_push_imm (code, method);
1765 x86_mov_reg_imm (code, X86_EAX, func);
1766 x86_call_reg (code, X86_EAX);
1768 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
1769 x86_push_imm (code, method);
1770 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1771 x86_call_code (code, 0);
1774 x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
1776 switch (save_mode) {
1778 x86_pop_reg (code, X86_EAX);
1779 x86_pop_reg (code, X86_EDX);
1782 x86_pop_reg (code, X86_EAX);
1785 x86_fld_membase (code, X86_ESP, 0, TRUE);
1786 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1793 x86_alu_reg_imm (code, X86_ADD, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - stack_usage);
1798 #define EMIT_COND_BRANCH(ins,cond,sign) \
1799 if (ins->inst_true_bb->native_offset) { \
1800 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
1802 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1803 if ((cfg->opt & MONO_OPT_BRANCH) && \
1804 x86_is_imm8 (ins->inst_true_bb->max_offset - cpos)) \
1805 x86_branch8 (code, cond, 0, sign); \
1807 x86_branch32 (code, cond, 0, sign); \
1811 * Emit an exception if condition is fail and
1812 * if possible do a directly branch to target
1814 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
1816 MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
1817 if (tins == NULL) { \
1818 mono_add_patch_info (cfg, code - cfg->native_code, \
1819 MONO_PATCH_INFO_EXC, exc_name); \
1820 x86_branch32 (code, cond, 0, signed); \
1822 EMIT_COND_BRANCH (tins, cond, signed); \
1826 #define EMIT_FPCOMPARE(code) do { \
1827 x86_fcompp (code); \
1828 x86_fnstsw (code); \
1833 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data)
1835 gboolean needs_paddings = TRUE;
1837 MonoJumpInfo *jinfo = NULL;
1839 if (cfg->abs_patches) {
1840 jinfo = g_hash_table_lookup (cfg->abs_patches, data);
1841 if (jinfo && jinfo->type == MONO_PATCH_INFO_JIT_ICALL_ADDR)
1842 needs_paddings = FALSE;
1845 if (cfg->compile_aot)
1846 needs_paddings = FALSE;
1847 /*The address must be 4 bytes aligned to avoid spanning multiple cache lines.
1848 This is required for code patching to be safe on SMP machines.
1850 pad_size = (guint32)(code + 1 - cfg->native_code) & 0x3;
1851 if (needs_paddings && pad_size)
1852 x86_padding (code, 4 - pad_size);
1854 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
1855 x86_call_code (code, 0);
1860 #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)))
1863 * mono_peephole_pass_1:
1865 * Perform peephole opts which should/can be performed before local regalloc
1868 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
1872 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
1873 MonoInst *last_ins = mono_inst_prev (ins, FILTER_IL_SEQ_POINT);
1875 switch (ins->opcode) {
1878 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS)) {
1880 * X86_LEA is like ADD, but doesn't have the
1881 * sreg1==dreg restriction.
1883 ins->opcode = OP_X86_LEA_MEMBASE;
1884 ins->inst_basereg = ins->sreg1;
1885 } else if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1886 ins->opcode = OP_X86_INC_REG;
1890 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS)) {
1891 ins->opcode = OP_X86_LEA_MEMBASE;
1892 ins->inst_basereg = ins->sreg1;
1893 ins->inst_imm = -ins->inst_imm;
1894 } else if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1895 ins->opcode = OP_X86_DEC_REG;
1897 case OP_COMPARE_IMM:
1898 case OP_ICOMPARE_IMM:
1899 /* OP_COMPARE_IMM (reg, 0)
1901 * OP_X86_TEST_NULL (reg)
1904 ins->opcode = OP_X86_TEST_NULL;
1906 case OP_X86_COMPARE_MEMBASE_IMM:
1908 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1909 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
1911 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1912 * OP_COMPARE_IMM reg, imm
1914 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
1916 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
1917 ins->inst_basereg == last_ins->inst_destbasereg &&
1918 ins->inst_offset == last_ins->inst_offset) {
1919 ins->opcode = OP_COMPARE_IMM;
1920 ins->sreg1 = last_ins->sreg1;
1922 /* check if we can remove cmp reg,0 with test null */
1924 ins->opcode = OP_X86_TEST_NULL;
1928 case OP_X86_PUSH_MEMBASE:
1929 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG ||
1930 last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1931 ins->inst_basereg == last_ins->inst_destbasereg &&
1932 ins->inst_offset == last_ins->inst_offset) {
1933 ins->opcode = OP_X86_PUSH;
1934 ins->sreg1 = last_ins->sreg1;
1939 mono_peephole_ins (bb, ins);
1944 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
1948 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
1949 switch (ins->opcode) {
1951 /* reg = 0 -> XOR (reg, reg) */
1952 /* XOR sets cflags on x86, so we cant do it always */
1953 if (ins->inst_c0 == 0 && (!ins->next || (ins->next && INST_IGNORES_CFLAGS (ins->next->opcode)))) {
1956 ins->opcode = OP_IXOR;
1957 ins->sreg1 = ins->dreg;
1958 ins->sreg2 = ins->dreg;
1961 * Convert succeeding STORE_MEMBASE_IMM 0 ins to STORE_MEMBASE_REG
1962 * since it takes 3 bytes instead of 7.
1964 for (ins2 = mono_inst_next (ins, FILTER_IL_SEQ_POINT); ins2; ins2 = ins2->next) {
1965 if ((ins2->opcode == OP_STORE_MEMBASE_IMM) && (ins2->inst_imm == 0)) {
1966 ins2->opcode = OP_STORE_MEMBASE_REG;
1967 ins2->sreg1 = ins->dreg;
1969 else if ((ins2->opcode == OP_STOREI4_MEMBASE_IMM) && (ins2->inst_imm == 0)) {
1970 ins2->opcode = OP_STOREI4_MEMBASE_REG;
1971 ins2->sreg1 = ins->dreg;
1973 else if ((ins2->opcode == OP_STOREI1_MEMBASE_IMM) || (ins2->opcode == OP_STOREI2_MEMBASE_IMM)) {
1974 /* Continue iteration */
1983 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1984 ins->opcode = OP_X86_INC_REG;
1988 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1989 ins->opcode = OP_X86_DEC_REG;
1993 mono_peephole_ins (bb, ins);
1997 #define NEW_INS(cfg,ins,dest,op) do { \
1998 MONO_INST_NEW ((cfg), (dest), (op)); \
1999 (dest)->cil_code = (ins)->cil_code; \
2000 mono_bblock_insert_before_ins (bb, ins, (dest)); \
2004 * mono_arch_lowering_pass:
2006 * Converts complex opcodes into simpler ones so that each IR instruction
2007 * corresponds to one machine instruction.
2010 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
2012 MonoInst *ins, *next;
2015 * FIXME: Need to add more instructions, but the current machine
2016 * description can't model some parts of the composite instructions like
2019 MONO_BB_FOR_EACH_INS_SAFE (bb, next, ins) {
2020 switch (ins->opcode) {
2023 case OP_IDIV_UN_IMM:
2024 case OP_IREM_UN_IMM:
2026 * Keep the cases where we could generated optimized code, otherwise convert
2027 * to the non-imm variant.
2029 if ((ins->opcode == OP_IREM_IMM) && mono_is_power_of_two (ins->inst_imm) >= 0)
2031 mono_decompose_op_imm (cfg, bb, ins);
2033 #ifdef MONO_ARCH_SIMD_INTRINSICS
2034 case OP_EXPAND_I1: {
2036 int temp_reg1 = mono_alloc_ireg (cfg);
2037 int temp_reg2 = mono_alloc_ireg (cfg);
2038 int original_reg = ins->sreg1;
2040 NEW_INS (cfg, ins, temp, OP_ICONV_TO_U1);
2041 temp->sreg1 = original_reg;
2042 temp->dreg = temp_reg1;
2044 NEW_INS (cfg, ins, temp, OP_SHL_IMM);
2045 temp->sreg1 = temp_reg1;
2046 temp->dreg = temp_reg2;
2049 NEW_INS (cfg, ins, temp, OP_IOR);
2050 temp->sreg1 = temp->dreg = temp_reg2;
2051 temp->sreg2 = temp_reg1;
2053 ins->opcode = OP_EXPAND_I2;
2054 ins->sreg1 = temp_reg2;
2063 bb->max_vreg = cfg->next_vreg;
2067 branch_cc_table [] = {
2068 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
2069 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
2070 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
2073 /* Maps CMP_... constants to X86_CC_... constants */
2076 X86_CC_EQ, X86_CC_NE, X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT,
2077 X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT
2081 cc_signed_table [] = {
2082 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
2083 FALSE, FALSE, FALSE, FALSE
2086 static unsigned char*
2087 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
2089 #define XMM_TEMP_REG 0
2090 /*This SSE2 optimization must not be done which OPT_SIMD in place as it clobbers xmm0.*/
2091 /*The xmm pass decomposes OP_FCONV_ ops anyway anyway.*/
2092 if (cfg->opt & MONO_OPT_SSE2 && size < 8 && !(cfg->opt & MONO_OPT_SIMD)) {
2093 /* optimize by assigning a local var for this use so we avoid
2094 * the stack manipulations */
2095 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2096 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
2097 x86_movsd_reg_membase (code, XMM_TEMP_REG, X86_ESP, 0);
2098 x86_cvttsd2si (code, dreg, XMM_TEMP_REG);
2099 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
2101 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
2103 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
2106 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
2107 x86_fnstcw_membase(code, X86_ESP, 0);
2108 x86_mov_reg_membase (code, dreg, X86_ESP, 0, 2);
2109 x86_alu_reg_imm (code, X86_OR, dreg, 0xc00);
2110 x86_mov_membase_reg (code, X86_ESP, 2, dreg, 2);
2111 x86_fldcw_membase (code, X86_ESP, 2);
2113 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2114 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
2115 x86_pop_reg (code, dreg);
2116 /* FIXME: need the high register
2117 * x86_pop_reg (code, dreg_high);
2120 x86_push_reg (code, X86_EAX); // SP = SP - 4
2121 x86_fist_pop_membase (code, X86_ESP, 0, FALSE);
2122 x86_pop_reg (code, dreg);
2124 x86_fldcw_membase (code, X86_ESP, 0);
2125 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2128 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
2130 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
2134 static unsigned char*
2135 mono_emit_stack_alloc (MonoCompile *cfg, guchar *code, MonoInst* tree)
2137 int sreg = tree->sreg1;
2138 int need_touch = FALSE;
2140 #if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
2149 * If requested stack size is larger than one page,
2150 * perform stack-touch operation
2153 * Generate stack probe code.
2154 * Under Windows, it is necessary to allocate one page at a time,
2155 * "touching" stack after each successful sub-allocation. This is
2156 * because of the way stack growth is implemented - there is a
2157 * guard page before the lowest stack page that is currently commited.
2158 * Stack normally grows sequentially so OS traps access to the
2159 * guard page and commits more pages when needed.
2161 x86_test_reg_imm (code, sreg, ~0xFFF);
2162 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2164 br[2] = code; /* loop */
2165 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
2166 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
2169 * By the end of the loop, sreg2 is smaller than 0x1000, so the init routine
2170 * that follows only initializes the last part of the area.
2172 /* Same as the init code below with size==0x1000 */
2173 if (tree->flags & MONO_INST_INIT) {
2174 x86_push_reg (code, X86_EAX);
2175 x86_push_reg (code, X86_ECX);
2176 x86_push_reg (code, X86_EDI);
2177 x86_mov_reg_imm (code, X86_ECX, (0x1000 >> 2));
2178 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2179 if (cfg->param_area)
2180 x86_lea_membase (code, X86_EDI, X86_ESP, 12 + ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
2182 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
2184 x86_prefix (code, X86_REP_PREFIX);
2186 x86_pop_reg (code, X86_EDI);
2187 x86_pop_reg (code, X86_ECX);
2188 x86_pop_reg (code, X86_EAX);
2191 x86_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
2192 x86_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
2193 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
2194 x86_patch (br[3], br[2]);
2195 x86_test_reg_reg (code, sreg, sreg);
2196 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2197 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
2199 br[1] = code; x86_jump8 (code, 0);
2201 x86_patch (br[0], code);
2202 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
2203 x86_patch (br[1], code);
2204 x86_patch (br[4], code);
2207 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
2209 if (tree->flags & MONO_INST_INIT) {
2211 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
2212 x86_push_reg (code, X86_EAX);
2215 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
2216 x86_push_reg (code, X86_ECX);
2219 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
2220 x86_push_reg (code, X86_EDI);
2224 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
2225 if (sreg != X86_ECX)
2226 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
2227 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2229 if (cfg->param_area)
2230 x86_lea_membase (code, X86_EDI, X86_ESP, offset + ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
2232 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
2234 x86_prefix (code, X86_REP_PREFIX);
2237 if (tree->dreg != X86_EDI && sreg != X86_EDI)
2238 x86_pop_reg (code, X86_EDI);
2239 if (tree->dreg != X86_ECX && sreg != X86_ECX)
2240 x86_pop_reg (code, X86_ECX);
2241 if (tree->dreg != X86_EAX && sreg != X86_EAX)
2242 x86_pop_reg (code, X86_EAX);
2249 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
2251 /* Move return value to the target register */
2252 switch (ins->opcode) {
2255 case OP_CALL_MEMBASE:
2256 if (ins->dreg != X86_EAX)
2257 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
2267 static int tls_gs_offset;
2271 mono_arch_have_fast_tls (void)
2274 static gboolean have_fast_tls = FALSE;
2275 static gboolean inited = FALSE;
2278 if (mini_get_debug_options ()->use_fallback_tls)
2281 return have_fast_tls;
2283 ins = (guint32*)pthread_getspecific;
2285 * We're looking for these two instructions:
2287 * mov 0x4(%esp),%eax
2288 * mov %gs:[offset](,%eax,4),%eax
2290 have_fast_tls = ins [0] == 0x0424448b && ins [1] == 0x85048b65;
2291 tls_gs_offset = ins [2];
2294 return have_fast_tls;
2295 #elif defined(TARGET_ANDROID)
2298 if (mini_get_debug_options ()->use_fallback_tls)
2305 mono_x86_emit_tls_get (guint8* code, int dreg, int tls_offset)
2307 #if defined(TARGET_MACH)
2308 x86_prefix (code, X86_GS_PREFIX);
2309 x86_mov_reg_mem (code, dreg, tls_gs_offset + (tls_offset * 4), 4);
2310 #elif defined(TARGET_WIN32)
2312 * See the Under the Hood article in the May 1996 issue of Microsoft Systems
2313 * Journal and/or a disassembly of the TlsGet () function.
2315 x86_prefix (code, X86_FS_PREFIX);
2316 x86_mov_reg_mem (code, dreg, 0x18, 4);
2317 if (tls_offset < 64) {
2318 x86_mov_reg_membase (code, dreg, dreg, 3600 + (tls_offset * 4), 4);
2322 g_assert (tls_offset < 0x440);
2323 /* Load TEB->TlsExpansionSlots */
2324 x86_mov_reg_membase (code, dreg, dreg, 0xf94, 4);
2325 x86_test_reg_reg (code, dreg, dreg);
2327 x86_branch (code, X86_CC_EQ, code, TRUE);
2328 x86_mov_reg_membase (code, dreg, dreg, (tls_offset * 4) - 0x100, 4);
2329 x86_patch (buf [0], code);
2332 if (optimize_for_xen) {
2333 x86_prefix (code, X86_GS_PREFIX);
2334 x86_mov_reg_mem (code, dreg, 0, 4);
2335 x86_mov_reg_membase (code, dreg, dreg, tls_offset, 4);
2337 x86_prefix (code, X86_GS_PREFIX);
2338 x86_mov_reg_mem (code, dreg, tls_offset, 4);
2345 mono_x86_emit_tls_set (guint8* code, int sreg, int tls_offset)
2347 #if defined(TARGET_MACH)
2348 x86_prefix (code, X86_GS_PREFIX);
2349 x86_mov_mem_reg (code, tls_gs_offset + (tls_offset * 4), sreg, 4);
2350 #elif defined(TARGET_WIN32)
2351 g_assert_not_reached ();
2353 x86_prefix (code, X86_GS_PREFIX);
2354 x86_mov_mem_reg (code, tls_offset, sreg, 4);
2362 * Emit code to initialize an LMF structure at LMF_OFFSET.
2365 emit_setup_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset, int cfa_offset)
2367 /* save all caller saved regs */
2368 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, ebx), X86_EBX, sizeof (mgreg_t));
2369 mono_emit_unwind_op_offset (cfg, code, X86_EBX, - cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, ebx));
2370 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, edi), X86_EDI, sizeof (mgreg_t));
2371 mono_emit_unwind_op_offset (cfg, code, X86_EDI, - cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, edi));
2372 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, esi), X86_ESI, sizeof (mgreg_t));
2373 mono_emit_unwind_op_offset (cfg, code, X86_ESI, - cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, esi));
2374 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, ebp), X86_EBP, sizeof (mgreg_t));
2376 /* save the current IP */
2377 if (cfg->compile_aot) {
2378 /* This pushes the current ip */
2379 x86_call_imm (code, 0);
2380 x86_pop_reg (code, X86_EAX);
2382 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
2383 x86_mov_reg_imm (code, X86_EAX, 0);
2385 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, eip), X86_EAX, sizeof (mgreg_t));
2387 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, eip), SLOT_NOREF);
2388 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, ebp), SLOT_NOREF);
2389 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, esi), SLOT_NOREF);
2390 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, edi), SLOT_NOREF);
2391 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, ebx), SLOT_NOREF);
2392 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, esp), SLOT_NOREF);
2393 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, method), SLOT_NOREF);
2394 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, lmf_addr), SLOT_NOREF);
2395 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), SLOT_NOREF);
2400 /* benchmark and set based on cpu */
2401 #define LOOP_ALIGNMENT 8
2402 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
2406 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2411 guint8 *code = cfg->native_code + cfg->code_len;
2414 if (cfg->opt & MONO_OPT_LOOP) {
2415 int pad, align = LOOP_ALIGNMENT;
2416 /* set alignment depending on cpu */
2417 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
2419 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
2420 x86_padding (code, pad);
2421 cfg->code_len += pad;
2422 bb->native_offset = cfg->code_len;
2426 if (cfg->verbose_level > 2)
2427 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2429 cpos = bb->max_offset;
2431 if ((cfg->prof_options & MONO_PROFILE_COVERAGE) && cfg->coverage_info) {
2432 MonoProfileCoverageInfo *cov = cfg->coverage_info;
2433 g_assert (!cfg->compile_aot);
2436 cov->data [bb->dfn].cil_code = bb->cil_code;
2437 /* this is not thread save, but good enough */
2438 x86_inc_mem (code, &cov->data [bb->dfn].count);
2441 offset = code - cfg->native_code;
2443 mono_debug_open_block (cfg, bb, offset);
2445 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)
2446 x86_breakpoint (code);
2448 MONO_BB_FOR_EACH_INS (bb, ins) {
2449 offset = code - cfg->native_code;
2451 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
2453 #define EXTRA_CODE_SPACE (16)
2455 if (G_UNLIKELY (offset > (cfg->code_size - max_len - EXTRA_CODE_SPACE))) {
2456 cfg->code_size *= 2;
2457 cfg->native_code = mono_realloc_native_code(cfg);
2458 code = cfg->native_code + offset;
2459 cfg->stat_code_reallocs++;
2462 if (cfg->debug_info)
2463 mono_debug_record_line_number (cfg, ins, offset);
2465 switch (ins->opcode) {
2467 x86_mul_reg (code, ins->sreg2, TRUE);
2470 x86_mul_reg (code, ins->sreg2, FALSE);
2472 case OP_X86_SETEQ_MEMBASE:
2473 case OP_X86_SETNE_MEMBASE:
2474 x86_set_membase (code, ins->opcode == OP_X86_SETEQ_MEMBASE ? X86_CC_EQ : X86_CC_NE,
2475 ins->inst_basereg, ins->inst_offset, TRUE);
2477 case OP_STOREI1_MEMBASE_IMM:
2478 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
2480 case OP_STOREI2_MEMBASE_IMM:
2481 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
2483 case OP_STORE_MEMBASE_IMM:
2484 case OP_STOREI4_MEMBASE_IMM:
2485 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
2487 case OP_STOREI1_MEMBASE_REG:
2488 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
2490 case OP_STOREI2_MEMBASE_REG:
2491 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
2493 case OP_STORE_MEMBASE_REG:
2494 case OP_STOREI4_MEMBASE_REG:
2495 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
2497 case OP_STORE_MEM_IMM:
2498 x86_mov_mem_imm (code, ins->inst_p0, ins->inst_c0, 4);
2501 x86_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
2505 /* These are created by the cprop pass so they use inst_imm as the source */
2506 x86_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
2509 x86_widen_mem (code, ins->dreg, ins->inst_imm, FALSE, FALSE);
2512 x86_widen_mem (code, ins->dreg, ins->inst_imm, FALSE, TRUE);
2514 case OP_LOAD_MEMBASE:
2515 case OP_LOADI4_MEMBASE:
2516 case OP_LOADU4_MEMBASE:
2517 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
2519 case OP_LOADU1_MEMBASE:
2520 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
2522 case OP_LOADI1_MEMBASE:
2523 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
2525 case OP_LOADU2_MEMBASE:
2526 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
2528 case OP_LOADI2_MEMBASE:
2529 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
2531 case OP_ICONV_TO_I1:
2533 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2535 case OP_ICONV_TO_I2:
2537 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2539 case OP_ICONV_TO_U1:
2540 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
2542 case OP_ICONV_TO_U2:
2543 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
2547 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
2549 case OP_COMPARE_IMM:
2550 case OP_ICOMPARE_IMM:
2551 x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
2553 case OP_X86_COMPARE_MEMBASE_REG:
2554 x86_alu_membase_reg (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2556 case OP_X86_COMPARE_MEMBASE_IMM:
2557 x86_alu_membase_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2559 case OP_X86_COMPARE_MEMBASE8_IMM:
2560 x86_alu_membase8_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2562 case OP_X86_COMPARE_REG_MEMBASE:
2563 x86_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
2565 case OP_X86_COMPARE_MEM_IMM:
2566 x86_alu_mem_imm (code, X86_CMP, ins->inst_offset, ins->inst_imm);
2568 case OP_X86_TEST_NULL:
2569 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2571 case OP_X86_ADD_MEMBASE_IMM:
2572 x86_alu_membase_imm (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2574 case OP_X86_ADD_REG_MEMBASE:
2575 x86_alu_reg_membase (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset);
2577 case OP_X86_SUB_MEMBASE_IMM:
2578 x86_alu_membase_imm (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2580 case OP_X86_SUB_REG_MEMBASE:
2581 x86_alu_reg_membase (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset);
2583 case OP_X86_AND_MEMBASE_IMM:
2584 x86_alu_membase_imm (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2586 case OP_X86_OR_MEMBASE_IMM:
2587 x86_alu_membase_imm (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2589 case OP_X86_XOR_MEMBASE_IMM:
2590 x86_alu_membase_imm (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2592 case OP_X86_ADD_MEMBASE_REG:
2593 x86_alu_membase_reg (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2595 case OP_X86_SUB_MEMBASE_REG:
2596 x86_alu_membase_reg (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2598 case OP_X86_AND_MEMBASE_REG:
2599 x86_alu_membase_reg (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2601 case OP_X86_OR_MEMBASE_REG:
2602 x86_alu_membase_reg (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2604 case OP_X86_XOR_MEMBASE_REG:
2605 x86_alu_membase_reg (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2607 case OP_X86_INC_MEMBASE:
2608 x86_inc_membase (code, ins->inst_basereg, ins->inst_offset);
2610 case OP_X86_INC_REG:
2611 x86_inc_reg (code, ins->dreg);
2613 case OP_X86_DEC_MEMBASE:
2614 x86_dec_membase (code, ins->inst_basereg, ins->inst_offset);
2616 case OP_X86_DEC_REG:
2617 x86_dec_reg (code, ins->dreg);
2619 case OP_X86_MUL_REG_MEMBASE:
2620 x86_imul_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
2622 case OP_X86_AND_REG_MEMBASE:
2623 x86_alu_reg_membase (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset);
2625 case OP_X86_OR_REG_MEMBASE:
2626 x86_alu_reg_membase (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset);
2628 case OP_X86_XOR_REG_MEMBASE:
2629 x86_alu_reg_membase (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset);
2632 x86_breakpoint (code);
2634 case OP_RELAXED_NOP:
2635 x86_prefix (code, X86_REP_PREFIX);
2643 case OP_DUMMY_STORE:
2644 case OP_DUMMY_ICONST:
2645 case OP_DUMMY_R8CONST:
2646 case OP_NOT_REACHED:
2649 case OP_IL_SEQ_POINT:
2650 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
2652 case OP_SEQ_POINT: {
2655 if (cfg->compile_aot)
2658 /* Have to use ecx as a temp reg since this can occur after OP_SETRET */
2661 * We do this _before_ the breakpoint, so single stepping after
2662 * a breakpoint is hit will step to the next IL offset.
2664 if (ins->flags & MONO_INST_SINGLE_STEP_LOC) {
2665 MonoInst *var = cfg->arch.ss_tramp_var;
2669 g_assert (var->opcode == OP_REGOFFSET);
2670 /* Load ss_tramp_var */
2671 /* This is equal to &ss_trampoline */
2672 x86_mov_reg_membase (code, X86_ECX, var->inst_basereg, var->inst_offset, sizeof (mgreg_t));
2673 x86_mov_reg_membase (code, X86_ECX, X86_ECX, 0, sizeof (mgreg_t));
2674 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
2675 br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
2676 x86_call_reg (code, X86_ECX);
2677 x86_patch (br [0], code);
2681 * Many parts of sdb depend on the ip after the single step trampoline call to be equal to the seq point offset.
2682 * This means we have to put the loading of bp_tramp_var after the offset.
2685 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
2687 MonoInst *var = cfg->arch.bp_tramp_var;
2690 g_assert (var->opcode == OP_REGOFFSET);
2691 /* Load the address of the bp trampoline */
2692 /* This needs to be constant size */
2693 guint8 *start = code;
2694 x86_mov_reg_membase (code, X86_ECX, var->inst_basereg, var->inst_offset, 4);
2695 if (code < start + OP_SEQ_POINT_BP_OFFSET) {
2696 int size = start + OP_SEQ_POINT_BP_OFFSET - code;
2697 x86_padding (code, size);
2700 * A placeholder for a possible breakpoint inserted by
2701 * mono_arch_set_breakpoint ().
2703 for (i = 0; i < 2; ++i)
2706 * Add an additional nop so skipping the bp doesn't cause the ip to point
2707 * to another IL offset.
2715 x86_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
2719 x86_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
2724 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
2728 x86_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
2733 x86_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
2737 x86_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
2742 x86_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
2746 x86_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
2749 x86_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
2753 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
2758 * The code is the same for div/rem, the allocator will allocate dreg
2759 * to RAX/RDX as appropriate.
2761 if (ins->sreg2 == X86_EDX) {
2762 /* cdq clobbers this */
2763 x86_push_reg (code, ins->sreg2);
2765 x86_div_membase (code, X86_ESP, 0, TRUE);
2766 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2769 x86_div_reg (code, ins->sreg2, TRUE);
2774 if (ins->sreg2 == X86_EDX) {
2775 x86_push_reg (code, ins->sreg2);
2776 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2777 x86_div_membase (code, X86_ESP, 0, FALSE);
2778 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2780 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2781 x86_div_reg (code, ins->sreg2, FALSE);
2785 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2787 x86_div_reg (code, ins->sreg2, TRUE);
2790 int power = mono_is_power_of_two (ins->inst_imm);
2792 g_assert (ins->sreg1 == X86_EAX);
2793 g_assert (ins->dreg == X86_EAX);
2794 g_assert (power >= 0);
2797 /* Based on http://compilers.iecc.com/comparch/article/93-04-079 */
2799 x86_alu_reg_imm (code, X86_AND, X86_EAX, 1);
2801 * If the divident is >= 0, this does not nothing. If it is positive, it
2802 * it transforms %eax=0 into %eax=0, and %eax=1 into %eax=-1.
2804 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EDX);
2805 x86_alu_reg_reg (code, X86_SUB, X86_EAX, X86_EDX);
2806 } else if (power == 0) {
2807 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2809 /* Based on gcc code */
2811 /* Add compensation for negative dividents */
2813 x86_shift_reg_imm (code, X86_SHR, X86_EDX, 32 - power);
2814 x86_alu_reg_reg (code, X86_ADD, X86_EAX, X86_EDX);
2815 /* Compute remainder */
2816 x86_alu_reg_imm (code, X86_AND, X86_EAX, (1 << power) - 1);
2817 /* Remove compensation */
2818 x86_alu_reg_reg (code, X86_SUB, X86_EAX, X86_EDX);
2823 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
2827 x86_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
2830 x86_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
2834 x86_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
2837 g_assert (ins->sreg2 == X86_ECX);
2838 x86_shift_reg (code, X86_SHL, ins->dreg);
2841 g_assert (ins->sreg2 == X86_ECX);
2842 x86_shift_reg (code, X86_SAR, ins->dreg);
2846 x86_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
2849 case OP_ISHR_UN_IMM:
2850 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
2853 g_assert (ins->sreg2 == X86_ECX);
2854 x86_shift_reg (code, X86_SHR, ins->dreg);
2858 x86_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
2861 guint8 *jump_to_end;
2863 /* handle shifts below 32 bits */
2864 x86_shld_reg (code, ins->backend.reg3, ins->sreg1);
2865 x86_shift_reg (code, X86_SHL, ins->sreg1);
2867 x86_test_reg_imm (code, X86_ECX, 32);
2868 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
2870 /* handle shift over 32 bit */
2871 x86_mov_reg_reg (code, ins->backend.reg3, ins->sreg1, 4);
2872 x86_clear_reg (code, ins->sreg1);
2874 x86_patch (jump_to_end, code);
2878 guint8 *jump_to_end;
2880 /* handle shifts below 32 bits */
2881 x86_shrd_reg (code, ins->sreg1, ins->backend.reg3);
2882 x86_shift_reg (code, X86_SAR, ins->backend.reg3);
2884 x86_test_reg_imm (code, X86_ECX, 32);
2885 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
2887 /* handle shifts over 31 bits */
2888 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2889 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, 31);
2891 x86_patch (jump_to_end, code);
2895 guint8 *jump_to_end;
2897 /* handle shifts below 32 bits */
2898 x86_shrd_reg (code, ins->sreg1, ins->backend.reg3);
2899 x86_shift_reg (code, X86_SHR, ins->backend.reg3);
2901 x86_test_reg_imm (code, X86_ECX, 32);
2902 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
2904 /* handle shifts over 31 bits */
2905 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2906 x86_clear_reg (code, ins->backend.reg3);
2908 x86_patch (jump_to_end, code);
2912 if (ins->inst_imm >= 32) {
2913 x86_mov_reg_reg (code, ins->backend.reg3, ins->sreg1, 4);
2914 x86_clear_reg (code, ins->sreg1);
2915 x86_shift_reg_imm (code, X86_SHL, ins->backend.reg3, ins->inst_imm - 32);
2917 x86_shld_reg_imm (code, ins->backend.reg3, ins->sreg1, ins->inst_imm);
2918 x86_shift_reg_imm (code, X86_SHL, ins->sreg1, ins->inst_imm);
2922 if (ins->inst_imm >= 32) {
2923 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2924 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, 0x1f);
2925 x86_shift_reg_imm (code, X86_SAR, ins->sreg1, ins->inst_imm - 32);
2927 x86_shrd_reg_imm (code, ins->sreg1, ins->backend.reg3, ins->inst_imm);
2928 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, ins->inst_imm);
2931 case OP_LSHR_UN_IMM:
2932 if (ins->inst_imm >= 32) {
2933 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2934 x86_clear_reg (code, ins->backend.reg3);
2935 x86_shift_reg_imm (code, X86_SHR, ins->sreg1, ins->inst_imm - 32);
2937 x86_shrd_reg_imm (code, ins->sreg1, ins->backend.reg3, ins->inst_imm);
2938 x86_shift_reg_imm (code, X86_SHR, ins->backend.reg3, ins->inst_imm);
2942 x86_not_reg (code, ins->sreg1);
2945 x86_neg_reg (code, ins->sreg1);
2949 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2953 switch (ins->inst_imm) {
2957 if (ins->dreg != ins->sreg1)
2958 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2959 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
2962 /* LEA r1, [r2 + r2*2] */
2963 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
2966 /* LEA r1, [r2 + r2*4] */
2967 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2970 /* LEA r1, [r2 + r2*2] */
2972 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
2973 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
2976 /* LEA r1, [r2 + r2*8] */
2977 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
2980 /* LEA r1, [r2 + r2*4] */
2982 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2983 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
2986 /* LEA r1, [r2 + r2*2] */
2988 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
2989 x86_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
2992 /* LEA r1, [r2 + r2*4] */
2993 /* LEA r1, [r1 + r1*4] */
2994 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2995 x86_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
2998 /* LEA r1, [r2 + r2*4] */
3000 /* LEA r1, [r1 + r1*4] */
3001 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3002 x86_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
3003 x86_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
3006 x86_imul_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_imm);
3011 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
3012 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
3014 case OP_IMUL_OVF_UN: {
3015 /* the mul operation and the exception check should most likely be split */
3016 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
3017 /*g_assert (ins->sreg2 == X86_EAX);
3018 g_assert (ins->dreg == X86_EAX);*/
3019 if (ins->sreg2 == X86_EAX) {
3020 non_eax_reg = ins->sreg1;
3021 } else if (ins->sreg1 == X86_EAX) {
3022 non_eax_reg = ins->sreg2;
3024 /* no need to save since we're going to store to it anyway */
3025 if (ins->dreg != X86_EAX) {
3027 x86_push_reg (code, X86_EAX);
3029 x86_mov_reg_reg (code, X86_EAX, ins->sreg1, 4);
3030 non_eax_reg = ins->sreg2;
3032 if (ins->dreg == X86_EDX) {
3035 x86_push_reg (code, X86_EAX);
3039 x86_push_reg (code, X86_EDX);
3041 x86_mul_reg (code, non_eax_reg, FALSE);
3042 /* save before the check since pop and mov don't change the flags */
3043 if (ins->dreg != X86_EAX)
3044 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
3046 x86_pop_reg (code, X86_EDX);
3048 x86_pop_reg (code, X86_EAX);
3049 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
3053 x86_mov_reg_imm (code, ins->dreg, ins->inst_c0);
3056 g_assert_not_reached ();
3057 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3058 x86_mov_reg_imm (code, ins->dreg, 0);
3061 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3062 x86_mov_reg_imm (code, ins->dreg, 0);
3064 case OP_LOAD_GOTADDR:
3065 g_assert (ins->dreg == MONO_ARCH_GOT_REG);
3066 code = mono_arch_emit_load_got_addr (cfg->native_code, code, cfg, NULL);
3069 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
3070 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, 0xf0f0f0f0, 4);
3072 case OP_X86_PUSH_GOT_ENTRY:
3073 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
3074 x86_push_membase (code, ins->inst_basereg, 0xf0f0f0f0);
3077 if (ins->dreg != ins->sreg1)
3078 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
3081 MonoCallInst *call = (MonoCallInst*)ins;
3084 ins->flags |= MONO_INST_GC_CALLSITE;
3085 ins->backend.pc_offset = code - cfg->native_code;
3087 /* reset offset to make max_len work */
3088 offset = code - cfg->native_code;
3090 g_assert (!cfg->method->save_lmf);
3092 /* restore callee saved registers */
3093 for (i = 0; i < X86_NREG; ++i)
3094 if (X86_IS_CALLEE_SAVED_REG (i) && cfg->used_int_regs & (1 << i))
3096 if (cfg->used_int_regs & (1 << X86_ESI)) {
3097 x86_mov_reg_membase (code, X86_ESI, X86_EBP, pos, 4);
3100 if (cfg->used_int_regs & (1 << X86_EDI)) {
3101 x86_mov_reg_membase (code, X86_EDI, X86_EBP, pos, 4);
3104 if (cfg->used_int_regs & (1 << X86_EBX)) {
3105 x86_mov_reg_membase (code, X86_EBX, X86_EBP, pos, 4);
3109 /* Copy arguments on the stack to our argument area */
3110 for (i = 0; i < call->stack_usage - call->stack_align_amount; i += 4) {
3111 x86_mov_reg_membase (code, X86_EAX, X86_ESP, i, 4);
3112 x86_mov_membase_reg (code, X86_EBP, 8 + i, X86_EAX, 4);
3115 /* restore ESP/EBP */
3117 offset = code - cfg->native_code;
3118 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD_JUMP, call->method);
3119 x86_jump32 (code, 0);
3121 ins->flags |= MONO_INST_GC_CALLSITE;
3122 cfg->disable_aot = TRUE;
3126 /* ensure ins->sreg1 is not NULL
3127 * note that cmp DWORD PTR [eax], eax is one byte shorter than
3128 * cmp DWORD PTR [eax], 0
3130 x86_alu_membase_reg (code, X86_CMP, ins->sreg1, 0, ins->sreg1);
3133 int hreg = ins->sreg1 == X86_EAX? X86_ECX: X86_EAX;
3134 x86_push_reg (code, hreg);
3135 x86_lea_membase (code, hreg, X86_EBP, cfg->sig_cookie);
3136 x86_mov_membase_reg (code, ins->sreg1, 0, hreg, 4);
3137 x86_pop_reg (code, hreg);
3150 case OP_VOIDCALL_REG:
3152 case OP_FCALL_MEMBASE:
3153 case OP_LCALL_MEMBASE:
3154 case OP_VCALL_MEMBASE:
3155 case OP_VCALL2_MEMBASE:
3156 case OP_VOIDCALL_MEMBASE:
3157 case OP_CALL_MEMBASE: {
3160 call = (MonoCallInst*)ins;
3161 cinfo = (CallInfo*)call->call_info;
3163 switch (ins->opcode) {
3170 if (ins->flags & MONO_INST_HAS_METHOD)
3171 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method);
3173 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr);
3179 case OP_VOIDCALL_REG:
3181 x86_call_reg (code, ins->sreg1);
3183 case OP_FCALL_MEMBASE:
3184 case OP_LCALL_MEMBASE:
3185 case OP_VCALL_MEMBASE:
3186 case OP_VCALL2_MEMBASE:
3187 case OP_VOIDCALL_MEMBASE:
3188 case OP_CALL_MEMBASE:
3189 x86_call_membase (code, ins->sreg1, ins->inst_offset);
3192 g_assert_not_reached ();
3195 ins->flags |= MONO_INST_GC_CALLSITE;
3196 ins->backend.pc_offset = code - cfg->native_code;
3197 if (cinfo->callee_stack_pop) {
3198 /* Have to compensate for the stack space popped by the callee */
3199 x86_alu_reg_imm (code, X86_SUB, X86_ESP, cinfo->callee_stack_pop);
3201 code = emit_move_return_value (cfg, ins, code);
3205 x86_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
3207 case OP_X86_LEA_MEMBASE:
3208 x86_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
3211 x86_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
3214 /* keep alignment */
3215 x86_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_LOCALLOC_ALIGNMENT - 1);
3216 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_LOCALLOC_ALIGNMENT - 1));
3217 code = mono_emit_stack_alloc (cfg, code, ins);
3218 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
3219 if (cfg->param_area)
3220 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
3222 case OP_LOCALLOC_IMM: {
3223 guint32 size = ins->inst_imm;
3224 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
3226 if (ins->flags & MONO_INST_INIT) {
3227 /* FIXME: Optimize this */
3228 x86_mov_reg_imm (code, ins->dreg, size);
3229 ins->sreg1 = ins->dreg;
3231 code = mono_emit_stack_alloc (cfg, code, ins);
3232 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
3234 x86_alu_reg_imm (code, X86_SUB, X86_ESP, size);
3235 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
3237 if (cfg->param_area)
3238 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
3242 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3243 x86_push_reg (code, ins->sreg1);
3244 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
3245 (gpointer)"mono_arch_throw_exception");
3246 ins->flags |= MONO_INST_GC_CALLSITE;
3247 ins->backend.pc_offset = code - cfg->native_code;
3251 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3252 x86_push_reg (code, ins->sreg1);
3253 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
3254 (gpointer)"mono_arch_rethrow_exception");
3255 ins->flags |= MONO_INST_GC_CALLSITE;
3256 ins->backend.pc_offset = code - cfg->native_code;
3259 case OP_CALL_HANDLER:
3260 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3261 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3262 x86_call_imm (code, 0);
3263 mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
3264 x86_alu_reg_imm (code, X86_ADD, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3266 case OP_START_HANDLER: {
3267 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3268 x86_mov_membase_reg (code, spvar->inst_basereg, spvar->inst_offset, X86_ESP, 4);
3269 if (cfg->param_area)
3270 x86_alu_reg_imm (code, X86_SUB, X86_ESP, ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
3273 case OP_ENDFINALLY: {
3274 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3275 x86_mov_reg_membase (code, X86_ESP, spvar->inst_basereg, spvar->inst_offset, 4);
3279 case OP_ENDFILTER: {
3280 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3281 x86_mov_reg_membase (code, X86_ESP, spvar->inst_basereg, spvar->inst_offset, 4);
3282 /* The local allocator will put the result into EAX */
3287 if (ins->dreg != X86_EAX)
3288 x86_mov_reg_reg (code, ins->dreg, X86_EAX, sizeof (gpointer));
3292 ins->inst_c0 = code - cfg->native_code;
3295 if (ins->inst_target_bb->native_offset) {
3296 x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
3298 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3299 if ((cfg->opt & MONO_OPT_BRANCH) &&
3300 x86_is_imm8 (ins->inst_target_bb->max_offset - cpos))
3301 x86_jump8 (code, 0);
3303 x86_jump32 (code, 0);
3307 x86_jump_reg (code, ins->sreg1);
3326 x86_set_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
3327 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3329 case OP_COND_EXC_EQ:
3330 case OP_COND_EXC_NE_UN:
3331 case OP_COND_EXC_LT:
3332 case OP_COND_EXC_LT_UN:
3333 case OP_COND_EXC_GT:
3334 case OP_COND_EXC_GT_UN:
3335 case OP_COND_EXC_GE:
3336 case OP_COND_EXC_GE_UN:
3337 case OP_COND_EXC_LE:
3338 case OP_COND_EXC_LE_UN:
3339 case OP_COND_EXC_IEQ:
3340 case OP_COND_EXC_INE_UN:
3341 case OP_COND_EXC_ILT:
3342 case OP_COND_EXC_ILT_UN:
3343 case OP_COND_EXC_IGT:
3344 case OP_COND_EXC_IGT_UN:
3345 case OP_COND_EXC_IGE:
3346 case OP_COND_EXC_IGE_UN:
3347 case OP_COND_EXC_ILE:
3348 case OP_COND_EXC_ILE_UN:
3349 EMIT_COND_SYSTEM_EXCEPTION (cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)], ins->inst_p1);
3351 case OP_COND_EXC_OV:
3352 case OP_COND_EXC_NO:
3354 case OP_COND_EXC_NC:
3355 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ], (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
3357 case OP_COND_EXC_IOV:
3358 case OP_COND_EXC_INO:
3359 case OP_COND_EXC_IC:
3360 case OP_COND_EXC_INC:
3361 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_IEQ], (ins->opcode < OP_COND_EXC_INE_UN), ins->inst_p1);
3373 EMIT_COND_BRANCH (ins, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
3381 case OP_CMOV_INE_UN:
3382 case OP_CMOV_IGE_UN:
3383 case OP_CMOV_IGT_UN:
3384 case OP_CMOV_ILE_UN:
3385 case OP_CMOV_ILT_UN:
3386 g_assert (ins->dreg == ins->sreg1);
3387 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);
3390 /* floating point opcodes */
3392 double d = *(double *)ins->inst_p0;
3394 if ((d == 0.0) && (mono_signbit (d) == 0)) {
3396 } else if (d == 1.0) {
3399 if (cfg->compile_aot) {
3400 guint32 *val = (guint32*)&d;
3401 x86_push_imm (code, val [1]);
3402 x86_push_imm (code, val [0]);
3403 x86_fld_membase (code, X86_ESP, 0, TRUE);
3404 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3407 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R8, ins->inst_p0);
3408 x86_fld (code, NULL, TRUE);
3414 float f = *(float *)ins->inst_p0;
3416 if ((f == 0.0) && (mono_signbit (f) == 0)) {
3418 } else if (f == 1.0) {
3421 if (cfg->compile_aot) {
3422 guint32 val = *(guint32*)&f;
3423 x86_push_imm (code, val);
3424 x86_fld_membase (code, X86_ESP, 0, FALSE);
3425 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3428 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R4, ins->inst_p0);
3429 x86_fld (code, NULL, FALSE);
3434 case OP_STORER8_MEMBASE_REG:
3435 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, TRUE, TRUE);
3437 case OP_LOADR8_MEMBASE:
3438 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
3440 case OP_STORER4_MEMBASE_REG:
3441 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, FALSE, TRUE);
3443 case OP_LOADR4_MEMBASE:
3444 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
3446 case OP_ICONV_TO_R4:
3447 x86_push_reg (code, ins->sreg1);
3448 x86_fild_membase (code, X86_ESP, 0, FALSE);
3449 /* Change precision */
3450 x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
3451 x86_fld_membase (code, X86_ESP, 0, FALSE);
3452 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3454 case OP_ICONV_TO_R8:
3455 x86_push_reg (code, ins->sreg1);
3456 x86_fild_membase (code, X86_ESP, 0, FALSE);
3457 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3459 case OP_ICONV_TO_R_UN:
3460 x86_push_imm (code, 0);
3461 x86_push_reg (code, ins->sreg1);
3462 x86_fild_membase (code, X86_ESP, 0, TRUE);
3463 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3465 case OP_X86_FP_LOAD_I8:
3466 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
3468 case OP_X86_FP_LOAD_I4:
3469 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
3471 case OP_FCONV_TO_R4:
3472 /* Change precision */
3473 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
3474 x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
3475 x86_fld_membase (code, X86_ESP, 0, FALSE);
3476 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3478 case OP_FCONV_TO_I1:
3479 code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
3481 case OP_FCONV_TO_U1:
3482 code = emit_float_to_int (cfg, code, ins->dreg, 1, FALSE);
3484 case OP_FCONV_TO_I2:
3485 code = emit_float_to_int (cfg, code, ins->dreg, 2, TRUE);
3487 case OP_FCONV_TO_U2:
3488 code = emit_float_to_int (cfg, code, ins->dreg, 2, FALSE);
3490 case OP_FCONV_TO_I4:
3492 code = emit_float_to_int (cfg, code, ins->dreg, 4, TRUE);
3494 case OP_FCONV_TO_I8:
3495 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
3496 x86_fnstcw_membase(code, X86_ESP, 0);
3497 x86_mov_reg_membase (code, ins->dreg, X86_ESP, 0, 2);
3498 x86_alu_reg_imm (code, X86_OR, ins->dreg, 0xc00);
3499 x86_mov_membase_reg (code, X86_ESP, 2, ins->dreg, 2);
3500 x86_fldcw_membase (code, X86_ESP, 2);
3501 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
3502 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
3503 x86_pop_reg (code, ins->dreg);
3504 x86_pop_reg (code, ins->backend.reg3);
3505 x86_fldcw_membase (code, X86_ESP, 0);
3506 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3508 case OP_LCONV_TO_R8_2:
3509 x86_push_reg (code, ins->sreg2);
3510 x86_push_reg (code, ins->sreg1);
3511 x86_fild_membase (code, X86_ESP, 0, TRUE);
3512 /* Change precision */
3513 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
3514 x86_fld_membase (code, X86_ESP, 0, TRUE);
3515 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3517 case OP_LCONV_TO_R4_2:
3518 x86_push_reg (code, ins->sreg2);
3519 x86_push_reg (code, ins->sreg1);
3520 x86_fild_membase (code, X86_ESP, 0, TRUE);
3521 /* Change precision */
3522 x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
3523 x86_fld_membase (code, X86_ESP, 0, FALSE);
3524 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3526 case OP_LCONV_TO_R_UN_2: {
3527 static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
3530 /* load 64bit integer to FP stack */
3531 x86_push_reg (code, ins->sreg2);
3532 x86_push_reg (code, ins->sreg1);
3533 x86_fild_membase (code, X86_ESP, 0, TRUE);
3535 /* test if lreg is negative */
3536 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
3537 br = code; x86_branch8 (code, X86_CC_GEZ, 0, TRUE);
3539 /* add correction constant mn */
3540 if (cfg->compile_aot) {
3541 x86_push_imm (code, (((guint32)mn [9]) << 24) | ((guint32)mn [8] << 16) | ((guint32)mn [7] << 8) | ((guint32)mn [6]));
3542 x86_push_imm (code, (((guint32)mn [5]) << 24) | ((guint32)mn [4] << 16) | ((guint32)mn [3] << 8) | ((guint32)mn [2]));
3543 x86_push_imm (code, (((guint32)mn [1]) << 24) | ((guint32)mn [0] << 16));
3544 x86_fld80_membase (code, X86_ESP, 2);
3545 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
3547 x86_fld80_mem (code, mn);
3549 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3551 x86_patch (br, code);
3553 /* Change precision */
3554 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
3555 x86_fld_membase (code, X86_ESP, 0, TRUE);
3557 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3561 case OP_LCONV_TO_OVF_I:
3562 case OP_LCONV_TO_OVF_I4_2: {
3563 guint8 *br [3], *label [1];
3567 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
3569 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
3571 /* If the low word top bit is set, see if we are negative */
3572 br [0] = code; x86_branch8 (code, X86_CC_LT, 0, TRUE);
3573 /* We are not negative (no top bit set, check for our top word to be zero */
3574 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
3575 br [1] = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
3578 /* throw exception */
3579 tins = mono_branch_optimize_exception_target (cfg, bb, "OverflowException");
3581 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, tins->inst_true_bb);
3582 if ((cfg->opt & MONO_OPT_BRANCH) && x86_is_imm8 (tins->inst_true_bb->max_offset - cpos))
3583 x86_jump8 (code, 0);
3585 x86_jump32 (code, 0);
3587 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
3588 x86_jump32 (code, 0);
3592 x86_patch (br [0], code);
3593 /* our top bit is set, check that top word is 0xfffffff */
3594 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0xffffffff);
3596 x86_patch (br [1], code);
3597 /* nope, emit exception */
3598 br [2] = code; x86_branch8 (code, X86_CC_NE, 0, TRUE);
3599 x86_patch (br [2], label [0]);
3601 if (ins->dreg != ins->sreg1)
3602 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
3606 /* Not needed on the fp stack */
3608 case OP_MOVE_F_TO_I4:
3609 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE, TRUE);
3610 x86_mov_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, 4);
3612 case OP_MOVE_I4_TO_F:
3613 x86_mov_membase_reg (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, ins->sreg1, 4);
3614 x86_fld_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE);
3617 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3620 x86_fp_op_reg (code, X86_FSUB, 1, TRUE);
3623 x86_fp_op_reg (code, X86_FMUL, 1, TRUE);
3626 x86_fp_op_reg (code, X86_FDIV, 1, TRUE);
3634 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3639 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3646 * it really doesn't make sense to inline all this code,
3647 * it's here just to show that things may not be as simple
3650 guchar *check_pos, *end_tan, *pop_jump;
3651 x86_push_reg (code, X86_EAX);
3654 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
3656 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3657 x86_fstp (code, 0); /* pop the 1.0 */
3659 x86_jump8 (code, 0);
3661 x86_fp_op (code, X86_FADD, 0);
3665 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
3667 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3670 x86_patch (pop_jump, code);
3671 x86_fstp (code, 0); /* pop the 1.0 */
3672 x86_patch (check_pos, code);
3673 x86_patch (end_tan, code);
3675 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3676 x86_pop_reg (code, X86_EAX);
3683 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3692 g_assert (cfg->opt & MONO_OPT_CMOV);
3693 g_assert (ins->dreg == ins->sreg1);
3694 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3695 x86_cmov_reg (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2);
3698 g_assert (cfg->opt & MONO_OPT_CMOV);
3699 g_assert (ins->dreg == ins->sreg1);
3700 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3701 x86_cmov_reg (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2);
3704 g_assert (cfg->opt & MONO_OPT_CMOV);
3705 g_assert (ins->dreg == ins->sreg1);
3706 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3707 x86_cmov_reg (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2);
3710 g_assert (cfg->opt & MONO_OPT_CMOV);
3711 g_assert (ins->dreg == ins->sreg1);
3712 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3713 x86_cmov_reg (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2);
3719 x86_fxch (code, ins->inst_imm);
3724 x86_push_reg (code, X86_EAX);
3725 /* we need to exchange ST(0) with ST(1) */
3728 /* this requires a loop, because fprem somtimes
3729 * returns a partial remainder */
3731 /* looks like MS is using fprem instead of the IEEE compatible fprem1 */
3732 /* x86_fprem1 (code); */
3735 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_C2);
3737 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3743 x86_pop_reg (code, X86_EAX);
3747 if (cfg->opt & MONO_OPT_FCMOV) {
3748 x86_fcomip (code, 1);
3752 /* this overwrites EAX */
3753 EMIT_FPCOMPARE(code);
3754 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3758 if (cfg->opt & MONO_OPT_FCMOV) {
3759 /* zeroing the register at the start results in
3760 * shorter and faster code (we can also remove the widening op)
3762 guchar *unordered_check;
3763 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3764 x86_fcomip (code, 1);
3766 unordered_check = code;
3767 x86_branch8 (code, X86_CC_P, 0, FALSE);
3768 if (ins->opcode == OP_FCEQ) {
3769 x86_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
3770 x86_patch (unordered_check, code);
3772 guchar *jump_to_end;
3773 x86_set_reg (code, X86_CC_NE, ins->dreg, FALSE);
3775 x86_jump8 (code, 0);
3776 x86_patch (unordered_check, code);
3777 x86_inc_reg (code, ins->dreg);
3778 x86_patch (jump_to_end, code);
3783 if (ins->dreg != X86_EAX)
3784 x86_push_reg (code, X86_EAX);
3786 EMIT_FPCOMPARE(code);
3787 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3788 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
3789 x86_set_reg (code, ins->opcode == OP_FCEQ ? X86_CC_EQ : X86_CC_NE, ins->dreg, TRUE);
3790 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3792 if (ins->dreg != X86_EAX)
3793 x86_pop_reg (code, X86_EAX);
3797 if (cfg->opt & MONO_OPT_FCMOV) {
3798 /* zeroing the register at the start results in
3799 * shorter and faster code (we can also remove the widening op)
3801 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3802 x86_fcomip (code, 1);
3804 if (ins->opcode == OP_FCLT_UN) {
3805 guchar *unordered_check = code;
3806 guchar *jump_to_end;
3807 x86_branch8 (code, X86_CC_P, 0, FALSE);
3808 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
3810 x86_jump8 (code, 0);
3811 x86_patch (unordered_check, code);
3812 x86_inc_reg (code, ins->dreg);
3813 x86_patch (jump_to_end, code);
3815 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
3819 if (ins->dreg != X86_EAX)
3820 x86_push_reg (code, X86_EAX);
3822 EMIT_FPCOMPARE(code);
3823 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3824 if (ins->opcode == OP_FCLT_UN) {
3825 guchar *is_not_zero_check, *end_jump;
3826 is_not_zero_check = code;
3827 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3829 x86_jump8 (code, 0);
3830 x86_patch (is_not_zero_check, code);
3831 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3833 x86_patch (end_jump, code);
3835 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3836 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3838 if (ins->dreg != X86_EAX)
3839 x86_pop_reg (code, X86_EAX);
3842 guchar *unordered_check;
3843 guchar *jump_to_end;
3844 if (cfg->opt & MONO_OPT_FCMOV) {
3845 /* zeroing the register at the start results in
3846 * shorter and faster code (we can also remove the widening op)
3848 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3849 x86_fcomip (code, 1);
3851 unordered_check = code;
3852 x86_branch8 (code, X86_CC_P, 0, FALSE);
3853 x86_set_reg (code, X86_CC_NB, ins->dreg, FALSE);
3854 x86_patch (unordered_check, code);
3857 if (ins->dreg != X86_EAX)
3858 x86_push_reg (code, X86_EAX);
3860 EMIT_FPCOMPARE(code);
3861 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3862 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
3863 unordered_check = code;
3864 x86_branch8 (code, X86_CC_EQ, 0, FALSE);
3866 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3867 x86_set_reg (code, X86_CC_NE, ins->dreg, TRUE);
3868 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3870 x86_jump8 (code, 0);
3871 x86_patch (unordered_check, code);
3872 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3873 x86_patch (jump_to_end, code);
3875 if (ins->dreg != X86_EAX)
3876 x86_pop_reg (code, X86_EAX);
3881 if (cfg->opt & MONO_OPT_FCMOV) {
3882 /* zeroing the register at the start results in
3883 * shorter and faster code (we can also remove the widening op)
3885 guchar *unordered_check;
3886 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3887 x86_fcomip (code, 1);
3889 if (ins->opcode == OP_FCGT) {
3890 unordered_check = code;
3891 x86_branch8 (code, X86_CC_P, 0, FALSE);
3892 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
3893 x86_patch (unordered_check, code);
3895 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
3899 if (ins->dreg != X86_EAX)
3900 x86_push_reg (code, X86_EAX);
3902 EMIT_FPCOMPARE(code);
3903 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3904 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3905 if (ins->opcode == OP_FCGT_UN) {
3906 guchar *is_not_zero_check, *end_jump;
3907 is_not_zero_check = code;
3908 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3910 x86_jump8 (code, 0);
3911 x86_patch (is_not_zero_check, code);
3912 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3914 x86_patch (end_jump, code);
3916 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3917 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3919 if (ins->dreg != X86_EAX)
3920 x86_pop_reg (code, X86_EAX);
3923 guchar *unordered_check;
3924 guchar *jump_to_end;
3925 if (cfg->opt & MONO_OPT_FCMOV) {
3926 /* zeroing the register at the start results in
3927 * shorter and faster code (we can also remove the widening op)
3929 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3930 x86_fcomip (code, 1);
3932 unordered_check = code;
3933 x86_branch8 (code, X86_CC_P, 0, FALSE);
3934 x86_set_reg (code, X86_CC_NA, ins->dreg, FALSE);
3935 x86_patch (unordered_check, code);
3938 if (ins->dreg != X86_EAX)
3939 x86_push_reg (code, X86_EAX);
3941 EMIT_FPCOMPARE(code);
3942 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3943 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
3944 unordered_check = code;
3945 x86_branch8 (code, X86_CC_EQ, 0, FALSE);
3947 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3948 x86_set_reg (code, X86_CC_GE, ins->dreg, TRUE);
3949 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3951 x86_jump8 (code, 0);
3952 x86_patch (unordered_check, code);
3953 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3954 x86_patch (jump_to_end, code);
3956 if (ins->dreg != X86_EAX)
3957 x86_pop_reg (code, X86_EAX);
3961 if (cfg->opt & MONO_OPT_FCMOV) {
3962 guchar *jump = code;
3963 x86_branch8 (code, X86_CC_P, 0, TRUE);
3964 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3965 x86_patch (jump, code);
3968 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
3969 EMIT_COND_BRANCH (ins, X86_CC_EQ, TRUE);
3972 /* Branch if C013 != 100 */
3973 if (cfg->opt & MONO_OPT_FCMOV) {
3974 /* branch if !ZF or (PF|CF) */
3975 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3976 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3977 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
3980 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
3981 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3984 if (cfg->opt & MONO_OPT_FCMOV) {
3985 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
3988 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3991 if (cfg->opt & MONO_OPT_FCMOV) {
3992 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3993 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
3996 if (ins->opcode == OP_FBLT_UN) {
3997 guchar *is_not_zero_check, *end_jump;
3998 is_not_zero_check = code;
3999 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
4001 x86_jump8 (code, 0);
4002 x86_patch (is_not_zero_check, code);
4003 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
4005 x86_patch (end_jump, code);
4007 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4011 if (cfg->opt & MONO_OPT_FCMOV) {
4012 if (ins->opcode == OP_FBGT) {
4015 /* skip branch if C1=1 */
4017 x86_branch8 (code, X86_CC_P, 0, FALSE);
4018 /* branch if (C0 | C3) = 1 */
4019 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
4020 x86_patch (br1, code);
4022 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
4026 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4027 if (ins->opcode == OP_FBGT_UN) {
4028 guchar *is_not_zero_check, *end_jump;
4029 is_not_zero_check = code;
4030 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
4032 x86_jump8 (code, 0);
4033 x86_patch (is_not_zero_check, code);
4034 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
4036 x86_patch (end_jump, code);
4038 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4041 /* Branch if C013 == 100 or 001 */
4042 if (cfg->opt & MONO_OPT_FCMOV) {
4045 /* skip branch if C1=1 */
4047 x86_branch8 (code, X86_CC_P, 0, FALSE);
4048 /* branch if (C0 | C3) = 1 */
4049 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
4050 x86_patch (br1, code);
4053 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4054 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4055 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
4056 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4059 /* Branch if C013 == 000 */
4060 if (cfg->opt & MONO_OPT_FCMOV) {
4061 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
4064 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
4067 /* Branch if C013=000 or 100 */
4068 if (cfg->opt & MONO_OPT_FCMOV) {
4071 /* skip branch if C1=1 */
4073 x86_branch8 (code, X86_CC_P, 0, FALSE);
4074 /* branch if C0=0 */
4075 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
4076 x86_patch (br1, code);
4079 x86_alu_reg_imm (code, X86_AND, X86_EAX, (X86_FP_C0|X86_FP_C1));
4080 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0);
4081 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4084 /* Branch if C013 != 001 */
4085 if (cfg->opt & MONO_OPT_FCMOV) {
4086 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
4087 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
4090 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4091 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
4095 x86_push_reg (code, X86_EAX);
4098 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
4099 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4100 x86_pop_reg (code, X86_EAX);
4102 /* Have to clean up the fp stack before throwing the exception */
4104 x86_branch8 (code, X86_CC_NE, 0, FALSE);
4107 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "OverflowException");
4109 x86_patch (br1, code);
4113 code = mono_x86_emit_tls_get (code, ins->dreg, ins->inst_offset);
4117 code = mono_x86_emit_tls_set (code, ins->sreg1, ins->inst_offset);
4120 case OP_MEMORY_BARRIER: {
4121 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ) {
4122 x86_prefix (code, X86_LOCK_PREFIX);
4123 x86_alu_membase_imm (code, X86_ADD, X86_ESP, 0, 0);
4127 case OP_ATOMIC_ADD_I4: {
4128 int dreg = ins->dreg;
4130 g_assert (cfg->has_atomic_add_i4);
4132 /* hack: limit in regalloc, dreg != sreg1 && dreg != sreg2 */
4133 if (ins->sreg2 == dreg) {
4134 if (dreg == X86_EBX) {
4136 if (ins->inst_basereg == X86_EDI)
4140 if (ins->inst_basereg == X86_EBX)
4143 } else if (ins->inst_basereg == dreg) {
4144 if (dreg == X86_EBX) {
4146 if (ins->sreg2 == X86_EDI)
4150 if (ins->sreg2 == X86_EBX)
4155 if (dreg != ins->dreg) {
4156 x86_push_reg (code, dreg);
4159 x86_mov_reg_reg (code, dreg, ins->sreg2, 4);
4160 x86_prefix (code, X86_LOCK_PREFIX);
4161 x86_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, 4);
4162 /* dreg contains the old value, add with sreg2 value */
4163 x86_alu_reg_reg (code, X86_ADD, dreg, ins->sreg2);
4165 if (ins->dreg != dreg) {
4166 x86_mov_reg_reg (code, ins->dreg, dreg, 4);
4167 x86_pop_reg (code, dreg);
4172 case OP_ATOMIC_EXCHANGE_I4: {
4174 int sreg2 = ins->sreg2;
4175 int breg = ins->inst_basereg;
4177 g_assert (cfg->has_atomic_exchange_i4);
4179 /* cmpxchg uses eax as comperand, need to make sure we can use it
4180 * hack to overcome limits in x86 reg allocator
4181 * (req: dreg == eax and sreg2 != eax and breg != eax)
4183 g_assert (ins->dreg == X86_EAX);
4185 /* We need the EAX reg for the cmpxchg */
4186 if (ins->sreg2 == X86_EAX) {
4187 sreg2 = (breg == X86_EDX) ? X86_EBX : X86_EDX;
4188 x86_push_reg (code, sreg2);
4189 x86_mov_reg_reg (code, sreg2, X86_EAX, 4);
4192 if (breg == X86_EAX) {
4193 breg = (sreg2 == X86_ESI) ? X86_EDI : X86_ESI;
4194 x86_push_reg (code, breg);
4195 x86_mov_reg_reg (code, breg, X86_EAX, 4);
4198 x86_mov_reg_membase (code, X86_EAX, breg, ins->inst_offset, 4);
4200 br [0] = code; x86_prefix (code, X86_LOCK_PREFIX);
4201 x86_cmpxchg_membase_reg (code, breg, ins->inst_offset, sreg2);
4202 br [1] = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
4203 x86_patch (br [1], br [0]);
4205 if (breg != ins->inst_basereg)
4206 x86_pop_reg (code, breg);
4208 if (ins->sreg2 != sreg2)
4209 x86_pop_reg (code, sreg2);
4213 case OP_ATOMIC_CAS_I4: {
4214 g_assert (ins->dreg == X86_EAX);
4215 g_assert (ins->sreg3 == X86_EAX);
4216 g_assert (ins->sreg1 != X86_EAX);
4217 g_assert (ins->sreg1 != ins->sreg2);
4219 x86_prefix (code, X86_LOCK_PREFIX);
4220 x86_cmpxchg_membase_reg (code, ins->sreg1, ins->inst_offset, ins->sreg2);
4223 case OP_ATOMIC_LOAD_I1: {
4224 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
4227 case OP_ATOMIC_LOAD_U1: {
4228 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
4231 case OP_ATOMIC_LOAD_I2: {
4232 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
4235 case OP_ATOMIC_LOAD_U2: {
4236 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
4239 case OP_ATOMIC_LOAD_I4:
4240 case OP_ATOMIC_LOAD_U4: {
4241 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
4244 case OP_ATOMIC_LOAD_R4:
4245 case OP_ATOMIC_LOAD_R8: {
4246 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, ins->opcode == OP_ATOMIC_LOAD_R8);
4249 case OP_ATOMIC_STORE_I1:
4250 case OP_ATOMIC_STORE_U1:
4251 case OP_ATOMIC_STORE_I2:
4252 case OP_ATOMIC_STORE_U2:
4253 case OP_ATOMIC_STORE_I4:
4254 case OP_ATOMIC_STORE_U4: {
4257 switch (ins->opcode) {
4258 case OP_ATOMIC_STORE_I1:
4259 case OP_ATOMIC_STORE_U1:
4262 case OP_ATOMIC_STORE_I2:
4263 case OP_ATOMIC_STORE_U2:
4266 case OP_ATOMIC_STORE_I4:
4267 case OP_ATOMIC_STORE_U4:
4272 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, size);
4274 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
4278 case OP_ATOMIC_STORE_R4:
4279 case OP_ATOMIC_STORE_R8: {
4280 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, ins->opcode == OP_ATOMIC_STORE_R8, TRUE);
4282 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
4286 case OP_CARD_TABLE_WBARRIER: {
4287 int ptr = ins->sreg1;
4288 int value = ins->sreg2;
4290 int nursery_shift, card_table_shift;
4291 gpointer card_table_mask;
4292 size_t nursery_size;
4293 gulong card_table = (gulong)mono_gc_get_card_table (&card_table_shift, &card_table_mask);
4294 gulong nursery_start = (gulong)mono_gc_get_nursery (&nursery_shift, &nursery_size);
4295 gboolean card_table_nursery_check = mono_gc_card_table_nursery_check ();
4298 * We need one register we can clobber, we choose EDX and make sreg1
4299 * fixed EAX to work around limitations in the local register allocator.
4300 * sreg2 might get allocated to EDX, but that is not a problem since
4301 * we use it before clobbering EDX.
4303 g_assert (ins->sreg1 == X86_EAX);
4306 * This is the code we produce:
4309 * edx >>= nursery_shift
4310 * cmp edx, (nursery_start >> nursery_shift)
4313 * edx >>= card_table_shift
4314 * card_table[edx] = 1
4318 if (card_table_nursery_check) {
4319 if (value != X86_EDX)
4320 x86_mov_reg_reg (code, X86_EDX, value, 4);
4321 x86_shift_reg_imm (code, X86_SHR, X86_EDX, nursery_shift);
4322 x86_alu_reg_imm (code, X86_CMP, X86_EDX, nursery_start >> nursery_shift);
4323 br = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
4325 x86_mov_reg_reg (code, X86_EDX, ptr, 4);
4326 x86_shift_reg_imm (code, X86_SHR, X86_EDX, card_table_shift);
4327 if (card_table_mask)
4328 x86_alu_reg_imm (code, X86_AND, X86_EDX, (int)card_table_mask);
4329 x86_mov_membase_imm (code, X86_EDX, card_table, 1, 1);
4330 if (card_table_nursery_check)
4331 x86_patch (br, code);
4334 #ifdef MONO_ARCH_SIMD_INTRINSICS
4336 x86_sse_alu_ps_reg_reg (code, X86_SSE_ADD, ins->sreg1, ins->sreg2);
4339 x86_sse_alu_ps_reg_reg (code, X86_SSE_DIV, ins->sreg1, ins->sreg2);
4342 x86_sse_alu_ps_reg_reg (code, X86_SSE_MUL, ins->sreg1, ins->sreg2);
4345 x86_sse_alu_ps_reg_reg (code, X86_SSE_SUB, ins->sreg1, ins->sreg2);
4348 x86_sse_alu_ps_reg_reg (code, X86_SSE_MAX, ins->sreg1, ins->sreg2);
4351 x86_sse_alu_ps_reg_reg (code, X86_SSE_MIN, ins->sreg1, ins->sreg2);
4354 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
4355 x86_sse_alu_ps_reg_reg_imm (code, X86_SSE_COMP, ins->sreg1, ins->sreg2, ins->inst_c0);
4358 x86_sse_alu_ps_reg_reg (code, X86_SSE_AND, ins->sreg1, ins->sreg2);
4361 x86_sse_alu_ps_reg_reg (code, X86_SSE_ANDN, ins->sreg1, ins->sreg2);
4364 x86_sse_alu_ps_reg_reg (code, X86_SSE_OR, ins->sreg1, ins->sreg2);
4367 x86_sse_alu_ps_reg_reg (code, X86_SSE_XOR, ins->sreg1, ins->sreg2);
4370 x86_sse_alu_ps_reg_reg (code, X86_SSE_SQRT, ins->dreg, ins->sreg1);
4373 x86_sse_alu_ps_reg_reg (code, X86_SSE_RSQRT, ins->dreg, ins->sreg1);
4376 x86_sse_alu_ps_reg_reg (code, X86_SSE_RCP, ins->dreg, ins->sreg1);
4379 x86_sse_alu_sd_reg_reg (code, X86_SSE_ADDSUB, ins->sreg1, ins->sreg2);
4382 x86_sse_alu_sd_reg_reg (code, X86_SSE_HADD, ins->sreg1, ins->sreg2);
4385 x86_sse_alu_sd_reg_reg (code, X86_SSE_HSUB, ins->sreg1, ins->sreg2);
4388 x86_sse_alu_ss_reg_reg (code, X86_SSE_MOVSHDUP, ins->dreg, ins->sreg1);
4391 x86_sse_alu_ss_reg_reg (code, X86_SSE_MOVSLDUP, ins->dreg, ins->sreg1);
4394 case OP_PSHUFLEW_HIGH:
4395 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
4396 x86_pshufw_reg_reg (code, ins->dreg, ins->sreg1, ins->inst_c0, 1);
4398 case OP_PSHUFLEW_LOW:
4399 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
4400 x86_pshufw_reg_reg (code, ins->dreg, ins->sreg1, ins->inst_c0, 0);
4403 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
4404 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->sreg1, ins->inst_c0);
4407 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
4408 x86_sse_alu_reg_reg_imm8 (code, X86_SSE_SHUFP, ins->sreg1, ins->sreg2, ins->inst_c0);
4411 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0x3);
4412 x86_sse_alu_pd_reg_reg_imm8 (code, X86_SSE_SHUFP, ins->sreg1, ins->sreg2, ins->inst_c0);
4416 x86_sse_alu_pd_reg_reg (code, X86_SSE_ADD, ins->sreg1, ins->sreg2);
4419 x86_sse_alu_pd_reg_reg (code, X86_SSE_DIV, ins->sreg1, ins->sreg2);
4422 x86_sse_alu_pd_reg_reg (code, X86_SSE_MUL, ins->sreg1, ins->sreg2);
4425 x86_sse_alu_pd_reg_reg (code, X86_SSE_SUB, ins->sreg1, ins->sreg2);
4428 x86_sse_alu_pd_reg_reg (code, X86_SSE_MAX, ins->sreg1, ins->sreg2);
4431 x86_sse_alu_pd_reg_reg (code, X86_SSE_MIN, ins->sreg1, ins->sreg2);
4434 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
4435 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_COMP, ins->sreg1, ins->sreg2, ins->inst_c0);
4438 x86_sse_alu_pd_reg_reg (code, X86_SSE_AND, ins->sreg1, ins->sreg2);
4441 x86_sse_alu_pd_reg_reg (code, X86_SSE_ANDN, ins->sreg1, ins->sreg2);
4444 x86_sse_alu_pd_reg_reg (code, X86_SSE_OR, ins->sreg1, ins->sreg2);
4447 x86_sse_alu_pd_reg_reg (code, X86_SSE_XOR, ins->sreg1, ins->sreg2);
4450 x86_sse_alu_pd_reg_reg (code, X86_SSE_SQRT, ins->dreg, ins->sreg1);
4453 x86_sse_alu_pd_reg_reg (code, X86_SSE_ADDSUB, ins->sreg1, ins->sreg2);
4456 x86_sse_alu_pd_reg_reg (code, X86_SSE_HADD, ins->sreg1, ins->sreg2);
4459 x86_sse_alu_pd_reg_reg (code, X86_SSE_HSUB, ins->sreg1, ins->sreg2);
4462 x86_sse_alu_sd_reg_reg (code, X86_SSE_MOVDDUP, ins->dreg, ins->sreg1);
4465 case OP_EXTRACT_MASK:
4466 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMOVMSKB, ins->dreg, ins->sreg1);
4470 x86_sse_alu_pd_reg_reg (code, X86_SSE_PAND, ins->sreg1, ins->sreg2);
4473 x86_sse_alu_pd_reg_reg (code, X86_SSE_POR, ins->sreg1, ins->sreg2);
4476 x86_sse_alu_pd_reg_reg (code, X86_SSE_PXOR, ins->sreg1, ins->sreg2);
4480 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDB, ins->sreg1, ins->sreg2);
4483 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDW, ins->sreg1, ins->sreg2);
4486 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDD, ins->sreg1, ins->sreg2);
4489 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDQ, ins->sreg1, ins->sreg2);
4493 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBB, ins->sreg1, ins->sreg2);
4496 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBW, ins->sreg1, ins->sreg2);
4499 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBD, ins->sreg1, ins->sreg2);
4502 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBQ, ins->sreg1, ins->sreg2);
4506 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMAXUB, ins->sreg1, ins->sreg2);
4509 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXUW, ins->sreg1, ins->sreg2);
4512 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXUD, ins->sreg1, ins->sreg2);
4516 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXSB, ins->sreg1, ins->sreg2);
4519 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMAXSW, ins->sreg1, ins->sreg2);
4522 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXSD, ins->sreg1, ins->sreg2);
4526 x86_sse_alu_pd_reg_reg (code, X86_SSE_PAVGB, ins->sreg1, ins->sreg2);
4529 x86_sse_alu_pd_reg_reg (code, X86_SSE_PAVGW, ins->sreg1, ins->sreg2);
4533 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMINUB, ins->sreg1, ins->sreg2);
4536 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINUW, ins->sreg1, ins->sreg2);
4539 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINUD, ins->sreg1, ins->sreg2);
4543 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINSB, ins->sreg1, ins->sreg2);
4546 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMINSW, ins->sreg1, ins->sreg2);
4549 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINSD, ins->sreg1, ins->sreg2);
4553 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQB, ins->sreg1, ins->sreg2);
4556 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQW, ins->sreg1, ins->sreg2);
4559 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQD, ins->sreg1, ins->sreg2);
4562 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PCMPEQQ, ins->sreg1, ins->sreg2);
4566 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPGTB, ins->sreg1, ins->sreg2);
4569 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPGTW, ins->sreg1, ins->sreg2);
4572 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPGTD, ins->sreg1, ins->sreg2);
4575 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PCMPGTQ, ins->sreg1, ins->sreg2);
4578 case OP_PSUM_ABS_DIFF:
4579 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSADBW, ins->sreg1, ins->sreg2);
4582 case OP_UNPACK_LOWB:
4583 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLBW, ins->sreg1, ins->sreg2);
4585 case OP_UNPACK_LOWW:
4586 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLWD, ins->sreg1, ins->sreg2);
4588 case OP_UNPACK_LOWD:
4589 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLDQ, ins->sreg1, ins->sreg2);
4591 case OP_UNPACK_LOWQ:
4592 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLQDQ, ins->sreg1, ins->sreg2);
4594 case OP_UNPACK_LOWPS:
4595 x86_sse_alu_ps_reg_reg (code, X86_SSE_UNPCKL, ins->sreg1, ins->sreg2);
4597 case OP_UNPACK_LOWPD:
4598 x86_sse_alu_pd_reg_reg (code, X86_SSE_UNPCKL, ins->sreg1, ins->sreg2);
4601 case OP_UNPACK_HIGHB:
4602 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHBW, ins->sreg1, ins->sreg2);
4604 case OP_UNPACK_HIGHW:
4605 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHWD, ins->sreg1, ins->sreg2);
4607 case OP_UNPACK_HIGHD:
4608 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHDQ, ins->sreg1, ins->sreg2);
4610 case OP_UNPACK_HIGHQ:
4611 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHQDQ, ins->sreg1, ins->sreg2);
4613 case OP_UNPACK_HIGHPS:
4614 x86_sse_alu_ps_reg_reg (code, X86_SSE_UNPCKH, ins->sreg1, ins->sreg2);
4616 case OP_UNPACK_HIGHPD:
4617 x86_sse_alu_pd_reg_reg (code, X86_SSE_UNPCKH, ins->sreg1, ins->sreg2);
4621 x86_sse_alu_pd_reg_reg (code, X86_SSE_PACKSSWB, ins->sreg1, ins->sreg2);
4624 x86_sse_alu_pd_reg_reg (code, X86_SSE_PACKSSDW, ins->sreg1, ins->sreg2);
4627 x86_sse_alu_pd_reg_reg (code, X86_SSE_PACKUSWB, ins->sreg1, ins->sreg2);
4630 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PACKUSDW, ins->sreg1, ins->sreg2);
4633 case OP_PADDB_SAT_UN:
4634 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDUSB, ins->sreg1, ins->sreg2);
4636 case OP_PSUBB_SAT_UN:
4637 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBUSB, ins->sreg1, ins->sreg2);
4639 case OP_PADDW_SAT_UN:
4640 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDUSW, ins->sreg1, ins->sreg2);
4642 case OP_PSUBW_SAT_UN:
4643 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBUSW, ins->sreg1, ins->sreg2);
4647 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDSB, ins->sreg1, ins->sreg2);
4650 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBSB, ins->sreg1, ins->sreg2);
4653 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDSW, ins->sreg1, ins->sreg2);
4656 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBSW, ins->sreg1, ins->sreg2);
4660 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULLW, ins->sreg1, ins->sreg2);
4663 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMULLD, ins->sreg1, ins->sreg2);
4666 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULUDQ, ins->sreg1, ins->sreg2);
4668 case OP_PMULW_HIGH_UN:
4669 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULHUW, ins->sreg1, ins->sreg2);
4672 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULHW, ins->sreg1, ins->sreg2);
4676 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTW, X86_SSE_SHR, ins->dreg, ins->inst_imm);
4679 x86_sse_shift_reg_reg (code, X86_SSE_PSRLW_REG, ins->dreg, ins->sreg2);
4683 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTW, X86_SSE_SAR, ins->dreg, ins->inst_imm);
4686 x86_sse_shift_reg_reg (code, X86_SSE_PSRAW_REG, ins->dreg, ins->sreg2);
4690 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTW, X86_SSE_SHL, ins->dreg, ins->inst_imm);
4693 x86_sse_shift_reg_reg (code, X86_SSE_PSLLW_REG, ins->dreg, ins->sreg2);
4697 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTD, X86_SSE_SHR, ins->dreg, ins->inst_imm);
4700 x86_sse_shift_reg_reg (code, X86_SSE_PSRLD_REG, ins->dreg, ins->sreg2);
4704 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTD, X86_SSE_SAR, ins->dreg, ins->inst_imm);
4707 x86_sse_shift_reg_reg (code, X86_SSE_PSRAD_REG, ins->dreg, ins->sreg2);
4711 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTD, X86_SSE_SHL, ins->dreg, ins->inst_imm);
4714 x86_sse_shift_reg_reg (code, X86_SSE_PSLLD_REG, ins->dreg, ins->sreg2);
4718 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTQ, X86_SSE_SHR, ins->dreg, ins->inst_imm);
4721 x86_sse_shift_reg_reg (code, X86_SSE_PSRLQ_REG, ins->dreg, ins->sreg2);
4725 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTQ, X86_SSE_SHL, ins->dreg, ins->inst_imm);
4728 x86_sse_shift_reg_reg (code, X86_SSE_PSLLQ_REG, ins->dreg, ins->sreg2);
4732 x86_movd_xreg_reg (code, ins->dreg, ins->sreg1);
4735 x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
4739 x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
4741 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_c0 * 8);
4742 x86_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I1, FALSE);
4746 x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
4748 x86_shift_reg_imm (code, X86_SHR, ins->dreg, 16);
4749 x86_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I2, TRUE);
4753 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);
4755 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);
4756 x86_fld_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE);
4760 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->sreg1, ins->sreg2, ins->inst_c0);
4762 case OP_EXTRACTX_U2:
4763 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PEXTRW, ins->dreg, ins->sreg1, ins->inst_c0);
4765 case OP_INSERTX_U1_SLOW:
4766 /*sreg1 is the extracted ireg (scratch)
4767 /sreg2 is the to be inserted ireg (scratch)
4768 /dreg is the xreg to receive the value*/
4770 /*clear the bits from the extracted word*/
4771 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_c0 & 1 ? 0x00FF : 0xFF00);
4772 /*shift the value to insert if needed*/
4773 if (ins->inst_c0 & 1)
4774 x86_shift_reg_imm (code, X86_SHL, ins->sreg2, 8);
4775 /*join them together*/
4776 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
4777 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, ins->inst_c0 / 2);
4779 case OP_INSERTX_I4_SLOW:
4780 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg2, ins->inst_c0 * 2);
4781 x86_shift_reg_imm (code, X86_SHR, ins->sreg2, 16);
4782 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg2, ins->inst_c0 * 2 + 1);
4785 case OP_INSERTX_R4_SLOW:
4786 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE, TRUE);
4787 /*TODO if inst_c0 == 0 use movss*/
4788 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);
4789 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);
4791 case OP_INSERTX_R8_SLOW:
4792 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
4793 if (cfg->verbose_level)
4794 printf ("CONVERTING a OP_INSERTX_R8_SLOW %d offset %x\n", ins->inst_c0, offset);
4796 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);
4798 x86_movsd_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
4801 case OP_STOREX_MEMBASE_REG:
4802 case OP_STOREX_MEMBASE:
4803 x86_movups_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
4805 case OP_LOADX_MEMBASE:
4806 x86_movups_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
4808 case OP_LOADX_ALIGNED_MEMBASE:
4809 x86_movaps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
4811 case OP_STOREX_ALIGNED_MEMBASE_REG:
4812 x86_movaps_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
4814 case OP_STOREX_NTA_MEMBASE_REG:
4815 x86_sse_alu_reg_membase (code, X86_SSE_MOVNTPS, ins->dreg, ins->sreg1, ins->inst_offset);
4817 case OP_PREFETCH_MEMBASE:
4818 x86_sse_alu_reg_membase (code, X86_SSE_PREFETCH, ins->backend.arg_info, ins->sreg1, ins->inst_offset);
4822 /*FIXME the peephole pass should have killed this*/
4823 if (ins->dreg != ins->sreg1)
4824 x86_movaps_reg_reg (code, ins->dreg, ins->sreg1);
4827 x86_sse_alu_pd_reg_reg (code, X86_SSE_PXOR, ins->dreg, ins->dreg);
4830 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQB, ins->dreg, ins->dreg);
4833 case OP_FCONV_TO_R8_X:
4834 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
4835 x86_movsd_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
4838 case OP_XCONV_R8_TO_I4:
4839 x86_cvttsd2si (code, ins->dreg, ins->sreg1);
4840 switch (ins->backend.source_opcode) {
4841 case OP_FCONV_TO_I1:
4842 x86_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
4844 case OP_FCONV_TO_U1:
4845 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
4847 case OP_FCONV_TO_I2:
4848 x86_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
4850 case OP_FCONV_TO_U2:
4851 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
4857 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 0);
4858 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 1);
4859 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
4862 x86_movd_xreg_reg (code, ins->dreg, ins->sreg1);
4863 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
4866 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE, TRUE);
4867 x86_movd_xreg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
4868 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
4871 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
4872 x86_movsd_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
4873 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0x44);
4877 x86_sse_alu_ss_reg_reg (code, X86_SSE_CVTDQ2PD, ins->dreg, ins->sreg1);
4880 x86_sse_alu_ps_reg_reg (code, X86_SSE_CVTDQ2PS, ins->dreg, ins->sreg1);
4883 x86_sse_alu_sd_reg_reg (code, X86_SSE_CVTPD2DQ, ins->dreg, ins->sreg1);
4886 x86_sse_alu_pd_reg_reg (code, X86_SSE_CVTPD2PS, ins->dreg, ins->sreg1);
4889 x86_sse_alu_pd_reg_reg (code, X86_SSE_CVTPS2DQ, ins->dreg, ins->sreg1);
4892 x86_sse_alu_ps_reg_reg (code, X86_SSE_CVTPS2PD, ins->dreg, ins->sreg1);
4895 x86_sse_alu_pd_reg_reg (code, X86_SSE_CVTTPD2DQ, ins->dreg, ins->sreg1);
4898 x86_sse_alu_ss_reg_reg (code, X86_SSE_CVTTPS2DQ, ins->dreg, ins->sreg1);
4902 case OP_LIVERANGE_START: {
4903 if (cfg->verbose_level > 1)
4904 printf ("R%d START=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
4905 MONO_VARINFO (cfg, ins->inst_c0)->live_range_start = code - cfg->native_code;
4908 case OP_LIVERANGE_END: {
4909 if (cfg->verbose_level > 1)
4910 printf ("R%d END=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
4911 MONO_VARINFO (cfg, ins->inst_c0)->live_range_end = code - cfg->native_code;
4914 case OP_GC_SAFE_POINT: {
4917 g_assert (mono_threads_is_coop_enabled ());
4919 x86_test_membase_imm (code, ins->sreg1, 0, 1);
4920 br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
4921 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_threads_state_poll");
4922 x86_patch (br [0], code);
4926 case OP_GC_LIVENESS_DEF:
4927 case OP_GC_LIVENESS_USE:
4928 case OP_GC_PARAM_SLOT_LIVENESS_DEF:
4929 ins->backend.pc_offset = code - cfg->native_code;
4931 case OP_GC_SPILL_SLOT_LIVENESS_DEF:
4932 ins->backend.pc_offset = code - cfg->native_code;
4933 bb->spill_slot_defs = g_slist_prepend_mempool (cfg->mempool, bb->spill_slot_defs, ins);
4936 x86_mov_reg_reg (code, ins->dreg, X86_ESP, sizeof (mgreg_t));
4939 x86_mov_reg_reg (code, X86_ESP, ins->sreg1, sizeof (mgreg_t));
4942 g_warning ("unknown opcode %s\n", mono_inst_name (ins->opcode));
4943 g_assert_not_reached ();
4946 if (G_UNLIKELY ((code - cfg->native_code - offset) > max_len)) {
4947 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
4948 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
4949 g_assert_not_reached ();
4955 cfg->code_len = code - cfg->native_code;
4958 #endif /* DISABLE_JIT */
4961 mono_arch_register_lowlevel_calls (void)
4966 mono_arch_patch_code_new (MonoCompile *cfg, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gpointer target)
4968 unsigned char *ip = ji->ip.i + code;
4971 case MONO_PATCH_INFO_IP:
4972 *((gconstpointer *)(ip)) = target;
4974 case MONO_PATCH_INFO_ABS:
4975 case MONO_PATCH_INFO_METHOD:
4976 case MONO_PATCH_INFO_METHOD_JUMP:
4977 case MONO_PATCH_INFO_INTERNAL_METHOD:
4978 case MONO_PATCH_INFO_BB:
4979 case MONO_PATCH_INFO_LABEL:
4980 case MONO_PATCH_INFO_RGCTX_FETCH:
4981 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
4982 x86_patch (ip, (unsigned char*)target);
4984 case MONO_PATCH_INFO_NONE:
4986 case MONO_PATCH_INFO_R4:
4987 case MONO_PATCH_INFO_R8: {
4988 guint32 offset = mono_arch_get_patch_offset (ip);
4989 *((gconstpointer *)(ip + offset)) = target;
4993 guint32 offset = mono_arch_get_patch_offset (ip);
4994 *((gconstpointer *)(ip + offset)) = target;
5000 static G_GNUC_UNUSED void
5001 stack_unaligned (MonoMethod *m, gpointer caller)
5003 printf ("%s\n", mono_method_full_name (m, TRUE));
5004 g_assert_not_reached ();
5008 mono_arch_emit_prolog (MonoCompile *cfg)
5010 MonoMethod *method = cfg->method;
5012 MonoMethodSignature *sig;
5016 int alloc_size, pos, max_offset, i, cfa_offset;
5018 gboolean need_stack_frame;
5020 cfg->code_size = MAX (cfg->header->code_size * 4, 10240);
5022 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
5023 cfg->code_size += 512;
5025 code = cfg->native_code = g_malloc (cfg->code_size);
5031 /* Check that the stack is aligned on osx */
5032 x86_mov_reg_reg (code, X86_EAX, X86_ESP, sizeof (mgreg_t));
5033 x86_alu_reg_imm (code, X86_AND, X86_EAX, 15);
5034 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0xc);
5036 x86_branch_disp (code, X86_CC_Z, 0, FALSE);
5037 x86_push_membase (code, X86_ESP, 0);
5038 x86_push_imm (code, cfg->method);
5039 x86_mov_reg_imm (code, X86_EAX, stack_unaligned);
5040 x86_call_reg (code, X86_EAX);
5041 x86_patch (br [0], code);
5045 /* Offset between RSP and the CFA */
5049 cfa_offset = sizeof (gpointer);
5050 mono_emit_unwind_op_def_cfa (cfg, code, X86_ESP, sizeof (gpointer));
5051 // IP saved at CFA - 4
5052 /* There is no IP reg on x86 */
5053 mono_emit_unwind_op_offset (cfg, code, X86_NREG, -cfa_offset);
5054 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
5056 need_stack_frame = needs_stack_frame (cfg);
5058 if (need_stack_frame) {
5059 x86_push_reg (code, X86_EBP);
5060 cfa_offset += sizeof (gpointer);
5061 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
5062 mono_emit_unwind_op_offset (cfg, code, X86_EBP, - cfa_offset);
5063 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
5064 mono_emit_unwind_op_def_cfa_reg (cfg, code, X86_EBP);
5065 /* These are handled automatically by the stack marking code */
5066 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
5068 cfg->frame_reg = X86_ESP;
5071 cfg->stack_offset += cfg->param_area;
5072 cfg->stack_offset = ALIGN_TO (cfg->stack_offset, MONO_ARCH_FRAME_ALIGNMENT);
5074 alloc_size = cfg->stack_offset;
5077 if (!method->save_lmf) {
5078 if (cfg->used_int_regs & (1 << X86_EBX)) {
5079 x86_push_reg (code, X86_EBX);
5081 cfa_offset += sizeof (gpointer);
5082 mono_emit_unwind_op_offset (cfg, code, X86_EBX, - cfa_offset);
5083 /* These are handled automatically by the stack marking code */
5084 mini_gc_set_slot_type_from_cfa (cfg, - cfa_offset, SLOT_NOREF);
5087 if (cfg->used_int_regs & (1 << X86_EDI)) {
5088 x86_push_reg (code, X86_EDI);
5090 cfa_offset += sizeof (gpointer);
5091 mono_emit_unwind_op_offset (cfg, code, X86_EDI, - cfa_offset);
5092 mini_gc_set_slot_type_from_cfa (cfg, - cfa_offset, SLOT_NOREF);
5095 if (cfg->used_int_regs & (1 << X86_ESI)) {
5096 x86_push_reg (code, X86_ESI);
5098 cfa_offset += sizeof (gpointer);
5099 mono_emit_unwind_op_offset (cfg, code, X86_ESI, - cfa_offset);
5100 mini_gc_set_slot_type_from_cfa (cfg, - cfa_offset, SLOT_NOREF);
5106 /* the original alloc_size is already aligned: there is %ebp and retip pushed, so realign */
5107 if (mono_do_x86_stack_align && need_stack_frame) {
5108 int tot = alloc_size + pos + 4; /* ret ip */
5109 if (need_stack_frame)
5111 tot &= MONO_ARCH_FRAME_ALIGNMENT - 1;
5113 alloc_size += MONO_ARCH_FRAME_ALIGNMENT - tot;
5114 for (i = 0; i < MONO_ARCH_FRAME_ALIGNMENT - tot; i += sizeof (mgreg_t))
5115 mini_gc_set_slot_type_from_fp (cfg, - (alloc_size + pos - i), SLOT_NOREF);
5119 cfg->arch.sp_fp_offset = alloc_size + pos;
5122 /* See mono_emit_stack_alloc */
5123 #if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
5124 guint32 remaining_size = alloc_size;
5125 /*FIXME handle unbounded code expansion, we should use a loop in case of more than X interactions*/
5126 guint32 required_code_size = ((remaining_size / 0x1000) + 1) * 8; /*8 is the max size of x86_alu_reg_imm + x86_test_membase_reg*/
5127 guint32 offset = code - cfg->native_code;
5128 if (G_UNLIKELY (required_code_size >= (cfg->code_size - offset))) {
5129 while (required_code_size >= (cfg->code_size - offset))
5130 cfg->code_size *= 2;
5131 cfg->native_code = mono_realloc_native_code(cfg);
5132 code = cfg->native_code + offset;
5133 cfg->stat_code_reallocs++;
5135 while (remaining_size >= 0x1000) {
5136 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
5137 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
5138 remaining_size -= 0x1000;
5141 x86_alu_reg_imm (code, X86_SUB, X86_ESP, remaining_size);
5143 x86_alu_reg_imm (code, X86_SUB, X86_ESP, alloc_size);
5146 g_assert (need_stack_frame);
5149 if (cfg->method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED ||
5150 cfg->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
5151 x86_alu_reg_imm (code, X86_AND, X86_ESP, -MONO_ARCH_FRAME_ALIGNMENT);
5154 #if DEBUG_STACK_ALIGNMENT
5155 /* check the stack is aligned */
5156 if (need_stack_frame && method->wrapper_type == MONO_WRAPPER_NONE) {
5157 x86_mov_reg_reg (code, X86_ECX, X86_ESP, 4);
5158 x86_alu_reg_imm (code, X86_AND, X86_ECX, MONO_ARCH_FRAME_ALIGNMENT - 1);
5159 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
5160 x86_branch_disp (code, X86_CC_EQ, 3, FALSE);
5161 x86_breakpoint (code);
5165 /* compute max_offset in order to use short forward jumps */
5167 if (cfg->opt & MONO_OPT_BRANCH) {
5168 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5170 bb->max_offset = max_offset;
5172 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
5174 /* max alignment for loops */
5175 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
5176 max_offset += LOOP_ALIGNMENT;
5177 MONO_BB_FOR_EACH_INS (bb, ins) {
5178 if (ins->opcode == OP_LABEL)
5179 ins->inst_c1 = max_offset;
5180 max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
5185 /* store runtime generic context */
5186 if (cfg->rgctx_var) {
5187 g_assert (cfg->rgctx_var->opcode == OP_REGOFFSET && cfg->rgctx_var->inst_basereg == X86_EBP);
5189 x86_mov_membase_reg (code, X86_EBP, cfg->rgctx_var->inst_offset, MONO_ARCH_RGCTX_REG, 4);
5192 if (method->save_lmf)
5193 code = emit_setup_lmf (cfg, code, cfg->lmf_var->inst_offset, cfa_offset);
5195 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
5196 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
5201 if (cfg->arch.ss_tramp_var) {
5202 /* Initialize ss_tramp_var */
5203 ins = cfg->arch.ss_tramp_var;
5204 g_assert (ins->opcode == OP_REGOFFSET);
5206 g_assert (!cfg->compile_aot);
5207 x86_mov_membase_imm (code, ins->inst_basereg, ins->inst_offset, (guint32)&ss_trampoline, 4);
5210 if (cfg->arch.bp_tramp_var) {
5211 /* Initialize bp_tramp_var */
5212 ins = cfg->arch.bp_tramp_var;
5213 g_assert (ins->opcode == OP_REGOFFSET);
5215 g_assert (!cfg->compile_aot);
5216 x86_mov_membase_imm (code, ins->inst_basereg, ins->inst_offset, (guint32)&bp_trampoline, 4);
5220 /* load arguments allocated to register from the stack */
5221 sig = mono_method_signature (method);
5224 cinfo = (CallInfo *)cfg->arch.cinfo;
5226 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
5227 inst = cfg->args [pos];
5228 ainfo = &cinfo->args [pos];
5229 if (inst->opcode == OP_REGVAR) {
5230 g_assert (need_stack_frame);
5231 x86_mov_reg_membase (code, inst->dreg, X86_EBP, ainfo->offset + ARGS_OFFSET, 4);
5232 if (cfg->verbose_level > 2)
5233 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
5238 cfg->code_len = code - cfg->native_code;
5240 g_assert (cfg->code_len < cfg->code_size);
5246 mono_arch_emit_epilog (MonoCompile *cfg)
5248 MonoMethod *method = cfg->method;
5249 MonoMethodSignature *sig = mono_method_signature (method);
5251 guint32 stack_to_pop;
5253 int max_epilog_size = 16;
5255 gboolean need_stack_frame = needs_stack_frame (cfg);
5257 if (cfg->method->save_lmf)
5258 max_epilog_size += 128;
5260 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
5261 cfg->code_size *= 2;
5262 cfg->native_code = mono_realloc_native_code(cfg);
5263 cfg->stat_code_reallocs++;
5266 code = cfg->native_code + cfg->code_len;
5268 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
5269 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
5271 /* the code restoring the registers must be kept in sync with OP_TAILCALL */
5274 if (method->save_lmf) {
5275 gint32 lmf_offset = cfg->lmf_var->inst_offset;
5278 /* check if we need to restore protection of the stack after a stack overflow */
5279 if (!cfg->compile_aot && mono_arch_have_fast_tls () && mono_tls_get_tls_offset (TLS_KEY_JIT_TLS) != -1) {
5280 code = mono_x86_emit_tls_get (code, X86_ECX, mono_tls_get_tls_offset (TLS_KEY_JIT_TLS));
5282 /* we load the value in a separate instruction: this mechanism may be
5283 * used later as a safer way to do thread interruption
5285 x86_mov_reg_membase (code, X86_ECX, X86_ECX, MONO_STRUCT_OFFSET (MonoJitTlsData, restore_stack_prot), 4);
5286 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
5288 x86_branch8 (code, X86_CC_Z, 0, FALSE);
5289 /* note that the call trampoline will preserve eax/edx */
5290 x86_call_reg (code, X86_ECX);
5291 x86_patch (patch, code);
5294 /* restore caller saved regs */
5295 if (cfg->used_int_regs & (1 << X86_EBX)) {
5296 x86_mov_reg_membase (code, X86_EBX, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, ebx), 4);
5299 if (cfg->used_int_regs & (1 << X86_EDI)) {
5300 x86_mov_reg_membase (code, X86_EDI, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, edi), 4);
5302 if (cfg->used_int_regs & (1 << X86_ESI)) {
5303 x86_mov_reg_membase (code, X86_ESI, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, esi), 4);
5306 /* EBP is restored by LEAVE */
5308 for (i = 0; i < X86_NREG; ++i) {
5309 if ((cfg->used_int_regs & X86_CALLER_REGS & (1 << i)) && (i != X86_EBP)) {
5315 g_assert (need_stack_frame);
5316 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
5320 g_assert (need_stack_frame);
5321 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
5324 if (cfg->used_int_regs & (1 << X86_ESI)) {
5325 x86_pop_reg (code, X86_ESI);
5327 if (cfg->used_int_regs & (1 << X86_EDI)) {
5328 x86_pop_reg (code, X86_EDI);
5330 if (cfg->used_int_regs & (1 << X86_EBX)) {
5331 x86_pop_reg (code, X86_EBX);
5335 /* Load returned vtypes into registers if needed */
5336 cinfo = (CallInfo *)cfg->arch.cinfo;
5337 if (cinfo->ret.storage == ArgValuetypeInReg) {
5338 for (quad = 0; quad < 2; quad ++) {
5339 switch (cinfo->ret.pair_storage [quad]) {
5341 x86_mov_reg_membase (code, cinfo->ret.pair_regs [quad], cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), 4);
5343 case ArgOnFloatFpStack:
5344 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), FALSE);
5346 case ArgOnDoubleFpStack:
5347 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), TRUE);
5352 g_assert_not_reached ();
5357 if (need_stack_frame)
5360 if (CALLCONV_IS_STDCALL (sig)) {
5361 MonoJitArgumentInfo *arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
5363 stack_to_pop = mono_arch_get_argument_info (sig, sig->param_count, arg_info);
5364 } else if (cinfo->callee_stack_pop)
5365 stack_to_pop = cinfo->callee_stack_pop;
5370 g_assert (need_stack_frame);
5371 x86_ret_imm (code, stack_to_pop);
5376 cfg->code_len = code - cfg->native_code;
5378 g_assert (cfg->code_len < cfg->code_size);
5382 mono_arch_emit_exceptions (MonoCompile *cfg)
5384 MonoJumpInfo *patch_info;
5387 MonoClass *exc_classes [16];
5388 guint8 *exc_throw_start [16], *exc_throw_end [16];
5392 /* Compute needed space */
5393 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5394 if (patch_info->type == MONO_PATCH_INFO_EXC)
5399 * make sure we have enough space for exceptions
5400 * 16 is the size of two push_imm instructions and a call
5402 if (cfg->compile_aot)
5403 code_size = exc_count * 32;
5405 code_size = exc_count * 16;
5407 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
5408 cfg->code_size *= 2;
5409 cfg->native_code = mono_realloc_native_code(cfg);
5410 cfg->stat_code_reallocs++;
5413 code = cfg->native_code + cfg->code_len;
5416 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5417 switch (patch_info->type) {
5418 case MONO_PATCH_INFO_EXC: {
5419 MonoClass *exc_class;
5423 x86_patch (patch_info->ip.i + cfg->native_code, code);
5425 exc_class = mono_class_load_from_name (mono_defaults.corlib, "System", patch_info->data.name);
5426 throw_ip = patch_info->ip.i;
5428 /* Find a throw sequence for the same exception class */
5429 for (i = 0; i < nthrows; ++i)
5430 if (exc_classes [i] == exc_class)
5433 x86_push_imm (code, (exc_throw_end [i] - cfg->native_code) - throw_ip);
5434 x86_jump_code (code, exc_throw_start [i]);
5435 patch_info->type = MONO_PATCH_INFO_NONE;
5440 /* Compute size of code following the push <OFFSET> */
5443 /*This is aligned to 16 bytes by the callee. This way we save a few bytes here.*/
5445 if ((code - cfg->native_code) - throw_ip < 126 - size) {
5446 /* Use the shorter form */
5448 x86_push_imm (code, 0);
5452 x86_push_imm (code, 0xf0f0f0f0);
5457 exc_classes [nthrows] = exc_class;
5458 exc_throw_start [nthrows] = code;
5461 x86_push_imm (code, exc_class->type_token - MONO_TOKEN_TYPE_DEF);
5462 patch_info->data.name = "mono_arch_throw_corlib_exception";
5463 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
5464 patch_info->ip.i = code - cfg->native_code;
5465 x86_call_code (code, 0);
5466 x86_push_imm (buf, (code - cfg->native_code) - throw_ip);
5471 exc_throw_end [nthrows] = code;
5483 cfg->code_len = code - cfg->native_code;
5485 g_assert (cfg->code_len < cfg->code_size);
5489 mono_arch_flush_icache (guint8 *code, gint size)
5495 mono_arch_flush_register_windows (void)
5500 mono_arch_is_inst_imm (gint64 imm)
5506 mono_arch_finish_init (void)
5508 char *mono_no_tls = g_getenv ("MONO_NO_TLS");
5510 #ifndef TARGET_WIN32
5512 optimize_for_xen = access ("/proc/xen", F_OK) == 0;
5516 g_free (mono_no_tls);
5521 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
5525 // Linear handler, the bsearch head compare is shorter
5526 //[2 + 4] x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
5527 //[1 + 1] x86_branch8(inst,cond,imm,is_signed)
5528 // x86_patch(ins,target)
5529 //[1 + 5] x86_jump_mem(inst,mem)
5532 #define BR_SMALL_SIZE 2
5533 #define BR_LARGE_SIZE 5
5534 #define JUMP_IMM_SIZE 6
5535 #define ENABLE_WRONG_METHOD_CHECK 0
5539 imt_branch_distance (MonoIMTCheckItem **imt_entries, int start, int target)
5541 int i, distance = 0;
5542 for (i = start; i < target; ++i)
5543 distance += imt_entries [i]->chunk_size;
5548 * LOCKING: called with the domain lock held
5551 mono_arch_build_imt_trampoline (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
5552 gpointer fail_tramp)
5556 guint8 *code, *start;
5559 for (i = 0; i < count; ++i) {
5560 MonoIMTCheckItem *item = imt_entries [i];
5561 if (item->is_equals) {
5562 if (item->check_target_idx) {
5563 if (!item->compare_done)
5564 item->chunk_size += CMP_SIZE;
5565 item->chunk_size += BR_SMALL_SIZE + JUMP_IMM_SIZE;
5568 item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + JUMP_IMM_SIZE * 2;
5570 item->chunk_size += JUMP_IMM_SIZE;
5571 #if ENABLE_WRONG_METHOD_CHECK
5572 item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
5577 item->chunk_size += CMP_SIZE + BR_LARGE_SIZE;
5578 imt_entries [item->check_target_idx]->compare_done = TRUE;
5580 size += item->chunk_size;
5583 code = mono_method_alloc_generic_virtual_trampoline (domain, size);
5585 code = mono_domain_code_reserve (domain, size);
5588 unwind_ops = mono_arch_get_cie_program ();
5590 for (i = 0; i < count; ++i) {
5591 MonoIMTCheckItem *item = imt_entries [i];
5592 item->code_target = code;
5593 if (item->is_equals) {
5594 if (item->check_target_idx) {
5595 if (!item->compare_done)
5596 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5597 item->jmp_code = code;
5598 x86_branch8 (code, X86_CC_NE, 0, FALSE);
5599 if (item->has_target_code)
5600 x86_jump_code (code, item->value.target_code);
5602 x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
5605 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5606 item->jmp_code = code;
5607 x86_branch8 (code, X86_CC_NE, 0, FALSE);
5608 if (item->has_target_code)
5609 x86_jump_code (code, item->value.target_code);
5611 x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
5612 x86_patch (item->jmp_code, code);
5613 x86_jump_code (code, fail_tramp);
5614 item->jmp_code = NULL;
5616 /* enable the commented code to assert on wrong method */
5617 #if ENABLE_WRONG_METHOD_CHECK
5618 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5619 item->jmp_code = code;
5620 x86_branch8 (code, X86_CC_NE, 0, FALSE);
5622 if (item->has_target_code)
5623 x86_jump_code (code, item->value.target_code);
5625 x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
5626 #if ENABLE_WRONG_METHOD_CHECK
5627 x86_patch (item->jmp_code, code);
5628 x86_breakpoint (code);
5629 item->jmp_code = NULL;
5634 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5635 item->jmp_code = code;
5636 if (x86_is_imm8 (imt_branch_distance (imt_entries, i, item->check_target_idx)))
5637 x86_branch8 (code, X86_CC_GE, 0, FALSE);
5639 x86_branch32 (code, X86_CC_GE, 0, FALSE);
5642 /* patch the branches to get to the target items */
5643 for (i = 0; i < count; ++i) {
5644 MonoIMTCheckItem *item = imt_entries [i];
5645 if (item->jmp_code) {
5646 if (item->check_target_idx) {
5647 x86_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
5653 mono_stats.imt_trampolines_size += code - start;
5654 g_assert (code - start <= size);
5658 char *buff = g_strdup_printf ("thunk_for_class_%s_%s_entries_%d", vtable->klass->name_space, vtable->klass->name, count);
5659 mono_disassemble_code (NULL, (guint8*)start, code - start, buff);
5663 if (mono_jit_map_is_enabled ()) {
5666 buff = g_strdup_printf ("imt_%s_%s_entries_%d", vtable->klass->name_space, vtable->klass->name, count);
5668 buff = g_strdup_printf ("imt_trampoline_entries_%d", count);
5669 mono_emit_jit_tramp (start, code - start, buff);
5673 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE, NULL);
5675 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
5681 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
5683 return (MonoMethod*) regs [MONO_ARCH_IMT_REG];
5687 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
5689 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
5693 mono_arch_get_cie_program (void)
5697 mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
5698 mono_add_unwind_op_offset (l, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
5704 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5706 MonoInst *ins = NULL;
5709 if (cmethod->klass == mono_defaults.math_class) {
5710 if (strcmp (cmethod->name, "Sin") == 0) {
5712 } else if (strcmp (cmethod->name, "Cos") == 0) {
5714 } else if (strcmp (cmethod->name, "Tan") == 0) {
5716 } else if (strcmp (cmethod->name, "Atan") == 0) {
5718 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
5720 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
5722 } else if (strcmp (cmethod->name, "Round") == 0 && fsig->param_count == 1 && fsig->params [0]->type == MONO_TYPE_R8) {
5726 if (opcode && fsig->param_count == 1) {
5727 MONO_INST_NEW (cfg, ins, opcode);
5728 ins->type = STACK_R8;
5729 ins->dreg = mono_alloc_freg (cfg);
5730 ins->sreg1 = args [0]->dreg;
5731 MONO_ADD_INS (cfg->cbb, ins);
5734 if (cfg->opt & MONO_OPT_CMOV) {
5737 if (strcmp (cmethod->name, "Min") == 0) {
5738 if (fsig->params [0]->type == MONO_TYPE_I4)
5740 } else if (strcmp (cmethod->name, "Max") == 0) {
5741 if (fsig->params [0]->type == MONO_TYPE_I4)
5745 if (opcode && fsig->param_count == 2) {
5746 MONO_INST_NEW (cfg, ins, opcode);
5747 ins->type = STACK_I4;
5748 ins->dreg = mono_alloc_ireg (cfg);
5749 ins->sreg1 = args [0]->dreg;
5750 ins->sreg2 = args [1]->dreg;
5751 MONO_ADD_INS (cfg->cbb, ins);
5756 /* OP_FREM is not IEEE compatible */
5757 else if (strcmp (cmethod->name, "IEEERemainder") == 0 && fsig->param_count == 2) {
5758 MONO_INST_NEW (cfg, ins, OP_FREM);
5759 ins->inst_i0 = args [0];
5760 ins->inst_i1 = args [1];
5769 mono_arch_print_tree (MonoInst *tree, int arity)
5775 mono_arch_get_patch_offset (guint8 *code)
5777 if ((code [0] == 0x8b) && (x86_modrm_mod (code [1]) == 0x2))
5779 else if (code [0] == 0xba)
5781 else if (code [0] == 0x68)
5784 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x6))
5785 /* push <OFFSET>(<REG>) */
5787 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x2))
5788 /* call *<OFFSET>(<REG>) */
5790 else if ((code [0] == 0xdd) || (code [0] == 0xd9))
5793 else if ((code [0] == 0x58) && (code [1] == 0x05))
5794 /* pop %eax; add <OFFSET>, %eax */
5796 else if ((code [0] >= 0x58) && (code [0] <= 0x58 + X86_NREG) && (code [1] == 0x81))
5797 /* pop <REG>; add <OFFSET>, <REG> */
5799 else if ((code [0] >= 0xb8) && (code [0] < 0xb8 + 8))
5800 /* mov <REG>, imm */
5803 g_assert_not_reached ();
5809 * \return TRUE if no sw breakpoint was present.
5811 * Copy \p size bytes from \p code - \p offset to the buffer \p buf. If the debugger inserted software
5812 * breakpoints in the original code, they are removed in the copy.
5815 mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size)
5818 * If method_start is non-NULL we need to perform bound checks, since we access memory
5819 * at code - offset we could go before the start of the method and end up in a different
5820 * page of memory that is not mapped or read incorrect data anyway. We zero-fill the bytes
5823 if (!method_start || code - offset >= method_start) {
5824 memcpy (buf, code - offset, size);
5826 int diff = code - method_start;
5827 memset (buf, 0, size);
5828 memcpy (buf + offset - diff, method_start, diff + size - offset);
5834 * mono_x86_get_this_arg_offset:
5836 * Return the offset of the stack location where this is passed during a virtual
5840 mono_x86_get_this_arg_offset (MonoMethodSignature *sig)
5846 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
5848 guint32 esp = regs [X86_ESP];
5855 * The stack looks like:
5859 res = ((MonoObject**)esp) [0];
5863 #define MAX_ARCH_DELEGATE_PARAMS 10
5866 get_delegate_invoke_impl (MonoTrampInfo **info, gboolean has_target, guint32 param_count)
5868 guint8 *code, *start;
5869 int code_reserve = 64;
5872 unwind_ops = mono_arch_get_cie_program ();
5875 * The stack contains:
5881 start = code = mono_global_codeman_reserve (code_reserve);
5883 /* Replace the this argument with the target */
5884 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
5885 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 4);
5886 x86_mov_membase_reg (code, X86_ESP, 4, X86_ECX, 4);
5887 x86_jump_membase (code, X86_EAX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
5889 g_assert ((code - start) < code_reserve);
5892 /* 8 for mov_reg and jump, plus 8 for each parameter */
5893 code_reserve = 8 + (param_count * 8);
5895 * The stack contains:
5896 * <args in reverse order>
5901 * <args in reverse order>
5904 * without unbalancing the stack.
5905 * So move each arg up a spot in the stack (overwriting un-needed 'this' arg)
5906 * and leaving original spot of first arg as placeholder in stack so
5907 * when callee pops stack everything works.
5910 start = code = mono_global_codeman_reserve (code_reserve);
5912 /* store delegate for access to method_ptr */
5913 x86_mov_reg_membase (code, X86_ECX, X86_ESP, 4, 4);
5916 for (i = 0; i < param_count; ++i) {
5917 x86_mov_reg_membase (code, X86_EAX, X86_ESP, (i+2)*4, 4);
5918 x86_mov_membase_reg (code, X86_ESP, (i+1)*4, X86_EAX, 4);
5921 x86_jump_membase (code, X86_ECX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
5923 g_assert ((code - start) < code_reserve);
5927 *info = mono_tramp_info_create ("delegate_invoke_impl_has_target", start, code - start, NULL, unwind_ops);
5929 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", param_count);
5930 *info = mono_tramp_info_create (name, start, code - start, NULL, unwind_ops);
5934 if (mono_jit_map_is_enabled ()) {
5937 buff = (char*)"delegate_invoke_has_target";
5939 buff = g_strdup_printf ("delegate_invoke_no_target_%d", param_count);
5940 mono_emit_jit_tramp (start, code - start, buff);
5944 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
5949 #define MAX_VIRTUAL_DELEGATE_OFFSET 32
5952 get_delegate_virtual_invoke_impl (MonoTrampInfo **info, gboolean load_imt_reg, int offset)
5954 guint8 *code, *start;
5959 if (offset / (int)sizeof (gpointer) > MAX_VIRTUAL_DELEGATE_OFFSET)
5963 * The stack contains:
5967 start = code = mono_global_codeman_reserve (size);
5969 unwind_ops = mono_arch_get_cie_program ();
5971 /* Replace the this argument with the target */
5972 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
5973 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 4);
5974 x86_mov_membase_reg (code, X86_ESP, 4, X86_ECX, 4);
5977 /* Load the IMT reg */
5978 x86_mov_reg_membase (code, MONO_ARCH_IMT_REG, X86_EAX, MONO_STRUCT_OFFSET (MonoDelegate, method), 4);
5981 /* Load the vtable */
5982 x86_mov_reg_membase (code, X86_EAX, X86_ECX, MONO_STRUCT_OFFSET (MonoObject, vtable), 4);
5983 x86_jump_membase (code, X86_EAX, offset);
5984 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
5986 tramp_name = mono_get_delegate_virtual_invoke_impl_name (load_imt_reg, offset);
5987 *info = mono_tramp_info_create (tramp_name, start, code - start, NULL, unwind_ops);
5988 g_free (tramp_name);
5995 mono_arch_get_delegate_invoke_impls (void)
5998 MonoTrampInfo *info;
6001 get_delegate_invoke_impl (&info, TRUE, 0);
6002 res = g_slist_prepend (res, info);
6004 for (i = 0; i <= MAX_ARCH_DELEGATE_PARAMS; ++i) {
6005 get_delegate_invoke_impl (&info, FALSE, i);
6006 res = g_slist_prepend (res, info);
6009 for (i = 0; i <= MAX_VIRTUAL_DELEGATE_OFFSET; ++i) {
6010 get_delegate_virtual_invoke_impl (&info, TRUE, - i * SIZEOF_VOID_P);
6011 res = g_slist_prepend (res, info);
6013 get_delegate_virtual_invoke_impl (&info, FALSE, i * SIZEOF_VOID_P);
6014 res = g_slist_prepend (res, info);
6021 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
6023 guint8 *code, *start;
6025 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
6028 /* FIXME: Support more cases */
6029 if (MONO_TYPE_ISSTRUCT (sig->ret))
6033 * The stack contains:
6039 static guint8* cached = NULL;
6043 if (mono_aot_only) {
6044 start = mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
6046 MonoTrampInfo *info;
6047 start = get_delegate_invoke_impl (&info, TRUE, 0);
6048 mono_tramp_info_register (info, NULL);
6051 mono_memory_barrier ();
6055 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
6058 for (i = 0; i < sig->param_count; ++i)
6059 if (!mono_is_regsize_var (sig->params [i]))
6062 code = cache [sig->param_count];
6066 if (mono_aot_only) {
6067 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
6068 start = mono_aot_get_trampoline (name);
6071 MonoTrampInfo *info;
6072 start = get_delegate_invoke_impl (&info, FALSE, sig->param_count);
6073 mono_tramp_info_register (info, NULL);
6076 mono_memory_barrier ();
6078 cache [sig->param_count] = start;
6085 mono_arch_get_delegate_virtual_invoke_impl (MonoMethodSignature *sig, MonoMethod *method, int offset, gboolean load_imt_reg)
6087 MonoTrampInfo *info;
6090 code = get_delegate_virtual_invoke_impl (&info, load_imt_reg, offset);
6092 mono_tramp_info_register (info, NULL);
6097 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
6100 case X86_EAX: return ctx->eax;
6101 case X86_EBX: return ctx->ebx;
6102 case X86_ECX: return ctx->ecx;
6103 case X86_EDX: return ctx->edx;
6104 case X86_ESP: return ctx->esp;
6105 case X86_EBP: return ctx->ebp;
6106 case X86_ESI: return ctx->esi;
6107 case X86_EDI: return ctx->edi;
6109 g_assert_not_reached ();
6115 mono_arch_context_set_int_reg (MonoContext *ctx, int reg, mgreg_t val)
6143 g_assert_not_reached ();
6147 #ifdef MONO_ARCH_SIMD_INTRINSICS
6150 get_float_to_x_spill_area (MonoCompile *cfg)
6152 if (!cfg->fconv_to_r8_x_var) {
6153 cfg->fconv_to_r8_x_var = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
6154 cfg->fconv_to_r8_x_var->flags |= MONO_INST_VOLATILE; /*FIXME, use the don't regalloc flag*/
6156 return cfg->fconv_to_r8_x_var;
6160 * Convert all fconv opts that MONO_OPT_SSE2 would get wrong.
6163 mono_arch_decompose_opts (MonoCompile *cfg, MonoInst *ins)
6166 int dreg, src_opcode;
6168 if (!(cfg->opt & MONO_OPT_SSE2) || !(cfg->opt & MONO_OPT_SIMD) || COMPILE_LLVM (cfg))
6171 switch (src_opcode = ins->opcode) {
6172 case OP_FCONV_TO_I1:
6173 case OP_FCONV_TO_U1:
6174 case OP_FCONV_TO_I2:
6175 case OP_FCONV_TO_U2:
6176 case OP_FCONV_TO_I4:
6183 /* dreg is the IREG and sreg1 is the FREG */
6184 MONO_INST_NEW (cfg, fconv, OP_FCONV_TO_R8_X);
6185 fconv->klass = NULL; /*FIXME, what can I use here as the Mono.Simd lib might not be loaded yet*/
6186 fconv->sreg1 = ins->sreg1;
6187 fconv->dreg = mono_alloc_ireg (cfg);
6188 fconv->type = STACK_VTYPE;
6189 fconv->backend.spill_var = get_float_to_x_spill_area (cfg);
6191 mono_bblock_insert_before_ins (cfg->cbb, ins, fconv);
6195 ins->opcode = OP_XCONV_R8_TO_I4;
6197 ins->klass = mono_defaults.int32_class;
6198 ins->sreg1 = fconv->dreg;
6200 ins->type = STACK_I4;
6201 ins->backend.source_opcode = src_opcode;
6204 #endif /* #ifdef MONO_ARCH_SIMD_INTRINSICS */
6207 mono_arch_decompose_long_opts (MonoCompile *cfg, MonoInst *long_ins)
6212 if (long_ins->opcode == OP_LNEG) {
6214 MONO_EMIT_NEW_UNALU (cfg, OP_INEG, MONO_LVREG_LS (ins->dreg), MONO_LVREG_LS (ins->sreg1));
6215 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADC_IMM, MONO_LVREG_MS (ins->dreg), MONO_LVREG_MS (ins->sreg1), 0);
6216 MONO_EMIT_NEW_UNALU (cfg, OP_INEG, MONO_LVREG_MS (ins->dreg), MONO_LVREG_MS (ins->dreg));
6221 #ifdef MONO_ARCH_SIMD_INTRINSICS
6223 if (!(cfg->opt & MONO_OPT_SIMD))
6226 /*TODO move this to simd-intrinsic.c once we support sse 4.1 dword extractors since we need the runtime caps info */
6227 switch (long_ins->opcode) {
6229 vreg = long_ins->sreg1;
6231 if (long_ins->inst_c0) {
6232 MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
6233 ins->klass = long_ins->klass;
6234 ins->sreg1 = long_ins->sreg1;
6236 ins->type = STACK_VTYPE;
6237 ins->dreg = vreg = alloc_ireg (cfg);
6238 MONO_ADD_INS (cfg->cbb, ins);
6241 MONO_INST_NEW (cfg, ins, OP_EXTRACT_I4);
6242 ins->klass = mono_defaults.int32_class;
6244 ins->type = STACK_I4;
6245 ins->dreg = MONO_LVREG_LS (long_ins->dreg);
6246 MONO_ADD_INS (cfg->cbb, ins);
6248 MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
6249 ins->klass = long_ins->klass;
6250 ins->sreg1 = long_ins->sreg1;
6251 ins->inst_c0 = long_ins->inst_c0 ? 3 : 1;
6252 ins->type = STACK_VTYPE;
6253 ins->dreg = vreg = alloc_ireg (cfg);
6254 MONO_ADD_INS (cfg->cbb, ins);
6256 MONO_INST_NEW (cfg, ins, OP_EXTRACT_I4);
6257 ins->klass = mono_defaults.int32_class;
6259 ins->type = STACK_I4;
6260 ins->dreg = MONO_LVREG_MS (long_ins->dreg);
6261 MONO_ADD_INS (cfg->cbb, ins);
6263 long_ins->opcode = OP_NOP;
6265 case OP_INSERTX_I8_SLOW:
6266 MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
6267 ins->dreg = long_ins->dreg;
6268 ins->sreg1 = long_ins->dreg;
6269 ins->sreg2 = MONO_LVREG_LS (long_ins->sreg2);
6270 ins->inst_c0 = long_ins->inst_c0 * 2;
6271 MONO_ADD_INS (cfg->cbb, ins);
6273 MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
6274 ins->dreg = long_ins->dreg;
6275 ins->sreg1 = long_ins->dreg;
6276 ins->sreg2 = MONO_LVREG_MS (long_ins->sreg2);
6277 ins->inst_c0 = long_ins->inst_c0 * 2 + 1;
6278 MONO_ADD_INS (cfg->cbb, ins);
6280 long_ins->opcode = OP_NOP;
6283 MONO_INST_NEW (cfg, ins, OP_ICONV_TO_X);
6284 ins->dreg = long_ins->dreg;
6285 ins->sreg1 = MONO_LVREG_LS (long_ins->sreg1);
6286 ins->klass = long_ins->klass;
6287 ins->type = STACK_VTYPE;
6288 MONO_ADD_INS (cfg->cbb, ins);
6290 MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
6291 ins->dreg = long_ins->dreg;
6292 ins->sreg1 = long_ins->dreg;
6293 ins->sreg2 = MONO_LVREG_MS (long_ins->sreg1);
6295 ins->klass = long_ins->klass;
6296 ins->type = STACK_VTYPE;
6297 MONO_ADD_INS (cfg->cbb, ins);
6299 MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
6300 ins->dreg = long_ins->dreg;
6301 ins->sreg1 = long_ins->dreg;;
6302 ins->inst_c0 = 0x44; /*Magic number for swizzling (X,Y,X,Y)*/
6303 ins->klass = long_ins->klass;
6304 ins->type = STACK_VTYPE;
6305 MONO_ADD_INS (cfg->cbb, ins);
6307 long_ins->opcode = OP_NOP;
6310 #endif /* MONO_ARCH_SIMD_INTRINSICS */
6313 /*MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD*/
6315 mono_arch_install_handler_block_guard (MonoJitInfo *ji, MonoJitExceptionInfo *clause, MonoContext *ctx, gpointer new_value)
6318 gpointer *sp, old_value;
6321 offset = clause->exvar_offset;
6324 bp = MONO_CONTEXT_GET_BP (ctx);
6325 sp = *(gpointer*)(bp + offset);
6328 if (old_value < ji->code_start || (char*)old_value > ((char*)ji->code_start + ji->code_size))
6337 * mono_aot_emit_load_got_addr:
6339 * Emit code to load the got address.
6340 * On x86, the result is placed into EBX.
6343 mono_arch_emit_load_got_addr (guint8 *start, guint8 *code, MonoCompile *cfg, MonoJumpInfo **ji)
6345 x86_call_imm (code, 0);
6347 * The patch needs to point to the pop, since the GOT offset needs
6348 * to be added to that address.
6351 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GOT_OFFSET, NULL);
6353 *ji = mono_patch_info_list_prepend (*ji, code - start, MONO_PATCH_INFO_GOT_OFFSET, NULL);
6354 x86_pop_reg (code, MONO_ARCH_GOT_REG);
6355 x86_alu_reg_imm (code, X86_ADD, MONO_ARCH_GOT_REG, 0xf0f0f0f0);
6361 emit_load_aotconst (guint8 *start, guint8 *code, MonoCompile *cfg, MonoJumpInfo **ji, int dreg, int tramp_type, gconstpointer target)
6364 mono_add_patch_info (cfg, code - cfg->native_code, tramp_type, target);
6366 g_assert_not_reached ();
6367 x86_mov_reg_membase (code, dreg, MONO_ARCH_GOT_REG, 0xf0f0f0f0, 4);
6372 * mono_arch_emit_load_aotconst:
6374 * Emit code to load the contents of the GOT slot identified by TRAMP_TYPE and
6375 * TARGET from the mscorlib GOT in full-aot code.
6376 * On x86, the GOT address is assumed to be in EBX, and the result is placed into
6380 mono_arch_emit_load_aotconst (guint8 *start, guint8 *code, MonoJumpInfo **ji, MonoJumpInfoType tramp_type, gconstpointer target)
6382 /* Load the mscorlib got address */
6383 x86_mov_reg_membase (code, X86_EAX, MONO_ARCH_GOT_REG, sizeof (gpointer), 4);
6384 *ji = mono_patch_info_list_prepend (*ji, code - start, tramp_type, target);
6385 /* arch_emit_got_access () patches this */
6386 x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0xf0f0f0f0, 4);
6391 /* Can't put this into mini-x86.h */
6393 mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot);
6396 mono_arch_get_trampolines (gboolean aot)
6398 MonoTrampInfo *info;
6399 GSList *tramps = NULL;
6401 mono_x86_get_signal_exception_trampoline (&info, aot);
6403 tramps = g_slist_append (tramps, info);
6408 /* Soft Debug support */
6409 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
6412 * mono_arch_set_breakpoint:
6414 * Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
6415 * The location should contain code emitted by OP_SEQ_POINT.
6418 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
6420 guint8 *code = ip + OP_SEQ_POINT_BP_OFFSET;
6422 g_assert (code [0] == 0x90);
6423 x86_call_membase (code, X86_ECX, 0);
6427 * mono_arch_clear_breakpoint:
6429 * Clear the breakpoint at IP.
6432 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
6434 guint8 *code = ip + OP_SEQ_POINT_BP_OFFSET;
6437 for (i = 0; i < 2; ++i)
6442 * mono_arch_start_single_stepping:
6444 * Start single stepping.
6447 mono_arch_start_single_stepping (void)
6449 ss_trampoline = mini_get_single_step_trampoline ();
6453 * mono_arch_stop_single_stepping:
6455 * Stop single stepping.
6458 mono_arch_stop_single_stepping (void)
6460 ss_trampoline = NULL;
6464 * mono_arch_is_single_step_event:
6466 * Return whenever the machine state in SIGCTX corresponds to a single
6470 mono_arch_is_single_step_event (void *info, void *sigctx)
6472 /* We use soft breakpoints */
6477 mono_arch_is_breakpoint_event (void *info, void *sigctx)
6479 /* We use soft breakpoints */
6483 #define BREAKPOINT_SIZE 2
6486 * mono_arch_skip_breakpoint:
6488 * See mini-amd64.c for docs.
6491 mono_arch_skip_breakpoint (MonoContext *ctx, MonoJitInfo *ji)
6493 g_assert_not_reached ();
6497 * mono_arch_skip_single_step:
6499 * See mini-amd64.c for docs.
6502 mono_arch_skip_single_step (MonoContext *ctx)
6504 g_assert_not_reached ();
6508 * mono_arch_get_seq_point_info:
6510 * See mini-amd64.c for docs.
6513 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
6520 mono_arch_init_lmf_ext (MonoLMFExt *ext, gpointer prev_lmf)
6522 ext->lmf.previous_lmf = (gsize)prev_lmf;
6523 /* Mark that this is a MonoLMFExt */
6524 ext->lmf.previous_lmf = (gsize)(gpointer)(((gssize)ext->lmf.previous_lmf) | 2);
6525 ext->lmf.ebp = (gssize)ext;
6531 mono_arch_opcode_supported (int opcode)
6534 case OP_ATOMIC_ADD_I4:
6535 case OP_ATOMIC_EXCHANGE_I4:
6536 case OP_ATOMIC_CAS_I4:
6537 case OP_ATOMIC_LOAD_I1:
6538 case OP_ATOMIC_LOAD_I2:
6539 case OP_ATOMIC_LOAD_I4:
6540 case OP_ATOMIC_LOAD_U1:
6541 case OP_ATOMIC_LOAD_U2:
6542 case OP_ATOMIC_LOAD_U4:
6543 case OP_ATOMIC_LOAD_R4:
6544 case OP_ATOMIC_LOAD_R8:
6545 case OP_ATOMIC_STORE_I1:
6546 case OP_ATOMIC_STORE_I2:
6547 case OP_ATOMIC_STORE_I4:
6548 case OP_ATOMIC_STORE_U1:
6549 case OP_ATOMIC_STORE_U2:
6550 case OP_ATOMIC_STORE_U4:
6551 case OP_ATOMIC_STORE_R4:
6552 case OP_ATOMIC_STORE_R8:
6560 mono_arch_get_call_info (MonoMemPool *mp, MonoMethodSignature *sig)
6562 return get_call_info (mp, sig);