2 * mini-amd64.c: AMD64 backend for the Mono code generator
7 * Paolo Molaro (lupus@ximian.com)
8 * Dietmar Maurer (dietmar@ximian.com)
10 * Zoltan Varga (vargaz@gmail.com)
12 * (C) 2003 Ximian, Inc.
21 #include <mono/metadata/appdomain.h>
22 #include <mono/metadata/debug-helpers.h>
23 #include <mono/metadata/threads.h>
24 #include <mono/metadata/profiler-private.h>
25 #include <mono/metadata/mono-debug.h>
26 #include <mono/utils/mono-math.h>
29 #include "mini-amd64.h"
31 #include "cpu-amd64.h"
34 * Can't define this in mini-amd64.h cause that would turn on the generic code in
37 #define MONO_ARCH_IMT_REG AMD64_R11
39 static gint lmf_tls_offset = -1;
40 static gint lmf_addr_tls_offset = -1;
41 static gint appdomain_tls_offset = -1;
42 static gint thread_tls_offset = -1;
45 static gboolean optimize_for_xen = TRUE;
47 #define optimize_for_xen 0
50 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
52 #define IS_IMM32(val) ((((guint64)val) >> 32) == 0)
54 #define IS_REX(inst) (((inst) >= 0x40) && ((inst) <= 0x4f))
57 /* Under windows, the calling convention is never stdcall */
58 #define CALLCONV_IS_STDCALL(call_conv) (FALSE)
60 #define CALLCONV_IS_STDCALL(call_conv) ((call_conv) == MONO_CALL_STDCALL)
63 /* This mutex protects architecture specific caches */
64 #define mono_mini_arch_lock() EnterCriticalSection (&mini_arch_mutex)
65 #define mono_mini_arch_unlock() LeaveCriticalSection (&mini_arch_mutex)
66 static CRITICAL_SECTION mini_arch_mutex;
69 mono_breakpoint_info [MONO_BREAKPOINT_ARRAY_SIZE];
72 /* On Win64 always reserve first 32 bytes for first four arguments */
73 #define ARGS_OFFSET 48
75 #define ARGS_OFFSET 16
77 #define GP_SCRATCH_REG AMD64_R11
80 * AMD64 register usage:
81 * - callee saved registers are used for global register allocation
82 * - %r11 is used for materializing 64 bit constants in opcodes
83 * - the rest is used for local allocation
87 * Floating point comparison results:
96 void mini_emit_memcpy2 (MonoCompile *cfg, int destreg, int doffset, int srcreg, int soffset, int size, int align);
99 mono_arch_regname (int reg)
102 case AMD64_RAX: return "%rax";
103 case AMD64_RBX: return "%rbx";
104 case AMD64_RCX: return "%rcx";
105 case AMD64_RDX: return "%rdx";
106 case AMD64_RSP: return "%rsp";
107 case AMD64_RBP: return "%rbp";
108 case AMD64_RDI: return "%rdi";
109 case AMD64_RSI: return "%rsi";
110 case AMD64_R8: return "%r8";
111 case AMD64_R9: return "%r9";
112 case AMD64_R10: return "%r10";
113 case AMD64_R11: return "%r11";
114 case AMD64_R12: return "%r12";
115 case AMD64_R13: return "%r13";
116 case AMD64_R14: return "%r14";
117 case AMD64_R15: return "%r15";
122 static const char * xmmregs [] = {
123 "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7", "xmm8",
124 "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15"
128 mono_arch_fregname (int reg)
130 if (reg < AMD64_XMM_NREG)
131 return xmmregs [reg];
136 G_GNUC_UNUSED static void
141 G_GNUC_UNUSED static gboolean
144 static int count = 0;
147 if (!getenv ("COUNT"))
150 if (count == atoi (getenv ("COUNT"))) {
154 if (count > atoi (getenv ("COUNT"))) {
165 return debug_count ();
171 static inline gboolean
172 amd64_is_near_call (guint8 *code)
175 if ((code [0] >= 0x40) && (code [0] <= 0x4f))
178 return code [0] == 0xe8;
182 amd64_patch (unsigned char* code, gpointer target)
187 if ((code [0] >= 0x40) && (code [0] <= 0x4f)) {
192 if ((code [0] & 0xf8) == 0xb8) {
193 /* amd64_set_reg_template */
194 *(guint64*)(code + 1) = (guint64)target;
196 else if ((code [0] == 0x8b) && rex && x86_modrm_mod (code [1]) == 0 && x86_modrm_rm (code [1]) == 5) {
197 /* mov 0(%rip), %dreg */
198 *(guint32*)(code + 2) = (guint32)(guint64)target - 7;
200 else if ((code [0] == 0xff) && (code [1] == 0x15)) {
201 /* call *<OFFSET>(%rip) */
202 *(guint32*)(code + 2) = ((guint32)(guint64)target) - 7;
204 else if ((code [0] == 0xe8)) {
206 gint64 disp = (guint8*)target - (guint8*)code;
207 g_assert (amd64_is_imm32 (disp));
208 x86_patch (code, (unsigned char*)target);
211 x86_patch (code, (unsigned char*)target);
215 mono_amd64_patch (unsigned char* code, gpointer target)
217 amd64_patch (code, target);
226 ArgValuetypeAddrInIReg,
227 ArgNone /* only in pair_storage */
235 /* Only if storage == ArgValuetypeInReg */
236 ArgStorage pair_storage [2];
245 gboolean need_stack_align;
251 #define DEBUG(a) if (cfg->verbose_level > 1) a
253 #define NEW_ICONST(cfg,dest,val) do { \
254 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
255 (dest)->opcode = OP_ICONST; \
256 (dest)->inst_c0 = (val); \
257 (dest)->type = STACK_I4; \
260 #ifdef PLATFORM_WIN32
263 static AMD64_Reg_No param_regs [] = { AMD64_RCX, AMD64_RDX, AMD64_R8, AMD64_R9 };
265 static AMD64_Reg_No return_regs [] = { AMD64_RAX, AMD64_RDX };
269 static AMD64_Reg_No param_regs [] = { AMD64_RDI, AMD64_RSI, AMD64_RDX, AMD64_RCX, AMD64_R8, AMD64_R9 };
271 static AMD64_Reg_No return_regs [] = { AMD64_RAX, AMD64_RDX };
275 add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
277 ainfo->offset = *stack_size;
279 if (*gr >= PARAM_REGS) {
280 ainfo->storage = ArgOnStack;
281 (*stack_size) += sizeof (gpointer);
284 ainfo->storage = ArgInIReg;
285 ainfo->reg = param_regs [*gr];
290 #ifdef PLATFORM_WIN32
291 #define FLOAT_PARAM_REGS 4
293 #define FLOAT_PARAM_REGS 8
297 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
299 ainfo->offset = *stack_size;
301 if (*gr >= FLOAT_PARAM_REGS) {
302 ainfo->storage = ArgOnStack;
303 (*stack_size) += sizeof (gpointer);
306 /* A double register */
308 ainfo->storage = ArgInDoubleSSEReg;
310 ainfo->storage = ArgInFloatSSEReg;
316 typedef enum ArgumentClass {
324 merge_argument_class_from_type (MonoType *type, ArgumentClass class1)
326 ArgumentClass class2 = ARG_CLASS_NO_CLASS;
329 ptype = mono_type_get_underlying_type (type);
330 switch (ptype->type) {
331 case MONO_TYPE_BOOLEAN:
341 case MONO_TYPE_STRING:
342 case MONO_TYPE_OBJECT:
343 case MONO_TYPE_CLASS:
344 case MONO_TYPE_SZARRAY:
346 case MONO_TYPE_FNPTR:
347 case MONO_TYPE_ARRAY:
350 class2 = ARG_CLASS_INTEGER;
354 #ifdef PLATFORM_WIN32
355 class2 = ARG_CLASS_INTEGER;
357 class2 = ARG_CLASS_SSE;
361 case MONO_TYPE_TYPEDBYREF:
362 g_assert_not_reached ();
364 case MONO_TYPE_GENERICINST:
365 if (!mono_type_generic_inst_is_valuetype (ptype)) {
366 class2 = ARG_CLASS_INTEGER;
370 case MONO_TYPE_VALUETYPE: {
371 MonoMarshalType *info = mono_marshal_load_type_info (ptype->data.klass);
374 for (i = 0; i < info->num_fields; ++i) {
376 class2 = merge_argument_class_from_type (info->fields [i].field->type, class2);
381 g_assert_not_reached ();
385 if (class1 == class2)
387 else if (class1 == ARG_CLASS_NO_CLASS)
389 else if ((class1 == ARG_CLASS_MEMORY) || (class2 == ARG_CLASS_MEMORY))
390 class1 = ARG_CLASS_MEMORY;
391 else if ((class1 == ARG_CLASS_INTEGER) || (class2 == ARG_CLASS_INTEGER))
392 class1 = ARG_CLASS_INTEGER;
394 class1 = ARG_CLASS_SSE;
400 add_valuetype (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
402 guint32 *gr, guint32 *fr, guint32 *stack_size)
404 guint32 size, quad, nquads, i;
405 ArgumentClass args [2];
406 MonoMarshalType *info;
409 klass = mono_class_from_mono_type (type);
411 size = mono_type_native_stack_size (&klass->byval_arg, NULL);
413 size = mini_type_stack_size (gsctx, &klass->byval_arg, NULL);
414 #ifndef PLATFORM_WIN32
415 if (!sig->pinvoke && !disable_vtypes_in_regs && ((is_return && (size == 8)) || (!is_return && (size <= 16)))) {
416 /* We pass and return vtypes of size 8 in a register */
417 } else if (!sig->pinvoke || (size == 0) || (size > 16)) {
421 /* Allways pass in memory */
422 ainfo->offset = *stack_size;
423 *stack_size += ALIGN_TO (size, 8);
424 ainfo->storage = ArgOnStack;
429 /* FIXME: Handle structs smaller than 8 bytes */
430 //if ((size % 8) != 0)
439 /* Always pass in 1 or 2 integer registers */
440 args [0] = ARG_CLASS_INTEGER;
441 args [1] = ARG_CLASS_INTEGER;
442 /* Only the simplest cases are supported */
443 if (is_return && nquads != 1) {
444 args [0] = ARG_CLASS_MEMORY;
445 args [1] = ARG_CLASS_MEMORY;
449 * Implement the algorithm from section 3.2.3 of the X86_64 ABI.
450 * The X87 and SSEUP stuff is left out since there are no such types in
453 info = mono_marshal_load_type_info (klass);
456 #ifndef PLATFORM_WIN32
457 if (info->native_size > 16) {
458 ainfo->offset = *stack_size;
459 *stack_size += ALIGN_TO (info->native_size, 8);
460 ainfo->storage = ArgOnStack;
465 switch (info->native_size) {
466 case 1: case 2: case 4: case 8:
470 ainfo->storage = ArgOnStack;
471 ainfo->offset = *stack_size;
472 *stack_size += ALIGN_TO (info->native_size, 8);
475 ainfo->storage = ArgValuetypeAddrInIReg;
477 if (*gr < PARAM_REGS) {
478 ainfo->pair_storage [0] = ArgInIReg;
479 ainfo->pair_regs [0] = param_regs [*gr];
483 ainfo->pair_storage [0] = ArgOnStack;
484 ainfo->offset = *stack_size;
493 args [0] = ARG_CLASS_NO_CLASS;
494 args [1] = ARG_CLASS_NO_CLASS;
495 for (quad = 0; quad < nquads; ++quad) {
498 ArgumentClass class1;
500 if (info->num_fields == 0)
501 class1 = ARG_CLASS_MEMORY;
503 class1 = ARG_CLASS_NO_CLASS;
504 for (i = 0; i < info->num_fields; ++i) {
505 size = mono_marshal_type_size (info->fields [i].field->type,
506 info->fields [i].mspec,
507 &align, TRUE, klass->unicode);
508 if ((info->fields [i].offset < 8) && (info->fields [i].offset + size) > 8) {
509 /* Unaligned field */
513 /* Skip fields in other quad */
514 if ((quad == 0) && (info->fields [i].offset >= 8))
516 if ((quad == 1) && (info->fields [i].offset < 8))
519 class1 = merge_argument_class_from_type (info->fields [i].field->type, class1);
521 g_assert (class1 != ARG_CLASS_NO_CLASS);
522 args [quad] = class1;
526 /* Post merger cleanup */
527 if ((args [0] == ARG_CLASS_MEMORY) || (args [1] == ARG_CLASS_MEMORY))
528 args [0] = args [1] = ARG_CLASS_MEMORY;
530 /* Allocate registers */
535 ainfo->storage = ArgValuetypeInReg;
536 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
537 for (quad = 0; quad < nquads; ++quad) {
538 switch (args [quad]) {
539 case ARG_CLASS_INTEGER:
540 if (*gr >= PARAM_REGS)
541 args [quad] = ARG_CLASS_MEMORY;
543 ainfo->pair_storage [quad] = ArgInIReg;
545 ainfo->pair_regs [quad] = return_regs [*gr];
547 ainfo->pair_regs [quad] = param_regs [*gr];
552 if (*fr >= FLOAT_PARAM_REGS)
553 args [quad] = ARG_CLASS_MEMORY;
555 ainfo->pair_storage [quad] = ArgInDoubleSSEReg;
556 ainfo->pair_regs [quad] = *fr;
560 case ARG_CLASS_MEMORY:
563 g_assert_not_reached ();
567 if ((args [0] == ARG_CLASS_MEMORY) || (args [1] == ARG_CLASS_MEMORY)) {
568 /* Revert possible register assignments */
572 ainfo->offset = *stack_size;
574 *stack_size += ALIGN_TO (info->native_size, 8);
576 *stack_size += nquads * sizeof (gpointer);
577 ainfo->storage = ArgOnStack;
585 * Obtain information about a call according to the calling convention.
586 * For AMD64, see the "System V ABI, x86-64 Architecture Processor Supplement
587 * Draft Version 0.23" document for more information.
590 get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSignature *sig, gboolean is_pinvoke)
594 int n = sig->hasthis + sig->param_count;
595 guint32 stack_size = 0;
599 cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
601 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
608 ret_type = mono_type_get_underlying_type (sig->ret);
609 ret_type = mini_get_basic_type_from_generic (gsctx, ret_type);
610 switch (ret_type->type) {
611 case MONO_TYPE_BOOLEAN:
622 case MONO_TYPE_FNPTR:
623 case MONO_TYPE_CLASS:
624 case MONO_TYPE_OBJECT:
625 case MONO_TYPE_SZARRAY:
626 case MONO_TYPE_ARRAY:
627 case MONO_TYPE_STRING:
628 cinfo->ret.storage = ArgInIReg;
629 cinfo->ret.reg = AMD64_RAX;
633 cinfo->ret.storage = ArgInIReg;
634 cinfo->ret.reg = AMD64_RAX;
637 cinfo->ret.storage = ArgInFloatSSEReg;
638 cinfo->ret.reg = AMD64_XMM0;
641 cinfo->ret.storage = ArgInDoubleSSEReg;
642 cinfo->ret.reg = AMD64_XMM0;
644 case MONO_TYPE_GENERICINST:
645 if (!mono_type_generic_inst_is_valuetype (sig->ret)) {
646 cinfo->ret.storage = ArgInIReg;
647 cinfo->ret.reg = AMD64_RAX;
651 case MONO_TYPE_VALUETYPE: {
652 guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
654 add_valuetype (gsctx, sig, &cinfo->ret, sig->ret, TRUE, &tmp_gr, &tmp_fr, &tmp_stacksize);
655 if (cinfo->ret.storage == ArgOnStack)
656 /* The caller passes the address where the value is stored */
657 add_general (&gr, &stack_size, &cinfo->ret);
660 case MONO_TYPE_TYPEDBYREF:
661 /* Same as a valuetype with size 24 */
662 add_general (&gr, &stack_size, &cinfo->ret);
668 g_error ("Can't handle as return value 0x%x", sig->ret->type);
674 add_general (&gr, &stack_size, cinfo->args + 0);
676 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
678 fr = FLOAT_PARAM_REGS;
680 /* Emit the signature cookie just before the implicit arguments */
681 add_general (&gr, &stack_size, &cinfo->sig_cookie);
684 for (i = 0; i < sig->param_count; ++i) {
685 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
688 #ifdef PLATFORM_WIN32
689 /* The float param registers and other param registers must be the same index on Windows x64.*/
696 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
697 /* We allways pass the sig cookie on the stack for simplicity */
699 * Prevent implicit arguments + the sig cookie from being passed
703 fr = FLOAT_PARAM_REGS;
705 /* Emit the signature cookie just before the implicit arguments */
706 add_general (&gr, &stack_size, &cinfo->sig_cookie);
709 if (sig->params [i]->byref) {
710 add_general (&gr, &stack_size, ainfo);
713 ptype = mono_type_get_underlying_type (sig->params [i]);
714 ptype = mini_get_basic_type_from_generic (gsctx, ptype);
715 switch (ptype->type) {
716 case MONO_TYPE_BOOLEAN:
719 add_general (&gr, &stack_size, ainfo);
724 add_general (&gr, &stack_size, ainfo);
728 add_general (&gr, &stack_size, ainfo);
733 case MONO_TYPE_FNPTR:
734 case MONO_TYPE_CLASS:
735 case MONO_TYPE_OBJECT:
736 case MONO_TYPE_STRING:
737 case MONO_TYPE_SZARRAY:
738 case MONO_TYPE_ARRAY:
739 add_general (&gr, &stack_size, ainfo);
741 case MONO_TYPE_GENERICINST:
742 if (!mono_type_generic_inst_is_valuetype (ptype)) {
743 add_general (&gr, &stack_size, ainfo);
747 case MONO_TYPE_VALUETYPE:
748 add_valuetype (gsctx, sig, ainfo, sig->params [i], FALSE, &gr, &fr, &stack_size);
750 case MONO_TYPE_TYPEDBYREF:
751 #ifdef PLATFORM_WIN32
752 add_valuetype (gsctx, sig, ainfo, sig->params [i], FALSE, &gr, &fr, &stack_size);
754 stack_size += sizeof (MonoTypedRef);
755 ainfo->storage = ArgOnStack;
760 add_general (&gr, &stack_size, ainfo);
763 add_float (&fr, &stack_size, ainfo, FALSE);
766 add_float (&fr, &stack_size, ainfo, TRUE);
769 g_assert_not_reached ();
773 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
775 fr = FLOAT_PARAM_REGS;
777 /* Emit the signature cookie just before the implicit arguments */
778 add_general (&gr, &stack_size, &cinfo->sig_cookie);
781 #ifdef PLATFORM_WIN32
782 // There always is 32 bytes reserved on the stack when calling on Winx64
786 if (stack_size & 0x8) {
787 /* The AMD64 ABI requires each stack frame to be 16 byte aligned */
788 cinfo->need_stack_align = TRUE;
792 cinfo->stack_usage = stack_size;
793 cinfo->reg_usage = gr;
794 cinfo->freg_usage = fr;
799 * mono_arch_get_argument_info:
800 * @csig: a method signature
801 * @param_count: the number of parameters to consider
802 * @arg_info: an array to store the result infos
804 * Gathers information on parameters such as size, alignment and
805 * padding. arg_info should be large enought to hold param_count + 1 entries.
807 * Returns the size of the argument area on the stack.
810 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
813 CallInfo *cinfo = get_call_info (NULL, NULL, csig, FALSE);
814 guint32 args_size = cinfo->stack_usage;
816 /* The arguments are saved to a stack area in mono_arch_instrument_prolog */
818 arg_info [0].offset = 0;
821 for (k = 0; k < param_count; k++) {
822 arg_info [k + 1].offset = ((k + csig->hasthis) * 8);
824 arg_info [k + 1].size = 0;
833 cpuid (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx)
836 __asm__ __volatile__ ("cpuid"
837 : "=a" (*p_eax), "=b" (*p_ebx), "=c" (*p_ecx), "=d" (*p_edx)
851 * Initialize the cpu to execute managed code.
854 mono_arch_cpu_init (void)
859 /* spec compliance requires running with double precision */
860 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
861 fpcw &= ~X86_FPCW_PRECC_MASK;
862 fpcw |= X86_FPCW_PREC_DOUBLE;
863 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
864 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
866 /* TODO: This is crashing on Win64 right now.
867 * _control87 (_PC_53, MCW_PC);
873 * Initialize architecture specific code.
876 mono_arch_init (void)
878 InitializeCriticalSection (&mini_arch_mutex);
882 * Cleanup architecture specific code.
885 mono_arch_cleanup (void)
887 DeleteCriticalSection (&mini_arch_mutex);
891 * This function returns the optimizations supported on this cpu.
894 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
896 int eax, ebx, ecx, edx;
902 /* Feature Flags function, flags returned in EDX. */
903 if (cpuid (1, &eax, &ebx, &ecx, &edx)) {
904 if (edx & (1 << 15)) {
905 opts |= MONO_OPT_CMOV;
907 opts |= MONO_OPT_FCMOV;
909 *exclude_mask |= MONO_OPT_FCMOV;
911 *exclude_mask |= MONO_OPT_CMOV;
913 #ifdef PLATFORM_WIN32
915 *exclude_mask |= (MONO_OPT_PEEPHOLE | MONO_OPT_BRANCH);
921 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
926 for (i = 0; i < cfg->num_varinfo; i++) {
927 MonoInst *ins = cfg->varinfo [i];
928 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
931 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
934 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
935 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
938 if (mono_is_regsize_var (ins->inst_vtype)) {
939 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
940 g_assert (i == vmv->idx);
941 vars = g_list_prepend (vars, vmv);
945 vars = mono_varlist_sort (cfg, vars, 0);
951 * mono_arch_compute_omit_fp:
953 * Determine whenever the frame pointer can be eliminated.
956 mono_arch_compute_omit_fp (MonoCompile *cfg)
958 MonoMethodSignature *sig;
959 MonoMethodHeader *header;
963 if (cfg->arch.omit_fp_computed)
966 header = mono_method_get_header (cfg->method);
968 sig = mono_method_signature (cfg->method);
970 if (!cfg->arch.cinfo)
971 cfg->arch.cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, FALSE);
972 cinfo = cfg->arch.cinfo;
975 * FIXME: Remove some of the restrictions.
977 cfg->arch.omit_fp = TRUE;
978 cfg->arch.omit_fp_computed = TRUE;
980 if (cfg->disable_omit_fp)
981 cfg->arch.omit_fp = FALSE;
983 if (!debug_omit_fp ())
984 cfg->arch.omit_fp = FALSE;
986 if (cfg->method->save_lmf)
987 cfg->arch.omit_fp = FALSE;
989 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
990 cfg->arch.omit_fp = FALSE;
991 if (header->num_clauses)
992 cfg->arch.omit_fp = FALSE;
994 cfg->arch.omit_fp = FALSE;
995 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
996 cfg->arch.omit_fp = FALSE;
997 if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) ||
998 (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
999 cfg->arch.omit_fp = FALSE;
1000 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1001 ArgInfo *ainfo = &cinfo->args [i];
1003 if (ainfo->storage == ArgOnStack) {
1005 * The stack offset can only be determined when the frame
1008 cfg->arch.omit_fp = FALSE;
1013 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1014 MonoInst *ins = cfg->varinfo [i];
1017 locals_size += mono_type_size (ins->inst_vtype, &ialign);
1020 if ((cfg->num_varinfo > 10000) || (locals_size >= (1 << 15))) {
1021 /* Avoid hitting the stack_alloc_size < (1 << 16) assertion in emit_epilog () */
1022 cfg->arch.omit_fp = FALSE;
1027 mono_arch_get_global_int_regs (MonoCompile *cfg)
1031 mono_arch_compute_omit_fp (cfg);
1033 if (cfg->globalra) {
1034 if (cfg->arch.omit_fp)
1035 regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
1037 regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
1038 regs = g_list_prepend (regs, (gpointer)AMD64_R12);
1039 regs = g_list_prepend (regs, (gpointer)AMD64_R13);
1040 regs = g_list_prepend (regs, (gpointer)AMD64_R14);
1041 regs = g_list_prepend (regs, (gpointer)AMD64_R15);
1043 regs = g_list_prepend (regs, (gpointer)AMD64_R10);
1044 regs = g_list_prepend (regs, (gpointer)AMD64_R9);
1045 regs = g_list_prepend (regs, (gpointer)AMD64_R8);
1046 regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
1047 regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
1048 regs = g_list_prepend (regs, (gpointer)AMD64_RDX);
1049 regs = g_list_prepend (regs, (gpointer)AMD64_RCX);
1050 regs = g_list_prepend (regs, (gpointer)AMD64_RAX);
1052 if (cfg->arch.omit_fp)
1053 regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
1055 /* We use the callee saved registers for global allocation */
1056 regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
1057 regs = g_list_prepend (regs, (gpointer)AMD64_R12);
1058 regs = g_list_prepend (regs, (gpointer)AMD64_R13);
1059 regs = g_list_prepend (regs, (gpointer)AMD64_R14);
1060 regs = g_list_prepend (regs, (gpointer)AMD64_R15);
1067 mono_arch_get_global_fp_regs (MonoCompile *cfg)
1072 /* All XMM registers */
1073 for (i = 0; i < 16; ++i)
1074 regs = g_list_prepend (regs, GINT_TO_POINTER (i));
1080 mono_arch_get_iregs_clobbered_by_call (MonoCallInst *call)
1082 static GList *r = NULL;
1087 regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
1088 regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
1089 regs = g_list_prepend (regs, (gpointer)AMD64_R12);
1090 regs = g_list_prepend (regs, (gpointer)AMD64_R13);
1091 regs = g_list_prepend (regs, (gpointer)AMD64_R14);
1092 regs = g_list_prepend (regs, (gpointer)AMD64_R15);
1094 regs = g_list_prepend (regs, (gpointer)AMD64_R10);
1095 regs = g_list_prepend (regs, (gpointer)AMD64_R9);
1096 regs = g_list_prepend (regs, (gpointer)AMD64_R8);
1097 regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
1098 regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
1099 regs = g_list_prepend (regs, (gpointer)AMD64_RDX);
1100 regs = g_list_prepend (regs, (gpointer)AMD64_RCX);
1101 regs = g_list_prepend (regs, (gpointer)AMD64_RAX);
1103 InterlockedCompareExchangePointer ((gpointer*)&r, regs, NULL);
1110 mono_arch_get_fregs_clobbered_by_call (MonoCallInst *call)
1113 static GList *r = NULL;
1118 for (i = 0; i < AMD64_XMM_NREG; ++i)
1119 regs = g_list_prepend (regs, GINT_TO_POINTER (MONO_MAX_IREGS + i));
1121 InterlockedCompareExchangePointer ((gpointer*)&r, regs, NULL);
1128 * mono_arch_regalloc_cost:
1130 * Return the cost, in number of memory references, of the action of
1131 * allocating the variable VMV into a register during global register
1135 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
1137 MonoInst *ins = cfg->varinfo [vmv->idx];
1139 if (cfg->method->save_lmf)
1140 /* The register is already saved */
1141 /* substract 1 for the invisible store in the prolog */
1142 return (ins->opcode == OP_ARG) ? 0 : 1;
1145 return (ins->opcode == OP_ARG) ? 1 : 2;
1149 * mono_arch_fill_argument_info:
1151 * Populate cfg->args, cfg->ret and cfg->vret_addr with information about the arguments
1155 mono_arch_fill_argument_info (MonoCompile *cfg)
1157 MonoMethodSignature *sig;
1158 MonoMethodHeader *header;
1163 header = mono_method_get_header (cfg->method);
1165 sig = mono_method_signature (cfg->method);
1167 cinfo = cfg->arch.cinfo;
1170 * Contrary to mono_arch_allocate_vars (), the information should describe
1171 * where the arguments are at the beginning of the method, not where they can be
1172 * accessed during the execution of the method. The later makes no sense for the
1173 * global register allocator, since a variable can be in more than one location.
1175 if (sig->ret->type != MONO_TYPE_VOID) {
1176 switch (cinfo->ret.storage) {
1178 case ArgInFloatSSEReg:
1179 case ArgInDoubleSSEReg:
1180 if ((MONO_TYPE_ISSTRUCT (sig->ret) && !mono_class_from_mono_type (sig->ret)->enumtype) || (sig->ret->type == MONO_TYPE_TYPEDBYREF)) {
1181 cfg->vret_addr->opcode = OP_REGVAR;
1182 cfg->vret_addr->inst_c0 = cinfo->ret.reg;
1185 cfg->ret->opcode = OP_REGVAR;
1186 cfg->ret->inst_c0 = cinfo->ret.reg;
1189 case ArgValuetypeInReg:
1190 cfg->ret->opcode = OP_REGOFFSET;
1191 cfg->ret->inst_basereg = -1;
1192 cfg->ret->inst_offset = -1;
1195 g_assert_not_reached ();
1199 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1200 ArgInfo *ainfo = &cinfo->args [i];
1203 ins = cfg->args [i];
1205 if (sig->hasthis && (i == 0))
1206 arg_type = &mono_defaults.object_class->byval_arg;
1208 arg_type = sig->params [i - sig->hasthis];
1210 switch (ainfo->storage) {
1212 case ArgInFloatSSEReg:
1213 case ArgInDoubleSSEReg:
1214 ins->opcode = OP_REGVAR;
1215 ins->inst_c0 = ainfo->reg;
1218 ins->opcode = OP_REGOFFSET;
1219 ins->inst_basereg = -1;
1220 ins->inst_offset = -1;
1222 case ArgValuetypeInReg:
1224 ins->opcode = OP_NOP;
1227 g_assert_not_reached ();
1233 mono_arch_allocate_vars (MonoCompile *cfg)
1235 MonoMethodSignature *sig;
1236 MonoMethodHeader *header;
1239 guint32 locals_stack_size, locals_stack_align;
1243 header = mono_method_get_header (cfg->method);
1245 sig = mono_method_signature (cfg->method);
1247 cinfo = cfg->arch.cinfo;
1249 mono_arch_compute_omit_fp (cfg);
1252 * We use the ABI calling conventions for managed code as well.
1253 * Exception: valuetypes are never passed or returned in registers.
1256 if (cfg->arch.omit_fp) {
1257 cfg->flags |= MONO_CFG_HAS_SPILLUP;
1258 cfg->frame_reg = AMD64_RSP;
1261 /* Locals are allocated backwards from %fp */
1262 cfg->frame_reg = AMD64_RBP;
1266 if (cfg->method->save_lmf) {
1267 /* Reserve stack space for saving LMF */
1268 /* mono_arch_find_jit_info () expects to find the LMF at a fixed offset */
1269 g_assert (offset == 0);
1270 if (cfg->arch.omit_fp) {
1271 cfg->arch.lmf_offset = offset;
1272 offset += sizeof (MonoLMF);
1275 offset += sizeof (MonoLMF);
1276 cfg->arch.lmf_offset = -offset;
1279 if (cfg->arch.omit_fp)
1280 cfg->arch.reg_save_area_offset = offset;
1281 /* Reserve space for caller saved registers */
1282 for (i = 0; i < AMD64_NREG; ++i)
1283 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
1284 offset += sizeof (gpointer);
1288 if (sig->ret->type != MONO_TYPE_VOID) {
1289 switch (cinfo->ret.storage) {
1291 case ArgInFloatSSEReg:
1292 case ArgInDoubleSSEReg:
1293 if ((MONO_TYPE_ISSTRUCT (sig->ret) && !mono_class_from_mono_type (sig->ret)->enumtype) || (sig->ret->type == MONO_TYPE_TYPEDBYREF)) {
1294 if (cfg->globalra) {
1295 cfg->vret_addr->opcode = OP_REGVAR;
1296 cfg->vret_addr->inst_c0 = cinfo->ret.reg;
1298 /* The register is volatile */
1299 cfg->vret_addr->opcode = OP_REGOFFSET;
1300 cfg->vret_addr->inst_basereg = cfg->frame_reg;
1301 if (cfg->arch.omit_fp) {
1302 cfg->vret_addr->inst_offset = offset;
1306 cfg->vret_addr->inst_offset = -offset;
1308 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1309 printf ("vret_addr =");
1310 mono_print_ins (cfg->vret_addr);
1315 cfg->ret->opcode = OP_REGVAR;
1316 cfg->ret->inst_c0 = cinfo->ret.reg;
1319 case ArgValuetypeInReg:
1320 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
1321 cfg->ret->opcode = OP_REGOFFSET;
1322 cfg->ret->inst_basereg = cfg->frame_reg;
1323 if (cfg->arch.omit_fp) {
1324 cfg->ret->inst_offset = offset;
1328 cfg->ret->inst_offset = - offset;
1332 g_assert_not_reached ();
1335 cfg->ret->dreg = cfg->ret->inst_c0;
1338 /* Allocate locals */
1339 if (!cfg->globalra) {
1340 offsets = mono_allocate_stack_slots_full (cfg, cfg->arch.omit_fp ? FALSE: TRUE, &locals_stack_size, &locals_stack_align);
1341 if (locals_stack_align) {
1342 offset += (locals_stack_align - 1);
1343 offset &= ~(locals_stack_align - 1);
1345 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1346 if (offsets [i] != -1) {
1347 MonoInst *ins = cfg->varinfo [i];
1348 ins->opcode = OP_REGOFFSET;
1349 ins->inst_basereg = cfg->frame_reg;
1350 if (cfg->arch.omit_fp)
1351 ins->inst_offset = (offset + offsets [i]);
1353 ins->inst_offset = - (offset + offsets [i]);
1354 //printf ("allocated local %d to ", i); mono_print_tree_nl (ins);
1357 offset += locals_stack_size;
1360 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG)) {
1361 g_assert (!cfg->arch.omit_fp);
1362 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
1363 cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
1366 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1367 ins = cfg->args [i];
1368 if (ins->opcode != OP_REGVAR) {
1369 ArgInfo *ainfo = &cinfo->args [i];
1370 gboolean inreg = TRUE;
1373 if (sig->hasthis && (i == 0))
1374 arg_type = &mono_defaults.object_class->byval_arg;
1376 arg_type = sig->params [i - sig->hasthis];
1378 if (cfg->globalra) {
1379 /* The new allocator needs info about the original locations of the arguments */
1380 switch (ainfo->storage) {
1382 case ArgInFloatSSEReg:
1383 case ArgInDoubleSSEReg:
1384 ins->opcode = OP_REGVAR;
1385 ins->inst_c0 = ainfo->reg;
1388 g_assert (!cfg->arch.omit_fp);
1389 ins->opcode = OP_REGOFFSET;
1390 ins->inst_basereg = cfg->frame_reg;
1391 ins->inst_offset = ainfo->offset + ARGS_OFFSET;
1393 case ArgValuetypeInReg:
1394 ins->opcode = OP_REGOFFSET;
1395 ins->inst_basereg = cfg->frame_reg;
1396 /* These arguments are saved to the stack in the prolog */
1397 offset = ALIGN_TO (offset, sizeof (gpointer));
1398 if (cfg->arch.omit_fp) {
1399 ins->inst_offset = offset;
1400 offset += (ainfo->storage == ArgValuetypeInReg) ? 2 * sizeof (gpointer) : sizeof (gpointer);
1402 offset += (ainfo->storage == ArgValuetypeInReg) ? 2 * sizeof (gpointer) : sizeof (gpointer);
1403 ins->inst_offset = - offset;
1407 g_assert_not_reached ();
1413 /* FIXME: Allocate volatile arguments to registers */
1414 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
1418 * Under AMD64, all registers used to pass arguments to functions
1419 * are volatile across calls.
1420 * FIXME: Optimize this.
1422 if ((ainfo->storage == ArgInIReg) || (ainfo->storage == ArgInFloatSSEReg) || (ainfo->storage == ArgInDoubleSSEReg) || (ainfo->storage == ArgValuetypeInReg))
1425 ins->opcode = OP_REGOFFSET;
1427 switch (ainfo->storage) {
1429 case ArgInFloatSSEReg:
1430 case ArgInDoubleSSEReg:
1432 ins->opcode = OP_REGVAR;
1433 ins->dreg = ainfo->reg;
1437 g_assert (!cfg->arch.omit_fp);
1438 ins->opcode = OP_REGOFFSET;
1439 ins->inst_basereg = cfg->frame_reg;
1440 ins->inst_offset = ainfo->offset + ARGS_OFFSET;
1442 case ArgValuetypeInReg:
1444 case ArgValuetypeAddrInIReg: {
1446 g_assert (!cfg->arch.omit_fp);
1448 MONO_INST_NEW (cfg, indir, 0);
1449 indir->opcode = OP_REGOFFSET;
1450 if (ainfo->pair_storage [0] == ArgInIReg) {
1451 indir->inst_basereg = cfg->frame_reg;
1452 offset = ALIGN_TO (offset, sizeof (gpointer));
1453 offset += (sizeof (gpointer));
1454 indir->inst_offset = - offset;
1457 indir->inst_basereg = cfg->frame_reg;
1458 indir->inst_offset = ainfo->offset + ARGS_OFFSET;
1461 ins->opcode = OP_VTARG_ADDR;
1462 ins->inst_left = indir;
1470 if (!inreg && (ainfo->storage != ArgOnStack) && (ainfo->storage != ArgValuetypeAddrInIReg)) {
1471 ins->opcode = OP_REGOFFSET;
1472 ins->inst_basereg = cfg->frame_reg;
1473 /* These arguments are saved to the stack in the prolog */
1474 offset = ALIGN_TO (offset, sizeof (gpointer));
1475 if (cfg->arch.omit_fp) {
1476 ins->inst_offset = offset;
1477 offset += (ainfo->storage == ArgValuetypeInReg) ? 2 * sizeof (gpointer) : sizeof (gpointer);
1479 offset += (ainfo->storage == ArgValuetypeInReg) ? 2 * sizeof (gpointer) : sizeof (gpointer);
1480 ins->inst_offset = - offset;
1486 cfg->stack_offset = offset;
1490 mono_arch_create_vars (MonoCompile *cfg)
1492 MonoMethodSignature *sig;
1495 sig = mono_method_signature (cfg->method);
1497 if (!cfg->arch.cinfo)
1498 cfg->arch.cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, FALSE);
1499 cinfo = cfg->arch.cinfo;
1501 if (cinfo->ret.storage == ArgValuetypeInReg)
1502 cfg->ret_var_is_local = TRUE;
1504 if ((cinfo->ret.storage != ArgValuetypeInReg) && MONO_TYPE_ISSTRUCT (sig->ret)) {
1505 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
1506 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1507 printf ("vret_addr = ");
1508 mono_print_ins (cfg->vret_addr);
1514 add_outarg_reg (MonoCompile *cfg, MonoCallInst *call, MonoInst *arg, ArgStorage storage, int reg, MonoInst *tree)
1518 arg->opcode = OP_OUTARG_REG;
1519 arg->inst_left = tree;
1520 arg->inst_call = call;
1521 arg->backend.reg3 = reg;
1523 case ArgInFloatSSEReg:
1524 arg->opcode = OP_AMD64_OUTARG_XMMREG_R4;
1525 arg->inst_left = tree;
1526 arg->inst_call = call;
1527 arg->backend.reg3 = reg;
1529 case ArgInDoubleSSEReg:
1530 arg->opcode = OP_AMD64_OUTARG_XMMREG_R8;
1531 arg->inst_left = tree;
1532 arg->inst_call = call;
1533 arg->backend.reg3 = reg;
1536 g_assert_not_reached ();
1541 add_outarg_reg2 (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int reg, MonoInst *tree)
1547 MONO_INST_NEW (cfg, ins, OP_MOVE);
1548 ins->dreg = mono_alloc_ireg (cfg);
1549 ins->sreg1 = tree->dreg;
1550 MONO_ADD_INS (cfg->cbb, ins);
1551 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, FALSE);
1553 case ArgInFloatSSEReg:
1554 MONO_INST_NEW (cfg, ins, OP_AMD64_SET_XMMREG_R4);
1555 ins->dreg = mono_alloc_freg (cfg);
1556 ins->sreg1 = tree->dreg;
1557 MONO_ADD_INS (cfg->cbb, ins);
1559 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
1561 case ArgInDoubleSSEReg:
1562 MONO_INST_NEW (cfg, ins, OP_FMOVE);
1563 ins->dreg = mono_alloc_freg (cfg);
1564 ins->sreg1 = tree->dreg;
1565 MONO_ADD_INS (cfg->cbb, ins);
1567 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
1571 g_assert_not_reached ();
1576 arg_storage_to_ldind (ArgStorage storage)
1581 case ArgInDoubleSSEReg:
1582 return CEE_LDIND_R8;
1583 case ArgInFloatSSEReg:
1584 return CEE_LDIND_R4;
1586 g_assert_not_reached ();
1593 arg_storage_to_load_membase (ArgStorage storage)
1597 return OP_LOAD_MEMBASE;
1598 case ArgInDoubleSSEReg:
1599 return OP_LOADR8_MEMBASE;
1600 case ArgInFloatSSEReg:
1601 return OP_LOADR4_MEMBASE;
1603 g_assert_not_reached ();
1610 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
1613 MonoMethodSignature *tmp_sig;
1616 /* FIXME: Add support for signature tokens to AOT */
1617 cfg->disable_aot = TRUE;
1619 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
1622 * mono_ArgIterator_Setup assumes the signature cookie is
1623 * passed first and all the arguments which were before it are
1624 * passed on the stack after the signature. So compensate by
1625 * passing a different signature.
1627 tmp_sig = mono_metadata_signature_dup (call->signature);
1628 tmp_sig->param_count -= call->signature->sentinelpos;
1629 tmp_sig->sentinelpos = 0;
1630 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
1632 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
1633 sig_arg->inst_p0 = tmp_sig;
1635 MONO_INST_NEW (cfg, arg, OP_OUTARG);
1636 arg->inst_left = sig_arg;
1637 arg->type = STACK_PTR;
1639 /* prepend, so they get reversed */
1640 arg->next = call->out_args;
1641 call->out_args = arg;
1645 * take the arguments and generate the arch-specific
1646 * instructions to properly call the function in call.
1647 * This includes pushing, moving arguments to the right register
1651 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
1653 MonoMethodSignature *sig;
1654 int i, n, stack_size;
1660 sig = call->signature;
1661 n = sig->param_count + sig->hasthis;
1663 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, sig->pinvoke);
1665 if (cfg->method->save_lmf) {
1666 MONO_INST_NEW (cfg, arg, OP_AMD64_SAVE_SP_TO_LMF);
1667 arg->next = call->out_args;
1668 call->out_args = arg;
1671 for (i = 0; i < n; ++i) {
1672 ainfo = cinfo->args + i;
1674 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1675 /* Emit the signature cookie just before the implicit arguments */
1676 emit_sig_cookie (cfg, call, cinfo);
1679 if (is_virtual && i == 0) {
1680 /* the argument will be attached to the call instruction */
1681 in = call->args [i];
1683 MONO_INST_NEW (cfg, arg, OP_OUTARG);
1684 in = call->args [i];
1685 arg->cil_code = in->cil_code;
1686 arg->inst_left = in;
1687 arg->type = in->type;
1688 /* prepend, so they get reversed */
1689 arg->next = call->out_args;
1690 call->out_args = arg;
1692 if (!cinfo->stack_usage)
1693 /* Keep the assignments to the arg registers in order if possible */
1694 MONO_INST_LIST_ADD_TAIL (&arg->node, &call->out_args);
1696 MONO_INST_LIST_ADD (&arg->node, &call->out_args);
1699 if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(sig->params [i - sig->hasthis]))) {
1703 if (sig->params [i - sig->hasthis]->type == MONO_TYPE_TYPEDBYREF) {
1704 size = sizeof (MonoTypedRef);
1705 align = sizeof (gpointer);
1709 size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
1712 * Other backends use mini_type_stack_size (), but that
1713 * aligns the size to 8, which is larger than the size of
1714 * the source, leading to reads of invalid memory if the
1715 * source is at the end of address space.
1717 size = mono_class_value_size (in->klass, &align);
1719 if (ainfo->storage == ArgValuetypeInReg) {
1720 if (ainfo->pair_storage [1] == ArgNone) {
1725 MONO_INST_NEW (cfg, load, arg_storage_to_ldind (ainfo->pair_storage [0]));
1726 load->inst_left = in;
1728 add_outarg_reg (cfg, call, arg, ainfo->pair_storage [0], ainfo->pair_regs [0], load);
1731 /* Trees can't be shared so make a copy */
1732 MonoInst *vtaddr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1733 MonoInst *load, *load2, *offset_ins;
1736 MONO_INST_NEW (cfg, load, CEE_LDIND_I);
1737 load->ssa_op = MONO_SSA_LOAD;
1738 load->inst_i0 = (cfg)->varinfo [vtaddr->inst_c0];
1740 NEW_ICONST (cfg, offset_ins, 0);
1741 MONO_INST_NEW (cfg, load2, CEE_ADD);
1742 load2->inst_left = load;
1743 load2->inst_right = offset_ins;
1745 MONO_INST_NEW (cfg, load, arg_storage_to_ldind (ainfo->pair_storage [0]));
1746 load->inst_left = load2;
1748 add_outarg_reg (cfg, call, arg, ainfo->pair_storage [0], ainfo->pair_regs [0], load);
1751 MONO_INST_NEW (cfg, load, CEE_LDIND_I);
1752 load->ssa_op = MONO_SSA_LOAD;
1753 load->inst_i0 = (cfg)->varinfo [vtaddr->inst_c0];
1755 NEW_ICONST (cfg, offset_ins, 8);
1756 MONO_INST_NEW (cfg, load2, CEE_ADD);
1757 load2->inst_left = load;
1758 load2->inst_right = offset_ins;
1760 MONO_INST_NEW (cfg, load, arg_storage_to_ldind (ainfo->pair_storage [1]));
1761 load->inst_left = load2;
1763 MONO_INST_NEW (cfg, arg, OP_OUTARG);
1764 arg->cil_code = in->cil_code;
1765 arg->type = in->type;
1766 /* prepend, so they get reversed */
1767 arg->next = call->out_args;
1768 call->out_args = arg;
1770 add_outarg_reg (cfg, call, arg, ainfo->pair_storage [1], ainfo->pair_regs [1], load);
1772 /* Prepend a copy inst */
1773 MONO_INST_NEW (cfg, arg, CEE_STIND_I);
1774 arg->cil_code = in->cil_code;
1775 arg->ssa_op = MONO_SSA_STORE;
1776 arg->inst_left = vtaddr;
1777 arg->inst_right = in;
1778 arg->type = in->type;
1780 /* prepend, so they get reversed */
1781 arg->next = call->out_args;
1782 call->out_args = arg;
1785 else if (ainfo->storage == ArgValuetypeAddrInIReg){
1787 /* Add a temp variable to the method*/
1789 MonoInst *vtaddr = mono_compile_create_var (cfg, &in->klass->byval_arg, OP_LOCAL);
1791 MONO_INST_NEW (cfg, load, OP_LDADDR);
1792 load->ssa_op = MONO_SSA_LOAD;
1793 load->inst_left = vtaddr;
1795 if (ainfo->pair_storage [0] == ArgInIReg) {
1796 /* Inserted after the copy. Load the address of the temp to the argument regster.*/
1797 arg->opcode = OP_OUTARG_REG;
1798 arg->inst_left = load;
1799 arg->inst_call = call;
1800 arg->backend.reg3 = ainfo->pair_regs [0];
1803 /* Inserted after the copy. Load the address of the temp on the stack.*/
1804 arg->opcode = OP_OUTARG_VT;
1805 arg->inst_left = load;
1806 arg->type = STACK_PTR;
1807 arg->klass = mono_defaults.int_class;
1808 arg->backend.is_pinvoke = sig->pinvoke;
1809 arg->inst_imm = size;
1812 /*Copy the argument to the temp variable.*/
1813 MONO_INST_NEW (cfg, load, OP_MEMCPY);
1814 load->backend.memcpy_args = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMemcpyArgs));
1815 load->backend.memcpy_args->size = size;
1816 load->backend.memcpy_args->align = align;
1817 load->inst_left = (cfg)->varinfo [vtaddr->inst_c0];
1818 load->inst_right = in->inst_i0;
1821 g_assert_not_reached ();
1822 //MONO_INST_LIST_ADD (&load->node, &call->out_args);
1825 arg->opcode = OP_OUTARG_VT;
1826 arg->klass = in->klass;
1827 arg->backend.is_pinvoke = sig->pinvoke;
1828 arg->inst_imm = size;
1832 switch (ainfo->storage) {
1834 add_outarg_reg (cfg, call, arg, ainfo->storage, ainfo->reg, in);
1836 case ArgInFloatSSEReg:
1837 case ArgInDoubleSSEReg:
1838 add_outarg_reg (cfg, call, arg, ainfo->storage, ainfo->reg, in);
1841 arg->opcode = OP_OUTARG;
1842 if (!sig->params [i - sig->hasthis]->byref) {
1843 if (sig->params [i - sig->hasthis]->type == MONO_TYPE_R4)
1844 arg->opcode = OP_OUTARG_R4;
1846 if (sig->params [i - sig->hasthis]->type == MONO_TYPE_R8)
1847 arg->opcode = OP_OUTARG_R8;
1851 g_assert_not_reached ();
1857 /* Handle the case where there are no implicit arguments */
1858 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos)) {
1859 emit_sig_cookie (cfg, call, cinfo);
1862 if (cinfo->ret.storage == ArgValuetypeInReg) {
1863 /* This is needed by mono_arch_emit_this_vret_args () */
1864 if (!cfg->arch.vret_addr_loc) {
1865 cfg->arch.vret_addr_loc = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1866 /* Prevent it from being register allocated or optimized away */
1867 ((MonoInst*)cfg->arch.vret_addr_loc)->flags |= MONO_INST_VOLATILE;
1871 if (cinfo->need_stack_align) {
1872 MONO_INST_NEW (cfg, arg, OP_AMD64_OUTARG_ALIGN_STACK);
1874 /* prepend, so they get reversed */
1875 arg->next = call->out_args;
1876 call->out_args = arg;
1879 #ifdef PLATFORM_WIN32
1880 /* Always reserve 32 bytes of stack space on Win64 */
1881 /*MONO_INST_NEW (cfg, arg, OP_AMD64_OUTARG_ALIGN_STACK);
1883 MONO_INST_LIST_ADD_TAIL (&arg->node, &call->out_args);*/
1888 if (cfg->method->save_lmf) {
1889 MONO_INST_NEW (cfg, arg, OP_AMD64_SAVE_SP_TO_LMF);
1890 MONO_INST_LIST_ADD_TAIL (&arg->node, &call->out_args);
1894 call->stack_usage = cinfo->stack_usage;
1895 cfg->param_area = MAX (cfg->param_area, call->stack_usage);
1896 cfg->flags |= MONO_CFG_HAS_CALLS;
1902 emit_sig_cookie2 (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
1905 MonoMethodSignature *tmp_sig;
1908 if (call->tail_call)
1911 /* FIXME: Add support for signature tokens to AOT */
1912 cfg->disable_aot = TRUE;
1914 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
1917 * mono_ArgIterator_Setup assumes the signature cookie is
1918 * passed first and all the arguments which were before it are
1919 * passed on the stack after the signature. So compensate by
1920 * passing a different signature.
1922 tmp_sig = mono_metadata_signature_dup (call->signature);
1923 tmp_sig->param_count -= call->signature->sentinelpos;
1924 tmp_sig->sentinelpos = 0;
1925 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
1927 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
1928 sig_arg->dreg = mono_alloc_ireg (cfg);
1929 sig_arg->inst_p0 = tmp_sig;
1930 MONO_ADD_INS (cfg->cbb, sig_arg);
1932 MONO_INST_NEW (cfg, arg, OP_X86_PUSH);
1933 arg->sreg1 = sig_arg->dreg;
1934 MONO_ADD_INS (cfg->cbb, arg);
1938 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
1941 MonoMethodSignature *sig;
1942 int i, n, stack_size;
1948 sig = call->signature;
1949 n = sig->param_count + sig->hasthis;
1951 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, sig->pinvoke);
1953 if (cinfo->need_stack_align) {
1954 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, 8);
1958 * Emit all parameters passed in registers in non-reverse order for better readability
1959 * and to help the optimization in emit_prolog ().
1961 for (i = 0; i < n; ++i) {
1962 ainfo = cinfo->args + i;
1964 in = call->args [i];
1966 if (ainfo->storage == ArgInIReg)
1967 add_outarg_reg2 (cfg, call, ainfo->storage, ainfo->reg, in);
1970 for (i = n - 1; i >= 0; --i) {
1971 ainfo = cinfo->args + i;
1973 in = call->args [i];
1975 switch (ainfo->storage) {
1979 case ArgInFloatSSEReg:
1980 case ArgInDoubleSSEReg:
1981 add_outarg_reg2 (cfg, call, ainfo->storage, ainfo->reg, in);
1984 case ArgValuetypeInReg:
1985 case ArgValuetypeAddrInIReg:
1986 if (ainfo->storage == ArgOnStack && call->tail_call)
1988 if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(sig->params [i - sig->hasthis]))) {
1992 if (sig->params [i - sig->hasthis]->type == MONO_TYPE_TYPEDBYREF) {
1993 size = sizeof (MonoTypedRef);
1994 align = sizeof (gpointer);
1998 size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
2001 * Other backends use mono_type_stack_size (), but that
2002 * aligns the size to 8, which is larger than the size of
2003 * the source, leading to reads of invalid memory if the
2004 * source is at the end of address space.
2006 size = mono_class_value_size (in->klass, &align);
2009 g_assert (in->klass);
2012 MONO_INST_NEW (cfg, arg, OP_OUTARG_VT);
2013 arg->sreg1 = in->dreg;
2014 arg->klass = in->klass;
2015 arg->backend.size = size;
2016 arg->inst_p0 = call;
2017 arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
2018 memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
2020 MONO_ADD_INS (cfg->cbb, arg);
2023 MONO_INST_NEW (cfg, arg, OP_X86_PUSH);
2024 arg->sreg1 = in->dreg;
2025 if (!sig->params [i - sig->hasthis]->byref) {
2026 if (sig->params [i - sig->hasthis]->type == MONO_TYPE_R4) {
2027 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, 8);
2028 arg->opcode = OP_STORER4_MEMBASE_REG;
2029 arg->inst_destbasereg = X86_ESP;
2030 arg->inst_offset = 0;
2031 } else if (sig->params [i - sig->hasthis]->type == MONO_TYPE_R8) {
2032 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, 8);
2033 arg->opcode = OP_STORER8_MEMBASE_REG;
2034 arg->inst_destbasereg = X86_ESP;
2035 arg->inst_offset = 0;
2038 MONO_ADD_INS (cfg->cbb, arg);
2042 g_assert_not_reached ();
2045 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
2046 /* Emit the signature cookie just before the implicit arguments */
2047 emit_sig_cookie2 (cfg, call, cinfo);
2051 /* Handle the case where there are no implicit arguments */
2052 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos)) {
2053 emit_sig_cookie2 (cfg, call, cinfo);
2056 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
2059 if (cinfo->ret.storage == ArgValuetypeInReg) {
2060 if (cinfo->ret.pair_storage [0] == ArgInIReg && cinfo->ret.pair_storage [1] == ArgNone) {
2062 * Tell the JIT to use a more efficient calling convention: call using
2063 * OP_CALL, compute the result location after the call, and save the
2066 call->vret_in_reg = TRUE;
2068 * Nullify the instruction computing the vret addr to enable
2069 * future optimizations.
2072 NULLIFY_INS (call->vret_var);
2074 if (call->tail_call)
2077 * The valuetype is in RAX:RDX after the call, need to be copied to
2078 * the stack. Push the address here, so the call instruction can
2081 if (!cfg->arch.vret_addr_loc) {
2082 cfg->arch.vret_addr_loc = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2083 /* Prevent it from being register allocated or optimized away */
2084 ((MonoInst*)cfg->arch.vret_addr_loc)->flags |= MONO_INST_VOLATILE;
2087 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, ((MonoInst*)cfg->arch.vret_addr_loc)->dreg, call->vret_var->dreg);
2091 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
2092 vtarg->sreg1 = call->vret_var->dreg;
2093 vtarg->dreg = mono_alloc_preg (cfg);
2094 MONO_ADD_INS (cfg->cbb, vtarg);
2096 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
2100 #ifdef PLATFORM_WIN32
2101 if (call->inst.opcode != OP_JMP && OP_TAILCALL != call->inst.opcode) {
2102 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, 0x20);
2106 if (cfg->method->save_lmf) {
2107 MONO_INST_NEW (cfg, arg, OP_AMD64_SAVE_SP_TO_LMF);
2108 MONO_ADD_INS (cfg->cbb, arg);
2111 call->stack_usage = cinfo->stack_usage;
2115 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
2118 MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
2119 ArgInfo *ainfo = (ArgInfo*)ins->inst_p1;
2120 int size = ins->backend.size;
2122 if (ainfo->storage == ArgValuetypeInReg) {
2126 for (part = 0; part < 2; ++part) {
2127 if (ainfo->pair_storage [part] == ArgNone)
2130 MONO_INST_NEW (cfg, load, arg_storage_to_load_membase (ainfo->pair_storage [part]));
2131 load->inst_basereg = src->dreg;
2132 load->inst_offset = part * sizeof (gpointer);
2134 switch (ainfo->pair_storage [part]) {
2136 load->dreg = mono_alloc_ireg (cfg);
2138 case ArgInDoubleSSEReg:
2139 case ArgInFloatSSEReg:
2140 load->dreg = mono_alloc_freg (cfg);
2143 g_assert_not_reached ();
2145 MONO_ADD_INS (cfg->cbb, load);
2147 add_outarg_reg2 (cfg, call, ainfo->pair_storage [part], ainfo->pair_regs [part], load);
2149 } else if (ainfo->storage == ArgValuetypeAddrInIReg) {
2150 MonoInst *vtaddr, *load;
2151 vtaddr = mono_compile_create_var (cfg, &ins->klass->byval_arg, OP_LOCAL);
2153 MONO_INST_NEW (cfg, load, OP_LDADDR);
2154 load->inst_p0 = vtaddr;
2155 vtaddr->flags |= MONO_INST_INDIRECT;
2156 load->type = STACK_MP;
2157 load->klass = vtaddr->klass;
2158 load->dreg = mono_alloc_ireg (cfg);
2159 MONO_ADD_INS (cfg->cbb, load);
2160 mini_emit_memcpy2 (cfg, load->dreg, 0, src->dreg, 0, size, 4);
2162 if (ainfo->pair_storage [0] == ArgInIReg) {
2163 MONO_INST_NEW (cfg, arg, OP_X86_LEA_MEMBASE);
2164 arg->dreg = ainfo->pair_regs [0];
2165 arg->sreg1 = load->dreg;
2167 MONO_ADD_INS (cfg->cbb, arg);
2169 MONO_INST_NEW (cfg, arg, OP_X86_PUSH);
2170 arg->sreg1 = load->dreg;
2171 MONO_ADD_INS (cfg->cbb, arg);
2175 /* Can't use this for < 8 since it does an 8 byte memory load */
2176 MONO_INST_NEW (cfg, arg, OP_X86_PUSH_MEMBASE);
2177 arg->inst_basereg = src->dreg;
2178 arg->inst_offset = 0;
2179 MONO_ADD_INS (cfg->cbb, arg);
2180 } else if (size <= 40) {
2181 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, ALIGN_TO (size, 8));
2182 mini_emit_memcpy2 (cfg, X86_ESP, 0, src->dreg, 0, size, 4);
2184 MONO_INST_NEW (cfg, arg, OP_X86_PUSH_OBJ);
2185 arg->inst_basereg = src->dreg;
2186 arg->inst_offset = 0;
2187 arg->inst_imm = size;
2188 MONO_ADD_INS (cfg->cbb, arg);
2194 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
2196 MonoType *ret = mono_type_get_underlying_type (mono_method_signature (method)->ret);
2199 if (ret->type == MONO_TYPE_R4) {
2200 MONO_EMIT_NEW_UNALU (cfg, OP_AMD64_SET_XMMREG_R4, cfg->ret->dreg, val->dreg);
2202 } else if (ret->type == MONO_TYPE_R8) {
2203 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
2208 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
2211 #define EMIT_COND_BRANCH(ins,cond,sign) \
2212 if (ins->flags & MONO_INST_BRLABEL) { \
2213 if (ins->inst_i0->inst_c0) { \
2214 x86_branch (code, cond, cfg->native_code + ins->inst_i0->inst_c0, sign); \
2216 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
2217 if ((cfg->opt & MONO_OPT_BRANCH) && \
2218 x86_is_imm8 (ins->inst_i0->inst_c1 - cpos)) \
2219 x86_branch8 (code, cond, 0, sign); \
2221 x86_branch32 (code, cond, 0, sign); \
2224 if (ins->inst_true_bb->native_offset) { \
2225 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
2227 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
2228 if ((cfg->opt & MONO_OPT_BRANCH) && \
2229 x86_is_imm8 (ins->inst_true_bb->max_offset - cpos)) \
2230 x86_branch8 (code, cond, 0, sign); \
2232 x86_branch32 (code, cond, 0, sign); \
2236 /* emit an exception if condition is fail */
2237 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
2239 MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
2240 if (tins == NULL) { \
2241 mono_add_patch_info (cfg, code - cfg->native_code, \
2242 MONO_PATCH_INFO_EXC, exc_name); \
2243 x86_branch32 (code, cond, 0, signed); \
2245 EMIT_COND_BRANCH (tins, cond, signed); \
2249 #define EMIT_FPCOMPARE(code) do { \
2250 amd64_fcompp (code); \
2251 amd64_fnstsw (code); \
2254 #define EMIT_SSE2_FPFUNC(code, op, dreg, sreg1) do { \
2255 amd64_movsd_membase_reg (code, AMD64_RSP, -8, (sreg1)); \
2256 amd64_fld_membase (code, AMD64_RSP, -8, TRUE); \
2257 amd64_ ##op (code); \
2258 amd64_fst_membase (code, AMD64_RSP, -8, TRUE, TRUE); \
2259 amd64_movsd_reg_membase (code, (dreg), AMD64_RSP, -8); \
2263 emit_call_body (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data)
2265 gboolean no_patch = FALSE;
2268 * FIXME: Add support for thunks
2271 gboolean near_call = FALSE;
2274 * Indirect calls are expensive so try to make a near call if possible.
2275 * The caller memory is allocated by the code manager so it is
2276 * guaranteed to be at a 32 bit offset.
2279 if (patch_type != MONO_PATCH_INFO_ABS) {
2280 /* The target is in memory allocated using the code manager */
2283 if ((patch_type == MONO_PATCH_INFO_METHOD) || (patch_type == MONO_PATCH_INFO_METHOD_JUMP)) {
2284 if (((MonoMethod*)data)->klass->image->aot_module)
2285 /* The callee might be an AOT method */
2287 if (((MonoMethod*)data)->dynamic)
2288 /* The target is in malloc-ed memory */
2292 if (patch_type == MONO_PATCH_INFO_INTERNAL_METHOD) {
2294 * The call might go directly to a native function without
2297 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (data);
2299 gconstpointer target = mono_icall_get_wrapper (mi);
2300 if ((((guint64)target) >> 32) != 0)
2306 if (mono_find_class_init_trampoline_by_addr (data))
2309 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (data);
2311 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
2312 strstr (cfg->method->name, info->name)) {
2313 /* A call to the wrapped function */
2314 if ((((guint64)data) >> 32) == 0)
2318 else if (info->func == info->wrapper) {
2320 if ((((guint64)info->func) >> 32) == 0)
2324 /* See the comment in mono_codegen () */
2325 if ((info->name [0] != 'v') || (strstr (info->name, "ves_array_new_va_") == NULL && strstr (info->name, "ves_array_element_address_") == NULL))
2329 else if ((((guint64)data) >> 32) == 0) {
2336 if (cfg->method->dynamic)
2337 /* These methods are allocated using malloc */
2340 if (cfg->compile_aot)
2343 #ifdef MONO_ARCH_NOMAP32BIT
2349 * Align the call displacement to an address divisible by 4 so it does
2350 * not span cache lines. This is required for code patching to work on SMP
2353 if (!no_patch && ((guint32)(code + 1 - cfg->native_code) % 4) != 0)
2354 amd64_padding (code, 4 - ((guint32)(code + 1 - cfg->native_code) % 4));
2355 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
2356 amd64_call_code (code, 0);
2359 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
2360 amd64_set_reg_template (code, GP_SCRATCH_REG);
2361 amd64_call_reg (code, GP_SCRATCH_REG);
2368 static inline guint8*
2369 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data, gboolean win64_adjust_stack)
2371 #ifdef PLATFORM_WIN32
2372 if (win64_adjust_stack)
2373 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 32);
2375 code = emit_call_body (cfg, code, patch_type, data);
2376 #ifdef PLATFORM_WIN32
2377 if (win64_adjust_stack)
2378 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 32);
2385 store_membase_imm_to_store_membase_reg (int opcode)
2388 case OP_STORE_MEMBASE_IMM:
2389 return OP_STORE_MEMBASE_REG;
2390 case OP_STOREI4_MEMBASE_IMM:
2391 return OP_STOREI4_MEMBASE_REG;
2392 case OP_STOREI8_MEMBASE_IMM:
2393 return OP_STOREI8_MEMBASE_REG;
2399 #define INST_IGNORES_CFLAGS(opcode) (!(((opcode) == OP_ADC) || ((opcode) == OP_ADC_IMM) || ((opcode) == OP_IADC) || ((opcode) == OP_IADC_IMM) || ((opcode) == OP_SBB) || ((opcode) == OP_SBB_IMM) || ((opcode) == OP_ISBB) || ((opcode) == OP_ISBB_IMM)))
2402 * mono_arch_peephole_pass_1:
2404 * Perform peephole opts which should/can be performed before local regalloc
2407 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
2411 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
2412 MonoInst *last_ins = ins->prev;
2414 switch (ins->opcode) {
2418 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS) && (ins->inst_imm > 0)) {
2420 * X86_LEA is like ADD, but doesn't have the
2421 * sreg1==dreg restriction. inst_imm > 0 is needed since LEA sign-extends
2422 * its operand to 64 bit.
2424 ins->opcode = OP_X86_LEA_MEMBASE;
2425 ins->inst_basereg = ins->sreg1;
2430 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
2434 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
2435 * the latter has length 2-3 instead of 6 (reverse constant
2436 * propagation). These instruction sequences are very common
2437 * in the initlocals bblock.
2439 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
2440 if (((ins2->opcode == OP_STORE_MEMBASE_IMM) || (ins2->opcode == OP_STOREI4_MEMBASE_IMM) || (ins2->opcode == OP_STOREI8_MEMBASE_IMM) || (ins2->opcode == OP_STORE_MEMBASE_IMM)) && (ins2->inst_imm == 0)) {
2441 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
2442 ins2->sreg1 = ins->dreg;
2443 } else if ((ins2->opcode == OP_STOREI1_MEMBASE_IMM) || (ins2->opcode == OP_STOREI2_MEMBASE_IMM) || (ins2->opcode == OP_STOREI8_MEMBASE_REG) || (ins2->opcode == OP_STORE_MEMBASE_REG)) {
2445 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
2454 case OP_COMPARE_IMM:
2455 case OP_LCOMPARE_IMM:
2456 /* OP_COMPARE_IMM (reg, 0)
2458 * OP_AMD64_TEST_NULL (reg)
2461 ins->opcode = OP_AMD64_TEST_NULL;
2463 case OP_ICOMPARE_IMM:
2465 ins->opcode = OP_X86_TEST_NULL;
2467 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
2469 * OP_STORE_MEMBASE_REG reg, offset(basereg)
2470 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
2472 * OP_STORE_MEMBASE_REG reg, offset(basereg)
2473 * OP_COMPARE_IMM reg, imm
2475 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
2477 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
2478 ins->inst_basereg == last_ins->inst_destbasereg &&
2479 ins->inst_offset == last_ins->inst_offset) {
2480 ins->opcode = OP_ICOMPARE_IMM;
2481 ins->sreg1 = last_ins->sreg1;
2483 /* check if we can remove cmp reg,0 with test null */
2485 ins->opcode = OP_X86_TEST_NULL;
2491 mono_peephole_ins (bb, ins);
2496 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
2500 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
2501 switch (ins->opcode) {
2504 /* reg = 0 -> XOR (reg, reg) */
2505 /* XOR sets cflags on x86, so we cant do it always */
2506 if (ins->inst_c0 == 0 && (!ins->next || (ins->next && INST_IGNORES_CFLAGS (ins->next->opcode)))) {
2507 ins->opcode = OP_LXOR;
2508 ins->sreg1 = ins->dreg;
2509 ins->sreg2 = ins->dreg;
2517 * Use IXOR to avoid a rex prefix if possible. The cpu will sign extend the
2518 * 0 result into 64 bits.
2520 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
2521 ins->opcode = OP_IXOR;
2525 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
2529 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
2530 * the latter has length 2-3 instead of 6 (reverse constant
2531 * propagation). These instruction sequences are very common
2532 * in the initlocals bblock.
2534 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
2535 if (((ins2->opcode == OP_STORE_MEMBASE_IMM) || (ins2->opcode == OP_STOREI4_MEMBASE_IMM) || (ins2->opcode == OP_STOREI8_MEMBASE_IMM) || (ins2->opcode == OP_STORE_MEMBASE_IMM)) && (ins2->inst_imm == 0)) {
2536 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
2537 ins2->sreg1 = ins->dreg;
2538 } else if ((ins2->opcode == OP_STOREI1_MEMBASE_IMM) || (ins2->opcode == OP_STOREI2_MEMBASE_IMM) || (ins2->opcode == OP_STOREI4_MEMBASE_REG) || (ins2->opcode == OP_STOREI8_MEMBASE_REG) || (ins2->opcode == OP_STORE_MEMBASE_REG)) {
2540 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
2550 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
2551 ins->opcode = OP_X86_INC_REG;
2554 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
2555 ins->opcode = OP_X86_DEC_REG;
2559 mono_peephole_ins (bb, ins);
2563 #define NEW_INS(cfg,ins,dest,op) do { \
2564 MONO_INST_NEW ((cfg), (dest), (op)); \
2565 (dest)->cil_code = (ins)->cil_code; \
2566 mono_bblock_insert_before_ins (bb, ins, (dest)); \
2570 * mono_arch_lowering_pass:
2572 * Converts complex opcodes into simpler ones so that each IR instruction
2573 * corresponds to one machine instruction.
2576 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
2578 MonoInst *ins, *n, *temp;
2580 if (bb->max_vreg > cfg->rs->next_vreg)
2581 cfg->rs->next_vreg = bb->max_vreg;
2584 * FIXME: Need to add more instructions, but the current machine
2585 * description can't model some parts of the composite instructions like
2588 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
2589 switch (ins->opcode) {
2594 case OP_IDIV_UN_IMM:
2595 case OP_IREM_UN_IMM:
2596 mono_decompose_op_imm (cfg, bb, ins);
2598 case OP_COMPARE_IMM:
2599 case OP_LCOMPARE_IMM:
2600 if (!amd64_is_imm32 (ins->inst_imm)) {
2601 NEW_INS (cfg, ins, temp, OP_I8CONST);
2602 temp->inst_c0 = ins->inst_imm;
2604 temp->dreg = mono_alloc_ireg (cfg);
2606 temp->dreg = mono_regstate_next_int (cfg->rs);
2607 ins->opcode = OP_COMPARE;
2608 ins->sreg2 = temp->dreg;
2611 case OP_LOAD_MEMBASE:
2612 case OP_LOADI8_MEMBASE:
2613 if (!amd64_is_imm32 (ins->inst_offset)) {
2614 NEW_INS (cfg, ins, temp, OP_I8CONST);
2615 temp->inst_c0 = ins->inst_offset;
2617 temp->dreg = mono_alloc_ireg (cfg);
2619 temp->dreg = mono_regstate_next_int (cfg->rs);
2620 ins->opcode = OP_AMD64_LOADI8_MEMINDEX;
2621 ins->inst_indexreg = temp->dreg;
2624 case OP_STORE_MEMBASE_IMM:
2625 case OP_STOREI8_MEMBASE_IMM:
2626 if (!amd64_is_imm32 (ins->inst_imm)) {
2627 NEW_INS (cfg, ins, temp, OP_I8CONST);
2628 temp->inst_c0 = ins->inst_imm;
2630 temp->dreg = mono_alloc_ireg (cfg);
2632 temp->dreg = mono_regstate_next_int (cfg->rs);
2633 ins->opcode = OP_STOREI8_MEMBASE_REG;
2634 ins->sreg1 = temp->dreg;
2642 bb->max_vreg = cfg->rs->next_vreg;
2646 branch_cc_table [] = {
2647 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
2648 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
2649 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
2652 /* Maps CMP_... constants to X86_CC_... constants */
2655 X86_CC_EQ, X86_CC_NE, X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT,
2656 X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT
2660 cc_signed_table [] = {
2661 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
2662 FALSE, FALSE, FALSE, FALSE
2665 /*#include "cprop.c"*/
2667 static unsigned char*
2668 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
2670 amd64_sse_cvttsd2si_reg_reg (code, dreg, sreg);
2673 amd64_widen_reg (code, dreg, dreg, is_signed, FALSE);
2675 amd64_widen_reg (code, dreg, dreg, is_signed, TRUE);
2679 static unsigned char*
2680 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
2682 int sreg = tree->sreg1;
2683 int need_touch = FALSE;
2685 #if defined(PLATFORM_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
2686 if (!tree->flags & MONO_INST_INIT)
2695 * If requested stack size is larger than one page,
2696 * perform stack-touch operation
2699 * Generate stack probe code.
2700 * Under Windows, it is necessary to allocate one page at a time,
2701 * "touching" stack after each successful sub-allocation. This is
2702 * because of the way stack growth is implemented - there is a
2703 * guard page before the lowest stack page that is currently commited.
2704 * Stack normally grows sequentially so OS traps access to the
2705 * guard page and commits more pages when needed.
2707 amd64_test_reg_imm (code, sreg, ~0xFFF);
2708 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2710 br[2] = code; /* loop */
2711 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
2712 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
2713 amd64_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
2714 amd64_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
2715 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
2716 amd64_patch (br[3], br[2]);
2717 amd64_test_reg_reg (code, sreg, sreg);
2718 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2719 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
2721 br[1] = code; x86_jump8 (code, 0);
2723 amd64_patch (br[0], code);
2724 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
2725 amd64_patch (br[1], code);
2726 amd64_patch (br[4], code);
2729 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, tree->sreg1);
2731 if (tree->flags & MONO_INST_INIT) {
2733 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX) {
2734 amd64_push_reg (code, AMD64_RAX);
2737 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX) {
2738 amd64_push_reg (code, AMD64_RCX);
2741 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI) {
2742 amd64_push_reg (code, AMD64_RDI);
2746 amd64_shift_reg_imm (code, X86_SHR, sreg, 3);
2747 if (sreg != AMD64_RCX)
2748 amd64_mov_reg_reg (code, AMD64_RCX, sreg, 8);
2749 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
2751 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, offset);
2753 amd64_prefix (code, X86_REP_PREFIX);
2756 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI)
2757 amd64_pop_reg (code, AMD64_RDI);
2758 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX)
2759 amd64_pop_reg (code, AMD64_RCX);
2760 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX)
2761 amd64_pop_reg (code, AMD64_RAX);
2767 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
2772 /* Move return value to the target register */
2773 /* FIXME: do this in the local reg allocator */
2774 switch (ins->opcode) {
2777 case OP_CALL_MEMBASE:
2780 case OP_LCALL_MEMBASE:
2781 g_assert (ins->dreg == AMD64_RAX);
2785 case OP_FCALL_MEMBASE:
2786 if (((MonoCallInst*)ins)->signature->ret->type == MONO_TYPE_R4) {
2787 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, AMD64_XMM0);
2790 if (ins->dreg != AMD64_XMM0)
2791 amd64_sse_movsd_reg_reg (code, ins->dreg, AMD64_XMM0);
2796 case OP_VCALL_MEMBASE:
2799 case OP_VCALL2_MEMBASE:
2800 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, ((MonoCallInst*)ins)->signature, FALSE);
2801 if (cinfo->ret.storage == ArgValuetypeInReg) {
2802 MonoInst *loc = cfg->arch.vret_addr_loc;
2804 /* Load the destination address */
2805 g_assert (loc->opcode == OP_REGOFFSET);
2806 amd64_mov_reg_membase (code, AMD64_RCX, loc->inst_basereg, loc->inst_offset, 8);
2808 for (quad = 0; quad < 2; quad ++) {
2809 switch (cinfo->ret.pair_storage [quad]) {
2811 amd64_mov_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad], 8);
2813 case ArgInFloatSSEReg:
2814 amd64_movss_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
2816 case ArgInDoubleSSEReg:
2817 amd64_movsd_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
2834 * @code: buffer to store code to
2835 * @dreg: hard register where to place the result
2836 * @tls_offset: offset info
2838 * emit_tls_get emits in @code the native code that puts in the dreg register
2839 * the item in the thread local storage identified by tls_offset.
2841 * Returns: a pointer to the end of the stored code
2844 emit_tls_get (guint8* code, int dreg, int tls_offset)
2846 #ifdef PLATFORM_WIN32
2847 g_assert (tls_offset < 64);
2848 x86_prefix (code, X86_GS_PREFIX);
2849 amd64_mov_reg_mem (code, dreg, (tls_offset * 8) + 0x1480, 8);
2851 if (optimize_for_xen) {
2852 x86_prefix (code, X86_FS_PREFIX);
2853 amd64_mov_reg_mem (code, dreg, 0, 8);
2854 amd64_mov_reg_membase (code, dreg, dreg, tls_offset, 8);
2856 x86_prefix (code, X86_FS_PREFIX);
2857 amd64_mov_reg_mem (code, dreg, tls_offset, 8);
2864 * emit_load_volatile_arguments:
2866 * Load volatile arguments from the stack to the original input registers.
2867 * Required before a tail call.
2870 emit_load_volatile_arguments (MonoCompile *cfg, guint8 *code)
2872 MonoMethod *method = cfg->method;
2873 MonoMethodSignature *sig;
2878 /* FIXME: Generate intermediate code instead */
2880 sig = mono_method_signature (method);
2882 cinfo = cfg->arch.cinfo;
2884 /* This is the opposite of the code in emit_prolog */
2885 if (sig->ret->type != MONO_TYPE_VOID) {
2886 if (cfg->vret_addr && (cfg->vret_addr->opcode != OP_REGVAR))
2887 amd64_mov_reg_membase (code, cinfo->ret.reg, cfg->vret_addr->inst_basereg, cfg->vret_addr->inst_offset, 8);
2890 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2891 ArgInfo *ainfo = cinfo->args + i;
2893 ins = cfg->args [i];
2895 if (sig->hasthis && (i == 0))
2896 arg_type = &mono_defaults.object_class->byval_arg;
2898 arg_type = sig->params [i - sig->hasthis];
2900 if (ins->opcode != OP_REGVAR) {
2901 switch (ainfo->storage) {
2906 amd64_mov_reg_membase (code, ainfo->reg, ins->inst_basereg, ins->inst_offset, size);
2909 case ArgInFloatSSEReg:
2910 amd64_movss_reg_membase (code, ainfo->reg, ins->inst_basereg, ins->inst_offset);
2912 case ArgInDoubleSSEReg:
2913 amd64_movsd_reg_membase (code, ainfo->reg, ins->inst_basereg, ins->inst_offset);
2915 case ArgValuetypeInReg:
2916 for (quad = 0; quad < 2; quad ++) {
2917 switch (ainfo->pair_storage [quad]) {
2919 amd64_mov_reg_membase (code, ainfo->pair_regs [quad], ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), sizeof (gpointer));
2921 case ArgInFloatSSEReg:
2922 case ArgInDoubleSSEReg:
2923 g_assert_not_reached ();
2928 g_assert_not_reached ();
2932 case ArgValuetypeAddrInIReg:
2933 if (ainfo->pair_storage [0] == ArgInIReg)
2934 amd64_mov_reg_membase (code, ainfo->pair_regs [0], ins->inst_left->inst_basereg, ins->inst_left->inst_offset, sizeof (gpointer));
2941 g_assert (ainfo->storage == ArgInIReg);
2943 amd64_mov_reg_reg (code, ainfo->reg, ins->dreg, 8);
2950 #define REAL_PRINT_REG(text,reg) \
2951 mono_assert (reg >= 0); \
2952 amd64_push_reg (code, AMD64_RAX); \
2953 amd64_push_reg (code, AMD64_RDX); \
2954 amd64_push_reg (code, AMD64_RCX); \
2955 amd64_push_reg (code, reg); \
2956 amd64_push_imm (code, reg); \
2957 amd64_push_imm (code, text " %d %p\n"); \
2958 amd64_mov_reg_imm (code, AMD64_RAX, printf); \
2959 amd64_call_reg (code, AMD64_RAX); \
2960 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 3*4); \
2961 amd64_pop_reg (code, AMD64_RCX); \
2962 amd64_pop_reg (code, AMD64_RDX); \
2963 amd64_pop_reg (code, AMD64_RAX);
2965 /* benchmark and set based on cpu */
2966 #define LOOP_ALIGNMENT 8
2967 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
2970 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2975 guint8 *code = cfg->native_code + cfg->code_len;
2976 MonoInst *last_ins = NULL;
2977 guint last_offset = 0;
2980 if (cfg->opt & MONO_OPT_LOOP) {
2981 int pad, align = LOOP_ALIGNMENT;
2982 /* set alignment depending on cpu */
2983 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
2985 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
2986 amd64_padding (code, pad);
2987 cfg->code_len += pad;
2988 bb->native_offset = cfg->code_len;
2992 if (cfg->verbose_level > 2)
2993 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2995 cpos = bb->max_offset;
2997 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2998 MonoProfileCoverageInfo *cov = cfg->coverage_info;
2999 g_assert (!cfg->compile_aot);
3002 cov->data [bb->dfn].cil_code = bb->cil_code;
3003 amd64_mov_reg_imm (code, AMD64_R11, (guint64)&cov->data [bb->dfn].count);
3004 /* this is not thread save, but good enough */
3005 amd64_inc_membase (code, AMD64_R11, 0);
3008 offset = code - cfg->native_code;
3010 mono_debug_open_block (cfg, bb, offset);
3012 MONO_BB_FOR_EACH_INS (bb, ins) {
3013 offset = code - cfg->native_code;
3015 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
3017 if (G_UNLIKELY (offset > (cfg->code_size - max_len - 16))) {
3018 cfg->code_size *= 2;
3019 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3020 code = cfg->native_code + offset;
3021 mono_jit_stats.code_reallocs++;
3024 if (cfg->debug_info)
3025 mono_debug_record_line_number (cfg, ins, offset);
3027 switch (ins->opcode) {
3029 amd64_mul_reg (code, ins->sreg2, TRUE);
3032 amd64_mul_reg (code, ins->sreg2, FALSE);
3034 case OP_X86_SETEQ_MEMBASE:
3035 amd64_set_membase (code, X86_CC_EQ, ins->inst_basereg, ins->inst_offset, TRUE);
3037 case OP_STOREI1_MEMBASE_IMM:
3038 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
3040 case OP_STOREI2_MEMBASE_IMM:
3041 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
3043 case OP_STOREI4_MEMBASE_IMM:
3044 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
3046 case OP_STOREI1_MEMBASE_REG:
3047 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
3049 case OP_STOREI2_MEMBASE_REG:
3050 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
3052 case OP_STORE_MEMBASE_REG:
3053 case OP_STOREI8_MEMBASE_REG:
3054 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 8);
3056 case OP_STOREI4_MEMBASE_REG:
3057 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
3059 case OP_STORE_MEMBASE_IMM:
3060 case OP_STOREI8_MEMBASE_IMM:
3061 g_assert (amd64_is_imm32 (ins->inst_imm));
3062 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 8);
3066 // FIXME: Decompose this earlier
3067 if (amd64_is_imm32 (ins->inst_imm))
3068 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, sizeof (gpointer));
3070 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3071 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 8);
3075 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3076 amd64_movsxd_reg_membase (code, ins->dreg, ins->dreg, 0);
3079 // FIXME: Decompose this earlier
3081 if (amd64_is_imm32 (ins->inst_imm))
3082 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
3084 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3085 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
3088 amd64_mov_reg_imm (code, ins->dreg, ins->inst_p0);
3089 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
3093 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3094 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, FALSE);
3097 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3098 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, TRUE);
3100 case OP_LOAD_MEMBASE:
3101 case OP_LOADI8_MEMBASE:
3102 g_assert (amd64_is_imm32 (ins->inst_offset));
3103 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, sizeof (gpointer));
3105 case OP_LOADI4_MEMBASE:
3106 amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3108 case OP_LOADU4_MEMBASE:
3109 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
3111 case OP_LOADU1_MEMBASE:
3112 /* The cpu zero extends the result into 64 bits */
3113 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE, 4);
3115 case OP_LOADI1_MEMBASE:
3116 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
3118 case OP_LOADU2_MEMBASE:
3119 /* The cpu zero extends the result into 64 bits */
3120 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE, 4);
3122 case OP_LOADI2_MEMBASE:
3123 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
3125 case OP_AMD64_LOADI8_MEMINDEX:
3126 amd64_mov_reg_memindex_size (code, ins->dreg, ins->inst_basereg, 0, ins->inst_indexreg, 0, 8);
3128 case OP_LCONV_TO_I1:
3129 case OP_ICONV_TO_I1:
3131 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
3133 case OP_LCONV_TO_I2:
3134 case OP_ICONV_TO_I2:
3136 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
3138 case OP_LCONV_TO_U1:
3139 case OP_ICONV_TO_U1:
3140 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
3142 case OP_LCONV_TO_U2:
3143 case OP_ICONV_TO_U2:
3144 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
3147 /* Clean out the upper word */
3148 amd64_mov_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
3151 amd64_movsxd_reg_reg (code, ins->dreg, ins->sreg1);
3155 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3157 case OP_COMPARE_IMM:
3158 case OP_LCOMPARE_IMM:
3159 g_assert (amd64_is_imm32 (ins->inst_imm));
3160 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
3162 case OP_X86_COMPARE_REG_MEMBASE:
3163 amd64_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
3165 case OP_X86_TEST_NULL:
3166 amd64_test_reg_reg_size (code, ins->sreg1, ins->sreg1, 4);
3168 case OP_AMD64_TEST_NULL:
3169 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
3172 case OP_X86_ADD_REG_MEMBASE:
3173 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3175 case OP_X86_SUB_REG_MEMBASE:
3176 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3178 case OP_X86_AND_REG_MEMBASE:
3179 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3181 case OP_X86_OR_REG_MEMBASE:
3182 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3184 case OP_X86_XOR_REG_MEMBASE:
3185 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3188 case OP_X86_ADD_MEMBASE_IMM:
3189 /* FIXME: Make a 64 version too */
3190 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3192 case OP_X86_SUB_MEMBASE_IMM:
3193 g_assert (amd64_is_imm32 (ins->inst_imm));
3194 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3196 case OP_X86_AND_MEMBASE_IMM:
3197 g_assert (amd64_is_imm32 (ins->inst_imm));
3198 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3200 case OP_X86_OR_MEMBASE_IMM:
3201 g_assert (amd64_is_imm32 (ins->inst_imm));
3202 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3204 case OP_X86_XOR_MEMBASE_IMM:
3205 g_assert (amd64_is_imm32 (ins->inst_imm));
3206 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3208 case OP_X86_ADD_MEMBASE_REG:
3209 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
3211 case OP_X86_SUB_MEMBASE_REG:
3212 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
3214 case OP_X86_AND_MEMBASE_REG:
3215 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
3217 case OP_X86_OR_MEMBASE_REG:
3218 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
3220 case OP_X86_XOR_MEMBASE_REG:
3221 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
3223 case OP_X86_INC_MEMBASE:
3224 amd64_inc_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
3226 case OP_X86_INC_REG:
3227 amd64_inc_reg_size (code, ins->dreg, 4);
3229 case OP_X86_DEC_MEMBASE:
3230 amd64_dec_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
3232 case OP_X86_DEC_REG:
3233 amd64_dec_reg_size (code, ins->dreg, 4);
3235 case OP_X86_MUL_REG_MEMBASE:
3236 case OP_X86_MUL_MEMBASE_REG:
3237 amd64_imul_reg_membase_size (code, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3239 case OP_AMD64_ICOMPARE_MEMBASE_REG:
3240 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
3242 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
3243 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3245 case OP_AMD64_COMPARE_MEMBASE_REG:
3246 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
3248 case OP_AMD64_COMPARE_MEMBASE_IMM:
3249 g_assert (amd64_is_imm32 (ins->inst_imm));
3250 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
3252 case OP_X86_COMPARE_MEMBASE8_IMM:
3253 amd64_alu_membase8_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3255 case OP_AMD64_ICOMPARE_REG_MEMBASE:
3256 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3258 case OP_AMD64_COMPARE_REG_MEMBASE:
3259 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
3262 case OP_AMD64_ADD_REG_MEMBASE:
3263 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
3265 case OP_AMD64_SUB_REG_MEMBASE:
3266 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
3268 case OP_AMD64_AND_REG_MEMBASE:
3269 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
3271 case OP_AMD64_OR_REG_MEMBASE:
3272 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
3274 case OP_AMD64_XOR_REG_MEMBASE:
3275 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
3278 case OP_AMD64_ADD_MEMBASE_REG:
3279 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
3281 case OP_AMD64_SUB_MEMBASE_REG:
3282 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
3284 case OP_AMD64_AND_MEMBASE_REG:
3285 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
3287 case OP_AMD64_OR_MEMBASE_REG:
3288 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
3290 case OP_AMD64_XOR_MEMBASE_REG:
3291 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
3294 case OP_AMD64_ADD_MEMBASE_IMM:
3295 g_assert (amd64_is_imm32 (ins->inst_imm));
3296 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
3298 case OP_AMD64_SUB_MEMBASE_IMM:
3299 g_assert (amd64_is_imm32 (ins->inst_imm));
3300 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
3302 case OP_AMD64_AND_MEMBASE_IMM:
3303 g_assert (amd64_is_imm32 (ins->inst_imm));
3304 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
3306 case OP_AMD64_OR_MEMBASE_IMM:
3307 g_assert (amd64_is_imm32 (ins->inst_imm));
3308 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
3310 case OP_AMD64_XOR_MEMBASE_IMM:
3311 g_assert (amd64_is_imm32 (ins->inst_imm));
3312 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
3316 amd64_breakpoint (code);
3320 case OP_DUMMY_STORE:
3321 case OP_NOT_REACHED:
3326 amd64_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
3329 amd64_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
3333 g_assert (amd64_is_imm32 (ins->inst_imm));
3334 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
3337 g_assert (amd64_is_imm32 (ins->inst_imm));
3338 amd64_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
3342 amd64_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
3345 amd64_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
3349 g_assert (amd64_is_imm32 (ins->inst_imm));
3350 amd64_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
3353 g_assert (amd64_is_imm32 (ins->inst_imm));
3354 amd64_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
3357 amd64_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
3361 g_assert (amd64_is_imm32 (ins->inst_imm));
3362 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
3365 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
3370 guint32 size = (ins->opcode == OP_IMUL_IMM) ? 4 : 8;
3372 switch (ins->inst_imm) {
3376 if (ins->dreg != ins->sreg1)
3377 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, size);
3378 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
3381 /* LEA r1, [r2 + r2*2] */
3382 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
3385 /* LEA r1, [r2 + r2*4] */
3386 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3389 /* LEA r1, [r2 + r2*2] */
3391 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
3392 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
3395 /* LEA r1, [r2 + r2*8] */
3396 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
3399 /* LEA r1, [r2 + r2*4] */
3401 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3402 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
3405 /* LEA r1, [r2 + r2*2] */
3407 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
3408 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
3411 /* LEA r1, [r2 + r2*4] */
3412 /* LEA r1, [r1 + r1*4] */
3413 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3414 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
3417 /* LEA r1, [r2 + r2*4] */
3419 /* LEA r1, [r1 + r1*4] */
3420 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3421 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
3422 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
3425 amd64_imul_reg_reg_imm_size (code, ins->dreg, ins->sreg1, ins->inst_imm, size);
3432 /* Regalloc magic makes the div/rem cases the same */
3433 if (ins->sreg2 == AMD64_RDX) {
3434 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
3436 amd64_div_membase (code, AMD64_RSP, -8, TRUE);
3439 amd64_div_reg (code, ins->sreg2, TRUE);
3444 if (ins->sreg2 == AMD64_RDX) {
3445 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
3446 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
3447 amd64_div_membase (code, AMD64_RSP, -8, FALSE);
3449 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
3450 amd64_div_reg (code, ins->sreg2, FALSE);
3455 if (ins->sreg2 == AMD64_RDX) {
3456 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
3457 amd64_cdq_size (code, 4);
3458 amd64_div_membase_size (code, AMD64_RSP, -8, TRUE, 4);
3460 amd64_cdq_size (code, 4);
3461 amd64_div_reg_size (code, ins->sreg2, TRUE, 4);
3466 if (ins->sreg2 == AMD64_RDX) {
3467 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
3468 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
3469 amd64_div_membase_size (code, AMD64_RSP, -8, FALSE, 4);
3471 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
3472 amd64_div_reg_size (code, ins->sreg2, FALSE, 4);
3476 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
3477 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
3480 amd64_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
3484 g_assert (amd64_is_imm32 (ins->inst_imm));
3485 amd64_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
3488 amd64_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
3492 g_assert (amd64_is_imm32 (ins->inst_imm));
3493 amd64_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
3496 g_assert (ins->sreg2 == AMD64_RCX);
3497 amd64_shift_reg (code, X86_SHL, ins->dreg);
3500 g_assert (ins->sreg2 == AMD64_RCX);
3501 amd64_shift_reg (code, X86_SAR, ins->dreg);
3504 g_assert (amd64_is_imm32 (ins->inst_imm));
3505 amd64_shift_reg_imm_size (code, X86_SAR, ins->dreg, ins->inst_imm, 4);
3508 g_assert (amd64_is_imm32 (ins->inst_imm));
3509 amd64_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
3512 g_assert (amd64_is_imm32 (ins->inst_imm));
3513 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
3515 case OP_LSHR_UN_IMM:
3516 g_assert (amd64_is_imm32 (ins->inst_imm));
3517 amd64_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
3520 g_assert (ins->sreg2 == AMD64_RCX);
3521 amd64_shift_reg (code, X86_SHR, ins->dreg);
3524 g_assert (amd64_is_imm32 (ins->inst_imm));
3525 amd64_shift_reg_imm_size (code, X86_SHL, ins->dreg, ins->inst_imm, 4);
3528 g_assert (amd64_is_imm32 (ins->inst_imm));
3529 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
3534 amd64_alu_reg_reg_size (code, X86_ADD, ins->sreg1, ins->sreg2, 4);
3537 amd64_alu_reg_reg_size (code, X86_ADC, ins->sreg1, ins->sreg2, 4);
3540 amd64_alu_reg_imm_size (code, X86_ADD, ins->dreg, ins->inst_imm, 4);
3543 amd64_alu_reg_imm_size (code, X86_ADC, ins->dreg, ins->inst_imm, 4);
3547 amd64_alu_reg_reg_size (code, X86_SUB, ins->sreg1, ins->sreg2, 4);
3550 amd64_alu_reg_reg_size (code, X86_SBB, ins->sreg1, ins->sreg2, 4);
3553 amd64_alu_reg_imm_size (code, X86_SUB, ins->dreg, ins->inst_imm, 4);
3556 amd64_alu_reg_imm_size (code, X86_SBB, ins->dreg, ins->inst_imm, 4);
3559 amd64_alu_reg_reg_size (code, X86_AND, ins->sreg1, ins->sreg2, 4);
3562 amd64_alu_reg_imm_size (code, X86_AND, ins->sreg1, ins->inst_imm, 4);
3565 amd64_alu_reg_reg_size (code, X86_OR, ins->sreg1, ins->sreg2, 4);
3568 amd64_alu_reg_imm_size (code, X86_OR, ins->sreg1, ins->inst_imm, 4);
3571 amd64_alu_reg_reg_size (code, X86_XOR, ins->sreg1, ins->sreg2, 4);
3574 amd64_alu_reg_imm_size (code, X86_XOR, ins->sreg1, ins->inst_imm, 4);
3577 amd64_neg_reg_size (code, ins->sreg1, 4);
3580 amd64_not_reg_size (code, ins->sreg1, 4);
3583 g_assert (ins->sreg2 == AMD64_RCX);
3584 amd64_shift_reg_size (code, X86_SHL, ins->dreg, 4);
3587 g_assert (ins->sreg2 == AMD64_RCX);
3588 amd64_shift_reg_size (code, X86_SAR, ins->dreg, 4);
3591 amd64_shift_reg_imm_size (code, X86_SAR, ins->dreg, ins->inst_imm, 4);
3593 case OP_ISHR_UN_IMM:
3594 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
3597 g_assert (ins->sreg2 == AMD64_RCX);
3598 amd64_shift_reg_size (code, X86_SHR, ins->dreg, 4);
3601 amd64_shift_reg_imm_size (code, X86_SHL, ins->dreg, ins->inst_imm, 4);
3604 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
3607 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
3608 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
3610 case OP_IMUL_OVF_UN:
3611 case OP_LMUL_OVF_UN: {
3612 /* the mul operation and the exception check should most likely be split */
3613 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
3614 int size = (ins->opcode == OP_IMUL_OVF_UN) ? 4 : 8;
3615 /*g_assert (ins->sreg2 == X86_EAX);
3616 g_assert (ins->dreg == X86_EAX);*/
3617 if (ins->sreg2 == X86_EAX) {
3618 non_eax_reg = ins->sreg1;
3619 } else if (ins->sreg1 == X86_EAX) {
3620 non_eax_reg = ins->sreg2;
3622 /* no need to save since we're going to store to it anyway */
3623 if (ins->dreg != X86_EAX) {
3625 amd64_push_reg (code, X86_EAX);
3627 amd64_mov_reg_reg (code, X86_EAX, ins->sreg1, size);
3628 non_eax_reg = ins->sreg2;
3630 if (ins->dreg == X86_EDX) {
3633 amd64_push_reg (code, X86_EAX);
3637 amd64_push_reg (code, X86_EDX);
3639 amd64_mul_reg_size (code, non_eax_reg, FALSE, size);
3640 /* save before the check since pop and mov don't change the flags */
3641 if (ins->dreg != X86_EAX)
3642 amd64_mov_reg_reg (code, ins->dreg, X86_EAX, size);
3644 amd64_pop_reg (code, X86_EDX);
3646 amd64_pop_reg (code, X86_EAX);
3647 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
3651 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
3653 case OP_ICOMPARE_IMM:
3654 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg1, ins->inst_imm, 4);
3676 EMIT_COND_BRANCH (ins, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
3684 case OP_CMOV_INE_UN:
3685 case OP_CMOV_IGE_UN:
3686 case OP_CMOV_IGT_UN:
3687 case OP_CMOV_ILE_UN:
3688 case OP_CMOV_ILT_UN:
3694 case OP_CMOV_LNE_UN:
3695 case OP_CMOV_LGE_UN:
3696 case OP_CMOV_LGT_UN:
3697 case OP_CMOV_LLE_UN:
3698 case OP_CMOV_LLT_UN:
3699 g_assert (ins->dreg == ins->sreg1);
3700 /* This needs to operate on 64 bit values */
3701 amd64_cmov_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, ins->sreg2);
3705 amd64_not_reg (code, ins->sreg1);
3708 amd64_neg_reg (code, ins->sreg1);
3713 if ((((guint64)ins->inst_c0) >> 32) == 0)
3714 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 4);
3716 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 8);
3719 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3720 amd64_mov_reg_membase (code, ins->dreg, AMD64_RIP, 0, 8);
3723 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3724 amd64_mov_reg_imm_size (code, ins->dreg, 0, 8);
3727 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, sizeof (gpointer));
3729 case OP_AMD64_SET_XMMREG_R4: {
3730 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
3733 case OP_AMD64_SET_XMMREG_R8: {
3734 if (ins->dreg != ins->sreg1)
3735 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
3741 * Note: this 'frame destruction' logic is useful for tail calls, too.
3742 * Keep in sync with the code in emit_epilog.
3746 /* FIXME: no tracing support... */
3747 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3748 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
3750 g_assert (!cfg->method->save_lmf);
3752 if (ins->opcode == OP_JMP)
3753 code = emit_load_volatile_arguments (cfg, code);
3755 if (cfg->arch.omit_fp) {
3756 guint32 save_offset = 0;
3757 /* Pop callee-saved registers */
3758 for (i = 0; i < AMD64_NREG; ++i)
3759 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
3760 amd64_mov_reg_membase (code, i, AMD64_RSP, save_offset, 8);
3763 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
3766 for (i = 0; i < AMD64_NREG; ++i)
3767 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i)))
3768 pos -= sizeof (gpointer);
3771 amd64_lea_membase (code, AMD64_RSP, AMD64_RBP, pos);
3773 /* Pop registers in reverse order */
3774 for (i = AMD64_NREG - 1; i > 0; --i)
3775 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
3776 amd64_pop_reg (code, i);
3782 offset = code - cfg->native_code;
3783 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
3784 if (cfg->compile_aot)
3785 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
3787 amd64_set_reg_template (code, AMD64_R11);
3788 amd64_jump_reg (code, AMD64_R11);
3792 /* ensure ins->sreg1 is not NULL */
3793 amd64_alu_membase_imm_size (code, X86_CMP, ins->sreg1, 0, 0, 4);
3796 amd64_lea_membase (code, AMD64_R11, cfg->frame_reg, cfg->sig_cookie);
3797 amd64_mov_membase_reg (code, ins->sreg1, 0, AMD64_R11, 8);
3806 call = (MonoCallInst*)ins;
3808 * The AMD64 ABI forces callers to know about varargs.
3810 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke))
3811 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
3812 else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
3814 * Since the unmanaged calling convention doesn't contain a
3815 * 'vararg' entry, we have to treat every pinvoke call as a
3816 * potential vararg call.
3820 for (i = 0; i < AMD64_XMM_NREG; ++i)
3821 if (call->used_fregs & (1 << i))
3824 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
3826 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
3829 if (ins->flags & MONO_INST_HAS_METHOD)
3830 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method, FALSE);
3832 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr, FALSE);
3833 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature->call_convention))
3834 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, call->stack_usage);
3835 code = emit_move_return_value (cfg, ins, code);
3841 case OP_VOIDCALL_REG:
3843 call = (MonoCallInst*)ins;
3845 if (AMD64_IS_ARGUMENT_REG (ins->sreg1)) {
3846 amd64_mov_reg_reg (code, AMD64_R11, ins->sreg1, 8);
3847 ins->sreg1 = AMD64_R11;
3851 * The AMD64 ABI forces callers to know about varargs.
3853 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke)) {
3854 if (ins->sreg1 == AMD64_RAX) {
3855 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
3856 ins->sreg1 = AMD64_R11;
3858 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
3859 } else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
3861 * Since the unmanaged calling convention doesn't contain a
3862 * 'vararg' entry, we have to treat every pinvoke call as a
3863 * potential vararg call.
3867 for (i = 0; i < AMD64_XMM_NREG; ++i)
3868 if (call->used_fregs & (1 << i))
3870 if (ins->sreg1 == AMD64_RAX) {
3871 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
3872 ins->sreg1 = AMD64_R11;
3875 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
3877 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
3880 amd64_call_reg (code, ins->sreg1);
3881 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature->call_convention))
3882 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, call->stack_usage);
3883 code = emit_move_return_value (cfg, ins, code);
3885 case OP_FCALL_MEMBASE:
3886 case OP_LCALL_MEMBASE:
3887 case OP_VCALL_MEMBASE:
3888 case OP_VCALL2_MEMBASE:
3889 case OP_VOIDCALL_MEMBASE:
3890 case OP_CALL_MEMBASE:
3891 call = (MonoCallInst*)ins;
3893 if (AMD64_IS_ARGUMENT_REG (ins->sreg1)) {
3895 * Can't use R11 because it is clobbered by the trampoline
3896 * code, and the reg value is needed by get_vcall_slot_addr.
3898 amd64_mov_reg_reg (code, AMD64_RAX, ins->sreg1, 8);
3899 ins->sreg1 = AMD64_RAX;
3902 if (call->method && ins->inst_offset < 0) {
3906 * This is a possible IMT call so save the IMT method in the proper
3907 * register. We don't use the generic code in method-to-ir.c, because
3908 * we need to disassemble this in get_vcall_slot_addr (), so we have to
3909 * maintain control over the layout of the code.
3910 * Also put the base reg in %rax to simplify find_imt_method ().
3912 if (ins->sreg1 != AMD64_RAX) {
3913 amd64_mov_reg_reg (code, AMD64_RAX, ins->sreg1, 8);
3914 ins->sreg1 = AMD64_RAX;
3916 val = (gssize)(gpointer)call->method;
3918 // FIXME: Generics sharing
3920 if ((((guint64)val) >> 32) == 0)
3921 amd64_mov_reg_imm_size (code, MONO_ARCH_IMT_REG, val, 4);
3923 amd64_mov_reg_imm_size (code, MONO_ARCH_IMT_REG, val, 8);
3927 amd64_call_membase (code, ins->sreg1, ins->inst_offset);
3928 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature->call_convention))
3929 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, call->stack_usage);
3930 code = emit_move_return_value (cfg, ins, code);
3932 case OP_AMD64_SAVE_SP_TO_LMF:
3933 amd64_mov_membase_reg (code, cfg->frame_reg, cfg->arch.lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
3937 amd64_push_reg (code, ins->sreg1);
3939 case OP_X86_PUSH_IMM:
3940 g_assert (amd64_is_imm32 (ins->inst_imm));
3941 amd64_push_imm (code, ins->inst_imm);
3943 case OP_X86_PUSH_MEMBASE:
3944 amd64_push_membase (code, ins->inst_basereg, ins->inst_offset);
3946 case OP_X86_PUSH_OBJ:
3947 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, ins->inst_imm);
3948 amd64_push_reg (code, AMD64_RDI);
3949 amd64_push_reg (code, AMD64_RSI);
3950 amd64_push_reg (code, AMD64_RCX);
3951 if (ins->inst_offset)
3952 amd64_lea_membase (code, AMD64_RSI, ins->inst_basereg, ins->inst_offset);
3954 amd64_mov_reg_reg (code, AMD64_RSI, ins->inst_basereg, 8);
3955 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, 3 * 8);
3956 amd64_mov_reg_imm (code, AMD64_RCX, (ins->inst_imm >> 3));
3958 amd64_prefix (code, X86_REP_PREFIX);
3960 amd64_pop_reg (code, AMD64_RCX);
3961 amd64_pop_reg (code, AMD64_RSI);
3962 amd64_pop_reg (code, AMD64_RDI);
3965 amd64_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
3967 case OP_X86_LEA_MEMBASE:
3968 amd64_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
3971 amd64_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
3974 /* keep alignment */
3975 amd64_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1);
3976 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_FRAME_ALIGNMENT - 1));
3977 code = mono_emit_stack_alloc (code, ins);
3978 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
3980 case OP_LOCALLOC_IMM: {
3981 guint32 size = ins->inst_imm;
3982 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
3984 if (ins->flags & MONO_INST_INIT) {
3988 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
3989 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3991 for (i = 0; i < size; i += 8)
3992 amd64_mov_membase_reg (code, AMD64_RSP, i, ins->dreg, 8);
3993 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
3995 amd64_mov_reg_imm (code, ins->dreg, size);
3996 ins->sreg1 = ins->dreg;
3998 code = mono_emit_stack_alloc (code, ins);
3999 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4002 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
4003 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4008 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
4009 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
4010 (gpointer)"mono_arch_throw_exception", FALSE);
4014 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
4015 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
4016 (gpointer)"mono_arch_rethrow_exception", FALSE);
4019 case OP_CALL_HANDLER:
4021 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
4022 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
4023 amd64_call_imm (code, 0);
4024 /* Restore stack alignment */
4025 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
4027 case OP_START_HANDLER: {
4028 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4029 amd64_mov_membase_reg (code, spvar->inst_basereg, spvar->inst_offset, AMD64_RSP, 8);
4032 case OP_ENDFINALLY: {
4033 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4034 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, 8);
4038 case OP_ENDFILTER: {
4039 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4040 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, 8);
4041 /* The local allocator will put the result into RAX */
4047 ins->inst_c0 = code - cfg->native_code;
4050 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
4051 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
4053 if (ins->flags & MONO_INST_BRLABEL) {
4054 if (ins->inst_i0->inst_c0) {
4055 amd64_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
4057 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
4058 if ((cfg->opt & MONO_OPT_BRANCH) &&
4059 x86_is_imm8 (ins->inst_i0->inst_c1 - cpos))
4060 x86_jump8 (code, 0);
4062 x86_jump32 (code, 0);
4065 if (ins->inst_target_bb->native_offset) {
4066 amd64_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
4068 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
4069 if ((cfg->opt & MONO_OPT_BRANCH) &&
4070 x86_is_imm8 (ins->inst_target_bb->max_offset - cpos))
4071 x86_jump8 (code, 0);
4073 x86_jump32 (code, 0);
4078 amd64_jump_reg (code, ins->sreg1);
4095 amd64_set_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
4096 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
4098 case OP_COND_EXC_EQ:
4099 case OP_COND_EXC_NE_UN:
4100 case OP_COND_EXC_LT:
4101 case OP_COND_EXC_LT_UN:
4102 case OP_COND_EXC_GT:
4103 case OP_COND_EXC_GT_UN:
4104 case OP_COND_EXC_GE:
4105 case OP_COND_EXC_GE_UN:
4106 case OP_COND_EXC_LE:
4107 case OP_COND_EXC_LE_UN:
4108 case OP_COND_EXC_IEQ:
4109 case OP_COND_EXC_INE_UN:
4110 case OP_COND_EXC_ILT:
4111 case OP_COND_EXC_ILT_UN:
4112 case OP_COND_EXC_IGT:
4113 case OP_COND_EXC_IGT_UN:
4114 case OP_COND_EXC_IGE:
4115 case OP_COND_EXC_IGE_UN:
4116 case OP_COND_EXC_ILE:
4117 case OP_COND_EXC_ILE_UN:
4118 EMIT_COND_SYSTEM_EXCEPTION (cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)], ins->inst_p1);
4120 case OP_COND_EXC_OV:
4121 case OP_COND_EXC_NO:
4123 case OP_COND_EXC_NC:
4124 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
4125 (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
4127 case OP_COND_EXC_IOV:
4128 case OP_COND_EXC_INO:
4129 case OP_COND_EXC_IC:
4130 case OP_COND_EXC_INC:
4131 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_IEQ],
4132 (ins->opcode < OP_COND_EXC_INE_UN), ins->inst_p1);
4135 /* floating point opcodes */
4137 double d = *(double *)ins->inst_p0;
4139 if ((d == 0.0) && (mono_signbit (d) == 0)) {
4140 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
4143 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
4144 amd64_sse_movsd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
4149 float f = *(float *)ins->inst_p0;
4151 if ((f == 0.0) && (mono_signbit (f) == 0)) {
4152 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
4155 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
4156 amd64_sse_movss_reg_membase (code, ins->dreg, AMD64_RIP, 0);
4157 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
4161 case OP_STORER8_MEMBASE_REG:
4162 amd64_sse_movsd_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
4164 case OP_LOADR8_SPILL_MEMBASE:
4165 g_assert_not_reached ();
4167 case OP_LOADR8_MEMBASE:
4168 amd64_sse_movsd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4170 case OP_STORER4_MEMBASE_REG:
4171 /* This requires a double->single conversion */
4172 amd64_sse_cvtsd2ss_reg_reg (code, AMD64_XMM15, ins->sreg1);
4173 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, AMD64_XMM15);
4175 case OP_LOADR4_MEMBASE:
4176 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4177 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
4179 case OP_ICONV_TO_R4: /* FIXME: change precision */
4180 case OP_ICONV_TO_R8:
4181 amd64_sse_cvtsi2sd_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
4183 case OP_LCONV_TO_R4: /* FIXME: change precision */
4184 case OP_LCONV_TO_R8:
4185 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
4187 case OP_FCONV_TO_R4:
4188 /* FIXME: nothing to do ?? */
4190 case OP_FCONV_TO_I1:
4191 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
4193 case OP_FCONV_TO_U1:
4194 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
4196 case OP_FCONV_TO_I2:
4197 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
4199 case OP_FCONV_TO_U2:
4200 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
4202 case OP_FCONV_TO_U4:
4203 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
4205 case OP_FCONV_TO_I4:
4207 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
4209 case OP_FCONV_TO_I8:
4210 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 8, TRUE);
4212 case OP_LCONV_TO_R_UN: {
4215 /* Based on gcc code */
4216 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
4217 br [0] = code; x86_branch8 (code, X86_CC_S, 0, TRUE);
4220 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
4221 br [1] = code; x86_jump8 (code, 0);
4222 amd64_patch (br [0], code);
4225 /* Save to the red zone */
4226 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RAX, 8);
4227 amd64_mov_membase_reg (code, AMD64_RSP, -16, AMD64_RCX, 8);
4228 amd64_mov_reg_reg (code, AMD64_RCX, ins->sreg1, 8);
4229 amd64_mov_reg_reg (code, AMD64_RAX, ins->sreg1, 8);
4230 amd64_alu_reg_imm (code, X86_AND, AMD64_RCX, 1);
4231 amd64_shift_reg_imm (code, X86_SHR, AMD64_RAX, 1);
4232 amd64_alu_reg_imm (code, X86_OR, AMD64_RAX, AMD64_RCX);
4233 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, AMD64_RAX);
4234 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->dreg);
4236 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
4237 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, -8, 8);
4238 amd64_patch (br [1], code);
4241 case OP_LCONV_TO_OVF_U4:
4242 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0);
4243 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_LT, TRUE, "OverflowException");
4244 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
4246 case OP_LCONV_TO_OVF_I4_UN:
4247 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0x7fffffff);
4248 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_GT, FALSE, "OverflowException");
4249 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
4252 if (ins->dreg != ins->sreg1)
4253 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
4256 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->sreg2);
4259 amd64_sse_subsd_reg_reg (code, ins->dreg, ins->sreg2);
4262 amd64_sse_mulsd_reg_reg (code, ins->dreg, ins->sreg2);
4265 amd64_sse_divsd_reg_reg (code, ins->dreg, ins->sreg2);
4268 static double r8_0 = -0.0;
4270 g_assert (ins->sreg1 == ins->dreg);
4272 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &r8_0);
4273 amd64_sse_xorpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
4277 EMIT_SSE2_FPFUNC (code, fsin, ins->dreg, ins->sreg1);
4280 EMIT_SSE2_FPFUNC (code, fcos, ins->dreg, ins->sreg1);
4283 static guint64 d = 0x7fffffffffffffffUL;
4285 g_assert (ins->sreg1 == ins->dreg);
4287 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &d);
4288 amd64_sse_andpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
4292 EMIT_SSE2_FPFUNC (code, fsqrt, ins->dreg, ins->sreg1);
4295 g_assert (cfg->opt & MONO_OPT_CMOV);
4296 g_assert (ins->dreg == ins->sreg1);
4297 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
4298 amd64_cmov_reg_size (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2, 4);
4301 g_assert (cfg->opt & MONO_OPT_CMOV);
4302 g_assert (ins->dreg == ins->sreg1);
4303 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
4304 amd64_cmov_reg_size (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2, 4);
4307 g_assert (cfg->opt & MONO_OPT_CMOV);
4308 g_assert (ins->dreg == ins->sreg1);
4309 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
4310 amd64_cmov_reg_size (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2, 4);
4313 g_assert (cfg->opt & MONO_OPT_CMOV);
4314 g_assert (ins->dreg == ins->sreg1);
4315 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
4316 amd64_cmov_reg_size (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2, 4);
4319 g_assert (cfg->opt & MONO_OPT_CMOV);
4320 g_assert (ins->dreg == ins->sreg1);
4321 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
4322 amd64_cmov_reg (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2);
4325 g_assert (cfg->opt & MONO_OPT_CMOV);
4326 g_assert (ins->dreg == ins->sreg1);
4327 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
4328 amd64_cmov_reg (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2);
4331 g_assert (cfg->opt & MONO_OPT_CMOV);
4332 g_assert (ins->dreg == ins->sreg1);
4333 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
4334 amd64_cmov_reg (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2);
4337 g_assert (cfg->opt & MONO_OPT_CMOV);
4338 g_assert (ins->dreg == ins->sreg1);
4339 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
4340 amd64_cmov_reg (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2);
4346 * The two arguments are swapped because the fbranch instructions
4347 * depend on this for the non-sse case to work.
4349 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
4352 /* zeroing the register at the start results in
4353 * shorter and faster code (we can also remove the widening op)
4355 guchar *unordered_check;
4356 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4357 amd64_sse_comisd_reg_reg (code, ins->sreg1, ins->sreg2);
4358 unordered_check = code;
4359 x86_branch8 (code, X86_CC_P, 0, FALSE);
4360 amd64_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
4361 amd64_patch (unordered_check, code);
4366 /* zeroing the register at the start results in
4367 * shorter and faster code (we can also remove the widening op)
4369 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4370 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
4371 if (ins->opcode == OP_FCLT_UN) {
4372 guchar *unordered_check = code;
4373 guchar *jump_to_end;
4374 x86_branch8 (code, X86_CC_P, 0, FALSE);
4375 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
4377 x86_jump8 (code, 0);
4378 amd64_patch (unordered_check, code);
4379 amd64_inc_reg (code, ins->dreg);
4380 amd64_patch (jump_to_end, code);
4382 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
4387 /* zeroing the register at the start results in
4388 * shorter and faster code (we can also remove the widening op)
4390 guchar *unordered_check;
4391 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4392 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
4393 if (ins->opcode == OP_FCGT) {
4394 unordered_check = code;
4395 x86_branch8 (code, X86_CC_P, 0, FALSE);
4396 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
4397 amd64_patch (unordered_check, code);
4399 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
4403 case OP_FCLT_MEMBASE:
4404 case OP_FCGT_MEMBASE:
4405 case OP_FCLT_UN_MEMBASE:
4406 case OP_FCGT_UN_MEMBASE:
4407 case OP_FCEQ_MEMBASE: {
4408 guchar *unordered_check, *jump_to_end;
4411 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4412 amd64_sse_comisd_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
4414 switch (ins->opcode) {
4415 case OP_FCEQ_MEMBASE:
4416 x86_cond = X86_CC_EQ;
4418 case OP_FCLT_MEMBASE:
4419 case OP_FCLT_UN_MEMBASE:
4420 x86_cond = X86_CC_LT;
4422 case OP_FCGT_MEMBASE:
4423 case OP_FCGT_UN_MEMBASE:
4424 x86_cond = X86_CC_GT;
4427 g_assert_not_reached ();
4430 unordered_check = code;
4431 x86_branch8 (code, X86_CC_P, 0, FALSE);
4432 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
4434 switch (ins->opcode) {
4435 case OP_FCEQ_MEMBASE:
4436 case OP_FCLT_MEMBASE:
4437 case OP_FCGT_MEMBASE:
4438 amd64_patch (unordered_check, code);
4440 case OP_FCLT_UN_MEMBASE:
4441 case OP_FCGT_UN_MEMBASE:
4443 x86_jump8 (code, 0);
4444 amd64_patch (unordered_check, code);
4445 amd64_inc_reg (code, ins->dreg);
4446 amd64_patch (jump_to_end, code);
4454 guchar *jump = code;
4455 x86_branch8 (code, X86_CC_P, 0, TRUE);
4456 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4457 amd64_patch (jump, code);
4461 /* Branch if C013 != 100 */
4462 /* branch if !ZF or (PF|CF) */
4463 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
4464 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
4465 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
4468 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
4471 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
4472 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
4476 if (ins->opcode == OP_FBGT) {
4479 /* skip branch if C1=1 */
4481 x86_branch8 (code, X86_CC_P, 0, FALSE);
4482 /* branch if (C0 | C3) = 1 */
4483 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
4484 amd64_patch (br1, code);
4487 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
4491 /* Branch if C013 == 100 or 001 */
4494 /* skip branch if C1=1 */
4496 x86_branch8 (code, X86_CC_P, 0, FALSE);
4497 /* branch if (C0 | C3) = 1 */
4498 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
4499 amd64_patch (br1, code);
4503 /* Branch if C013 == 000 */
4504 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
4507 /* Branch if C013=000 or 100 */
4510 /* skip branch if C1=1 */
4512 x86_branch8 (code, X86_CC_P, 0, FALSE);
4513 /* branch if C0=0 */
4514 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
4515 amd64_patch (br1, code);
4519 /* Branch if C013 != 001 */
4520 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
4521 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
4524 /* Transfer value to the fp stack */
4525 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 16);
4526 amd64_movsd_membase_reg (code, AMD64_RSP, 0, ins->sreg1);
4527 amd64_fld_membase (code, AMD64_RSP, 0, TRUE);
4529 amd64_push_reg (code, AMD64_RAX);
4531 amd64_fnstsw (code);
4532 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0x4100);
4533 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, X86_FP_C0);
4534 amd64_pop_reg (code, AMD64_RAX);
4535 amd64_fstp (code, 0);
4536 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
4537 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 16);
4540 code = emit_tls_get (code, ins->dreg, ins->inst_offset);
4543 case OP_MEMORY_BARRIER: {
4544 /* Not needed on amd64 */
4547 case OP_ATOMIC_ADD_I4:
4548 case OP_ATOMIC_ADD_I8: {
4549 int dreg = ins->dreg;
4550 guint32 size = (ins->opcode == OP_ATOMIC_ADD_I4) ? 4 : 8;
4552 if (dreg == ins->inst_basereg)
4555 if (dreg != ins->sreg2)
4556 amd64_mov_reg_reg (code, ins->dreg, ins->sreg2, size);
4558 x86_prefix (code, X86_LOCK_PREFIX);
4559 amd64_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, size);
4561 if (dreg != ins->dreg)
4562 amd64_mov_reg_reg (code, ins->dreg, dreg, size);
4566 case OP_ATOMIC_ADD_NEW_I4:
4567 case OP_ATOMIC_ADD_NEW_I8: {
4568 int dreg = ins->dreg;
4569 guint32 size = (ins->opcode == OP_ATOMIC_ADD_NEW_I4) ? 4 : 8;
4571 if ((dreg == ins->sreg2) || (dreg == ins->inst_basereg))
4574 amd64_mov_reg_reg (code, dreg, ins->sreg2, size);
4575 amd64_prefix (code, X86_LOCK_PREFIX);
4576 amd64_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, size);
4577 /* dreg contains the old value, add with sreg2 value */
4578 amd64_alu_reg_reg_size (code, X86_ADD, dreg, ins->sreg2, size);
4580 if (ins->dreg != dreg)
4581 amd64_mov_reg_reg (code, ins->dreg, dreg, size);
4585 case OP_ATOMIC_EXCHANGE_I4:
4586 case OP_ATOMIC_EXCHANGE_I8:
4587 case OP_ATOMIC_CAS_IMM_I4: {
4589 int sreg2 = ins->sreg2;
4590 int breg = ins->inst_basereg;
4592 gboolean need_push = FALSE, rdx_pushed = FALSE;
4594 if (ins->opcode == OP_ATOMIC_EXCHANGE_I8)
4600 * See http://msdn.microsoft.com/en-us/magazine/cc302329.aspx for
4601 * an explanation of how this works.
4604 /* cmpxchg uses eax as comperand, need to make sure we can use it
4605 * hack to overcome limits in x86 reg allocator
4606 * (req: dreg == eax and sreg2 != eax and breg != eax)
4608 g_assert (ins->dreg == AMD64_RAX);
4610 if (breg == AMD64_RAX && ins->sreg2 == AMD64_RAX)
4611 /* Highly unlikely, but possible */
4614 /* The pushes invalidate rsp */
4615 if ((breg == AMD64_RAX) || need_push) {
4616 amd64_mov_reg_reg (code, AMD64_R11, breg, 8);
4620 /* We need the EAX reg for the comparand */
4621 if (ins->sreg2 == AMD64_RAX) {
4622 if (breg != AMD64_R11) {
4623 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
4626 g_assert (need_push);
4627 amd64_push_reg (code, AMD64_RDX);
4628 amd64_mov_reg_reg (code, AMD64_RDX, AMD64_RAX, size);
4634 if (ins->opcode == OP_ATOMIC_CAS_IMM_I4) {
4635 if (ins->backend.data == NULL)
4636 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4638 amd64_mov_reg_imm (code, AMD64_RAX, ins->backend.data);
4640 amd64_prefix (code, X86_LOCK_PREFIX);
4641 amd64_cmpxchg_membase_reg_size (code, breg, ins->inst_offset, sreg2, size);
4643 amd64_mov_reg_membase (code, AMD64_RAX, breg, ins->inst_offset, size);
4645 br [0] = code; amd64_prefix (code, X86_LOCK_PREFIX);
4646 amd64_cmpxchg_membase_reg_size (code, breg, ins->inst_offset, sreg2, size);
4647 br [1] = code; amd64_branch8 (code, X86_CC_NE, -1, FALSE);
4648 amd64_patch (br [1], br [0]);
4652 amd64_pop_reg (code, AMD64_RDX);
4657 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
4658 g_assert_not_reached ();
4661 if ((code - cfg->native_code - offset) > max_len) {
4662 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %ld)",
4663 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
4664 g_assert_not_reached ();
4670 last_offset = offset;
4673 cfg->code_len = code - cfg->native_code;
4677 mono_arch_register_lowlevel_calls (void)
4679 /* The signature doesn't matter */
4680 mono_register_jit_icall (mono_amd64_throw_exception, "mono_amd64_throw_exception", mono_create_icall_signature ("void"), TRUE);
4684 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
4686 MonoJumpInfo *patch_info;
4687 gboolean compile_aot = !run_cctors;
4689 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
4690 unsigned char *ip = patch_info->ip.i + code;
4691 unsigned char *target;
4693 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
4696 switch (patch_info->type) {
4697 case MONO_PATCH_INFO_BB:
4698 case MONO_PATCH_INFO_LABEL:
4701 /* No need to patch these */
4706 switch (patch_info->type) {
4707 case MONO_PATCH_INFO_NONE:
4709 case MONO_PATCH_INFO_METHOD_REL:
4710 case MONO_PATCH_INFO_R8:
4711 case MONO_PATCH_INFO_R4:
4712 g_assert_not_reached ();
4714 case MONO_PATCH_INFO_BB:
4721 * Debug code to help track down problems where the target of a near call is
4724 if (amd64_is_near_call (ip)) {
4725 gint64 disp = (guint8*)target - (guint8*)ip;
4727 if (!amd64_is_imm32 (disp)) {
4728 printf ("TYPE: %d\n", patch_info->type);
4729 switch (patch_info->type) {
4730 case MONO_PATCH_INFO_INTERNAL_METHOD:
4731 printf ("V: %s\n", patch_info->data.name);
4733 case MONO_PATCH_INFO_METHOD_JUMP:
4734 case MONO_PATCH_INFO_METHOD:
4735 printf ("V: %s\n", patch_info->data.method->name);
4743 amd64_patch (ip, (gpointer)target);
4748 get_max_epilog_size (MonoCompile *cfg)
4750 int max_epilog_size = 16;
4752 if (cfg->method->save_lmf)
4753 max_epilog_size += 256;
4755 if (mono_jit_trace_calls != NULL)
4756 max_epilog_size += 50;
4758 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
4759 max_epilog_size += 50;
4761 max_epilog_size += (AMD64_NREG * 2);
4763 return max_epilog_size;
4767 * This macro is used for testing whenever the unwinder works correctly at every point
4768 * where an async exception can happen.
4770 /* This will generate a SIGSEGV at the given point in the code */
4771 #define async_exc_point(code) do { \
4772 if (mono_inject_async_exc_method && mono_method_desc_full_match (mono_inject_async_exc_method, cfg->method)) { \
4773 if (cfg->arch.async_point_count == mono_inject_async_exc_pos) \
4774 amd64_mov_reg_mem (code, AMD64_RAX, 0, 4); \
4775 cfg->arch.async_point_count ++; \
4780 mono_arch_emit_prolog (MonoCompile *cfg)
4782 MonoMethod *method = cfg->method;
4784 MonoMethodSignature *sig;
4786 int alloc_size, pos, max_offset, i, quad, max_epilog_size;
4789 gint32 lmf_offset = cfg->arch.lmf_offset;
4790 gboolean args_clobbered = FALSE;
4791 gboolean trace = FALSE;
4793 cfg->code_size = MAX (((MonoMethodNormal *)method)->header->code_size * 4, 10240);
4795 code = cfg->native_code = g_malloc (cfg->code_size);
4797 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
4800 /* Amount of stack space allocated by register saving code */
4804 * The prolog consists of the following parts:
4806 * - push rbp, mov rbp, rsp
4807 * - save callee saved regs using pushes
4809 * - save rgctx if needed
4810 * - save lmf if needed
4813 * - save rgctx if needed
4814 * - save lmf if needed
4815 * - save callee saved regs using moves
4818 async_exc_point (code);
4820 if (!cfg->arch.omit_fp) {
4821 amd64_push_reg (code, AMD64_RBP);
4822 async_exc_point (code);
4823 #ifdef PLATFORM_WIN32
4824 mono_arch_unwindinfo_add_push_nonvol (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
4827 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof (gpointer));
4828 async_exc_point (code);
4829 #ifdef PLATFORM_WIN32
4830 mono_arch_unwindinfo_add_set_fpreg (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
4834 /* Save callee saved registers */
4835 if (!cfg->arch.omit_fp && !method->save_lmf) {
4836 for (i = 0; i < AMD64_NREG; ++i)
4837 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
4838 amd64_push_reg (code, i);
4839 pos += sizeof (gpointer);
4840 async_exc_point (code);
4844 if (cfg->arch.omit_fp) {
4846 * On enter, the stack is misaligned by the the pushing of the return
4847 * address. It is either made aligned by the pushing of %rbp, or by
4850 alloc_size = ALIGN_TO (cfg->stack_offset, 8);
4851 if ((alloc_size % 16) == 0)
4854 alloc_size = ALIGN_TO (cfg->stack_offset, MONO_ARCH_FRAME_ALIGNMENT);
4859 cfg->arch.stack_alloc_size = alloc_size;
4861 /* Allocate stack frame */
4863 /* See mono_emit_stack_alloc */
4864 #if defined(PLATFORM_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
4865 guint32 remaining_size = alloc_size;
4866 while (remaining_size >= 0x1000) {
4867 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
4868 async_exc_point (code);
4869 #ifdef PLATFORM_WIN32
4870 if (cfg->arch.omit_fp)
4871 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, 0x1000);
4874 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
4875 remaining_size -= 0x1000;
4877 if (remaining_size) {
4878 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, remaining_size);
4879 async_exc_point (code);
4880 #ifdef PLATFORM_WIN32
4881 if (cfg->arch.omit_fp)
4882 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, remaining_size);
4886 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, alloc_size);
4887 async_exc_point (code);
4891 /* Stack alignment check */
4894 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_RSP, 8);
4895 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0xf);
4896 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, 0);
4897 x86_branch8 (code, X86_CC_EQ, 2, FALSE);
4898 amd64_breakpoint (code);
4903 if (method->save_lmf) {
4905 * The ip field is not set, the exception handling code will obtain it from the stack location pointed to by the sp field.
4907 /* sp is saved right before calls */
4908 /* Skip method (only needed for trampoline LMF frames) */
4909 /* Save callee saved regs */
4910 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbx), AMD64_RBX, 8);
4911 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbp), AMD64_RBP, 8);
4912 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r12), AMD64_R12, 8);
4913 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r13), AMD64_R13, 8);
4914 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r14), AMD64_R14, 8);
4915 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), AMD64_R15, 8);
4918 /* Save callee saved registers */
4919 if (cfg->arch.omit_fp && !method->save_lmf) {
4920 gint32 save_area_offset = cfg->arch.reg_save_area_offset;
4922 /* Save caller saved registers after sp is adjusted */
4923 /* The registers are saved at the bottom of the frame */
4924 /* FIXME: Optimize this so the regs are saved at the end of the frame in increasing order */
4925 for (i = 0; i < AMD64_NREG; ++i)
4926 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
4927 amd64_mov_membase_reg (code, AMD64_RSP, save_area_offset, i, 8);
4928 save_area_offset += 8;
4929 async_exc_point (code);
4933 /* store runtime generic context */
4934 if (cfg->rgctx_var) {
4935 g_assert (cfg->rgctx_var->opcode == OP_REGOFFSET &&
4936 (cfg->rgctx_var->inst_basereg == AMD64_RBP || cfg->rgctx_var->inst_basereg == AMD64_RSP));
4938 amd64_mov_membase_reg (code, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, MONO_ARCH_RGCTX_REG, 8);
4941 /* compute max_offset in order to use short forward jumps */
4943 max_epilog_size = get_max_epilog_size (cfg);
4944 if (cfg->opt & MONO_OPT_BRANCH) {
4945 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4947 bb->max_offset = max_offset;
4949 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
4951 /* max alignment for loops */
4952 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
4953 max_offset += LOOP_ALIGNMENT;
4955 MONO_BB_FOR_EACH_INS (bb, ins) {
4956 if (ins->opcode == OP_LABEL)
4957 ins->inst_c1 = max_offset;
4959 max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
4962 if (mono_jit_trace_calls && bb == cfg->bb_exit)
4963 /* The tracing code can be quite large */
4964 max_offset += max_epilog_size;
4968 sig = mono_method_signature (method);
4971 cinfo = cfg->arch.cinfo;
4973 if (sig->ret->type != MONO_TYPE_VOID) {
4974 /* Save volatile arguments to the stack */
4975 if (cfg->vret_addr && (cfg->vret_addr->opcode != OP_REGVAR))
4976 amd64_mov_membase_reg (code, cfg->vret_addr->inst_basereg, cfg->vret_addr->inst_offset, cinfo->ret.reg, 8);
4979 /* Keep this in sync with emit_load_volatile_arguments */
4980 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
4981 ArgInfo *ainfo = cinfo->args + i;
4982 gint32 stack_offset;
4985 ins = cfg->args [i];
4987 if ((ins->flags & MONO_INST_IS_DEAD) && !trace)
4988 /* Unused arguments */
4991 if (sig->hasthis && (i == 0))
4992 arg_type = &mono_defaults.object_class->byval_arg;
4994 arg_type = sig->params [i - sig->hasthis];
4996 stack_offset = ainfo->offset + ARGS_OFFSET;
4998 if (cfg->globalra) {
4999 /* All the other moves are done by the register allocator */
5000 switch (ainfo->storage) {
5001 case ArgInFloatSSEReg:
5002 amd64_sse_cvtss2sd_reg_reg (code, ainfo->reg, ainfo->reg);
5004 case ArgValuetypeInReg:
5005 for (quad = 0; quad < 2; quad ++) {
5006 switch (ainfo->pair_storage [quad]) {
5008 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad], sizeof (gpointer));
5010 case ArgInFloatSSEReg:
5011 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad]);
5013 case ArgInDoubleSSEReg:
5014 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad]);
5019 g_assert_not_reached ();
5030 /* Save volatile arguments to the stack */
5031 if (ins->opcode != OP_REGVAR) {
5032 switch (ainfo->storage) {
5038 if (stack_offset & 0x1)
5040 else if (stack_offset & 0x2)
5042 else if (stack_offset & 0x4)
5047 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg, size);
5050 case ArgInFloatSSEReg:
5051 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
5053 case ArgInDoubleSSEReg:
5054 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
5056 case ArgValuetypeInReg:
5057 for (quad = 0; quad < 2; quad ++) {
5058 switch (ainfo->pair_storage [quad]) {
5060 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad], sizeof (gpointer));
5062 case ArgInFloatSSEReg:
5063 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad]);
5065 case ArgInDoubleSSEReg:
5066 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad]);
5071 g_assert_not_reached ();
5075 case ArgValuetypeAddrInIReg:
5076 if (ainfo->pair_storage [0] == ArgInIReg)
5077 amd64_mov_membase_reg (code, ins->inst_left->inst_basereg, ins->inst_left->inst_offset, ainfo->pair_regs [0], sizeof (gpointer));
5083 /* Argument allocated to (non-volatile) register */
5084 switch (ainfo->storage) {
5086 amd64_mov_reg_reg (code, ins->dreg, ainfo->reg, 8);
5089 amd64_mov_reg_membase (code, ins->dreg, AMD64_RBP, ARGS_OFFSET + ainfo->offset, 8);
5092 g_assert_not_reached ();
5097 /* Might need to attach the thread to the JIT or change the domain for the callback */
5098 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
5099 guint64 domain = (guint64)cfg->domain;
5101 args_clobbered = TRUE;
5104 * The call might clobber argument registers, but they are already
5105 * saved to the stack/global regs.
5107 if (appdomain_tls_offset != -1 && lmf_tls_offset != -1) {
5108 guint8 *buf, *no_domain_branch;
5110 code = emit_tls_get (code, AMD64_RAX, appdomain_tls_offset);
5111 if ((domain >> 32) == 0)
5112 amd64_mov_reg_imm_size (code, AMD64_ARG_REG1, domain, 4);
5114 amd64_mov_reg_imm_size (code, AMD64_ARG_REG1, domain, 8);
5115 amd64_alu_reg_reg (code, X86_CMP, AMD64_RAX, AMD64_ARG_REG1);
5116 no_domain_branch = code;
5117 x86_branch8 (code, X86_CC_NE, 0, 0);
5118 code = emit_tls_get ( code, AMD64_RAX, lmf_addr_tls_offset);
5119 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
5121 x86_branch8 (code, X86_CC_NE, 0, 0);
5122 amd64_patch (no_domain_branch, code);
5123 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
5124 (gpointer)"mono_jit_thread_attach", TRUE);
5125 amd64_patch (buf, code);
5126 #ifdef PLATFORM_WIN32
5127 /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
5128 /* FIXME: Add a separate key for LMF to avoid this */
5129 amd64_alu_reg_imm (code, X86_ADD, AMD64_RAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
5132 g_assert (!cfg->compile_aot);
5133 if ((domain >> 32) == 0)
5134 amd64_mov_reg_imm_size (code, AMD64_ARG_REG1, domain, 4);
5136 amd64_mov_reg_imm_size (code, AMD64_ARG_REG1, domain, 8);
5137 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
5138 (gpointer)"mono_jit_thread_attach", TRUE);
5142 if (method->save_lmf) {
5143 if ((lmf_tls_offset != -1) && !optimize_for_xen) {
5145 * Optimized version which uses the mono_lmf TLS variable instead of indirection
5146 * through the mono_lmf_addr TLS variable.
5148 /* %rax = previous_lmf */
5149 x86_prefix (code, X86_FS_PREFIX);
5150 amd64_mov_reg_mem (code, AMD64_RAX, lmf_tls_offset, 8);
5152 /* Save previous_lmf */
5153 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_RAX, 8);
5155 if (lmf_offset == 0) {
5156 x86_prefix (code, X86_FS_PREFIX);
5157 amd64_mov_mem_reg (code, lmf_tls_offset, cfg->frame_reg, 8);
5159 amd64_lea_membase (code, AMD64_R11, cfg->frame_reg, lmf_offset);
5160 x86_prefix (code, X86_FS_PREFIX);
5161 amd64_mov_mem_reg (code, lmf_tls_offset, AMD64_R11, 8);
5164 if (lmf_addr_tls_offset != -1) {
5165 /* Load lmf quicky using the FS register */
5166 code = emit_tls_get (code, AMD64_RAX, lmf_addr_tls_offset);
5167 #ifdef PLATFORM_WIN32
5168 /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
5169 /* FIXME: Add a separate key for LMF to avoid this */
5170 amd64_alu_reg_imm (code, X86_ADD, AMD64_RAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
5175 * The call might clobber argument registers, but they are already
5176 * saved to the stack/global regs.
5178 args_clobbered = TRUE;
5179 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
5180 (gpointer)"mono_get_lmf_addr", TRUE);
5184 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), AMD64_RAX, 8);
5185 /* Save previous_lmf */
5186 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RAX, 0, 8);
5187 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_R11, 8);
5189 amd64_lea_membase (code, AMD64_R11, cfg->frame_reg, lmf_offset);
5190 amd64_mov_membase_reg (code, AMD64_RAX, 0, AMD64_R11, 8);
5195 args_clobbered = TRUE;
5196 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
5199 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
5200 args_clobbered = TRUE;
5203 * Optimize the common case of the first bblock making a call with the same
5204 * arguments as the method. This works because the arguments are still in their
5205 * original argument registers.
5206 * FIXME: Generalize this
5208 if (!args_clobbered) {
5209 MonoBasicBlock *first_bb = cfg->bb_entry;
5212 next = mono_bb_first_ins (first_bb);
5213 if (!next && first_bb->next_bb) {
5214 first_bb = first_bb->next_bb;
5215 next = mono_bb_first_ins (first_bb);
5218 if (first_bb->in_count > 1)
5221 for (i = 0; next && i < sig->param_count + sig->hasthis; ++i) {
5222 ArgInfo *ainfo = cinfo->args + i;
5223 gboolean match = FALSE;
5225 ins = cfg->args [i];
5226 if (ins->opcode != OP_REGVAR) {
5227 switch (ainfo->storage) {
5229 if (((next->opcode == OP_LOAD_MEMBASE) || (next->opcode == OP_LOADI4_MEMBASE)) && next->inst_basereg == ins->inst_basereg && next->inst_offset == ins->inst_offset) {
5230 if (next->dreg == ainfo->reg) {
5234 next->opcode = OP_MOVE;
5235 next->sreg1 = ainfo->reg;
5236 /* Only continue if the instruction doesn't change argument regs */
5237 if (next->dreg == ainfo->reg || next->dreg == AMD64_RAX)
5247 /* Argument allocated to (non-volatile) register */
5248 switch (ainfo->storage) {
5250 if (next->opcode == OP_MOVE && next->sreg1 == ins->dreg && next->dreg == ainfo->reg) {
5262 //next = mono_inst_list_next (&next->node, &first_bb->ins_list);
5269 cfg->code_len = code - cfg->native_code;
5271 g_assert (cfg->code_len < cfg->code_size);
5277 mono_arch_emit_epilog (MonoCompile *cfg)
5279 MonoMethod *method = cfg->method;
5282 int max_epilog_size;
5284 gint32 lmf_offset = cfg->arch.lmf_offset;
5286 max_epilog_size = get_max_epilog_size (cfg);
5288 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
5289 cfg->code_size *= 2;
5290 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
5291 mono_jit_stats.code_reallocs++;
5294 code = cfg->native_code + cfg->code_len;
5296 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
5297 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
5299 /* the code restoring the registers must be kept in sync with OP_JMP */
5302 if (method->save_lmf) {
5303 if ((lmf_tls_offset != -1) && !optimize_for_xen) {
5305 * Optimized version which uses the mono_lmf TLS variable instead of indirection
5306 * through the mono_lmf_addr TLS variable.
5308 /* reg = previous_lmf */
5309 amd64_mov_reg_membase (code, AMD64_R11, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 8);
5310 x86_prefix (code, X86_FS_PREFIX);
5311 amd64_mov_mem_reg (code, lmf_tls_offset, AMD64_R11, 8);
5313 /* Restore previous lmf */
5314 amd64_mov_reg_membase (code, AMD64_RCX, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 8);
5315 amd64_mov_reg_membase (code, AMD64_R11, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), 8);
5316 amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RCX, 8);
5319 /* Restore caller saved regs */
5320 if (cfg->used_int_regs & (1 << AMD64_RBP)) {
5321 amd64_mov_reg_membase (code, AMD64_RBP, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbp), 8);
5323 if (cfg->used_int_regs & (1 << AMD64_RBX)) {
5324 amd64_mov_reg_membase (code, AMD64_RBX, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbx), 8);
5326 if (cfg->used_int_regs & (1 << AMD64_R12)) {
5327 amd64_mov_reg_membase (code, AMD64_R12, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r12), 8);
5329 if (cfg->used_int_regs & (1 << AMD64_R13)) {
5330 amd64_mov_reg_membase (code, AMD64_R13, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r13), 8);
5332 if (cfg->used_int_regs & (1 << AMD64_R14)) {
5333 amd64_mov_reg_membase (code, AMD64_R14, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r14), 8);
5335 if (cfg->used_int_regs & (1 << AMD64_R15)) {
5336 amd64_mov_reg_membase (code, AMD64_R15, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), 8);
5340 if (cfg->arch.omit_fp) {
5341 gint32 save_area_offset = cfg->arch.reg_save_area_offset;
5343 for (i = 0; i < AMD64_NREG; ++i)
5344 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
5345 amd64_mov_reg_membase (code, i, AMD64_RSP, save_area_offset, 8);
5346 save_area_offset += 8;
5350 for (i = 0; i < AMD64_NREG; ++i)
5351 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i)))
5352 pos -= sizeof (gpointer);
5355 if (pos == - sizeof (gpointer)) {
5356 /* Only one register, so avoid lea */
5357 for (i = AMD64_NREG - 1; i > 0; --i)
5358 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
5359 amd64_mov_reg_membase (code, i, AMD64_RBP, pos, 8);
5363 amd64_lea_membase (code, AMD64_RSP, AMD64_RBP, pos);
5365 /* Pop registers in reverse order */
5366 for (i = AMD64_NREG - 1; i > 0; --i)
5367 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
5368 amd64_pop_reg (code, i);
5375 /* Load returned vtypes into registers if needed */
5376 cinfo = cfg->arch.cinfo;
5377 if (cinfo->ret.storage == ArgValuetypeInReg) {
5378 ArgInfo *ainfo = &cinfo->ret;
5379 MonoInst *inst = cfg->ret;
5381 for (quad = 0; quad < 2; quad ++) {
5382 switch (ainfo->pair_storage [quad]) {
5384 amd64_mov_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof (gpointer)), sizeof (gpointer));
5386 case ArgInFloatSSEReg:
5387 amd64_movss_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof (gpointer)));
5389 case ArgInDoubleSSEReg:
5390 amd64_movsd_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof (gpointer)));
5395 g_assert_not_reached ();
5400 if (cfg->arch.omit_fp) {
5401 if (cfg->arch.stack_alloc_size)
5402 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
5406 async_exc_point (code);
5409 cfg->code_len = code - cfg->native_code;
5411 g_assert (cfg->code_len < cfg->code_size);
5413 if (cfg->arch.omit_fp) {
5415 * Encode the stack size into used_int_regs so the exception handler
5418 g_assert (cfg->arch.stack_alloc_size < (1 << 16));
5419 cfg->used_int_regs |= (1 << 31) | (cfg->arch.stack_alloc_size << 16);
5424 mono_arch_emit_exceptions (MonoCompile *cfg)
5426 MonoJumpInfo *patch_info;
5429 MonoClass *exc_classes [16];
5430 guint8 *exc_throw_start [16], *exc_throw_end [16];
5431 guint32 code_size = 0;
5433 /* Compute needed space */
5434 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5435 if (patch_info->type == MONO_PATCH_INFO_EXC)
5437 if (patch_info->type == MONO_PATCH_INFO_R8)
5438 code_size += 8 + 15; /* sizeof (double) + alignment */
5439 if (patch_info->type == MONO_PATCH_INFO_R4)
5440 code_size += 4 + 15; /* sizeof (float) + alignment */
5443 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
5444 cfg->code_size *= 2;
5445 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
5446 mono_jit_stats.code_reallocs++;
5449 code = cfg->native_code + cfg->code_len;
5451 /* add code to raise exceptions */
5453 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5454 switch (patch_info->type) {
5455 case MONO_PATCH_INFO_EXC: {
5456 MonoClass *exc_class;
5460 amd64_patch (patch_info->ip.i + cfg->native_code, code);
5462 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
5463 g_assert (exc_class);
5464 throw_ip = patch_info->ip.i;
5466 //x86_breakpoint (code);
5467 /* Find a throw sequence for the same exception class */
5468 for (i = 0; i < nthrows; ++i)
5469 if (exc_classes [i] == exc_class)
5472 amd64_mov_reg_imm (code, AMD64_ARG_REG2, (exc_throw_end [i] - cfg->native_code) - throw_ip);
5473 x86_jump_code (code, exc_throw_start [i]);
5474 patch_info->type = MONO_PATCH_INFO_NONE;
5478 amd64_mov_reg_imm_size (code, AMD64_ARG_REG2, 0xf0f0f0f0, 4);
5482 exc_classes [nthrows] = exc_class;
5483 exc_throw_start [nthrows] = code;
5485 amd64_mov_reg_imm (code, AMD64_ARG_REG1, exc_class->type_token);
5487 patch_info->type = MONO_PATCH_INFO_NONE;
5489 code = emit_call_body (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_throw_corlib_exception");
5491 amd64_mov_reg_imm (buf, AMD64_ARG_REG2, (code - cfg->native_code) - throw_ip);
5496 exc_throw_end [nthrows] = code;
5508 /* Handle relocations with RIP relative addressing */
5509 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5510 gboolean remove = FALSE;
5512 switch (patch_info->type) {
5513 case MONO_PATCH_INFO_R8:
5514 case MONO_PATCH_INFO_R4: {
5517 /* The SSE opcodes require a 16 byte alignment */
5518 code = (guint8*)ALIGN_TO (code, 16);
5520 pos = cfg->native_code + patch_info->ip.i;
5522 if (IS_REX (pos [1]))
5523 *(guint32*)(pos + 5) = (guint8*)code - pos - 9;
5525 *(guint32*)(pos + 4) = (guint8*)code - pos - 8;
5527 if (patch_info->type == MONO_PATCH_INFO_R8) {
5528 *(double*)code = *(double*)patch_info->data.target;
5529 code += sizeof (double);
5531 *(float*)code = *(float*)patch_info->data.target;
5532 code += sizeof (float);
5543 if (patch_info == cfg->patch_info)
5544 cfg->patch_info = patch_info->next;
5548 for (tmp = cfg->patch_info; tmp->next != patch_info; tmp = tmp->next)
5550 tmp->next = patch_info->next;
5555 cfg->code_len = code - cfg->native_code;
5557 g_assert (cfg->code_len < cfg->code_size);
5562 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
5565 CallInfo *cinfo = NULL;
5566 MonoMethodSignature *sig;
5568 int i, n, stack_area = 0;
5570 /* Keep this in sync with mono_arch_get_argument_info */
5572 if (enable_arguments) {
5573 /* Allocate a new area on the stack and save arguments there */
5574 sig = mono_method_signature (cfg->method);
5576 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, FALSE);
5578 n = sig->param_count + sig->hasthis;
5580 stack_area = ALIGN_TO (n * 8, 16);
5582 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, stack_area);
5584 for (i = 0; i < n; ++i) {
5585 inst = cfg->args [i];
5587 if (inst->opcode == OP_REGVAR)
5588 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), inst->dreg, 8);
5590 amd64_mov_reg_membase (code, AMD64_R11, inst->inst_basereg, inst->inst_offset, 8);
5591 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), AMD64_R11, 8);
5596 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
5597 amd64_set_reg_template (code, AMD64_ARG_REG1);
5598 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RSP, 8);
5599 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
5601 if (enable_arguments)
5602 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, stack_area);
5616 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
5619 int save_mode = SAVE_NONE;
5620 MonoMethod *method = cfg->method;
5621 int rtype = mono_type_get_underlying_type (mono_method_signature (method)->ret)->type;
5624 case MONO_TYPE_VOID:
5625 /* special case string .ctor icall */
5626 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
5627 save_mode = SAVE_EAX;
5629 save_mode = SAVE_NONE;
5633 save_mode = SAVE_EAX;
5637 save_mode = SAVE_XMM;
5639 case MONO_TYPE_GENERICINST:
5640 if (!mono_type_generic_inst_is_valuetype (mono_method_signature (method)->ret)) {
5641 save_mode = SAVE_EAX;
5645 case MONO_TYPE_VALUETYPE:
5646 save_mode = SAVE_STRUCT;
5649 save_mode = SAVE_EAX;
5653 /* Save the result and copy it into the proper argument register */
5654 switch (save_mode) {
5656 amd64_push_reg (code, AMD64_RAX);
5658 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
5659 if (enable_arguments)
5660 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RAX, 8);
5664 if (enable_arguments)
5665 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
5668 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
5669 amd64_movsd_membase_reg (code, AMD64_RSP, 0, AMD64_XMM0);
5671 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
5673 * The result is already in the proper argument register so no copying
5680 g_assert_not_reached ();
5683 /* Set %al since this is a varargs call */
5684 if (save_mode == SAVE_XMM)
5685 amd64_mov_reg_imm (code, AMD64_RAX, 1);
5687 amd64_mov_reg_imm (code, AMD64_RAX, 0);
5689 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
5690 amd64_set_reg_template (code, AMD64_ARG_REG1);
5691 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
5693 /* Restore result */
5694 switch (save_mode) {
5696 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
5697 amd64_pop_reg (code, AMD64_RAX);
5703 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
5704 amd64_movsd_reg_membase (code, AMD64_XMM0, AMD64_RSP, 0);
5705 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
5710 g_assert_not_reached ();
5717 mono_arch_flush_icache (guint8 *code, gint size)
5723 mono_arch_flush_register_windows (void)
5728 mono_arch_is_inst_imm (gint64 imm)
5730 return amd64_is_imm32 (imm);
5734 * Determine whenever the trap whose info is in SIGINFO is caused by
5738 mono_arch_is_int_overflow (void *sigctx, void *info)
5745 mono_arch_sigctx_to_monoctx (sigctx, &ctx);
5747 rip = (guint8*)ctx.rip;
5749 if (IS_REX (rip [0])) {
5750 reg = amd64_rex_b (rip [0]);
5756 if ((rip [0] == 0xf7) && (x86_modrm_mod (rip [1]) == 0x3) && (x86_modrm_reg (rip [1]) == 0x7)) {
5758 reg += x86_modrm_rm (rip [1]);
5798 g_assert_not_reached ();
5810 mono_arch_get_patch_offset (guint8 *code)
5816 * mono_breakpoint_clean_code:
5818 * Copy @size bytes from @code - @offset to the buffer @buf. If the debugger inserted software
5819 * breakpoints in the original code, they are removed in the copy.
5821 * Returns TRUE if no sw breakpoint was present.
5824 mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size)
5827 gboolean can_write = TRUE;
5829 * If method_start is non-NULL we need to perform bound checks, since we access memory
5830 * at code - offset we could go before the start of the method and end up in a different
5831 * page of memory that is not mapped or read incorrect data anyway. We zero-fill the bytes
5834 if (!method_start || code - offset >= method_start) {
5835 memcpy (buf, code - offset, size);
5837 int diff = code - method_start;
5838 memset (buf, 0, size);
5839 memcpy (buf + offset - diff, method_start, diff + size - offset);
5842 for (i = 0; i < MONO_BREAKPOINT_ARRAY_SIZE; ++i) {
5843 int idx = mono_breakpoint_info_index [i];
5847 ptr = mono_breakpoint_info [idx].address;
5848 if (ptr >= code && ptr < code + size) {
5849 guint8 saved_byte = mono_breakpoint_info [idx].saved_byte;
5851 /*g_print ("patching %p with 0x%02x (was: 0x%02x)\n", ptr, saved_byte, buf [ptr - code]);*/
5852 buf [ptr - code] = saved_byte;
5859 mono_arch_get_vcall_slot (guint8 *code, gpointer *regs, int *displacement)
5866 mono_breakpoint_clean_code (NULL, code, 9, buf, sizeof (buf));
5871 /* go to the start of the call instruction
5873 * address_byte = (m << 6) | (o << 3) | reg
5874 * call opcode: 0xff address_byte displacement
5876 * 0xff m=2,o=2 imm32
5881 * A given byte sequence can match more than case here, so we have to be
5882 * really careful about the ordering of the cases. Longer sequences
5885 #ifdef MONO_ARCH_HAVE_IMT
5886 if ((code [-2] == 0x41) && (code [-1] == 0xbb) && (code [4] == 0xff) && (x86_modrm_mod (code [5]) == 1) && (x86_modrm_reg (code [5]) == 2) && ((signed char)code [6] < 0)) {
5887 /* IMT-based interface calls: with MONO_ARCH_IMT_REG == r11
5888 * 41 bb 14 f8 28 08 mov $0x828f814,%r11d
5889 * ff 50 fc call *0xfffffffc(%rax)
5891 reg = amd64_modrm_rm (code [5]);
5892 disp = (signed char)code [6];
5893 /* R10 is clobbered by the IMT thunk code */
5894 g_assert (reg != AMD64_R10);
5900 else if ((code [-1] == 0x8b) && (amd64_modrm_mod (code [0]) == 0x2) && (code [5] == 0xff) && (amd64_modrm_reg (code [6]) == 0x2) && (amd64_modrm_mod (code [6]) == 0x0)) {
5902 * This is a interface call
5903 * 48 8b 80 f0 e8 ff ff mov 0xffffffffffffe8f0(%rax),%rax
5904 * ff 10 callq *(%rax)
5906 if (IS_REX (code [4]))
5908 reg = amd64_modrm_rm (code [6]);
5910 /* R10 is clobbered by the IMT thunk code */
5911 g_assert (reg != AMD64_R10);
5912 } else if ((code [0] == 0x41) && (code [1] == 0xff) && (code [2] == 0x15)) {
5913 /* call OFFSET(%rip) */
5914 disp = *(guint32*)(code + 3);
5915 return (gpointer*)(code + disp + 7);
5916 } else if ((code [0] == 0xff) && (amd64_modrm_reg (code [1]) == 0x2) && (amd64_modrm_mod (code [1]) == 0x2) && (amd64_modrm_reg (code [2]) == X86_ESP) && (amd64_modrm_mod (code [2]) == 0) && (amd64_modrm_rm (code [2]) == X86_ESP)) {
5917 /* call *[r12+disp32] */
5918 if (IS_REX (code [-1]))
5921 disp = *(gint32*)(code + 3);
5922 } else if ((code [1] == 0xff) && (amd64_modrm_reg (code [2]) == 0x2) && (amd64_modrm_mod (code [2]) == 0x2)) {
5923 /* call *[reg+disp32] */
5924 if (IS_REX (code [0]))
5926 reg = amd64_modrm_rm (code [2]);
5927 disp = *(gint32*)(code + 3);
5928 /* R10 is clobbered by the IMT thunk code */
5929 g_assert (reg != AMD64_R10);
5930 } else if (code [2] == 0xe8) {
5933 } else if ((code [3] == 0xff) && (amd64_modrm_reg (code [4]) == 0x2) && (amd64_modrm_mod (code [4]) == 0x1) && (amd64_modrm_reg (code [5]) == X86_ESP) && (amd64_modrm_mod (code [5]) == 0) && (amd64_modrm_rm (code [5]) == X86_ESP)) {
5934 /* call *[r12+disp32] */
5935 if (IS_REX (code [2]))
5938 disp = *(gint8*)(code + 6);
5939 } else if (IS_REX (code [4]) && (code [5] == 0xff) && (amd64_modrm_reg (code [6]) == 0x2) && (amd64_modrm_mod (code [6]) == 0x3)) {
5942 } else if ((code [4] == 0xff) && (amd64_modrm_reg (code [5]) == 0x2) && (amd64_modrm_mod (code [5]) == 0x1)) {
5943 /* call *[reg+disp8] */
5944 if (IS_REX (code [3]))
5946 reg = amd64_modrm_rm (code [5]);
5947 disp = *(gint8*)(code + 6);
5948 //printf ("B: [%%r%d+0x%x]\n", reg, disp);
5950 else if ((code [5] == 0xff) && (amd64_modrm_reg (code [6]) == 0x2) && (amd64_modrm_mod (code [6]) == 0x0)) {
5952 * This is a interface call: should check the above code can't catch it earlier
5953 * 8b 40 30 mov 0x30(%eax),%eax
5954 * ff 10 call *(%eax)
5956 if (IS_REX (code [4]))
5958 reg = amd64_modrm_rm (code [6]);
5962 g_assert_not_reached ();
5964 reg += amd64_rex_b (rex);
5966 /* R11 is clobbered by the trampoline code */
5967 g_assert (reg != AMD64_R11);
5969 *displacement = disp;
5974 mono_arch_get_vcall_slot_addr (guint8* code, gpointer *regs)
5978 vt = mono_arch_get_vcall_slot (code, regs, &displacement);
5981 return (gpointer*)((char*)vt + displacement);
5985 mono_arch_get_this_arg_reg (MonoMethodSignature *sig, MonoGenericSharingContext *gsctx, guint8 *code)
5987 int this_reg = AMD64_ARG_REG1;
5989 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
5993 gsctx = mono_get_generic_context_from_code (code);
5995 cinfo = get_call_info (gsctx, NULL, sig, FALSE);
5997 if (cinfo->ret.storage != ArgValuetypeInReg)
5998 this_reg = AMD64_ARG_REG2;
6006 mono_arch_get_this_arg_from_call (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, gssize *regs, guint8 *code)
6008 return (gpointer)regs [mono_arch_get_this_arg_reg (sig, gsctx, code)];
6011 #define MAX_ARCH_DELEGATE_PARAMS 10
6014 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
6016 guint8 *code, *start;
6019 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
6022 /* FIXME: Support more cases */
6023 if (MONO_TYPE_ISSTRUCT (sig->ret))
6027 static guint8* cached = NULL;
6032 start = code = mono_global_codeman_reserve (64);
6034 /* Replace the this argument with the target */
6035 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
6036 amd64_mov_reg_membase (code, AMD64_ARG_REG1, AMD64_RAX, G_STRUCT_OFFSET (MonoDelegate, target), 8);
6037 amd64_jump_membase (code, AMD64_RAX, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
6039 g_assert ((code - start) < 64);
6041 mono_debug_add_delegate_trampoline (start, code - start);
6043 mono_memory_barrier ();
6047 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
6048 for (i = 0; i < sig->param_count; ++i)
6049 if (!mono_is_regsize_var (sig->params [i]))
6051 if (sig->param_count > 4)
6054 code = cache [sig->param_count];
6058 start = code = mono_global_codeman_reserve (64);
6060 if (sig->param_count == 0) {
6061 amd64_jump_membase (code, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
6063 /* We have to shift the arguments left */
6064 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
6065 for (i = 0; i < sig->param_count; ++i)
6066 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
6068 amd64_jump_membase (code, AMD64_RAX, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
6070 g_assert ((code - start) < 64);
6072 mono_debug_add_delegate_trampoline (start, code - start);
6074 mono_memory_barrier ();
6076 cache [sig->param_count] = start;
6083 * Support for fast access to the thread-local lmf structure using the GS
6084 * segment register on NPTL + kernel 2.6.x.
6087 static gboolean tls_offset_inited = FALSE;
6090 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
6092 if (!tls_offset_inited) {
6093 #ifdef PLATFORM_WIN32
6095 * We need to init this multiple times, since when we are first called, the key might not
6096 * be initialized yet.
6098 appdomain_tls_offset = mono_domain_get_tls_key ();
6099 lmf_tls_offset = mono_get_jit_tls_key ();
6100 thread_tls_offset = mono_thread_get_tls_key ();
6101 lmf_addr_tls_offset = mono_get_jit_tls_key ();
6103 /* Only 64 tls entries can be accessed using inline code */
6104 if (appdomain_tls_offset >= 64)
6105 appdomain_tls_offset = -1;
6106 if (lmf_tls_offset >= 64)
6107 lmf_tls_offset = -1;
6108 if (thread_tls_offset >= 64)
6109 thread_tls_offset = -1;
6111 tls_offset_inited = TRUE;
6113 optimize_for_xen = access ("/proc/xen", F_OK) == 0;
6115 appdomain_tls_offset = mono_domain_get_tls_offset ();
6116 lmf_tls_offset = mono_get_lmf_tls_offset ();
6117 lmf_addr_tls_offset = mono_get_lmf_addr_tls_offset ();
6118 thread_tls_offset = mono_thread_get_tls_offset ();
6124 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
6129 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
6131 MonoCallInst *call = (MonoCallInst*)inst;
6132 CallInfo * cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, inst->signature, FALSE);
6137 if (cinfo->ret.storage == ArgValuetypeInReg) {
6139 * The valuetype is in RAX:RDX after the call, need to be copied to
6140 * the stack. Save the address here, so the call instruction can
6143 MonoInst *loc = cfg->arch.vret_addr_loc;
6146 g_assert (loc->opcode == OP_REGOFFSET);
6148 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, loc->inst_basereg, loc->inst_offset, vt_reg);
6150 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
6151 vtarg->sreg1 = vt_reg;
6152 vtarg->dreg = mono_regstate_next_int (cfg->rs);
6153 mono_bblock_add_inst (cfg->cbb, vtarg);
6155 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
6159 /* add the this argument */
6160 if (this_reg != -1) {
6162 MONO_INST_NEW (cfg, this, OP_MOVE);
6163 this->type = this_type;
6164 this->sreg1 = this_reg;
6165 this->dreg = mono_regstate_next_int (cfg->rs);
6166 mono_bblock_add_inst (cfg->cbb, this);
6168 mono_call_inst_add_outarg_reg (cfg, call, this->dreg, cinfo->args [0].reg, FALSE);
6172 #ifdef MONO_ARCH_HAVE_IMT
6174 #define CMP_SIZE (6 + 1)
6175 #define CMP_REG_REG_SIZE (4 + 1)
6176 #define BR_SMALL_SIZE 2
6177 #define BR_LARGE_SIZE 6
6178 #define MOV_REG_IMM_SIZE 10
6179 #define MOV_REG_IMM_32BIT_SIZE 6
6180 #define JUMP_REG_SIZE (2 + 1)
6183 imt_branch_distance (MonoIMTCheckItem **imt_entries, int start, int target)
6185 int i, distance = 0;
6186 for (i = start; i < target; ++i)
6187 distance += imt_entries [i]->chunk_size;
6192 * LOCKING: called with the domain lock held
6195 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count)
6199 guint8 *code, *start;
6200 gboolean vtable_is_32bit = ((gsize)(vtable) == (gsize)(int)(gsize)(vtable));
6202 for (i = 0; i < count; ++i) {
6203 MonoIMTCheckItem *item = imt_entries [i];
6204 if (item->is_equals) {
6205 if (item->check_target_idx) {
6206 if (!item->compare_done) {
6207 if (amd64_is_imm32 (item->method))
6208 item->chunk_size += CMP_SIZE;
6210 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
6212 if (vtable_is_32bit)
6213 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
6215 item->chunk_size += MOV_REG_IMM_SIZE;
6216 item->chunk_size += BR_SMALL_SIZE + JUMP_REG_SIZE;
6218 if (vtable_is_32bit)
6219 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
6221 item->chunk_size += MOV_REG_IMM_SIZE;
6222 item->chunk_size += JUMP_REG_SIZE;
6223 /* with assert below:
6224 * item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
6228 if (amd64_is_imm32 (item->method))
6229 item->chunk_size += CMP_SIZE;
6231 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
6232 item->chunk_size += BR_LARGE_SIZE;
6233 imt_entries [item->check_target_idx]->compare_done = TRUE;
6235 size += item->chunk_size;
6237 code = mono_code_manager_reserve (domain->code_mp, size);
6239 for (i = 0; i < count; ++i) {
6240 MonoIMTCheckItem *item = imt_entries [i];
6241 item->code_target = code;
6242 if (item->is_equals) {
6243 if (item->check_target_idx) {
6244 if (!item->compare_done) {
6245 if (amd64_is_imm32 (item->method))
6246 amd64_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->method);
6248 amd64_mov_reg_imm (code, AMD64_R10, item->method);
6249 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, AMD64_R10);
6252 item->jmp_code = code;
6253 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
6254 /* See the comment below about R10 */
6255 amd64_mov_reg_imm (code, AMD64_R10, & (vtable->vtable [item->vtable_slot]));
6256 amd64_jump_membase (code, AMD64_R10, 0);
6258 /* enable the commented code to assert on wrong method */
6260 if (amd64_is_imm32 (item->method))
6261 amd64_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->method);
6263 amd64_mov_reg_imm (code, AMD64_R10, item->method);
6264 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, AMD64_R10);
6266 item->jmp_code = code;
6267 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
6268 /* See the comment below about R10 */
6269 amd64_mov_reg_imm (code, AMD64_R10, & (vtable->vtable [item->vtable_slot]));
6270 amd64_jump_membase (code, AMD64_R10, 0);
6271 amd64_patch (item->jmp_code, code);
6272 amd64_breakpoint (code);
6273 item->jmp_code = NULL;
6275 /* We're using R10 here because R11
6276 needs to be preserved. R10 needs
6277 to be preserved for calls which
6278 require a runtime generic context,
6279 but interface calls don't. */
6280 amd64_mov_reg_imm (code, AMD64_R10, & (vtable->vtable [item->vtable_slot]));
6281 amd64_jump_membase (code, AMD64_R10, 0);
6285 if (amd64_is_imm32 (item->method))
6286 amd64_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->method);
6288 amd64_mov_reg_imm (code, AMD64_R10, item->method);
6289 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, AMD64_R10);
6291 item->jmp_code = code;
6292 if (x86_is_imm8 (imt_branch_distance (imt_entries, i, item->check_target_idx)))
6293 x86_branch8 (code, X86_CC_GE, 0, FALSE);
6295 x86_branch32 (code, X86_CC_GE, 0, FALSE);
6297 g_assert (code - item->code_target <= item->chunk_size);
6299 /* patch the branches to get to the target items */
6300 for (i = 0; i < count; ++i) {
6301 MonoIMTCheckItem *item = imt_entries [i];
6302 if (item->jmp_code) {
6303 if (item->check_target_idx) {
6304 amd64_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
6309 mono_stats.imt_thunks_size += code - start;
6310 g_assert (code - start <= size);
6316 mono_arch_find_imt_method (gpointer *regs, guint8 *code)
6318 return regs [MONO_ARCH_IMT_REG];
6322 mono_arch_find_this_argument (gpointer *regs, MonoMethod *method, MonoGenericSharingContext *gsctx)
6324 return mono_arch_get_this_arg_from_call (gsctx, mono_method_signature (method), (gssize*)regs, NULL);
6328 mono_arch_emit_imt_argument (MonoCompile *cfg, MonoCallInst *call)
6330 /* Done by the implementation of the CALL_MEMBASE opcodes */
6335 mono_arch_find_static_call_vtable (gpointer *regs, guint8 *code)
6337 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
6341 mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
6343 MonoInst *ins = NULL;
6345 if (cmethod->klass == mono_defaults.math_class) {
6346 if (strcmp (cmethod->name, "Sin") == 0) {
6347 MONO_INST_NEW (cfg, ins, OP_SIN);
6348 ins->inst_i0 = args [0];
6349 } else if (strcmp (cmethod->name, "Cos") == 0) {
6350 MONO_INST_NEW (cfg, ins, OP_COS);
6351 ins->inst_i0 = args [0];
6352 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
6353 MONO_INST_NEW (cfg, ins, OP_SQRT);
6354 ins->inst_i0 = args [0];
6355 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
6356 MONO_INST_NEW (cfg, ins, OP_ABS);
6357 ins->inst_i0 = args [0];
6360 if (cfg->opt & MONO_OPT_CMOV) {
6363 if (strcmp (cmethod->name, "Min") == 0) {
6364 if (fsig->params [0]->type == MONO_TYPE_I4)
6366 if (fsig->params [0]->type == MONO_TYPE_U4)
6367 opcode = OP_IMIN_UN;
6368 else if (fsig->params [0]->type == MONO_TYPE_I8)
6370 else if (fsig->params [0]->type == MONO_TYPE_U8)
6371 opcode = OP_LMIN_UN;
6372 } else if (strcmp (cmethod->name, "Max") == 0) {
6373 if (fsig->params [0]->type == MONO_TYPE_I4)
6375 if (fsig->params [0]->type == MONO_TYPE_U4)
6376 opcode = OP_IMAX_UN;
6377 else if (fsig->params [0]->type == MONO_TYPE_I8)
6379 else if (fsig->params [0]->type == MONO_TYPE_U8)
6380 opcode = OP_LMAX_UN;
6384 MONO_INST_NEW (cfg, ins, opcode);
6385 ins->inst_i0 = args [0];
6386 ins->inst_i1 = args [1];
6391 /* OP_FREM is not IEEE compatible */
6392 else if (strcmp (cmethod->name, "IEEERemainder") == 0) {
6393 MONO_INST_NEW (cfg, ins, OP_FREM);
6394 ins->inst_i0 = args [0];
6395 ins->inst_i1 = args [1];
6404 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
6406 MonoInst *ins = NULL;
6409 if (cmethod->klass == mono_defaults.math_class) {
6410 if (strcmp (cmethod->name, "Sin") == 0) {
6412 } else if (strcmp (cmethod->name, "Cos") == 0) {
6414 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
6416 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
6421 MONO_INST_NEW (cfg, ins, opcode);
6422 ins->type = STACK_R8;
6423 ins->dreg = mono_alloc_freg (cfg);
6424 ins->sreg1 = args [0]->dreg;
6425 MONO_ADD_INS (cfg->cbb, ins);
6429 if (cfg->opt & MONO_OPT_CMOV) {
6430 if (strcmp (cmethod->name, "Min") == 0) {
6431 if (fsig->params [0]->type == MONO_TYPE_I4)
6433 if (fsig->params [0]->type == MONO_TYPE_U4)
6434 opcode = OP_IMIN_UN;
6435 else if (fsig->params [0]->type == MONO_TYPE_I8)
6437 else if (fsig->params [0]->type == MONO_TYPE_U8)
6438 opcode = OP_LMIN_UN;
6439 } else if (strcmp (cmethod->name, "Max") == 0) {
6440 if (fsig->params [0]->type == MONO_TYPE_I4)
6442 if (fsig->params [0]->type == MONO_TYPE_U4)
6443 opcode = OP_IMAX_UN;
6444 else if (fsig->params [0]->type == MONO_TYPE_I8)
6446 else if (fsig->params [0]->type == MONO_TYPE_U8)
6447 opcode = OP_LMAX_UN;
6452 MONO_INST_NEW (cfg, ins, opcode);
6453 ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
6454 ins->dreg = mono_alloc_ireg (cfg);
6455 ins->sreg1 = args [0]->dreg;
6456 ins->sreg2 = args [1]->dreg;
6457 MONO_ADD_INS (cfg->cbb, ins);
6461 /* OP_FREM is not IEEE compatible */
6462 else if (strcmp (cmethod->name, "IEEERemainder") == 0) {
6463 MONO_INST_NEW (cfg, ins, OP_FREM);
6464 ins->inst_i0 = args [0];
6465 ins->inst_i1 = args [1];
6471 * Can't implement CompareExchange methods this way since they have
6479 mono_arch_print_tree (MonoInst *tree, int arity)
6484 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
6488 if (appdomain_tls_offset == -1)
6491 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
6492 ins->inst_offset = appdomain_tls_offset;
6496 MonoInst* mono_arch_get_thread_intrinsic (MonoCompile* cfg)
6500 if (thread_tls_offset == -1)
6503 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
6504 ins->inst_offset = thread_tls_offset;
6508 #define _CTX_REG(ctx,fld,i) ((gpointer)((&ctx->fld)[i]))
6511 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
6514 case AMD64_RCX: return (gpointer)ctx->rcx;
6515 case AMD64_RDX: return (gpointer)ctx->rdx;
6516 case AMD64_RBX: return (gpointer)ctx->rbx;
6517 case AMD64_RBP: return (gpointer)ctx->rbp;
6518 case AMD64_RSP: return (gpointer)ctx->rsp;
6521 return _CTX_REG (ctx, rax, reg);
6523 return _CTX_REG (ctx, r12, reg - 12);
6525 g_assert_not_reached ();