2 * mini-x86.c: x86 backend for the Mono code generator
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
9 * Copyright 2003 Ximian, Inc.
10 * Copyright 2003-2011 Novell Inc.
11 * Copyright 2011 Xamarin Inc.
12 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
21 #include <mono/metadata/abi-details.h>
22 #include <mono/metadata/appdomain.h>
23 #include <mono/metadata/debug-helpers.h>
24 #include <mono/metadata/threads.h>
25 #include <mono/metadata/profiler-private.h>
26 #include <mono/metadata/mono-debug.h>
27 #include <mono/metadata/gc-internals.h>
28 #include <mono/utils/mono-math.h>
29 #include <mono/utils/mono-counters.h>
30 #include <mono/utils/mono-mmap.h>
31 #include <mono/utils/mono-memory-model.h>
32 #include <mono/utils/mono-hwcap.h>
33 #include <mono/utils/mono-threads.h>
43 static gboolean optimize_for_xen = TRUE;
45 #define optimize_for_xen 0
49 /* The single step trampoline */
50 static gpointer ss_trampoline;
52 /* The breakpoint trampoline */
53 static gpointer bp_trampoline;
55 /* This mutex protects architecture specific caches */
56 #define mono_mini_arch_lock() mono_os_mutex_lock (&mini_arch_mutex)
57 #define mono_mini_arch_unlock() mono_os_mutex_unlock (&mini_arch_mutex)
58 static mono_mutex_t mini_arch_mutex;
60 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
65 /* Under windows, the default pinvoke calling convention is stdcall */
66 #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))
68 #define CALLCONV_IS_STDCALL(sig) ((sig)->pinvoke && ((sig)->call_convention == MONO_CALL_STDCALL || (sig)->call_convention == MONO_CALL_THISCALL))
71 #define X86_IS_CALLEE_SAVED_REG(reg) (((reg) == X86_EBX) || ((reg) == X86_EDI) || ((reg) == X86_ESI))
73 #define OP_SEQ_POINT_BP_OFFSET 7
76 emit_load_aotconst (guint8 *start, guint8 *code, MonoCompile *cfg, MonoJumpInfo **ji, int dreg, int tramp_type, gconstpointer target);
79 mono_arch_regname (int reg)
82 case X86_EAX: return "%eax";
83 case X86_EBX: return "%ebx";
84 case X86_ECX: return "%ecx";
85 case X86_EDX: return "%edx";
86 case X86_ESP: return "%esp";
87 case X86_EBP: return "%ebp";
88 case X86_EDI: return "%edi";
89 case X86_ESI: return "%esi";
95 mono_arch_fregname (int reg)
120 mono_arch_xregname (int reg)
145 mono_x86_patch (unsigned char* code, gpointer target)
147 x86_patch (code, (unsigned char*)target);
150 #define FLOAT_PARAM_REGS 0
152 static const guint32 thiscall_param_regs [] = { X86_ECX, X86_NREG };
154 static const guint32 *callconv_param_regs(MonoMethodSignature *sig)
159 switch (sig->call_convention) {
160 case MONO_CALL_THISCALL:
161 return thiscall_param_regs;
167 #if defined(TARGET_WIN32) || defined(__APPLE__) || defined(__FreeBSD__)
168 #define SMALL_STRUCTS_IN_REGS
169 static X86_Reg_No return_regs [] = { X86_EAX, X86_EDX };
173 add_general (guint32 *gr, const guint32 *param_regs, guint32 *stack_size, ArgInfo *ainfo)
175 ainfo->offset = *stack_size;
177 if (!param_regs || param_regs [*gr] == X86_NREG) {
178 ainfo->storage = ArgOnStack;
180 (*stack_size) += sizeof (gpointer);
183 ainfo->storage = ArgInIReg;
184 ainfo->reg = param_regs [*gr];
190 add_general_pair (guint32 *gr, const guint32 *param_regs , guint32 *stack_size, ArgInfo *ainfo)
192 ainfo->offset = *stack_size;
194 g_assert(!param_regs || param_regs[*gr] == X86_NREG);
196 ainfo->storage = ArgOnStack;
197 (*stack_size) += sizeof (gpointer) * 2;
202 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
204 ainfo->offset = *stack_size;
206 if (*gr >= FLOAT_PARAM_REGS) {
207 ainfo->storage = ArgOnStack;
208 (*stack_size) += is_double ? 8 : 4;
209 ainfo->nslots = is_double ? 2 : 1;
212 /* A double register */
214 ainfo->storage = ArgInDoubleSSEReg;
216 ainfo->storage = ArgInFloatSSEReg;
224 add_valuetype (MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
226 guint32 *gr, const guint32 *param_regs, guint32 *fr, guint32 *stack_size)
231 klass = mono_class_from_mono_type (type);
232 size = mini_type_stack_size_full (&klass->byval_arg, NULL, sig->pinvoke);
234 #if defined(TARGET_WIN32)
236 * Standard C and C++ doesn't allow empty structs, empty structs will always have a size of 1 byte.
237 * GCC have an extension to allow empty structs, https://gcc.gnu.org/onlinedocs/gcc/Empty-Structures.html.
238 * This cause a little dilemma since runtime build using none GCC compiler will not be compatible with
239 * GCC build C libraries and the other way around. On platforms where empty structs has size of 1 byte
240 * it must be represented in call and cannot be dropped.
242 if (size == 0 && MONO_TYPE_ISSTRUCT (type) && sig->pinvoke) {
243 /* Empty structs (1 byte size) needs to be represented in a stack slot */
244 ainfo->pass_empty_struct = TRUE;
249 #ifdef SMALL_STRUCTS_IN_REGS
250 if (sig->pinvoke && is_return) {
251 MonoMarshalType *info;
253 info = mono_marshal_load_type_info (klass);
256 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
258 /* Ignore empty struct return value, if used. */
259 if (info->num_fields == 0 && ainfo->pass_empty_struct) {
260 ainfo->storage = ArgValuetypeInReg;
265 * Windows x86 ABI for returning structs of size 4 or 8 bytes (regardless of type) dictates that
266 * values are passed in EDX:EAX register pairs, https://msdn.microsoft.com/en-us/library/984x0h58.aspx.
267 * This is different compared to for example float or double return types (not in struct) that will be returned
268 * in ST(0), https://msdn.microsoft.com/en-us/library/ha59cbfz.aspx.
270 * Apples OSX x86 ABI for returning structs of size 4 or 8 bytes uses a slightly different approach.
271 * If a struct includes only one scalar value, it will be handled with the same rules as scalar values.
272 * This means that structs with one float or double will be returned in ST(0). For more details,
273 * https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/LowLevelABI/130-IA-32_Function_Calling_Conventions/IA32.html.
275 #if !defined(TARGET_WIN32)
277 /* Special case structs with only a float member */
278 if (info->num_fields == 1) {
279 int ftype = mini_get_underlying_type (info->fields [0].field->type)->type;
280 if ((info->native_size == 8) && (ftype == MONO_TYPE_R8)) {
281 ainfo->storage = ArgValuetypeInReg;
282 ainfo->pair_storage [0] = ArgOnDoubleFpStack;
285 if ((info->native_size == 4) && (ftype == MONO_TYPE_R4)) {
286 ainfo->storage = ArgValuetypeInReg;
287 ainfo->pair_storage [0] = ArgOnFloatFpStack;
293 if ((info->native_size == 1) || (info->native_size == 2) || (info->native_size == 4) || (info->native_size == 8)) {
294 ainfo->storage = ArgValuetypeInReg;
295 ainfo->pair_storage [0] = ArgInIReg;
296 ainfo->pair_regs [0] = return_regs [0];
297 if (info->native_size > 4) {
298 ainfo->pair_storage [1] = ArgInIReg;
299 ainfo->pair_regs [1] = return_regs [1];
306 if (param_regs && param_regs [*gr] != X86_NREG && !is_return) {
307 g_assert (size <= 4);
308 ainfo->storage = ArgValuetypeInReg;
309 ainfo->reg = param_regs [*gr];
314 ainfo->offset = *stack_size;
315 ainfo->storage = ArgOnStack;
316 *stack_size += ALIGN_TO (size, sizeof (gpointer));
317 ainfo->nslots = ALIGN_TO (size, sizeof (gpointer)) / sizeof (gpointer);
323 * Obtain information about a call according to the calling convention.
324 * For x86 ELF, see the "System V Application Binary Interface Intel386
325 * Architecture Processor Supplment, Fourth Edition" document for more
327 * For x86 win32, see https://msdn.microsoft.com/en-us/library/984x0h58.aspx.
330 get_call_info_internal (CallInfo *cinfo, MonoMethodSignature *sig)
332 guint32 i, gr, fr, pstart;
333 const guint32 *param_regs;
335 int n = sig->hasthis + sig->param_count;
336 guint32 stack_size = 0;
337 gboolean is_pinvoke = sig->pinvoke;
343 param_regs = callconv_param_regs(sig);
347 ret_type = mini_get_underlying_type (sig->ret);
348 switch (ret_type->type) {
358 case MONO_TYPE_FNPTR:
359 case MONO_TYPE_CLASS:
360 case MONO_TYPE_OBJECT:
361 case MONO_TYPE_SZARRAY:
362 case MONO_TYPE_ARRAY:
363 case MONO_TYPE_STRING:
364 cinfo->ret.storage = ArgInIReg;
365 cinfo->ret.reg = X86_EAX;
369 cinfo->ret.storage = ArgInIReg;
370 cinfo->ret.reg = X86_EAX;
371 cinfo->ret.is_pair = TRUE;
374 cinfo->ret.storage = ArgOnFloatFpStack;
377 cinfo->ret.storage = ArgOnDoubleFpStack;
379 case MONO_TYPE_GENERICINST:
380 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
381 cinfo->ret.storage = ArgInIReg;
382 cinfo->ret.reg = X86_EAX;
385 if (mini_is_gsharedvt_type (ret_type)) {
386 cinfo->ret.storage = ArgOnStack;
387 cinfo->vtype_retaddr = TRUE;
391 case MONO_TYPE_VALUETYPE:
392 case MONO_TYPE_TYPEDBYREF: {
393 guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
395 add_valuetype (sig, &cinfo->ret, ret_type, TRUE, &tmp_gr, NULL, &tmp_fr, &tmp_stacksize);
396 if (cinfo->ret.storage == ArgOnStack) {
397 cinfo->vtype_retaddr = TRUE;
398 /* The caller passes the address where the value is stored */
404 g_assert (mini_is_gsharedvt_type (ret_type));
405 cinfo->ret.storage = ArgOnStack;
406 cinfo->vtype_retaddr = TRUE;
409 cinfo->ret.storage = ArgNone;
412 g_error ("Can't handle as return value 0x%x", ret_type->type);
418 * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
419 * the first argument, allowing 'this' to be always passed in the first arg reg.
420 * Also do this if the first argument is a reference type, since virtual calls
421 * are sometimes made using calli without sig->hasthis set, like in the delegate
424 if (cinfo->vtype_retaddr && !is_pinvoke && (sig->hasthis || (sig->param_count > 0 && MONO_TYPE_IS_REFERENCE (mini_get_underlying_type (sig->params [0]))))) {
426 add_general (&gr, param_regs, &stack_size, cinfo->args + 0);
428 add_general (&gr, param_regs, &stack_size, &cinfo->args [sig->hasthis + 0]);
431 cinfo->vret_arg_offset = stack_size;
432 add_general (&gr, NULL, &stack_size, &cinfo->ret);
433 cinfo->vret_arg_index = 1;
437 add_general (&gr, param_regs, &stack_size, cinfo->args + 0);
439 if (cinfo->vtype_retaddr)
440 add_general (&gr, NULL, &stack_size, &cinfo->ret);
443 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
444 fr = FLOAT_PARAM_REGS;
446 /* Emit the signature cookie just before the implicit arguments */
447 add_general (&gr, param_regs, &stack_size, &cinfo->sig_cookie);
450 for (i = pstart; i < sig->param_count; ++i) {
451 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
454 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
455 /* We allways pass the sig cookie on the stack for simplicity */
457 * Prevent implicit arguments + the sig cookie from being passed
460 fr = FLOAT_PARAM_REGS;
462 /* Emit the signature cookie just before the implicit arguments */
463 add_general (&gr, param_regs, &stack_size, &cinfo->sig_cookie);
466 if (sig->params [i]->byref) {
467 add_general (&gr, param_regs, &stack_size, ainfo);
470 ptype = mini_get_underlying_type (sig->params [i]);
471 switch (ptype->type) {
474 add_general (&gr, param_regs, &stack_size, ainfo);
478 add_general (&gr, param_regs, &stack_size, ainfo);
482 add_general (&gr, param_regs, &stack_size, ainfo);
487 case MONO_TYPE_FNPTR:
488 case MONO_TYPE_CLASS:
489 case MONO_TYPE_OBJECT:
490 case MONO_TYPE_STRING:
491 case MONO_TYPE_SZARRAY:
492 case MONO_TYPE_ARRAY:
493 add_general (&gr, param_regs, &stack_size, ainfo);
495 case MONO_TYPE_GENERICINST:
496 if (!mono_type_generic_inst_is_valuetype (ptype)) {
497 add_general (&gr, param_regs, &stack_size, ainfo);
500 if (mini_is_gsharedvt_type (ptype)) {
501 /* gsharedvt arguments are passed by ref */
502 add_general (&gr, param_regs, &stack_size, ainfo);
503 g_assert (ainfo->storage == ArgOnStack);
504 ainfo->storage = ArgGSharedVt;
508 case MONO_TYPE_VALUETYPE:
509 case MONO_TYPE_TYPEDBYREF:
510 add_valuetype (sig, ainfo, ptype, FALSE, &gr, param_regs, &fr, &stack_size);
514 add_general_pair (&gr, param_regs, &stack_size, ainfo);
517 add_float (&fr, &stack_size, ainfo, FALSE);
520 add_float (&fr, &stack_size, ainfo, TRUE);
524 /* gsharedvt arguments are passed by ref */
525 g_assert (mini_is_gsharedvt_type (ptype));
526 add_general (&gr, param_regs, &stack_size, ainfo);
527 g_assert (ainfo->storage == ArgOnStack);
528 ainfo->storage = ArgGSharedVt;
531 g_error ("unexpected type 0x%x", ptype->type);
532 g_assert_not_reached ();
536 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
537 fr = FLOAT_PARAM_REGS;
539 /* Emit the signature cookie just before the implicit arguments */
540 add_general (&gr, param_regs, &stack_size, &cinfo->sig_cookie);
543 if (cinfo->vtype_retaddr) {
544 /* if the function returns a struct on stack, the called method already does a ret $0x4 */
545 cinfo->callee_stack_pop = 4;
546 } else if (CALLCONV_IS_STDCALL (sig)) {
547 /* Have to compensate for the stack space popped by the native callee */
548 cinfo->callee_stack_pop = stack_size;
551 if (mono_do_x86_stack_align && (stack_size % MONO_ARCH_FRAME_ALIGNMENT) != 0) {
552 cinfo->need_stack_align = TRUE;
553 cinfo->stack_align_amount = MONO_ARCH_FRAME_ALIGNMENT - (stack_size % MONO_ARCH_FRAME_ALIGNMENT);
554 stack_size += cinfo->stack_align_amount;
557 cinfo->stack_usage = stack_size;
558 cinfo->reg_usage = gr;
559 cinfo->freg_usage = fr;
564 get_call_info (MonoMemPool *mp, MonoMethodSignature *sig)
566 int n = sig->hasthis + sig->param_count;
570 cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
572 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
574 return get_call_info_internal (cinfo, sig);
578 * mono_arch_get_argument_info:
579 * @csig: a method signature
580 * @param_count: the number of parameters to consider
581 * @arg_info: an array to store the result infos
583 * Gathers information on parameters such as size, alignment and
584 * padding. arg_info should be large enought to hold param_count + 1 entries.
586 * Returns the size of the argument area on the stack.
587 * This should be signal safe, since it is called from
588 * mono_arch_unwind_frame ().
589 * FIXME: The metadata calls might not be signal safe.
592 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
594 int len, k, args_size = 0;
600 /* Avoid g_malloc as it is not signal safe */
601 len = sizeof (CallInfo) + (sizeof (ArgInfo) * (csig->param_count + 1));
602 cinfo = (CallInfo*)g_newa (guint8*, len);
603 memset (cinfo, 0, len);
605 cinfo = get_call_info_internal (cinfo, csig);
607 arg_info [0].offset = offset;
609 if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 0) {
610 args_size += sizeof (gpointer);
615 args_size += sizeof (gpointer);
619 if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 1 && csig->hasthis) {
620 /* Emitted after this */
621 args_size += sizeof (gpointer);
625 arg_info [0].size = args_size;
627 for (k = 0; k < param_count; k++) {
628 size = mini_type_stack_size_full (csig->params [k], &align, csig->pinvoke);
630 /* ignore alignment for now */
633 args_size += pad = (align - (args_size & (align - 1))) & (align - 1);
634 arg_info [k].pad = pad;
636 arg_info [k + 1].pad = 0;
637 arg_info [k + 1].size = size;
639 arg_info [k + 1].offset = offset;
642 if (k == 0 && cinfo->vtype_retaddr && cinfo->vret_arg_index == 1 && !csig->hasthis) {
643 /* Emitted after the first arg */
644 args_size += sizeof (gpointer);
649 if (mono_do_x86_stack_align && !CALLCONV_IS_STDCALL (csig))
650 align = MONO_ARCH_FRAME_ALIGNMENT;
653 args_size += pad = (align - (args_size & (align - 1))) & (align - 1);
654 arg_info [k].pad = pad;
660 mono_arch_tail_call_supported (MonoCompile *cfg, MonoMethodSignature *caller_sig, MonoMethodSignature *callee_sig)
662 MonoType *callee_ret;
666 if (cfg->compile_aot && !cfg->full_aot)
667 /* OP_TAILCALL doesn't work with AOT */
670 c1 = get_call_info (NULL, caller_sig);
671 c2 = get_call_info (NULL, callee_sig);
673 * Tail calls with more callee stack usage than the caller cannot be supported, since
674 * the extra stack space would be left on the stack after the tail call.
676 res = c1->stack_usage >= c2->stack_usage;
677 callee_ret = mini_get_underlying_type (callee_sig->ret);
678 if (callee_ret && MONO_TYPE_ISSTRUCT (callee_ret) && c2->ret.storage != ArgValuetypeInReg)
679 /* An address on the callee's stack is passed as the first argument */
689 * Initialize the cpu to execute managed code.
692 mono_arch_cpu_init (void)
694 /* spec compliance requires running with double precision */
698 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
699 fpcw &= ~X86_FPCW_PRECC_MASK;
700 fpcw |= X86_FPCW_PREC_DOUBLE;
701 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
702 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
704 _control87 (_PC_53, MCW_PC);
709 * Initialize architecture specific code.
712 mono_arch_init (void)
714 mono_os_mutex_init_recursive (&mini_arch_mutex);
717 bp_trampoline = mini_get_breakpoint_trampoline ();
719 mono_aot_register_jit_icall ("mono_x86_throw_exception", mono_x86_throw_exception);
720 mono_aot_register_jit_icall ("mono_x86_throw_corlib_exception", mono_x86_throw_corlib_exception);
721 #if defined(MONO_ARCH_GSHAREDVT_SUPPORTED)
722 mono_aot_register_jit_icall ("mono_x86_start_gsharedvt_call", mono_x86_start_gsharedvt_call);
727 * Cleanup architecture specific code.
730 mono_arch_cleanup (void)
732 mono_os_mutex_destroy (&mini_arch_mutex);
736 * This function returns the optimizations supported on this cpu.
739 mono_arch_cpu_optimizations (guint32 *exclude_mask)
745 if (mono_hwcap_x86_has_cmov) {
746 opts |= MONO_OPT_CMOV;
748 if (mono_hwcap_x86_has_fcmov)
749 opts |= MONO_OPT_FCMOV;
751 *exclude_mask |= MONO_OPT_FCMOV;
753 *exclude_mask |= MONO_OPT_CMOV;
756 if (mono_hwcap_x86_has_sse2)
757 opts |= MONO_OPT_SSE2;
759 *exclude_mask |= MONO_OPT_SSE2;
761 #ifdef MONO_ARCH_SIMD_INTRINSICS
762 /*SIMD intrinsics require at least SSE2.*/
763 if (!mono_hwcap_x86_has_sse2)
764 *exclude_mask |= MONO_OPT_SIMD;
771 * This function test for all SSE functions supported.
773 * Returns a bitmask corresponding to all supported versions.
777 mono_arch_cpu_enumerate_simd_versions (void)
779 guint32 sse_opts = 0;
781 if (mono_hwcap_x86_has_sse1)
782 sse_opts |= SIMD_VERSION_SSE1;
784 if (mono_hwcap_x86_has_sse2)
785 sse_opts |= SIMD_VERSION_SSE2;
787 if (mono_hwcap_x86_has_sse3)
788 sse_opts |= SIMD_VERSION_SSE3;
790 if (mono_hwcap_x86_has_ssse3)
791 sse_opts |= SIMD_VERSION_SSSE3;
793 if (mono_hwcap_x86_has_sse41)
794 sse_opts |= SIMD_VERSION_SSE41;
796 if (mono_hwcap_x86_has_sse42)
797 sse_opts |= SIMD_VERSION_SSE42;
799 if (mono_hwcap_x86_has_sse4a)
800 sse_opts |= SIMD_VERSION_SSE4a;
806 * Determine whenever the trap whose info is in SIGINFO is caused by
810 mono_arch_is_int_overflow (void *sigctx, void *info)
815 mono_sigctx_to_monoctx (sigctx, &ctx);
817 ip = (guint8*)ctx.eip;
819 if ((ip [0] == 0xf7) && (x86_modrm_mod (ip [1]) == 0x3) && (x86_modrm_reg (ip [1]) == 0x7)) {
823 switch (x86_modrm_rm (ip [1])) {
843 g_assert_not_reached ();
855 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
860 for (i = 0; i < cfg->num_varinfo; i++) {
861 MonoInst *ins = cfg->varinfo [i];
862 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
865 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
868 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
869 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
872 /* we dont allocate I1 to registers because there is no simply way to sign extend
873 * 8bit quantities in caller saved registers on x86 */
874 if (mono_is_regsize_var (ins->inst_vtype) && (ins->inst_vtype->type != MONO_TYPE_I1)) {
875 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
876 g_assert (i == vmv->idx);
877 vars = g_list_prepend (vars, vmv);
881 vars = mono_varlist_sort (cfg, vars, 0);
887 mono_arch_get_global_int_regs (MonoCompile *cfg)
891 /* we can use 3 registers for global allocation */
892 regs = g_list_prepend (regs, (gpointer)X86_EBX);
893 regs = g_list_prepend (regs, (gpointer)X86_ESI);
894 regs = g_list_prepend (regs, (gpointer)X86_EDI);
900 * mono_arch_regalloc_cost:
902 * Return the cost, in number of memory references, of the action of
903 * allocating the variable VMV into a register during global register
907 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
909 MonoInst *ins = cfg->varinfo [vmv->idx];
911 if (cfg->method->save_lmf)
912 /* The register is already saved */
913 return (ins->opcode == OP_ARG) ? 1 : 0;
915 /* push+pop+possible load if it is an argument */
916 return (ins->opcode == OP_ARG) ? 3 : 2;
920 set_needs_stack_frame (MonoCompile *cfg, gboolean flag)
922 static int inited = FALSE;
923 static int count = 0;
925 if (cfg->arch.need_stack_frame_inited) {
926 g_assert (cfg->arch.need_stack_frame == flag);
930 cfg->arch.need_stack_frame = flag;
931 cfg->arch.need_stack_frame_inited = TRUE;
937 mono_counters_register ("Could eliminate stack frame", MONO_COUNTER_INT|MONO_COUNTER_JIT, &count);
942 //g_print ("will eliminate %s.%s.%s\n", cfg->method->klass->name_space, cfg->method->klass->name, cfg->method->name);
946 needs_stack_frame (MonoCompile *cfg)
948 MonoMethodSignature *sig;
949 MonoMethodHeader *header;
950 gboolean result = FALSE;
952 #if defined(__APPLE__)
953 /*OSX requires stack frame code to have the correct alignment. */
957 if (cfg->arch.need_stack_frame_inited)
958 return cfg->arch.need_stack_frame;
960 header = cfg->header;
961 sig = mono_method_signature (cfg->method);
963 if (cfg->disable_omit_fp)
965 else if (cfg->flags & MONO_CFG_HAS_ALLOCA)
967 else if (cfg->method->save_lmf)
969 else if (cfg->stack_offset)
971 else if (cfg->param_area)
973 else if (cfg->flags & (MONO_CFG_HAS_CALLS | MONO_CFG_HAS_ALLOCA | MONO_CFG_HAS_TAIL))
975 else if (header->num_clauses)
977 else if (sig->param_count + sig->hasthis)
979 else if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
981 else if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) ||
982 (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
985 set_needs_stack_frame (cfg, result);
987 return cfg->arch.need_stack_frame;
991 * Set var information according to the calling convention. X86 version.
992 * The locals var stuff should most likely be split in another method.
995 mono_arch_allocate_vars (MonoCompile *cfg)
997 MonoMethodSignature *sig;
998 MonoMethodHeader *header;
1000 guint32 locals_stack_size, locals_stack_align;
1005 header = cfg->header;
1006 sig = mono_method_signature (cfg->method);
1008 if (!cfg->arch.cinfo)
1009 cfg->arch.cinfo = get_call_info (cfg->mempool, sig);
1010 cinfo = (CallInfo *)cfg->arch.cinfo;
1012 cfg->frame_reg = X86_EBP;
1015 if (cfg->has_atomic_add_i4 || cfg->has_atomic_exchange_i4) {
1016 /* The opcode implementations use callee-saved regs as scratch regs by pushing and pop-ing them, but that is not async safe */
1017 cfg->used_int_regs |= (1 << X86_EBX) | (1 << X86_EDI) | (1 << X86_ESI);
1020 /* Reserve space to save LMF and caller saved registers */
1022 if (cfg->method->save_lmf) {
1023 /* The LMF var is allocated normally */
1025 if (cfg->used_int_regs & (1 << X86_EBX)) {
1029 if (cfg->used_int_regs & (1 << X86_EDI)) {
1033 if (cfg->used_int_regs & (1 << X86_ESI)) {
1038 switch (cinfo->ret.storage) {
1039 case ArgValuetypeInReg:
1040 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
1042 cfg->ret->opcode = OP_REGOFFSET;
1043 cfg->ret->inst_basereg = X86_EBP;
1044 cfg->ret->inst_offset = - offset;
1050 /* Allocate locals */
1051 offsets = mono_allocate_stack_slots (cfg, TRUE, &locals_stack_size, &locals_stack_align);
1052 if (locals_stack_size > MONO_ARCH_MAX_FRAME_SIZE) {
1053 char *mname = mono_method_full_name (cfg->method, TRUE);
1054 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Method %s stack is too big.", mname));
1058 if (locals_stack_align) {
1059 int prev_offset = offset;
1061 offset += (locals_stack_align - 1);
1062 offset &= ~(locals_stack_align - 1);
1064 while (prev_offset < offset) {
1066 mini_gc_set_slot_type_from_fp (cfg, - prev_offset, SLOT_NOREF);
1069 cfg->locals_min_stack_offset = - (offset + locals_stack_size);
1070 cfg->locals_max_stack_offset = - offset;
1072 * EBP is at alignment 8 % MONO_ARCH_FRAME_ALIGNMENT, so if we
1073 * have locals larger than 8 bytes we need to make sure that
1074 * they have the appropriate offset.
1076 if (MONO_ARCH_FRAME_ALIGNMENT > 8 && locals_stack_align > 8) {
1077 int extra_size = MONO_ARCH_FRAME_ALIGNMENT - sizeof (gpointer) * 2;
1078 offset += extra_size;
1079 locals_stack_size += extra_size;
1081 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1082 if (offsets [i] != -1) {
1083 MonoInst *inst = cfg->varinfo [i];
1084 inst->opcode = OP_REGOFFSET;
1085 inst->inst_basereg = X86_EBP;
1086 inst->inst_offset = - (offset + offsets [i]);
1087 //printf ("allocated local %d to ", i); mono_print_tree_nl (inst);
1090 offset += locals_stack_size;
1094 * Allocate arguments+return value
1097 switch (cinfo->ret.storage) {
1099 if (cfg->vret_addr) {
1101 * In the new IR, the cfg->vret_addr variable represents the
1102 * vtype return value.
1104 cfg->vret_addr->opcode = OP_REGOFFSET;
1105 cfg->vret_addr->inst_basereg = cfg->frame_reg;
1106 cfg->vret_addr->inst_offset = cinfo->ret.offset + ARGS_OFFSET;
1107 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1108 printf ("vret_addr =");
1109 mono_print_ins (cfg->vret_addr);
1112 cfg->ret->opcode = OP_REGOFFSET;
1113 cfg->ret->inst_basereg = X86_EBP;
1114 cfg->ret->inst_offset = cinfo->ret.offset + ARGS_OFFSET;
1117 case ArgValuetypeInReg:
1120 cfg->ret->opcode = OP_REGVAR;
1121 cfg->ret->inst_c0 = cinfo->ret.reg;
1122 cfg->ret->dreg = cinfo->ret.reg;
1125 case ArgOnFloatFpStack:
1126 case ArgOnDoubleFpStack:
1129 g_assert_not_reached ();
1132 if (sig->call_convention == MONO_CALL_VARARG) {
1133 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
1134 cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
1137 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1138 ArgInfo *ainfo = &cinfo->args [i];
1139 inst = cfg->args [i];
1140 if (inst->opcode != OP_REGVAR) {
1141 inst->opcode = OP_REGOFFSET;
1142 inst->inst_basereg = X86_EBP;
1143 inst->inst_offset = ainfo->offset + ARGS_OFFSET;
1147 cfg->stack_offset = offset;
1151 mono_arch_create_vars (MonoCompile *cfg)
1154 MonoMethodSignature *sig;
1157 sig = mono_method_signature (cfg->method);
1159 if (!cfg->arch.cinfo)
1160 cfg->arch.cinfo = get_call_info (cfg->mempool, sig);
1161 cinfo = (CallInfo *)cfg->arch.cinfo;
1163 sig_ret = mini_get_underlying_type (sig->ret);
1165 if (cinfo->ret.storage == ArgValuetypeInReg)
1166 cfg->ret_var_is_local = TRUE;
1167 if ((cinfo->ret.storage != ArgValuetypeInReg) && (MONO_TYPE_ISSTRUCT (sig_ret) || mini_is_gsharedvt_variable_type (sig_ret))) {
1168 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
1171 if (cfg->gen_sdb_seq_points) {
1174 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1175 ins->flags |= MONO_INST_VOLATILE;
1176 cfg->arch.ss_tramp_var = ins;
1178 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1179 ins->flags |= MONO_INST_VOLATILE;
1180 cfg->arch.bp_tramp_var = ins;
1183 if (cfg->method->save_lmf) {
1184 cfg->create_lmf_var = TRUE;
1188 cfg->arch_eh_jit_info = 1;
1192 * It is expensive to adjust esp for each individual fp argument pushed on the stack
1193 * so we try to do it just once when we have multiple fp arguments in a row.
1194 * We don't use this mechanism generally because for int arguments the generated code
1195 * is slightly bigger and new generation cpus optimize away the dependency chains
1196 * created by push instructions on the esp value.
1197 * fp_arg_setup is the first argument in the execution sequence where the esp register
1200 static G_GNUC_UNUSED int
1201 collect_fp_stack_space (MonoMethodSignature *sig, int start_arg, int *fp_arg_setup)
1206 for (; start_arg < sig->param_count; ++start_arg) {
1207 t = mini_get_underlying_type (sig->params [start_arg]);
1208 if (!t->byref && t->type == MONO_TYPE_R8) {
1209 fp_space += sizeof (double);
1210 *fp_arg_setup = start_arg;
1219 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
1221 MonoMethodSignature *tmp_sig;
1225 * mono_ArgIterator_Setup assumes the signature cookie is
1226 * passed first and all the arguments which were before it are
1227 * passed on the stack after the signature. So compensate by
1228 * passing a different signature.
1230 tmp_sig = mono_metadata_signature_dup (call->signature);
1231 tmp_sig->param_count -= call->signature->sentinelpos;
1232 tmp_sig->sentinelpos = 0;
1233 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
1235 if (cfg->compile_aot) {
1236 sig_reg = mono_alloc_ireg (cfg);
1237 MONO_EMIT_NEW_SIGNATURECONST (cfg, sig_reg, tmp_sig);
1238 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, cinfo->sig_cookie.offset, sig_reg);
1240 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, X86_ESP, cinfo->sig_cookie.offset, tmp_sig);
1246 mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
1251 LLVMCallInfo *linfo;
1252 MonoType *t, *sig_ret;
1254 n = sig->param_count + sig->hasthis;
1256 cinfo = get_call_info (cfg->mempool, sig);
1259 linfo = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
1262 * LLVM always uses the native ABI while we use our own ABI, the
1263 * only difference is the handling of vtypes:
1264 * - we only pass/receive them in registers in some cases, and only
1265 * in 1 or 2 integer registers.
1267 if (cinfo->ret.storage == ArgValuetypeInReg) {
1269 cfg->exception_message = g_strdup ("pinvoke + vtypes");
1270 cfg->disable_llvm = TRUE;
1274 cfg->exception_message = g_strdup ("vtype ret in call");
1275 cfg->disable_llvm = TRUE;
1277 linfo->ret.storage = LLVMArgVtypeInReg;
1278 for (j = 0; j < 2; ++j)
1279 linfo->ret.pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, cinfo->ret.pair_storage [j]);
1283 if (mini_type_is_vtype (sig_ret) && cinfo->ret.storage == ArgInIReg) {
1284 /* Vtype returned using a hidden argument */
1285 linfo->ret.storage = LLVMArgVtypeRetAddr;
1286 linfo->vret_arg_index = cinfo->vret_arg_index;
1289 if (mini_type_is_vtype (sig_ret) && cinfo->ret.storage != ArgInIReg) {
1291 cfg->exception_message = g_strdup ("vtype ret in call");
1292 cfg->disable_llvm = TRUE;
1295 for (i = 0; i < n; ++i) {
1296 ainfo = cinfo->args + i;
1298 if (i >= sig->hasthis)
1299 t = sig->params [i - sig->hasthis];
1301 t = &mono_defaults.int_class->byval_arg;
1303 linfo->args [i].storage = LLVMArgNone;
1305 switch (ainfo->storage) {
1307 linfo->args [i].storage = LLVMArgNormal;
1309 case ArgInDoubleSSEReg:
1310 case ArgInFloatSSEReg:
1311 linfo->args [i].storage = LLVMArgNormal;
1314 if (mini_type_is_vtype (t)) {
1315 if (mono_class_value_size (mono_class_from_mono_type (t), NULL) == 0)
1316 /* LLVM seems to allocate argument space for empty structures too */
1317 linfo->args [i].storage = LLVMArgNone;
1319 linfo->args [i].storage = LLVMArgVtypeByVal;
1321 linfo->args [i].storage = LLVMArgNormal;
1324 case ArgValuetypeInReg:
1326 cfg->exception_message = g_strdup ("pinvoke + vtypes");
1327 cfg->disable_llvm = TRUE;
1331 cfg->exception_message = g_strdup ("vtype arg");
1332 cfg->disable_llvm = TRUE;
1334 linfo->args [i].storage = LLVMArgVtypeInReg;
1335 for (j = 0; j < 2; ++j)
1336 linfo->args [i].pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, ainfo->pair_storage [j]);
1340 linfo->args [i].storage = LLVMArgGSharedVt;
1343 cfg->exception_message = g_strdup ("ainfo->storage");
1344 cfg->disable_llvm = TRUE;
1354 emit_gc_param_slot_def (MonoCompile *cfg, int sp_offset, MonoType *t)
1356 if (cfg->compute_gc_maps) {
1359 /* Needs checking if the feature will be enabled again */
1360 g_assert_not_reached ();
1362 /* On x86, the offsets are from the sp value before the start of the call sequence */
1364 t = &mono_defaults.int_class->byval_arg;
1365 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, sp_offset, t);
1370 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
1374 MonoMethodSignature *sig;
1377 int sentinelpos = 0, sp_offset = 0;
1379 sig = call->signature;
1380 n = sig->param_count + sig->hasthis;
1381 sig_ret = mini_get_underlying_type (sig->ret);
1383 cinfo = get_call_info (cfg->mempool, sig);
1384 call->call_info = cinfo;
1386 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1387 sentinelpos = sig->sentinelpos + (sig->hasthis ? 1 : 0);
1389 if (sig_ret && MONO_TYPE_ISSTRUCT (sig_ret)) {
1390 if (cinfo->ret.storage == ArgValuetypeInReg && cinfo->ret.pair_storage[0] != ArgNone ) {
1392 * Tell the JIT to use a more efficient calling convention: call using
1393 * OP_CALL, compute the result location after the call, and save the
1396 call->vret_in_reg = TRUE;
1397 #if defined(__APPLE__)
1398 if (cinfo->ret.pair_storage [0] == ArgOnDoubleFpStack || cinfo->ret.pair_storage [0] == ArgOnFloatFpStack)
1399 call->vret_in_reg_fp = TRUE;
1402 NULLIFY_INS (call->vret_var);
1406 // FIXME: Emit EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF everywhere
1408 /* Handle the case where there are no implicit arguments */
1409 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sentinelpos)) {
1410 emit_sig_cookie (cfg, call, cinfo);
1411 sp_offset = cinfo->sig_cookie.offset;
1412 emit_gc_param_slot_def (cfg, sp_offset, NULL);
1415 /* Arguments are pushed in the reverse order */
1416 for (i = n - 1; i >= 0; i --) {
1417 ArgInfo *ainfo = cinfo->args + i;
1418 MonoType *orig_type, *t;
1421 if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 1 && i == 0) {
1424 /* Push the vret arg before the first argument */
1425 MONO_INST_NEW (cfg, vtarg, OP_STORE_MEMBASE_REG);
1426 vtarg->type = STACK_MP;
1427 vtarg->inst_destbasereg = X86_ESP;
1428 vtarg->sreg1 = call->vret_var->dreg;
1429 vtarg->inst_offset = cinfo->ret.offset;
1430 MONO_ADD_INS (cfg->cbb, vtarg);
1431 emit_gc_param_slot_def (cfg, cinfo->ret.offset, NULL);
1434 if (i >= sig->hasthis)
1435 t = sig->params [i - sig->hasthis];
1437 t = &mono_defaults.int_class->byval_arg;
1439 t = mini_get_underlying_type (t);
1441 MONO_INST_NEW (cfg, arg, OP_X86_PUSH);
1443 in = call->args [i];
1444 arg->cil_code = in->cil_code;
1445 arg->sreg1 = in->dreg;
1446 arg->type = in->type;
1448 g_assert (in->dreg != -1);
1450 if (ainfo->storage == ArgGSharedVt) {
1451 arg->opcode = OP_OUTARG_VT;
1452 arg->sreg1 = in->dreg;
1453 arg->klass = in->klass;
1454 arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
1455 memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
1457 MONO_ADD_INS (cfg->cbb, arg);
1458 } else if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(t))) {
1462 g_assert (in->klass);
1464 if (t->type == MONO_TYPE_TYPEDBYREF) {
1465 size = sizeof (MonoTypedRef);
1466 align = sizeof (gpointer);
1469 size = mini_type_stack_size_full (&in->klass->byval_arg, &align, sig->pinvoke);
1472 if (size > 0 || ainfo->pass_empty_struct) {
1473 arg->opcode = OP_OUTARG_VT;
1474 arg->sreg1 = in->dreg;
1475 arg->klass = in->klass;
1476 arg->backend.size = size;
1477 arg->inst_p0 = call;
1478 arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
1479 memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
1481 MONO_ADD_INS (cfg->cbb, arg);
1482 if (ainfo->storage != ArgValuetypeInReg) {
1483 emit_gc_param_slot_def (cfg, ainfo->offset, orig_type);
1487 switch (ainfo->storage) {
1490 if (t->type == MONO_TYPE_R4) {
1491 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, X86_ESP, ainfo->offset, in->dreg);
1493 } else if (t->type == MONO_TYPE_R8) {
1494 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, X86_ESP, ainfo->offset, in->dreg);
1496 } else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_U8) {
1497 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, ainfo->offset + 4, MONO_LVREG_MS (in->dreg));
1498 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, ainfo->offset, MONO_LVREG_LS (in->dreg));
1501 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, ainfo->offset, in->dreg);
1505 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, ainfo->offset, in->dreg);
1510 arg->opcode = OP_MOVE;
1511 arg->dreg = ainfo->reg;
1512 MONO_ADD_INS (cfg->cbb, arg);
1516 g_assert_not_reached ();
1519 if (cfg->compute_gc_maps) {
1521 /* FIXME: The == STACK_OBJ check might be fragile ? */
1522 if (sig->hasthis && i == 0 && call->args [i]->type == STACK_OBJ) {
1524 if (call->need_unbox_trampoline)
1525 /* The unbox trampoline transforms this into a managed pointer */
1526 emit_gc_param_slot_def (cfg, ainfo->offset, &mono_defaults.int_class->this_arg);
1528 emit_gc_param_slot_def (cfg, ainfo->offset, &mono_defaults.object_class->byval_arg);
1530 emit_gc_param_slot_def (cfg, ainfo->offset, orig_type);
1534 for (j = 0; j < argsize; j += 4)
1535 emit_gc_param_slot_def (cfg, ainfo->offset + j, NULL);
1540 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sentinelpos)) {
1541 /* Emit the signature cookie just before the implicit arguments */
1542 emit_sig_cookie (cfg, call, cinfo);
1543 emit_gc_param_slot_def (cfg, cinfo->sig_cookie.offset, NULL);
1547 if (sig_ret && (MONO_TYPE_ISSTRUCT (sig_ret) || cinfo->vtype_retaddr)) {
1550 if (cinfo->ret.storage == ArgValuetypeInReg) {
1553 else if (cinfo->ret.storage == ArgInIReg) {
1555 /* The return address is passed in a register */
1556 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
1557 vtarg->sreg1 = call->inst.dreg;
1558 vtarg->dreg = mono_alloc_ireg (cfg);
1559 MONO_ADD_INS (cfg->cbb, vtarg);
1561 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
1562 } else if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 0) {
1563 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, cinfo->ret.offset, call->vret_var->dreg);
1564 emit_gc_param_slot_def (cfg, cinfo->ret.offset, NULL);
1568 call->stack_usage = cinfo->stack_usage;
1569 call->stack_align_amount = cinfo->stack_align_amount;
1573 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
1575 MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
1576 ArgInfo *ainfo = ins->inst_p1;
1577 int size = ins->backend.size;
1579 if (ainfo->storage == ArgValuetypeInReg) {
1580 int dreg = mono_alloc_ireg (cfg);
1583 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, dreg, src->dreg, 0);
1586 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, dreg, src->dreg, 0);
1589 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, src->dreg, 0);
1593 g_assert_not_reached ();
1595 mono_call_inst_add_outarg_reg (cfg, call, dreg, ainfo->reg, FALSE);
1598 if (cfg->gsharedvt && mini_is_gsharedvt_klass (ins->klass)) {
1600 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, ainfo->offset, src->dreg);
1601 } else if (size <= 4) {
1602 int dreg = mono_alloc_ireg (cfg);
1603 if (ainfo->pass_empty_struct) {
1604 //Pass empty struct value as 0 on platforms representing empty structs as 1 byte.
1605 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
1607 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, src->dreg, 0);
1609 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, X86_ESP, ainfo->offset, dreg);
1610 } else if (size <= 20) {
1611 mini_emit_memcpy (cfg, X86_ESP, ainfo->offset, src->dreg, 0, size, 4);
1613 // FIXME: Code growth
1614 mini_emit_memcpy (cfg, X86_ESP, ainfo->offset, src->dreg, 0, size, 4);
1620 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
1622 MonoType *ret = mini_get_underlying_type (mono_method_signature (method)->ret);
1625 if (ret->type == MONO_TYPE_R4) {
1626 if (COMPILE_LLVM (cfg))
1627 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
1630 } else if (ret->type == MONO_TYPE_R8) {
1631 if (COMPILE_LLVM (cfg))
1632 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
1635 } else if (ret->type == MONO_TYPE_I8 || ret->type == MONO_TYPE_U8) {
1636 if (COMPILE_LLVM (cfg))
1637 MONO_EMIT_NEW_UNALU (cfg, OP_LMOVE, cfg->ret->dreg, val->dreg);
1639 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, X86_EAX, MONO_LVREG_LS (val->dreg));
1640 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, X86_EDX, MONO_LVREG_MS (val->dreg));
1646 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
1650 * Allow tracing to work with this interface (with an optional argument)
1653 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1657 g_assert (MONO_ARCH_FRAME_ALIGNMENT >= 8);
1658 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 8);
1660 /* if some args are passed in registers, we need to save them here */
1661 x86_push_reg (code, X86_EBP);
1663 if (cfg->compile_aot) {
1664 x86_push_imm (code, cfg->method);
1665 x86_mov_reg_imm (code, X86_EAX, func);
1666 x86_call_reg (code, X86_EAX);
1668 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
1669 x86_push_imm (code, cfg->method);
1670 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1671 x86_call_code (code, 0);
1673 x86_alu_reg_imm (code, X86_ADD, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT);
1687 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
1690 int arg_size = 0, stack_usage = 0, save_mode = SAVE_NONE;
1691 MonoMethod *method = cfg->method;
1692 MonoType *ret_type = mini_get_underlying_type (mono_method_signature (method)->ret);
1694 switch (ret_type->type) {
1695 case MONO_TYPE_VOID:
1696 /* special case string .ctor icall */
1697 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class) {
1698 save_mode = SAVE_EAX;
1699 stack_usage = enable_arguments ? 8 : 4;
1701 save_mode = SAVE_NONE;
1705 save_mode = SAVE_EAX_EDX;
1706 stack_usage = enable_arguments ? 16 : 8;
1710 save_mode = SAVE_FP;
1711 stack_usage = enable_arguments ? 16 : 8;
1713 case MONO_TYPE_GENERICINST:
1714 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
1715 save_mode = SAVE_EAX;
1716 stack_usage = enable_arguments ? 8 : 4;
1720 case MONO_TYPE_VALUETYPE:
1721 // FIXME: Handle SMALL_STRUCT_IN_REG here for proper alignment on darwin-x86
1722 save_mode = SAVE_STRUCT;
1723 stack_usage = enable_arguments ? 4 : 0;
1726 save_mode = SAVE_EAX;
1727 stack_usage = enable_arguments ? 8 : 4;
1731 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - stack_usage - 4);
1733 switch (save_mode) {
1735 x86_push_reg (code, X86_EDX);
1736 x86_push_reg (code, X86_EAX);
1737 if (enable_arguments) {
1738 x86_push_reg (code, X86_EDX);
1739 x86_push_reg (code, X86_EAX);
1744 x86_push_reg (code, X86_EAX);
1745 if (enable_arguments) {
1746 x86_push_reg (code, X86_EAX);
1751 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1752 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1753 if (enable_arguments) {
1754 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1755 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1760 if (enable_arguments) {
1761 x86_push_membase (code, X86_EBP, 8);
1770 if (cfg->compile_aot) {
1771 x86_push_imm (code, method);
1772 x86_mov_reg_imm (code, X86_EAX, func);
1773 x86_call_reg (code, X86_EAX);
1775 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
1776 x86_push_imm (code, method);
1777 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1778 x86_call_code (code, 0);
1781 x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
1783 switch (save_mode) {
1785 x86_pop_reg (code, X86_EAX);
1786 x86_pop_reg (code, X86_EDX);
1789 x86_pop_reg (code, X86_EAX);
1792 x86_fld_membase (code, X86_ESP, 0, TRUE);
1793 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1800 x86_alu_reg_imm (code, X86_ADD, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - stack_usage);
1805 #define EMIT_COND_BRANCH(ins,cond,sign) \
1806 if (ins->inst_true_bb->native_offset) { \
1807 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
1809 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1810 if ((cfg->opt & MONO_OPT_BRANCH) && \
1811 x86_is_imm8 (ins->inst_true_bb->max_offset - cpos)) \
1812 x86_branch8 (code, cond, 0, sign); \
1814 x86_branch32 (code, cond, 0, sign); \
1818 * Emit an exception if condition is fail and
1819 * if possible do a directly branch to target
1821 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
1823 MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
1824 if (tins == NULL) { \
1825 mono_add_patch_info (cfg, code - cfg->native_code, \
1826 MONO_PATCH_INFO_EXC, exc_name); \
1827 x86_branch32 (code, cond, 0, signed); \
1829 EMIT_COND_BRANCH (tins, cond, signed); \
1833 #define EMIT_FPCOMPARE(code) do { \
1834 x86_fcompp (code); \
1835 x86_fnstsw (code); \
1840 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data)
1842 gboolean needs_paddings = TRUE;
1844 MonoJumpInfo *jinfo = NULL;
1846 if (cfg->abs_patches) {
1847 jinfo = g_hash_table_lookup (cfg->abs_patches, data);
1848 if (jinfo && jinfo->type == MONO_PATCH_INFO_JIT_ICALL_ADDR)
1849 needs_paddings = FALSE;
1852 if (cfg->compile_aot)
1853 needs_paddings = FALSE;
1854 /*The address must be 4 bytes aligned to avoid spanning multiple cache lines.
1855 This is required for code patching to be safe on SMP machines.
1857 pad_size = (guint32)(code + 1 - cfg->native_code) & 0x3;
1858 if (needs_paddings && pad_size)
1859 x86_padding (code, 4 - pad_size);
1861 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
1862 x86_call_code (code, 0);
1867 #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)))
1870 * mono_peephole_pass_1:
1872 * Perform peephole opts which should/can be performed before local regalloc
1875 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
1879 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
1880 MonoInst *last_ins = mono_inst_prev (ins, FILTER_IL_SEQ_POINT);
1882 switch (ins->opcode) {
1885 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS)) {
1887 * X86_LEA is like ADD, but doesn't have the
1888 * sreg1==dreg restriction.
1890 ins->opcode = OP_X86_LEA_MEMBASE;
1891 ins->inst_basereg = ins->sreg1;
1892 } else if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1893 ins->opcode = OP_X86_INC_REG;
1897 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS)) {
1898 ins->opcode = OP_X86_LEA_MEMBASE;
1899 ins->inst_basereg = ins->sreg1;
1900 ins->inst_imm = -ins->inst_imm;
1901 } else if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1902 ins->opcode = OP_X86_DEC_REG;
1904 case OP_COMPARE_IMM:
1905 case OP_ICOMPARE_IMM:
1906 /* OP_COMPARE_IMM (reg, 0)
1908 * OP_X86_TEST_NULL (reg)
1911 ins->opcode = OP_X86_TEST_NULL;
1913 case OP_X86_COMPARE_MEMBASE_IMM:
1915 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1916 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
1918 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1919 * OP_COMPARE_IMM reg, imm
1921 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
1923 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
1924 ins->inst_basereg == last_ins->inst_destbasereg &&
1925 ins->inst_offset == last_ins->inst_offset) {
1926 ins->opcode = OP_COMPARE_IMM;
1927 ins->sreg1 = last_ins->sreg1;
1929 /* check if we can remove cmp reg,0 with test null */
1931 ins->opcode = OP_X86_TEST_NULL;
1935 case OP_X86_PUSH_MEMBASE:
1936 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG ||
1937 last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1938 ins->inst_basereg == last_ins->inst_destbasereg &&
1939 ins->inst_offset == last_ins->inst_offset) {
1940 ins->opcode = OP_X86_PUSH;
1941 ins->sreg1 = last_ins->sreg1;
1946 mono_peephole_ins (bb, ins);
1951 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
1955 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
1956 switch (ins->opcode) {
1958 /* reg = 0 -> XOR (reg, reg) */
1959 /* XOR sets cflags on x86, so we cant do it always */
1960 if (ins->inst_c0 == 0 && (!ins->next || (ins->next && INST_IGNORES_CFLAGS (ins->next->opcode)))) {
1963 ins->opcode = OP_IXOR;
1964 ins->sreg1 = ins->dreg;
1965 ins->sreg2 = ins->dreg;
1968 * Convert succeeding STORE_MEMBASE_IMM 0 ins to STORE_MEMBASE_REG
1969 * since it takes 3 bytes instead of 7.
1971 for (ins2 = mono_inst_next (ins, FILTER_IL_SEQ_POINT); ins2; ins2 = ins2->next) {
1972 if ((ins2->opcode == OP_STORE_MEMBASE_IMM) && (ins2->inst_imm == 0)) {
1973 ins2->opcode = OP_STORE_MEMBASE_REG;
1974 ins2->sreg1 = ins->dreg;
1976 else if ((ins2->opcode == OP_STOREI4_MEMBASE_IMM) && (ins2->inst_imm == 0)) {
1977 ins2->opcode = OP_STOREI4_MEMBASE_REG;
1978 ins2->sreg1 = ins->dreg;
1980 else if ((ins2->opcode == OP_STOREI1_MEMBASE_IMM) || (ins2->opcode == OP_STOREI2_MEMBASE_IMM)) {
1981 /* Continue iteration */
1990 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1991 ins->opcode = OP_X86_INC_REG;
1995 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1996 ins->opcode = OP_X86_DEC_REG;
2000 mono_peephole_ins (bb, ins);
2004 #define NEW_INS(cfg,ins,dest,op) do { \
2005 MONO_INST_NEW ((cfg), (dest), (op)); \
2006 (dest)->cil_code = (ins)->cil_code; \
2007 mono_bblock_insert_before_ins (bb, ins, (dest)); \
2011 * mono_arch_lowering_pass:
2013 * Converts complex opcodes into simpler ones so that each IR instruction
2014 * corresponds to one machine instruction.
2017 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
2019 MonoInst *ins, *next;
2022 * FIXME: Need to add more instructions, but the current machine
2023 * description can't model some parts of the composite instructions like
2026 MONO_BB_FOR_EACH_INS_SAFE (bb, next, ins) {
2027 switch (ins->opcode) {
2030 case OP_IDIV_UN_IMM:
2031 case OP_IREM_UN_IMM:
2033 * Keep the cases where we could generated optimized code, otherwise convert
2034 * to the non-imm variant.
2036 if ((ins->opcode == OP_IREM_IMM) && mono_is_power_of_two (ins->inst_imm) >= 0)
2038 mono_decompose_op_imm (cfg, bb, ins);
2040 #ifdef MONO_ARCH_SIMD_INTRINSICS
2041 case OP_EXPAND_I1: {
2043 int temp_reg1 = mono_alloc_ireg (cfg);
2044 int temp_reg2 = mono_alloc_ireg (cfg);
2045 int original_reg = ins->sreg1;
2047 NEW_INS (cfg, ins, temp, OP_ICONV_TO_U1);
2048 temp->sreg1 = original_reg;
2049 temp->dreg = temp_reg1;
2051 NEW_INS (cfg, ins, temp, OP_SHL_IMM);
2052 temp->sreg1 = temp_reg1;
2053 temp->dreg = temp_reg2;
2056 NEW_INS (cfg, ins, temp, OP_IOR);
2057 temp->sreg1 = temp->dreg = temp_reg2;
2058 temp->sreg2 = temp_reg1;
2060 ins->opcode = OP_EXPAND_I2;
2061 ins->sreg1 = temp_reg2;
2070 bb->max_vreg = cfg->next_vreg;
2074 branch_cc_table [] = {
2075 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
2076 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
2077 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
2080 /* Maps CMP_... constants to X86_CC_... constants */
2083 X86_CC_EQ, X86_CC_NE, X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT,
2084 X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT
2088 cc_signed_table [] = {
2089 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
2090 FALSE, FALSE, FALSE, FALSE
2093 static unsigned char*
2094 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
2096 #define XMM_TEMP_REG 0
2097 /*This SSE2 optimization must not be done which OPT_SIMD in place as it clobbers xmm0.*/
2098 /*The xmm pass decomposes OP_FCONV_ ops anyway anyway.*/
2099 if (cfg->opt & MONO_OPT_SSE2 && size < 8 && !(cfg->opt & MONO_OPT_SIMD)) {
2100 /* optimize by assigning a local var for this use so we avoid
2101 * the stack manipulations */
2102 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2103 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
2104 x86_movsd_reg_membase (code, XMM_TEMP_REG, X86_ESP, 0);
2105 x86_cvttsd2si (code, dreg, XMM_TEMP_REG);
2106 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
2108 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
2110 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
2113 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
2114 x86_fnstcw_membase(code, X86_ESP, 0);
2115 x86_mov_reg_membase (code, dreg, X86_ESP, 0, 2);
2116 x86_alu_reg_imm (code, X86_OR, dreg, 0xc00);
2117 x86_mov_membase_reg (code, X86_ESP, 2, dreg, 2);
2118 x86_fldcw_membase (code, X86_ESP, 2);
2120 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2121 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
2122 x86_pop_reg (code, dreg);
2123 /* FIXME: need the high register
2124 * x86_pop_reg (code, dreg_high);
2127 x86_push_reg (code, X86_EAX); // SP = SP - 4
2128 x86_fist_pop_membase (code, X86_ESP, 0, FALSE);
2129 x86_pop_reg (code, dreg);
2131 x86_fldcw_membase (code, X86_ESP, 0);
2132 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2135 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
2137 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
2141 static unsigned char*
2142 mono_emit_stack_alloc (MonoCompile *cfg, guchar *code, MonoInst* tree)
2144 int sreg = tree->sreg1;
2145 int need_touch = FALSE;
2147 #if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
2156 * If requested stack size is larger than one page,
2157 * perform stack-touch operation
2160 * Generate stack probe code.
2161 * Under Windows, it is necessary to allocate one page at a time,
2162 * "touching" stack after each successful sub-allocation. This is
2163 * because of the way stack growth is implemented - there is a
2164 * guard page before the lowest stack page that is currently commited.
2165 * Stack normally grows sequentially so OS traps access to the
2166 * guard page and commits more pages when needed.
2168 x86_test_reg_imm (code, sreg, ~0xFFF);
2169 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2171 br[2] = code; /* loop */
2172 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
2173 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
2176 * By the end of the loop, sreg2 is smaller than 0x1000, so the init routine
2177 * that follows only initializes the last part of the area.
2179 /* Same as the init code below with size==0x1000 */
2180 if (tree->flags & MONO_INST_INIT) {
2181 x86_push_reg (code, X86_EAX);
2182 x86_push_reg (code, X86_ECX);
2183 x86_push_reg (code, X86_EDI);
2184 x86_mov_reg_imm (code, X86_ECX, (0x1000 >> 2));
2185 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2186 if (cfg->param_area)
2187 x86_lea_membase (code, X86_EDI, X86_ESP, 12 + ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
2189 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
2191 x86_prefix (code, X86_REP_PREFIX);
2193 x86_pop_reg (code, X86_EDI);
2194 x86_pop_reg (code, X86_ECX);
2195 x86_pop_reg (code, X86_EAX);
2198 x86_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
2199 x86_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
2200 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
2201 x86_patch (br[3], br[2]);
2202 x86_test_reg_reg (code, sreg, sreg);
2203 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2204 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
2206 br[1] = code; x86_jump8 (code, 0);
2208 x86_patch (br[0], code);
2209 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
2210 x86_patch (br[1], code);
2211 x86_patch (br[4], code);
2214 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
2216 if (tree->flags & MONO_INST_INIT) {
2218 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
2219 x86_push_reg (code, X86_EAX);
2222 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
2223 x86_push_reg (code, X86_ECX);
2226 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
2227 x86_push_reg (code, X86_EDI);
2231 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
2232 if (sreg != X86_ECX)
2233 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
2234 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2236 if (cfg->param_area)
2237 x86_lea_membase (code, X86_EDI, X86_ESP, offset + ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
2239 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
2241 x86_prefix (code, X86_REP_PREFIX);
2244 if (tree->dreg != X86_EDI && sreg != X86_EDI)
2245 x86_pop_reg (code, X86_EDI);
2246 if (tree->dreg != X86_ECX && sreg != X86_ECX)
2247 x86_pop_reg (code, X86_ECX);
2248 if (tree->dreg != X86_EAX && sreg != X86_EAX)
2249 x86_pop_reg (code, X86_EAX);
2256 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
2258 /* Move return value to the target register */
2259 switch (ins->opcode) {
2262 case OP_CALL_MEMBASE:
2263 if (ins->dreg != X86_EAX)
2264 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
2274 static int tls_gs_offset;
2278 mono_arch_have_fast_tls (void)
2281 static gboolean have_fast_tls = FALSE;
2282 static gboolean inited = FALSE;
2285 if (mini_get_debug_options ()->use_fallback_tls)
2288 return have_fast_tls;
2290 ins = (guint32*)pthread_getspecific;
2292 * We're looking for these two instructions:
2294 * mov 0x4(%esp),%eax
2295 * mov %gs:[offset](,%eax,4),%eax
2297 have_fast_tls = ins [0] == 0x0424448b && ins [1] == 0x85048b65;
2298 tls_gs_offset = ins [2];
2301 return have_fast_tls;
2302 #elif defined(TARGET_ANDROID)
2305 if (mini_get_debug_options ()->use_fallback_tls)
2312 mono_x86_emit_tls_get (guint8* code, int dreg, int tls_offset)
2314 #if defined(TARGET_MACH)
2315 x86_prefix (code, X86_GS_PREFIX);
2316 x86_mov_reg_mem (code, dreg, tls_gs_offset + (tls_offset * 4), 4);
2317 #elif defined(TARGET_WIN32)
2319 * See the Under the Hood article in the May 1996 issue of Microsoft Systems
2320 * Journal and/or a disassembly of the TlsGet () function.
2322 x86_prefix (code, X86_FS_PREFIX);
2323 x86_mov_reg_mem (code, dreg, 0x18, 4);
2324 if (tls_offset < 64) {
2325 x86_mov_reg_membase (code, dreg, dreg, 3600 + (tls_offset * 4), 4);
2329 g_assert (tls_offset < 0x440);
2330 /* Load TEB->TlsExpansionSlots */
2331 x86_mov_reg_membase (code, dreg, dreg, 0xf94, 4);
2332 x86_test_reg_reg (code, dreg, dreg);
2334 x86_branch (code, X86_CC_EQ, code, TRUE);
2335 x86_mov_reg_membase (code, dreg, dreg, (tls_offset * 4) - 0x100, 4);
2336 x86_patch (buf [0], code);
2339 if (optimize_for_xen) {
2340 x86_prefix (code, X86_GS_PREFIX);
2341 x86_mov_reg_mem (code, dreg, 0, 4);
2342 x86_mov_reg_membase (code, dreg, dreg, tls_offset, 4);
2344 x86_prefix (code, X86_GS_PREFIX);
2345 x86_mov_reg_mem (code, dreg, tls_offset, 4);
2352 mono_x86_emit_tls_set (guint8* code, int sreg, int tls_offset)
2354 #if defined(TARGET_MACH)
2355 x86_prefix (code, X86_GS_PREFIX);
2356 x86_mov_mem_reg (code, tls_gs_offset + (tls_offset * 4), sreg, 4);
2357 #elif defined(TARGET_WIN32)
2358 g_assert_not_reached ();
2360 x86_prefix (code, X86_GS_PREFIX);
2361 x86_mov_mem_reg (code, tls_offset, sreg, 4);
2369 * Emit code to initialize an LMF structure at LMF_OFFSET.
2372 emit_setup_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset, int cfa_offset)
2374 /* save all caller saved regs */
2375 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, ebx), X86_EBX, sizeof (mgreg_t));
2376 mono_emit_unwind_op_offset (cfg, code, X86_EBX, - cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, ebx));
2377 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, edi), X86_EDI, sizeof (mgreg_t));
2378 mono_emit_unwind_op_offset (cfg, code, X86_EDI, - cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, edi));
2379 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, esi), X86_ESI, sizeof (mgreg_t));
2380 mono_emit_unwind_op_offset (cfg, code, X86_ESI, - cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, esi));
2381 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, ebp), X86_EBP, sizeof (mgreg_t));
2383 /* save the current IP */
2384 if (cfg->compile_aot) {
2385 /* This pushes the current ip */
2386 x86_call_imm (code, 0);
2387 x86_pop_reg (code, X86_EAX);
2389 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
2390 x86_mov_reg_imm (code, X86_EAX, 0);
2392 x86_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, eip), X86_EAX, sizeof (mgreg_t));
2394 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, eip), SLOT_NOREF);
2395 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, ebp), SLOT_NOREF);
2396 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, esi), SLOT_NOREF);
2397 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, edi), SLOT_NOREF);
2398 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, ebx), SLOT_NOREF);
2399 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, esp), SLOT_NOREF);
2400 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, method), SLOT_NOREF);
2401 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, lmf_addr), SLOT_NOREF);
2402 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset + lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), SLOT_NOREF);
2407 /* benchmark and set based on cpu */
2408 #define LOOP_ALIGNMENT 8
2409 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
2413 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2418 guint8 *code = cfg->native_code + cfg->code_len;
2421 if (cfg->opt & MONO_OPT_LOOP) {
2422 int pad, align = LOOP_ALIGNMENT;
2423 /* set alignment depending on cpu */
2424 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
2426 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
2427 x86_padding (code, pad);
2428 cfg->code_len += pad;
2429 bb->native_offset = cfg->code_len;
2433 if (cfg->verbose_level > 2)
2434 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2436 cpos = bb->max_offset;
2438 if ((cfg->prof_options & MONO_PROFILE_COVERAGE) && cfg->coverage_info) {
2439 MonoProfileCoverageInfo *cov = cfg->coverage_info;
2440 g_assert (!cfg->compile_aot);
2443 cov->data [bb->dfn].cil_code = bb->cil_code;
2444 /* this is not thread save, but good enough */
2445 x86_inc_mem (code, &cov->data [bb->dfn].count);
2448 offset = code - cfg->native_code;
2450 mono_debug_open_block (cfg, bb, offset);
2452 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)
2453 x86_breakpoint (code);
2455 MONO_BB_FOR_EACH_INS (bb, ins) {
2456 offset = code - cfg->native_code;
2458 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
2460 #define EXTRA_CODE_SPACE (16)
2462 if (G_UNLIKELY (offset > (cfg->code_size - max_len - EXTRA_CODE_SPACE))) {
2463 cfg->code_size *= 2;
2464 cfg->native_code = mono_realloc_native_code(cfg);
2465 code = cfg->native_code + offset;
2466 cfg->stat_code_reallocs++;
2469 if (cfg->debug_info)
2470 mono_debug_record_line_number (cfg, ins, offset);
2472 switch (ins->opcode) {
2474 x86_mul_reg (code, ins->sreg2, TRUE);
2477 x86_mul_reg (code, ins->sreg2, FALSE);
2479 case OP_X86_SETEQ_MEMBASE:
2480 case OP_X86_SETNE_MEMBASE:
2481 x86_set_membase (code, ins->opcode == OP_X86_SETEQ_MEMBASE ? X86_CC_EQ : X86_CC_NE,
2482 ins->inst_basereg, ins->inst_offset, TRUE);
2484 case OP_STOREI1_MEMBASE_IMM:
2485 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
2487 case OP_STOREI2_MEMBASE_IMM:
2488 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
2490 case OP_STORE_MEMBASE_IMM:
2491 case OP_STOREI4_MEMBASE_IMM:
2492 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
2494 case OP_STOREI1_MEMBASE_REG:
2495 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
2497 case OP_STOREI2_MEMBASE_REG:
2498 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
2500 case OP_STORE_MEMBASE_REG:
2501 case OP_STOREI4_MEMBASE_REG:
2502 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
2504 case OP_STORE_MEM_IMM:
2505 x86_mov_mem_imm (code, ins->inst_p0, ins->inst_c0, 4);
2508 x86_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
2512 /* These are created by the cprop pass so they use inst_imm as the source */
2513 x86_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
2516 x86_widen_mem (code, ins->dreg, ins->inst_imm, FALSE, FALSE);
2519 x86_widen_mem (code, ins->dreg, ins->inst_imm, FALSE, TRUE);
2521 case OP_LOAD_MEMBASE:
2522 case OP_LOADI4_MEMBASE:
2523 case OP_LOADU4_MEMBASE:
2524 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
2526 case OP_LOADU1_MEMBASE:
2527 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
2529 case OP_LOADI1_MEMBASE:
2530 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
2532 case OP_LOADU2_MEMBASE:
2533 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
2535 case OP_LOADI2_MEMBASE:
2536 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
2538 case OP_ICONV_TO_I1:
2540 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2542 case OP_ICONV_TO_I2:
2544 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2546 case OP_ICONV_TO_U1:
2547 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
2549 case OP_ICONV_TO_U2:
2550 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
2554 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
2556 case OP_COMPARE_IMM:
2557 case OP_ICOMPARE_IMM:
2558 x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
2560 case OP_X86_COMPARE_MEMBASE_REG:
2561 x86_alu_membase_reg (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2563 case OP_X86_COMPARE_MEMBASE_IMM:
2564 x86_alu_membase_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2566 case OP_X86_COMPARE_MEMBASE8_IMM:
2567 x86_alu_membase8_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2569 case OP_X86_COMPARE_REG_MEMBASE:
2570 x86_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
2572 case OP_X86_COMPARE_MEM_IMM:
2573 x86_alu_mem_imm (code, X86_CMP, ins->inst_offset, ins->inst_imm);
2575 case OP_X86_TEST_NULL:
2576 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2578 case OP_X86_ADD_MEMBASE_IMM:
2579 x86_alu_membase_imm (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2581 case OP_X86_ADD_REG_MEMBASE:
2582 x86_alu_reg_membase (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset);
2584 case OP_X86_SUB_MEMBASE_IMM:
2585 x86_alu_membase_imm (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2587 case OP_X86_SUB_REG_MEMBASE:
2588 x86_alu_reg_membase (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset);
2590 case OP_X86_AND_MEMBASE_IMM:
2591 x86_alu_membase_imm (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2593 case OP_X86_OR_MEMBASE_IMM:
2594 x86_alu_membase_imm (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2596 case OP_X86_XOR_MEMBASE_IMM:
2597 x86_alu_membase_imm (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2599 case OP_X86_ADD_MEMBASE_REG:
2600 x86_alu_membase_reg (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2602 case OP_X86_SUB_MEMBASE_REG:
2603 x86_alu_membase_reg (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2605 case OP_X86_AND_MEMBASE_REG:
2606 x86_alu_membase_reg (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2608 case OP_X86_OR_MEMBASE_REG:
2609 x86_alu_membase_reg (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2611 case OP_X86_XOR_MEMBASE_REG:
2612 x86_alu_membase_reg (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2614 case OP_X86_INC_MEMBASE:
2615 x86_inc_membase (code, ins->inst_basereg, ins->inst_offset);
2617 case OP_X86_INC_REG:
2618 x86_inc_reg (code, ins->dreg);
2620 case OP_X86_DEC_MEMBASE:
2621 x86_dec_membase (code, ins->inst_basereg, ins->inst_offset);
2623 case OP_X86_DEC_REG:
2624 x86_dec_reg (code, ins->dreg);
2626 case OP_X86_MUL_REG_MEMBASE:
2627 x86_imul_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
2629 case OP_X86_AND_REG_MEMBASE:
2630 x86_alu_reg_membase (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset);
2632 case OP_X86_OR_REG_MEMBASE:
2633 x86_alu_reg_membase (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset);
2635 case OP_X86_XOR_REG_MEMBASE:
2636 x86_alu_reg_membase (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset);
2639 x86_breakpoint (code);
2641 case OP_RELAXED_NOP:
2642 x86_prefix (code, X86_REP_PREFIX);
2650 case OP_DUMMY_STORE:
2651 case OP_DUMMY_ICONST:
2652 case OP_DUMMY_R8CONST:
2653 case OP_NOT_REACHED:
2656 case OP_IL_SEQ_POINT:
2657 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
2659 case OP_SEQ_POINT: {
2662 if (cfg->compile_aot)
2665 /* Have to use ecx as a temp reg since this can occur after OP_SETRET */
2668 * We do this _before_ the breakpoint, so single stepping after
2669 * a breakpoint is hit will step to the next IL offset.
2671 if (ins->flags & MONO_INST_SINGLE_STEP_LOC) {
2672 MonoInst *var = cfg->arch.ss_tramp_var;
2676 g_assert (var->opcode == OP_REGOFFSET);
2677 /* Load ss_tramp_var */
2678 /* This is equal to &ss_trampoline */
2679 x86_mov_reg_membase (code, X86_ECX, var->inst_basereg, var->inst_offset, sizeof (mgreg_t));
2680 x86_mov_reg_membase (code, X86_ECX, X86_ECX, 0, sizeof (mgreg_t));
2681 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
2682 br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
2683 x86_call_reg (code, X86_ECX);
2684 x86_patch (br [0], code);
2688 * Many parts of sdb depend on the ip after the single step trampoline call to be equal to the seq point offset.
2689 * This means we have to put the loading of bp_tramp_var after the offset.
2692 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
2694 MonoInst *var = cfg->arch.bp_tramp_var;
2697 g_assert (var->opcode == OP_REGOFFSET);
2698 /* Load the address of the bp trampoline */
2699 /* This needs to be constant size */
2700 guint8 *start = code;
2701 x86_mov_reg_membase (code, X86_ECX, var->inst_basereg, var->inst_offset, 4);
2702 if (code < start + OP_SEQ_POINT_BP_OFFSET) {
2703 int size = start + OP_SEQ_POINT_BP_OFFSET - code;
2704 x86_padding (code, size);
2707 * A placeholder for a possible breakpoint inserted by
2708 * mono_arch_set_breakpoint ().
2710 for (i = 0; i < 2; ++i)
2713 * Add an additional nop so skipping the bp doesn't cause the ip to point
2714 * to another IL offset.
2722 x86_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
2726 x86_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
2731 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
2735 x86_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
2740 x86_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
2744 x86_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
2749 x86_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
2753 x86_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
2756 x86_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
2760 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
2765 * The code is the same for div/rem, the allocator will allocate dreg
2766 * to RAX/RDX as appropriate.
2768 if (ins->sreg2 == X86_EDX) {
2769 /* cdq clobbers this */
2770 x86_push_reg (code, ins->sreg2);
2772 x86_div_membase (code, X86_ESP, 0, TRUE);
2773 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2776 x86_div_reg (code, ins->sreg2, TRUE);
2781 if (ins->sreg2 == X86_EDX) {
2782 x86_push_reg (code, ins->sreg2);
2783 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2784 x86_div_membase (code, X86_ESP, 0, FALSE);
2785 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2787 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2788 x86_div_reg (code, ins->sreg2, FALSE);
2792 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2794 x86_div_reg (code, ins->sreg2, TRUE);
2797 int power = mono_is_power_of_two (ins->inst_imm);
2799 g_assert (ins->sreg1 == X86_EAX);
2800 g_assert (ins->dreg == X86_EAX);
2801 g_assert (power >= 0);
2804 /* Based on http://compilers.iecc.com/comparch/article/93-04-079 */
2806 x86_alu_reg_imm (code, X86_AND, X86_EAX, 1);
2808 * If the divident is >= 0, this does not nothing. If it is positive, it
2809 * it transforms %eax=0 into %eax=0, and %eax=1 into %eax=-1.
2811 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EDX);
2812 x86_alu_reg_reg (code, X86_SUB, X86_EAX, X86_EDX);
2813 } else if (power == 0) {
2814 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2816 /* Based on gcc code */
2818 /* Add compensation for negative dividents */
2820 x86_shift_reg_imm (code, X86_SHR, X86_EDX, 32 - power);
2821 x86_alu_reg_reg (code, X86_ADD, X86_EAX, X86_EDX);
2822 /* Compute remainder */
2823 x86_alu_reg_imm (code, X86_AND, X86_EAX, (1 << power) - 1);
2824 /* Remove compensation */
2825 x86_alu_reg_reg (code, X86_SUB, X86_EAX, X86_EDX);
2830 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
2834 x86_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
2837 x86_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
2841 x86_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
2844 g_assert (ins->sreg2 == X86_ECX);
2845 x86_shift_reg (code, X86_SHL, ins->dreg);
2848 g_assert (ins->sreg2 == X86_ECX);
2849 x86_shift_reg (code, X86_SAR, ins->dreg);
2853 x86_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
2856 case OP_ISHR_UN_IMM:
2857 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
2860 g_assert (ins->sreg2 == X86_ECX);
2861 x86_shift_reg (code, X86_SHR, ins->dreg);
2865 x86_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
2868 guint8 *jump_to_end;
2870 /* handle shifts below 32 bits */
2871 x86_shld_reg (code, ins->backend.reg3, ins->sreg1);
2872 x86_shift_reg (code, X86_SHL, ins->sreg1);
2874 x86_test_reg_imm (code, X86_ECX, 32);
2875 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
2877 /* handle shift over 32 bit */
2878 x86_mov_reg_reg (code, ins->backend.reg3, ins->sreg1, 4);
2879 x86_clear_reg (code, ins->sreg1);
2881 x86_patch (jump_to_end, code);
2885 guint8 *jump_to_end;
2887 /* handle shifts below 32 bits */
2888 x86_shrd_reg (code, ins->sreg1, ins->backend.reg3);
2889 x86_shift_reg (code, X86_SAR, ins->backend.reg3);
2891 x86_test_reg_imm (code, X86_ECX, 32);
2892 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
2894 /* handle shifts over 31 bits */
2895 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2896 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, 31);
2898 x86_patch (jump_to_end, code);
2902 guint8 *jump_to_end;
2904 /* handle shifts below 32 bits */
2905 x86_shrd_reg (code, ins->sreg1, ins->backend.reg3);
2906 x86_shift_reg (code, X86_SHR, ins->backend.reg3);
2908 x86_test_reg_imm (code, X86_ECX, 32);
2909 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
2911 /* handle shifts over 31 bits */
2912 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2913 x86_clear_reg (code, ins->backend.reg3);
2915 x86_patch (jump_to_end, code);
2919 if (ins->inst_imm >= 32) {
2920 x86_mov_reg_reg (code, ins->backend.reg3, ins->sreg1, 4);
2921 x86_clear_reg (code, ins->sreg1);
2922 x86_shift_reg_imm (code, X86_SHL, ins->backend.reg3, ins->inst_imm - 32);
2924 x86_shld_reg_imm (code, ins->backend.reg3, ins->sreg1, ins->inst_imm);
2925 x86_shift_reg_imm (code, X86_SHL, ins->sreg1, ins->inst_imm);
2929 if (ins->inst_imm >= 32) {
2930 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2931 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, 0x1f);
2932 x86_shift_reg_imm (code, X86_SAR, ins->sreg1, ins->inst_imm - 32);
2934 x86_shrd_reg_imm (code, ins->sreg1, ins->backend.reg3, ins->inst_imm);
2935 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, ins->inst_imm);
2938 case OP_LSHR_UN_IMM:
2939 if (ins->inst_imm >= 32) {
2940 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2941 x86_clear_reg (code, ins->backend.reg3);
2942 x86_shift_reg_imm (code, X86_SHR, ins->sreg1, ins->inst_imm - 32);
2944 x86_shrd_reg_imm (code, ins->sreg1, ins->backend.reg3, ins->inst_imm);
2945 x86_shift_reg_imm (code, X86_SHR, ins->backend.reg3, ins->inst_imm);
2949 x86_not_reg (code, ins->sreg1);
2952 x86_neg_reg (code, ins->sreg1);
2956 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2960 switch (ins->inst_imm) {
2964 if (ins->dreg != ins->sreg1)
2965 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2966 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
2969 /* LEA r1, [r2 + r2*2] */
2970 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
2973 /* LEA r1, [r2 + r2*4] */
2974 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2977 /* LEA r1, [r2 + r2*2] */
2979 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
2980 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
2983 /* LEA r1, [r2 + r2*8] */
2984 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
2987 /* LEA r1, [r2 + r2*4] */
2989 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2990 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
2993 /* LEA r1, [r2 + r2*2] */
2995 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
2996 x86_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
2999 /* 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_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
3005 /* LEA r1, [r2 + r2*4] */
3007 /* LEA r1, [r1 + r1*4] */
3008 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3009 x86_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
3010 x86_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
3013 x86_imul_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_imm);
3018 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
3019 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
3021 case OP_IMUL_OVF_UN: {
3022 /* the mul operation and the exception check should most likely be split */
3023 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
3024 /*g_assert (ins->sreg2 == X86_EAX);
3025 g_assert (ins->dreg == X86_EAX);*/
3026 if (ins->sreg2 == X86_EAX) {
3027 non_eax_reg = ins->sreg1;
3028 } else if (ins->sreg1 == X86_EAX) {
3029 non_eax_reg = ins->sreg2;
3031 /* no need to save since we're going to store to it anyway */
3032 if (ins->dreg != X86_EAX) {
3034 x86_push_reg (code, X86_EAX);
3036 x86_mov_reg_reg (code, X86_EAX, ins->sreg1, 4);
3037 non_eax_reg = ins->sreg2;
3039 if (ins->dreg == X86_EDX) {
3042 x86_push_reg (code, X86_EAX);
3046 x86_push_reg (code, X86_EDX);
3048 x86_mul_reg (code, non_eax_reg, FALSE);
3049 /* save before the check since pop and mov don't change the flags */
3050 if (ins->dreg != X86_EAX)
3051 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
3053 x86_pop_reg (code, X86_EDX);
3055 x86_pop_reg (code, X86_EAX);
3056 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
3060 x86_mov_reg_imm (code, ins->dreg, ins->inst_c0);
3063 g_assert_not_reached ();
3064 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3065 x86_mov_reg_imm (code, ins->dreg, 0);
3068 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3069 x86_mov_reg_imm (code, ins->dreg, 0);
3071 case OP_LOAD_GOTADDR:
3072 g_assert (ins->dreg == MONO_ARCH_GOT_REG);
3073 code = mono_arch_emit_load_got_addr (cfg->native_code, code, cfg, NULL);
3076 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
3077 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, 0xf0f0f0f0, 4);
3079 case OP_X86_PUSH_GOT_ENTRY:
3080 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
3081 x86_push_membase (code, ins->inst_basereg, 0xf0f0f0f0);
3084 if (ins->dreg != ins->sreg1)
3085 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
3088 MonoCallInst *call = (MonoCallInst*)ins;
3091 ins->flags |= MONO_INST_GC_CALLSITE;
3092 ins->backend.pc_offset = code - cfg->native_code;
3094 /* reset offset to make max_len work */
3095 offset = code - cfg->native_code;
3097 g_assert (!cfg->method->save_lmf);
3099 /* restore callee saved registers */
3100 for (i = 0; i < X86_NREG; ++i)
3101 if (X86_IS_CALLEE_SAVED_REG (i) && cfg->used_int_regs & (1 << i))
3103 if (cfg->used_int_regs & (1 << X86_ESI)) {
3104 x86_mov_reg_membase (code, X86_ESI, X86_EBP, pos, 4);
3107 if (cfg->used_int_regs & (1 << X86_EDI)) {
3108 x86_mov_reg_membase (code, X86_EDI, X86_EBP, pos, 4);
3111 if (cfg->used_int_regs & (1 << X86_EBX)) {
3112 x86_mov_reg_membase (code, X86_EBX, X86_EBP, pos, 4);
3116 /* Copy arguments on the stack to our argument area */
3117 for (i = 0; i < call->stack_usage - call->stack_align_amount; i += 4) {
3118 x86_mov_reg_membase (code, X86_EAX, X86_ESP, i, 4);
3119 x86_mov_membase_reg (code, X86_EBP, 8 + i, X86_EAX, 4);
3122 /* restore ESP/EBP */
3124 offset = code - cfg->native_code;
3125 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD_JUMP, call->method);
3126 x86_jump32 (code, 0);
3128 ins->flags |= MONO_INST_GC_CALLSITE;
3129 cfg->disable_aot = TRUE;
3133 /* ensure ins->sreg1 is not NULL
3134 * note that cmp DWORD PTR [eax], eax is one byte shorter than
3135 * cmp DWORD PTR [eax], 0
3137 x86_alu_membase_reg (code, X86_CMP, ins->sreg1, 0, ins->sreg1);
3140 int hreg = ins->sreg1 == X86_EAX? X86_ECX: X86_EAX;
3141 x86_push_reg (code, hreg);
3142 x86_lea_membase (code, hreg, X86_EBP, cfg->sig_cookie);
3143 x86_mov_membase_reg (code, ins->sreg1, 0, hreg, 4);
3144 x86_pop_reg (code, hreg);
3157 case OP_VOIDCALL_REG:
3159 case OP_FCALL_MEMBASE:
3160 case OP_LCALL_MEMBASE:
3161 case OP_VCALL_MEMBASE:
3162 case OP_VCALL2_MEMBASE:
3163 case OP_VOIDCALL_MEMBASE:
3164 case OP_CALL_MEMBASE: {
3167 call = (MonoCallInst*)ins;
3168 cinfo = (CallInfo*)call->call_info;
3170 switch (ins->opcode) {
3177 if (ins->flags & MONO_INST_HAS_METHOD)
3178 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method);
3180 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr);
3186 case OP_VOIDCALL_REG:
3188 x86_call_reg (code, ins->sreg1);
3190 case OP_FCALL_MEMBASE:
3191 case OP_LCALL_MEMBASE:
3192 case OP_VCALL_MEMBASE:
3193 case OP_VCALL2_MEMBASE:
3194 case OP_VOIDCALL_MEMBASE:
3195 case OP_CALL_MEMBASE:
3196 x86_call_membase (code, ins->sreg1, ins->inst_offset);
3199 g_assert_not_reached ();
3202 ins->flags |= MONO_INST_GC_CALLSITE;
3203 ins->backend.pc_offset = code - cfg->native_code;
3204 if (cinfo->callee_stack_pop) {
3205 /* Have to compensate for the stack space popped by the callee */
3206 x86_alu_reg_imm (code, X86_SUB, X86_ESP, cinfo->callee_stack_pop);
3208 code = emit_move_return_value (cfg, ins, code);
3212 x86_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
3214 case OP_X86_LEA_MEMBASE:
3215 x86_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
3218 x86_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
3221 /* keep alignment */
3222 x86_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_LOCALLOC_ALIGNMENT - 1);
3223 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_LOCALLOC_ALIGNMENT - 1));
3224 code = mono_emit_stack_alloc (cfg, code, ins);
3225 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
3226 if (cfg->param_area)
3227 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
3229 case OP_LOCALLOC_IMM: {
3230 guint32 size = ins->inst_imm;
3231 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
3233 if (ins->flags & MONO_INST_INIT) {
3234 /* FIXME: Optimize this */
3235 x86_mov_reg_imm (code, ins->dreg, size);
3236 ins->sreg1 = ins->dreg;
3238 code = mono_emit_stack_alloc (cfg, code, ins);
3239 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
3241 x86_alu_reg_imm (code, X86_SUB, X86_ESP, size);
3242 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
3244 if (cfg->param_area)
3245 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
3249 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3250 x86_push_reg (code, ins->sreg1);
3251 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
3252 (gpointer)"mono_arch_throw_exception");
3253 ins->flags |= MONO_INST_GC_CALLSITE;
3254 ins->backend.pc_offset = code - cfg->native_code;
3258 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3259 x86_push_reg (code, ins->sreg1);
3260 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
3261 (gpointer)"mono_arch_rethrow_exception");
3262 ins->flags |= MONO_INST_GC_CALLSITE;
3263 ins->backend.pc_offset = code - cfg->native_code;
3266 case OP_CALL_HANDLER:
3267 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3268 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3269 x86_call_imm (code, 0);
3270 mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
3271 x86_alu_reg_imm (code, X86_ADD, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
3273 case OP_START_HANDLER: {
3274 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3275 x86_mov_membase_reg (code, spvar->inst_basereg, spvar->inst_offset, X86_ESP, 4);
3276 if (cfg->param_area)
3277 x86_alu_reg_imm (code, X86_SUB, X86_ESP, ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
3280 case OP_ENDFINALLY: {
3281 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3282 x86_mov_reg_membase (code, X86_ESP, spvar->inst_basereg, spvar->inst_offset, 4);
3286 case OP_ENDFILTER: {
3287 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3288 x86_mov_reg_membase (code, X86_ESP, spvar->inst_basereg, spvar->inst_offset, 4);
3289 /* The local allocator will put the result into EAX */
3294 if (ins->dreg != X86_EAX)
3295 x86_mov_reg_reg (code, ins->dreg, X86_EAX, sizeof (gpointer));
3299 ins->inst_c0 = code - cfg->native_code;
3302 if (ins->inst_target_bb->native_offset) {
3303 x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
3305 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3306 if ((cfg->opt & MONO_OPT_BRANCH) &&
3307 x86_is_imm8 (ins->inst_target_bb->max_offset - cpos))
3308 x86_jump8 (code, 0);
3310 x86_jump32 (code, 0);
3314 x86_jump_reg (code, ins->sreg1);
3333 x86_set_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
3334 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3336 case OP_COND_EXC_EQ:
3337 case OP_COND_EXC_NE_UN:
3338 case OP_COND_EXC_LT:
3339 case OP_COND_EXC_LT_UN:
3340 case OP_COND_EXC_GT:
3341 case OP_COND_EXC_GT_UN:
3342 case OP_COND_EXC_GE:
3343 case OP_COND_EXC_GE_UN:
3344 case OP_COND_EXC_LE:
3345 case OP_COND_EXC_LE_UN:
3346 case OP_COND_EXC_IEQ:
3347 case OP_COND_EXC_INE_UN:
3348 case OP_COND_EXC_ILT:
3349 case OP_COND_EXC_ILT_UN:
3350 case OP_COND_EXC_IGT:
3351 case OP_COND_EXC_IGT_UN:
3352 case OP_COND_EXC_IGE:
3353 case OP_COND_EXC_IGE_UN:
3354 case OP_COND_EXC_ILE:
3355 case OP_COND_EXC_ILE_UN:
3356 EMIT_COND_SYSTEM_EXCEPTION (cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)], ins->inst_p1);
3358 case OP_COND_EXC_OV:
3359 case OP_COND_EXC_NO:
3361 case OP_COND_EXC_NC:
3362 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ], (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
3364 case OP_COND_EXC_IOV:
3365 case OP_COND_EXC_INO:
3366 case OP_COND_EXC_IC:
3367 case OP_COND_EXC_INC:
3368 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_IEQ], (ins->opcode < OP_COND_EXC_INE_UN), ins->inst_p1);
3380 EMIT_COND_BRANCH (ins, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
3388 case OP_CMOV_INE_UN:
3389 case OP_CMOV_IGE_UN:
3390 case OP_CMOV_IGT_UN:
3391 case OP_CMOV_ILE_UN:
3392 case OP_CMOV_ILT_UN:
3393 g_assert (ins->dreg == ins->sreg1);
3394 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);
3397 /* floating point opcodes */
3399 double d = *(double *)ins->inst_p0;
3401 if ((d == 0.0) && (mono_signbit (d) == 0)) {
3403 } else if (d == 1.0) {
3406 if (cfg->compile_aot) {
3407 guint32 *val = (guint32*)&d;
3408 x86_push_imm (code, val [1]);
3409 x86_push_imm (code, val [0]);
3410 x86_fld_membase (code, X86_ESP, 0, TRUE);
3411 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3414 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R8, ins->inst_p0);
3415 x86_fld (code, NULL, TRUE);
3421 float f = *(float *)ins->inst_p0;
3423 if ((f == 0.0) && (mono_signbit (f) == 0)) {
3425 } else if (f == 1.0) {
3428 if (cfg->compile_aot) {
3429 guint32 val = *(guint32*)&f;
3430 x86_push_imm (code, val);
3431 x86_fld_membase (code, X86_ESP, 0, FALSE);
3432 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3435 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R4, ins->inst_p0);
3436 x86_fld (code, NULL, FALSE);
3441 case OP_STORER8_MEMBASE_REG:
3442 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, TRUE, TRUE);
3444 case OP_LOADR8_MEMBASE:
3445 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
3447 case OP_STORER4_MEMBASE_REG:
3448 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, FALSE, TRUE);
3450 case OP_LOADR4_MEMBASE:
3451 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
3453 case OP_ICONV_TO_R4:
3454 x86_push_reg (code, ins->sreg1);
3455 x86_fild_membase (code, X86_ESP, 0, FALSE);
3456 /* Change precision */
3457 x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
3458 x86_fld_membase (code, X86_ESP, 0, FALSE);
3459 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3461 case OP_ICONV_TO_R8:
3462 x86_push_reg (code, ins->sreg1);
3463 x86_fild_membase (code, X86_ESP, 0, FALSE);
3464 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3466 case OP_ICONV_TO_R_UN:
3467 x86_push_imm (code, 0);
3468 x86_push_reg (code, ins->sreg1);
3469 x86_fild_membase (code, X86_ESP, 0, TRUE);
3470 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3472 case OP_X86_FP_LOAD_I8:
3473 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
3475 case OP_X86_FP_LOAD_I4:
3476 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
3478 case OP_FCONV_TO_R4:
3479 /* Change precision */
3480 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
3481 x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
3482 x86_fld_membase (code, X86_ESP, 0, FALSE);
3483 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3485 case OP_FCONV_TO_I1:
3486 code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
3488 case OP_FCONV_TO_U1:
3489 code = emit_float_to_int (cfg, code, ins->dreg, 1, FALSE);
3491 case OP_FCONV_TO_I2:
3492 code = emit_float_to_int (cfg, code, ins->dreg, 2, TRUE);
3494 case OP_FCONV_TO_U2:
3495 code = emit_float_to_int (cfg, code, ins->dreg, 2, FALSE);
3497 case OP_FCONV_TO_I4:
3499 code = emit_float_to_int (cfg, code, ins->dreg, 4, TRUE);
3501 case OP_FCONV_TO_I8:
3502 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
3503 x86_fnstcw_membase(code, X86_ESP, 0);
3504 x86_mov_reg_membase (code, ins->dreg, X86_ESP, 0, 2);
3505 x86_alu_reg_imm (code, X86_OR, ins->dreg, 0xc00);
3506 x86_mov_membase_reg (code, X86_ESP, 2, ins->dreg, 2);
3507 x86_fldcw_membase (code, X86_ESP, 2);
3508 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
3509 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
3510 x86_pop_reg (code, ins->dreg);
3511 x86_pop_reg (code, ins->backend.reg3);
3512 x86_fldcw_membase (code, X86_ESP, 0);
3513 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3515 case OP_LCONV_TO_R8_2:
3516 x86_push_reg (code, ins->sreg2);
3517 x86_push_reg (code, ins->sreg1);
3518 x86_fild_membase (code, X86_ESP, 0, TRUE);
3519 /* Change precision */
3520 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
3521 x86_fld_membase (code, X86_ESP, 0, TRUE);
3522 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3524 case OP_LCONV_TO_R4_2:
3525 x86_push_reg (code, ins->sreg2);
3526 x86_push_reg (code, ins->sreg1);
3527 x86_fild_membase (code, X86_ESP, 0, TRUE);
3528 /* Change precision */
3529 x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
3530 x86_fld_membase (code, X86_ESP, 0, FALSE);
3531 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3533 case OP_LCONV_TO_R_UN_2: {
3534 static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
3537 /* load 64bit integer to FP stack */
3538 x86_push_reg (code, ins->sreg2);
3539 x86_push_reg (code, ins->sreg1);
3540 x86_fild_membase (code, X86_ESP, 0, TRUE);
3542 /* test if lreg is negative */
3543 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
3544 br = code; x86_branch8 (code, X86_CC_GEZ, 0, TRUE);
3546 /* add correction constant mn */
3547 if (cfg->compile_aot) {
3548 x86_push_imm (code, (((guint32)mn [9]) << 24) | ((guint32)mn [8] << 16) | ((guint32)mn [7] << 8) | ((guint32)mn [6]));
3549 x86_push_imm (code, (((guint32)mn [5]) << 24) | ((guint32)mn [4] << 16) | ((guint32)mn [3] << 8) | ((guint32)mn [2]));
3550 x86_push_imm (code, (((guint32)mn [1]) << 24) | ((guint32)mn [0] << 16));
3551 x86_fld80_membase (code, X86_ESP, 2);
3552 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
3554 x86_fld80_mem (code, mn);
3556 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3558 x86_patch (br, code);
3560 /* Change precision */
3561 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
3562 x86_fld_membase (code, X86_ESP, 0, TRUE);
3564 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3568 case OP_LCONV_TO_OVF_I:
3569 case OP_LCONV_TO_OVF_I4_2: {
3570 guint8 *br [3], *label [1];
3574 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
3576 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
3578 /* If the low word top bit is set, see if we are negative */
3579 br [0] = code; x86_branch8 (code, X86_CC_LT, 0, TRUE);
3580 /* We are not negative (no top bit set, check for our top word to be zero */
3581 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
3582 br [1] = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
3585 /* throw exception */
3586 tins = mono_branch_optimize_exception_target (cfg, bb, "OverflowException");
3588 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, tins->inst_true_bb);
3589 if ((cfg->opt & MONO_OPT_BRANCH) && x86_is_imm8 (tins->inst_true_bb->max_offset - cpos))
3590 x86_jump8 (code, 0);
3592 x86_jump32 (code, 0);
3594 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
3595 x86_jump32 (code, 0);
3599 x86_patch (br [0], code);
3600 /* our top bit is set, check that top word is 0xfffffff */
3601 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0xffffffff);
3603 x86_patch (br [1], code);
3604 /* nope, emit exception */
3605 br [2] = code; x86_branch8 (code, X86_CC_NE, 0, TRUE);
3606 x86_patch (br [2], label [0]);
3608 if (ins->dreg != ins->sreg1)
3609 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
3613 /* Not needed on the fp stack */
3615 case OP_MOVE_F_TO_I4:
3616 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE, TRUE);
3617 x86_mov_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, 4);
3619 case OP_MOVE_I4_TO_F:
3620 x86_mov_membase_reg (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, ins->sreg1, 4);
3621 x86_fld_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE);
3624 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3627 x86_fp_op_reg (code, X86_FSUB, 1, TRUE);
3630 x86_fp_op_reg (code, X86_FMUL, 1, TRUE);
3633 x86_fp_op_reg (code, X86_FDIV, 1, TRUE);
3641 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3646 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3653 * it really doesn't make sense to inline all this code,
3654 * it's here just to show that things may not be as simple
3657 guchar *check_pos, *end_tan, *pop_jump;
3658 x86_push_reg (code, X86_EAX);
3661 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
3663 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3664 x86_fstp (code, 0); /* pop the 1.0 */
3666 x86_jump8 (code, 0);
3668 x86_fp_op (code, X86_FADD, 0);
3672 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
3674 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3677 x86_patch (pop_jump, code);
3678 x86_fstp (code, 0); /* pop the 1.0 */
3679 x86_patch (check_pos, code);
3680 x86_patch (end_tan, code);
3682 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3683 x86_pop_reg (code, X86_EAX);
3690 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3699 g_assert (cfg->opt & MONO_OPT_CMOV);
3700 g_assert (ins->dreg == ins->sreg1);
3701 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3702 x86_cmov_reg (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2);
3705 g_assert (cfg->opt & MONO_OPT_CMOV);
3706 g_assert (ins->dreg == ins->sreg1);
3707 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3708 x86_cmov_reg (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2);
3711 g_assert (cfg->opt & MONO_OPT_CMOV);
3712 g_assert (ins->dreg == ins->sreg1);
3713 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3714 x86_cmov_reg (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2);
3717 g_assert (cfg->opt & MONO_OPT_CMOV);
3718 g_assert (ins->dreg == ins->sreg1);
3719 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3720 x86_cmov_reg (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2);
3726 x86_fxch (code, ins->inst_imm);
3731 x86_push_reg (code, X86_EAX);
3732 /* we need to exchange ST(0) with ST(1) */
3735 /* this requires a loop, because fprem somtimes
3736 * returns a partial remainder */
3738 /* looks like MS is using fprem instead of the IEEE compatible fprem1 */
3739 /* x86_fprem1 (code); */
3742 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_C2);
3744 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3750 x86_pop_reg (code, X86_EAX);
3754 if (cfg->opt & MONO_OPT_FCMOV) {
3755 x86_fcomip (code, 1);
3759 /* this overwrites EAX */
3760 EMIT_FPCOMPARE(code);
3761 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3765 if (cfg->opt & MONO_OPT_FCMOV) {
3766 /* zeroing the register at the start results in
3767 * shorter and faster code (we can also remove the widening op)
3769 guchar *unordered_check;
3770 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3771 x86_fcomip (code, 1);
3773 unordered_check = code;
3774 x86_branch8 (code, X86_CC_P, 0, FALSE);
3775 if (ins->opcode == OP_FCEQ) {
3776 x86_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
3777 x86_patch (unordered_check, code);
3779 guchar *jump_to_end;
3780 x86_set_reg (code, X86_CC_NE, ins->dreg, FALSE);
3782 x86_jump8 (code, 0);
3783 x86_patch (unordered_check, code);
3784 x86_inc_reg (code, ins->dreg);
3785 x86_patch (jump_to_end, code);
3790 if (ins->dreg != X86_EAX)
3791 x86_push_reg (code, X86_EAX);
3793 EMIT_FPCOMPARE(code);
3794 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3795 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
3796 x86_set_reg (code, ins->opcode == OP_FCEQ ? X86_CC_EQ : X86_CC_NE, ins->dreg, TRUE);
3797 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3799 if (ins->dreg != X86_EAX)
3800 x86_pop_reg (code, X86_EAX);
3804 if (cfg->opt & MONO_OPT_FCMOV) {
3805 /* zeroing the register at the start results in
3806 * shorter and faster code (we can also remove the widening op)
3808 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3809 x86_fcomip (code, 1);
3811 if (ins->opcode == OP_FCLT_UN) {
3812 guchar *unordered_check = code;
3813 guchar *jump_to_end;
3814 x86_branch8 (code, X86_CC_P, 0, FALSE);
3815 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
3817 x86_jump8 (code, 0);
3818 x86_patch (unordered_check, code);
3819 x86_inc_reg (code, ins->dreg);
3820 x86_patch (jump_to_end, code);
3822 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
3826 if (ins->dreg != X86_EAX)
3827 x86_push_reg (code, X86_EAX);
3829 EMIT_FPCOMPARE(code);
3830 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3831 if (ins->opcode == OP_FCLT_UN) {
3832 guchar *is_not_zero_check, *end_jump;
3833 is_not_zero_check = code;
3834 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3836 x86_jump8 (code, 0);
3837 x86_patch (is_not_zero_check, code);
3838 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3840 x86_patch (end_jump, code);
3842 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3843 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3845 if (ins->dreg != X86_EAX)
3846 x86_pop_reg (code, X86_EAX);
3849 guchar *unordered_check;
3850 guchar *jump_to_end;
3851 if (cfg->opt & MONO_OPT_FCMOV) {
3852 /* zeroing the register at the start results in
3853 * shorter and faster code (we can also remove the widening op)
3855 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3856 x86_fcomip (code, 1);
3858 unordered_check = code;
3859 x86_branch8 (code, X86_CC_P, 0, FALSE);
3860 x86_set_reg (code, X86_CC_NB, ins->dreg, FALSE);
3861 x86_patch (unordered_check, code);
3864 if (ins->dreg != X86_EAX)
3865 x86_push_reg (code, X86_EAX);
3867 EMIT_FPCOMPARE(code);
3868 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3869 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
3870 unordered_check = code;
3871 x86_branch8 (code, X86_CC_EQ, 0, FALSE);
3873 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3874 x86_set_reg (code, X86_CC_NE, ins->dreg, TRUE);
3875 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3877 x86_jump8 (code, 0);
3878 x86_patch (unordered_check, code);
3879 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3880 x86_patch (jump_to_end, code);
3882 if (ins->dreg != X86_EAX)
3883 x86_pop_reg (code, X86_EAX);
3888 if (cfg->opt & MONO_OPT_FCMOV) {
3889 /* zeroing the register at the start results in
3890 * shorter and faster code (we can also remove the widening op)
3892 guchar *unordered_check;
3893 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3894 x86_fcomip (code, 1);
3896 if (ins->opcode == OP_FCGT) {
3897 unordered_check = code;
3898 x86_branch8 (code, X86_CC_P, 0, FALSE);
3899 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
3900 x86_patch (unordered_check, code);
3902 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
3906 if (ins->dreg != X86_EAX)
3907 x86_push_reg (code, X86_EAX);
3909 EMIT_FPCOMPARE(code);
3910 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3911 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3912 if (ins->opcode == OP_FCGT_UN) {
3913 guchar *is_not_zero_check, *end_jump;
3914 is_not_zero_check = code;
3915 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3917 x86_jump8 (code, 0);
3918 x86_patch (is_not_zero_check, code);
3919 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3921 x86_patch (end_jump, code);
3923 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3924 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3926 if (ins->dreg != X86_EAX)
3927 x86_pop_reg (code, X86_EAX);
3930 guchar *unordered_check;
3931 guchar *jump_to_end;
3932 if (cfg->opt & MONO_OPT_FCMOV) {
3933 /* zeroing the register at the start results in
3934 * shorter and faster code (we can also remove the widening op)
3936 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3937 x86_fcomip (code, 1);
3939 unordered_check = code;
3940 x86_branch8 (code, X86_CC_P, 0, FALSE);
3941 x86_set_reg (code, X86_CC_NA, ins->dreg, FALSE);
3942 x86_patch (unordered_check, code);
3945 if (ins->dreg != X86_EAX)
3946 x86_push_reg (code, X86_EAX);
3948 EMIT_FPCOMPARE(code);
3949 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3950 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
3951 unordered_check = code;
3952 x86_branch8 (code, X86_CC_EQ, 0, FALSE);
3954 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3955 x86_set_reg (code, X86_CC_GE, ins->dreg, TRUE);
3956 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3958 x86_jump8 (code, 0);
3959 x86_patch (unordered_check, code);
3960 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3961 x86_patch (jump_to_end, code);
3963 if (ins->dreg != X86_EAX)
3964 x86_pop_reg (code, X86_EAX);
3968 if (cfg->opt & MONO_OPT_FCMOV) {
3969 guchar *jump = code;
3970 x86_branch8 (code, X86_CC_P, 0, TRUE);
3971 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3972 x86_patch (jump, code);
3975 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
3976 EMIT_COND_BRANCH (ins, X86_CC_EQ, TRUE);
3979 /* Branch if C013 != 100 */
3980 if (cfg->opt & MONO_OPT_FCMOV) {
3981 /* branch if !ZF or (PF|CF) */
3982 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3983 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3984 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
3987 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
3988 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3991 if (cfg->opt & MONO_OPT_FCMOV) {
3992 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
3995 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3998 if (cfg->opt & MONO_OPT_FCMOV) {
3999 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
4000 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
4003 if (ins->opcode == OP_FBLT_UN) {
4004 guchar *is_not_zero_check, *end_jump;
4005 is_not_zero_check = code;
4006 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
4008 x86_jump8 (code, 0);
4009 x86_patch (is_not_zero_check, code);
4010 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
4012 x86_patch (end_jump, code);
4014 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4018 if (cfg->opt & MONO_OPT_FCMOV) {
4019 if (ins->opcode == OP_FBGT) {
4022 /* skip branch if C1=1 */
4024 x86_branch8 (code, X86_CC_P, 0, FALSE);
4025 /* branch if (C0 | C3) = 1 */
4026 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
4027 x86_patch (br1, code);
4029 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
4033 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4034 if (ins->opcode == OP_FBGT_UN) {
4035 guchar *is_not_zero_check, *end_jump;
4036 is_not_zero_check = code;
4037 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
4039 x86_jump8 (code, 0);
4040 x86_patch (is_not_zero_check, code);
4041 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
4043 x86_patch (end_jump, code);
4045 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4048 /* Branch if C013 == 100 or 001 */
4049 if (cfg->opt & MONO_OPT_FCMOV) {
4052 /* skip branch if C1=1 */
4054 x86_branch8 (code, X86_CC_P, 0, FALSE);
4055 /* branch if (C0 | C3) = 1 */
4056 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
4057 x86_patch (br1, code);
4060 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4061 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4062 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
4063 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4066 /* Branch if C013 == 000 */
4067 if (cfg->opt & MONO_OPT_FCMOV) {
4068 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
4071 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
4074 /* Branch if C013=000 or 100 */
4075 if (cfg->opt & MONO_OPT_FCMOV) {
4078 /* skip branch if C1=1 */
4080 x86_branch8 (code, X86_CC_P, 0, FALSE);
4081 /* branch if C0=0 */
4082 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
4083 x86_patch (br1, code);
4086 x86_alu_reg_imm (code, X86_AND, X86_EAX, (X86_FP_C0|X86_FP_C1));
4087 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0);
4088 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4091 /* Branch if C013 != 001 */
4092 if (cfg->opt & MONO_OPT_FCMOV) {
4093 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
4094 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
4097 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4098 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
4102 x86_push_reg (code, X86_EAX);
4105 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
4106 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
4107 x86_pop_reg (code, X86_EAX);
4109 /* Have to clean up the fp stack before throwing the exception */
4111 x86_branch8 (code, X86_CC_NE, 0, FALSE);
4114 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "OverflowException");
4116 x86_patch (br1, code);
4120 code = mono_x86_emit_tls_get (code, ins->dreg, ins->inst_offset);
4124 code = mono_x86_emit_tls_set (code, ins->sreg1, ins->inst_offset);
4127 case OP_MEMORY_BARRIER: {
4128 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ) {
4129 x86_prefix (code, X86_LOCK_PREFIX);
4130 x86_alu_membase_imm (code, X86_ADD, X86_ESP, 0, 0);
4134 case OP_ATOMIC_ADD_I4: {
4135 int dreg = ins->dreg;
4137 g_assert (cfg->has_atomic_add_i4);
4139 /* hack: limit in regalloc, dreg != sreg1 && dreg != sreg2 */
4140 if (ins->sreg2 == dreg) {
4141 if (dreg == X86_EBX) {
4143 if (ins->inst_basereg == X86_EDI)
4147 if (ins->inst_basereg == X86_EBX)
4150 } else if (ins->inst_basereg == dreg) {
4151 if (dreg == X86_EBX) {
4153 if (ins->sreg2 == X86_EDI)
4157 if (ins->sreg2 == X86_EBX)
4162 if (dreg != ins->dreg) {
4163 x86_push_reg (code, dreg);
4166 x86_mov_reg_reg (code, dreg, ins->sreg2, 4);
4167 x86_prefix (code, X86_LOCK_PREFIX);
4168 x86_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, 4);
4169 /* dreg contains the old value, add with sreg2 value */
4170 x86_alu_reg_reg (code, X86_ADD, dreg, ins->sreg2);
4172 if (ins->dreg != dreg) {
4173 x86_mov_reg_reg (code, ins->dreg, dreg, 4);
4174 x86_pop_reg (code, dreg);
4179 case OP_ATOMIC_EXCHANGE_I4: {
4181 int sreg2 = ins->sreg2;
4182 int breg = ins->inst_basereg;
4184 g_assert (cfg->has_atomic_exchange_i4);
4186 /* cmpxchg uses eax as comperand, need to make sure we can use it
4187 * hack to overcome limits in x86 reg allocator
4188 * (req: dreg == eax and sreg2 != eax and breg != eax)
4190 g_assert (ins->dreg == X86_EAX);
4192 /* We need the EAX reg for the cmpxchg */
4193 if (ins->sreg2 == X86_EAX) {
4194 sreg2 = (breg == X86_EDX) ? X86_EBX : X86_EDX;
4195 x86_push_reg (code, sreg2);
4196 x86_mov_reg_reg (code, sreg2, X86_EAX, 4);
4199 if (breg == X86_EAX) {
4200 breg = (sreg2 == X86_ESI) ? X86_EDI : X86_ESI;
4201 x86_push_reg (code, breg);
4202 x86_mov_reg_reg (code, breg, X86_EAX, 4);
4205 x86_mov_reg_membase (code, X86_EAX, breg, ins->inst_offset, 4);
4207 br [0] = code; x86_prefix (code, X86_LOCK_PREFIX);
4208 x86_cmpxchg_membase_reg (code, breg, ins->inst_offset, sreg2);
4209 br [1] = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
4210 x86_patch (br [1], br [0]);
4212 if (breg != ins->inst_basereg)
4213 x86_pop_reg (code, breg);
4215 if (ins->sreg2 != sreg2)
4216 x86_pop_reg (code, sreg2);
4220 case OP_ATOMIC_CAS_I4: {
4221 g_assert (ins->dreg == X86_EAX);
4222 g_assert (ins->sreg3 == X86_EAX);
4223 g_assert (ins->sreg1 != X86_EAX);
4224 g_assert (ins->sreg1 != ins->sreg2);
4226 x86_prefix (code, X86_LOCK_PREFIX);
4227 x86_cmpxchg_membase_reg (code, ins->sreg1, ins->inst_offset, ins->sreg2);
4230 case OP_ATOMIC_LOAD_I1: {
4231 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
4234 case OP_ATOMIC_LOAD_U1: {
4235 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
4238 case OP_ATOMIC_LOAD_I2: {
4239 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
4242 case OP_ATOMIC_LOAD_U2: {
4243 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
4246 case OP_ATOMIC_LOAD_I4:
4247 case OP_ATOMIC_LOAD_U4: {
4248 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
4251 case OP_ATOMIC_LOAD_R4:
4252 case OP_ATOMIC_LOAD_R8: {
4253 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, ins->opcode == OP_ATOMIC_LOAD_R8);
4256 case OP_ATOMIC_STORE_I1:
4257 case OP_ATOMIC_STORE_U1:
4258 case OP_ATOMIC_STORE_I2:
4259 case OP_ATOMIC_STORE_U2:
4260 case OP_ATOMIC_STORE_I4:
4261 case OP_ATOMIC_STORE_U4: {
4264 switch (ins->opcode) {
4265 case OP_ATOMIC_STORE_I1:
4266 case OP_ATOMIC_STORE_U1:
4269 case OP_ATOMIC_STORE_I2:
4270 case OP_ATOMIC_STORE_U2:
4273 case OP_ATOMIC_STORE_I4:
4274 case OP_ATOMIC_STORE_U4:
4279 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, size);
4281 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
4285 case OP_ATOMIC_STORE_R4:
4286 case OP_ATOMIC_STORE_R8: {
4287 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, ins->opcode == OP_ATOMIC_STORE_R8, TRUE);
4289 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
4293 case OP_CARD_TABLE_WBARRIER: {
4294 int ptr = ins->sreg1;
4295 int value = ins->sreg2;
4297 int nursery_shift, card_table_shift;
4298 gpointer card_table_mask;
4299 size_t nursery_size;
4300 gulong card_table = (gulong)mono_gc_get_card_table (&card_table_shift, &card_table_mask);
4301 gulong nursery_start = (gulong)mono_gc_get_nursery (&nursery_shift, &nursery_size);
4302 gboolean card_table_nursery_check = mono_gc_card_table_nursery_check ();
4305 * We need one register we can clobber, we choose EDX and make sreg1
4306 * fixed EAX to work around limitations in the local register allocator.
4307 * sreg2 might get allocated to EDX, but that is not a problem since
4308 * we use it before clobbering EDX.
4310 g_assert (ins->sreg1 == X86_EAX);
4313 * This is the code we produce:
4316 * edx >>= nursery_shift
4317 * cmp edx, (nursery_start >> nursery_shift)
4320 * edx >>= card_table_shift
4321 * card_table[edx] = 1
4325 if (card_table_nursery_check) {
4326 if (value != X86_EDX)
4327 x86_mov_reg_reg (code, X86_EDX, value, 4);
4328 x86_shift_reg_imm (code, X86_SHR, X86_EDX, nursery_shift);
4329 x86_alu_reg_imm (code, X86_CMP, X86_EDX, nursery_start >> nursery_shift);
4330 br = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
4332 x86_mov_reg_reg (code, X86_EDX, ptr, 4);
4333 x86_shift_reg_imm (code, X86_SHR, X86_EDX, card_table_shift);
4334 if (card_table_mask)
4335 x86_alu_reg_imm (code, X86_AND, X86_EDX, (int)card_table_mask);
4336 x86_mov_membase_imm (code, X86_EDX, card_table, 1, 1);
4337 if (card_table_nursery_check)
4338 x86_patch (br, code);
4341 #ifdef MONO_ARCH_SIMD_INTRINSICS
4343 x86_sse_alu_ps_reg_reg (code, X86_SSE_ADD, ins->sreg1, ins->sreg2);
4346 x86_sse_alu_ps_reg_reg (code, X86_SSE_DIV, ins->sreg1, ins->sreg2);
4349 x86_sse_alu_ps_reg_reg (code, X86_SSE_MUL, ins->sreg1, ins->sreg2);
4352 x86_sse_alu_ps_reg_reg (code, X86_SSE_SUB, ins->sreg1, ins->sreg2);
4355 x86_sse_alu_ps_reg_reg (code, X86_SSE_MAX, ins->sreg1, ins->sreg2);
4358 x86_sse_alu_ps_reg_reg (code, X86_SSE_MIN, ins->sreg1, ins->sreg2);
4361 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
4362 x86_sse_alu_ps_reg_reg_imm (code, X86_SSE_COMP, ins->sreg1, ins->sreg2, ins->inst_c0);
4365 x86_sse_alu_ps_reg_reg (code, X86_SSE_AND, ins->sreg1, ins->sreg2);
4368 x86_sse_alu_ps_reg_reg (code, X86_SSE_ANDN, ins->sreg1, ins->sreg2);
4371 x86_sse_alu_ps_reg_reg (code, X86_SSE_OR, ins->sreg1, ins->sreg2);
4374 x86_sse_alu_ps_reg_reg (code, X86_SSE_XOR, ins->sreg1, ins->sreg2);
4377 x86_sse_alu_ps_reg_reg (code, X86_SSE_SQRT, ins->dreg, ins->sreg1);
4380 x86_sse_alu_ps_reg_reg (code, X86_SSE_RSQRT, ins->dreg, ins->sreg1);
4383 x86_sse_alu_ps_reg_reg (code, X86_SSE_RCP, ins->dreg, ins->sreg1);
4386 x86_sse_alu_sd_reg_reg (code, X86_SSE_ADDSUB, ins->sreg1, ins->sreg2);
4389 x86_sse_alu_sd_reg_reg (code, X86_SSE_HADD, ins->sreg1, ins->sreg2);
4392 x86_sse_alu_sd_reg_reg (code, X86_SSE_HSUB, ins->sreg1, ins->sreg2);
4395 x86_sse_alu_ss_reg_reg (code, X86_SSE_MOVSHDUP, ins->dreg, ins->sreg1);
4398 x86_sse_alu_ss_reg_reg (code, X86_SSE_MOVSLDUP, ins->dreg, ins->sreg1);
4401 case OP_PSHUFLEW_HIGH:
4402 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
4403 x86_pshufw_reg_reg (code, ins->dreg, ins->sreg1, ins->inst_c0, 1);
4405 case OP_PSHUFLEW_LOW:
4406 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
4407 x86_pshufw_reg_reg (code, ins->dreg, ins->sreg1, ins->inst_c0, 0);
4410 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
4411 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->sreg1, ins->inst_c0);
4414 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
4415 x86_sse_alu_reg_reg_imm8 (code, X86_SSE_SHUFP, ins->sreg1, ins->sreg2, ins->inst_c0);
4418 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0x3);
4419 x86_sse_alu_pd_reg_reg_imm8 (code, X86_SSE_SHUFP, ins->sreg1, ins->sreg2, ins->inst_c0);
4423 x86_sse_alu_pd_reg_reg (code, X86_SSE_ADD, ins->sreg1, ins->sreg2);
4426 x86_sse_alu_pd_reg_reg (code, X86_SSE_DIV, ins->sreg1, ins->sreg2);
4429 x86_sse_alu_pd_reg_reg (code, X86_SSE_MUL, ins->sreg1, ins->sreg2);
4432 x86_sse_alu_pd_reg_reg (code, X86_SSE_SUB, ins->sreg1, ins->sreg2);
4435 x86_sse_alu_pd_reg_reg (code, X86_SSE_MAX, ins->sreg1, ins->sreg2);
4438 x86_sse_alu_pd_reg_reg (code, X86_SSE_MIN, ins->sreg1, ins->sreg2);
4441 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
4442 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_COMP, ins->sreg1, ins->sreg2, ins->inst_c0);
4445 x86_sse_alu_pd_reg_reg (code, X86_SSE_AND, ins->sreg1, ins->sreg2);
4448 x86_sse_alu_pd_reg_reg (code, X86_SSE_ANDN, ins->sreg1, ins->sreg2);
4451 x86_sse_alu_pd_reg_reg (code, X86_SSE_OR, ins->sreg1, ins->sreg2);
4454 x86_sse_alu_pd_reg_reg (code, X86_SSE_XOR, ins->sreg1, ins->sreg2);
4457 x86_sse_alu_pd_reg_reg (code, X86_SSE_SQRT, ins->dreg, ins->sreg1);
4460 x86_sse_alu_pd_reg_reg (code, X86_SSE_ADDSUB, ins->sreg1, ins->sreg2);
4463 x86_sse_alu_pd_reg_reg (code, X86_SSE_HADD, ins->sreg1, ins->sreg2);
4466 x86_sse_alu_pd_reg_reg (code, X86_SSE_HSUB, ins->sreg1, ins->sreg2);
4469 x86_sse_alu_sd_reg_reg (code, X86_SSE_MOVDDUP, ins->dreg, ins->sreg1);
4472 case OP_EXTRACT_MASK:
4473 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMOVMSKB, ins->dreg, ins->sreg1);
4477 x86_sse_alu_pd_reg_reg (code, X86_SSE_PAND, ins->sreg1, ins->sreg2);
4480 x86_sse_alu_pd_reg_reg (code, X86_SSE_POR, ins->sreg1, ins->sreg2);
4483 x86_sse_alu_pd_reg_reg (code, X86_SSE_PXOR, ins->sreg1, ins->sreg2);
4487 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDB, ins->sreg1, ins->sreg2);
4490 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDW, ins->sreg1, ins->sreg2);
4493 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDD, ins->sreg1, ins->sreg2);
4496 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDQ, ins->sreg1, ins->sreg2);
4500 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBB, ins->sreg1, ins->sreg2);
4503 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBW, ins->sreg1, ins->sreg2);
4506 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBD, ins->sreg1, ins->sreg2);
4509 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBQ, ins->sreg1, ins->sreg2);
4513 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMAXUB, ins->sreg1, ins->sreg2);
4516 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXUW, ins->sreg1, ins->sreg2);
4519 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXUD, ins->sreg1, ins->sreg2);
4523 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXSB, ins->sreg1, ins->sreg2);
4526 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMAXSW, ins->sreg1, ins->sreg2);
4529 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMAXSD, ins->sreg1, ins->sreg2);
4533 x86_sse_alu_pd_reg_reg (code, X86_SSE_PAVGB, ins->sreg1, ins->sreg2);
4536 x86_sse_alu_pd_reg_reg (code, X86_SSE_PAVGW, ins->sreg1, ins->sreg2);
4540 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMINUB, ins->sreg1, ins->sreg2);
4543 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINUW, ins->sreg1, ins->sreg2);
4546 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINUD, ins->sreg1, ins->sreg2);
4550 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINSB, ins->sreg1, ins->sreg2);
4553 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMINSW, ins->sreg1, ins->sreg2);
4556 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMINSD, ins->sreg1, ins->sreg2);
4560 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQB, ins->sreg1, ins->sreg2);
4563 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQW, ins->sreg1, ins->sreg2);
4566 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQD, ins->sreg1, ins->sreg2);
4569 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PCMPEQQ, ins->sreg1, ins->sreg2);
4573 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPGTB, ins->sreg1, ins->sreg2);
4576 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPGTW, ins->sreg1, ins->sreg2);
4579 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPGTD, ins->sreg1, ins->sreg2);
4582 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PCMPGTQ, ins->sreg1, ins->sreg2);
4585 case OP_PSUM_ABS_DIFF:
4586 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSADBW, ins->sreg1, ins->sreg2);
4589 case OP_UNPACK_LOWB:
4590 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLBW, ins->sreg1, ins->sreg2);
4592 case OP_UNPACK_LOWW:
4593 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLWD, ins->sreg1, ins->sreg2);
4595 case OP_UNPACK_LOWD:
4596 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLDQ, ins->sreg1, ins->sreg2);
4598 case OP_UNPACK_LOWQ:
4599 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLQDQ, ins->sreg1, ins->sreg2);
4601 case OP_UNPACK_LOWPS:
4602 x86_sse_alu_ps_reg_reg (code, X86_SSE_UNPCKL, ins->sreg1, ins->sreg2);
4604 case OP_UNPACK_LOWPD:
4605 x86_sse_alu_pd_reg_reg (code, X86_SSE_UNPCKL, ins->sreg1, ins->sreg2);
4608 case OP_UNPACK_HIGHB:
4609 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHBW, ins->sreg1, ins->sreg2);
4611 case OP_UNPACK_HIGHW:
4612 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHWD, ins->sreg1, ins->sreg2);
4614 case OP_UNPACK_HIGHD:
4615 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHDQ, ins->sreg1, ins->sreg2);
4617 case OP_UNPACK_HIGHQ:
4618 x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHQDQ, ins->sreg1, ins->sreg2);
4620 case OP_UNPACK_HIGHPS:
4621 x86_sse_alu_ps_reg_reg (code, X86_SSE_UNPCKH, ins->sreg1, ins->sreg2);
4623 case OP_UNPACK_HIGHPD:
4624 x86_sse_alu_pd_reg_reg (code, X86_SSE_UNPCKH, ins->sreg1, ins->sreg2);
4628 x86_sse_alu_pd_reg_reg (code, X86_SSE_PACKSSWB, ins->sreg1, ins->sreg2);
4631 x86_sse_alu_pd_reg_reg (code, X86_SSE_PACKSSDW, ins->sreg1, ins->sreg2);
4634 x86_sse_alu_pd_reg_reg (code, X86_SSE_PACKUSWB, ins->sreg1, ins->sreg2);
4637 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PACKUSDW, ins->sreg1, ins->sreg2);
4640 case OP_PADDB_SAT_UN:
4641 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDUSB, ins->sreg1, ins->sreg2);
4643 case OP_PSUBB_SAT_UN:
4644 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBUSB, ins->sreg1, ins->sreg2);
4646 case OP_PADDW_SAT_UN:
4647 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDUSW, ins->sreg1, ins->sreg2);
4649 case OP_PSUBW_SAT_UN:
4650 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBUSW, ins->sreg1, ins->sreg2);
4654 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDSB, ins->sreg1, ins->sreg2);
4657 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBSB, ins->sreg1, ins->sreg2);
4660 x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDSW, ins->sreg1, ins->sreg2);
4663 x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBSW, ins->sreg1, ins->sreg2);
4667 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULLW, ins->sreg1, ins->sreg2);
4670 x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMULLD, ins->sreg1, ins->sreg2);
4673 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULUDQ, ins->sreg1, ins->sreg2);
4675 case OP_PMULW_HIGH_UN:
4676 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULHUW, ins->sreg1, ins->sreg2);
4679 x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULHW, ins->sreg1, ins->sreg2);
4683 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTW, X86_SSE_SHR, ins->dreg, ins->inst_imm);
4686 x86_sse_shift_reg_reg (code, X86_SSE_PSRLW_REG, ins->dreg, ins->sreg2);
4690 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTW, X86_SSE_SAR, ins->dreg, ins->inst_imm);
4693 x86_sse_shift_reg_reg (code, X86_SSE_PSRAW_REG, ins->dreg, ins->sreg2);
4697 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTW, X86_SSE_SHL, ins->dreg, ins->inst_imm);
4700 x86_sse_shift_reg_reg (code, X86_SSE_PSLLW_REG, ins->dreg, ins->sreg2);
4704 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTD, X86_SSE_SHR, ins->dreg, ins->inst_imm);
4707 x86_sse_shift_reg_reg (code, X86_SSE_PSRLD_REG, ins->dreg, ins->sreg2);
4711 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTD, X86_SSE_SAR, ins->dreg, ins->inst_imm);
4714 x86_sse_shift_reg_reg (code, X86_SSE_PSRAD_REG, ins->dreg, ins->sreg2);
4718 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTD, X86_SSE_SHL, ins->dreg, ins->inst_imm);
4721 x86_sse_shift_reg_reg (code, X86_SSE_PSLLD_REG, ins->dreg, ins->sreg2);
4725 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTQ, X86_SSE_SHR, ins->dreg, ins->inst_imm);
4728 x86_sse_shift_reg_reg (code, X86_SSE_PSRLQ_REG, ins->dreg, ins->sreg2);
4732 x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTQ, X86_SSE_SHL, ins->dreg, ins->inst_imm);
4735 x86_sse_shift_reg_reg (code, X86_SSE_PSLLQ_REG, ins->dreg, ins->sreg2);
4739 x86_movd_xreg_reg (code, ins->dreg, ins->sreg1);
4742 x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
4746 x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
4748 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_c0 * 8);
4749 x86_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I1, FALSE);
4753 x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
4755 x86_shift_reg_imm (code, X86_SHR, ins->dreg, 16);
4756 x86_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I2, TRUE);
4760 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);
4762 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);
4763 x86_fld_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE);
4767 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->sreg1, ins->sreg2, ins->inst_c0);
4769 case OP_EXTRACTX_U2:
4770 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PEXTRW, ins->dreg, ins->sreg1, ins->inst_c0);
4772 case OP_INSERTX_U1_SLOW:
4773 /*sreg1 is the extracted ireg (scratch)
4774 /sreg2 is the to be inserted ireg (scratch)
4775 /dreg is the xreg to receive the value*/
4777 /*clear the bits from the extracted word*/
4778 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_c0 & 1 ? 0x00FF : 0xFF00);
4779 /*shift the value to insert if needed*/
4780 if (ins->inst_c0 & 1)
4781 x86_shift_reg_imm (code, X86_SHL, ins->sreg2, 8);
4782 /*join them together*/
4783 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
4784 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, ins->inst_c0 / 2);
4786 case OP_INSERTX_I4_SLOW:
4787 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg2, ins->inst_c0 * 2);
4788 x86_shift_reg_imm (code, X86_SHR, ins->sreg2, 16);
4789 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg2, ins->inst_c0 * 2 + 1);
4792 case OP_INSERTX_R4_SLOW:
4793 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE, TRUE);
4794 /*TODO if inst_c0 == 0 use movss*/
4795 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);
4796 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);
4798 case OP_INSERTX_R8_SLOW:
4799 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
4800 if (cfg->verbose_level)
4801 printf ("CONVERTING a OP_INSERTX_R8_SLOW %d offset %x\n", ins->inst_c0, offset);
4803 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);
4805 x86_movsd_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
4808 case OP_STOREX_MEMBASE_REG:
4809 case OP_STOREX_MEMBASE:
4810 x86_movups_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
4812 case OP_LOADX_MEMBASE:
4813 x86_movups_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
4815 case OP_LOADX_ALIGNED_MEMBASE:
4816 x86_movaps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
4818 case OP_STOREX_ALIGNED_MEMBASE_REG:
4819 x86_movaps_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
4821 case OP_STOREX_NTA_MEMBASE_REG:
4822 x86_sse_alu_reg_membase (code, X86_SSE_MOVNTPS, ins->dreg, ins->sreg1, ins->inst_offset);
4824 case OP_PREFETCH_MEMBASE:
4825 x86_sse_alu_reg_membase (code, X86_SSE_PREFETCH, ins->backend.arg_info, ins->sreg1, ins->inst_offset);
4829 /*FIXME the peephole pass should have killed this*/
4830 if (ins->dreg != ins->sreg1)
4831 x86_movaps_reg_reg (code, ins->dreg, ins->sreg1);
4834 x86_sse_alu_pd_reg_reg (code, X86_SSE_PXOR, ins->dreg, ins->dreg);
4837 x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQB, ins->dreg, ins->dreg);
4840 case OP_FCONV_TO_R8_X:
4841 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
4842 x86_movsd_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
4845 case OP_XCONV_R8_TO_I4:
4846 x86_cvttsd2si (code, ins->dreg, ins->sreg1);
4847 switch (ins->backend.source_opcode) {
4848 case OP_FCONV_TO_I1:
4849 x86_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
4851 case OP_FCONV_TO_U1:
4852 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
4854 case OP_FCONV_TO_I2:
4855 x86_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
4857 case OP_FCONV_TO_U2:
4858 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
4864 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 0);
4865 x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 1);
4866 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
4869 x86_movd_xreg_reg (code, ins->dreg, ins->sreg1);
4870 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
4873 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE, TRUE);
4874 x86_movd_xreg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
4875 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
4878 x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
4879 x86_movsd_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
4880 x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0x44);
4884 x86_sse_alu_ss_reg_reg (code, X86_SSE_CVTDQ2PD, ins->dreg, ins->sreg1);
4887 x86_sse_alu_ps_reg_reg (code, X86_SSE_CVTDQ2PS, ins->dreg, ins->sreg1);
4890 x86_sse_alu_sd_reg_reg (code, X86_SSE_CVTPD2DQ, ins->dreg, ins->sreg1);
4893 x86_sse_alu_pd_reg_reg (code, X86_SSE_CVTPD2PS, ins->dreg, ins->sreg1);
4896 x86_sse_alu_pd_reg_reg (code, X86_SSE_CVTPS2DQ, ins->dreg, ins->sreg1);
4899 x86_sse_alu_ps_reg_reg (code, X86_SSE_CVTPS2PD, ins->dreg, ins->sreg1);
4902 x86_sse_alu_pd_reg_reg (code, X86_SSE_CVTTPD2DQ, ins->dreg, ins->sreg1);
4905 x86_sse_alu_ss_reg_reg (code, X86_SSE_CVTTPS2DQ, ins->dreg, ins->sreg1);
4909 case OP_LIVERANGE_START: {
4910 if (cfg->verbose_level > 1)
4911 printf ("R%d START=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
4912 MONO_VARINFO (cfg, ins->inst_c0)->live_range_start = code - cfg->native_code;
4915 case OP_LIVERANGE_END: {
4916 if (cfg->verbose_level > 1)
4917 printf ("R%d END=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
4918 MONO_VARINFO (cfg, ins->inst_c0)->live_range_end = code - cfg->native_code;
4921 case OP_GC_SAFE_POINT: {
4924 g_assert (mono_threads_is_coop_enabled ());
4926 x86_test_membase_imm (code, ins->sreg1, 0, 1);
4927 br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
4928 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_threads_state_poll");
4929 x86_patch (br [0], code);
4933 case OP_GC_LIVENESS_DEF:
4934 case OP_GC_LIVENESS_USE:
4935 case OP_GC_PARAM_SLOT_LIVENESS_DEF:
4936 ins->backend.pc_offset = code - cfg->native_code;
4938 case OP_GC_SPILL_SLOT_LIVENESS_DEF:
4939 ins->backend.pc_offset = code - cfg->native_code;
4940 bb->spill_slot_defs = g_slist_prepend_mempool (cfg->mempool, bb->spill_slot_defs, ins);
4943 x86_mov_reg_reg (code, ins->dreg, X86_ESP, sizeof (mgreg_t));
4946 x86_mov_reg_reg (code, X86_ESP, ins->sreg1, sizeof (mgreg_t));
4949 g_warning ("unknown opcode %s\n", mono_inst_name (ins->opcode));
4950 g_assert_not_reached ();
4953 if (G_UNLIKELY ((code - cfg->native_code - offset) > max_len)) {
4954 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
4955 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
4956 g_assert_not_reached ();
4962 cfg->code_len = code - cfg->native_code;
4965 #endif /* DISABLE_JIT */
4968 mono_arch_register_lowlevel_calls (void)
4973 mono_arch_patch_code_new (MonoCompile *cfg, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gpointer target)
4975 unsigned char *ip = ji->ip.i + code;
4978 case MONO_PATCH_INFO_IP:
4979 *((gconstpointer *)(ip)) = target;
4981 case MONO_PATCH_INFO_ABS:
4982 case MONO_PATCH_INFO_METHOD:
4983 case MONO_PATCH_INFO_METHOD_JUMP:
4984 case MONO_PATCH_INFO_INTERNAL_METHOD:
4985 case MONO_PATCH_INFO_BB:
4986 case MONO_PATCH_INFO_LABEL:
4987 case MONO_PATCH_INFO_RGCTX_FETCH:
4988 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
4989 x86_patch (ip, (unsigned char*)target);
4991 case MONO_PATCH_INFO_NONE:
4993 case MONO_PATCH_INFO_R4:
4994 case MONO_PATCH_INFO_R8: {
4995 guint32 offset = mono_arch_get_patch_offset (ip);
4996 *((gconstpointer *)(ip + offset)) = target;
5000 guint32 offset = mono_arch_get_patch_offset (ip);
5001 *((gconstpointer *)(ip + offset)) = target;
5007 static G_GNUC_UNUSED void
5008 stack_unaligned (MonoMethod *m, gpointer caller)
5010 printf ("%s\n", mono_method_full_name (m, TRUE));
5011 g_assert_not_reached ();
5015 mono_arch_emit_prolog (MonoCompile *cfg)
5017 MonoMethod *method = cfg->method;
5019 MonoMethodSignature *sig;
5023 int alloc_size, pos, max_offset, i, cfa_offset;
5025 gboolean need_stack_frame;
5027 cfg->code_size = MAX (cfg->header->code_size * 4, 10240);
5029 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
5030 cfg->code_size += 512;
5032 code = cfg->native_code = g_malloc (cfg->code_size);
5038 /* Check that the stack is aligned on osx */
5039 x86_mov_reg_reg (code, X86_EAX, X86_ESP, sizeof (mgreg_t));
5040 x86_alu_reg_imm (code, X86_AND, X86_EAX, 15);
5041 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0xc);
5043 x86_branch_disp (code, X86_CC_Z, 0, FALSE);
5044 x86_push_membase (code, X86_ESP, 0);
5045 x86_push_imm (code, cfg->method);
5046 x86_mov_reg_imm (code, X86_EAX, stack_unaligned);
5047 x86_call_reg (code, X86_EAX);
5048 x86_patch (br [0], code);
5052 /* Offset between RSP and the CFA */
5056 cfa_offset = sizeof (gpointer);
5057 mono_emit_unwind_op_def_cfa (cfg, code, X86_ESP, sizeof (gpointer));
5058 // IP saved at CFA - 4
5059 /* There is no IP reg on x86 */
5060 mono_emit_unwind_op_offset (cfg, code, X86_NREG, -cfa_offset);
5061 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
5063 need_stack_frame = needs_stack_frame (cfg);
5065 if (need_stack_frame) {
5066 x86_push_reg (code, X86_EBP);
5067 cfa_offset += sizeof (gpointer);
5068 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
5069 mono_emit_unwind_op_offset (cfg, code, X86_EBP, - cfa_offset);
5070 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
5071 mono_emit_unwind_op_def_cfa_reg (cfg, code, X86_EBP);
5072 /* These are handled automatically by the stack marking code */
5073 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
5075 cfg->frame_reg = X86_ESP;
5078 cfg->stack_offset += cfg->param_area;
5079 cfg->stack_offset = ALIGN_TO (cfg->stack_offset, MONO_ARCH_FRAME_ALIGNMENT);
5081 alloc_size = cfg->stack_offset;
5084 if (!method->save_lmf) {
5085 if (cfg->used_int_regs & (1 << X86_EBX)) {
5086 x86_push_reg (code, X86_EBX);
5088 cfa_offset += sizeof (gpointer);
5089 mono_emit_unwind_op_offset (cfg, code, X86_EBX, - cfa_offset);
5090 /* These are handled automatically by the stack marking code */
5091 mini_gc_set_slot_type_from_cfa (cfg, - cfa_offset, SLOT_NOREF);
5094 if (cfg->used_int_regs & (1 << X86_EDI)) {
5095 x86_push_reg (code, X86_EDI);
5097 cfa_offset += sizeof (gpointer);
5098 mono_emit_unwind_op_offset (cfg, code, X86_EDI, - cfa_offset);
5099 mini_gc_set_slot_type_from_cfa (cfg, - cfa_offset, SLOT_NOREF);
5102 if (cfg->used_int_regs & (1 << X86_ESI)) {
5103 x86_push_reg (code, X86_ESI);
5105 cfa_offset += sizeof (gpointer);
5106 mono_emit_unwind_op_offset (cfg, code, X86_ESI, - cfa_offset);
5107 mini_gc_set_slot_type_from_cfa (cfg, - cfa_offset, SLOT_NOREF);
5113 /* the original alloc_size is already aligned: there is %ebp and retip pushed, so realign */
5114 if (mono_do_x86_stack_align && need_stack_frame) {
5115 int tot = alloc_size + pos + 4; /* ret ip */
5116 if (need_stack_frame)
5118 tot &= MONO_ARCH_FRAME_ALIGNMENT - 1;
5120 alloc_size += MONO_ARCH_FRAME_ALIGNMENT - tot;
5121 for (i = 0; i < MONO_ARCH_FRAME_ALIGNMENT - tot; i += sizeof (mgreg_t))
5122 mini_gc_set_slot_type_from_fp (cfg, - (alloc_size + pos - i), SLOT_NOREF);
5126 cfg->arch.sp_fp_offset = alloc_size + pos;
5129 /* See mono_emit_stack_alloc */
5130 #if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
5131 guint32 remaining_size = alloc_size;
5132 /*FIXME handle unbounded code expansion, we should use a loop in case of more than X interactions*/
5133 guint32 required_code_size = ((remaining_size / 0x1000) + 1) * 8; /*8 is the max size of x86_alu_reg_imm + x86_test_membase_reg*/
5134 guint32 offset = code - cfg->native_code;
5135 if (G_UNLIKELY (required_code_size >= (cfg->code_size - offset))) {
5136 while (required_code_size >= (cfg->code_size - offset))
5137 cfg->code_size *= 2;
5138 cfg->native_code = mono_realloc_native_code(cfg);
5139 code = cfg->native_code + offset;
5140 cfg->stat_code_reallocs++;
5142 while (remaining_size >= 0x1000) {
5143 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
5144 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
5145 remaining_size -= 0x1000;
5148 x86_alu_reg_imm (code, X86_SUB, X86_ESP, remaining_size);
5150 x86_alu_reg_imm (code, X86_SUB, X86_ESP, alloc_size);
5153 g_assert (need_stack_frame);
5156 if (cfg->method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED ||
5157 cfg->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
5158 x86_alu_reg_imm (code, X86_AND, X86_ESP, -MONO_ARCH_FRAME_ALIGNMENT);
5161 #if DEBUG_STACK_ALIGNMENT
5162 /* check the stack is aligned */
5163 if (need_stack_frame && method->wrapper_type == MONO_WRAPPER_NONE) {
5164 x86_mov_reg_reg (code, X86_ECX, X86_ESP, 4);
5165 x86_alu_reg_imm (code, X86_AND, X86_ECX, MONO_ARCH_FRAME_ALIGNMENT - 1);
5166 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
5167 x86_branch_disp (code, X86_CC_EQ, 3, FALSE);
5168 x86_breakpoint (code);
5172 /* compute max_offset in order to use short forward jumps */
5174 if (cfg->opt & MONO_OPT_BRANCH) {
5175 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5177 bb->max_offset = max_offset;
5179 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
5181 /* max alignment for loops */
5182 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
5183 max_offset += LOOP_ALIGNMENT;
5184 MONO_BB_FOR_EACH_INS (bb, ins) {
5185 if (ins->opcode == OP_LABEL)
5186 ins->inst_c1 = max_offset;
5187 max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
5192 /* store runtime generic context */
5193 if (cfg->rgctx_var) {
5194 g_assert (cfg->rgctx_var->opcode == OP_REGOFFSET && cfg->rgctx_var->inst_basereg == X86_EBP);
5196 x86_mov_membase_reg (code, X86_EBP, cfg->rgctx_var->inst_offset, MONO_ARCH_RGCTX_REG, 4);
5199 if (method->save_lmf)
5200 code = emit_setup_lmf (cfg, code, cfg->lmf_var->inst_offset, cfa_offset);
5202 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
5203 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
5208 if (cfg->arch.ss_tramp_var) {
5209 /* Initialize ss_tramp_var */
5210 ins = cfg->arch.ss_tramp_var;
5211 g_assert (ins->opcode == OP_REGOFFSET);
5213 g_assert (!cfg->compile_aot);
5214 x86_mov_membase_imm (code, ins->inst_basereg, ins->inst_offset, (guint32)&ss_trampoline, 4);
5217 if (cfg->arch.bp_tramp_var) {
5218 /* Initialize bp_tramp_var */
5219 ins = cfg->arch.bp_tramp_var;
5220 g_assert (ins->opcode == OP_REGOFFSET);
5222 g_assert (!cfg->compile_aot);
5223 x86_mov_membase_imm (code, ins->inst_basereg, ins->inst_offset, (guint32)&bp_trampoline, 4);
5227 /* load arguments allocated to register from the stack */
5228 sig = mono_method_signature (method);
5231 cinfo = (CallInfo *)cfg->arch.cinfo;
5233 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
5234 inst = cfg->args [pos];
5235 ainfo = &cinfo->args [pos];
5236 if (inst->opcode == OP_REGVAR) {
5237 g_assert (need_stack_frame);
5238 x86_mov_reg_membase (code, inst->dreg, X86_EBP, ainfo->offset + ARGS_OFFSET, 4);
5239 if (cfg->verbose_level > 2)
5240 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
5245 cfg->code_len = code - cfg->native_code;
5247 g_assert (cfg->code_len < cfg->code_size);
5253 mono_arch_emit_epilog (MonoCompile *cfg)
5255 MonoMethod *method = cfg->method;
5256 MonoMethodSignature *sig = mono_method_signature (method);
5258 guint32 stack_to_pop;
5260 int max_epilog_size = 16;
5262 gboolean need_stack_frame = needs_stack_frame (cfg);
5264 if (cfg->method->save_lmf)
5265 max_epilog_size += 128;
5267 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
5268 cfg->code_size *= 2;
5269 cfg->native_code = mono_realloc_native_code(cfg);
5270 cfg->stat_code_reallocs++;
5273 code = cfg->native_code + cfg->code_len;
5275 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
5276 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
5278 /* the code restoring the registers must be kept in sync with OP_TAILCALL */
5281 if (method->save_lmf) {
5282 gint32 lmf_offset = cfg->lmf_var->inst_offset;
5285 /* check if we need to restore protection of the stack after a stack overflow */
5286 if (!cfg->compile_aot && mono_arch_have_fast_tls () && mono_tls_get_tls_offset (TLS_KEY_JIT_TLS) != -1) {
5287 code = mono_x86_emit_tls_get (code, X86_ECX, mono_tls_get_tls_offset (TLS_KEY_JIT_TLS));
5289 /* we load the value in a separate instruction: this mechanism may be
5290 * used later as a safer way to do thread interruption
5292 x86_mov_reg_membase (code, X86_ECX, X86_ECX, MONO_STRUCT_OFFSET (MonoJitTlsData, restore_stack_prot), 4);
5293 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
5295 x86_branch8 (code, X86_CC_Z, 0, FALSE);
5296 /* note that the call trampoline will preserve eax/edx */
5297 x86_call_reg (code, X86_ECX);
5298 x86_patch (patch, code);
5301 /* restore caller saved regs */
5302 if (cfg->used_int_regs & (1 << X86_EBX)) {
5303 x86_mov_reg_membase (code, X86_EBX, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, ebx), 4);
5306 if (cfg->used_int_regs & (1 << X86_EDI)) {
5307 x86_mov_reg_membase (code, X86_EDI, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, edi), 4);
5309 if (cfg->used_int_regs & (1 << X86_ESI)) {
5310 x86_mov_reg_membase (code, X86_ESI, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, esi), 4);
5313 /* EBP is restored by LEAVE */
5315 for (i = 0; i < X86_NREG; ++i) {
5316 if ((cfg->used_int_regs & X86_CALLER_REGS & (1 << i)) && (i != X86_EBP)) {
5322 g_assert (need_stack_frame);
5323 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
5327 g_assert (need_stack_frame);
5328 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
5331 if (cfg->used_int_regs & (1 << X86_ESI)) {
5332 x86_pop_reg (code, X86_ESI);
5334 if (cfg->used_int_regs & (1 << X86_EDI)) {
5335 x86_pop_reg (code, X86_EDI);
5337 if (cfg->used_int_regs & (1 << X86_EBX)) {
5338 x86_pop_reg (code, X86_EBX);
5342 /* Load returned vtypes into registers if needed */
5343 cinfo = (CallInfo *)cfg->arch.cinfo;
5344 if (cinfo->ret.storage == ArgValuetypeInReg) {
5345 for (quad = 0; quad < 2; quad ++) {
5346 switch (cinfo->ret.pair_storage [quad]) {
5348 x86_mov_reg_membase (code, cinfo->ret.pair_regs [quad], cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), 4);
5350 case ArgOnFloatFpStack:
5351 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), FALSE);
5353 case ArgOnDoubleFpStack:
5354 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), TRUE);
5359 g_assert_not_reached ();
5364 if (need_stack_frame)
5367 if (CALLCONV_IS_STDCALL (sig)) {
5368 MonoJitArgumentInfo *arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
5370 stack_to_pop = mono_arch_get_argument_info (sig, sig->param_count, arg_info);
5371 } else if (cinfo->callee_stack_pop)
5372 stack_to_pop = cinfo->callee_stack_pop;
5377 g_assert (need_stack_frame);
5378 x86_ret_imm (code, stack_to_pop);
5383 cfg->code_len = code - cfg->native_code;
5385 g_assert (cfg->code_len < cfg->code_size);
5389 mono_arch_emit_exceptions (MonoCompile *cfg)
5391 MonoJumpInfo *patch_info;
5394 MonoClass *exc_classes [16];
5395 guint8 *exc_throw_start [16], *exc_throw_end [16];
5399 /* Compute needed space */
5400 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5401 if (patch_info->type == MONO_PATCH_INFO_EXC)
5406 * make sure we have enough space for exceptions
5407 * 16 is the size of two push_imm instructions and a call
5409 if (cfg->compile_aot)
5410 code_size = exc_count * 32;
5412 code_size = exc_count * 16;
5414 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
5415 cfg->code_size *= 2;
5416 cfg->native_code = mono_realloc_native_code(cfg);
5417 cfg->stat_code_reallocs++;
5420 code = cfg->native_code + cfg->code_len;
5423 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5424 switch (patch_info->type) {
5425 case MONO_PATCH_INFO_EXC: {
5426 MonoClass *exc_class;
5430 x86_patch (patch_info->ip.i + cfg->native_code, code);
5432 exc_class = mono_class_load_from_name (mono_defaults.corlib, "System", patch_info->data.name);
5433 throw_ip = patch_info->ip.i;
5435 /* Find a throw sequence for the same exception class */
5436 for (i = 0; i < nthrows; ++i)
5437 if (exc_classes [i] == exc_class)
5440 x86_push_imm (code, (exc_throw_end [i] - cfg->native_code) - throw_ip);
5441 x86_jump_code (code, exc_throw_start [i]);
5442 patch_info->type = MONO_PATCH_INFO_NONE;
5447 /* Compute size of code following the push <OFFSET> */
5450 /*This is aligned to 16 bytes by the callee. This way we save a few bytes here.*/
5452 if ((code - cfg->native_code) - throw_ip < 126 - size) {
5453 /* Use the shorter form */
5455 x86_push_imm (code, 0);
5459 x86_push_imm (code, 0xf0f0f0f0);
5464 exc_classes [nthrows] = exc_class;
5465 exc_throw_start [nthrows] = code;
5468 x86_push_imm (code, exc_class->type_token - MONO_TOKEN_TYPE_DEF);
5469 patch_info->data.name = "mono_arch_throw_corlib_exception";
5470 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
5471 patch_info->ip.i = code - cfg->native_code;
5472 x86_call_code (code, 0);
5473 x86_push_imm (buf, (code - cfg->native_code) - throw_ip);
5478 exc_throw_end [nthrows] = code;
5490 cfg->code_len = code - cfg->native_code;
5492 g_assert (cfg->code_len < cfg->code_size);
5496 mono_arch_flush_icache (guint8 *code, gint size)
5502 mono_arch_flush_register_windows (void)
5507 mono_arch_is_inst_imm (gint64 imm)
5513 mono_arch_finish_init (void)
5515 if (!g_getenv ("MONO_NO_TLS")) {
5516 #ifndef TARGET_WIN32
5518 optimize_for_xen = access ("/proc/xen", F_OK) == 0;
5525 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
5529 // Linear handler, the bsearch head compare is shorter
5530 //[2 + 4] x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
5531 //[1 + 1] x86_branch8(inst,cond,imm,is_signed)
5532 // x86_patch(ins,target)
5533 //[1 + 5] x86_jump_mem(inst,mem)
5536 #define BR_SMALL_SIZE 2
5537 #define BR_LARGE_SIZE 5
5538 #define JUMP_IMM_SIZE 6
5539 #define ENABLE_WRONG_METHOD_CHECK 0
5543 imt_branch_distance (MonoIMTCheckItem **imt_entries, int start, int target)
5545 int i, distance = 0;
5546 for (i = start; i < target; ++i)
5547 distance += imt_entries [i]->chunk_size;
5552 * LOCKING: called with the domain lock held
5555 mono_arch_build_imt_trampoline (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
5556 gpointer fail_tramp)
5560 guint8 *code, *start;
5563 for (i = 0; i < count; ++i) {
5564 MonoIMTCheckItem *item = imt_entries [i];
5565 if (item->is_equals) {
5566 if (item->check_target_idx) {
5567 if (!item->compare_done)
5568 item->chunk_size += CMP_SIZE;
5569 item->chunk_size += BR_SMALL_SIZE + JUMP_IMM_SIZE;
5572 item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + JUMP_IMM_SIZE * 2;
5574 item->chunk_size += JUMP_IMM_SIZE;
5575 #if ENABLE_WRONG_METHOD_CHECK
5576 item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
5581 item->chunk_size += CMP_SIZE + BR_LARGE_SIZE;
5582 imt_entries [item->check_target_idx]->compare_done = TRUE;
5584 size += item->chunk_size;
5587 code = mono_method_alloc_generic_virtual_trampoline (domain, size);
5589 code = mono_domain_code_reserve (domain, size);
5592 unwind_ops = mono_arch_get_cie_program ();
5594 for (i = 0; i < count; ++i) {
5595 MonoIMTCheckItem *item = imt_entries [i];
5596 item->code_target = code;
5597 if (item->is_equals) {
5598 if (item->check_target_idx) {
5599 if (!item->compare_done)
5600 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5601 item->jmp_code = code;
5602 x86_branch8 (code, X86_CC_NE, 0, FALSE);
5603 if (item->has_target_code)
5604 x86_jump_code (code, item->value.target_code);
5606 x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
5609 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5610 item->jmp_code = code;
5611 x86_branch8 (code, X86_CC_NE, 0, FALSE);
5612 if (item->has_target_code)
5613 x86_jump_code (code, item->value.target_code);
5615 x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
5616 x86_patch (item->jmp_code, code);
5617 x86_jump_code (code, fail_tramp);
5618 item->jmp_code = NULL;
5620 /* enable the commented code to assert on wrong method */
5621 #if ENABLE_WRONG_METHOD_CHECK
5622 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5623 item->jmp_code = code;
5624 x86_branch8 (code, X86_CC_NE, 0, FALSE);
5626 if (item->has_target_code)
5627 x86_jump_code (code, item->value.target_code);
5629 x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
5630 #if ENABLE_WRONG_METHOD_CHECK
5631 x86_patch (item->jmp_code, code);
5632 x86_breakpoint (code);
5633 item->jmp_code = NULL;
5638 x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
5639 item->jmp_code = code;
5640 if (x86_is_imm8 (imt_branch_distance (imt_entries, i, item->check_target_idx)))
5641 x86_branch8 (code, X86_CC_GE, 0, FALSE);
5643 x86_branch32 (code, X86_CC_GE, 0, FALSE);
5646 /* patch the branches to get to the target items */
5647 for (i = 0; i < count; ++i) {
5648 MonoIMTCheckItem *item = imt_entries [i];
5649 if (item->jmp_code) {
5650 if (item->check_target_idx) {
5651 x86_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
5657 mono_stats.imt_trampolines_size += code - start;
5658 g_assert (code - start <= size);
5662 char *buff = g_strdup_printf ("thunk_for_class_%s_%s_entries_%d", vtable->klass->name_space, vtable->klass->name, count);
5663 mono_disassemble_code (NULL, (guint8*)start, code - start, buff);
5667 if (mono_jit_map_is_enabled ()) {
5670 buff = g_strdup_printf ("imt_%s_%s_entries_%d", vtable->klass->name_space, vtable->klass->name, count);
5672 buff = g_strdup_printf ("imt_trampoline_entries_%d", count);
5673 mono_emit_jit_tramp (start, code - start, buff);
5677 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE, NULL);
5679 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
5685 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
5687 return (MonoMethod*) regs [MONO_ARCH_IMT_REG];
5691 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
5693 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
5697 mono_arch_get_cie_program (void)
5701 mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
5702 mono_add_unwind_op_offset (l, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
5708 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5710 MonoInst *ins = NULL;
5713 if (cmethod->klass == mono_defaults.math_class) {
5714 if (strcmp (cmethod->name, "Sin") == 0) {
5716 } else if (strcmp (cmethod->name, "Cos") == 0) {
5718 } else if (strcmp (cmethod->name, "Tan") == 0) {
5720 } else if (strcmp (cmethod->name, "Atan") == 0) {
5722 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
5724 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
5726 } else if (strcmp (cmethod->name, "Round") == 0 && fsig->param_count == 1 && fsig->params [0]->type == MONO_TYPE_R8) {
5730 if (opcode && fsig->param_count == 1) {
5731 MONO_INST_NEW (cfg, ins, opcode);
5732 ins->type = STACK_R8;
5733 ins->dreg = mono_alloc_freg (cfg);
5734 ins->sreg1 = args [0]->dreg;
5735 MONO_ADD_INS (cfg->cbb, ins);
5738 if (cfg->opt & MONO_OPT_CMOV) {
5741 if (strcmp (cmethod->name, "Min") == 0) {
5742 if (fsig->params [0]->type == MONO_TYPE_I4)
5744 } else if (strcmp (cmethod->name, "Max") == 0) {
5745 if (fsig->params [0]->type == MONO_TYPE_I4)
5749 if (opcode && fsig->param_count == 2) {
5750 MONO_INST_NEW (cfg, ins, opcode);
5751 ins->type = STACK_I4;
5752 ins->dreg = mono_alloc_ireg (cfg);
5753 ins->sreg1 = args [0]->dreg;
5754 ins->sreg2 = args [1]->dreg;
5755 MONO_ADD_INS (cfg->cbb, ins);
5760 /* OP_FREM is not IEEE compatible */
5761 else if (strcmp (cmethod->name, "IEEERemainder") == 0 && fsig->param_count == 2) {
5762 MONO_INST_NEW (cfg, ins, OP_FREM);
5763 ins->inst_i0 = args [0];
5764 ins->inst_i1 = args [1];
5773 mono_arch_print_tree (MonoInst *tree, int arity)
5779 mono_arch_get_patch_offset (guint8 *code)
5781 if ((code [0] == 0x8b) && (x86_modrm_mod (code [1]) == 0x2))
5783 else if (code [0] == 0xba)
5785 else if (code [0] == 0x68)
5788 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x6))
5789 /* push <OFFSET>(<REG>) */
5791 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x2))
5792 /* call *<OFFSET>(<REG>) */
5794 else if ((code [0] == 0xdd) || (code [0] == 0xd9))
5797 else if ((code [0] == 0x58) && (code [1] == 0x05))
5798 /* pop %eax; add <OFFSET>, %eax */
5800 else if ((code [0] >= 0x58) && (code [0] <= 0x58 + X86_NREG) && (code [1] == 0x81))
5801 /* pop <REG>; add <OFFSET>, <REG> */
5803 else if ((code [0] >= 0xb8) && (code [0] < 0xb8 + 8))
5804 /* mov <REG>, imm */
5807 g_assert_not_reached ();
5813 * mono_breakpoint_clean_code:
5815 * Copy @size bytes from @code - @offset to the buffer @buf. If the debugger inserted software
5816 * breakpoints in the original code, they are removed in the copy.
5818 * Returns TRUE if no sw breakpoint was present.
5821 mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size)
5824 * If method_start is non-NULL we need to perform bound checks, since we access memory
5825 * at code - offset we could go before the start of the method and end up in a different
5826 * page of memory that is not mapped or read incorrect data anyway. We zero-fill the bytes
5829 if (!method_start || code - offset >= method_start) {
5830 memcpy (buf, code - offset, size);
5832 int diff = code - method_start;
5833 memset (buf, 0, size);
5834 memcpy (buf + offset - diff, method_start, diff + size - offset);
5840 * mono_x86_get_this_arg_offset:
5842 * Return the offset of the stack location where this is passed during a virtual
5846 mono_x86_get_this_arg_offset (MonoMethodSignature *sig)
5852 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
5854 guint32 esp = regs [X86_ESP];
5861 * The stack looks like:
5865 res = ((MonoObject**)esp) [0];
5869 #define MAX_ARCH_DELEGATE_PARAMS 10
5872 get_delegate_invoke_impl (MonoTrampInfo **info, gboolean has_target, guint32 param_count)
5874 guint8 *code, *start;
5875 int code_reserve = 64;
5878 unwind_ops = mono_arch_get_cie_program ();
5881 * The stack contains:
5887 start = code = mono_global_codeman_reserve (code_reserve);
5889 /* Replace the this argument with the target */
5890 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
5891 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 4);
5892 x86_mov_membase_reg (code, X86_ESP, 4, X86_ECX, 4);
5893 x86_jump_membase (code, X86_EAX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
5895 g_assert ((code - start) < code_reserve);
5898 /* 8 for mov_reg and jump, plus 8 for each parameter */
5899 code_reserve = 8 + (param_count * 8);
5901 * The stack contains:
5902 * <args in reverse order>
5907 * <args in reverse order>
5910 * without unbalancing the stack.
5911 * So move each arg up a spot in the stack (overwriting un-needed 'this' arg)
5912 * and leaving original spot of first arg as placeholder in stack so
5913 * when callee pops stack everything works.
5916 start = code = mono_global_codeman_reserve (code_reserve);
5918 /* store delegate for access to method_ptr */
5919 x86_mov_reg_membase (code, X86_ECX, X86_ESP, 4, 4);
5922 for (i = 0; i < param_count; ++i) {
5923 x86_mov_reg_membase (code, X86_EAX, X86_ESP, (i+2)*4, 4);
5924 x86_mov_membase_reg (code, X86_ESP, (i+1)*4, X86_EAX, 4);
5927 x86_jump_membase (code, X86_ECX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
5929 g_assert ((code - start) < code_reserve);
5933 *info = mono_tramp_info_create ("delegate_invoke_impl_has_target", start, code - start, NULL, unwind_ops);
5935 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", param_count);
5936 *info = mono_tramp_info_create (name, start, code - start, NULL, unwind_ops);
5940 if (mono_jit_map_is_enabled ()) {
5943 buff = (char*)"delegate_invoke_has_target";
5945 buff = g_strdup_printf ("delegate_invoke_no_target_%d", param_count);
5946 mono_emit_jit_tramp (start, code - start, buff);
5950 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
5955 #define MAX_VIRTUAL_DELEGATE_OFFSET 32
5958 get_delegate_virtual_invoke_impl (MonoTrampInfo **info, gboolean load_imt_reg, int offset)
5960 guint8 *code, *start;
5965 if (offset / (int)sizeof (gpointer) > MAX_VIRTUAL_DELEGATE_OFFSET)
5969 * The stack contains:
5973 start = code = mono_global_codeman_reserve (size);
5975 unwind_ops = mono_arch_get_cie_program ();
5977 /* Replace the this argument with the target */
5978 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
5979 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 4);
5980 x86_mov_membase_reg (code, X86_ESP, 4, X86_ECX, 4);
5983 /* Load the IMT reg */
5984 x86_mov_reg_membase (code, MONO_ARCH_IMT_REG, X86_EAX, MONO_STRUCT_OFFSET (MonoDelegate, method), 4);
5987 /* Load the vtable */
5988 x86_mov_reg_membase (code, X86_EAX, X86_ECX, MONO_STRUCT_OFFSET (MonoObject, vtable), 4);
5989 x86_jump_membase (code, X86_EAX, offset);
5990 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
5992 tramp_name = mono_get_delegate_virtual_invoke_impl_name (load_imt_reg, offset);
5993 *info = mono_tramp_info_create (tramp_name, start, code - start, NULL, unwind_ops);
5994 g_free (tramp_name);
6001 mono_arch_get_delegate_invoke_impls (void)
6004 MonoTrampInfo *info;
6007 get_delegate_invoke_impl (&info, TRUE, 0);
6008 res = g_slist_prepend (res, info);
6010 for (i = 0; i <= MAX_ARCH_DELEGATE_PARAMS; ++i) {
6011 get_delegate_invoke_impl (&info, FALSE, i);
6012 res = g_slist_prepend (res, info);
6015 for (i = 0; i <= MAX_VIRTUAL_DELEGATE_OFFSET; ++i) {
6016 get_delegate_virtual_invoke_impl (&info, TRUE, - i * SIZEOF_VOID_P);
6017 res = g_slist_prepend (res, info);
6019 get_delegate_virtual_invoke_impl (&info, FALSE, i * SIZEOF_VOID_P);
6020 res = g_slist_prepend (res, info);
6027 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
6029 guint8 *code, *start;
6031 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
6034 /* FIXME: Support more cases */
6035 if (MONO_TYPE_ISSTRUCT (sig->ret))
6039 * The stack contains:
6045 static guint8* cached = NULL;
6049 if (mono_aot_only) {
6050 start = mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
6052 MonoTrampInfo *info;
6053 start = get_delegate_invoke_impl (&info, TRUE, 0);
6054 mono_tramp_info_register (info, NULL);
6057 mono_memory_barrier ();
6061 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
6064 for (i = 0; i < sig->param_count; ++i)
6065 if (!mono_is_regsize_var (sig->params [i]))
6068 code = cache [sig->param_count];
6072 if (mono_aot_only) {
6073 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
6074 start = mono_aot_get_trampoline (name);
6077 MonoTrampInfo *info;
6078 start = get_delegate_invoke_impl (&info, FALSE, sig->param_count);
6079 mono_tramp_info_register (info, NULL);
6082 mono_memory_barrier ();
6084 cache [sig->param_count] = start;
6091 mono_arch_get_delegate_virtual_invoke_impl (MonoMethodSignature *sig, MonoMethod *method, int offset, gboolean load_imt_reg)
6093 MonoTrampInfo *info;
6096 code = get_delegate_virtual_invoke_impl (&info, load_imt_reg, offset);
6098 mono_tramp_info_register (info, NULL);
6103 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
6106 case X86_EAX: return ctx->eax;
6107 case X86_EBX: return ctx->ebx;
6108 case X86_ECX: return ctx->ecx;
6109 case X86_EDX: return ctx->edx;
6110 case X86_ESP: return ctx->esp;
6111 case X86_EBP: return ctx->ebp;
6112 case X86_ESI: return ctx->esi;
6113 case X86_EDI: return ctx->edi;
6115 g_assert_not_reached ();
6121 mono_arch_context_set_int_reg (MonoContext *ctx, int reg, mgreg_t val)
6149 g_assert_not_reached ();
6153 #ifdef MONO_ARCH_SIMD_INTRINSICS
6156 get_float_to_x_spill_area (MonoCompile *cfg)
6158 if (!cfg->fconv_to_r8_x_var) {
6159 cfg->fconv_to_r8_x_var = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
6160 cfg->fconv_to_r8_x_var->flags |= MONO_INST_VOLATILE; /*FIXME, use the don't regalloc flag*/
6162 return cfg->fconv_to_r8_x_var;
6166 * Convert all fconv opts that MONO_OPT_SSE2 would get wrong.
6169 mono_arch_decompose_opts (MonoCompile *cfg, MonoInst *ins)
6172 int dreg, src_opcode;
6174 if (!(cfg->opt & MONO_OPT_SSE2) || !(cfg->opt & MONO_OPT_SIMD) || COMPILE_LLVM (cfg))
6177 switch (src_opcode = ins->opcode) {
6178 case OP_FCONV_TO_I1:
6179 case OP_FCONV_TO_U1:
6180 case OP_FCONV_TO_I2:
6181 case OP_FCONV_TO_U2:
6182 case OP_FCONV_TO_I4:
6189 /* dreg is the IREG and sreg1 is the FREG */
6190 MONO_INST_NEW (cfg, fconv, OP_FCONV_TO_R8_X);
6191 fconv->klass = NULL; /*FIXME, what can I use here as the Mono.Simd lib might not be loaded yet*/
6192 fconv->sreg1 = ins->sreg1;
6193 fconv->dreg = mono_alloc_ireg (cfg);
6194 fconv->type = STACK_VTYPE;
6195 fconv->backend.spill_var = get_float_to_x_spill_area (cfg);
6197 mono_bblock_insert_before_ins (cfg->cbb, ins, fconv);
6201 ins->opcode = OP_XCONV_R8_TO_I4;
6203 ins->klass = mono_defaults.int32_class;
6204 ins->sreg1 = fconv->dreg;
6206 ins->type = STACK_I4;
6207 ins->backend.source_opcode = src_opcode;
6210 #endif /* #ifdef MONO_ARCH_SIMD_INTRINSICS */
6213 mono_arch_decompose_long_opts (MonoCompile *cfg, MonoInst *long_ins)
6218 if (long_ins->opcode == OP_LNEG) {
6220 MONO_EMIT_NEW_UNALU (cfg, OP_INEG, MONO_LVREG_LS (ins->dreg), MONO_LVREG_LS (ins->sreg1));
6221 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADC_IMM, MONO_LVREG_MS (ins->dreg), MONO_LVREG_MS (ins->sreg1), 0);
6222 MONO_EMIT_NEW_UNALU (cfg, OP_INEG, MONO_LVREG_MS (ins->dreg), MONO_LVREG_MS (ins->dreg));
6227 #ifdef MONO_ARCH_SIMD_INTRINSICS
6229 if (!(cfg->opt & MONO_OPT_SIMD))
6232 /*TODO move this to simd-intrinsic.c once we support sse 4.1 dword extractors since we need the runtime caps info */
6233 switch (long_ins->opcode) {
6235 vreg = long_ins->sreg1;
6237 if (long_ins->inst_c0) {
6238 MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
6239 ins->klass = long_ins->klass;
6240 ins->sreg1 = long_ins->sreg1;
6242 ins->type = STACK_VTYPE;
6243 ins->dreg = vreg = alloc_ireg (cfg);
6244 MONO_ADD_INS (cfg->cbb, ins);
6247 MONO_INST_NEW (cfg, ins, OP_EXTRACT_I4);
6248 ins->klass = mono_defaults.int32_class;
6250 ins->type = STACK_I4;
6251 ins->dreg = MONO_LVREG_LS (long_ins->dreg);
6252 MONO_ADD_INS (cfg->cbb, ins);
6254 MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
6255 ins->klass = long_ins->klass;
6256 ins->sreg1 = long_ins->sreg1;
6257 ins->inst_c0 = long_ins->inst_c0 ? 3 : 1;
6258 ins->type = STACK_VTYPE;
6259 ins->dreg = vreg = alloc_ireg (cfg);
6260 MONO_ADD_INS (cfg->cbb, ins);
6262 MONO_INST_NEW (cfg, ins, OP_EXTRACT_I4);
6263 ins->klass = mono_defaults.int32_class;
6265 ins->type = STACK_I4;
6266 ins->dreg = MONO_LVREG_MS (long_ins->dreg);
6267 MONO_ADD_INS (cfg->cbb, ins);
6269 long_ins->opcode = OP_NOP;
6271 case OP_INSERTX_I8_SLOW:
6272 MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
6273 ins->dreg = long_ins->dreg;
6274 ins->sreg1 = long_ins->dreg;
6275 ins->sreg2 = MONO_LVREG_LS (long_ins->sreg2);
6276 ins->inst_c0 = long_ins->inst_c0 * 2;
6277 MONO_ADD_INS (cfg->cbb, ins);
6279 MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
6280 ins->dreg = long_ins->dreg;
6281 ins->sreg1 = long_ins->dreg;
6282 ins->sreg2 = MONO_LVREG_MS (long_ins->sreg2);
6283 ins->inst_c0 = long_ins->inst_c0 * 2 + 1;
6284 MONO_ADD_INS (cfg->cbb, ins);
6286 long_ins->opcode = OP_NOP;
6289 MONO_INST_NEW (cfg, ins, OP_ICONV_TO_X);
6290 ins->dreg = long_ins->dreg;
6291 ins->sreg1 = MONO_LVREG_LS (long_ins->sreg1);
6292 ins->klass = long_ins->klass;
6293 ins->type = STACK_VTYPE;
6294 MONO_ADD_INS (cfg->cbb, ins);
6296 MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
6297 ins->dreg = long_ins->dreg;
6298 ins->sreg1 = long_ins->dreg;
6299 ins->sreg2 = MONO_LVREG_MS (long_ins->sreg1);
6301 ins->klass = long_ins->klass;
6302 ins->type = STACK_VTYPE;
6303 MONO_ADD_INS (cfg->cbb, ins);
6305 MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
6306 ins->dreg = long_ins->dreg;
6307 ins->sreg1 = long_ins->dreg;;
6308 ins->inst_c0 = 0x44; /*Magic number for swizzling (X,Y,X,Y)*/
6309 ins->klass = long_ins->klass;
6310 ins->type = STACK_VTYPE;
6311 MONO_ADD_INS (cfg->cbb, ins);
6313 long_ins->opcode = OP_NOP;
6316 #endif /* MONO_ARCH_SIMD_INTRINSICS */
6319 /*MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD*/
6321 mono_arch_install_handler_block_guard (MonoJitInfo *ji, MonoJitExceptionInfo *clause, MonoContext *ctx, gpointer new_value)
6324 gpointer *sp, old_value;
6327 offset = clause->exvar_offset;
6330 bp = MONO_CONTEXT_GET_BP (ctx);
6331 sp = *(gpointer*)(bp + offset);
6334 if (old_value < ji->code_start || (char*)old_value > ((char*)ji->code_start + ji->code_size))
6343 * mono_aot_emit_load_got_addr:
6345 * Emit code to load the got address.
6346 * On x86, the result is placed into EBX.
6349 mono_arch_emit_load_got_addr (guint8 *start, guint8 *code, MonoCompile *cfg, MonoJumpInfo **ji)
6351 x86_call_imm (code, 0);
6353 * The patch needs to point to the pop, since the GOT offset needs
6354 * to be added to that address.
6357 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GOT_OFFSET, NULL);
6359 *ji = mono_patch_info_list_prepend (*ji, code - start, MONO_PATCH_INFO_GOT_OFFSET, NULL);
6360 x86_pop_reg (code, MONO_ARCH_GOT_REG);
6361 x86_alu_reg_imm (code, X86_ADD, MONO_ARCH_GOT_REG, 0xf0f0f0f0);
6367 emit_load_aotconst (guint8 *start, guint8 *code, MonoCompile *cfg, MonoJumpInfo **ji, int dreg, int tramp_type, gconstpointer target)
6370 mono_add_patch_info (cfg, code - cfg->native_code, tramp_type, target);
6372 g_assert_not_reached ();
6373 x86_mov_reg_membase (code, dreg, MONO_ARCH_GOT_REG, 0xf0f0f0f0, 4);
6378 * mono_arch_emit_load_aotconst:
6380 * Emit code to load the contents of the GOT slot identified by TRAMP_TYPE and
6381 * TARGET from the mscorlib GOT in full-aot code.
6382 * On x86, the GOT address is assumed to be in EBX, and the result is placed into
6386 mono_arch_emit_load_aotconst (guint8 *start, guint8 *code, MonoJumpInfo **ji, MonoJumpInfoType tramp_type, gconstpointer target)
6388 /* Load the mscorlib got address */
6389 x86_mov_reg_membase (code, X86_EAX, MONO_ARCH_GOT_REG, sizeof (gpointer), 4);
6390 *ji = mono_patch_info_list_prepend (*ji, code - start, tramp_type, target);
6391 /* arch_emit_got_access () patches this */
6392 x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0xf0f0f0f0, 4);
6397 /* Can't put this into mini-x86.h */
6399 mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot);
6402 mono_arch_get_trampolines (gboolean aot)
6404 MonoTrampInfo *info;
6405 GSList *tramps = NULL;
6407 mono_x86_get_signal_exception_trampoline (&info, aot);
6409 tramps = g_slist_append (tramps, info);
6414 /* Soft Debug support */
6415 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
6418 * mono_arch_set_breakpoint:
6420 * Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
6421 * The location should contain code emitted by OP_SEQ_POINT.
6424 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
6426 guint8 *code = ip + OP_SEQ_POINT_BP_OFFSET;
6428 g_assert (code [0] == 0x90);
6429 x86_call_membase (code, X86_ECX, 0);
6433 * mono_arch_clear_breakpoint:
6435 * Clear the breakpoint at IP.
6438 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
6440 guint8 *code = ip + OP_SEQ_POINT_BP_OFFSET;
6443 for (i = 0; i < 2; ++i)
6448 * mono_arch_start_single_stepping:
6450 * Start single stepping.
6453 mono_arch_start_single_stepping (void)
6455 ss_trampoline = mini_get_single_step_trampoline ();
6459 * mono_arch_stop_single_stepping:
6461 * Stop single stepping.
6464 mono_arch_stop_single_stepping (void)
6466 ss_trampoline = NULL;
6470 * mono_arch_is_single_step_event:
6472 * Return whenever the machine state in SIGCTX corresponds to a single
6476 mono_arch_is_single_step_event (void *info, void *sigctx)
6478 /* We use soft breakpoints */
6483 mono_arch_is_breakpoint_event (void *info, void *sigctx)
6485 /* We use soft breakpoints */
6489 #define BREAKPOINT_SIZE 2
6492 * mono_arch_skip_breakpoint:
6494 * See mini-amd64.c for docs.
6497 mono_arch_skip_breakpoint (MonoContext *ctx, MonoJitInfo *ji)
6499 g_assert_not_reached ();
6503 * mono_arch_skip_single_step:
6505 * See mini-amd64.c for docs.
6508 mono_arch_skip_single_step (MonoContext *ctx)
6510 g_assert_not_reached ();
6514 * mono_arch_get_seq_point_info:
6516 * See mini-amd64.c for docs.
6519 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
6526 mono_arch_init_lmf_ext (MonoLMFExt *ext, gpointer prev_lmf)
6528 ext->lmf.previous_lmf = (gsize)prev_lmf;
6529 /* Mark that this is a MonoLMFExt */
6530 ext->lmf.previous_lmf = (gsize)(gpointer)(((gssize)ext->lmf.previous_lmf) | 2);
6531 ext->lmf.ebp = (gssize)ext;
6537 mono_arch_opcode_supported (int opcode)
6540 case OP_ATOMIC_ADD_I4:
6541 case OP_ATOMIC_EXCHANGE_I4:
6542 case OP_ATOMIC_CAS_I4:
6543 case OP_ATOMIC_LOAD_I1:
6544 case OP_ATOMIC_LOAD_I2:
6545 case OP_ATOMIC_LOAD_I4:
6546 case OP_ATOMIC_LOAD_U1:
6547 case OP_ATOMIC_LOAD_U2:
6548 case OP_ATOMIC_LOAD_U4:
6549 case OP_ATOMIC_LOAD_R4:
6550 case OP_ATOMIC_LOAD_R8:
6551 case OP_ATOMIC_STORE_I1:
6552 case OP_ATOMIC_STORE_I2:
6553 case OP_ATOMIC_STORE_I4:
6554 case OP_ATOMIC_STORE_U1:
6555 case OP_ATOMIC_STORE_U2:
6556 case OP_ATOMIC_STORE_U4:
6557 case OP_ATOMIC_STORE_R4:
6558 case OP_ATOMIC_STORE_R8:
6566 mono_arch_get_call_info (MonoMemPool *mp, MonoMethodSignature *sig)
6568 return get_call_info (mp, sig);