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) {
2368 #ifdef MONO_ARCH_NOMAP32BIT
2374 * Align the call displacement to an address divisible by 4 so it does
2375 * not span cache lines. This is required for code patching to work on SMP
2378 if (!no_patch && ((guint32)(code + 1 - cfg->native_code) % 4) != 0)
2379 amd64_padding (code, 4 - ((guint32)(code + 1 - cfg->native_code) % 4));
2380 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
2381 amd64_call_code (code, 0);
2384 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
2385 amd64_set_reg_template (code, GP_SCRATCH_REG);
2386 amd64_call_reg (code, GP_SCRATCH_REG);
2393 static inline guint8*
2394 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data, gboolean win64_adjust_stack)
2396 #ifdef PLATFORM_WIN32
2397 if (win64_adjust_stack)
2398 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 32);
2400 code = emit_call_body (cfg, code, patch_type, data);
2401 #ifdef PLATFORM_WIN32
2402 if (win64_adjust_stack)
2403 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 32);
2410 store_membase_imm_to_store_membase_reg (int opcode)
2413 case OP_STORE_MEMBASE_IMM:
2414 return OP_STORE_MEMBASE_REG;
2415 case OP_STOREI4_MEMBASE_IMM:
2416 return OP_STOREI4_MEMBASE_REG;
2417 case OP_STOREI8_MEMBASE_IMM:
2418 return OP_STOREI8_MEMBASE_REG;
2424 #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)))
2427 * mono_arch_peephole_pass_1:
2429 * Perform peephole opts which should/can be performed before local regalloc
2432 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
2436 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
2437 MonoInst *last_ins = ins->prev;
2439 switch (ins->opcode) {
2443 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS) && (ins->inst_imm > 0)) {
2445 * X86_LEA is like ADD, but doesn't have the
2446 * sreg1==dreg restriction. inst_imm > 0 is needed since LEA sign-extends
2447 * its operand to 64 bit.
2449 ins->opcode = OP_X86_LEA_MEMBASE;
2450 ins->inst_basereg = ins->sreg1;
2455 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
2459 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
2460 * the latter has length 2-3 instead of 6 (reverse constant
2461 * propagation). These instruction sequences are very common
2462 * in the initlocals bblock.
2464 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
2465 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)) {
2466 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
2467 ins2->sreg1 = ins->dreg;
2468 } 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)) {
2470 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
2479 case OP_COMPARE_IMM:
2480 case OP_LCOMPARE_IMM:
2481 /* OP_COMPARE_IMM (reg, 0)
2483 * OP_AMD64_TEST_NULL (reg)
2486 ins->opcode = OP_AMD64_TEST_NULL;
2488 case OP_ICOMPARE_IMM:
2490 ins->opcode = OP_X86_TEST_NULL;
2492 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
2494 * OP_STORE_MEMBASE_REG reg, offset(basereg)
2495 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
2497 * OP_STORE_MEMBASE_REG reg, offset(basereg)
2498 * OP_COMPARE_IMM reg, imm
2500 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
2502 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
2503 ins->inst_basereg == last_ins->inst_destbasereg &&
2504 ins->inst_offset == last_ins->inst_offset) {
2505 ins->opcode = OP_ICOMPARE_IMM;
2506 ins->sreg1 = last_ins->sreg1;
2508 /* check if we can remove cmp reg,0 with test null */
2510 ins->opcode = OP_X86_TEST_NULL;
2516 mono_peephole_ins (bb, ins);
2521 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
2525 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
2526 switch (ins->opcode) {
2529 /* reg = 0 -> XOR (reg, reg) */
2530 /* XOR sets cflags on x86, so we cant do it always */
2531 if (ins->inst_c0 == 0 && (!ins->next || (ins->next && INST_IGNORES_CFLAGS (ins->next->opcode)))) {
2532 ins->opcode = OP_LXOR;
2533 ins->sreg1 = ins->dreg;
2534 ins->sreg2 = ins->dreg;
2542 * Use IXOR to avoid a rex prefix if possible. The cpu will sign extend the
2543 * 0 result into 64 bits.
2545 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
2546 ins->opcode = OP_IXOR;
2550 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
2554 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
2555 * the latter has length 2-3 instead of 6 (reverse constant
2556 * propagation). These instruction sequences are very common
2557 * in the initlocals bblock.
2559 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
2560 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)) {
2561 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
2562 ins2->sreg1 = ins->dreg;
2563 } 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)) {
2565 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
2575 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
2576 ins->opcode = OP_X86_INC_REG;
2579 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
2580 ins->opcode = OP_X86_DEC_REG;
2584 mono_peephole_ins (bb, ins);
2588 #define NEW_INS(cfg,ins,dest,op) do { \
2589 MONO_INST_NEW ((cfg), (dest), (op)); \
2590 (dest)->cil_code = (ins)->cil_code; \
2591 mono_bblock_insert_before_ins (bb, ins, (dest)); \
2595 * mono_arch_lowering_pass:
2597 * Converts complex opcodes into simpler ones so that each IR instruction
2598 * corresponds to one machine instruction.
2601 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
2603 MonoInst *ins, *n, *temp;
2605 if (bb->max_vreg > cfg->rs->next_vreg)
2606 cfg->rs->next_vreg = bb->max_vreg;
2609 * FIXME: Need to add more instructions, but the current machine
2610 * description can't model some parts of the composite instructions like
2613 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
2614 switch (ins->opcode) {
2618 case OP_IDIV_UN_IMM:
2619 case OP_IREM_UN_IMM:
2620 mono_decompose_op_imm (cfg, bb, ins);
2623 /* Keep the opcode if we can implement it efficiently */
2624 if (!((ins->inst_imm > 0) && (mono_is_power_of_two (ins->inst_imm) != -1)))
2625 mono_decompose_op_imm (cfg, bb, ins);
2627 case OP_COMPARE_IMM:
2628 case OP_LCOMPARE_IMM:
2629 if (!amd64_is_imm32 (ins->inst_imm)) {
2630 NEW_INS (cfg, ins, temp, OP_I8CONST);
2631 temp->inst_c0 = ins->inst_imm;
2633 temp->dreg = mono_alloc_ireg (cfg);
2635 temp->dreg = mono_regstate_next_int (cfg->rs);
2636 ins->opcode = OP_COMPARE;
2637 ins->sreg2 = temp->dreg;
2640 case OP_LOAD_MEMBASE:
2641 case OP_LOADI8_MEMBASE:
2642 if (!amd64_is_imm32 (ins->inst_offset)) {
2643 NEW_INS (cfg, ins, temp, OP_I8CONST);
2644 temp->inst_c0 = ins->inst_offset;
2646 temp->dreg = mono_alloc_ireg (cfg);
2648 temp->dreg = mono_regstate_next_int (cfg->rs);
2649 ins->opcode = OP_AMD64_LOADI8_MEMINDEX;
2650 ins->inst_indexreg = temp->dreg;
2653 case OP_STORE_MEMBASE_IMM:
2654 case OP_STOREI8_MEMBASE_IMM:
2655 if (!amd64_is_imm32 (ins->inst_imm)) {
2656 NEW_INS (cfg, ins, temp, OP_I8CONST);
2657 temp->inst_c0 = ins->inst_imm;
2659 temp->dreg = mono_alloc_ireg (cfg);
2661 temp->dreg = mono_regstate_next_int (cfg->rs);
2662 ins->opcode = OP_STOREI8_MEMBASE_REG;
2663 ins->sreg1 = temp->dreg;
2671 bb->max_vreg = cfg->rs->next_vreg;
2675 branch_cc_table [] = {
2676 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
2677 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
2678 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
2681 /* Maps CMP_... constants to X86_CC_... constants */
2684 X86_CC_EQ, X86_CC_NE, X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT,
2685 X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT
2689 cc_signed_table [] = {
2690 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
2691 FALSE, FALSE, FALSE, FALSE
2694 /*#include "cprop.c"*/
2696 static unsigned char*
2697 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
2699 amd64_sse_cvttsd2si_reg_reg (code, dreg, sreg);
2702 amd64_widen_reg (code, dreg, dreg, is_signed, FALSE);
2704 amd64_widen_reg (code, dreg, dreg, is_signed, TRUE);
2708 static unsigned char*
2709 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
2711 int sreg = tree->sreg1;
2712 int need_touch = FALSE;
2714 #if defined(PLATFORM_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
2715 if (!tree->flags & MONO_INST_INIT)
2724 * If requested stack size is larger than one page,
2725 * perform stack-touch operation
2728 * Generate stack probe code.
2729 * Under Windows, it is necessary to allocate one page at a time,
2730 * "touching" stack after each successful sub-allocation. This is
2731 * because of the way stack growth is implemented - there is a
2732 * guard page before the lowest stack page that is currently commited.
2733 * Stack normally grows sequentially so OS traps access to the
2734 * guard page and commits more pages when needed.
2736 amd64_test_reg_imm (code, sreg, ~0xFFF);
2737 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2739 br[2] = code; /* loop */
2740 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
2741 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
2742 amd64_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
2743 amd64_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
2744 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
2745 amd64_patch (br[3], br[2]);
2746 amd64_test_reg_reg (code, sreg, sreg);
2747 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2748 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
2750 br[1] = code; x86_jump8 (code, 0);
2752 amd64_patch (br[0], code);
2753 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
2754 amd64_patch (br[1], code);
2755 amd64_patch (br[4], code);
2758 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, tree->sreg1);
2760 if (tree->flags & MONO_INST_INIT) {
2762 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX) {
2763 amd64_push_reg (code, AMD64_RAX);
2766 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX) {
2767 amd64_push_reg (code, AMD64_RCX);
2770 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI) {
2771 amd64_push_reg (code, AMD64_RDI);
2775 amd64_shift_reg_imm (code, X86_SHR, sreg, 3);
2776 if (sreg != AMD64_RCX)
2777 amd64_mov_reg_reg (code, AMD64_RCX, sreg, 8);
2778 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
2780 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, offset);
2782 amd64_prefix (code, X86_REP_PREFIX);
2785 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI)
2786 amd64_pop_reg (code, AMD64_RDI);
2787 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX)
2788 amd64_pop_reg (code, AMD64_RCX);
2789 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX)
2790 amd64_pop_reg (code, AMD64_RAX);
2796 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
2801 /* Move return value to the target register */
2802 /* FIXME: do this in the local reg allocator */
2803 switch (ins->opcode) {
2806 case OP_CALL_MEMBASE:
2809 case OP_LCALL_MEMBASE:
2810 g_assert (ins->dreg == AMD64_RAX);
2814 case OP_FCALL_MEMBASE:
2815 if (((MonoCallInst*)ins)->signature->ret->type == MONO_TYPE_R4) {
2816 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, AMD64_XMM0);
2819 if (ins->dreg != AMD64_XMM0)
2820 amd64_sse_movsd_reg_reg (code, ins->dreg, AMD64_XMM0);
2825 case OP_VCALL_MEMBASE:
2828 case OP_VCALL2_MEMBASE:
2829 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, ((MonoCallInst*)ins)->signature, FALSE);
2830 if (cinfo->ret.storage == ArgValuetypeInReg) {
2831 MonoInst *loc = cfg->arch.vret_addr_loc;
2833 /* Load the destination address */
2834 g_assert (loc->opcode == OP_REGOFFSET);
2835 amd64_mov_reg_membase (code, AMD64_RCX, loc->inst_basereg, loc->inst_offset, 8);
2837 for (quad = 0; quad < 2; quad ++) {
2838 switch (cinfo->ret.pair_storage [quad]) {
2840 amd64_mov_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad], 8);
2842 case ArgInFloatSSEReg:
2843 amd64_movss_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
2845 case ArgInDoubleSSEReg:
2846 amd64_movsd_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
2862 * mono_amd64_emit_tls_get:
2863 * @code: buffer to store code to
2864 * @dreg: hard register where to place the result
2865 * @tls_offset: offset info
2867 * mono_amd64_emit_tls_get emits in @code the native code that puts in
2868 * the dreg register the item in the thread local storage identified
2871 * Returns: a pointer to the end of the stored code
2874 mono_amd64_emit_tls_get (guint8* code, int dreg, int tls_offset)
2876 #ifdef PLATFORM_WIN32
2877 g_assert (tls_offset < 64);
2878 x86_prefix (code, X86_GS_PREFIX);
2879 amd64_mov_reg_mem (code, dreg, (tls_offset * 8) + 0x1480, 8);
2881 if (optimize_for_xen) {
2882 x86_prefix (code, X86_FS_PREFIX);
2883 amd64_mov_reg_mem (code, dreg, 0, 8);
2884 amd64_mov_reg_membase (code, dreg, dreg, tls_offset, 8);
2886 x86_prefix (code, X86_FS_PREFIX);
2887 amd64_mov_reg_mem (code, dreg, tls_offset, 8);
2894 * emit_load_volatile_arguments:
2896 * Load volatile arguments from the stack to the original input registers.
2897 * Required before a tail call.
2900 emit_load_volatile_arguments (MonoCompile *cfg, guint8 *code)
2902 MonoMethod *method = cfg->method;
2903 MonoMethodSignature *sig;
2908 /* FIXME: Generate intermediate code instead */
2910 sig = mono_method_signature (method);
2912 cinfo = cfg->arch.cinfo;
2914 /* This is the opposite of the code in emit_prolog */
2915 if (sig->ret->type != MONO_TYPE_VOID) {
2916 if (cfg->vret_addr && (cfg->vret_addr->opcode != OP_REGVAR))
2917 amd64_mov_reg_membase (code, cinfo->ret.reg, cfg->vret_addr->inst_basereg, cfg->vret_addr->inst_offset, 8);
2920 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2921 ArgInfo *ainfo = cinfo->args + i;
2923 ins = cfg->args [i];
2925 if (sig->hasthis && (i == 0))
2926 arg_type = &mono_defaults.object_class->byval_arg;
2928 arg_type = sig->params [i - sig->hasthis];
2930 if (ins->opcode != OP_REGVAR) {
2931 switch (ainfo->storage) {
2936 amd64_mov_reg_membase (code, ainfo->reg, ins->inst_basereg, ins->inst_offset, size);
2939 case ArgInFloatSSEReg:
2940 amd64_movss_reg_membase (code, ainfo->reg, ins->inst_basereg, ins->inst_offset);
2942 case ArgInDoubleSSEReg:
2943 amd64_movsd_reg_membase (code, ainfo->reg, ins->inst_basereg, ins->inst_offset);
2945 case ArgValuetypeInReg:
2946 for (quad = 0; quad < 2; quad ++) {
2947 switch (ainfo->pair_storage [quad]) {
2949 amd64_mov_reg_membase (code, ainfo->pair_regs [quad], ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), sizeof (gpointer));
2951 case ArgInFloatSSEReg:
2952 case ArgInDoubleSSEReg:
2953 g_assert_not_reached ();
2958 g_assert_not_reached ();
2962 case ArgValuetypeAddrInIReg:
2963 if (ainfo->pair_storage [0] == ArgInIReg)
2964 amd64_mov_reg_membase (code, ainfo->pair_regs [0], ins->inst_left->inst_basereg, ins->inst_left->inst_offset, sizeof (gpointer));
2971 g_assert (ainfo->storage == ArgInIReg);
2973 amd64_mov_reg_reg (code, ainfo->reg, ins->dreg, 8);
2980 #define REAL_PRINT_REG(text,reg) \
2981 mono_assert (reg >= 0); \
2982 amd64_push_reg (code, AMD64_RAX); \
2983 amd64_push_reg (code, AMD64_RDX); \
2984 amd64_push_reg (code, AMD64_RCX); \
2985 amd64_push_reg (code, reg); \
2986 amd64_push_imm (code, reg); \
2987 amd64_push_imm (code, text " %d %p\n"); \
2988 amd64_mov_reg_imm (code, AMD64_RAX, printf); \
2989 amd64_call_reg (code, AMD64_RAX); \
2990 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 3*4); \
2991 amd64_pop_reg (code, AMD64_RCX); \
2992 amd64_pop_reg (code, AMD64_RDX); \
2993 amd64_pop_reg (code, AMD64_RAX);
2995 /* benchmark and set based on cpu */
2996 #define LOOP_ALIGNMENT 8
2997 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
3002 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
3007 guint8 *code = cfg->native_code + cfg->code_len;
3008 MonoInst *last_ins = NULL;
3009 guint last_offset = 0;
3012 if (cfg->opt & MONO_OPT_LOOP) {
3013 int pad, align = LOOP_ALIGNMENT;
3014 /* set alignment depending on cpu */
3015 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
3017 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
3018 amd64_padding (code, pad);
3019 cfg->code_len += pad;
3020 bb->native_offset = cfg->code_len;
3024 if (cfg->verbose_level > 2)
3025 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
3027 cpos = bb->max_offset;
3029 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
3030 MonoProfileCoverageInfo *cov = cfg->coverage_info;
3031 g_assert (!cfg->compile_aot);
3034 cov->data [bb->dfn].cil_code = bb->cil_code;
3035 amd64_mov_reg_imm (code, AMD64_R11, (guint64)&cov->data [bb->dfn].count);
3036 /* this is not thread save, but good enough */
3037 amd64_inc_membase (code, AMD64_R11, 0);
3040 offset = code - cfg->native_code;
3042 mono_debug_open_block (cfg, bb, offset);
3044 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)
3045 x86_breakpoint (code);
3047 MONO_BB_FOR_EACH_INS (bb, ins) {
3048 offset = code - cfg->native_code;
3050 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
3052 if (G_UNLIKELY (offset > (cfg->code_size - max_len - 16))) {
3053 cfg->code_size *= 2;
3054 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3055 code = cfg->native_code + offset;
3056 mono_jit_stats.code_reallocs++;
3059 if (cfg->debug_info)
3060 mono_debug_record_line_number (cfg, ins, offset);
3062 switch (ins->opcode) {
3064 amd64_mul_reg (code, ins->sreg2, TRUE);
3067 amd64_mul_reg (code, ins->sreg2, FALSE);
3069 case OP_X86_SETEQ_MEMBASE:
3070 amd64_set_membase (code, X86_CC_EQ, ins->inst_basereg, ins->inst_offset, TRUE);
3072 case OP_STOREI1_MEMBASE_IMM:
3073 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
3075 case OP_STOREI2_MEMBASE_IMM:
3076 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
3078 case OP_STOREI4_MEMBASE_IMM:
3079 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
3081 case OP_STOREI1_MEMBASE_REG:
3082 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
3084 case OP_STOREI2_MEMBASE_REG:
3085 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
3087 case OP_STORE_MEMBASE_REG:
3088 case OP_STOREI8_MEMBASE_REG:
3089 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 8);
3091 case OP_STOREI4_MEMBASE_REG:
3092 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
3094 case OP_STORE_MEMBASE_IMM:
3095 case OP_STOREI8_MEMBASE_IMM:
3096 g_assert (amd64_is_imm32 (ins->inst_imm));
3097 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 8);
3101 // FIXME: Decompose this earlier
3102 if (amd64_is_imm32 (ins->inst_imm))
3103 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, sizeof (gpointer));
3105 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3106 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 8);
3110 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3111 amd64_movsxd_reg_membase (code, ins->dreg, ins->dreg, 0);
3114 // FIXME: Decompose this earlier
3116 if (amd64_is_imm32 (ins->inst_imm))
3117 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
3119 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3120 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
3123 amd64_mov_reg_imm (code, ins->dreg, ins->inst_p0);
3124 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
3128 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3129 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, FALSE);
3132 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3133 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, TRUE);
3135 case OP_LOAD_MEMBASE:
3136 case OP_LOADI8_MEMBASE:
3137 g_assert (amd64_is_imm32 (ins->inst_offset));
3138 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, sizeof (gpointer));
3140 case OP_LOADI4_MEMBASE:
3141 amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3143 case OP_LOADU4_MEMBASE:
3144 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
3146 case OP_LOADU1_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, FALSE, 4);
3150 case OP_LOADI1_MEMBASE:
3151 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
3153 case OP_LOADU2_MEMBASE:
3154 /* The cpu zero extends the result into 64 bits */
3155 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE, 4);
3157 case OP_LOADI2_MEMBASE:
3158 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
3160 case OP_AMD64_LOADI8_MEMINDEX:
3161 amd64_mov_reg_memindex_size (code, ins->dreg, ins->inst_basereg, 0, ins->inst_indexreg, 0, 8);
3163 case OP_LCONV_TO_I1:
3164 case OP_ICONV_TO_I1:
3166 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
3168 case OP_LCONV_TO_I2:
3169 case OP_ICONV_TO_I2:
3171 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
3173 case OP_LCONV_TO_U1:
3174 case OP_ICONV_TO_U1:
3175 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
3177 case OP_LCONV_TO_U2:
3178 case OP_ICONV_TO_U2:
3179 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
3182 /* Clean out the upper word */
3183 amd64_mov_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
3186 amd64_movsxd_reg_reg (code, ins->dreg, ins->sreg1);
3190 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3192 case OP_COMPARE_IMM:
3193 case OP_LCOMPARE_IMM:
3194 g_assert (amd64_is_imm32 (ins->inst_imm));
3195 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
3197 case OP_X86_COMPARE_REG_MEMBASE:
3198 amd64_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
3200 case OP_X86_TEST_NULL:
3201 amd64_test_reg_reg_size (code, ins->sreg1, ins->sreg1, 4);
3203 case OP_AMD64_TEST_NULL:
3204 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
3207 case OP_X86_ADD_REG_MEMBASE:
3208 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3210 case OP_X86_SUB_REG_MEMBASE:
3211 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3213 case OP_X86_AND_REG_MEMBASE:
3214 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3216 case OP_X86_OR_REG_MEMBASE:
3217 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3219 case OP_X86_XOR_REG_MEMBASE:
3220 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3223 case OP_X86_ADD_MEMBASE_IMM:
3224 /* FIXME: Make a 64 version too */
3225 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3227 case OP_X86_SUB_MEMBASE_IMM:
3228 g_assert (amd64_is_imm32 (ins->inst_imm));
3229 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3231 case OP_X86_AND_MEMBASE_IMM:
3232 g_assert (amd64_is_imm32 (ins->inst_imm));
3233 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3235 case OP_X86_OR_MEMBASE_IMM:
3236 g_assert (amd64_is_imm32 (ins->inst_imm));
3237 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3239 case OP_X86_XOR_MEMBASE_IMM:
3240 g_assert (amd64_is_imm32 (ins->inst_imm));
3241 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3243 case OP_X86_ADD_MEMBASE_REG:
3244 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
3246 case OP_X86_SUB_MEMBASE_REG:
3247 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
3249 case OP_X86_AND_MEMBASE_REG:
3250 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
3252 case OP_X86_OR_MEMBASE_REG:
3253 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
3255 case OP_X86_XOR_MEMBASE_REG:
3256 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
3258 case OP_X86_INC_MEMBASE:
3259 amd64_inc_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
3261 case OP_X86_INC_REG:
3262 amd64_inc_reg_size (code, ins->dreg, 4);
3264 case OP_X86_DEC_MEMBASE:
3265 amd64_dec_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
3267 case OP_X86_DEC_REG:
3268 amd64_dec_reg_size (code, ins->dreg, 4);
3270 case OP_X86_MUL_REG_MEMBASE:
3271 case OP_X86_MUL_MEMBASE_REG:
3272 amd64_imul_reg_membase_size (code, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3274 case OP_AMD64_ICOMPARE_MEMBASE_REG:
3275 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
3277 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
3278 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3280 case OP_AMD64_COMPARE_MEMBASE_REG:
3281 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
3283 case OP_AMD64_COMPARE_MEMBASE_IMM:
3284 g_assert (amd64_is_imm32 (ins->inst_imm));
3285 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
3287 case OP_X86_COMPARE_MEMBASE8_IMM:
3288 amd64_alu_membase8_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3290 case OP_AMD64_ICOMPARE_REG_MEMBASE:
3291 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3293 case OP_AMD64_COMPARE_REG_MEMBASE:
3294 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
3297 case OP_AMD64_ADD_REG_MEMBASE:
3298 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
3300 case OP_AMD64_SUB_REG_MEMBASE:
3301 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
3303 case OP_AMD64_AND_REG_MEMBASE:
3304 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
3306 case OP_AMD64_OR_REG_MEMBASE:
3307 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
3309 case OP_AMD64_XOR_REG_MEMBASE:
3310 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
3313 case OP_AMD64_ADD_MEMBASE_REG:
3314 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
3316 case OP_AMD64_SUB_MEMBASE_REG:
3317 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
3319 case OP_AMD64_AND_MEMBASE_REG:
3320 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
3322 case OP_AMD64_OR_MEMBASE_REG:
3323 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
3325 case OP_AMD64_XOR_MEMBASE_REG:
3326 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
3329 case OP_AMD64_ADD_MEMBASE_IMM:
3330 g_assert (amd64_is_imm32 (ins->inst_imm));
3331 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
3333 case OP_AMD64_SUB_MEMBASE_IMM:
3334 g_assert (amd64_is_imm32 (ins->inst_imm));
3335 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
3337 case OP_AMD64_AND_MEMBASE_IMM:
3338 g_assert (amd64_is_imm32 (ins->inst_imm));
3339 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
3341 case OP_AMD64_OR_MEMBASE_IMM:
3342 g_assert (amd64_is_imm32 (ins->inst_imm));
3343 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
3345 case OP_AMD64_XOR_MEMBASE_IMM:
3346 g_assert (amd64_is_imm32 (ins->inst_imm));
3347 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
3351 amd64_breakpoint (code);
3353 case OP_RELAXED_NOP:
3354 x86_prefix (code, X86_REP_PREFIX);
3362 case OP_DUMMY_STORE:
3363 case OP_NOT_REACHED:
3368 amd64_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
3371 amd64_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
3375 g_assert (amd64_is_imm32 (ins->inst_imm));
3376 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
3379 g_assert (amd64_is_imm32 (ins->inst_imm));
3380 amd64_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
3384 amd64_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
3387 amd64_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
3391 g_assert (amd64_is_imm32 (ins->inst_imm));
3392 amd64_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
3395 g_assert (amd64_is_imm32 (ins->inst_imm));
3396 amd64_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
3399 amd64_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
3403 g_assert (amd64_is_imm32 (ins->inst_imm));
3404 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
3407 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
3412 guint32 size = (ins->opcode == OP_IMUL_IMM) ? 4 : 8;
3414 switch (ins->inst_imm) {
3418 if (ins->dreg != ins->sreg1)
3419 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, size);
3420 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
3423 /* LEA r1, [r2 + r2*2] */
3424 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
3427 /* LEA r1, [r2 + r2*4] */
3428 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3431 /* LEA r1, [r2 + r2*2] */
3433 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
3434 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
3437 /* LEA r1, [r2 + r2*8] */
3438 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
3441 /* LEA r1, [r2 + r2*4] */
3443 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3444 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
3447 /* LEA r1, [r2 + r2*2] */
3449 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
3450 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
3453 /* LEA r1, [r2 + r2*4] */
3454 /* LEA r1, [r1 + r1*4] */
3455 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3456 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
3459 /* LEA r1, [r2 + r2*4] */
3461 /* LEA r1, [r1 + r1*4] */
3462 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
3463 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
3464 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
3467 amd64_imul_reg_reg_imm_size (code, ins->dreg, ins->sreg1, ins->inst_imm, size);
3474 /* Regalloc magic makes the div/rem cases the same */
3475 if (ins->sreg2 == AMD64_RDX) {
3476 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
3478 amd64_div_membase (code, AMD64_RSP, -8, TRUE);
3481 amd64_div_reg (code, ins->sreg2, TRUE);
3486 if (ins->sreg2 == AMD64_RDX) {
3487 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
3488 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
3489 amd64_div_membase (code, AMD64_RSP, -8, FALSE);
3491 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
3492 amd64_div_reg (code, ins->sreg2, FALSE);
3497 if (ins->sreg2 == AMD64_RDX) {
3498 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
3499 amd64_cdq_size (code, 4);
3500 amd64_div_membase_size (code, AMD64_RSP, -8, TRUE, 4);
3502 amd64_cdq_size (code, 4);
3503 amd64_div_reg_size (code, ins->sreg2, TRUE, 4);
3508 if (ins->sreg2 == AMD64_RDX) {
3509 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
3510 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
3511 amd64_div_membase_size (code, AMD64_RSP, -8, FALSE, 4);
3513 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
3514 amd64_div_reg_size (code, ins->sreg2, FALSE, 4);
3518 int power = mono_is_power_of_two (ins->inst_imm);
3520 g_assert (ins->sreg1 == X86_EAX);
3521 g_assert (ins->dreg == X86_EAX);
3522 g_assert (power >= 0);
3524 /* Based on gcc code */
3526 /* Add compensation for negative dividents */
3527 amd64_mov_reg_reg_size (code, AMD64_RDX, AMD64_RAX, 4);
3529 amd64_shift_reg_imm_size (code, X86_SAR, AMD64_RDX, 31, 4);
3530 amd64_shift_reg_imm_size (code, X86_SHR, AMD64_RDX, 32 - power, 4);
3531 amd64_alu_reg_reg_size (code, X86_ADD, AMD64_RAX, AMD64_RDX, 4);
3532 /* Compute remainder */
3533 amd64_alu_reg_imm_size (code, X86_AND, AMD64_RAX, (1 << power) - 1, 4);
3534 /* Remove compensation */
3535 amd64_alu_reg_reg_size (code, X86_SUB, AMD64_RAX, AMD64_RDX, 4);
3539 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
3540 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
3543 amd64_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
3547 g_assert (amd64_is_imm32 (ins->inst_imm));
3548 amd64_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
3551 amd64_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
3555 g_assert (amd64_is_imm32 (ins->inst_imm));
3556 amd64_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
3559 g_assert (ins->sreg2 == AMD64_RCX);
3560 amd64_shift_reg (code, X86_SHL, ins->dreg);
3563 g_assert (ins->sreg2 == AMD64_RCX);
3564 amd64_shift_reg (code, X86_SAR, ins->dreg);
3567 g_assert (amd64_is_imm32 (ins->inst_imm));
3568 amd64_shift_reg_imm_size (code, X86_SAR, ins->dreg, ins->inst_imm, 4);
3571 g_assert (amd64_is_imm32 (ins->inst_imm));
3572 amd64_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
3575 g_assert (amd64_is_imm32 (ins->inst_imm));
3576 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
3578 case OP_LSHR_UN_IMM:
3579 g_assert (amd64_is_imm32 (ins->inst_imm));
3580 amd64_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
3583 g_assert (ins->sreg2 == AMD64_RCX);
3584 amd64_shift_reg (code, X86_SHR, ins->dreg);
3587 g_assert (amd64_is_imm32 (ins->inst_imm));
3588 amd64_shift_reg_imm_size (code, X86_SHL, ins->dreg, ins->inst_imm, 4);
3591 g_assert (amd64_is_imm32 (ins->inst_imm));
3592 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
3597 amd64_alu_reg_reg_size (code, X86_ADD, ins->sreg1, ins->sreg2, 4);
3600 amd64_alu_reg_reg_size (code, X86_ADC, ins->sreg1, ins->sreg2, 4);
3603 amd64_alu_reg_imm_size (code, X86_ADD, ins->dreg, ins->inst_imm, 4);
3606 amd64_alu_reg_imm_size (code, X86_ADC, ins->dreg, ins->inst_imm, 4);
3610 amd64_alu_reg_reg_size (code, X86_SUB, ins->sreg1, ins->sreg2, 4);
3613 amd64_alu_reg_reg_size (code, X86_SBB, ins->sreg1, ins->sreg2, 4);
3616 amd64_alu_reg_imm_size (code, X86_SUB, ins->dreg, ins->inst_imm, 4);
3619 amd64_alu_reg_imm_size (code, X86_SBB, ins->dreg, ins->inst_imm, 4);
3622 amd64_alu_reg_reg_size (code, X86_AND, ins->sreg1, ins->sreg2, 4);
3625 amd64_alu_reg_imm_size (code, X86_AND, ins->sreg1, ins->inst_imm, 4);
3628 amd64_alu_reg_reg_size (code, X86_OR, ins->sreg1, ins->sreg2, 4);
3631 amd64_alu_reg_imm_size (code, X86_OR, ins->sreg1, ins->inst_imm, 4);
3634 amd64_alu_reg_reg_size (code, X86_XOR, ins->sreg1, ins->sreg2, 4);
3637 amd64_alu_reg_imm_size (code, X86_XOR, ins->sreg1, ins->inst_imm, 4);
3640 amd64_neg_reg_size (code, ins->sreg1, 4);
3643 amd64_not_reg_size (code, ins->sreg1, 4);
3646 g_assert (ins->sreg2 == AMD64_RCX);
3647 amd64_shift_reg_size (code, X86_SHL, ins->dreg, 4);
3650 g_assert (ins->sreg2 == AMD64_RCX);
3651 amd64_shift_reg_size (code, X86_SAR, ins->dreg, 4);
3654 amd64_shift_reg_imm_size (code, X86_SAR, ins->dreg, ins->inst_imm, 4);
3656 case OP_ISHR_UN_IMM:
3657 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
3660 g_assert (ins->sreg2 == AMD64_RCX);
3661 amd64_shift_reg_size (code, X86_SHR, ins->dreg, 4);
3664 amd64_shift_reg_imm_size (code, X86_SHL, ins->dreg, ins->inst_imm, 4);
3667 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
3670 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
3671 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
3673 case OP_IMUL_OVF_UN:
3674 case OP_LMUL_OVF_UN: {
3675 /* the mul operation and the exception check should most likely be split */
3676 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
3677 int size = (ins->opcode == OP_IMUL_OVF_UN) ? 4 : 8;
3678 /*g_assert (ins->sreg2 == X86_EAX);
3679 g_assert (ins->dreg == X86_EAX);*/
3680 if (ins->sreg2 == X86_EAX) {
3681 non_eax_reg = ins->sreg1;
3682 } else if (ins->sreg1 == X86_EAX) {
3683 non_eax_reg = ins->sreg2;
3685 /* no need to save since we're going to store to it anyway */
3686 if (ins->dreg != X86_EAX) {
3688 amd64_push_reg (code, X86_EAX);
3690 amd64_mov_reg_reg (code, X86_EAX, ins->sreg1, size);
3691 non_eax_reg = ins->sreg2;
3693 if (ins->dreg == X86_EDX) {
3696 amd64_push_reg (code, X86_EAX);
3700 amd64_push_reg (code, X86_EDX);
3702 amd64_mul_reg_size (code, non_eax_reg, FALSE, size);
3703 /* save before the check since pop and mov don't change the flags */
3704 if (ins->dreg != X86_EAX)
3705 amd64_mov_reg_reg (code, ins->dreg, X86_EAX, size);
3707 amd64_pop_reg (code, X86_EDX);
3709 amd64_pop_reg (code, X86_EAX);
3710 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
3714 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
3716 case OP_ICOMPARE_IMM:
3717 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg1, ins->inst_imm, 4);
3739 EMIT_COND_BRANCH (ins, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
3747 case OP_CMOV_INE_UN:
3748 case OP_CMOV_IGE_UN:
3749 case OP_CMOV_IGT_UN:
3750 case OP_CMOV_ILE_UN:
3751 case OP_CMOV_ILT_UN:
3757 case OP_CMOV_LNE_UN:
3758 case OP_CMOV_LGE_UN:
3759 case OP_CMOV_LGT_UN:
3760 case OP_CMOV_LLE_UN:
3761 case OP_CMOV_LLT_UN:
3762 g_assert (ins->dreg == ins->sreg1);
3763 /* This needs to operate on 64 bit values */
3764 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);
3768 amd64_not_reg (code, ins->sreg1);
3771 amd64_neg_reg (code, ins->sreg1);
3776 if ((((guint64)ins->inst_c0) >> 32) == 0)
3777 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 4);
3779 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 8);
3782 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3783 amd64_mov_reg_membase (code, ins->dreg, AMD64_RIP, 0, 8);
3786 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3787 amd64_mov_reg_imm_size (code, ins->dreg, 0, 8);
3790 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, sizeof (gpointer));
3792 case OP_AMD64_SET_XMMREG_R4: {
3793 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
3796 case OP_AMD64_SET_XMMREG_R8: {
3797 if (ins->dreg != ins->sreg1)
3798 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
3804 * Note: this 'frame destruction' logic is useful for tail calls, too.
3805 * Keep in sync with the code in emit_epilog.
3809 /* FIXME: no tracing support... */
3810 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3811 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
3813 g_assert (!cfg->method->save_lmf);
3815 if (ins->opcode == OP_JMP)
3816 code = emit_load_volatile_arguments (cfg, code);
3818 if (cfg->arch.omit_fp) {
3819 guint32 save_offset = 0;
3820 /* Pop callee-saved registers */
3821 for (i = 0; i < AMD64_NREG; ++i)
3822 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
3823 amd64_mov_reg_membase (code, i, AMD64_RSP, save_offset, 8);
3826 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
3829 for (i = 0; i < AMD64_NREG; ++i)
3830 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i)))
3831 pos -= sizeof (gpointer);
3834 amd64_lea_membase (code, AMD64_RSP, AMD64_RBP, pos);
3836 /* Pop registers in reverse order */
3837 for (i = AMD64_NREG - 1; i > 0; --i)
3838 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
3839 amd64_pop_reg (code, i);
3845 offset = code - cfg->native_code;
3846 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
3847 if (cfg->compile_aot)
3848 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
3850 amd64_set_reg_template (code, AMD64_R11);
3851 amd64_jump_reg (code, AMD64_R11);
3855 /* ensure ins->sreg1 is not NULL */
3856 amd64_alu_membase_imm_size (code, X86_CMP, ins->sreg1, 0, 0, 4);
3859 amd64_lea_membase (code, AMD64_R11, cfg->frame_reg, cfg->sig_cookie);
3860 amd64_mov_membase_reg (code, ins->sreg1, 0, AMD64_R11, 8);
3869 call = (MonoCallInst*)ins;
3871 * The AMD64 ABI forces callers to know about varargs.
3873 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke))
3874 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
3875 else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
3877 * Since the unmanaged calling convention doesn't contain a
3878 * 'vararg' entry, we have to treat every pinvoke call as a
3879 * potential vararg call.
3883 for (i = 0; i < AMD64_XMM_NREG; ++i)
3884 if (call->used_fregs & (1 << i))
3887 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
3889 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
3892 if (ins->flags & MONO_INST_HAS_METHOD)
3893 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method, FALSE);
3895 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr, FALSE);
3896 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature->call_convention))
3897 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, call->stack_usage);
3898 code = emit_move_return_value (cfg, ins, code);
3904 case OP_VOIDCALL_REG:
3906 call = (MonoCallInst*)ins;
3908 if (AMD64_IS_ARGUMENT_REG (ins->sreg1)) {
3909 amd64_mov_reg_reg (code, AMD64_R11, ins->sreg1, 8);
3910 ins->sreg1 = AMD64_R11;
3914 * The AMD64 ABI forces callers to know about varargs.
3916 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke)) {
3917 if (ins->sreg1 == AMD64_RAX) {
3918 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
3919 ins->sreg1 = AMD64_R11;
3921 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
3922 } else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
3924 * Since the unmanaged calling convention doesn't contain a
3925 * 'vararg' entry, we have to treat every pinvoke call as a
3926 * potential vararg call.
3930 for (i = 0; i < AMD64_XMM_NREG; ++i)
3931 if (call->used_fregs & (1 << i))
3933 if (ins->sreg1 == AMD64_RAX) {
3934 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
3935 ins->sreg1 = AMD64_R11;
3938 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
3940 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
3943 amd64_call_reg (code, ins->sreg1);
3944 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature->call_convention))
3945 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, call->stack_usage);
3946 code = emit_move_return_value (cfg, ins, code);
3948 case OP_FCALL_MEMBASE:
3949 case OP_LCALL_MEMBASE:
3950 case OP_VCALL_MEMBASE:
3951 case OP_VCALL2_MEMBASE:
3952 case OP_VOIDCALL_MEMBASE:
3953 case OP_CALL_MEMBASE:
3954 call = (MonoCallInst*)ins;
3956 if (AMD64_IS_ARGUMENT_REG (ins->sreg1)) {
3958 * Can't use R11 because it is clobbered by the trampoline
3959 * code, and the reg value is needed by get_vcall_slot_addr.
3961 amd64_mov_reg_reg (code, AMD64_RAX, ins->sreg1, 8);
3962 ins->sreg1 = AMD64_RAX;
3965 if (call->method && ins->inst_offset < 0) {
3969 * This is a possible IMT call so save the IMT method in the proper
3970 * register. We don't use the generic code in method-to-ir.c, because
3971 * we need to disassemble this in get_vcall_slot_addr (), so we have to
3972 * maintain control over the layout of the code.
3973 * Also put the base reg in %rax to simplify find_imt_method ().
3975 if (ins->sreg1 != AMD64_RAX) {
3976 amd64_mov_reg_reg (code, AMD64_RAX, ins->sreg1, 8);
3977 ins->sreg1 = AMD64_RAX;
3979 val = (gssize)(gpointer)call->method;
3981 // FIXME: Generics sharing
3983 if ((((guint64)val) >> 32) == 0)
3984 amd64_mov_reg_imm_size (code, MONO_ARCH_IMT_REG, val, 4);
3986 amd64_mov_reg_imm_size (code, MONO_ARCH_IMT_REG, val, 8);
3990 amd64_call_membase (code, ins->sreg1, ins->inst_offset);
3991 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature->call_convention))
3992 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, call->stack_usage);
3993 code = emit_move_return_value (cfg, ins, code);
3995 case OP_AMD64_SAVE_SP_TO_LMF:
3996 amd64_mov_membase_reg (code, cfg->frame_reg, cfg->arch.lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
4000 amd64_push_reg (code, ins->sreg1);
4002 case OP_X86_PUSH_IMM:
4003 g_assert (amd64_is_imm32 (ins->inst_imm));
4004 amd64_push_imm (code, ins->inst_imm);
4006 case OP_X86_PUSH_MEMBASE:
4007 amd64_push_membase (code, ins->inst_basereg, ins->inst_offset);
4009 case OP_X86_PUSH_OBJ: {
4010 int size = ALIGN_TO (ins->inst_imm, 8);
4011 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
4012 amd64_push_reg (code, AMD64_RDI);
4013 amd64_push_reg (code, AMD64_RSI);
4014 amd64_push_reg (code, AMD64_RCX);
4015 if (ins->inst_offset)
4016 amd64_lea_membase (code, AMD64_RSI, ins->inst_basereg, ins->inst_offset);
4018 amd64_mov_reg_reg (code, AMD64_RSI, ins->inst_basereg, 8);
4019 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, (3 * 8));
4020 amd64_mov_reg_imm (code, AMD64_RCX, (size >> 3));
4022 amd64_prefix (code, X86_REP_PREFIX);
4024 amd64_pop_reg (code, AMD64_RCX);
4025 amd64_pop_reg (code, AMD64_RSI);
4026 amd64_pop_reg (code, AMD64_RDI);
4030 amd64_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
4032 case OP_X86_LEA_MEMBASE:
4033 amd64_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
4036 amd64_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
4039 /* keep alignment */
4040 amd64_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1);
4041 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_FRAME_ALIGNMENT - 1));
4042 code = mono_emit_stack_alloc (code, ins);
4043 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4045 case OP_LOCALLOC_IMM: {
4046 guint32 size = ins->inst_imm;
4047 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
4049 if (ins->flags & MONO_INST_INIT) {
4053 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
4054 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4056 for (i = 0; i < size; i += 8)
4057 amd64_mov_membase_reg (code, AMD64_RSP, i, ins->dreg, 8);
4058 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4060 amd64_mov_reg_imm (code, ins->dreg, size);
4061 ins->sreg1 = ins->dreg;
4063 code = mono_emit_stack_alloc (code, ins);
4064 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4067 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
4068 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4073 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
4074 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
4075 (gpointer)"mono_arch_throw_exception", FALSE);
4079 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
4080 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
4081 (gpointer)"mono_arch_rethrow_exception", FALSE);
4084 case OP_CALL_HANDLER:
4086 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
4087 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
4088 amd64_call_imm (code, 0);
4089 /* Restore stack alignment */
4090 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
4092 case OP_START_HANDLER: {
4093 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4094 amd64_mov_membase_reg (code, spvar->inst_basereg, spvar->inst_offset, AMD64_RSP, 8);
4097 case OP_ENDFINALLY: {
4098 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4099 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, 8);
4103 case OP_ENDFILTER: {
4104 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4105 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, 8);
4106 /* The local allocator will put the result into RAX */
4112 ins->inst_c0 = code - cfg->native_code;
4115 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
4116 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
4118 if (ins->flags & MONO_INST_BRLABEL) {
4119 if (ins->inst_i0->inst_c0) {
4120 amd64_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
4122 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
4123 if ((cfg->opt & MONO_OPT_BRANCH) &&
4124 x86_is_imm8 (ins->inst_i0->inst_c1 - cpos))
4125 x86_jump8 (code, 0);
4127 x86_jump32 (code, 0);
4130 if (ins->inst_target_bb->native_offset) {
4131 amd64_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
4133 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
4134 if ((cfg->opt & MONO_OPT_BRANCH) &&
4135 x86_is_imm8 (ins->inst_target_bb->max_offset - cpos))
4136 x86_jump8 (code, 0);
4138 x86_jump32 (code, 0);
4143 amd64_jump_reg (code, ins->sreg1);
4160 amd64_set_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
4161 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
4163 case OP_COND_EXC_EQ:
4164 case OP_COND_EXC_NE_UN:
4165 case OP_COND_EXC_LT:
4166 case OP_COND_EXC_LT_UN:
4167 case OP_COND_EXC_GT:
4168 case OP_COND_EXC_GT_UN:
4169 case OP_COND_EXC_GE:
4170 case OP_COND_EXC_GE_UN:
4171 case OP_COND_EXC_LE:
4172 case OP_COND_EXC_LE_UN:
4173 case OP_COND_EXC_IEQ:
4174 case OP_COND_EXC_INE_UN:
4175 case OP_COND_EXC_ILT:
4176 case OP_COND_EXC_ILT_UN:
4177 case OP_COND_EXC_IGT:
4178 case OP_COND_EXC_IGT_UN:
4179 case OP_COND_EXC_IGE:
4180 case OP_COND_EXC_IGE_UN:
4181 case OP_COND_EXC_ILE:
4182 case OP_COND_EXC_ILE_UN:
4183 EMIT_COND_SYSTEM_EXCEPTION (cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)], ins->inst_p1);
4185 case OP_COND_EXC_OV:
4186 case OP_COND_EXC_NO:
4188 case OP_COND_EXC_NC:
4189 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
4190 (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
4192 case OP_COND_EXC_IOV:
4193 case OP_COND_EXC_INO:
4194 case OP_COND_EXC_IC:
4195 case OP_COND_EXC_INC:
4196 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_IEQ],
4197 (ins->opcode < OP_COND_EXC_INE_UN), ins->inst_p1);
4200 /* floating point opcodes */
4202 double d = *(double *)ins->inst_p0;
4204 if ((d == 0.0) && (mono_signbit (d) == 0)) {
4205 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
4208 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
4209 amd64_sse_movsd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
4214 float f = *(float *)ins->inst_p0;
4216 if ((f == 0.0) && (mono_signbit (f) == 0)) {
4217 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
4220 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
4221 amd64_sse_movss_reg_membase (code, ins->dreg, AMD64_RIP, 0);
4222 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
4226 case OP_STORER8_MEMBASE_REG:
4227 amd64_sse_movsd_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
4229 case OP_LOADR8_SPILL_MEMBASE:
4230 g_assert_not_reached ();
4232 case OP_LOADR8_MEMBASE:
4233 amd64_sse_movsd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4235 case OP_STORER4_MEMBASE_REG:
4236 /* This requires a double->single conversion */
4237 amd64_sse_cvtsd2ss_reg_reg (code, AMD64_XMM15, ins->sreg1);
4238 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, AMD64_XMM15);
4240 case OP_LOADR4_MEMBASE:
4241 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4242 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
4244 case OP_ICONV_TO_R4: /* FIXME: change precision */
4245 case OP_ICONV_TO_R8:
4246 amd64_sse_cvtsi2sd_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
4248 case OP_LCONV_TO_R4: /* FIXME: change precision */
4249 case OP_LCONV_TO_R8:
4250 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
4252 case OP_FCONV_TO_R4:
4253 /* FIXME: nothing to do ?? */
4255 case OP_FCONV_TO_I1:
4256 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
4258 case OP_FCONV_TO_U1:
4259 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
4261 case OP_FCONV_TO_I2:
4262 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
4264 case OP_FCONV_TO_U2:
4265 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
4267 case OP_FCONV_TO_U4:
4268 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
4270 case OP_FCONV_TO_I4:
4272 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
4274 case OP_FCONV_TO_I8:
4275 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 8, TRUE);
4277 case OP_LCONV_TO_R_UN: {
4280 /* Based on gcc code */
4281 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
4282 br [0] = code; x86_branch8 (code, X86_CC_S, 0, TRUE);
4285 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
4286 br [1] = code; x86_jump8 (code, 0);
4287 amd64_patch (br [0], code);
4290 /* Save to the red zone */
4291 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RAX, 8);
4292 amd64_mov_membase_reg (code, AMD64_RSP, -16, AMD64_RCX, 8);
4293 amd64_mov_reg_reg (code, AMD64_RCX, ins->sreg1, 8);
4294 amd64_mov_reg_reg (code, AMD64_RAX, ins->sreg1, 8);
4295 amd64_alu_reg_imm (code, X86_AND, AMD64_RCX, 1);
4296 amd64_shift_reg_imm (code, X86_SHR, AMD64_RAX, 1);
4297 amd64_alu_reg_imm (code, X86_OR, AMD64_RAX, AMD64_RCX);
4298 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, AMD64_RAX);
4299 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->dreg);
4301 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
4302 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, -8, 8);
4303 amd64_patch (br [1], code);
4306 case OP_LCONV_TO_OVF_U4:
4307 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0);
4308 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_LT, TRUE, "OverflowException");
4309 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
4311 case OP_LCONV_TO_OVF_I4_UN:
4312 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0x7fffffff);
4313 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_GT, FALSE, "OverflowException");
4314 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
4317 if (ins->dreg != ins->sreg1)
4318 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
4321 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->sreg2);
4324 amd64_sse_subsd_reg_reg (code, ins->dreg, ins->sreg2);
4327 amd64_sse_mulsd_reg_reg (code, ins->dreg, ins->sreg2);
4330 amd64_sse_divsd_reg_reg (code, ins->dreg, ins->sreg2);
4333 static double r8_0 = -0.0;
4335 g_assert (ins->sreg1 == ins->dreg);
4337 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &r8_0);
4338 amd64_sse_xorpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
4342 EMIT_SSE2_FPFUNC (code, fsin, ins->dreg, ins->sreg1);
4345 EMIT_SSE2_FPFUNC (code, fcos, ins->dreg, ins->sreg1);
4348 static guint64 d = 0x7fffffffffffffffUL;
4350 g_assert (ins->sreg1 == ins->dreg);
4352 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &d);
4353 amd64_sse_andpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
4357 EMIT_SSE2_FPFUNC (code, fsqrt, ins->dreg, ins->sreg1);
4360 g_assert (cfg->opt & MONO_OPT_CMOV);
4361 g_assert (ins->dreg == ins->sreg1);
4362 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
4363 amd64_cmov_reg_size (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2, 4);
4366 g_assert (cfg->opt & MONO_OPT_CMOV);
4367 g_assert (ins->dreg == ins->sreg1);
4368 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
4369 amd64_cmov_reg_size (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2, 4);
4372 g_assert (cfg->opt & MONO_OPT_CMOV);
4373 g_assert (ins->dreg == ins->sreg1);
4374 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
4375 amd64_cmov_reg_size (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2, 4);
4378 g_assert (cfg->opt & MONO_OPT_CMOV);
4379 g_assert (ins->dreg == ins->sreg1);
4380 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
4381 amd64_cmov_reg_size (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2, 4);
4384 g_assert (cfg->opt & MONO_OPT_CMOV);
4385 g_assert (ins->dreg == ins->sreg1);
4386 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
4387 amd64_cmov_reg (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2);
4390 g_assert (cfg->opt & MONO_OPT_CMOV);
4391 g_assert (ins->dreg == ins->sreg1);
4392 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
4393 amd64_cmov_reg (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2);
4396 g_assert (cfg->opt & MONO_OPT_CMOV);
4397 g_assert (ins->dreg == ins->sreg1);
4398 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
4399 amd64_cmov_reg (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2);
4402 g_assert (cfg->opt & MONO_OPT_CMOV);
4403 g_assert (ins->dreg == ins->sreg1);
4404 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
4405 amd64_cmov_reg (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2);
4411 * The two arguments are swapped because the fbranch instructions
4412 * depend on this for the non-sse case to work.
4414 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
4417 /* zeroing the register at the start results in
4418 * shorter and faster code (we can also remove the widening op)
4420 guchar *unordered_check;
4421 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4422 amd64_sse_comisd_reg_reg (code, ins->sreg1, ins->sreg2);
4423 unordered_check = code;
4424 x86_branch8 (code, X86_CC_P, 0, FALSE);
4425 amd64_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
4426 amd64_patch (unordered_check, code);
4431 /* zeroing the register at the start results in
4432 * shorter and faster code (we can also remove the widening op)
4434 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4435 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
4436 if (ins->opcode == OP_FCLT_UN) {
4437 guchar *unordered_check = code;
4438 guchar *jump_to_end;
4439 x86_branch8 (code, X86_CC_P, 0, FALSE);
4440 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
4442 x86_jump8 (code, 0);
4443 amd64_patch (unordered_check, code);
4444 amd64_inc_reg (code, ins->dreg);
4445 amd64_patch (jump_to_end, code);
4447 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
4452 /* zeroing the register at the start results in
4453 * shorter and faster code (we can also remove the widening op)
4455 guchar *unordered_check;
4456 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4457 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
4458 if (ins->opcode == OP_FCGT) {
4459 unordered_check = code;
4460 x86_branch8 (code, X86_CC_P, 0, FALSE);
4461 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
4462 amd64_patch (unordered_check, code);
4464 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
4468 case OP_FCLT_MEMBASE:
4469 case OP_FCGT_MEMBASE:
4470 case OP_FCLT_UN_MEMBASE:
4471 case OP_FCGT_UN_MEMBASE:
4472 case OP_FCEQ_MEMBASE: {
4473 guchar *unordered_check, *jump_to_end;
4476 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4477 amd64_sse_comisd_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
4479 switch (ins->opcode) {
4480 case OP_FCEQ_MEMBASE:
4481 x86_cond = X86_CC_EQ;
4483 case OP_FCLT_MEMBASE:
4484 case OP_FCLT_UN_MEMBASE:
4485 x86_cond = X86_CC_LT;
4487 case OP_FCGT_MEMBASE:
4488 case OP_FCGT_UN_MEMBASE:
4489 x86_cond = X86_CC_GT;
4492 g_assert_not_reached ();
4495 unordered_check = code;
4496 x86_branch8 (code, X86_CC_P, 0, FALSE);
4497 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
4499 switch (ins->opcode) {
4500 case OP_FCEQ_MEMBASE:
4501 case OP_FCLT_MEMBASE:
4502 case OP_FCGT_MEMBASE:
4503 amd64_patch (unordered_check, code);
4505 case OP_FCLT_UN_MEMBASE:
4506 case OP_FCGT_UN_MEMBASE:
4508 x86_jump8 (code, 0);
4509 amd64_patch (unordered_check, code);
4510 amd64_inc_reg (code, ins->dreg);
4511 amd64_patch (jump_to_end, code);
4519 guchar *jump = code;
4520 x86_branch8 (code, X86_CC_P, 0, TRUE);
4521 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
4522 amd64_patch (jump, code);
4526 /* Branch if C013 != 100 */
4527 /* branch if !ZF or (PF|CF) */
4528 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
4529 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
4530 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
4533 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
4536 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
4537 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
4541 if (ins->opcode == OP_FBGT) {
4544 /* skip branch if C1=1 */
4546 x86_branch8 (code, X86_CC_P, 0, FALSE);
4547 /* branch if (C0 | C3) = 1 */
4548 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
4549 amd64_patch (br1, code);
4552 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
4556 /* Branch if C013 == 100 or 001 */
4559 /* skip branch if C1=1 */
4561 x86_branch8 (code, X86_CC_P, 0, FALSE);
4562 /* branch if (C0 | C3) = 1 */
4563 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
4564 amd64_patch (br1, code);
4568 /* Branch if C013 == 000 */
4569 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
4572 /* Branch if C013=000 or 100 */
4575 /* skip branch if C1=1 */
4577 x86_branch8 (code, X86_CC_P, 0, FALSE);
4578 /* branch if C0=0 */
4579 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
4580 amd64_patch (br1, code);
4584 /* Branch if C013 != 001 */
4585 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
4586 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
4589 /* Transfer value to the fp stack */
4590 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 16);
4591 amd64_movsd_membase_reg (code, AMD64_RSP, 0, ins->sreg1);
4592 amd64_fld_membase (code, AMD64_RSP, 0, TRUE);
4594 amd64_push_reg (code, AMD64_RAX);
4596 amd64_fnstsw (code);
4597 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0x4100);
4598 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, X86_FP_C0);
4599 amd64_pop_reg (code, AMD64_RAX);
4600 amd64_fstp (code, 0);
4601 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
4602 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 16);
4605 code = mono_amd64_emit_tls_get (code, ins->dreg, ins->inst_offset);
4608 case OP_MEMORY_BARRIER: {
4609 /* Not needed on amd64 */
4612 case OP_ATOMIC_ADD_I4:
4613 case OP_ATOMIC_ADD_I8: {
4614 int dreg = ins->dreg;
4615 guint32 size = (ins->opcode == OP_ATOMIC_ADD_I4) ? 4 : 8;
4617 if (dreg == ins->inst_basereg)
4620 if (dreg != ins->sreg2)
4621 amd64_mov_reg_reg (code, ins->dreg, ins->sreg2, size);
4623 x86_prefix (code, X86_LOCK_PREFIX);
4624 amd64_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, size);
4626 if (dreg != ins->dreg)
4627 amd64_mov_reg_reg (code, ins->dreg, dreg, size);
4631 case OP_ATOMIC_ADD_NEW_I4:
4632 case OP_ATOMIC_ADD_NEW_I8: {
4633 int dreg = ins->dreg;
4634 guint32 size = (ins->opcode == OP_ATOMIC_ADD_NEW_I4) ? 4 : 8;
4636 if ((dreg == ins->sreg2) || (dreg == ins->inst_basereg))
4639 amd64_mov_reg_reg (code, dreg, ins->sreg2, size);
4640 amd64_prefix (code, X86_LOCK_PREFIX);
4641 amd64_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, size);
4642 /* dreg contains the old value, add with sreg2 value */
4643 amd64_alu_reg_reg_size (code, X86_ADD, dreg, ins->sreg2, size);
4645 if (ins->dreg != dreg)
4646 amd64_mov_reg_reg (code, ins->dreg, dreg, size);
4650 case OP_ATOMIC_EXCHANGE_I4:
4651 case OP_ATOMIC_EXCHANGE_I8:
4652 case OP_ATOMIC_CAS_IMM_I4: {
4654 int sreg2 = ins->sreg2;
4655 int breg = ins->inst_basereg;
4657 gboolean need_push = FALSE, rdx_pushed = FALSE;
4659 if (ins->opcode == OP_ATOMIC_EXCHANGE_I8)
4665 * See http://msdn.microsoft.com/en-us/magazine/cc302329.aspx for
4666 * an explanation of how this works.
4669 /* cmpxchg uses eax as comperand, need to make sure we can use it
4670 * hack to overcome limits in x86 reg allocator
4671 * (req: dreg == eax and sreg2 != eax and breg != eax)
4673 g_assert (ins->dreg == AMD64_RAX);
4675 if (breg == AMD64_RAX && ins->sreg2 == AMD64_RAX)
4676 /* Highly unlikely, but possible */
4679 /* The pushes invalidate rsp */
4680 if ((breg == AMD64_RAX) || need_push) {
4681 amd64_mov_reg_reg (code, AMD64_R11, breg, 8);
4685 /* We need the EAX reg for the comparand */
4686 if (ins->sreg2 == AMD64_RAX) {
4687 if (breg != AMD64_R11) {
4688 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
4691 g_assert (need_push);
4692 amd64_push_reg (code, AMD64_RDX);
4693 amd64_mov_reg_reg (code, AMD64_RDX, AMD64_RAX, size);
4699 if (ins->opcode == OP_ATOMIC_CAS_IMM_I4) {
4700 if (ins->backend.data == NULL)
4701 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4703 amd64_mov_reg_imm (code, AMD64_RAX, ins->backend.data);
4705 amd64_prefix (code, X86_LOCK_PREFIX);
4706 amd64_cmpxchg_membase_reg_size (code, breg, ins->inst_offset, sreg2, size);
4708 amd64_mov_reg_membase (code, AMD64_RAX, breg, ins->inst_offset, size);
4710 br [0] = code; amd64_prefix (code, X86_LOCK_PREFIX);
4711 amd64_cmpxchg_membase_reg_size (code, breg, ins->inst_offset, sreg2, size);
4712 br [1] = code; amd64_branch8 (code, X86_CC_NE, -1, FALSE);
4713 amd64_patch (br [1], br [0]);
4717 amd64_pop_reg (code, AMD64_RDX);
4722 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
4723 g_assert_not_reached ();
4726 if ((code - cfg->native_code - offset) > max_len) {
4727 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %ld)",
4728 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
4729 g_assert_not_reached ();
4735 last_offset = offset;
4738 cfg->code_len = code - cfg->native_code;
4741 #endif /* DISABLE_JIT */
4744 mono_arch_register_lowlevel_calls (void)
4746 /* The signature doesn't matter */
4747 mono_register_jit_icall (mono_amd64_throw_exception, "mono_amd64_throw_exception", mono_create_icall_signature ("void"), TRUE);
4751 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
4753 MonoJumpInfo *patch_info;
4754 gboolean compile_aot = !run_cctors;
4756 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
4757 unsigned char *ip = patch_info->ip.i + code;
4758 unsigned char *target;
4760 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
4763 switch (patch_info->type) {
4764 case MONO_PATCH_INFO_BB:
4765 case MONO_PATCH_INFO_LABEL:
4768 /* No need to patch these */
4773 switch (patch_info->type) {
4774 case MONO_PATCH_INFO_NONE:
4776 case MONO_PATCH_INFO_METHOD_REL:
4777 case MONO_PATCH_INFO_R8:
4778 case MONO_PATCH_INFO_R4:
4779 g_assert_not_reached ();
4781 case MONO_PATCH_INFO_BB:
4788 * Debug code to help track down problems where the target of a near call is
4791 if (amd64_is_near_call (ip)) {
4792 gint64 disp = (guint8*)target - (guint8*)ip;
4794 if (!amd64_is_imm32 (disp)) {
4795 printf ("TYPE: %d\n", patch_info->type);
4796 switch (patch_info->type) {
4797 case MONO_PATCH_INFO_INTERNAL_METHOD:
4798 printf ("V: %s\n", patch_info->data.name);
4800 case MONO_PATCH_INFO_METHOD_JUMP:
4801 case MONO_PATCH_INFO_METHOD:
4802 printf ("V: %s\n", patch_info->data.method->name);
4810 amd64_patch (ip, (gpointer)target);
4815 get_max_epilog_size (MonoCompile *cfg)
4817 int max_epilog_size = 16;
4819 if (cfg->method->save_lmf)
4820 max_epilog_size += 256;
4822 if (mono_jit_trace_calls != NULL)
4823 max_epilog_size += 50;
4825 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
4826 max_epilog_size += 50;
4828 max_epilog_size += (AMD64_NREG * 2);
4830 return max_epilog_size;
4834 * This macro is used for testing whenever the unwinder works correctly at every point
4835 * where an async exception can happen.
4837 /* This will generate a SIGSEGV at the given point in the code */
4838 #define async_exc_point(code) do { \
4839 if (mono_inject_async_exc_method && mono_method_desc_full_match (mono_inject_async_exc_method, cfg->method)) { \
4840 if (cfg->arch.async_point_count == mono_inject_async_exc_pos) \
4841 amd64_mov_reg_mem (code, AMD64_RAX, 0, 4); \
4842 cfg->arch.async_point_count ++; \
4847 mono_arch_emit_prolog (MonoCompile *cfg)
4849 MonoMethod *method = cfg->method;
4851 MonoMethodSignature *sig;
4853 int alloc_size, pos, max_offset, i, cfa_offset, quad, max_epilog_size;
4856 gint32 lmf_offset = cfg->arch.lmf_offset;
4857 gboolean args_clobbered = FALSE;
4858 gboolean trace = FALSE;
4860 cfg->code_size = MAX (((MonoMethodNormal *)method)->header->code_size * 4, 10240);
4862 code = cfg->native_code = g_malloc (cfg->code_size);
4864 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
4867 /* Amount of stack space allocated by register saving code */
4870 /* Offset between RSP and the CFA */
4874 * The prolog consists of the following parts:
4876 * - push rbp, mov rbp, rsp
4877 * - save callee saved regs using pushes
4879 * - save rgctx if needed
4880 * - save lmf if needed
4883 * - save rgctx if needed
4884 * - save lmf if needed
4885 * - save callee saved regs using moves
4890 mono_emit_unwind_op_def_cfa (cfg, code, AMD64_RSP, 8);
4891 // IP saved at CFA - 8
4892 mono_emit_unwind_op_offset (cfg, code, AMD64_RIP, -cfa_offset);
4893 async_exc_point (code);
4895 if (!cfg->arch.omit_fp) {
4896 amd64_push_reg (code, AMD64_RBP);
4898 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
4899 mono_emit_unwind_op_offset (cfg, code, AMD64_RBP, - cfa_offset);
4900 async_exc_point (code);
4901 #ifdef PLATFORM_WIN32
4902 mono_arch_unwindinfo_add_push_nonvol (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
4905 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof (gpointer));
4906 mono_emit_unwind_op_def_cfa_reg (cfg, code, AMD64_RBP);
4907 async_exc_point (code);
4908 #ifdef PLATFORM_WIN32
4909 mono_arch_unwindinfo_add_set_fpreg (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
4913 /* Save callee saved registers */
4914 if (!cfg->arch.omit_fp && !method->save_lmf) {
4915 int offset = cfa_offset;
4917 for (i = 0; i < AMD64_NREG; ++i)
4918 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
4919 amd64_push_reg (code, i);
4920 pos += sizeof (gpointer);
4922 mono_emit_unwind_op_offset (cfg, code, i, - offset);
4923 async_exc_point (code);
4927 if (cfg->arch.omit_fp) {
4929 * On enter, the stack is misaligned by the the pushing of the return
4930 * address. It is either made aligned by the pushing of %rbp, or by
4933 alloc_size = ALIGN_TO (cfg->stack_offset, 8);
4934 if ((alloc_size % 16) == 0)
4937 alloc_size = ALIGN_TO (cfg->stack_offset, MONO_ARCH_FRAME_ALIGNMENT);
4942 cfg->arch.stack_alloc_size = alloc_size;
4944 /* Allocate stack frame */
4946 /* See mono_emit_stack_alloc */
4947 #if defined(PLATFORM_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
4948 guint32 remaining_size = alloc_size;
4949 while (remaining_size >= 0x1000) {
4950 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
4951 if (cfg->arch.omit_fp) {
4952 cfa_offset += 0x1000;
4953 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
4955 async_exc_point (code);
4956 #ifdef PLATFORM_WIN32
4957 if (cfg->arch.omit_fp)
4958 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, 0x1000);
4961 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
4962 remaining_size -= 0x1000;
4964 if (remaining_size) {
4965 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, remaining_size);
4966 if (cfg->arch.omit_fp) {
4967 cfa_offset += remaining_size;
4968 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
4969 async_exc_point (code);
4971 #ifdef PLATFORM_WIN32
4972 if (cfg->arch.omit_fp)
4973 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, remaining_size);
4977 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, alloc_size);
4978 if (cfg->arch.omit_fp) {
4979 cfa_offset += alloc_size;
4980 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
4981 async_exc_point (code);
4986 /* Stack alignment check */
4989 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_RSP, 8);
4990 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0xf);
4991 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, 0);
4992 x86_branch8 (code, X86_CC_EQ, 2, FALSE);
4993 amd64_breakpoint (code);
4998 if (method->save_lmf) {
5000 * The ip field is not set, the exception handling code will obtain it from the stack location pointed to by the sp field.
5002 /* sp is saved right before calls */
5003 /* Skip method (only needed for trampoline LMF frames) */
5004 /* Save callee saved regs */
5005 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbx), AMD64_RBX, 8);
5006 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbp), AMD64_RBP, 8);
5007 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r12), AMD64_R12, 8);
5008 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r13), AMD64_R13, 8);
5009 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r14), AMD64_R14, 8);
5010 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), AMD64_R15, 8);
5013 /* Save callee saved registers */
5014 if (cfg->arch.omit_fp && !method->save_lmf) {
5015 gint32 save_area_offset = cfg->arch.reg_save_area_offset;
5017 /* Save caller saved registers after sp is adjusted */
5018 /* The registers are saved at the bottom of the frame */
5019 /* FIXME: Optimize this so the regs are saved at the end of the frame in increasing order */
5020 for (i = 0; i < AMD64_NREG; ++i)
5021 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
5022 amd64_mov_membase_reg (code, AMD64_RSP, save_area_offset, i, 8);
5023 mono_emit_unwind_op_offset (cfg, code, i, - (cfa_offset - save_area_offset));
5024 save_area_offset += 8;
5025 async_exc_point (code);
5029 /* store runtime generic context */
5030 if (cfg->rgctx_var) {
5031 g_assert (cfg->rgctx_var->opcode == OP_REGOFFSET &&
5032 (cfg->rgctx_var->inst_basereg == AMD64_RBP || cfg->rgctx_var->inst_basereg == AMD64_RSP));
5034 amd64_mov_membase_reg (code, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, MONO_ARCH_RGCTX_REG, 8);
5037 /* compute max_offset in order to use short forward jumps */
5039 max_epilog_size = get_max_epilog_size (cfg);
5040 if (cfg->opt & MONO_OPT_BRANCH) {
5041 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5043 bb->max_offset = max_offset;
5045 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
5047 /* max alignment for loops */
5048 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
5049 max_offset += LOOP_ALIGNMENT;
5051 MONO_BB_FOR_EACH_INS (bb, ins) {
5052 if (ins->opcode == OP_LABEL)
5053 ins->inst_c1 = max_offset;
5055 max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
5058 if (mono_jit_trace_calls && bb == cfg->bb_exit)
5059 /* The tracing code can be quite large */
5060 max_offset += max_epilog_size;
5064 sig = mono_method_signature (method);
5067 cinfo = cfg->arch.cinfo;
5069 if (sig->ret->type != MONO_TYPE_VOID) {
5070 /* Save volatile arguments to the stack */
5071 if (cfg->vret_addr && (cfg->vret_addr->opcode != OP_REGVAR))
5072 amd64_mov_membase_reg (code, cfg->vret_addr->inst_basereg, cfg->vret_addr->inst_offset, cinfo->ret.reg, 8);
5075 /* Keep this in sync with emit_load_volatile_arguments */
5076 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
5077 ArgInfo *ainfo = cinfo->args + i;
5078 gint32 stack_offset;
5081 ins = cfg->args [i];
5083 if ((ins->flags & MONO_INST_IS_DEAD) && !trace)
5084 /* Unused arguments */
5087 if (sig->hasthis && (i == 0))
5088 arg_type = &mono_defaults.object_class->byval_arg;
5090 arg_type = sig->params [i - sig->hasthis];
5092 stack_offset = ainfo->offset + ARGS_OFFSET;
5094 if (cfg->globalra) {
5095 /* All the other moves are done by the register allocator */
5096 switch (ainfo->storage) {
5097 case ArgInFloatSSEReg:
5098 amd64_sse_cvtss2sd_reg_reg (code, ainfo->reg, ainfo->reg);
5100 case ArgValuetypeInReg:
5101 for (quad = 0; quad < 2; quad ++) {
5102 switch (ainfo->pair_storage [quad]) {
5104 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad], sizeof (gpointer));
5106 case ArgInFloatSSEReg:
5107 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad]);
5109 case ArgInDoubleSSEReg:
5110 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad]);
5115 g_assert_not_reached ();
5126 /* Save volatile arguments to the stack */
5127 if (ins->opcode != OP_REGVAR) {
5128 switch (ainfo->storage) {
5134 if (stack_offset & 0x1)
5136 else if (stack_offset & 0x2)
5138 else if (stack_offset & 0x4)
5143 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg, size);
5146 case ArgInFloatSSEReg:
5147 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
5149 case ArgInDoubleSSEReg:
5150 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
5152 case ArgValuetypeInReg:
5153 for (quad = 0; quad < 2; quad ++) {
5154 switch (ainfo->pair_storage [quad]) {
5156 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad], sizeof (gpointer));
5158 case ArgInFloatSSEReg:
5159 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad]);
5161 case ArgInDoubleSSEReg:
5162 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad]);
5167 g_assert_not_reached ();
5171 case ArgValuetypeAddrInIReg:
5172 if (ainfo->pair_storage [0] == ArgInIReg)
5173 amd64_mov_membase_reg (code, ins->inst_left->inst_basereg, ins->inst_left->inst_offset, ainfo->pair_regs [0], sizeof (gpointer));
5179 /* Argument allocated to (non-volatile) register */
5180 switch (ainfo->storage) {
5182 amd64_mov_reg_reg (code, ins->dreg, ainfo->reg, 8);
5185 amd64_mov_reg_membase (code, ins->dreg, AMD64_RBP, ARGS_OFFSET + ainfo->offset, 8);
5188 g_assert_not_reached ();
5193 /* Might need to attach the thread to the JIT or change the domain for the callback */
5194 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
5195 guint64 domain = (guint64)cfg->domain;
5197 args_clobbered = TRUE;
5200 * The call might clobber argument registers, but they are already
5201 * saved to the stack/global regs.
5203 if (appdomain_tls_offset != -1 && lmf_tls_offset != -1) {
5204 guint8 *buf, *no_domain_branch;
5206 code = mono_amd64_emit_tls_get (code, AMD64_RAX, appdomain_tls_offset);
5207 if ((domain >> 32) == 0)
5208 amd64_mov_reg_imm_size (code, AMD64_ARG_REG1, domain, 4);
5210 amd64_mov_reg_imm_size (code, AMD64_ARG_REG1, domain, 8);
5211 amd64_alu_reg_reg (code, X86_CMP, AMD64_RAX, AMD64_ARG_REG1);
5212 no_domain_branch = code;
5213 x86_branch8 (code, X86_CC_NE, 0, 0);
5214 code = mono_amd64_emit_tls_get ( code, AMD64_RAX, lmf_addr_tls_offset);
5215 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
5217 x86_branch8 (code, X86_CC_NE, 0, 0);
5218 amd64_patch (no_domain_branch, code);
5219 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
5220 (gpointer)"mono_jit_thread_attach", TRUE);
5221 amd64_patch (buf, code);
5222 #ifdef PLATFORM_WIN32
5223 /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
5224 /* FIXME: Add a separate key for LMF to avoid this */
5225 amd64_alu_reg_imm (code, X86_ADD, AMD64_RAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
5228 g_assert (!cfg->compile_aot);
5229 if ((domain >> 32) == 0)
5230 amd64_mov_reg_imm_size (code, AMD64_ARG_REG1, domain, 4);
5232 amd64_mov_reg_imm_size (code, AMD64_ARG_REG1, domain, 8);
5233 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
5234 (gpointer)"mono_jit_thread_attach", TRUE);
5238 if (method->save_lmf) {
5239 if ((lmf_tls_offset != -1) && !optimize_for_xen) {
5241 * Optimized version which uses the mono_lmf TLS variable instead of indirection
5242 * through the mono_lmf_addr TLS variable.
5244 /* %rax = previous_lmf */
5245 x86_prefix (code, X86_FS_PREFIX);
5246 amd64_mov_reg_mem (code, AMD64_RAX, lmf_tls_offset, 8);
5248 /* Save previous_lmf */
5249 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_RAX, 8);
5251 if (lmf_offset == 0) {
5252 x86_prefix (code, X86_FS_PREFIX);
5253 amd64_mov_mem_reg (code, lmf_tls_offset, cfg->frame_reg, 8);
5255 amd64_lea_membase (code, AMD64_R11, cfg->frame_reg, lmf_offset);
5256 x86_prefix (code, X86_FS_PREFIX);
5257 amd64_mov_mem_reg (code, lmf_tls_offset, AMD64_R11, 8);
5260 if (lmf_addr_tls_offset != -1) {
5261 /* Load lmf quicky using the FS register */
5262 code = mono_amd64_emit_tls_get (code, AMD64_RAX, lmf_addr_tls_offset);
5263 #ifdef PLATFORM_WIN32
5264 /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
5265 /* FIXME: Add a separate key for LMF to avoid this */
5266 amd64_alu_reg_imm (code, X86_ADD, AMD64_RAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
5271 * The call might clobber argument registers, but they are already
5272 * saved to the stack/global regs.
5274 args_clobbered = TRUE;
5275 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
5276 (gpointer)"mono_get_lmf_addr", TRUE);
5280 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), AMD64_RAX, 8);
5281 /* Save previous_lmf */
5282 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RAX, 0, 8);
5283 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_R11, 8);
5285 amd64_lea_membase (code, AMD64_R11, cfg->frame_reg, lmf_offset);
5286 amd64_mov_membase_reg (code, AMD64_RAX, 0, AMD64_R11, 8);
5291 args_clobbered = TRUE;
5292 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
5295 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
5296 args_clobbered = TRUE;
5299 * Optimize the common case of the first bblock making a call with the same
5300 * arguments as the method. This works because the arguments are still in their
5301 * original argument registers.
5302 * FIXME: Generalize this
5304 if (!args_clobbered) {
5305 MonoBasicBlock *first_bb = cfg->bb_entry;
5308 next = mono_bb_first_ins (first_bb);
5309 if (!next && first_bb->next_bb) {
5310 first_bb = first_bb->next_bb;
5311 next = mono_bb_first_ins (first_bb);
5314 if (first_bb->in_count > 1)
5317 for (i = 0; next && i < sig->param_count + sig->hasthis; ++i) {
5318 ArgInfo *ainfo = cinfo->args + i;
5319 gboolean match = FALSE;
5321 ins = cfg->args [i];
5322 if (ins->opcode != OP_REGVAR) {
5323 switch (ainfo->storage) {
5325 if (((next->opcode == OP_LOAD_MEMBASE) || (next->opcode == OP_LOADI4_MEMBASE)) && next->inst_basereg == ins->inst_basereg && next->inst_offset == ins->inst_offset) {
5326 if (next->dreg == ainfo->reg) {
5330 next->opcode = OP_MOVE;
5331 next->sreg1 = ainfo->reg;
5332 /* Only continue if the instruction doesn't change argument regs */
5333 if (next->dreg == ainfo->reg || next->dreg == AMD64_RAX)
5343 /* Argument allocated to (non-volatile) register */
5344 switch (ainfo->storage) {
5346 if (next->opcode == OP_MOVE && next->sreg1 == ins->dreg && next->dreg == ainfo->reg) {
5358 //next = mono_inst_list_next (&next->node, &first_bb->ins_list);
5365 cfg->code_len = code - cfg->native_code;
5367 g_assert (cfg->code_len < cfg->code_size);
5373 mono_arch_emit_epilog (MonoCompile *cfg)
5375 MonoMethod *method = cfg->method;
5378 int max_epilog_size;
5380 gint32 lmf_offset = cfg->arch.lmf_offset;
5382 max_epilog_size = get_max_epilog_size (cfg);
5384 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
5385 cfg->code_size *= 2;
5386 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
5387 mono_jit_stats.code_reallocs++;
5390 code = cfg->native_code + cfg->code_len;
5392 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
5393 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
5395 /* the code restoring the registers must be kept in sync with OP_JMP */
5398 if (method->save_lmf) {
5399 /* check if we need to restore protection of the stack after a stack overflow */
5400 if (mono_get_jit_tls_offset () != -1) {
5402 code = mono_amd64_emit_tls_get (code, X86_ECX, mono_get_jit_tls_offset ());
5403 /* we load the value in a separate instruction: this mechanism may be
5404 * used later as a safer way to do thread interruption
5406 amd64_mov_reg_membase (code, X86_ECX, X86_ECX, G_STRUCT_OFFSET (MonoJitTlsData, restore_stack_prot), 8);
5407 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
5409 x86_branch8 (code, X86_CC_Z, 0, FALSE);
5410 /* note that the call trampoline will preserve eax/edx */
5411 x86_call_reg (code, X86_ECX);
5412 x86_patch (patch, code);
5414 /* FIXME: maybe save the jit tls in the prolog */
5416 if ((lmf_tls_offset != -1) && !optimize_for_xen) {
5418 * Optimized version which uses the mono_lmf TLS variable instead of indirection
5419 * through the mono_lmf_addr TLS variable.
5421 /* reg = previous_lmf */
5422 amd64_mov_reg_membase (code, AMD64_R11, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 8);
5423 x86_prefix (code, X86_FS_PREFIX);
5424 amd64_mov_mem_reg (code, lmf_tls_offset, AMD64_R11, 8);
5426 /* Restore previous lmf */
5427 amd64_mov_reg_membase (code, AMD64_RCX, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 8);
5428 amd64_mov_reg_membase (code, AMD64_R11, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), 8);
5429 amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RCX, 8);
5432 /* Restore caller saved regs */
5433 if (cfg->used_int_regs & (1 << AMD64_RBP)) {
5434 amd64_mov_reg_membase (code, AMD64_RBP, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbp), 8);
5436 if (cfg->used_int_regs & (1 << AMD64_RBX)) {
5437 amd64_mov_reg_membase (code, AMD64_RBX, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbx), 8);
5439 if (cfg->used_int_regs & (1 << AMD64_R12)) {
5440 amd64_mov_reg_membase (code, AMD64_R12, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r12), 8);
5442 if (cfg->used_int_regs & (1 << AMD64_R13)) {
5443 amd64_mov_reg_membase (code, AMD64_R13, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r13), 8);
5445 if (cfg->used_int_regs & (1 << AMD64_R14)) {
5446 amd64_mov_reg_membase (code, AMD64_R14, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r14), 8);
5448 if (cfg->used_int_regs & (1 << AMD64_R15)) {
5449 amd64_mov_reg_membase (code, AMD64_R15, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), 8);
5453 if (cfg->arch.omit_fp) {
5454 gint32 save_area_offset = cfg->arch.reg_save_area_offset;
5456 for (i = 0; i < AMD64_NREG; ++i)
5457 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
5458 amd64_mov_reg_membase (code, i, AMD64_RSP, save_area_offset, 8);
5459 save_area_offset += 8;
5463 for (i = 0; i < AMD64_NREG; ++i)
5464 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i)))
5465 pos -= sizeof (gpointer);
5468 if (pos == - sizeof (gpointer)) {
5469 /* Only one register, so avoid lea */
5470 for (i = AMD64_NREG - 1; i > 0; --i)
5471 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
5472 amd64_mov_reg_membase (code, i, AMD64_RBP, pos, 8);
5476 amd64_lea_membase (code, AMD64_RSP, AMD64_RBP, pos);
5478 /* Pop registers in reverse order */
5479 for (i = AMD64_NREG - 1; i > 0; --i)
5480 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
5481 amd64_pop_reg (code, i);
5488 /* Load returned vtypes into registers if needed */
5489 cinfo = cfg->arch.cinfo;
5490 if (cinfo->ret.storage == ArgValuetypeInReg) {
5491 ArgInfo *ainfo = &cinfo->ret;
5492 MonoInst *inst = cfg->ret;
5494 for (quad = 0; quad < 2; quad ++) {
5495 switch (ainfo->pair_storage [quad]) {
5497 amd64_mov_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof (gpointer)), sizeof (gpointer));
5499 case ArgInFloatSSEReg:
5500 amd64_movss_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof (gpointer)));
5502 case ArgInDoubleSSEReg:
5503 amd64_movsd_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof (gpointer)));
5508 g_assert_not_reached ();
5513 if (cfg->arch.omit_fp) {
5514 if (cfg->arch.stack_alloc_size)
5515 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
5519 async_exc_point (code);
5522 cfg->code_len = code - cfg->native_code;
5524 g_assert (cfg->code_len < cfg->code_size);
5526 if (cfg->arch.omit_fp) {
5528 * Encode the stack size into used_int_regs so the exception handler
5531 g_assert (cfg->arch.stack_alloc_size < (1 << 16));
5532 cfg->used_int_regs |= (1 << 31) | (cfg->arch.stack_alloc_size << 16);
5537 mono_arch_emit_exceptions (MonoCompile *cfg)
5539 MonoJumpInfo *patch_info;
5542 MonoClass *exc_classes [16];
5543 guint8 *exc_throw_start [16], *exc_throw_end [16];
5544 guint32 code_size = 0;
5546 /* Compute needed space */
5547 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5548 if (patch_info->type == MONO_PATCH_INFO_EXC)
5550 if (patch_info->type == MONO_PATCH_INFO_R8)
5551 code_size += 8 + 15; /* sizeof (double) + alignment */
5552 if (patch_info->type == MONO_PATCH_INFO_R4)
5553 code_size += 4 + 15; /* sizeof (float) + alignment */
5556 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
5557 cfg->code_size *= 2;
5558 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
5559 mono_jit_stats.code_reallocs++;
5562 code = cfg->native_code + cfg->code_len;
5564 /* add code to raise exceptions */
5566 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5567 switch (patch_info->type) {
5568 case MONO_PATCH_INFO_EXC: {
5569 MonoClass *exc_class;
5573 amd64_patch (patch_info->ip.i + cfg->native_code, code);
5575 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
5576 g_assert (exc_class);
5577 throw_ip = patch_info->ip.i;
5579 //x86_breakpoint (code);
5580 /* Find a throw sequence for the same exception class */
5581 for (i = 0; i < nthrows; ++i)
5582 if (exc_classes [i] == exc_class)
5585 amd64_mov_reg_imm (code, AMD64_ARG_REG2, (exc_throw_end [i] - cfg->native_code) - throw_ip);
5586 x86_jump_code (code, exc_throw_start [i]);
5587 patch_info->type = MONO_PATCH_INFO_NONE;
5591 amd64_mov_reg_imm_size (code, AMD64_ARG_REG2, 0xf0f0f0f0, 4);
5595 exc_classes [nthrows] = exc_class;
5596 exc_throw_start [nthrows] = code;
5598 amd64_mov_reg_imm (code, AMD64_ARG_REG1, exc_class->type_token);
5600 patch_info->type = MONO_PATCH_INFO_NONE;
5602 code = emit_call_body (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_throw_corlib_exception");
5604 amd64_mov_reg_imm (buf, AMD64_ARG_REG2, (code - cfg->native_code) - throw_ip);
5609 exc_throw_end [nthrows] = code;
5621 /* Handle relocations with RIP relative addressing */
5622 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5623 gboolean remove = FALSE;
5625 switch (patch_info->type) {
5626 case MONO_PATCH_INFO_R8:
5627 case MONO_PATCH_INFO_R4: {
5630 /* The SSE opcodes require a 16 byte alignment */
5631 code = (guint8*)ALIGN_TO (code, 16);
5633 pos = cfg->native_code + patch_info->ip.i;
5635 if (IS_REX (pos [1]))
5636 *(guint32*)(pos + 5) = (guint8*)code - pos - 9;
5638 *(guint32*)(pos + 4) = (guint8*)code - pos - 8;
5640 if (patch_info->type == MONO_PATCH_INFO_R8) {
5641 *(double*)code = *(double*)patch_info->data.target;
5642 code += sizeof (double);
5644 *(float*)code = *(float*)patch_info->data.target;
5645 code += sizeof (float);
5656 if (patch_info == cfg->patch_info)
5657 cfg->patch_info = patch_info->next;
5661 for (tmp = cfg->patch_info; tmp->next != patch_info; tmp = tmp->next)
5663 tmp->next = patch_info->next;
5668 cfg->code_len = code - cfg->native_code;
5670 g_assert (cfg->code_len < cfg->code_size);
5675 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
5678 CallInfo *cinfo = NULL;
5679 MonoMethodSignature *sig;
5681 int i, n, stack_area = 0;
5683 /* Keep this in sync with mono_arch_get_argument_info */
5685 if (enable_arguments) {
5686 /* Allocate a new area on the stack and save arguments there */
5687 sig = mono_method_signature (cfg->method);
5689 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, FALSE);
5691 n = sig->param_count + sig->hasthis;
5693 stack_area = ALIGN_TO (n * 8, 16);
5695 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, stack_area);
5697 for (i = 0; i < n; ++i) {
5698 inst = cfg->args [i];
5700 if (inst->opcode == OP_REGVAR)
5701 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), inst->dreg, 8);
5703 amd64_mov_reg_membase (code, AMD64_R11, inst->inst_basereg, inst->inst_offset, 8);
5704 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), AMD64_R11, 8);
5709 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
5710 amd64_set_reg_template (code, AMD64_ARG_REG1);
5711 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RSP, 8);
5712 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
5714 if (enable_arguments)
5715 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, stack_area);
5729 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
5732 int save_mode = SAVE_NONE;
5733 MonoMethod *method = cfg->method;
5734 int rtype = mini_type_get_underlying_type (NULL, mono_method_signature (method)->ret)->type;
5737 case MONO_TYPE_VOID:
5738 /* special case string .ctor icall */
5739 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
5740 save_mode = SAVE_EAX;
5742 save_mode = SAVE_NONE;
5746 save_mode = SAVE_EAX;
5750 save_mode = SAVE_XMM;
5752 case MONO_TYPE_GENERICINST:
5753 if (!mono_type_generic_inst_is_valuetype (mono_method_signature (method)->ret)) {
5754 save_mode = SAVE_EAX;
5758 case MONO_TYPE_VALUETYPE:
5759 save_mode = SAVE_STRUCT;
5762 save_mode = SAVE_EAX;
5766 /* Save the result and copy it into the proper argument register */
5767 switch (save_mode) {
5769 amd64_push_reg (code, AMD64_RAX);
5771 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
5772 if (enable_arguments)
5773 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RAX, 8);
5777 if (enable_arguments)
5778 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
5781 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
5782 amd64_movsd_membase_reg (code, AMD64_RSP, 0, AMD64_XMM0);
5784 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
5786 * The result is already in the proper argument register so no copying
5793 g_assert_not_reached ();
5796 /* Set %al since this is a varargs call */
5797 if (save_mode == SAVE_XMM)
5798 amd64_mov_reg_imm (code, AMD64_RAX, 1);
5800 amd64_mov_reg_imm (code, AMD64_RAX, 0);
5802 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
5803 amd64_set_reg_template (code, AMD64_ARG_REG1);
5804 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
5806 /* Restore result */
5807 switch (save_mode) {
5809 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
5810 amd64_pop_reg (code, AMD64_RAX);
5816 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
5817 amd64_movsd_reg_membase (code, AMD64_XMM0, AMD64_RSP, 0);
5818 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
5823 g_assert_not_reached ();
5830 mono_arch_flush_icache (guint8 *code, gint size)
5836 mono_arch_flush_register_windows (void)
5841 mono_arch_is_inst_imm (gint64 imm)
5843 return amd64_is_imm32 (imm);
5847 * Determine whenever the trap whose info is in SIGINFO is caused by
5851 mono_arch_is_int_overflow (void *sigctx, void *info)
5858 mono_arch_sigctx_to_monoctx (sigctx, &ctx);
5860 rip = (guint8*)ctx.rip;
5862 if (IS_REX (rip [0])) {
5863 reg = amd64_rex_b (rip [0]);
5869 if ((rip [0] == 0xf7) && (x86_modrm_mod (rip [1]) == 0x3) && (x86_modrm_reg (rip [1]) == 0x7)) {
5871 reg += x86_modrm_rm (rip [1]);
5911 g_assert_not_reached ();
5923 mono_arch_get_patch_offset (guint8 *code)
5929 * mono_breakpoint_clean_code:
5931 * Copy @size bytes from @code - @offset to the buffer @buf. If the debugger inserted software
5932 * breakpoints in the original code, they are removed in the copy.
5934 * Returns TRUE if no sw breakpoint was present.
5937 mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size)
5940 gboolean can_write = TRUE;
5942 * If method_start is non-NULL we need to perform bound checks, since we access memory
5943 * at code - offset we could go before the start of the method and end up in a different
5944 * page of memory that is not mapped or read incorrect data anyway. We zero-fill the bytes
5947 if (!method_start || code - offset >= method_start) {
5948 memcpy (buf, code - offset, size);
5950 int diff = code - method_start;
5951 memset (buf, 0, size);
5952 memcpy (buf + offset - diff, method_start, diff + size - offset);
5955 for (i = 0; i < MONO_BREAKPOINT_ARRAY_SIZE; ++i) {
5956 int idx = mono_breakpoint_info_index [i];
5960 ptr = mono_breakpoint_info [idx].address;
5961 if (ptr >= code && ptr < code + size) {
5962 guint8 saved_byte = mono_breakpoint_info [idx].saved_byte;
5964 /*g_print ("patching %p with 0x%02x (was: 0x%02x)\n", ptr, saved_byte, buf [ptr - code]);*/
5965 buf [ptr - code] = saved_byte;
5972 mono_arch_get_vcall_slot (guint8 *code, gpointer *regs, int *displacement)
5979 mono_breakpoint_clean_code (NULL, code, 9, buf, sizeof (buf));
5984 /* go to the start of the call instruction
5986 * address_byte = (m << 6) | (o << 3) | reg
5987 * call opcode: 0xff address_byte displacement
5989 * 0xff m=2,o=2 imm32
5994 * A given byte sequence can match more than case here, so we have to be
5995 * really careful about the ordering of the cases. Longer sequences
5998 #ifdef MONO_ARCH_HAVE_IMT
5999 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)) {
6000 /* IMT-based interface calls: with MONO_ARCH_IMT_REG == r11
6001 * 41 bb 14 f8 28 08 mov $0x828f814,%r11d
6002 * ff 50 fc call *0xfffffffc(%rax)
6004 reg = amd64_modrm_rm (code [5]);
6005 disp = (signed char)code [6];
6006 /* R10 is clobbered by the IMT thunk code */
6007 g_assert (reg != AMD64_R10);
6013 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)) {
6015 * This is a interface call
6016 * 48 8b 80 f0 e8 ff ff mov 0xffffffffffffe8f0(%rax),%rax
6017 * ff 10 callq *(%rax)
6019 if (IS_REX (code [4]))
6021 reg = amd64_modrm_rm (code [6]);
6023 /* R10 is clobbered by the IMT thunk code */
6024 g_assert (reg != AMD64_R10);
6025 } else if ((code [0] == 0x41) && (code [1] == 0xff) && (code [2] == 0x15)) {
6026 /* call OFFSET(%rip) */
6027 disp = *(guint32*)(code + 3);
6028 return (gpointer*)(code + disp + 7);
6029 } 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)) {
6030 /* call *[r12+disp32] */
6031 if (IS_REX (code [-1]))
6034 disp = *(gint32*)(code + 3);
6035 } else if ((code [1] == 0xff) && (amd64_modrm_reg (code [2]) == 0x2) && (amd64_modrm_mod (code [2]) == 0x2)) {
6036 /* call *[reg+disp32] */
6037 if (IS_REX (code [0]))
6039 reg = amd64_modrm_rm (code [2]);
6040 disp = *(gint32*)(code + 3);
6041 /* R10 is clobbered by the IMT thunk code */
6042 g_assert (reg != AMD64_R10);
6043 } else if (code [2] == 0xe8) {
6046 } 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)) {
6047 /* call *[r12+disp32] */
6048 if (IS_REX (code [2]))
6051 disp = *(gint8*)(code + 6);
6052 } else if (IS_REX (code [4]) && (code [5] == 0xff) && (amd64_modrm_reg (code [6]) == 0x2) && (amd64_modrm_mod (code [6]) == 0x3)) {
6055 } else if ((code [4] == 0xff) && (amd64_modrm_reg (code [5]) == 0x2) && (amd64_modrm_mod (code [5]) == 0x1)) {
6056 /* call *[reg+disp8] */
6057 if (IS_REX (code [3]))
6059 reg = amd64_modrm_rm (code [5]);
6060 disp = *(gint8*)(code + 6);
6061 //printf ("B: [%%r%d+0x%x]\n", reg, disp);
6063 else if ((code [5] == 0xff) && (amd64_modrm_reg (code [6]) == 0x2) && (amd64_modrm_mod (code [6]) == 0x0)) {
6065 * This is a interface call: should check the above code can't catch it earlier
6066 * 8b 40 30 mov 0x30(%eax),%eax
6067 * ff 10 call *(%eax)
6069 if (IS_REX (code [4]))
6071 reg = amd64_modrm_rm (code [6]);
6075 g_assert_not_reached ();
6077 reg += amd64_rex_b (rex);
6079 /* R11 is clobbered by the trampoline code */
6080 g_assert (reg != AMD64_R11);
6082 *displacement = disp;
6087 mono_arch_get_vcall_slot_addr (guint8* code, gpointer *regs)
6091 vt = mono_arch_get_vcall_slot (code, regs, &displacement);
6094 return (gpointer*)((char*)vt + displacement);
6098 mono_arch_get_this_arg_reg (MonoMethodSignature *sig, MonoGenericSharingContext *gsctx, guint8 *code)
6100 int this_reg = AMD64_ARG_REG1;
6102 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
6106 gsctx = mono_get_generic_context_from_code (code);
6108 cinfo = get_call_info (gsctx, NULL, sig, FALSE);
6110 if (cinfo->ret.storage != ArgValuetypeInReg)
6111 this_reg = AMD64_ARG_REG2;
6119 mono_arch_get_this_arg_from_call (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, gssize *regs, guint8 *code)
6121 return (gpointer)regs [mono_arch_get_this_arg_reg (sig, gsctx, code)];
6124 #define MAX_ARCH_DELEGATE_PARAMS 10
6127 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
6129 guint8 *code, *start;
6132 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
6135 /* FIXME: Support more cases */
6136 if (MONO_TYPE_ISSTRUCT (sig->ret))
6140 static guint8* cached = NULL;
6145 start = code = mono_global_codeman_reserve (64);
6147 /* Replace the this argument with the target */
6148 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
6149 amd64_mov_reg_membase (code, AMD64_ARG_REG1, AMD64_RAX, G_STRUCT_OFFSET (MonoDelegate, target), 8);
6150 amd64_jump_membase (code, AMD64_RAX, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
6152 g_assert ((code - start) < 64);
6154 mono_debug_add_delegate_trampoline (start, code - start);
6156 mono_memory_barrier ();
6160 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
6161 for (i = 0; i < sig->param_count; ++i)
6162 if (!mono_is_regsize_var (sig->params [i]))
6164 if (sig->param_count > 4)
6167 code = cache [sig->param_count];
6171 start = code = mono_global_codeman_reserve (64);
6173 if (sig->param_count == 0) {
6174 amd64_jump_membase (code, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
6176 /* We have to shift the arguments left */
6177 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
6178 for (i = 0; i < sig->param_count; ++i) {
6179 #ifdef PLATFORM_WIN32
6181 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
6183 amd64_mov_reg_membase (code, param_regs [i], AMD64_RSP, 0x28, 8);
6185 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
6189 amd64_jump_membase (code, AMD64_RAX, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
6191 g_assert ((code - start) < 64);
6193 mono_debug_add_delegate_trampoline (start, code - start);
6195 mono_memory_barrier ();
6197 cache [sig->param_count] = start;
6204 * Support for fast access to the thread-local lmf structure using the GS
6205 * segment register on NPTL + kernel 2.6.x.
6208 static gboolean tls_offset_inited = FALSE;
6211 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
6213 if (!tls_offset_inited) {
6214 #ifdef PLATFORM_WIN32
6216 * We need to init this multiple times, since when we are first called, the key might not
6217 * be initialized yet.
6219 appdomain_tls_offset = mono_domain_get_tls_key ();
6220 lmf_tls_offset = mono_get_jit_tls_key ();
6221 thread_tls_offset = mono_thread_get_tls_key ();
6222 lmf_addr_tls_offset = mono_get_jit_tls_key ();
6224 /* Only 64 tls entries can be accessed using inline code */
6225 if (appdomain_tls_offset >= 64)
6226 appdomain_tls_offset = -1;
6227 if (lmf_tls_offset >= 64)
6228 lmf_tls_offset = -1;
6229 if (thread_tls_offset >= 64)
6230 thread_tls_offset = -1;
6232 tls_offset_inited = TRUE;
6234 optimize_for_xen = access ("/proc/xen", F_OK) == 0;
6236 appdomain_tls_offset = mono_domain_get_tls_offset ();
6237 lmf_tls_offset = mono_get_lmf_tls_offset ();
6238 lmf_addr_tls_offset = mono_get_lmf_addr_tls_offset ();
6239 thread_tls_offset = mono_thread_get_tls_offset ();
6245 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
6250 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
6252 MonoCallInst *call = (MonoCallInst*)inst;
6253 CallInfo * cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, inst->signature, FALSE);
6258 if (cinfo->ret.storage == ArgValuetypeInReg) {
6260 * The valuetype is in RAX:RDX after the call, need to be copied to
6261 * the stack. Save the address here, so the call instruction can
6264 MonoInst *loc = cfg->arch.vret_addr_loc;
6267 g_assert (loc->opcode == OP_REGOFFSET);
6269 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, loc->inst_basereg, loc->inst_offset, vt_reg);
6271 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
6272 vtarg->sreg1 = vt_reg;
6273 vtarg->dreg = mono_regstate_next_int (cfg->rs);
6274 mono_bblock_add_inst (cfg->cbb, vtarg);
6276 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
6280 /* add the this argument */
6281 if (this_reg != -1) {
6283 MONO_INST_NEW (cfg, this, OP_MOVE);
6284 this->type = this_type;
6285 this->sreg1 = this_reg;
6286 this->dreg = mono_regstate_next_int (cfg->rs);
6287 mono_bblock_add_inst (cfg->cbb, this);
6289 mono_call_inst_add_outarg_reg (cfg, call, this->dreg, cinfo->args [0].reg, FALSE);
6293 #ifdef MONO_ARCH_HAVE_IMT
6295 #define CMP_SIZE (6 + 1)
6296 #define CMP_REG_REG_SIZE (4 + 1)
6297 #define BR_SMALL_SIZE 2
6298 #define BR_LARGE_SIZE 6
6299 #define MOV_REG_IMM_SIZE 10
6300 #define MOV_REG_IMM_32BIT_SIZE 6
6301 #define JUMP_REG_SIZE (2 + 1)
6304 imt_branch_distance (MonoIMTCheckItem **imt_entries, int start, int target)
6306 int i, distance = 0;
6307 for (i = start; i < target; ++i)
6308 distance += imt_entries [i]->chunk_size;
6313 * LOCKING: called with the domain lock held
6316 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
6317 gpointer fail_tramp)
6321 guint8 *code, *start;
6322 gboolean vtable_is_32bit = ((gsize)(vtable) == (gsize)(int)(gsize)(vtable));
6324 for (i = 0; i < count; ++i) {
6325 MonoIMTCheckItem *item = imt_entries [i];
6326 if (item->is_equals) {
6327 if (item->check_target_idx) {
6328 if (!item->compare_done) {
6329 if (amd64_is_imm32 (item->key))
6330 item->chunk_size += CMP_SIZE;
6332 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
6334 if (vtable_is_32bit)
6335 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
6337 item->chunk_size += MOV_REG_IMM_SIZE;
6338 item->chunk_size += BR_SMALL_SIZE + JUMP_REG_SIZE;
6341 item->chunk_size += MOV_REG_IMM_SIZE * 3 + CMP_REG_REG_SIZE +
6342 BR_SMALL_SIZE + JUMP_REG_SIZE * 2;
6344 if (vtable_is_32bit)
6345 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
6347 item->chunk_size += MOV_REG_IMM_SIZE;
6348 item->chunk_size += JUMP_REG_SIZE;
6349 /* with assert below:
6350 * item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
6355 if (amd64_is_imm32 (item->key))
6356 item->chunk_size += CMP_SIZE;
6358 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
6359 item->chunk_size += BR_LARGE_SIZE;
6360 imt_entries [item->check_target_idx]->compare_done = TRUE;
6362 size += item->chunk_size;
6365 code = mono_method_alloc_generic_virtual_thunk (domain, size);
6367 code = mono_code_manager_reserve (domain->code_mp, size);
6369 for (i = 0; i < count; ++i) {
6370 MonoIMTCheckItem *item = imt_entries [i];
6371 item->code_target = code;
6372 if (item->is_equals) {
6373 if (item->check_target_idx) {
6374 if (!item->compare_done) {
6375 if (amd64_is_imm32 (item->key))
6376 amd64_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key);
6378 amd64_mov_reg_imm (code, AMD64_R10, item->key);
6379 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, AMD64_R10);
6382 item->jmp_code = code;
6383 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
6384 /* See the comment below about R10 */
6386 amd64_mov_reg_imm (code, AMD64_R10, item->value.target_code);
6387 amd64_jump_reg (code, AMD64_R10);
6389 amd64_mov_reg_imm (code, AMD64_R10, & (vtable->vtable [item->value.vtable_slot]));
6390 amd64_jump_membase (code, AMD64_R10, 0);
6394 if (amd64_is_imm32 (item->key))
6395 amd64_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key);
6397 amd64_mov_reg_imm (code, AMD64_R10, item->key);
6398 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, AMD64_R10);
6400 item->jmp_code = code;
6401 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
6402 amd64_mov_reg_imm (code, AMD64_R10, item->value.target_code);
6403 amd64_jump_reg (code, AMD64_R10);
6404 amd64_patch (item->jmp_code, code);
6405 amd64_mov_reg_imm (code, AMD64_R10, fail_tramp);
6406 amd64_jump_reg (code, AMD64_R10);
6407 item->jmp_code = NULL;
6410 /* enable the commented code to assert on wrong method */
6412 if (amd64_is_imm32 (item->key))
6413 amd64_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key);
6415 amd64_mov_reg_imm (code, AMD64_R10, item->key);
6416 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, AMD64_R10);
6418 item->jmp_code = code;
6419 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
6420 /* See the comment below about R10 */
6421 amd64_mov_reg_imm (code, AMD64_R10, & (vtable->vtable [item->value.vtable_slot]));
6422 amd64_jump_membase (code, AMD64_R10, 0);
6423 amd64_patch (item->jmp_code, code);
6424 amd64_breakpoint (code);
6425 item->jmp_code = NULL;
6427 /* We're using R10 here because R11
6428 needs to be preserved. R10 needs
6429 to be preserved for calls which
6430 require a runtime generic context,
6431 but interface calls don't. */
6432 amd64_mov_reg_imm (code, AMD64_R10, & (vtable->vtable [item->value.vtable_slot]));
6433 amd64_jump_membase (code, AMD64_R10, 0);
6438 if (amd64_is_imm32 (item->key))
6439 amd64_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key);
6441 amd64_mov_reg_imm (code, AMD64_R10, item->key);
6442 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, AMD64_R10);
6444 item->jmp_code = code;
6445 if (x86_is_imm8 (imt_branch_distance (imt_entries, i, item->check_target_idx)))
6446 x86_branch8 (code, X86_CC_GE, 0, FALSE);
6448 x86_branch32 (code, X86_CC_GE, 0, FALSE);
6450 g_assert (code - item->code_target <= item->chunk_size);
6452 /* patch the branches to get to the target items */
6453 for (i = 0; i < count; ++i) {
6454 MonoIMTCheckItem *item = imt_entries [i];
6455 if (item->jmp_code) {
6456 if (item->check_target_idx) {
6457 amd64_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
6463 mono_stats.imt_thunks_size += code - start;
6464 g_assert (code - start <= size);
6470 mono_arch_find_imt_method (gpointer *regs, guint8 *code)
6472 return regs [MONO_ARCH_IMT_REG];
6476 mono_arch_find_this_argument (gpointer *regs, MonoMethod *method, MonoGenericSharingContext *gsctx)
6478 return mono_arch_get_this_arg_from_call (gsctx, mono_method_signature (method), (gssize*)regs, NULL);
6482 mono_arch_emit_imt_argument (MonoCompile *cfg, MonoCallInst *call, MonoInst *imt_arg)
6484 /* Done by the implementation of the CALL_MEMBASE opcodes */
6489 mono_arch_find_static_call_vtable (gpointer *regs, guint8 *code)
6491 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
6495 mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
6497 MonoInst *ins = NULL;
6499 if (cmethod->klass == mono_defaults.math_class) {
6500 if (strcmp (cmethod->name, "Sin") == 0) {
6501 MONO_INST_NEW (cfg, ins, OP_SIN);
6502 ins->inst_i0 = args [0];
6503 } else if (strcmp (cmethod->name, "Cos") == 0) {
6504 MONO_INST_NEW (cfg, ins, OP_COS);
6505 ins->inst_i0 = args [0];
6506 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
6507 MONO_INST_NEW (cfg, ins, OP_SQRT);
6508 ins->inst_i0 = args [0];
6509 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
6510 MONO_INST_NEW (cfg, ins, OP_ABS);
6511 ins->inst_i0 = args [0];
6514 if (cfg->opt & MONO_OPT_CMOV) {
6517 if (strcmp (cmethod->name, "Min") == 0) {
6518 if (fsig->params [0]->type == MONO_TYPE_I4)
6520 if (fsig->params [0]->type == MONO_TYPE_U4)
6521 opcode = OP_IMIN_UN;
6522 else if (fsig->params [0]->type == MONO_TYPE_I8)
6524 else if (fsig->params [0]->type == MONO_TYPE_U8)
6525 opcode = OP_LMIN_UN;
6526 } else if (strcmp (cmethod->name, "Max") == 0) {
6527 if (fsig->params [0]->type == MONO_TYPE_I4)
6529 if (fsig->params [0]->type == MONO_TYPE_U4)
6530 opcode = OP_IMAX_UN;
6531 else if (fsig->params [0]->type == MONO_TYPE_I8)
6533 else if (fsig->params [0]->type == MONO_TYPE_U8)
6534 opcode = OP_LMAX_UN;
6538 MONO_INST_NEW (cfg, ins, opcode);
6539 ins->inst_i0 = args [0];
6540 ins->inst_i1 = args [1];
6545 /* OP_FREM is not IEEE compatible */
6546 else if (strcmp (cmethod->name, "IEEERemainder") == 0) {
6547 MONO_INST_NEW (cfg, ins, OP_FREM);
6548 ins->inst_i0 = args [0];
6549 ins->inst_i1 = args [1];
6558 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
6560 MonoInst *ins = NULL;
6563 if (cmethod->klass == mono_defaults.math_class) {
6564 if (strcmp (cmethod->name, "Sin") == 0) {
6566 } else if (strcmp (cmethod->name, "Cos") == 0) {
6568 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
6570 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
6575 MONO_INST_NEW (cfg, ins, opcode);
6576 ins->type = STACK_R8;
6577 ins->dreg = mono_alloc_freg (cfg);
6578 ins->sreg1 = args [0]->dreg;
6579 MONO_ADD_INS (cfg->cbb, ins);
6583 if (cfg->opt & MONO_OPT_CMOV) {
6584 if (strcmp (cmethod->name, "Min") == 0) {
6585 if (fsig->params [0]->type == MONO_TYPE_I4)
6587 if (fsig->params [0]->type == MONO_TYPE_U4)
6588 opcode = OP_IMIN_UN;
6589 else if (fsig->params [0]->type == MONO_TYPE_I8)
6591 else if (fsig->params [0]->type == MONO_TYPE_U8)
6592 opcode = OP_LMIN_UN;
6593 } else if (strcmp (cmethod->name, "Max") == 0) {
6594 if (fsig->params [0]->type == MONO_TYPE_I4)
6596 if (fsig->params [0]->type == MONO_TYPE_U4)
6597 opcode = OP_IMAX_UN;
6598 else if (fsig->params [0]->type == MONO_TYPE_I8)
6600 else if (fsig->params [0]->type == MONO_TYPE_U8)
6601 opcode = OP_LMAX_UN;
6606 MONO_INST_NEW (cfg, ins, opcode);
6607 ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
6608 ins->dreg = mono_alloc_ireg (cfg);
6609 ins->sreg1 = args [0]->dreg;
6610 ins->sreg2 = args [1]->dreg;
6611 MONO_ADD_INS (cfg->cbb, ins);
6615 /* OP_FREM is not IEEE compatible */
6616 else if (strcmp (cmethod->name, "IEEERemainder") == 0) {
6617 MONO_INST_NEW (cfg, ins, OP_FREM);
6618 ins->inst_i0 = args [0];
6619 ins->inst_i1 = args [1];
6625 * Can't implement CompareExchange methods this way since they have
6633 mono_arch_print_tree (MonoInst *tree, int arity)
6638 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
6642 if (appdomain_tls_offset == -1)
6645 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
6646 ins->inst_offset = appdomain_tls_offset;
6650 MonoInst* mono_arch_get_thread_intrinsic (MonoCompile* cfg)
6654 if (thread_tls_offset == -1)
6657 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
6658 ins->inst_offset = thread_tls_offset;
6662 #define _CTX_REG(ctx,fld,i) ((gpointer)((&ctx->fld)[i]))
6665 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
6668 case AMD64_RCX: return (gpointer)ctx->rcx;
6669 case AMD64_RDX: return (gpointer)ctx->rdx;
6670 case AMD64_RBX: return (gpointer)ctx->rbx;
6671 case AMD64_RBP: return (gpointer)ctx->rbp;
6672 case AMD64_RSP: return (gpointer)ctx->rsp;
6675 return _CTX_REG (ctx, rax, reg);
6677 return _CTX_REG (ctx, r12, reg - 12);
6679 g_assert_not_reached ();