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 = mini_type_get_underlying_type (NULL, 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 = NULL;
408 MonoGenericSharingContext tmp_gsctx;
411 * The gsctx currently contains no data, it is only used for checking whenever
412 * open types are allowed, some callers like mono_arch_get_argument_info ()
413 * don't pass it to us, so work around that.
418 klass = mono_class_from_mono_type (type);
419 size = mini_type_stack_size_full (gsctx, &klass->byval_arg, NULL, sig->pinvoke);
420 #ifndef PLATFORM_WIN32
421 if (!sig->pinvoke && !disable_vtypes_in_regs && ((is_return && (size == 8)) || (!is_return && (size <= 16)))) {
422 /* We pass and return vtypes of size 8 in a register */
423 } else if (!sig->pinvoke || (size == 0) || (size > 16)) {
427 /* Allways pass in memory */
428 ainfo->offset = *stack_size;
429 *stack_size += ALIGN_TO (size, 8);
430 ainfo->storage = ArgOnStack;
435 /* FIXME: Handle structs smaller than 8 bytes */
436 //if ((size % 8) != 0)
445 /* Always pass in 1 or 2 integer registers */
446 args [0] = ARG_CLASS_INTEGER;
447 args [1] = ARG_CLASS_INTEGER;
448 /* Only the simplest cases are supported */
449 if (is_return && nquads != 1) {
450 args [0] = ARG_CLASS_MEMORY;
451 args [1] = ARG_CLASS_MEMORY;
455 * Implement the algorithm from section 3.2.3 of the X86_64 ABI.
456 * The X87 and SSEUP stuff is left out since there are no such types in
459 info = mono_marshal_load_type_info (klass);
462 #ifndef PLATFORM_WIN32
463 if (info->native_size > 16) {
464 ainfo->offset = *stack_size;
465 *stack_size += ALIGN_TO (info->native_size, 8);
466 ainfo->storage = ArgOnStack;
471 switch (info->native_size) {
472 case 1: case 2: case 4: case 8:
476 ainfo->storage = ArgOnStack;
477 ainfo->offset = *stack_size;
478 *stack_size += ALIGN_TO (info->native_size, 8);
481 ainfo->storage = ArgValuetypeAddrInIReg;
483 if (*gr < PARAM_REGS) {
484 ainfo->pair_storage [0] = ArgInIReg;
485 ainfo->pair_regs [0] = param_regs [*gr];
489 ainfo->pair_storage [0] = ArgOnStack;
490 ainfo->offset = *stack_size;
499 args [0] = ARG_CLASS_NO_CLASS;
500 args [1] = ARG_CLASS_NO_CLASS;
501 for (quad = 0; quad < nquads; ++quad) {
504 ArgumentClass class1;
506 if (info->num_fields == 0)
507 class1 = ARG_CLASS_MEMORY;
509 class1 = ARG_CLASS_NO_CLASS;
510 for (i = 0; i < info->num_fields; ++i) {
511 size = mono_marshal_type_size (info->fields [i].field->type,
512 info->fields [i].mspec,
513 &align, TRUE, klass->unicode);
514 if ((info->fields [i].offset < 8) && (info->fields [i].offset + size) > 8) {
515 /* Unaligned field */
519 /* Skip fields in other quad */
520 if ((quad == 0) && (info->fields [i].offset >= 8))
522 if ((quad == 1) && (info->fields [i].offset < 8))
525 class1 = merge_argument_class_from_type (info->fields [i].field->type, class1);
527 g_assert (class1 != ARG_CLASS_NO_CLASS);
528 args [quad] = class1;
532 /* Post merger cleanup */
533 if ((args [0] == ARG_CLASS_MEMORY) || (args [1] == ARG_CLASS_MEMORY))
534 args [0] = args [1] = ARG_CLASS_MEMORY;
536 /* Allocate registers */
541 ainfo->storage = ArgValuetypeInReg;
542 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
543 for (quad = 0; quad < nquads; ++quad) {
544 switch (args [quad]) {
545 case ARG_CLASS_INTEGER:
546 if (*gr >= PARAM_REGS)
547 args [quad] = ARG_CLASS_MEMORY;
549 ainfo->pair_storage [quad] = ArgInIReg;
551 ainfo->pair_regs [quad] = return_regs [*gr];
553 ainfo->pair_regs [quad] = param_regs [*gr];
558 if (*fr >= FLOAT_PARAM_REGS)
559 args [quad] = ARG_CLASS_MEMORY;
561 ainfo->pair_storage [quad] = ArgInDoubleSSEReg;
562 ainfo->pair_regs [quad] = *fr;
566 case ARG_CLASS_MEMORY:
569 g_assert_not_reached ();
573 if ((args [0] == ARG_CLASS_MEMORY) || (args [1] == ARG_CLASS_MEMORY)) {
574 /* Revert possible register assignments */
578 ainfo->offset = *stack_size;
580 *stack_size += ALIGN_TO (info->native_size, 8);
582 *stack_size += nquads * sizeof (gpointer);
583 ainfo->storage = ArgOnStack;
591 * Obtain information about a call according to the calling convention.
592 * For AMD64, see the "System V ABI, x86-64 Architecture Processor Supplement
593 * Draft Version 0.23" document for more information.
596 get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSignature *sig, gboolean is_pinvoke)
600 int n = sig->hasthis + sig->param_count;
601 guint32 stack_size = 0;
605 cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
607 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
614 ret_type = mini_type_get_underlying_type (gsctx, sig->ret);
615 switch (ret_type->type) {
616 case MONO_TYPE_BOOLEAN:
627 case MONO_TYPE_FNPTR:
628 case MONO_TYPE_CLASS:
629 case MONO_TYPE_OBJECT:
630 case MONO_TYPE_SZARRAY:
631 case MONO_TYPE_ARRAY:
632 case MONO_TYPE_STRING:
633 cinfo->ret.storage = ArgInIReg;
634 cinfo->ret.reg = AMD64_RAX;
638 cinfo->ret.storage = ArgInIReg;
639 cinfo->ret.reg = AMD64_RAX;
642 cinfo->ret.storage = ArgInFloatSSEReg;
643 cinfo->ret.reg = AMD64_XMM0;
646 cinfo->ret.storage = ArgInDoubleSSEReg;
647 cinfo->ret.reg = AMD64_XMM0;
649 case MONO_TYPE_GENERICINST:
650 if (!mono_type_generic_inst_is_valuetype (sig->ret)) {
651 cinfo->ret.storage = ArgInIReg;
652 cinfo->ret.reg = AMD64_RAX;
656 case MONO_TYPE_VALUETYPE: {
657 guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
659 add_valuetype (gsctx, sig, &cinfo->ret, sig->ret, TRUE, &tmp_gr, &tmp_fr, &tmp_stacksize);
660 if (cinfo->ret.storage == ArgOnStack)
661 /* The caller passes the address where the value is stored */
662 add_general (&gr, &stack_size, &cinfo->ret);
665 case MONO_TYPE_TYPEDBYREF:
666 /* Same as a valuetype with size 24 */
667 add_general (&gr, &stack_size, &cinfo->ret);
673 g_error ("Can't handle as return value 0x%x", sig->ret->type);
679 add_general (&gr, &stack_size, cinfo->args + 0);
681 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
683 fr = FLOAT_PARAM_REGS;
685 /* Emit the signature cookie just before the implicit arguments */
686 add_general (&gr, &stack_size, &cinfo->sig_cookie);
689 for (i = 0; i < sig->param_count; ++i) {
690 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
693 #ifdef PLATFORM_WIN32
694 /* The float param registers and other param registers must be the same index on Windows x64.*/
701 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
702 /* We allways pass the sig cookie on the stack for simplicity */
704 * Prevent implicit arguments + the sig cookie from being passed
708 fr = FLOAT_PARAM_REGS;
710 /* Emit the signature cookie just before the implicit arguments */
711 add_general (&gr, &stack_size, &cinfo->sig_cookie);
714 if (sig->params [i]->byref) {
715 add_general (&gr, &stack_size, ainfo);
718 ptype = mini_type_get_underlying_type (gsctx, sig->params [i]);
719 switch (ptype->type) {
720 case MONO_TYPE_BOOLEAN:
723 add_general (&gr, &stack_size, ainfo);
728 add_general (&gr, &stack_size, ainfo);
732 add_general (&gr, &stack_size, ainfo);
737 case MONO_TYPE_FNPTR:
738 case MONO_TYPE_CLASS:
739 case MONO_TYPE_OBJECT:
740 case MONO_TYPE_STRING:
741 case MONO_TYPE_SZARRAY:
742 case MONO_TYPE_ARRAY:
743 add_general (&gr, &stack_size, ainfo);
745 case MONO_TYPE_GENERICINST:
746 if (!mono_type_generic_inst_is_valuetype (ptype)) {
747 add_general (&gr, &stack_size, ainfo);
751 case MONO_TYPE_VALUETYPE:
752 add_valuetype (gsctx, sig, ainfo, sig->params [i], FALSE, &gr, &fr, &stack_size);
754 case MONO_TYPE_TYPEDBYREF:
755 #ifdef PLATFORM_WIN32
756 add_valuetype (gsctx, sig, ainfo, sig->params [i], FALSE, &gr, &fr, &stack_size);
758 stack_size += sizeof (MonoTypedRef);
759 ainfo->storage = ArgOnStack;
764 add_general (&gr, &stack_size, ainfo);
767 add_float (&fr, &stack_size, ainfo, FALSE);
770 add_float (&fr, &stack_size, ainfo, TRUE);
773 g_assert_not_reached ();
777 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
779 fr = FLOAT_PARAM_REGS;
781 /* Emit the signature cookie just before the implicit arguments */
782 add_general (&gr, &stack_size, &cinfo->sig_cookie);
785 #ifdef PLATFORM_WIN32
786 // There always is 32 bytes reserved on the stack when calling on Winx64
790 if (stack_size & 0x8) {
791 /* The AMD64 ABI requires each stack frame to be 16 byte aligned */
792 cinfo->need_stack_align = TRUE;
796 cinfo->stack_usage = stack_size;
797 cinfo->reg_usage = gr;
798 cinfo->freg_usage = fr;
803 * mono_arch_get_argument_info:
804 * @csig: a method signature
805 * @param_count: the number of parameters to consider
806 * @arg_info: an array to store the result infos
808 * Gathers information on parameters such as size, alignment and
809 * padding. arg_info should be large enought to hold param_count + 1 entries.
811 * Returns the size of the argument area on the stack.
814 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
817 CallInfo *cinfo = get_call_info (NULL, NULL, csig, FALSE);
818 guint32 args_size = cinfo->stack_usage;
820 /* The arguments are saved to a stack area in mono_arch_instrument_prolog */
822 arg_info [0].offset = 0;
825 for (k = 0; k < param_count; k++) {
826 arg_info [k + 1].offset = ((k + csig->hasthis) * 8);
828 arg_info [k + 1].size = 0;
837 cpuid (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx)
840 __asm__ __volatile__ ("cpuid"
841 : "=a" (*p_eax), "=b" (*p_ebx), "=c" (*p_ecx), "=d" (*p_edx)
855 * Initialize the cpu to execute managed code.
858 mono_arch_cpu_init (void)
863 /* spec compliance requires running with double precision */
864 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
865 fpcw &= ~X86_FPCW_PRECC_MASK;
866 fpcw |= X86_FPCW_PREC_DOUBLE;
867 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
868 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
870 /* TODO: This is crashing on Win64 right now.
871 * _control87 (_PC_53, MCW_PC);
877 * Initialize architecture specific code.
880 mono_arch_init (void)
882 InitializeCriticalSection (&mini_arch_mutex);
886 * Cleanup architecture specific code.
889 mono_arch_cleanup (void)
891 DeleteCriticalSection (&mini_arch_mutex);
895 * This function returns the optimizations supported on this cpu.
898 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
900 int eax, ebx, ecx, edx;
906 /* Feature Flags function, flags returned in EDX. */
907 if (cpuid (1, &eax, &ebx, &ecx, &edx)) {
908 if (edx & (1 << 15)) {
909 opts |= MONO_OPT_CMOV;
911 opts |= MONO_OPT_FCMOV;
913 *exclude_mask |= MONO_OPT_FCMOV;
915 *exclude_mask |= MONO_OPT_CMOV;
922 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
927 for (i = 0; i < cfg->num_varinfo; i++) {
928 MonoInst *ins = cfg->varinfo [i];
929 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
932 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
935 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
936 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
939 if (mono_is_regsize_var (ins->inst_vtype)) {
940 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
941 g_assert (i == vmv->idx);
942 vars = g_list_prepend (vars, vmv);
946 vars = mono_varlist_sort (cfg, vars, 0);
952 * mono_arch_compute_omit_fp:
954 * Determine whenever the frame pointer can be eliminated.
957 mono_arch_compute_omit_fp (MonoCompile *cfg)
959 MonoMethodSignature *sig;
960 MonoMethodHeader *header;
964 if (cfg->arch.omit_fp_computed)
967 header = mono_method_get_header (cfg->method);
969 sig = mono_method_signature (cfg->method);
971 if (!cfg->arch.cinfo)
972 cfg->arch.cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, FALSE);
973 cinfo = cfg->arch.cinfo;
976 * FIXME: Remove some of the restrictions.
978 cfg->arch.omit_fp = TRUE;
979 cfg->arch.omit_fp_computed = TRUE;
981 if (cfg->disable_omit_fp)
982 cfg->arch.omit_fp = FALSE;
984 if (!debug_omit_fp ())
985 cfg->arch.omit_fp = FALSE;
987 if (cfg->method->save_lmf)
988 cfg->arch.omit_fp = FALSE;
990 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
991 cfg->arch.omit_fp = FALSE;
992 if (header->num_clauses)
993 cfg->arch.omit_fp = FALSE;
995 cfg->arch.omit_fp = FALSE;
996 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
997 cfg->arch.omit_fp = FALSE;
998 if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) ||
999 (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
1000 cfg->arch.omit_fp = FALSE;
1001 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1002 ArgInfo *ainfo = &cinfo->args [i];
1004 if (ainfo->storage == ArgOnStack) {
1006 * The stack offset can only be determined when the frame
1009 cfg->arch.omit_fp = FALSE;
1014 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1015 MonoInst *ins = cfg->varinfo [i];
1018 locals_size += mono_type_size (ins->inst_vtype, &ialign);
1021 if ((cfg->num_varinfo > 10000) || (locals_size >= (1 << 15))) {
1022 /* Avoid hitting the stack_alloc_size < (1 << 16) assertion in emit_epilog () */
1023 cfg->arch.omit_fp = FALSE;
1028 mono_arch_get_global_int_regs (MonoCompile *cfg)
1032 mono_arch_compute_omit_fp (cfg);
1034 if (cfg->globalra) {
1035 if (cfg->arch.omit_fp)
1036 regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
1038 regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
1039 regs = g_list_prepend (regs, (gpointer)AMD64_R12);
1040 regs = g_list_prepend (regs, (gpointer)AMD64_R13);
1041 regs = g_list_prepend (regs, (gpointer)AMD64_R14);
1042 regs = g_list_prepend (regs, (gpointer)AMD64_R15);
1044 regs = g_list_prepend (regs, (gpointer)AMD64_R10);
1045 regs = g_list_prepend (regs, (gpointer)AMD64_R9);
1046 regs = g_list_prepend (regs, (gpointer)AMD64_R8);
1047 regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
1048 regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
1049 regs = g_list_prepend (regs, (gpointer)AMD64_RDX);
1050 regs = g_list_prepend (regs, (gpointer)AMD64_RCX);
1051 regs = g_list_prepend (regs, (gpointer)AMD64_RAX);
1053 if (cfg->arch.omit_fp)
1054 regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
1056 /* We use the callee saved registers for global allocation */
1057 regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
1058 regs = g_list_prepend (regs, (gpointer)AMD64_R12);
1059 regs = g_list_prepend (regs, (gpointer)AMD64_R13);
1060 regs = g_list_prepend (regs, (gpointer)AMD64_R14);
1061 regs = g_list_prepend (regs, (gpointer)AMD64_R15);
1068 mono_arch_get_global_fp_regs (MonoCompile *cfg)
1073 /* All XMM registers */
1074 for (i = 0; i < 16; ++i)
1075 regs = g_list_prepend (regs, GINT_TO_POINTER (i));
1081 mono_arch_get_iregs_clobbered_by_call (MonoCallInst *call)
1083 static GList *r = NULL;
1088 regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
1089 regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
1090 regs = g_list_prepend (regs, (gpointer)AMD64_R12);
1091 regs = g_list_prepend (regs, (gpointer)AMD64_R13);
1092 regs = g_list_prepend (regs, (gpointer)AMD64_R14);
1093 regs = g_list_prepend (regs, (gpointer)AMD64_R15);
1095 regs = g_list_prepend (regs, (gpointer)AMD64_R10);
1096 regs = g_list_prepend (regs, (gpointer)AMD64_R9);
1097 regs = g_list_prepend (regs, (gpointer)AMD64_R8);
1098 regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
1099 regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
1100 regs = g_list_prepend (regs, (gpointer)AMD64_RDX);
1101 regs = g_list_prepend (regs, (gpointer)AMD64_RCX);
1102 regs = g_list_prepend (regs, (gpointer)AMD64_RAX);
1104 InterlockedCompareExchangePointer ((gpointer*)&r, regs, NULL);
1111 mono_arch_get_fregs_clobbered_by_call (MonoCallInst *call)
1114 static GList *r = NULL;
1119 for (i = 0; i < AMD64_XMM_NREG; ++i)
1120 regs = g_list_prepend (regs, GINT_TO_POINTER (MONO_MAX_IREGS + i));
1122 InterlockedCompareExchangePointer ((gpointer*)&r, regs, NULL);
1129 * mono_arch_regalloc_cost:
1131 * Return the cost, in number of memory references, of the action of
1132 * allocating the variable VMV into a register during global register
1136 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
1138 MonoInst *ins = cfg->varinfo [vmv->idx];
1140 if (cfg->method->save_lmf)
1141 /* The register is already saved */
1142 /* substract 1 for the invisible store in the prolog */
1143 return (ins->opcode == OP_ARG) ? 0 : 1;
1146 return (ins->opcode == OP_ARG) ? 1 : 2;
1150 * mono_arch_fill_argument_info:
1152 * Populate cfg->args, cfg->ret and cfg->vret_addr with information about the arguments
1156 mono_arch_fill_argument_info (MonoCompile *cfg)
1158 MonoMethodSignature *sig;
1159 MonoMethodHeader *header;
1164 header = mono_method_get_header (cfg->method);
1166 sig = mono_method_signature (cfg->method);
1168 cinfo = cfg->arch.cinfo;
1171 * Contrary to mono_arch_allocate_vars (), the information should describe
1172 * where the arguments are at the beginning of the method, not where they can be
1173 * accessed during the execution of the method. The later makes no sense for the
1174 * global register allocator, since a variable can be in more than one location.
1176 if (sig->ret->type != MONO_TYPE_VOID) {
1177 switch (cinfo->ret.storage) {
1179 case ArgInFloatSSEReg:
1180 case ArgInDoubleSSEReg:
1181 if ((MONO_TYPE_ISSTRUCT (sig->ret) && !mono_class_from_mono_type (sig->ret)->enumtype) || (sig->ret->type == MONO_TYPE_TYPEDBYREF)) {
1182 cfg->vret_addr->opcode = OP_REGVAR;
1183 cfg->vret_addr->inst_c0 = cinfo->ret.reg;
1186 cfg->ret->opcode = OP_REGVAR;
1187 cfg->ret->inst_c0 = cinfo->ret.reg;
1190 case ArgValuetypeInReg:
1191 cfg->ret->opcode = OP_REGOFFSET;
1192 cfg->ret->inst_basereg = -1;
1193 cfg->ret->inst_offset = -1;
1196 g_assert_not_reached ();
1200 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1201 ArgInfo *ainfo = &cinfo->args [i];
1204 ins = cfg->args [i];
1206 if (sig->hasthis && (i == 0))
1207 arg_type = &mono_defaults.object_class->byval_arg;
1209 arg_type = sig->params [i - sig->hasthis];
1211 switch (ainfo->storage) {
1213 case ArgInFloatSSEReg:
1214 case ArgInDoubleSSEReg:
1215 ins->opcode = OP_REGVAR;
1216 ins->inst_c0 = ainfo->reg;
1219 ins->opcode = OP_REGOFFSET;
1220 ins->inst_basereg = -1;
1221 ins->inst_offset = -1;
1223 case ArgValuetypeInReg:
1225 ins->opcode = OP_NOP;
1228 g_assert_not_reached ();
1234 mono_arch_allocate_vars (MonoCompile *cfg)
1236 MonoMethodSignature *sig;
1237 MonoMethodHeader *header;
1240 guint32 locals_stack_size, locals_stack_align;
1244 header = mono_method_get_header (cfg->method);
1246 sig = mono_method_signature (cfg->method);
1248 cinfo = cfg->arch.cinfo;
1250 mono_arch_compute_omit_fp (cfg);
1253 * We use the ABI calling conventions for managed code as well.
1254 * Exception: valuetypes are never passed or returned in registers.
1257 if (cfg->arch.omit_fp) {
1258 cfg->flags |= MONO_CFG_HAS_SPILLUP;
1259 cfg->frame_reg = AMD64_RSP;
1262 /* Locals are allocated backwards from %fp */
1263 cfg->frame_reg = AMD64_RBP;
1267 if (cfg->method->save_lmf) {
1268 /* Reserve stack space for saving LMF */
1269 /* mono_arch_find_jit_info () expects to find the LMF at a fixed offset */
1270 g_assert (offset == 0);
1271 if (cfg->arch.omit_fp) {
1272 cfg->arch.lmf_offset = offset;
1273 offset += sizeof (MonoLMF);
1276 offset += sizeof (MonoLMF);
1277 cfg->arch.lmf_offset = -offset;
1280 if (cfg->arch.omit_fp)
1281 cfg->arch.reg_save_area_offset = offset;
1282 /* Reserve space for caller saved registers */
1283 for (i = 0; i < AMD64_NREG; ++i)
1284 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
1285 offset += sizeof (gpointer);
1289 if (sig->ret->type != MONO_TYPE_VOID) {
1290 switch (cinfo->ret.storage) {
1292 case ArgInFloatSSEReg:
1293 case ArgInDoubleSSEReg:
1294 if ((MONO_TYPE_ISSTRUCT (sig->ret) && !mono_class_from_mono_type (sig->ret)->enumtype) || (sig->ret->type == MONO_TYPE_TYPEDBYREF)) {
1295 if (cfg->globalra) {
1296 cfg->vret_addr->opcode = OP_REGVAR;
1297 cfg->vret_addr->inst_c0 = cinfo->ret.reg;
1299 /* The register is volatile */
1300 cfg->vret_addr->opcode = OP_REGOFFSET;
1301 cfg->vret_addr->inst_basereg = cfg->frame_reg;
1302 if (cfg->arch.omit_fp) {
1303 cfg->vret_addr->inst_offset = offset;
1307 cfg->vret_addr->inst_offset = -offset;
1309 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1310 printf ("vret_addr =");
1311 mono_print_ins (cfg->vret_addr);
1316 cfg->ret->opcode = OP_REGVAR;
1317 cfg->ret->inst_c0 = cinfo->ret.reg;
1320 case ArgValuetypeInReg:
1321 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
1322 cfg->ret->opcode = OP_REGOFFSET;
1323 cfg->ret->inst_basereg = cfg->frame_reg;
1324 if (cfg->arch.omit_fp) {
1325 cfg->ret->inst_offset = offset;
1329 cfg->ret->inst_offset = - offset;
1333 g_assert_not_reached ();
1336 cfg->ret->dreg = cfg->ret->inst_c0;
1339 /* Allocate locals */
1340 if (!cfg->globalra) {
1341 offsets = mono_allocate_stack_slots_full (cfg, cfg->arch.omit_fp ? FALSE: TRUE, &locals_stack_size, &locals_stack_align);
1342 if (locals_stack_align) {
1343 offset += (locals_stack_align - 1);
1344 offset &= ~(locals_stack_align - 1);
1346 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1347 if (offsets [i] != -1) {
1348 MonoInst *ins = cfg->varinfo [i];
1349 ins->opcode = OP_REGOFFSET;
1350 ins->inst_basereg = cfg->frame_reg;
1351 if (cfg->arch.omit_fp)
1352 ins->inst_offset = (offset + offsets [i]);
1354 ins->inst_offset = - (offset + offsets [i]);
1355 //printf ("allocated local %d to ", i); mono_print_tree_nl (ins);
1358 offset += locals_stack_size;
1361 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG)) {
1362 g_assert (!cfg->arch.omit_fp);
1363 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
1364 cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
1367 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1368 ins = cfg->args [i];
1369 if (ins->opcode != OP_REGVAR) {
1370 ArgInfo *ainfo = &cinfo->args [i];
1371 gboolean inreg = TRUE;
1374 if (sig->hasthis && (i == 0))
1375 arg_type = &mono_defaults.object_class->byval_arg;
1377 arg_type = sig->params [i - sig->hasthis];
1379 if (cfg->globalra) {
1380 /* The new allocator needs info about the original locations of the arguments */
1381 switch (ainfo->storage) {
1383 case ArgInFloatSSEReg:
1384 case ArgInDoubleSSEReg:
1385 ins->opcode = OP_REGVAR;
1386 ins->inst_c0 = ainfo->reg;
1389 g_assert (!cfg->arch.omit_fp);
1390 ins->opcode = OP_REGOFFSET;
1391 ins->inst_basereg = cfg->frame_reg;
1392 ins->inst_offset = ainfo->offset + ARGS_OFFSET;
1394 case ArgValuetypeInReg:
1395 ins->opcode = OP_REGOFFSET;
1396 ins->inst_basereg = cfg->frame_reg;
1397 /* These arguments are saved to the stack in the prolog */
1398 offset = ALIGN_TO (offset, sizeof (gpointer));
1399 if (cfg->arch.omit_fp) {
1400 ins->inst_offset = offset;
1401 offset += (ainfo->storage == ArgValuetypeInReg) ? 2 * sizeof (gpointer) : sizeof (gpointer);
1403 offset += (ainfo->storage == ArgValuetypeInReg) ? 2 * sizeof (gpointer) : sizeof (gpointer);
1404 ins->inst_offset = - offset;
1408 g_assert_not_reached ();
1414 /* FIXME: Allocate volatile arguments to registers */
1415 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
1419 * Under AMD64, all registers used to pass arguments to functions
1420 * are volatile across calls.
1421 * FIXME: Optimize this.
1423 if ((ainfo->storage == ArgInIReg) || (ainfo->storage == ArgInFloatSSEReg) || (ainfo->storage == ArgInDoubleSSEReg) || (ainfo->storage == ArgValuetypeInReg))
1426 ins->opcode = OP_REGOFFSET;
1428 switch (ainfo->storage) {
1430 case ArgInFloatSSEReg:
1431 case ArgInDoubleSSEReg:
1433 ins->opcode = OP_REGVAR;
1434 ins->dreg = ainfo->reg;
1438 g_assert (!cfg->arch.omit_fp);
1439 ins->opcode = OP_REGOFFSET;
1440 ins->inst_basereg = cfg->frame_reg;
1441 ins->inst_offset = ainfo->offset + ARGS_OFFSET;
1443 case ArgValuetypeInReg:
1445 case ArgValuetypeAddrInIReg: {
1447 g_assert (!cfg->arch.omit_fp);
1449 MONO_INST_NEW (cfg, indir, 0);
1450 indir->opcode = OP_REGOFFSET;
1451 if (ainfo->pair_storage [0] == ArgInIReg) {
1452 indir->inst_basereg = cfg->frame_reg;
1453 offset = ALIGN_TO (offset, sizeof (gpointer));
1454 offset += (sizeof (gpointer));
1455 indir->inst_offset = - offset;
1458 indir->inst_basereg = cfg->frame_reg;
1459 indir->inst_offset = ainfo->offset + ARGS_OFFSET;
1462 ins->opcode = OP_VTARG_ADDR;
1463 ins->inst_left = indir;
1471 if (!inreg && (ainfo->storage != ArgOnStack) && (ainfo->storage != ArgValuetypeAddrInIReg)) {
1472 ins->opcode = OP_REGOFFSET;
1473 ins->inst_basereg = cfg->frame_reg;
1474 /* These arguments are saved to the stack in the prolog */
1475 offset = ALIGN_TO (offset, sizeof (gpointer));
1476 if (cfg->arch.omit_fp) {
1477 ins->inst_offset = offset;
1478 offset += (ainfo->storage == ArgValuetypeInReg) ? 2 * sizeof (gpointer) : sizeof (gpointer);
1480 offset += (ainfo->storage == ArgValuetypeInReg) ? 2 * sizeof (gpointer) : sizeof (gpointer);
1481 ins->inst_offset = - offset;
1487 cfg->stack_offset = offset;
1491 mono_arch_create_vars (MonoCompile *cfg)
1493 MonoMethodSignature *sig;
1496 sig = mono_method_signature (cfg->method);
1498 if (!cfg->arch.cinfo)
1499 cfg->arch.cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, FALSE);
1500 cinfo = cfg->arch.cinfo;
1502 if (cinfo->ret.storage == ArgValuetypeInReg)
1503 cfg->ret_var_is_local = TRUE;
1505 if ((cinfo->ret.storage != ArgValuetypeInReg) && MONO_TYPE_ISSTRUCT (sig->ret)) {
1506 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
1507 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1508 printf ("vret_addr = ");
1509 mono_print_ins (cfg->vret_addr);
1515 add_outarg_reg (MonoCompile *cfg, MonoCallInst *call, MonoInst *arg, ArgStorage storage, int reg, MonoInst *tree)
1519 arg->opcode = OP_OUTARG_REG;
1520 arg->inst_left = tree;
1521 arg->inst_call = call;
1522 arg->backend.reg3 = reg;
1524 case ArgInFloatSSEReg:
1525 arg->opcode = OP_AMD64_OUTARG_XMMREG_R4;
1526 arg->inst_left = tree;
1527 arg->inst_call = call;
1528 arg->backend.reg3 = reg;
1530 case ArgInDoubleSSEReg:
1531 arg->opcode = OP_AMD64_OUTARG_XMMREG_R8;
1532 arg->inst_left = tree;
1533 arg->inst_call = call;
1534 arg->backend.reg3 = reg;
1537 g_assert_not_reached ();
1542 add_outarg_reg2 (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int reg, MonoInst *tree)
1548 MONO_INST_NEW (cfg, ins, OP_MOVE);
1549 ins->dreg = mono_alloc_ireg (cfg);
1550 ins->sreg1 = tree->dreg;
1551 MONO_ADD_INS (cfg->cbb, ins);
1552 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, FALSE);
1554 case ArgInFloatSSEReg:
1555 MONO_INST_NEW (cfg, ins, OP_AMD64_SET_XMMREG_R4);
1556 ins->dreg = mono_alloc_freg (cfg);
1557 ins->sreg1 = tree->dreg;
1558 MONO_ADD_INS (cfg->cbb, ins);
1560 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
1562 case ArgInDoubleSSEReg:
1563 MONO_INST_NEW (cfg, ins, OP_FMOVE);
1564 ins->dreg = mono_alloc_freg (cfg);
1565 ins->sreg1 = tree->dreg;
1566 MONO_ADD_INS (cfg->cbb, ins);
1568 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
1572 g_assert_not_reached ();
1577 arg_storage_to_ldind (ArgStorage storage)
1582 case ArgInDoubleSSEReg:
1583 return CEE_LDIND_R8;
1584 case ArgInFloatSSEReg:
1585 return CEE_LDIND_R4;
1587 g_assert_not_reached ();
1594 arg_storage_to_load_membase (ArgStorage storage)
1598 return OP_LOAD_MEMBASE;
1599 case ArgInDoubleSSEReg:
1600 return OP_LOADR8_MEMBASE;
1601 case ArgInFloatSSEReg:
1602 return OP_LOADR4_MEMBASE;
1604 g_assert_not_reached ();
1611 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
1614 MonoMethodSignature *tmp_sig;
1617 /* FIXME: Add support for signature tokens to AOT */
1618 cfg->disable_aot = TRUE;
1620 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
1623 * mono_ArgIterator_Setup assumes the signature cookie is
1624 * passed first and all the arguments which were before it are
1625 * passed on the stack after the signature. So compensate by
1626 * passing a different signature.
1628 tmp_sig = mono_metadata_signature_dup (call->signature);
1629 tmp_sig->param_count -= call->signature->sentinelpos;
1630 tmp_sig->sentinelpos = 0;
1631 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
1633 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
1634 sig_arg->inst_p0 = tmp_sig;
1636 MONO_INST_NEW (cfg, arg, OP_OUTARG);
1637 arg->inst_left = sig_arg;
1638 arg->type = STACK_PTR;
1640 /* prepend, so they get reversed */
1641 arg->next = call->out_args;
1642 call->out_args = arg;
1646 * take the arguments and generate the arch-specific
1647 * instructions to properly call the function in call.
1648 * This includes pushing, moving arguments to the right register
1652 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
1654 MonoMethodSignature *sig;
1655 int i, n, stack_size;
1661 sig = call->signature;
1662 n = sig->param_count + sig->hasthis;
1664 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, sig->pinvoke);
1666 if (cfg->method->save_lmf) {
1667 MONO_INST_NEW (cfg, arg, OP_AMD64_SAVE_SP_TO_LMF);
1668 arg->next = call->out_args;
1669 call->out_args = arg;
1672 for (i = 0; i < n; ++i) {
1673 ainfo = cinfo->args + i;
1675 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1676 /* Emit the signature cookie just before the implicit arguments */
1677 emit_sig_cookie (cfg, call, cinfo);
1680 if (is_virtual && i == 0) {
1681 /* the argument will be attached to the call instruction */
1682 in = call->args [i];
1684 MONO_INST_NEW (cfg, arg, OP_OUTARG);
1685 in = call->args [i];
1686 arg->cil_code = in->cil_code;
1687 arg->inst_left = in;
1688 arg->type = in->type;
1689 /* prepend, so they get reversed */
1690 arg->next = call->out_args;
1691 call->out_args = arg;
1693 if (!cinfo->stack_usage)
1694 /* Keep the assignments to the arg registers in order if possible */
1695 MONO_INST_LIST_ADD_TAIL (&arg->node, &call->out_args);
1697 MONO_INST_LIST_ADD (&arg->node, &call->out_args);
1700 if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(sig->params [i - sig->hasthis]))) {
1704 if (sig->params [i - sig->hasthis]->type == MONO_TYPE_TYPEDBYREF) {
1705 size = sizeof (MonoTypedRef);
1706 align = sizeof (gpointer);
1710 size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
1713 * Other backends use mini_type_stack_size (), but that
1714 * aligns the size to 8, which is larger than the size of
1715 * the source, leading to reads of invalid memory if the
1716 * source is at the end of address space.
1718 size = mono_class_value_size (in->klass, &align);
1720 if (ainfo->storage == ArgValuetypeInReg) {
1721 if (ainfo->pair_storage [1] == ArgNone) {
1726 MONO_INST_NEW (cfg, load, arg_storage_to_ldind (ainfo->pair_storage [0]));
1727 load->inst_left = in;
1729 add_outarg_reg (cfg, call, arg, ainfo->pair_storage [0], ainfo->pair_regs [0], load);
1732 /* Trees can't be shared so make a copy */
1733 MonoInst *vtaddr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1734 MonoInst *load, *load2, *offset_ins;
1737 MONO_INST_NEW (cfg, load, CEE_LDIND_I);
1738 load->ssa_op = MONO_SSA_LOAD;
1739 load->inst_i0 = (cfg)->varinfo [vtaddr->inst_c0];
1741 NEW_ICONST (cfg, offset_ins, 0);
1742 MONO_INST_NEW (cfg, load2, CEE_ADD);
1743 load2->inst_left = load;
1744 load2->inst_right = offset_ins;
1746 MONO_INST_NEW (cfg, load, arg_storage_to_ldind (ainfo->pair_storage [0]));
1747 load->inst_left = load2;
1749 add_outarg_reg (cfg, call, arg, ainfo->pair_storage [0], ainfo->pair_regs [0], load);
1752 MONO_INST_NEW (cfg, load, CEE_LDIND_I);
1753 load->ssa_op = MONO_SSA_LOAD;
1754 load->inst_i0 = (cfg)->varinfo [vtaddr->inst_c0];
1756 NEW_ICONST (cfg, offset_ins, 8);
1757 MONO_INST_NEW (cfg, load2, CEE_ADD);
1758 load2->inst_left = load;
1759 load2->inst_right = offset_ins;
1761 MONO_INST_NEW (cfg, load, arg_storage_to_ldind (ainfo->pair_storage [1]));
1762 load->inst_left = load2;
1764 MONO_INST_NEW (cfg, arg, OP_OUTARG);
1765 arg->cil_code = in->cil_code;
1766 arg->type = in->type;
1767 /* prepend, so they get reversed */
1768 arg->next = call->out_args;
1769 call->out_args = arg;
1771 add_outarg_reg (cfg, call, arg, ainfo->pair_storage [1], ainfo->pair_regs [1], load);
1773 /* Prepend a copy inst */
1774 MONO_INST_NEW (cfg, arg, CEE_STIND_I);
1775 arg->cil_code = in->cil_code;
1776 arg->ssa_op = MONO_SSA_STORE;
1777 arg->inst_left = vtaddr;
1778 arg->inst_right = in;
1779 arg->type = in->type;
1781 /* prepend, so they get reversed */
1782 arg->next = call->out_args;
1783 call->out_args = arg;
1786 else if (ainfo->storage == ArgValuetypeAddrInIReg){
1788 /* Add a temp variable to the method*/
1790 MonoInst *vtaddr = mono_compile_create_var (cfg, &in->klass->byval_arg, OP_LOCAL);
1792 MONO_INST_NEW (cfg, load, OP_LDADDR);
1793 load->ssa_op = MONO_SSA_LOAD;
1794 load->inst_left = vtaddr;
1796 if (ainfo->pair_storage [0] == ArgInIReg) {
1797 /* Inserted after the copy. Load the address of the temp to the argument regster.*/
1798 arg->opcode = OP_OUTARG_REG;
1799 arg->inst_left = load;
1800 arg->inst_call = call;
1801 arg->backend.reg3 = ainfo->pair_regs [0];
1804 /* Inserted after the copy. Load the address of the temp on the stack.*/
1805 arg->opcode = OP_OUTARG_VT;
1806 arg->inst_left = load;
1807 arg->type = STACK_PTR;
1808 arg->klass = mono_defaults.int_class;
1809 arg->backend.is_pinvoke = sig->pinvoke;
1810 arg->inst_imm = size;
1813 /*Copy the argument to the temp variable.*/
1814 MONO_INST_NEW (cfg, load, OP_MEMCPY);
1815 load->backend.memcpy_args = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMemcpyArgs));
1816 load->backend.memcpy_args->size = size;
1817 load->backend.memcpy_args->align = align;
1818 load->inst_left = (cfg)->varinfo [vtaddr->inst_c0];
1819 load->inst_right = in->inst_i0;
1822 g_assert_not_reached ();
1823 //MONO_INST_LIST_ADD (&load->node, &call->out_args);
1826 arg->opcode = OP_OUTARG_VT;
1827 arg->klass = in->klass;
1828 arg->backend.is_pinvoke = sig->pinvoke;
1829 arg->inst_imm = size;
1833 switch (ainfo->storage) {
1835 add_outarg_reg (cfg, call, arg, ainfo->storage, ainfo->reg, in);
1837 case ArgInFloatSSEReg:
1838 case ArgInDoubleSSEReg:
1839 add_outarg_reg (cfg, call, arg, ainfo->storage, ainfo->reg, in);
1842 arg->opcode = OP_OUTARG;
1843 if (!sig->params [i - sig->hasthis]->byref) {
1844 if (sig->params [i - sig->hasthis]->type == MONO_TYPE_R4)
1845 arg->opcode = OP_OUTARG_R4;
1847 if (sig->params [i - sig->hasthis]->type == MONO_TYPE_R8)
1848 arg->opcode = OP_OUTARG_R8;
1852 g_assert_not_reached ();
1858 /* Handle the case where there are no implicit arguments */
1859 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos)) {
1860 emit_sig_cookie (cfg, call, cinfo);
1863 if (cinfo->ret.storage == ArgValuetypeInReg) {
1864 /* This is needed by mono_arch_emit_this_vret_args () */
1865 if (!cfg->arch.vret_addr_loc) {
1866 cfg->arch.vret_addr_loc = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1867 /* Prevent it from being register allocated or optimized away */
1868 ((MonoInst*)cfg->arch.vret_addr_loc)->flags |= MONO_INST_VOLATILE;
1872 if (cinfo->need_stack_align) {
1873 MONO_INST_NEW (cfg, arg, OP_AMD64_OUTARG_ALIGN_STACK);
1875 /* prepend, so they get reversed */
1876 arg->next = call->out_args;
1877 call->out_args = arg;
1880 #ifdef PLATFORM_WIN32
1881 /* Always reserve 32 bytes of stack space on Win64 */
1882 /*MONO_INST_NEW (cfg, arg, OP_AMD64_OUTARG_ALIGN_STACK);
1884 MONO_INST_LIST_ADD_TAIL (&arg->node, &call->out_args);*/
1889 if (cfg->method->save_lmf) {
1890 MONO_INST_NEW (cfg, arg, OP_AMD64_SAVE_SP_TO_LMF);
1891 MONO_INST_LIST_ADD_TAIL (&arg->node, &call->out_args);
1895 call->stack_usage = cinfo->stack_usage;
1896 cfg->param_area = MAX (cfg->param_area, call->stack_usage);
1897 cfg->flags |= MONO_CFG_HAS_CALLS;
1903 emit_sig_cookie2 (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
1906 MonoMethodSignature *tmp_sig;
1909 if (call->tail_call)
1912 /* FIXME: Add support for signature tokens to AOT */
1913 cfg->disable_aot = TRUE;
1915 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
1918 * mono_ArgIterator_Setup assumes the signature cookie is
1919 * passed first and all the arguments which were before it are
1920 * passed on the stack after the signature. So compensate by
1921 * passing a different signature.
1923 tmp_sig = mono_metadata_signature_dup (call->signature);
1924 tmp_sig->param_count -= call->signature->sentinelpos;
1925 tmp_sig->sentinelpos = 0;
1926 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
1928 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
1929 sig_arg->dreg = mono_alloc_ireg (cfg);
1930 sig_arg->inst_p0 = tmp_sig;
1931 MONO_ADD_INS (cfg->cbb, sig_arg);
1933 MONO_INST_NEW (cfg, arg, OP_X86_PUSH);
1934 arg->sreg1 = sig_arg->dreg;
1935 MONO_ADD_INS (cfg->cbb, arg);
1938 #define NEW_VARSTORE(cfg,dest,var,vartype,inst) do { \
1939 MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
1940 (dest)->opcode = mono_type_to_regmove ((cfg), (vartype)); \
1941 (dest)->klass = (var)->klass; \
1942 (dest)->sreg1 = (inst)->dreg; \
1943 (dest)->dreg = (var)->dreg; \
1944 if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
1947 #define NEW_ARGSTORE(cfg,dest,num,inst) NEW_VARSTORE ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
1949 #define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do { NEW_ARGSTORE ((cfg), (dest), (num), (inst)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
1952 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
1955 MonoMethodSignature *sig;
1956 int i, n, stack_size;
1962 sig = call->signature;
1963 n = sig->param_count + sig->hasthis;
1965 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, sig->pinvoke);
1967 if (cinfo->need_stack_align) {
1968 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, 8);
1972 * Emit all parameters passed in registers in non-reverse order for better readability
1973 * and to help the optimization in emit_prolog ().
1975 for (i = 0; i < n; ++i) {
1976 ainfo = cinfo->args + i;
1978 in = call->args [i];
1980 if (ainfo->storage == ArgInIReg)
1981 add_outarg_reg2 (cfg, call, ainfo->storage, ainfo->reg, in);
1984 for (i = n - 1; i >= 0; --i) {
1985 ainfo = cinfo->args + i;
1987 in = call->args [i];
1989 switch (ainfo->storage) {
1993 case ArgInFloatSSEReg:
1994 case ArgInDoubleSSEReg:
1995 add_outarg_reg2 (cfg, call, ainfo->storage, ainfo->reg, in);
1998 case ArgValuetypeInReg:
1999 case ArgValuetypeAddrInIReg:
2000 if (ainfo->storage == ArgOnStack && call->tail_call) {
2001 MonoInst *call_inst = (MonoInst*)call;
2002 cfg->args [i]->flags |= MONO_INST_VOLATILE;
2003 EMIT_NEW_ARGSTORE (cfg, call_inst, i, in);
2004 } else if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(sig->params [i - sig->hasthis]))) {
2008 if (sig->params [i - sig->hasthis]->type == MONO_TYPE_TYPEDBYREF) {
2009 size = sizeof (MonoTypedRef);
2010 align = sizeof (gpointer);
2014 size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
2017 * Other backends use mono_type_stack_size (), but that
2018 * aligns the size to 8, which is larger than the size of
2019 * the source, leading to reads of invalid memory if the
2020 * source is at the end of address space.
2022 size = mono_class_value_size (in->klass, &align);
2025 g_assert (in->klass);
2028 MONO_INST_NEW (cfg, arg, OP_OUTARG_VT);
2029 arg->sreg1 = in->dreg;
2030 arg->klass = in->klass;
2031 arg->backend.size = size;
2032 arg->inst_p0 = call;
2033 arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
2034 memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
2036 MONO_ADD_INS (cfg->cbb, arg);
2039 MONO_INST_NEW (cfg, arg, OP_X86_PUSH);
2040 arg->sreg1 = in->dreg;
2041 if (!sig->params [i - sig->hasthis]->byref) {
2042 if (sig->params [i - sig->hasthis]->type == MONO_TYPE_R4) {
2043 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, 8);
2044 arg->opcode = OP_STORER4_MEMBASE_REG;
2045 arg->inst_destbasereg = X86_ESP;
2046 arg->inst_offset = 0;
2047 } else if (sig->params [i - sig->hasthis]->type == MONO_TYPE_R8) {
2048 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, 8);
2049 arg->opcode = OP_STORER8_MEMBASE_REG;
2050 arg->inst_destbasereg = X86_ESP;
2051 arg->inst_offset = 0;
2054 MONO_ADD_INS (cfg->cbb, arg);
2058 g_assert_not_reached ();
2061 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
2062 /* Emit the signature cookie just before the implicit arguments */
2063 emit_sig_cookie2 (cfg, call, cinfo);
2067 /* Handle the case where there are no implicit arguments */
2068 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos)) {
2069 emit_sig_cookie2 (cfg, call, cinfo);
2072 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
2075 if (cinfo->ret.storage == ArgValuetypeInReg) {
2076 if (cinfo->ret.pair_storage [0] == ArgInIReg && cinfo->ret.pair_storage [1] == ArgNone) {
2078 * Tell the JIT to use a more efficient calling convention: call using
2079 * OP_CALL, compute the result location after the call, and save the
2082 call->vret_in_reg = TRUE;
2084 * Nullify the instruction computing the vret addr to enable
2085 * future optimizations.
2088 NULLIFY_INS (call->vret_var);
2090 if (call->tail_call)
2093 * The valuetype is in RAX:RDX after the call, need to be copied to
2094 * the stack. Push the address here, so the call instruction can
2097 if (!cfg->arch.vret_addr_loc) {
2098 cfg->arch.vret_addr_loc = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2099 /* Prevent it from being register allocated or optimized away */
2100 ((MonoInst*)cfg->arch.vret_addr_loc)->flags |= MONO_INST_VOLATILE;
2103 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, ((MonoInst*)cfg->arch.vret_addr_loc)->dreg, call->vret_var->dreg);
2107 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
2108 vtarg->sreg1 = call->vret_var->dreg;
2109 vtarg->dreg = mono_alloc_preg (cfg);
2110 MONO_ADD_INS (cfg->cbb, vtarg);
2112 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
2116 #ifdef PLATFORM_WIN32
2117 if (call->inst.opcode != OP_JMP && OP_TAILCALL != call->inst.opcode) {
2118 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, 0x20);
2122 if (cfg->method->save_lmf) {
2123 MONO_INST_NEW (cfg, arg, OP_AMD64_SAVE_SP_TO_LMF);
2124 MONO_ADD_INS (cfg->cbb, arg);
2127 call->stack_usage = cinfo->stack_usage;
2131 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
2134 MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
2135 ArgInfo *ainfo = (ArgInfo*)ins->inst_p1;
2136 int size = ins->backend.size;
2138 if (ainfo->storage == ArgValuetypeInReg) {
2142 for (part = 0; part < 2; ++part) {
2143 if (ainfo->pair_storage [part] == ArgNone)
2146 MONO_INST_NEW (cfg, load, arg_storage_to_load_membase (ainfo->pair_storage [part]));
2147 load->inst_basereg = src->dreg;
2148 load->inst_offset = part * sizeof (gpointer);
2150 switch (ainfo->pair_storage [part]) {
2152 load->dreg = mono_alloc_ireg (cfg);
2154 case ArgInDoubleSSEReg:
2155 case ArgInFloatSSEReg:
2156 load->dreg = mono_alloc_freg (cfg);
2159 g_assert_not_reached ();
2161 MONO_ADD_INS (cfg->cbb, load);
2163 add_outarg_reg2 (cfg, call, ainfo->pair_storage [part], ainfo->pair_regs [part], load);
2165 } else if (ainfo->storage == ArgValuetypeAddrInIReg) {
2166 MonoInst *vtaddr, *load;
2167 vtaddr = mono_compile_create_var (cfg, &ins->klass->byval_arg, OP_LOCAL);
2169 MONO_INST_NEW (cfg, load, OP_LDADDR);
2170 load->inst_p0 = vtaddr;
2171 vtaddr->flags |= MONO_INST_INDIRECT;
2172 load->type = STACK_MP;
2173 load->klass = vtaddr->klass;
2174 load->dreg = mono_alloc_ireg (cfg);
2175 MONO_ADD_INS (cfg->cbb, load);
2176 mini_emit_memcpy2 (cfg, load->dreg, 0, src->dreg, 0, size, 4);
2178 if (ainfo->pair_storage [0] == ArgInIReg) {
2179 MONO_INST_NEW (cfg, arg, OP_X86_LEA_MEMBASE);
2180 arg->dreg = mono_alloc_ireg (cfg);
2181 arg->sreg1 = load->dreg;
2183 MONO_ADD_INS (cfg->cbb, arg);
2184 mono_call_inst_add_outarg_reg (cfg, call, arg->dreg, ainfo->pair_regs [0], FALSE);
2186 MONO_INST_NEW (cfg, arg, OP_X86_PUSH);
2187 arg->sreg1 = load->dreg;
2188 MONO_ADD_INS (cfg->cbb, arg);
2192 /* Can't use this for < 8 since it does an 8 byte memory load */
2193 MONO_INST_NEW (cfg, arg, OP_X86_PUSH_MEMBASE);
2194 arg->inst_basereg = src->dreg;
2195 arg->inst_offset = 0;
2196 MONO_ADD_INS (cfg->cbb, arg);
2197 } else if (size <= 40) {
2198 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, ALIGN_TO (size, 8));
2199 mini_emit_memcpy2 (cfg, X86_ESP, 0, src->dreg, 0, size, 4);
2201 MONO_INST_NEW (cfg, arg, OP_X86_PUSH_OBJ);
2202 arg->inst_basereg = src->dreg;
2203 arg->inst_offset = 0;
2204 arg->inst_imm = size;
2205 MONO_ADD_INS (cfg->cbb, arg);
2211 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
2213 MonoType *ret = mini_type_get_underlying_type (NULL, mono_method_signature (method)->ret);
2216 if (ret->type == MONO_TYPE_R4) {
2217 MONO_EMIT_NEW_UNALU (cfg, OP_AMD64_SET_XMMREG_R4, cfg->ret->dreg, val->dreg);
2219 } else if (ret->type == MONO_TYPE_R8) {
2220 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
2225 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
2228 #define EMIT_COND_BRANCH(ins,cond,sign) \
2229 if (ins->flags & MONO_INST_BRLABEL) { \
2230 if (ins->inst_i0->inst_c0) { \
2231 x86_branch (code, cond, cfg->native_code + ins->inst_i0->inst_c0, sign); \
2233 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
2234 if ((cfg->opt & MONO_OPT_BRANCH) && \
2235 x86_is_imm8 (ins->inst_i0->inst_c1 - cpos)) \
2236 x86_branch8 (code, cond, 0, sign); \
2238 x86_branch32 (code, cond, 0, sign); \
2241 if (ins->inst_true_bb->native_offset) { \
2242 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
2244 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
2245 if ((cfg->opt & MONO_OPT_BRANCH) && \
2246 x86_is_imm8 (ins->inst_true_bb->max_offset - cpos)) \
2247 x86_branch8 (code, cond, 0, sign); \
2249 x86_branch32 (code, cond, 0, sign); \
2253 /* emit an exception if condition is fail */
2254 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
2256 MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
2257 if (tins == NULL) { \
2258 mono_add_patch_info (cfg, code - cfg->native_code, \
2259 MONO_PATCH_INFO_EXC, exc_name); \
2260 x86_branch32 (code, cond, 0, signed); \
2262 EMIT_COND_BRANCH (tins, cond, signed); \
2266 #define EMIT_FPCOMPARE(code) do { \
2267 amd64_fcompp (code); \
2268 amd64_fnstsw (code); \
2271 #define EMIT_SSE2_FPFUNC(code, op, dreg, sreg1) do { \
2272 amd64_movsd_membase_reg (code, AMD64_RSP, -8, (sreg1)); \
2273 amd64_fld_membase (code, AMD64_RSP, -8, TRUE); \
2274 amd64_ ##op (code); \
2275 amd64_fst_membase (code, AMD64_RSP, -8, TRUE, TRUE); \
2276 amd64_movsd_reg_membase (code, (dreg), AMD64_RSP, -8); \
2280 emit_call_body (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data)
2282 gboolean no_patch = FALSE;
2285 * FIXME: Add support for thunks
2288 gboolean near_call = FALSE;
2291 * Indirect calls are expensive so try to make a near call if possible.
2292 * The caller memory is allocated by the code manager so it is
2293 * guaranteed to be at a 32 bit offset.
2296 if (patch_type != MONO_PATCH_INFO_ABS) {
2297 /* The target is in memory allocated using the code manager */
2300 if ((patch_type == MONO_PATCH_INFO_METHOD) || (patch_type == MONO_PATCH_INFO_METHOD_JUMP)) {
2301 if (((MonoMethod*)data)->klass->image->aot_module)
2302 /* The callee might be an AOT method */
2304 if (((MonoMethod*)data)->dynamic)
2305 /* The target is in malloc-ed memory */
2309 if (patch_type == MONO_PATCH_INFO_INTERNAL_METHOD) {
2311 * The call might go directly to a native function without
2314 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (data);
2316 gconstpointer target = mono_icall_get_wrapper (mi);
2317 if ((((guint64)target) >> 32) != 0)
2323 if (!cfg->new_ir && mono_find_class_init_trampoline_by_addr (data))
2325 else if (cfg->abs_patches && g_hash_table_lookup (cfg->abs_patches, data)) {
2327 * This is not really an optimization, but required because the
2328 * generic class init trampolines use R11 to pass the vtable.
2332 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (data);
2334 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
2335 strstr (cfg->method->name, info->name)) {
2336 /* A call to the wrapped function */
2337 if ((((guint64)data) >> 32) == 0)
2341 else if (info->func == info->wrapper) {
2343 if ((((guint64)info->func) >> 32) == 0)
2347 /* See the comment in mono_codegen () */
2348 if ((info->name [0] != 'v') || (strstr (info->name, "ves_array_new_va_") == NULL && strstr (info->name, "ves_array_element_address_") == NULL))
2352 else if ((((guint64)data) >> 32) == 0) {
2359 if (cfg->method->dynamic)
2360 /* These methods are allocated using malloc */
2363 if (cfg->compile_aot)
2366 #ifdef MONO_ARCH_NOMAP32BIT
2372 * Align the call displacement to an address divisible by 4 so it does
2373 * not span cache lines. This is required for code patching to work on SMP
2376 if (!no_patch && ((guint32)(code + 1 - cfg->native_code) % 4) != 0)
2377 amd64_padding (code, 4 - ((guint32)(code + 1 - cfg->native_code) % 4));
2378 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
2379 amd64_call_code (code, 0);
2382 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
2383 amd64_set_reg_template (code, GP_SCRATCH_REG);
2384 amd64_call_reg (code, GP_SCRATCH_REG);
2391 static inline guint8*
2392 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data, gboolean win64_adjust_stack)
2394 #ifdef PLATFORM_WIN32
2395 if (win64_adjust_stack)
2396 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 32);
2398 code = emit_call_body (cfg, code, patch_type, data);
2399 #ifdef PLATFORM_WIN32
2400 if (win64_adjust_stack)
2401 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 32);
2408 store_membase_imm_to_store_membase_reg (int opcode)
2411 case OP_STORE_MEMBASE_IMM:
2412 return OP_STORE_MEMBASE_REG;
2413 case OP_STOREI4_MEMBASE_IMM:
2414 return OP_STOREI4_MEMBASE_REG;
2415 case OP_STOREI8_MEMBASE_IMM:
2416 return OP_STOREI8_MEMBASE_REG;
2422 #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)))
2425 * mono_arch_peephole_pass_1:
2427 * Perform peephole opts which should/can be performed before local regalloc
2430 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
2434 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
2435 MonoInst *last_ins = ins->prev;
2437 switch (ins->opcode) {
2441 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS) && (ins->inst_imm > 0)) {
2443 * X86_LEA is like ADD, but doesn't have the
2444 * sreg1==dreg restriction. inst_imm > 0 is needed since LEA sign-extends
2445 * its operand to 64 bit.
2447 ins->opcode = OP_X86_LEA_MEMBASE;
2448 ins->inst_basereg = ins->sreg1;
2453 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
2457 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
2458 * the latter has length 2-3 instead of 6 (reverse constant
2459 * propagation). These instruction sequences are very common
2460 * in the initlocals bblock.
2462 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
2463 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)) {
2464 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
2465 ins2->sreg1 = ins->dreg;
2466 } 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)) {
2468 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
2477 case OP_COMPARE_IMM:
2478 case OP_LCOMPARE_IMM:
2479 /* OP_COMPARE_IMM (reg, 0)
2481 * OP_AMD64_TEST_NULL (reg)
2484 ins->opcode = OP_AMD64_TEST_NULL;
2486 case OP_ICOMPARE_IMM:
2488 ins->opcode = OP_X86_TEST_NULL;
2490 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
2492 * OP_STORE_MEMBASE_REG reg, offset(basereg)
2493 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
2495 * OP_STORE_MEMBASE_REG reg, offset(basereg)
2496 * OP_COMPARE_IMM reg, imm
2498 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
2500 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
2501 ins->inst_basereg == last_ins->inst_destbasereg &&
2502 ins->inst_offset == last_ins->inst_offset) {
2503 ins->opcode = OP_ICOMPARE_IMM;
2504 ins->sreg1 = last_ins->sreg1;
2506 /* check if we can remove cmp reg,0 with test null */
2508 ins->opcode = OP_X86_TEST_NULL;
2514 mono_peephole_ins (bb, ins);
2519 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
2523 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
2524 switch (ins->opcode) {
2527 /* reg = 0 -> XOR (reg, reg) */
2528 /* XOR sets cflags on x86, so we cant do it always */
2529 if (ins->inst_c0 == 0 && (!ins->next || (ins->next && INST_IGNORES_CFLAGS (ins->next->opcode)))) {
2530 ins->opcode = OP_LXOR;
2531 ins->sreg1 = ins->dreg;
2532 ins->sreg2 = ins->dreg;
2540 * Use IXOR to avoid a rex prefix if possible. The cpu will sign extend the
2541 * 0 result into 64 bits.
2543 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
2544 ins->opcode = OP_IXOR;
2548 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
2552 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
2553 * the latter has length 2-3 instead of 6 (reverse constant
2554 * propagation). These instruction sequences are very common
2555 * in the initlocals bblock.
2557 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
2558 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)) {
2559 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
2560 ins2->sreg1 = ins->dreg;
2561 } 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)) {
2563 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
2573 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
2574 ins->opcode = OP_X86_INC_REG;
2577 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
2578 ins->opcode = OP_X86_DEC_REG;
2582 mono_peephole_ins (bb, ins);
2586 #define NEW_INS(cfg,ins,dest,op) do { \
2587 MONO_INST_NEW ((cfg), (dest), (op)); \
2588 (dest)->cil_code = (ins)->cil_code; \
2589 mono_bblock_insert_before_ins (bb, ins, (dest)); \
2593 * mono_arch_lowering_pass:
2595 * Converts complex opcodes into simpler ones so that each IR instruction
2596 * corresponds to one machine instruction.
2599 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
2601 MonoInst *ins, *n, *temp;
2603 if (bb->max_vreg > cfg->rs->next_vreg)
2604 cfg->rs->next_vreg = bb->max_vreg;
2607 * FIXME: Need to add more instructions, but the current machine
2608 * description can't model some parts of the composite instructions like
2611 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
2612 switch (ins->opcode) {
2617 case OP_IDIV_UN_IMM:
2618 case OP_IREM_UN_IMM:
2619 mono_decompose_op_imm (cfg, bb, ins);
2621 case OP_COMPARE_IMM:
2622 case OP_LCOMPARE_IMM:
2623 if (!amd64_is_imm32 (ins->inst_imm)) {
2624 NEW_INS (cfg, ins, temp, OP_I8CONST);
2625 temp->inst_c0 = ins->inst_imm;
2627 temp->dreg = mono_alloc_ireg (cfg);
2629 temp->dreg = mono_regstate_next_int (cfg->rs);
2630 ins->opcode = OP_COMPARE;
2631 ins->sreg2 = temp->dreg;
2634 case OP_LOAD_MEMBASE:
2635 case OP_LOADI8_MEMBASE:
2636 if (!amd64_is_imm32 (ins->inst_offset)) {
2637 NEW_INS (cfg, ins, temp, OP_I8CONST);
2638 temp->inst_c0 = ins->inst_offset;
2640 temp->dreg = mono_alloc_ireg (cfg);
2642 temp->dreg = mono_regstate_next_int (cfg->rs);
2643 ins->opcode = OP_AMD64_LOADI8_MEMINDEX;
2644 ins->inst_indexreg = temp->dreg;
2647 case OP_STORE_MEMBASE_IMM:
2648 case OP_STOREI8_MEMBASE_IMM:
2649 if (!amd64_is_imm32 (ins->inst_imm)) {
2650 NEW_INS (cfg, ins, temp, OP_I8CONST);
2651 temp->inst_c0 = ins->inst_imm;
2653 temp->dreg = mono_alloc_ireg (cfg);
2655 temp->dreg = mono_regstate_next_int (cfg->rs);
2656 ins->opcode = OP_STOREI8_MEMBASE_REG;
2657 ins->sreg1 = temp->dreg;
2665 bb->max_vreg = cfg->rs->next_vreg;
2669 branch_cc_table [] = {
2670 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
2671 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
2672 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
2675 /* Maps CMP_... constants to X86_CC_... constants */
2678 X86_CC_EQ, X86_CC_NE, X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT,
2679 X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT
2683 cc_signed_table [] = {
2684 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
2685 FALSE, FALSE, FALSE, FALSE
2688 /*#include "cprop.c"*/
2690 static unsigned char*
2691 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
2693 amd64_sse_cvttsd2si_reg_reg (code, dreg, sreg);
2696 amd64_widen_reg (code, dreg, dreg, is_signed, FALSE);
2698 amd64_widen_reg (code, dreg, dreg, is_signed, TRUE);
2702 static unsigned char*
2703 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
2705 int sreg = tree->sreg1;
2706 int need_touch = FALSE;
2708 #if defined(PLATFORM_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
2709 if (!tree->flags & MONO_INST_INIT)
2718 * If requested stack size is larger than one page,
2719 * perform stack-touch operation
2722 * Generate stack probe code.
2723 * Under Windows, it is necessary to allocate one page at a time,
2724 * "touching" stack after each successful sub-allocation. This is
2725 * because of the way stack growth is implemented - there is a
2726 * guard page before the lowest stack page that is currently commited.
2727 * Stack normally grows sequentially so OS traps access to the
2728 * guard page and commits more pages when needed.
2730 amd64_test_reg_imm (code, sreg, ~0xFFF);
2731 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2733 br[2] = code; /* loop */
2734 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
2735 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
2736 amd64_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
2737 amd64_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
2738 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
2739 amd64_patch (br[3], br[2]);
2740 amd64_test_reg_reg (code, sreg, sreg);
2741 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2742 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
2744 br[1] = code; x86_jump8 (code, 0);
2746 amd64_patch (br[0], code);
2747 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
2748 amd64_patch (br[1], code);
2749 amd64_patch (br[4], code);
2752 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, tree->sreg1);
2754 if (tree->flags & MONO_INST_INIT) {
2756 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX) {
2757 amd64_push_reg (code, AMD64_RAX);
2760 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX) {
2761 amd64_push_reg (code, AMD64_RCX);
2764 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI) {
2765 amd64_push_reg (code, AMD64_RDI);
2769 amd64_shift_reg_imm (code, X86_SHR, sreg, 3);
2770 if (sreg != AMD64_RCX)
2771 amd64_mov_reg_reg (code, AMD64_RCX, sreg, 8);
2772 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
2774 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, offset);
2776 amd64_prefix (code, X86_REP_PREFIX);
2779 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI)
2780 amd64_pop_reg (code, AMD64_RDI);
2781 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX)
2782 amd64_pop_reg (code, AMD64_RCX);
2783 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX)
2784 amd64_pop_reg (code, AMD64_RAX);
2790 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
2795 /* Move return value to the target register */
2796 /* FIXME: do this in the local reg allocator */
2797 switch (ins->opcode) {
2800 case OP_CALL_MEMBASE:
2803 case OP_LCALL_MEMBASE:
2804 g_assert (ins->dreg == AMD64_RAX);
2808 case OP_FCALL_MEMBASE:
2809 if (((MonoCallInst*)ins)->signature->ret->type == MONO_TYPE_R4) {
2810 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, AMD64_XMM0);
2813 if (ins->dreg != AMD64_XMM0)
2814 amd64_sse_movsd_reg_reg (code, ins->dreg, AMD64_XMM0);
2819 case OP_VCALL_MEMBASE:
2822 case OP_VCALL2_MEMBASE:
2823 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, ((MonoCallInst*)ins)->signature, FALSE);
2824 if (cinfo->ret.storage == ArgValuetypeInReg) {
2825 MonoInst *loc = cfg->arch.vret_addr_loc;
2827 /* Load the destination address */
2828 g_assert (loc->opcode == OP_REGOFFSET);
2829 amd64_mov_reg_membase (code, AMD64_RCX, loc->inst_basereg, loc->inst_offset, 8);
2831 for (quad = 0; quad < 2; quad ++) {
2832 switch (cinfo->ret.pair_storage [quad]) {
2834 amd64_mov_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad], 8);
2836 case ArgInFloatSSEReg:
2837 amd64_movss_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
2839 case ArgInDoubleSSEReg:
2840 amd64_movsd_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
2857 * @code: buffer to store code to
2858 * @dreg: hard register where to place the result
2859 * @tls_offset: offset info
2861 * emit_tls_get emits in @code the native code that puts in the dreg register
2862 * the item in the thread local storage identified by tls_offset.
2864 * Returns: a pointer to the end of the stored code
2867 emit_tls_get (guint8* code, int dreg, int tls_offset)
2869 #ifdef PLATFORM_WIN32
2870 g_assert (tls_offset < 64);
2871 x86_prefix (code, X86_GS_PREFIX);
2872 amd64_mov_reg_mem (code, dreg, (tls_offset * 8) + 0x1480, 8);
2874 if (optimize_for_xen) {
2875 x86_prefix (code, X86_FS_PREFIX);
2876 amd64_mov_reg_mem (code, dreg, 0, 8);
2877 amd64_mov_reg_membase (code, dreg, dreg, tls_offset, 8);
2879 x86_prefix (code, X86_FS_PREFIX);
2880 amd64_mov_reg_mem (code, dreg, tls_offset, 8);
2887 * emit_load_volatile_arguments:
2889 * Load volatile arguments from the stack to the original input registers.
2890 * Required before a tail call.
2893 emit_load_volatile_arguments (MonoCompile *cfg, guint8 *code)
2895 MonoMethod *method = cfg->method;
2896 MonoMethodSignature *sig;
2901 /* FIXME: Generate intermediate code instead */
2903 sig = mono_method_signature (method);
2905 cinfo = cfg->arch.cinfo;
2907 /* This is the opposite of the code in emit_prolog */
2908 if (sig->ret->type != MONO_TYPE_VOID) {
2909 if (cfg->vret_addr && (cfg->vret_addr->opcode != OP_REGVAR))
2910 amd64_mov_reg_membase (code, cinfo->ret.reg, cfg->vret_addr->inst_basereg, cfg->vret_addr->inst_offset, 8);
2913 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2914 ArgInfo *ainfo = cinfo->args + i;
2916 ins = cfg->args [i];
2918 if (sig->hasthis && (i == 0))
2919 arg_type = &mono_defaults.object_class->byval_arg;
2921 arg_type = sig->params [i - sig->hasthis];
2923 if (ins->opcode != OP_REGVAR) {
2924 switch (ainfo->storage) {
2929 amd64_mov_reg_membase (code, ainfo->reg, ins->inst_basereg, ins->inst_offset, size);
2932 case ArgInFloatSSEReg:
2933 amd64_movss_reg_membase (code, ainfo->reg, ins->inst_basereg, ins->inst_offset);
2935 case ArgInDoubleSSEReg:
2936 amd64_movsd_reg_membase (code, ainfo->reg, ins->inst_basereg, ins->inst_offset);
2938 case ArgValuetypeInReg:
2939 for (quad = 0; quad < 2; quad ++) {
2940 switch (ainfo->pair_storage [quad]) {
2942 amd64_mov_reg_membase (code, ainfo->pair_regs [quad], ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), sizeof (gpointer));
2944 case ArgInFloatSSEReg:
2945 case ArgInDoubleSSEReg:
2946 g_assert_not_reached ();
2951 g_assert_not_reached ();
2955 case ArgValuetypeAddrInIReg:
2956 if (ainfo->pair_storage [0] == ArgInIReg)
2957 amd64_mov_reg_membase (code, ainfo->pair_regs [0], ins->inst_left->inst_basereg, ins->inst_left->inst_offset, sizeof (gpointer));
2964 g_assert (ainfo->storage == ArgInIReg);
2966 amd64_mov_reg_reg (code, ainfo->reg, ins->dreg, 8);
2973 #define REAL_PRINT_REG(text,reg) \
2974 mono_assert (reg >= 0); \
2975 amd64_push_reg (code, AMD64_RAX); \
2976 amd64_push_reg (code, AMD64_RDX); \
2977 amd64_push_reg (code, AMD64_RCX); \
2978 amd64_push_reg (code, reg); \
2979 amd64_push_imm (code, reg); \
2980 amd64_push_imm (code, text " %d %p\n"); \
2981 amd64_mov_reg_imm (code, AMD64_RAX, printf); \
2982 amd64_call_reg (code, AMD64_RAX); \
2983 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 3*4); \
2984 amd64_pop_reg (code, AMD64_RCX); \
2985 amd64_pop_reg (code, AMD64_RDX); \
2986 amd64_pop_reg (code, AMD64_RAX);
2988 /* benchmark and set based on cpu */
2989 #define LOOP_ALIGNMENT 8
2990 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
2995 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
3000 guint8 *code = cfg->native_code + cfg->code_len;
3001 MonoInst *last_ins = NULL;
3002 guint last_offset = 0;
3005 if (cfg->opt & MONO_OPT_LOOP) {
3006 int pad, align = LOOP_ALIGNMENT;
3007 /* set alignment depending on cpu */
3008 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
3010 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
3011 amd64_padding (code, pad);
3012 cfg->code_len += pad;
3013 bb->native_offset = cfg->code_len;
3017 if (cfg->verbose_level > 2)
3018 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
3020 cpos = bb->max_offset;
3022 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
3023 MonoProfileCoverageInfo *cov = cfg->coverage_info;
3024 g_assert (!cfg->compile_aot);
3027 cov->data [bb->dfn].cil_code = bb->cil_code;
3028 amd64_mov_reg_imm (code, AMD64_R11, (guint64)&cov->data [bb->dfn].count);
3029 /* this is not thread save, but good enough */
3030 amd64_inc_membase (code, AMD64_R11, 0);
3033 offset = code - cfg->native_code;
3035 mono_debug_open_block (cfg, bb, offset);
3037 if (mono_break_at_bb_method && mono_method_desc_full_match (mono_break_at_bb_method, cfg->method) && bb->block_num == mono_break_at_bb_bb_num)
3038 x86_breakpoint (code);
3040 MONO_BB_FOR_EACH_INS (bb, ins) {
3041 offset = code - cfg->native_code;
3043 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
3045 if (G_UNLIKELY (offset > (cfg->code_size - max_len - 16))) {
3046 cfg->code_size *= 2;
3047 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3048 code = cfg->native_code + offset;
3049 mono_jit_stats.code_reallocs++;
3052 if (cfg->debug_info)
3053 mono_debug_record_line_number (cfg, ins, offset);
3055 switch (ins->opcode) {
3057 amd64_mul_reg (code, ins->sreg2, TRUE);
3060 amd64_mul_reg (code, ins->sreg2, FALSE);
3062 case OP_X86_SETEQ_MEMBASE:
3063 amd64_set_membase (code, X86_CC_EQ, ins->inst_basereg, ins->inst_offset, TRUE);
3065 case OP_STOREI1_MEMBASE_IMM:
3066 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
3068 case OP_STOREI2_MEMBASE_IMM:
3069 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
3071 case OP_STOREI4_MEMBASE_IMM:
3072 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
3074 case OP_STOREI1_MEMBASE_REG:
3075 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
3077 case OP_STOREI2_MEMBASE_REG:
3078 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
3080 case OP_STORE_MEMBASE_REG:
3081 case OP_STOREI8_MEMBASE_REG:
3082 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 8);
3084 case OP_STOREI4_MEMBASE_REG:
3085 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
3087 case OP_STORE_MEMBASE_IMM:
3088 case OP_STOREI8_MEMBASE_IMM:
3089 g_assert (amd64_is_imm32 (ins->inst_imm));
3090 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 8);
3094 // FIXME: Decompose this earlier
3095 if (amd64_is_imm32 (ins->inst_imm))
3096 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, sizeof (gpointer));
3098 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3099 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 8);
3103 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3104 amd64_movsxd_reg_membase (code, ins->dreg, ins->dreg, 0);
3107 // FIXME: Decompose this earlier
3109 if (amd64_is_imm32 (ins->inst_imm))
3110 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
3112 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3113 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
3116 amd64_mov_reg_imm (code, ins->dreg, ins->inst_p0);
3117 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
3121 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3122 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, FALSE);
3125 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3126 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, TRUE);
3128 case OP_LOAD_MEMBASE:
3129 case OP_LOADI8_MEMBASE:
3130 g_assert (amd64_is_imm32 (ins->inst_offset));
3131 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, sizeof (gpointer));
3133 case OP_LOADI4_MEMBASE:
3134 amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3136 case OP_LOADU4_MEMBASE:
3137 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
3139 case OP_LOADU1_MEMBASE:
3140 /* The cpu zero extends the result into 64 bits */
3141 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE, 4);
3143 case OP_LOADI1_MEMBASE:
3144 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
3146 case OP_LOADU2_MEMBASE:
3147 /* The cpu zero extends the result into 64 bits */
3148 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE, 4);
3150 case OP_LOADI2_MEMBASE:
3151 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
3153 case OP_AMD64_LOADI8_MEMINDEX:
3154 amd64_mov_reg_memindex_size (code, ins->dreg, ins->inst_basereg, 0, ins->inst_indexreg, 0, 8);
3156 case OP_LCONV_TO_I1:
3157 case OP_ICONV_TO_I1:
3159 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
3161 case OP_LCONV_TO_I2:
3162 case OP_ICONV_TO_I2:
3164 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
3166 case OP_LCONV_TO_U1:
3167 case OP_ICONV_TO_U1:
3168 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
3170 case OP_LCONV_TO_U2:
3171 case OP_ICONV_TO_U2:
3172 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
3175 /* Clean out the upper word */
3176 amd64_mov_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
3179 amd64_movsxd_reg_reg (code, ins->dreg, ins->sreg1);
3183 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3185 case OP_COMPARE_IMM:
3186 case OP_LCOMPARE_IMM:
3187 g_assert (amd64_is_imm32 (ins->inst_imm));
3188 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
3190 case OP_X86_COMPARE_REG_MEMBASE:
3191 amd64_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
3193 case OP_X86_TEST_NULL:
3194 amd64_test_reg_reg_size (code, ins->sreg1, ins->sreg1, 4);
3196 case OP_AMD64_TEST_NULL:
3197 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
3200 case OP_X86_ADD_REG_MEMBASE:
3201 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3203 case OP_X86_SUB_REG_MEMBASE:
3204 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3206 case OP_X86_AND_REG_MEMBASE:
3207 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3209 case OP_X86_OR_REG_MEMBASE:
3210 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3212 case OP_X86_XOR_REG_MEMBASE:
3213 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3216 case OP_X86_ADD_MEMBASE_IMM:
3217 /* FIXME: Make a 64 version too */
3218 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3220 case OP_X86_SUB_MEMBASE_IMM:
3221 g_assert (amd64_is_imm32 (ins->inst_imm));
3222 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3224 case OP_X86_AND_MEMBASE_IMM:
3225 g_assert (amd64_is_imm32 (ins->inst_imm));
3226 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3228 case OP_X86_OR_MEMBASE_IMM:
3229 g_assert (amd64_is_imm32 (ins->inst_imm));
3230 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3232 case OP_X86_XOR_MEMBASE_IMM:
3233 g_assert (amd64_is_imm32 (ins->inst_imm));
3234 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3236 case OP_X86_ADD_MEMBASE_REG:
3237 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
3239 case OP_X86_SUB_MEMBASE_REG:
3240 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
3242 case OP_X86_AND_MEMBASE_REG:
3243 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
3245 case OP_X86_OR_MEMBASE_REG:
3246 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
3248 case OP_X86_XOR_MEMBASE_REG:
3249 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
3251 case OP_X86_INC_MEMBASE:
3252 amd64_inc_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
3254 case OP_X86_INC_REG:
3255 amd64_inc_reg_size (code, ins->dreg, 4);
3257 case OP_X86_DEC_MEMBASE:
3258 amd64_dec_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
3260 case OP_X86_DEC_REG:
3261 amd64_dec_reg_size (code, ins->dreg, 4);
3263 case OP_X86_MUL_REG_MEMBASE:
3264 case OP_X86_MUL_MEMBASE_REG:
3265 amd64_imul_reg_membase_size (code, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3267 case OP_AMD64_ICOMPARE_MEMBASE_REG:
3268 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
3270 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
3271 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3273 case OP_AMD64_COMPARE_MEMBASE_REG:
3274 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
3276 case OP_AMD64_COMPARE_MEMBASE_IMM:
3277 g_assert (amd64_is_imm32 (ins->inst_imm));
3278 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
3280 case OP_X86_COMPARE_MEMBASE8_IMM:
3281 amd64_alu_membase8_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3283 case OP_AMD64_ICOMPARE_REG_MEMBASE:
3284 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3286 case OP_AMD64_COMPARE_REG_MEMBASE:
3287 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
3290 case OP_AMD64_ADD_REG_MEMBASE:
3291 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
3293 case OP_AMD64_SUB_REG_MEMBASE:
3294 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
3296 case OP_AMD64_AND_REG_MEMBASE:
3297 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
3299 case OP_AMD64_OR_REG_MEMBASE:
3300 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
3302 case OP_AMD64_XOR_REG_MEMBASE:
3303 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
3306 case OP_AMD64_ADD_MEMBASE_REG:
3307 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
3309 case OP_AMD64_SUB_MEMBASE_REG:
3310 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
3312 case OP_AMD64_AND_MEMBASE_REG:
3313 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
3315 case OP_AMD64_OR_MEMBASE_REG:
3316 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
3318 case OP_AMD64_XOR_MEMBASE_REG:
3319 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
3322 case OP_AMD64_ADD_MEMBASE_IMM:
3323 g_assert (amd64_is_imm32 (ins->inst_imm));
3324 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
3326 case OP_AMD64_SUB_MEMBASE_IMM:
3327 g_assert (amd64_is_imm32 (ins->inst_imm));
3328 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
3330 case OP_AMD64_AND_MEMBASE_IMM:
3331 g_assert (amd64_is_imm32 (ins->inst_imm));
3332 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
3334 case OP_AMD64_OR_MEMBASE_IMM:
3335 g_assert (amd64_is_imm32 (ins->inst_imm));
3336 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
3338 case OP_AMD64_XOR_MEMBASE_IMM:
3339 g_assert (amd64_is_imm32 (ins->inst_imm));
3340 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
3344 amd64_breakpoint (code);
3346 case OP_RELAXED_NOP:
3347 x86_prefix (code, X86_REP_PREFIX);
3352 case OP_DUMMY_STORE:
3353 case OP_NOT_REACHED:
3358 amd64_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
3361 amd64_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
3365 g_assert (amd64_is_imm32 (ins->inst_imm));
3366 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
3369 g_assert (amd64_is_imm32 (ins->inst_imm));
3370 amd64_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
3374 amd64_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
3377 amd64_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
3381 g_assert (amd64_is_imm32 (ins->inst_imm));
3382 amd64_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
3385 g_assert (amd64_is_imm32 (ins->inst_imm));
3386 amd64_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
3389 amd64_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
3393 g_assert (amd64_is_imm32 (ins->inst_imm));
3394 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
3397 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
3402 guint32 size = (ins->opcode == OP_IMUL_IMM) ? 4 : 8;
3404 switch (ins->inst_imm) {
3408 if (ins->dreg != ins->sreg1)
3409 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, size);
3410 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
3413 /* LEA r1, [r2 + r2*2] */
3414 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
3417 /* LEA r1, [r2 + r2*4] */
3418 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3421 /* LEA r1, [r2 + r2*2] */
3423 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
3424 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
3427 /* LEA r1, [r2 + r2*8] */
3428 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
3431 /* LEA r1, [r2 + r2*4] */
3433 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3434 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
3437 /* LEA r1, [r2 + r2*2] */
3439 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
3440 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
3443 /* LEA r1, [r2 + r2*4] */
3444 /* LEA r1, [r1 + r1*4] */
3445 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3446 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
3449 /* LEA r1, [r2 + r2*4] */
3451 /* LEA r1, [r1 + r1*4] */
3452 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3453 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
3454 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
3457 amd64_imul_reg_reg_imm_size (code, ins->dreg, ins->sreg1, ins->inst_imm, size);
3464 /* Regalloc magic makes the div/rem cases the same */
3465 if (ins->sreg2 == AMD64_RDX) {
3466 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
3468 amd64_div_membase (code, AMD64_RSP, -8, TRUE);
3471 amd64_div_reg (code, ins->sreg2, TRUE);
3476 if (ins->sreg2 == AMD64_RDX) {
3477 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
3478 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
3479 amd64_div_membase (code, AMD64_RSP, -8, FALSE);
3481 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
3482 amd64_div_reg (code, ins->sreg2, FALSE);
3487 if (ins->sreg2 == AMD64_RDX) {
3488 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
3489 amd64_cdq_size (code, 4);
3490 amd64_div_membase_size (code, AMD64_RSP, -8, TRUE, 4);
3492 amd64_cdq_size (code, 4);
3493 amd64_div_reg_size (code, ins->sreg2, TRUE, 4);
3498 if (ins->sreg2 == AMD64_RDX) {
3499 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
3500 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
3501 amd64_div_membase_size (code, AMD64_RSP, -8, FALSE, 4);
3503 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
3504 amd64_div_reg_size (code, ins->sreg2, FALSE, 4);
3508 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
3509 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
3512 amd64_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
3516 g_assert (amd64_is_imm32 (ins->inst_imm));
3517 amd64_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
3520 amd64_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
3524 g_assert (amd64_is_imm32 (ins->inst_imm));
3525 amd64_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
3528 g_assert (ins->sreg2 == AMD64_RCX);
3529 amd64_shift_reg (code, X86_SHL, ins->dreg);
3532 g_assert (ins->sreg2 == AMD64_RCX);
3533 amd64_shift_reg (code, X86_SAR, ins->dreg);
3536 g_assert (amd64_is_imm32 (ins->inst_imm));
3537 amd64_shift_reg_imm_size (code, X86_SAR, ins->dreg, ins->inst_imm, 4);
3540 g_assert (amd64_is_imm32 (ins->inst_imm));
3541 amd64_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
3544 g_assert (amd64_is_imm32 (ins->inst_imm));
3545 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
3547 case OP_LSHR_UN_IMM:
3548 g_assert (amd64_is_imm32 (ins->inst_imm));
3549 amd64_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
3552 g_assert (ins->sreg2 == AMD64_RCX);
3553 amd64_shift_reg (code, X86_SHR, ins->dreg);
3556 g_assert (amd64_is_imm32 (ins->inst_imm));
3557 amd64_shift_reg_imm_size (code, X86_SHL, ins->dreg, ins->inst_imm, 4);
3560 g_assert (amd64_is_imm32 (ins->inst_imm));
3561 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
3566 amd64_alu_reg_reg_size (code, X86_ADD, ins->sreg1, ins->sreg2, 4);
3569 amd64_alu_reg_reg_size (code, X86_ADC, ins->sreg1, ins->sreg2, 4);
3572 amd64_alu_reg_imm_size (code, X86_ADD, ins->dreg, ins->inst_imm, 4);
3575 amd64_alu_reg_imm_size (code, X86_ADC, ins->dreg, ins->inst_imm, 4);
3579 amd64_alu_reg_reg_size (code, X86_SUB, ins->sreg1, ins->sreg2, 4);
3582 amd64_alu_reg_reg_size (code, X86_SBB, ins->sreg1, ins->sreg2, 4);
3585 amd64_alu_reg_imm_size (code, X86_SUB, ins->dreg, ins->inst_imm, 4);
3588 amd64_alu_reg_imm_size (code, X86_SBB, ins->dreg, ins->inst_imm, 4);
3591 amd64_alu_reg_reg_size (code, X86_AND, ins->sreg1, ins->sreg2, 4);
3594 amd64_alu_reg_imm_size (code, X86_AND, ins->sreg1, ins->inst_imm, 4);
3597 amd64_alu_reg_reg_size (code, X86_OR, ins->sreg1, ins->sreg2, 4);
3600 amd64_alu_reg_imm_size (code, X86_OR, ins->sreg1, ins->inst_imm, 4);
3603 amd64_alu_reg_reg_size (code, X86_XOR, ins->sreg1, ins->sreg2, 4);
3606 amd64_alu_reg_imm_size (code, X86_XOR, ins->sreg1, ins->inst_imm, 4);
3609 amd64_neg_reg_size (code, ins->sreg1, 4);
3612 amd64_not_reg_size (code, ins->sreg1, 4);
3615 g_assert (ins->sreg2 == AMD64_RCX);
3616 amd64_shift_reg_size (code, X86_SHL, ins->dreg, 4);
3619 g_assert (ins->sreg2 == AMD64_RCX);
3620 amd64_shift_reg_size (code, X86_SAR, ins->dreg, 4);
3623 amd64_shift_reg_imm_size (code, X86_SAR, ins->dreg, ins->inst_imm, 4);
3625 case OP_ISHR_UN_IMM:
3626 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
3629 g_assert (ins->sreg2 == AMD64_RCX);
3630 amd64_shift_reg_size (code, X86_SHR, ins->dreg, 4);
3633 amd64_shift_reg_imm_size (code, X86_SHL, ins->dreg, ins->inst_imm, 4);
3636 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
3639 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
3640 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
3642 case OP_IMUL_OVF_UN:
3643 case OP_LMUL_OVF_UN: {
3644 /* the mul operation and the exception check should most likely be split */
3645 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
3646 int size = (ins->opcode == OP_IMUL_OVF_UN) ? 4 : 8;
3647 /*g_assert (ins->sreg2 == X86_EAX);
3648 g_assert (ins->dreg == X86_EAX);*/
3649 if (ins->sreg2 == X86_EAX) {
3650 non_eax_reg = ins->sreg1;
3651 } else if (ins->sreg1 == X86_EAX) {
3652 non_eax_reg = ins->sreg2;
3654 /* no need to save since we're going to store to it anyway */
3655 if (ins->dreg != X86_EAX) {
3657 amd64_push_reg (code, X86_EAX);
3659 amd64_mov_reg_reg (code, X86_EAX, ins->sreg1, size);
3660 non_eax_reg = ins->sreg2;
3662 if (ins->dreg == X86_EDX) {
3665 amd64_push_reg (code, X86_EAX);
3669 amd64_push_reg (code, X86_EDX);
3671 amd64_mul_reg_size (code, non_eax_reg, FALSE, size);
3672 /* save before the check since pop and mov don't change the flags */
3673 if (ins->dreg != X86_EAX)
3674 amd64_mov_reg_reg (code, ins->dreg, X86_EAX, size);
3676 amd64_pop_reg (code, X86_EDX);
3678 amd64_pop_reg (code, X86_EAX);
3679 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
3683 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
3685 case OP_ICOMPARE_IMM:
3686 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg1, ins->inst_imm, 4);
3708 EMIT_COND_BRANCH (ins, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
3716 case OP_CMOV_INE_UN:
3717 case OP_CMOV_IGE_UN:
3718 case OP_CMOV_IGT_UN:
3719 case OP_CMOV_ILE_UN:
3720 case OP_CMOV_ILT_UN:
3726 case OP_CMOV_LNE_UN:
3727 case OP_CMOV_LGE_UN:
3728 case OP_CMOV_LGT_UN:
3729 case OP_CMOV_LLE_UN:
3730 case OP_CMOV_LLT_UN:
3731 g_assert (ins->dreg == ins->sreg1);
3732 /* This needs to operate on 64 bit values */
3733 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);
3737 amd64_not_reg (code, ins->sreg1);
3740 amd64_neg_reg (code, ins->sreg1);
3745 if ((((guint64)ins->inst_c0) >> 32) == 0)
3746 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 4);
3748 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 8);
3751 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3752 amd64_mov_reg_membase (code, ins->dreg, AMD64_RIP, 0, 8);
3755 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3756 amd64_mov_reg_imm_size (code, ins->dreg, 0, 8);
3759 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, sizeof (gpointer));
3761 case OP_AMD64_SET_XMMREG_R4: {
3762 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
3765 case OP_AMD64_SET_XMMREG_R8: {
3766 if (ins->dreg != ins->sreg1)
3767 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
3773 * Note: this 'frame destruction' logic is useful for tail calls, too.
3774 * Keep in sync with the code in emit_epilog.
3778 /* FIXME: no tracing support... */
3779 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3780 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
3782 g_assert (!cfg->method->save_lmf);
3784 if (ins->opcode == OP_JMP)
3785 code = emit_load_volatile_arguments (cfg, code);
3787 if (cfg->arch.omit_fp) {
3788 guint32 save_offset = 0;
3789 /* Pop callee-saved registers */
3790 for (i = 0; i < AMD64_NREG; ++i)
3791 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
3792 amd64_mov_reg_membase (code, i, AMD64_RSP, save_offset, 8);
3795 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
3798 for (i = 0; i < AMD64_NREG; ++i)
3799 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i)))
3800 pos -= sizeof (gpointer);
3803 amd64_lea_membase (code, AMD64_RSP, AMD64_RBP, pos);
3805 /* Pop registers in reverse order */
3806 for (i = AMD64_NREG - 1; i > 0; --i)
3807 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
3808 amd64_pop_reg (code, i);
3814 offset = code - cfg->native_code;
3815 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
3816 if (cfg->compile_aot)
3817 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
3819 amd64_set_reg_template (code, AMD64_R11);
3820 amd64_jump_reg (code, AMD64_R11);
3824 /* ensure ins->sreg1 is not NULL */
3825 amd64_alu_membase_imm_size (code, X86_CMP, ins->sreg1, 0, 0, 4);
3828 amd64_lea_membase (code, AMD64_R11, cfg->frame_reg, cfg->sig_cookie);
3829 amd64_mov_membase_reg (code, ins->sreg1, 0, AMD64_R11, 8);
3838 call = (MonoCallInst*)ins;
3840 * The AMD64 ABI forces callers to know about varargs.
3842 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke))
3843 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
3844 else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
3846 * Since the unmanaged calling convention doesn't contain a
3847 * 'vararg' entry, we have to treat every pinvoke call as a
3848 * potential vararg call.
3852 for (i = 0; i < AMD64_XMM_NREG; ++i)
3853 if (call->used_fregs & (1 << i))
3856 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
3858 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
3861 if (ins->flags & MONO_INST_HAS_METHOD)
3862 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method, FALSE);
3864 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr, FALSE);
3865 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature->call_convention))
3866 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, call->stack_usage);
3867 code = emit_move_return_value (cfg, ins, code);
3873 case OP_VOIDCALL_REG:
3875 call = (MonoCallInst*)ins;
3877 if (AMD64_IS_ARGUMENT_REG (ins->sreg1)) {
3878 amd64_mov_reg_reg (code, AMD64_R11, ins->sreg1, 8);
3879 ins->sreg1 = AMD64_R11;
3883 * The AMD64 ABI forces callers to know about varargs.
3885 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke)) {
3886 if (ins->sreg1 == AMD64_RAX) {
3887 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
3888 ins->sreg1 = AMD64_R11;
3890 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
3891 } else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
3893 * Since the unmanaged calling convention doesn't contain a
3894 * 'vararg' entry, we have to treat every pinvoke call as a
3895 * potential vararg call.
3899 for (i = 0; i < AMD64_XMM_NREG; ++i)
3900 if (call->used_fregs & (1 << i))
3902 if (ins->sreg1 == AMD64_RAX) {
3903 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
3904 ins->sreg1 = AMD64_R11;
3907 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
3909 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
3912 amd64_call_reg (code, ins->sreg1);
3913 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature->call_convention))
3914 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, call->stack_usage);
3915 code = emit_move_return_value (cfg, ins, code);
3917 case OP_FCALL_MEMBASE:
3918 case OP_LCALL_MEMBASE:
3919 case OP_VCALL_MEMBASE:
3920 case OP_VCALL2_MEMBASE:
3921 case OP_VOIDCALL_MEMBASE:
3922 case OP_CALL_MEMBASE:
3923 call = (MonoCallInst*)ins;
3925 if (AMD64_IS_ARGUMENT_REG (ins->sreg1)) {
3927 * Can't use R11 because it is clobbered by the trampoline
3928 * code, and the reg value is needed by get_vcall_slot_addr.
3930 amd64_mov_reg_reg (code, AMD64_RAX, ins->sreg1, 8);
3931 ins->sreg1 = AMD64_RAX;
3934 if (call->method && ins->inst_offset < 0) {
3938 * This is a possible IMT call so save the IMT method in the proper
3939 * register. We don't use the generic code in method-to-ir.c, because
3940 * we need to disassemble this in get_vcall_slot_addr (), so we have to
3941 * maintain control over the layout of the code.
3942 * Also put the base reg in %rax to simplify find_imt_method ().
3944 if (ins->sreg1 != AMD64_RAX) {
3945 amd64_mov_reg_reg (code, AMD64_RAX, ins->sreg1, 8);
3946 ins->sreg1 = AMD64_RAX;
3948 val = (gssize)(gpointer)call->method;
3950 // FIXME: Generics sharing
3952 if ((((guint64)val) >> 32) == 0)
3953 amd64_mov_reg_imm_size (code, MONO_ARCH_IMT_REG, val, 4);
3955 amd64_mov_reg_imm_size (code, MONO_ARCH_IMT_REG, val, 8);
3959 amd64_call_membase (code, ins->sreg1, ins->inst_offset);
3960 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature->call_convention))
3961 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, call->stack_usage);
3962 code = emit_move_return_value (cfg, ins, code);
3964 case OP_AMD64_SAVE_SP_TO_LMF:
3965 amd64_mov_membase_reg (code, cfg->frame_reg, cfg->arch.lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
3969 amd64_push_reg (code, ins->sreg1);
3971 case OP_X86_PUSH_IMM:
3972 g_assert (amd64_is_imm32 (ins->inst_imm));
3973 amd64_push_imm (code, ins->inst_imm);
3975 case OP_X86_PUSH_MEMBASE:
3976 amd64_push_membase (code, ins->inst_basereg, ins->inst_offset);
3978 case OP_X86_PUSH_OBJ: {
3979 int size = ALIGN_TO (ins->inst_imm, 8);
3980 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
3981 amd64_push_reg (code, AMD64_RDI);
3982 amd64_push_reg (code, AMD64_RSI);
3983 amd64_push_reg (code, AMD64_RCX);
3984 if (ins->inst_offset)
3985 amd64_lea_membase (code, AMD64_RSI, ins->inst_basereg, ins->inst_offset);
3987 amd64_mov_reg_reg (code, AMD64_RSI, ins->inst_basereg, 8);
3988 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, (3 * 8) + (size - ins->inst_imm));
3989 amd64_mov_reg_imm (code, AMD64_RCX, (ins->inst_imm >> 3));
3991 amd64_prefix (code, X86_REP_PREFIX);
3993 amd64_pop_reg (code, AMD64_RCX);
3994 amd64_pop_reg (code, AMD64_RSI);
3995 amd64_pop_reg (code, AMD64_RDI);
3999 amd64_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
4001 case OP_X86_LEA_MEMBASE:
4002 amd64_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
4005 amd64_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
4008 /* keep alignment */
4009 amd64_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1);
4010 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_FRAME_ALIGNMENT - 1));
4011 code = mono_emit_stack_alloc (code, ins);
4012 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4014 case OP_LOCALLOC_IMM: {
4015 guint32 size = ins->inst_imm;
4016 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
4018 if (ins->flags & MONO_INST_INIT) {
4022 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
4023 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4025 for (i = 0; i < size; i += 8)
4026 amd64_mov_membase_reg (code, AMD64_RSP, i, ins->dreg, 8);
4027 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4029 amd64_mov_reg_imm (code, ins->dreg, size);
4030 ins->sreg1 = ins->dreg;
4032 code = mono_emit_stack_alloc (code, ins);
4033 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4036 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
4037 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4042 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
4043 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
4044 (gpointer)"mono_arch_throw_exception", FALSE);
4048 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
4049 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
4050 (gpointer)"mono_arch_rethrow_exception", FALSE);
4053 case OP_CALL_HANDLER:
4055 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
4056 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
4057 amd64_call_imm (code, 0);
4058 /* Restore stack alignment */
4059 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
4061 case OP_START_HANDLER: {
4062 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4063 amd64_mov_membase_reg (code, spvar->inst_basereg, spvar->inst_offset, AMD64_RSP, 8);
4066 case OP_ENDFINALLY: {
4067 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4068 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, 8);
4072 case OP_ENDFILTER: {
4073 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4074 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, 8);
4075 /* The local allocator will put the result into RAX */
4081 ins->inst_c0 = code - cfg->native_code;
4084 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
4085 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
4087 if (ins->flags & MONO_INST_BRLABEL) {
4088 if (ins->inst_i0->inst_c0) {
4089 amd64_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
4091 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
4092 if ((cfg->opt & MONO_OPT_BRANCH) &&
4093 x86_is_imm8 (ins->inst_i0->inst_c1 - cpos))
4094 x86_jump8 (code, 0);
4096 x86_jump32 (code, 0);
4099 if (ins->inst_target_bb->native_offset) {
4100 amd64_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
4102 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
4103 if ((cfg->opt & MONO_OPT_BRANCH) &&
4104 x86_is_imm8 (ins->inst_target_bb->max_offset - cpos))
4105 x86_jump8 (code, 0);
4107 x86_jump32 (code, 0);
4112 amd64_jump_reg (code, ins->sreg1);
4129 amd64_set_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
4130 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
4132 case OP_COND_EXC_EQ:
4133 case OP_COND_EXC_NE_UN:
4134 case OP_COND_EXC_LT:
4135 case OP_COND_EXC_LT_UN:
4136 case OP_COND_EXC_GT:
4137 case OP_COND_EXC_GT_UN:
4138 case OP_COND_EXC_GE:
4139 case OP_COND_EXC_GE_UN:
4140 case OP_COND_EXC_LE:
4141 case OP_COND_EXC_LE_UN:
4142 case OP_COND_EXC_IEQ:
4143 case OP_COND_EXC_INE_UN:
4144 case OP_COND_EXC_ILT:
4145 case OP_COND_EXC_ILT_UN:
4146 case OP_COND_EXC_IGT:
4147 case OP_COND_EXC_IGT_UN:
4148 case OP_COND_EXC_IGE:
4149 case OP_COND_EXC_IGE_UN:
4150 case OP_COND_EXC_ILE:
4151 case OP_COND_EXC_ILE_UN:
4152 EMIT_COND_SYSTEM_EXCEPTION (cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)], ins->inst_p1);
4154 case OP_COND_EXC_OV:
4155 case OP_COND_EXC_NO:
4157 case OP_COND_EXC_NC:
4158 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
4159 (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
4161 case OP_COND_EXC_IOV:
4162 case OP_COND_EXC_INO:
4163 case OP_COND_EXC_IC:
4164 case OP_COND_EXC_INC:
4165 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_IEQ],
4166 (ins->opcode < OP_COND_EXC_INE_UN), ins->inst_p1);
4169 /* floating point opcodes */
4171 double d = *(double *)ins->inst_p0;
4173 if ((d == 0.0) && (mono_signbit (d) == 0)) {
4174 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
4177 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
4178 amd64_sse_movsd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
4183 float f = *(float *)ins->inst_p0;
4185 if ((f == 0.0) && (mono_signbit (f) == 0)) {
4186 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
4189 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
4190 amd64_sse_movss_reg_membase (code, ins->dreg, AMD64_RIP, 0);
4191 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
4195 case OP_STORER8_MEMBASE_REG:
4196 amd64_sse_movsd_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
4198 case OP_LOADR8_SPILL_MEMBASE:
4199 g_assert_not_reached ();
4201 case OP_LOADR8_MEMBASE:
4202 amd64_sse_movsd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4204 case OP_STORER4_MEMBASE_REG:
4205 /* This requires a double->single conversion */
4206 amd64_sse_cvtsd2ss_reg_reg (code, AMD64_XMM15, ins->sreg1);
4207 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, AMD64_XMM15);
4209 case OP_LOADR4_MEMBASE:
4210 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4211 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
4213 case OP_ICONV_TO_R4: /* FIXME: change precision */
4214 case OP_ICONV_TO_R8:
4215 amd64_sse_cvtsi2sd_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
4217 case OP_LCONV_TO_R4: /* FIXME: change precision */
4218 case OP_LCONV_TO_R8:
4219 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
4221 case OP_FCONV_TO_R4:
4222 /* FIXME: nothing to do ?? */
4224 case OP_FCONV_TO_I1:
4225 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
4227 case OP_FCONV_TO_U1:
4228 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
4230 case OP_FCONV_TO_I2:
4231 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
4233 case OP_FCONV_TO_U2:
4234 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
4236 case OP_FCONV_TO_U4:
4237 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
4239 case OP_FCONV_TO_I4:
4241 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
4243 case OP_FCONV_TO_I8:
4244 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 8, TRUE);
4246 case OP_LCONV_TO_R_UN: {
4249 /* Based on gcc code */
4250 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
4251 br [0] = code; x86_branch8 (code, X86_CC_S, 0, TRUE);
4254 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
4255 br [1] = code; x86_jump8 (code, 0);
4256 amd64_patch (br [0], code);
4259 /* Save to the red zone */
4260 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RAX, 8);
4261 amd64_mov_membase_reg (code, AMD64_RSP, -16, AMD64_RCX, 8);
4262 amd64_mov_reg_reg (code, AMD64_RCX, ins->sreg1, 8);
4263 amd64_mov_reg_reg (code, AMD64_RAX, ins->sreg1, 8);
4264 amd64_alu_reg_imm (code, X86_AND, AMD64_RCX, 1);
4265 amd64_shift_reg_imm (code, X86_SHR, AMD64_RAX, 1);
4266 amd64_alu_reg_imm (code, X86_OR, AMD64_RAX, AMD64_RCX);
4267 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, AMD64_RAX);
4268 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->dreg);
4270 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
4271 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, -8, 8);
4272 amd64_patch (br [1], code);
4275 case OP_LCONV_TO_OVF_U4:
4276 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0);
4277 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_LT, TRUE, "OverflowException");
4278 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
4280 case OP_LCONV_TO_OVF_I4_UN:
4281 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0x7fffffff);
4282 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_GT, FALSE, "OverflowException");
4283 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
4286 if (ins->dreg != ins->sreg1)
4287 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
4290 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->sreg2);
4293 amd64_sse_subsd_reg_reg (code, ins->dreg, ins->sreg2);
4296 amd64_sse_mulsd_reg_reg (code, ins->dreg, ins->sreg2);
4299 amd64_sse_divsd_reg_reg (code, ins->dreg, ins->sreg2);
4302 static double r8_0 = -0.0;
4304 g_assert (ins->sreg1 == ins->dreg);
4306 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &r8_0);
4307 amd64_sse_xorpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
4311 EMIT_SSE2_FPFUNC (code, fsin, ins->dreg, ins->sreg1);
4314 EMIT_SSE2_FPFUNC (code, fcos, ins->dreg, ins->sreg1);
4317 static guint64 d = 0x7fffffffffffffffUL;
4319 g_assert (ins->sreg1 == ins->dreg);
4321 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &d);
4322 amd64_sse_andpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
4326 EMIT_SSE2_FPFUNC (code, fsqrt, ins->dreg, ins->sreg1);
4329 g_assert (cfg->opt & MONO_OPT_CMOV);
4330 g_assert (ins->dreg == ins->sreg1);
4331 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
4332 amd64_cmov_reg_size (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2, 4);
4335 g_assert (cfg->opt & MONO_OPT_CMOV);
4336 g_assert (ins->dreg == ins->sreg1);
4337 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
4338 amd64_cmov_reg_size (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2, 4);
4341 g_assert (cfg->opt & MONO_OPT_CMOV);
4342 g_assert (ins->dreg == ins->sreg1);
4343 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
4344 amd64_cmov_reg_size (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2, 4);
4347 g_assert (cfg->opt & MONO_OPT_CMOV);
4348 g_assert (ins->dreg == ins->sreg1);
4349 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
4350 amd64_cmov_reg_size (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2, 4);
4353 g_assert (cfg->opt & MONO_OPT_CMOV);
4354 g_assert (ins->dreg == ins->sreg1);
4355 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
4356 amd64_cmov_reg (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2);
4359 g_assert (cfg->opt & MONO_OPT_CMOV);
4360 g_assert (ins->dreg == ins->sreg1);
4361 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
4362 amd64_cmov_reg (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2);
4365 g_assert (cfg->opt & MONO_OPT_CMOV);
4366 g_assert (ins->dreg == ins->sreg1);
4367 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
4368 amd64_cmov_reg (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2);
4371 g_assert (cfg->opt & MONO_OPT_CMOV);
4372 g_assert (ins->dreg == ins->sreg1);
4373 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
4374 amd64_cmov_reg (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2);
4380 * The two arguments are swapped because the fbranch instructions
4381 * depend on this for the non-sse case to work.
4383 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
4386 /* zeroing the register at the start results in
4387 * shorter and faster code (we can also remove the widening op)
4389 guchar *unordered_check;
4390 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4391 amd64_sse_comisd_reg_reg (code, ins->sreg1, ins->sreg2);
4392 unordered_check = code;
4393 x86_branch8 (code, X86_CC_P, 0, FALSE);
4394 amd64_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
4395 amd64_patch (unordered_check, code);
4400 /* zeroing the register at the start results in
4401 * shorter and faster code (we can also remove the widening op)
4403 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4404 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
4405 if (ins->opcode == OP_FCLT_UN) {
4406 guchar *unordered_check = code;
4407 guchar *jump_to_end;
4408 x86_branch8 (code, X86_CC_P, 0, FALSE);
4409 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
4411 x86_jump8 (code, 0);
4412 amd64_patch (unordered_check, code);
4413 amd64_inc_reg (code, ins->dreg);
4414 amd64_patch (jump_to_end, code);
4416 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
4421 /* zeroing the register at the start results in
4422 * shorter and faster code (we can also remove the widening op)
4424 guchar *unordered_check;
4425 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4426 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
4427 if (ins->opcode == OP_FCGT) {
4428 unordered_check = code;
4429 x86_branch8 (code, X86_CC_P, 0, FALSE);
4430 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
4431 amd64_patch (unordered_check, code);
4433 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
4437 case OP_FCLT_MEMBASE:
4438 case OP_FCGT_MEMBASE:
4439 case OP_FCLT_UN_MEMBASE:
4440 case OP_FCGT_UN_MEMBASE:
4441 case OP_FCEQ_MEMBASE: {
4442 guchar *unordered_check, *jump_to_end;
4445 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4446 amd64_sse_comisd_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
4448 switch (ins->opcode) {
4449 case OP_FCEQ_MEMBASE:
4450 x86_cond = X86_CC_EQ;
4452 case OP_FCLT_MEMBASE:
4453 case OP_FCLT_UN_MEMBASE:
4454 x86_cond = X86_CC_LT;
4456 case OP_FCGT_MEMBASE:
4457 case OP_FCGT_UN_MEMBASE:
4458 x86_cond = X86_CC_GT;
4461 g_assert_not_reached ();
4464 unordered_check = code;
4465 x86_branch8 (code, X86_CC_P, 0, FALSE);
4466 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
4468 switch (ins->opcode) {
4469 case OP_FCEQ_MEMBASE:
4470 case OP_FCLT_MEMBASE:
4471 case OP_FCGT_MEMBASE:
4472 amd64_patch (unordered_check, code);
4474 case OP_FCLT_UN_MEMBASE:
4475 case OP_FCGT_UN_MEMBASE:
4477 x86_jump8 (code, 0);
4478 amd64_patch (unordered_check, code);
4479 amd64_inc_reg (code, ins->dreg);
4480 amd64_patch (jump_to_end, code);
4488 guchar *jump = code;
4489 x86_branch8 (code, X86_CC_P, 0, TRUE);
4490 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4491 amd64_patch (jump, code);
4495 /* Branch if C013 != 100 */
4496 /* branch if !ZF or (PF|CF) */
4497 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
4498 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
4499 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
4502 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
4505 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
4506 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
4510 if (ins->opcode == OP_FBGT) {
4513 /* skip branch if C1=1 */
4515 x86_branch8 (code, X86_CC_P, 0, FALSE);
4516 /* branch if (C0 | C3) = 1 */
4517 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
4518 amd64_patch (br1, code);
4521 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
4525 /* Branch if C013 == 100 or 001 */
4528 /* skip branch if C1=1 */
4530 x86_branch8 (code, X86_CC_P, 0, FALSE);
4531 /* branch if (C0 | C3) = 1 */
4532 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
4533 amd64_patch (br1, code);
4537 /* Branch if C013 == 000 */
4538 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
4541 /* Branch if C013=000 or 100 */
4544 /* skip branch if C1=1 */
4546 x86_branch8 (code, X86_CC_P, 0, FALSE);
4547 /* branch if C0=0 */
4548 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
4549 amd64_patch (br1, code);
4553 /* Branch if C013 != 001 */
4554 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
4555 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
4558 /* Transfer value to the fp stack */
4559 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 16);
4560 amd64_movsd_membase_reg (code, AMD64_RSP, 0, ins->sreg1);
4561 amd64_fld_membase (code, AMD64_RSP, 0, TRUE);
4563 amd64_push_reg (code, AMD64_RAX);
4565 amd64_fnstsw (code);
4566 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0x4100);
4567 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, X86_FP_C0);
4568 amd64_pop_reg (code, AMD64_RAX);
4569 amd64_fstp (code, 0);
4570 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
4571 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 16);
4574 code = emit_tls_get (code, ins->dreg, ins->inst_offset);
4577 case OP_MEMORY_BARRIER: {
4578 /* Not needed on amd64 */
4581 case OP_ATOMIC_ADD_I4:
4582 case OP_ATOMIC_ADD_I8: {
4583 int dreg = ins->dreg;
4584 guint32 size = (ins->opcode == OP_ATOMIC_ADD_I4) ? 4 : 8;
4586 if (dreg == ins->inst_basereg)
4589 if (dreg != ins->sreg2)
4590 amd64_mov_reg_reg (code, ins->dreg, ins->sreg2, size);
4592 x86_prefix (code, X86_LOCK_PREFIX);
4593 amd64_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, size);
4595 if (dreg != ins->dreg)
4596 amd64_mov_reg_reg (code, ins->dreg, dreg, size);
4600 case OP_ATOMIC_ADD_NEW_I4:
4601 case OP_ATOMIC_ADD_NEW_I8: {
4602 int dreg = ins->dreg;
4603 guint32 size = (ins->opcode == OP_ATOMIC_ADD_NEW_I4) ? 4 : 8;
4605 if ((dreg == ins->sreg2) || (dreg == ins->inst_basereg))
4608 amd64_mov_reg_reg (code, dreg, ins->sreg2, size);
4609 amd64_prefix (code, X86_LOCK_PREFIX);
4610 amd64_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, size);
4611 /* dreg contains the old value, add with sreg2 value */
4612 amd64_alu_reg_reg_size (code, X86_ADD, dreg, ins->sreg2, size);
4614 if (ins->dreg != dreg)
4615 amd64_mov_reg_reg (code, ins->dreg, dreg, size);
4619 case OP_ATOMIC_EXCHANGE_I4:
4620 case OP_ATOMIC_EXCHANGE_I8:
4621 case OP_ATOMIC_CAS_IMM_I4: {
4623 int sreg2 = ins->sreg2;
4624 int breg = ins->inst_basereg;
4626 gboolean need_push = FALSE, rdx_pushed = FALSE;
4628 if (ins->opcode == OP_ATOMIC_EXCHANGE_I8)
4634 * See http://msdn.microsoft.com/en-us/magazine/cc302329.aspx for
4635 * an explanation of how this works.
4638 /* cmpxchg uses eax as comperand, need to make sure we can use it
4639 * hack to overcome limits in x86 reg allocator
4640 * (req: dreg == eax and sreg2 != eax and breg != eax)
4642 g_assert (ins->dreg == AMD64_RAX);
4644 if (breg == AMD64_RAX && ins->sreg2 == AMD64_RAX)
4645 /* Highly unlikely, but possible */
4648 /* The pushes invalidate rsp */
4649 if ((breg == AMD64_RAX) || need_push) {
4650 amd64_mov_reg_reg (code, AMD64_R11, breg, 8);
4654 /* We need the EAX reg for the comparand */
4655 if (ins->sreg2 == AMD64_RAX) {
4656 if (breg != AMD64_R11) {
4657 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
4660 g_assert (need_push);
4661 amd64_push_reg (code, AMD64_RDX);
4662 amd64_mov_reg_reg (code, AMD64_RDX, AMD64_RAX, size);
4668 if (ins->opcode == OP_ATOMIC_CAS_IMM_I4) {
4669 if (ins->backend.data == NULL)
4670 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4672 amd64_mov_reg_imm (code, AMD64_RAX, ins->backend.data);
4674 amd64_prefix (code, X86_LOCK_PREFIX);
4675 amd64_cmpxchg_membase_reg_size (code, breg, ins->inst_offset, sreg2, size);
4677 amd64_mov_reg_membase (code, AMD64_RAX, breg, ins->inst_offset, size);
4679 br [0] = code; amd64_prefix (code, X86_LOCK_PREFIX);
4680 amd64_cmpxchg_membase_reg_size (code, breg, ins->inst_offset, sreg2, size);
4681 br [1] = code; amd64_branch8 (code, X86_CC_NE, -1, FALSE);
4682 amd64_patch (br [1], br [0]);
4686 amd64_pop_reg (code, AMD64_RDX);
4691 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
4692 g_assert_not_reached ();
4695 if ((code - cfg->native_code - offset) > max_len) {
4696 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %ld)",
4697 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
4698 g_assert_not_reached ();
4704 last_offset = offset;
4707 cfg->code_len = code - cfg->native_code;
4710 #endif /* DISABLE_JIT */
4713 mono_arch_register_lowlevel_calls (void)
4715 /* The signature doesn't matter */
4716 mono_register_jit_icall (mono_amd64_throw_exception, "mono_amd64_throw_exception", mono_create_icall_signature ("void"), TRUE);
4720 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
4722 MonoJumpInfo *patch_info;
4723 gboolean compile_aot = !run_cctors;
4725 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
4726 unsigned char *ip = patch_info->ip.i + code;
4727 unsigned char *target;
4729 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
4732 switch (patch_info->type) {
4733 case MONO_PATCH_INFO_BB:
4734 case MONO_PATCH_INFO_LABEL:
4737 /* No need to patch these */
4742 switch (patch_info->type) {
4743 case MONO_PATCH_INFO_NONE:
4745 case MONO_PATCH_INFO_METHOD_REL:
4746 case MONO_PATCH_INFO_R8:
4747 case MONO_PATCH_INFO_R4:
4748 g_assert_not_reached ();
4750 case MONO_PATCH_INFO_BB:
4757 * Debug code to help track down problems where the target of a near call is
4760 if (amd64_is_near_call (ip)) {
4761 gint64 disp = (guint8*)target - (guint8*)ip;
4763 if (!amd64_is_imm32 (disp)) {
4764 printf ("TYPE: %d\n", patch_info->type);
4765 switch (patch_info->type) {
4766 case MONO_PATCH_INFO_INTERNAL_METHOD:
4767 printf ("V: %s\n", patch_info->data.name);
4769 case MONO_PATCH_INFO_METHOD_JUMP:
4770 case MONO_PATCH_INFO_METHOD:
4771 printf ("V: %s\n", patch_info->data.method->name);
4779 amd64_patch (ip, (gpointer)target);
4784 get_max_epilog_size (MonoCompile *cfg)
4786 int max_epilog_size = 16;
4788 if (cfg->method->save_lmf)
4789 max_epilog_size += 256;
4791 if (mono_jit_trace_calls != NULL)
4792 max_epilog_size += 50;
4794 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
4795 max_epilog_size += 50;
4797 max_epilog_size += (AMD64_NREG * 2);
4799 return max_epilog_size;
4803 * This macro is used for testing whenever the unwinder works correctly at every point
4804 * where an async exception can happen.
4806 /* This will generate a SIGSEGV at the given point in the code */
4807 #define async_exc_point(code) do { \
4808 if (mono_inject_async_exc_method && mono_method_desc_full_match (mono_inject_async_exc_method, cfg->method)) { \
4809 if (cfg->arch.async_point_count == mono_inject_async_exc_pos) \
4810 amd64_mov_reg_mem (code, AMD64_RAX, 0, 4); \
4811 cfg->arch.async_point_count ++; \
4816 mono_arch_emit_prolog (MonoCompile *cfg)
4818 MonoMethod *method = cfg->method;
4820 MonoMethodSignature *sig;
4822 int alloc_size, pos, max_offset, i, quad, max_epilog_size;
4825 gint32 lmf_offset = cfg->arch.lmf_offset;
4826 gboolean args_clobbered = FALSE;
4827 gboolean trace = FALSE;
4829 cfg->code_size = MAX (((MonoMethodNormal *)method)->header->code_size * 4, 10240);
4831 code = cfg->native_code = g_malloc (cfg->code_size);
4833 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
4836 /* Amount of stack space allocated by register saving code */
4840 * The prolog consists of the following parts:
4842 * - push rbp, mov rbp, rsp
4843 * - save callee saved regs using pushes
4845 * - save rgctx if needed
4846 * - save lmf if needed
4849 * - save rgctx if needed
4850 * - save lmf if needed
4851 * - save callee saved regs using moves
4854 async_exc_point (code);
4856 if (!cfg->arch.omit_fp) {
4857 amd64_push_reg (code, AMD64_RBP);
4858 async_exc_point (code);
4859 #ifdef PLATFORM_WIN32
4860 mono_arch_unwindinfo_add_push_nonvol (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
4863 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof (gpointer));
4864 async_exc_point (code);
4865 #ifdef PLATFORM_WIN32
4866 mono_arch_unwindinfo_add_set_fpreg (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
4870 /* Save callee saved registers */
4871 if (!cfg->arch.omit_fp && !method->save_lmf) {
4872 for (i = 0; i < AMD64_NREG; ++i)
4873 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
4874 amd64_push_reg (code, i);
4875 pos += sizeof (gpointer);
4876 async_exc_point (code);
4880 if (cfg->arch.omit_fp) {
4882 * On enter, the stack is misaligned by the the pushing of the return
4883 * address. It is either made aligned by the pushing of %rbp, or by
4886 alloc_size = ALIGN_TO (cfg->stack_offset, 8);
4887 if ((alloc_size % 16) == 0)
4890 alloc_size = ALIGN_TO (cfg->stack_offset, MONO_ARCH_FRAME_ALIGNMENT);
4895 cfg->arch.stack_alloc_size = alloc_size;
4897 /* Allocate stack frame */
4899 /* See mono_emit_stack_alloc */
4900 #if defined(PLATFORM_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
4901 guint32 remaining_size = alloc_size;
4902 while (remaining_size >= 0x1000) {
4903 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
4904 async_exc_point (code);
4905 #ifdef PLATFORM_WIN32
4906 if (cfg->arch.omit_fp)
4907 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, 0x1000);
4910 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
4911 remaining_size -= 0x1000;
4913 if (remaining_size) {
4914 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, remaining_size);
4915 async_exc_point (code);
4916 #ifdef PLATFORM_WIN32
4917 if (cfg->arch.omit_fp)
4918 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, remaining_size);
4922 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, alloc_size);
4923 async_exc_point (code);
4927 /* Stack alignment check */
4930 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_RSP, 8);
4931 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0xf);
4932 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, 0);
4933 x86_branch8 (code, X86_CC_EQ, 2, FALSE);
4934 amd64_breakpoint (code);
4939 if (method->save_lmf) {
4941 * The ip field is not set, the exception handling code will obtain it from the stack location pointed to by the sp field.
4943 /* sp is saved right before calls */
4944 /* Skip method (only needed for trampoline LMF frames) */
4945 /* Save callee saved regs */
4946 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbx), AMD64_RBX, 8);
4947 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbp), AMD64_RBP, 8);
4948 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r12), AMD64_R12, 8);
4949 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r13), AMD64_R13, 8);
4950 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r14), AMD64_R14, 8);
4951 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), AMD64_R15, 8);
4954 /* Save callee saved registers */
4955 if (cfg->arch.omit_fp && !method->save_lmf) {
4956 gint32 save_area_offset = cfg->arch.reg_save_area_offset;
4958 /* Save caller saved registers after sp is adjusted */
4959 /* The registers are saved at the bottom of the frame */
4960 /* FIXME: Optimize this so the regs are saved at the end of the frame in increasing order */
4961 for (i = 0; i < AMD64_NREG; ++i)
4962 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
4963 amd64_mov_membase_reg (code, AMD64_RSP, save_area_offset, i, 8);
4964 save_area_offset += 8;
4965 async_exc_point (code);
4969 /* store runtime generic context */
4970 if (cfg->rgctx_var) {
4971 g_assert (cfg->rgctx_var->opcode == OP_REGOFFSET &&
4972 (cfg->rgctx_var->inst_basereg == AMD64_RBP || cfg->rgctx_var->inst_basereg == AMD64_RSP));
4974 amd64_mov_membase_reg (code, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, MONO_ARCH_RGCTX_REG, 8);
4977 /* compute max_offset in order to use short forward jumps */
4979 max_epilog_size = get_max_epilog_size (cfg);
4980 if (cfg->opt & MONO_OPT_BRANCH) {
4981 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4983 bb->max_offset = max_offset;
4985 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
4987 /* max alignment for loops */
4988 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
4989 max_offset += LOOP_ALIGNMENT;
4991 MONO_BB_FOR_EACH_INS (bb, ins) {
4992 if (ins->opcode == OP_LABEL)
4993 ins->inst_c1 = max_offset;
4995 max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
4998 if (mono_jit_trace_calls && bb == cfg->bb_exit)
4999 /* The tracing code can be quite large */
5000 max_offset += max_epilog_size;
5004 sig = mono_method_signature (method);
5007 cinfo = cfg->arch.cinfo;
5009 if (sig->ret->type != MONO_TYPE_VOID) {
5010 /* Save volatile arguments to the stack */
5011 if (cfg->vret_addr && (cfg->vret_addr->opcode != OP_REGVAR))
5012 amd64_mov_membase_reg (code, cfg->vret_addr->inst_basereg, cfg->vret_addr->inst_offset, cinfo->ret.reg, 8);
5015 /* Keep this in sync with emit_load_volatile_arguments */
5016 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
5017 ArgInfo *ainfo = cinfo->args + i;
5018 gint32 stack_offset;
5021 ins = cfg->args [i];
5023 if ((ins->flags & MONO_INST_IS_DEAD) && !trace)
5024 /* Unused arguments */
5027 if (sig->hasthis && (i == 0))
5028 arg_type = &mono_defaults.object_class->byval_arg;
5030 arg_type = sig->params [i - sig->hasthis];
5032 stack_offset = ainfo->offset + ARGS_OFFSET;
5034 if (cfg->globalra) {
5035 /* All the other moves are done by the register allocator */
5036 switch (ainfo->storage) {
5037 case ArgInFloatSSEReg:
5038 amd64_sse_cvtss2sd_reg_reg (code, ainfo->reg, ainfo->reg);
5040 case ArgValuetypeInReg:
5041 for (quad = 0; quad < 2; quad ++) {
5042 switch (ainfo->pair_storage [quad]) {
5044 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad], sizeof (gpointer));
5046 case ArgInFloatSSEReg:
5047 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad]);
5049 case ArgInDoubleSSEReg:
5050 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad]);
5055 g_assert_not_reached ();
5066 /* Save volatile arguments to the stack */
5067 if (ins->opcode != OP_REGVAR) {
5068 switch (ainfo->storage) {
5074 if (stack_offset & 0x1)
5076 else if (stack_offset & 0x2)
5078 else if (stack_offset & 0x4)
5083 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg, size);
5086 case ArgInFloatSSEReg:
5087 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
5089 case ArgInDoubleSSEReg:
5090 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
5092 case ArgValuetypeInReg:
5093 for (quad = 0; quad < 2; quad ++) {
5094 switch (ainfo->pair_storage [quad]) {
5096 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad], sizeof (gpointer));
5098 case ArgInFloatSSEReg:
5099 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad]);
5101 case ArgInDoubleSSEReg:
5102 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad]);
5107 g_assert_not_reached ();
5111 case ArgValuetypeAddrInIReg:
5112 if (ainfo->pair_storage [0] == ArgInIReg)
5113 amd64_mov_membase_reg (code, ins->inst_left->inst_basereg, ins->inst_left->inst_offset, ainfo->pair_regs [0], sizeof (gpointer));
5119 /* Argument allocated to (non-volatile) register */
5120 switch (ainfo->storage) {
5122 amd64_mov_reg_reg (code, ins->dreg, ainfo->reg, 8);
5125 amd64_mov_reg_membase (code, ins->dreg, AMD64_RBP, ARGS_OFFSET + ainfo->offset, 8);
5128 g_assert_not_reached ();
5133 /* Might need to attach the thread to the JIT or change the domain for the callback */
5134 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
5135 guint64 domain = (guint64)cfg->domain;
5137 args_clobbered = TRUE;
5140 * The call might clobber argument registers, but they are already
5141 * saved to the stack/global regs.
5143 if (appdomain_tls_offset != -1 && lmf_tls_offset != -1) {
5144 guint8 *buf, *no_domain_branch;
5146 code = emit_tls_get (code, AMD64_RAX, appdomain_tls_offset);
5147 if ((domain >> 32) == 0)
5148 amd64_mov_reg_imm_size (code, AMD64_ARG_REG1, domain, 4);
5150 amd64_mov_reg_imm_size (code, AMD64_ARG_REG1, domain, 8);
5151 amd64_alu_reg_reg (code, X86_CMP, AMD64_RAX, AMD64_ARG_REG1);
5152 no_domain_branch = code;
5153 x86_branch8 (code, X86_CC_NE, 0, 0);
5154 code = emit_tls_get ( code, AMD64_RAX, lmf_addr_tls_offset);
5155 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
5157 x86_branch8 (code, X86_CC_NE, 0, 0);
5158 amd64_patch (no_domain_branch, code);
5159 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
5160 (gpointer)"mono_jit_thread_attach", TRUE);
5161 amd64_patch (buf, code);
5162 #ifdef PLATFORM_WIN32
5163 /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
5164 /* FIXME: Add a separate key for LMF to avoid this */
5165 amd64_alu_reg_imm (code, X86_ADD, AMD64_RAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
5168 g_assert (!cfg->compile_aot);
5169 if ((domain >> 32) == 0)
5170 amd64_mov_reg_imm_size (code, AMD64_ARG_REG1, domain, 4);
5172 amd64_mov_reg_imm_size (code, AMD64_ARG_REG1, domain, 8);
5173 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
5174 (gpointer)"mono_jit_thread_attach", TRUE);
5178 if (method->save_lmf) {
5179 if ((lmf_tls_offset != -1) && !optimize_for_xen) {
5181 * Optimized version which uses the mono_lmf TLS variable instead of indirection
5182 * through the mono_lmf_addr TLS variable.
5184 /* %rax = previous_lmf */
5185 x86_prefix (code, X86_FS_PREFIX);
5186 amd64_mov_reg_mem (code, AMD64_RAX, lmf_tls_offset, 8);
5188 /* Save previous_lmf */
5189 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_RAX, 8);
5191 if (lmf_offset == 0) {
5192 x86_prefix (code, X86_FS_PREFIX);
5193 amd64_mov_mem_reg (code, lmf_tls_offset, cfg->frame_reg, 8);
5195 amd64_lea_membase (code, AMD64_R11, cfg->frame_reg, lmf_offset);
5196 x86_prefix (code, X86_FS_PREFIX);
5197 amd64_mov_mem_reg (code, lmf_tls_offset, AMD64_R11, 8);
5200 if (lmf_addr_tls_offset != -1) {
5201 /* Load lmf quicky using the FS register */
5202 code = emit_tls_get (code, AMD64_RAX, lmf_addr_tls_offset);
5203 #ifdef PLATFORM_WIN32
5204 /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
5205 /* FIXME: Add a separate key for LMF to avoid this */
5206 amd64_alu_reg_imm (code, X86_ADD, AMD64_RAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
5211 * The call might clobber argument registers, but they are already
5212 * saved to the stack/global regs.
5214 args_clobbered = TRUE;
5215 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
5216 (gpointer)"mono_get_lmf_addr", TRUE);
5220 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), AMD64_RAX, 8);
5221 /* Save previous_lmf */
5222 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RAX, 0, 8);
5223 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_R11, 8);
5225 amd64_lea_membase (code, AMD64_R11, cfg->frame_reg, lmf_offset);
5226 amd64_mov_membase_reg (code, AMD64_RAX, 0, AMD64_R11, 8);
5231 args_clobbered = TRUE;
5232 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
5235 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
5236 args_clobbered = TRUE;
5239 * Optimize the common case of the first bblock making a call with the same
5240 * arguments as the method. This works because the arguments are still in their
5241 * original argument registers.
5242 * FIXME: Generalize this
5244 if (!args_clobbered) {
5245 MonoBasicBlock *first_bb = cfg->bb_entry;
5248 next = mono_bb_first_ins (first_bb);
5249 if (!next && first_bb->next_bb) {
5250 first_bb = first_bb->next_bb;
5251 next = mono_bb_first_ins (first_bb);
5254 if (first_bb->in_count > 1)
5257 for (i = 0; next && i < sig->param_count + sig->hasthis; ++i) {
5258 ArgInfo *ainfo = cinfo->args + i;
5259 gboolean match = FALSE;
5261 ins = cfg->args [i];
5262 if (ins->opcode != OP_REGVAR) {
5263 switch (ainfo->storage) {
5265 if (((next->opcode == OP_LOAD_MEMBASE) || (next->opcode == OP_LOADI4_MEMBASE)) && next->inst_basereg == ins->inst_basereg && next->inst_offset == ins->inst_offset) {
5266 if (next->dreg == ainfo->reg) {
5270 next->opcode = OP_MOVE;
5271 next->sreg1 = ainfo->reg;
5272 /* Only continue if the instruction doesn't change argument regs */
5273 if (next->dreg == ainfo->reg || next->dreg == AMD64_RAX)
5283 /* Argument allocated to (non-volatile) register */
5284 switch (ainfo->storage) {
5286 if (next->opcode == OP_MOVE && next->sreg1 == ins->dreg && next->dreg == ainfo->reg) {
5298 //next = mono_inst_list_next (&next->node, &first_bb->ins_list);
5305 cfg->code_len = code - cfg->native_code;
5307 g_assert (cfg->code_len < cfg->code_size);
5313 mono_arch_emit_epilog (MonoCompile *cfg)
5315 MonoMethod *method = cfg->method;
5318 int max_epilog_size;
5320 gint32 lmf_offset = cfg->arch.lmf_offset;
5322 max_epilog_size = get_max_epilog_size (cfg);
5324 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
5325 cfg->code_size *= 2;
5326 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
5327 mono_jit_stats.code_reallocs++;
5330 code = cfg->native_code + cfg->code_len;
5332 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
5333 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
5335 /* the code restoring the registers must be kept in sync with OP_JMP */
5338 if (method->save_lmf) {
5339 /* check if we need to restore protection of the stack after a stack overflow */
5340 if (mono_get_jit_tls_offset () != -1) {
5342 code = emit_tls_get (code, X86_ECX, mono_get_jit_tls_offset ());
5343 /* we load the value in a separate instruction: this mechanism may be
5344 * used later as a safer way to do thread interruption
5346 amd64_mov_reg_membase (code, X86_ECX, X86_ECX, G_STRUCT_OFFSET (MonoJitTlsData, restore_stack_prot), 8);
5347 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
5349 x86_branch8 (code, X86_CC_Z, 0, FALSE);
5350 /* note that the call trampoline will preserve eax/edx */
5351 x86_call_reg (code, X86_ECX);
5352 x86_patch (patch, code);
5354 /* FIXME: maybe save the jit tls in the prolog */
5356 if ((lmf_tls_offset != -1) && !optimize_for_xen) {
5358 * Optimized version which uses the mono_lmf TLS variable instead of indirection
5359 * through the mono_lmf_addr TLS variable.
5361 /* reg = previous_lmf */
5362 amd64_mov_reg_membase (code, AMD64_R11, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 8);
5363 x86_prefix (code, X86_FS_PREFIX);
5364 amd64_mov_mem_reg (code, lmf_tls_offset, AMD64_R11, 8);
5366 /* Restore previous lmf */
5367 amd64_mov_reg_membase (code, AMD64_RCX, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 8);
5368 amd64_mov_reg_membase (code, AMD64_R11, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), 8);
5369 amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RCX, 8);
5372 /* Restore caller saved regs */
5373 if (cfg->used_int_regs & (1 << AMD64_RBP)) {
5374 amd64_mov_reg_membase (code, AMD64_RBP, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbp), 8);
5376 if (cfg->used_int_regs & (1 << AMD64_RBX)) {
5377 amd64_mov_reg_membase (code, AMD64_RBX, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbx), 8);
5379 if (cfg->used_int_regs & (1 << AMD64_R12)) {
5380 amd64_mov_reg_membase (code, AMD64_R12, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r12), 8);
5382 if (cfg->used_int_regs & (1 << AMD64_R13)) {
5383 amd64_mov_reg_membase (code, AMD64_R13, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r13), 8);
5385 if (cfg->used_int_regs & (1 << AMD64_R14)) {
5386 amd64_mov_reg_membase (code, AMD64_R14, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r14), 8);
5388 if (cfg->used_int_regs & (1 << AMD64_R15)) {
5389 amd64_mov_reg_membase (code, AMD64_R15, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), 8);
5393 if (cfg->arch.omit_fp) {
5394 gint32 save_area_offset = cfg->arch.reg_save_area_offset;
5396 for (i = 0; i < AMD64_NREG; ++i)
5397 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
5398 amd64_mov_reg_membase (code, i, AMD64_RSP, save_area_offset, 8);
5399 save_area_offset += 8;
5403 for (i = 0; i < AMD64_NREG; ++i)
5404 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i)))
5405 pos -= sizeof (gpointer);
5408 if (pos == - sizeof (gpointer)) {
5409 /* Only one register, so avoid lea */
5410 for (i = AMD64_NREG - 1; i > 0; --i)
5411 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
5412 amd64_mov_reg_membase (code, i, AMD64_RBP, pos, 8);
5416 amd64_lea_membase (code, AMD64_RSP, AMD64_RBP, pos);
5418 /* Pop registers in reverse order */
5419 for (i = AMD64_NREG - 1; i > 0; --i)
5420 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
5421 amd64_pop_reg (code, i);
5428 /* Load returned vtypes into registers if needed */
5429 cinfo = cfg->arch.cinfo;
5430 if (cinfo->ret.storage == ArgValuetypeInReg) {
5431 ArgInfo *ainfo = &cinfo->ret;
5432 MonoInst *inst = cfg->ret;
5434 for (quad = 0; quad < 2; quad ++) {
5435 switch (ainfo->pair_storage [quad]) {
5437 amd64_mov_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof (gpointer)), sizeof (gpointer));
5439 case ArgInFloatSSEReg:
5440 amd64_movss_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof (gpointer)));
5442 case ArgInDoubleSSEReg:
5443 amd64_movsd_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof (gpointer)));
5448 g_assert_not_reached ();
5453 if (cfg->arch.omit_fp) {
5454 if (cfg->arch.stack_alloc_size)
5455 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
5459 async_exc_point (code);
5462 cfg->code_len = code - cfg->native_code;
5464 g_assert (cfg->code_len < cfg->code_size);
5466 if (cfg->arch.omit_fp) {
5468 * Encode the stack size into used_int_regs so the exception handler
5471 g_assert (cfg->arch.stack_alloc_size < (1 << 16));
5472 cfg->used_int_regs |= (1 << 31) | (cfg->arch.stack_alloc_size << 16);
5477 mono_arch_emit_exceptions (MonoCompile *cfg)
5479 MonoJumpInfo *patch_info;
5482 MonoClass *exc_classes [16];
5483 guint8 *exc_throw_start [16], *exc_throw_end [16];
5484 guint32 code_size = 0;
5486 /* Compute needed space */
5487 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5488 if (patch_info->type == MONO_PATCH_INFO_EXC)
5490 if (patch_info->type == MONO_PATCH_INFO_R8)
5491 code_size += 8 + 15; /* sizeof (double) + alignment */
5492 if (patch_info->type == MONO_PATCH_INFO_R4)
5493 code_size += 4 + 15; /* sizeof (float) + alignment */
5496 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
5497 cfg->code_size *= 2;
5498 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
5499 mono_jit_stats.code_reallocs++;
5502 code = cfg->native_code + cfg->code_len;
5504 /* add code to raise exceptions */
5506 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5507 switch (patch_info->type) {
5508 case MONO_PATCH_INFO_EXC: {
5509 MonoClass *exc_class;
5513 amd64_patch (patch_info->ip.i + cfg->native_code, code);
5515 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
5516 g_assert (exc_class);
5517 throw_ip = patch_info->ip.i;
5519 //x86_breakpoint (code);
5520 /* Find a throw sequence for the same exception class */
5521 for (i = 0; i < nthrows; ++i)
5522 if (exc_classes [i] == exc_class)
5525 amd64_mov_reg_imm (code, AMD64_ARG_REG2, (exc_throw_end [i] - cfg->native_code) - throw_ip);
5526 x86_jump_code (code, exc_throw_start [i]);
5527 patch_info->type = MONO_PATCH_INFO_NONE;
5531 amd64_mov_reg_imm_size (code, AMD64_ARG_REG2, 0xf0f0f0f0, 4);
5535 exc_classes [nthrows] = exc_class;
5536 exc_throw_start [nthrows] = code;
5538 amd64_mov_reg_imm (code, AMD64_ARG_REG1, exc_class->type_token);
5540 patch_info->type = MONO_PATCH_INFO_NONE;
5542 code = emit_call_body (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_throw_corlib_exception");
5544 amd64_mov_reg_imm (buf, AMD64_ARG_REG2, (code - cfg->native_code) - throw_ip);
5549 exc_throw_end [nthrows] = code;
5561 /* Handle relocations with RIP relative addressing */
5562 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5563 gboolean remove = FALSE;
5565 switch (patch_info->type) {
5566 case MONO_PATCH_INFO_R8:
5567 case MONO_PATCH_INFO_R4: {
5570 /* The SSE opcodes require a 16 byte alignment */
5571 code = (guint8*)ALIGN_TO (code, 16);
5573 pos = cfg->native_code + patch_info->ip.i;
5575 if (IS_REX (pos [1]))
5576 *(guint32*)(pos + 5) = (guint8*)code - pos - 9;
5578 *(guint32*)(pos + 4) = (guint8*)code - pos - 8;
5580 if (patch_info->type == MONO_PATCH_INFO_R8) {
5581 *(double*)code = *(double*)patch_info->data.target;
5582 code += sizeof (double);
5584 *(float*)code = *(float*)patch_info->data.target;
5585 code += sizeof (float);
5596 if (patch_info == cfg->patch_info)
5597 cfg->patch_info = patch_info->next;
5601 for (tmp = cfg->patch_info; tmp->next != patch_info; tmp = tmp->next)
5603 tmp->next = patch_info->next;
5608 cfg->code_len = code - cfg->native_code;
5610 g_assert (cfg->code_len < cfg->code_size);
5615 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
5618 CallInfo *cinfo = NULL;
5619 MonoMethodSignature *sig;
5621 int i, n, stack_area = 0;
5623 /* Keep this in sync with mono_arch_get_argument_info */
5625 if (enable_arguments) {
5626 /* Allocate a new area on the stack and save arguments there */
5627 sig = mono_method_signature (cfg->method);
5629 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, FALSE);
5631 n = sig->param_count + sig->hasthis;
5633 stack_area = ALIGN_TO (n * 8, 16);
5635 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, stack_area);
5637 for (i = 0; i < n; ++i) {
5638 inst = cfg->args [i];
5640 if (inst->opcode == OP_REGVAR)
5641 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), inst->dreg, 8);
5643 amd64_mov_reg_membase (code, AMD64_R11, inst->inst_basereg, inst->inst_offset, 8);
5644 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), AMD64_R11, 8);
5649 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
5650 amd64_set_reg_template (code, AMD64_ARG_REG1);
5651 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RSP, 8);
5652 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
5654 if (enable_arguments)
5655 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, stack_area);
5669 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
5672 int save_mode = SAVE_NONE;
5673 MonoMethod *method = cfg->method;
5674 int rtype = mini_type_get_underlying_type (NULL, mono_method_signature (method)->ret)->type;
5677 case MONO_TYPE_VOID:
5678 /* special case string .ctor icall */
5679 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
5680 save_mode = SAVE_EAX;
5682 save_mode = SAVE_NONE;
5686 save_mode = SAVE_EAX;
5690 save_mode = SAVE_XMM;
5692 case MONO_TYPE_GENERICINST:
5693 if (!mono_type_generic_inst_is_valuetype (mono_method_signature (method)->ret)) {
5694 save_mode = SAVE_EAX;
5698 case MONO_TYPE_VALUETYPE:
5699 save_mode = SAVE_STRUCT;
5702 save_mode = SAVE_EAX;
5706 /* Save the result and copy it into the proper argument register */
5707 switch (save_mode) {
5709 amd64_push_reg (code, AMD64_RAX);
5711 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
5712 if (enable_arguments)
5713 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RAX, 8);
5717 if (enable_arguments)
5718 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
5721 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
5722 amd64_movsd_membase_reg (code, AMD64_RSP, 0, AMD64_XMM0);
5724 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
5726 * The result is already in the proper argument register so no copying
5733 g_assert_not_reached ();
5736 /* Set %al since this is a varargs call */
5737 if (save_mode == SAVE_XMM)
5738 amd64_mov_reg_imm (code, AMD64_RAX, 1);
5740 amd64_mov_reg_imm (code, AMD64_RAX, 0);
5742 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
5743 amd64_set_reg_template (code, AMD64_ARG_REG1);
5744 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
5746 /* Restore result */
5747 switch (save_mode) {
5749 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
5750 amd64_pop_reg (code, AMD64_RAX);
5756 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
5757 amd64_movsd_reg_membase (code, AMD64_XMM0, AMD64_RSP, 0);
5758 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
5763 g_assert_not_reached ();
5770 mono_arch_flush_icache (guint8 *code, gint size)
5776 mono_arch_flush_register_windows (void)
5781 mono_arch_is_inst_imm (gint64 imm)
5783 return amd64_is_imm32 (imm);
5787 * Determine whenever the trap whose info is in SIGINFO is caused by
5791 mono_arch_is_int_overflow (void *sigctx, void *info)
5798 mono_arch_sigctx_to_monoctx (sigctx, &ctx);
5800 rip = (guint8*)ctx.rip;
5802 if (IS_REX (rip [0])) {
5803 reg = amd64_rex_b (rip [0]);
5809 if ((rip [0] == 0xf7) && (x86_modrm_mod (rip [1]) == 0x3) && (x86_modrm_reg (rip [1]) == 0x7)) {
5811 reg += x86_modrm_rm (rip [1]);
5851 g_assert_not_reached ();
5863 mono_arch_get_patch_offset (guint8 *code)
5869 * mono_breakpoint_clean_code:
5871 * Copy @size bytes from @code - @offset to the buffer @buf. If the debugger inserted software
5872 * breakpoints in the original code, they are removed in the copy.
5874 * Returns TRUE if no sw breakpoint was present.
5877 mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size)
5880 gboolean can_write = TRUE;
5882 * If method_start is non-NULL we need to perform bound checks, since we access memory
5883 * at code - offset we could go before the start of the method and end up in a different
5884 * page of memory that is not mapped or read incorrect data anyway. We zero-fill the bytes
5887 if (!method_start || code - offset >= method_start) {
5888 memcpy (buf, code - offset, size);
5890 int diff = code - method_start;
5891 memset (buf, 0, size);
5892 memcpy (buf + offset - diff, method_start, diff + size - offset);
5895 for (i = 0; i < MONO_BREAKPOINT_ARRAY_SIZE; ++i) {
5896 int idx = mono_breakpoint_info_index [i];
5900 ptr = mono_breakpoint_info [idx].address;
5901 if (ptr >= code && ptr < code + size) {
5902 guint8 saved_byte = mono_breakpoint_info [idx].saved_byte;
5904 /*g_print ("patching %p with 0x%02x (was: 0x%02x)\n", ptr, saved_byte, buf [ptr - code]);*/
5905 buf [ptr - code] = saved_byte;
5912 mono_arch_get_vcall_slot (guint8 *code, gpointer *regs, int *displacement)
5919 mono_breakpoint_clean_code (NULL, code, 9, buf, sizeof (buf));
5924 /* go to the start of the call instruction
5926 * address_byte = (m << 6) | (o << 3) | reg
5927 * call opcode: 0xff address_byte displacement
5929 * 0xff m=2,o=2 imm32
5934 * A given byte sequence can match more than case here, so we have to be
5935 * really careful about the ordering of the cases. Longer sequences
5938 #ifdef MONO_ARCH_HAVE_IMT
5939 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)) {
5940 /* IMT-based interface calls: with MONO_ARCH_IMT_REG == r11
5941 * 41 bb 14 f8 28 08 mov $0x828f814,%r11d
5942 * ff 50 fc call *0xfffffffc(%rax)
5944 reg = amd64_modrm_rm (code [5]);
5945 disp = (signed char)code [6];
5946 /* R10 is clobbered by the IMT thunk code */
5947 g_assert (reg != AMD64_R10);
5953 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)) {
5955 * This is a interface call
5956 * 48 8b 80 f0 e8 ff ff mov 0xffffffffffffe8f0(%rax),%rax
5957 * ff 10 callq *(%rax)
5959 if (IS_REX (code [4]))
5961 reg = amd64_modrm_rm (code [6]);
5963 /* R10 is clobbered by the IMT thunk code */
5964 g_assert (reg != AMD64_R10);
5965 } else if ((code [0] == 0x41) && (code [1] == 0xff) && (code [2] == 0x15)) {
5966 /* call OFFSET(%rip) */
5967 disp = *(guint32*)(code + 3);
5968 return (gpointer*)(code + disp + 7);
5969 } 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)) {
5970 /* call *[r12+disp32] */
5971 if (IS_REX (code [-1]))
5974 disp = *(gint32*)(code + 3);
5975 } else if ((code [1] == 0xff) && (amd64_modrm_reg (code [2]) == 0x2) && (amd64_modrm_mod (code [2]) == 0x2)) {
5976 /* call *[reg+disp32] */
5977 if (IS_REX (code [0]))
5979 reg = amd64_modrm_rm (code [2]);
5980 disp = *(gint32*)(code + 3);
5981 /* R10 is clobbered by the IMT thunk code */
5982 g_assert (reg != AMD64_R10);
5983 } else if (code [2] == 0xe8) {
5986 } 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)) {
5987 /* call *[r12+disp32] */
5988 if (IS_REX (code [2]))
5991 disp = *(gint8*)(code + 6);
5992 } else if (IS_REX (code [4]) && (code [5] == 0xff) && (amd64_modrm_reg (code [6]) == 0x2) && (amd64_modrm_mod (code [6]) == 0x3)) {
5995 } else if ((code [4] == 0xff) && (amd64_modrm_reg (code [5]) == 0x2) && (amd64_modrm_mod (code [5]) == 0x1)) {
5996 /* call *[reg+disp8] */
5997 if (IS_REX (code [3]))
5999 reg = amd64_modrm_rm (code [5]);
6000 disp = *(gint8*)(code + 6);
6001 //printf ("B: [%%r%d+0x%x]\n", reg, disp);
6003 else if ((code [5] == 0xff) && (amd64_modrm_reg (code [6]) == 0x2) && (amd64_modrm_mod (code [6]) == 0x0)) {
6005 * This is a interface call: should check the above code can't catch it earlier
6006 * 8b 40 30 mov 0x30(%eax),%eax
6007 * ff 10 call *(%eax)
6009 if (IS_REX (code [4]))
6011 reg = amd64_modrm_rm (code [6]);
6015 g_assert_not_reached ();
6017 reg += amd64_rex_b (rex);
6019 /* R11 is clobbered by the trampoline code */
6020 g_assert (reg != AMD64_R11);
6022 *displacement = disp;
6027 mono_arch_get_vcall_slot_addr (guint8* code, gpointer *regs)
6031 vt = mono_arch_get_vcall_slot (code, regs, &displacement);
6034 return (gpointer*)((char*)vt + displacement);
6038 mono_arch_get_this_arg_reg (MonoMethodSignature *sig, MonoGenericSharingContext *gsctx, guint8 *code)
6040 int this_reg = AMD64_ARG_REG1;
6042 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
6046 gsctx = mono_get_generic_context_from_code (code);
6048 cinfo = get_call_info (gsctx, NULL, sig, FALSE);
6050 if (cinfo->ret.storage != ArgValuetypeInReg)
6051 this_reg = AMD64_ARG_REG2;
6059 mono_arch_get_this_arg_from_call (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, gssize *regs, guint8 *code)
6061 return (gpointer)regs [mono_arch_get_this_arg_reg (sig, gsctx, code)];
6064 #define MAX_ARCH_DELEGATE_PARAMS 10
6067 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
6069 guint8 *code, *start;
6072 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
6075 /* FIXME: Support more cases */
6076 if (MONO_TYPE_ISSTRUCT (sig->ret))
6080 static guint8* cached = NULL;
6085 start = code = mono_global_codeman_reserve (64);
6087 /* Replace the this argument with the target */
6088 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
6089 amd64_mov_reg_membase (code, AMD64_ARG_REG1, AMD64_RAX, G_STRUCT_OFFSET (MonoDelegate, target), 8);
6090 amd64_jump_membase (code, AMD64_RAX, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
6092 g_assert ((code - start) < 64);
6094 mono_debug_add_delegate_trampoline (start, code - start);
6096 mono_memory_barrier ();
6100 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
6101 for (i = 0; i < sig->param_count; ++i)
6102 if (!mono_is_regsize_var (sig->params [i]))
6104 if (sig->param_count > 4)
6107 code = cache [sig->param_count];
6111 start = code = mono_global_codeman_reserve (64);
6113 if (sig->param_count == 0) {
6114 amd64_jump_membase (code, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
6116 /* We have to shift the arguments left */
6117 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
6118 for (i = 0; i < sig->param_count; ++i) {
6119 #ifdef PLATFORM_WIN32
6121 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
6123 amd64_mov_reg_membase (code, param_regs [i], AMD64_RSP, 0x28, 8);
6125 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
6129 amd64_jump_membase (code, AMD64_RAX, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
6131 g_assert ((code - start) < 64);
6133 mono_debug_add_delegate_trampoline (start, code - start);
6135 mono_memory_barrier ();
6137 cache [sig->param_count] = start;
6144 * Support for fast access to the thread-local lmf structure using the GS
6145 * segment register on NPTL + kernel 2.6.x.
6148 static gboolean tls_offset_inited = FALSE;
6151 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
6153 if (!tls_offset_inited) {
6154 #ifdef PLATFORM_WIN32
6156 * We need to init this multiple times, since when we are first called, the key might not
6157 * be initialized yet.
6159 appdomain_tls_offset = mono_domain_get_tls_key ();
6160 lmf_tls_offset = mono_get_jit_tls_key ();
6161 thread_tls_offset = mono_thread_get_tls_key ();
6162 lmf_addr_tls_offset = mono_get_jit_tls_key ();
6164 /* Only 64 tls entries can be accessed using inline code */
6165 if (appdomain_tls_offset >= 64)
6166 appdomain_tls_offset = -1;
6167 if (lmf_tls_offset >= 64)
6168 lmf_tls_offset = -1;
6169 if (thread_tls_offset >= 64)
6170 thread_tls_offset = -1;
6172 tls_offset_inited = TRUE;
6174 optimize_for_xen = access ("/proc/xen", F_OK) == 0;
6176 appdomain_tls_offset = mono_domain_get_tls_offset ();
6177 lmf_tls_offset = mono_get_lmf_tls_offset ();
6178 lmf_addr_tls_offset = mono_get_lmf_addr_tls_offset ();
6179 thread_tls_offset = mono_thread_get_tls_offset ();
6185 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
6190 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
6192 MonoCallInst *call = (MonoCallInst*)inst;
6193 CallInfo * cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, inst->signature, FALSE);
6198 if (cinfo->ret.storage == ArgValuetypeInReg) {
6200 * The valuetype is in RAX:RDX after the call, need to be copied to
6201 * the stack. Save the address here, so the call instruction can
6204 MonoInst *loc = cfg->arch.vret_addr_loc;
6207 g_assert (loc->opcode == OP_REGOFFSET);
6209 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, loc->inst_basereg, loc->inst_offset, vt_reg);
6211 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
6212 vtarg->sreg1 = vt_reg;
6213 vtarg->dreg = mono_regstate_next_int (cfg->rs);
6214 mono_bblock_add_inst (cfg->cbb, vtarg);
6216 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
6220 /* add the this argument */
6221 if (this_reg != -1) {
6223 MONO_INST_NEW (cfg, this, OP_MOVE);
6224 this->type = this_type;
6225 this->sreg1 = this_reg;
6226 this->dreg = mono_regstate_next_int (cfg->rs);
6227 mono_bblock_add_inst (cfg->cbb, this);
6229 mono_call_inst_add_outarg_reg (cfg, call, this->dreg, cinfo->args [0].reg, FALSE);
6233 #ifdef MONO_ARCH_HAVE_IMT
6235 #define CMP_SIZE (6 + 1)
6236 #define CMP_REG_REG_SIZE (4 + 1)
6237 #define BR_SMALL_SIZE 2
6238 #define BR_LARGE_SIZE 6
6239 #define MOV_REG_IMM_SIZE 10
6240 #define MOV_REG_IMM_32BIT_SIZE 6
6241 #define JUMP_REG_SIZE (2 + 1)
6244 imt_branch_distance (MonoIMTCheckItem **imt_entries, int start, int target)
6246 int i, distance = 0;
6247 for (i = start; i < target; ++i)
6248 distance += imt_entries [i]->chunk_size;
6253 * LOCKING: called with the domain lock held
6256 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
6257 gpointer fail_tramp)
6261 guint8 *code, *start;
6262 gboolean vtable_is_32bit = ((gsize)(vtable) == (gsize)(int)(gsize)(vtable));
6264 for (i = 0; i < count; ++i) {
6265 MonoIMTCheckItem *item = imt_entries [i];
6266 if (item->is_equals) {
6267 if (item->check_target_idx) {
6268 if (!item->compare_done) {
6269 if (amd64_is_imm32 (item->key))
6270 item->chunk_size += CMP_SIZE;
6272 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
6274 if (vtable_is_32bit)
6275 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
6277 item->chunk_size += MOV_REG_IMM_SIZE;
6278 item->chunk_size += BR_SMALL_SIZE + JUMP_REG_SIZE;
6281 item->chunk_size += MOV_REG_IMM_SIZE * 3 + CMP_REG_REG_SIZE +
6282 BR_SMALL_SIZE + JUMP_REG_SIZE * 2;
6284 if (vtable_is_32bit)
6285 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
6287 item->chunk_size += MOV_REG_IMM_SIZE;
6288 item->chunk_size += JUMP_REG_SIZE;
6289 /* with assert below:
6290 * item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
6295 if (amd64_is_imm32 (item->key))
6296 item->chunk_size += CMP_SIZE;
6298 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
6299 item->chunk_size += BR_LARGE_SIZE;
6300 imt_entries [item->check_target_idx]->compare_done = TRUE;
6302 size += item->chunk_size;
6305 code = mono_method_alloc_generic_virtual_thunk (domain, size);
6307 code = mono_code_manager_reserve (domain->code_mp, size);
6309 for (i = 0; i < count; ++i) {
6310 MonoIMTCheckItem *item = imt_entries [i];
6311 item->code_target = code;
6312 if (item->is_equals) {
6313 if (item->check_target_idx) {
6314 if (!item->compare_done) {
6315 if (amd64_is_imm32 (item->key))
6316 amd64_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key);
6318 amd64_mov_reg_imm (code, AMD64_R10, item->key);
6319 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, AMD64_R10);
6322 item->jmp_code = code;
6323 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
6324 /* See the comment below about R10 */
6326 amd64_mov_reg_imm (code, AMD64_R10, item->value.target_code);
6327 amd64_jump_reg (code, AMD64_R10);
6329 amd64_mov_reg_imm (code, AMD64_R10, & (vtable->vtable [item->value.vtable_slot]));
6330 amd64_jump_membase (code, AMD64_R10, 0);
6334 if (amd64_is_imm32 (item->key))
6335 amd64_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key);
6337 amd64_mov_reg_imm (code, AMD64_R10, item->key);
6338 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, AMD64_R10);
6340 item->jmp_code = code;
6341 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
6342 amd64_mov_reg_imm (code, AMD64_R10, item->value.target_code);
6343 amd64_jump_reg (code, AMD64_R10);
6344 amd64_patch (item->jmp_code, code);
6345 amd64_mov_reg_imm (code, AMD64_R10, fail_tramp);
6346 amd64_jump_reg (code, AMD64_R10);
6347 item->jmp_code = NULL;
6350 /* enable the commented code to assert on wrong method */
6352 if (amd64_is_imm32 (item->key))
6353 amd64_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key);
6355 amd64_mov_reg_imm (code, AMD64_R10, item->key);
6356 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, AMD64_R10);
6358 item->jmp_code = code;
6359 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
6360 /* See the comment below about R10 */
6361 amd64_mov_reg_imm (code, AMD64_R10, & (vtable->vtable [item->value.vtable_slot]));
6362 amd64_jump_membase (code, AMD64_R10, 0);
6363 amd64_patch (item->jmp_code, code);
6364 amd64_breakpoint (code);
6365 item->jmp_code = NULL;
6367 /* We're using R10 here because R11
6368 needs to be preserved. R10 needs
6369 to be preserved for calls which
6370 require a runtime generic context,
6371 but interface calls don't. */
6372 amd64_mov_reg_imm (code, AMD64_R10, & (vtable->vtable [item->value.vtable_slot]));
6373 amd64_jump_membase (code, AMD64_R10, 0);
6378 if (amd64_is_imm32 (item->key))
6379 amd64_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key);
6381 amd64_mov_reg_imm (code, AMD64_R10, item->key);
6382 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, AMD64_R10);
6384 item->jmp_code = code;
6385 if (x86_is_imm8 (imt_branch_distance (imt_entries, i, item->check_target_idx)))
6386 x86_branch8 (code, X86_CC_GE, 0, FALSE);
6388 x86_branch32 (code, X86_CC_GE, 0, FALSE);
6390 g_assert (code - item->code_target <= item->chunk_size);
6392 /* patch the branches to get to the target items */
6393 for (i = 0; i < count; ++i) {
6394 MonoIMTCheckItem *item = imt_entries [i];
6395 if (item->jmp_code) {
6396 if (item->check_target_idx) {
6397 amd64_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
6403 mono_stats.imt_thunks_size += code - start;
6404 g_assert (code - start <= size);
6410 mono_arch_find_imt_method (gpointer *regs, guint8 *code)
6412 return regs [MONO_ARCH_IMT_REG];
6416 mono_arch_find_this_argument (gpointer *regs, MonoMethod *method, MonoGenericSharingContext *gsctx)
6418 return mono_arch_get_this_arg_from_call (gsctx, mono_method_signature (method), (gssize*)regs, NULL);
6422 mono_arch_emit_imt_argument (MonoCompile *cfg, MonoCallInst *call, MonoInst *imt_arg)
6424 /* Done by the implementation of the CALL_MEMBASE opcodes */
6429 mono_arch_find_static_call_vtable (gpointer *regs, guint8 *code)
6431 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
6435 mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
6437 MonoInst *ins = NULL;
6439 if (cmethod->klass == mono_defaults.math_class) {
6440 if (strcmp (cmethod->name, "Sin") == 0) {
6441 MONO_INST_NEW (cfg, ins, OP_SIN);
6442 ins->inst_i0 = args [0];
6443 } else if (strcmp (cmethod->name, "Cos") == 0) {
6444 MONO_INST_NEW (cfg, ins, OP_COS);
6445 ins->inst_i0 = args [0];
6446 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
6447 MONO_INST_NEW (cfg, ins, OP_SQRT);
6448 ins->inst_i0 = args [0];
6449 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
6450 MONO_INST_NEW (cfg, ins, OP_ABS);
6451 ins->inst_i0 = args [0];
6454 if (cfg->opt & MONO_OPT_CMOV) {
6457 if (strcmp (cmethod->name, "Min") == 0) {
6458 if (fsig->params [0]->type == MONO_TYPE_I4)
6460 if (fsig->params [0]->type == MONO_TYPE_U4)
6461 opcode = OP_IMIN_UN;
6462 else if (fsig->params [0]->type == MONO_TYPE_I8)
6464 else if (fsig->params [0]->type == MONO_TYPE_U8)
6465 opcode = OP_LMIN_UN;
6466 } else if (strcmp (cmethod->name, "Max") == 0) {
6467 if (fsig->params [0]->type == MONO_TYPE_I4)
6469 if (fsig->params [0]->type == MONO_TYPE_U4)
6470 opcode = OP_IMAX_UN;
6471 else if (fsig->params [0]->type == MONO_TYPE_I8)
6473 else if (fsig->params [0]->type == MONO_TYPE_U8)
6474 opcode = OP_LMAX_UN;
6478 MONO_INST_NEW (cfg, ins, opcode);
6479 ins->inst_i0 = args [0];
6480 ins->inst_i1 = args [1];
6485 /* OP_FREM is not IEEE compatible */
6486 else if (strcmp (cmethod->name, "IEEERemainder") == 0) {
6487 MONO_INST_NEW (cfg, ins, OP_FREM);
6488 ins->inst_i0 = args [0];
6489 ins->inst_i1 = args [1];
6498 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
6500 MonoInst *ins = NULL;
6503 if (cmethod->klass == mono_defaults.math_class) {
6504 if (strcmp (cmethod->name, "Sin") == 0) {
6506 } else if (strcmp (cmethod->name, "Cos") == 0) {
6508 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
6510 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
6515 MONO_INST_NEW (cfg, ins, opcode);
6516 ins->type = STACK_R8;
6517 ins->dreg = mono_alloc_freg (cfg);
6518 ins->sreg1 = args [0]->dreg;
6519 MONO_ADD_INS (cfg->cbb, ins);
6523 if (cfg->opt & MONO_OPT_CMOV) {
6524 if (strcmp (cmethod->name, "Min") == 0) {
6525 if (fsig->params [0]->type == MONO_TYPE_I4)
6527 if (fsig->params [0]->type == MONO_TYPE_U4)
6528 opcode = OP_IMIN_UN;
6529 else if (fsig->params [0]->type == MONO_TYPE_I8)
6531 else if (fsig->params [0]->type == MONO_TYPE_U8)
6532 opcode = OP_LMIN_UN;
6533 } else if (strcmp (cmethod->name, "Max") == 0) {
6534 if (fsig->params [0]->type == MONO_TYPE_I4)
6536 if (fsig->params [0]->type == MONO_TYPE_U4)
6537 opcode = OP_IMAX_UN;
6538 else if (fsig->params [0]->type == MONO_TYPE_I8)
6540 else if (fsig->params [0]->type == MONO_TYPE_U8)
6541 opcode = OP_LMAX_UN;
6546 MONO_INST_NEW (cfg, ins, opcode);
6547 ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
6548 ins->dreg = mono_alloc_ireg (cfg);
6549 ins->sreg1 = args [0]->dreg;
6550 ins->sreg2 = args [1]->dreg;
6551 MONO_ADD_INS (cfg->cbb, ins);
6555 /* OP_FREM is not IEEE compatible */
6556 else if (strcmp (cmethod->name, "IEEERemainder") == 0) {
6557 MONO_INST_NEW (cfg, ins, OP_FREM);
6558 ins->inst_i0 = args [0];
6559 ins->inst_i1 = args [1];
6565 * Can't implement CompareExchange methods this way since they have
6573 mono_arch_print_tree (MonoInst *tree, int arity)
6578 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
6582 if (appdomain_tls_offset == -1)
6585 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
6586 ins->inst_offset = appdomain_tls_offset;
6590 MonoInst* mono_arch_get_thread_intrinsic (MonoCompile* cfg)
6594 if (thread_tls_offset == -1)
6597 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
6598 ins->inst_offset = thread_tls_offset;
6602 #define _CTX_REG(ctx,fld,i) ((gpointer)((&ctx->fld)[i]))
6605 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
6608 case AMD64_RCX: return (gpointer)ctx->rcx;
6609 case AMD64_RDX: return (gpointer)ctx->rdx;
6610 case AMD64_RBX: return (gpointer)ctx->rbx;
6611 case AMD64_RBP: return (gpointer)ctx->rbp;
6612 case AMD64_RSP: return (gpointer)ctx->rsp;
6615 return _CTX_REG (ctx, rax, reg);
6617 return _CTX_REG (ctx, r12, reg - 12);
6619 g_assert_not_reached ();